pcihp: compose PCNT callchain right before its user _GPE._E01
[qemu.git] / target / arm / m_helper.c
blobe7e746ea182687c391b6b5b848ca17e9fd51b4b2
1 /*
2 * ARM generic helpers.
4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include "qemu/osdep.h"
10 #include "cpu.h"
11 #include "internals.h"
12 #include "exec/helper-proto.h"
13 #include "qemu/main-loop.h"
14 #include "qemu/bitops.h"
15 #include "qemu/log.h"
16 #include "exec/exec-all.h"
17 #ifdef CONFIG_TCG
18 #include "exec/cpu_ldst.h"
19 #include "semihosting/common-semi.h"
20 #endif
22 static void v7m_msr_xpsr(CPUARMState *env, uint32_t mask,
23 uint32_t reg, uint32_t val)
25 /* Only APSR is actually writable */
26 if (!(reg & 4)) {
27 uint32_t apsrmask = 0;
29 if (mask & 8) {
30 apsrmask |= XPSR_NZCV | XPSR_Q;
32 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
33 apsrmask |= XPSR_GE;
35 xpsr_write(env, val, apsrmask);
39 static uint32_t v7m_mrs_xpsr(CPUARMState *env, uint32_t reg, unsigned el)
41 uint32_t mask = 0;
43 if ((reg & 1) && el) {
44 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
46 if (!(reg & 4)) {
47 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
48 if (arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
49 mask |= XPSR_GE;
52 /* EPSR reads as zero */
53 return xpsr_read(env) & mask;
56 static uint32_t v7m_mrs_control(CPUARMState *env, uint32_t secure)
58 uint32_t value = env->v7m.control[secure];
60 if (!secure) {
61 /* SFPA is RAZ/WI from NS; FPCA is stored in the M_REG_S bank */
62 value |= env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK;
64 return value;
67 #ifdef CONFIG_USER_ONLY
69 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
71 uint32_t mask = extract32(maskreg, 8, 4);
72 uint32_t reg = extract32(maskreg, 0, 8);
74 switch (reg) {
75 case 0 ... 7: /* xPSR sub-fields */
76 v7m_msr_xpsr(env, mask, reg, val);
77 break;
78 case 20: /* CONTROL */
79 /* There are no sub-fields that are actually writable from EL0. */
80 break;
81 default:
82 /* Unprivileged writes to other registers are ignored */
83 break;
87 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
89 switch (reg) {
90 case 0 ... 7: /* xPSR sub-fields */
91 return v7m_mrs_xpsr(env, reg, 0);
92 case 20: /* CONTROL */
93 return v7m_mrs_control(env, 0);
94 default:
95 /* Unprivileged reads others as zero. */
96 return 0;
100 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
102 /* translate.c should never generate calls here in user-only mode */
103 g_assert_not_reached();
106 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
108 /* translate.c should never generate calls here in user-only mode */
109 g_assert_not_reached();
112 void HELPER(v7m_preserve_fp_state)(CPUARMState *env)
114 /* translate.c should never generate calls here in user-only mode */
115 g_assert_not_reached();
118 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr)
120 /* translate.c should never generate calls here in user-only mode */
121 g_assert_not_reached();
124 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr)
126 /* translate.c should never generate calls here in user-only mode */
127 g_assert_not_reached();
130 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
133 * The TT instructions can be used by unprivileged code, but in
134 * user-only emulation we don't have the MPU.
135 * Luckily since we know we are NonSecure unprivileged (and that in
136 * turn means that the A flag wasn't specified), all the bits in the
137 * register must be zero:
138 * IREGION: 0 because IRVALID is 0
139 * IRVALID: 0 because NS
140 * S: 0 because NS
141 * NSRW: 0 because NS
142 * NSR: 0 because NS
143 * RW: 0 because unpriv and A flag not set
144 * R: 0 because unpriv and A flag not set
145 * SRVALID: 0 because NS
146 * MRVALID: 0 because unpriv and A flag not set
147 * SREGION: 0 becaus SRVALID is 0
148 * MREGION: 0 because MRVALID is 0
150 return 0;
153 #else
156 * What kind of stack write are we doing? This affects how exceptions
157 * generated during the stacking are treated.
159 typedef enum StackingMode {
160 STACK_NORMAL,
161 STACK_IGNFAULTS,
162 STACK_LAZYFP,
163 } StackingMode;
165 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
166 ARMMMUIdx mmu_idx, StackingMode mode)
168 CPUState *cs = CPU(cpu);
169 CPUARMState *env = &cpu->env;
170 MemTxResult txres;
171 GetPhysAddrResult res = {};
172 ARMMMUFaultInfo fi = {};
173 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
174 int exc;
175 bool exc_secure;
177 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &res, &fi)) {
178 /* MPU/SAU lookup failed */
179 if (fi.type == ARMFault_QEMU_SFault) {
180 if (mode == STACK_LAZYFP) {
181 qemu_log_mask(CPU_LOG_INT,
182 "...SecureFault with SFSR.LSPERR "
183 "during lazy stacking\n");
184 env->v7m.sfsr |= R_V7M_SFSR_LSPERR_MASK;
185 } else {
186 qemu_log_mask(CPU_LOG_INT,
187 "...SecureFault with SFSR.AUVIOL "
188 "during stacking\n");
189 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
191 env->v7m.sfsr |= R_V7M_SFSR_SFARVALID_MASK;
192 env->v7m.sfar = addr;
193 exc = ARMV7M_EXCP_SECURE;
194 exc_secure = false;
195 } else {
196 if (mode == STACK_LAZYFP) {
197 qemu_log_mask(CPU_LOG_INT,
198 "...MemManageFault with CFSR.MLSPERR\n");
199 env->v7m.cfsr[secure] |= R_V7M_CFSR_MLSPERR_MASK;
200 } else {
201 qemu_log_mask(CPU_LOG_INT,
202 "...MemManageFault with CFSR.MSTKERR\n");
203 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
205 exc = ARMV7M_EXCP_MEM;
206 exc_secure = secure;
208 goto pend_fault;
210 address_space_stl_le(arm_addressspace(cs, res.f.attrs), res.f.phys_addr,
211 value, res.f.attrs, &txres);
212 if (txres != MEMTX_OK) {
213 /* BusFault trying to write the data */
214 if (mode == STACK_LAZYFP) {
215 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.LSPERR\n");
216 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_LSPERR_MASK;
217 } else {
218 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
219 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
221 exc = ARMV7M_EXCP_BUS;
222 exc_secure = false;
223 goto pend_fault;
225 return true;
227 pend_fault:
229 * By pending the exception at this point we are making
230 * the IMPDEF choice "overridden exceptions pended" (see the
231 * MergeExcInfo() pseudocode). The other choice would be to not
232 * pend them now and then make a choice about which to throw away
233 * later if we have two derived exceptions.
234 * The only case when we must not pend the exception but instead
235 * throw it away is if we are doing the push of the callee registers
236 * and we've already generated a derived exception (this is indicated
237 * by the caller passing STACK_IGNFAULTS). Even in this case we will
238 * still update the fault status registers.
240 switch (mode) {
241 case STACK_NORMAL:
242 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
243 break;
244 case STACK_LAZYFP:
245 armv7m_nvic_set_pending_lazyfp(env->nvic, exc, exc_secure);
246 break;
247 case STACK_IGNFAULTS:
248 break;
250 return false;
253 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
254 ARMMMUIdx mmu_idx)
256 CPUState *cs = CPU(cpu);
257 CPUARMState *env = &cpu->env;
258 MemTxResult txres;
259 GetPhysAddrResult res = {};
260 ARMMMUFaultInfo fi = {};
261 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
262 int exc;
263 bool exc_secure;
264 uint32_t value;
266 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &res, &fi)) {
267 /* MPU/SAU lookup failed */
268 if (fi.type == ARMFault_QEMU_SFault) {
269 qemu_log_mask(CPU_LOG_INT,
270 "...SecureFault with SFSR.AUVIOL during unstack\n");
271 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
272 env->v7m.sfar = addr;
273 exc = ARMV7M_EXCP_SECURE;
274 exc_secure = false;
275 } else {
276 qemu_log_mask(CPU_LOG_INT,
277 "...MemManageFault with CFSR.MUNSTKERR\n");
278 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
279 exc = ARMV7M_EXCP_MEM;
280 exc_secure = secure;
282 goto pend_fault;
285 value = address_space_ldl(arm_addressspace(cs, res.f.attrs),
286 res.f.phys_addr, res.f.attrs, &txres);
287 if (txres != MEMTX_OK) {
288 /* BusFault trying to read the data */
289 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
290 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
291 exc = ARMV7M_EXCP_BUS;
292 exc_secure = false;
293 goto pend_fault;
296 *dest = value;
297 return true;
299 pend_fault:
301 * By pending the exception at this point we are making
302 * the IMPDEF choice "overridden exceptions pended" (see the
303 * MergeExcInfo() pseudocode). The other choice would be to not
304 * pend them now and then make a choice about which to throw away
305 * later if we have two derived exceptions.
307 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
308 return false;
311 void HELPER(v7m_preserve_fp_state)(CPUARMState *env)
314 * Preserve FP state (because LSPACT was set and we are about
315 * to execute an FP instruction). This corresponds to the
316 * PreserveFPState() pseudocode.
317 * We may throw an exception if the stacking fails.
319 ARMCPU *cpu = env_archcpu(env);
320 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
321 bool negpri = !(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_HFRDY_MASK);
322 bool is_priv = !(env->v7m.fpccr[is_secure] & R_V7M_FPCCR_USER_MASK);
323 bool splimviol = env->v7m.fpccr[is_secure] & R_V7M_FPCCR_SPLIMVIOL_MASK;
324 uint32_t fpcar = env->v7m.fpcar[is_secure];
325 bool stacked_ok = true;
326 bool ts = is_secure && (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK);
327 bool take_exception;
329 /* Take the iothread lock as we are going to touch the NVIC */
330 qemu_mutex_lock_iothread();
332 /* Check the background context had access to the FPU */
333 if (!v7m_cpacr_pass(env, is_secure, is_priv)) {
334 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, is_secure);
335 env->v7m.cfsr[is_secure] |= R_V7M_CFSR_NOCP_MASK;
336 stacked_ok = false;
337 } else if (!is_secure && !extract32(env->v7m.nsacr, 10, 1)) {
338 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S);
339 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK;
340 stacked_ok = false;
343 if (!splimviol && stacked_ok) {
344 /* We only stack if the stack limit wasn't violated */
345 int i;
346 ARMMMUIdx mmu_idx;
348 mmu_idx = arm_v7m_mmu_idx_all(env, is_secure, is_priv, negpri);
349 for (i = 0; i < (ts ? 32 : 16); i += 2) {
350 uint64_t dn = *aa32_vfp_dreg(env, i / 2);
351 uint32_t faddr = fpcar + 4 * i;
352 uint32_t slo = extract64(dn, 0, 32);
353 uint32_t shi = extract64(dn, 32, 32);
355 if (i >= 16) {
356 faddr += 8; /* skip the slot for the FPSCR/VPR */
358 stacked_ok = stacked_ok &&
359 v7m_stack_write(cpu, faddr, slo, mmu_idx, STACK_LAZYFP) &&
360 v7m_stack_write(cpu, faddr + 4, shi, mmu_idx, STACK_LAZYFP);
363 stacked_ok = stacked_ok &&
364 v7m_stack_write(cpu, fpcar + 0x40,
365 vfp_get_fpscr(env), mmu_idx, STACK_LAZYFP);
366 if (cpu_isar_feature(aa32_mve, cpu)) {
367 stacked_ok = stacked_ok &&
368 v7m_stack_write(cpu, fpcar + 0x44,
369 env->v7m.vpr, mmu_idx, STACK_LAZYFP);
374 * We definitely pended an exception, but it's possible that it
375 * might not be able to be taken now. If its priority permits us
376 * to take it now, then we must not update the LSPACT or FP regs,
377 * but instead jump out to take the exception immediately.
378 * If it's just pending and won't be taken until the current
379 * handler exits, then we do update LSPACT and the FP regs.
381 take_exception = !stacked_ok &&
382 armv7m_nvic_can_take_pending_exception(env->nvic);
384 qemu_mutex_unlock_iothread();
386 if (take_exception) {
387 raise_exception_ra(env, EXCP_LAZYFP, 0, 1, GETPC());
390 env->v7m.fpccr[is_secure] &= ~R_V7M_FPCCR_LSPACT_MASK;
392 if (ts) {
393 /* Clear s0 to s31 and the FPSCR and VPR */
394 int i;
396 for (i = 0; i < 32; i += 2) {
397 *aa32_vfp_dreg(env, i / 2) = 0;
399 vfp_set_fpscr(env, 0);
400 if (cpu_isar_feature(aa32_mve, cpu)) {
401 env->v7m.vpr = 0;
405 * Otherwise s0 to s15, FPSCR and VPR are UNKNOWN; we choose to leave them
406 * unchanged.
411 * Write to v7M CONTROL.SPSEL bit for the specified security bank.
412 * This may change the current stack pointer between Main and Process
413 * stack pointers if it is done for the CONTROL register for the current
414 * security state.
416 static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
417 bool new_spsel,
418 bool secstate)
420 bool old_is_psp = v7m_using_psp(env);
422 env->v7m.control[secstate] =
423 deposit32(env->v7m.control[secstate],
424 R_V7M_CONTROL_SPSEL_SHIFT,
425 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
427 if (secstate == env->v7m.secure) {
428 bool new_is_psp = v7m_using_psp(env);
429 uint32_t tmp;
431 if (old_is_psp != new_is_psp) {
432 tmp = env->v7m.other_sp;
433 env->v7m.other_sp = env->regs[13];
434 env->regs[13] = tmp;
440 * Write to v7M CONTROL.SPSEL bit. This may change the current
441 * stack pointer between Main and Process stack pointers.
443 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
445 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
448 void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
451 * Write a new value to v7m.exception, thus transitioning into or out
452 * of Handler mode; this may result in a change of active stack pointer.
454 bool new_is_psp, old_is_psp = v7m_using_psp(env);
455 uint32_t tmp;
457 env->v7m.exception = new_exc;
459 new_is_psp = v7m_using_psp(env);
461 if (old_is_psp != new_is_psp) {
462 tmp = env->v7m.other_sp;
463 env->v7m.other_sp = env->regs[13];
464 env->regs[13] = tmp;
468 /* Switch M profile security state between NS and S */
469 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
471 uint32_t new_ss_msp, new_ss_psp;
473 if (env->v7m.secure == new_secstate) {
474 return;
478 * All the banked state is accessed by looking at env->v7m.secure
479 * except for the stack pointer; rearrange the SP appropriately.
481 new_ss_msp = env->v7m.other_ss_msp;
482 new_ss_psp = env->v7m.other_ss_psp;
484 if (v7m_using_psp(env)) {
485 env->v7m.other_ss_psp = env->regs[13];
486 env->v7m.other_ss_msp = env->v7m.other_sp;
487 } else {
488 env->v7m.other_ss_msp = env->regs[13];
489 env->v7m.other_ss_psp = env->v7m.other_sp;
492 env->v7m.secure = new_secstate;
494 if (v7m_using_psp(env)) {
495 env->regs[13] = new_ss_psp;
496 env->v7m.other_sp = new_ss_msp;
497 } else {
498 env->regs[13] = new_ss_msp;
499 env->v7m.other_sp = new_ss_psp;
503 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
506 * Handle v7M BXNS:
507 * - if the return value is a magic value, do exception return (like BX)
508 * - otherwise bit 0 of the return value is the target security state
510 uint32_t min_magic;
512 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
513 /* Covers FNC_RETURN and EXC_RETURN magic */
514 min_magic = FNC_RETURN_MIN_MAGIC;
515 } else {
516 /* EXC_RETURN magic only */
517 min_magic = EXC_RETURN_MIN_MAGIC;
520 if (dest >= min_magic) {
522 * This is an exception return magic value; put it where
523 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
524 * Note that if we ever add gen_ss_advance() singlestep support to
525 * M profile this should count as an "instruction execution complete"
526 * event (compare gen_bx_excret_final_code()).
528 env->regs[15] = dest & ~1;
529 env->thumb = dest & 1;
530 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
531 /* notreached */
534 /* translate.c should have made BXNS UNDEF unless we're secure */
535 assert(env->v7m.secure);
537 if (!(dest & 1)) {
538 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
540 switch_v7m_security_state(env, dest & 1);
541 env->thumb = true;
542 env->regs[15] = dest & ~1;
543 arm_rebuild_hflags(env);
546 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
549 * Handle v7M BLXNS:
550 * - bit 0 of the destination address is the target security state
553 /* At this point regs[15] is the address just after the BLXNS */
554 uint32_t nextinst = env->regs[15] | 1;
555 uint32_t sp = env->regs[13] - 8;
556 uint32_t saved_psr;
558 /* translate.c will have made BLXNS UNDEF unless we're secure */
559 assert(env->v7m.secure);
561 if (dest & 1) {
563 * Target is Secure, so this is just a normal BLX,
564 * except that the low bit doesn't indicate Thumb/not.
566 env->regs[14] = nextinst;
567 env->thumb = true;
568 env->regs[15] = dest & ~1;
569 return;
572 /* Target is non-secure: first push a stack frame */
573 if (!QEMU_IS_ALIGNED(sp, 8)) {
574 qemu_log_mask(LOG_GUEST_ERROR,
575 "BLXNS with misaligned SP is UNPREDICTABLE\n");
578 if (sp < v7m_sp_limit(env)) {
579 raise_exception(env, EXCP_STKOF, 0, 1);
582 saved_psr = env->v7m.exception;
583 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
584 saved_psr |= XPSR_SFPA;
587 /* Note that these stores can throw exceptions on MPU faults */
588 cpu_stl_data_ra(env, sp, nextinst, GETPC());
589 cpu_stl_data_ra(env, sp + 4, saved_psr, GETPC());
591 env->regs[13] = sp;
592 env->regs[14] = 0xfeffffff;
593 if (arm_v7m_is_handler_mode(env)) {
595 * Write a dummy value to IPSR, to avoid leaking the current secure
596 * exception number to non-secure code. This is guaranteed not
597 * to cause write_v7m_exception() to actually change stacks.
599 write_v7m_exception(env, 1);
601 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
602 switch_v7m_security_state(env, 0);
603 env->thumb = true;
604 env->regs[15] = dest;
605 arm_rebuild_hflags(env);
608 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
609 bool spsel)
612 * Return a pointer to the location where we currently store the
613 * stack pointer for the requested security state and thread mode.
614 * This pointer will become invalid if the CPU state is updated
615 * such that the stack pointers are switched around (eg changing
616 * the SPSEL control bit).
617 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
618 * Unlike that pseudocode, we require the caller to pass us in the
619 * SPSEL control bit value; this is because we also use this
620 * function in handling of pushing of the callee-saves registers
621 * part of the v8M stack frame (pseudocode PushCalleeStack()),
622 * and in the tailchain codepath the SPSEL bit comes from the exception
623 * return magic LR value from the previous exception. The pseudocode
624 * opencodes the stack-selection in PushCalleeStack(), but we prefer
625 * to make this utility function generic enough to do the job.
627 bool want_psp = threadmode && spsel;
629 if (secure == env->v7m.secure) {
630 if (want_psp == v7m_using_psp(env)) {
631 return &env->regs[13];
632 } else {
633 return &env->v7m.other_sp;
635 } else {
636 if (want_psp) {
637 return &env->v7m.other_ss_psp;
638 } else {
639 return &env->v7m.other_ss_msp;
644 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
645 uint32_t *pvec)
647 CPUState *cs = CPU(cpu);
648 CPUARMState *env = &cpu->env;
649 MemTxResult result;
650 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
651 uint32_t vector_entry;
652 MemTxAttrs attrs = {};
653 ARMMMUIdx mmu_idx;
654 bool exc_secure;
656 qemu_log_mask(CPU_LOG_INT,
657 "...loading from element %d of %s vector table at 0x%x\n",
658 exc, targets_secure ? "secure" : "non-secure", addr);
660 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
663 * We don't do a get_phys_addr() here because the rules for vector
664 * loads are special: they always use the default memory map, and
665 * the default memory map permits reads from all addresses.
666 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
667 * that we want this special case which would always say "yes",
668 * we just do the SAU lookup here followed by a direct physical load.
670 attrs.secure = targets_secure;
671 attrs.user = false;
673 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
674 V8M_SAttributes sattrs = {};
676 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
677 targets_secure, &sattrs);
678 if (sattrs.ns) {
679 attrs.secure = false;
680 } else if (!targets_secure) {
682 * NS access to S memory: the underlying exception which we escalate
683 * to HardFault is SecureFault, which always targets Secure.
685 exc_secure = true;
686 goto load_fail;
690 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
691 attrs, &result);
692 if (result != MEMTX_OK) {
694 * Underlying exception is BusFault: its target security state
695 * depends on BFHFNMINS.
697 exc_secure = !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
698 goto load_fail;
700 *pvec = vector_entry;
701 qemu_log_mask(CPU_LOG_INT, "...loaded new PC 0x%x\n", *pvec);
702 return true;
704 load_fail:
706 * All vector table fetch fails are reported as HardFault, with
707 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
708 * technically the underlying exception is a SecureFault or BusFault
709 * that is escalated to HardFault.) This is a terminal exception,
710 * so we will either take the HardFault immediately or else enter
711 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
712 * The HardFault is Secure if BFHFNMINS is 0 (meaning that all HFs are
713 * secure); otherwise it targets the same security state as the
714 * underlying exception.
715 * In v8.1M HardFaults from vector table fetch fails don't set FORCED.
717 if (!(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
718 exc_secure = true;
720 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK;
721 if (!arm_feature(env, ARM_FEATURE_V8_1M)) {
722 env->v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
724 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
725 return false;
728 static uint32_t v7m_integrity_sig(CPUARMState *env, uint32_t lr)
731 * Return the integrity signature value for the callee-saves
732 * stack frame section. @lr is the exception return payload/LR value
733 * whose FType bit forms bit 0 of the signature if FP is present.
735 uint32_t sig = 0xfefa125a;
737 if (!cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))
738 || (lr & R_V7M_EXCRET_FTYPE_MASK)) {
739 sig |= 1;
741 return sig;
744 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
745 bool ignore_faults)
748 * For v8M, push the callee-saves register part of the stack frame.
749 * Compare the v8M pseudocode PushCalleeStack().
750 * In the tailchaining case this may not be the current stack.
752 CPUARMState *env = &cpu->env;
753 uint32_t *frame_sp_p;
754 uint32_t frameptr;
755 ARMMMUIdx mmu_idx;
756 bool stacked_ok;
757 uint32_t limit;
758 bool want_psp;
759 uint32_t sig;
760 StackingMode smode = ignore_faults ? STACK_IGNFAULTS : STACK_NORMAL;
762 if (dotailchain) {
763 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
764 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
765 !mode;
767 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
768 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
769 lr & R_V7M_EXCRET_SPSEL_MASK);
770 want_psp = mode && (lr & R_V7M_EXCRET_SPSEL_MASK);
771 if (want_psp) {
772 limit = env->v7m.psplim[M_REG_S];
773 } else {
774 limit = env->v7m.msplim[M_REG_S];
776 } else {
777 mmu_idx = arm_mmu_idx(env);
778 frame_sp_p = &env->regs[13];
779 limit = v7m_sp_limit(env);
782 frameptr = *frame_sp_p - 0x28;
783 if (frameptr < limit) {
785 * Stack limit failure: set SP to the limit value, and generate
786 * STKOF UsageFault. Stack pushes below the limit must not be
787 * performed. It is IMPDEF whether pushes above the limit are
788 * performed; we choose not to.
790 qemu_log_mask(CPU_LOG_INT,
791 "...STKOF during callee-saves register stacking\n");
792 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
793 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
794 env->v7m.secure);
795 *frame_sp_p = limit;
796 return true;
800 * Write as much of the stack frame as we can. A write failure may
801 * cause us to pend a derived exception.
803 sig = v7m_integrity_sig(env, lr);
804 stacked_ok =
805 v7m_stack_write(cpu, frameptr, sig, mmu_idx, smode) &&
806 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx, smode) &&
807 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx, smode) &&
808 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx, smode) &&
809 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx, smode) &&
810 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx, smode) &&
811 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx, smode) &&
812 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx, smode) &&
813 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx, smode);
815 /* Update SP regardless of whether any of the stack accesses failed. */
816 *frame_sp_p = frameptr;
818 return !stacked_ok;
821 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
822 bool ignore_stackfaults)
825 * Do the "take the exception" parts of exception entry,
826 * but not the pushing of state to the stack. This is
827 * similar to the pseudocode ExceptionTaken() function.
829 CPUARMState *env = &cpu->env;
830 uint32_t addr;
831 bool targets_secure;
832 int exc;
833 bool push_failed = false;
835 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
836 qemu_log_mask(CPU_LOG_INT, "...taking pending %s exception %d\n",
837 targets_secure ? "secure" : "nonsecure", exc);
839 if (dotailchain) {
840 /* Sanitize LR FType and PREFIX bits */
841 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
842 lr |= R_V7M_EXCRET_FTYPE_MASK;
844 lr = deposit32(lr, 24, 8, 0xff);
847 if (arm_feature(env, ARM_FEATURE_V8)) {
848 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
849 (lr & R_V7M_EXCRET_S_MASK)) {
851 * The background code (the owner of the registers in the
852 * exception frame) is Secure. This means it may either already
853 * have or now needs to push callee-saves registers.
855 if (targets_secure) {
856 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
858 * We took an exception from Secure to NonSecure
859 * (which means the callee-saved registers got stacked)
860 * and are now tailchaining to a Secure exception.
861 * Clear DCRS so eventual return from this Secure
862 * exception unstacks the callee-saved registers.
864 lr &= ~R_V7M_EXCRET_DCRS_MASK;
866 } else {
868 * We're going to a non-secure exception; push the
869 * callee-saves registers to the stack now, if they're
870 * not already saved.
872 if (lr & R_V7M_EXCRET_DCRS_MASK &&
873 !(dotailchain && !(lr & R_V7M_EXCRET_ES_MASK))) {
874 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
875 ignore_stackfaults);
877 lr |= R_V7M_EXCRET_DCRS_MASK;
881 lr &= ~R_V7M_EXCRET_ES_MASK;
882 if (targets_secure) {
883 lr |= R_V7M_EXCRET_ES_MASK;
885 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
886 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
887 lr |= R_V7M_EXCRET_SPSEL_MASK;
891 * Clear registers if necessary to prevent non-secure exception
892 * code being able to see register values from secure code.
893 * Where register values become architecturally UNKNOWN we leave
894 * them with their previous values. v8.1M is tighter than v8.0M
895 * here and always zeroes the caller-saved registers regardless
896 * of the security state the exception is targeting.
898 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
899 if (!targets_secure || arm_feature(env, ARM_FEATURE_V8_1M)) {
901 * Always clear the caller-saved registers (they have been
902 * pushed to the stack earlier in v7m_push_stack()).
903 * Clear callee-saved registers if the background code is
904 * Secure (in which case these regs were saved in
905 * v7m_push_callee_stack()).
907 int i;
909 * r4..r11 are callee-saves, zero only if background
910 * state was Secure (EXCRET.S == 1) and exception
911 * targets Non-secure state
913 bool zero_callee_saves = !targets_secure &&
914 (lr & R_V7M_EXCRET_S_MASK);
916 for (i = 0; i < 13; i++) {
917 if (i < 4 || i > 11 || zero_callee_saves) {
918 env->regs[i] = 0;
921 /* Clear EAPSR */
922 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
927 if (push_failed && !ignore_stackfaults) {
929 * Derived exception on callee-saves register stacking:
930 * we might now want to take a different exception which
931 * targets a different security state, so try again from the top.
933 qemu_log_mask(CPU_LOG_INT,
934 "...derived exception on callee-saves register stacking");
935 v7m_exception_taken(cpu, lr, true, true);
936 return;
939 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
940 /* Vector load failed: derived exception */
941 qemu_log_mask(CPU_LOG_INT, "...derived exception on vector table load");
942 v7m_exception_taken(cpu, lr, true, true);
943 return;
947 * Now we've done everything that might cause a derived exception
948 * we can go ahead and activate whichever exception we're going to
949 * take (which might now be the derived exception).
951 armv7m_nvic_acknowledge_irq(env->nvic);
953 /* Switch to target security state -- must do this before writing SPSEL */
954 switch_v7m_security_state(env, targets_secure);
955 write_v7m_control_spsel(env, 0);
956 arm_clear_exclusive(env);
957 /* Clear SFPA and FPCA (has no effect if no FPU) */
958 env->v7m.control[M_REG_S] &=
959 ~(R_V7M_CONTROL_FPCA_MASK | R_V7M_CONTROL_SFPA_MASK);
960 /* Clear IT bits */
961 env->condexec_bits = 0;
962 env->regs[14] = lr;
963 env->regs[15] = addr & 0xfffffffe;
964 env->thumb = addr & 1;
965 arm_rebuild_hflags(env);
968 static void v7m_update_fpccr(CPUARMState *env, uint32_t frameptr,
969 bool apply_splim)
972 * Like the pseudocode UpdateFPCCR: save state in FPCAR and FPCCR
973 * that we will need later in order to do lazy FP reg stacking.
975 bool is_secure = env->v7m.secure;
976 void *nvic = env->nvic;
978 * Some bits are unbanked and live always in fpccr[M_REG_S]; some bits
979 * are banked and we want to update the bit in the bank for the
980 * current security state; and in one case we want to specifically
981 * update the NS banked version of a bit even if we are secure.
983 uint32_t *fpccr_s = &env->v7m.fpccr[M_REG_S];
984 uint32_t *fpccr_ns = &env->v7m.fpccr[M_REG_NS];
985 uint32_t *fpccr = &env->v7m.fpccr[is_secure];
986 bool hfrdy, bfrdy, mmrdy, ns_ufrdy, s_ufrdy, sfrdy, monrdy;
988 env->v7m.fpcar[is_secure] = frameptr & ~0x7;
990 if (apply_splim && arm_feature(env, ARM_FEATURE_V8)) {
991 bool splimviol;
992 uint32_t splim = v7m_sp_limit(env);
993 bool ign = armv7m_nvic_neg_prio_requested(nvic, is_secure) &&
994 (env->v7m.ccr[is_secure] & R_V7M_CCR_STKOFHFNMIGN_MASK);
996 splimviol = !ign && frameptr < splim;
997 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, SPLIMVIOL, splimviol);
1000 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, LSPACT, 1);
1002 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, S, is_secure);
1004 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, USER, arm_current_el(env) == 0);
1006 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, THREAD,
1007 !arm_v7m_is_handler_mode(env));
1009 hfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_HARD, false);
1010 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, HFRDY, hfrdy);
1012 bfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_BUS, false);
1013 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, BFRDY, bfrdy);
1015 mmrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_MEM, is_secure);
1016 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, MMRDY, mmrdy);
1018 ns_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, false);
1019 *fpccr_ns = FIELD_DP32(*fpccr_ns, V7M_FPCCR, UFRDY, ns_ufrdy);
1021 monrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_DEBUG, false);
1022 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, MONRDY, monrdy);
1024 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1025 s_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, true);
1026 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, UFRDY, s_ufrdy);
1028 sfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_SECURE, false);
1029 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, SFRDY, sfrdy);
1033 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr)
1035 /* fptr is the value of Rn, the frame pointer we store the FP regs to */
1036 ARMCPU *cpu = env_archcpu(env);
1037 bool s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
1038 bool lspact = env->v7m.fpccr[s] & R_V7M_FPCCR_LSPACT_MASK;
1039 uintptr_t ra = GETPC();
1041 assert(env->v7m.secure);
1043 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) {
1044 return;
1047 /* Check access to the coprocessor is permitted */
1048 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) {
1049 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC());
1052 if (lspact) {
1053 /* LSPACT should not be active when there is active FP state */
1054 raise_exception_ra(env, EXCP_LSERR, 0, 1, GETPC());
1057 if (fptr & 7) {
1058 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC());
1062 * Note that we do not use v7m_stack_write() here, because the
1063 * accesses should not set the FSR bits for stacking errors if they
1064 * fail. (In pseudocode terms, they are AccType_NORMAL, not AccType_STACK
1065 * or AccType_LAZYFP). Faults in cpu_stl_data_ra() will throw exceptions
1066 * and longjmp out.
1068 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) {
1069 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK;
1070 int i;
1072 for (i = 0; i < (ts ? 32 : 16); i += 2) {
1073 uint64_t dn = *aa32_vfp_dreg(env, i / 2);
1074 uint32_t faddr = fptr + 4 * i;
1075 uint32_t slo = extract64(dn, 0, 32);
1076 uint32_t shi = extract64(dn, 32, 32);
1078 if (i >= 16) {
1079 faddr += 8; /* skip the slot for the FPSCR */
1081 cpu_stl_data_ra(env, faddr, slo, ra);
1082 cpu_stl_data_ra(env, faddr + 4, shi, ra);
1084 cpu_stl_data_ra(env, fptr + 0x40, vfp_get_fpscr(env), ra);
1085 if (cpu_isar_feature(aa32_mve, cpu)) {
1086 cpu_stl_data_ra(env, fptr + 0x44, env->v7m.vpr, ra);
1090 * If TS is 0 then s0 to s15, FPSCR and VPR are UNKNOWN; we choose to
1091 * leave them unchanged, matching our choice in v7m_preserve_fp_state.
1093 if (ts) {
1094 for (i = 0; i < 32; i += 2) {
1095 *aa32_vfp_dreg(env, i / 2) = 0;
1097 vfp_set_fpscr(env, 0);
1098 if (cpu_isar_feature(aa32_mve, cpu)) {
1099 env->v7m.vpr = 0;
1102 } else {
1103 v7m_update_fpccr(env, fptr, false);
1106 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK;
1109 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr)
1111 ARMCPU *cpu = env_archcpu(env);
1112 uintptr_t ra = GETPC();
1114 /* fptr is the value of Rn, the frame pointer we load the FP regs from */
1115 assert(env->v7m.secure);
1117 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) {
1118 return;
1121 /* Check access to the coprocessor is permitted */
1122 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) {
1123 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC());
1126 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) {
1127 /* State in FP is still valid */
1128 env->v7m.fpccr[M_REG_S] &= ~R_V7M_FPCCR_LSPACT_MASK;
1129 } else {
1130 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK;
1131 int i;
1132 uint32_t fpscr;
1134 if (fptr & 7) {
1135 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC());
1138 for (i = 0; i < (ts ? 32 : 16); i += 2) {
1139 uint32_t slo, shi;
1140 uint64_t dn;
1141 uint32_t faddr = fptr + 4 * i;
1143 if (i >= 16) {
1144 faddr += 8; /* skip the slot for the FPSCR and VPR */
1147 slo = cpu_ldl_data_ra(env, faddr, ra);
1148 shi = cpu_ldl_data_ra(env, faddr + 4, ra);
1150 dn = (uint64_t) shi << 32 | slo;
1151 *aa32_vfp_dreg(env, i / 2) = dn;
1153 fpscr = cpu_ldl_data_ra(env, fptr + 0x40, ra);
1154 vfp_set_fpscr(env, fpscr);
1155 if (cpu_isar_feature(aa32_mve, cpu)) {
1156 env->v7m.vpr = cpu_ldl_data_ra(env, fptr + 0x44, ra);
1160 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
1163 static bool v7m_push_stack(ARMCPU *cpu)
1166 * Do the "set up stack frame" part of exception entry,
1167 * similar to pseudocode PushStack().
1168 * Return true if we generate a derived exception (and so
1169 * should ignore further stack faults trying to process
1170 * that derived exception.)
1172 bool stacked_ok = true, limitviol = false;
1173 CPUARMState *env = &cpu->env;
1174 uint32_t xpsr = xpsr_read(env);
1175 uint32_t frameptr = env->regs[13];
1176 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
1177 uint32_t framesize;
1178 bool nsacr_cp10 = extract32(env->v7m.nsacr, 10, 1);
1180 if ((env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) &&
1181 (env->v7m.secure || nsacr_cp10)) {
1182 if (env->v7m.secure &&
1183 env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK) {
1184 framesize = 0xa8;
1185 } else {
1186 framesize = 0x68;
1188 } else {
1189 framesize = 0x20;
1192 /* Align stack pointer if the guest wants that */
1193 if ((frameptr & 4) &&
1194 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
1195 frameptr -= 4;
1196 xpsr |= XPSR_SPREALIGN;
1199 xpsr &= ~XPSR_SFPA;
1200 if (env->v7m.secure &&
1201 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) {
1202 xpsr |= XPSR_SFPA;
1205 frameptr -= framesize;
1207 if (arm_feature(env, ARM_FEATURE_V8)) {
1208 uint32_t limit = v7m_sp_limit(env);
1210 if (frameptr < limit) {
1212 * Stack limit failure: set SP to the limit value, and generate
1213 * STKOF UsageFault. Stack pushes below the limit must not be
1214 * performed. It is IMPDEF whether pushes above the limit are
1215 * performed; we choose not to.
1217 qemu_log_mask(CPU_LOG_INT,
1218 "...STKOF during stacking\n");
1219 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
1220 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
1221 env->v7m.secure);
1222 env->regs[13] = limit;
1224 * We won't try to perform any further memory accesses but
1225 * we must continue through the following code to check for
1226 * permission faults during FPU state preservation, and we
1227 * must update FPCCR if lazy stacking is enabled.
1229 limitviol = true;
1230 stacked_ok = false;
1235 * Write as much of the stack frame as we can. If we fail a stack
1236 * write this will result in a derived exception being pended
1237 * (which may be taken in preference to the one we started with
1238 * if it has higher priority).
1240 stacked_ok = stacked_ok &&
1241 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, STACK_NORMAL) &&
1242 v7m_stack_write(cpu, frameptr + 4, env->regs[1],
1243 mmu_idx, STACK_NORMAL) &&
1244 v7m_stack_write(cpu, frameptr + 8, env->regs[2],
1245 mmu_idx, STACK_NORMAL) &&
1246 v7m_stack_write(cpu, frameptr + 12, env->regs[3],
1247 mmu_idx, STACK_NORMAL) &&
1248 v7m_stack_write(cpu, frameptr + 16, env->regs[12],
1249 mmu_idx, STACK_NORMAL) &&
1250 v7m_stack_write(cpu, frameptr + 20, env->regs[14],
1251 mmu_idx, STACK_NORMAL) &&
1252 v7m_stack_write(cpu, frameptr + 24, env->regs[15],
1253 mmu_idx, STACK_NORMAL) &&
1254 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, STACK_NORMAL);
1256 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) {
1257 /* FPU is active, try to save its registers */
1258 bool fpccr_s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
1259 bool lspact = env->v7m.fpccr[fpccr_s] & R_V7M_FPCCR_LSPACT_MASK;
1261 if (lspact && arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1262 qemu_log_mask(CPU_LOG_INT,
1263 "...SecureFault because LSPACT and FPCA both set\n");
1264 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
1265 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
1266 } else if (!env->v7m.secure && !nsacr_cp10) {
1267 qemu_log_mask(CPU_LOG_INT,
1268 "...Secure UsageFault with CFSR.NOCP because "
1269 "NSACR.CP10 prevents stacking FP regs\n");
1270 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S);
1271 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK;
1272 } else {
1273 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) {
1274 /* Lazy stacking disabled, save registers now */
1275 int i;
1276 bool cpacr_pass = v7m_cpacr_pass(env, env->v7m.secure,
1277 arm_current_el(env) != 0);
1279 if (stacked_ok && !cpacr_pass) {
1281 * Take UsageFault if CPACR forbids access. The pseudocode
1282 * here does a full CheckCPEnabled() but we know the NSACR
1283 * check can never fail as we have already handled that.
1285 qemu_log_mask(CPU_LOG_INT,
1286 "...UsageFault with CFSR.NOCP because "
1287 "CPACR.CP10 prevents stacking FP regs\n");
1288 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
1289 env->v7m.secure);
1290 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
1291 stacked_ok = false;
1294 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) {
1295 uint64_t dn = *aa32_vfp_dreg(env, i / 2);
1296 uint32_t faddr = frameptr + 0x20 + 4 * i;
1297 uint32_t slo = extract64(dn, 0, 32);
1298 uint32_t shi = extract64(dn, 32, 32);
1300 if (i >= 16) {
1301 faddr += 8; /* skip the slot for the FPSCR and VPR */
1303 stacked_ok = stacked_ok &&
1304 v7m_stack_write(cpu, faddr, slo,
1305 mmu_idx, STACK_NORMAL) &&
1306 v7m_stack_write(cpu, faddr + 4, shi,
1307 mmu_idx, STACK_NORMAL);
1309 stacked_ok = stacked_ok &&
1310 v7m_stack_write(cpu, frameptr + 0x60,
1311 vfp_get_fpscr(env), mmu_idx, STACK_NORMAL);
1312 if (cpu_isar_feature(aa32_mve, cpu)) {
1313 stacked_ok = stacked_ok &&
1314 v7m_stack_write(cpu, frameptr + 0x64,
1315 env->v7m.vpr, mmu_idx, STACK_NORMAL);
1317 if (cpacr_pass) {
1318 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) {
1319 *aa32_vfp_dreg(env, i / 2) = 0;
1321 vfp_set_fpscr(env, 0);
1322 if (cpu_isar_feature(aa32_mve, cpu)) {
1323 env->v7m.vpr = 0;
1326 } else {
1327 /* Lazy stacking enabled, save necessary info to stack later */
1328 v7m_update_fpccr(env, frameptr + 0x20, true);
1334 * If we broke a stack limit then SP was already updated earlier;
1335 * otherwise we update SP regardless of whether any of the stack
1336 * accesses failed or we took some other kind of fault.
1338 if (!limitviol) {
1339 env->regs[13] = frameptr;
1342 return !stacked_ok;
1345 static void do_v7m_exception_exit(ARMCPU *cpu)
1347 CPUARMState *env = &cpu->env;
1348 uint32_t excret;
1349 uint32_t xpsr, xpsr_mask;
1350 bool ufault = false;
1351 bool sfault = false;
1352 bool return_to_sp_process;
1353 bool return_to_handler;
1354 bool rettobase = false;
1355 bool exc_secure = false;
1356 bool return_to_secure;
1357 bool ftype;
1358 bool restore_s16_s31 = false;
1361 * If we're not in Handler mode then jumps to magic exception-exit
1362 * addresses don't have magic behaviour. However for the v8M
1363 * security extensions the magic secure-function-return has to
1364 * work in thread mode too, so to avoid doing an extra check in
1365 * the generated code we allow exception-exit magic to also cause the
1366 * internal exception and bring us here in thread mode. Correct code
1367 * will never try to do this (the following insn fetch will always
1368 * fault) so we the overhead of having taken an unnecessary exception
1369 * doesn't matter.
1371 if (!arm_v7m_is_handler_mode(env)) {
1372 return;
1376 * In the spec pseudocode ExceptionReturn() is called directly
1377 * from BXWritePC() and gets the full target PC value including
1378 * bit zero. In QEMU's implementation we treat it as a normal
1379 * jump-to-register (which is then caught later on), and so split
1380 * the target value up between env->regs[15] and env->thumb in
1381 * gen_bx(). Reconstitute it.
1383 excret = env->regs[15];
1384 if (env->thumb) {
1385 excret |= 1;
1388 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
1389 " previous exception %d\n",
1390 excret, env->v7m.exception);
1392 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
1393 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
1394 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
1395 excret);
1398 ftype = excret & R_V7M_EXCRET_FTYPE_MASK;
1400 if (!ftype && !cpu_isar_feature(aa32_vfp_simd, cpu)) {
1401 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero FTYPE in exception "
1402 "exit PC value 0x%" PRIx32 " is UNPREDICTABLE "
1403 "if FPU not present\n",
1404 excret);
1405 ftype = true;
1408 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1410 * EXC_RETURN.ES validation check (R_SMFL). We must do this before
1411 * we pick which FAULTMASK to clear.
1413 if (!env->v7m.secure &&
1414 ((excret & R_V7M_EXCRET_ES_MASK) ||
1415 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
1416 sfault = 1;
1417 /* For all other purposes, treat ES as 0 (R_HXSR) */
1418 excret &= ~R_V7M_EXCRET_ES_MASK;
1420 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
1423 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
1425 * Auto-clear FAULTMASK on return from other than NMI.
1426 * If the security extension is implemented then this only
1427 * happens if the raw execution priority is >= 0; the
1428 * value of the ES bit in the exception return value indicates
1429 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
1431 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1432 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
1433 env->v7m.faultmask[exc_secure] = 0;
1435 } else {
1436 env->v7m.faultmask[M_REG_NS] = 0;
1440 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
1441 exc_secure)) {
1442 case -1:
1443 /* attempt to exit an exception that isn't active */
1444 ufault = true;
1445 break;
1446 case 0:
1447 /* still an irq active now */
1448 break;
1449 case 1:
1451 * We returned to base exception level, no nesting.
1452 * (In the pseudocode this is written using "NestedActivation != 1"
1453 * where we have 'rettobase == false'.)
1455 rettobase = true;
1456 break;
1457 default:
1458 g_assert_not_reached();
1461 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
1462 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
1463 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
1464 (excret & R_V7M_EXCRET_S_MASK);
1466 if (arm_feature(env, ARM_FEATURE_V8)) {
1467 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1469 * UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
1470 * we choose to take the UsageFault.
1472 if ((excret & R_V7M_EXCRET_S_MASK) ||
1473 (excret & R_V7M_EXCRET_ES_MASK) ||
1474 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
1475 ufault = true;
1478 if (excret & R_V7M_EXCRET_RES0_MASK) {
1479 ufault = true;
1481 } else {
1482 /* For v7M we only recognize certain combinations of the low bits */
1483 switch (excret & 0xf) {
1484 case 1: /* Return to Handler */
1485 break;
1486 case 13: /* Return to Thread using Process stack */
1487 case 9: /* Return to Thread using Main stack */
1489 * We only need to check NONBASETHRDENA for v7M, because in
1490 * v8M this bit does not exist (it is RES1).
1492 if (!rettobase &&
1493 !(env->v7m.ccr[env->v7m.secure] &
1494 R_V7M_CCR_NONBASETHRDENA_MASK)) {
1495 ufault = true;
1497 break;
1498 default:
1499 ufault = true;
1504 * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
1505 * Handler mode (and will be until we write the new XPSR.Interrupt
1506 * field) this does not switch around the current stack pointer.
1507 * We must do this before we do any kind of tailchaining, including
1508 * for the derived exceptions on integrity check failures, or we will
1509 * give the guest an incorrect EXCRET.SPSEL value on exception entry.
1511 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
1514 * Clear scratch FP values left in caller saved registers; this
1515 * must happen before any kind of tail chaining.
1517 if ((env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_CLRONRET_MASK) &&
1518 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) {
1519 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) {
1520 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
1521 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
1522 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
1523 "stackframe: error during lazy state deactivation\n");
1524 v7m_exception_taken(cpu, excret, true, false);
1525 return;
1526 } else {
1527 if (arm_feature(env, ARM_FEATURE_V8_1M)) {
1528 /* v8.1M adds this NOCP check */
1529 bool nsacr_pass = exc_secure ||
1530 extract32(env->v7m.nsacr, 10, 1);
1531 bool cpacr_pass = v7m_cpacr_pass(env, exc_secure, true);
1532 if (!nsacr_pass) {
1533 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, true);
1534 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK;
1535 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
1536 "stackframe: NSACR prevents clearing FPU registers\n");
1537 v7m_exception_taken(cpu, excret, true, false);
1538 return;
1539 } else if (!cpacr_pass) {
1540 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
1541 exc_secure);
1542 env->v7m.cfsr[exc_secure] |= R_V7M_CFSR_NOCP_MASK;
1543 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
1544 "stackframe: CPACR prevents clearing FPU registers\n");
1545 v7m_exception_taken(cpu, excret, true, false);
1546 return;
1549 /* Clear s0..s15, FPSCR and VPR */
1550 int i;
1552 for (i = 0; i < 16; i += 2) {
1553 *aa32_vfp_dreg(env, i / 2) = 0;
1555 vfp_set_fpscr(env, 0);
1556 if (cpu_isar_feature(aa32_mve, cpu)) {
1557 env->v7m.vpr = 0;
1562 if (sfault) {
1563 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
1564 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
1565 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
1566 "stackframe: failed EXC_RETURN.ES validity check\n");
1567 v7m_exception_taken(cpu, excret, true, false);
1568 return;
1571 if (ufault) {
1573 * Bad exception return: instead of popping the exception
1574 * stack, directly take a usage fault on the current stack.
1576 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
1577 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
1578 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
1579 "stackframe: failed exception return integrity check\n");
1580 v7m_exception_taken(cpu, excret, true, false);
1581 return;
1585 * Tailchaining: if there is currently a pending exception that
1586 * is high enough priority to preempt execution at the level we're
1587 * about to return to, then just directly take that exception now,
1588 * avoiding an unstack-and-then-stack. Note that now we have
1589 * deactivated the previous exception by calling armv7m_nvic_complete_irq()
1590 * our current execution priority is already the execution priority we are
1591 * returning to -- none of the state we would unstack or set based on
1592 * the EXCRET value affects it.
1594 if (armv7m_nvic_can_take_pending_exception(env->nvic)) {
1595 qemu_log_mask(CPU_LOG_INT, "...tailchaining to pending exception\n");
1596 v7m_exception_taken(cpu, excret, true, false);
1597 return;
1600 switch_v7m_security_state(env, return_to_secure);
1604 * The stack pointer we should be reading the exception frame from
1605 * depends on bits in the magic exception return type value (and
1606 * for v8M isn't necessarily the stack pointer we will eventually
1607 * end up resuming execution with). Get a pointer to the location
1608 * in the CPU state struct where the SP we need is currently being
1609 * stored; we will use and modify it in place.
1610 * We use this limited C variable scope so we don't accidentally
1611 * use 'frame_sp_p' after we do something that makes it invalid.
1613 bool spsel = env->v7m.control[return_to_secure] & R_V7M_CONTROL_SPSEL_MASK;
1614 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
1615 return_to_secure,
1616 !return_to_handler,
1617 spsel);
1618 uint32_t frameptr = *frame_sp_p;
1619 bool pop_ok = true;
1620 ARMMMUIdx mmu_idx;
1621 bool return_to_priv = return_to_handler ||
1622 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK);
1624 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
1625 return_to_priv);
1627 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
1628 arm_feature(env, ARM_FEATURE_V8)) {
1629 qemu_log_mask(LOG_GUEST_ERROR,
1630 "M profile exception return with non-8-aligned SP "
1631 "for destination state is UNPREDICTABLE\n");
1634 /* Do we need to pop callee-saved registers? */
1635 if (return_to_secure &&
1636 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
1637 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
1638 uint32_t actual_sig;
1640 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx);
1642 if (pop_ok && v7m_integrity_sig(env, excret) != actual_sig) {
1643 /* Take a SecureFault on the current stack */
1644 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
1645 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
1646 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
1647 "stackframe: failed exception return integrity "
1648 "signature check\n");
1649 v7m_exception_taken(cpu, excret, true, false);
1650 return;
1653 pop_ok = pop_ok &&
1654 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
1655 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
1656 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
1657 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
1658 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
1659 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
1660 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
1661 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
1663 frameptr += 0x28;
1666 /* Pop registers */
1667 pop_ok = pop_ok &&
1668 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
1669 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
1670 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
1671 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
1672 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
1673 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
1674 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
1675 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
1677 if (!pop_ok) {
1679 * v7m_stack_read() pended a fault, so take it (as a tail
1680 * chained exception on the same stack frame)
1682 qemu_log_mask(CPU_LOG_INT, "...derived exception on unstacking\n");
1683 v7m_exception_taken(cpu, excret, true, false);
1684 return;
1688 * Returning from an exception with a PC with bit 0 set is defined
1689 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
1690 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
1691 * the lsbit, and there are several RTOSes out there which incorrectly
1692 * assume the r15 in the stack frame should be a Thumb-style "lsbit
1693 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
1694 * complain about the badly behaved guest.
1696 if (env->regs[15] & 1) {
1697 env->regs[15] &= ~1U;
1698 if (!arm_feature(env, ARM_FEATURE_V8)) {
1699 qemu_log_mask(LOG_GUEST_ERROR,
1700 "M profile return from interrupt with misaligned "
1701 "PC is UNPREDICTABLE on v7M\n");
1705 if (arm_feature(env, ARM_FEATURE_V8)) {
1707 * For v8M we have to check whether the xPSR exception field
1708 * matches the EXCRET value for return to handler/thread
1709 * before we commit to changing the SP and xPSR.
1711 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
1712 if (return_to_handler != will_be_handler) {
1714 * Take an INVPC UsageFault on the current stack.
1715 * By this point we will have switched to the security state
1716 * for the background state, so this UsageFault will target
1717 * that state.
1719 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
1720 env->v7m.secure);
1721 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
1722 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
1723 "stackframe: failed exception return integrity "
1724 "check\n");
1725 v7m_exception_taken(cpu, excret, true, false);
1726 return;
1730 if (!ftype) {
1731 /* FP present and we need to handle it */
1732 if (!return_to_secure &&
1733 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK)) {
1734 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
1735 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
1736 qemu_log_mask(CPU_LOG_INT,
1737 "...taking SecureFault on existing stackframe: "
1738 "Secure LSPACT set but exception return is "
1739 "not to secure state\n");
1740 v7m_exception_taken(cpu, excret, true, false);
1741 return;
1744 restore_s16_s31 = return_to_secure &&
1745 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK);
1747 if (env->v7m.fpccr[return_to_secure] & R_V7M_FPCCR_LSPACT_MASK) {
1748 /* State in FPU is still valid, just clear LSPACT */
1749 env->v7m.fpccr[return_to_secure] &= ~R_V7M_FPCCR_LSPACT_MASK;
1750 } else {
1751 int i;
1752 uint32_t fpscr;
1753 bool cpacr_pass, nsacr_pass;
1755 cpacr_pass = v7m_cpacr_pass(env, return_to_secure,
1756 return_to_priv);
1757 nsacr_pass = return_to_secure ||
1758 extract32(env->v7m.nsacr, 10, 1);
1760 if (!cpacr_pass) {
1761 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
1762 return_to_secure);
1763 env->v7m.cfsr[return_to_secure] |= R_V7M_CFSR_NOCP_MASK;
1764 qemu_log_mask(CPU_LOG_INT,
1765 "...taking UsageFault on existing "
1766 "stackframe: CPACR.CP10 prevents unstacking "
1767 "FP regs\n");
1768 v7m_exception_taken(cpu, excret, true, false);
1769 return;
1770 } else if (!nsacr_pass) {
1771 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, true);
1772 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_INVPC_MASK;
1773 qemu_log_mask(CPU_LOG_INT,
1774 "...taking Secure UsageFault on existing "
1775 "stackframe: NSACR.CP10 prevents unstacking "
1776 "FP regs\n");
1777 v7m_exception_taken(cpu, excret, true, false);
1778 return;
1781 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) {
1782 uint32_t slo, shi;
1783 uint64_t dn;
1784 uint32_t faddr = frameptr + 0x20 + 4 * i;
1786 if (i >= 16) {
1787 faddr += 8; /* Skip the slot for the FPSCR and VPR */
1790 pop_ok = pop_ok &&
1791 v7m_stack_read(cpu, &slo, faddr, mmu_idx) &&
1792 v7m_stack_read(cpu, &shi, faddr + 4, mmu_idx);
1794 if (!pop_ok) {
1795 break;
1798 dn = (uint64_t)shi << 32 | slo;
1799 *aa32_vfp_dreg(env, i / 2) = dn;
1801 pop_ok = pop_ok &&
1802 v7m_stack_read(cpu, &fpscr, frameptr + 0x60, mmu_idx);
1803 if (pop_ok) {
1804 vfp_set_fpscr(env, fpscr);
1806 if (cpu_isar_feature(aa32_mve, cpu)) {
1807 pop_ok = pop_ok &&
1808 v7m_stack_read(cpu, &env->v7m.vpr,
1809 frameptr + 0x64, mmu_idx);
1811 if (!pop_ok) {
1813 * These regs are 0 if security extension present;
1814 * otherwise merely UNKNOWN. We zero always.
1816 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) {
1817 *aa32_vfp_dreg(env, i / 2) = 0;
1819 vfp_set_fpscr(env, 0);
1820 if (cpu_isar_feature(aa32_mve, cpu)) {
1821 env->v7m.vpr = 0;
1826 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S],
1827 V7M_CONTROL, FPCA, !ftype);
1829 /* Commit to consuming the stack frame */
1830 frameptr += 0x20;
1831 if (!ftype) {
1832 frameptr += 0x48;
1833 if (restore_s16_s31) {
1834 frameptr += 0x40;
1838 * Undo stack alignment (the SPREALIGN bit indicates that the original
1839 * pre-exception SP was not 8-aligned and we added a padding word to
1840 * align it, so we undo this by ORing in the bit that increases it
1841 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
1842 * would work too but a logical OR is how the pseudocode specifies it.)
1844 if (xpsr & XPSR_SPREALIGN) {
1845 frameptr |= 4;
1847 *frame_sp_p = frameptr;
1850 xpsr_mask = ~(XPSR_SPREALIGN | XPSR_SFPA);
1851 if (!arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
1852 xpsr_mask &= ~XPSR_GE;
1854 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
1855 xpsr_write(env, xpsr, xpsr_mask);
1857 if (env->v7m.secure) {
1858 bool sfpa = xpsr & XPSR_SFPA;
1860 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S],
1861 V7M_CONTROL, SFPA, sfpa);
1865 * The restored xPSR exception field will be zero if we're
1866 * resuming in Thread mode. If that doesn't match what the
1867 * exception return excret specified then this is a UsageFault.
1868 * v7M requires we make this check here; v8M did it earlier.
1870 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
1872 * Take an INVPC UsageFault by pushing the stack again;
1873 * we know we're v7M so this is never a Secure UsageFault.
1875 bool ignore_stackfaults;
1877 assert(!arm_feature(env, ARM_FEATURE_V8));
1878 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
1879 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
1880 ignore_stackfaults = v7m_push_stack(cpu);
1881 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
1882 "failed exception return integrity check\n");
1883 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
1884 return;
1887 /* Otherwise, we have a successful exception exit. */
1888 arm_clear_exclusive(env);
1889 arm_rebuild_hflags(env);
1890 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
1893 static bool do_v7m_function_return(ARMCPU *cpu)
1896 * v8M security extensions magic function return.
1897 * We may either:
1898 * (1) throw an exception (longjump)
1899 * (2) return true if we successfully handled the function return
1900 * (3) return false if we failed a consistency check and have
1901 * pended a UsageFault that needs to be taken now
1903 * At this point the magic return value is split between env->regs[15]
1904 * and env->thumb. We don't bother to reconstitute it because we don't
1905 * need it (all values are handled the same way).
1907 CPUARMState *env = &cpu->env;
1908 uint32_t newpc, newpsr, newpsr_exc;
1910 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
1913 bool threadmode, spsel;
1914 MemOpIdx oi;
1915 ARMMMUIdx mmu_idx;
1916 uint32_t *frame_sp_p;
1917 uint32_t frameptr;
1919 /* Pull the return address and IPSR from the Secure stack */
1920 threadmode = !arm_v7m_is_handler_mode(env);
1921 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
1923 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
1924 frameptr = *frame_sp_p;
1927 * These loads may throw an exception (for MPU faults). We want to
1928 * do them as secure, so work out what MMU index that is.
1930 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
1931 oi = make_memop_idx(MO_LEUL, arm_to_core_mmu_idx(mmu_idx));
1932 newpc = cpu_ldl_le_mmu(env, frameptr, oi, 0);
1933 newpsr = cpu_ldl_le_mmu(env, frameptr + 4, oi, 0);
1935 /* Consistency checks on new IPSR */
1936 newpsr_exc = newpsr & XPSR_EXCP;
1937 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
1938 (env->v7m.exception == 1 && newpsr_exc != 0))) {
1939 /* Pend the fault and tell our caller to take it */
1940 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
1941 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
1942 env->v7m.secure);
1943 qemu_log_mask(CPU_LOG_INT,
1944 "...taking INVPC UsageFault: "
1945 "IPSR consistency check failed\n");
1946 return false;
1949 *frame_sp_p = frameptr + 8;
1952 /* This invalidates frame_sp_p */
1953 switch_v7m_security_state(env, true);
1954 env->v7m.exception = newpsr_exc;
1955 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
1956 if (newpsr & XPSR_SFPA) {
1957 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
1959 xpsr_write(env, 0, XPSR_IT);
1960 env->thumb = newpc & 1;
1961 env->regs[15] = newpc & ~1;
1962 arm_rebuild_hflags(env);
1964 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
1965 return true;
1968 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx, bool secure,
1969 uint32_t addr, uint16_t *insn)
1972 * Load a 16-bit portion of a v7M instruction, returning true on success,
1973 * or false on failure (in which case we will have pended the appropriate
1974 * exception).
1975 * We need to do the instruction fetch's MPU and SAU checks
1976 * like this because there is no MMU index that would allow
1977 * doing the load with a single function call. Instead we must
1978 * first check that the security attributes permit the load
1979 * and that they don't mismatch on the two halves of the instruction,
1980 * and then we do the load as a secure load (ie using the security
1981 * attributes of the address, not the CPU, as architecturally required).
1983 CPUState *cs = CPU(cpu);
1984 CPUARMState *env = &cpu->env;
1985 V8M_SAttributes sattrs = {};
1986 GetPhysAddrResult res = {};
1987 ARMMMUFaultInfo fi = {};
1988 MemTxResult txres;
1990 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, secure, &sattrs);
1991 if (!sattrs.nsc || sattrs.ns) {
1993 * This must be the second half of the insn, and it straddles a
1994 * region boundary with the second half not being S&NSC.
1996 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
1997 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
1998 qemu_log_mask(CPU_LOG_INT,
1999 "...really SecureFault with SFSR.INVEP\n");
2000 return false;
2002 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx, &res, &fi)) {
2003 /* the MPU lookup failed */
2004 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
2005 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
2006 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
2007 return false;
2009 *insn = address_space_lduw_le(arm_addressspace(cs, res.f.attrs),
2010 res.f.phys_addr, res.f.attrs, &txres);
2011 if (txres != MEMTX_OK) {
2012 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
2013 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
2014 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
2015 return false;
2017 return true;
2020 static bool v7m_read_sg_stack_word(ARMCPU *cpu, ARMMMUIdx mmu_idx,
2021 uint32_t addr, uint32_t *spdata)
2024 * Read a word of data from the stack for the SG instruction,
2025 * writing the value into *spdata. If the load succeeds, return
2026 * true; otherwise pend an appropriate exception and return false.
2027 * (We can't use data load helpers here that throw an exception
2028 * because of the context we're called in, which is halfway through
2029 * arm_v7m_cpu_do_interrupt().)
2031 CPUState *cs = CPU(cpu);
2032 CPUARMState *env = &cpu->env;
2033 MemTxResult txres;
2034 GetPhysAddrResult res = {};
2035 ARMMMUFaultInfo fi = {};
2036 uint32_t value;
2038 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &res, &fi)) {
2039 /* MPU/SAU lookup failed */
2040 if (fi.type == ARMFault_QEMU_SFault) {
2041 qemu_log_mask(CPU_LOG_INT,
2042 "...SecureFault during stack word read\n");
2043 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
2044 env->v7m.sfar = addr;
2045 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
2046 } else {
2047 qemu_log_mask(CPU_LOG_INT,
2048 "...MemManageFault during stack word read\n");
2049 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_DACCVIOL_MASK |
2050 R_V7M_CFSR_MMARVALID_MASK;
2051 env->v7m.mmfar[M_REG_S] = addr;
2052 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, false);
2054 return false;
2056 value = address_space_ldl(arm_addressspace(cs, res.f.attrs),
2057 res.f.phys_addr, res.f.attrs, &txres);
2058 if (txres != MEMTX_OK) {
2059 /* BusFault trying to read the data */
2060 qemu_log_mask(CPU_LOG_INT,
2061 "...BusFault during stack word read\n");
2062 env->v7m.cfsr[M_REG_NS] |=
2063 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
2064 env->v7m.bfar = addr;
2065 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
2066 return false;
2069 *spdata = value;
2070 return true;
2073 static bool v7m_handle_execute_nsc(ARMCPU *cpu)
2076 * Check whether this attempt to execute code in a Secure & NS-Callable
2077 * memory region is for an SG instruction; if so, then emulate the
2078 * effect of the SG instruction and return true. Otherwise pend
2079 * the correct kind of exception and return false.
2081 CPUARMState *env = &cpu->env;
2082 ARMMMUIdx mmu_idx;
2083 uint16_t insn;
2086 * We should never get here unless get_phys_addr_pmsav8() caused
2087 * an exception for NS executing in S&NSC memory.
2089 assert(!env->v7m.secure);
2090 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
2092 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
2093 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
2095 if (!v7m_read_half_insn(cpu, mmu_idx, true, env->regs[15], &insn)) {
2096 return false;
2099 if (!env->thumb) {
2100 goto gen_invep;
2103 if (insn != 0xe97f) {
2105 * Not an SG instruction first half (we choose the IMPDEF
2106 * early-SG-check option).
2108 goto gen_invep;
2111 if (!v7m_read_half_insn(cpu, mmu_idx, true, env->regs[15] + 2, &insn)) {
2112 return false;
2115 if (insn != 0xe97f) {
2117 * Not an SG instruction second half (yes, both halves of the SG
2118 * insn have the same hex value)
2120 goto gen_invep;
2124 * OK, we have confirmed that we really have an SG instruction.
2125 * We know we're NS in S memory so don't need to repeat those checks.
2127 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
2128 ", executing it\n", env->regs[15]);
2130 if (cpu_isar_feature(aa32_m_sec_state, cpu) &&
2131 !arm_v7m_is_handler_mode(env)) {
2133 * v8.1M exception stack frame integrity check. Note that we
2134 * must perform the memory access even if CCR_S.TRD is zero
2135 * and we aren't going to check what the data loaded is.
2137 uint32_t spdata, sp;
2140 * We know we are currently NS, so the S stack pointers must be
2141 * in other_ss_{psp,msp}, not in regs[13]/other_sp.
2143 sp = v7m_using_psp(env) ? env->v7m.other_ss_psp : env->v7m.other_ss_msp;
2144 if (!v7m_read_sg_stack_word(cpu, mmu_idx, sp, &spdata)) {
2145 /* Stack access failed and an exception has been pended */
2146 return false;
2149 if (env->v7m.ccr[M_REG_S] & R_V7M_CCR_TRD_MASK) {
2150 if (((spdata & ~1) == 0xfefa125a) ||
2151 !(env->v7m.control[M_REG_S] & 1)) {
2152 goto gen_invep;
2157 env->regs[14] &= ~1;
2158 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
2159 switch_v7m_security_state(env, true);
2160 xpsr_write(env, 0, XPSR_IT);
2161 env->regs[15] += 4;
2162 arm_rebuild_hflags(env);
2163 return true;
2165 gen_invep:
2166 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
2167 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
2168 qemu_log_mask(CPU_LOG_INT,
2169 "...really SecureFault with SFSR.INVEP\n");
2170 return false;
2173 void arm_v7m_cpu_do_interrupt(CPUState *cs)
2175 ARMCPU *cpu = ARM_CPU(cs);
2176 CPUARMState *env = &cpu->env;
2177 uint32_t lr;
2178 bool ignore_stackfaults;
2180 arm_log_exception(cs);
2183 * For exceptions we just mark as pending on the NVIC, and let that
2184 * handle it.
2186 switch (cs->exception_index) {
2187 case EXCP_UDEF:
2188 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
2189 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
2190 break;
2191 case EXCP_NOCP:
2194 * NOCP might be directed to something other than the current
2195 * security state if this fault is because of NSACR; we indicate
2196 * the target security state using exception.target_el.
2198 int target_secstate;
2200 if (env->exception.target_el == 3) {
2201 target_secstate = M_REG_S;
2202 } else {
2203 target_secstate = env->v7m.secure;
2205 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, target_secstate);
2206 env->v7m.cfsr[target_secstate] |= R_V7M_CFSR_NOCP_MASK;
2207 break;
2209 case EXCP_INVSTATE:
2210 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
2211 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
2212 break;
2213 case EXCP_STKOF:
2214 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
2215 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
2216 break;
2217 case EXCP_LSERR:
2218 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
2219 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
2220 break;
2221 case EXCP_UNALIGNED:
2222 /* Unaligned faults reported by M-profile aware code */
2223 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
2224 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNALIGNED_MASK;
2225 break;
2226 case EXCP_DIVBYZERO:
2227 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
2228 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_DIVBYZERO_MASK;
2229 break;
2230 case EXCP_SWI:
2231 /* The PC already points to the next instruction. */
2232 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
2233 break;
2234 case EXCP_PREFETCH_ABORT:
2235 case EXCP_DATA_ABORT:
2237 * Note that for M profile we don't have a guest facing FSR, but
2238 * the env->exception.fsr will be populated by the code that
2239 * raises the fault, in the A profile short-descriptor format.
2241 * Log the exception.vaddress now regardless of subtype, because
2242 * logging below only logs it when it goes into a guest visible
2243 * register.
2245 qemu_log_mask(CPU_LOG_INT, "...at fault address 0x%x\n",
2246 (uint32_t)env->exception.vaddress);
2247 switch (env->exception.fsr & 0xf) {
2248 case M_FAKE_FSR_NSC_EXEC:
2250 * Exception generated when we try to execute code at an address
2251 * which is marked as Secure & Non-Secure Callable and the CPU
2252 * is in the Non-Secure state. The only instruction which can
2253 * be executed like this is SG (and that only if both halves of
2254 * the SG instruction have the same security attributes.)
2255 * Everything else must generate an INVEP SecureFault, so we
2256 * emulate the SG instruction here.
2258 if (v7m_handle_execute_nsc(cpu)) {
2259 return;
2261 break;
2262 case M_FAKE_FSR_SFAULT:
2264 * Various flavours of SecureFault for attempts to execute or
2265 * access data in the wrong security state.
2267 switch (cs->exception_index) {
2268 case EXCP_PREFETCH_ABORT:
2269 if (env->v7m.secure) {
2270 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
2271 qemu_log_mask(CPU_LOG_INT,
2272 "...really SecureFault with SFSR.INVTRAN\n");
2273 } else {
2274 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
2275 qemu_log_mask(CPU_LOG_INT,
2276 "...really SecureFault with SFSR.INVEP\n");
2278 break;
2279 case EXCP_DATA_ABORT:
2280 /* This must be an NS access to S memory */
2281 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
2282 qemu_log_mask(CPU_LOG_INT,
2283 "...really SecureFault with SFSR.AUVIOL\n");
2284 break;
2286 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
2287 break;
2288 case 0x8: /* External Abort */
2289 switch (cs->exception_index) {
2290 case EXCP_PREFETCH_ABORT:
2291 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
2292 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
2293 break;
2294 case EXCP_DATA_ABORT:
2295 env->v7m.cfsr[M_REG_NS] |=
2296 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
2297 env->v7m.bfar = env->exception.vaddress;
2298 qemu_log_mask(CPU_LOG_INT,
2299 "...with CFSR.PRECISERR and BFAR 0x%x\n",
2300 env->v7m.bfar);
2301 break;
2303 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
2304 break;
2305 case 0x1: /* Alignment fault reported by generic code */
2306 qemu_log_mask(CPU_LOG_INT,
2307 "...really UsageFault with UFSR.UNALIGNED\n");
2308 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNALIGNED_MASK;
2309 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
2310 env->v7m.secure);
2311 break;
2312 default:
2314 * All other FSR values are either MPU faults or "can't happen
2315 * for M profile" cases.
2317 switch (cs->exception_index) {
2318 case EXCP_PREFETCH_ABORT:
2319 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
2320 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
2321 break;
2322 case EXCP_DATA_ABORT:
2323 env->v7m.cfsr[env->v7m.secure] |=
2324 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
2325 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
2326 qemu_log_mask(CPU_LOG_INT,
2327 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
2328 env->v7m.mmfar[env->v7m.secure]);
2329 break;
2331 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
2332 env->v7m.secure);
2333 break;
2335 break;
2336 case EXCP_SEMIHOST:
2337 qemu_log_mask(CPU_LOG_INT,
2338 "...handling as semihosting call 0x%x\n",
2339 env->regs[0]);
2340 #ifdef CONFIG_TCG
2341 do_common_semihosting(cs);
2342 #else
2343 g_assert_not_reached();
2344 #endif
2345 env->regs[15] += env->thumb ? 2 : 4;
2346 return;
2347 case EXCP_BKPT:
2348 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
2349 break;
2350 case EXCP_IRQ:
2351 break;
2352 case EXCP_EXCEPTION_EXIT:
2353 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
2354 /* Must be v8M security extension function return */
2355 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
2356 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
2357 if (do_v7m_function_return(cpu)) {
2358 return;
2360 } else {
2361 do_v7m_exception_exit(cpu);
2362 return;
2364 break;
2365 case EXCP_LAZYFP:
2367 * We already pended the specific exception in the NVIC in the
2368 * v7m_preserve_fp_state() helper function.
2370 break;
2371 default:
2372 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
2373 return; /* Never happens. Keep compiler happy. */
2376 if (arm_feature(env, ARM_FEATURE_V8)) {
2377 lr = R_V7M_EXCRET_RES1_MASK |
2378 R_V7M_EXCRET_DCRS_MASK;
2380 * The S bit indicates whether we should return to Secure
2381 * or NonSecure (ie our current state).
2382 * The ES bit indicates whether we're taking this exception
2383 * to Secure or NonSecure (ie our target state). We set it
2384 * later, in v7m_exception_taken().
2385 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
2386 * This corresponds to the ARM ARM pseudocode for v8M setting
2387 * some LR bits in PushStack() and some in ExceptionTaken();
2388 * the distinction matters for the tailchain cases where we
2389 * can take an exception without pushing the stack.
2391 if (env->v7m.secure) {
2392 lr |= R_V7M_EXCRET_S_MASK;
2394 } else {
2395 lr = R_V7M_EXCRET_RES1_MASK |
2396 R_V7M_EXCRET_S_MASK |
2397 R_V7M_EXCRET_DCRS_MASK |
2398 R_V7M_EXCRET_ES_MASK;
2399 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
2400 lr |= R_V7M_EXCRET_SPSEL_MASK;
2403 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) {
2404 lr |= R_V7M_EXCRET_FTYPE_MASK;
2406 if (!arm_v7m_is_handler_mode(env)) {
2407 lr |= R_V7M_EXCRET_MODE_MASK;
2410 ignore_stackfaults = v7m_push_stack(cpu);
2411 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
2414 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2416 unsigned el = arm_current_el(env);
2418 /* First handle registers which unprivileged can read */
2419 switch (reg) {
2420 case 0 ... 7: /* xPSR sub-fields */
2421 return v7m_mrs_xpsr(env, reg, el);
2422 case 20: /* CONTROL */
2423 return v7m_mrs_control(env, env->v7m.secure);
2424 case 0x94: /* CONTROL_NS */
2426 * We have to handle this here because unprivileged Secure code
2427 * can read the NS CONTROL register.
2429 if (!env->v7m.secure) {
2430 return 0;
2432 return env->v7m.control[M_REG_NS] |
2433 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK);
2436 if (el == 0) {
2437 return 0; /* unprivileged reads others as zero */
2440 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
2441 switch (reg) {
2442 case 0x88: /* MSP_NS */
2443 if (!env->v7m.secure) {
2444 return 0;
2446 return env->v7m.other_ss_msp;
2447 case 0x89: /* PSP_NS */
2448 if (!env->v7m.secure) {
2449 return 0;
2451 return env->v7m.other_ss_psp;
2452 case 0x8a: /* MSPLIM_NS */
2453 if (!env->v7m.secure) {
2454 return 0;
2456 return env->v7m.msplim[M_REG_NS];
2457 case 0x8b: /* PSPLIM_NS */
2458 if (!env->v7m.secure) {
2459 return 0;
2461 return env->v7m.psplim[M_REG_NS];
2462 case 0x90: /* PRIMASK_NS */
2463 if (!env->v7m.secure) {
2464 return 0;
2466 return env->v7m.primask[M_REG_NS];
2467 case 0x91: /* BASEPRI_NS */
2468 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2469 goto bad_reg;
2471 if (!env->v7m.secure) {
2472 return 0;
2474 return env->v7m.basepri[M_REG_NS];
2475 case 0x93: /* FAULTMASK_NS */
2476 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2477 goto bad_reg;
2479 if (!env->v7m.secure) {
2480 return 0;
2482 return env->v7m.faultmask[M_REG_NS];
2483 case 0x98: /* SP_NS */
2486 * This gives the non-secure SP selected based on whether we're
2487 * currently in handler mode or not, using the NS CONTROL.SPSEL.
2489 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
2491 if (!env->v7m.secure) {
2492 return 0;
2494 if (!arm_v7m_is_handler_mode(env) && spsel) {
2495 return env->v7m.other_ss_psp;
2496 } else {
2497 return env->v7m.other_ss_msp;
2500 default:
2501 break;
2505 switch (reg) {
2506 case 8: /* MSP */
2507 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
2508 case 9: /* PSP */
2509 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
2510 case 10: /* MSPLIM */
2511 if (!arm_feature(env, ARM_FEATURE_V8)) {
2512 goto bad_reg;
2514 return env->v7m.msplim[env->v7m.secure];
2515 case 11: /* PSPLIM */
2516 if (!arm_feature(env, ARM_FEATURE_V8)) {
2517 goto bad_reg;
2519 return env->v7m.psplim[env->v7m.secure];
2520 case 16: /* PRIMASK */
2521 return env->v7m.primask[env->v7m.secure];
2522 case 17: /* BASEPRI */
2523 case 18: /* BASEPRI_MAX */
2524 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2525 goto bad_reg;
2527 return env->v7m.basepri[env->v7m.secure];
2528 case 19: /* FAULTMASK */
2529 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2530 goto bad_reg;
2532 return env->v7m.faultmask[env->v7m.secure];
2533 default:
2534 bad_reg:
2535 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
2536 " register %d\n", reg);
2537 return 0;
2541 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
2544 * We're passed bits [11..0] of the instruction; extract
2545 * SYSm and the mask bits.
2546 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
2547 * we choose to treat them as if the mask bits were valid.
2548 * NB that the pseudocode 'mask' variable is bits [11..10],
2549 * whereas ours is [11..8].
2551 uint32_t mask = extract32(maskreg, 8, 4);
2552 uint32_t reg = extract32(maskreg, 0, 8);
2553 int cur_el = arm_current_el(env);
2555 if (cur_el == 0 && reg > 7 && reg != 20) {
2557 * only xPSR sub-fields and CONTROL.SFPA may be written by
2558 * unprivileged code
2560 return;
2563 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
2564 switch (reg) {
2565 case 0x88: /* MSP_NS */
2566 if (!env->v7m.secure) {
2567 return;
2569 env->v7m.other_ss_msp = val & ~3;
2570 return;
2571 case 0x89: /* PSP_NS */
2572 if (!env->v7m.secure) {
2573 return;
2575 env->v7m.other_ss_psp = val & ~3;
2576 return;
2577 case 0x8a: /* MSPLIM_NS */
2578 if (!env->v7m.secure) {
2579 return;
2581 env->v7m.msplim[M_REG_NS] = val & ~7;
2582 return;
2583 case 0x8b: /* PSPLIM_NS */
2584 if (!env->v7m.secure) {
2585 return;
2587 env->v7m.psplim[M_REG_NS] = val & ~7;
2588 return;
2589 case 0x90: /* PRIMASK_NS */
2590 if (!env->v7m.secure) {
2591 return;
2593 env->v7m.primask[M_REG_NS] = val & 1;
2594 return;
2595 case 0x91: /* BASEPRI_NS */
2596 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2597 goto bad_reg;
2599 if (!env->v7m.secure) {
2600 return;
2602 env->v7m.basepri[M_REG_NS] = val & 0xff;
2603 return;
2604 case 0x93: /* FAULTMASK_NS */
2605 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2606 goto bad_reg;
2608 if (!env->v7m.secure) {
2609 return;
2611 env->v7m.faultmask[M_REG_NS] = val & 1;
2612 return;
2613 case 0x94: /* CONTROL_NS */
2614 if (!env->v7m.secure) {
2615 return;
2617 write_v7m_control_spsel_for_secstate(env,
2618 val & R_V7M_CONTROL_SPSEL_MASK,
2619 M_REG_NS);
2620 if (arm_feature(env, ARM_FEATURE_M_MAIN)) {
2621 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
2622 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
2625 * SFPA is RAZ/WI from NS. FPCA is RO if NSACR.CP10 == 0,
2626 * RES0 if the FPU is not present, and is stored in the S bank
2628 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env)) &&
2629 extract32(env->v7m.nsacr, 10, 1)) {
2630 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK;
2631 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK;
2633 return;
2634 case 0x98: /* SP_NS */
2637 * This gives the non-secure SP selected based on whether we're
2638 * currently in handler mode or not, using the NS CONTROL.SPSEL.
2640 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
2641 bool is_psp = !arm_v7m_is_handler_mode(env) && spsel;
2642 uint32_t limit;
2644 if (!env->v7m.secure) {
2645 return;
2648 limit = is_psp ? env->v7m.psplim[false] : env->v7m.msplim[false];
2650 val &= ~0x3;
2652 if (val < limit) {
2653 raise_exception_ra(env, EXCP_STKOF, 0, 1, GETPC());
2656 if (is_psp) {
2657 env->v7m.other_ss_psp = val;
2658 } else {
2659 env->v7m.other_ss_msp = val;
2661 return;
2663 default:
2664 break;
2668 switch (reg) {
2669 case 0 ... 7: /* xPSR sub-fields */
2670 v7m_msr_xpsr(env, mask, reg, val);
2671 break;
2672 case 8: /* MSP */
2673 if (v7m_using_psp(env)) {
2674 env->v7m.other_sp = val & ~3;
2675 } else {
2676 env->regs[13] = val & ~3;
2678 break;
2679 case 9: /* PSP */
2680 if (v7m_using_psp(env)) {
2681 env->regs[13] = val & ~3;
2682 } else {
2683 env->v7m.other_sp = val & ~3;
2685 break;
2686 case 10: /* MSPLIM */
2687 if (!arm_feature(env, ARM_FEATURE_V8)) {
2688 goto bad_reg;
2690 env->v7m.msplim[env->v7m.secure] = val & ~7;
2691 break;
2692 case 11: /* PSPLIM */
2693 if (!arm_feature(env, ARM_FEATURE_V8)) {
2694 goto bad_reg;
2696 env->v7m.psplim[env->v7m.secure] = val & ~7;
2697 break;
2698 case 16: /* PRIMASK */
2699 env->v7m.primask[env->v7m.secure] = val & 1;
2700 break;
2701 case 17: /* BASEPRI */
2702 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2703 goto bad_reg;
2705 env->v7m.basepri[env->v7m.secure] = val & 0xff;
2706 break;
2707 case 18: /* BASEPRI_MAX */
2708 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2709 goto bad_reg;
2711 val &= 0xff;
2712 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
2713 || env->v7m.basepri[env->v7m.secure] == 0)) {
2714 env->v7m.basepri[env->v7m.secure] = val;
2716 break;
2717 case 19: /* FAULTMASK */
2718 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
2719 goto bad_reg;
2721 env->v7m.faultmask[env->v7m.secure] = val & 1;
2722 break;
2723 case 20: /* CONTROL */
2725 * Writing to the SPSEL bit only has an effect if we are in
2726 * thread mode; other bits can be updated by any privileged code.
2727 * write_v7m_control_spsel() deals with updating the SPSEL bit in
2728 * env->v7m.control, so we only need update the others.
2729 * For v7M, we must just ignore explicit writes to SPSEL in handler
2730 * mode; for v8M the write is permitted but will have no effect.
2731 * All these bits are writes-ignored from non-privileged code,
2732 * except for SFPA.
2734 if (cur_el > 0 && (arm_feature(env, ARM_FEATURE_V8) ||
2735 !arm_v7m_is_handler_mode(env))) {
2736 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
2738 if (cur_el > 0 && arm_feature(env, ARM_FEATURE_M_MAIN)) {
2739 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
2740 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
2742 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
2744 * SFPA is RAZ/WI from NS or if no FPU.
2745 * FPCA is RO if NSACR.CP10 == 0, RES0 if the FPU is not present.
2746 * Both are stored in the S bank.
2748 if (env->v7m.secure) {
2749 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
2750 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_SFPA_MASK;
2752 if (cur_el > 0 &&
2753 (env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_SECURITY) ||
2754 extract32(env->v7m.nsacr, 10, 1))) {
2755 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK;
2756 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK;
2759 break;
2760 default:
2761 bad_reg:
2762 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
2763 " register %d\n", reg);
2764 return;
2768 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
2770 /* Implement the TT instruction. op is bits [7:6] of the insn. */
2771 bool forceunpriv = op & 1;
2772 bool alt = op & 2;
2773 V8M_SAttributes sattrs = {};
2774 uint32_t tt_resp;
2775 bool r, rw, nsr, nsrw, mrvalid;
2776 ARMMMUIdx mmu_idx;
2777 uint32_t mregion;
2778 bool targetpriv;
2779 bool targetsec = env->v7m.secure;
2782 * Work out what the security state and privilege level we're
2783 * interested in is...
2785 if (alt) {
2786 targetsec = !targetsec;
2789 if (forceunpriv) {
2790 targetpriv = false;
2791 } else {
2792 targetpriv = arm_v7m_is_handler_mode(env) ||
2793 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
2796 /* ...and then figure out which MMU index this is */
2797 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
2800 * We know that the MPU and SAU don't care about the access type
2801 * for our purposes beyond that we don't want to claim to be
2802 * an insn fetch, so we arbitrarily call this a read.
2806 * MPU region info only available for privileged or if
2807 * inspecting the other MPU state.
2809 if (arm_current_el(env) != 0 || alt) {
2810 GetPhysAddrResult res = {};
2811 ARMMMUFaultInfo fi = {};
2813 /* We can ignore the return value as prot is always set */
2814 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, targetsec,
2815 &res, &fi, &mregion);
2816 if (mregion == -1) {
2817 mrvalid = false;
2818 mregion = 0;
2819 } else {
2820 mrvalid = true;
2822 r = res.f.prot & PAGE_READ;
2823 rw = res.f.prot & PAGE_WRITE;
2824 } else {
2825 r = false;
2826 rw = false;
2827 mrvalid = false;
2828 mregion = 0;
2831 if (env->v7m.secure) {
2832 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
2833 targetsec, &sattrs);
2834 nsr = sattrs.ns && r;
2835 nsrw = sattrs.ns && rw;
2836 } else {
2837 sattrs.ns = true;
2838 nsr = false;
2839 nsrw = false;
2842 tt_resp = (sattrs.iregion << 24) |
2843 (sattrs.irvalid << 23) |
2844 ((!sattrs.ns) << 22) |
2845 (nsrw << 21) |
2846 (nsr << 20) |
2847 (rw << 19) |
2848 (r << 18) |
2849 (sattrs.srvalid << 17) |
2850 (mrvalid << 16) |
2851 (sattrs.sregion << 8) |
2852 mregion;
2854 return tt_resp;
2857 #endif /* !CONFIG_USER_ONLY */
2859 ARMMMUIdx arm_v7m_mmu_idx_all(CPUARMState *env,
2860 bool secstate, bool priv, bool negpri)
2862 ARMMMUIdx mmu_idx = ARM_MMU_IDX_M;
2864 if (priv) {
2865 mmu_idx |= ARM_MMU_IDX_M_PRIV;
2868 if (negpri) {
2869 mmu_idx |= ARM_MMU_IDX_M_NEGPRI;
2872 if (secstate) {
2873 mmu_idx |= ARM_MMU_IDX_M_S;
2876 return mmu_idx;
2879 ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env,
2880 bool secstate, bool priv)
2882 bool negpri = armv7m_nvic_neg_prio_requested(env->nvic, secstate);
2884 return arm_v7m_mmu_idx_all(env, secstate, priv, negpri);
2887 /* Return the MMU index for a v7M CPU in the specified security state */
2888 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
2890 bool priv = arm_v7m_is_handler_mode(env) ||
2891 !(env->v7m.control[secstate] & 1);
2893 return arm_v7m_mmu_idx_for_secstate_and_priv(env, secstate, priv);