xref: /openbmc/linux/arch/x86/kernel/signal_64.c (revision ead9d23d803ea3a73766c3cb27bf7563ac8d7266)
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
4  *
5  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
6  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
7  *  2000-2002   x86-64 support by Andi Kleen
8  */
9 
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/errno.h>
16 #include <linux/wait.h>
17 #include <linux/ptrace.h>
18 #include <linux/tracehook.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/compiler.h>
23 #include <linux/uaccess.h>
24 
25 #include <asm/processor.h>
26 #include <asm/ucontext.h>
27 #include <asm/i387.h>
28 #include <asm/proto.h>
29 #include <asm/ia32_unistd.h>
30 #include <asm/mce.h>
31 #include <asm/syscall.h>
32 #include <asm/syscalls.h>
33 #include "sigframe.h"
34 
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 
37 #define __FIX_EFLAGS	(X86_EFLAGS_AC | X86_EFLAGS_OF | \
38 			 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
39 			 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
40 			 X86_EFLAGS_CF)
41 
42 #ifdef CONFIG_X86_32
43 # define FIX_EFLAGS	(__FIX_EFLAGS | X86_EFLAGS_RF)
44 #else
45 # define FIX_EFLAGS	__FIX_EFLAGS
46 #endif
47 
48 asmlinkage long
49 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
50 		struct pt_regs *regs)
51 {
52 	return do_sigaltstack(uss, uoss, regs->sp);
53 }
54 
55 /*
56  * Do a signal return; undo the signal stack.
57  */
58 static int
59 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
60 		   unsigned long *pax)
61 {
62 	unsigned int err = 0;
63 
64 	/* Always make any pending restarted system calls return -EINTR */
65 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
66 
67 #define COPY(x)		(err |= __get_user(regs->x, &sc->x))
68 
69 	COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
70 	COPY(dx); COPY(cx); COPY(ip);
71 	COPY(r8);
72 	COPY(r9);
73 	COPY(r10);
74 	COPY(r11);
75 	COPY(r12);
76 	COPY(r13);
77 	COPY(r14);
78 	COPY(r15);
79 
80 	/* Kernel saves and restores only the CS segment register on signals,
81 	 * which is the bare minimum needed to allow mixed 32/64-bit code.
82 	 * App's signal handler can save/restore other segments if needed. */
83 	{
84 		unsigned cs;
85 		err |= __get_user(cs, &sc->cs);
86 		regs->cs = cs | 3;	/* Force into user mode */
87 	}
88 
89 	{
90 		unsigned int tmpflags;
91 		err |= __get_user(tmpflags, &sc->flags);
92 		regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
93 		regs->orig_ax = -1;		/* disable syscall checks */
94 	}
95 
96 	{
97 		struct _fpstate __user *buf;
98 		err |= __get_user(buf, &sc->fpstate);
99 		err |= restore_i387_xstate(buf);
100 	}
101 
102 	err |= __get_user(*pax, &sc->ax);
103 	return err;
104 }
105 
106 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
107 {
108 	struct rt_sigframe __user *frame;
109 	sigset_t set;
110 	unsigned long ax;
111 
112 	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
113 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
114 		goto badframe;
115 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
116 		goto badframe;
117 
118 	sigdelsetmask(&set, ~_BLOCKABLE);
119 	spin_lock_irq(&current->sighand->siglock);
120 	current->blocked = set;
121 	recalc_sigpending();
122 	spin_unlock_irq(&current->sighand->siglock);
123 
124 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
125 		goto badframe;
126 
127 	if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
128 		goto badframe;
129 
130 	return ax;
131 
132 badframe:
133 	signal_fault(regs, frame, "sigreturn");
134 	return 0;
135 }
136 
137 /*
138  * Set up a signal frame.
139  */
140 
141 static inline int
142 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
143 		unsigned long mask, struct task_struct *me)
144 {
145 	int err = 0;
146 
147 	err |= __put_user(regs->cs, &sc->cs);
148 	err |= __put_user(0, &sc->gs);
149 	err |= __put_user(0, &sc->fs);
150 
151 	err |= __put_user(regs->di, &sc->di);
152 	err |= __put_user(regs->si, &sc->si);
153 	err |= __put_user(regs->bp, &sc->bp);
154 	err |= __put_user(regs->sp, &sc->sp);
155 	err |= __put_user(regs->bx, &sc->bx);
156 	err |= __put_user(regs->dx, &sc->dx);
157 	err |= __put_user(regs->cx, &sc->cx);
158 	err |= __put_user(regs->ax, &sc->ax);
159 	err |= __put_user(regs->r8, &sc->r8);
160 	err |= __put_user(regs->r9, &sc->r9);
161 	err |= __put_user(regs->r10, &sc->r10);
162 	err |= __put_user(regs->r11, &sc->r11);
163 	err |= __put_user(regs->r12, &sc->r12);
164 	err |= __put_user(regs->r13, &sc->r13);
165 	err |= __put_user(regs->r14, &sc->r14);
166 	err |= __put_user(regs->r15, &sc->r15);
167 	err |= __put_user(me->thread.trap_no, &sc->trapno);
168 	err |= __put_user(me->thread.error_code, &sc->err);
169 	err |= __put_user(regs->ip, &sc->ip);
170 	err |= __put_user(regs->flags, &sc->flags);
171 	err |= __put_user(mask, &sc->oldmask);
172 	err |= __put_user(me->thread.cr2, &sc->cr2);
173 
174 	return err;
175 }
176 
177 /*
178  * Determine which stack to use..
179  */
180 
181 static void __user *
182 get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
183 {
184 	unsigned long sp;
185 
186 	/* Default to using normal stack - redzone*/
187 	sp = regs->sp - 128;
188 
189 	/* This is the X/Open sanctioned signal stack switching.  */
190 	if (ka->sa.sa_flags & SA_ONSTACK) {
191 		if (sas_ss_flags(sp) == 0)
192 			sp = current->sas_ss_sp + current->sas_ss_size;
193 	}
194 
195 	return (void __user *)round_down(sp - size, 64);
196 }
197 
198 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
199 			   sigset_t *set, struct pt_regs *regs)
200 {
201 	struct rt_sigframe __user *frame;
202 	void __user *fp = NULL;
203 	int err = 0;
204 	struct task_struct *me = current;
205 
206 	if (used_math()) {
207 		fp = get_stack(ka, regs, sig_xstate_size);
208 		frame = (void __user *)round_down(
209 			(unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
210 
211 		if (save_i387_xstate(fp) < 0)
212 			err |= -1;
213 	} else
214 		frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
215 
216 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
217 		goto give_sigsegv;
218 
219 	if (ka->sa.sa_flags & SA_SIGINFO) {
220 		err |= copy_siginfo_to_user(&frame->info, info);
221 		if (err)
222 			goto give_sigsegv;
223 	}
224 
225 	/* Create the ucontext.  */
226 	if (cpu_has_xsave)
227 		err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
228 	else
229 		err |= __put_user(0, &frame->uc.uc_flags);
230 	err |= __put_user(0, &frame->uc.uc_link);
231 	err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
232 	err |= __put_user(sas_ss_flags(regs->sp),
233 			  &frame->uc.uc_stack.ss_flags);
234 	err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
235 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
236 	err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
237 	if (sizeof(*set) == 16) {
238 		__put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
239 		__put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
240 	} else
241 		err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
242 
243 	/* Set up to return from userspace.  If provided, use a stub
244 	   already in userspace.  */
245 	/* x86-64 should always use SA_RESTORER. */
246 	if (ka->sa.sa_flags & SA_RESTORER) {
247 		err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
248 	} else {
249 		/* could use a vstub here */
250 		goto give_sigsegv;
251 	}
252 
253 	if (err)
254 		goto give_sigsegv;
255 
256 	/* Set up registers for signal handler */
257 	regs->di = sig;
258 	/* In case the signal handler was declared without prototypes */
259 	regs->ax = 0;
260 
261 	/* This also works for non SA_SIGINFO handlers because they expect the
262 	   next argument after the signal number on the stack. */
263 	regs->si = (unsigned long)&frame->info;
264 	regs->dx = (unsigned long)&frame->uc;
265 	regs->ip = (unsigned long) ka->sa.sa_handler;
266 
267 	regs->sp = (unsigned long)frame;
268 
269 	/* Set up the CS register to run signal handlers in 64-bit mode,
270 	   even if the handler happens to be interrupting 32-bit code. */
271 	regs->cs = __USER_CS;
272 
273 	return 0;
274 
275 give_sigsegv:
276 	force_sigsegv(sig, current);
277 	return -EFAULT;
278 }
279 
280 /*
281  * OK, we're invoking a handler
282  */
283 
284 static int
285 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
286 	      sigset_t *oldset, struct pt_regs *regs)
287 {
288 	int ret;
289 
290 	/* Are we from a system call? */
291 	if (syscall_get_nr(current, regs) >= 0) {
292 		/* If so, check system call restarting.. */
293 		switch (syscall_get_error(current, regs)) {
294 		case -ERESTART_RESTARTBLOCK:
295 		case -ERESTARTNOHAND:
296 			regs->ax = -EINTR;
297 			break;
298 
299 		case -ERESTARTSYS:
300 			if (!(ka->sa.sa_flags & SA_RESTART)) {
301 				regs->ax = -EINTR;
302 				break;
303 			}
304 		/* fallthrough */
305 		case -ERESTARTNOINTR:
306 			regs->ax = regs->orig_ax;
307 			regs->ip -= 2;
308 			break;
309 		}
310 	}
311 
312 	/*
313 	 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
314 	 * flag so that register information in the sigcontext is correct.
315 	 */
316 	if (unlikely(regs->flags & X86_EFLAGS_TF) &&
317 	    likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
318 		regs->flags &= ~X86_EFLAGS_TF;
319 
320 #ifdef CONFIG_IA32_EMULATION
321 	if (test_thread_flag(TIF_IA32)) {
322 		if (ka->sa.sa_flags & SA_SIGINFO)
323 			ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
324 		else
325 			ret = ia32_setup_frame(sig, ka, oldset, regs);
326 	} else
327 #endif
328 	ret = setup_rt_frame(sig, ka, info, oldset, regs);
329 
330 	if (ret == 0) {
331 		/*
332 		 * This has nothing to do with segment registers,
333 		 * despite the name.  This magic affects uaccess.h
334 		 * macros' behavior.  Reset it to the normal setting.
335 		 */
336 		set_fs(USER_DS);
337 
338 		/*
339 		 * Clear the direction flag as per the ABI for function entry.
340 		 */
341 		regs->flags &= ~X86_EFLAGS_DF;
342 
343 		/*
344 		 * Clear TF when entering the signal handler, but
345 		 * notify any tracer that was single-stepping it.
346 		 * The tracer may want to single-step inside the
347 		 * handler too.
348 		 */
349 		regs->flags &= ~X86_EFLAGS_TF;
350 
351 		spin_lock_irq(&current->sighand->siglock);
352 		sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
353 		if (!(ka->sa.sa_flags & SA_NODEFER))
354 			sigaddset(&current->blocked, sig);
355 		recalc_sigpending();
356 		spin_unlock_irq(&current->sighand->siglock);
357 
358 		tracehook_signal_handler(sig, info, ka, regs,
359 					 test_thread_flag(TIF_SINGLESTEP));
360 	}
361 
362 	return ret;
363 }
364 
365 /*
366  * Note that 'init' is a special process: it doesn't get signals it doesn't
367  * want to handle. Thus you cannot kill init even with a SIGKILL even by
368  * mistake.
369  */
370 static void do_signal(struct pt_regs *regs)
371 {
372 	struct k_sigaction ka;
373 	siginfo_t info;
374 	int signr;
375 	sigset_t *oldset;
376 
377 	/*
378 	 * We want the common case to go fast, which is why we may in certain
379 	 * cases get here from kernel mode. Just return without doing anything
380 	 * if so.
381 	 * X86_32: vm86 regs switched out by assembly code before reaching
382 	 * here, so testing against kernel CS suffices.
383 	 */
384 	if (!user_mode(regs))
385 		return;
386 
387 	if (current_thread_info()->status & TS_RESTORE_SIGMASK)
388 		oldset = &current->saved_sigmask;
389 	else
390 		oldset = &current->blocked;
391 
392 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
393 	if (signr > 0) {
394 		/* Re-enable any watchpoints before delivering the
395 		 * signal to user space. The processor register will
396 		 * have been cleared if the watchpoint triggered
397 		 * inside the kernel.
398 		 */
399 		if (current->thread.debugreg7)
400 			set_debugreg(current->thread.debugreg7, 7);
401 
402 		/* Whee!  Actually deliver the signal.  */
403 		if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
404 			/*
405 			 * A signal was successfully delivered; the saved
406 			 * sigmask will have been stored in the signal frame,
407 			 * and will be restored by sigreturn, so we can simply
408 			 * clear the TS_RESTORE_SIGMASK flag.
409 			 */
410 			current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
411 		}
412 		return;
413 	}
414 
415 	/* Did we come from a system call? */
416 	if (syscall_get_nr(current, regs) >= 0) {
417 		/* Restart the system call - no handlers present */
418 		switch (syscall_get_error(current, regs)) {
419 		case -ERESTARTNOHAND:
420 		case -ERESTARTSYS:
421 		case -ERESTARTNOINTR:
422 			regs->ax = regs->orig_ax;
423 			regs->ip -= 2;
424 			break;
425 		case -ERESTART_RESTARTBLOCK:
426 			regs->ax = test_thread_flag(TIF_IA32) ?
427 					__NR_ia32_restart_syscall :
428 					__NR_restart_syscall;
429 			regs->ip -= 2;
430 			break;
431 		}
432 	}
433 
434 	/*
435 	 * If there's no signal to deliver, we just put the saved sigmask
436 	 * back.
437 	 */
438 	if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
439 		current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
440 		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
441 	}
442 }
443 
444 void do_notify_resume(struct pt_regs *regs, void *unused,
445 		      __u32 thread_info_flags)
446 {
447 #ifdef CONFIG_X86_MCE
448 	/* notify userspace of pending MCEs */
449 	if (thread_info_flags & _TIF_MCE_NOTIFY)
450 		mce_notify_user();
451 #endif /* CONFIG_X86_MCE */
452 
453 	/* deal with pending signal delivery */
454 	if (thread_info_flags & _TIF_SIGPENDING)
455 		do_signal(regs);
456 
457 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
458 		clear_thread_flag(TIF_NOTIFY_RESUME);
459 		tracehook_notify_resume(regs);
460 	}
461 }
462 
463 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
464 {
465 	struct task_struct *me = current;
466 	if (show_unhandled_signals && printk_ratelimit()) {
467 		printk("%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
468 	       me->comm, me->pid, where, frame, regs->ip,
469 		   regs->sp, regs->orig_ax);
470 		print_vma_addr(" in ", regs->ip);
471 		printk("\n");
472 	}
473 
474 	force_sig(SIGSEGV, me);
475 }
476