From 2728346711245e07cbc0f8ec963a41cf108de36f Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 22 Sep 2024 09:54:56 +0200 Subject: [PATCH] add: kpanic --- headers/debug.h | 2 ++ headers/kpanic.h | 3 ++ headers/power.h | 4 +++ headers/shell.h | 6 ++-- headers/terminal.h | 3 +- src/debug/print_stack.c | 2 +- src/kernel.c | 5 --- src/kpanic.c | 23 +++++++++++++ src/power/halt.c | 4 +++ src/{shell/power.c => power/reboot.c} | 7 +--- src/terminal/put.c | 49 ++++++++------------------- 11 files changed, 58 insertions(+), 50 deletions(-) create mode 100644 headers/kpanic.h create mode 100644 headers/power.h create mode 100644 src/kpanic.c create mode 100644 src/power/halt.c rename src/{shell/power.c => power/reboot.c} (94%) diff --git a/headers/debug.h b/headers/debug.h index cdc4453..f0fc17b 100644 --- a/headers/debug.h +++ b/headers/debug.h @@ -6,3 +6,5 @@ struct stackframe { struct stackframe *ebp; uint32_t eip; }; + +void print_stack(void); \ No newline at end of file diff --git a/headers/kpanic.h b/headers/kpanic.h new file mode 100644 index 0000000..3318caf --- /dev/null +++ b/headers/kpanic.h @@ -0,0 +1,3 @@ +#pragma once + +void kpanic(const char *format, ...); \ No newline at end of file diff --git a/headers/power.h b/headers/power.h new file mode 100644 index 0000000..a40a207 --- /dev/null +++ b/headers/power.h @@ -0,0 +1,4 @@ +#pragma once + +void reboot(void); +void halt(void); \ No newline at end of file diff --git a/headers/shell.h b/headers/shell.h index b122e62..3b7318d 100644 --- a/headers/shell.h +++ b/headers/shell.h @@ -1,5 +1,8 @@ #pragma once +#include "debug.h" +#include "power.h" + #define PROMPT "> " [[__maybe_unused__]] static const char *POOP = @@ -44,8 +47,5 @@ "\n"; void shell_init(void); -void reboot(void); -void halt(void); -void print_stack(void); void date(void); void merdella(void); diff --git a/headers/terminal.h b/headers/terminal.h index 327ebe7..ab9c3f8 100644 --- a/headers/terminal.h +++ b/headers/terminal.h @@ -41,7 +41,8 @@ typedef enum { enum cursor_direction { LEFT, RIGHT, UP, DOWN }; void terminal_initialize(void); -void terminal_setcolor(uint8_t color); +void terminal_set_bg_color(uint8_t color); +void terminal_set_fg_color(uint8_t color); int terminal_putchar(char c); int terminal_write(const char *data, size_t size); int terminal_writestring(const char *data); diff --git a/src/debug/print_stack.c b/src/debug/print_stack.c index df0f556..e3ed9d2 100644 --- a/src/debug/print_stack.c +++ b/src/debug/print_stack.c @@ -7,7 +7,7 @@ void print_stack(void) (struct stackframe *)__builtin_frame_address(0); while (stack) { - kprintf(KERN_DEBUG "fn: %d\n", stack->eip); + kprintf(KERN_DEBUG "fn: %p\n", stack->eip); stack = stack->ebp; } } diff --git a/src/kernel.c b/src/kernel.c index a26eed5..18bb4dc 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -32,10 +32,5 @@ void kernel_main(void) kprintf(KERN_NOTICE "KERN_NOTICE\n"); kprintf(KERN_INFO "KERN_INFO\n"); kprintf(KERN_DEBUG "KERN_DEBUG\n"); - char *str = kalloc(10); - kfree(str); - str = kalloc(10); - strcpy(str, "Hello world\n"); - kprintf("%s", str); shell_init(); } diff --git a/src/kpanic.c b/src/kpanic.c new file mode 100644 index 0000000..974de58 --- /dev/null +++ b/src/kpanic.c @@ -0,0 +1,23 @@ +#include "debug.h" +#include "keyboard.h" +#include "kprintf.h" +#include "power.h" +#include "terminal.h" + +void kpanic(const char *format, ...) +{ + va_list va; + + terminal_set_bg_color(VGA_COLOR_BLUE); + terminal_clear(); + va_start(va, format); + kvprintf(format, va); + va_end(va); + kprintf("\n\n"); + print_stack(); + kprintf("\n\n"); + kprintf("PRESS SPACE TO REBOOT"); + while (terminal_getkey().scan_code != KEY_SPACE) + ; + reboot(); +} \ No newline at end of file diff --git a/src/power/halt.c b/src/power/halt.c new file mode 100644 index 0000000..2a3e656 --- /dev/null +++ b/src/power/halt.c @@ -0,0 +1,4 @@ +void halt(void) +{ + asm volatile("hlt"); +} diff --git a/src/shell/power.c b/src/power/reboot.c similarity index 94% rename from src/shell/power.c rename to src/power/reboot.c index 9f1afce..9aa5da7 100644 --- a/src/shell/power.c +++ b/src/power/reboot.c @@ -29,9 +29,4 @@ void reboot(void) loop: asm volatile("hlt"); goto loop; -} - -void halt(void) -{ - asm volatile("hlt"); -} +} \ No newline at end of file diff --git a/src/terminal/put.c b/src/terminal/put.c index 1b0d506..ef70fd0 100644 --- a/src/terminal/put.c +++ b/src/terminal/put.c @@ -49,9 +49,14 @@ void terminal_set_screen(int pos) kprintf(PROMPT); } -void terminal_setcolor(uint8_t color) +void terminal_set_bg_color(uint8_t bg_color) { - screen->color = color; + screen->color = bg_color << 4 | (screen->color & 0x0F); +} + +void terminal_set_fg_color(uint8_t fg_color) +{ + screen->color = (screen->color & 0xF0) | fg_color; } void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) @@ -81,7 +86,7 @@ void terminal_clear(void) for (size_t y = 0; y < VGA_HEIGHT; y++) { for (size_t x = 0; x < VGA_WIDTH; x++) { const size_t index = y * VGA_WIDTH + x; - TERM_BUF[index] = vga_entry(' ', VGA_COLOR_WHITE); + TERM_BUF[index] = vga_entry(' ', screen->color); } } memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer)); @@ -161,35 +166,11 @@ void move_cursor(int direction) void set_color_level(int level) { - switch (level) { - case 0: - screen->color = VGA_COLOR_WHITE; - break; - case 1: - screen->color = VGA_COLOR_RED; - break; - case 2: - screen->color = VGA_COLOR_RED; - break; - case 3: - screen->color = VGA_COLOR_MAGENTA; - break; - case 4: - screen->color = VGA_COLOR_LIGHT_YELLOW; - break; - case 5: - screen->color = VGA_COLOR_LIGHT_YELLOW; - break; - case 6: - screen->color = VGA_COLOR_LIGHT_BLUE; - break; - case 7: - screen->color = VGA_COLOR_GREEN; - break; - case 8: - screen->color = VGA_COLOR_LIGHT_GREY; - break; - default: - break; - } + int color_translation[] = { + VGA_COLOR_WHITE, VGA_COLOR_RED, + VGA_COLOR_RED, VGA_COLOR_MAGENTA, + VGA_COLOR_LIGHT_YELLOW, VGA_COLOR_LIGHT_YELLOW, + VGA_COLOR_LIGHT_BLUE, VGA_COLOR_GREEN, + VGA_COLOR_LIGHT_GREY}; + terminal_set_fg_color(color_translation[level]); }