1 /*
2  *  arch/s390/kernel/signal32.c
3  *
4  *  S390 version
5  *    Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
7  *               Gerhard Tonn (ton@de.ibm.com)
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/personality.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include "linux32.h"
30 
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32 
33 #define _USER_PSW_MASK32 0x0705C00080000000
34 
35 typedef struct
36 {
37 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
38 	struct sigcontext32 sc;
39 	_sigregs32 sregs;
40 	__u8 retcode[S390_SYSCALL_SIZE];
41 } sigframe32;
42 
43 typedef struct
44 {
45 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
46 	__u8 retcode[S390_SYSCALL_SIZE];
47 	struct siginfo32 info;
48 	struct ucontext32 uc;
49 } rt_sigframe32;
50 
51 asmlinkage int FASTCALL(do_signal(struct pt_regs *regs, sigset_t *oldset));
52 
53 int do_signal32(struct pt_regs *regs, sigset_t *oldset);
54 
copy_siginfo_to_user32(siginfo_t32 * to,siginfo_t * from)55 int copy_siginfo_to_user32(siginfo_t32 *to, siginfo_t *from)
56 {
57 	int err;
58 
59 	if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t32)))
60 		return -EFAULT;
61 
62 	/* If you change siginfo_t structure, please be sure
63 	   this code is fixed accordingly.
64 	   It should never copy any pad contained in the structure
65 	   to avoid security leaks, but must copy the generic
66 	   3 ints plus the relevant union member.
67 	   This routine must convert siginfo from 64bit to 32bit as well
68 	   at the same time.  */
69 	err = __put_user(from->si_signo, &to->si_signo);
70 	err |= __put_user(from->si_errno, &to->si_errno);
71 	err |= __put_user((short)from->si_code, &to->si_code);
72 	if (from->si_code < 0)
73 		err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
74 	else {
75 		switch (from->si_code >> 16) {
76 		case __SI_KILL >> 16:
77 			err |= __put_user(from->si_pid, &to->si_pid);
78 			err |= __put_user(from->si_uid, &to->si_uid);
79 			break;
80 		case __SI_CHLD >> 16:
81 			err |= __put_user(from->si_pid, &to->si_pid);
82 			err |= __put_user(from->si_uid, &to->si_uid);
83 			err |= __put_user(from->si_utime, &to->si_utime);
84 			err |= __put_user(from->si_stime, &to->si_stime);
85 			err |= __put_user(from->si_status, &to->si_status);
86 			break;
87 		case __SI_FAULT >> 16:
88 			err |= __put_user(from->si_addr, &to->si_addr);
89 			break;
90 		case __SI_POLL >> 16:
91 		case __SI_TIMER >> 16:
92 			err |= __put_user(from->si_band, &to->si_band);
93 			err |= __put_user(from->si_fd, &to->si_fd);
94 			break;
95 		default:
96 			break;
97 		/* case __SI_RT: This is not generated by the kernel as of now.  */
98 		}
99 	}
100 	return err;
101 }
102 
103 /*
104  * Atomically swap in the new signal mask, and wait for a signal.
105  */
106 asmlinkage int
sys32_sigsuspend(struct pt_regs * regs,int history0,int history1,old_sigset_t mask)107 sys32_sigsuspend(struct pt_regs * regs,int history0, int history1, old_sigset_t mask)
108 {
109 	sigset_t saveset;
110 
111 	mask &= _BLOCKABLE;
112 	spin_lock_irq(&current->sigmask_lock);
113 	saveset = current->blocked;
114 	siginitset(&current->blocked, mask);
115 	recalc_sigpending(current);
116 	spin_unlock_irq(&current->sigmask_lock);
117 	regs->gprs[2] = -EINTR;
118 
119 	while (1) {
120 		set_current_state(TASK_INTERRUPTIBLE);
121 		schedule();
122 		if (do_signal32(regs, &saveset))
123 			return -EINTR;
124 	}
125 }
126 
127 asmlinkage int
sys32_rt_sigsuspend(struct pt_regs * regs,sigset_t32 * unewset,size_t sigsetsize)128 sys32_rt_sigsuspend(struct pt_regs * regs,sigset_t32 *unewset, size_t sigsetsize)
129 {
130 	sigset_t saveset, newset;
131 	sigset_t32 set32;
132 
133 	/* XXX: Don't preclude handling different sized sigset_t's.  */
134 	if (sigsetsize != sizeof(sigset_t))
135 		return -EINVAL;
136 
137 	if (copy_from_user(&set32, unewset, sizeof(set32)))
138 		return -EFAULT;
139 	switch (_NSIG_WORDS) {
140 	case 4: newset.sig[3] = set32.sig[6] + (((long)set32.sig[7]) << 32);
141 	case 3: newset.sig[2] = set32.sig[4] + (((long)set32.sig[5]) << 32);
142 	case 2: newset.sig[1] = set32.sig[2] + (((long)set32.sig[3]) << 32);
143 	case 1: newset.sig[0] = set32.sig[0] + (((long)set32.sig[1]) << 32);
144 	}
145         sigdelsetmask(&newset, ~_BLOCKABLE);
146 
147         spin_lock_irq(&current->sigmask_lock);
148         saveset = current->blocked;
149         current->blocked = newset;
150         recalc_sigpending(current);
151         spin_unlock_irq(&current->sigmask_lock);
152         regs->gprs[2] = -EINTR;
153 
154         while (1) {
155                 set_current_state(TASK_INTERRUPTIBLE);
156                 schedule();
157                 if (do_signal32(regs, &saveset))
158                         return -EINTR;
159         }
160 }
161 
162 asmlinkage int
sys32_sigaction(int sig,const struct old_sigaction32 * act,struct old_sigaction32 * oact)163 sys32_sigaction(int sig, const struct old_sigaction32 *act,
164 		 struct old_sigaction32 *oact)
165 {
166         struct k_sigaction new_ka, old_ka;
167         int ret;
168 
169         if (act) {
170 		old_sigset_t32 mask;
171 		if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
172 		    __get_user((unsigned long)new_ka.sa.sa_handler, &act->sa_handler) ||
173 		    __get_user((unsigned long)new_ka.sa.sa_restorer, &act->sa_restorer))
174 			return -EFAULT;
175 		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
176 		__get_user(mask, &act->sa_mask);
177 		siginitset(&new_ka.sa.sa_mask, mask);
178         }
179 
180         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
181 
182 	if (!ret && oact) {
183 		if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
184 		    __put_user((unsigned long)old_ka.sa.sa_handler, &oact->sa_handler) ||
185 		    __put_user((unsigned long)old_ka.sa.sa_restorer, &oact->sa_restorer))
186 			return -EFAULT;
187 		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
188 		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
189         }
190 
191 	return ret;
192 }
193 
194 int
195 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact);
196 
197 asmlinkage long
sys32_rt_sigaction(int sig,const struct sigaction32 * act,struct sigaction32 * oact,size_t sigsetsize)198 sys32_rt_sigaction(int sig, const struct sigaction32 *act,
199 	   struct sigaction32 *oact,  size_t sigsetsize)
200 {
201 	struct k_sigaction new_ka, old_ka;
202 	int ret;
203 	sigset_t32 set32;
204 
205 	/* XXX: Don't preclude handling different sized sigset_t's.  */
206 	if (sigsetsize != sizeof(sigset_t32))
207 		return -EINVAL;
208 
209 	if (act) {
210 		ret = get_user((unsigned long)new_ka.sa.sa_handler, &act->sa_handler);
211 		ret |= __copy_from_user(&set32, &act->sa_mask,
212 					sizeof(sigset_t32));
213 		switch (_NSIG_WORDS) {
214 		case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
215 				| (((long)set32.sig[7]) << 32);
216 		case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
217 				| (((long)set32.sig[5]) << 32);
218 		case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
219 				| (((long)set32.sig[3]) << 32);
220 		case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
221 				| (((long)set32.sig[1]) << 32);
222 		}
223 		ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
224 
225 		if (ret)
226 			return -EFAULT;
227 	}
228 
229 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
230 
231 	if (!ret && oact) {
232 		switch (_NSIG_WORDS) {
233 		case 4:
234 			set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
235 			set32.sig[6] = old_ka.sa.sa_mask.sig[3];
236 		case 3:
237 			set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
238 			set32.sig[4] = old_ka.sa.sa_mask.sig[2];
239 		case 2:
240 			set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
241 			set32.sig[2] = old_ka.sa.sa_mask.sig[1];
242 		case 1:
243 			set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
244 			set32.sig[0] = old_ka.sa.sa_mask.sig[0];
245 		}
246 		ret = put_user((unsigned long)old_ka.sa.sa_handler, &oact->sa_handler);
247 		ret |= __copy_to_user(&oact->sa_mask, &set32,
248 				      sizeof(sigset_t32));
249 		ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
250 	}
251 
252 	return ret;
253 }
254 
255 asmlinkage int
sys32_sigaltstack(const stack_t32 * uss,stack_t32 * uoss,struct pt_regs * regs)256 sys32_sigaltstack(const stack_t32 *uss, stack_t32 *uoss, struct pt_regs *regs)
257 {
258 	stack_t kss, koss;
259 	int ret, err = 0;
260 	mm_segment_t old_fs = get_fs();
261 
262 	if (uss) {
263 		if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
264 			return -EFAULT;
265 		err |= __get_user(kss.ss_sp, &uss->ss_sp);
266 		err |= __get_user(kss.ss_size, &uss->ss_size);
267 		err |= __get_user(kss.ss_flags, &uss->ss_flags);
268 		if (err)
269 			return -EFAULT;
270 	}
271 
272 	set_fs (KERNEL_DS);
273 	ret = do_sigaltstack(uss ? &kss : NULL , uoss ? &koss : NULL, regs->gprs[15]);
274 	set_fs (old_fs);
275 
276 	if (!ret && uoss) {
277 		if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
278 			return -EFAULT;
279 		err |= __put_user(koss.ss_sp, &uoss->ss_sp);
280 		err |= __put_user(koss.ss_size, &uoss->ss_size);
281 		err |= __put_user(koss.ss_flags, &uoss->ss_flags);
282 		if (err)
283 			return -EFAULT;
284 	}
285 	return ret;
286 }
287 
save_sigregs32(struct pt_regs * regs,_sigregs32 * sregs)288 static int save_sigregs32(struct pt_regs *regs,_sigregs32 *sregs)
289 {
290 	int err = 0;
291 	s390_fp_regs fpregs;
292 	int i;
293 
294 	for(i=0; i<NUM_GPRS; i++)
295 		err |= __put_user(regs->gprs[i], &sregs->regs.gprs[i]);
296 	for(i=0; i<NUM_ACRS; i++)
297 		err |= __put_user(regs->acrs[i], &sregs->regs.acrs[i]);
298 	err |= __copy_to_user(&sregs->regs.psw.mask, &regs->psw.mask, 4);
299 	err |= __copy_to_user(&sregs->regs.psw.addr, ((char*)&regs->psw.addr)+4, 4);
300 	if(!err)
301 	{
302 		save_fp_regs(&fpregs);
303 		__put_user(fpregs.fpc, &sregs->fpregs.fpc);
304 		for(i=0; i<NUM_FPRS; i++)
305 			err |= __put_user(fpregs.fprs[i].ui, &sregs->fpregs.fprs[i].d);
306 	}
307 	return(err);
308 
309 }
310 
restore_sigregs32(struct pt_regs * regs,_sigregs32 * sregs)311 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 *sregs)
312 {
313 	int err = 0;
314 	s390_fp_regs fpregs;
315 	psw_t saved_psw=regs->psw;
316 	int i;
317 
318 	for(i=0; i<NUM_GPRS; i++)
319 		err |= __get_user(regs->gprs[i], &sregs->regs.gprs[i]);
320 	for(i=0; i<NUM_ACRS; i++)
321 		err |= __get_user(regs->acrs[i], &sregs->regs.acrs[i]);
322 	err |= __copy_from_user(&regs->psw.mask, &sregs->regs.psw.mask, 4);
323 	err |= __copy_from_user(((char*)&regs->psw.addr)+4, &sregs->regs.psw.addr, 4);
324 
325 	if(!err)
326 	{
327 		regs->trap = -1;		/* disable syscall checks */
328 		regs->psw.mask=(saved_psw.mask&~PSW_MASK_DEBUGCHANGE)|
329 		(regs->psw.mask&PSW_MASK_DEBUGCHANGE);
330 		regs->psw.addr=(saved_psw.addr&~PSW_ADDR_DEBUGCHANGE)|
331 		(regs->psw.addr&PSW_ADDR_DEBUGCHANGE);
332 		__get_user(fpregs.fpc, &sregs->fpregs.fpc);
333                 for(i=0; i<NUM_FPRS; i++)
334                         err |= __get_user(fpregs.fprs[i].ui, &sregs->fpregs.fprs[i].d);
335 		if(!err)
336 			restore_fp_regs(&fpregs);
337 	}
338 	return(err);
339 }
340 
sys32_sigreturn(struct pt_regs * regs)341 asmlinkage long sys32_sigreturn(struct pt_regs *regs)
342 {
343 	sigframe32 *frame = (sigframe32 *)regs->gprs[15];
344 	sigset_t set;
345 
346 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
347 		goto badframe;
348 	if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32))
349 		goto badframe;
350 
351 	sigdelsetmask(&set, ~_BLOCKABLE);
352 	spin_lock_irq(&current->sigmask_lock);
353 	current->blocked = set;
354 	recalc_sigpending(current);
355 	spin_unlock_irq(&current->sigmask_lock);
356 
357 	if (restore_sigregs32(regs, &frame->sregs))
358 		goto badframe;
359 
360 	return regs->gprs[2];
361 
362 badframe:
363 	force_sig(SIGSEGV, current);
364 	return 0;
365 }
366 
sys32_rt_sigreturn(struct pt_regs * regs)367 asmlinkage long sys32_rt_sigreturn(struct pt_regs *regs)
368 {
369 	rt_sigframe32 *frame = (rt_sigframe32 *)regs->gprs[15];
370 	sigset_t set;
371 	stack_t st;
372 	int err;
373 	mm_segment_t old_fs = get_fs();
374 
375 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
376 		goto badframe;
377 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
378 		goto badframe;
379 
380 	sigdelsetmask(&set, ~_BLOCKABLE);
381 	spin_lock_irq(&current->sigmask_lock);
382 	current->blocked = set;
383 	recalc_sigpending(current);
384 	spin_unlock_irq(&current->sigmask_lock);
385 
386 	if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
387 		goto badframe;
388 
389 	err = __get_user(st.ss_sp, &frame->uc.uc_stack.ss_sp);
390 	st.ss_sp = (void *) A((unsigned long)st.ss_sp);
391 	err |= __get_user(st.ss_size, &frame->uc.uc_stack.ss_size);
392 	err |= __get_user(st.ss_flags, &frame->uc.uc_stack.ss_flags);
393 	if (err)
394 		goto badframe;
395 
396 	/* It is more difficult to avoid calling this function than to
397 	   call it and ignore errors.  */
398 	set_fs (KERNEL_DS);
399 	do_sigaltstack(&st, NULL, regs->gprs[15]);
400 	set_fs (old_fs);
401 
402 	return regs->gprs[2];
403 
404 badframe:
405         force_sig(SIGSEGV, current);
406         return 0;
407 }
408 
409 /*
410  * Set up a signal frame.
411  */
412 
413 
414 /*
415  * Determine which stack to use..
416  */
417 static inline void *
get_sigframe(struct k_sigaction * ka,struct pt_regs * regs,size_t frame_size)418 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
419 {
420 	unsigned long sp;
421 
422 	/* Default to using normal stack */
423 	sp = (unsigned long) A(regs->gprs[15]);
424 
425 	/* This is the X/Open sanctioned signal stack switching.  */
426 	if (ka->sa.sa_flags & SA_ONSTACK) {
427 		if (! on_sig_stack(sp))
428 			sp = current->sas_ss_sp + current->sas_ss_size;
429 	}
430 
431 	/* This is the legacy signal stack switching. */
432 	else if (!user_mode(regs) &&
433 		 !(ka->sa.sa_flags & SA_RESTORER) &&
434 		 ka->sa.sa_restorer) {
435 		sp = (unsigned long) ka->sa.sa_restorer;
436 	}
437 
438 	return (void *)((sp - frame_size) & -8ul);
439 }
440 
map_signal(int sig)441 static inline int map_signal(int sig)
442 {
443 	if (current->exec_domain
444 	    && current->exec_domain->signal_invmap
445 	    && sig < 32)
446 		return current->exec_domain->signal_invmap[sig];
447         else
448 		return sig;
449 }
450 
setup_frame32(int sig,struct k_sigaction * ka,sigset_t * set,struct pt_regs * regs)451 static void setup_frame32(int sig, struct k_sigaction *ka,
452 			sigset_t *set, struct pt_regs * regs)
453 {
454 	sigframe32 *frame = get_sigframe(ka, regs, sizeof(sigframe32));
455 	if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe32)))
456 		goto give_sigsegv;
457 
458 	if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE32))
459 		goto give_sigsegv;
460 
461 	if (save_sigregs32(regs, &frame->sregs))
462 		goto give_sigsegv;
463 	if (__put_user(&frame->sregs, &frame->sc.sregs))
464 		goto give_sigsegv;
465 
466 	/* Set up to return from userspace.  If provided, use a stub
467 	   already in userspace.  */
468 	if (ka->sa.sa_flags & SA_RESTORER) {
469 		regs->gprs[14] = FIX_PSW(ka->sa.sa_restorer);
470 	} else {
471 		regs->gprs[14] = FIX_PSW(frame->retcode);
472 		if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
473 		               (u16 *)(frame->retcode)))
474 			goto give_sigsegv;
475         }
476 
477 	/* Set up backchain. */
478 	if (__put_user((unsigned int) regs->gprs[15], (unsigned int *) frame))
479 		goto give_sigsegv;
480 
481 	/* Set up registers for signal handler */
482 	regs->gprs[15] = (addr_t)frame;
483 	regs->psw.addr = FIX_PSW(ka->sa.sa_handler);
484 	regs->psw.mask = _USER_PSW_MASK32;
485 
486 	regs->gprs[2] = map_signal(sig);
487 	regs->gprs[3] = (addr_t)&frame->sc;
488 
489 	/* We forgot to include these in the sigcontext.
490 	   To avoid breaking binary compatibility, they are passed as args. */
491 	regs->gprs[4] = current->thread.trap_no;
492 	regs->gprs[5] = current->thread.prot_addr;
493 	return;
494 
495 give_sigsegv:
496 	if (sig == SIGSEGV)
497 		ka->sa.sa_handler = SIG_DFL;
498 	force_sig(SIGSEGV, current);
499 }
500 
setup_rt_frame32(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set,struct pt_regs * regs)501 static void setup_rt_frame32(int sig, struct k_sigaction *ka, siginfo_t *info,
502 			   sigset_t *set, struct pt_regs * regs)
503 {
504 	int err = 0;
505 	rt_sigframe32 *frame = get_sigframe(ka, regs, sizeof(rt_sigframe32));
506 	if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe32)))
507 		goto give_sigsegv;
508 
509 	if (copy_siginfo_to_user32(&frame->info, info))
510 		goto give_sigsegv;
511 
512 	/* Create the ucontext.  */
513 	err |= __put_user(0, &frame->uc.uc_flags);
514 	err |= __put_user(0, &frame->uc.uc_link);
515 	err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
516 	err |= __put_user(sas_ss_flags(regs->gprs[15]),
517 	                  &frame->uc.uc_stack.ss_flags);
518 	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
519 	err |= save_sigregs32(regs, &frame->uc.uc_mcontext);
520 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
521 	if (err)
522 		goto give_sigsegv;
523 
524 	/* Set up to return from userspace.  If provided, use a stub
525 	   already in userspace.  */
526 	if (ka->sa.sa_flags & SA_RESTORER) {
527 		regs->gprs[14] = FIX_PSW(ka->sa.sa_restorer);
528 	} else {
529 		regs->gprs[14] = FIX_PSW(frame->retcode);
530 		err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
531 		                  (u16 *)(frame->retcode));
532 	}
533 
534 	/* Set up backchain. */
535 	if (__put_user((unsigned int) regs->gprs[15], (unsigned int *) frame))
536 		goto give_sigsegv;
537 
538 	/* Set up registers for signal handler */
539 	regs->gprs[15] = (addr_t)frame;
540 	regs->psw.addr = FIX_PSW(ka->sa.sa_handler);
541 	regs->psw.mask = _USER_PSW_MASK32;
542 
543 	regs->gprs[2] = map_signal(sig);
544 	regs->gprs[3] = (addr_t)&frame->info;
545 	regs->gprs[4] = (addr_t)&frame->uc;
546 	return;
547 
548 give_sigsegv:
549 	if (sig == SIGSEGV)
550 		ka->sa.sa_handler = SIG_DFL;
551 	force_sig(SIGSEGV, current);
552 }
553 
554 /*
555  * OK, we're invoking a handler
556  */
557 
558 static void
handle_signal32(unsigned long sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * oldset,struct pt_regs * regs)559 handle_signal32(unsigned long sig, struct k_sigaction *ka,
560 	      siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
561 {
562 	/* Are we from a system call? */
563 	if (regs->trap == __LC_SVC_OLD_PSW) {
564 		/* If so, check system call restarting.. */
565 		switch (regs->gprs[2]) {
566 			case -ERESTARTNOHAND:
567 				regs->gprs[2] = -EINTR;
568 				break;
569 
570 			case -ERESTARTSYS:
571 				if (!(ka->sa.sa_flags & SA_RESTART)) {
572 					regs->gprs[2] = -EINTR;
573 					break;
574 				}
575 			/* fallthrough */
576 			case -ERESTARTNOINTR:
577 				regs->gprs[2] = regs->orig_gpr2;
578 				regs->psw.addr -= 2;
579 		}
580 	}
581 
582 	/* Set up the stack frame */
583 	if (ka->sa.sa_flags & SA_SIGINFO)
584 		setup_rt_frame32(sig, ka, info, oldset, regs);
585 	else
586 		setup_frame32(sig, ka, oldset, regs);
587 
588 	if (ka->sa.sa_flags & SA_ONESHOT)
589 		ka->sa.sa_handler = SIG_DFL;
590 
591 	if (!(ka->sa.sa_flags & SA_NODEFER)) {
592 		spin_lock_irq(&current->sigmask_lock);
593 		sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
594 		sigaddset(&current->blocked,sig);
595 		recalc_sigpending(current);
596 		spin_unlock_irq(&current->sigmask_lock);
597 	}
598 }
599 
600 /*
601  * Note that 'init' is a special process: it doesn't get signals it doesn't
602  * want to handle. Thus you cannot kill init even with a SIGKILL even by
603  * mistake.
604  *
605  * Note that we go through the signals twice: once to check the signals that
606  * the kernel can handle, and then we build all the user-level signal handling
607  * stack-frames in one go after that.
608  */
do_signal32(struct pt_regs * regs,sigset_t * oldset)609 int do_signal32(struct pt_regs *regs, sigset_t *oldset)
610 {
611 	siginfo_t info;
612 	struct k_sigaction *ka;
613 
614 	/*
615 	 * We want the common case to go fast, which
616 	 * is why we may in certain cases get here from
617 	 * kernel mode. Just return without doing anything
618 	 * if so.
619 	 */
620 	if (!user_mode(regs))
621 		return 1;
622 
623 	if (!oldset)
624 		oldset = &current->blocked;
625 
626 	for (;;) {
627 		unsigned long signr;
628 
629 		spin_lock_irq(&current->sigmask_lock);
630 		signr = dequeue_signal(&current->blocked, &info);
631 		spin_unlock_irq(&current->sigmask_lock);
632 
633 		if (!signr)
634 			break;
635 
636 		if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
637 			/* Let the debugger run.  */
638 			current->exit_code = signr;
639 			set_current_state(TASK_STOPPED);
640 			notify_parent(current, SIGCHLD);
641 			schedule();
642 
643 			/* We're back.  Did the debugger cancel the sig?  */
644 			if (!(signr = current->exit_code))
645 				continue;
646 			current->exit_code = 0;
647 
648 			/* The debugger continued.  Ignore SIGSTOP.  */
649 			if (signr == SIGSTOP)
650 				continue;
651 
652 			/* Update the siginfo structure.  Is this good?  */
653 			if (signr != info.si_signo) {
654 				info.si_signo = signr;
655 				info.si_errno = 0;
656 				info.si_code = SI_USER;
657 				info.si_pid = current->p_pptr->pid;
658 				info.si_uid = current->p_pptr->uid;
659 			}
660 
661 			/* If the (new) signal is now blocked, requeue it.  */
662 			if (sigismember(&current->blocked, signr)) {
663 				send_sig_info(signr, &info, current);
664 				continue;
665 			}
666 		}
667 
668 		ka = &current->sig->action[signr-1];
669 		if (ka->sa.sa_handler == SIG_IGN) {
670 			if (signr != SIGCHLD)
671 				continue;
672 			/* Check for SIGCHLD: it's special.  */
673 			while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
674 				/* nothing */;
675 			continue;
676 		}
677 
678 		if (ka->sa.sa_handler == SIG_DFL) {
679 			int exit_code = signr;
680 
681 			/* Init gets no signals it doesn't want.  */
682 			if (current->pid == 1)
683 				continue;
684 
685 			switch (signr) {
686 			case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
687 				continue;
688 
689 			case SIGTSTP: case SIGTTIN: case SIGTTOU:
690 				if (is_orphaned_pgrp(current->pgrp))
691 					continue;
692 				/* FALLTHRU */
693 
694 			case SIGSTOP:
695 				set_current_state(TASK_STOPPED);
696 				current->exit_code = signr;
697 				if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
698 					notify_parent(current, SIGCHLD);
699 				schedule();
700 				continue;
701 
702 			case SIGQUIT: case SIGILL: case SIGTRAP:
703 			case SIGABRT: case SIGFPE: case SIGSEGV:
704 			case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
705                                 if (do_coredump(signr, regs))
706                                         exit_code |= 0x80;
707                                 /* FALLTHRU */
708 
709 			default:
710 				sig_exit(signr, exit_code, &info);
711 				/* NOTREACHED */
712 			}
713 		}
714 
715 		/* Whee!  Actually deliver the signal.  */
716 		handle_signal32(signr, ka, &info, oldset, regs);
717 		return 1;
718 	}
719 
720 	/* Did we come from a system call? */
721 	if ( regs->trap == __LC_SVC_OLD_PSW /* System Call! */ ) {
722 		/* Restart the system call - no handlers present */
723 		if (regs->gprs[2] == -ERESTARTNOHAND ||
724 		    regs->gprs[2] == -ERESTARTSYS ||
725 		    regs->gprs[2] == -ERESTARTNOINTR) {
726 			regs->gprs[2] = regs->orig_gpr2;
727 			regs->psw.addr -= 2;
728 		}
729 	}
730 	return 0;
731 }
732