]>
Commit | Line | Data |
---|---|---|
b920de1b DH |
1 | /* MN10300 Signal handling |
2 | * | |
3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public Licence | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the Licence, or (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/sched.h> | |
13 | #include <linux/mm.h> | |
14 | #include <linux/smp.h> | |
b920de1b DH |
15 | #include <linux/kernel.h> |
16 | #include <linux/signal.h> | |
17 | #include <linux/errno.h> | |
18 | #include <linux/wait.h> | |
19 | #include <linux/ptrace.h> | |
20 | #include <linux/unistd.h> | |
21 | #include <linux/stddef.h> | |
22 | #include <linux/tty.h> | |
23 | #include <linux/personality.h> | |
24 | #include <linux/suspend.h> | |
5d289964 | 25 | #include <linux/tracehook.h> |
b920de1b DH |
26 | #include <asm/cacheflush.h> |
27 | #include <asm/ucontext.h> | |
28 | #include <asm/uaccess.h> | |
29 | #include <asm/fpu.h> | |
30 | #include "sigframe.h" | |
31 | ||
32 | #define DEBUG_SIG 0 | |
33 | ||
b920de1b DH |
34 | /* |
35 | * do a signal return; undo the signal stack. | |
36 | */ | |
37 | static int restore_sigcontext(struct pt_regs *regs, | |
38 | struct sigcontext __user *sc, long *_d0) | |
39 | { | |
40 | unsigned int err = 0; | |
41 | ||
c05628b4 | 42 | /* Always make any pending restarted system calls return -EINTR */ |
f56141e3 | 43 | current->restart_block.fn = do_no_restart_syscall; |
c05628b4 | 44 | |
b920de1b DH |
45 | if (is_using_fpu(current)) |
46 | fpu_kill_state(current); | |
47 | ||
48 | #define COPY(x) err |= __get_user(regs->x, &sc->x) | |
49 | COPY(d1); COPY(d2); COPY(d3); | |
50 | COPY(a0); COPY(a1); COPY(a2); COPY(a3); | |
51 | COPY(e0); COPY(e1); COPY(e2); COPY(e3); | |
52 | COPY(e4); COPY(e5); COPY(e6); COPY(e7); | |
53 | COPY(lar); COPY(lir); | |
54 | COPY(mdr); COPY(mdrq); | |
55 | COPY(mcvf); COPY(mcrl); COPY(mcrh); | |
56 | COPY(sp); COPY(pc); | |
57 | #undef COPY | |
58 | ||
59 | { | |
60 | unsigned int tmpflags; | |
61 | #ifndef CONFIG_MN10300_USING_JTAG | |
62 | #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \ | |
63 | EPSW_T | EPSW_nAR) | |
64 | #else | |
65 | #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \ | |
66 | EPSW_nAR) | |
67 | #endif | |
68 | err |= __get_user(tmpflags, &sc->epsw); | |
69 | regs->epsw = (regs->epsw & ~USER_EPSW) | | |
70 | (tmpflags & USER_EPSW); | |
71 | regs->orig_d0 = -1; /* disable syscall checks */ | |
72 | } | |
73 | ||
74 | { | |
75 | struct fpucontext *buf; | |
76 | err |= __get_user(buf, &sc->fpucontext); | |
77 | if (buf) { | |
78 | if (verify_area(VERIFY_READ, buf, sizeof(*buf))) | |
79 | goto badframe; | |
80 | err |= fpu_restore_sigcontext(buf); | |
81 | } | |
82 | } | |
83 | ||
84 | err |= __get_user(*_d0, &sc->d0); | |
85 | return err; | |
86 | ||
87 | badframe: | |
88 | return 1; | |
89 | } | |
90 | ||
91 | /* | |
92 | * standard signal return syscall | |
93 | */ | |
94 | asmlinkage long sys_sigreturn(void) | |
95 | { | |
7c7fcf76 | 96 | struct sigframe __user *frame; |
b920de1b DH |
97 | sigset_t set; |
98 | long d0; | |
99 | ||
7c7fcf76 | 100 | frame = (struct sigframe __user *) current_frame()->sp; |
b920de1b DH |
101 | if (verify_area(VERIFY_READ, frame, sizeof(*frame))) |
102 | goto badframe; | |
103 | if (__get_user(set.sig[0], &frame->sc.oldmask)) | |
104 | goto badframe; | |
105 | ||
106 | if (_NSIG_WORDS > 1 && | |
107 | __copy_from_user(&set.sig[1], &frame->extramask, | |
108 | sizeof(frame->extramask))) | |
109 | goto badframe; | |
110 | ||
00f35785 | 111 | set_current_blocked(&set); |
b920de1b | 112 | |
7c7fcf76 | 113 | if (restore_sigcontext(current_frame(), &frame->sc, &d0)) |
b920de1b DH |
114 | goto badframe; |
115 | ||
116 | return d0; | |
117 | ||
118 | badframe: | |
119 | force_sig(SIGSEGV, current); | |
120 | return 0; | |
121 | } | |
122 | ||
123 | /* | |
124 | * realtime signal return syscall | |
125 | */ | |
126 | asmlinkage long sys_rt_sigreturn(void) | |
127 | { | |
7c7fcf76 | 128 | struct rt_sigframe __user *frame; |
b920de1b | 129 | sigset_t set; |
7c7fcf76 | 130 | long d0; |
b920de1b | 131 | |
7c7fcf76 | 132 | frame = (struct rt_sigframe __user *) current_frame()->sp; |
b920de1b DH |
133 | if (verify_area(VERIFY_READ, frame, sizeof(*frame))) |
134 | goto badframe; | |
135 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | |
136 | goto badframe; | |
137 | ||
00f35785 | 138 | set_current_blocked(&set); |
b920de1b | 139 | |
7c7fcf76 | 140 | if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0)) |
b920de1b DH |
141 | goto badframe; |
142 | ||
b30c7d50 | 143 | if (restore_altstack(&frame->uc.uc_stack)) |
b920de1b DH |
144 | goto badframe; |
145 | ||
146 | return d0; | |
147 | ||
148 | badframe: | |
149 | force_sig(SIGSEGV, current); | |
150 | return 0; | |
151 | } | |
152 | ||
153 | /* | |
154 | * store the userspace context into a signal frame | |
155 | */ | |
156 | static int setup_sigcontext(struct sigcontext __user *sc, | |
157 | struct fpucontext *fpuctx, | |
158 | struct pt_regs *regs, | |
159 | unsigned long mask) | |
160 | { | |
161 | int tmp, err = 0; | |
162 | ||
163 | #define COPY(x) err |= __put_user(regs->x, &sc->x) | |
164 | COPY(d0); COPY(d1); COPY(d2); COPY(d3); | |
165 | COPY(a0); COPY(a1); COPY(a2); COPY(a3); | |
166 | COPY(e0); COPY(e1); COPY(e2); COPY(e3); | |
167 | COPY(e4); COPY(e5); COPY(e6); COPY(e7); | |
168 | COPY(lar); COPY(lir); | |
169 | COPY(mdr); COPY(mdrq); | |
170 | COPY(mcvf); COPY(mcrl); COPY(mcrh); | |
171 | COPY(sp); COPY(epsw); COPY(pc); | |
172 | #undef COPY | |
173 | ||
174 | tmp = fpu_setup_sigcontext(fpuctx); | |
175 | if (tmp < 0) | |
176 | err = 1; | |
177 | else | |
178 | err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext); | |
179 | ||
180 | /* non-iBCS2 extensions.. */ | |
181 | err |= __put_user(mask, &sc->oldmask); | |
182 | ||
183 | return err; | |
184 | } | |
185 | ||
186 | /* | |
187 | * determine which stack to use.. | |
188 | */ | |
a8040ff8 | 189 | static inline void __user *get_sigframe(struct ksignal *ksig, |
b920de1b DH |
190 | struct pt_regs *regs, |
191 | size_t frame_size) | |
192 | { | |
a8040ff8 | 193 | unsigned long sp = sigsp(regs->sp, ksig); |
b920de1b DH |
194 | |
195 | return (void __user *) ((sp - frame_size) & ~7UL); | |
196 | } | |
197 | ||
198 | /* | |
199 | * set up a normal signal frame | |
200 | */ | |
8b166553 | 201 | static int setup_frame(struct ksignal *ksig, sigset_t *set, |
b920de1b DH |
202 | struct pt_regs *regs) |
203 | { | |
204 | struct sigframe __user *frame; | |
8b166553 | 205 | int rsig, sig = ksig->sig; |
b920de1b | 206 | |
a8040ff8 | 207 | frame = get_sigframe(ksig, regs, sizeof(*frame)); |
b920de1b DH |
208 | |
209 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
8b166553 | 210 | return -EFAULT; |
b920de1b DH |
211 | |
212 | rsig = sig; | |
213 | if (sig < 32 && | |
214 | current_thread_info()->exec_domain && | |
215 | current_thread_info()->exec_domain->signal_invmap) | |
216 | rsig = current_thread_info()->exec_domain->signal_invmap[sig]; | |
217 | ||
218 | if (__put_user(rsig, &frame->sig) < 0 || | |
219 | __put_user(&frame->sc, &frame->psc) < 0) | |
8b166553 | 220 | return -EFAULT; |
b920de1b DH |
221 | |
222 | if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0])) | |
8b166553 | 223 | return -EFAULT; |
b920de1b DH |
224 | |
225 | if (_NSIG_WORDS > 1) { | |
226 | if (__copy_to_user(frame->extramask, &set->sig[1], | |
227 | sizeof(frame->extramask))) | |
8b166553 | 228 | return -EFAULT; |
b920de1b DH |
229 | } |
230 | ||
231 | /* set up to return from userspace. If provided, use a stub already in | |
232 | * userspace */ | |
8b166553 RW |
233 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
234 | if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode)) | |
235 | return -EFAULT; | |
b920de1b DH |
236 | } else { |
237 | if (__put_user((void (*)(void))frame->retcode, | |
238 | &frame->pretcode)) | |
8b166553 | 239 | return -EFAULT; |
b920de1b DH |
240 | /* this is mov $,d0; syscall 0 */ |
241 | if (__put_user(0x2c, (char *)(frame->retcode + 0)) || | |
242 | __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) || | |
243 | __put_user(0x00, (char *)(frame->retcode + 2)) || | |
244 | __put_user(0xf0, (char *)(frame->retcode + 3)) || | |
245 | __put_user(0xe0, (char *)(frame->retcode + 4))) | |
8b166553 | 246 | return -EFAULT; |
b920de1b DH |
247 | flush_icache_range((unsigned long) frame->retcode, |
248 | (unsigned long) frame->retcode + 5); | |
249 | } | |
250 | ||
251 | /* set up registers for signal handler */ | |
252 | regs->sp = (unsigned long) frame; | |
8b166553 | 253 | regs->pc = (unsigned long) ksig->ka.sa.sa_handler; |
b920de1b DH |
254 | regs->d0 = sig; |
255 | regs->d1 = (unsigned long) &frame->sc; | |
256 | ||
b920de1b DH |
257 | #if DEBUG_SIG |
258 | printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n", | |
259 | sig, current->comm, current->pid, frame, regs->pc, | |
260 | frame->pretcode); | |
261 | #endif | |
262 | ||
263 | return 0; | |
b920de1b DH |
264 | } |
265 | ||
266 | /* | |
267 | * set up a realtime signal frame | |
268 | */ | |
8b166553 RW |
269 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
270 | struct pt_regs *regs) | |
b920de1b DH |
271 | { |
272 | struct rt_sigframe __user *frame; | |
8b166553 | 273 | int rsig, sig = ksig->sig; |
b920de1b | 274 | |
a8040ff8 | 275 | frame = get_sigframe(ksig, regs, sizeof(*frame)); |
b920de1b DH |
276 | |
277 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
8b166553 | 278 | return -EFAULT; |
b920de1b DH |
279 | |
280 | rsig = sig; | |
281 | if (sig < 32 && | |
282 | current_thread_info()->exec_domain && | |
283 | current_thread_info()->exec_domain->signal_invmap) | |
284 | rsig = current_thread_info()->exec_domain->signal_invmap[sig]; | |
285 | ||
286 | if (__put_user(rsig, &frame->sig) || | |
287 | __put_user(&frame->info, &frame->pinfo) || | |
288 | __put_user(&frame->uc, &frame->puc) || | |
8b166553 RW |
289 | copy_siginfo_to_user(&frame->info, &ksig->info)) |
290 | return -EFAULT; | |
b920de1b DH |
291 | |
292 | /* create the ucontext. */ | |
293 | if (__put_user(0, &frame->uc.uc_flags) || | |
294 | __put_user(0, &frame->uc.uc_link) || | |
b30c7d50 | 295 | __save_altstack(&frame->uc.uc_stack, regs->sp) || |
b920de1b DH |
296 | setup_sigcontext(&frame->uc.uc_mcontext, |
297 | &frame->fpuctx, regs, set->sig[0]) || | |
298 | __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set))) | |
8b166553 | 299 | return -EFAULT; |
b920de1b DH |
300 | |
301 | /* set up to return from userspace. If provided, use a stub already in | |
302 | * userspace */ | |
8b166553 RW |
303 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
304 | if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode)) | |
305 | return -EFAULT; | |
306 | ||
b920de1b DH |
307 | } else { |
308 | if (__put_user((void(*)(void))frame->retcode, | |
309 | &frame->pretcode) || | |
310 | /* This is mov $,d0; syscall 0 */ | |
311 | __put_user(0x2c, (char *)(frame->retcode + 0)) || | |
312 | __put_user(__NR_rt_sigreturn, | |
313 | (char *)(frame->retcode + 1)) || | |
314 | __put_user(0x00, (char *)(frame->retcode + 2)) || | |
315 | __put_user(0xf0, (char *)(frame->retcode + 3)) || | |
316 | __put_user(0xe0, (char *)(frame->retcode + 4))) | |
8b166553 | 317 | return -EFAULT; |
b920de1b DH |
318 | |
319 | flush_icache_range((u_long) frame->retcode, | |
320 | (u_long) frame->retcode + 5); | |
321 | } | |
322 | ||
323 | /* Set up registers for signal handler */ | |
324 | regs->sp = (unsigned long) frame; | |
8b166553 | 325 | regs->pc = (unsigned long) ksig->ka.sa.sa_handler; |
b920de1b DH |
326 | regs->d0 = sig; |
327 | regs->d1 = (long) &frame->info; | |
328 | ||
b920de1b DH |
329 | #if DEBUG_SIG |
330 | printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n", | |
331 | sig, current->comm, current->pid, frame, regs->pc, | |
332 | frame->pretcode); | |
333 | #endif | |
334 | ||
335 | return 0; | |
b920de1b DH |
336 | } |
337 | ||
00cbf608 AV |
338 | static inline void stepback(struct pt_regs *regs) |
339 | { | |
340 | regs->pc -= 2; | |
341 | regs->orig_d0 = -1; | |
342 | } | |
343 | ||
b920de1b DH |
344 | /* |
345 | * handle the actual delivery of a signal to userspace | |
346 | */ | |
8b166553 | 347 | static int handle_signal(struct ksignal *ksig, struct pt_regs *regs) |
b920de1b | 348 | { |
b7f9a11a | 349 | sigset_t *oldset = sigmask_to_save(); |
b920de1b DH |
350 | int ret; |
351 | ||
352 | /* Are we from a system call? */ | |
353 | if (regs->orig_d0 >= 0) { | |
354 | /* If so, check system call restarting.. */ | |
355 | switch (regs->d0) { | |
356 | case -ERESTART_RESTARTBLOCK: | |
357 | case -ERESTARTNOHAND: | |
358 | regs->d0 = -EINTR; | |
359 | break; | |
360 | ||
361 | case -ERESTARTSYS: | |
8b166553 | 362 | if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { |
b920de1b DH |
363 | regs->d0 = -EINTR; |
364 | break; | |
365 | } | |
366 | ||
367 | /* fallthrough */ | |
368 | case -ERESTARTNOINTR: | |
369 | regs->d0 = regs->orig_d0; | |
00cbf608 | 370 | stepback(regs); |
b920de1b DH |
371 | } |
372 | } | |
373 | ||
374 | /* Set up the stack frame */ | |
8b166553 RW |
375 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
376 | ret = setup_rt_frame(ksig, oldset, regs); | |
b920de1b | 377 | else |
8b166553 | 378 | ret = setup_frame(ksig, oldset, regs); |
b920de1b | 379 | |
8b166553 | 380 | signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); |
b45f9330 | 381 | return 0; |
b920de1b DH |
382 | } |
383 | ||
384 | /* | |
385 | * handle a potential signal | |
386 | */ | |
387 | static void do_signal(struct pt_regs *regs) | |
388 | { | |
8b166553 | 389 | struct ksignal ksig; |
b920de1b | 390 | |
8b166553 RW |
391 | if (get_signal(&ksig)) { |
392 | handle_signal(&ksig, regs); | |
b920de1b DH |
393 | return; |
394 | } | |
395 | ||
396 | /* did we come from a system call? */ | |
397 | if (regs->orig_d0 >= 0) { | |
398 | /* restart the system call - no handlers present */ | |
399 | switch (regs->d0) { | |
400 | case -ERESTARTNOHAND: | |
401 | case -ERESTARTSYS: | |
402 | case -ERESTARTNOINTR: | |
403 | regs->d0 = regs->orig_d0; | |
00cbf608 | 404 | stepback(regs); |
b920de1b DH |
405 | break; |
406 | ||
407 | case -ERESTART_RESTARTBLOCK: | |
408 | regs->d0 = __NR_restart_syscall; | |
00cbf608 | 409 | stepback(regs); |
b920de1b DH |
410 | break; |
411 | } | |
412 | } | |
413 | ||
414 | /* if there's no signal to deliver, we just put the saved sigmask | |
415 | * back */ | |
51a7b448 | 416 | restore_saved_sigmask(); |
b920de1b DH |
417 | } |
418 | ||
419 | /* | |
420 | * notification of userspace execution resumption | |
421 | * - triggered by current->work.notify_resume | |
422 | */ | |
423 | asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags) | |
424 | { | |
425 | /* Pending single-step? */ | |
426 | if (thread_info_flags & _TIF_SINGLESTEP) { | |
427 | #ifndef CONFIG_MN10300_USING_JTAG | |
428 | regs->epsw |= EPSW_T; | |
429 | clear_thread_flag(TIF_SINGLESTEP); | |
430 | #else | |
431 | BUG(); /* no h/w single-step if using JTAG unit */ | |
432 | #endif | |
433 | } | |
434 | ||
435 | /* deal with pending signal delivery */ | |
6fd84c08 | 436 | if (thread_info_flags & _TIF_SIGPENDING) |
b920de1b | 437 | do_signal(regs); |
5d289964 DH |
438 | |
439 | if (thread_info_flags & _TIF_NOTIFY_RESUME) { | |
440 | clear_thread_flag(TIF_NOTIFY_RESUME); | |
7c7fcf76 | 441 | tracehook_notify_resume(current_frame()); |
5d289964 | 442 | } |
b920de1b | 443 | } |