i387: Split up <asm/i387.h> into exported and internal interfaces
[linux-3.10.git] / arch / x86 / ia32 / ia32_signal.c
1 /*
2  *  linux/arch/x86_64/ia32/ia32_signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
7  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
8  *  2000-12-*   x86-64 compatibility mode signal handling by Andi Kleen
9  */
10
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/compat.h>
23 #include <linux/binfmts.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/i387.h>
27 #include <asm/fpu-internal.h>
28 #include <asm/ptrace.h>
29 #include <asm/ia32_unistd.h>
30 #include <asm/user32.h>
31 #include <asm/sigcontext32.h>
32 #include <asm/proto.h>
33 #include <asm/vdso.h>
34 #include <asm/sigframe.h>
35 #include <asm/sys_ia32.h>
36
37 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38
39 #define FIX_EFLAGS      (X86_EFLAGS_AC | X86_EFLAGS_OF | \
40                          X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
41                          X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
42                          X86_EFLAGS_CF)
43
44 void signal_fault(struct pt_regs *regs, void __user *frame, char *where);
45
46 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
47 {
48         int err = 0;
49
50         if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
51                 return -EFAULT;
52
53         put_user_try {
54                 /* If you change siginfo_t structure, please make sure that
55                    this code is fixed accordingly.
56                    It should never copy any pad contained in the structure
57                    to avoid security leaks, but must copy the generic
58                    3 ints plus the relevant union member.  */
59                 put_user_ex(from->si_signo, &to->si_signo);
60                 put_user_ex(from->si_errno, &to->si_errno);
61                 put_user_ex((short)from->si_code, &to->si_code);
62
63                 if (from->si_code < 0) {
64                         put_user_ex(from->si_pid, &to->si_pid);
65                         put_user_ex(from->si_uid, &to->si_uid);
66                         put_user_ex(ptr_to_compat(from->si_ptr), &to->si_ptr);
67                 } else {
68                         /*
69                          * First 32bits of unions are always present:
70                          * si_pid === si_band === si_tid === si_addr(LS half)
71                          */
72                         put_user_ex(from->_sifields._pad[0],
73                                           &to->_sifields._pad[0]);
74                         switch (from->si_code >> 16) {
75                         case __SI_FAULT >> 16:
76                                 break;
77                         case __SI_CHLD >> 16:
78                                 put_user_ex(from->si_utime, &to->si_utime);
79                                 put_user_ex(from->si_stime, &to->si_stime);
80                                 put_user_ex(from->si_status, &to->si_status);
81                                 /* FALL THROUGH */
82                         default:
83                         case __SI_KILL >> 16:
84                                 put_user_ex(from->si_uid, &to->si_uid);
85                                 break;
86                         case __SI_POLL >> 16:
87                                 put_user_ex(from->si_fd, &to->si_fd);
88                                 break;
89                         case __SI_TIMER >> 16:
90                                 put_user_ex(from->si_overrun, &to->si_overrun);
91                                 put_user_ex(ptr_to_compat(from->si_ptr),
92                                             &to->si_ptr);
93                                 break;
94                                  /* This is not generated by the kernel as of now.  */
95                         case __SI_RT >> 16:
96                         case __SI_MESGQ >> 16:
97                                 put_user_ex(from->si_uid, &to->si_uid);
98                                 put_user_ex(from->si_int, &to->si_int);
99                                 break;
100                         }
101                 }
102         } put_user_catch(err);
103
104         return err;
105 }
106
107 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
108 {
109         int err = 0;
110         u32 ptr32;
111
112         if (!access_ok(VERIFY_READ, from, sizeof(compat_siginfo_t)))
113                 return -EFAULT;
114
115         get_user_try {
116                 get_user_ex(to->si_signo, &from->si_signo);
117                 get_user_ex(to->si_errno, &from->si_errno);
118                 get_user_ex(to->si_code, &from->si_code);
119
120                 get_user_ex(to->si_pid, &from->si_pid);
121                 get_user_ex(to->si_uid, &from->si_uid);
122                 get_user_ex(ptr32, &from->si_ptr);
123                 to->si_ptr = compat_ptr(ptr32);
124         } get_user_catch(err);
125
126         return err;
127 }
128
129 asmlinkage long sys32_sigsuspend(int history0, int history1, old_sigset_t mask)
130 {
131         sigset_t blocked;
132
133         current->saved_sigmask = current->blocked;
134
135         mask &= _BLOCKABLE;
136         siginitset(&blocked, mask);
137         set_current_blocked(&blocked);
138
139         current->state = TASK_INTERRUPTIBLE;
140         schedule();
141
142         set_restore_sigmask();
143         return -ERESTARTNOHAND;
144 }
145
146 asmlinkage long sys32_sigaltstack(const stack_ia32_t __user *uss_ptr,
147                                   stack_ia32_t __user *uoss_ptr,
148                                   struct pt_regs *regs)
149 {
150         stack_t uss, uoss;
151         int ret, err = 0;
152         mm_segment_t seg;
153
154         if (uss_ptr) {
155                 u32 ptr;
156
157                 memset(&uss, 0, sizeof(stack_t));
158                 if (!access_ok(VERIFY_READ, uss_ptr, sizeof(stack_ia32_t)))
159                         return -EFAULT;
160
161                 get_user_try {
162                         get_user_ex(ptr, &uss_ptr->ss_sp);
163                         get_user_ex(uss.ss_flags, &uss_ptr->ss_flags);
164                         get_user_ex(uss.ss_size, &uss_ptr->ss_size);
165                 } get_user_catch(err);
166
167                 if (err)
168                         return -EFAULT;
169                 uss.ss_sp = compat_ptr(ptr);
170         }
171         seg = get_fs();
172         set_fs(KERNEL_DS);
173         ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss, regs->sp);
174         set_fs(seg);
175         if (ret >= 0 && uoss_ptr)  {
176                 if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(stack_ia32_t)))
177                         return -EFAULT;
178
179                 put_user_try {
180                         put_user_ex(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp);
181                         put_user_ex(uoss.ss_flags, &uoss_ptr->ss_flags);
182                         put_user_ex(uoss.ss_size, &uoss_ptr->ss_size);
183                 } put_user_catch(err);
184
185                 if (err)
186                         ret = -EFAULT;
187         }
188         return ret;
189 }
190
191 /*
192  * Do a signal return; undo the signal stack.
193  */
194 #define loadsegment_gs(v)       load_gs_index(v)
195 #define loadsegment_fs(v)       loadsegment(fs, v)
196 #define loadsegment_ds(v)       loadsegment(ds, v)
197 #define loadsegment_es(v)       loadsegment(es, v)
198
199 #define get_user_seg(seg)       ({ unsigned int v; savesegment(seg, v); v; })
200 #define set_user_seg(seg, v)    loadsegment_##seg(v)
201
202 #define COPY(x)                 {               \
203         get_user_ex(regs->x, &sc->x);           \
204 }
205
206 #define GET_SEG(seg)            ({                      \
207         unsigned short tmp;                             \
208         get_user_ex(tmp, &sc->seg);                     \
209         tmp;                                            \
210 })
211
212 #define COPY_SEG_CPL3(seg)      do {                    \
213         regs->seg = GET_SEG(seg) | 3;                   \
214 } while (0)
215
216 #define RELOAD_SEG(seg)         {               \
217         unsigned int pre = GET_SEG(seg);        \
218         unsigned int cur = get_user_seg(seg);   \
219         pre |= 3;                               \
220         if (pre != cur)                         \
221                 set_user_seg(seg, pre);         \
222 }
223
224 static int ia32_restore_sigcontext(struct pt_regs *regs,
225                                    struct sigcontext_ia32 __user *sc,
226                                    unsigned int *pax)
227 {
228         unsigned int tmpflags, err = 0;
229         void __user *buf;
230         u32 tmp;
231
232         /* Always make any pending restarted system calls return -EINTR */
233         current_thread_info()->restart_block.fn = do_no_restart_syscall;
234
235         get_user_try {
236                 /*
237                  * Reload fs and gs if they have changed in the signal
238                  * handler.  This does not handle long fs/gs base changes in
239                  * the handler, but does not clobber them at least in the
240                  * normal case.
241                  */
242                 RELOAD_SEG(gs);
243                 RELOAD_SEG(fs);
244                 RELOAD_SEG(ds);
245                 RELOAD_SEG(es);
246
247                 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
248                 COPY(dx); COPY(cx); COPY(ip);
249                 /* Don't touch extended registers */
250
251                 COPY_SEG_CPL3(cs);
252                 COPY_SEG_CPL3(ss);
253
254                 get_user_ex(tmpflags, &sc->flags);
255                 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
256                 /* disable syscall checks */
257                 regs->orig_ax = -1;
258
259                 get_user_ex(tmp, &sc->fpstate);
260                 buf = compat_ptr(tmp);
261                 err |= restore_i387_xstate_ia32(buf);
262
263                 get_user_ex(*pax, &sc->ax);
264         } get_user_catch(err);
265
266         return err;
267 }
268
269 asmlinkage long sys32_sigreturn(struct pt_regs *regs)
270 {
271         struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
272         sigset_t set;
273         unsigned int ax;
274
275         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
276                 goto badframe;
277         if (__get_user(set.sig[0], &frame->sc.oldmask)
278             || (_COMPAT_NSIG_WORDS > 1
279                 && __copy_from_user((((char *) &set.sig) + 4),
280                                     &frame->extramask,
281                                     sizeof(frame->extramask))))
282                 goto badframe;
283
284         sigdelsetmask(&set, ~_BLOCKABLE);
285         set_current_blocked(&set);
286
287         if (ia32_restore_sigcontext(regs, &frame->sc, &ax))
288                 goto badframe;
289         return ax;
290
291 badframe:
292         signal_fault(regs, frame, "32bit sigreturn");
293         return 0;
294 }
295
296 asmlinkage long sys32_rt_sigreturn(struct pt_regs *regs)
297 {
298         struct rt_sigframe_ia32 __user *frame;
299         sigset_t set;
300         unsigned int ax;
301         struct pt_regs tregs;
302
303         frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
304
305         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
306                 goto badframe;
307         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
308                 goto badframe;
309
310         sigdelsetmask(&set, ~_BLOCKABLE);
311         set_current_blocked(&set);
312
313         if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
314                 goto badframe;
315
316         tregs = *regs;
317         if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
318                 goto badframe;
319
320         return ax;
321
322 badframe:
323         signal_fault(regs, frame, "32bit rt sigreturn");
324         return 0;
325 }
326
327 /*
328  * Set up a signal frame.
329  */
330
331 static int ia32_setup_sigcontext(struct sigcontext_ia32 __user *sc,
332                                  void __user *fpstate,
333                                  struct pt_regs *regs, unsigned int mask)
334 {
335         int err = 0;
336
337         put_user_try {
338                 put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs);
339                 put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs);
340                 put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds);
341                 put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es);
342
343                 put_user_ex(regs->di, &sc->di);
344                 put_user_ex(regs->si, &sc->si);
345                 put_user_ex(regs->bp, &sc->bp);
346                 put_user_ex(regs->sp, &sc->sp);
347                 put_user_ex(regs->bx, &sc->bx);
348                 put_user_ex(regs->dx, &sc->dx);
349                 put_user_ex(regs->cx, &sc->cx);
350                 put_user_ex(regs->ax, &sc->ax);
351                 put_user_ex(current->thread.trap_no, &sc->trapno);
352                 put_user_ex(current->thread.error_code, &sc->err);
353                 put_user_ex(regs->ip, &sc->ip);
354                 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
355                 put_user_ex(regs->flags, &sc->flags);
356                 put_user_ex(regs->sp, &sc->sp_at_signal);
357                 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
358
359                 put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
360
361                 /* non-iBCS2 extensions.. */
362                 put_user_ex(mask, &sc->oldmask);
363                 put_user_ex(current->thread.cr2, &sc->cr2);
364         } put_user_catch(err);
365
366         return err;
367 }
368
369 /*
370  * Determine which stack to use..
371  */
372 static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
373                                  size_t frame_size,
374                                  void **fpstate)
375 {
376         unsigned long sp;
377
378         /* Default to using normal stack */
379         sp = regs->sp;
380
381         /* This is the X/Open sanctioned signal stack switching.  */
382         if (ka->sa.sa_flags & SA_ONSTACK) {
383                 if (sas_ss_flags(sp) == 0)
384                         sp = current->sas_ss_sp + current->sas_ss_size;
385         }
386
387         /* This is the legacy signal stack switching. */
388         else if ((regs->ss & 0xffff) != __USER32_DS &&
389                 !(ka->sa.sa_flags & SA_RESTORER) &&
390                  ka->sa.sa_restorer)
391                 sp = (unsigned long) ka->sa.sa_restorer;
392
393         if (used_math()) {
394                 sp = sp - sig_xstate_ia32_size;
395                 *fpstate = (struct _fpstate_ia32 *) sp;
396                 if (save_i387_xstate_ia32(*fpstate) < 0)
397                         return (void __user *) -1L;
398         }
399
400         sp -= frame_size;
401         /* Align the stack pointer according to the i386 ABI,
402          * i.e. so that on function entry ((sp + 4) & 15) == 0. */
403         sp = ((sp + 4) & -16ul) - 4;
404         return (void __user *) sp;
405 }
406
407 int ia32_setup_frame(int sig, struct k_sigaction *ka,
408                      compat_sigset_t *set, struct pt_regs *regs)
409 {
410         struct sigframe_ia32 __user *frame;
411         void __user *restorer;
412         int err = 0;
413         void __user *fpstate = NULL;
414
415         /* copy_to_user optimizes that into a single 8 byte store */
416         static const struct {
417                 u16 poplmovl;
418                 u32 val;
419                 u16 int80;
420         } __attribute__((packed)) code = {
421                 0xb858,          /* popl %eax ; movl $...,%eax */
422                 __NR_ia32_sigreturn,
423                 0x80cd,         /* int $0x80 */
424         };
425
426         frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
427
428         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
429                 return -EFAULT;
430
431         if (__put_user(sig, &frame->sig))
432                 return -EFAULT;
433
434         if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
435                 return -EFAULT;
436
437         if (_COMPAT_NSIG_WORDS > 1) {
438                 if (__copy_to_user(frame->extramask, &set->sig[1],
439                                    sizeof(frame->extramask)))
440                         return -EFAULT;
441         }
442
443         if (ka->sa.sa_flags & SA_RESTORER) {
444                 restorer = ka->sa.sa_restorer;
445         } else {
446                 /* Return stub is in 32bit vsyscall page */
447                 if (current->mm->context.vdso)
448                         restorer = VDSO32_SYMBOL(current->mm->context.vdso,
449                                                  sigreturn);
450                 else
451                         restorer = &frame->retcode;
452         }
453
454         put_user_try {
455                 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
456
457                 /*
458                  * These are actually not used anymore, but left because some
459                  * gdb versions depend on them as a marker.
460                  */
461                 put_user_ex(*((u64 *)&code), (u64 *)frame->retcode);
462         } put_user_catch(err);
463
464         if (err)
465                 return -EFAULT;
466
467         /* Set up registers for signal handler */
468         regs->sp = (unsigned long) frame;
469         regs->ip = (unsigned long) ka->sa.sa_handler;
470
471         /* Make -mregparm=3 work */
472         regs->ax = sig;
473         regs->dx = 0;
474         regs->cx = 0;
475
476         loadsegment(ds, __USER32_DS);
477         loadsegment(es, __USER32_DS);
478
479         regs->cs = __USER32_CS;
480         regs->ss = __USER32_DS;
481
482         return 0;
483 }
484
485 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
486                         compat_sigset_t *set, struct pt_regs *regs)
487 {
488         struct rt_sigframe_ia32 __user *frame;
489         void __user *restorer;
490         int err = 0;
491         void __user *fpstate = NULL;
492
493         /* __copy_to_user optimizes that into a single 8 byte store */
494         static const struct {
495                 u8 movl;
496                 u32 val;
497                 u16 int80;
498                 u8  pad;
499         } __attribute__((packed)) code = {
500                 0xb8,
501                 __NR_ia32_rt_sigreturn,
502                 0x80cd,
503                 0,
504         };
505
506         frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
507
508         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
509                 return -EFAULT;
510
511         put_user_try {
512                 put_user_ex(sig, &frame->sig);
513                 put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
514                 put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
515                 err |= copy_siginfo_to_user32(&frame->info, info);
516
517                 /* Create the ucontext.  */
518                 if (cpu_has_xsave)
519                         put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
520                 else
521                         put_user_ex(0, &frame->uc.uc_flags);
522                 put_user_ex(0, &frame->uc.uc_link);
523                 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
524                 put_user_ex(sas_ss_flags(regs->sp),
525                             &frame->uc.uc_stack.ss_flags);
526                 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
527                 err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
528                                              regs, set->sig[0]);
529                 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
530
531                 if (ka->sa.sa_flags & SA_RESTORER)
532                         restorer = ka->sa.sa_restorer;
533                 else
534                         restorer = VDSO32_SYMBOL(current->mm->context.vdso,
535                                                  rt_sigreturn);
536                 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
537
538                 /*
539                  * Not actually used anymore, but left because some gdb
540                  * versions need it.
541                  */
542                 put_user_ex(*((u64 *)&code), (u64 *)frame->retcode);
543         } put_user_catch(err);
544
545         if (err)
546                 return -EFAULT;
547
548         /* Set up registers for signal handler */
549         regs->sp = (unsigned long) frame;
550         regs->ip = (unsigned long) ka->sa.sa_handler;
551
552         /* Make -mregparm=3 work */
553         regs->ax = sig;
554         regs->dx = (unsigned long) &frame->info;
555         regs->cx = (unsigned long) &frame->uc;
556
557         loadsegment(ds, __USER32_DS);
558         loadsegment(es, __USER32_DS);
559
560         regs->cs = __USER32_CS;
561         regs->ss = __USER32_DS;
562
563         return 0;
564 }