xref: /linux/arch/x86/um/signal.c (revision 831c1926ee728c3e747255f7c0f434762e8e863d)
1 /*
2  * Copyright (C) 2003 PathScale, Inc.
3  * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4  * Licensed under the GPL
5  */
6 
7 
8 #include <linux/personality.h>
9 #include <linux/ptrace.h>
10 #include <linux/kernel.h>
11 #include <linux/syscalls.h>
12 #include <asm/unistd.h>
13 #include <linux/uaccess.h>
14 #include <asm/ucontext.h>
15 #include <frame_kern.h>
16 #include <registers.h>
17 #include <skas.h>
18 
19 #include <linux/regset.h>
20 #include <asm/sigframe.h>
21 
22 #ifdef CONFIG_X86_32
23 struct _xstate_64 {
24 	struct _fpstate_64		fpstate;
25 	struct _header			xstate_hdr;
26 	struct _ymmh_state		ymmh;
27 	/* New processor state extensions go here: */
28 };
29 #else
30 #define _xstate_64 _xstate
31 #endif
32 
copy_sc_from_user(struct pt_regs * regs,struct sigcontext __user * from)33 static int copy_sc_from_user(struct pt_regs *regs,
34 			     struct sigcontext __user *from)
35 {
36 	struct _xstate_64 __user *from_fp64;
37 	struct sigcontext sc;
38 	int err;
39 
40 	/* Always make any pending restarted system calls return -EINTR */
41 	current->restart_block.fn = do_no_restart_syscall;
42 
43 	err = copy_from_user(&sc, from, sizeof(sc));
44 	if (err)
45 		return err;
46 
47 #define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
48 
49 #ifdef CONFIG_X86_32
50 	GETREG(GS, gs);
51 	GETREG(FS, fs);
52 	GETREG(ES, es);
53 	GETREG(DS, ds);
54 #endif
55 	GETREG(DI, di);
56 	GETREG(SI, si);
57 	GETREG(BP, bp);
58 	GETREG(SP, sp);
59 	GETREG(BX, bx);
60 	GETREG(DX, dx);
61 	GETREG(CX, cx);
62 	GETREG(AX, ax);
63 	GETREG(IP, ip);
64 
65 #ifdef CONFIG_X86_64
66 	GETREG(R8, r8);
67 	GETREG(R9, r9);
68 	GETREG(R10, r10);
69 	GETREG(R11, r11);
70 	GETREG(R12, r12);
71 	GETREG(R13, r13);
72 	GETREG(R14, r14);
73 	GETREG(R15, r15);
74 #endif
75 
76 	GETREG(CS, cs);
77 	GETREG(EFLAGS, flags);
78 #ifdef CONFIG_X86_32
79 	GETREG(SS, ss);
80 #endif
81 
82 #undef GETREG
83 
84 #ifdef CONFIG_X86_32
85 	from_fp64 = ((void __user *)sc.fpstate) +
86 		    offsetof(struct _fpstate_32, _fxsr_env);
87 #else
88 	from_fp64 = (void __user *)sc.fpstate;
89 #endif
90 
91 	err = copy_from_user(regs->regs.fp, from_fp64, host_fp_size);
92 	if (err)
93 		return 1;
94 
95 #ifdef CONFIG_X86_32
96 	/* Data is duplicated and this copy is the important one */
97 	err = copy_regset_from_user(current,
98 				    task_user_regset_view(current),
99 				    REGSET_FP_LEGACY, 0,
100 				    sizeof(struct user_i387_struct),
101 				    (void __user *)sc.fpstate);
102 	if (err < 0)
103 		return err;
104 #endif
105 
106 	return 0;
107 }
108 
copy_sc_to_user(struct sigcontext __user * to,struct _xstate __user * to_fp,struct pt_regs * regs,unsigned long mask)109 static int copy_sc_to_user(struct sigcontext __user *to,
110 			   struct _xstate __user *to_fp, struct pt_regs *regs,
111 			   unsigned long mask)
112 {
113 	struct _xstate_64 __user *to_fp64;
114 	struct sigcontext sc;
115 	struct faultinfo * fi = &current->thread.arch.faultinfo;
116 	int err;
117 	memset(&sc, 0, sizeof(struct sigcontext));
118 
119 #define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno]
120 
121 #ifdef CONFIG_X86_32
122 	PUTREG(GS, gs);
123 	PUTREG(FS, fs);
124 	PUTREG(ES, es);
125 	PUTREG(DS, ds);
126 #endif
127 	PUTREG(DI, di);
128 	PUTREG(SI, si);
129 	PUTREG(BP, bp);
130 	PUTREG(SP, sp);
131 	PUTREG(BX, bx);
132 	PUTREG(DX, dx);
133 	PUTREG(CX, cx);
134 	PUTREG(AX, ax);
135 #ifdef CONFIG_X86_64
136 	PUTREG(R8, r8);
137 	PUTREG(R9, r9);
138 	PUTREG(R10, r10);
139 	PUTREG(R11, r11);
140 	PUTREG(R12, r12);
141 	PUTREG(R13, r13);
142 	PUTREG(R14, r14);
143 	PUTREG(R15, r15);
144 #endif
145 
146 	sc.cr2 = fi->cr2;
147 	sc.err = fi->error_code;
148 	sc.trapno = fi->trap_no;
149 	PUTREG(IP, ip);
150 	PUTREG(CS, cs);
151 	PUTREG(EFLAGS, flags);
152 #ifdef CONFIG_X86_32
153 	PUTREG(SP, sp_at_signal);
154 	PUTREG(SS, ss);
155 #endif
156 #undef PUTREG
157 	sc.oldmask = mask;
158 	sc.fpstate = (unsigned long)to_fp;
159 
160 	err = copy_to_user(to, &sc, sizeof(struct sigcontext));
161 	if (err)
162 		return 1;
163 
164 #ifdef CONFIG_X86_32
165 	err = copy_regset_to_user(current,
166 				  task_user_regset_view(current),
167 				  REGSET_FP_LEGACY, 0,
168 				  sizeof(struct _fpstate_32), to_fp);
169 	if (err < 0)
170 		return err;
171 
172 	__put_user(X86_FXSR_MAGIC, &to_fp->fpstate.magic);
173 
174 	BUILD_BUG_ON(offsetof(struct _xstate, xstate_hdr) !=
175 		     offsetof(struct _xstate_64, xstate_hdr) +
176 			offsetof(struct _fpstate_32, _fxsr_env));
177 	to_fp64 = (void __user *)to_fp +
178 		  offsetof(struct _fpstate_32, _fxsr_env);
179 #else
180 	to_fp64 = to_fp;
181 #endif /* CONFIG_X86_32 */
182 
183 	if (copy_to_user(to_fp64, regs->regs.fp, host_fp_size))
184 		return 1;
185 
186 	/*
187 	 * Put magic/size values for userspace. We do not bother to verify them
188 	 * later on, however, userspace needs them should it try to read the
189 	 * XSTATE data. And ptrace does not fill in these parts.
190 	 */
191 	BUILD_BUG_ON(sizeof(int) != FP_XSTATE_MAGIC2_SIZE);
192 #ifdef CONFIG_X86_32
193 	__put_user(offsetof(struct _fpstate_32, _fxsr_env) +
194 		   host_fp_size + FP_XSTATE_MAGIC2_SIZE,
195 		   &to_fp64->fpstate.sw_reserved.extended_size);
196 #else
197 	__put_user(host_fp_size + FP_XSTATE_MAGIC2_SIZE,
198 		   &to_fp64->fpstate.sw_reserved.extended_size);
199 #endif
200 	__put_user(host_fp_size, &to_fp64->fpstate.sw_reserved.xstate_size);
201 
202 	__put_user(FP_XSTATE_MAGIC1, &to_fp64->fpstate.sw_reserved.magic1);
203 	__put_user(FP_XSTATE_MAGIC2,
204 		   (int __user *)((void __user *)to_fp64 + host_fp_size));
205 
206 	return 0;
207 }
208 
209 #ifdef CONFIG_X86_32
copy_ucontext_to_user(struct ucontext __user * uc,struct _xstate __user * fp,sigset_t * set,unsigned long sp)210 static int copy_ucontext_to_user(struct ucontext __user *uc,
211 				 struct _xstate __user *fp, sigset_t *set,
212 				 unsigned long sp)
213 {
214 	int err = 0;
215 
216 	err |= __save_altstack(&uc->uc_stack, sp);
217 	err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, 0);
218 	err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
219 	return err;
220 }
221 
setup_signal_stack_sc(unsigned long stack_top,struct ksignal * ksig,struct pt_regs * regs,sigset_t * mask)222 int setup_signal_stack_sc(unsigned long stack_top, struct ksignal *ksig,
223 			  struct pt_regs *regs, sigset_t *mask)
224 {
225 	size_t math_size = offsetof(struct _fpstate_32, _fxsr_env) +
226 			   host_fp_size + FP_XSTATE_MAGIC2_SIZE;
227 	struct sigframe __user *frame;
228 	void __user *restorer;
229 	int err = 0, sig = ksig->sig;
230 	unsigned long fp_to;
231 
232 	/* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
233 	stack_top = ((stack_top + 4) & -16UL) - 4;
234 	frame = (struct sigframe __user *) stack_top - 1;
235 	if (!access_ok(frame, sizeof(*frame)))
236 		return 1;
237 
238 	/* Add required space for math frame */
239 	frame = (struct sigframe __user *)((unsigned long)frame - math_size);
240 
241 	restorer = frame->retcode;
242 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
243 		restorer = ksig->ka.sa.sa_restorer;
244 
245 	err |= __put_user(restorer, (void __user * __user *)&frame->pretcode);
246 	err |= __put_user(sig, &frame->sig);
247 
248 	fp_to = (unsigned long)frame + sizeof(*frame);
249 
250 	err |= copy_sc_to_user(&frame->sc,
251 			       (struct _xstate __user *)fp_to,
252 			       regs, mask->sig[0]);
253 	if (_NSIG_WORDS > 1)
254 		err |= __copy_to_user(&frame->extramask, &mask->sig[1],
255 				      sizeof(frame->extramask));
256 
257 	/*
258 	 * This is popl %eax ; movl $,%eax ; int $0x80
259 	 *
260 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
261 	 * reasons and because gdb uses it as a signature to notice
262 	 * signal handler stack frames.
263 	 */
264 	err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
265 	err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
266 	err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
267 
268 	if (err)
269 		return err;
270 
271 	PT_REGS_SP(regs) = (unsigned long) frame;
272 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
273 	PT_REGS_AX(regs) = (unsigned long) sig;
274 	PT_REGS_DX(regs) = (unsigned long) 0;
275 	PT_REGS_CX(regs) = (unsigned long) 0;
276 	return 0;
277 }
278 
setup_signal_stack_si(unsigned long stack_top,struct ksignal * ksig,struct pt_regs * regs,sigset_t * mask)279 int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
280 			  struct pt_regs *regs, sigset_t *mask)
281 {
282 	size_t math_size = offsetof(struct _fpstate_32, _fxsr_env) +
283 			   host_fp_size + FP_XSTATE_MAGIC2_SIZE;
284 	struct rt_sigframe __user *frame;
285 	void __user *restorer;
286 	int err = 0, sig = ksig->sig;
287 	unsigned long fp_to;
288 
289 	stack_top &= -8UL;
290 	frame = (struct rt_sigframe __user *) stack_top - 1;
291 	if (!access_ok(frame, sizeof(*frame)))
292 		return 1;
293 
294 	/* Add required space for math frame */
295 	frame = (struct rt_sigframe __user *)((unsigned long)frame - math_size);
296 
297 	restorer = frame->retcode;
298 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
299 		restorer = ksig->ka.sa.sa_restorer;
300 
301 	err |= __put_user(restorer, (void __user * __user *)&frame->pretcode);
302 	err |= __put_user(sig, &frame->sig);
303 	err |= __put_user(&frame->info, (void __user * __user *)&frame->pinfo);
304 	err |= __put_user(&frame->uc, (void __user * __user *)&frame->puc);
305 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
306 
307 	fp_to = (unsigned long)frame + sizeof(*frame);
308 
309 	err |= copy_ucontext_to_user(&frame->uc, (struct _xstate __user *)fp_to,
310 				     mask, PT_REGS_SP(regs));
311 
312 	/*
313 	 * This is movl $,%eax ; int $0x80
314 	 *
315 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
316 	 * reasons and because gdb uses it as a signature to notice
317 	 * signal handler stack frames.
318 	 */
319 	err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
320 	err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
321 	err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
322 
323 	if (err)
324 		return err;
325 
326 	PT_REGS_SP(regs) = (unsigned long) frame;
327 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
328 	PT_REGS_AX(regs) = (unsigned long) sig;
329 	PT_REGS_DX(regs) = (unsigned long) &frame->info;
330 	PT_REGS_CX(regs) = (unsigned long) &frame->uc;
331 	return 0;
332 }
333 
SYSCALL_DEFINE0(sigreturn)334 SYSCALL_DEFINE0(sigreturn)
335 {
336 	unsigned long sp = PT_REGS_SP(&current->thread.regs);
337 	struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
338 	sigset_t set;
339 	struct sigcontext __user *sc = &frame->sc;
340 	int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
341 
342 	if (copy_from_user(&set.sig[0], &sc->oldmask, sizeof(set.sig[0])) ||
343 	    copy_from_user(&set.sig[1], frame->extramask, sig_size))
344 		goto segfault;
345 
346 	set_current_blocked(&set);
347 
348 	if (copy_sc_from_user(&current->thread.regs, sc))
349 		goto segfault;
350 
351 	/* Avoid ERESTART handling */
352 	PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
353 	return PT_REGS_SYSCALL_RET(&current->thread.regs);
354 
355  segfault:
356 	force_sig(SIGSEGV);
357 	return 0;
358 }
359 
360 #else
361 
setup_signal_stack_si(unsigned long stack_top,struct ksignal * ksig,struct pt_regs * regs,sigset_t * set)362 int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
363 			  struct pt_regs *regs, sigset_t *set)
364 {
365 	unsigned long math_size = host_fp_size + FP_XSTATE_MAGIC2_SIZE;
366 	struct rt_sigframe __user *frame;
367 	int err = 0, sig = ksig->sig;
368 	unsigned long fp_to;
369 
370 	frame = (struct rt_sigframe __user *)
371 		round_down(stack_top - sizeof(struct rt_sigframe), 16);
372 
373 	/* Add required space for math frame */
374 	frame = (struct rt_sigframe __user *)((unsigned long)frame - math_size);
375 
376 	/* Subtract 128 for a red zone and 8 for proper alignment */
377 	frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
378 
379 	if (!access_ok(frame, sizeof(*frame) + math_size))
380 		goto out;
381 
382 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
383 		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
384 		if (err)
385 			goto out;
386 	}
387 
388 	/* Create the ucontext.  */
389 	err |= __put_user(0, &frame->uc.uc_flags);
390 	err |= __put_user(NULL, &frame->uc.uc_link);
391 	err |= __save_altstack(&frame->uc.uc_stack, PT_REGS_SP(regs));
392 
393 	fp_to = (unsigned long)frame + sizeof(*frame);
394 
395 	err |= copy_sc_to_user(&frame->uc.uc_mcontext,
396 			       (struct _xstate __user *)fp_to,
397 			       regs, set->sig[0]);
398 
399 	err |= __put_user(fp_to, &frame->uc.uc_mcontext.fpstate);
400 	if (sizeof(*set) == 16) {
401 		err |= __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
402 		err |= __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
403 	}
404 	else
405 		err |= __copy_to_user(&frame->uc.uc_sigmask, set,
406 				      sizeof(*set));
407 
408 	/*
409 	 * Set up to return from userspace.  If provided, use a stub
410 	 * already in userspace.
411 	 */
412 	/* x86-64 should always use SA_RESTORER. */
413 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
414 		err |= __put_user((void __user *)ksig->ka.sa.sa_restorer,
415 				  &frame->pretcode);
416 	else
417 		/* could use a vstub here */
418 		return err;
419 
420 	if (err)
421 		return err;
422 
423 	PT_REGS_SP(regs) = (unsigned long) frame;
424 	PT_REGS_DI(regs) = sig;
425 	/* In case the signal handler was declared without prototypes */
426 	PT_REGS_AX(regs) = 0;
427 
428 	/*
429 	 * This also works for non SA_SIGINFO handlers because they expect the
430 	 * next argument after the signal number on the stack.
431 	 */
432 	PT_REGS_SI(regs) = (unsigned long) &frame->info;
433 	PT_REGS_DX(regs) = (unsigned long) &frame->uc;
434 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
435  out:
436 	return err;
437 }
438 #endif
439 
SYSCALL_DEFINE0(rt_sigreturn)440 SYSCALL_DEFINE0(rt_sigreturn)
441 {
442 	unsigned long sp = PT_REGS_SP(&current->thread.regs);
443 	struct rt_sigframe __user *frame =
444 		(struct rt_sigframe __user *)(sp - sizeof(long));
445 	struct ucontext __user *uc = &frame->uc;
446 	sigset_t set;
447 
448 	if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
449 		goto segfault;
450 
451 	set_current_blocked(&set);
452 
453 	if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
454 		goto segfault;
455 
456 	/* Avoid ERESTART handling */
457 	PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
458 	return PT_REGS_SYSCALL_RET(&current->thread.regs);
459 
460  segfault:
461 	force_sig(SIGSEGV);
462 	return 0;
463 }
464