From a02931a16546bfb0a46063bb2c675a72ed7a5e08 Mon Sep 17 00:00:00 2001 From: 0x35c Date: Tue, 17 Sep 2024 11:10:41 +0200 Subject: [PATCH] feature: print_stack working (dont use opti flags, else the stack is broken) --- Makefile | 4 ++-- headers/debug.h | 8 ++++++++ headers/gdt.h | 34 +++++++++++++++++----------------- headers/shell.h | 2 ++ src/debug/print_stack.c | 13 +++++++++++++ src/gdt/gdt.c | 6 +++--- src/kernel.c | 2 +- src/shell/exec.c | 5 +++++ src/shell/power.c | 4 ++-- src/shell/poweroff.c | 0 10 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 headers/debug.h create mode 100644 src/debug/print_stack.c delete mode 100644 src/shell/poweroff.c diff --git a/Makefile b/Makefile index bd6dca5..af54644 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,9 @@ NAME := bozOS AS := i386-elf-as ASFLAGS := CC := i386-elf-gcc -CFLAGS := -std=gnu99 -ffreestanding -O2 -Wall -Wextra -iquotelibbozo/headers -iquoteheaders -MMD -g +CFLAGS := -std=gnu99 -ffreestanding -Wall -Wextra -iquotelibbozo/headers -iquoteheaders -MMD -fno-omit-frame-pointer -g LD := $(CC) -LDFLAGS := -T boot/linker.ld -ffreestanding -O2 -nostdlib +LDFLAGS := -T boot/linker.ld -ffreestanding -nostdlib LIBS := -L libbozo/build/ -lbozo -lgcc SSRC := $(shell find src -name '*.s') diff --git a/headers/debug.h b/headers/debug.h new file mode 100644 index 0000000..cdc4453 --- /dev/null +++ b/headers/debug.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +struct stackframe { + struct stackframe *ebp; + uint32_t eip; +}; diff --git a/headers/gdt.h b/headers/gdt.h index e4fdc68..5659557 100644 --- a/headers/gdt.h +++ b/headers/gdt.h @@ -3,30 +3,30 @@ #include #define GDT_ADDRESS 0x00000800 -#define GDT_SIZE 7 +#define GDT_SIZE 7 // https://wiki.osdev.org/Global_Descriptor_Table#GDTR struct gdt_descriptor { - uint16_t size; - uint32_t base; + uint16_t size; + uint32_t base; } __attribute__((packed)); -void initGdt(); +void init_gdt(); #define GDT_FLAG_64BIT_MODE 0b0010 #define GDT_FLAG_32BIT_MODE 0b0100 -#define GDT_FLAG_PAGE_MODE 0b1000 -#define GDT_FLAG_BYTE_MODE 0b1000 +#define GDT_FLAG_PAGE_MODE 0b1000 +#define GDT_FLAG_BYTE_MODE 0b1000 -#define GDT_ACCESS_P_VALID 0b10000000 -#define GDT_ACCESS_DPL_KERNEL_MODE 0b00000000 -#define GDT_ACCESS_DPL_USER_MODE 0b01100000 -#define GDT_ACCESS_S_CODE_OR_DATA 0b00010000 -#define GDT_ACCESS_S_SYSTEM_SEGMENT 0b00010000 -#define GDT_ACCESS_E_EXECUTABLE 0b00001000 -#define GDT_ACCESS_E_NOT_EXECUTABLE 0b00000000 -#define GDT_ACCESS_DC_CONFORM 0b00000100 // TODO UNDERSTAND THIS BIT -#define GDT_ACCESS_DC_NOT_CONFORM 0b00000000 +#define GDT_ACCESS_P_VALID 0b10000000 +#define GDT_ACCESS_DPL_KERNEL_MODE 0b00000000 +#define GDT_ACCESS_DPL_USER_MODE 0b01100000 +#define GDT_ACCESS_S_CODE_OR_DATA 0b00010000 +#define GDT_ACCESS_S_SYSTEM_SEGMENT 0b00010000 +#define GDT_ACCESS_E_EXECUTABLE 0b00001000 +#define GDT_ACCESS_E_NOT_EXECUTABLE 0b00000000 +#define GDT_ACCESS_DC_CONFORM 0b00000100 // TODO UNDERSTAND THIS BIT +#define GDT_ACCESS_DC_NOT_CONFORM 0b00000000 #define GDT_ACCESS_RW_READABLE_FOR_CODE_WRITABLE_FOR_DATA 0b00000010 -#define GDT_ACCESS_A_ACCESSED 0b00000001 -#define GDT_ACCESS_A_NOT_ACCESSED 0b00000000 \ No newline at end of file +#define GDT_ACCESS_A_ACCESSED 0b00000001 +#define GDT_ACCESS_A_NOT_ACCESSED 0b00000000 diff --git a/headers/shell.h b/headers/shell.h index c26e965..da0c8c0 100644 --- a/headers/shell.h +++ b/headers/shell.h @@ -48,6 +48,7 @@ typedef enum { REBOOT, POWEROFF, HALT, + STACK, ECHO, COLOR, MERDELLA, @@ -57,3 +58,4 @@ typedef enum { void reboot(void); void halt(void); void shell_init(void); +void print_stack(void); diff --git a/src/debug/print_stack.c b/src/debug/print_stack.c new file mode 100644 index 0000000..3d92e3e --- /dev/null +++ b/src/debug/print_stack.c @@ -0,0 +1,13 @@ +#include "debug.h" +#include "kprintf.h" + +void print_stack(void) +{ + struct stackframe *stack = + (struct stackframe *)__builtin_frame_address(0); + + while (stack) { + kprintf(0, "stack->ebp: %d\n", stack->ebp); + stack = stack->ebp; + } +} diff --git a/src/gdt/gdt.c b/src/gdt/gdt.c index 72d256c..41db62a 100644 --- a/src/gdt/gdt.c +++ b/src/gdt/gdt.c @@ -34,10 +34,10 @@ static void set_gdt_entry_value(uint8_t *target, uint32_t base, uint32_t limit, target[6] |= (granularity << 4); } -void initGdt(void) +void init_gdt(void) { gdtr->size = 8 * GDT_SIZE - 1; - gdtr->base = (uint32_t)&gdt_entries; + gdtr->base = (uint32_t)&gdt_entries[0]; set_gdt_entry_value(gdt_entries + 0x00, 0, 0, 0, 0); // Null segment @@ -58,7 +58,7 @@ void initGdt(void) GDT_ACCESS_A_ACCESSED, GDT_FLAG_32BIT_MODE | GDT_FLAG_PAGE_MODE); // Kernel data set_gdt_entry_value( - gdt_entries + 0x18, 0, 0x0, + gdt_entries + 0x18, 0, 0xFFFFF, GDT_ACCESS_P_VALID | GDT_ACCESS_DPL_KERNEL_MODE | GDT_ACCESS_S_CODE_OR_DATA | GDT_ACCESS_E_NOT_EXECUTABLE | GDT_ACCESS_DC_CONFORM | diff --git a/src/kernel.c b/src/kernel.c index 04cb88f..046d342 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -24,6 +24,6 @@ void kernel_main(void) /* Initialize terminal interface */ terminal_initialize(); - initGdt(); + init_gdt(); shell_init(); } diff --git a/src/shell/exec.c b/src/shell/exec.c index b992d9b..17f0137 100644 --- a/src/shell/exec.c +++ b/src/shell/exec.c @@ -29,6 +29,8 @@ static CMD_TOK find_command(char *line) command = POWEROFF; else if (!strcmp(line, "halt")) command = HALT; + else if (!strcmp(line, "stack")) + command = STACK; else if (!strcmp(line, "echo")) command = ECHO; else if (!strcmp(line, "color")) @@ -95,6 +97,9 @@ void shell_init(void) case HALT: halt(); break; + case STACK: + print_stack(); + break; case ECHO: kprintf(0, "echoing\n"); break; diff --git a/src/shell/power.c b/src/shell/power.c index 427d84e..9f1afce 100644 --- a/src/shell/power.c +++ b/src/shell/power.c @@ -27,8 +27,8 @@ void reboot(void) } while (check_flag(tmp, KBD_BIT_UDATA)); outb(KBD_INTERFACE, KBD_RESET); /* pulse CPU reset line */ loop: - asm volatile("hlt"); /* if that didn't work, halt the CPU */ - goto loop; /* if a NMI is received, halt again */ + asm volatile("hlt"); + goto loop; } void halt(void) diff --git a/src/shell/poweroff.c b/src/shell/poweroff.c deleted file mode 100644 index e69de29..0000000