add: kpanic

This commit is contained in:
starnakin 2024-09-22 09:54:56 +02:00
parent 283f073124
commit 2728346711
11 changed files with 58 additions and 50 deletions

View File

@ -6,3 +6,5 @@ struct stackframe {
struct stackframe *ebp;
uint32_t eip;
};
void print_stack(void);

3
headers/kpanic.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void kpanic(const char *format, ...);

4
headers/power.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
void reboot(void);
void halt(void);

View File

@ -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);

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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();
}

23
src/kpanic.c Normal file
View File

@ -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();
}

4
src/power/halt.c Normal file
View File

@ -0,0 +1,4 @@
void halt(void)
{
asm volatile("hlt");
}

View File

@ -29,9 +29,4 @@ void reboot(void)
loop:
asm volatile("hlt");
goto loop;
}
void halt(void)
{
asm volatile("hlt");
}
}

View File

@ -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]);
}