feature (kernel): implemented function to resolve physical address from virtual address

This commit is contained in:
antifallobst 2023-02-10 20:13:33 +01:00
parent e16b287da4
commit ed2d276717
3 changed files with 35 additions and 0 deletions

View File

@ -44,6 +44,7 @@ extern page_map_T* page_map_fetch_current ();
extern void page_map_load (page_map_T* page_map); 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_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); 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_destruct (page_map_T* page_map);
void page_map_entry_set_flags (uint64_t* entry, uint64_t flags); void page_map_entry_set_flags (uint64_t* entry, uint64_t flags);

View File

@ -44,7 +44,11 @@ void kmain(boot_info_T boot_info) {
// this should cause a kernel panic // this should cause a kernel panic
// int x = 1312 / 0; // int x = 1312 / 0;
void* debug;
debug = page_map_get_physical_address(g_kernel_page_map, (void*)0x100000000000);
page_map_map_memory(g_kernel_page_map, (void*)0x100000000000, pframe_request(), PM_FLAG_READ_WRITE); page_map_map_memory(g_kernel_page_map, (void*)0x100000000000, pframe_request(), PM_FLAG_READ_WRITE);
debug = page_map_get_physical_address(g_kernel_page_map, (void*)0x100000000000);
memory_copy("test string", (void*)0x100000000000, 13); memory_copy("test string", (void*)0x100000000000, 13);
log(LOG_DEBUG, (string_t)0x100000000000); log(LOG_DEBUG, (string_t)0x100000000000);

View File

@ -90,6 +90,36 @@ void page_map_unmap_memory(page_map_T* page_map, void* virtual_address) {
page_table->entries[page_index] = 0; page_table->entries[page_index] = 0;
} }
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);
// 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 NULL;
}
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 NULL;
}
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 NULL;
}
page_map_T* page_table = page_map_entry_get_address(&page_directory->entries[page_table_index]);
if (!page_map_entry_get_flag(&page_table->entries[page_index], PM_FLAG_PRESENT)) {
return NULL;
}
return page_map_entry_get_address(&page_table->entries[page_index]);
}
void page_map_destruct(page_map_T* page_map) { void page_map_destruct(page_map_T* page_map) {
for (uint16_t page_directory_page_index = 0; page_directory_page_index < 512; page_directory_page_index++) { 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)) { if (!page_map_entry_get_flag(&page_map->entries[page_directory_page_index], PM_FLAG_PRESENT)) {