xref: /linux/arch/x86/um/signal.c (revision ad73b9a17d66366d8c9198bc90f1ea99f24a912c)
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 	 * Skip this if we do not have an XSTATE frame.
192 	 */
193 	if (host_fp_size <= sizeof(to_fp64->fpstate))
194 		return 0;
195 
196 	BUILD_BUG_ON(sizeof(int) != FP_XSTATE_MAGIC2_SIZE);
197 #ifdef CONFIG_X86_32
198 	__put_user(offsetof(struct _fpstate_32, _fxsr_env) +
199 		   host_fp_size + FP_XSTATE_MAGIC2_SIZE,
200 		   &to_fp64->fpstate.sw_reserved.extended_size);
201 #else
202 	__put_user(host_fp_size + FP_XSTATE_MAGIC2_SIZE,
203 		   &to_fp64->fpstate.sw_reserved.extended_size);
204 #endif
205 	__put_user(host_fp_size, &to_fp64->fpstate.sw_reserved.xstate_size);
206 
207 	__put_user(FP_XSTATE_MAGIC1, &to_fp64->fpstate.sw_reserved.magic1);
208 	__put_user(FP_XSTATE_MAGIC2,
209 		   (int __user *)((void __user *)to_fp64 + host_fp_size));
210 
211 	return 0;
212 }
213 
214 #ifdef CONFIG_X86_32
copy_ucontext_to_user(struct ucontext __user * uc,struct _xstate __user * fp,sigset_t * set,unsigned long sp)215 static int copy_ucontext_to_user(struct ucontext __user *uc,
216 				 struct _xstate __user *fp, sigset_t *set,
217 				 unsigned long sp)
218 {
219 	int err = 0;
220 
221 	err |= __save_altstack(&uc->uc_stack, sp);
222 	err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, 0);
223 	err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
224 	return err;
225 }
226 
setup_signal_stack_sc(unsigned long stack_top,struct ksignal * ksig,struct pt_regs * regs,sigset_t * mask)227 int setup_signal_stack_sc(unsigned long stack_top, struct ksignal *ksig,
228 			  struct pt_regs *regs, sigset_t *mask)
229 {
230 	size_t math_size = offsetof(struct _fpstate_32, _fxsr_env) +
231 			   host_fp_size + FP_XSTATE_MAGIC2_SIZE;
232 	struct sigframe __user *frame;
233 	void __user *restorer;
234 	int err = 0, sig = ksig->sig;
235 	unsigned long fp_to;
236 
237 	/* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
238 	stack_top = ((stack_top + 4) & -16UL) - 4;
239 	frame = (struct sigframe __user *) stack_top - 1;
240 	if (!access_ok(frame, sizeof(*frame)))
241 		return 1;
242 
243 	/* Add required space for math frame */
244 	frame = (struct sigframe __user *)((unsigned long)frame - math_size);
245 
246 	restorer = frame->retcode;
247 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
248 		restorer = ksig->ka.sa.sa_restorer;
249 
250 	err |= __put_user(restorer, (void __user * __user *)&frame->pretcode);
251 	err |= __put_user(sig, &frame->sig);
252 
253 	fp_to = (unsigned long)frame + sizeof(*frame);
254 
255 	err |= copy_sc_to_user(&frame->sc,
256 			       (struct _xstate __user *)fp_to,
257 			       regs, mask->sig[0]);
258 	if (_NSIG_WORDS > 1)
259 		err |= __copy_to_user(&frame->extramask, &mask->sig[1],
260 				      sizeof(frame->extramask));
261 
262 	/*
263 	 * This is popl %eax ; movl $,%eax ; int $0x80
264 	 *
265 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
266 	 * reasons and because gdb uses it as a signature to notice
267 	 * signal handler stack frames.
268 	 */
269 	err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
270 	err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
271 	err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
272 
273 	if (err)
274 		return err;
275 
276 	PT_REGS_SP(regs) = (unsigned long) frame;
277 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
278 	PT_REGS_AX(regs) = (unsigned long) sig;
279 	PT_REGS_DX(regs) = (unsigned long) 0;
280 	PT_REGS_CX(regs) = (unsigned long) 0;
281 	return 0;
282 }
283 
setup_signal_stack_si(unsigned long stack_top,struct ksignal * ksig,struct pt_regs * regs,sigset_t * mask)284 int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
285 			  struct pt_regs *regs, sigset_t *mask)
286 {
287 	size_t math_size = offsetof(struct _fpstate_32, _fxsr_env) +
288 			   host_fp_size + FP_XSTATE_MAGIC2_SIZE;
289 	struct rt_sigframe __user *frame;
290 	void __user *restorer;
291 	int err = 0, sig = ksig->sig;
292 	unsigned long fp_to;
293 
294 	stack_top &= -8UL;
295 	frame = (struct rt_sigframe __user *) stack_top - 1;
296 	if (!access_ok(frame, sizeof(*frame)))
297 		return 1;
298 
299 	/* Add required space for math frame */
300 	frame = (struct rt_sigframe __user *)((unsigned long)frame - math_size);
301 
302 	restorer = frame->retcode;
303 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
304 		restorer = ksig->ka.sa.sa_restorer;
305 
306 	err |= __put_user(restorer, (void __user * __user *)&frame->pretcode);
307 	err |= __put_user(sig, &frame->sig);
308 	err |= __put_user(&frame->info, (void __user * __user *)&frame->pinfo);
309 	err |= __put_user(&frame->uc, (void __user * __user *)&frame->puc);
310 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
311 
312 	fp_to = (unsigned long)frame + sizeof(*frame);
313 
314 	err |= copy_ucontext_to_user(&frame->uc, (struct _xstate __user *)fp_to,
315 				     mask, PT_REGS_SP(regs));
316 
317 	/*
318 	 * This is movl $,%eax ; int $0x80
319 	 *
320 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
321 	 * reasons and because gdb uses it as a signature to notice
322 	 * signal handler stack frames.
323 	 */
324 	err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
325 	err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
326 	err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
327 
328 	if (err)
329 		return err;
330 
331 	PT_REGS_SP(regs) = (unsigned long) frame;
332 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
333 	PT_REGS_AX(regs) = (unsigned long) sig;
334 	PT_REGS_DX(regs) = (unsigned long) &frame->info;
335 	PT_REGS_CX(regs) = (unsigned long) &frame->uc;
336 	return 0;
337 }
338 
SYSCALL_DEFINE0(sigreturn)339 SYSCALL_DEFINE0(sigreturn)
340 {
341 	unsigned long sp = PT_REGS_SP(&current->thread.regs);
342 	struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
343 	sigset_t set;
344 	struct sigcontext __user *sc = &frame->sc;
345 	int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
346 
347 	if (copy_from_user(&set.sig[0], &sc->oldmask, sizeof(set.sig[0])) ||
348 	    copy_from_user(&set.sig[1], frame->extramask, sig_size))
349 		goto segfault;
350 
351 	set_current_blocked(&set);
352 
353 	if (copy_sc_from_user(&current->thread.regs, sc))
354 		goto segfault;
355 
356 	/* Avoid ERESTART handling */
357 	PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
358 	return PT_REGS_SYSCALL_RET(&current->thread.regs);
359 
360  segfault:
361 	force_sig(SIGSEGV);
362 	return 0;
363 }
364 
365 #else
366 
setup_signal_stack_si(unsigned long stack_top,struct ksignal * ksig,struct pt_regs * regs,sigset_t * set)367 int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
368 			  struct pt_regs *regs, sigset_t *set)
369 {
370 	unsigned long math_size = host_fp_size + FP_XSTATE_MAGIC2_SIZE;
371 	struct rt_sigframe __user *frame;
372 	int err = 0, sig = ksig->sig;
373 	unsigned long fp_to;
374 
375 	frame = (void __user *)stack_top - sizeof(struct rt_sigframe);
376 
377 	/* Add required space for math frame */
378 	frame = (void __user *)((unsigned long)frame - math_size);
379 
380 	/* ABI requires 16 byte boundary alignment */
381 	frame = (void __user *)round_down((unsigned long)frame, 16);
382 
383 	/* Subtract 128 for a red zone and 8 for proper alignment */
384 	frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
385 
386 	if (!access_ok(frame, sizeof(*frame) + math_size))
387 		goto out;
388 
389 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
390 		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
391 		if (err)
392 			goto out;
393 	}
394 
395 	/* Create the ucontext.  */
396 	err |= __put_user(0, &frame->uc.uc_flags);
397 	err |= __put_user(NULL, &frame->uc.uc_link);
398 	err |= __save_altstack(&frame->uc.uc_stack, PT_REGS_SP(regs));
399 
400 	fp_to = (unsigned long)frame + sizeof(*frame);
401 
402 	err |= copy_sc_to_user(&frame->uc.uc_mcontext,
403 			       (struct _xstate __user *)fp_to,
404 			       regs, set->sig[0]);
405 
406 	err |= __put_user(fp_to, &frame->uc.uc_mcontext.fpstate);
407 	if (sizeof(*set) == 16) {
408 		err |= __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
409 		err |= __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
410 	}
411 	else
412 		err |= __copy_to_user(&frame->uc.uc_sigmask, set,
413 				      sizeof(*set));
414 
415 	/*
416 	 * Set up to return from userspace.  If provided, use a stub
417 	 * already in userspace.
418 	 */
419 	/* x86-64 should always use SA_RESTORER. */
420 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
421 		err |= __put_user((void __user *)ksig->ka.sa.sa_restorer,
422 				  &frame->pretcode);
423 	else
424 		/* could use a vstub here */
425 		return err;
426 
427 	if (err)
428 		return err;
429 
430 	PT_REGS_SP(regs) = (unsigned long) frame;
431 	PT_REGS_DI(regs) = sig;
432 	/* In case the signal handler was declared without prototypes */
433 	PT_REGS_AX(regs) = 0;
434 
435 	/*
436 	 * This also works for non SA_SIGINFO handlers because they expect the
437 	 * next argument after the signal number on the stack.
438 	 */
439 	PT_REGS_SI(regs) = (unsigned long) &frame->info;
440 	PT_REGS_DX(regs) = (unsigned long) &frame->uc;
441 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
442  out:
443 	return err;
444 }
445 #endif
446 
SYSCALL_DEFINE0(rt_sigreturn)447 SYSCALL_DEFINE0(rt_sigreturn)
448 {
449 	unsigned long sp = PT_REGS_SP(&current->thread.regs);
450 	struct rt_sigframe __user *frame =
451 		(struct rt_sigframe __user *)(sp - sizeof(long));
452 	struct ucontext __user *uc = &frame->uc;
453 	sigset_t set;
454 
455 	if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
456 		goto segfault;
457 
458 	set_current_blocked(&set);
459 
460 	if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
461 		goto segfault;
462 
463 	/* Avoid ERESTART handling */
464 	PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
465 	return PT_REGS_SYSCALL_RET(&current->thread.regs);
466 
467  segfault:
468 	force_sig(SIGSEGV);
469 	return 0;
470 }
471