From 2c9329928c5133577c7b184d7bb56591554a846d Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 16 Nov 2022 20:37:32 +0100 Subject: [PATCH] Replace page_size (function) with ARCH_PAGE_SIZE (constant) --- kernel/src/MemoryManager.cpp | 44 +++++++++++++++++----------------- kernel/src/arch/MMU.h | 4 ++-- kernel/src/arch/x86_64/MMU.cpp | 6 ++--- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/kernel/src/MemoryManager.cpp b/kernel/src/MemoryManager.cpp index 63153a92..e089953a 100644 --- a/kernel/src/MemoryManager.cpp +++ b/kernel/src/MemoryManager.cpp @@ -43,10 +43,10 @@ namespace MemoryManager { Result protect_kernel_sections() { - u64 rodata_pages = get_blocks_from_size((u64)(end_of_kernel_rodata - start_of_kernel_rodata), MMU::page_size()); + u64 rodata_pages = get_blocks_from_size((u64)(end_of_kernel_rodata - start_of_kernel_rodata), ARCH_PAGE_SIZE); TRY(remap((u64)start_of_kernel_rodata, rodata_pages, MMU::NoExecute)); - u64 data_pages = get_blocks_from_size((u64)(end_of_kernel_data - start_of_kernel_data), MMU::page_size()); + u64 data_pages = get_blocks_from_size((u64)(end_of_kernel_data - start_of_kernel_data), ARCH_PAGE_SIZE); TRY(remap((u64)start_of_kernel_data, data_pages, MMU::NoExecute | MMU::ReadWrite)); return {}; @@ -81,25 +81,25 @@ namespace MemoryManager page_bitmap_addr = (char*)biggest_memory_block; page_virtual_bitmap_addr = page_bitmap_addr; // we'll map this to virtual memory as soon as the MMU is ready - if ((total_mem / MMU::page_size() / 8) >= biggest_memory_block_size) + if ((total_mem / ARCH_PAGE_SIZE / 8) >= biggest_memory_block_size) { Serial::println("ERROR: No single memory block is enough to hold the page bitmap"); for (;;) ; } - page_bitmap_size = total_mem / MMU::page_size() / 8 + 1; + page_bitmap_size = total_mem / ARCH_PAGE_SIZE / 8 + 1; memset(page_bitmap_addr, 0xFF, page_bitmap_size); ptr = &bootboot.mmap; for (uint64_t i = 0; i < mmap_entries; i++) { - uint64_t index = MMapEnt_Ptr(ptr) / MMU::page_size(); + uint64_t index = MMapEnt_Ptr(ptr) / ARCH_PAGE_SIZE; if (!MMapEnt_IsFree(ptr)) { reserved_mem += MMapEnt_Size(ptr); } else { free_mem += MMapEnt_Size(ptr); - for (uint64_t j = 0; j < (MMapEnt_Size(ptr) / MMU::page_size()); j++) + for (uint64_t j = 0; j < (MMapEnt_Size(ptr) / ARCH_PAGE_SIZE); j++) { page_bitmap_set(index + j, false); } @@ -107,7 +107,7 @@ namespace MemoryManager ptr++; } - lock_pages((u64)page_bitmap_addr, page_bitmap_size / MMU::page_size() + 1); + lock_pages((u64)page_bitmap_addr, page_bitmap_size / ARCH_PAGE_SIZE + 1); } void init() @@ -118,16 +118,16 @@ namespace MemoryManager void lock_page(u64 page) { - uint64_t index = ((uint64_t)page) / MMU::page_size(); + uint64_t index = ((uint64_t)page) / ARCH_PAGE_SIZE; if (page_bitmap_read(index)) return; page_bitmap_set(index, true); - used_mem += MMU::page_size(); - free_mem -= MMU::page_size(); + used_mem += ARCH_PAGE_SIZE; + free_mem -= ARCH_PAGE_SIZE; } void lock_pages(u64 pages, u64 count) { - for (u64 index = 0; index < count; index++) { lock_page(pages + (index * MMU::page_size())); } + for (u64 index = 0; index < count; index++) { lock_page(pages + (index * ARCH_PAGE_SIZE)); } } Result alloc_physical_page() @@ -137,9 +137,9 @@ namespace MemoryManager if (page_bitmap_read(index)) continue; page_bitmap_set(index, true); start_index = index + 1; - free_mem -= MMU::page_size(); - used_mem += MMU::page_size(); - return index * MMU::page_size(); + free_mem -= ARCH_PAGE_SIZE; + used_mem += ARCH_PAGE_SIZE; + return index * ARCH_PAGE_SIZE; } return err; // FIXME: ENOMEM. @@ -147,24 +147,24 @@ namespace MemoryManager Result free_physical_page(u64 page) { - u64 index = page / MMU::page_size(); + u64 index = page / ARCH_PAGE_SIZE; if (index > (page_bitmap_size * 8)) return err; if (!page_bitmap_read(index)) return err; page_bitmap_set(index, false); - used_mem -= MMU::page_size(); - free_mem += MMU::page_size(); + used_mem -= ARCH_PAGE_SIZE; + free_mem += ARCH_PAGE_SIZE; if (start_index > index) start_index = index; return {}; } Result remap(u64 address, usize count, int flags) { - check(is_aligned(address, MMU::page_size())); + check(is_aligned(address, ARCH_PAGE_SIZE)); while (count--) { TRY(MMU::remap(address, flags)); - address += MMU::page_size(); + address += ARCH_PAGE_SIZE; } return {}; @@ -172,13 +172,13 @@ namespace MemoryManager Result remap_unaligned(u64 address, usize count, int flags) { - if (!is_aligned(address, MMU::page_size())) count++; - address = align_down(address, MMU::page_size()); + if (!is_aligned(address, ARCH_PAGE_SIZE)) count++; + address = align_down(address, ARCH_PAGE_SIZE); while (count--) { TRY(MMU::remap(address, flags)); - address += MMU::page_size(); + address += ARCH_PAGE_SIZE; } return {}; diff --git a/kernel/src/arch/MMU.h b/kernel/src/arch/MMU.h index b0ccc8b3..d15c7b87 100644 --- a/kernel/src/arch/MMU.h +++ b/kernel/src/arch/MMU.h @@ -3,6 +3,8 @@ struct PageDirectory; +extern const usize ARCH_PAGE_SIZE; + namespace MMU { enum Flags @@ -28,6 +30,4 @@ namespace MMU Result create_page_directory_for_userspace(); void setup_initial_page_directory(); - - usize page_size(); } \ No newline at end of file diff --git a/kernel/src/arch/x86_64/MMU.cpp b/kernel/src/arch/x86_64/MMU.cpp index fda4c36c..e8fb1bc0 100644 --- a/kernel/src/arch/x86_64/MMU.cpp +++ b/kernel/src/arch/x86_64/MMU.cpp @@ -4,6 +4,8 @@ #define PAGE_SIZE 4096 +const usize ARCH_PAGE_SIZE = PAGE_SIZE; + const u64 rindex = 0776; // recursive index const u64 sign = 0177777UL << 48; // sign extension @@ -52,10 +54,6 @@ static_assert(sizeof(PageDirectory) == PAGE_SIZE); namespace MMU { - usize page_size() - { - return PAGE_SIZE; - } PageDirectory* l4_table() {