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);