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 /* register banks for CPU modes */
38 static inline bool excp_is_internal(int excp
)
40 /* Return true if this exception number represents a QEMU-internal
41 * exception that will not be passed to the guest.
43 return excp
== EXCP_INTERRUPT
46 || excp
== EXCP_HALTED
47 || excp
== EXCP_EXCEPTION_EXIT
48 || excp
== EXCP_KERNEL_TRAP
49 || excp
== EXCP_SEMIHOST
50 || excp
== EXCP_STREX
;
53 /* Exception names for debug logging; note that not all of these
54 * precisely correspond to architectural exceptions.
56 static const char * const excnames
[] = {
57 [EXCP_UDEF
] = "Undefined Instruction",
59 [EXCP_PREFETCH_ABORT
] = "Prefetch Abort",
60 [EXCP_DATA_ABORT
] = "Data Abort",
63 [EXCP_BKPT
] = "Breakpoint",
64 [EXCP_EXCEPTION_EXIT
] = "QEMU v7M exception exit",
65 [EXCP_KERNEL_TRAP
] = "QEMU intercept of kernel commpage",
66 [EXCP_STREX
] = "QEMU intercept of STREX",
67 [EXCP_HVC
] = "Hypervisor Call",
68 [EXCP_HYP_TRAP
] = "Hypervisor Trap",
69 [EXCP_SMC
] = "Secure Monitor Call",
70 [EXCP_VIRQ
] = "Virtual IRQ",
71 [EXCP_VFIQ
] = "Virtual FIQ",
72 [EXCP_SEMIHOST
] = "Semihosting call",
75 static inline void arm_log_exception(int idx
)
77 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
78 const char *exc
= NULL
;
80 if (idx
>= 0 && idx
< ARRAY_SIZE(excnames
)) {
86 qemu_log_mask(CPU_LOG_INT
, "Taking exception %d [%s]\n", idx
, exc
);
90 /* Scale factor for generic timers, ie number of ns per tick.
91 * This gives a 62.5MHz timer.
93 #define GTIMER_SCALE 16
96 * For AArch64, map a given EL to an index in the banked_spsr array.
97 * Note that this mapping and the AArch32 mapping defined in bank_number()
98 * must agree such that the AArch64<->AArch32 SPSRs have the architecturally
99 * mandated mapping between each other.
101 static inline unsigned int aarch64_banked_spsr_index(unsigned int el
)
103 static const unsigned int map
[4] = {
104 [1] = BANK_SVC
, /* EL1. */
105 [2] = BANK_HYP
, /* EL2. */
106 [3] = BANK_MON
, /* EL3. */
108 assert(el
>= 1 && el
<= 3);
112 /* Map CPU modes onto saved register banks. */
113 static inline int bank_number(int mode
)
116 case ARM_CPU_MODE_USR
:
117 case ARM_CPU_MODE_SYS
:
119 case ARM_CPU_MODE_SVC
:
121 case ARM_CPU_MODE_ABT
:
123 case ARM_CPU_MODE_UND
:
125 case ARM_CPU_MODE_IRQ
:
127 case ARM_CPU_MODE_FIQ
:
129 case ARM_CPU_MODE_HYP
:
131 case ARM_CPU_MODE_MON
:
134 g_assert_not_reached();
137 void switch_mode(CPUARMState
*, int);
138 void arm_cpu_register_gdb_regs_for_features(ARMCPU
*cpu
);
139 void arm_translate_init(void);
141 enum arm_fprounding
{
150 int arm_rmode_to_sf(int rmode
);
152 static inline void aarch64_save_sp(CPUARMState
*env
, int el
)
154 if (env
->pstate
& PSTATE_SP
) {
155 env
->sp_el
[el
] = env
->xregs
[31];
157 env
->sp_el
[0] = env
->xregs
[31];
161 static inline void aarch64_restore_sp(CPUARMState
*env
, int el
)
163 if (env
->pstate
& PSTATE_SP
) {
164 env
->xregs
[31] = env
->sp_el
[el
];
166 env
->xregs
[31] = env
->sp_el
[0];
170 static inline void update_spsel(CPUARMState
*env
, uint32_t imm
)
172 unsigned int cur_el
= arm_current_el(env
);
173 /* Update PSTATE SPSel bit; this requires us to update the
174 * working stack pointer in xregs[31].
176 if (!((imm
^ env
->pstate
) & PSTATE_SP
)) {
179 aarch64_save_sp(env
, cur_el
);
180 env
->pstate
= deposit32(env
->pstate
, 0, 1, imm
);
182 /* We rely on illegal updates to SPsel from EL0 to get trapped
183 * at translation time.
185 assert(cur_el
>= 1 && cur_el
<= 3);
186 aarch64_restore_sp(env
, cur_el
);
193 * Returns the implementation defined bit-width of physical addresses.
194 * The ARMv8 reference manuals refer to this as PAMax().
196 static inline unsigned int arm_pamax(ARMCPU
*cpu
)
198 static const unsigned int pamax_map
[] = {
206 unsigned int parange
= extract32(cpu
->id_aa64mmfr0
, 0, 4);
208 /* id_aa64mmfr0 is a read-only register so values outside of the
209 * supported mappings can be considered an implementation error. */
210 assert(parange
< ARRAY_SIZE(pamax_map
));
211 return pamax_map
[parange
];
214 /* Return true if extended addresses are enabled.
215 * This is always the case if our translation regime is 64 bit,
216 * but depends on TTBCR.EAE for 32 bit.
218 static inline bool extended_addresses_enabled(CPUARMState
*env
)
220 TCR
*tcr
= &env
->cp15
.tcr_el
[arm_is_secure(env
) ? 3 : 1];
221 return arm_el_is_aa64(env
, 1) ||
222 (arm_feature(env
, ARM_FEATURE_LPAE
) && (tcr
->raw_tcr
& TTBCR_EAE
));
225 /* Valid Syndrome Register EC field values */
226 enum arm_exception_class
{
227 EC_UNCATEGORIZED
= 0x00,
229 EC_CP15RTTRAP
= 0x03,
230 EC_CP15RRTTRAP
= 0x04,
231 EC_CP14RTTRAP
= 0x05,
232 EC_CP14DTTRAP
= 0x06,
233 EC_ADVSIMDFPACCESSTRAP
= 0x07,
235 EC_CP14RRTTRAP
= 0x0c,
236 EC_ILLEGALSTATE
= 0x0e,
243 EC_SYSTEMREGISTERTRAP
= 0x18,
245 EC_INSNABORT_SAME_EL
= 0x21,
246 EC_PCALIGNMENT
= 0x22,
248 EC_DATAABORT_SAME_EL
= 0x25,
249 EC_SPALIGNMENT
= 0x26,
250 EC_AA32_FPTRAP
= 0x28,
251 EC_AA64_FPTRAP
= 0x2c,
253 EC_BREAKPOINT
= 0x30,
254 EC_BREAKPOINT_SAME_EL
= 0x31,
255 EC_SOFTWARESTEP
= 0x32,
256 EC_SOFTWARESTEP_SAME_EL
= 0x33,
257 EC_WATCHPOINT
= 0x34,
258 EC_WATCHPOINT_SAME_EL
= 0x35,
260 EC_VECTORCATCH
= 0x3a,
264 #define ARM_EL_EC_SHIFT 26
265 #define ARM_EL_IL_SHIFT 25
266 #define ARM_EL_ISV_SHIFT 24
267 #define ARM_EL_IL (1 << ARM_EL_IL_SHIFT)
268 #define ARM_EL_ISV (1 << ARM_EL_ISV_SHIFT)
270 /* Utility functions for constructing various kinds of syndrome value.
271 * Note that in general we follow the AArch64 syndrome values; in a
272 * few cases the value in HSR for exceptions taken to AArch32 Hyp
273 * mode differs slightly, so if we ever implemented Hyp mode then the
274 * syndrome value would need some massaging on exception entry.
275 * (One example of this is that AArch64 defaults to IL bit set for
276 * exceptions which don't specifically indicate information about the
277 * trapping instruction, whereas AArch32 defaults to IL bit clear.)
279 static inline uint32_t syn_uncategorized(void)
281 return (EC_UNCATEGORIZED
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
;
284 static inline uint32_t syn_aa64_svc(uint32_t imm16
)
286 return (EC_AA64_SVC
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
| (imm16
& 0xffff);
289 static inline uint32_t syn_aa64_hvc(uint32_t imm16
)
291 return (EC_AA64_HVC
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
| (imm16
& 0xffff);
294 static inline uint32_t syn_aa64_smc(uint32_t imm16
)
296 return (EC_AA64_SMC
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
| (imm16
& 0xffff);
299 static inline uint32_t syn_aa32_svc(uint32_t imm16
, bool is_16bit
)
301 return (EC_AA32_SVC
<< ARM_EL_EC_SHIFT
) | (imm16
& 0xffff)
302 | (is_16bit
? 0 : ARM_EL_IL
);
305 static inline uint32_t syn_aa32_hvc(uint32_t imm16
)
307 return (EC_AA32_HVC
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
| (imm16
& 0xffff);
310 static inline uint32_t syn_aa32_smc(void)
312 return (EC_AA32_SMC
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
;
315 static inline uint32_t syn_aa64_bkpt(uint32_t imm16
)
317 return (EC_AA64_BKPT
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
| (imm16
& 0xffff);
320 static inline uint32_t syn_aa32_bkpt(uint32_t imm16
, bool is_16bit
)
322 return (EC_AA32_BKPT
<< ARM_EL_EC_SHIFT
) | (imm16
& 0xffff)
323 | (is_16bit
? 0 : ARM_EL_IL
);
326 static inline uint32_t syn_aa64_sysregtrap(int op0
, int op1
, int op2
,
327 int crn
, int crm
, int rt
,
330 return (EC_SYSTEMREGISTERTRAP
<< ARM_EL_EC_SHIFT
) | ARM_EL_IL
331 | (op0
<< 20) | (op2
<< 17) | (op1
<< 14) | (crn
<< 10) | (rt
<< 5)
332 | (crm
<< 1) | isread
;
335 static inline uint32_t syn_cp14_rt_trap(int cv
, int cond
, int opc1
, int opc2
,
336 int crn
, int crm
, int rt
, int isread
,
339 return (EC_CP14RTTRAP
<< ARM_EL_EC_SHIFT
)
340 | (is_16bit
? 0 : ARM_EL_IL
)
341 | (cv
<< 24) | (cond
<< 20) | (opc2
<< 17) | (opc1
<< 14)
342 | (crn
<< 10) | (rt
<< 5) | (crm
<< 1) | isread
;
345 static inline uint32_t syn_cp15_rt_trap(int cv
, int cond
, int opc1
, int opc2
,
346 int crn
, int crm
, int rt
, int isread
,
349 return (EC_CP15RTTRAP
<< ARM_EL_EC_SHIFT
)
350 | (is_16bit
? 0 : ARM_EL_IL
)
351 | (cv
<< 24) | (cond
<< 20) | (opc2
<< 17) | (opc1
<< 14)
352 | (crn
<< 10) | (rt
<< 5) | (crm
<< 1) | isread
;
355 static inline uint32_t syn_cp14_rrt_trap(int cv
, int cond
, int opc1
, int crm
,
356 int rt
, int rt2
, int isread
,
359 return (EC_CP14RRTTRAP
<< ARM_EL_EC_SHIFT
)
360 | (is_16bit
? 0 : ARM_EL_IL
)
361 | (cv
<< 24) | (cond
<< 20) | (opc1
<< 16)
362 | (rt2
<< 10) | (rt
<< 5) | (crm
<< 1) | isread
;
365 static inline uint32_t syn_cp15_rrt_trap(int cv
, int cond
, int opc1
, int crm
,
366 int rt
, int rt2
, int isread
,
369 return (EC_CP15RRTTRAP
<< ARM_EL_EC_SHIFT
)
370 | (is_16bit
? 0 : ARM_EL_IL
)
371 | (cv
<< 24) | (cond
<< 20) | (opc1
<< 16)
372 | (rt2
<< 10) | (rt
<< 5) | (crm
<< 1) | isread
;
375 static inline uint32_t syn_fp_access_trap(int cv
, int cond
, bool is_16bit
)
377 return (EC_ADVSIMDFPACCESSTRAP
<< ARM_EL_EC_SHIFT
)
378 | (is_16bit
? 0 : ARM_EL_IL
)
379 | (cv
<< 24) | (cond
<< 20);
382 static inline uint32_t syn_insn_abort(int same_el
, int ea
, int s1ptw
, int fsc
)
384 return (EC_INSNABORT
<< ARM_EL_EC_SHIFT
) | (same_el
<< ARM_EL_EC_SHIFT
)
385 | (ea
<< 9) | (s1ptw
<< 7) | fsc
;
388 static inline uint32_t syn_data_abort_no_iss(int same_el
,
389 int ea
, int cm
, int s1ptw
,
392 return (EC_DATAABORT
<< ARM_EL_EC_SHIFT
) | (same_el
<< ARM_EL_EC_SHIFT
)
394 | (ea
<< 9) | (cm
<< 8) | (s1ptw
<< 7) | (wnr
<< 6) | fsc
;
397 static inline uint32_t syn_data_abort_with_iss(int same_el
,
398 int sas
, int sse
, int srt
,
400 int ea
, int cm
, int s1ptw
,
404 return (EC_DATAABORT
<< ARM_EL_EC_SHIFT
) | (same_el
<< ARM_EL_EC_SHIFT
)
405 | (is_16bit
? 0 : ARM_EL_IL
)
406 | ARM_EL_ISV
| (sas
<< 22) | (sse
<< 21) | (srt
<< 16)
407 | (sf
<< 15) | (ar
<< 14)
408 | (ea
<< 9) | (cm
<< 8) | (s1ptw
<< 7) | (wnr
<< 6) | fsc
;
411 static inline uint32_t syn_swstep(int same_el
, int isv
, int ex
)
413 return (EC_SOFTWARESTEP
<< ARM_EL_EC_SHIFT
) | (same_el
<< ARM_EL_EC_SHIFT
)
414 | (isv
<< 24) | (ex
<< 6) | 0x22;
417 static inline uint32_t syn_watchpoint(int same_el
, int cm
, int wnr
)
419 return (EC_WATCHPOINT
<< ARM_EL_EC_SHIFT
) | (same_el
<< ARM_EL_EC_SHIFT
)
420 | (cm
<< 8) | (wnr
<< 6) | 0x22;
423 static inline uint32_t syn_breakpoint(int same_el
)
425 return (EC_BREAKPOINT
<< ARM_EL_EC_SHIFT
) | (same_el
<< ARM_EL_EC_SHIFT
)
429 static inline uint32_t syn_wfx(int cv
, int cond
, int ti
)
431 return (EC_WFX_TRAP
<< ARM_EL_EC_SHIFT
) |
432 (cv
<< 24) | (cond
<< 20) | ti
;
435 /* Update a QEMU watchpoint based on the information the guest has set in the
436 * DBGWCR<n>_EL1 and DBGWVR<n>_EL1 registers.
438 void hw_watchpoint_update(ARMCPU
*cpu
, int n
);
439 /* Update the QEMU watchpoints for every guest watchpoint. This does a
440 * complete delete-and-reinstate of the QEMU watchpoint list and so is
441 * suitable for use after migration or on reset.
443 void hw_watchpoint_update_all(ARMCPU
*cpu
);
444 /* Update a QEMU breakpoint based on the information the guest has set in the
445 * DBGBCR<n>_EL1 and DBGBVR<n>_EL1 registers.
447 void hw_breakpoint_update(ARMCPU
*cpu
, int n
);
448 /* Update the QEMU breakpoints for every guest breakpoint. This does a
449 * complete delete-and-reinstate of the QEMU breakpoint list and so is
450 * suitable for use after migration or on reset.
452 void hw_breakpoint_update_all(ARMCPU
*cpu
);
454 /* Callback function for checking if a watchpoint should trigger. */
455 bool arm_debug_check_watchpoint(CPUState
*cs
, CPUWatchpoint
*wp
);
457 /* Callback function for when a watchpoint or breakpoint triggers. */
458 void arm_debug_excp_handler(CPUState
*cs
);
460 #ifdef CONFIG_USER_ONLY
461 static inline bool arm_is_psci_call(ARMCPU
*cpu
, int excp_type
)
466 /* Return true if the r0/x0 value indicates that this SMC/HVC is a PSCI call. */
467 bool arm_is_psci_call(ARMCPU
*cpu
, int excp_type
);
468 /* Actually handle a PSCI call */
469 void arm_handle_psci_call(ARMCPU
*cpu
);
473 * ARMMMUFaultInfo: Information describing an ARM MMU Fault
474 * @s2addr: Address that caused a fault at stage 2
475 * @stage2: True if we faulted at stage 2
476 * @s1ptw: True if we faulted at stage 2 while doing a stage 1 page-table walk
478 typedef struct ARMMMUFaultInfo ARMMMUFaultInfo
;
479 struct ARMMMUFaultInfo
{
485 /* Do a page table walk and add page to TLB if possible */
486 bool arm_tlb_fill(CPUState
*cpu
, vaddr address
, int rw
, int mmu_idx
,
487 uint32_t *fsr
, ARMMMUFaultInfo
*fi
);
489 /* Return true if the stage 1 translation regime is using LPAE format page
491 bool arm_s1_regime_using_lpae_format(CPUARMState
*env
, ARMMMUIdx mmu_idx
);
493 /* Raise a data fault alignment exception for the specified virtual address */
494 void arm_cpu_do_unaligned_access(CPUState
*cs
, vaddr vaddr
, int is_write
,
495 int is_user
, uintptr_t retaddr
);