1 /*
2 * arch/s390/kernel/signal.c
3 *
4 * S390 version
5 * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
7 *
8 * Based on Intel version
9 *
10 * Copyright (C) 1991, 1992 Linus Torvalds
11 *
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
13 */
14
15 #include <linux/config.h>
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27 #include <linux/personality.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32
33
34 typedef struct
35 {
36 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
37 struct sigcontext sc;
38 _sigregs sregs;
39 __u8 retcode[S390_SYSCALL_SIZE];
40 } sigframe;
41
42 typedef struct
43 {
44 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
45 __u8 retcode[S390_SYSCALL_SIZE];
46 struct siginfo info;
47 struct ucontext uc;
48 } rt_sigframe;
49
50 asmlinkage int FASTCALL(do_signal(struct pt_regs *regs, sigset_t *oldset));
51
copy_siginfo_to_user(siginfo_t * to,siginfo_t * from)52 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
53 {
54 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
55 return -EFAULT;
56 if (from->si_code < 0)
57 return __copy_to_user(to, from, sizeof(siginfo_t));
58 else {
59 int err;
60
61 /* If you change siginfo_t structure, please be sure
62 this code is fixed accordingly.
63 It should never copy any pad contained in the structure
64 to avoid security leaks, but must copy the generic
65 3 ints plus the relevant union member. */
66 err = __put_user(from->si_signo, &to->si_signo);
67 err |= __put_user(from->si_errno, &to->si_errno);
68 err |= __put_user((short)from->si_code, &to->si_code);
69 /* First 32bits of unions are always present. */
70 err |= __put_user(from->si_pid, &to->si_pid);
71 switch (from->si_code >> 16) {
72 case __SI_FAULT >> 16:
73 break;
74 case __SI_CHLD >> 16:
75 err |= __put_user(from->si_utime, &to->si_utime);
76 err |= __put_user(from->si_stime, &to->si_stime);
77 err |= __put_user(from->si_status, &to->si_status);
78 default:
79 err |= __put_user(from->si_uid, &to->si_uid);
80 break;
81 /* case __SI_RT: This is not generated by the kernel as of now. */
82 }
83 return err;
84 }
85 }
86
87 /*
88 * Atomically swap in the new signal mask, and wait for a signal.
89 */
90 asmlinkage int
sys_sigsuspend(struct pt_regs * regs,int history0,int history1,old_sigset_t mask)91 sys_sigsuspend(struct pt_regs * regs,int history0, int history1, old_sigset_t mask)
92 {
93 sigset_t saveset;
94
95 mask &= _BLOCKABLE;
96 spin_lock_irq(¤t->sigmask_lock);
97 saveset = current->blocked;
98 siginitset(¤t->blocked, mask);
99 recalc_sigpending(current);
100 spin_unlock_irq(¤t->sigmask_lock);
101 regs->gprs[2] = -EINTR;
102
103 while (1) {
104 set_current_state(TASK_INTERRUPTIBLE);
105 schedule();
106 if (do_signal(regs, &saveset))
107 return -EINTR;
108 }
109 }
110
111 asmlinkage int
sys_rt_sigsuspend(struct pt_regs * regs,sigset_t * unewset,size_t sigsetsize)112 sys_rt_sigsuspend(struct pt_regs * regs,sigset_t *unewset, size_t sigsetsize)
113 {
114 sigset_t saveset, newset;
115
116 /* XXX: Don't preclude handling different sized sigset_t's. */
117 if (sigsetsize != sizeof(sigset_t))
118 return -EINVAL;
119
120 if (copy_from_user(&newset, unewset, sizeof(newset)))
121 return -EFAULT;
122 sigdelsetmask(&newset, ~_BLOCKABLE);
123
124 spin_lock_irq(¤t->sigmask_lock);
125 saveset = current->blocked;
126 current->blocked = newset;
127 recalc_sigpending(current);
128 spin_unlock_irq(¤t->sigmask_lock);
129 regs->gprs[2] = -EINTR;
130
131 while (1) {
132 set_current_state(TASK_INTERRUPTIBLE);
133 schedule();
134 if (do_signal(regs, &saveset))
135 return -EINTR;
136 }
137 }
138
139 asmlinkage int
sys_sigaction(int sig,const struct old_sigaction * act,struct old_sigaction * oact)140 sys_sigaction(int sig, const struct old_sigaction *act,
141 struct old_sigaction *oact)
142 {
143 struct k_sigaction new_ka, old_ka;
144 int ret;
145
146 if (act) {
147 old_sigset_t mask;
148 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
149 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
150 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
151 return -EFAULT;
152 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
153 __get_user(mask, &act->sa_mask);
154 siginitset(&new_ka.sa.sa_mask, mask);
155 }
156
157 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
158
159 if (!ret && oact) {
160 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
161 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
162 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
163 return -EFAULT;
164 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
165 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
166 }
167
168 return ret;
169 }
170
171 asmlinkage int
sys_sigaltstack(const stack_t * uss,stack_t * uoss,struct pt_regs * regs)172 sys_sigaltstack(const stack_t *uss, stack_t *uoss, struct pt_regs *regs)
173 {
174 return do_sigaltstack(uss, uoss, regs->gprs[15]);
175 }
176
177
178
179
save_sigregs(struct pt_regs * regs,_sigregs * sregs)180 static int save_sigregs(struct pt_regs *regs,_sigregs *sregs)
181 {
182 int err;
183 s390_fp_regs fpregs;
184
185 err = __copy_to_user(&sregs->regs,regs,sizeof(_s390_regs_common));
186 if(!err)
187 {
188 save_fp_regs(&fpregs);
189 err=__copy_to_user(&sregs->fpregs,&fpregs,sizeof(fpregs));
190 }
191 return(err);
192
193 }
194
restore_sigregs(struct pt_regs * regs,_sigregs * sregs)195 static int restore_sigregs(struct pt_regs *regs,_sigregs *sregs)
196 {
197 int err;
198 s390_fp_regs fpregs;
199 psw_t saved_psw=regs->psw;
200 err=__copy_from_user(regs,&sregs->regs,sizeof(_s390_regs_common));
201 if(!err)
202 {
203 regs->trap = -1; /* disable syscall checks */
204 regs->psw.mask=(saved_psw.mask&~PSW_MASK_DEBUGCHANGE)|
205 (regs->psw.mask&PSW_MASK_DEBUGCHANGE);
206 regs->psw.addr=(saved_psw.addr&~PSW_ADDR_DEBUGCHANGE)|
207 (regs->psw.addr&PSW_ADDR_DEBUGCHANGE);
208 err=__copy_from_user(&fpregs,&sregs->fpregs,sizeof(fpregs));
209 if(!err)
210 restore_fp_regs(&fpregs);
211 }
212 return(err);
213 }
214
sys_sigreturn(struct pt_regs * regs)215 asmlinkage long sys_sigreturn(struct pt_regs *regs)
216 {
217 sigframe *frame = (sigframe *)regs->gprs[15];
218 sigset_t set;
219
220 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
221 goto badframe;
222 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
223 goto badframe;
224
225 sigdelsetmask(&set, ~_BLOCKABLE);
226 spin_lock_irq(¤t->sigmask_lock);
227 current->blocked = set;
228 recalc_sigpending(current);
229 spin_unlock_irq(¤t->sigmask_lock);
230
231 if (restore_sigregs(regs, &frame->sregs))
232 goto badframe;
233
234 return regs->gprs[2];
235
236 badframe:
237 force_sig(SIGSEGV, current);
238 return 0;
239 }
240
sys_rt_sigreturn(struct pt_regs * regs)241 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
242 {
243 rt_sigframe *frame = (rt_sigframe *)regs->gprs[15];
244 sigset_t set;
245
246 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
247 goto badframe;
248 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
249 goto badframe;
250
251 sigdelsetmask(&set, ~_BLOCKABLE);
252 spin_lock_irq(¤t->sigmask_lock);
253 current->blocked = set;
254 recalc_sigpending(current);
255 spin_unlock_irq(¤t->sigmask_lock);
256
257 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
258 goto badframe;
259
260 /* It is more difficult to avoid calling this function than to
261 call it and ignore errors. */
262 do_sigaltstack(&frame->uc.uc_stack, NULL, regs->gprs[15]);
263 return regs->gprs[2];
264
265 badframe:
266 force_sig(SIGSEGV, current);
267 return 0;
268 }
269
270 /*
271 * Set up a signal frame.
272 */
273
274
275 /*
276 * Determine which stack to use..
277 */
278 static inline void *
get_sigframe(struct k_sigaction * ka,struct pt_regs * regs,size_t frame_size)279 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
280 {
281 unsigned long sp;
282
283 /* Default to using normal stack */
284 sp = regs->gprs[15];
285
286 /* This is the X/Open sanctioned signal stack switching. */
287 if (ka->sa.sa_flags & SA_ONSTACK) {
288 if (! on_sig_stack(sp))
289 sp = current->sas_ss_sp + current->sas_ss_size;
290 }
291
292 /* This is the legacy signal stack switching. */
293 else if (!user_mode(regs) &&
294 !(ka->sa.sa_flags & SA_RESTORER) &&
295 ka->sa.sa_restorer) {
296 sp = (unsigned long) ka->sa.sa_restorer;
297 }
298
299 return (void *)((sp - frame_size) & -8ul);
300 }
301
map_signal(int sig)302 static inline int map_signal(int sig)
303 {
304 if (current->exec_domain
305 && current->exec_domain->signal_invmap
306 && sig < 32)
307 return current->exec_domain->signal_invmap[sig];
308 else
309 return sig;
310 }
311
setup_frame(int sig,struct k_sigaction * ka,sigset_t * set,struct pt_regs * regs)312 static void setup_frame(int sig, struct k_sigaction *ka,
313 sigset_t *set, struct pt_regs * regs)
314 {
315 sigframe *frame = get_sigframe(ka, regs, sizeof(sigframe));
316 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
317 goto give_sigsegv;
318
319 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
320 goto give_sigsegv;
321
322 if (save_sigregs(regs, &frame->sregs))
323 goto give_sigsegv;
324 if (__put_user(&frame->sregs, &frame->sc.sregs))
325 goto give_sigsegv;
326
327 /* Set up to return from userspace. If provided, use a stub
328 already in userspace. */
329 if (ka->sa.sa_flags & SA_RESTORER) {
330 regs->gprs[14] = FIX_PSW(ka->sa.sa_restorer);
331 } else {
332 regs->gprs[14] = FIX_PSW(frame->retcode);
333 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
334 (u16 *)(frame->retcode)))
335 goto give_sigsegv;
336 }
337
338 /* Set up backchain. */
339 if (__put_user(regs->gprs[15], (addr_t *) frame))
340 goto give_sigsegv;
341
342 /* Set up registers for signal handler */
343 regs->gprs[15] = (addr_t)frame;
344 regs->psw.addr = FIX_PSW(ka->sa.sa_handler);
345 regs->psw.mask = _USER_PSW_MASK;
346
347 regs->gprs[2] = map_signal(sig);
348 regs->gprs[3] = (addr_t)&frame->sc;
349
350 /* We forgot to include these in the sigcontext.
351 To avoid breaking binary compatibility, they are passed as args. */
352 regs->gprs[4] = current->thread.trap_no;
353 regs->gprs[5] = current->thread.prot_addr;
354 return;
355
356 give_sigsegv:
357 if (sig == SIGSEGV)
358 ka->sa.sa_handler = SIG_DFL;
359 force_sig(SIGSEGV, current);
360 }
361
setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set,struct pt_regs * regs)362 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
363 sigset_t *set, struct pt_regs * regs)
364 {
365 int err = 0;
366 rt_sigframe *frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
367 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
368 goto give_sigsegv;
369
370 if (copy_siginfo_to_user(&frame->info, info))
371 goto give_sigsegv;
372
373 /* Create the ucontext. */
374 err |= __put_user(0, &frame->uc.uc_flags);
375 err |= __put_user(0, &frame->uc.uc_link);
376 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
377 err |= __put_user(sas_ss_flags(regs->gprs[15]),
378 &frame->uc.uc_stack.ss_flags);
379 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
380 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
381 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
382 if (err)
383 goto give_sigsegv;
384
385 /* Set up to return from userspace. If provided, use a stub
386 already in userspace. */
387 if (ka->sa.sa_flags & SA_RESTORER) {
388 regs->gprs[14] = FIX_PSW(ka->sa.sa_restorer);
389 } else {
390 regs->gprs[14] = FIX_PSW(frame->retcode);
391 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
392 (u16 *)(frame->retcode));
393 }
394
395 /* Set up backchain. */
396 if (__put_user(regs->gprs[15], (addr_t *) frame))
397 goto give_sigsegv;
398
399 /* Set up registers for signal handler */
400 regs->gprs[15] = (addr_t)frame;
401 regs->psw.addr = FIX_PSW(ka->sa.sa_handler);
402 regs->psw.mask = _USER_PSW_MASK;
403
404 regs->gprs[2] = map_signal(sig);
405 regs->gprs[3] = (addr_t)&frame->info;
406 regs->gprs[4] = (addr_t)&frame->uc;
407 return;
408
409 give_sigsegv:
410 if (sig == SIGSEGV)
411 ka->sa.sa_handler = SIG_DFL;
412 force_sig(SIGSEGV, current);
413 }
414
415 /*
416 * OK, we're invoking a handler
417 */
418
419 static void
handle_signal(unsigned long sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * oldset,struct pt_regs * regs)420 handle_signal(unsigned long sig, struct k_sigaction *ka,
421 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
422 {
423 /* Are we from a system call? */
424 if (regs->trap == __LC_SVC_OLD_PSW) {
425 /* If so, check system call restarting.. */
426 switch (regs->gprs[2]) {
427 case -ERESTARTNOHAND:
428 regs->gprs[2] = -EINTR;
429 break;
430
431 case -ERESTARTSYS:
432 if (!(ka->sa.sa_flags & SA_RESTART)) {
433 regs->gprs[2] = -EINTR;
434 break;
435 }
436 /* fallthrough */
437 case -ERESTARTNOINTR:
438 regs->gprs[2] = regs->orig_gpr2;
439 regs->psw.addr -= 2;
440 }
441 }
442
443 /* Set up the stack frame */
444 if (ka->sa.sa_flags & SA_SIGINFO)
445 setup_rt_frame(sig, ka, info, oldset, regs);
446 else
447 setup_frame(sig, ka, oldset, regs);
448
449 if (ka->sa.sa_flags & SA_ONESHOT)
450 ka->sa.sa_handler = SIG_DFL;
451
452 if (!(ka->sa.sa_flags & SA_NODEFER)) {
453 spin_lock_irq(¤t->sigmask_lock);
454 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
455 sigaddset(¤t->blocked,sig);
456 recalc_sigpending(current);
457 spin_unlock_irq(¤t->sigmask_lock);
458 }
459 }
460
461 /*
462 * Note that 'init' is a special process: it doesn't get signals it doesn't
463 * want to handle. Thus you cannot kill init even with a SIGKILL even by
464 * mistake.
465 *
466 * Note that we go through the signals twice: once to check the signals that
467 * the kernel can handle, and then we build all the user-level signal handling
468 * stack-frames in one go after that.
469 */
do_signal(struct pt_regs * regs,sigset_t * oldset)470 int do_signal(struct pt_regs *regs, sigset_t *oldset)
471 {
472 siginfo_t info;
473 struct k_sigaction *ka;
474
475 /*
476 * We want the common case to go fast, which
477 * is why we may in certain cases get here from
478 * kernel mode. Just return without doing anything
479 * if so.
480 */
481 if (!user_mode(regs))
482 return 1;
483
484 if (!oldset)
485 oldset = ¤t->blocked;
486
487 for (;;) {
488 unsigned long signr;
489
490 spin_lock_irq(¤t->sigmask_lock);
491 signr = dequeue_signal(¤t->blocked, &info);
492 spin_unlock_irq(¤t->sigmask_lock);
493
494 if (!signr)
495 break;
496
497 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
498 /* Let the debugger run. */
499 current->exit_code = signr;
500 set_current_state(TASK_STOPPED);
501 notify_parent(current, SIGCHLD);
502 schedule();
503
504 /* We're back. Did the debugger cancel the sig? */
505 if (!(signr = current->exit_code))
506 continue;
507 current->exit_code = 0;
508
509 /* The debugger continued. Ignore SIGSTOP. */
510 if (signr == SIGSTOP)
511 continue;
512
513 /* Update the siginfo structure. Is this good? */
514 if (signr != info.si_signo) {
515 info.si_signo = signr;
516 info.si_errno = 0;
517 info.si_code = SI_USER;
518 info.si_pid = current->p_pptr->pid;
519 info.si_uid = current->p_pptr->uid;
520 }
521
522 /* If the (new) signal is now blocked, requeue it. */
523 if (sigismember(¤t->blocked, signr)) {
524 send_sig_info(signr, &info, current);
525 continue;
526 }
527 }
528
529 ka = ¤t->sig->action[signr-1];
530 if (ka->sa.sa_handler == SIG_IGN) {
531 if (signr != SIGCHLD)
532 continue;
533 /* Check for SIGCHLD: it's special. */
534 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
535 /* nothing */;
536 continue;
537 }
538
539 if (ka->sa.sa_handler == SIG_DFL) {
540 int exit_code = signr;
541
542 /* Init gets no signals it doesn't want. */
543 if (current->pid == 1)
544 continue;
545
546 switch (signr) {
547 case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
548 continue;
549
550 case SIGTSTP: case SIGTTIN: case SIGTTOU:
551 if (is_orphaned_pgrp(current->pgrp))
552 continue;
553 /* FALLTHRU */
554
555 case SIGSTOP: {
556 struct signal_struct *sig;
557 set_current_state(TASK_STOPPED);
558 current->exit_code = signr;
559 sig = current->p_pptr->sig;
560 if (sig && !(sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
561 notify_parent(current, SIGCHLD);
562 schedule();
563 continue;
564 }
565
566 case SIGQUIT: case SIGILL: case SIGTRAP:
567 case SIGABRT: case SIGFPE: case SIGSEGV:
568 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
569 if (do_coredump(signr, regs))
570 exit_code |= 0x80;
571 /* FALLTHRU */
572
573 default:
574 sig_exit(signr, exit_code, &info);
575 /* NOTREACHED */
576 }
577 }
578
579 /* Whee! Actually deliver the signal. */
580 handle_signal(signr, ka, &info, oldset, regs);
581 return 1;
582 }
583
584 /* Did we come from a system call? */
585 if ( regs->trap == __LC_SVC_OLD_PSW /* System Call! */ ) {
586 /* Restart the system call - no handlers present */
587 if (regs->gprs[2] == -ERESTARTNOHAND ||
588 regs->gprs[2] == -ERESTARTSYS ||
589 regs->gprs[2] == -ERESTARTNOINTR) {
590 regs->gprs[2] = regs->orig_gpr2;
591 regs->psw.addr -= 2;
592 }
593 }
594 return 0;
595 }
596