[PATCH] pcnet32: Use kcalloc instead of kmalloc and memset
[linux-2.6/libata-dev.git] / arch / s390 / kernel / signal.c
bloba887b686f27927bb6119d5cff813388e3c74ac62
1 /*
2 * arch/s390/kernel/signal.c
4 * Copyright (C) IBM Corp. 1999,2006
5 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
7 * Based on Intel version
8 *
9 * Copyright (C) 1991, 1992 Linus Torvalds
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 #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>
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35 typedef struct
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;
44 typedef struct
46 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
47 __u8 retcode[S390_SYSCALL_SIZE];
48 struct siginfo info;
49 struct ucontext uc;
50 } rt_sigframe;
53 * Atomically swap in the new signal mask, and wait for a signal.
55 asmlinkage int
56 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
58 mask &= _BLOCKABLE;
59 spin_lock_irq(&current->sighand->siglock);
60 current->saved_sigmask = current->blocked;
61 siginitset(&current->blocked, mask);
62 recalc_sigpending();
63 spin_unlock_irq(&current->sighand->siglock);
65 current->state = TASK_INTERRUPTIBLE;
66 schedule();
67 set_thread_flag(TIF_RESTORE_SIGMASK);
69 return -ERESTARTNOHAND;
72 asmlinkage long
73 sys_sigaction(int sig, const struct old_sigaction __user *act,
74 struct old_sigaction __user *oact)
76 struct k_sigaction new_ka, old_ka;
77 int ret;
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);
90 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
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);
101 return ret;
104 asmlinkage long
105 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
106 struct pt_regs *regs)
108 return do_sigaltstack(uss, uoss, regs->gprs[15]);
113 /* Returns non-zero on fault. */
114 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
116 unsigned long old_mask = regs->psw.mask;
117 int err;
119 save_access_regs(current->thread.acrs);
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;
134 * We have to store the fp registers to current->thread.fp_regs
135 * to merge them with the emulated registers.
137 save_fp_regs(&current->thread.fp_regs);
138 return __copy_to_user(&sregs->fpregs, &current->thread.fp_regs,
139 sizeof(s390_fp_regs));
142 /* Returns positive number on error */
143 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
145 unsigned long old_mask = regs->psw.mask;
146 int err;
148 /* Alwys make any pending restarted system call return -EINTR */
149 current_thread_info()->restart_block.fn = do_no_restart_syscall;
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);
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;
169 restore_fp_regs(&current->thread.fp_regs);
170 regs->trap = -1; /* disable syscall checks */
171 return 0;
174 asmlinkage long sys_sigreturn(struct pt_regs *regs)
176 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
177 sigset_t set;
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;
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);
190 if (restore_sigregs(regs, &frame->sregs))
191 goto badframe;
193 return regs->gprs[2];
195 badframe:
196 force_sig(SIGSEGV, current);
197 return 0;
200 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
202 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
203 sigset_t set;
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;
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);
216 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
217 goto badframe;
219 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
220 regs->gprs[15]) == -EFAULT)
221 goto badframe;
222 return regs->gprs[2];
224 badframe:
225 force_sig(SIGSEGV, current);
226 return 0;
230 * Set up a signal frame.
235 * Determine which stack to use..
237 static inline void __user *
238 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
240 unsigned long sp;
242 /* Default to using normal stack */
243 sp = regs->gprs[15];
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;
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;
258 return (void __user *)((sp - frame_size) & -8ul);
261 static inline int map_signal(int sig)
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;
271 static int setup_frame(int sig, struct k_sigaction *ka,
272 sigset_t *set, struct pt_regs * regs)
274 sigframe __user *frame;
276 frame = get_sigframe(ka, regs, sizeof(sigframe));
277 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
278 goto give_sigsegv;
280 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
281 goto give_sigsegv;
283 if (save_sigregs(regs, &frame->sregs))
284 goto give_sigsegv;
285 if (__put_user(&frame->sregs, &frame->sc.sregs))
286 goto give_sigsegv;
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;
301 /* Set up backchain. */
302 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
303 goto give_sigsegv;
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;
309 regs->gprs[2] = map_signal(sig);
310 regs->gprs[3] = (unsigned long) &frame->sc;
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;
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;
320 return 0;
322 give_sigsegv:
323 force_sigsegv(sig, current);
324 return -EFAULT;
327 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
328 sigset_t *set, struct pt_regs * regs)
330 int err = 0;
331 rt_sigframe __user *frame;
333 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
334 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
335 goto give_sigsegv;
337 if (copy_siginfo_to_user(&frame->info, info))
338 goto give_sigsegv;
340 /* Create the ucontext. */
341 err |= __put_user(0, &frame->uc.uc_flags);
342 err |= __put_user(NULL, &frame->uc.uc_link);
343 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
344 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;
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;
360 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
361 (u16 __user *)(frame->retcode)))
362 goto give_sigsegv;
365 /* Set up backchain. */
366 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
367 goto give_sigsegv;
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;
373 regs->gprs[2] = map_signal(sig);
374 regs->gprs[3] = (unsigned long) &frame->info;
375 regs->gprs[4] = (unsigned long) &frame->uc;
376 return 0;
378 give_sigsegv:
379 force_sigsegv(sig, current);
380 return -EFAULT;
384 * OK, we're invoking a handler
387 static int
388 handle_signal(unsigned long sig, struct k_sigaction *ka,
389 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
391 int ret;
393 /* Set up the stack frame */
394 if (ka->sa.sa_flags & SA_SIGINFO)
395 ret = setup_rt_frame(sig, ka, info, oldset, regs);
396 else
397 ret = setup_frame(sig, ka, oldset, regs);
399 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);
408 return ret;
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.
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.
420 void do_signal(struct pt_regs *regs)
422 unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
423 siginfo_t info;
424 int signr;
425 struct k_sigaction ka;
426 sigset_t *oldset;
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.
434 if (!user_mode(regs))
435 return;
437 if (test_thread_flag(TIF_RESTORE_SIGMASK))
438 oldset = &current->saved_sigmask;
439 else
440 oldset = &current->blocked;
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];
448 /* Prepare for system call restart. We do this here so that a
449 debugger will see the already changed PSW. */
450 switch (retval) {
451 case -ERESTARTNOHAND:
452 case -ERESTARTSYS:
453 case -ERESTARTNOINTR:
454 regs->gprs[2] = regs->orig_gpr2;
455 regs->psw.addr = restart_addr;
456 break;
457 case -ERESTART_RESTARTBLOCK:
458 regs->gprs[2] = -EINTR;
462 /* Get signal to deliver. When running under ptrace, at this point
463 the debugger may change all our registers ... */
464 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
466 /* Depending on the signal settings we may need to revert the
467 decision to restart the system call. */
468 if (signr > 0 && regs->psw.addr == restart_addr) {
469 if (retval == -ERESTARTNOHAND
470 || (retval == -ERESTARTSYS
471 && !(current->sighand->action[signr-1].sa.sa_flags
472 & SA_RESTART))) {
473 regs->gprs[2] = -EINTR;
474 regs->psw.addr = continue_addr;
478 if (signr > 0) {
479 /* Whee! Actually deliver the signal. */
480 #ifdef CONFIG_COMPAT
481 if (test_thread_flag(TIF_31BIT)) {
482 extern int handle_signal32(unsigned long sig,
483 struct k_sigaction *ka,
484 siginfo_t *info,
485 sigset_t *oldset,
486 struct pt_regs *regs);
487 if (handle_signal32(
488 signr, &ka, &info, oldset, regs) == 0) {
489 if (test_thread_flag(TIF_RESTORE_SIGMASK))
490 clear_thread_flag(TIF_RESTORE_SIGMASK);
492 return;
494 #endif
495 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
497 * A signal was successfully delivered; the saved
498 * sigmask will have been stored in the signal frame,
499 * and will be restored by sigreturn, so we can simply
500 * clear the TIF_RESTORE_SIGMASK flag.
502 if (test_thread_flag(TIF_RESTORE_SIGMASK))
503 clear_thread_flag(TIF_RESTORE_SIGMASK);
505 return;
509 * If there's no signal to deliver, we just put the saved sigmask back.
511 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
512 clear_thread_flag(TIF_RESTORE_SIGMASK);
513 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
516 /* Restart a different system call. */
517 if (retval == -ERESTART_RESTARTBLOCK
518 && regs->psw.addr == continue_addr) {
519 regs->gprs[2] = __NR_restart_syscall;
520 set_thread_flag(TIF_RESTART_SVC);