diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-17 04:01:50 -0500 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-17 04:01:50 -0500 |
commit | b740f4df90c902aa5c1028ef25f97e4b3653f142 (patch) | |
tree | 8db274ea9c3b267279fa425c29733da85888ae41 | |
parent | 0523f48ae9b61170e4490f36599d399d6e4417c1 (diff) |
kernel: virtual_mm: Implement paging, properly
-rw-r--r-- | kernel/include/mm/virtual_mm.h | 44 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/virtual_mm.c | 25 |
2 files changed, 40 insertions, 29 deletions
diff --git a/kernel/include/mm/virtual_mm.h b/kernel/include/mm/virtual_mm.h index 4873a30..92ffd3c 100644 --- a/kernel/include/mm/virtual_mm.h +++ b/kernel/include/mm/virtual_mm.h @@ -29,37 +29,37 @@ #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 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) /* Never set (reserved by Intel) */ -#define SET_PDE_RESERVED(x) (x << 6) -#define SET_PDE_PAGE_SIZE(x) (x << 7) +#define SET_PDE_RESERVED(x) ((x) << 6) +#define SET_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 SET_PDE_UNUSED(x) ((x) << 8) /* Page table address */ -#define SET_PDE_FRAME(x) (x << 12) +#define SET_PDE_FRAME(x) ((x) << 12) -#define GET_PDE_FRAME(x) (*x >> 12) +#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 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) /* NOTE: Unused by the CPU, free to be used by us! */ -#define SET_PTE_UNUSED(x) (x << 9) +#define SET_PTE_UNUSED(x) ((x) << 9) /* MAX: 0xFFFFF000 */ -#define SET_PTE_FRAME(x) (x << 12) +#define SET_PTE_FRAME(x) ((x) << 12) -#define PTE_IS_PRESENT(x) (x & 1) -#define GET_PTE_FRAME(x) (*x >> 12) +#define PTE_IS_PRESENT(x) ((x) &1) +#define GET_PTE_FRAME(x) ((*x) >> 12) #define ADD_ATTRIB(entry, attribute) (*entry |= (attribute)) diff --git a/kernel/mm/virtual_mm/virtual_mm.c b/kernel/mm/virtual_mm/virtual_mm.c index d8d3d70..cfa32e1 100644 --- a/kernel/mm/virtual_mm/virtual_mm.c +++ b/kernel/mm/virtual_mm/virtual_mm.c @@ -33,7 +33,7 @@ uint32_t *current_page_directory = 0; ALWAYS_INLINE void virtual_mm_load_page_directory(uint32_t *page_directory) { - __asm__("movl %0, %%eax" ::"r"(page_directory)); + __asm__("movl %0, %%cr3" ::"r"(page_directory)); } bool @@ -64,10 +64,10 @@ virtual_mm_initialize(void) ASSERT_NOT_REACHED(); for (uint32_t i = 0; i < 1024; i++) - table[i] = 0; + 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; + /* 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)); @@ -76,7 +76,16 @@ virtual_mm_initialize(void) table[PAGE_TABLE_INDEX(virtual_addr)] = pt_entry; } - printk("debug", "Page table is at: 0x%x", table); + 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; + } uint32_t *page_directory = physical_mm_allocate_block(); if (!page_directory) @@ -85,13 +94,15 @@ virtual_mm_initialize(void) for (uint32_t i = 0; i < 1024; i++) page_directory[i] = 0; - uint32_t *pd_entry = &page_directory[PAGE_DIRECTORY_INDEX(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)); + 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", "pd_entry: 0x%x", pd_entry); + printk("debug", "513th entry: 0x%x", table[513]); virtual_mm_switch_page_directory(page_directory); virtual_mm_enable_paging(); |