Эх сурвалжийг харах

refactor(assert): move assert and breakpoint code

greatbridf 2 жил өмнө
parent
commit
1285e661c3

+ 0 - 10
include/kernel_main.h

@@ -1,16 +1,6 @@
 #pragma once
 #include <types/types.h>
 
-#define __crash() asm volatile("ud2")
-
-#ifdef __BOCHS_SYSTEM__
-#define __break_point() asm volatile("xchgw %bx, %bx")
-#else
-#define __break_point() __crash()
-#endif
-
-#define MAKE_BREAK_POINT() __break_point()
-
 #define KERNEL_STACK_SIZE (16 * 1024)
 #define KERNEL_STACK_SEGMENT (0x10)
 

+ 26 - 5
include/types/assert.h

@@ -2,15 +2,36 @@
 
 #include "types.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void crash(void);
+void _debugger_breakpoint(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifndef NDEBUG
+#define breakpoint() _debugger_breakpoint()
+#else
+#define breakpoint() _crash()
+#endif
+
 #ifndef NDEBUG
 
 #define assert(_statement) \
-    if (!(_statement))     \
-    asm volatile("ud2")
+    if (!(_statement)) {   \
+        breakpoint();      \
+        crash();           \
+    }
 
-#define assert_likely(_statement) \
-    if (unlikely(!(_statement)))  \
-    asm volatile("ud2")
+#define assert_likely(_statement)  \
+    if (unlikely(!(_statement))) { \
+        breakpoint();              \
+        crash();                   \
+    }
 
 #else
 

+ 3 - 2
src/kernel/hw/serial.c

@@ -1,3 +1,4 @@
+#include <types/status.h>
 #include <asm/port_io.h>
 #include <kernel/hw/serial.h>
 #include <kernel/stdio.h>
@@ -21,7 +22,7 @@ int32_t init_serial_port(port_id_t port)
 
     // Check if serial is faulty (i.e: not same byte as sent)
     if (asm_inb(port + 0) != 0xAE) {
-        return 1;
+        return GB_FAILED;
     }
 
     // If serial is not faulty set it in normal operation mode
@@ -29,7 +30,7 @@ int32_t init_serial_port(port_id_t port)
     asm_outb(port + 4, 0x0F);
 
     asm_outb(port + 1, 0x01); // Enable interrupts #0: Received Data Available
-    return 0;
+    return GB_OK;
 }
 
 int32_t is_serial_has_data(port_id_t port)

+ 2 - 3
src/kernel/interrupt.cpp

@@ -14,6 +14,7 @@
 #include <kernel/vfs.hpp>
 #include <kernel/vga.h>
 #include <kernel_main.h>
+#include <types/assert.h>
 #include <types/size.h>
 #include <types/stdint.h>
 #include <types/types.h>
@@ -157,9 +158,7 @@ static inline void _int14_panic(void* eip, void* cr2, struct page_fault_error_co
         buf, 256,
         "\nkilled: segmentation fault (eip: %x, cr2: %x, error_code: %x)\n", eip, cr2, error_code);
     tty_print(console, buf);
-    MAKE_BREAK_POINT();
-    asm_cli();
-    asm_hlt();
+    assert(false);
 }
 
 // page fault

+ 4 - 8
src/kernel/mem.cpp

@@ -221,14 +221,10 @@ void ki_free(void* ptr)
 
 void* ptovp(pptr_t p_ptr)
 {
-    if (p_ptr <= 0x30000000) {
-        // memory below 768MiB is identically mapped
-        return (void*)p_ptr;
-    } else {
-        // TODO: address translation
-        MAKE_BREAK_POINT();
-        return (void*)0xffffffff;
-    }
+    // memory below 768MiB is identically mapped
+    // TODO: address translation for high mem
+    assert(p_ptr <= 0x30000000);
+    return (void*)p_ptr;
 }
 
 inline void mark_page(page_t n)

+ 25 - 35
src/kernel_main.c

@@ -1,6 +1,5 @@
 #include "kernel_main.h"
 
-#include <types/types.h>
 #include <asm/boot.h>
 #include <asm/port_io.h>
 #include <asm/sys.h>
@@ -14,7 +13,10 @@
 #include <kernel/task.h>
 #include <kernel/tty.h>
 #include <kernel/vga.h>
+#include <types/assert.h>
 #include <types/bitmap.h>
+#include <types/status.h>
+#include <types/types.h>
 
 #define KERNEL_MAIN_BUF_SIZE (128)
 
@@ -28,26 +30,6 @@ struct tty* console = NULL;
 #define INIT_OK() printkf("ok\n")
 #define INIT_FAILED() printkf("failed\n")
 
-static inline void check_a20_status(void)
-{
-    uint32_t result;
-    result = check_a20_on();
-
-    if (result) {
-        tty_print(console, "a20 is on");
-    } else {
-        tty_print(console, "a20 is NOT on");
-    }
-}
-
-static inline void halt_on_init_error(void)
-{
-    MAKE_BREAK_POINT();
-    asm_cli();
-    while (1)
-        asm_hlt();
-}
-
 typedef void (*constructor)(void);
 extern constructor start_ctors;
 extern constructor end_ctors;
@@ -148,7 +130,8 @@ extern void NORETURN init_scheduler();
 
 void NORETURN kernel_main(void)
 {
-    // MAKE_BREAK_POINT();
+    assert(check_a20_on());
+
     asm_enable_sse();
 
     init_bss_section();
@@ -157,13 +140,10 @@ void NORETURN kernel_main(void)
 
     load_new_gdt();
 
-    char buf[KERNEL_MAIN_BUF_SIZE];
-
-    init_serial_port(PORT_SERIAL0);
+    char buf[KERNEL_MAIN_BUF_SIZE] = { 0 };
 
-    if (make_serial_tty(&early_console, PORT_SERIAL0) != GB_OK) {
-        halt_on_init_error();
-    }
+    assert(init_serial_port(PORT_SERIAL0) == GB_OK);
+    assert(make_serial_tty(&early_console, PORT_SERIAL0) == GB_OK);
     console = &early_console;
 
     show_mem_info(buf);
@@ -194,8 +174,6 @@ void NORETURN kernel_main(void)
     tty_print(console, k_malloc_buf);
     k_free(k_malloc_buf);
 
-    k_malloc_buf[4096] = '\x89';
-
     init_vfs();
 
     printkf("switching execution to the scheduler...\n");
@@ -204,9 +182,21 @@ void NORETURN kernel_main(void)
 
 void NORETURN __stack_chk_fail(void)
 {
-    tty_print(console, "***** stack smashing detected! *****\nhalting\n");
-    for (;;) {
-        asm_cli();
-        asm_hlt();
-    }
+    tty_print(console, "***** stack smashing detected! *****\n");
+    for (;;)
+        assert(0);
+}
+
+void crash(void)
+{
+    asm volatile("ud2");
+}
+
+void _debugger_breakpoint(void)
+{
+#ifdef __BOCHS_SYSTEM__
+    asm volatile("xchgw %%bx, %%bx");
+#else
+    asm volatile("nop");
+#endif
 }

+ 5 - 1
src/mbr.S

@@ -16,7 +16,11 @@ mbr_start:
 # read the first 64k
     call read_data
 
-# read the rest
+# read the following 128k
+    addw $(0x100 * 16), read_data_segment
+    addl $(8 * 16), read_data_lba
+    call read_data
+
     addw $(0x100 * 16), read_data_segment
     addl $(8 * 16), read_data_lba
     call read_data