From 9f53e4bc399bbf3fae11d53dd55bb931c62dcc04 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sat, 4 Mar 2023 22:36:52 +0100 Subject: [PATCH] feature (memory management): Added pag_map info dump function --- kernel/inc/mm/page_map.h | 1 + kernel/inc/mm/region.h | 33 +++++++++++++++ kernel/src/kmain.c | 45 ++++++--------------- kernel/src/mm/page_map.c | 86 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 32 deletions(-) create mode 100644 kernel/inc/mm/region.h diff --git a/kernel/inc/mm/page_map.h b/kernel/inc/mm/page_map.h index acb709a..82c231f 100644 --- a/kernel/inc/mm/page_map.h +++ b/kernel/inc/mm/page_map.h @@ -54,6 +54,7 @@ void page_map_map_memory (page_map_T* page_map, void* void page_map_unmap_memory (page_map_T* page_map, void* virtual_address); void* page_map_get_physical_address (page_map_T* page_map, void* virtual_address); void page_map_destruct (page_map_T* page_map); +void page_map_dump_info (page_map_T* page_map); void page_map_entry_set_flags (uint64_t* entry, uint64_t flags); bool page_map_entry_get_flag (uint64_t* entry, page_map_flag_E flag); diff --git a/kernel/inc/mm/region.h b/kernel/inc/mm/region.h new file mode 100644 index 0000000..9c4a47e --- /dev/null +++ b/kernel/inc/mm/region.h @@ -0,0 +1,33 @@ +/* + * Copyright 2023 Antifallobst + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the “Software”), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef NOX_REGION_H +#define NOX_REGION_H + +#define MEM_REGION_PROCESS 0x0000000000000000 + +#define MEM_REGION_KERNEL 0xFFFF800000000000 +#define MEM_REGION_KERNEL_EXEC 0xFFFFFFFF80000000 +#define MEM_REGION_KERNEL_HEAP 0xFFFFFFFFF0000000 + +#endif //NOX_REGION_H diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index 3ca6eca..ab29bbc 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -28,6 +28,7 @@ #include "platform/interrupts.h" #include "mm/page_frame.h" #include "mm/page_map.h" +#include "mm/region.h" #include "drivers/time/pit.h" #include "drivers/graphics/renderer.h" #include "drivers/fs/vfs.h" @@ -59,7 +60,7 @@ void kernel_init(boot_info_T* boot_info) { limine_terminal_print(boot_info, " ok\n"); limine_terminal_print(boot_info, " Initializing heap..."); - memory_allocator_init((void*)0x100000000000); + memory_allocator_init((void*)MEM_REGION_KERNEL_HEAP); limine_terminal_print(boot_info, " ok\n"); limine_terminal_print(boot_info, " Initializing graphics renderer..."); @@ -76,12 +77,6 @@ void kernel_init(boot_info_T* boot_info) { limine_terminal_print(boot_info, " ok\n"); } -void test() { - while(1) { - int x = 0 / 0; - } -} - void kmain(boot_info_T boot_info) { limine_terminal_print(&boot_info, "Booting NoxOS...\n"); @@ -92,36 +87,22 @@ void kmain(boot_info_T boot_info) { limine_terminal_print(&boot_info, "Kernel initialized\n"); log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n"); - -// pid_t proc1 = process_spawn(PROCESS_KERNEL, "test_1"); -// -// pid_t proc1_1 = process_spawn(proc1, "test_1-1"); -// thread_spawn(proc1_1, NULL); -// pid_t proc1_2 = process_spawn(proc1, "test_1-2"); -// -// pid_t proc2 = process_spawn(PROCESS_KERNEL, "test_2"); - - thread_T* thread = thread_spawn(PROCESS_KERNEL, test); - - scheduler_start_thread(thread); - - scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0); - -// process_kill_pid(proc1); -// scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0); + page_map_dump_info(g_kernel_page_map); // vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/test.elf"); +// void* buffer = memory_allocate(node->size); +// vfs_file_read(node, 0, node->size, buffer); // -// elf_executable_T* exec = elf_executable_create(node->cache->buffer); +// elf_executable_T* exec = elf_executable_create(buffer); // -// page_map_T* page_map = pframe_request(); -// elf_mappings_apply(exec->mappings, exec->num_mappings, node->cache->buffer, 0xFFFFFFFFFF000000, page_map); -// -// int (*entry)() = (0xFFFFFFFFFF000000 + symbol_resolve_from_name(exec->symbols, exec->num_symbols, "_start")->address); -// -// thread_spawn(PROCESS_KERNEL, entry); +// pid_t process = process_spawn(PROCESS_KERNEL, "test 1", exec, buffer); +// void* func = (void*)symbol_resolve_from_name(scheduler_get_process(process)->executable->symbols, +// scheduler_get_process(process)->executable->num_symbols, +// "_start")->address; +// thread_start(thread_spawn(process, func)); -// log(LOG_DEBUG, "ELF returned: %d", entry()); + +// scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0); CORE_HALT_FOREVER } diff --git a/kernel/src/mm/page_map.c b/kernel/src/mm/page_map.c index 19e9472..0dd8dac 100644 --- a/kernel/src/mm/page_map.c +++ b/kernel/src/mm/page_map.c @@ -160,6 +160,92 @@ void page_map_destruct(page_map_T* page_map) { pframe_free(page_map); } +void page_map_dump_info(page_map_T* page_map) { + log(LOG_INFO, "Page map info:"); + log(LOG_INFO, " Address: 0x%x", page_map); + log(LOG_INFO, " Mapped regions:"); + + uint64_t mapped_pages = 0; + uint64_t num_pdp = 0; + uint64_t num_pd = 0; + uint64_t num_pt = 0; + bool is_last_page_mapped = false; + uint64_t region_begin = 0; + + for (uint16_t page_directory_page_index = 0; page_directory_page_index < 512; page_directory_page_index++) { + if (!page_map_entry_get_flag(&page_map->entries[page_directory_page_index], PM_FLAG_PRESENT)) { + continue; + } + page_map_T* page_directory_page = page_map_entry_get_address(&page_map->entries[page_directory_page_index]); + + for (uint16_t page_directory_index = 0; page_directory_index < 512; page_directory_index++) { + if (!page_map_entry_get_flag(&page_directory_page->entries[page_directory_index], PM_FLAG_PRESENT)) { + continue; + } + page_map_T *page_directory = page_map_entry_get_address(&page_directory_page->entries[page_directory_index]); + + for (uint16_t page_table_index = 0; page_table_index < 512; page_table_index++) { + if (!page_map_entry_get_flag(&page_directory->entries[page_table_index], PM_FLAG_PRESENT)) { + continue; + } + page_map_T *page_table = page_map_entry_get_address(&page_directory->entries[page_table_index]); + + for (uint16_t page_index = 0; page_index < 512; page_index++) { + if (!page_map_entry_get_flag(&page_table->entries[page_index], PM_FLAG_PRESENT)) { + if (!is_last_page_mapped) { continue; } + + is_last_page_mapped = false; + + uint64_t region_end = 0; + + region_end += page_directory_page_index; + region_end <<= 9; + region_end += page_directory_index; + region_end <<= 9; + region_end += page_table_index; + region_end <<= 9; + region_end += page_index; + region_end <<= 12; + + log(LOG_INFO, " 0x%x <--> 0x%x", region_begin, region_end); + continue; + } + + if (!is_last_page_mapped) { + region_begin = 0; + + region_begin += page_directory_page_index; + region_begin <<= 9; + region_begin += page_directory_index; + region_begin <<= 9; + region_begin += page_table_index; + region_begin <<= 9; + region_begin += page_index; + region_begin <<= 12; + } + + is_last_page_mapped = true; + mapped_pages++; + } + + num_pt++; + } + + num_pd++; + } + + num_pdp++; + } + + log(LOG_INFO, " Mapped pages: %d", mapped_pages); + log(LOG_INFO, " Mapped memory: %d KB", mapped_pages * 4); + log(LOG_INFO, " Structure Pages: %d", num_pdp + num_pd + num_pt); + log(LOG_INFO, " PML4 -> 1"); + log(LOG_INFO, " |- PDP -> %d", num_pdp); + log(LOG_INFO, " |- PD -> %d", num_pd); + log(LOG_INFO, " |- PT -> %d", num_pt); +} + void page_map_entry_set_flags(uint64_t* entry, uint64_t flags) { *entry |= flags; }