Luna/kernel/src/sys/mem.cpp

105 lines
3.2 KiB
C++
Raw Normal View History

#define MODULE "mem"
#include "errno.h"
#include "interrupts/Context.h"
#include "log/Log.h"
#include "memory/MemoryManager.h"
#include "memory/VMM.h"
#include "misc/utils.h"
#include <stddef.h>
#define MAP_FAIL(errno) 0xffffffffffffff00 | (unsigned char)(errno)
void sys_mmap(Context* context, void* address, size_t size, int flags)
{
if (size < PAGE_SIZE)
{
2022-10-13 16:50:12 +00:00
kwarnln("mmap(): size too small");
context->rax = MAP_FAIL(EINVAL);
return;
}
if (size % PAGE_SIZE)
{
kwarnln("mmap(): size not a multiple of PAGE_SIZE");
context->rax = MAP_FAIL(EINVAL);
return;
}
int real_flags = MAP_USER;
if (flags & MAP_READ_WRITE) real_flags |= MAP_READ_WRITE;
if (address)
{
2022-10-13 16:50:12 +00:00
kdbgln("mmap(): %ld pages at address %p, %s", size / PAGE_SIZE, address,
real_flags & MAP_READ_WRITE ? "rw" : "ro");
2022-10-13 16:42:53 +00:00
if (VMM::get_physical((uint64_t)address) != (uint64_t)-1) // Address is already used.
{
2022-10-13 16:50:12 +00:00
kwarnln("attempt to map an already mapped address");
context->rax = MAP_FAIL(ENOMEM);
return;
}
uint64_t offset = (uint64_t)address % PAGE_SIZE;
void* result = MemoryManager::get_pages_at((uint64_t)address - offset,
Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
if (result)
{
2022-10-13 16:50:12 +00:00
kdbgln("mmap() succeeded: %p", result);
context->rax = (uint64_t)result;
return;
}
else
{
2022-10-13 16:50:12 +00:00
kwarnln("mmap() failed");
context->rax = MAP_FAIL(ENOMEM);
return;
}
}
2022-10-13 16:50:12 +00:00
kdbgln("mmap(): %ld pages at any address, %s", Utilities::get_blocks_from_size(PAGE_SIZE, size),
real_flags & MAP_READ_WRITE ? "rw" : "ro");
void* result = MemoryManager::get_pages(Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
if (result)
{
2022-10-13 16:50:12 +00:00
kdbgln("mmap() succeeded: %p", result);
context->rax = (uint64_t)result;
return;
}
else
{
2022-10-13 16:50:12 +00:00
kwarnln("mmap() failed");
context->rax = MAP_FAIL(ENOMEM);
return;
}
}
void sys_munmap(Context* context, void* address, size_t size)
{
2022-10-13 16:50:12 +00:00
kdbgln("munmap(): attempting to unmap %p", address);
if (size < PAGE_SIZE)
{
2022-10-13 16:50:12 +00:00
kwarnln("munmap() failed: size is too small");
context->rax = -EINVAL;
return;
}
if (size % PAGE_SIZE)
{
kwarnln("munmap() failed: size is not a multiple of PAGE_SIZE");
context->rax = -EINVAL;
return;
}
if (!address)
{
2022-10-13 16:50:12 +00:00
kwarnln("munmap() failed: attempted to unmap page 0");
context->rax = -EINVAL;
return;
}
2022-10-13 16:42:53 +00:00
uint64_t flags = VMM::get_flags((uint64_t)address);
if (!(flags & MAP_USER))
{
2022-10-13 16:50:12 +00:00
kwarnln("munmap() failed: attempted to unmap a non-existent or kernel page");
context->rax = -EINVAL;
return;
}
uint64_t offset = (uint64_t)address % PAGE_SIZE;
MemoryManager::release_pages((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size));
2022-10-13 16:50:12 +00:00
kdbgln("munmap() succeeded");
context->rax = 0;
return;
}