From 1b7be49c34ba6ed142d7c3b4a305c17753ba72c9 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Thu, 20 Apr 2023 18:29:21 +0200 Subject: [PATCH] feature (page maps): implemented a function to check if a page is mapped or not --- kernel/inc/mm/page_map.h | 1 + kernel/src/mm/page_map.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/kernel/inc/mm/page_map.h b/kernel/inc/mm/page_map.h index d3fe27f..90082c9 100644 --- a/kernel/inc/mm/page_map.h +++ b/kernel/inc/mm/page_map.h @@ -31,6 +31,7 @@ extern page_map_T* page_map_fetch_current (); extern void page_map_load (page_map_T* page_map); void page_map_map_memory (page_map_T* page_map, void* virtual_address, void* physical_address, uint64_t flags); void page_map_unmap_memory (page_map_T* page_map, void* virtual_address); +bool page_map_check_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); diff --git a/kernel/src/mm/page_map.c b/kernel/src/mm/page_map.c index 17443b4..03650c9 100644 --- a/kernel/src/mm/page_map.c +++ b/kernel/src/mm/page_map.c @@ -89,6 +89,27 @@ void page_map_unmap_memory(page_map_T* page_map, void* virtual_address) { page_table->entries[page_index] = 0; } +bool page_map_check_memory(page_map_T* page_map, void* virtual_address) { + virtual_address = (void*)FLOOR_TO((uint64_t)virtual_address, PFRAME_SIZE); + + // Intel's Developer Manual has a nice graph, that explains how these indexes are calculated + uint16_t page_index = ((uint64_t)virtual_address >> 12) & 0x1FF; + uint16_t page_table_index = ((uint64_t)virtual_address >> 21) & 0x1FF; + uint16_t page_directory_index = ((uint64_t)virtual_address >> 30) & 0x1FF; + uint16_t page_directory_page_index = ((uint64_t)virtual_address >> 39) & 0x1FF; + + if (!page_map_entry_get_flag(&page_map->entries[page_directory_page_index], PM_FLAG_PRESENT)) return false; + page_map_T* page_directory_page = page_map_entry_get_address(&page_map->entries[page_directory_page_index]); + + if (!page_map_entry_get_flag(&page_directory_page->entries[page_directory_index], PM_FLAG_PRESENT)) return false; + page_map_T* page_directory = page_map_entry_get_address(&page_directory_page->entries[page_directory_index]); + + if (!page_map_entry_get_flag(&page_directory->entries[page_table_index], PM_FLAG_PRESENT)) return false; + page_map_T* page_table = page_map_entry_get_address(&page_directory->entries[page_table_index]); + + return page_map_entry_get_flag(&page_table->entries[page_index], PM_FLAG_PRESENT); +} + void* page_map_get_physical_address(page_map_T* page_map, void* virtual_address) { virtual_address = (void*)FLOOR_TO((uint64_t)virtual_address, PFRAME_SIZE);