diff options
author | Marvin Borner | 2020-04-28 20:59:57 +0200 |
---|---|---|
committer | Marvin Borner | 2020-04-28 20:59:57 +0200 |
commit | 5f8b5ce7efb7738eaebad43f9648975788ae19ff (patch) | |
tree | ab8fc4d4baa4adb99dc90461df689650acf34cef /src/kernel | |
parent | bfe16de4be67565f1a1e7b1331fcbe3aedf9c54e (diff) |
Fixed userspace entering...
Many other fixes too, but I won't mention them because I don't want to
:)
Diffstat (limited to 'src/kernel')
27 files changed, 90 insertions, 178 deletions
diff --git a/src/kernel/input/input.h b/src/kernel/input/input.h index 9dc9276..172f0ae 100644 --- a/src/kernel/input/input.h +++ b/src/kernel/input/input.h @@ -12,10 +12,6 @@ void mouse_install(); */ void keyboard_install(); -void keyboard_clear_buffer(); - char keyboard_char_buffer; -char *keyboard_buffer; - #endif
\ No newline at end of file diff --git a/src/kernel/input/ps2/keyboard.c b/src/kernel/input/ps2/keyboard.c index 4dc5119..2056045 100644 --- a/src/kernel/input/ps2/keyboard.c +++ b/src/kernel/input/ps2/keyboard.c @@ -108,7 +108,6 @@ void keyboard_handler(struct regs *r) } keyboard_char_buffer = current_keymap[scan_code]; - keyboard_buffer[strlen(keyboard_buffer)] = keyboard_char_buffer; } else { // RELEASE if (current_keymap[scan_code] == (int)0xffffffb5) // TODO: IDK WHY -107?! control_pressed = 0; @@ -128,16 +127,8 @@ void keyboard_rate() outb(0x60, 0x0); // Rate{00000} Delay{00} 0 } -void keyboard_clear_buffer() -{ - // kfree(keyboard_buffer); - keyboard_buffer = (char *)kmalloc(4096); // 4KiB -} - -// Installs the keyboard handler into IRQ1 void keyboard_install() { - keyboard_clear_buffer(); keyboard_rate(); irq_install_handler(1, keyboard_handler); shift_pressed = 0; diff --git a/src/kernel/interrupts/isr.c b/src/kernel/interrupts/isr.c index 2837239..9d92529 100644 --- a/src/kernel/interrupts/isr.c +++ b/src/kernel/interrupts/isr.c @@ -3,7 +3,10 @@ #include <kernel/system.h> #include <kernel/lib/string.h> #include <kernel/lib/stdio.h> +#include <kernel/lib/lib.h> #include <kernel/graphics/vesa.h> +#include <kernel/tasks/process.h> +#include <kernel/io/io.h> // Install ISRs in IDT void isrs_install() @@ -107,6 +110,7 @@ void fault_handler(struct regs *r) if (handler) { handler(r); } else { + cli(); uint32_t faulting_address; asm("mov %%cr2, %0" : "=r"(faulting_address)); @@ -114,17 +118,22 @@ void fault_handler(struct regs *r) r->eip, r->eax, r->ebx, r->ecx, r->edx, r->esp, faulting_address, r->eflags, r->err_code, r->int_no, exception_messages[r->int_no]); + char *message; if (r->int_no <= 32) { - char *message = (char *)exception_messages[r->int_no]; + message = (char *)exception_messages[r->int_no]; strcat(message, " Exception"); + } else { + message = "Unknown Exception"; + } - // Show message if there wasn't an error in video memory - if (faulting_address != (uint32_t)fb || fb == 0) - panic(message); - else - halt_loop(); + if (current_proc != NULL) { + memcpy(¤t_proc->registers, r, sizeof(struct regs)); + process_suspend(current_proc->pid); + warn("%s: Halting process %d", message, current_proc->pid); + scheduler(r); + sti(); } else { - panic("Unknown Exception"); + panic("Page fault before multitasking started!"); } } }
\ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 1b34a73..3efe193 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -44,8 +44,8 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address, uint32_t esp) isrs_install(); irq_install(); - memory_init(multiboot_address); paging_install(); + log("0x%x", multiboot_address); // TODO: Fix multiboot table mmap multiboot_parse(multiboot_address); // Install drivers @@ -79,4 +79,4 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address, uint32_t esp) log("Okidoko!"); halt_loop(); // asm ("div %0" :: "r"(0)); // Exception testing x/0 -} +}
\ No newline at end of file diff --git a/src/kernel/lib/lib.h b/src/kernel/lib/lib.h index 7ebae9a..b08904f 100644 --- a/src/kernel/lib/lib.h +++ b/src/kernel/lib/lib.h @@ -36,8 +36,6 @@ void memory_info_init(struct multiboot_tag_basic_meminfo *tag); void memory_mmap_init(struct multiboot_tag_mmap *tag); -int memory_init(uint32_t multiboot_address); - void memory_print(); uint32_t memory_get_all(); diff --git a/src/kernel/lib/memory.c b/src/kernel/lib/memory.c index 6aed060..30f75f9 100644 --- a/src/kernel/lib/memory.c +++ b/src/kernel/lib/memory.c @@ -96,24 +96,4 @@ void memory_mmap_init(struct multiboot_tag_mmap *tag) } } total = sum >> 10; // I want kb -} - -int memory_init(uint32_t multiboot_address) -{ - int ret = 0; - struct multiboot_tag *tag; - - for (tag = (struct multiboot_tag *)(multiboot_address + 8); - tag->type != MULTIBOOT_TAG_TYPE_END; - tag = (struct multiboot_tag *)((multiboot_uint8_t *)tag + ((tag->size + 7) & ~7))) { - if (tag->type == MULTIBOOT_TAG_TYPE_BASIC_MEMINFO) { - info("Got memory info"); - memory_info_init((struct multiboot_tag_basic_meminfo *)tag); - } else if (tag->type == MULTIBOOT_TAG_TYPE_MMAP) { - info("Got memory map"); - memory_mmap_init((struct multiboot_tag_mmap *)tag); - ret = 1; - } - } - return ret; }
\ No newline at end of file diff --git a/src/kernel/lib/stdio.h b/src/kernel/lib/stdio.h index 8668775..7dae60e 100644 --- a/src/kernel/lib/stdio.h +++ b/src/kernel/lib/stdio.h @@ -5,9 +5,7 @@ char getch(); -char *readline(); - -void writec(char c); +void putch(char c); void vprintf(const char *fmt, va_list args); diff --git a/src/kernel/lib/stdio/getch.c b/src/kernel/lib/stdio/getch.c index e806013..4f6ed39 100644 --- a/src/kernel/lib/stdio/getch.c +++ b/src/kernel/lib/stdio/getch.c @@ -1,11 +1,11 @@ #include <kernel/input/input.h> #include <kernel/timer/timer.h> +#include <kernel/system.h> char getch() { keyboard_char_buffer = 0; while (keyboard_char_buffer == 0) { - timer_wait(1); // IDK why! - } + }; return keyboard_char_buffer; }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/writec.c b/src/kernel/lib/stdio/putch.c index 83b05de..f7e0248 100644 --- a/src/kernel/lib/stdio/writec.c +++ b/src/kernel/lib/stdio/putch.c @@ -1,6 +1,6 @@ #include <kernel/graphics/vesa.h> -void writec(char c) +void putch(char c) { vesa_draw_char(c); }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/readline.c b/src/kernel/lib/stdio/readline.c deleted file mode 100644 index 1bda252..0000000 --- a/src/kernel/lib/stdio/readline.c +++ /dev/null @@ -1,10 +0,0 @@ -#include <kernel/input/input.h> -#include <kernel/lib/string.h> - -char *readline() -{ - keyboard_clear_buffer(); - while (keyboard_buffer[strlen(keyboard_buffer) - 1] != '\n') { - } - return keyboard_buffer; -}
\ No newline at end of file diff --git a/src/kernel/lib/stdio/vprintf.c b/src/kernel/lib/stdio/vprintf.c index b9188c6..2431a48 100644 --- a/src/kernel/lib/stdio/vprintf.c +++ b/src/kernel/lib/stdio/vprintf.c @@ -5,10 +5,10 @@ #include <kernel/lib/stdlib.h> #include <kernel/memory/alloc.h> -void _writes(const char *data) +void _puts(const char *data) { for (size_t i = 0; i < strlen(data); i++) - writec(data[i]); + putch(data[i]); } void vprintf(const char *fmt, va_list args) @@ -20,7 +20,7 @@ void vprintf(const char *fmt, va_list args) for (; *fmt; fmt++) { if (readyToFormat) { if (*fmt == '%') { - writec('%'); + putch('%'); readyToFormat = 0; continue; } @@ -28,27 +28,27 @@ void vprintf(const char *fmt, va_list args) buff = *fmt; if (buff == 's') { const char *str = va_arg(args, const char *); - _writes(str); + _puts(str); readyToFormat = 0; } else if (buff == 'x') { char *p = htoa((uint32_t)va_arg(args, int)); - _writes(p); + _puts(p); kfree(p); readyToFormat = 0; } else if (buff == 'd') { char *p = itoa(va_arg(args, int)); - _writes(p); + _puts(p); kfree(p); readyToFormat = 0; } else if (buff == 'c') { - writec((char)va_arg(args, int)); + putch((char)va_arg(args, int)); readyToFormat = 0; } } else { if (*fmt == '%') readyToFormat = 1; else - writec(*fmt); + putch(*fmt); } } }
\ No newline at end of file diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index 0789c2a..9075d1d 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -102,21 +102,6 @@ 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(); @@ -124,11 +109,19 @@ 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"); + + // Test mallocing + uintptr_t a = (uintptr_t)kmalloc(4096); // TODO: Fix "can't collapse top of heap" + uintptr_t b = (uintptr_t)kmalloc(4096); + kfree((void *)b); + kfree((void *)a); + uintptr_t c = (uintptr_t)kmalloc(2048); + assert(a == c); + info("kmalloc test succeeded!"); } void paging_convert_page(struct page_directory *kdir) diff --git a/src/kernel/multiboot.c b/src/kernel/multiboot.c index fe59f45..086e628 100644 --- a/src/kernel/multiboot.c +++ b/src/kernel/multiboot.c @@ -24,9 +24,17 @@ void multiboot_parse(uint32_t multiboot_address) case MULTIBOOT_TAG_TYPE_MODULE: debug("Got modules"); break; + case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: + debug("Got memory info"); + memory_info_init((struct multiboot_tag_basic_meminfo *)tag); + break; case MULTIBOOT_TAG_TYPE_BOOTDEV: debug("Got boot device"); break; + case MULTIBOOT_TAG_TYPE_MMAP: + debug("Got memory map"); + memory_mmap_init((struct multiboot_tag_mmap *)tag); + break; case MULTIBOOT_TAG_TYPE_VBE: debug("Got VBE debug"); break; diff --git a/src/kernel/syscall/actions/sys_alloc.c b/src/kernel/syscall/actions/sys_alloc.c deleted file mode 100644 index 65269de..0000000 --- a/src/kernel/syscall/actions/sys_alloc.c +++ /dev/null @@ -1,6 +0,0 @@ -#include <stdint.h> - -uint32_t sys_alloc(uint32_t count) -{ - return 0; // (uint32_t) umalloc(count); -}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_free.c b/src/kernel/syscall/actions/sys_free.c index cbd4c70..480cd53 100644 --- a/src/kernel/syscall/actions/sys_free.c +++ b/src/kernel/syscall/actions/sys_free.c @@ -3,6 +3,6 @@ uint32_t sys_free(uint32_t ptr) { - kfree((void *)ptr); + ufree((void *)ptr); return 0; }
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_get_pointers.c b/src/kernel/syscall/actions/sys_get_pointers.c deleted file mode 100644 index 181630b..0000000 --- a/src/kernel/syscall/actions/sys_get_pointers.c +++ /dev/null @@ -1,18 +0,0 @@ -#include <stdint.h> -#include <kernel/graphics/vesa.h> -#include <kernel/fs/load.h> -#include <kernel/memory/alloc.h> - -struct userspace_pointers { - unsigned char *fb; - struct font *font; -}; - -uint32_t sys_get_pointers() -{ - struct userspace_pointers *pointers = - (struct userspace_pointers *)kmalloc(sizeof(struct userspace_pointers)); - pointers->fb = fb; - pointers->font = font; - return (uint32_t)pointers; -}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_getch.c b/src/kernel/syscall/actions/sys_getch.c new file mode 100644 index 0000000..f1e4dfb --- /dev/null +++ b/src/kernel/syscall/actions/sys_getch.c @@ -0,0 +1,10 @@ +#include <stdint.h> +#include <kernel/lib/stdio.h> +#include <kernel/input/input.h> +#include <kernel/lib/lib.h> +#include <kernel/lib/string.h> + +uint32_t sys_getch() +{ + return (uint32_t)getch(); +}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_malloc.c b/src/kernel/syscall/actions/sys_malloc.c new file mode 100644 index 0000000..8adc362 --- /dev/null +++ b/src/kernel/syscall/actions/sys_malloc.c @@ -0,0 +1,7 @@ +#include <stdint.h> +#include <kernel/memory/alloc.h> + +uint32_t sys_malloc(uint32_t count) +{ + return (uint32_t)umalloc(count); +}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_putch.c b/src/kernel/syscall/actions/sys_putch.c new file mode 100644 index 0000000..beaa4a2 --- /dev/null +++ b/src/kernel/syscall/actions/sys_putch.c @@ -0,0 +1,9 @@ +#include <stdint.h> +#include <kernel/lib/stdio.h> +#include <kernel/lib/string.h> + +uint32_t sys_putch(char ch) +{ + putch(ch); + return 0; +}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_read.c b/src/kernel/syscall/actions/sys_read.c deleted file mode 100644 index 24d83d1..0000000 --- a/src/kernel/syscall/actions/sys_read.c +++ /dev/null @@ -1,21 +0,0 @@ -#include <stdint.h> -#include <kernel/lib/stdio.h> -#include <kernel/input/input.h> -#include <kernel/lib/lib.h> -#include <kernel/lib/string.h> - -uint32_t sys_read(char *buf) -{ - keyboard_clear_buffer(); - keyboard_char_buffer = 0; - while (keyboard_char_buffer != '\n') { - getch(); - } - memcpy(buf, keyboard_buffer, strlen(keyboard_buffer)); - return strlen(buf); -} - -uint32_t sys_readc() -{ - return (uint32_t)getch(); -}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_write.c b/src/kernel/syscall/actions/sys_write.c deleted file mode 100644 index f15004f..0000000 --- a/src/kernel/syscall/actions/sys_write.c +++ /dev/null @@ -1,15 +0,0 @@ -#include <stdint.h> -#include <kernel/lib/stdio.h> -#include <kernel/lib/string.h> - -uint32_t sys_write(char *buf) -{ - printf(buf); - return strlen((const char *)buf); -} - -uint32_t sys_writec(char ch) -{ - writec(ch); - return 0; -}
\ No newline at end of file diff --git a/src/kernel/syscall/syscall.c b/src/kernel/syscall/syscall.c index f883f2e..a106f07 100644 --- a/src/kernel/syscall/syscall.c +++ b/src/kernel/syscall/syscall.c @@ -3,20 +3,19 @@ #include <kernel/interrupts/interrupts.h> #include <kernel/system.h> #include <kernel/lib/stdio.h> +#include <kernel/io/io.h> -typedef uint32_t (*syscall_func)(unsigned int, ...); +typedef uint32_t (*syscall_func)(uint32_t, ...); uint32_t (*syscalls[])() = { [0] = (uint32_t(*)())halt_loop, // DEBUG! - [1] = sys_write, - [2] = sys_read, - [3] = (uint32_t(*)())sys_writec, - [4] = sys_readc, - [5] = sys_get_pointers, - [6] = sys_alloc, - [7] = sys_free }; + [1] = sys_putch, + [2] = sys_getch, + [3] = sys_malloc, + [4] = sys_free }; void syscall_handler(struct regs *r) { + sti(); log("Received syscall!"); if (r->eax >= sizeof(syscalls) / sizeof(*syscalls)) @@ -35,4 +34,4 @@ void syscall_handler(struct regs *r) void syscalls_install() { isr_install_handler(0x80, syscall_handler); -} +}
\ No newline at end of file diff --git a/src/kernel/syscall/syscall.h b/src/kernel/syscall/syscall.h index 229cf04..b2bdc6d 100644 --- a/src/kernel/syscall/syscall.h +++ b/src/kernel/syscall/syscall.h @@ -1,21 +1,17 @@ #ifndef MELVIX_SYSCALL_H #define MELVIX_SYSCALL_H +#include <stdint.h> + extern void idt_syscall(); void syscalls_install(); -uint32_t sys_write(char *buf); - -uint32_t sys_writec(char ch); - -uint32_t sys_read(); - -uint32_t sys_readc(char *ch); +uint32_t sys_putch(char ch); -uint32_t sys_get_pointers(); +uint32_t sys_getch(); -uint32_t sys_alloc(uint32_t count); +uint32_t sys_malloc(uint32_t count); uint32_t sys_free(uint32_t ptr); diff --git a/src/kernel/system.c b/src/kernel/system.c index ba798a0..6d5afca 100644 --- a/src/kernel/system.c +++ b/src/kernel/system.c @@ -7,8 +7,6 @@ #include <kernel/lib/stdio.h> #include <stdarg.h> -uint32_t initial_esp; - char *vga_buffer = (char *)0x500; void vga_clear() @@ -28,7 +26,7 @@ void vga_log(char *msg) uint16_t *terminal_buffer = (uint16_t *)0xB8000; for (size_t i = 0; i < strlen(msg); i++) terminal_buffer[line * 80 + i] = (uint16_t)msg[i] | (uint16_t)0x700; - log("%s", msg); + info("%s", msg); char string[80]; strcpy(string, "["); strcat(string, itoa((int)get_time())); diff --git a/src/kernel/system.h b/src/kernel/system.h index e5d4f0b..eff7925 100644 --- a/src/kernel/system.h +++ b/src/kernel/system.h @@ -11,16 +11,6 @@ extern void ASM_KERNEL_END(); /** - * The initial stack pointer - */ -uint32_t initial_esp; - -/** - * Initialize the basic features of the OS - */ -void init(); - -/** * The ASM registers as packed structure */ typedef struct __attribute__((packed)) { diff --git a/src/kernel/tasks/process.c b/src/kernel/tasks/process.c index 123d2d7..4cb8d8c 100644 --- a/src/kernel/tasks/process.c +++ b/src/kernel/tasks/process.c @@ -28,7 +28,7 @@ void scheduler(struct regs *regs) current_proc = root; } - debug("Task switch to %s", current_proc->name); + //debug("Task switch to %s", current_proc->name); while (current_proc->state == PROC_ASLEEP) { current_proc = current_proc->next; diff --git a/src/kernel/tasks/userspace.c b/src/kernel/tasks/userspace.c index 8b88b87..696a9e9 100644 --- a/src/kernel/tasks/userspace.c +++ b/src/kernel/tasks/userspace.c @@ -29,7 +29,7 @@ void userspace_enter(struct process *proc) current_proc = proc; - sti(); + sti(); // TODO: Prevent race conditions in userspace jumping debug("Jumping to userspace!"); jump_userspace(); } |