2 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include "linux/ptrace.h"
7 #include "asm/unistd.h"
8 #include "asm/uaccess.h"
9 #include "asm/ucontext.h"
10 #include "frame_kern.h"
13 void copy_sc(struct uml_pt_regs *regs, void *from)
15 struct sigcontext *sc = from;
17 REGS_GS(regs->gp) = sc->gs;
18 REGS_FS(regs->gp) = sc->fs;
19 REGS_ES(regs->gp) = sc->es;
20 REGS_DS(regs->gp) = sc->ds;
21 REGS_EDI(regs->gp) = sc->edi;
22 REGS_ESI(regs->gp) = sc->esi;
23 REGS_EBP(regs->gp) = sc->ebp;
24 REGS_SP(regs->gp) = sc->esp;
25 REGS_EBX(regs->gp) = sc->ebx;
26 REGS_EDX(regs->gp) = sc->edx;
27 REGS_ECX(regs->gp) = sc->ecx;
28 REGS_EAX(regs->gp) = sc->eax;
29 REGS_IP(regs->gp) = sc->eip;
30 REGS_CS(regs->gp) = sc->cs;
31 REGS_EFLAGS(regs->gp) = sc->eflags;
32 REGS_SS(regs->gp) = sc->ss;
36 * FPU tag word conversions.
39 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
41 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
43 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
45 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
46 /* and move the valid bits to the lower byte. */
47 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
48 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
49 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
53 static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct *fxsave)
55 struct _fpxreg *st = NULL;
56 unsigned long twd = (unsigned long) fxsave->twd;
58 unsigned long ret = 0xffff0000;
61 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
63 for (i = 0; i < 8; i++) {
65 st = (struct _fpxreg *) FPREG_ADDR(fxsave, i);
67 switch (st->exponent & 0x7fff) {
69 tag = 2; /* Special */
72 if ( !st->significand[0] &&
73 !st->significand[1] &&
74 !st->significand[2] &&
75 !st->significand[3] ) {
78 tag = 2; /* Special */
82 if (st->significand[3] & 0x8000) {
85 tag = 2; /* Special */
92 ret |= (tag << (2 * i));
98 static int convert_fxsr_to_user(struct _fpstate __user *buf,
99 struct user_fxsr_struct *fxsave)
101 unsigned long env[7];
102 struct _fpreg __user *to;
103 struct _fpxreg *from;
106 env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
107 env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
108 env[2] = twd_fxsr_to_i387(fxsave);
109 env[3] = fxsave->fip;
110 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
111 env[5] = fxsave->foo;
112 env[6] = fxsave->fos;
114 if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
118 from = (struct _fpxreg *) &fxsave->st_space[0];
119 for (i = 0; i < 8; i++, to++, from++) {
120 unsigned long __user *t = (unsigned long __user *)to;
121 unsigned long *f = (unsigned long *)from;
123 if (__put_user(*f, t) ||
124 __put_user(*(f + 1), t + 1) ||
125 __put_user(from->exponent, &to->exponent))
131 static int convert_fxsr_from_user(struct user_fxsr_struct *fxsave,
132 struct _fpstate __user *buf)
134 unsigned long env[7];
136 struct _fpreg __user *from;
139 if (copy_from_user( env, buf, 7 * sizeof(long)))
142 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
143 fxsave->swd = (unsigned short)(env[1] & 0xffff);
144 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
145 fxsave->fip = env[3];
146 fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
147 fxsave->fcs = (env[4] & 0xffff);
148 fxsave->foo = env[5];
149 fxsave->fos = env[6];
151 to = (struct _fpxreg *) &fxsave->st_space[0];
153 for (i = 0; i < 8; i++, to++, from++) {
154 unsigned long *t = (unsigned long *)to;
155 unsigned long __user *f = (unsigned long __user *)from;
157 if (__get_user(*t, f) ||
158 __get_user(*(t + 1), f + 1) ||
159 __get_user(to->exponent, &from->exponent))
165 extern int have_fpx_regs;
167 static int copy_sc_from_user(struct pt_regs *regs,
168 struct sigcontext __user *from)
170 struct sigcontext sc;
173 err = copy_from_user(&sc, from, sizeof(sc));
177 copy_sc(®s->regs, &sc);
179 struct user_fxsr_struct fpx;
181 err = copy_from_user(&fpx, &sc.fpstate->_fxsr_env[0],
182 sizeof(struct user_fxsr_struct));
186 err = convert_fxsr_from_user(&fpx, sc.fpstate);
190 err = restore_fpx_registers(userspace_pid[current_thread->cpu],
191 (unsigned long *) &fpx);
193 printk(KERN_ERR "copy_sc_from_user - "
194 "restore_fpx_registers failed, errno = %d\n",
200 struct user_i387_struct fp;
202 err = copy_from_user(&fp, sc.fpstate,
203 sizeof(struct user_i387_struct));
207 err = restore_fp_registers(userspace_pid[current_thread->cpu],
208 (unsigned long *) &fp);
210 printk(KERN_ERR "copy_sc_from_user - "
211 "restore_fp_registers failed, errno = %d\n",
220 static int copy_sc_to_user(struct sigcontext __user *to,
221 struct _fpstate __user *to_fp, struct pt_regs *regs,
224 struct sigcontext sc;
225 struct faultinfo * fi = ¤t->thread.arch.faultinfo;
228 sc.gs = REGS_GS(regs->regs.gp);
229 sc.fs = REGS_FS(regs->regs.gp);
230 sc.es = REGS_ES(regs->regs.gp);
231 sc.ds = REGS_DS(regs->regs.gp);
232 sc.edi = REGS_EDI(regs->regs.gp);
233 sc.esi = REGS_ESI(regs->regs.gp);
234 sc.ebp = REGS_EBP(regs->regs.gp);
236 sc.ebx = REGS_EBX(regs->regs.gp);
237 sc.edx = REGS_EDX(regs->regs.gp);
238 sc.ecx = REGS_ECX(regs->regs.gp);
239 sc.eax = REGS_EAX(regs->regs.gp);
240 sc.eip = REGS_IP(regs->regs.gp);
241 sc.cs = REGS_CS(regs->regs.gp);
242 sc.eflags = REGS_EFLAGS(regs->regs.gp);
243 sc.esp_at_signal = regs->regs.gp[UESP];
244 sc.ss = regs->regs.gp[SS];
246 sc.err = fi->error_code;
247 sc.trapno = fi->trap_no;
249 to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
253 struct user_fxsr_struct fpx;
255 err = save_fpx_registers(userspace_pid[current_thread->cpu],
256 (unsigned long *) &fpx);
258 printk(KERN_ERR "copy_sc_to_user - save_fpx_registers "
259 "failed, errno = %d\n", err);
263 err = convert_fxsr_to_user(to_fp, &fpx);
267 err |= __put_user(fpx.swd, &to_fp->status);
268 err |= __put_user(X86_FXSR_MAGIC, &to_fp->magic);
272 if (copy_to_user(&to_fp->_fxsr_env[0], &fpx,
273 sizeof(struct user_fxsr_struct)))
277 struct user_i387_struct fp;
279 err = save_fp_registers(userspace_pid[current_thread->cpu],
280 (unsigned long *) &fp);
281 if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
285 return copy_to_user(to, &sc, sizeof(sc));
288 static int copy_ucontext_to_user(struct ucontext __user *uc,
289 struct _fpstate __user *fp, sigset_t *set,
294 err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
295 err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
296 err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
297 err |= copy_sc_to_user(&uc->uc_mcontext, fp, ¤t->thread.regs, sp);
298 err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
304 char __user *pretcode;
306 struct sigcontext sc;
307 struct _fpstate fpstate;
308 unsigned long extramask[_NSIG_WORDS-1];
314 char __user *pretcode;
316 struct siginfo __user *pinfo;
320 struct _fpstate fpstate;
324 int setup_signal_stack_sc(unsigned long stack_top, int sig,
325 struct k_sigaction *ka, struct pt_regs *regs,
328 struct sigframe __user *frame;
329 void __user *restorer;
330 unsigned long save_sp = PT_REGS_SP(regs);
333 /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
334 stack_top = ((stack_top + 4) & -16UL) - 4;
335 frame = (struct sigframe __user *) stack_top - 1;
336 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
339 restorer = frame->retcode;
340 if (ka->sa.sa_flags & SA_RESTORER)
341 restorer = ka->sa.sa_restorer;
343 /* Update SP now because the page fault handler refuses to extend
344 * the stack if the faulting address is too far below the current
345 * SP, which frame now certainly is. If there's an error, the original
346 * value is restored on the way out.
347 * When writing the sigcontext to the stack, we have to write the
348 * original value, so that's passed to copy_sc_to_user, which does
349 * the right thing with it.
351 PT_REGS_SP(regs) = (unsigned long) frame;
353 err |= __put_user(restorer, &frame->pretcode);
354 err |= __put_user(sig, &frame->sig);
355 err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
356 err |= __put_user(mask->sig[0], &frame->sc.oldmask);
358 err |= __copy_to_user(&frame->extramask, &mask->sig[1],
359 sizeof(frame->extramask));
362 * This is popl %eax ; movl $,%eax ; int $0x80
364 * WE DO NOT USE IT ANY MORE! It's only left here for historical
365 * reasons and because gdb uses it as a signature to notice
366 * signal handler stack frames.
368 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
369 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
370 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
375 PT_REGS_SP(regs) = (unsigned long) frame;
376 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
377 PT_REGS_EAX(regs) = (unsigned long) sig;
378 PT_REGS_EDX(regs) = (unsigned long) 0;
379 PT_REGS_ECX(regs) = (unsigned long) 0;
381 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
382 ptrace_notify(SIGTRAP);
386 PT_REGS_SP(regs) = save_sp;
390 int setup_signal_stack_si(unsigned long stack_top, int sig,
391 struct k_sigaction *ka, struct pt_regs *regs,
392 siginfo_t *info, sigset_t *mask)
394 struct rt_sigframe __user *frame;
395 void __user *restorer;
396 unsigned long save_sp = PT_REGS_SP(regs);
400 frame = (struct rt_sigframe __user *) stack_top - 1;
401 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
404 restorer = frame->retcode;
405 if (ka->sa.sa_flags & SA_RESTORER)
406 restorer = ka->sa.sa_restorer;
408 /* See comment above about why this is here */
409 PT_REGS_SP(regs) = (unsigned long) frame;
411 err |= __put_user(restorer, &frame->pretcode);
412 err |= __put_user(sig, &frame->sig);
413 err |= __put_user(&frame->info, &frame->pinfo);
414 err |= __put_user(&frame->uc, &frame->puc);
415 err |= copy_siginfo_to_user(&frame->info, info);
416 err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
420 * This is movl $,%eax ; int $0x80
422 * WE DO NOT USE IT ANY MORE! It's only left here for historical
423 * reasons and because gdb uses it as a signature to notice
424 * signal handler stack frames.
426 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
427 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
428 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
433 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
434 PT_REGS_EAX(regs) = (unsigned long) sig;
435 PT_REGS_EDX(regs) = (unsigned long) &frame->info;
436 PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
438 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
439 ptrace_notify(SIGTRAP);
443 PT_REGS_SP(regs) = save_sp;
447 long sys_sigreturn(struct pt_regs regs)
449 unsigned long sp = PT_REGS_SP(¤t->thread.regs);
450 struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
452 struct sigcontext __user *sc = &frame->sc;
453 unsigned long __user *oldmask = &sc->oldmask;
454 unsigned long __user *extramask = frame->extramask;
455 int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
457 if (copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
458 copy_from_user(&set.sig[1], extramask, sig_size))
461 sigdelsetmask(&set, ~_BLOCKABLE);
463 spin_lock_irq(¤t->sighand->siglock);
464 current->blocked = set;
466 spin_unlock_irq(¤t->sighand->siglock);
468 if (copy_sc_from_user(¤t->thread.regs, sc))
471 /* Avoid ERESTART handling */
472 PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1;
473 return PT_REGS_SYSCALL_RET(¤t->thread.regs);
476 force_sig(SIGSEGV, current);
480 long sys_rt_sigreturn(struct pt_regs regs)
482 unsigned long sp = PT_REGS_SP(¤t->thread.regs);
483 struct rt_sigframe __user *frame =
484 (struct rt_sigframe __user *) (sp - 4);
486 struct ucontext __user *uc = &frame->uc;
487 int sig_size = _NSIG_WORDS * sizeof(unsigned long);
489 if (copy_from_user(&set, &uc->uc_sigmask, sig_size))
492 sigdelsetmask(&set, ~_BLOCKABLE);
494 spin_lock_irq(¤t->sighand->siglock);
495 current->blocked = set;
497 spin_unlock_irq(¤t->sighand->siglock);
499 if (copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext))
502 /* Avoid ERESTART handling */
503 PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1;
504 return PT_REGS_SYSCALL_RET(¤t->thread.regs);
507 force_sig(SIGSEGV, current);