]> Git Repo - qemu.git/blob - target/arm/cpu.h
target/arm: Add MMU indexes for secure v8M
[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
20 #ifndef ARM_CPU_H
21 #define ARM_CPU_H
22
23 #include "kvm-consts.h"
24 #include "hw/registerfields.h"
25
26 #if defined(TARGET_AARCH64)
27   /* AArch64 definitions */
28 #  define TARGET_LONG_BITS 64
29 #else
30 #  define TARGET_LONG_BITS 32
31 #endif
32
33 /* ARM processors have a weak memory model */
34 #define TCG_GUEST_DEFAULT_MO      (0)
35
36 #define CPUArchState struct CPUARMState
37
38 #include "qemu-common.h"
39 #include "cpu-qom.h"
40 #include "exec/cpu-defs.h"
41
42 #include "fpu/softfloat.h"
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_HVC            11   /* HyperVisor Call */
54 #define EXCP_HYP_TRAP       12
55 #define EXCP_SMC            13   /* Secure Monitor Call */
56 #define EXCP_VIRQ           14
57 #define EXCP_VFIQ           15
58 #define EXCP_SEMIHOST       16   /* semihosting call */
59 #define EXCP_NOCP           17   /* v7M NOCP UsageFault */
60 #define EXCP_INVSTATE       18   /* v7M INVSTATE UsageFault */
61 /* NB: add new EXCP_ defines to the array in arm_log_exception() too */
62
63 #define ARMV7M_EXCP_RESET   1
64 #define ARMV7M_EXCP_NMI     2
65 #define ARMV7M_EXCP_HARD    3
66 #define ARMV7M_EXCP_MEM     4
67 #define ARMV7M_EXCP_BUS     5
68 #define ARMV7M_EXCP_USAGE   6
69 #define ARMV7M_EXCP_SECURE  7
70 #define ARMV7M_EXCP_SVC     11
71 #define ARMV7M_EXCP_DEBUG   12
72 #define ARMV7M_EXCP_PENDSV  14
73 #define ARMV7M_EXCP_SYSTICK 15
74
75 /* ARM-specific interrupt pending bits.  */
76 #define CPU_INTERRUPT_FIQ   CPU_INTERRUPT_TGT_EXT_1
77 #define CPU_INTERRUPT_VIRQ  CPU_INTERRUPT_TGT_EXT_2
78 #define CPU_INTERRUPT_VFIQ  CPU_INTERRUPT_TGT_EXT_3
79
80 /* The usual mapping for an AArch64 system register to its AArch32
81  * counterpart is for the 32 bit world to have access to the lower
82  * half only (with writes leaving the upper half untouched). It's
83  * therefore useful to be able to pass TCG the offset of the least
84  * significant half of a uint64_t struct member.
85  */
86 #ifdef HOST_WORDS_BIGENDIAN
87 #define offsetoflow32(S, M) (offsetof(S, M) + sizeof(uint32_t))
88 #define offsetofhigh32(S, M) offsetof(S, M)
89 #else
90 #define offsetoflow32(S, M) offsetof(S, M)
91 #define offsetofhigh32(S, M) (offsetof(S, M) + sizeof(uint32_t))
92 #endif
93
94 /* Meanings of the ARMCPU object's four inbound GPIO lines */
95 #define ARM_CPU_IRQ 0
96 #define ARM_CPU_FIQ 1
97 #define ARM_CPU_VIRQ 2
98 #define ARM_CPU_VFIQ 3
99
100 #define NB_MMU_MODES 7
101 /* ARM-specific extra insn start words:
102  * 1: Conditional execution bits
103  * 2: Partial exception syndrome for data aborts
104  */
105 #define TARGET_INSN_START_EXTRA_WORDS 2
106
107 /* The 2nd extra word holding syndrome info for data aborts does not use
108  * the upper 6 bits nor the lower 14 bits. We mask and shift it down to
109  * help the sleb128 encoder do a better job.
110  * When restoring the CPU state, we shift it back up.
111  */
112 #define ARM_INSN_START_WORD2_MASK ((1 << 26) - 1)
113 #define ARM_INSN_START_WORD2_SHIFT 14
114
115 /* We currently assume float and double are IEEE single and double
116    precision respectively.
117    Doing runtime conversions is tricky because VFP registers may contain
118    integer values (eg. as the result of a FTOSI instruction).
119    s<2n> maps to the least significant half of d<n>
120    s<2n+1> maps to the most significant half of d<n>
121  */
122
123 /* CPU state for each instance of a generic timer (in cp15 c14) */
124 typedef struct ARMGenericTimer {
125     uint64_t cval; /* Timer CompareValue register */
126     uint64_t ctl; /* Timer Control register */
127 } ARMGenericTimer;
128
129 #define GTIMER_PHYS 0
130 #define GTIMER_VIRT 1
131 #define GTIMER_HYP  2
132 #define GTIMER_SEC  3
133 #define NUM_GTIMERS 4
134
135 typedef struct {
136     uint64_t raw_tcr;
137     uint32_t mask;
138     uint32_t base_mask;
139 } TCR;
140
141 typedef struct CPUARMState {
142     /* Regs for current mode.  */
143     uint32_t regs[16];
144
145     /* 32/64 switch only happens when taking and returning from
146      * exceptions so the overlap semantics are taken care of then
147      * instead of having a complicated union.
148      */
149     /* Regs for A64 mode.  */
150     uint64_t xregs[32];
151     uint64_t pc;
152     /* PSTATE isn't an architectural register for ARMv8. However, it is
153      * convenient for us to assemble the underlying state into a 32 bit format
154      * identical to the architectural format used for the SPSR. (This is also
155      * what the Linux kernel's 'pstate' field in signal handlers and KVM's
156      * 'pstate' register are.) Of the PSTATE bits:
157      *  NZCV are kept in the split out env->CF/VF/NF/ZF, (which have the same
158      *    semantics as for AArch32, as described in the comments on each field)
159      *  nRW (also known as M[4]) is kept, inverted, in env->aarch64
160      *  DAIF (exception masks) are kept in env->daif
161      *  all other bits are stored in their correct places in env->pstate
162      */
163     uint32_t pstate;
164     uint32_t aarch64; /* 1 if CPU is in aarch64 state; inverse of PSTATE.nRW */
165
166     /* Frequently accessed CPSR bits are stored separately for efficiency.
167        This contains all the other bits.  Use cpsr_{read,write} to access
168        the whole CPSR.  */
169     uint32_t uncached_cpsr;
170     uint32_t spsr;
171
172     /* Banked registers.  */
173     uint64_t banked_spsr[8];
174     uint32_t banked_r13[8];
175     uint32_t banked_r14[8];
176
177     /* These hold r8-r12.  */
178     uint32_t usr_regs[5];
179     uint32_t fiq_regs[5];
180
181     /* cpsr flag cache for faster execution */
182     uint32_t CF; /* 0 or 1 */
183     uint32_t VF; /* V is the bit 31. All other bits are undefined */
184     uint32_t NF; /* N is bit 31. All other bits are undefined.  */
185     uint32_t ZF; /* Z set if zero.  */
186     uint32_t QF; /* 0 or 1 */
187     uint32_t GE; /* cpsr[19:16] */
188     uint32_t thumb; /* cpsr[5]. 0 = arm mode, 1 = thumb mode. */
189     uint32_t condexec_bits; /* IT bits.  cpsr[15:10,26:25].  */
190     uint64_t daif; /* exception masks, in the bits they are in PSTATE */
191
192     uint64_t elr_el[4]; /* AArch64 exception link regs  */
193     uint64_t sp_el[4]; /* AArch64 banked stack pointers */
194
195     /* System control coprocessor (cp15) */
196     struct {
197         uint32_t c0_cpuid;
198         union { /* Cache size selection */
199             struct {
200                 uint64_t _unused_csselr0;
201                 uint64_t csselr_ns;
202                 uint64_t _unused_csselr1;
203                 uint64_t csselr_s;
204             };
205             uint64_t csselr_el[4];
206         };
207         union { /* System control register. */
208             struct {
209                 uint64_t _unused_sctlr;
210                 uint64_t sctlr_ns;
211                 uint64_t hsctlr;
212                 uint64_t sctlr_s;
213             };
214             uint64_t sctlr_el[4];
215         };
216         uint64_t cpacr_el1; /* Architectural feature access control register */
217         uint64_t cptr_el[4];  /* ARMv8 feature trap registers */
218         uint32_t c1_xscaleauxcr; /* XScale auxiliary control register.  */
219         uint64_t sder; /* Secure debug enable register. */
220         uint32_t nsacr; /* Non-secure access control register. */
221         union { /* MMU translation table base 0. */
222             struct {
223                 uint64_t _unused_ttbr0_0;
224                 uint64_t ttbr0_ns;
225                 uint64_t _unused_ttbr0_1;
226                 uint64_t ttbr0_s;
227             };
228             uint64_t ttbr0_el[4];
229         };
230         union { /* MMU translation table base 1. */
231             struct {
232                 uint64_t _unused_ttbr1_0;
233                 uint64_t ttbr1_ns;
234                 uint64_t _unused_ttbr1_1;
235                 uint64_t ttbr1_s;
236             };
237             uint64_t ttbr1_el[4];
238         };
239         uint64_t vttbr_el2; /* Virtualization Translation Table Base.  */
240         /* MMU translation table base control. */
241         TCR tcr_el[4];
242         TCR vtcr_el2; /* Virtualization Translation Control.  */
243         uint32_t c2_data; /* MPU data cacheable bits.  */
244         uint32_t c2_insn; /* MPU instruction cacheable bits.  */
245         union { /* MMU domain access control register
246                  * MPU write buffer control.
247                  */
248             struct {
249                 uint64_t dacr_ns;
250                 uint64_t dacr_s;
251             };
252             struct {
253                 uint64_t dacr32_el2;
254             };
255         };
256         uint32_t pmsav5_data_ap; /* PMSAv5 MPU data access permissions */
257         uint32_t pmsav5_insn_ap; /* PMSAv5 MPU insn access permissions */
258         uint64_t hcr_el2; /* Hypervisor configuration register */
259         uint64_t scr_el3; /* Secure configuration register.  */
260         union { /* Fault status registers.  */
261             struct {
262                 uint64_t ifsr_ns;
263                 uint64_t ifsr_s;
264             };
265             struct {
266                 uint64_t ifsr32_el2;
267             };
268         };
269         union {
270             struct {
271                 uint64_t _unused_dfsr;
272                 uint64_t dfsr_ns;
273                 uint64_t hsr;
274                 uint64_t dfsr_s;
275             };
276             uint64_t esr_el[4];
277         };
278         uint32_t c6_region[8]; /* MPU base/size registers.  */
279         union { /* Fault address registers. */
280             struct {
281                 uint64_t _unused_far0;
282 #ifdef HOST_WORDS_BIGENDIAN
283                 uint32_t ifar_ns;
284                 uint32_t dfar_ns;
285                 uint32_t ifar_s;
286                 uint32_t dfar_s;
287 #else
288                 uint32_t dfar_ns;
289                 uint32_t ifar_ns;
290                 uint32_t dfar_s;
291                 uint32_t ifar_s;
292 #endif
293                 uint64_t _unused_far3;
294             };
295             uint64_t far_el[4];
296         };
297         uint64_t hpfar_el2;
298         uint64_t hstr_el2;
299         union { /* Translation result. */
300             struct {
301                 uint64_t _unused_par_0;
302                 uint64_t par_ns;
303                 uint64_t _unused_par_1;
304                 uint64_t par_s;
305             };
306             uint64_t par_el[4];
307         };
308
309         uint32_t c9_insn; /* Cache lockdown registers.  */
310         uint32_t c9_data;
311         uint64_t c9_pmcr; /* performance monitor control register */
312         uint64_t c9_pmcnten; /* perf monitor counter enables */
313         uint32_t c9_pmovsr; /* perf monitor overflow status */
314         uint32_t c9_pmuserenr; /* perf monitor user enable */
315         uint64_t c9_pmselr; /* perf monitor counter selection register */
316         uint64_t c9_pminten; /* perf monitor interrupt enables */
317         union { /* Memory attribute redirection */
318             struct {
319 #ifdef HOST_WORDS_BIGENDIAN
320                 uint64_t _unused_mair_0;
321                 uint32_t mair1_ns;
322                 uint32_t mair0_ns;
323                 uint64_t _unused_mair_1;
324                 uint32_t mair1_s;
325                 uint32_t mair0_s;
326 #else
327                 uint64_t _unused_mair_0;
328                 uint32_t mair0_ns;
329                 uint32_t mair1_ns;
330                 uint64_t _unused_mair_1;
331                 uint32_t mair0_s;
332                 uint32_t mair1_s;
333 #endif
334             };
335             uint64_t mair_el[4];
336         };
337         union { /* vector base address register */
338             struct {
339                 uint64_t _unused_vbar;
340                 uint64_t vbar_ns;
341                 uint64_t hvbar;
342                 uint64_t vbar_s;
343             };
344             uint64_t vbar_el[4];
345         };
346         uint32_t mvbar; /* (monitor) vector base address register */
347         struct { /* FCSE PID. */
348             uint32_t fcseidr_ns;
349             uint32_t fcseidr_s;
350         };
351         union { /* Context ID. */
352             struct {
353                 uint64_t _unused_contextidr_0;
354                 uint64_t contextidr_ns;
355                 uint64_t _unused_contextidr_1;
356                 uint64_t contextidr_s;
357             };
358             uint64_t contextidr_el[4];
359         };
360         union { /* User RW Thread register. */
361             struct {
362                 uint64_t tpidrurw_ns;
363                 uint64_t tpidrprw_ns;
364                 uint64_t htpidr;
365                 uint64_t _tpidr_el3;
366             };
367             uint64_t tpidr_el[4];
368         };
369         /* The secure banks of these registers don't map anywhere */
370         uint64_t tpidrurw_s;
371         uint64_t tpidrprw_s;
372         uint64_t tpidruro_s;
373
374         union { /* User RO Thread register. */
375             uint64_t tpidruro_ns;
376             uint64_t tpidrro_el[1];
377         };
378         uint64_t c14_cntfrq; /* Counter Frequency register */
379         uint64_t c14_cntkctl; /* Timer Control register */
380         uint32_t cnthctl_el2; /* Counter/Timer Hyp Control register */
381         uint64_t cntvoff_el2; /* Counter Virtual Offset register */
382         ARMGenericTimer c14_timer[NUM_GTIMERS];
383         uint32_t c15_cpar; /* XScale Coprocessor Access Register */
384         uint32_t c15_ticonfig; /* TI925T configuration byte.  */
385         uint32_t c15_i_max; /* Maximum D-cache dirty line index.  */
386         uint32_t c15_i_min; /* Minimum D-cache dirty line index.  */
387         uint32_t c15_threadid; /* TI debugger thread-ID.  */
388         uint32_t c15_config_base_address; /* SCU base address.  */
389         uint32_t c15_diagnostic; /* diagnostic register */
390         uint32_t c15_power_diagnostic;
391         uint32_t c15_power_control; /* power control */
392         uint64_t dbgbvr[16]; /* breakpoint value registers */
393         uint64_t dbgbcr[16]; /* breakpoint control registers */
394         uint64_t dbgwvr[16]; /* watchpoint value registers */
395         uint64_t dbgwcr[16]; /* watchpoint control registers */
396         uint64_t mdscr_el1;
397         uint64_t oslsr_el1; /* OS Lock Status */
398         uint64_t mdcr_el2;
399         uint64_t mdcr_el3;
400         /* If the counter is enabled, this stores the last time the counter
401          * was reset. Otherwise it stores the counter value
402          */
403         uint64_t c15_ccnt;
404         uint64_t pmccfiltr_el0; /* Performance Monitor Filter Register */
405         uint64_t vpidr_el2; /* Virtualization Processor ID Register */
406         uint64_t vmpidr_el2; /* Virtualization Multiprocessor ID Register */
407     } cp15;
408
409     struct {
410         uint32_t other_sp;
411         uint32_t vecbase;
412         uint32_t basepri;
413         uint32_t control;
414         uint32_t ccr; /* Configuration and Control */
415         uint32_t cfsr; /* Configurable Fault Status */
416         uint32_t hfsr; /* HardFault Status */
417         uint32_t dfsr; /* Debug Fault Status Register */
418         uint32_t mmfar; /* MemManage Fault Address */
419         uint32_t bfar; /* BusFault Address */
420         unsigned mpu_ctrl; /* MPU_CTRL */
421         int exception;
422         uint32_t primask;
423         uint32_t faultmask;
424         uint32_t secure; /* Is CPU in Secure state? (not guest visible) */
425     } v7m;
426
427     /* Information associated with an exception about to be taken:
428      * code which raises an exception must set cs->exception_index and
429      * the relevant parts of this structure; the cpu_do_interrupt function
430      * will then set the guest-visible registers as part of the exception
431      * entry process.
432      */
433     struct {
434         uint32_t syndrome; /* AArch64 format syndrome register */
435         uint32_t fsr; /* AArch32 format fault status register info */
436         uint64_t vaddress; /* virtual addr associated with exception, if any */
437         uint32_t target_el; /* EL the exception should be targeted for */
438         /* If we implement EL2 we will also need to store information
439          * about the intermediate physical address for stage 2 faults.
440          */
441     } exception;
442
443     /* Thumb-2 EE state.  */
444     uint32_t teecr;
445     uint32_t teehbr;
446
447     /* VFP coprocessor state.  */
448     struct {
449         /* VFP/Neon register state. Note that the mapping between S, D and Q
450          * views of the register bank differs between AArch64 and AArch32:
451          * In AArch32:
452          *  Qn = regs[2n+1]:regs[2n]
453          *  Dn = regs[n]
454          *  Sn = regs[n/2] bits 31..0 for even n, and bits 63..32 for odd n
455          * (and regs[32] to regs[63] are inaccessible)
456          * In AArch64:
457          *  Qn = regs[2n+1]:regs[2n]
458          *  Dn = regs[2n]
459          *  Sn = regs[2n] bits 31..0
460          * This corresponds to the architecturally defined mapping between
461          * the two execution states, and means we do not need to explicitly
462          * map these registers when changing states.
463          */
464         float64 regs[64];
465
466         uint32_t xregs[16];
467         /* We store these fpcsr fields separately for convenience.  */
468         int vec_len;
469         int vec_stride;
470
471         /* scratch space when Tn are not sufficient.  */
472         uint32_t scratch[8];
473
474         /* fp_status is the "normal" fp status. standard_fp_status retains
475          * values corresponding to the ARM "Standard FPSCR Value", ie
476          * default-NaN, flush-to-zero, round-to-nearest and is used by
477          * any operations (generally Neon) which the architecture defines
478          * as controlled by the standard FPSCR value rather than the FPSCR.
479          *
480          * To avoid having to transfer exception bits around, we simply
481          * say that the FPSCR cumulative exception flags are the logical
482          * OR of the flags in the two fp statuses. This relies on the
483          * only thing which needs to read the exception flags being
484          * an explicit FPSCR read.
485          */
486         float_status fp_status;
487         float_status standard_fp_status;
488     } vfp;
489     uint64_t exclusive_addr;
490     uint64_t exclusive_val;
491     uint64_t exclusive_high;
492
493     /* iwMMXt coprocessor state.  */
494     struct {
495         uint64_t regs[16];
496         uint64_t val;
497
498         uint32_t cregs[16];
499     } iwmmxt;
500
501 #if defined(CONFIG_USER_ONLY)
502     /* For usermode syscall translation.  */
503     int eabi;
504 #endif
505
506     struct CPUBreakpoint *cpu_breakpoint[16];
507     struct CPUWatchpoint *cpu_watchpoint[16];
508
509     /* Fields up to this point are cleared by a CPU reset */
510     struct {} end_reset_fields;
511
512     CPU_COMMON
513
514     /* Fields after CPU_COMMON are preserved across CPU reset. */
515
516     /* Internal CPU feature flags.  */
517     uint64_t features;
518
519     /* PMSAv7 MPU */
520     struct {
521         uint32_t *drbar;
522         uint32_t *drsr;
523         uint32_t *dracr;
524         uint32_t rnr;
525     } pmsav7;
526
527     /* PMSAv8 MPU */
528     struct {
529         /* The PMSAv8 implementation also shares some PMSAv7 config
530          * and state:
531          *  pmsav7.rnr (region number register)
532          *  pmsav7_dregion (number of configured regions)
533          */
534         uint32_t *rbar;
535         uint32_t *rlar;
536         uint32_t mair0;
537         uint32_t mair1;
538     } pmsav8;
539
540     void *nvic;
541     const struct arm_boot_info *boot_info;
542     /* Store GICv3CPUState to access from this struct */
543     void *gicv3state;
544 } CPUARMState;
545
546 /**
547  * ARMELChangeHook:
548  * type of a function which can be registered via arm_register_el_change_hook()
549  * to get callbacks when the CPU changes its exception level or mode.
550  */
551 typedef void ARMELChangeHook(ARMCPU *cpu, void *opaque);
552
553
554 /* These values map onto the return values for
555  * QEMU_PSCI_0_2_FN_AFFINITY_INFO */
556 typedef enum ARMPSCIState {
557     PSCI_ON = 0,
558     PSCI_OFF = 1,
559     PSCI_ON_PENDING = 2
560 } ARMPSCIState;
561
562 /**
563  * ARMCPU:
564  * @env: #CPUARMState
565  *
566  * An ARM CPU core.
567  */
568 struct ARMCPU {
569     /*< private >*/
570     CPUState parent_obj;
571     /*< public >*/
572
573     CPUARMState env;
574
575     /* Coprocessor information */
576     GHashTable *cp_regs;
577     /* For marshalling (mostly coprocessor) register state between the
578      * kernel and QEMU (for KVM) and between two QEMUs (for migration),
579      * we use these arrays.
580      */
581     /* List of register indexes managed via these arrays; (full KVM style
582      * 64 bit indexes, not CPRegInfo 32 bit indexes)
583      */
584     uint64_t *cpreg_indexes;
585     /* Values of the registers (cpreg_indexes[i]'s value is cpreg_values[i]) */
586     uint64_t *cpreg_values;
587     /* Length of the indexes, values, reset_values arrays */
588     int32_t cpreg_array_len;
589     /* These are used only for migration: incoming data arrives in
590      * these fields and is sanity checked in post_load before copying
591      * to the working data structures above.
592      */
593     uint64_t *cpreg_vmstate_indexes;
594     uint64_t *cpreg_vmstate_values;
595     int32_t cpreg_vmstate_array_len;
596
597     /* Timers used by the generic (architected) timer */
598     QEMUTimer *gt_timer[NUM_GTIMERS];
599     /* GPIO outputs for generic timer */
600     qemu_irq gt_timer_outputs[NUM_GTIMERS];
601     /* GPIO output for GICv3 maintenance interrupt signal */
602     qemu_irq gicv3_maintenance_interrupt;
603     /* GPIO output for the PMU interrupt */
604     qemu_irq pmu_interrupt;
605
606     /* MemoryRegion to use for secure physical accesses */
607     MemoryRegion *secure_memory;
608
609     /* 'compatible' string for this CPU for Linux device trees */
610     const char *dtb_compatible;
611
612     /* PSCI version for this CPU
613      * Bits[31:16] = Major Version
614      * Bits[15:0] = Minor Version
615      */
616     uint32_t psci_version;
617
618     /* Should CPU start in PSCI powered-off state? */
619     bool start_powered_off;
620
621     /* Current power state, access guarded by BQL */
622     ARMPSCIState power_state;
623
624     /* CPU has virtualization extension */
625     bool has_el2;
626     /* CPU has security extension */
627     bool has_el3;
628     /* CPU has PMU (Performance Monitor Unit) */
629     bool has_pmu;
630
631     /* CPU has memory protection unit */
632     bool has_mpu;
633     /* PMSAv7 MPU number of supported regions */
634     uint32_t pmsav7_dregion;
635
636     /* PSCI conduit used to invoke PSCI methods
637      * 0 - disabled, 1 - smc, 2 - hvc
638      */
639     uint32_t psci_conduit;
640
641     /* [QEMU_]KVM_ARM_TARGET_* constant for this CPU, or
642      * QEMU_KVM_ARM_TARGET_NONE if the kernel doesn't support this CPU type.
643      */
644     uint32_t kvm_target;
645
646     /* KVM init features for this CPU */
647     uint32_t kvm_init_features[7];
648
649     /* Uniprocessor system with MP extensions */
650     bool mp_is_up;
651
652     /* The instance init functions for implementation-specific subclasses
653      * set these fields to specify the implementation-dependent values of
654      * various constant registers and reset values of non-constant
655      * registers.
656      * Some of these might become QOM properties eventually.
657      * Field names match the official register names as defined in the
658      * ARMv7AR ARM Architecture Reference Manual. A reset_ prefix
659      * is used for reset values of non-constant registers; no reset_
660      * prefix means a constant register.
661      */
662     uint32_t midr;
663     uint32_t revidr;
664     uint32_t reset_fpsid;
665     uint32_t mvfr0;
666     uint32_t mvfr1;
667     uint32_t mvfr2;
668     uint32_t ctr;
669     uint32_t reset_sctlr;
670     uint32_t id_pfr0;
671     uint32_t id_pfr1;
672     uint32_t id_dfr0;
673     uint32_t pmceid0;
674     uint32_t pmceid1;
675     uint32_t id_afr0;
676     uint32_t id_mmfr0;
677     uint32_t id_mmfr1;
678     uint32_t id_mmfr2;
679     uint32_t id_mmfr3;
680     uint32_t id_mmfr4;
681     uint32_t id_isar0;
682     uint32_t id_isar1;
683     uint32_t id_isar2;
684     uint32_t id_isar3;
685     uint32_t id_isar4;
686     uint32_t id_isar5;
687     uint64_t id_aa64pfr0;
688     uint64_t id_aa64pfr1;
689     uint64_t id_aa64dfr0;
690     uint64_t id_aa64dfr1;
691     uint64_t id_aa64afr0;
692     uint64_t id_aa64afr1;
693     uint64_t id_aa64isar0;
694     uint64_t id_aa64isar1;
695     uint64_t id_aa64mmfr0;
696     uint64_t id_aa64mmfr1;
697     uint32_t dbgdidr;
698     uint32_t clidr;
699     uint64_t mp_affinity; /* MP ID without feature bits */
700     /* The elements of this array are the CCSIDR values for each cache,
701      * in the order L1DCache, L1ICache, L2DCache, L2ICache, etc.
702      */
703     uint32_t ccsidr[16];
704     uint64_t reset_cbar;
705     uint32_t reset_auxcr;
706     bool reset_hivecs;
707     /* DCZ blocksize, in log_2(words), ie low 4 bits of DCZID_EL0 */
708     uint32_t dcz_blocksize;
709     uint64_t rvbar;
710
711     /* Configurable aspects of GIC cpu interface (which is part of the CPU) */
712     int gic_num_lrs; /* number of list registers */
713     int gic_vpribits; /* number of virtual priority bits */
714     int gic_vprebits; /* number of virtual preemption bits */
715
716     /* Whether the cfgend input is high (i.e. this CPU should reset into
717      * big-endian mode).  This setting isn't used directly: instead it modifies
718      * the reset_sctlr value to have SCTLR_B or SCTLR_EE set, depending on the
719      * architecture version.
720      */
721     bool cfgend;
722
723     ARMELChangeHook *el_change_hook;
724     void *el_change_hook_opaque;
725
726     int32_t node_id; /* NUMA node this CPU belongs to */
727
728     /* Used to synchronize KVM and QEMU in-kernel device levels */
729     uint8_t device_irq_level;
730 };
731
732 static inline ARMCPU *arm_env_get_cpu(CPUARMState *env)
733 {
734     return container_of(env, ARMCPU, env);
735 }
736
737 uint64_t arm_cpu_mp_affinity(int idx, uint8_t clustersz);
738
739 #define ENV_GET_CPU(e) CPU(arm_env_get_cpu(e))
740
741 #define ENV_OFFSET offsetof(ARMCPU, env)
742
743 #ifndef CONFIG_USER_ONLY
744 extern const struct VMStateDescription vmstate_arm_cpu;
745 #endif
746
747 void arm_cpu_do_interrupt(CPUState *cpu);
748 void arm_v7m_cpu_do_interrupt(CPUState *cpu);
749 bool arm_cpu_exec_interrupt(CPUState *cpu, int int_req);
750
751 void arm_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
752                         int flags);
753
754 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
755                                          MemTxAttrs *attrs);
756
757 int arm_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
758 int arm_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
759
760 int arm_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
761                              int cpuid, void *opaque);
762 int arm_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
763                              int cpuid, void *opaque);
764
765 #ifdef TARGET_AARCH64
766 int aarch64_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
767 int aarch64_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
768 #endif
769
770 target_ulong do_arm_semihosting(CPUARMState *env);
771 void aarch64_sync_32_to_64(CPUARMState *env);
772 void aarch64_sync_64_to_32(CPUARMState *env);
773
774 static inline bool is_a64(CPUARMState *env)
775 {
776     return env->aarch64;
777 }
778
779 /* you can call this signal handler from your SIGBUS and SIGSEGV
780    signal handlers to inform the virtual CPU of exceptions. non zero
781    is returned if the signal was handled by the virtual CPU.  */
782 int cpu_arm_signal_handler(int host_signum, void *pinfo,
783                            void *puc);
784
785 /**
786  * pmccntr_sync
787  * @env: CPUARMState
788  *
789  * Synchronises the counter in the PMCCNTR. This must always be called twice,
790  * once before any action that might affect the timer and again afterwards.
791  * The function is used to swap the state of the register if required.
792  * This only happens when not in user mode (!CONFIG_USER_ONLY)
793  */
794 void pmccntr_sync(CPUARMState *env);
795
796 /* SCTLR bit meanings. Several bits have been reused in newer
797  * versions of the architecture; in that case we define constants
798  * for both old and new bit meanings. Code which tests against those
799  * bits should probably check or otherwise arrange that the CPU
800  * is the architectural version it expects.
801  */
802 #define SCTLR_M       (1U << 0)
803 #define SCTLR_A       (1U << 1)
804 #define SCTLR_C       (1U << 2)
805 #define SCTLR_W       (1U << 3) /* up to v6; RAO in v7 */
806 #define SCTLR_SA      (1U << 3)
807 #define SCTLR_P       (1U << 4) /* up to v5; RAO in v6 and v7 */
808 #define SCTLR_SA0     (1U << 4) /* v8 onward, AArch64 only */
809 #define SCTLR_D       (1U << 5) /* up to v5; RAO in v6 */
810 #define SCTLR_CP15BEN (1U << 5) /* v7 onward */
811 #define SCTLR_L       (1U << 6) /* up to v5; RAO in v6 and v7; RAZ in v8 */
812 #define SCTLR_B       (1U << 7) /* up to v6; RAZ in v7 */
813 #define SCTLR_ITD     (1U << 7) /* v8 onward */
814 #define SCTLR_S       (1U << 8) /* up to v6; RAZ in v7 */
815 #define SCTLR_SED     (1U << 8) /* v8 onward */
816 #define SCTLR_R       (1U << 9) /* up to v6; RAZ in v7 */
817 #define SCTLR_UMA     (1U << 9) /* v8 onward, AArch64 only */
818 #define SCTLR_F       (1U << 10) /* up to v6 */
819 #define SCTLR_SW      (1U << 10) /* v7 onward */
820 #define SCTLR_Z       (1U << 11)
821 #define SCTLR_I       (1U << 12)
822 #define SCTLR_V       (1U << 13)
823 #define SCTLR_RR      (1U << 14) /* up to v7 */
824 #define SCTLR_DZE     (1U << 14) /* v8 onward, AArch64 only */
825 #define SCTLR_L4      (1U << 15) /* up to v6; RAZ in v7 */
826 #define SCTLR_UCT     (1U << 15) /* v8 onward, AArch64 only */
827 #define SCTLR_DT      (1U << 16) /* up to ??, RAO in v6 and v7 */
828 #define SCTLR_nTWI    (1U << 16) /* v8 onward */
829 #define SCTLR_HA      (1U << 17)
830 #define SCTLR_BR      (1U << 17) /* PMSA only */
831 #define SCTLR_IT      (1U << 18) /* up to ??, RAO in v6 and v7 */
832 #define SCTLR_nTWE    (1U << 18) /* v8 onward */
833 #define SCTLR_WXN     (1U << 19)
834 #define SCTLR_ST      (1U << 20) /* up to ??, RAZ in v6 */
835 #define SCTLR_UWXN    (1U << 20) /* v7 onward */
836 #define SCTLR_FI      (1U << 21)
837 #define SCTLR_U       (1U << 22)
838 #define SCTLR_XP      (1U << 23) /* up to v6; v7 onward RAO */
839 #define SCTLR_VE      (1U << 24) /* up to v7 */
840 #define SCTLR_E0E     (1U << 24) /* v8 onward, AArch64 only */
841 #define SCTLR_EE      (1U << 25)
842 #define SCTLR_L2      (1U << 26) /* up to v6, RAZ in v7 */
843 #define SCTLR_UCI     (1U << 26) /* v8 onward, AArch64 only */
844 #define SCTLR_NMFI    (1U << 27)
845 #define SCTLR_TRE     (1U << 28)
846 #define SCTLR_AFE     (1U << 29)
847 #define SCTLR_TE      (1U << 30)
848
849 #define CPTR_TCPAC    (1U << 31)
850 #define CPTR_TTA      (1U << 20)
851 #define CPTR_TFP      (1U << 10)
852
853 #define MDCR_EPMAD    (1U << 21)
854 #define MDCR_EDAD     (1U << 20)
855 #define MDCR_SPME     (1U << 17)
856 #define MDCR_SDD      (1U << 16)
857 #define MDCR_SPD      (3U << 14)
858 #define MDCR_TDRA     (1U << 11)
859 #define MDCR_TDOSA    (1U << 10)
860 #define MDCR_TDA      (1U << 9)
861 #define MDCR_TDE      (1U << 8)
862 #define MDCR_HPME     (1U << 7)
863 #define MDCR_TPM      (1U << 6)
864 #define MDCR_TPMCR    (1U << 5)
865
866 /* Not all of the MDCR_EL3 bits are present in the 32-bit SDCR */
867 #define SDCR_VALID_MASK (MDCR_EPMAD | MDCR_EDAD | MDCR_SPME | MDCR_SPD)
868
869 #define CPSR_M (0x1fU)
870 #define CPSR_T (1U << 5)
871 #define CPSR_F (1U << 6)
872 #define CPSR_I (1U << 7)
873 #define CPSR_A (1U << 8)
874 #define CPSR_E (1U << 9)
875 #define CPSR_IT_2_7 (0xfc00U)
876 #define CPSR_GE (0xfU << 16)
877 #define CPSR_IL (1U << 20)
878 /* Note that the RESERVED bits include bit 21, which is PSTATE_SS in
879  * an AArch64 SPSR but RES0 in AArch32 SPSR and CPSR. In QEMU we use
880  * env->uncached_cpsr bit 21 to store PSTATE.SS when executing in AArch32,
881  * where it is live state but not accessible to the AArch32 code.
882  */
883 #define CPSR_RESERVED (0x7U << 21)
884 #define CPSR_J (1U << 24)
885 #define CPSR_IT_0_1 (3U << 25)
886 #define CPSR_Q (1U << 27)
887 #define CPSR_V (1U << 28)
888 #define CPSR_C (1U << 29)
889 #define CPSR_Z (1U << 30)
890 #define CPSR_N (1U << 31)
891 #define CPSR_NZCV (CPSR_N | CPSR_Z | CPSR_C | CPSR_V)
892 #define CPSR_AIF (CPSR_A | CPSR_I | CPSR_F)
893
894 #define CPSR_IT (CPSR_IT_0_1 | CPSR_IT_2_7)
895 #define CACHED_CPSR_BITS (CPSR_T | CPSR_AIF | CPSR_GE | CPSR_IT | CPSR_Q \
896     | CPSR_NZCV)
897 /* Bits writable in user mode.  */
898 #define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
899 /* Execution state bits.  MRS read as zero, MSR writes ignored.  */
900 #define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J | CPSR_IL)
901 /* Mask of bits which may be set by exception return copying them from SPSR */
902 #define CPSR_ERET_MASK (~CPSR_RESERVED)
903
904 /* Bit definitions for M profile XPSR. Most are the same as CPSR. */
905 #define XPSR_EXCP 0x1ffU
906 #define XPSR_SPREALIGN (1U << 9) /* Only set in exception stack frames */
907 #define XPSR_IT_2_7 CPSR_IT_2_7
908 #define XPSR_GE CPSR_GE
909 #define XPSR_SFPA (1U << 20) /* Only set in exception stack frames */
910 #define XPSR_T (1U << 24) /* Not the same as CPSR_T ! */
911 #define XPSR_IT_0_1 CPSR_IT_0_1
912 #define XPSR_Q CPSR_Q
913 #define XPSR_V CPSR_V
914 #define XPSR_C CPSR_C
915 #define XPSR_Z CPSR_Z
916 #define XPSR_N CPSR_N
917 #define XPSR_NZCV CPSR_NZCV
918 #define XPSR_IT CPSR_IT
919
920 #define TTBCR_N      (7U << 0) /* TTBCR.EAE==0 */
921 #define TTBCR_T0SZ   (7U << 0) /* TTBCR.EAE==1 */
922 #define TTBCR_PD0    (1U << 4)
923 #define TTBCR_PD1    (1U << 5)
924 #define TTBCR_EPD0   (1U << 7)
925 #define TTBCR_IRGN0  (3U << 8)
926 #define TTBCR_ORGN0  (3U << 10)
927 #define TTBCR_SH0    (3U << 12)
928 #define TTBCR_T1SZ   (3U << 16)
929 #define TTBCR_A1     (1U << 22)
930 #define TTBCR_EPD1   (1U << 23)
931 #define TTBCR_IRGN1  (3U << 24)
932 #define TTBCR_ORGN1  (3U << 26)
933 #define TTBCR_SH1    (1U << 28)
934 #define TTBCR_EAE    (1U << 31)
935
936 /* Bit definitions for ARMv8 SPSR (PSTATE) format.
937  * Only these are valid when in AArch64 mode; in
938  * AArch32 mode SPSRs are basically CPSR-format.
939  */
940 #define PSTATE_SP (1U)
941 #define PSTATE_M (0xFU)
942 #define PSTATE_nRW (1U << 4)
943 #define PSTATE_F (1U << 6)
944 #define PSTATE_I (1U << 7)
945 #define PSTATE_A (1U << 8)
946 #define PSTATE_D (1U << 9)
947 #define PSTATE_IL (1U << 20)
948 #define PSTATE_SS (1U << 21)
949 #define PSTATE_V (1U << 28)
950 #define PSTATE_C (1U << 29)
951 #define PSTATE_Z (1U << 30)
952 #define PSTATE_N (1U << 31)
953 #define PSTATE_NZCV (PSTATE_N | PSTATE_Z | PSTATE_C | PSTATE_V)
954 #define PSTATE_DAIF (PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F)
955 #define CACHED_PSTATE_BITS (PSTATE_NZCV | PSTATE_DAIF)
956 /* Mode values for AArch64 */
957 #define PSTATE_MODE_EL3h 13
958 #define PSTATE_MODE_EL3t 12
959 #define PSTATE_MODE_EL2h 9
960 #define PSTATE_MODE_EL2t 8
961 #define PSTATE_MODE_EL1h 5
962 #define PSTATE_MODE_EL1t 4
963 #define PSTATE_MODE_EL0t 0
964
965 /* Map EL and handler into a PSTATE_MODE.  */
966 static inline unsigned int aarch64_pstate_mode(unsigned int el, bool handler)
967 {
968     return (el << 2) | handler;
969 }
970
971 /* Return the current PSTATE value. For the moment we don't support 32<->64 bit
972  * interprocessing, so we don't attempt to sync with the cpsr state used by
973  * the 32 bit decoder.
974  */
975 static inline uint32_t pstate_read(CPUARMState *env)
976 {
977     int ZF;
978
979     ZF = (env->ZF == 0);
980     return (env->NF & 0x80000000) | (ZF << 30)
981         | (env->CF << 29) | ((env->VF & 0x80000000) >> 3)
982         | env->pstate | env->daif;
983 }
984
985 static inline void pstate_write(CPUARMState *env, uint32_t val)
986 {
987     env->ZF = (~val) & PSTATE_Z;
988     env->NF = val;
989     env->CF = (val >> 29) & 1;
990     env->VF = (val << 3) & 0x80000000;
991     env->daif = val & PSTATE_DAIF;
992     env->pstate = val & ~CACHED_PSTATE_BITS;
993 }
994
995 /* Return the current CPSR value.  */
996 uint32_t cpsr_read(CPUARMState *env);
997
998 typedef enum CPSRWriteType {
999     CPSRWriteByInstr = 0,         /* from guest MSR or CPS */
1000     CPSRWriteExceptionReturn = 1, /* from guest exception return insn */
1001     CPSRWriteRaw = 2,             /* trust values, do not switch reg banks */
1002     CPSRWriteByGDBStub = 3,       /* from the GDB stub */
1003 } CPSRWriteType;
1004
1005 /* Set the CPSR.  Note that some bits of mask must be all-set or all-clear.*/
1006 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
1007                 CPSRWriteType write_type);
1008
1009 /* Return the current xPSR value.  */
1010 static inline uint32_t xpsr_read(CPUARMState *env)
1011 {
1012     int ZF;
1013     ZF = (env->ZF == 0);
1014     return (env->NF & 0x80000000) | (ZF << 30)
1015         | (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
1016         | (env->thumb << 24) | ((env->condexec_bits & 3) << 25)
1017         | ((env->condexec_bits & 0xfc) << 8)
1018         | env->v7m.exception;
1019 }
1020
1021 /* Set the xPSR.  Note that some bits of mask must be all-set or all-clear.  */
1022 static inline void xpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
1023 {
1024     if (mask & XPSR_NZCV) {
1025         env->ZF = (~val) & XPSR_Z;
1026         env->NF = val;
1027         env->CF = (val >> 29) & 1;
1028         env->VF = (val << 3) & 0x80000000;
1029     }
1030     if (mask & XPSR_Q) {
1031         env->QF = ((val & XPSR_Q) != 0);
1032     }
1033     if (mask & XPSR_T) {
1034         env->thumb = ((val & XPSR_T) != 0);
1035     }
1036     if (mask & XPSR_IT_0_1) {
1037         env->condexec_bits &= ~3;
1038         env->condexec_bits |= (val >> 25) & 3;
1039     }
1040     if (mask & XPSR_IT_2_7) {
1041         env->condexec_bits &= 3;
1042         env->condexec_bits |= (val >> 8) & 0xfc;
1043     }
1044     if (mask & XPSR_EXCP) {
1045         env->v7m.exception = val & XPSR_EXCP;
1046     }
1047 }
1048
1049 #define HCR_VM        (1ULL << 0)
1050 #define HCR_SWIO      (1ULL << 1)
1051 #define HCR_PTW       (1ULL << 2)
1052 #define HCR_FMO       (1ULL << 3)
1053 #define HCR_IMO       (1ULL << 4)
1054 #define HCR_AMO       (1ULL << 5)
1055 #define HCR_VF        (1ULL << 6)
1056 #define HCR_VI        (1ULL << 7)
1057 #define HCR_VSE       (1ULL << 8)
1058 #define HCR_FB        (1ULL << 9)
1059 #define HCR_BSU_MASK  (3ULL << 10)
1060 #define HCR_DC        (1ULL << 12)
1061 #define HCR_TWI       (1ULL << 13)
1062 #define HCR_TWE       (1ULL << 14)
1063 #define HCR_TID0      (1ULL << 15)
1064 #define HCR_TID1      (1ULL << 16)
1065 #define HCR_TID2      (1ULL << 17)
1066 #define HCR_TID3      (1ULL << 18)
1067 #define HCR_TSC       (1ULL << 19)
1068 #define HCR_TIDCP     (1ULL << 20)
1069 #define HCR_TACR      (1ULL << 21)
1070 #define HCR_TSW       (1ULL << 22)
1071 #define HCR_TPC       (1ULL << 23)
1072 #define HCR_TPU       (1ULL << 24)
1073 #define HCR_TTLB      (1ULL << 25)
1074 #define HCR_TVM       (1ULL << 26)
1075 #define HCR_TGE       (1ULL << 27)
1076 #define HCR_TDZ       (1ULL << 28)
1077 #define HCR_HCD       (1ULL << 29)
1078 #define HCR_TRVM      (1ULL << 30)
1079 #define HCR_RW        (1ULL << 31)
1080 #define HCR_CD        (1ULL << 32)
1081 #define HCR_ID        (1ULL << 33)
1082 #define HCR_MASK      ((1ULL << 34) - 1)
1083
1084 #define SCR_NS                (1U << 0)
1085 #define SCR_IRQ               (1U << 1)
1086 #define SCR_FIQ               (1U << 2)
1087 #define SCR_EA                (1U << 3)
1088 #define SCR_FW                (1U << 4)
1089 #define SCR_AW                (1U << 5)
1090 #define SCR_NET               (1U << 6)
1091 #define SCR_SMD               (1U << 7)
1092 #define SCR_HCE               (1U << 8)
1093 #define SCR_SIF               (1U << 9)
1094 #define SCR_RW                (1U << 10)
1095 #define SCR_ST                (1U << 11)
1096 #define SCR_TWI               (1U << 12)
1097 #define SCR_TWE               (1U << 13)
1098 #define SCR_AARCH32_MASK      (0x3fff & ~(SCR_RW | SCR_ST))
1099 #define SCR_AARCH64_MASK      (0x3fff & ~SCR_NET)
1100
1101 /* Return the current FPSCR value.  */
1102 uint32_t vfp_get_fpscr(CPUARMState *env);
1103 void vfp_set_fpscr(CPUARMState *env, uint32_t val);
1104
1105 /* For A64 the FPSCR is split into two logically distinct registers,
1106  * FPCR and FPSR. However since they still use non-overlapping bits
1107  * we store the underlying state in fpscr and just mask on read/write.
1108  */
1109 #define FPSR_MASK 0xf800009f
1110 #define FPCR_MASK 0x07f79f00
1111 static inline uint32_t vfp_get_fpsr(CPUARMState *env)
1112 {
1113     return vfp_get_fpscr(env) & FPSR_MASK;
1114 }
1115
1116 static inline void vfp_set_fpsr(CPUARMState *env, uint32_t val)
1117 {
1118     uint32_t new_fpscr = (vfp_get_fpscr(env) & ~FPSR_MASK) | (val & FPSR_MASK);
1119     vfp_set_fpscr(env, new_fpscr);
1120 }
1121
1122 static inline uint32_t vfp_get_fpcr(CPUARMState *env)
1123 {
1124     return vfp_get_fpscr(env) & FPCR_MASK;
1125 }
1126
1127 static inline void vfp_set_fpcr(CPUARMState *env, uint32_t val)
1128 {
1129     uint32_t new_fpscr = (vfp_get_fpscr(env) & ~FPCR_MASK) | (val & FPCR_MASK);
1130     vfp_set_fpscr(env, new_fpscr);
1131 }
1132
1133 enum arm_cpu_mode {
1134   ARM_CPU_MODE_USR = 0x10,
1135   ARM_CPU_MODE_FIQ = 0x11,
1136   ARM_CPU_MODE_IRQ = 0x12,
1137   ARM_CPU_MODE_SVC = 0x13,
1138   ARM_CPU_MODE_MON = 0x16,
1139   ARM_CPU_MODE_ABT = 0x17,
1140   ARM_CPU_MODE_HYP = 0x1a,
1141   ARM_CPU_MODE_UND = 0x1b,
1142   ARM_CPU_MODE_SYS = 0x1f
1143 };
1144
1145 /* VFP system registers.  */
1146 #define ARM_VFP_FPSID   0
1147 #define ARM_VFP_FPSCR   1
1148 #define ARM_VFP_MVFR2   5
1149 #define ARM_VFP_MVFR1   6
1150 #define ARM_VFP_MVFR0   7
1151 #define ARM_VFP_FPEXC   8
1152 #define ARM_VFP_FPINST  9
1153 #define ARM_VFP_FPINST2 10
1154
1155 /* iwMMXt coprocessor control registers.  */
1156 #define ARM_IWMMXT_wCID         0
1157 #define ARM_IWMMXT_wCon         1
1158 #define ARM_IWMMXT_wCSSF        2
1159 #define ARM_IWMMXT_wCASF        3
1160 #define ARM_IWMMXT_wCGR0        8
1161 #define ARM_IWMMXT_wCGR1        9
1162 #define ARM_IWMMXT_wCGR2        10
1163 #define ARM_IWMMXT_wCGR3        11
1164
1165 /* V7M CCR bits */
1166 FIELD(V7M_CCR, NONBASETHRDENA, 0, 1)
1167 FIELD(V7M_CCR, USERSETMPEND, 1, 1)
1168 FIELD(V7M_CCR, UNALIGN_TRP, 3, 1)
1169 FIELD(V7M_CCR, DIV_0_TRP, 4, 1)
1170 FIELD(V7M_CCR, BFHFNMIGN, 8, 1)
1171 FIELD(V7M_CCR, STKALIGN, 9, 1)
1172 FIELD(V7M_CCR, DC, 16, 1)
1173 FIELD(V7M_CCR, IC, 17, 1)
1174
1175 /* V7M CFSR bits for MMFSR */
1176 FIELD(V7M_CFSR, IACCVIOL, 0, 1)
1177 FIELD(V7M_CFSR, DACCVIOL, 1, 1)
1178 FIELD(V7M_CFSR, MUNSTKERR, 3, 1)
1179 FIELD(V7M_CFSR, MSTKERR, 4, 1)
1180 FIELD(V7M_CFSR, MLSPERR, 5, 1)
1181 FIELD(V7M_CFSR, MMARVALID, 7, 1)
1182
1183 /* V7M CFSR bits for BFSR */
1184 FIELD(V7M_CFSR, IBUSERR, 8 + 0, 1)
1185 FIELD(V7M_CFSR, PRECISERR, 8 + 1, 1)
1186 FIELD(V7M_CFSR, IMPRECISERR, 8 + 2, 1)
1187 FIELD(V7M_CFSR, UNSTKERR, 8 + 3, 1)
1188 FIELD(V7M_CFSR, STKERR, 8 + 4, 1)
1189 FIELD(V7M_CFSR, LSPERR, 8 + 5, 1)
1190 FIELD(V7M_CFSR, BFARVALID, 8 + 7, 1)
1191
1192 /* V7M CFSR bits for UFSR */
1193 FIELD(V7M_CFSR, UNDEFINSTR, 16 + 0, 1)
1194 FIELD(V7M_CFSR, INVSTATE, 16 + 1, 1)
1195 FIELD(V7M_CFSR, INVPC, 16 + 2, 1)
1196 FIELD(V7M_CFSR, NOCP, 16 + 3, 1)
1197 FIELD(V7M_CFSR, UNALIGNED, 16 + 8, 1)
1198 FIELD(V7M_CFSR, DIVBYZERO, 16 + 9, 1)
1199
1200 /* V7M HFSR bits */
1201 FIELD(V7M_HFSR, VECTTBL, 1, 1)
1202 FIELD(V7M_HFSR, FORCED, 30, 1)
1203 FIELD(V7M_HFSR, DEBUGEVT, 31, 1)
1204
1205 /* V7M DFSR bits */
1206 FIELD(V7M_DFSR, HALTED, 0, 1)
1207 FIELD(V7M_DFSR, BKPT, 1, 1)
1208 FIELD(V7M_DFSR, DWTTRAP, 2, 1)
1209 FIELD(V7M_DFSR, VCATCH, 3, 1)
1210 FIELD(V7M_DFSR, EXTERNAL, 4, 1)
1211
1212 /* v7M MPU_CTRL bits */
1213 FIELD(V7M_MPU_CTRL, ENABLE, 0, 1)
1214 FIELD(V7M_MPU_CTRL, HFNMIENA, 1, 1)
1215 FIELD(V7M_MPU_CTRL, PRIVDEFENA, 2, 1)
1216
1217 /* If adding a feature bit which corresponds to a Linux ELF
1218  * HWCAP bit, remember to update the feature-bit-to-hwcap
1219  * mapping in linux-user/elfload.c:get_elf_hwcap().
1220  */
1221 enum arm_features {
1222     ARM_FEATURE_VFP,
1223     ARM_FEATURE_AUXCR,  /* ARM1026 Auxiliary control register.  */
1224     ARM_FEATURE_XSCALE, /* Intel XScale extensions.  */
1225     ARM_FEATURE_IWMMXT, /* Intel iwMMXt extension.  */
1226     ARM_FEATURE_V6,
1227     ARM_FEATURE_V6K,
1228     ARM_FEATURE_V7,
1229     ARM_FEATURE_THUMB2,
1230     ARM_FEATURE_PMSA,   /* no MMU; may have Memory Protection Unit */
1231     ARM_FEATURE_VFP3,
1232     ARM_FEATURE_VFP_FP16,
1233     ARM_FEATURE_NEON,
1234     ARM_FEATURE_THUMB_DIV, /* divide supported in Thumb encoding */
1235     ARM_FEATURE_M, /* Microcontroller profile.  */
1236     ARM_FEATURE_OMAPCP, /* OMAP specific CP15 ops handling.  */
1237     ARM_FEATURE_THUMB2EE,
1238     ARM_FEATURE_V7MP,    /* v7 Multiprocessing Extensions */
1239     ARM_FEATURE_V4T,
1240     ARM_FEATURE_V5,
1241     ARM_FEATURE_STRONGARM,
1242     ARM_FEATURE_VAPA, /* cp15 VA to PA lookups */
1243     ARM_FEATURE_ARM_DIV, /* divide supported in ARM encoding */
1244     ARM_FEATURE_VFP4, /* VFPv4 (implies that NEON is v2) */
1245     ARM_FEATURE_GENERIC_TIMER,
1246     ARM_FEATURE_MVFR, /* Media and VFP Feature Registers 0 and 1 */
1247     ARM_FEATURE_DUMMY_C15_REGS, /* RAZ/WI all of cp15 crn=15 */
1248     ARM_FEATURE_CACHE_TEST_CLEAN, /* 926/1026 style test-and-clean ops */
1249     ARM_FEATURE_CACHE_DIRTY_REG, /* 1136/1176 cache dirty status register */
1250     ARM_FEATURE_CACHE_BLOCK_OPS, /* v6 optional cache block operations */
1251     ARM_FEATURE_MPIDR, /* has cp15 MPIDR */
1252     ARM_FEATURE_PXN, /* has Privileged Execute Never bit */
1253     ARM_FEATURE_LPAE, /* has Large Physical Address Extension */
1254     ARM_FEATURE_V8,
1255     ARM_FEATURE_AARCH64, /* supports 64 bit mode */
1256     ARM_FEATURE_V8_AES, /* implements AES part of v8 Crypto Extensions */
1257     ARM_FEATURE_CBAR, /* has cp15 CBAR */
1258     ARM_FEATURE_CRC, /* ARMv8 CRC instructions */
1259     ARM_FEATURE_CBAR_RO, /* has cp15 CBAR and it is read-only */
1260     ARM_FEATURE_EL2, /* has EL2 Virtualization support */
1261     ARM_FEATURE_EL3, /* has EL3 Secure monitor support */
1262     ARM_FEATURE_V8_SHA1, /* implements SHA1 part of v8 Crypto Extensions */
1263     ARM_FEATURE_V8_SHA256, /* implements SHA256 part of v8 Crypto Extensions */
1264     ARM_FEATURE_V8_PMULL, /* implements PMULL part of v8 Crypto Extensions */
1265     ARM_FEATURE_THUMB_DSP, /* DSP insns supported in the Thumb encodings */
1266     ARM_FEATURE_PMU, /* has PMU support */
1267     ARM_FEATURE_VBAR, /* has cp15 VBAR */
1268     ARM_FEATURE_M_SECURITY, /* M profile Security Extension */
1269 };
1270
1271 static inline int arm_feature(CPUARMState *env, int feature)
1272 {
1273     return (env->features & (1ULL << feature)) != 0;
1274 }
1275
1276 #if !defined(CONFIG_USER_ONLY)
1277 /* Return true if exception levels below EL3 are in secure state,
1278  * or would be following an exception return to that level.
1279  * Unlike arm_is_secure() (which is always a question about the
1280  * _current_ state of the CPU) this doesn't care about the current
1281  * EL or mode.
1282  */
1283 static inline bool arm_is_secure_below_el3(CPUARMState *env)
1284 {
1285     if (arm_feature(env, ARM_FEATURE_EL3)) {
1286         return !(env->cp15.scr_el3 & SCR_NS);
1287     } else {
1288         /* If EL3 is not supported then the secure state is implementation
1289          * defined, in which case QEMU defaults to non-secure.
1290          */
1291         return false;
1292     }
1293 }
1294
1295 /* Return true if the CPU is AArch64 EL3 or AArch32 Mon */
1296 static inline bool arm_is_el3_or_mon(CPUARMState *env)
1297 {
1298     if (arm_feature(env, ARM_FEATURE_EL3)) {
1299         if (is_a64(env) && extract32(env->pstate, 2, 2) == 3) {
1300             /* CPU currently in AArch64 state and EL3 */
1301             return true;
1302         } else if (!is_a64(env) &&
1303                 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
1304             /* CPU currently in AArch32 state and monitor mode */
1305             return true;
1306         }
1307     }
1308     return false;
1309 }
1310
1311 /* Return true if the processor is in secure state */
1312 static inline bool arm_is_secure(CPUARMState *env)
1313 {
1314     if (arm_is_el3_or_mon(env)) {
1315         return true;
1316     }
1317     return arm_is_secure_below_el3(env);
1318 }
1319
1320 #else
1321 static inline bool arm_is_secure_below_el3(CPUARMState *env)
1322 {
1323     return false;
1324 }
1325
1326 static inline bool arm_is_secure(CPUARMState *env)
1327 {
1328     return false;
1329 }
1330 #endif
1331
1332 /* Return true if the specified exception level is running in AArch64 state. */
1333 static inline bool arm_el_is_aa64(CPUARMState *env, int el)
1334 {
1335     /* This isn't valid for EL0 (if we're in EL0, is_a64() is what you want,
1336      * and if we're not in EL0 then the state of EL0 isn't well defined.)
1337      */
1338     assert(el >= 1 && el <= 3);
1339     bool aa64 = arm_feature(env, ARM_FEATURE_AARCH64);
1340
1341     /* The highest exception level is always at the maximum supported
1342      * register width, and then lower levels have a register width controlled
1343      * by bits in the SCR or HCR registers.
1344      */
1345     if (el == 3) {
1346         return aa64;
1347     }
1348
1349     if (arm_feature(env, ARM_FEATURE_EL3)) {
1350         aa64 = aa64 && (env->cp15.scr_el3 & SCR_RW);
1351     }
1352
1353     if (el == 2) {
1354         return aa64;
1355     }
1356
1357     if (arm_feature(env, ARM_FEATURE_EL2) && !arm_is_secure_below_el3(env)) {
1358         aa64 = aa64 && (env->cp15.hcr_el2 & HCR_RW);
1359     }
1360
1361     return aa64;
1362 }
1363
1364 /* Function for determing whether guest cp register reads and writes should
1365  * access the secure or non-secure bank of a cp register.  When EL3 is
1366  * operating in AArch32 state, the NS-bit determines whether the secure
1367  * instance of a cp register should be used. When EL3 is AArch64 (or if
1368  * it doesn't exist at all) then there is no register banking, and all
1369  * accesses are to the non-secure version.
1370  */
1371 static inline bool access_secure_reg(CPUARMState *env)
1372 {
1373     bool ret = (arm_feature(env, ARM_FEATURE_EL3) &&
1374                 !arm_el_is_aa64(env, 3) &&
1375                 !(env->cp15.scr_el3 & SCR_NS));
1376
1377     return ret;
1378 }
1379
1380 /* Macros for accessing a specified CP register bank */
1381 #define A32_BANKED_REG_GET(_env, _regname, _secure)    \
1382     ((_secure) ? (_env)->cp15._regname##_s : (_env)->cp15._regname##_ns)
1383
1384 #define A32_BANKED_REG_SET(_env, _regname, _secure, _val)   \
1385     do {                                                \
1386         if (_secure) {                                   \
1387             (_env)->cp15._regname##_s = (_val);            \
1388         } else {                                        \
1389             (_env)->cp15._regname##_ns = (_val);           \
1390         }                                               \
1391     } while (0)
1392
1393 /* Macros for automatically accessing a specific CP register bank depending on
1394  * the current secure state of the system.  These macros are not intended for
1395  * supporting instruction translation reads/writes as these are dependent
1396  * solely on the SCR.NS bit and not the mode.
1397  */
1398 #define A32_BANKED_CURRENT_REG_GET(_env, _regname)        \
1399     A32_BANKED_REG_GET((_env), _regname,                \
1400                        (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)))
1401
1402 #define A32_BANKED_CURRENT_REG_SET(_env, _regname, _val)                       \
1403     A32_BANKED_REG_SET((_env), _regname,                                    \
1404                        (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)), \
1405                        (_val))
1406
1407 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf);
1408 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
1409                                  uint32_t cur_el, bool secure);
1410
1411 /* Interface between CPU and Interrupt controller.  */
1412 #ifndef CONFIG_USER_ONLY
1413 bool armv7m_nvic_can_take_pending_exception(void *opaque);
1414 #else
1415 static inline bool armv7m_nvic_can_take_pending_exception(void *opaque)
1416 {
1417     return true;
1418 }
1419 #endif
1420 void armv7m_nvic_set_pending(void *opaque, int irq);
1421 void armv7m_nvic_acknowledge_irq(void *opaque);
1422 /**
1423  * armv7m_nvic_complete_irq: complete specified interrupt or exception
1424  * @opaque: the NVIC
1425  * @irq: the exception number to complete
1426  *
1427  * Returns: -1 if the irq was not active
1428  *           1 if completing this irq brought us back to base (no active irqs)
1429  *           0 if there is still an irq active after this one was completed
1430  * (Ignoring -1, this is the same as the RETTOBASE value before completion.)
1431  */
1432 int armv7m_nvic_complete_irq(void *opaque, int irq);
1433
1434 /* Interface for defining coprocessor registers.
1435  * Registers are defined in tables of arm_cp_reginfo structs
1436  * which are passed to define_arm_cp_regs().
1437  */
1438
1439 /* When looking up a coprocessor register we look for it
1440  * via an integer which encodes all of:
1441  *  coprocessor number
1442  *  Crn, Crm, opc1, opc2 fields
1443  *  32 or 64 bit register (ie is it accessed via MRC/MCR
1444  *    or via MRRC/MCRR?)
1445  *  non-secure/secure bank (AArch32 only)
1446  * We allow 4 bits for opc1 because MRRC/MCRR have a 4 bit field.
1447  * (In this case crn and opc2 should be zero.)
1448  * For AArch64, there is no 32/64 bit size distinction;
1449  * instead all registers have a 2 bit op0, 3 bit op1 and op2,
1450  * and 4 bit CRn and CRm. The encoding patterns are chosen
1451  * to be easy to convert to and from the KVM encodings, and also
1452  * so that the hashtable can contain both AArch32 and AArch64
1453  * registers (to allow for interprocessing where we might run
1454  * 32 bit code on a 64 bit core).
1455  */
1456 /* This bit is private to our hashtable cpreg; in KVM register
1457  * IDs the AArch64/32 distinction is the KVM_REG_ARM/ARM64
1458  * in the upper bits of the 64 bit ID.
1459  */
1460 #define CP_REG_AA64_SHIFT 28
1461 #define CP_REG_AA64_MASK (1 << CP_REG_AA64_SHIFT)
1462
1463 /* To enable banking of coprocessor registers depending on ns-bit we
1464  * add a bit to distinguish between secure and non-secure cpregs in the
1465  * hashtable.
1466  */
1467 #define CP_REG_NS_SHIFT 29
1468 #define CP_REG_NS_MASK (1 << CP_REG_NS_SHIFT)
1469
1470 #define ENCODE_CP_REG(cp, is64, ns, crn, crm, opc1, opc2)   \
1471     ((ns) << CP_REG_NS_SHIFT | ((cp) << 16) | ((is64) << 15) |   \
1472      ((crn) << 11) | ((crm) << 7) | ((opc1) << 3) | (opc2))
1473
1474 #define ENCODE_AA64_CP_REG(cp, crn, crm, op0, op1, op2) \
1475     (CP_REG_AA64_MASK |                                 \
1476      ((cp) << CP_REG_ARM_COPROC_SHIFT) |                \
1477      ((op0) << CP_REG_ARM64_SYSREG_OP0_SHIFT) |         \
1478      ((op1) << CP_REG_ARM64_SYSREG_OP1_SHIFT) |         \
1479      ((crn) << CP_REG_ARM64_SYSREG_CRN_SHIFT) |         \
1480      ((crm) << CP_REG_ARM64_SYSREG_CRM_SHIFT) |         \
1481      ((op2) << CP_REG_ARM64_SYSREG_OP2_SHIFT))
1482
1483 /* Convert a full 64 bit KVM register ID to the truncated 32 bit
1484  * version used as a key for the coprocessor register hashtable
1485  */
1486 static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
1487 {
1488     uint32_t cpregid = kvmid;
1489     if ((kvmid & CP_REG_ARCH_MASK) == CP_REG_ARM64) {
1490         cpregid |= CP_REG_AA64_MASK;
1491     } else {
1492         if ((kvmid & CP_REG_SIZE_MASK) == CP_REG_SIZE_U64) {
1493             cpregid |= (1 << 15);
1494         }
1495
1496         /* KVM is always non-secure so add the NS flag on AArch32 register
1497          * entries.
1498          */
1499          cpregid |= 1 << CP_REG_NS_SHIFT;
1500     }
1501     return cpregid;
1502 }
1503
1504 /* Convert a truncated 32 bit hashtable key into the full
1505  * 64 bit KVM register ID.
1506  */
1507 static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)
1508 {
1509     uint64_t kvmid;
1510
1511     if (cpregid & CP_REG_AA64_MASK) {
1512         kvmid = cpregid & ~CP_REG_AA64_MASK;
1513         kvmid |= CP_REG_SIZE_U64 | CP_REG_ARM64;
1514     } else {
1515         kvmid = cpregid & ~(1 << 15);
1516         if (cpregid & (1 << 15)) {
1517             kvmid |= CP_REG_SIZE_U64 | CP_REG_ARM;
1518         } else {
1519             kvmid |= CP_REG_SIZE_U32 | CP_REG_ARM;
1520         }
1521     }
1522     return kvmid;
1523 }
1524
1525 /* ARMCPRegInfo type field bits. If the SPECIAL bit is set this is a
1526  * special-behaviour cp reg and bits [15..8] indicate what behaviour
1527  * it has. Otherwise it is a simple cp reg, where CONST indicates that
1528  * TCG can assume the value to be constant (ie load at translate time)
1529  * and 64BIT indicates a 64 bit wide coprocessor register. SUPPRESS_TB_END
1530  * indicates that the TB should not be ended after a write to this register
1531  * (the default is that the TB ends after cp writes). OVERRIDE permits
1532  * a register definition to override a previous definition for the
1533  * same (cp, is64, crn, crm, opc1, opc2) tuple: either the new or the
1534  * old must have the OVERRIDE bit set.
1535  * ALIAS indicates that this register is an alias view of some underlying
1536  * state which is also visible via another register, and that the other
1537  * register is handling migration and reset; registers marked ALIAS will not be
1538  * migrated but may have their state set by syncing of register state from KVM.
1539  * NO_RAW indicates that this register has no underlying state and does not
1540  * support raw access for state saving/loading; it will not be used for either
1541  * migration or KVM state synchronization. (Typically this is for "registers"
1542  * which are actually used as instructions for cache maintenance and so on.)
1543  * IO indicates that this register does I/O and therefore its accesses
1544  * need to be surrounded by gen_io_start()/gen_io_end(). In particular,
1545  * registers which implement clocks or timers require this.
1546  */
1547 #define ARM_CP_SPECIAL 1
1548 #define ARM_CP_CONST 2
1549 #define ARM_CP_64BIT 4
1550 #define ARM_CP_SUPPRESS_TB_END 8
1551 #define ARM_CP_OVERRIDE 16
1552 #define ARM_CP_ALIAS 32
1553 #define ARM_CP_IO 64
1554 #define ARM_CP_NO_RAW 128
1555 #define ARM_CP_NOP (ARM_CP_SPECIAL | (1 << 8))
1556 #define ARM_CP_WFI (ARM_CP_SPECIAL | (2 << 8))
1557 #define ARM_CP_NZCV (ARM_CP_SPECIAL | (3 << 8))
1558 #define ARM_CP_CURRENTEL (ARM_CP_SPECIAL | (4 << 8))
1559 #define ARM_CP_DC_ZVA (ARM_CP_SPECIAL | (5 << 8))
1560 #define ARM_LAST_SPECIAL ARM_CP_DC_ZVA
1561 /* Used only as a terminator for ARMCPRegInfo lists */
1562 #define ARM_CP_SENTINEL 0xffff
1563 /* Mask of only the flag bits in a type field */
1564 #define ARM_CP_FLAG_MASK 0xff
1565
1566 /* Valid values for ARMCPRegInfo state field, indicating which of
1567  * the AArch32 and AArch64 execution states this register is visible in.
1568  * If the reginfo doesn't explicitly specify then it is AArch32 only.
1569  * If the reginfo is declared to be visible in both states then a second
1570  * reginfo is synthesised for the AArch32 view of the AArch64 register,
1571  * such that the AArch32 view is the lower 32 bits of the AArch64 one.
1572  * Note that we rely on the values of these enums as we iterate through
1573  * the various states in some places.
1574  */
1575 enum {
1576     ARM_CP_STATE_AA32 = 0,
1577     ARM_CP_STATE_AA64 = 1,
1578     ARM_CP_STATE_BOTH = 2,
1579 };
1580
1581 /* ARM CP register secure state flags.  These flags identify security state
1582  * attributes for a given CP register entry.
1583  * The existence of both or neither secure and non-secure flags indicates that
1584  * the register has both a secure and non-secure hash entry.  A single one of
1585  * these flags causes the register to only be hashed for the specified
1586  * security state.
1587  * Although definitions may have any combination of the S/NS bits, each
1588  * registered entry will only have one to identify whether the entry is secure
1589  * or non-secure.
1590  */
1591 enum {
1592     ARM_CP_SECSTATE_S =   (1 << 0), /* bit[0]: Secure state register */
1593     ARM_CP_SECSTATE_NS =  (1 << 1), /* bit[1]: Non-secure state register */
1594 };
1595
1596 /* Return true if cptype is a valid type field. This is used to try to
1597  * catch errors where the sentinel has been accidentally left off the end
1598  * of a list of registers.
1599  */
1600 static inline bool cptype_valid(int cptype)
1601 {
1602     return ((cptype & ~ARM_CP_FLAG_MASK) == 0)
1603         || ((cptype & ARM_CP_SPECIAL) &&
1604             ((cptype & ~ARM_CP_FLAG_MASK) <= ARM_LAST_SPECIAL));
1605 }
1606
1607 /* Access rights:
1608  * We define bits for Read and Write access for what rev C of the v7-AR ARM ARM
1609  * defines as PL0 (user), PL1 (fiq/irq/svc/abt/und/sys, ie privileged), and
1610  * PL2 (hyp). The other level which has Read and Write bits is Secure PL1
1611  * (ie any of the privileged modes in Secure state, or Monitor mode).
1612  * If a register is accessible in one privilege level it's always accessible
1613  * in higher privilege levels too. Since "Secure PL1" also follows this rule
1614  * (ie anything visible in PL2 is visible in S-PL1, some things are only
1615  * visible in S-PL1) but "Secure PL1" is a bit of a mouthful, we bend the
1616  * terminology a little and call this PL3.
1617  * In AArch64 things are somewhat simpler as the PLx bits line up exactly
1618  * with the ELx exception levels.
1619  *
1620  * If access permissions for a register are more complex than can be
1621  * described with these bits, then use a laxer set of restrictions, and
1622  * do the more restrictive/complex check inside a helper function.
1623  */
1624 #define PL3_R 0x80
1625 #define PL3_W 0x40
1626 #define PL2_R (0x20 | PL3_R)
1627 #define PL2_W (0x10 | PL3_W)
1628 #define PL1_R (0x08 | PL2_R)
1629 #define PL1_W (0x04 | PL2_W)
1630 #define PL0_R (0x02 | PL1_R)
1631 #define PL0_W (0x01 | PL1_W)
1632
1633 #define PL3_RW (PL3_R | PL3_W)
1634 #define PL2_RW (PL2_R | PL2_W)
1635 #define PL1_RW (PL1_R | PL1_W)
1636 #define PL0_RW (PL0_R | PL0_W)
1637
1638 /* Return the highest implemented Exception Level */
1639 static inline int arm_highest_el(CPUARMState *env)
1640 {
1641     if (arm_feature(env, ARM_FEATURE_EL3)) {
1642         return 3;
1643     }
1644     if (arm_feature(env, ARM_FEATURE_EL2)) {
1645         return 2;
1646     }
1647     return 1;
1648 }
1649
1650 /* Return true if a v7M CPU is in Handler mode */
1651 static inline bool arm_v7m_is_handler_mode(CPUARMState *env)
1652 {
1653     return env->v7m.exception != 0;
1654 }
1655
1656 /* Return the current Exception Level (as per ARMv8; note that this differs
1657  * from the ARMv7 Privilege Level).
1658  */
1659 static inline int arm_current_el(CPUARMState *env)
1660 {
1661     if (arm_feature(env, ARM_FEATURE_M)) {
1662         return arm_v7m_is_handler_mode(env) || !(env->v7m.control & 1);
1663     }
1664
1665     if (is_a64(env)) {
1666         return extract32(env->pstate, 2, 2);
1667     }
1668
1669     switch (env->uncached_cpsr & 0x1f) {
1670     case ARM_CPU_MODE_USR:
1671         return 0;
1672     case ARM_CPU_MODE_HYP:
1673         return 2;
1674     case ARM_CPU_MODE_MON:
1675         return 3;
1676     default:
1677         if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
1678             /* If EL3 is 32-bit then all secure privileged modes run in
1679              * EL3
1680              */
1681             return 3;
1682         }
1683
1684         return 1;
1685     }
1686 }
1687
1688 typedef struct ARMCPRegInfo ARMCPRegInfo;
1689
1690 typedef enum CPAccessResult {
1691     /* Access is permitted */
1692     CP_ACCESS_OK = 0,
1693     /* Access fails due to a configurable trap or enable which would
1694      * result in a categorized exception syndrome giving information about
1695      * the failing instruction (ie syndrome category 0x3, 0x4, 0x5, 0x6,
1696      * 0xc or 0x18). The exception is taken to the usual target EL (EL1 or
1697      * PL1 if in EL0, otherwise to the current EL).
1698      */
1699     CP_ACCESS_TRAP = 1,
1700     /* Access fails and results in an exception syndrome 0x0 ("uncategorized").
1701      * Note that this is not a catch-all case -- the set of cases which may
1702      * result in this failure is specifically defined by the architecture.
1703      */
1704     CP_ACCESS_TRAP_UNCATEGORIZED = 2,
1705     /* As CP_ACCESS_TRAP, but for traps directly to EL2 or EL3 */
1706     CP_ACCESS_TRAP_EL2 = 3,
1707     CP_ACCESS_TRAP_EL3 = 4,
1708     /* As CP_ACCESS_UNCATEGORIZED, but for traps directly to EL2 or EL3 */
1709     CP_ACCESS_TRAP_UNCATEGORIZED_EL2 = 5,
1710     CP_ACCESS_TRAP_UNCATEGORIZED_EL3 = 6,
1711     /* Access fails and results in an exception syndrome for an FP access,
1712      * trapped directly to EL2 or EL3
1713      */
1714     CP_ACCESS_TRAP_FP_EL2 = 7,
1715     CP_ACCESS_TRAP_FP_EL3 = 8,
1716 } CPAccessResult;
1717
1718 /* Access functions for coprocessor registers. These cannot fail and
1719  * may not raise exceptions.
1720  */
1721 typedef uint64_t CPReadFn(CPUARMState *env, const ARMCPRegInfo *opaque);
1722 typedef void CPWriteFn(CPUARMState *env, const ARMCPRegInfo *opaque,
1723                        uint64_t value);
1724 /* Access permission check functions for coprocessor registers. */
1725 typedef CPAccessResult CPAccessFn(CPUARMState *env,
1726                                   const ARMCPRegInfo *opaque,
1727                                   bool isread);
1728 /* Hook function for register reset */
1729 typedef void CPResetFn(CPUARMState *env, const ARMCPRegInfo *opaque);
1730
1731 #define CP_ANY 0xff
1732
1733 /* Definition of an ARM coprocessor register */
1734 struct ARMCPRegInfo {
1735     /* Name of register (useful mainly for debugging, need not be unique) */
1736     const char *name;
1737     /* Location of register: coprocessor number and (crn,crm,opc1,opc2)
1738      * tuple. Any of crm, opc1 and opc2 may be CP_ANY to indicate a
1739      * 'wildcard' field -- any value of that field in the MRC/MCR insn
1740      * will be decoded to this register. The register read and write
1741      * callbacks will be passed an ARMCPRegInfo with the crn/crm/opc1/opc2
1742      * used by the program, so it is possible to register a wildcard and
1743      * then behave differently on read/write if necessary.
1744      * For 64 bit registers, only crm and opc1 are relevant; crn and opc2
1745      * must both be zero.
1746      * For AArch64-visible registers, opc0 is also used.
1747      * Since there are no "coprocessors" in AArch64, cp is purely used as a
1748      * way to distinguish (for KVM's benefit) guest-visible system registers
1749      * from demuxed ones provided to preserve the "no side effects on
1750      * KVM register read/write from QEMU" semantics. cp==0x13 is guest
1751      * visible (to match KVM's encoding); cp==0 will be converted to
1752      * cp==0x13 when the ARMCPRegInfo is registered, for convenience.
1753      */
1754     uint8_t cp;
1755     uint8_t crn;
1756     uint8_t crm;
1757     uint8_t opc0;
1758     uint8_t opc1;
1759     uint8_t opc2;
1760     /* Execution state in which this register is visible: ARM_CP_STATE_* */
1761     int state;
1762     /* Register type: ARM_CP_* bits/values */
1763     int type;
1764     /* Access rights: PL*_[RW] */
1765     int access;
1766     /* Security state: ARM_CP_SECSTATE_* bits/values */
1767     int secure;
1768     /* The opaque pointer passed to define_arm_cp_regs_with_opaque() when
1769      * this register was defined: can be used to hand data through to the
1770      * register read/write functions, since they are passed the ARMCPRegInfo*.
1771      */
1772     void *opaque;
1773     /* Value of this register, if it is ARM_CP_CONST. Otherwise, if
1774      * fieldoffset is non-zero, the reset value of the register.
1775      */
1776     uint64_t resetvalue;
1777     /* Offset of the field in CPUARMState for this register.
1778      *
1779      * This is not needed if either:
1780      *  1. type is ARM_CP_CONST or one of the ARM_CP_SPECIALs
1781      *  2. both readfn and writefn are specified
1782      */
1783     ptrdiff_t fieldoffset; /* offsetof(CPUARMState, field) */
1784
1785     /* Offsets of the secure and non-secure fields in CPUARMState for the
1786      * register if it is banked.  These fields are only used during the static
1787      * registration of a register.  During hashing the bank associated
1788      * with a given security state is copied to fieldoffset which is used from
1789      * there on out.
1790      *
1791      * It is expected that register definitions use either fieldoffset or
1792      * bank_fieldoffsets in the definition but not both.  It is also expected
1793      * that both bank offsets are set when defining a banked register.  This
1794      * use indicates that a register is banked.
1795      */
1796     ptrdiff_t bank_fieldoffsets[2];
1797
1798     /* Function for making any access checks for this register in addition to
1799      * those specified by the 'access' permissions bits. If NULL, no extra
1800      * checks required. The access check is performed at runtime, not at
1801      * translate time.
1802      */
1803     CPAccessFn *accessfn;
1804     /* Function for handling reads of this register. If NULL, then reads
1805      * will be done by loading from the offset into CPUARMState specified
1806      * by fieldoffset.
1807      */
1808     CPReadFn *readfn;
1809     /* Function for handling writes of this register. If NULL, then writes
1810      * will be done by writing to the offset into CPUARMState specified
1811      * by fieldoffset.
1812      */
1813     CPWriteFn *writefn;
1814     /* Function for doing a "raw" read; used when we need to copy
1815      * coprocessor state to the kernel for KVM or out for
1816      * migration. This only needs to be provided if there is also a
1817      * readfn and it has side effects (for instance clear-on-read bits).
1818      */
1819     CPReadFn *raw_readfn;
1820     /* Function for doing a "raw" write; used when we need to copy KVM
1821      * kernel coprocessor state into userspace, or for inbound
1822      * migration. This only needs to be provided if there is also a
1823      * writefn and it masks out "unwritable" bits or has write-one-to-clear
1824      * or similar behaviour.
1825      */
1826     CPWriteFn *raw_writefn;
1827     /* Function for resetting the register. If NULL, then reset will be done
1828      * by writing resetvalue to the field specified in fieldoffset. If
1829      * fieldoffset is 0 then no reset will be done.
1830      */
1831     CPResetFn *resetfn;
1832 };
1833
1834 /* Macros which are lvalues for the field in CPUARMState for the
1835  * ARMCPRegInfo *ri.
1836  */
1837 #define CPREG_FIELD32(env, ri) \
1838     (*(uint32_t *)((char *)(env) + (ri)->fieldoffset))
1839 #define CPREG_FIELD64(env, ri) \
1840     (*(uint64_t *)((char *)(env) + (ri)->fieldoffset))
1841
1842 #define REGINFO_SENTINEL { .type = ARM_CP_SENTINEL }
1843
1844 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1845                                     const ARMCPRegInfo *regs, void *opaque);
1846 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
1847                                        const ARMCPRegInfo *regs, void *opaque);
1848 static inline void define_arm_cp_regs(ARMCPU *cpu, const ARMCPRegInfo *regs)
1849 {
1850     define_arm_cp_regs_with_opaque(cpu, regs, 0);
1851 }
1852 static inline void define_one_arm_cp_reg(ARMCPU *cpu, const ARMCPRegInfo *regs)
1853 {
1854     define_one_arm_cp_reg_with_opaque(cpu, regs, 0);
1855 }
1856 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp);
1857
1858 /* CPWriteFn that can be used to implement writes-ignored behaviour */
1859 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1860                          uint64_t value);
1861 /* CPReadFn that can be used for read-as-zero behaviour */
1862 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri);
1863
1864 /* CPResetFn that does nothing, for use if no reset is required even
1865  * if fieldoffset is non zero.
1866  */
1867 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque);
1868
1869 /* Return true if this reginfo struct's field in the cpu state struct
1870  * is 64 bits wide.
1871  */
1872 static inline bool cpreg_field_is_64bit(const ARMCPRegInfo *ri)
1873 {
1874     return (ri->state == ARM_CP_STATE_AA64) || (ri->type & ARM_CP_64BIT);
1875 }
1876
1877 static inline bool cp_access_ok(int current_el,
1878                                 const ARMCPRegInfo *ri, int isread)
1879 {
1880     return (ri->access >> ((current_el * 2) + isread)) & 1;
1881 }
1882
1883 /* Raw read of a coprocessor register (as needed for migration, etc) */
1884 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri);
1885
1886 /**
1887  * write_list_to_cpustate
1888  * @cpu: ARMCPU
1889  *
1890  * For each register listed in the ARMCPU cpreg_indexes list, write
1891  * its value from the cpreg_values list into the ARMCPUState structure.
1892  * This updates TCG's working data structures from KVM data or
1893  * from incoming migration state.
1894  *
1895  * Returns: true if all register values were updated correctly,
1896  * false if some register was unknown or could not be written.
1897  * Note that we do not stop early on failure -- we will attempt
1898  * writing all registers in the list.
1899  */
1900 bool write_list_to_cpustate(ARMCPU *cpu);
1901
1902 /**
1903  * write_cpustate_to_list:
1904  * @cpu: ARMCPU
1905  *
1906  * For each register listed in the ARMCPU cpreg_indexes list, write
1907  * its value from the ARMCPUState structure into the cpreg_values list.
1908  * This is used to copy info from TCG's working data structures into
1909  * KVM or for outbound migration.
1910  *
1911  * Returns: true if all register values were read correctly,
1912  * false if some register was unknown or could not be read.
1913  * Note that we do not stop early on failure -- we will attempt
1914  * reading all registers in the list.
1915  */
1916 bool write_cpustate_to_list(ARMCPU *cpu);
1917
1918 #define ARM_CPUID_TI915T      0x54029152
1919 #define ARM_CPUID_TI925T      0x54029252
1920
1921 #if defined(CONFIG_USER_ONLY)
1922 #define TARGET_PAGE_BITS 12
1923 #else
1924 /* ARMv7 and later CPUs have 4K pages minimum, but ARMv5 and v6
1925  * have to support 1K tiny pages.
1926  */
1927 #define TARGET_PAGE_BITS_VARY
1928 #define TARGET_PAGE_BITS_MIN 10
1929 #endif
1930
1931 #if defined(TARGET_AARCH64)
1932 #  define TARGET_PHYS_ADDR_SPACE_BITS 48
1933 #  define TARGET_VIRT_ADDR_SPACE_BITS 64
1934 #else
1935 #  define TARGET_PHYS_ADDR_SPACE_BITS 40
1936 #  define TARGET_VIRT_ADDR_SPACE_BITS 32
1937 #endif
1938
1939 static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
1940                                      unsigned int target_el)
1941 {
1942     CPUARMState *env = cs->env_ptr;
1943     unsigned int cur_el = arm_current_el(env);
1944     bool secure = arm_is_secure(env);
1945     bool pstate_unmasked;
1946     int8_t unmasked = 0;
1947
1948     /* Don't take exceptions if they target a lower EL.
1949      * This check should catch any exceptions that would not be taken but left
1950      * pending.
1951      */
1952     if (cur_el > target_el) {
1953         return false;
1954     }
1955
1956     switch (excp_idx) {
1957     case EXCP_FIQ:
1958         pstate_unmasked = !(env->daif & PSTATE_F);
1959         break;
1960
1961     case EXCP_IRQ:
1962         pstate_unmasked = !(env->daif & PSTATE_I);
1963         break;
1964
1965     case EXCP_VFIQ:
1966         if (secure || !(env->cp15.hcr_el2 & HCR_FMO)) {
1967             /* VFIQs are only taken when hypervized and non-secure.  */
1968             return false;
1969         }
1970         return !(env->daif & PSTATE_F);
1971     case EXCP_VIRQ:
1972         if (secure || !(env->cp15.hcr_el2 & HCR_IMO)) {
1973             /* VIRQs are only taken when hypervized and non-secure.  */
1974             return false;
1975         }
1976         return !(env->daif & PSTATE_I);
1977     default:
1978         g_assert_not_reached();
1979     }
1980
1981     /* Use the target EL, current execution state and SCR/HCR settings to
1982      * determine whether the corresponding CPSR bit is used to mask the
1983      * interrupt.
1984      */
1985     if ((target_el > cur_el) && (target_el != 1)) {
1986         /* Exceptions targeting a higher EL may not be maskable */
1987         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
1988             /* 64-bit masking rules are simple: exceptions to EL3
1989              * can't be masked, and exceptions to EL2 can only be
1990              * masked from Secure state. The HCR and SCR settings
1991              * don't affect the masking logic, only the interrupt routing.
1992              */
1993             if (target_el == 3 || !secure) {
1994                 unmasked = 1;
1995             }
1996         } else {
1997             /* The old 32-bit-only environment has a more complicated
1998              * masking setup. HCR and SCR bits not only affect interrupt
1999              * routing but also change the behaviour of masking.
2000              */
2001             bool hcr, scr;
2002
2003             switch (excp_idx) {
2004             case EXCP_FIQ:
2005                 /* If FIQs are routed to EL3 or EL2 then there are cases where
2006                  * we override the CPSR.F in determining if the exception is
2007                  * masked or not. If neither of these are set then we fall back
2008                  * to the CPSR.F setting otherwise we further assess the state
2009                  * below.
2010                  */
2011                 hcr = (env->cp15.hcr_el2 & HCR_FMO);
2012                 scr = (env->cp15.scr_el3 & SCR_FIQ);
2013
2014                 /* When EL3 is 32-bit, the SCR.FW bit controls whether the
2015                  * CPSR.F bit masks FIQ interrupts when taken in non-secure
2016                  * state. If SCR.FW is set then FIQs can be masked by CPSR.F
2017                  * when non-secure but only when FIQs are only routed to EL3.
2018                  */
2019                 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
2020                 break;
2021             case EXCP_IRQ:
2022                 /* When EL3 execution state is 32-bit, if HCR.IMO is set then
2023                  * we may override the CPSR.I masking when in non-secure state.
2024                  * The SCR.IRQ setting has already been taken into consideration
2025                  * when setting the target EL, so it does not have a further
2026                  * affect here.
2027                  */
2028                 hcr = (env->cp15.hcr_el2 & HCR_IMO);
2029                 scr = false;
2030                 break;
2031             default:
2032                 g_assert_not_reached();
2033             }
2034
2035             if ((scr || hcr) && !secure) {
2036                 unmasked = 1;
2037             }
2038         }
2039     }
2040
2041     /* The PSTATE bits only mask the interrupt if we have not overriden the
2042      * ability above.
2043      */
2044     return unmasked || pstate_unmasked;
2045 }
2046
2047 #define cpu_init(cpu_model) cpu_generic_init(TYPE_ARM_CPU, cpu_model)
2048
2049 #define cpu_signal_handler cpu_arm_signal_handler
2050 #define cpu_list arm_cpu_list
2051
2052 /* ARM has the following "translation regimes" (as the ARM ARM calls them):
2053  *
2054  * If EL3 is 64-bit:
2055  *  + NonSecure EL1 & 0 stage 1
2056  *  + NonSecure EL1 & 0 stage 2
2057  *  + NonSecure EL2
2058  *  + Secure EL1 & EL0
2059  *  + Secure EL3
2060  * If EL3 is 32-bit:
2061  *  + NonSecure PL1 & 0 stage 1
2062  *  + NonSecure PL1 & 0 stage 2
2063  *  + NonSecure PL2
2064  *  + Secure PL0 & PL1
2065  * (reminder: for 32 bit EL3, Secure PL1 is *EL3*, not EL1.)
2066  *
2067  * For QEMU, an mmu_idx is not quite the same as a translation regime because:
2068  *  1. we need to split the "EL1 & 0" regimes into two mmu_idxes, because they
2069  *     may differ in access permissions even if the VA->PA map is the same
2070  *  2. we want to cache in our TLB the full VA->IPA->PA lookup for a stage 1+2
2071  *     translation, which means that we have one mmu_idx that deals with two
2072  *     concatenated translation regimes [this sort of combined s1+2 TLB is
2073  *     architecturally permitted]
2074  *  3. we don't need to allocate an mmu_idx to translations that we won't be
2075  *     handling via the TLB. The only way to do a stage 1 translation without
2076  *     the immediate stage 2 translation is via the ATS or AT system insns,
2077  *     which can be slow-pathed and always do a page table walk.
2078  *  4. we can also safely fold together the "32 bit EL3" and "64 bit EL3"
2079  *     translation regimes, because they map reasonably well to each other
2080  *     and they can't both be active at the same time.
2081  * This gives us the following list of mmu_idx values:
2082  *
2083  * NS EL0 (aka NS PL0) stage 1+2
2084  * NS EL1 (aka NS PL1) stage 1+2
2085  * NS EL2 (aka NS PL2)
2086  * S EL3 (aka S PL1)
2087  * S EL0 (aka S PL0)
2088  * S EL1 (not used if EL3 is 32 bit)
2089  * NS EL0+1 stage 2
2090  *
2091  * (The last of these is an mmu_idx because we want to be able to use the TLB
2092  * for the accesses done as part of a stage 1 page table walk, rather than
2093  * having to walk the stage 2 page table over and over.)
2094  *
2095  * R profile CPUs have an MPU, but can use the same set of MMU indexes
2096  * as A profile. They only need to distinguish NS EL0 and NS EL1 (and
2097  * NS EL2 if we ever model a Cortex-R52).
2098  *
2099  * M profile CPUs are rather different as they do not have a true MMU.
2100  * They have the following different MMU indexes:
2101  *  User
2102  *  Privileged
2103  *  Execution priority negative (this is like privileged, but the
2104  *  MPU HFNMIENA bit means that it may have different access permission
2105  *  check results to normal privileged code, so can't share a TLB).
2106  * If the CPU supports the v8M Security Extension then there are also:
2107  *  Secure User
2108  *  Secure Privileged
2109  *  Secure, execution priority negative
2110  *
2111  * The ARMMMUIdx and the mmu index value used by the core QEMU TLB code
2112  * are not quite the same -- different CPU types (most notably M profile
2113  * vs A/R profile) would like to use MMU indexes with different semantics,
2114  * but since we don't ever need to use all of those in a single CPU we
2115  * can avoid setting NB_MMU_MODES to more than 8. The lower bits of
2116  * ARMMMUIdx are the core TLB mmu index, and the higher bits are always
2117  * the same for any particular CPU.
2118  * Variables of type ARMMUIdx are always full values, and the core
2119  * index values are in variables of type 'int'.
2120  *
2121  * Our enumeration includes at the end some entries which are not "true"
2122  * mmu_idx values in that they don't have corresponding TLBs and are only
2123  * valid for doing slow path page table walks.
2124  *
2125  * The constant names here are patterned after the general style of the names
2126  * of the AT/ATS operations.
2127  * The values used are carefully arranged to make mmu_idx => EL lookup easy.
2128  */
2129 #define ARM_MMU_IDX_A 0x10 /* A profile */
2130 #define ARM_MMU_IDX_NOTLB 0x20 /* does not have a TLB */
2131 #define ARM_MMU_IDX_M 0x40 /* M profile */
2132
2133 #define ARM_MMU_IDX_TYPE_MASK (~0x7)
2134 #define ARM_MMU_IDX_COREIDX_MASK 0x7
2135
2136 typedef enum ARMMMUIdx {
2137     ARMMMUIdx_S12NSE0 = 0 | ARM_MMU_IDX_A,
2138     ARMMMUIdx_S12NSE1 = 1 | ARM_MMU_IDX_A,
2139     ARMMMUIdx_S1E2 = 2 | ARM_MMU_IDX_A,
2140     ARMMMUIdx_S1E3 = 3 | ARM_MMU_IDX_A,
2141     ARMMMUIdx_S1SE0 = 4 | ARM_MMU_IDX_A,
2142     ARMMMUIdx_S1SE1 = 5 | ARM_MMU_IDX_A,
2143     ARMMMUIdx_S2NS = 6 | ARM_MMU_IDX_A,
2144     ARMMMUIdx_MUser = 0 | ARM_MMU_IDX_M,
2145     ARMMMUIdx_MPriv = 1 | ARM_MMU_IDX_M,
2146     ARMMMUIdx_MNegPri = 2 | ARM_MMU_IDX_M,
2147     ARMMMUIdx_MSUser = 3 | ARM_MMU_IDX_M,
2148     ARMMMUIdx_MSPriv = 4 | ARM_MMU_IDX_M,
2149     ARMMMUIdx_MSNegPri = 5 | ARM_MMU_IDX_M,
2150     /* Indexes below here don't have TLBs and are used only for AT system
2151      * instructions or for the first stage of an S12 page table walk.
2152      */
2153     ARMMMUIdx_S1NSE0 = 0 | ARM_MMU_IDX_NOTLB,
2154     ARMMMUIdx_S1NSE1 = 1 | ARM_MMU_IDX_NOTLB,
2155 } ARMMMUIdx;
2156
2157 /* Bit macros for the core-mmu-index values for each index,
2158  * for use when calling tlb_flush_by_mmuidx() and friends.
2159  */
2160 typedef enum ARMMMUIdxBit {
2161     ARMMMUIdxBit_S12NSE0 = 1 << 0,
2162     ARMMMUIdxBit_S12NSE1 = 1 << 1,
2163     ARMMMUIdxBit_S1E2 = 1 << 2,
2164     ARMMMUIdxBit_S1E3 = 1 << 3,
2165     ARMMMUIdxBit_S1SE0 = 1 << 4,
2166     ARMMMUIdxBit_S1SE1 = 1 << 5,
2167     ARMMMUIdxBit_S2NS = 1 << 6,
2168     ARMMMUIdxBit_MUser = 1 << 0,
2169     ARMMMUIdxBit_MPriv = 1 << 1,
2170     ARMMMUIdxBit_MNegPri = 1 << 2,
2171     ARMMMUIdxBit_MSUser = 1 << 3,
2172     ARMMMUIdxBit_MSPriv = 1 << 4,
2173     ARMMMUIdxBit_MSNegPri = 1 << 5,
2174 } ARMMMUIdxBit;
2175
2176 #define MMU_USER_IDX 0
2177
2178 static inline int arm_to_core_mmu_idx(ARMMMUIdx mmu_idx)
2179 {
2180     return mmu_idx & ARM_MMU_IDX_COREIDX_MASK;
2181 }
2182
2183 static inline ARMMMUIdx core_to_arm_mmu_idx(CPUARMState *env, int mmu_idx)
2184 {
2185     if (arm_feature(env, ARM_FEATURE_M)) {
2186         return mmu_idx | ARM_MMU_IDX_M;
2187     } else {
2188         return mmu_idx | ARM_MMU_IDX_A;
2189     }
2190 }
2191
2192 /* Return the exception level we're running at if this is our mmu_idx */
2193 static inline int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
2194 {
2195     switch (mmu_idx & ARM_MMU_IDX_TYPE_MASK) {
2196     case ARM_MMU_IDX_A:
2197         return mmu_idx & 3;
2198     case ARM_MMU_IDX_M:
2199         return (mmu_idx == ARMMMUIdx_MUser || mmu_idx == ARMMMUIdx_MSUser)
2200             ? 0 : 1;
2201     default:
2202         g_assert_not_reached();
2203     }
2204 }
2205
2206 /* Determine the current mmu_idx to use for normal loads/stores */
2207 static inline int cpu_mmu_index(CPUARMState *env, bool ifetch)
2208 {
2209     int el = arm_current_el(env);
2210
2211     if (arm_feature(env, ARM_FEATURE_M)) {
2212         ARMMMUIdx mmu_idx = el == 0 ? ARMMMUIdx_MUser : ARMMMUIdx_MPriv;
2213
2214         /* Execution priority is negative if FAULTMASK is set or
2215          * we're in a HardFault or NMI handler.
2216          */
2217         if ((env->v7m.exception > 0 && env->v7m.exception <= 3)
2218             || env->v7m.faultmask) {
2219             mmu_idx = ARMMMUIdx_MNegPri;
2220         }
2221
2222         if (env->v7m.secure) {
2223             mmu_idx += ARMMMUIdx_MSUser;
2224         }
2225
2226         return arm_to_core_mmu_idx(mmu_idx);
2227     }
2228
2229     if (el < 2 && arm_is_secure_below_el3(env)) {
2230         return arm_to_core_mmu_idx(ARMMMUIdx_S1SE0 + el);
2231     }
2232     return el;
2233 }
2234
2235 /* Indexes used when registering address spaces with cpu_address_space_init */
2236 typedef enum ARMASIdx {
2237     ARMASIdx_NS = 0,
2238     ARMASIdx_S = 1,
2239 } ARMASIdx;
2240
2241 /* Return the Exception Level targeted by debug exceptions. */
2242 static inline int arm_debug_target_el(CPUARMState *env)
2243 {
2244     bool secure = arm_is_secure(env);
2245     bool route_to_el2 = false;
2246
2247     if (arm_feature(env, ARM_FEATURE_EL2) && !secure) {
2248         route_to_el2 = env->cp15.hcr_el2 & HCR_TGE ||
2249                        env->cp15.mdcr_el2 & (1 << 8);
2250     }
2251
2252     if (route_to_el2) {
2253         return 2;
2254     } else if (arm_feature(env, ARM_FEATURE_EL3) &&
2255                !arm_el_is_aa64(env, 3) && secure) {
2256         return 3;
2257     } else {
2258         return 1;
2259     }
2260 }
2261
2262 static inline bool aa64_generate_debug_exceptions(CPUARMState *env)
2263 {
2264     if (arm_is_secure(env)) {
2265         /* MDCR_EL3.SDD disables debug events from Secure state */
2266         if (extract32(env->cp15.mdcr_el3, 16, 1) != 0
2267             || arm_current_el(env) == 3) {
2268             return false;
2269         }
2270     }
2271
2272     if (arm_current_el(env) == arm_debug_target_el(env)) {
2273         if ((extract32(env->cp15.mdscr_el1, 13, 1) == 0)
2274             || (env->daif & PSTATE_D)) {
2275             return false;
2276         }
2277     }
2278     return true;
2279 }
2280
2281 static inline bool aa32_generate_debug_exceptions(CPUARMState *env)
2282 {
2283     int el = arm_current_el(env);
2284
2285     if (el == 0 && arm_el_is_aa64(env, 1)) {
2286         return aa64_generate_debug_exceptions(env);
2287     }
2288
2289     if (arm_is_secure(env)) {
2290         int spd;
2291
2292         if (el == 0 && (env->cp15.sder & 1)) {
2293             /* SDER.SUIDEN means debug exceptions from Secure EL0
2294              * are always enabled. Otherwise they are controlled by
2295              * SDCR.SPD like those from other Secure ELs.
2296              */
2297             return true;
2298         }
2299
2300         spd = extract32(env->cp15.mdcr_el3, 14, 2);
2301         switch (spd) {
2302         case 1:
2303             /* SPD == 0b01 is reserved, but behaves as 0b00. */
2304         case 0:
2305             /* For 0b00 we return true if external secure invasive debug
2306              * is enabled. On real hardware this is controlled by external
2307              * signals to the core. QEMU always permits debug, and behaves
2308              * as if DBGEN, SPIDEN, NIDEN and SPNIDEN are all tied high.
2309              */
2310             return true;
2311         case 2:
2312             return false;
2313         case 3:
2314             return true;
2315         }
2316     }
2317
2318     return el != 2;
2319 }
2320
2321 /* Return true if debugging exceptions are currently enabled.
2322  * This corresponds to what in ARM ARM pseudocode would be
2323  *    if UsingAArch32() then
2324  *        return AArch32.GenerateDebugExceptions()
2325  *    else
2326  *        return AArch64.GenerateDebugExceptions()
2327  * We choose to push the if() down into this function for clarity,
2328  * since the pseudocode has it at all callsites except for the one in
2329  * CheckSoftwareStep(), where it is elided because both branches would
2330  * always return the same value.
2331  *
2332  * Parts of the pseudocode relating to EL2 and EL3 are omitted because we
2333  * don't yet implement those exception levels or their associated trap bits.
2334  */
2335 static inline bool arm_generate_debug_exceptions(CPUARMState *env)
2336 {
2337     if (env->aarch64) {
2338         return aa64_generate_debug_exceptions(env);
2339     } else {
2340         return aa32_generate_debug_exceptions(env);
2341     }
2342 }
2343
2344 /* Is single-stepping active? (Note that the "is EL_D AArch64?" check
2345  * implicitly means this always returns false in pre-v8 CPUs.)
2346  */
2347 static inline bool arm_singlestep_active(CPUARMState *env)
2348 {
2349     return extract32(env->cp15.mdscr_el1, 0, 1)
2350         && arm_el_is_aa64(env, arm_debug_target_el(env))
2351         && arm_generate_debug_exceptions(env);
2352 }
2353
2354 static inline bool arm_sctlr_b(CPUARMState *env)
2355 {
2356     return
2357         /* We need not implement SCTLR.ITD in user-mode emulation, so
2358          * let linux-user ignore the fact that it conflicts with SCTLR_B.
2359          * This lets people run BE32 binaries with "-cpu any".
2360          */
2361 #ifndef CONFIG_USER_ONLY
2362         !arm_feature(env, ARM_FEATURE_V7) &&
2363 #endif
2364         (env->cp15.sctlr_el[1] & SCTLR_B) != 0;
2365 }
2366
2367 /* Return true if the processor is in big-endian mode. */
2368 static inline bool arm_cpu_data_is_big_endian(CPUARMState *env)
2369 {
2370     int cur_el;
2371
2372     /* In 32bit endianness is determined by looking at CPSR's E bit */
2373     if (!is_a64(env)) {
2374         return
2375 #ifdef CONFIG_USER_ONLY
2376             /* In system mode, BE32 is modelled in line with the
2377              * architecture (as word-invariant big-endianness), where loads
2378              * and stores are done little endian but from addresses which
2379              * are adjusted by XORing with the appropriate constant. So the
2380              * endianness to use for the raw data access is not affected by
2381              * SCTLR.B.
2382              * In user mode, however, we model BE32 as byte-invariant
2383              * big-endianness (because user-only code cannot tell the
2384              * difference), and so we need to use a data access endianness
2385              * that depends on SCTLR.B.
2386              */
2387             arm_sctlr_b(env) ||
2388 #endif
2389                 ((env->uncached_cpsr & CPSR_E) ? 1 : 0);
2390     }
2391
2392     cur_el = arm_current_el(env);
2393
2394     if (cur_el == 0) {
2395         return (env->cp15.sctlr_el[1] & SCTLR_E0E) != 0;
2396     }
2397
2398     return (env->cp15.sctlr_el[cur_el] & SCTLR_EE) != 0;
2399 }
2400
2401 #include "exec/cpu-all.h"
2402
2403 /* Bit usage in the TB flags field: bit 31 indicates whether we are
2404  * in 32 or 64 bit mode. The meaning of the other bits depends on that.
2405  * We put flags which are shared between 32 and 64 bit mode at the top
2406  * of the word, and flags which apply to only one mode at the bottom.
2407  */
2408 #define ARM_TBFLAG_AARCH64_STATE_SHIFT 31
2409 #define ARM_TBFLAG_AARCH64_STATE_MASK  (1U << ARM_TBFLAG_AARCH64_STATE_SHIFT)
2410 #define ARM_TBFLAG_MMUIDX_SHIFT 28
2411 #define ARM_TBFLAG_MMUIDX_MASK (0x7 << ARM_TBFLAG_MMUIDX_SHIFT)
2412 #define ARM_TBFLAG_SS_ACTIVE_SHIFT 27
2413 #define ARM_TBFLAG_SS_ACTIVE_MASK (1 << ARM_TBFLAG_SS_ACTIVE_SHIFT)
2414 #define ARM_TBFLAG_PSTATE_SS_SHIFT 26
2415 #define ARM_TBFLAG_PSTATE_SS_MASK (1 << ARM_TBFLAG_PSTATE_SS_SHIFT)
2416 /* Target EL if we take a floating-point-disabled exception */
2417 #define ARM_TBFLAG_FPEXC_EL_SHIFT 24
2418 #define ARM_TBFLAG_FPEXC_EL_MASK (0x3 << ARM_TBFLAG_FPEXC_EL_SHIFT)
2419
2420 /* Bit usage when in AArch32 state: */
2421 #define ARM_TBFLAG_THUMB_SHIFT      0
2422 #define ARM_TBFLAG_THUMB_MASK       (1 << ARM_TBFLAG_THUMB_SHIFT)
2423 #define ARM_TBFLAG_VECLEN_SHIFT     1
2424 #define ARM_TBFLAG_VECLEN_MASK      (0x7 << ARM_TBFLAG_VECLEN_SHIFT)
2425 #define ARM_TBFLAG_VECSTRIDE_SHIFT  4
2426 #define ARM_TBFLAG_VECSTRIDE_MASK   (0x3 << ARM_TBFLAG_VECSTRIDE_SHIFT)
2427 #define ARM_TBFLAG_VFPEN_SHIFT      7
2428 #define ARM_TBFLAG_VFPEN_MASK       (1 << ARM_TBFLAG_VFPEN_SHIFT)
2429 #define ARM_TBFLAG_CONDEXEC_SHIFT   8
2430 #define ARM_TBFLAG_CONDEXEC_MASK    (0xff << ARM_TBFLAG_CONDEXEC_SHIFT)
2431 #define ARM_TBFLAG_SCTLR_B_SHIFT    16
2432 #define ARM_TBFLAG_SCTLR_B_MASK     (1 << ARM_TBFLAG_SCTLR_B_SHIFT)
2433 /* We store the bottom two bits of the CPAR as TB flags and handle
2434  * checks on the other bits at runtime
2435  */
2436 #define ARM_TBFLAG_XSCALE_CPAR_SHIFT 17
2437 #define ARM_TBFLAG_XSCALE_CPAR_MASK (3 << ARM_TBFLAG_XSCALE_CPAR_SHIFT)
2438 /* Indicates whether cp register reads and writes by guest code should access
2439  * the secure or nonsecure bank of banked registers; note that this is not
2440  * the same thing as the current security state of the processor!
2441  */
2442 #define ARM_TBFLAG_NS_SHIFT         19
2443 #define ARM_TBFLAG_NS_MASK          (1 << ARM_TBFLAG_NS_SHIFT)
2444 #define ARM_TBFLAG_BE_DATA_SHIFT    20
2445 #define ARM_TBFLAG_BE_DATA_MASK     (1 << ARM_TBFLAG_BE_DATA_SHIFT)
2446 /* For M profile only, Handler (ie not Thread) mode */
2447 #define ARM_TBFLAG_HANDLER_SHIFT    21
2448 #define ARM_TBFLAG_HANDLER_MASK     (1 << ARM_TBFLAG_HANDLER_SHIFT)
2449
2450 /* Bit usage when in AArch64 state */
2451 #define ARM_TBFLAG_TBI0_SHIFT 0        /* TBI0 for EL0/1 or TBI for EL2/3 */
2452 #define ARM_TBFLAG_TBI0_MASK (0x1ull << ARM_TBFLAG_TBI0_SHIFT)
2453 #define ARM_TBFLAG_TBI1_SHIFT 1        /* TBI1 for EL0/1  */
2454 #define ARM_TBFLAG_TBI1_MASK (0x1ull << ARM_TBFLAG_TBI1_SHIFT)
2455
2456 /* some convenience accessor macros */
2457 #define ARM_TBFLAG_AARCH64_STATE(F) \
2458     (((F) & ARM_TBFLAG_AARCH64_STATE_MASK) >> ARM_TBFLAG_AARCH64_STATE_SHIFT)
2459 #define ARM_TBFLAG_MMUIDX(F) \
2460     (((F) & ARM_TBFLAG_MMUIDX_MASK) >> ARM_TBFLAG_MMUIDX_SHIFT)
2461 #define ARM_TBFLAG_SS_ACTIVE(F) \
2462     (((F) & ARM_TBFLAG_SS_ACTIVE_MASK) >> ARM_TBFLAG_SS_ACTIVE_SHIFT)
2463 #define ARM_TBFLAG_PSTATE_SS(F) \
2464     (((F) & ARM_TBFLAG_PSTATE_SS_MASK) >> ARM_TBFLAG_PSTATE_SS_SHIFT)
2465 #define ARM_TBFLAG_FPEXC_EL(F) \
2466     (((F) & ARM_TBFLAG_FPEXC_EL_MASK) >> ARM_TBFLAG_FPEXC_EL_SHIFT)
2467 #define ARM_TBFLAG_THUMB(F) \
2468     (((F) & ARM_TBFLAG_THUMB_MASK) >> ARM_TBFLAG_THUMB_SHIFT)
2469 #define ARM_TBFLAG_VECLEN(F) \
2470     (((F) & ARM_TBFLAG_VECLEN_MASK) >> ARM_TBFLAG_VECLEN_SHIFT)
2471 #define ARM_TBFLAG_VECSTRIDE(F) \
2472     (((F) & ARM_TBFLAG_VECSTRIDE_MASK) >> ARM_TBFLAG_VECSTRIDE_SHIFT)
2473 #define ARM_TBFLAG_VFPEN(F) \
2474     (((F) & ARM_TBFLAG_VFPEN_MASK) >> ARM_TBFLAG_VFPEN_SHIFT)
2475 #define ARM_TBFLAG_CONDEXEC(F) \
2476     (((F) & ARM_TBFLAG_CONDEXEC_MASK) >> ARM_TBFLAG_CONDEXEC_SHIFT)
2477 #define ARM_TBFLAG_SCTLR_B(F) \
2478     (((F) & ARM_TBFLAG_SCTLR_B_MASK) >> ARM_TBFLAG_SCTLR_B_SHIFT)
2479 #define ARM_TBFLAG_XSCALE_CPAR(F) \
2480     (((F) & ARM_TBFLAG_XSCALE_CPAR_MASK) >> ARM_TBFLAG_XSCALE_CPAR_SHIFT)
2481 #define ARM_TBFLAG_NS(F) \
2482     (((F) & ARM_TBFLAG_NS_MASK) >> ARM_TBFLAG_NS_SHIFT)
2483 #define ARM_TBFLAG_BE_DATA(F) \
2484     (((F) & ARM_TBFLAG_BE_DATA_MASK) >> ARM_TBFLAG_BE_DATA_SHIFT)
2485 #define ARM_TBFLAG_HANDLER(F) \
2486     (((F) & ARM_TBFLAG_HANDLER_MASK) >> ARM_TBFLAG_HANDLER_SHIFT)
2487 #define ARM_TBFLAG_TBI0(F) \
2488     (((F) & ARM_TBFLAG_TBI0_MASK) >> ARM_TBFLAG_TBI0_SHIFT)
2489 #define ARM_TBFLAG_TBI1(F) \
2490     (((F) & ARM_TBFLAG_TBI1_MASK) >> ARM_TBFLAG_TBI1_SHIFT)
2491
2492 static inline bool bswap_code(bool sctlr_b)
2493 {
2494 #ifdef CONFIG_USER_ONLY
2495     /* BE8 (SCTLR.B = 0, TARGET_WORDS_BIGENDIAN = 1) is mixed endian.
2496      * The invalid combination SCTLR.B=1/CPSR.E=1/TARGET_WORDS_BIGENDIAN=0
2497      * would also end up as a mixed-endian mode with BE code, LE data.
2498      */
2499     return
2500 #ifdef TARGET_WORDS_BIGENDIAN
2501         1 ^
2502 #endif
2503         sctlr_b;
2504 #else
2505     /* All code access in ARM is little endian, and there are no loaders
2506      * doing swaps that need to be reversed
2507      */
2508     return 0;
2509 #endif
2510 }
2511
2512 /* Return the exception level to which FP-disabled exceptions should
2513  * be taken, or 0 if FP is enabled.
2514  */
2515 static inline int fp_exception_el(CPUARMState *env)
2516 {
2517     int fpen;
2518     int cur_el = arm_current_el(env);
2519
2520     /* CPACR and the CPTR registers don't exist before v6, so FP is
2521      * always accessible
2522      */
2523     if (!arm_feature(env, ARM_FEATURE_V6)) {
2524         return 0;
2525     }
2526
2527     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
2528      * 0, 2 : trap EL0 and EL1/PL1 accesses
2529      * 1    : trap only EL0 accesses
2530      * 3    : trap no accesses
2531      */
2532     fpen = extract32(env->cp15.cpacr_el1, 20, 2);
2533     switch (fpen) {
2534     case 0:
2535     case 2:
2536         if (cur_el == 0 || cur_el == 1) {
2537             /* Trap to PL1, which might be EL1 or EL3 */
2538             if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
2539                 return 3;
2540             }
2541             return 1;
2542         }
2543         if (cur_el == 3 && !is_a64(env)) {
2544             /* Secure PL1 running at EL3 */
2545             return 3;
2546         }
2547         break;
2548     case 1:
2549         if (cur_el == 0) {
2550             return 1;
2551         }
2552         break;
2553     case 3:
2554         break;
2555     }
2556
2557     /* For the CPTR registers we don't need to guard with an ARM_FEATURE
2558      * check because zero bits in the registers mean "don't trap".
2559      */
2560
2561     /* CPTR_EL2 : present in v7VE or v8 */
2562     if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
2563         && !arm_is_secure_below_el3(env)) {
2564         /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
2565         return 2;
2566     }
2567
2568     /* CPTR_EL3 : present in v8 */
2569     if (extract32(env->cp15.cptr_el[3], 10, 1)) {
2570         /* Trap all FP ops to EL3 */
2571         return 3;
2572     }
2573
2574     return 0;
2575 }
2576
2577 #ifdef CONFIG_USER_ONLY
2578 static inline bool arm_cpu_bswap_data(CPUARMState *env)
2579 {
2580     return
2581 #ifdef TARGET_WORDS_BIGENDIAN
2582        1 ^
2583 #endif
2584        arm_cpu_data_is_big_endian(env);
2585 }
2586 #endif
2587
2588 #ifndef CONFIG_USER_ONLY
2589 /**
2590  * arm_regime_tbi0:
2591  * @env: CPUARMState
2592  * @mmu_idx: MMU index indicating required translation regime
2593  *
2594  * Extracts the TBI0 value from the appropriate TCR for the current EL
2595  *
2596  * Returns: the TBI0 value.
2597  */
2598 uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx);
2599
2600 /**
2601  * arm_regime_tbi1:
2602  * @env: CPUARMState
2603  * @mmu_idx: MMU index indicating required translation regime
2604  *
2605  * Extracts the TBI1 value from the appropriate TCR for the current EL
2606  *
2607  * Returns: the TBI1 value.
2608  */
2609 uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx);
2610 #else
2611 /* We can't handle tagged addresses properly in user-only mode */
2612 static inline uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
2613 {
2614     return 0;
2615 }
2616
2617 static inline uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
2618 {
2619     return 0;
2620 }
2621 #endif
2622
2623 static inline void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
2624                                         target_ulong *cs_base, uint32_t *flags)
2625 {
2626     ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
2627     if (is_a64(env)) {
2628         *pc = env->pc;
2629         *flags = ARM_TBFLAG_AARCH64_STATE_MASK;
2630         /* Get control bits for tagged addresses */
2631         *flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT);
2632         *flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT);
2633     } else {
2634         *pc = env->regs[15];
2635         *flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
2636             | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
2637             | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
2638             | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
2639             | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT);
2640         if (!(access_secure_reg(env))) {
2641             *flags |= ARM_TBFLAG_NS_MASK;
2642         }
2643         if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
2644             || arm_el_is_aa64(env, 1)) {
2645             *flags |= ARM_TBFLAG_VFPEN_MASK;
2646         }
2647         *flags |= (extract32(env->cp15.c15_cpar, 0, 2)
2648                    << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
2649     }
2650
2651     *flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT);
2652
2653     /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
2654      * states defined in the ARM ARM for software singlestep:
2655      *  SS_ACTIVE   PSTATE.SS   State
2656      *     0            x       Inactive (the TB flag for SS is always 0)
2657      *     1            0       Active-pending
2658      *     1            1       Active-not-pending
2659      */
2660     if (arm_singlestep_active(env)) {
2661         *flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
2662         if (is_a64(env)) {
2663             if (env->pstate & PSTATE_SS) {
2664                 *flags |= ARM_TBFLAG_PSTATE_SS_MASK;
2665             }
2666         } else {
2667             if (env->uncached_cpsr & PSTATE_SS) {
2668                 *flags |= ARM_TBFLAG_PSTATE_SS_MASK;
2669             }
2670         }
2671     }
2672     if (arm_cpu_data_is_big_endian(env)) {
2673         *flags |= ARM_TBFLAG_BE_DATA_MASK;
2674     }
2675     *flags |= fp_exception_el(env) << ARM_TBFLAG_FPEXC_EL_SHIFT;
2676
2677     if (arm_v7m_is_handler_mode(env)) {
2678         *flags |= ARM_TBFLAG_HANDLER_MASK;
2679     }
2680
2681     *cs_base = 0;
2682 }
2683
2684 enum {
2685     QEMU_PSCI_CONDUIT_DISABLED = 0,
2686     QEMU_PSCI_CONDUIT_SMC = 1,
2687     QEMU_PSCI_CONDUIT_HVC = 2,
2688 };
2689
2690 #ifndef CONFIG_USER_ONLY
2691 /* Return the address space index to use for a memory access */
2692 static inline int arm_asidx_from_attrs(CPUState *cs, MemTxAttrs attrs)
2693 {
2694     return attrs.secure ? ARMASIdx_S : ARMASIdx_NS;
2695 }
2696
2697 /* Return the AddressSpace to use for a memory access
2698  * (which depends on whether the access is S or NS, and whether
2699  * the board gave us a separate AddressSpace for S accesses).
2700  */
2701 static inline AddressSpace *arm_addressspace(CPUState *cs, MemTxAttrs attrs)
2702 {
2703     return cpu_get_address_space(cs, arm_asidx_from_attrs(cs, attrs));
2704 }
2705 #endif
2706
2707 /**
2708  * arm_register_el_change_hook:
2709  * Register a hook function which will be called back whenever this
2710  * CPU changes exception level or mode. The hook function will be
2711  * passed a pointer to the ARMCPU and the opaque data pointer passed
2712  * to this function when the hook was registered.
2713  *
2714  * Note that we currently only support registering a single hook function,
2715  * and will assert if this function is called twice.
2716  * This facility is intended for the use of the GICv3 emulation.
2717  */
2718 void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHook *hook,
2719                                  void *opaque);
2720
2721 /**
2722  * arm_get_el_change_hook_opaque:
2723  * Return the opaque data that will be used by the el_change_hook
2724  * for this CPU.
2725  */
2726 static inline void *arm_get_el_change_hook_opaque(ARMCPU *cpu)
2727 {
2728     return cpu->el_change_hook_opaque;
2729 }
2730
2731 #endif
This page took 0.169643 seconds and 4 git commands to generate.