diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | apps/Makefile | 2 | ||||
-rw-r--r-- | apps/idle.c | 7 | ||||
-rw-r--r-- | apps/init.c | 1 | ||||
-rw-r--r-- | kernel/features/load.c | 17 | ||||
-rw-r--r-- | kernel/features/mm.c | 24 | ||||
-rw-r--r-- | kernel/features/proc.c | 19 | ||||
-rw-r--r-- | kernel/inc/load.h | 1 | ||||
-rw-r--r-- | kernel/inc/mm.h | 9 | ||||
-rw-r--r-- | libc/cpu.c | 7 | ||||
-rw-r--r-- | libc/inc/cpu.h | 1 | ||||
-rw-r--r-- | libc/print.c | 1 |
12 files changed, 51 insertions, 40 deletions
@@ -6,7 +6,7 @@ CFLAGS_DEFAULT = $(CFLAGS_WARNINGS) $(CFLAGS_OPTIMIZATION) -std=c99 -m32 -nostdl all: compile -debug: CFLAGS_DEFAULT += -Wno-error -ggdb3 -s -fstack-protector-all -fsanitize=undefined +debug: CFLAGS_DEFAULT += -Wno-error -ggdb3 -s -fsanitize=undefined # -fstack-protector-all # TODO: Fix stack protector in userspace debug: compile export diff --git a/apps/Makefile b/apps/Makefile index 8426b9c..2c6c643 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,6 +1,6 @@ # MIT License, Copyright (c) 2020 Marvin Borner -COBJS = init.o wm.o test.o window.o #mandelbrot.o window.o exec.o files.o test.o cc.o browser.o server.o +COBJS = init.o idle.o wm.o test.o window.o #mandelbrot.o window.o exec.o files.o test.o cc.o browser.o server.o CC = ccache ../cross/opt/bin/i686-elf-gcc LD = ccache ../cross/opt/bin/i686-elf-ld OC = ccache ../cross/opt/bin/i686-elf-objcopy diff --git a/apps/idle.c b/apps/idle.c new file mode 100644 index 0000000..3f10c3e --- /dev/null +++ b/apps/idle.c @@ -0,0 +1,7 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +int main(void) +{ + while (1) + ; +} diff --git a/apps/init.c b/apps/init.c index 7044365..9e57078 100644 --- a/apps/init.c +++ b/apps/init.c @@ -8,6 +8,7 @@ int main(int argc, char **argv) { + log("Loaded!\n"); while (1) { }; (void)argc; diff --git a/kernel/features/load.c b/kernel/features/load.c index ec9a387..4ad2cbf 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -8,25 +8,16 @@ #define PROC_STACK_SIZE 0x4000 -/*void proc_load(struct proc *proc, u32 entry) -{ - 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 = 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 : virtual_kernel_dir(); - u32 size = PAGE_ALIGN_UP(s.size); + struct page_dir *prev; + memory_backup_dir(&prev); memory_switch_dir(proc->page_dir); + + u32 size = PAGE_ALIGN_UP(s.size); u32 data = (u32)memory_alloc(proc->page_dir, size, MEMORY_USER | MEMORY_CLEAR); if (!vfs_read(path, (void *)data, 0, s.size)) { diff --git a/kernel/features/mm.c b/kernel/features/mm.c index c39b8b7..5fe70fd 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -15,10 +15,10 @@ static struct page_table kernel_tables[256] ALIGNED(PAGE_SIZE) = { 0 }; * Lowlevel paging */ -static void paging_disable(void) +/*static void paging_disable(void) { cr0_set(cr0_get() | 0x7fffffff); -} +}*/ static void paging_enable(void) { @@ -27,7 +27,6 @@ static void paging_enable(void) static void paging_switch_dir(u32 dir) { - assert(dir); cr3_set(dir); } @@ -154,7 +153,7 @@ struct memory_range physical_alloc(u32 size) return memory_range(0, 0); } -static void physical_free(struct memory_range range) +void physical_free(struct memory_range range) { assert(PAGE_ALIGNED(range.base) && PAGE_ALIGNED(range.size)); physical_set_free(range); @@ -297,11 +296,6 @@ struct page_dir *virtual_create_dir(void) return dir; } -struct page_dir *virtual_kernel_dir(void) -{ - return &kernel_dir; -} - void virtual_destroy_dir(struct page_dir *dir) { assert(dir != &kernel_dir); @@ -326,6 +320,11 @@ void virtual_destroy_dir(struct page_dir *dir) memory_free(&kernel_dir, memory_range((u32)dir, sizeof(*dir))); } +struct page_dir *virtual_kernel_dir(void) +{ + return &kernel_dir; +} + /** * Memory wrappers */ @@ -405,6 +404,13 @@ void memory_switch_dir(struct page_dir *dir) paging_switch_dir(virtual_to_physical(&kernel_dir, (u32)dir)); } +void memory_backup_dir(struct page_dir **backup) +{ + struct proc *proc = proc_current(); + struct page_dir *dir = proc ? proc->page_dir : virtual_kernel_dir(); + *backup = dir; +} + struct memory_range memory_range_from(u32 base, u32 size) { u32 align = PAGE_SIZE - base % PAGE_SIZE; diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 4d1311e..db2291c 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -76,12 +76,6 @@ void scheduler(struct regs *regs) /* printf("{%d}", ((struct proc *)current->data)->pid); */ } -static void kernel_idle(void) -{ - while (1) - ; -} - void proc_print(void) { struct node *node = proc_list->head; @@ -328,7 +322,7 @@ static s32 procfs_write(const char *path, void *buf, u32 offset, u32 count, stru } } - printf("%s - off: %d, cnt: %d, buf: %x, dev %x\n", path, offset, count, buf, dev); + printf("ERR: %s - off: %d, cnt: %d, buf: %x, dev %x\n", path, offset, count, buf, dev); return -1; } @@ -472,11 +466,10 @@ 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); + bin_load("/bin/idle", kernel_proc); + 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,9 +489,7 @@ void proc_init(void) /* ((u32 *)_esp)[-1] = (u32)argv; // Second argument (argv) */ printf("Jumping to userspace!\n"); - /* 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) { }; diff --git a/kernel/inc/load.h b/kernel/inc/load.h index 330caca..f493e84 100644 --- a/kernel/inc/load.h +++ b/kernel/inc/load.h @@ -5,7 +5,6 @@ #include <proc.h> -void proc_load(struct proc *proc, u32 entry); int bin_load(const char *path, struct proc *proc); #endif diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h index aad4ffd..6a1c063 100644 --- a/kernel/inc/mm.h +++ b/kernel/inc/mm.h @@ -16,6 +16,7 @@ struct memory_range { */ struct memory_range physical_alloc(u32 size); +void physical_free(struct memory_range range); /** * Virtual @@ -70,11 +71,14 @@ struct page_dir { union page_dir_entry entries[PAGE_COUNT]; } PACKED; +u8 virtual_present(struct page_dir *dir, u32 vaddr); u32 virtual_to_physical(struct page_dir *dir, u32 vaddr); void virtual_map(struct page_dir *dir, struct memory_range prange, u32 vaddr, u32 flags); struct memory_range virtual_alloc(struct page_dir *dir, struct memory_range physical_range, u32 flags); +void virtual_free(struct page_dir *dir, struct memory_range vrange); struct page_dir *virtual_create_dir(void); +void virtual_destroy_dir(struct page_dir *dir); struct page_dir *virtual_kernel_dir(void); /** @@ -89,10 +93,13 @@ struct page_dir *virtual_kernel_dir(void); struct memory_range memory_range_from(u32 base, u32 size); struct memory_range memory_range_around(u32 base, u32 size); -void memory_install(struct mem_info *mem_info); void *memory_alloc(struct page_dir *dir, u32 size, u32 flags); void *memory_alloc_identity(struct page_dir *dir, u32 flags); +void memory_map_identity(struct page_dir *dir, struct memory_range prange, u32 flags); void memory_free(struct page_dir *dir, struct memory_range vrange); void memory_switch_dir(struct page_dir *dir); +void memory_backup_dir(struct page_dir **backup); + +void memory_install(struct mem_info *mem_info); #endif @@ -82,6 +82,13 @@ void cr0_set(u32 cr0) __asm__ volatile("movl %%eax, %%cr0" ::"a"(cr0)); } +u32 cr3_get(void) +{ + u32 cr3; + __asm__ volatile("movl %%cr0, %%eax" : "=a"(cr3)); + return cr3; +} + void cr3_set(u32 cr3) { __asm__ volatile("movl %%eax, %%cr3" ::"a"(cr3)); diff --git a/libc/inc/cpu.h b/libc/inc/cpu.h index 161b7a4..d709d86 100644 --- a/libc/inc/cpu.h +++ b/libc/inc/cpu.h @@ -29,6 +29,7 @@ void fpu_restore(void); u32 cr0_get(void); void cr0_set(u32 cr0); +u32 cr3_get(void); void cr3_set(u32 cr3); u32 cr4_get(void); void cr4_set(u32 cr4); diff --git a/libc/print.c b/libc/print.c index 173e117..1c577e5 100644 --- a/libc/print.c +++ b/libc/print.c @@ -238,6 +238,7 @@ NORETURN void panic(const char *format, ...) vsprintf(buf, format, ap); va_end(ap); #ifdef kernel + print("--- DON'T PANIC! ---\n"); print(buf); loop(); #else |