blob: 1cbf95f6858ae2b0a97fcb7c87cd711338da1331 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
5
6#include "linux/signal.h"
7#include "linux/ptrace.h"
8#include "asm/current.h"
9#include "asm/ucontext.h"
10#include "asm/uaccess.h"
11#include "asm/unistd.h"
12#include "frame_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "sigcontext.h"
14#include "registers.h"
15#include "mode.h"
16
17#ifdef CONFIG_MODE_SKAS
18
19#include "skas.h"
20
Jeff Dike377fad32007-05-06 14:51:25 -070021void copy_sc(union uml_pt_regs *regs, void *from)
22{
23 struct sigcontext *sc = from;
24
25 REGS_GS(regs->skas.regs) = sc->gs;
26 REGS_FS(regs->skas.regs) = sc->fs;
27 REGS_ES(regs->skas.regs) = sc->es;
28 REGS_DS(regs->skas.regs) = sc->ds;
29 REGS_EDI(regs->skas.regs) = sc->edi;
30 REGS_ESI(regs->skas.regs) = sc->esi;
31 REGS_EBP(regs->skas.regs) = sc->ebp;
32 REGS_SP(regs->skas.regs) = sc->esp;
33 REGS_EBX(regs->skas.regs) = sc->ebx;
34 REGS_EDX(regs->skas.regs) = sc->edx;
35 REGS_ECX(regs->skas.regs) = sc->ecx;
36 REGS_EAX(regs->skas.regs) = sc->eax;
37 REGS_IP(regs->skas.regs) = sc->eip;
38 REGS_CS(regs->skas.regs) = sc->cs;
39 REGS_EFLAGS(regs->skas.regs) = sc->eflags;
40 REGS_SS(regs->skas.regs) = sc->ss;
41}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int copy_sc_from_user_skas(struct pt_regs *regs,
Al Viro4d338e12006-03-31 02:30:15 -080044 struct sigcontext __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct sigcontext sc;
47 unsigned long fpregs[HOST_FP_SIZE];
48 int err;
49
50 err = copy_from_user(&sc, from, sizeof(sc));
51 err |= copy_from_user(fpregs, sc.fpstate, sizeof(fpregs));
52 if(err)
Jeff Dike5d864562007-05-06 14:51:24 -070053 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jeff Dike377fad32007-05-06 14:51:25 -070055 copy_sc(&regs->regs, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 err = restore_fp_registers(userspace_pid[0], fpregs);
Jeff Dike377fad32007-05-06 14:51:25 -070058 if(err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 printk("copy_sc_from_user_skas - PTRACE_SETFPREGS failed, "
Jeff Dike5d864562007-05-06 14:51:24 -070060 "errno = %d\n", -err);
61 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
Jeff Dike5d864562007-05-06 14:51:24 -070064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Al Virof983c452006-04-18 22:21:42 -070067int copy_sc_to_user_skas(struct sigcontext __user *to, struct _fpstate __user *to_fp,
Jeff Dike98c18232006-03-27 01:14:38 -080068 struct pt_regs *regs, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct sigcontext sc;
71 unsigned long fpregs[HOST_FP_SIZE];
Bodo Stroesserc5784552005-05-05 16:15:31 -070072 struct faultinfo * fi = &current->thread.arch.faultinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int err;
74
75 sc.gs = REGS_GS(regs->regs.skas.regs);
76 sc.fs = REGS_FS(regs->regs.skas.regs);
77 sc.es = REGS_ES(regs->regs.skas.regs);
78 sc.ds = REGS_DS(regs->regs.skas.regs);
79 sc.edi = REGS_EDI(regs->regs.skas.regs);
80 sc.esi = REGS_ESI(regs->regs.skas.regs);
81 sc.ebp = REGS_EBP(regs->regs.skas.regs);
Jeff Dike98c18232006-03-27 01:14:38 -080082 sc.esp = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 sc.ebx = REGS_EBX(regs->regs.skas.regs);
84 sc.edx = REGS_EDX(regs->regs.skas.regs);
85 sc.ecx = REGS_ECX(regs->regs.skas.regs);
86 sc.eax = REGS_EAX(regs->regs.skas.regs);
87 sc.eip = REGS_IP(regs->regs.skas.regs);
88 sc.cs = REGS_CS(regs->regs.skas.regs);
89 sc.eflags = REGS_EFLAGS(regs->regs.skas.regs);
90 sc.esp_at_signal = regs->regs.skas.regs[UESP];
91 sc.ss = regs->regs.skas.regs[SS];
Bodo Stroesserc5784552005-05-05 16:15:31 -070092 sc.cr2 = fi->cr2;
93 sc.err = fi->error_code;
94 sc.trapno = fi->trap_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 err = save_fp_registers(userspace_pid[0], fpregs);
97 if(err < 0){
98 printk("copy_sc_to_user_skas - PTRACE_GETFPREGS failed, "
99 "errno = %d\n", err);
Jeff Dike5d864562007-05-06 14:51:24 -0700100 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Al Viro4d338e12006-03-31 02:30:15 -0800102 to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 sc.fpstate = to_fp;
104
105 if(err)
Jeff Dike5d864562007-05-06 14:51:24 -0700106 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Jeff Dike5d864562007-05-06 14:51:24 -0700108 return copy_to_user(to, &sc, sizeof(sc)) ||
109 copy_to_user(to_fp, fpregs, sizeof(fpregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111#endif
112
113#ifdef CONFIG_MODE_TT
114
115/* These copy a sigcontext to/from userspace. They copy the fpstate pointer,
116 * blowing away the old, good one. So, that value is saved, and then restored
117 * after the sigcontext copy. In copy_from, the variable holding the saved
118 * fpstate pointer, and the sigcontext that it should be restored to are both
119 * in the kernel, so we can just restore using an assignment. In copy_to, the
120 * saved pointer is in the kernel, but the sigcontext is in userspace, so we
121 * copy_to_user it.
122 */
Al Viro4d338e12006-03-31 02:30:15 -0800123int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext __user *from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int fpsize)
125{
Al Viro4d338e12006-03-31 02:30:15 -0800126 struct _fpstate *to_fp;
127 struct _fpstate __user *from_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned long sigs;
129 int err;
130
131 to_fp = to->fpstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 sigs = to->oldmask;
133 err = copy_from_user(to, from, sizeof(*to));
Al Viroe54a5df2005-09-03 15:57:27 -0700134 from_fp = to->fpstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 to->oldmask = sigs;
136 to->fpstate = to_fp;
137 if(to_fp != NULL)
138 err |= copy_from_user(to_fp, from_fp, fpsize);
Jeff Dike5d864562007-05-06 14:51:24 -0700139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Al Virof983c452006-04-18 22:21:42 -0700142int copy_sc_to_user_tt(struct sigcontext __user *to, struct _fpstate __user *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800143 struct sigcontext *from, int fpsize, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Al Viro4d338e12006-03-31 02:30:15 -0800145 struct _fpstate __user *to_fp;
146 struct _fpstate *from_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int err;
148
Al Viro4d338e12006-03-31 02:30:15 -0800149 to_fp = (fp ? fp : (struct _fpstate __user *) (to + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 from_fp = from->fpstate;
151 err = copy_to_user(to, from, sizeof(*to));
Jeff Dike98c18232006-03-27 01:14:38 -0800152
153 /* The SP in the sigcontext is the updated one for the signal
154 * delivery. The sp passed in is the original, and this needs
155 * to be restored, so we stick it in separately.
156 */
Paolo 'Blaisorblade' Giarrussof53389d2006-04-10 22:53:34 -0700157 err |= copy_to_user(&SC_SP(to), &sp, sizeof(sp));
Jeff Dike98c18232006-03-27 01:14:38 -0800158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if(from_fp != NULL){
160 err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate));
161 err |= copy_to_user(to_fp, from_fp, fpsize);
162 }
Jeff Dike98c18232006-03-27 01:14:38 -0800163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165#endif
166
167static int copy_sc_from_user(struct pt_regs *to, void __user *from)
168{
169 int ret;
170
171 ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from,
172 sizeof(struct _fpstate)),
173 copy_sc_from_user_skas(to, from));
Jeff Dike5d864562007-05-06 14:51:24 -0700174 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Al Virof983c452006-04-18 22:21:42 -0700177static int copy_sc_to_user(struct sigcontext __user *to, struct _fpstate __user *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800178 struct pt_regs *from, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Jeff Dike5d864562007-05-06 14:51:24 -0700180 return CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
Jeff Dike98c18232006-03-27 01:14:38 -0800181 sizeof(*fp), sp),
Jeff Dike5d864562007-05-06 14:51:24 -0700182 copy_sc_to_user_skas(to, fp, from, sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Al Viro4d338e12006-03-31 02:30:15 -0800185static int copy_ucontext_to_user(struct ucontext __user *uc, struct _fpstate __user *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sigset_t *set, unsigned long sp)
187{
188 int err = 0;
189
190 err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
191 err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
192 err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
Jeff Dike98c18232006-03-27 01:14:38 -0800193 err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
Jeff Dike5d864562007-05-06 14:51:24 -0700195 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198struct sigframe
199{
Al Viro4d338e12006-03-31 02:30:15 -0800200 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int sig;
202 struct sigcontext sc;
203 struct _fpstate fpstate;
204 unsigned long extramask[_NSIG_WORDS-1];
205 char retcode[8];
206};
207
208struct rt_sigframe
209{
Al Viro4d338e12006-03-31 02:30:15 -0800210 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int sig;
Al Viro4d338e12006-03-31 02:30:15 -0800212 struct siginfo __user *pinfo;
213 void __user *puc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct siginfo info;
215 struct ucontext uc;
216 struct _fpstate fpstate;
217 char retcode[8];
218};
219
220int setup_signal_stack_sc(unsigned long stack_top, int sig,
221 struct k_sigaction *ka, struct pt_regs *regs,
222 sigset_t *mask)
223{
224 struct sigframe __user *frame;
Al Viro4d338e12006-03-31 02:30:15 -0800225 void __user *restorer;
Jeff Dike98c18232006-03-27 01:14:38 -0800226 unsigned long save_sp = PT_REGS_SP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int err = 0;
228
Jeff Dike38966252007-01-30 14:36:17 -0800229 /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
230 stack_top = ((stack_top + 4) & -16UL) - 4;
Al Viro4d338e12006-03-31 02:30:15 -0800231 frame = (struct sigframe __user *) stack_top - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
233 return 1;
234
Al Viro4d338e12006-03-31 02:30:15 -0800235 restorer = frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if(ka->sa.sa_flags & SA_RESTORER)
237 restorer = ka->sa.sa_restorer;
238
Jeff Dike98c18232006-03-27 01:14:38 -0800239 /* Update SP now because the page fault handler refuses to extend
240 * the stack if the faulting address is too far below the current
241 * SP, which frame now certainly is. If there's an error, the original
242 * value is restored on the way out.
243 * When writing the sigcontext to the stack, we have to write the
244 * original value, so that's passed to copy_sc_to_user, which does
245 * the right thing with it.
246 */
247 PT_REGS_SP(regs) = (unsigned long) frame;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 err |= __put_user(restorer, &frame->pretcode);
250 err |= __put_user(sig, &frame->sig);
Jeff Dike98c18232006-03-27 01:14:38 -0800251 err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 err |= __put_user(mask->sig[0], &frame->sc.oldmask);
253 if (_NSIG_WORDS > 1)
254 err |= __copy_to_user(&frame->extramask, &mask->sig[1],
255 sizeof(frame->extramask));
256
257 /*
258 * This is popl %eax ; movl $,%eax ; int $0x80
259 *
260 * WE DO NOT USE IT ANY MORE! It's only left here for historical
261 * reasons and because gdb uses it as a signature to notice
262 * signal handler stack frames.
263 */
264 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
265 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
266 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
267
268 if(err)
Jeff Dike98c18232006-03-27 01:14:38 -0800269 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 PT_REGS_SP(regs) = (unsigned long) frame;
272 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
273 PT_REGS_EAX(regs) = (unsigned long) sig;
274 PT_REGS_EDX(regs) = (unsigned long) 0;
275 PT_REGS_ECX(regs) = (unsigned long) 0;
276
277 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
278 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800279 return 0;
280
281err:
282 PT_REGS_SP(regs) = save_sp;
283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286int setup_signal_stack_si(unsigned long stack_top, int sig,
287 struct k_sigaction *ka, struct pt_regs *regs,
288 siginfo_t *info, sigset_t *mask)
289{
290 struct rt_sigframe __user *frame;
Al Viro4d338e12006-03-31 02:30:15 -0800291 void __user *restorer;
Jeff Dike98c18232006-03-27 01:14:38 -0800292 unsigned long save_sp = PT_REGS_SP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int err = 0;
294
295 stack_top &= -8UL;
Al Viro4d338e12006-03-31 02:30:15 -0800296 frame = (struct rt_sigframe __user *) stack_top - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
298 return 1;
299
Al Viro4d338e12006-03-31 02:30:15 -0800300 restorer = frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if(ka->sa.sa_flags & SA_RESTORER)
302 restorer = ka->sa.sa_restorer;
303
Jeff Dike98c18232006-03-27 01:14:38 -0800304 /* See comment above about why this is here */
305 PT_REGS_SP(regs) = (unsigned long) frame;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 err |= __put_user(restorer, &frame->pretcode);
308 err |= __put_user(sig, &frame->sig);
309 err |= __put_user(&frame->info, &frame->pinfo);
310 err |= __put_user(&frame->uc, &frame->puc);
311 err |= copy_siginfo_to_user(&frame->info, info);
312 err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
Jeff Dike98c18232006-03-27 01:14:38 -0800313 save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /*
316 * This is movl $,%eax ; int $0x80
317 *
318 * WE DO NOT USE IT ANY MORE! It's only left here for historical
319 * reasons and because gdb uses it as a signature to notice
320 * signal handler stack frames.
321 */
322 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
323 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
324 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
325
326 if(err)
Jeff Dike98c18232006-03-27 01:14:38 -0800327 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
330 PT_REGS_EAX(regs) = (unsigned long) sig;
331 PT_REGS_EDX(regs) = (unsigned long) &frame->info;
332 PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
333
334 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
335 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800336 return 0;
337
338err:
339 PT_REGS_SP(regs) = save_sp;
340 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343long sys_sigreturn(struct pt_regs regs)
344{
345 unsigned long sp = PT_REGS_SP(&current->thread.regs);
Al Viro4d338e12006-03-31 02:30:15 -0800346 struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 sigset_t set;
348 struct sigcontext __user *sc = &frame->sc;
349 unsigned long __user *oldmask = &sc->oldmask;
350 unsigned long __user *extramask = frame->extramask;
351 int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
352
Andrew Morton350d5bd2005-06-25 14:55:19 -0700353 if(copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 copy_from_user(&set.sig[1], extramask, sig_size))
355 goto segfault;
356
357 sigdelsetmask(&set, ~_BLOCKABLE);
358
359 spin_lock_irq(&current->sighand->siglock);
360 current->blocked = set;
361 recalc_sigpending();
362 spin_unlock_irq(&current->sighand->siglock);
363
364 if(copy_sc_from_user(&current->thread.regs, sc))
365 goto segfault;
366
367 /* Avoid ERESTART handling */
368 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
Jeff Dike5d864562007-05-06 14:51:24 -0700369 return PT_REGS_SYSCALL_RET(&current->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 segfault:
372 force_sig(SIGSEGV, current);
373 return 0;
374}
375
376long sys_rt_sigreturn(struct pt_regs regs)
377{
Al Viro4d338e12006-03-31 02:30:15 -0800378 unsigned long sp = PT_REGS_SP(&current->thread.regs);
379 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (sp - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sigset_t set;
381 struct ucontext __user *uc = &frame->uc;
382 int sig_size = _NSIG_WORDS * sizeof(unsigned long);
383
384 if(copy_from_user(&set, &uc->uc_sigmask, sig_size))
385 goto segfault;
386
387 sigdelsetmask(&set, ~_BLOCKABLE);
388
389 spin_lock_irq(&current->sighand->siglock);
390 current->blocked = set;
391 recalc_sigpending();
392 spin_unlock_irq(&current->sighand->siglock);
393
394 if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
395 goto segfault;
396
397 /* Avoid ERESTART handling */
398 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
Jeff Dike5d864562007-05-06 14:51:24 -0700399 return PT_REGS_SYSCALL_RET(&current->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 segfault:
402 force_sig(SIGSEGV, current);
403 return 0;
404}