]>
Commit | Line | Data |
---|---|---|
7aab5a8c PMD |
1 | /* |
2 | * ARM generic helpers. | |
3 | * | |
4 | * This code is licensed under the GNU GPL v2 or later. | |
5 | * | |
6 | * SPDX-License-Identifier: GPL-2.0-or-later | |
7 | */ | |
db725815 | 8 | |
7aab5a8c PMD |
9 | #include "qemu/osdep.h" |
10 | #include "qemu/units.h" | |
11 | #include "target/arm/idau.h" | |
12 | #include "trace.h" | |
13 | #include "cpu.h" | |
14 | #include "internals.h" | |
15 | #include "exec/gdbstub.h" | |
16 | #include "exec/helper-proto.h" | |
17 | #include "qemu/host-utils.h" | |
db725815 | 18 | #include "qemu/main-loop.h" |
7aab5a8c PMD |
19 | #include "qemu/bitops.h" |
20 | #include "qemu/crc32c.h" | |
21 | #include "qemu/qemu-print.h" | |
cd617484 | 22 | #include "qemu/log.h" |
7aab5a8c PMD |
23 | #include "exec/exec-all.h" |
24 | #include <zlib.h> /* For crc32 */ | |
6b5fe137 | 25 | #include "semihosting/semihost.h" |
7aab5a8c PMD |
26 | #include "sysemu/cpus.h" |
27 | #include "sysemu/kvm.h" | |
28 | #include "qemu/range.h" | |
29 | #include "qapi/qapi-commands-machine-target.h" | |
30 | #include "qapi/error.h" | |
31 | #include "qemu/guest-random.h" | |
32 | #ifdef CONFIG_TCG | |
33 | #include "arm_ldst.h" | |
34 | #include "exec/cpu_ldst.h" | |
6b5fe137 | 35 | #include "semihosting/common-semi.h" |
7aab5a8c PMD |
36 | #endif |
37 | ||
04c9c81b RH |
38 | static void v7m_msr_xpsr(CPUARMState *env, uint32_t mask, |
39 | uint32_t reg, uint32_t val) | |
40 | { | |
41 | /* Only APSR is actually writable */ | |
42 | if (!(reg & 4)) { | |
43 | uint32_t apsrmask = 0; | |
44 | ||
45 | if (mask & 8) { | |
46 | apsrmask |= XPSR_NZCV | XPSR_Q; | |
47 | } | |
48 | if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) { | |
49 | apsrmask |= XPSR_GE; | |
50 | } | |
51 | xpsr_write(env, val, apsrmask); | |
52 | } | |
53 | } | |
54 | ||
55 | static uint32_t v7m_mrs_xpsr(CPUARMState *env, uint32_t reg, unsigned el) | |
56 | { | |
57 | uint32_t mask = 0; | |
58 | ||
59 | if ((reg & 1) && el) { | |
60 | mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */ | |
61 | } | |
62 | if (!(reg & 4)) { | |
63 | mask |= XPSR_NZCV | XPSR_Q; /* APSR */ | |
64 | if (arm_feature(env, ARM_FEATURE_THUMB_DSP)) { | |
65 | mask |= XPSR_GE; | |
66 | } | |
67 | } | |
68 | /* EPSR reads as zero */ | |
69 | return xpsr_read(env) & mask; | |
70 | } | |
71 | ||
72 | static uint32_t v7m_mrs_control(CPUARMState *env, uint32_t secure) | |
73 | { | |
74 | uint32_t value = env->v7m.control[secure]; | |
75 | ||
76 | if (!secure) { | |
77 | /* SFPA is RAZ/WI from NS; FPCA is stored in the M_REG_S bank */ | |
78 | value |= env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK; | |
79 | } | |
80 | return value; | |
81 | } | |
82 | ||
7aab5a8c PMD |
83 | #ifdef CONFIG_USER_ONLY |
84 | ||
04c9c81b | 85 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val) |
7aab5a8c | 86 | { |
04c9c81b RH |
87 | uint32_t mask = extract32(maskreg, 8, 4); |
88 | uint32_t reg = extract32(maskreg, 0, 8); | |
7aab5a8c | 89 | |
04c9c81b RH |
90 | switch (reg) { |
91 | case 0 ... 7: /* xPSR sub-fields */ | |
92 | v7m_msr_xpsr(env, mask, reg, val); | |
93 | break; | |
94 | case 20: /* CONTROL */ | |
95 | /* There are no sub-fields that are actually writable from EL0. */ | |
96 | break; | |
97 | default: | |
98 | /* Unprivileged writes to other registers are ignored */ | |
99 | break; | |
100 | } | |
7aab5a8c PMD |
101 | } |
102 | ||
103 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) | |
104 | { | |
04c9c81b RH |
105 | switch (reg) { |
106 | case 0 ... 7: /* xPSR sub-fields */ | |
107 | return v7m_mrs_xpsr(env, reg, 0); | |
108 | case 20: /* CONTROL */ | |
109 | return v7m_mrs_control(env, 0); | |
110 | default: | |
111 | /* Unprivileged reads others as zero. */ | |
112 | return 0; | |
113 | } | |
7aab5a8c PMD |
114 | } |
115 | ||
116 | void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) | |
117 | { | |
118 | /* translate.c should never generate calls here in user-only mode */ | |
119 | g_assert_not_reached(); | |
120 | } | |
121 | ||
122 | void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) | |
123 | { | |
124 | /* translate.c should never generate calls here in user-only mode */ | |
125 | g_assert_not_reached(); | |
126 | } | |
127 | ||
128 | void HELPER(v7m_preserve_fp_state)(CPUARMState *env) | |
129 | { | |
130 | /* translate.c should never generate calls here in user-only mode */ | |
131 | g_assert_not_reached(); | |
132 | } | |
133 | ||
134 | void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr) | |
135 | { | |
136 | /* translate.c should never generate calls here in user-only mode */ | |
137 | g_assert_not_reached(); | |
138 | } | |
139 | ||
140 | void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr) | |
141 | { | |
142 | /* translate.c should never generate calls here in user-only mode */ | |
143 | g_assert_not_reached(); | |
144 | } | |
145 | ||
146 | uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) | |
147 | { | |
148 | /* | |
149 | * The TT instructions can be used by unprivileged code, but in | |
150 | * user-only emulation we don't have the MPU. | |
151 | * Luckily since we know we are NonSecure unprivileged (and that in | |
152 | * turn means that the A flag wasn't specified), all the bits in the | |
153 | * register must be zero: | |
154 | * IREGION: 0 because IRVALID is 0 | |
155 | * IRVALID: 0 because NS | |
156 | * S: 0 because NS | |
157 | * NSRW: 0 because NS | |
158 | * NSR: 0 because NS | |
159 | * RW: 0 because unpriv and A flag not set | |
160 | * R: 0 because unpriv and A flag not set | |
161 | * SRVALID: 0 because NS | |
162 | * MRVALID: 0 because unpriv and A flag not set | |
163 | * SREGION: 0 becaus SRVALID is 0 | |
164 | * MREGION: 0 because MRVALID is 0 | |
165 | */ | |
166 | return 0; | |
167 | } | |
168 | ||
169 | #else | |
170 | ||
171 | /* | |
172 | * What kind of stack write are we doing? This affects how exceptions | |
173 | * generated during the stacking are treated. | |
174 | */ | |
175 | typedef enum StackingMode { | |
176 | STACK_NORMAL, | |
177 | STACK_IGNFAULTS, | |
178 | STACK_LAZYFP, | |
179 | } StackingMode; | |
180 | ||
181 | static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value, | |
182 | ARMMMUIdx mmu_idx, StackingMode mode) | |
183 | { | |
184 | CPUState *cs = CPU(cpu); | |
185 | CPUARMState *env = &cpu->env; | |
186 | MemTxAttrs attrs = {}; | |
187 | MemTxResult txres; | |
188 | target_ulong page_size; | |
189 | hwaddr physaddr; | |
190 | int prot; | |
191 | ARMMMUFaultInfo fi = {}; | |
7e98e21c | 192 | ARMCacheAttrs cacheattrs = {}; |
7aab5a8c PMD |
193 | bool secure = mmu_idx & ARM_MMU_IDX_M_S; |
194 | int exc; | |
195 | bool exc_secure; | |
196 | ||
197 | if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr, | |
7e98e21c | 198 | &attrs, &prot, &page_size, &fi, &cacheattrs)) { |
7aab5a8c PMD |
199 | /* MPU/SAU lookup failed */ |
200 | if (fi.type == ARMFault_QEMU_SFault) { | |
201 | if (mode == STACK_LAZYFP) { | |
202 | qemu_log_mask(CPU_LOG_INT, | |
203 | "...SecureFault with SFSR.LSPERR " | |
204 | "during lazy stacking\n"); | |
205 | env->v7m.sfsr |= R_V7M_SFSR_LSPERR_MASK; | |
206 | } else { | |
207 | qemu_log_mask(CPU_LOG_INT, | |
208 | "...SecureFault with SFSR.AUVIOL " | |
209 | "during stacking\n"); | |
210 | env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; | |
211 | } | |
212 | env->v7m.sfsr |= R_V7M_SFSR_SFARVALID_MASK; | |
213 | env->v7m.sfar = addr; | |
214 | exc = ARMV7M_EXCP_SECURE; | |
215 | exc_secure = false; | |
216 | } else { | |
217 | if (mode == STACK_LAZYFP) { | |
218 | qemu_log_mask(CPU_LOG_INT, | |
219 | "...MemManageFault with CFSR.MLSPERR\n"); | |
220 | env->v7m.cfsr[secure] |= R_V7M_CFSR_MLSPERR_MASK; | |
221 | } else { | |
222 | qemu_log_mask(CPU_LOG_INT, | |
223 | "...MemManageFault with CFSR.MSTKERR\n"); | |
224 | env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK; | |
225 | } | |
226 | exc = ARMV7M_EXCP_MEM; | |
227 | exc_secure = secure; | |
228 | } | |
229 | goto pend_fault; | |
230 | } | |
231 | address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value, | |
232 | attrs, &txres); | |
233 | if (txres != MEMTX_OK) { | |
234 | /* BusFault trying to write the data */ | |
235 | if (mode == STACK_LAZYFP) { | |
236 | qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.LSPERR\n"); | |
237 | env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_LSPERR_MASK; | |
238 | } else { | |
239 | qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n"); | |
240 | env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK; | |
241 | } | |
242 | exc = ARMV7M_EXCP_BUS; | |
243 | exc_secure = false; | |
244 | goto pend_fault; | |
245 | } | |
246 | return true; | |
247 | ||
248 | pend_fault: | |
249 | /* | |
250 | * By pending the exception at this point we are making | |
251 | * the IMPDEF choice "overridden exceptions pended" (see the | |
252 | * MergeExcInfo() pseudocode). The other choice would be to not | |
253 | * pend them now and then make a choice about which to throw away | |
254 | * later if we have two derived exceptions. | |
255 | * The only case when we must not pend the exception but instead | |
256 | * throw it away is if we are doing the push of the callee registers | |
257 | * and we've already generated a derived exception (this is indicated | |
258 | * by the caller passing STACK_IGNFAULTS). Even in this case we will | |
259 | * still update the fault status registers. | |
260 | */ | |
261 | switch (mode) { | |
262 | case STACK_NORMAL: | |
263 | armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure); | |
264 | break; | |
265 | case STACK_LAZYFP: | |
266 | armv7m_nvic_set_pending_lazyfp(env->nvic, exc, exc_secure); | |
267 | break; | |
268 | case STACK_IGNFAULTS: | |
269 | break; | |
270 | } | |
271 | return false; | |
272 | } | |
273 | ||
274 | static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr, | |
275 | ARMMMUIdx mmu_idx) | |
276 | { | |
277 | CPUState *cs = CPU(cpu); | |
278 | CPUARMState *env = &cpu->env; | |
279 | MemTxAttrs attrs = {}; | |
280 | MemTxResult txres; | |
281 | target_ulong page_size; | |
282 | hwaddr physaddr; | |
283 | int prot; | |
284 | ARMMMUFaultInfo fi = {}; | |
7e98e21c | 285 | ARMCacheAttrs cacheattrs = {}; |
7aab5a8c PMD |
286 | bool secure = mmu_idx & ARM_MMU_IDX_M_S; |
287 | int exc; | |
288 | bool exc_secure; | |
289 | uint32_t value; | |
290 | ||
291 | if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr, | |
7e98e21c | 292 | &attrs, &prot, &page_size, &fi, &cacheattrs)) { |
7aab5a8c PMD |
293 | /* MPU/SAU lookup failed */ |
294 | if (fi.type == ARMFault_QEMU_SFault) { | |
295 | qemu_log_mask(CPU_LOG_INT, | |
296 | "...SecureFault with SFSR.AUVIOL during unstack\n"); | |
297 | env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; | |
298 | env->v7m.sfar = addr; | |
299 | exc = ARMV7M_EXCP_SECURE; | |
300 | exc_secure = false; | |
301 | } else { | |
302 | qemu_log_mask(CPU_LOG_INT, | |
303 | "...MemManageFault with CFSR.MUNSTKERR\n"); | |
304 | env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK; | |
305 | exc = ARMV7M_EXCP_MEM; | |
306 | exc_secure = secure; | |
307 | } | |
308 | goto pend_fault; | |
309 | } | |
310 | ||
311 | value = address_space_ldl(arm_addressspace(cs, attrs), physaddr, | |
312 | attrs, &txres); | |
313 | if (txres != MEMTX_OK) { | |
314 | /* BusFault trying to read the data */ | |
315 | qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n"); | |
316 | env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK; | |
317 | exc = ARMV7M_EXCP_BUS; | |
318 | exc_secure = false; | |
319 | goto pend_fault; | |
320 | } | |
321 | ||
322 | *dest = value; | |
323 | return true; | |
324 | ||
325 | pend_fault: | |
326 | /* | |
327 | * By pending the exception at this point we are making | |
328 | * the IMPDEF choice "overridden exceptions pended" (see the | |
329 | * MergeExcInfo() pseudocode). The other choice would be to not | |
330 | * pend them now and then make a choice about which to throw away | |
331 | * later if we have two derived exceptions. | |
332 | */ | |
333 | armv7m_nvic_set_pending(env->nvic, exc, exc_secure); | |
334 | return false; | |
335 | } | |
336 | ||
337 | void HELPER(v7m_preserve_fp_state)(CPUARMState *env) | |
338 | { | |
339 | /* | |
340 | * Preserve FP state (because LSPACT was set and we are about | |
341 | * to execute an FP instruction). This corresponds to the | |
342 | * PreserveFPState() pseudocode. | |
343 | * We may throw an exception if the stacking fails. | |
344 | */ | |
345 | ARMCPU *cpu = env_archcpu(env); | |
346 | bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; | |
347 | bool negpri = !(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_HFRDY_MASK); | |
348 | bool is_priv = !(env->v7m.fpccr[is_secure] & R_V7M_FPCCR_USER_MASK); | |
349 | bool splimviol = env->v7m.fpccr[is_secure] & R_V7M_FPCCR_SPLIMVIOL_MASK; | |
350 | uint32_t fpcar = env->v7m.fpcar[is_secure]; | |
351 | bool stacked_ok = true; | |
352 | bool ts = is_secure && (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK); | |
353 | bool take_exception; | |
354 | ||
355 | /* Take the iothread lock as we are going to touch the NVIC */ | |
356 | qemu_mutex_lock_iothread(); | |
357 | ||
358 | /* Check the background context had access to the FPU */ | |
359 | if (!v7m_cpacr_pass(env, is_secure, is_priv)) { | |
360 | armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, is_secure); | |
361 | env->v7m.cfsr[is_secure] |= R_V7M_CFSR_NOCP_MASK; | |
362 | stacked_ok = false; | |
363 | } else if (!is_secure && !extract32(env->v7m.nsacr, 10, 1)) { | |
364 | armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S); | |
365 | env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK; | |
366 | stacked_ok = false; | |
367 | } | |
368 | ||
369 | if (!splimviol && stacked_ok) { | |
370 | /* We only stack if the stack limit wasn't violated */ | |
371 | int i; | |
372 | ARMMMUIdx mmu_idx; | |
373 | ||
374 | mmu_idx = arm_v7m_mmu_idx_all(env, is_secure, is_priv, negpri); | |
375 | for (i = 0; i < (ts ? 32 : 16); i += 2) { | |
376 | uint64_t dn = *aa32_vfp_dreg(env, i / 2); | |
377 | uint32_t faddr = fpcar + 4 * i; | |
378 | uint32_t slo = extract64(dn, 0, 32); | |
379 | uint32_t shi = extract64(dn, 32, 32); | |
380 | ||
381 | if (i >= 16) { | |
375256a8 | 382 | faddr += 8; /* skip the slot for the FPSCR/VPR */ |
7aab5a8c PMD |
383 | } |
384 | stacked_ok = stacked_ok && | |
385 | v7m_stack_write(cpu, faddr, slo, mmu_idx, STACK_LAZYFP) && | |
386 | v7m_stack_write(cpu, faddr + 4, shi, mmu_idx, STACK_LAZYFP); | |
387 | } | |
388 | ||
389 | stacked_ok = stacked_ok && | |
390 | v7m_stack_write(cpu, fpcar + 0x40, | |
391 | vfp_get_fpscr(env), mmu_idx, STACK_LAZYFP); | |
375256a8 PM |
392 | if (cpu_isar_feature(aa32_mve, cpu)) { |
393 | stacked_ok = stacked_ok && | |
394 | v7m_stack_write(cpu, fpcar + 0x44, | |
395 | env->v7m.vpr, mmu_idx, STACK_LAZYFP); | |
396 | } | |
7aab5a8c PMD |
397 | } |
398 | ||
399 | /* | |
400 | * We definitely pended an exception, but it's possible that it | |
401 | * might not be able to be taken now. If its priority permits us | |
402 | * to take it now, then we must not update the LSPACT or FP regs, | |
403 | * but instead jump out to take the exception immediately. | |
404 | * If it's just pending and won't be taken until the current | |
405 | * handler exits, then we do update LSPACT and the FP regs. | |
406 | */ | |
407 | take_exception = !stacked_ok && | |
408 | armv7m_nvic_can_take_pending_exception(env->nvic); | |
409 | ||
410 | qemu_mutex_unlock_iothread(); | |
411 | ||
412 | if (take_exception) { | |
413 | raise_exception_ra(env, EXCP_LAZYFP, 0, 1, GETPC()); | |
414 | } | |
415 | ||
416 | env->v7m.fpccr[is_secure] &= ~R_V7M_FPCCR_LSPACT_MASK; | |
417 | ||
418 | if (ts) { | |
375256a8 | 419 | /* Clear s0 to s31 and the FPSCR and VPR */ |
7aab5a8c PMD |
420 | int i; |
421 | ||
422 | for (i = 0; i < 32; i += 2) { | |
423 | *aa32_vfp_dreg(env, i / 2) = 0; | |
424 | } | |
425 | vfp_set_fpscr(env, 0); | |
375256a8 PM |
426 | if (cpu_isar_feature(aa32_mve, cpu)) { |
427 | env->v7m.vpr = 0; | |
428 | } | |
7aab5a8c PMD |
429 | } |
430 | /* | |
375256a8 | 431 | * Otherwise s0 to s15, FPSCR and VPR are UNKNOWN; we choose to leave them |
7aab5a8c PMD |
432 | * unchanged. |
433 | */ | |
434 | } | |
435 | ||
436 | /* | |
437 | * Write to v7M CONTROL.SPSEL bit for the specified security bank. | |
438 | * This may change the current stack pointer between Main and Process | |
439 | * stack pointers if it is done for the CONTROL register for the current | |
440 | * security state. | |
441 | */ | |
442 | static void write_v7m_control_spsel_for_secstate(CPUARMState *env, | |
443 | bool new_spsel, | |
444 | bool secstate) | |
445 | { | |
446 | bool old_is_psp = v7m_using_psp(env); | |
447 | ||
448 | env->v7m.control[secstate] = | |
449 | deposit32(env->v7m.control[secstate], | |
450 | R_V7M_CONTROL_SPSEL_SHIFT, | |
451 | R_V7M_CONTROL_SPSEL_LENGTH, new_spsel); | |
452 | ||
453 | if (secstate == env->v7m.secure) { | |
454 | bool new_is_psp = v7m_using_psp(env); | |
455 | uint32_t tmp; | |
456 | ||
457 | if (old_is_psp != new_is_psp) { | |
458 | tmp = env->v7m.other_sp; | |
459 | env->v7m.other_sp = env->regs[13]; | |
460 | env->regs[13] = tmp; | |
461 | } | |
462 | } | |
463 | } | |
464 | ||
465 | /* | |
466 | * Write to v7M CONTROL.SPSEL bit. This may change the current | |
467 | * stack pointer between Main and Process stack pointers. | |
468 | */ | |
469 | static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel) | |
470 | { | |
471 | write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure); | |
472 | } | |
473 | ||
474 | void write_v7m_exception(CPUARMState *env, uint32_t new_exc) | |
475 | { | |
476 | /* | |
477 | * Write a new value to v7m.exception, thus transitioning into or out | |
478 | * of Handler mode; this may result in a change of active stack pointer. | |
479 | */ | |
480 | bool new_is_psp, old_is_psp = v7m_using_psp(env); | |
481 | uint32_t tmp; | |
482 | ||
483 | env->v7m.exception = new_exc; | |
484 | ||
485 | new_is_psp = v7m_using_psp(env); | |
486 | ||
487 | if (old_is_psp != new_is_psp) { | |
488 | tmp = env->v7m.other_sp; | |
489 | env->v7m.other_sp = env->regs[13]; | |
490 | env->regs[13] = tmp; | |
491 | } | |
492 | } | |
493 | ||
494 | /* Switch M profile security state between NS and S */ | |
495 | static void switch_v7m_security_state(CPUARMState *env, bool new_secstate) | |
496 | { | |
497 | uint32_t new_ss_msp, new_ss_psp; | |
498 | ||
499 | if (env->v7m.secure == new_secstate) { | |
500 | return; | |
501 | } | |
502 | ||
503 | /* | |
504 | * All the banked state is accessed by looking at env->v7m.secure | |
505 | * except for the stack pointer; rearrange the SP appropriately. | |
506 | */ | |
507 | new_ss_msp = env->v7m.other_ss_msp; | |
508 | new_ss_psp = env->v7m.other_ss_psp; | |
509 | ||
510 | if (v7m_using_psp(env)) { | |
511 | env->v7m.other_ss_psp = env->regs[13]; | |
512 | env->v7m.other_ss_msp = env->v7m.other_sp; | |
513 | } else { | |
514 | env->v7m.other_ss_msp = env->regs[13]; | |
515 | env->v7m.other_ss_psp = env->v7m.other_sp; | |
516 | } | |
517 | ||
518 | env->v7m.secure = new_secstate; | |
519 | ||
520 | if (v7m_using_psp(env)) { | |
521 | env->regs[13] = new_ss_psp; | |
522 | env->v7m.other_sp = new_ss_msp; | |
523 | } else { | |
524 | env->regs[13] = new_ss_msp; | |
525 | env->v7m.other_sp = new_ss_psp; | |
526 | } | |
527 | } | |
528 | ||
529 | void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) | |
530 | { | |
531 | /* | |
532 | * Handle v7M BXNS: | |
533 | * - if the return value is a magic value, do exception return (like BX) | |
534 | * - otherwise bit 0 of the return value is the target security state | |
535 | */ | |
536 | uint32_t min_magic; | |
537 | ||
538 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
539 | /* Covers FNC_RETURN and EXC_RETURN magic */ | |
540 | min_magic = FNC_RETURN_MIN_MAGIC; | |
541 | } else { | |
542 | /* EXC_RETURN magic only */ | |
543 | min_magic = EXC_RETURN_MIN_MAGIC; | |
544 | } | |
545 | ||
546 | if (dest >= min_magic) { | |
547 | /* | |
548 | * This is an exception return magic value; put it where | |
549 | * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT. | |
550 | * Note that if we ever add gen_ss_advance() singlestep support to | |
551 | * M profile this should count as an "instruction execution complete" | |
552 | * event (compare gen_bx_excret_final_code()). | |
553 | */ | |
554 | env->regs[15] = dest & ~1; | |
555 | env->thumb = dest & 1; | |
556 | HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT); | |
557 | /* notreached */ | |
558 | } | |
559 | ||
560 | /* translate.c should have made BXNS UNDEF unless we're secure */ | |
561 | assert(env->v7m.secure); | |
562 | ||
563 | if (!(dest & 1)) { | |
564 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; | |
565 | } | |
566 | switch_v7m_security_state(env, dest & 1); | |
567 | env->thumb = 1; | |
568 | env->regs[15] = dest & ~1; | |
873be7b6 | 569 | arm_rebuild_hflags(env); |
7aab5a8c PMD |
570 | } |
571 | ||
572 | void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) | |
573 | { | |
574 | /* | |
575 | * Handle v7M BLXNS: | |
576 | * - bit 0 of the destination address is the target security state | |
577 | */ | |
578 | ||
579 | /* At this point regs[15] is the address just after the BLXNS */ | |
580 | uint32_t nextinst = env->regs[15] | 1; | |
581 | uint32_t sp = env->regs[13] - 8; | |
582 | uint32_t saved_psr; | |
583 | ||
584 | /* translate.c will have made BLXNS UNDEF unless we're secure */ | |
585 | assert(env->v7m.secure); | |
586 | ||
587 | if (dest & 1) { | |
588 | /* | |
589 | * Target is Secure, so this is just a normal BLX, | |
590 | * except that the low bit doesn't indicate Thumb/not. | |
591 | */ | |
592 | env->regs[14] = nextinst; | |
593 | env->thumb = 1; | |
594 | env->regs[15] = dest & ~1; | |
595 | return; | |
596 | } | |
597 | ||
598 | /* Target is non-secure: first push a stack frame */ | |
599 | if (!QEMU_IS_ALIGNED(sp, 8)) { | |
600 | qemu_log_mask(LOG_GUEST_ERROR, | |
601 | "BLXNS with misaligned SP is UNPREDICTABLE\n"); | |
602 | } | |
603 | ||
604 | if (sp < v7m_sp_limit(env)) { | |
605 | raise_exception(env, EXCP_STKOF, 0, 1); | |
606 | } | |
607 | ||
608 | saved_psr = env->v7m.exception; | |
609 | if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) { | |
610 | saved_psr |= XPSR_SFPA; | |
611 | } | |
612 | ||
613 | /* Note that these stores can throw exceptions on MPU faults */ | |
2884fbb6 PM |
614 | cpu_stl_data_ra(env, sp, nextinst, GETPC()); |
615 | cpu_stl_data_ra(env, sp + 4, saved_psr, GETPC()); | |
7aab5a8c PMD |
616 | |
617 | env->regs[13] = sp; | |
618 | env->regs[14] = 0xfeffffff; | |
619 | if (arm_v7m_is_handler_mode(env)) { | |
620 | /* | |
621 | * Write a dummy value to IPSR, to avoid leaking the current secure | |
622 | * exception number to non-secure code. This is guaranteed not | |
623 | * to cause write_v7m_exception() to actually change stacks. | |
624 | */ | |
625 | write_v7m_exception(env, 1); | |
626 | } | |
627 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; | |
628 | switch_v7m_security_state(env, 0); | |
629 | env->thumb = 1; | |
630 | env->regs[15] = dest; | |
873be7b6 | 631 | arm_rebuild_hflags(env); |
7aab5a8c PMD |
632 | } |
633 | ||
634 | static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode, | |
635 | bool spsel) | |
636 | { | |
637 | /* | |
638 | * Return a pointer to the location where we currently store the | |
639 | * stack pointer for the requested security state and thread mode. | |
640 | * This pointer will become invalid if the CPU state is updated | |
641 | * such that the stack pointers are switched around (eg changing | |
642 | * the SPSEL control bit). | |
643 | * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode(). | |
644 | * Unlike that pseudocode, we require the caller to pass us in the | |
645 | * SPSEL control bit value; this is because we also use this | |
646 | * function in handling of pushing of the callee-saves registers | |
647 | * part of the v8M stack frame (pseudocode PushCalleeStack()), | |
648 | * and in the tailchain codepath the SPSEL bit comes from the exception | |
649 | * return magic LR value from the previous exception. The pseudocode | |
650 | * opencodes the stack-selection in PushCalleeStack(), but we prefer | |
651 | * to make this utility function generic enough to do the job. | |
652 | */ | |
653 | bool want_psp = threadmode && spsel; | |
654 | ||
655 | if (secure == env->v7m.secure) { | |
656 | if (want_psp == v7m_using_psp(env)) { | |
657 | return &env->regs[13]; | |
658 | } else { | |
659 | return &env->v7m.other_sp; | |
660 | } | |
661 | } else { | |
662 | if (want_psp) { | |
663 | return &env->v7m.other_ss_psp; | |
664 | } else { | |
665 | return &env->v7m.other_ss_msp; | |
666 | } | |
667 | } | |
668 | } | |
669 | ||
670 | static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure, | |
671 | uint32_t *pvec) | |
672 | { | |
673 | CPUState *cs = CPU(cpu); | |
674 | CPUARMState *env = &cpu->env; | |
675 | MemTxResult result; | |
676 | uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4; | |
677 | uint32_t vector_entry; | |
678 | MemTxAttrs attrs = {}; | |
679 | ARMMMUIdx mmu_idx; | |
680 | bool exc_secure; | |
681 | ||
682 | mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true); | |
683 | ||
684 | /* | |
685 | * We don't do a get_phys_addr() here because the rules for vector | |
686 | * loads are special: they always use the default memory map, and | |
687 | * the default memory map permits reads from all addresses. | |
688 | * Since there's no easy way to pass through to pmsav8_mpu_lookup() | |
689 | * that we want this special case which would always say "yes", | |
690 | * we just do the SAU lookup here followed by a direct physical load. | |
691 | */ | |
692 | attrs.secure = targets_secure; | |
693 | attrs.user = false; | |
694 | ||
695 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
696 | V8M_SAttributes sattrs = {}; | |
697 | ||
698 | v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); | |
699 | if (sattrs.ns) { | |
700 | attrs.secure = false; | |
701 | } else if (!targets_secure) { | |
51c9122e PM |
702 | /* |
703 | * NS access to S memory: the underlying exception which we escalate | |
704 | * to HardFault is SecureFault, which always targets Secure. | |
705 | */ | |
706 | exc_secure = true; | |
7aab5a8c PMD |
707 | goto load_fail; |
708 | } | |
709 | } | |
710 | ||
711 | vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr, | |
712 | attrs, &result); | |
713 | if (result != MEMTX_OK) { | |
51c9122e PM |
714 | /* |
715 | * Underlying exception is BusFault: its target security state | |
716 | * depends on BFHFNMINS. | |
717 | */ | |
718 | exc_secure = !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK); | |
7aab5a8c PMD |
719 | goto load_fail; |
720 | } | |
721 | *pvec = vector_entry; | |
722 | return true; | |
723 | ||
724 | load_fail: | |
725 | /* | |
726 | * All vector table fetch fails are reported as HardFault, with | |
727 | * HFSR.VECTTBL and .FORCED set. (FORCED is set because | |
51c9122e | 728 | * technically the underlying exception is a SecureFault or BusFault |
7aab5a8c PMD |
729 | * that is escalated to HardFault.) This is a terminal exception, |
730 | * so we will either take the HardFault immediately or else enter | |
731 | * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()). | |
51c9122e PM |
732 | * The HardFault is Secure if BFHFNMINS is 0 (meaning that all HFs are |
733 | * secure); otherwise it targets the same security state as the | |
734 | * underlying exception. | |
be9500bb | 735 | * In v8.1M HardFaults from vector table fetch fails don't set FORCED. |
7aab5a8c | 736 | */ |
51c9122e PM |
737 | if (!(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { |
738 | exc_secure = true; | |
739 | } | |
be9500bb PM |
740 | env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK; |
741 | if (!arm_feature(env, ARM_FEATURE_V8_1M)) { | |
742 | env->v7m.hfsr |= R_V7M_HFSR_FORCED_MASK; | |
743 | } | |
7aab5a8c PMD |
744 | armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure); |
745 | return false; | |
746 | } | |
747 | ||
748 | static uint32_t v7m_integrity_sig(CPUARMState *env, uint32_t lr) | |
749 | { | |
750 | /* | |
751 | * Return the integrity signature value for the callee-saves | |
752 | * stack frame section. @lr is the exception return payload/LR value | |
753 | * whose FType bit forms bit 0 of the signature if FP is present. | |
754 | */ | |
755 | uint32_t sig = 0xfefa125a; | |
756 | ||
7fbc6a40 RH |
757 | if (!cpu_isar_feature(aa32_vfp_simd, env_archcpu(env)) |
758 | || (lr & R_V7M_EXCRET_FTYPE_MASK)) { | |
7aab5a8c PMD |
759 | sig |= 1; |
760 | } | |
761 | return sig; | |
762 | } | |
763 | ||
764 | static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain, | |
765 | bool ignore_faults) | |
766 | { | |
767 | /* | |
768 | * For v8M, push the callee-saves register part of the stack frame. | |
769 | * Compare the v8M pseudocode PushCalleeStack(). | |
770 | * In the tailchaining case this may not be the current stack. | |
771 | */ | |
772 | CPUARMState *env = &cpu->env; | |
773 | uint32_t *frame_sp_p; | |
774 | uint32_t frameptr; | |
775 | ARMMMUIdx mmu_idx; | |
776 | bool stacked_ok; | |
777 | uint32_t limit; | |
778 | bool want_psp; | |
779 | uint32_t sig; | |
780 | StackingMode smode = ignore_faults ? STACK_IGNFAULTS : STACK_NORMAL; | |
781 | ||
782 | if (dotailchain) { | |
783 | bool mode = lr & R_V7M_EXCRET_MODE_MASK; | |
784 | bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) || | |
785 | !mode; | |
786 | ||
787 | mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv); | |
788 | frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode, | |
789 | lr & R_V7M_EXCRET_SPSEL_MASK); | |
790 | want_psp = mode && (lr & R_V7M_EXCRET_SPSEL_MASK); | |
791 | if (want_psp) { | |
792 | limit = env->v7m.psplim[M_REG_S]; | |
793 | } else { | |
794 | limit = env->v7m.msplim[M_REG_S]; | |
795 | } | |
796 | } else { | |
797 | mmu_idx = arm_mmu_idx(env); | |
798 | frame_sp_p = &env->regs[13]; | |
799 | limit = v7m_sp_limit(env); | |
800 | } | |
801 | ||
802 | frameptr = *frame_sp_p - 0x28; | |
803 | if (frameptr < limit) { | |
804 | /* | |
805 | * Stack limit failure: set SP to the limit value, and generate | |
806 | * STKOF UsageFault. Stack pushes below the limit must not be | |
807 | * performed. It is IMPDEF whether pushes above the limit are | |
808 | * performed; we choose not to. | |
809 | */ | |
810 | qemu_log_mask(CPU_LOG_INT, | |
811 | "...STKOF during callee-saves register stacking\n"); | |
812 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; | |
813 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
814 | env->v7m.secure); | |
815 | *frame_sp_p = limit; | |
816 | return true; | |
817 | } | |
818 | ||
819 | /* | |
820 | * Write as much of the stack frame as we can. A write failure may | |
821 | * cause us to pend a derived exception. | |
822 | */ | |
823 | sig = v7m_integrity_sig(env, lr); | |
824 | stacked_ok = | |
825 | v7m_stack_write(cpu, frameptr, sig, mmu_idx, smode) && | |
826 | v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx, smode) && | |
827 | v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx, smode) && | |
828 | v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx, smode) && | |
829 | v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx, smode) && | |
830 | v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx, smode) && | |
831 | v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx, smode) && | |
832 | v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx, smode) && | |
833 | v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx, smode); | |
834 | ||
835 | /* Update SP regardless of whether any of the stack accesses failed. */ | |
836 | *frame_sp_p = frameptr; | |
837 | ||
838 | return !stacked_ok; | |
839 | } | |
840 | ||
841 | static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain, | |
842 | bool ignore_stackfaults) | |
843 | { | |
844 | /* | |
845 | * Do the "take the exception" parts of exception entry, | |
846 | * but not the pushing of state to the stack. This is | |
847 | * similar to the pseudocode ExceptionTaken() function. | |
848 | */ | |
849 | CPUARMState *env = &cpu->env; | |
850 | uint32_t addr; | |
851 | bool targets_secure; | |
852 | int exc; | |
853 | bool push_failed = false; | |
854 | ||
855 | armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure); | |
856 | qemu_log_mask(CPU_LOG_INT, "...taking pending %s exception %d\n", | |
857 | targets_secure ? "secure" : "nonsecure", exc); | |
858 | ||
859 | if (dotailchain) { | |
860 | /* Sanitize LR FType and PREFIX bits */ | |
7fbc6a40 | 861 | if (!cpu_isar_feature(aa32_vfp_simd, cpu)) { |
7aab5a8c PMD |
862 | lr |= R_V7M_EXCRET_FTYPE_MASK; |
863 | } | |
864 | lr = deposit32(lr, 24, 8, 0xff); | |
865 | } | |
866 | ||
867 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
868 | if (arm_feature(env, ARM_FEATURE_M_SECURITY) && | |
869 | (lr & R_V7M_EXCRET_S_MASK)) { | |
870 | /* | |
871 | * The background code (the owner of the registers in the | |
872 | * exception frame) is Secure. This means it may either already | |
873 | * have or now needs to push callee-saves registers. | |
874 | */ | |
875 | if (targets_secure) { | |
876 | if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) { | |
877 | /* | |
878 | * We took an exception from Secure to NonSecure | |
879 | * (which means the callee-saved registers got stacked) | |
880 | * and are now tailchaining to a Secure exception. | |
881 | * Clear DCRS so eventual return from this Secure | |
882 | * exception unstacks the callee-saved registers. | |
883 | */ | |
884 | lr &= ~R_V7M_EXCRET_DCRS_MASK; | |
885 | } | |
886 | } else { | |
887 | /* | |
888 | * We're going to a non-secure exception; push the | |
889 | * callee-saves registers to the stack now, if they're | |
890 | * not already saved. | |
891 | */ | |
892 | if (lr & R_V7M_EXCRET_DCRS_MASK && | |
893 | !(dotailchain && !(lr & R_V7M_EXCRET_ES_MASK))) { | |
894 | push_failed = v7m_push_callee_stack(cpu, lr, dotailchain, | |
895 | ignore_stackfaults); | |
896 | } | |
897 | lr |= R_V7M_EXCRET_DCRS_MASK; | |
898 | } | |
899 | } | |
900 | ||
901 | lr &= ~R_V7M_EXCRET_ES_MASK; | |
902 | if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
903 | lr |= R_V7M_EXCRET_ES_MASK; | |
904 | } | |
905 | lr &= ~R_V7M_EXCRET_SPSEL_MASK; | |
906 | if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) { | |
907 | lr |= R_V7M_EXCRET_SPSEL_MASK; | |
908 | } | |
909 | ||
910 | /* | |
911 | * Clear registers if necessary to prevent non-secure exception | |
912 | * code being able to see register values from secure code. | |
913 | * Where register values become architecturally UNKNOWN we leave | |
a59b1ed6 PM |
914 | * them with their previous values. v8.1M is tighter than v8.0M |
915 | * here and always zeroes the caller-saved registers regardless | |
916 | * of the security state the exception is targeting. | |
7aab5a8c PMD |
917 | */ |
918 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
a59b1ed6 | 919 | if (!targets_secure || arm_feature(env, ARM_FEATURE_V8_1M)) { |
7aab5a8c PMD |
920 | /* |
921 | * Always clear the caller-saved registers (they have been | |
922 | * pushed to the stack earlier in v7m_push_stack()). | |
923 | * Clear callee-saved registers if the background code is | |
924 | * Secure (in which case these regs were saved in | |
925 | * v7m_push_callee_stack()). | |
926 | */ | |
927 | int i; | |
a59b1ed6 PM |
928 | /* |
929 | * r4..r11 are callee-saves, zero only if background | |
930 | * state was Secure (EXCRET.S == 1) and exception | |
931 | * targets Non-secure state | |
932 | */ | |
933 | bool zero_callee_saves = !targets_secure && | |
934 | (lr & R_V7M_EXCRET_S_MASK); | |
7aab5a8c PMD |
935 | |
936 | for (i = 0; i < 13; i++) { | |
a59b1ed6 | 937 | if (i < 4 || i > 11 || zero_callee_saves) { |
7aab5a8c PMD |
938 | env->regs[i] = 0; |
939 | } | |
940 | } | |
941 | /* Clear EAPSR */ | |
942 | xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT); | |
943 | } | |
944 | } | |
945 | } | |
946 | ||
947 | if (push_failed && !ignore_stackfaults) { | |
948 | /* | |
949 | * Derived exception on callee-saves register stacking: | |
950 | * we might now want to take a different exception which | |
951 | * targets a different security state, so try again from the top. | |
952 | */ | |
953 | qemu_log_mask(CPU_LOG_INT, | |
954 | "...derived exception on callee-saves register stacking"); | |
955 | v7m_exception_taken(cpu, lr, true, true); | |
956 | return; | |
957 | } | |
958 | ||
959 | if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) { | |
960 | /* Vector load failed: derived exception */ | |
961 | qemu_log_mask(CPU_LOG_INT, "...derived exception on vector table load"); | |
962 | v7m_exception_taken(cpu, lr, true, true); | |
963 | return; | |
964 | } | |
965 | ||
966 | /* | |
967 | * Now we've done everything that might cause a derived exception | |
968 | * we can go ahead and activate whichever exception we're going to | |
969 | * take (which might now be the derived exception). | |
970 | */ | |
971 | armv7m_nvic_acknowledge_irq(env->nvic); | |
972 | ||
973 | /* Switch to target security state -- must do this before writing SPSEL */ | |
974 | switch_v7m_security_state(env, targets_secure); | |
975 | write_v7m_control_spsel(env, 0); | |
976 | arm_clear_exclusive(env); | |
977 | /* Clear SFPA and FPCA (has no effect if no FPU) */ | |
978 | env->v7m.control[M_REG_S] &= | |
979 | ~(R_V7M_CONTROL_FPCA_MASK | R_V7M_CONTROL_SFPA_MASK); | |
980 | /* Clear IT bits */ | |
981 | env->condexec_bits = 0; | |
982 | env->regs[14] = lr; | |
983 | env->regs[15] = addr & 0xfffffffe; | |
984 | env->thumb = addr & 1; | |
873be7b6 | 985 | arm_rebuild_hflags(env); |
7aab5a8c PMD |
986 | } |
987 | ||
988 | static void v7m_update_fpccr(CPUARMState *env, uint32_t frameptr, | |
989 | bool apply_splim) | |
990 | { | |
991 | /* | |
992 | * Like the pseudocode UpdateFPCCR: save state in FPCAR and FPCCR | |
993 | * that we will need later in order to do lazy FP reg stacking. | |
994 | */ | |
995 | bool is_secure = env->v7m.secure; | |
996 | void *nvic = env->nvic; | |
997 | /* | |
998 | * Some bits are unbanked and live always in fpccr[M_REG_S]; some bits | |
999 | * are banked and we want to update the bit in the bank for the | |
1000 | * current security state; and in one case we want to specifically | |
1001 | * update the NS banked version of a bit even if we are secure. | |
1002 | */ | |
1003 | uint32_t *fpccr_s = &env->v7m.fpccr[M_REG_S]; | |
1004 | uint32_t *fpccr_ns = &env->v7m.fpccr[M_REG_NS]; | |
1005 | uint32_t *fpccr = &env->v7m.fpccr[is_secure]; | |
1006 | bool hfrdy, bfrdy, mmrdy, ns_ufrdy, s_ufrdy, sfrdy, monrdy; | |
1007 | ||
1008 | env->v7m.fpcar[is_secure] = frameptr & ~0x7; | |
1009 | ||
1010 | if (apply_splim && arm_feature(env, ARM_FEATURE_V8)) { | |
1011 | bool splimviol; | |
1012 | uint32_t splim = v7m_sp_limit(env); | |
1013 | bool ign = armv7m_nvic_neg_prio_requested(nvic, is_secure) && | |
1014 | (env->v7m.ccr[is_secure] & R_V7M_CCR_STKOFHFNMIGN_MASK); | |
1015 | ||
1016 | splimviol = !ign && frameptr < splim; | |
1017 | *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, SPLIMVIOL, splimviol); | |
1018 | } | |
1019 | ||
1020 | *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, LSPACT, 1); | |
1021 | ||
1022 | *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, S, is_secure); | |
1023 | ||
1024 | *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, USER, arm_current_el(env) == 0); | |
1025 | ||
1026 | *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, THREAD, | |
1027 | !arm_v7m_is_handler_mode(env)); | |
1028 | ||
1029 | hfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_HARD, false); | |
1030 | *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, HFRDY, hfrdy); | |
1031 | ||
1032 | bfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_BUS, false); | |
1033 | *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, BFRDY, bfrdy); | |
1034 | ||
1035 | mmrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_MEM, is_secure); | |
1036 | *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, MMRDY, mmrdy); | |
1037 | ||
1038 | ns_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, false); | |
1039 | *fpccr_ns = FIELD_DP32(*fpccr_ns, V7M_FPCCR, UFRDY, ns_ufrdy); | |
1040 | ||
1041 | monrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_DEBUG, false); | |
1042 | *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, MONRDY, monrdy); | |
1043 | ||
1044 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
1045 | s_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, true); | |
1046 | *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, UFRDY, s_ufrdy); | |
1047 | ||
1048 | sfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_SECURE, false); | |
1049 | *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, SFRDY, sfrdy); | |
1050 | } | |
1051 | } | |
1052 | ||
1053 | void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr) | |
1054 | { | |
1055 | /* fptr is the value of Rn, the frame pointer we store the FP regs to */ | |
375256a8 | 1056 | ARMCPU *cpu = env_archcpu(env); |
7aab5a8c PMD |
1057 | bool s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; |
1058 | bool lspact = env->v7m.fpccr[s] & R_V7M_FPCCR_LSPACT_MASK; | |
2884fbb6 | 1059 | uintptr_t ra = GETPC(); |
7aab5a8c PMD |
1060 | |
1061 | assert(env->v7m.secure); | |
1062 | ||
1063 | if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { | |
1064 | return; | |
1065 | } | |
1066 | ||
1067 | /* Check access to the coprocessor is permitted */ | |
1068 | if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) { | |
1069 | raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC()); | |
1070 | } | |
1071 | ||
1072 | if (lspact) { | |
1073 | /* LSPACT should not be active when there is active FP state */ | |
1074 | raise_exception_ra(env, EXCP_LSERR, 0, 1, GETPC()); | |
1075 | } | |
1076 | ||
1077 | if (fptr & 7) { | |
1078 | raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC()); | |
1079 | } | |
1080 | ||
1081 | /* | |
1082 | * Note that we do not use v7m_stack_write() here, because the | |
1083 | * accesses should not set the FSR bits for stacking errors if they | |
1084 | * fail. (In pseudocode terms, they are AccType_NORMAL, not AccType_STACK | |
2884fbb6 | 1085 | * or AccType_LAZYFP). Faults in cpu_stl_data_ra() will throw exceptions |
7aab5a8c PMD |
1086 | * and longjmp out. |
1087 | */ | |
1088 | if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) { | |
1089 | bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK; | |
1090 | int i; | |
1091 | ||
1092 | for (i = 0; i < (ts ? 32 : 16); i += 2) { | |
1093 | uint64_t dn = *aa32_vfp_dreg(env, i / 2); | |
1094 | uint32_t faddr = fptr + 4 * i; | |
1095 | uint32_t slo = extract64(dn, 0, 32); | |
1096 | uint32_t shi = extract64(dn, 32, 32); | |
1097 | ||
1098 | if (i >= 16) { | |
1099 | faddr += 8; /* skip the slot for the FPSCR */ | |
1100 | } | |
2884fbb6 PM |
1101 | cpu_stl_data_ra(env, faddr, slo, ra); |
1102 | cpu_stl_data_ra(env, faddr + 4, shi, ra); | |
7aab5a8c | 1103 | } |
2884fbb6 | 1104 | cpu_stl_data_ra(env, fptr + 0x40, vfp_get_fpscr(env), ra); |
375256a8 PM |
1105 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1106 | cpu_stl_data_ra(env, fptr + 0x44, env->v7m.vpr, ra); | |
1107 | } | |
7aab5a8c PMD |
1108 | |
1109 | /* | |
375256a8 | 1110 | * If TS is 0 then s0 to s15, FPSCR and VPR are UNKNOWN; we choose to |
7aab5a8c PMD |
1111 | * leave them unchanged, matching our choice in v7m_preserve_fp_state. |
1112 | */ | |
1113 | if (ts) { | |
1114 | for (i = 0; i < 32; i += 2) { | |
1115 | *aa32_vfp_dreg(env, i / 2) = 0; | |
1116 | } | |
1117 | vfp_set_fpscr(env, 0); | |
375256a8 PM |
1118 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1119 | env->v7m.vpr = 0; | |
1120 | } | |
7aab5a8c PMD |
1121 | } |
1122 | } else { | |
1123 | v7m_update_fpccr(env, fptr, false); | |
1124 | } | |
1125 | ||
1126 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; | |
1127 | } | |
1128 | ||
1129 | void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr) | |
1130 | { | |
375256a8 | 1131 | ARMCPU *cpu = env_archcpu(env); |
2884fbb6 PM |
1132 | uintptr_t ra = GETPC(); |
1133 | ||
7aab5a8c PMD |
1134 | /* fptr is the value of Rn, the frame pointer we load the FP regs from */ |
1135 | assert(env->v7m.secure); | |
1136 | ||
1137 | if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { | |
1138 | return; | |
1139 | } | |
1140 | ||
1141 | /* Check access to the coprocessor is permitted */ | |
1142 | if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) { | |
1143 | raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC()); | |
1144 | } | |
1145 | ||
1146 | if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) { | |
1147 | /* State in FP is still valid */ | |
1148 | env->v7m.fpccr[M_REG_S] &= ~R_V7M_FPCCR_LSPACT_MASK; | |
1149 | } else { | |
1150 | bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK; | |
1151 | int i; | |
1152 | uint32_t fpscr; | |
1153 | ||
1154 | if (fptr & 7) { | |
1155 | raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC()); | |
1156 | } | |
1157 | ||
1158 | for (i = 0; i < (ts ? 32 : 16); i += 2) { | |
1159 | uint32_t slo, shi; | |
1160 | uint64_t dn; | |
1161 | uint32_t faddr = fptr + 4 * i; | |
1162 | ||
1163 | if (i >= 16) { | |
375256a8 | 1164 | faddr += 8; /* skip the slot for the FPSCR and VPR */ |
7aab5a8c PMD |
1165 | } |
1166 | ||
2884fbb6 PM |
1167 | slo = cpu_ldl_data_ra(env, faddr, ra); |
1168 | shi = cpu_ldl_data_ra(env, faddr + 4, ra); | |
7aab5a8c PMD |
1169 | |
1170 | dn = (uint64_t) shi << 32 | slo; | |
1171 | *aa32_vfp_dreg(env, i / 2) = dn; | |
1172 | } | |
2884fbb6 | 1173 | fpscr = cpu_ldl_data_ra(env, fptr + 0x40, ra); |
7aab5a8c | 1174 | vfp_set_fpscr(env, fpscr); |
375256a8 PM |
1175 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1176 | env->v7m.vpr = cpu_ldl_data_ra(env, fptr + 0x44, ra); | |
1177 | } | |
7aab5a8c PMD |
1178 | } |
1179 | ||
1180 | env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK; | |
1181 | } | |
1182 | ||
1183 | static bool v7m_push_stack(ARMCPU *cpu) | |
1184 | { | |
1185 | /* | |
1186 | * Do the "set up stack frame" part of exception entry, | |
1187 | * similar to pseudocode PushStack(). | |
1188 | * Return true if we generate a derived exception (and so | |
1189 | * should ignore further stack faults trying to process | |
1190 | * that derived exception.) | |
1191 | */ | |
1192 | bool stacked_ok = true, limitviol = false; | |
1193 | CPUARMState *env = &cpu->env; | |
1194 | uint32_t xpsr = xpsr_read(env); | |
1195 | uint32_t frameptr = env->regs[13]; | |
1196 | ARMMMUIdx mmu_idx = arm_mmu_idx(env); | |
1197 | uint32_t framesize; | |
1198 | bool nsacr_cp10 = extract32(env->v7m.nsacr, 10, 1); | |
1199 | ||
1200 | if ((env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) && | |
1201 | (env->v7m.secure || nsacr_cp10)) { | |
1202 | if (env->v7m.secure && | |
1203 | env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK) { | |
1204 | framesize = 0xa8; | |
1205 | } else { | |
1206 | framesize = 0x68; | |
1207 | } | |
1208 | } else { | |
1209 | framesize = 0x20; | |
1210 | } | |
1211 | ||
1212 | /* Align stack pointer if the guest wants that */ | |
1213 | if ((frameptr & 4) && | |
1214 | (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) { | |
1215 | frameptr -= 4; | |
1216 | xpsr |= XPSR_SPREALIGN; | |
1217 | } | |
1218 | ||
1219 | xpsr &= ~XPSR_SFPA; | |
1220 | if (env->v7m.secure && | |
1221 | (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { | |
1222 | xpsr |= XPSR_SFPA; | |
1223 | } | |
1224 | ||
1225 | frameptr -= framesize; | |
1226 | ||
1227 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
1228 | uint32_t limit = v7m_sp_limit(env); | |
1229 | ||
1230 | if (frameptr < limit) { | |
1231 | /* | |
1232 | * Stack limit failure: set SP to the limit value, and generate | |
1233 | * STKOF UsageFault. Stack pushes below the limit must not be | |
1234 | * performed. It is IMPDEF whether pushes above the limit are | |
1235 | * performed; we choose not to. | |
1236 | */ | |
1237 | qemu_log_mask(CPU_LOG_INT, | |
1238 | "...STKOF during stacking\n"); | |
1239 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; | |
1240 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
1241 | env->v7m.secure); | |
1242 | env->regs[13] = limit; | |
1243 | /* | |
1244 | * We won't try to perform any further memory accesses but | |
1245 | * we must continue through the following code to check for | |
1246 | * permission faults during FPU state preservation, and we | |
1247 | * must update FPCCR if lazy stacking is enabled. | |
1248 | */ | |
1249 | limitviol = true; | |
1250 | stacked_ok = false; | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | /* | |
1255 | * Write as much of the stack frame as we can. If we fail a stack | |
1256 | * write this will result in a derived exception being pended | |
1257 | * (which may be taken in preference to the one we started with | |
1258 | * if it has higher priority). | |
1259 | */ | |
1260 | stacked_ok = stacked_ok && | |
1261 | v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, STACK_NORMAL) && | |
1262 | v7m_stack_write(cpu, frameptr + 4, env->regs[1], | |
1263 | mmu_idx, STACK_NORMAL) && | |
1264 | v7m_stack_write(cpu, frameptr + 8, env->regs[2], | |
1265 | mmu_idx, STACK_NORMAL) && | |
1266 | v7m_stack_write(cpu, frameptr + 12, env->regs[3], | |
1267 | mmu_idx, STACK_NORMAL) && | |
1268 | v7m_stack_write(cpu, frameptr + 16, env->regs[12], | |
1269 | mmu_idx, STACK_NORMAL) && | |
1270 | v7m_stack_write(cpu, frameptr + 20, env->regs[14], | |
1271 | mmu_idx, STACK_NORMAL) && | |
1272 | v7m_stack_write(cpu, frameptr + 24, env->regs[15], | |
1273 | mmu_idx, STACK_NORMAL) && | |
1274 | v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, STACK_NORMAL); | |
1275 | ||
1276 | if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) { | |
1277 | /* FPU is active, try to save its registers */ | |
1278 | bool fpccr_s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; | |
1279 | bool lspact = env->v7m.fpccr[fpccr_s] & R_V7M_FPCCR_LSPACT_MASK; | |
1280 | ||
1281 | if (lspact && arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
1282 | qemu_log_mask(CPU_LOG_INT, | |
1283 | "...SecureFault because LSPACT and FPCA both set\n"); | |
1284 | env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; | |
1285 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
1286 | } else if (!env->v7m.secure && !nsacr_cp10) { | |
1287 | qemu_log_mask(CPU_LOG_INT, | |
1288 | "...Secure UsageFault with CFSR.NOCP because " | |
1289 | "NSACR.CP10 prevents stacking FP regs\n"); | |
1290 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S); | |
1291 | env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK; | |
1292 | } else { | |
1293 | if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) { | |
1294 | /* Lazy stacking disabled, save registers now */ | |
1295 | int i; | |
1296 | bool cpacr_pass = v7m_cpacr_pass(env, env->v7m.secure, | |
1297 | arm_current_el(env) != 0); | |
1298 | ||
1299 | if (stacked_ok && !cpacr_pass) { | |
1300 | /* | |
1301 | * Take UsageFault if CPACR forbids access. The pseudocode | |
1302 | * here does a full CheckCPEnabled() but we know the NSACR | |
1303 | * check can never fail as we have already handled that. | |
1304 | */ | |
1305 | qemu_log_mask(CPU_LOG_INT, | |
1306 | "...UsageFault with CFSR.NOCP because " | |
1307 | "CPACR.CP10 prevents stacking FP regs\n"); | |
1308 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
1309 | env->v7m.secure); | |
1310 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK; | |
1311 | stacked_ok = false; | |
1312 | } | |
1313 | ||
1314 | for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) { | |
1315 | uint64_t dn = *aa32_vfp_dreg(env, i / 2); | |
1316 | uint32_t faddr = frameptr + 0x20 + 4 * i; | |
1317 | uint32_t slo = extract64(dn, 0, 32); | |
1318 | uint32_t shi = extract64(dn, 32, 32); | |
1319 | ||
1320 | if (i >= 16) { | |
375256a8 | 1321 | faddr += 8; /* skip the slot for the FPSCR and VPR */ |
7aab5a8c PMD |
1322 | } |
1323 | stacked_ok = stacked_ok && | |
1324 | v7m_stack_write(cpu, faddr, slo, | |
1325 | mmu_idx, STACK_NORMAL) && | |
1326 | v7m_stack_write(cpu, faddr + 4, shi, | |
1327 | mmu_idx, STACK_NORMAL); | |
1328 | } | |
1329 | stacked_ok = stacked_ok && | |
1330 | v7m_stack_write(cpu, frameptr + 0x60, | |
1331 | vfp_get_fpscr(env), mmu_idx, STACK_NORMAL); | |
375256a8 PM |
1332 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1333 | stacked_ok = stacked_ok && | |
1334 | v7m_stack_write(cpu, frameptr + 0x64, | |
1335 | env->v7m.vpr, mmu_idx, STACK_NORMAL); | |
1336 | } | |
7aab5a8c PMD |
1337 | if (cpacr_pass) { |
1338 | for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) { | |
1339 | *aa32_vfp_dreg(env, i / 2) = 0; | |
1340 | } | |
1341 | vfp_set_fpscr(env, 0); | |
375256a8 PM |
1342 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1343 | env->v7m.vpr = 0; | |
1344 | } | |
7aab5a8c PMD |
1345 | } |
1346 | } else { | |
1347 | /* Lazy stacking enabled, save necessary info to stack later */ | |
1348 | v7m_update_fpccr(env, frameptr + 0x20, true); | |
1349 | } | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | /* | |
1354 | * If we broke a stack limit then SP was already updated earlier; | |
1355 | * otherwise we update SP regardless of whether any of the stack | |
1356 | * accesses failed or we took some other kind of fault. | |
1357 | */ | |
1358 | if (!limitviol) { | |
1359 | env->regs[13] = frameptr; | |
1360 | } | |
1361 | ||
1362 | return !stacked_ok; | |
1363 | } | |
1364 | ||
1365 | static void do_v7m_exception_exit(ARMCPU *cpu) | |
1366 | { | |
1367 | CPUARMState *env = &cpu->env; | |
1368 | uint32_t excret; | |
1369 | uint32_t xpsr, xpsr_mask; | |
1370 | bool ufault = false; | |
1371 | bool sfault = false; | |
1372 | bool return_to_sp_process; | |
1373 | bool return_to_handler; | |
1374 | bool rettobase = false; | |
1375 | bool exc_secure = false; | |
1376 | bool return_to_secure; | |
1377 | bool ftype; | |
0ae4f11e | 1378 | bool restore_s16_s31 = false; |
7aab5a8c PMD |
1379 | |
1380 | /* | |
1381 | * If we're not in Handler mode then jumps to magic exception-exit | |
1382 | * addresses don't have magic behaviour. However for the v8M | |
1383 | * security extensions the magic secure-function-return has to | |
1384 | * work in thread mode too, so to avoid doing an extra check in | |
1385 | * the generated code we allow exception-exit magic to also cause the | |
1386 | * internal exception and bring us here in thread mode. Correct code | |
1387 | * will never try to do this (the following insn fetch will always | |
1388 | * fault) so we the overhead of having taken an unnecessary exception | |
1389 | * doesn't matter. | |
1390 | */ | |
1391 | if (!arm_v7m_is_handler_mode(env)) { | |
1392 | return; | |
1393 | } | |
1394 | ||
1395 | /* | |
1396 | * In the spec pseudocode ExceptionReturn() is called directly | |
1397 | * from BXWritePC() and gets the full target PC value including | |
1398 | * bit zero. In QEMU's implementation we treat it as a normal | |
1399 | * jump-to-register (which is then caught later on), and so split | |
1400 | * the target value up between env->regs[15] and env->thumb in | |
1401 | * gen_bx(). Reconstitute it. | |
1402 | */ | |
1403 | excret = env->regs[15]; | |
1404 | if (env->thumb) { | |
1405 | excret |= 1; | |
1406 | } | |
1407 | ||
1408 | qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32 | |
1409 | " previous exception %d\n", | |
1410 | excret, env->v7m.exception); | |
1411 | ||
1412 | if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) { | |
1413 | qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception " | |
1414 | "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n", | |
1415 | excret); | |
1416 | } | |
1417 | ||
1418 | ftype = excret & R_V7M_EXCRET_FTYPE_MASK; | |
1419 | ||
7fbc6a40 | 1420 | if (!ftype && !cpu_isar_feature(aa32_vfp_simd, cpu)) { |
7aab5a8c PMD |
1421 | qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero FTYPE in exception " |
1422 | "exit PC value 0x%" PRIx32 " is UNPREDICTABLE " | |
1423 | "if FPU not present\n", | |
1424 | excret); | |
1425 | ftype = true; | |
1426 | } | |
1427 | ||
1428 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
1429 | /* | |
1430 | * EXC_RETURN.ES validation check (R_SMFL). We must do this before | |
1431 | * we pick which FAULTMASK to clear. | |
1432 | */ | |
1433 | if (!env->v7m.secure && | |
1434 | ((excret & R_V7M_EXCRET_ES_MASK) || | |
1435 | !(excret & R_V7M_EXCRET_DCRS_MASK))) { | |
1436 | sfault = 1; | |
1437 | /* For all other purposes, treat ES as 0 (R_HXSR) */ | |
1438 | excret &= ~R_V7M_EXCRET_ES_MASK; | |
1439 | } | |
1440 | exc_secure = excret & R_V7M_EXCRET_ES_MASK; | |
1441 | } | |
1442 | ||
1443 | if (env->v7m.exception != ARMV7M_EXCP_NMI) { | |
1444 | /* | |
1445 | * Auto-clear FAULTMASK on return from other than NMI. | |
1446 | * If the security extension is implemented then this only | |
1447 | * happens if the raw execution priority is >= 0; the | |
1448 | * value of the ES bit in the exception return value indicates | |
1449 | * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.) | |
1450 | */ | |
1451 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
1452 | if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) { | |
1453 | env->v7m.faultmask[exc_secure] = 0; | |
1454 | } | |
1455 | } else { | |
1456 | env->v7m.faultmask[M_REG_NS] = 0; | |
1457 | } | |
1458 | } | |
1459 | ||
1460 | switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception, | |
1461 | exc_secure)) { | |
1462 | case -1: | |
1463 | /* attempt to exit an exception that isn't active */ | |
1464 | ufault = true; | |
1465 | break; | |
1466 | case 0: | |
1467 | /* still an irq active now */ | |
1468 | break; | |
1469 | case 1: | |
1470 | /* | |
1471 | * We returned to base exception level, no nesting. | |
1472 | * (In the pseudocode this is written using "NestedActivation != 1" | |
1473 | * where we have 'rettobase == false'.) | |
1474 | */ | |
1475 | rettobase = true; | |
1476 | break; | |
1477 | default: | |
1478 | g_assert_not_reached(); | |
1479 | } | |
1480 | ||
1481 | return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK); | |
1482 | return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK; | |
1483 | return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) && | |
1484 | (excret & R_V7M_EXCRET_S_MASK); | |
1485 | ||
1486 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
1487 | if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
1488 | /* | |
1489 | * UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP); | |
1490 | * we choose to take the UsageFault. | |
1491 | */ | |
1492 | if ((excret & R_V7M_EXCRET_S_MASK) || | |
1493 | (excret & R_V7M_EXCRET_ES_MASK) || | |
1494 | !(excret & R_V7M_EXCRET_DCRS_MASK)) { | |
1495 | ufault = true; | |
1496 | } | |
1497 | } | |
1498 | if (excret & R_V7M_EXCRET_RES0_MASK) { | |
1499 | ufault = true; | |
1500 | } | |
1501 | } else { | |
1502 | /* For v7M we only recognize certain combinations of the low bits */ | |
1503 | switch (excret & 0xf) { | |
1504 | case 1: /* Return to Handler */ | |
1505 | break; | |
1506 | case 13: /* Return to Thread using Process stack */ | |
1507 | case 9: /* Return to Thread using Main stack */ | |
1508 | /* | |
1509 | * We only need to check NONBASETHRDENA for v7M, because in | |
1510 | * v8M this bit does not exist (it is RES1). | |
1511 | */ | |
1512 | if (!rettobase && | |
1513 | !(env->v7m.ccr[env->v7m.secure] & | |
1514 | R_V7M_CCR_NONBASETHRDENA_MASK)) { | |
1515 | ufault = true; | |
1516 | } | |
1517 | break; | |
1518 | default: | |
1519 | ufault = true; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | /* | |
1524 | * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in | |
1525 | * Handler mode (and will be until we write the new XPSR.Interrupt | |
1526 | * field) this does not switch around the current stack pointer. | |
1527 | * We must do this before we do any kind of tailchaining, including | |
1528 | * for the derived exceptions on integrity check failures, or we will | |
1529 | * give the guest an incorrect EXCRET.SPSEL value on exception entry. | |
1530 | */ | |
1531 | write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure); | |
1532 | ||
1533 | /* | |
1534 | * Clear scratch FP values left in caller saved registers; this | |
1535 | * must happen before any kind of tail chaining. | |
1536 | */ | |
1537 | if ((env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_CLRONRET_MASK) && | |
1538 | (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) { | |
1539 | if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) { | |
1540 | env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; | |
1541 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
1542 | qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " | |
1543 | "stackframe: error during lazy state deactivation\n"); | |
1544 | v7m_exception_taken(cpu, excret, true, false); | |
1545 | return; | |
1546 | } else { | |
3423fbf1 PM |
1547 | if (arm_feature(env, ARM_FEATURE_V8_1M)) { |
1548 | /* v8.1M adds this NOCP check */ | |
1549 | bool nsacr_pass = exc_secure || | |
1550 | extract32(env->v7m.nsacr, 10, 1); | |
1551 | bool cpacr_pass = v7m_cpacr_pass(env, exc_secure, true); | |
1552 | if (!nsacr_pass) { | |
1553 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, true); | |
1554 | env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK; | |
1555 | qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " | |
1556 | "stackframe: NSACR prevents clearing FPU registers\n"); | |
1557 | v7m_exception_taken(cpu, excret, true, false); | |
0c317eb3 | 1558 | return; |
3423fbf1 PM |
1559 | } else if (!cpacr_pass) { |
1560 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
1561 | exc_secure); | |
1562 | env->v7m.cfsr[exc_secure] |= R_V7M_CFSR_NOCP_MASK; | |
1563 | qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " | |
1564 | "stackframe: CPACR prevents clearing FPU registers\n"); | |
1565 | v7m_exception_taken(cpu, excret, true, false); | |
0c317eb3 | 1566 | return; |
3423fbf1 PM |
1567 | } |
1568 | } | |
375256a8 | 1569 | /* Clear s0..s15, FPSCR and VPR */ |
7aab5a8c PMD |
1570 | int i; |
1571 | ||
1572 | for (i = 0; i < 16; i += 2) { | |
1573 | *aa32_vfp_dreg(env, i / 2) = 0; | |
1574 | } | |
1575 | vfp_set_fpscr(env, 0); | |
375256a8 PM |
1576 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1577 | env->v7m.vpr = 0; | |
1578 | } | |
7aab5a8c PMD |
1579 | } |
1580 | } | |
1581 | ||
1582 | if (sfault) { | |
1583 | env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK; | |
1584 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
1585 | qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " | |
1586 | "stackframe: failed EXC_RETURN.ES validity check\n"); | |
1587 | v7m_exception_taken(cpu, excret, true, false); | |
1588 | return; | |
1589 | } | |
1590 | ||
1591 | if (ufault) { | |
1592 | /* | |
1593 | * Bad exception return: instead of popping the exception | |
1594 | * stack, directly take a usage fault on the current stack. | |
1595 | */ | |
1596 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; | |
1597 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); | |
1598 | qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " | |
1599 | "stackframe: failed exception return integrity check\n"); | |
1600 | v7m_exception_taken(cpu, excret, true, false); | |
1601 | return; | |
1602 | } | |
1603 | ||
1604 | /* | |
1605 | * Tailchaining: if there is currently a pending exception that | |
1606 | * is high enough priority to preempt execution at the level we're | |
1607 | * about to return to, then just directly take that exception now, | |
1608 | * avoiding an unstack-and-then-stack. Note that now we have | |
1609 | * deactivated the previous exception by calling armv7m_nvic_complete_irq() | |
1610 | * our current execution priority is already the execution priority we are | |
1611 | * returning to -- none of the state we would unstack or set based on | |
1612 | * the EXCRET value affects it. | |
1613 | */ | |
1614 | if (armv7m_nvic_can_take_pending_exception(env->nvic)) { | |
1615 | qemu_log_mask(CPU_LOG_INT, "...tailchaining to pending exception\n"); | |
1616 | v7m_exception_taken(cpu, excret, true, false); | |
1617 | return; | |
1618 | } | |
1619 | ||
1620 | switch_v7m_security_state(env, return_to_secure); | |
1621 | ||
1622 | { | |
1623 | /* | |
1624 | * The stack pointer we should be reading the exception frame from | |
1625 | * depends on bits in the magic exception return type value (and | |
1626 | * for v8M isn't necessarily the stack pointer we will eventually | |
1627 | * end up resuming execution with). Get a pointer to the location | |
1628 | * in the CPU state struct where the SP we need is currently being | |
1629 | * stored; we will use and modify it in place. | |
1630 | * We use this limited C variable scope so we don't accidentally | |
1631 | * use 'frame_sp_p' after we do something that makes it invalid. | |
1632 | */ | |
659f042b | 1633 | bool spsel = env->v7m.control[return_to_secure] & R_V7M_CONTROL_SPSEL_MASK; |
7aab5a8c PMD |
1634 | uint32_t *frame_sp_p = get_v7m_sp_ptr(env, |
1635 | return_to_secure, | |
1636 | !return_to_handler, | |
659f042b | 1637 | spsel); |
7aab5a8c PMD |
1638 | uint32_t frameptr = *frame_sp_p; |
1639 | bool pop_ok = true; | |
1640 | ARMMMUIdx mmu_idx; | |
1641 | bool return_to_priv = return_to_handler || | |
1642 | !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK); | |
1643 | ||
1644 | mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure, | |
1645 | return_to_priv); | |
1646 | ||
1647 | if (!QEMU_IS_ALIGNED(frameptr, 8) && | |
1648 | arm_feature(env, ARM_FEATURE_V8)) { | |
1649 | qemu_log_mask(LOG_GUEST_ERROR, | |
1650 | "M profile exception return with non-8-aligned SP " | |
1651 | "for destination state is UNPREDICTABLE\n"); | |
1652 | } | |
1653 | ||
1654 | /* Do we need to pop callee-saved registers? */ | |
1655 | if (return_to_secure && | |
1656 | ((excret & R_V7M_EXCRET_ES_MASK) == 0 || | |
1657 | (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) { | |
1658 | uint32_t actual_sig; | |
1659 | ||
1660 | pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx); | |
1661 | ||
1662 | if (pop_ok && v7m_integrity_sig(env, excret) != actual_sig) { | |
1663 | /* Take a SecureFault on the current stack */ | |
1664 | env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK; | |
1665 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
1666 | qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " | |
1667 | "stackframe: failed exception return integrity " | |
1668 | "signature check\n"); | |
1669 | v7m_exception_taken(cpu, excret, true, false); | |
1670 | return; | |
1671 | } | |
1672 | ||
1673 | pop_ok = pop_ok && | |
1674 | v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) && | |
1675 | v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) && | |
1676 | v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) && | |
1677 | v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) && | |
1678 | v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) && | |
1679 | v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) && | |
1680 | v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) && | |
1681 | v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx); | |
1682 | ||
1683 | frameptr += 0x28; | |
1684 | } | |
1685 | ||
1686 | /* Pop registers */ | |
1687 | pop_ok = pop_ok && | |
1688 | v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) && | |
1689 | v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) && | |
1690 | v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) && | |
1691 | v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) && | |
1692 | v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) && | |
1693 | v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) && | |
1694 | v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) && | |
1695 | v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx); | |
1696 | ||
1697 | if (!pop_ok) { | |
1698 | /* | |
1699 | * v7m_stack_read() pended a fault, so take it (as a tail | |
1700 | * chained exception on the same stack frame) | |
1701 | */ | |
1702 | qemu_log_mask(CPU_LOG_INT, "...derived exception on unstacking\n"); | |
1703 | v7m_exception_taken(cpu, excret, true, false); | |
1704 | return; | |
1705 | } | |
1706 | ||
1707 | /* | |
1708 | * Returning from an exception with a PC with bit 0 set is defined | |
1709 | * behaviour on v8M (bit 0 is ignored), but for v7M it was specified | |
1710 | * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore | |
1711 | * the lsbit, and there are several RTOSes out there which incorrectly | |
1712 | * assume the r15 in the stack frame should be a Thumb-style "lsbit | |
1713 | * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but | |
1714 | * complain about the badly behaved guest. | |
1715 | */ | |
1716 | if (env->regs[15] & 1) { | |
1717 | env->regs[15] &= ~1U; | |
1718 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
1719 | qemu_log_mask(LOG_GUEST_ERROR, | |
1720 | "M profile return from interrupt with misaligned " | |
1721 | "PC is UNPREDICTABLE on v7M\n"); | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
1726 | /* | |
1727 | * For v8M we have to check whether the xPSR exception field | |
1728 | * matches the EXCRET value for return to handler/thread | |
1729 | * before we commit to changing the SP and xPSR. | |
1730 | */ | |
1731 | bool will_be_handler = (xpsr & XPSR_EXCP) != 0; | |
1732 | if (return_to_handler != will_be_handler) { | |
1733 | /* | |
1734 | * Take an INVPC UsageFault on the current stack. | |
1735 | * By this point we will have switched to the security state | |
1736 | * for the background state, so this UsageFault will target | |
1737 | * that state. | |
1738 | */ | |
1739 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
1740 | env->v7m.secure); | |
1741 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; | |
1742 | qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " | |
1743 | "stackframe: failed exception return integrity " | |
1744 | "check\n"); | |
1745 | v7m_exception_taken(cpu, excret, true, false); | |
1746 | return; | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | if (!ftype) { | |
1751 | /* FP present and we need to handle it */ | |
1752 | if (!return_to_secure && | |
1753 | (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK)) { | |
1754 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
1755 | env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; | |
1756 | qemu_log_mask(CPU_LOG_INT, | |
1757 | "...taking SecureFault on existing stackframe: " | |
1758 | "Secure LSPACT set but exception return is " | |
1759 | "not to secure state\n"); | |
1760 | v7m_exception_taken(cpu, excret, true, false); | |
1761 | return; | |
1762 | } | |
1763 | ||
1764 | restore_s16_s31 = return_to_secure && | |
1765 | (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK); | |
1766 | ||
1767 | if (env->v7m.fpccr[return_to_secure] & R_V7M_FPCCR_LSPACT_MASK) { | |
1768 | /* State in FPU is still valid, just clear LSPACT */ | |
1769 | env->v7m.fpccr[return_to_secure] &= ~R_V7M_FPCCR_LSPACT_MASK; | |
1770 | } else { | |
1771 | int i; | |
1772 | uint32_t fpscr; | |
1773 | bool cpacr_pass, nsacr_pass; | |
1774 | ||
1775 | cpacr_pass = v7m_cpacr_pass(env, return_to_secure, | |
1776 | return_to_priv); | |
1777 | nsacr_pass = return_to_secure || | |
1778 | extract32(env->v7m.nsacr, 10, 1); | |
1779 | ||
1780 | if (!cpacr_pass) { | |
1781 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
1782 | return_to_secure); | |
1783 | env->v7m.cfsr[return_to_secure] |= R_V7M_CFSR_NOCP_MASK; | |
1784 | qemu_log_mask(CPU_LOG_INT, | |
1785 | "...taking UsageFault on existing " | |
1786 | "stackframe: CPACR.CP10 prevents unstacking " | |
1787 | "FP regs\n"); | |
1788 | v7m_exception_taken(cpu, excret, true, false); | |
1789 | return; | |
1790 | } else if (!nsacr_pass) { | |
1791 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, true); | |
1792 | env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_INVPC_MASK; | |
1793 | qemu_log_mask(CPU_LOG_INT, | |
1794 | "...taking Secure UsageFault on existing " | |
1795 | "stackframe: NSACR.CP10 prevents unstacking " | |
1796 | "FP regs\n"); | |
1797 | v7m_exception_taken(cpu, excret, true, false); | |
1798 | return; | |
1799 | } | |
1800 | ||
1801 | for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) { | |
1802 | uint32_t slo, shi; | |
1803 | uint64_t dn; | |
1804 | uint32_t faddr = frameptr + 0x20 + 4 * i; | |
1805 | ||
1806 | if (i >= 16) { | |
375256a8 | 1807 | faddr += 8; /* Skip the slot for the FPSCR and VPR */ |
7aab5a8c PMD |
1808 | } |
1809 | ||
1810 | pop_ok = pop_ok && | |
1811 | v7m_stack_read(cpu, &slo, faddr, mmu_idx) && | |
1812 | v7m_stack_read(cpu, &shi, faddr + 4, mmu_idx); | |
1813 | ||
1814 | if (!pop_ok) { | |
1815 | break; | |
1816 | } | |
1817 | ||
1818 | dn = (uint64_t)shi << 32 | slo; | |
1819 | *aa32_vfp_dreg(env, i / 2) = dn; | |
1820 | } | |
1821 | pop_ok = pop_ok && | |
1822 | v7m_stack_read(cpu, &fpscr, frameptr + 0x60, mmu_idx); | |
1823 | if (pop_ok) { | |
1824 | vfp_set_fpscr(env, fpscr); | |
1825 | } | |
375256a8 PM |
1826 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1827 | pop_ok = pop_ok && | |
1828 | v7m_stack_read(cpu, &env->v7m.vpr, | |
1829 | frameptr + 0x64, mmu_idx); | |
1830 | } | |
7aab5a8c PMD |
1831 | if (!pop_ok) { |
1832 | /* | |
1833 | * These regs are 0 if security extension present; | |
1834 | * otherwise merely UNKNOWN. We zero always. | |
1835 | */ | |
1836 | for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) { | |
1837 | *aa32_vfp_dreg(env, i / 2) = 0; | |
1838 | } | |
1839 | vfp_set_fpscr(env, 0); | |
375256a8 PM |
1840 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1841 | env->v7m.vpr = 0; | |
1842 | } | |
7aab5a8c PMD |
1843 | } |
1844 | } | |
1845 | } | |
1846 | env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S], | |
1847 | V7M_CONTROL, FPCA, !ftype); | |
1848 | ||
1849 | /* Commit to consuming the stack frame */ | |
1850 | frameptr += 0x20; | |
1851 | if (!ftype) { | |
1852 | frameptr += 0x48; | |
1853 | if (restore_s16_s31) { | |
1854 | frameptr += 0x40; | |
1855 | } | |
1856 | } | |
1857 | /* | |
1858 | * Undo stack alignment (the SPREALIGN bit indicates that the original | |
1859 | * pre-exception SP was not 8-aligned and we added a padding word to | |
1860 | * align it, so we undo this by ORing in the bit that increases it | |
1861 | * from the current 8-aligned value to the 8-unaligned value. (Adding 4 | |
1862 | * would work too but a logical OR is how the pseudocode specifies it.) | |
1863 | */ | |
1864 | if (xpsr & XPSR_SPREALIGN) { | |
1865 | frameptr |= 4; | |
1866 | } | |
1867 | *frame_sp_p = frameptr; | |
1868 | } | |
1869 | ||
1870 | xpsr_mask = ~(XPSR_SPREALIGN | XPSR_SFPA); | |
1871 | if (!arm_feature(env, ARM_FEATURE_THUMB_DSP)) { | |
1872 | xpsr_mask &= ~XPSR_GE; | |
1873 | } | |
1874 | /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */ | |
1875 | xpsr_write(env, xpsr, xpsr_mask); | |
1876 | ||
1877 | if (env->v7m.secure) { | |
1878 | bool sfpa = xpsr & XPSR_SFPA; | |
1879 | ||
1880 | env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S], | |
1881 | V7M_CONTROL, SFPA, sfpa); | |
1882 | } | |
1883 | ||
1884 | /* | |
1885 | * The restored xPSR exception field will be zero if we're | |
1886 | * resuming in Thread mode. If that doesn't match what the | |
1887 | * exception return excret specified then this is a UsageFault. | |
1888 | * v7M requires we make this check here; v8M did it earlier. | |
1889 | */ | |
1890 | if (return_to_handler != arm_v7m_is_handler_mode(env)) { | |
1891 | /* | |
1892 | * Take an INVPC UsageFault by pushing the stack again; | |
1893 | * we know we're v7M so this is never a Secure UsageFault. | |
1894 | */ | |
1895 | bool ignore_stackfaults; | |
1896 | ||
1897 | assert(!arm_feature(env, ARM_FEATURE_V8)); | |
1898 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false); | |
1899 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; | |
1900 | ignore_stackfaults = v7m_push_stack(cpu); | |
1901 | qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: " | |
1902 | "failed exception return integrity check\n"); | |
1903 | v7m_exception_taken(cpu, excret, false, ignore_stackfaults); | |
1904 | return; | |
1905 | } | |
1906 | ||
1907 | /* Otherwise, we have a successful exception exit. */ | |
1908 | arm_clear_exclusive(env); | |
873be7b6 | 1909 | arm_rebuild_hflags(env); |
7aab5a8c PMD |
1910 | qemu_log_mask(CPU_LOG_INT, "...successful exception return\n"); |
1911 | } | |
1912 | ||
1913 | static bool do_v7m_function_return(ARMCPU *cpu) | |
1914 | { | |
1915 | /* | |
1916 | * v8M security extensions magic function return. | |
1917 | * We may either: | |
1918 | * (1) throw an exception (longjump) | |
1919 | * (2) return true if we successfully handled the function return | |
1920 | * (3) return false if we failed a consistency check and have | |
1921 | * pended a UsageFault that needs to be taken now | |
1922 | * | |
1923 | * At this point the magic return value is split between env->regs[15] | |
1924 | * and env->thumb. We don't bother to reconstitute it because we don't | |
1925 | * need it (all values are handled the same way). | |
1926 | */ | |
1927 | CPUARMState *env = &cpu->env; | |
1928 | uint32_t newpc, newpsr, newpsr_exc; | |
1929 | ||
1930 | qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n"); | |
1931 | ||
1932 | { | |
1933 | bool threadmode, spsel; | |
9002ffcb | 1934 | MemOpIdx oi; |
7aab5a8c PMD |
1935 | ARMMMUIdx mmu_idx; |
1936 | uint32_t *frame_sp_p; | |
1937 | uint32_t frameptr; | |
1938 | ||
1939 | /* Pull the return address and IPSR from the Secure stack */ | |
1940 | threadmode = !arm_v7m_is_handler_mode(env); | |
1941 | spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK; | |
1942 | ||
1943 | frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel); | |
1944 | frameptr = *frame_sp_p; | |
1945 | ||
1946 | /* | |
1947 | * These loads may throw an exception (for MPU faults). We want to | |
1948 | * do them as secure, so work out what MMU index that is. | |
1949 | */ | |
1950 | mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); | |
1a2eaf9e RH |
1951 | oi = make_memop_idx(MO_LEUL, arm_to_core_mmu_idx(mmu_idx)); |
1952 | newpc = cpu_ldl_le_mmu(env, frameptr, oi, 0); | |
1953 | newpsr = cpu_ldl_le_mmu(env, frameptr + 4, oi, 0); | |
7aab5a8c PMD |
1954 | |
1955 | /* Consistency checks on new IPSR */ | |
1956 | newpsr_exc = newpsr & XPSR_EXCP; | |
1957 | if (!((env->v7m.exception == 0 && newpsr_exc == 0) || | |
1958 | (env->v7m.exception == 1 && newpsr_exc != 0))) { | |
1959 | /* Pend the fault and tell our caller to take it */ | |
1960 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; | |
1961 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
1962 | env->v7m.secure); | |
1963 | qemu_log_mask(CPU_LOG_INT, | |
1964 | "...taking INVPC UsageFault: " | |
1965 | "IPSR consistency check failed\n"); | |
1966 | return false; | |
1967 | } | |
1968 | ||
1969 | *frame_sp_p = frameptr + 8; | |
1970 | } | |
1971 | ||
1972 | /* This invalidates frame_sp_p */ | |
1973 | switch_v7m_security_state(env, true); | |
1974 | env->v7m.exception = newpsr_exc; | |
1975 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; | |
1976 | if (newpsr & XPSR_SFPA) { | |
1977 | env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK; | |
1978 | } | |
1979 | xpsr_write(env, 0, XPSR_IT); | |
1980 | env->thumb = newpc & 1; | |
1981 | env->regs[15] = newpc & ~1; | |
873be7b6 | 1982 | arm_rebuild_hflags(env); |
7aab5a8c PMD |
1983 | |
1984 | qemu_log_mask(CPU_LOG_INT, "...function return successful\n"); | |
1985 | return true; | |
1986 | } | |
1987 | ||
1988 | static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx, | |
1989 | uint32_t addr, uint16_t *insn) | |
1990 | { | |
1991 | /* | |
1992 | * Load a 16-bit portion of a v7M instruction, returning true on success, | |
1993 | * or false on failure (in which case we will have pended the appropriate | |
1994 | * exception). | |
1995 | * We need to do the instruction fetch's MPU and SAU checks | |
1996 | * like this because there is no MMU index that would allow | |
1997 | * doing the load with a single function call. Instead we must | |
1998 | * first check that the security attributes permit the load | |
1999 | * and that they don't mismatch on the two halves of the instruction, | |
2000 | * and then we do the load as a secure load (ie using the security | |
2001 | * attributes of the address, not the CPU, as architecturally required). | |
2002 | */ | |
2003 | CPUState *cs = CPU(cpu); | |
2004 | CPUARMState *env = &cpu->env; | |
2005 | V8M_SAttributes sattrs = {}; | |
2006 | MemTxAttrs attrs = {}; | |
2007 | ARMMMUFaultInfo fi = {}; | |
7e98e21c | 2008 | ARMCacheAttrs cacheattrs = {}; |
7aab5a8c PMD |
2009 | MemTxResult txres; |
2010 | target_ulong page_size; | |
2011 | hwaddr physaddr; | |
2012 | int prot; | |
2013 | ||
2014 | v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs); | |
2015 | if (!sattrs.nsc || sattrs.ns) { | |
2016 | /* | |
2017 | * This must be the second half of the insn, and it straddles a | |
2018 | * region boundary with the second half not being S&NSC. | |
2019 | */ | |
2020 | env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; | |
2021 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
2022 | qemu_log_mask(CPU_LOG_INT, | |
2023 | "...really SecureFault with SFSR.INVEP\n"); | |
2024 | return false; | |
2025 | } | |
7e98e21c RH |
2026 | if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx, &physaddr, |
2027 | &attrs, &prot, &page_size, &fi, &cacheattrs)) { | |
7aab5a8c PMD |
2028 | /* the MPU lookup failed */ |
2029 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; | |
2030 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure); | |
2031 | qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n"); | |
2032 | return false; | |
2033 | } | |
2034 | *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr, | |
2035 | attrs, &txres); | |
2036 | if (txres != MEMTX_OK) { | |
2037 | env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; | |
2038 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); | |
2039 | qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n"); | |
2040 | return false; | |
2041 | } | |
2042 | return true; | |
2043 | } | |
2044 | ||
7f484147 PM |
2045 | static bool v7m_read_sg_stack_word(ARMCPU *cpu, ARMMMUIdx mmu_idx, |
2046 | uint32_t addr, uint32_t *spdata) | |
2047 | { | |
2048 | /* | |
2049 | * Read a word of data from the stack for the SG instruction, | |
2050 | * writing the value into *spdata. If the load succeeds, return | |
2051 | * true; otherwise pend an appropriate exception and return false. | |
2052 | * (We can't use data load helpers here that throw an exception | |
2053 | * because of the context we're called in, which is halfway through | |
2054 | * arm_v7m_cpu_do_interrupt().) | |
2055 | */ | |
2056 | CPUState *cs = CPU(cpu); | |
2057 | CPUARMState *env = &cpu->env; | |
2058 | MemTxAttrs attrs = {}; | |
2059 | MemTxResult txres; | |
2060 | target_ulong page_size; | |
2061 | hwaddr physaddr; | |
2062 | int prot; | |
2063 | ARMMMUFaultInfo fi = {}; | |
2064 | ARMCacheAttrs cacheattrs = {}; | |
2065 | uint32_t value; | |
2066 | ||
2067 | if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr, | |
2068 | &attrs, &prot, &page_size, &fi, &cacheattrs)) { | |
2069 | /* MPU/SAU lookup failed */ | |
2070 | if (fi.type == ARMFault_QEMU_SFault) { | |
2071 | qemu_log_mask(CPU_LOG_INT, | |
2072 | "...SecureFault during stack word read\n"); | |
2073 | env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; | |
2074 | env->v7m.sfar = addr; | |
2075 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
2076 | } else { | |
2077 | qemu_log_mask(CPU_LOG_INT, | |
2078 | "...MemManageFault during stack word read\n"); | |
2079 | env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_DACCVIOL_MASK | | |
2080 | R_V7M_CFSR_MMARVALID_MASK; | |
2081 | env->v7m.mmfar[M_REG_S] = addr; | |
2082 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, false); | |
2083 | } | |
2084 | return false; | |
2085 | } | |
2086 | value = address_space_ldl(arm_addressspace(cs, attrs), physaddr, | |
2087 | attrs, &txres); | |
2088 | if (txres != MEMTX_OK) { | |
2089 | /* BusFault trying to read the data */ | |
2090 | qemu_log_mask(CPU_LOG_INT, | |
2091 | "...BusFault during stack word read\n"); | |
2092 | env->v7m.cfsr[M_REG_NS] |= | |
2093 | (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK); | |
2094 | env->v7m.bfar = addr; | |
2095 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); | |
2096 | return false; | |
2097 | } | |
2098 | ||
2099 | *spdata = value; | |
2100 | return true; | |
2101 | } | |
2102 | ||
7aab5a8c PMD |
2103 | static bool v7m_handle_execute_nsc(ARMCPU *cpu) |
2104 | { | |
2105 | /* | |
2106 | * Check whether this attempt to execute code in a Secure & NS-Callable | |
2107 | * memory region is for an SG instruction; if so, then emulate the | |
2108 | * effect of the SG instruction and return true. Otherwise pend | |
2109 | * the correct kind of exception and return false. | |
2110 | */ | |
2111 | CPUARMState *env = &cpu->env; | |
2112 | ARMMMUIdx mmu_idx; | |
2113 | uint16_t insn; | |
2114 | ||
2115 | /* | |
2116 | * We should never get here unless get_phys_addr_pmsav8() caused | |
2117 | * an exception for NS executing in S&NSC memory. | |
2118 | */ | |
2119 | assert(!env->v7m.secure); | |
2120 | assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); | |
2121 | ||
2122 | /* We want to do the MPU lookup as secure; work out what mmu_idx that is */ | |
2123 | mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); | |
2124 | ||
2125 | if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) { | |
2126 | return false; | |
2127 | } | |
2128 | ||
2129 | if (!env->thumb) { | |
2130 | goto gen_invep; | |
2131 | } | |
2132 | ||
2133 | if (insn != 0xe97f) { | |
2134 | /* | |
2135 | * Not an SG instruction first half (we choose the IMPDEF | |
2136 | * early-SG-check option). | |
2137 | */ | |
2138 | goto gen_invep; | |
2139 | } | |
2140 | ||
2141 | if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) { | |
2142 | return false; | |
2143 | } | |
2144 | ||
2145 | if (insn != 0xe97f) { | |
2146 | /* | |
2147 | * Not an SG instruction second half (yes, both halves of the SG | |
2148 | * insn have the same hex value) | |
2149 | */ | |
2150 | goto gen_invep; | |
2151 | } | |
2152 | ||
2153 | /* | |
2154 | * OK, we have confirmed that we really have an SG instruction. | |
2155 | * We know we're NS in S memory so don't need to repeat those checks. | |
2156 | */ | |
2157 | qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32 | |
2158 | ", executing it\n", env->regs[15]); | |
7f484147 PM |
2159 | |
2160 | if (cpu_isar_feature(aa32_m_sec_state, cpu) && | |
2161 | !arm_v7m_is_handler_mode(env)) { | |
2162 | /* | |
2163 | * v8.1M exception stack frame integrity check. Note that we | |
2164 | * must perform the memory access even if CCR_S.TRD is zero | |
2165 | * and we aren't going to check what the data loaded is. | |
2166 | */ | |
2167 | uint32_t spdata, sp; | |
2168 | ||
2169 | /* | |
2170 | * We know we are currently NS, so the S stack pointers must be | |
2171 | * in other_ss_{psp,msp}, not in regs[13]/other_sp. | |
2172 | */ | |
2173 | sp = v7m_using_psp(env) ? env->v7m.other_ss_psp : env->v7m.other_ss_msp; | |
2174 | if (!v7m_read_sg_stack_word(cpu, mmu_idx, sp, &spdata)) { | |
2175 | /* Stack access failed and an exception has been pended */ | |
2176 | return false; | |
2177 | } | |
2178 | ||
2179 | if (env->v7m.ccr[M_REG_S] & R_V7M_CCR_TRD_MASK) { | |
2180 | if (((spdata & ~1) == 0xfefa125a) || | |
2181 | !(env->v7m.control[M_REG_S] & 1)) { | |
2182 | goto gen_invep; | |
2183 | } | |
2184 | } | |
2185 | } | |
2186 | ||
7aab5a8c PMD |
2187 | env->regs[14] &= ~1; |
2188 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; | |
2189 | switch_v7m_security_state(env, true); | |
2190 | xpsr_write(env, 0, XPSR_IT); | |
2191 | env->regs[15] += 4; | |
873be7b6 | 2192 | arm_rebuild_hflags(env); |
7aab5a8c PMD |
2193 | return true; |
2194 | ||
2195 | gen_invep: | |
2196 | env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; | |
2197 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
2198 | qemu_log_mask(CPU_LOG_INT, | |
2199 | "...really SecureFault with SFSR.INVEP\n"); | |
2200 | return false; | |
2201 | } | |
2202 | ||
2203 | void arm_v7m_cpu_do_interrupt(CPUState *cs) | |
2204 | { | |
2205 | ARMCPU *cpu = ARM_CPU(cs); | |
2206 | CPUARMState *env = &cpu->env; | |
2207 | uint32_t lr; | |
2208 | bool ignore_stackfaults; | |
2209 | ||
fc6177af | 2210 | arm_log_exception(cs); |
7aab5a8c PMD |
2211 | |
2212 | /* | |
2213 | * For exceptions we just mark as pending on the NVIC, and let that | |
2214 | * handle it. | |
2215 | */ | |
2216 | switch (cs->exception_index) { | |
2217 | case EXCP_UDEF: | |
2218 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); | |
2219 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK; | |
2220 | break; | |
2221 | case EXCP_NOCP: | |
2222 | { | |
2223 | /* | |
2224 | * NOCP might be directed to something other than the current | |
2225 | * security state if this fault is because of NSACR; we indicate | |
2226 | * the target security state using exception.target_el. | |
2227 | */ | |
2228 | int target_secstate; | |
2229 | ||
2230 | if (env->exception.target_el == 3) { | |
2231 | target_secstate = M_REG_S; | |
2232 | } else { | |
2233 | target_secstate = env->v7m.secure; | |
2234 | } | |
2235 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, target_secstate); | |
2236 | env->v7m.cfsr[target_secstate] |= R_V7M_CFSR_NOCP_MASK; | |
2237 | break; | |
2238 | } | |
2239 | case EXCP_INVSTATE: | |
2240 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); | |
2241 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK; | |
2242 | break; | |
2243 | case EXCP_STKOF: | |
2244 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); | |
2245 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; | |
2246 | break; | |
2247 | case EXCP_LSERR: | |
2248 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
2249 | env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; | |
2250 | break; | |
2251 | case EXCP_UNALIGNED: | |
d4f68839 | 2252 | /* Unaligned faults reported by M-profile aware code */ |
7aab5a8c PMD |
2253 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); |
2254 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNALIGNED_MASK; | |
2255 | break; | |
e5346292 PM |
2256 | case EXCP_DIVBYZERO: |
2257 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); | |
2258 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_DIVBYZERO_MASK; | |
2259 | break; | |
7aab5a8c PMD |
2260 | case EXCP_SWI: |
2261 | /* The PC already points to the next instruction. */ | |
2262 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure); | |
2263 | break; | |
2264 | case EXCP_PREFETCH_ABORT: | |
2265 | case EXCP_DATA_ABORT: | |
2266 | /* | |
2267 | * Note that for M profile we don't have a guest facing FSR, but | |
2268 | * the env->exception.fsr will be populated by the code that | |
2269 | * raises the fault, in the A profile short-descriptor format. | |
2270 | */ | |
2271 | switch (env->exception.fsr & 0xf) { | |
2272 | case M_FAKE_FSR_NSC_EXEC: | |
2273 | /* | |
2274 | * Exception generated when we try to execute code at an address | |
2275 | * which is marked as Secure & Non-Secure Callable and the CPU | |
2276 | * is in the Non-Secure state. The only instruction which can | |
2277 | * be executed like this is SG (and that only if both halves of | |
2278 | * the SG instruction have the same security attributes.) | |
2279 | * Everything else must generate an INVEP SecureFault, so we | |
2280 | * emulate the SG instruction here. | |
2281 | */ | |
2282 | if (v7m_handle_execute_nsc(cpu)) { | |
2283 | return; | |
2284 | } | |
2285 | break; | |
2286 | case M_FAKE_FSR_SFAULT: | |
2287 | /* | |
2288 | * Various flavours of SecureFault for attempts to execute or | |
2289 | * access data in the wrong security state. | |
2290 | */ | |
2291 | switch (cs->exception_index) { | |
2292 | case EXCP_PREFETCH_ABORT: | |
2293 | if (env->v7m.secure) { | |
2294 | env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK; | |
2295 | qemu_log_mask(CPU_LOG_INT, | |
2296 | "...really SecureFault with SFSR.INVTRAN\n"); | |
2297 | } else { | |
2298 | env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; | |
2299 | qemu_log_mask(CPU_LOG_INT, | |
2300 | "...really SecureFault with SFSR.INVEP\n"); | |
2301 | } | |
2302 | break; | |
2303 | case EXCP_DATA_ABORT: | |
2304 | /* This must be an NS access to S memory */ | |
2305 | env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; | |
2306 | qemu_log_mask(CPU_LOG_INT, | |
2307 | "...really SecureFault with SFSR.AUVIOL\n"); | |
2308 | break; | |
2309 | } | |
2310 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); | |
2311 | break; | |
2312 | case 0x8: /* External Abort */ | |
2313 | switch (cs->exception_index) { | |
2314 | case EXCP_PREFETCH_ABORT: | |
2315 | env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; | |
2316 | qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n"); | |
2317 | break; | |
2318 | case EXCP_DATA_ABORT: | |
2319 | env->v7m.cfsr[M_REG_NS] |= | |
2320 | (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK); | |
2321 | env->v7m.bfar = env->exception.vaddress; | |
2322 | qemu_log_mask(CPU_LOG_INT, | |
2323 | "...with CFSR.PRECISERR and BFAR 0x%x\n", | |
2324 | env->v7m.bfar); | |
2325 | break; | |
2326 | } | |
2327 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); | |
2328 | break; | |
d4f68839 PM |
2329 | case 0x1: /* Alignment fault reported by generic code */ |
2330 | qemu_log_mask(CPU_LOG_INT, | |
2331 | "...really UsageFault with UFSR.UNALIGNED\n"); | |
2332 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNALIGNED_MASK; | |
2333 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, | |
2334 | env->v7m.secure); | |
2335 | break; | |
7aab5a8c PMD |
2336 | default: |
2337 | /* | |
2338 | * All other FSR values are either MPU faults or "can't happen | |
2339 | * for M profile" cases. | |
2340 | */ | |
2341 | switch (cs->exception_index) { | |
2342 | case EXCP_PREFETCH_ABORT: | |
2343 | env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; | |
2344 | qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n"); | |
2345 | break; | |
2346 | case EXCP_DATA_ABORT: | |
2347 | env->v7m.cfsr[env->v7m.secure] |= | |
2348 | (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK); | |
2349 | env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress; | |
2350 | qemu_log_mask(CPU_LOG_INT, | |
2351 | "...with CFSR.DACCVIOL and MMFAR 0x%x\n", | |
2352 | env->v7m.mmfar[env->v7m.secure]); | |
2353 | break; | |
2354 | } | |
2355 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, | |
2356 | env->v7m.secure); | |
2357 | break; | |
2358 | } | |
2359 | break; | |
376214e4 AB |
2360 | case EXCP_SEMIHOST: |
2361 | qemu_log_mask(CPU_LOG_INT, | |
2362 | "...handling as semihosting call 0x%x\n", | |
2363 | env->regs[0]); | |
0bb446d8 KP |
2364 | #ifdef CONFIG_TCG |
2365 | env->regs[0] = do_common_semihosting(cs); | |
2366 | #else | |
2367 | g_assert_not_reached(); | |
2368 | #endif | |
4ff5ef9e | 2369 | env->regs[15] += env->thumb ? 2 : 4; |
376214e4 | 2370 | return; |
7aab5a8c | 2371 | case EXCP_BKPT: |
7aab5a8c PMD |
2372 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false); |
2373 | break; | |
2374 | case EXCP_IRQ: | |
2375 | break; | |
2376 | case EXCP_EXCEPTION_EXIT: | |
2377 | if (env->regs[15] < EXC_RETURN_MIN_MAGIC) { | |
2378 | /* Must be v8M security extension function return */ | |
2379 | assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC); | |
2380 | assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); | |
2381 | if (do_v7m_function_return(cpu)) { | |
2382 | return; | |
2383 | } | |
2384 | } else { | |
2385 | do_v7m_exception_exit(cpu); | |
2386 | return; | |
2387 | } | |
2388 | break; | |
2389 | case EXCP_LAZYFP: | |
2390 | /* | |
2391 | * We already pended the specific exception in the NVIC in the | |
2392 | * v7m_preserve_fp_state() helper function. | |
2393 | */ | |
2394 | break; | |
2395 | default: | |
2396 | cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); | |
2397 | return; /* Never happens. Keep compiler happy. */ | |
2398 | } | |
2399 | ||
2400 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
2401 | lr = R_V7M_EXCRET_RES1_MASK | | |
2402 | R_V7M_EXCRET_DCRS_MASK; | |
2403 | /* | |
2404 | * The S bit indicates whether we should return to Secure | |
2405 | * or NonSecure (ie our current state). | |
2406 | * The ES bit indicates whether we're taking this exception | |
2407 | * to Secure or NonSecure (ie our target state). We set it | |
2408 | * later, in v7m_exception_taken(). | |
2409 | * The SPSEL bit is also set in v7m_exception_taken() for v8M. | |
2410 | * This corresponds to the ARM ARM pseudocode for v8M setting | |
2411 | * some LR bits in PushStack() and some in ExceptionTaken(); | |
2412 | * the distinction matters for the tailchain cases where we | |
2413 | * can take an exception without pushing the stack. | |
2414 | */ | |
2415 | if (env->v7m.secure) { | |
2416 | lr |= R_V7M_EXCRET_S_MASK; | |
2417 | } | |
7aab5a8c PMD |
2418 | } else { |
2419 | lr = R_V7M_EXCRET_RES1_MASK | | |
2420 | R_V7M_EXCRET_S_MASK | | |
2421 | R_V7M_EXCRET_DCRS_MASK | | |
7aab5a8c PMD |
2422 | R_V7M_EXCRET_ES_MASK; |
2423 | if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) { | |
2424 | lr |= R_V7M_EXCRET_SPSEL_MASK; | |
2425 | } | |
2426 | } | |
f900b1e5 JHD |
2427 | if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) { |
2428 | lr |= R_V7M_EXCRET_FTYPE_MASK; | |
2429 | } | |
7aab5a8c PMD |
2430 | if (!arm_v7m_is_handler_mode(env)) { |
2431 | lr |= R_V7M_EXCRET_MODE_MASK; | |
2432 | } | |
2433 | ||
2434 | ignore_stackfaults = v7m_push_stack(cpu); | |
2435 | v7m_exception_taken(cpu, lr, false, ignore_stackfaults); | |
2436 | } | |
2437 | ||
2438 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) | |
2439 | { | |
7aab5a8c PMD |
2440 | unsigned el = arm_current_el(env); |
2441 | ||
2442 | /* First handle registers which unprivileged can read */ | |
7aab5a8c PMD |
2443 | switch (reg) { |
2444 | case 0 ... 7: /* xPSR sub-fields */ | |
04c9c81b | 2445 | return v7m_mrs_xpsr(env, reg, el); |
7aab5a8c | 2446 | case 20: /* CONTROL */ |
04c9c81b | 2447 | return v7m_mrs_control(env, env->v7m.secure); |
7aab5a8c PMD |
2448 | case 0x94: /* CONTROL_NS */ |
2449 | /* | |
2450 | * We have to handle this here because unprivileged Secure code | |
2451 | * can read the NS CONTROL register. | |
2452 | */ | |
2453 | if (!env->v7m.secure) { | |
2454 | return 0; | |
2455 | } | |
2456 | return env->v7m.control[M_REG_NS] | | |
2457 | (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK); | |
2458 | } | |
2459 | ||
2460 | if (el == 0) { | |
2461 | return 0; /* unprivileged reads others as zero */ | |
2462 | } | |
2463 | ||
2464 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
2465 | switch (reg) { | |
2466 | case 0x88: /* MSP_NS */ | |
2467 | if (!env->v7m.secure) { | |
2468 | return 0; | |
2469 | } | |
2470 | return env->v7m.other_ss_msp; | |
2471 | case 0x89: /* PSP_NS */ | |
2472 | if (!env->v7m.secure) { | |
2473 | return 0; | |
2474 | } | |
2475 | return env->v7m.other_ss_psp; | |
2476 | case 0x8a: /* MSPLIM_NS */ | |
2477 | if (!env->v7m.secure) { | |
2478 | return 0; | |
2479 | } | |
2480 | return env->v7m.msplim[M_REG_NS]; | |
2481 | case 0x8b: /* PSPLIM_NS */ | |
2482 | if (!env->v7m.secure) { | |
2483 | return 0; | |
2484 | } | |
2485 | return env->v7m.psplim[M_REG_NS]; | |
2486 | case 0x90: /* PRIMASK_NS */ | |
2487 | if (!env->v7m.secure) { | |
2488 | return 0; | |
2489 | } | |
2490 | return env->v7m.primask[M_REG_NS]; | |
2491 | case 0x91: /* BASEPRI_NS */ | |
2492 | if (!env->v7m.secure) { | |
2493 | return 0; | |
2494 | } | |
2495 | return env->v7m.basepri[M_REG_NS]; | |
2496 | case 0x93: /* FAULTMASK_NS */ | |
2497 | if (!env->v7m.secure) { | |
2498 | return 0; | |
2499 | } | |
2500 | return env->v7m.faultmask[M_REG_NS]; | |
2501 | case 0x98: /* SP_NS */ | |
2502 | { | |
2503 | /* | |
2504 | * This gives the non-secure SP selected based on whether we're | |
2505 | * currently in handler mode or not, using the NS CONTROL.SPSEL. | |
2506 | */ | |
2507 | bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; | |
2508 | ||
2509 | if (!env->v7m.secure) { | |
2510 | return 0; | |
2511 | } | |
2512 | if (!arm_v7m_is_handler_mode(env) && spsel) { | |
2513 | return env->v7m.other_ss_psp; | |
2514 | } else { | |
2515 | return env->v7m.other_ss_msp; | |
2516 | } | |
2517 | } | |
2518 | default: | |
2519 | break; | |
2520 | } | |
2521 | } | |
2522 | ||
2523 | switch (reg) { | |
2524 | case 8: /* MSP */ | |
2525 | return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13]; | |
2526 | case 9: /* PSP */ | |
2527 | return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp; | |
2528 | case 10: /* MSPLIM */ | |
2529 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
2530 | goto bad_reg; | |
2531 | } | |
2532 | return env->v7m.msplim[env->v7m.secure]; | |
2533 | case 11: /* PSPLIM */ | |
2534 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
2535 | goto bad_reg; | |
2536 | } | |
2537 | return env->v7m.psplim[env->v7m.secure]; | |
2538 | case 16: /* PRIMASK */ | |
2539 | return env->v7m.primask[env->v7m.secure]; | |
2540 | case 17: /* BASEPRI */ | |
2541 | case 18: /* BASEPRI_MAX */ | |
2542 | return env->v7m.basepri[env->v7m.secure]; | |
2543 | case 19: /* FAULTMASK */ | |
2544 | return env->v7m.faultmask[env->v7m.secure]; | |
2545 | default: | |
2546 | bad_reg: | |
2547 | qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special" | |
2548 | " register %d\n", reg); | |
2549 | return 0; | |
2550 | } | |
2551 | } | |
2552 | ||
2553 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val) | |
2554 | { | |
2555 | /* | |
2556 | * We're passed bits [11..0] of the instruction; extract | |
2557 | * SYSm and the mask bits. | |
2558 | * Invalid combinations of SYSm and mask are UNPREDICTABLE; | |
2559 | * we choose to treat them as if the mask bits were valid. | |
2560 | * NB that the pseudocode 'mask' variable is bits [11..10], | |
2561 | * whereas ours is [11..8]. | |
2562 | */ | |
2563 | uint32_t mask = extract32(maskreg, 8, 4); | |
2564 | uint32_t reg = extract32(maskreg, 0, 8); | |
2565 | int cur_el = arm_current_el(env); | |
2566 | ||
2567 | if (cur_el == 0 && reg > 7 && reg != 20) { | |
2568 | /* | |
2569 | * only xPSR sub-fields and CONTROL.SFPA may be written by | |
2570 | * unprivileged code | |
2571 | */ | |
2572 | return; | |
2573 | } | |
2574 | ||
2575 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
2576 | switch (reg) { | |
2577 | case 0x88: /* MSP_NS */ | |
2578 | if (!env->v7m.secure) { | |
2579 | return; | |
2580 | } | |
888f470f | 2581 | env->v7m.other_ss_msp = val & ~3; |
7aab5a8c PMD |
2582 | return; |
2583 | case 0x89: /* PSP_NS */ | |
2584 | if (!env->v7m.secure) { | |
2585 | return; | |
2586 | } | |
888f470f | 2587 | env->v7m.other_ss_psp = val & ~3; |
7aab5a8c PMD |
2588 | return; |
2589 | case 0x8a: /* MSPLIM_NS */ | |
2590 | if (!env->v7m.secure) { | |
2591 | return; | |
2592 | } | |
2593 | env->v7m.msplim[M_REG_NS] = val & ~7; | |
2594 | return; | |
2595 | case 0x8b: /* PSPLIM_NS */ | |
2596 | if (!env->v7m.secure) { | |
2597 | return; | |
2598 | } | |
2599 | env->v7m.psplim[M_REG_NS] = val & ~7; | |
2600 | return; | |
2601 | case 0x90: /* PRIMASK_NS */ | |
2602 | if (!env->v7m.secure) { | |
2603 | return; | |
2604 | } | |
2605 | env->v7m.primask[M_REG_NS] = val & 1; | |
2606 | return; | |
2607 | case 0x91: /* BASEPRI_NS */ | |
2608 | if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { | |
2609 | return; | |
2610 | } | |
2611 | env->v7m.basepri[M_REG_NS] = val & 0xff; | |
2612 | return; | |
2613 | case 0x93: /* FAULTMASK_NS */ | |
2614 | if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { | |
2615 | return; | |
2616 | } | |
2617 | env->v7m.faultmask[M_REG_NS] = val & 1; | |
2618 | return; | |
2619 | case 0x94: /* CONTROL_NS */ | |
2620 | if (!env->v7m.secure) { | |
2621 | return; | |
2622 | } | |
2623 | write_v7m_control_spsel_for_secstate(env, | |
2624 | val & R_V7M_CONTROL_SPSEL_MASK, | |
2625 | M_REG_NS); | |
2626 | if (arm_feature(env, ARM_FEATURE_M_MAIN)) { | |
2627 | env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK; | |
2628 | env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK; | |
2629 | } | |
2630 | /* | |
2631 | * SFPA is RAZ/WI from NS. FPCA is RO if NSACR.CP10 == 0, | |
2632 | * RES0 if the FPU is not present, and is stored in the S bank | |
2633 | */ | |
7fbc6a40 | 2634 | if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env)) && |
7aab5a8c PMD |
2635 | extract32(env->v7m.nsacr, 10, 1)) { |
2636 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; | |
2637 | env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK; | |
2638 | } | |
2639 | return; | |
2640 | case 0x98: /* SP_NS */ | |
2641 | { | |
2642 | /* | |
2643 | * This gives the non-secure SP selected based on whether we're | |
2644 | * currently in handler mode or not, using the NS CONTROL.SPSEL. | |
2645 | */ | |
2646 | bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; | |
2647 | bool is_psp = !arm_v7m_is_handler_mode(env) && spsel; | |
2648 | uint32_t limit; | |
2649 | ||
2650 | if (!env->v7m.secure) { | |
2651 | return; | |
2652 | } | |
2653 | ||
2654 | limit = is_psp ? env->v7m.psplim[false] : env->v7m.msplim[false]; | |
2655 | ||
888f470f PM |
2656 | val &= ~0x3; |
2657 | ||
7aab5a8c | 2658 | if (val < limit) { |
9d75d45c | 2659 | raise_exception_ra(env, EXCP_STKOF, 0, 1, GETPC()); |
7aab5a8c PMD |
2660 | } |
2661 | ||
2662 | if (is_psp) { | |
2663 | env->v7m.other_ss_psp = val; | |
2664 | } else { | |
2665 | env->v7m.other_ss_msp = val; | |
2666 | } | |
2667 | return; | |
2668 | } | |
2669 | default: | |
2670 | break; | |
2671 | } | |
2672 | } | |
2673 | ||
2674 | switch (reg) { | |
2675 | case 0 ... 7: /* xPSR sub-fields */ | |
04c9c81b | 2676 | v7m_msr_xpsr(env, mask, reg, val); |
7aab5a8c PMD |
2677 | break; |
2678 | case 8: /* MSP */ | |
2679 | if (v7m_using_psp(env)) { | |
888f470f | 2680 | env->v7m.other_sp = val & ~3; |
7aab5a8c | 2681 | } else { |
888f470f | 2682 | env->regs[13] = val & ~3; |
7aab5a8c PMD |
2683 | } |
2684 | break; | |
2685 | case 9: /* PSP */ | |
2686 | if (v7m_using_psp(env)) { | |
888f470f | 2687 | env->regs[13] = val & ~3; |
7aab5a8c | 2688 | } else { |
888f470f | 2689 | env->v7m.other_sp = val & ~3; |
7aab5a8c PMD |
2690 | } |
2691 | break; | |
2692 | case 10: /* MSPLIM */ | |
2693 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
2694 | goto bad_reg; | |
2695 | } | |
2696 | env->v7m.msplim[env->v7m.secure] = val & ~7; | |
2697 | break; | |
2698 | case 11: /* PSPLIM */ | |
2699 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
2700 | goto bad_reg; | |
2701 | } | |
2702 | env->v7m.psplim[env->v7m.secure] = val & ~7; | |
2703 | break; | |
2704 | case 16: /* PRIMASK */ | |
2705 | env->v7m.primask[env->v7m.secure] = val & 1; | |
2706 | break; | |
2707 | case 17: /* BASEPRI */ | |
2708 | if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { | |
2709 | goto bad_reg; | |
2710 | } | |
2711 | env->v7m.basepri[env->v7m.secure] = val & 0xff; | |
2712 | break; | |
2713 | case 18: /* BASEPRI_MAX */ | |
2714 | if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { | |
2715 | goto bad_reg; | |
2716 | } | |
2717 | val &= 0xff; | |
2718 | if (val != 0 && (val < env->v7m.basepri[env->v7m.secure] | |
2719 | || env->v7m.basepri[env->v7m.secure] == 0)) { | |
2720 | env->v7m.basepri[env->v7m.secure] = val; | |
2721 | } | |
2722 | break; | |
2723 | case 19: /* FAULTMASK */ | |
2724 | if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { | |
2725 | goto bad_reg; | |
2726 | } | |
2727 | env->v7m.faultmask[env->v7m.secure] = val & 1; | |
2728 | break; | |
2729 | case 20: /* CONTROL */ | |
2730 | /* | |
2731 | * Writing to the SPSEL bit only has an effect if we are in | |
2732 | * thread mode; other bits can be updated by any privileged code. | |
2733 | * write_v7m_control_spsel() deals with updating the SPSEL bit in | |
2734 | * env->v7m.control, so we only need update the others. | |
2735 | * For v7M, we must just ignore explicit writes to SPSEL in handler | |
2736 | * mode; for v8M the write is permitted but will have no effect. | |
2737 | * All these bits are writes-ignored from non-privileged code, | |
2738 | * except for SFPA. | |
2739 | */ | |
2740 | if (cur_el > 0 && (arm_feature(env, ARM_FEATURE_V8) || | |
2741 | !arm_v7m_is_handler_mode(env))) { | |
2742 | write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0); | |
2743 | } | |
2744 | if (cur_el > 0 && arm_feature(env, ARM_FEATURE_M_MAIN)) { | |
2745 | env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK; | |
2746 | env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK; | |
2747 | } | |
7fbc6a40 | 2748 | if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) { |
7aab5a8c PMD |
2749 | /* |
2750 | * SFPA is RAZ/WI from NS or if no FPU. | |
2751 | * FPCA is RO if NSACR.CP10 == 0, RES0 if the FPU is not present. | |
2752 | * Both are stored in the S bank. | |
2753 | */ | |
2754 | if (env->v7m.secure) { | |
2755 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; | |
2756 | env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_SFPA_MASK; | |
2757 | } | |
2758 | if (cur_el > 0 && | |
2759 | (env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_SECURITY) || | |
2760 | extract32(env->v7m.nsacr, 10, 1))) { | |
2761 | env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; | |
2762 | env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK; | |
2763 | } | |
2764 | } | |
2765 | break; | |
2766 | default: | |
2767 | bad_reg: | |
2768 | qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special" | |
2769 | " register %d\n", reg); | |
2770 | return; | |
2771 | } | |
2772 | } | |
2773 | ||
2774 | uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) | |
2775 | { | |
2776 | /* Implement the TT instruction. op is bits [7:6] of the insn. */ | |
2777 | bool forceunpriv = op & 1; | |
2778 | bool alt = op & 2; | |
2779 | V8M_SAttributes sattrs = {}; | |
2780 | uint32_t tt_resp; | |
2781 | bool r, rw, nsr, nsrw, mrvalid; | |
2782 | int prot; | |
2783 | ARMMMUFaultInfo fi = {}; | |
2784 | MemTxAttrs attrs = {}; | |
2785 | hwaddr phys_addr; | |
2786 | ARMMMUIdx mmu_idx; | |
2787 | uint32_t mregion; | |
2788 | bool targetpriv; | |
2789 | bool targetsec = env->v7m.secure; | |
2790 | bool is_subpage; | |
2791 | ||
2792 | /* | |
2793 | * Work out what the security state and privilege level we're | |
2794 | * interested in is... | |
2795 | */ | |
2796 | if (alt) { | |
2797 | targetsec = !targetsec; | |
2798 | } | |
2799 | ||
2800 | if (forceunpriv) { | |
2801 | targetpriv = false; | |
2802 | } else { | |
2803 | targetpriv = arm_v7m_is_handler_mode(env) || | |
2804 | !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK); | |
2805 | } | |
2806 | ||
2807 | /* ...and then figure out which MMU index this is */ | |
2808 | mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv); | |
2809 | ||
2810 | /* | |
2811 | * We know that the MPU and SAU don't care about the access type | |
2812 | * for our purposes beyond that we don't want to claim to be | |
2813 | * an insn fetch, so we arbitrarily call this a read. | |
2814 | */ | |
2815 | ||
2816 | /* | |
2817 | * MPU region info only available for privileged or if | |
2818 | * inspecting the other MPU state. | |
2819 | */ | |
2820 | if (arm_current_el(env) != 0 || alt) { | |
2821 | /* We can ignore the return value as prot is always set */ | |
2822 | pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, | |
2823 | &phys_addr, &attrs, &prot, &is_subpage, | |
2824 | &fi, &mregion); | |
2825 | if (mregion == -1) { | |
2826 | mrvalid = false; | |
2827 | mregion = 0; | |
2828 | } else { | |
2829 | mrvalid = true; | |
2830 | } | |
2831 | r = prot & PAGE_READ; | |
2832 | rw = prot & PAGE_WRITE; | |
2833 | } else { | |
2834 | r = false; | |
2835 | rw = false; | |
2836 | mrvalid = false; | |
2837 | mregion = 0; | |
2838 | } | |
2839 | ||
2840 | if (env->v7m.secure) { | |
2841 | v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); | |
2842 | nsr = sattrs.ns && r; | |
2843 | nsrw = sattrs.ns && rw; | |
2844 | } else { | |
2845 | sattrs.ns = true; | |
2846 | nsr = false; | |
2847 | nsrw = false; | |
2848 | } | |
2849 | ||
2850 | tt_resp = (sattrs.iregion << 24) | | |
2851 | (sattrs.irvalid << 23) | | |
2852 | ((!sattrs.ns) << 22) | | |
2853 | (nsrw << 21) | | |
2854 | (nsr << 20) | | |
2855 | (rw << 19) | | |
2856 | (r << 18) | | |
2857 | (sattrs.srvalid << 17) | | |
2858 | (mrvalid << 16) | | |
2859 | (sattrs.sregion << 8) | | |
2860 | mregion; | |
2861 | ||
2862 | return tt_resp; | |
2863 | } | |
2864 | ||
2865 | #endif /* !CONFIG_USER_ONLY */ | |
2866 | ||
2867 | ARMMMUIdx arm_v7m_mmu_idx_all(CPUARMState *env, | |
2868 | bool secstate, bool priv, bool negpri) | |
2869 | { | |
2870 | ARMMMUIdx mmu_idx = ARM_MMU_IDX_M; | |
2871 | ||
2872 | if (priv) { | |
2873 | mmu_idx |= ARM_MMU_IDX_M_PRIV; | |
2874 | } | |
2875 | ||
2876 | if (negpri) { | |
2877 | mmu_idx |= ARM_MMU_IDX_M_NEGPRI; | |
2878 | } | |
2879 | ||
2880 | if (secstate) { | |
2881 | mmu_idx |= ARM_MMU_IDX_M_S; | |
2882 | } | |
2883 | ||
2884 | return mmu_idx; | |
2885 | } | |
2886 | ||
2887 | ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env, | |
2888 | bool secstate, bool priv) | |
2889 | { | |
2890 | bool negpri = armv7m_nvic_neg_prio_requested(env->nvic, secstate); | |
2891 | ||
2892 | return arm_v7m_mmu_idx_all(env, secstate, priv, negpri); | |
2893 | } | |
2894 | ||
2895 | /* Return the MMU index for a v7M CPU in the specified security state */ | |
2896 | ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate) | |
2897 | { | |
7142eb9e PM |
2898 | bool priv = arm_v7m_is_handler_mode(env) || |
2899 | !(env->v7m.control[secstate] & 1); | |
7aab5a8c PMD |
2900 | |
2901 | return arm_v7m_mmu_idx_for_secstate_and_priv(env, secstate, priv); | |
2902 | } |