blob: fe8ec04d35bbbecc9b685702f69c44b6df4b6be0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 PathScale, Inc.
3 * Licensed under the GPL
4 */
5
6#include "linux/stddef.h"
7#include "linux/errno.h"
8#include "linux/personality.h"
9#include "linux/ptrace.h"
10#include "asm/current.h"
11#include "asm/uaccess.h"
12#include "asm/sigcontext.h"
13#include "asm/ptrace.h"
14#include "asm/arch/ucontext.h"
15#include "choose-mode.h"
16#include "sysdep/ptrace.h"
17#include "frame_kern.h"
18
19#ifdef CONFIG_MODE_SKAS
20
21#include "skas.h"
22
Jeff Dike377fad32007-05-06 14:51:25 -070023void copy_sc(union uml_pt_regs *regs, void *from)
24{
25 struct sigcontext *sc = from;
26
27#define GETREG(regs, regno, sc, regname) \
28 (regs)->skas.regs[(regno) / sizeof(unsigned long)] = (sc)->regname
29
30 GETREG(regs, R8, sc, r8);
31 GETREG(regs, R9, sc, r9);
32 GETREG(regs, R10, sc, r10);
33 GETREG(regs, R11, sc, r11);
34 GETREG(regs, R12, sc, r12);
35 GETREG(regs, R13, sc, r13);
36 GETREG(regs, R14, sc, r14);
37 GETREG(regs, R15, sc, r15);
38 GETREG(regs, RDI, sc, rdi);
39 GETREG(regs, RSI, sc, rsi);
40 GETREG(regs, RBP, sc, rbp);
41 GETREG(regs, RBX, sc, rbx);
42 GETREG(regs, RDX, sc, rdx);
43 GETREG(regs, RAX, sc, rax);
44 GETREG(regs, RCX, sc, rcx);
45 GETREG(regs, RSP, sc, rsp);
46 GETREG(regs, RIP, sc, rip);
47 GETREG(regs, EFLAGS, sc, eflags);
48 GETREG(regs, CS, sc, cs);
49
50#undef GETREG
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int copy_sc_from_user_skas(struct pt_regs *regs,
Al Virob8719c32006-06-04 02:51:48 -070054 struct sigcontext __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 int err = 0;
57
58#define GETREG(regs, regno, sc, regname) \
59 __get_user((regs)->regs.skas.regs[(regno) / sizeof(unsigned long)], \
60 &(sc)->regname)
61
62 err |= GETREG(regs, R8, from, r8);
63 err |= GETREG(regs, R9, from, r9);
64 err |= GETREG(regs, R10, from, r10);
65 err |= GETREG(regs, R11, from, r11);
66 err |= GETREG(regs, R12, from, r12);
67 err |= GETREG(regs, R13, from, r13);
68 err |= GETREG(regs, R14, from, r14);
69 err |= GETREG(regs, R15, from, r15);
70 err |= GETREG(regs, RDI, from, rdi);
71 err |= GETREG(regs, RSI, from, rsi);
72 err |= GETREG(regs, RBP, from, rbp);
73 err |= GETREG(regs, RBX, from, rbx);
74 err |= GETREG(regs, RDX, from, rdx);
75 err |= GETREG(regs, RAX, from, rax);
76 err |= GETREG(regs, RCX, from, rcx);
77 err |= GETREG(regs, RSP, from, rsp);
78 err |= GETREG(regs, RIP, from, rip);
79 err |= GETREG(regs, EFLAGS, from, eflags);
80 err |= GETREG(regs, CS, from, cs);
81
82#undef GETREG
83
Jeff Dike5d864562007-05-06 14:51:24 -070084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Al Virob8719c32006-06-04 02:51:48 -070087int copy_sc_to_user_skas(struct sigcontext __user *to,
88 struct _fpstate __user *to_fp,
Jeff Dike98c18232006-03-27 01:14:38 -080089 struct pt_regs *regs, unsigned long mask,
90 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Bodo Stroesserc5784552005-05-05 16:15:31 -070092 struct faultinfo * fi = &current->thread.arch.faultinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int err = 0;
94
95 err |= __put_user(0, &to->gs);
96 err |= __put_user(0, &to->fs);
97
98#define PUTREG(regs, regno, sc, regname) \
99 __put_user((regs)->regs.skas.regs[(regno) / sizeof(unsigned long)], \
100 &(sc)->regname)
101
102 err |= PUTREG(regs, RDI, to, rdi);
103 err |= PUTREG(regs, RSI, to, rsi);
104 err |= PUTREG(regs, RBP, to, rbp);
Jeff Dike98c18232006-03-27 01:14:38 -0800105 /* Must use orignal RSP, which is passed in, rather than what's in
106 * the pt_regs, because that's already been updated to point at the
107 * signal frame.
108 */
109 err |= __put_user(sp, &to->rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 err |= PUTREG(regs, RBX, to, rbx);
111 err |= PUTREG(regs, RDX, to, rdx);
112 err |= PUTREG(regs, RCX, to, rcx);
113 err |= PUTREG(regs, RAX, to, rax);
114 err |= PUTREG(regs, R8, to, r8);
115 err |= PUTREG(regs, R9, to, r9);
116 err |= PUTREG(regs, R10, to, r10);
117 err |= PUTREG(regs, R11, to, r11);
118 err |= PUTREG(regs, R12, to, r12);
119 err |= PUTREG(regs, R13, to, r13);
120 err |= PUTREG(regs, R14, to, r14);
121 err |= PUTREG(regs, R15, to, r15);
122 err |= PUTREG(regs, CS, to, cs); /* XXX x86_64 doesn't do this */
Bodo Stroesserc5784552005-05-05 16:15:31 -0700123
124 err |= __put_user(fi->cr2, &to->cr2);
125 err |= __put_user(fi->error_code, &to->err);
126 err |= __put_user(fi->trap_no, &to->trapno);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 err |= PUTREG(regs, RIP, to, rip);
129 err |= PUTREG(regs, EFLAGS, to, eflags);
130#undef PUTREG
131
132 err |= __put_user(mask, &to->oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 return(err);
135}
136
137#endif
138
139#ifdef CONFIG_MODE_TT
Al Virob8719c32006-06-04 02:51:48 -0700140int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext __user *from,
Jeff Dike98c18232006-03-27 01:14:38 -0800141 int fpsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Al Virob8719c32006-06-04 02:51:48 -0700143 struct _fpstate *to_fp;
144 struct _fpstate __user *from_fp;
Al Viroe54a5df2005-09-03 15:57:27 -0700145 unsigned long sigs;
146 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Al Viroe54a5df2005-09-03 15:57:27 -0700148 to_fp = to->fpstate;
149 sigs = to->oldmask;
150 err = copy_from_user(to, from, sizeof(*to));
151 from_fp = to->fpstate;
152 to->fpstate = to_fp;
153 to->oldmask = sigs;
154 if(to_fp != NULL)
155 err |= copy_from_user(to_fp, from_fp, fpsize);
156 return(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Al Virob8719c32006-06-04 02:51:48 -0700159int copy_sc_to_user_tt(struct sigcontext __user *to, struct _fpstate __user *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800160 struct sigcontext *from, int fpsize, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Al Virob8719c32006-06-04 02:51:48 -0700162 struct _fpstate __user *to_fp;
163 struct _fpstate *from_fp;
Al Viroe54a5df2005-09-03 15:57:27 -0700164 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Al Virob8719c32006-06-04 02:51:48 -0700166 to_fp = (fp ? fp : (struct _fpstate __user *) (to + 1));
Al Viroe54a5df2005-09-03 15:57:27 -0700167 from_fp = from->fpstate;
168 err = copy_to_user(to, from, sizeof(*to));
Jeff Dike98c18232006-03-27 01:14:38 -0800169 /* The SP in the sigcontext is the updated one for the signal
170 * delivery. The sp passed in is the original, and this needs
171 * to be restored, so we stick it in separately.
172 */
Paolo 'Blaisorblade' Giarrussof53389d2006-04-10 22:53:34 -0700173 err |= copy_to_user(&SC_SP(to), &sp, sizeof(sp));
Jeff Dike98c18232006-03-27 01:14:38 -0800174
Al Viroe54a5df2005-09-03 15:57:27 -0700175 if(from_fp != NULL){
176 err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate));
177 err |= copy_to_user(to_fp, from_fp, fpsize);
178 }
Jeff Dike98c18232006-03-27 01:14:38 -0800179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182#endif
183
184static int copy_sc_from_user(struct pt_regs *to, void __user *from)
185{
186 int ret;
187
188 ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from,
189 sizeof(struct _fpstate)),
190 copy_sc_from_user_skas(to, from));
191 return(ret);
192}
193
Al Virob8719c32006-06-04 02:51:48 -0700194static int copy_sc_to_user(struct sigcontext __user *to,
195 struct _fpstate __user *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800196 struct pt_regs *from, unsigned long mask,
197 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 return(CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
Jeff Dike98c18232006-03-27 01:14:38 -0800200 sizeof(*fp), sp),
201 copy_sc_to_user_skas(to, fp, from, mask, sp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204struct rt_sigframe
205{
Al Virob8719c32006-06-04 02:51:48 -0700206 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct ucontext uc;
208 struct siginfo info;
209};
210
211#define round_down(m, n) (((m) / (n)) * (n))
212
213int setup_signal_stack_si(unsigned long stack_top, int sig,
214 struct k_sigaction *ka, struct pt_regs * regs,
215 siginfo_t *info, sigset_t *set)
216{
217 struct rt_sigframe __user *frame;
218 struct _fpstate __user *fp = NULL;
Jeff Dike98c18232006-03-27 01:14:38 -0800219 unsigned long save_sp = PT_REGS_RSP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int err = 0;
221 struct task_struct *me = current;
222
223 frame = (struct rt_sigframe __user *)
Jeff Dike38966252007-01-30 14:36:17 -0800224 round_down(stack_top - sizeof(struct rt_sigframe), 16);
225 /* Subtract 128 for a red zone and 8 for proper alignment */
226 frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
229 goto out;
230
231#if 0 /* XXX */
232 if (save_i387(fp) < 0)
233 err |= -1;
234#endif
235 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
236 goto out;
237
238 if (ka->sa.sa_flags & SA_SIGINFO) {
239 err |= copy_siginfo_to_user(&frame->info, info);
240 if (err)
241 goto out;
242 }
243
Jeff Dike98c18232006-03-27 01:14:38 -0800244 /* Update SP now because the page fault handler refuses to extend
245 * the stack if the faulting address is too far below the current
246 * SP, which frame now certainly is. If there's an error, the original
247 * value is restored on the way out.
248 * When writing the sigcontext to the stack, we have to write the
249 * original value, so that's passed to copy_sc_to_user, which does
250 * the right thing with it.
251 */
252 PT_REGS_RSP(regs) = (unsigned long) frame;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Create the ucontext. */
255 err |= __put_user(0, &frame->uc.uc_flags);
256 err |= __put_user(0, &frame->uc.uc_link);
257 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
Jeff Dike98c18232006-03-27 01:14:38 -0800258 err |= __put_user(sas_ss_flags(save_sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 &frame->uc.uc_stack.ss_flags);
260 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
Jeff Dike98c18232006-03-27 01:14:38 -0800261 err |= copy_sc_to_user(&frame->uc.uc_mcontext, fp, regs, set->sig[0],
262 save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
264 if (sizeof(*set) == 16) {
265 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
266 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
267 }
268 else
269 err |= __copy_to_user(&frame->uc.uc_sigmask, set,
270 sizeof(*set));
271
272 /* Set up to return from userspace. If provided, use a stub
273 already in userspace. */
274 /* x86-64 should always use SA_RESTORER. */
275 if (ka->sa.sa_flags & SA_RESTORER)
276 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
277 else
278 /* could use a vstub here */
Jeff Dike98c18232006-03-27 01:14:38 -0800279 goto restore_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (err)
Jeff Dike98c18232006-03-27 01:14:38 -0800282 goto restore_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* Set up registers for signal handler */
285 {
286 struct exec_domain *ed = current_thread_info()->exec_domain;
287 if (unlikely(ed && ed->signal_invmap && sig < 32))
288 sig = ed->signal_invmap[sig];
289 }
290
291 PT_REGS_RDI(regs) = sig;
292 /* In case the signal handler was declared without prototypes */
293 PT_REGS_RAX(regs) = 0;
294
295 /* This also works for non SA_SIGINFO handlers because they expect the
296 next argument after the signal number on the stack. */
297 PT_REGS_RSI(regs) = (unsigned long) &frame->info;
298 PT_REGS_RDX(regs) = (unsigned long) &frame->uc;
299 PT_REGS_RIP(regs) = (unsigned long) ka->sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 out:
Jeff Dike98c18232006-03-27 01:14:38 -0800301 return err;
302
303restore_sp:
304 PT_REGS_RSP(regs) = save_sp;
305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308long sys_rt_sigreturn(struct pt_regs *regs)
309{
310 unsigned long sp = PT_REGS_SP(&current->thread.regs);
311 struct rt_sigframe __user *frame =
312 (struct rt_sigframe __user *)(sp - 8);
313 struct ucontext __user *uc = &frame->uc;
314 sigset_t set;
315
316 if(copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
317 goto segfault;
318
319 sigdelsetmask(&set, ~_BLOCKABLE);
320
321 spin_lock_irq(&current->sighand->siglock);
322 current->blocked = set;
323 recalc_sigpending();
324 spin_unlock_irq(&current->sighand->siglock);
325
326 if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
327 goto segfault;
328
329 /* Avoid ERESTART handling */
330 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
331 return(PT_REGS_SYSCALL_RET(&current->thread.regs));
332
333 segfault:
334 force_sig(SIGSEGV, current);
335 return 0;
336}
337/*
338 * Overrides for Emacs so that we follow Linus's tabbing style.
339 * Emacs will notice this stuff at the end of the file and automatically
340 * adjust the settings for this buffer only. This must remain at the end
341 * of the file.
342 * ---------------------------------------------------------------------------
343 * Local variables:
344 * c-file-style: "linux"
345 * End:
346 */