]> Git Repo - qemu.git/blob - target/arm/helper.c
target/arm: Rename pmu_8_n feature test functions
[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 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN)
1088 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME)
1089
1090 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1091  * the current EL, security state, and register configuration.
1092  */
1093 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1094 {
1095     uint64_t filter;
1096     bool e, p, u, nsk, nsu, nsh, m;
1097     bool enabled, prohibited = false, filtered;
1098     bool secure = arm_is_secure(env);
1099     int el = arm_current_el(env);
1100     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1101     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1102
1103     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1104         return false;
1105     }
1106
1107     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1108             (counter < hpmn || counter == 31)) {
1109         e = env->cp15.c9_pmcr & PMCRE;
1110     } else {
1111         e = mdcr_el2 & MDCR_HPME;
1112     }
1113     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1114
1115     /* Is event counting prohibited? */
1116     if (el == 2 && (counter < hpmn || counter == 31)) {
1117         prohibited = mdcr_el2 & MDCR_HPMD;
1118     }
1119     if (secure) {
1120         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1121     }
1122
1123     if (prohibited && counter == 31) {
1124         prohibited = env->cp15.c9_pmcr & PMCRDP;
1125     }
1126
1127     if (counter == 31) {
1128         filter = env->cp15.pmccfiltr_el0;
1129     } else {
1130         filter = env->cp15.c14_pmevtyper[counter];
1131     }
1132
1133     p   = filter & PMXEVTYPER_P;
1134     u   = filter & PMXEVTYPER_U;
1135     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1136     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1137     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1138     m   = arm_el_is_aa64(env, 1) &&
1139               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1140
1141     if (el == 0) {
1142         filtered = secure ? u : u != nsu;
1143     } else if (el == 1) {
1144         filtered = secure ? p : p != nsk;
1145     } else if (el == 2) {
1146         filtered = !nsh;
1147     } else { /* EL3 */
1148         filtered = m != p;
1149     }
1150
1151     if (counter != 31) {
1152         /*
1153          * If not checking PMCCNTR, ensure the counter is setup to an event we
1154          * support
1155          */
1156         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1157         if (!event_supported(event)) {
1158             return false;
1159         }
1160     }
1161
1162     return enabled && !prohibited && !filtered;
1163 }
1164
1165 static void pmu_update_irq(CPUARMState *env)
1166 {
1167     ARMCPU *cpu = env_archcpu(env);
1168     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1169             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1170 }
1171
1172 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1173 {
1174     /*
1175      * Return true if the clock divider is enabled and the cycle counter
1176      * is supposed to tick only once every 64 clock cycles. This is
1177      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1178      * (64-bit) cycle counter PMCR.D has no effect.
1179      */
1180     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1181 }
1182
1183 /*
1184  * Ensure c15_ccnt is the guest-visible count so that operations such as
1185  * enabling/disabling the counter or filtering, modifying the count itself,
1186  * etc. can be done logically. This is essentially a no-op if the counter is
1187  * not enabled at the time of the call.
1188  */
1189 static void pmccntr_op_start(CPUARMState *env)
1190 {
1191     uint64_t cycles = cycles_get_count(env);
1192
1193     if (pmu_counter_enabled(env, 31)) {
1194         uint64_t eff_cycles = cycles;
1195         if (pmccntr_clockdiv_enabled(env)) {
1196             eff_cycles /= 64;
1197         }
1198
1199         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1200
1201         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1202                                  1ull << 63 : 1ull << 31;
1203         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1204             env->cp15.c9_pmovsr |= (1ULL << 31);
1205             pmu_update_irq(env);
1206         }
1207
1208         env->cp15.c15_ccnt = new_pmccntr;
1209     }
1210     env->cp15.c15_ccnt_delta = cycles;
1211 }
1212
1213 /*
1214  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1215  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1216  * pmccntr_op_start.
1217  */
1218 static void pmccntr_op_finish(CPUARMState *env)
1219 {
1220     if (pmu_counter_enabled(env, 31)) {
1221 #ifndef CONFIG_USER_ONLY
1222         /* Calculate when the counter will next overflow */
1223         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1224         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1225             remaining_cycles = (uint32_t)remaining_cycles;
1226         }
1227         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1228
1229         if (overflow_in > 0) {
1230             int64_t overflow_at;
1231
1232             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1233                                  overflow_in, &overflow_at)) {
1234                 ARMCPU *cpu = env_archcpu(env);
1235                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1236             }
1237         }
1238 #endif
1239
1240         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1241         if (pmccntr_clockdiv_enabled(env)) {
1242             prev_cycles /= 64;
1243         }
1244         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1245     }
1246 }
1247
1248 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1249 {
1250
1251     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1252     uint64_t count = 0;
1253     if (event_supported(event)) {
1254         uint16_t event_idx = supported_event_map[event];
1255         count = pm_events[event_idx].get_count(env);
1256     }
1257
1258     if (pmu_counter_enabled(env, counter)) {
1259         uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1260
1261         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1262             env->cp15.c9_pmovsr |= (1 << counter);
1263             pmu_update_irq(env);
1264         }
1265         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1266     }
1267     env->cp15.c14_pmevcntr_delta[counter] = count;
1268 }
1269
1270 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1271 {
1272     if (pmu_counter_enabled(env, counter)) {
1273 #ifndef CONFIG_USER_ONLY
1274         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1275         uint16_t event_idx = supported_event_map[event];
1276         uint64_t delta = UINT32_MAX -
1277             (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1278         int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1279
1280         if (overflow_in > 0) {
1281             int64_t overflow_at;
1282
1283             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1284                                  overflow_in, &overflow_at)) {
1285                 ARMCPU *cpu = env_archcpu(env);
1286                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1287             }
1288         }
1289 #endif
1290
1291         env->cp15.c14_pmevcntr_delta[counter] -=
1292             env->cp15.c14_pmevcntr[counter];
1293     }
1294 }
1295
1296 void pmu_op_start(CPUARMState *env)
1297 {
1298     unsigned int i;
1299     pmccntr_op_start(env);
1300     for (i = 0; i < pmu_num_counters(env); i++) {
1301         pmevcntr_op_start(env, i);
1302     }
1303 }
1304
1305 void pmu_op_finish(CPUARMState *env)
1306 {
1307     unsigned int i;
1308     pmccntr_op_finish(env);
1309     for (i = 0; i < pmu_num_counters(env); i++) {
1310         pmevcntr_op_finish(env, i);
1311     }
1312 }
1313
1314 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1315 {
1316     pmu_op_start(&cpu->env);
1317 }
1318
1319 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1320 {
1321     pmu_op_finish(&cpu->env);
1322 }
1323
1324 void arm_pmu_timer_cb(void *opaque)
1325 {
1326     ARMCPU *cpu = opaque;
1327
1328     /*
1329      * Update all the counter values based on the current underlying counts,
1330      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1331      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1332      * counter may expire.
1333      */
1334     pmu_op_start(&cpu->env);
1335     pmu_op_finish(&cpu->env);
1336 }
1337
1338 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1339                        uint64_t value)
1340 {
1341     pmu_op_start(env);
1342
1343     if (value & PMCRC) {
1344         /* The counter has been reset */
1345         env->cp15.c15_ccnt = 0;
1346     }
1347
1348     if (value & PMCRP) {
1349         unsigned int i;
1350         for (i = 0; i < pmu_num_counters(env); i++) {
1351             env->cp15.c14_pmevcntr[i] = 0;
1352         }
1353     }
1354
1355     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1356     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1357
1358     pmu_op_finish(env);
1359 }
1360
1361 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1362                           uint64_t value)
1363 {
1364     unsigned int i;
1365     for (i = 0; i < pmu_num_counters(env); i++) {
1366         /* Increment a counter's count iff: */
1367         if ((value & (1 << i)) && /* counter's bit is set */
1368                 /* counter is enabled and not filtered */
1369                 pmu_counter_enabled(env, i) &&
1370                 /* counter is SW_INCR */
1371                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1372             pmevcntr_op_start(env, i);
1373
1374             /*
1375              * Detect if this write causes an overflow since we can't predict
1376              * PMSWINC overflows like we can for other events
1377              */
1378             uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1379
1380             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1381                 env->cp15.c9_pmovsr |= (1 << i);
1382                 pmu_update_irq(env);
1383             }
1384
1385             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1386
1387             pmevcntr_op_finish(env, i);
1388         }
1389     }
1390 }
1391
1392 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1393 {
1394     uint64_t ret;
1395     pmccntr_op_start(env);
1396     ret = env->cp15.c15_ccnt;
1397     pmccntr_op_finish(env);
1398     return ret;
1399 }
1400
1401 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1402                          uint64_t value)
1403 {
1404     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1405      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1406      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1407      * accessed.
1408      */
1409     env->cp15.c9_pmselr = value & 0x1f;
1410 }
1411
1412 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1413                         uint64_t value)
1414 {
1415     pmccntr_op_start(env);
1416     env->cp15.c15_ccnt = value;
1417     pmccntr_op_finish(env);
1418 }
1419
1420 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1421                             uint64_t value)
1422 {
1423     uint64_t cur_val = pmccntr_read(env, NULL);
1424
1425     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1426 }
1427
1428 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1429                             uint64_t value)
1430 {
1431     pmccntr_op_start(env);
1432     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1433     pmccntr_op_finish(env);
1434 }
1435
1436 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1437                             uint64_t value)
1438 {
1439     pmccntr_op_start(env);
1440     /* M is not accessible from AArch32 */
1441     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1442         (value & PMCCFILTR);
1443     pmccntr_op_finish(env);
1444 }
1445
1446 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1447 {
1448     /* M is not visible in AArch32 */
1449     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1450 }
1451
1452 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453                             uint64_t value)
1454 {
1455     pmu_op_start(env);
1456     value &= pmu_counter_mask(env);
1457     env->cp15.c9_pmcnten |= value;
1458     pmu_op_finish(env);
1459 }
1460
1461 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1462                              uint64_t value)
1463 {
1464     pmu_op_start(env);
1465     value &= pmu_counter_mask(env);
1466     env->cp15.c9_pmcnten &= ~value;
1467     pmu_op_finish(env);
1468 }
1469
1470 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1471                          uint64_t value)
1472 {
1473     value &= pmu_counter_mask(env);
1474     env->cp15.c9_pmovsr &= ~value;
1475     pmu_update_irq(env);
1476 }
1477
1478 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1479                          uint64_t value)
1480 {
1481     value &= pmu_counter_mask(env);
1482     env->cp15.c9_pmovsr |= value;
1483     pmu_update_irq(env);
1484 }
1485
1486 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1487                              uint64_t value, const uint8_t counter)
1488 {
1489     if (counter == 31) {
1490         pmccfiltr_write(env, ri, value);
1491     } else if (counter < pmu_num_counters(env)) {
1492         pmevcntr_op_start(env, counter);
1493
1494         /*
1495          * If this counter's event type is changing, store the current
1496          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1497          * pmevcntr_op_finish has the correct baseline when it converts back to
1498          * a delta.
1499          */
1500         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1501             PMXEVTYPER_EVTCOUNT;
1502         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1503         if (old_event != new_event) {
1504             uint64_t count = 0;
1505             if (event_supported(new_event)) {
1506                 uint16_t event_idx = supported_event_map[new_event];
1507                 count = pm_events[event_idx].get_count(env);
1508             }
1509             env->cp15.c14_pmevcntr_delta[counter] = count;
1510         }
1511
1512         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1513         pmevcntr_op_finish(env, counter);
1514     }
1515     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1516      * PMSELR value is equal to or greater than the number of implemented
1517      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1518      */
1519 }
1520
1521 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1522                                const uint8_t counter)
1523 {
1524     if (counter == 31) {
1525         return env->cp15.pmccfiltr_el0;
1526     } else if (counter < pmu_num_counters(env)) {
1527         return env->cp15.c14_pmevtyper[counter];
1528     } else {
1529       /*
1530        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1531        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1532        */
1533         return 0;
1534     }
1535 }
1536
1537 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1538                               uint64_t value)
1539 {
1540     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1541     pmevtyper_write(env, ri, value, counter);
1542 }
1543
1544 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1545                                uint64_t value)
1546 {
1547     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1548     env->cp15.c14_pmevtyper[counter] = value;
1549
1550     /*
1551      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1552      * pmu_op_finish calls when loading saved state for a migration. Because
1553      * we're potentially updating the type of event here, the value written to
1554      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1555      * different counter type. Therefore, we need to set this value to the
1556      * current count for the counter type we're writing so that pmu_op_finish
1557      * has the correct count for its calculation.
1558      */
1559     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1560     if (event_supported(event)) {
1561         uint16_t event_idx = supported_event_map[event];
1562         env->cp15.c14_pmevcntr_delta[counter] =
1563             pm_events[event_idx].get_count(env);
1564     }
1565 }
1566
1567 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1568 {
1569     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1570     return pmevtyper_read(env, ri, counter);
1571 }
1572
1573 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1574                              uint64_t value)
1575 {
1576     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1577 }
1578
1579 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1580 {
1581     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1582 }
1583
1584 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585                              uint64_t value, uint8_t counter)
1586 {
1587     if (counter < pmu_num_counters(env)) {
1588         pmevcntr_op_start(env, counter);
1589         env->cp15.c14_pmevcntr[counter] = value;
1590         pmevcntr_op_finish(env, counter);
1591     }
1592     /*
1593      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1594      * are CONSTRAINED UNPREDICTABLE.
1595      */
1596 }
1597
1598 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1599                               uint8_t counter)
1600 {
1601     if (counter < pmu_num_counters(env)) {
1602         uint64_t ret;
1603         pmevcntr_op_start(env, counter);
1604         ret = env->cp15.c14_pmevcntr[counter];
1605         pmevcntr_op_finish(env, counter);
1606         return ret;
1607     } else {
1608       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1609        * are CONSTRAINED UNPREDICTABLE. */
1610         return 0;
1611     }
1612 }
1613
1614 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1615                              uint64_t value)
1616 {
1617     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1618     pmevcntr_write(env, ri, value, counter);
1619 }
1620
1621 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1622 {
1623     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1624     return pmevcntr_read(env, ri, counter);
1625 }
1626
1627 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1628                              uint64_t value)
1629 {
1630     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1631     assert(counter < pmu_num_counters(env));
1632     env->cp15.c14_pmevcntr[counter] = value;
1633     pmevcntr_write(env, ri, value, counter);
1634 }
1635
1636 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1637 {
1638     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1639     assert(counter < pmu_num_counters(env));
1640     return env->cp15.c14_pmevcntr[counter];
1641 }
1642
1643 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1644                              uint64_t value)
1645 {
1646     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1647 }
1648
1649 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1650 {
1651     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1652 }
1653
1654 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1655                             uint64_t value)
1656 {
1657     if (arm_feature(env, ARM_FEATURE_V8)) {
1658         env->cp15.c9_pmuserenr = value & 0xf;
1659     } else {
1660         env->cp15.c9_pmuserenr = value & 1;
1661     }
1662 }
1663
1664 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1665                              uint64_t value)
1666 {
1667     /* We have no event counters so only the C bit can be changed */
1668     value &= pmu_counter_mask(env);
1669     env->cp15.c9_pminten |= value;
1670     pmu_update_irq(env);
1671 }
1672
1673 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1674                              uint64_t value)
1675 {
1676     value &= pmu_counter_mask(env);
1677     env->cp15.c9_pminten &= ~value;
1678     pmu_update_irq(env);
1679 }
1680
1681 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1682                        uint64_t value)
1683 {
1684     /* Note that even though the AArch64 view of this register has bits
1685      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1686      * architectural requirements for bits which are RES0 only in some
1687      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1688      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1689      */
1690     raw_write(env, ri, value & ~0x1FULL);
1691 }
1692
1693 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1694 {
1695     /* Begin with base v8.0 state.  */
1696     uint32_t valid_mask = 0x3fff;
1697     ARMCPU *cpu = env_archcpu(env);
1698
1699     /*
1700      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1701      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1702      * Instead, choose the format based on the mode of EL3.
1703      */
1704     if (arm_el_is_aa64(env, 3)) {
1705         value |= SCR_FW | SCR_AW;      /* RES1 */
1706         valid_mask &= ~SCR_NET;        /* RES0 */
1707
1708         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1709             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1710             value |= SCR_RW;           /* RAO/WI */
1711         }
1712         if (cpu_isar_feature(aa64_ras, cpu)) {
1713             valid_mask |= SCR_TERR;
1714         }
1715         if (cpu_isar_feature(aa64_lor, cpu)) {
1716             valid_mask |= SCR_TLOR;
1717         }
1718         if (cpu_isar_feature(aa64_pauth, cpu)) {
1719             valid_mask |= SCR_API | SCR_APK;
1720         }
1721         if (cpu_isar_feature(aa64_sel2, cpu)) {
1722             valid_mask |= SCR_EEL2;
1723         }
1724         if (cpu_isar_feature(aa64_mte, cpu)) {
1725             valid_mask |= SCR_ATA;
1726         }
1727         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1728             valid_mask |= SCR_ENSCXT;
1729         }
1730         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1731             valid_mask |= SCR_EASE | SCR_NMEA;
1732         }
1733     } else {
1734         valid_mask &= ~(SCR_RW | SCR_ST);
1735         if (cpu_isar_feature(aa32_ras, cpu)) {
1736             valid_mask |= SCR_TERR;
1737         }
1738     }
1739
1740     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1741         valid_mask &= ~SCR_HCE;
1742
1743         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1744          * supported if EL2 exists. The bit is UNK/SBZP when
1745          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1746          * when EL2 is unavailable.
1747          * On ARMv8, this bit is always available.
1748          */
1749         if (arm_feature(env, ARM_FEATURE_V7) &&
1750             !arm_feature(env, ARM_FEATURE_V8)) {
1751             valid_mask &= ~SCR_SMD;
1752         }
1753     }
1754
1755     /* Clear all-context RES0 bits.  */
1756     value &= valid_mask;
1757     raw_write(env, ri, value);
1758 }
1759
1760 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1761 {
1762     /*
1763      * scr_write will set the RES1 bits on an AArch64-only CPU.
1764      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1765      */
1766     scr_write(env, ri, 0);
1767 }
1768
1769 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1770                                        const ARMCPRegInfo *ri,
1771                                        bool isread)
1772 {
1773     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1774         return CP_ACCESS_TRAP_EL2;
1775     }
1776
1777     return CP_ACCESS_OK;
1778 }
1779
1780 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1781 {
1782     ARMCPU *cpu = env_archcpu(env);
1783
1784     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1785      * bank
1786      */
1787     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1788                                         ri->secure & ARM_CP_SECSTATE_S);
1789
1790     return cpu->ccsidr[index];
1791 }
1792
1793 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794                          uint64_t value)
1795 {
1796     raw_write(env, ri, value & 0xf);
1797 }
1798
1799 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1800 {
1801     CPUState *cs = env_cpu(env);
1802     bool el1 = arm_current_el(env) == 1;
1803     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1804     uint64_t ret = 0;
1805
1806     if (hcr_el2 & HCR_IMO) {
1807         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1808             ret |= CPSR_I;
1809         }
1810     } else {
1811         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1812             ret |= CPSR_I;
1813         }
1814     }
1815
1816     if (hcr_el2 & HCR_FMO) {
1817         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1818             ret |= CPSR_F;
1819         }
1820     } else {
1821         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1822             ret |= CPSR_F;
1823         }
1824     }
1825
1826     if (hcr_el2 & HCR_AMO) {
1827         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1828             ret |= CPSR_A;
1829         }
1830     }
1831
1832     return ret;
1833 }
1834
1835 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1836                                        bool isread)
1837 {
1838     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1839         return CP_ACCESS_TRAP_EL2;
1840     }
1841
1842     return CP_ACCESS_OK;
1843 }
1844
1845 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1846                                        bool isread)
1847 {
1848     if (arm_feature(env, ARM_FEATURE_V8)) {
1849         return access_aa64_tid1(env, ri, isread);
1850     }
1851
1852     return CP_ACCESS_OK;
1853 }
1854
1855 static const ARMCPRegInfo v7_cp_reginfo[] = {
1856     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1857     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1858       .access = PL1_W, .type = ARM_CP_NOP },
1859     /* Performance monitors are implementation defined in v7,
1860      * but with an ARM recommended set of registers, which we
1861      * follow.
1862      *
1863      * Performance registers fall into three categories:
1864      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1865      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1866      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1867      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1868      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1869      */
1870     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1871       .access = PL0_RW, .type = ARM_CP_ALIAS,
1872       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1873       .writefn = pmcntenset_write,
1874       .accessfn = pmreg_access,
1875       .raw_writefn = raw_write },
1876     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1877       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1878       .access = PL0_RW, .accessfn = pmreg_access,
1879       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1880       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1881     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1882       .access = PL0_RW,
1883       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1884       .accessfn = pmreg_access,
1885       .writefn = pmcntenclr_write,
1886       .type = ARM_CP_ALIAS },
1887     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1888       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1889       .access = PL0_RW, .accessfn = pmreg_access,
1890       .type = ARM_CP_ALIAS,
1891       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1892       .writefn = pmcntenclr_write },
1893     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1894       .access = PL0_RW, .type = ARM_CP_IO,
1895       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1896       .accessfn = pmreg_access,
1897       .writefn = pmovsr_write,
1898       .raw_writefn = raw_write },
1899     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1900       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1901       .access = PL0_RW, .accessfn = pmreg_access,
1902       .type = ARM_CP_ALIAS | ARM_CP_IO,
1903       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1904       .writefn = pmovsr_write,
1905       .raw_writefn = raw_write },
1906     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1907       .access = PL0_W, .accessfn = pmreg_access_swinc,
1908       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1909       .writefn = pmswinc_write },
1910     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1911       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1912       .access = PL0_W, .accessfn = pmreg_access_swinc,
1913       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1914       .writefn = pmswinc_write },
1915     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1916       .access = PL0_RW, .type = ARM_CP_ALIAS,
1917       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1918       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1919       .raw_writefn = raw_write},
1920     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1921       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1922       .access = PL0_RW, .accessfn = pmreg_access_selr,
1923       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1924       .writefn = pmselr_write, .raw_writefn = raw_write, },
1925     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1926       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1927       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1928       .accessfn = pmreg_access_ccntr },
1929     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1930       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1931       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1932       .type = ARM_CP_IO,
1933       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1934       .readfn = pmccntr_read, .writefn = pmccntr_write,
1935       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1936     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1937       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1938       .access = PL0_RW, .accessfn = pmreg_access,
1939       .type = ARM_CP_ALIAS | ARM_CP_IO,
1940       .resetvalue = 0, },
1941     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1942       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1943       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1944       .access = PL0_RW, .accessfn = pmreg_access,
1945       .type = ARM_CP_IO,
1946       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1947       .resetvalue = 0, },
1948     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1949       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1950       .accessfn = pmreg_access,
1951       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1952     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1953       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1954       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1955       .accessfn = pmreg_access,
1956       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1957     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1958       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1959       .accessfn = pmreg_access_xevcntr,
1960       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1961     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1962       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1963       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1964       .accessfn = pmreg_access_xevcntr,
1965       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1966     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1967       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
1968       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
1969       .resetvalue = 0,
1970       .writefn = pmuserenr_write, .raw_writefn = raw_write },
1971     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1972       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1973       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1974       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1975       .resetvalue = 0,
1976       .writefn = pmuserenr_write, .raw_writefn = raw_write },
1977     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1978       .access = PL1_RW, .accessfn = access_tpm,
1979       .type = ARM_CP_ALIAS | ARM_CP_IO,
1980       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
1981       .resetvalue = 0,
1982       .writefn = pmintenset_write, .raw_writefn = raw_write },
1983     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1984       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1985       .access = PL1_RW, .accessfn = access_tpm,
1986       .type = ARM_CP_IO,
1987       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1988       .writefn = pmintenset_write, .raw_writefn = raw_write,
1989       .resetvalue = 0x0 },
1990     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1991       .access = PL1_RW, .accessfn = access_tpm,
1992       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
1993       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1994       .writefn = pmintenclr_write, },
1995     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1996       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1997       .access = PL1_RW, .accessfn = access_tpm,
1998       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
1999       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2000       .writefn = pmintenclr_write },
2001     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2002       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2003       .access = PL1_R,
2004       .accessfn = access_aa64_tid2,
2005       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2006     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2007       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2008       .access = PL1_RW,
2009       .accessfn = access_aa64_tid2,
2010       .writefn = csselr_write, .resetvalue = 0,
2011       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2012                              offsetof(CPUARMState, cp15.csselr_ns) } },
2013     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2014      * just RAZ for all cores:
2015      */
2016     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2017       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2018       .access = PL1_R, .type = ARM_CP_CONST,
2019       .accessfn = access_aa64_tid1,
2020       .resetvalue = 0 },
2021     /* Auxiliary fault status registers: these also are IMPDEF, and we
2022      * choose to RAZ/WI for all cores.
2023      */
2024     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2025       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2026       .access = PL1_RW, .accessfn = access_tvm_trvm,
2027       .type = ARM_CP_CONST, .resetvalue = 0 },
2028     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2029       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2030       .access = PL1_RW, .accessfn = access_tvm_trvm,
2031       .type = ARM_CP_CONST, .resetvalue = 0 },
2032     /* MAIR can just read-as-written because we don't implement caches
2033      * and so don't need to care about memory attributes.
2034      */
2035     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2036       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2037       .access = PL1_RW, .accessfn = access_tvm_trvm,
2038       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2039       .resetvalue = 0 },
2040     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2041       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2042       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2043       .resetvalue = 0 },
2044     /* For non-long-descriptor page tables these are PRRR and NMRR;
2045      * regardless they still act as reads-as-written for QEMU.
2046      */
2047      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2048       * allows them to assign the correct fieldoffset based on the endianness
2049       * handled in the field definitions.
2050       */
2051     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2052       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2053       .access = PL1_RW, .accessfn = access_tvm_trvm,
2054       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2055                              offsetof(CPUARMState, cp15.mair0_ns) },
2056       .resetfn = arm_cp_reset_ignore },
2057     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2058       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2059       .access = PL1_RW, .accessfn = access_tvm_trvm,
2060       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2061                              offsetof(CPUARMState, cp15.mair1_ns) },
2062       .resetfn = arm_cp_reset_ignore },
2063     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2064       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2065       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2066     /* 32 bit ITLB invalidates */
2067     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2068       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2069       .writefn = tlbiall_write },
2070     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2071       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2072       .writefn = tlbimva_write },
2073     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2074       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2075       .writefn = tlbiasid_write },
2076     /* 32 bit DTLB invalidates */
2077     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2078       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2079       .writefn = tlbiall_write },
2080     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2081       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2082       .writefn = tlbimva_write },
2083     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2084       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2085       .writefn = tlbiasid_write },
2086     /* 32 bit TLB invalidates */
2087     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2088       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2089       .writefn = tlbiall_write },
2090     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2091       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2092       .writefn = tlbimva_write },
2093     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2094       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2095       .writefn = tlbiasid_write },
2096     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2097       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2098       .writefn = tlbimvaa_write },
2099 };
2100
2101 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2102     /* 32 bit TLB invalidates, Inner Shareable */
2103     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2104       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2105       .writefn = tlbiall_is_write },
2106     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2107       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2108       .writefn = tlbimva_is_write },
2109     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2110       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111       .writefn = tlbiasid_is_write },
2112     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2113       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2114       .writefn = tlbimvaa_is_write },
2115 };
2116
2117 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2118     /* PMOVSSET is not implemented in v7 before v7ve */
2119     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2120       .access = PL0_RW, .accessfn = pmreg_access,
2121       .type = ARM_CP_ALIAS | ARM_CP_IO,
2122       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2123       .writefn = pmovsset_write,
2124       .raw_writefn = raw_write },
2125     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2126       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2127       .access = PL0_RW, .accessfn = pmreg_access,
2128       .type = ARM_CP_ALIAS | ARM_CP_IO,
2129       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2130       .writefn = pmovsset_write,
2131       .raw_writefn = raw_write },
2132 };
2133
2134 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2135                         uint64_t value)
2136 {
2137     value &= 1;
2138     env->teecr = value;
2139 }
2140
2141 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2142                                    bool isread)
2143 {
2144     /*
2145      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2146      * at all, so we don't need to check whether we're v8A.
2147      */
2148     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2149         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2150         return CP_ACCESS_TRAP_EL2;
2151     }
2152     return CP_ACCESS_OK;
2153 }
2154
2155 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2156                                     bool isread)
2157 {
2158     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2159         return CP_ACCESS_TRAP;
2160     }
2161     return teecr_access(env, ri, isread);
2162 }
2163
2164 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2165     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2166       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2167       .resetvalue = 0,
2168       .writefn = teecr_write, .accessfn = teecr_access },
2169     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2170       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2171       .accessfn = teehbr_access, .resetvalue = 0 },
2172 };
2173
2174 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2175     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2176       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2177       .access = PL0_RW,
2178       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2179     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2180       .access = PL0_RW,
2181       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2182                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2183       .resetfn = arm_cp_reset_ignore },
2184     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2185       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2186       .access = PL0_R|PL1_W,
2187       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2188       .resetvalue = 0},
2189     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2190       .access = PL0_R|PL1_W,
2191       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2192                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2193       .resetfn = arm_cp_reset_ignore },
2194     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2195       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2196       .access = PL1_RW,
2197       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2198     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2199       .access = PL1_RW,
2200       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2201                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2202       .resetvalue = 0 },
2203 };
2204
2205 #ifndef CONFIG_USER_ONLY
2206
2207 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2208                                        bool isread)
2209 {
2210     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2211      * Writable only at the highest implemented exception level.
2212      */
2213     int el = arm_current_el(env);
2214     uint64_t hcr;
2215     uint32_t cntkctl;
2216
2217     switch (el) {
2218     case 0:
2219         hcr = arm_hcr_el2_eff(env);
2220         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2221             cntkctl = env->cp15.cnthctl_el2;
2222         } else {
2223             cntkctl = env->cp15.c14_cntkctl;
2224         }
2225         if (!extract32(cntkctl, 0, 2)) {
2226             return CP_ACCESS_TRAP;
2227         }
2228         break;
2229     case 1:
2230         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2231             arm_is_secure_below_el3(env)) {
2232             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2233             return CP_ACCESS_TRAP_UNCATEGORIZED;
2234         }
2235         break;
2236     case 2:
2237     case 3:
2238         break;
2239     }
2240
2241     if (!isread && el < arm_highest_el(env)) {
2242         return CP_ACCESS_TRAP_UNCATEGORIZED;
2243     }
2244
2245     return CP_ACCESS_OK;
2246 }
2247
2248 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2249                                         bool isread)
2250 {
2251     unsigned int cur_el = arm_current_el(env);
2252     bool has_el2 = arm_is_el2_enabled(env);
2253     uint64_t hcr = arm_hcr_el2_eff(env);
2254
2255     switch (cur_el) {
2256     case 0:
2257         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2258         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2259             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2260                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2261         }
2262
2263         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2264         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2265             return CP_ACCESS_TRAP;
2266         }
2267
2268         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2269         if (hcr & HCR_E2H) {
2270             if (timeridx == GTIMER_PHYS &&
2271                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2272                 return CP_ACCESS_TRAP_EL2;
2273             }
2274         } else {
2275             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2276             if (has_el2 && timeridx == GTIMER_PHYS &&
2277                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2278                 return CP_ACCESS_TRAP_EL2;
2279             }
2280         }
2281         break;
2282
2283     case 1:
2284         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2285         if (has_el2 && timeridx == GTIMER_PHYS &&
2286             (hcr & HCR_E2H
2287              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2288              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2289             return CP_ACCESS_TRAP_EL2;
2290         }
2291         break;
2292     }
2293     return CP_ACCESS_OK;
2294 }
2295
2296 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2297                                       bool isread)
2298 {
2299     unsigned int cur_el = arm_current_el(env);
2300     bool has_el2 = arm_is_el2_enabled(env);
2301     uint64_t hcr = arm_hcr_el2_eff(env);
2302
2303     switch (cur_el) {
2304     case 0:
2305         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2306             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2307             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2308                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2309         }
2310
2311         /*
2312          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2313          * EL0 if EL0[PV]TEN is zero.
2314          */
2315         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2316             return CP_ACCESS_TRAP;
2317         }
2318         /* fall through */
2319
2320     case 1:
2321         if (has_el2 && timeridx == GTIMER_PHYS) {
2322             if (hcr & HCR_E2H) {
2323                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2324                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2325                     return CP_ACCESS_TRAP_EL2;
2326                 }
2327             } else {
2328                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2329                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2330                     return CP_ACCESS_TRAP_EL2;
2331                 }
2332             }
2333         }
2334         break;
2335     }
2336     return CP_ACCESS_OK;
2337 }
2338
2339 static CPAccessResult gt_pct_access(CPUARMState *env,
2340                                     const ARMCPRegInfo *ri,
2341                                     bool isread)
2342 {
2343     return gt_counter_access(env, GTIMER_PHYS, isread);
2344 }
2345
2346 static CPAccessResult gt_vct_access(CPUARMState *env,
2347                                     const ARMCPRegInfo *ri,
2348                                     bool isread)
2349 {
2350     return gt_counter_access(env, GTIMER_VIRT, isread);
2351 }
2352
2353 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2354                                        bool isread)
2355 {
2356     return gt_timer_access(env, GTIMER_PHYS, isread);
2357 }
2358
2359 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2360                                        bool isread)
2361 {
2362     return gt_timer_access(env, GTIMER_VIRT, isread);
2363 }
2364
2365 static CPAccessResult gt_stimer_access(CPUARMState *env,
2366                                        const ARMCPRegInfo *ri,
2367                                        bool isread)
2368 {
2369     /* The AArch64 register view of the secure physical timer is
2370      * always accessible from EL3, and configurably accessible from
2371      * Secure EL1.
2372      */
2373     switch (arm_current_el(env)) {
2374     case 1:
2375         if (!arm_is_secure(env)) {
2376             return CP_ACCESS_TRAP;
2377         }
2378         if (!(env->cp15.scr_el3 & SCR_ST)) {
2379             return CP_ACCESS_TRAP_EL3;
2380         }
2381         return CP_ACCESS_OK;
2382     case 0:
2383     case 2:
2384         return CP_ACCESS_TRAP;
2385     case 3:
2386         return CP_ACCESS_OK;
2387     default:
2388         g_assert_not_reached();
2389     }
2390 }
2391
2392 static uint64_t gt_get_countervalue(CPUARMState *env)
2393 {
2394     ARMCPU *cpu = env_archcpu(env);
2395
2396     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2397 }
2398
2399 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2400 {
2401     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2402
2403     if (gt->ctl & 1) {
2404         /* Timer enabled: calculate and set current ISTATUS, irq, and
2405          * reset timer to when ISTATUS next has to change
2406          */
2407         uint64_t offset = timeridx == GTIMER_VIRT ?
2408                                       cpu->env.cp15.cntvoff_el2 : 0;
2409         uint64_t count = gt_get_countervalue(&cpu->env);
2410         /* Note that this must be unsigned 64 bit arithmetic: */
2411         int istatus = count - offset >= gt->cval;
2412         uint64_t nexttick;
2413         int irqstate;
2414
2415         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2416
2417         irqstate = (istatus && !(gt->ctl & 2));
2418         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2419
2420         if (istatus) {
2421             /* Next transition is when count rolls back over to zero */
2422             nexttick = UINT64_MAX;
2423         } else {
2424             /* Next transition is when we hit cval */
2425             nexttick = gt->cval + offset;
2426         }
2427         /* Note that the desired next expiry time might be beyond the
2428          * signed-64-bit range of a QEMUTimer -- in this case we just
2429          * set the timer for as far in the future as possible. When the
2430          * timer expires we will reset the timer for any remaining period.
2431          */
2432         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2433             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2434         } else {
2435             timer_mod(cpu->gt_timer[timeridx], nexttick);
2436         }
2437         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2438     } else {
2439         /* Timer disabled: ISTATUS and timer output always clear */
2440         gt->ctl &= ~4;
2441         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2442         timer_del(cpu->gt_timer[timeridx]);
2443         trace_arm_gt_recalc_disabled(timeridx);
2444     }
2445 }
2446
2447 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2448                            int timeridx)
2449 {
2450     ARMCPU *cpu = env_archcpu(env);
2451
2452     timer_del(cpu->gt_timer[timeridx]);
2453 }
2454
2455 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2456 {
2457     return gt_get_countervalue(env);
2458 }
2459
2460 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2461 {
2462     uint64_t hcr;
2463
2464     switch (arm_current_el(env)) {
2465     case 2:
2466         hcr = arm_hcr_el2_eff(env);
2467         if (hcr & HCR_E2H) {
2468             return 0;
2469         }
2470         break;
2471     case 0:
2472         hcr = arm_hcr_el2_eff(env);
2473         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2474             return 0;
2475         }
2476         break;
2477     }
2478
2479     return env->cp15.cntvoff_el2;
2480 }
2481
2482 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2483 {
2484     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2485 }
2486
2487 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2488                           int timeridx,
2489                           uint64_t value)
2490 {
2491     trace_arm_gt_cval_write(timeridx, value);
2492     env->cp15.c14_timer[timeridx].cval = value;
2493     gt_recalc_timer(env_archcpu(env), timeridx);
2494 }
2495
2496 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2497                              int timeridx)
2498 {
2499     uint64_t offset = 0;
2500
2501     switch (timeridx) {
2502     case GTIMER_VIRT:
2503     case GTIMER_HYPVIRT:
2504         offset = gt_virt_cnt_offset(env);
2505         break;
2506     }
2507
2508     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2509                       (gt_get_countervalue(env) - offset));
2510 }
2511
2512 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2513                           int timeridx,
2514                           uint64_t value)
2515 {
2516     uint64_t offset = 0;
2517
2518     switch (timeridx) {
2519     case GTIMER_VIRT:
2520     case GTIMER_HYPVIRT:
2521         offset = gt_virt_cnt_offset(env);
2522         break;
2523     }
2524
2525     trace_arm_gt_tval_write(timeridx, value);
2526     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2527                                          sextract64(value, 0, 32);
2528     gt_recalc_timer(env_archcpu(env), timeridx);
2529 }
2530
2531 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2532                          int timeridx,
2533                          uint64_t value)
2534 {
2535     ARMCPU *cpu = env_archcpu(env);
2536     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2537
2538     trace_arm_gt_ctl_write(timeridx, value);
2539     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2540     if ((oldval ^ value) & 1) {
2541         /* Enable toggled */
2542         gt_recalc_timer(cpu, timeridx);
2543     } else if ((oldval ^ value) & 2) {
2544         /* IMASK toggled: don't need to recalculate,
2545          * just set the interrupt line based on ISTATUS
2546          */
2547         int irqstate = (oldval & 4) && !(value & 2);
2548
2549         trace_arm_gt_imask_toggle(timeridx, irqstate);
2550         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2551     }
2552 }
2553
2554 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2555 {
2556     gt_timer_reset(env, ri, GTIMER_PHYS);
2557 }
2558
2559 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2560                                uint64_t value)
2561 {
2562     gt_cval_write(env, ri, GTIMER_PHYS, value);
2563 }
2564
2565 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2566 {
2567     return gt_tval_read(env, ri, GTIMER_PHYS);
2568 }
2569
2570 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2571                                uint64_t value)
2572 {
2573     gt_tval_write(env, ri, GTIMER_PHYS, value);
2574 }
2575
2576 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2577                               uint64_t value)
2578 {
2579     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2580 }
2581
2582 static int gt_phys_redir_timeridx(CPUARMState *env)
2583 {
2584     switch (arm_mmu_idx(env)) {
2585     case ARMMMUIdx_E20_0:
2586     case ARMMMUIdx_E20_2:
2587     case ARMMMUIdx_E20_2_PAN:
2588     case ARMMMUIdx_SE20_0:
2589     case ARMMMUIdx_SE20_2:
2590     case ARMMMUIdx_SE20_2_PAN:
2591         return GTIMER_HYP;
2592     default:
2593         return GTIMER_PHYS;
2594     }
2595 }
2596
2597 static int gt_virt_redir_timeridx(CPUARMState *env)
2598 {
2599     switch (arm_mmu_idx(env)) {
2600     case ARMMMUIdx_E20_0:
2601     case ARMMMUIdx_E20_2:
2602     case ARMMMUIdx_E20_2_PAN:
2603     case ARMMMUIdx_SE20_0:
2604     case ARMMMUIdx_SE20_2:
2605     case ARMMMUIdx_SE20_2_PAN:
2606         return GTIMER_HYPVIRT;
2607     default:
2608         return GTIMER_VIRT;
2609     }
2610 }
2611
2612 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2613                                         const ARMCPRegInfo *ri)
2614 {
2615     int timeridx = gt_phys_redir_timeridx(env);
2616     return env->cp15.c14_timer[timeridx].cval;
2617 }
2618
2619 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2620                                      uint64_t value)
2621 {
2622     int timeridx = gt_phys_redir_timeridx(env);
2623     gt_cval_write(env, ri, timeridx, value);
2624 }
2625
2626 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2627                                         const ARMCPRegInfo *ri)
2628 {
2629     int timeridx = gt_phys_redir_timeridx(env);
2630     return gt_tval_read(env, ri, timeridx);
2631 }
2632
2633 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2634                                      uint64_t value)
2635 {
2636     int timeridx = gt_phys_redir_timeridx(env);
2637     gt_tval_write(env, ri, timeridx, value);
2638 }
2639
2640 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2641                                        const ARMCPRegInfo *ri)
2642 {
2643     int timeridx = gt_phys_redir_timeridx(env);
2644     return env->cp15.c14_timer[timeridx].ctl;
2645 }
2646
2647 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2648                                     uint64_t value)
2649 {
2650     int timeridx = gt_phys_redir_timeridx(env);
2651     gt_ctl_write(env, ri, timeridx, value);
2652 }
2653
2654 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2655 {
2656     gt_timer_reset(env, ri, GTIMER_VIRT);
2657 }
2658
2659 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2660                                uint64_t value)
2661 {
2662     gt_cval_write(env, ri, GTIMER_VIRT, value);
2663 }
2664
2665 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2666 {
2667     return gt_tval_read(env, ri, GTIMER_VIRT);
2668 }
2669
2670 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2671                                uint64_t value)
2672 {
2673     gt_tval_write(env, ri, GTIMER_VIRT, value);
2674 }
2675
2676 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2677                               uint64_t value)
2678 {
2679     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2680 }
2681
2682 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2683                               uint64_t value)
2684 {
2685     ARMCPU *cpu = env_archcpu(env);
2686
2687     trace_arm_gt_cntvoff_write(value);
2688     raw_write(env, ri, value);
2689     gt_recalc_timer(cpu, GTIMER_VIRT);
2690 }
2691
2692 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2693                                         const ARMCPRegInfo *ri)
2694 {
2695     int timeridx = gt_virt_redir_timeridx(env);
2696     return env->cp15.c14_timer[timeridx].cval;
2697 }
2698
2699 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2700                                      uint64_t value)
2701 {
2702     int timeridx = gt_virt_redir_timeridx(env);
2703     gt_cval_write(env, ri, timeridx, value);
2704 }
2705
2706 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2707                                         const ARMCPRegInfo *ri)
2708 {
2709     int timeridx = gt_virt_redir_timeridx(env);
2710     return gt_tval_read(env, ri, timeridx);
2711 }
2712
2713 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2714                                      uint64_t value)
2715 {
2716     int timeridx = gt_virt_redir_timeridx(env);
2717     gt_tval_write(env, ri, timeridx, value);
2718 }
2719
2720 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2721                                        const ARMCPRegInfo *ri)
2722 {
2723     int timeridx = gt_virt_redir_timeridx(env);
2724     return env->cp15.c14_timer[timeridx].ctl;
2725 }
2726
2727 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2728                                     uint64_t value)
2729 {
2730     int timeridx = gt_virt_redir_timeridx(env);
2731     gt_ctl_write(env, ri, timeridx, value);
2732 }
2733
2734 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2735 {
2736     gt_timer_reset(env, ri, GTIMER_HYP);
2737 }
2738
2739 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2740                               uint64_t value)
2741 {
2742     gt_cval_write(env, ri, GTIMER_HYP, value);
2743 }
2744
2745 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2746 {
2747     return gt_tval_read(env, ri, GTIMER_HYP);
2748 }
2749
2750 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2751                               uint64_t value)
2752 {
2753     gt_tval_write(env, ri, GTIMER_HYP, value);
2754 }
2755
2756 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2757                               uint64_t value)
2758 {
2759     gt_ctl_write(env, ri, GTIMER_HYP, value);
2760 }
2761
2762 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2763 {
2764     gt_timer_reset(env, ri, GTIMER_SEC);
2765 }
2766
2767 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2768                               uint64_t value)
2769 {
2770     gt_cval_write(env, ri, GTIMER_SEC, value);
2771 }
2772
2773 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2774 {
2775     return gt_tval_read(env, ri, GTIMER_SEC);
2776 }
2777
2778 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2779                               uint64_t value)
2780 {
2781     gt_tval_write(env, ri, GTIMER_SEC, value);
2782 }
2783
2784 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2785                               uint64_t value)
2786 {
2787     gt_ctl_write(env, ri, GTIMER_SEC, value);
2788 }
2789
2790 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2791 {
2792     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2793 }
2794
2795 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2796                              uint64_t value)
2797 {
2798     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2799 }
2800
2801 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2802 {
2803     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2804 }
2805
2806 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2807                              uint64_t value)
2808 {
2809     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2810 }
2811
2812 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2813                             uint64_t value)
2814 {
2815     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2816 }
2817
2818 void arm_gt_ptimer_cb(void *opaque)
2819 {
2820     ARMCPU *cpu = opaque;
2821
2822     gt_recalc_timer(cpu, GTIMER_PHYS);
2823 }
2824
2825 void arm_gt_vtimer_cb(void *opaque)
2826 {
2827     ARMCPU *cpu = opaque;
2828
2829     gt_recalc_timer(cpu, GTIMER_VIRT);
2830 }
2831
2832 void arm_gt_htimer_cb(void *opaque)
2833 {
2834     ARMCPU *cpu = opaque;
2835
2836     gt_recalc_timer(cpu, GTIMER_HYP);
2837 }
2838
2839 void arm_gt_stimer_cb(void *opaque)
2840 {
2841     ARMCPU *cpu = opaque;
2842
2843     gt_recalc_timer(cpu, GTIMER_SEC);
2844 }
2845
2846 void arm_gt_hvtimer_cb(void *opaque)
2847 {
2848     ARMCPU *cpu = opaque;
2849
2850     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2851 }
2852
2853 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2854 {
2855     ARMCPU *cpu = env_archcpu(env);
2856
2857     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2858 }
2859
2860 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2861     /* Note that CNTFRQ is purely reads-as-written for the benefit
2862      * of software; writing it doesn't actually change the timer frequency.
2863      * Our reset value matches the fixed frequency we implement the timer at.
2864      */
2865     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2866       .type = ARM_CP_ALIAS,
2867       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2868       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2869     },
2870     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2871       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2872       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2873       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2874       .resetfn = arm_gt_cntfrq_reset,
2875     },
2876     /* overall control: mostly access permissions */
2877     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2878       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2879       .access = PL1_RW,
2880       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2881       .resetvalue = 0,
2882     },
2883     /* per-timer control */
2884     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2885       .secure = ARM_CP_SECSTATE_NS,
2886       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2887       .accessfn = gt_ptimer_access,
2888       .fieldoffset = offsetoflow32(CPUARMState,
2889                                    cp15.c14_timer[GTIMER_PHYS].ctl),
2890       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2891       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2892     },
2893     { .name = "CNTP_CTL_S",
2894       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2895       .secure = ARM_CP_SECSTATE_S,
2896       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2897       .accessfn = gt_ptimer_access,
2898       .fieldoffset = offsetoflow32(CPUARMState,
2899                                    cp15.c14_timer[GTIMER_SEC].ctl),
2900       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2901     },
2902     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2903       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2904       .type = ARM_CP_IO, .access = PL0_RW,
2905       .accessfn = gt_ptimer_access,
2906       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2907       .resetvalue = 0,
2908       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2909       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2910     },
2911     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2912       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2913       .accessfn = gt_vtimer_access,
2914       .fieldoffset = offsetoflow32(CPUARMState,
2915                                    cp15.c14_timer[GTIMER_VIRT].ctl),
2916       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2917       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2918     },
2919     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2920       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2921       .type = ARM_CP_IO, .access = PL0_RW,
2922       .accessfn = gt_vtimer_access,
2923       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2924       .resetvalue = 0,
2925       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2926       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2927     },
2928     /* TimerValue views: a 32 bit downcounting view of the underlying state */
2929     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2930       .secure = ARM_CP_SECSTATE_NS,
2931       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2932       .accessfn = gt_ptimer_access,
2933       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2934     },
2935     { .name = "CNTP_TVAL_S",
2936       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2937       .secure = ARM_CP_SECSTATE_S,
2938       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2939       .accessfn = gt_ptimer_access,
2940       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2941     },
2942     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2943       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2944       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2945       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2946       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2947     },
2948     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2949       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2950       .accessfn = gt_vtimer_access,
2951       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2952     },
2953     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2954       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2955       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2956       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2957       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2958     },
2959     /* The counter itself */
2960     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2961       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2962       .accessfn = gt_pct_access,
2963       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2964     },
2965     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2966       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2967       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2968       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2969     },
2970     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2971       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2972       .accessfn = gt_vct_access,
2973       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2974     },
2975     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2976       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2977       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2978       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2979     },
2980     /* Comparison value, indicating when the timer goes off */
2981     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2982       .secure = ARM_CP_SECSTATE_NS,
2983       .access = PL0_RW,
2984       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2985       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2986       .accessfn = gt_ptimer_access,
2987       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
2988       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
2989     },
2990     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
2991       .secure = ARM_CP_SECSTATE_S,
2992       .access = PL0_RW,
2993       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2994       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2995       .accessfn = gt_ptimer_access,
2996       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2997     },
2998     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2999       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3000       .access = PL0_RW,
3001       .type = ARM_CP_IO,
3002       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3003       .resetvalue = 0, .accessfn = gt_ptimer_access,
3004       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3005       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3006     },
3007     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3008       .access = PL0_RW,
3009       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3010       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3011       .accessfn = gt_vtimer_access,
3012       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3013       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3014     },
3015     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3016       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3017       .access = PL0_RW,
3018       .type = ARM_CP_IO,
3019       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3020       .resetvalue = 0, .accessfn = gt_vtimer_access,
3021       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3022       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3023     },
3024     /* Secure timer -- this is actually restricted to only EL3
3025      * and configurably Secure-EL1 via the accessfn.
3026      */
3027     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3028       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3029       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3030       .accessfn = gt_stimer_access,
3031       .readfn = gt_sec_tval_read,
3032       .writefn = gt_sec_tval_write,
3033       .resetfn = gt_sec_timer_reset,
3034     },
3035     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3036       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3037       .type = ARM_CP_IO, .access = PL1_RW,
3038       .accessfn = gt_stimer_access,
3039       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3040       .resetvalue = 0,
3041       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3042     },
3043     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3044       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3045       .type = ARM_CP_IO, .access = PL1_RW,
3046       .accessfn = gt_stimer_access,
3047       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3048       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3049     },
3050 };
3051
3052 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3053                                  bool isread)
3054 {
3055     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3056         return CP_ACCESS_TRAP;
3057     }
3058     return CP_ACCESS_OK;
3059 }
3060
3061 #else
3062
3063 /* In user-mode most of the generic timer registers are inaccessible
3064  * however modern kernels (4.12+) allow access to cntvct_el0
3065  */
3066
3067 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3068 {
3069     ARMCPU *cpu = env_archcpu(env);
3070
3071     /* Currently we have no support for QEMUTimer in linux-user so we
3072      * can't call gt_get_countervalue(env), instead we directly
3073      * call the lower level functions.
3074      */
3075     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3076 }
3077
3078 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3079     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3080       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3081       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3082       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3083       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3084     },
3085     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3086       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3087       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3088       .readfn = gt_virt_cnt_read,
3089     },
3090 };
3091
3092 #endif
3093
3094 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3095 {
3096     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3097         raw_write(env, ri, value);
3098     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3099         raw_write(env, ri, value & 0xfffff6ff);
3100     } else {
3101         raw_write(env, ri, value & 0xfffff1ff);
3102     }
3103 }
3104
3105 #ifndef CONFIG_USER_ONLY
3106 /* get_phys_addr() isn't present for user-mode-only targets */
3107
3108 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3109                                  bool isread)
3110 {
3111     if (ri->opc2 & 4) {
3112         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3113          * Secure EL1 (which can only happen if EL3 is AArch64).
3114          * They are simply UNDEF if executed from NS EL1.
3115          * They function normally from EL2 or EL3.
3116          */
3117         if (arm_current_el(env) == 1) {
3118             if (arm_is_secure_below_el3(env)) {
3119                 if (env->cp15.scr_el3 & SCR_EEL2) {
3120                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3121                 }
3122                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3123             }
3124             return CP_ACCESS_TRAP_UNCATEGORIZED;
3125         }
3126     }
3127     return CP_ACCESS_OK;
3128 }
3129
3130 #ifdef CONFIG_TCG
3131 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3132                              MMUAccessType access_type, ARMMMUIdx mmu_idx)
3133 {
3134     hwaddr phys_addr;
3135     target_ulong page_size;
3136     int prot;
3137     bool ret;
3138     uint64_t par64;
3139     bool format64 = false;
3140     MemTxAttrs attrs = {};
3141     ARMMMUFaultInfo fi = {};
3142     ARMCacheAttrs cacheattrs = {};
3143
3144     ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3145                         &prot, &page_size, &fi, &cacheattrs);
3146
3147     /*
3148      * ATS operations only do S1 or S1+S2 translations, so we never
3149      * have to deal with the ARMCacheAttrs format for S2 only.
3150      */
3151     assert(!cacheattrs.is_s2_format);
3152
3153     if (ret) {
3154         /*
3155          * Some kinds of translation fault must cause exceptions rather
3156          * than being reported in the PAR.
3157          */
3158         int current_el = arm_current_el(env);
3159         int target_el;
3160         uint32_t syn, fsr, fsc;
3161         bool take_exc = false;
3162
3163         if (fi.s1ptw && current_el == 1
3164             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3165             /*
3166              * Synchronous stage 2 fault on an access made as part of the
3167              * translation table walk for AT S1E0* or AT S1E1* insn
3168              * executed from NS EL1. If this is a synchronous external abort
3169              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3170              * to EL3. Otherwise the fault is taken as an exception to EL2,
3171              * and HPFAR_EL2 holds the faulting IPA.
3172              */
3173             if (fi.type == ARMFault_SyncExternalOnWalk &&
3174                 (env->cp15.scr_el3 & SCR_EA)) {
3175                 target_el = 3;
3176             } else {
3177                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3178                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3179                     env->cp15.hpfar_el2 |= HPFAR_NS;
3180                 }
3181                 target_el = 2;
3182             }
3183             take_exc = true;
3184         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3185             /*
3186              * Synchronous external aborts during a translation table walk
3187              * are taken as Data Abort exceptions.
3188              */
3189             if (fi.stage2) {
3190                 if (current_el == 3) {
3191                     target_el = 3;
3192                 } else {
3193                     target_el = 2;
3194                 }
3195             } else {
3196                 target_el = exception_target_el(env);
3197             }
3198             take_exc = true;
3199         }
3200
3201         if (take_exc) {
3202             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3203             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3204                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3205                 fsr = arm_fi_to_lfsc(&fi);
3206                 fsc = extract32(fsr, 0, 6);
3207             } else {
3208                 fsr = arm_fi_to_sfsc(&fi);
3209                 fsc = 0x3f;
3210             }
3211             /*
3212              * Report exception with ESR indicating a fault due to a
3213              * translation table walk for a cache maintenance instruction.
3214              */
3215             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3216                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3217             env->exception.vaddress = value;
3218             env->exception.fsr = fsr;
3219             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3220         }
3221     }
3222
3223     if (is_a64(env)) {
3224         format64 = true;
3225     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3226         /*
3227          * ATS1Cxx:
3228          * * TTBCR.EAE determines whether the result is returned using the
3229          *   32-bit or the 64-bit PAR format
3230          * * Instructions executed in Hyp mode always use the 64bit format
3231          *
3232          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3233          * * The Non-secure TTBCR.EAE bit is set to 1
3234          * * The implementation includes EL2, and the value of HCR.VM is 1
3235          *
3236          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3237          *
3238          * ATS1Hx always uses the 64bit format.
3239          */
3240         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3241
3242         if (arm_feature(env, ARM_FEATURE_EL2)) {
3243             if (mmu_idx == ARMMMUIdx_E10_0 ||
3244                 mmu_idx == ARMMMUIdx_E10_1 ||
3245                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3246                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3247             } else {
3248                 format64 |= arm_current_el(env) == 2;
3249             }
3250         }
3251     }
3252
3253     if (format64) {
3254         /* Create a 64-bit PAR */
3255         par64 = (1 << 11); /* LPAE bit always set */
3256         if (!ret) {
3257             par64 |= phys_addr & ~0xfffULL;
3258             if (!attrs.secure) {
3259                 par64 |= (1 << 9); /* NS */
3260             }
3261             par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3262             par64 |= cacheattrs.shareability << 7; /* SH */
3263         } else {
3264             uint32_t fsr = arm_fi_to_lfsc(&fi);
3265
3266             par64 |= 1; /* F */
3267             par64 |= (fsr & 0x3f) << 1; /* FS */
3268             if (fi.stage2) {
3269                 par64 |= (1 << 9); /* S */
3270             }
3271             if (fi.s1ptw) {
3272                 par64 |= (1 << 8); /* PTW */
3273             }
3274         }
3275     } else {
3276         /* fsr is a DFSR/IFSR value for the short descriptor
3277          * translation table format (with WnR always clear).
3278          * Convert it to a 32-bit PAR.
3279          */
3280         if (!ret) {
3281             /* We do not set any attribute bits in the PAR */
3282             if (page_size == (1 << 24)
3283                 && arm_feature(env, ARM_FEATURE_V7)) {
3284                 par64 = (phys_addr & 0xff000000) | (1 << 1);
3285             } else {
3286                 par64 = phys_addr & 0xfffff000;
3287             }
3288             if (!attrs.secure) {
3289                 par64 |= (1 << 9); /* NS */
3290             }
3291         } else {
3292             uint32_t fsr = arm_fi_to_sfsc(&fi);
3293
3294             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3295                     ((fsr & 0xf) << 1) | 1;
3296         }
3297     }
3298     return par64;
3299 }
3300 #endif /* CONFIG_TCG */
3301
3302 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3303 {
3304 #ifdef CONFIG_TCG
3305     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3306     uint64_t par64;
3307     ARMMMUIdx mmu_idx;
3308     int el = arm_current_el(env);
3309     bool secure = arm_is_secure_below_el3(env);
3310
3311     switch (ri->opc2 & 6) {
3312     case 0:
3313         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3314         switch (el) {
3315         case 3:
3316             mmu_idx = ARMMMUIdx_SE3;
3317             break;
3318         case 2:
3319             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3320             /* fall through */
3321         case 1:
3322             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3323                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3324                            : ARMMMUIdx_Stage1_E1_PAN);
3325             } else {
3326                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3327             }
3328             break;
3329         default:
3330             g_assert_not_reached();
3331         }
3332         break;
3333     case 2:
3334         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3335         switch (el) {
3336         case 3:
3337             mmu_idx = ARMMMUIdx_SE10_0;
3338             break;
3339         case 2:
3340             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3341             mmu_idx = ARMMMUIdx_Stage1_E0;
3342             break;
3343         case 1:
3344             mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3345             break;
3346         default:
3347             g_assert_not_reached();
3348         }
3349         break;
3350     case 4:
3351         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3352         mmu_idx = ARMMMUIdx_E10_1;
3353         break;
3354     case 6:
3355         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3356         mmu_idx = ARMMMUIdx_E10_0;
3357         break;
3358     default:
3359         g_assert_not_reached();
3360     }
3361
3362     par64 = do_ats_write(env, value, access_type, mmu_idx);
3363
3364     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3365 #else
3366     /* Handled by hardware accelerator. */
3367     g_assert_not_reached();
3368 #endif /* CONFIG_TCG */
3369 }
3370
3371 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3372                         uint64_t value)
3373 {
3374 #ifdef CONFIG_TCG
3375     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3376     uint64_t par64;
3377
3378     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3379
3380     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3381 #else
3382     /* Handled by hardware accelerator. */
3383     g_assert_not_reached();
3384 #endif /* CONFIG_TCG */
3385 }
3386
3387 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3388                                      bool isread)
3389 {
3390     if (arm_current_el(env) == 3 &&
3391         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3392         return CP_ACCESS_TRAP;
3393     }
3394     return CP_ACCESS_OK;
3395 }
3396
3397 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3398                         uint64_t value)
3399 {
3400 #ifdef CONFIG_TCG
3401     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3402     ARMMMUIdx mmu_idx;
3403     int secure = arm_is_secure_below_el3(env);
3404
3405     switch (ri->opc2 & 6) {
3406     case 0:
3407         switch (ri->opc1) {
3408         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3409             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3410                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3411                            : ARMMMUIdx_Stage1_E1_PAN);
3412             } else {
3413                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3414             }
3415             break;
3416         case 4: /* AT S1E2R, AT S1E2W */
3417             mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3418             break;
3419         case 6: /* AT S1E3R, AT S1E3W */
3420             mmu_idx = ARMMMUIdx_SE3;
3421             break;
3422         default:
3423             g_assert_not_reached();
3424         }
3425         break;
3426     case 2: /* AT S1E0R, AT S1E0W */
3427         mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3428         break;
3429     case 4: /* AT S12E1R, AT S12E1W */
3430         mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3431         break;
3432     case 6: /* AT S12E0R, AT S12E0W */
3433         mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3434         break;
3435     default:
3436         g_assert_not_reached();
3437     }
3438
3439     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3440 #else
3441     /* Handled by hardware accelerator. */
3442     g_assert_not_reached();
3443 #endif /* CONFIG_TCG */
3444 }
3445 #endif
3446
3447 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3448     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3449       .access = PL1_RW, .resetvalue = 0,
3450       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3451                              offsetoflow32(CPUARMState, cp15.par_ns) },
3452       .writefn = par_write },
3453 #ifndef CONFIG_USER_ONLY
3454     /* This underdecoding is safe because the reginfo is NO_RAW. */
3455     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3456       .access = PL1_W, .accessfn = ats_access,
3457       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3458 #endif
3459 };
3460
3461 /* Return basic MPU access permission bits.  */
3462 static uint32_t simple_mpu_ap_bits(uint32_t val)
3463 {
3464     uint32_t ret;
3465     uint32_t mask;
3466     int i;
3467     ret = 0;
3468     mask = 3;
3469     for (i = 0; i < 16; i += 2) {
3470         ret |= (val >> i) & mask;
3471         mask <<= 2;
3472     }
3473     return ret;
3474 }
3475
3476 /* Pad basic MPU access permission bits to extended format.  */
3477 static uint32_t extended_mpu_ap_bits(uint32_t val)
3478 {
3479     uint32_t ret;
3480     uint32_t mask;
3481     int i;
3482     ret = 0;
3483     mask = 3;
3484     for (i = 0; i < 16; i += 2) {
3485         ret |= (val & mask) << i;
3486         mask <<= 2;
3487     }
3488     return ret;
3489 }
3490
3491 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3492                                  uint64_t value)
3493 {
3494     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3495 }
3496
3497 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3498 {
3499     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3500 }
3501
3502 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3503                                  uint64_t value)
3504 {
3505     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3506 }
3507
3508 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3509 {
3510     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3511 }
3512
3513 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3514 {
3515     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3516
3517     if (!u32p) {
3518         return 0;
3519     }
3520
3521     u32p += env->pmsav7.rnr[M_REG_NS];
3522     return *u32p;
3523 }
3524
3525 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3526                          uint64_t value)
3527 {
3528     ARMCPU *cpu = env_archcpu(env);
3529     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3530
3531     if (!u32p) {
3532         return;
3533     }
3534
3535     u32p += env->pmsav7.rnr[M_REG_NS];
3536     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3537     *u32p = value;
3538 }
3539
3540 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3541                               uint64_t value)
3542 {
3543     ARMCPU *cpu = env_archcpu(env);
3544     uint32_t nrgs = cpu->pmsav7_dregion;
3545
3546     if (value >= nrgs) {
3547         qemu_log_mask(LOG_GUEST_ERROR,
3548                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3549                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3550         return;
3551     }
3552
3553     raw_write(env, ri, value);
3554 }
3555
3556 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3557     /* Reset for all these registers is handled in arm_cpu_reset(),
3558      * because the PMSAv7 is also used by M-profile CPUs, which do
3559      * not register cpregs but still need the state to be reset.
3560      */
3561     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3562       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3563       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3564       .readfn = pmsav7_read, .writefn = pmsav7_write,
3565       .resetfn = arm_cp_reset_ignore },
3566     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3567       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3568       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3569       .readfn = pmsav7_read, .writefn = pmsav7_write,
3570       .resetfn = arm_cp_reset_ignore },
3571     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3572       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3573       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3574       .readfn = pmsav7_read, .writefn = pmsav7_write,
3575       .resetfn = arm_cp_reset_ignore },
3576     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3577       .access = PL1_RW,
3578       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3579       .writefn = pmsav7_rgnr_write,
3580       .resetfn = arm_cp_reset_ignore },
3581 };
3582
3583 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3584     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3585       .access = PL1_RW, .type = ARM_CP_ALIAS,
3586       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3587       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3588     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3589       .access = PL1_RW, .type = ARM_CP_ALIAS,
3590       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3591       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3592     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3593       .access = PL1_RW,
3594       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3595       .resetvalue = 0, },
3596     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3597       .access = PL1_RW,
3598       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3599       .resetvalue = 0, },
3600     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3601       .access = PL1_RW,
3602       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3603     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3604       .access = PL1_RW,
3605       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3606     /* Protection region base and size registers */
3607     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3608       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3609       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3610     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3611       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3612       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3613     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3614       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3615       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3616     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3617       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3618       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3619     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3620       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3621       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3622     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3623       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3624       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3625     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3626       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3627       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3628     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3629       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3630       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3631 };
3632
3633 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3634                              uint64_t value)
3635 {
3636     ARMCPU *cpu = env_archcpu(env);
3637
3638     if (!arm_feature(env, ARM_FEATURE_V8)) {
3639         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3640             /*
3641              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3642              * using Long-descriptor translation table format
3643              */
3644             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3645         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3646             /*
3647              * In an implementation that includes the Security Extensions
3648              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3649              * Short-descriptor translation table format.
3650              */
3651             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3652         } else {
3653             value &= TTBCR_N;
3654         }
3655     }
3656
3657     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3658         /* With LPAE the TTBCR could result in a change of ASID
3659          * via the TTBCR.A1 bit, so do a TLB flush.
3660          */
3661         tlb_flush(CPU(cpu));
3662     }
3663     raw_write(env, ri, value);
3664 }
3665
3666 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3667                                uint64_t value)
3668 {
3669     ARMCPU *cpu = env_archcpu(env);
3670
3671     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3672     tlb_flush(CPU(cpu));
3673     raw_write(env, ri, value);
3674 }
3675
3676 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3677                             uint64_t value)
3678 {
3679     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3680     if (cpreg_field_is_64bit(ri) &&
3681         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3682         ARMCPU *cpu = env_archcpu(env);
3683         tlb_flush(CPU(cpu));
3684     }
3685     raw_write(env, ri, value);
3686 }
3687
3688 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3689                                     uint64_t value)
3690 {
3691     /*
3692      * If we are running with E2&0 regime, then an ASID is active.
3693      * Flush if that might be changing.  Note we're not checking
3694      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3695      * holds the active ASID, only checking the field that might.
3696      */
3697     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3698         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3699         uint16_t mask = ARMMMUIdxBit_E20_2 |
3700                         ARMMMUIdxBit_E20_2_PAN |
3701                         ARMMMUIdxBit_E20_0;
3702
3703         if (arm_is_secure_below_el3(env)) {
3704             mask >>= ARM_MMU_IDX_A_NS;
3705         }
3706
3707         tlb_flush_by_mmuidx(env_cpu(env), mask);
3708     }
3709     raw_write(env, ri, value);
3710 }
3711
3712 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3713                         uint64_t value)
3714 {
3715     ARMCPU *cpu = env_archcpu(env);
3716     CPUState *cs = CPU(cpu);
3717
3718     /*
3719      * A change in VMID to the stage2 page table (Stage2) invalidates
3720      * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3721      */
3722     if (raw_read(env, ri) != value) {
3723         uint16_t mask = ARMMMUIdxBit_E10_1 |
3724                         ARMMMUIdxBit_E10_1_PAN |
3725                         ARMMMUIdxBit_E10_0;
3726
3727         if (arm_is_secure_below_el3(env)) {
3728             mask >>= ARM_MMU_IDX_A_NS;
3729         }
3730
3731         tlb_flush_by_mmuidx(cs, mask);
3732         raw_write(env, ri, value);
3733     }
3734 }
3735
3736 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3737     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3738       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3739       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3740                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3741     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3742       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3743       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3744                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3745     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3746       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3747       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3748                              offsetof(CPUARMState, cp15.dfar_ns) } },
3749     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3750       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3751       .access = PL1_RW, .accessfn = access_tvm_trvm,
3752       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3753       .resetvalue = 0, },
3754 };
3755
3756 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3757     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3758       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3759       .access = PL1_RW, .accessfn = access_tvm_trvm,
3760       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3761     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3762       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3763       .access = PL1_RW, .accessfn = access_tvm_trvm,
3764       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3765       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3766                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3767     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3768       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3769       .access = PL1_RW, .accessfn = access_tvm_trvm,
3770       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3771       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3772                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3773     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3774       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3775       .access = PL1_RW, .accessfn = access_tvm_trvm,
3776       .writefn = vmsa_tcr_el12_write,
3777       .raw_writefn = raw_write,
3778       .resetvalue = 0,
3779       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3780     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3781       .access = PL1_RW, .accessfn = access_tvm_trvm,
3782       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3783       .raw_writefn = raw_write,
3784       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3785                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3786 };
3787
3788 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3789  * qemu tlbs nor adjusting cached masks.
3790  */
3791 static const ARMCPRegInfo ttbcr2_reginfo = {
3792     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3793     .access = PL1_RW, .accessfn = access_tvm_trvm,
3794     .type = ARM_CP_ALIAS,
3795     .bank_fieldoffsets = {
3796         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3797         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
3798     },
3799 };
3800
3801 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3802                                 uint64_t value)
3803 {
3804     env->cp15.c15_ticonfig = value & 0xe7;
3805     /* The OS_TYPE bit in this register changes the reported CPUID! */
3806     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3807         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3808 }
3809
3810 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3811                                 uint64_t value)
3812 {
3813     env->cp15.c15_threadid = value & 0xffff;
3814 }
3815
3816 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3817                            uint64_t value)
3818 {
3819     /* Wait-for-interrupt (deprecated) */
3820     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3821 }
3822
3823 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3824                                   uint64_t value)
3825 {
3826     /* On OMAP there are registers indicating the max/min index of dcache lines
3827      * containing a dirty line; cache flush operations have to reset these.
3828      */
3829     env->cp15.c15_i_max = 0x000;
3830     env->cp15.c15_i_min = 0xff0;
3831 }
3832
3833 static const ARMCPRegInfo omap_cp_reginfo[] = {
3834     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3835       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3836       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3837       .resetvalue = 0, },
3838     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3839       .access = PL1_RW, .type = ARM_CP_NOP },
3840     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3841       .access = PL1_RW,
3842       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3843       .writefn = omap_ticonfig_write },
3844     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3845       .access = PL1_RW,
3846       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3847     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3848       .access = PL1_RW, .resetvalue = 0xff0,
3849       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3850     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3851       .access = PL1_RW,
3852       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3853       .writefn = omap_threadid_write },
3854     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3855       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3856       .type = ARM_CP_NO_RAW,
3857       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3858     /* TODO: Peripheral port remap register:
3859      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3860      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3861      * when MMU is off.
3862      */
3863     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3864       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3865       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3866       .writefn = omap_cachemaint_write },
3867     { .name = "C9", .cp = 15, .crn = 9,
3868       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3869       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3870 };
3871
3872 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3873                               uint64_t value)
3874 {
3875     env->cp15.c15_cpar = value & 0x3fff;
3876 }
3877
3878 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3879     { .name = "XSCALE_CPAR",
3880       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3881       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3882       .writefn = xscale_cpar_write, },
3883     { .name = "XSCALE_AUXCR",
3884       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3885       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3886       .resetvalue = 0, },
3887     /* XScale specific cache-lockdown: since we have no cache we NOP these
3888      * and hope the guest does not really rely on cache behaviour.
3889      */
3890     { .name = "XSCALE_LOCK_ICACHE_LINE",
3891       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3892       .access = PL1_W, .type = ARM_CP_NOP },
3893     { .name = "XSCALE_UNLOCK_ICACHE",
3894       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3895       .access = PL1_W, .type = ARM_CP_NOP },
3896     { .name = "XSCALE_DCACHE_LOCK",
3897       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3898       .access = PL1_RW, .type = ARM_CP_NOP },
3899     { .name = "XSCALE_UNLOCK_DCACHE",
3900       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3901       .access = PL1_W, .type = ARM_CP_NOP },
3902 };
3903
3904 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3905     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3906      * implementation of this implementation-defined space.
3907      * Ideally this should eventually disappear in favour of actually
3908      * implementing the correct behaviour for all cores.
3909      */
3910     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3911       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3912       .access = PL1_RW,
3913       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3914       .resetvalue = 0 },
3915 };
3916
3917 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3918     /* Cache status: RAZ because we have no cache so it's always clean */
3919     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3920       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3921       .resetvalue = 0 },
3922 };
3923
3924 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3925     /* We never have a block transfer operation in progress */
3926     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3927       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3928       .resetvalue = 0 },
3929     /* The cache ops themselves: these all NOP for QEMU */
3930     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3931       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3932     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3933       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3934     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3935       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3936     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3937       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3938     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3939       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3940     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3941       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3942 };
3943
3944 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3945     /* The cache test-and-clean instructions always return (1 << 30)
3946      * to indicate that there are no dirty cache lines.
3947      */
3948     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
3949       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3950       .resetvalue = (1 << 30) },
3951     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
3952       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3953       .resetvalue = (1 << 30) },
3954 };
3955
3956 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
3957     /* Ignore ReadBuffer accesses */
3958     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
3959       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3960       .access = PL1_RW, .resetvalue = 0,
3961       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
3962 };
3963
3964 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3965 {
3966     unsigned int cur_el = arm_current_el(env);
3967
3968     if (arm_is_el2_enabled(env) && cur_el == 1) {
3969         return env->cp15.vpidr_el2;
3970     }
3971     return raw_read(env, ri);
3972 }
3973
3974 static uint64_t mpidr_read_val(CPUARMState *env)
3975 {
3976     ARMCPU *cpu = env_archcpu(env);
3977     uint64_t mpidr = cpu->mp_affinity;
3978
3979     if (arm_feature(env, ARM_FEATURE_V7MP)) {
3980         mpidr |= (1U << 31);
3981         /* Cores which are uniprocessor (non-coherent)
3982          * but still implement the MP extensions set
3983          * bit 30. (For instance, Cortex-R5).
3984          */
3985         if (cpu->mp_is_up) {
3986             mpidr |= (1u << 30);
3987         }
3988     }
3989     return mpidr;
3990 }
3991
3992 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3993 {
3994     unsigned int cur_el = arm_current_el(env);
3995
3996     if (arm_is_el2_enabled(env) && cur_el == 1) {
3997         return env->cp15.vmpidr_el2;
3998     }
3999     return mpidr_read_val(env);
4000 }
4001
4002 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4003     /* NOP AMAIR0/1 */
4004     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4005       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4006       .access = PL1_RW, .accessfn = access_tvm_trvm,
4007       .type = ARM_CP_CONST, .resetvalue = 0 },
4008     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4009     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4010       .access = PL1_RW, .accessfn = access_tvm_trvm,
4011       .type = ARM_CP_CONST, .resetvalue = 0 },
4012     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4013       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4014       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4015                              offsetof(CPUARMState, cp15.par_ns)} },
4016     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4017       .access = PL1_RW, .accessfn = access_tvm_trvm,
4018       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4019       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4020                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4021       .writefn = vmsa_ttbr_write, },
4022     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4023       .access = PL1_RW, .accessfn = access_tvm_trvm,
4024       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4025       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4026                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4027       .writefn = vmsa_ttbr_write, },
4028 };
4029
4030 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4031 {
4032     return vfp_get_fpcr(env);
4033 }
4034
4035 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4036                             uint64_t value)
4037 {
4038     vfp_set_fpcr(env, value);
4039 }
4040
4041 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4042 {
4043     return vfp_get_fpsr(env);
4044 }
4045
4046 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4047                             uint64_t value)
4048 {
4049     vfp_set_fpsr(env, value);
4050 }
4051
4052 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4053                                        bool isread)
4054 {
4055     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4056         return CP_ACCESS_TRAP;
4057     }
4058     return CP_ACCESS_OK;
4059 }
4060
4061 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4062                             uint64_t value)
4063 {
4064     env->daif = value & PSTATE_DAIF;
4065 }
4066
4067 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4068 {
4069     return env->pstate & PSTATE_PAN;
4070 }
4071
4072 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4073                            uint64_t value)
4074 {
4075     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4076 }
4077
4078 static const ARMCPRegInfo pan_reginfo = {
4079     .name = "PAN", .state = ARM_CP_STATE_AA64,
4080     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4081     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4082     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4083 };
4084
4085 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4086 {
4087     return env->pstate & PSTATE_UAO;
4088 }
4089
4090 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4091                            uint64_t value)
4092 {
4093     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4094 }
4095
4096 static const ARMCPRegInfo uao_reginfo = {
4097     .name = "UAO", .state = ARM_CP_STATE_AA64,
4098     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4099     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4100     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4101 };
4102
4103 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4104 {
4105     return env->pstate & PSTATE_DIT;
4106 }
4107
4108 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4109                            uint64_t value)
4110 {
4111     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4112 }
4113
4114 static const ARMCPRegInfo dit_reginfo = {
4115     .name = "DIT", .state = ARM_CP_STATE_AA64,
4116     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4117     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4118     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4119 };
4120
4121 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4122 {
4123     return env->pstate & PSTATE_SSBS;
4124 }
4125
4126 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4127                            uint64_t value)
4128 {
4129     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4130 }
4131
4132 static const ARMCPRegInfo ssbs_reginfo = {
4133     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4134     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4135     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4136     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4137 };
4138
4139 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4140                                               const ARMCPRegInfo *ri,
4141                                               bool isread)
4142 {
4143     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4144     switch (arm_current_el(env)) {
4145     case 0:
4146         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4147         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4148             return CP_ACCESS_TRAP;
4149         }
4150         /* fall through */
4151     case 1:
4152         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4153         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4154             return CP_ACCESS_TRAP_EL2;
4155         }
4156         break;
4157     }
4158     return CP_ACCESS_OK;
4159 }
4160
4161 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4162                                               const ARMCPRegInfo *ri,
4163                                               bool isread)
4164 {
4165     /* Cache invalidate/clean to Point of Unification... */
4166     switch (arm_current_el(env)) {
4167     case 0:
4168         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4169         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4170             return CP_ACCESS_TRAP;
4171         }
4172         /* fall through */
4173     case 1:
4174         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4175         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4176             return CP_ACCESS_TRAP_EL2;
4177         }
4178         break;
4179     }
4180     return CP_ACCESS_OK;
4181 }
4182
4183 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4184  * Page D4-1736 (DDI0487A.b)
4185  */
4186
4187 static int vae1_tlbmask(CPUARMState *env)
4188 {
4189     uint64_t hcr = arm_hcr_el2_eff(env);
4190     uint16_t mask;
4191
4192     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4193         mask = ARMMMUIdxBit_E20_2 |
4194                ARMMMUIdxBit_E20_2_PAN |
4195                ARMMMUIdxBit_E20_0;
4196     } else {
4197         mask = ARMMMUIdxBit_E10_1 |
4198                ARMMMUIdxBit_E10_1_PAN |
4199                ARMMMUIdxBit_E10_0;
4200     }
4201
4202     if (arm_is_secure_below_el3(env)) {
4203         mask >>= ARM_MMU_IDX_A_NS;
4204     }
4205
4206     return mask;
4207 }
4208
4209 /* Return 56 if TBI is enabled, 64 otherwise. */
4210 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4211                               uint64_t addr)
4212 {
4213     uint64_t tcr = regime_tcr(env, mmu_idx);
4214     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4215     int select = extract64(addr, 55, 1);
4216
4217     return (tbi >> select) & 1 ? 56 : 64;
4218 }
4219
4220 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4221 {
4222     uint64_t hcr = arm_hcr_el2_eff(env);
4223     ARMMMUIdx mmu_idx;
4224
4225     /* Only the regime of the mmu_idx below is significant. */
4226     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4227         mmu_idx = ARMMMUIdx_E20_0;
4228     } else {
4229         mmu_idx = ARMMMUIdx_E10_0;
4230     }
4231
4232     if (arm_is_secure_below_el3(env)) {
4233         mmu_idx &= ~ARM_MMU_IDX_A_NS;
4234     }
4235
4236     return tlbbits_for_regime(env, mmu_idx, addr);
4237 }
4238
4239 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4240                                       uint64_t value)
4241 {
4242     CPUState *cs = env_cpu(env);
4243     int mask = vae1_tlbmask(env);
4244
4245     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4246 }
4247
4248 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4249                                     uint64_t value)
4250 {
4251     CPUState *cs = env_cpu(env);
4252     int mask = vae1_tlbmask(env);
4253
4254     if (tlb_force_broadcast(env)) {
4255         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4256     } else {
4257         tlb_flush_by_mmuidx(cs, mask);
4258     }
4259 }
4260
4261 static int alle1_tlbmask(CPUARMState *env)
4262 {
4263     /*
4264      * Note that the 'ALL' scope must invalidate both stage 1 and
4265      * stage 2 translations, whereas most other scopes only invalidate
4266      * stage 1 translations.
4267      */
4268     if (arm_is_secure_below_el3(env)) {
4269         return ARMMMUIdxBit_SE10_1 |
4270                ARMMMUIdxBit_SE10_1_PAN |
4271                ARMMMUIdxBit_SE10_0;
4272     } else {
4273         return ARMMMUIdxBit_E10_1 |
4274                ARMMMUIdxBit_E10_1_PAN |
4275                ARMMMUIdxBit_E10_0;
4276     }
4277 }
4278
4279 static int e2_tlbmask(CPUARMState *env)
4280 {
4281     if (arm_is_secure_below_el3(env)) {
4282         return ARMMMUIdxBit_SE20_0 |
4283                ARMMMUIdxBit_SE20_2 |
4284                ARMMMUIdxBit_SE20_2_PAN |
4285                ARMMMUIdxBit_SE2;
4286     } else {
4287         return ARMMMUIdxBit_E20_0 |
4288                ARMMMUIdxBit_E20_2 |
4289                ARMMMUIdxBit_E20_2_PAN |
4290                ARMMMUIdxBit_E2;
4291     }
4292 }
4293
4294 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4295                                   uint64_t value)
4296 {
4297     CPUState *cs = env_cpu(env);
4298     int mask = alle1_tlbmask(env);
4299
4300     tlb_flush_by_mmuidx(cs, mask);
4301 }
4302
4303 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4304                                   uint64_t value)
4305 {
4306     CPUState *cs = env_cpu(env);
4307     int mask = e2_tlbmask(env);
4308
4309     tlb_flush_by_mmuidx(cs, mask);
4310 }
4311
4312 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4313                                   uint64_t value)
4314 {
4315     ARMCPU *cpu = env_archcpu(env);
4316     CPUState *cs = CPU(cpu);
4317
4318     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4319 }
4320
4321 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4322                                     uint64_t value)
4323 {
4324     CPUState *cs = env_cpu(env);
4325     int mask = alle1_tlbmask(env);
4326
4327     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4328 }
4329
4330 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4331                                     uint64_t value)
4332 {
4333     CPUState *cs = env_cpu(env);
4334     int mask = e2_tlbmask(env);
4335
4336     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4337 }
4338
4339 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4340                                     uint64_t value)
4341 {
4342     CPUState *cs = env_cpu(env);
4343
4344     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4345 }
4346
4347 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4348                                  uint64_t value)
4349 {
4350     /* Invalidate by VA, EL2
4351      * Currently handles both VAE2 and VALE2, since we don't support
4352      * flush-last-level-only.
4353      */
4354     CPUState *cs = env_cpu(env);
4355     int mask = e2_tlbmask(env);
4356     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4357
4358     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4359 }
4360
4361 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4362                                  uint64_t value)
4363 {
4364     /* Invalidate by VA, EL3
4365      * Currently handles both VAE3 and VALE3, since we don't support
4366      * flush-last-level-only.
4367      */
4368     ARMCPU *cpu = env_archcpu(env);
4369     CPUState *cs = CPU(cpu);
4370     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4371
4372     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4373 }
4374
4375 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4376                                    uint64_t value)
4377 {
4378     CPUState *cs = env_cpu(env);
4379     int mask = vae1_tlbmask(env);
4380     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4381     int bits = vae1_tlbbits(env, pageaddr);
4382
4383     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4384 }
4385
4386 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4387                                  uint64_t value)
4388 {
4389     /* Invalidate by VA, EL1&0 (AArch64 version).
4390      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4391      * since we don't support flush-for-specific-ASID-only or
4392      * flush-last-level-only.
4393      */
4394     CPUState *cs = env_cpu(env);
4395     int mask = vae1_tlbmask(env);
4396     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4397     int bits = vae1_tlbbits(env, pageaddr);
4398
4399     if (tlb_force_broadcast(env)) {
4400         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4401     } else {
4402         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4403     }
4404 }
4405
4406 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4407                                    uint64_t value)
4408 {
4409     CPUState *cs = env_cpu(env);
4410     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4411     bool secure = arm_is_secure_below_el3(env);
4412     int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4413     int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4414                                   pageaddr);
4415
4416     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4417 }
4418
4419 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4420                                    uint64_t value)
4421 {
4422     CPUState *cs = env_cpu(env);
4423     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4424     int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4425
4426     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4427                                                   ARMMMUIdxBit_SE3, bits);
4428 }
4429
4430 #ifdef TARGET_AARCH64
4431 typedef struct {
4432     uint64_t base;
4433     uint64_t length;
4434 } TLBIRange;
4435
4436 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4437                                      uint64_t value)
4438 {
4439     unsigned int page_size_granule, page_shift, num, scale, exponent;
4440     /* Extract one bit to represent the va selector in use. */
4441     uint64_t select = sextract64(value, 36, 1);
4442     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4443     TLBIRange ret = { };
4444
4445     page_size_granule = extract64(value, 46, 2);
4446
4447     /* The granule encoded in value must match the granule in use. */
4448     if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4449         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4450                       page_size_granule);
4451         return ret;
4452     }
4453
4454     page_shift = (page_size_granule - 1) * 2 + 12;
4455     num = extract64(value, 39, 5);
4456     scale = extract64(value, 44, 2);
4457     exponent = (5 * scale) + 1;
4458
4459     ret.length = (num + 1) << (exponent + page_shift);
4460
4461     if (param.select) {
4462         ret.base = sextract64(value, 0, 37);
4463     } else {
4464         ret.base = extract64(value, 0, 37);
4465     }
4466     if (param.ds) {
4467         /*
4468          * With DS=1, BaseADDR is always shifted 16 so that it is able
4469          * to address all 52 va bits.  The input address is perforce
4470          * aligned on a 64k boundary regardless of translation granule.
4471          */
4472         page_shift = 16;
4473     }
4474     ret.base <<= page_shift;
4475
4476     return ret;
4477 }
4478
4479 static void do_rvae_write(CPUARMState *env, uint64_t value,
4480                           int idxmap, bool synced)
4481 {
4482     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4483     TLBIRange range;
4484     int bits;
4485
4486     range = tlbi_aa64_get_range(env, one_idx, value);
4487     bits = tlbbits_for_regime(env, one_idx, range.base);
4488
4489     if (synced) {
4490         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4491                                                   range.base,
4492                                                   range.length,
4493                                                   idxmap,
4494                                                   bits);
4495     } else {
4496         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4497                                   range.length, idxmap, bits);
4498     }
4499 }
4500
4501 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4502                                   const ARMCPRegInfo *ri,
4503                                   uint64_t value)
4504 {
4505     /*
4506      * Invalidate by VA range, EL1&0.
4507      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4508      * since we don't support flush-for-specific-ASID-only or
4509      * flush-last-level-only.
4510      */
4511
4512     do_rvae_write(env, value, vae1_tlbmask(env),
4513                   tlb_force_broadcast(env));
4514 }
4515
4516 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4517                                     const ARMCPRegInfo *ri,
4518                                     uint64_t value)
4519 {
4520     /*
4521      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4522      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4523      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4524      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4525      * shareable specific flushes.
4526      */
4527
4528     do_rvae_write(env, value, vae1_tlbmask(env), true);
4529 }
4530
4531 static int vae2_tlbmask(CPUARMState *env)
4532 {
4533     return (arm_is_secure_below_el3(env)
4534             ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4535 }
4536
4537 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4538                                   const ARMCPRegInfo *ri,
4539                                   uint64_t value)
4540 {
4541     /*
4542      * Invalidate by VA range, EL2.
4543      * Currently handles all of RVAE2 and RVALE2,
4544      * since we don't support flush-for-specific-ASID-only or
4545      * flush-last-level-only.
4546      */
4547
4548     do_rvae_write(env, value, vae2_tlbmask(env),
4549                   tlb_force_broadcast(env));
4550
4551
4552 }
4553
4554 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4555                                     const ARMCPRegInfo *ri,
4556                                     uint64_t value)
4557 {
4558     /*
4559      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4560      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4561      * since we don't support flush-for-specific-ASID-only,
4562      * flush-last-level-only or inner/outer shareable specific flushes.
4563      */
4564
4565     do_rvae_write(env, value, vae2_tlbmask(env), true);
4566
4567 }
4568
4569 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4570                                   const ARMCPRegInfo *ri,
4571                                   uint64_t value)
4572 {
4573     /*
4574      * Invalidate by VA range, EL3.
4575      * Currently handles all of RVAE3 and RVALE3,
4576      * since we don't support flush-for-specific-ASID-only or
4577      * flush-last-level-only.
4578      */
4579
4580     do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4581                   tlb_force_broadcast(env));
4582 }
4583
4584 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4585                                     const ARMCPRegInfo *ri,
4586                                     uint64_t value)
4587 {
4588     /*
4589      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4590      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4591      * since we don't support flush-for-specific-ASID-only,
4592      * flush-last-level-only or inner/outer specific flushes.
4593      */
4594
4595     do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4596 }
4597 #endif
4598
4599 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4600                                       bool isread)
4601 {
4602     int cur_el = arm_current_el(env);
4603
4604     if (cur_el < 2) {
4605         uint64_t hcr = arm_hcr_el2_eff(env);
4606
4607         if (cur_el == 0) {
4608             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4609                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4610                     return CP_ACCESS_TRAP_EL2;
4611                 }
4612             } else {
4613                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4614                     return CP_ACCESS_TRAP;
4615                 }
4616                 if (hcr & HCR_TDZ) {
4617                     return CP_ACCESS_TRAP_EL2;
4618                 }
4619             }
4620         } else if (hcr & HCR_TDZ) {
4621             return CP_ACCESS_TRAP_EL2;
4622         }
4623     }
4624     return CP_ACCESS_OK;
4625 }
4626
4627 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4628 {
4629     ARMCPU *cpu = env_archcpu(env);
4630     int dzp_bit = 1 << 4;
4631
4632     /* DZP indicates whether DC ZVA access is allowed */
4633     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4634         dzp_bit = 0;
4635     }
4636     return cpu->dcz_blocksize | dzp_bit;
4637 }
4638
4639 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4640                                     bool isread)
4641 {
4642     if (!(env->pstate & PSTATE_SP)) {
4643         /* Access to SP_EL0 is undefined if it's being used as
4644          * the stack pointer.
4645          */
4646         return CP_ACCESS_TRAP_UNCATEGORIZED;
4647     }
4648     return CP_ACCESS_OK;
4649 }
4650
4651 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4652 {
4653     return env->pstate & PSTATE_SP;
4654 }
4655
4656 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4657 {
4658     update_spsel(env, val);
4659 }
4660
4661 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4662                         uint64_t value)
4663 {
4664     ARMCPU *cpu = env_archcpu(env);
4665
4666     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4667         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4668         value &= ~SCTLR_M;
4669     }
4670
4671     /* ??? Lots of these bits are not implemented.  */
4672
4673     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4674         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4675             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4676         } else {
4677             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4678                        SCTLR_ATA0 | SCTLR_ATA);
4679         }
4680     }
4681
4682     if (raw_read(env, ri) == value) {
4683         /* Skip the TLB flush if nothing actually changed; Linux likes
4684          * to do a lot of pointless SCTLR writes.
4685          */
4686         return;
4687     }
4688
4689     raw_write(env, ri, value);
4690
4691     /* This may enable/disable the MMU, so do a TLB flush.  */
4692     tlb_flush(CPU(cpu));
4693
4694     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4695         /*
4696          * Normally we would always end the TB on an SCTLR write; see the
4697          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4698          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4699          * of hflags from the translator, so do it here.
4700          */
4701         arm_rebuild_hflags(env);
4702     }
4703 }
4704
4705 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4706                        uint64_t value)
4707 {
4708     /*
4709      * Some MDCR_EL3 bits affect whether PMU counters are running:
4710      * if we are trying to change any of those then we must
4711      * bracket this update with PMU start/finish calls.
4712      */
4713     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4714
4715     if (pmu_op) {
4716         pmu_op_start(env);
4717     }
4718     env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4719     if (pmu_op) {
4720         pmu_op_finish(env);
4721     }
4722 }
4723
4724 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4725                            uint64_t value)
4726 {
4727     /*
4728      * Some MDCR_EL2 bits affect whether PMU counters are running:
4729      * if we are trying to change any of those then we must
4730      * bracket this update with PMU start/finish calls.
4731      */
4732     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4733
4734     if (pmu_op) {
4735         pmu_op_start(env);
4736     }
4737     env->cp15.mdcr_el2 = value;
4738     if (pmu_op) {
4739         pmu_op_finish(env);
4740     }
4741 }
4742
4743 static const ARMCPRegInfo v8_cp_reginfo[] = {
4744     /* Minimal set of EL0-visible registers. This will need to be expanded
4745      * significantly for system emulation of AArch64 CPUs.
4746      */
4747     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4748       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4749       .access = PL0_RW, .type = ARM_CP_NZCV },
4750     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4751       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4752       .type = ARM_CP_NO_RAW,
4753       .access = PL0_RW, .accessfn = aa64_daif_access,
4754       .fieldoffset = offsetof(CPUARMState, daif),
4755       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4756     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4757       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4758       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4759       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4760     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4761       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4762       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4763       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4764     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4765       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4766       .access = PL0_R, .type = ARM_CP_NO_RAW,
4767       .readfn = aa64_dczid_read },
4768     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4769       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4770       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4771 #ifndef CONFIG_USER_ONLY
4772       /* Avoid overhead of an access check that always passes in user-mode */
4773       .accessfn = aa64_zva_access,
4774 #endif
4775     },
4776     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4777       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4778       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4779     /* Cache ops: all NOPs since we don't emulate caches */
4780     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4781       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4782       .access = PL1_W, .type = ARM_CP_NOP,
4783       .accessfn = aa64_cacheop_pou_access },
4784     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4785       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4786       .access = PL1_W, .type = ARM_CP_NOP,
4787       .accessfn = aa64_cacheop_pou_access },
4788     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4789       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4790       .access = PL0_W, .type = ARM_CP_NOP,
4791       .accessfn = aa64_cacheop_pou_access },
4792     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4793       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4794       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4795       .type = ARM_CP_NOP },
4796     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4797       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4798       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4799     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4800       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4801       .access = PL0_W, .type = ARM_CP_NOP,
4802       .accessfn = aa64_cacheop_poc_access },
4803     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4804       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4805       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4806     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4807       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4808       .access = PL0_W, .type = ARM_CP_NOP,
4809       .accessfn = aa64_cacheop_pou_access },
4810     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4811       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4812       .access = PL0_W, .type = ARM_CP_NOP,
4813       .accessfn = aa64_cacheop_poc_access },
4814     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4815       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4816       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4817     /* TLBI operations */
4818     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4819       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4820       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4821       .writefn = tlbi_aa64_vmalle1is_write },
4822     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4823       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4824       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4825       .writefn = tlbi_aa64_vae1is_write },
4826     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4827       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4828       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4829       .writefn = tlbi_aa64_vmalle1is_write },
4830     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4831       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4832       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4833       .writefn = tlbi_aa64_vae1is_write },
4834     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4835       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4836       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4837       .writefn = tlbi_aa64_vae1is_write },
4838     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4839       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4840       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4841       .writefn = tlbi_aa64_vae1is_write },
4842     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4843       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4844       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4845       .writefn = tlbi_aa64_vmalle1_write },
4846     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4847       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4848       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4849       .writefn = tlbi_aa64_vae1_write },
4850     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4851       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4852       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4853       .writefn = tlbi_aa64_vmalle1_write },
4854     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4855       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4856       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4857       .writefn = tlbi_aa64_vae1_write },
4858     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4859       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4860       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4861       .writefn = tlbi_aa64_vae1_write },
4862     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4863       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4864       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4865       .writefn = tlbi_aa64_vae1_write },
4866     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4867       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4868       .access = PL2_W, .type = ARM_CP_NOP },
4869     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4870       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4871       .access = PL2_W, .type = ARM_CP_NOP },
4872     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4873       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4874       .access = PL2_W, .type = ARM_CP_NO_RAW,
4875       .writefn = tlbi_aa64_alle1is_write },
4876     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4877       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4878       .access = PL2_W, .type = ARM_CP_NO_RAW,
4879       .writefn = tlbi_aa64_alle1is_write },
4880     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4881       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4882       .access = PL2_W, .type = ARM_CP_NOP },
4883     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4884       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4885       .access = PL2_W, .type = ARM_CP_NOP },
4886     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4887       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4888       .access = PL2_W, .type = ARM_CP_NO_RAW,
4889       .writefn = tlbi_aa64_alle1_write },
4890     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4891       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4892       .access = PL2_W, .type = ARM_CP_NO_RAW,
4893       .writefn = tlbi_aa64_alle1is_write },
4894 #ifndef CONFIG_USER_ONLY
4895     /* 64 bit address translation operations */
4896     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4897       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4898       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4899       .writefn = ats_write64 },
4900     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4901       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4902       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4903       .writefn = ats_write64 },
4904     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4905       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4906       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4907       .writefn = ats_write64 },
4908     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4909       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4910       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4911       .writefn = ats_write64 },
4912     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4913       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4914       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4915       .writefn = ats_write64 },
4916     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4917       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4918       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4919       .writefn = ats_write64 },
4920     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4921       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4922       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4923       .writefn = ats_write64 },
4924     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4925       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4926       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4927       .writefn = ats_write64 },
4928     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4929     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4930       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4931       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4932       .writefn = ats_write64 },
4933     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4934       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4935       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4936       .writefn = ats_write64 },
4937     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4938       .type = ARM_CP_ALIAS,
4939       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4940       .access = PL1_RW, .resetvalue = 0,
4941       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4942       .writefn = par_write },
4943 #endif
4944     /* TLB invalidate last level of translation table walk */
4945     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4946       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4947       .writefn = tlbimva_is_write },
4948     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4949       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4950       .writefn = tlbimvaa_is_write },
4951     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4952       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4953       .writefn = tlbimva_write },
4954     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4955       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4956       .writefn = tlbimvaa_write },
4957     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4958       .type = ARM_CP_NO_RAW, .access = PL2_W,
4959       .writefn = tlbimva_hyp_write },
4960     { .name = "TLBIMVALHIS",
4961       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4962       .type = ARM_CP_NO_RAW, .access = PL2_W,
4963       .writefn = tlbimva_hyp_is_write },
4964     { .name = "TLBIIPAS2",
4965       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4966       .type = ARM_CP_NOP, .access = PL2_W },
4967     { .name = "TLBIIPAS2IS",
4968       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4969       .type = ARM_CP_NOP, .access = PL2_W },
4970     { .name = "TLBIIPAS2L",
4971       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4972       .type = ARM_CP_NOP, .access = PL2_W },
4973     { .name = "TLBIIPAS2LIS",
4974       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4975       .type = ARM_CP_NOP, .access = PL2_W },
4976     /* 32 bit cache operations */
4977     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4978       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4979     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4980       .type = ARM_CP_NOP, .access = PL1_W },
4981     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4982       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4983     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4984       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4985     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4986       .type = ARM_CP_NOP, .access = PL1_W },
4987     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4988       .type = ARM_CP_NOP, .access = PL1_W },
4989     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4990       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
4991     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4992       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4993     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4994       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
4995     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4996       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4997     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4998       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4999     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5000       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5001     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5002       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5003     /* MMU Domain access control / MPU write buffer control */
5004     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5005       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5006       .writefn = dacr_write, .raw_writefn = raw_write,
5007       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5008                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5009     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5010       .type = ARM_CP_ALIAS,
5011       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5012       .access = PL1_RW,
5013       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5014     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5015       .type = ARM_CP_ALIAS,
5016       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5017       .access = PL1_RW,
5018       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5019     /* We rely on the access checks not allowing the guest to write to the
5020      * state field when SPSel indicates that it's being used as the stack
5021      * pointer.
5022      */
5023     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5024       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5025       .access = PL1_RW, .accessfn = sp_el0_access,
5026       .type = ARM_CP_ALIAS,
5027       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5028     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5029       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5030       .access = PL2_RW, .type = ARM_CP_ALIAS,
5031       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5032     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5033       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5034       .type = ARM_CP_NO_RAW,
5035       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5036     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5037       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5038       .access = PL2_RW,
5039       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5040       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5041     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5042       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5043       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5044       .writefn = dacr_write, .raw_writefn = raw_write,
5045       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5046     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5047       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5048       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5049       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5050     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5051       .type = ARM_CP_ALIAS,
5052       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5053       .access = PL2_RW,
5054       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5055     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5056       .type = ARM_CP_ALIAS,
5057       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5058       .access = PL2_RW,
5059       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5060     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5061       .type = ARM_CP_ALIAS,
5062       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5063       .access = PL2_RW,
5064       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5065     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5066       .type = ARM_CP_ALIAS,
5067       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5068       .access = PL2_RW,
5069       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5070     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5071       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5072       .resetvalue = 0,
5073       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5074     { .name = "SDCR", .type = ARM_CP_ALIAS,
5075       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5076       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5077       .writefn = sdcr_write,
5078       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5079 };
5080
5081 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5082 {
5083     ARMCPU *cpu = env_archcpu(env);
5084
5085     if (arm_feature(env, ARM_FEATURE_V8)) {
5086         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5087     } else {
5088         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5089     }
5090
5091     if (arm_feature(env, ARM_FEATURE_EL3)) {
5092         valid_mask &= ~HCR_HCD;
5093     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5094         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5095          * However, if we're using the SMC PSCI conduit then QEMU is
5096          * effectively acting like EL3 firmware and so the guest at
5097          * EL2 should retain the ability to prevent EL1 from being
5098          * able to make SMC calls into the ersatz firmware, so in
5099          * that case HCR.TSC should be read/write.
5100          */
5101         valid_mask &= ~HCR_TSC;
5102     }
5103
5104     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5105         if (cpu_isar_feature(aa64_vh, cpu)) {
5106             valid_mask |= HCR_E2H;
5107         }
5108         if (cpu_isar_feature(aa64_ras, cpu)) {
5109             valid_mask |= HCR_TERR | HCR_TEA;
5110         }
5111         if (cpu_isar_feature(aa64_lor, cpu)) {
5112             valid_mask |= HCR_TLOR;
5113         }
5114         if (cpu_isar_feature(aa64_pauth, cpu)) {
5115             valid_mask |= HCR_API | HCR_APK;
5116         }
5117         if (cpu_isar_feature(aa64_mte, cpu)) {
5118             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5119         }
5120         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5121             valid_mask |= HCR_ENSCXT;
5122         }
5123         if (cpu_isar_feature(aa64_fwb, cpu)) {
5124             valid_mask |= HCR_FWB;
5125         }
5126     }
5127
5128     /* Clear RES0 bits.  */
5129     value &= valid_mask;
5130
5131     /*
5132      * These bits change the MMU setup:
5133      * HCR_VM enables stage 2 translation
5134      * HCR_PTW forbids certain page-table setups
5135      * HCR_DC disables stage1 and enables stage2 translation
5136      * HCR_DCT enables tagging on (disabled) stage1 translation
5137      * HCR_FWB changes the interpretation of stage2 descriptor bits
5138      */
5139     if ((env->cp15.hcr_el2 ^ value) &
5140         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5141         tlb_flush(CPU(cpu));
5142     }
5143     env->cp15.hcr_el2 = value;
5144
5145     /*
5146      * Updates to VI and VF require us to update the status of
5147      * virtual interrupts, which are the logical OR of these bits
5148      * and the state of the input lines from the GIC. (This requires
5149      * that we have the iothread lock, which is done by marking the
5150      * reginfo structs as ARM_CP_IO.)
5151      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5152      * possible for it to be taken immediately, because VIRQ and
5153      * VFIQ are masked unless running at EL0 or EL1, and HCR
5154      * can only be written at EL2.
5155      */
5156     g_assert(qemu_mutex_iothread_locked());
5157     arm_cpu_update_virq(cpu);
5158     arm_cpu_update_vfiq(cpu);
5159     arm_cpu_update_vserr(cpu);
5160 }
5161
5162 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5163 {
5164     do_hcr_write(env, value, 0);
5165 }
5166
5167 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5168                           uint64_t value)
5169 {
5170     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5171     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5172     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5173 }
5174
5175 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5176                          uint64_t value)
5177 {
5178     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5179     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5180     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5181 }
5182
5183 /*
5184  * Return the effective value of HCR_EL2.
5185  * Bits that are not included here:
5186  * RW       (read from SCR_EL3.RW as needed)
5187  */
5188 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5189 {
5190     uint64_t ret = env->cp15.hcr_el2;
5191
5192     if (!arm_is_el2_enabled(env)) {
5193         /*
5194          * "This register has no effect if EL2 is not enabled in the
5195          * current Security state".  This is ARMv8.4-SecEL2 speak for
5196          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5197          *
5198          * Prior to that, the language was "In an implementation that
5199          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5200          * as if this field is 0 for all purposes other than a direct
5201          * read or write access of HCR_EL2".  With lots of enumeration
5202          * on a per-field basis.  In current QEMU, this is condition
5203          * is arm_is_secure_below_el3.
5204          *
5205          * Since the v8.4 language applies to the entire register, and
5206          * appears to be backward compatible, use that.
5207          */
5208         return 0;
5209     }
5210
5211     /*
5212      * For a cpu that supports both aarch64 and aarch32, we can set bits
5213      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5214      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5215      */
5216     if (!arm_el_is_aa64(env, 2)) {
5217         uint64_t aa32_valid;
5218
5219         /*
5220          * These bits are up-to-date as of ARMv8.6.
5221          * For HCR, it's easiest to list just the 2 bits that are invalid.
5222          * For HCR2, list those that are valid.
5223          */
5224         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5225         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5226                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5227         ret &= aa32_valid;
5228     }
5229
5230     if (ret & HCR_TGE) {
5231         /* These bits are up-to-date as of ARMv8.6.  */
5232         if (ret & HCR_E2H) {
5233             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5234                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5235                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5236                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5237                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5238                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5239         } else {
5240             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5241         }
5242         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5243                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5244                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5245                  HCR_TLOR);
5246     }
5247
5248     return ret;
5249 }
5250
5251 /*
5252  * Corresponds to ARM pseudocode function ELIsInHost().
5253  */
5254 bool el_is_in_host(CPUARMState *env, int el)
5255 {
5256     uint64_t mask;
5257
5258     /*
5259      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5260      * Perform the simplest bit tests first, and validate EL2 afterward.
5261      */
5262     if (el & 1) {
5263         return false; /* EL1 or EL3 */
5264     }
5265
5266     /*
5267      * Note that hcr_write() checks isar_feature_aa64_vh(),
5268      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5269      */
5270     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5271     if ((env->cp15.hcr_el2 & mask) != mask) {
5272         return false;
5273     }
5274
5275     /* TGE and/or E2H set: double check those bits are currently legal. */
5276     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5277 }
5278
5279 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5280                        uint64_t value)
5281 {
5282     uint64_t valid_mask = 0;
5283
5284     /* No features adding bits to HCRX are implemented. */
5285
5286     /* Clear RES0 bits.  */
5287     env->cp15.hcrx_el2 = value & valid_mask;
5288 }
5289
5290 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5291                                   bool isread)
5292 {
5293     if (arm_current_el(env) < 3
5294         && arm_feature(env, ARM_FEATURE_EL3)
5295         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5296         return CP_ACCESS_TRAP_EL3;
5297     }
5298     return CP_ACCESS_OK;
5299 }
5300
5301 static const ARMCPRegInfo hcrx_el2_reginfo = {
5302     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5303     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5304     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5305     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5306 };
5307
5308 /* Return the effective value of HCRX_EL2.  */
5309 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5310 {
5311     /*
5312      * The bits in this register behave as 0 for all purposes other than
5313      * direct reads of the register if:
5314      *   - EL2 is not enabled in the current security state,
5315      *   - SCR_EL3.HXEn is 0.
5316      */
5317     if (!arm_is_el2_enabled(env)
5318         || (arm_feature(env, ARM_FEATURE_EL3)
5319             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5320         return 0;
5321     }
5322     return env->cp15.hcrx_el2;
5323 }
5324
5325 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5326                            uint64_t value)
5327 {
5328     /*
5329      * For A-profile AArch32 EL3, if NSACR.CP10
5330      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5331      */
5332     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5333         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5334         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5335         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5336     }
5337     env->cp15.cptr_el[2] = value;
5338 }
5339
5340 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5341 {
5342     /*
5343      * For A-profile AArch32 EL3, if NSACR.CP10
5344      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5345      */
5346     uint64_t value = env->cp15.cptr_el[2];
5347
5348     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5349         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5350         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5351     }
5352     return value;
5353 }
5354
5355 static const ARMCPRegInfo el2_cp_reginfo[] = {
5356     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5357       .type = ARM_CP_IO,
5358       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5359       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5360       .writefn = hcr_write },
5361     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5362       .type = ARM_CP_ALIAS | ARM_CP_IO,
5363       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5364       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5365       .writefn = hcr_writelow },
5366     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5367       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5368       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5369     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5370       .type = ARM_CP_ALIAS,
5371       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5372       .access = PL2_RW,
5373       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5374     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5375       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5376       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5377     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5378       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5379       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5380     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5381       .type = ARM_CP_ALIAS,
5382       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5383       .access = PL2_RW,
5384       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5385     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5386       .type = ARM_CP_ALIAS,
5387       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5388       .access = PL2_RW,
5389       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5390     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5391       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5392       .access = PL2_RW, .writefn = vbar_write,
5393       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5394       .resetvalue = 0 },
5395     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5396       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5397       .access = PL3_RW, .type = ARM_CP_ALIAS,
5398       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5399     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5400       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5401       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5402       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5403       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5404     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5405       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5406       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5407       .resetvalue = 0 },
5408     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5409       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5410       .access = PL2_RW, .type = ARM_CP_ALIAS,
5411       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5412     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5413       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5414       .access = PL2_RW, .type = ARM_CP_CONST,
5415       .resetvalue = 0 },
5416     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5417     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5418       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5419       .access = PL2_RW, .type = ARM_CP_CONST,
5420       .resetvalue = 0 },
5421     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5422       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5423       .access = PL2_RW, .type = ARM_CP_CONST,
5424       .resetvalue = 0 },
5425     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5426       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5427       .access = PL2_RW, .type = ARM_CP_CONST,
5428       .resetvalue = 0 },
5429     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5430       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5431       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5432       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5433     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5434       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5435       .type = ARM_CP_ALIAS,
5436       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5437       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5438     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5439       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5440       .access = PL2_RW,
5441       /* no .writefn needed as this can't cause an ASID change */
5442       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5443     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5444       .cp = 15, .opc1 = 6, .crm = 2,
5445       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5446       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5447       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5448       .writefn = vttbr_write },
5449     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5450       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5451       .access = PL2_RW, .writefn = vttbr_write,
5452       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5453     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5454       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5455       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5456       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5457     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5458       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5459       .access = PL2_RW, .resetvalue = 0,
5460       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5461     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5462       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5463       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5464       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5465     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5466       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5467       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5468     { .name = "TLBIALLNSNH",
5469       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5470       .type = ARM_CP_NO_RAW, .access = PL2_W,
5471       .writefn = tlbiall_nsnh_write },
5472     { .name = "TLBIALLNSNHIS",
5473       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5474       .type = ARM_CP_NO_RAW, .access = PL2_W,
5475       .writefn = tlbiall_nsnh_is_write },
5476     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5477       .type = ARM_CP_NO_RAW, .access = PL2_W,
5478       .writefn = tlbiall_hyp_write },
5479     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5480       .type = ARM_CP_NO_RAW, .access = PL2_W,
5481       .writefn = tlbiall_hyp_is_write },
5482     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5483       .type = ARM_CP_NO_RAW, .access = PL2_W,
5484       .writefn = tlbimva_hyp_write },
5485     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5486       .type = ARM_CP_NO_RAW, .access = PL2_W,
5487       .writefn = tlbimva_hyp_is_write },
5488     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5489       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5490       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5491       .writefn = tlbi_aa64_alle2_write },
5492     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5493       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5494       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5495       .writefn = tlbi_aa64_vae2_write },
5496     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5497       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5498       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5499       .writefn = tlbi_aa64_vae2_write },
5500     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5501       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5502       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5503       .writefn = tlbi_aa64_alle2is_write },
5504     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5505       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5506       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5507       .writefn = tlbi_aa64_vae2is_write },
5508     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5509       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5510       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5511       .writefn = tlbi_aa64_vae2is_write },
5512 #ifndef CONFIG_USER_ONLY
5513     /* Unlike the other EL2-related AT operations, these must
5514      * UNDEF from EL3 if EL2 is not implemented, which is why we
5515      * define them here rather than with the rest of the AT ops.
5516      */
5517     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5518       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5519       .access = PL2_W, .accessfn = at_s1e2_access,
5520       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5521       .writefn = ats_write64 },
5522     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5523       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5524       .access = PL2_W, .accessfn = at_s1e2_access,
5525       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5526       .writefn = ats_write64 },
5527     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5528      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5529      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5530      * to behave as if SCR.NS was 1.
5531      */
5532     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5533       .access = PL2_W,
5534       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5535     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5536       .access = PL2_W,
5537       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5538     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5539       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5540       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5541        * reset values as IMPDEF. We choose to reset to 3 to comply with
5542        * both ARMv7 and ARMv8.
5543        */
5544       .access = PL2_RW, .resetvalue = 3,
5545       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5546     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5547       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5548       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5549       .writefn = gt_cntvoff_write,
5550       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5551     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5552       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5553       .writefn = gt_cntvoff_write,
5554       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5555     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5556       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5557       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5558       .type = ARM_CP_IO, .access = PL2_RW,
5559       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5560     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5561       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5562       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5563       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5564     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5565       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5566       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5567       .resetfn = gt_hyp_timer_reset,
5568       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5569     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5570       .type = ARM_CP_IO,
5571       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5572       .access = PL2_RW,
5573       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5574       .resetvalue = 0,
5575       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5576 #endif
5577     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5578       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5579       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5580       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5581     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5582       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5583       .access = PL2_RW,
5584       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5585     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5586       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5587       .access = PL2_RW,
5588       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5589 };
5590
5591 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5592     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5593       .type = ARM_CP_ALIAS | ARM_CP_IO,
5594       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5595       .access = PL2_RW,
5596       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5597       .writefn = hcr_writehigh },
5598 };
5599
5600 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5601                                   bool isread)
5602 {
5603     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5604         return CP_ACCESS_OK;
5605     }
5606     return CP_ACCESS_TRAP_UNCATEGORIZED;
5607 }
5608
5609 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5610     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5611       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5612       .access = PL2_RW, .accessfn = sel2_access,
5613       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5614     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5615       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5616       .access = PL2_RW, .accessfn = sel2_access,
5617       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5618 };
5619
5620 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5621                                    bool isread)
5622 {
5623     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5624      * At Secure EL1 it traps to EL3 or EL2.
5625      */
5626     if (arm_current_el(env) == 3) {
5627         return CP_ACCESS_OK;
5628     }
5629     if (arm_is_secure_below_el3(env)) {
5630         if (env->cp15.scr_el3 & SCR_EEL2) {
5631             return CP_ACCESS_TRAP_EL2;
5632         }
5633         return CP_ACCESS_TRAP_EL3;
5634     }
5635     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5636     if (isread) {
5637         return CP_ACCESS_OK;
5638     }
5639     return CP_ACCESS_TRAP_UNCATEGORIZED;
5640 }
5641
5642 static const ARMCPRegInfo el3_cp_reginfo[] = {
5643     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5644       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5645       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5646       .resetfn = scr_reset, .writefn = scr_write },
5647     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5648       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5649       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5650       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5651       .writefn = scr_write },
5652     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5653       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5654       .access = PL3_RW, .resetvalue = 0,
5655       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5656     { .name = "SDER",
5657       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5658       .access = PL3_RW, .resetvalue = 0,
5659       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5660     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5661       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5662       .writefn = vbar_write, .resetvalue = 0,
5663       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5664     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5665       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5666       .access = PL3_RW, .resetvalue = 0,
5667       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5668     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5669       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5670       .access = PL3_RW,
5671       /* no .writefn needed as this can't cause an ASID change */
5672       .resetvalue = 0,
5673       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5674     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5675       .type = ARM_CP_ALIAS,
5676       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5677       .access = PL3_RW,
5678       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5679     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5680       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5681       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5682     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5683       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5684       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5685     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5686       .type = ARM_CP_ALIAS,
5687       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5688       .access = PL3_RW,
5689       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5690     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5691       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5692       .access = PL3_RW, .writefn = vbar_write,
5693       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5694       .resetvalue = 0 },
5695     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5696       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5697       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5698       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5699     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5700       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5701       .access = PL3_RW, .resetvalue = 0,
5702       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5703     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5704       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5705       .access = PL3_RW, .type = ARM_CP_CONST,
5706       .resetvalue = 0 },
5707     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5708       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5709       .access = PL3_RW, .type = ARM_CP_CONST,
5710       .resetvalue = 0 },
5711     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5712       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5713       .access = PL3_RW, .type = ARM_CP_CONST,
5714       .resetvalue = 0 },
5715     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5716       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5717       .access = PL3_W, .type = ARM_CP_NO_RAW,
5718       .writefn = tlbi_aa64_alle3is_write },
5719     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5720       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5721       .access = PL3_W, .type = ARM_CP_NO_RAW,
5722       .writefn = tlbi_aa64_vae3is_write },
5723     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5724       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5725       .access = PL3_W, .type = ARM_CP_NO_RAW,
5726       .writefn = tlbi_aa64_vae3is_write },
5727     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5728       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5729       .access = PL3_W, .type = ARM_CP_NO_RAW,
5730       .writefn = tlbi_aa64_alle3_write },
5731     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5732       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5733       .access = PL3_W, .type = ARM_CP_NO_RAW,
5734       .writefn = tlbi_aa64_vae3_write },
5735     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5736       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5737       .access = PL3_W, .type = ARM_CP_NO_RAW,
5738       .writefn = tlbi_aa64_vae3_write },
5739 };
5740
5741 #ifndef CONFIG_USER_ONLY
5742 /* Test if system register redirection is to occur in the current state.  */
5743 static bool redirect_for_e2h(CPUARMState *env)
5744 {
5745     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5746 }
5747
5748 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5749 {
5750     CPReadFn *readfn;
5751
5752     if (redirect_for_e2h(env)) {
5753         /* Switch to the saved EL2 version of the register.  */
5754         ri = ri->opaque;
5755         readfn = ri->readfn;
5756     } else {
5757         readfn = ri->orig_readfn;
5758     }
5759     if (readfn == NULL) {
5760         readfn = raw_read;
5761     }
5762     return readfn(env, ri);
5763 }
5764
5765 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5766                           uint64_t value)
5767 {
5768     CPWriteFn *writefn;
5769
5770     if (redirect_for_e2h(env)) {
5771         /* Switch to the saved EL2 version of the register.  */
5772         ri = ri->opaque;
5773         writefn = ri->writefn;
5774     } else {
5775         writefn = ri->orig_writefn;
5776     }
5777     if (writefn == NULL) {
5778         writefn = raw_write;
5779     }
5780     writefn(env, ri, value);
5781 }
5782
5783 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5784 {
5785     struct E2HAlias {
5786         uint32_t src_key, dst_key, new_key;
5787         const char *src_name, *dst_name, *new_name;
5788         bool (*feature)(const ARMISARegisters *id);
5789     };
5790
5791 #define K(op0, op1, crn, crm, op2) \
5792     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5793
5794     static const struct E2HAlias aliases[] = {
5795         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5796           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5797         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5798           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5799         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5800           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5801         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5802           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5803         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5804           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5805         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5806           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5807         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5808           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5809         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5810           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5811         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5812           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5813         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5814           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5815         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5816           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5817         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5818           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5819         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5820           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5821         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5822           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5823         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5824           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5825         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5826           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5827
5828         /*
5829          * Note that redirection of ZCR is mentioned in the description
5830          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5831          * not in the summary table.
5832          */
5833         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5834           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5835         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5836           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5837
5838         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5839           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5840
5841         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5842           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5843           isar_feature_aa64_scxtnum },
5844
5845         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5846         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5847     };
5848 #undef K
5849
5850     size_t i;
5851
5852     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5853         const struct E2HAlias *a = &aliases[i];
5854         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5855         bool ok;
5856
5857         if (a->feature && !a->feature(&cpu->isar)) {
5858             continue;
5859         }
5860
5861         src_reg = g_hash_table_lookup(cpu->cp_regs,
5862                                       (gpointer)(uintptr_t)a->src_key);
5863         dst_reg = g_hash_table_lookup(cpu->cp_regs,
5864                                       (gpointer)(uintptr_t)a->dst_key);
5865         g_assert(src_reg != NULL);
5866         g_assert(dst_reg != NULL);
5867
5868         /* Cross-compare names to detect typos in the keys.  */
5869         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5870         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5871
5872         /* None of the core system registers use opaque; we will.  */
5873         g_assert(src_reg->opaque == NULL);
5874
5875         /* Create alias before redirection so we dup the right data. */
5876         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5877
5878         new_reg->name = a->new_name;
5879         new_reg->type |= ARM_CP_ALIAS;
5880         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
5881         new_reg->access &= PL2_RW | PL3_RW;
5882
5883         ok = g_hash_table_insert(cpu->cp_regs,
5884                                  (gpointer)(uintptr_t)a->new_key, new_reg);
5885         g_assert(ok);
5886
5887         src_reg->opaque = dst_reg;
5888         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5889         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5890         if (!src_reg->raw_readfn) {
5891             src_reg->raw_readfn = raw_read;
5892         }
5893         if (!src_reg->raw_writefn) {
5894             src_reg->raw_writefn = raw_write;
5895         }
5896         src_reg->readfn = el2_e2h_read;
5897         src_reg->writefn = el2_e2h_write;
5898     }
5899 }
5900 #endif
5901
5902 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5903                                      bool isread)
5904 {
5905     int cur_el = arm_current_el(env);
5906
5907     if (cur_el < 2) {
5908         uint64_t hcr = arm_hcr_el2_eff(env);
5909
5910         if (cur_el == 0) {
5911             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5912                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5913                     return CP_ACCESS_TRAP_EL2;
5914                 }
5915             } else {
5916                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5917                     return CP_ACCESS_TRAP;
5918                 }
5919                 if (hcr & HCR_TID2) {
5920                     return CP_ACCESS_TRAP_EL2;
5921                 }
5922             }
5923         } else if (hcr & HCR_TID2) {
5924             return CP_ACCESS_TRAP_EL2;
5925         }
5926     }
5927
5928     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5929         return CP_ACCESS_TRAP_EL2;
5930     }
5931
5932     return CP_ACCESS_OK;
5933 }
5934
5935 /*
5936  * Check for traps to RAS registers, which are controlled
5937  * by HCR_EL2.TERR and SCR_EL3.TERR.
5938  */
5939 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
5940                                   bool isread)
5941 {
5942     int el = arm_current_el(env);
5943
5944     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
5945         return CP_ACCESS_TRAP_EL2;
5946     }
5947     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
5948         return CP_ACCESS_TRAP_EL3;
5949     }
5950     return CP_ACCESS_OK;
5951 }
5952
5953 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
5954 {
5955     int el = arm_current_el(env);
5956
5957     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
5958         return env->cp15.vdisr_el2;
5959     }
5960     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
5961         return 0; /* RAZ/WI */
5962     }
5963     return env->cp15.disr_el1;
5964 }
5965
5966 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5967 {
5968     int el = arm_current_el(env);
5969
5970     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
5971         env->cp15.vdisr_el2 = val;
5972         return;
5973     }
5974     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
5975         return; /* RAZ/WI */
5976     }
5977     env->cp15.disr_el1 = val;
5978 }
5979
5980 /*
5981  * Minimal RAS implementation with no Error Records.
5982  * Which means that all of the Error Record registers:
5983  *   ERXADDR_EL1
5984  *   ERXCTLR_EL1
5985  *   ERXFR_EL1
5986  *   ERXMISC0_EL1
5987  *   ERXMISC1_EL1
5988  *   ERXMISC2_EL1
5989  *   ERXMISC3_EL1
5990  *   ERXPFGCDN_EL1  (RASv1p1)
5991  *   ERXPFGCTL_EL1  (RASv1p1)
5992  *   ERXPFGF_EL1    (RASv1p1)
5993  *   ERXSTATUS_EL1
5994  * and
5995  *   ERRSELR_EL1
5996  * may generate UNDEFINED, which is the effect we get by not
5997  * listing them at all.
5998  */
5999 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6000     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6001       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6002       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6003       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6004     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6005       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6006       .access = PL1_R, .accessfn = access_terr,
6007       .type = ARM_CP_CONST, .resetvalue = 0 },
6008     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6009       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6010       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6011     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6012       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6013       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6014 };
6015
6016 /*
6017  * Return the exception level to which exceptions should be taken
6018  * via SVEAccessTrap.  This excludes the check for whether the exception
6019  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6020  * be found by testing 0 < fp_exception_el < sve_exception_el.
6021  *
6022  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6023  * pseudocode does *not* separate out the FP trap checks, but has them
6024  * all in one function.
6025  */
6026 int sve_exception_el(CPUARMState *env, int el)
6027 {
6028 #ifndef CONFIG_USER_ONLY
6029     if (el <= 1 && !el_is_in_host(env, el)) {
6030         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6031         case 1:
6032             if (el != 0) {
6033                 break;
6034             }
6035             /* fall through */
6036         case 0:
6037         case 2:
6038             return 1;
6039         }
6040     }
6041
6042     if (el <= 2 && arm_is_el2_enabled(env)) {
6043         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6044         if (env->cp15.hcr_el2 & HCR_E2H) {
6045             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6046             case 1:
6047                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6048                     break;
6049                 }
6050                 /* fall through */
6051             case 0:
6052             case 2:
6053                 return 2;
6054             }
6055         } else {
6056             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6057                 return 2;
6058             }
6059         }
6060     }
6061
6062     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6063     if (arm_feature(env, ARM_FEATURE_EL3)
6064         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6065         return 3;
6066     }
6067 #endif
6068     return 0;
6069 }
6070
6071 /*
6072  * Return the exception level to which exceptions should be taken for SME.
6073  * C.f. the ARM pseudocode function CheckSMEAccess.
6074  */
6075 int sme_exception_el(CPUARMState *env, int el)
6076 {
6077 #ifndef CONFIG_USER_ONLY
6078     if (el <= 1 && !el_is_in_host(env, el)) {
6079         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6080         case 1:
6081             if (el != 0) {
6082                 break;
6083             }
6084             /* fall through */
6085         case 0:
6086         case 2:
6087             return 1;
6088         }
6089     }
6090
6091     if (el <= 2 && arm_is_el2_enabled(env)) {
6092         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6093         if (env->cp15.hcr_el2 & HCR_E2H) {
6094             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6095             case 1:
6096                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6097                     break;
6098                 }
6099                 /* fall through */
6100             case 0:
6101             case 2:
6102                 return 2;
6103             }
6104         } else {
6105             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6106                 return 2;
6107             }
6108         }
6109     }
6110
6111     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6112     if (arm_feature(env, ARM_FEATURE_EL3)
6113         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6114         return 3;
6115     }
6116 #endif
6117     return 0;
6118 }
6119
6120 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6121 static bool sme_fa64(CPUARMState *env, int el)
6122 {
6123     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6124         return false;
6125     }
6126
6127     if (el <= 1 && !el_is_in_host(env, el)) {
6128         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6129             return false;
6130         }
6131     }
6132     if (el <= 2 && arm_is_el2_enabled(env)) {
6133         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6134             return false;
6135         }
6136     }
6137     if (arm_feature(env, ARM_FEATURE_EL3)) {
6138         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6139             return false;
6140         }
6141     }
6142
6143     return true;
6144 }
6145
6146 /*
6147  * Given that SVE is enabled, return the vector length for EL.
6148  */
6149 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6150 {
6151     ARMCPU *cpu = env_archcpu(env);
6152     uint64_t *cr = env->vfp.zcr_el;
6153     uint32_t map = cpu->sve_vq.map;
6154     uint32_t len = ARM_MAX_VQ - 1;
6155
6156     if (sm) {
6157         cr = env->vfp.smcr_el;
6158         map = cpu->sme_vq.map;
6159     }
6160
6161     if (el <= 1 && !el_is_in_host(env, el)) {
6162         len = MIN(len, 0xf & (uint32_t)cr[1]);
6163     }
6164     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6165         len = MIN(len, 0xf & (uint32_t)cr[2]);
6166     }
6167     if (arm_feature(env, ARM_FEATURE_EL3)) {
6168         len = MIN(len, 0xf & (uint32_t)cr[3]);
6169     }
6170
6171     map &= MAKE_64BIT_MASK(0, len + 1);
6172     if (map != 0) {
6173         return 31 - clz32(map);
6174     }
6175
6176     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6177     assert(sm);
6178     return ctz32(cpu->sme_vq.map);
6179 }
6180
6181 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6182 {
6183     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6184 }
6185
6186 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6187                       uint64_t value)
6188 {
6189     int cur_el = arm_current_el(env);
6190     int old_len = sve_vqm1_for_el(env, cur_el);
6191     int new_len;
6192
6193     /* Bits other than [3:0] are RAZ/WI.  */
6194     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6195     raw_write(env, ri, value & 0xf);
6196
6197     /*
6198      * Because we arrived here, we know both FP and SVE are enabled;
6199      * otherwise we would have trapped access to the ZCR_ELn register.
6200      */
6201     new_len = sve_vqm1_for_el(env, cur_el);
6202     if (new_len < old_len) {
6203         aarch64_sve_narrow_vq(env, new_len + 1);
6204     }
6205 }
6206
6207 static const ARMCPRegInfo zcr_reginfo[] = {
6208     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6209       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6210       .access = PL1_RW, .type = ARM_CP_SVE,
6211       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6212       .writefn = zcr_write, .raw_writefn = raw_write },
6213     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6214       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6215       .access = PL2_RW, .type = ARM_CP_SVE,
6216       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6217       .writefn = zcr_write, .raw_writefn = raw_write },
6218     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6219       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6220       .access = PL3_RW, .type = ARM_CP_SVE,
6221       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6222       .writefn = zcr_write, .raw_writefn = raw_write },
6223 };
6224
6225 #ifdef TARGET_AARCH64
6226 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6227                                     bool isread)
6228 {
6229     int el = arm_current_el(env);
6230
6231     if (el == 0) {
6232         uint64_t sctlr = arm_sctlr(env, el);
6233         if (!(sctlr & SCTLR_EnTP2)) {
6234             return CP_ACCESS_TRAP;
6235         }
6236     }
6237     /* TODO: FEAT_FGT */
6238     if (el < 3
6239         && arm_feature(env, ARM_FEATURE_EL3)
6240         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6241         return CP_ACCESS_TRAP_EL3;
6242     }
6243     return CP_ACCESS_OK;
6244 }
6245
6246 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6247                                  bool isread)
6248 {
6249     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6250     if (arm_current_el(env) < 3
6251         && arm_feature(env, ARM_FEATURE_EL3)
6252         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6253         return CP_ACCESS_TRAP_EL3;
6254     }
6255     return CP_ACCESS_OK;
6256 }
6257
6258 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6259                        uint64_t value)
6260 {
6261     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6262     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6263     arm_rebuild_hflags(env);
6264 }
6265
6266 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6267                        uint64_t value)
6268 {
6269     int cur_el = arm_current_el(env);
6270     int old_len = sve_vqm1_for_el(env, cur_el);
6271     int new_len;
6272
6273     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6274     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6275     raw_write(env, ri, value);
6276
6277     /*
6278      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6279      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6280      * current values for simplicity.  But for QEMU internals, we must still
6281      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6282      * above aarch64_sve_narrow_vq.
6283      */
6284     new_len = sve_vqm1_for_el(env, cur_el);
6285     if (new_len < old_len) {
6286         aarch64_sve_narrow_vq(env, new_len + 1);
6287     }
6288 }
6289
6290 static const ARMCPRegInfo sme_reginfo[] = {
6291     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6292       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6293       .access = PL0_RW, .accessfn = access_tpidr2,
6294       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6295     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6296       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6297       .access = PL0_RW, .type = ARM_CP_SME,
6298       .fieldoffset = offsetof(CPUARMState, svcr),
6299       .writefn = svcr_write, .raw_writefn = raw_write },
6300     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6301       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6302       .access = PL1_RW, .type = ARM_CP_SME,
6303       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6304       .writefn = smcr_write, .raw_writefn = raw_write },
6305     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6306       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6307       .access = PL2_RW, .type = ARM_CP_SME,
6308       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6309       .writefn = smcr_write, .raw_writefn = raw_write },
6310     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6311       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6312       .access = PL3_RW, .type = ARM_CP_SME,
6313       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6314       .writefn = smcr_write, .raw_writefn = raw_write },
6315     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6316       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6317       .access = PL1_R, .accessfn = access_aa64_tid1,
6318       /*
6319        * IMPLEMENTOR = 0 (software)
6320        * REVISION    = 0 (implementation defined)
6321        * SMPS        = 0 (no streaming execution priority in QEMU)
6322        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6323        */
6324       .type = ARM_CP_CONST, .resetvalue = 0, },
6325     /*
6326      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6327      */
6328     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6329       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6330       .access = PL1_RW, .accessfn = access_esm,
6331       .type = ARM_CP_CONST, .resetvalue = 0 },
6332     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6333       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6334       .access = PL2_RW, .accessfn = access_esm,
6335       .type = ARM_CP_CONST, .resetvalue = 0 },
6336 };
6337 #endif /* TARGET_AARCH64 */
6338
6339 static void define_pmu_regs(ARMCPU *cpu)
6340 {
6341     /*
6342      * v7 performance monitor control register: same implementor
6343      * field as main ID register, and we implement four counters in
6344      * addition to the cycle count register.
6345      */
6346     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6347     ARMCPRegInfo pmcr = {
6348         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6349         .access = PL0_RW,
6350         .type = ARM_CP_IO | ARM_CP_ALIAS,
6351         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6352         .accessfn = pmreg_access, .writefn = pmcr_write,
6353         .raw_writefn = raw_write,
6354     };
6355     ARMCPRegInfo pmcr64 = {
6356         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6357         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6358         .access = PL0_RW, .accessfn = pmreg_access,
6359         .type = ARM_CP_IO,
6360         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6361         .resetvalue = cpu->isar.reset_pmcr_el0,
6362         .writefn = pmcr_write, .raw_writefn = raw_write,
6363     };
6364
6365     define_one_arm_cp_reg(cpu, &pmcr);
6366     define_one_arm_cp_reg(cpu, &pmcr64);
6367     for (i = 0; i < pmcrn; i++) {
6368         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6369         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6370         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6371         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6372         ARMCPRegInfo pmev_regs[] = {
6373             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6374               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6375               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6376               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6377               .accessfn = pmreg_access_xevcntr },
6378             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6379               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6380               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6381               .type = ARM_CP_IO,
6382               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6383               .raw_readfn = pmevcntr_rawread,
6384               .raw_writefn = pmevcntr_rawwrite },
6385             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6386               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6387               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6388               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6389               .accessfn = pmreg_access },
6390             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6391               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6392               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6393               .type = ARM_CP_IO,
6394               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6395               .raw_writefn = pmevtyper_rawwrite },
6396         };
6397         define_arm_cp_regs(cpu, pmev_regs);
6398         g_free(pmevcntr_name);
6399         g_free(pmevcntr_el0_name);
6400         g_free(pmevtyper_name);
6401         g_free(pmevtyper_el0_name);
6402     }
6403     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6404         ARMCPRegInfo v81_pmu_regs[] = {
6405             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6406               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6407               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6408               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6409             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6410               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6411               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6412               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6413         };
6414         define_arm_cp_regs(cpu, v81_pmu_regs);
6415     }
6416     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6417         static const ARMCPRegInfo v84_pmmir = {
6418             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6419             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6420             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6421             .resetvalue = 0
6422         };
6423         define_one_arm_cp_reg(cpu, &v84_pmmir);
6424     }
6425 }
6426
6427 /* We don't know until after realize whether there's a GICv3
6428  * attached, and that is what registers the gicv3 sysregs.
6429  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6430  * at runtime.
6431  */
6432 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6433 {
6434     ARMCPU *cpu = env_archcpu(env);
6435     uint64_t pfr1 = cpu->isar.id_pfr1;
6436
6437     if (env->gicv3state) {
6438         pfr1 |= 1 << 28;
6439     }
6440     return pfr1;
6441 }
6442
6443 #ifndef CONFIG_USER_ONLY
6444 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6445 {
6446     ARMCPU *cpu = env_archcpu(env);
6447     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6448
6449     if (env->gicv3state) {
6450         pfr0 |= 1 << 24;
6451     }
6452     return pfr0;
6453 }
6454 #endif
6455
6456 /* Shared logic between LORID and the rest of the LOR* registers.
6457  * Secure state exclusion has already been dealt with.
6458  */
6459 static CPAccessResult access_lor_ns(CPUARMState *env,
6460                                     const ARMCPRegInfo *ri, bool isread)
6461 {
6462     int el = arm_current_el(env);
6463
6464     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6465         return CP_ACCESS_TRAP_EL2;
6466     }
6467     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6468         return CP_ACCESS_TRAP_EL3;
6469     }
6470     return CP_ACCESS_OK;
6471 }
6472
6473 static CPAccessResult access_lor_other(CPUARMState *env,
6474                                        const ARMCPRegInfo *ri, bool isread)
6475 {
6476     if (arm_is_secure_below_el3(env)) {
6477         /* Access denied in secure mode.  */
6478         return CP_ACCESS_TRAP;
6479     }
6480     return access_lor_ns(env, ri, isread);
6481 }
6482
6483 /*
6484  * A trivial implementation of ARMv8.1-LOR leaves all of these
6485  * registers fixed at 0, which indicates that there are zero
6486  * supported Limited Ordering regions.
6487  */
6488 static const ARMCPRegInfo lor_reginfo[] = {
6489     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6490       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6491       .access = PL1_RW, .accessfn = access_lor_other,
6492       .type = ARM_CP_CONST, .resetvalue = 0 },
6493     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6494       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6495       .access = PL1_RW, .accessfn = access_lor_other,
6496       .type = ARM_CP_CONST, .resetvalue = 0 },
6497     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6498       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6499       .access = PL1_RW, .accessfn = access_lor_other,
6500       .type = ARM_CP_CONST, .resetvalue = 0 },
6501     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6502       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6503       .access = PL1_RW, .accessfn = access_lor_other,
6504       .type = ARM_CP_CONST, .resetvalue = 0 },
6505     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6506       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6507       .access = PL1_R, .accessfn = access_lor_ns,
6508       .type = ARM_CP_CONST, .resetvalue = 0 },
6509 };
6510
6511 #ifdef TARGET_AARCH64
6512 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6513                                    bool isread)
6514 {
6515     int el = arm_current_el(env);
6516
6517     if (el < 2 &&
6518         arm_is_el2_enabled(env) &&
6519         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6520         return CP_ACCESS_TRAP_EL2;
6521     }
6522     if (el < 3 &&
6523         arm_feature(env, ARM_FEATURE_EL3) &&
6524         !(env->cp15.scr_el3 & SCR_APK)) {
6525         return CP_ACCESS_TRAP_EL3;
6526     }
6527     return CP_ACCESS_OK;
6528 }
6529
6530 static const ARMCPRegInfo pauth_reginfo[] = {
6531     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6532       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6533       .access = PL1_RW, .accessfn = access_pauth,
6534       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6535     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6536       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6537       .access = PL1_RW, .accessfn = access_pauth,
6538       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6539     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6540       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6541       .access = PL1_RW, .accessfn = access_pauth,
6542       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6543     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6544       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6545       .access = PL1_RW, .accessfn = access_pauth,
6546       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6547     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6548       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6549       .access = PL1_RW, .accessfn = access_pauth,
6550       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6551     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6552       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6553       .access = PL1_RW, .accessfn = access_pauth,
6554       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6555     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6556       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6557       .access = PL1_RW, .accessfn = access_pauth,
6558       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6559     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6560       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6561       .access = PL1_RW, .accessfn = access_pauth,
6562       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6563     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6564       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6565       .access = PL1_RW, .accessfn = access_pauth,
6566       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6567     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6568       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6569       .access = PL1_RW, .accessfn = access_pauth,
6570       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6571 };
6572
6573 static const ARMCPRegInfo tlbirange_reginfo[] = {
6574     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6575       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6576       .access = PL1_W, .type = ARM_CP_NO_RAW,
6577       .writefn = tlbi_aa64_rvae1is_write },
6578     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6579       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6580       .access = PL1_W, .type = ARM_CP_NO_RAW,
6581       .writefn = tlbi_aa64_rvae1is_write },
6582    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6583       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6584       .access = PL1_W, .type = ARM_CP_NO_RAW,
6585       .writefn = tlbi_aa64_rvae1is_write },
6586     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6587       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6588       .access = PL1_W, .type = ARM_CP_NO_RAW,
6589       .writefn = tlbi_aa64_rvae1is_write },
6590     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6591       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6592       .access = PL1_W, .type = ARM_CP_NO_RAW,
6593       .writefn = tlbi_aa64_rvae1is_write },
6594     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6595       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6596       .access = PL1_W, .type = ARM_CP_NO_RAW,
6597       .writefn = tlbi_aa64_rvae1is_write },
6598    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6599       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6600       .access = PL1_W, .type = ARM_CP_NO_RAW,
6601       .writefn = tlbi_aa64_rvae1is_write },
6602     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6603       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6604       .access = PL1_W, .type = ARM_CP_NO_RAW,
6605       .writefn = tlbi_aa64_rvae1is_write },
6606     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6607       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6608       .access = PL1_W, .type = ARM_CP_NO_RAW,
6609       .writefn = tlbi_aa64_rvae1_write },
6610     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6611       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6612       .access = PL1_W, .type = ARM_CP_NO_RAW,
6613       .writefn = tlbi_aa64_rvae1_write },
6614    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6615       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6616       .access = PL1_W, .type = ARM_CP_NO_RAW,
6617       .writefn = tlbi_aa64_rvae1_write },
6618     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6619       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6620       .access = PL1_W, .type = ARM_CP_NO_RAW,
6621       .writefn = tlbi_aa64_rvae1_write },
6622     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6623       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6624       .access = PL2_W, .type = ARM_CP_NOP },
6625     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6626       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6627       .access = PL2_W, .type = ARM_CP_NOP },
6628     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6629       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6630       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6631       .writefn = tlbi_aa64_rvae2is_write },
6632    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6633       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6634       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6635       .writefn = tlbi_aa64_rvae2is_write },
6636     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6637       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6638       .access = PL2_W, .type = ARM_CP_NOP },
6639    { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6640       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6641       .access = PL2_W, .type = ARM_CP_NOP },
6642    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6643       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6644       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6645       .writefn = tlbi_aa64_rvae2is_write },
6646    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6647       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6648       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6649       .writefn = tlbi_aa64_rvae2is_write },
6650     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6651       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6652       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6653       .writefn = tlbi_aa64_rvae2_write },
6654    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6655       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6656       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6657       .writefn = tlbi_aa64_rvae2_write },
6658    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6659       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6660       .access = PL3_W, .type = ARM_CP_NO_RAW,
6661       .writefn = tlbi_aa64_rvae3is_write },
6662    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6663       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6664       .access = PL3_W, .type = ARM_CP_NO_RAW,
6665       .writefn = tlbi_aa64_rvae3is_write },
6666    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6667       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6668       .access = PL3_W, .type = ARM_CP_NO_RAW,
6669       .writefn = tlbi_aa64_rvae3is_write },
6670    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6671       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6672       .access = PL3_W, .type = ARM_CP_NO_RAW,
6673       .writefn = tlbi_aa64_rvae3is_write },
6674    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6675       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6676       .access = PL3_W, .type = ARM_CP_NO_RAW,
6677       .writefn = tlbi_aa64_rvae3_write },
6678    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6679       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6680       .access = PL3_W, .type = ARM_CP_NO_RAW,
6681       .writefn = tlbi_aa64_rvae3_write },
6682 };
6683
6684 static const ARMCPRegInfo tlbios_reginfo[] = {
6685     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6686       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6687       .access = PL1_W, .type = ARM_CP_NO_RAW,
6688       .writefn = tlbi_aa64_vmalle1is_write },
6689     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6690       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6691       .access = PL1_W, .type = ARM_CP_NO_RAW,
6692       .writefn = tlbi_aa64_vae1is_write },
6693     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6694       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6695       .access = PL1_W, .type = ARM_CP_NO_RAW,
6696       .writefn = tlbi_aa64_vmalle1is_write },
6697     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6698       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6699       .access = PL1_W, .type = ARM_CP_NO_RAW,
6700       .writefn = tlbi_aa64_vae1is_write },
6701     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6702       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6703       .access = PL1_W, .type = ARM_CP_NO_RAW,
6704       .writefn = tlbi_aa64_vae1is_write },
6705     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6706       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6707       .access = PL1_W, .type = ARM_CP_NO_RAW,
6708       .writefn = tlbi_aa64_vae1is_write },
6709     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6710       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6711       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6712       .writefn = tlbi_aa64_alle2is_write },
6713     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6714       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6715       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6716       .writefn = tlbi_aa64_vae2is_write },
6717    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6718       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6719       .access = PL2_W, .type = ARM_CP_NO_RAW,
6720       .writefn = tlbi_aa64_alle1is_write },
6721     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6722       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6723       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6724       .writefn = tlbi_aa64_vae2is_write },
6725     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6726       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6727       .access = PL2_W, .type = ARM_CP_NO_RAW,
6728       .writefn = tlbi_aa64_alle1is_write },
6729     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6730       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6731       .access = PL2_W, .type = ARM_CP_NOP },
6732     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6733       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6734       .access = PL2_W, .type = ARM_CP_NOP },
6735     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6736       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6737       .access = PL2_W, .type = ARM_CP_NOP },
6738     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6739       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6740       .access = PL2_W, .type = ARM_CP_NOP },
6741     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6742       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6743       .access = PL3_W, .type = ARM_CP_NO_RAW,
6744       .writefn = tlbi_aa64_alle3is_write },
6745     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6746       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6747       .access = PL3_W, .type = ARM_CP_NO_RAW,
6748       .writefn = tlbi_aa64_vae3is_write },
6749     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6750       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6751       .access = PL3_W, .type = ARM_CP_NO_RAW,
6752       .writefn = tlbi_aa64_vae3is_write },
6753 };
6754
6755 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6756 {
6757     Error *err = NULL;
6758     uint64_t ret;
6759
6760     /* Success sets NZCV = 0000.  */
6761     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6762
6763     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6764         /*
6765          * ??? Failed, for unknown reasons in the crypto subsystem.
6766          * The best we can do is log the reason and return the
6767          * timed-out indication to the guest.  There is no reason
6768          * we know to expect this failure to be transitory, so the
6769          * guest may well hang retrying the operation.
6770          */
6771         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6772                       ri->name, error_get_pretty(err));
6773         error_free(err);
6774
6775         env->ZF = 0; /* NZCF = 0100 */
6776         return 0;
6777     }
6778     return ret;
6779 }
6780
6781 /* We do not support re-seeding, so the two registers operate the same.  */
6782 static const ARMCPRegInfo rndr_reginfo[] = {
6783     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6784       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6785       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6786       .access = PL0_R, .readfn = rndr_readfn },
6787     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6788       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6789       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6790       .access = PL0_R, .readfn = rndr_readfn },
6791 };
6792
6793 #ifndef CONFIG_USER_ONLY
6794 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6795                           uint64_t value)
6796 {
6797     ARMCPU *cpu = env_archcpu(env);
6798     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6799     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6800     uint64_t vaddr_in = (uint64_t) value;
6801     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6802     void *haddr;
6803     int mem_idx = cpu_mmu_index(env, false);
6804
6805     /* This won't be crossing page boundaries */
6806     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6807     if (haddr) {
6808
6809         ram_addr_t offset;
6810         MemoryRegion *mr;
6811
6812         /* RCU lock is already being held */
6813         mr = memory_region_from_host(haddr, &offset);
6814
6815         if (mr) {
6816             memory_region_writeback(mr, offset, dline_size);
6817         }
6818     }
6819 }
6820
6821 static const ARMCPRegInfo dcpop_reg[] = {
6822     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6823       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6824       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6825       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6826 };
6827
6828 static const ARMCPRegInfo dcpodp_reg[] = {
6829     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6830       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6831       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6832       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6833 };
6834 #endif /*CONFIG_USER_ONLY*/
6835
6836 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6837                                        bool isread)
6838 {
6839     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6840         return CP_ACCESS_TRAP_EL2;
6841     }
6842
6843     return CP_ACCESS_OK;
6844 }
6845
6846 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6847                                  bool isread)
6848 {
6849     int el = arm_current_el(env);
6850
6851     if (el < 2 && arm_is_el2_enabled(env)) {
6852         uint64_t hcr = arm_hcr_el2_eff(env);
6853         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6854             return CP_ACCESS_TRAP_EL2;
6855         }
6856     }
6857     if (el < 3 &&
6858         arm_feature(env, ARM_FEATURE_EL3) &&
6859         !(env->cp15.scr_el3 & SCR_ATA)) {
6860         return CP_ACCESS_TRAP_EL3;
6861     }
6862     return CP_ACCESS_OK;
6863 }
6864
6865 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6866 {
6867     return env->pstate & PSTATE_TCO;
6868 }
6869
6870 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6871 {
6872     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6873 }
6874
6875 static const ARMCPRegInfo mte_reginfo[] = {
6876     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6877       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6878       .access = PL1_RW, .accessfn = access_mte,
6879       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6880     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6881       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6882       .access = PL1_RW, .accessfn = access_mte,
6883       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6884     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6885       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6886       .access = PL2_RW, .accessfn = access_mte,
6887       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6888     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6889       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6890       .access = PL3_RW,
6891       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6892     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6893       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6894       .access = PL1_RW, .accessfn = access_mte,
6895       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6896     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6897       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6898       .access = PL1_RW, .accessfn = access_mte,
6899       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6900     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6901       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6902       .access = PL1_R, .accessfn = access_aa64_tid5,
6903       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6904     { .name = "TCO", .state = ARM_CP_STATE_AA64,
6905       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6906       .type = ARM_CP_NO_RAW,
6907       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
6908     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6909       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6910       .type = ARM_CP_NOP, .access = PL1_W,
6911       .accessfn = aa64_cacheop_poc_access },
6912     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6913       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6914       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6915     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6916       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6917       .type = ARM_CP_NOP, .access = PL1_W,
6918       .accessfn = aa64_cacheop_poc_access },
6919     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6920       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6921       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6922     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6923       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6924       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6925     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6926       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
6927       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6928     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
6929       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
6930       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6931     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
6932       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
6933       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6934 };
6935
6936 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
6937     { .name = "TCO", .state = ARM_CP_STATE_AA64,
6938       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6939       .type = ARM_CP_CONST, .access = PL0_RW, },
6940 };
6941
6942 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
6943     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
6944       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
6945       .type = ARM_CP_NOP, .access = PL0_W,
6946       .accessfn = aa64_cacheop_poc_access },
6947     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
6948       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
6949       .type = ARM_CP_NOP, .access = PL0_W,
6950       .accessfn = aa64_cacheop_poc_access },
6951     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
6952       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
6953       .type = ARM_CP_NOP, .access = PL0_W,
6954       .accessfn = aa64_cacheop_poc_access },
6955     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
6956       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
6957       .type = ARM_CP_NOP, .access = PL0_W,
6958       .accessfn = aa64_cacheop_poc_access },
6959     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
6960       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
6961       .type = ARM_CP_NOP, .access = PL0_W,
6962       .accessfn = aa64_cacheop_poc_access },
6963     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
6964       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
6965       .type = ARM_CP_NOP, .access = PL0_W,
6966       .accessfn = aa64_cacheop_poc_access },
6967     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
6968       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
6969       .type = ARM_CP_NOP, .access = PL0_W,
6970       .accessfn = aa64_cacheop_poc_access },
6971     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
6972       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
6973       .type = ARM_CP_NOP, .access = PL0_W,
6974       .accessfn = aa64_cacheop_poc_access },
6975     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
6976       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
6977       .access = PL0_W, .type = ARM_CP_DC_GVA,
6978 #ifndef CONFIG_USER_ONLY
6979       /* Avoid overhead of an access check that always passes in user-mode */
6980       .accessfn = aa64_zva_access,
6981 #endif
6982     },
6983     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
6984       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
6985       .access = PL0_W, .type = ARM_CP_DC_GZVA,
6986 #ifndef CONFIG_USER_ONLY
6987       /* Avoid overhead of an access check that always passes in user-mode */
6988       .accessfn = aa64_zva_access,
6989 #endif
6990     },
6991 };
6992
6993 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
6994                                      bool isread)
6995 {
6996     uint64_t hcr = arm_hcr_el2_eff(env);
6997     int el = arm_current_el(env);
6998
6999     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7000         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7001             if (hcr & HCR_TGE) {
7002                 return CP_ACCESS_TRAP_EL2;
7003             }
7004             return CP_ACCESS_TRAP;
7005         }
7006     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7007         return CP_ACCESS_TRAP_EL2;
7008     }
7009     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7010         return CP_ACCESS_TRAP_EL2;
7011     }
7012     if (el < 3
7013         && arm_feature(env, ARM_FEATURE_EL3)
7014         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7015         return CP_ACCESS_TRAP_EL3;
7016     }
7017     return CP_ACCESS_OK;
7018 }
7019
7020 static const ARMCPRegInfo scxtnum_reginfo[] = {
7021     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7022       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7023       .access = PL0_RW, .accessfn = access_scxtnum,
7024       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7025     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7026       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7027       .access = PL1_RW, .accessfn = access_scxtnum,
7028       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7029     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7030       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7031       .access = PL2_RW, .accessfn = access_scxtnum,
7032       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7033     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7034       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7035       .access = PL3_RW,
7036       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7037 };
7038 #endif /* TARGET_AARCH64 */
7039
7040 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7041                                      bool isread)
7042 {
7043     int el = arm_current_el(env);
7044
7045     if (el == 0) {
7046         uint64_t sctlr = arm_sctlr(env, el);
7047         if (!(sctlr & SCTLR_EnRCTX)) {
7048             return CP_ACCESS_TRAP;
7049         }
7050     } else if (el == 1) {
7051         uint64_t hcr = arm_hcr_el2_eff(env);
7052         if (hcr & HCR_NV) {
7053             return CP_ACCESS_TRAP_EL2;
7054         }
7055     }
7056     return CP_ACCESS_OK;
7057 }
7058
7059 static const ARMCPRegInfo predinv_reginfo[] = {
7060     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7061       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7062       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7063     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7064       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7065       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7066     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7067       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7068       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7069     /*
7070      * Note the AArch32 opcodes have a different OPC1.
7071      */
7072     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7073       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7074       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7075     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7076       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7077       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7078     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7079       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7080       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7081 };
7082
7083 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7084 {
7085     /* Read the high 32 bits of the current CCSIDR */
7086     return extract64(ccsidr_read(env, ri), 32, 32);
7087 }
7088
7089 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7090     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7091       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7092       .access = PL1_R,
7093       .accessfn = access_aa64_tid2,
7094       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7095 };
7096
7097 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7098                                        bool isread)
7099 {
7100     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7101         return CP_ACCESS_TRAP_EL2;
7102     }
7103
7104     return CP_ACCESS_OK;
7105 }
7106
7107 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7108                                        bool isread)
7109 {
7110     if (arm_feature(env, ARM_FEATURE_V8)) {
7111         return access_aa64_tid3(env, ri, isread);
7112     }
7113
7114     return CP_ACCESS_OK;
7115 }
7116
7117 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7118                                      bool isread)
7119 {
7120     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7121         return CP_ACCESS_TRAP_EL2;
7122     }
7123
7124     return CP_ACCESS_OK;
7125 }
7126
7127 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7128                                         const ARMCPRegInfo *ri, bool isread)
7129 {
7130     /*
7131      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7132      * in v7A, not in v8A.
7133      */
7134     if (!arm_feature(env, ARM_FEATURE_V8) &&
7135         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7136         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7137         return CP_ACCESS_TRAP_EL2;
7138     }
7139     return CP_ACCESS_OK;
7140 }
7141
7142 static const ARMCPRegInfo jazelle_regs[] = {
7143     { .name = "JIDR",
7144       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7145       .access = PL1_R, .accessfn = access_jazelle,
7146       .type = ARM_CP_CONST, .resetvalue = 0 },
7147     { .name = "JOSCR",
7148       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7149       .accessfn = access_joscr_jmcr,
7150       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7151     { .name = "JMCR",
7152       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7153       .accessfn = access_joscr_jmcr,
7154       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7155 };
7156
7157 static const ARMCPRegInfo contextidr_el2 = {
7158     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7159     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7160     .access = PL2_RW,
7161     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7162 };
7163
7164 static const ARMCPRegInfo vhe_reginfo[] = {
7165     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7166       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7167       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7168       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7169 #ifndef CONFIG_USER_ONLY
7170     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7171       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7172       .fieldoffset =
7173         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7174       .type = ARM_CP_IO, .access = PL2_RW,
7175       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7176     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7177       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7178       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7179       .resetfn = gt_hv_timer_reset,
7180       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7181     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7182       .type = ARM_CP_IO,
7183       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7184       .access = PL2_RW,
7185       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7186       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7187     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7188       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7189       .type = ARM_CP_IO | ARM_CP_ALIAS,
7190       .access = PL2_RW, .accessfn = e2h_access,
7191       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7192       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7193     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7194       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7195       .type = ARM_CP_IO | ARM_CP_ALIAS,
7196       .access = PL2_RW, .accessfn = e2h_access,
7197       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7198       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7199     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7200       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7201       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7202       .access = PL2_RW, .accessfn = e2h_access,
7203       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7204     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7205       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7206       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7207       .access = PL2_RW, .accessfn = e2h_access,
7208       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7209     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7210       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7211       .type = ARM_CP_IO | ARM_CP_ALIAS,
7212       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7213       .access = PL2_RW, .accessfn = e2h_access,
7214       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7215     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7216       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7217       .type = ARM_CP_IO | ARM_CP_ALIAS,
7218       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7219       .access = PL2_RW, .accessfn = e2h_access,
7220       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7221 #endif
7222 };
7223
7224 #ifndef CONFIG_USER_ONLY
7225 static const ARMCPRegInfo ats1e1_reginfo[] = {
7226     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7227       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7228       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7229       .writefn = ats_write64 },
7230     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7231       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7232       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7233       .writefn = ats_write64 },
7234 };
7235
7236 static const ARMCPRegInfo ats1cp_reginfo[] = {
7237     { .name = "ATS1CPRP",
7238       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7239       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7240       .writefn = ats_write },
7241     { .name = "ATS1CPWP",
7242       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7243       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7244       .writefn = ats_write },
7245 };
7246 #endif
7247
7248 /*
7249  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7250  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7251  * is non-zero, which is never for ARMv7, optionally in ARMv8
7252  * and mandatorily for ARMv8.2 and up.
7253  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7254  * implementation is RAZ/WI we can ignore this detail, as we
7255  * do for ACTLR.
7256  */
7257 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7258     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7259       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7260       .access = PL1_RW, .accessfn = access_tacr,
7261       .type = ARM_CP_CONST, .resetvalue = 0 },
7262     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7263       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7264       .access = PL2_RW, .type = ARM_CP_CONST,
7265       .resetvalue = 0 },
7266 };
7267
7268 void register_cp_regs_for_features(ARMCPU *cpu)
7269 {
7270     /* Register all the coprocessor registers based on feature bits */
7271     CPUARMState *env = &cpu->env;
7272     if (arm_feature(env, ARM_FEATURE_M)) {
7273         /* M profile has no coprocessor registers */
7274         return;
7275     }
7276
7277     define_arm_cp_regs(cpu, cp_reginfo);
7278     if (!arm_feature(env, ARM_FEATURE_V8)) {
7279         /* Must go early as it is full of wildcards that may be
7280          * overridden by later definitions.
7281          */
7282         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7283     }
7284
7285     if (arm_feature(env, ARM_FEATURE_V6)) {
7286         /* The ID registers all have impdef reset values */
7287         ARMCPRegInfo v6_idregs[] = {
7288             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7289               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7290               .access = PL1_R, .type = ARM_CP_CONST,
7291               .accessfn = access_aa32_tid3,
7292               .resetvalue = cpu->isar.id_pfr0 },
7293             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7294              * the value of the GIC field until after we define these regs.
7295              */
7296             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7297               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7298               .access = PL1_R, .type = ARM_CP_NO_RAW,
7299               .accessfn = access_aa32_tid3,
7300               .readfn = id_pfr1_read,
7301               .writefn = arm_cp_write_ignore },
7302             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7303               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7304               .access = PL1_R, .type = ARM_CP_CONST,
7305               .accessfn = access_aa32_tid3,
7306               .resetvalue = cpu->isar.id_dfr0 },
7307             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7308               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7309               .access = PL1_R, .type = ARM_CP_CONST,
7310               .accessfn = access_aa32_tid3,
7311               .resetvalue = cpu->id_afr0 },
7312             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7313               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7314               .access = PL1_R, .type = ARM_CP_CONST,
7315               .accessfn = access_aa32_tid3,
7316               .resetvalue = cpu->isar.id_mmfr0 },
7317             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7318               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7319               .access = PL1_R, .type = ARM_CP_CONST,
7320               .accessfn = access_aa32_tid3,
7321               .resetvalue = cpu->isar.id_mmfr1 },
7322             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7323               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7324               .access = PL1_R, .type = ARM_CP_CONST,
7325               .accessfn = access_aa32_tid3,
7326               .resetvalue = cpu->isar.id_mmfr2 },
7327             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7328               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7329               .access = PL1_R, .type = ARM_CP_CONST,
7330               .accessfn = access_aa32_tid3,
7331               .resetvalue = cpu->isar.id_mmfr3 },
7332             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7333               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7334               .access = PL1_R, .type = ARM_CP_CONST,
7335               .accessfn = access_aa32_tid3,
7336               .resetvalue = cpu->isar.id_isar0 },
7337             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7338               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7339               .access = PL1_R, .type = ARM_CP_CONST,
7340               .accessfn = access_aa32_tid3,
7341               .resetvalue = cpu->isar.id_isar1 },
7342             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7343               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7344               .access = PL1_R, .type = ARM_CP_CONST,
7345               .accessfn = access_aa32_tid3,
7346               .resetvalue = cpu->isar.id_isar2 },
7347             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7348               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7349               .access = PL1_R, .type = ARM_CP_CONST,
7350               .accessfn = access_aa32_tid3,
7351               .resetvalue = cpu->isar.id_isar3 },
7352             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7353               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7354               .access = PL1_R, .type = ARM_CP_CONST,
7355               .accessfn = access_aa32_tid3,
7356               .resetvalue = cpu->isar.id_isar4 },
7357             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7358               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7359               .access = PL1_R, .type = ARM_CP_CONST,
7360               .accessfn = access_aa32_tid3,
7361               .resetvalue = cpu->isar.id_isar5 },
7362             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7363               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7364               .access = PL1_R, .type = ARM_CP_CONST,
7365               .accessfn = access_aa32_tid3,
7366               .resetvalue = cpu->isar.id_mmfr4 },
7367             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7368               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7369               .access = PL1_R, .type = ARM_CP_CONST,
7370               .accessfn = access_aa32_tid3,
7371               .resetvalue = cpu->isar.id_isar6 },
7372         };
7373         define_arm_cp_regs(cpu, v6_idregs);
7374         define_arm_cp_regs(cpu, v6_cp_reginfo);
7375     } else {
7376         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7377     }
7378     if (arm_feature(env, ARM_FEATURE_V6K)) {
7379         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7380     }
7381     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7382         !arm_feature(env, ARM_FEATURE_PMSA)) {
7383         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7384     }
7385     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7386         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7387     }
7388     if (arm_feature(env, ARM_FEATURE_V7)) {
7389         ARMCPRegInfo clidr = {
7390             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7391             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7392             .access = PL1_R, .type = ARM_CP_CONST,
7393             .accessfn = access_aa64_tid2,
7394             .resetvalue = cpu->clidr
7395         };
7396         define_one_arm_cp_reg(cpu, &clidr);
7397         define_arm_cp_regs(cpu, v7_cp_reginfo);
7398         define_debug_regs(cpu);
7399         define_pmu_regs(cpu);
7400     } else {
7401         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7402     }
7403     if (arm_feature(env, ARM_FEATURE_V8)) {
7404         /*
7405          * v8 ID registers, which all have impdef reset values.
7406          * Note that within the ID register ranges the unused slots
7407          * must all RAZ, not UNDEF; future architecture versions may
7408          * define new registers here.
7409          * ID registers which are AArch64 views of the AArch32 ID registers
7410          * which already existed in v6 and v7 are handled elsewhere,
7411          * in v6_idregs[].
7412          */
7413         int i;
7414         ARMCPRegInfo v8_idregs[] = {
7415             /*
7416              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7417              * emulation because we don't know the right value for the
7418              * GIC field until after we define these regs.
7419              */
7420             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7421               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7422               .access = PL1_R,
7423 #ifdef CONFIG_USER_ONLY
7424               .type = ARM_CP_CONST,
7425               .resetvalue = cpu->isar.id_aa64pfr0
7426 #else
7427               .type = ARM_CP_NO_RAW,
7428               .accessfn = access_aa64_tid3,
7429               .readfn = id_aa64pfr0_read,
7430               .writefn = arm_cp_write_ignore
7431 #endif
7432             },
7433             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7434               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7435               .access = PL1_R, .type = ARM_CP_CONST,
7436               .accessfn = access_aa64_tid3,
7437               .resetvalue = cpu->isar.id_aa64pfr1},
7438             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7439               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7440               .access = PL1_R, .type = ARM_CP_CONST,
7441               .accessfn = access_aa64_tid3,
7442               .resetvalue = 0 },
7443             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7444               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7445               .access = PL1_R, .type = ARM_CP_CONST,
7446               .accessfn = access_aa64_tid3,
7447               .resetvalue = 0 },
7448             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7449               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7450               .access = PL1_R, .type = ARM_CP_CONST,
7451               .accessfn = access_aa64_tid3,
7452               .resetvalue = cpu->isar.id_aa64zfr0 },
7453             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7454               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7455               .access = PL1_R, .type = ARM_CP_CONST,
7456               .accessfn = access_aa64_tid3,
7457               .resetvalue = cpu->isar.id_aa64smfr0 },
7458             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7459               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7460               .access = PL1_R, .type = ARM_CP_CONST,
7461               .accessfn = access_aa64_tid3,
7462               .resetvalue = 0 },
7463             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7464               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7465               .access = PL1_R, .type = ARM_CP_CONST,
7466               .accessfn = access_aa64_tid3,
7467               .resetvalue = 0 },
7468             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7469               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7470               .access = PL1_R, .type = ARM_CP_CONST,
7471               .accessfn = access_aa64_tid3,
7472               .resetvalue = cpu->isar.id_aa64dfr0 },
7473             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7474               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7475               .access = PL1_R, .type = ARM_CP_CONST,
7476               .accessfn = access_aa64_tid3,
7477               .resetvalue = cpu->isar.id_aa64dfr1 },
7478             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7479               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7480               .access = PL1_R, .type = ARM_CP_CONST,
7481               .accessfn = access_aa64_tid3,
7482               .resetvalue = 0 },
7483             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7484               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7485               .access = PL1_R, .type = ARM_CP_CONST,
7486               .accessfn = access_aa64_tid3,
7487               .resetvalue = 0 },
7488             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7489               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7490               .access = PL1_R, .type = ARM_CP_CONST,
7491               .accessfn = access_aa64_tid3,
7492               .resetvalue = cpu->id_aa64afr0 },
7493             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7494               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7495               .access = PL1_R, .type = ARM_CP_CONST,
7496               .accessfn = access_aa64_tid3,
7497               .resetvalue = cpu->id_aa64afr1 },
7498             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7499               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7500               .access = PL1_R, .type = ARM_CP_CONST,
7501               .accessfn = access_aa64_tid3,
7502               .resetvalue = 0 },
7503             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7504               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7505               .access = PL1_R, .type = ARM_CP_CONST,
7506               .accessfn = access_aa64_tid3,
7507               .resetvalue = 0 },
7508             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7509               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7510               .access = PL1_R, .type = ARM_CP_CONST,
7511               .accessfn = access_aa64_tid3,
7512               .resetvalue = cpu->isar.id_aa64isar0 },
7513             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7514               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7515               .access = PL1_R, .type = ARM_CP_CONST,
7516               .accessfn = access_aa64_tid3,
7517               .resetvalue = cpu->isar.id_aa64isar1 },
7518             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7519               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7520               .access = PL1_R, .type = ARM_CP_CONST,
7521               .accessfn = access_aa64_tid3,
7522               .resetvalue = 0 },
7523             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7524               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7525               .access = PL1_R, .type = ARM_CP_CONST,
7526               .accessfn = access_aa64_tid3,
7527               .resetvalue = 0 },
7528             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7529               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7530               .access = PL1_R, .type = ARM_CP_CONST,
7531               .accessfn = access_aa64_tid3,
7532               .resetvalue = 0 },
7533             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7534               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7535               .access = PL1_R, .type = ARM_CP_CONST,
7536               .accessfn = access_aa64_tid3,
7537               .resetvalue = 0 },
7538             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7539               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7540               .access = PL1_R, .type = ARM_CP_CONST,
7541               .accessfn = access_aa64_tid3,
7542               .resetvalue = 0 },
7543             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7544               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7545               .access = PL1_R, .type = ARM_CP_CONST,
7546               .accessfn = access_aa64_tid3,
7547               .resetvalue = 0 },
7548             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7549               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7550               .access = PL1_R, .type = ARM_CP_CONST,
7551               .accessfn = access_aa64_tid3,
7552               .resetvalue = cpu->isar.id_aa64mmfr0 },
7553             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7554               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7555               .access = PL1_R, .type = ARM_CP_CONST,
7556               .accessfn = access_aa64_tid3,
7557               .resetvalue = cpu->isar.id_aa64mmfr1 },
7558             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7559               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7560               .access = PL1_R, .type = ARM_CP_CONST,
7561               .accessfn = access_aa64_tid3,
7562               .resetvalue = cpu->isar.id_aa64mmfr2 },
7563             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7564               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7565               .access = PL1_R, .type = ARM_CP_CONST,
7566               .accessfn = access_aa64_tid3,
7567               .resetvalue = 0 },
7568             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7569               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7570               .access = PL1_R, .type = ARM_CP_CONST,
7571               .accessfn = access_aa64_tid3,
7572               .resetvalue = 0 },
7573             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7574               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7575               .access = PL1_R, .type = ARM_CP_CONST,
7576               .accessfn = access_aa64_tid3,
7577               .resetvalue = 0 },
7578             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7579               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7580               .access = PL1_R, .type = ARM_CP_CONST,
7581               .accessfn = access_aa64_tid3,
7582               .resetvalue = 0 },
7583             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7584               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7585               .access = PL1_R, .type = ARM_CP_CONST,
7586               .accessfn = access_aa64_tid3,
7587               .resetvalue = 0 },
7588             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7589               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7590               .access = PL1_R, .type = ARM_CP_CONST,
7591               .accessfn = access_aa64_tid3,
7592               .resetvalue = cpu->isar.mvfr0 },
7593             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7594               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7595               .access = PL1_R, .type = ARM_CP_CONST,
7596               .accessfn = access_aa64_tid3,
7597               .resetvalue = cpu->isar.mvfr1 },
7598             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7599               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7600               .access = PL1_R, .type = ARM_CP_CONST,
7601               .accessfn = access_aa64_tid3,
7602               .resetvalue = cpu->isar.mvfr2 },
7603             /*
7604              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7605              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7606              * as RAZ, since it is in the "reserved for future ID
7607              * registers, RAZ" part of the AArch32 encoding space.
7608              */
7609             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7610               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7611               .access = PL1_R, .type = ARM_CP_CONST,
7612               .accessfn = access_aa64_tid3,
7613               .resetvalue = 0 },
7614             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7615               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7616               .access = PL1_R, .type = ARM_CP_CONST,
7617               .accessfn = access_aa64_tid3,
7618               .resetvalue = 0 },
7619             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7620               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7621               .access = PL1_R, .type = ARM_CP_CONST,
7622               .accessfn = access_aa64_tid3,
7623               .resetvalue = 0 },
7624             /*
7625              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7626              * they're also RAZ for AArch64, and in v8 are gradually
7627              * being filled with AArch64-view-of-AArch32-ID-register
7628              * for new ID registers.
7629              */
7630             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7631               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7632               .access = PL1_R, .type = ARM_CP_CONST,
7633               .accessfn = access_aa64_tid3,
7634               .resetvalue = 0 },
7635             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7636               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7637               .access = PL1_R, .type = ARM_CP_CONST,
7638               .accessfn = access_aa64_tid3,
7639               .resetvalue = cpu->isar.id_pfr2 },
7640             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7641               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7642               .access = PL1_R, .type = ARM_CP_CONST,
7643               .accessfn = access_aa64_tid3,
7644               .resetvalue = cpu->isar.id_dfr1 },
7645             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7646               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7647               .access = PL1_R, .type = ARM_CP_CONST,
7648               .accessfn = access_aa64_tid3,
7649               .resetvalue = cpu->isar.id_mmfr5 },
7650             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7651               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7652               .access = PL1_R, .type = ARM_CP_CONST,
7653               .accessfn = access_aa64_tid3,
7654               .resetvalue = 0 },
7655             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7656               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7657               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7658               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7659             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7660               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7661               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7662               .resetvalue = cpu->pmceid0 },
7663             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7664               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7665               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7666               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7667             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7668               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7669               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7670               .resetvalue = cpu->pmceid1 },
7671         };
7672 #ifdef CONFIG_USER_ONLY
7673         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7674             { .name = "ID_AA64PFR0_EL1",
7675               .exported_bits = 0x000f000f00ff0000,
7676               .fixed_bits    = 0x0000000000000011 },
7677             { .name = "ID_AA64PFR1_EL1",
7678               .exported_bits = 0x00000000000000f0 },
7679             { .name = "ID_AA64PFR*_EL1_RESERVED",
7680               .is_glob = true                     },
7681             { .name = "ID_AA64ZFR0_EL1"           },
7682             { .name = "ID_AA64MMFR0_EL1",
7683               .fixed_bits    = 0x00000000ff000000 },
7684             { .name = "ID_AA64MMFR1_EL1"          },
7685             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7686               .is_glob = true                     },
7687             { .name = "ID_AA64DFR0_EL1",
7688               .fixed_bits    = 0x0000000000000006 },
7689             { .name = "ID_AA64DFR1_EL1"           },
7690             { .name = "ID_AA64DFR*_EL1_RESERVED",
7691               .is_glob = true                     },
7692             { .name = "ID_AA64AFR*",
7693               .is_glob = true                     },
7694             { .name = "ID_AA64ISAR0_EL1",
7695               .exported_bits = 0x00fffffff0fffff0 },
7696             { .name = "ID_AA64ISAR1_EL1",
7697               .exported_bits = 0x000000f0ffffffff },
7698             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7699               .is_glob = true                     },
7700         };
7701         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7702 #endif
7703         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7704         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7705             !arm_feature(env, ARM_FEATURE_EL2)) {
7706             ARMCPRegInfo rvbar = {
7707                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7708                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7709                 .access = PL1_R,
7710                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7711             };
7712             define_one_arm_cp_reg(cpu, &rvbar);
7713         }
7714         define_arm_cp_regs(cpu, v8_idregs);
7715         define_arm_cp_regs(cpu, v8_cp_reginfo);
7716
7717         for (i = 4; i < 16; i++) {
7718             /*
7719              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7720              * For pre-v8 cores there are RAZ patterns for these in
7721              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7722              * v8 extends the "must RAZ" part of the ID register space
7723              * to also cover c0, 0, c{8-15}, {0-7}.
7724              * These are STATE_AA32 because in the AArch64 sysreg space
7725              * c4-c7 is where the AArch64 ID registers live (and we've
7726              * already defined those in v8_idregs[]), and c8-c15 are not
7727              * "must RAZ" for AArch64.
7728              */
7729             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7730             ARMCPRegInfo v8_aa32_raz_idregs = {
7731                 .name = name,
7732                 .state = ARM_CP_STATE_AA32,
7733                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7734                 .access = PL1_R, .type = ARM_CP_CONST,
7735                 .accessfn = access_aa64_tid3,
7736                 .resetvalue = 0 };
7737             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7738         }
7739     }
7740
7741     /*
7742      * Register the base EL2 cpregs.
7743      * Pre v8, these registers are implemented only as part of the
7744      * Virtualization Extensions (EL2 present).  Beginning with v8,
7745      * if EL2 is missing but EL3 is enabled, mostly these become
7746      * RES0 from EL3, with some specific exceptions.
7747      */
7748     if (arm_feature(env, ARM_FEATURE_EL2)
7749         || (arm_feature(env, ARM_FEATURE_EL3)
7750             && arm_feature(env, ARM_FEATURE_V8))) {
7751         uint64_t vmpidr_def = mpidr_read_val(env);
7752         ARMCPRegInfo vpidr_regs[] = {
7753             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7754               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7755               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7756               .resetvalue = cpu->midr,
7757               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7758               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7759             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7760               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7761               .access = PL2_RW, .resetvalue = cpu->midr,
7762               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7763               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7764             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7765               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7766               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7767               .resetvalue = vmpidr_def,
7768               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7769               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7770             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7771               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7772               .access = PL2_RW, .resetvalue = vmpidr_def,
7773               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7774               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7775         };
7776         /*
7777          * The only field of MDCR_EL2 that has a defined architectural reset
7778          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7779          */
7780         ARMCPRegInfo mdcr_el2 = {
7781             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
7782             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
7783             .writefn = mdcr_el2_write,
7784             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7785             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7786         };
7787         define_one_arm_cp_reg(cpu, &mdcr_el2);
7788         define_arm_cp_regs(cpu, vpidr_regs);
7789         define_arm_cp_regs(cpu, el2_cp_reginfo);
7790         if (arm_feature(env, ARM_FEATURE_V8)) {
7791             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7792         }
7793         if (cpu_isar_feature(aa64_sel2, cpu)) {
7794             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7795         }
7796         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7797         if (!arm_feature(env, ARM_FEATURE_EL3)) {
7798             ARMCPRegInfo rvbar = {
7799                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7800                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7801                 .access = PL2_R,
7802                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7803             };
7804             define_one_arm_cp_reg(cpu, &rvbar);
7805         }
7806     }
7807
7808     /* Register the base EL3 cpregs. */
7809     if (arm_feature(env, ARM_FEATURE_EL3)) {
7810         define_arm_cp_regs(cpu, el3_cp_reginfo);
7811         ARMCPRegInfo el3_regs[] = {
7812             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7813               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7814               .access = PL3_R,
7815               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7816             },
7817             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7818               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7819               .access = PL3_RW,
7820               .raw_writefn = raw_write, .writefn = sctlr_write,
7821               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7822               .resetvalue = cpu->reset_sctlr },
7823         };
7824
7825         define_arm_cp_regs(cpu, el3_regs);
7826     }
7827     /* The behaviour of NSACR is sufficiently various that we don't
7828      * try to describe it in a single reginfo:
7829      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
7830      *     reads as constant 0xc00 from NS EL1 and NS EL2
7831      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7832      *  if v7 without EL3, register doesn't exist
7833      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7834      */
7835     if (arm_feature(env, ARM_FEATURE_EL3)) {
7836         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7837             static const ARMCPRegInfo nsacr = {
7838                 .name = "NSACR", .type = ARM_CP_CONST,
7839                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7840                 .access = PL1_RW, .accessfn = nsacr_access,
7841                 .resetvalue = 0xc00
7842             };
7843             define_one_arm_cp_reg(cpu, &nsacr);
7844         } else {
7845             static const ARMCPRegInfo nsacr = {
7846                 .name = "NSACR",
7847                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7848                 .access = PL3_RW | PL1_R,
7849                 .resetvalue = 0,
7850                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7851             };
7852             define_one_arm_cp_reg(cpu, &nsacr);
7853         }
7854     } else {
7855         if (arm_feature(env, ARM_FEATURE_V8)) {
7856             static const ARMCPRegInfo nsacr = {
7857                 .name = "NSACR", .type = ARM_CP_CONST,
7858                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7859                 .access = PL1_R,
7860                 .resetvalue = 0xc00
7861             };
7862             define_one_arm_cp_reg(cpu, &nsacr);
7863         }
7864     }
7865
7866     if (arm_feature(env, ARM_FEATURE_PMSA)) {
7867         if (arm_feature(env, ARM_FEATURE_V6)) {
7868             /* PMSAv6 not implemented */
7869             assert(arm_feature(env, ARM_FEATURE_V7));
7870             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7871             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7872         } else {
7873             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7874         }
7875     } else {
7876         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7877         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
7878         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
7879         if (cpu_isar_feature(aa32_hpd, cpu)) {
7880             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7881         }
7882     }
7883     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7884         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7885     }
7886     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7887         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7888     }
7889     if (arm_feature(env, ARM_FEATURE_VAPA)) {
7890         define_arm_cp_regs(cpu, vapa_cp_reginfo);
7891     }
7892     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7893         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7894     }
7895     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7896         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7897     }
7898     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7899         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7900     }
7901     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7902         define_arm_cp_regs(cpu, omap_cp_reginfo);
7903     }
7904     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7905         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7906     }
7907     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7908         define_arm_cp_regs(cpu, xscale_cp_reginfo);
7909     }
7910     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7911         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7912     }
7913     if (arm_feature(env, ARM_FEATURE_LPAE)) {
7914         define_arm_cp_regs(cpu, lpae_cp_reginfo);
7915     }
7916     if (cpu_isar_feature(aa32_jazelle, cpu)) {
7917         define_arm_cp_regs(cpu, jazelle_regs);
7918     }
7919     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7920      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7921      * be read-only (ie write causes UNDEF exception).
7922      */
7923     {
7924         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7925             /* Pre-v8 MIDR space.
7926              * Note that the MIDR isn't a simple constant register because
7927              * of the TI925 behaviour where writes to another register can
7928              * cause the MIDR value to change.
7929              *
7930              * Unimplemented registers in the c15 0 0 0 space default to
7931              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7932              * and friends override accordingly.
7933              */
7934             { .name = "MIDR",
7935               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7936               .access = PL1_R, .resetvalue = cpu->midr,
7937               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
7938               .readfn = midr_read,
7939               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7940               .type = ARM_CP_OVERRIDE },
7941             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7942             { .name = "DUMMY",
7943               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7944               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7945             { .name = "DUMMY",
7946               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7947               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7948             { .name = "DUMMY",
7949               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7950               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7951             { .name = "DUMMY",
7952               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7953               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7954             { .name = "DUMMY",
7955               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7956               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7957         };
7958         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
7959             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7960               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
7961               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7962               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7963               .readfn = midr_read },
7964             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7965             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7966               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7967               .access = PL1_R, .resetvalue = cpu->midr },
7968             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7969               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7970               .access = PL1_R, .resetvalue = cpu->midr },
7971             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7972               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
7973               .access = PL1_R,
7974               .accessfn = access_aa64_tid1,
7975               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
7976         };
7977         ARMCPRegInfo id_cp_reginfo[] = {
7978             /* These are common to v8 and pre-v8 */
7979             { .name = "CTR",
7980               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
7981               .access = PL1_R, .accessfn = ctr_el0_access,
7982               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7983             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7984               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7985               .access = PL0_R, .accessfn = ctr_el0_access,
7986               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7987             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7988             { .name = "TCMTR",
7989               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
7990               .access = PL1_R,
7991               .accessfn = access_aa32_tid1,
7992               .type = ARM_CP_CONST, .resetvalue = 0 },
7993         };
7994         /* TLBTR is specific to VMSA */
7995         ARMCPRegInfo id_tlbtr_reginfo = {
7996               .name = "TLBTR",
7997               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
7998               .access = PL1_R,
7999               .accessfn = access_aa32_tid1,
8000               .type = ARM_CP_CONST, .resetvalue = 0,
8001         };
8002         /* MPUIR is specific to PMSA V6+ */
8003         ARMCPRegInfo id_mpuir_reginfo = {
8004               .name = "MPUIR",
8005               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8006               .access = PL1_R, .type = ARM_CP_CONST,
8007               .resetvalue = cpu->pmsav7_dregion << 8
8008         };
8009         static const ARMCPRegInfo crn0_wi_reginfo = {
8010             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8011             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8012             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8013         };
8014 #ifdef CONFIG_USER_ONLY
8015         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8016             { .name = "MIDR_EL1",
8017               .exported_bits = 0x00000000ffffffff },
8018             { .name = "REVIDR_EL1"                },
8019         };
8020         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8021 #endif
8022         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8023             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8024             size_t i;
8025             /* Register the blanket "writes ignored" value first to cover the
8026              * whole space. Then update the specific ID registers to allow write
8027              * access, so that they ignore writes rather than causing them to
8028              * UNDEF.
8029              */
8030             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8031             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8032                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8033             }
8034             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8035                 id_cp_reginfo[i].access = PL1_RW;
8036             }
8037             id_mpuir_reginfo.access = PL1_RW;
8038             id_tlbtr_reginfo.access = PL1_RW;
8039         }
8040         if (arm_feature(env, ARM_FEATURE_V8)) {
8041             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8042         } else {
8043             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8044         }
8045         define_arm_cp_regs(cpu, id_cp_reginfo);
8046         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8047             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8048         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8049             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8050         }
8051     }
8052
8053     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8054         ARMCPRegInfo mpidr_cp_reginfo[] = {
8055             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8056               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8057               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8058         };
8059 #ifdef CONFIG_USER_ONLY
8060         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8061             { .name = "MPIDR_EL1",
8062               .fixed_bits = 0x0000000080000000 },
8063         };
8064         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8065 #endif
8066         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8067     }
8068
8069     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8070         ARMCPRegInfo auxcr_reginfo[] = {
8071             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8072               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8073               .access = PL1_RW, .accessfn = access_tacr,
8074               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8075             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8076               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8077               .access = PL2_RW, .type = ARM_CP_CONST,
8078               .resetvalue = 0 },
8079             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8080               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8081               .access = PL3_RW, .type = ARM_CP_CONST,
8082               .resetvalue = 0 },
8083         };
8084         define_arm_cp_regs(cpu, auxcr_reginfo);
8085         if (cpu_isar_feature(aa32_ac2, cpu)) {
8086             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8087         }
8088     }
8089
8090     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8091         /*
8092          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8093          * There are two flavours:
8094          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8095          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8096          *      32-bit register visible to AArch32 at a different encoding
8097          *      to the "flavour 1" register and with the bits rearranged to
8098          *      be able to squash a 64-bit address into the 32-bit view.
8099          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8100          * in future if we support AArch32-only configs of some of the
8101          * AArch64 cores we might need to add a specific feature flag
8102          * to indicate cores with "flavour 2" CBAR.
8103          */
8104         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8105             /* 32 bit view is [31:18] 0...0 [43:32]. */
8106             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8107                 | extract64(cpu->reset_cbar, 32, 12);
8108             ARMCPRegInfo cbar_reginfo[] = {
8109                 { .name = "CBAR",
8110                   .type = ARM_CP_CONST,
8111                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8112                   .access = PL1_R, .resetvalue = cbar32 },
8113                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8114                   .type = ARM_CP_CONST,
8115                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8116                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8117             };
8118             /* We don't implement a r/w 64 bit CBAR currently */
8119             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8120             define_arm_cp_regs(cpu, cbar_reginfo);
8121         } else {
8122             ARMCPRegInfo cbar = {
8123                 .name = "CBAR",
8124                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8125                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8126                 .fieldoffset = offsetof(CPUARMState,
8127                                         cp15.c15_config_base_address)
8128             };
8129             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8130                 cbar.access = PL1_R;
8131                 cbar.fieldoffset = 0;
8132                 cbar.type = ARM_CP_CONST;
8133             }
8134             define_one_arm_cp_reg(cpu, &cbar);
8135         }
8136     }
8137
8138     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8139         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8140             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8141               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8142               .access = PL1_RW, .writefn = vbar_write,
8143               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8144                                      offsetof(CPUARMState, cp15.vbar_ns) },
8145               .resetvalue = 0 },
8146         };
8147         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8148     }
8149
8150     /* Generic registers whose values depend on the implementation */
8151     {
8152         ARMCPRegInfo sctlr = {
8153             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8154             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8155             .access = PL1_RW, .accessfn = access_tvm_trvm,
8156             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8157                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8158             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8159             .raw_writefn = raw_write,
8160         };
8161         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8162             /* Normally we would always end the TB on an SCTLR write, but Linux
8163              * arch/arm/mach-pxa/sleep.S expects two instructions following
8164              * an MMU enable to execute from cache.  Imitate this behaviour.
8165              */
8166             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8167         }
8168         define_one_arm_cp_reg(cpu, &sctlr);
8169     }
8170
8171     if (cpu_isar_feature(aa64_lor, cpu)) {
8172         define_arm_cp_regs(cpu, lor_reginfo);
8173     }
8174     if (cpu_isar_feature(aa64_pan, cpu)) {
8175         define_one_arm_cp_reg(cpu, &pan_reginfo);
8176     }
8177 #ifndef CONFIG_USER_ONLY
8178     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8179         define_arm_cp_regs(cpu, ats1e1_reginfo);
8180     }
8181     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8182         define_arm_cp_regs(cpu, ats1cp_reginfo);
8183     }
8184 #endif
8185     if (cpu_isar_feature(aa64_uao, cpu)) {
8186         define_one_arm_cp_reg(cpu, &uao_reginfo);
8187     }
8188
8189     if (cpu_isar_feature(aa64_dit, cpu)) {
8190         define_one_arm_cp_reg(cpu, &dit_reginfo);
8191     }
8192     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8193         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8194     }
8195     if (cpu_isar_feature(any_ras, cpu)) {
8196         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8197     }
8198
8199     if (cpu_isar_feature(aa64_vh, cpu) ||
8200         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8201         define_one_arm_cp_reg(cpu, &contextidr_el2);
8202     }
8203     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8204         define_arm_cp_regs(cpu, vhe_reginfo);
8205     }
8206
8207     if (cpu_isar_feature(aa64_sve, cpu)) {
8208         define_arm_cp_regs(cpu, zcr_reginfo);
8209     }
8210
8211     if (cpu_isar_feature(aa64_hcx, cpu)) {
8212         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8213     }
8214
8215 #ifdef TARGET_AARCH64
8216     if (cpu_isar_feature(aa64_sme, cpu)) {
8217         define_arm_cp_regs(cpu, sme_reginfo);
8218     }
8219     if (cpu_isar_feature(aa64_pauth, cpu)) {
8220         define_arm_cp_regs(cpu, pauth_reginfo);
8221     }
8222     if (cpu_isar_feature(aa64_rndr, cpu)) {
8223         define_arm_cp_regs(cpu, rndr_reginfo);
8224     }
8225     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8226         define_arm_cp_regs(cpu, tlbirange_reginfo);
8227     }
8228     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8229         define_arm_cp_regs(cpu, tlbios_reginfo);
8230     }
8231 #ifndef CONFIG_USER_ONLY
8232     /* Data Cache clean instructions up to PoP */
8233     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8234         define_one_arm_cp_reg(cpu, dcpop_reg);
8235
8236         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8237             define_one_arm_cp_reg(cpu, dcpodp_reg);
8238         }
8239     }
8240 #endif /*CONFIG_USER_ONLY*/
8241
8242     /*
8243      * If full MTE is enabled, add all of the system registers.
8244      * If only "instructions available at EL0" are enabled,
8245      * then define only a RAZ/WI version of PSTATE.TCO.
8246      */
8247     if (cpu_isar_feature(aa64_mte, cpu)) {
8248         define_arm_cp_regs(cpu, mte_reginfo);
8249         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8250     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8251         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8252         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8253     }
8254
8255     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8256         define_arm_cp_regs(cpu, scxtnum_reginfo);
8257     }
8258 #endif
8259
8260     if (cpu_isar_feature(any_predinv, cpu)) {
8261         define_arm_cp_regs(cpu, predinv_reginfo);
8262     }
8263
8264     if (cpu_isar_feature(any_ccidx, cpu)) {
8265         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8266     }
8267
8268 #ifndef CONFIG_USER_ONLY
8269     /*
8270      * Register redirections and aliases must be done last,
8271      * after the registers from the other extensions have been defined.
8272      */
8273     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8274         define_arm_vh_e2h_redirects_aliases(cpu);
8275     }
8276 #endif
8277 }
8278
8279 /* Sort alphabetically by type name, except for "any". */
8280 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8281 {
8282     ObjectClass *class_a = (ObjectClass *)a;
8283     ObjectClass *class_b = (ObjectClass *)b;
8284     const char *name_a, *name_b;
8285
8286     name_a = object_class_get_name(class_a);
8287     name_b = object_class_get_name(class_b);
8288     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8289         return 1;
8290     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8291         return -1;
8292     } else {
8293         return strcmp(name_a, name_b);
8294     }
8295 }
8296
8297 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8298 {
8299     ObjectClass *oc = data;
8300     CPUClass *cc = CPU_CLASS(oc);
8301     const char *typename;
8302     char *name;
8303
8304     typename = object_class_get_name(oc);
8305     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8306     if (cc->deprecation_note) {
8307         qemu_printf("  %s (deprecated)\n", name);
8308     } else {
8309         qemu_printf("  %s\n", name);
8310     }
8311     g_free(name);
8312 }
8313
8314 void arm_cpu_list(void)
8315 {
8316     GSList *list;
8317
8318     list = object_class_get_list(TYPE_ARM_CPU, false);
8319     list = g_slist_sort(list, arm_cpu_list_compare);
8320     qemu_printf("Available CPUs:\n");
8321     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8322     g_slist_free(list);
8323 }
8324
8325 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8326 {
8327     ObjectClass *oc = data;
8328     CpuDefinitionInfoList **cpu_list = user_data;
8329     CpuDefinitionInfo *info;
8330     const char *typename;
8331
8332     typename = object_class_get_name(oc);
8333     info = g_malloc0(sizeof(*info));
8334     info->name = g_strndup(typename,
8335                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8336     info->q_typename = g_strdup(typename);
8337
8338     QAPI_LIST_PREPEND(*cpu_list, info);
8339 }
8340
8341 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8342 {
8343     CpuDefinitionInfoList *cpu_list = NULL;
8344     GSList *list;
8345
8346     list = object_class_get_list(TYPE_ARM_CPU, false);
8347     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8348     g_slist_free(list);
8349
8350     return cpu_list;
8351 }
8352
8353 /*
8354  * Private utility function for define_one_arm_cp_reg_with_opaque():
8355  * add a single reginfo struct to the hash table.
8356  */
8357 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8358                                    void *opaque, CPState state,
8359                                    CPSecureState secstate,
8360                                    int crm, int opc1, int opc2,
8361                                    const char *name)
8362 {
8363     CPUARMState *env = &cpu->env;
8364     uint32_t key;
8365     ARMCPRegInfo *r2;
8366     bool is64 = r->type & ARM_CP_64BIT;
8367     bool ns = secstate & ARM_CP_SECSTATE_NS;
8368     int cp = r->cp;
8369     size_t name_len;
8370     bool make_const;
8371
8372     switch (state) {
8373     case ARM_CP_STATE_AA32:
8374         /* We assume it is a cp15 register if the .cp field is left unset. */
8375         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8376             cp = 15;
8377         }
8378         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8379         break;
8380     case ARM_CP_STATE_AA64:
8381         /*
8382          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8383          * cp == 0 as equivalent to the value for "standard guest-visible
8384          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8385          * in their AArch64 view (the .cp value may be non-zero for the
8386          * benefit of the AArch32 view).
8387          */
8388         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8389             cp = CP_REG_ARM64_SYSREG_CP;
8390         }
8391         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8392         break;
8393     default:
8394         g_assert_not_reached();
8395     }
8396
8397     /* Overriding of an existing definition must be explicitly requested. */
8398     if (!(r->type & ARM_CP_OVERRIDE)) {
8399         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8400         if (oldreg) {
8401             assert(oldreg->type & ARM_CP_OVERRIDE);
8402         }
8403     }
8404
8405     /*
8406      * Eliminate registers that are not present because the EL is missing.
8407      * Doing this here makes it easier to put all registers for a given
8408      * feature into the same ARMCPRegInfo array and define them all at once.
8409      */
8410     make_const = false;
8411     if (arm_feature(env, ARM_FEATURE_EL3)) {
8412         /*
8413          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8414          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8415          */
8416         int min_el = ctz32(r->access) / 2;
8417         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8418             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8419                 return;
8420             }
8421             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8422         }
8423     } else {
8424         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8425                                  ? PL2_RW : PL1_RW);
8426         if ((r->access & max_el) == 0) {
8427             return;
8428         }
8429     }
8430
8431     /* Combine cpreg and name into one allocation. */
8432     name_len = strlen(name) + 1;
8433     r2 = g_malloc(sizeof(*r2) + name_len);
8434     *r2 = *r;
8435     r2->name = memcpy(r2 + 1, name, name_len);
8436
8437     /*
8438      * Update fields to match the instantiation, overwiting wildcards
8439      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8440      */
8441     r2->cp = cp;
8442     r2->crm = crm;
8443     r2->opc1 = opc1;
8444     r2->opc2 = opc2;
8445     r2->state = state;
8446     r2->secure = secstate;
8447     if (opaque) {
8448         r2->opaque = opaque;
8449     }
8450
8451     if (make_const) {
8452         /* This should not have been a very special register to begin. */
8453         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8454         assert(old_special == 0 || old_special == ARM_CP_NOP);
8455         /*
8456          * Set the special function to CONST, retaining the other flags.
8457          * This is important for e.g. ARM_CP_SVE so that we still
8458          * take the SVE trap if CPTR_EL3.EZ == 0.
8459          */
8460         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8461         /*
8462          * Usually, these registers become RES0, but there are a few
8463          * special cases like VPIDR_EL2 which have a constant non-zero
8464          * value with writes ignored.
8465          */
8466         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8467             r2->resetvalue = 0;
8468         }
8469         /*
8470          * ARM_CP_CONST has precedence, so removing the callbacks and
8471          * offsets are not strictly necessary, but it is potentially
8472          * less confusing to debug later.
8473          */
8474         r2->readfn = NULL;
8475         r2->writefn = NULL;
8476         r2->raw_readfn = NULL;
8477         r2->raw_writefn = NULL;
8478         r2->resetfn = NULL;
8479         r2->fieldoffset = 0;
8480         r2->bank_fieldoffsets[0] = 0;
8481         r2->bank_fieldoffsets[1] = 0;
8482     } else {
8483         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8484
8485         if (isbanked) {
8486             /*
8487              * Register is banked (using both entries in array).
8488              * Overwriting fieldoffset as the array is only used to define
8489              * banked registers but later only fieldoffset is used.
8490              */
8491             r2->fieldoffset = r->bank_fieldoffsets[ns];
8492         }
8493         if (state == ARM_CP_STATE_AA32) {
8494             if (isbanked) {
8495                 /*
8496                  * If the register is banked then we don't need to migrate or
8497                  * reset the 32-bit instance in certain cases:
8498                  *
8499                  * 1) If the register has both 32-bit and 64-bit instances
8500                  *    then we can count on the 64-bit instance taking care
8501                  *    of the non-secure bank.
8502                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8503                  *    version taking care of the secure bank.  This requires
8504                  *    that separate 32 and 64-bit definitions are provided.
8505                  */
8506                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8507                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8508                     r2->type |= ARM_CP_ALIAS;
8509                 }
8510             } else if ((secstate != r->secure) && !ns) {
8511                 /*
8512                  * The register is not banked so we only want to allow
8513                  * migration of the non-secure instance.
8514                  */
8515                 r2->type |= ARM_CP_ALIAS;
8516             }
8517
8518             if (HOST_BIG_ENDIAN &&
8519                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8520                 r2->fieldoffset += sizeof(uint32_t);
8521             }
8522         }
8523     }
8524
8525     /*
8526      * By convention, for wildcarded registers only the first
8527      * entry is used for migration; the others are marked as
8528      * ALIAS so we don't try to transfer the register
8529      * multiple times. Special registers (ie NOP/WFI) are
8530      * never migratable and not even raw-accessible.
8531      */
8532     if (r2->type & ARM_CP_SPECIAL_MASK) {
8533         r2->type |= ARM_CP_NO_RAW;
8534     }
8535     if (((r->crm == CP_ANY) && crm != 0) ||
8536         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8537         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8538         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8539     }
8540
8541     /*
8542      * Check that raw accesses are either forbidden or handled. Note that
8543      * we can't assert this earlier because the setup of fieldoffset for
8544      * banked registers has to be done first.
8545      */
8546     if (!(r2->type & ARM_CP_NO_RAW)) {
8547         assert(!raw_accessors_invalid(r2));
8548     }
8549
8550     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8551 }
8552
8553
8554 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8555                                        const ARMCPRegInfo *r, void *opaque)
8556 {
8557     /* Define implementations of coprocessor registers.
8558      * We store these in a hashtable because typically
8559      * there are less than 150 registers in a space which
8560      * is 16*16*16*8*8 = 262144 in size.
8561      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8562      * If a register is defined twice then the second definition is
8563      * used, so this can be used to define some generic registers and
8564      * then override them with implementation specific variations.
8565      * At least one of the original and the second definition should
8566      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8567      * against accidental use.
8568      *
8569      * The state field defines whether the register is to be
8570      * visible in the AArch32 or AArch64 execution state. If the
8571      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8572      * reginfo structure for the AArch32 view, which sees the lower
8573      * 32 bits of the 64 bit register.
8574      *
8575      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8576      * be wildcarded. AArch64 registers are always considered to be 64
8577      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8578      * the register, if any.
8579      */
8580     int crm, opc1, opc2;
8581     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8582     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8583     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8584     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8585     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8586     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8587     CPState state;
8588
8589     /* 64 bit registers have only CRm and Opc1 fields */
8590     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8591     /* op0 only exists in the AArch64 encodings */
8592     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8593     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8594     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8595     /*
8596      * This API is only for Arm's system coprocessors (14 and 15) or
8597      * (M-profile or v7A-and-earlier only) for implementation defined
8598      * coprocessors in the range 0..7.  Our decode assumes this, since
8599      * 8..13 can be used for other insns including VFP and Neon. See
8600      * valid_cp() in translate.c.  Assert here that we haven't tried
8601      * to use an invalid coprocessor number.
8602      */
8603     switch (r->state) {
8604     case ARM_CP_STATE_BOTH:
8605         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8606         if (r->cp == 0) {
8607             break;
8608         }
8609         /* fall through */
8610     case ARM_CP_STATE_AA32:
8611         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8612             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8613             assert(r->cp >= 14 && r->cp <= 15);
8614         } else {
8615             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8616         }
8617         break;
8618     case ARM_CP_STATE_AA64:
8619         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8620         break;
8621     default:
8622         g_assert_not_reached();
8623     }
8624     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8625      * encodes a minimum access level for the register. We roll this
8626      * runtime check into our general permission check code, so check
8627      * here that the reginfo's specified permissions are strict enough
8628      * to encompass the generic architectural permission check.
8629      */
8630     if (r->state != ARM_CP_STATE_AA32) {
8631         CPAccessRights mask;
8632         switch (r->opc1) {
8633         case 0:
8634             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8635             mask = PL0U_R | PL1_RW;
8636             break;
8637         case 1: case 2:
8638             /* min_EL EL1 */
8639             mask = PL1_RW;
8640             break;
8641         case 3:
8642             /* min_EL EL0 */
8643             mask = PL0_RW;
8644             break;
8645         case 4:
8646         case 5:
8647             /* min_EL EL2 */
8648             mask = PL2_RW;
8649             break;
8650         case 6:
8651             /* min_EL EL3 */
8652             mask = PL3_RW;
8653             break;
8654         case 7:
8655             /* min_EL EL1, secure mode only (we don't check the latter) */
8656             mask = PL1_RW;
8657             break;
8658         default:
8659             /* broken reginfo with out-of-range opc1 */
8660             g_assert_not_reached();
8661         }
8662         /* assert our permissions are not too lax (stricter is fine) */
8663         assert((r->access & ~mask) == 0);
8664     }
8665
8666     /* Check that the register definition has enough info to handle
8667      * reads and writes if they are permitted.
8668      */
8669     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8670         if (r->access & PL3_R) {
8671             assert((r->fieldoffset ||
8672                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8673                    r->readfn);
8674         }
8675         if (r->access & PL3_W) {
8676             assert((r->fieldoffset ||
8677                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8678                    r->writefn);
8679         }
8680     }
8681
8682     for (crm = crmmin; crm <= crmmax; crm++) {
8683         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8684             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8685                 for (state = ARM_CP_STATE_AA32;
8686                      state <= ARM_CP_STATE_AA64; state++) {
8687                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8688                         continue;
8689                     }
8690                     if (state == ARM_CP_STATE_AA32) {
8691                         /* Under AArch32 CP registers can be common
8692                          * (same for secure and non-secure world) or banked.
8693                          */
8694                         char *name;
8695
8696                         switch (r->secure) {
8697                         case ARM_CP_SECSTATE_S:
8698                         case ARM_CP_SECSTATE_NS:
8699                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8700                                                    r->secure, crm, opc1, opc2,
8701                                                    r->name);
8702                             break;
8703                         case ARM_CP_SECSTATE_BOTH:
8704                             name = g_strdup_printf("%s_S", r->name);
8705                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8706                                                    ARM_CP_SECSTATE_S,
8707                                                    crm, opc1, opc2, name);
8708                             g_free(name);
8709                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8710                                                    ARM_CP_SECSTATE_NS,
8711                                                    crm, opc1, opc2, r->name);
8712                             break;
8713                         default:
8714                             g_assert_not_reached();
8715                         }
8716                     } else {
8717                         /* AArch64 registers get mapped to non-secure instance
8718                          * of AArch32 */
8719                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8720                                                ARM_CP_SECSTATE_NS,
8721                                                crm, opc1, opc2, r->name);
8722                     }
8723                 }
8724             }
8725         }
8726     }
8727 }
8728
8729 /* Define a whole list of registers */
8730 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8731                                         void *opaque, size_t len)
8732 {
8733     size_t i;
8734     for (i = 0; i < len; ++i) {
8735         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8736     }
8737 }
8738
8739 /*
8740  * Modify ARMCPRegInfo for access from userspace.
8741  *
8742  * This is a data driven modification directed by
8743  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8744  * user-space cannot alter any values and dynamic values pertaining to
8745  * execution state are hidden from user space view anyway.
8746  */
8747 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8748                                  const ARMCPRegUserSpaceInfo *mods,
8749                                  size_t mods_len)
8750 {
8751     for (size_t mi = 0; mi < mods_len; ++mi) {
8752         const ARMCPRegUserSpaceInfo *m = mods + mi;
8753         GPatternSpec *pat = NULL;
8754
8755         if (m->is_glob) {
8756             pat = g_pattern_spec_new(m->name);
8757         }
8758         for (size_t ri = 0; ri < regs_len; ++ri) {
8759             ARMCPRegInfo *r = regs + ri;
8760
8761             if (pat && g_pattern_match_string(pat, r->name)) {
8762                 r->type = ARM_CP_CONST;
8763                 r->access = PL0U_R;
8764                 r->resetvalue = 0;
8765                 /* continue */
8766             } else if (strcmp(r->name, m->name) == 0) {
8767                 r->type = ARM_CP_CONST;
8768                 r->access = PL0U_R;
8769                 r->resetvalue &= m->exported_bits;
8770                 r->resetvalue |= m->fixed_bits;
8771                 break;
8772             }
8773         }
8774         if (pat) {
8775             g_pattern_spec_free(pat);
8776         }
8777     }
8778 }
8779
8780 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8781 {
8782     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8783 }
8784
8785 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8786                          uint64_t value)
8787 {
8788     /* Helper coprocessor write function for write-ignore registers */
8789 }
8790
8791 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8792 {
8793     /* Helper coprocessor write function for read-as-zero registers */
8794     return 0;
8795 }
8796
8797 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8798 {
8799     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8800 }
8801
8802 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8803 {
8804     /* Return true if it is not valid for us to switch to
8805      * this CPU mode (ie all the UNPREDICTABLE cases in
8806      * the ARM ARM CPSRWriteByInstr pseudocode).
8807      */
8808
8809     /* Changes to or from Hyp via MSR and CPS are illegal. */
8810     if (write_type == CPSRWriteByInstr &&
8811         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8812          mode == ARM_CPU_MODE_HYP)) {
8813         return 1;
8814     }
8815
8816     switch (mode) {
8817     case ARM_CPU_MODE_USR:
8818         return 0;
8819     case ARM_CPU_MODE_SYS:
8820     case ARM_CPU_MODE_SVC:
8821     case ARM_CPU_MODE_ABT:
8822     case ARM_CPU_MODE_UND:
8823     case ARM_CPU_MODE_IRQ:
8824     case ARM_CPU_MODE_FIQ:
8825         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8826          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8827          */
8828         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8829          * and CPS are treated as illegal mode changes.
8830          */
8831         if (write_type == CPSRWriteByInstr &&
8832             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8833             (arm_hcr_el2_eff(env) & HCR_TGE)) {
8834             return 1;
8835         }
8836         return 0;
8837     case ARM_CPU_MODE_HYP:
8838         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8839     case ARM_CPU_MODE_MON:
8840         return arm_current_el(env) < 3;
8841     default:
8842         return 1;
8843     }
8844 }
8845
8846 uint32_t cpsr_read(CPUARMState *env)
8847 {
8848     int ZF;
8849     ZF = (env->ZF == 0);
8850     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8851         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8852         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8853         | ((env->condexec_bits & 0xfc) << 8)
8854         | (env->GE << 16) | (env->daif & CPSR_AIF);
8855 }
8856
8857 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8858                 CPSRWriteType write_type)
8859 {
8860     uint32_t changed_daif;
8861     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8862         (mask & (CPSR_M | CPSR_E | CPSR_IL));
8863
8864     if (mask & CPSR_NZCV) {
8865         env->ZF = (~val) & CPSR_Z;
8866         env->NF = val;
8867         env->CF = (val >> 29) & 1;
8868         env->VF = (val << 3) & 0x80000000;
8869     }
8870     if (mask & CPSR_Q)
8871         env->QF = ((val & CPSR_Q) != 0);
8872     if (mask & CPSR_T)
8873         env->thumb = ((val & CPSR_T) != 0);
8874     if (mask & CPSR_IT_0_1) {
8875         env->condexec_bits &= ~3;
8876         env->condexec_bits |= (val >> 25) & 3;
8877     }
8878     if (mask & CPSR_IT_2_7) {
8879         env->condexec_bits &= 3;
8880         env->condexec_bits |= (val >> 8) & 0xfc;
8881     }
8882     if (mask & CPSR_GE) {
8883         env->GE = (val >> 16) & 0xf;
8884     }
8885
8886     /* In a V7 implementation that includes the security extensions but does
8887      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8888      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8889      * bits respectively.
8890      *
8891      * In a V8 implementation, it is permitted for privileged software to
8892      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8893      */
8894     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
8895         arm_feature(env, ARM_FEATURE_EL3) &&
8896         !arm_feature(env, ARM_FEATURE_EL2) &&
8897         !arm_is_secure(env)) {
8898
8899         changed_daif = (env->daif ^ val) & mask;
8900
8901         if (changed_daif & CPSR_A) {
8902             /* Check to see if we are allowed to change the masking of async
8903              * abort exceptions from a non-secure state.
8904              */
8905             if (!(env->cp15.scr_el3 & SCR_AW)) {
8906                 qemu_log_mask(LOG_GUEST_ERROR,
8907                               "Ignoring attempt to switch CPSR_A flag from "
8908                               "non-secure world with SCR.AW bit clear\n");
8909                 mask &= ~CPSR_A;
8910             }
8911         }
8912
8913         if (changed_daif & CPSR_F) {
8914             /* Check to see if we are allowed to change the masking of FIQ
8915              * exceptions from a non-secure state.
8916              */
8917             if (!(env->cp15.scr_el3 & SCR_FW)) {
8918                 qemu_log_mask(LOG_GUEST_ERROR,
8919                               "Ignoring attempt to switch CPSR_F flag from "
8920                               "non-secure world with SCR.FW bit clear\n");
8921                 mask &= ~CPSR_F;
8922             }
8923
8924             /* Check whether non-maskable FIQ (NMFI) support is enabled.
8925              * If this bit is set software is not allowed to mask
8926              * FIQs, but is allowed to set CPSR_F to 0.
8927              */
8928             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8929                 (val & CPSR_F)) {
8930                 qemu_log_mask(LOG_GUEST_ERROR,
8931                               "Ignoring attempt to enable CPSR_F flag "
8932                               "(non-maskable FIQ [NMFI] support enabled)\n");
8933                 mask &= ~CPSR_F;
8934             }
8935         }
8936     }
8937
8938     env->daif &= ~(CPSR_AIF & mask);
8939     env->daif |= val & CPSR_AIF & mask;
8940
8941     if (write_type != CPSRWriteRaw &&
8942         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8943         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8944             /* Note that we can only get here in USR mode if this is a
8945              * gdb stub write; for this case we follow the architectural
8946              * behaviour for guest writes in USR mode of ignoring an attempt
8947              * to switch mode. (Those are caught by translate.c for writes
8948              * triggered by guest instructions.)
8949              */
8950             mask &= ~CPSR_M;
8951         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
8952             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8953              * v7, and has defined behaviour in v8:
8954              *  + leave CPSR.M untouched
8955              *  + allow changes to the other CPSR fields
8956              *  + set PSTATE.IL
8957              * For user changes via the GDB stub, we don't set PSTATE.IL,
8958              * as this would be unnecessarily harsh for a user error.
8959              */
8960             mask &= ~CPSR_M;
8961             if (write_type != CPSRWriteByGDBStub &&
8962                 arm_feature(env, ARM_FEATURE_V8)) {
8963                 mask |= CPSR_IL;
8964                 val |= CPSR_IL;
8965             }
8966             qemu_log_mask(LOG_GUEST_ERROR,
8967                           "Illegal AArch32 mode switch attempt from %s to %s\n",
8968                           aarch32_mode_name(env->uncached_cpsr),
8969                           aarch32_mode_name(val));
8970         } else {
8971             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8972                           write_type == CPSRWriteExceptionReturn ?
8973                           "Exception return from AArch32" :
8974                           "AArch32 mode switch from",
8975                           aarch32_mode_name(env->uncached_cpsr),
8976                           aarch32_mode_name(val), env->regs[15]);
8977             switch_mode(env, val & CPSR_M);
8978         }
8979     }
8980     mask &= ~CACHED_CPSR_BITS;
8981     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8982     if (rebuild_hflags) {
8983         arm_rebuild_hflags(env);
8984     }
8985 }
8986
8987 /* Sign/zero extend */
8988 uint32_t HELPER(sxtb16)(uint32_t x)
8989 {
8990     uint32_t res;
8991     res = (uint16_t)(int8_t)x;
8992     res |= (uint32_t)(int8_t)(x >> 16) << 16;
8993     return res;
8994 }
8995
8996 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
8997 {
8998     /*
8999      * Take a division-by-zero exception if necessary; otherwise return
9000      * to get the usual non-trapping division behaviour (result of 0)
9001      */
9002     if (arm_feature(env, ARM_FEATURE_M)
9003         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9004         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9005     }
9006 }
9007
9008 uint32_t HELPER(uxtb16)(uint32_t x)
9009 {
9010     uint32_t res;
9011     res = (uint16_t)(uint8_t)x;
9012     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9013     return res;
9014 }
9015
9016 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9017 {
9018     if (den == 0) {
9019         handle_possible_div0_trap(env, GETPC());
9020         return 0;
9021     }
9022     if (num == INT_MIN && den == -1) {
9023         return INT_MIN;
9024     }
9025     return num / den;
9026 }
9027
9028 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9029 {
9030     if (den == 0) {
9031         handle_possible_div0_trap(env, GETPC());
9032         return 0;
9033     }
9034     return num / den;
9035 }
9036
9037 uint32_t HELPER(rbit)(uint32_t x)
9038 {
9039     return revbit32(x);
9040 }
9041
9042 #ifdef CONFIG_USER_ONLY
9043
9044 static void switch_mode(CPUARMState *env, int mode)
9045 {
9046     ARMCPU *cpu = env_archcpu(env);
9047
9048     if (mode != ARM_CPU_MODE_USR) {
9049         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9050     }
9051 }
9052
9053 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9054                                  uint32_t cur_el, bool secure)
9055 {
9056     return 1;
9057 }
9058
9059 void aarch64_sync_64_to_32(CPUARMState *env)
9060 {
9061     g_assert_not_reached();
9062 }
9063
9064 #else
9065
9066 static void switch_mode(CPUARMState *env, int mode)
9067 {
9068     int old_mode;
9069     int i;
9070
9071     old_mode = env->uncached_cpsr & CPSR_M;
9072     if (mode == old_mode)
9073         return;
9074
9075     if (old_mode == ARM_CPU_MODE_FIQ) {
9076         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9077         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9078     } else if (mode == ARM_CPU_MODE_FIQ) {
9079         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9080         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9081     }
9082
9083     i = bank_number(old_mode);
9084     env->banked_r13[i] = env->regs[13];
9085     env->banked_spsr[i] = env->spsr;
9086
9087     i = bank_number(mode);
9088     env->regs[13] = env->banked_r13[i];
9089     env->spsr = env->banked_spsr[i];
9090
9091     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9092     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9093 }
9094
9095 /* Physical Interrupt Target EL Lookup Table
9096  *
9097  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9098  *
9099  * The below multi-dimensional table is used for looking up the target
9100  * exception level given numerous condition criteria.  Specifically, the
9101  * target EL is based on SCR and HCR routing controls as well as the
9102  * currently executing EL and secure state.
9103  *
9104  *    Dimensions:
9105  *    target_el_table[2][2][2][2][2][4]
9106  *                    |  |  |  |  |  +--- Current EL
9107  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9108  *                    |  |  |  +--------- HCR mask override
9109  *                    |  |  +------------ SCR exec state control
9110  *                    |  +--------------- SCR mask override
9111  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9112  *
9113  *    The table values are as such:
9114  *    0-3 = EL0-EL3
9115  *     -1 = Cannot occur
9116  *
9117  * The ARM ARM target EL table includes entries indicating that an "exception
9118  * is not taken".  The two cases where this is applicable are:
9119  *    1) An exception is taken from EL3 but the SCR does not have the exception
9120  *    routed to EL3.
9121  *    2) An exception is taken from EL2 but the HCR does not have the exception
9122  *    routed to EL2.
9123  * In these two cases, the below table contain a target of EL1.  This value is
9124  * returned as it is expected that the consumer of the table data will check
9125  * for "target EL >= current EL" to ensure the exception is not taken.
9126  *
9127  *            SCR     HCR
9128  *         64  EA     AMO                 From
9129  *        BIT IRQ     IMO      Non-secure         Secure
9130  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9131  */
9132 static const int8_t target_el_table[2][2][2][2][2][4] = {
9133     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9134        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9135       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9136        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9137      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9138        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9139       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9140        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9141     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9142        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9143       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9144        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9145      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9146        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9147       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9148        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9149 };
9150
9151 /*
9152  * Determine the target EL for physical exceptions
9153  */
9154 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9155                                  uint32_t cur_el, bool secure)
9156 {
9157     CPUARMState *env = cs->env_ptr;
9158     bool rw;
9159     bool scr;
9160     bool hcr;
9161     int target_el;
9162     /* Is the highest EL AArch64? */
9163     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9164     uint64_t hcr_el2;
9165
9166     if (arm_feature(env, ARM_FEATURE_EL3)) {
9167         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9168     } else {
9169         /* Either EL2 is the highest EL (and so the EL2 register width
9170          * is given by is64); or there is no EL2 or EL3, in which case
9171          * the value of 'rw' does not affect the table lookup anyway.
9172          */
9173         rw = is64;
9174     }
9175
9176     hcr_el2 = arm_hcr_el2_eff(env);
9177     switch (excp_idx) {
9178     case EXCP_IRQ:
9179         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9180         hcr = hcr_el2 & HCR_IMO;
9181         break;
9182     case EXCP_FIQ:
9183         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9184         hcr = hcr_el2 & HCR_FMO;
9185         break;
9186     default:
9187         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9188         hcr = hcr_el2 & HCR_AMO;
9189         break;
9190     };
9191
9192     /*
9193      * For these purposes, TGE and AMO/IMO/FMO both force the
9194      * interrupt to EL2.  Fold TGE into the bit extracted above.
9195      */
9196     hcr |= (hcr_el2 & HCR_TGE) != 0;
9197
9198     /* Perform a table-lookup for the target EL given the current state */
9199     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9200
9201     assert(target_el > 0);
9202
9203     return target_el;
9204 }
9205
9206 void arm_log_exception(CPUState *cs)
9207 {
9208     int idx = cs->exception_index;
9209
9210     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9211         const char *exc = NULL;
9212         static const char * const excnames[] = {
9213             [EXCP_UDEF] = "Undefined Instruction",
9214             [EXCP_SWI] = "SVC",
9215             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9216             [EXCP_DATA_ABORT] = "Data Abort",
9217             [EXCP_IRQ] = "IRQ",
9218             [EXCP_FIQ] = "FIQ",
9219             [EXCP_BKPT] = "Breakpoint",
9220             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9221             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9222             [EXCP_HVC] = "Hypervisor Call",
9223             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9224             [EXCP_SMC] = "Secure Monitor Call",
9225             [EXCP_VIRQ] = "Virtual IRQ",
9226             [EXCP_VFIQ] = "Virtual FIQ",
9227             [EXCP_SEMIHOST] = "Semihosting call",
9228             [EXCP_NOCP] = "v7M NOCP UsageFault",
9229             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9230             [EXCP_STKOF] = "v8M STKOF UsageFault",
9231             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9232             [EXCP_LSERR] = "v8M LSERR UsageFault",
9233             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9234             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9235             [EXCP_VSERR] = "Virtual SERR",
9236         };
9237
9238         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9239             exc = excnames[idx];
9240         }
9241         if (!exc) {
9242             exc = "unknown";
9243         }
9244         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9245                       idx, exc, cs->cpu_index);
9246     }
9247 }
9248
9249 /*
9250  * Function used to synchronize QEMU's AArch64 register set with AArch32
9251  * register set.  This is necessary when switching between AArch32 and AArch64
9252  * execution state.
9253  */
9254 void aarch64_sync_32_to_64(CPUARMState *env)
9255 {
9256     int i;
9257     uint32_t mode = env->uncached_cpsr & CPSR_M;
9258
9259     /* We can blanket copy R[0:7] to X[0:7] */
9260     for (i = 0; i < 8; i++) {
9261         env->xregs[i] = env->regs[i];
9262     }
9263
9264     /*
9265      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9266      * Otherwise, they come from the banked user regs.
9267      */
9268     if (mode == ARM_CPU_MODE_FIQ) {
9269         for (i = 8; i < 13; i++) {
9270             env->xregs[i] = env->usr_regs[i - 8];
9271         }
9272     } else {
9273         for (i = 8; i < 13; i++) {
9274             env->xregs[i] = env->regs[i];
9275         }
9276     }
9277
9278     /*
9279      * Registers x13-x23 are the various mode SP and FP registers. Registers
9280      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9281      * from the mode banked register.
9282      */
9283     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9284         env->xregs[13] = env->regs[13];
9285         env->xregs[14] = env->regs[14];
9286     } else {
9287         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9288         /* HYP is an exception in that it is copied from r14 */
9289         if (mode == ARM_CPU_MODE_HYP) {
9290             env->xregs[14] = env->regs[14];
9291         } else {
9292             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9293         }
9294     }
9295
9296     if (mode == ARM_CPU_MODE_HYP) {
9297         env->xregs[15] = env->regs[13];
9298     } else {
9299         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9300     }
9301
9302     if (mode == ARM_CPU_MODE_IRQ) {
9303         env->xregs[16] = env->regs[14];
9304         env->xregs[17] = env->regs[13];
9305     } else {
9306         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9307         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9308     }
9309
9310     if (mode == ARM_CPU_MODE_SVC) {
9311         env->xregs[18] = env->regs[14];
9312         env->xregs[19] = env->regs[13];
9313     } else {
9314         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9315         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9316     }
9317
9318     if (mode == ARM_CPU_MODE_ABT) {
9319         env->xregs[20] = env->regs[14];
9320         env->xregs[21] = env->regs[13];
9321     } else {
9322         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9323         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9324     }
9325
9326     if (mode == ARM_CPU_MODE_UND) {
9327         env->xregs[22] = env->regs[14];
9328         env->xregs[23] = env->regs[13];
9329     } else {
9330         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9331         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9332     }
9333
9334     /*
9335      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9336      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9337      * FIQ bank for r8-r14.
9338      */
9339     if (mode == ARM_CPU_MODE_FIQ) {
9340         for (i = 24; i < 31; i++) {
9341             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9342         }
9343     } else {
9344         for (i = 24; i < 29; i++) {
9345             env->xregs[i] = env->fiq_regs[i - 24];
9346         }
9347         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9348         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9349     }
9350
9351     env->pc = env->regs[15];
9352 }
9353
9354 /*
9355  * Function used to synchronize QEMU's AArch32 register set with AArch64
9356  * register set.  This is necessary when switching between AArch32 and AArch64
9357  * execution state.
9358  */
9359 void aarch64_sync_64_to_32(CPUARMState *env)
9360 {
9361     int i;
9362     uint32_t mode = env->uncached_cpsr & CPSR_M;
9363
9364     /* We can blanket copy X[0:7] to R[0:7] */
9365     for (i = 0; i < 8; i++) {
9366         env->regs[i] = env->xregs[i];
9367     }
9368
9369     /*
9370      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9371      * Otherwise, we copy x8-x12 into the banked user regs.
9372      */
9373     if (mode == ARM_CPU_MODE_FIQ) {
9374         for (i = 8; i < 13; i++) {
9375             env->usr_regs[i - 8] = env->xregs[i];
9376         }
9377     } else {
9378         for (i = 8; i < 13; i++) {
9379             env->regs[i] = env->xregs[i];
9380         }
9381     }
9382
9383     /*
9384      * Registers r13 & r14 depend on the current mode.
9385      * If we are in a given mode, we copy the corresponding x registers to r13
9386      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9387      * for the mode.
9388      */
9389     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9390         env->regs[13] = env->xregs[13];
9391         env->regs[14] = env->xregs[14];
9392     } else {
9393         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9394
9395         /*
9396          * HYP is an exception in that it does not have its own banked r14 but
9397          * shares the USR r14
9398          */
9399         if (mode == ARM_CPU_MODE_HYP) {
9400             env->regs[14] = env->xregs[14];
9401         } else {
9402             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9403         }
9404     }
9405
9406     if (mode == ARM_CPU_MODE_HYP) {
9407         env->regs[13] = env->xregs[15];
9408     } else {
9409         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9410     }
9411
9412     if (mode == ARM_CPU_MODE_IRQ) {
9413         env->regs[14] = env->xregs[16];
9414         env->regs[13] = env->xregs[17];
9415     } else {
9416         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9417         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9418     }
9419
9420     if (mode == ARM_CPU_MODE_SVC) {
9421         env->regs[14] = env->xregs[18];
9422         env->regs[13] = env->xregs[19];
9423     } else {
9424         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9425         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9426     }
9427
9428     if (mode == ARM_CPU_MODE_ABT) {
9429         env->regs[14] = env->xregs[20];
9430         env->regs[13] = env->xregs[21];
9431     } else {
9432         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9433         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9434     }
9435
9436     if (mode == ARM_CPU_MODE_UND) {
9437         env->regs[14] = env->xregs[22];
9438         env->regs[13] = env->xregs[23];
9439     } else {
9440         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9441         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9442     }
9443
9444     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9445      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9446      * FIQ bank for r8-r14.
9447      */
9448     if (mode == ARM_CPU_MODE_FIQ) {
9449         for (i = 24; i < 31; i++) {
9450             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9451         }
9452     } else {
9453         for (i = 24; i < 29; i++) {
9454             env->fiq_regs[i - 24] = env->xregs[i];
9455         }
9456         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9457         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9458     }
9459
9460     env->regs[15] = env->pc;
9461 }
9462
9463 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9464                                    uint32_t mask, uint32_t offset,
9465                                    uint32_t newpc)
9466 {
9467     int new_el;
9468
9469     /* Change the CPU state so as to actually take the exception. */
9470     switch_mode(env, new_mode);
9471
9472     /*
9473      * For exceptions taken to AArch32 we must clear the SS bit in both
9474      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9475      */
9476     env->pstate &= ~PSTATE_SS;
9477     env->spsr = cpsr_read(env);
9478     /* Clear IT bits.  */
9479     env->condexec_bits = 0;
9480     /* Switch to the new mode, and to the correct instruction set.  */
9481     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9482
9483     /* This must be after mode switching. */
9484     new_el = arm_current_el(env);
9485
9486     /* Set new mode endianness */
9487     env->uncached_cpsr &= ~CPSR_E;
9488     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9489         env->uncached_cpsr |= CPSR_E;
9490     }
9491     /* J and IL must always be cleared for exception entry */
9492     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9493     env->daif |= mask;
9494
9495     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9496         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9497             env->uncached_cpsr |= CPSR_SSBS;
9498         } else {
9499             env->uncached_cpsr &= ~CPSR_SSBS;
9500         }
9501     }
9502
9503     if (new_mode == ARM_CPU_MODE_HYP) {
9504         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9505         env->elr_el[2] = env->regs[15];
9506     } else {
9507         /* CPSR.PAN is normally preserved preserved unless...  */
9508         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9509             switch (new_el) {
9510             case 3:
9511                 if (!arm_is_secure_below_el3(env)) {
9512                     /* ... the target is EL3, from non-secure state.  */
9513                     env->uncached_cpsr &= ~CPSR_PAN;
9514                     break;
9515                 }
9516                 /* ... the target is EL3, from secure state ... */
9517                 /* fall through */
9518             case 1:
9519                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9520                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9521                     env->uncached_cpsr |= CPSR_PAN;
9522                 }
9523                 break;
9524             }
9525         }
9526         /*
9527          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9528          * and we should just guard the thumb mode on V4
9529          */
9530         if (arm_feature(env, ARM_FEATURE_V4T)) {
9531             env->thumb =
9532                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9533         }
9534         env->regs[14] = env->regs[15] + offset;
9535     }
9536     env->regs[15] = newpc;
9537     arm_rebuild_hflags(env);
9538 }
9539
9540 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9541 {
9542     /*
9543      * Handle exception entry to Hyp mode; this is sufficiently
9544      * different to entry to other AArch32 modes that we handle it
9545      * separately here.
9546      *
9547      * The vector table entry used is always the 0x14 Hyp mode entry point,
9548      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9549      * The offset applied to the preferred return address is always zero
9550      * (see DDI0487C.a section G1.12.3).
9551      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9552      */
9553     uint32_t addr, mask;
9554     ARMCPU *cpu = ARM_CPU(cs);
9555     CPUARMState *env = &cpu->env;
9556
9557     switch (cs->exception_index) {
9558     case EXCP_UDEF:
9559         addr = 0x04;
9560         break;
9561     case EXCP_SWI:
9562         addr = 0x08;
9563         break;
9564     case EXCP_BKPT:
9565         /* Fall through to prefetch abort.  */
9566     case EXCP_PREFETCH_ABORT:
9567         env->cp15.ifar_s = env->exception.vaddress;
9568         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9569                       (uint32_t)env->exception.vaddress);
9570         addr = 0x0c;
9571         break;
9572     case EXCP_DATA_ABORT:
9573         env->cp15.dfar_s = env->exception.vaddress;
9574         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9575                       (uint32_t)env->exception.vaddress);
9576         addr = 0x10;
9577         break;
9578     case EXCP_IRQ:
9579         addr = 0x18;
9580         break;
9581     case EXCP_FIQ:
9582         addr = 0x1c;
9583         break;
9584     case EXCP_HVC:
9585         addr = 0x08;
9586         break;
9587     case EXCP_HYP_TRAP:
9588         addr = 0x14;
9589         break;
9590     default:
9591         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9592     }
9593
9594     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9595         if (!arm_feature(env, ARM_FEATURE_V8)) {
9596             /*
9597              * QEMU syndrome values are v8-style. v7 has the IL bit
9598              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9599              * If this is a v7 CPU, squash the IL bit in those cases.
9600              */
9601             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9602                 (cs->exception_index == EXCP_DATA_ABORT &&
9603                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9604                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9605                 env->exception.syndrome &= ~ARM_EL_IL;
9606             }
9607         }
9608         env->cp15.esr_el[2] = env->exception.syndrome;
9609     }
9610
9611     if (arm_current_el(env) != 2 && addr < 0x14) {
9612         addr = 0x14;
9613     }
9614
9615     mask = 0;
9616     if (!(env->cp15.scr_el3 & SCR_EA)) {
9617         mask |= CPSR_A;
9618     }
9619     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9620         mask |= CPSR_I;
9621     }
9622     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9623         mask |= CPSR_F;
9624     }
9625
9626     addr += env->cp15.hvbar;
9627
9628     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9629 }
9630
9631 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9632 {
9633     ARMCPU *cpu = ARM_CPU(cs);
9634     CPUARMState *env = &cpu->env;
9635     uint32_t addr;
9636     uint32_t mask;
9637     int new_mode;
9638     uint32_t offset;
9639     uint32_t moe;
9640
9641     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9642     switch (syn_get_ec(env->exception.syndrome)) {
9643     case EC_BREAKPOINT:
9644     case EC_BREAKPOINT_SAME_EL:
9645         moe = 1;
9646         break;
9647     case EC_WATCHPOINT:
9648     case EC_WATCHPOINT_SAME_EL:
9649         moe = 10;
9650         break;
9651     case EC_AA32_BKPT:
9652         moe = 3;
9653         break;
9654     case EC_VECTORCATCH:
9655         moe = 5;
9656         break;
9657     default:
9658         moe = 0;
9659         break;
9660     }
9661
9662     if (moe) {
9663         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9664     }
9665
9666     if (env->exception.target_el == 2) {
9667         arm_cpu_do_interrupt_aarch32_hyp(cs);
9668         return;
9669     }
9670
9671     switch (cs->exception_index) {
9672     case EXCP_UDEF:
9673         new_mode = ARM_CPU_MODE_UND;
9674         addr = 0x04;
9675         mask = CPSR_I;
9676         if (env->thumb)
9677             offset = 2;
9678         else
9679             offset = 4;
9680         break;
9681     case EXCP_SWI:
9682         new_mode = ARM_CPU_MODE_SVC;
9683         addr = 0x08;
9684         mask = CPSR_I;
9685         /* The PC already points to the next instruction.  */
9686         offset = 0;
9687         break;
9688     case EXCP_BKPT:
9689         /* Fall through to prefetch abort.  */
9690     case EXCP_PREFETCH_ABORT:
9691         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9692         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9693         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9694                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9695         new_mode = ARM_CPU_MODE_ABT;
9696         addr = 0x0c;
9697         mask = CPSR_A | CPSR_I;
9698         offset = 4;
9699         break;
9700     case EXCP_DATA_ABORT:
9701         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9702         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9703         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9704                       env->exception.fsr,
9705                       (uint32_t)env->exception.vaddress);
9706         new_mode = ARM_CPU_MODE_ABT;
9707         addr = 0x10;
9708         mask = CPSR_A | CPSR_I;
9709         offset = 8;
9710         break;
9711     case EXCP_IRQ:
9712         new_mode = ARM_CPU_MODE_IRQ;
9713         addr = 0x18;
9714         /* Disable IRQ and imprecise data aborts.  */
9715         mask = CPSR_A | CPSR_I;
9716         offset = 4;
9717         if (env->cp15.scr_el3 & SCR_IRQ) {
9718             /* IRQ routed to monitor mode */
9719             new_mode = ARM_CPU_MODE_MON;
9720             mask |= CPSR_F;
9721         }
9722         break;
9723     case EXCP_FIQ:
9724         new_mode = ARM_CPU_MODE_FIQ;
9725         addr = 0x1c;
9726         /* Disable FIQ, IRQ and imprecise data aborts.  */
9727         mask = CPSR_A | CPSR_I | CPSR_F;
9728         if (env->cp15.scr_el3 & SCR_FIQ) {
9729             /* FIQ routed to monitor mode */
9730             new_mode = ARM_CPU_MODE_MON;
9731         }
9732         offset = 4;
9733         break;
9734     case EXCP_VIRQ:
9735         new_mode = ARM_CPU_MODE_IRQ;
9736         addr = 0x18;
9737         /* Disable IRQ and imprecise data aborts.  */
9738         mask = CPSR_A | CPSR_I;
9739         offset = 4;
9740         break;
9741     case EXCP_VFIQ:
9742         new_mode = ARM_CPU_MODE_FIQ;
9743         addr = 0x1c;
9744         /* Disable FIQ, IRQ and imprecise data aborts.  */
9745         mask = CPSR_A | CPSR_I | CPSR_F;
9746         offset = 4;
9747         break;
9748     case EXCP_VSERR:
9749         {
9750             /*
9751              * Note that this is reported as a data abort, but the DFAR
9752              * has an UNKNOWN value.  Construct the SError syndrome from
9753              * AET and ExT fields.
9754              */
9755             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9756
9757             if (extended_addresses_enabled(env)) {
9758                 env->exception.fsr = arm_fi_to_lfsc(&fi);
9759             } else {
9760                 env->exception.fsr = arm_fi_to_sfsc(&fi);
9761             }
9762             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9763             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9764             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9765                           env->exception.fsr);
9766
9767             new_mode = ARM_CPU_MODE_ABT;
9768             addr = 0x10;
9769             mask = CPSR_A | CPSR_I;
9770             offset = 8;
9771         }
9772         break;
9773     case EXCP_SMC:
9774         new_mode = ARM_CPU_MODE_MON;
9775         addr = 0x08;
9776         mask = CPSR_A | CPSR_I | CPSR_F;
9777         offset = 0;
9778         break;
9779     default:
9780         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9781         return; /* Never happens.  Keep compiler happy.  */
9782     }
9783
9784     if (new_mode == ARM_CPU_MODE_MON) {
9785         addr += env->cp15.mvbar;
9786     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9787         /* High vectors. When enabled, base address cannot be remapped. */
9788         addr += 0xffff0000;
9789     } else {
9790         /* ARM v7 architectures provide a vector base address register to remap
9791          * the interrupt vector table.
9792          * This register is only followed in non-monitor mode, and is banked.
9793          * Note: only bits 31:5 are valid.
9794          */
9795         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9796     }
9797
9798     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9799         env->cp15.scr_el3 &= ~SCR_NS;
9800     }
9801
9802     take_aarch32_exception(env, new_mode, mask, offset, addr);
9803 }
9804
9805 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9806 {
9807     /*
9808      * Return the register number of the AArch64 view of the AArch32
9809      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9810      * be that of the AArch32 mode the exception came from.
9811      */
9812     int mode = env->uncached_cpsr & CPSR_M;
9813
9814     switch (aarch32_reg) {
9815     case 0 ... 7:
9816         return aarch32_reg;
9817     case 8 ... 12:
9818         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9819     case 13:
9820         switch (mode) {
9821         case ARM_CPU_MODE_USR:
9822         case ARM_CPU_MODE_SYS:
9823             return 13;
9824         case ARM_CPU_MODE_HYP:
9825             return 15;
9826         case ARM_CPU_MODE_IRQ:
9827             return 17;
9828         case ARM_CPU_MODE_SVC:
9829             return 19;
9830         case ARM_CPU_MODE_ABT:
9831             return 21;
9832         case ARM_CPU_MODE_UND:
9833             return 23;
9834         case ARM_CPU_MODE_FIQ:
9835             return 29;
9836         default:
9837             g_assert_not_reached();
9838         }
9839     case 14:
9840         switch (mode) {
9841         case ARM_CPU_MODE_USR:
9842         case ARM_CPU_MODE_SYS:
9843         case ARM_CPU_MODE_HYP:
9844             return 14;
9845         case ARM_CPU_MODE_IRQ:
9846             return 16;
9847         case ARM_CPU_MODE_SVC:
9848             return 18;
9849         case ARM_CPU_MODE_ABT:
9850             return 20;
9851         case ARM_CPU_MODE_UND:
9852             return 22;
9853         case ARM_CPU_MODE_FIQ:
9854             return 30;
9855         default:
9856             g_assert_not_reached();
9857         }
9858     case 15:
9859         return 31;
9860     default:
9861         g_assert_not_reached();
9862     }
9863 }
9864
9865 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9866 {
9867     uint32_t ret = cpsr_read(env);
9868
9869     /* Move DIT to the correct location for SPSR_ELx */
9870     if (ret & CPSR_DIT) {
9871         ret &= ~CPSR_DIT;
9872         ret |= PSTATE_DIT;
9873     }
9874     /* Merge PSTATE.SS into SPSR_ELx */
9875     ret |= env->pstate & PSTATE_SS;
9876
9877     return ret;
9878 }
9879
9880 static bool syndrome_is_sync_extabt(uint32_t syndrome)
9881 {
9882     /* Return true if this syndrome value is a synchronous external abort */
9883     switch (syn_get_ec(syndrome)) {
9884     case EC_INSNABORT:
9885     case EC_INSNABORT_SAME_EL:
9886     case EC_DATAABORT:
9887     case EC_DATAABORT_SAME_EL:
9888         /* Look at fault status code for all the synchronous ext abort cases */
9889         switch (syndrome & 0x3f) {
9890         case 0x10:
9891         case 0x13:
9892         case 0x14:
9893         case 0x15:
9894         case 0x16:
9895         case 0x17:
9896             return true;
9897         default:
9898             return false;
9899         }
9900     default:
9901         return false;
9902     }
9903 }
9904
9905 /* Handle exception entry to a target EL which is using AArch64 */
9906 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9907 {
9908     ARMCPU *cpu = ARM_CPU(cs);
9909     CPUARMState *env = &cpu->env;
9910     unsigned int new_el = env->exception.target_el;
9911     target_ulong addr = env->cp15.vbar_el[new_el];
9912     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9913     unsigned int old_mode;
9914     unsigned int cur_el = arm_current_el(env);
9915     int rt;
9916
9917     /*
9918      * Note that new_el can never be 0.  If cur_el is 0, then
9919      * el0_a64 is is_a64(), else el0_a64 is ignored.
9920      */
9921     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9922
9923     if (cur_el < new_el) {
9924         /* Entry vector offset depends on whether the implemented EL
9925          * immediately lower than the target level is using AArch32 or AArch64
9926          */
9927         bool is_aa64;
9928         uint64_t hcr;
9929
9930         switch (new_el) {
9931         case 3:
9932             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9933             break;
9934         case 2:
9935             hcr = arm_hcr_el2_eff(env);
9936             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9937                 is_aa64 = (hcr & HCR_RW) != 0;
9938                 break;
9939             }
9940             /* fall through */
9941         case 1:
9942             is_aa64 = is_a64(env);
9943             break;
9944         default:
9945             g_assert_not_reached();
9946         }
9947
9948         if (is_aa64) {
9949             addr += 0x400;
9950         } else {
9951             addr += 0x600;
9952         }
9953     } else if (pstate_read(env) & PSTATE_SP) {
9954         addr += 0x200;
9955     }
9956
9957     switch (cs->exception_index) {
9958     case EXCP_PREFETCH_ABORT:
9959     case EXCP_DATA_ABORT:
9960         /*
9961          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
9962          * to be taken to the SError vector entrypoint.
9963          */
9964         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
9965             syndrome_is_sync_extabt(env->exception.syndrome)) {
9966             addr += 0x180;
9967         }
9968         env->cp15.far_el[new_el] = env->exception.vaddress;
9969         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9970                       env->cp15.far_el[new_el]);
9971         /* fall through */
9972     case EXCP_BKPT:
9973     case EXCP_UDEF:
9974     case EXCP_SWI:
9975     case EXCP_HVC:
9976     case EXCP_HYP_TRAP:
9977     case EXCP_SMC:
9978         switch (syn_get_ec(env->exception.syndrome)) {
9979         case EC_ADVSIMDFPACCESSTRAP:
9980             /*
9981              * QEMU internal FP/SIMD syndromes from AArch32 include the
9982              * TA and coproc fields which are only exposed if the exception
9983              * is taken to AArch32 Hyp mode. Mask them out to get a valid
9984              * AArch64 format syndrome.
9985              */
9986             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9987             break;
9988         case EC_CP14RTTRAP:
9989         case EC_CP15RTTRAP:
9990         case EC_CP14DTTRAP:
9991             /*
9992              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
9993              * the raw register field from the insn; when taking this to
9994              * AArch64 we must convert it to the AArch64 view of the register
9995              * number. Notice that we read a 4-bit AArch32 register number and
9996              * write back a 5-bit AArch64 one.
9997              */
9998             rt = extract32(env->exception.syndrome, 5, 4);
9999             rt = aarch64_regnum(env, rt);
10000             env->exception.syndrome = deposit32(env->exception.syndrome,
10001                                                 5, 5, rt);
10002             break;
10003         case EC_CP15RRTTRAP:
10004         case EC_CP14RRTTRAP:
10005             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10006             rt = extract32(env->exception.syndrome, 5, 4);
10007             rt = aarch64_regnum(env, rt);
10008             env->exception.syndrome = deposit32(env->exception.syndrome,
10009                                                 5, 5, rt);
10010             rt = extract32(env->exception.syndrome, 10, 4);
10011             rt = aarch64_regnum(env, rt);
10012             env->exception.syndrome = deposit32(env->exception.syndrome,
10013                                                 10, 5, rt);
10014             break;
10015         }
10016         env->cp15.esr_el[new_el] = env->exception.syndrome;
10017         break;
10018     case EXCP_IRQ:
10019     case EXCP_VIRQ:
10020         addr += 0x80;
10021         break;
10022     case EXCP_FIQ:
10023     case EXCP_VFIQ:
10024         addr += 0x100;
10025         break;
10026     case EXCP_VSERR:
10027         addr += 0x180;
10028         /* Construct the SError syndrome from IDS and ISS fields. */
10029         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10030         env->cp15.esr_el[new_el] = env->exception.syndrome;
10031         break;
10032     default:
10033         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10034     }
10035
10036     if (is_a64(env)) {
10037         old_mode = pstate_read(env);
10038         aarch64_save_sp(env, arm_current_el(env));
10039         env->elr_el[new_el] = env->pc;
10040     } else {
10041         old_mode = cpsr_read_for_spsr_elx(env);
10042         env->elr_el[new_el] = env->regs[15];
10043
10044         aarch64_sync_32_to_64(env);
10045
10046         env->condexec_bits = 0;
10047     }
10048     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10049
10050     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10051                   env->elr_el[new_el]);
10052
10053     if (cpu_isar_feature(aa64_pan, cpu)) {
10054         /* The value of PSTATE.PAN is normally preserved, except when ... */
10055         new_mode |= old_mode & PSTATE_PAN;
10056         switch (new_el) {
10057         case 2:
10058             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10059             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10060                 != (HCR_E2H | HCR_TGE)) {
10061                 break;
10062             }
10063             /* fall through */
10064         case 1:
10065             /* ... the target is EL1 ... */
10066             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10067             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10068                 new_mode |= PSTATE_PAN;
10069             }
10070             break;
10071         }
10072     }
10073     if (cpu_isar_feature(aa64_mte, cpu)) {
10074         new_mode |= PSTATE_TCO;
10075     }
10076
10077     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10078         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10079             new_mode |= PSTATE_SSBS;
10080         } else {
10081             new_mode &= ~PSTATE_SSBS;
10082         }
10083     }
10084
10085     pstate_write(env, PSTATE_DAIF | new_mode);
10086     env->aarch64 = true;
10087     aarch64_restore_sp(env, new_el);
10088     helper_rebuild_hflags_a64(env, new_el);
10089
10090     env->pc = addr;
10091
10092     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10093                   new_el, env->pc, pstate_read(env));
10094 }
10095
10096 /*
10097  * Do semihosting call and set the appropriate return value. All the
10098  * permission and validity checks have been done at translate time.
10099  *
10100  * We only see semihosting exceptions in TCG only as they are not
10101  * trapped to the hypervisor in KVM.
10102  */
10103 #ifdef CONFIG_TCG
10104 static void handle_semihosting(CPUState *cs)
10105 {
10106     ARMCPU *cpu = ARM_CPU(cs);
10107     CPUARMState *env = &cpu->env;
10108
10109     if (is_a64(env)) {
10110         qemu_log_mask(CPU_LOG_INT,
10111                       "...handling as semihosting call 0x%" PRIx64 "\n",
10112                       env->xregs[0]);
10113         do_common_semihosting(cs);
10114         env->pc += 4;
10115     } else {
10116         qemu_log_mask(CPU_LOG_INT,
10117                       "...handling as semihosting call 0x%x\n",
10118                       env->regs[0]);
10119         do_common_semihosting(cs);
10120         env->regs[15] += env->thumb ? 2 : 4;
10121     }
10122 }
10123 #endif
10124
10125 /* Handle a CPU exception for A and R profile CPUs.
10126  * Do any appropriate logging, handle PSCI calls, and then hand off
10127  * to the AArch64-entry or AArch32-entry function depending on the
10128  * target exception level's register width.
10129  *
10130  * Note: this is used for both TCG (as the do_interrupt tcg op),
10131  *       and KVM to re-inject guest debug exceptions, and to
10132  *       inject a Synchronous-External-Abort.
10133  */
10134 void arm_cpu_do_interrupt(CPUState *cs)
10135 {
10136     ARMCPU *cpu = ARM_CPU(cs);
10137     CPUARMState *env = &cpu->env;
10138     unsigned int new_el = env->exception.target_el;
10139
10140     assert(!arm_feature(env, ARM_FEATURE_M));
10141
10142     arm_log_exception(cs);
10143     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10144                   new_el);
10145     if (qemu_loglevel_mask(CPU_LOG_INT)
10146         && !excp_is_internal(cs->exception_index)) {
10147         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10148                       syn_get_ec(env->exception.syndrome),
10149                       env->exception.syndrome);
10150     }
10151
10152     if (arm_is_psci_call(cpu, cs->exception_index)) {
10153         arm_handle_psci_call(cpu);
10154         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10155         return;
10156     }
10157
10158     /*
10159      * Semihosting semantics depend on the register width of the code
10160      * that caused the exception, not the target exception level, so
10161      * must be handled here.
10162      */
10163 #ifdef CONFIG_TCG
10164     if (cs->exception_index == EXCP_SEMIHOST) {
10165         handle_semihosting(cs);
10166         return;
10167     }
10168 #endif
10169
10170     /* Hooks may change global state so BQL should be held, also the
10171      * BQL needs to be held for any modification of
10172      * cs->interrupt_request.
10173      */
10174     g_assert(qemu_mutex_iothread_locked());
10175
10176     arm_call_pre_el_change_hook(cpu);
10177
10178     assert(!excp_is_internal(cs->exception_index));
10179     if (arm_el_is_aa64(env, new_el)) {
10180         arm_cpu_do_interrupt_aarch64(cs);
10181     } else {
10182         arm_cpu_do_interrupt_aarch32(cs);
10183     }
10184
10185     arm_call_el_change_hook(cpu);
10186
10187     if (!kvm_enabled()) {
10188         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10189     }
10190 }
10191 #endif /* !CONFIG_USER_ONLY */
10192
10193 uint64_t arm_sctlr(CPUARMState *env, int el)
10194 {
10195     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10196     if (el == 0) {
10197         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10198         el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10199              ? 2 : 1;
10200     }
10201     return env->cp15.sctlr_el[el];
10202 }
10203
10204 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10205 {
10206     if (regime_has_2_ranges(mmu_idx)) {
10207         return extract64(tcr, 37, 2);
10208     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10209         return 0; /* VTCR_EL2 */
10210     } else {
10211         /* Replicate the single TBI bit so we always have 2 bits.  */
10212         return extract32(tcr, 20, 1) * 3;
10213     }
10214 }
10215
10216 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10217 {
10218     if (regime_has_2_ranges(mmu_idx)) {
10219         return extract64(tcr, 51, 2);
10220     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10221         return 0; /* VTCR_EL2 */
10222     } else {
10223         /* Replicate the single TBID bit so we always have 2 bits.  */
10224         return extract32(tcr, 29, 1) * 3;
10225     }
10226 }
10227
10228 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10229 {
10230     if (regime_has_2_ranges(mmu_idx)) {
10231         return extract64(tcr, 57, 2);
10232     } else {
10233         /* Replicate the single TCMA bit so we always have 2 bits.  */
10234         return extract32(tcr, 30, 1) * 3;
10235     }
10236 }
10237
10238 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10239                                    ARMMMUIdx mmu_idx, bool data)
10240 {
10241     uint64_t tcr = regime_tcr(env, mmu_idx);
10242     bool epd, hpd, using16k, using64k, tsz_oob, ds;
10243     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10244     ARMCPU *cpu = env_archcpu(env);
10245
10246     if (!regime_has_2_ranges(mmu_idx)) {
10247         select = 0;
10248         tsz = extract32(tcr, 0, 6);
10249         using64k = extract32(tcr, 14, 1);
10250         using16k = extract32(tcr, 15, 1);
10251         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10252             /* VTCR_EL2 */
10253             hpd = false;
10254         } else {
10255             hpd = extract32(tcr, 24, 1);
10256         }
10257         epd = false;
10258         sh = extract32(tcr, 12, 2);
10259         ps = extract32(tcr, 16, 3);
10260         ds = extract64(tcr, 32, 1);
10261     } else {
10262         /*
10263          * Bit 55 is always between the two regions, and is canonical for
10264          * determining if address tagging is enabled.
10265          */
10266         select = extract64(va, 55, 1);
10267         if (!select) {
10268             tsz = extract32(tcr, 0, 6);
10269             epd = extract32(tcr, 7, 1);
10270             sh = extract32(tcr, 12, 2);
10271             using64k = extract32(tcr, 14, 1);
10272             using16k = extract32(tcr, 15, 1);
10273             hpd = extract64(tcr, 41, 1);
10274         } else {
10275             int tg = extract32(tcr, 30, 2);
10276             using16k = tg == 1;
10277             using64k = tg == 3;
10278             tsz = extract32(tcr, 16, 6);
10279             epd = extract32(tcr, 23, 1);
10280             sh = extract32(tcr, 28, 2);
10281             hpd = extract64(tcr, 42, 1);
10282         }
10283         ps = extract64(tcr, 32, 3);
10284         ds = extract64(tcr, 59, 1);
10285     }
10286
10287     if (cpu_isar_feature(aa64_st, cpu)) {
10288         max_tsz = 48 - using64k;
10289     } else {
10290         max_tsz = 39;
10291     }
10292
10293     /*
10294      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10295      * adjust the effective value of DS, as documented.
10296      */
10297     min_tsz = 16;
10298     if (using64k) {
10299         if (cpu_isar_feature(aa64_lva, cpu)) {
10300             min_tsz = 12;
10301         }
10302         ds = false;
10303     } else if (ds) {
10304         switch (mmu_idx) {
10305         case ARMMMUIdx_Stage2:
10306         case ARMMMUIdx_Stage2_S:
10307             if (using16k) {
10308                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10309             } else {
10310                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10311             }
10312             break;
10313         default:
10314             if (using16k) {
10315                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10316             } else {
10317                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10318             }
10319             break;
10320         }
10321         if (ds) {
10322             min_tsz = 12;
10323         }
10324     }
10325
10326     if (tsz > max_tsz) {
10327         tsz = max_tsz;
10328         tsz_oob = true;
10329     } else if (tsz < min_tsz) {
10330         tsz = min_tsz;
10331         tsz_oob = true;
10332     } else {
10333         tsz_oob = false;
10334     }
10335
10336     /* Present TBI as a composite with TBID.  */
10337     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10338     if (!data) {
10339         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10340     }
10341     tbi = (tbi >> select) & 1;
10342
10343     return (ARMVAParameters) {
10344         .tsz = tsz,
10345         .ps = ps,
10346         .sh = sh,
10347         .select = select,
10348         .tbi = tbi,
10349         .epd = epd,
10350         .hpd = hpd,
10351         .using16k = using16k,
10352         .using64k = using64k,
10353         .tsz_oob = tsz_oob,
10354         .ds = ds,
10355     };
10356 }
10357
10358 /* Note that signed overflow is undefined in C.  The following routines are
10359    careful to use unsigned types where modulo arithmetic is required.
10360    Failure to do so _will_ break on newer gcc.  */
10361
10362 /* Signed saturating arithmetic.  */
10363
10364 /* Perform 16-bit signed saturating addition.  */
10365 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10366 {
10367     uint16_t res;
10368
10369     res = a + b;
10370     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10371         if (a & 0x8000)
10372             res = 0x8000;
10373         else
10374             res = 0x7fff;
10375     }
10376     return res;
10377 }
10378
10379 /* Perform 8-bit signed saturating addition.  */
10380 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10381 {
10382     uint8_t res;
10383
10384     res = a + b;
10385     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10386         if (a & 0x80)
10387             res = 0x80;
10388         else
10389             res = 0x7f;
10390     }
10391     return res;
10392 }
10393
10394 /* Perform 16-bit signed saturating subtraction.  */
10395 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10396 {
10397     uint16_t res;
10398
10399     res = a - b;
10400     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10401         if (a & 0x8000)
10402             res = 0x8000;
10403         else
10404             res = 0x7fff;
10405     }
10406     return res;
10407 }
10408
10409 /* Perform 8-bit signed saturating subtraction.  */
10410 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10411 {
10412     uint8_t res;
10413
10414     res = a - b;
10415     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10416         if (a & 0x80)
10417             res = 0x80;
10418         else
10419             res = 0x7f;
10420     }
10421     return res;
10422 }
10423
10424 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10425 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10426 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10427 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10428 #define PFX q
10429
10430 #include "op_addsub.h"
10431
10432 /* Unsigned saturating arithmetic.  */
10433 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10434 {
10435     uint16_t res;
10436     res = a + b;
10437     if (res < a)
10438         res = 0xffff;
10439     return res;
10440 }
10441
10442 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10443 {
10444     if (a > b)
10445         return a - b;
10446     else
10447         return 0;
10448 }
10449
10450 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10451 {
10452     uint8_t res;
10453     res = a + b;
10454     if (res < a)
10455         res = 0xff;
10456     return res;
10457 }
10458
10459 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10460 {
10461     if (a > b)
10462         return a - b;
10463     else
10464         return 0;
10465 }
10466
10467 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10468 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10469 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10470 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10471 #define PFX uq
10472
10473 #include "op_addsub.h"
10474
10475 /* Signed modulo arithmetic.  */
10476 #define SARITH16(a, b, n, op) do { \
10477     int32_t sum; \
10478     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10479     RESULT(sum, n, 16); \
10480     if (sum >= 0) \
10481         ge |= 3 << (n * 2); \
10482     } while(0)
10483
10484 #define SARITH8(a, b, n, op) do { \
10485     int32_t sum; \
10486     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10487     RESULT(sum, n, 8); \
10488     if (sum >= 0) \
10489         ge |= 1 << n; \
10490     } while(0)
10491
10492
10493 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10494 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10495 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10496 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10497 #define PFX s
10498 #define ARITH_GE
10499
10500 #include "op_addsub.h"
10501
10502 /* Unsigned modulo arithmetic.  */
10503 #define ADD16(a, b, n) do { \
10504     uint32_t sum; \
10505     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10506     RESULT(sum, n, 16); \
10507     if ((sum >> 16) == 1) \
10508         ge |= 3 << (n * 2); \
10509     } while(0)
10510
10511 #define ADD8(a, b, n) do { \
10512     uint32_t sum; \
10513     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10514     RESULT(sum, n, 8); \
10515     if ((sum >> 8) == 1) \
10516         ge |= 1 << n; \
10517     } while(0)
10518
10519 #define SUB16(a, b, n) do { \
10520     uint32_t sum; \
10521     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10522     RESULT(sum, n, 16); \
10523     if ((sum >> 16) == 0) \
10524         ge |= 3 << (n * 2); \
10525     } while(0)
10526
10527 #define SUB8(a, b, n) do { \
10528     uint32_t sum; \
10529     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10530     RESULT(sum, n, 8); \
10531     if ((sum >> 8) == 0) \
10532         ge |= 1 << n; \
10533     } while(0)
10534
10535 #define PFX u
10536 #define ARITH_GE
10537
10538 #include "op_addsub.h"
10539
10540 /* Halved signed arithmetic.  */
10541 #define ADD16(a, b, n) \
10542   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10543 #define SUB16(a, b, n) \
10544   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10545 #define ADD8(a, b, n) \
10546   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10547 #define SUB8(a, b, n) \
10548   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10549 #define PFX sh
10550
10551 #include "op_addsub.h"
10552
10553 /* Halved unsigned arithmetic.  */
10554 #define ADD16(a, b, n) \
10555   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10556 #define SUB16(a, b, n) \
10557   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10558 #define ADD8(a, b, n) \
10559   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10560 #define SUB8(a, b, n) \
10561   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10562 #define PFX uh
10563
10564 #include "op_addsub.h"
10565
10566 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10567 {
10568     if (a > b)
10569         return a - b;
10570     else
10571         return b - a;
10572 }
10573
10574 /* Unsigned sum of absolute byte differences.  */
10575 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10576 {
10577     uint32_t sum;
10578     sum = do_usad(a, b);
10579     sum += do_usad(a >> 8, b >> 8);
10580     sum += do_usad(a >> 16, b >> 16);
10581     sum += do_usad(a >> 24, b >> 24);
10582     return sum;
10583 }
10584
10585 /* For ARMv6 SEL instruction.  */
10586 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10587 {
10588     uint32_t mask;
10589
10590     mask = 0;
10591     if (flags & 1)
10592         mask |= 0xff;
10593     if (flags & 2)
10594         mask |= 0xff00;
10595     if (flags & 4)
10596         mask |= 0xff0000;
10597     if (flags & 8)
10598         mask |= 0xff000000;
10599     return (a & mask) | (b & ~mask);
10600 }
10601
10602 /* CRC helpers.
10603  * The upper bytes of val (above the number specified by 'bytes') must have
10604  * been zeroed out by the caller.
10605  */
10606 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10607 {
10608     uint8_t buf[4];
10609
10610     stl_le_p(buf, val);
10611
10612     /* zlib crc32 converts the accumulator and output to one's complement.  */
10613     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10614 }
10615
10616 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10617 {
10618     uint8_t buf[4];
10619
10620     stl_le_p(buf, val);
10621
10622     /* Linux crc32c converts the output to one's complement.  */
10623     return crc32c(acc, buf, bytes) ^ 0xffffffff;
10624 }
10625
10626 /* Return the exception level to which FP-disabled exceptions should
10627  * be taken, or 0 if FP is enabled.
10628  */
10629 int fp_exception_el(CPUARMState *env, int cur_el)
10630 {
10631 #ifndef CONFIG_USER_ONLY
10632     uint64_t hcr_el2;
10633
10634     /* CPACR and the CPTR registers don't exist before v6, so FP is
10635      * always accessible
10636      */
10637     if (!arm_feature(env, ARM_FEATURE_V6)) {
10638         return 0;
10639     }
10640
10641     if (arm_feature(env, ARM_FEATURE_M)) {
10642         /* CPACR can cause a NOCP UsageFault taken to current security state */
10643         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10644             return 1;
10645         }
10646
10647         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10648             if (!extract32(env->v7m.nsacr, 10, 1)) {
10649                 /* FP insns cause a NOCP UsageFault taken to Secure */
10650                 return 3;
10651             }
10652         }
10653
10654         return 0;
10655     }
10656
10657     hcr_el2 = arm_hcr_el2_eff(env);
10658
10659     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10660      * 0, 2 : trap EL0 and EL1/PL1 accesses
10661      * 1    : trap only EL0 accesses
10662      * 3    : trap no accesses
10663      * This register is ignored if E2H+TGE are both set.
10664      */
10665     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10666         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10667
10668         switch (fpen) {
10669         case 1:
10670             if (cur_el != 0) {
10671                 break;
10672             }
10673             /* fall through */
10674         case 0:
10675         case 2:
10676             /* Trap from Secure PL0 or PL1 to Secure PL1. */
10677             if (!arm_el_is_aa64(env, 3)
10678                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10679                 return 3;
10680             }
10681             if (cur_el <= 1) {
10682                 return 1;
10683             }
10684             break;
10685         }
10686     }
10687
10688     /*
10689      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10690      * to control non-secure access to the FPU. It doesn't have any
10691      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10692      */
10693     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10694          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10695         if (!extract32(env->cp15.nsacr, 10, 1)) {
10696             /* FP insns act as UNDEF */
10697             return cur_el == 2 ? 2 : 1;
10698         }
10699     }
10700
10701     /*
10702      * CPTR_EL2 is present in v7VE or v8, and changes format
10703      * with HCR_EL2.E2H (regardless of TGE).
10704      */
10705     if (cur_el <= 2) {
10706         if (hcr_el2 & HCR_E2H) {
10707             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
10708             case 1:
10709                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10710                     break;
10711                 }
10712                 /* fall through */
10713             case 0:
10714             case 2:
10715                 return 2;
10716             }
10717         } else if (arm_is_el2_enabled(env)) {
10718             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
10719                 return 2;
10720             }
10721         }
10722     }
10723
10724     /* CPTR_EL3 : present in v8 */
10725     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
10726         /* Trap all FP ops to EL3 */
10727         return 3;
10728     }
10729 #endif
10730     return 0;
10731 }
10732
10733 /* Return the exception level we're running at if this is our mmu_idx */
10734 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
10735 {
10736     if (mmu_idx & ARM_MMU_IDX_M) {
10737         return mmu_idx & ARM_MMU_IDX_M_PRIV;
10738     }
10739
10740     switch (mmu_idx) {
10741     case ARMMMUIdx_E10_0:
10742     case ARMMMUIdx_E20_0:
10743     case ARMMMUIdx_SE10_0:
10744     case ARMMMUIdx_SE20_0:
10745         return 0;
10746     case ARMMMUIdx_E10_1:
10747     case ARMMMUIdx_E10_1_PAN:
10748     case ARMMMUIdx_SE10_1:
10749     case ARMMMUIdx_SE10_1_PAN:
10750         return 1;
10751     case ARMMMUIdx_E2:
10752     case ARMMMUIdx_E20_2:
10753     case ARMMMUIdx_E20_2_PAN:
10754     case ARMMMUIdx_SE2:
10755     case ARMMMUIdx_SE20_2:
10756     case ARMMMUIdx_SE20_2_PAN:
10757         return 2;
10758     case ARMMMUIdx_SE3:
10759         return 3;
10760     default:
10761         g_assert_not_reached();
10762     }
10763 }
10764
10765 #ifndef CONFIG_TCG
10766 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10767 {
10768     g_assert_not_reached();
10769 }
10770 #endif
10771
10772 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
10773 {
10774     ARMMMUIdx idx;
10775     uint64_t hcr;
10776
10777     if (arm_feature(env, ARM_FEATURE_M)) {
10778         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
10779     }
10780
10781     /* See ARM pseudo-function ELIsInHost.  */
10782     switch (el) {
10783     case 0:
10784         hcr = arm_hcr_el2_eff(env);
10785         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
10786             idx = ARMMMUIdx_E20_0;
10787         } else {
10788             idx = ARMMMUIdx_E10_0;
10789         }
10790         break;
10791     case 1:
10792         if (env->pstate & PSTATE_PAN) {
10793             idx = ARMMMUIdx_E10_1_PAN;
10794         } else {
10795             idx = ARMMMUIdx_E10_1;
10796         }
10797         break;
10798     case 2:
10799         /* Note that TGE does not apply at EL2.  */
10800         if (arm_hcr_el2_eff(env) & HCR_E2H) {
10801             if (env->pstate & PSTATE_PAN) {
10802                 idx = ARMMMUIdx_E20_2_PAN;
10803             } else {
10804                 idx = ARMMMUIdx_E20_2;
10805             }
10806         } else {
10807             idx = ARMMMUIdx_E2;
10808         }
10809         break;
10810     case 3:
10811         return ARMMMUIdx_SE3;
10812     default:
10813         g_assert_not_reached();
10814     }
10815
10816     if (arm_is_secure_below_el3(env)) {
10817         idx &= ~ARM_MMU_IDX_A_NS;
10818     }
10819
10820     return idx;
10821 }
10822
10823 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
10824 {
10825     return arm_mmu_idx_el(env, arm_current_el(env));
10826 }
10827
10828 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
10829                                            ARMMMUIdx mmu_idx,
10830                                            CPUARMTBFlags flags)
10831 {
10832     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
10833     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
10834
10835     if (arm_singlestep_active(env)) {
10836         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
10837     }
10838     return flags;
10839 }
10840
10841 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
10842                                               ARMMMUIdx mmu_idx,
10843                                               CPUARMTBFlags flags)
10844 {
10845     bool sctlr_b = arm_sctlr_b(env);
10846
10847     if (sctlr_b) {
10848         DP_TBFLAG_A32(flags, SCTLR__B, 1);
10849     }
10850     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
10851         DP_TBFLAG_ANY(flags, BE_DATA, 1);
10852     }
10853     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
10854
10855     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
10856 }
10857
10858 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
10859                                         ARMMMUIdx mmu_idx)
10860 {
10861     CPUARMTBFlags flags = {};
10862     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
10863
10864     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
10865     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
10866         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10867     }
10868
10869     if (arm_v7m_is_handler_mode(env)) {
10870         DP_TBFLAG_M32(flags, HANDLER, 1);
10871     }
10872
10873     /*
10874      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
10875      * is suppressing them because the requested execution priority
10876      * is less than 0.
10877      */
10878     if (arm_feature(env, ARM_FEATURE_V8) &&
10879         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
10880           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
10881         DP_TBFLAG_M32(flags, STACKCHECK, 1);
10882     }
10883
10884     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10885 }
10886
10887 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
10888                                         ARMMMUIdx mmu_idx)
10889 {
10890     CPUARMTBFlags flags = {};
10891     int el = arm_current_el(env);
10892
10893     if (arm_sctlr(env, el) & SCTLR_A) {
10894         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10895     }
10896
10897     if (arm_el_is_aa64(env, 1)) {
10898         DP_TBFLAG_A32(flags, VFPEN, 1);
10899     }
10900
10901     if (el < 2 && env->cp15.hstr_el2 &&
10902         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10903         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
10904     }
10905
10906     if (env->uncached_cpsr & CPSR_IL) {
10907         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
10908     }
10909
10910     /*
10911      * The SME exception we are testing for is raised via
10912      * AArch64.CheckFPAdvSIMDEnabled(), as called from
10913      * AArch32.CheckAdvSIMDOrFPEnabled().
10914      */
10915     if (el == 0
10916         && FIELD_EX64(env->svcr, SVCR, SM)
10917         && (!arm_is_el2_enabled(env)
10918             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
10919         && arm_el_is_aa64(env, 1)
10920         && !sme_fa64(env, el)) {
10921         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
10922     }
10923
10924     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10925 }
10926
10927 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
10928                                         ARMMMUIdx mmu_idx)
10929 {
10930     CPUARMTBFlags flags = {};
10931     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
10932     uint64_t tcr = regime_tcr(env, mmu_idx);
10933     uint64_t sctlr;
10934     int tbii, tbid;
10935
10936     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
10937
10938     /* Get control bits for tagged addresses.  */
10939     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
10940     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
10941
10942     DP_TBFLAG_A64(flags, TBII, tbii);
10943     DP_TBFLAG_A64(flags, TBID, tbid);
10944
10945     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
10946         int sve_el = sve_exception_el(env, el);
10947
10948         /*
10949          * If either FP or SVE are disabled, translator does not need len.
10950          * If SVE EL > FP EL, FP exception has precedence, and translator
10951          * does not need SVE EL.  Save potential re-translations by forcing
10952          * the unneeded data to zero.
10953          */
10954         if (fp_el != 0) {
10955             if (sve_el > fp_el) {
10956                 sve_el = 0;
10957             }
10958         } else if (sve_el == 0) {
10959             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
10960         }
10961         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
10962     }
10963     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
10964         int sme_el = sme_exception_el(env, el);
10965         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
10966
10967         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
10968         if (sme_el == 0) {
10969             /* Similarly, do not compute SVL if SME is disabled. */
10970             int svl = sve_vqm1_for_el_sm(env, el, true);
10971             DP_TBFLAG_A64(flags, SVL, svl);
10972             if (sm) {
10973                 /* If SVE is disabled, we will not have set VL above. */
10974                 DP_TBFLAG_A64(flags, VL, svl);
10975             }
10976         }
10977         if (sm) {
10978             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
10979             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
10980         }
10981         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
10982     }
10983
10984     sctlr = regime_sctlr(env, stage1);
10985
10986     if (sctlr & SCTLR_A) {
10987         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10988     }
10989
10990     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
10991         DP_TBFLAG_ANY(flags, BE_DATA, 1);
10992     }
10993
10994     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
10995         /*
10996          * In order to save space in flags, we record only whether
10997          * pauth is "inactive", meaning all insns are implemented as
10998          * a nop, or "active" when some action must be performed.
10999          * The decision of which action to take is left to a helper.
11000          */
11001         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11002             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11003         }
11004     }
11005
11006     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11007         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11008         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11009             DP_TBFLAG_A64(flags, BT, 1);
11010         }
11011     }
11012
11013     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11014     if (!(env->pstate & PSTATE_UAO)) {
11015         switch (mmu_idx) {
11016         case ARMMMUIdx_E10_1:
11017         case ARMMMUIdx_E10_1_PAN:
11018         case ARMMMUIdx_SE10_1:
11019         case ARMMMUIdx_SE10_1_PAN:
11020             /* TODO: ARMv8.3-NV */
11021             DP_TBFLAG_A64(flags, UNPRIV, 1);
11022             break;
11023         case ARMMMUIdx_E20_2:
11024         case ARMMMUIdx_E20_2_PAN:
11025         case ARMMMUIdx_SE20_2:
11026         case ARMMMUIdx_SE20_2_PAN:
11027             /*
11028              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11029              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11030              */
11031             if (env->cp15.hcr_el2 & HCR_TGE) {
11032                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11033             }
11034             break;
11035         default:
11036             break;
11037         }
11038     }
11039
11040     if (env->pstate & PSTATE_IL) {
11041         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11042     }
11043
11044     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11045         /*
11046          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11047          * if all accesses must be Unchecked:
11048          * 1) If no TBI, then there are no tags in the address to check,
11049          * 2) If Tag Check Override, then all accesses are Unchecked,
11050          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11051          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11052          */
11053         if (allocation_tag_access_enabled(env, el, sctlr)) {
11054             DP_TBFLAG_A64(flags, ATA, 1);
11055             if (tbid
11056                 && !(env->pstate & PSTATE_TCO)
11057                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11058                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11059             }
11060         }
11061         /* And again for unprivileged accesses, if required.  */
11062         if (EX_TBFLAG_A64(flags, UNPRIV)
11063             && tbid
11064             && !(env->pstate & PSTATE_TCO)
11065             && (sctlr & SCTLR_TCF0)
11066             && allocation_tag_access_enabled(env, 0, sctlr)) {
11067             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11068         }
11069         /* Cache TCMA as well as TBI. */
11070         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11071     }
11072
11073     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11074 }
11075
11076 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11077 {
11078     int el = arm_current_el(env);
11079     int fp_el = fp_exception_el(env, el);
11080     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11081
11082     if (is_a64(env)) {
11083         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11084     } else if (arm_feature(env, ARM_FEATURE_M)) {
11085         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11086     } else {
11087         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11088     }
11089 }
11090
11091 void arm_rebuild_hflags(CPUARMState *env)
11092 {
11093     env->hflags = rebuild_hflags_internal(env);
11094 }
11095
11096 /*
11097  * If we have triggered a EL state change we can't rely on the
11098  * translator having passed it to us, we need to recompute.
11099  */
11100 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11101 {
11102     int el = arm_current_el(env);
11103     int fp_el = fp_exception_el(env, el);
11104     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11105
11106     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11107 }
11108
11109 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11110 {
11111     int fp_el = fp_exception_el(env, el);
11112     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11113
11114     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11115 }
11116
11117 /*
11118  * If we have triggered a EL state change we can't rely on the
11119  * translator having passed it to us, we need to recompute.
11120  */
11121 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11122 {
11123     int el = arm_current_el(env);
11124     int fp_el = fp_exception_el(env, el);
11125     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11126     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11127 }
11128
11129 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11130 {
11131     int fp_el = fp_exception_el(env, el);
11132     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11133
11134     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11135 }
11136
11137 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11138 {
11139     int fp_el = fp_exception_el(env, el);
11140     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11141
11142     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11143 }
11144
11145 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11146 {
11147 #ifdef CONFIG_DEBUG_TCG
11148     CPUARMTBFlags c = env->hflags;
11149     CPUARMTBFlags r = rebuild_hflags_internal(env);
11150
11151     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11152         fprintf(stderr, "TCG hflags mismatch "
11153                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11154                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11155                 c.flags, c.flags2, r.flags, r.flags2);
11156         abort();
11157     }
11158 #endif
11159 }
11160
11161 static bool mve_no_pred(CPUARMState *env)
11162 {
11163     /*
11164      * Return true if there is definitely no predication of MVE
11165      * instructions by VPR or LTPSIZE. (Returning false even if there
11166      * isn't any predication is OK; generated code will just be
11167      * a little worse.)
11168      * If the CPU does not implement MVE then this TB flag is always 0.
11169      *
11170      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11171      * logic in gen_update_fp_context() needs to be updated to match.
11172      *
11173      * We do not include the effect of the ECI bits here -- they are
11174      * tracked in other TB flags. This simplifies the logic for
11175      * "when did we emit code that changes the MVE_NO_PRED TB flag
11176      * and thus need to end the TB?".
11177      */
11178     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11179         return false;
11180     }
11181     if (env->v7m.vpr) {
11182         return false;
11183     }
11184     if (env->v7m.ltpsize < 4) {
11185         return false;
11186     }
11187     return true;
11188 }
11189
11190 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11191                           target_ulong *cs_base, uint32_t *pflags)
11192 {
11193     CPUARMTBFlags flags;
11194
11195     assert_hflags_rebuild_correctly(env);
11196     flags = env->hflags;
11197
11198     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11199         *pc = env->pc;
11200         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11201             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11202         }
11203     } else {
11204         *pc = env->regs[15];
11205
11206         if (arm_feature(env, ARM_FEATURE_M)) {
11207             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11208                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11209                 != env->v7m.secure) {
11210                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11211             }
11212
11213             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11214                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11215                  (env->v7m.secure &&
11216                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11217                 /*
11218                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11219                  * active FP context; we must create a new FP context before
11220                  * executing any FP insn.
11221                  */
11222                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11223             }
11224
11225             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11226             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11227                 DP_TBFLAG_M32(flags, LSPACT, 1);
11228             }
11229
11230             if (mve_no_pred(env)) {
11231                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11232             }
11233         } else {
11234             /*
11235              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11236              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11237              */
11238             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11239                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11240             } else {
11241                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11242                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11243             }
11244             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11245                 DP_TBFLAG_A32(flags, VFPEN, 1);
11246             }
11247         }
11248
11249         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11250         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11251     }
11252
11253     /*
11254      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11255      * states defined in the ARM ARM for software singlestep:
11256      *  SS_ACTIVE   PSTATE.SS   State
11257      *     0            x       Inactive (the TB flag for SS is always 0)
11258      *     1            0       Active-pending
11259      *     1            1       Active-not-pending
11260      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11261      */
11262     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11263         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11264     }
11265
11266     *pflags = flags.flags;
11267     *cs_base = flags.flags2;
11268 }
11269
11270 #ifdef TARGET_AARCH64
11271 /*
11272  * The manual says that when SVE is enabled and VQ is widened the
11273  * implementation is allowed to zero the previously inaccessible
11274  * portion of the registers.  The corollary to that is that when
11275  * SVE is enabled and VQ is narrowed we are also allowed to zero
11276  * the now inaccessible portion of the registers.
11277  *
11278  * The intent of this is that no predicate bit beyond VQ is ever set.
11279  * Which means that some operations on predicate registers themselves
11280  * may operate on full uint64_t or even unrolled across the maximum
11281  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11282  * may well be cheaper than conditionals to restrict the operation
11283  * to the relevant portion of a uint16_t[16].
11284  */
11285 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11286 {
11287     int i, j;
11288     uint64_t pmask;
11289
11290     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11291     assert(vq <= env_archcpu(env)->sve_max_vq);
11292
11293     /* Zap the high bits of the zregs.  */
11294     for (i = 0; i < 32; i++) {
11295         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11296     }
11297
11298     /* Zap the high bits of the pregs and ffr.  */
11299     pmask = 0;
11300     if (vq & 3) {
11301         pmask = ~(-1ULL << (16 * (vq & 3)));
11302     }
11303     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11304         for (i = 0; i < 17; ++i) {
11305             env->vfp.pregs[i].p[j] &= pmask;
11306         }
11307         pmask = 0;
11308     }
11309 }
11310
11311 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11312 {
11313     int exc_el;
11314
11315     if (sm) {
11316         exc_el = sme_exception_el(env, el);
11317     } else {
11318         exc_el = sve_exception_el(env, el);
11319     }
11320     if (exc_el) {
11321         return 0; /* disabled */
11322     }
11323     return sve_vqm1_for_el_sm(env, el, sm);
11324 }
11325
11326 /*
11327  * Notice a change in SVE vector size when changing EL.
11328  */
11329 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11330                            int new_el, bool el0_a64)
11331 {
11332     ARMCPU *cpu = env_archcpu(env);
11333     int old_len, new_len;
11334     bool old_a64, new_a64, sm;
11335
11336     /* Nothing to do if no SVE.  */
11337     if (!cpu_isar_feature(aa64_sve, cpu)) {
11338         return;
11339     }
11340
11341     /* Nothing to do if FP is disabled in either EL.  */
11342     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11343         return;
11344     }
11345
11346     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11347     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11348
11349     /*
11350      * Both AArch64.TakeException and AArch64.ExceptionReturn
11351      * invoke ResetSVEState when taking an exception from, or
11352      * returning to, AArch32 state when PSTATE.SM is enabled.
11353      */
11354     sm = FIELD_EX64(env->svcr, SVCR, SM);
11355     if (old_a64 != new_a64 && sm) {
11356         arm_reset_sve_state(env);
11357         return;
11358     }
11359
11360     /*
11361      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11362      * at ELx, or not available because the EL is in AArch32 state, then
11363      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11364      * has an effective value of 0".
11365      *
11366      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11367      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11368      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11369      * we already have the correct register contents when encountering the
11370      * vq0->vq0 transition between EL0->EL1.
11371      */
11372     old_len = new_len = 0;
11373     if (old_a64) {
11374         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11375     }
11376     if (new_a64) {
11377         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11378     }
11379
11380     /* When changing vector length, clear inaccessible state.  */
11381     if (new_len < old_len) {
11382         aarch64_sve_narrow_vq(env, new_len + 1);
11383     }
11384 }
11385 #endif
This page took 0.66537 seconds and 4 git commands to generate.