refactor: elf parsing utility and exec related
[lunaix-os.git] / lunaix-os / kernel / process / signal.c
1 #include <lunaix/process.h>
2 #include <lunaix/sched.h>
3 #include <lunaix/signal.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/status.h>
6 #include <lunaix/syscall.h>
7
8 #include <klibc/string.h>
9
10 extern struct scheduler sched_ctx; /* kernel/sched.c */
11
12 void __USER__
13 default_sighandler_term(int signum)
14 {
15     _exit(signum);
16 }
17
18 void* default_handlers[_SIG_NUM] = {
19     // TODO: 添加默认handler
20     [_SIGINT] = default_sighandler_term,
21     [_SIGTERM] = default_sighandler_term,
22     [_SIGKILL] = default_sighandler_term,
23     [_SIGSEGV] = default_sighandler_term,
24 };
25
26 // Referenced in kernel/asm/x86/interrupt.S
27 void*
28 signal_dispatch()
29 {
30     if (!__current->sig_pending) {
31         // 没有待处理信号
32         return 0;
33     }
34
35     int sig_selected =
36       31 - __builtin_clz(__current->sig_pending &
37                          ~(__current->sig_mask | __current->sig_inprogress));
38
39     __SIGCLEAR(__current->sig_pending, sig_selected);
40
41     if (sig_selected == 0) {
42         // SIG0 is reserved
43         return 0;
44     }
45
46     // TODO: SIG{INT|TERM|KILL|SEGV} should have highest priority.
47     //       Terminate the process right here if any of unmaskable signal is
48     //       set.
49
50     if (!__current->sig_handler[sig_selected] &&
51         !default_handlers[sig_selected]) {
52         // 如果该信号没有handler,则忽略
53         return 0;
54     }
55
56     uintptr_t ustack = __current->ustack_top & ~0xf;
57
58     if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
59         // 用户栈没有空间存放信号上下文
60         return 0;
61     }
62
63     struct proc_sig* sig_ctx =
64       (struct proc_sig*)(ustack - sizeof(struct proc_sig));
65
66     /*
67         这是一个相当恶心的坑。
68         问题是出在原本的sig_ctx->prev_context = __current->intr_ctx的上面
69         这个语句会被gcc在编译时,用更加高效的 rep movsl 来代替。
70
71         由于我们采用按需分页,所以在很多情况下,用户栈实际被分配的空间不允许我们进行完整的
72         注入,而需要走page fault handler进行动态分页。
73
74         竞态条件就出现在这里!
75
76         假若我们的__current->intr_ctx注入了一半,然后产生page-fault中断,
77         那么这就会导致我们的__current->intr_ctx被这个page-fault中断导致的
78         上下文信息覆盖。那么当page-fault handler成功分配了一个页,返回,
79         拷贝也就得以进行。遗憾的是,只不过这次拷贝的内容和前面的拷贝是没有任何的关系
80         (因为此时的intr_ctx已经不是之前的intr_ctx了!)
81         而这就会导致我们保存在信号上下文中的进程上下文信息不完整,从而在soft_iret时
82         触发#GP。
83
84         解决办法就是先吧intr_ctx拷贝到一个静态分配的区域里,然后再注入到用户栈。
85     */
86     static volatile struct proc_sigstate __temp_save;
87     __temp_save.proc_regs = __current->intr_ctx;
88     memcpy(__temp_save.fxstate, __current->fxstate, 512);
89
90     sig_ctx->prev_context = __temp_save;
91
92     sig_ctx->sig_num = sig_selected;
93     sig_ctx->signal_handler = __current->sig_handler[sig_selected];
94
95     if (!sig_ctx->signal_handler) {
96         // 如果没有用户自定义的Handler,则使用系统默认Handler。
97         sig_ctx->signal_handler = default_handlers[sig_selected];
98     }
99
100     __SIGSET(__current->sig_inprogress, sig_selected);
101
102     return sig_ctx;
103 }
104
105 int
106 signal_send(pid_t pid, int signum)
107 {
108     if (signum < 0 || signum >= _SIG_NUM) {
109         __current->k_status = EINVAL;
110         return -1;
111     }
112
113     struct proc_info* proc;
114     if (pid > 0) {
115         proc = get_process(pid);
116         goto send_single;
117     } else if (!pid) {
118         proc = __current;
119         goto send_grp;
120     } else if (pid < -1) {
121         proc = get_process(-pid);
122         goto send_grp;
123     } else {
124         // TODO: send to all process.
125         //  But I don't want to support it yet.
126         __current->k_status = EINVAL;
127         return -1;
128     }
129
130 send_grp:
131     struct proc_info *pos, *n;
132     llist_for_each(pos, n, &proc->grp_member, grp_member)
133     {
134         __SIGSET(pos->sig_pending, signum);
135     }
136
137 send_single:
138     if (PROC_TERMINATED(proc->state)) {
139         __current->k_status = EINVAL;
140         return -1;
141     }
142     __SIGSET(proc->sig_pending, signum);
143     return 0;
144 }
145
146 __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx)
147 {
148     memcpy(__current->fxstate, sig_ctx->prev_context.fxstate, 512);
149     __current->intr_ctx = sig_ctx->prev_context.proc_regs;
150     __current->flags &= ~PROC_FINPAUSE;
151     __SIGCLEAR(__current->sig_inprogress, sig_ctx->sig_num);
152     schedule();
153 }
154
155 __DEFINE_LXSYSCALL3(int,
156                     sigprocmask,
157                     int,
158                     how,
159                     const sigset_t,
160                     *set,
161                     sigset_t,
162                     *oldset)
163 {
164     *oldset = __current->sig_mask;
165     if (how == _SIG_BLOCK) {
166         __current->sig_mask |= *set;
167     } else if (how == _SIG_UNBLOCK) {
168         __current->sig_mask &= ~(*set);
169     } else if (how == _SIG_SETMASK) {
170         __current->sig_mask = *set;
171     } else {
172         return 0;
173     }
174     __current->sig_mask &= ~_SIGNAL_UNMASKABLE;
175     return 1;
176 }
177
178 __DEFINE_LXSYSCALL2(int, signal, int, signum, sighandler_t, handler)
179 {
180     if (signum <= 0 || signum >= _SIG_NUM) {
181         return -1;
182     }
183
184     if ((__SIGNAL(signum) & _SIGNAL_UNMASKABLE)) {
185         return -1;
186     }
187
188     __current->sig_handler[signum] = (void*)handler;
189
190     return 0;
191 }
192
193 void
194 __do_pause()
195 {
196     __current->flags |= PROC_FINPAUSE;
197
198     while ((__current->flags & PROC_FINPAUSE)) {
199         sched_yieldk();
200     }
201
202     __current->k_status = EINTR;
203 }
204
205 __DEFINE_LXSYSCALL(int, pause)
206 {
207     __do_pause();
208     return -1;
209 }
210
211 __DEFINE_LXSYSCALL2(int, kill, pid_t, pid, int, signum)
212 {
213     return signal_send(pid, signum);
214 }
215
216 __DEFINE_LXSYSCALL1(int, sigpending, sigset_t, *sigset)
217 {
218     *sigset = __current->sig_pending;
219     return 0;
220 }
221
222 __DEFINE_LXSYSCALL1(int, sigsuspend, sigset_t, *mask)
223 {
224     sigset_t tmp = __current->sig_mask;
225     __current->sig_mask = (*mask) & ~_SIGNAL_UNMASKABLE;
226     __do_pause();
227     __current->sig_mask = tmp;
228     return -1;
229 }