2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "user-internals.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
24 #include "target/arm/cpu-features.h"
25 #include "vdso-asmoffset.h"
27 struct target_sigcontext
{
48 abi_ulong fault_address
;
51 struct target_ucontext
{
54 target_stack_t tuc_stack
;
55 struct target_sigcontext tuc_mcontext
;
56 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
57 char __unused
[128 - sizeof(target_sigset_t
)];
58 abi_ulong tuc_regspace
[128] __attribute__((__aligned__(8)));
61 struct target_user_vfp
{
66 struct target_user_vfp_exc
{
72 struct target_vfp_sigframe
{
75 struct target_user_vfp ufp
;
76 struct target_user_vfp_exc ufp_exc
;
77 } __attribute__((__aligned__(8)));
79 struct target_iwmmxt_sigframe
{
83 /* Note that not all the coprocessor control registers are stored here */
90 } __attribute__((__aligned__(8)));
92 #define TARGET_VFP_MAGIC 0x56465001
93 #define TARGET_IWMMXT_MAGIC 0x12ef842a
97 struct target_ucontext uc
;
103 struct target_siginfo info
;
107 QEMU_BUILD_BUG_ON(offsetof(struct sigframe
, retcode
[3])
108 != SIGFRAME_RC3_OFFSET
);
109 QEMU_BUILD_BUG_ON(offsetof(struct rt_sigframe
, sig
.retcode
[3])
110 != RT_SIGFRAME_RC3_OFFSET
);
112 static abi_ptr sigreturn_fdpic_tramp
;
115 * Up to 3 words of 'retcode' in the sigframe are code,
116 * with retcode[3] being used by fdpic for the function descriptor.
117 * This code is not actually executed, but is retained for ABI compat.
119 * We will create a table of 8 retcode variants in the sigtramp page.
120 * Let each table entry use 3 words.
122 #define RETCODE_WORDS 3
123 #define RETCODE_BYTES (RETCODE_WORDS * 4)
125 static inline int valid_user_regs(CPUARMState
*regs
)
131 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
132 CPUARMState
*env
, abi_ulong mask
)
134 __put_user(env
->regs
[0], &sc
->arm_r0
);
135 __put_user(env
->regs
[1], &sc
->arm_r1
);
136 __put_user(env
->regs
[2], &sc
->arm_r2
);
137 __put_user(env
->regs
[3], &sc
->arm_r3
);
138 __put_user(env
->regs
[4], &sc
->arm_r4
);
139 __put_user(env
->regs
[5], &sc
->arm_r5
);
140 __put_user(env
->regs
[6], &sc
->arm_r6
);
141 __put_user(env
->regs
[7], &sc
->arm_r7
);
142 __put_user(env
->regs
[8], &sc
->arm_r8
);
143 __put_user(env
->regs
[9], &sc
->arm_r9
);
144 __put_user(env
->regs
[10], &sc
->arm_r10
);
145 __put_user(env
->regs
[11], &sc
->arm_fp
);
146 __put_user(env
->regs
[12], &sc
->arm_ip
);
147 __put_user(env
->regs
[13], &sc
->arm_sp
);
148 __put_user(env
->regs
[14], &sc
->arm_lr
);
149 __put_user(env
->regs
[15], &sc
->arm_pc
);
150 __put_user(cpsr_read(env
), &sc
->arm_cpsr
);
152 __put_user(/* current->thread.trap_no */ 0, &sc
->trap_no
);
153 __put_user(/* current->thread.error_code */ 0, &sc
->error_code
);
154 __put_user(/* current->thread.address */ 0, &sc
->fault_address
);
155 __put_user(mask
, &sc
->oldmask
);
158 static inline abi_ulong
159 get_sigframe(struct target_sigaction
*ka
, CPUARMState
*regs
, int framesize
)
163 sp
= target_sigsp(get_sp_from_cpustate(regs
), ka
);
165 * ATPCS B01 mandates 8-byte alignment
167 return (sp
- framesize
) & ~7;
170 static void write_arm_sigreturn(uint32_t *rc
, int syscall
);
171 static void write_arm_fdpic_sigreturn(uint32_t *rc
, int ofs
);
174 setup_return(CPUARMState
*env
, struct target_sigaction
*ka
, int usig
,
175 struct sigframe
*frame
, abi_ulong sp_addr
)
177 abi_ulong handler
= 0;
178 abi_ulong handler_fdpic_GOT
= 0;
180 bool is_fdpic
= info_is_fdpic(get_task_state(thread_cpu
)->info
);
181 bool is_rt
= ka
->sa_flags
& TARGET_SA_SIGINFO
;
185 /* In FDPIC mode, ka->_sa_handler points to a function
186 * descriptor (FD). The first word contains the address of the
187 * handler. The second word contains the value of the PIC
189 abi_ulong funcdesc_ptr
= ka
->_sa_handler
;
190 if (get_user_ual(handler
, funcdesc_ptr
)
191 || get_user_ual(handler_fdpic_GOT
, funcdesc_ptr
+ 4)) {
195 handler
= ka
->_sa_handler
;
199 uint32_t cpsr
= cpsr_read(env
);
207 if (env
->cp15
.sctlr_el
[1] & SCTLR_E0E
) {
213 /* Our vdso default_sigreturn label is a table of entry points. */
214 retcode
= default_sigreturn
+ (is_fdpic
* 2 + is_rt
) * 8;
217 * Put the sigreturn code on the stack no matter which return
218 * mechanism we use in order to remain ABI compliant.
219 * Because this is about ABI, always use the A32 instructions,
220 * despite the fact that our actual vdso trampoline is T16.
223 write_arm_fdpic_sigreturn(frame
->retcode
,
224 is_rt
? RT_SIGFRAME_RC3_OFFSET
225 : SIGFRAME_RC3_OFFSET
);
227 write_arm_sigreturn(frame
->retcode
,
228 is_rt
? TARGET_NR_rt_sigreturn
229 : TARGET_NR_sigreturn
);
232 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
234 /* Place the function descriptor in slot 3. */
235 __put_user((abi_ulong
)ka
->sa_restorer
, &frame
->retcode
[3]);
237 retcode
= ka
->sa_restorer
;
243 env
->regs
[9] = handler_fdpic_GOT
;
245 env
->regs
[13] = sp_addr
;
246 env
->regs
[14] = retcode
;
247 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
248 cpsr_write(env
, cpsr
, CPSR_IT
| CPSR_T
| CPSR_E
, CPSRWriteByInstr
);
253 static abi_ulong
*setup_sigframe_vfp(abi_ulong
*regspace
, CPUARMState
*env
)
256 struct target_vfp_sigframe
*vfpframe
;
257 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
258 __put_user(TARGET_VFP_MAGIC
, &vfpframe
->magic
);
259 __put_user(sizeof(*vfpframe
), &vfpframe
->size
);
260 for (i
= 0; i
< 32; i
++) {
261 __put_user(*aa32_vfp_dreg(env
, i
), &vfpframe
->ufp
.fpregs
[i
]);
263 __put_user(vfp_get_fpscr(env
), &vfpframe
->ufp
.fpscr
);
264 __put_user(env
->vfp
.xregs
[ARM_VFP_FPEXC
], &vfpframe
->ufp_exc
.fpexc
);
265 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
266 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
267 return (abi_ulong
*)(vfpframe
+1);
270 static abi_ulong
*setup_sigframe_iwmmxt(abi_ulong
*regspace
, CPUARMState
*env
)
273 struct target_iwmmxt_sigframe
*iwmmxtframe
;
274 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
275 __put_user(TARGET_IWMMXT_MAGIC
, &iwmmxtframe
->magic
);
276 __put_user(sizeof(*iwmmxtframe
), &iwmmxtframe
->size
);
277 for (i
= 0; i
< 16; i
++) {
278 __put_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
280 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
281 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
282 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
283 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
284 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
285 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
286 return (abi_ulong
*)(iwmmxtframe
+1);
289 static void setup_sigframe(struct target_ucontext
*uc
,
290 target_sigset_t
*set
, CPUARMState
*env
)
292 struct target_sigaltstack stack
;
296 /* Clear all the bits of the ucontext we don't use. */
297 memset(uc
, 0, offsetof(struct target_ucontext
, tuc_mcontext
));
299 memset(&stack
, 0, sizeof(stack
));
300 target_save_altstack(&stack
, env
);
301 memcpy(&uc
->tuc_stack
, &stack
, sizeof(stack
));
303 setup_sigcontext(&uc
->tuc_mcontext
, env
, set
->sig
[0]);
304 /* Save coprocessor signal frame. */
305 regspace
= uc
->tuc_regspace
;
306 if (cpu_isar_feature(aa32_vfp_simd
, env_archcpu(env
))) {
307 regspace
= setup_sigframe_vfp(regspace
, env
);
309 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
310 regspace
= setup_sigframe_iwmmxt(regspace
, env
);
313 /* Write terminating magic word */
314 __put_user(0, regspace
);
316 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
317 __put_user(set
->sig
[i
], &uc
->tuc_sigmask
.sig
[i
]);
321 void setup_frame(int usig
, struct target_sigaction
*ka
,
322 target_sigset_t
*set
, CPUARMState
*regs
)
324 struct sigframe
*frame
;
325 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
327 trace_user_setup_frame(regs
, frame_addr
);
328 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
332 setup_sigframe(&frame
->uc
, set
, regs
);
334 if (setup_return(regs
, ka
, usig
, frame
, frame_addr
)) {
338 unlock_user_struct(frame
, frame_addr
, 1);
341 unlock_user_struct(frame
, frame_addr
, 1);
345 void setup_rt_frame(int usig
, struct target_sigaction
*ka
,
346 target_siginfo_t
*info
,
347 target_sigset_t
*set
, CPUARMState
*env
)
349 struct rt_sigframe
*frame
;
350 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
351 abi_ulong info_addr
, uc_addr
;
353 trace_user_setup_rt_frame(env
, frame_addr
);
354 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
358 info_addr
= frame_addr
+ offsetof(struct rt_sigframe
, info
);
359 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe
, sig
.uc
);
362 setup_sigframe(&frame
->sig
.uc
, set
, env
);
364 if (setup_return(env
, ka
, usig
, &frame
->sig
, frame_addr
)) {
368 env
->regs
[1] = info_addr
;
369 env
->regs
[2] = uc_addr
;
371 unlock_user_struct(frame
, frame_addr
, 1);
374 unlock_user_struct(frame
, frame_addr
, 1);
379 restore_sigcontext(CPUARMState
*env
, struct target_sigcontext
*sc
)
384 __get_user(env
->regs
[0], &sc
->arm_r0
);
385 __get_user(env
->regs
[1], &sc
->arm_r1
);
386 __get_user(env
->regs
[2], &sc
->arm_r2
);
387 __get_user(env
->regs
[3], &sc
->arm_r3
);
388 __get_user(env
->regs
[4], &sc
->arm_r4
);
389 __get_user(env
->regs
[5], &sc
->arm_r5
);
390 __get_user(env
->regs
[6], &sc
->arm_r6
);
391 __get_user(env
->regs
[7], &sc
->arm_r7
);
392 __get_user(env
->regs
[8], &sc
->arm_r8
);
393 __get_user(env
->regs
[9], &sc
->arm_r9
);
394 __get_user(env
->regs
[10], &sc
->arm_r10
);
395 __get_user(env
->regs
[11], &sc
->arm_fp
);
396 __get_user(env
->regs
[12], &sc
->arm_ip
);
397 __get_user(env
->regs
[13], &sc
->arm_sp
);
398 __get_user(env
->regs
[14], &sc
->arm_lr
);
399 __get_user(env
->regs
[15], &sc
->arm_pc
);
400 __get_user(cpsr
, &sc
->arm_cpsr
);
401 cpsr_write(env
, cpsr
, CPSR_USER
| CPSR_EXEC
, CPSRWriteByInstr
);
403 err
|= !valid_user_regs(env
);
408 static abi_ulong
*restore_sigframe_vfp(CPUARMState
*env
, abi_ulong
*regspace
)
412 uint32_t fpscr
, fpexc
;
413 struct target_vfp_sigframe
*vfpframe
;
414 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
416 __get_user(magic
, &vfpframe
->magic
);
417 __get_user(sz
, &vfpframe
->size
);
418 if (magic
!= TARGET_VFP_MAGIC
|| sz
!= sizeof(*vfpframe
)) {
421 for (i
= 0; i
< 32; i
++) {
422 __get_user(*aa32_vfp_dreg(env
, i
), &vfpframe
->ufp
.fpregs
[i
]);
424 __get_user(fpscr
, &vfpframe
->ufp
.fpscr
);
425 vfp_set_fpscr(env
, fpscr
);
426 __get_user(fpexc
, &vfpframe
->ufp_exc
.fpexc
);
427 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
428 * and the exception flag is cleared
431 fpexc
&= ~((1 << 31) | (1 << 28));
432 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = fpexc
;
433 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
434 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
435 return (abi_ulong
*)(vfpframe
+ 1);
438 static abi_ulong
*restore_sigframe_iwmmxt(CPUARMState
*env
,
443 struct target_iwmmxt_sigframe
*iwmmxtframe
;
444 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
446 __get_user(magic
, &iwmmxtframe
->magic
);
447 __get_user(sz
, &iwmmxtframe
->size
);
448 if (magic
!= TARGET_IWMMXT_MAGIC
|| sz
!= sizeof(*iwmmxtframe
)) {
451 for (i
= 0; i
< 16; i
++) {
452 __get_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
454 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
455 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
456 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
457 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
458 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
459 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
460 return (abi_ulong
*)(iwmmxtframe
+ 1);
463 static int do_sigframe_return(CPUARMState
*env
,
464 target_ulong context_addr
,
465 struct target_ucontext
*uc
)
470 target_to_host_sigset(&host_set
, &uc
->tuc_sigmask
);
471 set_sigmask(&host_set
);
473 if (restore_sigcontext(env
, &uc
->tuc_mcontext
)) {
477 /* Restore coprocessor signal frame */
478 regspace
= uc
->tuc_regspace
;
479 if (cpu_isar_feature(aa32_vfp_simd
, env_archcpu(env
))) {
480 regspace
= restore_sigframe_vfp(env
, regspace
);
485 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
486 regspace
= restore_sigframe_iwmmxt(env
, regspace
);
492 target_restore_altstack(&uc
->tuc_stack
, env
);
495 /* Send SIGTRAP if we're single-stepping */
496 if (ptrace_cancel_bpt(current
))
497 send_sig(SIGTRAP
, current
, 1);
503 long do_sigreturn(CPUARMState
*env
)
505 abi_ulong frame_addr
;
506 struct sigframe
*frame
= NULL
;
509 * Since we stacked the signal on a 64-bit boundary,
510 * then 'sp' should be word aligned here. If it's
511 * not, then the user is trying to mess with us.
513 frame_addr
= env
->regs
[13];
514 trace_user_do_sigreturn(env
, frame_addr
);
515 if (frame_addr
& 7) {
519 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
523 if (do_sigframe_return(env
,
524 frame_addr
+ offsetof(struct sigframe
, uc
),
529 unlock_user_struct(frame
, frame_addr
, 0);
530 return -QEMU_ESIGRETURN
;
533 unlock_user_struct(frame
, frame_addr
, 0);
534 force_sig(TARGET_SIGSEGV
);
535 return -QEMU_ESIGRETURN
;
538 long do_rt_sigreturn(CPUARMState
*env
)
540 abi_ulong frame_addr
;
541 struct rt_sigframe
*frame
= NULL
;
544 * Since we stacked the signal on a 64-bit boundary,
545 * then 'sp' should be word aligned here. If it's
546 * not, then the user is trying to mess with us.
548 frame_addr
= env
->regs
[13];
549 trace_user_do_rt_sigreturn(env
, frame_addr
);
550 if (frame_addr
& 7) {
554 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
558 if (do_sigframe_return(env
,
559 frame_addr
+ offsetof(struct rt_sigframe
, sig
.uc
),
564 unlock_user_struct(frame
, frame_addr
, 0);
565 return -QEMU_ESIGRETURN
;
568 unlock_user_struct(frame
, frame_addr
, 0);
569 force_sig(TARGET_SIGSEGV
);
570 return -QEMU_ESIGRETURN
;
574 * EABI syscalls pass the number via r7.
575 * Note that the kernel still adds the OABI syscall number to the trap,
576 * presumably for backward ABI compatibility with unwinders.
578 #define ARM_MOV_R7_IMM(X) (0xe3a07000 | (X))
579 #define ARM_SWI_SYS(X) (0xef000000 | (X) | ARM_SYSCALL_BASE)
581 #define THUMB_MOVS_R7_IMM(X) (0x2700 | (X))
582 #define THUMB_SWI_SYS 0xdf00
584 static void write_arm_sigreturn(uint32_t *rc
, int syscall
)
586 __put_user(ARM_MOV_R7_IMM(syscall
), rc
);
587 __put_user(ARM_SWI_SYS(syscall
), rc
+ 1);
588 /* Wrote 8 of 12 bytes */
591 static void write_thm_sigreturn(uint32_t *rc
, int syscall
)
593 __put_user(THUMB_SWI_SYS
<< 16 | THUMB_MOVS_R7_IMM(syscall
), rc
);
594 /* Wrote 4 of 12 bytes */
598 * Stub needed to make sure the FD register (r9) contains the right value.
599 * Use the same instruction sequence as the kernel.
601 static void write_arm_fdpic_sigreturn(uint32_t *rc
, int ofs
)
603 assert(ofs
<= 0xfff);
604 __put_user(0xe59d3000 | ofs
, rc
+ 0); /* ldr r3, [sp, #ofs] */
605 __put_user(0xe8930908, rc
+ 1); /* ldm r3, { r3, r9 } */
606 __put_user(0xe12fff13, rc
+ 2); /* bx r3 */
607 /* Wrote 12 of 12 bytes */
610 static void write_thm_fdpic_sigreturn(void *vrc
, int ofs
)
614 assert((ofs
& ~0x3fc) == 0);
615 __put_user(0x9b00 | (ofs
>> 2), rc
+ 0); /* ldr r3, [sp, #ofs] */
616 __put_user(0xcb0c, rc
+ 1); /* ldm r3, { r2, r3 } */
617 __put_user(0x4699, rc
+ 2); /* mov r9, r3 */
618 __put_user(0x4710, rc
+ 3); /* bx r2 */
619 /* Wrote 8 of 12 bytes */
622 void setup_sigtramp(abi_ulong sigtramp_page
)
624 uint32_t total_size
= 8 * RETCODE_BYTES
;
625 uint32_t *tramp
= lock_user(VERIFY_WRITE
, sigtramp_page
, total_size
, 0);
627 assert(tramp
!= NULL
);
629 default_sigreturn
= sigtramp_page
;
630 write_arm_sigreturn(&tramp
[0 * RETCODE_WORDS
], TARGET_NR_sigreturn
);
631 write_thm_sigreturn(&tramp
[1 * RETCODE_WORDS
], TARGET_NR_sigreturn
);
632 write_arm_sigreturn(&tramp
[2 * RETCODE_WORDS
], TARGET_NR_rt_sigreturn
);
633 write_thm_sigreturn(&tramp
[3 * RETCODE_WORDS
], TARGET_NR_rt_sigreturn
);
635 sigreturn_fdpic_tramp
= sigtramp_page
+ 4 * RETCODE_BYTES
;
636 write_arm_fdpic_sigreturn(tramp
+ 4 * RETCODE_WORDS
,
637 offsetof(struct sigframe
, retcode
[3]));
638 write_thm_fdpic_sigreturn(tramp
+ 5 * RETCODE_WORDS
,
639 offsetof(struct sigframe
, retcode
[3]));
640 write_arm_fdpic_sigreturn(tramp
+ 6 * RETCODE_WORDS
,
641 offsetof(struct rt_sigframe
, sig
.retcode
[3]));
642 write_thm_fdpic_sigreturn(tramp
+ 7 * RETCODE_WORDS
,
643 offsetof(struct rt_sigframe
, sig
.retcode
[3]));
645 unlock_user(tramp
, sigtramp_page
, total_size
);