2 * Based on arch/arm/kernel/signal.c
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
6 * Modified by Will Deacon <will.deacon@arm.com>
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.
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.
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/>.
21 #include <linux/compat.h>
22 #include <linux/signal.h>
23 #include <linux/syscalls.h>
24 #include <linux/ratelimit.h>
27 #include <asm/fpsimd.h>
28 #include <asm/signal32.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
32 struct compat_sigcontext {
33 /* We always set these two fields to 0 */
34 compat_ulong_t trap_no;
35 compat_ulong_t error_code;
37 compat_ulong_t oldmask;
38 compat_ulong_t arm_r0;
39 compat_ulong_t arm_r1;
40 compat_ulong_t arm_r2;
41 compat_ulong_t arm_r3;
42 compat_ulong_t arm_r4;
43 compat_ulong_t arm_r5;
44 compat_ulong_t arm_r6;
45 compat_ulong_t arm_r7;
46 compat_ulong_t arm_r8;
47 compat_ulong_t arm_r9;
48 compat_ulong_t arm_r10;
49 compat_ulong_t arm_fp;
50 compat_ulong_t arm_ip;
51 compat_ulong_t arm_sp;
52 compat_ulong_t arm_lr;
53 compat_ulong_t arm_pc;
54 compat_ulong_t arm_cpsr;
55 compat_ulong_t fault_address;
58 struct compat_ucontext {
59 compat_ulong_t uc_flags;
60 compat_uptr_t uc_link;
61 compat_stack_t uc_stack;
62 struct compat_sigcontext uc_mcontext;
63 compat_sigset_t uc_sigmask;
64 int __unused[32 - (sizeof (compat_sigset_t) / sizeof (int))];
65 compat_ulong_t uc_regspace[128] __attribute__((__aligned__(8)));
68 struct compat_vfp_sigframe {
71 struct compat_user_vfp {
72 compat_u64 fpregs[32];
75 struct compat_user_vfp_exc {
77 compat_ulong_t fpinst;
78 compat_ulong_t fpinst2;
80 } __attribute__((__aligned__(8)));
82 #define VFP_MAGIC 0x56465001
83 #define VFP_STORAGE_SIZE sizeof(struct compat_vfp_sigframe)
85 #define FSR_WRITE_SHIFT (11)
87 struct compat_aux_sigframe {
88 struct compat_vfp_sigframe vfp;
90 /* Something that isn't a valid magic number for any coprocessor. */
91 unsigned long end_magic;
92 } __attribute__((__aligned__(8)));
94 struct compat_sigframe {
95 struct compat_ucontext uc;
96 compat_ulong_t retcode[2];
99 struct compat_rt_sigframe {
100 struct compat_siginfo info;
101 struct compat_sigframe sig;
104 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
106 static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
108 compat_sigset_t cset;
110 cset.sig[0] = set->sig[0] & 0xffffffffull;
111 cset.sig[1] = set->sig[0] >> 32;
113 return copy_to_user(uset, &cset, sizeof(*uset));
116 static inline int get_sigset_t(sigset_t *set,
117 const compat_sigset_t __user *uset)
121 if (copy_from_user(&s32, uset, sizeof(*uset)))
124 set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
128 int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
132 if (!access_ok(VERIFY_WRITE, to, sizeof(*to)))
135 /* If you change siginfo_t structure, please be sure
136 * this code is fixed accordingly.
137 * It should never copy any pad contained in the structure
138 * to avoid security leaks, but must copy the generic
139 * 3 ints plus the relevant union member.
140 * This routine must convert siginfo from 64bit to 32bit as well
143 err = __put_user(from->si_signo, &to->si_signo);
144 err |= __put_user(from->si_errno, &to->si_errno);
145 err |= __put_user((short)from->si_code, &to->si_code);
146 if (from->si_code < 0)
147 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad,
149 else switch (from->si_code & __SI_MASK) {
151 err |= __put_user(from->si_pid, &to->si_pid);
152 err |= __put_user(from->si_uid, &to->si_uid);
155 err |= __put_user(from->si_tid, &to->si_tid);
156 err |= __put_user(from->si_overrun, &to->si_overrun);
157 err |= __put_user(from->si_int, &to->si_int);
160 err |= __put_user(from->si_band, &to->si_band);
161 err |= __put_user(from->si_fd, &to->si_fd);
164 err |= __put_user((compat_uptr_t)(unsigned long)from->si_addr,
168 * Other callers might not initialize the si_lsb field,
169 * so check explicitely for the right codes here.
171 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
172 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
176 err |= __put_user(from->si_pid, &to->si_pid);
177 err |= __put_user(from->si_uid, &to->si_uid);
178 err |= __put_user(from->si_status, &to->si_status);
179 err |= __put_user(from->si_utime, &to->si_utime);
180 err |= __put_user(from->si_stime, &to->si_stime);
182 case __SI_RT: /* This is not generated by the kernel as of now. */
183 case __SI_MESGQ: /* But this is */
184 err |= __put_user(from->si_pid, &to->si_pid);
185 err |= __put_user(from->si_uid, &to->si_uid);
186 err |= __put_user(from->si_int, &to->si_int);
189 err |= __put_user((compat_uptr_t)(unsigned long)
190 from->si_call_addr, &to->si_call_addr);
191 err |= __put_user(from->si_syscall, &to->si_syscall);
192 err |= __put_user(from->si_arch, &to->si_arch);
194 default: /* this is just in case for now ... */
195 err |= __put_user(from->si_pid, &to->si_pid);
196 err |= __put_user(from->si_uid, &to->si_uid);
202 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
204 memset(to, 0, sizeof *to);
206 if (copy_from_user(to, from, __ARCH_SI_PREAMBLE_SIZE) ||
207 copy_from_user(to->_sifields._pad,
208 from->_sifields._pad, SI_PAD_SIZE))
215 * VFP save/restore code.
217 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
219 struct fpsimd_state *fpsimd = ¤t->thread.fpsimd_state;
220 compat_ulong_t magic = VFP_MAGIC;
221 compat_ulong_t size = VFP_STORAGE_SIZE;
222 compat_ulong_t fpscr, fpexc;
226 * Save the hardware registers to the fpsimd_state structure.
227 * Note that this also saves V16-31, which aren't visible
230 fpsimd_preserve_current_state();
232 /* Place structure header on the stack */
233 __put_user_error(magic, &frame->magic, err);
234 __put_user_error(size, &frame->size, err);
237 * Now copy the FP registers. Since the registers are packed,
238 * we can copy the prefix we want (V0-V15) as it is.
239 * FIXME: Won't work if big endian.
241 err |= __copy_to_user(&frame->ufp.fpregs, fpsimd->vregs,
242 sizeof(frame->ufp.fpregs));
244 /* Create an AArch32 fpscr from the fpsr and the fpcr. */
245 fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
246 (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
247 __put_user_error(fpscr, &frame->ufp.fpscr, err);
250 * The exception register aren't available so we fake up a
251 * basic FPEXC and zero everything else.
254 __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
255 __put_user_error(0, &frame->ufp_exc.fpinst, err);
256 __put_user_error(0, &frame->ufp_exc.fpinst2, err);
258 return err ? -EFAULT : 0;
261 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
263 struct fpsimd_state fpsimd;
264 compat_ulong_t magic = VFP_MAGIC;
265 compat_ulong_t size = VFP_STORAGE_SIZE;
266 compat_ulong_t fpscr;
269 __get_user_error(magic, &frame->magic, err);
270 __get_user_error(size, &frame->size, err);
274 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
278 * Copy the FP registers into the start of the fpsimd_state.
279 * FIXME: Won't work if big endian.
281 err |= __copy_from_user(fpsimd.vregs, frame->ufp.fpregs,
282 sizeof(frame->ufp.fpregs));
284 /* Extract the fpsr and the fpcr from the fpscr */
285 __get_user_error(fpscr, &frame->ufp.fpscr, err);
286 fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
287 fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
290 * We don't need to touch the exception register, so
291 * reload the hardware state.
294 fpsimd_update_current_state(&fpsimd);
296 return err ? -EFAULT : 0;
299 static int compat_restore_sigframe(struct pt_regs *regs,
300 struct compat_sigframe __user *sf)
304 struct compat_aux_sigframe __user *aux;
306 err = get_sigset_t(&set, &sf->uc.uc_sigmask);
308 sigdelsetmask(&set, ~_BLOCKABLE);
309 set_current_blocked(&set);
312 __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
313 __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
314 __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
315 __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
316 __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
317 __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
318 __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
319 __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
320 __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
321 __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
322 __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
323 __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
324 __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
325 __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
326 __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
327 __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
328 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
331 * Avoid compat_sys_sigreturn() restarting.
333 regs->syscallno = ~0UL;
335 err |= !valid_user_regs(®s->user_regs);
337 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
339 err |= compat_restore_vfp_context(&aux->vfp);
344 asmlinkage int compat_sys_sigreturn(struct pt_regs *regs)
346 struct compat_sigframe __user *frame;
348 /* Always make any pending restarted system calls return -EINTR */
349 current->restart_block.fn = do_no_restart_syscall;
352 * Since we stacked the signal on a 64-bit boundary,
353 * then 'sp' should be word aligned here. If it's
354 * not, then the user is trying to mess with us.
356 if (regs->compat_sp & 7)
359 frame = (struct compat_sigframe __user *)regs->compat_sp;
361 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
364 if (compat_restore_sigframe(regs, frame))
367 return regs->regs[0];
370 if (show_unhandled_signals)
371 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
372 current->comm, task_pid_nr(current), __func__,
374 force_sig(SIGSEGV, current);
378 asmlinkage int compat_sys_rt_sigreturn(struct pt_regs *regs)
380 struct compat_rt_sigframe __user *frame;
382 /* Always make any pending restarted system calls return -EINTR */
383 current->restart_block.fn = do_no_restart_syscall;
386 * Since we stacked the signal on a 64-bit boundary,
387 * then 'sp' should be word aligned here. If it's
388 * not, then the user is trying to mess with us.
390 if (regs->compat_sp & 7)
393 frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
395 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
398 if (compat_restore_sigframe(regs, &frame->sig))
401 if (compat_restore_altstack(&frame->sig.uc.uc_stack))
404 return regs->regs[0];
407 if (show_unhandled_signals)
408 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
409 current->comm, task_pid_nr(current), __func__,
411 force_sig(SIGSEGV, current);
415 static void __user *compat_get_sigframe(struct ksignal *ksig,
416 struct pt_regs *regs,
419 compat_ulong_t sp = sigsp(regs->compat_sp, ksig);
423 * ATPCS B01 mandates 8-byte alignment
425 frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
428 * Check that we can actually write to the signal frame.
430 if (!access_ok(VERIFY_WRITE, frame, framesize))
436 static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
437 compat_ulong_t __user *rc, void __user *frame,
440 compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
441 compat_ulong_t retcode;
442 compat_ulong_t spsr = regs->pstate & ~(PSR_f | COMPAT_PSR_E_BIT);
445 /* Check if the handler is written for ARM or Thumb */
449 spsr |= COMPAT_PSR_T_BIT;
451 spsr &= ~COMPAT_PSR_T_BIT;
453 /* The IT state must be cleared for both ARM and Thumb-2 */
454 spsr &= ~COMPAT_PSR_IT_MASK;
456 /* Restore the original endianness */
457 spsr |= COMPAT_PSR_ENDSTATE;
459 if (ka->sa.sa_flags & SA_RESTORER) {
460 retcode = ptr_to_compat(ka->sa.sa_restorer);
462 /* Set up sigreturn pointer */
463 unsigned int idx = thumb << 1;
465 if (ka->sa.sa_flags & SA_SIGINFO)
468 retcode = AARCH32_VECTORS_BASE +
469 AARCH32_KERN_SIGRET_CODE_OFFSET +
473 regs->regs[0] = usig;
474 regs->compat_sp = ptr_to_compat(frame);
475 regs->compat_lr = retcode;
480 static int compat_setup_sigframe(struct compat_sigframe __user *sf,
481 struct pt_regs *regs, sigset_t *set)
483 struct compat_aux_sigframe __user *aux;
486 __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
487 __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
488 __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
489 __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
490 __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
491 __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
492 __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
493 __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
494 __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
495 __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
496 __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
497 __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
498 __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
499 __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
500 __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
501 __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
502 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
504 __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
505 /* set the compat FSR WnR */
506 __put_user_error(!!(current->thread.fault_code & ESR_ELx_WNR) <<
507 FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err);
508 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
509 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
511 err |= put_sigset_t(&sf->uc.uc_sigmask, set);
513 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
516 err |= compat_preserve_vfp_context(&aux->vfp);
517 __put_user_error(0, &aux->end_magic, err);
523 * 32-bit signal handling routines called from signal.c
525 int compat_setup_rt_frame(int usig, struct ksignal *ksig,
526 sigset_t *set, struct pt_regs *regs)
528 struct compat_rt_sigframe __user *frame;
531 frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
536 err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
538 __put_user_error(0, &frame->sig.uc.uc_flags, err);
539 __put_user_error(0, &frame->sig.uc.uc_link, err);
541 err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
543 err |= compat_setup_sigframe(&frame->sig, regs, set);
546 compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig);
547 regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
548 regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
554 int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set,
555 struct pt_regs *regs)
557 struct compat_sigframe __user *frame;
560 frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
565 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
567 err |= compat_setup_sigframe(frame, regs, set);
569 compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig);
574 void compat_setup_restart_syscall(struct pt_regs *regs)
576 regs->regs[7] = __NR_compat_restart_syscall;