diff options
Diffstat (limited to 'kernel/mm/virtual_mm/virtual_mm.c')
-rw-r--r-- | kernel/mm/virtual_mm/virtual_mm.c | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/kernel/mm/virtual_mm/virtual_mm.c b/kernel/mm/virtual_mm/virtual_mm.c index 929b37d..4153065 100644 --- a/kernel/mm/virtual_mm/virtual_mm.c +++ b/kernel/mm/virtual_mm/virtual_mm.c @@ -89,7 +89,7 @@ virtual_mm_initialize(void) void virtual_mm_map_page(void *physical_address, void *virtual_address) { - uint32_t *pd_entry = ¤t_page_directory[GET_PDE_FRAME(virtual_address)]; + uint32_t *pd_entry = ¤t_page_directory[GET_PD_INDEX(virtual_address)]; uint32_t *table = 0; /* If the pd_entry isn't present, allocate a block for it, zero the table, @@ -106,16 +106,18 @@ virtual_mm_map_page(void *physical_address, void *virtual_address) } else table = (uint32_t *) PDE_GET_TABLE(pd_entry); - uint32_t *pt_entry = &table[GET_PTE_FRAME(virtual_address)]; - if (PTE_IS_PRESENT(pt_entry)) - ASSERT_NOT_REACHED(); /* TODO: Mapping previously mapped memory */ + uint32_t *pt_entry = &table[GET_PT_INDEX(virtual_address)]; + if (PTE_IS_PRESENT(pt_entry)) { + printk("debug", "Mapping previously mapped memory: 0x%x", pt_entry); + ASSERT_NOT_REACHED(); + } *pt_entry = PTE_FRAME((uint32_t) physical_address) | PTE_PRESENT(1) | PTE_WRITABLE(1); } ALWAYS_INLINE uint32_t * -get_table(uint32_t *pd_entry) +get_or_make_table(uint32_t *pd_entry) { uint32_t *table; if (!PDE_IS_PRESENT(pd_entry)) { @@ -123,8 +125,8 @@ get_table(uint32_t *pd_entry) if (!table) ASSERT_NOT_REACHED(); - for (uint32_t _ = 0; _ < 1024; _++) - table[_] = 0; + for (uint32_t i = 0; i < 1024; i++) + table[i] = 0x0; *pd_entry = PDE_FRAME((uint32_t) table) | PDE_PRESENT(1) | PDE_WRITABLE(1); } else @@ -136,33 +138,38 @@ get_table(uint32_t *pd_entry) ALWAYS_INLINE void virtual_mm_find_free_virtual_addresses(uint32_t n) { - for (uint32_t i = 0; i < PAGE_DIRECTORY_SIZE; i++) { - uint32_t current_pd_index = i; - uint32_t *pd_entry = ¤t_page_directory[current_pd_index]; - uint32_t *table = get_table(pd_entry); + /* Skip the first page directory, we don't wanna touch the first 4MiB. */ + for (uint32_t i = 1; i < PAGE_DIRECTORY_SIZE; i++) { + uint32_t *pd_entry = ¤t_page_directory[i]; + uint32_t *table = get_or_make_table(pd_entry); for (uint32_t j = 0; j < PAGE_TABLE_SIZE; j++) { uint32_t *pt_entry = &table[j]; if (PTE_IS_PRESENT(pt_entry)) continue; - /* We found our starting pt_entry */ + /* We found our starting pt_entry */ printk("debug", "Starting: 0x%x", pt_entry); uint32_t count = 0; for (uint32_t k = j; k <= PAGE_TABLE_SIZE; k++) { /* If we overflow, switch to the consecutive page directory entry */ if (k == PAGE_TABLE_SIZE) { - pd_entry = ¤t_page_directory[++current_pd_index]; - table = get_table(pd_entry); + if (++i == PAGE_DIRECTORY_SIZE) + ASSERT_NOT_REACHED(); /* Ran out of pd_entries */ + + pd_entry = ¤t_page_directory[i]; printk("debug", "Switching pd_entry: 0x%x", pd_entry); + + table = get_or_make_table(pd_entry); k = 0; } + /* If page table entry is already used, break from the current loop */ uint32_t *pt_entry = &table[k]; if (PTE_IS_PRESENT(pt_entry)) { - count = 0; - current_pd_index = i; - pd_entry = ¤t_page_directory[current_pd_index]; + /* Since we have some used address at some point between j and count, + * we can't find n consecutive free addresses in between j and the + * used block (j + count + 1) */ j += count; break; } @@ -170,6 +177,8 @@ virtual_mm_find_free_virtual_addresses(uint32_t n) // printk("debug", "Found page: 0x%x", &table[k]); count++; if (count == n) { + /* TODO: Convert this into a virtual_address using the pd_index & + * pt_index */ printk("debug", "Found starting page at: 0x%x", &table[j]); return; } |