diff options
-rw-r--r-- | kernel/drivers/interrupts.c | 2 | ||||
-rw-r--r-- | kernel/features/fs.c | 1 | ||||
-rw-r--r-- | kernel/features/load.c | 28 | ||||
-rw-r--r-- | kernel/features/mm.c | 13 | ||||
-rw-r--r-- | kernel/features/proc.c | 5 | ||||
-rw-r--r-- | kernel/features/syscall.c | 4 | ||||
-rw-r--r-- | kernel/inc/mm.h | 7 | ||||
-rw-r--r-- | kernel/inc/proc.h | 7 | ||||
-rw-r--r-- | libs/libc/alloc.c | 8 | ||||
-rw-r--r-- | libs/libc/inc/sys.h | 2 | ||||
-rw-r--r-- | libs/libc/print.c | 2 | ||||
-rw-r--r-- | libs/libc/sys.c | 5 | ||||
-rwxr-xr-x | run | 2 |
13 files changed, 44 insertions, 42 deletions
diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c index 9898ef1..8ca6987 100644 --- a/kernel/drivers/interrupts.c +++ b/kernel/drivers/interrupts.c @@ -181,6 +181,8 @@ void isr_panic(struct regs *r) if (proc) { printf("\t-> Exception occurred in %s at addr 0x%x (offset 0x%x)\n", proc->name, r->eip, r->eip - proc->entry); + printf("\t\t-> Process: [entry: %x, kstack: %x, ustack: %x]\n", proc->entry, + proc->stack.kernel, proc->stack.user); proc_exit(proc, r, 1); } else { __asm__ volatile("cli\nhlt"); diff --git a/kernel/features/fs.c b/kernel/features/fs.c index 76c230e..82d00b6 100644 --- a/kernel/features/fs.c +++ b/kernel/features/fs.c @@ -40,6 +40,7 @@ u8 vfs_mounted(struct device *dev, const char *path) return 0; } +// TODO: Reduce allocations in VFS find static struct mount_info *vfs_recursive_find(char *path) { struct node *iterator = mount_points->head; diff --git a/kernel/features/load.c b/kernel/features/load.c index 02c02ae..77bb680 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -157,33 +157,21 @@ res elf_load(const char *name, struct proc *proc) memory_backup_dir(&prev); memory_switch_dir(proc->page_dir); - stac(); - // Allocate user stack with readonly lower and upper page boundary - u32 user_stack = - PAGE_SIZE + (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE + 2 * PAGE_SIZE, - MEMORY_USER | MEMORY_CLEAR); - virtual_remap_readonly(proc->page_dir, memory_range(user_stack - PAGE_SIZE, PAGE_SIZE)); - virtual_remap_readonly(proc->page_dir, - memory_range(user_stack + PROC_STACK_SIZE, PAGE_SIZE)); + u32 user_stack = (u32)memory_alloc_with_boundary(proc->page_dir, PROC_STACK_SIZE, + MEMORY_CLEAR | MEMORY_USER); // Allocate kernel stack with readonly lower and upper page boundary u32 kernel_stack = - PAGE_SIZE + - (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE + 2 * PAGE_SIZE, MEMORY_CLEAR); - virtual_remap_readonly(proc->page_dir, memory_range(kernel_stack - PAGE_SIZE, PAGE_SIZE)); - virtual_remap_readonly(proc->page_dir, - memory_range(kernel_stack + PROC_STACK_SIZE, PAGE_SIZE)); - - proc->user_stack = user_stack + PROC_STACK_SIZE; - proc->kernel_stack = kernel_stack + PROC_STACK_SIZE; - proc->regs.ebp = proc->user_stack; - proc->regs.useresp = proc->user_stack; + (u32)memory_alloc_with_boundary(proc->page_dir, PROC_STACK_SIZE, MEMORY_CLEAR); + + proc->stack.user = user_stack + PROC_STACK_SIZE; + proc->stack.kernel = kernel_stack + PROC_STACK_SIZE; + proc->regs.ebp = proc->stack.user; + proc->regs.useresp = proc->stack.user; proc->regs.eip = header.entry + rand_off; proc->entry = header.entry + rand_off; - clac(); - memory_switch_dir(prev); return EOK; } diff --git a/kernel/features/mm.c b/kernel/features/mm.c index 0972ab3..9a7a64e 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -82,14 +82,19 @@ void page_fault_handler(struct regs *r) u32 paddr = virtual_to_physical(dir, vaddr); // Print! + printf("%s process tried to %s a %s page at [vaddr=%x; paddr=%x]\n", super, operation, type, vaddr, paddr); + if (proc && vaddr > proc->regs.ebp - PROC_STACK_SIZE - PAGE_SIZE && vaddr < proc->regs.ebp + PAGE_SIZE) print("Probably a stack overflow\n"); + printf("Sections: [vaddr_section=%s; paddr_section=%s; eip_section=%s]\n", page_fault_section(vaddr), page_fault_section(paddr), page_fault_section(r->eip)); + /* printf("%b\n", virtual_entry(dir, vaddr)->uint); */ + isr_panic(r); } @@ -421,6 +426,14 @@ err: return NULL; } +void *memory_alloc_with_boundary(struct page_dir *dir, u32 size, u32 flags) +{ + u32 mem = PAGE_SIZE + (u32)memory_alloc(dir, size + 2 * PAGE_SIZE, flags); + virtual_remap_readonly(dir, memory_range(mem - PAGE_SIZE, PAGE_SIZE)); + virtual_remap_readonly(dir, memory_range(mem + size, PAGE_SIZE)); + return (void *)mem; +} + void *memory_alloc_identity(struct page_dir *dir, u32 flags) { for (u32 i = 1; i < PAGE_KERNEL_COUNT * PAGE_COUNT; i++) { diff --git a/kernel/features/proc.c b/kernel/features/proc.c index d5bf2e6..3a6b429 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -54,7 +54,7 @@ HOT FLATTEN void scheduler(struct regs *regs) current = idle_proc; } - tss_set_stack(0x10, PROC(current)->kernel_stack); + tss_set_stack(GDT_SUPER_DATA_OFFSET, PROC(current)->stack.kernel); memory_switch_dir(PROC(current)->page_dir); memcpy(regs, &PROC(current)->regs, sizeof(*regs)); @@ -223,6 +223,7 @@ end: proc_state(p, PROC_BLOCKED); } +// TODO: Rewrite block/unblock mechanisms void proc_unblock(u32 id, enum proc_block_type type) { struct page_dir *dir_bak; @@ -582,7 +583,7 @@ NORETURN void proc_init(void) // We'll shortly jump to usermode. Clear and protect every secret! memory_user_hook(); - tss_set_stack(0x10, init->kernel_stack); + tss_set_stack(GDT_SUPER_DATA_OFFSET, init->stack.kernel); memory_switch_dir(init->page_dir); printf("Jumping to userspace!\n"); diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index 98a7ff9..65777d8 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -23,10 +23,6 @@ static void syscall_handler(struct regs *r) /* printf("[SYSCALL] %d from %s\n", num, proc_current()->name); */ switch (num) { - case SYS_LOOP: { - panic("Loop is deprecated!\n"); - break; - } case SYS_ALLOC: { r->eax = memory_sys_alloc(proc_current()->page_dir, r->ebx, (u32 *)r->ecx, (u32 *)r->edx, (u8)r->esi); diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h index a9524ad..0f1b4ec 100644 --- a/kernel/inc/mm.h +++ b/kernel/inc/mm.h @@ -32,9 +32,9 @@ void physical_free(struct memory_range range); * Virtual */ -#define PAGE_SIZE 0x1000 -#define PAGE_COUNT 1024 -#define PAGE_KERNEL_COUNT 256 +#define PAGE_SIZE 0x1000u +#define PAGE_COUNT 1024u +#define PAGE_KERNEL_COUNT 256u #define PAGE_ALIGN(x) ((x) + PAGE_SIZE - ((x) % PAGE_SIZE)) #define PAGE_ALIGNED(x) ((x) % PAGE_SIZE == 0) #define PAGE_ALIGN_UP(x) (((x) % PAGE_SIZE == 0) ? (x) : (x) + PAGE_SIZE - ((x) % PAGE_SIZE)) @@ -122,6 +122,7 @@ struct memory_range memory_range_from(u32 base, u32 size); struct memory_range memory_range_around(u32 base, u32 size); void *memory_alloc(struct page_dir *dir, u32 size, u32 flags) NONNULL; +void *memory_alloc_with_boundary(struct page_dir *dir, u32 size, u32 flags) NONNULL; void *memory_alloc_identity(struct page_dir *dir, u32 flags) NONNULL; void memory_free(struct page_dir *dir, struct memory_range vrange) NONNULL; void memory_map_identity(struct page_dir *dir, struct memory_range prange, u32 flags) NONNULL; diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h index f5dbf1a..b8912a5 100644 --- a/kernel/inc/proc.h +++ b/kernel/inc/proc.h @@ -53,8 +53,6 @@ struct stream { struct proc { u32 pid; u32 entry; - u32 user_stack; - u32 kernel_stack; char name[64]; char dir[64]; @@ -70,6 +68,11 @@ struct proc { struct { u32 user; u32 kernel; + } stack; + + struct { + u32 user; + u32 kernel; } ticks; struct { diff --git a/libs/libc/alloc.c b/libs/libc/alloc.c index 2f8e0ba..4913479 100644 --- a/libs/libc/alloc.c +++ b/libs/libc/alloc.c @@ -403,7 +403,9 @@ void *malloc_debug(u32 size, const char *file, int line, const char *func, const (void)line; (void)func; (void)inp; - /* FUNC(PREFIX "MALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp); */ + /* #ifdef KERNEL */ + /* FUNC(PREFIX "MALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp); */ + /* #endif */ return ret; } @@ -415,5 +417,7 @@ void free_debug(void *ptr, const char *file, int line, const char *func, const c (void)line; (void)func; (void)inp; - /* FUNC(PREFIX "FREE\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp); */ + /* #ifdef KERNEL */ + /* FUNC(PREFIX "FREE\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp); */ + /* #endif */ } diff --git a/libs/libc/inc/sys.h b/libs/libc/inc/sys.h index 58d7969..a9ebf2e 100644 --- a/libs/libc/inc/sys.h +++ b/libs/libc/inc/sys.h @@ -15,7 +15,6 @@ #define SYS_BOOT_SHUTDOWN 0xdead enum sys { - SYS_LOOP, // To infinity and beyond (debug)! SYS_ALLOC, // Allocate memory SYS_SHACCESS, // Access shared memory SYS_FREE, // Free memory @@ -64,7 +63,6 @@ struct stat { * Syscall wrappers */ -void loop(void); void exit(s32 status) NORETURN; res read(const char *path, void *buf, u32 offset, u32 count) NONNULL; res write(const char *path, const void *buf, u32 offset, u32 count) NONNULL; diff --git a/libs/libc/print.c b/libs/libc/print.c index dcaa8cb..425eb01 100644 --- a/libs/libc/print.c +++ b/libs/libc/print.c @@ -251,7 +251,7 @@ NORETURN void panic(const char *format, ...) #ifdef KERNEL print("--- DON'T PANIC! ---\n"); print(buf); - print_trace(5); + print_trace(10); while (1) __asm__ volatile("cli\nhlt"); #else diff --git a/libs/libc/sys.c b/libs/libc/sys.c index 139f523..70dcc97 100644 --- a/libs/libc/sys.c +++ b/libs/libc/sys.c @@ -100,11 +100,6 @@ res shaccess(u32 id, u32 *addr, u32 *size) return sys3(SYS_SHACCESS, (int)id, (int)addr, (int)size); } -void loop(void) -{ - sys0(SYS_LOOP); -} - res read(const char *path, void *buf, u32 offset, u32 count) { return sys4(SYS_READ, (int)path, (int)buf, (int)offset, (int)count); @@ -26,7 +26,7 @@ no_ask="${2}" # TODO: Support -enable-kvm: GPF?! qemu_with_flags() { network="rtl8139" - qemu-system-i386 -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@" + qemu-system-i386 -d guest_errors -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@" } make_cross() { |