Commit | Line | Data |
---|---|---|
1394f032 | 1 | /* |
e8f263df | 2 | * Copyright 2004-2010 Analog Devices Inc. |
1394f032 | 3 | * |
96f1050d | 4 | * Licensed under the GPL-2 or later |
1394f032 BW |
5 | */ |
6 | ||
7 | #include <linux/signal.h> | |
8 | #include <linux/syscalls.h> | |
9 | #include <linux/ptrace.h> | |
10 | #include <linux/tty.h> | |
11 | #include <linux/personality.h> | |
12 | #include <linux/binfmts.h> | |
13 | #include <linux/freezer.h> | |
1f83b8f1 | 14 | #include <linux/uaccess.h> |
d1be2e48 | 15 | #include <linux/tracehook.h> |
1394f032 | 16 | |
1394f032 BW |
17 | #include <asm/cacheflush.h> |
18 | #include <asm/ucontext.h> | |
697a9d65 | 19 | #include <asm/fixed_code.h> |
ddaebcab | 20 | #include <asm/syscall.h> |
1394f032 BW |
21 | |
22 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) | |
23 | ||
8513c42e BS |
24 | /* Location of the trace bit in SYSCFG. */ |
25 | #define TRACE_BITS 0x0001 | |
26 | ||
1394f032 BW |
27 | struct fdpic_func_descriptor { |
28 | unsigned long text; | |
29 | unsigned long GOT; | |
30 | }; | |
31 | ||
32 | struct rt_sigframe { | |
33 | int sig; | |
34 | struct siginfo *pinfo; | |
35 | void *puc; | |
697a9d65 BS |
36 | /* This is no longer needed by the kernel, but unfortunately userspace |
37 | * code expects it to be there. */ | |
1394f032 BW |
38 | char retcode[8]; |
39 | struct siginfo info; | |
40 | struct ucontext uc; | |
41 | }; | |
42 | ||
0ddeeca2 | 43 | asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss) |
1394f032 BW |
44 | { |
45 | return do_sigaltstack(uss, uoss, rdusp()); | |
46 | } | |
47 | ||
48 | static inline int | |
0ddeeca2 | 49 | rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0) |
1394f032 BW |
50 | { |
51 | unsigned long usp = 0; | |
52 | int err = 0; | |
53 | ||
ddaebcab MF |
54 | /* Always make any pending restarted system calls return -EINTR */ |
55 | current_thread_info()->restart_block.fn = do_no_restart_syscall; | |
56 | ||
1394f032 BW |
57 | #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x) |
58 | ||
59 | /* restore passed registers */ | |
60 | RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3); | |
61 | RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7); | |
62 | RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3); | |
63 | RESTORE(p4); RESTORE(p5); | |
64 | err |= __get_user(usp, &sc->sc_usp); | |
65 | wrusp(usp); | |
66 | RESTORE(a0w); RESTORE(a1w); | |
67 | RESTORE(a0x); RESTORE(a1x); | |
68 | RESTORE(astat); | |
69 | RESTORE(rets); | |
70 | RESTORE(pc); | |
71 | RESTORE(retx); | |
72 | RESTORE(fp); | |
73 | RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3); | |
74 | RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3); | |
75 | RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3); | |
76 | RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3); | |
77 | RESTORE(lc0); RESTORE(lc1); | |
78 | RESTORE(lt0); RESTORE(lt1); | |
79 | RESTORE(lb0); RESTORE(lb1); | |
80 | RESTORE(seqstat); | |
81 | ||
82 | regs->orig_p0 = -1; /* disable syscall checks */ | |
83 | ||
84 | *pr0 = regs->r0; | |
85 | return err; | |
86 | } | |
87 | ||
88 | asmlinkage int do_rt_sigreturn(unsigned long __unused) | |
89 | { | |
90 | struct pt_regs *regs = (struct pt_regs *)__unused; | |
91 | unsigned long usp = rdusp(); | |
92 | struct rt_sigframe *frame = (struct rt_sigframe *)(usp); | |
93 | sigset_t set; | |
94 | int r0; | |
95 | ||
96 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
97 | goto badframe; | |
98 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | |
99 | goto badframe; | |
100 | ||
101 | sigdelsetmask(&set, ~_BLOCKABLE); | |
8e3f9f65 | 102 | set_current_blocked(&set); |
1394f032 BW |
103 | |
104 | if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0)) | |
105 | goto badframe; | |
106 | ||
107 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT) | |
108 | goto badframe; | |
109 | ||
110 | return r0; | |
111 | ||
1f83b8f1 | 112 | badframe: |
1394f032 BW |
113 | force_sig(SIGSEGV, current); |
114 | return 0; | |
115 | } | |
116 | ||
117 | static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs) | |
118 | { | |
119 | int err = 0; | |
120 | ||
121 | #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x) | |
122 | ||
123 | SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3); | |
124 | SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7); | |
125 | SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3); | |
126 | SETUP(p4); SETUP(p5); | |
127 | err |= __put_user(rdusp(), &sc->sc_usp); | |
128 | SETUP(a0w); SETUP(a1w); | |
129 | SETUP(a0x); SETUP(a1x); | |
130 | SETUP(astat); | |
131 | SETUP(rets); | |
132 | SETUP(pc); | |
133 | SETUP(retx); | |
134 | SETUP(fp); | |
135 | SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3); | |
136 | SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3); | |
137 | SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3); | |
138 | SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3); | |
139 | SETUP(lc0); SETUP(lc1); | |
140 | SETUP(lt0); SETUP(lt1); | |
141 | SETUP(lb0); SETUP(lb1); | |
142 | SETUP(seqstat); | |
143 | ||
144 | return err; | |
145 | } | |
146 | ||
1394f032 BW |
147 | static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, |
148 | size_t frame_size) | |
149 | { | |
150 | unsigned long usp; | |
151 | ||
152 | /* Default to using normal stack. */ | |
153 | usp = rdusp(); | |
154 | ||
155 | /* This is the X/Open sanctioned signal stack switching. */ | |
156 | if (ka->sa.sa_flags & SA_ONSTACK) { | |
157 | if (!on_sig_stack(usp)) | |
158 | usp = current->sas_ss_sp + current->sas_ss_size; | |
159 | } | |
160 | return (void *)((usp - frame_size) & -8UL); | |
161 | } | |
162 | ||
163 | static int | |
164 | setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info, | |
165 | sigset_t * set, struct pt_regs *regs) | |
166 | { | |
167 | struct rt_sigframe *frame; | |
168 | int err = 0; | |
169 | ||
170 | frame = get_sigframe(ka, regs, sizeof(*frame)); | |
171 | ||
172 | err |= __put_user((current_thread_info()->exec_domain | |
173 | && current_thread_info()->exec_domain->signal_invmap | |
174 | && sig < 32 | |
175 | ? current_thread_info()->exec_domain-> | |
176 | signal_invmap[sig] : sig), &frame->sig); | |
177 | ||
178 | err |= __put_user(&frame->info, &frame->pinfo); | |
179 | err |= __put_user(&frame->uc, &frame->puc); | |
180 | err |= copy_siginfo_to_user(&frame->info, info); | |
181 | ||
182 | /* Create the ucontext. */ | |
183 | err |= __put_user(0, &frame->uc.uc_flags); | |
184 | err |= __put_user(0, &frame->uc.uc_link); | |
185 | err |= | |
186 | __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); | |
187 | err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags); | |
188 | err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); | |
189 | err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs); | |
190 | err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | |
191 | ||
1394f032 BW |
192 | if (err) |
193 | goto give_sigsegv; | |
194 | ||
1394f032 BW |
195 | /* Set up registers for signal handler */ |
196 | wrusp((unsigned long)frame); | |
ecd0fa98 | 197 | if (current->personality & FDPIC_FUNCPTRS) { |
1394f032 BW |
198 | struct fdpic_func_descriptor __user *funcptr = |
199 | (struct fdpic_func_descriptor *) ka->sa.sa_handler; | |
200 | __get_user(regs->pc, &funcptr->text); | |
201 | __get_user(regs->p3, &funcptr->GOT); | |
202 | } else | |
203 | regs->pc = (unsigned long)ka->sa.sa_handler; | |
697a9d65 | 204 | regs->rets = SIGRETURN_STUB; |
1394f032 BW |
205 | |
206 | regs->r0 = frame->sig; | |
207 | regs->r1 = (unsigned long)(&frame->info); | |
208 | regs->r2 = (unsigned long)(&frame->uc); | |
209 | ||
210 | return 0; | |
211 | ||
1f83b8f1 | 212 | give_sigsegv: |
5d71c55b | 213 | force_sigsegv(sig, current); |
1394f032 BW |
214 | return -EFAULT; |
215 | } | |
216 | ||
217 | static inline void | |
218 | handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler) | |
219 | { | |
220 | switch (regs->r0) { | |
221 | case -ERESTARTNOHAND: | |
222 | if (!has_handler) | |
223 | goto do_restart; | |
224 | regs->r0 = -EINTR; | |
225 | break; | |
226 | ||
227 | case -ERESTARTSYS: | |
228 | if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) { | |
229 | regs->r0 = -EINTR; | |
230 | break; | |
231 | } | |
232 | /* fallthrough */ | |
233 | case -ERESTARTNOINTR: | |
1f83b8f1 | 234 | do_restart: |
1394f032 BW |
235 | regs->p0 = regs->orig_p0; |
236 | regs->r0 = regs->orig_r0; | |
237 | regs->pc -= 2; | |
238 | break; | |
ddaebcab MF |
239 | |
240 | case -ERESTART_RESTARTBLOCK: | |
241 | regs->p0 = __NR_restart_syscall; | |
242 | regs->pc -= 2; | |
243 | break; | |
1394f032 BW |
244 | } |
245 | } | |
246 | ||
247 | /* | |
248 | * OK, we're invoking a handler | |
249 | */ | |
250 | static int | |
251 | handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka, | |
b7f9a11a | 252 | struct pt_regs *regs) |
1394f032 BW |
253 | { |
254 | int ret; | |
255 | ||
256 | /* are we from a system call? to see pt_regs->orig_p0 */ | |
257 | if (regs->orig_p0 >= 0) | |
258 | /* If so, check system call restarting.. */ | |
259 | handle_restart(regs, ka, 1); | |
260 | ||
261 | /* set up the stack frame */ | |
b7f9a11a | 262 | ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs); |
1394f032 | 263 | |
8e3f9f65 MF |
264 | if (ret == 0) |
265 | block_sigmask(ka, sig); | |
266 | ||
1394f032 BW |
267 | return ret; |
268 | } | |
269 | ||
270 | /* | |
271 | * Note that 'init' is a special process: it doesn't get signals it doesn't | |
272 | * want to handle. Thus you cannot kill init even with a SIGKILL even by | |
273 | * mistake. | |
274 | * | |
275 | * Note that we go through the signals twice: once to check the signals | |
276 | * that the kernel can handle, and then we build all the user-level signal | |
277 | * handling stack-frames in one go after that. | |
278 | */ | |
279 | asmlinkage void do_signal(struct pt_regs *regs) | |
280 | { | |
281 | siginfo_t info; | |
282 | int signr; | |
283 | struct k_sigaction ka; | |
1394f032 BW |
284 | |
285 | current->thread.esp0 = (unsigned long)regs; | |
286 | ||
287 | if (try_to_freeze()) | |
288 | goto no_signal; | |
289 | ||
1394f032 BW |
290 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
291 | if (signr > 0) { | |
292 | /* Whee! Actually deliver the signal. */ | |
b7f9a11a | 293 | if (handle_signal(signr, &info, &ka, regs) == 0) { |
1394f032 BW |
294 | /* a signal was successfully delivered; the saved |
295 | * sigmask will have been stored in the signal frame, | |
296 | * and will be restored by sigreturn, so we can simply | |
297 | * clear the TIF_RESTORE_SIGMASK flag */ | |
298 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) | |
299 | clear_thread_flag(TIF_RESTORE_SIGMASK); | |
e8f263df | 300 | |
600482c1 MF |
301 | tracehook_signal_handler(signr, &info, &ka, regs, |
302 | test_thread_flag(TIF_SINGLESTEP)); | |
1394f032 BW |
303 | } |
304 | ||
305 | return; | |
306 | } | |
307 | ||
1f83b8f1 | 308 | no_signal: |
1394f032 BW |
309 | /* Did we come from a system call? */ |
310 | if (regs->orig_p0 >= 0) | |
311 | /* Restart the system call - no handlers present */ | |
312 | handle_restart(regs, NULL, 0); | |
313 | ||
314 | /* if there's no signal to deliver, we just put the saved sigmask | |
315 | * back */ | |
51a7b448 | 316 | restore_saved_sigmask(); |
1394f032 | 317 | } |
d1be2e48 BS |
318 | |
319 | /* | |
320 | * notification of userspace execution resumption | |
321 | */ | |
322 | asmlinkage void do_notify_resume(struct pt_regs *regs) | |
323 | { | |
324 | if (test_thread_flag(TIF_SIGPENDING) || test_thread_flag(TIF_RESTORE_SIGMASK)) | |
325 | do_signal(regs); | |
326 | ||
327 | if (test_thread_flag(TIF_NOTIFY_RESUME)) { | |
328 | clear_thread_flag(TIF_NOTIFY_RESUME); | |
329 | tracehook_notify_resume(regs); | |
d1be2e48 BS |
330 | } |
331 | } | |
332 |