diff options
-rw-r--r-- | apps/init.c | 2 | ||||
-rw-r--r-- | kernel/drivers/interrupts.c | 4 | ||||
-rw-r--r-- | kernel/features/load.c | 37 | ||||
-rw-r--r-- | kernel/features/mm.c | 39 | ||||
-rw-r--r-- | kernel/features/proc.c | 20 | ||||
-rw-r--r-- | kernel/inc/mm.h | 2 | ||||
-rw-r--r-- | kernel/main.c | 4 | ||||
-rw-r--r-- | libc/alloc.c | 12 | ||||
-rw-r--r-- | libc/inc/def.h | 1 | ||||
-rw-r--r-- | libc/inc/print.h | 3 | ||||
-rw-r--r-- | libc/print.c | 2 | ||||
-rw-r--r-- | libc/sanitize.c | 4 |
12 files changed, 77 insertions, 53 deletions
diff --git a/apps/init.c b/apps/init.c index 87f0d82..7044365 100644 --- a/apps/init.c +++ b/apps/init.c @@ -8,6 +8,8 @@ int main(int argc, char **argv) { + while (1) { + }; (void)argc; log("%s loaded\n", argv[0]); diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c index 55926b7..a0cd106 100644 --- a/kernel/drivers/interrupts.c +++ b/kernel/drivers/interrupts.c @@ -171,9 +171,9 @@ void isr_uninstall_handler(int isr) void isr_panic(struct regs *r) { + printf("%s Exception (%x) at 0x%x (ring %d), exiting!\n", isr_exceptions[r->int_no], + r->err_code, r->eip, r->cs & 3); struct proc *proc = proc_current(); - printf("%s Exception (%x) at 0x%x, exiting!\n", isr_exceptions[r->int_no], r->err_code, - r->eip); if (proc) { printf("\t-> Exception occurred in %s at addr 0x%x\n", proc->name, r->eip - proc->entry); 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) { }; diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h index 6d3714d..aad4ffd 100644 --- a/kernel/inc/mm.h +++ b/kernel/inc/mm.h @@ -15,6 +15,8 @@ struct memory_range { * Physical */ +struct memory_range physical_alloc(u32 size); + /** * Virtual */ diff --git a/kernel/main.c b/kernel/main.c index f0bfc3c..8247672 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -17,6 +17,8 @@ #include <syscall.h> #include <timer.h> +#include <print.h> + struct vid_info *boot_passed; void kernel_main(struct mem_info *mem_info, struct vid_info *vid_info); // Decl @@ -31,7 +33,7 @@ void kernel_main(struct mem_info *mem_info, struct vid_info *vid_info) serial_print("\nKernel was compiled at " __TIME__ " on " __DATE__ "\n"); serial_print("Serial connected.\n"); - paging_install(mem_info); + memory_install(mem_info); boot_passed = vid_info; diff --git a/libc/alloc.c b/libc/alloc.c index 11639a6..f621c4e 100644 --- a/libc/alloc.c +++ b/libc/alloc.c @@ -303,6 +303,9 @@ static void _free(void *ptr) void *zalloc(u32 size) { +#ifdef userspace + panic("AAH!\n"); +#endif void *ret = malloc(size); memset(ret, 0, size); return ret; @@ -311,6 +314,9 @@ void *zalloc(u32 size) // Naive realloc implementation - TODO! void *realloc(void *ptr, u32 size) { +#ifdef userspace + panic("AAH!\n"); +#endif if (!ptr) return malloc(size); @@ -330,6 +336,9 @@ void *realloc(void *ptr, u32 size) void *malloc_debug(u32 size, const char *file, int line, const char *func, const char *inp) { +#ifdef userspace + panic("AAH!\n"); +#endif assert(size < (100 << 20)); // Don't brag with memory pls void *ret = _malloc(size); @@ -343,6 +352,9 @@ void *malloc_debug(u32 size, const char *file, int line, const char *func, const void free_debug(void *ptr, const char *file, int line, const char *func, const char *inp) { +#ifdef userspace + panic("AAH!\n"); +#endif if (ptr) _free(ptr); diff --git a/libc/inc/def.h b/libc/inc/def.h index 945ccb0..db1c95e 100644 --- a/libc/inc/def.h +++ b/libc/inc/def.h @@ -25,6 +25,7 @@ typedef unsigned long long u64; #define UNUSED(a) ((void)(a)) +#define NORETURN __attribute__((noreturn)) #define NO_SANITIZE __attribute__((no_sanitize("undefined"))) #define PACKED __attribute__((packed)) #define ALIGNED(align) __attribute__((aligned(align))) diff --git a/libc/inc/print.h b/libc/inc/print.h index 110ba4c..58b5dc6 100644 --- a/libc/inc/print.h +++ b/libc/inc/print.h @@ -4,13 +4,14 @@ #define PRINT_H #include "arg.h" +#include <def.h> int printf(const char *format, ...); int vprintf(const char *format, va_list ap); int sprintf(char *str, const char *format, ...); int vsprintf(char *str, const char *format, va_list ap); int print(const char *str); -void panic(const char *format, ...); +NORETURN void panic(const char *format, ...); #ifdef userspace int vfprintf(const char *path, const char *format, va_list ap); diff --git a/libc/print.c b/libc/print.c index ca2ab98..173e117 100644 --- a/libc/print.c +++ b/libc/print.c @@ -230,7 +230,7 @@ void print_trace(u32 count) #endif -void panic(const char *format, ...) +NORETURN void panic(const char *format, ...) { char buf[1024] = { 0 }; va_list ap; diff --git a/libc/sanitize.c b/libc/sanitize.c index 8cfec49..8514e49 100644 --- a/libc/sanitize.c +++ b/libc/sanitize.c @@ -13,13 +13,13 @@ u32 __stack_chk_guard = STACK_CHK_GUARD; void __stack_chk_fail(void); -void __stack_chk_fail(void) +NORETURN void __stack_chk_fail(void) { panic("FATAL: Stack smashing detected\n"); } void __stack_chk_fail_local(void); -void __stack_chk_fail_local(void) +NORETURN void __stack_chk_fail_local(void) { panic("FATAL: Local stack smashing detected\n"); } |