]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/um/signal_32.c
um: increase stack growth cushion in pagefault
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / um / signal_32.c
CommitLineData
1da177e4 1/*
ba180fd4 2 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
3 * Licensed under the GPL
4 */
5
7796931f
JD
6#include <linux/ptrace.h>
7#include <asm/unistd.h>
8#include <asm/uaccess.h>
9#include <asm/ucontext.h>
1da177e4 10#include "frame_kern.h"
1da177e4
LT
11#include "skas.h"
12
a5f6096c
JD
13/*
14 * FPU tag word conversions.
15 */
16
17static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
18{
19 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
20
21 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
22 tmp = ~twd;
23 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
24 /* and move the valid bits to the lower byte. */
25 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
26 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
27 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
28 return tmp;
29}
30
31static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct *fxsave)
32{
33 struct _fpxreg *st = NULL;
34 unsigned long twd = (unsigned long) fxsave->twd;
35 unsigned long tag;
36 unsigned long ret = 0xffff0000;
37 int i;
38
497888cf 39#define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16)
a5f6096c
JD
40
41 for (i = 0; i < 8; i++) {
42 if (twd & 0x1) {
43 st = (struct _fpxreg *) FPREG_ADDR(fxsave, i);
44
45 switch (st->exponent & 0x7fff) {
46 case 0x7fff:
47 tag = 2; /* Special */
48 break;
49 case 0x0000:
50 if ( !st->significand[0] &&
51 !st->significand[1] &&
52 !st->significand[2] &&
53 !st->significand[3] ) {
54 tag = 1; /* Zero */
55 } else {
56 tag = 2; /* Special */
57 }
58 break;
59 default:
60 if (st->significand[3] & 0x8000) {
61 tag = 0; /* Valid */
62 } else {
63 tag = 2; /* Special */
64 }
65 break;
66 }
67 } else {
68 tag = 3; /* Empty */
69 }
70 ret |= (tag << (2 * i));
71 twd = twd >> 1;
72 }
73 return ret;
74}
75
76static int convert_fxsr_to_user(struct _fpstate __user *buf,
77 struct user_fxsr_struct *fxsave)
78{
79 unsigned long env[7];
80 struct _fpreg __user *to;
81 struct _fpxreg *from;
82 int i;
83
84 env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
85 env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
86 env[2] = twd_fxsr_to_i387(fxsave);
87 env[3] = fxsave->fip;
88 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
89 env[5] = fxsave->foo;
90 env[6] = fxsave->fos;
91
92 if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
93 return 1;
94
95 to = &buf->_st[0];
96 from = (struct _fpxreg *) &fxsave->st_space[0];
97 for (i = 0; i < 8; i++, to++, from++) {
98 unsigned long __user *t = (unsigned long __user *)to;
99 unsigned long *f = (unsigned long *)from;
100
101 if (__put_user(*f, t) ||
102 __put_user(*(f + 1), t + 1) ||
103 __put_user(from->exponent, &to->exponent))
104 return 1;
105 }
106 return 0;
107}
108
109static int convert_fxsr_from_user(struct user_fxsr_struct *fxsave,
110 struct _fpstate __user *buf)
111{
112 unsigned long env[7];
113 struct _fpxreg *to;
114 struct _fpreg __user *from;
115 int i;
116
117 if (copy_from_user( env, buf, 7 * sizeof(long)))
118 return 1;
119
120 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
121 fxsave->swd = (unsigned short)(env[1] & 0xffff);
122 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
123 fxsave->fip = env[3];
124 fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
125 fxsave->fcs = (env[4] & 0xffff);
126 fxsave->foo = env[5];
127 fxsave->fos = env[6];
128
129 to = (struct _fpxreg *) &fxsave->st_space[0];
130 from = &buf->_st[0];
131 for (i = 0; i < 8; i++, to++, from++) {
132 unsigned long *t = (unsigned long *)to;
133 unsigned long __user *f = (unsigned long __user *)from;
134
135 if (__get_user(*t, f) ||
136 __get_user(*(t + 1), f + 1) ||
137 __get_user(to->exponent, &from->exponent))
138 return 1;
139 }
140 return 0;
141}
142
143extern int have_fpx_regs;
144
77bf4400
JD
145static int copy_sc_from_user(struct pt_regs *regs,
146 struct sigcontext __user *from)
1da177e4 147{
a5f6096c 148 struct sigcontext sc;
a5a678c8 149 int err, pid;
1da177e4
LT
150
151 err = copy_from_user(&sc, from, sizeof(sc));
ba180fd4 152 if (err)
5d86456d 153 return err;
1da177e4 154
a5a678c8 155 pid = userspace_pid[current_thread_info()->cpu];
a955bd61
AV
156
157#define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
158
159 GETREG(GS, gs);
160 GETREG(FS, fs);
161 GETREG(ES, es);
162 GETREG(DS, ds);
3579a389
AV
163 GETREG(DI, di);
164 GETREG(SI, si);
165 GETREG(BP, bp);
a955bd61 166 GETREG(SP, sp);
3579a389
AV
167 GETREG(BX, bx);
168 GETREG(DX, dx);
169 GETREG(CX, cx);
170 GETREG(AX, ax);
a955bd61
AV
171 GETREG(IP, ip);
172 GETREG(CS, cs);
173 GETREG(EFLAGS, flags);
174 GETREG(SS, ss);
175
176#undef GETREG
a5f6096c
JD
177 if (have_fpx_regs) {
178 struct user_fxsr_struct fpx;
1da177e4 179
966c8079
WC
180 err = copy_from_user(&fpx,
181 &((struct _fpstate __user *)sc.fpstate)->_fxsr_env[0],
a5f6096c
JD
182 sizeof(struct user_fxsr_struct));
183 if (err)
184 return 1;
185
186 err = convert_fxsr_from_user(&fpx, sc.fpstate);
187 if (err)
188 return 1;
189
a5a678c8 190 err = restore_fpx_registers(pid, (unsigned long *) &fpx);
a5f6096c
JD
191 if (err < 0) {
192 printk(KERN_ERR "copy_sc_from_user - "
193 "restore_fpx_registers failed, errno = %d\n",
194 -err);
195 return 1;
196 }
a955bd61 197 } else {
a5f6096c
JD
198 struct user_i387_struct fp;
199
200 err = copy_from_user(&fp, sc.fpstate,
201 sizeof(struct user_i387_struct));
202 if (err)
203 return 1;
204
a5a678c8 205 err = restore_fp_registers(pid, (unsigned long *) &fp);
a5f6096c
JD
206 if (err < 0) {
207 printk(KERN_ERR "copy_sc_from_user - "
208 "restore_fp_registers failed, errno = %d\n",
209 -err);
210 return 1;
211 }
1da177e4
LT
212 }
213
5d86456d 214 return 0;
1da177e4
LT
215}
216
77bf4400
JD
217static int copy_sc_to_user(struct sigcontext __user *to,
218 struct _fpstate __user *to_fp, struct pt_regs *regs,
219 unsigned long sp)
1da177e4 220{
ba180fd4 221 struct sigcontext sc;
c578455a 222 struct faultinfo * fi = &current->thread.arch.faultinfo;
a5a678c8 223 int err, pid;
a955bd61 224 memset(&sc, 0, sizeof(struct sigcontext));
1da177e4 225
18baddda
JD
226 sc.gs = REGS_GS(regs->regs.gp);
227 sc.fs = REGS_FS(regs->regs.gp);
228 sc.es = REGS_ES(regs->regs.gp);
229 sc.ds = REGS_DS(regs->regs.gp);
7796931f
JD
230 sc.di = REGS_EDI(regs->regs.gp);
231 sc.si = REGS_ESI(regs->regs.gp);
232 sc.bp = REGS_EBP(regs->regs.gp);
233 sc.sp = sp;
234 sc.bx = REGS_EBX(regs->regs.gp);
235 sc.dx = REGS_EDX(regs->regs.gp);
236 sc.cx = REGS_ECX(regs->regs.gp);
237 sc.ax = REGS_EAX(regs->regs.gp);
238 sc.ip = REGS_IP(regs->regs.gp);
18baddda 239 sc.cs = REGS_CS(regs->regs.gp);
7796931f
JD
240 sc.flags = REGS_EFLAGS(regs->regs.gp);
241 sc.sp_at_signal = regs->regs.gp[UESP];
18baddda 242 sc.ss = regs->regs.gp[SS];
ba180fd4
JD
243 sc.cr2 = fi->cr2;
244 sc.err = fi->error_code;
245 sc.trapno = fi->trap_no;
1da177e4 246
4d338e1a 247 to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
1da177e4
LT
248 sc.fpstate = to_fp;
249
a5a678c8 250 pid = userspace_pid[current_thread_info()->cpu];
a5f6096c
JD
251 if (have_fpx_regs) {
252 struct user_fxsr_struct fpx;
253
a5a678c8 254 err = save_fpx_registers(pid, (unsigned long *) &fpx);
a5f6096c
JD
255 if (err < 0){
256 printk(KERN_ERR "copy_sc_to_user - save_fpx_registers "
257 "failed, errno = %d\n", err);
258 return 1;
259 }
260
261 err = convert_fxsr_to_user(to_fp, &fpx);
262 if (err)
263 return 1;
264
265 err |= __put_user(fpx.swd, &to_fp->status);
266 err |= __put_user(X86_FXSR_MAGIC, &to_fp->magic);
267 if (err)
268 return 1;
269
270 if (copy_to_user(&to_fp->_fxsr_env[0], &fpx,
271 sizeof(struct user_fxsr_struct)))
272 return 1;
273 }
274 else {
275 struct user_i387_struct fp;
276
a5a678c8 277 err = save_fp_registers(pid, (unsigned long *) &fp);
a5f6096c
JD
278 if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
279 return 1;
280 }
1da177e4 281
a5f6096c 282 return copy_to_user(to, &sc, sizeof(sc));
1da177e4 283}
1da177e4 284
ba180fd4
JD
285static int copy_ucontext_to_user(struct ucontext __user *uc,
286 struct _fpstate __user *fp, sigset_t *set,
287 unsigned long sp)
1da177e4
LT
288{
289 int err = 0;
290
291 err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
292 err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
293 err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
98c18238 294 err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
1da177e4 295 err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
5d86456d 296 return err;
1da177e4
LT
297}
298
299struct sigframe
300{
4d338e1a 301 char __user *pretcode;
1da177e4
LT
302 int sig;
303 struct sigcontext sc;
304 struct _fpstate fpstate;
305 unsigned long extramask[_NSIG_WORDS-1];
306 char retcode[8];
307};
308
309struct rt_sigframe
310{
4d338e1a 311 char __user *pretcode;
1da177e4 312 int sig;
4d338e1a
AV
313 struct siginfo __user *pinfo;
314 void __user *puc;
1da177e4
LT
315 struct siginfo info;
316 struct ucontext uc;
317 struct _fpstate fpstate;
318 char retcode[8];
319};
320
321int setup_signal_stack_sc(unsigned long stack_top, int sig,
322 struct k_sigaction *ka, struct pt_regs *regs,
323 sigset_t *mask)
324{
325 struct sigframe __user *frame;
4d338e1a 326 void __user *restorer;
98c18238 327 unsigned long save_sp = PT_REGS_SP(regs);
1da177e4
LT
328 int err = 0;
329
3896625d
JD
330 /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
331 stack_top = ((stack_top + 4) & -16UL) - 4;
4d338e1a 332 frame = (struct sigframe __user *) stack_top - 1;
1da177e4
LT
333 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
334 return 1;
335
4d338e1a 336 restorer = frame->retcode;
ba180fd4 337 if (ka->sa.sa_flags & SA_RESTORER)
1da177e4
LT
338 restorer = ka->sa.sa_restorer;
339
98c18238
JD
340 /* Update SP now because the page fault handler refuses to extend
341 * the stack if the faulting address is too far below the current
342 * SP, which frame now certainly is. If there's an error, the original
343 * value is restored on the way out.
344 * When writing the sigcontext to the stack, we have to write the
345 * original value, so that's passed to copy_sc_to_user, which does
346 * the right thing with it.
347 */
348 PT_REGS_SP(regs) = (unsigned long) frame;
349
1da177e4
LT
350 err |= __put_user(restorer, &frame->pretcode);
351 err |= __put_user(sig, &frame->sig);
98c18238 352 err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
1da177e4
LT
353 err |= __put_user(mask->sig[0], &frame->sc.oldmask);
354 if (_NSIG_WORDS > 1)
355 err |= __copy_to_user(&frame->extramask, &mask->sig[1],
356 sizeof(frame->extramask));
357
358 /*
359 * This is popl %eax ; movl $,%eax ; int $0x80
360 *
361 * WE DO NOT USE IT ANY MORE! It's only left here for historical
362 * reasons and because gdb uses it as a signature to notice
363 * signal handler stack frames.
364 */
365 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
366 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
367 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
368
ba180fd4 369 if (err)
98c18238 370 goto err;
1da177e4
LT
371
372 PT_REGS_SP(regs) = (unsigned long) frame;
373 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
374 PT_REGS_EAX(regs) = (unsigned long) sig;
375 PT_REGS_EDX(regs) = (unsigned long) 0;
376 PT_REGS_ECX(regs) = (unsigned long) 0;
377
378 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
379 ptrace_notify(SIGTRAP);
98c18238
JD
380 return 0;
381
382err:
383 PT_REGS_SP(regs) = save_sp;
384 return err;
1da177e4
LT
385}
386
387int setup_signal_stack_si(unsigned long stack_top, int sig,
388 struct k_sigaction *ka, struct pt_regs *regs,
389 siginfo_t *info, sigset_t *mask)
390{
391 struct rt_sigframe __user *frame;
4d338e1a 392 void __user *restorer;
98c18238 393 unsigned long save_sp = PT_REGS_SP(regs);
1da177e4
LT
394 int err = 0;
395
396 stack_top &= -8UL;
4d338e1a 397 frame = (struct rt_sigframe __user *) stack_top - 1;
1da177e4
LT
398 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
399 return 1;
400
4d338e1a 401 restorer = frame->retcode;
ba180fd4 402 if (ka->sa.sa_flags & SA_RESTORER)
1da177e4
LT
403 restorer = ka->sa.sa_restorer;
404
98c18238
JD
405 /* See comment above about why this is here */
406 PT_REGS_SP(regs) = (unsigned long) frame;
407
1da177e4
LT
408 err |= __put_user(restorer, &frame->pretcode);
409 err |= __put_user(sig, &frame->sig);
410 err |= __put_user(&frame->info, &frame->pinfo);
411 err |= __put_user(&frame->uc, &frame->puc);
412 err |= copy_siginfo_to_user(&frame->info, info);
413 err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
98c18238 414 save_sp);
1da177e4
LT
415
416 /*
417 * This is movl $,%eax ; int $0x80
418 *
419 * WE DO NOT USE IT ANY MORE! It's only left here for historical
420 * reasons and because gdb uses it as a signature to notice
421 * signal handler stack frames.
422 */
423 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
424 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
425 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
426
ba180fd4 427 if (err)
98c18238 428 goto err;
1da177e4 429
1da177e4
LT
430 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
431 PT_REGS_EAX(regs) = (unsigned long) sig;
432 PT_REGS_EDX(regs) = (unsigned long) &frame->info;
433 PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
434
435 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
436 ptrace_notify(SIGTRAP);
98c18238
JD
437 return 0;
438
439err:
440 PT_REGS_SP(regs) = save_sp;
441 return err;
1da177e4
LT
442}
443
444long sys_sigreturn(struct pt_regs regs)
445{
446 unsigned long sp = PT_REGS_SP(&current->thread.regs);
4d338e1a 447 struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
1da177e4
LT
448 sigset_t set;
449 struct sigcontext __user *sc = &frame->sc;
450 unsigned long __user *oldmask = &sc->oldmask;
451 unsigned long __user *extramask = frame->extramask;
452 int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
453
ba180fd4
JD
454 if (copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
455 copy_from_user(&set.sig[1], extramask, sig_size))
1da177e4
LT
456 goto segfault;
457
458 sigdelsetmask(&set, ~_BLOCKABLE);
78722a44 459 set_current_blocked(&set);
1da177e4 460
ba180fd4 461 if (copy_sc_from_user(&current->thread.regs, sc))
1da177e4
LT
462 goto segfault;
463
464 /* Avoid ERESTART handling */
465 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
5d86456d 466 return PT_REGS_SYSCALL_RET(&current->thread.regs);
1da177e4
LT
467
468 segfault:
469 force_sig(SIGSEGV, current);
470 return 0;
471}
472
473long sys_rt_sigreturn(struct pt_regs regs)
474{
4d338e1a 475 unsigned long sp = PT_REGS_SP(&current->thread.regs);
ba180fd4
JD
476 struct rt_sigframe __user *frame =
477 (struct rt_sigframe __user *) (sp - 4);
1da177e4
LT
478 sigset_t set;
479 struct ucontext __user *uc = &frame->uc;
480 int sig_size = _NSIG_WORDS * sizeof(unsigned long);
481
ba180fd4 482 if (copy_from_user(&set, &uc->uc_sigmask, sig_size))
1da177e4
LT
483 goto segfault;
484
485 sigdelsetmask(&set, ~_BLOCKABLE);
78722a44 486 set_current_blocked(&set);
1da177e4 487
ba180fd4 488 if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
1da177e4
LT
489 goto segfault;
490
491 /* Avoid ERESTART handling */
492 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
5d86456d 493 return PT_REGS_SYSCALL_RET(&current->thread.regs);
1da177e4
LT
494
495 segfault:
496 force_sig(SIGSEGV, current);
497 return 0;
498}