Browse Source

feat(assert): check error with assert

greatbridf 2 years ago
parent
commit
cebc24a3ae
6 changed files with 45 additions and 38 deletions
  1. 1 0
      CMakeLists.txt
  2. 0 14
      include/kernel/syscall.hpp
  3. 19 0
      include/types/assert.h
  4. 8 10
      src/fs/fat.cpp
  5. 4 5
      src/kernel/mem.cpp
  6. 13 9
      src/kernel/vfs.cpp

+ 1 - 0
CMakeLists.txt

@@ -89,6 +89,7 @@ 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.h
                         include/types/elf.hpp

+ 0 - 14
include/kernel/syscall.hpp

@@ -6,18 +6,4 @@
 // return value is stored in %eax and %edx
 typedef void (*syscall_handler)(interrupt_stack* data);
 
-inline uint32_t syscall(uint32_t num, uint32_t arg1 = 0, uint32_t arg2 = 0)
-{
-    asm volatile(
-        "movl %1, %%edi\n"
-        "movl %2, %%esi\n"
-        "movl %3, %%eax\n"
-        "int $0x80\n"
-        "movl %%eax, %0"
-        : "=g"(num)
-        : "g"(arg1), "g"(arg2), "g"(num)
-        : "eax", "edx", "edi", "esi");
-    return num;
-}
-
 void init_syscall(void);

+ 19 - 0
include/types/assert.h

@@ -0,0 +1,19 @@
+#pragma once
+
+#include "types.h"
+
+#ifndef NDEBUG
+
+#define assert(_statement) \
+    if (!(_statement))     \
+    asm volatile("ud2")
+
+#define assert_likely(_statement) \
+    if (unlikely(!(_statement)))  \
+    asm volatile("ud2")
+
+#else
+
+#define assert(_statement) ;
+
+#endif

+ 8 - 10
src/fs/fat.cpp

@@ -1,9 +1,9 @@
 #include <fs/fat.hpp>
 #include <kernel/mem.h>
 #include <kernel/mm.hpp>
-#include <kernel/syscall.hpp>
 #include <kernel/vfs.hpp>
 #include <types/allocator.hpp>
+#include <types/assert.h>
 #include <types/hash_map.hpp>
 #include <types/status.h>
 #include <types/stdint.h>
@@ -12,15 +12,13 @@ namespace fs::fat {
 // buf MUST be larger than 512 bytes
 inline void fat32::read_sector(void* buf, uint32_t sector_no)
 {
-    if (vfs_read(
-            device,
-            (char*)buf,
-            SECTOR_SIZE,
-            sector_no * SECTOR_SIZE,
-            SECTOR_SIZE)
-        != SECTOR_SIZE) {
-        syscall(0x03);
-    }
+    assert(vfs_read(
+               device,
+               (char*)buf,
+               SECTOR_SIZE,
+               sector_no * SECTOR_SIZE,
+               SECTOR_SIZE)
+        == SECTOR_SIZE);
 }
 
 // buf MUST be larger than 4096 bytes

+ 4 - 5
src/kernel/mem.cpp

@@ -10,6 +10,7 @@
 #include <kernel/vga.h>
 #include <kernel_main.h>
 #include <types/allocator.hpp>
+#include <types/assert.h>
 #include <types/bitmap.h>
 #include <types/size.h>
 #include <types/status.h>
@@ -197,8 +198,7 @@ static brk_memory_allocator
 void* k_malloc(size_t size)
 {
     void* ptr = kernel_heap_allocator->alloc(size);
-    if unlikely (!ptr)
-        MAKE_BREAK_POINT();
+    assert_likely(ptr);
     return ptr;
 }
 
@@ -210,8 +210,7 @@ void k_free(void* ptr)
 void* ki_malloc(size_t size)
 {
     void* ptr = kernel_ident_mapped_allocator.alloc(size);
-    if (unlikely(!ptr))
-        MAKE_BREAK_POINT();
+    assert_likely(ptr);
     return ptr;
 }
 
@@ -295,7 +294,7 @@ page_t alloc_n_raw_pages(size_t n)
             return first;
         }
     }
-    MAKE_BREAK_POINT();
+    assert(false);
     return 0xffffffff;
 }
 

+ 13 - 9
src/kernel/vfs.cpp

@@ -1,10 +1,10 @@
 #include <kernel/errno.h>
 #include <kernel/mem.h>
 #include <kernel/stdio.h>
-#include <kernel/syscall.hpp>
 #include <kernel/tty.h>
 #include <kernel/vfs.hpp>
 #include <types/allocator.hpp>
+#include <types/assert.h>
 #include <types/list.hpp>
 #include <types/status.h>
 #include <types/stdint.h>
@@ -116,7 +116,7 @@ void fs::vfs::register_root_node(inode* root)
 }
 int fs::vfs::load_dentry(dentry*)
 {
-    syscall(0x03);
+    assert(false);
     return GB_FAILED;
 }
 int fs::vfs::mount(dentry* mnt, vfs* new_fs)
@@ -137,37 +137,37 @@ int fs::vfs::mount(dentry* mnt, vfs* new_fs)
 }
 size_t fs::vfs::inode_read(inode*, char*, size_t, size_t, size_t)
 {
-    syscall(0x03);
+    assert(false);
     return 0xffffffff;
 }
 size_t fs::vfs::inode_write(inode*, const char*, size_t, size_t)
 {
-    syscall(0x03);
+    assert(false);
     return 0xffffffff;
 }
 int fs::vfs::inode_mkfile(dentry*, const char*)
 {
-    syscall(0x03);
+    assert(false);
     return GB_FAILED;
 }
 int fs::vfs::inode_mknode(dentry*, const char*, node_t)
 {
-    syscall(0x03);
+    assert(false);
     return GB_FAILED;
 }
 int fs::vfs::inode_rmfile(dentry*, const char*)
 {
-    syscall(0x03);
+    assert(false);
     return GB_FAILED;
 }
 int fs::vfs::inode_mkdir(dentry*, const char*)
 {
-    syscall(0x03);
+    assert(false);
     return GB_FAILED;
 }
 int fs::vfs::inode_stat(dentry*, stat*)
 {
-    syscall(0x03);
+    assert(false);
     return GB_FAILED;
 }
 
@@ -384,6 +384,10 @@ fs::vfs::dentry* fs::vfs_open(const char* path)
             }
             if (path[n] == '/') {
                 cur = cur->find(string(path, n));
+
+                if (!cur)
+                    return cur;
+
                 if (path[n + 1] == 0x00) {
                     return cur;
                 } else {