Kaynağa Gözat

change: remove assert.h in kernel

greatbridf 2 yıl önce
ebeveyn
işleme
f8dda1c0bd

+ 0 - 1
CMakeLists.txt

@@ -75,7 +75,6 @@ set(KERNEL_MAIN_SOURCES src/fs/fat.cpp
                         include/kernel/hw/timer.h
                         include/kernel/input/keycodes.h
                         include/kernel/input/input_event.h
-                        include/types/assert.h
                         include/types/bitmap.h
                         include/types/buffer.hpp
                         include/types/elf.hpp

+ 0 - 41
include/types/assert.h

@@ -1,41 +0,0 @@
-#pragma once
-
-#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)) {   \
-        breakpoint();      \
-        crash();           \
-    }
-
-#define assert_likely(_statement)  \
-    if (unlikely(!(_statement))) { \
-        breakpoint();              \
-        crash();                   \
-    }
-
-#else
-
-#define assert(_statement) (void)(_statement)
-#define assert_likely(_statement) (void)(_statement)
-
-#endif

+ 8 - 8
src/fs/fat.cpp

@@ -1,3 +1,4 @@
+#include <assert.h>
 #include <fs/fat.hpp>
 #include <kernel/mem.h>
 #include <kernel/mm.hpp>
@@ -5,7 +6,6 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/assert.h>
 #include <types/hash_map.hpp>
 #include <types/status.h>
 
@@ -13,13 +13,13 @@ namespace fs::fat {
 // buf MUST be larger than 512 bytes
 inline void fat32::_raw_read_sector(void* buf, uint32_t sector_no)
 {
-    assert(vfs_read(
-               device,
-               (char*)buf,
-               SECTOR_SIZE,
-               sector_no * SECTOR_SIZE,
-               SECTOR_SIZE)
-        == SECTOR_SIZE);
+    size_t n = vfs_read(
+        device,
+        (char*)buf,
+        SECTOR_SIZE,
+        sector_no * SECTOR_SIZE,
+        SECTOR_SIZE);
+    assert(n == SECTOR_SIZE);
 }
 
 // buf MUST be larger than 4096 bytes

+ 1 - 1
src/kernel/event/event.cpp

@@ -1,4 +1,5 @@
 #include <asm/port_io.h>
+#include <assert.h>
 #include <kernel/event/event.h>
 #include <kernel/event/evtqueue.hpp>
 #include <kernel/input/input_event.h>
@@ -6,7 +7,6 @@
 #include <kernel/process.hpp>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/assert.h>
 #include <types/cplusplus.hpp>
 #include <types/list.hpp>
 #include <types/lock.hpp>

+ 2 - 2
src/kernel/hw/ata.cpp

@@ -1,4 +1,5 @@
 #include <asm/port_io.h>
+#include <assert.h>
 #include <fs/fat.hpp>
 #include <kernel/hw/ata.hpp>
 #include <kernel/syscall.hpp>
@@ -6,7 +7,6 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/assert.h>
 #include <types/status.h>
 
 hw::ata::ata(port_id_t p)
@@ -136,7 +136,7 @@ constexpr hw::ata** p_ata_sec = &ata_sec;
 template <hw::ata** ata_bus>
 size_t _ata_read(fs::special_node* sn, char* buf, size_t buf_size, size_t offset, size_t n)
 {
-    assert_likely(buf_size >= n);
+    assert(buf_size >= n);
 
     char b[512] {};
     char* orig_buf = buf;

+ 1 - 1
src/kernel/interrupt.cpp

@@ -1,6 +1,7 @@
 #define _INTERRUPT_C_
 
 #include <asm/port_io.h>
+#include <assert.h>
 #include <kernel/hw/keyboard.h>
 #include <kernel/hw/serial.h>
 #include <kernel/hw/timer.h>
@@ -15,7 +16,6 @@
 #include <kernel_main.hpp>
 #include <stdint.h>
 #include <stdio.h>
-#include <types/assert.h>
 #include <types/size.h>
 #include <types/types.h>
 

+ 3 - 3
src/kernel/mem.cpp

@@ -1,6 +1,7 @@
 #include <asm/boot.h>
 #include <asm/port_io.h>
 #include <asm/sys.h>
+#include <assert.h>
 #include <kernel/errno.h>
 #include <kernel/mem.h>
 #include <kernel/mm.hpp>
@@ -10,7 +11,6 @@
 #include <kernel_main.hpp>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/assert.h>
 #include <types/bitmap.h>
 #include <types/size.h>
 #include <types/status.h>
@@ -198,7 +198,7 @@ static brk_memory_allocator
 void* k_malloc(size_t size)
 {
     void* ptr = kernel_heap_allocator->alloc(size);
-    assert_likely(ptr);
+    assert(likely(ptr));
     return ptr;
 }
 
@@ -210,7 +210,7 @@ void k_free(void* ptr)
 void* ki_malloc(size_t size)
 {
     void* ptr = kernel_ident_mapped_allocator.alloc(size);
-    assert_likely(ptr);
+    assert(likely(ptr));
     return ptr;
 }
 

+ 5 - 4
src/kernel/process.cpp

@@ -1,5 +1,6 @@
 #include <asm/port_io.h>
 #include <asm/sys.h>
+#include <assert.h>
 #include <fs/fat.hpp>
 #include <kernel/hw/ata.hpp>
 #include <kernel/interrupt.h>
@@ -12,7 +13,6 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/assert.h>
 #include <types/cplusplus.hpp>
 #include <types/elf.hpp>
 #include <types/hash_map.hpp>
@@ -103,7 +103,7 @@ void proclist::kill(pid_t pid, int exit_code)
     // init should never exit
     if (proc->ppid == 0) {
         console->print("kernel panic: init exited!\n");
-        crash();
+        assert(false);
     }
 
     // make child processes orphans (children of init)
@@ -197,7 +197,7 @@ void NORETURN _kernel_init(void)
     // TODO: parse kernel parameters
     auto* _new_fs = fs::register_fs(types::_new<types::kernel_allocator, fs::fat::fat32>(fs::vfs_open("/dev/hda1")->ind));
     int ret = fs::fs_root->ind->fs->mount(fs::vfs_open("/mnt"), _new_fs);
-    assert_likely(ret == GB_OK);
+    assert(ret == GB_OK);
 
     current_process->attr.system = 0;
     current_thread->attr.system = 0;
@@ -211,7 +211,8 @@ void NORETURN _kernel_init(void)
     d.envp = envp;
     d.system = false;
 
-    assert(types::elf::elf32_load(&d) == GB_OK);
+    ret = types::elf::elf32_load(&d);
+    assert(ret == GB_OK);
 
     asm volatile(
         "movw $0x23, %%ax\n"

+ 1 - 1
src/kernel/syscall.cpp

@@ -1,5 +1,6 @@
 #include <asm/port_io.h>
 #include <asm/sys.h>
+#include <assert.h>
 #include <kernel/errno.h>
 #include <kernel/interrupt.h>
 #include <kernel/log.hpp>
@@ -13,7 +14,6 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/assert.h>
 #include <types/elf.hpp>
 #include <types/status.h>
 

+ 1 - 1
src/kernel/vfs.cpp

@@ -1,3 +1,4 @@
+#include <assert.h>
 #include <kernel/errno.h>
 #include <kernel/mem.h>
 #include <kernel/tty.hpp>
@@ -5,7 +6,6 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/assert.h>
 #include <types/list.hpp>
 #include <types/map.hpp>
 #include <types/pair.hpp>

+ 8 - 32
src/kernel_main.cpp

@@ -3,6 +3,7 @@
 #include <asm/boot.h>
 #include <asm/port_io.h>
 #include <asm/sys.h>
+#include <assert.h>
 #include <kernel/event/event.h>
 #include <kernel/hw/keyboard.h>
 #include <kernel/hw/serial.h>
@@ -16,7 +17,6 @@
 #include <kernel/vga.hpp>
 #include <stdint.h>
 #include <stdio.h>
-#include <types/assert.h>
 #include <types/bitmap.h>
 #include <types/status.h>
 #include <types/types.h>
@@ -138,7 +138,6 @@ int init_console(const char* name)
             return GB_OK;
         }
     }
-    crash();
     return GB_FAILED;
 }
 
@@ -147,7 +146,9 @@ extern "C" uint32_t check_a20_on(void);
 
 extern "C" void NORETURN kernel_main(void)
 {
-    assert(check_a20_on());
+    int ret;
+    ret = check_a20_on();
+    assert(ret == 1);
 
     asm_enable_sse();
 
@@ -164,14 +165,16 @@ extern "C" void NORETURN kernel_main(void)
 
     char buf[KERNEL_MAIN_BUF_SIZE] = { 0 };
 
-    assert(init_serial_port(PORT_SERIAL0) == GB_OK);
+    ret = init_serial_port(PORT_SERIAL0);
+    assert(ret == GB_OK);
 
     init_idt();
     init_mem();
     init_pic();
     init_pit();
 
-    assert(init_console("ttyS0") == GB_OK);
+    ret = init_console("ttyS0");
+    assert(ret == GB_OK);
 
     show_mem_info(buf);
 
@@ -180,30 +183,3 @@ extern "C" void NORETURN kernel_main(void)
     kmsg("switching execution to the scheduler...\n");
     init_scheduler();
 }
-
-void NORETURN __stack_chk_fail(void)
-{
-    kmsg("***** stack smashing detected! *****\n");
-    for (;;)
-        assert(0);
-}
-
-extern "C" void NORETURN __cxa_pure_virtual(void)
-{
-    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
-}

+ 2 - 2
src/types/elf.cpp

@@ -1,9 +1,9 @@
+#include <assert.h>
 #include <kernel/errno.h>
 #include <kernel/mem.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
-#include <types/assert.h>
 #include <types/elf.hpp>
 #include <types/string.hpp>
 #include <types/vector.hpp>
@@ -94,7 +94,7 @@ int types::elf::elf32_load(types::elf::elf32_load_data* d)
     // map stack area
     auto ret = mmap((void*)types::elf::ELF_STACK_TOP, types::elf::ELF_STACK_SIZE,
         fs::vfs_open("/dev/null")->ind, 0, 1, 0);
-    assert_likely(ret == GB_OK);
+    assert(ret == GB_OK);
 
     d->eip = (void*)hdr.entry;
     d->sp = reinterpret_cast<uint32_t*>(types::elf::ELF_STACK_BOTTOM);

+ 30 - 3
src/types/libstdcpp.cpp

@@ -1,10 +1,37 @@
-#include <types/assert.h>
+#include <asm/port_io.h>
+#include <assert.h>
+#include <kernel/log.hpp>
+#include <stdio.h>
+#include <types/types.h>
 
 void operator delete(void*)
 {
-    crash();
+    assert(false);
 }
 void operator delete(void*, unsigned int)
 {
-    crash();
+    assert(false);
+}
+
+extern "C" void NORETURN __stack_chk_fail(void)
+{
+    assert(false);
+}
+
+extern "C" void NORETURN __cxa_pure_virtual(void)
+{
+    assert(false);
+}
+
+void NORETURN
+__assert_fail(const char* statement, const char* file, int line, const char* func)
+{
+    char buf[256];
+    snprintf(buf, sizeof(buf), "Kernel assertion failed: (%s), %s:%d, %s\n",
+        statement, file, line, func);
+    kmsg(buf);
+    asm_cli();
+    asm_hlt();
+    for (;;)
+        ;
 }