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/traps.h>
30 #include <linux/uaccess.h>
31 #include <asm/unistd.h>
33 struct compat_sigcontext
{
34 /* We always set these two fields to 0 */
35 compat_ulong_t trap_no
;
36 compat_ulong_t error_code
;
38 compat_ulong_t oldmask
;
39 compat_ulong_t arm_r0
;
40 compat_ulong_t arm_r1
;
41 compat_ulong_t arm_r2
;
42 compat_ulong_t arm_r3
;
43 compat_ulong_t arm_r4
;
44 compat_ulong_t arm_r5
;
45 compat_ulong_t arm_r6
;
46 compat_ulong_t arm_r7
;
47 compat_ulong_t arm_r8
;
48 compat_ulong_t arm_r9
;
49 compat_ulong_t arm_r10
;
50 compat_ulong_t arm_fp
;
51 compat_ulong_t arm_ip
;
52 compat_ulong_t arm_sp
;
53 compat_ulong_t arm_lr
;
54 compat_ulong_t arm_pc
;
55 compat_ulong_t arm_cpsr
;
56 compat_ulong_t fault_address
;
59 struct compat_ucontext
{
60 compat_ulong_t uc_flags
;
61 compat_uptr_t uc_link
;
62 compat_stack_t uc_stack
;
63 struct compat_sigcontext uc_mcontext
;
64 compat_sigset_t uc_sigmask
;
65 int __unused
[32 - (sizeof (compat_sigset_t
) / sizeof (int))];
66 compat_ulong_t uc_regspace
[128] __attribute__((__aligned__(8)));
69 struct compat_vfp_sigframe
{
72 struct compat_user_vfp
{
73 compat_u64 fpregs
[32];
76 struct compat_user_vfp_exc
{
78 compat_ulong_t fpinst
;
79 compat_ulong_t fpinst2
;
81 } __attribute__((__aligned__(8)));
83 #define VFP_MAGIC 0x56465001
84 #define VFP_STORAGE_SIZE sizeof(struct compat_vfp_sigframe)
86 #define FSR_WRITE_SHIFT (11)
88 struct compat_aux_sigframe
{
89 struct compat_vfp_sigframe vfp
;
91 /* Something that isn't a valid magic number for any coprocessor. */
92 unsigned long end_magic
;
93 } __attribute__((__aligned__(8)));
95 struct compat_sigframe
{
96 struct compat_ucontext uc
;
97 compat_ulong_t retcode
[2];
100 struct compat_rt_sigframe
{
101 struct compat_siginfo info
;
102 struct compat_sigframe sig
;
105 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
107 static inline int put_sigset_t(compat_sigset_t __user
*uset
, sigset_t
*set
)
109 compat_sigset_t cset
;
111 cset
.sig
[0] = set
->sig
[0] & 0xffffffffull
;
112 cset
.sig
[1] = set
->sig
[0] >> 32;
114 return copy_to_user(uset
, &cset
, sizeof(*uset
));
117 static inline int get_sigset_t(sigset_t
*set
,
118 const compat_sigset_t __user
*uset
)
122 if (copy_from_user(&s32
, uset
, sizeof(*uset
)))
125 set
->sig
[0] = s32
.sig
[0] | (((long)s32
.sig
[1]) << 32);
130 * VFP save/restore code.
132 * We have to be careful with endianness, since the fpsimd context-switch
133 * code operates on 128-bit (Q) register values whereas the compat ABI
134 * uses an array of 64-bit (D) registers. Consequently, we need to swap
135 * the two halves of each Q register when running on a big-endian CPU.
137 union __fpsimd_vreg
{
150 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user
*frame
)
152 struct user_fpsimd_state
const *fpsimd
=
153 ¤t
->thread
.uw
.fpsimd_state
;
154 compat_ulong_t magic
= VFP_MAGIC
;
155 compat_ulong_t size
= VFP_STORAGE_SIZE
;
156 compat_ulong_t fpscr
, fpexc
;
160 * Save the hardware registers to the fpsimd_state structure.
161 * Note that this also saves V16-31, which aren't visible
164 fpsimd_signal_preserve_current_state();
166 /* Place structure header on the stack */
167 __put_user_error(magic
, &frame
->magic
, err
);
168 __put_user_error(size
, &frame
->size
, err
);
171 * Now copy the FP registers. Since the registers are packed,
172 * we can copy the prefix we want (V0-V15) as it is.
174 for (i
= 0; i
< ARRAY_SIZE(frame
->ufp
.fpregs
); i
+= 2) {
175 union __fpsimd_vreg vreg
= {
176 .raw
= fpsimd
->vregs
[i
>> 1],
179 __put_user_error(vreg
.lo
, &frame
->ufp
.fpregs
[i
], err
);
180 __put_user_error(vreg
.hi
, &frame
->ufp
.fpregs
[i
+ 1], err
);
183 /* Create an AArch32 fpscr from the fpsr and the fpcr. */
184 fpscr
= (fpsimd
->fpsr
& VFP_FPSCR_STAT_MASK
) |
185 (fpsimd
->fpcr
& VFP_FPSCR_CTRL_MASK
);
186 __put_user_error(fpscr
, &frame
->ufp
.fpscr
, err
);
189 * The exception register aren't available so we fake up a
190 * basic FPEXC and zero everything else.
193 __put_user_error(fpexc
, &frame
->ufp_exc
.fpexc
, err
);
194 __put_user_error(0, &frame
->ufp_exc
.fpinst
, err
);
195 __put_user_error(0, &frame
->ufp_exc
.fpinst2
, err
);
197 return err
? -EFAULT
: 0;
200 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user
*frame
)
202 struct user_fpsimd_state fpsimd
;
203 compat_ulong_t magic
= VFP_MAGIC
;
204 compat_ulong_t size
= VFP_STORAGE_SIZE
;
205 compat_ulong_t fpscr
;
208 __get_user_error(magic
, &frame
->magic
, err
);
209 __get_user_error(size
, &frame
->size
, err
);
213 if (magic
!= VFP_MAGIC
|| size
!= VFP_STORAGE_SIZE
)
216 /* Copy the FP registers into the start of the fpsimd_state. */
217 for (i
= 0; i
< ARRAY_SIZE(frame
->ufp
.fpregs
); i
+= 2) {
218 union __fpsimd_vreg vreg
;
220 __get_user_error(vreg
.lo
, &frame
->ufp
.fpregs
[i
], err
);
221 __get_user_error(vreg
.hi
, &frame
->ufp
.fpregs
[i
+ 1], err
);
222 fpsimd
.vregs
[i
>> 1] = vreg
.raw
;
225 /* Extract the fpsr and the fpcr from the fpscr */
226 __get_user_error(fpscr
, &frame
->ufp
.fpscr
, err
);
227 fpsimd
.fpsr
= fpscr
& VFP_FPSCR_STAT_MASK
;
228 fpsimd
.fpcr
= fpscr
& VFP_FPSCR_CTRL_MASK
;
231 * We don't need to touch the exception register, so
232 * reload the hardware state.
235 fpsimd_update_current_state(&fpsimd
);
237 return err
? -EFAULT
: 0;
240 static int compat_restore_sigframe(struct pt_regs
*regs
,
241 struct compat_sigframe __user
*sf
)
245 struct compat_aux_sigframe __user
*aux
;
248 err
= get_sigset_t(&set
, &sf
->uc
.uc_sigmask
);
250 sigdelsetmask(&set
, ~_BLOCKABLE
);
251 set_current_blocked(&set
);
254 __get_user_error(regs
->regs
[0], &sf
->uc
.uc_mcontext
.arm_r0
, err
);
255 __get_user_error(regs
->regs
[1], &sf
->uc
.uc_mcontext
.arm_r1
, err
);
256 __get_user_error(regs
->regs
[2], &sf
->uc
.uc_mcontext
.arm_r2
, err
);
257 __get_user_error(regs
->regs
[3], &sf
->uc
.uc_mcontext
.arm_r3
, err
);
258 __get_user_error(regs
->regs
[4], &sf
->uc
.uc_mcontext
.arm_r4
, err
);
259 __get_user_error(regs
->regs
[5], &sf
->uc
.uc_mcontext
.arm_r5
, err
);
260 __get_user_error(regs
->regs
[6], &sf
->uc
.uc_mcontext
.arm_r6
, err
);
261 __get_user_error(regs
->regs
[7], &sf
->uc
.uc_mcontext
.arm_r7
, err
);
262 __get_user_error(regs
->regs
[8], &sf
->uc
.uc_mcontext
.arm_r8
, err
);
263 __get_user_error(regs
->regs
[9], &sf
->uc
.uc_mcontext
.arm_r9
, err
);
264 __get_user_error(regs
->regs
[10], &sf
->uc
.uc_mcontext
.arm_r10
, err
);
265 __get_user_error(regs
->regs
[11], &sf
->uc
.uc_mcontext
.arm_fp
, err
);
266 __get_user_error(regs
->regs
[12], &sf
->uc
.uc_mcontext
.arm_ip
, err
);
267 __get_user_error(regs
->compat_sp
, &sf
->uc
.uc_mcontext
.arm_sp
, err
);
268 __get_user_error(regs
->compat_lr
, &sf
->uc
.uc_mcontext
.arm_lr
, err
);
269 __get_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.arm_pc
, err
);
270 __get_user_error(psr
, &sf
->uc
.uc_mcontext
.arm_cpsr
, err
);
272 regs
->pstate
= compat_psr_to_pstate(psr
);
275 * Avoid compat_sys_sigreturn() restarting.
277 forget_syscall(regs
);
279 err
|= !valid_user_regs(®s
->user_regs
, current
);
281 aux
= (struct compat_aux_sigframe __user
*) sf
->uc
.uc_regspace
;
283 err
|= compat_restore_vfp_context(&aux
->vfp
);
288 COMPAT_SYSCALL_DEFINE0(sigreturn
)
290 struct pt_regs
*regs
= current_pt_regs();
291 struct compat_sigframe __user
*frame
;
293 /* Always make any pending restarted system calls return -EINTR */
294 current
->restart_block
.fn
= do_no_restart_syscall
;
297 * Since we stacked the signal on a 64-bit boundary,
298 * then 'sp' should be word aligned here. If it's
299 * not, then the user is trying to mess with us.
301 if (regs
->compat_sp
& 7)
304 frame
= (struct compat_sigframe __user
*)regs
->compat_sp
;
306 if (!access_ok(frame
, sizeof (*frame
)))
309 if (compat_restore_sigframe(regs
, frame
))
312 return regs
->regs
[0];
315 arm64_notify_segfault(regs
->compat_sp
);
319 COMPAT_SYSCALL_DEFINE0(rt_sigreturn
)
321 struct pt_regs
*regs
= current_pt_regs();
322 struct compat_rt_sigframe __user
*frame
;
324 /* Always make any pending restarted system calls return -EINTR */
325 current
->restart_block
.fn
= do_no_restart_syscall
;
328 * Since we stacked the signal on a 64-bit boundary,
329 * then 'sp' should be word aligned here. If it's
330 * not, then the user is trying to mess with us.
332 if (regs
->compat_sp
& 7)
335 frame
= (struct compat_rt_sigframe __user
*)regs
->compat_sp
;
337 if (!access_ok(frame
, sizeof (*frame
)))
340 if (compat_restore_sigframe(regs
, &frame
->sig
))
343 if (compat_restore_altstack(&frame
->sig
.uc
.uc_stack
))
346 return regs
->regs
[0];
349 arm64_notify_segfault(regs
->compat_sp
);
353 static void __user
*compat_get_sigframe(struct ksignal
*ksig
,
354 struct pt_regs
*regs
,
357 compat_ulong_t sp
= sigsp(regs
->compat_sp
, ksig
);
361 * ATPCS B01 mandates 8-byte alignment
363 frame
= compat_ptr((compat_uptr_t
)((sp
- framesize
) & ~7));
366 * Check that we can actually write to the signal frame.
368 if (!access_ok(frame
, framesize
))
374 static void compat_setup_return(struct pt_regs
*regs
, struct k_sigaction
*ka
,
375 compat_ulong_t __user
*rc
, void __user
*frame
,
378 compat_ulong_t handler
= ptr_to_compat(ka
->sa
.sa_handler
);
379 compat_ulong_t retcode
;
380 compat_ulong_t spsr
= regs
->pstate
& ~(PSR_f
| PSR_AA32_E_BIT
);
383 /* Check if the handler is written for ARM or Thumb */
387 spsr
|= PSR_AA32_T_BIT
;
389 spsr
&= ~PSR_AA32_T_BIT
;
391 /* The IT state must be cleared for both ARM and Thumb-2 */
392 spsr
&= ~PSR_AA32_IT_MASK
;
394 /* Restore the original endianness */
395 spsr
|= PSR_AA32_ENDSTATE
;
397 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
398 retcode
= ptr_to_compat(ka
->sa
.sa_restorer
);
400 /* Set up sigreturn pointer */
401 unsigned int idx
= thumb
<< 1;
403 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
406 retcode
= (unsigned long)current
->mm
->context
.vdso
+
410 regs
->regs
[0] = usig
;
411 regs
->compat_sp
= ptr_to_compat(frame
);
412 regs
->compat_lr
= retcode
;
417 static int compat_setup_sigframe(struct compat_sigframe __user
*sf
,
418 struct pt_regs
*regs
, sigset_t
*set
)
420 struct compat_aux_sigframe __user
*aux
;
421 unsigned long psr
= pstate_to_compat_psr(regs
->pstate
);
424 __put_user_error(regs
->regs
[0], &sf
->uc
.uc_mcontext
.arm_r0
, err
);
425 __put_user_error(regs
->regs
[1], &sf
->uc
.uc_mcontext
.arm_r1
, err
);
426 __put_user_error(regs
->regs
[2], &sf
->uc
.uc_mcontext
.arm_r2
, err
);
427 __put_user_error(regs
->regs
[3], &sf
->uc
.uc_mcontext
.arm_r3
, err
);
428 __put_user_error(regs
->regs
[4], &sf
->uc
.uc_mcontext
.arm_r4
, err
);
429 __put_user_error(regs
->regs
[5], &sf
->uc
.uc_mcontext
.arm_r5
, err
);
430 __put_user_error(regs
->regs
[6], &sf
->uc
.uc_mcontext
.arm_r6
, err
);
431 __put_user_error(regs
->regs
[7], &sf
->uc
.uc_mcontext
.arm_r7
, err
);
432 __put_user_error(regs
->regs
[8], &sf
->uc
.uc_mcontext
.arm_r8
, err
);
433 __put_user_error(regs
->regs
[9], &sf
->uc
.uc_mcontext
.arm_r9
, err
);
434 __put_user_error(regs
->regs
[10], &sf
->uc
.uc_mcontext
.arm_r10
, err
);
435 __put_user_error(regs
->regs
[11], &sf
->uc
.uc_mcontext
.arm_fp
, err
);
436 __put_user_error(regs
->regs
[12], &sf
->uc
.uc_mcontext
.arm_ip
, err
);
437 __put_user_error(regs
->compat_sp
, &sf
->uc
.uc_mcontext
.arm_sp
, err
);
438 __put_user_error(regs
->compat_lr
, &sf
->uc
.uc_mcontext
.arm_lr
, err
);
439 __put_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.arm_pc
, err
);
440 __put_user_error(psr
, &sf
->uc
.uc_mcontext
.arm_cpsr
, err
);
442 __put_user_error((compat_ulong_t
)0, &sf
->uc
.uc_mcontext
.trap_no
, err
);
443 /* set the compat FSR WnR */
444 __put_user_error(!!(current
->thread
.fault_code
& ESR_ELx_WNR
) <<
445 FSR_WRITE_SHIFT
, &sf
->uc
.uc_mcontext
.error_code
, err
);
446 __put_user_error(current
->thread
.fault_address
, &sf
->uc
.uc_mcontext
.fault_address
, err
);
447 __put_user_error(set
->sig
[0], &sf
->uc
.uc_mcontext
.oldmask
, err
);
449 err
|= put_sigset_t(&sf
->uc
.uc_sigmask
, set
);
451 aux
= (struct compat_aux_sigframe __user
*) sf
->uc
.uc_regspace
;
454 err
|= compat_preserve_vfp_context(&aux
->vfp
);
455 __put_user_error(0, &aux
->end_magic
, err
);
461 * 32-bit signal handling routines called from signal.c
463 int compat_setup_rt_frame(int usig
, struct ksignal
*ksig
,
464 sigset_t
*set
, struct pt_regs
*regs
)
466 struct compat_rt_sigframe __user
*frame
;
469 frame
= compat_get_sigframe(ksig
, regs
, sizeof(*frame
));
474 err
|= copy_siginfo_to_user32(&frame
->info
, &ksig
->info
);
476 __put_user_error(0, &frame
->sig
.uc
.uc_flags
, err
);
477 __put_user_error(0, &frame
->sig
.uc
.uc_link
, err
);
479 err
|= __compat_save_altstack(&frame
->sig
.uc
.uc_stack
, regs
->compat_sp
);
481 err
|= compat_setup_sigframe(&frame
->sig
, regs
, set
);
484 compat_setup_return(regs
, &ksig
->ka
, frame
->sig
.retcode
, frame
, usig
);
485 regs
->regs
[1] = (compat_ulong_t
)(unsigned long)&frame
->info
;
486 regs
->regs
[2] = (compat_ulong_t
)(unsigned long)&frame
->sig
.uc
;
492 int compat_setup_frame(int usig
, struct ksignal
*ksig
, sigset_t
*set
,
493 struct pt_regs
*regs
)
495 struct compat_sigframe __user
*frame
;
498 frame
= compat_get_sigframe(ksig
, regs
, sizeof(*frame
));
503 __put_user_error(0x5ac3c35a, &frame
->uc
.uc_flags
, err
);
505 err
|= compat_setup_sigframe(frame
, regs
, set
);
507 compat_setup_return(regs
, &ksig
->ka
, frame
->retcode
, frame
, usig
);
512 void compat_setup_restart_syscall(struct pt_regs
*regs
)
514 regs
->regs
[7] = __NR_compat_restart_syscall
;