blob: dd05423f87a8425fb51ac446fbaa0f4acc9416f5 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
17#include <linux/smp_lock.h>
18#include <linux/kernel.h>
19#include <linux/signal.h>
20#include <linux/errno.h>
21#include <linux/wait.h>
22#include <linux/ptrace.h>
23#include <linux/unistd.h>
24#include <linux/stddef.h>
25#include <linux/tty.h>
26#include <linux/personality.h>
27#include <linux/binfmts.h>
28#include <asm/ucontext.h>
29#include <asm/uaccess.h>
30#include <asm/lowcore.h>
31
32#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
34
35typedef struct
36{
37 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
38 struct sigcontext sc;
39 _sigregs sregs;
40 int signo;
41 __u8 retcode[S390_SYSCALL_SIZE];
42} sigframe;
43
44typedef struct
45{
46 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
47 __u8 retcode[S390_SYSCALL_SIZE];
48 struct siginfo info;
49 struct ucontext uc;
50} rt_sigframe;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Atomically swap in the new signal mask, and wait for a signal.
54 */
55asmlinkage int
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080056sys_sigsuspend(int history0, int history1, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mask &= _BLOCKABLE;
59 spin_lock_irq(&current->sighand->siglock);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080060 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 siginitset(&current->blocked, mask);
62 recalc_sigpending();
63 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080065 current->state = TASK_INTERRUPTIBLE;
66 schedule();
67 set_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080069 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72asmlinkage long
73sys_sigaction(int sig, const struct old_sigaction __user *act,
74 struct old_sigaction __user *oact)
75{
76 struct k_sigaction new_ka, old_ka;
77 int ret;
78
79 if (act) {
80 old_sigset_t mask;
81 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
82 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
83 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
84 return -EFAULT;
85 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
86 __get_user(mask, &act->sa_mask);
87 siginitset(&new_ka.sa.sa_mask, mask);
88 }
89
90 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
91
92 if (!ret && oact) {
93 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
94 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
95 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
96 return -EFAULT;
97 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
98 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
99 }
100
101 return ret;
102}
103
104asmlinkage long
105sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
106 struct pt_regs *regs)
107{
108 return do_sigaltstack(uss, uoss, regs->gprs[15]);
109}
110
111
112
113/* Returns non-zero on fault. */
114static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
115{
116 unsigned long old_mask = regs->psw.mask;
117 int err;
118
119 save_access_regs(current->thread.acrs);
120
121 /* Copy a 'clean' PSW mask to the user to avoid leaking
122 information about whether PER is currently on. */
123 regs->psw.mask = PSW_MASK_MERGE(PSW_USER_BITS, regs->psw.mask);
124 err = __copy_to_user(&sregs->regs.psw, &regs->psw,
125 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
126 regs->psw.mask = old_mask;
127 if (err != 0)
128 return err;
129 err = __copy_to_user(&sregs->regs.acrs, current->thread.acrs,
130 sizeof(sregs->regs.acrs));
131 if (err != 0)
132 return err;
133 /*
134 * We have to store the fp registers to current->thread.fp_regs
135 * to merge them with the emulated registers.
136 */
137 save_fp_regs(&current->thread.fp_regs);
138 return __copy_to_user(&sregs->fpregs, &current->thread.fp_regs,
139 sizeof(s390_fp_regs));
140}
141
142/* Returns positive number on error */
143static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
144{
145 unsigned long old_mask = regs->psw.mask;
146 int err;
147
148 /* Alwys make any pending restarted system call return -EINTR */
149 current_thread_info()->restart_block.fn = do_no_restart_syscall;
150
151 err = __copy_from_user(&regs->psw, &sregs->regs.psw,
152 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
153 regs->psw.mask = PSW_MASK_MERGE(old_mask, regs->psw.mask);
154 regs->psw.addr |= PSW_ADDR_AMODE;
155 if (err)
156 return err;
157 err = __copy_from_user(&current->thread.acrs, &sregs->regs.acrs,
158 sizeof(sregs->regs.acrs));
159 if (err)
160 return err;
161 restore_access_regs(current->thread.acrs);
162
163 err = __copy_from_user(&current->thread.fp_regs, &sregs->fpregs,
164 sizeof(s390_fp_regs));
165 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
166 if (err)
167 return err;
168
169 restore_fp_regs(&current->thread.fp_regs);
170 regs->trap = -1; /* disable syscall checks */
171 return 0;
172}
173
174asmlinkage long sys_sigreturn(struct pt_regs *regs)
175{
176 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
177 sigset_t set;
178
179 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
180 goto badframe;
181 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
182 goto badframe;
183
184 sigdelsetmask(&set, ~_BLOCKABLE);
185 spin_lock_irq(&current->sighand->siglock);
186 current->blocked = set;
187 recalc_sigpending();
188 spin_unlock_irq(&current->sighand->siglock);
189
190 if (restore_sigregs(regs, &frame->sregs))
191 goto badframe;
192
193 return regs->gprs[2];
194
195badframe:
196 force_sig(SIGSEGV, current);
197 return 0;
198}
199
200asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
201{
202 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
203 sigset_t set;
204
205 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
206 goto badframe;
207 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
208 goto badframe;
209
210 sigdelsetmask(&set, ~_BLOCKABLE);
211 spin_lock_irq(&current->sighand->siglock);
212 current->blocked = set;
213 recalc_sigpending();
214 spin_unlock_irq(&current->sighand->siglock);
215
216 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
217 goto badframe;
218
Cedric Le Goater4e3df372006-01-06 00:19:10 -0800219 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
220 regs->gprs[15]) == -EFAULT)
221 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return regs->gprs[2];
223
224badframe:
225 force_sig(SIGSEGV, current);
226 return 0;
227}
228
229/*
230 * Set up a signal frame.
231 */
232
233
234/*
235 * Determine which stack to use..
236 */
237static inline void __user *
238get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
239{
240 unsigned long sp;
241
242 /* Default to using normal stack */
243 sp = regs->gprs[15];
244
245 /* This is the X/Open sanctioned signal stack switching. */
246 if (ka->sa.sa_flags & SA_ONSTACK) {
247 if (! sas_ss_flags(sp))
248 sp = current->sas_ss_sp + current->sas_ss_size;
249 }
250
251 /* This is the legacy signal stack switching. */
252 else if (!user_mode(regs) &&
253 !(ka->sa.sa_flags & SA_RESTORER) &&
254 ka->sa.sa_restorer) {
255 sp = (unsigned long) ka->sa.sa_restorer;
256 }
257
258 return (void __user *)((sp - frame_size) & -8ul);
259}
260
261static inline int map_signal(int sig)
262{
263 if (current_thread_info()->exec_domain
264 && current_thread_info()->exec_domain->signal_invmap
265 && sig < 32)
266 return current_thread_info()->exec_domain->signal_invmap[sig];
267 else
268 return sig;
269}
270
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800271static int setup_frame(int sig, struct k_sigaction *ka,
272 sigset_t *set, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 sigframe __user *frame;
275
276 frame = get_sigframe(ka, regs, sizeof(sigframe));
277 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
278 goto give_sigsegv;
279
280 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
281 goto give_sigsegv;
282
283 if (save_sigregs(regs, &frame->sregs))
284 goto give_sigsegv;
285 if (__put_user(&frame->sregs, &frame->sc.sregs))
286 goto give_sigsegv;
287
288 /* Set up to return from userspace. If provided, use a stub
289 already in userspace. */
290 if (ka->sa.sa_flags & SA_RESTORER) {
291 regs->gprs[14] = (unsigned long)
292 ka->sa.sa_restorer | PSW_ADDR_AMODE;
293 } else {
294 regs->gprs[14] = (unsigned long)
295 frame->retcode | PSW_ADDR_AMODE;
296 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
297 (u16 __user *)(frame->retcode)))
298 goto give_sigsegv;
299 }
300
301 /* Set up backchain. */
302 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
303 goto give_sigsegv;
304
305 /* Set up registers for signal handler */
306 regs->gprs[15] = (unsigned long) frame;
307 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
308
309 regs->gprs[2] = map_signal(sig);
310 regs->gprs[3] = (unsigned long) &frame->sc;
311
312 /* We forgot to include these in the sigcontext.
313 To avoid breaking binary compatibility, they are passed as args. */
314 regs->gprs[4] = current->thread.trap_no;
315 regs->gprs[5] = current->thread.prot_addr;
316
317 /* Place signal number on stack to allow backtrace from handler. */
318 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
319 goto give_sigsegv;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322give_sigsegv:
323 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800324 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800327static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sigset_t *set, struct pt_regs * regs)
329{
330 int err = 0;
331 rt_sigframe __user *frame;
332
333 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
334 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
335 goto give_sigsegv;
336
337 if (copy_siginfo_to_user(&frame->info, info))
338 goto give_sigsegv;
339
340 /* Create the ucontext. */
341 err |= __put_user(0, &frame->uc.uc_flags);
Al Viroc2814472005-09-29 00:16:02 +0100342 err |= __put_user(NULL, &frame->uc.uc_link);
343 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 err |= __put_user(sas_ss_flags(regs->gprs[15]),
345 &frame->uc.uc_stack.ss_flags);
346 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
347 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
348 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
349 if (err)
350 goto give_sigsegv;
351
352 /* Set up to return from userspace. If provided, use a stub
353 already in userspace. */
354 if (ka->sa.sa_flags & SA_RESTORER) {
355 regs->gprs[14] = (unsigned long)
356 ka->sa.sa_restorer | PSW_ADDR_AMODE;
357 } else {
358 regs->gprs[14] = (unsigned long)
359 frame->retcode | PSW_ADDR_AMODE;
Heiko Carstensb44df332006-05-01 12:16:15 -0700360 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
361 (u16 __user *)(frame->retcode)))
362 goto give_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
365 /* Set up backchain. */
366 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
367 goto give_sigsegv;
368
369 /* Set up registers for signal handler */
370 regs->gprs[15] = (unsigned long) frame;
371 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
372
373 regs->gprs[2] = map_signal(sig);
374 regs->gprs[3] = (unsigned long) &frame->info;
375 regs->gprs[4] = (unsigned long) &frame->uc;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378give_sigsegv:
379 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800380 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383/*
384 * OK, we're invoking a handler
385 */
386
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800387static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388handle_signal(unsigned long sig, struct k_sigaction *ka,
389 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
390{
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800391 int ret;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* Set up the stack frame */
394 if (ka->sa.sa_flags & SA_SIGINFO)
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800395 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 else
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800397 ret = setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800399 if (ret == 0) {
400 spin_lock_irq(&current->sighand->siglock);
401 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
402 if (!(ka->sa.sa_flags & SA_NODEFER))
403 sigaddset(&current->blocked,sig);
404 recalc_sigpending();
405 spin_unlock_irq(&current->sighand->siglock);
406 }
407
408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/*
412 * Note that 'init' is a special process: it doesn't get signals it doesn't
413 * want to handle. Thus you cannot kill init even with a SIGKILL even by
414 * mistake.
415 *
416 * Note that we go through the signals twice: once to check the signals that
417 * the kernel can handle, and then we build all the user-level signal handling
418 * stack-frames in one go after that.
419 */
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800420void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
423 siginfo_t info;
424 int signr;
425 struct k_sigaction ka;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800426 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /*
429 * We want the common case to go fast, which
430 * is why we may in certain cases get here from
431 * kernel mode. Just return without doing anything
432 * if so.
433 */
434 if (!user_mode(regs))
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800437 if (test_thread_flag(TIF_RESTORE_SIGMASK))
438 oldset = &current->saved_sigmask;
439 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 oldset = &current->blocked;
441
442 /* Are we from a system call? */
443 if (regs->trap == __LC_SVC_OLD_PSW) {
444 continue_addr = regs->psw.addr;
445 restart_addr = continue_addr - regs->ilc;
446 retval = regs->gprs[2];
447
448 /* Prepare for system call restart. We do this here so that a
449 debugger will see the already changed PSW. */
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800450 switch (retval) {
451 case -ERESTARTNOHAND:
452 case -ERESTARTSYS:
453 case -ERESTARTNOINTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 regs->gprs[2] = regs->orig_gpr2;
455 regs->psw.addr = restart_addr;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800456 break;
457 case -ERESTART_RESTARTBLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 regs->gprs[2] = -EINTR;
459 }
Heiko Carstens84270822006-09-20 15:58:54 +0200460 regs->trap = -1; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
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}