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