2 * Copyright (C) 2003 PathScale, Inc.
3 * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
7 #include <linux/personality.h>
8 #include <linux/ptrace.h>
9 #include <linux/kernel.h>
10 #include <asm/unistd.h>
11 #include <asm/uaccess.h>
12 #include <asm/ucontext.h>
13 #include "frame_kern.h"
16 static int copy_sc_from_user(struct pt_regs *regs,
17 struct sigcontext __user *from)
20 struct user_i387_struct fp;
24 err = copy_from_user(&sc, from, sizeof(sc));
28 #define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
47 GETREG(EFLAGS, flags);
53 err = copy_from_user(&fp, buf, sizeof(struct user_i387_struct));
57 err = restore_fp_registers(userspace_pid[current_thread_info()->cpu],
58 (unsigned long *) &fp);
60 printk(KERN_ERR "copy_sc_from_user - "
61 "restore_fp_registers failed, errno = %d\n",
69 static int copy_sc_to_user(struct sigcontext __user *to,
70 struct _fpstate __user *to_fp, struct pt_regs *regs,
71 unsigned long mask, unsigned long sp)
73 struct faultinfo * fi = ¤t->thread.arch.faultinfo;
75 struct user_i387_struct fp;
77 memset(&sc, 0, sizeof(struct sigcontext));
79 #define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno]
85 * Must use original RSP, which is passed in, rather than what's in
101 PUTREG(CS, cs); /* XXX x86_64 doesn't do this */
104 sc.err = fi->error_code;
105 sc.trapno = fi->trap_no;
108 PUTREG(EFLAGS, flags);
113 err = copy_to_user(to, &sc, sizeof(struct sigcontext));
117 err = save_fp_registers(userspace_pid[current_thread_info()->cpu],
118 (unsigned long *) &fp);
120 printk(KERN_ERR "copy_sc_from_user - restore_fp_registers "
121 "failed, errno = %d\n", -err);
125 if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
133 char __user *pretcode;
136 struct _fpstate fpstate;
139 int setup_signal_stack_si(unsigned long stack_top, int sig,
140 struct k_sigaction *ka, struct pt_regs * regs,
141 siginfo_t *info, sigset_t *set)
143 struct rt_sigframe __user *frame;
144 unsigned long save_sp = PT_REGS_RSP(regs);
146 struct task_struct *me = current;
148 frame = (struct rt_sigframe __user *)
149 round_down(stack_top - sizeof(struct rt_sigframe), 16);
150 /* Subtract 128 for a red zone and 8 for proper alignment */
151 frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
153 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
156 if (ka->sa.sa_flags & SA_SIGINFO) {
157 err |= copy_siginfo_to_user(&frame->info, info);
163 * Update SP now because the page fault handler refuses to extend
164 * the stack if the faulting address is too far below the current
165 * SP, which frame now certainly is. If there's an error, the original
166 * value is restored on the way out.
167 * When writing the sigcontext to the stack, we have to write the
168 * original value, so that's passed to copy_sc_to_user, which does
169 * the right thing with it.
171 PT_REGS_RSP(regs) = (unsigned long) frame;
173 /* Create the ucontext. */
174 err |= __put_user(0, &frame->uc.uc_flags);
175 err |= __put_user(0, &frame->uc.uc_link);
176 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
177 err |= __put_user(sas_ss_flags(save_sp),
178 &frame->uc.uc_stack.ss_flags);
179 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
180 err |= copy_sc_to_user(&frame->uc.uc_mcontext, &frame->fpstate, regs,
181 set->sig[0], save_sp);
182 err |= __put_user(&frame->fpstate, &frame->uc.uc_mcontext.fpstate);
183 if (sizeof(*set) == 16) {
184 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
185 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
188 err |= __copy_to_user(&frame->uc.uc_sigmask, set,
192 * Set up to return from userspace. If provided, use a stub
193 * already in userspace.
195 /* x86-64 should always use SA_RESTORER. */
196 if (ka->sa.sa_flags & SA_RESTORER)
197 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
199 /* could use a vstub here */
205 /* Set up registers for signal handler */
207 struct exec_domain *ed = current_thread_info()->exec_domain;
208 if (unlikely(ed && ed->signal_invmap && sig < 32))
209 sig = ed->signal_invmap[sig];
212 PT_REGS_RDI(regs) = sig;
213 /* In case the signal handler was declared without prototypes */
214 PT_REGS_RAX(regs) = 0;
217 * This also works for non SA_SIGINFO handlers because they expect the
218 * next argument after the signal number on the stack.
220 PT_REGS_RSI(regs) = (unsigned long) &frame->info;
221 PT_REGS_RDX(regs) = (unsigned long) &frame->uc;
222 PT_REGS_RIP(regs) = (unsigned long) ka->sa.sa_handler;
227 PT_REGS_RSP(regs) = save_sp;
231 long sys_rt_sigreturn(struct pt_regs *regs)
233 unsigned long sp = PT_REGS_SP(¤t->thread.regs);
234 struct rt_sigframe __user *frame =
235 (struct rt_sigframe __user *)(sp - 8);
236 struct ucontext __user *uc = &frame->uc;
239 if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
242 sigdelsetmask(&set, ~_BLOCKABLE);
243 set_current_blocked(&set);
245 if (copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext))
248 /* Avoid ERESTART handling */
249 PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1;
250 return PT_REGS_SYSCALL_RET(¤t->thread.regs);
253 force_sig(SIGSEGV, current);