Kernel, libc: Round up to nearest page-aligned size instead of down

This commit is contained in:
apio 2022-10-12 12:15:12 +02:00
parent a8eb7a6b66
commit e90b90c556
2 changed files with 9 additions and 8 deletions

View File

@ -5,10 +5,9 @@
#include "log/Log.h"
#include "memory/MemoryManager.h"
#include "memory/VMM.h"
#include "misc/utils.h"
#include <stddef.h>
// FIXME: Round size up instead of down. (use get_blocks_from_size)
#define MAP_FAIL(errno) 0xffffffffffffff00 | (unsigned char)(errno)
void sys_mmap(Context* context, void* address, size_t size, int flags)
@ -32,7 +31,8 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
return;
}
uint64_t offset = (uint64_t)address % PAGE_SIZE;
void* result = MemoryManager::get_pages_at((uint64_t)address - offset, size / PAGE_SIZE, real_flags);
void* result = MemoryManager::get_pages_at((uint64_t)address - offset,
Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
if (result)
{
kdbgln("mmap succeeded: %p", result);
@ -46,8 +46,9 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
return;
}
}
kdbgln("sys_mmap: %ld pages at any address, %s", size / PAGE_SIZE, real_flags & MAP_READ_WRITE ? "rw" : "ro");
void* result = MemoryManager::get_pages(size / PAGE_SIZE, real_flags);
kdbgln("sys_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)
{
kdbgln("mmap succeeded: %p", result);
@ -85,7 +86,7 @@ void sys_munmap(Context* context, void* address, size_t size)
return;
}
uint64_t offset = (uint64_t)address % PAGE_SIZE;
MemoryManager::release_pages((void*)((uint64_t)address - offset), size / PAGE_SIZE);
MemoryManager::release_pages((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size));
kdbgln("munmap succeeded");
context->rax = 0;
return;

View File

@ -17,11 +17,11 @@ extern "C"
{
#endif
/* Maps size bytes of memory (rounded down to the nearest page-aligned size) into the current process's address
/* Maps size bytes of memory (rounded up to the nearest page-aligned size) into the current process's address
* space at addr. If addr is null, the kernel will choose an address. */
void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset);
/* Unmaps size bytes of memory (rounded down to the nearest page-aligned size) at addr from the current process's
/* Unmaps size bytes of memory (rounded up to the nearest page-aligned size) at addr from the current process's
* address space. */
int munmap(void* addr, size_t size);