Luna/kernel/include/sys/UserMemory.h
apio b63a9f5ba9 Kernel: Improve strdup_from_user and add copy to and from user
Still a bit funky, that's why we're not using it in read() and write() yet.
2022-11-05 11:54:55 +01:00

51 lines
1.6 KiB
C++

#pragma once
#ifndef MODULE
#define MODULE "mem"
#endif
#include "log/Log.h"
#include "memory/MemoryManager.h"
#include "memory/VMM.h"
#include "misc/utils.h"
#include <stddef.h>
char* strdup_from_user(const char* user_string);
bool validate_user_readable_page(uintptr_t address);
bool validate_user_writable_page(uintptr_t address);
bool validate_user_read(uintptr_t address, size_t size);
bool validate_user_write(uintptr_t address, size_t size);
bool copy_from_user(const void* user_ptr, void* ptr, size_t size);
bool copy_to_user(void* user_ptr, const void* ptr, size_t size);
// FIXME: Map the physical addresses into kernel address space. Right now, something overwrites KernelHeap and crashes
// it, so that's not really possible. But it should be done in the future.
template <typename T, unsigned long S = sizeof(T), typename V> T* user_address_to_typed_pointer(V address)
{
uint64_t phys = VMM::get_physical((uint64_t)address);
if (phys == (uint64_t)-1)
{
kinfoln("warning: user pointer is not mapped in its address space");
return nullptr;
}
// return (T*)MemoryManager::get_unaligned_mappings((void*)phys, Utilities::get_blocks_from_size(PAGE_SIZE, S),
// MAP_READ_WRITE);
return (T*)phys;
}
template <typename T, unsigned long S = sizeof(T)> void free_user_typed_pointer(T*)
{
// MemoryManager::release_unaligned_mappings(ptr, Utilities::get_blocks_from_size(PAGE_SIZE, S));
}
template <typename T> T* obtain_user_ref(T* user_ptr)
{
return user_address_to_typed_pointer<T>(user_ptr);
}
template <typename T> void release_user_ref(T* ptr)
{
return free_user_typed_pointer(ptr);
}