diff options
-rw-r--r-- | kernel/include/mm/virtual_mm.h | 44 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/virtual_mm.c | 34 |
2 files changed, 28 insertions, 50 deletions
diff --git a/kernel/include/mm/virtual_mm.h b/kernel/include/mm/virtual_mm.h index 92ffd3c..52eb92c 100644 --- a/kernel/include/mm/virtual_mm.h +++ b/kernel/include/mm/virtual_mm.h @@ -28,35 +28,35 @@ #define PAGE_DIRECTORY_INDEX(virtual_address) ((virtual_address >> 22) & 0x3ff) #define PAGE_TABLE_INDEX(virtual_address) ((virtual_address >> 12) & 0x3ff) -#define SET_PDE_PRESENT(x) x -#define SET_PDE_WRITABLE(x) ((x) << 1) -#define SET_PDE_USER(x) ((x) << 2) -#define SET_PDE_WRITETHROUGH(x) ((x) << 3) -#define SET_PDE_CACHE_DISABLE(x) ((x) << 4) -#define SET_PDE_ACCESSED(x) ((x) << 5) +#define PDE_PRESENT(x) x +#define PDE_WRITABLE(x) ((x) << 1) +#define PDE_USER(x) ((x) << 2) +#define PDE_WRITETHROUGH(x) ((x) << 3) +#define PDE_CACHE_DISABLE(x) ((x) << 4) +#define PDE_ACCESSED(x) ((x) << 5) /* Never set (reserved by Intel) */ -#define SET_PDE_RESERVED(x) ((x) << 6) -#define SET_PDE_PAGE_SIZE(x) ((x) << 7) +#define PDE_RESERVED(x) ((x) << 6) +#define PDE_PAGE_SIZE(x) ((x) << 7) /* NOTE: Unused by the CPU, free to be used by us! */ -#define SET_PDE_UNUSED(x) ((x) << 8) +#define PDE_UNUSED(x) ((x) << 8) /* Page table address */ -#define SET_PDE_FRAME(x) ((x) << 12) +#define PDE_FRAME(x) ((x) &0xFFFFF000) #define GET_PDE_FRAME(x) ((*x) >> 12) -#define SET_PTE_PRESENT(x) x -#define SET_PTE_WRITABLE(x) ((x) << 1) -#define SET_PTE_USER(x) ((x) << 2) -#define SET_PTE_WRITETHROUGH(x) ((x) << 3) -#define SET_PTE_CACHE_DISABLE(x) ((x) << 4) -#define SET_PTE_ACCESSED(x) ((x) << 5) -#define SET_PTE_DIRTY(x) ((x) << 6) -#define SET_PTE_PAT(x) ((x) << 7) -#define SET_PTE_GLOBAL(x) ((x) << 8) +#define PTE_PRESENT(x) x +#define PTE_WRITABLE(x) ((x) << 1) +#define PTE_USER(x) ((x) << 2) +#define PTE_WRITETHROUGH(x) ((x) << 3) +#define PTE_CACHE_DISABLE(x) ((x) << 4) +#define PTE_ACCESSED(x) ((x) << 5) +#define PTE_DIRTY(x) ((x) << 6) +#define PTE_PAT(x) ((x) << 7) +#define PTE_GLOBAL(x) ((x) << 8) /* NOTE: Unused by the CPU, free to be used by us! */ -#define SET_PTE_UNUSED(x) ((x) << 9) -/* MAX: 0xFFFFF000 */ -#define SET_PTE_FRAME(x) ((x) << 12) +#define PTE_UNUSED(x) ((x) << 9) +/* Left shift by 12 because we only need the bits from the twelfth bit. */ +#define PTE_FRAME(x) ((x) << 12) #define PTE_IS_PRESENT(x) ((x) &1) #define GET_PTE_FRAME(x) ((*x) >> 12) diff --git a/kernel/mm/virtual_mm/virtual_mm.c b/kernel/mm/virtual_mm/virtual_mm.c index cfa32e1..ba2f472 100644 --- a/kernel/mm/virtual_mm/virtual_mm.c +++ b/kernel/mm/virtual_mm/virtual_mm.c @@ -62,47 +62,25 @@ virtual_mm_initialize(void) uint32_t *table = physical_mm_allocate_block(); if (!table) ASSERT_NOT_REACHED(); + printk("debug", "Page table is at: 0x%x", table); for (uint32_t i = 0; i < 1024; i++) table[i] = 0x2; /* Identity map the first 4MiB (maps 4KiB 1024 times) */ - /* for (uint32_t i = 0, physical_addr = 0, virtual_addr = 0; i < 1024; - i++, physical_addr += 4096, virtual_addr += 4096) { - uint32_t pt_entry = 0; - ADD_ATTRIB(&pt_entry, SET_PTE_PRESENT(1)); - ADD_ATTRIB(&pt_entry, SET_PTE_WRITABLE(1)); - ADD_ATTRIB(&pt_entry, SET_PTE_FRAME(physical_addr)); - - table[PAGE_TABLE_INDEX(virtual_addr)] = pt_entry; - } - printk("debug", "Page table is at: 0x%x", table); */ - - for (uint32_t i = 0; i < 1024; i++) { - uint32_t pt_entry = 0; - ADD_ATTRIB(&pt_entry, SET_PTE_PRESENT(1)); - ADD_ATTRIB(&pt_entry, SET_PTE_WRITABLE(1)); - ADD_ATTRIB(&pt_entry, SET_PTE_FRAME(i)); - - table[i] = pt_entry; - } + for (uint32_t i = 0; i < 1024; i++) + table[i] = PTE_FRAME(i) | PTE_PRESENT(1) | PTE_WRITABLE(1); uint32_t *page_directory = physical_mm_allocate_block(); if (!page_directory) ASSERT_NOT_REACHED(); + printk("debug", "Page directory is at: 0x%x", page_directory); for (uint32_t i = 0; i < 1024; i++) page_directory[i] = 0; - /* uint32_t *pd_entry = &page_directory[0]; - ADD_ATTRIB(pd_entry, SET_PDE_PRESENT(1)); - ADD_ATTRIB(pd_entry, SET_PDE_WRITABLE(1)); - ADD_ATTRIB(pd_entry, SET_PDE_FRAME((uint32_t) table)); */ - page_directory[0] = ((uint32_t) table) | 3; - - printk("debug", "Page directory is at: 0x%x", page_directory); - // printk("debug", "pd_entry: 0x%x", pd_entry); - printk("debug", "513th entry: 0x%x", table[513]); + uint32_t *pd_entry = &page_directory[0]; + *pd_entry = PDE_FRAME((uint32_t) table) | PDE_PRESENT(1) | PDE_WRITABLE(1); virtual_mm_switch_page_directory(page_directory); virtual_mm_enable_paging(); |