diff options
author | Marvin Borner | 2020-05-26 18:14:14 +0200 |
---|---|---|
committer | Marvin Borner | 2020-05-26 18:14:14 +0200 |
commit | 0977f984ca61c6cce96705ab923bdf93dc3ce5a3 (patch) | |
tree | 1d69d41996b383a15d897a68e140147adeebcd8b /src | |
parent | b70116dc7c57132c109955c4b174ff2417afda68 (diff) |
Reduced some magic numbers
Diffstat (limited to 'src')
-rw-r--r-- | src/kernel/fs/elf.c | 5 | ||||
-rw-r--r-- | src/kernel/memory/paging.c | 63 | ||||
-rw-r--r-- | src/kernel/memory/paging.h | 4 | ||||
-rw-r--r-- | src/kernel/tasks/process.c | 4 |
4 files changed, 36 insertions, 40 deletions
diff --git a/src/kernel/fs/elf.c b/src/kernel/fs/elf.c index 27857a9..2dddc0e 100644 --- a/src/kernel/fs/elf.c +++ b/src/kernel/fs/elf.c @@ -48,16 +48,11 @@ struct process *elf_load(char *path) debug("File is valid: %s", path); } - // TODO: Fix here - halt_loop(); struct process *proc = process_make_new(); strcpy(proc->name, path); proc->registers.eip = header->entry; paging_switch_directory(proc->cr3); - if (!memory_init()) - paging_set_present(0, memory_get_all() >> 3); - u32 stk = (u32)valloc(PAGE_SIZE); proc->registers.useresp = 0x40000000 - (PAGE_SIZE / 2); diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index 59965b7..aea717a 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -5,39 +5,39 @@ #include <system.h> u32 **current_page_directory; -u32 kernel_page_directory[1024] __attribute__((aligned(4096))); -u32 kernel_page_tables[1024][1024] __attribute__((aligned(4096))); -u32 *temp_dir[1024] __attribute__((aligned(4096))); +u32 kernel_page_directory[PAGE_COUNT] __attribute__((aligned(PAGE_ALIGN))); +u32 kernel_page_tables[PAGE_COUNT][PAGE_COUNT] __attribute__((aligned(PAGE_ALIGN))); +u32 *temp_dir[PAGE_COUNT] __attribute__((aligned(PAGE_ALIGN))); void paging_init(u32 **dir) { - for (u32 i = 0; i < 1024; i++) { - for (u32 j = 0; j < 1024; j++) { - dir[i][j] = ((j * 0x1000) + (i * 0x400000)) | PT_RW | PT_USER; + for (u32 i = 0; i < PAGE_COUNT; i++) { + for (u32 j = 0; j < PAGE_COUNT; j++) { + dir[i][j] = ((j * PAGE_ALIGN) + (i * PAGE_SIZE)) | PT_RW | PT_USER; } } - for (u32 i = 0; i < 1024; i++) { + for (u32 i = 0; i < PAGE_COUNT; i++) { dir[i] = ((u32)dir[i]) | PD_RW | PD_PRESENT | PD_USER; } } void paging_kernel_init(u32 *dir) { - for (u32 i = 0; i < 1024; i++) { - for (u32 j = 0; j < 1024; j++) { - kernel_page_tables[i][j] = ((j * 0x1000) + (i * 0x400000)) | PT_RW; + for (u32 i = 0; i < PAGE_COUNT; i++) { + for (u32 j = 0; j < PAGE_COUNT; j++) { + kernel_page_tables[i][j] = ((j * PAGE_ALIGN) + (i * PAGE_SIZE)) | PT_RW; } } - for (u32 i = 0; i < 1024; i++) { + for (u32 i = 0; i < PAGE_COUNT; i++) { kernel_page_directory[i] = ((u32)kernel_page_tables[i]) | PD_RW | PD_PRESENT; } current_page_directory = temp_dir; - for (int i = 0; i < 1024; i++) { + for (int i = 0; i < PAGE_COUNT; i++) { current_page_directory[i] = kernel_page_tables[i]; - for (int j = 0; j < 1024; j++) { + for (int j = 0; j < PAGE_COUNT; j++) { current_page_directory[i][j] = kernel_page_tables[i][j]; } } @@ -53,31 +53,30 @@ void paging_install() paging_set_present(0, memory_get_all() >> 3); paging_set_used(0, ((u32)KERNEL_END >> 12) + 1); - log("Installing paging"); paging_enable(); log("Installed paging"); // Test! - u32 *a = malloc(4096); - u32 *b = malloc(4096); + u32 *a = malloc(PAGE_ALIGN); + u32 *b = malloc(PAGE_ALIGN); free(b); free(a); u32 *c = malloc(2048); assert(a == c); c[42] = 0x4242; + assert(c[42] == 0x4242); free(c); info("Malloc test succeeded!"); } u32 **paging_make_directory() { - u32 **dir = valloc(1024 * 32); - dir[0] = valloc(1024 * 1024 * 32); - for (int i = 1; i < 1024; i++) - dir[i] = dir[0] + i * 1024; + u32 **dir = valloc(PAGE_COUNT * sizeof(*dir)); + dir[0] = valloc(PAGE_COUNT * PAGE_COUNT * sizeof(u32)); + for (int i = 1; i < PAGE_COUNT; i++) + dir[i] = dir[0] + i * PAGE_COUNT; paging_init(dir); - paging_set_present(0, memory_get_all() >> 3); return dir; } @@ -135,19 +134,19 @@ u16 paging_get_flags(u32 virt) void paging_set_flag_up(u32 virt, u32 count, u32 flag) { - u32 page_n = virt / 0x1000; + u32 page_n = virt / PAGE_ALIGN; for (u32 i = page_n; i < page_n + count; i++) { - current_page_directory[i / 1024][i % 1024] |= flag; - invlpg(i * 0x1000); + current_page_directory[i / PAGE_COUNT][i % PAGE_COUNT] |= flag; + invlpg(i * PAGE_ALIGN); } } void paging_set_flag_down(u32 virt, u32 count, u32 flag) { - u32 page_n = virt / 0x1000; + u32 page_n = virt / PAGE_ALIGN; for (u32 i = page_n; i < page_n + count; i++) { - current_page_directory[i / 1024][i % 1024] &= ~flag; - invlpg(i * 0x1000); + current_page_directory[i / PAGE_COUNT][i % PAGE_COUNT] &= ~flag; + invlpg(i * PAGE_ALIGN); } } @@ -176,8 +175,8 @@ u32 paging_find_pages(u32 count) u32 continuous = 0; u32 start_dir = 0; u32 start_page = 0; - for (u32 i = 0; i < 1024; i++) { - for (u32 j = 0; j < 1024; j++) { + for (u32 i = 0; i < PAGE_COUNT; i++) { + for (u32 j = 0; j < PAGE_COUNT; j++) { if (!(current_page_directory[i][j] & PT_PRESENT) || (current_page_directory[i][j] & PT_USED)) { continuous = 0; @@ -185,7 +184,7 @@ u32 paging_find_pages(u32 count) start_page = j + 1; } else { if (++continuous == count) - return (start_dir * 0x400000) + (start_page * 0x1000); + return (start_dir * PAGE_SIZE) + (start_page * PAGE_ALIGN); } } } @@ -204,8 +203,8 @@ u32 paging_alloc_pages(u32 count) u32 paging_get_used_pages() { u32 n = 0; - for (u32 i = 0; i < 1024; i++) { - for (u32 j = 0; j < 1024; j++) { + for (u32 i = 0; i < PAGE_COUNT; i++) { + for (u32 j = 0; j < PAGE_COUNT; j++) { u8 flags = current_page_directory[i][j] & PT_USED; if (flags == 1) n++; diff --git a/src/kernel/memory/paging.h b/src/kernel/memory/paging.h index f42bb2e..3d3b176 100644 --- a/src/kernel/memory/paging.h +++ b/src/kernel/memory/paging.h @@ -3,7 +3,9 @@ #include <stdint.h> -#define PAGE_SIZE 0x400000 +#define PAGE_ALIGN 4096 +#define PAGE_COUNT 1024 +#define PAGE_SIZE PAGE_ALIGN *PAGE_COUNT #define PD_PRESENT 1 << 0 #define PD_RW 1 << 1 diff --git a/src/kernel/tasks/process.c b/src/kernel/tasks/process.c index fa41313..e11cfee 100644 --- a/src/kernel/tasks/process.c +++ b/src/kernel/tasks/process.c @@ -187,8 +187,8 @@ struct process *process_make_new() proc->brk = 0x50000000; - /* for (int i = 0; i < 1024; i++) */ - /* proc->cr3[i] = kernel_page_directory[i]; */ + for (int i = 0; i < 1024; i++) + proc->cr3[i] = kernel_page_directory[i]; proc->pid = pid++; return proc; |