refactor: rewrite kernel's make script
[lunaix-os.git] / lunaix-os / kernel / process / signal.c
index 5d09d9b6efa59bb02df9faabb3d0c7111d5ce9d2..d12c62e176e8f1d3800a3345e94f672aa93a26d3 100644 (file)
@@ -1,5 +1,3 @@
-#include <lunaix/lunistd.h>
-#include <lunaix/lxsignal.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/signal.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/signal.h>
@@ -7,22 +5,13 @@
 #include <lunaix/status.h>
 #include <lunaix/syscall.h>
 
 #include <lunaix/status.h>
 #include <lunaix/syscall.h>
 
-extern struct scheduler sched_ctx; /* kernel/sched.c */
+#include <klibc/string.h>
 
 
-void __USER__
-default_sighandler_term(int signum)
-{
-    _exit(signum);
-}
+extern struct scheduler sched_ctx; /* kernel/sched.c */
 
 
-void* default_handlers[_SIG_NUM] = {
-    // TODO: 添加默认handler
-    [_SIGINT] = default_sighandler_term,  [_SIGTERM] = default_sighandler_term,
-    [_SIGKILL] = default_sighandler_term, [_SIGSEGV] = default_sighandler_term,
-    [_SIGINT] = default_sighandler_term,
-};
+static u32_t term_sigs =
+  (1 << SIGSEGV) | (1 << SIGINT) | (1 << SIGKILL) | (1 << SIGTERM);
 
 
-volatile isr_param __temp_save;
 // Referenced in kernel/asm/x86/interrupt.S
 void*
 signal_dispatch()
 // Referenced in kernel/asm/x86/interrupt.S
 void*
 signal_dispatch()
@@ -43,13 +32,16 @@ signal_dispatch()
         return 0;
     }
 
         return 0;
     }
 
-    if (!__current->sig_handler[sig_selected] &&
-        !default_handlers[sig_selected]) {
-        // 如果该信号没有handler,则忽略
+    if (!__current->sig_handler[sig_selected]) {
+        if ((term_sigs & (1 << sig_selected))) {
+            terminate_proc(sig_selected);
+            schedule();
+            // never return
+        }
         return 0;
     }
 
         return 0;
     }
 
-    uintptr_t ustack = __current->ustack_top & ~0xf;
+    ptr_t ustack = __current->ustack_top & ~0xf;
 
     if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
         // 用户栈没有空间存放信号上下文
 
     if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
         // 用户栈没有空间存放信号上下文
@@ -79,17 +71,15 @@ signal_dispatch()
 
         解决办法就是先吧intr_ctx拷贝到一个静态分配的区域里,然后再注入到用户栈。
     */
 
         解决办法就是先吧intr_ctx拷贝到一个静态分配的区域里,然后再注入到用户栈。
     */
-    __temp_save = __current->intr_ctx;
+    static volatile struct proc_sigstate __temp_save;
+    __temp_save.proc_regs = __current->intr_ctx;
+    memcpy(__temp_save.fxstate, __current->fxstate, 512);
+
     sig_ctx->prev_context = __temp_save;
 
     sig_ctx->sig_num = sig_selected;
     sig_ctx->signal_handler = __current->sig_handler[sig_selected];
 
     sig_ctx->prev_context = __temp_save;
 
     sig_ctx->sig_num = sig_selected;
     sig_ctx->signal_handler = __current->sig_handler[sig_selected];
 
-    if (!sig_ctx->signal_handler) {
-        // 如果没有用户自定义的Handler,则使用系统默认Handler。
-        sig_ctx->signal_handler = default_handlers[sig_selected];
-    }
-
     __SIGSET(__current->sig_inprogress, sig_selected);
 
     return sig_ctx;
     __SIGSET(__current->sig_inprogress, sig_selected);
 
     return sig_ctx;
@@ -138,10 +128,14 @@ send_single:
 
 __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx)
 {
 
 __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx)
 {
-    __current->intr_ctx = sig_ctx->prev_context;
+    memcpy(__current->fxstate, sig_ctx->prev_context.fxstate, 512);
+    __current->intr_ctx = sig_ctx->prev_context.proc_regs;
     __current->flags &= ~PROC_FINPAUSE;
     __SIGCLEAR(__current->sig_inprogress, sig_ctx->sig_num);
     schedule();
     __current->flags &= ~PROC_FINPAUSE;
     __SIGCLEAR(__current->sig_inprogress, sig_ctx->sig_num);
     schedule();
+
+    // never reach!
+    return 0;
 }
 
 __DEFINE_LXSYSCALL3(int,
 }
 
 __DEFINE_LXSYSCALL3(int,