1 #include <lunaix/lunistd.h>
2 #include <lunaix/lxsignal.h>
3 #include <lunaix/process.h>
4 #include <lunaix/sched.h>
5 #include <lunaix/signal.h>
6 #include <lunaix/spike.h>
7 #include <lunaix/status.h>
8 #include <lunaix/syscall.h>
10 extern struct scheduler sched_ctx; /* kernel/sched.c */
13 default_sighandler_term(int signum)
18 void* default_handlers[_SIG_NUM] = {
20 [_SIGINT] = default_sighandler_term, [_SIGTERM] = default_sighandler_term,
21 [_SIGKILL] = default_sighandler_term, [_SIGSEGV] = default_sighandler_term,
22 [_SIGINT] = default_sighandler_term,
25 volatile isr_param __temp_save;
26 // Referenced in kernel/asm/x86/interrupt.S
30 if (!__current->sig_pending) {
36 31 - __builtin_clz(__current->sig_pending &
37 ~(__current->sig_mask | __current->sig_inprogress));
39 __SIGCLEAR(__current->sig_pending, sig_selected);
41 if (sig_selected == 0) {
46 if (!__current->sig_handler[sig_selected] &&
47 !default_handlers[sig_selected]) {
52 uintptr_t ustack = __current->ustack_top & ~0xf;
54 if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
59 struct proc_sig* sig_ctx =
60 (struct proc_sig*)(ustack - sizeof(struct proc_sig));
64 问题是出在原本的sig_ctx->prev_context = __current->intr_ctx的上面
65 这个语句会被gcc在编译时,用更加高效的 rep movsl 来代替。
67 由于我们采用按需分页,所以在很多情况下,用户栈实际被分配的空间不允许我们进行完整的
68 注入,而需要走page fault handler进行动态分页。
72 假若我们的__current->intr_ctx注入了一半,然后产生page-fault中断,
73 那么这就会导致我们的__current->intr_ctx被这个page-fault中断导致的
74 上下文信息覆盖。那么当page-fault handler成功分配了一个页,返回,
75 拷贝也就得以进行。遗憾的是,只不过这次拷贝的内容和前面的拷贝是没有任何的关系
76 (因为此时的intr_ctx已经不是之前的intr_ctx了!)
77 而这就会导致我们保存在信号上下文中的进程上下文信息不完整,从而在soft_iret时
80 解决办法就是先吧intr_ctx拷贝到一个静态分配的区域里,然后再注入到用户栈。
82 __temp_save = __current->intr_ctx;
83 sig_ctx->prev_context = __temp_save;
85 sig_ctx->sig_num = sig_selected;
86 sig_ctx->signal_handler = __current->sig_handler[sig_selected];
88 if (!sig_ctx->signal_handler) {
89 // 如果没有用户自定义的Handler,则使用系统默认Handler。
90 sig_ctx->signal_handler = default_handlers[sig_selected];
93 __SIGSET(__current->sig_inprogress, sig_selected);
99 signal_send(pid_t pid, int signum)
101 if (signum < 0 || signum >= _SIG_NUM) {
102 __current->k_status = EINVAL;
106 struct proc_info* proc;
108 proc = get_process(pid);
113 } else if (pid < -1) {
114 proc = get_process(-pid);
117 // TODO: send to all process.
118 // But I don't want to support it yet.
119 __current->k_status = EINVAL;
124 struct proc_info *pos, *n;
125 llist_for_each(pos, n, &proc->grp_member, grp_member)
127 __SIGSET(pos->sig_pending, signum);
132 if (PROC_TERMINATED(proc->state)) {
133 __current->k_status = EINVAL;
136 __SIGSET(proc->sig_pending, signum);
140 __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx)
142 __current->intr_ctx = sig_ctx->prev_context;
143 __current->flags &= ~PROC_FINPAUSE;
144 __SIGCLEAR(__current->sig_inprogress, sig_ctx->sig_num);
148 __DEFINE_LXSYSCALL3(int,
157 *oldset = __current->sig_mask;
158 if (how == _SIG_BLOCK) {
159 __current->sig_mask |= *set;
160 } else if (how == _SIG_UNBLOCK) {
161 __current->sig_mask &= ~(*set);
162 } else if (how == _SIG_SETMASK) {
163 __current->sig_mask = *set;
167 __current->sig_mask &= ~_SIGNAL_UNMASKABLE;
171 __DEFINE_LXSYSCALL2(int, signal, int, signum, sighandler_t, handler)
173 if (signum <= 0 || signum >= _SIG_NUM) {
177 if ((__SIGNAL(signum) & _SIGNAL_UNMASKABLE)) {
181 __current->sig_handler[signum] = (void*)handler;
189 __current->flags |= PROC_FINPAUSE;
191 while ((__current->flags & PROC_FINPAUSE)) {
195 __current->k_status = EINTR;
198 __DEFINE_LXSYSCALL(int, pause)
204 __DEFINE_LXSYSCALL2(int, kill, pid_t, pid, int, signum)
206 return signal_send(pid, signum);
209 __DEFINE_LXSYSCALL1(int, sigpending, sigset_t, *sigset)
211 *sigset = __current->sig_pending;
215 __DEFINE_LXSYSCALL1(int, sigsuspend, sigset_t, *mask)
217 sigset_t tmp = __current->sig_mask;
218 __current->sig_mask = (*mask) & ~_SIGNAL_UNMASKABLE;
220 __current->sig_mask = tmp;