2 * Copyright (C) 2003 PathScale, Inc.
3 * Licensed under the GPL
6 #include "linux/personality.h"
7 #include "linux/ptrace.h"
8 #include "asm/unistd.h"
9 #include "asm/uaccess.h"
10 #include "asm/ucontext.h"
11 #include "frame_kern.h"
14 void copy_sc(struct uml_pt_regs *regs, void *from)
16 struct sigcontext *sc = from;
18 #define GETREG(regs, regno, sc, regname) \
19 (regs)->gp[(regno) / sizeof(unsigned long)] = (sc)->regname
21 GETREG(regs, R8, sc, r8);
22 GETREG(regs, R9, sc, r9);
23 GETREG(regs, R10, sc, r10);
24 GETREG(regs, R11, sc, r11);
25 GETREG(regs, R12, sc, r12);
26 GETREG(regs, R13, sc, r13);
27 GETREG(regs, R14, sc, r14);
28 GETREG(regs, R15, sc, r15);
29 GETREG(regs, RDI, sc, rdi);
30 GETREG(regs, RSI, sc, rsi);
31 GETREG(regs, RBP, sc, rbp);
32 GETREG(regs, RBX, sc, rbx);
33 GETREG(regs, RDX, sc, rdx);
34 GETREG(regs, RAX, sc, rax);
35 GETREG(regs, RCX, sc, rcx);
36 GETREG(regs, RSP, sc, rsp);
37 GETREG(regs, RIP, sc, rip);
38 GETREG(regs, EFLAGS, sc, eflags);
39 GETREG(regs, CS, sc, cs);
44 static int copy_sc_from_user(struct pt_regs *regs,
45 struct sigcontext __user *from)
49 #define GETREG(regs, regno, sc, regname) \
50 __get_user((regs)->regs.gp[(regno) / sizeof(unsigned long)], \
53 err |= GETREG(regs, R8, from, r8);
54 err |= GETREG(regs, R9, from, r9);
55 err |= GETREG(regs, R10, from, r10);
56 err |= GETREG(regs, R11, from, r11);
57 err |= GETREG(regs, R12, from, r12);
58 err |= GETREG(regs, R13, from, r13);
59 err |= GETREG(regs, R14, from, r14);
60 err |= GETREG(regs, R15, from, r15);
61 err |= GETREG(regs, RDI, from, rdi);
62 err |= GETREG(regs, RSI, from, rsi);
63 err |= GETREG(regs, RBP, from, rbp);
64 err |= GETREG(regs, RBX, from, rbx);
65 err |= GETREG(regs, RDX, from, rdx);
66 err |= GETREG(regs, RAX, from, rax);
67 err |= GETREG(regs, RCX, from, rcx);
68 err |= GETREG(regs, RSP, from, rsp);
69 err |= GETREG(regs, RIP, from, rip);
70 err |= GETREG(regs, EFLAGS, from, eflags);
71 err |= GETREG(regs, CS, from, cs);
78 static int copy_sc_to_user(struct sigcontext __user *to,
79 struct _fpstate __user *to_fp, struct pt_regs *regs,
80 unsigned long mask, unsigned long sp)
82 struct faultinfo * fi = ¤t->thread.arch.faultinfo;
85 err |= __put_user(0, &to->gs);
86 err |= __put_user(0, &to->fs);
88 #define PUTREG(regs, regno, sc, regname) \
89 __put_user((regs)->regs.gp[(regno) / sizeof(unsigned long)], \
92 err |= PUTREG(regs, RDI, to, rdi);
93 err |= PUTREG(regs, RSI, to, rsi);
94 err |= PUTREG(regs, RBP, to, rbp);
95 /* Must use orignal RSP, which is passed in, rather than what's in
96 * the pt_regs, because that's already been updated to point at the
99 err |= __put_user(sp, &to->rsp);
100 err |= PUTREG(regs, RBX, to, rbx);
101 err |= PUTREG(regs, RDX, to, rdx);
102 err |= PUTREG(regs, RCX, to, rcx);
103 err |= PUTREG(regs, RAX, to, rax);
104 err |= PUTREG(regs, R8, to, r8);
105 err |= PUTREG(regs, R9, to, r9);
106 err |= PUTREG(regs, R10, to, r10);
107 err |= PUTREG(regs, R11, to, r11);
108 err |= PUTREG(regs, R12, to, r12);
109 err |= PUTREG(regs, R13, to, r13);
110 err |= PUTREG(regs, R14, to, r14);
111 err |= PUTREG(regs, R15, to, r15);
112 err |= PUTREG(regs, CS, to, cs); /* XXX x86_64 doesn't do this */
114 err |= __put_user(fi->cr2, &to->cr2);
115 err |= __put_user(fi->error_code, &to->err);
116 err |= __put_user(fi->trap_no, &to->trapno);
118 err |= PUTREG(regs, RIP, to, rip);
119 err |= PUTREG(regs, EFLAGS, to, eflags);
122 err |= __put_user(mask, &to->oldmask);
129 char __user *pretcode;
134 #define round_down(m, n) (((m) / (n)) * (n))
136 int setup_signal_stack_si(unsigned long stack_top, int sig,
137 struct k_sigaction *ka, struct pt_regs * regs,
138 siginfo_t *info, sigset_t *set)
140 struct rt_sigframe __user *frame;
141 struct _fpstate __user *fp = NULL;
142 unsigned long save_sp = PT_REGS_RSP(regs);
144 struct task_struct *me = current;
146 frame = (struct rt_sigframe __user *)
147 round_down(stack_top - sizeof(struct rt_sigframe), 16);
148 /* Subtract 128 for a red zone and 8 for proper alignment */
149 frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
151 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
155 if (save_i387(fp) < 0)
158 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
161 if (ka->sa.sa_flags & SA_SIGINFO) {
162 err |= copy_siginfo_to_user(&frame->info, info);
167 /* Update SP now because the page fault handler refuses to extend
168 * the stack if the faulting address is too far below the current
169 * SP, which frame now certainly is. If there's an error, the original
170 * value is restored on the way out.
171 * When writing the sigcontext to the stack, we have to write the
172 * original value, so that's passed to copy_sc_to_user, which does
173 * the right thing with it.
175 PT_REGS_RSP(regs) = (unsigned long) frame;
177 /* Create the ucontext. */
178 err |= __put_user(0, &frame->uc.uc_flags);
179 err |= __put_user(0, &frame->uc.uc_link);
180 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
181 err |= __put_user(sas_ss_flags(save_sp),
182 &frame->uc.uc_stack.ss_flags);
183 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
184 err |= copy_sc_to_user(&frame->uc.uc_mcontext, fp, regs, set->sig[0],
186 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
187 if (sizeof(*set) == 16) {
188 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
189 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
192 err |= __copy_to_user(&frame->uc.uc_sigmask, set,
195 /* Set up to return from userspace. If provided, use a stub
196 already in userspace. */
197 /* x86-64 should always use SA_RESTORER. */
198 if (ka->sa.sa_flags & SA_RESTORER)
199 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
201 /* could use a vstub here */
207 /* Set up registers for signal handler */
209 struct exec_domain *ed = current_thread_info()->exec_domain;
210 if (unlikely(ed && ed->signal_invmap && sig < 32))
211 sig = ed->signal_invmap[sig];
214 PT_REGS_RDI(regs) = sig;
215 /* In case the signal handler was declared without prototypes */
216 PT_REGS_RAX(regs) = 0;
218 /* This also works for non SA_SIGINFO handlers because they expect the
219 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);
244 spin_lock_irq(¤t->sighand->siglock);
245 current->blocked = set;
247 spin_unlock_irq(¤t->sighand->siglock);
249 if (copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext))
252 /* Avoid ERESTART handling */
253 PT_REGS_SYSCALL_NR(¤t->thread.regs) = -1;
254 return(PT_REGS_SYSCALL_RET(¤t->thread.regs));
257 force_sig(SIGSEGV, current);