]> Git Repo - qemu.git/blob - target/arm/helper.c
target/arm: Add TBFLAG_M32.SECURE
[qemu.git] / target / arm / helper.c
1 /*
2  * ARM generic helpers.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include "qemu/osdep.h"
10 #include "qemu/units.h"
11 #include "qemu/log.h"
12 #include "trace.h"
13 #include "cpu.h"
14 #include "internals.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/crc32c.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/exec-all.h"
23 #include <zlib.h> /* For crc32 */
24 #include "hw/irq.h"
25 #include "semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/cpu-timers.h"
28 #include "sysemu/kvm.h"
29 #include "qemu/range.h"
30 #include "qapi/qapi-commands-machine-target.h"
31 #include "qapi/error.h"
32 #include "qemu/guest-random.h"
33 #ifdef CONFIG_TCG
34 #include "arm_ldst.h"
35 #include "exec/cpu_ldst.h"
36 #include "semihosting/common-semi.h"
37 #endif
38 #include "cpregs.h"
39
40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
41
42 static void switch_mode(CPUARMState *env, int mode);
43
44 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
45 {
46     assert(ri->fieldoffset);
47     if (cpreg_field_is_64bit(ri)) {
48         return CPREG_FIELD64(env, ri);
49     } else {
50         return CPREG_FIELD32(env, ri);
51     }
52 }
53
54 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
55 {
56     assert(ri->fieldoffset);
57     if (cpreg_field_is_64bit(ri)) {
58         CPREG_FIELD64(env, ri) = value;
59     } else {
60         CPREG_FIELD32(env, ri) = value;
61     }
62 }
63
64 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
65 {
66     return (char *)env + ri->fieldoffset;
67 }
68
69 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
70 {
71     /* Raw read of a coprocessor register (as needed for migration, etc). */
72     if (ri->type & ARM_CP_CONST) {
73         return ri->resetvalue;
74     } else if (ri->raw_readfn) {
75         return ri->raw_readfn(env, ri);
76     } else if (ri->readfn) {
77         return ri->readfn(env, ri);
78     } else {
79         return raw_read(env, ri);
80     }
81 }
82
83 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
84                              uint64_t v)
85 {
86     /* Raw write of a coprocessor register (as needed for migration, etc).
87      * Note that constant registers are treated as write-ignored; the
88      * caller should check for success by whether a readback gives the
89      * value written.
90      */
91     if (ri->type & ARM_CP_CONST) {
92         return;
93     } else if (ri->raw_writefn) {
94         ri->raw_writefn(env, ri, v);
95     } else if (ri->writefn) {
96         ri->writefn(env, ri, v);
97     } else {
98         raw_write(env, ri, v);
99     }
100 }
101
102 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
103 {
104    /* Return true if the regdef would cause an assertion if you called
105     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
106     * program bug for it not to have the NO_RAW flag).
107     * NB that returning false here doesn't necessarily mean that calling
108     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
109     * read/write access functions which are safe for raw use" from "has
110     * read/write access functions which have side effects but has forgotten
111     * to provide raw access functions".
112     * The tests here line up with the conditions in read/write_raw_cp_reg()
113     * and assertions in raw_read()/raw_write().
114     */
115     if ((ri->type & ARM_CP_CONST) ||
116         ri->fieldoffset ||
117         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
118         return false;
119     }
120     return true;
121 }
122
123 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
124 {
125     /* Write the coprocessor state from cpu->env to the (index,value) list. */
126     int i;
127     bool ok = true;
128
129     for (i = 0; i < cpu->cpreg_array_len; i++) {
130         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
131         const ARMCPRegInfo *ri;
132         uint64_t newval;
133
134         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
135         if (!ri) {
136             ok = false;
137             continue;
138         }
139         if (ri->type & ARM_CP_NO_RAW) {
140             continue;
141         }
142
143         newval = read_raw_cp_reg(&cpu->env, ri);
144         if (kvm_sync) {
145             /*
146              * Only sync if the previous list->cpustate sync succeeded.
147              * Rather than tracking the success/failure state for every
148              * item in the list, we just recheck "does the raw write we must
149              * have made in write_list_to_cpustate() read back OK" here.
150              */
151             uint64_t oldval = cpu->cpreg_values[i];
152
153             if (oldval == newval) {
154                 continue;
155             }
156
157             write_raw_cp_reg(&cpu->env, ri, oldval);
158             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
159                 continue;
160             }
161
162             write_raw_cp_reg(&cpu->env, ri, newval);
163         }
164         cpu->cpreg_values[i] = newval;
165     }
166     return ok;
167 }
168
169 bool write_list_to_cpustate(ARMCPU *cpu)
170 {
171     int i;
172     bool ok = true;
173
174     for (i = 0; i < cpu->cpreg_array_len; i++) {
175         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
176         uint64_t v = cpu->cpreg_values[i];
177         const ARMCPRegInfo *ri;
178
179         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
180         if (!ri) {
181             ok = false;
182             continue;
183         }
184         if (ri->type & ARM_CP_NO_RAW) {
185             continue;
186         }
187         /* Write value and confirm it reads back as written
188          * (to catch read-only registers and partially read-only
189          * registers where the incoming migration value doesn't match)
190          */
191         write_raw_cp_reg(&cpu->env, ri, v);
192         if (read_raw_cp_reg(&cpu->env, ri) != v) {
193             ok = false;
194         }
195     }
196     return ok;
197 }
198
199 static void add_cpreg_to_list(gpointer key, gpointer opaque)
200 {
201     ARMCPU *cpu = opaque;
202     uint32_t regidx = (uintptr_t)key;
203     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
204
205     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
206         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207         /* The value array need not be initialized at this point */
208         cpu->cpreg_array_len++;
209     }
210 }
211
212 static void count_cpreg(gpointer key, gpointer opaque)
213 {
214     ARMCPU *cpu = opaque;
215     const ARMCPRegInfo *ri;
216
217     ri = g_hash_table_lookup(cpu->cp_regs, key);
218
219     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
220         cpu->cpreg_array_len++;
221     }
222 }
223
224 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
225 {
226     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
227     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
228
229     if (aidx > bidx) {
230         return 1;
231     }
232     if (aidx < bidx) {
233         return -1;
234     }
235     return 0;
236 }
237
238 void init_cpreg_list(ARMCPU *cpu)
239 {
240     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
241      * Note that we require cpreg_tuples[] to be sorted by key ID.
242      */
243     GList *keys;
244     int arraylen;
245
246     keys = g_hash_table_get_keys(cpu->cp_regs);
247     keys = g_list_sort(keys, cpreg_key_compare);
248
249     cpu->cpreg_array_len = 0;
250
251     g_list_foreach(keys, count_cpreg, cpu);
252
253     arraylen = cpu->cpreg_array_len;
254     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
255     cpu->cpreg_values = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
258     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
259     cpu->cpreg_array_len = 0;
260
261     g_list_foreach(keys, add_cpreg_to_list, cpu);
262
263     assert(cpu->cpreg_array_len == arraylen);
264
265     g_list_free(keys);
266 }
267
268 /*
269  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
270  */
271 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
272                                         const ARMCPRegInfo *ri,
273                                         bool isread)
274 {
275     if (!is_a64(env) && arm_current_el(env) == 3 &&
276         arm_is_secure_below_el3(env)) {
277         return CP_ACCESS_TRAP_UNCATEGORIZED;
278     }
279     return CP_ACCESS_OK;
280 }
281
282 /* Some secure-only AArch32 registers trap to EL3 if used from
283  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
284  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
285  * We assume that the .access field is set to PL1_RW.
286  */
287 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
288                                             const ARMCPRegInfo *ri,
289                                             bool isread)
290 {
291     if (arm_current_el(env) == 3) {
292         return CP_ACCESS_OK;
293     }
294     if (arm_is_secure_below_el3(env)) {
295         if (env->cp15.scr_el3 & SCR_EEL2) {
296             return CP_ACCESS_TRAP_EL2;
297         }
298         return CP_ACCESS_TRAP_EL3;
299     }
300     /* This will be EL1 NS and EL2 NS, which just UNDEF */
301     return CP_ACCESS_TRAP_UNCATEGORIZED;
302 }
303
304 /* Check for traps to performance monitor registers, which are controlled
305  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
306  */
307 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
308                                  bool isread)
309 {
310     int el = arm_current_el(env);
311     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
312
313     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
314         return CP_ACCESS_TRAP_EL2;
315     }
316     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
317         return CP_ACCESS_TRAP_EL3;
318     }
319     return CP_ACCESS_OK;
320 }
321
322 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
323 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
324                                       bool isread)
325 {
326     if (arm_current_el(env) == 1) {
327         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
328         if (arm_hcr_el2_eff(env) & trap) {
329             return CP_ACCESS_TRAP_EL2;
330         }
331     }
332     return CP_ACCESS_OK;
333 }
334
335 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
336 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
337                                  bool isread)
338 {
339     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
340         return CP_ACCESS_TRAP_EL2;
341     }
342     return CP_ACCESS_OK;
343 }
344
345 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
346 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
347                                   bool isread)
348 {
349     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
350         return CP_ACCESS_TRAP_EL2;
351     }
352     return CP_ACCESS_OK;
353 }
354
355 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
356 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
357                                   bool isread)
358 {
359     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
360         return CP_ACCESS_TRAP_EL2;
361     }
362     return CP_ACCESS_OK;
363 }
364
365 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
366 {
367     ARMCPU *cpu = env_archcpu(env);
368
369     raw_write(env, ri, value);
370     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
371 }
372
373 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
374 {
375     ARMCPU *cpu = env_archcpu(env);
376
377     if (raw_read(env, ri) != value) {
378         /* Unlike real hardware the qemu TLB uses virtual addresses,
379          * not modified virtual addresses, so this causes a TLB flush.
380          */
381         tlb_flush(CPU(cpu));
382         raw_write(env, ri, value);
383     }
384 }
385
386 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
387                              uint64_t value)
388 {
389     ARMCPU *cpu = env_archcpu(env);
390
391     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
392         && !extended_addresses_enabled(env)) {
393         /* For VMSA (when not using the LPAE long descriptor page table
394          * format) this register includes the ASID, so do a TLB flush.
395          * For PMSA it is purely a process ID and no action is needed.
396          */
397         tlb_flush(CPU(cpu));
398     }
399     raw_write(env, ri, value);
400 }
401
402 /* IS variants of TLB operations must affect all cores */
403 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
404                              uint64_t value)
405 {
406     CPUState *cs = env_cpu(env);
407
408     tlb_flush_all_cpus_synced(cs);
409 }
410
411 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
412                              uint64_t value)
413 {
414     CPUState *cs = env_cpu(env);
415
416     tlb_flush_all_cpus_synced(cs);
417 }
418
419 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
420                              uint64_t value)
421 {
422     CPUState *cs = env_cpu(env);
423
424     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
425 }
426
427 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
428                              uint64_t value)
429 {
430     CPUState *cs = env_cpu(env);
431
432     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
433 }
434
435 /*
436  * Non-IS variants of TLB operations are upgraded to
437  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
438  * force broadcast of these operations.
439  */
440 static bool tlb_force_broadcast(CPUARMState *env)
441 {
442     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
443 }
444
445 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
446                           uint64_t value)
447 {
448     /* Invalidate all (TLBIALL) */
449     CPUState *cs = env_cpu(env);
450
451     if (tlb_force_broadcast(env)) {
452         tlb_flush_all_cpus_synced(cs);
453     } else {
454         tlb_flush(cs);
455     }
456 }
457
458 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
459                           uint64_t value)
460 {
461     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
462     CPUState *cs = env_cpu(env);
463
464     value &= TARGET_PAGE_MASK;
465     if (tlb_force_broadcast(env)) {
466         tlb_flush_page_all_cpus_synced(cs, value);
467     } else {
468         tlb_flush_page(cs, value);
469     }
470 }
471
472 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
473                            uint64_t value)
474 {
475     /* Invalidate by ASID (TLBIASID) */
476     CPUState *cs = env_cpu(env);
477
478     if (tlb_force_broadcast(env)) {
479         tlb_flush_all_cpus_synced(cs);
480     } else {
481         tlb_flush(cs);
482     }
483 }
484
485 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
486                            uint64_t value)
487 {
488     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
489     CPUState *cs = env_cpu(env);
490
491     value &= TARGET_PAGE_MASK;
492     if (tlb_force_broadcast(env)) {
493         tlb_flush_page_all_cpus_synced(cs, value);
494     } else {
495         tlb_flush_page(cs, value);
496     }
497 }
498
499 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
500                                uint64_t value)
501 {
502     CPUState *cs = env_cpu(env);
503
504     tlb_flush_by_mmuidx(cs,
505                         ARMMMUIdxBit_E10_1 |
506                         ARMMMUIdxBit_E10_1_PAN |
507                         ARMMMUIdxBit_E10_0);
508 }
509
510 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
511                                   uint64_t value)
512 {
513     CPUState *cs = env_cpu(env);
514
515     tlb_flush_by_mmuidx_all_cpus_synced(cs,
516                                         ARMMMUIdxBit_E10_1 |
517                                         ARMMMUIdxBit_E10_1_PAN |
518                                         ARMMMUIdxBit_E10_0);
519 }
520
521
522 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
523                               uint64_t value)
524 {
525     CPUState *cs = env_cpu(env);
526
527     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
528 }
529
530 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
531                                  uint64_t value)
532 {
533     CPUState *cs = env_cpu(env);
534
535     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
536 }
537
538 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
539                               uint64_t value)
540 {
541     CPUState *cs = env_cpu(env);
542     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
543
544     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
545 }
546
547 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548                                  uint64_t value)
549 {
550     CPUState *cs = env_cpu(env);
551     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
552
553     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
554                                              ARMMMUIdxBit_E2);
555 }
556
557 static const ARMCPRegInfo cp_reginfo[] = {
558     /* Define the secure and non-secure FCSE identifier CP registers
559      * separately because there is no secure bank in V8 (no _EL3).  This allows
560      * the secure register to be properly reset and migrated. There is also no
561      * v8 EL1 version of the register so the non-secure instance stands alone.
562      */
563     { .name = "FCSEIDR",
564       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
565       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
566       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
567       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
568     { .name = "FCSEIDR_S",
569       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
570       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
571       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
572       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
573     /* Define the secure and non-secure context identifier CP registers
574      * separately because there is no secure bank in V8 (no _EL3).  This allows
575      * the secure register to be properly reset and migrated.  In the
576      * non-secure case, the 32-bit register will have reset and migration
577      * disabled during registration as it is handled by the 64-bit instance.
578      */
579     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
580       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
581       .access = PL1_RW, .accessfn = access_tvm_trvm,
582       .secure = ARM_CP_SECSTATE_NS,
583       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
584       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
585     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
586       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
587       .access = PL1_RW, .accessfn = access_tvm_trvm,
588       .secure = ARM_CP_SECSTATE_S,
589       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
590       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
591 };
592
593 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
594     /* NB: Some of these registers exist in v8 but with more precise
595      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
596      */
597     /* MMU Domain access control / MPU write buffer control */
598     { .name = "DACR",
599       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
600       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
601       .writefn = dacr_write, .raw_writefn = raw_write,
602       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
603                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
604     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
605      * For v6 and v5, these mappings are overly broad.
606      */
607     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
608       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
609     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
610       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
611     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
612       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
613     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
614       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
615     /* Cache maintenance ops; some of this space may be overridden later. */
616     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
617       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
618       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
619 };
620
621 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
622     /* Not all pre-v6 cores implemented this WFI, so this is slightly
623      * over-broad.
624      */
625     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
626       .access = PL1_W, .type = ARM_CP_WFI },
627 };
628
629 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
630     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
631      * is UNPREDICTABLE; we choose to NOP as most implementations do).
632      */
633     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
634       .access = PL1_W, .type = ARM_CP_WFI },
635     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
636      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
637      * OMAPCP will override this space.
638      */
639     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
640       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
641       .resetvalue = 0 },
642     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
643       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
644       .resetvalue = 0 },
645     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
646     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
647       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
648       .resetvalue = 0 },
649     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
650      * implementing it as RAZ means the "debug architecture version" bits
651      * will read as a reserved value, which should cause Linux to not try
652      * to use the debug hardware.
653      */
654     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
655       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
656     /* MMU TLB control. Note that the wildcarding means we cover not just
657      * the unified TLB ops but also the dside/iside/inner-shareable variants.
658      */
659     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
660       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
661       .type = ARM_CP_NO_RAW },
662     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
663       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
664       .type = ARM_CP_NO_RAW },
665     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
666       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
667       .type = ARM_CP_NO_RAW },
668     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
669       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
670       .type = ARM_CP_NO_RAW },
671     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
672       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
673     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
674       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
675 };
676
677 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
678                         uint64_t value)
679 {
680     uint32_t mask = 0;
681
682     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
683     if (!arm_feature(env, ARM_FEATURE_V8)) {
684         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
685          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
686          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
687          */
688         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
689             /* VFP coprocessor: cp10 & cp11 [23:20] */
690             mask |= R_CPACR_ASEDIS_MASK |
691                     R_CPACR_D32DIS_MASK |
692                     R_CPACR_CP11_MASK |
693                     R_CPACR_CP10_MASK;
694
695             if (!arm_feature(env, ARM_FEATURE_NEON)) {
696                 /* ASEDIS [31] bit is RAO/WI */
697                 value |= R_CPACR_ASEDIS_MASK;
698             }
699
700             /* VFPv3 and upwards with NEON implement 32 double precision
701              * registers (D0-D31).
702              */
703             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
704                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
705                 value |= R_CPACR_D32DIS_MASK;
706             }
707         }
708         value &= mask;
709     }
710
711     /*
712      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
713      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
714      */
715     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
716         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
717         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
718         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
719     }
720
721     env->cp15.cpacr_el1 = value;
722 }
723
724 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
725 {
726     /*
727      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
728      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
729      */
730     uint64_t value = env->cp15.cpacr_el1;
731
732     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
733         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
734         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
735     }
736     return value;
737 }
738
739
740 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
741 {
742     /* Call cpacr_write() so that we reset with the correct RAO bits set
743      * for our CPU features.
744      */
745     cpacr_write(env, ri, 0);
746 }
747
748 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
749                                    bool isread)
750 {
751     if (arm_feature(env, ARM_FEATURE_V8)) {
752         /* Check if CPACR accesses are to be trapped to EL2 */
753         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
754             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
755             return CP_ACCESS_TRAP_EL2;
756         /* Check if CPACR accesses are to be trapped to EL3 */
757         } else if (arm_current_el(env) < 3 &&
758                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
759             return CP_ACCESS_TRAP_EL3;
760         }
761     }
762
763     return CP_ACCESS_OK;
764 }
765
766 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
767                                   bool isread)
768 {
769     /* Check if CPTR accesses are set to trap to EL3 */
770     if (arm_current_el(env) == 2 &&
771         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
772         return CP_ACCESS_TRAP_EL3;
773     }
774
775     return CP_ACCESS_OK;
776 }
777
778 static const ARMCPRegInfo v6_cp_reginfo[] = {
779     /* prefetch by MVA in v6, NOP in v7 */
780     { .name = "MVA_prefetch",
781       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
782       .access = PL1_W, .type = ARM_CP_NOP },
783     /* We need to break the TB after ISB to execute self-modifying code
784      * correctly and also to take any pending interrupts immediately.
785      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
786      */
787     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
788       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
789     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
790       .access = PL0_W, .type = ARM_CP_NOP },
791     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
792       .access = PL0_W, .type = ARM_CP_NOP },
793     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
794       .access = PL1_RW, .accessfn = access_tvm_trvm,
795       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
796                              offsetof(CPUARMState, cp15.ifar_ns) },
797       .resetvalue = 0, },
798     /* Watchpoint Fault Address Register : should actually only be present
799      * for 1136, 1176, 11MPCore.
800      */
801     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
802       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
803     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
804       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
805       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
806       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
807 };
808
809 typedef struct pm_event {
810     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
811     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
812     bool (*supported)(CPUARMState *);
813     /*
814      * Retrieve the current count of the underlying event. The programmed
815      * counters hold a difference from the return value from this function
816      */
817     uint64_t (*get_count)(CPUARMState *);
818     /*
819      * Return how many nanoseconds it will take (at a minimum) for count events
820      * to occur. A negative value indicates the counter will never overflow, or
821      * that the counter has otherwise arranged for the overflow bit to be set
822      * and the PMU interrupt to be raised on overflow.
823      */
824     int64_t (*ns_per_count)(uint64_t);
825 } pm_event;
826
827 static bool event_always_supported(CPUARMState *env)
828 {
829     return true;
830 }
831
832 static uint64_t swinc_get_count(CPUARMState *env)
833 {
834     /*
835      * SW_INCR events are written directly to the pmevcntr's by writes to
836      * PMSWINC, so there is no underlying count maintained by the PMU itself
837      */
838     return 0;
839 }
840
841 static int64_t swinc_ns_per(uint64_t ignored)
842 {
843     return -1;
844 }
845
846 /*
847  * Return the underlying cycle count for the PMU cycle counters. If we're in
848  * usermode, simply return 0.
849  */
850 static uint64_t cycles_get_count(CPUARMState *env)
851 {
852 #ifndef CONFIG_USER_ONLY
853     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
854                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
855 #else
856     return cpu_get_host_ticks();
857 #endif
858 }
859
860 #ifndef CONFIG_USER_ONLY
861 static int64_t cycles_ns_per(uint64_t cycles)
862 {
863     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
864 }
865
866 static bool instructions_supported(CPUARMState *env)
867 {
868     return icount_enabled() == 1; /* Precise instruction counting */
869 }
870
871 static uint64_t instructions_get_count(CPUARMState *env)
872 {
873     return (uint64_t)icount_get_raw();
874 }
875
876 static int64_t instructions_ns_per(uint64_t icount)
877 {
878     return icount_to_ns((int64_t)icount);
879 }
880 #endif
881
882 static bool pmuv3p1_events_supported(CPUARMState *env)
883 {
884     /* For events which are supported in any v8.1 PMU */
885     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
886 }
887
888 static bool pmuv3p4_events_supported(CPUARMState *env)
889 {
890     /* For events which are supported in any v8.1 PMU */
891     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
892 }
893
894 static uint64_t zero_event_get_count(CPUARMState *env)
895 {
896     /* For events which on QEMU never fire, so their count is always zero */
897     return 0;
898 }
899
900 static int64_t zero_event_ns_per(uint64_t cycles)
901 {
902     /* An event which never fires can never overflow */
903     return -1;
904 }
905
906 static const pm_event pm_events[] = {
907     { .number = 0x000, /* SW_INCR */
908       .supported = event_always_supported,
909       .get_count = swinc_get_count,
910       .ns_per_count = swinc_ns_per,
911     },
912 #ifndef CONFIG_USER_ONLY
913     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
914       .supported = instructions_supported,
915       .get_count = instructions_get_count,
916       .ns_per_count = instructions_ns_per,
917     },
918     { .number = 0x011, /* CPU_CYCLES, Cycle */
919       .supported = event_always_supported,
920       .get_count = cycles_get_count,
921       .ns_per_count = cycles_ns_per,
922     },
923 #endif
924     { .number = 0x023, /* STALL_FRONTEND */
925       .supported = pmuv3p1_events_supported,
926       .get_count = zero_event_get_count,
927       .ns_per_count = zero_event_ns_per,
928     },
929     { .number = 0x024, /* STALL_BACKEND */
930       .supported = pmuv3p1_events_supported,
931       .get_count = zero_event_get_count,
932       .ns_per_count = zero_event_ns_per,
933     },
934     { .number = 0x03c, /* STALL */
935       .supported = pmuv3p4_events_supported,
936       .get_count = zero_event_get_count,
937       .ns_per_count = zero_event_ns_per,
938     },
939 };
940
941 /*
942  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
943  * events (i.e. the statistical profiling extension), this implementation
944  * should first be updated to something sparse instead of the current
945  * supported_event_map[] array.
946  */
947 #define MAX_EVENT_ID 0x3c
948 #define UNSUPPORTED_EVENT UINT16_MAX
949 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
950
951 /*
952  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
953  * of ARM event numbers to indices in our pm_events array.
954  *
955  * Note: Events in the 0x40XX range are not currently supported.
956  */
957 void pmu_init(ARMCPU *cpu)
958 {
959     unsigned int i;
960
961     /*
962      * Empty supported_event_map and cpu->pmceid[01] before adding supported
963      * events to them
964      */
965     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
966         supported_event_map[i] = UNSUPPORTED_EVENT;
967     }
968     cpu->pmceid0 = 0;
969     cpu->pmceid1 = 0;
970
971     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
972         const pm_event *cnt = &pm_events[i];
973         assert(cnt->number <= MAX_EVENT_ID);
974         /* We do not currently support events in the 0x40xx range */
975         assert(cnt->number <= 0x3f);
976
977         if (cnt->supported(&cpu->env)) {
978             supported_event_map[cnt->number] = i;
979             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
980             if (cnt->number & 0x20) {
981                 cpu->pmceid1 |= event_mask;
982             } else {
983                 cpu->pmceid0 |= event_mask;
984             }
985         }
986     }
987 }
988
989 /*
990  * Check at runtime whether a PMU event is supported for the current machine
991  */
992 static bool event_supported(uint16_t number)
993 {
994     if (number > MAX_EVENT_ID) {
995         return false;
996     }
997     return supported_event_map[number] != UNSUPPORTED_EVENT;
998 }
999
1000 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1001                                    bool isread)
1002 {
1003     /* Performance monitor registers user accessibility is controlled
1004      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1005      * trapping to EL2 or EL3 for other accesses.
1006      */
1007     int el = arm_current_el(env);
1008     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1009
1010     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1011         return CP_ACCESS_TRAP;
1012     }
1013     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1014         return CP_ACCESS_TRAP_EL2;
1015     }
1016     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1017         return CP_ACCESS_TRAP_EL3;
1018     }
1019
1020     return CP_ACCESS_OK;
1021 }
1022
1023 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1024                                            const ARMCPRegInfo *ri,
1025                                            bool isread)
1026 {
1027     /* ER: event counter read trap control */
1028     if (arm_feature(env, ARM_FEATURE_V8)
1029         && arm_current_el(env) == 0
1030         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1031         && isread) {
1032         return CP_ACCESS_OK;
1033     }
1034
1035     return pmreg_access(env, ri, isread);
1036 }
1037
1038 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1039                                          const ARMCPRegInfo *ri,
1040                                          bool isread)
1041 {
1042     /* SW: software increment write trap control */
1043     if (arm_feature(env, ARM_FEATURE_V8)
1044         && arm_current_el(env) == 0
1045         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1046         && !isread) {
1047         return CP_ACCESS_OK;
1048     }
1049
1050     return pmreg_access(env, ri, isread);
1051 }
1052
1053 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1054                                         const ARMCPRegInfo *ri,
1055                                         bool isread)
1056 {
1057     /* ER: event counter read trap control */
1058     if (arm_feature(env, ARM_FEATURE_V8)
1059         && arm_current_el(env) == 0
1060         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1061         return CP_ACCESS_OK;
1062     }
1063
1064     return pmreg_access(env, ri, isread);
1065 }
1066
1067 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1068                                          const ARMCPRegInfo *ri,
1069                                          bool isread)
1070 {
1071     /* CR: cycle counter read trap control */
1072     if (arm_feature(env, ARM_FEATURE_V8)
1073         && arm_current_el(env) == 0
1074         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1075         && isread) {
1076         return CP_ACCESS_OK;
1077     }
1078
1079     return pmreg_access(env, ri, isread);
1080 }
1081
1082 /*
1083  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1084  * We use these to decide whether we need to wrap a write to MDCR_EL2
1085  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1086  */
1087 #define MDCR_EL2_PMU_ENABLE_BITS \
1088     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1089 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1090
1091 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1092  * the current EL, security state, and register configuration.
1093  */
1094 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1095 {
1096     uint64_t filter;
1097     bool e, p, u, nsk, nsu, nsh, m;
1098     bool enabled, prohibited = false, filtered;
1099     bool secure = arm_is_secure(env);
1100     int el = arm_current_el(env);
1101     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1102     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1103
1104     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1105         return false;
1106     }
1107
1108     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1109             (counter < hpmn || counter == 31)) {
1110         e = env->cp15.c9_pmcr & PMCRE;
1111     } else {
1112         e = mdcr_el2 & MDCR_HPME;
1113     }
1114     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1115
1116     /* Is event counting prohibited? */
1117     if (el == 2 && (counter < hpmn || counter == 31)) {
1118         prohibited = mdcr_el2 & MDCR_HPMD;
1119     }
1120     if (secure) {
1121         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1122     }
1123
1124     if (counter == 31) {
1125         /*
1126          * The cycle counter defaults to running. PMCR.DP says "disable
1127          * the cycle counter when event counting is prohibited".
1128          * Some MDCR bits disable the cycle counter specifically.
1129          */
1130         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1131         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1132             if (secure) {
1133                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1134             }
1135             if (el == 2) {
1136                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1137             }
1138         }
1139     }
1140
1141     if (counter == 31) {
1142         filter = env->cp15.pmccfiltr_el0;
1143     } else {
1144         filter = env->cp15.c14_pmevtyper[counter];
1145     }
1146
1147     p   = filter & PMXEVTYPER_P;
1148     u   = filter & PMXEVTYPER_U;
1149     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1150     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1151     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1152     m   = arm_el_is_aa64(env, 1) &&
1153               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1154
1155     if (el == 0) {
1156         filtered = secure ? u : u != nsu;
1157     } else if (el == 1) {
1158         filtered = secure ? p : p != nsk;
1159     } else if (el == 2) {
1160         filtered = !nsh;
1161     } else { /* EL3 */
1162         filtered = m != p;
1163     }
1164
1165     if (counter != 31) {
1166         /*
1167          * If not checking PMCCNTR, ensure the counter is setup to an event we
1168          * support
1169          */
1170         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1171         if (!event_supported(event)) {
1172             return false;
1173         }
1174     }
1175
1176     return enabled && !prohibited && !filtered;
1177 }
1178
1179 static void pmu_update_irq(CPUARMState *env)
1180 {
1181     ARMCPU *cpu = env_archcpu(env);
1182     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1183             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1184 }
1185
1186 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1187 {
1188     /*
1189      * Return true if the clock divider is enabled and the cycle counter
1190      * is supposed to tick only once every 64 clock cycles. This is
1191      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1192      * (64-bit) cycle counter PMCR.D has no effect.
1193      */
1194     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1195 }
1196
1197 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1198 {
1199     /* Return true if the specified event counter is configured to be 64 bit */
1200
1201     /* This isn't intended to be used with the cycle counter */
1202     assert(counter < 31);
1203
1204     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1205         return false;
1206     }
1207
1208     if (arm_feature(env, ARM_FEATURE_EL2)) {
1209         /*
1210          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1211          * current security state, so we don't use arm_mdcr_el2_eff() here.
1212          */
1213         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1214         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1215
1216         if (hpmn != 0 && counter >= hpmn) {
1217             return hlp;
1218         }
1219     }
1220     return env->cp15.c9_pmcr & PMCRLP;
1221 }
1222
1223 /*
1224  * Ensure c15_ccnt is the guest-visible count so that operations such as
1225  * enabling/disabling the counter or filtering, modifying the count itself,
1226  * etc. can be done logically. This is essentially a no-op if the counter is
1227  * not enabled at the time of the call.
1228  */
1229 static void pmccntr_op_start(CPUARMState *env)
1230 {
1231     uint64_t cycles = cycles_get_count(env);
1232
1233     if (pmu_counter_enabled(env, 31)) {
1234         uint64_t eff_cycles = cycles;
1235         if (pmccntr_clockdiv_enabled(env)) {
1236             eff_cycles /= 64;
1237         }
1238
1239         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1240
1241         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1242                                  1ull << 63 : 1ull << 31;
1243         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1244             env->cp15.c9_pmovsr |= (1ULL << 31);
1245             pmu_update_irq(env);
1246         }
1247
1248         env->cp15.c15_ccnt = new_pmccntr;
1249     }
1250     env->cp15.c15_ccnt_delta = cycles;
1251 }
1252
1253 /*
1254  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1255  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1256  * pmccntr_op_start.
1257  */
1258 static void pmccntr_op_finish(CPUARMState *env)
1259 {
1260     if (pmu_counter_enabled(env, 31)) {
1261 #ifndef CONFIG_USER_ONLY
1262         /* Calculate when the counter will next overflow */
1263         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1264         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1265             remaining_cycles = (uint32_t)remaining_cycles;
1266         }
1267         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1268
1269         if (overflow_in > 0) {
1270             int64_t overflow_at;
1271
1272             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1273                                  overflow_in, &overflow_at)) {
1274                 ARMCPU *cpu = env_archcpu(env);
1275                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1276             }
1277         }
1278 #endif
1279
1280         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1281         if (pmccntr_clockdiv_enabled(env)) {
1282             prev_cycles /= 64;
1283         }
1284         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1285     }
1286 }
1287
1288 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1289 {
1290
1291     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1292     uint64_t count = 0;
1293     if (event_supported(event)) {
1294         uint16_t event_idx = supported_event_map[event];
1295         count = pm_events[event_idx].get_count(env);
1296     }
1297
1298     if (pmu_counter_enabled(env, counter)) {
1299         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1300         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1301             1ULL << 63 : 1ULL << 31;
1302
1303         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1304             env->cp15.c9_pmovsr |= (1 << counter);
1305             pmu_update_irq(env);
1306         }
1307         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1308     }
1309     env->cp15.c14_pmevcntr_delta[counter] = count;
1310 }
1311
1312 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1313 {
1314     if (pmu_counter_enabled(env, counter)) {
1315 #ifndef CONFIG_USER_ONLY
1316         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1317         uint16_t event_idx = supported_event_map[event];
1318         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1319         int64_t overflow_in;
1320
1321         if (!pmevcntr_is_64_bit(env, counter)) {
1322             delta = (uint32_t)delta;
1323         }
1324         overflow_in = pm_events[event_idx].ns_per_count(delta);
1325
1326         if (overflow_in > 0) {
1327             int64_t overflow_at;
1328
1329             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1330                                  overflow_in, &overflow_at)) {
1331                 ARMCPU *cpu = env_archcpu(env);
1332                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1333             }
1334         }
1335 #endif
1336
1337         env->cp15.c14_pmevcntr_delta[counter] -=
1338             env->cp15.c14_pmevcntr[counter];
1339     }
1340 }
1341
1342 void pmu_op_start(CPUARMState *env)
1343 {
1344     unsigned int i;
1345     pmccntr_op_start(env);
1346     for (i = 0; i < pmu_num_counters(env); i++) {
1347         pmevcntr_op_start(env, i);
1348     }
1349 }
1350
1351 void pmu_op_finish(CPUARMState *env)
1352 {
1353     unsigned int i;
1354     pmccntr_op_finish(env);
1355     for (i = 0; i < pmu_num_counters(env); i++) {
1356         pmevcntr_op_finish(env, i);
1357     }
1358 }
1359
1360 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1361 {
1362     pmu_op_start(&cpu->env);
1363 }
1364
1365 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1366 {
1367     pmu_op_finish(&cpu->env);
1368 }
1369
1370 void arm_pmu_timer_cb(void *opaque)
1371 {
1372     ARMCPU *cpu = opaque;
1373
1374     /*
1375      * Update all the counter values based on the current underlying counts,
1376      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1377      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1378      * counter may expire.
1379      */
1380     pmu_op_start(&cpu->env);
1381     pmu_op_finish(&cpu->env);
1382 }
1383
1384 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1385                        uint64_t value)
1386 {
1387     pmu_op_start(env);
1388
1389     if (value & PMCRC) {
1390         /* The counter has been reset */
1391         env->cp15.c15_ccnt = 0;
1392     }
1393
1394     if (value & PMCRP) {
1395         unsigned int i;
1396         for (i = 0; i < pmu_num_counters(env); i++) {
1397             env->cp15.c14_pmevcntr[i] = 0;
1398         }
1399     }
1400
1401     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1402     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1403
1404     pmu_op_finish(env);
1405 }
1406
1407 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1408                           uint64_t value)
1409 {
1410     unsigned int i;
1411     uint64_t overflow_mask, new_pmswinc;
1412
1413     for (i = 0; i < pmu_num_counters(env); i++) {
1414         /* Increment a counter's count iff: */
1415         if ((value & (1 << i)) && /* counter's bit is set */
1416                 /* counter is enabled and not filtered */
1417                 pmu_counter_enabled(env, i) &&
1418                 /* counter is SW_INCR */
1419                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1420             pmevcntr_op_start(env, i);
1421
1422             /*
1423              * Detect if this write causes an overflow since we can't predict
1424              * PMSWINC overflows like we can for other events
1425              */
1426             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1427
1428             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1429                 1ULL << 63 : 1ULL << 31;
1430
1431             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1432                 env->cp15.c9_pmovsr |= (1 << i);
1433                 pmu_update_irq(env);
1434             }
1435
1436             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1437
1438             pmevcntr_op_finish(env, i);
1439         }
1440     }
1441 }
1442
1443 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1444 {
1445     uint64_t ret;
1446     pmccntr_op_start(env);
1447     ret = env->cp15.c15_ccnt;
1448     pmccntr_op_finish(env);
1449     return ret;
1450 }
1451
1452 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453                          uint64_t value)
1454 {
1455     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1456      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1457      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1458      * accessed.
1459      */
1460     env->cp15.c9_pmselr = value & 0x1f;
1461 }
1462
1463 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1464                         uint64_t value)
1465 {
1466     pmccntr_op_start(env);
1467     env->cp15.c15_ccnt = value;
1468     pmccntr_op_finish(env);
1469 }
1470
1471 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1472                             uint64_t value)
1473 {
1474     uint64_t cur_val = pmccntr_read(env, NULL);
1475
1476     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1477 }
1478
1479 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1480                             uint64_t value)
1481 {
1482     pmccntr_op_start(env);
1483     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1484     pmccntr_op_finish(env);
1485 }
1486
1487 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1488                             uint64_t value)
1489 {
1490     pmccntr_op_start(env);
1491     /* M is not accessible from AArch32 */
1492     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1493         (value & PMCCFILTR);
1494     pmccntr_op_finish(env);
1495 }
1496
1497 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1498 {
1499     /* M is not visible in AArch32 */
1500     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1501 }
1502
1503 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504                             uint64_t value)
1505 {
1506     pmu_op_start(env);
1507     value &= pmu_counter_mask(env);
1508     env->cp15.c9_pmcnten |= value;
1509     pmu_op_finish(env);
1510 }
1511
1512 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1513                              uint64_t value)
1514 {
1515     pmu_op_start(env);
1516     value &= pmu_counter_mask(env);
1517     env->cp15.c9_pmcnten &= ~value;
1518     pmu_op_finish(env);
1519 }
1520
1521 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1522                          uint64_t value)
1523 {
1524     value &= pmu_counter_mask(env);
1525     env->cp15.c9_pmovsr &= ~value;
1526     pmu_update_irq(env);
1527 }
1528
1529 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1530                          uint64_t value)
1531 {
1532     value &= pmu_counter_mask(env);
1533     env->cp15.c9_pmovsr |= value;
1534     pmu_update_irq(env);
1535 }
1536
1537 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1538                              uint64_t value, const uint8_t counter)
1539 {
1540     if (counter == 31) {
1541         pmccfiltr_write(env, ri, value);
1542     } else if (counter < pmu_num_counters(env)) {
1543         pmevcntr_op_start(env, counter);
1544
1545         /*
1546          * If this counter's event type is changing, store the current
1547          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1548          * pmevcntr_op_finish has the correct baseline when it converts back to
1549          * a delta.
1550          */
1551         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1552             PMXEVTYPER_EVTCOUNT;
1553         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1554         if (old_event != new_event) {
1555             uint64_t count = 0;
1556             if (event_supported(new_event)) {
1557                 uint16_t event_idx = supported_event_map[new_event];
1558                 count = pm_events[event_idx].get_count(env);
1559             }
1560             env->cp15.c14_pmevcntr_delta[counter] = count;
1561         }
1562
1563         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1564         pmevcntr_op_finish(env, counter);
1565     }
1566     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1567      * PMSELR value is equal to or greater than the number of implemented
1568      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1569      */
1570 }
1571
1572 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1573                                const uint8_t counter)
1574 {
1575     if (counter == 31) {
1576         return env->cp15.pmccfiltr_el0;
1577     } else if (counter < pmu_num_counters(env)) {
1578         return env->cp15.c14_pmevtyper[counter];
1579     } else {
1580       /*
1581        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1582        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1583        */
1584         return 0;
1585     }
1586 }
1587
1588 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1589                               uint64_t value)
1590 {
1591     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1592     pmevtyper_write(env, ri, value, counter);
1593 }
1594
1595 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1596                                uint64_t value)
1597 {
1598     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1599     env->cp15.c14_pmevtyper[counter] = value;
1600
1601     /*
1602      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1603      * pmu_op_finish calls when loading saved state for a migration. Because
1604      * we're potentially updating the type of event here, the value written to
1605      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1606      * different counter type. Therefore, we need to set this value to the
1607      * current count for the counter type we're writing so that pmu_op_finish
1608      * has the correct count for its calculation.
1609      */
1610     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1611     if (event_supported(event)) {
1612         uint16_t event_idx = supported_event_map[event];
1613         env->cp15.c14_pmevcntr_delta[counter] =
1614             pm_events[event_idx].get_count(env);
1615     }
1616 }
1617
1618 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1619 {
1620     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1621     return pmevtyper_read(env, ri, counter);
1622 }
1623
1624 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1625                              uint64_t value)
1626 {
1627     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1628 }
1629
1630 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1631 {
1632     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1633 }
1634
1635 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636                              uint64_t value, uint8_t counter)
1637 {
1638     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1639         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1640         value &= MAKE_64BIT_MASK(0, 32);
1641     }
1642     if (counter < pmu_num_counters(env)) {
1643         pmevcntr_op_start(env, counter);
1644         env->cp15.c14_pmevcntr[counter] = value;
1645         pmevcntr_op_finish(env, counter);
1646     }
1647     /*
1648      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1649      * are CONSTRAINED UNPREDICTABLE.
1650      */
1651 }
1652
1653 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1654                               uint8_t counter)
1655 {
1656     if (counter < pmu_num_counters(env)) {
1657         uint64_t ret;
1658         pmevcntr_op_start(env, counter);
1659         ret = env->cp15.c14_pmevcntr[counter];
1660         pmevcntr_op_finish(env, counter);
1661         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1662             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1663             ret &= MAKE_64BIT_MASK(0, 32);
1664         }
1665         return ret;
1666     } else {
1667       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1668        * are CONSTRAINED UNPREDICTABLE. */
1669         return 0;
1670     }
1671 }
1672
1673 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1674                              uint64_t value)
1675 {
1676     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1677     pmevcntr_write(env, ri, value, counter);
1678 }
1679
1680 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1681 {
1682     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1683     return pmevcntr_read(env, ri, counter);
1684 }
1685
1686 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1687                              uint64_t value)
1688 {
1689     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1690     assert(counter < pmu_num_counters(env));
1691     env->cp15.c14_pmevcntr[counter] = value;
1692     pmevcntr_write(env, ri, value, counter);
1693 }
1694
1695 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1696 {
1697     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1698     assert(counter < pmu_num_counters(env));
1699     return env->cp15.c14_pmevcntr[counter];
1700 }
1701
1702 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1703                              uint64_t value)
1704 {
1705     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1706 }
1707
1708 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1709 {
1710     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1711 }
1712
1713 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1714                             uint64_t value)
1715 {
1716     if (arm_feature(env, ARM_FEATURE_V8)) {
1717         env->cp15.c9_pmuserenr = value & 0xf;
1718     } else {
1719         env->cp15.c9_pmuserenr = value & 1;
1720     }
1721 }
1722
1723 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1724                              uint64_t value)
1725 {
1726     /* We have no event counters so only the C bit can be changed */
1727     value &= pmu_counter_mask(env);
1728     env->cp15.c9_pminten |= value;
1729     pmu_update_irq(env);
1730 }
1731
1732 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1733                              uint64_t value)
1734 {
1735     value &= pmu_counter_mask(env);
1736     env->cp15.c9_pminten &= ~value;
1737     pmu_update_irq(env);
1738 }
1739
1740 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1741                        uint64_t value)
1742 {
1743     /* Note that even though the AArch64 view of this register has bits
1744      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1745      * architectural requirements for bits which are RES0 only in some
1746      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1747      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1748      */
1749     raw_write(env, ri, value & ~0x1FULL);
1750 }
1751
1752 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1753 {
1754     /* Begin with base v8.0 state.  */
1755     uint64_t valid_mask = 0x3fff;
1756     ARMCPU *cpu = env_archcpu(env);
1757
1758     /*
1759      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1760      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1761      * Instead, choose the format based on the mode of EL3.
1762      */
1763     if (arm_el_is_aa64(env, 3)) {
1764         value |= SCR_FW | SCR_AW;      /* RES1 */
1765         valid_mask &= ~SCR_NET;        /* RES0 */
1766
1767         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1768             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1769             value |= SCR_RW;           /* RAO/WI */
1770         }
1771         if (cpu_isar_feature(aa64_ras, cpu)) {
1772             valid_mask |= SCR_TERR;
1773         }
1774         if (cpu_isar_feature(aa64_lor, cpu)) {
1775             valid_mask |= SCR_TLOR;
1776         }
1777         if (cpu_isar_feature(aa64_pauth, cpu)) {
1778             valid_mask |= SCR_API | SCR_APK;
1779         }
1780         if (cpu_isar_feature(aa64_sel2, cpu)) {
1781             valid_mask |= SCR_EEL2;
1782         }
1783         if (cpu_isar_feature(aa64_mte, cpu)) {
1784             valid_mask |= SCR_ATA;
1785         }
1786         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1787             valid_mask |= SCR_ENSCXT;
1788         }
1789         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1790             valid_mask |= SCR_EASE | SCR_NMEA;
1791         }
1792         if (cpu_isar_feature(aa64_sme, cpu)) {
1793             valid_mask |= SCR_ENTP2;
1794         }
1795     } else {
1796         valid_mask &= ~(SCR_RW | SCR_ST);
1797         if (cpu_isar_feature(aa32_ras, cpu)) {
1798             valid_mask |= SCR_TERR;
1799         }
1800     }
1801
1802     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1803         valid_mask &= ~SCR_HCE;
1804
1805         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1806          * supported if EL2 exists. The bit is UNK/SBZP when
1807          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1808          * when EL2 is unavailable.
1809          * On ARMv8, this bit is always available.
1810          */
1811         if (arm_feature(env, ARM_FEATURE_V7) &&
1812             !arm_feature(env, ARM_FEATURE_V8)) {
1813             valid_mask &= ~SCR_SMD;
1814         }
1815     }
1816
1817     /* Clear all-context RES0 bits.  */
1818     value &= valid_mask;
1819     raw_write(env, ri, value);
1820 }
1821
1822 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1823 {
1824     /*
1825      * scr_write will set the RES1 bits on an AArch64-only CPU.
1826      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1827      */
1828     scr_write(env, ri, 0);
1829 }
1830
1831 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1832                                        const ARMCPRegInfo *ri,
1833                                        bool isread)
1834 {
1835     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1836         return CP_ACCESS_TRAP_EL2;
1837     }
1838
1839     return CP_ACCESS_OK;
1840 }
1841
1842 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1843 {
1844     ARMCPU *cpu = env_archcpu(env);
1845
1846     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1847      * bank
1848      */
1849     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1850                                         ri->secure & ARM_CP_SECSTATE_S);
1851
1852     return cpu->ccsidr[index];
1853 }
1854
1855 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1856                          uint64_t value)
1857 {
1858     raw_write(env, ri, value & 0xf);
1859 }
1860
1861 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1862 {
1863     CPUState *cs = env_cpu(env);
1864     bool el1 = arm_current_el(env) == 1;
1865     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1866     uint64_t ret = 0;
1867
1868     if (hcr_el2 & HCR_IMO) {
1869         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1870             ret |= CPSR_I;
1871         }
1872     } else {
1873         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1874             ret |= CPSR_I;
1875         }
1876     }
1877
1878     if (hcr_el2 & HCR_FMO) {
1879         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1880             ret |= CPSR_F;
1881         }
1882     } else {
1883         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1884             ret |= CPSR_F;
1885         }
1886     }
1887
1888     if (hcr_el2 & HCR_AMO) {
1889         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1890             ret |= CPSR_A;
1891         }
1892     }
1893
1894     return ret;
1895 }
1896
1897 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1898                                        bool isread)
1899 {
1900     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1901         return CP_ACCESS_TRAP_EL2;
1902     }
1903
1904     return CP_ACCESS_OK;
1905 }
1906
1907 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1908                                        bool isread)
1909 {
1910     if (arm_feature(env, ARM_FEATURE_V8)) {
1911         return access_aa64_tid1(env, ri, isread);
1912     }
1913
1914     return CP_ACCESS_OK;
1915 }
1916
1917 static const ARMCPRegInfo v7_cp_reginfo[] = {
1918     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1919     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1920       .access = PL1_W, .type = ARM_CP_NOP },
1921     /* Performance monitors are implementation defined in v7,
1922      * but with an ARM recommended set of registers, which we
1923      * follow.
1924      *
1925      * Performance registers fall into three categories:
1926      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1927      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1928      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1929      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1930      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1931      */
1932     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1933       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1934       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1935       .writefn = pmcntenset_write,
1936       .accessfn = pmreg_access,
1937       .raw_writefn = raw_write },
1938     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1939       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1940       .access = PL0_RW, .accessfn = pmreg_access,
1941       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1942       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1943     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1944       .access = PL0_RW,
1945       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1946       .accessfn = pmreg_access,
1947       .writefn = pmcntenclr_write,
1948       .type = ARM_CP_ALIAS | ARM_CP_IO },
1949     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1950       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1951       .access = PL0_RW, .accessfn = pmreg_access,
1952       .type = ARM_CP_ALIAS | ARM_CP_IO,
1953       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1954       .writefn = pmcntenclr_write },
1955     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1956       .access = PL0_RW, .type = ARM_CP_IO,
1957       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1958       .accessfn = pmreg_access,
1959       .writefn = pmovsr_write,
1960       .raw_writefn = raw_write },
1961     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1962       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1963       .access = PL0_RW, .accessfn = pmreg_access,
1964       .type = ARM_CP_ALIAS | ARM_CP_IO,
1965       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1966       .writefn = pmovsr_write,
1967       .raw_writefn = raw_write },
1968     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1969       .access = PL0_W, .accessfn = pmreg_access_swinc,
1970       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1971       .writefn = pmswinc_write },
1972     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1973       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1974       .access = PL0_W, .accessfn = pmreg_access_swinc,
1975       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1976       .writefn = pmswinc_write },
1977     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1978       .access = PL0_RW, .type = ARM_CP_ALIAS,
1979       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1980       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1981       .raw_writefn = raw_write},
1982     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1983       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1984       .access = PL0_RW, .accessfn = pmreg_access_selr,
1985       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1986       .writefn = pmselr_write, .raw_writefn = raw_write, },
1987     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1988       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1989       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1990       .accessfn = pmreg_access_ccntr },
1991     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1992       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1993       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1994       .type = ARM_CP_IO,
1995       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1996       .readfn = pmccntr_read, .writefn = pmccntr_write,
1997       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1998     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1999       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2000       .access = PL0_RW, .accessfn = pmreg_access,
2001       .type = ARM_CP_ALIAS | ARM_CP_IO,
2002       .resetvalue = 0, },
2003     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2004       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2005       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2006       .access = PL0_RW, .accessfn = pmreg_access,
2007       .type = ARM_CP_IO,
2008       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2009       .resetvalue = 0, },
2010     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2011       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2012       .accessfn = pmreg_access,
2013       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2014     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2015       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2016       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2017       .accessfn = pmreg_access,
2018       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2019     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2020       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2021       .accessfn = pmreg_access_xevcntr,
2022       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2023     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2024       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2025       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2026       .accessfn = pmreg_access_xevcntr,
2027       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2028     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2029       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2030       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2031       .resetvalue = 0,
2032       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2033     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2034       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2035       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2036       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2037       .resetvalue = 0,
2038       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2039     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2040       .access = PL1_RW, .accessfn = access_tpm,
2041       .type = ARM_CP_ALIAS | ARM_CP_IO,
2042       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2043       .resetvalue = 0,
2044       .writefn = pmintenset_write, .raw_writefn = raw_write },
2045     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2046       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2047       .access = PL1_RW, .accessfn = access_tpm,
2048       .type = ARM_CP_IO,
2049       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2050       .writefn = pmintenset_write, .raw_writefn = raw_write,
2051       .resetvalue = 0x0 },
2052     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2053       .access = PL1_RW, .accessfn = access_tpm,
2054       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2055       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2056       .writefn = pmintenclr_write, },
2057     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2058       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2059       .access = PL1_RW, .accessfn = access_tpm,
2060       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2061       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2062       .writefn = pmintenclr_write },
2063     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2064       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2065       .access = PL1_R,
2066       .accessfn = access_aa64_tid2,
2067       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2068     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2069       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2070       .access = PL1_RW,
2071       .accessfn = access_aa64_tid2,
2072       .writefn = csselr_write, .resetvalue = 0,
2073       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2074                              offsetof(CPUARMState, cp15.csselr_ns) } },
2075     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2076      * just RAZ for all cores:
2077      */
2078     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2079       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2080       .access = PL1_R, .type = ARM_CP_CONST,
2081       .accessfn = access_aa64_tid1,
2082       .resetvalue = 0 },
2083     /* Auxiliary fault status registers: these also are IMPDEF, and we
2084      * choose to RAZ/WI for all cores.
2085      */
2086     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2087       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2088       .access = PL1_RW, .accessfn = access_tvm_trvm,
2089       .type = ARM_CP_CONST, .resetvalue = 0 },
2090     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2091       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2092       .access = PL1_RW, .accessfn = access_tvm_trvm,
2093       .type = ARM_CP_CONST, .resetvalue = 0 },
2094     /* MAIR can just read-as-written because we don't implement caches
2095      * and so don't need to care about memory attributes.
2096      */
2097     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2098       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2099       .access = PL1_RW, .accessfn = access_tvm_trvm,
2100       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2101       .resetvalue = 0 },
2102     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2103       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2104       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2105       .resetvalue = 0 },
2106     /* For non-long-descriptor page tables these are PRRR and NMRR;
2107      * regardless they still act as reads-as-written for QEMU.
2108      */
2109      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2110       * allows them to assign the correct fieldoffset based on the endianness
2111       * handled in the field definitions.
2112       */
2113     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2114       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2115       .access = PL1_RW, .accessfn = access_tvm_trvm,
2116       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2117                              offsetof(CPUARMState, cp15.mair0_ns) },
2118       .resetfn = arm_cp_reset_ignore },
2119     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2120       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2121       .access = PL1_RW, .accessfn = access_tvm_trvm,
2122       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2123                              offsetof(CPUARMState, cp15.mair1_ns) },
2124       .resetfn = arm_cp_reset_ignore },
2125     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2126       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2127       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2128     /* 32 bit ITLB invalidates */
2129     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2130       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2131       .writefn = tlbiall_write },
2132     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2133       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134       .writefn = tlbimva_write },
2135     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2136       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2137       .writefn = tlbiasid_write },
2138     /* 32 bit DTLB invalidates */
2139     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2140       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2141       .writefn = tlbiall_write },
2142     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2143       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2144       .writefn = tlbimva_write },
2145     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2146       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2147       .writefn = tlbiasid_write },
2148     /* 32 bit TLB invalidates */
2149     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2150       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2151       .writefn = tlbiall_write },
2152     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2153       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2154       .writefn = tlbimva_write },
2155     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2156       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2157       .writefn = tlbiasid_write },
2158     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2159       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2160       .writefn = tlbimvaa_write },
2161 };
2162
2163 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2164     /* 32 bit TLB invalidates, Inner Shareable */
2165     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2166       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2167       .writefn = tlbiall_is_write },
2168     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2169       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2170       .writefn = tlbimva_is_write },
2171     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2172       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2173       .writefn = tlbiasid_is_write },
2174     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2175       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2176       .writefn = tlbimvaa_is_write },
2177 };
2178
2179 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2180     /* PMOVSSET is not implemented in v7 before v7ve */
2181     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2182       .access = PL0_RW, .accessfn = pmreg_access,
2183       .type = ARM_CP_ALIAS | ARM_CP_IO,
2184       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2185       .writefn = pmovsset_write,
2186       .raw_writefn = raw_write },
2187     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2188       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2189       .access = PL0_RW, .accessfn = pmreg_access,
2190       .type = ARM_CP_ALIAS | ARM_CP_IO,
2191       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2192       .writefn = pmovsset_write,
2193       .raw_writefn = raw_write },
2194 };
2195
2196 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2197                         uint64_t value)
2198 {
2199     value &= 1;
2200     env->teecr = value;
2201 }
2202
2203 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2204                                    bool isread)
2205 {
2206     /*
2207      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2208      * at all, so we don't need to check whether we're v8A.
2209      */
2210     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2211         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2212         return CP_ACCESS_TRAP_EL2;
2213     }
2214     return CP_ACCESS_OK;
2215 }
2216
2217 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2218                                     bool isread)
2219 {
2220     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2221         return CP_ACCESS_TRAP;
2222     }
2223     return teecr_access(env, ri, isread);
2224 }
2225
2226 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2227     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2228       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2229       .resetvalue = 0,
2230       .writefn = teecr_write, .accessfn = teecr_access },
2231     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2232       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2233       .accessfn = teehbr_access, .resetvalue = 0 },
2234 };
2235
2236 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2237     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2238       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2239       .access = PL0_RW,
2240       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2241     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2242       .access = PL0_RW,
2243       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2244                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2245       .resetfn = arm_cp_reset_ignore },
2246     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2247       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2248       .access = PL0_R|PL1_W,
2249       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2250       .resetvalue = 0},
2251     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2252       .access = PL0_R|PL1_W,
2253       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2254                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2255       .resetfn = arm_cp_reset_ignore },
2256     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2257       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2258       .access = PL1_RW,
2259       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2260     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2261       .access = PL1_RW,
2262       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2263                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2264       .resetvalue = 0 },
2265 };
2266
2267 #ifndef CONFIG_USER_ONLY
2268
2269 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2270                                        bool isread)
2271 {
2272     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2273      * Writable only at the highest implemented exception level.
2274      */
2275     int el = arm_current_el(env);
2276     uint64_t hcr;
2277     uint32_t cntkctl;
2278
2279     switch (el) {
2280     case 0:
2281         hcr = arm_hcr_el2_eff(env);
2282         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2283             cntkctl = env->cp15.cnthctl_el2;
2284         } else {
2285             cntkctl = env->cp15.c14_cntkctl;
2286         }
2287         if (!extract32(cntkctl, 0, 2)) {
2288             return CP_ACCESS_TRAP;
2289         }
2290         break;
2291     case 1:
2292         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2293             arm_is_secure_below_el3(env)) {
2294             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2295             return CP_ACCESS_TRAP_UNCATEGORIZED;
2296         }
2297         break;
2298     case 2:
2299     case 3:
2300         break;
2301     }
2302
2303     if (!isread && el < arm_highest_el(env)) {
2304         return CP_ACCESS_TRAP_UNCATEGORIZED;
2305     }
2306
2307     return CP_ACCESS_OK;
2308 }
2309
2310 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2311                                         bool isread)
2312 {
2313     unsigned int cur_el = arm_current_el(env);
2314     bool has_el2 = arm_is_el2_enabled(env);
2315     uint64_t hcr = arm_hcr_el2_eff(env);
2316
2317     switch (cur_el) {
2318     case 0:
2319         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2320         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2321             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2322                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2323         }
2324
2325         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2326         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2327             return CP_ACCESS_TRAP;
2328         }
2329
2330         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2331         if (hcr & HCR_E2H) {
2332             if (timeridx == GTIMER_PHYS &&
2333                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2334                 return CP_ACCESS_TRAP_EL2;
2335             }
2336         } else {
2337             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2338             if (has_el2 && timeridx == GTIMER_PHYS &&
2339                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2340                 return CP_ACCESS_TRAP_EL2;
2341             }
2342         }
2343         break;
2344
2345     case 1:
2346         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2347         if (has_el2 && timeridx == GTIMER_PHYS &&
2348             (hcr & HCR_E2H
2349              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2350              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2351             return CP_ACCESS_TRAP_EL2;
2352         }
2353         break;
2354     }
2355     return CP_ACCESS_OK;
2356 }
2357
2358 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2359                                       bool isread)
2360 {
2361     unsigned int cur_el = arm_current_el(env);
2362     bool has_el2 = arm_is_el2_enabled(env);
2363     uint64_t hcr = arm_hcr_el2_eff(env);
2364
2365     switch (cur_el) {
2366     case 0:
2367         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2368             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2369             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2370                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2371         }
2372
2373         /*
2374          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2375          * EL0 if EL0[PV]TEN is zero.
2376          */
2377         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2378             return CP_ACCESS_TRAP;
2379         }
2380         /* fall through */
2381
2382     case 1:
2383         if (has_el2 && timeridx == GTIMER_PHYS) {
2384             if (hcr & HCR_E2H) {
2385                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2386                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2387                     return CP_ACCESS_TRAP_EL2;
2388                 }
2389             } else {
2390                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2391                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2392                     return CP_ACCESS_TRAP_EL2;
2393                 }
2394             }
2395         }
2396         break;
2397     }
2398     return CP_ACCESS_OK;
2399 }
2400
2401 static CPAccessResult gt_pct_access(CPUARMState *env,
2402                                     const ARMCPRegInfo *ri,
2403                                     bool isread)
2404 {
2405     return gt_counter_access(env, GTIMER_PHYS, isread);
2406 }
2407
2408 static CPAccessResult gt_vct_access(CPUARMState *env,
2409                                     const ARMCPRegInfo *ri,
2410                                     bool isread)
2411 {
2412     return gt_counter_access(env, GTIMER_VIRT, isread);
2413 }
2414
2415 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2416                                        bool isread)
2417 {
2418     return gt_timer_access(env, GTIMER_PHYS, isread);
2419 }
2420
2421 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2422                                        bool isread)
2423 {
2424     return gt_timer_access(env, GTIMER_VIRT, isread);
2425 }
2426
2427 static CPAccessResult gt_stimer_access(CPUARMState *env,
2428                                        const ARMCPRegInfo *ri,
2429                                        bool isread)
2430 {
2431     /* The AArch64 register view of the secure physical timer is
2432      * always accessible from EL3, and configurably accessible from
2433      * Secure EL1.
2434      */
2435     switch (arm_current_el(env)) {
2436     case 1:
2437         if (!arm_is_secure(env)) {
2438             return CP_ACCESS_TRAP;
2439         }
2440         if (!(env->cp15.scr_el3 & SCR_ST)) {
2441             return CP_ACCESS_TRAP_EL3;
2442         }
2443         return CP_ACCESS_OK;
2444     case 0:
2445     case 2:
2446         return CP_ACCESS_TRAP;
2447     case 3:
2448         return CP_ACCESS_OK;
2449     default:
2450         g_assert_not_reached();
2451     }
2452 }
2453
2454 static uint64_t gt_get_countervalue(CPUARMState *env)
2455 {
2456     ARMCPU *cpu = env_archcpu(env);
2457
2458     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2459 }
2460
2461 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2462 {
2463     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2464
2465     if (gt->ctl & 1) {
2466         /* Timer enabled: calculate and set current ISTATUS, irq, and
2467          * reset timer to when ISTATUS next has to change
2468          */
2469         uint64_t offset = timeridx == GTIMER_VIRT ?
2470                                       cpu->env.cp15.cntvoff_el2 : 0;
2471         uint64_t count = gt_get_countervalue(&cpu->env);
2472         /* Note that this must be unsigned 64 bit arithmetic: */
2473         int istatus = count - offset >= gt->cval;
2474         uint64_t nexttick;
2475         int irqstate;
2476
2477         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2478
2479         irqstate = (istatus && !(gt->ctl & 2));
2480         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2481
2482         if (istatus) {
2483             /* Next transition is when count rolls back over to zero */
2484             nexttick = UINT64_MAX;
2485         } else {
2486             /* Next transition is when we hit cval */
2487             nexttick = gt->cval + offset;
2488         }
2489         /* Note that the desired next expiry time might be beyond the
2490          * signed-64-bit range of a QEMUTimer -- in this case we just
2491          * set the timer for as far in the future as possible. When the
2492          * timer expires we will reset the timer for any remaining period.
2493          */
2494         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2495             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2496         } else {
2497             timer_mod(cpu->gt_timer[timeridx], nexttick);
2498         }
2499         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2500     } else {
2501         /* Timer disabled: ISTATUS and timer output always clear */
2502         gt->ctl &= ~4;
2503         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2504         timer_del(cpu->gt_timer[timeridx]);
2505         trace_arm_gt_recalc_disabled(timeridx);
2506     }
2507 }
2508
2509 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2510                            int timeridx)
2511 {
2512     ARMCPU *cpu = env_archcpu(env);
2513
2514     timer_del(cpu->gt_timer[timeridx]);
2515 }
2516
2517 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2518 {
2519     return gt_get_countervalue(env);
2520 }
2521
2522 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2523 {
2524     uint64_t hcr;
2525
2526     switch (arm_current_el(env)) {
2527     case 2:
2528         hcr = arm_hcr_el2_eff(env);
2529         if (hcr & HCR_E2H) {
2530             return 0;
2531         }
2532         break;
2533     case 0:
2534         hcr = arm_hcr_el2_eff(env);
2535         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2536             return 0;
2537         }
2538         break;
2539     }
2540
2541     return env->cp15.cntvoff_el2;
2542 }
2543
2544 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2545 {
2546     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2547 }
2548
2549 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2550                           int timeridx,
2551                           uint64_t value)
2552 {
2553     trace_arm_gt_cval_write(timeridx, value);
2554     env->cp15.c14_timer[timeridx].cval = value;
2555     gt_recalc_timer(env_archcpu(env), timeridx);
2556 }
2557
2558 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2559                              int timeridx)
2560 {
2561     uint64_t offset = 0;
2562
2563     switch (timeridx) {
2564     case GTIMER_VIRT:
2565     case GTIMER_HYPVIRT:
2566         offset = gt_virt_cnt_offset(env);
2567         break;
2568     }
2569
2570     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2571                       (gt_get_countervalue(env) - offset));
2572 }
2573
2574 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2575                           int timeridx,
2576                           uint64_t value)
2577 {
2578     uint64_t offset = 0;
2579
2580     switch (timeridx) {
2581     case GTIMER_VIRT:
2582     case GTIMER_HYPVIRT:
2583         offset = gt_virt_cnt_offset(env);
2584         break;
2585     }
2586
2587     trace_arm_gt_tval_write(timeridx, value);
2588     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2589                                          sextract64(value, 0, 32);
2590     gt_recalc_timer(env_archcpu(env), timeridx);
2591 }
2592
2593 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2594                          int timeridx,
2595                          uint64_t value)
2596 {
2597     ARMCPU *cpu = env_archcpu(env);
2598     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2599
2600     trace_arm_gt_ctl_write(timeridx, value);
2601     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2602     if ((oldval ^ value) & 1) {
2603         /* Enable toggled */
2604         gt_recalc_timer(cpu, timeridx);
2605     } else if ((oldval ^ value) & 2) {
2606         /* IMASK toggled: don't need to recalculate,
2607          * just set the interrupt line based on ISTATUS
2608          */
2609         int irqstate = (oldval & 4) && !(value & 2);
2610
2611         trace_arm_gt_imask_toggle(timeridx, irqstate);
2612         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2613     }
2614 }
2615
2616 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2617 {
2618     gt_timer_reset(env, ri, GTIMER_PHYS);
2619 }
2620
2621 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2622                                uint64_t value)
2623 {
2624     gt_cval_write(env, ri, GTIMER_PHYS, value);
2625 }
2626
2627 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2628 {
2629     return gt_tval_read(env, ri, GTIMER_PHYS);
2630 }
2631
2632 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2633                                uint64_t value)
2634 {
2635     gt_tval_write(env, ri, GTIMER_PHYS, value);
2636 }
2637
2638 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2639                               uint64_t value)
2640 {
2641     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2642 }
2643
2644 static int gt_phys_redir_timeridx(CPUARMState *env)
2645 {
2646     switch (arm_mmu_idx(env)) {
2647     case ARMMMUIdx_E20_0:
2648     case ARMMMUIdx_E20_2:
2649     case ARMMMUIdx_E20_2_PAN:
2650     case ARMMMUIdx_SE20_0:
2651     case ARMMMUIdx_SE20_2:
2652     case ARMMMUIdx_SE20_2_PAN:
2653         return GTIMER_HYP;
2654     default:
2655         return GTIMER_PHYS;
2656     }
2657 }
2658
2659 static int gt_virt_redir_timeridx(CPUARMState *env)
2660 {
2661     switch (arm_mmu_idx(env)) {
2662     case ARMMMUIdx_E20_0:
2663     case ARMMMUIdx_E20_2:
2664     case ARMMMUIdx_E20_2_PAN:
2665     case ARMMMUIdx_SE20_0:
2666     case ARMMMUIdx_SE20_2:
2667     case ARMMMUIdx_SE20_2_PAN:
2668         return GTIMER_HYPVIRT;
2669     default:
2670         return GTIMER_VIRT;
2671     }
2672 }
2673
2674 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2675                                         const ARMCPRegInfo *ri)
2676 {
2677     int timeridx = gt_phys_redir_timeridx(env);
2678     return env->cp15.c14_timer[timeridx].cval;
2679 }
2680
2681 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2682                                      uint64_t value)
2683 {
2684     int timeridx = gt_phys_redir_timeridx(env);
2685     gt_cval_write(env, ri, timeridx, value);
2686 }
2687
2688 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2689                                         const ARMCPRegInfo *ri)
2690 {
2691     int timeridx = gt_phys_redir_timeridx(env);
2692     return gt_tval_read(env, ri, timeridx);
2693 }
2694
2695 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2696                                      uint64_t value)
2697 {
2698     int timeridx = gt_phys_redir_timeridx(env);
2699     gt_tval_write(env, ri, timeridx, value);
2700 }
2701
2702 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2703                                        const ARMCPRegInfo *ri)
2704 {
2705     int timeridx = gt_phys_redir_timeridx(env);
2706     return env->cp15.c14_timer[timeridx].ctl;
2707 }
2708
2709 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2710                                     uint64_t value)
2711 {
2712     int timeridx = gt_phys_redir_timeridx(env);
2713     gt_ctl_write(env, ri, timeridx, value);
2714 }
2715
2716 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2717 {
2718     gt_timer_reset(env, ri, GTIMER_VIRT);
2719 }
2720
2721 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2722                                uint64_t value)
2723 {
2724     gt_cval_write(env, ri, GTIMER_VIRT, value);
2725 }
2726
2727 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2728 {
2729     return gt_tval_read(env, ri, GTIMER_VIRT);
2730 }
2731
2732 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2733                                uint64_t value)
2734 {
2735     gt_tval_write(env, ri, GTIMER_VIRT, value);
2736 }
2737
2738 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2739                               uint64_t value)
2740 {
2741     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2742 }
2743
2744 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2745                               uint64_t value)
2746 {
2747     ARMCPU *cpu = env_archcpu(env);
2748
2749     trace_arm_gt_cntvoff_write(value);
2750     raw_write(env, ri, value);
2751     gt_recalc_timer(cpu, GTIMER_VIRT);
2752 }
2753
2754 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2755                                         const ARMCPRegInfo *ri)
2756 {
2757     int timeridx = gt_virt_redir_timeridx(env);
2758     return env->cp15.c14_timer[timeridx].cval;
2759 }
2760
2761 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2762                                      uint64_t value)
2763 {
2764     int timeridx = gt_virt_redir_timeridx(env);
2765     gt_cval_write(env, ri, timeridx, value);
2766 }
2767
2768 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2769                                         const ARMCPRegInfo *ri)
2770 {
2771     int timeridx = gt_virt_redir_timeridx(env);
2772     return gt_tval_read(env, ri, timeridx);
2773 }
2774
2775 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2776                                      uint64_t value)
2777 {
2778     int timeridx = gt_virt_redir_timeridx(env);
2779     gt_tval_write(env, ri, timeridx, value);
2780 }
2781
2782 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2783                                        const ARMCPRegInfo *ri)
2784 {
2785     int timeridx = gt_virt_redir_timeridx(env);
2786     return env->cp15.c14_timer[timeridx].ctl;
2787 }
2788
2789 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2790                                     uint64_t value)
2791 {
2792     int timeridx = gt_virt_redir_timeridx(env);
2793     gt_ctl_write(env, ri, timeridx, value);
2794 }
2795
2796 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2797 {
2798     gt_timer_reset(env, ri, GTIMER_HYP);
2799 }
2800
2801 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2802                               uint64_t value)
2803 {
2804     gt_cval_write(env, ri, GTIMER_HYP, value);
2805 }
2806
2807 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2808 {
2809     return gt_tval_read(env, ri, GTIMER_HYP);
2810 }
2811
2812 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2813                               uint64_t value)
2814 {
2815     gt_tval_write(env, ri, GTIMER_HYP, value);
2816 }
2817
2818 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2819                               uint64_t value)
2820 {
2821     gt_ctl_write(env, ri, GTIMER_HYP, value);
2822 }
2823
2824 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2825 {
2826     gt_timer_reset(env, ri, GTIMER_SEC);
2827 }
2828
2829 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2830                               uint64_t value)
2831 {
2832     gt_cval_write(env, ri, GTIMER_SEC, value);
2833 }
2834
2835 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2836 {
2837     return gt_tval_read(env, ri, GTIMER_SEC);
2838 }
2839
2840 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2841                               uint64_t value)
2842 {
2843     gt_tval_write(env, ri, GTIMER_SEC, value);
2844 }
2845
2846 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2847                               uint64_t value)
2848 {
2849     gt_ctl_write(env, ri, GTIMER_SEC, value);
2850 }
2851
2852 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2853 {
2854     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2855 }
2856
2857 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2858                              uint64_t value)
2859 {
2860     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2861 }
2862
2863 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2864 {
2865     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2866 }
2867
2868 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2869                              uint64_t value)
2870 {
2871     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2872 }
2873
2874 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2875                             uint64_t value)
2876 {
2877     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2878 }
2879
2880 void arm_gt_ptimer_cb(void *opaque)
2881 {
2882     ARMCPU *cpu = opaque;
2883
2884     gt_recalc_timer(cpu, GTIMER_PHYS);
2885 }
2886
2887 void arm_gt_vtimer_cb(void *opaque)
2888 {
2889     ARMCPU *cpu = opaque;
2890
2891     gt_recalc_timer(cpu, GTIMER_VIRT);
2892 }
2893
2894 void arm_gt_htimer_cb(void *opaque)
2895 {
2896     ARMCPU *cpu = opaque;
2897
2898     gt_recalc_timer(cpu, GTIMER_HYP);
2899 }
2900
2901 void arm_gt_stimer_cb(void *opaque)
2902 {
2903     ARMCPU *cpu = opaque;
2904
2905     gt_recalc_timer(cpu, GTIMER_SEC);
2906 }
2907
2908 void arm_gt_hvtimer_cb(void *opaque)
2909 {
2910     ARMCPU *cpu = opaque;
2911
2912     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2913 }
2914
2915 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2916 {
2917     ARMCPU *cpu = env_archcpu(env);
2918
2919     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2920 }
2921
2922 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2923     /* Note that CNTFRQ is purely reads-as-written for the benefit
2924      * of software; writing it doesn't actually change the timer frequency.
2925      * Our reset value matches the fixed frequency we implement the timer at.
2926      */
2927     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2928       .type = ARM_CP_ALIAS,
2929       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2930       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2931     },
2932     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2933       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2934       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2935       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2936       .resetfn = arm_gt_cntfrq_reset,
2937     },
2938     /* overall control: mostly access permissions */
2939     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2940       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2941       .access = PL1_RW,
2942       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2943       .resetvalue = 0,
2944     },
2945     /* per-timer control */
2946     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2947       .secure = ARM_CP_SECSTATE_NS,
2948       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2949       .accessfn = gt_ptimer_access,
2950       .fieldoffset = offsetoflow32(CPUARMState,
2951                                    cp15.c14_timer[GTIMER_PHYS].ctl),
2952       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2953       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2954     },
2955     { .name = "CNTP_CTL_S",
2956       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2957       .secure = ARM_CP_SECSTATE_S,
2958       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2959       .accessfn = gt_ptimer_access,
2960       .fieldoffset = offsetoflow32(CPUARMState,
2961                                    cp15.c14_timer[GTIMER_SEC].ctl),
2962       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2963     },
2964     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2965       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2966       .type = ARM_CP_IO, .access = PL0_RW,
2967       .accessfn = gt_ptimer_access,
2968       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2969       .resetvalue = 0,
2970       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2971       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2972     },
2973     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2974       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2975       .accessfn = gt_vtimer_access,
2976       .fieldoffset = offsetoflow32(CPUARMState,
2977                                    cp15.c14_timer[GTIMER_VIRT].ctl),
2978       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2979       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2980     },
2981     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2982       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2983       .type = ARM_CP_IO, .access = PL0_RW,
2984       .accessfn = gt_vtimer_access,
2985       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2986       .resetvalue = 0,
2987       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2988       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2989     },
2990     /* TimerValue views: a 32 bit downcounting view of the underlying state */
2991     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2992       .secure = ARM_CP_SECSTATE_NS,
2993       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2994       .accessfn = gt_ptimer_access,
2995       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2996     },
2997     { .name = "CNTP_TVAL_S",
2998       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2999       .secure = ARM_CP_SECSTATE_S,
3000       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3001       .accessfn = gt_ptimer_access,
3002       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3003     },
3004     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3005       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3006       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3007       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3008       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3009     },
3010     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3011       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3012       .accessfn = gt_vtimer_access,
3013       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3014     },
3015     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3016       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3017       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3018       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3019       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3020     },
3021     /* The counter itself */
3022     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3023       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3024       .accessfn = gt_pct_access,
3025       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3026     },
3027     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3028       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3029       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3030       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3031     },
3032     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3033       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3034       .accessfn = gt_vct_access,
3035       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3036     },
3037     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3038       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3039       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3040       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3041     },
3042     /* Comparison value, indicating when the timer goes off */
3043     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3044       .secure = ARM_CP_SECSTATE_NS,
3045       .access = PL0_RW,
3046       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3047       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3048       .accessfn = gt_ptimer_access,
3049       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3050       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3051     },
3052     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3053       .secure = ARM_CP_SECSTATE_S,
3054       .access = PL0_RW,
3055       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3056       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3057       .accessfn = gt_ptimer_access,
3058       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3059     },
3060     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3061       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3062       .access = PL0_RW,
3063       .type = ARM_CP_IO,
3064       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3065       .resetvalue = 0, .accessfn = gt_ptimer_access,
3066       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3067       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3068     },
3069     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3070       .access = PL0_RW,
3071       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3072       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3073       .accessfn = gt_vtimer_access,
3074       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3075       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3076     },
3077     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3078       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3079       .access = PL0_RW,
3080       .type = ARM_CP_IO,
3081       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3082       .resetvalue = 0, .accessfn = gt_vtimer_access,
3083       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3084       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3085     },
3086     /* Secure timer -- this is actually restricted to only EL3
3087      * and configurably Secure-EL1 via the accessfn.
3088      */
3089     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3090       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3091       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3092       .accessfn = gt_stimer_access,
3093       .readfn = gt_sec_tval_read,
3094       .writefn = gt_sec_tval_write,
3095       .resetfn = gt_sec_timer_reset,
3096     },
3097     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3098       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3099       .type = ARM_CP_IO, .access = PL1_RW,
3100       .accessfn = gt_stimer_access,
3101       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3102       .resetvalue = 0,
3103       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3104     },
3105     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3106       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3107       .type = ARM_CP_IO, .access = PL1_RW,
3108       .accessfn = gt_stimer_access,
3109       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3110       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3111     },
3112 };
3113
3114 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3115                                  bool isread)
3116 {
3117     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3118         return CP_ACCESS_TRAP;
3119     }
3120     return CP_ACCESS_OK;
3121 }
3122
3123 #else
3124
3125 /* In user-mode most of the generic timer registers are inaccessible
3126  * however modern kernels (4.12+) allow access to cntvct_el0
3127  */
3128
3129 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3130 {
3131     ARMCPU *cpu = env_archcpu(env);
3132
3133     /* Currently we have no support for QEMUTimer in linux-user so we
3134      * can't call gt_get_countervalue(env), instead we directly
3135      * call the lower level functions.
3136      */
3137     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3138 }
3139
3140 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3141     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3142       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3143       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3144       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3145       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3146     },
3147     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3148       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3149       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3150       .readfn = gt_virt_cnt_read,
3151     },
3152 };
3153
3154 #endif
3155
3156 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3157 {
3158     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3159         raw_write(env, ri, value);
3160     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3161         raw_write(env, ri, value & 0xfffff6ff);
3162     } else {
3163         raw_write(env, ri, value & 0xfffff1ff);
3164     }
3165 }
3166
3167 #ifndef CONFIG_USER_ONLY
3168 /* get_phys_addr() isn't present for user-mode-only targets */
3169
3170 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3171                                  bool isread)
3172 {
3173     if (ri->opc2 & 4) {
3174         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3175          * Secure EL1 (which can only happen if EL3 is AArch64).
3176          * They are simply UNDEF if executed from NS EL1.
3177          * They function normally from EL2 or EL3.
3178          */
3179         if (arm_current_el(env) == 1) {
3180             if (arm_is_secure_below_el3(env)) {
3181                 if (env->cp15.scr_el3 & SCR_EEL2) {
3182                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3183                 }
3184                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3185             }
3186             return CP_ACCESS_TRAP_UNCATEGORIZED;
3187         }
3188     }
3189     return CP_ACCESS_OK;
3190 }
3191
3192 #ifdef CONFIG_TCG
3193 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3194                              MMUAccessType access_type, ARMMMUIdx mmu_idx)
3195 {
3196     bool ret;
3197     uint64_t par64;
3198     bool format64 = false;
3199     ARMMMUFaultInfo fi = {};
3200     GetPhysAddrResult res = {};
3201
3202     ret = get_phys_addr(env, value, access_type, mmu_idx, &res, &fi);
3203
3204     /*
3205      * ATS operations only do S1 or S1+S2 translations, so we never
3206      * have to deal with the ARMCacheAttrs format for S2 only.
3207      */
3208     assert(!res.cacheattrs.is_s2_format);
3209
3210     if (ret) {
3211         /*
3212          * Some kinds of translation fault must cause exceptions rather
3213          * than being reported in the PAR.
3214          */
3215         int current_el = arm_current_el(env);
3216         int target_el;
3217         uint32_t syn, fsr, fsc;
3218         bool take_exc = false;
3219
3220         if (fi.s1ptw && current_el == 1
3221             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3222             /*
3223              * Synchronous stage 2 fault on an access made as part of the
3224              * translation table walk for AT S1E0* or AT S1E1* insn
3225              * executed from NS EL1. If this is a synchronous external abort
3226              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3227              * to EL3. Otherwise the fault is taken as an exception to EL2,
3228              * and HPFAR_EL2 holds the faulting IPA.
3229              */
3230             if (fi.type == ARMFault_SyncExternalOnWalk &&
3231                 (env->cp15.scr_el3 & SCR_EA)) {
3232                 target_el = 3;
3233             } else {
3234                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3235                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3236                     env->cp15.hpfar_el2 |= HPFAR_NS;
3237                 }
3238                 target_el = 2;
3239             }
3240             take_exc = true;
3241         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3242             /*
3243              * Synchronous external aborts during a translation table walk
3244              * are taken as Data Abort exceptions.
3245              */
3246             if (fi.stage2) {
3247                 if (current_el == 3) {
3248                     target_el = 3;
3249                 } else {
3250                     target_el = 2;
3251                 }
3252             } else {
3253                 target_el = exception_target_el(env);
3254             }
3255             take_exc = true;
3256         }
3257
3258         if (take_exc) {
3259             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3260             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3261                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3262                 fsr = arm_fi_to_lfsc(&fi);
3263                 fsc = extract32(fsr, 0, 6);
3264             } else {
3265                 fsr = arm_fi_to_sfsc(&fi);
3266                 fsc = 0x3f;
3267             }
3268             /*
3269              * Report exception with ESR indicating a fault due to a
3270              * translation table walk for a cache maintenance instruction.
3271              */
3272             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3273                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3274             env->exception.vaddress = value;
3275             env->exception.fsr = fsr;
3276             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3277         }
3278     }
3279
3280     if (is_a64(env)) {
3281         format64 = true;
3282     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3283         /*
3284          * ATS1Cxx:
3285          * * TTBCR.EAE determines whether the result is returned using the
3286          *   32-bit or the 64-bit PAR format
3287          * * Instructions executed in Hyp mode always use the 64bit format
3288          *
3289          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3290          * * The Non-secure TTBCR.EAE bit is set to 1
3291          * * The implementation includes EL2, and the value of HCR.VM is 1
3292          *
3293          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3294          *
3295          * ATS1Hx always uses the 64bit format.
3296          */
3297         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3298
3299         if (arm_feature(env, ARM_FEATURE_EL2)) {
3300             if (mmu_idx == ARMMMUIdx_E10_0 ||
3301                 mmu_idx == ARMMMUIdx_E10_1 ||
3302                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3303                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3304             } else {
3305                 format64 |= arm_current_el(env) == 2;
3306             }
3307         }
3308     }
3309
3310     if (format64) {
3311         /* Create a 64-bit PAR */
3312         par64 = (1 << 11); /* LPAE bit always set */
3313         if (!ret) {
3314             par64 |= res.phys & ~0xfffULL;
3315             if (!res.attrs.secure) {
3316                 par64 |= (1 << 9); /* NS */
3317             }
3318             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3319             par64 |= res.cacheattrs.shareability << 7; /* SH */
3320         } else {
3321             uint32_t fsr = arm_fi_to_lfsc(&fi);
3322
3323             par64 |= 1; /* F */
3324             par64 |= (fsr & 0x3f) << 1; /* FS */
3325             if (fi.stage2) {
3326                 par64 |= (1 << 9); /* S */
3327             }
3328             if (fi.s1ptw) {
3329                 par64 |= (1 << 8); /* PTW */
3330             }
3331         }
3332     } else {
3333         /* fsr is a DFSR/IFSR value for the short descriptor
3334          * translation table format (with WnR always clear).
3335          * Convert it to a 32-bit PAR.
3336          */
3337         if (!ret) {
3338             /* We do not set any attribute bits in the PAR */
3339             if (res.page_size == (1 << 24)
3340                 && arm_feature(env, ARM_FEATURE_V7)) {
3341                 par64 = (res.phys & 0xff000000) | (1 << 1);
3342             } else {
3343                 par64 = res.phys & 0xfffff000;
3344             }
3345             if (!res.attrs.secure) {
3346                 par64 |= (1 << 9); /* NS */
3347             }
3348         } else {
3349             uint32_t fsr = arm_fi_to_sfsc(&fi);
3350
3351             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3352                     ((fsr & 0xf) << 1) | 1;
3353         }
3354     }
3355     return par64;
3356 }
3357 #endif /* CONFIG_TCG */
3358
3359 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3360 {
3361 #ifdef CONFIG_TCG
3362     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3363     uint64_t par64;
3364     ARMMMUIdx mmu_idx;
3365     int el = arm_current_el(env);
3366     bool secure = arm_is_secure_below_el3(env);
3367
3368     switch (ri->opc2 & 6) {
3369     case 0:
3370         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3371         switch (el) {
3372         case 3:
3373             mmu_idx = ARMMMUIdx_SE3;
3374             break;
3375         case 2:
3376             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3377             /* fall through */
3378         case 1:
3379             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3380                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3381                            : ARMMMUIdx_Stage1_E1_PAN);
3382             } else {
3383                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3384             }
3385             break;
3386         default:
3387             g_assert_not_reached();
3388         }
3389         break;
3390     case 2:
3391         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3392         switch (el) {
3393         case 3:
3394             mmu_idx = ARMMMUIdx_SE10_0;
3395             break;
3396         case 2:
3397             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3398             mmu_idx = ARMMMUIdx_Stage1_E0;
3399             break;
3400         case 1:
3401             mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3402             break;
3403         default:
3404             g_assert_not_reached();
3405         }
3406         break;
3407     case 4:
3408         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3409         mmu_idx = ARMMMUIdx_E10_1;
3410         break;
3411     case 6:
3412         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3413         mmu_idx = ARMMMUIdx_E10_0;
3414         break;
3415     default:
3416         g_assert_not_reached();
3417     }
3418
3419     par64 = do_ats_write(env, value, access_type, mmu_idx);
3420
3421     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3422 #else
3423     /* Handled by hardware accelerator. */
3424     g_assert_not_reached();
3425 #endif /* CONFIG_TCG */
3426 }
3427
3428 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3429                         uint64_t value)
3430 {
3431 #ifdef CONFIG_TCG
3432     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3433     uint64_t par64;
3434
3435     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3436
3437     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3438 #else
3439     /* Handled by hardware accelerator. */
3440     g_assert_not_reached();
3441 #endif /* CONFIG_TCG */
3442 }
3443
3444 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3445                                      bool isread)
3446 {
3447     if (arm_current_el(env) == 3 &&
3448         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3449         return CP_ACCESS_TRAP;
3450     }
3451     return CP_ACCESS_OK;
3452 }
3453
3454 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3455                         uint64_t value)
3456 {
3457 #ifdef CONFIG_TCG
3458     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3459     ARMMMUIdx mmu_idx;
3460     int secure = arm_is_secure_below_el3(env);
3461
3462     switch (ri->opc2 & 6) {
3463     case 0:
3464         switch (ri->opc1) {
3465         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3466             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3467                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3468                            : ARMMMUIdx_Stage1_E1_PAN);
3469             } else {
3470                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3471             }
3472             break;
3473         case 4: /* AT S1E2R, AT S1E2W */
3474             mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3475             break;
3476         case 6: /* AT S1E3R, AT S1E3W */
3477             mmu_idx = ARMMMUIdx_SE3;
3478             break;
3479         default:
3480             g_assert_not_reached();
3481         }
3482         break;
3483     case 2: /* AT S1E0R, AT S1E0W */
3484         mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3485         break;
3486     case 4: /* AT S12E1R, AT S12E1W */
3487         mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3488         break;
3489     case 6: /* AT S12E0R, AT S12E0W */
3490         mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3491         break;
3492     default:
3493         g_assert_not_reached();
3494     }
3495
3496     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3497 #else
3498     /* Handled by hardware accelerator. */
3499     g_assert_not_reached();
3500 #endif /* CONFIG_TCG */
3501 }
3502 #endif
3503
3504 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3505     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3506       .access = PL1_RW, .resetvalue = 0,
3507       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3508                              offsetoflow32(CPUARMState, cp15.par_ns) },
3509       .writefn = par_write },
3510 #ifndef CONFIG_USER_ONLY
3511     /* This underdecoding is safe because the reginfo is NO_RAW. */
3512     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3513       .access = PL1_W, .accessfn = ats_access,
3514       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3515 #endif
3516 };
3517
3518 /* Return basic MPU access permission bits.  */
3519 static uint32_t simple_mpu_ap_bits(uint32_t val)
3520 {
3521     uint32_t ret;
3522     uint32_t mask;
3523     int i;
3524     ret = 0;
3525     mask = 3;
3526     for (i = 0; i < 16; i += 2) {
3527         ret |= (val >> i) & mask;
3528         mask <<= 2;
3529     }
3530     return ret;
3531 }
3532
3533 /* Pad basic MPU access permission bits to extended format.  */
3534 static uint32_t extended_mpu_ap_bits(uint32_t val)
3535 {
3536     uint32_t ret;
3537     uint32_t mask;
3538     int i;
3539     ret = 0;
3540     mask = 3;
3541     for (i = 0; i < 16; i += 2) {
3542         ret |= (val & mask) << i;
3543         mask <<= 2;
3544     }
3545     return ret;
3546 }
3547
3548 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3549                                  uint64_t value)
3550 {
3551     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3552 }
3553
3554 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3555 {
3556     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3557 }
3558
3559 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3560                                  uint64_t value)
3561 {
3562     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3563 }
3564
3565 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3566 {
3567     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3568 }
3569
3570 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3571 {
3572     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3573
3574     if (!u32p) {
3575         return 0;
3576     }
3577
3578     u32p += env->pmsav7.rnr[M_REG_NS];
3579     return *u32p;
3580 }
3581
3582 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3583                          uint64_t value)
3584 {
3585     ARMCPU *cpu = env_archcpu(env);
3586     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3587
3588     if (!u32p) {
3589         return;
3590     }
3591
3592     u32p += env->pmsav7.rnr[M_REG_NS];
3593     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3594     *u32p = value;
3595 }
3596
3597 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3598                               uint64_t value)
3599 {
3600     ARMCPU *cpu = env_archcpu(env);
3601     uint32_t nrgs = cpu->pmsav7_dregion;
3602
3603     if (value >= nrgs) {
3604         qemu_log_mask(LOG_GUEST_ERROR,
3605                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3606                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3607         return;
3608     }
3609
3610     raw_write(env, ri, value);
3611 }
3612
3613 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3614     /* Reset for all these registers is handled in arm_cpu_reset(),
3615      * because the PMSAv7 is also used by M-profile CPUs, which do
3616      * not register cpregs but still need the state to be reset.
3617      */
3618     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3619       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3620       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3621       .readfn = pmsav7_read, .writefn = pmsav7_write,
3622       .resetfn = arm_cp_reset_ignore },
3623     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3624       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3625       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3626       .readfn = pmsav7_read, .writefn = pmsav7_write,
3627       .resetfn = arm_cp_reset_ignore },
3628     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3629       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3630       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3631       .readfn = pmsav7_read, .writefn = pmsav7_write,
3632       .resetfn = arm_cp_reset_ignore },
3633     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3634       .access = PL1_RW,
3635       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3636       .writefn = pmsav7_rgnr_write,
3637       .resetfn = arm_cp_reset_ignore },
3638 };
3639
3640 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3641     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3642       .access = PL1_RW, .type = ARM_CP_ALIAS,
3643       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3644       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3645     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3646       .access = PL1_RW, .type = ARM_CP_ALIAS,
3647       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3648       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3649     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3650       .access = PL1_RW,
3651       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3652       .resetvalue = 0, },
3653     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3654       .access = PL1_RW,
3655       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3656       .resetvalue = 0, },
3657     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3658       .access = PL1_RW,
3659       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3660     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3661       .access = PL1_RW,
3662       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3663     /* Protection region base and size registers */
3664     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3665       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3666       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3667     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3668       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3669       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3670     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3671       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3672       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3673     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3674       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3675       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3676     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3677       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3678       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3679     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3680       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3681       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3682     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3683       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3684       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3685     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3686       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3687       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3688 };
3689
3690 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3691                              uint64_t value)
3692 {
3693     ARMCPU *cpu = env_archcpu(env);
3694
3695     if (!arm_feature(env, ARM_FEATURE_V8)) {
3696         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3697             /*
3698              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3699              * using Long-descriptor translation table format
3700              */
3701             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3702         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3703             /*
3704              * In an implementation that includes the Security Extensions
3705              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3706              * Short-descriptor translation table format.
3707              */
3708             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3709         } else {
3710             value &= TTBCR_N;
3711         }
3712     }
3713
3714     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3715         /* With LPAE the TTBCR could result in a change of ASID
3716          * via the TTBCR.A1 bit, so do a TLB flush.
3717          */
3718         tlb_flush(CPU(cpu));
3719     }
3720     raw_write(env, ri, value);
3721 }
3722
3723 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3724                                uint64_t value)
3725 {
3726     ARMCPU *cpu = env_archcpu(env);
3727
3728     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3729     tlb_flush(CPU(cpu));
3730     raw_write(env, ri, value);
3731 }
3732
3733 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3734                             uint64_t value)
3735 {
3736     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3737     if (cpreg_field_is_64bit(ri) &&
3738         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3739         ARMCPU *cpu = env_archcpu(env);
3740         tlb_flush(CPU(cpu));
3741     }
3742     raw_write(env, ri, value);
3743 }
3744
3745 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3746                                     uint64_t value)
3747 {
3748     /*
3749      * If we are running with E2&0 regime, then an ASID is active.
3750      * Flush if that might be changing.  Note we're not checking
3751      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3752      * holds the active ASID, only checking the field that might.
3753      */
3754     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3755         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3756         uint16_t mask = ARMMMUIdxBit_E20_2 |
3757                         ARMMMUIdxBit_E20_2_PAN |
3758                         ARMMMUIdxBit_E20_0;
3759
3760         if (arm_is_secure_below_el3(env)) {
3761             mask >>= ARM_MMU_IDX_A_NS;
3762         }
3763
3764         tlb_flush_by_mmuidx(env_cpu(env), mask);
3765     }
3766     raw_write(env, ri, value);
3767 }
3768
3769 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3770                         uint64_t value)
3771 {
3772     ARMCPU *cpu = env_archcpu(env);
3773     CPUState *cs = CPU(cpu);
3774
3775     /*
3776      * A change in VMID to the stage2 page table (Stage2) invalidates
3777      * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3778      */
3779     if (raw_read(env, ri) != value) {
3780         uint16_t mask = ARMMMUIdxBit_E10_1 |
3781                         ARMMMUIdxBit_E10_1_PAN |
3782                         ARMMMUIdxBit_E10_0;
3783
3784         if (arm_is_secure_below_el3(env)) {
3785             mask >>= ARM_MMU_IDX_A_NS;
3786         }
3787
3788         tlb_flush_by_mmuidx(cs, mask);
3789         raw_write(env, ri, value);
3790     }
3791 }
3792
3793 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3794     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3795       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3796       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3797                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3798     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3799       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3800       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3801                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3802     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3803       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3804       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3805                              offsetof(CPUARMState, cp15.dfar_ns) } },
3806     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3807       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3808       .access = PL1_RW, .accessfn = access_tvm_trvm,
3809       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3810       .resetvalue = 0, },
3811 };
3812
3813 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3814     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3815       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3816       .access = PL1_RW, .accessfn = access_tvm_trvm,
3817       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3818     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3819       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3820       .access = PL1_RW, .accessfn = access_tvm_trvm,
3821       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3822       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3823                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3824     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3825       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3826       .access = PL1_RW, .accessfn = access_tvm_trvm,
3827       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3828       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3829                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3830     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3831       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3832       .access = PL1_RW, .accessfn = access_tvm_trvm,
3833       .writefn = vmsa_tcr_el12_write,
3834       .raw_writefn = raw_write,
3835       .resetvalue = 0,
3836       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3837     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3838       .access = PL1_RW, .accessfn = access_tvm_trvm,
3839       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3840       .raw_writefn = raw_write,
3841       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3842                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3843 };
3844
3845 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3846  * qemu tlbs nor adjusting cached masks.
3847  */
3848 static const ARMCPRegInfo ttbcr2_reginfo = {
3849     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3850     .access = PL1_RW, .accessfn = access_tvm_trvm,
3851     .type = ARM_CP_ALIAS,
3852     .bank_fieldoffsets = {
3853         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3854         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
3855     },
3856 };
3857
3858 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3859                                 uint64_t value)
3860 {
3861     env->cp15.c15_ticonfig = value & 0xe7;
3862     /* The OS_TYPE bit in this register changes the reported CPUID! */
3863     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3864         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3865 }
3866
3867 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3868                                 uint64_t value)
3869 {
3870     env->cp15.c15_threadid = value & 0xffff;
3871 }
3872
3873 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3874                            uint64_t value)
3875 {
3876     /* Wait-for-interrupt (deprecated) */
3877     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3878 }
3879
3880 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3881                                   uint64_t value)
3882 {
3883     /* On OMAP there are registers indicating the max/min index of dcache lines
3884      * containing a dirty line; cache flush operations have to reset these.
3885      */
3886     env->cp15.c15_i_max = 0x000;
3887     env->cp15.c15_i_min = 0xff0;
3888 }
3889
3890 static const ARMCPRegInfo omap_cp_reginfo[] = {
3891     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3892       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3893       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3894       .resetvalue = 0, },
3895     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3896       .access = PL1_RW, .type = ARM_CP_NOP },
3897     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3898       .access = PL1_RW,
3899       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3900       .writefn = omap_ticonfig_write },
3901     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3902       .access = PL1_RW,
3903       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3904     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3905       .access = PL1_RW, .resetvalue = 0xff0,
3906       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3907     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3908       .access = PL1_RW,
3909       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3910       .writefn = omap_threadid_write },
3911     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3912       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3913       .type = ARM_CP_NO_RAW,
3914       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3915     /* TODO: Peripheral port remap register:
3916      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3917      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3918      * when MMU is off.
3919      */
3920     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3921       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3922       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3923       .writefn = omap_cachemaint_write },
3924     { .name = "C9", .cp = 15, .crn = 9,
3925       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3926       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3927 };
3928
3929 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3930                               uint64_t value)
3931 {
3932     env->cp15.c15_cpar = value & 0x3fff;
3933 }
3934
3935 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3936     { .name = "XSCALE_CPAR",
3937       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3938       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3939       .writefn = xscale_cpar_write, },
3940     { .name = "XSCALE_AUXCR",
3941       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3942       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3943       .resetvalue = 0, },
3944     /* XScale specific cache-lockdown: since we have no cache we NOP these
3945      * and hope the guest does not really rely on cache behaviour.
3946      */
3947     { .name = "XSCALE_LOCK_ICACHE_LINE",
3948       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3949       .access = PL1_W, .type = ARM_CP_NOP },
3950     { .name = "XSCALE_UNLOCK_ICACHE",
3951       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3952       .access = PL1_W, .type = ARM_CP_NOP },
3953     { .name = "XSCALE_DCACHE_LOCK",
3954       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3955       .access = PL1_RW, .type = ARM_CP_NOP },
3956     { .name = "XSCALE_UNLOCK_DCACHE",
3957       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3958       .access = PL1_W, .type = ARM_CP_NOP },
3959 };
3960
3961 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3962     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3963      * implementation of this implementation-defined space.
3964      * Ideally this should eventually disappear in favour of actually
3965      * implementing the correct behaviour for all cores.
3966      */
3967     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3968       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3969       .access = PL1_RW,
3970       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3971       .resetvalue = 0 },
3972 };
3973
3974 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3975     /* Cache status: RAZ because we have no cache so it's always clean */
3976     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3977       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3978       .resetvalue = 0 },
3979 };
3980
3981 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3982     /* We never have a block transfer operation in progress */
3983     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3984       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3985       .resetvalue = 0 },
3986     /* The cache ops themselves: these all NOP for QEMU */
3987     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3988       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3989     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3990       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3991     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3992       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3993     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3994       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3995     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3996       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3997     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3998       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3999 };
4000
4001 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4002     /* The cache test-and-clean instructions always return (1 << 30)
4003      * to indicate that there are no dirty cache lines.
4004      */
4005     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4006       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4007       .resetvalue = (1 << 30) },
4008     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4009       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4010       .resetvalue = (1 << 30) },
4011 };
4012
4013 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4014     /* Ignore ReadBuffer accesses */
4015     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4016       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4017       .access = PL1_RW, .resetvalue = 0,
4018       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4019 };
4020
4021 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4022 {
4023     unsigned int cur_el = arm_current_el(env);
4024
4025     if (arm_is_el2_enabled(env) && cur_el == 1) {
4026         return env->cp15.vpidr_el2;
4027     }
4028     return raw_read(env, ri);
4029 }
4030
4031 static uint64_t mpidr_read_val(CPUARMState *env)
4032 {
4033     ARMCPU *cpu = env_archcpu(env);
4034     uint64_t mpidr = cpu->mp_affinity;
4035
4036     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4037         mpidr |= (1U << 31);
4038         /* Cores which are uniprocessor (non-coherent)
4039          * but still implement the MP extensions set
4040          * bit 30. (For instance, Cortex-R5).
4041          */
4042         if (cpu->mp_is_up) {
4043             mpidr |= (1u << 30);
4044         }
4045     }
4046     return mpidr;
4047 }
4048
4049 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4050 {
4051     unsigned int cur_el = arm_current_el(env);
4052
4053     if (arm_is_el2_enabled(env) && cur_el == 1) {
4054         return env->cp15.vmpidr_el2;
4055     }
4056     return mpidr_read_val(env);
4057 }
4058
4059 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4060     /* NOP AMAIR0/1 */
4061     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4062       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4063       .access = PL1_RW, .accessfn = access_tvm_trvm,
4064       .type = ARM_CP_CONST, .resetvalue = 0 },
4065     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4066     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4067       .access = PL1_RW, .accessfn = access_tvm_trvm,
4068       .type = ARM_CP_CONST, .resetvalue = 0 },
4069     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4070       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4071       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4072                              offsetof(CPUARMState, cp15.par_ns)} },
4073     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4074       .access = PL1_RW, .accessfn = access_tvm_trvm,
4075       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4076       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4077                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4078       .writefn = vmsa_ttbr_write, },
4079     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4080       .access = PL1_RW, .accessfn = access_tvm_trvm,
4081       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4082       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4083                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4084       .writefn = vmsa_ttbr_write, },
4085 };
4086
4087 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4088 {
4089     return vfp_get_fpcr(env);
4090 }
4091
4092 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4093                             uint64_t value)
4094 {
4095     vfp_set_fpcr(env, value);
4096 }
4097
4098 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4099 {
4100     return vfp_get_fpsr(env);
4101 }
4102
4103 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4104                             uint64_t value)
4105 {
4106     vfp_set_fpsr(env, value);
4107 }
4108
4109 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4110                                        bool isread)
4111 {
4112     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4113         return CP_ACCESS_TRAP;
4114     }
4115     return CP_ACCESS_OK;
4116 }
4117
4118 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4119                             uint64_t value)
4120 {
4121     env->daif = value & PSTATE_DAIF;
4122 }
4123
4124 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4125 {
4126     return env->pstate & PSTATE_PAN;
4127 }
4128
4129 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4130                            uint64_t value)
4131 {
4132     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4133 }
4134
4135 static const ARMCPRegInfo pan_reginfo = {
4136     .name = "PAN", .state = ARM_CP_STATE_AA64,
4137     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4138     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4139     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4140 };
4141
4142 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4143 {
4144     return env->pstate & PSTATE_UAO;
4145 }
4146
4147 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4148                            uint64_t value)
4149 {
4150     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4151 }
4152
4153 static const ARMCPRegInfo uao_reginfo = {
4154     .name = "UAO", .state = ARM_CP_STATE_AA64,
4155     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4156     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4157     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4158 };
4159
4160 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4161 {
4162     return env->pstate & PSTATE_DIT;
4163 }
4164
4165 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4166                            uint64_t value)
4167 {
4168     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4169 }
4170
4171 static const ARMCPRegInfo dit_reginfo = {
4172     .name = "DIT", .state = ARM_CP_STATE_AA64,
4173     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4174     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4175     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4176 };
4177
4178 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4179 {
4180     return env->pstate & PSTATE_SSBS;
4181 }
4182
4183 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4184                            uint64_t value)
4185 {
4186     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4187 }
4188
4189 static const ARMCPRegInfo ssbs_reginfo = {
4190     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4191     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4192     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4193     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4194 };
4195
4196 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4197                                               const ARMCPRegInfo *ri,
4198                                               bool isread)
4199 {
4200     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4201     switch (arm_current_el(env)) {
4202     case 0:
4203         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4204         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4205             return CP_ACCESS_TRAP;
4206         }
4207         /* fall through */
4208     case 1:
4209         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4210         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4211             return CP_ACCESS_TRAP_EL2;
4212         }
4213         break;
4214     }
4215     return CP_ACCESS_OK;
4216 }
4217
4218 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4219                                               const ARMCPRegInfo *ri,
4220                                               bool isread)
4221 {
4222     /* Cache invalidate/clean to Point of Unification... */
4223     switch (arm_current_el(env)) {
4224     case 0:
4225         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4226         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4227             return CP_ACCESS_TRAP;
4228         }
4229         /* fall through */
4230     case 1:
4231         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4232         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4233             return CP_ACCESS_TRAP_EL2;
4234         }
4235         break;
4236     }
4237     return CP_ACCESS_OK;
4238 }
4239
4240 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4241  * Page D4-1736 (DDI0487A.b)
4242  */
4243
4244 static int vae1_tlbmask(CPUARMState *env)
4245 {
4246     uint64_t hcr = arm_hcr_el2_eff(env);
4247     uint16_t mask;
4248
4249     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4250         mask = ARMMMUIdxBit_E20_2 |
4251                ARMMMUIdxBit_E20_2_PAN |
4252                ARMMMUIdxBit_E20_0;
4253     } else {
4254         mask = ARMMMUIdxBit_E10_1 |
4255                ARMMMUIdxBit_E10_1_PAN |
4256                ARMMMUIdxBit_E10_0;
4257     }
4258
4259     if (arm_is_secure_below_el3(env)) {
4260         mask >>= ARM_MMU_IDX_A_NS;
4261     }
4262
4263     return mask;
4264 }
4265
4266 /* Return 56 if TBI is enabled, 64 otherwise. */
4267 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4268                               uint64_t addr)
4269 {
4270     uint64_t tcr = regime_tcr(env, mmu_idx);
4271     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4272     int select = extract64(addr, 55, 1);
4273
4274     return (tbi >> select) & 1 ? 56 : 64;
4275 }
4276
4277 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4278 {
4279     uint64_t hcr = arm_hcr_el2_eff(env);
4280     ARMMMUIdx mmu_idx;
4281
4282     /* Only the regime of the mmu_idx below is significant. */
4283     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4284         mmu_idx = ARMMMUIdx_E20_0;
4285     } else {
4286         mmu_idx = ARMMMUIdx_E10_0;
4287     }
4288
4289     if (arm_is_secure_below_el3(env)) {
4290         mmu_idx &= ~ARM_MMU_IDX_A_NS;
4291     }
4292
4293     return tlbbits_for_regime(env, mmu_idx, addr);
4294 }
4295
4296 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4297                                       uint64_t value)
4298 {
4299     CPUState *cs = env_cpu(env);
4300     int mask = vae1_tlbmask(env);
4301
4302     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4303 }
4304
4305 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4306                                     uint64_t value)
4307 {
4308     CPUState *cs = env_cpu(env);
4309     int mask = vae1_tlbmask(env);
4310
4311     if (tlb_force_broadcast(env)) {
4312         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4313     } else {
4314         tlb_flush_by_mmuidx(cs, mask);
4315     }
4316 }
4317
4318 static int alle1_tlbmask(CPUARMState *env)
4319 {
4320     /*
4321      * Note that the 'ALL' scope must invalidate both stage 1 and
4322      * stage 2 translations, whereas most other scopes only invalidate
4323      * stage 1 translations.
4324      */
4325     if (arm_is_secure_below_el3(env)) {
4326         return ARMMMUIdxBit_SE10_1 |
4327                ARMMMUIdxBit_SE10_1_PAN |
4328                ARMMMUIdxBit_SE10_0;
4329     } else {
4330         return ARMMMUIdxBit_E10_1 |
4331                ARMMMUIdxBit_E10_1_PAN |
4332                ARMMMUIdxBit_E10_0;
4333     }
4334 }
4335
4336 static int e2_tlbmask(CPUARMState *env)
4337 {
4338     if (arm_is_secure_below_el3(env)) {
4339         return ARMMMUIdxBit_SE20_0 |
4340                ARMMMUIdxBit_SE20_2 |
4341                ARMMMUIdxBit_SE20_2_PAN |
4342                ARMMMUIdxBit_SE2;
4343     } else {
4344         return ARMMMUIdxBit_E20_0 |
4345                ARMMMUIdxBit_E20_2 |
4346                ARMMMUIdxBit_E20_2_PAN |
4347                ARMMMUIdxBit_E2;
4348     }
4349 }
4350
4351 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4352                                   uint64_t value)
4353 {
4354     CPUState *cs = env_cpu(env);
4355     int mask = alle1_tlbmask(env);
4356
4357     tlb_flush_by_mmuidx(cs, mask);
4358 }
4359
4360 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4361                                   uint64_t value)
4362 {
4363     CPUState *cs = env_cpu(env);
4364     int mask = e2_tlbmask(env);
4365
4366     tlb_flush_by_mmuidx(cs, mask);
4367 }
4368
4369 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4370                                   uint64_t value)
4371 {
4372     ARMCPU *cpu = env_archcpu(env);
4373     CPUState *cs = CPU(cpu);
4374
4375     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4376 }
4377
4378 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4379                                     uint64_t value)
4380 {
4381     CPUState *cs = env_cpu(env);
4382     int mask = alle1_tlbmask(env);
4383
4384     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4385 }
4386
4387 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4388                                     uint64_t value)
4389 {
4390     CPUState *cs = env_cpu(env);
4391     int mask = e2_tlbmask(env);
4392
4393     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4394 }
4395
4396 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4397                                     uint64_t value)
4398 {
4399     CPUState *cs = env_cpu(env);
4400
4401     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4402 }
4403
4404 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4405                                  uint64_t value)
4406 {
4407     /* Invalidate by VA, EL2
4408      * Currently handles both VAE2 and VALE2, since we don't support
4409      * flush-last-level-only.
4410      */
4411     CPUState *cs = env_cpu(env);
4412     int mask = e2_tlbmask(env);
4413     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4414
4415     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4416 }
4417
4418 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4419                                  uint64_t value)
4420 {
4421     /* Invalidate by VA, EL3
4422      * Currently handles both VAE3 and VALE3, since we don't support
4423      * flush-last-level-only.
4424      */
4425     ARMCPU *cpu = env_archcpu(env);
4426     CPUState *cs = CPU(cpu);
4427     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4428
4429     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4430 }
4431
4432 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4433                                    uint64_t value)
4434 {
4435     CPUState *cs = env_cpu(env);
4436     int mask = vae1_tlbmask(env);
4437     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4438     int bits = vae1_tlbbits(env, pageaddr);
4439
4440     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4441 }
4442
4443 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4444                                  uint64_t value)
4445 {
4446     /* Invalidate by VA, EL1&0 (AArch64 version).
4447      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4448      * since we don't support flush-for-specific-ASID-only or
4449      * flush-last-level-only.
4450      */
4451     CPUState *cs = env_cpu(env);
4452     int mask = vae1_tlbmask(env);
4453     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4454     int bits = vae1_tlbbits(env, pageaddr);
4455
4456     if (tlb_force_broadcast(env)) {
4457         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4458     } else {
4459         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4460     }
4461 }
4462
4463 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4464                                    uint64_t value)
4465 {
4466     CPUState *cs = env_cpu(env);
4467     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4468     bool secure = arm_is_secure_below_el3(env);
4469     int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4470     int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4471                                   pageaddr);
4472
4473     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4474 }
4475
4476 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4477                                    uint64_t value)
4478 {
4479     CPUState *cs = env_cpu(env);
4480     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4481     int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4482
4483     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4484                                                   ARMMMUIdxBit_SE3, bits);
4485 }
4486
4487 #ifdef TARGET_AARCH64
4488 typedef struct {
4489     uint64_t base;
4490     uint64_t length;
4491 } TLBIRange;
4492
4493 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4494                                      uint64_t value)
4495 {
4496     unsigned int page_size_granule, page_shift, num, scale, exponent;
4497     /* Extract one bit to represent the va selector in use. */
4498     uint64_t select = sextract64(value, 36, 1);
4499     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4500     TLBIRange ret = { };
4501
4502     page_size_granule = extract64(value, 46, 2);
4503
4504     /* The granule encoded in value must match the granule in use. */
4505     if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4506         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4507                       page_size_granule);
4508         return ret;
4509     }
4510
4511     page_shift = (page_size_granule - 1) * 2 + 12;
4512     num = extract64(value, 39, 5);
4513     scale = extract64(value, 44, 2);
4514     exponent = (5 * scale) + 1;
4515
4516     ret.length = (num + 1) << (exponent + page_shift);
4517
4518     if (param.select) {
4519         ret.base = sextract64(value, 0, 37);
4520     } else {
4521         ret.base = extract64(value, 0, 37);
4522     }
4523     if (param.ds) {
4524         /*
4525          * With DS=1, BaseADDR is always shifted 16 so that it is able
4526          * to address all 52 va bits.  The input address is perforce
4527          * aligned on a 64k boundary regardless of translation granule.
4528          */
4529         page_shift = 16;
4530     }
4531     ret.base <<= page_shift;
4532
4533     return ret;
4534 }
4535
4536 static void do_rvae_write(CPUARMState *env, uint64_t value,
4537                           int idxmap, bool synced)
4538 {
4539     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4540     TLBIRange range;
4541     int bits;
4542
4543     range = tlbi_aa64_get_range(env, one_idx, value);
4544     bits = tlbbits_for_regime(env, one_idx, range.base);
4545
4546     if (synced) {
4547         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4548                                                   range.base,
4549                                                   range.length,
4550                                                   idxmap,
4551                                                   bits);
4552     } else {
4553         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4554                                   range.length, idxmap, bits);
4555     }
4556 }
4557
4558 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4559                                   const ARMCPRegInfo *ri,
4560                                   uint64_t value)
4561 {
4562     /*
4563      * Invalidate by VA range, EL1&0.
4564      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4565      * since we don't support flush-for-specific-ASID-only or
4566      * flush-last-level-only.
4567      */
4568
4569     do_rvae_write(env, value, vae1_tlbmask(env),
4570                   tlb_force_broadcast(env));
4571 }
4572
4573 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4574                                     const ARMCPRegInfo *ri,
4575                                     uint64_t value)
4576 {
4577     /*
4578      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4579      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4580      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4581      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4582      * shareable specific flushes.
4583      */
4584
4585     do_rvae_write(env, value, vae1_tlbmask(env), true);
4586 }
4587
4588 static int vae2_tlbmask(CPUARMState *env)
4589 {
4590     return (arm_is_secure_below_el3(env)
4591             ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4592 }
4593
4594 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4595                                   const ARMCPRegInfo *ri,
4596                                   uint64_t value)
4597 {
4598     /*
4599      * Invalidate by VA range, EL2.
4600      * Currently handles all of RVAE2 and RVALE2,
4601      * since we don't support flush-for-specific-ASID-only or
4602      * flush-last-level-only.
4603      */
4604
4605     do_rvae_write(env, value, vae2_tlbmask(env),
4606                   tlb_force_broadcast(env));
4607
4608
4609 }
4610
4611 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4612                                     const ARMCPRegInfo *ri,
4613                                     uint64_t value)
4614 {
4615     /*
4616      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4617      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4618      * since we don't support flush-for-specific-ASID-only,
4619      * flush-last-level-only or inner/outer shareable specific flushes.
4620      */
4621
4622     do_rvae_write(env, value, vae2_tlbmask(env), true);
4623
4624 }
4625
4626 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4627                                   const ARMCPRegInfo *ri,
4628                                   uint64_t value)
4629 {
4630     /*
4631      * Invalidate by VA range, EL3.
4632      * Currently handles all of RVAE3 and RVALE3,
4633      * since we don't support flush-for-specific-ASID-only or
4634      * flush-last-level-only.
4635      */
4636
4637     do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4638                   tlb_force_broadcast(env));
4639 }
4640
4641 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4642                                     const ARMCPRegInfo *ri,
4643                                     uint64_t value)
4644 {
4645     /*
4646      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4647      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4648      * since we don't support flush-for-specific-ASID-only,
4649      * flush-last-level-only or inner/outer specific flushes.
4650      */
4651
4652     do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4653 }
4654 #endif
4655
4656 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4657                                       bool isread)
4658 {
4659     int cur_el = arm_current_el(env);
4660
4661     if (cur_el < 2) {
4662         uint64_t hcr = arm_hcr_el2_eff(env);
4663
4664         if (cur_el == 0) {
4665             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4666                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4667                     return CP_ACCESS_TRAP_EL2;
4668                 }
4669             } else {
4670                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4671                     return CP_ACCESS_TRAP;
4672                 }
4673                 if (hcr & HCR_TDZ) {
4674                     return CP_ACCESS_TRAP_EL2;
4675                 }
4676             }
4677         } else if (hcr & HCR_TDZ) {
4678             return CP_ACCESS_TRAP_EL2;
4679         }
4680     }
4681     return CP_ACCESS_OK;
4682 }
4683
4684 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4685 {
4686     ARMCPU *cpu = env_archcpu(env);
4687     int dzp_bit = 1 << 4;
4688
4689     /* DZP indicates whether DC ZVA access is allowed */
4690     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4691         dzp_bit = 0;
4692     }
4693     return cpu->dcz_blocksize | dzp_bit;
4694 }
4695
4696 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4697                                     bool isread)
4698 {
4699     if (!(env->pstate & PSTATE_SP)) {
4700         /* Access to SP_EL0 is undefined if it's being used as
4701          * the stack pointer.
4702          */
4703         return CP_ACCESS_TRAP_UNCATEGORIZED;
4704     }
4705     return CP_ACCESS_OK;
4706 }
4707
4708 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4709 {
4710     return env->pstate & PSTATE_SP;
4711 }
4712
4713 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4714 {
4715     update_spsel(env, val);
4716 }
4717
4718 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4719                         uint64_t value)
4720 {
4721     ARMCPU *cpu = env_archcpu(env);
4722
4723     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4724         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4725         value &= ~SCTLR_M;
4726     }
4727
4728     /* ??? Lots of these bits are not implemented.  */
4729
4730     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4731         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4732             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4733         } else {
4734             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4735                        SCTLR_ATA0 | SCTLR_ATA);
4736         }
4737     }
4738
4739     if (raw_read(env, ri) == value) {
4740         /* Skip the TLB flush if nothing actually changed; Linux likes
4741          * to do a lot of pointless SCTLR writes.
4742          */
4743         return;
4744     }
4745
4746     raw_write(env, ri, value);
4747
4748     /* This may enable/disable the MMU, so do a TLB flush.  */
4749     tlb_flush(CPU(cpu));
4750
4751     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4752         /*
4753          * Normally we would always end the TB on an SCTLR write; see the
4754          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4755          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4756          * of hflags from the translator, so do it here.
4757          */
4758         arm_rebuild_hflags(env);
4759     }
4760 }
4761
4762 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4763                            uint64_t value)
4764 {
4765     /*
4766      * Some MDCR_EL3 bits affect whether PMU counters are running:
4767      * if we are trying to change any of those then we must
4768      * bracket this update with PMU start/finish calls.
4769      */
4770     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4771
4772     if (pmu_op) {
4773         pmu_op_start(env);
4774     }
4775     env->cp15.mdcr_el3 = value;
4776     if (pmu_op) {
4777         pmu_op_finish(env);
4778     }
4779 }
4780
4781 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4782                        uint64_t value)
4783 {
4784     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4785     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4786 }
4787
4788 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4789                            uint64_t value)
4790 {
4791     /*
4792      * Some MDCR_EL2 bits affect whether PMU counters are running:
4793      * if we are trying to change any of those then we must
4794      * bracket this update with PMU start/finish calls.
4795      */
4796     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4797
4798     if (pmu_op) {
4799         pmu_op_start(env);
4800     }
4801     env->cp15.mdcr_el2 = value;
4802     if (pmu_op) {
4803         pmu_op_finish(env);
4804     }
4805 }
4806
4807 static const ARMCPRegInfo v8_cp_reginfo[] = {
4808     /* Minimal set of EL0-visible registers. This will need to be expanded
4809      * significantly for system emulation of AArch64 CPUs.
4810      */
4811     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4812       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4813       .access = PL0_RW, .type = ARM_CP_NZCV },
4814     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4815       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4816       .type = ARM_CP_NO_RAW,
4817       .access = PL0_RW, .accessfn = aa64_daif_access,
4818       .fieldoffset = offsetof(CPUARMState, daif),
4819       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4820     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4821       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4822       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4823       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4824     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4825       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4826       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4827       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4828     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4829       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4830       .access = PL0_R, .type = ARM_CP_NO_RAW,
4831       .readfn = aa64_dczid_read },
4832     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4833       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4834       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4835 #ifndef CONFIG_USER_ONLY
4836       /* Avoid overhead of an access check that always passes in user-mode */
4837       .accessfn = aa64_zva_access,
4838 #endif
4839     },
4840     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4841       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4842       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4843     /* Cache ops: all NOPs since we don't emulate caches */
4844     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4845       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4846       .access = PL1_W, .type = ARM_CP_NOP,
4847       .accessfn = aa64_cacheop_pou_access },
4848     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4849       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4850       .access = PL1_W, .type = ARM_CP_NOP,
4851       .accessfn = aa64_cacheop_pou_access },
4852     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4853       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4854       .access = PL0_W, .type = ARM_CP_NOP,
4855       .accessfn = aa64_cacheop_pou_access },
4856     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4857       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4858       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4859       .type = ARM_CP_NOP },
4860     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4861       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4862       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4863     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4864       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4865       .access = PL0_W, .type = ARM_CP_NOP,
4866       .accessfn = aa64_cacheop_poc_access },
4867     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4868       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4869       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4870     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4871       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4872       .access = PL0_W, .type = ARM_CP_NOP,
4873       .accessfn = aa64_cacheop_pou_access },
4874     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4875       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4876       .access = PL0_W, .type = ARM_CP_NOP,
4877       .accessfn = aa64_cacheop_poc_access },
4878     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4879       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4880       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4881     /* TLBI operations */
4882     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4883       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4884       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4885       .writefn = tlbi_aa64_vmalle1is_write },
4886     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4887       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4888       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4889       .writefn = tlbi_aa64_vae1is_write },
4890     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4891       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4892       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4893       .writefn = tlbi_aa64_vmalle1is_write },
4894     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4895       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4896       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4897       .writefn = tlbi_aa64_vae1is_write },
4898     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4899       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4900       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4901       .writefn = tlbi_aa64_vae1is_write },
4902     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4903       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4904       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4905       .writefn = tlbi_aa64_vae1is_write },
4906     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4907       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4908       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4909       .writefn = tlbi_aa64_vmalle1_write },
4910     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4911       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4912       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4913       .writefn = tlbi_aa64_vae1_write },
4914     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4915       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4916       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4917       .writefn = tlbi_aa64_vmalle1_write },
4918     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4919       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4920       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4921       .writefn = tlbi_aa64_vae1_write },
4922     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4923       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4924       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4925       .writefn = tlbi_aa64_vae1_write },
4926     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4927       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4928       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4929       .writefn = tlbi_aa64_vae1_write },
4930     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4931       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4932       .access = PL2_W, .type = ARM_CP_NOP },
4933     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4934       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4935       .access = PL2_W, .type = ARM_CP_NOP },
4936     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4937       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4938       .access = PL2_W, .type = ARM_CP_NO_RAW,
4939       .writefn = tlbi_aa64_alle1is_write },
4940     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4941       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4942       .access = PL2_W, .type = ARM_CP_NO_RAW,
4943       .writefn = tlbi_aa64_alle1is_write },
4944     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4945       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4946       .access = PL2_W, .type = ARM_CP_NOP },
4947     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4948       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4949       .access = PL2_W, .type = ARM_CP_NOP },
4950     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4951       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4952       .access = PL2_W, .type = ARM_CP_NO_RAW,
4953       .writefn = tlbi_aa64_alle1_write },
4954     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4955       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4956       .access = PL2_W, .type = ARM_CP_NO_RAW,
4957       .writefn = tlbi_aa64_alle1is_write },
4958 #ifndef CONFIG_USER_ONLY
4959     /* 64 bit address translation operations */
4960     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4961       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4962       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4963       .writefn = ats_write64 },
4964     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4965       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4966       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4967       .writefn = ats_write64 },
4968     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4969       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4970       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4971       .writefn = ats_write64 },
4972     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4973       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4974       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4975       .writefn = ats_write64 },
4976     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4977       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4978       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4979       .writefn = ats_write64 },
4980     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4981       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4982       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4983       .writefn = ats_write64 },
4984     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4985       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4986       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4987       .writefn = ats_write64 },
4988     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4989       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4990       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4991       .writefn = ats_write64 },
4992     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4993     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4994       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4995       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4996       .writefn = ats_write64 },
4997     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4998       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4999       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5000       .writefn = ats_write64 },
5001     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5002       .type = ARM_CP_ALIAS,
5003       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5004       .access = PL1_RW, .resetvalue = 0,
5005       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5006       .writefn = par_write },
5007 #endif
5008     /* TLB invalidate last level of translation table walk */
5009     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5010       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5011       .writefn = tlbimva_is_write },
5012     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5013       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5014       .writefn = tlbimvaa_is_write },
5015     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5016       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5017       .writefn = tlbimva_write },
5018     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5019       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5020       .writefn = tlbimvaa_write },
5021     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5022       .type = ARM_CP_NO_RAW, .access = PL2_W,
5023       .writefn = tlbimva_hyp_write },
5024     { .name = "TLBIMVALHIS",
5025       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5026       .type = ARM_CP_NO_RAW, .access = PL2_W,
5027       .writefn = tlbimva_hyp_is_write },
5028     { .name = "TLBIIPAS2",
5029       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5030       .type = ARM_CP_NOP, .access = PL2_W },
5031     { .name = "TLBIIPAS2IS",
5032       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5033       .type = ARM_CP_NOP, .access = PL2_W },
5034     { .name = "TLBIIPAS2L",
5035       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5036       .type = ARM_CP_NOP, .access = PL2_W },
5037     { .name = "TLBIIPAS2LIS",
5038       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5039       .type = ARM_CP_NOP, .access = PL2_W },
5040     /* 32 bit cache operations */
5041     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5042       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5043     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5044       .type = ARM_CP_NOP, .access = PL1_W },
5045     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5046       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5047     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5048       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5049     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5050       .type = ARM_CP_NOP, .access = PL1_W },
5051     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5052       .type = ARM_CP_NOP, .access = PL1_W },
5053     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5054       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5055     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5056       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5057     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5058       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5059     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5060       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5061     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5062       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5063     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5064       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5065     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5066       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5067     /* MMU Domain access control / MPU write buffer control */
5068     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5069       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5070       .writefn = dacr_write, .raw_writefn = raw_write,
5071       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5072                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5073     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5074       .type = ARM_CP_ALIAS,
5075       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5076       .access = PL1_RW,
5077       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5078     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5079       .type = ARM_CP_ALIAS,
5080       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5081       .access = PL1_RW,
5082       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5083     /* We rely on the access checks not allowing the guest to write to the
5084      * state field when SPSel indicates that it's being used as the stack
5085      * pointer.
5086      */
5087     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5088       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5089       .access = PL1_RW, .accessfn = sp_el0_access,
5090       .type = ARM_CP_ALIAS,
5091       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5092     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5093       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5094       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5095       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5096     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5097       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5098       .type = ARM_CP_NO_RAW,
5099       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5100     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5101       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5102       .access = PL2_RW,
5103       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5104       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5105     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5106       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5107       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5108       .writefn = dacr_write, .raw_writefn = raw_write,
5109       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5110     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5111       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5112       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5113       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5114     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5115       .type = ARM_CP_ALIAS,
5116       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5117       .access = PL2_RW,
5118       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5119     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5120       .type = ARM_CP_ALIAS,
5121       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5122       .access = PL2_RW,
5123       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5124     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5125       .type = ARM_CP_ALIAS,
5126       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5127       .access = PL2_RW,
5128       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5129     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5130       .type = ARM_CP_ALIAS,
5131       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5132       .access = PL2_RW,
5133       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5134     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5135       .type = ARM_CP_IO,
5136       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5137       .resetvalue = 0,
5138       .access = PL3_RW,
5139       .writefn = mdcr_el3_write,
5140       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5141     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5142       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5143       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5144       .writefn = sdcr_write,
5145       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5146 };
5147
5148 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5149 {
5150     ARMCPU *cpu = env_archcpu(env);
5151
5152     if (arm_feature(env, ARM_FEATURE_V8)) {
5153         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5154     } else {
5155         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5156     }
5157
5158     if (arm_feature(env, ARM_FEATURE_EL3)) {
5159         valid_mask &= ~HCR_HCD;
5160     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5161         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5162          * However, if we're using the SMC PSCI conduit then QEMU is
5163          * effectively acting like EL3 firmware and so the guest at
5164          * EL2 should retain the ability to prevent EL1 from being
5165          * able to make SMC calls into the ersatz firmware, so in
5166          * that case HCR.TSC should be read/write.
5167          */
5168         valid_mask &= ~HCR_TSC;
5169     }
5170
5171     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5172         if (cpu_isar_feature(aa64_vh, cpu)) {
5173             valid_mask |= HCR_E2H;
5174         }
5175         if (cpu_isar_feature(aa64_ras, cpu)) {
5176             valid_mask |= HCR_TERR | HCR_TEA;
5177         }
5178         if (cpu_isar_feature(aa64_lor, cpu)) {
5179             valid_mask |= HCR_TLOR;
5180         }
5181         if (cpu_isar_feature(aa64_pauth, cpu)) {
5182             valid_mask |= HCR_API | HCR_APK;
5183         }
5184         if (cpu_isar_feature(aa64_mte, cpu)) {
5185             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5186         }
5187         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5188             valid_mask |= HCR_ENSCXT;
5189         }
5190         if (cpu_isar_feature(aa64_fwb, cpu)) {
5191             valid_mask |= HCR_FWB;
5192         }
5193     }
5194
5195     /* Clear RES0 bits.  */
5196     value &= valid_mask;
5197
5198     /*
5199      * These bits change the MMU setup:
5200      * HCR_VM enables stage 2 translation
5201      * HCR_PTW forbids certain page-table setups
5202      * HCR_DC disables stage1 and enables stage2 translation
5203      * HCR_DCT enables tagging on (disabled) stage1 translation
5204      * HCR_FWB changes the interpretation of stage2 descriptor bits
5205      */
5206     if ((env->cp15.hcr_el2 ^ value) &
5207         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5208         tlb_flush(CPU(cpu));
5209     }
5210     env->cp15.hcr_el2 = value;
5211
5212     /*
5213      * Updates to VI and VF require us to update the status of
5214      * virtual interrupts, which are the logical OR of these bits
5215      * and the state of the input lines from the GIC. (This requires
5216      * that we have the iothread lock, which is done by marking the
5217      * reginfo structs as ARM_CP_IO.)
5218      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5219      * possible for it to be taken immediately, because VIRQ and
5220      * VFIQ are masked unless running at EL0 or EL1, and HCR
5221      * can only be written at EL2.
5222      */
5223     g_assert(qemu_mutex_iothread_locked());
5224     arm_cpu_update_virq(cpu);
5225     arm_cpu_update_vfiq(cpu);
5226     arm_cpu_update_vserr(cpu);
5227 }
5228
5229 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5230 {
5231     do_hcr_write(env, value, 0);
5232 }
5233
5234 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5235                           uint64_t value)
5236 {
5237     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5238     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5239     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5240 }
5241
5242 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5243                          uint64_t value)
5244 {
5245     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5246     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5247     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5248 }
5249
5250 /*
5251  * Return the effective value of HCR_EL2.
5252  * Bits that are not included here:
5253  * RW       (read from SCR_EL3.RW as needed)
5254  */
5255 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5256 {
5257     uint64_t ret = env->cp15.hcr_el2;
5258
5259     if (!arm_is_el2_enabled(env)) {
5260         /*
5261          * "This register has no effect if EL2 is not enabled in the
5262          * current Security state".  This is ARMv8.4-SecEL2 speak for
5263          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5264          *
5265          * Prior to that, the language was "In an implementation that
5266          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5267          * as if this field is 0 for all purposes other than a direct
5268          * read or write access of HCR_EL2".  With lots of enumeration
5269          * on a per-field basis.  In current QEMU, this is condition
5270          * is arm_is_secure_below_el3.
5271          *
5272          * Since the v8.4 language applies to the entire register, and
5273          * appears to be backward compatible, use that.
5274          */
5275         return 0;
5276     }
5277
5278     /*
5279      * For a cpu that supports both aarch64 and aarch32, we can set bits
5280      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5281      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5282      */
5283     if (!arm_el_is_aa64(env, 2)) {
5284         uint64_t aa32_valid;
5285
5286         /*
5287          * These bits are up-to-date as of ARMv8.6.
5288          * For HCR, it's easiest to list just the 2 bits that are invalid.
5289          * For HCR2, list those that are valid.
5290          */
5291         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5292         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5293                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5294         ret &= aa32_valid;
5295     }
5296
5297     if (ret & HCR_TGE) {
5298         /* These bits are up-to-date as of ARMv8.6.  */
5299         if (ret & HCR_E2H) {
5300             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5301                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5302                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5303                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5304                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5305                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5306         } else {
5307             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5308         }
5309         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5310                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5311                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5312                  HCR_TLOR);
5313     }
5314
5315     return ret;
5316 }
5317
5318 /*
5319  * Corresponds to ARM pseudocode function ELIsInHost().
5320  */
5321 bool el_is_in_host(CPUARMState *env, int el)
5322 {
5323     uint64_t mask;
5324
5325     /*
5326      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5327      * Perform the simplest bit tests first, and validate EL2 afterward.
5328      */
5329     if (el & 1) {
5330         return false; /* EL1 or EL3 */
5331     }
5332
5333     /*
5334      * Note that hcr_write() checks isar_feature_aa64_vh(),
5335      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5336      */
5337     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5338     if ((env->cp15.hcr_el2 & mask) != mask) {
5339         return false;
5340     }
5341
5342     /* TGE and/or E2H set: double check those bits are currently legal. */
5343     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5344 }
5345
5346 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5347                        uint64_t value)
5348 {
5349     uint64_t valid_mask = 0;
5350
5351     /* No features adding bits to HCRX are implemented. */
5352
5353     /* Clear RES0 bits.  */
5354     env->cp15.hcrx_el2 = value & valid_mask;
5355 }
5356
5357 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5358                                   bool isread)
5359 {
5360     if (arm_current_el(env) < 3
5361         && arm_feature(env, ARM_FEATURE_EL3)
5362         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5363         return CP_ACCESS_TRAP_EL3;
5364     }
5365     return CP_ACCESS_OK;
5366 }
5367
5368 static const ARMCPRegInfo hcrx_el2_reginfo = {
5369     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5370     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5371     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5372     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5373 };
5374
5375 /* Return the effective value of HCRX_EL2.  */
5376 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5377 {
5378     /*
5379      * The bits in this register behave as 0 for all purposes other than
5380      * direct reads of the register if:
5381      *   - EL2 is not enabled in the current security state,
5382      *   - SCR_EL3.HXEn is 0.
5383      */
5384     if (!arm_is_el2_enabled(env)
5385         || (arm_feature(env, ARM_FEATURE_EL3)
5386             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5387         return 0;
5388     }
5389     return env->cp15.hcrx_el2;
5390 }
5391
5392 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5393                            uint64_t value)
5394 {
5395     /*
5396      * For A-profile AArch32 EL3, if NSACR.CP10
5397      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5398      */
5399     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5400         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5401         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5402         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5403     }
5404     env->cp15.cptr_el[2] = value;
5405 }
5406
5407 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5408 {
5409     /*
5410      * For A-profile AArch32 EL3, if NSACR.CP10
5411      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5412      */
5413     uint64_t value = env->cp15.cptr_el[2];
5414
5415     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5416         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5417         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5418     }
5419     return value;
5420 }
5421
5422 static const ARMCPRegInfo el2_cp_reginfo[] = {
5423     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5424       .type = ARM_CP_IO,
5425       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5426       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5427       .writefn = hcr_write },
5428     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5429       .type = ARM_CP_ALIAS | ARM_CP_IO,
5430       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5431       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5432       .writefn = hcr_writelow },
5433     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5434       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5435       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5436     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5437       .type = ARM_CP_ALIAS,
5438       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5439       .access = PL2_RW,
5440       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5441     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5442       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5443       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5444     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5445       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5446       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5447     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5448       .type = ARM_CP_ALIAS,
5449       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5450       .access = PL2_RW,
5451       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5452     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5453       .type = ARM_CP_ALIAS,
5454       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5455       .access = PL2_RW,
5456       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5457     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5458       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5459       .access = PL2_RW, .writefn = vbar_write,
5460       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5461       .resetvalue = 0 },
5462     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5463       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5464       .access = PL3_RW, .type = ARM_CP_ALIAS,
5465       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5466     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5467       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5468       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5469       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5470       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5471     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5472       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5473       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5474       .resetvalue = 0 },
5475     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5476       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5477       .access = PL2_RW, .type = ARM_CP_ALIAS,
5478       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5479     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5480       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5481       .access = PL2_RW, .type = ARM_CP_CONST,
5482       .resetvalue = 0 },
5483     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5484     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5485       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5486       .access = PL2_RW, .type = ARM_CP_CONST,
5487       .resetvalue = 0 },
5488     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5489       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5490       .access = PL2_RW, .type = ARM_CP_CONST,
5491       .resetvalue = 0 },
5492     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5493       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5494       .access = PL2_RW, .type = ARM_CP_CONST,
5495       .resetvalue = 0 },
5496     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5497       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5498       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5499       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5500     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5501       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5502       .type = ARM_CP_ALIAS,
5503       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5504       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5505     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5506       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5507       .access = PL2_RW,
5508       /* no .writefn needed as this can't cause an ASID change */
5509       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5510     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5511       .cp = 15, .opc1 = 6, .crm = 2,
5512       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5513       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5514       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5515       .writefn = vttbr_write },
5516     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5517       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5518       .access = PL2_RW, .writefn = vttbr_write,
5519       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5520     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5521       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5522       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5523       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5524     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5525       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5526       .access = PL2_RW, .resetvalue = 0,
5527       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5528     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5529       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5530       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5531       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5532     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5533       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5534       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5535     { .name = "TLBIALLNSNH",
5536       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5537       .type = ARM_CP_NO_RAW, .access = PL2_W,
5538       .writefn = tlbiall_nsnh_write },
5539     { .name = "TLBIALLNSNHIS",
5540       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5541       .type = ARM_CP_NO_RAW, .access = PL2_W,
5542       .writefn = tlbiall_nsnh_is_write },
5543     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5544       .type = ARM_CP_NO_RAW, .access = PL2_W,
5545       .writefn = tlbiall_hyp_write },
5546     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5547       .type = ARM_CP_NO_RAW, .access = PL2_W,
5548       .writefn = tlbiall_hyp_is_write },
5549     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5550       .type = ARM_CP_NO_RAW, .access = PL2_W,
5551       .writefn = tlbimva_hyp_write },
5552     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5553       .type = ARM_CP_NO_RAW, .access = PL2_W,
5554       .writefn = tlbimva_hyp_is_write },
5555     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5556       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5557       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5558       .writefn = tlbi_aa64_alle2_write },
5559     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5560       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5561       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5562       .writefn = tlbi_aa64_vae2_write },
5563     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5564       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5565       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5566       .writefn = tlbi_aa64_vae2_write },
5567     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5568       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5569       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5570       .writefn = tlbi_aa64_alle2is_write },
5571     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5572       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5573       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5574       .writefn = tlbi_aa64_vae2is_write },
5575     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5576       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5577       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5578       .writefn = tlbi_aa64_vae2is_write },
5579 #ifndef CONFIG_USER_ONLY
5580     /* Unlike the other EL2-related AT operations, these must
5581      * UNDEF from EL3 if EL2 is not implemented, which is why we
5582      * define them here rather than with the rest of the AT ops.
5583      */
5584     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5585       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5586       .access = PL2_W, .accessfn = at_s1e2_access,
5587       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5588       .writefn = ats_write64 },
5589     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5590       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5591       .access = PL2_W, .accessfn = at_s1e2_access,
5592       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5593       .writefn = ats_write64 },
5594     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5595      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5596      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5597      * to behave as if SCR.NS was 1.
5598      */
5599     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5600       .access = PL2_W,
5601       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5602     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5603       .access = PL2_W,
5604       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5605     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5606       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5607       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5608        * reset values as IMPDEF. We choose to reset to 3 to comply with
5609        * both ARMv7 and ARMv8.
5610        */
5611       .access = PL2_RW, .resetvalue = 3,
5612       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5613     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5614       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5615       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5616       .writefn = gt_cntvoff_write,
5617       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5618     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5619       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5620       .writefn = gt_cntvoff_write,
5621       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5622     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5623       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5624       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5625       .type = ARM_CP_IO, .access = PL2_RW,
5626       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5627     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5628       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5629       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5630       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5631     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5632       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5633       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5634       .resetfn = gt_hyp_timer_reset,
5635       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5636     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5637       .type = ARM_CP_IO,
5638       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5639       .access = PL2_RW,
5640       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5641       .resetvalue = 0,
5642       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5643 #endif
5644     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5645       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5646       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5647       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5648     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5649       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5650       .access = PL2_RW,
5651       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5652     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5653       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5654       .access = PL2_RW,
5655       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5656 };
5657
5658 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5659     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5660       .type = ARM_CP_ALIAS | ARM_CP_IO,
5661       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5662       .access = PL2_RW,
5663       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5664       .writefn = hcr_writehigh },
5665 };
5666
5667 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5668                                   bool isread)
5669 {
5670     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5671         return CP_ACCESS_OK;
5672     }
5673     return CP_ACCESS_TRAP_UNCATEGORIZED;
5674 }
5675
5676 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5677     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5678       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5679       .access = PL2_RW, .accessfn = sel2_access,
5680       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5681     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5682       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5683       .access = PL2_RW, .accessfn = sel2_access,
5684       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5685 };
5686
5687 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5688                                    bool isread)
5689 {
5690     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5691      * At Secure EL1 it traps to EL3 or EL2.
5692      */
5693     if (arm_current_el(env) == 3) {
5694         return CP_ACCESS_OK;
5695     }
5696     if (arm_is_secure_below_el3(env)) {
5697         if (env->cp15.scr_el3 & SCR_EEL2) {
5698             return CP_ACCESS_TRAP_EL2;
5699         }
5700         return CP_ACCESS_TRAP_EL3;
5701     }
5702     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5703     if (isread) {
5704         return CP_ACCESS_OK;
5705     }
5706     return CP_ACCESS_TRAP_UNCATEGORIZED;
5707 }
5708
5709 static const ARMCPRegInfo el3_cp_reginfo[] = {
5710     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5711       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5712       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5713       .resetfn = scr_reset, .writefn = scr_write },
5714     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5715       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5716       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5717       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5718       .writefn = scr_write },
5719     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5720       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5721       .access = PL3_RW, .resetvalue = 0,
5722       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5723     { .name = "SDER",
5724       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5725       .access = PL3_RW, .resetvalue = 0,
5726       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5727     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5728       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5729       .writefn = vbar_write, .resetvalue = 0,
5730       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5731     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5732       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5733       .access = PL3_RW, .resetvalue = 0,
5734       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5735     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5736       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5737       .access = PL3_RW,
5738       /* no .writefn needed as this can't cause an ASID change */
5739       .resetvalue = 0,
5740       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5741     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5742       .type = ARM_CP_ALIAS,
5743       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5744       .access = PL3_RW,
5745       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5746     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5747       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5748       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5749     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5750       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5751       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5752     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5753       .type = ARM_CP_ALIAS,
5754       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5755       .access = PL3_RW,
5756       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5757     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5758       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5759       .access = PL3_RW, .writefn = vbar_write,
5760       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5761       .resetvalue = 0 },
5762     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5763       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5764       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5765       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5766     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5767       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5768       .access = PL3_RW, .resetvalue = 0,
5769       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5770     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5771       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5772       .access = PL3_RW, .type = ARM_CP_CONST,
5773       .resetvalue = 0 },
5774     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5775       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5776       .access = PL3_RW, .type = ARM_CP_CONST,
5777       .resetvalue = 0 },
5778     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5779       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5780       .access = PL3_RW, .type = ARM_CP_CONST,
5781       .resetvalue = 0 },
5782     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5783       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5784       .access = PL3_W, .type = ARM_CP_NO_RAW,
5785       .writefn = tlbi_aa64_alle3is_write },
5786     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5787       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5788       .access = PL3_W, .type = ARM_CP_NO_RAW,
5789       .writefn = tlbi_aa64_vae3is_write },
5790     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5791       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5792       .access = PL3_W, .type = ARM_CP_NO_RAW,
5793       .writefn = tlbi_aa64_vae3is_write },
5794     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5795       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5796       .access = PL3_W, .type = ARM_CP_NO_RAW,
5797       .writefn = tlbi_aa64_alle3_write },
5798     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5799       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5800       .access = PL3_W, .type = ARM_CP_NO_RAW,
5801       .writefn = tlbi_aa64_vae3_write },
5802     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5803       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5804       .access = PL3_W, .type = ARM_CP_NO_RAW,
5805       .writefn = tlbi_aa64_vae3_write },
5806 };
5807
5808 #ifndef CONFIG_USER_ONLY
5809 /* Test if system register redirection is to occur in the current state.  */
5810 static bool redirect_for_e2h(CPUARMState *env)
5811 {
5812     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5813 }
5814
5815 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5816 {
5817     CPReadFn *readfn;
5818
5819     if (redirect_for_e2h(env)) {
5820         /* Switch to the saved EL2 version of the register.  */
5821         ri = ri->opaque;
5822         readfn = ri->readfn;
5823     } else {
5824         readfn = ri->orig_readfn;
5825     }
5826     if (readfn == NULL) {
5827         readfn = raw_read;
5828     }
5829     return readfn(env, ri);
5830 }
5831
5832 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5833                           uint64_t value)
5834 {
5835     CPWriteFn *writefn;
5836
5837     if (redirect_for_e2h(env)) {
5838         /* Switch to the saved EL2 version of the register.  */
5839         ri = ri->opaque;
5840         writefn = ri->writefn;
5841     } else {
5842         writefn = ri->orig_writefn;
5843     }
5844     if (writefn == NULL) {
5845         writefn = raw_write;
5846     }
5847     writefn(env, ri, value);
5848 }
5849
5850 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5851 {
5852     struct E2HAlias {
5853         uint32_t src_key, dst_key, new_key;
5854         const char *src_name, *dst_name, *new_name;
5855         bool (*feature)(const ARMISARegisters *id);
5856     };
5857
5858 #define K(op0, op1, crn, crm, op2) \
5859     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5860
5861     static const struct E2HAlias aliases[] = {
5862         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5863           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5864         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5865           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5866         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5867           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5868         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5869           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5870         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5871           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5872         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5873           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5874         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5875           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5876         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5877           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5878         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5879           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5880         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5881           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5882         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5883           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5884         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5885           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5886         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5887           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5888         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5889           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5890         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5891           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5892         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5893           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5894
5895         /*
5896          * Note that redirection of ZCR is mentioned in the description
5897          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5898          * not in the summary table.
5899          */
5900         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5901           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5902         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5903           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5904
5905         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5906           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5907
5908         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5909           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5910           isar_feature_aa64_scxtnum },
5911
5912         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5913         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5914     };
5915 #undef K
5916
5917     size_t i;
5918
5919     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5920         const struct E2HAlias *a = &aliases[i];
5921         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5922         bool ok;
5923
5924         if (a->feature && !a->feature(&cpu->isar)) {
5925             continue;
5926         }
5927
5928         src_reg = g_hash_table_lookup(cpu->cp_regs,
5929                                       (gpointer)(uintptr_t)a->src_key);
5930         dst_reg = g_hash_table_lookup(cpu->cp_regs,
5931                                       (gpointer)(uintptr_t)a->dst_key);
5932         g_assert(src_reg != NULL);
5933         g_assert(dst_reg != NULL);
5934
5935         /* Cross-compare names to detect typos in the keys.  */
5936         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5937         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5938
5939         /* None of the core system registers use opaque; we will.  */
5940         g_assert(src_reg->opaque == NULL);
5941
5942         /* Create alias before redirection so we dup the right data. */
5943         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5944
5945         new_reg->name = a->new_name;
5946         new_reg->type |= ARM_CP_ALIAS;
5947         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
5948         new_reg->access &= PL2_RW | PL3_RW;
5949
5950         ok = g_hash_table_insert(cpu->cp_regs,
5951                                  (gpointer)(uintptr_t)a->new_key, new_reg);
5952         g_assert(ok);
5953
5954         src_reg->opaque = dst_reg;
5955         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5956         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5957         if (!src_reg->raw_readfn) {
5958             src_reg->raw_readfn = raw_read;
5959         }
5960         if (!src_reg->raw_writefn) {
5961             src_reg->raw_writefn = raw_write;
5962         }
5963         src_reg->readfn = el2_e2h_read;
5964         src_reg->writefn = el2_e2h_write;
5965     }
5966 }
5967 #endif
5968
5969 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5970                                      bool isread)
5971 {
5972     int cur_el = arm_current_el(env);
5973
5974     if (cur_el < 2) {
5975         uint64_t hcr = arm_hcr_el2_eff(env);
5976
5977         if (cur_el == 0) {
5978             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5979                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5980                     return CP_ACCESS_TRAP_EL2;
5981                 }
5982             } else {
5983                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5984                     return CP_ACCESS_TRAP;
5985                 }
5986                 if (hcr & HCR_TID2) {
5987                     return CP_ACCESS_TRAP_EL2;
5988                 }
5989             }
5990         } else if (hcr & HCR_TID2) {
5991             return CP_ACCESS_TRAP_EL2;
5992         }
5993     }
5994
5995     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5996         return CP_ACCESS_TRAP_EL2;
5997     }
5998
5999     return CP_ACCESS_OK;
6000 }
6001
6002 /*
6003  * Check for traps to RAS registers, which are controlled
6004  * by HCR_EL2.TERR and SCR_EL3.TERR.
6005  */
6006 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6007                                   bool isread)
6008 {
6009     int el = arm_current_el(env);
6010
6011     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6012         return CP_ACCESS_TRAP_EL2;
6013     }
6014     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6015         return CP_ACCESS_TRAP_EL3;
6016     }
6017     return CP_ACCESS_OK;
6018 }
6019
6020 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6021 {
6022     int el = arm_current_el(env);
6023
6024     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6025         return env->cp15.vdisr_el2;
6026     }
6027     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6028         return 0; /* RAZ/WI */
6029     }
6030     return env->cp15.disr_el1;
6031 }
6032
6033 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6034 {
6035     int el = arm_current_el(env);
6036
6037     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6038         env->cp15.vdisr_el2 = val;
6039         return;
6040     }
6041     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6042         return; /* RAZ/WI */
6043     }
6044     env->cp15.disr_el1 = val;
6045 }
6046
6047 /*
6048  * Minimal RAS implementation with no Error Records.
6049  * Which means that all of the Error Record registers:
6050  *   ERXADDR_EL1
6051  *   ERXCTLR_EL1
6052  *   ERXFR_EL1
6053  *   ERXMISC0_EL1
6054  *   ERXMISC1_EL1
6055  *   ERXMISC2_EL1
6056  *   ERXMISC3_EL1
6057  *   ERXPFGCDN_EL1  (RASv1p1)
6058  *   ERXPFGCTL_EL1  (RASv1p1)
6059  *   ERXPFGF_EL1    (RASv1p1)
6060  *   ERXSTATUS_EL1
6061  * and
6062  *   ERRSELR_EL1
6063  * may generate UNDEFINED, which is the effect we get by not
6064  * listing them at all.
6065  */
6066 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6067     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6068       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6069       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6070       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6071     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6072       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6073       .access = PL1_R, .accessfn = access_terr,
6074       .type = ARM_CP_CONST, .resetvalue = 0 },
6075     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6076       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6077       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6078     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6079       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6080       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6081 };
6082
6083 /*
6084  * Return the exception level to which exceptions should be taken
6085  * via SVEAccessTrap.  This excludes the check for whether the exception
6086  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6087  * be found by testing 0 < fp_exception_el < sve_exception_el.
6088  *
6089  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6090  * pseudocode does *not* separate out the FP trap checks, but has them
6091  * all in one function.
6092  */
6093 int sve_exception_el(CPUARMState *env, int el)
6094 {
6095 #ifndef CONFIG_USER_ONLY
6096     if (el <= 1 && !el_is_in_host(env, el)) {
6097         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6098         case 1:
6099             if (el != 0) {
6100                 break;
6101             }
6102             /* fall through */
6103         case 0:
6104         case 2:
6105             return 1;
6106         }
6107     }
6108
6109     if (el <= 2 && arm_is_el2_enabled(env)) {
6110         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6111         if (env->cp15.hcr_el2 & HCR_E2H) {
6112             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6113             case 1:
6114                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6115                     break;
6116                 }
6117                 /* fall through */
6118             case 0:
6119             case 2:
6120                 return 2;
6121             }
6122         } else {
6123             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6124                 return 2;
6125             }
6126         }
6127     }
6128
6129     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6130     if (arm_feature(env, ARM_FEATURE_EL3)
6131         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6132         return 3;
6133     }
6134 #endif
6135     return 0;
6136 }
6137
6138 /*
6139  * Return the exception level to which exceptions should be taken for SME.
6140  * C.f. the ARM pseudocode function CheckSMEAccess.
6141  */
6142 int sme_exception_el(CPUARMState *env, int el)
6143 {
6144 #ifndef CONFIG_USER_ONLY
6145     if (el <= 1 && !el_is_in_host(env, el)) {
6146         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6147         case 1:
6148             if (el != 0) {
6149                 break;
6150             }
6151             /* fall through */
6152         case 0:
6153         case 2:
6154             return 1;
6155         }
6156     }
6157
6158     if (el <= 2 && arm_is_el2_enabled(env)) {
6159         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6160         if (env->cp15.hcr_el2 & HCR_E2H) {
6161             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6162             case 1:
6163                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6164                     break;
6165                 }
6166                 /* fall through */
6167             case 0:
6168             case 2:
6169                 return 2;
6170             }
6171         } else {
6172             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6173                 return 2;
6174             }
6175         }
6176     }
6177
6178     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6179     if (arm_feature(env, ARM_FEATURE_EL3)
6180         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6181         return 3;
6182     }
6183 #endif
6184     return 0;
6185 }
6186
6187 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6188 static bool sme_fa64(CPUARMState *env, int el)
6189 {
6190     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6191         return false;
6192     }
6193
6194     if (el <= 1 && !el_is_in_host(env, el)) {
6195         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6196             return false;
6197         }
6198     }
6199     if (el <= 2 && arm_is_el2_enabled(env)) {
6200         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6201             return false;
6202         }
6203     }
6204     if (arm_feature(env, ARM_FEATURE_EL3)) {
6205         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6206             return false;
6207         }
6208     }
6209
6210     return true;
6211 }
6212
6213 /*
6214  * Given that SVE is enabled, return the vector length for EL.
6215  */
6216 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6217 {
6218     ARMCPU *cpu = env_archcpu(env);
6219     uint64_t *cr = env->vfp.zcr_el;
6220     uint32_t map = cpu->sve_vq.map;
6221     uint32_t len = ARM_MAX_VQ - 1;
6222
6223     if (sm) {
6224         cr = env->vfp.smcr_el;
6225         map = cpu->sme_vq.map;
6226     }
6227
6228     if (el <= 1 && !el_is_in_host(env, el)) {
6229         len = MIN(len, 0xf & (uint32_t)cr[1]);
6230     }
6231     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6232         len = MIN(len, 0xf & (uint32_t)cr[2]);
6233     }
6234     if (arm_feature(env, ARM_FEATURE_EL3)) {
6235         len = MIN(len, 0xf & (uint32_t)cr[3]);
6236     }
6237
6238     map &= MAKE_64BIT_MASK(0, len + 1);
6239     if (map != 0) {
6240         return 31 - clz32(map);
6241     }
6242
6243     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6244     assert(sm);
6245     return ctz32(cpu->sme_vq.map);
6246 }
6247
6248 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6249 {
6250     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6251 }
6252
6253 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6254                       uint64_t value)
6255 {
6256     int cur_el = arm_current_el(env);
6257     int old_len = sve_vqm1_for_el(env, cur_el);
6258     int new_len;
6259
6260     /* Bits other than [3:0] are RAZ/WI.  */
6261     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6262     raw_write(env, ri, value & 0xf);
6263
6264     /*
6265      * Because we arrived here, we know both FP and SVE are enabled;
6266      * otherwise we would have trapped access to the ZCR_ELn register.
6267      */
6268     new_len = sve_vqm1_for_el(env, cur_el);
6269     if (new_len < old_len) {
6270         aarch64_sve_narrow_vq(env, new_len + 1);
6271     }
6272 }
6273
6274 static const ARMCPRegInfo zcr_reginfo[] = {
6275     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6276       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6277       .access = PL1_RW, .type = ARM_CP_SVE,
6278       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6279       .writefn = zcr_write, .raw_writefn = raw_write },
6280     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6281       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6282       .access = PL2_RW, .type = ARM_CP_SVE,
6283       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6284       .writefn = zcr_write, .raw_writefn = raw_write },
6285     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6286       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6287       .access = PL3_RW, .type = ARM_CP_SVE,
6288       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6289       .writefn = zcr_write, .raw_writefn = raw_write },
6290 };
6291
6292 #ifdef TARGET_AARCH64
6293 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6294                                     bool isread)
6295 {
6296     int el = arm_current_el(env);
6297
6298     if (el == 0) {
6299         uint64_t sctlr = arm_sctlr(env, el);
6300         if (!(sctlr & SCTLR_EnTP2)) {
6301             return CP_ACCESS_TRAP;
6302         }
6303     }
6304     /* TODO: FEAT_FGT */
6305     if (el < 3
6306         && arm_feature(env, ARM_FEATURE_EL3)
6307         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6308         return CP_ACCESS_TRAP_EL3;
6309     }
6310     return CP_ACCESS_OK;
6311 }
6312
6313 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6314                                  bool isread)
6315 {
6316     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6317     if (arm_current_el(env) < 3
6318         && arm_feature(env, ARM_FEATURE_EL3)
6319         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6320         return CP_ACCESS_TRAP_EL3;
6321     }
6322     return CP_ACCESS_OK;
6323 }
6324
6325 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6326                        uint64_t value)
6327 {
6328     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6329     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6330     arm_rebuild_hflags(env);
6331 }
6332
6333 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6334                        uint64_t value)
6335 {
6336     int cur_el = arm_current_el(env);
6337     int old_len = sve_vqm1_for_el(env, cur_el);
6338     int new_len;
6339
6340     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6341     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6342     raw_write(env, ri, value);
6343
6344     /*
6345      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6346      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6347      * current values for simplicity.  But for QEMU internals, we must still
6348      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6349      * above aarch64_sve_narrow_vq.
6350      */
6351     new_len = sve_vqm1_for_el(env, cur_el);
6352     if (new_len < old_len) {
6353         aarch64_sve_narrow_vq(env, new_len + 1);
6354     }
6355 }
6356
6357 static const ARMCPRegInfo sme_reginfo[] = {
6358     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6359       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6360       .access = PL0_RW, .accessfn = access_tpidr2,
6361       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6362     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6363       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6364       .access = PL0_RW, .type = ARM_CP_SME,
6365       .fieldoffset = offsetof(CPUARMState, svcr),
6366       .writefn = svcr_write, .raw_writefn = raw_write },
6367     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6368       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6369       .access = PL1_RW, .type = ARM_CP_SME,
6370       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6371       .writefn = smcr_write, .raw_writefn = raw_write },
6372     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6373       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6374       .access = PL2_RW, .type = ARM_CP_SME,
6375       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6376       .writefn = smcr_write, .raw_writefn = raw_write },
6377     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6378       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6379       .access = PL3_RW, .type = ARM_CP_SME,
6380       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6381       .writefn = smcr_write, .raw_writefn = raw_write },
6382     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6383       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6384       .access = PL1_R, .accessfn = access_aa64_tid1,
6385       /*
6386        * IMPLEMENTOR = 0 (software)
6387        * REVISION    = 0 (implementation defined)
6388        * SMPS        = 0 (no streaming execution priority in QEMU)
6389        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6390        */
6391       .type = ARM_CP_CONST, .resetvalue = 0, },
6392     /*
6393      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6394      */
6395     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6396       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6397       .access = PL1_RW, .accessfn = access_esm,
6398       .type = ARM_CP_CONST, .resetvalue = 0 },
6399     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6400       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6401       .access = PL2_RW, .accessfn = access_esm,
6402       .type = ARM_CP_CONST, .resetvalue = 0 },
6403 };
6404 #endif /* TARGET_AARCH64 */
6405
6406 static void define_pmu_regs(ARMCPU *cpu)
6407 {
6408     /*
6409      * v7 performance monitor control register: same implementor
6410      * field as main ID register, and we implement four counters in
6411      * addition to the cycle count register.
6412      */
6413     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6414     ARMCPRegInfo pmcr = {
6415         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6416         .access = PL0_RW,
6417         .type = ARM_CP_IO | ARM_CP_ALIAS,
6418         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6419         .accessfn = pmreg_access, .writefn = pmcr_write,
6420         .raw_writefn = raw_write,
6421     };
6422     ARMCPRegInfo pmcr64 = {
6423         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6424         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6425         .access = PL0_RW, .accessfn = pmreg_access,
6426         .type = ARM_CP_IO,
6427         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6428         .resetvalue = cpu->isar.reset_pmcr_el0,
6429         .writefn = pmcr_write, .raw_writefn = raw_write,
6430     };
6431
6432     define_one_arm_cp_reg(cpu, &pmcr);
6433     define_one_arm_cp_reg(cpu, &pmcr64);
6434     for (i = 0; i < pmcrn; i++) {
6435         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6436         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6437         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6438         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6439         ARMCPRegInfo pmev_regs[] = {
6440             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6441               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6442               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6443               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6444               .accessfn = pmreg_access_xevcntr },
6445             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6446               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6447               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6448               .type = ARM_CP_IO,
6449               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6450               .raw_readfn = pmevcntr_rawread,
6451               .raw_writefn = pmevcntr_rawwrite },
6452             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6453               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6454               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6455               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6456               .accessfn = pmreg_access },
6457             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6458               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6459               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6460               .type = ARM_CP_IO,
6461               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6462               .raw_writefn = pmevtyper_rawwrite },
6463         };
6464         define_arm_cp_regs(cpu, pmev_regs);
6465         g_free(pmevcntr_name);
6466         g_free(pmevcntr_el0_name);
6467         g_free(pmevtyper_name);
6468         g_free(pmevtyper_el0_name);
6469     }
6470     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6471         ARMCPRegInfo v81_pmu_regs[] = {
6472             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6473               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6474               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6475               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6476             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6477               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6478               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6479               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6480         };
6481         define_arm_cp_regs(cpu, v81_pmu_regs);
6482     }
6483     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6484         static const ARMCPRegInfo v84_pmmir = {
6485             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6486             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6487             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6488             .resetvalue = 0
6489         };
6490         define_one_arm_cp_reg(cpu, &v84_pmmir);
6491     }
6492 }
6493
6494 /* We don't know until after realize whether there's a GICv3
6495  * attached, and that is what registers the gicv3 sysregs.
6496  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6497  * at runtime.
6498  */
6499 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6500 {
6501     ARMCPU *cpu = env_archcpu(env);
6502     uint64_t pfr1 = cpu->isar.id_pfr1;
6503
6504     if (env->gicv3state) {
6505         pfr1 |= 1 << 28;
6506     }
6507     return pfr1;
6508 }
6509
6510 #ifndef CONFIG_USER_ONLY
6511 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6512 {
6513     ARMCPU *cpu = env_archcpu(env);
6514     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6515
6516     if (env->gicv3state) {
6517         pfr0 |= 1 << 24;
6518     }
6519     return pfr0;
6520 }
6521 #endif
6522
6523 /* Shared logic between LORID and the rest of the LOR* registers.
6524  * Secure state exclusion has already been dealt with.
6525  */
6526 static CPAccessResult access_lor_ns(CPUARMState *env,
6527                                     const ARMCPRegInfo *ri, bool isread)
6528 {
6529     int el = arm_current_el(env);
6530
6531     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6532         return CP_ACCESS_TRAP_EL2;
6533     }
6534     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6535         return CP_ACCESS_TRAP_EL3;
6536     }
6537     return CP_ACCESS_OK;
6538 }
6539
6540 static CPAccessResult access_lor_other(CPUARMState *env,
6541                                        const ARMCPRegInfo *ri, bool isread)
6542 {
6543     if (arm_is_secure_below_el3(env)) {
6544         /* Access denied in secure mode.  */
6545         return CP_ACCESS_TRAP;
6546     }
6547     return access_lor_ns(env, ri, isread);
6548 }
6549
6550 /*
6551  * A trivial implementation of ARMv8.1-LOR leaves all of these
6552  * registers fixed at 0, which indicates that there are zero
6553  * supported Limited Ordering regions.
6554  */
6555 static const ARMCPRegInfo lor_reginfo[] = {
6556     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6557       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6558       .access = PL1_RW, .accessfn = access_lor_other,
6559       .type = ARM_CP_CONST, .resetvalue = 0 },
6560     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6561       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6562       .access = PL1_RW, .accessfn = access_lor_other,
6563       .type = ARM_CP_CONST, .resetvalue = 0 },
6564     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6565       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6566       .access = PL1_RW, .accessfn = access_lor_other,
6567       .type = ARM_CP_CONST, .resetvalue = 0 },
6568     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6569       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6570       .access = PL1_RW, .accessfn = access_lor_other,
6571       .type = ARM_CP_CONST, .resetvalue = 0 },
6572     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6573       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6574       .access = PL1_R, .accessfn = access_lor_ns,
6575       .type = ARM_CP_CONST, .resetvalue = 0 },
6576 };
6577
6578 #ifdef TARGET_AARCH64
6579 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6580                                    bool isread)
6581 {
6582     int el = arm_current_el(env);
6583
6584     if (el < 2 &&
6585         arm_is_el2_enabled(env) &&
6586         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6587         return CP_ACCESS_TRAP_EL2;
6588     }
6589     if (el < 3 &&
6590         arm_feature(env, ARM_FEATURE_EL3) &&
6591         !(env->cp15.scr_el3 & SCR_APK)) {
6592         return CP_ACCESS_TRAP_EL3;
6593     }
6594     return CP_ACCESS_OK;
6595 }
6596
6597 static const ARMCPRegInfo pauth_reginfo[] = {
6598     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6599       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6600       .access = PL1_RW, .accessfn = access_pauth,
6601       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6602     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6603       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6604       .access = PL1_RW, .accessfn = access_pauth,
6605       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6606     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6607       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6608       .access = PL1_RW, .accessfn = access_pauth,
6609       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6610     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6611       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6612       .access = PL1_RW, .accessfn = access_pauth,
6613       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6614     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6615       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6616       .access = PL1_RW, .accessfn = access_pauth,
6617       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6618     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6619       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6620       .access = PL1_RW, .accessfn = access_pauth,
6621       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6622     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6623       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6624       .access = PL1_RW, .accessfn = access_pauth,
6625       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6626     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6627       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6628       .access = PL1_RW, .accessfn = access_pauth,
6629       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6630     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6631       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6632       .access = PL1_RW, .accessfn = access_pauth,
6633       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6634     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6635       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6636       .access = PL1_RW, .accessfn = access_pauth,
6637       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6638 };
6639
6640 static const ARMCPRegInfo tlbirange_reginfo[] = {
6641     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6642       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6643       .access = PL1_W, .type = ARM_CP_NO_RAW,
6644       .writefn = tlbi_aa64_rvae1is_write },
6645     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6646       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6647       .access = PL1_W, .type = ARM_CP_NO_RAW,
6648       .writefn = tlbi_aa64_rvae1is_write },
6649    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6650       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6651       .access = PL1_W, .type = ARM_CP_NO_RAW,
6652       .writefn = tlbi_aa64_rvae1is_write },
6653     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6654       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6655       .access = PL1_W, .type = ARM_CP_NO_RAW,
6656       .writefn = tlbi_aa64_rvae1is_write },
6657     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6658       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6659       .access = PL1_W, .type = ARM_CP_NO_RAW,
6660       .writefn = tlbi_aa64_rvae1is_write },
6661     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6662       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6663       .access = PL1_W, .type = ARM_CP_NO_RAW,
6664       .writefn = tlbi_aa64_rvae1is_write },
6665    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6666       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6667       .access = PL1_W, .type = ARM_CP_NO_RAW,
6668       .writefn = tlbi_aa64_rvae1is_write },
6669     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6670       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6671       .access = PL1_W, .type = ARM_CP_NO_RAW,
6672       .writefn = tlbi_aa64_rvae1is_write },
6673     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6674       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6675       .access = PL1_W, .type = ARM_CP_NO_RAW,
6676       .writefn = tlbi_aa64_rvae1_write },
6677     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6678       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6679       .access = PL1_W, .type = ARM_CP_NO_RAW,
6680       .writefn = tlbi_aa64_rvae1_write },
6681    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6682       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6683       .access = PL1_W, .type = ARM_CP_NO_RAW,
6684       .writefn = tlbi_aa64_rvae1_write },
6685     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6686       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6687       .access = PL1_W, .type = ARM_CP_NO_RAW,
6688       .writefn = tlbi_aa64_rvae1_write },
6689     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6690       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6691       .access = PL2_W, .type = ARM_CP_NOP },
6692     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6693       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6694       .access = PL2_W, .type = ARM_CP_NOP },
6695     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6696       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6697       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6698       .writefn = tlbi_aa64_rvae2is_write },
6699    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6700       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6701       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6702       .writefn = tlbi_aa64_rvae2is_write },
6703     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6704       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6705       .access = PL2_W, .type = ARM_CP_NOP },
6706    { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6707       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6708       .access = PL2_W, .type = ARM_CP_NOP },
6709    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6710       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6711       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6712       .writefn = tlbi_aa64_rvae2is_write },
6713    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6714       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6715       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6716       .writefn = tlbi_aa64_rvae2is_write },
6717     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6718       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6719       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6720       .writefn = tlbi_aa64_rvae2_write },
6721    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6722       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6723       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6724       .writefn = tlbi_aa64_rvae2_write },
6725    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6726       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6727       .access = PL3_W, .type = ARM_CP_NO_RAW,
6728       .writefn = tlbi_aa64_rvae3is_write },
6729    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6730       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6731       .access = PL3_W, .type = ARM_CP_NO_RAW,
6732       .writefn = tlbi_aa64_rvae3is_write },
6733    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6734       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6735       .access = PL3_W, .type = ARM_CP_NO_RAW,
6736       .writefn = tlbi_aa64_rvae3is_write },
6737    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6738       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6739       .access = PL3_W, .type = ARM_CP_NO_RAW,
6740       .writefn = tlbi_aa64_rvae3is_write },
6741    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6742       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6743       .access = PL3_W, .type = ARM_CP_NO_RAW,
6744       .writefn = tlbi_aa64_rvae3_write },
6745    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6746       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6747       .access = PL3_W, .type = ARM_CP_NO_RAW,
6748       .writefn = tlbi_aa64_rvae3_write },
6749 };
6750
6751 static const ARMCPRegInfo tlbios_reginfo[] = {
6752     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6753       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6754       .access = PL1_W, .type = ARM_CP_NO_RAW,
6755       .writefn = tlbi_aa64_vmalle1is_write },
6756     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6757       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6758       .access = PL1_W, .type = ARM_CP_NO_RAW,
6759       .writefn = tlbi_aa64_vae1is_write },
6760     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6761       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6762       .access = PL1_W, .type = ARM_CP_NO_RAW,
6763       .writefn = tlbi_aa64_vmalle1is_write },
6764     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6765       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6766       .access = PL1_W, .type = ARM_CP_NO_RAW,
6767       .writefn = tlbi_aa64_vae1is_write },
6768     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6769       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6770       .access = PL1_W, .type = ARM_CP_NO_RAW,
6771       .writefn = tlbi_aa64_vae1is_write },
6772     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6773       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6774       .access = PL1_W, .type = ARM_CP_NO_RAW,
6775       .writefn = tlbi_aa64_vae1is_write },
6776     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6777       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6778       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6779       .writefn = tlbi_aa64_alle2is_write },
6780     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6781       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6782       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6783       .writefn = tlbi_aa64_vae2is_write },
6784    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6785       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6786       .access = PL2_W, .type = ARM_CP_NO_RAW,
6787       .writefn = tlbi_aa64_alle1is_write },
6788     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6789       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6790       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6791       .writefn = tlbi_aa64_vae2is_write },
6792     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6793       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6794       .access = PL2_W, .type = ARM_CP_NO_RAW,
6795       .writefn = tlbi_aa64_alle1is_write },
6796     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6797       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6798       .access = PL2_W, .type = ARM_CP_NOP },
6799     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6800       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6801       .access = PL2_W, .type = ARM_CP_NOP },
6802     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6803       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6804       .access = PL2_W, .type = ARM_CP_NOP },
6805     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6806       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6807       .access = PL2_W, .type = ARM_CP_NOP },
6808     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6809       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6810       .access = PL3_W, .type = ARM_CP_NO_RAW,
6811       .writefn = tlbi_aa64_alle3is_write },
6812     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6813       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6814       .access = PL3_W, .type = ARM_CP_NO_RAW,
6815       .writefn = tlbi_aa64_vae3is_write },
6816     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6817       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6818       .access = PL3_W, .type = ARM_CP_NO_RAW,
6819       .writefn = tlbi_aa64_vae3is_write },
6820 };
6821
6822 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6823 {
6824     Error *err = NULL;
6825     uint64_t ret;
6826
6827     /* Success sets NZCV = 0000.  */
6828     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6829
6830     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6831         /*
6832          * ??? Failed, for unknown reasons in the crypto subsystem.
6833          * The best we can do is log the reason and return the
6834          * timed-out indication to the guest.  There is no reason
6835          * we know to expect this failure to be transitory, so the
6836          * guest may well hang retrying the operation.
6837          */
6838         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6839                       ri->name, error_get_pretty(err));
6840         error_free(err);
6841
6842         env->ZF = 0; /* NZCF = 0100 */
6843         return 0;
6844     }
6845     return ret;
6846 }
6847
6848 /* We do not support re-seeding, so the two registers operate the same.  */
6849 static const ARMCPRegInfo rndr_reginfo[] = {
6850     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6851       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6852       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6853       .access = PL0_R, .readfn = rndr_readfn },
6854     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6855       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6856       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6857       .access = PL0_R, .readfn = rndr_readfn },
6858 };
6859
6860 #ifndef CONFIG_USER_ONLY
6861 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6862                           uint64_t value)
6863 {
6864     ARMCPU *cpu = env_archcpu(env);
6865     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6866     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6867     uint64_t vaddr_in = (uint64_t) value;
6868     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6869     void *haddr;
6870     int mem_idx = cpu_mmu_index(env, false);
6871
6872     /* This won't be crossing page boundaries */
6873     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6874     if (haddr) {
6875
6876         ram_addr_t offset;
6877         MemoryRegion *mr;
6878
6879         /* RCU lock is already being held */
6880         mr = memory_region_from_host(haddr, &offset);
6881
6882         if (mr) {
6883             memory_region_writeback(mr, offset, dline_size);
6884         }
6885     }
6886 }
6887
6888 static const ARMCPRegInfo dcpop_reg[] = {
6889     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6890       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6891       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6892       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6893 };
6894
6895 static const ARMCPRegInfo dcpodp_reg[] = {
6896     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6897       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6898       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6899       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6900 };
6901 #endif /*CONFIG_USER_ONLY*/
6902
6903 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6904                                        bool isread)
6905 {
6906     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6907         return CP_ACCESS_TRAP_EL2;
6908     }
6909
6910     return CP_ACCESS_OK;
6911 }
6912
6913 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6914                                  bool isread)
6915 {
6916     int el = arm_current_el(env);
6917
6918     if (el < 2 && arm_is_el2_enabled(env)) {
6919         uint64_t hcr = arm_hcr_el2_eff(env);
6920         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6921             return CP_ACCESS_TRAP_EL2;
6922         }
6923     }
6924     if (el < 3 &&
6925         arm_feature(env, ARM_FEATURE_EL3) &&
6926         !(env->cp15.scr_el3 & SCR_ATA)) {
6927         return CP_ACCESS_TRAP_EL3;
6928     }
6929     return CP_ACCESS_OK;
6930 }
6931
6932 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6933 {
6934     return env->pstate & PSTATE_TCO;
6935 }
6936
6937 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6938 {
6939     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6940 }
6941
6942 static const ARMCPRegInfo mte_reginfo[] = {
6943     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6944       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6945       .access = PL1_RW, .accessfn = access_mte,
6946       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6947     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6948       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6949       .access = PL1_RW, .accessfn = access_mte,
6950       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6951     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6952       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6953       .access = PL2_RW, .accessfn = access_mte,
6954       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6955     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6956       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6957       .access = PL3_RW,
6958       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6959     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6960       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6961       .access = PL1_RW, .accessfn = access_mte,
6962       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6963     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6964       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6965       .access = PL1_RW, .accessfn = access_mte,
6966       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6967     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6968       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6969       .access = PL1_R, .accessfn = access_aa64_tid5,
6970       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6971     { .name = "TCO", .state = ARM_CP_STATE_AA64,
6972       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6973       .type = ARM_CP_NO_RAW,
6974       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
6975     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6976       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6977       .type = ARM_CP_NOP, .access = PL1_W,
6978       .accessfn = aa64_cacheop_poc_access },
6979     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6980       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6981       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6982     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6983       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6984       .type = ARM_CP_NOP, .access = PL1_W,
6985       .accessfn = aa64_cacheop_poc_access },
6986     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6987       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6988       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6989     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6990       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6991       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6992     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6993       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
6994       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6995     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
6996       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
6997       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6998     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
6999       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7000       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7001 };
7002
7003 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7004     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7005       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7006       .type = ARM_CP_CONST, .access = PL0_RW, },
7007 };
7008
7009 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7010     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7011       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7012       .type = ARM_CP_NOP, .access = PL0_W,
7013       .accessfn = aa64_cacheop_poc_access },
7014     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7015       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7016       .type = ARM_CP_NOP, .access = PL0_W,
7017       .accessfn = aa64_cacheop_poc_access },
7018     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7019       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7020       .type = ARM_CP_NOP, .access = PL0_W,
7021       .accessfn = aa64_cacheop_poc_access },
7022     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7023       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7024       .type = ARM_CP_NOP, .access = PL0_W,
7025       .accessfn = aa64_cacheop_poc_access },
7026     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7027       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7028       .type = ARM_CP_NOP, .access = PL0_W,
7029       .accessfn = aa64_cacheop_poc_access },
7030     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7031       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7032       .type = ARM_CP_NOP, .access = PL0_W,
7033       .accessfn = aa64_cacheop_poc_access },
7034     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7035       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7036       .type = ARM_CP_NOP, .access = PL0_W,
7037       .accessfn = aa64_cacheop_poc_access },
7038     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7039       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7040       .type = ARM_CP_NOP, .access = PL0_W,
7041       .accessfn = aa64_cacheop_poc_access },
7042     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7043       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7044       .access = PL0_W, .type = ARM_CP_DC_GVA,
7045 #ifndef CONFIG_USER_ONLY
7046       /* Avoid overhead of an access check that always passes in user-mode */
7047       .accessfn = aa64_zva_access,
7048 #endif
7049     },
7050     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7051       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7052       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7053 #ifndef CONFIG_USER_ONLY
7054       /* Avoid overhead of an access check that always passes in user-mode */
7055       .accessfn = aa64_zva_access,
7056 #endif
7057     },
7058 };
7059
7060 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7061                                      bool isread)
7062 {
7063     uint64_t hcr = arm_hcr_el2_eff(env);
7064     int el = arm_current_el(env);
7065
7066     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7067         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7068             if (hcr & HCR_TGE) {
7069                 return CP_ACCESS_TRAP_EL2;
7070             }
7071             return CP_ACCESS_TRAP;
7072         }
7073     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7074         return CP_ACCESS_TRAP_EL2;
7075     }
7076     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7077         return CP_ACCESS_TRAP_EL2;
7078     }
7079     if (el < 3
7080         && arm_feature(env, ARM_FEATURE_EL3)
7081         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7082         return CP_ACCESS_TRAP_EL3;
7083     }
7084     return CP_ACCESS_OK;
7085 }
7086
7087 static const ARMCPRegInfo scxtnum_reginfo[] = {
7088     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7089       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7090       .access = PL0_RW, .accessfn = access_scxtnum,
7091       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7092     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7093       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7094       .access = PL1_RW, .accessfn = access_scxtnum,
7095       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7096     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7097       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7098       .access = PL2_RW, .accessfn = access_scxtnum,
7099       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7100     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7101       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7102       .access = PL3_RW,
7103       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7104 };
7105 #endif /* TARGET_AARCH64 */
7106
7107 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7108                                      bool isread)
7109 {
7110     int el = arm_current_el(env);
7111
7112     if (el == 0) {
7113         uint64_t sctlr = arm_sctlr(env, el);
7114         if (!(sctlr & SCTLR_EnRCTX)) {
7115             return CP_ACCESS_TRAP;
7116         }
7117     } else if (el == 1) {
7118         uint64_t hcr = arm_hcr_el2_eff(env);
7119         if (hcr & HCR_NV) {
7120             return CP_ACCESS_TRAP_EL2;
7121         }
7122     }
7123     return CP_ACCESS_OK;
7124 }
7125
7126 static const ARMCPRegInfo predinv_reginfo[] = {
7127     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7128       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7129       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7130     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7131       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7132       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7133     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7134       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7135       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7136     /*
7137      * Note the AArch32 opcodes have a different OPC1.
7138      */
7139     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7140       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7141       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7142     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7143       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7144       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7145     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7146       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7147       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7148 };
7149
7150 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7151 {
7152     /* Read the high 32 bits of the current CCSIDR */
7153     return extract64(ccsidr_read(env, ri), 32, 32);
7154 }
7155
7156 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7157     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7158       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7159       .access = PL1_R,
7160       .accessfn = access_aa64_tid2,
7161       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7162 };
7163
7164 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7165                                        bool isread)
7166 {
7167     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7168         return CP_ACCESS_TRAP_EL2;
7169     }
7170
7171     return CP_ACCESS_OK;
7172 }
7173
7174 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7175                                        bool isread)
7176 {
7177     if (arm_feature(env, ARM_FEATURE_V8)) {
7178         return access_aa64_tid3(env, ri, isread);
7179     }
7180
7181     return CP_ACCESS_OK;
7182 }
7183
7184 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7185                                      bool isread)
7186 {
7187     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7188         return CP_ACCESS_TRAP_EL2;
7189     }
7190
7191     return CP_ACCESS_OK;
7192 }
7193
7194 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7195                                         const ARMCPRegInfo *ri, bool isread)
7196 {
7197     /*
7198      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7199      * in v7A, not in v8A.
7200      */
7201     if (!arm_feature(env, ARM_FEATURE_V8) &&
7202         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7203         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7204         return CP_ACCESS_TRAP_EL2;
7205     }
7206     return CP_ACCESS_OK;
7207 }
7208
7209 static const ARMCPRegInfo jazelle_regs[] = {
7210     { .name = "JIDR",
7211       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7212       .access = PL1_R, .accessfn = access_jazelle,
7213       .type = ARM_CP_CONST, .resetvalue = 0 },
7214     { .name = "JOSCR",
7215       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7216       .accessfn = access_joscr_jmcr,
7217       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7218     { .name = "JMCR",
7219       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7220       .accessfn = access_joscr_jmcr,
7221       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7222 };
7223
7224 static const ARMCPRegInfo contextidr_el2 = {
7225     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7226     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7227     .access = PL2_RW,
7228     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7229 };
7230
7231 static const ARMCPRegInfo vhe_reginfo[] = {
7232     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7233       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7234       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7235       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7236 #ifndef CONFIG_USER_ONLY
7237     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7238       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7239       .fieldoffset =
7240         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7241       .type = ARM_CP_IO, .access = PL2_RW,
7242       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7243     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7244       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7245       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7246       .resetfn = gt_hv_timer_reset,
7247       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7248     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7249       .type = ARM_CP_IO,
7250       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7251       .access = PL2_RW,
7252       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7253       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7254     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7255       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7256       .type = ARM_CP_IO | ARM_CP_ALIAS,
7257       .access = PL2_RW, .accessfn = e2h_access,
7258       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7259       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7260     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7261       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7262       .type = ARM_CP_IO | ARM_CP_ALIAS,
7263       .access = PL2_RW, .accessfn = e2h_access,
7264       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7265       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7266     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7267       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7268       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7269       .access = PL2_RW, .accessfn = e2h_access,
7270       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7271     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7272       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7273       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7274       .access = PL2_RW, .accessfn = e2h_access,
7275       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7276     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7277       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7278       .type = ARM_CP_IO | ARM_CP_ALIAS,
7279       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7280       .access = PL2_RW, .accessfn = e2h_access,
7281       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7282     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7283       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7284       .type = ARM_CP_IO | ARM_CP_ALIAS,
7285       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7286       .access = PL2_RW, .accessfn = e2h_access,
7287       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7288 #endif
7289 };
7290
7291 #ifndef CONFIG_USER_ONLY
7292 static const ARMCPRegInfo ats1e1_reginfo[] = {
7293     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7294       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7295       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7296       .writefn = ats_write64 },
7297     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7298       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7299       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7300       .writefn = ats_write64 },
7301 };
7302
7303 static const ARMCPRegInfo ats1cp_reginfo[] = {
7304     { .name = "ATS1CPRP",
7305       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7306       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7307       .writefn = ats_write },
7308     { .name = "ATS1CPWP",
7309       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7310       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7311       .writefn = ats_write },
7312 };
7313 #endif
7314
7315 /*
7316  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7317  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7318  * is non-zero, which is never for ARMv7, optionally in ARMv8
7319  * and mandatorily for ARMv8.2 and up.
7320  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7321  * implementation is RAZ/WI we can ignore this detail, as we
7322  * do for ACTLR.
7323  */
7324 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7325     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7326       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7327       .access = PL1_RW, .accessfn = access_tacr,
7328       .type = ARM_CP_CONST, .resetvalue = 0 },
7329     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7330       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7331       .access = PL2_RW, .type = ARM_CP_CONST,
7332       .resetvalue = 0 },
7333 };
7334
7335 void register_cp_regs_for_features(ARMCPU *cpu)
7336 {
7337     /* Register all the coprocessor registers based on feature bits */
7338     CPUARMState *env = &cpu->env;
7339     if (arm_feature(env, ARM_FEATURE_M)) {
7340         /* M profile has no coprocessor registers */
7341         return;
7342     }
7343
7344     define_arm_cp_regs(cpu, cp_reginfo);
7345     if (!arm_feature(env, ARM_FEATURE_V8)) {
7346         /* Must go early as it is full of wildcards that may be
7347          * overridden by later definitions.
7348          */
7349         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7350     }
7351
7352     if (arm_feature(env, ARM_FEATURE_V6)) {
7353         /* The ID registers all have impdef reset values */
7354         ARMCPRegInfo v6_idregs[] = {
7355             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7356               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7357               .access = PL1_R, .type = ARM_CP_CONST,
7358               .accessfn = access_aa32_tid3,
7359               .resetvalue = cpu->isar.id_pfr0 },
7360             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7361              * the value of the GIC field until after we define these regs.
7362              */
7363             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7364               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7365               .access = PL1_R, .type = ARM_CP_NO_RAW,
7366               .accessfn = access_aa32_tid3,
7367               .readfn = id_pfr1_read,
7368               .writefn = arm_cp_write_ignore },
7369             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7370               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7371               .access = PL1_R, .type = ARM_CP_CONST,
7372               .accessfn = access_aa32_tid3,
7373               .resetvalue = cpu->isar.id_dfr0 },
7374             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7375               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7376               .access = PL1_R, .type = ARM_CP_CONST,
7377               .accessfn = access_aa32_tid3,
7378               .resetvalue = cpu->id_afr0 },
7379             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7380               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7381               .access = PL1_R, .type = ARM_CP_CONST,
7382               .accessfn = access_aa32_tid3,
7383               .resetvalue = cpu->isar.id_mmfr0 },
7384             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7385               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7386               .access = PL1_R, .type = ARM_CP_CONST,
7387               .accessfn = access_aa32_tid3,
7388               .resetvalue = cpu->isar.id_mmfr1 },
7389             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7390               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7391               .access = PL1_R, .type = ARM_CP_CONST,
7392               .accessfn = access_aa32_tid3,
7393               .resetvalue = cpu->isar.id_mmfr2 },
7394             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7395               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7396               .access = PL1_R, .type = ARM_CP_CONST,
7397               .accessfn = access_aa32_tid3,
7398               .resetvalue = cpu->isar.id_mmfr3 },
7399             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7400               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7401               .access = PL1_R, .type = ARM_CP_CONST,
7402               .accessfn = access_aa32_tid3,
7403               .resetvalue = cpu->isar.id_isar0 },
7404             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7405               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7406               .access = PL1_R, .type = ARM_CP_CONST,
7407               .accessfn = access_aa32_tid3,
7408               .resetvalue = cpu->isar.id_isar1 },
7409             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7410               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7411               .access = PL1_R, .type = ARM_CP_CONST,
7412               .accessfn = access_aa32_tid3,
7413               .resetvalue = cpu->isar.id_isar2 },
7414             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7415               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7416               .access = PL1_R, .type = ARM_CP_CONST,
7417               .accessfn = access_aa32_tid3,
7418               .resetvalue = cpu->isar.id_isar3 },
7419             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7420               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7421               .access = PL1_R, .type = ARM_CP_CONST,
7422               .accessfn = access_aa32_tid3,
7423               .resetvalue = cpu->isar.id_isar4 },
7424             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7425               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7426               .access = PL1_R, .type = ARM_CP_CONST,
7427               .accessfn = access_aa32_tid3,
7428               .resetvalue = cpu->isar.id_isar5 },
7429             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7430               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7431               .access = PL1_R, .type = ARM_CP_CONST,
7432               .accessfn = access_aa32_tid3,
7433               .resetvalue = cpu->isar.id_mmfr4 },
7434             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7435               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7436               .access = PL1_R, .type = ARM_CP_CONST,
7437               .accessfn = access_aa32_tid3,
7438               .resetvalue = cpu->isar.id_isar6 },
7439         };
7440         define_arm_cp_regs(cpu, v6_idregs);
7441         define_arm_cp_regs(cpu, v6_cp_reginfo);
7442     } else {
7443         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7444     }
7445     if (arm_feature(env, ARM_FEATURE_V6K)) {
7446         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7447     }
7448     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7449         !arm_feature(env, ARM_FEATURE_PMSA)) {
7450         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7451     }
7452     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7453         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7454     }
7455     if (arm_feature(env, ARM_FEATURE_V7)) {
7456         ARMCPRegInfo clidr = {
7457             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7458             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7459             .access = PL1_R, .type = ARM_CP_CONST,
7460             .accessfn = access_aa64_tid2,
7461             .resetvalue = cpu->clidr
7462         };
7463         define_one_arm_cp_reg(cpu, &clidr);
7464         define_arm_cp_regs(cpu, v7_cp_reginfo);
7465         define_debug_regs(cpu);
7466         define_pmu_regs(cpu);
7467     } else {
7468         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7469     }
7470     if (arm_feature(env, ARM_FEATURE_V8)) {
7471         /*
7472          * v8 ID registers, which all have impdef reset values.
7473          * Note that within the ID register ranges the unused slots
7474          * must all RAZ, not UNDEF; future architecture versions may
7475          * define new registers here.
7476          * ID registers which are AArch64 views of the AArch32 ID registers
7477          * which already existed in v6 and v7 are handled elsewhere,
7478          * in v6_idregs[].
7479          */
7480         int i;
7481         ARMCPRegInfo v8_idregs[] = {
7482             /*
7483              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7484              * emulation because we don't know the right value for the
7485              * GIC field until after we define these regs.
7486              */
7487             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7488               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7489               .access = PL1_R,
7490 #ifdef CONFIG_USER_ONLY
7491               .type = ARM_CP_CONST,
7492               .resetvalue = cpu->isar.id_aa64pfr0
7493 #else
7494               .type = ARM_CP_NO_RAW,
7495               .accessfn = access_aa64_tid3,
7496               .readfn = id_aa64pfr0_read,
7497               .writefn = arm_cp_write_ignore
7498 #endif
7499             },
7500             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7501               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7502               .access = PL1_R, .type = ARM_CP_CONST,
7503               .accessfn = access_aa64_tid3,
7504               .resetvalue = cpu->isar.id_aa64pfr1},
7505             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7506               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7507               .access = PL1_R, .type = ARM_CP_CONST,
7508               .accessfn = access_aa64_tid3,
7509               .resetvalue = 0 },
7510             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7511               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7512               .access = PL1_R, .type = ARM_CP_CONST,
7513               .accessfn = access_aa64_tid3,
7514               .resetvalue = 0 },
7515             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7516               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7517               .access = PL1_R, .type = ARM_CP_CONST,
7518               .accessfn = access_aa64_tid3,
7519               .resetvalue = cpu->isar.id_aa64zfr0 },
7520             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7521               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7522               .access = PL1_R, .type = ARM_CP_CONST,
7523               .accessfn = access_aa64_tid3,
7524               .resetvalue = cpu->isar.id_aa64smfr0 },
7525             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7526               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7527               .access = PL1_R, .type = ARM_CP_CONST,
7528               .accessfn = access_aa64_tid3,
7529               .resetvalue = 0 },
7530             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7531               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7532               .access = PL1_R, .type = ARM_CP_CONST,
7533               .accessfn = access_aa64_tid3,
7534               .resetvalue = 0 },
7535             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7536               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7537               .access = PL1_R, .type = ARM_CP_CONST,
7538               .accessfn = access_aa64_tid3,
7539               .resetvalue = cpu->isar.id_aa64dfr0 },
7540             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7541               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7542               .access = PL1_R, .type = ARM_CP_CONST,
7543               .accessfn = access_aa64_tid3,
7544               .resetvalue = cpu->isar.id_aa64dfr1 },
7545             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7546               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7547               .access = PL1_R, .type = ARM_CP_CONST,
7548               .accessfn = access_aa64_tid3,
7549               .resetvalue = 0 },
7550             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7551               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7552               .access = PL1_R, .type = ARM_CP_CONST,
7553               .accessfn = access_aa64_tid3,
7554               .resetvalue = 0 },
7555             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7556               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7557               .access = PL1_R, .type = ARM_CP_CONST,
7558               .accessfn = access_aa64_tid3,
7559               .resetvalue = cpu->id_aa64afr0 },
7560             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7561               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7562               .access = PL1_R, .type = ARM_CP_CONST,
7563               .accessfn = access_aa64_tid3,
7564               .resetvalue = cpu->id_aa64afr1 },
7565             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7566               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7567               .access = PL1_R, .type = ARM_CP_CONST,
7568               .accessfn = access_aa64_tid3,
7569               .resetvalue = 0 },
7570             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7571               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7572               .access = PL1_R, .type = ARM_CP_CONST,
7573               .accessfn = access_aa64_tid3,
7574               .resetvalue = 0 },
7575             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7576               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7577               .access = PL1_R, .type = ARM_CP_CONST,
7578               .accessfn = access_aa64_tid3,
7579               .resetvalue = cpu->isar.id_aa64isar0 },
7580             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7581               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7582               .access = PL1_R, .type = ARM_CP_CONST,
7583               .accessfn = access_aa64_tid3,
7584               .resetvalue = cpu->isar.id_aa64isar1 },
7585             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7586               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7587               .access = PL1_R, .type = ARM_CP_CONST,
7588               .accessfn = access_aa64_tid3,
7589               .resetvalue = 0 },
7590             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7591               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7592               .access = PL1_R, .type = ARM_CP_CONST,
7593               .accessfn = access_aa64_tid3,
7594               .resetvalue = 0 },
7595             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7596               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7597               .access = PL1_R, .type = ARM_CP_CONST,
7598               .accessfn = access_aa64_tid3,
7599               .resetvalue = 0 },
7600             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7601               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7602               .access = PL1_R, .type = ARM_CP_CONST,
7603               .accessfn = access_aa64_tid3,
7604               .resetvalue = 0 },
7605             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7606               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7607               .access = PL1_R, .type = ARM_CP_CONST,
7608               .accessfn = access_aa64_tid3,
7609               .resetvalue = 0 },
7610             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7611               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7612               .access = PL1_R, .type = ARM_CP_CONST,
7613               .accessfn = access_aa64_tid3,
7614               .resetvalue = 0 },
7615             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7616               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7617               .access = PL1_R, .type = ARM_CP_CONST,
7618               .accessfn = access_aa64_tid3,
7619               .resetvalue = cpu->isar.id_aa64mmfr0 },
7620             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7621               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7622               .access = PL1_R, .type = ARM_CP_CONST,
7623               .accessfn = access_aa64_tid3,
7624               .resetvalue = cpu->isar.id_aa64mmfr1 },
7625             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7626               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7627               .access = PL1_R, .type = ARM_CP_CONST,
7628               .accessfn = access_aa64_tid3,
7629               .resetvalue = cpu->isar.id_aa64mmfr2 },
7630             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7631               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7632               .access = PL1_R, .type = ARM_CP_CONST,
7633               .accessfn = access_aa64_tid3,
7634               .resetvalue = 0 },
7635             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7636               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7637               .access = PL1_R, .type = ARM_CP_CONST,
7638               .accessfn = access_aa64_tid3,
7639               .resetvalue = 0 },
7640             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7641               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7642               .access = PL1_R, .type = ARM_CP_CONST,
7643               .accessfn = access_aa64_tid3,
7644               .resetvalue = 0 },
7645             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7646               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7647               .access = PL1_R, .type = ARM_CP_CONST,
7648               .accessfn = access_aa64_tid3,
7649               .resetvalue = 0 },
7650             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7651               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7652               .access = PL1_R, .type = ARM_CP_CONST,
7653               .accessfn = access_aa64_tid3,
7654               .resetvalue = 0 },
7655             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7656               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7657               .access = PL1_R, .type = ARM_CP_CONST,
7658               .accessfn = access_aa64_tid3,
7659               .resetvalue = cpu->isar.mvfr0 },
7660             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7661               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7662               .access = PL1_R, .type = ARM_CP_CONST,
7663               .accessfn = access_aa64_tid3,
7664               .resetvalue = cpu->isar.mvfr1 },
7665             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7666               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7667               .access = PL1_R, .type = ARM_CP_CONST,
7668               .accessfn = access_aa64_tid3,
7669               .resetvalue = cpu->isar.mvfr2 },
7670             /*
7671              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7672              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7673              * as RAZ, since it is in the "reserved for future ID
7674              * registers, RAZ" part of the AArch32 encoding space.
7675              */
7676             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7677               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7678               .access = PL1_R, .type = ARM_CP_CONST,
7679               .accessfn = access_aa64_tid3,
7680               .resetvalue = 0 },
7681             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7682               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7683               .access = PL1_R, .type = ARM_CP_CONST,
7684               .accessfn = access_aa64_tid3,
7685               .resetvalue = 0 },
7686             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7687               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7688               .access = PL1_R, .type = ARM_CP_CONST,
7689               .accessfn = access_aa64_tid3,
7690               .resetvalue = 0 },
7691             /*
7692              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7693              * they're also RAZ for AArch64, and in v8 are gradually
7694              * being filled with AArch64-view-of-AArch32-ID-register
7695              * for new ID registers.
7696              */
7697             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7698               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7699               .access = PL1_R, .type = ARM_CP_CONST,
7700               .accessfn = access_aa64_tid3,
7701               .resetvalue = 0 },
7702             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7703               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7704               .access = PL1_R, .type = ARM_CP_CONST,
7705               .accessfn = access_aa64_tid3,
7706               .resetvalue = cpu->isar.id_pfr2 },
7707             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7708               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7709               .access = PL1_R, .type = ARM_CP_CONST,
7710               .accessfn = access_aa64_tid3,
7711               .resetvalue = cpu->isar.id_dfr1 },
7712             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7713               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7714               .access = PL1_R, .type = ARM_CP_CONST,
7715               .accessfn = access_aa64_tid3,
7716               .resetvalue = cpu->isar.id_mmfr5 },
7717             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7718               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7719               .access = PL1_R, .type = ARM_CP_CONST,
7720               .accessfn = access_aa64_tid3,
7721               .resetvalue = 0 },
7722             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7723               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7724               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7725               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7726             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7727               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7728               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7729               .resetvalue = cpu->pmceid0 },
7730             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7731               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7732               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7733               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7734             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7735               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7736               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7737               .resetvalue = cpu->pmceid1 },
7738         };
7739 #ifdef CONFIG_USER_ONLY
7740         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7741             { .name = "ID_AA64PFR0_EL1",
7742               .exported_bits = 0x000f000f00ff0000,
7743               .fixed_bits    = 0x0000000000000011 },
7744             { .name = "ID_AA64PFR1_EL1",
7745               .exported_bits = 0x00000000000000f0 },
7746             { .name = "ID_AA64PFR*_EL1_RESERVED",
7747               .is_glob = true                     },
7748             { .name = "ID_AA64ZFR0_EL1"           },
7749             { .name = "ID_AA64MMFR0_EL1",
7750               .fixed_bits    = 0x00000000ff000000 },
7751             { .name = "ID_AA64MMFR1_EL1"          },
7752             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7753               .is_glob = true                     },
7754             { .name = "ID_AA64DFR0_EL1",
7755               .fixed_bits    = 0x0000000000000006 },
7756             { .name = "ID_AA64DFR1_EL1"           },
7757             { .name = "ID_AA64DFR*_EL1_RESERVED",
7758               .is_glob = true                     },
7759             { .name = "ID_AA64AFR*",
7760               .is_glob = true                     },
7761             { .name = "ID_AA64ISAR0_EL1",
7762               .exported_bits = 0x00fffffff0fffff0 },
7763             { .name = "ID_AA64ISAR1_EL1",
7764               .exported_bits = 0x000000f0ffffffff },
7765             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7766               .is_glob = true                     },
7767         };
7768         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7769 #endif
7770         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7771         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7772             !arm_feature(env, ARM_FEATURE_EL2)) {
7773             ARMCPRegInfo rvbar = {
7774                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7775                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7776                 .access = PL1_R,
7777                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7778             };
7779             define_one_arm_cp_reg(cpu, &rvbar);
7780         }
7781         define_arm_cp_regs(cpu, v8_idregs);
7782         define_arm_cp_regs(cpu, v8_cp_reginfo);
7783
7784         for (i = 4; i < 16; i++) {
7785             /*
7786              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7787              * For pre-v8 cores there are RAZ patterns for these in
7788              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7789              * v8 extends the "must RAZ" part of the ID register space
7790              * to also cover c0, 0, c{8-15}, {0-7}.
7791              * These are STATE_AA32 because in the AArch64 sysreg space
7792              * c4-c7 is where the AArch64 ID registers live (and we've
7793              * already defined those in v8_idregs[]), and c8-c15 are not
7794              * "must RAZ" for AArch64.
7795              */
7796             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7797             ARMCPRegInfo v8_aa32_raz_idregs = {
7798                 .name = name,
7799                 .state = ARM_CP_STATE_AA32,
7800                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7801                 .access = PL1_R, .type = ARM_CP_CONST,
7802                 .accessfn = access_aa64_tid3,
7803                 .resetvalue = 0 };
7804             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7805         }
7806     }
7807
7808     /*
7809      * Register the base EL2 cpregs.
7810      * Pre v8, these registers are implemented only as part of the
7811      * Virtualization Extensions (EL2 present).  Beginning with v8,
7812      * if EL2 is missing but EL3 is enabled, mostly these become
7813      * RES0 from EL3, with some specific exceptions.
7814      */
7815     if (arm_feature(env, ARM_FEATURE_EL2)
7816         || (arm_feature(env, ARM_FEATURE_EL3)
7817             && arm_feature(env, ARM_FEATURE_V8))) {
7818         uint64_t vmpidr_def = mpidr_read_val(env);
7819         ARMCPRegInfo vpidr_regs[] = {
7820             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7821               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7822               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7823               .resetvalue = cpu->midr,
7824               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7825               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7826             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7827               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7828               .access = PL2_RW, .resetvalue = cpu->midr,
7829               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7830               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7831             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7832               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7833               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7834               .resetvalue = vmpidr_def,
7835               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7836               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7837             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7838               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7839               .access = PL2_RW, .resetvalue = vmpidr_def,
7840               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7841               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7842         };
7843         /*
7844          * The only field of MDCR_EL2 that has a defined architectural reset
7845          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7846          */
7847         ARMCPRegInfo mdcr_el2 = {
7848             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
7849             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
7850             .writefn = mdcr_el2_write,
7851             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7852             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7853         };
7854         define_one_arm_cp_reg(cpu, &mdcr_el2);
7855         define_arm_cp_regs(cpu, vpidr_regs);
7856         define_arm_cp_regs(cpu, el2_cp_reginfo);
7857         if (arm_feature(env, ARM_FEATURE_V8)) {
7858             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7859         }
7860         if (cpu_isar_feature(aa64_sel2, cpu)) {
7861             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7862         }
7863         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7864         if (!arm_feature(env, ARM_FEATURE_EL3)) {
7865             ARMCPRegInfo rvbar = {
7866                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7867                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7868                 .access = PL2_R,
7869                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7870             };
7871             define_one_arm_cp_reg(cpu, &rvbar);
7872         }
7873     }
7874
7875     /* Register the base EL3 cpregs. */
7876     if (arm_feature(env, ARM_FEATURE_EL3)) {
7877         define_arm_cp_regs(cpu, el3_cp_reginfo);
7878         ARMCPRegInfo el3_regs[] = {
7879             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7880               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7881               .access = PL3_R,
7882               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7883             },
7884             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7885               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7886               .access = PL3_RW,
7887               .raw_writefn = raw_write, .writefn = sctlr_write,
7888               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7889               .resetvalue = cpu->reset_sctlr },
7890         };
7891
7892         define_arm_cp_regs(cpu, el3_regs);
7893     }
7894     /* The behaviour of NSACR is sufficiently various that we don't
7895      * try to describe it in a single reginfo:
7896      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
7897      *     reads as constant 0xc00 from NS EL1 and NS EL2
7898      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7899      *  if v7 without EL3, register doesn't exist
7900      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7901      */
7902     if (arm_feature(env, ARM_FEATURE_EL3)) {
7903         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7904             static const ARMCPRegInfo nsacr = {
7905                 .name = "NSACR", .type = ARM_CP_CONST,
7906                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7907                 .access = PL1_RW, .accessfn = nsacr_access,
7908                 .resetvalue = 0xc00
7909             };
7910             define_one_arm_cp_reg(cpu, &nsacr);
7911         } else {
7912             static const ARMCPRegInfo nsacr = {
7913                 .name = "NSACR",
7914                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7915                 .access = PL3_RW | PL1_R,
7916                 .resetvalue = 0,
7917                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7918             };
7919             define_one_arm_cp_reg(cpu, &nsacr);
7920         }
7921     } else {
7922         if (arm_feature(env, ARM_FEATURE_V8)) {
7923             static const ARMCPRegInfo nsacr = {
7924                 .name = "NSACR", .type = ARM_CP_CONST,
7925                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7926                 .access = PL1_R,
7927                 .resetvalue = 0xc00
7928             };
7929             define_one_arm_cp_reg(cpu, &nsacr);
7930         }
7931     }
7932
7933     if (arm_feature(env, ARM_FEATURE_PMSA)) {
7934         if (arm_feature(env, ARM_FEATURE_V6)) {
7935             /* PMSAv6 not implemented */
7936             assert(arm_feature(env, ARM_FEATURE_V7));
7937             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7938             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7939         } else {
7940             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7941         }
7942     } else {
7943         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7944         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
7945         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
7946         if (cpu_isar_feature(aa32_hpd, cpu)) {
7947             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7948         }
7949     }
7950     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7951         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7952     }
7953     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7954         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7955     }
7956     if (arm_feature(env, ARM_FEATURE_VAPA)) {
7957         define_arm_cp_regs(cpu, vapa_cp_reginfo);
7958     }
7959     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7960         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7961     }
7962     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7963         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7964     }
7965     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7966         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7967     }
7968     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7969         define_arm_cp_regs(cpu, omap_cp_reginfo);
7970     }
7971     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7972         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7973     }
7974     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7975         define_arm_cp_regs(cpu, xscale_cp_reginfo);
7976     }
7977     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7978         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7979     }
7980     if (arm_feature(env, ARM_FEATURE_LPAE)) {
7981         define_arm_cp_regs(cpu, lpae_cp_reginfo);
7982     }
7983     if (cpu_isar_feature(aa32_jazelle, cpu)) {
7984         define_arm_cp_regs(cpu, jazelle_regs);
7985     }
7986     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7987      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7988      * be read-only (ie write causes UNDEF exception).
7989      */
7990     {
7991         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7992             /* Pre-v8 MIDR space.
7993              * Note that the MIDR isn't a simple constant register because
7994              * of the TI925 behaviour where writes to another register can
7995              * cause the MIDR value to change.
7996              *
7997              * Unimplemented registers in the c15 0 0 0 space default to
7998              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7999              * and friends override accordingly.
8000              */
8001             { .name = "MIDR",
8002               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8003               .access = PL1_R, .resetvalue = cpu->midr,
8004               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8005               .readfn = midr_read,
8006               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8007               .type = ARM_CP_OVERRIDE },
8008             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8009             { .name = "DUMMY",
8010               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8011               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8012             { .name = "DUMMY",
8013               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8014               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8015             { .name = "DUMMY",
8016               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8017               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8018             { .name = "DUMMY",
8019               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8020               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8021             { .name = "DUMMY",
8022               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8023               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8024         };
8025         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8026             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8027               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8028               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8029               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8030               .readfn = midr_read },
8031             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8032             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8033               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8034               .access = PL1_R, .resetvalue = cpu->midr },
8035             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8036               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8037               .access = PL1_R, .resetvalue = cpu->midr },
8038             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8039               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8040               .access = PL1_R,
8041               .accessfn = access_aa64_tid1,
8042               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8043         };
8044         ARMCPRegInfo id_cp_reginfo[] = {
8045             /* These are common to v8 and pre-v8 */
8046             { .name = "CTR",
8047               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8048               .access = PL1_R, .accessfn = ctr_el0_access,
8049               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8050             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8051               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8052               .access = PL0_R, .accessfn = ctr_el0_access,
8053               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8054             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8055             { .name = "TCMTR",
8056               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8057               .access = PL1_R,
8058               .accessfn = access_aa32_tid1,
8059               .type = ARM_CP_CONST, .resetvalue = 0 },
8060         };
8061         /* TLBTR is specific to VMSA */
8062         ARMCPRegInfo id_tlbtr_reginfo = {
8063               .name = "TLBTR",
8064               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8065               .access = PL1_R,
8066               .accessfn = access_aa32_tid1,
8067               .type = ARM_CP_CONST, .resetvalue = 0,
8068         };
8069         /* MPUIR is specific to PMSA V6+ */
8070         ARMCPRegInfo id_mpuir_reginfo = {
8071               .name = "MPUIR",
8072               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8073               .access = PL1_R, .type = ARM_CP_CONST,
8074               .resetvalue = cpu->pmsav7_dregion << 8
8075         };
8076         static const ARMCPRegInfo crn0_wi_reginfo = {
8077             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8078             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8079             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8080         };
8081 #ifdef CONFIG_USER_ONLY
8082         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8083             { .name = "MIDR_EL1",
8084               .exported_bits = 0x00000000ffffffff },
8085             { .name = "REVIDR_EL1"                },
8086         };
8087         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8088 #endif
8089         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8090             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8091             size_t i;
8092             /* Register the blanket "writes ignored" value first to cover the
8093              * whole space. Then update the specific ID registers to allow write
8094              * access, so that they ignore writes rather than causing them to
8095              * UNDEF.
8096              */
8097             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8098             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8099                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8100             }
8101             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8102                 id_cp_reginfo[i].access = PL1_RW;
8103             }
8104             id_mpuir_reginfo.access = PL1_RW;
8105             id_tlbtr_reginfo.access = PL1_RW;
8106         }
8107         if (arm_feature(env, ARM_FEATURE_V8)) {
8108             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8109         } else {
8110             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8111         }
8112         define_arm_cp_regs(cpu, id_cp_reginfo);
8113         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8114             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8115         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8116             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8117         }
8118     }
8119
8120     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8121         ARMCPRegInfo mpidr_cp_reginfo[] = {
8122             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8123               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8124               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8125         };
8126 #ifdef CONFIG_USER_ONLY
8127         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8128             { .name = "MPIDR_EL1",
8129               .fixed_bits = 0x0000000080000000 },
8130         };
8131         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8132 #endif
8133         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8134     }
8135
8136     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8137         ARMCPRegInfo auxcr_reginfo[] = {
8138             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8139               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8140               .access = PL1_RW, .accessfn = access_tacr,
8141               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8142             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8143               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8144               .access = PL2_RW, .type = ARM_CP_CONST,
8145               .resetvalue = 0 },
8146             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8147               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8148               .access = PL3_RW, .type = ARM_CP_CONST,
8149               .resetvalue = 0 },
8150         };
8151         define_arm_cp_regs(cpu, auxcr_reginfo);
8152         if (cpu_isar_feature(aa32_ac2, cpu)) {
8153             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8154         }
8155     }
8156
8157     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8158         /*
8159          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8160          * There are two flavours:
8161          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8162          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8163          *      32-bit register visible to AArch32 at a different encoding
8164          *      to the "flavour 1" register and with the bits rearranged to
8165          *      be able to squash a 64-bit address into the 32-bit view.
8166          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8167          * in future if we support AArch32-only configs of some of the
8168          * AArch64 cores we might need to add a specific feature flag
8169          * to indicate cores with "flavour 2" CBAR.
8170          */
8171         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8172             /* 32 bit view is [31:18] 0...0 [43:32]. */
8173             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8174                 | extract64(cpu->reset_cbar, 32, 12);
8175             ARMCPRegInfo cbar_reginfo[] = {
8176                 { .name = "CBAR",
8177                   .type = ARM_CP_CONST,
8178                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8179                   .access = PL1_R, .resetvalue = cbar32 },
8180                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8181                   .type = ARM_CP_CONST,
8182                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8183                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8184             };
8185             /* We don't implement a r/w 64 bit CBAR currently */
8186             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8187             define_arm_cp_regs(cpu, cbar_reginfo);
8188         } else {
8189             ARMCPRegInfo cbar = {
8190                 .name = "CBAR",
8191                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8192                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8193                 .fieldoffset = offsetof(CPUARMState,
8194                                         cp15.c15_config_base_address)
8195             };
8196             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8197                 cbar.access = PL1_R;
8198                 cbar.fieldoffset = 0;
8199                 cbar.type = ARM_CP_CONST;
8200             }
8201             define_one_arm_cp_reg(cpu, &cbar);
8202         }
8203     }
8204
8205     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8206         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8207             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8208               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8209               .access = PL1_RW, .writefn = vbar_write,
8210               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8211                                      offsetof(CPUARMState, cp15.vbar_ns) },
8212               .resetvalue = 0 },
8213         };
8214         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8215     }
8216
8217     /* Generic registers whose values depend on the implementation */
8218     {
8219         ARMCPRegInfo sctlr = {
8220             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8221             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8222             .access = PL1_RW, .accessfn = access_tvm_trvm,
8223             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8224                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8225             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8226             .raw_writefn = raw_write,
8227         };
8228         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8229             /* Normally we would always end the TB on an SCTLR write, but Linux
8230              * arch/arm/mach-pxa/sleep.S expects two instructions following
8231              * an MMU enable to execute from cache.  Imitate this behaviour.
8232              */
8233             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8234         }
8235         define_one_arm_cp_reg(cpu, &sctlr);
8236     }
8237
8238     if (cpu_isar_feature(aa64_lor, cpu)) {
8239         define_arm_cp_regs(cpu, lor_reginfo);
8240     }
8241     if (cpu_isar_feature(aa64_pan, cpu)) {
8242         define_one_arm_cp_reg(cpu, &pan_reginfo);
8243     }
8244 #ifndef CONFIG_USER_ONLY
8245     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8246         define_arm_cp_regs(cpu, ats1e1_reginfo);
8247     }
8248     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8249         define_arm_cp_regs(cpu, ats1cp_reginfo);
8250     }
8251 #endif
8252     if (cpu_isar_feature(aa64_uao, cpu)) {
8253         define_one_arm_cp_reg(cpu, &uao_reginfo);
8254     }
8255
8256     if (cpu_isar_feature(aa64_dit, cpu)) {
8257         define_one_arm_cp_reg(cpu, &dit_reginfo);
8258     }
8259     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8260         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8261     }
8262     if (cpu_isar_feature(any_ras, cpu)) {
8263         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8264     }
8265
8266     if (cpu_isar_feature(aa64_vh, cpu) ||
8267         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8268         define_one_arm_cp_reg(cpu, &contextidr_el2);
8269     }
8270     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8271         define_arm_cp_regs(cpu, vhe_reginfo);
8272     }
8273
8274     if (cpu_isar_feature(aa64_sve, cpu)) {
8275         define_arm_cp_regs(cpu, zcr_reginfo);
8276     }
8277
8278     if (cpu_isar_feature(aa64_hcx, cpu)) {
8279         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8280     }
8281
8282 #ifdef TARGET_AARCH64
8283     if (cpu_isar_feature(aa64_sme, cpu)) {
8284         define_arm_cp_regs(cpu, sme_reginfo);
8285     }
8286     if (cpu_isar_feature(aa64_pauth, cpu)) {
8287         define_arm_cp_regs(cpu, pauth_reginfo);
8288     }
8289     if (cpu_isar_feature(aa64_rndr, cpu)) {
8290         define_arm_cp_regs(cpu, rndr_reginfo);
8291     }
8292     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8293         define_arm_cp_regs(cpu, tlbirange_reginfo);
8294     }
8295     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8296         define_arm_cp_regs(cpu, tlbios_reginfo);
8297     }
8298 #ifndef CONFIG_USER_ONLY
8299     /* Data Cache clean instructions up to PoP */
8300     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8301         define_one_arm_cp_reg(cpu, dcpop_reg);
8302
8303         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8304             define_one_arm_cp_reg(cpu, dcpodp_reg);
8305         }
8306     }
8307 #endif /*CONFIG_USER_ONLY*/
8308
8309     /*
8310      * If full MTE is enabled, add all of the system registers.
8311      * If only "instructions available at EL0" are enabled,
8312      * then define only a RAZ/WI version of PSTATE.TCO.
8313      */
8314     if (cpu_isar_feature(aa64_mte, cpu)) {
8315         define_arm_cp_regs(cpu, mte_reginfo);
8316         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8317     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8318         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8319         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8320     }
8321
8322     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8323         define_arm_cp_regs(cpu, scxtnum_reginfo);
8324     }
8325 #endif
8326
8327     if (cpu_isar_feature(any_predinv, cpu)) {
8328         define_arm_cp_regs(cpu, predinv_reginfo);
8329     }
8330
8331     if (cpu_isar_feature(any_ccidx, cpu)) {
8332         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8333     }
8334
8335 #ifndef CONFIG_USER_ONLY
8336     /*
8337      * Register redirections and aliases must be done last,
8338      * after the registers from the other extensions have been defined.
8339      */
8340     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8341         define_arm_vh_e2h_redirects_aliases(cpu);
8342     }
8343 #endif
8344 }
8345
8346 /* Sort alphabetically by type name, except for "any". */
8347 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8348 {
8349     ObjectClass *class_a = (ObjectClass *)a;
8350     ObjectClass *class_b = (ObjectClass *)b;
8351     const char *name_a, *name_b;
8352
8353     name_a = object_class_get_name(class_a);
8354     name_b = object_class_get_name(class_b);
8355     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8356         return 1;
8357     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8358         return -1;
8359     } else {
8360         return strcmp(name_a, name_b);
8361     }
8362 }
8363
8364 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8365 {
8366     ObjectClass *oc = data;
8367     CPUClass *cc = CPU_CLASS(oc);
8368     const char *typename;
8369     char *name;
8370
8371     typename = object_class_get_name(oc);
8372     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8373     if (cc->deprecation_note) {
8374         qemu_printf("  %s (deprecated)\n", name);
8375     } else {
8376         qemu_printf("  %s\n", name);
8377     }
8378     g_free(name);
8379 }
8380
8381 void arm_cpu_list(void)
8382 {
8383     GSList *list;
8384
8385     list = object_class_get_list(TYPE_ARM_CPU, false);
8386     list = g_slist_sort(list, arm_cpu_list_compare);
8387     qemu_printf("Available CPUs:\n");
8388     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8389     g_slist_free(list);
8390 }
8391
8392 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8393 {
8394     ObjectClass *oc = data;
8395     CpuDefinitionInfoList **cpu_list = user_data;
8396     CpuDefinitionInfo *info;
8397     const char *typename;
8398
8399     typename = object_class_get_name(oc);
8400     info = g_malloc0(sizeof(*info));
8401     info->name = g_strndup(typename,
8402                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8403     info->q_typename = g_strdup(typename);
8404
8405     QAPI_LIST_PREPEND(*cpu_list, info);
8406 }
8407
8408 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8409 {
8410     CpuDefinitionInfoList *cpu_list = NULL;
8411     GSList *list;
8412
8413     list = object_class_get_list(TYPE_ARM_CPU, false);
8414     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8415     g_slist_free(list);
8416
8417     return cpu_list;
8418 }
8419
8420 /*
8421  * Private utility function for define_one_arm_cp_reg_with_opaque():
8422  * add a single reginfo struct to the hash table.
8423  */
8424 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8425                                    void *opaque, CPState state,
8426                                    CPSecureState secstate,
8427                                    int crm, int opc1, int opc2,
8428                                    const char *name)
8429 {
8430     CPUARMState *env = &cpu->env;
8431     uint32_t key;
8432     ARMCPRegInfo *r2;
8433     bool is64 = r->type & ARM_CP_64BIT;
8434     bool ns = secstate & ARM_CP_SECSTATE_NS;
8435     int cp = r->cp;
8436     size_t name_len;
8437     bool make_const;
8438
8439     switch (state) {
8440     case ARM_CP_STATE_AA32:
8441         /* We assume it is a cp15 register if the .cp field is left unset. */
8442         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8443             cp = 15;
8444         }
8445         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8446         break;
8447     case ARM_CP_STATE_AA64:
8448         /*
8449          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8450          * cp == 0 as equivalent to the value for "standard guest-visible
8451          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8452          * in their AArch64 view (the .cp value may be non-zero for the
8453          * benefit of the AArch32 view).
8454          */
8455         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8456             cp = CP_REG_ARM64_SYSREG_CP;
8457         }
8458         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8459         break;
8460     default:
8461         g_assert_not_reached();
8462     }
8463
8464     /* Overriding of an existing definition must be explicitly requested. */
8465     if (!(r->type & ARM_CP_OVERRIDE)) {
8466         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8467         if (oldreg) {
8468             assert(oldreg->type & ARM_CP_OVERRIDE);
8469         }
8470     }
8471
8472     /*
8473      * Eliminate registers that are not present because the EL is missing.
8474      * Doing this here makes it easier to put all registers for a given
8475      * feature into the same ARMCPRegInfo array and define them all at once.
8476      */
8477     make_const = false;
8478     if (arm_feature(env, ARM_FEATURE_EL3)) {
8479         /*
8480          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8481          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8482          */
8483         int min_el = ctz32(r->access) / 2;
8484         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8485             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8486                 return;
8487             }
8488             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8489         }
8490     } else {
8491         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8492                                  ? PL2_RW : PL1_RW);
8493         if ((r->access & max_el) == 0) {
8494             return;
8495         }
8496     }
8497
8498     /* Combine cpreg and name into one allocation. */
8499     name_len = strlen(name) + 1;
8500     r2 = g_malloc(sizeof(*r2) + name_len);
8501     *r2 = *r;
8502     r2->name = memcpy(r2 + 1, name, name_len);
8503
8504     /*
8505      * Update fields to match the instantiation, overwiting wildcards
8506      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8507      */
8508     r2->cp = cp;
8509     r2->crm = crm;
8510     r2->opc1 = opc1;
8511     r2->opc2 = opc2;
8512     r2->state = state;
8513     r2->secure = secstate;
8514     if (opaque) {
8515         r2->opaque = opaque;
8516     }
8517
8518     if (make_const) {
8519         /* This should not have been a very special register to begin. */
8520         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8521         assert(old_special == 0 || old_special == ARM_CP_NOP);
8522         /*
8523          * Set the special function to CONST, retaining the other flags.
8524          * This is important for e.g. ARM_CP_SVE so that we still
8525          * take the SVE trap if CPTR_EL3.EZ == 0.
8526          */
8527         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8528         /*
8529          * Usually, these registers become RES0, but there are a few
8530          * special cases like VPIDR_EL2 which have a constant non-zero
8531          * value with writes ignored.
8532          */
8533         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8534             r2->resetvalue = 0;
8535         }
8536         /*
8537          * ARM_CP_CONST has precedence, so removing the callbacks and
8538          * offsets are not strictly necessary, but it is potentially
8539          * less confusing to debug later.
8540          */
8541         r2->readfn = NULL;
8542         r2->writefn = NULL;
8543         r2->raw_readfn = NULL;
8544         r2->raw_writefn = NULL;
8545         r2->resetfn = NULL;
8546         r2->fieldoffset = 0;
8547         r2->bank_fieldoffsets[0] = 0;
8548         r2->bank_fieldoffsets[1] = 0;
8549     } else {
8550         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8551
8552         if (isbanked) {
8553             /*
8554              * Register is banked (using both entries in array).
8555              * Overwriting fieldoffset as the array is only used to define
8556              * banked registers but later only fieldoffset is used.
8557              */
8558             r2->fieldoffset = r->bank_fieldoffsets[ns];
8559         }
8560         if (state == ARM_CP_STATE_AA32) {
8561             if (isbanked) {
8562                 /*
8563                  * If the register is banked then we don't need to migrate or
8564                  * reset the 32-bit instance in certain cases:
8565                  *
8566                  * 1) If the register has both 32-bit and 64-bit instances
8567                  *    then we can count on the 64-bit instance taking care
8568                  *    of the non-secure bank.
8569                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8570                  *    version taking care of the secure bank.  This requires
8571                  *    that separate 32 and 64-bit definitions are provided.
8572                  */
8573                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8574                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8575                     r2->type |= ARM_CP_ALIAS;
8576                 }
8577             } else if ((secstate != r->secure) && !ns) {
8578                 /*
8579                  * The register is not banked so we only want to allow
8580                  * migration of the non-secure instance.
8581                  */
8582                 r2->type |= ARM_CP_ALIAS;
8583             }
8584
8585             if (HOST_BIG_ENDIAN &&
8586                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8587                 r2->fieldoffset += sizeof(uint32_t);
8588             }
8589         }
8590     }
8591
8592     /*
8593      * By convention, for wildcarded registers only the first
8594      * entry is used for migration; the others are marked as
8595      * ALIAS so we don't try to transfer the register
8596      * multiple times. Special registers (ie NOP/WFI) are
8597      * never migratable and not even raw-accessible.
8598      */
8599     if (r2->type & ARM_CP_SPECIAL_MASK) {
8600         r2->type |= ARM_CP_NO_RAW;
8601     }
8602     if (((r->crm == CP_ANY) && crm != 0) ||
8603         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8604         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8605         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8606     }
8607
8608     /*
8609      * Check that raw accesses are either forbidden or handled. Note that
8610      * we can't assert this earlier because the setup of fieldoffset for
8611      * banked registers has to be done first.
8612      */
8613     if (!(r2->type & ARM_CP_NO_RAW)) {
8614         assert(!raw_accessors_invalid(r2));
8615     }
8616
8617     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8618 }
8619
8620
8621 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8622                                        const ARMCPRegInfo *r, void *opaque)
8623 {
8624     /* Define implementations of coprocessor registers.
8625      * We store these in a hashtable because typically
8626      * there are less than 150 registers in a space which
8627      * is 16*16*16*8*8 = 262144 in size.
8628      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8629      * If a register is defined twice then the second definition is
8630      * used, so this can be used to define some generic registers and
8631      * then override them with implementation specific variations.
8632      * At least one of the original and the second definition should
8633      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8634      * against accidental use.
8635      *
8636      * The state field defines whether the register is to be
8637      * visible in the AArch32 or AArch64 execution state. If the
8638      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8639      * reginfo structure for the AArch32 view, which sees the lower
8640      * 32 bits of the 64 bit register.
8641      *
8642      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8643      * be wildcarded. AArch64 registers are always considered to be 64
8644      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8645      * the register, if any.
8646      */
8647     int crm, opc1, opc2;
8648     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8649     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8650     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8651     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8652     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8653     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8654     CPState state;
8655
8656     /* 64 bit registers have only CRm and Opc1 fields */
8657     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8658     /* op0 only exists in the AArch64 encodings */
8659     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8660     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8661     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8662     /*
8663      * This API is only for Arm's system coprocessors (14 and 15) or
8664      * (M-profile or v7A-and-earlier only) for implementation defined
8665      * coprocessors in the range 0..7.  Our decode assumes this, since
8666      * 8..13 can be used for other insns including VFP and Neon. See
8667      * valid_cp() in translate.c.  Assert here that we haven't tried
8668      * to use an invalid coprocessor number.
8669      */
8670     switch (r->state) {
8671     case ARM_CP_STATE_BOTH:
8672         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8673         if (r->cp == 0) {
8674             break;
8675         }
8676         /* fall through */
8677     case ARM_CP_STATE_AA32:
8678         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8679             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8680             assert(r->cp >= 14 && r->cp <= 15);
8681         } else {
8682             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8683         }
8684         break;
8685     case ARM_CP_STATE_AA64:
8686         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8687         break;
8688     default:
8689         g_assert_not_reached();
8690     }
8691     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8692      * encodes a minimum access level for the register. We roll this
8693      * runtime check into our general permission check code, so check
8694      * here that the reginfo's specified permissions are strict enough
8695      * to encompass the generic architectural permission check.
8696      */
8697     if (r->state != ARM_CP_STATE_AA32) {
8698         CPAccessRights mask;
8699         switch (r->opc1) {
8700         case 0:
8701             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8702             mask = PL0U_R | PL1_RW;
8703             break;
8704         case 1: case 2:
8705             /* min_EL EL1 */
8706             mask = PL1_RW;
8707             break;
8708         case 3:
8709             /* min_EL EL0 */
8710             mask = PL0_RW;
8711             break;
8712         case 4:
8713         case 5:
8714             /* min_EL EL2 */
8715             mask = PL2_RW;
8716             break;
8717         case 6:
8718             /* min_EL EL3 */
8719             mask = PL3_RW;
8720             break;
8721         case 7:
8722             /* min_EL EL1, secure mode only (we don't check the latter) */
8723             mask = PL1_RW;
8724             break;
8725         default:
8726             /* broken reginfo with out-of-range opc1 */
8727             g_assert_not_reached();
8728         }
8729         /* assert our permissions are not too lax (stricter is fine) */
8730         assert((r->access & ~mask) == 0);
8731     }
8732
8733     /* Check that the register definition has enough info to handle
8734      * reads and writes if they are permitted.
8735      */
8736     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8737         if (r->access & PL3_R) {
8738             assert((r->fieldoffset ||
8739                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8740                    r->readfn);
8741         }
8742         if (r->access & PL3_W) {
8743             assert((r->fieldoffset ||
8744                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8745                    r->writefn);
8746         }
8747     }
8748
8749     for (crm = crmmin; crm <= crmmax; crm++) {
8750         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8751             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8752                 for (state = ARM_CP_STATE_AA32;
8753                      state <= ARM_CP_STATE_AA64; state++) {
8754                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8755                         continue;
8756                     }
8757                     if (state == ARM_CP_STATE_AA32) {
8758                         /* Under AArch32 CP registers can be common
8759                          * (same for secure and non-secure world) or banked.
8760                          */
8761                         char *name;
8762
8763                         switch (r->secure) {
8764                         case ARM_CP_SECSTATE_S:
8765                         case ARM_CP_SECSTATE_NS:
8766                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8767                                                    r->secure, crm, opc1, opc2,
8768                                                    r->name);
8769                             break;
8770                         case ARM_CP_SECSTATE_BOTH:
8771                             name = g_strdup_printf("%s_S", r->name);
8772                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8773                                                    ARM_CP_SECSTATE_S,
8774                                                    crm, opc1, opc2, name);
8775                             g_free(name);
8776                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8777                                                    ARM_CP_SECSTATE_NS,
8778                                                    crm, opc1, opc2, r->name);
8779                             break;
8780                         default:
8781                             g_assert_not_reached();
8782                         }
8783                     } else {
8784                         /* AArch64 registers get mapped to non-secure instance
8785                          * of AArch32 */
8786                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8787                                                ARM_CP_SECSTATE_NS,
8788                                                crm, opc1, opc2, r->name);
8789                     }
8790                 }
8791             }
8792         }
8793     }
8794 }
8795
8796 /* Define a whole list of registers */
8797 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8798                                         void *opaque, size_t len)
8799 {
8800     size_t i;
8801     for (i = 0; i < len; ++i) {
8802         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8803     }
8804 }
8805
8806 /*
8807  * Modify ARMCPRegInfo for access from userspace.
8808  *
8809  * This is a data driven modification directed by
8810  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8811  * user-space cannot alter any values and dynamic values pertaining to
8812  * execution state are hidden from user space view anyway.
8813  */
8814 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8815                                  const ARMCPRegUserSpaceInfo *mods,
8816                                  size_t mods_len)
8817 {
8818     for (size_t mi = 0; mi < mods_len; ++mi) {
8819         const ARMCPRegUserSpaceInfo *m = mods + mi;
8820         GPatternSpec *pat = NULL;
8821
8822         if (m->is_glob) {
8823             pat = g_pattern_spec_new(m->name);
8824         }
8825         for (size_t ri = 0; ri < regs_len; ++ri) {
8826             ARMCPRegInfo *r = regs + ri;
8827
8828             if (pat && g_pattern_match_string(pat, r->name)) {
8829                 r->type = ARM_CP_CONST;
8830                 r->access = PL0U_R;
8831                 r->resetvalue = 0;
8832                 /* continue */
8833             } else if (strcmp(r->name, m->name) == 0) {
8834                 r->type = ARM_CP_CONST;
8835                 r->access = PL0U_R;
8836                 r->resetvalue &= m->exported_bits;
8837                 r->resetvalue |= m->fixed_bits;
8838                 break;
8839             }
8840         }
8841         if (pat) {
8842             g_pattern_spec_free(pat);
8843         }
8844     }
8845 }
8846
8847 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8848 {
8849     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8850 }
8851
8852 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8853                          uint64_t value)
8854 {
8855     /* Helper coprocessor write function for write-ignore registers */
8856 }
8857
8858 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8859 {
8860     /* Helper coprocessor write function for read-as-zero registers */
8861     return 0;
8862 }
8863
8864 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8865 {
8866     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8867 }
8868
8869 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8870 {
8871     /* Return true if it is not valid for us to switch to
8872      * this CPU mode (ie all the UNPREDICTABLE cases in
8873      * the ARM ARM CPSRWriteByInstr pseudocode).
8874      */
8875
8876     /* Changes to or from Hyp via MSR and CPS are illegal. */
8877     if (write_type == CPSRWriteByInstr &&
8878         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8879          mode == ARM_CPU_MODE_HYP)) {
8880         return 1;
8881     }
8882
8883     switch (mode) {
8884     case ARM_CPU_MODE_USR:
8885         return 0;
8886     case ARM_CPU_MODE_SYS:
8887     case ARM_CPU_MODE_SVC:
8888     case ARM_CPU_MODE_ABT:
8889     case ARM_CPU_MODE_UND:
8890     case ARM_CPU_MODE_IRQ:
8891     case ARM_CPU_MODE_FIQ:
8892         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8893          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8894          */
8895         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8896          * and CPS are treated as illegal mode changes.
8897          */
8898         if (write_type == CPSRWriteByInstr &&
8899             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8900             (arm_hcr_el2_eff(env) & HCR_TGE)) {
8901             return 1;
8902         }
8903         return 0;
8904     case ARM_CPU_MODE_HYP:
8905         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8906     case ARM_CPU_MODE_MON:
8907         return arm_current_el(env) < 3;
8908     default:
8909         return 1;
8910     }
8911 }
8912
8913 uint32_t cpsr_read(CPUARMState *env)
8914 {
8915     int ZF;
8916     ZF = (env->ZF == 0);
8917     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8918         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8919         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8920         | ((env->condexec_bits & 0xfc) << 8)
8921         | (env->GE << 16) | (env->daif & CPSR_AIF);
8922 }
8923
8924 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8925                 CPSRWriteType write_type)
8926 {
8927     uint32_t changed_daif;
8928     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8929         (mask & (CPSR_M | CPSR_E | CPSR_IL));
8930
8931     if (mask & CPSR_NZCV) {
8932         env->ZF = (~val) & CPSR_Z;
8933         env->NF = val;
8934         env->CF = (val >> 29) & 1;
8935         env->VF = (val << 3) & 0x80000000;
8936     }
8937     if (mask & CPSR_Q)
8938         env->QF = ((val & CPSR_Q) != 0);
8939     if (mask & CPSR_T)
8940         env->thumb = ((val & CPSR_T) != 0);
8941     if (mask & CPSR_IT_0_1) {
8942         env->condexec_bits &= ~3;
8943         env->condexec_bits |= (val >> 25) & 3;
8944     }
8945     if (mask & CPSR_IT_2_7) {
8946         env->condexec_bits &= 3;
8947         env->condexec_bits |= (val >> 8) & 0xfc;
8948     }
8949     if (mask & CPSR_GE) {
8950         env->GE = (val >> 16) & 0xf;
8951     }
8952
8953     /* In a V7 implementation that includes the security extensions but does
8954      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8955      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8956      * bits respectively.
8957      *
8958      * In a V8 implementation, it is permitted for privileged software to
8959      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8960      */
8961     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
8962         arm_feature(env, ARM_FEATURE_EL3) &&
8963         !arm_feature(env, ARM_FEATURE_EL2) &&
8964         !arm_is_secure(env)) {
8965
8966         changed_daif = (env->daif ^ val) & mask;
8967
8968         if (changed_daif & CPSR_A) {
8969             /* Check to see if we are allowed to change the masking of async
8970              * abort exceptions from a non-secure state.
8971              */
8972             if (!(env->cp15.scr_el3 & SCR_AW)) {
8973                 qemu_log_mask(LOG_GUEST_ERROR,
8974                               "Ignoring attempt to switch CPSR_A flag from "
8975                               "non-secure world with SCR.AW bit clear\n");
8976                 mask &= ~CPSR_A;
8977             }
8978         }
8979
8980         if (changed_daif & CPSR_F) {
8981             /* Check to see if we are allowed to change the masking of FIQ
8982              * exceptions from a non-secure state.
8983              */
8984             if (!(env->cp15.scr_el3 & SCR_FW)) {
8985                 qemu_log_mask(LOG_GUEST_ERROR,
8986                               "Ignoring attempt to switch CPSR_F flag from "
8987                               "non-secure world with SCR.FW bit clear\n");
8988                 mask &= ~CPSR_F;
8989             }
8990
8991             /* Check whether non-maskable FIQ (NMFI) support is enabled.
8992              * If this bit is set software is not allowed to mask
8993              * FIQs, but is allowed to set CPSR_F to 0.
8994              */
8995             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8996                 (val & CPSR_F)) {
8997                 qemu_log_mask(LOG_GUEST_ERROR,
8998                               "Ignoring attempt to enable CPSR_F flag "
8999                               "(non-maskable FIQ [NMFI] support enabled)\n");
9000                 mask &= ~CPSR_F;
9001             }
9002         }
9003     }
9004
9005     env->daif &= ~(CPSR_AIF & mask);
9006     env->daif |= val & CPSR_AIF & mask;
9007
9008     if (write_type != CPSRWriteRaw &&
9009         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9010         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9011             /* Note that we can only get here in USR mode if this is a
9012              * gdb stub write; for this case we follow the architectural
9013              * behaviour for guest writes in USR mode of ignoring an attempt
9014              * to switch mode. (Those are caught by translate.c for writes
9015              * triggered by guest instructions.)
9016              */
9017             mask &= ~CPSR_M;
9018         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9019             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9020              * v7, and has defined behaviour in v8:
9021              *  + leave CPSR.M untouched
9022              *  + allow changes to the other CPSR fields
9023              *  + set PSTATE.IL
9024              * For user changes via the GDB stub, we don't set PSTATE.IL,
9025              * as this would be unnecessarily harsh for a user error.
9026              */
9027             mask &= ~CPSR_M;
9028             if (write_type != CPSRWriteByGDBStub &&
9029                 arm_feature(env, ARM_FEATURE_V8)) {
9030                 mask |= CPSR_IL;
9031                 val |= CPSR_IL;
9032             }
9033             qemu_log_mask(LOG_GUEST_ERROR,
9034                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9035                           aarch32_mode_name(env->uncached_cpsr),
9036                           aarch32_mode_name(val));
9037         } else {
9038             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9039                           write_type == CPSRWriteExceptionReturn ?
9040                           "Exception return from AArch32" :
9041                           "AArch32 mode switch from",
9042                           aarch32_mode_name(env->uncached_cpsr),
9043                           aarch32_mode_name(val), env->regs[15]);
9044             switch_mode(env, val & CPSR_M);
9045         }
9046     }
9047     mask &= ~CACHED_CPSR_BITS;
9048     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9049     if (rebuild_hflags) {
9050         arm_rebuild_hflags(env);
9051     }
9052 }
9053
9054 /* Sign/zero extend */
9055 uint32_t HELPER(sxtb16)(uint32_t x)
9056 {
9057     uint32_t res;
9058     res = (uint16_t)(int8_t)x;
9059     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9060     return res;
9061 }
9062
9063 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9064 {
9065     /*
9066      * Take a division-by-zero exception if necessary; otherwise return
9067      * to get the usual non-trapping division behaviour (result of 0)
9068      */
9069     if (arm_feature(env, ARM_FEATURE_M)
9070         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9071         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9072     }
9073 }
9074
9075 uint32_t HELPER(uxtb16)(uint32_t x)
9076 {
9077     uint32_t res;
9078     res = (uint16_t)(uint8_t)x;
9079     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9080     return res;
9081 }
9082
9083 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9084 {
9085     if (den == 0) {
9086         handle_possible_div0_trap(env, GETPC());
9087         return 0;
9088     }
9089     if (num == INT_MIN && den == -1) {
9090         return INT_MIN;
9091     }
9092     return num / den;
9093 }
9094
9095 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9096 {
9097     if (den == 0) {
9098         handle_possible_div0_trap(env, GETPC());
9099         return 0;
9100     }
9101     return num / den;
9102 }
9103
9104 uint32_t HELPER(rbit)(uint32_t x)
9105 {
9106     return revbit32(x);
9107 }
9108
9109 #ifdef CONFIG_USER_ONLY
9110
9111 static void switch_mode(CPUARMState *env, int mode)
9112 {
9113     ARMCPU *cpu = env_archcpu(env);
9114
9115     if (mode != ARM_CPU_MODE_USR) {
9116         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9117     }
9118 }
9119
9120 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9121                                  uint32_t cur_el, bool secure)
9122 {
9123     return 1;
9124 }
9125
9126 void aarch64_sync_64_to_32(CPUARMState *env)
9127 {
9128     g_assert_not_reached();
9129 }
9130
9131 #else
9132
9133 static void switch_mode(CPUARMState *env, int mode)
9134 {
9135     int old_mode;
9136     int i;
9137
9138     old_mode = env->uncached_cpsr & CPSR_M;
9139     if (mode == old_mode)
9140         return;
9141
9142     if (old_mode == ARM_CPU_MODE_FIQ) {
9143         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9144         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9145     } else if (mode == ARM_CPU_MODE_FIQ) {
9146         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9147         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9148     }
9149
9150     i = bank_number(old_mode);
9151     env->banked_r13[i] = env->regs[13];
9152     env->banked_spsr[i] = env->spsr;
9153
9154     i = bank_number(mode);
9155     env->regs[13] = env->banked_r13[i];
9156     env->spsr = env->banked_spsr[i];
9157
9158     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9159     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9160 }
9161
9162 /* Physical Interrupt Target EL Lookup Table
9163  *
9164  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9165  *
9166  * The below multi-dimensional table is used for looking up the target
9167  * exception level given numerous condition criteria.  Specifically, the
9168  * target EL is based on SCR and HCR routing controls as well as the
9169  * currently executing EL and secure state.
9170  *
9171  *    Dimensions:
9172  *    target_el_table[2][2][2][2][2][4]
9173  *                    |  |  |  |  |  +--- Current EL
9174  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9175  *                    |  |  |  +--------- HCR mask override
9176  *                    |  |  +------------ SCR exec state control
9177  *                    |  +--------------- SCR mask override
9178  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9179  *
9180  *    The table values are as such:
9181  *    0-3 = EL0-EL3
9182  *     -1 = Cannot occur
9183  *
9184  * The ARM ARM target EL table includes entries indicating that an "exception
9185  * is not taken".  The two cases where this is applicable are:
9186  *    1) An exception is taken from EL3 but the SCR does not have the exception
9187  *    routed to EL3.
9188  *    2) An exception is taken from EL2 but the HCR does not have the exception
9189  *    routed to EL2.
9190  * In these two cases, the below table contain a target of EL1.  This value is
9191  * returned as it is expected that the consumer of the table data will check
9192  * for "target EL >= current EL" to ensure the exception is not taken.
9193  *
9194  *            SCR     HCR
9195  *         64  EA     AMO                 From
9196  *        BIT IRQ     IMO      Non-secure         Secure
9197  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9198  */
9199 static const int8_t target_el_table[2][2][2][2][2][4] = {
9200     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9201        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9202       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9203        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9204      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9205        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9206       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9207        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9208     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9209        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9210       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9211        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9212      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9213        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9214       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9215        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9216 };
9217
9218 /*
9219  * Determine the target EL for physical exceptions
9220  */
9221 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9222                                  uint32_t cur_el, bool secure)
9223 {
9224     CPUARMState *env = cs->env_ptr;
9225     bool rw;
9226     bool scr;
9227     bool hcr;
9228     int target_el;
9229     /* Is the highest EL AArch64? */
9230     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9231     uint64_t hcr_el2;
9232
9233     if (arm_feature(env, ARM_FEATURE_EL3)) {
9234         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9235     } else {
9236         /* Either EL2 is the highest EL (and so the EL2 register width
9237          * is given by is64); or there is no EL2 or EL3, in which case
9238          * the value of 'rw' does not affect the table lookup anyway.
9239          */
9240         rw = is64;
9241     }
9242
9243     hcr_el2 = arm_hcr_el2_eff(env);
9244     switch (excp_idx) {
9245     case EXCP_IRQ:
9246         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9247         hcr = hcr_el2 & HCR_IMO;
9248         break;
9249     case EXCP_FIQ:
9250         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9251         hcr = hcr_el2 & HCR_FMO;
9252         break;
9253     default:
9254         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9255         hcr = hcr_el2 & HCR_AMO;
9256         break;
9257     };
9258
9259     /*
9260      * For these purposes, TGE and AMO/IMO/FMO both force the
9261      * interrupt to EL2.  Fold TGE into the bit extracted above.
9262      */
9263     hcr |= (hcr_el2 & HCR_TGE) != 0;
9264
9265     /* Perform a table-lookup for the target EL given the current state */
9266     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9267
9268     assert(target_el > 0);
9269
9270     return target_el;
9271 }
9272
9273 void arm_log_exception(CPUState *cs)
9274 {
9275     int idx = cs->exception_index;
9276
9277     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9278         const char *exc = NULL;
9279         static const char * const excnames[] = {
9280             [EXCP_UDEF] = "Undefined Instruction",
9281             [EXCP_SWI] = "SVC",
9282             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9283             [EXCP_DATA_ABORT] = "Data Abort",
9284             [EXCP_IRQ] = "IRQ",
9285             [EXCP_FIQ] = "FIQ",
9286             [EXCP_BKPT] = "Breakpoint",
9287             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9288             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9289             [EXCP_HVC] = "Hypervisor Call",
9290             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9291             [EXCP_SMC] = "Secure Monitor Call",
9292             [EXCP_VIRQ] = "Virtual IRQ",
9293             [EXCP_VFIQ] = "Virtual FIQ",
9294             [EXCP_SEMIHOST] = "Semihosting call",
9295             [EXCP_NOCP] = "v7M NOCP UsageFault",
9296             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9297             [EXCP_STKOF] = "v8M STKOF UsageFault",
9298             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9299             [EXCP_LSERR] = "v8M LSERR UsageFault",
9300             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9301             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9302             [EXCP_VSERR] = "Virtual SERR",
9303         };
9304
9305         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9306             exc = excnames[idx];
9307         }
9308         if (!exc) {
9309             exc = "unknown";
9310         }
9311         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9312                       idx, exc, cs->cpu_index);
9313     }
9314 }
9315
9316 /*
9317  * Function used to synchronize QEMU's AArch64 register set with AArch32
9318  * register set.  This is necessary when switching between AArch32 and AArch64
9319  * execution state.
9320  */
9321 void aarch64_sync_32_to_64(CPUARMState *env)
9322 {
9323     int i;
9324     uint32_t mode = env->uncached_cpsr & CPSR_M;
9325
9326     /* We can blanket copy R[0:7] to X[0:7] */
9327     for (i = 0; i < 8; i++) {
9328         env->xregs[i] = env->regs[i];
9329     }
9330
9331     /*
9332      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9333      * Otherwise, they come from the banked user regs.
9334      */
9335     if (mode == ARM_CPU_MODE_FIQ) {
9336         for (i = 8; i < 13; i++) {
9337             env->xregs[i] = env->usr_regs[i - 8];
9338         }
9339     } else {
9340         for (i = 8; i < 13; i++) {
9341             env->xregs[i] = env->regs[i];
9342         }
9343     }
9344
9345     /*
9346      * Registers x13-x23 are the various mode SP and FP registers. Registers
9347      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9348      * from the mode banked register.
9349      */
9350     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9351         env->xregs[13] = env->regs[13];
9352         env->xregs[14] = env->regs[14];
9353     } else {
9354         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9355         /* HYP is an exception in that it is copied from r14 */
9356         if (mode == ARM_CPU_MODE_HYP) {
9357             env->xregs[14] = env->regs[14];
9358         } else {
9359             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9360         }
9361     }
9362
9363     if (mode == ARM_CPU_MODE_HYP) {
9364         env->xregs[15] = env->regs[13];
9365     } else {
9366         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9367     }
9368
9369     if (mode == ARM_CPU_MODE_IRQ) {
9370         env->xregs[16] = env->regs[14];
9371         env->xregs[17] = env->regs[13];
9372     } else {
9373         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9374         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9375     }
9376
9377     if (mode == ARM_CPU_MODE_SVC) {
9378         env->xregs[18] = env->regs[14];
9379         env->xregs[19] = env->regs[13];
9380     } else {
9381         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9382         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9383     }
9384
9385     if (mode == ARM_CPU_MODE_ABT) {
9386         env->xregs[20] = env->regs[14];
9387         env->xregs[21] = env->regs[13];
9388     } else {
9389         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9390         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9391     }
9392
9393     if (mode == ARM_CPU_MODE_UND) {
9394         env->xregs[22] = env->regs[14];
9395         env->xregs[23] = env->regs[13];
9396     } else {
9397         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9398         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9399     }
9400
9401     /*
9402      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9403      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9404      * FIQ bank for r8-r14.
9405      */
9406     if (mode == ARM_CPU_MODE_FIQ) {
9407         for (i = 24; i < 31; i++) {
9408             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9409         }
9410     } else {
9411         for (i = 24; i < 29; i++) {
9412             env->xregs[i] = env->fiq_regs[i - 24];
9413         }
9414         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9415         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9416     }
9417
9418     env->pc = env->regs[15];
9419 }
9420
9421 /*
9422  * Function used to synchronize QEMU's AArch32 register set with AArch64
9423  * register set.  This is necessary when switching between AArch32 and AArch64
9424  * execution state.
9425  */
9426 void aarch64_sync_64_to_32(CPUARMState *env)
9427 {
9428     int i;
9429     uint32_t mode = env->uncached_cpsr & CPSR_M;
9430
9431     /* We can blanket copy X[0:7] to R[0:7] */
9432     for (i = 0; i < 8; i++) {
9433         env->regs[i] = env->xregs[i];
9434     }
9435
9436     /*
9437      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9438      * Otherwise, we copy x8-x12 into the banked user regs.
9439      */
9440     if (mode == ARM_CPU_MODE_FIQ) {
9441         for (i = 8; i < 13; i++) {
9442             env->usr_regs[i - 8] = env->xregs[i];
9443         }
9444     } else {
9445         for (i = 8; i < 13; i++) {
9446             env->regs[i] = env->xregs[i];
9447         }
9448     }
9449
9450     /*
9451      * Registers r13 & r14 depend on the current mode.
9452      * If we are in a given mode, we copy the corresponding x registers to r13
9453      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9454      * for the mode.
9455      */
9456     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9457         env->regs[13] = env->xregs[13];
9458         env->regs[14] = env->xregs[14];
9459     } else {
9460         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9461
9462         /*
9463          * HYP is an exception in that it does not have its own banked r14 but
9464          * shares the USR r14
9465          */
9466         if (mode == ARM_CPU_MODE_HYP) {
9467             env->regs[14] = env->xregs[14];
9468         } else {
9469             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9470         }
9471     }
9472
9473     if (mode == ARM_CPU_MODE_HYP) {
9474         env->regs[13] = env->xregs[15];
9475     } else {
9476         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9477     }
9478
9479     if (mode == ARM_CPU_MODE_IRQ) {
9480         env->regs[14] = env->xregs[16];
9481         env->regs[13] = env->xregs[17];
9482     } else {
9483         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9484         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9485     }
9486
9487     if (mode == ARM_CPU_MODE_SVC) {
9488         env->regs[14] = env->xregs[18];
9489         env->regs[13] = env->xregs[19];
9490     } else {
9491         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9492         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9493     }
9494
9495     if (mode == ARM_CPU_MODE_ABT) {
9496         env->regs[14] = env->xregs[20];
9497         env->regs[13] = env->xregs[21];
9498     } else {
9499         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9500         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9501     }
9502
9503     if (mode == ARM_CPU_MODE_UND) {
9504         env->regs[14] = env->xregs[22];
9505         env->regs[13] = env->xregs[23];
9506     } else {
9507         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9508         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9509     }
9510
9511     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9512      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9513      * FIQ bank for r8-r14.
9514      */
9515     if (mode == ARM_CPU_MODE_FIQ) {
9516         for (i = 24; i < 31; i++) {
9517             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9518         }
9519     } else {
9520         for (i = 24; i < 29; i++) {
9521             env->fiq_regs[i - 24] = env->xregs[i];
9522         }
9523         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9524         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9525     }
9526
9527     env->regs[15] = env->pc;
9528 }
9529
9530 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9531                                    uint32_t mask, uint32_t offset,
9532                                    uint32_t newpc)
9533 {
9534     int new_el;
9535
9536     /* Change the CPU state so as to actually take the exception. */
9537     switch_mode(env, new_mode);
9538
9539     /*
9540      * For exceptions taken to AArch32 we must clear the SS bit in both
9541      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9542      */
9543     env->pstate &= ~PSTATE_SS;
9544     env->spsr = cpsr_read(env);
9545     /* Clear IT bits.  */
9546     env->condexec_bits = 0;
9547     /* Switch to the new mode, and to the correct instruction set.  */
9548     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9549
9550     /* This must be after mode switching. */
9551     new_el = arm_current_el(env);
9552
9553     /* Set new mode endianness */
9554     env->uncached_cpsr &= ~CPSR_E;
9555     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9556         env->uncached_cpsr |= CPSR_E;
9557     }
9558     /* J and IL must always be cleared for exception entry */
9559     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9560     env->daif |= mask;
9561
9562     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9563         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9564             env->uncached_cpsr |= CPSR_SSBS;
9565         } else {
9566             env->uncached_cpsr &= ~CPSR_SSBS;
9567         }
9568     }
9569
9570     if (new_mode == ARM_CPU_MODE_HYP) {
9571         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9572         env->elr_el[2] = env->regs[15];
9573     } else {
9574         /* CPSR.PAN is normally preserved preserved unless...  */
9575         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9576             switch (new_el) {
9577             case 3:
9578                 if (!arm_is_secure_below_el3(env)) {
9579                     /* ... the target is EL3, from non-secure state.  */
9580                     env->uncached_cpsr &= ~CPSR_PAN;
9581                     break;
9582                 }
9583                 /* ... the target is EL3, from secure state ... */
9584                 /* fall through */
9585             case 1:
9586                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9587                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9588                     env->uncached_cpsr |= CPSR_PAN;
9589                 }
9590                 break;
9591             }
9592         }
9593         /*
9594          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9595          * and we should just guard the thumb mode on V4
9596          */
9597         if (arm_feature(env, ARM_FEATURE_V4T)) {
9598             env->thumb =
9599                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9600         }
9601         env->regs[14] = env->regs[15] + offset;
9602     }
9603     env->regs[15] = newpc;
9604     arm_rebuild_hflags(env);
9605 }
9606
9607 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9608 {
9609     /*
9610      * Handle exception entry to Hyp mode; this is sufficiently
9611      * different to entry to other AArch32 modes that we handle it
9612      * separately here.
9613      *
9614      * The vector table entry used is always the 0x14 Hyp mode entry point,
9615      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9616      * The offset applied to the preferred return address is always zero
9617      * (see DDI0487C.a section G1.12.3).
9618      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9619      */
9620     uint32_t addr, mask;
9621     ARMCPU *cpu = ARM_CPU(cs);
9622     CPUARMState *env = &cpu->env;
9623
9624     switch (cs->exception_index) {
9625     case EXCP_UDEF:
9626         addr = 0x04;
9627         break;
9628     case EXCP_SWI:
9629         addr = 0x08;
9630         break;
9631     case EXCP_BKPT:
9632         /* Fall through to prefetch abort.  */
9633     case EXCP_PREFETCH_ABORT:
9634         env->cp15.ifar_s = env->exception.vaddress;
9635         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9636                       (uint32_t)env->exception.vaddress);
9637         addr = 0x0c;
9638         break;
9639     case EXCP_DATA_ABORT:
9640         env->cp15.dfar_s = env->exception.vaddress;
9641         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9642                       (uint32_t)env->exception.vaddress);
9643         addr = 0x10;
9644         break;
9645     case EXCP_IRQ:
9646         addr = 0x18;
9647         break;
9648     case EXCP_FIQ:
9649         addr = 0x1c;
9650         break;
9651     case EXCP_HVC:
9652         addr = 0x08;
9653         break;
9654     case EXCP_HYP_TRAP:
9655         addr = 0x14;
9656         break;
9657     default:
9658         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9659     }
9660
9661     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9662         if (!arm_feature(env, ARM_FEATURE_V8)) {
9663             /*
9664              * QEMU syndrome values are v8-style. v7 has the IL bit
9665              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9666              * If this is a v7 CPU, squash the IL bit in those cases.
9667              */
9668             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9669                 (cs->exception_index == EXCP_DATA_ABORT &&
9670                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9671                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9672                 env->exception.syndrome &= ~ARM_EL_IL;
9673             }
9674         }
9675         env->cp15.esr_el[2] = env->exception.syndrome;
9676     }
9677
9678     if (arm_current_el(env) != 2 && addr < 0x14) {
9679         addr = 0x14;
9680     }
9681
9682     mask = 0;
9683     if (!(env->cp15.scr_el3 & SCR_EA)) {
9684         mask |= CPSR_A;
9685     }
9686     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9687         mask |= CPSR_I;
9688     }
9689     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9690         mask |= CPSR_F;
9691     }
9692
9693     addr += env->cp15.hvbar;
9694
9695     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9696 }
9697
9698 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9699 {
9700     ARMCPU *cpu = ARM_CPU(cs);
9701     CPUARMState *env = &cpu->env;
9702     uint32_t addr;
9703     uint32_t mask;
9704     int new_mode;
9705     uint32_t offset;
9706     uint32_t moe;
9707
9708     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9709     switch (syn_get_ec(env->exception.syndrome)) {
9710     case EC_BREAKPOINT:
9711     case EC_BREAKPOINT_SAME_EL:
9712         moe = 1;
9713         break;
9714     case EC_WATCHPOINT:
9715     case EC_WATCHPOINT_SAME_EL:
9716         moe = 10;
9717         break;
9718     case EC_AA32_BKPT:
9719         moe = 3;
9720         break;
9721     case EC_VECTORCATCH:
9722         moe = 5;
9723         break;
9724     default:
9725         moe = 0;
9726         break;
9727     }
9728
9729     if (moe) {
9730         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9731     }
9732
9733     if (env->exception.target_el == 2) {
9734         arm_cpu_do_interrupt_aarch32_hyp(cs);
9735         return;
9736     }
9737
9738     switch (cs->exception_index) {
9739     case EXCP_UDEF:
9740         new_mode = ARM_CPU_MODE_UND;
9741         addr = 0x04;
9742         mask = CPSR_I;
9743         if (env->thumb)
9744             offset = 2;
9745         else
9746             offset = 4;
9747         break;
9748     case EXCP_SWI:
9749         new_mode = ARM_CPU_MODE_SVC;
9750         addr = 0x08;
9751         mask = CPSR_I;
9752         /* The PC already points to the next instruction.  */
9753         offset = 0;
9754         break;
9755     case EXCP_BKPT:
9756         /* Fall through to prefetch abort.  */
9757     case EXCP_PREFETCH_ABORT:
9758         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9759         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9760         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9761                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9762         new_mode = ARM_CPU_MODE_ABT;
9763         addr = 0x0c;
9764         mask = CPSR_A | CPSR_I;
9765         offset = 4;
9766         break;
9767     case EXCP_DATA_ABORT:
9768         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9769         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9770         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9771                       env->exception.fsr,
9772                       (uint32_t)env->exception.vaddress);
9773         new_mode = ARM_CPU_MODE_ABT;
9774         addr = 0x10;
9775         mask = CPSR_A | CPSR_I;
9776         offset = 8;
9777         break;
9778     case EXCP_IRQ:
9779         new_mode = ARM_CPU_MODE_IRQ;
9780         addr = 0x18;
9781         /* Disable IRQ and imprecise data aborts.  */
9782         mask = CPSR_A | CPSR_I;
9783         offset = 4;
9784         if (env->cp15.scr_el3 & SCR_IRQ) {
9785             /* IRQ routed to monitor mode */
9786             new_mode = ARM_CPU_MODE_MON;
9787             mask |= CPSR_F;
9788         }
9789         break;
9790     case EXCP_FIQ:
9791         new_mode = ARM_CPU_MODE_FIQ;
9792         addr = 0x1c;
9793         /* Disable FIQ, IRQ and imprecise data aborts.  */
9794         mask = CPSR_A | CPSR_I | CPSR_F;
9795         if (env->cp15.scr_el3 & SCR_FIQ) {
9796             /* FIQ routed to monitor mode */
9797             new_mode = ARM_CPU_MODE_MON;
9798         }
9799         offset = 4;
9800         break;
9801     case EXCP_VIRQ:
9802         new_mode = ARM_CPU_MODE_IRQ;
9803         addr = 0x18;
9804         /* Disable IRQ and imprecise data aborts.  */
9805         mask = CPSR_A | CPSR_I;
9806         offset = 4;
9807         break;
9808     case EXCP_VFIQ:
9809         new_mode = ARM_CPU_MODE_FIQ;
9810         addr = 0x1c;
9811         /* Disable FIQ, IRQ and imprecise data aborts.  */
9812         mask = CPSR_A | CPSR_I | CPSR_F;
9813         offset = 4;
9814         break;
9815     case EXCP_VSERR:
9816         {
9817             /*
9818              * Note that this is reported as a data abort, but the DFAR
9819              * has an UNKNOWN value.  Construct the SError syndrome from
9820              * AET and ExT fields.
9821              */
9822             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9823
9824             if (extended_addresses_enabled(env)) {
9825                 env->exception.fsr = arm_fi_to_lfsc(&fi);
9826             } else {
9827                 env->exception.fsr = arm_fi_to_sfsc(&fi);
9828             }
9829             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9830             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9831             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9832                           env->exception.fsr);
9833
9834             new_mode = ARM_CPU_MODE_ABT;
9835             addr = 0x10;
9836             mask = CPSR_A | CPSR_I;
9837             offset = 8;
9838         }
9839         break;
9840     case EXCP_SMC:
9841         new_mode = ARM_CPU_MODE_MON;
9842         addr = 0x08;
9843         mask = CPSR_A | CPSR_I | CPSR_F;
9844         offset = 0;
9845         break;
9846     default:
9847         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9848         return; /* Never happens.  Keep compiler happy.  */
9849     }
9850
9851     if (new_mode == ARM_CPU_MODE_MON) {
9852         addr += env->cp15.mvbar;
9853     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9854         /* High vectors. When enabled, base address cannot be remapped. */
9855         addr += 0xffff0000;
9856     } else {
9857         /* ARM v7 architectures provide a vector base address register to remap
9858          * the interrupt vector table.
9859          * This register is only followed in non-monitor mode, and is banked.
9860          * Note: only bits 31:5 are valid.
9861          */
9862         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9863     }
9864
9865     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9866         env->cp15.scr_el3 &= ~SCR_NS;
9867     }
9868
9869     take_aarch32_exception(env, new_mode, mask, offset, addr);
9870 }
9871
9872 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9873 {
9874     /*
9875      * Return the register number of the AArch64 view of the AArch32
9876      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9877      * be that of the AArch32 mode the exception came from.
9878      */
9879     int mode = env->uncached_cpsr & CPSR_M;
9880
9881     switch (aarch32_reg) {
9882     case 0 ... 7:
9883         return aarch32_reg;
9884     case 8 ... 12:
9885         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9886     case 13:
9887         switch (mode) {
9888         case ARM_CPU_MODE_USR:
9889         case ARM_CPU_MODE_SYS:
9890             return 13;
9891         case ARM_CPU_MODE_HYP:
9892             return 15;
9893         case ARM_CPU_MODE_IRQ:
9894             return 17;
9895         case ARM_CPU_MODE_SVC:
9896             return 19;
9897         case ARM_CPU_MODE_ABT:
9898             return 21;
9899         case ARM_CPU_MODE_UND:
9900             return 23;
9901         case ARM_CPU_MODE_FIQ:
9902             return 29;
9903         default:
9904             g_assert_not_reached();
9905         }
9906     case 14:
9907         switch (mode) {
9908         case ARM_CPU_MODE_USR:
9909         case ARM_CPU_MODE_SYS:
9910         case ARM_CPU_MODE_HYP:
9911             return 14;
9912         case ARM_CPU_MODE_IRQ:
9913             return 16;
9914         case ARM_CPU_MODE_SVC:
9915             return 18;
9916         case ARM_CPU_MODE_ABT:
9917             return 20;
9918         case ARM_CPU_MODE_UND:
9919             return 22;
9920         case ARM_CPU_MODE_FIQ:
9921             return 30;
9922         default:
9923             g_assert_not_reached();
9924         }
9925     case 15:
9926         return 31;
9927     default:
9928         g_assert_not_reached();
9929     }
9930 }
9931
9932 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9933 {
9934     uint32_t ret = cpsr_read(env);
9935
9936     /* Move DIT to the correct location for SPSR_ELx */
9937     if (ret & CPSR_DIT) {
9938         ret &= ~CPSR_DIT;
9939         ret |= PSTATE_DIT;
9940     }
9941     /* Merge PSTATE.SS into SPSR_ELx */
9942     ret |= env->pstate & PSTATE_SS;
9943
9944     return ret;
9945 }
9946
9947 static bool syndrome_is_sync_extabt(uint32_t syndrome)
9948 {
9949     /* Return true if this syndrome value is a synchronous external abort */
9950     switch (syn_get_ec(syndrome)) {
9951     case EC_INSNABORT:
9952     case EC_INSNABORT_SAME_EL:
9953     case EC_DATAABORT:
9954     case EC_DATAABORT_SAME_EL:
9955         /* Look at fault status code for all the synchronous ext abort cases */
9956         switch (syndrome & 0x3f) {
9957         case 0x10:
9958         case 0x13:
9959         case 0x14:
9960         case 0x15:
9961         case 0x16:
9962         case 0x17:
9963             return true;
9964         default:
9965             return false;
9966         }
9967     default:
9968         return false;
9969     }
9970 }
9971
9972 /* Handle exception entry to a target EL which is using AArch64 */
9973 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9974 {
9975     ARMCPU *cpu = ARM_CPU(cs);
9976     CPUARMState *env = &cpu->env;
9977     unsigned int new_el = env->exception.target_el;
9978     target_ulong addr = env->cp15.vbar_el[new_el];
9979     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9980     unsigned int old_mode;
9981     unsigned int cur_el = arm_current_el(env);
9982     int rt;
9983
9984     /*
9985      * Note that new_el can never be 0.  If cur_el is 0, then
9986      * el0_a64 is is_a64(), else el0_a64 is ignored.
9987      */
9988     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9989
9990     if (cur_el < new_el) {
9991         /* Entry vector offset depends on whether the implemented EL
9992          * immediately lower than the target level is using AArch32 or AArch64
9993          */
9994         bool is_aa64;
9995         uint64_t hcr;
9996
9997         switch (new_el) {
9998         case 3:
9999             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10000             break;
10001         case 2:
10002             hcr = arm_hcr_el2_eff(env);
10003             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10004                 is_aa64 = (hcr & HCR_RW) != 0;
10005                 break;
10006             }
10007             /* fall through */
10008         case 1:
10009             is_aa64 = is_a64(env);
10010             break;
10011         default:
10012             g_assert_not_reached();
10013         }
10014
10015         if (is_aa64) {
10016             addr += 0x400;
10017         } else {
10018             addr += 0x600;
10019         }
10020     } else if (pstate_read(env) & PSTATE_SP) {
10021         addr += 0x200;
10022     }
10023
10024     switch (cs->exception_index) {
10025     case EXCP_PREFETCH_ABORT:
10026     case EXCP_DATA_ABORT:
10027         /*
10028          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10029          * to be taken to the SError vector entrypoint.
10030          */
10031         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10032             syndrome_is_sync_extabt(env->exception.syndrome)) {
10033             addr += 0x180;
10034         }
10035         env->cp15.far_el[new_el] = env->exception.vaddress;
10036         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10037                       env->cp15.far_el[new_el]);
10038         /* fall through */
10039     case EXCP_BKPT:
10040     case EXCP_UDEF:
10041     case EXCP_SWI:
10042     case EXCP_HVC:
10043     case EXCP_HYP_TRAP:
10044     case EXCP_SMC:
10045         switch (syn_get_ec(env->exception.syndrome)) {
10046         case EC_ADVSIMDFPACCESSTRAP:
10047             /*
10048              * QEMU internal FP/SIMD syndromes from AArch32 include the
10049              * TA and coproc fields which are only exposed if the exception
10050              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10051              * AArch64 format syndrome.
10052              */
10053             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10054             break;
10055         case EC_CP14RTTRAP:
10056         case EC_CP15RTTRAP:
10057         case EC_CP14DTTRAP:
10058             /*
10059              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10060              * the raw register field from the insn; when taking this to
10061              * AArch64 we must convert it to the AArch64 view of the register
10062              * number. Notice that we read a 4-bit AArch32 register number and
10063              * write back a 5-bit AArch64 one.
10064              */
10065             rt = extract32(env->exception.syndrome, 5, 4);
10066             rt = aarch64_regnum(env, rt);
10067             env->exception.syndrome = deposit32(env->exception.syndrome,
10068                                                 5, 5, rt);
10069             break;
10070         case EC_CP15RRTTRAP:
10071         case EC_CP14RRTTRAP:
10072             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10073             rt = extract32(env->exception.syndrome, 5, 4);
10074             rt = aarch64_regnum(env, rt);
10075             env->exception.syndrome = deposit32(env->exception.syndrome,
10076                                                 5, 5, rt);
10077             rt = extract32(env->exception.syndrome, 10, 4);
10078             rt = aarch64_regnum(env, rt);
10079             env->exception.syndrome = deposit32(env->exception.syndrome,
10080                                                 10, 5, rt);
10081             break;
10082         }
10083         env->cp15.esr_el[new_el] = env->exception.syndrome;
10084         break;
10085     case EXCP_IRQ:
10086     case EXCP_VIRQ:
10087         addr += 0x80;
10088         break;
10089     case EXCP_FIQ:
10090     case EXCP_VFIQ:
10091         addr += 0x100;
10092         break;
10093     case EXCP_VSERR:
10094         addr += 0x180;
10095         /* Construct the SError syndrome from IDS and ISS fields. */
10096         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10097         env->cp15.esr_el[new_el] = env->exception.syndrome;
10098         break;
10099     default:
10100         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10101     }
10102
10103     if (is_a64(env)) {
10104         old_mode = pstate_read(env);
10105         aarch64_save_sp(env, arm_current_el(env));
10106         env->elr_el[new_el] = env->pc;
10107     } else {
10108         old_mode = cpsr_read_for_spsr_elx(env);
10109         env->elr_el[new_el] = env->regs[15];
10110
10111         aarch64_sync_32_to_64(env);
10112
10113         env->condexec_bits = 0;
10114     }
10115     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10116
10117     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10118                   env->elr_el[new_el]);
10119
10120     if (cpu_isar_feature(aa64_pan, cpu)) {
10121         /* The value of PSTATE.PAN is normally preserved, except when ... */
10122         new_mode |= old_mode & PSTATE_PAN;
10123         switch (new_el) {
10124         case 2:
10125             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10126             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10127                 != (HCR_E2H | HCR_TGE)) {
10128                 break;
10129             }
10130             /* fall through */
10131         case 1:
10132             /* ... the target is EL1 ... */
10133             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10134             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10135                 new_mode |= PSTATE_PAN;
10136             }
10137             break;
10138         }
10139     }
10140     if (cpu_isar_feature(aa64_mte, cpu)) {
10141         new_mode |= PSTATE_TCO;
10142     }
10143
10144     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10145         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10146             new_mode |= PSTATE_SSBS;
10147         } else {
10148             new_mode &= ~PSTATE_SSBS;
10149         }
10150     }
10151
10152     pstate_write(env, PSTATE_DAIF | new_mode);
10153     env->aarch64 = true;
10154     aarch64_restore_sp(env, new_el);
10155     helper_rebuild_hflags_a64(env, new_el);
10156
10157     env->pc = addr;
10158
10159     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10160                   new_el, env->pc, pstate_read(env));
10161 }
10162
10163 /*
10164  * Do semihosting call and set the appropriate return value. All the
10165  * permission and validity checks have been done at translate time.
10166  *
10167  * We only see semihosting exceptions in TCG only as they are not
10168  * trapped to the hypervisor in KVM.
10169  */
10170 #ifdef CONFIG_TCG
10171 static void handle_semihosting(CPUState *cs)
10172 {
10173     ARMCPU *cpu = ARM_CPU(cs);
10174     CPUARMState *env = &cpu->env;
10175
10176     if (is_a64(env)) {
10177         qemu_log_mask(CPU_LOG_INT,
10178                       "...handling as semihosting call 0x%" PRIx64 "\n",
10179                       env->xregs[0]);
10180         do_common_semihosting(cs);
10181         env->pc += 4;
10182     } else {
10183         qemu_log_mask(CPU_LOG_INT,
10184                       "...handling as semihosting call 0x%x\n",
10185                       env->regs[0]);
10186         do_common_semihosting(cs);
10187         env->regs[15] += env->thumb ? 2 : 4;
10188     }
10189 }
10190 #endif
10191
10192 /* Handle a CPU exception for A and R profile CPUs.
10193  * Do any appropriate logging, handle PSCI calls, and then hand off
10194  * to the AArch64-entry or AArch32-entry function depending on the
10195  * target exception level's register width.
10196  *
10197  * Note: this is used for both TCG (as the do_interrupt tcg op),
10198  *       and KVM to re-inject guest debug exceptions, and to
10199  *       inject a Synchronous-External-Abort.
10200  */
10201 void arm_cpu_do_interrupt(CPUState *cs)
10202 {
10203     ARMCPU *cpu = ARM_CPU(cs);
10204     CPUARMState *env = &cpu->env;
10205     unsigned int new_el = env->exception.target_el;
10206
10207     assert(!arm_feature(env, ARM_FEATURE_M));
10208
10209     arm_log_exception(cs);
10210     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10211                   new_el);
10212     if (qemu_loglevel_mask(CPU_LOG_INT)
10213         && !excp_is_internal(cs->exception_index)) {
10214         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10215                       syn_get_ec(env->exception.syndrome),
10216                       env->exception.syndrome);
10217     }
10218
10219     if (arm_is_psci_call(cpu, cs->exception_index)) {
10220         arm_handle_psci_call(cpu);
10221         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10222         return;
10223     }
10224
10225     /*
10226      * Semihosting semantics depend on the register width of the code
10227      * that caused the exception, not the target exception level, so
10228      * must be handled here.
10229      */
10230 #ifdef CONFIG_TCG
10231     if (cs->exception_index == EXCP_SEMIHOST) {
10232         handle_semihosting(cs);
10233         return;
10234     }
10235 #endif
10236
10237     /* Hooks may change global state so BQL should be held, also the
10238      * BQL needs to be held for any modification of
10239      * cs->interrupt_request.
10240      */
10241     g_assert(qemu_mutex_iothread_locked());
10242
10243     arm_call_pre_el_change_hook(cpu);
10244
10245     assert(!excp_is_internal(cs->exception_index));
10246     if (arm_el_is_aa64(env, new_el)) {
10247         arm_cpu_do_interrupt_aarch64(cs);
10248     } else {
10249         arm_cpu_do_interrupt_aarch32(cs);
10250     }
10251
10252     arm_call_el_change_hook(cpu);
10253
10254     if (!kvm_enabled()) {
10255         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10256     }
10257 }
10258 #endif /* !CONFIG_USER_ONLY */
10259
10260 uint64_t arm_sctlr(CPUARMState *env, int el)
10261 {
10262     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10263     if (el == 0) {
10264         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10265         el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10266              ? 2 : 1;
10267     }
10268     return env->cp15.sctlr_el[el];
10269 }
10270
10271 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10272 {
10273     if (regime_has_2_ranges(mmu_idx)) {
10274         return extract64(tcr, 37, 2);
10275     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10276         return 0; /* VTCR_EL2 */
10277     } else {
10278         /* Replicate the single TBI bit so we always have 2 bits.  */
10279         return extract32(tcr, 20, 1) * 3;
10280     }
10281 }
10282
10283 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10284 {
10285     if (regime_has_2_ranges(mmu_idx)) {
10286         return extract64(tcr, 51, 2);
10287     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10288         return 0; /* VTCR_EL2 */
10289     } else {
10290         /* Replicate the single TBID bit so we always have 2 bits.  */
10291         return extract32(tcr, 29, 1) * 3;
10292     }
10293 }
10294
10295 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10296 {
10297     if (regime_has_2_ranges(mmu_idx)) {
10298         return extract64(tcr, 57, 2);
10299     } else {
10300         /* Replicate the single TCMA bit so we always have 2 bits.  */
10301         return extract32(tcr, 30, 1) * 3;
10302     }
10303 }
10304
10305 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10306                                    ARMMMUIdx mmu_idx, bool data)
10307 {
10308     uint64_t tcr = regime_tcr(env, mmu_idx);
10309     bool epd, hpd, using16k, using64k, tsz_oob, ds;
10310     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10311     ARMCPU *cpu = env_archcpu(env);
10312
10313     if (!regime_has_2_ranges(mmu_idx)) {
10314         select = 0;
10315         tsz = extract32(tcr, 0, 6);
10316         using64k = extract32(tcr, 14, 1);
10317         using16k = extract32(tcr, 15, 1);
10318         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10319             /* VTCR_EL2 */
10320             hpd = false;
10321         } else {
10322             hpd = extract32(tcr, 24, 1);
10323         }
10324         epd = false;
10325         sh = extract32(tcr, 12, 2);
10326         ps = extract32(tcr, 16, 3);
10327         ds = extract64(tcr, 32, 1);
10328     } else {
10329         /*
10330          * Bit 55 is always between the two regions, and is canonical for
10331          * determining if address tagging is enabled.
10332          */
10333         select = extract64(va, 55, 1);
10334         if (!select) {
10335             tsz = extract32(tcr, 0, 6);
10336             epd = extract32(tcr, 7, 1);
10337             sh = extract32(tcr, 12, 2);
10338             using64k = extract32(tcr, 14, 1);
10339             using16k = extract32(tcr, 15, 1);
10340             hpd = extract64(tcr, 41, 1);
10341         } else {
10342             int tg = extract32(tcr, 30, 2);
10343             using16k = tg == 1;
10344             using64k = tg == 3;
10345             tsz = extract32(tcr, 16, 6);
10346             epd = extract32(tcr, 23, 1);
10347             sh = extract32(tcr, 28, 2);
10348             hpd = extract64(tcr, 42, 1);
10349         }
10350         ps = extract64(tcr, 32, 3);
10351         ds = extract64(tcr, 59, 1);
10352     }
10353
10354     if (cpu_isar_feature(aa64_st, cpu)) {
10355         max_tsz = 48 - using64k;
10356     } else {
10357         max_tsz = 39;
10358     }
10359
10360     /*
10361      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10362      * adjust the effective value of DS, as documented.
10363      */
10364     min_tsz = 16;
10365     if (using64k) {
10366         if (cpu_isar_feature(aa64_lva, cpu)) {
10367             min_tsz = 12;
10368         }
10369         ds = false;
10370     } else if (ds) {
10371         switch (mmu_idx) {
10372         case ARMMMUIdx_Stage2:
10373         case ARMMMUIdx_Stage2_S:
10374             if (using16k) {
10375                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10376             } else {
10377                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10378             }
10379             break;
10380         default:
10381             if (using16k) {
10382                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10383             } else {
10384                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10385             }
10386             break;
10387         }
10388         if (ds) {
10389             min_tsz = 12;
10390         }
10391     }
10392
10393     if (tsz > max_tsz) {
10394         tsz = max_tsz;
10395         tsz_oob = true;
10396     } else if (tsz < min_tsz) {
10397         tsz = min_tsz;
10398         tsz_oob = true;
10399     } else {
10400         tsz_oob = false;
10401     }
10402
10403     /* Present TBI as a composite with TBID.  */
10404     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10405     if (!data) {
10406         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10407     }
10408     tbi = (tbi >> select) & 1;
10409
10410     return (ARMVAParameters) {
10411         .tsz = tsz,
10412         .ps = ps,
10413         .sh = sh,
10414         .select = select,
10415         .tbi = tbi,
10416         .epd = epd,
10417         .hpd = hpd,
10418         .using16k = using16k,
10419         .using64k = using64k,
10420         .tsz_oob = tsz_oob,
10421         .ds = ds,
10422     };
10423 }
10424
10425 /* Note that signed overflow is undefined in C.  The following routines are
10426    careful to use unsigned types where modulo arithmetic is required.
10427    Failure to do so _will_ break on newer gcc.  */
10428
10429 /* Signed saturating arithmetic.  */
10430
10431 /* Perform 16-bit signed saturating addition.  */
10432 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10433 {
10434     uint16_t res;
10435
10436     res = a + b;
10437     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10438         if (a & 0x8000)
10439             res = 0x8000;
10440         else
10441             res = 0x7fff;
10442     }
10443     return res;
10444 }
10445
10446 /* Perform 8-bit signed saturating addition.  */
10447 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10448 {
10449     uint8_t res;
10450
10451     res = a + b;
10452     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10453         if (a & 0x80)
10454             res = 0x80;
10455         else
10456             res = 0x7f;
10457     }
10458     return res;
10459 }
10460
10461 /* Perform 16-bit signed saturating subtraction.  */
10462 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10463 {
10464     uint16_t res;
10465
10466     res = a - b;
10467     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10468         if (a & 0x8000)
10469             res = 0x8000;
10470         else
10471             res = 0x7fff;
10472     }
10473     return res;
10474 }
10475
10476 /* Perform 8-bit signed saturating subtraction.  */
10477 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10478 {
10479     uint8_t res;
10480
10481     res = a - b;
10482     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10483         if (a & 0x80)
10484             res = 0x80;
10485         else
10486             res = 0x7f;
10487     }
10488     return res;
10489 }
10490
10491 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10492 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10493 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10494 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10495 #define PFX q
10496
10497 #include "op_addsub.h"
10498
10499 /* Unsigned saturating arithmetic.  */
10500 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10501 {
10502     uint16_t res;
10503     res = a + b;
10504     if (res < a)
10505         res = 0xffff;
10506     return res;
10507 }
10508
10509 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10510 {
10511     if (a > b)
10512         return a - b;
10513     else
10514         return 0;
10515 }
10516
10517 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10518 {
10519     uint8_t res;
10520     res = a + b;
10521     if (res < a)
10522         res = 0xff;
10523     return res;
10524 }
10525
10526 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10527 {
10528     if (a > b)
10529         return a - b;
10530     else
10531         return 0;
10532 }
10533
10534 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10535 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10536 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10537 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10538 #define PFX uq
10539
10540 #include "op_addsub.h"
10541
10542 /* Signed modulo arithmetic.  */
10543 #define SARITH16(a, b, n, op) do { \
10544     int32_t sum; \
10545     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10546     RESULT(sum, n, 16); \
10547     if (sum >= 0) \
10548         ge |= 3 << (n * 2); \
10549     } while(0)
10550
10551 #define SARITH8(a, b, n, op) do { \
10552     int32_t sum; \
10553     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10554     RESULT(sum, n, 8); \
10555     if (sum >= 0) \
10556         ge |= 1 << n; \
10557     } while(0)
10558
10559
10560 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10561 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10562 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10563 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10564 #define PFX s
10565 #define ARITH_GE
10566
10567 #include "op_addsub.h"
10568
10569 /* Unsigned modulo arithmetic.  */
10570 #define ADD16(a, b, n) do { \
10571     uint32_t sum; \
10572     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10573     RESULT(sum, n, 16); \
10574     if ((sum >> 16) == 1) \
10575         ge |= 3 << (n * 2); \
10576     } while(0)
10577
10578 #define ADD8(a, b, n) do { \
10579     uint32_t sum; \
10580     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10581     RESULT(sum, n, 8); \
10582     if ((sum >> 8) == 1) \
10583         ge |= 1 << n; \
10584     } while(0)
10585
10586 #define SUB16(a, b, n) do { \
10587     uint32_t sum; \
10588     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10589     RESULT(sum, n, 16); \
10590     if ((sum >> 16) == 0) \
10591         ge |= 3 << (n * 2); \
10592     } while(0)
10593
10594 #define SUB8(a, b, n) do { \
10595     uint32_t sum; \
10596     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10597     RESULT(sum, n, 8); \
10598     if ((sum >> 8) == 0) \
10599         ge |= 1 << n; \
10600     } while(0)
10601
10602 #define PFX u
10603 #define ARITH_GE
10604
10605 #include "op_addsub.h"
10606
10607 /* Halved signed arithmetic.  */
10608 #define ADD16(a, b, n) \
10609   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10610 #define SUB16(a, b, n) \
10611   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10612 #define ADD8(a, b, n) \
10613   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10614 #define SUB8(a, b, n) \
10615   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10616 #define PFX sh
10617
10618 #include "op_addsub.h"
10619
10620 /* Halved unsigned arithmetic.  */
10621 #define ADD16(a, b, n) \
10622   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10623 #define SUB16(a, b, n) \
10624   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10625 #define ADD8(a, b, n) \
10626   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10627 #define SUB8(a, b, n) \
10628   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10629 #define PFX uh
10630
10631 #include "op_addsub.h"
10632
10633 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10634 {
10635     if (a > b)
10636         return a - b;
10637     else
10638         return b - a;
10639 }
10640
10641 /* Unsigned sum of absolute byte differences.  */
10642 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10643 {
10644     uint32_t sum;
10645     sum = do_usad(a, b);
10646     sum += do_usad(a >> 8, b >> 8);
10647     sum += do_usad(a >> 16, b >> 16);
10648     sum += do_usad(a >> 24, b >> 24);
10649     return sum;
10650 }
10651
10652 /* For ARMv6 SEL instruction.  */
10653 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10654 {
10655     uint32_t mask;
10656
10657     mask = 0;
10658     if (flags & 1)
10659         mask |= 0xff;
10660     if (flags & 2)
10661         mask |= 0xff00;
10662     if (flags & 4)
10663         mask |= 0xff0000;
10664     if (flags & 8)
10665         mask |= 0xff000000;
10666     return (a & mask) | (b & ~mask);
10667 }
10668
10669 /* CRC helpers.
10670  * The upper bytes of val (above the number specified by 'bytes') must have
10671  * been zeroed out by the caller.
10672  */
10673 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10674 {
10675     uint8_t buf[4];
10676
10677     stl_le_p(buf, val);
10678
10679     /* zlib crc32 converts the accumulator and output to one's complement.  */
10680     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10681 }
10682
10683 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10684 {
10685     uint8_t buf[4];
10686
10687     stl_le_p(buf, val);
10688
10689     /* Linux crc32c converts the output to one's complement.  */
10690     return crc32c(acc, buf, bytes) ^ 0xffffffff;
10691 }
10692
10693 /* Return the exception level to which FP-disabled exceptions should
10694  * be taken, or 0 if FP is enabled.
10695  */
10696 int fp_exception_el(CPUARMState *env, int cur_el)
10697 {
10698 #ifndef CONFIG_USER_ONLY
10699     uint64_t hcr_el2;
10700
10701     /* CPACR and the CPTR registers don't exist before v6, so FP is
10702      * always accessible
10703      */
10704     if (!arm_feature(env, ARM_FEATURE_V6)) {
10705         return 0;
10706     }
10707
10708     if (arm_feature(env, ARM_FEATURE_M)) {
10709         /* CPACR can cause a NOCP UsageFault taken to current security state */
10710         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10711             return 1;
10712         }
10713
10714         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10715             if (!extract32(env->v7m.nsacr, 10, 1)) {
10716                 /* FP insns cause a NOCP UsageFault taken to Secure */
10717                 return 3;
10718             }
10719         }
10720
10721         return 0;
10722     }
10723
10724     hcr_el2 = arm_hcr_el2_eff(env);
10725
10726     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10727      * 0, 2 : trap EL0 and EL1/PL1 accesses
10728      * 1    : trap only EL0 accesses
10729      * 3    : trap no accesses
10730      * This register is ignored if E2H+TGE are both set.
10731      */
10732     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10733         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10734
10735         switch (fpen) {
10736         case 1:
10737             if (cur_el != 0) {
10738                 break;
10739             }
10740             /* fall through */
10741         case 0:
10742         case 2:
10743             /* Trap from Secure PL0 or PL1 to Secure PL1. */
10744             if (!arm_el_is_aa64(env, 3)
10745                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10746                 return 3;
10747             }
10748             if (cur_el <= 1) {
10749                 return 1;
10750             }
10751             break;
10752         }
10753     }
10754
10755     /*
10756      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10757      * to control non-secure access to the FPU. It doesn't have any
10758      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10759      */
10760     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10761          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10762         if (!extract32(env->cp15.nsacr, 10, 1)) {
10763             /* FP insns act as UNDEF */
10764             return cur_el == 2 ? 2 : 1;
10765         }
10766     }
10767
10768     /*
10769      * CPTR_EL2 is present in v7VE or v8, and changes format
10770      * with HCR_EL2.E2H (regardless of TGE).
10771      */
10772     if (cur_el <= 2) {
10773         if (hcr_el2 & HCR_E2H) {
10774             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
10775             case 1:
10776                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10777                     break;
10778                 }
10779                 /* fall through */
10780             case 0:
10781             case 2:
10782                 return 2;
10783             }
10784         } else if (arm_is_el2_enabled(env)) {
10785             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
10786                 return 2;
10787             }
10788         }
10789     }
10790
10791     /* CPTR_EL3 : present in v8 */
10792     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
10793         /* Trap all FP ops to EL3 */
10794         return 3;
10795     }
10796 #endif
10797     return 0;
10798 }
10799
10800 /* Return the exception level we're running at if this is our mmu_idx */
10801 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
10802 {
10803     if (mmu_idx & ARM_MMU_IDX_M) {
10804         return mmu_idx & ARM_MMU_IDX_M_PRIV;
10805     }
10806
10807     switch (mmu_idx) {
10808     case ARMMMUIdx_E10_0:
10809     case ARMMMUIdx_E20_0:
10810     case ARMMMUIdx_SE10_0:
10811     case ARMMMUIdx_SE20_0:
10812         return 0;
10813     case ARMMMUIdx_E10_1:
10814     case ARMMMUIdx_E10_1_PAN:
10815     case ARMMMUIdx_SE10_1:
10816     case ARMMMUIdx_SE10_1_PAN:
10817         return 1;
10818     case ARMMMUIdx_E2:
10819     case ARMMMUIdx_E20_2:
10820     case ARMMMUIdx_E20_2_PAN:
10821     case ARMMMUIdx_SE2:
10822     case ARMMMUIdx_SE20_2:
10823     case ARMMMUIdx_SE20_2_PAN:
10824         return 2;
10825     case ARMMMUIdx_SE3:
10826         return 3;
10827     default:
10828         g_assert_not_reached();
10829     }
10830 }
10831
10832 #ifndef CONFIG_TCG
10833 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10834 {
10835     g_assert_not_reached();
10836 }
10837 #endif
10838
10839 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
10840 {
10841     ARMMMUIdx idx;
10842     uint64_t hcr;
10843
10844     if (arm_feature(env, ARM_FEATURE_M)) {
10845         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
10846     }
10847
10848     /* See ARM pseudo-function ELIsInHost.  */
10849     switch (el) {
10850     case 0:
10851         hcr = arm_hcr_el2_eff(env);
10852         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
10853             idx = ARMMMUIdx_E20_0;
10854         } else {
10855             idx = ARMMMUIdx_E10_0;
10856         }
10857         break;
10858     case 1:
10859         if (env->pstate & PSTATE_PAN) {
10860             idx = ARMMMUIdx_E10_1_PAN;
10861         } else {
10862             idx = ARMMMUIdx_E10_1;
10863         }
10864         break;
10865     case 2:
10866         /* Note that TGE does not apply at EL2.  */
10867         if (arm_hcr_el2_eff(env) & HCR_E2H) {
10868             if (env->pstate & PSTATE_PAN) {
10869                 idx = ARMMMUIdx_E20_2_PAN;
10870             } else {
10871                 idx = ARMMMUIdx_E20_2;
10872             }
10873         } else {
10874             idx = ARMMMUIdx_E2;
10875         }
10876         break;
10877     case 3:
10878         return ARMMMUIdx_SE3;
10879     default:
10880         g_assert_not_reached();
10881     }
10882
10883     if (arm_is_secure_below_el3(env)) {
10884         idx &= ~ARM_MMU_IDX_A_NS;
10885     }
10886
10887     return idx;
10888 }
10889
10890 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
10891 {
10892     return arm_mmu_idx_el(env, arm_current_el(env));
10893 }
10894
10895 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
10896                                            ARMMMUIdx mmu_idx,
10897                                            CPUARMTBFlags flags)
10898 {
10899     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
10900     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
10901
10902     if (arm_singlestep_active(env)) {
10903         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
10904     }
10905     return flags;
10906 }
10907
10908 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
10909                                               ARMMMUIdx mmu_idx,
10910                                               CPUARMTBFlags flags)
10911 {
10912     bool sctlr_b = arm_sctlr_b(env);
10913
10914     if (sctlr_b) {
10915         DP_TBFLAG_A32(flags, SCTLR__B, 1);
10916     }
10917     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
10918         DP_TBFLAG_ANY(flags, BE_DATA, 1);
10919     }
10920     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
10921
10922     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
10923 }
10924
10925 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
10926                                         ARMMMUIdx mmu_idx)
10927 {
10928     CPUARMTBFlags flags = {};
10929     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
10930
10931     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
10932     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
10933         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10934     }
10935
10936     if (arm_v7m_is_handler_mode(env)) {
10937         DP_TBFLAG_M32(flags, HANDLER, 1);
10938     }
10939
10940     /*
10941      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
10942      * is suppressing them because the requested execution priority
10943      * is less than 0.
10944      */
10945     if (arm_feature(env, ARM_FEATURE_V8) &&
10946         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
10947           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
10948         DP_TBFLAG_M32(flags, STACKCHECK, 1);
10949     }
10950
10951     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
10952         DP_TBFLAG_M32(flags, SECURE, 1);
10953     }
10954
10955     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10956 }
10957
10958 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
10959                                         ARMMMUIdx mmu_idx)
10960 {
10961     CPUARMTBFlags flags = {};
10962     int el = arm_current_el(env);
10963
10964     if (arm_sctlr(env, el) & SCTLR_A) {
10965         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10966     }
10967
10968     if (arm_el_is_aa64(env, 1)) {
10969         DP_TBFLAG_A32(flags, VFPEN, 1);
10970     }
10971
10972     if (el < 2 && env->cp15.hstr_el2 &&
10973         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10974         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
10975     }
10976
10977     if (env->uncached_cpsr & CPSR_IL) {
10978         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
10979     }
10980
10981     /*
10982      * The SME exception we are testing for is raised via
10983      * AArch64.CheckFPAdvSIMDEnabled(), as called from
10984      * AArch32.CheckAdvSIMDOrFPEnabled().
10985      */
10986     if (el == 0
10987         && FIELD_EX64(env->svcr, SVCR, SM)
10988         && (!arm_is_el2_enabled(env)
10989             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
10990         && arm_el_is_aa64(env, 1)
10991         && !sme_fa64(env, el)) {
10992         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
10993     }
10994
10995     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10996 }
10997
10998 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
10999                                         ARMMMUIdx mmu_idx)
11000 {
11001     CPUARMTBFlags flags = {};
11002     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11003     uint64_t tcr = regime_tcr(env, mmu_idx);
11004     uint64_t sctlr;
11005     int tbii, tbid;
11006
11007     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11008
11009     /* Get control bits for tagged addresses.  */
11010     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11011     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11012
11013     DP_TBFLAG_A64(flags, TBII, tbii);
11014     DP_TBFLAG_A64(flags, TBID, tbid);
11015
11016     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11017         int sve_el = sve_exception_el(env, el);
11018
11019         /*
11020          * If either FP or SVE are disabled, translator does not need len.
11021          * If SVE EL > FP EL, FP exception has precedence, and translator
11022          * does not need SVE EL.  Save potential re-translations by forcing
11023          * the unneeded data to zero.
11024          */
11025         if (fp_el != 0) {
11026             if (sve_el > fp_el) {
11027                 sve_el = 0;
11028             }
11029         } else if (sve_el == 0) {
11030             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11031         }
11032         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11033     }
11034     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11035         int sme_el = sme_exception_el(env, el);
11036         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11037
11038         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11039         if (sme_el == 0) {
11040             /* Similarly, do not compute SVL if SME is disabled. */
11041             int svl = sve_vqm1_for_el_sm(env, el, true);
11042             DP_TBFLAG_A64(flags, SVL, svl);
11043             if (sm) {
11044                 /* If SVE is disabled, we will not have set VL above. */
11045                 DP_TBFLAG_A64(flags, VL, svl);
11046             }
11047         }
11048         if (sm) {
11049             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11050             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11051         }
11052         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11053     }
11054
11055     sctlr = regime_sctlr(env, stage1);
11056
11057     if (sctlr & SCTLR_A) {
11058         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11059     }
11060
11061     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11062         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11063     }
11064
11065     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11066         /*
11067          * In order to save space in flags, we record only whether
11068          * pauth is "inactive", meaning all insns are implemented as
11069          * a nop, or "active" when some action must be performed.
11070          * The decision of which action to take is left to a helper.
11071          */
11072         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11073             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11074         }
11075     }
11076
11077     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11078         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11079         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11080             DP_TBFLAG_A64(flags, BT, 1);
11081         }
11082     }
11083
11084     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11085     if (!(env->pstate & PSTATE_UAO)) {
11086         switch (mmu_idx) {
11087         case ARMMMUIdx_E10_1:
11088         case ARMMMUIdx_E10_1_PAN:
11089         case ARMMMUIdx_SE10_1:
11090         case ARMMMUIdx_SE10_1_PAN:
11091             /* TODO: ARMv8.3-NV */
11092             DP_TBFLAG_A64(flags, UNPRIV, 1);
11093             break;
11094         case ARMMMUIdx_E20_2:
11095         case ARMMMUIdx_E20_2_PAN:
11096         case ARMMMUIdx_SE20_2:
11097         case ARMMMUIdx_SE20_2_PAN:
11098             /*
11099              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11100              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11101              */
11102             if (env->cp15.hcr_el2 & HCR_TGE) {
11103                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11104             }
11105             break;
11106         default:
11107             break;
11108         }
11109     }
11110
11111     if (env->pstate & PSTATE_IL) {
11112         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11113     }
11114
11115     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11116         /*
11117          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11118          * if all accesses must be Unchecked:
11119          * 1) If no TBI, then there are no tags in the address to check,
11120          * 2) If Tag Check Override, then all accesses are Unchecked,
11121          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11122          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11123          */
11124         if (allocation_tag_access_enabled(env, el, sctlr)) {
11125             DP_TBFLAG_A64(flags, ATA, 1);
11126             if (tbid
11127                 && !(env->pstate & PSTATE_TCO)
11128                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11129                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11130             }
11131         }
11132         /* And again for unprivileged accesses, if required.  */
11133         if (EX_TBFLAG_A64(flags, UNPRIV)
11134             && tbid
11135             && !(env->pstate & PSTATE_TCO)
11136             && (sctlr & SCTLR_TCF0)
11137             && allocation_tag_access_enabled(env, 0, sctlr)) {
11138             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11139         }
11140         /* Cache TCMA as well as TBI. */
11141         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11142     }
11143
11144     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11145 }
11146
11147 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11148 {
11149     int el = arm_current_el(env);
11150     int fp_el = fp_exception_el(env, el);
11151     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11152
11153     if (is_a64(env)) {
11154         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11155     } else if (arm_feature(env, ARM_FEATURE_M)) {
11156         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11157     } else {
11158         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11159     }
11160 }
11161
11162 void arm_rebuild_hflags(CPUARMState *env)
11163 {
11164     env->hflags = rebuild_hflags_internal(env);
11165 }
11166
11167 /*
11168  * If we have triggered a EL state change we can't rely on the
11169  * translator having passed it to us, we need to recompute.
11170  */
11171 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11172 {
11173     int el = arm_current_el(env);
11174     int fp_el = fp_exception_el(env, el);
11175     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11176
11177     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11178 }
11179
11180 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11181 {
11182     int fp_el = fp_exception_el(env, el);
11183     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11184
11185     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11186 }
11187
11188 /*
11189  * If we have triggered a EL state change we can't rely on the
11190  * translator having passed it to us, we need to recompute.
11191  */
11192 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11193 {
11194     int el = arm_current_el(env);
11195     int fp_el = fp_exception_el(env, el);
11196     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11197     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11198 }
11199
11200 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11201 {
11202     int fp_el = fp_exception_el(env, el);
11203     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11204
11205     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11206 }
11207
11208 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11209 {
11210     int fp_el = fp_exception_el(env, el);
11211     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11212
11213     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11214 }
11215
11216 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11217 {
11218 #ifdef CONFIG_DEBUG_TCG
11219     CPUARMTBFlags c = env->hflags;
11220     CPUARMTBFlags r = rebuild_hflags_internal(env);
11221
11222     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11223         fprintf(stderr, "TCG hflags mismatch "
11224                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11225                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11226                 c.flags, c.flags2, r.flags, r.flags2);
11227         abort();
11228     }
11229 #endif
11230 }
11231
11232 static bool mve_no_pred(CPUARMState *env)
11233 {
11234     /*
11235      * Return true if there is definitely no predication of MVE
11236      * instructions by VPR or LTPSIZE. (Returning false even if there
11237      * isn't any predication is OK; generated code will just be
11238      * a little worse.)
11239      * If the CPU does not implement MVE then this TB flag is always 0.
11240      *
11241      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11242      * logic in gen_update_fp_context() needs to be updated to match.
11243      *
11244      * We do not include the effect of the ECI bits here -- they are
11245      * tracked in other TB flags. This simplifies the logic for
11246      * "when did we emit code that changes the MVE_NO_PRED TB flag
11247      * and thus need to end the TB?".
11248      */
11249     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11250         return false;
11251     }
11252     if (env->v7m.vpr) {
11253         return false;
11254     }
11255     if (env->v7m.ltpsize < 4) {
11256         return false;
11257     }
11258     return true;
11259 }
11260
11261 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11262                           target_ulong *cs_base, uint32_t *pflags)
11263 {
11264     CPUARMTBFlags flags;
11265
11266     assert_hflags_rebuild_correctly(env);
11267     flags = env->hflags;
11268
11269     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11270         *pc = env->pc;
11271         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11272             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11273         }
11274     } else {
11275         *pc = env->regs[15];
11276
11277         if (arm_feature(env, ARM_FEATURE_M)) {
11278             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11279                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11280                 != env->v7m.secure) {
11281                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11282             }
11283
11284             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11285                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11286                  (env->v7m.secure &&
11287                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11288                 /*
11289                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11290                  * active FP context; we must create a new FP context before
11291                  * executing any FP insn.
11292                  */
11293                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11294             }
11295
11296             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11297             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11298                 DP_TBFLAG_M32(flags, LSPACT, 1);
11299             }
11300
11301             if (mve_no_pred(env)) {
11302                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11303             }
11304         } else {
11305             /*
11306              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11307              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11308              */
11309             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11310                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11311             } else {
11312                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11313                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11314             }
11315             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11316                 DP_TBFLAG_A32(flags, VFPEN, 1);
11317             }
11318         }
11319
11320         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11321         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11322     }
11323
11324     /*
11325      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11326      * states defined in the ARM ARM for software singlestep:
11327      *  SS_ACTIVE   PSTATE.SS   State
11328      *     0            x       Inactive (the TB flag for SS is always 0)
11329      *     1            0       Active-pending
11330      *     1            1       Active-not-pending
11331      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11332      */
11333     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11334         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11335     }
11336
11337     *pflags = flags.flags;
11338     *cs_base = flags.flags2;
11339 }
11340
11341 #ifdef TARGET_AARCH64
11342 /*
11343  * The manual says that when SVE is enabled and VQ is widened the
11344  * implementation is allowed to zero the previously inaccessible
11345  * portion of the registers.  The corollary to that is that when
11346  * SVE is enabled and VQ is narrowed we are also allowed to zero
11347  * the now inaccessible portion of the registers.
11348  *
11349  * The intent of this is that no predicate bit beyond VQ is ever set.
11350  * Which means that some operations on predicate registers themselves
11351  * may operate on full uint64_t or even unrolled across the maximum
11352  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11353  * may well be cheaper than conditionals to restrict the operation
11354  * to the relevant portion of a uint16_t[16].
11355  */
11356 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11357 {
11358     int i, j;
11359     uint64_t pmask;
11360
11361     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11362     assert(vq <= env_archcpu(env)->sve_max_vq);
11363
11364     /* Zap the high bits of the zregs.  */
11365     for (i = 0; i < 32; i++) {
11366         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11367     }
11368
11369     /* Zap the high bits of the pregs and ffr.  */
11370     pmask = 0;
11371     if (vq & 3) {
11372         pmask = ~(-1ULL << (16 * (vq & 3)));
11373     }
11374     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11375         for (i = 0; i < 17; ++i) {
11376             env->vfp.pregs[i].p[j] &= pmask;
11377         }
11378         pmask = 0;
11379     }
11380 }
11381
11382 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11383 {
11384     int exc_el;
11385
11386     if (sm) {
11387         exc_el = sme_exception_el(env, el);
11388     } else {
11389         exc_el = sve_exception_el(env, el);
11390     }
11391     if (exc_el) {
11392         return 0; /* disabled */
11393     }
11394     return sve_vqm1_for_el_sm(env, el, sm);
11395 }
11396
11397 /*
11398  * Notice a change in SVE vector size when changing EL.
11399  */
11400 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11401                            int new_el, bool el0_a64)
11402 {
11403     ARMCPU *cpu = env_archcpu(env);
11404     int old_len, new_len;
11405     bool old_a64, new_a64, sm;
11406
11407     /* Nothing to do if no SVE.  */
11408     if (!cpu_isar_feature(aa64_sve, cpu)) {
11409         return;
11410     }
11411
11412     /* Nothing to do if FP is disabled in either EL.  */
11413     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11414         return;
11415     }
11416
11417     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11418     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11419
11420     /*
11421      * Both AArch64.TakeException and AArch64.ExceptionReturn
11422      * invoke ResetSVEState when taking an exception from, or
11423      * returning to, AArch32 state when PSTATE.SM is enabled.
11424      */
11425     sm = FIELD_EX64(env->svcr, SVCR, SM);
11426     if (old_a64 != new_a64 && sm) {
11427         arm_reset_sve_state(env);
11428         return;
11429     }
11430
11431     /*
11432      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11433      * at ELx, or not available because the EL is in AArch32 state, then
11434      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11435      * has an effective value of 0".
11436      *
11437      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11438      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11439      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11440      * we already have the correct register contents when encountering the
11441      * vq0->vq0 transition between EL0->EL1.
11442      */
11443     old_len = new_len = 0;
11444     if (old_a64) {
11445         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11446     }
11447     if (new_a64) {
11448         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11449     }
11450
11451     /* When changing vector length, clear inaccessible state.  */
11452     if (new_len < old_len) {
11453         aarch64_sve_narrow_vq(env, new_len + 1);
11454     }
11455 }
11456 #endif
This page took 0.685764 seconds and 4 git commands to generate.