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