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