blob: 33a40f5ef0d2d5666df8bfa43052a73013eaebf4 [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
21static int copy_sc_from_user_skas(struct pt_regs *regs,
22 struct sigcontext *from)
23{
24 struct sigcontext sc;
25 unsigned long fpregs[HOST_FP_SIZE];
26 int err;
27
28 err = copy_from_user(&sc, from, sizeof(sc));
29 err |= copy_from_user(fpregs, sc.fpstate, sizeof(fpregs));
30 if(err)
31 return(err);
32
33 REGS_GS(regs->regs.skas.regs) = sc.gs;
34 REGS_FS(regs->regs.skas.regs) = sc.fs;
35 REGS_ES(regs->regs.skas.regs) = sc.es;
36 REGS_DS(regs->regs.skas.regs) = sc.ds;
37 REGS_EDI(regs->regs.skas.regs) = sc.edi;
38 REGS_ESI(regs->regs.skas.regs) = sc.esi;
39 REGS_EBP(regs->regs.skas.regs) = sc.ebp;
40 REGS_SP(regs->regs.skas.regs) = sc.esp;
41 REGS_EBX(regs->regs.skas.regs) = sc.ebx;
42 REGS_EDX(regs->regs.skas.regs) = sc.edx;
43 REGS_ECX(regs->regs.skas.regs) = sc.ecx;
44 REGS_EAX(regs->regs.skas.regs) = sc.eax;
45 REGS_IP(regs->regs.skas.regs) = sc.eip;
46 REGS_CS(regs->regs.skas.regs) = sc.cs;
47 REGS_EFLAGS(regs->regs.skas.regs) = sc.eflags;
48 REGS_SS(regs->regs.skas.regs) = sc.ss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 err = restore_fp_registers(userspace_pid[0], fpregs);
51 if(err < 0){
52 printk("copy_sc_from_user_skas - PTRACE_SETFPREGS failed, "
53 "errno = %d\n", err);
54 return(1);
55 }
56
57 return(0);
58}
59
60int copy_sc_to_user_skas(struct sigcontext *to, struct _fpstate *to_fp,
Jeff Dike98c18232006-03-27 01:14:38 -080061 struct pt_regs *regs, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct sigcontext sc;
64 unsigned long fpregs[HOST_FP_SIZE];
Bodo Stroesserc5784552005-05-05 16:15:31 -070065 struct faultinfo * fi = &current->thread.arch.faultinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int err;
67
68 sc.gs = REGS_GS(regs->regs.skas.regs);
69 sc.fs = REGS_FS(regs->regs.skas.regs);
70 sc.es = REGS_ES(regs->regs.skas.regs);
71 sc.ds = REGS_DS(regs->regs.skas.regs);
72 sc.edi = REGS_EDI(regs->regs.skas.regs);
73 sc.esi = REGS_ESI(regs->regs.skas.regs);
74 sc.ebp = REGS_EBP(regs->regs.skas.regs);
Jeff Dike98c18232006-03-27 01:14:38 -080075 sc.esp = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 sc.ebx = REGS_EBX(regs->regs.skas.regs);
77 sc.edx = REGS_EDX(regs->regs.skas.regs);
78 sc.ecx = REGS_ECX(regs->regs.skas.regs);
79 sc.eax = REGS_EAX(regs->regs.skas.regs);
80 sc.eip = REGS_IP(regs->regs.skas.regs);
81 sc.cs = REGS_CS(regs->regs.skas.regs);
82 sc.eflags = REGS_EFLAGS(regs->regs.skas.regs);
83 sc.esp_at_signal = regs->regs.skas.regs[UESP];
84 sc.ss = regs->regs.skas.regs[SS];
Bodo Stroesserc5784552005-05-05 16:15:31 -070085 sc.cr2 = fi->cr2;
86 sc.err = fi->error_code;
87 sc.trapno = fi->trap_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 err = save_fp_registers(userspace_pid[0], fpregs);
90 if(err < 0){
91 printk("copy_sc_to_user_skas - PTRACE_GETFPREGS failed, "
92 "errno = %d\n", err);
93 return(1);
94 }
95 to_fp = (to_fp ? to_fp : (struct _fpstate *) (to + 1));
96 sc.fpstate = to_fp;
97
98 if(err)
99 return(err);
100
101 return(copy_to_user(to, &sc, sizeof(sc)) ||
102 copy_to_user(to_fp, fpregs, sizeof(fpregs)));
103}
104#endif
105
106#ifdef CONFIG_MODE_TT
107
108/* These copy a sigcontext to/from userspace. They copy the fpstate pointer,
109 * blowing away the old, good one. So, that value is saved, and then restored
110 * after the sigcontext copy. In copy_from, the variable holding the saved
111 * fpstate pointer, and the sigcontext that it should be restored to are both
112 * in the kernel, so we can just restore using an assignment. In copy_to, the
113 * saved pointer is in the kernel, but the sigcontext is in userspace, so we
114 * copy_to_user it.
115 */
116int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext *from,
117 int fpsize)
118{
119 struct _fpstate *to_fp, *from_fp;
120 unsigned long sigs;
121 int err;
122
123 to_fp = to->fpstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 sigs = to->oldmask;
125 err = copy_from_user(to, from, sizeof(*to));
Al Viroe54a5df2005-09-03 15:57:27 -0700126 from_fp = to->fpstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 to->oldmask = sigs;
128 to->fpstate = to_fp;
129 if(to_fp != NULL)
130 err |= copy_from_user(to_fp, from_fp, fpsize);
131 return(err);
132}
133
134int copy_sc_to_user_tt(struct sigcontext *to, struct _fpstate *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800135 struct sigcontext *from, int fpsize, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct _fpstate *to_fp, *from_fp;
138 int err;
139
140 to_fp = (fp ? fp : (struct _fpstate *) (to + 1));
141 from_fp = from->fpstate;
142 err = copy_to_user(to, from, sizeof(*to));
Jeff Dike98c18232006-03-27 01:14:38 -0800143
144 /* The SP in the sigcontext is the updated one for the signal
145 * delivery. The sp passed in is the original, and this needs
146 * to be restored, so we stick it in separately.
147 */
148 err |= copy_to_user(&SC_SP(to), sp, sizeof(sp));
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if(from_fp != NULL){
151 err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate));
152 err |= copy_to_user(to_fp, from_fp, fpsize);
153 }
Jeff Dike98c18232006-03-27 01:14:38 -0800154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156#endif
157
158static int copy_sc_from_user(struct pt_regs *to, void __user *from)
159{
160 int ret;
161
162 ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from,
163 sizeof(struct _fpstate)),
164 copy_sc_from_user_skas(to, from));
165 return(ret);
166}
167
168static int copy_sc_to_user(struct sigcontext *to, struct _fpstate *fp,
Jeff Dike98c18232006-03-27 01:14:38 -0800169 struct pt_regs *from, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 return(CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
Jeff Dike98c18232006-03-27 01:14:38 -0800172 sizeof(*fp), sp),
173 copy_sc_to_user_skas(to, fp, from, sp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176static int copy_ucontext_to_user(struct ucontext *uc, struct _fpstate *fp,
177 sigset_t *set, unsigned long sp)
178{
179 int err = 0;
180
181 err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
182 err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
183 err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
Jeff Dike98c18232006-03-27 01:14:38 -0800184 err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
186 return(err);
187}
188
189struct sigframe
190{
191 char *pretcode;
192 int sig;
193 struct sigcontext sc;
194 struct _fpstate fpstate;
195 unsigned long extramask[_NSIG_WORDS-1];
196 char retcode[8];
197};
198
199struct rt_sigframe
200{
201 char *pretcode;
202 int sig;
203 struct siginfo *pinfo;
204 void *puc;
205 struct siginfo info;
206 struct ucontext uc;
207 struct _fpstate fpstate;
208 char retcode[8];
209};
210
211int setup_signal_stack_sc(unsigned long stack_top, int sig,
212 struct k_sigaction *ka, struct pt_regs *regs,
213 sigset_t *mask)
214{
215 struct sigframe __user *frame;
216 void *restorer;
Jeff Dike98c18232006-03-27 01:14:38 -0800217 unsigned long save_sp = PT_REGS_SP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int err = 0;
219
220 stack_top &= -8UL;
221 frame = (struct sigframe *) stack_top - 1;
222 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
223 return 1;
224
225 restorer = (void *) frame->retcode;
226 if(ka->sa.sa_flags & SA_RESTORER)
227 restorer = ka->sa.sa_restorer;
228
Jeff Dike98c18232006-03-27 01:14:38 -0800229 /* Update SP now because the page fault handler refuses to extend
230 * the stack if the faulting address is too far below the current
231 * SP, which frame now certainly is. If there's an error, the original
232 * value is restored on the way out.
233 * When writing the sigcontext to the stack, we have to write the
234 * original value, so that's passed to copy_sc_to_user, which does
235 * the right thing with it.
236 */
237 PT_REGS_SP(regs) = (unsigned long) frame;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 err |= __put_user(restorer, &frame->pretcode);
240 err |= __put_user(sig, &frame->sig);
Jeff Dike98c18232006-03-27 01:14:38 -0800241 err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 err |= __put_user(mask->sig[0], &frame->sc.oldmask);
243 if (_NSIG_WORDS > 1)
244 err |= __copy_to_user(&frame->extramask, &mask->sig[1],
245 sizeof(frame->extramask));
246
247 /*
248 * This is popl %eax ; movl $,%eax ; int $0x80
249 *
250 * WE DO NOT USE IT ANY MORE! It's only left here for historical
251 * reasons and because gdb uses it as a signature to notice
252 * signal handler stack frames.
253 */
254 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
255 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
256 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
257
258 if(err)
Jeff Dike98c18232006-03-27 01:14:38 -0800259 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 PT_REGS_SP(regs) = (unsigned long) frame;
262 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
263 PT_REGS_EAX(regs) = (unsigned long) sig;
264 PT_REGS_EDX(regs) = (unsigned long) 0;
265 PT_REGS_ECX(regs) = (unsigned long) 0;
266
267 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
268 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800269 return 0;
270
271err:
272 PT_REGS_SP(regs) = save_sp;
273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276int setup_signal_stack_si(unsigned long stack_top, int sig,
277 struct k_sigaction *ka, struct pt_regs *regs,
278 siginfo_t *info, sigset_t *mask)
279{
280 struct rt_sigframe __user *frame;
281 void *restorer;
Jeff Dike98c18232006-03-27 01:14:38 -0800282 unsigned long save_sp = PT_REGS_SP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int err = 0;
284
285 stack_top &= -8UL;
286 frame = (struct rt_sigframe *) stack_top - 1;
287 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288 return 1;
289
290 restorer = (void *) frame->retcode;
291 if(ka->sa.sa_flags & SA_RESTORER)
292 restorer = ka->sa.sa_restorer;
293
Jeff Dike98c18232006-03-27 01:14:38 -0800294 /* See comment above about why this is here */
295 PT_REGS_SP(regs) = (unsigned long) frame;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 err |= __put_user(restorer, &frame->pretcode);
298 err |= __put_user(sig, &frame->sig);
299 err |= __put_user(&frame->info, &frame->pinfo);
300 err |= __put_user(&frame->uc, &frame->puc);
301 err |= copy_siginfo_to_user(&frame->info, info);
302 err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
Jeff Dike98c18232006-03-27 01:14:38 -0800303 save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /*
306 * This is movl $,%eax ; int $0x80
307 *
308 * WE DO NOT USE IT ANY MORE! It's only left here for historical
309 * reasons and because gdb uses it as a signature to notice
310 * signal handler stack frames.
311 */
312 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
313 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
314 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
315
316 if(err)
Jeff Dike98c18232006-03-27 01:14:38 -0800317 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
320 PT_REGS_EAX(regs) = (unsigned long) sig;
321 PT_REGS_EDX(regs) = (unsigned long) &frame->info;
322 PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
323
324 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
325 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800326 return 0;
327
328err:
329 PT_REGS_SP(regs) = save_sp;
330 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333long sys_sigreturn(struct pt_regs regs)
334{
335 unsigned long sp = PT_REGS_SP(&current->thread.regs);
336 struct sigframe __user *frame = (struct sigframe *)(sp - 8);
337 sigset_t set;
338 struct sigcontext __user *sc = &frame->sc;
339 unsigned long __user *oldmask = &sc->oldmask;
340 unsigned long __user *extramask = frame->extramask;
341 int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
342
Andrew Morton350d5bd2005-06-25 14:55:19 -0700343 if(copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 copy_from_user(&set.sig[1], extramask, sig_size))
345 goto segfault;
346
347 sigdelsetmask(&set, ~_BLOCKABLE);
348
349 spin_lock_irq(&current->sighand->siglock);
350 current->blocked = set;
351 recalc_sigpending();
352 spin_unlock_irq(&current->sighand->siglock);
353
354 if(copy_sc_from_user(&current->thread.regs, sc))
355 goto segfault;
356
357 /* Avoid ERESTART handling */
358 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
359 return(PT_REGS_SYSCALL_RET(&current->thread.regs));
360
361 segfault:
362 force_sig(SIGSEGV, current);
363 return 0;
364}
365
366long sys_rt_sigreturn(struct pt_regs regs)
367{
368 unsigned long __user sp = PT_REGS_SP(&current->thread.regs);
369 struct rt_sigframe __user *frame = (struct rt_sigframe *) (sp - 4);
370 sigset_t set;
371 struct ucontext __user *uc = &frame->uc;
372 int sig_size = _NSIG_WORDS * sizeof(unsigned long);
373
374 if(copy_from_user(&set, &uc->uc_sigmask, sig_size))
375 goto segfault;
376
377 sigdelsetmask(&set, ~_BLOCKABLE);
378
379 spin_lock_irq(&current->sighand->siglock);
380 current->blocked = set;
381 recalc_sigpending();
382 spin_unlock_irq(&current->sighand->siglock);
383
384 if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
385 goto segfault;
386
387 /* Avoid ERESTART handling */
388 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
389 return(PT_REGS_SYSCALL_RET(&current->thread.regs));
390
391 segfault:
392 force_sig(SIGSEGV, current);
393 return 0;
394}
395
396/*
397 * Overrides for Emacs so that we follow Linus's tabbing style.
398 * Emacs will notice this stuff at the end of the file and automatically
399 * adjust the settings for this buffer only. This must remain at the end
400 * of the file.
401 * ---------------------------------------------------------------------------
402 * Local variables:
403 * c-file-style: "linux"
404 * End:
405 */