diff options
author | Marvin Borner | 2021-03-12 16:07:45 +0100 |
---|---|---|
committer | Marvin Borner | 2021-03-12 16:07:45 +0100 |
commit | f1751c121d48f2d8936c72bdc347777d1e7402d9 (patch) | |
tree | 399c08a59756d8980aad6c451e2b0e4c97b3911f /kernel/features | |
parent | 99b674a9e8d78b12188cc0280b990ec50de109d8 (diff) |
Let's gooo!
Diffstat (limited to 'kernel/features')
-rw-r--r-- | kernel/features/load.c | 37 | ||||
-rw-r--r-- | kernel/features/mm.c | 39 | ||||
-rw-r--r-- | kernel/features/proc.c | 20 |
3 files changed, 50 insertions, 46 deletions
diff --git a/kernel/features/load.c b/kernel/features/load.c index 31e22dc..ec9a387 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -6,45 +6,42 @@ #include <mm.h> #include <str.h> -// TODO: Fix pdi < 256! -#define PROC_DATA_ADDR 0xc000000 - #define PROC_STACK_SIZE 0x4000 -#define PROC_STACK_ADDR (PROC_DATA_ADDR - 256) -void proc_load(struct proc *proc, u32 entry) +/*void proc_load(struct proc *proc, u32 entry) { - /* memory_dir_switch(proc->page_dir); */ - u32 paddr = physical_alloc(PROC_STACK_SIZE); - virtual_map(proc->page_dir, PROC_STACK_ADDR, paddr, PROC_STACK_SIZE, - MEMORY_USER | MEMORY_CLEAR); + u32 stack = (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE, MEMORY_USER | MEMORY_CLEAR); - proc->regs.ebp = PROC_STACK_ADDR; - proc->regs.useresp = PROC_STACK_ADDR; + proc->regs.ebp = stack; + proc->regs.useresp = stack; proc->regs.eip = entry; proc->entry = entry; -} +}*/ int bin_load(const char *path, struct proc *proc) { struct stat s = { 0 }; vfs_stat(path, &s); struct proc *current = proc_current(); - struct page_dir *prev = current ? current->page_dir : memory_kernel_dir(); + struct page_dir *prev = current ? current->page_dir : virtual_kernel_dir(); u32 size = PAGE_ALIGN_UP(s.size); - memory_dir_switch(proc->page_dir); - u32 paddr = physical_alloc(size); - virtual_map(proc->page_dir, PROC_DATA_ADDR, paddr, size, MEMORY_USER | MEMORY_CLEAR); + memory_switch_dir(proc->page_dir); + u32 data = (u32)memory_alloc(proc->page_dir, size, MEMORY_USER | MEMORY_CLEAR); - if (!vfs_read(path, (void *)PROC_DATA_ADDR, 0, s.size)) { - memory_dir_switch(prev); + if (!vfs_read(path, (void *)data, 0, s.size)) { + memory_switch_dir(prev); return 1; } strcpy(proc->name, path); - proc_load(proc, PROC_DATA_ADDR); - memory_dir_switch(prev); + u32 stack = (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE, MEMORY_USER | MEMORY_CLEAR); + proc->regs.ebp = stack; + proc->regs.useresp = stack; + proc->regs.eip = data; + proc->entry = data; + + memory_switch_dir(prev); return 0; } diff --git a/kernel/features/mm.c b/kernel/features/mm.c index cde9a94..c39b8b7 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -6,7 +6,6 @@ #include <def.h> #include <mem.h> #include <mm.h> - #include <print.h> static struct page_dir kernel_dir ALIGNED(PAGE_SIZE) = { 0 }; @@ -138,7 +137,7 @@ static u8 physical_is_used(struct memory_range range) return 0; } -static struct memory_range physical_alloc(u32 size) +struct memory_range physical_alloc(u32 size) { assert(PAGE_ALIGNED(size)); @@ -165,34 +164,34 @@ static void physical_free(struct memory_range range) * Virtual */ -#define PDI(vaddr) ((vaddr) >> 22) +#define PDI(vaddr) (((vaddr) >> 22) & 0x03ff) #define PTI(vaddr) (((vaddr) >> 12) & 0x03ff) u8 virtual_present(struct page_dir *dir, u32 vaddr) { u32 pdi = PDI(vaddr); - u32 pti = PTI(vaddr); - union page_dir_entry *dir_entry = &dir->entries[pdi]; if (!dir_entry->bits.present) return 0; struct page_table *table = (struct page_table *)(dir_entry->bits.address * PAGE_SIZE); + + u32 pti = PTI(vaddr); union page_table_entry *table_entry = &table->entries[pti]; - return !table_entry->bits.present; + return table_entry->bits.present; } u32 virtual_to_physical(struct page_dir *dir, u32 vaddr) { u32 pdi = PDI(vaddr); - u32 pti = PTI(vaddr); - union page_dir_entry *dir_entry = &dir->entries[pdi]; if (!dir_entry->bits.present) return 0; struct page_table *table = (struct page_table *)(dir_entry->bits.address * PAGE_SIZE); + + u32 pti = PTI(vaddr); union page_table_entry *table_entry = &table->entries[pti]; if (!table_entry->bits.present) return 0; @@ -204,9 +203,8 @@ void virtual_map(struct page_dir *dir, struct memory_range prange, u32 vaddr, u3 { for (u32 i = 0; i < prange.size / PAGE_SIZE; i++) { u32 offset = i * PAGE_SIZE; - u32 pdi = PDI(vaddr + offset); - u32 pti = PTI(vaddr + offset); + u32 pdi = PDI(vaddr + offset); union page_dir_entry *dir_entry = &dir->entries[pdi]; struct page_table *table = (struct page_table *)(dir_entry->bits.address * PAGE_SIZE); @@ -219,6 +217,7 @@ void virtual_map(struct page_dir *dir, struct memory_range prange, u32 vaddr, u3 dir_entry->bits.address = (u32)(table) >> 12; } + u32 pti = PTI(vaddr + offset); union page_table_entry *table_entry = &table->entries[pti]; table_entry->bits.present = 1; table_entry->bits.writable = 1; @@ -263,14 +262,14 @@ void virtual_free(struct page_dir *dir, struct memory_range vrange) u32 offset = i * PAGE_SIZE; u32 pdi = PDI(vrange.base + offset); - u32 pti = PTI(vrange.base + offset); - union page_dir_entry *dir_entry = &dir->entries[pdi]; if (!dir_entry->bits.present) continue; struct page_table *table = (struct page_table *)(dir_entry->bits.address * PAGE_SIZE); + + u32 pti = PTI(vrange.base + offset); union page_table_entry *table_entry = &table->entries[pti]; if (table_entry->bits.present) @@ -284,12 +283,14 @@ struct page_dir *virtual_create_dir(void) { struct page_dir *dir = memory_alloc(&kernel_dir, sizeof(*dir), MEMORY_CLEAR); + memset(dir, 0, sizeof(*dir)); + for (u32 i = 0; i < 256; i++) { union page_dir_entry *dir_entry = &dir->entries[i]; - dir_entry->bits.user = 0; - dir_entry->bits.writable = 1; dir_entry->bits.present = 1; + dir_entry->bits.writable = 1; + dir_entry->bits.user = 0; dir_entry->bits.address = (u32)&kernel_tables[i] / PAGE_SIZE; } @@ -334,22 +335,26 @@ void *memory_alloc(struct page_dir *dir, u32 size, u32 flags) assert(PAGE_ALIGNED(size)); if (!size) - return 0; + goto err; struct memory_range prange = physical_alloc(size); if (prange.size == 0) - return 0; + goto err; u32 vaddr = virtual_alloc(dir, prange, flags).base; if (!vaddr) { physical_free(prange); - return 0; + goto err; } if (flags & MEMORY_CLEAR) memset((void *)vaddr, 0, size); return (void *)vaddr; + +err: + print("Memory allocation error!\n"); + return 0; } void *memory_alloc_identity(struct page_dir *dir, u32 flags) diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 19a92d3..4d1311e 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -57,7 +57,7 @@ void scheduler(struct regs *regs) } } - memory_dir_switch(((struct proc *)current->data)->page_dir); + memory_switch_dir(((struct proc *)current->data)->page_dir); memcpy(regs, &((struct proc *)current->data)->regs, sizeof(struct regs)); if (regs->cs != GDT_USER_CODE_OFFSET) { @@ -248,9 +248,9 @@ struct proc *proc_make(enum proc_priv priv) proc->state = PROC_RUNNING; if (priv == PROC_PRIV_KERNEL) - proc->page_dir = memory_kernel_dir(); + proc->page_dir = virtual_kernel_dir(); else - proc->page_dir = memory_dir_create(); + proc->page_dir = virtual_create_dir(); if (current) list_add(proc_list, proc); @@ -472,11 +472,11 @@ void proc_init(void) vfs_mount(dev, "/proc/"); // Idle proc - struct proc *kernel_proc = proc_make(PROC_PRIV_NONE); - proc_load(kernel_proc, (u32)kernel_idle); - strcpy(kernel_proc->name, "idle"); - kernel_proc->state = PROC_SLEEPING; - idle_proc = list_add(proc_list, kernel_proc); + /* struct proc *kernel_proc = proc_make(PROC_PRIV_NONE); */ + /* proc_load(kernel_proc, (u32)kernel_idle); */ + /* strcpy(kernel_proc->name, "idle"); */ + /* kernel_proc->state = PROC_SLEEPING; */ + /* idle_proc = list_add(proc_list, kernel_proc); */ // Init proc (root) struct node *new = list_add(proc_list, proc_make(PROC_PRIV_ROOT)); @@ -496,7 +496,9 @@ void proc_init(void) /* ((u32 *)_esp)[-1] = (u32)argv; // Second argument (argv) */ printf("Jumping to userspace!\n"); - memory_dir_switch(((struct proc *)new->data)->page_dir); + /* printf("%x\n", ((u32 *)((struct proc *)new->data)->entry)[5]); */ + memory_switch_dir(((struct proc *)new->data)->page_dir); + /* printf("%x\n", ((u32 *)((struct proc *)new->data)->entry)[5]); */ proc_jump_userspace(); while (1) { }; |