V4L/DVB (6058): ivtv: add support for highmem udma
[firewire-audio.git] / arch / um / sys-i386 / signal.c
blob1cbf95f6858ae2b0a97fcb7c87cd711338da1331
1 /*
2 * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
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"
17 #ifdef CONFIG_MODE_SKAS
19 #include "skas.h"
21 void copy_sc(union uml_pt_regs *regs, void *from)
23 struct sigcontext *sc = from;
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;
43 static int copy_sc_from_user_skas(struct pt_regs *regs,
44 struct sigcontext __user *from)
46 struct sigcontext sc;
47 unsigned long fpregs[HOST_FP_SIZE];
48 int err;
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;
55 copy_sc(&regs->regs, &sc);
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;
64 return 0;
67 int copy_sc_to_user_skas(struct sigcontext __user *to, struct _fpstate __user *to_fp,
68 struct pt_regs *regs, unsigned long sp)
70 struct sigcontext sc;
71 unsigned long fpregs[HOST_FP_SIZE];
72 struct faultinfo * fi = &current->thread.arch.faultinfo;
73 int err;
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;
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;
102 to_fp = (to_fp ? to_fp : (struct _fpstate __user *) (to + 1));
103 sc.fpstate = to_fp;
105 if(err)
106 return err;
108 return copy_to_user(to, &sc, sizeof(sc)) ||
109 copy_to_user(to_fp, fpregs, sizeof(fpregs));
111 #endif
113 #ifdef CONFIG_MODE_TT
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.
123 int copy_sc_from_user_tt(struct sigcontext *to, struct sigcontext __user *from,
124 int fpsize)
126 struct _fpstate *to_fp;
127 struct _fpstate __user *from_fp;
128 unsigned long sigs;
129 int err;
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;
142 int copy_sc_to_user_tt(struct sigcontext __user *to, struct _fpstate __user *fp,
143 struct sigcontext *from, int fpsize, unsigned long sp)
145 struct _fpstate __user *to_fp;
146 struct _fpstate *from_fp;
147 int err;
149 to_fp = (fp ? fp : (struct _fpstate __user *) (to + 1));
150 from_fp = from->fpstate;
151 err = copy_to_user(to, from, sizeof(*to));
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.
157 err |= copy_to_user(&SC_SP(to), &sp, sizeof(sp));
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);
163 return err;
165 #endif
167 static int copy_sc_from_user(struct pt_regs *to, void __user *from)
169 int ret;
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;
177 static int copy_sc_to_user(struct sigcontext __user *to, struct _fpstate __user *fp,
178 struct pt_regs *from, unsigned long sp)
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));
185 static int copy_ucontext_to_user(struct ucontext __user *uc, struct _fpstate __user *fp,
186 sigset_t *set, unsigned long sp)
188 int err = 0;
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;
198 struct sigframe
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];
208 struct rt_sigframe
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];
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)
224 struct sigframe __user *frame;
225 void __user *restorer;
226 unsigned long save_sp = PT_REGS_SP(regs);
227 int err = 0;
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;
235 restorer = frame->retcode;
236 if(ka->sa.sa_flags & SA_RESTORER)
237 restorer = ka->sa.sa_restorer;
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.
247 PT_REGS_SP(regs) = (unsigned long) frame;
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));
258 * This is popl %eax ; movl $,%eax ; int $0x80
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.
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));
268 if(err)
269 goto err;
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;
277 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
278 ptrace_notify(SIGTRAP);
279 return 0;
281 err:
282 PT_REGS_SP(regs) = save_sp;
283 return err;
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)
290 struct rt_sigframe __user *frame;
291 void __user *restorer;
292 unsigned long save_sp = PT_REGS_SP(regs);
293 int err = 0;
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;
300 restorer = frame->retcode;
301 if(ka->sa.sa_flags & SA_RESTORER)
302 restorer = ka->sa.sa_restorer;
304 /* See comment above about why this is here */
305 PT_REGS_SP(regs) = (unsigned long) frame;
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);
316 * This is movl $,%eax ; int $0x80
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.
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));
326 if(err)
327 goto err;
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;
334 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
335 ptrace_notify(SIGTRAP);
336 return 0;
338 err:
339 PT_REGS_SP(regs) = save_sp;
340 return err;
343 long sys_sigreturn(struct pt_regs regs)
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);
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;
357 sigdelsetmask(&set, ~_BLOCKABLE);
359 spin_lock_irq(&current->sighand->siglock);
360 current->blocked = set;
361 recalc_sigpending();
362 spin_unlock_irq(&current->sighand->siglock);
364 if(copy_sc_from_user(&current->thread.regs, sc))
365 goto segfault;
367 /* Avoid ERESTART handling */
368 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
369 return PT_REGS_SYSCALL_RET(&current->thread.regs);
371 segfault:
372 force_sig(SIGSEGV, current);
373 return 0;
376 long sys_rt_sigreturn(struct pt_regs regs)
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);
384 if(copy_from_user(&set, &uc->uc_sigmask, sig_size))
385 goto segfault;
387 sigdelsetmask(&set, ~_BLOCKABLE);
389 spin_lock_irq(&current->sighand->siglock);
390 current->blocked = set;
391 recalc_sigpending();
392 spin_unlock_irq(&current->sighand->siglock);
394 if(copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
395 goto segfault;
397 /* Avoid ERESTART handling */
398 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
399 return PT_REGS_SYSCALL_RET(&current->thread.regs);
401 segfault:
402 force_sig(SIGSEGV, current);
403 return 0;