MemoryManager: Add copy_to_user() and copy_to_user_typed()

This commit is contained in:
apio 2023-01-07 00:17:16 +01:00
parent b851dcf9b9
commit 6e9b4491a6
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 36 additions and 0 deletions

View File

@ -364,6 +364,35 @@ namespace MemoryManager
return true;
}
bool copy_to_user(void* user, const void* kernel, usize size)
{
uintptr_t user_ptr = (uintptr_t)user;
uintptr_t user_page = align_down<ARCH_PAGE_SIZE>(user_ptr);
const char* kernel_ptr = (const char*)kernel;
// Userspace pointer not aligned on page boundary
if (user_ptr != user_page)
{
// FIXME: Validate that this page is writable by the user, not just the kernel.
if (!validate_writable_page(user_page)) return false;
}
while (size--)
{
// Crossed a page boundary, gotta check the page tables again before touching any memory!!
if (user_ptr % ARCH_PAGE_SIZE)
{
if (!validate_writable_page(user_ptr)) return false;
}
*(char*)user_ptr = *kernel_ptr++;
user_ptr++;
}
return true;
}
usize free()
{
return free_mem;

View File

@ -22,6 +22,13 @@ namespace MemoryManager
bool validate_userspace_string(u64 address);
bool copy_to_user(void* user, const void* kernel, usize size);
template <typename T> bool copy_to_user_typed(T* user, const T* kernel)
{
return copy_to_user(user, kernel, sizeof(T));
}
Result<void> map_frames_at(u64 virt, u64 phys, usize count, int flags);
Result<u64> alloc_at(u64 virt, usize count, int flags);