1 /*  $Id: signal.c,v 1.108 2001/01/24 21:05:12 davem Exp $
2  *  linux/arch/sparc/kernel/signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6  *  Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
7  *  Copyright (C) 1997 Eddie C. Dost   (ecd@skynet.be)
8  */
9 
10 #include <linux/config.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 
22 #include <asm/uaccess.h>
23 #include <asm/bitops.h>
24 #include <asm/ptrace.h>
25 #include <asm/svr4.h>
26 #include <asm/pgalloc.h>
27 #include <asm/pgtable.h>
28 
29 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
30 
31 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
32 		   void *fpqueue, unsigned long *fpqdepth);
33 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
34 
35 asmlinkage int do_signal(sigset_t *oldset, struct pt_regs * regs,
36 			 unsigned long orig_o0, int ret_from_syscall);
37 
38 /* This turned off for production... */
39 /* #define DEBUG_SIGNALS 1 */
40 /* #define DEBUG_SIGNALS_TRACE 1 */
41 /* #define DEBUG_SIGNALS_MAPS 1 */
42 
43 /* Signal frames: the original one (compatible with SunOS):
44  *
45  * Set up a signal frame... Make the stack look the way SunOS
46  * expects it to look which is basically:
47  *
48  * ---------------------------------- <-- %sp at signal time
49  * Struct sigcontext
50  * Signal address
51  * Ptr to sigcontext area above
52  * Signal code
53  * The signal number itself
54  * One register window
55  * ---------------------------------- <-- New %sp
56  */
57 struct signal_sframe {
58 	struct reg_window	sig_window;
59 	int			sig_num;
60 	int			sig_code;
61 	struct sigcontext	*sig_scptr;
62 	int			sig_address;
63 	struct sigcontext	sig_context;
64 	unsigned int		extramask[_NSIG_WORDS - 1];
65 };
66 
67 /*
68  * And the new one, intended to be used for Linux applications only
69  * (we have enough in there to work with clone).
70  * All the interesting bits are in the info field.
71  */
72 
73 struct new_signal_frame {
74 	struct sparc_stackf	ss;
75 	__siginfo_t		info;
76 	__siginfo_fpu_t		*fpu_save;
77 	unsigned long		insns [2] __attribute__ ((aligned (8)));
78 	unsigned int		extramask[_NSIG_WORDS - 1];
79 	unsigned int		extra_size; /* Should be 0 */
80 	__siginfo_fpu_t		fpu_state;
81 };
82 
83 struct rt_signal_frame {
84 	struct sparc_stackf	ss;
85 	siginfo_t		info;
86 	struct pt_regs		regs;
87 	sigset_t		mask;
88 	__siginfo_fpu_t		*fpu_save;
89 	unsigned int		insns [2];
90 	stack_t			stack;
91 	unsigned int		extra_size; /* Should be 0 */
92 	__siginfo_fpu_t		fpu_state;
93 };
94 
copy_siginfo_to_user(siginfo_t * to,siginfo_t * from)95 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
96 {
97 	if (!access_ok(VERIFY_WRITE, to, sizeof(siginfo_t)))
98 		return -EFAULT;
99 	if (from->si_code < 0)
100 		return __copy_to_user(to, from, sizeof(siginfo_t));
101 	else {
102 		int err;
103 
104 		/* If you change siginfo_t structure, please be sure
105 		   this code is fixed accordingly.
106 		   It should never copy any pad contained in the structure
107 		   to avoid security leaks, but must copy the generic
108 		   3 ints plus the relevant union member.  */
109 		err = __put_user(from->si_signo, &to->si_signo);
110 		err |= __put_user(from->si_errno, &to->si_errno);
111 		err |= __put_user((short)from->si_code, &to->si_code);
112 		switch (from->si_code >> 16) {
113 		case __SI_CHLD >> 16:
114 			err |= __put_user(from->si_utime, &to->si_utime);
115 			err |= __put_user(from->si_stime, &to->si_stime);
116 		/* case __SI_RT: This is not generated by the kernel as of now.  */
117 			err |= __put_user(from->si_status, &to->si_status);
118 		default:
119 			err |= __put_user(from->si_uid, &to->si_uid);
120 			err |= __put_user(from->si_pid, &to->si_pid);
121 			break;
122 		}
123 		return err;
124 	}
125 }
126 
127 /* Align macros */
128 #define SF_ALIGNEDSZ  (((sizeof(struct signal_sframe) + 7) & (~7)))
129 #define NF_ALIGNEDSZ  (((sizeof(struct new_signal_frame) + 7) & (~7)))
130 #define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame) + 7) & (~7)))
131 
132 /*
133  * atomically swap in the new signal mask, and wait for a signal.
134  * This is really tricky on the Sparc, watch out...
135  */
_sigpause_common(old_sigset_t set,struct pt_regs * regs)136 asmlinkage void _sigpause_common(old_sigset_t set, struct pt_regs *regs)
137 {
138 	sigset_t saveset;
139 
140 	set &= _BLOCKABLE;
141 	spin_lock_irq(&current->sigmask_lock);
142 	saveset = current->blocked;
143 	siginitset(&current->blocked, set);
144 	recalc_sigpending(current);
145 	spin_unlock_irq(&current->sigmask_lock);
146 
147 	regs->pc = regs->npc;
148 	regs->npc += 4;
149 
150 	/* Condition codes and return value where set here for sigpause,
151 	 * and so got used by setup_frame, which again causes sigreturn()
152 	 * to return -EINTR.
153 	 */
154 	while (1) {
155 		current->state = TASK_INTERRUPTIBLE;
156 		schedule();
157 		/*
158 		 * Return -EINTR and set condition code here,
159 		 * so the interrupted system call actually returns
160 		 * these.
161 		 */
162 		regs->psr |= PSR_C;
163 		regs->u_regs[UREG_I0] = EINTR;
164 		if (do_signal(&saveset, regs, 0, 0))
165 			return;
166 	}
167 }
168 
do_sigpause(unsigned int set,struct pt_regs * regs)169 asmlinkage void do_sigpause(unsigned int set, struct pt_regs *regs)
170 {
171 	_sigpause_common(set, regs);
172 }
173 
do_sigsuspend(struct pt_regs * regs)174 asmlinkage void do_sigsuspend (struct pt_regs *regs)
175 {
176 	_sigpause_common(regs->u_regs[UREG_I0], regs);
177 }
178 
do_rt_sigsuspend(sigset_t * uset,size_t sigsetsize,struct pt_regs * regs)179 asmlinkage void do_rt_sigsuspend(sigset_t *uset, size_t sigsetsize,
180 				 struct pt_regs *regs)
181 {
182 	sigset_t oldset, set;
183 
184 	/* XXX: Don't preclude handling different sized sigset_t's.  */
185 	if (sigsetsize != sizeof(sigset_t)) {
186 		regs->psr |= PSR_C;
187 		regs->u_regs[UREG_I0] = EINVAL;
188 		return;
189 	}
190 
191 	if (copy_from_user(&set, uset, sizeof(set))) {
192 		regs->psr |= PSR_C;
193 		regs->u_regs[UREG_I0] = EFAULT;
194 		return;
195 	}
196 
197 	sigdelsetmask(&set, ~_BLOCKABLE);
198 	spin_lock_irq(&current->sigmask_lock);
199 	oldset = current->blocked;
200 	current->blocked = set;
201 	recalc_sigpending(current);
202 	spin_unlock_irq(&current->sigmask_lock);
203 
204 	regs->pc = regs->npc;
205 	regs->npc += 4;
206 
207 	/* Condition codes and return value where set here for sigpause,
208 	 * and so got used by setup_frame, which again causes sigreturn()
209 	 * to return -EINTR.
210 	 */
211 	while (1) {
212 		current->state = TASK_INTERRUPTIBLE;
213 		schedule();
214 		/*
215 		 * Return -EINTR and set condition code here,
216 		 * so the interrupted system call actually returns
217 		 * these.
218 		 */
219 		regs->psr |= PSR_C;
220 		regs->u_regs[UREG_I0] = EINTR;
221 		if (do_signal(&oldset, regs, 0, 0))
222 			return;
223 	}
224 }
225 
226 static inline int
restore_fpu_state(struct pt_regs * regs,__siginfo_fpu_t * fpu)227 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t *fpu)
228 {
229 	int err;
230 #ifdef CONFIG_SMP
231 	if (current->flags & PF_USEDFPU)
232 		regs->psr &= ~PSR_EF;
233 #else
234 	if (current == last_task_used_math) {
235 		last_task_used_math = 0;
236 		regs->psr &= ~PSR_EF;
237 	}
238 #endif
239 	current->used_math = 1;
240 	current->flags &= ~PF_USEDFPU;
241 
242 	if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
243 		return -EFAULT;
244 
245 	err = __copy_from_user(&current->thread.float_regs[0], &fpu->si_float_regs[0],
246 			       (sizeof(unsigned long) * 32));
247 	err |= __get_user(current->thread.fsr, &fpu->si_fsr);
248 	err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
249 	if (current->thread.fpqdepth != 0)
250 		err |= __copy_from_user(&current->thread.fpqueue[0],
251 					&fpu->si_fpqueue[0],
252 					((sizeof(unsigned long) +
253 					(sizeof(unsigned long *)))*16));
254 	return err;
255 }
256 
do_new_sigreturn(struct pt_regs * regs)257 static inline void do_new_sigreturn (struct pt_regs *regs)
258 {
259 	struct new_signal_frame *sf;
260 	unsigned long up_psr, pc, npc;
261 	sigset_t set;
262 	__siginfo_fpu_t *fpu_save;
263 	int err;
264 
265 	sf = (struct new_signal_frame *) regs->u_regs [UREG_FP];
266 
267 	/* 1. Make sure we are not getting garbage from the user */
268 	if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
269 		goto segv_and_exit;
270 
271 	if (((uint) sf) & 3)
272 		goto segv_and_exit;
273 
274 	err = __get_user(pc,  &sf->info.si_regs.pc);
275 	err |= __get_user(npc, &sf->info.si_regs.npc);
276 
277 	if ((pc | npc) & 3)
278 		goto segv_and_exit;
279 
280 	/* 2. Restore the state */
281 	up_psr = regs->psr;
282 	err |= __copy_from_user(regs, &sf->info.si_regs, sizeof (struct pt_regs));
283 
284 	/* User can only change condition codes and FPU enabling in %psr. */
285 	regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
286 		  | (regs->psr & (PSR_ICC | PSR_EF));
287 
288 	err |= __get_user(fpu_save, &sf->fpu_save);
289 
290 	if (fpu_save)
291 		err |= restore_fpu_state(regs, fpu_save);
292 
293 	/* This is pretty much atomic, no amount locking would prevent
294 	 * the races which exist anyways.
295 	 */
296 	err |= __get_user(set.sig[0], &sf->info.si_mask);
297 	err |= __copy_from_user(&set.sig[1], &sf->extramask,
298 			        (_NSIG_WORDS-1) * sizeof(unsigned int));
299 
300 	if (err)
301 		goto segv_and_exit;
302 
303 	sigdelsetmask(&set, ~_BLOCKABLE);
304 	spin_lock_irq(&current->sigmask_lock);
305 	current->blocked = set;
306 	recalc_sigpending(current);
307 	spin_unlock_irq(&current->sigmask_lock);
308 	return;
309 
310 segv_and_exit:
311 	do_exit(SIGSEGV);
312 }
313 
do_sigreturn(struct pt_regs * regs)314 asmlinkage void do_sigreturn(struct pt_regs *regs)
315 {
316 	struct sigcontext *scptr;
317 	unsigned long pc, npc, psr;
318 	sigset_t set;
319 	int err;
320 
321 	synchronize_user_stack();
322 
323 	if (current->thread.new_signal)
324 		return do_new_sigreturn (regs);
325 
326 	scptr = (struct sigcontext *) regs->u_regs[UREG_I0];
327 
328 	/* Check sanity of the user arg. */
329 	if(verify_area(VERIFY_READ, scptr, sizeof(struct sigcontext)) ||
330 	   (((unsigned long) scptr) & 3))
331 		goto segv_and_exit;
332 
333 	err = __get_user(pc, &scptr->sigc_pc);
334 	err |= __get_user(npc, &scptr->sigc_npc);
335 
336 	if((pc | npc) & 3)
337 		goto segv_and_exit;
338 
339 	/* This is pretty much atomic, no amount locking would prevent
340 	 * the races which exist anyways.
341 	 */
342 	err |= __get_user(set.sig[0], &scptr->sigc_mask);
343 	/* Note that scptr + 1 points to extramask */
344 	err |= __copy_from_user(&set.sig[1], scptr + 1,
345 				(_NSIG_WORDS - 1) * sizeof(unsigned int));
346 
347 	if (err)
348 		goto segv_and_exit;
349 
350 	sigdelsetmask(&set, ~_BLOCKABLE);
351 	spin_lock_irq(&current->sigmask_lock);
352 	current->blocked = set;
353 	recalc_sigpending(current);
354 	spin_unlock_irq(&current->sigmask_lock);
355 
356 	regs->pc = pc;
357 	regs->npc = npc;
358 
359 	err = __get_user(regs->u_regs[UREG_FP], &scptr->sigc_sp);
360 	err |= __get_user(regs->u_regs[UREG_I0], &scptr->sigc_o0);
361 	err |= __get_user(regs->u_regs[UREG_G1], &scptr->sigc_g1);
362 
363 	/* User can only change condition codes in %psr. */
364 	err |= __get_user(psr, &scptr->sigc_psr);
365 	if (err)
366 		goto segv_and_exit;
367 
368 	regs->psr &= ~(PSR_ICC);
369 	regs->psr |= (psr & PSR_ICC);
370 	return;
371 
372 segv_and_exit:
373 	send_sig(SIGSEGV, current, 1);
374 }
375 
do_rt_sigreturn(struct pt_regs * regs)376 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
377 {
378 	struct rt_signal_frame *sf;
379 	unsigned int psr, pc, npc;
380 	__siginfo_fpu_t *fpu_save;
381 	sigset_t set;
382 	stack_t st;
383 	int err;
384 
385 	synchronize_user_stack();
386 	sf = (struct rt_signal_frame *) regs->u_regs[UREG_FP];
387 	if(verify_area(VERIFY_READ, sf, sizeof(*sf)) ||
388 	   (((unsigned long) sf) & 0x03))
389 		goto segv;
390 
391 	err = __get_user(pc, &sf->regs.pc);
392 	err |= __get_user(npc, &sf->regs.npc);
393 	err |= ((pc | npc) & 0x03);
394 
395 	err |= __get_user(regs->y, &sf->regs.y);
396 	err |= __get_user(psr, &sf->regs.psr);
397 
398 	err |= __copy_from_user(&regs->u_regs[UREG_G1], &sf->regs.u_regs[UREG_G1], 15*sizeof(u32));
399 
400 	regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
401 
402 	err |= __get_user(fpu_save, &sf->fpu_save);
403 
404 	if(fpu_save)
405 		err |= restore_fpu_state(regs, fpu_save);
406 	err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
407 
408 	err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
409 
410 	if (err)
411 		goto segv;
412 
413 	regs->pc = pc;
414 	regs->npc = npc;
415 
416 	/* It is more difficult to avoid calling this function than to
417 	   call it and ignore errors.  */
418 	do_sigaltstack(&st, NULL, (unsigned long)sf);
419 
420 	sigdelsetmask(&set, ~_BLOCKABLE);
421 	spin_lock_irq(&current->sigmask_lock);
422 	current->blocked = set;
423 	recalc_sigpending(current);
424 	spin_unlock_irq(&current->sigmask_lock);
425 	return;
426 segv:
427 	send_sig(SIGSEGV, current, 1);
428 }
429 
430 /* Checks if the fp is valid */
invalid_frame_pointer(void * fp,int fplen)431 static inline int invalid_frame_pointer (void *fp, int fplen)
432 {
433 	if ((((unsigned long) fp) & 7) ||
434 	    !__access_ok((unsigned long)fp, fplen) ||
435 	    ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
436 	     ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
437 		return 1;
438 
439 	return 0;
440 }
441 
get_sigframe(struct sigaction * sa,struct pt_regs * regs,unsigned long framesize)442 static inline void *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
443 {
444 	unsigned long sp;
445 
446 	sp = regs->u_regs[UREG_FP];
447 
448 	/* This is the X/Open sanctioned signal stack switching.  */
449 	if (sa->sa_flags & SA_ONSTACK) {
450 		if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
451 			sp = current->sas_ss_sp + current->sas_ss_size;
452 	}
453 	return (void *)(sp - framesize);
454 }
455 
456 static inline void
setup_frame(struct sigaction * sa,struct pt_regs * regs,int signr,sigset_t * oldset,siginfo_t * info)457 setup_frame(struct sigaction *sa, struct pt_regs *regs, int signr, sigset_t *oldset, siginfo_t *info)
458 {
459 	struct signal_sframe *sframep;
460 	struct sigcontext *sc;
461 	int window = 0, err;
462 	unsigned long pc = regs->pc;
463 	unsigned long npc = regs->npc;
464 	void *sig_address;
465 	int sig_code;
466 
467 	synchronize_user_stack();
468 	sframep = (struct signal_sframe *)get_sigframe(sa, regs, SF_ALIGNEDSZ);
469 	if (invalid_frame_pointer (sframep, sizeof(*sframep))){
470 #ifdef DEBUG_SIGNALS /* fills up the console logs during crashme runs, yuck... */
471 		printk("%s [%d]: User has trashed signal stack\n",
472 		       current->comm, current->pid);
473 		printk("Sigstack ptr %p handler at pc<%08lx> for sig<%d>\n",
474 		       sframep, pc, signr);
475 #endif
476 		/* Don't change signal code and address, so that
477 		 * post mortem debuggers can have a look.
478 		 */
479 		goto sigill_and_return;
480 	}
481 
482 	sc = &sframep->sig_context;
483 
484 	/* We've already made sure frame pointer isn't in kernel space... */
485 	err  = __put_user((sas_ss_flags(regs->u_regs[UREG_FP]) == SS_ONSTACK),
486 			 &sc->sigc_onstack);
487 	err |= __put_user(oldset->sig[0], &sc->sigc_mask);
488 	err |= __copy_to_user(sframep->extramask, &oldset->sig[1],
489 			      (_NSIG_WORDS - 1) * sizeof(unsigned int));
490 	err |= __put_user(regs->u_regs[UREG_FP], &sc->sigc_sp);
491 	err |= __put_user(pc, &sc->sigc_pc);
492 	err |= __put_user(npc, &sc->sigc_npc);
493 	err |= __put_user(regs->psr, &sc->sigc_psr);
494 	err |= __put_user(regs->u_regs[UREG_G1], &sc->sigc_g1);
495 	err |= __put_user(regs->u_regs[UREG_I0], &sc->sigc_o0);
496 	err |= __put_user(current->thread.w_saved, &sc->sigc_oswins);
497 	if(current->thread.w_saved)
498 		for(window = 0; window < current->thread.w_saved; window++) {
499 			sc->sigc_spbuf[window] =
500 				(char *)current->thread.rwbuf_stkptrs[window];
501 			err |= __copy_to_user(&sc->sigc_wbuf[window],
502 					      &current->thread.reg_window[window],
503 					      sizeof(struct reg_window));
504 		}
505 	else
506 		err |= __copy_to_user(sframep, (char *)regs->u_regs[UREG_FP],
507 				      sizeof(struct reg_window));
508 
509 	current->thread.w_saved = 0; /* So process is allowed to execute. */
510 
511 	err |= __put_user(signr, &sframep->sig_num);
512 	sig_address = NULL;
513 	sig_code = 0;
514 	if (SI_FROMKERNEL (info) && (info->si_code & __SI_MASK) == __SI_FAULT) {
515 		sig_address = info->si_addr;
516 		switch (signr) {
517 		case SIGSEGV:
518 			switch (info->si_code) {
519 			case SEGV_MAPERR: sig_code = SUBSIG_NOMAPPING; break;
520 			default: sig_code = SUBSIG_PROTECTION; break;
521 			}
522 			break;
523 		case SIGILL:
524 			switch (info->si_code) {
525 			case ILL_ILLOPC: sig_code = SUBSIG_ILLINST; break;
526 			case ILL_PRVOPC: sig_code = SUBSIG_PRIVINST; break;
527 			case ILL_ILLTRP: sig_code = SUBSIG_BADTRAP (info->si_trapno); break;
528 			default: sig_code = SUBSIG_STACK; break;
529 			}
530 			break;
531 		case SIGFPE:
532 			switch (info->si_code) {
533 			case FPE_INTDIV: sig_code = SUBSIG_IDIVZERO; break;
534 			case FPE_INTOVF: sig_code = SUBSIG_FPINTOVFL; break;
535 			case FPE_FLTDIV: sig_code = SUBSIG_FPDIVZERO; break;
536 			case FPE_FLTOVF: sig_code = SUBSIG_FPOVFLOW; break;
537 			case FPE_FLTUND: sig_code = SUBSIG_FPUNFLOW; break;
538 			case FPE_FLTRES: sig_code = SUBSIG_FPINEXACT; break;
539 			case FPE_FLTINV: sig_code = SUBSIG_FPOPERROR; break;
540 			default: sig_code = SUBSIG_FPERROR; break;
541 			}
542 			break;
543 		case SIGBUS:
544 			switch (info->si_code) {
545 			case BUS_ADRALN: sig_code = SUBSIG_ALIGNMENT; break;
546 			case BUS_ADRERR: sig_code = SUBSIG_MISCERROR; break;
547 			default: sig_code = SUBSIG_BUSTIMEOUT; break;
548 			}
549 			break;
550 		case SIGEMT:
551 			switch (info->si_code) {
552 			case EMT_TAGOVF: sig_code = SUBSIG_TAG; break;
553 			}
554 			break;
555 		case SIGSYS:
556 			if (info->si_code == (__SI_FAULT|0x100)) {
557 				/* See sys_sunos.c */
558 				sig_code = info->si_trapno;
559 				break;
560 			}
561 		default:
562 			sig_address = NULL;
563 		}
564 	}
565 	err |= __put_user((long)sig_address, &sframep->sig_address);
566 	err |= __put_user(sig_code, &sframep->sig_code);
567 	err |= __put_user(sc, &sframep->sig_scptr);
568 	if (err)
569 		goto sigsegv;
570 
571 	regs->u_regs[UREG_FP] = (unsigned long) sframep;
572 	regs->pc = (unsigned long) sa->sa_handler;
573 	regs->npc = (regs->pc + 4);
574 	return;
575 
576 sigill_and_return:
577 	do_exit(SIGILL);
578 sigsegv:
579 	do_exit(SIGSEGV);
580 }
581 
582 
583 static inline int
save_fpu_state(struct pt_regs * regs,__siginfo_fpu_t * fpu)584 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t *fpu)
585 {
586 	int err = 0;
587 #ifdef CONFIG_SMP
588 	if (current->flags & PF_USEDFPU) {
589 		put_psr(get_psr() | PSR_EF);
590 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
591 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
592 		regs->psr &= ~(PSR_EF);
593 		current->flags &= ~(PF_USEDFPU);
594 	}
595 #else
596 	if (current == last_task_used_math) {
597 		put_psr(get_psr() | PSR_EF);
598 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
599 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
600 		last_task_used_math = 0;
601 		regs->psr &= ~(PSR_EF);
602 	}
603 #endif
604 	err |= __copy_to_user(&fpu->si_float_regs[0], &current->thread.float_regs[0],
605 			      (sizeof(unsigned long) * 32));
606 	err |= __put_user(current->thread.fsr, &fpu->si_fsr);
607 	err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
608 	if (current->thread.fpqdepth != 0)
609 		err |= __copy_to_user(&fpu->si_fpqueue[0], &current->thread.fpqueue[0],
610 				      ((sizeof(unsigned long) +
611 				      (sizeof(unsigned long *)))*16));
612 	current->used_math = 0;
613 	return err;
614 }
615 
616 static inline void
new_setup_frame(struct k_sigaction * ka,struct pt_regs * regs,int signo,sigset_t * oldset)617 new_setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
618 		int signo, sigset_t *oldset)
619 {
620 	struct new_signal_frame *sf;
621 	int sigframe_size, err;
622 
623 	/* 1. Make sure everything is clean */
624 	synchronize_user_stack();
625 
626 	sigframe_size = NF_ALIGNEDSZ;
627 	if (!current->used_math)
628 		sigframe_size -= sizeof(__siginfo_fpu_t);
629 
630 	sf = (struct new_signal_frame *)get_sigframe(&ka->sa, regs, sigframe_size);
631 
632 	if (invalid_frame_pointer (sf, sigframe_size))
633 		goto sigill_and_return;
634 
635 	if (current->thread.w_saved != 0) {
636 #ifdef DEBUG_SIGNALS
637 		printk ("%s [%d]: Invalid user stack frame for "
638 			"signal delivery.\n", current->comm, current->pid);
639 #endif
640 		goto sigill_and_return;
641 	}
642 
643 	/* 2. Save the current process state */
644 	err = __copy_to_user(&sf->info.si_regs, regs, sizeof (struct pt_regs));
645 
646 	err |= __put_user(0, &sf->extra_size);
647 
648 	if (current->used_math) {
649 		err |= save_fpu_state(regs, &sf->fpu_state);
650 		err |= __put_user(&sf->fpu_state, &sf->fpu_save);
651 	} else {
652 		err |= __put_user(0, &sf->fpu_save);
653 	}
654 
655 	err |= __put_user(oldset->sig[0], &sf->info.si_mask);
656 	err |= __copy_to_user(sf->extramask, &oldset->sig[1],
657 			      (_NSIG_WORDS - 1) * sizeof(unsigned int));
658 	err |= __copy_to_user(sf, (char *) regs->u_regs [UREG_FP],
659 			      sizeof (struct reg_window));
660 	if (err)
661 		goto sigsegv;
662 
663 	/* 3. signal handler back-trampoline and parameters */
664 	regs->u_regs[UREG_FP] = (unsigned long) sf;
665 	regs->u_regs[UREG_I0] = signo;
666 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
667 	regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
668 
669 	/* 4. signal handler */
670 	regs->pc = (unsigned long) ka->sa.sa_handler;
671 	regs->npc = (regs->pc + 4);
672 
673 	/* 5. return to kernel instructions */
674 	if (ka->ka_restorer)
675 		regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
676 	else {
677 		regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
678 
679 		/* mov __NR_sigreturn, %g1 */
680 		err |= __put_user(0x821020d8, &sf->insns[0]);
681 
682 		/* t 0x10 */
683 		err |= __put_user(0x91d02010, &sf->insns[1]);
684 		if (err)
685 			goto sigsegv;
686 
687 		/* Flush instruction space. */
688 		flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
689 	}
690 	return;
691 
692 sigill_and_return:
693 	do_exit(SIGILL);
694 sigsegv:
695 	do_exit(SIGSEGV);
696 }
697 
698 static inline void
new_setup_rt_frame(struct k_sigaction * ka,struct pt_regs * regs,int signo,sigset_t * oldset,siginfo_t * info)699 new_setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
700 		   int signo, sigset_t *oldset, siginfo_t *info)
701 {
702 	struct rt_signal_frame *sf;
703 	int sigframe_size;
704 	unsigned int psr;
705 	int err;
706 
707 	synchronize_user_stack();
708 	sigframe_size = RT_ALIGNEDSZ;
709 	if(!current->used_math)
710 		sigframe_size -= sizeof(__siginfo_fpu_t);
711 	sf = (struct rt_signal_frame *)get_sigframe(&ka->sa, regs, sigframe_size);
712 	if(invalid_frame_pointer(sf, sigframe_size))
713 		goto sigill;
714 	if(current->thread.w_saved != 0)
715 		goto sigill;
716 
717 	err  = __put_user(regs->pc, &sf->regs.pc);
718 	err |= __put_user(regs->npc, &sf->regs.npc);
719 	err |= __put_user(regs->y, &sf->regs.y);
720 	psr = regs->psr;
721 	if(current->used_math)
722 		psr |= PSR_EF;
723 	err |= __put_user(psr, &sf->regs.psr);
724 	err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
725 	err |= __put_user(0, &sf->extra_size);
726 
727 	if(psr & PSR_EF) {
728 		err |= save_fpu_state(regs, &sf->fpu_state);
729 		err |= __put_user(&sf->fpu_state, &sf->fpu_save);
730 	} else {
731 		err |= __put_user(0, &sf->fpu_save);
732 	}
733 	err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
734 
735 	/* Setup sigaltstack */
736 	err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
737 	err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
738 	err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
739 
740 	err |= __copy_to_user(sf, (char *) regs->u_regs [UREG_FP],
741 			      sizeof (struct reg_window));
742 
743 	err |= copy_siginfo_to_user(&sf->info, info);
744 
745 	if (err)
746 		goto sigsegv;
747 
748 	regs->u_regs[UREG_FP] = (unsigned long) sf;
749 	regs->u_regs[UREG_I0] = signo;
750 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
751 	regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
752 
753 	regs->pc = (unsigned long) ka->sa.sa_handler;
754 	regs->npc = (regs->pc + 4);
755 
756 	if(ka->ka_restorer)
757 		regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
758 	else {
759 		regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
760 
761 		/* mov __NR_sigreturn, %g1 */
762 		err |= __put_user(0x821020d8, &sf->insns[0]);
763 
764 		/* t 0x10 */
765 		err |= __put_user(0x91d02010, &sf->insns[1]);
766 		if (err)
767 			goto sigsegv;
768 
769 		/* Flush instruction space. */
770 		flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
771 	}
772 	return;
773 
774 sigill:
775 	do_exit(SIGILL);
776 sigsegv:
777 	do_exit(SIGSEGV);
778 }
779 
780 /* Setup a Solaris stack frame */
781 static inline void
setup_svr4_frame(struct sigaction * sa,unsigned long pc,unsigned long npc,struct pt_regs * regs,int signr,sigset_t * oldset)782 setup_svr4_frame(struct sigaction *sa, unsigned long pc, unsigned long npc,
783 		 struct pt_regs *regs, int signr, sigset_t *oldset)
784 {
785 	svr4_signal_frame_t *sfp;
786 	svr4_gregset_t  *gr;
787 	svr4_siginfo_t  *si;
788 	svr4_mcontext_t *mc;
789 	svr4_gwindows_t *gw;
790 	svr4_ucontext_t *uc;
791 	svr4_sigset_t	setv;
792 	int window = 0, err;
793 
794 	synchronize_user_stack();
795 	sfp = (svr4_signal_frame_t *) get_sigframe(sa, regs, SVR4_SF_ALIGNED + sizeof(struct reg_window));
796 
797 	if (invalid_frame_pointer (sfp, sizeof (*sfp))){
798 #ifdef DEBUG_SIGNALS
799 		printk ("Invalid stack frame\n");
800 #endif
801 		goto sigill_and_return;
802 	}
803 
804 	/* Start with a clean frame pointer and fill it */
805 	err = __clear_user(sfp, sizeof (*sfp));
806 
807 	/* Setup convenience variables */
808 	si = &sfp->si;
809 	uc = &sfp->uc;
810 	gw = &sfp->gw;
811 	mc = &uc->mcontext;
812 	gr = &mc->greg;
813 
814 	/* FIXME: where am I supposed to put this?
815 	 * sc->sigc_onstack = old_status;
816 	 * anyways, it does not look like it is used for anything at all.
817 	 */
818 	setv.sigbits[0] = oldset->sig[0];
819 	setv.sigbits[1] = oldset->sig[1];
820 	if (_NSIG_WORDS >= 4) {
821 		setv.sigbits[2] = oldset->sig[2];
822 		setv.sigbits[3] = oldset->sig[3];
823 		err |= __copy_to_user(&uc->sigmask, &setv, sizeof(svr4_sigset_t));
824 	} else
825 		err |= __copy_to_user(&uc->sigmask, &setv, 2 * sizeof(unsigned int));
826 
827 	/* Store registers */
828 	err |= __put_user(regs->pc, &((*gr) [SVR4_PC]));
829 	err |= __put_user(regs->npc, &((*gr) [SVR4_NPC]));
830 	err |= __put_user(regs->psr, &((*gr) [SVR4_PSR]));
831 	err |= __put_user(regs->y, &((*gr) [SVR4_Y]));
832 
833 	/* Copy g [1..7] and o [0..7] registers */
834 	err |= __copy_to_user(&(*gr)[SVR4_G1], &regs->u_regs [UREG_G1], sizeof (long) * 7);
835 	err |= __copy_to_user(&(*gr)[SVR4_O0], &regs->u_regs [UREG_I0], sizeof (long) * 8);
836 
837 	/* Setup sigaltstack */
838 	err |= __put_user(current->sas_ss_sp, &uc->stack.sp);
839 	err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &uc->stack.flags);
840 	err |= __put_user(current->sas_ss_size, &uc->stack.size);
841 
842 	/* Save the currently window file: */
843 
844 	/* 1. Link sfp->uc->gwins to our windows */
845 	err |= __put_user(gw, &mc->gwin);
846 
847 	/* 2. Number of windows to restore at setcontext (): */
848 	err |= __put_user(current->thread.w_saved, &gw->count);
849 
850 	/* 3. Save each valid window
851 	 *    Currently, it makes a copy of the windows from the kernel copy.
852 	 *    David's code for SunOS, makes the copy but keeps the pointer to
853 	 *    the kernel.  My version makes the pointer point to a userland
854 	 *    copy of those.  Mhm, I wonder if I shouldn't just ignore those
855 	 *    on setcontext and use those that are on the kernel, the signal
856 	 *    handler should not be modyfing those, mhm.
857 	 *
858 	 *    These windows are just used in case synchronize_user_stack failed
859 	 *    to flush the user windows.
860 	 */
861 	for(window = 0; window < current->thread.w_saved; window++) {
862 		err |= __put_user((int *) &(gw->win [window]), &gw->winptr [window]);
863 		err |= __copy_to_user(&gw->win [window],
864 				      &current->thread.reg_window [window],
865 				      sizeof (svr4_rwindow_t));
866 		err |= __put_user(0, gw->winptr [window]);
867 	}
868 
869 	/* 4. We just pay attention to the gw->count field on setcontext */
870 	current->thread.w_saved = 0; /* So process is allowed to execute. */
871 
872 	/* Setup the signal information.  Solaris expects a bunch of
873 	 * information to be passed to the signal handler, we don't provide
874 	 * that much currently, should use siginfo.
875 	 */
876 	err |= __put_user(signr, &si->siginfo.signo);
877 	err |= __put_user(SVR4_SINOINFO, &si->siginfo.code);
878 	if (err)
879 		goto sigsegv;
880 
881 	regs->u_regs[UREG_FP] = (unsigned long) sfp;
882 	regs->pc = (unsigned long) sa->sa_handler;
883 	regs->npc = (regs->pc + 4);
884 
885 #ifdef DEBUG_SIGNALS
886 	printk ("Solaris-frame: %x %x\n", (int) regs->pc, (int) regs->npc);
887 #endif
888 	/* Arguments passed to signal handler */
889 	if (regs->u_regs [14]){
890 		struct reg_window *rw = (struct reg_window *) regs->u_regs [14];
891 
892 		err |= __put_user(signr, &rw->ins [0]);
893 		err |= __put_user(si, &rw->ins [1]);
894 		err |= __put_user(uc, &rw->ins [2]);
895 		err |= __put_user(sfp, &rw->ins [6]);	/* frame pointer */
896 		if (err)
897 			goto sigsegv;
898 
899 		regs->u_regs[UREG_I0] = signr;
900 		regs->u_regs[UREG_I1] = (uint) si;
901 		regs->u_regs[UREG_I2] = (uint) uc;
902 	}
903 	return;
904 
905 sigill_and_return:
906 	do_exit(SIGILL);
907 sigsegv:
908 	do_exit(SIGSEGV);
909 }
910 
svr4_getcontext(svr4_ucontext_t * uc,struct pt_regs * regs)911 asmlinkage int svr4_getcontext (svr4_ucontext_t *uc, struct pt_regs *regs)
912 {
913 	svr4_gregset_t  *gr;
914 	svr4_mcontext_t *mc;
915 	svr4_sigset_t	setv;
916 	int err = 0;
917 
918 	synchronize_user_stack();
919 
920 	if (current->thread.w_saved)
921 		goto sigsegv_and_return;
922 
923 	err = clear_user(uc, sizeof (*uc));
924 	if (err)
925 		return -EFAULT;
926 
927 	/* Setup convenience variables */
928 	mc = &uc->mcontext;
929 	gr = &mc->greg;
930 
931 	setv.sigbits[0] = current->blocked.sig[0];
932 	setv.sigbits[1] = current->blocked.sig[1];
933 	if (_NSIG_WORDS >= 4) {
934 		setv.sigbits[2] = current->blocked.sig[2];
935 		setv.sigbits[3] = current->blocked.sig[3];
936 		err |= __copy_to_user(&uc->sigmask, &setv, sizeof(svr4_sigset_t));
937 	} else
938 		err |= __copy_to_user(&uc->sigmask, &setv, 2 * sizeof(unsigned int));
939 
940 	/* Store registers */
941 	err |= __put_user(regs->pc, &uc->mcontext.greg [SVR4_PC]);
942 	err |= __put_user(regs->npc, &uc->mcontext.greg [SVR4_NPC]);
943 	err |= __put_user(regs->psr, &uc->mcontext.greg [SVR4_PSR]);
944 	err |= __put_user(regs->y, &uc->mcontext.greg [SVR4_Y]);
945 
946 	/* Copy g [1..7] and o [0..7] registers */
947 	err |= __copy_to_user(&(*gr)[SVR4_G1], &regs->u_regs [UREG_G1], sizeof (uint) * 7);
948 	err |= __copy_to_user(&(*gr)[SVR4_O0], &regs->u_regs [UREG_I0], sizeof (uint) * 8);
949 
950 	/* Setup sigaltstack */
951 	err |= __put_user(current->sas_ss_sp, &uc->stack.sp);
952 	err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &uc->stack.flags);
953 	err |= __put_user(current->sas_ss_size, &uc->stack.size);
954 
955 	/* The register file is not saved
956 	 * we have already stuffed all of it with sync_user_stack
957 	 */
958 	return (err ? -EFAULT : 0);
959 
960 sigsegv_and_return:
961 	do_exit(SIGSEGV);
962 }
963 
964 /* Set the context for a svr4 application, this is Solaris way to sigreturn */
svr4_setcontext(svr4_ucontext_t * c,struct pt_regs * regs)965 asmlinkage int svr4_setcontext (svr4_ucontext_t *c, struct pt_regs *regs)
966 {
967 	struct thread_struct *tp = &current->thread;
968 	svr4_gregset_t  *gr;
969 	unsigned long pc, npc, psr;
970 	sigset_t set;
971 	svr4_sigset_t setv;
972 	int err;
973 	stack_t st;
974 
975 	/* Fixme: restore windows, or is this already taken care of in
976 	 * svr4_setup_frame when sync_user_windows is done?
977 	 */
978 	flush_user_windows();
979 
980 	if (tp->w_saved)
981 		goto sigsegv_and_return;
982 
983 	if (((uint) c) & 3)
984 		goto sigsegv_and_return;
985 
986 	if(!__access_ok((unsigned long)c, sizeof(*c)))
987 		goto sigsegv_and_return;
988 
989 	/* Check for valid PC and nPC */
990 	gr = &c->mcontext.greg;
991 	err = __get_user(pc, &((*gr)[SVR4_PC]));
992 	err |= __get_user(npc, &((*gr)[SVR4_NPC]));
993 
994 	if((pc | npc) & 3)
995 		goto sigsegv_and_return;
996 
997 	/* Retrieve information from passed ucontext */
998 	/* note that nPC is ored a 1, this is used to inform entry.S */
999 	/* that we don't want it to mess with our PC and nPC */
1000 
1001 	/* This is pretty much atomic, no amount locking would prevent
1002 	 * the races which exist anyways.
1003 	 */
1004 	err |= __copy_from_user(&setv, &c->sigmask, sizeof(svr4_sigset_t));
1005 
1006 	err |= __get_user(st.ss_sp, &c->stack.sp);
1007 	err |= __get_user(st.ss_flags, &c->stack.flags);
1008 	err |= __get_user(st.ss_size, &c->stack.size);
1009 
1010 	if (err)
1011 		goto sigsegv_and_return;
1012 
1013 	/* It is more difficult to avoid calling this function than to
1014 	   call it and ignore errors.  */
1015 	do_sigaltstack(&st, NULL, regs->u_regs[UREG_I6]);
1016 
1017 	set.sig[0] = setv.sigbits[0];
1018 	set.sig[1] = setv.sigbits[1];
1019 	if (_NSIG_WORDS >= 4) {
1020 		set.sig[2] = setv.sigbits[2];
1021 		set.sig[3] = setv.sigbits[3];
1022 	}
1023 	sigdelsetmask(&set, ~_BLOCKABLE);
1024 	spin_lock_irq(&current->sigmask_lock);
1025 	current->blocked = set;
1026 	recalc_sigpending(current);
1027 	spin_unlock_irq(&current->sigmask_lock);
1028 	regs->pc = pc;
1029 	regs->npc = npc | 1;
1030 	err |= __get_user(regs->y, &((*gr) [SVR4_Y]));
1031 	err |= __get_user(psr, &((*gr) [SVR4_PSR]));
1032 	regs->psr &= ~(PSR_ICC);
1033 	regs->psr |= (psr & PSR_ICC);
1034 
1035 	/* Restore g[1..7] and o[0..7] registers */
1036 	err |= __copy_from_user(&regs->u_regs [UREG_G1], &(*gr)[SVR4_G1],
1037 			      sizeof (long) * 7);
1038 	err |= __copy_from_user(&regs->u_regs [UREG_I0], &(*gr)[SVR4_O0],
1039 			      sizeof (long) * 8);
1040 	return (err ? -EFAULT : 0);
1041 
1042 sigsegv_and_return:
1043 	do_exit(SIGSEGV);
1044 }
1045 
1046 static inline void
handle_signal(unsigned long signr,struct k_sigaction * ka,siginfo_t * info,sigset_t * oldset,struct pt_regs * regs,int svr4_signal)1047 handle_signal(unsigned long signr, struct k_sigaction *ka,
1048 	      siginfo_t *info, sigset_t *oldset, struct pt_regs *regs,
1049 	      int svr4_signal)
1050 {
1051 	if (svr4_signal)
1052 		setup_svr4_frame(&ka->sa, regs->pc, regs->npc, regs, signr, oldset);
1053 	else {
1054 		if (ka->sa.sa_flags & SA_SIGINFO)
1055 			new_setup_rt_frame(ka, regs, signr, oldset, info);
1056 		else if (current->thread.new_signal)
1057 			new_setup_frame (ka, regs, signr, oldset);
1058 		else
1059 			setup_frame(&ka->sa, regs, signr, oldset, info);
1060 	}
1061 	if(ka->sa.sa_flags & SA_ONESHOT)
1062 		ka->sa.sa_handler = SIG_DFL;
1063 	if(!(ka->sa.sa_flags & SA_NOMASK)) {
1064 		spin_lock_irq(&current->sigmask_lock);
1065 		sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
1066 		sigaddset(&current->blocked, signr);
1067 		recalc_sigpending(current);
1068 		spin_unlock_irq(&current->sigmask_lock);
1069 	}
1070 }
1071 
syscall_restart(unsigned long orig_i0,struct pt_regs * regs,struct sigaction * sa)1072 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
1073 				   struct sigaction *sa)
1074 {
1075 	switch(regs->u_regs[UREG_I0]) {
1076 		case ERESTARTNOHAND:
1077 		no_system_call_restart:
1078 			regs->u_regs[UREG_I0] = EINTR;
1079 			regs->psr |= PSR_C;
1080 			break;
1081 		case ERESTARTSYS:
1082 			if(!(sa->sa_flags & SA_RESTART))
1083 				goto no_system_call_restart;
1084 		/* fallthrough */
1085 		case ERESTARTNOINTR:
1086 			regs->u_regs[UREG_I0] = orig_i0;
1087 			regs->pc -= 4;
1088 			regs->npc -= 4;
1089 	}
1090 }
1091 
1092 #ifdef DEBUG_SIGNALS_MAPS
1093 
1094 #define MAPS_LINE_FORMAT	  "%08lx-%08lx %s %08lx %s %lu "
1095 
read_maps(void)1096 static inline void read_maps (void)
1097 {
1098 	struct vm_area_struct * map, * next;
1099 	char * buffer;
1100 	ssize_t i;
1101 
1102 	buffer = (char*)__get_free_page(GFP_KERNEL);
1103 	if (!buffer)
1104 		return;
1105 
1106 	for (map = current->mm->mmap ; map ; map = next ) {
1107 		/* produce the next line */
1108 		char *line;
1109 		char str[5], *cp = str;
1110 		int flags;
1111 		kdev_t dev;
1112 		unsigned long ino;
1113 
1114 		/*
1115 		 * Get the next vma now (but it won't be used if we sleep).
1116 		 */
1117 		next = map->vm_next;
1118 		flags = map->vm_flags;
1119 
1120 		*cp++ = flags & VM_READ ? 'r' : '-';
1121 		*cp++ = flags & VM_WRITE ? 'w' : '-';
1122 		*cp++ = flags & VM_EXEC ? 'x' : '-';
1123 		*cp++ = flags & VM_MAYSHARE ? 's' : 'p';
1124 		*cp++ = 0;
1125 
1126 		dev = 0;
1127 		ino = 0;
1128 		if (map->vm_file != NULL) {
1129 			dev = map->vm_file->f_dentry->d_inode->i_dev;
1130 			ino = map->vm_file->f_dentry->d_inode->i_ino;
1131 			line = d_path(map->vm_file->f_dentry,
1132 				      map->vm_file->f_vfsmnt,
1133 				      buffer, PAGE_SIZE);
1134 			if (IS_ERR(line))
1135 				break;
1136 		}
1137 		printk(MAPS_LINE_FORMAT, map->vm_start, map->vm_end, str, map->vm_pgoff << PAGE_SHIFT,
1138 			      kdevname(dev), ino);
1139 		if (map->vm_file != NULL)
1140 			printk("%s\n", line);
1141 		else
1142 			printk("\n");
1143 	}
1144 	free_page((unsigned long)buffer);
1145 	return;
1146 }
1147 #endif
1148 
1149 /* Note that 'init' is a special process: it doesn't get signals it doesn't
1150  * want to handle. Thus you cannot kill init even with a SIGKILL even by
1151  * mistake.
1152  */
do_signal(sigset_t * oldset,struct pt_regs * regs,unsigned long orig_i0,int restart_syscall)1153 asmlinkage int do_signal(sigset_t *oldset, struct pt_regs * regs,
1154 			 unsigned long orig_i0, int restart_syscall)
1155 {
1156 	unsigned long signr;
1157 	struct k_sigaction *ka;
1158 	siginfo_t info;
1159 
1160 	/*
1161 	 * XXX Disable svr4 signal handling until solaris emulation works.
1162 	 * It is buggy - Anton
1163 	 */
1164 #define SVR4_SIGNAL_BROKEN 1
1165 #ifdef SVR4_SIGNAL_BROKEN
1166 	int svr4_signal = 0;
1167 #else
1168 	int svr4_signal = current->personality == PER_SVR4;
1169 #endif
1170 
1171 	if (!oldset)
1172 		oldset = &current->blocked;
1173 
1174 	for (;;) {
1175 		spin_lock_irq(&current->sigmask_lock);
1176 		signr = dequeue_signal(&current->blocked, &info);
1177 		spin_unlock_irq(&current->sigmask_lock);
1178 
1179 		if (!signr)
1180 			break;
1181 
1182 		if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1183 			/* Do the syscall restart before we let the debugger
1184 			 * look at the child registers.
1185 			 */
1186 			if (restart_syscall &&
1187 			    (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
1188 			     regs->u_regs[UREG_I0] == ERESTARTSYS ||
1189 			     regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
1190 				regs->u_regs[UREG_I0] = orig_i0;
1191 				regs->pc -= 4;
1192 				regs->npc -= 4;
1193 				restart_syscall = 0;
1194 			}
1195 			current->exit_code = signr;
1196 			current->state = TASK_STOPPED;
1197 
1198 			/* This happens to be SMP safe so no need to
1199 			 * grab master kernel lock even in this case.
1200 			 */
1201 			notify_parent(current, SIGCHLD);
1202 			schedule();
1203 			if (!(signr = current->exit_code))
1204 				continue;
1205 			current->exit_code = 0;
1206 			if (signr == SIGSTOP)
1207 				continue;
1208 
1209 			/* Update the siginfo structure.  Is this good?  */
1210 			if (signr != info.si_signo) {
1211 				info.si_signo = signr;
1212 				info.si_errno = 0;
1213 				info.si_code = SI_USER;
1214 				info.si_pid = current->p_pptr->pid;
1215 				info.si_uid = current->p_pptr->uid;
1216 			}
1217 
1218 			/* If the (new) signal is now blocked, requeue it.  */
1219 			if (sigismember(&current->blocked, signr)) {
1220 				send_sig_info(signr, &info, current);
1221 				continue;
1222 			}
1223 		}
1224 
1225 		ka = &current->sig->action[signr-1];
1226 
1227 		if (ka->sa.sa_handler == SIG_IGN) {
1228 			if (signr != SIGCHLD)
1229 				continue;
1230 
1231 			/* sys_wait4() grabs the master kernel lock, so
1232 			 * we need not do so, that sucker should be
1233 			 * threaded and would not be that difficult to
1234 			 * do anyways.
1235 			 */
1236 			while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
1237 				;
1238 			continue;
1239 		}
1240 		if (ka->sa.sa_handler == SIG_DFL) {
1241 			unsigned long exit_code = signr;
1242 
1243 			if (current->pid == 1)
1244 				continue;
1245 			switch (signr) {
1246 			case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
1247 				continue;
1248 
1249 			case SIGTSTP: case SIGTTIN: case SIGTTOU:
1250 				/* The operations performed by
1251 				 * is_orphaned_pgrp() are protected by
1252 				 * the tasklist_lock.
1253 				 */
1254 				if (is_orphaned_pgrp(current->pgrp))
1255 					continue;
1256 
1257 			case SIGSTOP:
1258 				if (current->ptrace & PT_PTRACED)
1259 					continue;
1260 				current->state = TASK_STOPPED;
1261 				current->exit_code = signr;
1262 
1263 				/* notify_parent() is SMP safe */
1264 				if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags &
1265 				      SA_NOCLDSTOP))
1266 					notify_parent(current, SIGCHLD);
1267 				schedule();
1268 				continue;
1269 
1270 			case SIGQUIT: case SIGILL: case SIGTRAP:
1271 			case SIGABRT: case SIGFPE: case SIGSEGV:
1272 			case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
1273 				if (do_coredump(signr, regs))
1274 					exit_code |= 0x80;
1275 #ifdef DEBUG_SIGNALS
1276 				/* Very useful to debug dynamic linker problems */
1277 				printk ("Sig %ld going for %s[%d]...\n", signr, current->comm, current->pid);
1278 				show_regs (regs);
1279 #ifdef DEBUG_SIGNALS_TRACE
1280 				{
1281 					struct reg_window *rw = (struct reg_window *)regs->u_regs[UREG_FP];
1282 					unsigned int ins[8];
1283 
1284 					while (rw &&
1285 					       !(((unsigned long) rw) & 0x3)) {
1286 						copy_from_user(ins, &rw->ins[0], sizeof(ins));
1287 						printk("Caller[%08x](%08x,%08x,%08x,%08x,%08x,%08x)\n", ins[7], ins[0], ins[1], ins[2], ins[3], ins[4], ins[5]);
1288 						rw = (struct reg_window *)(unsigned long)ins[6];
1289 					}
1290 				}
1291 #endif
1292 #ifdef DEBUG_SIGNALS_MAPS
1293 				printk("Maps:\n");
1294 				read_maps();
1295 #endif
1296 #endif
1297 				/* fall through */
1298 			default:
1299 				sigaddset(&current->pending.signal, signr);
1300 				recalc_sigpending(current);
1301 				current->flags |= PF_SIGNALED;
1302 				do_exit(exit_code);
1303 				/* NOT REACHED */
1304 			}
1305 		}
1306 		if (restart_syscall)
1307 			syscall_restart(orig_i0, regs, &ka->sa);
1308 		handle_signal(signr, ka, &info, oldset, regs, svr4_signal);
1309 		return 1;
1310 	}
1311 	if (restart_syscall &&
1312 	    (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
1313 	     regs->u_regs[UREG_I0] == ERESTARTSYS ||
1314 	     regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
1315 		/* replay the system call when we are done */
1316 		regs->u_regs[UREG_I0] = orig_i0;
1317 		regs->pc -= 4;
1318 		regs->npc -= 4;
1319 	}
1320 	return 0;
1321 }
1322 
1323 asmlinkage int
do_sys_sigstack(struct sigstack * ssptr,struct sigstack * ossptr,unsigned long sp)1324 do_sys_sigstack(struct sigstack *ssptr, struct sigstack *ossptr, unsigned long sp)
1325 {
1326 	int ret = -EFAULT;
1327 
1328 	/* First see if old state is wanted. */
1329 	if (ossptr) {
1330 		if (put_user(current->sas_ss_sp + current->sas_ss_size, &ossptr->the_stack) ||
1331 		    __put_user(on_sig_stack(sp), &ossptr->cur_status))
1332 			goto out;
1333 	}
1334 
1335 	/* Now see if we want to update the new state. */
1336 	if (ssptr) {
1337 		void *ss_sp;
1338 
1339 		if (get_user(ss_sp, &ssptr->the_stack))
1340 			goto out;
1341 		/* If the current stack was set with sigaltstack, don't
1342 		   swap stacks while we are on it.  */
1343 		ret = -EPERM;
1344 		if (current->sas_ss_sp && on_sig_stack(sp))
1345 			goto out;
1346 
1347 		/* Since we don't know the extent of the stack, and we don't
1348 		   track onstack-ness, but rather calculate it, we must
1349 		   presume a size.  Ho hum this interface is lossy.  */
1350 		current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
1351 		current->sas_ss_size = SIGSTKSZ;
1352 	}
1353 	ret = 0;
1354 out:
1355 	return ret;
1356 }
1357