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