xref: /openbmc/linux/arch/powerpc/kernel/signal_64.c (revision 87c2ce3b)
1 /*
2  *  linux/arch/ppc64/kernel/signal.c
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Derived from "arch/i386/kernel/signal.c"
8  *    Copyright (C) 1991, 1992 Linus Torvalds
9  *    1997-11-28  Modified for POSIX.1b signals by Richard Henderson
10  *
11  *  This program is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU General Public License
13  *  as published by the Free Software Foundation; either version
14  *  2 of the License, or (at your option) any later version.
15  */
16 
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/kernel.h>
23 #include <linux/signal.h>
24 #include <linux/errno.h>
25 #include <linux/wait.h>
26 #include <linux/unistd.h>
27 #include <linux/stddef.h>
28 #include <linux/elf.h>
29 #include <linux/ptrace.h>
30 #include <linux/module.h>
31 
32 #include <asm/sigcontext.h>
33 #include <asm/ucontext.h>
34 #include <asm/uaccess.h>
35 #include <asm/pgtable.h>
36 #include <asm/unistd.h>
37 #include <asm/cacheflush.h>
38 #include <asm/vdso.h>
39 
40 #define DEBUG_SIG 0
41 
42 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
43 
44 #define GP_REGS_SIZE	min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
45 #define FP_REGS_SIZE	sizeof(elf_fpregset_t)
46 
47 #define TRAMP_TRACEBACK	3
48 #define TRAMP_SIZE	6
49 
50 /*
51  * When we have signals to deliver, we set up on the user stack,
52  * going down from the original stack pointer:
53  *	1) a rt_sigframe struct which contains the ucontext
54  *	2) a gap of __SIGNAL_FRAMESIZE bytes which acts as a dummy caller
55  *	   frame for the signal handler.
56  */
57 
58 struct rt_sigframe {
59 	/* sys_rt_sigreturn requires the ucontext be the first field */
60 	struct ucontext uc;
61 	unsigned long _unused[2];
62 	unsigned int tramp[TRAMP_SIZE];
63 	struct siginfo *pinfo;
64 	void *puc;
65 	struct siginfo info;
66 	/* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
67 	char abigap[288];
68 } __attribute__ ((aligned (16)));
69 
70 
71 /*
72  * Atomically swap in the new signal mask, and wait for a signal.
73  */
74 long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, int p3, int p4,
75 		       int p6, int p7, struct pt_regs *regs)
76 {
77 	sigset_t saveset, newset;
78 
79 	/* XXX: Don't preclude handling different sized sigset_t's.  */
80 	if (sigsetsize != sizeof(sigset_t))
81 		return -EINVAL;
82 
83 	if (copy_from_user(&newset, unewset, sizeof(newset)))
84 		return -EFAULT;
85 	sigdelsetmask(&newset, ~_BLOCKABLE);
86 
87 	spin_lock_irq(&current->sighand->siglock);
88 	saveset = current->blocked;
89 	current->blocked = newset;
90 	recalc_sigpending();
91 	spin_unlock_irq(&current->sighand->siglock);
92 
93 	regs->result = -EINTR;
94 	regs->gpr[3] = EINTR;
95 	regs->ccr |= 0x10000000;
96 	while (1) {
97 		current->state = TASK_INTERRUPTIBLE;
98 		schedule();
99 		if (do_signal(&saveset, regs)) {
100 			set_thread_flag(TIF_RESTOREALL);
101 			return 0;
102 		}
103 	}
104 }
105 
106 long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, unsigned long r5,
107 		     unsigned long r6, unsigned long r7, unsigned long r8,
108 		     struct pt_regs *regs)
109 {
110 	return do_sigaltstack(uss, uoss, regs->gpr[1]);
111 }
112 
113 
114 /*
115  * Set up the sigcontext for the signal frame.
116  */
117 
118 static long setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
119 		 int signr, sigset_t *set, unsigned long handler)
120 {
121 	/* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
122 	 * process never used altivec yet (MSR_VEC is zero in pt_regs of
123 	 * the context). This is very important because we must ensure we
124 	 * don't lose the VRSAVE content that may have been set prior to
125 	 * the process doing its first vector operation
126 	 * Userland shall check AT_HWCAP to know wether it can rely on the
127 	 * v_regs pointer or not
128 	 */
129 #ifdef CONFIG_ALTIVEC
130 	elf_vrreg_t __user *v_regs = (elf_vrreg_t __user *)(((unsigned long)sc->vmx_reserve + 15) & ~0xful);
131 #endif
132 	long err = 0;
133 
134 	flush_fp_to_thread(current);
135 
136 #ifdef CONFIG_ALTIVEC
137 	err |= __put_user(v_regs, &sc->v_regs);
138 
139 	/* save altivec registers */
140 	if (current->thread.used_vr) {
141 		flush_altivec_to_thread(current);
142 		/* Copy 33 vec registers (vr0..31 and vscr) to the stack */
143 		err |= __copy_to_user(v_regs, current->thread.vr, 33 * sizeof(vector128));
144 		/* set MSR_VEC in the MSR value in the frame to indicate that sc->v_reg)
145 		 * contains valid data.
146 		 */
147 		regs->msr |= MSR_VEC;
148 	}
149 	/* We always copy to/from vrsave, it's 0 if we don't have or don't
150 	 * use altivec.
151 	 */
152 	err |= __put_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
153 #else /* CONFIG_ALTIVEC */
154 	err |= __put_user(0, &sc->v_regs);
155 #endif /* CONFIG_ALTIVEC */
156 	err |= __put_user(&sc->gp_regs, &sc->regs);
157 	if (!FULL_REGS(regs)) {
158 		/* Zero out the unsaved GPRs to avoid information
159 		   leak, and set TIF_SAVE_NVGPRS to ensure that the
160 		   registers do actually get saved later. */
161 		memset(&regs->gpr[14], 0, 18 * sizeof(unsigned long));
162 		set_thread_flag(TIF_SAVE_NVGPRS);
163 		current_thread_info()->nvgprs_frame = &sc->gp_regs;
164 	}
165 	err |= __copy_to_user(&sc->gp_regs, regs, GP_REGS_SIZE);
166 	err |= __copy_to_user(&sc->fp_regs, &current->thread.fpr, FP_REGS_SIZE);
167 	err |= __put_user(signr, &sc->signal);
168 	err |= __put_user(handler, &sc->handler);
169 	if (set != NULL)
170 		err |=  __put_user(set->sig[0], &sc->oldmask);
171 
172 	return err;
173 }
174 
175 /*
176  * Restore the sigcontext from the signal frame.
177  */
178 
179 static long restore_sigcontext(struct pt_regs *regs, sigset_t *set, int sig,
180 			      struct sigcontext __user *sc)
181 {
182 #ifdef CONFIG_ALTIVEC
183 	elf_vrreg_t __user *v_regs;
184 #endif
185 	unsigned long err = 0;
186 	unsigned long save_r13 = 0;
187 	elf_greg_t *gregs = (elf_greg_t *)regs;
188 #ifdef CONFIG_ALTIVEC
189 	unsigned long msr;
190 #endif
191 	int i;
192 
193 	/* If this is not a signal return, we preserve the TLS in r13 */
194 	if (!sig)
195 		save_r13 = regs->gpr[13];
196 
197 	/* copy everything before MSR */
198 	err |= __copy_from_user(regs, &sc->gp_regs,
199 				PT_MSR*sizeof(unsigned long));
200 
201 	/* skip MSR and SOFTE */
202 	for (i = PT_MSR+1; i <= PT_RESULT; i++) {
203 		if (i == PT_SOFTE)
204 			continue;
205 		err |= __get_user(gregs[i], &sc->gp_regs[i]);
206 	}
207 
208 	if (!sig)
209 		regs->gpr[13] = save_r13;
210 	err |= __copy_from_user(&current->thread.fpr, &sc->fp_regs, FP_REGS_SIZE);
211 	if (set != NULL)
212 		err |=  __get_user(set->sig[0], &sc->oldmask);
213 
214 #ifdef CONFIG_ALTIVEC
215 	err |= __get_user(v_regs, &sc->v_regs);
216 	err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
217 	if (err)
218 		return err;
219 	/* Copy 33 vec registers (vr0..31 and vscr) from the stack */
220 	if (v_regs != 0 && (msr & MSR_VEC) != 0)
221 		err |= __copy_from_user(current->thread.vr, v_regs,
222 					33 * sizeof(vector128));
223 	else if (current->thread.used_vr)
224 		memset(current->thread.vr, 0, 33 * sizeof(vector128));
225 	/* Always get VRSAVE back */
226 	if (v_regs != 0)
227 		err |= __get_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
228 	else
229 		current->thread.vrsave = 0;
230 #endif /* CONFIG_ALTIVEC */
231 
232 #ifndef CONFIG_SMP
233 	preempt_disable();
234 	if (last_task_used_math == current)
235 		last_task_used_math = NULL;
236 	if (last_task_used_altivec == current)
237 		last_task_used_altivec = NULL;
238 	preempt_enable();
239 #endif
240 	/* Force reload of FP/VEC */
241 	regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC);
242 
243 	return err;
244 }
245 
246 /*
247  * Allocate space for the signal frame
248  */
249 static inline void __user * get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
250 				  size_t frame_size)
251 {
252         unsigned long newsp;
253 
254         /* Default to using normal stack */
255         newsp = regs->gpr[1];
256 
257 	if (ka->sa.sa_flags & SA_ONSTACK) {
258 		if (! on_sig_stack(regs->gpr[1]))
259 			newsp = (current->sas_ss_sp + current->sas_ss_size);
260 	}
261 
262         return (void __user *)((newsp - frame_size) & -16ul);
263 }
264 
265 /*
266  * Setup the trampoline code on the stack
267  */
268 static long setup_trampoline(unsigned int syscall, unsigned int __user *tramp)
269 {
270 	int i;
271 	long err = 0;
272 
273 	/* addi r1, r1, __SIGNAL_FRAMESIZE  # Pop the dummy stackframe */
274 	err |= __put_user(0x38210000UL | (__SIGNAL_FRAMESIZE & 0xffff), &tramp[0]);
275 	/* li r0, __NR_[rt_]sigreturn| */
276 	err |= __put_user(0x38000000UL | (syscall & 0xffff), &tramp[1]);
277 	/* sc */
278 	err |= __put_user(0x44000002UL, &tramp[2]);
279 
280 	/* Minimal traceback info */
281 	for (i=TRAMP_TRACEBACK; i < TRAMP_SIZE ;i++)
282 		err |= __put_user(0, &tramp[i]);
283 
284 	if (!err)
285 		flush_icache_range((unsigned long) &tramp[0],
286 			   (unsigned long) &tramp[TRAMP_SIZE]);
287 
288 	return err;
289 }
290 
291 /*
292  * Restore the user process's signal mask (also used by signal32.c)
293  */
294 void restore_sigmask(sigset_t *set)
295 {
296 	sigdelsetmask(set, ~_BLOCKABLE);
297 	spin_lock_irq(&current->sighand->siglock);
298 	current->blocked = *set;
299 	recalc_sigpending();
300 	spin_unlock_irq(&current->sighand->siglock);
301 }
302 
303 
304 /*
305  * Handle {get,set,swap}_context operations
306  */
307 int sys_swapcontext(struct ucontext __user *old_ctx,
308 		    struct ucontext __user *new_ctx,
309 		    long ctx_size, long r6, long r7, long r8, struct pt_regs *regs)
310 {
311 	unsigned char tmp;
312 	sigset_t set;
313 
314 	/* Context size is for future use. Right now, we only make sure
315 	 * we are passed something we understand
316 	 */
317 	if (ctx_size < sizeof(struct ucontext))
318 		return -EINVAL;
319 
320 	if (old_ctx != NULL) {
321 		if (!access_ok(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
322 		    || setup_sigcontext(&old_ctx->uc_mcontext, regs, 0, NULL, 0)
323 		    || __copy_to_user(&old_ctx->uc_sigmask,
324 				      &current->blocked, sizeof(sigset_t)))
325 			return -EFAULT;
326 	}
327 	if (new_ctx == NULL)
328 		return 0;
329 	if (!access_ok(VERIFY_READ, new_ctx, sizeof(*new_ctx))
330 	    || __get_user(tmp, (u8 __user *) new_ctx)
331 	    || __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
332 		return -EFAULT;
333 
334 	/*
335 	 * If we get a fault copying the context into the kernel's
336 	 * image of the user's registers, we can't just return -EFAULT
337 	 * because the user's registers will be corrupted.  For instance
338 	 * the NIP value may have been updated but not some of the
339 	 * other registers.  Given that we have done the access_ok
340 	 * and successfully read the first and last bytes of the region
341 	 * above, this should only happen in an out-of-memory situation
342 	 * or if another thread unmaps the region containing the context.
343 	 * We kill the task with a SIGSEGV in this situation.
344 	 */
345 
346 	if (__copy_from_user(&set, &new_ctx->uc_sigmask, sizeof(set)))
347 		do_exit(SIGSEGV);
348 	restore_sigmask(&set);
349 	if (restore_sigcontext(regs, NULL, 0, &new_ctx->uc_mcontext))
350 		do_exit(SIGSEGV);
351 
352 	/* This returns like rt_sigreturn */
353 	set_thread_flag(TIF_RESTOREALL);
354 	return 0;
355 }
356 
357 
358 /*
359  * Do a signal return; undo the signal stack.
360  */
361 
362 int sys_rt_sigreturn(unsigned long r3, unsigned long r4, unsigned long r5,
363 		     unsigned long r6, unsigned long r7, unsigned long r8,
364 		     struct pt_regs *regs)
365 {
366 	struct ucontext __user *uc = (struct ucontext __user *)regs->gpr[1];
367 	sigset_t set;
368 
369 	/* Always make any pending restarted system calls return -EINTR */
370 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
371 
372 	if (!access_ok(VERIFY_READ, uc, sizeof(*uc)))
373 		goto badframe;
374 
375 	if (__copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
376 		goto badframe;
377 	restore_sigmask(&set);
378 	if (restore_sigcontext(regs, NULL, 1, &uc->uc_mcontext))
379 		goto badframe;
380 
381 	/* do_sigaltstack expects a __user pointer and won't modify
382 	 * what's in there anyway
383 	 */
384 	do_sigaltstack(&uc->uc_stack, NULL, regs->gpr[1]);
385 
386 	set_thread_flag(TIF_RESTOREALL);
387 	return 0;
388 
389 badframe:
390 #if DEBUG_SIG
391 	printk("badframe in sys_rt_sigreturn, regs=%p uc=%p &uc->uc_mcontext=%p\n",
392 	       regs, uc, &uc->uc_mcontext);
393 #endif
394 	force_sig(SIGSEGV, current);
395 	return 0;
396 }
397 
398 static int setup_rt_frame(int signr, struct k_sigaction *ka, siginfo_t *info,
399 		sigset_t *set, struct pt_regs *regs)
400 {
401 	/* Handler is *really* a pointer to the function descriptor for
402 	 * the signal routine.  The first entry in the function
403 	 * descriptor is the entry address of signal and the second
404 	 * entry is the TOC value we need to use.
405 	 */
406 	func_descr_t __user *funct_desc_ptr;
407 	struct rt_sigframe __user *frame;
408 	unsigned long newsp = 0;
409 	long err = 0;
410 
411 	frame = get_sigframe(ka, regs, sizeof(*frame));
412 
413 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
414 		goto badframe;
415 
416 	err |= __put_user(&frame->info, &frame->pinfo);
417 	err |= __put_user(&frame->uc, &frame->puc);
418 	err |= copy_siginfo_to_user(&frame->info, info);
419 	if (err)
420 		goto badframe;
421 
422 	/* Create the ucontext.  */
423 	err |= __put_user(0, &frame->uc.uc_flags);
424 	err |= __put_user(0, &frame->uc.uc_link);
425 	err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
426 	err |= __put_user(sas_ss_flags(regs->gpr[1]),
427 			  &frame->uc.uc_stack.ss_flags);
428 	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
429 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, signr, NULL,
430 				(unsigned long)ka->sa.sa_handler);
431 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
432 	if (err)
433 		goto badframe;
434 
435 	/* Make sure signal handler doesn't get spurious FP exceptions */
436 	current->thread.fpscr.val = 0;
437 
438 	/* Set up to return from userspace. */
439 	if (vdso64_rt_sigtramp && current->thread.vdso_base) {
440 		regs->link = current->thread.vdso_base + vdso64_rt_sigtramp;
441 	} else {
442 		err |= setup_trampoline(__NR_rt_sigreturn, &frame->tramp[0]);
443 		if (err)
444 			goto badframe;
445 		regs->link = (unsigned long) &frame->tramp[0];
446 	}
447 	funct_desc_ptr = (func_descr_t __user *) ka->sa.sa_handler;
448 
449 	/* Allocate a dummy caller frame for the signal handler. */
450 	newsp = (unsigned long)frame - __SIGNAL_FRAMESIZE;
451 	err |= put_user(regs->gpr[1], (unsigned long __user *)newsp);
452 
453 	/* Set up "regs" so we "return" to the signal handler. */
454 	err |= get_user(regs->nip, &funct_desc_ptr->entry);
455 	regs->gpr[1] = newsp;
456 	err |= get_user(regs->gpr[2], &funct_desc_ptr->toc);
457 	regs->gpr[3] = signr;
458 	regs->result = 0;
459 	if (ka->sa.sa_flags & SA_SIGINFO) {
460 		err |= get_user(regs->gpr[4], (unsigned long __user *)&frame->pinfo);
461 		err |= get_user(regs->gpr[5], (unsigned long __user *)&frame->puc);
462 		regs->gpr[6] = (unsigned long) frame;
463 	} else {
464 		regs->gpr[4] = (unsigned long)&frame->uc.uc_mcontext;
465 	}
466 	if (err)
467 		goto badframe;
468 
469 	return 1;
470 
471 badframe:
472 #if DEBUG_SIG
473 	printk("badframe in setup_rt_frame, regs=%p frame=%p newsp=%lx\n",
474 	       regs, frame, newsp);
475 #endif
476 	force_sigsegv(signr, current);
477 	return 0;
478 }
479 
480 
481 /*
482  * OK, we're invoking a handler
483  */
484 static int handle_signal(unsigned long sig, struct k_sigaction *ka,
485 			 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
486 {
487 	int ret;
488 
489 	/* Set up Signal Frame */
490 	ret = setup_rt_frame(sig, ka, info, oldset, regs);
491 
492 	if (ret) {
493 		spin_lock_irq(&current->sighand->siglock);
494 		sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
495 		if (!(ka->sa.sa_flags & SA_NODEFER))
496 			sigaddset(&current->blocked,sig);
497 		recalc_sigpending();
498 		spin_unlock_irq(&current->sighand->siglock);
499 	}
500 
501 	return ret;
502 }
503 
504 static inline void syscall_restart(struct pt_regs *regs, struct k_sigaction *ka)
505 {
506 	switch ((int)regs->result) {
507 	case -ERESTART_RESTARTBLOCK:
508 	case -ERESTARTNOHAND:
509 		/* ERESTARTNOHAND means that the syscall should only be
510 		 * restarted if there was no handler for the signal, and since
511 		 * we only get here if there is a handler, we dont restart.
512 		 */
513 		regs->result = -EINTR;
514 		regs->gpr[3] = EINTR;
515 		regs->ccr |= 0x10000000;
516 		break;
517 	case -ERESTARTSYS:
518 		/* ERESTARTSYS means to restart the syscall if there is no
519 		 * handler or the handler was registered with SA_RESTART
520 		 */
521 		if (!(ka->sa.sa_flags & SA_RESTART)) {
522 			regs->result = -EINTR;
523 			regs->gpr[3] = EINTR;
524 			regs->ccr |= 0x10000000;
525 			break;
526 		}
527 		/* fallthrough */
528 	case -ERESTARTNOINTR:
529 		/* ERESTARTNOINTR means that the syscall should be
530 		 * called again after the signal handler returns.
531 		 */
532 		regs->gpr[3] = regs->orig_gpr3;
533 		regs->nip -= 4;
534 		regs->result = 0;
535 		break;
536 	}
537 }
538 
539 /*
540  * Note that 'init' is a special process: it doesn't get signals it doesn't
541  * want to handle. Thus you cannot kill init even with a SIGKILL even by
542  * mistake.
543  */
544 int do_signal(sigset_t *oldset, struct pt_regs *regs)
545 {
546 	siginfo_t info;
547 	int signr;
548 	struct k_sigaction ka;
549 
550 	/*
551 	 * If the current thread is 32 bit - invoke the
552 	 * 32 bit signal handling code
553 	 */
554 	if (test_thread_flag(TIF_32BIT))
555 		return do_signal32(oldset, regs);
556 
557 	if (!oldset)
558 		oldset = &current->blocked;
559 
560 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
561 	if (signr > 0) {
562 		/* Whee!  Actually deliver the signal.  */
563 		if (TRAP(regs) == 0x0C00)
564 			syscall_restart(regs, &ka);
565 
566 		/*
567 		 * Reenable the DABR before delivering the signal to
568 		 * user space. The DABR will have been cleared if it
569 		 * triggered inside the kernel.
570 		 */
571 		if (current->thread.dabr)
572 			set_dabr(current->thread.dabr);
573 
574 		return handle_signal(signr, &ka, &info, oldset, regs);
575 	}
576 
577 	if (TRAP(regs) == 0x0C00) {	/* System Call! */
578 		if ((int)regs->result == -ERESTARTNOHAND ||
579 		    (int)regs->result == -ERESTARTSYS ||
580 		    (int)regs->result == -ERESTARTNOINTR) {
581 			regs->gpr[3] = regs->orig_gpr3;
582 			regs->nip -= 4; /* Back up & retry system call */
583 			regs->result = 0;
584 		} else if ((int)regs->result == -ERESTART_RESTARTBLOCK) {
585 			regs->gpr[0] = __NR_restart_syscall;
586 			regs->nip -= 4;
587 			regs->result = 0;
588 		}
589 	}
590 
591 	return 0;
592 }
593 EXPORT_SYMBOL(do_signal);
594