Kernel: Add copy_from_user() and copy_from_user_typed()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-11 19:25:28 +01:00
parent 73ddd0b0c5
commit 6e4cd6300d
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 35 additions and 1 deletions

View File

@ -438,7 +438,6 @@ namespace MemoryManager
// 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_user_writable_page(user_page)) return false;
}
@ -457,6 +456,34 @@ namespace MemoryManager
return true;
}
bool copy_from_user(const void* user, void* kernel, usize size)
{
uintptr_t user_ptr = (uintptr_t)user;
uintptr_t user_page = align_down<ARCH_PAGE_SIZE>(user_ptr);
char* kernel_ptr = (char*)kernel;
// Userspace pointer not aligned on page boundary
if (user_ptr != user_page)
{
if (!validate_user_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_user_writable_page(user_ptr)) return false;
}
*kernel_ptr++ = *(char*)user_ptr;
user_ptr++;
}
return true;
}
usize free()
{
return free_mem;

View File

@ -45,6 +45,13 @@ namespace MemoryManager
return copy_to_user(user, kernel, sizeof(T));
}
bool copy_from_user(const void* user, void* kernel, usize size);
template <typename T> bool copy_from_user_typed(const T* user, T* kernel)
{
return copy_from_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);