4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "qemu/osdep.h"
10 #include "qemu/units.h"
11 #include "target/arm/idau.h"
14 #include "internals.h"
15 #include "exec/gdbstub.h"
16 #include "exec/helper-proto.h"
17 #include "qemu/host-utils.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/bitops.h"
20 #include "qemu/crc32c.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/exec-all.h"
23 #include <zlib.h> /* For crc32 */
24 #include "hw/semihosting/semihost.h"
25 #include "sysemu/cpus.h"
26 #include "sysemu/kvm.h"
27 #include "qemu/range.h"
28 #include "qapi/qapi-commands-machine-target.h"
29 #include "qapi/error.h"
30 #include "qemu/guest-random.h"
33 #include "exec/cpu_ldst.h"
36 static void v7m_msr_xpsr(CPUARMState
*env
, uint32_t mask
,
37 uint32_t reg
, uint32_t val
)
39 /* Only APSR is actually writable */
41 uint32_t apsrmask
= 0;
44 apsrmask
|= XPSR_NZCV
| XPSR_Q
;
46 if ((mask
& 4) && arm_feature(env
, ARM_FEATURE_THUMB_DSP
)) {
49 xpsr_write(env
, val
, apsrmask
);
53 static uint32_t v7m_mrs_xpsr(CPUARMState
*env
, uint32_t reg
, unsigned el
)
57 if ((reg
& 1) && el
) {
58 mask
|= XPSR_EXCP
; /* IPSR (unpriv. reads as zero) */
61 mask
|= XPSR_NZCV
| XPSR_Q
; /* APSR */
62 if (arm_feature(env
, ARM_FEATURE_THUMB_DSP
)) {
66 /* EPSR reads as zero */
67 return xpsr_read(env
) & mask
;
70 static uint32_t v7m_mrs_control(CPUARMState
*env
, uint32_t secure
)
72 uint32_t value
= env
->v7m
.control
[secure
];
75 /* SFPA is RAZ/WI from NS; FPCA is stored in the M_REG_S bank */
76 value
|= env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_FPCA_MASK
;
81 #ifdef CONFIG_USER_ONLY
83 void HELPER(v7m_msr
)(CPUARMState
*env
, uint32_t maskreg
, uint32_t val
)
85 uint32_t mask
= extract32(maskreg
, 8, 4);
86 uint32_t reg
= extract32(maskreg
, 0, 8);
89 case 0 ... 7: /* xPSR sub-fields */
90 v7m_msr_xpsr(env
, mask
, reg
, val
);
92 case 20: /* CONTROL */
93 /* There are no sub-fields that are actually writable from EL0. */
96 /* Unprivileged writes to other registers are ignored */
101 uint32_t HELPER(v7m_mrs
)(CPUARMState
*env
, uint32_t reg
)
104 case 0 ... 7: /* xPSR sub-fields */
105 return v7m_mrs_xpsr(env
, reg
, 0);
106 case 20: /* CONTROL */
107 return v7m_mrs_control(env
, 0);
109 /* Unprivileged reads others as zero. */
114 void HELPER(v7m_bxns
)(CPUARMState
*env
, uint32_t dest
)
116 /* translate.c should never generate calls here in user-only mode */
117 g_assert_not_reached();
120 void HELPER(v7m_blxns
)(CPUARMState
*env
, uint32_t dest
)
122 /* translate.c should never generate calls here in user-only mode */
123 g_assert_not_reached();
126 void HELPER(v7m_preserve_fp_state
)(CPUARMState
*env
)
128 /* translate.c should never generate calls here in user-only mode */
129 g_assert_not_reached();
132 void HELPER(v7m_vlstm
)(CPUARMState
*env
, uint32_t fptr
)
134 /* translate.c should never generate calls here in user-only mode */
135 g_assert_not_reached();
138 void HELPER(v7m_vlldm
)(CPUARMState
*env
, uint32_t fptr
)
140 /* translate.c should never generate calls here in user-only mode */
141 g_assert_not_reached();
144 uint32_t HELPER(v7m_tt
)(CPUARMState
*env
, uint32_t addr
, uint32_t op
)
147 * The TT instructions can be used by unprivileged code, but in
148 * user-only emulation we don't have the MPU.
149 * Luckily since we know we are NonSecure unprivileged (and that in
150 * turn means that the A flag wasn't specified), all the bits in the
151 * register must be zero:
152 * IREGION: 0 because IRVALID is 0
153 * IRVALID: 0 because NS
157 * RW: 0 because unpriv and A flag not set
158 * R: 0 because unpriv and A flag not set
159 * SRVALID: 0 because NS
160 * MRVALID: 0 because unpriv and A flag not set
161 * SREGION: 0 becaus SRVALID is 0
162 * MREGION: 0 because MRVALID is 0
170 * What kind of stack write are we doing? This affects how exceptions
171 * generated during the stacking are treated.
173 typedef enum StackingMode
{
179 static bool v7m_stack_write(ARMCPU
*cpu
, uint32_t addr
, uint32_t value
,
180 ARMMMUIdx mmu_idx
, StackingMode mode
)
182 CPUState
*cs
= CPU(cpu
);
183 CPUARMState
*env
= &cpu
->env
;
184 MemTxAttrs attrs
= {};
186 target_ulong page_size
;
189 ARMMMUFaultInfo fi
= {};
190 ARMCacheAttrs cacheattrs
= {};
191 bool secure
= mmu_idx
& ARM_MMU_IDX_M_S
;
195 if (get_phys_addr(env
, addr
, MMU_DATA_STORE
, mmu_idx
, &physaddr
,
196 &attrs
, &prot
, &page_size
, &fi
, &cacheattrs
)) {
197 /* MPU/SAU lookup failed */
198 if (fi
.type
== ARMFault_QEMU_SFault
) {
199 if (mode
== STACK_LAZYFP
) {
200 qemu_log_mask(CPU_LOG_INT
,
201 "...SecureFault with SFSR.LSPERR "
202 "during lazy stacking\n");
203 env
->v7m
.sfsr
|= R_V7M_SFSR_LSPERR_MASK
;
205 qemu_log_mask(CPU_LOG_INT
,
206 "...SecureFault with SFSR.AUVIOL "
207 "during stacking\n");
208 env
->v7m
.sfsr
|= R_V7M_SFSR_AUVIOL_MASK
;
210 env
->v7m
.sfsr
|= R_V7M_SFSR_SFARVALID_MASK
;
211 env
->v7m
.sfar
= addr
;
212 exc
= ARMV7M_EXCP_SECURE
;
215 if (mode
== STACK_LAZYFP
) {
216 qemu_log_mask(CPU_LOG_INT
,
217 "...MemManageFault with CFSR.MLSPERR\n");
218 env
->v7m
.cfsr
[secure
] |= R_V7M_CFSR_MLSPERR_MASK
;
220 qemu_log_mask(CPU_LOG_INT
,
221 "...MemManageFault with CFSR.MSTKERR\n");
222 env
->v7m
.cfsr
[secure
] |= R_V7M_CFSR_MSTKERR_MASK
;
224 exc
= ARMV7M_EXCP_MEM
;
229 address_space_stl_le(arm_addressspace(cs
, attrs
), physaddr
, value
,
231 if (txres
!= MEMTX_OK
) {
232 /* BusFault trying to write the data */
233 if (mode
== STACK_LAZYFP
) {
234 qemu_log_mask(CPU_LOG_INT
, "...BusFault with BFSR.LSPERR\n");
235 env
->v7m
.cfsr
[M_REG_NS
] |= R_V7M_CFSR_LSPERR_MASK
;
237 qemu_log_mask(CPU_LOG_INT
, "...BusFault with BFSR.STKERR\n");
238 env
->v7m
.cfsr
[M_REG_NS
] |= R_V7M_CFSR_STKERR_MASK
;
240 exc
= ARMV7M_EXCP_BUS
;
248 * By pending the exception at this point we are making
249 * the IMPDEF choice "overridden exceptions pended" (see the
250 * MergeExcInfo() pseudocode). The other choice would be to not
251 * pend them now and then make a choice about which to throw away
252 * later if we have two derived exceptions.
253 * The only case when we must not pend the exception but instead
254 * throw it away is if we are doing the push of the callee registers
255 * and we've already generated a derived exception (this is indicated
256 * by the caller passing STACK_IGNFAULTS). Even in this case we will
257 * still update the fault status registers.
261 armv7m_nvic_set_pending_derived(env
->nvic
, exc
, exc_secure
);
264 armv7m_nvic_set_pending_lazyfp(env
->nvic
, exc
, exc_secure
);
266 case STACK_IGNFAULTS
:
272 static bool v7m_stack_read(ARMCPU
*cpu
, uint32_t *dest
, uint32_t addr
,
275 CPUState
*cs
= CPU(cpu
);
276 CPUARMState
*env
= &cpu
->env
;
277 MemTxAttrs attrs
= {};
279 target_ulong page_size
;
282 ARMMMUFaultInfo fi
= {};
283 ARMCacheAttrs cacheattrs
= {};
284 bool secure
= mmu_idx
& ARM_MMU_IDX_M_S
;
289 if (get_phys_addr(env
, addr
, MMU_DATA_LOAD
, mmu_idx
, &physaddr
,
290 &attrs
, &prot
, &page_size
, &fi
, &cacheattrs
)) {
291 /* MPU/SAU lookup failed */
292 if (fi
.type
== ARMFault_QEMU_SFault
) {
293 qemu_log_mask(CPU_LOG_INT
,
294 "...SecureFault with SFSR.AUVIOL during unstack\n");
295 env
->v7m
.sfsr
|= R_V7M_SFSR_AUVIOL_MASK
| R_V7M_SFSR_SFARVALID_MASK
;
296 env
->v7m
.sfar
= addr
;
297 exc
= ARMV7M_EXCP_SECURE
;
300 qemu_log_mask(CPU_LOG_INT
,
301 "...MemManageFault with CFSR.MUNSTKERR\n");
302 env
->v7m
.cfsr
[secure
] |= R_V7M_CFSR_MUNSTKERR_MASK
;
303 exc
= ARMV7M_EXCP_MEM
;
309 value
= address_space_ldl(arm_addressspace(cs
, attrs
), physaddr
,
311 if (txres
!= MEMTX_OK
) {
312 /* BusFault trying to read the data */
313 qemu_log_mask(CPU_LOG_INT
, "...BusFault with BFSR.UNSTKERR\n");
314 env
->v7m
.cfsr
[M_REG_NS
] |= R_V7M_CFSR_UNSTKERR_MASK
;
315 exc
= ARMV7M_EXCP_BUS
;
325 * By pending the exception at this point we are making
326 * the IMPDEF choice "overridden exceptions pended" (see the
327 * MergeExcInfo() pseudocode). The other choice would be to not
328 * pend them now and then make a choice about which to throw away
329 * later if we have two derived exceptions.
331 armv7m_nvic_set_pending(env
->nvic
, exc
, exc_secure
);
335 void HELPER(v7m_preserve_fp_state
)(CPUARMState
*env
)
338 * Preserve FP state (because LSPACT was set and we are about
339 * to execute an FP instruction). This corresponds to the
340 * PreserveFPState() pseudocode.
341 * We may throw an exception if the stacking fails.
343 ARMCPU
*cpu
= env_archcpu(env
);
344 bool is_secure
= env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_S_MASK
;
345 bool negpri
= !(env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_HFRDY_MASK
);
346 bool is_priv
= !(env
->v7m
.fpccr
[is_secure
] & R_V7M_FPCCR_USER_MASK
);
347 bool splimviol
= env
->v7m
.fpccr
[is_secure
] & R_V7M_FPCCR_SPLIMVIOL_MASK
;
348 uint32_t fpcar
= env
->v7m
.fpcar
[is_secure
];
349 bool stacked_ok
= true;
350 bool ts
= is_secure
&& (env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_TS_MASK
);
353 /* Take the iothread lock as we are going to touch the NVIC */
354 qemu_mutex_lock_iothread();
356 /* Check the background context had access to the FPU */
357 if (!v7m_cpacr_pass(env
, is_secure
, is_priv
)) {
358 armv7m_nvic_set_pending_lazyfp(env
->nvic
, ARMV7M_EXCP_USAGE
, is_secure
);
359 env
->v7m
.cfsr
[is_secure
] |= R_V7M_CFSR_NOCP_MASK
;
361 } else if (!is_secure
&& !extract32(env
->v7m
.nsacr
, 10, 1)) {
362 armv7m_nvic_set_pending_lazyfp(env
->nvic
, ARMV7M_EXCP_USAGE
, M_REG_S
);
363 env
->v7m
.cfsr
[M_REG_S
] |= R_V7M_CFSR_NOCP_MASK
;
367 if (!splimviol
&& stacked_ok
) {
368 /* We only stack if the stack limit wasn't violated */
372 mmu_idx
= arm_v7m_mmu_idx_all(env
, is_secure
, is_priv
, negpri
);
373 for (i
= 0; i
< (ts
? 32 : 16); i
+= 2) {
374 uint64_t dn
= *aa32_vfp_dreg(env
, i
/ 2);
375 uint32_t faddr
= fpcar
+ 4 * i
;
376 uint32_t slo
= extract64(dn
, 0, 32);
377 uint32_t shi
= extract64(dn
, 32, 32);
380 faddr
+= 8; /* skip the slot for the FPSCR */
382 stacked_ok
= stacked_ok
&&
383 v7m_stack_write(cpu
, faddr
, slo
, mmu_idx
, STACK_LAZYFP
) &&
384 v7m_stack_write(cpu
, faddr
+ 4, shi
, mmu_idx
, STACK_LAZYFP
);
387 stacked_ok
= stacked_ok
&&
388 v7m_stack_write(cpu
, fpcar
+ 0x40,
389 vfp_get_fpscr(env
), mmu_idx
, STACK_LAZYFP
);
393 * We definitely pended an exception, but it's possible that it
394 * might not be able to be taken now. If its priority permits us
395 * to take it now, then we must not update the LSPACT or FP regs,
396 * but instead jump out to take the exception immediately.
397 * If it's just pending and won't be taken until the current
398 * handler exits, then we do update LSPACT and the FP regs.
400 take_exception
= !stacked_ok
&&
401 armv7m_nvic_can_take_pending_exception(env
->nvic
);
403 qemu_mutex_unlock_iothread();
405 if (take_exception
) {
406 raise_exception_ra(env
, EXCP_LAZYFP
, 0, 1, GETPC());
409 env
->v7m
.fpccr
[is_secure
] &= ~R_V7M_FPCCR_LSPACT_MASK
;
412 /* Clear s0 to s31 and the FPSCR */
415 for (i
= 0; i
< 32; i
+= 2) {
416 *aa32_vfp_dreg(env
, i
/ 2) = 0;
418 vfp_set_fpscr(env
, 0);
421 * Otherwise s0 to s15 and FPSCR are UNKNOWN; we choose to leave them
427 * Write to v7M CONTROL.SPSEL bit for the specified security bank.
428 * This may change the current stack pointer between Main and Process
429 * stack pointers if it is done for the CONTROL register for the current
432 static void write_v7m_control_spsel_for_secstate(CPUARMState
*env
,
436 bool old_is_psp
= v7m_using_psp(env
);
438 env
->v7m
.control
[secstate
] =
439 deposit32(env
->v7m
.control
[secstate
],
440 R_V7M_CONTROL_SPSEL_SHIFT
,
441 R_V7M_CONTROL_SPSEL_LENGTH
, new_spsel
);
443 if (secstate
== env
->v7m
.secure
) {
444 bool new_is_psp
= v7m_using_psp(env
);
447 if (old_is_psp
!= new_is_psp
) {
448 tmp
= env
->v7m
.other_sp
;
449 env
->v7m
.other_sp
= env
->regs
[13];
456 * Write to v7M CONTROL.SPSEL bit. This may change the current
457 * stack pointer between Main and Process stack pointers.
459 static void write_v7m_control_spsel(CPUARMState
*env
, bool new_spsel
)
461 write_v7m_control_spsel_for_secstate(env
, new_spsel
, env
->v7m
.secure
);
464 void write_v7m_exception(CPUARMState
*env
, uint32_t new_exc
)
467 * Write a new value to v7m.exception, thus transitioning into or out
468 * of Handler mode; this may result in a change of active stack pointer.
470 bool new_is_psp
, old_is_psp
= v7m_using_psp(env
);
473 env
->v7m
.exception
= new_exc
;
475 new_is_psp
= v7m_using_psp(env
);
477 if (old_is_psp
!= new_is_psp
) {
478 tmp
= env
->v7m
.other_sp
;
479 env
->v7m
.other_sp
= env
->regs
[13];
484 /* Switch M profile security state between NS and S */
485 static void switch_v7m_security_state(CPUARMState
*env
, bool new_secstate
)
487 uint32_t new_ss_msp
, new_ss_psp
;
489 if (env
->v7m
.secure
== new_secstate
) {
494 * All the banked state is accessed by looking at env->v7m.secure
495 * except for the stack pointer; rearrange the SP appropriately.
497 new_ss_msp
= env
->v7m
.other_ss_msp
;
498 new_ss_psp
= env
->v7m
.other_ss_psp
;
500 if (v7m_using_psp(env
)) {
501 env
->v7m
.other_ss_psp
= env
->regs
[13];
502 env
->v7m
.other_ss_msp
= env
->v7m
.other_sp
;
504 env
->v7m
.other_ss_msp
= env
->regs
[13];
505 env
->v7m
.other_ss_psp
= env
->v7m
.other_sp
;
508 env
->v7m
.secure
= new_secstate
;
510 if (v7m_using_psp(env
)) {
511 env
->regs
[13] = new_ss_psp
;
512 env
->v7m
.other_sp
= new_ss_msp
;
514 env
->regs
[13] = new_ss_msp
;
515 env
->v7m
.other_sp
= new_ss_psp
;
519 void HELPER(v7m_bxns
)(CPUARMState
*env
, uint32_t dest
)
523 * - if the return value is a magic value, do exception return (like BX)
524 * - otherwise bit 0 of the return value is the target security state
528 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
529 /* Covers FNC_RETURN and EXC_RETURN magic */
530 min_magic
= FNC_RETURN_MIN_MAGIC
;
532 /* EXC_RETURN magic only */
533 min_magic
= EXC_RETURN_MIN_MAGIC
;
536 if (dest
>= min_magic
) {
538 * This is an exception return magic value; put it where
539 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
540 * Note that if we ever add gen_ss_advance() singlestep support to
541 * M profile this should count as an "instruction execution complete"
542 * event (compare gen_bx_excret_final_code()).
544 env
->regs
[15] = dest
& ~1;
545 env
->thumb
= dest
& 1;
546 HELPER(exception_internal
)(env
, EXCP_EXCEPTION_EXIT
);
550 /* translate.c should have made BXNS UNDEF unless we're secure */
551 assert(env
->v7m
.secure
);
554 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_SFPA_MASK
;
556 switch_v7m_security_state(env
, dest
& 1);
558 env
->regs
[15] = dest
& ~1;
559 arm_rebuild_hflags(env
);
562 void HELPER(v7m_blxns
)(CPUARMState
*env
, uint32_t dest
)
566 * - bit 0 of the destination address is the target security state
569 /* At this point regs[15] is the address just after the BLXNS */
570 uint32_t nextinst
= env
->regs
[15] | 1;
571 uint32_t sp
= env
->regs
[13] - 8;
574 /* translate.c will have made BLXNS UNDEF unless we're secure */
575 assert(env
->v7m
.secure
);
579 * Target is Secure, so this is just a normal BLX,
580 * except that the low bit doesn't indicate Thumb/not.
582 env
->regs
[14] = nextinst
;
584 env
->regs
[15] = dest
& ~1;
588 /* Target is non-secure: first push a stack frame */
589 if (!QEMU_IS_ALIGNED(sp
, 8)) {
590 qemu_log_mask(LOG_GUEST_ERROR
,
591 "BLXNS with misaligned SP is UNPREDICTABLE\n");
594 if (sp
< v7m_sp_limit(env
)) {
595 raise_exception(env
, EXCP_STKOF
, 0, 1);
598 saved_psr
= env
->v7m
.exception
;
599 if (env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_SFPA_MASK
) {
600 saved_psr
|= XPSR_SFPA
;
603 /* Note that these stores can throw exceptions on MPU faults */
604 cpu_stl_data_ra(env
, sp
, nextinst
, GETPC());
605 cpu_stl_data_ra(env
, sp
+ 4, saved_psr
, GETPC());
608 env
->regs
[14] = 0xfeffffff;
609 if (arm_v7m_is_handler_mode(env
)) {
611 * Write a dummy value to IPSR, to avoid leaking the current secure
612 * exception number to non-secure code. This is guaranteed not
613 * to cause write_v7m_exception() to actually change stacks.
615 write_v7m_exception(env
, 1);
617 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_SFPA_MASK
;
618 switch_v7m_security_state(env
, 0);
620 env
->regs
[15] = dest
;
621 arm_rebuild_hflags(env
);
624 static uint32_t *get_v7m_sp_ptr(CPUARMState
*env
, bool secure
, bool threadmode
,
628 * Return a pointer to the location where we currently store the
629 * stack pointer for the requested security state and thread mode.
630 * This pointer will become invalid if the CPU state is updated
631 * such that the stack pointers are switched around (eg changing
632 * the SPSEL control bit).
633 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
634 * Unlike that pseudocode, we require the caller to pass us in the
635 * SPSEL control bit value; this is because we also use this
636 * function in handling of pushing of the callee-saves registers
637 * part of the v8M stack frame (pseudocode PushCalleeStack()),
638 * and in the tailchain codepath the SPSEL bit comes from the exception
639 * return magic LR value from the previous exception. The pseudocode
640 * opencodes the stack-selection in PushCalleeStack(), but we prefer
641 * to make this utility function generic enough to do the job.
643 bool want_psp
= threadmode
&& spsel
;
645 if (secure
== env
->v7m
.secure
) {
646 if (want_psp
== v7m_using_psp(env
)) {
647 return &env
->regs
[13];
649 return &env
->v7m
.other_sp
;
653 return &env
->v7m
.other_ss_psp
;
655 return &env
->v7m
.other_ss_msp
;
660 static bool arm_v7m_load_vector(ARMCPU
*cpu
, int exc
, bool targets_secure
,
663 CPUState
*cs
= CPU(cpu
);
664 CPUARMState
*env
= &cpu
->env
;
666 uint32_t addr
= env
->v7m
.vecbase
[targets_secure
] + exc
* 4;
667 uint32_t vector_entry
;
668 MemTxAttrs attrs
= {};
672 mmu_idx
= arm_v7m_mmu_idx_for_secstate_and_priv(env
, targets_secure
, true);
675 * We don't do a get_phys_addr() here because the rules for vector
676 * loads are special: they always use the default memory map, and
677 * the default memory map permits reads from all addresses.
678 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
679 * that we want this special case which would always say "yes",
680 * we just do the SAU lookup here followed by a direct physical load.
682 attrs
.secure
= targets_secure
;
685 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
686 V8M_SAttributes sattrs
= {};
688 v8m_security_lookup(env
, addr
, MMU_DATA_LOAD
, mmu_idx
, &sattrs
);
690 attrs
.secure
= false;
691 } else if (!targets_secure
) {
693 * NS access to S memory: the underlying exception which we escalate
694 * to HardFault is SecureFault, which always targets Secure.
701 vector_entry
= address_space_ldl(arm_addressspace(cs
, attrs
), addr
,
703 if (result
!= MEMTX_OK
) {
705 * Underlying exception is BusFault: its target security state
706 * depends on BFHFNMINS.
708 exc_secure
= !(cpu
->env
.v7m
.aircr
& R_V7M_AIRCR_BFHFNMINS_MASK
);
711 *pvec
= vector_entry
;
716 * All vector table fetch fails are reported as HardFault, with
717 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
718 * technically the underlying exception is a SecureFault or BusFault
719 * that is escalated to HardFault.) This is a terminal exception,
720 * so we will either take the HardFault immediately or else enter
721 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
722 * The HardFault is Secure if BFHFNMINS is 0 (meaning that all HFs are
723 * secure); otherwise it targets the same security state as the
724 * underlying exception.
725 * In v8.1M HardFaults from vector table fetch fails don't set FORCED.
727 if (!(cpu
->env
.v7m
.aircr
& R_V7M_AIRCR_BFHFNMINS_MASK
)) {
730 env
->v7m
.hfsr
|= R_V7M_HFSR_VECTTBL_MASK
;
731 if (!arm_feature(env
, ARM_FEATURE_V8_1M
)) {
732 env
->v7m
.hfsr
|= R_V7M_HFSR_FORCED_MASK
;
734 armv7m_nvic_set_pending_derived(env
->nvic
, ARMV7M_EXCP_HARD
, exc_secure
);
738 static uint32_t v7m_integrity_sig(CPUARMState
*env
, uint32_t lr
)
741 * Return the integrity signature value for the callee-saves
742 * stack frame section. @lr is the exception return payload/LR value
743 * whose FType bit forms bit 0 of the signature if FP is present.
745 uint32_t sig
= 0xfefa125a;
747 if (!cpu_isar_feature(aa32_vfp_simd
, env_archcpu(env
))
748 || (lr
& R_V7M_EXCRET_FTYPE_MASK
)) {
754 static bool v7m_push_callee_stack(ARMCPU
*cpu
, uint32_t lr
, bool dotailchain
,
758 * For v8M, push the callee-saves register part of the stack frame.
759 * Compare the v8M pseudocode PushCalleeStack().
760 * In the tailchaining case this may not be the current stack.
762 CPUARMState
*env
= &cpu
->env
;
763 uint32_t *frame_sp_p
;
770 StackingMode smode
= ignore_faults
? STACK_IGNFAULTS
: STACK_NORMAL
;
773 bool mode
= lr
& R_V7M_EXCRET_MODE_MASK
;
774 bool priv
= !(env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_NPRIV_MASK
) ||
777 mmu_idx
= arm_v7m_mmu_idx_for_secstate_and_priv(env
, M_REG_S
, priv
);
778 frame_sp_p
= get_v7m_sp_ptr(env
, M_REG_S
, mode
,
779 lr
& R_V7M_EXCRET_SPSEL_MASK
);
780 want_psp
= mode
&& (lr
& R_V7M_EXCRET_SPSEL_MASK
);
782 limit
= env
->v7m
.psplim
[M_REG_S
];
784 limit
= env
->v7m
.msplim
[M_REG_S
];
787 mmu_idx
= arm_mmu_idx(env
);
788 frame_sp_p
= &env
->regs
[13];
789 limit
= v7m_sp_limit(env
);
792 frameptr
= *frame_sp_p
- 0x28;
793 if (frameptr
< limit
) {
795 * Stack limit failure: set SP to the limit value, and generate
796 * STKOF UsageFault. Stack pushes below the limit must not be
797 * performed. It is IMPDEF whether pushes above the limit are
798 * performed; we choose not to.
800 qemu_log_mask(CPU_LOG_INT
,
801 "...STKOF during callee-saves register stacking\n");
802 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_STKOF_MASK
;
803 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
,
810 * Write as much of the stack frame as we can. A write failure may
811 * cause us to pend a derived exception.
813 sig
= v7m_integrity_sig(env
, lr
);
815 v7m_stack_write(cpu
, frameptr
, sig
, mmu_idx
, smode
) &&
816 v7m_stack_write(cpu
, frameptr
+ 0x8, env
->regs
[4], mmu_idx
, smode
) &&
817 v7m_stack_write(cpu
, frameptr
+ 0xc, env
->regs
[5], mmu_idx
, smode
) &&
818 v7m_stack_write(cpu
, frameptr
+ 0x10, env
->regs
[6], mmu_idx
, smode
) &&
819 v7m_stack_write(cpu
, frameptr
+ 0x14, env
->regs
[7], mmu_idx
, smode
) &&
820 v7m_stack_write(cpu
, frameptr
+ 0x18, env
->regs
[8], mmu_idx
, smode
) &&
821 v7m_stack_write(cpu
, frameptr
+ 0x1c, env
->regs
[9], mmu_idx
, smode
) &&
822 v7m_stack_write(cpu
, frameptr
+ 0x20, env
->regs
[10], mmu_idx
, smode
) &&
823 v7m_stack_write(cpu
, frameptr
+ 0x24, env
->regs
[11], mmu_idx
, smode
);
825 /* Update SP regardless of whether any of the stack accesses failed. */
826 *frame_sp_p
= frameptr
;
831 static void v7m_exception_taken(ARMCPU
*cpu
, uint32_t lr
, bool dotailchain
,
832 bool ignore_stackfaults
)
835 * Do the "take the exception" parts of exception entry,
836 * but not the pushing of state to the stack. This is
837 * similar to the pseudocode ExceptionTaken() function.
839 CPUARMState
*env
= &cpu
->env
;
843 bool push_failed
= false;
845 armv7m_nvic_get_pending_irq_info(env
->nvic
, &exc
, &targets_secure
);
846 qemu_log_mask(CPU_LOG_INT
, "...taking pending %s exception %d\n",
847 targets_secure
? "secure" : "nonsecure", exc
);
850 /* Sanitize LR FType and PREFIX bits */
851 if (!cpu_isar_feature(aa32_vfp_simd
, cpu
)) {
852 lr
|= R_V7M_EXCRET_FTYPE_MASK
;
854 lr
= deposit32(lr
, 24, 8, 0xff);
857 if (arm_feature(env
, ARM_FEATURE_V8
)) {
858 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
) &&
859 (lr
& R_V7M_EXCRET_S_MASK
)) {
861 * The background code (the owner of the registers in the
862 * exception frame) is Secure. This means it may either already
863 * have or now needs to push callee-saves registers.
865 if (targets_secure
) {
866 if (dotailchain
&& !(lr
& R_V7M_EXCRET_ES_MASK
)) {
868 * We took an exception from Secure to NonSecure
869 * (which means the callee-saved registers got stacked)
870 * and are now tailchaining to a Secure exception.
871 * Clear DCRS so eventual return from this Secure
872 * exception unstacks the callee-saved registers.
874 lr
&= ~R_V7M_EXCRET_DCRS_MASK
;
878 * We're going to a non-secure exception; push the
879 * callee-saves registers to the stack now, if they're
882 if (lr
& R_V7M_EXCRET_DCRS_MASK
&&
883 !(dotailchain
&& !(lr
& R_V7M_EXCRET_ES_MASK
))) {
884 push_failed
= v7m_push_callee_stack(cpu
, lr
, dotailchain
,
887 lr
|= R_V7M_EXCRET_DCRS_MASK
;
891 lr
&= ~R_V7M_EXCRET_ES_MASK
;
892 if (targets_secure
|| !arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
893 lr
|= R_V7M_EXCRET_ES_MASK
;
895 lr
&= ~R_V7M_EXCRET_SPSEL_MASK
;
896 if (env
->v7m
.control
[targets_secure
] & R_V7M_CONTROL_SPSEL_MASK
) {
897 lr
|= R_V7M_EXCRET_SPSEL_MASK
;
901 * Clear registers if necessary to prevent non-secure exception
902 * code being able to see register values from secure code.
903 * Where register values become architecturally UNKNOWN we leave
904 * them with their previous values. v8.1M is tighter than v8.0M
905 * here and always zeroes the caller-saved registers regardless
906 * of the security state the exception is targeting.
908 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
909 if (!targets_secure
|| arm_feature(env
, ARM_FEATURE_V8_1M
)) {
911 * Always clear the caller-saved registers (they have been
912 * pushed to the stack earlier in v7m_push_stack()).
913 * Clear callee-saved registers if the background code is
914 * Secure (in which case these regs were saved in
915 * v7m_push_callee_stack()).
919 * r4..r11 are callee-saves, zero only if background
920 * state was Secure (EXCRET.S == 1) and exception
921 * targets Non-secure state
923 bool zero_callee_saves
= !targets_secure
&&
924 (lr
& R_V7M_EXCRET_S_MASK
);
926 for (i
= 0; i
< 13; i
++) {
927 if (i
< 4 || i
> 11 || zero_callee_saves
) {
932 xpsr_write(env
, 0, XPSR_NZCV
| XPSR_Q
| XPSR_GE
| XPSR_IT
);
937 if (push_failed
&& !ignore_stackfaults
) {
939 * Derived exception on callee-saves register stacking:
940 * we might now want to take a different exception which
941 * targets a different security state, so try again from the top.
943 qemu_log_mask(CPU_LOG_INT
,
944 "...derived exception on callee-saves register stacking");
945 v7m_exception_taken(cpu
, lr
, true, true);
949 if (!arm_v7m_load_vector(cpu
, exc
, targets_secure
, &addr
)) {
950 /* Vector load failed: derived exception */
951 qemu_log_mask(CPU_LOG_INT
, "...derived exception on vector table load");
952 v7m_exception_taken(cpu
, lr
, true, true);
957 * Now we've done everything that might cause a derived exception
958 * we can go ahead and activate whichever exception we're going to
959 * take (which might now be the derived exception).
961 armv7m_nvic_acknowledge_irq(env
->nvic
);
963 /* Switch to target security state -- must do this before writing SPSEL */
964 switch_v7m_security_state(env
, targets_secure
);
965 write_v7m_control_spsel(env
, 0);
966 arm_clear_exclusive(env
);
967 /* Clear SFPA and FPCA (has no effect if no FPU) */
968 env
->v7m
.control
[M_REG_S
] &=
969 ~(R_V7M_CONTROL_FPCA_MASK
| R_V7M_CONTROL_SFPA_MASK
);
971 env
->condexec_bits
= 0;
973 env
->regs
[15] = addr
& 0xfffffffe;
974 env
->thumb
= addr
& 1;
975 arm_rebuild_hflags(env
);
978 static void v7m_update_fpccr(CPUARMState
*env
, uint32_t frameptr
,
982 * Like the pseudocode UpdateFPCCR: save state in FPCAR and FPCCR
983 * that we will need later in order to do lazy FP reg stacking.
985 bool is_secure
= env
->v7m
.secure
;
986 void *nvic
= env
->nvic
;
988 * Some bits are unbanked and live always in fpccr[M_REG_S]; some bits
989 * are banked and we want to update the bit in the bank for the
990 * current security state; and in one case we want to specifically
991 * update the NS banked version of a bit even if we are secure.
993 uint32_t *fpccr_s
= &env
->v7m
.fpccr
[M_REG_S
];
994 uint32_t *fpccr_ns
= &env
->v7m
.fpccr
[M_REG_NS
];
995 uint32_t *fpccr
= &env
->v7m
.fpccr
[is_secure
];
996 bool hfrdy
, bfrdy
, mmrdy
, ns_ufrdy
, s_ufrdy
, sfrdy
, monrdy
;
998 env
->v7m
.fpcar
[is_secure
] = frameptr
& ~0x7;
1000 if (apply_splim
&& arm_feature(env
, ARM_FEATURE_V8
)) {
1002 uint32_t splim
= v7m_sp_limit(env
);
1003 bool ign
= armv7m_nvic_neg_prio_requested(nvic
, is_secure
) &&
1004 (env
->v7m
.ccr
[is_secure
] & R_V7M_CCR_STKOFHFNMIGN_MASK
);
1006 splimviol
= !ign
&& frameptr
< splim
;
1007 *fpccr
= FIELD_DP32(*fpccr
, V7M_FPCCR
, SPLIMVIOL
, splimviol
);
1010 *fpccr
= FIELD_DP32(*fpccr
, V7M_FPCCR
, LSPACT
, 1);
1012 *fpccr_s
= FIELD_DP32(*fpccr_s
, V7M_FPCCR
, S
, is_secure
);
1014 *fpccr
= FIELD_DP32(*fpccr
, V7M_FPCCR
, USER
, arm_current_el(env
) == 0);
1016 *fpccr
= FIELD_DP32(*fpccr
, V7M_FPCCR
, THREAD
,
1017 !arm_v7m_is_handler_mode(env
));
1019 hfrdy
= armv7m_nvic_get_ready_status(nvic
, ARMV7M_EXCP_HARD
, false);
1020 *fpccr_s
= FIELD_DP32(*fpccr_s
, V7M_FPCCR
, HFRDY
, hfrdy
);
1022 bfrdy
= armv7m_nvic_get_ready_status(nvic
, ARMV7M_EXCP_BUS
, false);
1023 *fpccr_s
= FIELD_DP32(*fpccr_s
, V7M_FPCCR
, BFRDY
, bfrdy
);
1025 mmrdy
= armv7m_nvic_get_ready_status(nvic
, ARMV7M_EXCP_MEM
, is_secure
);
1026 *fpccr
= FIELD_DP32(*fpccr
, V7M_FPCCR
, MMRDY
, mmrdy
);
1028 ns_ufrdy
= armv7m_nvic_get_ready_status(nvic
, ARMV7M_EXCP_USAGE
, false);
1029 *fpccr_ns
= FIELD_DP32(*fpccr_ns
, V7M_FPCCR
, UFRDY
, ns_ufrdy
);
1031 monrdy
= armv7m_nvic_get_ready_status(nvic
, ARMV7M_EXCP_DEBUG
, false);
1032 *fpccr_s
= FIELD_DP32(*fpccr_s
, V7M_FPCCR
, MONRDY
, monrdy
);
1034 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
1035 s_ufrdy
= armv7m_nvic_get_ready_status(nvic
, ARMV7M_EXCP_USAGE
, true);
1036 *fpccr_s
= FIELD_DP32(*fpccr_s
, V7M_FPCCR
, UFRDY
, s_ufrdy
);
1038 sfrdy
= armv7m_nvic_get_ready_status(nvic
, ARMV7M_EXCP_SECURE
, false);
1039 *fpccr_s
= FIELD_DP32(*fpccr_s
, V7M_FPCCR
, SFRDY
, sfrdy
);
1043 void HELPER(v7m_vlstm
)(CPUARMState
*env
, uint32_t fptr
)
1045 /* fptr is the value of Rn, the frame pointer we store the FP regs to */
1046 bool s
= env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_S_MASK
;
1047 bool lspact
= env
->v7m
.fpccr
[s
] & R_V7M_FPCCR_LSPACT_MASK
;
1048 uintptr_t ra
= GETPC();
1050 assert(env
->v7m
.secure
);
1052 if (!(env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_SFPA_MASK
)) {
1056 /* Check access to the coprocessor is permitted */
1057 if (!v7m_cpacr_pass(env
, true, arm_current_el(env
) != 0)) {
1058 raise_exception_ra(env
, EXCP_NOCP
, 0, 1, GETPC());
1062 /* LSPACT should not be active when there is active FP state */
1063 raise_exception_ra(env
, EXCP_LSERR
, 0, 1, GETPC());
1067 raise_exception_ra(env
, EXCP_UNALIGNED
, 0, 1, GETPC());
1071 * Note that we do not use v7m_stack_write() here, because the
1072 * accesses should not set the FSR bits for stacking errors if they
1073 * fail. (In pseudocode terms, they are AccType_NORMAL, not AccType_STACK
1074 * or AccType_LAZYFP). Faults in cpu_stl_data_ra() will throw exceptions
1077 if (!(env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_LSPEN_MASK
)) {
1078 bool ts
= env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_TS_MASK
;
1081 for (i
= 0; i
< (ts
? 32 : 16); i
+= 2) {
1082 uint64_t dn
= *aa32_vfp_dreg(env
, i
/ 2);
1083 uint32_t faddr
= fptr
+ 4 * i
;
1084 uint32_t slo
= extract64(dn
, 0, 32);
1085 uint32_t shi
= extract64(dn
, 32, 32);
1088 faddr
+= 8; /* skip the slot for the FPSCR */
1090 cpu_stl_data_ra(env
, faddr
, slo
, ra
);
1091 cpu_stl_data_ra(env
, faddr
+ 4, shi
, ra
);
1093 cpu_stl_data_ra(env
, fptr
+ 0x40, vfp_get_fpscr(env
), ra
);
1096 * If TS is 0 then s0 to s15 and FPSCR are UNKNOWN; we choose to
1097 * leave them unchanged, matching our choice in v7m_preserve_fp_state.
1100 for (i
= 0; i
< 32; i
+= 2) {
1101 *aa32_vfp_dreg(env
, i
/ 2) = 0;
1103 vfp_set_fpscr(env
, 0);
1106 v7m_update_fpccr(env
, fptr
, false);
1109 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_FPCA_MASK
;
1112 void HELPER(v7m_vlldm
)(CPUARMState
*env
, uint32_t fptr
)
1114 uintptr_t ra
= GETPC();
1116 /* fptr is the value of Rn, the frame pointer we load the FP regs from */
1117 assert(env
->v7m
.secure
);
1119 if (!(env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_SFPA_MASK
)) {
1123 /* Check access to the coprocessor is permitted */
1124 if (!v7m_cpacr_pass(env
, true, arm_current_el(env
) != 0)) {
1125 raise_exception_ra(env
, EXCP_NOCP
, 0, 1, GETPC());
1128 if (env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_LSPACT_MASK
) {
1129 /* State in FP is still valid */
1130 env
->v7m
.fpccr
[M_REG_S
] &= ~R_V7M_FPCCR_LSPACT_MASK
;
1132 bool ts
= env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_TS_MASK
;
1137 raise_exception_ra(env
, EXCP_UNALIGNED
, 0, 1, GETPC());
1140 for (i
= 0; i
< (ts
? 32 : 16); i
+= 2) {
1143 uint32_t faddr
= fptr
+ 4 * i
;
1146 faddr
+= 8; /* skip the slot for the FPSCR */
1149 slo
= cpu_ldl_data_ra(env
, faddr
, ra
);
1150 shi
= cpu_ldl_data_ra(env
, faddr
+ 4, ra
);
1152 dn
= (uint64_t) shi
<< 32 | slo
;
1153 *aa32_vfp_dreg(env
, i
/ 2) = dn
;
1155 fpscr
= cpu_ldl_data_ra(env
, fptr
+ 0x40, ra
);
1156 vfp_set_fpscr(env
, fpscr
);
1159 env
->v7m
.control
[M_REG_S
] |= R_V7M_CONTROL_FPCA_MASK
;
1162 static bool v7m_push_stack(ARMCPU
*cpu
)
1165 * Do the "set up stack frame" part of exception entry,
1166 * similar to pseudocode PushStack().
1167 * Return true if we generate a derived exception (and so
1168 * should ignore further stack faults trying to process
1169 * that derived exception.)
1171 bool stacked_ok
= true, limitviol
= false;
1172 CPUARMState
*env
= &cpu
->env
;
1173 uint32_t xpsr
= xpsr_read(env
);
1174 uint32_t frameptr
= env
->regs
[13];
1175 ARMMMUIdx mmu_idx
= arm_mmu_idx(env
);
1177 bool nsacr_cp10
= extract32(env
->v7m
.nsacr
, 10, 1);
1179 if ((env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_FPCA_MASK
) &&
1180 (env
->v7m
.secure
|| nsacr_cp10
)) {
1181 if (env
->v7m
.secure
&&
1182 env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_TS_MASK
) {
1191 /* Align stack pointer if the guest wants that */
1192 if ((frameptr
& 4) &&
1193 (env
->v7m
.ccr
[env
->v7m
.secure
] & R_V7M_CCR_STKALIGN_MASK
)) {
1195 xpsr
|= XPSR_SPREALIGN
;
1199 if (env
->v7m
.secure
&&
1200 (env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_SFPA_MASK
)) {
1204 frameptr
-= framesize
;
1206 if (arm_feature(env
, ARM_FEATURE_V8
)) {
1207 uint32_t limit
= v7m_sp_limit(env
);
1209 if (frameptr
< limit
) {
1211 * Stack limit failure: set SP to the limit value, and generate
1212 * STKOF UsageFault. Stack pushes below the limit must not be
1213 * performed. It is IMPDEF whether pushes above the limit are
1214 * performed; we choose not to.
1216 qemu_log_mask(CPU_LOG_INT
,
1217 "...STKOF during stacking\n");
1218 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_STKOF_MASK
;
1219 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
,
1221 env
->regs
[13] = limit
;
1223 * We won't try to perform any further memory accesses but
1224 * we must continue through the following code to check for
1225 * permission faults during FPU state preservation, and we
1226 * must update FPCCR if lazy stacking is enabled.
1234 * Write as much of the stack frame as we can. If we fail a stack
1235 * write this will result in a derived exception being pended
1236 * (which may be taken in preference to the one we started with
1237 * if it has higher priority).
1239 stacked_ok
= stacked_ok
&&
1240 v7m_stack_write(cpu
, frameptr
, env
->regs
[0], mmu_idx
, STACK_NORMAL
) &&
1241 v7m_stack_write(cpu
, frameptr
+ 4, env
->regs
[1],
1242 mmu_idx
, STACK_NORMAL
) &&
1243 v7m_stack_write(cpu
, frameptr
+ 8, env
->regs
[2],
1244 mmu_idx
, STACK_NORMAL
) &&
1245 v7m_stack_write(cpu
, frameptr
+ 12, env
->regs
[3],
1246 mmu_idx
, STACK_NORMAL
) &&
1247 v7m_stack_write(cpu
, frameptr
+ 16, env
->regs
[12],
1248 mmu_idx
, STACK_NORMAL
) &&
1249 v7m_stack_write(cpu
, frameptr
+ 20, env
->regs
[14],
1250 mmu_idx
, STACK_NORMAL
) &&
1251 v7m_stack_write(cpu
, frameptr
+ 24, env
->regs
[15],
1252 mmu_idx
, STACK_NORMAL
) &&
1253 v7m_stack_write(cpu
, frameptr
+ 28, xpsr
, mmu_idx
, STACK_NORMAL
);
1255 if (env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_FPCA_MASK
) {
1256 /* FPU is active, try to save its registers */
1257 bool fpccr_s
= env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_S_MASK
;
1258 bool lspact
= env
->v7m
.fpccr
[fpccr_s
] & R_V7M_FPCCR_LSPACT_MASK
;
1260 if (lspact
&& arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
1261 qemu_log_mask(CPU_LOG_INT
,
1262 "...SecureFault because LSPACT and FPCA both set\n");
1263 env
->v7m
.sfsr
|= R_V7M_SFSR_LSERR_MASK
;
1264 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
1265 } else if (!env
->v7m
.secure
&& !nsacr_cp10
) {
1266 qemu_log_mask(CPU_LOG_INT
,
1267 "...Secure UsageFault with CFSR.NOCP because "
1268 "NSACR.CP10 prevents stacking FP regs\n");
1269 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, M_REG_S
);
1270 env
->v7m
.cfsr
[M_REG_S
] |= R_V7M_CFSR_NOCP_MASK
;
1272 if (!(env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_LSPEN_MASK
)) {
1273 /* Lazy stacking disabled, save registers now */
1275 bool cpacr_pass
= v7m_cpacr_pass(env
, env
->v7m
.secure
,
1276 arm_current_el(env
) != 0);
1278 if (stacked_ok
&& !cpacr_pass
) {
1280 * Take UsageFault if CPACR forbids access. The pseudocode
1281 * here does a full CheckCPEnabled() but we know the NSACR
1282 * check can never fail as we have already handled that.
1284 qemu_log_mask(CPU_LOG_INT
,
1285 "...UsageFault with CFSR.NOCP because "
1286 "CPACR.CP10 prevents stacking FP regs\n");
1287 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
,
1289 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_NOCP_MASK
;
1293 for (i
= 0; i
< ((framesize
== 0xa8) ? 32 : 16); i
+= 2) {
1294 uint64_t dn
= *aa32_vfp_dreg(env
, i
/ 2);
1295 uint32_t faddr
= frameptr
+ 0x20 + 4 * i
;
1296 uint32_t slo
= extract64(dn
, 0, 32);
1297 uint32_t shi
= extract64(dn
, 32, 32);
1300 faddr
+= 8; /* skip the slot for the FPSCR */
1302 stacked_ok
= stacked_ok
&&
1303 v7m_stack_write(cpu
, faddr
, slo
,
1304 mmu_idx
, STACK_NORMAL
) &&
1305 v7m_stack_write(cpu
, faddr
+ 4, shi
,
1306 mmu_idx
, STACK_NORMAL
);
1308 stacked_ok
= stacked_ok
&&
1309 v7m_stack_write(cpu
, frameptr
+ 0x60,
1310 vfp_get_fpscr(env
), mmu_idx
, STACK_NORMAL
);
1312 for (i
= 0; i
< ((framesize
== 0xa8) ? 32 : 16); i
+= 2) {
1313 *aa32_vfp_dreg(env
, i
/ 2) = 0;
1315 vfp_set_fpscr(env
, 0);
1318 /* Lazy stacking enabled, save necessary info to stack later */
1319 v7m_update_fpccr(env
, frameptr
+ 0x20, true);
1325 * If we broke a stack limit then SP was already updated earlier;
1326 * otherwise we update SP regardless of whether any of the stack
1327 * accesses failed or we took some other kind of fault.
1330 env
->regs
[13] = frameptr
;
1336 static void do_v7m_exception_exit(ARMCPU
*cpu
)
1338 CPUARMState
*env
= &cpu
->env
;
1340 uint32_t xpsr
, xpsr_mask
;
1341 bool ufault
= false;
1342 bool sfault
= false;
1343 bool return_to_sp_process
;
1344 bool return_to_handler
;
1345 bool rettobase
= false;
1346 bool exc_secure
= false;
1347 bool return_to_secure
;
1349 bool restore_s16_s31
;
1352 * If we're not in Handler mode then jumps to magic exception-exit
1353 * addresses don't have magic behaviour. However for the v8M
1354 * security extensions the magic secure-function-return has to
1355 * work in thread mode too, so to avoid doing an extra check in
1356 * the generated code we allow exception-exit magic to also cause the
1357 * internal exception and bring us here in thread mode. Correct code
1358 * will never try to do this (the following insn fetch will always
1359 * fault) so we the overhead of having taken an unnecessary exception
1362 if (!arm_v7m_is_handler_mode(env
)) {
1367 * In the spec pseudocode ExceptionReturn() is called directly
1368 * from BXWritePC() and gets the full target PC value including
1369 * bit zero. In QEMU's implementation we treat it as a normal
1370 * jump-to-register (which is then caught later on), and so split
1371 * the target value up between env->regs[15] and env->thumb in
1372 * gen_bx(). Reconstitute it.
1374 excret
= env
->regs
[15];
1379 qemu_log_mask(CPU_LOG_INT
, "Exception return: magic PC %" PRIx32
1380 " previous exception %d\n",
1381 excret
, env
->v7m
.exception
);
1383 if ((excret
& R_V7M_EXCRET_RES1_MASK
) != R_V7M_EXCRET_RES1_MASK
) {
1384 qemu_log_mask(LOG_GUEST_ERROR
, "M profile: zero high bits in exception "
1385 "exit PC value 0x%" PRIx32
" are UNPREDICTABLE\n",
1389 ftype
= excret
& R_V7M_EXCRET_FTYPE_MASK
;
1391 if (!ftype
&& !cpu_isar_feature(aa32_vfp_simd
, cpu
)) {
1392 qemu_log_mask(LOG_GUEST_ERROR
, "M profile: zero FTYPE in exception "
1393 "exit PC value 0x%" PRIx32
" is UNPREDICTABLE "
1394 "if FPU not present\n",
1399 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
1401 * EXC_RETURN.ES validation check (R_SMFL). We must do this before
1402 * we pick which FAULTMASK to clear.
1404 if (!env
->v7m
.secure
&&
1405 ((excret
& R_V7M_EXCRET_ES_MASK
) ||
1406 !(excret
& R_V7M_EXCRET_DCRS_MASK
))) {
1408 /* For all other purposes, treat ES as 0 (R_HXSR) */
1409 excret
&= ~R_V7M_EXCRET_ES_MASK
;
1411 exc_secure
= excret
& R_V7M_EXCRET_ES_MASK
;
1414 if (env
->v7m
.exception
!= ARMV7M_EXCP_NMI
) {
1416 * Auto-clear FAULTMASK on return from other than NMI.
1417 * If the security extension is implemented then this only
1418 * happens if the raw execution priority is >= 0; the
1419 * value of the ES bit in the exception return value indicates
1420 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
1422 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
1423 if (armv7m_nvic_raw_execution_priority(env
->nvic
) >= 0) {
1424 env
->v7m
.faultmask
[exc_secure
] = 0;
1427 env
->v7m
.faultmask
[M_REG_NS
] = 0;
1431 switch (armv7m_nvic_complete_irq(env
->nvic
, env
->v7m
.exception
,
1434 /* attempt to exit an exception that isn't active */
1438 /* still an irq active now */
1442 * We returned to base exception level, no nesting.
1443 * (In the pseudocode this is written using "NestedActivation != 1"
1444 * where we have 'rettobase == false'.)
1449 g_assert_not_reached();
1452 return_to_handler
= !(excret
& R_V7M_EXCRET_MODE_MASK
);
1453 return_to_sp_process
= excret
& R_V7M_EXCRET_SPSEL_MASK
;
1454 return_to_secure
= arm_feature(env
, ARM_FEATURE_M_SECURITY
) &&
1455 (excret
& R_V7M_EXCRET_S_MASK
);
1457 if (arm_feature(env
, ARM_FEATURE_V8
)) {
1458 if (!arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
1460 * UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
1461 * we choose to take the UsageFault.
1463 if ((excret
& R_V7M_EXCRET_S_MASK
) ||
1464 (excret
& R_V7M_EXCRET_ES_MASK
) ||
1465 !(excret
& R_V7M_EXCRET_DCRS_MASK
)) {
1469 if (excret
& R_V7M_EXCRET_RES0_MASK
) {
1473 /* For v7M we only recognize certain combinations of the low bits */
1474 switch (excret
& 0xf) {
1475 case 1: /* Return to Handler */
1477 case 13: /* Return to Thread using Process stack */
1478 case 9: /* Return to Thread using Main stack */
1480 * We only need to check NONBASETHRDENA for v7M, because in
1481 * v8M this bit does not exist (it is RES1).
1484 !(env
->v7m
.ccr
[env
->v7m
.secure
] &
1485 R_V7M_CCR_NONBASETHRDENA_MASK
)) {
1495 * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
1496 * Handler mode (and will be until we write the new XPSR.Interrupt
1497 * field) this does not switch around the current stack pointer.
1498 * We must do this before we do any kind of tailchaining, including
1499 * for the derived exceptions on integrity check failures, or we will
1500 * give the guest an incorrect EXCRET.SPSEL value on exception entry.
1502 write_v7m_control_spsel_for_secstate(env
, return_to_sp_process
, exc_secure
);
1505 * Clear scratch FP values left in caller saved registers; this
1506 * must happen before any kind of tail chaining.
1508 if ((env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_CLRONRET_MASK
) &&
1509 (env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_FPCA_MASK
)) {
1510 if (env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_LSPACT_MASK
) {
1511 env
->v7m
.sfsr
|= R_V7M_SFSR_LSERR_MASK
;
1512 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
1513 qemu_log_mask(CPU_LOG_INT
, "...taking SecureFault on existing "
1514 "stackframe: error during lazy state deactivation\n");
1515 v7m_exception_taken(cpu
, excret
, true, false);
1518 if (arm_feature(env
, ARM_FEATURE_V8_1M
)) {
1519 /* v8.1M adds this NOCP check */
1520 bool nsacr_pass
= exc_secure
||
1521 extract32(env
->v7m
.nsacr
, 10, 1);
1522 bool cpacr_pass
= v7m_cpacr_pass(env
, exc_secure
, true);
1524 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, true);
1525 env
->v7m
.cfsr
[M_REG_S
] |= R_V7M_CFSR_NOCP_MASK
;
1526 qemu_log_mask(CPU_LOG_INT
, "...taking UsageFault on existing "
1527 "stackframe: NSACR prevents clearing FPU registers\n");
1528 v7m_exception_taken(cpu
, excret
, true, false);
1529 } else if (!cpacr_pass
) {
1530 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
,
1532 env
->v7m
.cfsr
[exc_secure
] |= R_V7M_CFSR_NOCP_MASK
;
1533 qemu_log_mask(CPU_LOG_INT
, "...taking UsageFault on existing "
1534 "stackframe: CPACR prevents clearing FPU registers\n");
1535 v7m_exception_taken(cpu
, excret
, true, false);
1538 /* Clear s0..s15 and FPSCR; TODO also VPR when MVE is implemented */
1541 for (i
= 0; i
< 16; i
+= 2) {
1542 *aa32_vfp_dreg(env
, i
/ 2) = 0;
1544 vfp_set_fpscr(env
, 0);
1549 env
->v7m
.sfsr
|= R_V7M_SFSR_INVER_MASK
;
1550 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
1551 qemu_log_mask(CPU_LOG_INT
, "...taking SecureFault on existing "
1552 "stackframe: failed EXC_RETURN.ES validity check\n");
1553 v7m_exception_taken(cpu
, excret
, true, false);
1559 * Bad exception return: instead of popping the exception
1560 * stack, directly take a usage fault on the current stack.
1562 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_INVPC_MASK
;
1563 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, env
->v7m
.secure
);
1564 qemu_log_mask(CPU_LOG_INT
, "...taking UsageFault on existing "
1565 "stackframe: failed exception return integrity check\n");
1566 v7m_exception_taken(cpu
, excret
, true, false);
1571 * Tailchaining: if there is currently a pending exception that
1572 * is high enough priority to preempt execution at the level we're
1573 * about to return to, then just directly take that exception now,
1574 * avoiding an unstack-and-then-stack. Note that now we have
1575 * deactivated the previous exception by calling armv7m_nvic_complete_irq()
1576 * our current execution priority is already the execution priority we are
1577 * returning to -- none of the state we would unstack or set based on
1578 * the EXCRET value affects it.
1580 if (armv7m_nvic_can_take_pending_exception(env
->nvic
)) {
1581 qemu_log_mask(CPU_LOG_INT
, "...tailchaining to pending exception\n");
1582 v7m_exception_taken(cpu
, excret
, true, false);
1586 switch_v7m_security_state(env
, return_to_secure
);
1590 * The stack pointer we should be reading the exception frame from
1591 * depends on bits in the magic exception return type value (and
1592 * for v8M isn't necessarily the stack pointer we will eventually
1593 * end up resuming execution with). Get a pointer to the location
1594 * in the CPU state struct where the SP we need is currently being
1595 * stored; we will use and modify it in place.
1596 * We use this limited C variable scope so we don't accidentally
1597 * use 'frame_sp_p' after we do something that makes it invalid.
1599 uint32_t *frame_sp_p
= get_v7m_sp_ptr(env
,
1602 return_to_sp_process
);
1603 uint32_t frameptr
= *frame_sp_p
;
1606 bool return_to_priv
= return_to_handler
||
1607 !(env
->v7m
.control
[return_to_secure
] & R_V7M_CONTROL_NPRIV_MASK
);
1609 mmu_idx
= arm_v7m_mmu_idx_for_secstate_and_priv(env
, return_to_secure
,
1612 if (!QEMU_IS_ALIGNED(frameptr
, 8) &&
1613 arm_feature(env
, ARM_FEATURE_V8
)) {
1614 qemu_log_mask(LOG_GUEST_ERROR
,
1615 "M profile exception return with non-8-aligned SP "
1616 "for destination state is UNPREDICTABLE\n");
1619 /* Do we need to pop callee-saved registers? */
1620 if (return_to_secure
&&
1621 ((excret
& R_V7M_EXCRET_ES_MASK
) == 0 ||
1622 (excret
& R_V7M_EXCRET_DCRS_MASK
) == 0)) {
1623 uint32_t actual_sig
;
1625 pop_ok
= v7m_stack_read(cpu
, &actual_sig
, frameptr
, mmu_idx
);
1627 if (pop_ok
&& v7m_integrity_sig(env
, excret
) != actual_sig
) {
1628 /* Take a SecureFault on the current stack */
1629 env
->v7m
.sfsr
|= R_V7M_SFSR_INVIS_MASK
;
1630 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
1631 qemu_log_mask(CPU_LOG_INT
, "...taking SecureFault on existing "
1632 "stackframe: failed exception return integrity "
1633 "signature check\n");
1634 v7m_exception_taken(cpu
, excret
, true, false);
1639 v7m_stack_read(cpu
, &env
->regs
[4], frameptr
+ 0x8, mmu_idx
) &&
1640 v7m_stack_read(cpu
, &env
->regs
[5], frameptr
+ 0xc, mmu_idx
) &&
1641 v7m_stack_read(cpu
, &env
->regs
[6], frameptr
+ 0x10, mmu_idx
) &&
1642 v7m_stack_read(cpu
, &env
->regs
[7], frameptr
+ 0x14, mmu_idx
) &&
1643 v7m_stack_read(cpu
, &env
->regs
[8], frameptr
+ 0x18, mmu_idx
) &&
1644 v7m_stack_read(cpu
, &env
->regs
[9], frameptr
+ 0x1c, mmu_idx
) &&
1645 v7m_stack_read(cpu
, &env
->regs
[10], frameptr
+ 0x20, mmu_idx
) &&
1646 v7m_stack_read(cpu
, &env
->regs
[11], frameptr
+ 0x24, mmu_idx
);
1653 v7m_stack_read(cpu
, &env
->regs
[0], frameptr
, mmu_idx
) &&
1654 v7m_stack_read(cpu
, &env
->regs
[1], frameptr
+ 0x4, mmu_idx
) &&
1655 v7m_stack_read(cpu
, &env
->regs
[2], frameptr
+ 0x8, mmu_idx
) &&
1656 v7m_stack_read(cpu
, &env
->regs
[3], frameptr
+ 0xc, mmu_idx
) &&
1657 v7m_stack_read(cpu
, &env
->regs
[12], frameptr
+ 0x10, mmu_idx
) &&
1658 v7m_stack_read(cpu
, &env
->regs
[14], frameptr
+ 0x14, mmu_idx
) &&
1659 v7m_stack_read(cpu
, &env
->regs
[15], frameptr
+ 0x18, mmu_idx
) &&
1660 v7m_stack_read(cpu
, &xpsr
, frameptr
+ 0x1c, mmu_idx
);
1664 * v7m_stack_read() pended a fault, so take it (as a tail
1665 * chained exception on the same stack frame)
1667 qemu_log_mask(CPU_LOG_INT
, "...derived exception on unstacking\n");
1668 v7m_exception_taken(cpu
, excret
, true, false);
1673 * Returning from an exception with a PC with bit 0 set is defined
1674 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
1675 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
1676 * the lsbit, and there are several RTOSes out there which incorrectly
1677 * assume the r15 in the stack frame should be a Thumb-style "lsbit
1678 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
1679 * complain about the badly behaved guest.
1681 if (env
->regs
[15] & 1) {
1682 env
->regs
[15] &= ~1U;
1683 if (!arm_feature(env
, ARM_FEATURE_V8
)) {
1684 qemu_log_mask(LOG_GUEST_ERROR
,
1685 "M profile return from interrupt with misaligned "
1686 "PC is UNPREDICTABLE on v7M\n");
1690 if (arm_feature(env
, ARM_FEATURE_V8
)) {
1692 * For v8M we have to check whether the xPSR exception field
1693 * matches the EXCRET value for return to handler/thread
1694 * before we commit to changing the SP and xPSR.
1696 bool will_be_handler
= (xpsr
& XPSR_EXCP
) != 0;
1697 if (return_to_handler
!= will_be_handler
) {
1699 * Take an INVPC UsageFault on the current stack.
1700 * By this point we will have switched to the security state
1701 * for the background state, so this UsageFault will target
1704 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
,
1706 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_INVPC_MASK
;
1707 qemu_log_mask(CPU_LOG_INT
, "...taking UsageFault on existing "
1708 "stackframe: failed exception return integrity "
1710 v7m_exception_taken(cpu
, excret
, true, false);
1716 /* FP present and we need to handle it */
1717 if (!return_to_secure
&&
1718 (env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_LSPACT_MASK
)) {
1719 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
1720 env
->v7m
.sfsr
|= R_V7M_SFSR_LSERR_MASK
;
1721 qemu_log_mask(CPU_LOG_INT
,
1722 "...taking SecureFault on existing stackframe: "
1723 "Secure LSPACT set but exception return is "
1724 "not to secure state\n");
1725 v7m_exception_taken(cpu
, excret
, true, false);
1729 restore_s16_s31
= return_to_secure
&&
1730 (env
->v7m
.fpccr
[M_REG_S
] & R_V7M_FPCCR_TS_MASK
);
1732 if (env
->v7m
.fpccr
[return_to_secure
] & R_V7M_FPCCR_LSPACT_MASK
) {
1733 /* State in FPU is still valid, just clear LSPACT */
1734 env
->v7m
.fpccr
[return_to_secure
] &= ~R_V7M_FPCCR_LSPACT_MASK
;
1738 bool cpacr_pass
, nsacr_pass
;
1740 cpacr_pass
= v7m_cpacr_pass(env
, return_to_secure
,
1742 nsacr_pass
= return_to_secure
||
1743 extract32(env
->v7m
.nsacr
, 10, 1);
1746 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
,
1748 env
->v7m
.cfsr
[return_to_secure
] |= R_V7M_CFSR_NOCP_MASK
;
1749 qemu_log_mask(CPU_LOG_INT
,
1750 "...taking UsageFault on existing "
1751 "stackframe: CPACR.CP10 prevents unstacking "
1753 v7m_exception_taken(cpu
, excret
, true, false);
1755 } else if (!nsacr_pass
) {
1756 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, true);
1757 env
->v7m
.cfsr
[M_REG_S
] |= R_V7M_CFSR_INVPC_MASK
;
1758 qemu_log_mask(CPU_LOG_INT
,
1759 "...taking Secure UsageFault on existing "
1760 "stackframe: NSACR.CP10 prevents unstacking "
1762 v7m_exception_taken(cpu
, excret
, true, false);
1766 for (i
= 0; i
< (restore_s16_s31
? 32 : 16); i
+= 2) {
1769 uint32_t faddr
= frameptr
+ 0x20 + 4 * i
;
1772 faddr
+= 8; /* Skip the slot for the FPSCR */
1776 v7m_stack_read(cpu
, &slo
, faddr
, mmu_idx
) &&
1777 v7m_stack_read(cpu
, &shi
, faddr
+ 4, mmu_idx
);
1783 dn
= (uint64_t)shi
<< 32 | slo
;
1784 *aa32_vfp_dreg(env
, i
/ 2) = dn
;
1787 v7m_stack_read(cpu
, &fpscr
, frameptr
+ 0x60, mmu_idx
);
1789 vfp_set_fpscr(env
, fpscr
);
1793 * These regs are 0 if security extension present;
1794 * otherwise merely UNKNOWN. We zero always.
1796 for (i
= 0; i
< (restore_s16_s31
? 32 : 16); i
+= 2) {
1797 *aa32_vfp_dreg(env
, i
/ 2) = 0;
1799 vfp_set_fpscr(env
, 0);
1803 env
->v7m
.control
[M_REG_S
] = FIELD_DP32(env
->v7m
.control
[M_REG_S
],
1804 V7M_CONTROL
, FPCA
, !ftype
);
1806 /* Commit to consuming the stack frame */
1810 if (restore_s16_s31
) {
1815 * Undo stack alignment (the SPREALIGN bit indicates that the original
1816 * pre-exception SP was not 8-aligned and we added a padding word to
1817 * align it, so we undo this by ORing in the bit that increases it
1818 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
1819 * would work too but a logical OR is how the pseudocode specifies it.)
1821 if (xpsr
& XPSR_SPREALIGN
) {
1824 *frame_sp_p
= frameptr
;
1827 xpsr_mask
= ~(XPSR_SPREALIGN
| XPSR_SFPA
);
1828 if (!arm_feature(env
, ARM_FEATURE_THUMB_DSP
)) {
1829 xpsr_mask
&= ~XPSR_GE
;
1831 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
1832 xpsr_write(env
, xpsr
, xpsr_mask
);
1834 if (env
->v7m
.secure
) {
1835 bool sfpa
= xpsr
& XPSR_SFPA
;
1837 env
->v7m
.control
[M_REG_S
] = FIELD_DP32(env
->v7m
.control
[M_REG_S
],
1838 V7M_CONTROL
, SFPA
, sfpa
);
1842 * The restored xPSR exception field will be zero if we're
1843 * resuming in Thread mode. If that doesn't match what the
1844 * exception return excret specified then this is a UsageFault.
1845 * v7M requires we make this check here; v8M did it earlier.
1847 if (return_to_handler
!= arm_v7m_is_handler_mode(env
)) {
1849 * Take an INVPC UsageFault by pushing the stack again;
1850 * we know we're v7M so this is never a Secure UsageFault.
1852 bool ignore_stackfaults
;
1854 assert(!arm_feature(env
, ARM_FEATURE_V8
));
1855 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, false);
1856 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_INVPC_MASK
;
1857 ignore_stackfaults
= v7m_push_stack(cpu
);
1858 qemu_log_mask(CPU_LOG_INT
, "...taking UsageFault on new stackframe: "
1859 "failed exception return integrity check\n");
1860 v7m_exception_taken(cpu
, excret
, false, ignore_stackfaults
);
1864 /* Otherwise, we have a successful exception exit. */
1865 arm_clear_exclusive(env
);
1866 arm_rebuild_hflags(env
);
1867 qemu_log_mask(CPU_LOG_INT
, "...successful exception return\n");
1870 static bool do_v7m_function_return(ARMCPU
*cpu
)
1873 * v8M security extensions magic function return.
1875 * (1) throw an exception (longjump)
1876 * (2) return true if we successfully handled the function return
1877 * (3) return false if we failed a consistency check and have
1878 * pended a UsageFault that needs to be taken now
1880 * At this point the magic return value is split between env->regs[15]
1881 * and env->thumb. We don't bother to reconstitute it because we don't
1882 * need it (all values are handled the same way).
1884 CPUARMState
*env
= &cpu
->env
;
1885 uint32_t newpc
, newpsr
, newpsr_exc
;
1887 qemu_log_mask(CPU_LOG_INT
, "...really v7M secure function return\n");
1890 bool threadmode
, spsel
;
1893 uint32_t *frame_sp_p
;
1896 /* Pull the return address and IPSR from the Secure stack */
1897 threadmode
= !arm_v7m_is_handler_mode(env
);
1898 spsel
= env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_SPSEL_MASK
;
1900 frame_sp_p
= get_v7m_sp_ptr(env
, true, threadmode
, spsel
);
1901 frameptr
= *frame_sp_p
;
1904 * These loads may throw an exception (for MPU faults). We want to
1905 * do them as secure, so work out what MMU index that is.
1907 mmu_idx
= arm_v7m_mmu_idx_for_secstate(env
, true);
1908 oi
= make_memop_idx(MO_LE
, arm_to_core_mmu_idx(mmu_idx
));
1909 newpc
= helper_le_ldul_mmu(env
, frameptr
, oi
, 0);
1910 newpsr
= helper_le_ldul_mmu(env
, frameptr
+ 4, oi
, 0);
1912 /* Consistency checks on new IPSR */
1913 newpsr_exc
= newpsr
& XPSR_EXCP
;
1914 if (!((env
->v7m
.exception
== 0 && newpsr_exc
== 0) ||
1915 (env
->v7m
.exception
== 1 && newpsr_exc
!= 0))) {
1916 /* Pend the fault and tell our caller to take it */
1917 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_INVPC_MASK
;
1918 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
,
1920 qemu_log_mask(CPU_LOG_INT
,
1921 "...taking INVPC UsageFault: "
1922 "IPSR consistency check failed\n");
1926 *frame_sp_p
= frameptr
+ 8;
1929 /* This invalidates frame_sp_p */
1930 switch_v7m_security_state(env
, true);
1931 env
->v7m
.exception
= newpsr_exc
;
1932 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_SFPA_MASK
;
1933 if (newpsr
& XPSR_SFPA
) {
1934 env
->v7m
.control
[M_REG_S
] |= R_V7M_CONTROL_SFPA_MASK
;
1936 xpsr_write(env
, 0, XPSR_IT
);
1937 env
->thumb
= newpc
& 1;
1938 env
->regs
[15] = newpc
& ~1;
1939 arm_rebuild_hflags(env
);
1941 qemu_log_mask(CPU_LOG_INT
, "...function return successful\n");
1945 static bool v7m_read_half_insn(ARMCPU
*cpu
, ARMMMUIdx mmu_idx
,
1946 uint32_t addr
, uint16_t *insn
)
1949 * Load a 16-bit portion of a v7M instruction, returning true on success,
1950 * or false on failure (in which case we will have pended the appropriate
1952 * We need to do the instruction fetch's MPU and SAU checks
1953 * like this because there is no MMU index that would allow
1954 * doing the load with a single function call. Instead we must
1955 * first check that the security attributes permit the load
1956 * and that they don't mismatch on the two halves of the instruction,
1957 * and then we do the load as a secure load (ie using the security
1958 * attributes of the address, not the CPU, as architecturally required).
1960 CPUState
*cs
= CPU(cpu
);
1961 CPUARMState
*env
= &cpu
->env
;
1962 V8M_SAttributes sattrs
= {};
1963 MemTxAttrs attrs
= {};
1964 ARMMMUFaultInfo fi
= {};
1965 ARMCacheAttrs cacheattrs
= {};
1967 target_ulong page_size
;
1971 v8m_security_lookup(env
, addr
, MMU_INST_FETCH
, mmu_idx
, &sattrs
);
1972 if (!sattrs
.nsc
|| sattrs
.ns
) {
1974 * This must be the second half of the insn, and it straddles a
1975 * region boundary with the second half not being S&NSC.
1977 env
->v7m
.sfsr
|= R_V7M_SFSR_INVEP_MASK
;
1978 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
1979 qemu_log_mask(CPU_LOG_INT
,
1980 "...really SecureFault with SFSR.INVEP\n");
1983 if (get_phys_addr(env
, addr
, MMU_INST_FETCH
, mmu_idx
, &physaddr
,
1984 &attrs
, &prot
, &page_size
, &fi
, &cacheattrs
)) {
1985 /* the MPU lookup failed */
1986 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_IACCVIOL_MASK
;
1987 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
, env
->v7m
.secure
);
1988 qemu_log_mask(CPU_LOG_INT
, "...really MemManage with CFSR.IACCVIOL\n");
1991 *insn
= address_space_lduw_le(arm_addressspace(cs
, attrs
), physaddr
,
1993 if (txres
!= MEMTX_OK
) {
1994 env
->v7m
.cfsr
[M_REG_NS
] |= R_V7M_CFSR_IBUSERR_MASK
;
1995 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_BUS
, false);
1996 qemu_log_mask(CPU_LOG_INT
, "...really BusFault with CFSR.IBUSERR\n");
2002 static bool v7m_read_sg_stack_word(ARMCPU
*cpu
, ARMMMUIdx mmu_idx
,
2003 uint32_t addr
, uint32_t *spdata
)
2006 * Read a word of data from the stack for the SG instruction,
2007 * writing the value into *spdata. If the load succeeds, return
2008 * true; otherwise pend an appropriate exception and return false.
2009 * (We can't use data load helpers here that throw an exception
2010 * because of the context we're called in, which is halfway through
2011 * arm_v7m_cpu_do_interrupt().)
2013 CPUState
*cs
= CPU(cpu
);
2014 CPUARMState
*env
= &cpu
->env
;
2015 MemTxAttrs attrs
= {};
2017 target_ulong page_size
;
2020 ARMMMUFaultInfo fi
= {};
2021 ARMCacheAttrs cacheattrs
= {};
2024 if (get_phys_addr(env
, addr
, MMU_DATA_LOAD
, mmu_idx
, &physaddr
,
2025 &attrs
, &prot
, &page_size
, &fi
, &cacheattrs
)) {
2026 /* MPU/SAU lookup failed */
2027 if (fi
.type
== ARMFault_QEMU_SFault
) {
2028 qemu_log_mask(CPU_LOG_INT
,
2029 "...SecureFault during stack word read\n");
2030 env
->v7m
.sfsr
|= R_V7M_SFSR_AUVIOL_MASK
| R_V7M_SFSR_SFARVALID_MASK
;
2031 env
->v7m
.sfar
= addr
;
2032 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
2034 qemu_log_mask(CPU_LOG_INT
,
2035 "...MemManageFault during stack word read\n");
2036 env
->v7m
.cfsr
[M_REG_S
] |= R_V7M_CFSR_DACCVIOL_MASK
|
2037 R_V7M_CFSR_MMARVALID_MASK
;
2038 env
->v7m
.mmfar
[M_REG_S
] = addr
;
2039 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
, false);
2043 value
= address_space_ldl(arm_addressspace(cs
, attrs
), physaddr
,
2045 if (txres
!= MEMTX_OK
) {
2046 /* BusFault trying to read the data */
2047 qemu_log_mask(CPU_LOG_INT
,
2048 "...BusFault during stack word read\n");
2049 env
->v7m
.cfsr
[M_REG_NS
] |=
2050 (R_V7M_CFSR_PRECISERR_MASK
| R_V7M_CFSR_BFARVALID_MASK
);
2051 env
->v7m
.bfar
= addr
;
2052 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_BUS
, false);
2060 static bool v7m_handle_execute_nsc(ARMCPU
*cpu
)
2063 * Check whether this attempt to execute code in a Secure & NS-Callable
2064 * memory region is for an SG instruction; if so, then emulate the
2065 * effect of the SG instruction and return true. Otherwise pend
2066 * the correct kind of exception and return false.
2068 CPUARMState
*env
= &cpu
->env
;
2073 * We should never get here unless get_phys_addr_pmsav8() caused
2074 * an exception for NS executing in S&NSC memory.
2076 assert(!env
->v7m
.secure
);
2077 assert(arm_feature(env
, ARM_FEATURE_M_SECURITY
));
2079 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
2080 mmu_idx
= arm_v7m_mmu_idx_for_secstate(env
, true);
2082 if (!v7m_read_half_insn(cpu
, mmu_idx
, env
->regs
[15], &insn
)) {
2090 if (insn
!= 0xe97f) {
2092 * Not an SG instruction first half (we choose the IMPDEF
2093 * early-SG-check option).
2098 if (!v7m_read_half_insn(cpu
, mmu_idx
, env
->regs
[15] + 2, &insn
)) {
2102 if (insn
!= 0xe97f) {
2104 * Not an SG instruction second half (yes, both halves of the SG
2105 * insn have the same hex value)
2111 * OK, we have confirmed that we really have an SG instruction.
2112 * We know we're NS in S memory so don't need to repeat those checks.
2114 qemu_log_mask(CPU_LOG_INT
, "...really an SG instruction at 0x%08" PRIx32
2115 ", executing it\n", env
->regs
[15]);
2117 if (cpu_isar_feature(aa32_m_sec_state
, cpu
) &&
2118 !arm_v7m_is_handler_mode(env
)) {
2120 * v8.1M exception stack frame integrity check. Note that we
2121 * must perform the memory access even if CCR_S.TRD is zero
2122 * and we aren't going to check what the data loaded is.
2124 uint32_t spdata
, sp
;
2127 * We know we are currently NS, so the S stack pointers must be
2128 * in other_ss_{psp,msp}, not in regs[13]/other_sp.
2130 sp
= v7m_using_psp(env
) ? env
->v7m
.other_ss_psp
: env
->v7m
.other_ss_msp
;
2131 if (!v7m_read_sg_stack_word(cpu
, mmu_idx
, sp
, &spdata
)) {
2132 /* Stack access failed and an exception has been pended */
2136 if (env
->v7m
.ccr
[M_REG_S
] & R_V7M_CCR_TRD_MASK
) {
2137 if (((spdata
& ~1) == 0xfefa125a) ||
2138 !(env
->v7m
.control
[M_REG_S
] & 1)) {
2144 env
->regs
[14] &= ~1;
2145 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_SFPA_MASK
;
2146 switch_v7m_security_state(env
, true);
2147 xpsr_write(env
, 0, XPSR_IT
);
2149 arm_rebuild_hflags(env
);
2153 env
->v7m
.sfsr
|= R_V7M_SFSR_INVEP_MASK
;
2154 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
2155 qemu_log_mask(CPU_LOG_INT
,
2156 "...really SecureFault with SFSR.INVEP\n");
2160 void arm_v7m_cpu_do_interrupt(CPUState
*cs
)
2162 ARMCPU
*cpu
= ARM_CPU(cs
);
2163 CPUARMState
*env
= &cpu
->env
;
2165 bool ignore_stackfaults
;
2167 arm_log_exception(cs
->exception_index
);
2170 * For exceptions we just mark as pending on the NVIC, and let that
2173 switch (cs
->exception_index
) {
2175 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, env
->v7m
.secure
);
2176 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_UNDEFINSTR_MASK
;
2181 * NOCP might be directed to something other than the current
2182 * security state if this fault is because of NSACR; we indicate
2183 * the target security state using exception.target_el.
2185 int target_secstate
;
2187 if (env
->exception
.target_el
== 3) {
2188 target_secstate
= M_REG_S
;
2190 target_secstate
= env
->v7m
.secure
;
2192 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, target_secstate
);
2193 env
->v7m
.cfsr
[target_secstate
] |= R_V7M_CFSR_NOCP_MASK
;
2197 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, env
->v7m
.secure
);
2198 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_INVSTATE_MASK
;
2201 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, env
->v7m
.secure
);
2202 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_STKOF_MASK
;
2205 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
2206 env
->v7m
.sfsr
|= R_V7M_SFSR_LSERR_MASK
;
2208 case EXCP_UNALIGNED
:
2209 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
, env
->v7m
.secure
);
2210 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_UNALIGNED_MASK
;
2213 /* The PC already points to the next instruction. */
2214 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SVC
, env
->v7m
.secure
);
2216 case EXCP_PREFETCH_ABORT
:
2217 case EXCP_DATA_ABORT
:
2219 * Note that for M profile we don't have a guest facing FSR, but
2220 * the env->exception.fsr will be populated by the code that
2221 * raises the fault, in the A profile short-descriptor format.
2223 switch (env
->exception
.fsr
& 0xf) {
2224 case M_FAKE_FSR_NSC_EXEC
:
2226 * Exception generated when we try to execute code at an address
2227 * which is marked as Secure & Non-Secure Callable and the CPU
2228 * is in the Non-Secure state. The only instruction which can
2229 * be executed like this is SG (and that only if both halves of
2230 * the SG instruction have the same security attributes.)
2231 * Everything else must generate an INVEP SecureFault, so we
2232 * emulate the SG instruction here.
2234 if (v7m_handle_execute_nsc(cpu
)) {
2238 case M_FAKE_FSR_SFAULT
:
2240 * Various flavours of SecureFault for attempts to execute or
2241 * access data in the wrong security state.
2243 switch (cs
->exception_index
) {
2244 case EXCP_PREFETCH_ABORT
:
2245 if (env
->v7m
.secure
) {
2246 env
->v7m
.sfsr
|= R_V7M_SFSR_INVTRAN_MASK
;
2247 qemu_log_mask(CPU_LOG_INT
,
2248 "...really SecureFault with SFSR.INVTRAN\n");
2250 env
->v7m
.sfsr
|= R_V7M_SFSR_INVEP_MASK
;
2251 qemu_log_mask(CPU_LOG_INT
,
2252 "...really SecureFault with SFSR.INVEP\n");
2255 case EXCP_DATA_ABORT
:
2256 /* This must be an NS access to S memory */
2257 env
->v7m
.sfsr
|= R_V7M_SFSR_AUVIOL_MASK
;
2258 qemu_log_mask(CPU_LOG_INT
,
2259 "...really SecureFault with SFSR.AUVIOL\n");
2262 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SECURE
, false);
2264 case 0x8: /* External Abort */
2265 switch (cs
->exception_index
) {
2266 case EXCP_PREFETCH_ABORT
:
2267 env
->v7m
.cfsr
[M_REG_NS
] |= R_V7M_CFSR_IBUSERR_MASK
;
2268 qemu_log_mask(CPU_LOG_INT
, "...with CFSR.IBUSERR\n");
2270 case EXCP_DATA_ABORT
:
2271 env
->v7m
.cfsr
[M_REG_NS
] |=
2272 (R_V7M_CFSR_PRECISERR_MASK
| R_V7M_CFSR_BFARVALID_MASK
);
2273 env
->v7m
.bfar
= env
->exception
.vaddress
;
2274 qemu_log_mask(CPU_LOG_INT
,
2275 "...with CFSR.PRECISERR and BFAR 0x%x\n",
2279 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_BUS
, false);
2283 * All other FSR values are either MPU faults or "can't happen
2284 * for M profile" cases.
2286 switch (cs
->exception_index
) {
2287 case EXCP_PREFETCH_ABORT
:
2288 env
->v7m
.cfsr
[env
->v7m
.secure
] |= R_V7M_CFSR_IACCVIOL_MASK
;
2289 qemu_log_mask(CPU_LOG_INT
, "...with CFSR.IACCVIOL\n");
2291 case EXCP_DATA_ABORT
:
2292 env
->v7m
.cfsr
[env
->v7m
.secure
] |=
2293 (R_V7M_CFSR_DACCVIOL_MASK
| R_V7M_CFSR_MMARVALID_MASK
);
2294 env
->v7m
.mmfar
[env
->v7m
.secure
] = env
->exception
.vaddress
;
2295 qemu_log_mask(CPU_LOG_INT
,
2296 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
2297 env
->v7m
.mmfar
[env
->v7m
.secure
]);
2300 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
,
2306 qemu_log_mask(CPU_LOG_INT
,
2307 "...handling as semihosting call 0x%x\n",
2309 env
->regs
[0] = do_arm_semihosting(env
);
2310 env
->regs
[15] += env
->thumb
? 2 : 4;
2313 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_DEBUG
, false);
2317 case EXCP_EXCEPTION_EXIT
:
2318 if (env
->regs
[15] < EXC_RETURN_MIN_MAGIC
) {
2319 /* Must be v8M security extension function return */
2320 assert(env
->regs
[15] >= FNC_RETURN_MIN_MAGIC
);
2321 assert(arm_feature(env
, ARM_FEATURE_M_SECURITY
));
2322 if (do_v7m_function_return(cpu
)) {
2326 do_v7m_exception_exit(cpu
);
2332 * We already pended the specific exception in the NVIC in the
2333 * v7m_preserve_fp_state() helper function.
2337 cpu_abort(cs
, "Unhandled exception 0x%x\n", cs
->exception_index
);
2338 return; /* Never happens. Keep compiler happy. */
2341 if (arm_feature(env
, ARM_FEATURE_V8
)) {
2342 lr
= R_V7M_EXCRET_RES1_MASK
|
2343 R_V7M_EXCRET_DCRS_MASK
;
2345 * The S bit indicates whether we should return to Secure
2346 * or NonSecure (ie our current state).
2347 * The ES bit indicates whether we're taking this exception
2348 * to Secure or NonSecure (ie our target state). We set it
2349 * later, in v7m_exception_taken().
2350 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
2351 * This corresponds to the ARM ARM pseudocode for v8M setting
2352 * some LR bits in PushStack() and some in ExceptionTaken();
2353 * the distinction matters for the tailchain cases where we
2354 * can take an exception without pushing the stack.
2356 if (env
->v7m
.secure
) {
2357 lr
|= R_V7M_EXCRET_S_MASK
;
2360 lr
= R_V7M_EXCRET_RES1_MASK
|
2361 R_V7M_EXCRET_S_MASK
|
2362 R_V7M_EXCRET_DCRS_MASK
|
2363 R_V7M_EXCRET_ES_MASK
;
2364 if (env
->v7m
.control
[M_REG_NS
] & R_V7M_CONTROL_SPSEL_MASK
) {
2365 lr
|= R_V7M_EXCRET_SPSEL_MASK
;
2368 if (!(env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_FPCA_MASK
)) {
2369 lr
|= R_V7M_EXCRET_FTYPE_MASK
;
2371 if (!arm_v7m_is_handler_mode(env
)) {
2372 lr
|= R_V7M_EXCRET_MODE_MASK
;
2375 ignore_stackfaults
= v7m_push_stack(cpu
);
2376 v7m_exception_taken(cpu
, lr
, false, ignore_stackfaults
);
2379 uint32_t HELPER(v7m_mrs
)(CPUARMState
*env
, uint32_t reg
)
2381 unsigned el
= arm_current_el(env
);
2383 /* First handle registers which unprivileged can read */
2385 case 0 ... 7: /* xPSR sub-fields */
2386 return v7m_mrs_xpsr(env
, reg
, el
);
2387 case 20: /* CONTROL */
2388 return v7m_mrs_control(env
, env
->v7m
.secure
);
2389 case 0x94: /* CONTROL_NS */
2391 * We have to handle this here because unprivileged Secure code
2392 * can read the NS CONTROL register.
2394 if (!env
->v7m
.secure
) {
2397 return env
->v7m
.control
[M_REG_NS
] |
2398 (env
->v7m
.control
[M_REG_S
] & R_V7M_CONTROL_FPCA_MASK
);
2402 return 0; /* unprivileged reads others as zero */
2405 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
2407 case 0x88: /* MSP_NS */
2408 if (!env
->v7m
.secure
) {
2411 return env
->v7m
.other_ss_msp
;
2412 case 0x89: /* PSP_NS */
2413 if (!env
->v7m
.secure
) {
2416 return env
->v7m
.other_ss_psp
;
2417 case 0x8a: /* MSPLIM_NS */
2418 if (!env
->v7m
.secure
) {
2421 return env
->v7m
.msplim
[M_REG_NS
];
2422 case 0x8b: /* PSPLIM_NS */
2423 if (!env
->v7m
.secure
) {
2426 return env
->v7m
.psplim
[M_REG_NS
];
2427 case 0x90: /* PRIMASK_NS */
2428 if (!env
->v7m
.secure
) {
2431 return env
->v7m
.primask
[M_REG_NS
];
2432 case 0x91: /* BASEPRI_NS */
2433 if (!env
->v7m
.secure
) {
2436 return env
->v7m
.basepri
[M_REG_NS
];
2437 case 0x93: /* FAULTMASK_NS */
2438 if (!env
->v7m
.secure
) {
2441 return env
->v7m
.faultmask
[M_REG_NS
];
2442 case 0x98: /* SP_NS */
2445 * This gives the non-secure SP selected based on whether we're
2446 * currently in handler mode or not, using the NS CONTROL.SPSEL.
2448 bool spsel
= env
->v7m
.control
[M_REG_NS
] & R_V7M_CONTROL_SPSEL_MASK
;
2450 if (!env
->v7m
.secure
) {
2453 if (!arm_v7m_is_handler_mode(env
) && spsel
) {
2454 return env
->v7m
.other_ss_psp
;
2456 return env
->v7m
.other_ss_msp
;
2466 return v7m_using_psp(env
) ? env
->v7m
.other_sp
: env
->regs
[13];
2468 return v7m_using_psp(env
) ? env
->regs
[13] : env
->v7m
.other_sp
;
2469 case 10: /* MSPLIM */
2470 if (!arm_feature(env
, ARM_FEATURE_V8
)) {
2473 return env
->v7m
.msplim
[env
->v7m
.secure
];
2474 case 11: /* PSPLIM */
2475 if (!arm_feature(env
, ARM_FEATURE_V8
)) {
2478 return env
->v7m
.psplim
[env
->v7m
.secure
];
2479 case 16: /* PRIMASK */
2480 return env
->v7m
.primask
[env
->v7m
.secure
];
2481 case 17: /* BASEPRI */
2482 case 18: /* BASEPRI_MAX */
2483 return env
->v7m
.basepri
[env
->v7m
.secure
];
2484 case 19: /* FAULTMASK */
2485 return env
->v7m
.faultmask
[env
->v7m
.secure
];
2488 qemu_log_mask(LOG_GUEST_ERROR
, "Attempt to read unknown special"
2489 " register %d\n", reg
);
2494 void HELPER(v7m_msr
)(CPUARMState
*env
, uint32_t maskreg
, uint32_t val
)
2497 * We're passed bits [11..0] of the instruction; extract
2498 * SYSm and the mask bits.
2499 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
2500 * we choose to treat them as if the mask bits were valid.
2501 * NB that the pseudocode 'mask' variable is bits [11..10],
2502 * whereas ours is [11..8].
2504 uint32_t mask
= extract32(maskreg
, 8, 4);
2505 uint32_t reg
= extract32(maskreg
, 0, 8);
2506 int cur_el
= arm_current_el(env
);
2508 if (cur_el
== 0 && reg
> 7 && reg
!= 20) {
2510 * only xPSR sub-fields and CONTROL.SFPA may be written by
2516 if (arm_feature(env
, ARM_FEATURE_M_SECURITY
)) {
2518 case 0x88: /* MSP_NS */
2519 if (!env
->v7m
.secure
) {
2522 env
->v7m
.other_ss_msp
= val
;
2524 case 0x89: /* PSP_NS */
2525 if (!env
->v7m
.secure
) {
2528 env
->v7m
.other_ss_psp
= val
;
2530 case 0x8a: /* MSPLIM_NS */
2531 if (!env
->v7m
.secure
) {
2534 env
->v7m
.msplim
[M_REG_NS
] = val
& ~7;
2536 case 0x8b: /* PSPLIM_NS */
2537 if (!env
->v7m
.secure
) {
2540 env
->v7m
.psplim
[M_REG_NS
] = val
& ~7;
2542 case 0x90: /* PRIMASK_NS */
2543 if (!env
->v7m
.secure
) {
2546 env
->v7m
.primask
[M_REG_NS
] = val
& 1;
2548 case 0x91: /* BASEPRI_NS */
2549 if (!env
->v7m
.secure
|| !arm_feature(env
, ARM_FEATURE_M_MAIN
)) {
2552 env
->v7m
.basepri
[M_REG_NS
] = val
& 0xff;
2554 case 0x93: /* FAULTMASK_NS */
2555 if (!env
->v7m
.secure
|| !arm_feature(env
, ARM_FEATURE_M_MAIN
)) {
2558 env
->v7m
.faultmask
[M_REG_NS
] = val
& 1;
2560 case 0x94: /* CONTROL_NS */
2561 if (!env
->v7m
.secure
) {
2564 write_v7m_control_spsel_for_secstate(env
,
2565 val
& R_V7M_CONTROL_SPSEL_MASK
,
2567 if (arm_feature(env
, ARM_FEATURE_M_MAIN
)) {
2568 env
->v7m
.control
[M_REG_NS
] &= ~R_V7M_CONTROL_NPRIV_MASK
;
2569 env
->v7m
.control
[M_REG_NS
] |= val
& R_V7M_CONTROL_NPRIV_MASK
;
2572 * SFPA is RAZ/WI from NS. FPCA is RO if NSACR.CP10 == 0,
2573 * RES0 if the FPU is not present, and is stored in the S bank
2575 if (cpu_isar_feature(aa32_vfp_simd
, env_archcpu(env
)) &&
2576 extract32(env
->v7m
.nsacr
, 10, 1)) {
2577 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_FPCA_MASK
;
2578 env
->v7m
.control
[M_REG_S
] |= val
& R_V7M_CONTROL_FPCA_MASK
;
2581 case 0x98: /* SP_NS */
2584 * This gives the non-secure SP selected based on whether we're
2585 * currently in handler mode or not, using the NS CONTROL.SPSEL.
2587 bool spsel
= env
->v7m
.control
[M_REG_NS
] & R_V7M_CONTROL_SPSEL_MASK
;
2588 bool is_psp
= !arm_v7m_is_handler_mode(env
) && spsel
;
2591 if (!env
->v7m
.secure
) {
2595 limit
= is_psp
? env
->v7m
.psplim
[false] : env
->v7m
.msplim
[false];
2598 CPUState
*cs
= env_cpu(env
);
2600 cpu_restore_state(cs
, GETPC(), true);
2601 raise_exception(env
, EXCP_STKOF
, 0, 1);
2605 env
->v7m
.other_ss_psp
= val
;
2607 env
->v7m
.other_ss_msp
= val
;
2617 case 0 ... 7: /* xPSR sub-fields */
2618 v7m_msr_xpsr(env
, mask
, reg
, val
);
2621 if (v7m_using_psp(env
)) {
2622 env
->v7m
.other_sp
= val
;
2624 env
->regs
[13] = val
;
2628 if (v7m_using_psp(env
)) {
2629 env
->regs
[13] = val
;
2631 env
->v7m
.other_sp
= val
;
2634 case 10: /* MSPLIM */
2635 if (!arm_feature(env
, ARM_FEATURE_V8
)) {
2638 env
->v7m
.msplim
[env
->v7m
.secure
] = val
& ~7;
2640 case 11: /* PSPLIM */
2641 if (!arm_feature(env
, ARM_FEATURE_V8
)) {
2644 env
->v7m
.psplim
[env
->v7m
.secure
] = val
& ~7;
2646 case 16: /* PRIMASK */
2647 env
->v7m
.primask
[env
->v7m
.secure
] = val
& 1;
2649 case 17: /* BASEPRI */
2650 if (!arm_feature(env
, ARM_FEATURE_M_MAIN
)) {
2653 env
->v7m
.basepri
[env
->v7m
.secure
] = val
& 0xff;
2655 case 18: /* BASEPRI_MAX */
2656 if (!arm_feature(env
, ARM_FEATURE_M_MAIN
)) {
2660 if (val
!= 0 && (val
< env
->v7m
.basepri
[env
->v7m
.secure
]
2661 || env
->v7m
.basepri
[env
->v7m
.secure
] == 0)) {
2662 env
->v7m
.basepri
[env
->v7m
.secure
] = val
;
2665 case 19: /* FAULTMASK */
2666 if (!arm_feature(env
, ARM_FEATURE_M_MAIN
)) {
2669 env
->v7m
.faultmask
[env
->v7m
.secure
] = val
& 1;
2671 case 20: /* CONTROL */
2673 * Writing to the SPSEL bit only has an effect if we are in
2674 * thread mode; other bits can be updated by any privileged code.
2675 * write_v7m_control_spsel() deals with updating the SPSEL bit in
2676 * env->v7m.control, so we only need update the others.
2677 * For v7M, we must just ignore explicit writes to SPSEL in handler
2678 * mode; for v8M the write is permitted but will have no effect.
2679 * All these bits are writes-ignored from non-privileged code,
2682 if (cur_el
> 0 && (arm_feature(env
, ARM_FEATURE_V8
) ||
2683 !arm_v7m_is_handler_mode(env
))) {
2684 write_v7m_control_spsel(env
, (val
& R_V7M_CONTROL_SPSEL_MASK
) != 0);
2686 if (cur_el
> 0 && arm_feature(env
, ARM_FEATURE_M_MAIN
)) {
2687 env
->v7m
.control
[env
->v7m
.secure
] &= ~R_V7M_CONTROL_NPRIV_MASK
;
2688 env
->v7m
.control
[env
->v7m
.secure
] |= val
& R_V7M_CONTROL_NPRIV_MASK
;
2690 if (cpu_isar_feature(aa32_vfp_simd
, env_archcpu(env
))) {
2692 * SFPA is RAZ/WI from NS or if no FPU.
2693 * FPCA is RO if NSACR.CP10 == 0, RES0 if the FPU is not present.
2694 * Both are stored in the S bank.
2696 if (env
->v7m
.secure
) {
2697 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_SFPA_MASK
;
2698 env
->v7m
.control
[M_REG_S
] |= val
& R_V7M_CONTROL_SFPA_MASK
;
2701 (env
->v7m
.secure
|| !arm_feature(env
, ARM_FEATURE_M_SECURITY
) ||
2702 extract32(env
->v7m
.nsacr
, 10, 1))) {
2703 env
->v7m
.control
[M_REG_S
] &= ~R_V7M_CONTROL_FPCA_MASK
;
2704 env
->v7m
.control
[M_REG_S
] |= val
& R_V7M_CONTROL_FPCA_MASK
;
2710 qemu_log_mask(LOG_GUEST_ERROR
, "Attempt to write unknown special"
2711 " register %d\n", reg
);
2716 uint32_t HELPER(v7m_tt
)(CPUARMState
*env
, uint32_t addr
, uint32_t op
)
2718 /* Implement the TT instruction. op is bits [7:6] of the insn. */
2719 bool forceunpriv
= op
& 1;
2721 V8M_SAttributes sattrs
= {};
2723 bool r
, rw
, nsr
, nsrw
, mrvalid
;
2725 ARMMMUFaultInfo fi
= {};
2726 MemTxAttrs attrs
= {};
2731 bool targetsec
= env
->v7m
.secure
;
2735 * Work out what the security state and privilege level we're
2736 * interested in is...
2739 targetsec
= !targetsec
;
2745 targetpriv
= arm_v7m_is_handler_mode(env
) ||
2746 !(env
->v7m
.control
[targetsec
] & R_V7M_CONTROL_NPRIV_MASK
);
2749 /* ...and then figure out which MMU index this is */
2750 mmu_idx
= arm_v7m_mmu_idx_for_secstate_and_priv(env
, targetsec
, targetpriv
);
2753 * We know that the MPU and SAU don't care about the access type
2754 * for our purposes beyond that we don't want to claim to be
2755 * an insn fetch, so we arbitrarily call this a read.
2759 * MPU region info only available for privileged or if
2760 * inspecting the other MPU state.
2762 if (arm_current_el(env
) != 0 || alt
) {
2763 /* We can ignore the return value as prot is always set */
2764 pmsav8_mpu_lookup(env
, addr
, MMU_DATA_LOAD
, mmu_idx
,
2765 &phys_addr
, &attrs
, &prot
, &is_subpage
,
2767 if (mregion
== -1) {
2773 r
= prot
& PAGE_READ
;
2774 rw
= prot
& PAGE_WRITE
;
2782 if (env
->v7m
.secure
) {
2783 v8m_security_lookup(env
, addr
, MMU_DATA_LOAD
, mmu_idx
, &sattrs
);
2784 nsr
= sattrs
.ns
&& r
;
2785 nsrw
= sattrs
.ns
&& rw
;
2792 tt_resp
= (sattrs
.iregion
<< 24) |
2793 (sattrs
.irvalid
<< 23) |
2794 ((!sattrs
.ns
) << 22) |
2799 (sattrs
.srvalid
<< 17) |
2801 (sattrs
.sregion
<< 8) |
2807 #endif /* !CONFIG_USER_ONLY */
2809 ARMMMUIdx
arm_v7m_mmu_idx_all(CPUARMState
*env
,
2810 bool secstate
, bool priv
, bool negpri
)
2812 ARMMMUIdx mmu_idx
= ARM_MMU_IDX_M
;
2815 mmu_idx
|= ARM_MMU_IDX_M_PRIV
;
2819 mmu_idx
|= ARM_MMU_IDX_M_NEGPRI
;
2823 mmu_idx
|= ARM_MMU_IDX_M_S
;
2829 ARMMMUIdx
arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState
*env
,
2830 bool secstate
, bool priv
)
2832 bool negpri
= armv7m_nvic_neg_prio_requested(env
->nvic
, secstate
);
2834 return arm_v7m_mmu_idx_all(env
, secstate
, priv
, negpri
);
2837 /* Return the MMU index for a v7M CPU in the specified security state */
2838 ARMMMUIdx
arm_v7m_mmu_idx_for_secstate(CPUARMState
*env
, bool secstate
)
2840 bool priv
= arm_v7m_is_handler_mode(env
) ||
2841 !(env
->v7m
.control
[secstate
] & 1);
2843 return arm_v7m_mmu_idx_for_secstate_and_priv(env
, secstate
, priv
);