2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003 PathScale, Inc.
|
2007-10-16 02:27:18 -06:00
|
|
|
* Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-16 16:20:36 -06:00
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
2008-01-30 05:31:08 -07:00
|
|
|
#include <linux/personality.h>
|
|
|
|
#include <linux/ptrace.h>
|
2010-04-20 09:47:24 -06:00
|
|
|
#include <linux/kernel.h>
|
2008-01-30 05:31:08 -07:00
|
|
|
#include <asm/unistd.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <asm/ucontext.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include "frame_kern.h"
|
|
|
|
#include "skas.h"
|
|
|
|
|
2007-10-16 02:26:58 -06:00
|
|
|
static int copy_sc_from_user(struct pt_regs *regs,
|
2011-08-18 13:00:29 -06:00
|
|
|
struct sigcontext __user *from)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2011-08-18 13:00:49 -06:00
|
|
|
struct sigcontext sc;
|
2007-10-16 02:27:15 -06:00
|
|
|
struct user_i387_struct fp;
|
2011-08-18 13:00:29 -06:00
|
|
|
void __user *buf;
|
2011-08-18 13:00:49 -06:00
|
|
|
int err;
|
2007-10-16 02:27:00 -06:00
|
|
|
|
2011-08-18 13:00:49 -06:00
|
|
|
err = copy_from_user(&sc, from, sizeof(sc));
|
2007-10-16 02:27:15 -06:00
|
|
|
if (err)
|
2011-08-18 13:00:49 -06:00
|
|
|
return err;
|
|
|
|
|
|
|
|
#define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
|
|
|
|
|
|
|
|
GETREG(R8, r8);
|
|
|
|
GETREG(R9, r9);
|
|
|
|
GETREG(R10, r10);
|
|
|
|
GETREG(R11, r11);
|
|
|
|
GETREG(R12, r12);
|
|
|
|
GETREG(R13, r13);
|
|
|
|
GETREG(R14, r14);
|
|
|
|
GETREG(R15, r15);
|
|
|
|
GETREG(RDI, di);
|
|
|
|
GETREG(RSI, si);
|
|
|
|
GETREG(RBP, bp);
|
|
|
|
GETREG(RBX, bx);
|
|
|
|
GETREG(RDX, dx);
|
|
|
|
GETREG(RAX, ax);
|
|
|
|
GETREG(RCX, cx);
|
|
|
|
GETREG(SP, sp);
|
|
|
|
GETREG(IP, ip);
|
|
|
|
GETREG(EFLAGS, flags);
|
|
|
|
GETREG(CS, cs);
|
2005-04-16 16:20:36 -06:00
|
|
|
#undef GETREG
|
|
|
|
|
2011-08-18 13:00:49 -06:00
|
|
|
buf = sc.fpstate;
|
2011-08-18 13:00:29 -06:00
|
|
|
|
|
|
|
err = copy_from_user(&fp, buf, sizeof(struct user_i387_struct));
|
2007-10-16 02:27:15 -06:00
|
|
|
if (err)
|
|
|
|
return 1;
|
|
|
|
|
2008-02-04 23:30:54 -07:00
|
|
|
err = restore_fp_registers(userspace_pid[current_thread_info()->cpu],
|
2007-10-16 02:27:15 -06:00
|
|
|
(unsigned long *) &fp);
|
|
|
|
if (err < 0) {
|
|
|
|
printk(KERN_ERR "copy_sc_from_user - "
|
|
|
|
"restore_fp_registers failed, errno = %d\n",
|
|
|
|
-err);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:26:58 -06:00
|
|
|
static int copy_sc_to_user(struct sigcontext __user *to,
|
|
|
|
struct _fpstate __user *to_fp, struct pt_regs *regs,
|
|
|
|
unsigned long mask, unsigned long sp)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-10-16 02:27:00 -06:00
|
|
|
struct faultinfo * fi = ¤t->thread.arch.faultinfo;
|
2011-08-18 13:00:49 -06:00
|
|
|
struct sigcontext sc;
|
2007-10-16 02:27:15 -06:00
|
|
|
struct user_i387_struct fp;
|
2005-04-16 16:20:36 -06:00
|
|
|
int err = 0;
|
2011-08-18 13:00:49 -06:00
|
|
|
memset(&sc, 0, sizeof(struct sigcontext));
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2011-08-18 13:00:49 -06:00
|
|
|
#define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno]
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2011-08-18 13:00:49 -06:00
|
|
|
PUTREG(RDI, di);
|
|
|
|
PUTREG(RSI, si);
|
|
|
|
PUTREG(RBP, bp);
|
2007-10-16 02:27:18 -06:00
|
|
|
/*
|
2008-02-03 08:05:25 -07:00
|
|
|
* Must use original RSP, which is passed in, rather than what's in
|
2007-10-16 02:27:00 -06:00
|
|
|
* signal frame.
|
|
|
|
*/
|
2011-08-18 13:00:49 -06:00
|
|
|
sc.sp = sp;
|
|
|
|
PUTREG(RBX, bx);
|
|
|
|
PUTREG(RDX, dx);
|
|
|
|
PUTREG(RCX, cx);
|
|
|
|
PUTREG(RAX, ax);
|
|
|
|
PUTREG(R8, r8);
|
|
|
|
PUTREG(R9, r9);
|
|
|
|
PUTREG(R10, r10);
|
|
|
|
PUTREG(R11, r11);
|
|
|
|
PUTREG(R12, r12);
|
|
|
|
PUTREG(R13, r13);
|
|
|
|
PUTREG(R14, r14);
|
|
|
|
PUTREG(R15, r15);
|
|
|
|
PUTREG(CS, cs); /* XXX x86_64 doesn't do this */
|
|
|
|
|
|
|
|
sc.cr2 = fi->cr2;
|
|
|
|
sc.err = fi->error_code;
|
|
|
|
sc.trapno = fi->trap_no;
|
|
|
|
|
|
|
|
PUTREG(IP, ip);
|
|
|
|
PUTREG(EFLAGS, flags);
|
2005-04-16 16:20:36 -06:00
|
|
|
#undef PUTREG
|
|
|
|
|
2011-08-18 13:00:49 -06:00
|
|
|
sc.oldmask = mask;
|
|
|
|
|
|
|
|
err = copy_to_user(to, &sc, sizeof(struct sigcontext));
|
2007-10-16 02:27:15 -06:00
|
|
|
if (err)
|
|
|
|
return 1;
|
|
|
|
|
2008-02-04 23:30:54 -07:00
|
|
|
err = save_fp_registers(userspace_pid[current_thread_info()->cpu],
|
2007-10-16 02:27:15 -06:00
|
|
|
(unsigned long *) &fp);
|
|
|
|
if (err < 0) {
|
|
|
|
printk(KERN_ERR "copy_sc_from_user - restore_fp_registers "
|
|
|
|
"failed, errno = %d\n", -err);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
|
|
|
|
return 1;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-10-16 02:27:18 -06:00
|
|
|
return err;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rt_sigframe
|
|
|
|
{
|
2007-10-16 02:27:00 -06:00
|
|
|
char __user *pretcode;
|
|
|
|
struct ucontext uc;
|
|
|
|
struct siginfo info;
|
2007-10-16 02:27:15 -06:00
|
|
|
struct _fpstate fpstate;
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
int setup_signal_stack_si(unsigned long stack_top, int sig,
|
|
|
|
struct k_sigaction *ka, struct pt_regs * regs,
|
|
|
|
siginfo_t *info, sigset_t *set)
|
|
|
|
{
|
|
|
|
struct rt_sigframe __user *frame;
|
2006-03-27 02:14:38 -07:00
|
|
|
unsigned long save_sp = PT_REGS_RSP(regs);
|
2005-04-16 16:20:36 -06:00
|
|
|
int err = 0;
|
|
|
|
struct task_struct *me = current;
|
|
|
|
|
|
|
|
frame = (struct rt_sigframe __user *)
|
2007-01-30 15:36:17 -07:00
|
|
|
round_down(stack_top - sizeof(struct rt_sigframe), 16);
|
|
|
|
/* Subtract 128 for a red zone and 8 for proper alignment */
|
2007-10-16 02:27:00 -06:00
|
|
|
frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (ka->sa.sa_flags & SA_SIGINFO) {
|
|
|
|
err |= copy_siginfo_to_user(&frame->info, info);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:18 -06:00
|
|
|
/*
|
|
|
|
* Update SP now because the page fault handler refuses to extend
|
2006-03-27 02:14:38 -07:00
|
|
|
* the stack if the faulting address is too far below the current
|
|
|
|
* SP, which frame now certainly is. If there's an error, the original
|
|
|
|
* value is restored on the way out.
|
|
|
|
* When writing the sigcontext to the stack, we have to write the
|
|
|
|
* original value, so that's passed to copy_sc_to_user, which does
|
|
|
|
* the right thing with it.
|
|
|
|
*/
|
|
|
|
PT_REGS_RSP(regs) = (unsigned long) frame;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Create the ucontext. */
|
|
|
|
err |= __put_user(0, &frame->uc.uc_flags);
|
|
|
|
err |= __put_user(0, &frame->uc.uc_link);
|
|
|
|
err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
|
2006-03-27 02:14:38 -07:00
|
|
|
err |= __put_user(sas_ss_flags(save_sp),
|
2005-04-16 16:20:36 -06:00
|
|
|
&frame->uc.uc_stack.ss_flags);
|
|
|
|
err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
|
2007-10-16 02:27:15 -06:00
|
|
|
err |= copy_sc_to_user(&frame->uc.uc_mcontext, &frame->fpstate, regs,
|
|
|
|
set->sig[0], save_sp);
|
|
|
|
err |= __put_user(&frame->fpstate, &frame->uc.uc_mcontext.fpstate);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (sizeof(*set) == 16) {
|
|
|
|
__put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
|
|
|
|
__put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
err |= __copy_to_user(&frame->uc.uc_sigmask, set,
|
|
|
|
sizeof(*set));
|
|
|
|
|
2007-10-16 02:27:18 -06:00
|
|
|
/*
|
|
|
|
* Set up to return from userspace. If provided, use a stub
|
|
|
|
* already in userspace.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
/* x86-64 should always use SA_RESTORER. */
|
|
|
|
if (ka->sa.sa_flags & SA_RESTORER)
|
|
|
|
err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
|
|
|
|
else
|
|
|
|
/* could use a vstub here */
|
2006-03-27 02:14:38 -07:00
|
|
|
goto restore_sp;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (err)
|
2006-03-27 02:14:38 -07:00
|
|
|
goto restore_sp;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* Set up registers for signal handler */
|
|
|
|
{
|
|
|
|
struct exec_domain *ed = current_thread_info()->exec_domain;
|
|
|
|
if (unlikely(ed && ed->signal_invmap && sig < 32))
|
|
|
|
sig = ed->signal_invmap[sig];
|
|
|
|
}
|
|
|
|
|
|
|
|
PT_REGS_RDI(regs) = sig;
|
|
|
|
/* In case the signal handler was declared without prototypes */
|
|
|
|
PT_REGS_RAX(regs) = 0;
|
|
|
|
|
2007-10-16 02:27:18 -06:00
|
|
|
/*
|
|
|
|
* This also works for non SA_SIGINFO handlers because they expect the
|
|
|
|
* next argument after the signal number on the stack.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
PT_REGS_RSI(regs) = (unsigned long) &frame->info;
|
|
|
|
PT_REGS_RDX(regs) = (unsigned long) &frame->uc;
|
|
|
|
PT_REGS_RIP(regs) = (unsigned long) ka->sa.sa_handler;
|
|
|
|
out:
|
2006-03-27 02:14:38 -07:00
|
|
|
return err;
|
|
|
|
|
|
|
|
restore_sp:
|
|
|
|
PT_REGS_RSP(regs) = save_sp;
|
|
|
|
return err;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
long sys_rt_sigreturn(struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
unsigned long sp = PT_REGS_SP(¤t->thread.regs);
|
|
|
|
struct rt_sigframe __user *frame =
|
|
|
|
(struct rt_sigframe __user *)(sp - 8);
|
|
|
|
struct ucontext __user *uc = &frame->uc;
|
|
|
|
sigset_t set;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
|
2005-04-16 16:20:36 -06:00
|
|
|
goto segfault;
|
|
|
|
|
|
|
|
sigdelsetmask(&set, ~_BLOCKABLE);
|
2011-08-18 13:00:19 -06:00
|
|
|
set_current_blocked(&set);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2011-08-18 13:00:29 -06:00
|
|
|
if (copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext))
|
2005-04-16 16:20:36 -06:00
|
|
|
goto segfault;
|
|
|
|
|
|
|
|
/* Avoid ERESTART handling */
|
|
|
|
PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1;
|
2007-10-16 02:27:18 -06:00
|
|
|
return PT_REGS_SYSCALL_RET(¤t->thread.regs);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
segfault:
|
|
|
|
force_sig(SIGSEGV, current);
|
|
|
|
return 0;
|
|
|
|
}
|