1 #include <lunaix/process.h>
2 #include <lunaix/sched.h>
3 #include <lunaix/signal.h>
4 #include <lunaix/syscall.h>
6 extern struct scheduler sched_ctx; /* kernel/sched.c */
8 void* default_handlers[_SIG_NUM] = {
12 // Referenced in kernel/asm/x86/interrupt.S
16 // if (!(SEL_RPL(__current->intr_ctx.cs))) {
21 if (!__current->sig_pending) {
27 31 - __builtin_clz(__current->sig_pending & ~__current->sig_mask);
29 __current->sig_pending = __current->sig_pending & ~(1 << sig_selected);
31 if (!__current->sig_handler[sig_selected] &&
32 !default_handlers[sig_selected]) {
37 uintptr_t ustack = __current->ustack_top;
39 if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
44 struct proc_sig* sig_ctx =
45 (struct proc_sig*)(ustack - sizeof(struct proc_sig));
47 sig_ctx->prev_context = __current->intr_ctx;
48 sig_ctx->sig_num = sig_selected;
49 sig_ctx->signal_handler = __current->sig_handler[sig_selected];
51 if (!sig_ctx->signal_handler) {
52 // 如果没有用户自定义的Handler,则使用系统默认Handler。
53 sig_ctx->signal_handler = default_handlers[sig_selected];
59 __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx)
61 __current->intr_ctx = sig_ctx->prev_context;
65 __DEFINE_LXSYSCALL3(int,
74 *oldset = __current->sig_mask;
75 if (how == _SIG_BLOCK) {
76 __current->sig_mask |= *set;
77 } else if (how == _SIG_UNBLOCK) {
78 __current->sig_mask &= ~(*set);
79 } else if (how == _SIG_SETMASK) {
80 __current->sig_mask = *set;
84 __current->sig_mask &= ~_SIGNAL_UNMASKABLE;
88 __DEFINE_LXSYSCALL2(int, signal, int, signum, sighandler_t, handler)
90 if (signum < 0 || signum >= _SIG_NUM) {
94 if (((1 << signum) & _SIGNAL_UNMASKABLE)) {
98 __current->sig_handler[signum] = (void*)handler;