Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[sfrench/cifs-2.6.git] / arch / um / sys-i386 / signal.c
1 /*
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"
13 #include "sigcontext.h"
14 #include "registers.h"
15 #include "mode.h"
16
17 #ifdef CONFIG_MODE_SKAS
18
19 #include "skas.h"
20
21 void 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
43 static int copy_sc_from_user_skas(struct pt_regs *regs,
44                                   struct sigcontext __user *from)
45 {
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)
53                 return err;
54
55         copy_sc(&regs->regs, &sc);
56
57         err = restore_fp_registers(userspace_pid[0], fpregs);
58         if(err < 0) {
59                 printk("copy_sc_from_user_skas - PTRACE_SETFPREGS failed, "
60                        "errno = %d\n", -err);
61                 return err;
62         }
63
64         return 0;
65 }
66
67 int copy_sc_to_user_skas(struct sigcontext __user *to, struct _fpstate __user *to_fp,
68                          struct pt_regs *regs, unsigned long sp)
69 {
70         struct sigcontext sc;
71         unsigned long fpregs[HOST_FP_SIZE];
72         struct faultinfo * fi = &current->thread.arch.faultinfo;
73         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);
82         sc.esp = sp;
83         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];
92         sc.cr2 = fi->cr2;
93         sc.err = fi->error_code;
94         sc.trapno = fi->trap_no;
95
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);
100                 return 1;
101         }
102         to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
103         sc.fpstate = to_fp;
104
105         if(err)
106                 return err;
107
108         return copy_to_user(to, &sc, sizeof(sc)) ||
109                copy_to_user(to_fp, fpregs, sizeof(fpregs));
110 }
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  */
123 int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext __user *from,
124                          int fpsize)
125 {
126         struct _fpstate *to_fp;
127         struct _fpstate __user *from_fp;
128         unsigned long sigs;
129         int err;
130
131         to_fp = to->fpstate;
132         sigs = to->oldmask;
133         err = copy_from_user(to, from, sizeof(*to));
134         from_fp = to->fpstate;
135         to->oldmask = sigs;
136         to->fpstate = to_fp;
137         if(to_fp != NULL)
138                 err |= copy_from_user(to_fp, from_fp, fpsize);
139         return err;
140 }
141
142 int copy_sc_to_user_tt(struct sigcontext __user *to, struct _fpstate __user *fp,
143                        struct sigcontext *from, int fpsize, unsigned long sp)
144 {
145         struct _fpstate __user *to_fp;
146         struct _fpstate *from_fp;
147         int err;
148
149         to_fp = (fp ? fp : (struct _fpstate __user *) (to + 1));
150         from_fp = from->fpstate;
151         err = copy_to_user(to, from, sizeof(*to));
152
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          */
157         err |= copy_to_user(&SC_SP(to), &sp, sizeof(sp));
158
159         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         }
163         return err;
164 }
165 #endif
166
167 static 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));
174         return ret;
175 }
176
177 static int copy_sc_to_user(struct sigcontext __user *to, struct _fpstate __user *fp,
178                            struct pt_regs *from, unsigned long sp)
179 {
180         return CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
181                                               sizeof(*fp), sp),
182                            copy_sc_to_user_skas(to, fp, from, sp));
183 }
184
185 static int copy_ucontext_to_user(struct ucontext __user *uc, struct _fpstate __user *fp,
186                                  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);
193         err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, sp);
194         err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
195         return err;
196 }
197
198 struct sigframe
199 {
200         char __user *pretcode;
201         int sig;
202         struct sigcontext sc;
203         struct _fpstate fpstate;
204         unsigned long extramask[_NSIG_WORDS-1];
205         char retcode[8];
206 };
207
208 struct rt_sigframe
209 {
210         char __user *pretcode;
211         int sig;
212         struct siginfo __user *pinfo;
213         void __user *puc;
214         struct siginfo info;
215         struct ucontext uc;
216         struct _fpstate fpstate;
217         char retcode[8];
218 };
219
220 int 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;
225         void __user *restorer;
226         unsigned long save_sp = PT_REGS_SP(regs);
227         int err = 0;
228
229         /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
230         stack_top = ((stack_top + 4) & -16UL) - 4;
231         frame = (struct sigframe __user *) stack_top - 1;
232         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
233                 return 1;
234
235         restorer = frame->retcode;
236         if(ka->sa.sa_flags & SA_RESTORER)
237                 restorer = ka->sa.sa_restorer;
238
239         /* 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
249         err |= __put_user(restorer, &frame->pretcode);
250         err |= __put_user(sig, &frame->sig);
251         err |= copy_sc_to_user(&frame->sc, NULL, regs, save_sp);
252         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)
269                 goto err;
270
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);
279         return 0;
280
281 err:
282         PT_REGS_SP(regs) = save_sp;
283         return err;
284 }
285
286 int 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;
291         void __user *restorer;
292         unsigned long save_sp = PT_REGS_SP(regs);
293         int err = 0;
294
295         stack_top &= -8UL;
296         frame = (struct rt_sigframe __user *) stack_top - 1;
297         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
298                 return 1;
299
300         restorer = frame->retcode;
301         if(ka->sa.sa_flags & SA_RESTORER)
302                 restorer = ka->sa.sa_restorer;
303
304         /* See comment above about why this is here */
305         PT_REGS_SP(regs) = (unsigned long) frame;
306
307         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,
313                                      save_sp);
314
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)
327                 goto err;
328
329         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);
336         return 0;
337
338 err:
339         PT_REGS_SP(regs) = save_sp;
340         return err;
341 }
342
343 long sys_sigreturn(struct pt_regs regs)
344 {
345         unsigned long sp = PT_REGS_SP(&current->thread.regs);
346         struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
347         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
353         if(copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
354            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;
369         return PT_REGS_SYSCALL_RET(&current->thread.regs);
370
371  segfault:
372         force_sig(SIGSEGV, current);
373         return 0;
374 }
375
376 long sys_rt_sigreturn(struct pt_regs regs)
377 {
378         unsigned long sp = PT_REGS_SP(&current->thread.regs);
379         struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (sp - 4);
380         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;
399         return PT_REGS_SYSCALL_RET(&current->thread.regs);
400
401  segfault:
402         force_sig(SIGSEGV, current);
403         return 0;
404 }