MemoryManager: Add copy_to_user() and copy_to_user_typed()
This commit is contained in:
parent
b851dcf9b9
commit
6e9b4491a6
@ -364,6 +364,35 @@ namespace MemoryManager
|
|||||||
return true;
|
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()
|
usize free()
|
||||||
{
|
{
|
||||||
return free_mem;
|
return free_mem;
|
||||||
|
@ -22,6 +22,13 @@ namespace MemoryManager
|
|||||||
|
|
||||||
bool validate_userspace_string(u64 address);
|
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<void> map_frames_at(u64 virt, u64 phys, usize count, int flags);
|
||||||
|
|
||||||
Result<u64> alloc_at(u64 virt, usize count, int flags);
|
Result<u64> alloc_at(u64 virt, usize count, int flags);
|
||||||
|
Loading…
Reference in New Issue
Block a user