diff options
Diffstat (limited to 'src/kernel/memory/paging.c')
-rw-r--r-- | src/kernel/memory/paging.c | 73 |
1 files changed, 46 insertions, 27 deletions
diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index c0c1a6a..94b777e 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -4,47 +4,54 @@ #include <stdint.h> #include <system.h> -struct page_dir *current_page_directory; -struct page_dir kernel_page_directory[1024] __attribute__((aligned(4096))); +u32 *current_page_directory; +u32 (*current_page_tables)[1024]; +u32 kernel_page_directory[1024] __attribute__((aligned(4096))); u32 kernel_page_tables[1024][1024] __attribute__((aligned(4096))); -void paging_init(struct page_dir *dir, int user) +void paging_init(u32 *dir, u32 tables[1024][1024], int user) { for (u32 i = 0; i < 1024; i++) { for (u32 j = 0; j < 1024; j++) { - dir->tables[i][j] = + tables[i][j] = ((j * 0x1000) + (i * 0x400000)) | PT_RW | (user ? PT_USER : 0); } } for (u32 i = 0; i < 1024; i++) { - dir[i] = *(struct page_dir *)(((u32)dir->tables[i]) | PD_RW | PD_PRESENT | - (user ? PD_USER : 0)); + dir[i] = ((u32)tables[i]) | PD_RW | PD_PRESENT | (user ? PD_USER : 0); } } extern void KERNEL_END(); -void paging_install(u32 multiboot_address) +void paging_install() { - kernel_page_directory->tables = kernel_page_tables; + paging_init(kernel_page_directory, kernel_page_tables, 0); paging_switch_directory(kernel_page_directory); - paging_init(current_page_directory, 0); - // if mmap approach didn't work - if (!memory_init(multiboot_address)) - paging_set_present(0, memory_get_all() >> 3); // /4 - paging_set_used(0, ((u32)KERNEL_END >> 12) + 1); // /4096 + if (!memory_init()) + paging_set_present(0, memory_get_all() >> 3); + paging_set_used(0, ((u32)KERNEL_END >> 12) + 1); paging_enable(); log("Installed paging"); + + // Test! + u32 a = (u32)malloc(4096); + u32 b = (u32)malloc(4096); + free((void *)b); + free((void *)a); + u32 c = (u32)malloc(2048); + assert(a == c); + info("Malloc test succeeded!"); } -struct page_dir *paging_make_directory(int user) +u32 *paging_make_directory(int user) { - struct page_dir *dir = valloc(1024 * 1024 * 32); - dir->tables = valloc(1024 * 1024 * 32); + u32 *dir = valloc(1024 * 1024 * 32); + u32 *tables = valloc(1024 * 1024 * 32); - paging_init(dir, user); + paging_init(dir, tables, user); return dir; } @@ -60,6 +67,7 @@ void paging_disable() void paging_enable() { + asm("mov %0, %%cr3" ::"r"(current_page_directory)); u32 cr0; asm("mov %%cr0, %0" : "=r"(cr0)); cr0 |= 0x80000000; @@ -67,10 +75,11 @@ void paging_enable() paging_enabled = 1; } -void paging_switch_directory(struct page_dir *dir) +void paging_switch_directory(u32 *dir) { + current_page_tables = dir; current_page_directory = dir; - asm("mov %0, %%cr3" ::"r"(dir)); + asm("mov %0, %%cr3" ::"r"(current_page_directory)); } void invlpg(u32 addr) @@ -82,7 +91,7 @@ void paging_map(u32 phy, u32 virt, u16 flags) { u32 pdi = virt >> 22; u32 pti = virt >> 12 & 0x03FF; - current_page_directory->tables[pdi][pti] = phy | flags; + current_page_tables[pdi][pti] = phy | flags; invlpg(virt); } @@ -90,21 +99,21 @@ u32 paging_get_phys(u32 virt) { u32 pdi = virt >> 22; u32 pti = (virt >> 12) & 0x03FF; - return current_page_directory->tables[pdi][pti] & 0xFFFFF000; + return current_page_tables[pdi][pti] & 0xFFFFF000; } u16 paging_get_flags(u32 virt) { u32 pdi = virt >> 22; u32 pti = (virt >> 12) & 0x03FF; - return current_page_directory->tables[pdi][pti] & 0xFFF; + return current_page_tables[pdi][pti] & 0xFFF; } void paging_set_flag_up(u32 virt, u32 count, u32 flag) { u32 page_n = virt / 0x1000; for (u32 i = page_n; i < page_n + count; i++) { - current_page_directory->tables[i / 1024][i % 1024] |= flag; + current_page_tables[i / 1024][i % 1024] |= flag; invlpg(i * 0x1000); } } @@ -113,7 +122,7 @@ void paging_set_flag_down(u32 virt, u32 count, u32 flag) { u32 page_n = virt / 0x1000; for (u32 i = page_n; i < page_n + count; i++) { - current_page_directory->tables[i / 1024][i % 1024] &= ~flag; + current_page_tables[i / 1024][i % 1024] &= ~flag; invlpg(i * 0x1000); } } @@ -138,6 +147,15 @@ void paging_set_free(u32 virt, u32 count) paging_set_flag_down(virt, count, PT_USED); } +void paging_set_user(u32 virt, u32 count) +{ + u32 page_n = virt / 0x1000; + for (u32 i = page_n; i < page_n + count; i += 1024) { + current_page_directory[i / 1024] |= PD_USER; + } + paging_set_flag_up(virt, count, PT_USER); +} + u32 paging_find_pages(u32 count) { u32 continuous = 0; @@ -145,8 +163,8 @@ u32 paging_find_pages(u32 count) u32 start_page = 0; for (u32 i = 0; i < 1024; i++) { for (u32 j = 0; j < 1024; j++) { - if (!(current_page_directory->tables[i][j] & PT_PRESENT) || - (current_page_directory->tables[i][j] & PT_USED)) { + if (!(current_page_tables[i][j] & PT_PRESENT) || + (current_page_tables[i][j] & PT_USED)) { continuous = 0; start_dir = i; start_page = j + 1; @@ -165,6 +183,7 @@ u32 paging_alloc_pages(u32 count) { u32 ptr = paging_find_pages(count); paging_set_used(ptr, count); + paging_set_user(ptr, count); return ptr; } @@ -173,7 +192,7 @@ u32 paging_get_used_pages() u32 n = 0; for (u32 i = 0; i < 1024; i++) { for (u32 j = 0; j < 1024; j++) { - u8 flags = current_page_directory->tables[i][j] & PT_USED; + u8 flags = current_page_tables[i][j] & PT_USED; if (flags == 1) n++; } |