target-arm: Add a Hypervisor Trap exception type
[qemu/ar7.git] / target-arm / cpu.h
blob98610f03522b91a6ee4a3b41ee28d94015f24a39
1 /*
2 * ARM virtual CPU header
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #ifndef CPU_ARM_H
20 #define CPU_ARM_H
22 #include "config.h"
24 #include "kvm-consts.h"
26 #if defined(TARGET_AARCH64)
27 /* AArch64 definitions */
28 # define TARGET_LONG_BITS 64
29 # define ELF_MACHINE EM_AARCH64
30 #else
31 # define TARGET_LONG_BITS 32
32 # define ELF_MACHINE EM_ARM
33 #endif
35 #define CPUArchState struct CPUARMState
37 #include "qemu-common.h"
38 #include "exec/cpu-defs.h"
40 #include "fpu/softfloat.h"
42 #define TARGET_HAS_ICE 1
44 #define EXCP_UDEF 1 /* undefined instruction */
45 #define EXCP_SWI 2 /* software interrupt */
46 #define EXCP_PREFETCH_ABORT 3
47 #define EXCP_DATA_ABORT 4
48 #define EXCP_IRQ 5
49 #define EXCP_FIQ 6
50 #define EXCP_BKPT 7
51 #define EXCP_EXCEPTION_EXIT 8 /* Return from v7M exception. */
52 #define EXCP_KERNEL_TRAP 9 /* Jumped to kernel code page. */
53 #define EXCP_STREX 10
54 #define EXCP_HVC 11 /* HyperVisor Call */
55 #define EXCP_HYP_TRAP 12
57 #define ARMV7M_EXCP_RESET 1
58 #define ARMV7M_EXCP_NMI 2
59 #define ARMV7M_EXCP_HARD 3
60 #define ARMV7M_EXCP_MEM 4
61 #define ARMV7M_EXCP_BUS 5
62 #define ARMV7M_EXCP_USAGE 6
63 #define ARMV7M_EXCP_SVC 11
64 #define ARMV7M_EXCP_DEBUG 12
65 #define ARMV7M_EXCP_PENDSV 14
66 #define ARMV7M_EXCP_SYSTICK 15
68 /* ARM-specific interrupt pending bits. */
69 #define CPU_INTERRUPT_FIQ CPU_INTERRUPT_TGT_EXT_1
71 /* The usual mapping for an AArch64 system register to its AArch32
72 * counterpart is for the 32 bit world to have access to the lower
73 * half only (with writes leaving the upper half untouched). It's
74 * therefore useful to be able to pass TCG the offset of the least
75 * significant half of a uint64_t struct member.
77 #ifdef HOST_WORDS_BIGENDIAN
78 #define offsetoflow32(S, M) (offsetof(S, M) + sizeof(uint32_t))
79 #define offsetofhigh32(S, M) offsetof(S, M)
80 #else
81 #define offsetoflow32(S, M) offsetof(S, M)
82 #define offsetofhigh32(S, M) (offsetof(S, M) + sizeof(uint32_t))
83 #endif
85 /* Meanings of the ARMCPU object's two inbound GPIO lines */
86 #define ARM_CPU_IRQ 0
87 #define ARM_CPU_FIQ 1
89 typedef void ARMWriteCPFunc(void *opaque, int cp_info,
90 int srcreg, int operand, uint32_t value);
91 typedef uint32_t ARMReadCPFunc(void *opaque, int cp_info,
92 int dstreg, int operand);
94 struct arm_boot_info;
96 #define NB_MMU_MODES 2
98 /* We currently assume float and double are IEEE single and double
99 precision respectively.
100 Doing runtime conversions is tricky because VFP registers may contain
101 integer values (eg. as the result of a FTOSI instruction).
102 s<2n> maps to the least significant half of d<n>
103 s<2n+1> maps to the most significant half of d<n>
106 /* CPU state for each instance of a generic timer (in cp15 c14) */
107 typedef struct ARMGenericTimer {
108 uint64_t cval; /* Timer CompareValue register */
109 uint64_t ctl; /* Timer Control register */
110 } ARMGenericTimer;
112 #define GTIMER_PHYS 0
113 #define GTIMER_VIRT 1
114 #define NUM_GTIMERS 2
116 typedef struct CPUARMState {
117 /* Regs for current mode. */
118 uint32_t regs[16];
120 /* 32/64 switch only happens when taking and returning from
121 * exceptions so the overlap semantics are taken care of then
122 * instead of having a complicated union.
124 /* Regs for A64 mode. */
125 uint64_t xregs[32];
126 uint64_t pc;
127 /* PSTATE isn't an architectural register for ARMv8. However, it is
128 * convenient for us to assemble the underlying state into a 32 bit format
129 * identical to the architectural format used for the SPSR. (This is also
130 * what the Linux kernel's 'pstate' field in signal handlers and KVM's
131 * 'pstate' register are.) Of the PSTATE bits:
132 * NZCV are kept in the split out env->CF/VF/NF/ZF, (which have the same
133 * semantics as for AArch32, as described in the comments on each field)
134 * nRW (also known as M[4]) is kept, inverted, in env->aarch64
135 * DAIF (exception masks) are kept in env->daif
136 * all other bits are stored in their correct places in env->pstate
138 uint32_t pstate;
139 uint32_t aarch64; /* 1 if CPU is in aarch64 state; inverse of PSTATE.nRW */
141 /* Frequently accessed CPSR bits are stored separately for efficiency.
142 This contains all the other bits. Use cpsr_{read,write} to access
143 the whole CPSR. */
144 uint32_t uncached_cpsr;
145 uint32_t spsr;
147 /* Banked registers. */
148 uint64_t banked_spsr[8];
149 uint32_t banked_r13[6];
150 uint32_t banked_r14[6];
152 /* These hold r8-r12. */
153 uint32_t usr_regs[5];
154 uint32_t fiq_regs[5];
156 /* cpsr flag cache for faster execution */
157 uint32_t CF; /* 0 or 1 */
158 uint32_t VF; /* V is the bit 31. All other bits are undefined */
159 uint32_t NF; /* N is bit 31. All other bits are undefined. */
160 uint32_t ZF; /* Z set if zero. */
161 uint32_t QF; /* 0 or 1 */
162 uint32_t GE; /* cpsr[19:16] */
163 uint32_t thumb; /* cpsr[5]. 0 = arm mode, 1 = thumb mode. */
164 uint32_t condexec_bits; /* IT bits. cpsr[15:10,26:25]. */
165 uint64_t daif; /* exception masks, in the bits they are in in PSTATE */
167 uint64_t elr_el[4]; /* AArch64 exception link regs */
168 uint64_t sp_el[4]; /* AArch64 banked stack pointers */
170 /* System control coprocessor (cp15) */
171 struct {
172 uint32_t c0_cpuid;
173 uint64_t c0_cssel; /* Cache size selection. */
174 uint64_t c1_sys; /* System control register. */
175 uint64_t c1_coproc; /* Coprocessor access register. */
176 uint32_t c1_xscaleauxcr; /* XScale auxiliary control register. */
177 uint64_t ttbr0_el1; /* MMU translation table base 0. */
178 uint64_t ttbr1_el1; /* MMU translation table base 1. */
179 uint64_t c2_control; /* MMU translation table base control. */
180 uint32_t c2_mask; /* MMU translation table base selection mask. */
181 uint32_t c2_base_mask; /* MMU translation table base 0 mask. */
182 uint32_t c2_data; /* MPU data cachable bits. */
183 uint32_t c2_insn; /* MPU instruction cachable bits. */
184 uint32_t c3; /* MMU domain access control register
185 MPU write buffer control. */
186 uint32_t pmsav5_data_ap; /* PMSAv5 MPU data access permissions */
187 uint32_t pmsav5_insn_ap; /* PMSAv5 MPU insn access permissions */
188 uint64_t hcr_el2; /* Hypervisor configuration register */
189 uint64_t scr_el3; /* Secure configuration register. */
190 uint32_t ifsr_el2; /* Fault status registers. */
191 uint64_t esr_el[4];
192 uint32_t c6_region[8]; /* MPU base/size registers. */
193 uint64_t far_el[4]; /* Fault address registers. */
194 uint64_t par_el1; /* Translation result. */
195 uint32_t c9_insn; /* Cache lockdown registers. */
196 uint32_t c9_data;
197 uint64_t c9_pmcr; /* performance monitor control register */
198 uint64_t c9_pmcnten; /* perf monitor counter enables */
199 uint32_t c9_pmovsr; /* perf monitor overflow status */
200 uint32_t c9_pmxevtyper; /* perf monitor event type */
201 uint32_t c9_pmuserenr; /* perf monitor user enable */
202 uint32_t c9_pminten; /* perf monitor interrupt enables */
203 uint64_t mair_el1;
204 uint64_t vbar_el[4]; /* vector base address register */
205 uint32_t c13_fcse; /* FCSE PID. */
206 uint64_t contextidr_el1; /* Context ID. */
207 uint64_t tpidr_el0; /* User RW Thread register. */
208 uint64_t tpidrro_el0; /* User RO Thread register. */
209 uint64_t tpidr_el1; /* Privileged Thread register. */
210 uint64_t c14_cntfrq; /* Counter Frequency register */
211 uint64_t c14_cntkctl; /* Timer Control register */
212 ARMGenericTimer c14_timer[NUM_GTIMERS];
213 uint32_t c15_cpar; /* XScale Coprocessor Access Register */
214 uint32_t c15_ticonfig; /* TI925T configuration byte. */
215 uint32_t c15_i_max; /* Maximum D-cache dirty line index. */
216 uint32_t c15_i_min; /* Minimum D-cache dirty line index. */
217 uint32_t c15_threadid; /* TI debugger thread-ID. */
218 uint32_t c15_config_base_address; /* SCU base address. */
219 uint32_t c15_diagnostic; /* diagnostic register */
220 uint32_t c15_power_diagnostic;
221 uint32_t c15_power_control; /* power control */
222 uint64_t dbgbvr[16]; /* breakpoint value registers */
223 uint64_t dbgbcr[16]; /* breakpoint control registers */
224 uint64_t dbgwvr[16]; /* watchpoint value registers */
225 uint64_t dbgwcr[16]; /* watchpoint control registers */
226 uint64_t mdscr_el1;
227 /* If the counter is enabled, this stores the last time the counter
228 * was reset. Otherwise it stores the counter value
230 uint64_t c15_ccnt;
231 uint64_t pmccfiltr_el0; /* Performance Monitor Filter Register */
232 } cp15;
234 struct {
235 uint32_t other_sp;
236 uint32_t vecbase;
237 uint32_t basepri;
238 uint32_t control;
239 int current_sp;
240 int exception;
241 int pending_exception;
242 } v7m;
244 /* Information associated with an exception about to be taken:
245 * code which raises an exception must set cs->exception_index and
246 * the relevant parts of this structure; the cpu_do_interrupt function
247 * will then set the guest-visible registers as part of the exception
248 * entry process.
250 struct {
251 uint32_t syndrome; /* AArch64 format syndrome register */
252 uint32_t fsr; /* AArch32 format fault status register info */
253 uint64_t vaddress; /* virtual addr associated with exception, if any */
254 /* If we implement EL2 we will also need to store information
255 * about the intermediate physical address for stage 2 faults.
257 } exception;
259 /* Thumb-2 EE state. */
260 uint32_t teecr;
261 uint32_t teehbr;
263 /* VFP coprocessor state. */
264 struct {
265 /* VFP/Neon register state. Note that the mapping between S, D and Q
266 * views of the register bank differs between AArch64 and AArch32:
267 * In AArch32:
268 * Qn = regs[2n+1]:regs[2n]
269 * Dn = regs[n]
270 * Sn = regs[n/2] bits 31..0 for even n, and bits 63..32 for odd n
271 * (and regs[32] to regs[63] are inaccessible)
272 * In AArch64:
273 * Qn = regs[2n+1]:regs[2n]
274 * Dn = regs[2n]
275 * Sn = regs[2n] bits 31..0
276 * This corresponds to the architecturally defined mapping between
277 * the two execution states, and means we do not need to explicitly
278 * map these registers when changing states.
280 float64 regs[64];
282 uint32_t xregs[16];
283 /* We store these fpcsr fields separately for convenience. */
284 int vec_len;
285 int vec_stride;
287 /* scratch space when Tn are not sufficient. */
288 uint32_t scratch[8];
290 /* fp_status is the "normal" fp status. standard_fp_status retains
291 * values corresponding to the ARM "Standard FPSCR Value", ie
292 * default-NaN, flush-to-zero, round-to-nearest and is used by
293 * any operations (generally Neon) which the architecture defines
294 * as controlled by the standard FPSCR value rather than the FPSCR.
296 * To avoid having to transfer exception bits around, we simply
297 * say that the FPSCR cumulative exception flags are the logical
298 * OR of the flags in the two fp statuses. This relies on the
299 * only thing which needs to read the exception flags being
300 * an explicit FPSCR read.
302 float_status fp_status;
303 float_status standard_fp_status;
304 } vfp;
305 uint64_t exclusive_addr;
306 uint64_t exclusive_val;
307 uint64_t exclusive_high;
308 #if defined(CONFIG_USER_ONLY)
309 uint64_t exclusive_test;
310 uint32_t exclusive_info;
311 #endif
313 /* iwMMXt coprocessor state. */
314 struct {
315 uint64_t regs[16];
316 uint64_t val;
318 uint32_t cregs[16];
319 } iwmmxt;
321 /* For mixed endian mode. */
322 bool bswap_code;
324 #if defined(CONFIG_USER_ONLY)
325 /* For usermode syscall translation. */
326 int eabi;
327 #endif
329 struct CPUBreakpoint *cpu_breakpoint[16];
330 struct CPUWatchpoint *cpu_watchpoint[16];
332 CPU_COMMON
334 /* These fields after the common ones so they are preserved on reset. */
336 /* Internal CPU feature flags. */
337 uint64_t features;
339 void *nvic;
340 const struct arm_boot_info *boot_info;
341 } CPUARMState;
343 #include "cpu-qom.h"
345 ARMCPU *cpu_arm_init(const char *cpu_model);
346 int cpu_arm_exec(CPUARMState *s);
347 uint32_t do_arm_semihosting(CPUARMState *env);
349 static inline bool is_a64(CPUARMState *env)
351 return env->aarch64;
354 /* you can call this signal handler from your SIGBUS and SIGSEGV
355 signal handlers to inform the virtual CPU of exceptions. non zero
356 is returned if the signal was handled by the virtual CPU. */
357 int cpu_arm_signal_handler(int host_signum, void *pinfo,
358 void *puc);
359 int arm_cpu_handle_mmu_fault(CPUState *cpu, vaddr address, int rw,
360 int mmu_idx);
363 * pmccntr_sync
364 * @env: CPUARMState
366 * Synchronises the counter in the PMCCNTR. This must always be called twice,
367 * once before any action that might affect the timer and again afterwards.
368 * The function is used to swap the state of the register if required.
369 * This only happens when not in user mode (!CONFIG_USER_ONLY)
371 void pmccntr_sync(CPUARMState *env);
373 /* SCTLR bit meanings. Several bits have been reused in newer
374 * versions of the architecture; in that case we define constants
375 * for both old and new bit meanings. Code which tests against those
376 * bits should probably check or otherwise arrange that the CPU
377 * is the architectural version it expects.
379 #define SCTLR_M (1U << 0)
380 #define SCTLR_A (1U << 1)
381 #define SCTLR_C (1U << 2)
382 #define SCTLR_W (1U << 3) /* up to v6; RAO in v7 */
383 #define SCTLR_SA (1U << 3)
384 #define SCTLR_P (1U << 4) /* up to v5; RAO in v6 and v7 */
385 #define SCTLR_SA0 (1U << 4) /* v8 onward, AArch64 only */
386 #define SCTLR_D (1U << 5) /* up to v5; RAO in v6 */
387 #define SCTLR_CP15BEN (1U << 5) /* v7 onward */
388 #define SCTLR_L (1U << 6) /* up to v5; RAO in v6 and v7; RAZ in v8 */
389 #define SCTLR_B (1U << 7) /* up to v6; RAZ in v7 */
390 #define SCTLR_ITD (1U << 7) /* v8 onward */
391 #define SCTLR_S (1U << 8) /* up to v6; RAZ in v7 */
392 #define SCTLR_SED (1U << 8) /* v8 onward */
393 #define SCTLR_R (1U << 9) /* up to v6; RAZ in v7 */
394 #define SCTLR_UMA (1U << 9) /* v8 onward, AArch64 only */
395 #define SCTLR_F (1U << 10) /* up to v6 */
396 #define SCTLR_SW (1U << 10) /* v7 onward */
397 #define SCTLR_Z (1U << 11)
398 #define SCTLR_I (1U << 12)
399 #define SCTLR_V (1U << 13)
400 #define SCTLR_RR (1U << 14) /* up to v7 */
401 #define SCTLR_DZE (1U << 14) /* v8 onward, AArch64 only */
402 #define SCTLR_L4 (1U << 15) /* up to v6; RAZ in v7 */
403 #define SCTLR_UCT (1U << 15) /* v8 onward, AArch64 only */
404 #define SCTLR_DT (1U << 16) /* up to ??, RAO in v6 and v7 */
405 #define SCTLR_nTWI (1U << 16) /* v8 onward */
406 #define SCTLR_HA (1U << 17)
407 #define SCTLR_IT (1U << 18) /* up to ??, RAO in v6 and v7 */
408 #define SCTLR_nTWE (1U << 18) /* v8 onward */
409 #define SCTLR_WXN (1U << 19)
410 #define SCTLR_ST (1U << 20) /* up to ??, RAZ in v6 */
411 #define SCTLR_UWXN (1U << 20) /* v7 onward */
412 #define SCTLR_FI (1U << 21)
413 #define SCTLR_U (1U << 22)
414 #define SCTLR_XP (1U << 23) /* up to v6; v7 onward RAO */
415 #define SCTLR_VE (1U << 24) /* up to v7 */
416 #define SCTLR_E0E (1U << 24) /* v8 onward, AArch64 only */
417 #define SCTLR_EE (1U << 25)
418 #define SCTLR_L2 (1U << 26) /* up to v6, RAZ in v7 */
419 #define SCTLR_UCI (1U << 26) /* v8 onward, AArch64 only */
420 #define SCTLR_NMFI (1U << 27)
421 #define SCTLR_TRE (1U << 28)
422 #define SCTLR_AFE (1U << 29)
423 #define SCTLR_TE (1U << 30)
425 #define CPSR_M (0x1fU)
426 #define CPSR_T (1U << 5)
427 #define CPSR_F (1U << 6)
428 #define CPSR_I (1U << 7)
429 #define CPSR_A (1U << 8)
430 #define CPSR_E (1U << 9)
431 #define CPSR_IT_2_7 (0xfc00U)
432 #define CPSR_GE (0xfU << 16)
433 #define CPSR_IL (1U << 20)
434 /* Note that the RESERVED bits include bit 21, which is PSTATE_SS in
435 * an AArch64 SPSR but RES0 in AArch32 SPSR and CPSR. In QEMU we use
436 * env->uncached_cpsr bit 21 to store PSTATE.SS when executing in AArch32,
437 * where it is live state but not accessible to the AArch32 code.
439 #define CPSR_RESERVED (0x7U << 21)
440 #define CPSR_J (1U << 24)
441 #define CPSR_IT_0_1 (3U << 25)
442 #define CPSR_Q (1U << 27)
443 #define CPSR_V (1U << 28)
444 #define CPSR_C (1U << 29)
445 #define CPSR_Z (1U << 30)
446 #define CPSR_N (1U << 31)
447 #define CPSR_NZCV (CPSR_N | CPSR_Z | CPSR_C | CPSR_V)
448 #define CPSR_AIF (CPSR_A | CPSR_I | CPSR_F)
450 #define CPSR_IT (CPSR_IT_0_1 | CPSR_IT_2_7)
451 #define CACHED_CPSR_BITS (CPSR_T | CPSR_AIF | CPSR_GE | CPSR_IT | CPSR_Q \
452 | CPSR_NZCV)
453 /* Bits writable in user mode. */
454 #define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
455 /* Execution state bits. MRS read as zero, MSR writes ignored. */
456 #define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J | CPSR_IL)
457 /* Mask of bits which may be set by exception return copying them from SPSR */
458 #define CPSR_ERET_MASK (~CPSR_RESERVED)
460 #define TTBCR_N (7U << 0) /* TTBCR.EAE==0 */
461 #define TTBCR_T0SZ (7U << 0) /* TTBCR.EAE==1 */
462 #define TTBCR_PD0 (1U << 4)
463 #define TTBCR_PD1 (1U << 5)
464 #define TTBCR_EPD0 (1U << 7)
465 #define TTBCR_IRGN0 (3U << 8)
466 #define TTBCR_ORGN0 (3U << 10)
467 #define TTBCR_SH0 (3U << 12)
468 #define TTBCR_T1SZ (3U << 16)
469 #define TTBCR_A1 (1U << 22)
470 #define TTBCR_EPD1 (1U << 23)
471 #define TTBCR_IRGN1 (3U << 24)
472 #define TTBCR_ORGN1 (3U << 26)
473 #define TTBCR_SH1 (1U << 28)
474 #define TTBCR_EAE (1U << 31)
476 /* Bit definitions for ARMv8 SPSR (PSTATE) format.
477 * Only these are valid when in AArch64 mode; in
478 * AArch32 mode SPSRs are basically CPSR-format.
480 #define PSTATE_SP (1U)
481 #define PSTATE_M (0xFU)
482 #define PSTATE_nRW (1U << 4)
483 #define PSTATE_F (1U << 6)
484 #define PSTATE_I (1U << 7)
485 #define PSTATE_A (1U << 8)
486 #define PSTATE_D (1U << 9)
487 #define PSTATE_IL (1U << 20)
488 #define PSTATE_SS (1U << 21)
489 #define PSTATE_V (1U << 28)
490 #define PSTATE_C (1U << 29)
491 #define PSTATE_Z (1U << 30)
492 #define PSTATE_N (1U << 31)
493 #define PSTATE_NZCV (PSTATE_N | PSTATE_Z | PSTATE_C | PSTATE_V)
494 #define PSTATE_DAIF (PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F)
495 #define CACHED_PSTATE_BITS (PSTATE_NZCV | PSTATE_DAIF)
496 /* Mode values for AArch64 */
497 #define PSTATE_MODE_EL3h 13
498 #define PSTATE_MODE_EL3t 12
499 #define PSTATE_MODE_EL2h 9
500 #define PSTATE_MODE_EL2t 8
501 #define PSTATE_MODE_EL1h 5
502 #define PSTATE_MODE_EL1t 4
503 #define PSTATE_MODE_EL0t 0
505 /* Map EL and handler into a PSTATE_MODE. */
506 static inline unsigned int aarch64_pstate_mode(unsigned int el, bool handler)
508 return (el << 2) | handler;
511 /* Return the current PSTATE value. For the moment we don't support 32<->64 bit
512 * interprocessing, so we don't attempt to sync with the cpsr state used by
513 * the 32 bit decoder.
515 static inline uint32_t pstate_read(CPUARMState *env)
517 int ZF;
519 ZF = (env->ZF == 0);
520 return (env->NF & 0x80000000) | (ZF << 30)
521 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3)
522 | env->pstate | env->daif;
525 static inline void pstate_write(CPUARMState *env, uint32_t val)
527 env->ZF = (~val) & PSTATE_Z;
528 env->NF = val;
529 env->CF = (val >> 29) & 1;
530 env->VF = (val << 3) & 0x80000000;
531 env->daif = val & PSTATE_DAIF;
532 env->pstate = val & ~CACHED_PSTATE_BITS;
535 /* Return the current CPSR value. */
536 uint32_t cpsr_read(CPUARMState *env);
537 /* Set the CPSR. Note that some bits of mask must be all-set or all-clear. */
538 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask);
540 /* Return the current xPSR value. */
541 static inline uint32_t xpsr_read(CPUARMState *env)
543 int ZF;
544 ZF = (env->ZF == 0);
545 return (env->NF & 0x80000000) | (ZF << 30)
546 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
547 | (env->thumb << 24) | ((env->condexec_bits & 3) << 25)
548 | ((env->condexec_bits & 0xfc) << 8)
549 | env->v7m.exception;
552 /* Set the xPSR. Note that some bits of mask must be all-set or all-clear. */
553 static inline void xpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
555 if (mask & CPSR_NZCV) {
556 env->ZF = (~val) & CPSR_Z;
557 env->NF = val;
558 env->CF = (val >> 29) & 1;
559 env->VF = (val << 3) & 0x80000000;
561 if (mask & CPSR_Q)
562 env->QF = ((val & CPSR_Q) != 0);
563 if (mask & (1 << 24))
564 env->thumb = ((val & (1 << 24)) != 0);
565 if (mask & CPSR_IT_0_1) {
566 env->condexec_bits &= ~3;
567 env->condexec_bits |= (val >> 25) & 3;
569 if (mask & CPSR_IT_2_7) {
570 env->condexec_bits &= 3;
571 env->condexec_bits |= (val >> 8) & 0xfc;
573 if (mask & 0x1ff) {
574 env->v7m.exception = val & 0x1ff;
578 #define HCR_VM (1ULL << 0)
579 #define HCR_SWIO (1ULL << 1)
580 #define HCR_PTW (1ULL << 2)
581 #define HCR_FMO (1ULL << 3)
582 #define HCR_IMO (1ULL << 4)
583 #define HCR_AMO (1ULL << 5)
584 #define HCR_VF (1ULL << 6)
585 #define HCR_VI (1ULL << 7)
586 #define HCR_VSE (1ULL << 8)
587 #define HCR_FB (1ULL << 9)
588 #define HCR_BSU_MASK (3ULL << 10)
589 #define HCR_DC (1ULL << 12)
590 #define HCR_TWI (1ULL << 13)
591 #define HCR_TWE (1ULL << 14)
592 #define HCR_TID0 (1ULL << 15)
593 #define HCR_TID1 (1ULL << 16)
594 #define HCR_TID2 (1ULL << 17)
595 #define HCR_TID3 (1ULL << 18)
596 #define HCR_TSC (1ULL << 19)
597 #define HCR_TIDCP (1ULL << 20)
598 #define HCR_TACR (1ULL << 21)
599 #define HCR_TSW (1ULL << 22)
600 #define HCR_TPC (1ULL << 23)
601 #define HCR_TPU (1ULL << 24)
602 #define HCR_TTLB (1ULL << 25)
603 #define HCR_TVM (1ULL << 26)
604 #define HCR_TGE (1ULL << 27)
605 #define HCR_TDZ (1ULL << 28)
606 #define HCR_HCD (1ULL << 29)
607 #define HCR_TRVM (1ULL << 30)
608 #define HCR_RW (1ULL << 31)
609 #define HCR_CD (1ULL << 32)
610 #define HCR_ID (1ULL << 33)
611 #define HCR_MASK ((1ULL << 34) - 1)
613 #define SCR_NS (1U << 0)
614 #define SCR_IRQ (1U << 1)
615 #define SCR_FIQ (1U << 2)
616 #define SCR_EA (1U << 3)
617 #define SCR_FW (1U << 4)
618 #define SCR_AW (1U << 5)
619 #define SCR_NET (1U << 6)
620 #define SCR_SMD (1U << 7)
621 #define SCR_HCE (1U << 8)
622 #define SCR_SIF (1U << 9)
623 #define SCR_RW (1U << 10)
624 #define SCR_ST (1U << 11)
625 #define SCR_TWI (1U << 12)
626 #define SCR_TWE (1U << 13)
627 #define SCR_AARCH32_MASK (0x3fff & ~(SCR_RW | SCR_ST))
628 #define SCR_AARCH64_MASK (0x3fff & ~SCR_NET)
630 /* Return the current FPSCR value. */
631 uint32_t vfp_get_fpscr(CPUARMState *env);
632 void vfp_set_fpscr(CPUARMState *env, uint32_t val);
634 /* For A64 the FPSCR is split into two logically distinct registers,
635 * FPCR and FPSR. However since they still use non-overlapping bits
636 * we store the underlying state in fpscr and just mask on read/write.
638 #define FPSR_MASK 0xf800009f
639 #define FPCR_MASK 0x07f79f00
640 static inline uint32_t vfp_get_fpsr(CPUARMState *env)
642 return vfp_get_fpscr(env) & FPSR_MASK;
645 static inline void vfp_set_fpsr(CPUARMState *env, uint32_t val)
647 uint32_t new_fpscr = (vfp_get_fpscr(env) & ~FPSR_MASK) | (val & FPSR_MASK);
648 vfp_set_fpscr(env, new_fpscr);
651 static inline uint32_t vfp_get_fpcr(CPUARMState *env)
653 return vfp_get_fpscr(env) & FPCR_MASK;
656 static inline void vfp_set_fpcr(CPUARMState *env, uint32_t val)
658 uint32_t new_fpscr = (vfp_get_fpscr(env) & ~FPCR_MASK) | (val & FPCR_MASK);
659 vfp_set_fpscr(env, new_fpscr);
662 enum arm_cpu_mode {
663 ARM_CPU_MODE_USR = 0x10,
664 ARM_CPU_MODE_FIQ = 0x11,
665 ARM_CPU_MODE_IRQ = 0x12,
666 ARM_CPU_MODE_SVC = 0x13,
667 ARM_CPU_MODE_MON = 0x16,
668 ARM_CPU_MODE_ABT = 0x17,
669 ARM_CPU_MODE_HYP = 0x1a,
670 ARM_CPU_MODE_UND = 0x1b,
671 ARM_CPU_MODE_SYS = 0x1f
674 /* VFP system registers. */
675 #define ARM_VFP_FPSID 0
676 #define ARM_VFP_FPSCR 1
677 #define ARM_VFP_MVFR2 5
678 #define ARM_VFP_MVFR1 6
679 #define ARM_VFP_MVFR0 7
680 #define ARM_VFP_FPEXC 8
681 #define ARM_VFP_FPINST 9
682 #define ARM_VFP_FPINST2 10
684 /* iwMMXt coprocessor control registers. */
685 #define ARM_IWMMXT_wCID 0
686 #define ARM_IWMMXT_wCon 1
687 #define ARM_IWMMXT_wCSSF 2
688 #define ARM_IWMMXT_wCASF 3
689 #define ARM_IWMMXT_wCGR0 8
690 #define ARM_IWMMXT_wCGR1 9
691 #define ARM_IWMMXT_wCGR2 10
692 #define ARM_IWMMXT_wCGR3 11
694 /* If adding a feature bit which corresponds to a Linux ELF
695 * HWCAP bit, remember to update the feature-bit-to-hwcap
696 * mapping in linux-user/elfload.c:get_elf_hwcap().
698 enum arm_features {
699 ARM_FEATURE_VFP,
700 ARM_FEATURE_AUXCR, /* ARM1026 Auxiliary control register. */
701 ARM_FEATURE_XSCALE, /* Intel XScale extensions. */
702 ARM_FEATURE_IWMMXT, /* Intel iwMMXt extension. */
703 ARM_FEATURE_V6,
704 ARM_FEATURE_V6K,
705 ARM_FEATURE_V7,
706 ARM_FEATURE_THUMB2,
707 ARM_FEATURE_MPU, /* Only has Memory Protection Unit, not full MMU. */
708 ARM_FEATURE_VFP3,
709 ARM_FEATURE_VFP_FP16,
710 ARM_FEATURE_NEON,
711 ARM_FEATURE_THUMB_DIV, /* divide supported in Thumb encoding */
712 ARM_FEATURE_M, /* Microcontroller profile. */
713 ARM_FEATURE_OMAPCP, /* OMAP specific CP15 ops handling. */
714 ARM_FEATURE_THUMB2EE,
715 ARM_FEATURE_V7MP, /* v7 Multiprocessing Extensions */
716 ARM_FEATURE_V4T,
717 ARM_FEATURE_V5,
718 ARM_FEATURE_STRONGARM,
719 ARM_FEATURE_VAPA, /* cp15 VA to PA lookups */
720 ARM_FEATURE_ARM_DIV, /* divide supported in ARM encoding */
721 ARM_FEATURE_VFP4, /* VFPv4 (implies that NEON is v2) */
722 ARM_FEATURE_GENERIC_TIMER,
723 ARM_FEATURE_MVFR, /* Media and VFP Feature Registers 0 and 1 */
724 ARM_FEATURE_DUMMY_C15_REGS, /* RAZ/WI all of cp15 crn=15 */
725 ARM_FEATURE_CACHE_TEST_CLEAN, /* 926/1026 style test-and-clean ops */
726 ARM_FEATURE_CACHE_DIRTY_REG, /* 1136/1176 cache dirty status register */
727 ARM_FEATURE_CACHE_BLOCK_OPS, /* v6 optional cache block operations */
728 ARM_FEATURE_MPIDR, /* has cp15 MPIDR */
729 ARM_FEATURE_PXN, /* has Privileged Execute Never bit */
730 ARM_FEATURE_LPAE, /* has Large Physical Address Extension */
731 ARM_FEATURE_V8,
732 ARM_FEATURE_AARCH64, /* supports 64 bit mode */
733 ARM_FEATURE_V8_AES, /* implements AES part of v8 Crypto Extensions */
734 ARM_FEATURE_CBAR, /* has cp15 CBAR */
735 ARM_FEATURE_CRC, /* ARMv8 CRC instructions */
736 ARM_FEATURE_CBAR_RO, /* has cp15 CBAR and it is read-only */
737 ARM_FEATURE_EL2, /* has EL2 Virtualization support */
738 ARM_FEATURE_EL3, /* has EL3 Secure monitor support */
739 ARM_FEATURE_V8_SHA1, /* implements SHA1 part of v8 Crypto Extensions */
740 ARM_FEATURE_V8_SHA256, /* implements SHA256 part of v8 Crypto Extensions */
741 ARM_FEATURE_V8_PMULL, /* implements PMULL part of v8 Crypto Extensions */
744 static inline int arm_feature(CPUARMState *env, int feature)
746 return (env->features & (1ULL << feature)) != 0;
749 /* Return true if the specified exception level is running in AArch64 state. */
750 static inline bool arm_el_is_aa64(CPUARMState *env, int el)
752 /* We don't currently support EL2 or EL3, and this isn't valid for EL0
753 * (if we're in EL0, is_a64() is what you want, and if we're not in EL0
754 * then the state of EL0 isn't well defined.)
756 assert(el == 1);
757 /* AArch64-capable CPUs always run with EL1 in AArch64 mode. This
758 * is a QEMU-imposed simplification which we may wish to change later.
759 * If we in future support EL2 and/or EL3, then the state of lower
760 * exception levels is controlled by the HCR.RW and SCR.RW bits.
762 return arm_feature(env, ARM_FEATURE_AARCH64);
765 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf);
766 unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx);
768 /* Interface between CPU and Interrupt controller. */
769 void armv7m_nvic_set_pending(void *opaque, int irq);
770 int armv7m_nvic_acknowledge_irq(void *opaque);
771 void armv7m_nvic_complete_irq(void *opaque, int irq);
773 /* Interface for defining coprocessor registers.
774 * Registers are defined in tables of arm_cp_reginfo structs
775 * which are passed to define_arm_cp_regs().
778 /* When looking up a coprocessor register we look for it
779 * via an integer which encodes all of:
780 * coprocessor number
781 * Crn, Crm, opc1, opc2 fields
782 * 32 or 64 bit register (ie is it accessed via MRC/MCR
783 * or via MRRC/MCRR?)
784 * We allow 4 bits for opc1 because MRRC/MCRR have a 4 bit field.
785 * (In this case crn and opc2 should be zero.)
786 * For AArch64, there is no 32/64 bit size distinction;
787 * instead all registers have a 2 bit op0, 3 bit op1 and op2,
788 * and 4 bit CRn and CRm. The encoding patterns are chosen
789 * to be easy to convert to and from the KVM encodings, and also
790 * so that the hashtable can contain both AArch32 and AArch64
791 * registers (to allow for interprocessing where we might run
792 * 32 bit code on a 64 bit core).
794 /* This bit is private to our hashtable cpreg; in KVM register
795 * IDs the AArch64/32 distinction is the KVM_REG_ARM/ARM64
796 * in the upper bits of the 64 bit ID.
798 #define CP_REG_AA64_SHIFT 28
799 #define CP_REG_AA64_MASK (1 << CP_REG_AA64_SHIFT)
801 #define ENCODE_CP_REG(cp, is64, crn, crm, opc1, opc2) \
802 (((cp) << 16) | ((is64) << 15) | ((crn) << 11) | \
803 ((crm) << 7) | ((opc1) << 3) | (opc2))
805 #define ENCODE_AA64_CP_REG(cp, crn, crm, op0, op1, op2) \
806 (CP_REG_AA64_MASK | \
807 ((cp) << CP_REG_ARM_COPROC_SHIFT) | \
808 ((op0) << CP_REG_ARM64_SYSREG_OP0_SHIFT) | \
809 ((op1) << CP_REG_ARM64_SYSREG_OP1_SHIFT) | \
810 ((crn) << CP_REG_ARM64_SYSREG_CRN_SHIFT) | \
811 ((crm) << CP_REG_ARM64_SYSREG_CRM_SHIFT) | \
812 ((op2) << CP_REG_ARM64_SYSREG_OP2_SHIFT))
814 /* Convert a full 64 bit KVM register ID to the truncated 32 bit
815 * version used as a key for the coprocessor register hashtable
817 static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
819 uint32_t cpregid = kvmid;
820 if ((kvmid & CP_REG_ARCH_MASK) == CP_REG_ARM64) {
821 cpregid |= CP_REG_AA64_MASK;
822 } else if ((kvmid & CP_REG_SIZE_MASK) == CP_REG_SIZE_U64) {
823 cpregid |= (1 << 15);
825 return cpregid;
828 /* Convert a truncated 32 bit hashtable key into the full
829 * 64 bit KVM register ID.
831 static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)
833 uint64_t kvmid;
835 if (cpregid & CP_REG_AA64_MASK) {
836 kvmid = cpregid & ~CP_REG_AA64_MASK;
837 kvmid |= CP_REG_SIZE_U64 | CP_REG_ARM64;
838 } else {
839 kvmid = cpregid & ~(1 << 15);
840 if (cpregid & (1 << 15)) {
841 kvmid |= CP_REG_SIZE_U64 | CP_REG_ARM;
842 } else {
843 kvmid |= CP_REG_SIZE_U32 | CP_REG_ARM;
846 return kvmid;
849 /* ARMCPRegInfo type field bits. If the SPECIAL bit is set this is a
850 * special-behaviour cp reg and bits [15..8] indicate what behaviour
851 * it has. Otherwise it is a simple cp reg, where CONST indicates that
852 * TCG can assume the value to be constant (ie load at translate time)
853 * and 64BIT indicates a 64 bit wide coprocessor register. SUPPRESS_TB_END
854 * indicates that the TB should not be ended after a write to this register
855 * (the default is that the TB ends after cp writes). OVERRIDE permits
856 * a register definition to override a previous definition for the
857 * same (cp, is64, crn, crm, opc1, opc2) tuple: either the new or the
858 * old must have the OVERRIDE bit set.
859 * NO_MIGRATE indicates that this register should be ignored for migration;
860 * (eg because any state is accessed via some other coprocessor register).
861 * IO indicates that this register does I/O and therefore its accesses
862 * need to be surrounded by gen_io_start()/gen_io_end(). In particular,
863 * registers which implement clocks or timers require this.
865 #define ARM_CP_SPECIAL 1
866 #define ARM_CP_CONST 2
867 #define ARM_CP_64BIT 4
868 #define ARM_CP_SUPPRESS_TB_END 8
869 #define ARM_CP_OVERRIDE 16
870 #define ARM_CP_NO_MIGRATE 32
871 #define ARM_CP_IO 64
872 #define ARM_CP_NOP (ARM_CP_SPECIAL | (1 << 8))
873 #define ARM_CP_WFI (ARM_CP_SPECIAL | (2 << 8))
874 #define ARM_CP_NZCV (ARM_CP_SPECIAL | (3 << 8))
875 #define ARM_CP_CURRENTEL (ARM_CP_SPECIAL | (4 << 8))
876 #define ARM_CP_DC_ZVA (ARM_CP_SPECIAL | (5 << 8))
877 #define ARM_LAST_SPECIAL ARM_CP_DC_ZVA
878 /* Used only as a terminator for ARMCPRegInfo lists */
879 #define ARM_CP_SENTINEL 0xffff
880 /* Mask of only the flag bits in a type field */
881 #define ARM_CP_FLAG_MASK 0x7f
883 /* Valid values for ARMCPRegInfo state field, indicating which of
884 * the AArch32 and AArch64 execution states this register is visible in.
885 * If the reginfo doesn't explicitly specify then it is AArch32 only.
886 * If the reginfo is declared to be visible in both states then a second
887 * reginfo is synthesised for the AArch32 view of the AArch64 register,
888 * such that the AArch32 view is the lower 32 bits of the AArch64 one.
889 * Note that we rely on the values of these enums as we iterate through
890 * the various states in some places.
892 enum {
893 ARM_CP_STATE_AA32 = 0,
894 ARM_CP_STATE_AA64 = 1,
895 ARM_CP_STATE_BOTH = 2,
898 /* Return true if cptype is a valid type field. This is used to try to
899 * catch errors where the sentinel has been accidentally left off the end
900 * of a list of registers.
902 static inline bool cptype_valid(int cptype)
904 return ((cptype & ~ARM_CP_FLAG_MASK) == 0)
905 || ((cptype & ARM_CP_SPECIAL) &&
906 ((cptype & ~ARM_CP_FLAG_MASK) <= ARM_LAST_SPECIAL));
909 /* Access rights:
910 * We define bits for Read and Write access for what rev C of the v7-AR ARM ARM
911 * defines as PL0 (user), PL1 (fiq/irq/svc/abt/und/sys, ie privileged), and
912 * PL2 (hyp). The other level which has Read and Write bits is Secure PL1
913 * (ie any of the privileged modes in Secure state, or Monitor mode).
914 * If a register is accessible in one privilege level it's always accessible
915 * in higher privilege levels too. Since "Secure PL1" also follows this rule
916 * (ie anything visible in PL2 is visible in S-PL1, some things are only
917 * visible in S-PL1) but "Secure PL1" is a bit of a mouthful, we bend the
918 * terminology a little and call this PL3.
919 * In AArch64 things are somewhat simpler as the PLx bits line up exactly
920 * with the ELx exception levels.
922 * If access permissions for a register are more complex than can be
923 * described with these bits, then use a laxer set of restrictions, and
924 * do the more restrictive/complex check inside a helper function.
926 #define PL3_R 0x80
927 #define PL3_W 0x40
928 #define PL2_R (0x20 | PL3_R)
929 #define PL2_W (0x10 | PL3_W)
930 #define PL1_R (0x08 | PL2_R)
931 #define PL1_W (0x04 | PL2_W)
932 #define PL0_R (0x02 | PL1_R)
933 #define PL0_W (0x01 | PL1_W)
935 #define PL3_RW (PL3_R | PL3_W)
936 #define PL2_RW (PL2_R | PL2_W)
937 #define PL1_RW (PL1_R | PL1_W)
938 #define PL0_RW (PL0_R | PL0_W)
940 static inline int arm_current_pl(CPUARMState *env)
942 if (env->aarch64) {
943 return extract32(env->pstate, 2, 2);
946 if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
947 return 0;
949 /* We don't currently implement the Virtualization or TrustZone
950 * extensions, so PL2 and PL3 don't exist for us.
952 return 1;
955 typedef struct ARMCPRegInfo ARMCPRegInfo;
957 typedef enum CPAccessResult {
958 /* Access is permitted */
959 CP_ACCESS_OK = 0,
960 /* Access fails due to a configurable trap or enable which would
961 * result in a categorized exception syndrome giving information about
962 * the failing instruction (ie syndrome category 0x3, 0x4, 0x5, 0x6,
963 * 0xc or 0x18).
965 CP_ACCESS_TRAP = 1,
966 /* Access fails and results in an exception syndrome 0x0 ("uncategorized").
967 * Note that this is not a catch-all case -- the set of cases which may
968 * result in this failure is specifically defined by the architecture.
970 CP_ACCESS_TRAP_UNCATEGORIZED = 2,
971 } CPAccessResult;
973 /* Access functions for coprocessor registers. These cannot fail and
974 * may not raise exceptions.
976 typedef uint64_t CPReadFn(CPUARMState *env, const ARMCPRegInfo *opaque);
977 typedef void CPWriteFn(CPUARMState *env, const ARMCPRegInfo *opaque,
978 uint64_t value);
979 /* Access permission check functions for coprocessor registers. */
980 typedef CPAccessResult CPAccessFn(CPUARMState *env, const ARMCPRegInfo *opaque);
981 /* Hook function for register reset */
982 typedef void CPResetFn(CPUARMState *env, const ARMCPRegInfo *opaque);
984 #define CP_ANY 0xff
986 /* Definition of an ARM coprocessor register */
987 struct ARMCPRegInfo {
988 /* Name of register (useful mainly for debugging, need not be unique) */
989 const char *name;
990 /* Location of register: coprocessor number and (crn,crm,opc1,opc2)
991 * tuple. Any of crm, opc1 and opc2 may be CP_ANY to indicate a
992 * 'wildcard' field -- any value of that field in the MRC/MCR insn
993 * will be decoded to this register. The register read and write
994 * callbacks will be passed an ARMCPRegInfo with the crn/crm/opc1/opc2
995 * used by the program, so it is possible to register a wildcard and
996 * then behave differently on read/write if necessary.
997 * For 64 bit registers, only crm and opc1 are relevant; crn and opc2
998 * must both be zero.
999 * For AArch64-visible registers, opc0 is also used.
1000 * Since there are no "coprocessors" in AArch64, cp is purely used as a
1001 * way to distinguish (for KVM's benefit) guest-visible system registers
1002 * from demuxed ones provided to preserve the "no side effects on
1003 * KVM register read/write from QEMU" semantics. cp==0x13 is guest
1004 * visible (to match KVM's encoding); cp==0 will be converted to
1005 * cp==0x13 when the ARMCPRegInfo is registered, for convenience.
1007 uint8_t cp;
1008 uint8_t crn;
1009 uint8_t crm;
1010 uint8_t opc0;
1011 uint8_t opc1;
1012 uint8_t opc2;
1013 /* Execution state in which this register is visible: ARM_CP_STATE_* */
1014 int state;
1015 /* Register type: ARM_CP_* bits/values */
1016 int type;
1017 /* Access rights: PL*_[RW] */
1018 int access;
1019 /* The opaque pointer passed to define_arm_cp_regs_with_opaque() when
1020 * this register was defined: can be used to hand data through to the
1021 * register read/write functions, since they are passed the ARMCPRegInfo*.
1023 void *opaque;
1024 /* Value of this register, if it is ARM_CP_CONST. Otherwise, if
1025 * fieldoffset is non-zero, the reset value of the register.
1027 uint64_t resetvalue;
1028 /* Offset of the field in CPUARMState for this register. This is not
1029 * needed if either:
1030 * 1. type is ARM_CP_CONST or one of the ARM_CP_SPECIALs
1031 * 2. both readfn and writefn are specified
1033 ptrdiff_t fieldoffset; /* offsetof(CPUARMState, field) */
1034 /* Function for making any access checks for this register in addition to
1035 * those specified by the 'access' permissions bits. If NULL, no extra
1036 * checks required. The access check is performed at runtime, not at
1037 * translate time.
1039 CPAccessFn *accessfn;
1040 /* Function for handling reads of this register. If NULL, then reads
1041 * will be done by loading from the offset into CPUARMState specified
1042 * by fieldoffset.
1044 CPReadFn *readfn;
1045 /* Function for handling writes of this register. If NULL, then writes
1046 * will be done by writing to the offset into CPUARMState specified
1047 * by fieldoffset.
1049 CPWriteFn *writefn;
1050 /* Function for doing a "raw" read; used when we need to copy
1051 * coprocessor state to the kernel for KVM or out for
1052 * migration. This only needs to be provided if there is also a
1053 * readfn and it has side effects (for instance clear-on-read bits).
1055 CPReadFn *raw_readfn;
1056 /* Function for doing a "raw" write; used when we need to copy KVM
1057 * kernel coprocessor state into userspace, or for inbound
1058 * migration. This only needs to be provided if there is also a
1059 * writefn and it masks out "unwritable" bits or has write-one-to-clear
1060 * or similar behaviour.
1062 CPWriteFn *raw_writefn;
1063 /* Function for resetting the register. If NULL, then reset will be done
1064 * by writing resetvalue to the field specified in fieldoffset. If
1065 * fieldoffset is 0 then no reset will be done.
1067 CPResetFn *resetfn;
1070 /* Macros which are lvalues for the field in CPUARMState for the
1071 * ARMCPRegInfo *ri.
1073 #define CPREG_FIELD32(env, ri) \
1074 (*(uint32_t *)((char *)(env) + (ri)->fieldoffset))
1075 #define CPREG_FIELD64(env, ri) \
1076 (*(uint64_t *)((char *)(env) + (ri)->fieldoffset))
1078 #define REGINFO_SENTINEL { .type = ARM_CP_SENTINEL }
1080 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1081 const ARMCPRegInfo *regs, void *opaque);
1082 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
1083 const ARMCPRegInfo *regs, void *opaque);
1084 static inline void define_arm_cp_regs(ARMCPU *cpu, const ARMCPRegInfo *regs)
1086 define_arm_cp_regs_with_opaque(cpu, regs, 0);
1088 static inline void define_one_arm_cp_reg(ARMCPU *cpu, const ARMCPRegInfo *regs)
1090 define_one_arm_cp_reg_with_opaque(cpu, regs, 0);
1092 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp);
1094 /* CPWriteFn that can be used to implement writes-ignored behaviour */
1095 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1096 uint64_t value);
1097 /* CPReadFn that can be used for read-as-zero behaviour */
1098 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri);
1100 /* CPResetFn that does nothing, for use if no reset is required even
1101 * if fieldoffset is non zero.
1103 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque);
1105 /* Return true if this reginfo struct's field in the cpu state struct
1106 * is 64 bits wide.
1108 static inline bool cpreg_field_is_64bit(const ARMCPRegInfo *ri)
1110 return (ri->state == ARM_CP_STATE_AA64) || (ri->type & ARM_CP_64BIT);
1113 static inline bool cp_access_ok(int current_pl,
1114 const ARMCPRegInfo *ri, int isread)
1116 return (ri->access >> ((current_pl * 2) + isread)) & 1;
1120 * write_list_to_cpustate
1121 * @cpu: ARMCPU
1123 * For each register listed in the ARMCPU cpreg_indexes list, write
1124 * its value from the cpreg_values list into the ARMCPUState structure.
1125 * This updates TCG's working data structures from KVM data or
1126 * from incoming migration state.
1128 * Returns: true if all register values were updated correctly,
1129 * false if some register was unknown or could not be written.
1130 * Note that we do not stop early on failure -- we will attempt
1131 * writing all registers in the list.
1133 bool write_list_to_cpustate(ARMCPU *cpu);
1136 * write_cpustate_to_list:
1137 * @cpu: ARMCPU
1139 * For each register listed in the ARMCPU cpreg_indexes list, write
1140 * its value from the ARMCPUState structure into the cpreg_values list.
1141 * This is used to copy info from TCG's working data structures into
1142 * KVM or for outbound migration.
1144 * Returns: true if all register values were read correctly,
1145 * false if some register was unknown or could not be read.
1146 * Note that we do not stop early on failure -- we will attempt
1147 * reading all registers in the list.
1149 bool write_cpustate_to_list(ARMCPU *cpu);
1151 /* Does the core conform to the the "MicroController" profile. e.g. Cortex-M3.
1152 Note the M in older cores (eg. ARM7TDMI) stands for Multiply. These are
1153 conventional cores (ie. Application or Realtime profile). */
1155 #define IS_M(env) arm_feature(env, ARM_FEATURE_M)
1157 #define ARM_CPUID_TI915T 0x54029152
1158 #define ARM_CPUID_TI925T 0x54029252
1160 #if defined(CONFIG_USER_ONLY)
1161 #define TARGET_PAGE_BITS 12
1162 #else
1163 /* The ARM MMU allows 1k pages. */
1164 /* ??? Linux doesn't actually use these, and they're deprecated in recent
1165 architecture revisions. Maybe a configure option to disable them. */
1166 #define TARGET_PAGE_BITS 10
1167 #endif
1169 #if defined(TARGET_AARCH64)
1170 # define TARGET_PHYS_ADDR_SPACE_BITS 48
1171 # define TARGET_VIRT_ADDR_SPACE_BITS 64
1172 #else
1173 # define TARGET_PHYS_ADDR_SPACE_BITS 40
1174 # define TARGET_VIRT_ADDR_SPACE_BITS 32
1175 #endif
1177 static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx)
1179 CPUARMState *env = cs->env_ptr;
1180 unsigned int cur_el = arm_current_pl(env);
1181 unsigned int target_el = arm_excp_target_el(cs, excp_idx);
1183 /* Don't take exceptions if they target a lower EL. */
1184 if (cur_el > target_el) {
1185 return false;
1188 switch (excp_idx) {
1189 case EXCP_FIQ:
1190 return !(env->daif & PSTATE_F);
1191 case EXCP_IRQ:
1192 return !(env->daif & PSTATE_I)
1193 && (!IS_M(env) || env->regs[15] < 0xfffffff0);
1194 default:
1195 g_assert_not_reached();
1199 static inline CPUARMState *cpu_init(const char *cpu_model)
1201 ARMCPU *cpu = cpu_arm_init(cpu_model);
1202 if (cpu) {
1203 return &cpu->env;
1205 return NULL;
1208 #define cpu_exec cpu_arm_exec
1209 #define cpu_gen_code cpu_arm_gen_code
1210 #define cpu_signal_handler cpu_arm_signal_handler
1211 #define cpu_list arm_cpu_list
1213 /* MMU modes definitions */
1214 #define MMU_MODE0_SUFFIX _user
1215 #define MMU_MODE1_SUFFIX _kernel
1216 #define MMU_USER_IDX 0
1217 static inline int cpu_mmu_index (CPUARMState *env)
1219 return arm_current_pl(env);
1222 /* Return the Exception Level targeted by debug exceptions;
1223 * currently always EL1 since we don't implement EL2 or EL3.
1225 static inline int arm_debug_target_el(CPUARMState *env)
1227 return 1;
1230 static inline bool aa64_generate_debug_exceptions(CPUARMState *env)
1232 if (arm_current_pl(env) == arm_debug_target_el(env)) {
1233 if ((extract32(env->cp15.mdscr_el1, 13, 1) == 0)
1234 || (env->daif & PSTATE_D)) {
1235 return false;
1238 return true;
1241 static inline bool aa32_generate_debug_exceptions(CPUARMState *env)
1243 if (arm_current_pl(env) == 0 && arm_el_is_aa64(env, 1)) {
1244 return aa64_generate_debug_exceptions(env);
1246 return arm_current_pl(env) != 2;
1249 /* Return true if debugging exceptions are currently enabled.
1250 * This corresponds to what in ARM ARM pseudocode would be
1251 * if UsingAArch32() then
1252 * return AArch32.GenerateDebugExceptions()
1253 * else
1254 * return AArch64.GenerateDebugExceptions()
1255 * We choose to push the if() down into this function for clarity,
1256 * since the pseudocode has it at all callsites except for the one in
1257 * CheckSoftwareStep(), where it is elided because both branches would
1258 * always return the same value.
1260 * Parts of the pseudocode relating to EL2 and EL3 are omitted because we
1261 * don't yet implement those exception levels or their associated trap bits.
1263 static inline bool arm_generate_debug_exceptions(CPUARMState *env)
1265 if (env->aarch64) {
1266 return aa64_generate_debug_exceptions(env);
1267 } else {
1268 return aa32_generate_debug_exceptions(env);
1272 /* Is single-stepping active? (Note that the "is EL_D AArch64?" check
1273 * implicitly means this always returns false in pre-v8 CPUs.)
1275 static inline bool arm_singlestep_active(CPUARMState *env)
1277 return extract32(env->cp15.mdscr_el1, 0, 1)
1278 && arm_el_is_aa64(env, arm_debug_target_el(env))
1279 && arm_generate_debug_exceptions(env);
1282 #include "exec/cpu-all.h"
1284 /* Bit usage in the TB flags field: bit 31 indicates whether we are
1285 * in 32 or 64 bit mode. The meaning of the other bits depends on that.
1287 #define ARM_TBFLAG_AARCH64_STATE_SHIFT 31
1288 #define ARM_TBFLAG_AARCH64_STATE_MASK (1U << ARM_TBFLAG_AARCH64_STATE_SHIFT)
1290 /* Bit usage when in AArch32 state: */
1291 #define ARM_TBFLAG_THUMB_SHIFT 0
1292 #define ARM_TBFLAG_THUMB_MASK (1 << ARM_TBFLAG_THUMB_SHIFT)
1293 #define ARM_TBFLAG_VECLEN_SHIFT 1
1294 #define ARM_TBFLAG_VECLEN_MASK (0x7 << ARM_TBFLAG_VECLEN_SHIFT)
1295 #define ARM_TBFLAG_VECSTRIDE_SHIFT 4
1296 #define ARM_TBFLAG_VECSTRIDE_MASK (0x3 << ARM_TBFLAG_VECSTRIDE_SHIFT)
1297 #define ARM_TBFLAG_PRIV_SHIFT 6
1298 #define ARM_TBFLAG_PRIV_MASK (1 << ARM_TBFLAG_PRIV_SHIFT)
1299 #define ARM_TBFLAG_VFPEN_SHIFT 7
1300 #define ARM_TBFLAG_VFPEN_MASK (1 << ARM_TBFLAG_VFPEN_SHIFT)
1301 #define ARM_TBFLAG_CONDEXEC_SHIFT 8
1302 #define ARM_TBFLAG_CONDEXEC_MASK (0xff << ARM_TBFLAG_CONDEXEC_SHIFT)
1303 #define ARM_TBFLAG_BSWAP_CODE_SHIFT 16
1304 #define ARM_TBFLAG_BSWAP_CODE_MASK (1 << ARM_TBFLAG_BSWAP_CODE_SHIFT)
1305 #define ARM_TBFLAG_CPACR_FPEN_SHIFT 17
1306 #define ARM_TBFLAG_CPACR_FPEN_MASK (1 << ARM_TBFLAG_CPACR_FPEN_SHIFT)
1307 #define ARM_TBFLAG_SS_ACTIVE_SHIFT 18
1308 #define ARM_TBFLAG_SS_ACTIVE_MASK (1 << ARM_TBFLAG_SS_ACTIVE_SHIFT)
1309 #define ARM_TBFLAG_PSTATE_SS_SHIFT 19
1310 #define ARM_TBFLAG_PSTATE_SS_MASK (1 << ARM_TBFLAG_PSTATE_SS_SHIFT)
1311 /* We store the bottom two bits of the CPAR as TB flags and handle
1312 * checks on the other bits at runtime
1314 #define ARM_TBFLAG_XSCALE_CPAR_SHIFT 20
1315 #define ARM_TBFLAG_XSCALE_CPAR_MASK (3 << ARM_TBFLAG_XSCALE_CPAR_SHIFT)
1317 /* Bit usage when in AArch64 state */
1318 #define ARM_TBFLAG_AA64_EL_SHIFT 0
1319 #define ARM_TBFLAG_AA64_EL_MASK (0x3 << ARM_TBFLAG_AA64_EL_SHIFT)
1320 #define ARM_TBFLAG_AA64_FPEN_SHIFT 2
1321 #define ARM_TBFLAG_AA64_FPEN_MASK (1 << ARM_TBFLAG_AA64_FPEN_SHIFT)
1322 #define ARM_TBFLAG_AA64_SS_ACTIVE_SHIFT 3
1323 #define ARM_TBFLAG_AA64_SS_ACTIVE_MASK (1 << ARM_TBFLAG_AA64_SS_ACTIVE_SHIFT)
1324 #define ARM_TBFLAG_AA64_PSTATE_SS_SHIFT 4
1325 #define ARM_TBFLAG_AA64_PSTATE_SS_MASK (1 << ARM_TBFLAG_AA64_PSTATE_SS_SHIFT)
1327 /* some convenience accessor macros */
1328 #define ARM_TBFLAG_AARCH64_STATE(F) \
1329 (((F) & ARM_TBFLAG_AARCH64_STATE_MASK) >> ARM_TBFLAG_AARCH64_STATE_SHIFT)
1330 #define ARM_TBFLAG_THUMB(F) \
1331 (((F) & ARM_TBFLAG_THUMB_MASK) >> ARM_TBFLAG_THUMB_SHIFT)
1332 #define ARM_TBFLAG_VECLEN(F) \
1333 (((F) & ARM_TBFLAG_VECLEN_MASK) >> ARM_TBFLAG_VECLEN_SHIFT)
1334 #define ARM_TBFLAG_VECSTRIDE(F) \
1335 (((F) & ARM_TBFLAG_VECSTRIDE_MASK) >> ARM_TBFLAG_VECSTRIDE_SHIFT)
1336 #define ARM_TBFLAG_PRIV(F) \
1337 (((F) & ARM_TBFLAG_PRIV_MASK) >> ARM_TBFLAG_PRIV_SHIFT)
1338 #define ARM_TBFLAG_VFPEN(F) \
1339 (((F) & ARM_TBFLAG_VFPEN_MASK) >> ARM_TBFLAG_VFPEN_SHIFT)
1340 #define ARM_TBFLAG_CONDEXEC(F) \
1341 (((F) & ARM_TBFLAG_CONDEXEC_MASK) >> ARM_TBFLAG_CONDEXEC_SHIFT)
1342 #define ARM_TBFLAG_BSWAP_CODE(F) \
1343 (((F) & ARM_TBFLAG_BSWAP_CODE_MASK) >> ARM_TBFLAG_BSWAP_CODE_SHIFT)
1344 #define ARM_TBFLAG_CPACR_FPEN(F) \
1345 (((F) & ARM_TBFLAG_CPACR_FPEN_MASK) >> ARM_TBFLAG_CPACR_FPEN_SHIFT)
1346 #define ARM_TBFLAG_SS_ACTIVE(F) \
1347 (((F) & ARM_TBFLAG_SS_ACTIVE_MASK) >> ARM_TBFLAG_SS_ACTIVE_SHIFT)
1348 #define ARM_TBFLAG_PSTATE_SS(F) \
1349 (((F) & ARM_TBFLAG_PSTATE_SS_MASK) >> ARM_TBFLAG_PSTATE_SS_SHIFT)
1350 #define ARM_TBFLAG_XSCALE_CPAR(F) \
1351 (((F) & ARM_TBFLAG_XSCALE_CPAR_MASK) >> ARM_TBFLAG_XSCALE_CPAR_SHIFT)
1352 #define ARM_TBFLAG_AA64_EL(F) \
1353 (((F) & ARM_TBFLAG_AA64_EL_MASK) >> ARM_TBFLAG_AA64_EL_SHIFT)
1354 #define ARM_TBFLAG_AA64_FPEN(F) \
1355 (((F) & ARM_TBFLAG_AA64_FPEN_MASK) >> ARM_TBFLAG_AA64_FPEN_SHIFT)
1356 #define ARM_TBFLAG_AA64_SS_ACTIVE(F) \
1357 (((F) & ARM_TBFLAG_AA64_SS_ACTIVE_MASK) >> ARM_TBFLAG_AA64_SS_ACTIVE_SHIFT)
1358 #define ARM_TBFLAG_AA64_PSTATE_SS(F) \
1359 (((F) & ARM_TBFLAG_AA64_PSTATE_SS_MASK) >> ARM_TBFLAG_AA64_PSTATE_SS_SHIFT)
1361 static inline void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
1362 target_ulong *cs_base, int *flags)
1364 int fpen;
1366 if (arm_feature(env, ARM_FEATURE_V6)) {
1367 fpen = extract32(env->cp15.c1_coproc, 20, 2);
1368 } else {
1369 /* CPACR doesn't exist before v6, so VFP is always accessible */
1370 fpen = 3;
1373 if (is_a64(env)) {
1374 *pc = env->pc;
1375 *flags = ARM_TBFLAG_AARCH64_STATE_MASK
1376 | (arm_current_pl(env) << ARM_TBFLAG_AA64_EL_SHIFT);
1377 if (fpen == 3 || (fpen == 1 && arm_current_pl(env) != 0)) {
1378 *flags |= ARM_TBFLAG_AA64_FPEN_MASK;
1380 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
1381 * states defined in the ARM ARM for software singlestep:
1382 * SS_ACTIVE PSTATE.SS State
1383 * 0 x Inactive (the TB flag for SS is always 0)
1384 * 1 0 Active-pending
1385 * 1 1 Active-not-pending
1387 if (arm_singlestep_active(env)) {
1388 *flags |= ARM_TBFLAG_AA64_SS_ACTIVE_MASK;
1389 if (env->pstate & PSTATE_SS) {
1390 *flags |= ARM_TBFLAG_AA64_PSTATE_SS_MASK;
1393 } else {
1394 int privmode;
1395 *pc = env->regs[15];
1396 *flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
1397 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
1398 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
1399 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
1400 | (env->bswap_code << ARM_TBFLAG_BSWAP_CODE_SHIFT);
1401 if (arm_feature(env, ARM_FEATURE_M)) {
1402 privmode = !((env->v7m.exception == 0) && (env->v7m.control & 1));
1403 } else {
1404 privmode = (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR;
1406 if (privmode) {
1407 *flags |= ARM_TBFLAG_PRIV_MASK;
1409 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
1410 || arm_el_is_aa64(env, 1)) {
1411 *flags |= ARM_TBFLAG_VFPEN_MASK;
1413 if (fpen == 3 || (fpen == 1 && arm_current_pl(env) != 0)) {
1414 *flags |= ARM_TBFLAG_CPACR_FPEN_MASK;
1416 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
1417 * states defined in the ARM ARM for software singlestep:
1418 * SS_ACTIVE PSTATE.SS State
1419 * 0 x Inactive (the TB flag for SS is always 0)
1420 * 1 0 Active-pending
1421 * 1 1 Active-not-pending
1423 if (arm_singlestep_active(env)) {
1424 *flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
1425 if (env->uncached_cpsr & PSTATE_SS) {
1426 *flags |= ARM_TBFLAG_PSTATE_SS_MASK;
1429 *flags |= (extract32(env->cp15.c15_cpar, 0, 2)
1430 << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
1433 *cs_base = 0;
1436 #include "exec/exec-all.h"
1438 static inline void cpu_pc_from_tb(CPUARMState *env, TranslationBlock *tb)
1440 if (ARM_TBFLAG_AARCH64_STATE(tb->flags)) {
1441 env->pc = tb->pc;
1442 } else {
1443 env->regs[15] = tb->pc;
1447 #endif