2 * Copyright (C) 2003 PathScale, Inc.
3 * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
8 #include <linux/personality.h>
9 #include <linux/ptrace.h>
10 #include <linux/kernel.h>
11 #include <asm/unistd.h>
12 #include <asm/uaccess.h>
13 #include <asm/ucontext.h>
14 #include <frame_kern.h>
20 * FPU tag word conversions.
23 static inline unsigned short twd_i387_to_fxsr(unsigned short twd
)
25 unsigned int tmp
; /* to avoid 16 bit prefixes in the code */
27 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
29 tmp
= (tmp
| (tmp
>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
30 /* and move the valid bits to the lower byte. */
31 tmp
= (tmp
| (tmp
>> 1)) & 0x3333; /* 00VV00VV00VV00VV */
32 tmp
= (tmp
| (tmp
>> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
33 tmp
= (tmp
| (tmp
>> 4)) & 0x00ff; /* 00000000VVVVVVVV */
37 static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct
*fxsave
)
39 struct _fpxreg
*st
= NULL
;
40 unsigned long twd
= (unsigned long) fxsave
->twd
;
42 unsigned long ret
= 0xffff0000;
45 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16)
47 for (i
= 0; i
< 8; i
++) {
49 st
= (struct _fpxreg
*) FPREG_ADDR(fxsave
, i
);
51 switch (st
->exponent
& 0x7fff) {
53 tag
= 2; /* Special */
56 if ( !st
->significand
[0] &&
57 !st
->significand
[1] &&
58 !st
->significand
[2] &&
59 !st
->significand
[3] ) {
62 tag
= 2; /* Special */
66 if (st
->significand
[3] & 0x8000) {
69 tag
= 2; /* Special */
76 ret
|= (tag
<< (2 * i
));
82 static int convert_fxsr_to_user(struct _fpstate __user
*buf
,
83 struct user_fxsr_struct
*fxsave
)
86 struct _fpreg __user
*to
;
90 env
[0] = (unsigned long)fxsave
->cwd
| 0xffff0000ul
;
91 env
[1] = (unsigned long)fxsave
->swd
| 0xffff0000ul
;
92 env
[2] = twd_fxsr_to_i387(fxsave
);
94 env
[4] = fxsave
->fcs
| ((unsigned long)fxsave
->fop
<< 16);
98 if (__copy_to_user(buf
, env
, 7 * sizeof(unsigned long)))
102 from
= (struct _fpxreg
*) &fxsave
->st_space
[0];
103 for (i
= 0; i
< 8; i
++, to
++, from
++) {
104 unsigned long __user
*t
= (unsigned long __user
*)to
;
105 unsigned long *f
= (unsigned long *)from
;
107 if (__put_user(*f
, t
) ||
108 __put_user(*(f
+ 1), t
+ 1) ||
109 __put_user(from
->exponent
, &to
->exponent
))
115 static int convert_fxsr_from_user(struct user_fxsr_struct
*fxsave
,
116 struct _fpstate __user
*buf
)
118 unsigned long env
[7];
120 struct _fpreg __user
*from
;
123 if (copy_from_user( env
, buf
, 7 * sizeof(long)))
126 fxsave
->cwd
= (unsigned short)(env
[0] & 0xffff);
127 fxsave
->swd
= (unsigned short)(env
[1] & 0xffff);
128 fxsave
->twd
= twd_i387_to_fxsr((unsigned short)(env
[2] & 0xffff));
129 fxsave
->fip
= env
[3];
130 fxsave
->fop
= (unsigned short)((env
[4] & 0xffff0000ul
) >> 16);
131 fxsave
->fcs
= (env
[4] & 0xffff);
132 fxsave
->foo
= env
[5];
133 fxsave
->fos
= env
[6];
135 to
= (struct _fpxreg
*) &fxsave
->st_space
[0];
137 for (i
= 0; i
< 8; i
++, to
++, from
++) {
138 unsigned long *t
= (unsigned long *)to
;
139 unsigned long __user
*f
= (unsigned long __user
*)from
;
141 if (__get_user(*t
, f
) ||
142 __get_user(*(t
+ 1), f
+ 1) ||
143 __get_user(to
->exponent
, &from
->exponent
))
149 extern int have_fpx_regs
;
153 static int copy_sc_from_user(struct pt_regs
*regs
,
154 struct sigcontext __user
*from
)
156 struct sigcontext sc
;
159 /* Always make any pending restarted system calls return -EINTR */
160 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
162 err
= copy_from_user(&sc
, from
, sizeof(sc
));
166 #define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
196 GETREG(EFLAGS
, flags
);
203 pid
= userspace_pid
[current_thread_info()->cpu
];
206 struct user_fxsr_struct fpx
;
208 err
= copy_from_user(&fpx
,
209 &((struct _fpstate __user
*)sc
.fpstate
)->_fxsr_env
[0],
210 sizeof(struct user_fxsr_struct
));
214 err
= convert_fxsr_from_user(&fpx
, sc
.fpstate
);
218 err
= restore_fpx_registers(pid
, (unsigned long *) &fpx
);
220 printk(KERN_ERR
"copy_sc_from_user - "
221 "restore_fpx_registers failed, errno = %d\n",
228 struct user_i387_struct fp
;
230 err
= copy_from_user(&fp
, sc
.fpstate
,
231 sizeof(struct user_i387_struct
));
235 err
= restore_fp_registers(pid
, (unsigned long *) &fp
);
237 printk(KERN_ERR
"copy_sc_from_user - "
238 "restore_fp_registers failed, errno = %d\n",
246 static int copy_sc_to_user(struct sigcontext __user
*to
,
247 struct _fpstate __user
*to_fp
, struct pt_regs
*regs
,
250 struct sigcontext sc
;
251 struct faultinfo
* fi
= ¤t
->thread
.arch
.faultinfo
;
253 memset(&sc
, 0, sizeof(struct sigcontext
));
255 #define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno]
283 sc
.err
= fi
->error_code
;
284 sc
.trapno
= fi
->trap_no
;
287 PUTREG(EFLAGS
, flags
);
289 PUTREG(SP
, sp_at_signal
);
296 err
= copy_to_user(to
, &sc
, sizeof(struct sigcontext
));
300 pid
= userspace_pid
[current_thread_info()->cpu
];
304 struct user_fxsr_struct fpx
;
306 err
= save_fpx_registers(pid
, (unsigned long *) &fpx
);
308 printk(KERN_ERR
"copy_sc_to_user - save_fpx_registers "
309 "failed, errno = %d\n", err
);
313 err
= convert_fxsr_to_user(to_fp
, &fpx
);
317 err
|= __put_user(fpx
.swd
, &to_fp
->status
);
318 err
|= __put_user(X86_FXSR_MAGIC
, &to_fp
->magic
);
322 if (copy_to_user(&to_fp
->_fxsr_env
[0], &fpx
,
323 sizeof(struct user_fxsr_struct
)))
328 struct user_i387_struct fp
;
330 err
= save_fp_registers(pid
, (unsigned long *) &fp
);
331 if (copy_to_user(to_fp
, &fp
, sizeof(struct user_i387_struct
)))
339 static int copy_ucontext_to_user(struct ucontext __user
*uc
,
340 struct _fpstate __user
*fp
, sigset_t
*set
,
345 err
|= __save_altstack(&uc
->uc_stack
, sp
);
346 err
|= copy_sc_to_user(&uc
->uc_mcontext
, fp
, ¤t
->thread
.regs
, 0);
347 err
|= copy_to_user(&uc
->uc_sigmask
, set
, sizeof(*set
));
353 char __user
*pretcode
;
355 struct sigcontext sc
;
356 struct _fpstate fpstate
;
357 unsigned long extramask
[_NSIG_WORDS
-1];
363 char __user
*pretcode
;
365 struct siginfo __user
*pinfo
;
369 struct _fpstate fpstate
;
373 int setup_signal_stack_sc(unsigned long stack_top
, int sig
,
374 struct k_sigaction
*ka
, struct pt_regs
*regs
,
377 struct sigframe __user
*frame
;
378 void __user
*restorer
;
381 /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
382 stack_top
= ((stack_top
+ 4) & -16UL) - 4;
383 frame
= (struct sigframe __user
*) stack_top
- 1;
384 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
387 restorer
= frame
->retcode
;
388 if (ka
->sa
.sa_flags
& SA_RESTORER
)
389 restorer
= ka
->sa
.sa_restorer
;
391 err
|= __put_user(restorer
, &frame
->pretcode
);
392 err
|= __put_user(sig
, &frame
->sig
);
393 err
|= copy_sc_to_user(&frame
->sc
, &frame
->fpstate
, regs
, mask
->sig
[0]);
395 err
|= __copy_to_user(&frame
->extramask
, &mask
->sig
[1],
396 sizeof(frame
->extramask
));
399 * This is popl %eax ; movl $,%eax ; int $0x80
401 * WE DO NOT USE IT ANY MORE! It's only left here for historical
402 * reasons and because gdb uses it as a signature to notice
403 * signal handler stack frames.
405 err
|= __put_user(0xb858, (short __user
*)(frame
->retcode
+0));
406 err
|= __put_user(__NR_sigreturn
, (int __user
*)(frame
->retcode
+2));
407 err
|= __put_user(0x80cd, (short __user
*)(frame
->retcode
+6));
412 PT_REGS_SP(regs
) = (unsigned long) frame
;
413 PT_REGS_IP(regs
) = (unsigned long) ka
->sa
.sa_handler
;
414 PT_REGS_AX(regs
) = (unsigned long) sig
;
415 PT_REGS_DX(regs
) = (unsigned long) 0;
416 PT_REGS_CX(regs
) = (unsigned long) 0;
420 int setup_signal_stack_si(unsigned long stack_top
, int sig
,
421 struct k_sigaction
*ka
, struct pt_regs
*regs
,
422 siginfo_t
*info
, sigset_t
*mask
)
424 struct rt_sigframe __user
*frame
;
425 void __user
*restorer
;
429 frame
= (struct rt_sigframe __user
*) stack_top
- 1;
430 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
433 restorer
= frame
->retcode
;
434 if (ka
->sa
.sa_flags
& SA_RESTORER
)
435 restorer
= ka
->sa
.sa_restorer
;
437 err
|= __put_user(restorer
, &frame
->pretcode
);
438 err
|= __put_user(sig
, &frame
->sig
);
439 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
440 err
|= __put_user(&frame
->uc
, &frame
->puc
);
441 err
|= copy_siginfo_to_user(&frame
->info
, info
);
442 err
|= copy_ucontext_to_user(&frame
->uc
, &frame
->fpstate
, mask
,
446 * This is movl $,%eax ; int $0x80
448 * WE DO NOT USE IT ANY MORE! It's only left here for historical
449 * reasons and because gdb uses it as a signature to notice
450 * signal handler stack frames.
452 err
|= __put_user(0xb8, (char __user
*)(frame
->retcode
+0));
453 err
|= __put_user(__NR_rt_sigreturn
, (int __user
*)(frame
->retcode
+1));
454 err
|= __put_user(0x80cd, (short __user
*)(frame
->retcode
+5));
459 PT_REGS_SP(regs
) = (unsigned long) frame
;
460 PT_REGS_IP(regs
) = (unsigned long) ka
->sa
.sa_handler
;
461 PT_REGS_AX(regs
) = (unsigned long) sig
;
462 PT_REGS_DX(regs
) = (unsigned long) &frame
->info
;
463 PT_REGS_CX(regs
) = (unsigned long) &frame
->uc
;
467 long sys_sigreturn(struct pt_regs
*regs
)
469 unsigned long sp
= PT_REGS_SP(¤t
->thread
.regs
);
470 struct sigframe __user
*frame
= (struct sigframe __user
*)(sp
- 8);
472 struct sigcontext __user
*sc
= &frame
->sc
;
473 unsigned long __user
*oldmask
= &sc
->oldmask
;
474 unsigned long __user
*extramask
= frame
->extramask
;
475 int sig_size
= (_NSIG_WORDS
- 1) * sizeof(unsigned long);
477 if (copy_from_user(&set
.sig
[0], oldmask
, sizeof(set
.sig
[0])) ||
478 copy_from_user(&set
.sig
[1], extramask
, sig_size
))
481 set_current_blocked(&set
);
483 if (copy_sc_from_user(¤t
->thread
.regs
, sc
))
486 /* Avoid ERESTART handling */
487 PT_REGS_SYSCALL_NR(¤t
->thread
.regs
) = -1;
488 return PT_REGS_SYSCALL_RET(¤t
->thread
.regs
);
491 force_sig(SIGSEGV
, current
);
499 char __user
*pretcode
;
502 struct _fpstate fpstate
;
505 int setup_signal_stack_si(unsigned long stack_top
, int sig
,
506 struct k_sigaction
*ka
, struct pt_regs
* regs
,
507 siginfo_t
*info
, sigset_t
*set
)
509 struct rt_sigframe __user
*frame
;
511 struct task_struct
*me
= current
;
513 frame
= (struct rt_sigframe __user
*)
514 round_down(stack_top
- sizeof(struct rt_sigframe
), 16);
515 /* Subtract 128 for a red zone and 8 for proper alignment */
516 frame
= (struct rt_sigframe __user
*) ((unsigned long) frame
- 128 - 8);
518 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
521 if (ka
->sa
.sa_flags
& SA_SIGINFO
) {
522 err
|= copy_siginfo_to_user(&frame
->info
, info
);
527 /* Create the ucontext. */
528 err
|= __put_user(0, &frame
->uc
.uc_flags
);
529 err
|= __put_user(0, &frame
->uc
.uc_link
);
530 err
|= __save_altstack(&frame
->uc
.uc_stack
, PT_REGS_SP(regs
));
531 err
|= copy_sc_to_user(&frame
->uc
.uc_mcontext
, &frame
->fpstate
, regs
,
533 err
|= __put_user(&frame
->fpstate
, &frame
->uc
.uc_mcontext
.fpstate
);
534 if (sizeof(*set
) == 16) {
535 err
|= __put_user(set
->sig
[0], &frame
->uc
.uc_sigmask
.sig
[0]);
536 err
|= __put_user(set
->sig
[1], &frame
->uc
.uc_sigmask
.sig
[1]);
539 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
,
543 * Set up to return from userspace. If provided, use a stub
544 * already in userspace.
546 /* x86-64 should always use SA_RESTORER. */
547 if (ka
->sa
.sa_flags
& SA_RESTORER
)
548 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
550 /* could use a vstub here */
556 /* Set up registers for signal handler */
558 struct exec_domain
*ed
= current_thread_info()->exec_domain
;
559 if (unlikely(ed
&& ed
->signal_invmap
&& sig
< 32))
560 sig
= ed
->signal_invmap
[sig
];
563 PT_REGS_SP(regs
) = (unsigned long) frame
;
564 PT_REGS_DI(regs
) = sig
;
565 /* In case the signal handler was declared without prototypes */
566 PT_REGS_AX(regs
) = 0;
569 * This also works for non SA_SIGINFO handlers because they expect the
570 * next argument after the signal number on the stack.
572 PT_REGS_SI(regs
) = (unsigned long) &frame
->info
;
573 PT_REGS_DX(regs
) = (unsigned long) &frame
->uc
;
574 PT_REGS_IP(regs
) = (unsigned long) ka
->sa
.sa_handler
;
580 long sys_rt_sigreturn(struct pt_regs
*regs
)
582 unsigned long sp
= PT_REGS_SP(¤t
->thread
.regs
);
583 struct rt_sigframe __user
*frame
=
584 (struct rt_sigframe __user
*)(sp
- sizeof(long));
585 struct ucontext __user
*uc
= &frame
->uc
;
588 if (copy_from_user(&set
, &uc
->uc_sigmask
, sizeof(set
)))
591 set_current_blocked(&set
);
593 if (copy_sc_from_user(¤t
->thread
.regs
, &uc
->uc_mcontext
))
596 /* Avoid ERESTART handling */
597 PT_REGS_SYSCALL_NR(¤t
->thread
.regs
) = -1;
598 return PT_REGS_SYSCALL_RET(¤t
->thread
.regs
);
601 force_sig(SIGSEGV
, current
);
606 long ptregs_sigreturn(void)
608 return sys_sigreturn(NULL
);
610 long ptregs_rt_sigreturn(void)
612 return sys_rt_sigreturn(NULL
);