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"
26 struct target_sigcontext
{
47 abi_ulong fault_address
;
50 struct target_ucontext
{
53 target_stack_t tuc_stack
;
54 struct target_sigcontext tuc_mcontext
;
55 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
56 char __unused
[128 - sizeof(target_sigset_t
)];
57 abi_ulong tuc_regspace
[128] __attribute__((__aligned__(8)));
60 struct target_user_vfp
{
65 struct target_user_vfp_exc
{
71 struct target_vfp_sigframe
{
74 struct target_user_vfp ufp
;
75 struct target_user_vfp_exc ufp_exc
;
76 } __attribute__((__aligned__(8)));
78 struct target_iwmmxt_sigframe
{
82 /* Note that not all the coprocessor control registers are stored here */
89 } __attribute__((__aligned__(8)));
91 #define TARGET_VFP_MAGIC 0x56465001
92 #define TARGET_IWMMXT_MAGIC 0x12ef842a
96 struct target_ucontext uc
;
102 struct target_siginfo info
;
106 static abi_ptr sigreturn_fdpic_tramp
;
109 * Up to 3 words of 'retcode' in the sigframe are code,
110 * with retcode[3] being used by fdpic for the function descriptor.
111 * This code is not actually executed, but is retained for ABI compat.
113 * We will create a table of 8 retcode variants in the sigtramp page.
114 * Let each table entry use 3 words.
116 #define RETCODE_WORDS 3
117 #define RETCODE_BYTES (RETCODE_WORDS * 4)
119 static inline int valid_user_regs(CPUARMState
*regs
)
125 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
126 CPUARMState
*env
, abi_ulong mask
)
128 __put_user(env
->regs
[0], &sc
->arm_r0
);
129 __put_user(env
->regs
[1], &sc
->arm_r1
);
130 __put_user(env
->regs
[2], &sc
->arm_r2
);
131 __put_user(env
->regs
[3], &sc
->arm_r3
);
132 __put_user(env
->regs
[4], &sc
->arm_r4
);
133 __put_user(env
->regs
[5], &sc
->arm_r5
);
134 __put_user(env
->regs
[6], &sc
->arm_r6
);
135 __put_user(env
->regs
[7], &sc
->arm_r7
);
136 __put_user(env
->regs
[8], &sc
->arm_r8
);
137 __put_user(env
->regs
[9], &sc
->arm_r9
);
138 __put_user(env
->regs
[10], &sc
->arm_r10
);
139 __put_user(env
->regs
[11], &sc
->arm_fp
);
140 __put_user(env
->regs
[12], &sc
->arm_ip
);
141 __put_user(env
->regs
[13], &sc
->arm_sp
);
142 __put_user(env
->regs
[14], &sc
->arm_lr
);
143 __put_user(env
->regs
[15], &sc
->arm_pc
);
144 __put_user(cpsr_read(env
), &sc
->arm_cpsr
);
146 __put_user(/* current->thread.trap_no */ 0, &sc
->trap_no
);
147 __put_user(/* current->thread.error_code */ 0, &sc
->error_code
);
148 __put_user(/* current->thread.address */ 0, &sc
->fault_address
);
149 __put_user(mask
, &sc
->oldmask
);
152 static inline abi_ulong
153 get_sigframe(struct target_sigaction
*ka
, CPUARMState
*regs
, int framesize
)
157 sp
= target_sigsp(get_sp_from_cpustate(regs
), ka
);
159 * ATPCS B01 mandates 8-byte alignment
161 return (sp
- framesize
) & ~7;
165 setup_return(CPUARMState
*env
, struct target_sigaction
*ka
, int usig
,
166 struct sigframe
*frame
, abi_ulong sp_addr
)
168 abi_ulong handler
= 0;
169 abi_ulong handler_fdpic_GOT
= 0;
171 int thumb
, retcode_idx
;
172 int is_fdpic
= info_is_fdpic(((TaskState
*)thread_cpu
->opaque
)->info
);
176 /* In FDPIC mode, ka->_sa_handler points to a function
177 * descriptor (FD). The first word contains the address of the
178 * handler. The second word contains the value of the PIC
180 abi_ulong funcdesc_ptr
= ka
->_sa_handler
;
181 if (get_user_ual(handler
, funcdesc_ptr
)
182 || get_user_ual(handler_fdpic_GOT
, funcdesc_ptr
+ 4)) {
186 handler
= ka
->_sa_handler
;
190 retcode_idx
= thumb
+ (ka
->sa_flags
& TARGET_SA_SIGINFO
? 2 : 0);
192 uint32_t cpsr
= cpsr_read(env
);
200 if (env
->cp15
.sctlr_el
[1] & SCTLR_E0E
) {
206 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
208 __put_user((abi_ulong
)ka
->sa_restorer
, &frame
->retcode
[3]);
209 retcode
= (sigreturn_fdpic_tramp
+
210 retcode_idx
* RETCODE_BYTES
+ thumb
);
213 retcode
= ka
->sa_restorer
;
214 copy_retcode
= false;
217 retcode
= default_sigreturn
+ retcode_idx
* RETCODE_BYTES
+ thumb
;
221 /* Copy the code to the stack slot for ABI compatibility. */
223 memcpy(frame
->retcode
, g2h_untagged(retcode
& ~1), RETCODE_BYTES
);
228 env
->regs
[9] = handler_fdpic_GOT
;
230 env
->regs
[13] = sp_addr
;
231 env
->regs
[14] = retcode
;
232 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
233 cpsr_write(env
, cpsr
, CPSR_IT
| CPSR_T
| CPSR_E
, CPSRWriteByInstr
);
238 static abi_ulong
*setup_sigframe_vfp(abi_ulong
*regspace
, CPUARMState
*env
)
241 struct target_vfp_sigframe
*vfpframe
;
242 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
243 __put_user(TARGET_VFP_MAGIC
, &vfpframe
->magic
);
244 __put_user(sizeof(*vfpframe
), &vfpframe
->size
);
245 for (i
= 0; i
< 32; i
++) {
246 __put_user(*aa32_vfp_dreg(env
, i
), &vfpframe
->ufp
.fpregs
[i
]);
248 __put_user(vfp_get_fpscr(env
), &vfpframe
->ufp
.fpscr
);
249 __put_user(env
->vfp
.xregs
[ARM_VFP_FPEXC
], &vfpframe
->ufp_exc
.fpexc
);
250 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
251 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
252 return (abi_ulong
*)(vfpframe
+1);
255 static abi_ulong
*setup_sigframe_iwmmxt(abi_ulong
*regspace
, CPUARMState
*env
)
258 struct target_iwmmxt_sigframe
*iwmmxtframe
;
259 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
260 __put_user(TARGET_IWMMXT_MAGIC
, &iwmmxtframe
->magic
);
261 __put_user(sizeof(*iwmmxtframe
), &iwmmxtframe
->size
);
262 for (i
= 0; i
< 16; i
++) {
263 __put_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
265 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
266 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
267 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
268 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
269 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
270 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
271 return (abi_ulong
*)(iwmmxtframe
+1);
274 static void setup_sigframe(struct target_ucontext
*uc
,
275 target_sigset_t
*set
, CPUARMState
*env
)
277 struct target_sigaltstack stack
;
281 /* Clear all the bits of the ucontext we don't use. */
282 memset(uc
, 0, offsetof(struct target_ucontext
, tuc_mcontext
));
284 memset(&stack
, 0, sizeof(stack
));
285 target_save_altstack(&stack
, env
);
286 memcpy(&uc
->tuc_stack
, &stack
, sizeof(stack
));
288 setup_sigcontext(&uc
->tuc_mcontext
, env
, set
->sig
[0]);
289 /* Save coprocessor signal frame. */
290 regspace
= uc
->tuc_regspace
;
291 if (cpu_isar_feature(aa32_vfp_simd
, env_archcpu(env
))) {
292 regspace
= setup_sigframe_vfp(regspace
, env
);
294 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
295 regspace
= setup_sigframe_iwmmxt(regspace
, env
);
298 /* Write terminating magic word */
299 __put_user(0, regspace
);
301 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
302 __put_user(set
->sig
[i
], &uc
->tuc_sigmask
.sig
[i
]);
306 void setup_frame(int usig
, struct target_sigaction
*ka
,
307 target_sigset_t
*set
, CPUARMState
*regs
)
309 struct sigframe
*frame
;
310 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
312 trace_user_setup_frame(regs
, frame_addr
);
313 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
317 setup_sigframe(&frame
->uc
, set
, regs
);
319 if (setup_return(regs
, ka
, usig
, frame
, frame_addr
)) {
323 unlock_user_struct(frame
, frame_addr
, 1);
326 unlock_user_struct(frame
, frame_addr
, 1);
330 void setup_rt_frame(int usig
, struct target_sigaction
*ka
,
331 target_siginfo_t
*info
,
332 target_sigset_t
*set
, CPUARMState
*env
)
334 struct rt_sigframe
*frame
;
335 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
336 abi_ulong info_addr
, uc_addr
;
338 trace_user_setup_rt_frame(env
, frame_addr
);
339 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
343 info_addr
= frame_addr
+ offsetof(struct rt_sigframe
, info
);
344 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe
, sig
.uc
);
345 tswap_siginfo(&frame
->info
, info
);
347 setup_sigframe(&frame
->sig
.uc
, set
, env
);
349 if (setup_return(env
, ka
, usig
, &frame
->sig
, frame_addr
)) {
353 env
->regs
[1] = info_addr
;
354 env
->regs
[2] = uc_addr
;
356 unlock_user_struct(frame
, frame_addr
, 1);
359 unlock_user_struct(frame
, frame_addr
, 1);
364 restore_sigcontext(CPUARMState
*env
, struct target_sigcontext
*sc
)
369 __get_user(env
->regs
[0], &sc
->arm_r0
);
370 __get_user(env
->regs
[1], &sc
->arm_r1
);
371 __get_user(env
->regs
[2], &sc
->arm_r2
);
372 __get_user(env
->regs
[3], &sc
->arm_r3
);
373 __get_user(env
->regs
[4], &sc
->arm_r4
);
374 __get_user(env
->regs
[5], &sc
->arm_r5
);
375 __get_user(env
->regs
[6], &sc
->arm_r6
);
376 __get_user(env
->regs
[7], &sc
->arm_r7
);
377 __get_user(env
->regs
[8], &sc
->arm_r8
);
378 __get_user(env
->regs
[9], &sc
->arm_r9
);
379 __get_user(env
->regs
[10], &sc
->arm_r10
);
380 __get_user(env
->regs
[11], &sc
->arm_fp
);
381 __get_user(env
->regs
[12], &sc
->arm_ip
);
382 __get_user(env
->regs
[13], &sc
->arm_sp
);
383 __get_user(env
->regs
[14], &sc
->arm_lr
);
384 __get_user(env
->regs
[15], &sc
->arm_pc
);
385 __get_user(cpsr
, &sc
->arm_cpsr
);
386 cpsr_write(env
, cpsr
, CPSR_USER
| CPSR_EXEC
, CPSRWriteByInstr
);
388 err
|= !valid_user_regs(env
);
393 static abi_ulong
*restore_sigframe_vfp(CPUARMState
*env
, abi_ulong
*regspace
)
397 uint32_t fpscr
, fpexc
;
398 struct target_vfp_sigframe
*vfpframe
;
399 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
401 __get_user(magic
, &vfpframe
->magic
);
402 __get_user(sz
, &vfpframe
->size
);
403 if (magic
!= TARGET_VFP_MAGIC
|| sz
!= sizeof(*vfpframe
)) {
406 for (i
= 0; i
< 32; i
++) {
407 __get_user(*aa32_vfp_dreg(env
, i
), &vfpframe
->ufp
.fpregs
[i
]);
409 __get_user(fpscr
, &vfpframe
->ufp
.fpscr
);
410 vfp_set_fpscr(env
, fpscr
);
411 __get_user(fpexc
, &vfpframe
->ufp_exc
.fpexc
);
412 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
413 * and the exception flag is cleared
416 fpexc
&= ~((1 << 31) | (1 << 28));
417 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = fpexc
;
418 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
419 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
420 return (abi_ulong
*)(vfpframe
+ 1);
423 static abi_ulong
*restore_sigframe_iwmmxt(CPUARMState
*env
,
428 struct target_iwmmxt_sigframe
*iwmmxtframe
;
429 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
431 __get_user(magic
, &iwmmxtframe
->magic
);
432 __get_user(sz
, &iwmmxtframe
->size
);
433 if (magic
!= TARGET_IWMMXT_MAGIC
|| sz
!= sizeof(*iwmmxtframe
)) {
436 for (i
= 0; i
< 16; i
++) {
437 __get_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
439 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
440 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
441 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
442 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
443 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
444 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
445 return (abi_ulong
*)(iwmmxtframe
+ 1);
448 static int do_sigframe_return(CPUARMState
*env
,
449 target_ulong context_addr
,
450 struct target_ucontext
*uc
)
455 target_to_host_sigset(&host_set
, &uc
->tuc_sigmask
);
456 set_sigmask(&host_set
);
458 if (restore_sigcontext(env
, &uc
->tuc_mcontext
)) {
462 /* Restore coprocessor signal frame */
463 regspace
= uc
->tuc_regspace
;
464 if (cpu_isar_feature(aa32_vfp_simd
, env_archcpu(env
))) {
465 regspace
= restore_sigframe_vfp(env
, regspace
);
470 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
471 regspace
= restore_sigframe_iwmmxt(env
, regspace
);
477 target_restore_altstack(&uc
->tuc_stack
, env
);
480 /* Send SIGTRAP if we're single-stepping */
481 if (ptrace_cancel_bpt(current
))
482 send_sig(SIGTRAP
, current
, 1);
488 long do_sigreturn(CPUARMState
*env
)
490 abi_ulong frame_addr
;
491 struct sigframe
*frame
= NULL
;
494 * Since we stacked the signal on a 64-bit boundary,
495 * then 'sp' should be word aligned here. If it's
496 * not, then the user is trying to mess with us.
498 frame_addr
= env
->regs
[13];
499 trace_user_do_sigreturn(env
, frame_addr
);
500 if (frame_addr
& 7) {
504 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
508 if (do_sigframe_return(env
,
509 frame_addr
+ offsetof(struct sigframe
, uc
),
514 unlock_user_struct(frame
, frame_addr
, 0);
515 return -QEMU_ESIGRETURN
;
518 unlock_user_struct(frame
, frame_addr
, 0);
519 force_sig(TARGET_SIGSEGV
);
520 return -QEMU_ESIGRETURN
;
523 long do_rt_sigreturn(CPUARMState
*env
)
525 abi_ulong frame_addr
;
526 struct rt_sigframe
*frame
= NULL
;
529 * Since we stacked the signal on a 64-bit boundary,
530 * then 'sp' should be word aligned here. If it's
531 * not, then the user is trying to mess with us.
533 frame_addr
= env
->regs
[13];
534 trace_user_do_rt_sigreturn(env
, frame_addr
);
535 if (frame_addr
& 7) {
539 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
543 if (do_sigframe_return(env
,
544 frame_addr
+ offsetof(struct rt_sigframe
, sig
.uc
),
549 unlock_user_struct(frame
, frame_addr
, 0);
550 return -QEMU_ESIGRETURN
;
553 unlock_user_struct(frame
, frame_addr
, 0);
554 force_sig(TARGET_SIGSEGV
);
555 return -QEMU_ESIGRETURN
;
559 * EABI syscalls pass the number via r7.
560 * Note that the kernel still adds the OABI syscall number to the trap,
561 * presumably for backward ABI compatibility with unwinders.
563 #define ARM_MOV_R7_IMM(X) (0xe3a07000 | (X))
564 #define ARM_SWI_SYS(X) (0xef000000 | (X) | ARM_SYSCALL_BASE)
566 #define THUMB_MOVS_R7_IMM(X) (0x2700 | (X))
567 #define THUMB_SWI_SYS 0xdf00
569 static void write_arm_sigreturn(uint32_t *rc
, int syscall
)
571 __put_user(ARM_MOV_R7_IMM(syscall
), rc
);
572 __put_user(ARM_SWI_SYS(syscall
), rc
+ 1);
573 /* Wrote 8 of 12 bytes */
576 static void write_thm_sigreturn(uint32_t *rc
, int syscall
)
578 __put_user(THUMB_SWI_SYS
<< 16 | THUMB_MOVS_R7_IMM(syscall
), rc
);
579 /* Wrote 4 of 12 bytes */
583 * Stub needed to make sure the FD register (r9) contains the right value.
584 * Use the same instruction sequence as the kernel.
586 static void write_arm_fdpic_sigreturn(uint32_t *rc
, int ofs
)
588 assert(ofs
<= 0xfff);
589 __put_user(0xe59d3000 | ofs
, rc
+ 0); /* ldr r3, [sp, #ofs] */
590 __put_user(0xe8930908, rc
+ 1); /* ldm r3, { r3, r9 } */
591 __put_user(0xe12fff13, rc
+ 2); /* bx r3 */
592 /* Wrote 12 of 12 bytes */
595 static void write_thm_fdpic_sigreturn(void *vrc
, int ofs
)
599 assert((ofs
& ~0x3fc) == 0);
600 __put_user(0x9b00 | (ofs
>> 2), rc
+ 0); /* ldr r3, [sp, #ofs] */
601 __put_user(0xcb0c, rc
+ 1); /* ldm r3, { r2, r3 } */
602 __put_user(0x4699, rc
+ 2); /* mov r9, r3 */
603 __put_user(0x4710, rc
+ 3); /* bx r2 */
604 /* Wrote 8 of 12 bytes */
607 void setup_sigtramp(abi_ulong sigtramp_page
)
609 uint32_t total_size
= 8 * RETCODE_BYTES
;
610 uint32_t *tramp
= lock_user(VERIFY_WRITE
, sigtramp_page
, total_size
, 0);
612 assert(tramp
!= NULL
);
614 default_sigreturn
= sigtramp_page
;
615 write_arm_sigreturn(&tramp
[0 * RETCODE_WORDS
], TARGET_NR_sigreturn
);
616 write_thm_sigreturn(&tramp
[1 * RETCODE_WORDS
], TARGET_NR_sigreturn
);
617 write_arm_sigreturn(&tramp
[2 * RETCODE_WORDS
], TARGET_NR_rt_sigreturn
);
618 write_thm_sigreturn(&tramp
[3 * RETCODE_WORDS
], TARGET_NR_rt_sigreturn
);
620 sigreturn_fdpic_tramp
= sigtramp_page
+ 4 * RETCODE_BYTES
;
621 write_arm_fdpic_sigreturn(tramp
+ 4 * RETCODE_WORDS
,
622 offsetof(struct sigframe
, retcode
[3]));
623 write_thm_fdpic_sigreturn(tramp
+ 5 * RETCODE_WORDS
,
624 offsetof(struct sigframe
, retcode
[3]));
625 write_arm_fdpic_sigreturn(tramp
+ 6 * RETCODE_WORDS
,
626 offsetof(struct rt_sigframe
, sig
.retcode
[3]));
627 write_thm_fdpic_sigreturn(tramp
+ 7 * RETCODE_WORDS
,
628 offsetof(struct rt_sigframe
, sig
.retcode
[3]));
630 unlock_user(tramp
, sigtramp_page
, total_size
);