2 * QEMU ARM CPU -- internal functions and types
4 * Copyright (c) 2014 Linaro Ltd
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see
18 * <http://www.gnu.org/licenses/gpl-2.0.html>
20 * This header defines functions, types, etc which need to be shared
21 * between different source files within target/arm/ but which are
22 * private to it and not required by the rest of QEMU.
25 #ifndef TARGET_ARM_INTERNALS_H
26 #define TARGET_ARM_INTERNALS_H
28 #include "hw/registerfields.h"
30 /* register banks for CPU modes */
40 static inline bool excp_is_internal(int excp)
42 /* Return true if this exception number represents a QEMU-internal
43 * exception that will not be passed to the guest.
45 return excp == EXCP_INTERRUPT
48 || excp == EXCP_HALTED
49 || excp == EXCP_EXCEPTION_EXIT
50 || excp == EXCP_KERNEL_TRAP
51 || excp == EXCP_SEMIHOST;
54 /* Scale factor for generic timers, ie number of ns per tick.
55 * This gives a 62.5MHz timer.
57 #define GTIMER_SCALE 16
59 /* Bit definitions for the v7M CONTROL register */
60 FIELD(V7M_CONTROL, NPRIV, 0, 1)
61 FIELD(V7M_CONTROL, SPSEL, 1, 1)
62 FIELD(V7M_CONTROL, FPCA, 2, 1)
63 FIELD(V7M_CONTROL, SFPA, 3, 1)
65 /* Bit definitions for v7M exception return payload */
66 FIELD(V7M_EXCRET, ES, 0, 1)
67 FIELD(V7M_EXCRET, RES0, 1, 1)
68 FIELD(V7M_EXCRET, SPSEL, 2, 1)
69 FIELD(V7M_EXCRET, MODE, 3, 1)
70 FIELD(V7M_EXCRET, FTYPE, 4, 1)
71 FIELD(V7M_EXCRET, DCRS, 5, 1)
72 FIELD(V7M_EXCRET, S, 6, 1)
73 FIELD(V7M_EXCRET, RES1, 7, 25) /* including the must-be-1 prefix */
75 /* Minimum value which is a magic number for exception return */
76 #define EXC_RETURN_MIN_MAGIC 0xff000000
77 /* Minimum number which is a magic number for function or exception return
78 * when using v8M security extension
80 #define FNC_RETURN_MIN_MAGIC 0xfefffffe
82 /* We use a few fake FSR values for internal purposes in M profile.
83 * M profile cores don't have A/R format FSRs, but currently our
84 * get_phys_addr() code assumes A/R profile and reports failures via
85 * an A/R format FSR value. We then translate that into the proper
86 * M profile exception and FSR status bit in arm_v7m_cpu_do_interrupt().
87 * Mostly the FSR values we use for this are those defined for v7PMSA,
88 * since we share some of that codepath. A few kinds of fault are
89 * only for M profile and have no A/R equivalent, though, so we have
90 * to pick a value from the reserved range (which we never otherwise
91 * generate) to use for these.
92 * These values will never be visible to the guest.
94 #define M_FAKE_FSR_NSC_EXEC 0xf /* NS executing in S&NSC memory */
95 #define M_FAKE_FSR_SFAULT 0xe /* SecureFault INVTRAN, INVEP or AUVIOL */
98 * For AArch64, map a given EL to an index in the banked_spsr array.
99 * Note that this mapping and the AArch32 mapping defined in bank_number()
100 * must agree such that the AArch64<->AArch32 SPSRs have the architecturally
101 * mandated mapping between each other.
103 static inline unsigned int aarch64_banked_spsr_index(unsigned int el)
105 static const unsigned int map[4] = {
106 [1] = BANK_SVC, /* EL1. */
107 [2] = BANK_HYP, /* EL2. */
108 [3] = BANK_MON, /* EL3. */
110 assert(el >= 1 && el <= 3);
114 /* Map CPU modes onto saved register banks. */
115 static inline int bank_number(int mode)
118 case ARM_CPU_MODE_USR:
119 case ARM_CPU_MODE_SYS:
121 case ARM_CPU_MODE_SVC:
123 case ARM_CPU_MODE_ABT:
125 case ARM_CPU_MODE_UND:
127 case ARM_CPU_MODE_IRQ:
129 case ARM_CPU_MODE_FIQ:
131 case ARM_CPU_MODE_HYP:
133 case ARM_CPU_MODE_MON:
136 g_assert_not_reached();
139 void switch_mode(CPUARMState *, int);
140 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu);
141 void arm_translate_init(void);
143 enum arm_fprounding {
152 int arm_rmode_to_sf(int rmode);
154 static inline void aarch64_save_sp(CPUARMState *env, int el)
156 if (env->pstate & PSTATE_SP) {
157 env->sp_el[el] = env->xregs[31];
159 env->sp_el[0] = env->xregs[31];
163 static inline void aarch64_restore_sp(CPUARMState *env, int el)
165 if (env->pstate & PSTATE_SP) {
166 env->xregs[31] = env->sp_el[el];
168 env->xregs[31] = env->sp_el[0];
172 static inline void update_spsel(CPUARMState *env, uint32_t imm)
174 unsigned int cur_el = arm_current_el(env);
175 /* Update PSTATE SPSel bit; this requires us to update the
176 * working stack pointer in xregs[31].
178 if (!((imm ^ env->pstate) & PSTATE_SP)) {
181 aarch64_save_sp(env, cur_el);
182 env->pstate = deposit32(env->pstate, 0, 1, imm);
184 /* We rely on illegal updates to SPsel from EL0 to get trapped
185 * at translation time.
187 assert(cur_el >= 1 && cur_el <= 3);
188 aarch64_restore_sp(env, cur_el);
195 * Returns the implementation defined bit-width of physical addresses.
196 * The ARMv8 reference manuals refer to this as PAMax().
198 static inline unsigned int arm_pamax(ARMCPU *cpu)
200 static const unsigned int pamax_map[] = {
208 unsigned int parange = extract32(cpu->id_aa64mmfr0, 0, 4);
210 /* id_aa64mmfr0 is a read-only register so values outside of the
211 * supported mappings can be considered an implementation error. */
212 assert(parange < ARRAY_SIZE(pamax_map));
213 return pamax_map[parange];
216 /* Return true if extended addresses are enabled.
217 * This is always the case if our translation regime is 64 bit,
218 * but depends on TTBCR.EAE for 32 bit.
220 static inline bool extended_addresses_enabled(CPUARMState *env)
222 TCR *tcr = &env->cp15.tcr_el[arm_is_secure(env) ? 3 : 1];
223 return arm_el_is_aa64(env, 1) ||
224 (arm_feature(env, ARM_FEATURE_LPAE) && (tcr->raw_tcr & TTBCR_EAE));
227 /* Valid Syndrome Register EC field values */
228 enum arm_exception_class {
229 EC_UNCATEGORIZED = 0x00,
231 EC_CP15RTTRAP = 0x03,
232 EC_CP15RRTTRAP = 0x04,
233 EC_CP14RTTRAP = 0x05,
234 EC_CP14DTTRAP = 0x06,
235 EC_ADVSIMDFPACCESSTRAP = 0x07,
237 EC_CP14RRTTRAP = 0x0c,
238 EC_ILLEGALSTATE = 0x0e,
245 EC_SYSTEMREGISTERTRAP = 0x18,
247 EC_INSNABORT_SAME_EL = 0x21,
248 EC_PCALIGNMENT = 0x22,
250 EC_DATAABORT_SAME_EL = 0x25,
251 EC_SPALIGNMENT = 0x26,
252 EC_AA32_FPTRAP = 0x28,
253 EC_AA64_FPTRAP = 0x2c,
255 EC_BREAKPOINT = 0x30,
256 EC_BREAKPOINT_SAME_EL = 0x31,
257 EC_SOFTWARESTEP = 0x32,
258 EC_SOFTWARESTEP_SAME_EL = 0x33,
259 EC_WATCHPOINT = 0x34,
260 EC_WATCHPOINT_SAME_EL = 0x35,
262 EC_VECTORCATCH = 0x3a,
266 #define ARM_EL_EC_SHIFT 26
267 #define ARM_EL_IL_SHIFT 25
268 #define ARM_EL_ISV_SHIFT 24
269 #define ARM_EL_IL (1 << ARM_EL_IL_SHIFT)
270 #define ARM_EL_ISV (1 << ARM_EL_ISV_SHIFT)
272 /* Utility functions for constructing various kinds of syndrome value.
273 * Note that in general we follow the AArch64 syndrome values; in a
274 * few cases the value in HSR for exceptions taken to AArch32 Hyp
275 * mode differs slightly, so if we ever implemented Hyp mode then the
276 * syndrome value would need some massaging on exception entry.
277 * (One example of this is that AArch64 defaults to IL bit set for
278 * exceptions which don't specifically indicate information about the
279 * trapping instruction, whereas AArch32 defaults to IL bit clear.)
281 static inline uint32_t syn_uncategorized(void)
283 return (EC_UNCATEGORIZED << ARM_EL_EC_SHIFT) | ARM_EL_IL;
286 static inline uint32_t syn_aa64_svc(uint32_t imm16)
288 return (EC_AA64_SVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
291 static inline uint32_t syn_aa64_hvc(uint32_t imm16)
293 return (EC_AA64_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
296 static inline uint32_t syn_aa64_smc(uint32_t imm16)
298 return (EC_AA64_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
301 static inline uint32_t syn_aa32_svc(uint32_t imm16, bool is_16bit)
303 return (EC_AA32_SVC << ARM_EL_EC_SHIFT) | (imm16 & 0xffff)
304 | (is_16bit ? 0 : ARM_EL_IL);
307 static inline uint32_t syn_aa32_hvc(uint32_t imm16)
309 return (EC_AA32_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
312 static inline uint32_t syn_aa32_smc(void)
314 return (EC_AA32_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL;
317 static inline uint32_t syn_aa64_bkpt(uint32_t imm16)
319 return (EC_AA64_BKPT << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
322 static inline uint32_t syn_aa32_bkpt(uint32_t imm16, bool is_16bit)
324 return (EC_AA32_BKPT << ARM_EL_EC_SHIFT) | (imm16 & 0xffff)
325 | (is_16bit ? 0 : ARM_EL_IL);
328 static inline uint32_t syn_aa64_sysregtrap(int op0, int op1, int op2,
329 int crn, int crm, int rt,
332 return (EC_SYSTEMREGISTERTRAP << ARM_EL_EC_SHIFT) | ARM_EL_IL
333 | (op0 << 20) | (op2 << 17) | (op1 << 14) | (crn << 10) | (rt << 5)
334 | (crm << 1) | isread;
337 static inline uint32_t syn_cp14_rt_trap(int cv, int cond, int opc1, int opc2,
338 int crn, int crm, int rt, int isread,
341 return (EC_CP14RTTRAP << ARM_EL_EC_SHIFT)
342 | (is_16bit ? 0 : ARM_EL_IL)
343 | (cv << 24) | (cond << 20) | (opc2 << 17) | (opc1 << 14)
344 | (crn << 10) | (rt << 5) | (crm << 1) | isread;
347 static inline uint32_t syn_cp15_rt_trap(int cv, int cond, int opc1, int opc2,
348 int crn, int crm, int rt, int isread,
351 return (EC_CP15RTTRAP << ARM_EL_EC_SHIFT)
352 | (is_16bit ? 0 : ARM_EL_IL)
353 | (cv << 24) | (cond << 20) | (opc2 << 17) | (opc1 << 14)
354 | (crn << 10) | (rt << 5) | (crm << 1) | isread;
357 static inline uint32_t syn_cp14_rrt_trap(int cv, int cond, int opc1, int crm,
358 int rt, int rt2, int isread,
361 return (EC_CP14RRTTRAP << ARM_EL_EC_SHIFT)
362 | (is_16bit ? 0 : ARM_EL_IL)
363 | (cv << 24) | (cond << 20) | (opc1 << 16)
364 | (rt2 << 10) | (rt << 5) | (crm << 1) | isread;
367 static inline uint32_t syn_cp15_rrt_trap(int cv, int cond, int opc1, int crm,
368 int rt, int rt2, int isread,
371 return (EC_CP15RRTTRAP << ARM_EL_EC_SHIFT)
372 | (is_16bit ? 0 : ARM_EL_IL)
373 | (cv << 24) | (cond << 20) | (opc1 << 16)
374 | (rt2 << 10) | (rt << 5) | (crm << 1) | isread;
377 static inline uint32_t syn_fp_access_trap(int cv, int cond, bool is_16bit)
379 return (EC_ADVSIMDFPACCESSTRAP << ARM_EL_EC_SHIFT)
380 | (is_16bit ? 0 : ARM_EL_IL)
381 | (cv << 24) | (cond << 20);
384 static inline uint32_t syn_insn_abort(int same_el, int ea, int s1ptw, int fsc)
386 return (EC_INSNABORT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
387 | ARM_EL_IL | (ea << 9) | (s1ptw << 7) | fsc;
390 static inline uint32_t syn_data_abort_no_iss(int same_el,
391 int ea, int cm, int s1ptw,
394 return (EC_DATAABORT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
396 | (ea << 9) | (cm << 8) | (s1ptw << 7) | (wnr << 6) | fsc;
399 static inline uint32_t syn_data_abort_with_iss(int same_el,
400 int sas, int sse, int srt,
402 int ea, int cm, int s1ptw,
406 return (EC_DATAABORT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
407 | (is_16bit ? 0 : ARM_EL_IL)
408 | ARM_EL_ISV | (sas << 22) | (sse << 21) | (srt << 16)
409 | (sf << 15) | (ar << 14)
410 | (ea << 9) | (cm << 8) | (s1ptw << 7) | (wnr << 6) | fsc;
413 static inline uint32_t syn_swstep(int same_el, int isv, int ex)
415 return (EC_SOFTWARESTEP << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
416 | ARM_EL_IL | (isv << 24) | (ex << 6) | 0x22;
419 static inline uint32_t syn_watchpoint(int same_el, int cm, int wnr)
421 return (EC_WATCHPOINT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
422 | ARM_EL_IL | (cm << 8) | (wnr << 6) | 0x22;
425 static inline uint32_t syn_breakpoint(int same_el)
427 return (EC_BREAKPOINT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
431 static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
433 return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) |
434 (is_16bit ? 0 : (1 << ARM_EL_IL_SHIFT)) |
435 (cv << 24) | (cond << 20) | ti;
438 /* Update a QEMU watchpoint based on the information the guest has set in the
439 * DBGWCR<n>_EL1 and DBGWVR<n>_EL1 registers.
441 void hw_watchpoint_update(ARMCPU *cpu, int n);
442 /* Update the QEMU watchpoints for every guest watchpoint. This does a
443 * complete delete-and-reinstate of the QEMU watchpoint list and so is
444 * suitable for use after migration or on reset.
446 void hw_watchpoint_update_all(ARMCPU *cpu);
447 /* Update a QEMU breakpoint based on the information the guest has set in the
448 * DBGBCR<n>_EL1 and DBGBVR<n>_EL1 registers.
450 void hw_breakpoint_update(ARMCPU *cpu, int n);
451 /* Update the QEMU breakpoints for every guest breakpoint. This does a
452 * complete delete-and-reinstate of the QEMU breakpoint list and so is
453 * suitable for use after migration or on reset.
455 void hw_breakpoint_update_all(ARMCPU *cpu);
457 /* Callback function for checking if a watchpoint should trigger. */
458 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
460 /* Adjust addresses (in BE32 mode) before testing against watchpoint
463 vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len);
465 /* Callback function for when a watchpoint or breakpoint triggers. */
466 void arm_debug_excp_handler(CPUState *cs);
468 #ifdef CONFIG_USER_ONLY
469 static inline bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
474 /* Return true if the r0/x0 value indicates that this SMC/HVC is a PSCI call. */
475 bool arm_is_psci_call(ARMCPU *cpu, int excp_type);
476 /* Actually handle a PSCI call */
477 void arm_handle_psci_call(ARMCPU *cpu);
481 * arm_clear_exclusive: clear the exclusive monitor
483 * Clear the CPU's exclusive monitor, like the guest CLREX instruction.
485 static inline void arm_clear_exclusive(CPUARMState *env)
487 env->exclusive_addr = -1;
491 * ARMMMUFaultInfo: Information describing an ARM MMU Fault
492 * @s2addr: Address that caused a fault at stage 2
493 * @stage2: True if we faulted at stage 2
494 * @s1ptw: True if we faulted at stage 2 while doing a stage 1 page-table walk
495 * @ea: True if we should set the EA (external abort type) bit in syndrome
497 typedef struct ARMMMUFaultInfo ARMMMUFaultInfo;
498 struct ARMMMUFaultInfo {
505 /* Do a page table walk and add page to TLB if possible */
506 bool arm_tlb_fill(CPUState *cpu, vaddr address,
507 MMUAccessType access_type, int mmu_idx,
508 uint32_t *fsr, ARMMMUFaultInfo *fi);
510 /* Return true if the stage 1 translation regime is using LPAE format page
512 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx);
514 /* Raise a data fault alignment exception for the specified virtual address */
515 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
516 MMUAccessType access_type,
517 int mmu_idx, uintptr_t retaddr);
519 /* arm_cpu_do_transaction_failed: handle a memory system error response
520 * (eg "no device/memory present at address") by raising an external abort
523 void arm_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
524 vaddr addr, unsigned size,
525 MMUAccessType access_type,
526 int mmu_idx, MemTxAttrs attrs,
527 MemTxResult response, uintptr_t retaddr);
529 /* Call the EL change hook if one has been registered */
530 static inline void arm_call_el_change_hook(ARMCPU *cpu)
532 if (cpu->el_change_hook) {
533 cpu->el_change_hook(cpu, cpu->el_change_hook_opaque);
537 /* Return true if this address translation regime is secure */
538 static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
541 case ARMMMUIdx_S12NSE0:
542 case ARMMMUIdx_S12NSE1:
543 case ARMMMUIdx_S1NSE0:
544 case ARMMMUIdx_S1NSE1:
547 case ARMMMUIdx_MPriv:
548 case ARMMMUIdx_MNegPri:
549 case ARMMMUIdx_MUser:
552 case ARMMMUIdx_S1SE0:
553 case ARMMMUIdx_S1SE1:
554 case ARMMMUIdx_MSPriv:
555 case ARMMMUIdx_MSNegPri:
556 case ARMMMUIdx_MSUser:
559 g_assert_not_reached();