arm64: 32-bit (compat) applications support
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / signal32.c
1 /*
2  * Based on arch/arm/kernel/signal.c
3  *
4  * Copyright (C) 1995-2009 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  * Modified by Will Deacon <will.deacon@arm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #define __SYSCALL_COMPAT
22
23 #include <linux/compat.h>
24 #include <linux/signal.h>
25 #include <linux/syscalls.h>
26 #include <linux/ratelimit.h>
27
28 #include <asm/fpsimd.h>
29 #include <asm/signal32.h>
30 #include <asm/uaccess.h>
31 #include <asm/unistd.h>
32
33 typedef struct compat_siginfo {
34         int si_signo;
35         int si_errno;
36         int si_code;
37
38         union {
39                 /* The padding is the same size as AArch64. */
40                 int _pad[SI_PAD_SIZE];
41
42                 /* kill() */
43                 struct {
44                         compat_pid_t _pid;      /* sender's pid */
45                         __compat_uid32_t _uid;  /* sender's uid */
46                 } _kill;
47
48                 /* POSIX.1b timers */
49                 struct {
50                         compat_timer_t _tid;    /* timer id */
51                         int _overrun;           /* overrun count */
52                         compat_sigval_t _sigval;        /* same as below */
53                         int _sys_private;       /* not to be passed to user */
54                 } _timer;
55
56                 /* POSIX.1b signals */
57                 struct {
58                         compat_pid_t _pid;      /* sender's pid */
59                         __compat_uid32_t _uid;  /* sender's uid */
60                         compat_sigval_t _sigval;
61                 } _rt;
62
63                 /* SIGCHLD */
64                 struct {
65                         compat_pid_t _pid;      /* which child */
66                         __compat_uid32_t _uid;  /* sender's uid */
67                         int _status;            /* exit code */
68                         compat_clock_t _utime;
69                         compat_clock_t _stime;
70                 } _sigchld;
71
72                 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
73                 struct {
74                         compat_uptr_t _addr; /* faulting insn/memory ref. */
75                         short _addr_lsb; /* LSB of the reported address */
76                 } _sigfault;
77
78                 /* SIGPOLL */
79                 struct {
80                         compat_long_t _band;    /* POLL_IN, POLL_OUT, POLL_MSG */
81                         int _fd;
82                 } _sigpoll;
83         } _sifields;
84 } compat_siginfo_t;
85
86 struct compat_sigaction {
87         compat_uptr_t                   sa_handler;
88         compat_ulong_t                  sa_flags;
89         compat_uptr_t                   sa_restorer;
90         compat_sigset_t                 sa_mask;
91 };
92
93 struct compat_old_sigaction {
94         compat_uptr_t                   sa_handler;
95         compat_old_sigset_t             sa_mask;
96         compat_ulong_t                  sa_flags;
97         compat_uptr_t                   sa_restorer;
98 };
99
100 typedef struct compat_sigaltstack {
101         compat_uptr_t                   ss_sp;
102         int                             ss_flags;
103         compat_size_t                   ss_size;
104 } compat_stack_t;
105
106 struct compat_sigcontext {
107         /* We always set these two fields to 0 */
108         compat_ulong_t                  trap_no;
109         compat_ulong_t                  error_code;
110
111         compat_ulong_t                  oldmask;
112         compat_ulong_t                  arm_r0;
113         compat_ulong_t                  arm_r1;
114         compat_ulong_t                  arm_r2;
115         compat_ulong_t                  arm_r3;
116         compat_ulong_t                  arm_r4;
117         compat_ulong_t                  arm_r5;
118         compat_ulong_t                  arm_r6;
119         compat_ulong_t                  arm_r7;
120         compat_ulong_t                  arm_r8;
121         compat_ulong_t                  arm_r9;
122         compat_ulong_t                  arm_r10;
123         compat_ulong_t                  arm_fp;
124         compat_ulong_t                  arm_ip;
125         compat_ulong_t                  arm_sp;
126         compat_ulong_t                  arm_lr;
127         compat_ulong_t                  arm_pc;
128         compat_ulong_t                  arm_cpsr;
129         compat_ulong_t                  fault_address;
130 };
131
132 struct compat_ucontext {
133         compat_ulong_t                  uc_flags;
134         struct compat_ucontext          *uc_link;
135         compat_stack_t                  uc_stack;
136         struct compat_sigcontext        uc_mcontext;
137         compat_sigset_t                 uc_sigmask;
138         int             __unused[32 - (sizeof (compat_sigset_t) / sizeof (int))];
139         compat_ulong_t  uc_regspace[128] __attribute__((__aligned__(8)));
140 };
141
142 struct compat_vfp_sigframe {
143         compat_ulong_t  magic;
144         compat_ulong_t  size;
145         struct compat_user_vfp {
146                 compat_u64      fpregs[32];
147                 compat_ulong_t  fpscr;
148         } ufp;
149         struct compat_user_vfp_exc {
150                 compat_ulong_t  fpexc;
151                 compat_ulong_t  fpinst;
152                 compat_ulong_t  fpinst2;
153         } ufp_exc;
154 } __attribute__((__aligned__(8)));
155
156 #define VFP_MAGIC               0x56465001
157 #define VFP_STORAGE_SIZE        sizeof(struct compat_vfp_sigframe)
158
159 struct compat_aux_sigframe {
160         struct compat_vfp_sigframe      vfp;
161
162         /* Something that isn't a valid magic number for any coprocessor.  */
163         unsigned long                   end_magic;
164 } __attribute__((__aligned__(8)));
165
166 struct compat_sigframe {
167         struct compat_ucontext  uc;
168         compat_ulong_t          retcode[2];
169 };
170
171 struct compat_rt_sigframe {
172         struct compat_siginfo info;
173         struct compat_sigframe sig;
174 };
175
176 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
177
178 /*
179  * For ARM syscalls, the syscall number has to be loaded into r7.
180  * We do not support an OABI userspace.
181  */
182 #define MOV_R7_NR_SIGRETURN     (0xe3a07000 | __NR_sigreturn)
183 #define SVC_SYS_SIGRETURN       (0xef000000 | __NR_sigreturn)
184 #define MOV_R7_NR_RT_SIGRETURN  (0xe3a07000 | __NR_rt_sigreturn)
185 #define SVC_SYS_RT_SIGRETURN    (0xef000000 | __NR_rt_sigreturn)
186
187 /*
188  * For Thumb syscalls, we also pass the syscall number via r7. We therefore
189  * need two 16-bit instructions.
190  */
191 #define SVC_THUMB_SIGRETURN     (((0xdf00 | __NR_sigreturn) << 16) | \
192                                    0x2700 | __NR_sigreturn)
193 #define SVC_THUMB_RT_SIGRETURN  (((0xdf00 | __NR_rt_sigreturn) << 16) | \
194                                    0x2700 | __NR_rt_sigreturn)
195
196 const compat_ulong_t aarch32_sigret_code[6] = {
197         /*
198          * AArch32 sigreturn code.
199          * We don't construct an OABI SWI - instead we just set the imm24 field
200          * to the EABI syscall number so that we create a sane disassembly.
201          */
202         MOV_R7_NR_SIGRETURN,    SVC_SYS_SIGRETURN,    SVC_THUMB_SIGRETURN,
203         MOV_R7_NR_RT_SIGRETURN, SVC_SYS_RT_SIGRETURN, SVC_THUMB_RT_SIGRETURN,
204 };
205
206 static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
207 {
208         compat_sigset_t cset;
209
210         cset.sig[0] = set->sig[0] & 0xffffffffull;
211         cset.sig[1] = set->sig[0] >> 32;
212
213         return copy_to_user(uset, &cset, sizeof(*uset));
214 }
215
216 static inline int get_sigset_t(sigset_t *set,
217                                const compat_sigset_t __user *uset)
218 {
219         compat_sigset_t s32;
220
221         if (copy_from_user(&s32, uset, sizeof(*uset)))
222                 return -EFAULT;
223
224         set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
225         return 0;
226 }
227
228 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
229 {
230         int err;
231
232         if (!access_ok(VERIFY_WRITE, to, sizeof(*to)))
233                 return -EFAULT;
234
235         /* If you change siginfo_t structure, please be sure
236          * this code is fixed accordingly.
237          * It should never copy any pad contained in the structure
238          * to avoid security leaks, but must copy the generic
239          * 3 ints plus the relevant union member.
240          * This routine must convert siginfo from 64bit to 32bit as well
241          * at the same time.
242          */
243         err = __put_user(from->si_signo, &to->si_signo);
244         err |= __put_user(from->si_errno, &to->si_errno);
245         err |= __put_user((short)from->si_code, &to->si_code);
246         if (from->si_code < 0)
247                 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad,
248                                       SI_PAD_SIZE);
249         else switch (from->si_code & __SI_MASK) {
250         case __SI_KILL:
251                 err |= __put_user(from->si_pid, &to->si_pid);
252                 err |= __put_user(from->si_uid, &to->si_uid);
253                 break;
254         case __SI_TIMER:
255                  err |= __put_user(from->si_tid, &to->si_tid);
256                  err |= __put_user(from->si_overrun, &to->si_overrun);
257                  err |= __put_user((compat_uptr_t)(unsigned long)from->si_ptr,
258                                    &to->si_ptr);
259                 break;
260         case __SI_POLL:
261                 err |= __put_user(from->si_band, &to->si_band);
262                 err |= __put_user(from->si_fd, &to->si_fd);
263                 break;
264         case __SI_FAULT:
265                 err |= __put_user((compat_uptr_t)(unsigned long)from->si_addr,
266                                   &to->si_addr);
267 #ifdef BUS_MCEERR_AO
268                 /*
269                  * Other callers might not initialize the si_lsb field,
270                  * so check explicitely for the right codes here.
271                  */
272                 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
273                         err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
274 #endif
275                 break;
276         case __SI_CHLD:
277                 err |= __put_user(from->si_pid, &to->si_pid);
278                 err |= __put_user(from->si_uid, &to->si_uid);
279                 err |= __put_user(from->si_status, &to->si_status);
280                 err |= __put_user(from->si_utime, &to->si_utime);
281                 err |= __put_user(from->si_stime, &to->si_stime);
282                 break;
283         case __SI_RT: /* This is not generated by the kernel as of now. */
284         case __SI_MESGQ: /* But this is */
285                 err |= __put_user(from->si_pid, &to->si_pid);
286                 err |= __put_user(from->si_uid, &to->si_uid);
287                 err |= __put_user((compat_uptr_t)(unsigned long)from->si_ptr, &to->si_ptr);
288                 break;
289         default: /* this is just in case for now ... */
290                 err |= __put_user(from->si_pid, &to->si_pid);
291                 err |= __put_user(from->si_uid, &to->si_uid);
292                 break;
293         }
294         return err;
295 }
296
297 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
298 {
299         memset(to, 0, sizeof *to);
300
301         if (copy_from_user(to, from, __ARCH_SI_PREAMBLE_SIZE) ||
302             copy_from_user(to->_sifields._pad,
303                            from->_sifields._pad, SI_PAD_SIZE))
304                 return -EFAULT;
305
306         return 0;
307 }
308
309 /*
310  * VFP save/restore code.
311  */
312 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
313 {
314         struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
315         compat_ulong_t magic = VFP_MAGIC;
316         compat_ulong_t size = VFP_STORAGE_SIZE;
317         compat_ulong_t fpscr, fpexc;
318         int err = 0;
319
320         /*
321          * Save the hardware registers to the fpsimd_state structure.
322          * Note that this also saves V16-31, which aren't visible
323          * in AArch32.
324          */
325         fpsimd_save_state(fpsimd);
326
327         /* Place structure header on the stack */
328         __put_user_error(magic, &frame->magic, err);
329         __put_user_error(size, &frame->size, err);
330
331         /*
332          * Now copy the FP registers. Since the registers are packed,
333          * we can copy the prefix we want (V0-V15) as it is.
334          * FIXME: Won't work if big endian.
335          */
336         err |= __copy_to_user(&frame->ufp.fpregs, fpsimd->vregs,
337                               sizeof(frame->ufp.fpregs));
338
339         /* Create an AArch32 fpscr from the fpsr and the fpcr. */
340         fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
341                 (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
342         __put_user_error(fpscr, &frame->ufp.fpscr, err);
343
344         /*
345          * The exception register aren't available so we fake up a
346          * basic FPEXC and zero everything else.
347          */
348         fpexc = (1 << 30);
349         __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
350         __put_user_error(0, &frame->ufp_exc.fpinst, err);
351         __put_user_error(0, &frame->ufp_exc.fpinst2, err);
352
353         return err ? -EFAULT : 0;
354 }
355
356 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
357 {
358         struct fpsimd_state fpsimd;
359         compat_ulong_t magic = VFP_MAGIC;
360         compat_ulong_t size = VFP_STORAGE_SIZE;
361         compat_ulong_t fpscr;
362         int err = 0;
363
364         __get_user_error(magic, &frame->magic, err);
365         __get_user_error(size, &frame->size, err);
366
367         if (err)
368                 return -EFAULT;
369         if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
370                 return -EINVAL;
371
372         /*
373          * Copy the FP registers into the start of the fpsimd_state.
374          * FIXME: Won't work if big endian.
375          */
376         err |= __copy_from_user(fpsimd.vregs, frame->ufp.fpregs,
377                                 sizeof(frame->ufp.fpregs));
378
379         /* Extract the fpsr and the fpcr from the fpscr */
380         __get_user_error(fpscr, &frame->ufp.fpscr, err);
381         fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
382         fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
383
384         /*
385          * We don't need to touch the exception register, so
386          * reload the hardware state.
387          */
388         if (!err) {
389                 preempt_disable();
390                 fpsimd_load_state(&fpsimd);
391                 preempt_enable();
392         }
393
394         return err ? -EFAULT : 0;
395 }
396
397 /*
398  * atomically swap in the new signal mask, and wait for a signal.
399  */
400 asmlinkage int compat_sys_sigsuspend(int restart, compat_ulong_t oldmask,
401                                      compat_old_sigset_t mask)
402 {
403         sigset_t blocked;
404
405         siginitset(&current->blocked, mask);
406         return sigsuspend(&blocked);
407 }
408
409 asmlinkage int compat_sys_sigaction(int sig,
410                                     const struct compat_old_sigaction __user *act,
411                                     struct compat_old_sigaction __user *oact)
412 {
413         struct k_sigaction new_ka, old_ka;
414         int ret;
415         compat_old_sigset_t mask;
416         compat_uptr_t handler, restorer;
417
418         if (act) {
419                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
420                     __get_user(handler, &act->sa_handler) ||
421                     __get_user(restorer, &act->sa_restorer) ||
422                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
423                     __get_user(mask, &act->sa_mask))
424                         return -EFAULT;
425
426                 new_ka.sa.sa_handler = compat_ptr(handler);
427                 new_ka.sa.sa_restorer = compat_ptr(restorer);
428                 siginitset(&new_ka.sa.sa_mask, mask);
429         }
430
431         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
432
433         if (!ret && oact) {
434                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
435                     __put_user(ptr_to_compat(old_ka.sa.sa_handler),
436                                &oact->sa_handler) ||
437                     __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
438                                &oact->sa_restorer) ||
439                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
440                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
441                         return -EFAULT;
442         }
443
444         return ret;
445 }
446
447 asmlinkage int compat_sys_rt_sigaction(int sig,
448                                        const struct compat_sigaction __user *act,
449                                        struct compat_sigaction __user *oact,
450                                        compat_size_t sigsetsize)
451 {
452         struct k_sigaction new_ka, old_ka;
453         int ret;
454
455         /* XXX: Don't preclude handling different sized sigset_t's.  */
456         if (sigsetsize != sizeof(compat_sigset_t))
457                 return -EINVAL;
458
459         if (act) {
460                 compat_uptr_t handler, restorer;
461
462                 ret = get_user(handler, &act->sa_handler);
463                 new_ka.sa.sa_handler = compat_ptr(handler);
464                 ret |= get_user(restorer, &act->sa_restorer);
465                 new_ka.sa.sa_restorer = compat_ptr(restorer);
466                 ret |= get_sigset_t(&new_ka.sa.sa_mask, &act->sa_mask);
467                 ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
468                 if (ret)
469                         return -EFAULT;
470         }
471
472         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
473         if (!ret && oact) {
474                 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler);
475                 ret |= put_sigset_t(&oact->sa_mask, &old_ka.sa.sa_mask);
476                 ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
477         }
478         return ret;
479 }
480
481 int compat_do_sigaltstack(compat_uptr_t compat_uss, compat_uptr_t compat_uoss,
482                           compat_ulong_t sp)
483 {
484         compat_stack_t __user *newstack = compat_ptr(compat_uss);
485         compat_stack_t __user *oldstack = compat_ptr(compat_uoss);
486         compat_uptr_t ss_sp;
487         int ret;
488         mm_segment_t old_fs;
489         stack_t uss, uoss;
490
491         /* Marshall the compat new stack into a stack_t */
492         if (newstack) {
493                 if (get_user(ss_sp, &newstack->ss_sp) ||
494                     __get_user(uss.ss_flags, &newstack->ss_flags) ||
495                     __get_user(uss.ss_size, &newstack->ss_size))
496                         return -EFAULT;
497                 uss.ss_sp = compat_ptr(ss_sp);
498         }
499
500         old_fs = get_fs();
501         set_fs(KERNEL_DS);
502         /* The __user pointer casts are valid because of the set_fs() */
503         ret = do_sigaltstack(
504                 newstack ? (stack_t __user *) &uss : NULL,
505                 oldstack ? (stack_t __user *) &uoss : NULL,
506                 (unsigned long)sp);
507         set_fs(old_fs);
508
509         /* Convert the old stack_t into a compat stack. */
510         if (!ret && oldstack &&
511                 (put_user(ptr_to_compat(uoss.ss_sp), &oldstack->ss_sp) ||
512                  __put_user(uoss.ss_flags, &oldstack->ss_flags) ||
513                  __put_user(uoss.ss_size, &oldstack->ss_size)))
514                 return -EFAULT;
515         return ret;
516 }
517
518 static int compat_restore_sigframe(struct pt_regs *regs,
519                                    struct compat_sigframe __user *sf)
520 {
521         int err;
522         sigset_t set;
523         struct compat_aux_sigframe __user *aux;
524
525         err = get_sigset_t(&set, &sf->uc.uc_sigmask);
526         if (err == 0) {
527                 sigdelsetmask(&set, ~_BLOCKABLE);
528                 set_current_blocked(&set);
529         }
530
531         __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
532         __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
533         __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
534         __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
535         __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
536         __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
537         __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
538         __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
539         __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
540         __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
541         __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
542         __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
543         __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
544         __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
545         __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
546         __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
547         __get_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
548
549         /*
550          * Avoid compat_sys_sigreturn() restarting.
551          */
552         regs->syscallno = ~0UL;
553
554         err |= !valid_user_regs(&regs->user_regs);
555
556         aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
557         if (err == 0)
558                 err |= compat_restore_vfp_context(&aux->vfp);
559
560         return err;
561 }
562
563 asmlinkage int compat_sys_sigreturn(struct pt_regs *regs)
564 {
565         struct compat_sigframe __user *frame;
566
567         /* Always make any pending restarted system calls return -EINTR */
568         current_thread_info()->restart_block.fn = do_no_restart_syscall;
569
570         /*
571          * Since we stacked the signal on a 64-bit boundary,
572          * then 'sp' should be word aligned here.  If it's
573          * not, then the user is trying to mess with us.
574          */
575         if (regs->compat_sp & 7)
576                 goto badframe;
577
578         frame = (struct compat_sigframe __user *)regs->compat_sp;
579
580         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
581                 goto badframe;
582
583         if (compat_restore_sigframe(regs, frame))
584                 goto badframe;
585
586         return regs->regs[0];
587
588 badframe:
589         if (show_unhandled_signals)
590                 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
591                                     current->comm, task_pid_nr(current), __func__,
592                                     regs->pc, regs->sp);
593         force_sig(SIGSEGV, current);
594         return 0;
595 }
596
597 asmlinkage int compat_sys_rt_sigreturn(struct pt_regs *regs)
598 {
599         struct compat_rt_sigframe __user *frame;
600
601         /* Always make any pending restarted system calls return -EINTR */
602         current_thread_info()->restart_block.fn = do_no_restart_syscall;
603
604         /*
605          * Since we stacked the signal on a 64-bit boundary,
606          * then 'sp' should be word aligned here.  If it's
607          * not, then the user is trying to mess with us.
608          */
609         if (regs->compat_sp & 7)
610                 goto badframe;
611
612         frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
613
614         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
615                 goto badframe;
616
617         if (compat_restore_sigframe(regs, &frame->sig))
618                 goto badframe;
619
620         if (compat_do_sigaltstack(ptr_to_compat(&frame->sig.uc.uc_stack),
621                                  ptr_to_compat((void __user *)NULL),
622                                  regs->compat_sp) == -EFAULT)
623                 goto badframe;
624
625         return regs->regs[0];
626
627 badframe:
628         if (show_unhandled_signals)
629                 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
630                                     current->comm, task_pid_nr(current), __func__,
631                                     regs->pc, regs->sp);
632         force_sig(SIGSEGV, current);
633         return 0;
634 }
635
636 static inline void __user *compat_get_sigframe(struct k_sigaction *ka,
637                                                struct pt_regs *regs,
638                                                int framesize)
639 {
640         compat_ulong_t sp = regs->compat_sp;
641         void __user *frame;
642
643         /*
644          * This is the X/Open sanctioned signal stack switching.
645          */
646         if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
647                 sp = current->sas_ss_sp + current->sas_ss_size;
648
649         /*
650          * ATPCS B01 mandates 8-byte alignment
651          */
652         frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
653
654         /*
655          * Check that we can actually write to the signal frame.
656          */
657         if (!access_ok(VERIFY_WRITE, frame, framesize))
658                 frame = NULL;
659
660         return frame;
661 }
662
663 static int compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
664                                compat_ulong_t __user *rc, void __user *frame,
665                                int usig)
666 {
667         compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
668         compat_ulong_t retcode;
669         compat_ulong_t spsr = regs->pstate & ~PSR_f;
670         int thumb;
671
672         /* Check if the handler is written for ARM or Thumb */
673         thumb = handler & 1;
674
675         if (thumb) {
676                 spsr |= COMPAT_PSR_T_BIT;
677                 spsr &= ~COMPAT_PSR_IT_MASK;
678         } else {
679                 spsr &= ~COMPAT_PSR_T_BIT;
680         }
681
682         if (ka->sa.sa_flags & SA_RESTORER) {
683                 retcode = ptr_to_compat(ka->sa.sa_restorer);
684         } else {
685                 /* Set up sigreturn pointer */
686                 unsigned int idx = thumb << 1;
687
688                 if (ka->sa.sa_flags & SA_SIGINFO)
689                         idx += 3;
690
691                 retcode = AARCH32_VECTORS_BASE +
692                           AARCH32_KERN_SIGRET_CODE_OFFSET +
693                           (idx << 2) + thumb;
694         }
695
696         regs->regs[0]   = usig;
697         regs->compat_sp = ptr_to_compat(frame);
698         regs->compat_lr = retcode;
699         regs->pc        = handler;
700         regs->pstate    = spsr;
701
702         return 0;
703 }
704
705 static int compat_setup_sigframe(struct compat_sigframe __user *sf,
706                                  struct pt_regs *regs, sigset_t *set)
707 {
708         struct compat_aux_sigframe __user *aux;
709         int err = 0;
710
711         __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
712         __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
713         __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
714         __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
715         __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
716         __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
717         __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
718         __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
719         __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
720         __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
721         __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
722         __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
723         __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
724         __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
725         __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
726         __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
727         __put_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
728
729         __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
730         __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.error_code, err);
731         __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
732         __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
733
734         err |= put_sigset_t(&sf->uc.uc_sigmask, set);
735
736         aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
737
738         if (err == 0)
739                 err |= compat_preserve_vfp_context(&aux->vfp);
740         __put_user_error(0, &aux->end_magic, err);
741
742         return err;
743 }
744
745 /*
746  * 32-bit signal handling routines called from signal.c
747  */
748 int compat_setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
749                           sigset_t *set, struct pt_regs *regs)
750 {
751         struct compat_rt_sigframe __user *frame;
752         compat_stack_t stack;
753         int err = 0;
754
755         frame = compat_get_sigframe(ka, regs, sizeof(*frame));
756
757         if (!frame)
758                 return 1;
759
760         err |= copy_siginfo_to_user32(&frame->info, info);
761
762         __put_user_error(0, &frame->sig.uc.uc_flags, err);
763         __put_user_error(NULL, &frame->sig.uc.uc_link, err);
764
765         memset(&stack, 0, sizeof(stack));
766         stack.ss_sp = (compat_uptr_t)current->sas_ss_sp;
767         stack.ss_flags = sas_ss_flags(regs->compat_sp);
768         stack.ss_size = current->sas_ss_size;
769         err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
770
771         err |= compat_setup_sigframe(&frame->sig, regs, set);
772         if (err == 0)
773                 err = compat_setup_return(regs, ka, frame->sig.retcode, frame,
774                                           usig);
775
776         if (err == 0) {
777                 regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
778                 regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
779         }
780
781         return err;
782 }
783
784 int compat_setup_frame(int usig, struct k_sigaction *ka, sigset_t *set,
785                        struct pt_regs *regs)
786 {
787         struct compat_sigframe __user *frame;
788         int err = 0;
789
790         frame = compat_get_sigframe(ka, regs, sizeof(*frame));
791
792         if (!frame)
793                 return 1;
794
795         __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
796
797         err |= compat_setup_sigframe(frame, regs, set);
798         if (err == 0)
799                 err = compat_setup_return(regs, ka, frame->retcode, frame, usig);
800
801         return err;
802 }
803
804 /*
805  * RT signals don't have generic compat wrappers.
806  * See arch/powerpc/kernel/signal_32.c
807  */
808 asmlinkage int compat_sys_rt_sigprocmask(int how, compat_sigset_t __user *set,
809                                          compat_sigset_t __user *oset,
810                                          compat_size_t sigsetsize)
811 {
812         sigset_t s;
813         sigset_t __user *up;
814         int ret;
815         mm_segment_t old_fs = get_fs();
816
817         if (set) {
818                 if (get_sigset_t(&s, set))
819                         return -EFAULT;
820         }
821
822         set_fs(KERNEL_DS);
823         /* This is valid because of the set_fs() */
824         up = (sigset_t __user *) &s;
825         ret = sys_rt_sigprocmask(how, set ? up : NULL, oset ? up : NULL,
826                                  sigsetsize);
827         set_fs(old_fs);
828         if (ret)
829                 return ret;
830         if (oset) {
831                 if (put_sigset_t(oset, &s))
832                         return -EFAULT;
833         }
834         return 0;
835 }
836
837 asmlinkage int compat_sys_rt_sigpending(compat_sigset_t __user *set,
838                                         compat_size_t sigsetsize)
839 {
840         sigset_t s;
841         int ret;
842         mm_segment_t old_fs = get_fs();
843
844         set_fs(KERNEL_DS);
845         /* The __user pointer cast is valid because of the set_fs() */
846         ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
847         set_fs(old_fs);
848         if (!ret) {
849                 if (put_sigset_t(set, &s))
850                         return -EFAULT;
851         }
852         return ret;
853 }
854
855 asmlinkage int compat_sys_rt_sigqueueinfo(int pid, int sig,
856                                           compat_siginfo_t __user *uinfo)
857 {
858         siginfo_t info;
859         int ret;
860         mm_segment_t old_fs = get_fs();
861
862         ret = copy_siginfo_from_user32(&info, uinfo);
863         if (unlikely(ret))
864                 return ret;
865
866         set_fs (KERNEL_DS);
867         /* The __user pointer cast is valid because of the set_fs() */
868         ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *) &info);
869         set_fs (old_fs);
870         return ret;
871 }
872
873 void compat_setup_restart_syscall(struct pt_regs *regs)
874 {
875        regs->regs[7] = __NR_restart_syscall;
876 }