Parcourir la source

Merge branch 'mem-priv'

greatbridf il y a 2 ans
Parent
commit
ab1d508e44

+ 4 - 0
include/kernel/process.hpp

@@ -422,6 +422,7 @@ public:
 
 void NORETURN init_scheduler(void);
 void schedule(void);
+void NORETURN schedule_noreturn(void);
 
 constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
 {
@@ -438,3 +439,6 @@ constexpr thdlist::~thdlist()
 }
 
 void k_new_thread(void (*func)(void*), void* data);
+
+void NORETURN freeze(void);
+void NORETURN kill_current(int exit_code);

+ 36 - 51
src/kernel/interrupt.cpp

@@ -21,12 +21,20 @@
 
 static struct IDT_entry IDT[256];
 
-static inline void NORETURN _halt_forever(void)
+static inline void NORETURN die(regs_32& regs, ptr_t eip)
 {
-    asm_cli();
-    asm_hlt();
-    for (;;)
-        ;
+    char buf[512] = {};
+    snprintf(
+        buf, sizeof(buf),
+        "***** KERNEL PANIC *****\n"
+        "eax: %x, ebx: %x, ecx: %x, edx: %x\n"
+        "esp: %x, ebp: %x, esi: %x, edi: %x\n"
+        "eip: %x\n",
+        regs.eax, regs.ebx, regs.ecx,
+        regs.edx, regs.esp, regs.ebp,
+        regs.esi, regs.edi, eip);
+    kmsg(buf);
+    freeze();
 }
 
 void init_idt()
@@ -92,30 +100,19 @@ void init_pic(void)
 }
 
 extern "C" void int6_handler(
-    struct regs_32 s_regs,
+    regs_32 s_regs,
     ptr_t eip,
     uint16_t cs,
     uint32_t eflags)
 {
-    char buf[512];
-
-    kmsg("\n---- INVALID OPCODE ----\n");
-
-    snprintf(
-        buf, 512,
-        "eax: %x, ebx: %x, ecx: %x, edx: %x\n"
-        "esp: %x, ebp: %x, esi: %x, edi: %x\n"
-        "eip: %x, cs: %x, eflags: %x       \n",
-        s_regs.eax, s_regs.ebx, s_regs.ecx,
-        s_regs.edx, s_regs.esp, s_regs.ebp,
-        s_regs.esi, s_regs.edi, eip,
-        cs, eflags);
+    char buf[128] = {};
+    snprintf(buf, sizeof(buf),
+        "[kernel] int6 data: cs: %x, eflags: %x\n", cs, eflags);
     kmsg(buf);
-
-    kmsg("----   HALTING SYSTEM   ----\n");
-
-    asm_cli();
-    asm_hlt();
+    if (!current_process->attr.system)
+        kill_current(-1);
+    else
+        die(s_regs, eip);
 }
 
 // general protection
@@ -126,26 +123,15 @@ extern "C" void int13_handler(
     uint16_t cs,
     uint32_t eflags)
 {
-    char buf[512];
-
-    kmsg("\n---- SEGMENTATION FAULT ----\n");
-
-    snprintf(
-        buf, 512,
-        "eax: %x, ebx: %x, ecx: %x, edx: %x\n"
-        "esp: %x, ebp: %x, esi: %x, edi: %x\n"
-        "eip: %x, cs: %x, error_code: %x   \n"
-        "eflags: %x                        \n",
-        s_regs.eax, s_regs.ebx, s_regs.ecx,
-        s_regs.edx, s_regs.esp, s_regs.ebp,
-        s_regs.esi, s_regs.edi, eip,
-        cs, error_code, eflags);
+    char buf[128] = {};
+    snprintf(buf, sizeof(buf),
+        "[kernel] int13 data: error_code: %x, cs: %x, eflags: %x\n",
+        error_code, cs, eflags);
     kmsg(buf);
-
-    kmsg("----   HALTING SYSTEM   ----\n");
-
-    asm_cli();
-    asm_hlt();
+    if (!current_process->attr.system)
+        kill_current(-1);
+    else
+        die(s_regs, eip);
 }
 
 struct PACKED int14_data {
@@ -159,12 +145,13 @@ struct PACKED int14_data {
 
 static inline void _int14_panic(void* eip, void* cr2, struct page_fault_error_code error_code)
 {
-    char buf[256] {};
-    snprintf(
-        buf, 256,
-        "\nkilled: segmentation fault (eip: %x, cr2: %x, error_code: %x)\n", eip, cr2, error_code);
+    char buf[128] = {};
+    snprintf(buf, sizeof(buf),
+        "[kernel] int14 data: eip: %p, cr2: %p, error_code: %x\n"
+        "[kernel] freezing...\n",
+        eip, cr2, error_code);
     kmsg(buf);
-    assert(false);
+    freeze();
 }
 
 static inline void NORETURN _int14_kill_user(void)
@@ -172,9 +159,7 @@ static inline void NORETURN _int14_kill_user(void)
     char buf[256] {};
     snprintf(buf, 256, "Segmentation Fault (pid%d killed)\n", current_process->pid);
     kmsg(buf);
-    procs->kill(current_process->pid, -1);
-    schedule();
-    _halt_forever();
+    kill_current(-1);
 }
 
 // page fault

+ 22 - 8
src/kernel/process.cpp

@@ -120,12 +120,6 @@ void proclist::kill(pid_t pid, int exit_code)
     parent->wait_lst.push({ nullptr, (void*)pid, (void*)exit_code, nullptr });
 }
 
-inline void NORETURN _noreturn_crash(void)
-{
-    for (;;)
-        assert(false);
-}
-
 void kernel_threadd_main(void)
 {
     kmsg("kernel thread daemon started\n");
@@ -232,7 +226,7 @@ void NORETURN _kernel_init(void)
         : "c"(d.sp), "d"(d.eip)
         : "eax", "memory");
 
-    _noreturn_crash();
+    freeze();
 }
 
 void k_new_thread(void (*func)(void*), void* data)
@@ -292,7 +286,7 @@ void NORETURN init_scheduler(void)
         : "a"(current_thread->esp), "c"(_kernel_init)
         : "memory");
 
-    _noreturn_crash();
+    freeze();
 }
 
 extern "C" void asm_ctx_switch(uint32_t** curr_esp, uint32_t* next_esp);
@@ -316,3 +310,23 @@ void schedule()
 
     asm_ctx_switch(&curr_thd->esp, thd->esp);
 }
+
+void NORETURN schedule_noreturn(void)
+{
+    schedule();
+    freeze();
+}
+
+void NORETURN freeze(void)
+{
+    asm_cli();
+    asm_hlt();
+    for (;;)
+        ;
+}
+
+void NORETURN kill_current(int exit_code)
+{
+    procs->kill(current_process->pid, exit_code);
+    schedule_noreturn();
+}

+ 1 - 2
src/kernel/syscall.cpp

@@ -125,8 +125,7 @@ void _syscall_sleep(interrupt_stack* data)
 void _syscall_crash(interrupt_stack*)
 {
     kmsg("\nan error occurred while executing command\n");
-    asm_cli();
-    asm_hlt();
+    freeze();
 }
 
 // syscall_exec(const char* exec, const char** argv)

+ 2 - 4
src/types/libstdcpp.cpp

@@ -1,6 +1,7 @@
 #include <asm/port_io.h>
 #include <assert.h>
 #include <kernel/log.hpp>
+#include <kernel/process.hpp>
 #include <stdio.h>
 #include <types/types.h>
 
@@ -30,8 +31,5 @@ __assert_fail(const char* statement, const char* file, int line, const char* fun
     snprintf(buf, sizeof(buf), "Kernel assertion failed: (%s), %s:%d, %s\n",
         statement, file, line, func);
     kmsg(buf);
-    asm_cli();
-    asm_hlt();
-    for (;;)
-        ;
+    freeze();
 }