Merge branch 'x86-debug-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / arch / mn10300 / kernel / signal.c
CommitLineData
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 * atomically swap in the new signal mask, and wait for a signal.
36 */
37asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
38{
68f3f16d
AV
39 sigset_t blocked;
40 siginitset(&blocked, mask);
41 return sigsuspend(&blocked);
b920de1b
DH
42}
43
44/*
45 * set signal action syscall
46 */
47asmlinkage long sys_sigaction(int sig,
48 const struct old_sigaction __user *act,
49 struct old_sigaction __user *oact)
50{
51 struct k_sigaction new_ka, old_ka;
52 int ret;
53
54 if (act) {
55 old_sigset_t mask;
56 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
57 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
60bdb72e
AV
58 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
59 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
60 __get_user(mask, &act->sa_mask))
b920de1b 61 return -EFAULT;
b920de1b
DH
62 siginitset(&new_ka.sa.sa_mask, mask);
63 }
64
65 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
66
67 if (!ret && oact) {
68 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
69 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
60bdb72e
AV
70 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
71 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
72 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
b920de1b 73 return -EFAULT;
b920de1b
DH
74 }
75
76 return ret;
77}
78
79/*
80 * set alternate signal stack syscall
81 */
82asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
83{
7c7fcf76 84 return do_sigaltstack(uss, uoss, current_frame()->sp);
b920de1b
DH
85}
86
87/*
88 * do a signal return; undo the signal stack.
89 */
90static int restore_sigcontext(struct pt_regs *regs,
91 struct sigcontext __user *sc, long *_d0)
92{
93 unsigned int err = 0;
94
c05628b4
AV
95 /* Always make any pending restarted system calls return -EINTR */
96 current_thread_info()->restart_block.fn = do_no_restart_syscall;
97
b920de1b
DH
98 if (is_using_fpu(current))
99 fpu_kill_state(current);
100
101#define COPY(x) err |= __get_user(regs->x, &sc->x)
102 COPY(d1); COPY(d2); COPY(d3);
103 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
104 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
105 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
106 COPY(lar); COPY(lir);
107 COPY(mdr); COPY(mdrq);
108 COPY(mcvf); COPY(mcrl); COPY(mcrh);
109 COPY(sp); COPY(pc);
110#undef COPY
111
112 {
113 unsigned int tmpflags;
114#ifndef CONFIG_MN10300_USING_JTAG
115#define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
116 EPSW_T | EPSW_nAR)
117#else
118#define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
119 EPSW_nAR)
120#endif
121 err |= __get_user(tmpflags, &sc->epsw);
122 regs->epsw = (regs->epsw & ~USER_EPSW) |
123 (tmpflags & USER_EPSW);
124 regs->orig_d0 = -1; /* disable syscall checks */
125 }
126
127 {
128 struct fpucontext *buf;
129 err |= __get_user(buf, &sc->fpucontext);
130 if (buf) {
131 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
132 goto badframe;
133 err |= fpu_restore_sigcontext(buf);
134 }
135 }
136
137 err |= __get_user(*_d0, &sc->d0);
138 return err;
139
140badframe:
141 return 1;
142}
143
144/*
145 * standard signal return syscall
146 */
147asmlinkage long sys_sigreturn(void)
148{
7c7fcf76 149 struct sigframe __user *frame;
b920de1b
DH
150 sigset_t set;
151 long d0;
152
7c7fcf76 153 frame = (struct sigframe __user *) current_frame()->sp;
b920de1b
DH
154 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
155 goto badframe;
156 if (__get_user(set.sig[0], &frame->sc.oldmask))
157 goto badframe;
158
159 if (_NSIG_WORDS > 1 &&
160 __copy_from_user(&set.sig[1], &frame->extramask,
161 sizeof(frame->extramask)))
162 goto badframe;
163
00f35785 164 set_current_blocked(&set);
b920de1b 165
7c7fcf76 166 if (restore_sigcontext(current_frame(), &frame->sc, &d0))
b920de1b
DH
167 goto badframe;
168
169 return d0;
170
171badframe:
172 force_sig(SIGSEGV, current);
173 return 0;
174}
175
176/*
177 * realtime signal return syscall
178 */
179asmlinkage long sys_rt_sigreturn(void)
180{
7c7fcf76 181 struct rt_sigframe __user *frame;
b920de1b 182 sigset_t set;
7c7fcf76 183 long d0;
b920de1b 184
7c7fcf76 185 frame = (struct rt_sigframe __user *) current_frame()->sp;
b920de1b
DH
186 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
187 goto badframe;
188 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
189 goto badframe;
190
00f35785 191 set_current_blocked(&set);
b920de1b 192
7c7fcf76 193 if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
b920de1b
DH
194 goto badframe;
195
7c7fcf76
DH
196 if (do_sigaltstack(&frame->uc.uc_stack, NULL, current_frame()->sp) ==
197 -EFAULT)
b920de1b
DH
198 goto badframe;
199
200 return d0;
201
202badframe:
203 force_sig(SIGSEGV, current);
204 return 0;
205}
206
207/*
208 * store the userspace context into a signal frame
209 */
210static int setup_sigcontext(struct sigcontext __user *sc,
211 struct fpucontext *fpuctx,
212 struct pt_regs *regs,
213 unsigned long mask)
214{
215 int tmp, err = 0;
216
217#define COPY(x) err |= __put_user(regs->x, &sc->x)
218 COPY(d0); COPY(d1); COPY(d2); COPY(d3);
219 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
220 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
221 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
222 COPY(lar); COPY(lir);
223 COPY(mdr); COPY(mdrq);
224 COPY(mcvf); COPY(mcrl); COPY(mcrh);
225 COPY(sp); COPY(epsw); COPY(pc);
226#undef COPY
227
228 tmp = fpu_setup_sigcontext(fpuctx);
229 if (tmp < 0)
230 err = 1;
231 else
232 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
233
234 /* non-iBCS2 extensions.. */
235 err |= __put_user(mask, &sc->oldmask);
236
237 return err;
238}
239
240/*
241 * determine which stack to use..
242 */
243static inline void __user *get_sigframe(struct k_sigaction *ka,
244 struct pt_regs *regs,
245 size_t frame_size)
246{
247 unsigned long sp;
248
249 /* default to using normal stack */
250 sp = regs->sp;
251
252 /* this is the X/Open sanctioned signal stack switching. */
253 if (ka->sa.sa_flags & SA_ONSTACK) {
4c03ee73 254 if (sas_ss_flags(sp) == 0)
b920de1b
DH
255 sp = current->sas_ss_sp + current->sas_ss_size;
256 }
257
258 return (void __user *) ((sp - frame_size) & ~7UL);
259}
260
261/*
262 * set up a normal signal frame
263 */
264static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
265 struct pt_regs *regs)
266{
267 struct sigframe __user *frame;
268 int rsig;
269
270 frame = get_sigframe(ka, regs, sizeof(*frame));
271
272 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
273 goto give_sigsegv;
274
275 rsig = sig;
276 if (sig < 32 &&
277 current_thread_info()->exec_domain &&
278 current_thread_info()->exec_domain->signal_invmap)
279 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
280
281 if (__put_user(rsig, &frame->sig) < 0 ||
282 __put_user(&frame->sc, &frame->psc) < 0)
283 goto give_sigsegv;
284
285 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
286 goto give_sigsegv;
287
288 if (_NSIG_WORDS > 1) {
289 if (__copy_to_user(frame->extramask, &set->sig[1],
290 sizeof(frame->extramask)))
291 goto give_sigsegv;
292 }
293
294 /* set up to return from userspace. If provided, use a stub already in
295 * userspace */
296 if (ka->sa.sa_flags & SA_RESTORER) {
297 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
298 goto give_sigsegv;
299 } else {
300 if (__put_user((void (*)(void))frame->retcode,
301 &frame->pretcode))
302 goto give_sigsegv;
303 /* this is mov $,d0; syscall 0 */
304 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
305 __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
306 __put_user(0x00, (char *)(frame->retcode + 2)) ||
307 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
308 __put_user(0xe0, (char *)(frame->retcode + 4)))
309 goto give_sigsegv;
310 flush_icache_range((unsigned long) frame->retcode,
311 (unsigned long) frame->retcode + 5);
312 }
313
314 /* set up registers for signal handler */
315 regs->sp = (unsigned long) frame;
316 regs->pc = (unsigned long) ka->sa.sa_handler;
317 regs->d0 = sig;
318 regs->d1 = (unsigned long) &frame->sc;
319
b920de1b
DH
320 /* the tracer may want to single-step inside the handler */
321 if (test_thread_flag(TIF_SINGLESTEP))
322 ptrace_notify(SIGTRAP);
323
324#if DEBUG_SIG
325 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
326 sig, current->comm, current->pid, frame, regs->pc,
327 frame->pretcode);
328#endif
329
330 return 0;
331
332give_sigsegv:
e46924d2 333 force_sigsegv(sig, current);
b920de1b
DH
334 return -EFAULT;
335}
336
337/*
338 * set up a realtime signal frame
339 */
340static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
341 sigset_t *set, struct pt_regs *regs)
342{
343 struct rt_sigframe __user *frame;
344 int rsig;
345
346 frame = get_sigframe(ka, regs, sizeof(*frame));
347
348 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
349 goto give_sigsegv;
350
351 rsig = sig;
352 if (sig < 32 &&
353 current_thread_info()->exec_domain &&
354 current_thread_info()->exec_domain->signal_invmap)
355 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
356
357 if (__put_user(rsig, &frame->sig) ||
358 __put_user(&frame->info, &frame->pinfo) ||
359 __put_user(&frame->uc, &frame->puc) ||
360 copy_siginfo_to_user(&frame->info, info))
361 goto give_sigsegv;
362
363 /* create the ucontext. */
364 if (__put_user(0, &frame->uc.uc_flags) ||
365 __put_user(0, &frame->uc.uc_link) ||
366 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
367 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
368 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
369 setup_sigcontext(&frame->uc.uc_mcontext,
370 &frame->fpuctx, regs, set->sig[0]) ||
371 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
372 goto give_sigsegv;
373
374 /* set up to return from userspace. If provided, use a stub already in
375 * userspace */
376 if (ka->sa.sa_flags & SA_RESTORER) {
377 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
378 goto give_sigsegv;
379 } else {
380 if (__put_user((void(*)(void))frame->retcode,
381 &frame->pretcode) ||
382 /* This is mov $,d0; syscall 0 */
383 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
384 __put_user(__NR_rt_sigreturn,
385 (char *)(frame->retcode + 1)) ||
386 __put_user(0x00, (char *)(frame->retcode + 2)) ||
387 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
388 __put_user(0xe0, (char *)(frame->retcode + 4)))
389 goto give_sigsegv;
390
391 flush_icache_range((u_long) frame->retcode,
392 (u_long) frame->retcode + 5);
393 }
394
395 /* Set up registers for signal handler */
396 regs->sp = (unsigned long) frame;
397 regs->pc = (unsigned long) ka->sa.sa_handler;
398 regs->d0 = sig;
399 regs->d1 = (long) &frame->info;
400
b920de1b
DH
401 /* the tracer may want to single-step inside the handler */
402 if (test_thread_flag(TIF_SINGLESTEP))
403 ptrace_notify(SIGTRAP);
404
405#if DEBUG_SIG
406 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
407 sig, current->comm, current->pid, frame, regs->pc,
408 frame->pretcode);
409#endif
410
411 return 0;
412
413give_sigsegv:
e46924d2 414 force_sigsegv(sig, current);
b920de1b
DH
415 return -EFAULT;
416}
417
00cbf608
AV
418static inline void stepback(struct pt_regs *regs)
419{
420 regs->pc -= 2;
421 regs->orig_d0 = -1;
422}
423
b920de1b
DH
424/*
425 * handle the actual delivery of a signal to userspace
426 */
427static int handle_signal(int sig,
428 siginfo_t *info, struct k_sigaction *ka,
b7f9a11a 429 struct pt_regs *regs)
b920de1b 430{
b7f9a11a 431 sigset_t *oldset = sigmask_to_save();
b920de1b
DH
432 int ret;
433
434 /* Are we from a system call? */
435 if (regs->orig_d0 >= 0) {
436 /* If so, check system call restarting.. */
437 switch (regs->d0) {
438 case -ERESTART_RESTARTBLOCK:
439 case -ERESTARTNOHAND:
440 regs->d0 = -EINTR;
441 break;
442
443 case -ERESTARTSYS:
444 if (!(ka->sa.sa_flags & SA_RESTART)) {
445 regs->d0 = -EINTR;
446 break;
447 }
448
449 /* fallthrough */
450 case -ERESTARTNOINTR:
451 regs->d0 = regs->orig_d0;
00cbf608 452 stepback(regs);
b920de1b
DH
453 }
454 }
455
456 /* Set up the stack frame */
457 if (ka->sa.sa_flags & SA_SIGINFO)
458 ret = setup_rt_frame(sig, ka, info, oldset, regs);
459 else
460 ret = setup_frame(sig, ka, oldset, regs);
a610d6e6 461 if (ret)
b45f9330 462 return ret;
b920de1b 463
efee984c 464 signal_delivered(sig, info, ka, regs,
b45f9330
GU
465 test_thread_flag(TIF_SINGLESTEP));
466 return 0;
b920de1b
DH
467}
468
469/*
470 * handle a potential signal
471 */
472static void do_signal(struct pt_regs *regs)
473{
474 struct k_sigaction ka;
475 siginfo_t info;
b920de1b
DH
476 int signr;
477
478 /* we want the common case to go fast, which is why we may in certain
479 * cases get here from kernel mode */
480 if (!user_mode(regs))
481 return;
482
b920de1b
DH
483 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
484 if (signr > 0) {
b7f9a11a 485 if (handle_signal(signr, &info, &ka, regs) == 0) {
b920de1b
DH
486 }
487
488 return;
489 }
490
491 /* did we come from a system call? */
492 if (regs->orig_d0 >= 0) {
493 /* restart the system call - no handlers present */
494 switch (regs->d0) {
495 case -ERESTARTNOHAND:
496 case -ERESTARTSYS:
497 case -ERESTARTNOINTR:
498 regs->d0 = regs->orig_d0;
00cbf608 499 stepback(regs);
b920de1b
DH
500 break;
501
502 case -ERESTART_RESTARTBLOCK:
503 regs->d0 = __NR_restart_syscall;
00cbf608 504 stepback(regs);
b920de1b
DH
505 break;
506 }
507 }
508
509 /* if there's no signal to deliver, we just put the saved sigmask
510 * back */
51a7b448 511 restore_saved_sigmask();
b920de1b
DH
512}
513
514/*
515 * notification of userspace execution resumption
516 * - triggered by current->work.notify_resume
517 */
518asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
519{
520 /* Pending single-step? */
521 if (thread_info_flags & _TIF_SINGLESTEP) {
522#ifndef CONFIG_MN10300_USING_JTAG
523 regs->epsw |= EPSW_T;
524 clear_thread_flag(TIF_SINGLESTEP);
525#else
526 BUG(); /* no h/w single-step if using JTAG unit */
527#endif
528 }
529
530 /* deal with pending signal delivery */
6fd84c08 531 if (thread_info_flags & _TIF_SIGPENDING)
b920de1b 532 do_signal(regs);
5d289964
DH
533
534 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
535 clear_thread_flag(TIF_NOTIFY_RESUME);
7c7fcf76 536 tracehook_notify_resume(current_frame());
5d289964 537 }
b920de1b 538}
This page took 0.321786 seconds and 5 git commands to generate.