blob: c82e5f562ec6b081572a95f06ec3c706f974c13a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/ptrace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "asm/unistd.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -07008#include "asm/uaccess.h"
9#include "asm/ucontext.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "frame_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "skas.h"
12
Jeff Dike77bf4402007-10-16 01:26:58 -070013void copy_sc(struct uml_pt_regs *regs, void *from)
Jeff Dike377fad32007-05-06 14:51:25 -070014{
15 struct sigcontext *sc = from;
16
Jeff Dike77bf4402007-10-16 01:26:58 -070017 REGS_GS(regs->regs) = sc->gs;
18 REGS_FS(regs->regs) = sc->fs;
19 REGS_ES(regs->regs) = sc->es;
20 REGS_DS(regs->regs) = sc->ds;
21 REGS_EDI(regs->regs) = sc->edi;
22 REGS_ESI(regs->regs) = sc->esi;
23 REGS_EBP(regs->regs) = sc->ebp;
24 REGS_SP(regs->regs) = sc->esp;
25 REGS_EBX(regs->regs) = sc->ebx;
26 REGS_EDX(regs->regs) = sc->edx;
27 REGS_ECX(regs->regs) = sc->ecx;
28 REGS_EAX(regs->regs) = sc->eax;
29 REGS_IP(regs->regs) = sc->eip;
30 REGS_CS(regs->regs) = sc->cs;
31 REGS_EFLAGS(regs->regs) = sc->eflags;
32 REGS_SS(regs->regs) = sc->ss;
Jeff Dike377fad32007-05-06 14:51:25 -070033}
34
Jeff Dike77bf4402007-10-16 01:26:58 -070035static int copy_sc_from_user(struct pt_regs *regs,
36 struct sigcontext __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Jeff Dikeba180fd2007-10-16 01:27:00 -070038 struct sigcontext sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 unsigned long fpregs[HOST_FP_SIZE];
40 int err;
41
42 err = copy_from_user(&sc, from, sizeof(sc));
43 err |= copy_from_user(fpregs, sc.fpstate, sizeof(fpregs));
Jeff Dikeba180fd2007-10-16 01:27:00 -070044 if (err)
Jeff Dike5d864562007-05-06 14:51:24 -070045 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jeff Dike377fad32007-05-06 14:51:25 -070047 copy_sc(&regs->regs, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 err = restore_fp_registers(userspace_pid[0], fpregs);
Jeff Dikeba180fd2007-10-16 01:27:00 -070050 if (err < 0) {
51 printk(KERN_ERR "copy_sc_from_user_skas - PTRACE_SETFPREGS "
52 "failed, errno = %d\n", -err);
Jeff Dike5d864562007-05-06 14:51:24 -070053 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
55
Jeff Dike5d864562007-05-06 14:51:24 -070056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Jeff Dike77bf4402007-10-16 01:26:58 -070059static int copy_sc_to_user(struct sigcontext __user *to,
60 struct _fpstate __user *to_fp, struct pt_regs *regs,
61 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Jeff Dikeba180fd2007-10-16 01:27:00 -070063 struct sigcontext sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 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
Jeff Dike77bf4402007-10-16 01:26:58 -070068 sc.gs = REGS_GS(regs->regs.regs);
69 sc.fs = REGS_FS(regs->regs.regs);
70 sc.es = REGS_ES(regs->regs.regs);
71 sc.ds = REGS_DS(regs->regs.regs);
72 sc.edi = REGS_EDI(regs->regs.regs);
73 sc.esi = REGS_ESI(regs->regs.regs);
74 sc.ebp = REGS_EBP(regs->regs.regs);
Jeff Dike98c18232006-03-27 01:14:38 -080075 sc.esp = sp;
Jeff Dike77bf4402007-10-16 01:26:58 -070076 sc.ebx = REGS_EBX(regs->regs.regs);
77 sc.edx = REGS_EDX(regs->regs.regs);
78 sc.ecx = REGS_ECX(regs->regs.regs);
79 sc.eax = REGS_EAX(regs->regs.regs);
80 sc.eip = REGS_IP(regs->regs.regs);
81 sc.cs = REGS_CS(regs->regs.regs);
82 sc.eflags = REGS_EFLAGS(regs->regs.regs);
83 sc.esp_at_signal = regs->regs.regs[UESP];
84 sc.ss = regs->regs.regs[SS];
Jeff Dikeba180fd2007-10-16 01:27:00 -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);
Jeff Dikeba180fd2007-10-16 01:27:00 -070090 if (err < 0) {
91 printk(KERN_ERR "copy_sc_to_user_skas - PTRACE_GETFPREGS "
92 "failed, errno = %d\n", err);
Jeff Dike5d864562007-05-06 14:51:24 -070093 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Al Viro4d338e12006-03-31 02:30:15 -080095 to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 sc.fpstate = to_fp;
97
Jeff Dikeba180fd2007-10-16 01:27:00 -070098 if (err)
Jeff Dike5d864562007-05-06 14:51:24 -070099 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Jeff Dike5d864562007-05-06 14:51:24 -0700101 return copy_to_user(to, &sc, sizeof(sc)) ||
102 copy_to_user(to_fp, fpregs, sizeof(fpregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Jeff Dikeba180fd2007-10-16 01:27:00 -0700105static int copy_ucontext_to_user(struct ucontext __user *uc,
106 struct _fpstate __user *fp, sigset_t *set,
107 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int err = 0;
110
111 err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
112 err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
113 err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
Jeff Dike98c18232006-03-27 01:14:38 -0800114 err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
Jeff Dike5d864562007-05-06 14:51:24 -0700116 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119struct sigframe
120{
Al Viro4d338e12006-03-31 02:30:15 -0800121 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int sig;
123 struct sigcontext sc;
124 struct _fpstate fpstate;
125 unsigned long extramask[_NSIG_WORDS-1];
126 char retcode[8];
127};
128
129struct rt_sigframe
130{
Al Viro4d338e12006-03-31 02:30:15 -0800131 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int sig;
Al Viro4d338e12006-03-31 02:30:15 -0800133 struct siginfo __user *pinfo;
134 void __user *puc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct siginfo info;
136 struct ucontext uc;
137 struct _fpstate fpstate;
138 char retcode[8];
139};
140
141int setup_signal_stack_sc(unsigned long stack_top, int sig,
142 struct k_sigaction *ka, struct pt_regs *regs,
143 sigset_t *mask)
144{
145 struct sigframe __user *frame;
Al Viro4d338e12006-03-31 02:30:15 -0800146 void __user *restorer;
Jeff Dike98c18232006-03-27 01:14:38 -0800147 unsigned long save_sp = PT_REGS_SP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int err = 0;
149
Jeff Dike38966252007-01-30 14:36:17 -0800150 /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
151 stack_top = ((stack_top + 4) & -16UL) - 4;
Al Viro4d338e12006-03-31 02:30:15 -0800152 frame = (struct sigframe __user *) stack_top - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
154 return 1;
155
Al Viro4d338e12006-03-31 02:30:15 -0800156 restorer = frame->retcode;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700157 if (ka->sa.sa_flags & SA_RESTORER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 restorer = ka->sa.sa_restorer;
159
Jeff Dike98c18232006-03-27 01:14:38 -0800160 /* Update SP now because the page fault handler refuses to extend
161 * the stack if the faulting address is too far below the current
162 * SP, which frame now certainly is. If there's an error, the original
163 * value is restored on the way out.
164 * When writing the sigcontext to the stack, we have to write the
165 * original value, so that's passed to copy_sc_to_user, which does
166 * the right thing with it.
167 */
168 PT_REGS_SP(regs) = (unsigned long) frame;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 err |= __put_user(restorer, &frame->pretcode);
171 err |= __put_user(sig, &frame->sig);
Jeff Dike98c18232006-03-27 01:14:38 -0800172 err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 err |= __put_user(mask->sig[0], &frame->sc.oldmask);
174 if (_NSIG_WORDS > 1)
175 err |= __copy_to_user(&frame->extramask, &mask->sig[1],
176 sizeof(frame->extramask));
177
178 /*
179 * This is popl %eax ; movl $,%eax ; int $0x80
180 *
181 * WE DO NOT USE IT ANY MORE! It's only left here for historical
182 * reasons and because gdb uses it as a signature to notice
183 * signal handler stack frames.
184 */
185 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
186 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
187 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
188
Jeff Dikeba180fd2007-10-16 01:27:00 -0700189 if (err)
Jeff Dike98c18232006-03-27 01:14:38 -0800190 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 PT_REGS_SP(regs) = (unsigned long) frame;
193 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
194 PT_REGS_EAX(regs) = (unsigned long) sig;
195 PT_REGS_EDX(regs) = (unsigned long) 0;
196 PT_REGS_ECX(regs) = (unsigned long) 0;
197
198 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
199 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800200 return 0;
201
202err:
203 PT_REGS_SP(regs) = save_sp;
204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207int setup_signal_stack_si(unsigned long stack_top, int sig,
208 struct k_sigaction *ka, struct pt_regs *regs,
209 siginfo_t *info, sigset_t *mask)
210{
211 struct rt_sigframe __user *frame;
Al Viro4d338e12006-03-31 02:30:15 -0800212 void __user *restorer;
Jeff Dike98c18232006-03-27 01:14:38 -0800213 unsigned long save_sp = PT_REGS_SP(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int err = 0;
215
216 stack_top &= -8UL;
Al Viro4d338e12006-03-31 02:30:15 -0800217 frame = (struct rt_sigframe __user *) stack_top - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
219 return 1;
220
Al Viro4d338e12006-03-31 02:30:15 -0800221 restorer = frame->retcode;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700222 if (ka->sa.sa_flags & SA_RESTORER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 restorer = ka->sa.sa_restorer;
224
Jeff Dike98c18232006-03-27 01:14:38 -0800225 /* See comment above about why this is here */
226 PT_REGS_SP(regs) = (unsigned long) frame;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 err |= __put_user(restorer, &frame->pretcode);
229 err |= __put_user(sig, &frame->sig);
230 err |= __put_user(&frame->info, &frame->pinfo);
231 err |= __put_user(&frame->uc, &frame->puc);
232 err |= copy_siginfo_to_user(&frame->info, info);
233 err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
Jeff Dike98c18232006-03-27 01:14:38 -0800234 save_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * This is movl $,%eax ; int $0x80
238 *
239 * WE DO NOT USE IT ANY MORE! It's only left here for historical
240 * reasons and because gdb uses it as a signature to notice
241 * signal handler stack frames.
242 */
243 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
244 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
245 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
246
Jeff Dikeba180fd2007-10-16 01:27:00 -0700247 if (err)
Jeff Dike98c18232006-03-27 01:14:38 -0800248 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
251 PT_REGS_EAX(regs) = (unsigned long) sig;
252 PT_REGS_EDX(regs) = (unsigned long) &frame->info;
253 PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
254
255 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
256 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800257 return 0;
258
259err:
260 PT_REGS_SP(regs) = save_sp;
261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264long sys_sigreturn(struct pt_regs regs)
265{
266 unsigned long sp = PT_REGS_SP(&current->thread.regs);
Al Viro4d338e12006-03-31 02:30:15 -0800267 struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 sigset_t set;
269 struct sigcontext __user *sc = &frame->sc;
270 unsigned long __user *oldmask = &sc->oldmask;
271 unsigned long __user *extramask = frame->extramask;
272 int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
273
Jeff Dikeba180fd2007-10-16 01:27:00 -0700274 if (copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
275 copy_from_user(&set.sig[1], extramask, sig_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto segfault;
277
278 sigdelsetmask(&set, ~_BLOCKABLE);
279
280 spin_lock_irq(&current->sighand->siglock);
281 current->blocked = set;
282 recalc_sigpending();
283 spin_unlock_irq(&current->sighand->siglock);
284
Jeff Dikeba180fd2007-10-16 01:27:00 -0700285 if (copy_sc_from_user(&current->thread.regs, sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto segfault;
287
288 /* Avoid ERESTART handling */
289 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
Jeff Dike5d864562007-05-06 14:51:24 -0700290 return PT_REGS_SYSCALL_RET(&current->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 segfault:
293 force_sig(SIGSEGV, current);
294 return 0;
295}
296
297long sys_rt_sigreturn(struct pt_regs regs)
298{
Al Viro4d338e12006-03-31 02:30:15 -0800299 unsigned long sp = PT_REGS_SP(&current->thread.regs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700300 struct rt_sigframe __user *frame =
301 (struct rt_sigframe __user *) (sp - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 sigset_t set;
303 struct ucontext __user *uc = &frame->uc;
304 int sig_size = _NSIG_WORDS * sizeof(unsigned long);
305
Jeff Dikeba180fd2007-10-16 01:27:00 -0700306 if (copy_from_user(&set, &uc->uc_sigmask, sig_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 goto segfault;
308
309 sigdelsetmask(&set, ~_BLOCKABLE);
310
311 spin_lock_irq(&current->sighand->siglock);
312 current->blocked = set;
313 recalc_sigpending();
314 spin_unlock_irq(&current->sighand->siglock);
315
Jeff Dikeba180fd2007-10-16 01:27:00 -0700316 if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto segfault;
318
319 /* Avoid ERESTART handling */
320 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
Jeff Dike5d864562007-05-06 14:51:24 -0700321 return PT_REGS_SYSCALL_RET(&current->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 segfault:
324 force_sig(SIGSEGV, current);
325 return 0;
326}