linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-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 static 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;
49
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
60 int copy_sc_to_user_skas(struct sigcontext *to, struct _fpstate *to_fp,
61                          struct pt_regs *regs)
62 {
63         struct sigcontext sc;
64         unsigned long fpregs[HOST_FP_SIZE];
65         struct faultinfo * fi = &current->thread.arch.faultinfo;
66         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);
75         sc.esp = REGS_SP(regs->regs.skas.regs);
76         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];
85         sc.cr2 = fi->cr2;
86         sc.err = fi->error_code;
87         sc.trapno = fi->trap_no;
88
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  */
116 int 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;
124         sigs = to->oldmask;
125         err = copy_from_user(to, from, sizeof(*to));
126         from_fp = to->fpstate;
127         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
134 int copy_sc_to_user_tt(struct sigcontext *to, struct _fpstate *fp,
135                        struct sigcontext *from, int fpsize)
136 {
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));
143         if(from_fp != NULL){
144                 err |= copy_to_user(&to->fpstate, &to_fp, sizeof(to->fpstate));
145                 err |= copy_to_user(to_fp, from_fp, fpsize);
146         }
147         return(err);
148 }
149 #endif
150
151 static int copy_sc_from_user(struct pt_regs *to, void __user *from)
152 {
153         int ret;
154
155         ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from,
156                                                sizeof(struct _fpstate)),
157                           copy_sc_from_user_skas(to, from));
158         return(ret);
159 }
160
161 static int copy_sc_to_user(struct sigcontext *to, struct _fpstate *fp,
162                            struct pt_regs *from)
163 {
164         return(CHOOSE_MODE(copy_sc_to_user_tt(to, fp, UPT_SC(&from->regs),
165                                               sizeof(*fp)),
166                            copy_sc_to_user_skas(to, fp, from)));
167 }
168
169 static int copy_ucontext_to_user(struct ucontext *uc, struct _fpstate *fp,
170                                  sigset_t *set, unsigned long sp)
171 {
172         int err = 0;
173
174         err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
175         err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
176         err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
177         err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs);
178         err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
179         return(err);
180 }
181
182 struct sigframe
183 {
184         char *pretcode;
185         int sig;
186         struct sigcontext sc;
187         struct _fpstate fpstate;
188         unsigned long extramask[_NSIG_WORDS-1];
189         char retcode[8];
190 };
191
192 struct rt_sigframe
193 {
194         char *pretcode;
195         int sig;
196         struct siginfo *pinfo;
197         void *puc;
198         struct siginfo info;
199         struct ucontext uc;
200         struct _fpstate fpstate;
201         char retcode[8];
202 };
203
204 int setup_signal_stack_sc(unsigned long stack_top, int sig,
205                           struct k_sigaction *ka, struct pt_regs *regs,
206                           sigset_t *mask)
207 {
208         struct sigframe __user *frame;
209         void *restorer;
210         int err = 0;
211
212         stack_top &= -8UL;
213         frame = (struct sigframe *) stack_top - 1;
214         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
215                 return 1;
216
217         restorer = (void *) frame->retcode;
218         if(ka->sa.sa_flags & SA_RESTORER)
219                 restorer = ka->sa.sa_restorer;
220
221         err |= __put_user(restorer, &frame->pretcode);
222         err |= __put_user(sig, &frame->sig);
223         err |= copy_sc_to_user(&frame->sc, NULL, regs);
224         err |= __put_user(mask->sig[0], &frame->sc.oldmask);
225         if (_NSIG_WORDS > 1)
226                 err |= __copy_to_user(&frame->extramask, &mask->sig[1],
227                                       sizeof(frame->extramask));
228
229         /*
230          * This is popl %eax ; movl $,%eax ; int $0x80
231          *
232          * WE DO NOT USE IT ANY MORE! It's only left here for historical
233          * reasons and because gdb uses it as a signature to notice
234          * signal handler stack frames.
235          */
236         err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
237         err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
238         err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
239
240         if(err)
241                 return(err);
242
243         PT_REGS_SP(regs) = (unsigned long) frame;
244         PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
245         PT_REGS_EAX(regs) = (unsigned long) sig;
246         PT_REGS_EDX(regs) = (unsigned long) 0;
247         PT_REGS_ECX(regs) = (unsigned long) 0;
248
249         if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
250                 ptrace_notify(SIGTRAP);
251         return(0);
252 }
253
254 int setup_signal_stack_si(unsigned long stack_top, int sig,
255                           struct k_sigaction *ka, struct pt_regs *regs,
256                           siginfo_t *info, sigset_t *mask)
257 {
258         struct rt_sigframe __user *frame;
259         void *restorer;
260         int err = 0;
261
262         stack_top &= -8UL;
263         frame = (struct rt_sigframe *) stack_top - 1;
264         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
265                 return 1;
266
267         restorer = (void *) frame->retcode;
268         if(ka->sa.sa_flags & SA_RESTORER)
269                 restorer = ka->sa.sa_restorer;
270
271         err |= __put_user(restorer, &frame->pretcode);
272         err |= __put_user(sig, &frame->sig);
273         err |= __put_user(&frame->info, &frame->pinfo);
274         err |= __put_user(&frame->uc, &frame->puc);
275         err |= copy_siginfo_to_user(&frame->info, info);
276         err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
277                                      PT_REGS_SP(regs));
278
279         /*
280          * This is movl $,%eax ; int $0x80
281          *
282          * WE DO NOT USE IT ANY MORE! It's only left here for historical
283          * reasons and because gdb uses it as a signature to notice
284          * signal handler stack frames.
285          */
286         err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
287         err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
288         err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
289
290         if(err)
291                 return(err);
292
293         PT_REGS_SP(regs) = (unsigned long) frame;
294         PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
295         PT_REGS_EAX(regs) = (unsigned long) sig;
296         PT_REGS_EDX(regs) = (unsigned long) &frame->info;
297         PT_REGS_ECX(regs) = (unsigned long) &frame->uc;
298
299         if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
300                 ptrace_notify(SIGTRAP);
301         return(0);
302 }
303
304 long sys_sigreturn(struct pt_regs regs)
305 {
306         unsigned long sp = PT_REGS_SP(&current->thread.regs);
307         struct sigframe __user *frame = (struct sigframe *)(sp - 8);
308         sigset_t set;
309         struct sigcontext __user *sc = &frame->sc;
310         unsigned long __user *oldmask = &sc->oldmask;
311         unsigned long __user *extramask = frame->extramask;
312         int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
313
314         if(copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
315            copy_from_user(&set.sig[1], extramask, sig_size))
316                 goto segfault;
317
318         sigdelsetmask(&set, ~_BLOCKABLE);
319
320         spin_lock_irq(&current->sighand->siglock);
321         current->blocked = set;
322         recalc_sigpending();
323         spin_unlock_irq(&current->sighand->siglock);
324
325         if(copy_sc_from_user(&current->thread.regs, sc))
326                 goto segfault;
327
328         /* Avoid ERESTART handling */
329         PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
330         return(PT_REGS_SYSCALL_RET(&current->thread.regs));
331
332  segfault:
333         force_sig(SIGSEGV, current);
334         return 0;
335 }
336
337 long sys_rt_sigreturn(struct pt_regs regs)
338 {
339         unsigned long __user sp = PT_REGS_SP(&current->thread.regs);
340         struct rt_sigframe __user *frame = (struct rt_sigframe *) (sp - 4);
341         sigset_t set;
342         struct ucontext __user *uc = &frame->uc;
343         int sig_size = _NSIG_WORDS * sizeof(unsigned long);
344
345         if(copy_from_user(&set, &uc->uc_sigmask, sig_size))
346                 goto segfault;
347
348         sigdelsetmask(&set, ~_BLOCKABLE);
349
350         spin_lock_irq(&current->sighand->siglock);
351         current->blocked = set;
352         recalc_sigpending();
353         spin_unlock_irq(&current->sighand->siglock);
354
355         if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
356                 goto segfault;
357
358         /* Avoid ERESTART handling */
359         PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
360         return(PT_REGS_SYSCALL_RET(&current->thread.regs));
361
362  segfault:
363         force_sig(SIGSEGV, current);
364         return 0;
365 }
366
367 /*
368  * Overrides for Emacs so that we follow Linus's tabbing style.
369  * Emacs will notice this stuff at the end of the file and automatically
370  * adjust the settings for this buffer only.  This must remain at the end
371  * of the file.
372  * ---------------------------------------------------------------------------
373  * Local variables:
374  * c-file-style: "linux"
375  * End:
376  */