diff options
author | Marvin Borner | 2020-05-19 21:55:27 +0200 |
---|---|---|
committer | Marvin Borner | 2020-05-19 21:55:27 +0200 |
commit | 61c7bf940d9f6dedb1b5c0307fd170cae562fbd1 (patch) | |
tree | c4b4381fd9519f8f43067e55718dd502b9100e0d /src/kernel/memory | |
parent | ffb7cb87593a5de58f7c5183020576dac9d50fc4 (diff) |
Woah, so many changes :O
Diffstat (limited to 'src/kernel/memory')
-rw-r--r-- | src/kernel/memory/alloc.c | 8 | ||||
-rw-r--r-- | src/kernel/memory/alloc.h | 1 | ||||
-rw-r--r-- | src/kernel/memory/paging.c | 37 | ||||
-rw-r--r-- | src/kernel/memory/paging.h | 4 |
4 files changed, 34 insertions, 16 deletions
diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c index a4fb60a..904be3a 100644 --- a/src/kernel/memory/alloc.c +++ b/src/kernel/memory/alloc.c @@ -361,6 +361,14 @@ void *malloc(u32 req_size) return NULL; } +// Definitely improveable +void *valloc(u32 req_size) +{ + u32 mask = l_page_size - 1; + u32 mem = malloc(req_size + l_page_size); + return (void *)((mem + mask) & ~mask); +} + void free(void *ptr) { struct liballoc_minor *min; diff --git a/src/kernel/memory/alloc.h b/src/kernel/memory/alloc.h index 6c4290e..bb3f452 100644 --- a/src/kernel/memory/alloc.h +++ b/src/kernel/memory/alloc.h @@ -4,6 +4,7 @@ #include <stdint.h> void *malloc(u32); +void *valloc(u32); void *realloc(void *, u32); void *calloc(u32, u32); void free(void *); diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index de93ffb..94b777e 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -9,40 +9,49 @@ 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(u32 *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++) { - current_page_tables[i][j] = ((j * 0x1000) + (i * 0x400000)) | PT_RW; + tables[i][j] = + ((j * 0x1000) + (i * 0x400000)) | PT_RW | (user ? PT_USER : 0); } } for (u32 i = 0; i < 1024; i++) { - current_page_directory[i] = ((u32)current_page_tables[i]) | PD_RW | PD_PRESENT; + 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() { + 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 - // paging_set_user(0, memory_get_all() >> 3); // HMM + 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!"); } u32 *paging_make_directory(int user) { - u32 *dir = malloc(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; } @@ -68,7 +77,7 @@ void paging_enable() void paging_switch_directory(u32 *dir) { - current_page_tables = kernel_page_tables; + current_page_tables = dir; current_page_directory = dir; asm("mov %0, %%cr3" ::"r"(current_page_directory)); } @@ -189,4 +198,4 @@ u32 paging_get_used_pages() } } return n; -} +}
\ No newline at end of file diff --git a/src/kernel/memory/paging.h b/src/kernel/memory/paging.h index 8857381..553c0e6 100644 --- a/src/kernel/memory/paging.h +++ b/src/kernel/memory/paging.h @@ -27,7 +27,7 @@ u32 *current_page_directory; u32 kernel_page_directory[1024] __attribute__((aligned(4096))); int paging_enabled; -void paging_install(u32 multiboot_address); +void paging_install(); void paging_enable(); void paging_disable(); @@ -51,4 +51,4 @@ void paging_set_user(u32 virt, u32 count); u32 paging_find_pages(u32 count); u32 paging_alloc_pages(u32 count); -#endif +#endif
\ No newline at end of file |