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] = {
15 // if (!(SEL_RPL(__current->intr_ctx.cs))) {
20 if (!__current->sig_pending) {
26 31 - __builtin_clz(__current->sig_pending & ~__current->sig_mask);
28 __current->sig_pending = __current->sig_pending & ~(1 << sig_selected);
30 if (!__current->sig_handler[sig_selected] &&
31 !default_handlers[sig_selected]) {
36 uintptr_t ustack = __current->ustack_top;
38 if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
43 struct proc_sig* sig_ctx =
44 (struct proc_sig*)(ustack - sizeof(struct proc_sig));
46 sig_ctx->prev_context = __current->intr_ctx;
47 sig_ctx->sig_num = sig_selected;
48 sig_ctx->signal_handler = __current->sig_handler[sig_selected];
50 if (!sig_ctx->signal_handler) {
51 // 如果没有用户自定义的Handler,则使用系统默认Handler。
52 sig_ctx->signal_handler = default_handlers[sig_selected];
55 asm volatile("pushl %0\n"
56 "jmp handle_signal" ::"r"(sig_ctx));
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;