From e90b90c5561f59066b8669fa9051b01c930e9a59 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 12 Oct 2022 12:15:12 +0200 Subject: [PATCH] Kernel, libc: Round up to nearest page-aligned size instead of down --- kernel/src/sys/mem.cpp | 13 +++++++------ libs/libc/include/sys/mman.h | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/kernel/src/sys/mem.cpp b/kernel/src/sys/mem.cpp index 1d046d00..0e946673 100644 --- a/kernel/src/sys/mem.cpp +++ b/kernel/src/sys/mem.cpp @@ -5,10 +5,9 @@ #include "log/Log.h" #include "memory/MemoryManager.h" #include "memory/VMM.h" +#include "misc/utils.h" #include -// 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; diff --git a/libs/libc/include/sys/mman.h b/libs/libc/include/sys/mman.h index e2ac6204..bcee7711 100644 --- a/libs/libc/include/sys/mman.h +++ b/libs/libc/include/sys/mman.h @@ -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);