xref: /openbmc/linux/arch/sparc/kernel/signal_32.c (revision 99b06feb)
1 /*  linux/arch/sparc/kernel/signal.c
2  *
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  *  Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6  *  Copyright (C) 1997 Eddie C. Dost   (ecd@skynet.be)
7  */
8 
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/wait.h>
14 #include <linux/ptrace.h>
15 #include <linux/unistd.h>
16 #include <linux/mm.h>
17 #include <linux/tty.h>
18 #include <linux/smp.h>
19 #include <linux/binfmts.h>	/* do_coredum */
20 #include <linux/bitops.h>
21 #include <linux/tracehook.h>
22 
23 #include <asm/uaccess.h>
24 #include <asm/ptrace.h>
25 #include <asm/pgalloc.h>
26 #include <asm/pgtable.h>
27 #include <asm/cacheflush.h>	/* flush_sig_insns */
28 #include <asm/switch_to.h>
29 
30 #include "sigutil.h"
31 
32 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
33 		   void *fpqueue, unsigned long *fpqdepth);
34 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
35 
36 struct signal_frame {
37 	struct sparc_stackf	ss;
38 	__siginfo32_t		info;
39 	__siginfo_fpu_t __user	*fpu_save;
40 	unsigned long		insns[2] __attribute__ ((aligned (8)));
41 	unsigned int		extramask[_NSIG_WORDS - 1];
42 	unsigned int		extra_size; /* Should be 0 */
43 	__siginfo_rwin_t __user	*rwin_save;
44 } __attribute__((aligned(8)));
45 
46 struct rt_signal_frame {
47 	struct sparc_stackf	ss;
48 	siginfo_t		info;
49 	struct pt_regs		regs;
50 	sigset_t		mask;
51 	__siginfo_fpu_t __user	*fpu_save;
52 	unsigned int		insns[2];
53 	stack_t			stack;
54 	unsigned int		extra_size; /* Should be 0 */
55 	__siginfo_rwin_t __user	*rwin_save;
56 } __attribute__((aligned(8)));
57 
58 /* Align macros */
59 #define SF_ALIGNEDSZ  (((sizeof(struct signal_frame) + 7) & (~7)))
60 #define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame) + 7) & (~7)))
61 
62 static int _sigpause_common(old_sigset_t set)
63 {
64 	sigset_t blocked;
65 	siginitset(&blocked, set);
66 	return sigsuspend(&blocked);
67 }
68 
69 asmlinkage int sys_sigsuspend(old_sigset_t set)
70 {
71 	return _sigpause_common(set);
72 }
73 
74 asmlinkage void do_sigreturn(struct pt_regs *regs)
75 {
76 	struct signal_frame __user *sf;
77 	unsigned long up_psr, pc, npc;
78 	sigset_t set;
79 	__siginfo_fpu_t __user *fpu_save;
80 	__siginfo_rwin_t __user *rwin_save;
81 	int err;
82 
83 	/* Always make any pending restarted system calls return -EINTR */
84 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
85 
86 	synchronize_user_stack();
87 
88 	sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
89 
90 	/* 1. Make sure we are not getting garbage from the user */
91 	if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
92 		goto segv_and_exit;
93 
94 	if (((unsigned long) sf) & 3)
95 		goto segv_and_exit;
96 
97 	err = __get_user(pc,  &sf->info.si_regs.pc);
98 	err |= __get_user(npc, &sf->info.si_regs.npc);
99 
100 	if ((pc | npc) & 3)
101 		goto segv_and_exit;
102 
103 	/* 2. Restore the state */
104 	up_psr = regs->psr;
105 	err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
106 
107 	/* User can only change condition codes and FPU enabling in %psr. */
108 	regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
109 		  | (regs->psr & (PSR_ICC | PSR_EF));
110 
111 	/* Prevent syscall restart.  */
112 	pt_regs_clear_syscall(regs);
113 
114 	err |= __get_user(fpu_save, &sf->fpu_save);
115 	if (fpu_save)
116 		err |= restore_fpu_state(regs, fpu_save);
117 	err |= __get_user(rwin_save, &sf->rwin_save);
118 	if (rwin_save)
119 		err |= restore_rwin_state(rwin_save);
120 
121 	/* This is pretty much atomic, no amount locking would prevent
122 	 * the races which exist anyways.
123 	 */
124 	err |= __get_user(set.sig[0], &sf->info.si_mask);
125 	err |= __copy_from_user(&set.sig[1], &sf->extramask,
126 			        (_NSIG_WORDS-1) * sizeof(unsigned int));
127 
128 	if (err)
129 		goto segv_and_exit;
130 
131 	set_current_blocked(&set);
132 	return;
133 
134 segv_and_exit:
135 	force_sig(SIGSEGV, current);
136 }
137 
138 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
139 {
140 	struct rt_signal_frame __user *sf;
141 	unsigned int psr, pc, npc;
142 	__siginfo_fpu_t __user *fpu_save;
143 	__siginfo_rwin_t __user *rwin_save;
144 	sigset_t set;
145 	int err;
146 
147 	synchronize_user_stack();
148 	sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
149 	if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
150 	    (((unsigned long) sf) & 0x03))
151 		goto segv;
152 
153 	err = __get_user(pc, &sf->regs.pc);
154 	err |= __get_user(npc, &sf->regs.npc);
155 	err |= ((pc | npc) & 0x03);
156 
157 	err |= __get_user(regs->y, &sf->regs.y);
158 	err |= __get_user(psr, &sf->regs.psr);
159 
160 	err |= __copy_from_user(&regs->u_regs[UREG_G1],
161 				&sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
162 
163 	regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
164 
165 	/* Prevent syscall restart.  */
166 	pt_regs_clear_syscall(regs);
167 
168 	err |= __get_user(fpu_save, &sf->fpu_save);
169 	if (!err && fpu_save)
170 		err |= restore_fpu_state(regs, fpu_save);
171 	err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
172 	err |= restore_altstack(&sf->stack);
173 
174 	if (err)
175 		goto segv;
176 
177 	regs->pc = pc;
178 	regs->npc = npc;
179 
180 	err |= __get_user(rwin_save, &sf->rwin_save);
181 	if (!err && rwin_save) {
182 		if (restore_rwin_state(rwin_save))
183 			goto segv;
184 	}
185 
186 	set_current_blocked(&set);
187 	return;
188 segv:
189 	force_sig(SIGSEGV, current);
190 }
191 
192 /* Checks if the fp is valid */
193 static inline int invalid_frame_pointer(void __user *fp, int fplen)
194 {
195 	if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
196 		return 1;
197 
198 	return 0;
199 }
200 
201 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
202 {
203 	unsigned long sp = regs->u_regs[UREG_FP];
204 
205 	/*
206 	 * If we are on the alternate signal stack and would overflow it, don't.
207 	 * Return an always-bogus address instead so we will die with SIGSEGV.
208 	 */
209 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
210 		return (void __user *) -1L;
211 
212 	/* This is the X/Open sanctioned signal stack switching.  */
213 	if (sa->sa_flags & SA_ONSTACK) {
214 		if (sas_ss_flags(sp) == 0)
215 			sp = current->sas_ss_sp + current->sas_ss_size;
216 	}
217 
218 	sp -= framesize;
219 
220 	/* Always align the stack frame.  This handles two cases.  First,
221 	 * sigaltstack need not be mindful of platform specific stack
222 	 * alignment.  Second, if we took this signal because the stack
223 	 * is not aligned properly, we'd like to take the signal cleanly
224 	 * and report that.
225 	 */
226 	sp &= ~15UL;
227 
228 	return (void __user *) sp;
229 }
230 
231 static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
232 		       int signo, sigset_t *oldset)
233 {
234 	struct signal_frame __user *sf;
235 	int sigframe_size, err, wsaved;
236 	void __user *tail;
237 
238 	/* 1. Make sure everything is clean */
239 	synchronize_user_stack();
240 
241 	wsaved = current_thread_info()->w_saved;
242 
243 	sigframe_size = sizeof(*sf);
244 	if (used_math())
245 		sigframe_size += sizeof(__siginfo_fpu_t);
246 	if (wsaved)
247 		sigframe_size += sizeof(__siginfo_rwin_t);
248 
249 	sf = (struct signal_frame __user *)
250 		get_sigframe(&ka->sa, regs, sigframe_size);
251 
252 	if (invalid_frame_pointer(sf, sigframe_size))
253 		goto sigill_and_return;
254 
255 	tail = sf + 1;
256 
257 	/* 2. Save the current process state */
258 	err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
259 
260 	err |= __put_user(0, &sf->extra_size);
261 
262 	if (used_math()) {
263 		__siginfo_fpu_t __user *fp = tail;
264 		tail += sizeof(*fp);
265 		err |= save_fpu_state(regs, fp);
266 		err |= __put_user(fp, &sf->fpu_save);
267 	} else {
268 		err |= __put_user(0, &sf->fpu_save);
269 	}
270 	if (wsaved) {
271 		__siginfo_rwin_t __user *rwp = tail;
272 		tail += sizeof(*rwp);
273 		err |= save_rwin_state(wsaved, rwp);
274 		err |= __put_user(rwp, &sf->rwin_save);
275 	} else {
276 		err |= __put_user(0, &sf->rwin_save);
277 	}
278 
279 	err |= __put_user(oldset->sig[0], &sf->info.si_mask);
280 	err |= __copy_to_user(sf->extramask, &oldset->sig[1],
281 			      (_NSIG_WORDS - 1) * sizeof(unsigned int));
282 	if (!wsaved) {
283 		err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
284 				      sizeof(struct reg_window32));
285 	} else {
286 		struct reg_window32 *rp;
287 
288 		rp = &current_thread_info()->reg_window[wsaved - 1];
289 		err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
290 	}
291 	if (err)
292 		goto sigsegv;
293 
294 	/* 3. signal handler back-trampoline and parameters */
295 	regs->u_regs[UREG_FP] = (unsigned long) sf;
296 	regs->u_regs[UREG_I0] = signo;
297 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
298 	regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
299 
300 	/* 4. signal handler */
301 	regs->pc = (unsigned long) ka->sa.sa_handler;
302 	regs->npc = (regs->pc + 4);
303 
304 	/* 5. return to kernel instructions */
305 	if (ka->ka_restorer)
306 		regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
307 	else {
308 		regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
309 
310 		/* mov __NR_sigreturn, %g1 */
311 		err |= __put_user(0x821020d8, &sf->insns[0]);
312 
313 		/* t 0x10 */
314 		err |= __put_user(0x91d02010, &sf->insns[1]);
315 		if (err)
316 			goto sigsegv;
317 
318 		/* Flush instruction space. */
319 		flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
320 	}
321 	return 0;
322 
323 sigill_and_return:
324 	do_exit(SIGILL);
325 	return -EINVAL;
326 
327 sigsegv:
328 	force_sigsegv(signo, current);
329 	return -EFAULT;
330 }
331 
332 static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
333 			  int signo, sigset_t *oldset, siginfo_t *info)
334 {
335 	struct rt_signal_frame __user *sf;
336 	int sigframe_size, wsaved;
337 	void __user *tail;
338 	unsigned int psr;
339 	int err;
340 
341 	synchronize_user_stack();
342 	wsaved = current_thread_info()->w_saved;
343 	sigframe_size = sizeof(*sf);
344 	if (used_math())
345 		sigframe_size += sizeof(__siginfo_fpu_t);
346 	if (wsaved)
347 		sigframe_size += sizeof(__siginfo_rwin_t);
348 	sf = (struct rt_signal_frame __user *)
349 		get_sigframe(&ka->sa, regs, sigframe_size);
350 	if (invalid_frame_pointer(sf, sigframe_size))
351 		goto sigill;
352 
353 	tail = sf + 1;
354 	err  = __put_user(regs->pc, &sf->regs.pc);
355 	err |= __put_user(regs->npc, &sf->regs.npc);
356 	err |= __put_user(regs->y, &sf->regs.y);
357 	psr = regs->psr;
358 	if (used_math())
359 		psr |= PSR_EF;
360 	err |= __put_user(psr, &sf->regs.psr);
361 	err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
362 	err |= __put_user(0, &sf->extra_size);
363 
364 	if (psr & PSR_EF) {
365 		__siginfo_fpu_t *fp = tail;
366 		tail += sizeof(*fp);
367 		err |= save_fpu_state(regs, fp);
368 		err |= __put_user(fp, &sf->fpu_save);
369 	} else {
370 		err |= __put_user(0, &sf->fpu_save);
371 	}
372 	if (wsaved) {
373 		__siginfo_rwin_t *rwp = tail;
374 		tail += sizeof(*rwp);
375 		err |= save_rwin_state(wsaved, rwp);
376 		err |= __put_user(rwp, &sf->rwin_save);
377 	} else {
378 		err |= __put_user(0, &sf->rwin_save);
379 	}
380 	err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
381 
382 	/* Setup sigaltstack */
383 	err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
384 
385 	if (!wsaved) {
386 		err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
387 				      sizeof(struct reg_window32));
388 	} else {
389 		struct reg_window32 *rp;
390 
391 		rp = &current_thread_info()->reg_window[wsaved - 1];
392 		err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
393 	}
394 
395 	err |= copy_siginfo_to_user(&sf->info, info);
396 
397 	if (err)
398 		goto sigsegv;
399 
400 	regs->u_regs[UREG_FP] = (unsigned long) sf;
401 	regs->u_regs[UREG_I0] = signo;
402 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
403 	regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
404 
405 	regs->pc = (unsigned long) ka->sa.sa_handler;
406 	regs->npc = (regs->pc + 4);
407 
408 	if (ka->ka_restorer)
409 		regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
410 	else {
411 		regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
412 
413 		/* mov __NR_sigreturn, %g1 */
414 		err |= __put_user(0x821020d8, &sf->insns[0]);
415 
416 		/* t 0x10 */
417 		err |= __put_user(0x91d02010, &sf->insns[1]);
418 		if (err)
419 			goto sigsegv;
420 
421 		/* Flush instruction space. */
422 		flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
423 	}
424 	return 0;
425 
426 sigill:
427 	do_exit(SIGILL);
428 	return -EINVAL;
429 
430 sigsegv:
431 	force_sigsegv(signo, current);
432 	return -EFAULT;
433 }
434 
435 static inline void
436 handle_signal(unsigned long signr, struct k_sigaction *ka,
437 	      siginfo_t *info, struct pt_regs *regs)
438 {
439 	sigset_t *oldset = sigmask_to_save();
440 	int err;
441 
442 	if (ka->sa.sa_flags & SA_SIGINFO)
443 		err = setup_rt_frame(ka, regs, signr, oldset, info);
444 	else
445 		err = setup_frame(ka, regs, signr, oldset);
446 
447 	if (err)
448 		return;
449 
450 	signal_delivered(signr, info, ka, regs, 0);
451 }
452 
453 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
454 				   struct sigaction *sa)
455 {
456 	switch(regs->u_regs[UREG_I0]) {
457 	case ERESTART_RESTARTBLOCK:
458 	case ERESTARTNOHAND:
459 	no_system_call_restart:
460 		regs->u_regs[UREG_I0] = EINTR;
461 		regs->psr |= PSR_C;
462 		break;
463 	case ERESTARTSYS:
464 		if (!(sa->sa_flags & SA_RESTART))
465 			goto no_system_call_restart;
466 		/* fallthrough */
467 	case ERESTARTNOINTR:
468 		regs->u_regs[UREG_I0] = orig_i0;
469 		regs->pc -= 4;
470 		regs->npc -= 4;
471 	}
472 }
473 
474 /* Note that 'init' is a special process: it doesn't get signals it doesn't
475  * want to handle. Thus you cannot kill init even with a SIGKILL even by
476  * mistake.
477  */
478 static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
479 {
480 	struct k_sigaction ka;
481 	int restart_syscall;
482 	siginfo_t info;
483 	int signr;
484 
485 	/* It's a lot of work and synchronization to add a new ptrace
486 	 * register for GDB to save and restore in order to get
487 	 * orig_i0 correct for syscall restarts when debugging.
488 	 *
489 	 * Although it should be the case that most of the global
490 	 * registers are volatile across a system call, glibc already
491 	 * depends upon that fact that we preserve them.  So we can't
492 	 * just use any global register to save away the orig_i0 value.
493 	 *
494 	 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
495 	 * preserved across a system call trap by various pieces of
496 	 * code in glibc.
497 	 *
498 	 * %g7 is used as the "thread register".   %g6 is not used in
499 	 * any fixed manner.  %g6 is used as a scratch register and
500 	 * a compiler temporary, but it's value is never used across
501 	 * a system call.  Therefore %g6 is usable for orig_i0 storage.
502 	 */
503 	if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
504 		regs->u_regs[UREG_G6] = orig_i0;
505 
506 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
507 
508 	/* If the debugger messes with the program counter, it clears
509 	 * the software "in syscall" bit, directing us to not perform
510 	 * a syscall restart.
511 	 */
512 	restart_syscall = 0;
513 	if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
514 		restart_syscall = 1;
515 		orig_i0 = regs->u_regs[UREG_G6];
516 	}
517 
518 
519 	if (signr > 0) {
520 		if (restart_syscall)
521 			syscall_restart(orig_i0, regs, &ka.sa);
522 		handle_signal(signr, &ka, &info, regs);
523 		return;
524 	}
525 	if (restart_syscall &&
526 	    (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
527 	     regs->u_regs[UREG_I0] == ERESTARTSYS ||
528 	     regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
529 		/* replay the system call when we are done */
530 		regs->u_regs[UREG_I0] = orig_i0;
531 		regs->pc -= 4;
532 		regs->npc -= 4;
533 		pt_regs_clear_syscall(regs);
534 	}
535 	if (restart_syscall &&
536 	    regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
537 		regs->u_regs[UREG_G1] = __NR_restart_syscall;
538 		regs->pc -= 4;
539 		regs->npc -= 4;
540 		pt_regs_clear_syscall(regs);
541 	}
542 
543 	/* if there's no signal to deliver, we just put the saved sigmask
544 	 * back
545 	 */
546 	restore_saved_sigmask();
547 }
548 
549 void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
550 		      unsigned long thread_info_flags)
551 {
552 	if (thread_info_flags & _TIF_SIGPENDING)
553 		do_signal(regs, orig_i0);
554 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
555 		clear_thread_flag(TIF_NOTIFY_RESUME);
556 		tracehook_notify_resume(regs);
557 	}
558 }
559 
560 asmlinkage int
561 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
562 		unsigned long sp)
563 {
564 	int ret = -EFAULT;
565 
566 	/* First see if old state is wanted. */
567 	if (ossptr) {
568 		if (put_user(current->sas_ss_sp + current->sas_ss_size,
569 			     &ossptr->the_stack) ||
570 		    __put_user(on_sig_stack(sp), &ossptr->cur_status))
571 			goto out;
572 	}
573 
574 	/* Now see if we want to update the new state. */
575 	if (ssptr) {
576 		char *ss_sp;
577 
578 		if (get_user(ss_sp, &ssptr->the_stack))
579 			goto out;
580 		/* If the current stack was set with sigaltstack, don't
581 		   swap stacks while we are on it.  */
582 		ret = -EPERM;
583 		if (current->sas_ss_sp && on_sig_stack(sp))
584 			goto out;
585 
586 		/* Since we don't know the extent of the stack, and we don't
587 		   track onstack-ness, but rather calculate it, we must
588 		   presume a size.  Ho hum this interface is lossy.  */
589 		current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
590 		current->sas_ss_size = SIGSTKSZ;
591 	}
592 	ret = 0;
593 out:
594 	return ret;
595 }
596