2020-09-12 03:11:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2021-09-04 21:48:46 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2020-09-12 03:11:07 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-12 03:11:07 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2020-09-12 03:11:07 +00:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
bool UserOrKernelBuffer::is_kernel_buffer() const
|
|
|
|
{
|
2021-08-06 11:49:36 +00:00
|
|
|
return !Memory::is_user_address(VirtualAddress(m_buffer));
|
2020-09-12 03:11:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> UserOrKernelBuffer::try_copy_into_kstring(size_t size) const
|
2021-07-17 20:07:49 +00:00
|
|
|
{
|
|
|
|
if (!m_buffer)
|
|
|
|
return EINVAL;
|
2021-08-06 11:49:36 +00:00
|
|
|
if (Memory::is_user_address(VirtualAddress(m_buffer))) {
|
2021-07-17 20:07:49 +00:00
|
|
|
char* buffer;
|
2021-09-06 17:24:54 +00:00
|
|
|
auto kstring = TRY(KString::try_create_uninitialized(size, buffer));
|
2021-09-05 15:38:37 +00:00
|
|
|
TRY(copy_from_user(buffer, m_buffer, size));
|
2021-09-06 17:24:54 +00:00
|
|
|
return kstring;
|
2021-07-17 20:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 17:24:54 +00:00
|
|
|
return KString::try_create(ReadonlyBytes { m_buffer, size });
|
2021-07-17 20:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> UserOrKernelBuffer::write(void const* src, size_t offset, size_t len)
|
2020-09-12 03:11:07 +00:00
|
|
|
{
|
|
|
|
if (!m_buffer)
|
2021-09-07 10:09:52 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
if (Memory::is_user_address(VirtualAddress(m_buffer)))
|
2021-09-07 10:09:52 +00:00
|
|
|
return copy_to_user(m_buffer + offset, src, len);
|
2020-09-12 03:11:07 +00:00
|
|
|
|
|
|
|
memcpy(m_buffer + offset, src, len);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-09-12 03:11:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> UserOrKernelBuffer::read(void* dest, size_t offset, size_t len) const
|
2020-09-12 03:11:07 +00:00
|
|
|
{
|
|
|
|
if (!m_buffer)
|
2021-09-07 10:09:52 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
if (Memory::is_user_address(VirtualAddress(m_buffer)))
|
2021-09-07 10:09:52 +00:00
|
|
|
return copy_from_user(dest, m_buffer + offset, len);
|
2020-09-12 03:11:07 +00:00
|
|
|
|
|
|
|
memcpy(dest, m_buffer + offset, len);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-09-12 03:11:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> UserOrKernelBuffer::memset(int value, size_t offset, size_t len)
|
2020-09-12 03:11:07 +00:00
|
|
|
{
|
|
|
|
if (!m_buffer)
|
2021-09-07 10:09:52 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
if (Memory::is_user_address(VirtualAddress(m_buffer)))
|
2021-09-07 10:09:52 +00:00
|
|
|
return memset_user(m_buffer + offset, value, len);
|
2020-09-22 11:11:05 +00:00
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
::memset(m_buffer + offset, value, len);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-09-12 03:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|