From 4b8518b4e791c68154ec52badcc921b62afafb49 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 28 Apr 2020 18:32:36 +0200 Subject: Maaaany fixes... Sorry for the increasingly worse-getting commit messages :D --- run | 10 +++++----- src/kernel/boot.asm | 17 ++++++++--------- src/kernel/fs/elf.c | 6 ++++-- src/kernel/gdt/gdt.c | 7 ++++++- src/kernel/graphics/vesa.c | 2 +- src/kernel/input/ps2/keyboard.c | 2 +- src/kernel/interrupts/interrupts.h | 6 ++++++ src/kernel/interrupts/isr.c | 2 +- src/kernel/kernel.c | 20 +++++++++++++------- src/kernel/memory/paging.c | 20 ++++++++++++++++++++ src/kernel/tasks/process.c | 4 ++-- src/kernel/tasks/process.h | 2 ++ src/kernel/tasks/userspace.c | 1 + src/userspace/main.c | 13 +++---------- 14 files changed, 73 insertions(+), 39 deletions(-) diff --git a/run b/run index b181689..42a8d05 100755 --- a/run +++ b/run @@ -54,8 +54,8 @@ make_cross() { # Get sources mkdir "${DIR}/src" && cd "${DIR}/src" echo "Downloading..." - curl -sSL "https://ftp.gnu.org/gnu/binutils/binutils-2.32.tar.xz" | tar xJ - curl -sSL "https://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.xz" | tar xJ + curl -sSL "https://ftp.gnu.org/gnu/binutils/binutils-2.34.tar.xz" | tar xJ + curl -sSL "https://ftp.gnu.org/gnu/gcc/gcc-9.3.0/gcc-9.3.0.tar.xz" | tar xJ # Prepare compiling mkdir -p "${DIR}/opt/bin" @@ -65,13 +65,13 @@ make_cross() { # Compile binutils mkdir "${DIR}/src/build-binutils" && cd "${DIR}/src/build-binutils" - ../binutils-2.32/configure --target="$TARGET" --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror + ../binutils-2.34/configure --target="$TARGET" --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror make make install # Compile GCC mkdir "${DIR}/src/build-gcc" && cd "${DIR}/src/build-gcc" - ../gcc-9.2.0/configure --target="$TARGET" --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers + ../gcc-9.3.0/configure --target="$TARGET" --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers make all-gcc make all-target-libgcc make install-gcc @@ -216,5 +216,5 @@ elif [ "${mode}" = "font" ]; then make_tidy else echo "Please use the following syntax:" - echo "./run {cross | build | clean | test | debug | image | sync | tidy | font}" + echo "./run {cross | build | clean | test | debug | image | sync | tidy | font} [-y]" fi diff --git a/src/kernel/boot.asm b/src/kernel/boot.asm index 363f76a..cc9c674 100644 --- a/src/kernel/boot.asm +++ b/src/kernel/boot.asm @@ -26,20 +26,19 @@ section .start_section dd _start ; Initialize stack -section .bss - align 16 - global STACK_BOTTOM - global STACK_TOP - - STACK_BOTTOM: - resb 0x4000 - STACK_TOP: +;section .bss +; align 16 +; +; STACK_BOTTOM: +; resb 0x4000 +; STACK_TOP: section .text global _start extern kernel_main _start: - mov esp, STACK_TOP + ;mov esp, STACK_TOP + push esp push ebx push eax cli diff --git a/src/kernel/fs/elf.c b/src/kernel/fs/elf.c index ec22d77..9ca98f0 100644 --- a/src/kernel/fs/elf.c +++ b/src/kernel/fs/elf.c @@ -25,7 +25,7 @@ struct process *elf_load(char *path) { uint8_t *file = read_file(path); if (!file) { - warn("File or directory not found: %s", file); + warn("File or directory not found: %s", path); return NULL; } @@ -40,8 +40,9 @@ struct process *elf_load(char *path) } struct process *proc = process_make_new(); - proc->name = "TEST"; + proc->name = "ROOT"; proc->registers.eip = header->entry; + paging_switch_directory(proc->cr3); uint32_t stk = (uint32_t)kmalloc_a(PAGE_S); proc->registers.useresp = 0x40000000 - (PAGE_S / 2); @@ -68,5 +69,6 @@ struct process *elf_load(char *path) } } + paging_switch_directory(paging_root_directory); return proc; } \ No newline at end of file diff --git a/src/kernel/gdt/gdt.c b/src/kernel/gdt/gdt.c index 33cec57..79ea51c 100644 --- a/src/kernel/gdt/gdt.c +++ b/src/kernel/gdt/gdt.c @@ -71,6 +71,8 @@ void gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, ui gdt[num].access = access; } +extern uint32_t stack_hold; + void gdt_install() { // Set GDT pointer and limit @@ -93,7 +95,10 @@ void gdt_install() gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); // Write TSS - tss_write(5, 0x10, 0x0); + tss_write(5, 0x10, stack_hold); + + gdt_set_gate(6, 0, 0xFFFFF, 0x92, 0x0); + gdt_set_gate(7, 0, 0xFFFFF, 0x9A, 0x0); // Remove old GDT and install the new changes! gdt_flush(); diff --git a/src/kernel/graphics/vesa.c b/src/kernel/graphics/vesa.c index 5284f7e..43b0799 100644 --- a/src/kernel/graphics/vesa.c +++ b/src/kernel/graphics/vesa.c @@ -184,7 +184,7 @@ void set_optimal_resolution() uint32_t fb_size = vbe_width * vbe_height * vbe_bpl; /* cursor_buffer = kmalloc(fb_size); */ - for (uint32_t z = 0; z < fb_size; z += 4096) { + for (uint32_t z = 0; z < fb_size; z += PAGE_S) { paging_map(paging_root_directory, (uint32_t)fb + z, (uint32_t)fb + z); /* paging_map(paging_root_directory, (uint32_t)cursor_buffer + z, */ /* (uint32_t)cursor_buffer + z); */ diff --git a/src/kernel/input/ps2/keyboard.c b/src/kernel/input/ps2/keyboard.c index b23d1bc..4dc5119 100644 --- a/src/kernel/input/ps2/keyboard.c +++ b/src/kernel/input/ps2/keyboard.c @@ -130,7 +130,7 @@ void keyboard_rate() void keyboard_clear_buffer() { - kfree(keyboard_buffer); + // kfree(keyboard_buffer); keyboard_buffer = (char *)kmalloc(4096); // 4KiB } diff --git a/src/kernel/interrupts/interrupts.h b/src/kernel/interrupts/interrupts.h index 9047104..6651cfd 100644 --- a/src/kernel/interrupts/interrupts.h +++ b/src/kernel/interrupts/interrupts.h @@ -80,6 +80,12 @@ void irq_handler(struct regs *r); */ int irq_is_installed(int irq); +/** + * Logs fault messages and panics + * @param r The registers + */ +void fault_handler(struct regs *r); + // Defined in isr.asm extern void isr0(); diff --git a/src/kernel/interrupts/isr.c b/src/kernel/interrupts/isr.c index c2d58ef..2837239 100644 --- a/src/kernel/interrupts/isr.c +++ b/src/kernel/interrupts/isr.c @@ -57,7 +57,7 @@ void isr_install_handler(size_t isr, irq_handler_t handler) isr_routines[isr] = handler; } -// Removes the custom IRQ handler +// Remove the custom IRQ handler void isr_uninstall_handler(size_t isr) { isr_routines[isr] = 0; diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 662bb57..3461913 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -19,8 +19,12 @@ #include #include -void kernel_main(uint32_t magic, uint32_t multiboot_address) +uint32_t stack_hold; + +void kernel_main(uint32_t magic, uint32_t multiboot_address, uint32_t esp) { + stack_hold = esp; + if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { vga_log("Invalid boot magic!"); halt_loop(); @@ -58,19 +62,21 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address) ata_init(); ext2_init_fs(); - log("%s", read_file("/etc/test")); load_binaries(); set_optimal_resolution(); + printf("%s", read_file("/etc/test")); syscalls_install(); struct process *proc = elf_load("/bin/user"); - proc->stdin = NULL; - proc->stdout = NULL; - proc->stderr = NULL; - process_init(proc); + if (proc) { + proc->stdin = NULL; + proc->stdout = NULL; + proc->stderr = NULL; + process_init(proc); + } + log("Okidoko!"); halt_loop(); - // asm ("div %0" :: "r"(0)); // Exception testing x/0 } \ No newline at end of file diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index 4192ba3..0789c2a 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include struct page_directory *paging_current_directory = NULL; struct page_directory *paging_root_directory = NULL; @@ -100,6 +102,21 @@ void paging_map_user(struct page_directory *dir, uint32_t phys, uint32_t virt) } } +void page_fault(struct regs *regs) +{ + cli(); + if (current_proc != NULL) { + memcpy(¤t_proc->registers, regs, sizeof(struct regs)); + process_suspend(current_proc->pid); + warn("Segfault, halting process %d", current_proc->pid); + scheduler(regs); + } else { + warn("Page fault before multitasking started!"); + // fault_handler(regs); + halt_loop(); + } +} + void paging_install() { kheap_init(); @@ -107,8 +124,11 @@ void paging_install() paging_current_directory = paging_make_directory(); paging_root_directory = paging_current_directory; + isr_install_handler(14, page_fault); for (uint32_t i = 0; i < 0xF0000000; i += PAGE_S) paging_map(paging_root_directory, i, i); + paging_switch_directory(paging_root_directory); + info("Installed paging"); } void paging_convert_page(struct page_directory *kdir) diff --git a/src/kernel/tasks/process.c b/src/kernel/tasks/process.c index 5ac4010..123d2d7 100644 --- a/src/kernel/tasks/process.c +++ b/src/kernel/tasks/process.c @@ -21,8 +21,6 @@ void scheduler(struct regs *regs) { memcpy(¤t_proc->registers, regs, sizeof(struct regs)); - debug("Task switch"); - timer_handler(regs); current_proc = current_proc->next; @@ -30,6 +28,8 @@ void scheduler(struct regs *regs) current_proc = root; } + debug("Task switch to %s", current_proc->name); + while (current_proc->state == PROC_ASLEEP) { current_proc = current_proc->next; if (current_proc == NULL) diff --git a/src/kernel/tasks/process.h b/src/kernel/tasks/process.h index fc277f4..1a5c607 100644 --- a/src/kernel/tasks/process.h +++ b/src/kernel/tasks/process.h @@ -34,6 +34,8 @@ struct process { struct process *next; }; +void scheduler(struct regs *regs); + void process_kill(uint32_t pid); uint32_t process_spawn(struct process *process); diff --git a/src/kernel/tasks/userspace.c b/src/kernel/tasks/userspace.c index f8d0472..8b88b87 100644 --- a/src/kernel/tasks/userspace.c +++ b/src/kernel/tasks/userspace.c @@ -30,6 +30,7 @@ void userspace_enter(struct process *proc) current_proc = proc; sti(); + debug("Jumping to userspace!"); jump_userspace(); } diff --git a/src/userspace/main.c b/src/userspace/main.c index 7a18f85..4a8ff33 100644 --- a/src/userspace/main.c +++ b/src/userspace/main.c @@ -1,20 +1,13 @@ #include #include -int32_t starts_with(const char *a, const char *b) -{ - size_t length_pre = strlen(b); - size_t length_main = strlen(a); - return length_main < length_pre ? 0 : memcmp(b, a, length_pre) == 0; -} - void main() { syscall_halt(); // As char[]: 0xC105BFD6 // As const char *: 0x8048B20 - char test[] = "banane"; - syscall_write(test); - syscall_halt(); + /* char test[] = "banane"; */ + /* syscall_write(test); */ + /* syscall_halt(); */ } \ No newline at end of file -- cgit v1.2.3