blob: d48cfc726b68d9e2fbe3b7968c2283dd476e71de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/signal.c
3 *
Heiko Carstens54dfe5d2006-02-01 03:06:38 -08004 * Copyright (C) IBM Corp. 1999,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6 *
7 * Based on Intel version
8 *
9 * Copyright (C) 1991, 1992 Linus Torvalds
10 *
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
12 */
13
14#include <linux/config.h>
15#include <linux/sched.h>
16#include <linux/mm.h>
17#include <linux/smp.h>
18#include <linux/smp_lock.h>
19#include <linux/kernel.h>
20#include <linux/signal.h>
21#include <linux/errno.h>
22#include <linux/wait.h>
23#include <linux/ptrace.h>
24#include <linux/unistd.h>
25#include <linux/stddef.h>
26#include <linux/tty.h>
27#include <linux/personality.h>
28#include <linux/binfmts.h>
29#include <asm/ucontext.h>
30#include <asm/uaccess.h>
31#include <asm/lowcore.h>
32
33#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34
35
36typedef struct
37{
38 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
39 struct sigcontext sc;
40 _sigregs sregs;
41 int signo;
42 __u8 retcode[S390_SYSCALL_SIZE];
43} sigframe;
44
45typedef struct
46{
47 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
48 __u8 retcode[S390_SYSCALL_SIZE];
49 struct siginfo info;
50 struct ucontext uc;
51} rt_sigframe;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * Atomically swap in the new signal mask, and wait for a signal.
55 */
56asmlinkage int
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080057sys_sigsuspend(int history0, int history1, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 mask &= _BLOCKABLE;
60 spin_lock_irq(&current->sighand->siglock);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080061 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 siginitset(&current->blocked, mask);
63 recalc_sigpending();
64 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080066 current->state = TASK_INTERRUPTIBLE;
67 schedule();
68 set_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080070 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73asmlinkage long
74sys_sigaction(int sig, const struct old_sigaction __user *act,
75 struct old_sigaction __user *oact)
76{
77 struct k_sigaction new_ka, old_ka;
78 int ret;
79
80 if (act) {
81 old_sigset_t mask;
82 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
83 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
84 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
85 return -EFAULT;
86 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
87 __get_user(mask, &act->sa_mask);
88 siginitset(&new_ka.sa.sa_mask, mask);
89 }
90
91 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
92
93 if (!ret && oact) {
94 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
95 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
96 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
97 return -EFAULT;
98 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
99 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
100 }
101
102 return ret;
103}
104
105asmlinkage long
106sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
107 struct pt_regs *regs)
108{
109 return do_sigaltstack(uss, uoss, regs->gprs[15]);
110}
111
112
113
114/* Returns non-zero on fault. */
115static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
116{
117 unsigned long old_mask = regs->psw.mask;
118 int err;
119
120 save_access_regs(current->thread.acrs);
121
122 /* Copy a 'clean' PSW mask to the user to avoid leaking
123 information about whether PER is currently on. */
124 regs->psw.mask = PSW_MASK_MERGE(PSW_USER_BITS, regs->psw.mask);
125 err = __copy_to_user(&sregs->regs.psw, &regs->psw,
126 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
127 regs->psw.mask = old_mask;
128 if (err != 0)
129 return err;
130 err = __copy_to_user(&sregs->regs.acrs, current->thread.acrs,
131 sizeof(sregs->regs.acrs));
132 if (err != 0)
133 return err;
134 /*
135 * We have to store the fp registers to current->thread.fp_regs
136 * to merge them with the emulated registers.
137 */
138 save_fp_regs(&current->thread.fp_regs);
139 return __copy_to_user(&sregs->fpregs, &current->thread.fp_regs,
140 sizeof(s390_fp_regs));
141}
142
143/* Returns positive number on error */
144static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
145{
146 unsigned long old_mask = regs->psw.mask;
147 int err;
148
149 /* Alwys make any pending restarted system call return -EINTR */
150 current_thread_info()->restart_block.fn = do_no_restart_syscall;
151
152 err = __copy_from_user(&regs->psw, &sregs->regs.psw,
153 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
154 regs->psw.mask = PSW_MASK_MERGE(old_mask, regs->psw.mask);
155 regs->psw.addr |= PSW_ADDR_AMODE;
156 if (err)
157 return err;
158 err = __copy_from_user(&current->thread.acrs, &sregs->regs.acrs,
159 sizeof(sregs->regs.acrs));
160 if (err)
161 return err;
162 restore_access_regs(current->thread.acrs);
163
164 err = __copy_from_user(&current->thread.fp_regs, &sregs->fpregs,
165 sizeof(s390_fp_regs));
166 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
167 if (err)
168 return err;
169
170 restore_fp_regs(&current->thread.fp_regs);
171 regs->trap = -1; /* disable syscall checks */
172 return 0;
173}
174
175asmlinkage long sys_sigreturn(struct pt_regs *regs)
176{
177 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
178 sigset_t set;
179
180 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
181 goto badframe;
182 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
183 goto badframe;
184
185 sigdelsetmask(&set, ~_BLOCKABLE);
186 spin_lock_irq(&current->sighand->siglock);
187 current->blocked = set;
188 recalc_sigpending();
189 spin_unlock_irq(&current->sighand->siglock);
190
191 if (restore_sigregs(regs, &frame->sregs))
192 goto badframe;
193
194 return regs->gprs[2];
195
196badframe:
197 force_sig(SIGSEGV, current);
198 return 0;
199}
200
201asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
202{
203 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
204 sigset_t set;
205
206 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
207 goto badframe;
208 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
209 goto badframe;
210
211 sigdelsetmask(&set, ~_BLOCKABLE);
212 spin_lock_irq(&current->sighand->siglock);
213 current->blocked = set;
214 recalc_sigpending();
215 spin_unlock_irq(&current->sighand->siglock);
216
217 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
218 goto badframe;
219
Cedric Le Goater4e3df372006-01-06 00:19:10 -0800220 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
221 regs->gprs[15]) == -EFAULT)
222 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return regs->gprs[2];
224
225badframe:
226 force_sig(SIGSEGV, current);
227 return 0;
228}
229
230/*
231 * Set up a signal frame.
232 */
233
234
235/*
236 * Determine which stack to use..
237 */
238static inline void __user *
239get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
240{
241 unsigned long sp;
242
243 /* Default to using normal stack */
244 sp = regs->gprs[15];
245
246 /* This is the X/Open sanctioned signal stack switching. */
247 if (ka->sa.sa_flags & SA_ONSTACK) {
248 if (! sas_ss_flags(sp))
249 sp = current->sas_ss_sp + current->sas_ss_size;
250 }
251
252 /* This is the legacy signal stack switching. */
253 else if (!user_mode(regs) &&
254 !(ka->sa.sa_flags & SA_RESTORER) &&
255 ka->sa.sa_restorer) {
256 sp = (unsigned long) ka->sa.sa_restorer;
257 }
258
259 return (void __user *)((sp - frame_size) & -8ul);
260}
261
262static inline int map_signal(int sig)
263{
264 if (current_thread_info()->exec_domain
265 && current_thread_info()->exec_domain->signal_invmap
266 && sig < 32)
267 return current_thread_info()->exec_domain->signal_invmap[sig];
268 else
269 return sig;
270}
271
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800272static int setup_frame(int sig, struct k_sigaction *ka,
273 sigset_t *set, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 sigframe __user *frame;
276
277 frame = get_sigframe(ka, regs, sizeof(sigframe));
278 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
279 goto give_sigsegv;
280
281 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
282 goto give_sigsegv;
283
284 if (save_sigregs(regs, &frame->sregs))
285 goto give_sigsegv;
286 if (__put_user(&frame->sregs, &frame->sc.sregs))
287 goto give_sigsegv;
288
289 /* Set up to return from userspace. If provided, use a stub
290 already in userspace. */
291 if (ka->sa.sa_flags & SA_RESTORER) {
292 regs->gprs[14] = (unsigned long)
293 ka->sa.sa_restorer | PSW_ADDR_AMODE;
294 } else {
295 regs->gprs[14] = (unsigned long)
296 frame->retcode | PSW_ADDR_AMODE;
297 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
298 (u16 __user *)(frame->retcode)))
299 goto give_sigsegv;
300 }
301
302 /* Set up backchain. */
303 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
304 goto give_sigsegv;
305
306 /* Set up registers for signal handler */
307 regs->gprs[15] = (unsigned long) frame;
308 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
309
310 regs->gprs[2] = map_signal(sig);
311 regs->gprs[3] = (unsigned long) &frame->sc;
312
313 /* We forgot to include these in the sigcontext.
314 To avoid breaking binary compatibility, they are passed as args. */
315 regs->gprs[4] = current->thread.trap_no;
316 regs->gprs[5] = current->thread.prot_addr;
317
318 /* Place signal number on stack to allow backtrace from handler. */
319 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
320 goto give_sigsegv;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323give_sigsegv:
324 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800325 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800328static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 sigset_t *set, struct pt_regs * regs)
330{
331 int err = 0;
332 rt_sigframe __user *frame;
333
334 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
335 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
336 goto give_sigsegv;
337
338 if (copy_siginfo_to_user(&frame->info, info))
339 goto give_sigsegv;
340
341 /* Create the ucontext. */
342 err |= __put_user(0, &frame->uc.uc_flags);
Al Viroc2814472005-09-29 00:16:02 +0100343 err |= __put_user(NULL, &frame->uc.uc_link);
344 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 err |= __put_user(sas_ss_flags(regs->gprs[15]),
346 &frame->uc.uc_stack.ss_flags);
347 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
348 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
349 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
350 if (err)
351 goto give_sigsegv;
352
353 /* Set up to return from userspace. If provided, use a stub
354 already in userspace. */
355 if (ka->sa.sa_flags & SA_RESTORER) {
356 regs->gprs[14] = (unsigned long)
357 ka->sa.sa_restorer | PSW_ADDR_AMODE;
358 } else {
359 regs->gprs[14] = (unsigned long)
360 frame->retcode | PSW_ADDR_AMODE;
Heiko Carstensb44df332006-05-01 12:16:15 -0700361 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
362 (u16 __user *)(frame->retcode)))
363 goto give_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 /* Set up backchain. */
367 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
368 goto give_sigsegv;
369
370 /* Set up registers for signal handler */
371 regs->gprs[15] = (unsigned long) frame;
372 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
373
374 regs->gprs[2] = map_signal(sig);
375 regs->gprs[3] = (unsigned long) &frame->info;
376 regs->gprs[4] = (unsigned long) &frame->uc;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379give_sigsegv:
380 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800381 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/*
385 * OK, we're invoking a handler
386 */
387
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800388static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389handle_signal(unsigned long sig, struct k_sigaction *ka,
390 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
391{
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800392 int ret;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* Set up the stack frame */
395 if (ka->sa.sa_flags & SA_SIGINFO)
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800396 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 else
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800398 ret = setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800400 if (ret == 0) {
401 spin_lock_irq(&current->sighand->siglock);
402 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
403 if (!(ka->sa.sa_flags & SA_NODEFER))
404 sigaddset(&current->blocked,sig);
405 recalc_sigpending();
406 spin_unlock_irq(&current->sighand->siglock);
407 }
408
409 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412/*
413 * Note that 'init' is a special process: it doesn't get signals it doesn't
414 * want to handle. Thus you cannot kill init even with a SIGKILL even by
415 * mistake.
416 *
417 * Note that we go through the signals twice: once to check the signals that
418 * the kernel can handle, and then we build all the user-level signal handling
419 * stack-frames in one go after that.
420 */
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800421void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
424 siginfo_t info;
425 int signr;
426 struct k_sigaction ka;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800427 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
430 * We want the common case to go fast, which
431 * is why we may in certain cases get here from
432 * kernel mode. Just return without doing anything
433 * if so.
434 */
435 if (!user_mode(regs))
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800438 if (test_thread_flag(TIF_RESTORE_SIGMASK))
439 oldset = &current->saved_sigmask;
440 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 oldset = &current->blocked;
442
443 /* Are we from a system call? */
444 if (regs->trap == __LC_SVC_OLD_PSW) {
445 continue_addr = regs->psw.addr;
446 restart_addr = continue_addr - regs->ilc;
447 retval = regs->gprs[2];
448
449 /* Prepare for system call restart. We do this here so that a
450 debugger will see the already changed PSW. */
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800451 switch (retval) {
452 case -ERESTARTNOHAND:
453 case -ERESTARTSYS:
454 case -ERESTARTNOINTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 regs->gprs[2] = regs->orig_gpr2;
456 regs->psw.addr = restart_addr;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800457 break;
458 case -ERESTART_RESTARTBLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 regs->gprs[2] = -EINTR;
460 }
461 }
462
463 /* Get signal to deliver. When running under ptrace, at this point
464 the debugger may change all our registers ... */
465 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
466
467 /* Depending on the signal settings we may need to revert the
468 decision to restart the system call. */
469 if (signr > 0 && regs->psw.addr == restart_addr) {
470 if (retval == -ERESTARTNOHAND
471 || (retval == -ERESTARTSYS
472 && !(current->sighand->action[signr-1].sa.sa_flags
473 & SA_RESTART))) {
474 regs->gprs[2] = -EINTR;
475 regs->psw.addr = continue_addr;
476 }
477 }
478
479 if (signr > 0) {
480 /* Whee! Actually deliver the signal. */
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800481#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (test_thread_flag(TIF_31BIT)) {
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800483 extern int handle_signal32(unsigned long sig,
484 struct k_sigaction *ka,
485 siginfo_t *info,
486 sigset_t *oldset,
487 struct pt_regs *regs);
488 if (handle_signal32(
489 signr, &ka, &info, oldset, regs) == 0) {
490 if (test_thread_flag(TIF_RESTORE_SIGMASK))
491 clear_thread_flag(TIF_RESTORE_SIGMASK);
492 }
493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495#endif
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800496 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
497 /*
498 * A signal was successfully delivered; the saved
499 * sigmask will have been stored in the signal frame,
500 * and will be restored by sigreturn, so we can simply
501 * clear the TIF_RESTORE_SIGMASK flag.
502 */
503 if (test_thread_flag(TIF_RESTORE_SIGMASK))
504 clear_thread_flag(TIF_RESTORE_SIGMASK);
505 }
506 return;
507 }
508
509 /*
510 * If there's no signal to deliver, we just put the saved sigmask back.
511 */
512 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
513 clear_thread_flag(TIF_RESTORE_SIGMASK);
514 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 /* Restart a different system call. */
518 if (retval == -ERESTART_RESTARTBLOCK
519 && regs->psw.addr == continue_addr) {
520 regs->gprs[2] = __NR_restart_syscall;
521 set_thread_flag(TIF_RESTART_SVC);
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}