OSDN Git Service

2c27077fb2dcb5f8d6c2447e6ef4fd2c9b5797e5
[qmiga/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 "target/arm/idau.h"
12 #include "trace.h"
13 #include "cpu.h"
14 #include "internals.h"
15 #include "exec/gdbstub.h"
16 #include "exec/helper-proto.h"
17 #include "qemu/host-utils.h"
18 #include "qemu/main-loop.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 "hw/semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/cpu-timers.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/tcg.h"
30 #include "qemu/range.h"
31 #include "qapi/qapi-commands-machine-target.h"
32 #include "qapi/error.h"
33 #include "qemu/guest-random.h"
34 #ifdef CONFIG_TCG
35 #include "arm_ldst.h"
36 #include "exec/cpu_ldst.h"
37 #include "hw/semihosting/common-semi.h"
38 #endif
39
40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
41
42 #ifndef CONFIG_USER_ONLY
43
44 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
45                                MMUAccessType access_type, ARMMMUIdx mmu_idx,
46                                bool s1_is_el0,
47                                hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
48                                target_ulong *page_size_ptr,
49                                ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
50     __attribute__((nonnull));
51 #endif
52
53 static void switch_mode(CPUARMState *env, int mode);
54 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx);
55
56 static int vfp_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
57 {
58     ARMCPU *cpu = env_archcpu(env);
59     int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
60
61     /* VFP data registers are always little-endian.  */
62     if (reg < nregs) {
63         return gdb_get_reg64(buf, *aa32_vfp_dreg(env, reg));
64     }
65     if (arm_feature(env, ARM_FEATURE_NEON)) {
66         /* Aliases for Q regs.  */
67         nregs += 16;
68         if (reg < nregs) {
69             uint64_t *q = aa32_vfp_qreg(env, reg - 32);
70             return gdb_get_reg128(buf, q[0], q[1]);
71         }
72     }
73     switch (reg - nregs) {
74     case 0: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPSID]); break;
75     case 1: return gdb_get_reg32(buf, vfp_get_fpscr(env)); break;
76     case 2: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPEXC]); break;
77     }
78     return 0;
79 }
80
81 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
82 {
83     ARMCPU *cpu = env_archcpu(env);
84     int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
85
86     if (reg < nregs) {
87         *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
88         return 8;
89     }
90     if (arm_feature(env, ARM_FEATURE_NEON)) {
91         nregs += 16;
92         if (reg < nregs) {
93             uint64_t *q = aa32_vfp_qreg(env, reg - 32);
94             q[0] = ldq_le_p(buf);
95             q[1] = ldq_le_p(buf + 8);
96             return 16;
97         }
98     }
99     switch (reg - nregs) {
100     case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
101     case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
102     case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
103     }
104     return 0;
105 }
106
107 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
108 {
109     switch (reg) {
110     case 0 ... 31:
111     {
112         /* 128 bit FP register - quads are in LE order */
113         uint64_t *q = aa64_vfp_qreg(env, reg);
114         return gdb_get_reg128(buf, q[1], q[0]);
115     }
116     case 32:
117         /* FPSR */
118         return gdb_get_reg32(buf, vfp_get_fpsr(env));
119     case 33:
120         /* FPCR */
121         return gdb_get_reg32(buf,vfp_get_fpcr(env));
122     default:
123         return 0;
124     }
125 }
126
127 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
128 {
129     switch (reg) {
130     case 0 ... 31:
131         /* 128 bit FP register */
132         {
133             uint64_t *q = aa64_vfp_qreg(env, reg);
134             q[0] = ldq_le_p(buf);
135             q[1] = ldq_le_p(buf + 8);
136             return 16;
137         }
138     case 32:
139         /* FPSR */
140         vfp_set_fpsr(env, ldl_p(buf));
141         return 4;
142     case 33:
143         /* FPCR */
144         vfp_set_fpcr(env, ldl_p(buf));
145         return 4;
146     default:
147         return 0;
148     }
149 }
150
151 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
152 {
153     assert(ri->fieldoffset);
154     if (cpreg_field_is_64bit(ri)) {
155         return CPREG_FIELD64(env, ri);
156     } else {
157         return CPREG_FIELD32(env, ri);
158     }
159 }
160
161 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
162                       uint64_t value)
163 {
164     assert(ri->fieldoffset);
165     if (cpreg_field_is_64bit(ri)) {
166         CPREG_FIELD64(env, ri) = value;
167     } else {
168         CPREG_FIELD32(env, ri) = value;
169     }
170 }
171
172 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
173 {
174     return (char *)env + ri->fieldoffset;
175 }
176
177 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
178 {
179     /* Raw read of a coprocessor register (as needed for migration, etc). */
180     if (ri->type & ARM_CP_CONST) {
181         return ri->resetvalue;
182     } else if (ri->raw_readfn) {
183         return ri->raw_readfn(env, ri);
184     } else if (ri->readfn) {
185         return ri->readfn(env, ri);
186     } else {
187         return raw_read(env, ri);
188     }
189 }
190
191 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
192                              uint64_t v)
193 {
194     /* Raw write of a coprocessor register (as needed for migration, etc).
195      * Note that constant registers are treated as write-ignored; the
196      * caller should check for success by whether a readback gives the
197      * value written.
198      */
199     if (ri->type & ARM_CP_CONST) {
200         return;
201     } else if (ri->raw_writefn) {
202         ri->raw_writefn(env, ri, v);
203     } else if (ri->writefn) {
204         ri->writefn(env, ri, v);
205     } else {
206         raw_write(env, ri, v);
207     }
208 }
209
210 /**
211  * arm_get/set_gdb_*: get/set a gdb register
212  * @env: the CPU state
213  * @buf: a buffer to copy to/from
214  * @reg: register number (offset from start of group)
215  *
216  * We return the number of bytes copied
217  */
218
219 static int arm_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg)
220 {
221     ARMCPU *cpu = env_archcpu(env);
222     const ARMCPRegInfo *ri;
223     uint32_t key;
224
225     key = cpu->dyn_sysreg_xml.data.cpregs.keys[reg];
226     ri = get_arm_cp_reginfo(cpu->cp_regs, key);
227     if (ri) {
228         if (cpreg_field_is_64bit(ri)) {
229             return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
230         } else {
231             return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
232         }
233     }
234     return 0;
235 }
236
237 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
238 {
239     return 0;
240 }
241
242 #ifdef TARGET_AARCH64
243 static int arm_gdb_get_svereg(CPUARMState *env, GByteArray *buf, int reg)
244 {
245     ARMCPU *cpu = env_archcpu(env);
246
247     switch (reg) {
248     /* The first 32 registers are the zregs */
249     case 0 ... 31:
250     {
251         int vq, len = 0;
252         for (vq = 0; vq < cpu->sve_max_vq; vq++) {
253             len += gdb_get_reg128(buf,
254                                   env->vfp.zregs[reg].d[vq * 2 + 1],
255                                   env->vfp.zregs[reg].d[vq * 2]);
256         }
257         return len;
258     }
259     case 32:
260         return gdb_get_reg32(buf, vfp_get_fpsr(env));
261     case 33:
262         return gdb_get_reg32(buf, vfp_get_fpcr(env));
263     /* then 16 predicates and the ffr */
264     case 34 ... 50:
265     {
266         int preg = reg - 34;
267         int vq, len = 0;
268         for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
269             len += gdb_get_reg64(buf, env->vfp.pregs[preg].p[vq / 4]);
270         }
271         return len;
272     }
273     case 51:
274     {
275         /*
276          * We report in Vector Granules (VG) which is 64bit in a Z reg
277          * while the ZCR works in Vector Quads (VQ) which is 128bit chunks.
278          */
279         int vq = sve_zcr_len_for_el(env, arm_current_el(env)) + 1;
280         return gdb_get_reg64(buf, vq * 2);
281     }
282     default:
283         /* gdbstub asked for something out our range */
284         qemu_log_mask(LOG_UNIMP, "%s: out of range register %d", __func__, reg);
285         break;
286     }
287
288     return 0;
289 }
290
291 static int arm_gdb_set_svereg(CPUARMState *env, uint8_t *buf, int reg)
292 {
293     ARMCPU *cpu = env_archcpu(env);
294
295     /* The first 32 registers are the zregs */
296     switch (reg) {
297     /* The first 32 registers are the zregs */
298     case 0 ... 31:
299     {
300         int vq, len = 0;
301         uint64_t *p = (uint64_t *) buf;
302         for (vq = 0; vq < cpu->sve_max_vq; vq++) {
303             env->vfp.zregs[reg].d[vq * 2 + 1] = *p++;
304             env->vfp.zregs[reg].d[vq * 2] = *p++;
305             len += 16;
306         }
307         return len;
308     }
309     case 32:
310         vfp_set_fpsr(env, *(uint32_t *)buf);
311         return 4;
312     case 33:
313         vfp_set_fpcr(env, *(uint32_t *)buf);
314         return 4;
315     case 34 ... 50:
316     {
317         int preg = reg - 34;
318         int vq, len = 0;
319         uint64_t *p = (uint64_t *) buf;
320         for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
321             env->vfp.pregs[preg].p[vq / 4] = *p++;
322             len += 8;
323         }
324         return len;
325     }
326     case 51:
327         /* cannot set vg via gdbstub */
328         return 0;
329     default:
330         /* gdbstub asked for something out our range */
331         break;
332     }
333
334     return 0;
335 }
336 #endif /* TARGET_AARCH64 */
337
338 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
339 {
340    /* Return true if the regdef would cause an assertion if you called
341     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
342     * program bug for it not to have the NO_RAW flag).
343     * NB that returning false here doesn't necessarily mean that calling
344     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
345     * read/write access functions which are safe for raw use" from "has
346     * read/write access functions which have side effects but has forgotten
347     * to provide raw access functions".
348     * The tests here line up with the conditions in read/write_raw_cp_reg()
349     * and assertions in raw_read()/raw_write().
350     */
351     if ((ri->type & ARM_CP_CONST) ||
352         ri->fieldoffset ||
353         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
354         return false;
355     }
356     return true;
357 }
358
359 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
360 {
361     /* Write the coprocessor state from cpu->env to the (index,value) list. */
362     int i;
363     bool ok = true;
364
365     for (i = 0; i < cpu->cpreg_array_len; i++) {
366         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
367         const ARMCPRegInfo *ri;
368         uint64_t newval;
369
370         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
371         if (!ri) {
372             ok = false;
373             continue;
374         }
375         if (ri->type & ARM_CP_NO_RAW) {
376             continue;
377         }
378
379         newval = read_raw_cp_reg(&cpu->env, ri);
380         if (kvm_sync) {
381             /*
382              * Only sync if the previous list->cpustate sync succeeded.
383              * Rather than tracking the success/failure state for every
384              * item in the list, we just recheck "does the raw write we must
385              * have made in write_list_to_cpustate() read back OK" here.
386              */
387             uint64_t oldval = cpu->cpreg_values[i];
388
389             if (oldval == newval) {
390                 continue;
391             }
392
393             write_raw_cp_reg(&cpu->env, ri, oldval);
394             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
395                 continue;
396             }
397
398             write_raw_cp_reg(&cpu->env, ri, newval);
399         }
400         cpu->cpreg_values[i] = newval;
401     }
402     return ok;
403 }
404
405 bool write_list_to_cpustate(ARMCPU *cpu)
406 {
407     int i;
408     bool ok = true;
409
410     for (i = 0; i < cpu->cpreg_array_len; i++) {
411         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
412         uint64_t v = cpu->cpreg_values[i];
413         const ARMCPRegInfo *ri;
414
415         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
416         if (!ri) {
417             ok = false;
418             continue;
419         }
420         if (ri->type & ARM_CP_NO_RAW) {
421             continue;
422         }
423         /* Write value and confirm it reads back as written
424          * (to catch read-only registers and partially read-only
425          * registers where the incoming migration value doesn't match)
426          */
427         write_raw_cp_reg(&cpu->env, ri, v);
428         if (read_raw_cp_reg(&cpu->env, ri) != v) {
429             ok = false;
430         }
431     }
432     return ok;
433 }
434
435 static void add_cpreg_to_list(gpointer key, gpointer opaque)
436 {
437     ARMCPU *cpu = opaque;
438     uint64_t regidx;
439     const ARMCPRegInfo *ri;
440
441     regidx = *(uint32_t *)key;
442     ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
443
444     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
445         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
446         /* The value array need not be initialized at this point */
447         cpu->cpreg_array_len++;
448     }
449 }
450
451 static void count_cpreg(gpointer key, gpointer opaque)
452 {
453     ARMCPU *cpu = opaque;
454     uint64_t regidx;
455     const ARMCPRegInfo *ri;
456
457     regidx = *(uint32_t *)key;
458     ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
459
460     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
461         cpu->cpreg_array_len++;
462     }
463 }
464
465 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
466 {
467     uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
468     uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
469
470     if (aidx > bidx) {
471         return 1;
472     }
473     if (aidx < bidx) {
474         return -1;
475     }
476     return 0;
477 }
478
479 void init_cpreg_list(ARMCPU *cpu)
480 {
481     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
482      * Note that we require cpreg_tuples[] to be sorted by key ID.
483      */
484     GList *keys;
485     int arraylen;
486
487     keys = g_hash_table_get_keys(cpu->cp_regs);
488     keys = g_list_sort(keys, cpreg_key_compare);
489
490     cpu->cpreg_array_len = 0;
491
492     g_list_foreach(keys, count_cpreg, cpu);
493
494     arraylen = cpu->cpreg_array_len;
495     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
496     cpu->cpreg_values = g_new(uint64_t, arraylen);
497     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
498     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
499     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
500     cpu->cpreg_array_len = 0;
501
502     g_list_foreach(keys, add_cpreg_to_list, cpu);
503
504     assert(cpu->cpreg_array_len == arraylen);
505
506     g_list_free(keys);
507 }
508
509 /*
510  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
511  */
512 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
513                                         const ARMCPRegInfo *ri,
514                                         bool isread)
515 {
516     if (!is_a64(env) && arm_current_el(env) == 3 &&
517         arm_is_secure_below_el3(env)) {
518         return CP_ACCESS_TRAP_UNCATEGORIZED;
519     }
520     return CP_ACCESS_OK;
521 }
522
523 /* Some secure-only AArch32 registers trap to EL3 if used from
524  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
525  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
526  * We assume that the .access field is set to PL1_RW.
527  */
528 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
529                                             const ARMCPRegInfo *ri,
530                                             bool isread)
531 {
532     if (arm_current_el(env) == 3) {
533         return CP_ACCESS_OK;
534     }
535     if (arm_is_secure_below_el3(env)) {
536         if (env->cp15.scr_el3 & SCR_EEL2) {
537             return CP_ACCESS_TRAP_EL2;
538         }
539         return CP_ACCESS_TRAP_EL3;
540     }
541     /* This will be EL1 NS and EL2 NS, which just UNDEF */
542     return CP_ACCESS_TRAP_UNCATEGORIZED;
543 }
544
545 static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
546 {
547     return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
548 }
549
550 /* Check for traps to "powerdown debug" registers, which are controlled
551  * by MDCR.TDOSA
552  */
553 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
554                                    bool isread)
555 {
556     int el = arm_current_el(env);
557     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
558     bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
559         (arm_hcr_el2_eff(env) & HCR_TGE);
560
561     if (el < 2 && mdcr_el2_tdosa) {
562         return CP_ACCESS_TRAP_EL2;
563     }
564     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
565         return CP_ACCESS_TRAP_EL3;
566     }
567     return CP_ACCESS_OK;
568 }
569
570 /* Check for traps to "debug ROM" registers, which are controlled
571  * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
572  */
573 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
574                                   bool isread)
575 {
576     int el = arm_current_el(env);
577     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
578     bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
579         (arm_hcr_el2_eff(env) & HCR_TGE);
580
581     if (el < 2 && mdcr_el2_tdra) {
582         return CP_ACCESS_TRAP_EL2;
583     }
584     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
585         return CP_ACCESS_TRAP_EL3;
586     }
587     return CP_ACCESS_OK;
588 }
589
590 /* Check for traps to general debug registers, which are controlled
591  * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
592  */
593 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
594                                   bool isread)
595 {
596     int el = arm_current_el(env);
597     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
598     bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
599         (arm_hcr_el2_eff(env) & HCR_TGE);
600
601     if (el < 2 && mdcr_el2_tda) {
602         return CP_ACCESS_TRAP_EL2;
603     }
604     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
605         return CP_ACCESS_TRAP_EL3;
606     }
607     return CP_ACCESS_OK;
608 }
609
610 /* Check for traps to performance monitor registers, which are controlled
611  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
612  */
613 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
614                                  bool isread)
615 {
616     int el = arm_current_el(env);
617     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
618
619     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
620         return CP_ACCESS_TRAP_EL2;
621     }
622     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
623         return CP_ACCESS_TRAP_EL3;
624     }
625     return CP_ACCESS_OK;
626 }
627
628 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
629 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
630                                       bool isread)
631 {
632     if (arm_current_el(env) == 1) {
633         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
634         if (arm_hcr_el2_eff(env) & trap) {
635             return CP_ACCESS_TRAP_EL2;
636         }
637     }
638     return CP_ACCESS_OK;
639 }
640
641 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
642 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
643                                  bool isread)
644 {
645     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
646         return CP_ACCESS_TRAP_EL2;
647     }
648     return CP_ACCESS_OK;
649 }
650
651 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
652 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
653                                   bool isread)
654 {
655     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
656         return CP_ACCESS_TRAP_EL2;
657     }
658     return CP_ACCESS_OK;
659 }
660
661 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
662 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
663                                   bool isread)
664 {
665     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
666         return CP_ACCESS_TRAP_EL2;
667     }
668     return CP_ACCESS_OK;
669 }
670
671 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
672 {
673     ARMCPU *cpu = env_archcpu(env);
674
675     raw_write(env, ri, value);
676     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
677 }
678
679 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
680 {
681     ARMCPU *cpu = env_archcpu(env);
682
683     if (raw_read(env, ri) != value) {
684         /* Unlike real hardware the qemu TLB uses virtual addresses,
685          * not modified virtual addresses, so this causes a TLB flush.
686          */
687         tlb_flush(CPU(cpu));
688         raw_write(env, ri, value);
689     }
690 }
691
692 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
693                              uint64_t value)
694 {
695     ARMCPU *cpu = env_archcpu(env);
696
697     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
698         && !extended_addresses_enabled(env)) {
699         /* For VMSA (when not using the LPAE long descriptor page table
700          * format) this register includes the ASID, so do a TLB flush.
701          * For PMSA it is purely a process ID and no action is needed.
702          */
703         tlb_flush(CPU(cpu));
704     }
705     raw_write(env, ri, value);
706 }
707
708 /* IS variants of TLB operations must affect all cores */
709 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
710                              uint64_t value)
711 {
712     CPUState *cs = env_cpu(env);
713
714     tlb_flush_all_cpus_synced(cs);
715 }
716
717 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
718                              uint64_t value)
719 {
720     CPUState *cs = env_cpu(env);
721
722     tlb_flush_all_cpus_synced(cs);
723 }
724
725 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
726                              uint64_t value)
727 {
728     CPUState *cs = env_cpu(env);
729
730     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
731 }
732
733 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
734                              uint64_t value)
735 {
736     CPUState *cs = env_cpu(env);
737
738     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
739 }
740
741 /*
742  * Non-IS variants of TLB operations are upgraded to
743  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
744  * force broadcast of these operations.
745  */
746 static bool tlb_force_broadcast(CPUARMState *env)
747 {
748     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
749 }
750
751 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
752                           uint64_t value)
753 {
754     /* Invalidate all (TLBIALL) */
755     CPUState *cs = env_cpu(env);
756
757     if (tlb_force_broadcast(env)) {
758         tlb_flush_all_cpus_synced(cs);
759     } else {
760         tlb_flush(cs);
761     }
762 }
763
764 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
765                           uint64_t value)
766 {
767     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
768     CPUState *cs = env_cpu(env);
769
770     value &= TARGET_PAGE_MASK;
771     if (tlb_force_broadcast(env)) {
772         tlb_flush_page_all_cpus_synced(cs, value);
773     } else {
774         tlb_flush_page(cs, value);
775     }
776 }
777
778 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
779                            uint64_t value)
780 {
781     /* Invalidate by ASID (TLBIASID) */
782     CPUState *cs = env_cpu(env);
783
784     if (tlb_force_broadcast(env)) {
785         tlb_flush_all_cpus_synced(cs);
786     } else {
787         tlb_flush(cs);
788     }
789 }
790
791 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
792                            uint64_t value)
793 {
794     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
795     CPUState *cs = env_cpu(env);
796
797     value &= TARGET_PAGE_MASK;
798     if (tlb_force_broadcast(env)) {
799         tlb_flush_page_all_cpus_synced(cs, value);
800     } else {
801         tlb_flush_page(cs, value);
802     }
803 }
804
805 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
806                                uint64_t value)
807 {
808     CPUState *cs = env_cpu(env);
809
810     tlb_flush_by_mmuidx(cs,
811                         ARMMMUIdxBit_E10_1 |
812                         ARMMMUIdxBit_E10_1_PAN |
813                         ARMMMUIdxBit_E10_0);
814 }
815
816 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
817                                   uint64_t value)
818 {
819     CPUState *cs = env_cpu(env);
820
821     tlb_flush_by_mmuidx_all_cpus_synced(cs,
822                                         ARMMMUIdxBit_E10_1 |
823                                         ARMMMUIdxBit_E10_1_PAN |
824                                         ARMMMUIdxBit_E10_0);
825 }
826
827
828 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
829                               uint64_t value)
830 {
831     CPUState *cs = env_cpu(env);
832
833     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
834 }
835
836 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
837                                  uint64_t value)
838 {
839     CPUState *cs = env_cpu(env);
840
841     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
842 }
843
844 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
845                               uint64_t value)
846 {
847     CPUState *cs = env_cpu(env);
848     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
849
850     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
851 }
852
853 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
854                                  uint64_t value)
855 {
856     CPUState *cs = env_cpu(env);
857     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
858
859     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
860                                              ARMMMUIdxBit_E2);
861 }
862
863 static const ARMCPRegInfo cp_reginfo[] = {
864     /* Define the secure and non-secure FCSE identifier CP registers
865      * separately because there is no secure bank in V8 (no _EL3).  This allows
866      * the secure register to be properly reset and migrated. There is also no
867      * v8 EL1 version of the register so the non-secure instance stands alone.
868      */
869     { .name = "FCSEIDR",
870       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
871       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
872       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
873       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
874     { .name = "FCSEIDR_S",
875       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
876       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
877       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
878       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
879     /* Define the secure and non-secure context identifier CP registers
880      * separately because there is no secure bank in V8 (no _EL3).  This allows
881      * the secure register to be properly reset and migrated.  In the
882      * non-secure case, the 32-bit register will have reset and migration
883      * disabled during registration as it is handled by the 64-bit instance.
884      */
885     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
886       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
887       .access = PL1_RW, .accessfn = access_tvm_trvm,
888       .secure = ARM_CP_SECSTATE_NS,
889       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
890       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
891     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
892       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
893       .access = PL1_RW, .accessfn = access_tvm_trvm,
894       .secure = ARM_CP_SECSTATE_S,
895       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
896       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
897     REGINFO_SENTINEL
898 };
899
900 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
901     /* NB: Some of these registers exist in v8 but with more precise
902      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
903      */
904     /* MMU Domain access control / MPU write buffer control */
905     { .name = "DACR",
906       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
907       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
908       .writefn = dacr_write, .raw_writefn = raw_write,
909       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
910                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
911     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
912      * For v6 and v5, these mappings are overly broad.
913      */
914     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
915       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
916     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
917       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
918     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
919       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
920     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
921       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
922     /* Cache maintenance ops; some of this space may be overridden later. */
923     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
924       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
925       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
926     REGINFO_SENTINEL
927 };
928
929 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
930     /* Not all pre-v6 cores implemented this WFI, so this is slightly
931      * over-broad.
932      */
933     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
934       .access = PL1_W, .type = ARM_CP_WFI },
935     REGINFO_SENTINEL
936 };
937
938 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
939     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
940      * is UNPREDICTABLE; we choose to NOP as most implementations do).
941      */
942     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
943       .access = PL1_W, .type = ARM_CP_WFI },
944     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
945      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
946      * OMAPCP will override this space.
947      */
948     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
949       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
950       .resetvalue = 0 },
951     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
952       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
953       .resetvalue = 0 },
954     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
955     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
956       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
957       .resetvalue = 0 },
958     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
959      * implementing it as RAZ means the "debug architecture version" bits
960      * will read as a reserved value, which should cause Linux to not try
961      * to use the debug hardware.
962      */
963     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
964       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
965     /* MMU TLB control. Note that the wildcarding means we cover not just
966      * the unified TLB ops but also the dside/iside/inner-shareable variants.
967      */
968     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
969       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
970       .type = ARM_CP_NO_RAW },
971     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
972       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
973       .type = ARM_CP_NO_RAW },
974     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
975       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
976       .type = ARM_CP_NO_RAW },
977     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
978       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
979       .type = ARM_CP_NO_RAW },
980     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
981       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
982     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
983       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
984     REGINFO_SENTINEL
985 };
986
987 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
988                         uint64_t value)
989 {
990     uint32_t mask = 0;
991
992     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
993     if (!arm_feature(env, ARM_FEATURE_V8)) {
994         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
995          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
996          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
997          */
998         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
999             /* VFP coprocessor: cp10 & cp11 [23:20] */
1000             mask |= (1 << 31) | (1 << 30) | (0xf << 20);
1001
1002             if (!arm_feature(env, ARM_FEATURE_NEON)) {
1003                 /* ASEDIS [31] bit is RAO/WI */
1004                 value |= (1 << 31);
1005             }
1006
1007             /* VFPv3 and upwards with NEON implement 32 double precision
1008              * registers (D0-D31).
1009              */
1010             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
1011                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
1012                 value |= (1 << 30);
1013             }
1014         }
1015         value &= mask;
1016     }
1017
1018     /*
1019      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1020      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1021      */
1022     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1023         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1024         value &= ~(0xf << 20);
1025         value |= env->cp15.cpacr_el1 & (0xf << 20);
1026     }
1027
1028     env->cp15.cpacr_el1 = value;
1029 }
1030
1031 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1032 {
1033     /*
1034      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1035      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1036      */
1037     uint64_t value = env->cp15.cpacr_el1;
1038
1039     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1040         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1041         value &= ~(0xf << 20);
1042     }
1043     return value;
1044 }
1045
1046
1047 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1048 {
1049     /* Call cpacr_write() so that we reset with the correct RAO bits set
1050      * for our CPU features.
1051      */
1052     cpacr_write(env, ri, 0);
1053 }
1054
1055 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1056                                    bool isread)
1057 {
1058     if (arm_feature(env, ARM_FEATURE_V8)) {
1059         /* Check if CPACR accesses are to be trapped to EL2 */
1060         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
1061             (env->cp15.cptr_el[2] & CPTR_TCPAC)) {
1062             return CP_ACCESS_TRAP_EL2;
1063         /* Check if CPACR accesses are to be trapped to EL3 */
1064         } else if (arm_current_el(env) < 3 &&
1065                    (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1066             return CP_ACCESS_TRAP_EL3;
1067         }
1068     }
1069
1070     return CP_ACCESS_OK;
1071 }
1072
1073 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1074                                   bool isread)
1075 {
1076     /* Check if CPTR accesses are set to trap to EL3 */
1077     if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1078         return CP_ACCESS_TRAP_EL3;
1079     }
1080
1081     return CP_ACCESS_OK;
1082 }
1083
1084 static const ARMCPRegInfo v6_cp_reginfo[] = {
1085     /* prefetch by MVA in v6, NOP in v7 */
1086     { .name = "MVA_prefetch",
1087       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
1088       .access = PL1_W, .type = ARM_CP_NOP },
1089     /* We need to break the TB after ISB to execute self-modifying code
1090      * correctly and also to take any pending interrupts immediately.
1091      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
1092      */
1093     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
1094       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
1095     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
1096       .access = PL0_W, .type = ARM_CP_NOP },
1097     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
1098       .access = PL0_W, .type = ARM_CP_NOP },
1099     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
1100       .access = PL1_RW, .accessfn = access_tvm_trvm,
1101       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1102                              offsetof(CPUARMState, cp15.ifar_ns) },
1103       .resetvalue = 0, },
1104     /* Watchpoint Fault Address Register : should actually only be present
1105      * for 1136, 1176, 11MPCore.
1106      */
1107     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1108       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
1109     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
1110       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
1111       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
1112       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
1113     REGINFO_SENTINEL
1114 };
1115
1116 /* Definitions for the PMU registers */
1117 #define PMCRN_MASK  0xf800
1118 #define PMCRN_SHIFT 11
1119 #define PMCRLC  0x40
1120 #define PMCRDP  0x20
1121 #define PMCRX   0x10
1122 #define PMCRD   0x8
1123 #define PMCRC   0x4
1124 #define PMCRP   0x2
1125 #define PMCRE   0x1
1126 /*
1127  * Mask of PMCR bits writeable by guest (not including WO bits like C, P,
1128  * which can be written as 1 to trigger behaviour but which stay RAZ).
1129  */
1130 #define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE)
1131
1132 #define PMXEVTYPER_P          0x80000000
1133 #define PMXEVTYPER_U          0x40000000
1134 #define PMXEVTYPER_NSK        0x20000000
1135 #define PMXEVTYPER_NSU        0x10000000
1136 #define PMXEVTYPER_NSH        0x08000000
1137 #define PMXEVTYPER_M          0x04000000
1138 #define PMXEVTYPER_MT         0x02000000
1139 #define PMXEVTYPER_EVTCOUNT   0x0000ffff
1140 #define PMXEVTYPER_MASK       (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1141                                PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1142                                PMXEVTYPER_M | PMXEVTYPER_MT | \
1143                                PMXEVTYPER_EVTCOUNT)
1144
1145 #define PMCCFILTR             0xf8000000
1146 #define PMCCFILTR_M           PMXEVTYPER_M
1147 #define PMCCFILTR_EL0         (PMCCFILTR | PMCCFILTR_M)
1148
1149 static inline uint32_t pmu_num_counters(CPUARMState *env)
1150 {
1151   return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1152 }
1153
1154 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1155 static inline uint64_t pmu_counter_mask(CPUARMState *env)
1156 {
1157   return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1158 }
1159
1160 typedef struct pm_event {
1161     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1162     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1163     bool (*supported)(CPUARMState *);
1164     /*
1165      * Retrieve the current count of the underlying event. The programmed
1166      * counters hold a difference from the return value from this function
1167      */
1168     uint64_t (*get_count)(CPUARMState *);
1169     /*
1170      * Return how many nanoseconds it will take (at a minimum) for count events
1171      * to occur. A negative value indicates the counter will never overflow, or
1172      * that the counter has otherwise arranged for the overflow bit to be set
1173      * and the PMU interrupt to be raised on overflow.
1174      */
1175     int64_t (*ns_per_count)(uint64_t);
1176 } pm_event;
1177
1178 static bool event_always_supported(CPUARMState *env)
1179 {
1180     return true;
1181 }
1182
1183 static uint64_t swinc_get_count(CPUARMState *env)
1184 {
1185     /*
1186      * SW_INCR events are written directly to the pmevcntr's by writes to
1187      * PMSWINC, so there is no underlying count maintained by the PMU itself
1188      */
1189     return 0;
1190 }
1191
1192 static int64_t swinc_ns_per(uint64_t ignored)
1193 {
1194     return -1;
1195 }
1196
1197 /*
1198  * Return the underlying cycle count for the PMU cycle counters. If we're in
1199  * usermode, simply return 0.
1200  */
1201 static uint64_t cycles_get_count(CPUARMState *env)
1202 {
1203 #ifndef CONFIG_USER_ONLY
1204     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1205                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1206 #else
1207     return cpu_get_host_ticks();
1208 #endif
1209 }
1210
1211 #ifndef CONFIG_USER_ONLY
1212 static int64_t cycles_ns_per(uint64_t cycles)
1213 {
1214     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1215 }
1216
1217 static bool instructions_supported(CPUARMState *env)
1218 {
1219     return icount_enabled() == 1; /* Precise instruction counting */
1220 }
1221
1222 static uint64_t instructions_get_count(CPUARMState *env)
1223 {
1224     return (uint64_t)icount_get_raw();
1225 }
1226
1227 static int64_t instructions_ns_per(uint64_t icount)
1228 {
1229     return icount_to_ns((int64_t)icount);
1230 }
1231 #endif
1232
1233 static bool pmu_8_1_events_supported(CPUARMState *env)
1234 {
1235     /* For events which are supported in any v8.1 PMU */
1236     return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
1237 }
1238
1239 static bool pmu_8_4_events_supported(CPUARMState *env)
1240 {
1241     /* For events which are supported in any v8.1 PMU */
1242     return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
1243 }
1244
1245 static uint64_t zero_event_get_count(CPUARMState *env)
1246 {
1247     /* For events which on QEMU never fire, so their count is always zero */
1248     return 0;
1249 }
1250
1251 static int64_t zero_event_ns_per(uint64_t cycles)
1252 {
1253     /* An event which never fires can never overflow */
1254     return -1;
1255 }
1256
1257 static const pm_event pm_events[] = {
1258     { .number = 0x000, /* SW_INCR */
1259       .supported = event_always_supported,
1260       .get_count = swinc_get_count,
1261       .ns_per_count = swinc_ns_per,
1262     },
1263 #ifndef CONFIG_USER_ONLY
1264     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1265       .supported = instructions_supported,
1266       .get_count = instructions_get_count,
1267       .ns_per_count = instructions_ns_per,
1268     },
1269     { .number = 0x011, /* CPU_CYCLES, Cycle */
1270       .supported = event_always_supported,
1271       .get_count = cycles_get_count,
1272       .ns_per_count = cycles_ns_per,
1273     },
1274 #endif
1275     { .number = 0x023, /* STALL_FRONTEND */
1276       .supported = pmu_8_1_events_supported,
1277       .get_count = zero_event_get_count,
1278       .ns_per_count = zero_event_ns_per,
1279     },
1280     { .number = 0x024, /* STALL_BACKEND */
1281       .supported = pmu_8_1_events_supported,
1282       .get_count = zero_event_get_count,
1283       .ns_per_count = zero_event_ns_per,
1284     },
1285     { .number = 0x03c, /* STALL */
1286       .supported = pmu_8_4_events_supported,
1287       .get_count = zero_event_get_count,
1288       .ns_per_count = zero_event_ns_per,
1289     },
1290 };
1291
1292 /*
1293  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1294  * events (i.e. the statistical profiling extension), this implementation
1295  * should first be updated to something sparse instead of the current
1296  * supported_event_map[] array.
1297  */
1298 #define MAX_EVENT_ID 0x3c
1299 #define UNSUPPORTED_EVENT UINT16_MAX
1300 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1301
1302 /*
1303  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1304  * of ARM event numbers to indices in our pm_events array.
1305  *
1306  * Note: Events in the 0x40XX range are not currently supported.
1307  */
1308 void pmu_init(ARMCPU *cpu)
1309 {
1310     unsigned int i;
1311
1312     /*
1313      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1314      * events to them
1315      */
1316     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1317         supported_event_map[i] = UNSUPPORTED_EVENT;
1318     }
1319     cpu->pmceid0 = 0;
1320     cpu->pmceid1 = 0;
1321
1322     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1323         const pm_event *cnt = &pm_events[i];
1324         assert(cnt->number <= MAX_EVENT_ID);
1325         /* We do not currently support events in the 0x40xx range */
1326         assert(cnt->number <= 0x3f);
1327
1328         if (cnt->supported(&cpu->env)) {
1329             supported_event_map[cnt->number] = i;
1330             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1331             if (cnt->number & 0x20) {
1332                 cpu->pmceid1 |= event_mask;
1333             } else {
1334                 cpu->pmceid0 |= event_mask;
1335             }
1336         }
1337     }
1338 }
1339
1340 /*
1341  * Check at runtime whether a PMU event is supported for the current machine
1342  */
1343 static bool event_supported(uint16_t number)
1344 {
1345     if (number > MAX_EVENT_ID) {
1346         return false;
1347     }
1348     return supported_event_map[number] != UNSUPPORTED_EVENT;
1349 }
1350
1351 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1352                                    bool isread)
1353 {
1354     /* Performance monitor registers user accessibility is controlled
1355      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1356      * trapping to EL2 or EL3 for other accesses.
1357      */
1358     int el = arm_current_el(env);
1359     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1360
1361     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1362         return CP_ACCESS_TRAP;
1363     }
1364     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1365         return CP_ACCESS_TRAP_EL2;
1366     }
1367     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1368         return CP_ACCESS_TRAP_EL3;
1369     }
1370
1371     return CP_ACCESS_OK;
1372 }
1373
1374 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1375                                            const ARMCPRegInfo *ri,
1376                                            bool isread)
1377 {
1378     /* ER: event counter read trap control */
1379     if (arm_feature(env, ARM_FEATURE_V8)
1380         && arm_current_el(env) == 0
1381         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1382         && isread) {
1383         return CP_ACCESS_OK;
1384     }
1385
1386     return pmreg_access(env, ri, isread);
1387 }
1388
1389 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1390                                          const ARMCPRegInfo *ri,
1391                                          bool isread)
1392 {
1393     /* SW: software increment write trap control */
1394     if (arm_feature(env, ARM_FEATURE_V8)
1395         && arm_current_el(env) == 0
1396         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1397         && !isread) {
1398         return CP_ACCESS_OK;
1399     }
1400
1401     return pmreg_access(env, ri, isread);
1402 }
1403
1404 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1405                                         const ARMCPRegInfo *ri,
1406                                         bool isread)
1407 {
1408     /* ER: event counter read trap control */
1409     if (arm_feature(env, ARM_FEATURE_V8)
1410         && arm_current_el(env) == 0
1411         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1412         return CP_ACCESS_OK;
1413     }
1414
1415     return pmreg_access(env, ri, isread);
1416 }
1417
1418 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1419                                          const ARMCPRegInfo *ri,
1420                                          bool isread)
1421 {
1422     /* CR: cycle counter read trap control */
1423     if (arm_feature(env, ARM_FEATURE_V8)
1424         && arm_current_el(env) == 0
1425         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1426         && isread) {
1427         return CP_ACCESS_OK;
1428     }
1429
1430     return pmreg_access(env, ri, isread);
1431 }
1432
1433 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1434  * the current EL, security state, and register configuration.
1435  */
1436 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1437 {
1438     uint64_t filter;
1439     bool e, p, u, nsk, nsu, nsh, m;
1440     bool enabled, prohibited, filtered;
1441     bool secure = arm_is_secure(env);
1442     int el = arm_current_el(env);
1443     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1444     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1445
1446     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1447         return false;
1448     }
1449
1450     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1451             (counter < hpmn || counter == 31)) {
1452         e = env->cp15.c9_pmcr & PMCRE;
1453     } else {
1454         e = mdcr_el2 & MDCR_HPME;
1455     }
1456     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1457
1458     if (!secure) {
1459         if (el == 2 && (counter < hpmn || counter == 31)) {
1460             prohibited = mdcr_el2 & MDCR_HPMD;
1461         } else {
1462             prohibited = false;
1463         }
1464     } else {
1465         prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1466            !(env->cp15.mdcr_el3 & MDCR_SPME);
1467     }
1468
1469     if (prohibited && counter == 31) {
1470         prohibited = env->cp15.c9_pmcr & PMCRDP;
1471     }
1472
1473     if (counter == 31) {
1474         filter = env->cp15.pmccfiltr_el0;
1475     } else {
1476         filter = env->cp15.c14_pmevtyper[counter];
1477     }
1478
1479     p   = filter & PMXEVTYPER_P;
1480     u   = filter & PMXEVTYPER_U;
1481     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1482     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1483     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1484     m   = arm_el_is_aa64(env, 1) &&
1485               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1486
1487     if (el == 0) {
1488         filtered = secure ? u : u != nsu;
1489     } else if (el == 1) {
1490         filtered = secure ? p : p != nsk;
1491     } else if (el == 2) {
1492         filtered = !nsh;
1493     } else { /* EL3 */
1494         filtered = m != p;
1495     }
1496
1497     if (counter != 31) {
1498         /*
1499          * If not checking PMCCNTR, ensure the counter is setup to an event we
1500          * support
1501          */
1502         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1503         if (!event_supported(event)) {
1504             return false;
1505         }
1506     }
1507
1508     return enabled && !prohibited && !filtered;
1509 }
1510
1511 static void pmu_update_irq(CPUARMState *env)
1512 {
1513     ARMCPU *cpu = env_archcpu(env);
1514     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1515             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1516 }
1517
1518 /*
1519  * Ensure c15_ccnt is the guest-visible count so that operations such as
1520  * enabling/disabling the counter or filtering, modifying the count itself,
1521  * etc. can be done logically. This is essentially a no-op if the counter is
1522  * not enabled at the time of the call.
1523  */
1524 static void pmccntr_op_start(CPUARMState *env)
1525 {
1526     uint64_t cycles = cycles_get_count(env);
1527
1528     if (pmu_counter_enabled(env, 31)) {
1529         uint64_t eff_cycles = cycles;
1530         if (env->cp15.c9_pmcr & PMCRD) {
1531             /* Increment once every 64 processor clock cycles */
1532             eff_cycles /= 64;
1533         }
1534
1535         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1536
1537         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1538                                  1ull << 63 : 1ull << 31;
1539         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1540             env->cp15.c9_pmovsr |= (1 << 31);
1541             pmu_update_irq(env);
1542         }
1543
1544         env->cp15.c15_ccnt = new_pmccntr;
1545     }
1546     env->cp15.c15_ccnt_delta = cycles;
1547 }
1548
1549 /*
1550  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1551  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1552  * pmccntr_op_start.
1553  */
1554 static void pmccntr_op_finish(CPUARMState *env)
1555 {
1556     if (pmu_counter_enabled(env, 31)) {
1557 #ifndef CONFIG_USER_ONLY
1558         /* Calculate when the counter will next overflow */
1559         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1560         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1561             remaining_cycles = (uint32_t)remaining_cycles;
1562         }
1563         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1564
1565         if (overflow_in > 0) {
1566             int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1567                 overflow_in;
1568             ARMCPU *cpu = env_archcpu(env);
1569             timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1570         }
1571 #endif
1572
1573         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1574         if (env->cp15.c9_pmcr & PMCRD) {
1575             /* Increment once every 64 processor clock cycles */
1576             prev_cycles /= 64;
1577         }
1578         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1579     }
1580 }
1581
1582 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1583 {
1584
1585     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1586     uint64_t count = 0;
1587     if (event_supported(event)) {
1588         uint16_t event_idx = supported_event_map[event];
1589         count = pm_events[event_idx].get_count(env);
1590     }
1591
1592     if (pmu_counter_enabled(env, counter)) {
1593         uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1594
1595         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1596             env->cp15.c9_pmovsr |= (1 << counter);
1597             pmu_update_irq(env);
1598         }
1599         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1600     }
1601     env->cp15.c14_pmevcntr_delta[counter] = count;
1602 }
1603
1604 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1605 {
1606     if (pmu_counter_enabled(env, counter)) {
1607 #ifndef CONFIG_USER_ONLY
1608         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1609         uint16_t event_idx = supported_event_map[event];
1610         uint64_t delta = UINT32_MAX -
1611             (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1612         int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1613
1614         if (overflow_in > 0) {
1615             int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1616                 overflow_in;
1617             ARMCPU *cpu = env_archcpu(env);
1618             timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1619         }
1620 #endif
1621
1622         env->cp15.c14_pmevcntr_delta[counter] -=
1623             env->cp15.c14_pmevcntr[counter];
1624     }
1625 }
1626
1627 void pmu_op_start(CPUARMState *env)
1628 {
1629     unsigned int i;
1630     pmccntr_op_start(env);
1631     for (i = 0; i < pmu_num_counters(env); i++) {
1632         pmevcntr_op_start(env, i);
1633     }
1634 }
1635
1636 void pmu_op_finish(CPUARMState *env)
1637 {
1638     unsigned int i;
1639     pmccntr_op_finish(env);
1640     for (i = 0; i < pmu_num_counters(env); i++) {
1641         pmevcntr_op_finish(env, i);
1642     }
1643 }
1644
1645 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1646 {
1647     pmu_op_start(&cpu->env);
1648 }
1649
1650 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1651 {
1652     pmu_op_finish(&cpu->env);
1653 }
1654
1655 void arm_pmu_timer_cb(void *opaque)
1656 {
1657     ARMCPU *cpu = opaque;
1658
1659     /*
1660      * Update all the counter values based on the current underlying counts,
1661      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1662      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1663      * counter may expire.
1664      */
1665     pmu_op_start(&cpu->env);
1666     pmu_op_finish(&cpu->env);
1667 }
1668
1669 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1670                        uint64_t value)
1671 {
1672     pmu_op_start(env);
1673
1674     if (value & PMCRC) {
1675         /* The counter has been reset */
1676         env->cp15.c15_ccnt = 0;
1677     }
1678
1679     if (value & PMCRP) {
1680         unsigned int i;
1681         for (i = 0; i < pmu_num_counters(env); i++) {
1682             env->cp15.c14_pmevcntr[i] = 0;
1683         }
1684     }
1685
1686     env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1687     env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
1688
1689     pmu_op_finish(env);
1690 }
1691
1692 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1693                           uint64_t value)
1694 {
1695     unsigned int i;
1696     for (i = 0; i < pmu_num_counters(env); i++) {
1697         /* Increment a counter's count iff: */
1698         if ((value & (1 << i)) && /* counter's bit is set */
1699                 /* counter is enabled and not filtered */
1700                 pmu_counter_enabled(env, i) &&
1701                 /* counter is SW_INCR */
1702                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1703             pmevcntr_op_start(env, i);
1704
1705             /*
1706              * Detect if this write causes an overflow since we can't predict
1707              * PMSWINC overflows like we can for other events
1708              */
1709             uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1710
1711             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1712                 env->cp15.c9_pmovsr |= (1 << i);
1713                 pmu_update_irq(env);
1714             }
1715
1716             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1717
1718             pmevcntr_op_finish(env, i);
1719         }
1720     }
1721 }
1722
1723 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1724 {
1725     uint64_t ret;
1726     pmccntr_op_start(env);
1727     ret = env->cp15.c15_ccnt;
1728     pmccntr_op_finish(env);
1729     return ret;
1730 }
1731
1732 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1733                          uint64_t value)
1734 {
1735     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1736      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1737      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1738      * accessed.
1739      */
1740     env->cp15.c9_pmselr = value & 0x1f;
1741 }
1742
1743 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1744                         uint64_t value)
1745 {
1746     pmccntr_op_start(env);
1747     env->cp15.c15_ccnt = value;
1748     pmccntr_op_finish(env);
1749 }
1750
1751 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1752                             uint64_t value)
1753 {
1754     uint64_t cur_val = pmccntr_read(env, NULL);
1755
1756     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1757 }
1758
1759 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1760                             uint64_t value)
1761 {
1762     pmccntr_op_start(env);
1763     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1764     pmccntr_op_finish(env);
1765 }
1766
1767 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1768                             uint64_t value)
1769 {
1770     pmccntr_op_start(env);
1771     /* M is not accessible from AArch32 */
1772     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1773         (value & PMCCFILTR);
1774     pmccntr_op_finish(env);
1775 }
1776
1777 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1778 {
1779     /* M is not visible in AArch32 */
1780     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1781 }
1782
1783 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1784                             uint64_t value)
1785 {
1786     value &= pmu_counter_mask(env);
1787     env->cp15.c9_pmcnten |= value;
1788 }
1789
1790 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1791                              uint64_t value)
1792 {
1793     value &= pmu_counter_mask(env);
1794     env->cp15.c9_pmcnten &= ~value;
1795 }
1796
1797 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1798                          uint64_t value)
1799 {
1800     value &= pmu_counter_mask(env);
1801     env->cp15.c9_pmovsr &= ~value;
1802     pmu_update_irq(env);
1803 }
1804
1805 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1806                          uint64_t value)
1807 {
1808     value &= pmu_counter_mask(env);
1809     env->cp15.c9_pmovsr |= value;
1810     pmu_update_irq(env);
1811 }
1812
1813 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1814                              uint64_t value, const uint8_t counter)
1815 {
1816     if (counter == 31) {
1817         pmccfiltr_write(env, ri, value);
1818     } else if (counter < pmu_num_counters(env)) {
1819         pmevcntr_op_start(env, counter);
1820
1821         /*
1822          * If this counter's event type is changing, store the current
1823          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1824          * pmevcntr_op_finish has the correct baseline when it converts back to
1825          * a delta.
1826          */
1827         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1828             PMXEVTYPER_EVTCOUNT;
1829         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1830         if (old_event != new_event) {
1831             uint64_t count = 0;
1832             if (event_supported(new_event)) {
1833                 uint16_t event_idx = supported_event_map[new_event];
1834                 count = pm_events[event_idx].get_count(env);
1835             }
1836             env->cp15.c14_pmevcntr_delta[counter] = count;
1837         }
1838
1839         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1840         pmevcntr_op_finish(env, counter);
1841     }
1842     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1843      * PMSELR value is equal to or greater than the number of implemented
1844      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1845      */
1846 }
1847
1848 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1849                                const uint8_t counter)
1850 {
1851     if (counter == 31) {
1852         return env->cp15.pmccfiltr_el0;
1853     } else if (counter < pmu_num_counters(env)) {
1854         return env->cp15.c14_pmevtyper[counter];
1855     } else {
1856       /*
1857        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1858        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1859        */
1860         return 0;
1861     }
1862 }
1863
1864 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1865                               uint64_t value)
1866 {
1867     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1868     pmevtyper_write(env, ri, value, counter);
1869 }
1870
1871 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1872                                uint64_t value)
1873 {
1874     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1875     env->cp15.c14_pmevtyper[counter] = value;
1876
1877     /*
1878      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1879      * pmu_op_finish calls when loading saved state for a migration. Because
1880      * we're potentially updating the type of event here, the value written to
1881      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1882      * different counter type. Therefore, we need to set this value to the
1883      * current count for the counter type we're writing so that pmu_op_finish
1884      * has the correct count for its calculation.
1885      */
1886     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1887     if (event_supported(event)) {
1888         uint16_t event_idx = supported_event_map[event];
1889         env->cp15.c14_pmevcntr_delta[counter] =
1890             pm_events[event_idx].get_count(env);
1891     }
1892 }
1893
1894 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1895 {
1896     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1897     return pmevtyper_read(env, ri, counter);
1898 }
1899
1900 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1901                              uint64_t value)
1902 {
1903     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1904 }
1905
1906 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1907 {
1908     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1909 }
1910
1911 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1912                              uint64_t value, uint8_t counter)
1913 {
1914     if (counter < pmu_num_counters(env)) {
1915         pmevcntr_op_start(env, counter);
1916         env->cp15.c14_pmevcntr[counter] = value;
1917         pmevcntr_op_finish(env, counter);
1918     }
1919     /*
1920      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1921      * are CONSTRAINED UNPREDICTABLE.
1922      */
1923 }
1924
1925 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1926                               uint8_t counter)
1927 {
1928     if (counter < pmu_num_counters(env)) {
1929         uint64_t ret;
1930         pmevcntr_op_start(env, counter);
1931         ret = env->cp15.c14_pmevcntr[counter];
1932         pmevcntr_op_finish(env, counter);
1933         return ret;
1934     } else {
1935       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1936        * are CONSTRAINED UNPREDICTABLE. */
1937         return 0;
1938     }
1939 }
1940
1941 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1942                              uint64_t value)
1943 {
1944     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1945     pmevcntr_write(env, ri, value, counter);
1946 }
1947
1948 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1949 {
1950     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1951     return pmevcntr_read(env, ri, counter);
1952 }
1953
1954 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1955                              uint64_t value)
1956 {
1957     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1958     assert(counter < pmu_num_counters(env));
1959     env->cp15.c14_pmevcntr[counter] = value;
1960     pmevcntr_write(env, ri, value, counter);
1961 }
1962
1963 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1964 {
1965     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1966     assert(counter < pmu_num_counters(env));
1967     return env->cp15.c14_pmevcntr[counter];
1968 }
1969
1970 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1971                              uint64_t value)
1972 {
1973     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1974 }
1975
1976 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1977 {
1978     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1979 }
1980
1981 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1982                             uint64_t value)
1983 {
1984     if (arm_feature(env, ARM_FEATURE_V8)) {
1985         env->cp15.c9_pmuserenr = value & 0xf;
1986     } else {
1987         env->cp15.c9_pmuserenr = value & 1;
1988     }
1989 }
1990
1991 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1992                              uint64_t value)
1993 {
1994     /* We have no event counters so only the C bit can be changed */
1995     value &= pmu_counter_mask(env);
1996     env->cp15.c9_pminten |= value;
1997     pmu_update_irq(env);
1998 }
1999
2000 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2001                              uint64_t value)
2002 {
2003     value &= pmu_counter_mask(env);
2004     env->cp15.c9_pminten &= ~value;
2005     pmu_update_irq(env);
2006 }
2007
2008 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2009                        uint64_t value)
2010 {
2011     /* Note that even though the AArch64 view of this register has bits
2012      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
2013      * architectural requirements for bits which are RES0 only in some
2014      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
2015      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
2016      */
2017     raw_write(env, ri, value & ~0x1FULL);
2018 }
2019
2020 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2021 {
2022     /* Begin with base v8.0 state.  */
2023     uint32_t valid_mask = 0x3fff;
2024     ARMCPU *cpu = env_archcpu(env);
2025
2026     if (ri->state == ARM_CP_STATE_AA64) {
2027         if (arm_feature(env, ARM_FEATURE_AARCH64) &&
2028             !cpu_isar_feature(aa64_aa32_el1, cpu)) {
2029                 value |= SCR_FW | SCR_AW;   /* these two bits are RES1.  */
2030         }
2031         valid_mask &= ~SCR_NET;
2032
2033         if (cpu_isar_feature(aa64_lor, cpu)) {
2034             valid_mask |= SCR_TLOR;
2035         }
2036         if (cpu_isar_feature(aa64_pauth, cpu)) {
2037             valid_mask |= SCR_API | SCR_APK;
2038         }
2039         if (cpu_isar_feature(aa64_sel2, cpu)) {
2040             valid_mask |= SCR_EEL2;
2041         }
2042         if (cpu_isar_feature(aa64_mte, cpu)) {
2043             valid_mask |= SCR_ATA;
2044         }
2045     } else {
2046         valid_mask &= ~(SCR_RW | SCR_ST);
2047     }
2048
2049     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2050         valid_mask &= ~SCR_HCE;
2051
2052         /* On ARMv7, SMD (or SCD as it is called in v7) is only
2053          * supported if EL2 exists. The bit is UNK/SBZP when
2054          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
2055          * when EL2 is unavailable.
2056          * On ARMv8, this bit is always available.
2057          */
2058         if (arm_feature(env, ARM_FEATURE_V7) &&
2059             !arm_feature(env, ARM_FEATURE_V8)) {
2060             valid_mask &= ~SCR_SMD;
2061         }
2062     }
2063
2064     /* Clear all-context RES0 bits.  */
2065     value &= valid_mask;
2066     raw_write(env, ri, value);
2067 }
2068
2069 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2070 {
2071     /*
2072      * scr_write will set the RES1 bits on an AArch64-only CPU.
2073      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
2074      */
2075     scr_write(env, ri, 0);
2076 }
2077
2078 static CPAccessResult access_aa64_tid2(CPUARMState *env,
2079                                        const ARMCPRegInfo *ri,
2080                                        bool isread)
2081 {
2082     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
2083         return CP_ACCESS_TRAP_EL2;
2084     }
2085
2086     return CP_ACCESS_OK;
2087 }
2088
2089 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2090 {
2091     ARMCPU *cpu = env_archcpu(env);
2092
2093     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
2094      * bank
2095      */
2096     uint32_t index = A32_BANKED_REG_GET(env, csselr,
2097                                         ri->secure & ARM_CP_SECSTATE_S);
2098
2099     return cpu->ccsidr[index];
2100 }
2101
2102 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2103                          uint64_t value)
2104 {
2105     raw_write(env, ri, value & 0xf);
2106 }
2107
2108 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2109 {
2110     CPUState *cs = env_cpu(env);
2111     bool el1 = arm_current_el(env) == 1;
2112     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
2113     uint64_t ret = 0;
2114
2115     if (hcr_el2 & HCR_IMO) {
2116         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2117             ret |= CPSR_I;
2118         }
2119     } else {
2120         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2121             ret |= CPSR_I;
2122         }
2123     }
2124
2125     if (hcr_el2 & HCR_FMO) {
2126         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2127             ret |= CPSR_F;
2128         }
2129     } else {
2130         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2131             ret |= CPSR_F;
2132         }
2133     }
2134
2135     /* External aborts are not possible in QEMU so A bit is always clear */
2136     return ret;
2137 }
2138
2139 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2140                                        bool isread)
2141 {
2142     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2143         return CP_ACCESS_TRAP_EL2;
2144     }
2145
2146     return CP_ACCESS_OK;
2147 }
2148
2149 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2150                                        bool isread)
2151 {
2152     if (arm_feature(env, ARM_FEATURE_V8)) {
2153         return access_aa64_tid1(env, ri, isread);
2154     }
2155
2156     return CP_ACCESS_OK;
2157 }
2158
2159 static const ARMCPRegInfo v7_cp_reginfo[] = {
2160     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2161     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2162       .access = PL1_W, .type = ARM_CP_NOP },
2163     /* Performance monitors are implementation defined in v7,
2164      * but with an ARM recommended set of registers, which we
2165      * follow.
2166      *
2167      * Performance registers fall into three categories:
2168      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2169      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2170      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2171      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2172      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2173      */
2174     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2175       .access = PL0_RW, .type = ARM_CP_ALIAS,
2176       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2177       .writefn = pmcntenset_write,
2178       .accessfn = pmreg_access,
2179       .raw_writefn = raw_write },
2180     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2181       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2182       .access = PL0_RW, .accessfn = pmreg_access,
2183       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2184       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2185     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2186       .access = PL0_RW,
2187       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2188       .accessfn = pmreg_access,
2189       .writefn = pmcntenclr_write,
2190       .type = ARM_CP_ALIAS },
2191     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2192       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2193       .access = PL0_RW, .accessfn = pmreg_access,
2194       .type = ARM_CP_ALIAS,
2195       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2196       .writefn = pmcntenclr_write },
2197     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2198       .access = PL0_RW, .type = ARM_CP_IO,
2199       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2200       .accessfn = pmreg_access,
2201       .writefn = pmovsr_write,
2202       .raw_writefn = raw_write },
2203     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2204       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2205       .access = PL0_RW, .accessfn = pmreg_access,
2206       .type = ARM_CP_ALIAS | ARM_CP_IO,
2207       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2208       .writefn = pmovsr_write,
2209       .raw_writefn = raw_write },
2210     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2211       .access = PL0_W, .accessfn = pmreg_access_swinc,
2212       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2213       .writefn = pmswinc_write },
2214     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2215       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2216       .access = PL0_W, .accessfn = pmreg_access_swinc,
2217       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2218       .writefn = pmswinc_write },
2219     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2220       .access = PL0_RW, .type = ARM_CP_ALIAS,
2221       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2222       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2223       .raw_writefn = raw_write},
2224     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2225       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2226       .access = PL0_RW, .accessfn = pmreg_access_selr,
2227       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2228       .writefn = pmselr_write, .raw_writefn = raw_write, },
2229     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2230       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2231       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2232       .accessfn = pmreg_access_ccntr },
2233     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2234       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2235       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2236       .type = ARM_CP_IO,
2237       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2238       .readfn = pmccntr_read, .writefn = pmccntr_write,
2239       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2240     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2241       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2242       .access = PL0_RW, .accessfn = pmreg_access,
2243       .type = ARM_CP_ALIAS | ARM_CP_IO,
2244       .resetvalue = 0, },
2245     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2246       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2247       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2248       .access = PL0_RW, .accessfn = pmreg_access,
2249       .type = ARM_CP_IO,
2250       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2251       .resetvalue = 0, },
2252     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2253       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2254       .accessfn = pmreg_access,
2255       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2256     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2257       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2258       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2259       .accessfn = pmreg_access,
2260       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2261     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2262       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2263       .accessfn = pmreg_access_xevcntr,
2264       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2265     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2266       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2267       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2268       .accessfn = pmreg_access_xevcntr,
2269       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2270     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2271       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2272       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2273       .resetvalue = 0,
2274       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2275     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2276       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2277       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2278       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2279       .resetvalue = 0,
2280       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2281     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2282       .access = PL1_RW, .accessfn = access_tpm,
2283       .type = ARM_CP_ALIAS | ARM_CP_IO,
2284       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2285       .resetvalue = 0,
2286       .writefn = pmintenset_write, .raw_writefn = raw_write },
2287     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2288       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2289       .access = PL1_RW, .accessfn = access_tpm,
2290       .type = ARM_CP_IO,
2291       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2292       .writefn = pmintenset_write, .raw_writefn = raw_write,
2293       .resetvalue = 0x0 },
2294     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2295       .access = PL1_RW, .accessfn = access_tpm,
2296       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2297       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2298       .writefn = pmintenclr_write, },
2299     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2300       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2301       .access = PL1_RW, .accessfn = access_tpm,
2302       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2303       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2304       .writefn = pmintenclr_write },
2305     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2306       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2307       .access = PL1_R,
2308       .accessfn = access_aa64_tid2,
2309       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2310     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2311       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2312       .access = PL1_RW,
2313       .accessfn = access_aa64_tid2,
2314       .writefn = csselr_write, .resetvalue = 0,
2315       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2316                              offsetof(CPUARMState, cp15.csselr_ns) } },
2317     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2318      * just RAZ for all cores:
2319      */
2320     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2321       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2322       .access = PL1_R, .type = ARM_CP_CONST,
2323       .accessfn = access_aa64_tid1,
2324       .resetvalue = 0 },
2325     /* Auxiliary fault status registers: these also are IMPDEF, and we
2326      * choose to RAZ/WI for all cores.
2327      */
2328     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2329       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2330       .access = PL1_RW, .accessfn = access_tvm_trvm,
2331       .type = ARM_CP_CONST, .resetvalue = 0 },
2332     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2333       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2334       .access = PL1_RW, .accessfn = access_tvm_trvm,
2335       .type = ARM_CP_CONST, .resetvalue = 0 },
2336     /* MAIR can just read-as-written because we don't implement caches
2337      * and so don't need to care about memory attributes.
2338      */
2339     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2340       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2341       .access = PL1_RW, .accessfn = access_tvm_trvm,
2342       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2343       .resetvalue = 0 },
2344     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2345       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2346       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2347       .resetvalue = 0 },
2348     /* For non-long-descriptor page tables these are PRRR and NMRR;
2349      * regardless they still act as reads-as-written for QEMU.
2350      */
2351      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2352       * allows them to assign the correct fieldoffset based on the endianness
2353       * handled in the field definitions.
2354       */
2355     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2356       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2357       .access = PL1_RW, .accessfn = access_tvm_trvm,
2358       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2359                              offsetof(CPUARMState, cp15.mair0_ns) },
2360       .resetfn = arm_cp_reset_ignore },
2361     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2362       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2363       .access = PL1_RW, .accessfn = access_tvm_trvm,
2364       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2365                              offsetof(CPUARMState, cp15.mair1_ns) },
2366       .resetfn = arm_cp_reset_ignore },
2367     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2368       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2369       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2370     /* 32 bit ITLB invalidates */
2371     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2372       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2373       .writefn = tlbiall_write },
2374     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2375       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2376       .writefn = tlbimva_write },
2377     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2378       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2379       .writefn = tlbiasid_write },
2380     /* 32 bit DTLB invalidates */
2381     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2382       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2383       .writefn = tlbiall_write },
2384     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2385       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2386       .writefn = tlbimva_write },
2387     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2388       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2389       .writefn = tlbiasid_write },
2390     /* 32 bit TLB invalidates */
2391     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2392       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2393       .writefn = tlbiall_write },
2394     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2395       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2396       .writefn = tlbimva_write },
2397     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2398       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2399       .writefn = tlbiasid_write },
2400     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2401       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2402       .writefn = tlbimvaa_write },
2403     REGINFO_SENTINEL
2404 };
2405
2406 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2407     /* 32 bit TLB invalidates, Inner Shareable */
2408     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2409       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2410       .writefn = tlbiall_is_write },
2411     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2412       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2413       .writefn = tlbimva_is_write },
2414     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2415       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2416       .writefn = tlbiasid_is_write },
2417     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2418       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2419       .writefn = tlbimvaa_is_write },
2420     REGINFO_SENTINEL
2421 };
2422
2423 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2424     /* PMOVSSET is not implemented in v7 before v7ve */
2425     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2426       .access = PL0_RW, .accessfn = pmreg_access,
2427       .type = ARM_CP_ALIAS | ARM_CP_IO,
2428       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2429       .writefn = pmovsset_write,
2430       .raw_writefn = raw_write },
2431     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2432       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2433       .access = PL0_RW, .accessfn = pmreg_access,
2434       .type = ARM_CP_ALIAS | ARM_CP_IO,
2435       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2436       .writefn = pmovsset_write,
2437       .raw_writefn = raw_write },
2438     REGINFO_SENTINEL
2439 };
2440
2441 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2442                         uint64_t value)
2443 {
2444     value &= 1;
2445     env->teecr = value;
2446 }
2447
2448 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2449                                     bool isread)
2450 {
2451     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2452         return CP_ACCESS_TRAP;
2453     }
2454     return CP_ACCESS_OK;
2455 }
2456
2457 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2458     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2459       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2460       .resetvalue = 0,
2461       .writefn = teecr_write },
2462     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2463       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2464       .accessfn = teehbr_access, .resetvalue = 0 },
2465     REGINFO_SENTINEL
2466 };
2467
2468 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2469     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2470       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2471       .access = PL0_RW,
2472       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2473     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2474       .access = PL0_RW,
2475       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2476                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2477       .resetfn = arm_cp_reset_ignore },
2478     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2479       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2480       .access = PL0_R|PL1_W,
2481       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2482       .resetvalue = 0},
2483     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2484       .access = PL0_R|PL1_W,
2485       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2486                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2487       .resetfn = arm_cp_reset_ignore },
2488     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2489       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2490       .access = PL1_RW,
2491       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2492     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2493       .access = PL1_RW,
2494       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2495                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2496       .resetvalue = 0 },
2497     REGINFO_SENTINEL
2498 };
2499
2500 #ifndef CONFIG_USER_ONLY
2501
2502 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2503                                        bool isread)
2504 {
2505     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2506      * Writable only at the highest implemented exception level.
2507      */
2508     int el = arm_current_el(env);
2509     uint64_t hcr;
2510     uint32_t cntkctl;
2511
2512     switch (el) {
2513     case 0:
2514         hcr = arm_hcr_el2_eff(env);
2515         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2516             cntkctl = env->cp15.cnthctl_el2;
2517         } else {
2518             cntkctl = env->cp15.c14_cntkctl;
2519         }
2520         if (!extract32(cntkctl, 0, 2)) {
2521             return CP_ACCESS_TRAP;
2522         }
2523         break;
2524     case 1:
2525         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2526             arm_is_secure_below_el3(env)) {
2527             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2528             return CP_ACCESS_TRAP_UNCATEGORIZED;
2529         }
2530         break;
2531     case 2:
2532     case 3:
2533         break;
2534     }
2535
2536     if (!isread && el < arm_highest_el(env)) {
2537         return CP_ACCESS_TRAP_UNCATEGORIZED;
2538     }
2539
2540     return CP_ACCESS_OK;
2541 }
2542
2543 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2544                                         bool isread)
2545 {
2546     unsigned int cur_el = arm_current_el(env);
2547     bool has_el2 = arm_is_el2_enabled(env);
2548     uint64_t hcr = arm_hcr_el2_eff(env);
2549
2550     switch (cur_el) {
2551     case 0:
2552         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2553         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2554             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2555                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2556         }
2557
2558         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2559         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2560             return CP_ACCESS_TRAP;
2561         }
2562
2563         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2564         if (hcr & HCR_E2H) {
2565             if (timeridx == GTIMER_PHYS &&
2566                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2567                 return CP_ACCESS_TRAP_EL2;
2568             }
2569         } else {
2570             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2571             if (has_el2 && timeridx == GTIMER_PHYS &&
2572                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2573                 return CP_ACCESS_TRAP_EL2;
2574             }
2575         }
2576         break;
2577
2578     case 1:
2579         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2580         if (has_el2 && timeridx == GTIMER_PHYS &&
2581             (hcr & HCR_E2H
2582              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2583              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2584             return CP_ACCESS_TRAP_EL2;
2585         }
2586         break;
2587     }
2588     return CP_ACCESS_OK;
2589 }
2590
2591 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2592                                       bool isread)
2593 {
2594     unsigned int cur_el = arm_current_el(env);
2595     bool has_el2 = arm_is_el2_enabled(env);
2596     uint64_t hcr = arm_hcr_el2_eff(env);
2597
2598     switch (cur_el) {
2599     case 0:
2600         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2601             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2602             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2603                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2604         }
2605
2606         /*
2607          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2608          * EL0 if EL0[PV]TEN is zero.
2609          */
2610         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2611             return CP_ACCESS_TRAP;
2612         }
2613         /* fall through */
2614
2615     case 1:
2616         if (has_el2 && timeridx == GTIMER_PHYS) {
2617             if (hcr & HCR_E2H) {
2618                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2619                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2620                     return CP_ACCESS_TRAP_EL2;
2621                 }
2622             } else {
2623                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2624                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2625                     return CP_ACCESS_TRAP_EL2;
2626                 }
2627             }
2628         }
2629         break;
2630     }
2631     return CP_ACCESS_OK;
2632 }
2633
2634 static CPAccessResult gt_pct_access(CPUARMState *env,
2635                                     const ARMCPRegInfo *ri,
2636                                     bool isread)
2637 {
2638     return gt_counter_access(env, GTIMER_PHYS, isread);
2639 }
2640
2641 static CPAccessResult gt_vct_access(CPUARMState *env,
2642                                     const ARMCPRegInfo *ri,
2643                                     bool isread)
2644 {
2645     return gt_counter_access(env, GTIMER_VIRT, isread);
2646 }
2647
2648 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2649                                        bool isread)
2650 {
2651     return gt_timer_access(env, GTIMER_PHYS, isread);
2652 }
2653
2654 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2655                                        bool isread)
2656 {
2657     return gt_timer_access(env, GTIMER_VIRT, isread);
2658 }
2659
2660 static CPAccessResult gt_stimer_access(CPUARMState *env,
2661                                        const ARMCPRegInfo *ri,
2662                                        bool isread)
2663 {
2664     /* The AArch64 register view of the secure physical timer is
2665      * always accessible from EL3, and configurably accessible from
2666      * Secure EL1.
2667      */
2668     switch (arm_current_el(env)) {
2669     case 1:
2670         if (!arm_is_secure(env)) {
2671             return CP_ACCESS_TRAP;
2672         }
2673         if (!(env->cp15.scr_el3 & SCR_ST)) {
2674             return CP_ACCESS_TRAP_EL3;
2675         }
2676         return CP_ACCESS_OK;
2677     case 0:
2678     case 2:
2679         return CP_ACCESS_TRAP;
2680     case 3:
2681         return CP_ACCESS_OK;
2682     default:
2683         g_assert_not_reached();
2684     }
2685 }
2686
2687 static uint64_t gt_get_countervalue(CPUARMState *env)
2688 {
2689     ARMCPU *cpu = env_archcpu(env);
2690
2691     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2692 }
2693
2694 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2695 {
2696     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2697
2698     if (gt->ctl & 1) {
2699         /* Timer enabled: calculate and set current ISTATUS, irq, and
2700          * reset timer to when ISTATUS next has to change
2701          */
2702         uint64_t offset = timeridx == GTIMER_VIRT ?
2703                                       cpu->env.cp15.cntvoff_el2 : 0;
2704         uint64_t count = gt_get_countervalue(&cpu->env);
2705         /* Note that this must be unsigned 64 bit arithmetic: */
2706         int istatus = count - offset >= gt->cval;
2707         uint64_t nexttick;
2708         int irqstate;
2709
2710         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2711
2712         irqstate = (istatus && !(gt->ctl & 2));
2713         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2714
2715         if (istatus) {
2716             /* Next transition is when count rolls back over to zero */
2717             nexttick = UINT64_MAX;
2718         } else {
2719             /* Next transition is when we hit cval */
2720             nexttick = gt->cval + offset;
2721         }
2722         /* Note that the desired next expiry time might be beyond the
2723          * signed-64-bit range of a QEMUTimer -- in this case we just
2724          * set the timer for as far in the future as possible. When the
2725          * timer expires we will reset the timer for any remaining period.
2726          */
2727         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2728             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2729         } else {
2730             timer_mod(cpu->gt_timer[timeridx], nexttick);
2731         }
2732         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2733     } else {
2734         /* Timer disabled: ISTATUS and timer output always clear */
2735         gt->ctl &= ~4;
2736         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2737         timer_del(cpu->gt_timer[timeridx]);
2738         trace_arm_gt_recalc_disabled(timeridx);
2739     }
2740 }
2741
2742 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2743                            int timeridx)
2744 {
2745     ARMCPU *cpu = env_archcpu(env);
2746
2747     timer_del(cpu->gt_timer[timeridx]);
2748 }
2749
2750 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2751 {
2752     return gt_get_countervalue(env);
2753 }
2754
2755 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2756 {
2757     uint64_t hcr;
2758
2759     switch (arm_current_el(env)) {
2760     case 2:
2761         hcr = arm_hcr_el2_eff(env);
2762         if (hcr & HCR_E2H) {
2763             return 0;
2764         }
2765         break;
2766     case 0:
2767         hcr = arm_hcr_el2_eff(env);
2768         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2769             return 0;
2770         }
2771         break;
2772     }
2773
2774     return env->cp15.cntvoff_el2;
2775 }
2776
2777 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2778 {
2779     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2780 }
2781
2782 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2783                           int timeridx,
2784                           uint64_t value)
2785 {
2786     trace_arm_gt_cval_write(timeridx, value);
2787     env->cp15.c14_timer[timeridx].cval = value;
2788     gt_recalc_timer(env_archcpu(env), timeridx);
2789 }
2790
2791 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2792                              int timeridx)
2793 {
2794     uint64_t offset = 0;
2795
2796     switch (timeridx) {
2797     case GTIMER_VIRT:
2798     case GTIMER_HYPVIRT:
2799         offset = gt_virt_cnt_offset(env);
2800         break;
2801     }
2802
2803     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2804                       (gt_get_countervalue(env) - offset));
2805 }
2806
2807 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2808                           int timeridx,
2809                           uint64_t value)
2810 {
2811     uint64_t offset = 0;
2812
2813     switch (timeridx) {
2814     case GTIMER_VIRT:
2815     case GTIMER_HYPVIRT:
2816         offset = gt_virt_cnt_offset(env);
2817         break;
2818     }
2819
2820     trace_arm_gt_tval_write(timeridx, value);
2821     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2822                                          sextract64(value, 0, 32);
2823     gt_recalc_timer(env_archcpu(env), timeridx);
2824 }
2825
2826 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2827                          int timeridx,
2828                          uint64_t value)
2829 {
2830     ARMCPU *cpu = env_archcpu(env);
2831     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2832
2833     trace_arm_gt_ctl_write(timeridx, value);
2834     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2835     if ((oldval ^ value) & 1) {
2836         /* Enable toggled */
2837         gt_recalc_timer(cpu, timeridx);
2838     } else if ((oldval ^ value) & 2) {
2839         /* IMASK toggled: don't need to recalculate,
2840          * just set the interrupt line based on ISTATUS
2841          */
2842         int irqstate = (oldval & 4) && !(value & 2);
2843
2844         trace_arm_gt_imask_toggle(timeridx, irqstate);
2845         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2846     }
2847 }
2848
2849 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2850 {
2851     gt_timer_reset(env, ri, GTIMER_PHYS);
2852 }
2853
2854 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2855                                uint64_t value)
2856 {
2857     gt_cval_write(env, ri, GTIMER_PHYS, value);
2858 }
2859
2860 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2861 {
2862     return gt_tval_read(env, ri, GTIMER_PHYS);
2863 }
2864
2865 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2866                                uint64_t value)
2867 {
2868     gt_tval_write(env, ri, GTIMER_PHYS, value);
2869 }
2870
2871 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2872                               uint64_t value)
2873 {
2874     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2875 }
2876
2877 static int gt_phys_redir_timeridx(CPUARMState *env)
2878 {
2879     switch (arm_mmu_idx(env)) {
2880     case ARMMMUIdx_E20_0:
2881     case ARMMMUIdx_E20_2:
2882     case ARMMMUIdx_E20_2_PAN:
2883     case ARMMMUIdx_SE20_0:
2884     case ARMMMUIdx_SE20_2:
2885     case ARMMMUIdx_SE20_2_PAN:
2886         return GTIMER_HYP;
2887     default:
2888         return GTIMER_PHYS;
2889     }
2890 }
2891
2892 static int gt_virt_redir_timeridx(CPUARMState *env)
2893 {
2894     switch (arm_mmu_idx(env)) {
2895     case ARMMMUIdx_E20_0:
2896     case ARMMMUIdx_E20_2:
2897     case ARMMMUIdx_E20_2_PAN:
2898     case ARMMMUIdx_SE20_0:
2899     case ARMMMUIdx_SE20_2:
2900     case ARMMMUIdx_SE20_2_PAN:
2901         return GTIMER_HYPVIRT;
2902     default:
2903         return GTIMER_VIRT;
2904     }
2905 }
2906
2907 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2908                                         const ARMCPRegInfo *ri)
2909 {
2910     int timeridx = gt_phys_redir_timeridx(env);
2911     return env->cp15.c14_timer[timeridx].cval;
2912 }
2913
2914 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915                                      uint64_t value)
2916 {
2917     int timeridx = gt_phys_redir_timeridx(env);
2918     gt_cval_write(env, ri, timeridx, value);
2919 }
2920
2921 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2922                                         const ARMCPRegInfo *ri)
2923 {
2924     int timeridx = gt_phys_redir_timeridx(env);
2925     return gt_tval_read(env, ri, timeridx);
2926 }
2927
2928 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2929                                      uint64_t value)
2930 {
2931     int timeridx = gt_phys_redir_timeridx(env);
2932     gt_tval_write(env, ri, timeridx, value);
2933 }
2934
2935 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2936                                        const ARMCPRegInfo *ri)
2937 {
2938     int timeridx = gt_phys_redir_timeridx(env);
2939     return env->cp15.c14_timer[timeridx].ctl;
2940 }
2941
2942 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2943                                     uint64_t value)
2944 {
2945     int timeridx = gt_phys_redir_timeridx(env);
2946     gt_ctl_write(env, ri, timeridx, value);
2947 }
2948
2949 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2950 {
2951     gt_timer_reset(env, ri, GTIMER_VIRT);
2952 }
2953
2954 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955                                uint64_t value)
2956 {
2957     gt_cval_write(env, ri, GTIMER_VIRT, value);
2958 }
2959
2960 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2961 {
2962     return gt_tval_read(env, ri, GTIMER_VIRT);
2963 }
2964
2965 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2966                                uint64_t value)
2967 {
2968     gt_tval_write(env, ri, GTIMER_VIRT, value);
2969 }
2970
2971 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972                               uint64_t value)
2973 {
2974     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2975 }
2976
2977 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2978                               uint64_t value)
2979 {
2980     ARMCPU *cpu = env_archcpu(env);
2981
2982     trace_arm_gt_cntvoff_write(value);
2983     raw_write(env, ri, value);
2984     gt_recalc_timer(cpu, GTIMER_VIRT);
2985 }
2986
2987 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2988                                         const ARMCPRegInfo *ri)
2989 {
2990     int timeridx = gt_virt_redir_timeridx(env);
2991     return env->cp15.c14_timer[timeridx].cval;
2992 }
2993
2994 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2995                                      uint64_t value)
2996 {
2997     int timeridx = gt_virt_redir_timeridx(env);
2998     gt_cval_write(env, ri, timeridx, value);
2999 }
3000
3001 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
3002                                         const ARMCPRegInfo *ri)
3003 {
3004     int timeridx = gt_virt_redir_timeridx(env);
3005     return gt_tval_read(env, ri, timeridx);
3006 }
3007
3008 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3009                                      uint64_t value)
3010 {
3011     int timeridx = gt_virt_redir_timeridx(env);
3012     gt_tval_write(env, ri, timeridx, value);
3013 }
3014
3015 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3016                                        const ARMCPRegInfo *ri)
3017 {
3018     int timeridx = gt_virt_redir_timeridx(env);
3019     return env->cp15.c14_timer[timeridx].ctl;
3020 }
3021
3022 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3023                                     uint64_t value)
3024 {
3025     int timeridx = gt_virt_redir_timeridx(env);
3026     gt_ctl_write(env, ri, timeridx, value);
3027 }
3028
3029 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3030 {
3031     gt_timer_reset(env, ri, GTIMER_HYP);
3032 }
3033
3034 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3035                               uint64_t value)
3036 {
3037     gt_cval_write(env, ri, GTIMER_HYP, value);
3038 }
3039
3040 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3041 {
3042     return gt_tval_read(env, ri, GTIMER_HYP);
3043 }
3044
3045 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3046                               uint64_t value)
3047 {
3048     gt_tval_write(env, ri, GTIMER_HYP, value);
3049 }
3050
3051 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3052                               uint64_t value)
3053 {
3054     gt_ctl_write(env, ri, GTIMER_HYP, value);
3055 }
3056
3057 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3058 {
3059     gt_timer_reset(env, ri, GTIMER_SEC);
3060 }
3061
3062 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3063                               uint64_t value)
3064 {
3065     gt_cval_write(env, ri, GTIMER_SEC, value);
3066 }
3067
3068 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3069 {
3070     return gt_tval_read(env, ri, GTIMER_SEC);
3071 }
3072
3073 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3074                               uint64_t value)
3075 {
3076     gt_tval_write(env, ri, GTIMER_SEC, value);
3077 }
3078
3079 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3080                               uint64_t value)
3081 {
3082     gt_ctl_write(env, ri, GTIMER_SEC, value);
3083 }
3084
3085 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3086 {
3087     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3088 }
3089
3090 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3091                              uint64_t value)
3092 {
3093     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3094 }
3095
3096 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3097 {
3098     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3099 }
3100
3101 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3102                              uint64_t value)
3103 {
3104     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3105 }
3106
3107 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3108                             uint64_t value)
3109 {
3110     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3111 }
3112
3113 void arm_gt_ptimer_cb(void *opaque)
3114 {
3115     ARMCPU *cpu = opaque;
3116
3117     gt_recalc_timer(cpu, GTIMER_PHYS);
3118 }
3119
3120 void arm_gt_vtimer_cb(void *opaque)
3121 {
3122     ARMCPU *cpu = opaque;
3123
3124     gt_recalc_timer(cpu, GTIMER_VIRT);
3125 }
3126
3127 void arm_gt_htimer_cb(void *opaque)
3128 {
3129     ARMCPU *cpu = opaque;
3130
3131     gt_recalc_timer(cpu, GTIMER_HYP);
3132 }
3133
3134 void arm_gt_stimer_cb(void *opaque)
3135 {
3136     ARMCPU *cpu = opaque;
3137
3138     gt_recalc_timer(cpu, GTIMER_SEC);
3139 }
3140
3141 void arm_gt_hvtimer_cb(void *opaque)
3142 {
3143     ARMCPU *cpu = opaque;
3144
3145     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3146 }
3147
3148 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3149 {
3150     ARMCPU *cpu = env_archcpu(env);
3151
3152     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3153 }
3154
3155 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3156     /* Note that CNTFRQ is purely reads-as-written for the benefit
3157      * of software; writing it doesn't actually change the timer frequency.
3158      * Our reset value matches the fixed frequency we implement the timer at.
3159      */
3160     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3161       .type = ARM_CP_ALIAS,
3162       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3163       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3164     },
3165     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3166       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3167       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3168       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3169       .resetfn = arm_gt_cntfrq_reset,
3170     },
3171     /* overall control: mostly access permissions */
3172     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3173       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3174       .access = PL1_RW,
3175       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3176       .resetvalue = 0,
3177     },
3178     /* per-timer control */
3179     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3180       .secure = ARM_CP_SECSTATE_NS,
3181       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3182       .accessfn = gt_ptimer_access,
3183       .fieldoffset = offsetoflow32(CPUARMState,
3184                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3185       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3186       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3187     },
3188     { .name = "CNTP_CTL_S",
3189       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3190       .secure = ARM_CP_SECSTATE_S,
3191       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3192       .accessfn = gt_ptimer_access,
3193       .fieldoffset = offsetoflow32(CPUARMState,
3194                                    cp15.c14_timer[GTIMER_SEC].ctl),
3195       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3196     },
3197     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3198       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3199       .type = ARM_CP_IO, .access = PL0_RW,
3200       .accessfn = gt_ptimer_access,
3201       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3202       .resetvalue = 0,
3203       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3204       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3205     },
3206     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3207       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3208       .accessfn = gt_vtimer_access,
3209       .fieldoffset = offsetoflow32(CPUARMState,
3210                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3211       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3212       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3213     },
3214     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3215       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3216       .type = ARM_CP_IO, .access = PL0_RW,
3217       .accessfn = gt_vtimer_access,
3218       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3219       .resetvalue = 0,
3220       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3221       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3222     },
3223     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3224     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3225       .secure = ARM_CP_SECSTATE_NS,
3226       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3227       .accessfn = gt_ptimer_access,
3228       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3229     },
3230     { .name = "CNTP_TVAL_S",
3231       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3232       .secure = ARM_CP_SECSTATE_S,
3233       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3234       .accessfn = gt_ptimer_access,
3235       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3236     },
3237     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3238       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3239       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3240       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3241       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3242     },
3243     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3244       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3245       .accessfn = gt_vtimer_access,
3246       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3247     },
3248     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3249       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3250       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3251       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3252       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3253     },
3254     /* The counter itself */
3255     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3256       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3257       .accessfn = gt_pct_access,
3258       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3259     },
3260     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3261       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3262       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3263       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3264     },
3265     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3266       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3267       .accessfn = gt_vct_access,
3268       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3269     },
3270     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3271       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3272       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3273       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3274     },
3275     /* Comparison value, indicating when the timer goes off */
3276     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3277       .secure = ARM_CP_SECSTATE_NS,
3278       .access = PL0_RW,
3279       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3280       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3281       .accessfn = gt_ptimer_access,
3282       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3283       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3284     },
3285     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3286       .secure = ARM_CP_SECSTATE_S,
3287       .access = PL0_RW,
3288       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3289       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3290       .accessfn = gt_ptimer_access,
3291       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3292     },
3293     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3294       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3295       .access = PL0_RW,
3296       .type = ARM_CP_IO,
3297       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3298       .resetvalue = 0, .accessfn = gt_ptimer_access,
3299       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3300       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3301     },
3302     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3303       .access = PL0_RW,
3304       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3305       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3306       .accessfn = gt_vtimer_access,
3307       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3308       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3309     },
3310     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3311       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3312       .access = PL0_RW,
3313       .type = ARM_CP_IO,
3314       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3315       .resetvalue = 0, .accessfn = gt_vtimer_access,
3316       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3317       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3318     },
3319     /* Secure timer -- this is actually restricted to only EL3
3320      * and configurably Secure-EL1 via the accessfn.
3321      */
3322     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3323       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3324       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3325       .accessfn = gt_stimer_access,
3326       .readfn = gt_sec_tval_read,
3327       .writefn = gt_sec_tval_write,
3328       .resetfn = gt_sec_timer_reset,
3329     },
3330     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3331       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3332       .type = ARM_CP_IO, .access = PL1_RW,
3333       .accessfn = gt_stimer_access,
3334       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3335       .resetvalue = 0,
3336       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3337     },
3338     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3339       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3340       .type = ARM_CP_IO, .access = PL1_RW,
3341       .accessfn = gt_stimer_access,
3342       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3343       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3344     },
3345     REGINFO_SENTINEL
3346 };
3347
3348 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3349                                  bool isread)
3350 {
3351     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3352         return CP_ACCESS_TRAP;
3353     }
3354     return CP_ACCESS_OK;
3355 }
3356
3357 #else
3358
3359 /* In user-mode most of the generic timer registers are inaccessible
3360  * however modern kernels (4.12+) allow access to cntvct_el0
3361  */
3362
3363 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3364 {
3365     ARMCPU *cpu = env_archcpu(env);
3366
3367     /* Currently we have no support for QEMUTimer in linux-user so we
3368      * can't call gt_get_countervalue(env), instead we directly
3369      * call the lower level functions.
3370      */
3371     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3372 }
3373
3374 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3375     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3376       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3377       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3378       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3379       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3380     },
3381     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3382       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3383       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3384       .readfn = gt_virt_cnt_read,
3385     },
3386     REGINFO_SENTINEL
3387 };
3388
3389 #endif
3390
3391 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3392 {
3393     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3394         raw_write(env, ri, value);
3395     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3396         raw_write(env, ri, value & 0xfffff6ff);
3397     } else {
3398         raw_write(env, ri, value & 0xfffff1ff);
3399     }
3400 }
3401
3402 #ifndef CONFIG_USER_ONLY
3403 /* get_phys_addr() isn't present for user-mode-only targets */
3404
3405 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3406                                  bool isread)
3407 {
3408     if (ri->opc2 & 4) {
3409         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3410          * Secure EL1 (which can only happen if EL3 is AArch64).
3411          * They are simply UNDEF if executed from NS EL1.
3412          * They function normally from EL2 or EL3.
3413          */
3414         if (arm_current_el(env) == 1) {
3415             if (arm_is_secure_below_el3(env)) {
3416                 if (env->cp15.scr_el3 & SCR_EEL2) {
3417                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3418                 }
3419                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3420             }
3421             return CP_ACCESS_TRAP_UNCATEGORIZED;
3422         }
3423     }
3424     return CP_ACCESS_OK;
3425 }
3426
3427 #ifdef CONFIG_TCG
3428 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3429                              MMUAccessType access_type, ARMMMUIdx mmu_idx)
3430 {
3431     hwaddr phys_addr;
3432     target_ulong page_size;
3433     int prot;
3434     bool ret;
3435     uint64_t par64;
3436     bool format64 = false;
3437     MemTxAttrs attrs = {};
3438     ARMMMUFaultInfo fi = {};
3439     ARMCacheAttrs cacheattrs = {};
3440
3441     ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3442                         &prot, &page_size, &fi, &cacheattrs);
3443
3444     if (ret) {
3445         /*
3446          * Some kinds of translation fault must cause exceptions rather
3447          * than being reported in the PAR.
3448          */
3449         int current_el = arm_current_el(env);
3450         int target_el;
3451         uint32_t syn, fsr, fsc;
3452         bool take_exc = false;
3453
3454         if (fi.s1ptw && current_el == 1
3455             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3456             /*
3457              * Synchronous stage 2 fault on an access made as part of the
3458              * translation table walk for AT S1E0* or AT S1E1* insn
3459              * executed from NS EL1. If this is a synchronous external abort
3460              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3461              * to EL3. Otherwise the fault is taken as an exception to EL2,
3462              * and HPFAR_EL2 holds the faulting IPA.
3463              */
3464             if (fi.type == ARMFault_SyncExternalOnWalk &&
3465                 (env->cp15.scr_el3 & SCR_EA)) {
3466                 target_el = 3;
3467             } else {
3468                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3469                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3470                     env->cp15.hpfar_el2 |= HPFAR_NS;
3471                 }
3472                 target_el = 2;
3473             }
3474             take_exc = true;
3475         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3476             /*
3477              * Synchronous external aborts during a translation table walk
3478              * are taken as Data Abort exceptions.
3479              */
3480             if (fi.stage2) {
3481                 if (current_el == 3) {
3482                     target_el = 3;
3483                 } else {
3484                     target_el = 2;
3485                 }
3486             } else {
3487                 target_el = exception_target_el(env);
3488             }
3489             take_exc = true;
3490         }
3491
3492         if (take_exc) {
3493             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3494             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3495                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3496                 fsr = arm_fi_to_lfsc(&fi);
3497                 fsc = extract32(fsr, 0, 6);
3498             } else {
3499                 fsr = arm_fi_to_sfsc(&fi);
3500                 fsc = 0x3f;
3501             }
3502             /*
3503              * Report exception with ESR indicating a fault due to a
3504              * translation table walk for a cache maintenance instruction.
3505              */
3506             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3507                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3508             env->exception.vaddress = value;
3509             env->exception.fsr = fsr;
3510             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3511         }
3512     }
3513
3514     if (is_a64(env)) {
3515         format64 = true;
3516     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3517         /*
3518          * ATS1Cxx:
3519          * * TTBCR.EAE determines whether the result is returned using the
3520          *   32-bit or the 64-bit PAR format
3521          * * Instructions executed in Hyp mode always use the 64bit format
3522          *
3523          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3524          * * The Non-secure TTBCR.EAE bit is set to 1
3525          * * The implementation includes EL2, and the value of HCR.VM is 1
3526          *
3527          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3528          *
3529          * ATS1Hx always uses the 64bit format.
3530          */
3531         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3532
3533         if (arm_feature(env, ARM_FEATURE_EL2)) {
3534             if (mmu_idx == ARMMMUIdx_E10_0 ||
3535                 mmu_idx == ARMMMUIdx_E10_1 ||
3536                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3537                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3538             } else {
3539                 format64 |= arm_current_el(env) == 2;
3540             }
3541         }
3542     }
3543
3544     if (format64) {
3545         /* Create a 64-bit PAR */
3546         par64 = (1 << 11); /* LPAE bit always set */
3547         if (!ret) {
3548             par64 |= phys_addr & ~0xfffULL;
3549             if (!attrs.secure) {
3550                 par64 |= (1 << 9); /* NS */
3551             }
3552             par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3553             par64 |= cacheattrs.shareability << 7; /* SH */
3554         } else {
3555             uint32_t fsr = arm_fi_to_lfsc(&fi);
3556
3557             par64 |= 1; /* F */
3558             par64 |= (fsr & 0x3f) << 1; /* FS */
3559             if (fi.stage2) {
3560                 par64 |= (1 << 9); /* S */
3561             }
3562             if (fi.s1ptw) {
3563                 par64 |= (1 << 8); /* PTW */
3564             }
3565         }
3566     } else {
3567         /* fsr is a DFSR/IFSR value for the short descriptor
3568          * translation table format (with WnR always clear).
3569          * Convert it to a 32-bit PAR.
3570          */
3571         if (!ret) {
3572             /* We do not set any attribute bits in the PAR */
3573             if (page_size == (1 << 24)
3574                 && arm_feature(env, ARM_FEATURE_V7)) {
3575                 par64 = (phys_addr & 0xff000000) | (1 << 1);
3576             } else {
3577                 par64 = phys_addr & 0xfffff000;
3578             }
3579             if (!attrs.secure) {
3580                 par64 |= (1 << 9); /* NS */
3581             }
3582         } else {
3583             uint32_t fsr = arm_fi_to_sfsc(&fi);
3584
3585             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3586                     ((fsr & 0xf) << 1) | 1;
3587         }
3588     }
3589     return par64;
3590 }
3591 #endif /* CONFIG_TCG */
3592
3593 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3594 {
3595 #ifdef CONFIG_TCG
3596     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3597     uint64_t par64;
3598     ARMMMUIdx mmu_idx;
3599     int el = arm_current_el(env);
3600     bool secure = arm_is_secure_below_el3(env);
3601
3602     switch (ri->opc2 & 6) {
3603     case 0:
3604         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3605         switch (el) {
3606         case 3:
3607             mmu_idx = ARMMMUIdx_SE3;
3608             break;
3609         case 2:
3610             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3611             /* fall through */
3612         case 1:
3613             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3614                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3615                            : ARMMMUIdx_Stage1_E1_PAN);
3616             } else {
3617                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3618             }
3619             break;
3620         default:
3621             g_assert_not_reached();
3622         }
3623         break;
3624     case 2:
3625         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3626         switch (el) {
3627         case 3:
3628             mmu_idx = ARMMMUIdx_SE10_0;
3629             break;
3630         case 2:
3631             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3632             mmu_idx = ARMMMUIdx_Stage1_E0;
3633             break;
3634         case 1:
3635             mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3636             break;
3637         default:
3638             g_assert_not_reached();
3639         }
3640         break;
3641     case 4:
3642         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3643         mmu_idx = ARMMMUIdx_E10_1;
3644         break;
3645     case 6:
3646         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3647         mmu_idx = ARMMMUIdx_E10_0;
3648         break;
3649     default:
3650         g_assert_not_reached();
3651     }
3652
3653     par64 = do_ats_write(env, value, access_type, mmu_idx);
3654
3655     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3656 #else
3657     /* Handled by hardware accelerator. */
3658     g_assert_not_reached();
3659 #endif /* CONFIG_TCG */
3660 }
3661
3662 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3663                         uint64_t value)
3664 {
3665 #ifdef CONFIG_TCG
3666     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3667     uint64_t par64;
3668
3669     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3670
3671     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3672 #else
3673     /* Handled by hardware accelerator. */
3674     g_assert_not_reached();
3675 #endif /* CONFIG_TCG */
3676 }
3677
3678 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3679                                      bool isread)
3680 {
3681     if (arm_current_el(env) == 3 &&
3682         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3683         return CP_ACCESS_TRAP;
3684     }
3685     return CP_ACCESS_OK;
3686 }
3687
3688 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3689                         uint64_t value)
3690 {
3691 #ifdef CONFIG_TCG
3692     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3693     ARMMMUIdx mmu_idx;
3694     int secure = arm_is_secure_below_el3(env);
3695
3696     switch (ri->opc2 & 6) {
3697     case 0:
3698         switch (ri->opc1) {
3699         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3700             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3701                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3702                            : ARMMMUIdx_Stage1_E1_PAN);
3703             } else {
3704                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3705             }
3706             break;
3707         case 4: /* AT S1E2R, AT S1E2W */
3708             mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3709             break;
3710         case 6: /* AT S1E3R, AT S1E3W */
3711             mmu_idx = ARMMMUIdx_SE3;
3712             break;
3713         default:
3714             g_assert_not_reached();
3715         }
3716         break;
3717     case 2: /* AT S1E0R, AT S1E0W */
3718         mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3719         break;
3720     case 4: /* AT S12E1R, AT S12E1W */
3721         mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3722         break;
3723     case 6: /* AT S12E0R, AT S12E0W */
3724         mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3725         break;
3726     default:
3727         g_assert_not_reached();
3728     }
3729
3730     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3731 #else
3732     /* Handled by hardware accelerator. */
3733     g_assert_not_reached();
3734 #endif /* CONFIG_TCG */
3735 }
3736 #endif
3737
3738 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3739     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3740       .access = PL1_RW, .resetvalue = 0,
3741       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3742                              offsetoflow32(CPUARMState, cp15.par_ns) },
3743       .writefn = par_write },
3744 #ifndef CONFIG_USER_ONLY
3745     /* This underdecoding is safe because the reginfo is NO_RAW. */
3746     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3747       .access = PL1_W, .accessfn = ats_access,
3748       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3749 #endif
3750     REGINFO_SENTINEL
3751 };
3752
3753 /* Return basic MPU access permission bits.  */
3754 static uint32_t simple_mpu_ap_bits(uint32_t val)
3755 {
3756     uint32_t ret;
3757     uint32_t mask;
3758     int i;
3759     ret = 0;
3760     mask = 3;
3761     for (i = 0; i < 16; i += 2) {
3762         ret |= (val >> i) & mask;
3763         mask <<= 2;
3764     }
3765     return ret;
3766 }
3767
3768 /* Pad basic MPU access permission bits to extended format.  */
3769 static uint32_t extended_mpu_ap_bits(uint32_t val)
3770 {
3771     uint32_t ret;
3772     uint32_t mask;
3773     int i;
3774     ret = 0;
3775     mask = 3;
3776     for (i = 0; i < 16; i += 2) {
3777         ret |= (val & mask) << i;
3778         mask <<= 2;
3779     }
3780     return ret;
3781 }
3782
3783 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3784                                  uint64_t value)
3785 {
3786     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3787 }
3788
3789 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3790 {
3791     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3792 }
3793
3794 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3795                                  uint64_t value)
3796 {
3797     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3798 }
3799
3800 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3801 {
3802     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3803 }
3804
3805 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3806 {
3807     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3808
3809     if (!u32p) {
3810         return 0;
3811     }
3812
3813     u32p += env->pmsav7.rnr[M_REG_NS];
3814     return *u32p;
3815 }
3816
3817 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3818                          uint64_t value)
3819 {
3820     ARMCPU *cpu = env_archcpu(env);
3821     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3822
3823     if (!u32p) {
3824         return;
3825     }
3826
3827     u32p += env->pmsav7.rnr[M_REG_NS];
3828     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3829     *u32p = value;
3830 }
3831
3832 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3833                               uint64_t value)
3834 {
3835     ARMCPU *cpu = env_archcpu(env);
3836     uint32_t nrgs = cpu->pmsav7_dregion;
3837
3838     if (value >= nrgs) {
3839         qemu_log_mask(LOG_GUEST_ERROR,
3840                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3841                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3842         return;
3843     }
3844
3845     raw_write(env, ri, value);
3846 }
3847
3848 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3849     /* Reset for all these registers is handled in arm_cpu_reset(),
3850      * because the PMSAv7 is also used by M-profile CPUs, which do
3851      * not register cpregs but still need the state to be reset.
3852      */
3853     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3854       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3855       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3856       .readfn = pmsav7_read, .writefn = pmsav7_write,
3857       .resetfn = arm_cp_reset_ignore },
3858     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3859       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3860       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3861       .readfn = pmsav7_read, .writefn = pmsav7_write,
3862       .resetfn = arm_cp_reset_ignore },
3863     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3864       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3865       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3866       .readfn = pmsav7_read, .writefn = pmsav7_write,
3867       .resetfn = arm_cp_reset_ignore },
3868     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3869       .access = PL1_RW,
3870       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3871       .writefn = pmsav7_rgnr_write,
3872       .resetfn = arm_cp_reset_ignore },
3873     REGINFO_SENTINEL
3874 };
3875
3876 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3877     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3878       .access = PL1_RW, .type = ARM_CP_ALIAS,
3879       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3880       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3881     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3882       .access = PL1_RW, .type = ARM_CP_ALIAS,
3883       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3884       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3885     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3886       .access = PL1_RW,
3887       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3888       .resetvalue = 0, },
3889     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3890       .access = PL1_RW,
3891       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3892       .resetvalue = 0, },
3893     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3894       .access = PL1_RW,
3895       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3896     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3897       .access = PL1_RW,
3898       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3899     /* Protection region base and size registers */
3900     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3901       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3902       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3903     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3904       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3905       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3906     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3907       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3908       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3909     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3910       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3911       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3912     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3913       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3914       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3915     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3916       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3917       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3918     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3919       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3920       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3921     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3922       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3923       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3924     REGINFO_SENTINEL
3925 };
3926
3927 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3928                                  uint64_t value)
3929 {
3930     TCR *tcr = raw_ptr(env, ri);
3931     int maskshift = extract32(value, 0, 3);
3932
3933     if (!arm_feature(env, ARM_FEATURE_V8)) {
3934         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3935             /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3936              * using Long-desciptor translation table format */
3937             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3938         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3939             /* In an implementation that includes the Security Extensions
3940              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3941              * Short-descriptor translation table format.
3942              */
3943             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3944         } else {
3945             value &= TTBCR_N;
3946         }
3947     }
3948
3949     /* Update the masks corresponding to the TCR bank being written
3950      * Note that we always calculate mask and base_mask, but
3951      * they are only used for short-descriptor tables (ie if EAE is 0);
3952      * for long-descriptor tables the TCR fields are used differently
3953      * and the mask and base_mask values are meaningless.
3954      */
3955     tcr->raw_tcr = value;
3956     tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3957     tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3958 }
3959
3960 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3961                              uint64_t value)
3962 {
3963     ARMCPU *cpu = env_archcpu(env);
3964     TCR *tcr = raw_ptr(env, ri);
3965
3966     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3967         /* With LPAE the TTBCR could result in a change of ASID
3968          * via the TTBCR.A1 bit, so do a TLB flush.
3969          */
3970         tlb_flush(CPU(cpu));
3971     }
3972     /* Preserve the high half of TCR_EL1, set via TTBCR2.  */
3973     value = deposit64(tcr->raw_tcr, 0, 32, value);
3974     vmsa_ttbcr_raw_write(env, ri, value);
3975 }
3976
3977 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3978 {
3979     TCR *tcr = raw_ptr(env, ri);
3980
3981     /* Reset both the TCR as well as the masks corresponding to the bank of
3982      * the TCR being reset.
3983      */
3984     tcr->raw_tcr = 0;
3985     tcr->mask = 0;
3986     tcr->base_mask = 0xffffc000u;
3987 }
3988
3989 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3990                                uint64_t value)
3991 {
3992     ARMCPU *cpu = env_archcpu(env);
3993     TCR *tcr = raw_ptr(env, ri);
3994
3995     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3996     tlb_flush(CPU(cpu));
3997     tcr->raw_tcr = value;
3998 }
3999
4000 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4001                             uint64_t value)
4002 {
4003     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4004     if (cpreg_field_is_64bit(ri) &&
4005         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4006         ARMCPU *cpu = env_archcpu(env);
4007         tlb_flush(CPU(cpu));
4008     }
4009     raw_write(env, ri, value);
4010 }
4011
4012 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4013                                     uint64_t value)
4014 {
4015     /*
4016      * If we are running with E2&0 regime, then an ASID is active.
4017      * Flush if that might be changing.  Note we're not checking
4018      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4019      * holds the active ASID, only checking the field that might.
4020      */
4021     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4022         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4023         uint16_t mask = ARMMMUIdxBit_E20_2 |
4024                         ARMMMUIdxBit_E20_2_PAN |
4025                         ARMMMUIdxBit_E20_0;
4026
4027         if (arm_is_secure_below_el3(env)) {
4028             mask >>= ARM_MMU_IDX_A_NS;
4029         }
4030
4031         tlb_flush_by_mmuidx(env_cpu(env), mask);
4032     }
4033     raw_write(env, ri, value);
4034 }
4035
4036 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4037                         uint64_t value)
4038 {
4039     ARMCPU *cpu = env_archcpu(env);
4040     CPUState *cs = CPU(cpu);
4041
4042     /*
4043      * A change in VMID to the stage2 page table (Stage2) invalidates
4044      * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
4045      */
4046     if (raw_read(env, ri) != value) {
4047         uint16_t mask = ARMMMUIdxBit_E10_1 |
4048                         ARMMMUIdxBit_E10_1_PAN |
4049                         ARMMMUIdxBit_E10_0;
4050
4051         if (arm_is_secure_below_el3(env)) {
4052             mask >>= ARM_MMU_IDX_A_NS;
4053         }
4054
4055         tlb_flush_by_mmuidx(cs, mask);
4056         raw_write(env, ri, value);
4057     }
4058 }
4059
4060 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4061     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4062       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4063       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4064                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4065     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4066       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4067       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4068                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4069     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4070       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4071       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4072                              offsetof(CPUARMState, cp15.dfar_ns) } },
4073     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4074       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4075       .access = PL1_RW, .accessfn = access_tvm_trvm,
4076       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4077       .resetvalue = 0, },
4078     REGINFO_SENTINEL
4079 };
4080
4081 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4082     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4083       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4084       .access = PL1_RW, .accessfn = access_tvm_trvm,
4085       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4086     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4087       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4088       .access = PL1_RW, .accessfn = access_tvm_trvm,
4089       .writefn = vmsa_ttbr_write, .resetvalue = 0,
4090       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4091                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4092     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4093       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4094       .access = PL1_RW, .accessfn = access_tvm_trvm,
4095       .writefn = vmsa_ttbr_write, .resetvalue = 0,
4096       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4097                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4098     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4099       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4100       .access = PL1_RW, .accessfn = access_tvm_trvm,
4101       .writefn = vmsa_tcr_el12_write,
4102       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
4103       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4104     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4105       .access = PL1_RW, .accessfn = access_tvm_trvm,
4106       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4107       .raw_writefn = vmsa_ttbcr_raw_write,
4108       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4109                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4110     REGINFO_SENTINEL
4111 };
4112
4113 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4114  * qemu tlbs nor adjusting cached masks.
4115  */
4116 static const ARMCPRegInfo ttbcr2_reginfo = {
4117     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4118     .access = PL1_RW, .accessfn = access_tvm_trvm,
4119     .type = ARM_CP_ALIAS,
4120     .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4121                            offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
4122 };
4123
4124 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4125                                 uint64_t value)
4126 {
4127     env->cp15.c15_ticonfig = value & 0xe7;
4128     /* The OS_TYPE bit in this register changes the reported CPUID! */
4129     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4130         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4131 }
4132
4133 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4134                                 uint64_t value)
4135 {
4136     env->cp15.c15_threadid = value & 0xffff;
4137 }
4138
4139 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4140                            uint64_t value)
4141 {
4142     /* Wait-for-interrupt (deprecated) */
4143     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4144 }
4145
4146 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4147                                   uint64_t value)
4148 {
4149     /* On OMAP there are registers indicating the max/min index of dcache lines
4150      * containing a dirty line; cache flush operations have to reset these.
4151      */
4152     env->cp15.c15_i_max = 0x000;
4153     env->cp15.c15_i_min = 0xff0;
4154 }
4155
4156 static const ARMCPRegInfo omap_cp_reginfo[] = {
4157     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4158       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4159       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4160       .resetvalue = 0, },
4161     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4162       .access = PL1_RW, .type = ARM_CP_NOP },
4163     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4164       .access = PL1_RW,
4165       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4166       .writefn = omap_ticonfig_write },
4167     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4168       .access = PL1_RW,
4169       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4170     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4171       .access = PL1_RW, .resetvalue = 0xff0,
4172       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4173     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4174       .access = PL1_RW,
4175       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4176       .writefn = omap_threadid_write },
4177     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4178       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4179       .type = ARM_CP_NO_RAW,
4180       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4181     /* TODO: Peripheral port remap register:
4182      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4183      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4184      * when MMU is off.
4185      */
4186     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4187       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4188       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4189       .writefn = omap_cachemaint_write },
4190     { .name = "C9", .cp = 15, .crn = 9,
4191       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4192       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4193     REGINFO_SENTINEL
4194 };
4195
4196 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4197                               uint64_t value)
4198 {
4199     env->cp15.c15_cpar = value & 0x3fff;
4200 }
4201
4202 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4203     { .name = "XSCALE_CPAR",
4204       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4205       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4206       .writefn = xscale_cpar_write, },
4207     { .name = "XSCALE_AUXCR",
4208       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4209       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4210       .resetvalue = 0, },
4211     /* XScale specific cache-lockdown: since we have no cache we NOP these
4212      * and hope the guest does not really rely on cache behaviour.
4213      */
4214     { .name = "XSCALE_LOCK_ICACHE_LINE",
4215       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4216       .access = PL1_W, .type = ARM_CP_NOP },
4217     { .name = "XSCALE_UNLOCK_ICACHE",
4218       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4219       .access = PL1_W, .type = ARM_CP_NOP },
4220     { .name = "XSCALE_DCACHE_LOCK",
4221       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4222       .access = PL1_RW, .type = ARM_CP_NOP },
4223     { .name = "XSCALE_UNLOCK_DCACHE",
4224       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4225       .access = PL1_W, .type = ARM_CP_NOP },
4226     REGINFO_SENTINEL
4227 };
4228
4229 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4230     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4231      * implementation of this implementation-defined space.
4232      * Ideally this should eventually disappear in favour of actually
4233      * implementing the correct behaviour for all cores.
4234      */
4235     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4236       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4237       .access = PL1_RW,
4238       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4239       .resetvalue = 0 },
4240     REGINFO_SENTINEL
4241 };
4242
4243 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4244     /* Cache status: RAZ because we have no cache so it's always clean */
4245     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4246       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4247       .resetvalue = 0 },
4248     REGINFO_SENTINEL
4249 };
4250
4251 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4252     /* We never have a a block transfer operation in progress */
4253     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4254       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4255       .resetvalue = 0 },
4256     /* The cache ops themselves: these all NOP for QEMU */
4257     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4258       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4259     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4260       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4261     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4262       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4263     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4264       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4265     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4266       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4267     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4268       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4269     REGINFO_SENTINEL
4270 };
4271
4272 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4273     /* The cache test-and-clean instructions always return (1 << 30)
4274      * to indicate that there are no dirty cache lines.
4275      */
4276     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4277       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4278       .resetvalue = (1 << 30) },
4279     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4280       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4281       .resetvalue = (1 << 30) },
4282     REGINFO_SENTINEL
4283 };
4284
4285 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4286     /* Ignore ReadBuffer accesses */
4287     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4288       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4289       .access = PL1_RW, .resetvalue = 0,
4290       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4291     REGINFO_SENTINEL
4292 };
4293
4294 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4295 {
4296     unsigned int cur_el = arm_current_el(env);
4297
4298     if (arm_is_el2_enabled(env) && cur_el == 1) {
4299         return env->cp15.vpidr_el2;
4300     }
4301     return raw_read(env, ri);
4302 }
4303
4304 static uint64_t mpidr_read_val(CPUARMState *env)
4305 {
4306     ARMCPU *cpu = env_archcpu(env);
4307     uint64_t mpidr = cpu->mp_affinity;
4308
4309     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4310         mpidr |= (1U << 31);
4311         /* Cores which are uniprocessor (non-coherent)
4312          * but still implement the MP extensions set
4313          * bit 30. (For instance, Cortex-R5).
4314          */
4315         if (cpu->mp_is_up) {
4316             mpidr |= (1u << 30);
4317         }
4318     }
4319     return mpidr;
4320 }
4321
4322 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4323 {
4324     unsigned int cur_el = arm_current_el(env);
4325
4326     if (arm_is_el2_enabled(env) && cur_el == 1) {
4327         return env->cp15.vmpidr_el2;
4328     }
4329     return mpidr_read_val(env);
4330 }
4331
4332 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4333     /* NOP AMAIR0/1 */
4334     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4335       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4336       .access = PL1_RW, .accessfn = access_tvm_trvm,
4337       .type = ARM_CP_CONST, .resetvalue = 0 },
4338     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4339     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4340       .access = PL1_RW, .accessfn = access_tvm_trvm,
4341       .type = ARM_CP_CONST, .resetvalue = 0 },
4342     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4343       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4344       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4345                              offsetof(CPUARMState, cp15.par_ns)} },
4346     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4347       .access = PL1_RW, .accessfn = access_tvm_trvm,
4348       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4349       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4350                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4351       .writefn = vmsa_ttbr_write, },
4352     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4353       .access = PL1_RW, .accessfn = access_tvm_trvm,
4354       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4355       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4356                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4357       .writefn = vmsa_ttbr_write, },
4358     REGINFO_SENTINEL
4359 };
4360
4361 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4362 {
4363     return vfp_get_fpcr(env);
4364 }
4365
4366 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4367                             uint64_t value)
4368 {
4369     vfp_set_fpcr(env, value);
4370 }
4371
4372 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4373 {
4374     return vfp_get_fpsr(env);
4375 }
4376
4377 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4378                             uint64_t value)
4379 {
4380     vfp_set_fpsr(env, value);
4381 }
4382
4383 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4384                                        bool isread)
4385 {
4386     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4387         return CP_ACCESS_TRAP;
4388     }
4389     return CP_ACCESS_OK;
4390 }
4391
4392 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4393                             uint64_t value)
4394 {
4395     env->daif = value & PSTATE_DAIF;
4396 }
4397
4398 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4399 {
4400     return env->pstate & PSTATE_PAN;
4401 }
4402
4403 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4404                            uint64_t value)
4405 {
4406     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4407 }
4408
4409 static const ARMCPRegInfo pan_reginfo = {
4410     .name = "PAN", .state = ARM_CP_STATE_AA64,
4411     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4412     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4413     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4414 };
4415
4416 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4417 {
4418     return env->pstate & PSTATE_UAO;
4419 }
4420
4421 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4422                            uint64_t value)
4423 {
4424     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4425 }
4426
4427 static const ARMCPRegInfo uao_reginfo = {
4428     .name = "UAO", .state = ARM_CP_STATE_AA64,
4429     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4430     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4431     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4432 };
4433
4434 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4435 {
4436     return env->pstate & PSTATE_DIT;
4437 }
4438
4439 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4440                            uint64_t value)
4441 {
4442     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4443 }
4444
4445 static const ARMCPRegInfo dit_reginfo = {
4446     .name = "DIT", .state = ARM_CP_STATE_AA64,
4447     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4448     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4449     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4450 };
4451
4452 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4453                                               const ARMCPRegInfo *ri,
4454                                               bool isread)
4455 {
4456     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4457     switch (arm_current_el(env)) {
4458     case 0:
4459         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4460         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4461             return CP_ACCESS_TRAP;
4462         }
4463         /* fall through */
4464     case 1:
4465         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4466         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4467             return CP_ACCESS_TRAP_EL2;
4468         }
4469         break;
4470     }
4471     return CP_ACCESS_OK;
4472 }
4473
4474 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4475                                               const ARMCPRegInfo *ri,
4476                                               bool isread)
4477 {
4478     /* Cache invalidate/clean to Point of Unification... */
4479     switch (arm_current_el(env)) {
4480     case 0:
4481         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4482         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4483             return CP_ACCESS_TRAP;
4484         }
4485         /* fall through */
4486     case 1:
4487         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4488         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4489             return CP_ACCESS_TRAP_EL2;
4490         }
4491         break;
4492     }
4493     return CP_ACCESS_OK;
4494 }
4495
4496 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4497  * Page D4-1736 (DDI0487A.b)
4498  */
4499
4500 static int vae1_tlbmask(CPUARMState *env)
4501 {
4502     uint64_t hcr = arm_hcr_el2_eff(env);
4503     uint16_t mask;
4504
4505     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4506         mask = ARMMMUIdxBit_E20_2 |
4507                ARMMMUIdxBit_E20_2_PAN |
4508                ARMMMUIdxBit_E20_0;
4509     } else {
4510         mask = ARMMMUIdxBit_E10_1 |
4511                ARMMMUIdxBit_E10_1_PAN |
4512                ARMMMUIdxBit_E10_0;
4513     }
4514
4515     if (arm_is_secure_below_el3(env)) {
4516         mask >>= ARM_MMU_IDX_A_NS;
4517     }
4518
4519     return mask;
4520 }
4521
4522 /* Return 56 if TBI is enabled, 64 otherwise. */
4523 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4524                               uint64_t addr)
4525 {
4526     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4527     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4528     int select = extract64(addr, 55, 1);
4529
4530     return (tbi >> select) & 1 ? 56 : 64;
4531 }
4532
4533 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4534 {
4535     uint64_t hcr = arm_hcr_el2_eff(env);
4536     ARMMMUIdx mmu_idx;
4537
4538     /* Only the regime of the mmu_idx below is significant. */
4539     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4540         mmu_idx = ARMMMUIdx_E20_0;
4541     } else {
4542         mmu_idx = ARMMMUIdx_E10_0;
4543     }
4544
4545     if (arm_is_secure_below_el3(env)) {
4546         mmu_idx &= ~ARM_MMU_IDX_A_NS;
4547     }
4548
4549     return tlbbits_for_regime(env, mmu_idx, addr);
4550 }
4551
4552 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4553                                       uint64_t value)
4554 {
4555     CPUState *cs = env_cpu(env);
4556     int mask = vae1_tlbmask(env);
4557
4558     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4559 }
4560
4561 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4562                                     uint64_t value)
4563 {
4564     CPUState *cs = env_cpu(env);
4565     int mask = vae1_tlbmask(env);
4566
4567     if (tlb_force_broadcast(env)) {
4568         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4569     } else {
4570         tlb_flush_by_mmuidx(cs, mask);
4571     }
4572 }
4573
4574 static int alle1_tlbmask(CPUARMState *env)
4575 {
4576     /*
4577      * Note that the 'ALL' scope must invalidate both stage 1 and
4578      * stage 2 translations, whereas most other scopes only invalidate
4579      * stage 1 translations.
4580      */
4581     if (arm_is_secure_below_el3(env)) {
4582         return ARMMMUIdxBit_SE10_1 |
4583                ARMMMUIdxBit_SE10_1_PAN |
4584                ARMMMUIdxBit_SE10_0;
4585     } else {
4586         return ARMMMUIdxBit_E10_1 |
4587                ARMMMUIdxBit_E10_1_PAN |
4588                ARMMMUIdxBit_E10_0;
4589     }
4590 }
4591
4592 static int e2_tlbmask(CPUARMState *env)
4593 {
4594     if (arm_is_secure_below_el3(env)) {
4595         return ARMMMUIdxBit_SE20_0 |
4596                ARMMMUIdxBit_SE20_2 |
4597                ARMMMUIdxBit_SE20_2_PAN |
4598                ARMMMUIdxBit_SE2;
4599     } else {
4600         return ARMMMUIdxBit_E20_0 |
4601                ARMMMUIdxBit_E20_2 |
4602                ARMMMUIdxBit_E20_2_PAN |
4603                ARMMMUIdxBit_E2;
4604     }
4605 }
4606
4607 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4608                                   uint64_t value)
4609 {
4610     CPUState *cs = env_cpu(env);
4611     int mask = alle1_tlbmask(env);
4612
4613     tlb_flush_by_mmuidx(cs, mask);
4614 }
4615
4616 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4617                                   uint64_t value)
4618 {
4619     CPUState *cs = env_cpu(env);
4620     int mask = e2_tlbmask(env);
4621
4622     tlb_flush_by_mmuidx(cs, mask);
4623 }
4624
4625 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4626                                   uint64_t value)
4627 {
4628     ARMCPU *cpu = env_archcpu(env);
4629     CPUState *cs = CPU(cpu);
4630
4631     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4632 }
4633
4634 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4635                                     uint64_t value)
4636 {
4637     CPUState *cs = env_cpu(env);
4638     int mask = alle1_tlbmask(env);
4639
4640     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4641 }
4642
4643 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4644                                     uint64_t value)
4645 {
4646     CPUState *cs = env_cpu(env);
4647     int mask = e2_tlbmask(env);
4648
4649     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4650 }
4651
4652 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4653                                     uint64_t value)
4654 {
4655     CPUState *cs = env_cpu(env);
4656
4657     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4658 }
4659
4660 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4661                                  uint64_t value)
4662 {
4663     /* Invalidate by VA, EL2
4664      * Currently handles both VAE2 and VALE2, since we don't support
4665      * flush-last-level-only.
4666      */
4667     CPUState *cs = env_cpu(env);
4668     int mask = e2_tlbmask(env);
4669     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4670
4671     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4672 }
4673
4674 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4675                                  uint64_t value)
4676 {
4677     /* Invalidate by VA, EL3
4678      * Currently handles both VAE3 and VALE3, since we don't support
4679      * flush-last-level-only.
4680      */
4681     ARMCPU *cpu = env_archcpu(env);
4682     CPUState *cs = CPU(cpu);
4683     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4684
4685     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4686 }
4687
4688 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4689                                    uint64_t value)
4690 {
4691     CPUState *cs = env_cpu(env);
4692     int mask = vae1_tlbmask(env);
4693     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4694     int bits = vae1_tlbbits(env, pageaddr);
4695
4696     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4697 }
4698
4699 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4700                                  uint64_t value)
4701 {
4702     /* Invalidate by VA, EL1&0 (AArch64 version).
4703      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4704      * since we don't support flush-for-specific-ASID-only or
4705      * flush-last-level-only.
4706      */
4707     CPUState *cs = env_cpu(env);
4708     int mask = vae1_tlbmask(env);
4709     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4710     int bits = vae1_tlbbits(env, pageaddr);
4711
4712     if (tlb_force_broadcast(env)) {
4713         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4714     } else {
4715         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4716     }
4717 }
4718
4719 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4720                                    uint64_t value)
4721 {
4722     CPUState *cs = env_cpu(env);
4723     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4724     bool secure = arm_is_secure_below_el3(env);
4725     int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4726     int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_E2 : ARMMMUIdx_SE2,
4727                                   pageaddr);
4728
4729     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4730 }
4731
4732 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4733                                    uint64_t value)
4734 {
4735     CPUState *cs = env_cpu(env);
4736     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4737     int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4738
4739     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4740                                                   ARMMMUIdxBit_SE3, bits);
4741 }
4742
4743 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4744                                       bool isread)
4745 {
4746     int cur_el = arm_current_el(env);
4747
4748     if (cur_el < 2) {
4749         uint64_t hcr = arm_hcr_el2_eff(env);
4750
4751         if (cur_el == 0) {
4752             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4753                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4754                     return CP_ACCESS_TRAP_EL2;
4755                 }
4756             } else {
4757                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4758                     return CP_ACCESS_TRAP;
4759                 }
4760                 if (hcr & HCR_TDZ) {
4761                     return CP_ACCESS_TRAP_EL2;
4762                 }
4763             }
4764         } else if (hcr & HCR_TDZ) {
4765             return CP_ACCESS_TRAP_EL2;
4766         }
4767     }
4768     return CP_ACCESS_OK;
4769 }
4770
4771 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4772 {
4773     ARMCPU *cpu = env_archcpu(env);
4774     int dzp_bit = 1 << 4;
4775
4776     /* DZP indicates whether DC ZVA access is allowed */
4777     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4778         dzp_bit = 0;
4779     }
4780     return cpu->dcz_blocksize | dzp_bit;
4781 }
4782
4783 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4784                                     bool isread)
4785 {
4786     if (!(env->pstate & PSTATE_SP)) {
4787         /* Access to SP_EL0 is undefined if it's being used as
4788          * the stack pointer.
4789          */
4790         return CP_ACCESS_TRAP_UNCATEGORIZED;
4791     }
4792     return CP_ACCESS_OK;
4793 }
4794
4795 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4796 {
4797     return env->pstate & PSTATE_SP;
4798 }
4799
4800 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4801 {
4802     update_spsel(env, val);
4803 }
4804
4805 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4806                         uint64_t value)
4807 {
4808     ARMCPU *cpu = env_archcpu(env);
4809
4810     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4811         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4812         value &= ~SCTLR_M;
4813     }
4814
4815     /* ??? Lots of these bits are not implemented.  */
4816
4817     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4818         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4819             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4820         } else {
4821             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4822                        SCTLR_ATA0 | SCTLR_ATA);
4823         }
4824     }
4825
4826     if (raw_read(env, ri) == value) {
4827         /* Skip the TLB flush if nothing actually changed; Linux likes
4828          * to do a lot of pointless SCTLR writes.
4829          */
4830         return;
4831     }
4832
4833     raw_write(env, ri, value);
4834
4835     /* This may enable/disable the MMU, so do a TLB flush.  */
4836     tlb_flush(CPU(cpu));
4837
4838     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4839         /*
4840          * Normally we would always end the TB on an SCTLR write; see the
4841          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4842          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4843          * of hflags from the translator, so do it here.
4844          */
4845         arm_rebuild_hflags(env);
4846     }
4847 }
4848
4849 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4850                                      bool isread)
4851 {
4852     if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
4853         return CP_ACCESS_TRAP_FP_EL2;
4854     }
4855     if (env->cp15.cptr_el[3] & CPTR_TFP) {
4856         return CP_ACCESS_TRAP_FP_EL3;
4857     }
4858     return CP_ACCESS_OK;
4859 }
4860
4861 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4862                        uint64_t value)
4863 {
4864     env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4865 }
4866
4867 static const ARMCPRegInfo v8_cp_reginfo[] = {
4868     /* Minimal set of EL0-visible registers. This will need to be expanded
4869      * significantly for system emulation of AArch64 CPUs.
4870      */
4871     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4872       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4873       .access = PL0_RW, .type = ARM_CP_NZCV },
4874     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4875       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4876       .type = ARM_CP_NO_RAW,
4877       .access = PL0_RW, .accessfn = aa64_daif_access,
4878       .fieldoffset = offsetof(CPUARMState, daif),
4879       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4880     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4881       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4882       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4883       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4884     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4885       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4886       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4887       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4888     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4889       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4890       .access = PL0_R, .type = ARM_CP_NO_RAW,
4891       .readfn = aa64_dczid_read },
4892     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4893       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4894       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4895 #ifndef CONFIG_USER_ONLY
4896       /* Avoid overhead of an access check that always passes in user-mode */
4897       .accessfn = aa64_zva_access,
4898 #endif
4899     },
4900     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4901       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4902       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4903     /* Cache ops: all NOPs since we don't emulate caches */
4904     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4905       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4906       .access = PL1_W, .type = ARM_CP_NOP,
4907       .accessfn = aa64_cacheop_pou_access },
4908     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4909       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4910       .access = PL1_W, .type = ARM_CP_NOP,
4911       .accessfn = aa64_cacheop_pou_access },
4912     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4913       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4914       .access = PL0_W, .type = ARM_CP_NOP,
4915       .accessfn = aa64_cacheop_pou_access },
4916     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4917       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4918       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4919       .type = ARM_CP_NOP },
4920     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4921       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4922       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4923     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4924       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4925       .access = PL0_W, .type = ARM_CP_NOP,
4926       .accessfn = aa64_cacheop_poc_access },
4927     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4928       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4929       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4930     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4931       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4932       .access = PL0_W, .type = ARM_CP_NOP,
4933       .accessfn = aa64_cacheop_pou_access },
4934     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4935       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4936       .access = PL0_W, .type = ARM_CP_NOP,
4937       .accessfn = aa64_cacheop_poc_access },
4938     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4939       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4940       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4941     /* TLBI operations */
4942     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4943       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4944       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4945       .writefn = tlbi_aa64_vmalle1is_write },
4946     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4947       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4948       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4949       .writefn = tlbi_aa64_vae1is_write },
4950     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4951       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4952       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4953       .writefn = tlbi_aa64_vmalle1is_write },
4954     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4955       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4956       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4957       .writefn = tlbi_aa64_vae1is_write },
4958     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4959       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4960       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4961       .writefn = tlbi_aa64_vae1is_write },
4962     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4963       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4964       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4965       .writefn = tlbi_aa64_vae1is_write },
4966     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4967       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4968       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4969       .writefn = tlbi_aa64_vmalle1_write },
4970     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4971       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4972       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4973       .writefn = tlbi_aa64_vae1_write },
4974     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4975       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4976       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4977       .writefn = tlbi_aa64_vmalle1_write },
4978     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4979       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4980       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4981       .writefn = tlbi_aa64_vae1_write },
4982     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4983       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4984       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4985       .writefn = tlbi_aa64_vae1_write },
4986     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4987       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4988       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4989       .writefn = tlbi_aa64_vae1_write },
4990     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4991       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4992       .access = PL2_W, .type = ARM_CP_NOP },
4993     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4994       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4995       .access = PL2_W, .type = ARM_CP_NOP },
4996     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4997       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4998       .access = PL2_W, .type = ARM_CP_NO_RAW,
4999       .writefn = tlbi_aa64_alle1is_write },
5000     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5001       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5002       .access = PL2_W, .type = ARM_CP_NO_RAW,
5003       .writefn = tlbi_aa64_alle1is_write },
5004     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5005       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5006       .access = PL2_W, .type = ARM_CP_NOP },
5007     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5008       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5009       .access = PL2_W, .type = ARM_CP_NOP },
5010     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5011       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5012       .access = PL2_W, .type = ARM_CP_NO_RAW,
5013       .writefn = tlbi_aa64_alle1_write },
5014     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5015       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5016       .access = PL2_W, .type = ARM_CP_NO_RAW,
5017       .writefn = tlbi_aa64_alle1is_write },
5018 #ifndef CONFIG_USER_ONLY
5019     /* 64 bit address translation operations */
5020     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5021       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5022       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5023       .writefn = ats_write64 },
5024     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5025       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5026       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5027       .writefn = ats_write64 },
5028     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5029       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5030       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5031       .writefn = ats_write64 },
5032     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5033       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5034       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5035       .writefn = ats_write64 },
5036     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5037       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5038       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5039       .writefn = ats_write64 },
5040     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5041       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5042       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5043       .writefn = ats_write64 },
5044     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5045       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5046       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5047       .writefn = ats_write64 },
5048     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5049       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5050       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5051       .writefn = ats_write64 },
5052     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5053     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5054       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5055       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5056       .writefn = ats_write64 },
5057     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5058       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5059       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5060       .writefn = ats_write64 },
5061     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5062       .type = ARM_CP_ALIAS,
5063       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5064       .access = PL1_RW, .resetvalue = 0,
5065       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5066       .writefn = par_write },
5067 #endif
5068     /* TLB invalidate last level of translation table walk */
5069     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5070       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5071       .writefn = tlbimva_is_write },
5072     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5073       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5074       .writefn = tlbimvaa_is_write },
5075     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5076       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5077       .writefn = tlbimva_write },
5078     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5079       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5080       .writefn = tlbimvaa_write },
5081     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5082       .type = ARM_CP_NO_RAW, .access = PL2_W,
5083       .writefn = tlbimva_hyp_write },
5084     { .name = "TLBIMVALHIS",
5085       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5086       .type = ARM_CP_NO_RAW, .access = PL2_W,
5087       .writefn = tlbimva_hyp_is_write },
5088     { .name = "TLBIIPAS2",
5089       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5090       .type = ARM_CP_NOP, .access = PL2_W },
5091     { .name = "TLBIIPAS2IS",
5092       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5093       .type = ARM_CP_NOP, .access = PL2_W },
5094     { .name = "TLBIIPAS2L",
5095       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5096       .type = ARM_CP_NOP, .access = PL2_W },
5097     { .name = "TLBIIPAS2LIS",
5098       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5099       .type = ARM_CP_NOP, .access = PL2_W },
5100     /* 32 bit cache operations */
5101     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5102       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5103     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5104       .type = ARM_CP_NOP, .access = PL1_W },
5105     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5106       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5107     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5108       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5109     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5110       .type = ARM_CP_NOP, .access = PL1_W },
5111     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5112       .type = ARM_CP_NOP, .access = PL1_W },
5113     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5114       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5115     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5116       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5117     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5118       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5119     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5120       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5121     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5122       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5123     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5124       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5125     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5126       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5127     /* MMU Domain access control / MPU write buffer control */
5128     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5129       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5130       .writefn = dacr_write, .raw_writefn = raw_write,
5131       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5132                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5133     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5134       .type = ARM_CP_ALIAS,
5135       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5136       .access = PL1_RW,
5137       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5138     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5139       .type = ARM_CP_ALIAS,
5140       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5141       .access = PL1_RW,
5142       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5143     /* We rely on the access checks not allowing the guest to write to the
5144      * state field when SPSel indicates that it's being used as the stack
5145      * pointer.
5146      */
5147     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5148       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5149       .access = PL1_RW, .accessfn = sp_el0_access,
5150       .type = ARM_CP_ALIAS,
5151       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5152     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5153       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5154       .access = PL2_RW, .type = ARM_CP_ALIAS,
5155       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5156     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5157       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5158       .type = ARM_CP_NO_RAW,
5159       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5160     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5161       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5162       .type = ARM_CP_ALIAS,
5163       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
5164       .access = PL2_RW, .accessfn = fpexc32_access },
5165     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5166       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5167       .access = PL2_RW, .resetvalue = 0,
5168       .writefn = dacr_write, .raw_writefn = raw_write,
5169       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5170     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5171       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5172       .access = PL2_RW, .resetvalue = 0,
5173       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5174     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5175       .type = ARM_CP_ALIAS,
5176       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5177       .access = PL2_RW,
5178       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5179     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5180       .type = ARM_CP_ALIAS,
5181       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5182       .access = PL2_RW,
5183       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5184     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5185       .type = ARM_CP_ALIAS,
5186       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5187       .access = PL2_RW,
5188       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5189     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5190       .type = ARM_CP_ALIAS,
5191       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5192       .access = PL2_RW,
5193       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5194     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5195       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5196       .resetvalue = 0,
5197       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5198     { .name = "SDCR", .type = ARM_CP_ALIAS,
5199       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5200       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5201       .writefn = sdcr_write,
5202       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5203     REGINFO_SENTINEL
5204 };
5205
5206 /* Used to describe the behaviour of EL2 regs when EL2 does not exist.  */
5207 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
5208     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5209       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5210       .access = PL2_RW,
5211       .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
5212     { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
5213       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5214       .access = PL2_RW,
5215       .type = ARM_CP_CONST, .resetvalue = 0 },
5216     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5217       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5218       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5219     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5220       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5221       .access = PL2_RW,
5222       .type = ARM_CP_CONST, .resetvalue = 0 },
5223     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5224       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5225       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5226     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5227       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5228       .access = PL2_RW, .type = ARM_CP_CONST,
5229       .resetvalue = 0 },
5230     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5231       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5232       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5233     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5234       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5235       .access = PL2_RW, .type = ARM_CP_CONST,
5236       .resetvalue = 0 },
5237     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5238       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5239       .access = PL2_RW, .type = ARM_CP_CONST,
5240       .resetvalue = 0 },
5241     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5242       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5243       .access = PL2_RW, .type = ARM_CP_CONST,
5244       .resetvalue = 0 },
5245     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5246       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5247       .access = PL2_RW, .type = ARM_CP_CONST,
5248       .resetvalue = 0 },
5249     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5250       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5251       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5252     { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5253       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5254       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5255       .type = ARM_CP_CONST, .resetvalue = 0 },
5256     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5257       .cp = 15, .opc1 = 6, .crm = 2,
5258       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5259       .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5260     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5261       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5262       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5263     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5264       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5265       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5266     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5267       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5268       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5269     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5270       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5271       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5272     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5273       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5274       .resetvalue = 0 },
5275     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5276       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5277       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5278     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5279       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5280       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5281     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5282       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5283       .resetvalue = 0 },
5284     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5285       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5286       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5287     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5288       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5289       .resetvalue = 0 },
5290     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5291       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5292       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5293     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5294       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5295       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5296     { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5297       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5298       .access = PL2_RW, .accessfn = access_tda,
5299       .type = ARM_CP_CONST, .resetvalue = 0 },
5300     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5301       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5302       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5303       .type = ARM_CP_CONST, .resetvalue = 0 },
5304     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5305       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5306       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5307     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5308       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5309       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5310     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5311       .type = ARM_CP_CONST,
5312       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5313       .access = PL2_RW, .resetvalue = 0 },
5314     REGINFO_SENTINEL
5315 };
5316
5317 /* Ditto, but for registers which exist in ARMv8 but not v7 */
5318 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5319     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5320       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5321       .access = PL2_RW,
5322       .type = ARM_CP_CONST, .resetvalue = 0 },
5323     REGINFO_SENTINEL
5324 };
5325
5326 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5327 {
5328     ARMCPU *cpu = env_archcpu(env);
5329
5330     if (arm_feature(env, ARM_FEATURE_V8)) {
5331         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5332     } else {
5333         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5334     }
5335
5336     if (arm_feature(env, ARM_FEATURE_EL3)) {
5337         valid_mask &= ~HCR_HCD;
5338     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5339         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5340          * However, if we're using the SMC PSCI conduit then QEMU is
5341          * effectively acting like EL3 firmware and so the guest at
5342          * EL2 should retain the ability to prevent EL1 from being
5343          * able to make SMC calls into the ersatz firmware, so in
5344          * that case HCR.TSC should be read/write.
5345          */
5346         valid_mask &= ~HCR_TSC;
5347     }
5348
5349     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5350         if (cpu_isar_feature(aa64_vh, cpu)) {
5351             valid_mask |= HCR_E2H;
5352         }
5353         if (cpu_isar_feature(aa64_lor, cpu)) {
5354             valid_mask |= HCR_TLOR;
5355         }
5356         if (cpu_isar_feature(aa64_pauth, cpu)) {
5357             valid_mask |= HCR_API | HCR_APK;
5358         }
5359         if (cpu_isar_feature(aa64_mte, cpu)) {
5360             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5361         }
5362     }
5363
5364     /* Clear RES0 bits.  */
5365     value &= valid_mask;
5366
5367     /*
5368      * These bits change the MMU setup:
5369      * HCR_VM enables stage 2 translation
5370      * HCR_PTW forbids certain page-table setups
5371      * HCR_DC disables stage1 and enables stage2 translation
5372      * HCR_DCT enables tagging on (disabled) stage1 translation
5373      */
5374     if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
5375         tlb_flush(CPU(cpu));
5376     }
5377     env->cp15.hcr_el2 = value;
5378
5379     /*
5380      * Updates to VI and VF require us to update the status of
5381      * virtual interrupts, which are the logical OR of these bits
5382      * and the state of the input lines from the GIC. (This requires
5383      * that we have the iothread lock, which is done by marking the
5384      * reginfo structs as ARM_CP_IO.)
5385      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5386      * possible for it to be taken immediately, because VIRQ and
5387      * VFIQ are masked unless running at EL0 or EL1, and HCR
5388      * can only be written at EL2.
5389      */
5390     g_assert(qemu_mutex_iothread_locked());
5391     arm_cpu_update_virq(cpu);
5392     arm_cpu_update_vfiq(cpu);
5393 }
5394
5395 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5396 {
5397     do_hcr_write(env, value, 0);
5398 }
5399
5400 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5401                           uint64_t value)
5402 {
5403     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5404     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5405     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5406 }
5407
5408 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5409                          uint64_t value)
5410 {
5411     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5412     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5413     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5414 }
5415
5416 /*
5417  * Return the effective value of HCR_EL2.
5418  * Bits that are not included here:
5419  * RW       (read from SCR_EL3.RW as needed)
5420  */
5421 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5422 {
5423     uint64_t ret = env->cp15.hcr_el2;
5424
5425     if (!arm_is_el2_enabled(env)) {
5426         /*
5427          * "This register has no effect if EL2 is not enabled in the
5428          * current Security state".  This is ARMv8.4-SecEL2 speak for
5429          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5430          *
5431          * Prior to that, the language was "In an implementation that
5432          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5433          * as if this field is 0 for all purposes other than a direct
5434          * read or write access of HCR_EL2".  With lots of enumeration
5435          * on a per-field basis.  In current QEMU, this is condition
5436          * is arm_is_secure_below_el3.
5437          *
5438          * Since the v8.4 language applies to the entire register, and
5439          * appears to be backward compatible, use that.
5440          */
5441         return 0;
5442     }
5443
5444     /*
5445      * For a cpu that supports both aarch64 and aarch32, we can set bits
5446      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5447      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5448      */
5449     if (!arm_el_is_aa64(env, 2)) {
5450         uint64_t aa32_valid;
5451
5452         /*
5453          * These bits are up-to-date as of ARMv8.6.
5454          * For HCR, it's easiest to list just the 2 bits that are invalid.
5455          * For HCR2, list those that are valid.
5456          */
5457         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5458         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5459                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5460         ret &= aa32_valid;
5461     }
5462
5463     if (ret & HCR_TGE) {
5464         /* These bits are up-to-date as of ARMv8.6.  */
5465         if (ret & HCR_E2H) {
5466             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5467                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5468                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5469                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5470                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5471                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5472         } else {
5473             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5474         }
5475         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5476                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5477                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5478                  HCR_TLOR);
5479     }
5480
5481     return ret;
5482 }
5483
5484 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5485                            uint64_t value)
5486 {
5487     /*
5488      * For A-profile AArch32 EL3, if NSACR.CP10
5489      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5490      */
5491     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5492         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5493         value &= ~(0x3 << 10);
5494         value |= env->cp15.cptr_el[2] & (0x3 << 10);
5495     }
5496     env->cp15.cptr_el[2] = value;
5497 }
5498
5499 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5500 {
5501     /*
5502      * For A-profile AArch32 EL3, if NSACR.CP10
5503      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5504      */
5505     uint64_t value = env->cp15.cptr_el[2];
5506
5507     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5508         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5509         value |= 0x3 << 10;
5510     }
5511     return value;
5512 }
5513
5514 static const ARMCPRegInfo el2_cp_reginfo[] = {
5515     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5516       .type = ARM_CP_IO,
5517       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5518       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5519       .writefn = hcr_write },
5520     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5521       .type = ARM_CP_ALIAS | ARM_CP_IO,
5522       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5523       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5524       .writefn = hcr_writelow },
5525     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5526       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5527       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5528     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5529       .type = ARM_CP_ALIAS,
5530       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5531       .access = PL2_RW,
5532       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5533     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5534       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5535       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5536     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5537       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5538       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5539     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5540       .type = ARM_CP_ALIAS,
5541       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5542       .access = PL2_RW,
5543       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5544     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5545       .type = ARM_CP_ALIAS,
5546       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5547       .access = PL2_RW,
5548       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5549     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5550       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5551       .access = PL2_RW, .writefn = vbar_write,
5552       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5553       .resetvalue = 0 },
5554     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5555       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5556       .access = PL3_RW, .type = ARM_CP_ALIAS,
5557       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5558     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5559       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5560       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5561       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5562       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5563     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5564       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5565       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5566       .resetvalue = 0 },
5567     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5568       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5569       .access = PL2_RW, .type = ARM_CP_ALIAS,
5570       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5571     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5572       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5573       .access = PL2_RW, .type = ARM_CP_CONST,
5574       .resetvalue = 0 },
5575     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5576     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5577       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5578       .access = PL2_RW, .type = ARM_CP_CONST,
5579       .resetvalue = 0 },
5580     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5581       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5582       .access = PL2_RW, .type = ARM_CP_CONST,
5583       .resetvalue = 0 },
5584     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5585       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5586       .access = PL2_RW, .type = ARM_CP_CONST,
5587       .resetvalue = 0 },
5588     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5589       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5590       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5591       /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
5592       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5593     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5594       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5595       .type = ARM_CP_ALIAS,
5596       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5597       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5598     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5599       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5600       .access = PL2_RW,
5601       /* no .writefn needed as this can't cause an ASID change;
5602        * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5603        */
5604       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5605     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5606       .cp = 15, .opc1 = 6, .crm = 2,
5607       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5608       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5609       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5610       .writefn = vttbr_write },
5611     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5612       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5613       .access = PL2_RW, .writefn = vttbr_write,
5614       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5615     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5616       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5617       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5618       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5619     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5620       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5621       .access = PL2_RW, .resetvalue = 0,
5622       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5623     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5624       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5625       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5626       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5627     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5628       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5629       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5630     { .name = "TLBIALLNSNH",
5631       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5632       .type = ARM_CP_NO_RAW, .access = PL2_W,
5633       .writefn = tlbiall_nsnh_write },
5634     { .name = "TLBIALLNSNHIS",
5635       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5636       .type = ARM_CP_NO_RAW, .access = PL2_W,
5637       .writefn = tlbiall_nsnh_is_write },
5638     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5639       .type = ARM_CP_NO_RAW, .access = PL2_W,
5640       .writefn = tlbiall_hyp_write },
5641     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5642       .type = ARM_CP_NO_RAW, .access = PL2_W,
5643       .writefn = tlbiall_hyp_is_write },
5644     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5645       .type = ARM_CP_NO_RAW, .access = PL2_W,
5646       .writefn = tlbimva_hyp_write },
5647     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5648       .type = ARM_CP_NO_RAW, .access = PL2_W,
5649       .writefn = tlbimva_hyp_is_write },
5650     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5651       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5652       .type = ARM_CP_NO_RAW, .access = PL2_W,
5653       .writefn = tlbi_aa64_alle2_write },
5654     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5655       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5656       .type = ARM_CP_NO_RAW, .access = PL2_W,
5657       .writefn = tlbi_aa64_vae2_write },
5658     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5659       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5660       .access = PL2_W, .type = ARM_CP_NO_RAW,
5661       .writefn = tlbi_aa64_vae2_write },
5662     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5663       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5664       .access = PL2_W, .type = ARM_CP_NO_RAW,
5665       .writefn = tlbi_aa64_alle2is_write },
5666     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5667       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5668       .type = ARM_CP_NO_RAW, .access = PL2_W,
5669       .writefn = tlbi_aa64_vae2is_write },
5670     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5671       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5672       .access = PL2_W, .type = ARM_CP_NO_RAW,
5673       .writefn = tlbi_aa64_vae2is_write },
5674 #ifndef CONFIG_USER_ONLY
5675     /* Unlike the other EL2-related AT operations, these must
5676      * UNDEF from EL3 if EL2 is not implemented, which is why we
5677      * define them here rather than with the rest of the AT ops.
5678      */
5679     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5680       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5681       .access = PL2_W, .accessfn = at_s1e2_access,
5682       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
5683     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5684       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5685       .access = PL2_W, .accessfn = at_s1e2_access,
5686       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
5687     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5688      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5689      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5690      * to behave as if SCR.NS was 1.
5691      */
5692     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5693       .access = PL2_W,
5694       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5695     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5696       .access = PL2_W,
5697       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5698     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5699       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5700       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5701        * reset values as IMPDEF. We choose to reset to 3 to comply with
5702        * both ARMv7 and ARMv8.
5703        */
5704       .access = PL2_RW, .resetvalue = 3,
5705       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5706     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5707       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5708       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5709       .writefn = gt_cntvoff_write,
5710       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5711     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5712       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5713       .writefn = gt_cntvoff_write,
5714       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5715     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5716       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5717       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5718       .type = ARM_CP_IO, .access = PL2_RW,
5719       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5720     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5721       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5722       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5723       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5724     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5725       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5726       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5727       .resetfn = gt_hyp_timer_reset,
5728       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5729     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5730       .type = ARM_CP_IO,
5731       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5732       .access = PL2_RW,
5733       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5734       .resetvalue = 0,
5735       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5736 #endif
5737     /* The only field of MDCR_EL2 that has a defined architectural reset value
5738      * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5739      * don't implement any PMU event counters, so using zero as a reset
5740      * value for MDCR_EL2 is okay
5741      */
5742     { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5743       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5744       .access = PL2_RW, .resetvalue = 0,
5745       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
5746     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5747       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5748       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5749       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5750     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5751       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5752       .access = PL2_RW,
5753       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5754     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5755       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5756       .access = PL2_RW,
5757       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5758     REGINFO_SENTINEL
5759 };
5760
5761 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5762     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5763       .type = ARM_CP_ALIAS | ARM_CP_IO,
5764       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5765       .access = PL2_RW,
5766       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5767       .writefn = hcr_writehigh },
5768     REGINFO_SENTINEL
5769 };
5770
5771 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5772                                   bool isread)
5773 {
5774     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5775         return CP_ACCESS_OK;
5776     }
5777     return CP_ACCESS_TRAP_UNCATEGORIZED;
5778 }
5779
5780 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5781     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5782       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5783       .access = PL2_RW, .accessfn = sel2_access,
5784       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5785     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5786       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5787       .access = PL2_RW, .accessfn = sel2_access,
5788       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5789     REGINFO_SENTINEL
5790 };
5791
5792 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5793                                    bool isread)
5794 {
5795     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5796      * At Secure EL1 it traps to EL3 or EL2.
5797      */
5798     if (arm_current_el(env) == 3) {
5799         return CP_ACCESS_OK;
5800     }
5801     if (arm_is_secure_below_el3(env)) {
5802         if (env->cp15.scr_el3 & SCR_EEL2) {
5803             return CP_ACCESS_TRAP_EL2;
5804         }
5805         return CP_ACCESS_TRAP_EL3;
5806     }
5807     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5808     if (isread) {
5809         return CP_ACCESS_OK;
5810     }
5811     return CP_ACCESS_TRAP_UNCATEGORIZED;
5812 }
5813
5814 static const ARMCPRegInfo el3_cp_reginfo[] = {
5815     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5816       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5817       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5818       .resetfn = scr_reset, .writefn = scr_write },
5819     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5820       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5821       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5822       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5823       .writefn = scr_write },
5824     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5825       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5826       .access = PL3_RW, .resetvalue = 0,
5827       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5828     { .name = "SDER",
5829       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5830       .access = PL3_RW, .resetvalue = 0,
5831       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5832     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5833       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5834       .writefn = vbar_write, .resetvalue = 0,
5835       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5836     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5837       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5838       .access = PL3_RW, .resetvalue = 0,
5839       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5840     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5841       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5842       .access = PL3_RW,
5843       /* no .writefn needed as this can't cause an ASID change;
5844        * we must provide a .raw_writefn and .resetfn because we handle
5845        * reset and migration for the AArch32 TTBCR(S), which might be
5846        * using mask and base_mask.
5847        */
5848       .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5849       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5850     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5851       .type = ARM_CP_ALIAS,
5852       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5853       .access = PL3_RW,
5854       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5855     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5856       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5857       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5858     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5859       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5860       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5861     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5862       .type = ARM_CP_ALIAS,
5863       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5864       .access = PL3_RW,
5865       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5866     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5867       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5868       .access = PL3_RW, .writefn = vbar_write,
5869       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5870       .resetvalue = 0 },
5871     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5872       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5873       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5874       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5875     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5876       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5877       .access = PL3_RW, .resetvalue = 0,
5878       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5879     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5880       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5881       .access = PL3_RW, .type = ARM_CP_CONST,
5882       .resetvalue = 0 },
5883     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5884       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5885       .access = PL3_RW, .type = ARM_CP_CONST,
5886       .resetvalue = 0 },
5887     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5888       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5889       .access = PL3_RW, .type = ARM_CP_CONST,
5890       .resetvalue = 0 },
5891     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5892       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5893       .access = PL3_W, .type = ARM_CP_NO_RAW,
5894       .writefn = tlbi_aa64_alle3is_write },
5895     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5896       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5897       .access = PL3_W, .type = ARM_CP_NO_RAW,
5898       .writefn = tlbi_aa64_vae3is_write },
5899     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5900       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5901       .access = PL3_W, .type = ARM_CP_NO_RAW,
5902       .writefn = tlbi_aa64_vae3is_write },
5903     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5904       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5905       .access = PL3_W, .type = ARM_CP_NO_RAW,
5906       .writefn = tlbi_aa64_alle3_write },
5907     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5908       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5909       .access = PL3_W, .type = ARM_CP_NO_RAW,
5910       .writefn = tlbi_aa64_vae3_write },
5911     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5912       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5913       .access = PL3_W, .type = ARM_CP_NO_RAW,
5914       .writefn = tlbi_aa64_vae3_write },
5915     REGINFO_SENTINEL
5916 };
5917
5918 #ifndef CONFIG_USER_ONLY
5919 /* Test if system register redirection is to occur in the current state.  */
5920 static bool redirect_for_e2h(CPUARMState *env)
5921 {
5922     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5923 }
5924
5925 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5926 {
5927     CPReadFn *readfn;
5928
5929     if (redirect_for_e2h(env)) {
5930         /* Switch to the saved EL2 version of the register.  */
5931         ri = ri->opaque;
5932         readfn = ri->readfn;
5933     } else {
5934         readfn = ri->orig_readfn;
5935     }
5936     if (readfn == NULL) {
5937         readfn = raw_read;
5938     }
5939     return readfn(env, ri);
5940 }
5941
5942 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5943                           uint64_t value)
5944 {
5945     CPWriteFn *writefn;
5946
5947     if (redirect_for_e2h(env)) {
5948         /* Switch to the saved EL2 version of the register.  */
5949         ri = ri->opaque;
5950         writefn = ri->writefn;
5951     } else {
5952         writefn = ri->orig_writefn;
5953     }
5954     if (writefn == NULL) {
5955         writefn = raw_write;
5956     }
5957     writefn(env, ri, value);
5958 }
5959
5960 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5961 {
5962     struct E2HAlias {
5963         uint32_t src_key, dst_key, new_key;
5964         const char *src_name, *dst_name, *new_name;
5965         bool (*feature)(const ARMISARegisters *id);
5966     };
5967
5968 #define K(op0, op1, crn, crm, op2) \
5969     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5970
5971     static const struct E2HAlias aliases[] = {
5972         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5973           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5974         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5975           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5976         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5977           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5978         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5979           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5980         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5981           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5982         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5983           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5984         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5985           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5986         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5987           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5988         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5989           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5990         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5991           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5992         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5993           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5994         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5995           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5996         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5997           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5998         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5999           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6000         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6001           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6002         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6003           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6004
6005         /*
6006          * Note that redirection of ZCR is mentioned in the description
6007          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6008          * not in the summary table.
6009          */
6010         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6011           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6012
6013         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6014           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6015
6016         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6017         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6018     };
6019 #undef K
6020
6021     size_t i;
6022
6023     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6024         const struct E2HAlias *a = &aliases[i];
6025         ARMCPRegInfo *src_reg, *dst_reg;
6026
6027         if (a->feature && !a->feature(&cpu->isar)) {
6028             continue;
6029         }
6030
6031         src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
6032         dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
6033         g_assert(src_reg != NULL);
6034         g_assert(dst_reg != NULL);
6035
6036         /* Cross-compare names to detect typos in the keys.  */
6037         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6038         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6039
6040         /* None of the core system registers use opaque; we will.  */
6041         g_assert(src_reg->opaque == NULL);
6042
6043         /* Create alias before redirection so we dup the right data. */
6044         if (a->new_key) {
6045             ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6046             uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
6047             bool ok;
6048
6049             new_reg->name = a->new_name;
6050             new_reg->type |= ARM_CP_ALIAS;
6051             /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6052             new_reg->access &= PL2_RW | PL3_RW;
6053
6054             ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
6055             g_assert(ok);
6056         }
6057
6058         src_reg->opaque = dst_reg;
6059         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6060         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6061         if (!src_reg->raw_readfn) {
6062             src_reg->raw_readfn = raw_read;
6063         }
6064         if (!src_reg->raw_writefn) {
6065             src_reg->raw_writefn = raw_write;
6066         }
6067         src_reg->readfn = el2_e2h_read;
6068         src_reg->writefn = el2_e2h_write;
6069     }
6070 }
6071 #endif
6072
6073 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6074                                      bool isread)
6075 {
6076     int cur_el = arm_current_el(env);
6077
6078     if (cur_el < 2) {
6079         uint64_t hcr = arm_hcr_el2_eff(env);
6080
6081         if (cur_el == 0) {
6082             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6083                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6084                     return CP_ACCESS_TRAP_EL2;
6085                 }
6086             } else {
6087                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6088                     return CP_ACCESS_TRAP;
6089                 }
6090                 if (hcr & HCR_TID2) {
6091                     return CP_ACCESS_TRAP_EL2;
6092                 }
6093             }
6094         } else if (hcr & HCR_TID2) {
6095             return CP_ACCESS_TRAP_EL2;
6096         }
6097     }
6098
6099     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6100         return CP_ACCESS_TRAP_EL2;
6101     }
6102
6103     return CP_ACCESS_OK;
6104 }
6105
6106 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
6107                         uint64_t value)
6108 {
6109     /* Writes to OSLAR_EL1 may update the OS lock status, which can be
6110      * read via a bit in OSLSR_EL1.
6111      */
6112     int oslock;
6113
6114     if (ri->state == ARM_CP_STATE_AA32) {
6115         oslock = (value == 0xC5ACCE55);
6116     } else {
6117         oslock = value & 1;
6118     }
6119
6120     env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
6121 }
6122
6123 static const ARMCPRegInfo debug_cp_reginfo[] = {
6124     /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
6125      * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
6126      * unlike DBGDRAR it is never accessible from EL0.
6127      * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6128      * accessor.
6129      */
6130     { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
6131       .access = PL0_R, .accessfn = access_tdra,
6132       .type = ARM_CP_CONST, .resetvalue = 0 },
6133     { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6134       .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
6135       .access = PL1_R, .accessfn = access_tdra,
6136       .type = ARM_CP_CONST, .resetvalue = 0 },
6137     { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
6138       .access = PL0_R, .accessfn = access_tdra,
6139       .type = ARM_CP_CONST, .resetvalue = 0 },
6140     /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
6141     { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6142       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
6143       .access = PL1_RW, .accessfn = access_tda,
6144       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6145       .resetvalue = 0 },
6146     /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
6147      * We don't implement the configurable EL0 access.
6148      */
6149     { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
6150       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6151       .type = ARM_CP_ALIAS,
6152       .access = PL1_R, .accessfn = access_tda,
6153       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
6154     { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6155       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
6156       .access = PL1_W, .type = ARM_CP_NO_RAW,
6157       .accessfn = access_tdosa,
6158       .writefn = oslar_write },
6159     { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6160       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6161       .access = PL1_R, .resetvalue = 10,
6162       .accessfn = access_tdosa,
6163       .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
6164     /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6165     { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6166       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
6167       .access = PL1_RW, .accessfn = access_tdosa,
6168       .type = ARM_CP_NOP },
6169     /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6170      * implement vector catch debug events yet.
6171      */
6172     { .name = "DBGVCR",
6173       .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6174       .access = PL1_RW, .accessfn = access_tda,
6175       .type = ARM_CP_NOP },
6176     /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6177      * to save and restore a 32-bit guest's DBGVCR)
6178      */
6179     { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6180       .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6181       .access = PL2_RW, .accessfn = access_tda,
6182       .type = ARM_CP_NOP },
6183     /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6184      * Channel but Linux may try to access this register. The 32-bit
6185      * alias is DBGDCCINT.
6186      */
6187     { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6188       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6189       .access = PL1_RW, .accessfn = access_tda,
6190       .type = ARM_CP_NOP },
6191     REGINFO_SENTINEL
6192 };
6193
6194 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6195     /* 64 bit access versions of the (dummy) debug registers */
6196     { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6197       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6198     { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6199       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6200     REGINFO_SENTINEL
6201 };
6202
6203 /* Return the exception level to which exceptions should be taken
6204  * via SVEAccessTrap.  If an exception should be routed through
6205  * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6206  * take care of raising that exception.
6207  * C.f. the ARM pseudocode function CheckSVEEnabled.
6208  */
6209 int sve_exception_el(CPUARMState *env, int el)
6210 {
6211 #ifndef CONFIG_USER_ONLY
6212     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6213
6214     if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
6215         bool disabled = false;
6216
6217         /* The CPACR.ZEN controls traps to EL1:
6218          * 0, 2 : trap EL0 and EL1 accesses
6219          * 1    : trap only EL0 accesses
6220          * 3    : trap no accesses
6221          */
6222         if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
6223             disabled = true;
6224         } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
6225             disabled = el == 0;
6226         }
6227         if (disabled) {
6228             /* route_to_el2 */
6229             return hcr_el2 & HCR_TGE ? 2 : 1;
6230         }
6231
6232         /* Check CPACR.FPEN.  */
6233         if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
6234             disabled = true;
6235         } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
6236             disabled = el == 0;
6237         }
6238         if (disabled) {
6239             return 0;
6240         }
6241     }
6242
6243     /* CPTR_EL2.  Since TZ and TFP are positive,
6244      * they will be zero when EL2 is not present.
6245      */
6246     if (el <= 2 && arm_is_el2_enabled(env)) {
6247         if (env->cp15.cptr_el[2] & CPTR_TZ) {
6248             return 2;
6249         }
6250         if (env->cp15.cptr_el[2] & CPTR_TFP) {
6251             return 0;
6252         }
6253     }
6254
6255     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6256     if (arm_feature(env, ARM_FEATURE_EL3)
6257         && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
6258         return 3;
6259     }
6260 #endif
6261     return 0;
6262 }
6263
6264 static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6265 {
6266     uint32_t end_len;
6267
6268     end_len = start_len &= 0xf;
6269     if (!test_bit(start_len, cpu->sve_vq_map)) {
6270         end_len = find_last_bit(cpu->sve_vq_map, start_len);
6271         assert(end_len < start_len);
6272     }
6273     return end_len;
6274 }
6275
6276 /*
6277  * Given that SVE is enabled, return the vector length for EL.
6278  */
6279 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
6280 {
6281     ARMCPU *cpu = env_archcpu(env);
6282     uint32_t zcr_len = cpu->sve_max_vq - 1;
6283
6284     if (el <= 1) {
6285         zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6286     }
6287     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6288         zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6289     }
6290     if (arm_feature(env, ARM_FEATURE_EL3)) {
6291         zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6292     }
6293
6294     return sve_zcr_get_valid_len(cpu, zcr_len);
6295 }
6296
6297 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6298                       uint64_t value)
6299 {
6300     int cur_el = arm_current_el(env);
6301     int old_len = sve_zcr_len_for_el(env, cur_el);
6302     int new_len;
6303
6304     /* Bits other than [3:0] are RAZ/WI.  */
6305     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6306     raw_write(env, ri, value & 0xf);
6307
6308     /*
6309      * Because we arrived here, we know both FP and SVE are enabled;
6310      * otherwise we would have trapped access to the ZCR_ELn register.
6311      */
6312     new_len = sve_zcr_len_for_el(env, cur_el);
6313     if (new_len < old_len) {
6314         aarch64_sve_narrow_vq(env, new_len + 1);
6315     }
6316 }
6317
6318 static const ARMCPRegInfo zcr_el1_reginfo = {
6319     .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6320     .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6321     .access = PL1_RW, .type = ARM_CP_SVE,
6322     .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6323     .writefn = zcr_write, .raw_writefn = raw_write
6324 };
6325
6326 static const ARMCPRegInfo zcr_el2_reginfo = {
6327     .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6328     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6329     .access = PL2_RW, .type = ARM_CP_SVE,
6330     .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6331     .writefn = zcr_write, .raw_writefn = raw_write
6332 };
6333
6334 static const ARMCPRegInfo zcr_no_el2_reginfo = {
6335     .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6336     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6337     .access = PL2_RW, .type = ARM_CP_SVE,
6338     .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6339 };
6340
6341 static const ARMCPRegInfo zcr_el3_reginfo = {
6342     .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6343     .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6344     .access = PL3_RW, .type = ARM_CP_SVE,
6345     .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6346     .writefn = zcr_write, .raw_writefn = raw_write
6347 };
6348
6349 void hw_watchpoint_update(ARMCPU *cpu, int n)
6350 {
6351     CPUARMState *env = &cpu->env;
6352     vaddr len = 0;
6353     vaddr wvr = env->cp15.dbgwvr[n];
6354     uint64_t wcr = env->cp15.dbgwcr[n];
6355     int mask;
6356     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6357
6358     if (env->cpu_watchpoint[n]) {
6359         cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6360         env->cpu_watchpoint[n] = NULL;
6361     }
6362
6363     if (!extract64(wcr, 0, 1)) {
6364         /* E bit clear : watchpoint disabled */
6365         return;
6366     }
6367
6368     switch (extract64(wcr, 3, 2)) {
6369     case 0:
6370         /* LSC 00 is reserved and must behave as if the wp is disabled */
6371         return;
6372     case 1:
6373         flags |= BP_MEM_READ;
6374         break;
6375     case 2:
6376         flags |= BP_MEM_WRITE;
6377         break;
6378     case 3:
6379         flags |= BP_MEM_ACCESS;
6380         break;
6381     }
6382
6383     /* Attempts to use both MASK and BAS fields simultaneously are
6384      * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6385      * thus generating a watchpoint for every byte in the masked region.
6386      */
6387     mask = extract64(wcr, 24, 4);
6388     if (mask == 1 || mask == 2) {
6389         /* Reserved values of MASK; we must act as if the mask value was
6390          * some non-reserved value, or as if the watchpoint were disabled.
6391          * We choose the latter.
6392          */
6393         return;
6394     } else if (mask) {
6395         /* Watchpoint covers an aligned area up to 2GB in size */
6396         len = 1ULL << mask;
6397         /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6398          * whether the watchpoint fires when the unmasked bits match; we opt
6399          * to generate the exceptions.
6400          */
6401         wvr &= ~(len - 1);
6402     } else {
6403         /* Watchpoint covers bytes defined by the byte address select bits */
6404         int bas = extract64(wcr, 5, 8);
6405         int basstart;
6406
6407         if (extract64(wvr, 2, 1)) {
6408             /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6409              * ignored, and BAS[3:0] define which bytes to watch.
6410              */
6411             bas &= 0xf;
6412         }
6413
6414         if (bas == 0) {
6415             /* This must act as if the watchpoint is disabled */
6416             return;
6417         }
6418
6419         /* The BAS bits are supposed to be programmed to indicate a contiguous
6420          * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6421          * we fire for each byte in the word/doubleword addressed by the WVR.
6422          * We choose to ignore any non-zero bits after the first range of 1s.
6423          */
6424         basstart = ctz32(bas);
6425         len = cto32(bas >> basstart);
6426         wvr += basstart;
6427     }
6428
6429     cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6430                           &env->cpu_watchpoint[n]);
6431 }
6432
6433 void hw_watchpoint_update_all(ARMCPU *cpu)
6434 {
6435     int i;
6436     CPUARMState *env = &cpu->env;
6437
6438     /* Completely clear out existing QEMU watchpoints and our array, to
6439      * avoid possible stale entries following migration load.
6440      */
6441     cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6442     memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6443
6444     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6445         hw_watchpoint_update(cpu, i);
6446     }
6447 }
6448
6449 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6450                          uint64_t value)
6451 {
6452     ARMCPU *cpu = env_archcpu(env);
6453     int i = ri->crm;
6454
6455     /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6456      * register reads and behaves as if values written are sign extended.
6457      * Bits [1:0] are RES0.
6458      */
6459     value = sextract64(value, 0, 49) & ~3ULL;
6460
6461     raw_write(env, ri, value);
6462     hw_watchpoint_update(cpu, i);
6463 }
6464
6465 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6466                          uint64_t value)
6467 {
6468     ARMCPU *cpu = env_archcpu(env);
6469     int i = ri->crm;
6470
6471     raw_write(env, ri, value);
6472     hw_watchpoint_update(cpu, i);
6473 }
6474
6475 void hw_breakpoint_update(ARMCPU *cpu, int n)
6476 {
6477     CPUARMState *env = &cpu->env;
6478     uint64_t bvr = env->cp15.dbgbvr[n];
6479     uint64_t bcr = env->cp15.dbgbcr[n];
6480     vaddr addr;
6481     int bt;
6482     int flags = BP_CPU;
6483
6484     if (env->cpu_breakpoint[n]) {
6485         cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6486         env->cpu_breakpoint[n] = NULL;
6487     }
6488
6489     if (!extract64(bcr, 0, 1)) {
6490         /* E bit clear : watchpoint disabled */
6491         return;
6492     }
6493
6494     bt = extract64(bcr, 20, 4);
6495
6496     switch (bt) {
6497     case 4: /* unlinked address mismatch (reserved if AArch64) */
6498     case 5: /* linked address mismatch (reserved if AArch64) */
6499         qemu_log_mask(LOG_UNIMP,
6500                       "arm: address mismatch breakpoint types not implemented\n");
6501         return;
6502     case 0: /* unlinked address match */
6503     case 1: /* linked address match */
6504     {
6505         /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6506          * we behave as if the register was sign extended. Bits [1:0] are
6507          * RES0. The BAS field is used to allow setting breakpoints on 16
6508          * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6509          * a bp will fire if the addresses covered by the bp and the addresses
6510          * covered by the insn overlap but the insn doesn't start at the
6511          * start of the bp address range. We choose to require the insn and
6512          * the bp to have the same address. The constraints on writing to
6513          * BAS enforced in dbgbcr_write mean we have only four cases:
6514          *  0b0000  => no breakpoint
6515          *  0b0011  => breakpoint on addr
6516          *  0b1100  => breakpoint on addr + 2
6517          *  0b1111  => breakpoint on addr
6518          * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6519          */
6520         int bas = extract64(bcr, 5, 4);
6521         addr = sextract64(bvr, 0, 49) & ~3ULL;
6522         if (bas == 0) {
6523             return;
6524         }
6525         if (bas == 0xc) {
6526             addr += 2;
6527         }
6528         break;
6529     }
6530     case 2: /* unlinked context ID match */
6531     case 8: /* unlinked VMID match (reserved if no EL2) */
6532     case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6533         qemu_log_mask(LOG_UNIMP,
6534                       "arm: unlinked context breakpoint types not implemented\n");
6535         return;
6536     case 9: /* linked VMID match (reserved if no EL2) */
6537     case 11: /* linked context ID and VMID match (reserved if no EL2) */
6538     case 3: /* linked context ID match */
6539     default:
6540         /* We must generate no events for Linked context matches (unless
6541          * they are linked to by some other bp/wp, which is handled in
6542          * updates for the linking bp/wp). We choose to also generate no events
6543          * for reserved values.
6544          */
6545         return;
6546     }
6547
6548     cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6549 }
6550
6551 void hw_breakpoint_update_all(ARMCPU *cpu)
6552 {
6553     int i;
6554     CPUARMState *env = &cpu->env;
6555
6556     /* Completely clear out existing QEMU breakpoints and our array, to
6557      * avoid possible stale entries following migration load.
6558      */
6559     cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6560     memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6561
6562     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6563         hw_breakpoint_update(cpu, i);
6564     }
6565 }
6566
6567 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6568                          uint64_t value)
6569 {
6570     ARMCPU *cpu = env_archcpu(env);
6571     int i = ri->crm;
6572
6573     raw_write(env, ri, value);
6574     hw_breakpoint_update(cpu, i);
6575 }
6576
6577 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6578                          uint64_t value)
6579 {
6580     ARMCPU *cpu = env_archcpu(env);
6581     int i = ri->crm;
6582
6583     /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6584      * copy of BAS[0].
6585      */
6586     value = deposit64(value, 6, 1, extract64(value, 5, 1));
6587     value = deposit64(value, 8, 1, extract64(value, 7, 1));
6588
6589     raw_write(env, ri, value);
6590     hw_breakpoint_update(cpu, i);
6591 }
6592
6593 static void define_debug_regs(ARMCPU *cpu)
6594 {
6595     /* Define v7 and v8 architectural debug registers.
6596      * These are just dummy implementations for now.
6597      */
6598     int i;
6599     int wrps, brps, ctx_cmps;
6600
6601     /*
6602      * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6603      * use AArch32.  Given that bit 15 is RES1, if the value is 0 then
6604      * the register must not exist for this cpu.
6605      */
6606     if (cpu->isar.dbgdidr != 0) {
6607         ARMCPRegInfo dbgdidr = {
6608             .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6609             .opc1 = 0, .opc2 = 0,
6610             .access = PL0_R, .accessfn = access_tda,
6611             .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6612         };
6613         define_one_arm_cp_reg(cpu, &dbgdidr);
6614     }
6615
6616     /* Note that all these register fields hold "number of Xs minus 1". */
6617     brps = arm_num_brps(cpu);
6618     wrps = arm_num_wrps(cpu);
6619     ctx_cmps = arm_num_ctx_cmps(cpu);
6620
6621     assert(ctx_cmps <= brps);
6622
6623     define_arm_cp_regs(cpu, debug_cp_reginfo);
6624
6625     if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6626         define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6627     }
6628
6629     for (i = 0; i < brps; i++) {
6630         ARMCPRegInfo dbgregs[] = {
6631             { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6632               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
6633               .access = PL1_RW, .accessfn = access_tda,
6634               .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6635               .writefn = dbgbvr_write, .raw_writefn = raw_write
6636             },
6637             { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6638               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
6639               .access = PL1_RW, .accessfn = access_tda,
6640               .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6641               .writefn = dbgbcr_write, .raw_writefn = raw_write
6642             },
6643             REGINFO_SENTINEL
6644         };
6645         define_arm_cp_regs(cpu, dbgregs);
6646     }
6647
6648     for (i = 0; i < wrps; i++) {
6649         ARMCPRegInfo dbgregs[] = {
6650             { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6651               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
6652               .access = PL1_RW, .accessfn = access_tda,
6653               .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6654               .writefn = dbgwvr_write, .raw_writefn = raw_write
6655             },
6656             { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6657               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
6658               .access = PL1_RW, .accessfn = access_tda,
6659               .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6660               .writefn = dbgwcr_write, .raw_writefn = raw_write
6661             },
6662             REGINFO_SENTINEL
6663         };
6664         define_arm_cp_regs(cpu, dbgregs);
6665     }
6666 }
6667
6668 static void define_pmu_regs(ARMCPU *cpu)
6669 {
6670     /*
6671      * v7 performance monitor control register: same implementor
6672      * field as main ID register, and we implement four counters in
6673      * addition to the cycle count register.
6674      */
6675     unsigned int i, pmcrn = 4;
6676     ARMCPRegInfo pmcr = {
6677         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6678         .access = PL0_RW,
6679         .type = ARM_CP_IO | ARM_CP_ALIAS,
6680         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6681         .accessfn = pmreg_access, .writefn = pmcr_write,
6682         .raw_writefn = raw_write,
6683     };
6684     ARMCPRegInfo pmcr64 = {
6685         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6686         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6687         .access = PL0_RW, .accessfn = pmreg_access,
6688         .type = ARM_CP_IO,
6689         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6690         .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6691                       PMCRLC,
6692         .writefn = pmcr_write, .raw_writefn = raw_write,
6693     };
6694     define_one_arm_cp_reg(cpu, &pmcr);
6695     define_one_arm_cp_reg(cpu, &pmcr64);
6696     for (i = 0; i < pmcrn; i++) {
6697         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6698         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6699         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6700         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6701         ARMCPRegInfo pmev_regs[] = {
6702             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6703               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6704               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6705               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6706               .accessfn = pmreg_access },
6707             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6708               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6709               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6710               .type = ARM_CP_IO,
6711               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6712               .raw_readfn = pmevcntr_rawread,
6713               .raw_writefn = pmevcntr_rawwrite },
6714             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6715               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6716               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6717               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6718               .accessfn = pmreg_access },
6719             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6720               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6721               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6722               .type = ARM_CP_IO,
6723               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6724               .raw_writefn = pmevtyper_rawwrite },
6725             REGINFO_SENTINEL
6726         };
6727         define_arm_cp_regs(cpu, pmev_regs);
6728         g_free(pmevcntr_name);
6729         g_free(pmevcntr_el0_name);
6730         g_free(pmevtyper_name);
6731         g_free(pmevtyper_el0_name);
6732     }
6733     if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
6734         ARMCPRegInfo v81_pmu_regs[] = {
6735             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6736               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6737               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6738               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6739             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6740               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6741               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6742               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6743             REGINFO_SENTINEL
6744         };
6745         define_arm_cp_regs(cpu, v81_pmu_regs);
6746     }
6747     if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6748         static const ARMCPRegInfo v84_pmmir = {
6749             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6750             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6751             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6752             .resetvalue = 0
6753         };
6754         define_one_arm_cp_reg(cpu, &v84_pmmir);
6755     }
6756 }
6757
6758 /* We don't know until after realize whether there's a GICv3
6759  * attached, and that is what registers the gicv3 sysregs.
6760  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6761  * at runtime.
6762  */
6763 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6764 {
6765     ARMCPU *cpu = env_archcpu(env);
6766     uint64_t pfr1 = cpu->isar.id_pfr1;
6767
6768     if (env->gicv3state) {
6769         pfr1 |= 1 << 28;
6770     }
6771     return pfr1;
6772 }
6773
6774 #ifndef CONFIG_USER_ONLY
6775 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6776 {
6777     ARMCPU *cpu = env_archcpu(env);
6778     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6779
6780     if (env->gicv3state) {
6781         pfr0 |= 1 << 24;
6782     }
6783     return pfr0;
6784 }
6785 #endif
6786
6787 /* Shared logic between LORID and the rest of the LOR* registers.
6788  * Secure state exclusion has already been dealt with.
6789  */
6790 static CPAccessResult access_lor_ns(CPUARMState *env,
6791                                     const ARMCPRegInfo *ri, bool isread)
6792 {
6793     int el = arm_current_el(env);
6794
6795     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6796         return CP_ACCESS_TRAP_EL2;
6797     }
6798     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6799         return CP_ACCESS_TRAP_EL3;
6800     }
6801     return CP_ACCESS_OK;
6802 }
6803
6804 static CPAccessResult access_lor_other(CPUARMState *env,
6805                                        const ARMCPRegInfo *ri, bool isread)
6806 {
6807     if (arm_is_secure_below_el3(env)) {
6808         /* Access denied in secure mode.  */
6809         return CP_ACCESS_TRAP;
6810     }
6811     return access_lor_ns(env, ri, isread);
6812 }
6813
6814 /*
6815  * A trivial implementation of ARMv8.1-LOR leaves all of these
6816  * registers fixed at 0, which indicates that there are zero
6817  * supported Limited Ordering regions.
6818  */
6819 static const ARMCPRegInfo lor_reginfo[] = {
6820     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6821       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6822       .access = PL1_RW, .accessfn = access_lor_other,
6823       .type = ARM_CP_CONST, .resetvalue = 0 },
6824     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6825       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6826       .access = PL1_RW, .accessfn = access_lor_other,
6827       .type = ARM_CP_CONST, .resetvalue = 0 },
6828     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6829       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6830       .access = PL1_RW, .accessfn = access_lor_other,
6831       .type = ARM_CP_CONST, .resetvalue = 0 },
6832     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6833       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6834       .access = PL1_RW, .accessfn = access_lor_other,
6835       .type = ARM_CP_CONST, .resetvalue = 0 },
6836     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6837       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6838       .access = PL1_R, .accessfn = access_lor_ns,
6839       .type = ARM_CP_CONST, .resetvalue = 0 },
6840     REGINFO_SENTINEL
6841 };
6842
6843 #ifdef TARGET_AARCH64
6844 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6845                                    bool isread)
6846 {
6847     int el = arm_current_el(env);
6848
6849     if (el < 2 &&
6850         arm_feature(env, ARM_FEATURE_EL2) &&
6851         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6852         return CP_ACCESS_TRAP_EL2;
6853     }
6854     if (el < 3 &&
6855         arm_feature(env, ARM_FEATURE_EL3) &&
6856         !(env->cp15.scr_el3 & SCR_APK)) {
6857         return CP_ACCESS_TRAP_EL3;
6858     }
6859     return CP_ACCESS_OK;
6860 }
6861
6862 static const ARMCPRegInfo pauth_reginfo[] = {
6863     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6864       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6865       .access = PL1_RW, .accessfn = access_pauth,
6866       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6867     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6868       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6869       .access = PL1_RW, .accessfn = access_pauth,
6870       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6871     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6872       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6873       .access = PL1_RW, .accessfn = access_pauth,
6874       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6875     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6876       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6877       .access = PL1_RW, .accessfn = access_pauth,
6878       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6879     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6880       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6881       .access = PL1_RW, .accessfn = access_pauth,
6882       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6883     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6884       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6885       .access = PL1_RW, .accessfn = access_pauth,
6886       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6887     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6888       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6889       .access = PL1_RW, .accessfn = access_pauth,
6890       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6891     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6892       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6893       .access = PL1_RW, .accessfn = access_pauth,
6894       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6895     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6896       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6897       .access = PL1_RW, .accessfn = access_pauth,
6898       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6899     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6900       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6901       .access = PL1_RW, .accessfn = access_pauth,
6902       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6903     REGINFO_SENTINEL
6904 };
6905
6906 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6907 {
6908     Error *err = NULL;
6909     uint64_t ret;
6910
6911     /* Success sets NZCV = 0000.  */
6912     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6913
6914     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6915         /*
6916          * ??? Failed, for unknown reasons in the crypto subsystem.
6917          * The best we can do is log the reason and return the
6918          * timed-out indication to the guest.  There is no reason
6919          * we know to expect this failure to be transitory, so the
6920          * guest may well hang retrying the operation.
6921          */
6922         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6923                       ri->name, error_get_pretty(err));
6924         error_free(err);
6925
6926         env->ZF = 0; /* NZCF = 0100 */
6927         return 0;
6928     }
6929     return ret;
6930 }
6931
6932 /* We do not support re-seeding, so the two registers operate the same.  */
6933 static const ARMCPRegInfo rndr_reginfo[] = {
6934     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6935       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6936       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6937       .access = PL0_R, .readfn = rndr_readfn },
6938     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6939       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6940       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6941       .access = PL0_R, .readfn = rndr_readfn },
6942     REGINFO_SENTINEL
6943 };
6944
6945 #ifndef CONFIG_USER_ONLY
6946 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6947                           uint64_t value)
6948 {
6949     ARMCPU *cpu = env_archcpu(env);
6950     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6951     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6952     uint64_t vaddr_in = (uint64_t) value;
6953     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6954     void *haddr;
6955     int mem_idx = cpu_mmu_index(env, false);
6956
6957     /* This won't be crossing page boundaries */
6958     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6959     if (haddr) {
6960
6961         ram_addr_t offset;
6962         MemoryRegion *mr;
6963
6964         /* RCU lock is already being held */
6965         mr = memory_region_from_host(haddr, &offset);
6966
6967         if (mr) {
6968             memory_region_writeback(mr, offset, dline_size);
6969         }
6970     }
6971 }
6972
6973 static const ARMCPRegInfo dcpop_reg[] = {
6974     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6975       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6976       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6977       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6978     REGINFO_SENTINEL
6979 };
6980
6981 static const ARMCPRegInfo dcpodp_reg[] = {
6982     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6983       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6984       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6985       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6986     REGINFO_SENTINEL
6987 };
6988 #endif /*CONFIG_USER_ONLY*/
6989
6990 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6991                                        bool isread)
6992 {
6993     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6994         return CP_ACCESS_TRAP_EL2;
6995     }
6996
6997     return CP_ACCESS_OK;
6998 }
6999
7000 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7001                                  bool isread)
7002 {
7003     int el = arm_current_el(env);
7004
7005     if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
7006         uint64_t hcr = arm_hcr_el2_eff(env);
7007         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7008             return CP_ACCESS_TRAP_EL2;
7009         }
7010     }
7011     if (el < 3 &&
7012         arm_feature(env, ARM_FEATURE_EL3) &&
7013         !(env->cp15.scr_el3 & SCR_ATA)) {
7014         return CP_ACCESS_TRAP_EL3;
7015     }
7016     return CP_ACCESS_OK;
7017 }
7018
7019 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7020 {
7021     return env->pstate & PSTATE_TCO;
7022 }
7023
7024 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7025 {
7026     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7027 }
7028
7029 static const ARMCPRegInfo mte_reginfo[] = {
7030     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7031       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7032       .access = PL1_RW, .accessfn = access_mte,
7033       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7034     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7035       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7036       .access = PL1_RW, .accessfn = access_mte,
7037       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7038     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7039       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7040       .access = PL2_RW, .accessfn = access_mte,
7041       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7042     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7043       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7044       .access = PL3_RW,
7045       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7046     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7047       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7048       .access = PL1_RW, .accessfn = access_mte,
7049       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7050     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7051       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7052       .access = PL1_RW, .accessfn = access_mte,
7053       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7054     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7055       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7056       .access = PL1_R, .accessfn = access_aa64_tid5,
7057       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7058     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7059       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7060       .type = ARM_CP_NO_RAW,
7061       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7062     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7063       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7064       .type = ARM_CP_NOP, .access = PL1_W,
7065       .accessfn = aa64_cacheop_poc_access },
7066     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7067       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7068       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7069     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7070       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7071       .type = ARM_CP_NOP, .access = PL1_W,
7072       .accessfn = aa64_cacheop_poc_access },
7073     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7074       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7075       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7076     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7077       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7078       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7079     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7080       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7081       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7082     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7083       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7084       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7085     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7086       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7087       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7088     REGINFO_SENTINEL
7089 };
7090
7091 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7092     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7093       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7094       .type = ARM_CP_CONST, .access = PL0_RW, },
7095     REGINFO_SENTINEL
7096 };
7097
7098 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7099     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7100       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7101       .type = ARM_CP_NOP, .access = PL0_W,
7102       .accessfn = aa64_cacheop_poc_access },
7103     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7104       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7105       .type = ARM_CP_NOP, .access = PL0_W,
7106       .accessfn = aa64_cacheop_poc_access },
7107     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7108       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7109       .type = ARM_CP_NOP, .access = PL0_W,
7110       .accessfn = aa64_cacheop_poc_access },
7111     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7112       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7113       .type = ARM_CP_NOP, .access = PL0_W,
7114       .accessfn = aa64_cacheop_poc_access },
7115     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7116       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7117       .type = ARM_CP_NOP, .access = PL0_W,
7118       .accessfn = aa64_cacheop_poc_access },
7119     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7120       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7121       .type = ARM_CP_NOP, .access = PL0_W,
7122       .accessfn = aa64_cacheop_poc_access },
7123     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7124       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7125       .type = ARM_CP_NOP, .access = PL0_W,
7126       .accessfn = aa64_cacheop_poc_access },
7127     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7128       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7129       .type = ARM_CP_NOP, .access = PL0_W,
7130       .accessfn = aa64_cacheop_poc_access },
7131     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7132       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7133       .access = PL0_W, .type = ARM_CP_DC_GVA,
7134 #ifndef CONFIG_USER_ONLY
7135       /* Avoid overhead of an access check that always passes in user-mode */
7136       .accessfn = aa64_zva_access,
7137 #endif
7138     },
7139     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7140       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7141       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7142 #ifndef CONFIG_USER_ONLY
7143       /* Avoid overhead of an access check that always passes in user-mode */
7144       .accessfn = aa64_zva_access,
7145 #endif
7146     },
7147     REGINFO_SENTINEL
7148 };
7149
7150 #endif
7151
7152 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7153                                      bool isread)
7154 {
7155     int el = arm_current_el(env);
7156
7157     if (el == 0) {
7158         uint64_t sctlr = arm_sctlr(env, el);
7159         if (!(sctlr & SCTLR_EnRCTX)) {
7160             return CP_ACCESS_TRAP;
7161         }
7162     } else if (el == 1) {
7163         uint64_t hcr = arm_hcr_el2_eff(env);
7164         if (hcr & HCR_NV) {
7165             return CP_ACCESS_TRAP_EL2;
7166         }
7167     }
7168     return CP_ACCESS_OK;
7169 }
7170
7171 static const ARMCPRegInfo predinv_reginfo[] = {
7172     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7173       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7174       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7175     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7176       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7177       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7178     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7179       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7180       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7181     /*
7182      * Note the AArch32 opcodes have a different OPC1.
7183      */
7184     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7185       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7186       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7187     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7188       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7189       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7190     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7191       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7192       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7193     REGINFO_SENTINEL
7194 };
7195
7196 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7197 {
7198     /* Read the high 32 bits of the current CCSIDR */
7199     return extract64(ccsidr_read(env, ri), 32, 32);
7200 }
7201
7202 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7203     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7204       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7205       .access = PL1_R,
7206       .accessfn = access_aa64_tid2,
7207       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7208     REGINFO_SENTINEL
7209 };
7210
7211 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7212                                        bool isread)
7213 {
7214     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7215         return CP_ACCESS_TRAP_EL2;
7216     }
7217
7218     return CP_ACCESS_OK;
7219 }
7220
7221 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7222                                        bool isread)
7223 {
7224     if (arm_feature(env, ARM_FEATURE_V8)) {
7225         return access_aa64_tid3(env, ri, isread);
7226     }
7227
7228     return CP_ACCESS_OK;
7229 }
7230
7231 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7232                                      bool isread)
7233 {
7234     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7235         return CP_ACCESS_TRAP_EL2;
7236     }
7237
7238     return CP_ACCESS_OK;
7239 }
7240
7241 static const ARMCPRegInfo jazelle_regs[] = {
7242     { .name = "JIDR",
7243       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7244       .access = PL1_R, .accessfn = access_jazelle,
7245       .type = ARM_CP_CONST, .resetvalue = 0 },
7246     { .name = "JOSCR",
7247       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7248       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7249     { .name = "JMCR",
7250       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7251       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7252     REGINFO_SENTINEL
7253 };
7254
7255 static const ARMCPRegInfo vhe_reginfo[] = {
7256     { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7257       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7258       .access = PL2_RW,
7259       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
7260     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7261       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7262       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7263       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7264 #ifndef CONFIG_USER_ONLY
7265     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7266       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7267       .fieldoffset =
7268         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7269       .type = ARM_CP_IO, .access = PL2_RW,
7270       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7271     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7272       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7273       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7274       .resetfn = gt_hv_timer_reset,
7275       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7276     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7277       .type = ARM_CP_IO,
7278       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7279       .access = PL2_RW,
7280       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7281       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7282     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7283       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7284       .type = ARM_CP_IO | ARM_CP_ALIAS,
7285       .access = PL2_RW, .accessfn = e2h_access,
7286       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7287       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7288     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7289       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7290       .type = ARM_CP_IO | ARM_CP_ALIAS,
7291       .access = PL2_RW, .accessfn = e2h_access,
7292       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7293       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7294     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7295       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7296       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7297       .access = PL2_RW, .accessfn = e2h_access,
7298       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7299     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7300       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7301       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7302       .access = PL2_RW, .accessfn = e2h_access,
7303       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7304     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7305       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7306       .type = ARM_CP_IO | ARM_CP_ALIAS,
7307       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7308       .access = PL2_RW, .accessfn = e2h_access,
7309       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7310     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7311       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7312       .type = ARM_CP_IO | ARM_CP_ALIAS,
7313       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7314       .access = PL2_RW, .accessfn = e2h_access,
7315       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7316 #endif
7317     REGINFO_SENTINEL
7318 };
7319
7320 #ifndef CONFIG_USER_ONLY
7321 static const ARMCPRegInfo ats1e1_reginfo[] = {
7322     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7323       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7324       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7325       .writefn = ats_write64 },
7326     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7327       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7328       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7329       .writefn = ats_write64 },
7330     REGINFO_SENTINEL
7331 };
7332
7333 static const ARMCPRegInfo ats1cp_reginfo[] = {
7334     { .name = "ATS1CPRP",
7335       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7336       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7337       .writefn = ats_write },
7338     { .name = "ATS1CPWP",
7339       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7340       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7341       .writefn = ats_write },
7342     REGINFO_SENTINEL
7343 };
7344 #endif
7345
7346 /*
7347  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7348  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7349  * is non-zero, which is never for ARMv7, optionally in ARMv8
7350  * and mandatorily for ARMv8.2 and up.
7351  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7352  * implementation is RAZ/WI we can ignore this detail, as we
7353  * do for ACTLR.
7354  */
7355 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7356     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7357       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7358       .access = PL1_RW, .accessfn = access_tacr,
7359       .type = ARM_CP_CONST, .resetvalue = 0 },
7360     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7361       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7362       .access = PL2_RW, .type = ARM_CP_CONST,
7363       .resetvalue = 0 },
7364     REGINFO_SENTINEL
7365 };
7366
7367 void register_cp_regs_for_features(ARMCPU *cpu)
7368 {
7369     /* Register all the coprocessor registers based on feature bits */
7370     CPUARMState *env = &cpu->env;
7371     if (arm_feature(env, ARM_FEATURE_M)) {
7372         /* M profile has no coprocessor registers */
7373         return;
7374     }
7375
7376     define_arm_cp_regs(cpu, cp_reginfo);
7377     if (!arm_feature(env, ARM_FEATURE_V8)) {
7378         /* Must go early as it is full of wildcards that may be
7379          * overridden by later definitions.
7380          */
7381         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7382     }
7383
7384     if (arm_feature(env, ARM_FEATURE_V6)) {
7385         /* The ID registers all have impdef reset values */
7386         ARMCPRegInfo v6_idregs[] = {
7387             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7388               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7389               .access = PL1_R, .type = ARM_CP_CONST,
7390               .accessfn = access_aa32_tid3,
7391               .resetvalue = cpu->isar.id_pfr0 },
7392             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7393              * the value of the GIC field until after we define these regs.
7394              */
7395             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7396               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7397               .access = PL1_R, .type = ARM_CP_NO_RAW,
7398               .accessfn = access_aa32_tid3,
7399               .readfn = id_pfr1_read,
7400               .writefn = arm_cp_write_ignore },
7401             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7402               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7403               .access = PL1_R, .type = ARM_CP_CONST,
7404               .accessfn = access_aa32_tid3,
7405               .resetvalue = cpu->isar.id_dfr0 },
7406             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7407               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7408               .access = PL1_R, .type = ARM_CP_CONST,
7409               .accessfn = access_aa32_tid3,
7410               .resetvalue = cpu->id_afr0 },
7411             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7412               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7413               .access = PL1_R, .type = ARM_CP_CONST,
7414               .accessfn = access_aa32_tid3,
7415               .resetvalue = cpu->isar.id_mmfr0 },
7416             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7417               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7418               .access = PL1_R, .type = ARM_CP_CONST,
7419               .accessfn = access_aa32_tid3,
7420               .resetvalue = cpu->isar.id_mmfr1 },
7421             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7422               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7423               .access = PL1_R, .type = ARM_CP_CONST,
7424               .accessfn = access_aa32_tid3,
7425               .resetvalue = cpu->isar.id_mmfr2 },
7426             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7427               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7428               .access = PL1_R, .type = ARM_CP_CONST,
7429               .accessfn = access_aa32_tid3,
7430               .resetvalue = cpu->isar.id_mmfr3 },
7431             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7432               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7433               .access = PL1_R, .type = ARM_CP_CONST,
7434               .accessfn = access_aa32_tid3,
7435               .resetvalue = cpu->isar.id_isar0 },
7436             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7437               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7438               .access = PL1_R, .type = ARM_CP_CONST,
7439               .accessfn = access_aa32_tid3,
7440               .resetvalue = cpu->isar.id_isar1 },
7441             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7442               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7443               .access = PL1_R, .type = ARM_CP_CONST,
7444               .accessfn = access_aa32_tid3,
7445               .resetvalue = cpu->isar.id_isar2 },
7446             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7447               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7448               .access = PL1_R, .type = ARM_CP_CONST,
7449               .accessfn = access_aa32_tid3,
7450               .resetvalue = cpu->isar.id_isar3 },
7451             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7452               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7453               .access = PL1_R, .type = ARM_CP_CONST,
7454               .accessfn = access_aa32_tid3,
7455               .resetvalue = cpu->isar.id_isar4 },
7456             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7457               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7458               .access = PL1_R, .type = ARM_CP_CONST,
7459               .accessfn = access_aa32_tid3,
7460               .resetvalue = cpu->isar.id_isar5 },
7461             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7462               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7463               .access = PL1_R, .type = ARM_CP_CONST,
7464               .accessfn = access_aa32_tid3,
7465               .resetvalue = cpu->isar.id_mmfr4 },
7466             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7467               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7468               .access = PL1_R, .type = ARM_CP_CONST,
7469               .accessfn = access_aa32_tid3,
7470               .resetvalue = cpu->isar.id_isar6 },
7471             REGINFO_SENTINEL
7472         };
7473         define_arm_cp_regs(cpu, v6_idregs);
7474         define_arm_cp_regs(cpu, v6_cp_reginfo);
7475     } else {
7476         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7477     }
7478     if (arm_feature(env, ARM_FEATURE_V6K)) {
7479         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7480     }
7481     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7482         !arm_feature(env, ARM_FEATURE_PMSA)) {
7483         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7484     }
7485     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7486         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7487     }
7488     if (arm_feature(env, ARM_FEATURE_V7)) {
7489         ARMCPRegInfo clidr = {
7490             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7491             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7492             .access = PL1_R, .type = ARM_CP_CONST,
7493             .accessfn = access_aa64_tid2,
7494             .resetvalue = cpu->clidr
7495         };
7496         define_one_arm_cp_reg(cpu, &clidr);
7497         define_arm_cp_regs(cpu, v7_cp_reginfo);
7498         define_debug_regs(cpu);
7499         define_pmu_regs(cpu);
7500     } else {
7501         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7502     }
7503     if (arm_feature(env, ARM_FEATURE_V8)) {
7504         /* AArch64 ID registers, which all have impdef reset values.
7505          * Note that within the ID register ranges the unused slots
7506          * must all RAZ, not UNDEF; future architecture versions may
7507          * define new registers here.
7508          */
7509         ARMCPRegInfo v8_idregs[] = {
7510             /*
7511              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7512              * emulation because we don't know the right value for the
7513              * GIC field until after we define these regs.
7514              */
7515             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7516               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7517               .access = PL1_R,
7518 #ifdef CONFIG_USER_ONLY
7519               .type = ARM_CP_CONST,
7520               .resetvalue = cpu->isar.id_aa64pfr0
7521 #else
7522               .type = ARM_CP_NO_RAW,
7523               .accessfn = access_aa64_tid3,
7524               .readfn = id_aa64pfr0_read,
7525               .writefn = arm_cp_write_ignore
7526 #endif
7527             },
7528             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7529               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7530               .access = PL1_R, .type = ARM_CP_CONST,
7531               .accessfn = access_aa64_tid3,
7532               .resetvalue = cpu->isar.id_aa64pfr1},
7533             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7534               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7535               .access = PL1_R, .type = ARM_CP_CONST,
7536               .accessfn = access_aa64_tid3,
7537               .resetvalue = 0 },
7538             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7539               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7540               .access = PL1_R, .type = ARM_CP_CONST,
7541               .accessfn = access_aa64_tid3,
7542               .resetvalue = 0 },
7543             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7544               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7545               .access = PL1_R, .type = ARM_CP_CONST,
7546               .accessfn = access_aa64_tid3,
7547               /* At present, only SVEver == 0 is defined anyway.  */
7548               .resetvalue = 0 },
7549             { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7550               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7551               .access = PL1_R, .type = ARM_CP_CONST,
7552               .accessfn = access_aa64_tid3,
7553               .resetvalue = 0 },
7554             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7555               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7556               .access = PL1_R, .type = ARM_CP_CONST,
7557               .accessfn = access_aa64_tid3,
7558               .resetvalue = 0 },
7559             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7560               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7561               .access = PL1_R, .type = ARM_CP_CONST,
7562               .accessfn = access_aa64_tid3,
7563               .resetvalue = 0 },
7564             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7565               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7566               .access = PL1_R, .type = ARM_CP_CONST,
7567               .accessfn = access_aa64_tid3,
7568               .resetvalue = cpu->isar.id_aa64dfr0 },
7569             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7570               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7571               .access = PL1_R, .type = ARM_CP_CONST,
7572               .accessfn = access_aa64_tid3,
7573               .resetvalue = cpu->isar.id_aa64dfr1 },
7574             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7575               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7576               .access = PL1_R, .type = ARM_CP_CONST,
7577               .accessfn = access_aa64_tid3,
7578               .resetvalue = 0 },
7579             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7580               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7581               .access = PL1_R, .type = ARM_CP_CONST,
7582               .accessfn = access_aa64_tid3,
7583               .resetvalue = 0 },
7584             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7585               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7586               .access = PL1_R, .type = ARM_CP_CONST,
7587               .accessfn = access_aa64_tid3,
7588               .resetvalue = cpu->id_aa64afr0 },
7589             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7590               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7591               .access = PL1_R, .type = ARM_CP_CONST,
7592               .accessfn = access_aa64_tid3,
7593               .resetvalue = cpu->id_aa64afr1 },
7594             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7595               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7596               .access = PL1_R, .type = ARM_CP_CONST,
7597               .accessfn = access_aa64_tid3,
7598               .resetvalue = 0 },
7599             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7600               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7601               .access = PL1_R, .type = ARM_CP_CONST,
7602               .accessfn = access_aa64_tid3,
7603               .resetvalue = 0 },
7604             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7605               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7606               .access = PL1_R, .type = ARM_CP_CONST,
7607               .accessfn = access_aa64_tid3,
7608               .resetvalue = cpu->isar.id_aa64isar0 },
7609             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7610               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7611               .access = PL1_R, .type = ARM_CP_CONST,
7612               .accessfn = access_aa64_tid3,
7613               .resetvalue = cpu->isar.id_aa64isar1 },
7614             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7615               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7616               .access = PL1_R, .type = ARM_CP_CONST,
7617               .accessfn = access_aa64_tid3,
7618               .resetvalue = 0 },
7619             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7620               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7621               .access = PL1_R, .type = ARM_CP_CONST,
7622               .accessfn = access_aa64_tid3,
7623               .resetvalue = 0 },
7624             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7625               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7626               .access = PL1_R, .type = ARM_CP_CONST,
7627               .accessfn = access_aa64_tid3,
7628               .resetvalue = 0 },
7629             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7630               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7631               .access = PL1_R, .type = ARM_CP_CONST,
7632               .accessfn = access_aa64_tid3,
7633               .resetvalue = 0 },
7634             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7635               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7636               .access = PL1_R, .type = ARM_CP_CONST,
7637               .accessfn = access_aa64_tid3,
7638               .resetvalue = 0 },
7639             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7640               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7641               .access = PL1_R, .type = ARM_CP_CONST,
7642               .accessfn = access_aa64_tid3,
7643               .resetvalue = 0 },
7644             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7645               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7646               .access = PL1_R, .type = ARM_CP_CONST,
7647               .accessfn = access_aa64_tid3,
7648               .resetvalue = cpu->isar.id_aa64mmfr0 },
7649             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7650               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7651               .access = PL1_R, .type = ARM_CP_CONST,
7652               .accessfn = access_aa64_tid3,
7653               .resetvalue = cpu->isar.id_aa64mmfr1 },
7654             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7655               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7656               .access = PL1_R, .type = ARM_CP_CONST,
7657               .accessfn = access_aa64_tid3,
7658               .resetvalue = cpu->isar.id_aa64mmfr2 },
7659             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7660               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7661               .access = PL1_R, .type = ARM_CP_CONST,
7662               .accessfn = access_aa64_tid3,
7663               .resetvalue = 0 },
7664             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7665               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7666               .access = PL1_R, .type = ARM_CP_CONST,
7667               .accessfn = access_aa64_tid3,
7668               .resetvalue = 0 },
7669             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7670               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7671               .access = PL1_R, .type = ARM_CP_CONST,
7672               .accessfn = access_aa64_tid3,
7673               .resetvalue = 0 },
7674             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7675               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7676               .access = PL1_R, .type = ARM_CP_CONST,
7677               .accessfn = access_aa64_tid3,
7678               .resetvalue = 0 },
7679             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7680               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7681               .access = PL1_R, .type = ARM_CP_CONST,
7682               .accessfn = access_aa64_tid3,
7683               .resetvalue = 0 },
7684             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7685               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7686               .access = PL1_R, .type = ARM_CP_CONST,
7687               .accessfn = access_aa64_tid3,
7688               .resetvalue = cpu->isar.mvfr0 },
7689             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7690               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7691               .access = PL1_R, .type = ARM_CP_CONST,
7692               .accessfn = access_aa64_tid3,
7693               .resetvalue = cpu->isar.mvfr1 },
7694             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7695               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7696               .access = PL1_R, .type = ARM_CP_CONST,
7697               .accessfn = access_aa64_tid3,
7698               .resetvalue = cpu->isar.mvfr2 },
7699             { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7700               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7701               .access = PL1_R, .type = ARM_CP_CONST,
7702               .accessfn = access_aa64_tid3,
7703               .resetvalue = 0 },
7704             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7705               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7706               .access = PL1_R, .type = ARM_CP_CONST,
7707               .accessfn = access_aa64_tid3,
7708               .resetvalue = cpu->isar.id_pfr2 },
7709             { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7710               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7711               .access = PL1_R, .type = ARM_CP_CONST,
7712               .accessfn = access_aa64_tid3,
7713               .resetvalue = 0 },
7714             { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7715               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7716               .access = PL1_R, .type = ARM_CP_CONST,
7717               .accessfn = access_aa64_tid3,
7718               .resetvalue = 0 },
7719             { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7720               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7721               .access = PL1_R, .type = ARM_CP_CONST,
7722               .accessfn = access_aa64_tid3,
7723               .resetvalue = 0 },
7724             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7725               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7726               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7727               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7728             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7729               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7730               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7731               .resetvalue = cpu->pmceid0 },
7732             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7733               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7734               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7735               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7736             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7737               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7738               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7739               .resetvalue = cpu->pmceid1 },
7740             REGINFO_SENTINEL
7741         };
7742 #ifdef CONFIG_USER_ONLY
7743         ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7744             { .name = "ID_AA64PFR0_EL1",
7745               .exported_bits = 0x000f000f00ff0000,
7746               .fixed_bits    = 0x0000000000000011 },
7747             { .name = "ID_AA64PFR1_EL1",
7748               .exported_bits = 0x00000000000000f0 },
7749             { .name = "ID_AA64PFR*_EL1_RESERVED",
7750               .is_glob = true                     },
7751             { .name = "ID_AA64ZFR0_EL1"           },
7752             { .name = "ID_AA64MMFR0_EL1",
7753               .fixed_bits    = 0x00000000ff000000 },
7754             { .name = "ID_AA64MMFR1_EL1"          },
7755             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7756               .is_glob = true                     },
7757             { .name = "ID_AA64DFR0_EL1",
7758               .fixed_bits    = 0x0000000000000006 },
7759             { .name = "ID_AA64DFR1_EL1"           },
7760             { .name = "ID_AA64DFR*_EL1_RESERVED",
7761               .is_glob = true                     },
7762             { .name = "ID_AA64AFR*",
7763               .is_glob = true                     },
7764             { .name = "ID_AA64ISAR0_EL1",
7765               .exported_bits = 0x00fffffff0fffff0 },
7766             { .name = "ID_AA64ISAR1_EL1",
7767               .exported_bits = 0x000000f0ffffffff },
7768             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7769               .is_glob = true                     },
7770             REGUSERINFO_SENTINEL
7771         };
7772         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7773 #endif
7774         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7775         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7776             !arm_feature(env, ARM_FEATURE_EL2)) {
7777             ARMCPRegInfo rvbar = {
7778                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7779                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7780                 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7781             };
7782             define_one_arm_cp_reg(cpu, &rvbar);
7783         }
7784         define_arm_cp_regs(cpu, v8_idregs);
7785         define_arm_cp_regs(cpu, v8_cp_reginfo);
7786     }
7787     if (arm_feature(env, ARM_FEATURE_EL2)) {
7788         uint64_t vmpidr_def = mpidr_read_val(env);
7789         ARMCPRegInfo vpidr_regs[] = {
7790             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7791               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7792               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7793               .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7794               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7795             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7796               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7797               .access = PL2_RW, .resetvalue = cpu->midr,
7798               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7799             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7800               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7801               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7802               .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7803               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7804             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7805               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7806               .access = PL2_RW,
7807               .resetvalue = vmpidr_def,
7808               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7809             REGINFO_SENTINEL
7810         };
7811         define_arm_cp_regs(cpu, vpidr_regs);
7812         define_arm_cp_regs(cpu, el2_cp_reginfo);
7813         if (arm_feature(env, ARM_FEATURE_V8)) {
7814             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7815         }
7816         if (cpu_isar_feature(aa64_sel2, cpu)) {
7817             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7818         }
7819         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7820         if (!arm_feature(env, ARM_FEATURE_EL3)) {
7821             ARMCPRegInfo rvbar = {
7822                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7823                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7824                 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7825             };
7826             define_one_arm_cp_reg(cpu, &rvbar);
7827         }
7828     } else {
7829         /* If EL2 is missing but higher ELs are enabled, we need to
7830          * register the no_el2 reginfos.
7831          */
7832         if (arm_feature(env, ARM_FEATURE_EL3)) {
7833             /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7834              * of MIDR_EL1 and MPIDR_EL1.
7835              */
7836             ARMCPRegInfo vpidr_regs[] = {
7837                 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7838                   .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7839                   .access = PL2_RW, .accessfn = access_el3_aa32ns,
7840                   .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7841                   .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7842                 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7843                   .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7844                   .access = PL2_RW, .accessfn = access_el3_aa32ns,
7845                   .type = ARM_CP_NO_RAW,
7846                   .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
7847                 REGINFO_SENTINEL
7848             };
7849             define_arm_cp_regs(cpu, vpidr_regs);
7850             define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
7851             if (arm_feature(env, ARM_FEATURE_V8)) {
7852                 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7853             }
7854         }
7855     }
7856     if (arm_feature(env, ARM_FEATURE_EL3)) {
7857         define_arm_cp_regs(cpu, el3_cp_reginfo);
7858         ARMCPRegInfo el3_regs[] = {
7859             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7860               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7861               .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7862             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7863               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7864               .access = PL3_RW,
7865               .raw_writefn = raw_write, .writefn = sctlr_write,
7866               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7867               .resetvalue = cpu->reset_sctlr },
7868             REGINFO_SENTINEL
7869         };
7870
7871         define_arm_cp_regs(cpu, el3_regs);
7872     }
7873     /* The behaviour of NSACR is sufficiently various that we don't
7874      * try to describe it in a single reginfo:
7875      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
7876      *     reads as constant 0xc00 from NS EL1 and NS EL2
7877      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7878      *  if v7 without EL3, register doesn't exist
7879      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7880      */
7881     if (arm_feature(env, ARM_FEATURE_EL3)) {
7882         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7883             ARMCPRegInfo nsacr = {
7884                 .name = "NSACR", .type = ARM_CP_CONST,
7885                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7886                 .access = PL1_RW, .accessfn = nsacr_access,
7887                 .resetvalue = 0xc00
7888             };
7889             define_one_arm_cp_reg(cpu, &nsacr);
7890         } else {
7891             ARMCPRegInfo nsacr = {
7892                 .name = "NSACR",
7893                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7894                 .access = PL3_RW | PL1_R,
7895                 .resetvalue = 0,
7896                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7897             };
7898             define_one_arm_cp_reg(cpu, &nsacr);
7899         }
7900     } else {
7901         if (arm_feature(env, ARM_FEATURE_V8)) {
7902             ARMCPRegInfo nsacr = {
7903                 .name = "NSACR", .type = ARM_CP_CONST,
7904                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7905                 .access = PL1_R,
7906                 .resetvalue = 0xc00
7907             };
7908             define_one_arm_cp_reg(cpu, &nsacr);
7909         }
7910     }
7911
7912     if (arm_feature(env, ARM_FEATURE_PMSA)) {
7913         if (arm_feature(env, ARM_FEATURE_V6)) {
7914             /* PMSAv6 not implemented */
7915             assert(arm_feature(env, ARM_FEATURE_V7));
7916             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7917             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7918         } else {
7919             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7920         }
7921     } else {
7922         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7923         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
7924         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
7925         if (cpu_isar_feature(aa32_hpd, cpu)) {
7926             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7927         }
7928     }
7929     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7930         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7931     }
7932     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7933         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7934     }
7935     if (arm_feature(env, ARM_FEATURE_VAPA)) {
7936         define_arm_cp_regs(cpu, vapa_cp_reginfo);
7937     }
7938     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7939         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7940     }
7941     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7942         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7943     }
7944     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7945         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7946     }
7947     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7948         define_arm_cp_regs(cpu, omap_cp_reginfo);
7949     }
7950     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7951         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7952     }
7953     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7954         define_arm_cp_regs(cpu, xscale_cp_reginfo);
7955     }
7956     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7957         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7958     }
7959     if (arm_feature(env, ARM_FEATURE_LPAE)) {
7960         define_arm_cp_regs(cpu, lpae_cp_reginfo);
7961     }
7962     if (cpu_isar_feature(aa32_jazelle, cpu)) {
7963         define_arm_cp_regs(cpu, jazelle_regs);
7964     }
7965     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7966      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7967      * be read-only (ie write causes UNDEF exception).
7968      */
7969     {
7970         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7971             /* Pre-v8 MIDR space.
7972              * Note that the MIDR isn't a simple constant register because
7973              * of the TI925 behaviour where writes to another register can
7974              * cause the MIDR value to change.
7975              *
7976              * Unimplemented registers in the c15 0 0 0 space default to
7977              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7978              * and friends override accordingly.
7979              */
7980             { .name = "MIDR",
7981               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7982               .access = PL1_R, .resetvalue = cpu->midr,
7983               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
7984               .readfn = midr_read,
7985               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7986               .type = ARM_CP_OVERRIDE },
7987             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7988             { .name = "DUMMY",
7989               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7990               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7991             { .name = "DUMMY",
7992               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7993               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7994             { .name = "DUMMY",
7995               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7996               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7997             { .name = "DUMMY",
7998               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7999               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8000             { .name = "DUMMY",
8001               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8002               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8003             REGINFO_SENTINEL
8004         };
8005         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8006             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8007               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8008               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8009               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8010               .readfn = midr_read },
8011             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8012             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8013               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8014               .access = PL1_R, .resetvalue = cpu->midr },
8015             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8016               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8017               .access = PL1_R, .resetvalue = cpu->midr },
8018             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8019               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8020               .access = PL1_R,
8021               .accessfn = access_aa64_tid1,
8022               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8023             REGINFO_SENTINEL
8024         };
8025         ARMCPRegInfo id_cp_reginfo[] = {
8026             /* These are common to v8 and pre-v8 */
8027             { .name = "CTR",
8028               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8029               .access = PL1_R, .accessfn = ctr_el0_access,
8030               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8031             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8032               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8033               .access = PL0_R, .accessfn = ctr_el0_access,
8034               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8035             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8036             { .name = "TCMTR",
8037               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8038               .access = PL1_R,
8039               .accessfn = access_aa32_tid1,
8040               .type = ARM_CP_CONST, .resetvalue = 0 },
8041             REGINFO_SENTINEL
8042         };
8043         /* TLBTR is specific to VMSA */
8044         ARMCPRegInfo id_tlbtr_reginfo = {
8045               .name = "TLBTR",
8046               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8047               .access = PL1_R,
8048               .accessfn = access_aa32_tid1,
8049               .type = ARM_CP_CONST, .resetvalue = 0,
8050         };
8051         /* MPUIR is specific to PMSA V6+ */
8052         ARMCPRegInfo id_mpuir_reginfo = {
8053               .name = "MPUIR",
8054               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8055               .access = PL1_R, .type = ARM_CP_CONST,
8056               .resetvalue = cpu->pmsav7_dregion << 8
8057         };
8058         ARMCPRegInfo crn0_wi_reginfo = {
8059             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8060             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8061             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8062         };
8063 #ifdef CONFIG_USER_ONLY
8064         ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8065             { .name = "MIDR_EL1",
8066               .exported_bits = 0x00000000ffffffff },
8067             { .name = "REVIDR_EL1"                },
8068             REGUSERINFO_SENTINEL
8069         };
8070         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8071 #endif
8072         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8073             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8074             ARMCPRegInfo *r;
8075             /* Register the blanket "writes ignored" value first to cover the
8076              * whole space. Then update the specific ID registers to allow write
8077              * access, so that they ignore writes rather than causing them to
8078              * UNDEF.
8079              */
8080             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8081             for (r = id_pre_v8_midr_cp_reginfo;
8082                  r->type != ARM_CP_SENTINEL; r++) {
8083                 r->access = PL1_RW;
8084             }
8085             for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
8086                 r->access = PL1_RW;
8087             }
8088             id_mpuir_reginfo.access = PL1_RW;
8089             id_tlbtr_reginfo.access = PL1_RW;
8090         }
8091         if (arm_feature(env, ARM_FEATURE_V8)) {
8092             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8093         } else {
8094             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8095         }
8096         define_arm_cp_regs(cpu, id_cp_reginfo);
8097         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8098             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8099         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8100             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8101         }
8102     }
8103
8104     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8105         ARMCPRegInfo mpidr_cp_reginfo[] = {
8106             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8107               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8108               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8109             REGINFO_SENTINEL
8110         };
8111 #ifdef CONFIG_USER_ONLY
8112         ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8113             { .name = "MPIDR_EL1",
8114               .fixed_bits = 0x0000000080000000 },
8115             REGUSERINFO_SENTINEL
8116         };
8117         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8118 #endif
8119         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8120     }
8121
8122     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8123         ARMCPRegInfo auxcr_reginfo[] = {
8124             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8125               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8126               .access = PL1_RW, .accessfn = access_tacr,
8127               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8128             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8129               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8130               .access = PL2_RW, .type = ARM_CP_CONST,
8131               .resetvalue = 0 },
8132             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8133               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8134               .access = PL3_RW, .type = ARM_CP_CONST,
8135               .resetvalue = 0 },
8136             REGINFO_SENTINEL
8137         };
8138         define_arm_cp_regs(cpu, auxcr_reginfo);
8139         if (cpu_isar_feature(aa32_ac2, cpu)) {
8140             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8141         }
8142     }
8143
8144     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8145         /*
8146          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8147          * There are two flavours:
8148          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8149          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8150          *      32-bit register visible to AArch32 at a different encoding
8151          *      to the "flavour 1" register and with the bits rearranged to
8152          *      be able to squash a 64-bit address into the 32-bit view.
8153          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8154          * in future if we support AArch32-only configs of some of the
8155          * AArch64 cores we might need to add a specific feature flag
8156          * to indicate cores with "flavour 2" CBAR.
8157          */
8158         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8159             /* 32 bit view is [31:18] 0...0 [43:32]. */
8160             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8161                 | extract64(cpu->reset_cbar, 32, 12);
8162             ARMCPRegInfo cbar_reginfo[] = {
8163                 { .name = "CBAR",
8164                   .type = ARM_CP_CONST,
8165                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8166                   .access = PL1_R, .resetvalue = cbar32 },
8167                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8168                   .type = ARM_CP_CONST,
8169                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8170                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8171                 REGINFO_SENTINEL
8172             };
8173             /* We don't implement a r/w 64 bit CBAR currently */
8174             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8175             define_arm_cp_regs(cpu, cbar_reginfo);
8176         } else {
8177             ARMCPRegInfo cbar = {
8178                 .name = "CBAR",
8179                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8180                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8181                 .fieldoffset = offsetof(CPUARMState,
8182                                         cp15.c15_config_base_address)
8183             };
8184             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8185                 cbar.access = PL1_R;
8186                 cbar.fieldoffset = 0;
8187                 cbar.type = ARM_CP_CONST;
8188             }
8189             define_one_arm_cp_reg(cpu, &cbar);
8190         }
8191     }
8192
8193     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8194         ARMCPRegInfo vbar_cp_reginfo[] = {
8195             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8196               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8197               .access = PL1_RW, .writefn = vbar_write,
8198               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8199                                      offsetof(CPUARMState, cp15.vbar_ns) },
8200               .resetvalue = 0 },
8201             REGINFO_SENTINEL
8202         };
8203         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8204     }
8205
8206     /* Generic registers whose values depend on the implementation */
8207     {
8208         ARMCPRegInfo sctlr = {
8209             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8210             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8211             .access = PL1_RW, .accessfn = access_tvm_trvm,
8212             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8213                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8214             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8215             .raw_writefn = raw_write,
8216         };
8217         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8218             /* Normally we would always end the TB on an SCTLR write, but Linux
8219              * arch/arm/mach-pxa/sleep.S expects two instructions following
8220              * an MMU enable to execute from cache.  Imitate this behaviour.
8221              */
8222             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8223         }
8224         define_one_arm_cp_reg(cpu, &sctlr);
8225     }
8226
8227     if (cpu_isar_feature(aa64_lor, cpu)) {
8228         define_arm_cp_regs(cpu, lor_reginfo);
8229     }
8230     if (cpu_isar_feature(aa64_pan, cpu)) {
8231         define_one_arm_cp_reg(cpu, &pan_reginfo);
8232     }
8233 #ifndef CONFIG_USER_ONLY
8234     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8235         define_arm_cp_regs(cpu, ats1e1_reginfo);
8236     }
8237     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8238         define_arm_cp_regs(cpu, ats1cp_reginfo);
8239     }
8240 #endif
8241     if (cpu_isar_feature(aa64_uao, cpu)) {
8242         define_one_arm_cp_reg(cpu, &uao_reginfo);
8243     }
8244
8245     if (cpu_isar_feature(aa64_dit, cpu)) {
8246         define_one_arm_cp_reg(cpu, &dit_reginfo);
8247     }
8248
8249     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8250         define_arm_cp_regs(cpu, vhe_reginfo);
8251     }
8252
8253     if (cpu_isar_feature(aa64_sve, cpu)) {
8254         define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8255         if (arm_feature(env, ARM_FEATURE_EL2)) {
8256             define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8257         } else {
8258             define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8259         }
8260         if (arm_feature(env, ARM_FEATURE_EL3)) {
8261             define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8262         }
8263     }
8264
8265 #ifdef TARGET_AARCH64
8266     if (cpu_isar_feature(aa64_pauth, cpu)) {
8267         define_arm_cp_regs(cpu, pauth_reginfo);
8268     }
8269     if (cpu_isar_feature(aa64_rndr, cpu)) {
8270         define_arm_cp_regs(cpu, rndr_reginfo);
8271     }
8272 #ifndef CONFIG_USER_ONLY
8273     /* Data Cache clean instructions up to PoP */
8274     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8275         define_one_arm_cp_reg(cpu, dcpop_reg);
8276
8277         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8278             define_one_arm_cp_reg(cpu, dcpodp_reg);
8279         }
8280     }
8281 #endif /*CONFIG_USER_ONLY*/
8282
8283     /*
8284      * If full MTE is enabled, add all of the system registers.
8285      * If only "instructions available at EL0" are enabled,
8286      * then define only a RAZ/WI version of PSTATE.TCO.
8287      */
8288     if (cpu_isar_feature(aa64_mte, cpu)) {
8289         define_arm_cp_regs(cpu, mte_reginfo);
8290         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8291     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8292         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8293         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8294     }
8295 #endif
8296
8297     if (cpu_isar_feature(any_predinv, cpu)) {
8298         define_arm_cp_regs(cpu, predinv_reginfo);
8299     }
8300
8301     if (cpu_isar_feature(any_ccidx, cpu)) {
8302         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8303     }
8304
8305 #ifndef CONFIG_USER_ONLY
8306     /*
8307      * Register redirections and aliases must be done last,
8308      * after the registers from the other extensions have been defined.
8309      */
8310     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8311         define_arm_vh_e2h_redirects_aliases(cpu);
8312     }
8313 #endif
8314 }
8315
8316 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
8317 {
8318     CPUState *cs = CPU(cpu);
8319     CPUARMState *env = &cpu->env;
8320
8321     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8322         /*
8323          * The lower part of each SVE register aliases to the FPU
8324          * registers so we don't need to include both.
8325          */
8326 #ifdef TARGET_AARCH64
8327         if (isar_feature_aa64_sve(&cpu->isar)) {
8328             gdb_register_coprocessor(cs, arm_gdb_get_svereg, arm_gdb_set_svereg,
8329                                      arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs),
8330                                      "sve-registers.xml", 0);
8331         } else
8332 #endif
8333         {
8334             gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
8335                                      aarch64_fpu_gdb_set_reg,
8336                                      34, "aarch64-fpu.xml", 0);
8337         }
8338     } else if (arm_feature(env, ARM_FEATURE_NEON)) {
8339         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
8340                                  51, "arm-neon.xml", 0);
8341     } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
8342         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
8343                                  35, "arm-vfp3.xml", 0);
8344     } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
8345         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
8346                                  19, "arm-vfp.xml", 0);
8347     }
8348     gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
8349                              arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs),
8350                              "system-registers.xml", 0);
8351
8352 }
8353
8354 /* Sort alphabetically by type name, except for "any". */
8355 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8356 {
8357     ObjectClass *class_a = (ObjectClass *)a;
8358     ObjectClass *class_b = (ObjectClass *)b;
8359     const char *name_a, *name_b;
8360
8361     name_a = object_class_get_name(class_a);
8362     name_b = object_class_get_name(class_b);
8363     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8364         return 1;
8365     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8366         return -1;
8367     } else {
8368         return strcmp(name_a, name_b);
8369     }
8370 }
8371
8372 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8373 {
8374     ObjectClass *oc = data;
8375     const char *typename;
8376     char *name;
8377
8378     typename = object_class_get_name(oc);
8379     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8380     qemu_printf("  %s\n", name);
8381     g_free(name);
8382 }
8383
8384 void arm_cpu_list(void)
8385 {
8386     GSList *list;
8387
8388     list = object_class_get_list(TYPE_ARM_CPU, false);
8389     list = g_slist_sort(list, arm_cpu_list_compare);
8390     qemu_printf("Available CPUs:\n");
8391     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8392     g_slist_free(list);
8393 }
8394
8395 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8396 {
8397     ObjectClass *oc = data;
8398     CpuDefinitionInfoList **cpu_list = user_data;
8399     CpuDefinitionInfo *info;
8400     const char *typename;
8401
8402     typename = object_class_get_name(oc);
8403     info = g_malloc0(sizeof(*info));
8404     info->name = g_strndup(typename,
8405                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8406     info->q_typename = g_strdup(typename);
8407
8408     QAPI_LIST_PREPEND(*cpu_list, info);
8409 }
8410
8411 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8412 {
8413     CpuDefinitionInfoList *cpu_list = NULL;
8414     GSList *list;
8415
8416     list = object_class_get_list(TYPE_ARM_CPU, false);
8417     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8418     g_slist_free(list);
8419
8420     return cpu_list;
8421 }
8422
8423 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8424                                    void *opaque, int state, int secstate,
8425                                    int crm, int opc1, int opc2,
8426                                    const char *name)
8427 {
8428     /* Private utility function for define_one_arm_cp_reg_with_opaque():
8429      * add a single reginfo struct to the hash table.
8430      */
8431     uint32_t *key = g_new(uint32_t, 1);
8432     ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8433     int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
8434     int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8435
8436     r2->name = g_strdup(name);
8437     /* Reset the secure state to the specific incoming state.  This is
8438      * necessary as the register may have been defined with both states.
8439      */
8440     r2->secure = secstate;
8441
8442     if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8443         /* Register is banked (using both entries in array).
8444          * Overwriting fieldoffset as the array is only used to define
8445          * banked registers but later only fieldoffset is used.
8446          */
8447         r2->fieldoffset = r->bank_fieldoffsets[ns];
8448     }
8449
8450     if (state == ARM_CP_STATE_AA32) {
8451         if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8452             /* If the register is banked then we don't need to migrate or
8453              * reset the 32-bit instance in certain cases:
8454              *
8455              * 1) If the register has both 32-bit and 64-bit instances then we
8456              *    can count on the 64-bit instance taking care of the
8457              *    non-secure bank.
8458              * 2) If ARMv8 is enabled then we can count on a 64-bit version
8459              *    taking care of the secure bank.  This requires that separate
8460              *    32 and 64-bit definitions are provided.
8461              */
8462             if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8463                 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
8464                 r2->type |= ARM_CP_ALIAS;
8465             }
8466         } else if ((secstate != r->secure) && !ns) {
8467             /* The register is not banked so we only want to allow migration of
8468              * the non-secure instance.
8469              */
8470             r2->type |= ARM_CP_ALIAS;
8471         }
8472
8473         if (r->state == ARM_CP_STATE_BOTH) {
8474             /* We assume it is a cp15 register if the .cp field is left unset.
8475              */
8476             if (r2->cp == 0) {
8477                 r2->cp = 15;
8478             }
8479
8480 #ifdef HOST_WORDS_BIGENDIAN
8481             if (r2->fieldoffset) {
8482                 r2->fieldoffset += sizeof(uint32_t);
8483             }
8484 #endif
8485         }
8486     }
8487     if (state == ARM_CP_STATE_AA64) {
8488         /* To allow abbreviation of ARMCPRegInfo
8489          * definitions, we treat cp == 0 as equivalent to
8490          * the value for "standard guest-visible sysreg".
8491          * STATE_BOTH definitions are also always "standard
8492          * sysreg" in their AArch64 view (the .cp value may
8493          * be non-zero for the benefit of the AArch32 view).
8494          */
8495         if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8496             r2->cp = CP_REG_ARM64_SYSREG_CP;
8497         }
8498         *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8499                                   r2->opc0, opc1, opc2);
8500     } else {
8501         *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
8502     }
8503     if (opaque) {
8504         r2->opaque = opaque;
8505     }
8506     /* reginfo passed to helpers is correct for the actual access,
8507      * and is never ARM_CP_STATE_BOTH:
8508      */
8509     r2->state = state;
8510     /* Make sure reginfo passed to helpers for wildcarded regs
8511      * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8512      */
8513     r2->crm = crm;
8514     r2->opc1 = opc1;
8515     r2->opc2 = opc2;
8516     /* By convention, for wildcarded registers only the first
8517      * entry is used for migration; the others are marked as
8518      * ALIAS so we don't try to transfer the register
8519      * multiple times. Special registers (ie NOP/WFI) are
8520      * never migratable and not even raw-accessible.
8521      */
8522     if ((r->type & ARM_CP_SPECIAL)) {
8523         r2->type |= ARM_CP_NO_RAW;
8524     }
8525     if (((r->crm == CP_ANY) && crm != 0) ||
8526         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8527         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8528         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8529     }
8530
8531     /* Check that raw accesses are either forbidden or handled. Note that
8532      * we can't assert this earlier because the setup of fieldoffset for
8533      * banked registers has to be done first.
8534      */
8535     if (!(r2->type & ARM_CP_NO_RAW)) {
8536         assert(!raw_accessors_invalid(r2));
8537     }
8538
8539     /* Overriding of an existing definition must be explicitly
8540      * requested.
8541      */
8542     if (!(r->type & ARM_CP_OVERRIDE)) {
8543         ARMCPRegInfo *oldreg;
8544         oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8545         if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8546             fprintf(stderr, "Register redefined: cp=%d %d bit "
8547                     "crn=%d crm=%d opc1=%d opc2=%d, "
8548                     "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8549                     r2->crn, r2->crm, r2->opc1, r2->opc2,
8550                     oldreg->name, r2->name);
8551             g_assert_not_reached();
8552         }
8553     }
8554     g_hash_table_insert(cpu->cp_regs, key, r2);
8555 }
8556
8557
8558 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8559                                        const ARMCPRegInfo *r, void *opaque)
8560 {
8561     /* Define implementations of coprocessor registers.
8562      * We store these in a hashtable because typically
8563      * there are less than 150 registers in a space which
8564      * is 16*16*16*8*8 = 262144 in size.
8565      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8566      * If a register is defined twice then the second definition is
8567      * used, so this can be used to define some generic registers and
8568      * then override them with implementation specific variations.
8569      * At least one of the original and the second definition should
8570      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8571      * against accidental use.
8572      *
8573      * The state field defines whether the register is to be
8574      * visible in the AArch32 or AArch64 execution state. If the
8575      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8576      * reginfo structure for the AArch32 view, which sees the lower
8577      * 32 bits of the 64 bit register.
8578      *
8579      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8580      * be wildcarded. AArch64 registers are always considered to be 64
8581      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8582      * the register, if any.
8583      */
8584     int crm, opc1, opc2, state;
8585     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8586     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8587     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8588     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8589     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8590     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8591     /* 64 bit registers have only CRm and Opc1 fields */
8592     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8593     /* op0 only exists in the AArch64 encodings */
8594     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8595     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8596     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8597     /*
8598      * This API is only for Arm's system coprocessors (14 and 15) or
8599      * (M-profile or v7A-and-earlier only) for implementation defined
8600      * coprocessors in the range 0..7.  Our decode assumes this, since
8601      * 8..13 can be used for other insns including VFP and Neon. See
8602      * valid_cp() in translate.c.  Assert here that we haven't tried
8603      * to use an invalid coprocessor number.
8604      */
8605     switch (r->state) {
8606     case ARM_CP_STATE_BOTH:
8607         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8608         if (r->cp == 0) {
8609             break;
8610         }
8611         /* fall through */
8612     case ARM_CP_STATE_AA32:
8613         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8614             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8615             assert(r->cp >= 14 && r->cp <= 15);
8616         } else {
8617             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8618         }
8619         break;
8620     case ARM_CP_STATE_AA64:
8621         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8622         break;
8623     default:
8624         g_assert_not_reached();
8625     }
8626     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8627      * encodes a minimum access level for the register. We roll this
8628      * runtime check into our general permission check code, so check
8629      * here that the reginfo's specified permissions are strict enough
8630      * to encompass the generic architectural permission check.
8631      */
8632     if (r->state != ARM_CP_STATE_AA32) {
8633         int mask = 0;
8634         switch (r->opc1) {
8635         case 0:
8636             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8637             mask = PL0U_R | PL1_RW;
8638             break;
8639         case 1: case 2:
8640             /* min_EL EL1 */
8641             mask = PL1_RW;
8642             break;
8643         case 3:
8644             /* min_EL EL0 */
8645             mask = PL0_RW;
8646             break;
8647         case 4:
8648         case 5:
8649             /* min_EL EL2 */
8650             mask = PL2_RW;
8651             break;
8652         case 6:
8653             /* min_EL EL3 */
8654             mask = PL3_RW;
8655             break;
8656         case 7:
8657             /* min_EL EL1, secure mode only (we don't check the latter) */
8658             mask = PL1_RW;
8659             break;
8660         default:
8661             /* broken reginfo with out-of-range opc1 */
8662             assert(false);
8663             break;
8664         }
8665         /* assert our permissions are not too lax (stricter is fine) */
8666         assert((r->access & ~mask) == 0);
8667     }
8668
8669     /* Check that the register definition has enough info to handle
8670      * reads and writes if they are permitted.
8671      */
8672     if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8673         if (r->access & PL3_R) {
8674             assert((r->fieldoffset ||
8675                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8676                    r->readfn);
8677         }
8678         if (r->access & PL3_W) {
8679             assert((r->fieldoffset ||
8680                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8681                    r->writefn);
8682         }
8683     }
8684     /* Bad type field probably means missing sentinel at end of reg list */
8685     assert(cptype_valid(r->type));
8686     for (crm = crmmin; crm <= crmmax; crm++) {
8687         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8688             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8689                 for (state = ARM_CP_STATE_AA32;
8690                      state <= ARM_CP_STATE_AA64; state++) {
8691                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8692                         continue;
8693                     }
8694                     if (state == ARM_CP_STATE_AA32) {
8695                         /* Under AArch32 CP registers can be common
8696                          * (same for secure and non-secure world) or banked.
8697                          */
8698                         char *name;
8699
8700                         switch (r->secure) {
8701                         case ARM_CP_SECSTATE_S:
8702                         case ARM_CP_SECSTATE_NS:
8703                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8704                                                    r->secure, crm, opc1, opc2,
8705                                                    r->name);
8706                             break;
8707                         default:
8708                             name = g_strdup_printf("%s_S", r->name);
8709                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8710                                                    ARM_CP_SECSTATE_S,
8711                                                    crm, opc1, opc2, name);
8712                             g_free(name);
8713                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8714                                                    ARM_CP_SECSTATE_NS,
8715                                                    crm, opc1, opc2, r->name);
8716                             break;
8717                         }
8718                     } else {
8719                         /* AArch64 registers get mapped to non-secure instance
8720                          * of AArch32 */
8721                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8722                                                ARM_CP_SECSTATE_NS,
8723                                                crm, opc1, opc2, r->name);
8724                     }
8725                 }
8726             }
8727         }
8728     }
8729 }
8730
8731 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8732                                     const ARMCPRegInfo *regs, void *opaque)
8733 {
8734     /* Define a whole list of registers */
8735     const ARMCPRegInfo *r;
8736     for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8737         define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8738     }
8739 }
8740
8741 /*
8742  * Modify ARMCPRegInfo for access from userspace.
8743  *
8744  * This is a data driven modification directed by
8745  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8746  * user-space cannot alter any values and dynamic values pertaining to
8747  * execution state are hidden from user space view anyway.
8748  */
8749 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8750 {
8751     const ARMCPRegUserSpaceInfo *m;
8752     ARMCPRegInfo *r;
8753
8754     for (m = mods; m->name; m++) {
8755         GPatternSpec *pat = NULL;
8756         if (m->is_glob) {
8757             pat = g_pattern_spec_new(m->name);
8758         }
8759         for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8760             if (pat && g_pattern_match_string(pat, r->name)) {
8761                 r->type = ARM_CP_CONST;
8762                 r->access = PL0U_R;
8763                 r->resetvalue = 0;
8764                 /* continue */
8765             } else if (strcmp(r->name, m->name) == 0) {
8766                 r->type = ARM_CP_CONST;
8767                 r->access = PL0U_R;
8768                 r->resetvalue &= m->exported_bits;
8769                 r->resetvalue |= m->fixed_bits;
8770                 break;
8771             }
8772         }
8773         if (pat) {
8774             g_pattern_spec_free(pat);
8775         }
8776     }
8777 }
8778
8779 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8780 {
8781     return g_hash_table_lookup(cpregs, &encoded_cp);
8782 }
8783
8784 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8785                          uint64_t value)
8786 {
8787     /* Helper coprocessor write function for write-ignore registers */
8788 }
8789
8790 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8791 {
8792     /* Helper coprocessor write function for read-as-zero registers */
8793     return 0;
8794 }
8795
8796 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8797 {
8798     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8799 }
8800
8801 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8802 {
8803     /* Return true if it is not valid for us to switch to
8804      * this CPU mode (ie all the UNPREDICTABLE cases in
8805      * the ARM ARM CPSRWriteByInstr pseudocode).
8806      */
8807
8808     /* Changes to or from Hyp via MSR and CPS are illegal. */
8809     if (write_type == CPSRWriteByInstr &&
8810         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8811          mode == ARM_CPU_MODE_HYP)) {
8812         return 1;
8813     }
8814
8815     switch (mode) {
8816     case ARM_CPU_MODE_USR:
8817         return 0;
8818     case ARM_CPU_MODE_SYS:
8819     case ARM_CPU_MODE_SVC:
8820     case ARM_CPU_MODE_ABT:
8821     case ARM_CPU_MODE_UND:
8822     case ARM_CPU_MODE_IRQ:
8823     case ARM_CPU_MODE_FIQ:
8824         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8825          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8826          */
8827         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8828          * and CPS are treated as illegal mode changes.
8829          */
8830         if (write_type == CPSRWriteByInstr &&
8831             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8832             (arm_hcr_el2_eff(env) & HCR_TGE)) {
8833             return 1;
8834         }
8835         return 0;
8836     case ARM_CPU_MODE_HYP:
8837         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8838     case ARM_CPU_MODE_MON:
8839         return arm_current_el(env) < 3;
8840     default:
8841         return 1;
8842     }
8843 }
8844
8845 uint32_t cpsr_read(CPUARMState *env)
8846 {
8847     int ZF;
8848     ZF = (env->ZF == 0);
8849     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8850         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8851         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8852         | ((env->condexec_bits & 0xfc) << 8)
8853         | (env->GE << 16) | (env->daif & CPSR_AIF);
8854 }
8855
8856 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8857                 CPSRWriteType write_type)
8858 {
8859     uint32_t changed_daif;
8860
8861     if (mask & CPSR_NZCV) {
8862         env->ZF = (~val) & CPSR_Z;
8863         env->NF = val;
8864         env->CF = (val >> 29) & 1;
8865         env->VF = (val << 3) & 0x80000000;
8866     }
8867     if (mask & CPSR_Q)
8868         env->QF = ((val & CPSR_Q) != 0);
8869     if (mask & CPSR_T)
8870         env->thumb = ((val & CPSR_T) != 0);
8871     if (mask & CPSR_IT_0_1) {
8872         env->condexec_bits &= ~3;
8873         env->condexec_bits |= (val >> 25) & 3;
8874     }
8875     if (mask & CPSR_IT_2_7) {
8876         env->condexec_bits &= 3;
8877         env->condexec_bits |= (val >> 8) & 0xfc;
8878     }
8879     if (mask & CPSR_GE) {
8880         env->GE = (val >> 16) & 0xf;
8881     }
8882
8883     /* In a V7 implementation that includes the security extensions but does
8884      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8885      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8886      * bits respectively.
8887      *
8888      * In a V8 implementation, it is permitted for privileged software to
8889      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8890      */
8891     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
8892         arm_feature(env, ARM_FEATURE_EL3) &&
8893         !arm_feature(env, ARM_FEATURE_EL2) &&
8894         !arm_is_secure(env)) {
8895
8896         changed_daif = (env->daif ^ val) & mask;
8897
8898         if (changed_daif & CPSR_A) {
8899             /* Check to see if we are allowed to change the masking of async
8900              * abort exceptions from a non-secure state.
8901              */
8902             if (!(env->cp15.scr_el3 & SCR_AW)) {
8903                 qemu_log_mask(LOG_GUEST_ERROR,
8904                               "Ignoring attempt to switch CPSR_A flag from "
8905                               "non-secure world with SCR.AW bit clear\n");
8906                 mask &= ~CPSR_A;
8907             }
8908         }
8909
8910         if (changed_daif & CPSR_F) {
8911             /* Check to see if we are allowed to change the masking of FIQ
8912              * exceptions from a non-secure state.
8913              */
8914             if (!(env->cp15.scr_el3 & SCR_FW)) {
8915                 qemu_log_mask(LOG_GUEST_ERROR,
8916                               "Ignoring attempt to switch CPSR_F flag from "
8917                               "non-secure world with SCR.FW bit clear\n");
8918                 mask &= ~CPSR_F;
8919             }
8920
8921             /* Check whether non-maskable FIQ (NMFI) support is enabled.
8922              * If this bit is set software is not allowed to mask
8923              * FIQs, but is allowed to set CPSR_F to 0.
8924              */
8925             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8926                 (val & CPSR_F)) {
8927                 qemu_log_mask(LOG_GUEST_ERROR,
8928                               "Ignoring attempt to enable CPSR_F flag "
8929                               "(non-maskable FIQ [NMFI] support enabled)\n");
8930                 mask &= ~CPSR_F;
8931             }
8932         }
8933     }
8934
8935     env->daif &= ~(CPSR_AIF & mask);
8936     env->daif |= val & CPSR_AIF & mask;
8937
8938     if (write_type != CPSRWriteRaw &&
8939         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8940         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8941             /* Note that we can only get here in USR mode if this is a
8942              * gdb stub write; for this case we follow the architectural
8943              * behaviour for guest writes in USR mode of ignoring an attempt
8944              * to switch mode. (Those are caught by translate.c for writes
8945              * triggered by guest instructions.)
8946              */
8947             mask &= ~CPSR_M;
8948         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
8949             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8950              * v7, and has defined behaviour in v8:
8951              *  + leave CPSR.M untouched
8952              *  + allow changes to the other CPSR fields
8953              *  + set PSTATE.IL
8954              * For user changes via the GDB stub, we don't set PSTATE.IL,
8955              * as this would be unnecessarily harsh for a user error.
8956              */
8957             mask &= ~CPSR_M;
8958             if (write_type != CPSRWriteByGDBStub &&
8959                 arm_feature(env, ARM_FEATURE_V8)) {
8960                 mask |= CPSR_IL;
8961                 val |= CPSR_IL;
8962             }
8963             qemu_log_mask(LOG_GUEST_ERROR,
8964                           "Illegal AArch32 mode switch attempt from %s to %s\n",
8965                           aarch32_mode_name(env->uncached_cpsr),
8966                           aarch32_mode_name(val));
8967         } else {
8968             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8969                           write_type == CPSRWriteExceptionReturn ?
8970                           "Exception return from AArch32" :
8971                           "AArch32 mode switch from",
8972                           aarch32_mode_name(env->uncached_cpsr),
8973                           aarch32_mode_name(val), env->regs[15]);
8974             switch_mode(env, val & CPSR_M);
8975         }
8976     }
8977     mask &= ~CACHED_CPSR_BITS;
8978     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8979 }
8980
8981 /* Sign/zero extend */
8982 uint32_t HELPER(sxtb16)(uint32_t x)
8983 {
8984     uint32_t res;
8985     res = (uint16_t)(int8_t)x;
8986     res |= (uint32_t)(int8_t)(x >> 16) << 16;
8987     return res;
8988 }
8989
8990 uint32_t HELPER(uxtb16)(uint32_t x)
8991 {
8992     uint32_t res;
8993     res = (uint16_t)(uint8_t)x;
8994     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8995     return res;
8996 }
8997
8998 int32_t HELPER(sdiv)(int32_t num, int32_t den)
8999 {
9000     if (den == 0)
9001       return 0;
9002     if (num == INT_MIN && den == -1)
9003       return INT_MIN;
9004     return num / den;
9005 }
9006
9007 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
9008 {
9009     if (den == 0)
9010       return 0;
9011     return num / den;
9012 }
9013
9014 uint32_t HELPER(rbit)(uint32_t x)
9015 {
9016     return revbit32(x);
9017 }
9018
9019 #ifdef CONFIG_USER_ONLY
9020
9021 static void switch_mode(CPUARMState *env, int mode)
9022 {
9023     ARMCPU *cpu = env_archcpu(env);
9024
9025     if (mode != ARM_CPU_MODE_USR) {
9026         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9027     }
9028 }
9029
9030 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9031                                  uint32_t cur_el, bool secure)
9032 {
9033     return 1;
9034 }
9035
9036 void aarch64_sync_64_to_32(CPUARMState *env)
9037 {
9038     g_assert_not_reached();
9039 }
9040
9041 #else
9042
9043 static void switch_mode(CPUARMState *env, int mode)
9044 {
9045     int old_mode;
9046     int i;
9047
9048     old_mode = env->uncached_cpsr & CPSR_M;
9049     if (mode == old_mode)
9050         return;
9051
9052     if (old_mode == ARM_CPU_MODE_FIQ) {
9053         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9054         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9055     } else if (mode == ARM_CPU_MODE_FIQ) {
9056         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9057         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9058     }
9059
9060     i = bank_number(old_mode);
9061     env->banked_r13[i] = env->regs[13];
9062     env->banked_spsr[i] = env->spsr;
9063
9064     i = bank_number(mode);
9065     env->regs[13] = env->banked_r13[i];
9066     env->spsr = env->banked_spsr[i];
9067
9068     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9069     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9070 }
9071
9072 /* Physical Interrupt Target EL Lookup Table
9073  *
9074  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9075  *
9076  * The below multi-dimensional table is used for looking up the target
9077  * exception level given numerous condition criteria.  Specifically, the
9078  * target EL is based on SCR and HCR routing controls as well as the
9079  * currently executing EL and secure state.
9080  *
9081  *    Dimensions:
9082  *    target_el_table[2][2][2][2][2][4]
9083  *                    |  |  |  |  |  +--- Current EL
9084  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9085  *                    |  |  |  +--------- HCR mask override
9086  *                    |  |  +------------ SCR exec state control
9087  *                    |  +--------------- SCR mask override
9088  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9089  *
9090  *    The table values are as such:
9091  *    0-3 = EL0-EL3
9092  *     -1 = Cannot occur
9093  *
9094  * The ARM ARM target EL table includes entries indicating that an "exception
9095  * is not taken".  The two cases where this is applicable are:
9096  *    1) An exception is taken from EL3 but the SCR does not have the exception
9097  *    routed to EL3.
9098  *    2) An exception is taken from EL2 but the HCR does not have the exception
9099  *    routed to EL2.
9100  * In these two cases, the below table contain a target of EL1.  This value is
9101  * returned as it is expected that the consumer of the table data will check
9102  * for "target EL >= current EL" to ensure the exception is not taken.
9103  *
9104  *            SCR     HCR
9105  *         64  EA     AMO                 From
9106  *        BIT IRQ     IMO      Non-secure         Secure
9107  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9108  */
9109 static const int8_t target_el_table[2][2][2][2][2][4] = {
9110     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9111        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9112       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9113        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9114      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9115        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9116       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9117        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9118     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9119        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9120       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9121        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9122      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9123        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9124       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9125        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9126 };
9127
9128 /*
9129  * Determine the target EL for physical exceptions
9130  */
9131 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9132                                  uint32_t cur_el, bool secure)
9133 {
9134     CPUARMState *env = cs->env_ptr;
9135     bool rw;
9136     bool scr;
9137     bool hcr;
9138     int target_el;
9139     /* Is the highest EL AArch64? */
9140     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9141     uint64_t hcr_el2;
9142
9143     if (arm_feature(env, ARM_FEATURE_EL3)) {
9144         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9145     } else {
9146         /* Either EL2 is the highest EL (and so the EL2 register width
9147          * is given by is64); or there is no EL2 or EL3, in which case
9148          * the value of 'rw' does not affect the table lookup anyway.
9149          */
9150         rw = is64;
9151     }
9152
9153     hcr_el2 = arm_hcr_el2_eff(env);
9154     switch (excp_idx) {
9155     case EXCP_IRQ:
9156         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9157         hcr = hcr_el2 & HCR_IMO;
9158         break;
9159     case EXCP_FIQ:
9160         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9161         hcr = hcr_el2 & HCR_FMO;
9162         break;
9163     default:
9164         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9165         hcr = hcr_el2 & HCR_AMO;
9166         break;
9167     };
9168
9169     /*
9170      * For these purposes, TGE and AMO/IMO/FMO both force the
9171      * interrupt to EL2.  Fold TGE into the bit extracted above.
9172      */
9173     hcr |= (hcr_el2 & HCR_TGE) != 0;
9174
9175     /* Perform a table-lookup for the target EL given the current state */
9176     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9177
9178     assert(target_el > 0);
9179
9180     return target_el;
9181 }
9182
9183 void arm_log_exception(int idx)
9184 {
9185     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9186         const char *exc = NULL;
9187         static const char * const excnames[] = {
9188             [EXCP_UDEF] = "Undefined Instruction",
9189             [EXCP_SWI] = "SVC",
9190             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9191             [EXCP_DATA_ABORT] = "Data Abort",
9192             [EXCP_IRQ] = "IRQ",
9193             [EXCP_FIQ] = "FIQ",
9194             [EXCP_BKPT] = "Breakpoint",
9195             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9196             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9197             [EXCP_HVC] = "Hypervisor Call",
9198             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9199             [EXCP_SMC] = "Secure Monitor Call",
9200             [EXCP_VIRQ] = "Virtual IRQ",
9201             [EXCP_VFIQ] = "Virtual FIQ",
9202             [EXCP_SEMIHOST] = "Semihosting call",
9203             [EXCP_NOCP] = "v7M NOCP UsageFault",
9204             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9205             [EXCP_STKOF] = "v8M STKOF UsageFault",
9206             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9207             [EXCP_LSERR] = "v8M LSERR UsageFault",
9208             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9209         };
9210
9211         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9212             exc = excnames[idx];
9213         }
9214         if (!exc) {
9215             exc = "unknown";
9216         }
9217         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
9218     }
9219 }
9220
9221 /*
9222  * Function used to synchronize QEMU's AArch64 register set with AArch32
9223  * register set.  This is necessary when switching between AArch32 and AArch64
9224  * execution state.
9225  */
9226 void aarch64_sync_32_to_64(CPUARMState *env)
9227 {
9228     int i;
9229     uint32_t mode = env->uncached_cpsr & CPSR_M;
9230
9231     /* We can blanket copy R[0:7] to X[0:7] */
9232     for (i = 0; i < 8; i++) {
9233         env->xregs[i] = env->regs[i];
9234     }
9235
9236     /*
9237      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9238      * Otherwise, they come from the banked user regs.
9239      */
9240     if (mode == ARM_CPU_MODE_FIQ) {
9241         for (i = 8; i < 13; i++) {
9242             env->xregs[i] = env->usr_regs[i - 8];
9243         }
9244     } else {
9245         for (i = 8; i < 13; i++) {
9246             env->xregs[i] = env->regs[i];
9247         }
9248     }
9249
9250     /*
9251      * Registers x13-x23 are the various mode SP and FP registers. Registers
9252      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9253      * from the mode banked register.
9254      */
9255     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9256         env->xregs[13] = env->regs[13];
9257         env->xregs[14] = env->regs[14];
9258     } else {
9259         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9260         /* HYP is an exception in that it is copied from r14 */
9261         if (mode == ARM_CPU_MODE_HYP) {
9262             env->xregs[14] = env->regs[14];
9263         } else {
9264             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9265         }
9266     }
9267
9268     if (mode == ARM_CPU_MODE_HYP) {
9269         env->xregs[15] = env->regs[13];
9270     } else {
9271         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9272     }
9273
9274     if (mode == ARM_CPU_MODE_IRQ) {
9275         env->xregs[16] = env->regs[14];
9276         env->xregs[17] = env->regs[13];
9277     } else {
9278         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9279         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9280     }
9281
9282     if (mode == ARM_CPU_MODE_SVC) {
9283         env->xregs[18] = env->regs[14];
9284         env->xregs[19] = env->regs[13];
9285     } else {
9286         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9287         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9288     }
9289
9290     if (mode == ARM_CPU_MODE_ABT) {
9291         env->xregs[20] = env->regs[14];
9292         env->xregs[21] = env->regs[13];
9293     } else {
9294         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9295         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9296     }
9297
9298     if (mode == ARM_CPU_MODE_UND) {
9299         env->xregs[22] = env->regs[14];
9300         env->xregs[23] = env->regs[13];
9301     } else {
9302         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9303         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9304     }
9305
9306     /*
9307      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9308      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9309      * FIQ bank for r8-r14.
9310      */
9311     if (mode == ARM_CPU_MODE_FIQ) {
9312         for (i = 24; i < 31; i++) {
9313             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9314         }
9315     } else {
9316         for (i = 24; i < 29; i++) {
9317             env->xregs[i] = env->fiq_regs[i - 24];
9318         }
9319         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9320         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9321     }
9322
9323     env->pc = env->regs[15];
9324 }
9325
9326 /*
9327  * Function used to synchronize QEMU's AArch32 register set with AArch64
9328  * register set.  This is necessary when switching between AArch32 and AArch64
9329  * execution state.
9330  */
9331 void aarch64_sync_64_to_32(CPUARMState *env)
9332 {
9333     int i;
9334     uint32_t mode = env->uncached_cpsr & CPSR_M;
9335
9336     /* We can blanket copy X[0:7] to R[0:7] */
9337     for (i = 0; i < 8; i++) {
9338         env->regs[i] = env->xregs[i];
9339     }
9340
9341     /*
9342      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9343      * Otherwise, we copy x8-x12 into the banked user regs.
9344      */
9345     if (mode == ARM_CPU_MODE_FIQ) {
9346         for (i = 8; i < 13; i++) {
9347             env->usr_regs[i - 8] = env->xregs[i];
9348         }
9349     } else {
9350         for (i = 8; i < 13; i++) {
9351             env->regs[i] = env->xregs[i];
9352         }
9353     }
9354
9355     /*
9356      * Registers r13 & r14 depend on the current mode.
9357      * If we are in a given mode, we copy the corresponding x registers to r13
9358      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9359      * for the mode.
9360      */
9361     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9362         env->regs[13] = env->xregs[13];
9363         env->regs[14] = env->xregs[14];
9364     } else {
9365         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9366
9367         /*
9368          * HYP is an exception in that it does not have its own banked r14 but
9369          * shares the USR r14
9370          */
9371         if (mode == ARM_CPU_MODE_HYP) {
9372             env->regs[14] = env->xregs[14];
9373         } else {
9374             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9375         }
9376     }
9377
9378     if (mode == ARM_CPU_MODE_HYP) {
9379         env->regs[13] = env->xregs[15];
9380     } else {
9381         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9382     }
9383
9384     if (mode == ARM_CPU_MODE_IRQ) {
9385         env->regs[14] = env->xregs[16];
9386         env->regs[13] = env->xregs[17];
9387     } else {
9388         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9389         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9390     }
9391
9392     if (mode == ARM_CPU_MODE_SVC) {
9393         env->regs[14] = env->xregs[18];
9394         env->regs[13] = env->xregs[19];
9395     } else {
9396         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9397         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9398     }
9399
9400     if (mode == ARM_CPU_MODE_ABT) {
9401         env->regs[14] = env->xregs[20];
9402         env->regs[13] = env->xregs[21];
9403     } else {
9404         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9405         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9406     }
9407
9408     if (mode == ARM_CPU_MODE_UND) {
9409         env->regs[14] = env->xregs[22];
9410         env->regs[13] = env->xregs[23];
9411     } else {
9412         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9413         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9414     }
9415
9416     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9417      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9418      * FIQ bank for r8-r14.
9419      */
9420     if (mode == ARM_CPU_MODE_FIQ) {
9421         for (i = 24; i < 31; i++) {
9422             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9423         }
9424     } else {
9425         for (i = 24; i < 29; i++) {
9426             env->fiq_regs[i - 24] = env->xregs[i];
9427         }
9428         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9429         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9430     }
9431
9432     env->regs[15] = env->pc;
9433 }
9434
9435 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9436                                    uint32_t mask, uint32_t offset,
9437                                    uint32_t newpc)
9438 {
9439     int new_el;
9440
9441     /* Change the CPU state so as to actually take the exception. */
9442     switch_mode(env, new_mode);
9443
9444     /*
9445      * For exceptions taken to AArch32 we must clear the SS bit in both
9446      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9447      */
9448     env->pstate &= ~PSTATE_SS;
9449     env->spsr = cpsr_read(env);
9450     /* Clear IT bits.  */
9451     env->condexec_bits = 0;
9452     /* Switch to the new mode, and to the correct instruction set.  */
9453     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9454
9455     /* This must be after mode switching. */
9456     new_el = arm_current_el(env);
9457
9458     /* Set new mode endianness */
9459     env->uncached_cpsr &= ~CPSR_E;
9460     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9461         env->uncached_cpsr |= CPSR_E;
9462     }
9463     /* J and IL must always be cleared for exception entry */
9464     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9465     env->daif |= mask;
9466
9467     if (new_mode == ARM_CPU_MODE_HYP) {
9468         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9469         env->elr_el[2] = env->regs[15];
9470     } else {
9471         /* CPSR.PAN is normally preserved preserved unless...  */
9472         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9473             switch (new_el) {
9474             case 3:
9475                 if (!arm_is_secure_below_el3(env)) {
9476                     /* ... the target is EL3, from non-secure state.  */
9477                     env->uncached_cpsr &= ~CPSR_PAN;
9478                     break;
9479                 }
9480                 /* ... the target is EL3, from secure state ... */
9481                 /* fall through */
9482             case 1:
9483                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9484                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9485                     env->uncached_cpsr |= CPSR_PAN;
9486                 }
9487                 break;
9488             }
9489         }
9490         /*
9491          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9492          * and we should just guard the thumb mode on V4
9493          */
9494         if (arm_feature(env, ARM_FEATURE_V4T)) {
9495             env->thumb =
9496                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9497         }
9498         env->regs[14] = env->regs[15] + offset;
9499     }
9500     env->regs[15] = newpc;
9501     arm_rebuild_hflags(env);
9502 }
9503
9504 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9505 {
9506     /*
9507      * Handle exception entry to Hyp mode; this is sufficiently
9508      * different to entry to other AArch32 modes that we handle it
9509      * separately here.
9510      *
9511      * The vector table entry used is always the 0x14 Hyp mode entry point,
9512      * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9513      * The offset applied to the preferred return address is always zero
9514      * (see DDI0487C.a section G1.12.3).
9515      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9516      */
9517     uint32_t addr, mask;
9518     ARMCPU *cpu = ARM_CPU(cs);
9519     CPUARMState *env = &cpu->env;
9520
9521     switch (cs->exception_index) {
9522     case EXCP_UDEF:
9523         addr = 0x04;
9524         break;
9525     case EXCP_SWI:
9526         addr = 0x14;
9527         break;
9528     case EXCP_BKPT:
9529         /* Fall through to prefetch abort.  */
9530     case EXCP_PREFETCH_ABORT:
9531         env->cp15.ifar_s = env->exception.vaddress;
9532         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9533                       (uint32_t)env->exception.vaddress);
9534         addr = 0x0c;
9535         break;
9536     case EXCP_DATA_ABORT:
9537         env->cp15.dfar_s = env->exception.vaddress;
9538         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9539                       (uint32_t)env->exception.vaddress);
9540         addr = 0x10;
9541         break;
9542     case EXCP_IRQ:
9543         addr = 0x18;
9544         break;
9545     case EXCP_FIQ:
9546         addr = 0x1c;
9547         break;
9548     case EXCP_HVC:
9549         addr = 0x08;
9550         break;
9551     case EXCP_HYP_TRAP:
9552         addr = 0x14;
9553         break;
9554     default:
9555         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9556     }
9557
9558     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9559         if (!arm_feature(env, ARM_FEATURE_V8)) {
9560             /*
9561              * QEMU syndrome values are v8-style. v7 has the IL bit
9562              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9563              * If this is a v7 CPU, squash the IL bit in those cases.
9564              */
9565             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9566                 (cs->exception_index == EXCP_DATA_ABORT &&
9567                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9568                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9569                 env->exception.syndrome &= ~ARM_EL_IL;
9570             }
9571         }
9572         env->cp15.esr_el[2] = env->exception.syndrome;
9573     }
9574
9575     if (arm_current_el(env) != 2 && addr < 0x14) {
9576         addr = 0x14;
9577     }
9578
9579     mask = 0;
9580     if (!(env->cp15.scr_el3 & SCR_EA)) {
9581         mask |= CPSR_A;
9582     }
9583     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9584         mask |= CPSR_I;
9585     }
9586     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9587         mask |= CPSR_F;
9588     }
9589
9590     addr += env->cp15.hvbar;
9591
9592     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9593 }
9594
9595 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9596 {
9597     ARMCPU *cpu = ARM_CPU(cs);
9598     CPUARMState *env = &cpu->env;
9599     uint32_t addr;
9600     uint32_t mask;
9601     int new_mode;
9602     uint32_t offset;
9603     uint32_t moe;
9604
9605     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9606     switch (syn_get_ec(env->exception.syndrome)) {
9607     case EC_BREAKPOINT:
9608     case EC_BREAKPOINT_SAME_EL:
9609         moe = 1;
9610         break;
9611     case EC_WATCHPOINT:
9612     case EC_WATCHPOINT_SAME_EL:
9613         moe = 10;
9614         break;
9615     case EC_AA32_BKPT:
9616         moe = 3;
9617         break;
9618     case EC_VECTORCATCH:
9619         moe = 5;
9620         break;
9621     default:
9622         moe = 0;
9623         break;
9624     }
9625
9626     if (moe) {
9627         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9628     }
9629
9630     if (env->exception.target_el == 2) {
9631         arm_cpu_do_interrupt_aarch32_hyp(cs);
9632         return;
9633     }
9634
9635     switch (cs->exception_index) {
9636     case EXCP_UDEF:
9637         new_mode = ARM_CPU_MODE_UND;
9638         addr = 0x04;
9639         mask = CPSR_I;
9640         if (env->thumb)
9641             offset = 2;
9642         else
9643             offset = 4;
9644         break;
9645     case EXCP_SWI:
9646         new_mode = ARM_CPU_MODE_SVC;
9647         addr = 0x08;
9648         mask = CPSR_I;
9649         /* The PC already points to the next instruction.  */
9650         offset = 0;
9651         break;
9652     case EXCP_BKPT:
9653         /* Fall through to prefetch abort.  */
9654     case EXCP_PREFETCH_ABORT:
9655         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9656         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9657         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9658                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9659         new_mode = ARM_CPU_MODE_ABT;
9660         addr = 0x0c;
9661         mask = CPSR_A | CPSR_I;
9662         offset = 4;
9663         break;
9664     case EXCP_DATA_ABORT:
9665         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9666         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9667         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9668                       env->exception.fsr,
9669                       (uint32_t)env->exception.vaddress);
9670         new_mode = ARM_CPU_MODE_ABT;
9671         addr = 0x10;
9672         mask = CPSR_A | CPSR_I;
9673         offset = 8;
9674         break;
9675     case EXCP_IRQ:
9676         new_mode = ARM_CPU_MODE_IRQ;
9677         addr = 0x18;
9678         /* Disable IRQ and imprecise data aborts.  */
9679         mask = CPSR_A | CPSR_I;
9680         offset = 4;
9681         if (env->cp15.scr_el3 & SCR_IRQ) {
9682             /* IRQ routed to monitor mode */
9683             new_mode = ARM_CPU_MODE_MON;
9684             mask |= CPSR_F;
9685         }
9686         break;
9687     case EXCP_FIQ:
9688         new_mode = ARM_CPU_MODE_FIQ;
9689         addr = 0x1c;
9690         /* Disable FIQ, IRQ and imprecise data aborts.  */
9691         mask = CPSR_A | CPSR_I | CPSR_F;
9692         if (env->cp15.scr_el3 & SCR_FIQ) {
9693             /* FIQ routed to monitor mode */
9694             new_mode = ARM_CPU_MODE_MON;
9695         }
9696         offset = 4;
9697         break;
9698     case EXCP_VIRQ:
9699         new_mode = ARM_CPU_MODE_IRQ;
9700         addr = 0x18;
9701         /* Disable IRQ and imprecise data aborts.  */
9702         mask = CPSR_A | CPSR_I;
9703         offset = 4;
9704         break;
9705     case EXCP_VFIQ:
9706         new_mode = ARM_CPU_MODE_FIQ;
9707         addr = 0x1c;
9708         /* Disable FIQ, IRQ and imprecise data aborts.  */
9709         mask = CPSR_A | CPSR_I | CPSR_F;
9710         offset = 4;
9711         break;
9712     case EXCP_SMC:
9713         new_mode = ARM_CPU_MODE_MON;
9714         addr = 0x08;
9715         mask = CPSR_A | CPSR_I | CPSR_F;
9716         offset = 0;
9717         break;
9718     default:
9719         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9720         return; /* Never happens.  Keep compiler happy.  */
9721     }
9722
9723     if (new_mode == ARM_CPU_MODE_MON) {
9724         addr += env->cp15.mvbar;
9725     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9726         /* High vectors. When enabled, base address cannot be remapped. */
9727         addr += 0xffff0000;
9728     } else {
9729         /* ARM v7 architectures provide a vector base address register to remap
9730          * the interrupt vector table.
9731          * This register is only followed in non-monitor mode, and is banked.
9732          * Note: only bits 31:5 are valid.
9733          */
9734         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9735     }
9736
9737     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9738         env->cp15.scr_el3 &= ~SCR_NS;
9739     }
9740
9741     take_aarch32_exception(env, new_mode, mask, offset, addr);
9742 }
9743
9744 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9745 {
9746     /*
9747      * Return the register number of the AArch64 view of the AArch32
9748      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9749      * be that of the AArch32 mode the exception came from.
9750      */
9751     int mode = env->uncached_cpsr & CPSR_M;
9752
9753     switch (aarch32_reg) {
9754     case 0 ... 7:
9755         return aarch32_reg;
9756     case 8 ... 12:
9757         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9758     case 13:
9759         switch (mode) {
9760         case ARM_CPU_MODE_USR:
9761         case ARM_CPU_MODE_SYS:
9762             return 13;
9763         case ARM_CPU_MODE_HYP:
9764             return 15;
9765         case ARM_CPU_MODE_IRQ:
9766             return 17;
9767         case ARM_CPU_MODE_SVC:
9768             return 19;
9769         case ARM_CPU_MODE_ABT:
9770             return 21;
9771         case ARM_CPU_MODE_UND:
9772             return 23;
9773         case ARM_CPU_MODE_FIQ:
9774             return 29;
9775         default:
9776             g_assert_not_reached();
9777         }
9778     case 14:
9779         switch (mode) {
9780         case ARM_CPU_MODE_USR:
9781         case ARM_CPU_MODE_SYS:
9782         case ARM_CPU_MODE_HYP:
9783             return 14;
9784         case ARM_CPU_MODE_IRQ:
9785             return 16;
9786         case ARM_CPU_MODE_SVC:
9787             return 18;
9788         case ARM_CPU_MODE_ABT:
9789             return 20;
9790         case ARM_CPU_MODE_UND:
9791             return 22;
9792         case ARM_CPU_MODE_FIQ:
9793             return 30;
9794         default:
9795             g_assert_not_reached();
9796         }
9797     case 15:
9798         return 31;
9799     default:
9800         g_assert_not_reached();
9801     }
9802 }
9803
9804 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9805 {
9806     uint32_t ret = cpsr_read(env);
9807
9808     /* Move DIT to the correct location for SPSR_ELx */
9809     if (ret & CPSR_DIT) {
9810         ret &= ~CPSR_DIT;
9811         ret |= PSTATE_DIT;
9812     }
9813     /* Merge PSTATE.SS into SPSR_ELx */
9814     ret |= env->pstate & PSTATE_SS;
9815
9816     return ret;
9817 }
9818
9819 /* Handle exception entry to a target EL which is using AArch64 */
9820 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9821 {
9822     ARMCPU *cpu = ARM_CPU(cs);
9823     CPUARMState *env = &cpu->env;
9824     unsigned int new_el = env->exception.target_el;
9825     target_ulong addr = env->cp15.vbar_el[new_el];
9826     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9827     unsigned int old_mode;
9828     unsigned int cur_el = arm_current_el(env);
9829     int rt;
9830
9831     /*
9832      * Note that new_el can never be 0.  If cur_el is 0, then
9833      * el0_a64 is is_a64(), else el0_a64 is ignored.
9834      */
9835     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9836
9837     if (cur_el < new_el) {
9838         /* Entry vector offset depends on whether the implemented EL
9839          * immediately lower than the target level is using AArch32 or AArch64
9840          */
9841         bool is_aa64;
9842         uint64_t hcr;
9843
9844         switch (new_el) {
9845         case 3:
9846             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9847             break;
9848         case 2:
9849             hcr = arm_hcr_el2_eff(env);
9850             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9851                 is_aa64 = (hcr & HCR_RW) != 0;
9852                 break;
9853             }
9854             /* fall through */
9855         case 1:
9856             is_aa64 = is_a64(env);
9857             break;
9858         default:
9859             g_assert_not_reached();
9860         }
9861
9862         if (is_aa64) {
9863             addr += 0x400;
9864         } else {
9865             addr += 0x600;
9866         }
9867     } else if (pstate_read(env) & PSTATE_SP) {
9868         addr += 0x200;
9869     }
9870
9871     switch (cs->exception_index) {
9872     case EXCP_PREFETCH_ABORT:
9873     case EXCP_DATA_ABORT:
9874         env->cp15.far_el[new_el] = env->exception.vaddress;
9875         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9876                       env->cp15.far_el[new_el]);
9877         /* fall through */
9878     case EXCP_BKPT:
9879     case EXCP_UDEF:
9880     case EXCP_SWI:
9881     case EXCP_HVC:
9882     case EXCP_HYP_TRAP:
9883     case EXCP_SMC:
9884         switch (syn_get_ec(env->exception.syndrome)) {
9885         case EC_ADVSIMDFPACCESSTRAP:
9886             /*
9887              * QEMU internal FP/SIMD syndromes from AArch32 include the
9888              * TA and coproc fields which are only exposed if the exception
9889              * is taken to AArch32 Hyp mode. Mask them out to get a valid
9890              * AArch64 format syndrome.
9891              */
9892             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9893             break;
9894         case EC_CP14RTTRAP:
9895         case EC_CP15RTTRAP:
9896         case EC_CP14DTTRAP:
9897             /*
9898              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
9899              * the raw register field from the insn; when taking this to
9900              * AArch64 we must convert it to the AArch64 view of the register
9901              * number. Notice that we read a 4-bit AArch32 register number and
9902              * write back a 5-bit AArch64 one.
9903              */
9904             rt = extract32(env->exception.syndrome, 5, 4);
9905             rt = aarch64_regnum(env, rt);
9906             env->exception.syndrome = deposit32(env->exception.syndrome,
9907                                                 5, 5, rt);
9908             break;
9909         case EC_CP15RRTTRAP:
9910         case EC_CP14RRTTRAP:
9911             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
9912             rt = extract32(env->exception.syndrome, 5, 4);
9913             rt = aarch64_regnum(env, rt);
9914             env->exception.syndrome = deposit32(env->exception.syndrome,
9915                                                 5, 5, rt);
9916             rt = extract32(env->exception.syndrome, 10, 4);
9917             rt = aarch64_regnum(env, rt);
9918             env->exception.syndrome = deposit32(env->exception.syndrome,
9919                                                 10, 5, rt);
9920             break;
9921         }
9922         env->cp15.esr_el[new_el] = env->exception.syndrome;
9923         break;
9924     case EXCP_IRQ:
9925     case EXCP_VIRQ:
9926         addr += 0x80;
9927         break;
9928     case EXCP_FIQ:
9929     case EXCP_VFIQ:
9930         addr += 0x100;
9931         break;
9932     default:
9933         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9934     }
9935
9936     if (is_a64(env)) {
9937         old_mode = pstate_read(env);
9938         aarch64_save_sp(env, arm_current_el(env));
9939         env->elr_el[new_el] = env->pc;
9940     } else {
9941         old_mode = cpsr_read_for_spsr_elx(env);
9942         env->elr_el[new_el] = env->regs[15];
9943
9944         aarch64_sync_32_to_64(env);
9945
9946         env->condexec_bits = 0;
9947     }
9948     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9949
9950     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9951                   env->elr_el[new_el]);
9952
9953     if (cpu_isar_feature(aa64_pan, cpu)) {
9954         /* The value of PSTATE.PAN is normally preserved, except when ... */
9955         new_mode |= old_mode & PSTATE_PAN;
9956         switch (new_el) {
9957         case 2:
9958             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
9959             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9960                 != (HCR_E2H | HCR_TGE)) {
9961                 break;
9962             }
9963             /* fall through */
9964         case 1:
9965             /* ... the target is EL1 ... */
9966             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
9967             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9968                 new_mode |= PSTATE_PAN;
9969             }
9970             break;
9971         }
9972     }
9973     if (cpu_isar_feature(aa64_mte, cpu)) {
9974         new_mode |= PSTATE_TCO;
9975     }
9976
9977     pstate_write(env, PSTATE_DAIF | new_mode);
9978     env->aarch64 = 1;
9979     aarch64_restore_sp(env, new_el);
9980     helper_rebuild_hflags_a64(env, new_el);
9981
9982     env->pc = addr;
9983
9984     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9985                   new_el, env->pc, pstate_read(env));
9986 }
9987
9988 /*
9989  * Do semihosting call and set the appropriate return value. All the
9990  * permission and validity checks have been done at translate time.
9991  *
9992  * We only see semihosting exceptions in TCG only as they are not
9993  * trapped to the hypervisor in KVM.
9994  */
9995 #ifdef CONFIG_TCG
9996 static void handle_semihosting(CPUState *cs)
9997 {
9998     ARMCPU *cpu = ARM_CPU(cs);
9999     CPUARMState *env = &cpu->env;
10000
10001     if (is_a64(env)) {
10002         qemu_log_mask(CPU_LOG_INT,
10003                       "...handling as semihosting call 0x%" PRIx64 "\n",
10004                       env->xregs[0]);
10005         env->xregs[0] = do_common_semihosting(cs);
10006         env->pc += 4;
10007     } else {
10008         qemu_log_mask(CPU_LOG_INT,
10009                       "...handling as semihosting call 0x%x\n",
10010                       env->regs[0]);
10011         env->regs[0] = do_common_semihosting(cs);
10012         env->regs[15] += env->thumb ? 2 : 4;
10013     }
10014 }
10015 #endif
10016
10017 /* Handle a CPU exception for A and R profile CPUs.
10018  * Do any appropriate logging, handle PSCI calls, and then hand off
10019  * to the AArch64-entry or AArch32-entry function depending on the
10020  * target exception level's register width.
10021  *
10022  * Note: this is used for both TCG (as the do_interrupt tcg op),
10023  *       and KVM to re-inject guest debug exceptions, and to
10024  *       inject a Synchronous-External-Abort.
10025  */
10026 void arm_cpu_do_interrupt(CPUState *cs)
10027 {
10028     ARMCPU *cpu = ARM_CPU(cs);
10029     CPUARMState *env = &cpu->env;
10030     unsigned int new_el = env->exception.target_el;
10031
10032     assert(!arm_feature(env, ARM_FEATURE_M));
10033
10034     arm_log_exception(cs->exception_index);
10035     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10036                   new_el);
10037     if (qemu_loglevel_mask(CPU_LOG_INT)
10038         && !excp_is_internal(cs->exception_index)) {
10039         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10040                       syn_get_ec(env->exception.syndrome),
10041                       env->exception.syndrome);
10042     }
10043
10044     if (arm_is_psci_call(cpu, cs->exception_index)) {
10045         arm_handle_psci_call(cpu);
10046         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10047         return;
10048     }
10049
10050     /*
10051      * Semihosting semantics depend on the register width of the code
10052      * that caused the exception, not the target exception level, so
10053      * must be handled here.
10054      */
10055 #ifdef CONFIG_TCG
10056     if (cs->exception_index == EXCP_SEMIHOST) {
10057         handle_semihosting(cs);
10058         return;
10059     }
10060 #endif
10061
10062     /* Hooks may change global state so BQL should be held, also the
10063      * BQL needs to be held for any modification of
10064      * cs->interrupt_request.
10065      */
10066     g_assert(qemu_mutex_iothread_locked());
10067
10068     arm_call_pre_el_change_hook(cpu);
10069
10070     assert(!excp_is_internal(cs->exception_index));
10071     if (arm_el_is_aa64(env, new_el)) {
10072         arm_cpu_do_interrupt_aarch64(cs);
10073     } else {
10074         arm_cpu_do_interrupt_aarch32(cs);
10075     }
10076
10077     arm_call_el_change_hook(cpu);
10078
10079     if (!kvm_enabled()) {
10080         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10081     }
10082 }
10083 #endif /* !CONFIG_USER_ONLY */
10084
10085 uint64_t arm_sctlr(CPUARMState *env, int el)
10086 {
10087     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10088     if (el == 0) {
10089         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10090         el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10091              ? 2 : 1;
10092     }
10093     return env->cp15.sctlr_el[el];
10094 }
10095
10096 /* Return the SCTLR value which controls this address translation regime */
10097 static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
10098 {
10099     return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10100 }
10101
10102 #ifndef CONFIG_USER_ONLY
10103
10104 /* Return true if the specified stage of address translation is disabled */
10105 static inline bool regime_translation_disabled(CPUARMState *env,
10106                                                ARMMMUIdx mmu_idx)
10107 {
10108     uint64_t hcr_el2;
10109
10110     if (arm_feature(env, ARM_FEATURE_M)) {
10111         switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
10112                 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10113         case R_V7M_MPU_CTRL_ENABLE_MASK:
10114             /* Enabled, but not for HardFault and NMI */
10115             return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
10116         case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10117             /* Enabled for all cases */
10118             return false;
10119         case 0:
10120         default:
10121             /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10122              * we warned about that in armv7m_nvic.c when the guest set it.
10123              */
10124             return true;
10125         }
10126     }
10127
10128     hcr_el2 = arm_hcr_el2_eff(env);
10129
10130     if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10131         /* HCR.DC means HCR.VM behaves as 1 */
10132         return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
10133     }
10134
10135     if (hcr_el2 & HCR_TGE) {
10136         /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10137         if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10138             return true;
10139         }
10140     }
10141
10142     if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
10143         /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10144         return true;
10145     }
10146
10147     return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10148 }
10149
10150 static inline bool regime_translation_big_endian(CPUARMState *env,
10151                                                  ARMMMUIdx mmu_idx)
10152 {
10153     return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10154 }
10155
10156 /* Return the TTBR associated with this translation regime */
10157 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10158                                    int ttbrn)
10159 {
10160     if (mmu_idx == ARMMMUIdx_Stage2) {
10161         return env->cp15.vttbr_el2;
10162     }
10163     if (mmu_idx == ARMMMUIdx_Stage2_S) {
10164         return env->cp15.vsttbr_el2;
10165     }
10166     if (ttbrn == 0) {
10167         return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10168     } else {
10169         return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10170     }
10171 }
10172
10173 #endif /* !CONFIG_USER_ONLY */
10174
10175 /* Convert a possible stage1+2 MMU index into the appropriate
10176  * stage 1 MMU index
10177  */
10178 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10179 {
10180     switch (mmu_idx) {
10181     case ARMMMUIdx_SE10_0:
10182         return ARMMMUIdx_Stage1_SE0;
10183     case ARMMMUIdx_SE10_1:
10184         return ARMMMUIdx_Stage1_SE1;
10185     case ARMMMUIdx_SE10_1_PAN:
10186         return ARMMMUIdx_Stage1_SE1_PAN;
10187     case ARMMMUIdx_E10_0:
10188         return ARMMMUIdx_Stage1_E0;
10189     case ARMMMUIdx_E10_1:
10190         return ARMMMUIdx_Stage1_E1;
10191     case ARMMMUIdx_E10_1_PAN:
10192         return ARMMMUIdx_Stage1_E1_PAN;
10193     default:
10194         return mmu_idx;
10195     }
10196 }
10197
10198 /* Return true if the translation regime is using LPAE format page tables */
10199 static inline bool regime_using_lpae_format(CPUARMState *env,
10200                                             ARMMMUIdx mmu_idx)
10201 {
10202     int el = regime_el(env, mmu_idx);
10203     if (el == 2 || arm_el_is_aa64(env, el)) {
10204         return true;
10205     }
10206     if (arm_feature(env, ARM_FEATURE_LPAE)
10207         && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10208         return true;
10209     }
10210     return false;
10211 }
10212
10213 /* Returns true if the stage 1 translation regime is using LPAE format page
10214  * tables. Used when raising alignment exceptions, whose FSR changes depending
10215  * on whether the long or short descriptor format is in use. */
10216 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
10217 {
10218     mmu_idx = stage_1_mmu_idx(mmu_idx);
10219
10220     return regime_using_lpae_format(env, mmu_idx);
10221 }
10222
10223 #ifndef CONFIG_USER_ONLY
10224 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10225 {
10226     switch (mmu_idx) {
10227     case ARMMMUIdx_SE10_0:
10228     case ARMMMUIdx_E20_0:
10229     case ARMMMUIdx_SE20_0:
10230     case ARMMMUIdx_Stage1_E0:
10231     case ARMMMUIdx_Stage1_SE0:
10232     case ARMMMUIdx_MUser:
10233     case ARMMMUIdx_MSUser:
10234     case ARMMMUIdx_MUserNegPri:
10235     case ARMMMUIdx_MSUserNegPri:
10236         return true;
10237     default:
10238         return false;
10239     case ARMMMUIdx_E10_0:
10240     case ARMMMUIdx_E10_1:
10241     case ARMMMUIdx_E10_1_PAN:
10242         g_assert_not_reached();
10243     }
10244 }
10245
10246 /* Translate section/page access permissions to page
10247  * R/W protection flags
10248  *
10249  * @env:         CPUARMState
10250  * @mmu_idx:     MMU index indicating required translation regime
10251  * @ap:          The 3-bit access permissions (AP[2:0])
10252  * @domain_prot: The 2-bit domain access permissions
10253  */
10254 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10255                                 int ap, int domain_prot)
10256 {
10257     bool is_user = regime_is_user(env, mmu_idx);
10258
10259     if (domain_prot == 3) {
10260         return PAGE_READ | PAGE_WRITE;
10261     }
10262
10263     switch (ap) {
10264     case 0:
10265         if (arm_feature(env, ARM_FEATURE_V7)) {
10266             return 0;
10267         }
10268         switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10269         case SCTLR_S:
10270             return is_user ? 0 : PAGE_READ;
10271         case SCTLR_R:
10272             return PAGE_READ;
10273         default:
10274             return 0;
10275         }
10276     case 1:
10277         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10278     case 2:
10279         if (is_user) {
10280             return PAGE_READ;
10281         } else {
10282             return PAGE_READ | PAGE_WRITE;
10283         }
10284     case 3:
10285         return PAGE_READ | PAGE_WRITE;
10286     case 4: /* Reserved.  */
10287         return 0;
10288     case 5:
10289         return is_user ? 0 : PAGE_READ;
10290     case 6:
10291         return PAGE_READ;
10292     case 7:
10293         if (!arm_feature(env, ARM_FEATURE_V6K)) {
10294             return 0;
10295         }
10296         return PAGE_READ;
10297     default:
10298         g_assert_not_reached();
10299     }
10300 }
10301
10302 /* Translate section/page access permissions to page
10303  * R/W protection flags.
10304  *
10305  * @ap:      The 2-bit simple AP (AP[2:1])
10306  * @is_user: TRUE if accessing from PL0
10307  */
10308 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
10309 {
10310     switch (ap) {
10311     case 0:
10312         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10313     case 1:
10314         return PAGE_READ | PAGE_WRITE;
10315     case 2:
10316         return is_user ? 0 : PAGE_READ;
10317     case 3:
10318         return PAGE_READ;
10319     default:
10320         g_assert_not_reached();
10321     }
10322 }
10323
10324 static inline int
10325 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10326 {
10327     return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10328 }
10329
10330 /* Translate S2 section/page access permissions to protection flags
10331  *
10332  * @env:     CPUARMState
10333  * @s2ap:    The 2-bit stage2 access permissions (S2AP)
10334  * @xn:      XN (execute-never) bits
10335  * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
10336  */
10337 static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
10338 {
10339     int prot = 0;
10340
10341     if (s2ap & 1) {
10342         prot |= PAGE_READ;
10343     }
10344     if (s2ap & 2) {
10345         prot |= PAGE_WRITE;
10346     }
10347
10348     if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10349         switch (xn) {
10350         case 0:
10351             prot |= PAGE_EXEC;
10352             break;
10353         case 1:
10354             if (s1_is_el0) {
10355                 prot |= PAGE_EXEC;
10356             }
10357             break;
10358         case 2:
10359             break;
10360         case 3:
10361             if (!s1_is_el0) {
10362                 prot |= PAGE_EXEC;
10363             }
10364             break;
10365         default:
10366             g_assert_not_reached();
10367         }
10368     } else {
10369         if (!extract32(xn, 1, 1)) {
10370             if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10371                 prot |= PAGE_EXEC;
10372             }
10373         }
10374     }
10375     return prot;
10376 }
10377
10378 /* Translate section/page access permissions to protection flags
10379  *
10380  * @env:     CPUARMState
10381  * @mmu_idx: MMU index indicating required translation regime
10382  * @is_aa64: TRUE if AArch64
10383  * @ap:      The 2-bit simple AP (AP[2:1])
10384  * @ns:      NS (non-secure) bit
10385  * @xn:      XN (execute-never) bit
10386  * @pxn:     PXN (privileged execute-never) bit
10387  */
10388 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10389                       int ap, int ns, int xn, int pxn)
10390 {
10391     bool is_user = regime_is_user(env, mmu_idx);
10392     int prot_rw, user_rw;
10393     bool have_wxn;
10394     int wxn = 0;
10395
10396     assert(mmu_idx != ARMMMUIdx_Stage2);
10397     assert(mmu_idx != ARMMMUIdx_Stage2_S);
10398
10399     user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10400     if (is_user) {
10401         prot_rw = user_rw;
10402     } else {
10403         if (user_rw && regime_is_pan(env, mmu_idx)) {
10404             /* PAN forbids data accesses but doesn't affect insn fetch */
10405             prot_rw = 0;
10406         } else {
10407             prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
10408         }
10409     }
10410
10411     if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10412         return prot_rw;
10413     }
10414
10415     /* TODO have_wxn should be replaced with
10416      *   ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10417      * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10418      * compatible processors have EL2, which is required for [U]WXN.
10419      */
10420     have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10421
10422     if (have_wxn) {
10423         wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10424     }
10425
10426     if (is_aa64) {
10427         if (regime_has_2_ranges(mmu_idx) && !is_user) {
10428             xn = pxn || (user_rw & PAGE_WRITE);
10429         }
10430     } else if (arm_feature(env, ARM_FEATURE_V7)) {
10431         switch (regime_el(env, mmu_idx)) {
10432         case 1:
10433         case 3:
10434             if (is_user) {
10435                 xn = xn || !(user_rw & PAGE_READ);
10436             } else {
10437                 int uwxn = 0;
10438                 if (have_wxn) {
10439                     uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10440                 }
10441                 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10442                      (uwxn && (user_rw & PAGE_WRITE));
10443             }
10444             break;
10445         case 2:
10446             break;
10447         }
10448     } else {
10449         xn = wxn = 0;
10450     }
10451
10452     if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10453         return prot_rw;
10454     }
10455     return prot_rw | PAGE_EXEC;
10456 }
10457
10458 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10459                                      uint32_t *table, uint32_t address)
10460 {
10461     /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
10462     TCR *tcr = regime_tcr(env, mmu_idx);
10463
10464     if (address & tcr->mask) {
10465         if (tcr->raw_tcr & TTBCR_PD1) {
10466             /* Translation table walk disabled for TTBR1 */
10467             return false;
10468         }
10469         *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
10470     } else {
10471         if (tcr->raw_tcr & TTBCR_PD0) {
10472             /* Translation table walk disabled for TTBR0 */
10473             return false;
10474         }
10475         *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
10476     }
10477     *table |= (address >> 18) & 0x3ffc;
10478     return true;
10479 }
10480
10481 /* Translate a S1 pagetable walk through S2 if needed.  */
10482 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10483                                hwaddr addr, bool *is_secure,
10484                                ARMMMUFaultInfo *fi)
10485 {
10486     if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
10487         !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
10488         target_ulong s2size;
10489         hwaddr s2pa;
10490         int s2prot;
10491         int ret;
10492         ARMMMUIdx s2_mmu_idx = *is_secure ? ARMMMUIdx_Stage2_S
10493                                           : ARMMMUIdx_Stage2;
10494         ARMCacheAttrs cacheattrs = {};
10495         MemTxAttrs txattrs = {};
10496
10497         ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, s2_mmu_idx, false,
10498                                  &s2pa, &txattrs, &s2prot, &s2size, fi,
10499                                  &cacheattrs);
10500         if (ret) {
10501             assert(fi->type != ARMFault_None);
10502             fi->s2addr = addr;
10503             fi->stage2 = true;
10504             fi->s1ptw = true;
10505             fi->s1ns = !*is_secure;
10506             return ~0;
10507         }
10508         if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10509             (cacheattrs.attrs & 0xf0) == 0) {
10510             /*
10511              * PTW set and S1 walk touched S2 Device memory:
10512              * generate Permission fault.
10513              */
10514             fi->type = ARMFault_Permission;
10515             fi->s2addr = addr;
10516             fi->stage2 = true;
10517             fi->s1ptw = true;
10518             fi->s1ns = !*is_secure;
10519             return ~0;
10520         }
10521
10522         if (arm_is_secure_below_el3(env)) {
10523             /* Check if page table walk is to secure or non-secure PA space. */
10524             if (*is_secure) {
10525                 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10526             } else {
10527                 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10528             }
10529         } else {
10530             assert(!*is_secure);
10531         }
10532
10533         addr = s2pa;
10534     }
10535     return addr;
10536 }
10537
10538 /* All loads done in the course of a page table walk go through here. */
10539 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10540                             ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10541 {
10542     ARMCPU *cpu = ARM_CPU(cs);
10543     CPUARMState *env = &cpu->env;
10544     MemTxAttrs attrs = {};
10545     MemTxResult result = MEMTX_OK;
10546     AddressSpace *as;
10547     uint32_t data;
10548
10549     addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
10550     attrs.secure = is_secure;
10551     as = arm_addressspace(cs, attrs);
10552     if (fi->s1ptw) {
10553         return 0;
10554     }
10555     if (regime_translation_big_endian(env, mmu_idx)) {
10556         data = address_space_ldl_be(as, addr, attrs, &result);
10557     } else {
10558         data = address_space_ldl_le(as, addr, attrs, &result);
10559     }
10560     if (result == MEMTX_OK) {
10561         return data;
10562     }
10563     fi->type = ARMFault_SyncExternalOnWalk;
10564     fi->ea = arm_extabort_type(result);
10565     return 0;
10566 }
10567
10568 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10569                             ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10570 {
10571     ARMCPU *cpu = ARM_CPU(cs);
10572     CPUARMState *env = &cpu->env;
10573     MemTxAttrs attrs = {};
10574     MemTxResult result = MEMTX_OK;
10575     AddressSpace *as;
10576     uint64_t data;
10577
10578     addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
10579     attrs.secure = is_secure;
10580     as = arm_addressspace(cs, attrs);
10581     if (fi->s1ptw) {
10582         return 0;
10583     }
10584     if (regime_translation_big_endian(env, mmu_idx)) {
10585         data = address_space_ldq_be(as, addr, attrs, &result);
10586     } else {
10587         data = address_space_ldq_le(as, addr, attrs, &result);
10588     }
10589     if (result == MEMTX_OK) {
10590         return data;
10591     }
10592     fi->type = ARMFault_SyncExternalOnWalk;
10593     fi->ea = arm_extabort_type(result);
10594     return 0;
10595 }
10596
10597 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
10598                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
10599                              hwaddr *phys_ptr, int *prot,
10600                              target_ulong *page_size,
10601                              ARMMMUFaultInfo *fi)
10602 {
10603     CPUState *cs = env_cpu(env);
10604     int level = 1;
10605     uint32_t table;
10606     uint32_t desc;
10607     int type;
10608     int ap;
10609     int domain = 0;
10610     int domain_prot;
10611     hwaddr phys_addr;
10612     uint32_t dacr;
10613
10614     /* Pagetable walk.  */
10615     /* Lookup l1 descriptor.  */
10616     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
10617         /* Section translation fault if page walk is disabled by PD0 or PD1 */
10618         fi->type = ARMFault_Translation;
10619         goto do_fault;
10620     }
10621     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10622                        mmu_idx, fi);
10623     if (fi->type != ARMFault_None) {
10624         goto do_fault;
10625     }
10626     type = (desc & 3);
10627     domain = (desc >> 5) & 0x0f;
10628     if (regime_el(env, mmu_idx) == 1) {
10629         dacr = env->cp15.dacr_ns;
10630     } else {
10631         dacr = env->cp15.dacr_s;
10632     }
10633     domain_prot = (dacr >> (domain * 2)) & 3;
10634     if (type == 0) {
10635         /* Section translation fault.  */
10636         fi->type = ARMFault_Translation;
10637         goto do_fault;
10638     }
10639     if (type != 2) {
10640         level = 2;
10641     }
10642     if (domain_prot == 0 || domain_prot == 2) {
10643         fi->type = ARMFault_Domain;
10644         goto do_fault;
10645     }
10646     if (type == 2) {
10647         /* 1Mb section.  */
10648         phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10649         ap = (desc >> 10) & 3;
10650         *page_size = 1024 * 1024;
10651     } else {
10652         /* Lookup l2 entry.  */
10653         if (type == 1) {
10654             /* Coarse pagetable.  */
10655             table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10656         } else {
10657             /* Fine pagetable.  */
10658             table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10659         }
10660         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10661                            mmu_idx, fi);
10662         if (fi->type != ARMFault_None) {
10663             goto do_fault;
10664         }
10665         switch (desc & 3) {
10666         case 0: /* Page translation fault.  */
10667             fi->type = ARMFault_Translation;
10668             goto do_fault;
10669         case 1: /* 64k page.  */
10670             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10671             ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
10672             *page_size = 0x10000;
10673             break;
10674         case 2: /* 4k page.  */
10675             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10676             ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
10677             *page_size = 0x1000;
10678             break;
10679         case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
10680             if (type == 1) {
10681                 /* ARMv6/XScale extended small page format */
10682                 if (arm_feature(env, ARM_FEATURE_XSCALE)
10683                     || arm_feature(env, ARM_FEATURE_V6)) {
10684                     phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10685                     *page_size = 0x1000;
10686                 } else {
10687                     /* UNPREDICTABLE in ARMv5; we choose to take a
10688                      * page translation fault.
10689                      */
10690                     fi->type = ARMFault_Translation;
10691                     goto do_fault;
10692                 }
10693             } else {
10694                 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
10695                 *page_size = 0x400;
10696             }
10697             ap = (desc >> 4) & 3;
10698             break;
10699         default:
10700             /* Never happens, but compiler isn't smart enough to tell.  */
10701             abort();
10702         }
10703     }
10704     *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10705     *prot |= *prot ? PAGE_EXEC : 0;
10706     if (!(*prot & (1 << access_type))) {
10707         /* Access permission fault.  */
10708         fi->type = ARMFault_Permission;
10709         goto do_fault;
10710     }
10711     *phys_ptr = phys_addr;
10712     return false;
10713 do_fault:
10714     fi->domain = domain;
10715     fi->level = level;
10716     return true;
10717 }
10718
10719 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
10720                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
10721                              hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10722                              target_ulong *page_size, ARMMMUFaultInfo *fi)
10723 {
10724     CPUState *cs = env_cpu(env);
10725     ARMCPU *cpu = env_archcpu(env);
10726     int level = 1;
10727     uint32_t table;
10728     uint32_t desc;
10729     uint32_t xn;
10730     uint32_t pxn = 0;
10731     int type;
10732     int ap;
10733     int domain = 0;
10734     int domain_prot;
10735     hwaddr phys_addr;
10736     uint32_t dacr;
10737     bool ns;
10738
10739     /* Pagetable walk.  */
10740     /* Lookup l1 descriptor.  */
10741     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
10742         /* Section translation fault if page walk is disabled by PD0 or PD1 */
10743         fi->type = ARMFault_Translation;
10744         goto do_fault;
10745     }
10746     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10747                        mmu_idx, fi);
10748     if (fi->type != ARMFault_None) {
10749         goto do_fault;
10750     }
10751     type = (desc & 3);
10752     if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
10753         /* Section translation fault, or attempt to use the encoding
10754          * which is Reserved on implementations without PXN.
10755          */
10756         fi->type = ARMFault_Translation;
10757         goto do_fault;
10758     }
10759     if ((type == 1) || !(desc & (1 << 18))) {
10760         /* Page or Section.  */
10761         domain = (desc >> 5) & 0x0f;
10762     }
10763     if (regime_el(env, mmu_idx) == 1) {
10764         dacr = env->cp15.dacr_ns;
10765     } else {
10766         dacr = env->cp15.dacr_s;
10767     }
10768     if (type == 1) {
10769         level = 2;
10770     }
10771     domain_prot = (dacr >> (domain * 2)) & 3;
10772     if (domain_prot == 0 || domain_prot == 2) {
10773         /* Section or Page domain fault */
10774         fi->type = ARMFault_Domain;
10775         goto do_fault;
10776     }
10777     if (type != 1) {
10778         if (desc & (1 << 18)) {
10779             /* Supersection.  */
10780             phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
10781             phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10782             phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
10783             *page_size = 0x1000000;
10784         } else {
10785             /* Section.  */
10786             phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10787             *page_size = 0x100000;
10788         }
10789         ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10790         xn = desc & (1 << 4);
10791         pxn = desc & 1;
10792         ns = extract32(desc, 19, 1);
10793     } else {
10794         if (cpu_isar_feature(aa32_pxn, cpu)) {
10795             pxn = (desc >> 2) & 1;
10796         }
10797         ns = extract32(desc, 3, 1);
10798         /* Lookup l2 entry.  */
10799         table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10800         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10801                            mmu_idx, fi);
10802         if (fi->type != ARMFault_None) {
10803             goto do_fault;
10804         }
10805         ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10806         switch (desc & 3) {
10807         case 0: /* Page translation fault.  */
10808             fi->type = ARMFault_Translation;
10809             goto do_fault;
10810         case 1: /* 64k page.  */
10811             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10812             xn = desc & (1 << 15);
10813             *page_size = 0x10000;
10814             break;
10815         case 2: case 3: /* 4k page.  */
10816             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10817             xn = desc & 1;
10818             *page_size = 0x1000;
10819             break;
10820         default:
10821             /* Never happens, but compiler isn't smart enough to tell.  */
10822             abort();
10823         }
10824     }
10825     if (domain_prot == 3) {
10826         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10827     } else {
10828         if (pxn && !regime_is_user(env, mmu_idx)) {
10829             xn = 1;
10830         }
10831         if (xn && access_type == MMU_INST_FETCH) {
10832             fi->type = ARMFault_Permission;
10833             goto do_fault;
10834         }
10835
10836         if (arm_feature(env, ARM_FEATURE_V6K) &&
10837                 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10838             /* The simplified model uses AP[0] as an access control bit.  */
10839             if ((ap & 1) == 0) {
10840                 /* Access flag fault.  */
10841                 fi->type = ARMFault_AccessFlag;
10842                 goto do_fault;
10843             }
10844             *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10845         } else {
10846             *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10847         }
10848         if (*prot && !xn) {
10849             *prot |= PAGE_EXEC;
10850         }
10851         if (!(*prot & (1 << access_type))) {
10852             /* Access permission fault.  */
10853             fi->type = ARMFault_Permission;
10854             goto do_fault;
10855         }
10856     }
10857     if (ns) {
10858         /* The NS bit will (as required by the architecture) have no effect if
10859          * the CPU doesn't support TZ or this is a non-secure translation
10860          * regime, because the attribute will already be non-secure.
10861          */
10862         attrs->secure = false;
10863     }
10864     *phys_ptr = phys_addr;
10865     return false;
10866 do_fault:
10867     fi->domain = domain;
10868     fi->level = level;
10869     return true;
10870 }
10871
10872 /*
10873  * check_s2_mmu_setup
10874  * @cpu:        ARMCPU
10875  * @is_aa64:    True if the translation regime is in AArch64 state
10876  * @startlevel: Suggested starting level
10877  * @inputsize:  Bitsize of IPAs
10878  * @stride:     Page-table stride (See the ARM ARM)
10879  *
10880  * Returns true if the suggested S2 translation parameters are OK and
10881  * false otherwise.
10882  */
10883 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10884                                int inputsize, int stride)
10885 {
10886     const int grainsize = stride + 3;
10887     int startsizecheck;
10888
10889     /* Negative levels are never allowed.  */
10890     if (level < 0) {
10891         return false;
10892     }
10893
10894     startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10895     if (startsizecheck < 1 || startsizecheck > stride + 4) {
10896         return false;
10897     }
10898
10899     if (is_aa64) {
10900         CPUARMState *env = &cpu->env;
10901         unsigned int pamax = arm_pamax(cpu);
10902
10903         switch (stride) {
10904         case 13: /* 64KB Pages.  */
10905             if (level == 0 || (level == 1 && pamax <= 42)) {
10906                 return false;
10907             }
10908             break;
10909         case 11: /* 16KB Pages.  */
10910             if (level == 0 || (level == 1 && pamax <= 40)) {
10911                 return false;
10912             }
10913             break;
10914         case 9: /* 4KB Pages.  */
10915             if (level == 0 && pamax <= 42) {
10916                 return false;
10917             }
10918             break;
10919         default:
10920             g_assert_not_reached();
10921         }
10922
10923         /* Inputsize checks.  */
10924         if (inputsize > pamax &&
10925             (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10926             /* This is CONSTRAINED UNPREDICTABLE and we choose to fault.  */
10927             return false;
10928         }
10929     } else {
10930         /* AArch32 only supports 4KB pages. Assert on that.  */
10931         assert(stride == 9);
10932
10933         if (level == 0) {
10934             return false;
10935         }
10936     }
10937     return true;
10938 }
10939
10940 /* Translate from the 4-bit stage 2 representation of
10941  * memory attributes (without cache-allocation hints) to
10942  * the 8-bit representation of the stage 1 MAIR registers
10943  * (which includes allocation hints).
10944  *
10945  * ref: shared/translation/attrs/S2AttrDecode()
10946  *      .../S2ConvertAttrsHints()
10947  */
10948 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10949 {
10950     uint8_t hiattr = extract32(s2attrs, 2, 2);
10951     uint8_t loattr = extract32(s2attrs, 0, 2);
10952     uint8_t hihint = 0, lohint = 0;
10953
10954     if (hiattr != 0) { /* normal memory */
10955         if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
10956             hiattr = loattr = 1; /* non-cacheable */
10957         } else {
10958             if (hiattr != 1) { /* Write-through or write-back */
10959                 hihint = 3; /* RW allocate */
10960             }
10961             if (loattr != 1) { /* Write-through or write-back */
10962                 lohint = 3; /* RW allocate */
10963             }
10964         }
10965     }
10966
10967     return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10968 }
10969 #endif /* !CONFIG_USER_ONLY */
10970
10971 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10972 {
10973     if (regime_has_2_ranges(mmu_idx)) {
10974         return extract64(tcr, 37, 2);
10975     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10976         return 0; /* VTCR_EL2 */
10977     } else {
10978         /* Replicate the single TBI bit so we always have 2 bits.  */
10979         return extract32(tcr, 20, 1) * 3;
10980     }
10981 }
10982
10983 static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10984 {
10985     if (regime_has_2_ranges(mmu_idx)) {
10986         return extract64(tcr, 51, 2);
10987     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10988         return 0; /* VTCR_EL2 */
10989     } else {
10990         /* Replicate the single TBID bit so we always have 2 bits.  */
10991         return extract32(tcr, 29, 1) * 3;
10992     }
10993 }
10994
10995 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10996 {
10997     if (regime_has_2_ranges(mmu_idx)) {
10998         return extract64(tcr, 57, 2);
10999     } else {
11000         /* Replicate the single TCMA bit so we always have 2 bits.  */
11001         return extract32(tcr, 30, 1) * 3;
11002     }
11003 }
11004
11005 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11006                                    ARMMMUIdx mmu_idx, bool data)
11007 {
11008     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11009     bool epd, hpd, using16k, using64k;
11010     int select, tsz, tbi, max_tsz;
11011
11012     if (!regime_has_2_ranges(mmu_idx)) {
11013         select = 0;
11014         tsz = extract32(tcr, 0, 6);
11015         using64k = extract32(tcr, 14, 1);
11016         using16k = extract32(tcr, 15, 1);
11017         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11018             /* VTCR_EL2 */
11019             hpd = false;
11020         } else {
11021             hpd = extract32(tcr, 24, 1);
11022         }
11023         epd = false;
11024     } else {
11025         /*
11026          * Bit 55 is always between the two regions, and is canonical for
11027          * determining if address tagging is enabled.
11028          */
11029         select = extract64(va, 55, 1);
11030         if (!select) {
11031             tsz = extract32(tcr, 0, 6);
11032             epd = extract32(tcr, 7, 1);
11033             using64k = extract32(tcr, 14, 1);
11034             using16k = extract32(tcr, 15, 1);
11035             hpd = extract64(tcr, 41, 1);
11036         } else {
11037             int tg = extract32(tcr, 30, 2);
11038             using16k = tg == 1;
11039             using64k = tg == 3;
11040             tsz = extract32(tcr, 16, 6);
11041             epd = extract32(tcr, 23, 1);
11042             hpd = extract64(tcr, 42, 1);
11043         }
11044     }
11045
11046     if (cpu_isar_feature(aa64_st, env_archcpu(env))) {
11047         max_tsz = 48 - using64k;
11048     } else {
11049         max_tsz = 39;
11050     }
11051
11052     tsz = MIN(tsz, max_tsz);
11053     tsz = MAX(tsz, 16);  /* TODO: ARMv8.2-LVA  */
11054
11055     /* Present TBI as a composite with TBID.  */
11056     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11057     if (!data) {
11058         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11059     }
11060     tbi = (tbi >> select) & 1;
11061
11062     return (ARMVAParameters) {
11063         .tsz = tsz,
11064         .select = select,
11065         .tbi = tbi,
11066         .epd = epd,
11067         .hpd = hpd,
11068         .using16k = using16k,
11069         .using64k = using64k,
11070     };
11071 }
11072
11073 #ifndef CONFIG_USER_ONLY
11074 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11075                                           ARMMMUIdx mmu_idx)
11076 {
11077     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11078     uint32_t el = regime_el(env, mmu_idx);
11079     int select, tsz;
11080     bool epd, hpd;
11081
11082     assert(mmu_idx != ARMMMUIdx_Stage2_S);
11083
11084     if (mmu_idx == ARMMMUIdx_Stage2) {
11085         /* VTCR */
11086         bool sext = extract32(tcr, 4, 1);
11087         bool sign = extract32(tcr, 3, 1);
11088
11089         /*
11090          * If the sign-extend bit is not the same as t0sz[3], the result
11091          * is unpredictable. Flag this as a guest error.
11092          */
11093         if (sign != sext) {
11094             qemu_log_mask(LOG_GUEST_ERROR,
11095                           "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11096         }
11097         tsz = sextract32(tcr, 0, 4) + 8;
11098         select = 0;
11099         hpd = false;
11100         epd = false;
11101     } else if (el == 2) {
11102         /* HTCR */
11103         tsz = extract32(tcr, 0, 3);
11104         select = 0;
11105         hpd = extract64(tcr, 24, 1);
11106         epd = false;
11107     } else {
11108         int t0sz = extract32(tcr, 0, 3);
11109         int t1sz = extract32(tcr, 16, 3);
11110
11111         if (t1sz == 0) {
11112             select = va > (0xffffffffu >> t0sz);
11113         } else {
11114             /* Note that we will detect errors later.  */
11115             select = va >= ~(0xffffffffu >> t1sz);
11116         }
11117         if (!select) {
11118             tsz = t0sz;
11119             epd = extract32(tcr, 7, 1);
11120             hpd = extract64(tcr, 41, 1);
11121         } else {
11122             tsz = t1sz;
11123             epd = extract32(tcr, 23, 1);
11124             hpd = extract64(tcr, 42, 1);
11125         }
11126         /* For aarch32, hpd0 is not enabled without t2e as well.  */
11127         hpd &= extract32(tcr, 6, 1);
11128     }
11129
11130     return (ARMVAParameters) {
11131         .tsz = tsz,
11132         .select = select,
11133         .epd = epd,
11134         .hpd = hpd,
11135     };
11136 }
11137
11138 /**
11139  * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11140  *
11141  * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11142  * prot and page_size may not be filled in, and the populated fsr value provides
11143  * information on why the translation aborted, in the format of a long-format
11144  * DFSR/IFSR fault register, with the following caveats:
11145  *  * the WnR bit is never set (the caller must do this).
11146  *
11147  * @env: CPUARMState
11148  * @address: virtual address to get physical address for
11149  * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11150  * @mmu_idx: MMU index indicating required translation regime
11151  * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11152  *             walk), must be true if this is stage 2 of a stage 1+2 walk for an
11153  *             EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11154  * @phys_ptr: set to the physical address corresponding to the virtual address
11155  * @attrs: set to the memory transaction attributes to use
11156  * @prot: set to the permissions for the page containing phys_ptr
11157  * @page_size_ptr: set to the size of the page containing phys_ptr
11158  * @fi: set to fault info if the translation fails
11159  * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11160  */
11161 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
11162                                MMUAccessType access_type, ARMMMUIdx mmu_idx,
11163                                bool s1_is_el0,
11164                                hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
11165                                target_ulong *page_size_ptr,
11166                                ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
11167 {
11168     ARMCPU *cpu = env_archcpu(env);
11169     CPUState *cs = CPU(cpu);
11170     /* Read an LPAE long-descriptor translation table. */
11171     ARMFaultType fault_type = ARMFault_Translation;
11172     uint32_t level;
11173     ARMVAParameters param;
11174     uint64_t ttbr;
11175     hwaddr descaddr, indexmask, indexmask_grainsize;
11176     uint32_t tableattrs;
11177     target_ulong page_size;
11178     uint32_t attrs;
11179     int32_t stride;
11180     int addrsize, inputsize;
11181     TCR *tcr = regime_tcr(env, mmu_idx);
11182     int ap, ns, xn, pxn;
11183     uint32_t el = regime_el(env, mmu_idx);
11184     uint64_t descaddrmask;
11185     bool aarch64 = arm_el_is_aa64(env, el);
11186     bool guarded = false;
11187
11188     /* TODO: This code does not support shareability levels. */
11189     if (aarch64) {
11190         param = aa64_va_parameters(env, address, mmu_idx,
11191                                    access_type != MMU_INST_FETCH);
11192         level = 0;
11193         addrsize = 64 - 8 * param.tbi;
11194         inputsize = 64 - param.tsz;
11195     } else {
11196         param = aa32_va_parameters(env, address, mmu_idx);
11197         level = 1;
11198         addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
11199         inputsize = addrsize - param.tsz;
11200     }
11201
11202     /*
11203      * We determined the region when collecting the parameters, but we
11204      * have not yet validated that the address is valid for the region.
11205      * Extract the top bits and verify that they all match select.
11206      *
11207      * For aa32, if inputsize == addrsize, then we have selected the
11208      * region by exclusion in aa32_va_parameters and there is no more
11209      * validation to do here.
11210      */
11211     if (inputsize < addrsize) {
11212         target_ulong top_bits = sextract64(address, inputsize,
11213                                            addrsize - inputsize);
11214         if (-top_bits != param.select) {
11215             /* The gap between the two regions is a Translation fault */
11216             fault_type = ARMFault_Translation;
11217             goto do_fault;
11218         }
11219     }
11220
11221     if (param.using64k) {
11222         stride = 13;
11223     } else if (param.using16k) {
11224         stride = 11;
11225     } else {
11226         stride = 9;
11227     }
11228
11229     /* Note that QEMU ignores shareability and cacheability attributes,
11230      * so we don't need to do anything with the SH, ORGN, IRGN fields
11231      * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
11232      * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11233      * implement any ASID-like capability so we can ignore it (instead
11234      * we will always flush the TLB any time the ASID is changed).
11235      */
11236     ttbr = regime_ttbr(env, mmu_idx, param.select);
11237
11238     /* Here we should have set up all the parameters for the translation:
11239      * inputsize, ttbr, epd, stride, tbi
11240      */
11241
11242     if (param.epd) {
11243         /* Translation table walk disabled => Translation fault on TLB miss
11244          * Note: This is always 0 on 64-bit EL2 and EL3.
11245          */
11246         goto do_fault;
11247     }
11248
11249     if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
11250         /* The starting level depends on the virtual address size (which can
11251          * be up to 48 bits) and the translation granule size. It indicates
11252          * the number of strides (stride bits at a time) needed to
11253          * consume the bits of the input address. In the pseudocode this is:
11254          *  level = 4 - RoundUp((inputsize - grainsize) / stride)
11255          * where their 'inputsize' is our 'inputsize', 'grainsize' is
11256          * our 'stride + 3' and 'stride' is our 'stride'.
11257          * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11258          * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11259          * = 4 - (inputsize - 4) / stride;
11260          */
11261         level = 4 - (inputsize - 4) / stride;
11262     } else {
11263         /* For stage 2 translations the starting level is specified by the
11264          * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11265          */
11266         uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
11267         uint32_t startlevel;
11268         bool ok;
11269
11270         if (!aarch64 || stride == 9) {
11271             /* AArch32 or 4KB pages */
11272             startlevel = 2 - sl0;
11273
11274             if (cpu_isar_feature(aa64_st, cpu)) {
11275                 startlevel &= 3;
11276             }
11277         } else {
11278             /* 16KB or 64KB pages */
11279             startlevel = 3 - sl0;
11280         }
11281
11282         /* Check that the starting level is valid. */
11283         ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
11284                                 inputsize, stride);
11285         if (!ok) {
11286             fault_type = ARMFault_Translation;
11287             goto do_fault;
11288         }
11289         level = startlevel;
11290     }
11291
11292     indexmask_grainsize = (1ULL << (stride + 3)) - 1;
11293     indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
11294
11295     /* Now we can extract the actual base address from the TTBR */
11296     descaddr = extract64(ttbr, 0, 48);
11297     /*
11298      * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11299      * and also to mask out CnP (bit 0) which could validly be non-zero.
11300      */
11301     descaddr &= ~indexmask;
11302
11303     /* The address field in the descriptor goes up to bit 39 for ARMv7
11304      * but up to bit 47 for ARMv8, but we use the descaddrmask
11305      * up to bit 39 for AArch32, because we don't need other bits in that case
11306      * to construct next descriptor address (anyway they should be all zeroes).
11307      */
11308     descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
11309                    ~indexmask_grainsize;
11310
11311     /* Secure accesses start with the page table in secure memory and
11312      * can be downgraded to non-secure at any step. Non-secure accesses
11313      * remain non-secure. We implement this by just ORing in the NSTable/NS
11314      * bits at each step.
11315      */
11316     tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
11317     for (;;) {
11318         uint64_t descriptor;
11319         bool nstable;
11320
11321         descaddr |= (address >> (stride * (4 - level))) & indexmask;
11322         descaddr &= ~7ULL;
11323         nstable = extract32(tableattrs, 4, 1);
11324         descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
11325         if (fi->type != ARMFault_None) {
11326             goto do_fault;
11327         }
11328
11329         if (!(descriptor & 1) ||
11330             (!(descriptor & 2) && (level == 3))) {
11331             /* Invalid, or the Reserved level 3 encoding */
11332             goto do_fault;
11333         }
11334         descaddr = descriptor & descaddrmask;
11335
11336         if ((descriptor & 2) && (level < 3)) {
11337             /* Table entry. The top five bits are attributes which may
11338              * propagate down through lower levels of the table (and
11339              * which are all arranged so that 0 means "no effect", so
11340              * we can gather them up by ORing in the bits at each level).
11341              */
11342             tableattrs |= extract64(descriptor, 59, 5);
11343             level++;
11344             indexmask = indexmask_grainsize;
11345             continue;
11346         }
11347         /* Block entry at level 1 or 2, or page entry at level 3.
11348          * These are basically the same thing, although the number
11349          * of bits we pull in from the vaddr varies.
11350          */
11351         page_size = (1ULL << ((stride * (4 - level)) + 3));
11352         descaddr |= (address & (page_size - 1));
11353         /* Extract attributes from the descriptor */
11354         attrs = extract64(descriptor, 2, 10)
11355             | (extract64(descriptor, 52, 12) << 10);
11356
11357         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11358             /* Stage 2 table descriptors do not include any attribute fields */
11359             break;
11360         }
11361         /* Merge in attributes from table descriptors */
11362         attrs |= nstable << 3; /* NS */
11363         guarded = extract64(descriptor, 50, 1);  /* GP */
11364         if (param.hpd) {
11365             /* HPD disables all the table attributes except NSTable.  */
11366             break;
11367         }
11368         attrs |= extract32(tableattrs, 0, 2) << 11;     /* XN, PXN */
11369         /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11370          * means "force PL1 access only", which means forcing AP[1] to 0.
11371          */
11372         attrs &= ~(extract32(tableattrs, 2, 1) << 4);   /* !APT[0] => AP[1] */
11373         attrs |= extract32(tableattrs, 3, 1) << 5;      /* APT[1] => AP[2] */
11374         break;
11375     }
11376     /* Here descaddr is the final physical address, and attributes
11377      * are all in attrs.
11378      */
11379     fault_type = ARMFault_AccessFlag;
11380     if ((attrs & (1 << 8)) == 0) {
11381         /* Access flag */
11382         goto do_fault;
11383     }
11384
11385     ap = extract32(attrs, 4, 2);
11386
11387     if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11388         ns = mmu_idx == ARMMMUIdx_Stage2;
11389         xn = extract32(attrs, 11, 2);
11390         *prot = get_S2prot(env, ap, xn, s1_is_el0);
11391     } else {
11392         ns = extract32(attrs, 3, 1);
11393         xn = extract32(attrs, 12, 1);
11394         pxn = extract32(attrs, 11, 1);
11395         *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
11396     }
11397
11398     fault_type = ARMFault_Permission;
11399     if (!(*prot & (1 << access_type))) {
11400         goto do_fault;
11401     }
11402
11403     if (ns) {
11404         /* The NS bit will (as required by the architecture) have no effect if
11405          * the CPU doesn't support TZ or this is a non-secure translation
11406          * regime, because the attribute will already be non-secure.
11407          */
11408         txattrs->secure = false;
11409     }
11410     /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB.  */
11411     if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
11412         arm_tlb_bti_gp(txattrs) = true;
11413     }
11414
11415     if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11416         cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11417     } else {
11418         /* Index into MAIR registers for cache attributes */
11419         uint8_t attrindx = extract32(attrs, 0, 3);
11420         uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11421         assert(attrindx <= 7);
11422         cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
11423     }
11424     cacheattrs->shareability = extract32(attrs, 6, 2);
11425
11426     *phys_ptr = descaddr;
11427     *page_size_ptr = page_size;
11428     return false;
11429
11430 do_fault:
11431     fi->type = fault_type;
11432     fi->level = level;
11433     /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2.  */
11434     fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2 ||
11435                                mmu_idx == ARMMMUIdx_Stage2_S);
11436     fi->s1ns = mmu_idx == ARMMMUIdx_Stage2;
11437     return true;
11438 }
11439
11440 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11441                                                 ARMMMUIdx mmu_idx,
11442                                                 int32_t address, int *prot)
11443 {
11444     if (!arm_feature(env, ARM_FEATURE_M)) {
11445         *prot = PAGE_READ | PAGE_WRITE;
11446         switch (address) {
11447         case 0xF0000000 ... 0xFFFFFFFF:
11448             if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11449                 /* hivecs execing is ok */
11450                 *prot |= PAGE_EXEC;
11451             }
11452             break;
11453         case 0x00000000 ... 0x7FFFFFFF:
11454             *prot |= PAGE_EXEC;
11455             break;
11456         }
11457     } else {
11458         /* Default system address map for M profile cores.
11459          * The architecture specifies which regions are execute-never;
11460          * at the MPU level no other checks are defined.
11461          */
11462         switch (address) {
11463         case 0x00000000 ... 0x1fffffff: /* ROM */
11464         case 0x20000000 ... 0x3fffffff: /* SRAM */
11465         case 0x60000000 ... 0x7fffffff: /* RAM */
11466         case 0x80000000 ... 0x9fffffff: /* RAM */
11467             *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11468             break;
11469         case 0x40000000 ... 0x5fffffff: /* Peripheral */
11470         case 0xa0000000 ... 0xbfffffff: /* Device */
11471         case 0xc0000000 ... 0xdfffffff: /* Device */
11472         case 0xe0000000 ... 0xffffffff: /* System */
11473             *prot = PAGE_READ | PAGE_WRITE;
11474             break;
11475         default:
11476             g_assert_not_reached();
11477         }
11478     }
11479 }
11480
11481 static bool pmsav7_use_background_region(ARMCPU *cpu,
11482                                          ARMMMUIdx mmu_idx, bool is_user)
11483 {
11484     /* Return true if we should use the default memory map as a
11485      * "background" region if there are no hits against any MPU regions.
11486      */
11487     CPUARMState *env = &cpu->env;
11488
11489     if (is_user) {
11490         return false;
11491     }
11492
11493     if (arm_feature(env, ARM_FEATURE_M)) {
11494         return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11495             & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
11496     } else {
11497         return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11498     }
11499 }
11500
11501 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11502 {
11503     /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11504     return arm_feature(env, ARM_FEATURE_M) &&
11505         extract32(address, 20, 12) == 0xe00;
11506 }
11507
11508 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11509 {
11510     /* True if address is in the M profile system region
11511      * 0xe0000000 - 0xffffffff
11512      */
11513     return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11514 }
11515
11516 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
11517                                  MMUAccessType access_type, ARMMMUIdx mmu_idx,
11518                                  hwaddr *phys_ptr, int *prot,
11519                                  target_ulong *page_size,
11520                                  ARMMMUFaultInfo *fi)
11521 {
11522     ARMCPU *cpu = env_archcpu(env);
11523     int n;
11524     bool is_user = regime_is_user(env, mmu_idx);
11525
11526     *phys_ptr = address;
11527     *page_size = TARGET_PAGE_SIZE;
11528     *prot = 0;
11529
11530     if (regime_translation_disabled(env, mmu_idx) ||
11531         m_is_ppb_region(env, address)) {
11532         /* MPU disabled or M profile PPB access: use default memory map.
11533          * The other case which uses the default memory map in the
11534          * v7M ARM ARM pseudocode is exception vector reads from the vector
11535          * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11536          * which always does a direct read using address_space_ldl(), rather
11537          * than going via this function, so we don't need to check that here.
11538          */
11539         get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11540     } else { /* MPU enabled */
11541         for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11542             /* region search */
11543             uint32_t base = env->pmsav7.drbar[n];
11544             uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11545             uint32_t rmask;
11546             bool srdis = false;
11547
11548             if (!(env->pmsav7.drsr[n] & 0x1)) {
11549                 continue;
11550             }
11551
11552             if (!rsize) {
11553                 qemu_log_mask(LOG_GUEST_ERROR,
11554                               "DRSR[%d]: Rsize field cannot be 0\n", n);
11555                 continue;
11556             }
11557             rsize++;
11558             rmask = (1ull << rsize) - 1;
11559
11560             if (base & rmask) {
11561                 qemu_log_mask(LOG_GUEST_ERROR,
11562                               "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11563                               "to DRSR region size, mask = 0x%" PRIx32 "\n",
11564                               n, base, rmask);
11565                 continue;
11566             }
11567
11568             if (address < base || address > base + rmask) {
11569                 /*
11570                  * Address not in this region. We must check whether the
11571                  * region covers addresses in the same page as our address.
11572                  * In that case we must not report a size that covers the
11573                  * whole page for a subsequent hit against a different MPU
11574                  * region or the background region, because it would result in
11575                  * incorrect TLB hits for subsequent accesses to addresses that
11576                  * are in this MPU region.
11577                  */
11578                 if (ranges_overlap(base, rmask,
11579                                    address & TARGET_PAGE_MASK,
11580                                    TARGET_PAGE_SIZE)) {
11581                     *page_size = 1;
11582                 }
11583                 continue;
11584             }
11585
11586             /* Region matched */
11587
11588             if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11589                 int i, snd;
11590                 uint32_t srdis_mask;
11591
11592                 rsize -= 3; /* sub region size (power of 2) */
11593                 snd = ((address - base) >> rsize) & 0x7;
11594                 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11595
11596                 srdis_mask = srdis ? 0x3 : 0x0;
11597                 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11598                     /* This will check in groups of 2, 4 and then 8, whether
11599                      * the subregion bits are consistent. rsize is incremented
11600                      * back up to give the region size, considering consistent
11601                      * adjacent subregions as one region. Stop testing if rsize
11602                      * is already big enough for an entire QEMU page.
11603                      */
11604                     int snd_rounded = snd & ~(i - 1);
11605                     uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11606                                                      snd_rounded + 8, i);
11607                     if (srdis_mask ^ srdis_multi) {
11608                         break;
11609                     }
11610                     srdis_mask = (srdis_mask << i) | srdis_mask;
11611                     rsize++;
11612                 }
11613             }
11614             if (srdis) {
11615                 continue;
11616             }
11617             if (rsize < TARGET_PAGE_BITS) {
11618                 *page_size = 1 << rsize;
11619             }
11620             break;
11621         }
11622
11623         if (n == -1) { /* no hits */
11624             if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11625                 /* background fault */
11626                 fi->type = ARMFault_Background;
11627                 return true;
11628             }
11629             get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11630         } else { /* a MPU hit! */
11631             uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
11632             uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11633
11634             if (m_is_system_region(env, address)) {
11635                 /* System space is always execute never */
11636                 xn = 1;
11637             }
11638
11639             if (is_user) { /* User mode AP bit decoding */
11640                 switch (ap) {
11641                 case 0:
11642                 case 1:
11643                 case 5:
11644                     break; /* no access */
11645                 case 3:
11646                     *prot |= PAGE_WRITE;
11647                     /* fall through */
11648                 case 2:
11649                 case 6:
11650                     *prot |= PAGE_READ | PAGE_EXEC;
11651                     break;
11652                 case 7:
11653                     /* for v7M, same as 6; for R profile a reserved value */
11654                     if (arm_feature(env, ARM_FEATURE_M)) {
11655                         *prot |= PAGE_READ | PAGE_EXEC;
11656                         break;
11657                     }
11658                     /* fall through */
11659                 default:
11660                     qemu_log_mask(LOG_GUEST_ERROR,
11661                                   "DRACR[%d]: Bad value for AP bits: 0x%"
11662                                   PRIx32 "\n", n, ap);
11663                 }
11664             } else { /* Priv. mode AP bits decoding */
11665                 switch (ap) {
11666                 case 0:
11667                     break; /* no access */
11668                 case 1:
11669                 case 2:
11670                 case 3:
11671                     *prot |= PAGE_WRITE;
11672                     /* fall through */
11673                 case 5:
11674                 case 6:
11675                     *prot |= PAGE_READ | PAGE_EXEC;
11676                     break;
11677                 case 7:
11678                     /* for v7M, same as 6; for R profile a reserved value */
11679                     if (arm_feature(env, ARM_FEATURE_M)) {
11680                         *prot |= PAGE_READ | PAGE_EXEC;
11681                         break;
11682                     }
11683                     /* fall through */
11684                 default:
11685                     qemu_log_mask(LOG_GUEST_ERROR,
11686                                   "DRACR[%d]: Bad value for AP bits: 0x%"
11687                                   PRIx32 "\n", n, ap);
11688                 }
11689             }
11690
11691             /* execute never */
11692             if (xn) {
11693                 *prot &= ~PAGE_EXEC;
11694             }
11695         }
11696     }
11697
11698     fi->type = ARMFault_Permission;
11699     fi->level = 1;
11700     return !(*prot & (1 << access_type));
11701 }
11702
11703 static bool v8m_is_sau_exempt(CPUARMState *env,
11704                               uint32_t address, MMUAccessType access_type)
11705 {
11706     /* The architecture specifies that certain address ranges are
11707      * exempt from v8M SAU/IDAU checks.
11708      */
11709     return
11710         (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11711         (address >= 0xe0000000 && address <= 0xe0002fff) ||
11712         (address >= 0xe000e000 && address <= 0xe000efff) ||
11713         (address >= 0xe002e000 && address <= 0xe002efff) ||
11714         (address >= 0xe0040000 && address <= 0xe0041fff) ||
11715         (address >= 0xe00ff000 && address <= 0xe00fffff);
11716 }
11717
11718 void v8m_security_lookup(CPUARMState *env, uint32_t address,
11719                                 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11720                                 V8M_SAttributes *sattrs)
11721 {
11722     /* Look up the security attributes for this address. Compare the
11723      * pseudocode SecurityCheck() function.
11724      * We assume the caller has zero-initialized *sattrs.
11725      */
11726     ARMCPU *cpu = env_archcpu(env);
11727     int r;
11728     bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11729     int idau_region = IREGION_NOTVALID;
11730     uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11731     uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
11732
11733     if (cpu->idau) {
11734         IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11735         IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11736
11737         iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11738                    &idau_nsc);
11739     }
11740
11741     if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11742         /* 0xf0000000..0xffffffff is always S for insn fetches */
11743         return;
11744     }
11745
11746     if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
11747         sattrs->ns = !regime_is_secure(env, mmu_idx);
11748         return;
11749     }
11750
11751     if (idau_region != IREGION_NOTVALID) {
11752         sattrs->irvalid = true;
11753         sattrs->iregion = idau_region;
11754     }
11755
11756     switch (env->sau.ctrl & 3) {
11757     case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11758         break;
11759     case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11760         sattrs->ns = true;
11761         break;
11762     default: /* SAU.ENABLE == 1 */
11763         for (r = 0; r < cpu->sau_sregion; r++) {
11764             if (env->sau.rlar[r] & 1) {
11765                 uint32_t base = env->sau.rbar[r] & ~0x1f;
11766                 uint32_t limit = env->sau.rlar[r] | 0x1f;
11767
11768                 if (base <= address && limit >= address) {
11769                     if (base > addr_page_base || limit < addr_page_limit) {
11770                         sattrs->subpage = true;
11771                     }
11772                     if (sattrs->srvalid) {
11773                         /* If we hit in more than one region then we must report
11774                          * as Secure, not NS-Callable, with no valid region
11775                          * number info.
11776                          */
11777                         sattrs->ns = false;
11778                         sattrs->nsc = false;
11779                         sattrs->sregion = 0;
11780                         sattrs->srvalid = false;
11781                         break;
11782                     } else {
11783                         if (env->sau.rlar[r] & 2) {
11784                             sattrs->nsc = true;
11785                         } else {
11786                             sattrs->ns = true;
11787                         }
11788                         sattrs->srvalid = true;
11789                         sattrs->sregion = r;
11790                     }
11791                 } else {
11792                     /*
11793                      * Address not in this region. We must check whether the
11794                      * region covers addresses in the same page as our address.
11795                      * In that case we must not report a size that covers the
11796                      * whole page for a subsequent hit against a different MPU
11797                      * region or the background region, because it would result
11798                      * in incorrect TLB hits for subsequent accesses to
11799                      * addresses that are in this MPU region.
11800                      */
11801                     if (limit >= base &&
11802                         ranges_overlap(base, limit - base + 1,
11803                                        addr_page_base,
11804                                        TARGET_PAGE_SIZE)) {
11805                         sattrs->subpage = true;
11806                     }
11807                 }
11808             }
11809         }
11810         break;
11811     }
11812
11813     /*
11814      * The IDAU will override the SAU lookup results if it specifies
11815      * higher security than the SAU does.
11816      */
11817     if (!idau_ns) {
11818         if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11819             sattrs->ns = false;
11820             sattrs->nsc = idau_nsc;
11821         }
11822     }
11823 }
11824
11825 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
11826                               MMUAccessType access_type, ARMMMUIdx mmu_idx,
11827                               hwaddr *phys_ptr, MemTxAttrs *txattrs,
11828                               int *prot, bool *is_subpage,
11829                               ARMMMUFaultInfo *fi, uint32_t *mregion)
11830 {
11831     /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11832      * that a full phys-to-virt translation does).
11833      * mregion is (if not NULL) set to the region number which matched,
11834      * or -1 if no region number is returned (MPU off, address did not
11835      * hit a region, address hit in multiple regions).
11836      * We set is_subpage to true if the region hit doesn't cover the
11837      * entire TARGET_PAGE the address is within.
11838      */
11839     ARMCPU *cpu = env_archcpu(env);
11840     bool is_user = regime_is_user(env, mmu_idx);
11841     uint32_t secure = regime_is_secure(env, mmu_idx);
11842     int n;
11843     int matchregion = -1;
11844     bool hit = false;
11845     uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11846     uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
11847
11848     *is_subpage = false;
11849     *phys_ptr = address;
11850     *prot = 0;
11851     if (mregion) {
11852         *mregion = -1;
11853     }
11854
11855     /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11856      * was an exception vector read from the vector table (which is always
11857      * done using the default system address map), because those accesses
11858      * are done in arm_v7m_load_vector(), which always does a direct
11859      * read using address_space_ldl(), rather than going via this function.
11860      */
11861     if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11862         hit = true;
11863     } else if (m_is_ppb_region(env, address)) {
11864         hit = true;
11865     } else {
11866         if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11867             hit = true;
11868         }
11869
11870         for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11871             /* region search */
11872             /* Note that the base address is bits [31:5] from the register
11873              * with bits [4:0] all zeroes, but the limit address is bits
11874              * [31:5] from the register with bits [4:0] all ones.
11875              */
11876             uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11877             uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
11878
11879             if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
11880                 /* Region disabled */
11881                 continue;
11882             }
11883
11884             if (address < base || address > limit) {
11885                 /*
11886                  * Address not in this region. We must check whether the
11887                  * region covers addresses in the same page as our address.
11888                  * In that case we must not report a size that covers the
11889                  * whole page for a subsequent hit against a different MPU
11890                  * region or the background region, because it would result in
11891                  * incorrect TLB hits for subsequent accesses to addresses that
11892                  * are in this MPU region.
11893                  */
11894                 if (limit >= base &&
11895                     ranges_overlap(base, limit - base + 1,
11896                                    addr_page_base,
11897                                    TARGET_PAGE_SIZE)) {
11898                     *is_subpage = true;
11899                 }
11900                 continue;
11901             }
11902
11903             if (base > addr_page_base || limit < addr_page_limit) {
11904                 *is_subpage = true;
11905             }
11906
11907             if (matchregion != -1) {
11908                 /* Multiple regions match -- always a failure (unlike
11909                  * PMSAv7 where highest-numbered-region wins)
11910                  */
11911                 fi->type = ARMFault_Permission;
11912                 fi->level = 1;
11913                 return true;
11914             }
11915
11916             matchregion = n;
11917             hit = true;
11918         }
11919     }
11920
11921     if (!hit) {
11922         /* background fault */
11923         fi->type = ARMFault_Background;
11924         return true;
11925     }
11926
11927     if (matchregion == -1) {
11928         /* hit using the background region */
11929         get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11930     } else {
11931         uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11932         uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
11933         bool pxn = false;
11934
11935         if (arm_feature(env, ARM_FEATURE_V8_1M)) {
11936             pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
11937         }
11938
11939         if (m_is_system_region(env, address)) {
11940             /* System space is always execute never */
11941             xn = 1;
11942         }
11943
11944         *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11945         if (*prot && !xn && !(pxn && !is_user)) {
11946             *prot |= PAGE_EXEC;
11947         }
11948         /* We don't need to look the attribute up in the MAIR0/MAIR1
11949          * registers because that only tells us about cacheability.
11950          */
11951         if (mregion) {
11952             *mregion = matchregion;
11953         }
11954     }
11955
11956     fi->type = ARMFault_Permission;
11957     fi->level = 1;
11958     return !(*prot & (1 << access_type));
11959 }
11960
11961
11962 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11963                                  MMUAccessType access_type, ARMMMUIdx mmu_idx,
11964                                  hwaddr *phys_ptr, MemTxAttrs *txattrs,
11965                                  int *prot, target_ulong *page_size,
11966                                  ARMMMUFaultInfo *fi)
11967 {
11968     uint32_t secure = regime_is_secure(env, mmu_idx);
11969     V8M_SAttributes sattrs = {};
11970     bool ret;
11971     bool mpu_is_subpage;
11972
11973     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11974         v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11975         if (access_type == MMU_INST_FETCH) {
11976             /* Instruction fetches always use the MMU bank and the
11977              * transaction attribute determined by the fetch address,
11978              * regardless of CPU state. This is painful for QEMU
11979              * to handle, because it would mean we need to encode
11980              * into the mmu_idx not just the (user, negpri) information
11981              * for the current security state but also that for the
11982              * other security state, which would balloon the number
11983              * of mmu_idx values needed alarmingly.
11984              * Fortunately we can avoid this because it's not actually
11985              * possible to arbitrarily execute code from memory with
11986              * the wrong security attribute: it will always generate
11987              * an exception of some kind or another, apart from the
11988              * special case of an NS CPU executing an SG instruction
11989              * in S&NSC memory. So we always just fail the translation
11990              * here and sort things out in the exception handler
11991              * (including possibly emulating an SG instruction).
11992              */
11993             if (sattrs.ns != !secure) {
11994                 if (sattrs.nsc) {
11995                     fi->type = ARMFault_QEMU_NSCExec;
11996                 } else {
11997                     fi->type = ARMFault_QEMU_SFault;
11998                 }
11999                 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12000                 *phys_ptr = address;
12001                 *prot = 0;
12002                 return true;
12003             }
12004         } else {
12005             /* For data accesses we always use the MMU bank indicated
12006              * by the current CPU state, but the security attributes
12007              * might downgrade a secure access to nonsecure.
12008              */
12009             if (sattrs.ns) {
12010                 txattrs->secure = false;
12011             } else if (!secure) {
12012                 /* NS access to S memory must fault.
12013                  * Architecturally we should first check whether the
12014                  * MPU information for this address indicates that we
12015                  * are doing an unaligned access to Device memory, which
12016                  * should generate a UsageFault instead. QEMU does not
12017                  * currently check for that kind of unaligned access though.
12018                  * If we added it we would need to do so as a special case
12019                  * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12020                  */
12021                 fi->type = ARMFault_QEMU_SFault;
12022                 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12023                 *phys_ptr = address;
12024                 *prot = 0;
12025                 return true;
12026             }
12027         }
12028     }
12029
12030     ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12031                             txattrs, prot, &mpu_is_subpage, fi, NULL);
12032     *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12033     return ret;
12034 }
12035
12036 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
12037                                  MMUAccessType access_type, ARMMMUIdx mmu_idx,
12038                                  hwaddr *phys_ptr, int *prot,
12039                                  ARMMMUFaultInfo *fi)
12040 {
12041     int n;
12042     uint32_t mask;
12043     uint32_t base;
12044     bool is_user = regime_is_user(env, mmu_idx);
12045
12046     if (regime_translation_disabled(env, mmu_idx)) {
12047         /* MPU disabled.  */
12048         *phys_ptr = address;
12049         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12050         return false;
12051     }
12052
12053     *phys_ptr = address;
12054     for (n = 7; n >= 0; n--) {
12055         base = env->cp15.c6_region[n];
12056         if ((base & 1) == 0) {
12057             continue;
12058         }
12059         mask = 1 << ((base >> 1) & 0x1f);
12060         /* Keep this shift separate from the above to avoid an
12061            (undefined) << 32.  */
12062         mask = (mask << 1) - 1;
12063         if (((base ^ address) & ~mask) == 0) {
12064             break;
12065         }
12066     }
12067     if (n < 0) {
12068         fi->type = ARMFault_Background;
12069         return true;
12070     }
12071
12072     if (access_type == MMU_INST_FETCH) {
12073         mask = env->cp15.pmsav5_insn_ap;
12074     } else {
12075         mask = env->cp15.pmsav5_data_ap;
12076     }
12077     mask = (mask >> (n * 4)) & 0xf;
12078     switch (mask) {
12079     case 0:
12080         fi->type = ARMFault_Permission;
12081         fi->level = 1;
12082         return true;
12083     case 1:
12084         if (is_user) {
12085             fi->type = ARMFault_Permission;
12086             fi->level = 1;
12087             return true;
12088         }
12089         *prot = PAGE_READ | PAGE_WRITE;
12090         break;
12091     case 2:
12092         *prot = PAGE_READ;
12093         if (!is_user) {
12094             *prot |= PAGE_WRITE;
12095         }
12096         break;
12097     case 3:
12098         *prot = PAGE_READ | PAGE_WRITE;
12099         break;
12100     case 5:
12101         if (is_user) {
12102             fi->type = ARMFault_Permission;
12103             fi->level = 1;
12104             return true;
12105         }
12106         *prot = PAGE_READ;
12107         break;
12108     case 6:
12109         *prot = PAGE_READ;
12110         break;
12111     default:
12112         /* Bad permission.  */
12113         fi->type = ARMFault_Permission;
12114         fi->level = 1;
12115         return true;
12116     }
12117     *prot |= PAGE_EXEC;
12118     return false;
12119 }
12120
12121 /* Combine either inner or outer cacheability attributes for normal
12122  * memory, according to table D4-42 and pseudocode procedure
12123  * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12124  *
12125  * NB: only stage 1 includes allocation hints (RW bits), leading to
12126  * some asymmetry.
12127  */
12128 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12129 {
12130     if (s1 == 4 || s2 == 4) {
12131         /* non-cacheable has precedence */
12132         return 4;
12133     } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12134         /* stage 1 write-through takes precedence */
12135         return s1;
12136     } else if (extract32(s2, 2, 2) == 2) {
12137         /* stage 2 write-through takes precedence, but the allocation hint
12138          * is still taken from stage 1
12139          */
12140         return (2 << 2) | extract32(s1, 0, 2);
12141     } else { /* write-back */
12142         return s1;
12143     }
12144 }
12145
12146 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12147  * and CombineS1S2Desc()
12148  *
12149  * @s1:      Attributes from stage 1 walk
12150  * @s2:      Attributes from stage 2 walk
12151  */
12152 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12153 {
12154     uint8_t s1lo, s2lo, s1hi, s2hi;
12155     ARMCacheAttrs ret;
12156     bool tagged = false;
12157
12158     if (s1.attrs == 0xf0) {
12159         tagged = true;
12160         s1.attrs = 0xff;
12161     }
12162
12163     s1lo = extract32(s1.attrs, 0, 4);
12164     s2lo = extract32(s2.attrs, 0, 4);
12165     s1hi = extract32(s1.attrs, 4, 4);
12166     s2hi = extract32(s2.attrs, 4, 4);
12167
12168     /* Combine shareability attributes (table D4-43) */
12169     if (s1.shareability == 2 || s2.shareability == 2) {
12170         /* if either are outer-shareable, the result is outer-shareable */
12171         ret.shareability = 2;
12172     } else if (s1.shareability == 3 || s2.shareability == 3) {
12173         /* if either are inner-shareable, the result is inner-shareable */
12174         ret.shareability = 3;
12175     } else {
12176         /* both non-shareable */
12177         ret.shareability = 0;
12178     }
12179
12180     /* Combine memory type and cacheability attributes */
12181     if (s1hi == 0 || s2hi == 0) {
12182         /* Device has precedence over normal */
12183         if (s1lo == 0 || s2lo == 0) {
12184             /* nGnRnE has precedence over anything */
12185             ret.attrs = 0;
12186         } else if (s1lo == 4 || s2lo == 4) {
12187             /* non-Reordering has precedence over Reordering */
12188             ret.attrs = 4;  /* nGnRE */
12189         } else if (s1lo == 8 || s2lo == 8) {
12190             /* non-Gathering has precedence over Gathering */
12191             ret.attrs = 8;  /* nGRE */
12192         } else {
12193             ret.attrs = 0xc; /* GRE */
12194         }
12195
12196         /* Any location for which the resultant memory type is any
12197          * type of Device memory is always treated as Outer Shareable.
12198          */
12199         ret.shareability = 2;
12200     } else { /* Normal memory */
12201         /* Outer/inner cacheability combine independently */
12202         ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12203                   | combine_cacheattr_nibble(s1lo, s2lo);
12204
12205         if (ret.attrs == 0x44) {
12206             /* Any location for which the resultant memory type is Normal
12207              * Inner Non-cacheable, Outer Non-cacheable is always treated
12208              * as Outer Shareable.
12209              */
12210             ret.shareability = 2;
12211         }
12212     }
12213
12214     /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12215     if (tagged && ret.attrs == 0xff) {
12216         ret.attrs = 0xf0;
12217     }
12218
12219     return ret;
12220 }
12221
12222
12223 /* get_phys_addr - get the physical address for this virtual address
12224  *
12225  * Find the physical address corresponding to the given virtual address,
12226  * by doing a translation table walk on MMU based systems or using the
12227  * MPU state on MPU based systems.
12228  *
12229  * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12230  * prot and page_size may not be filled in, and the populated fsr value provides
12231  * information on why the translation aborted, in the format of a
12232  * DFSR/IFSR fault register, with the following caveats:
12233  *  * we honour the short vs long DFSR format differences.
12234  *  * the WnR bit is never set (the caller must do this).
12235  *  * for PSMAv5 based systems we don't bother to return a full FSR format
12236  *    value.
12237  *
12238  * @env: CPUARMState
12239  * @address: virtual address to get physical address for
12240  * @access_type: 0 for read, 1 for write, 2 for execute
12241  * @mmu_idx: MMU index indicating required translation regime
12242  * @phys_ptr: set to the physical address corresponding to the virtual address
12243  * @attrs: set to the memory transaction attributes to use
12244  * @prot: set to the permissions for the page containing phys_ptr
12245  * @page_size: set to the size of the page containing phys_ptr
12246  * @fi: set to fault info if the translation fails
12247  * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
12248  */
12249 bool get_phys_addr(CPUARMState *env, target_ulong address,
12250                    MMUAccessType access_type, ARMMMUIdx mmu_idx,
12251                    hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12252                    target_ulong *page_size,
12253                    ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
12254 {
12255     ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
12256
12257     if (mmu_idx != s1_mmu_idx) {
12258         /* Call ourselves recursively to do the stage 1 and then stage 2
12259          * translations if mmu_idx is a two-stage regime.
12260          */
12261         if (arm_feature(env, ARM_FEATURE_EL2)) {
12262             hwaddr ipa;
12263             int s2_prot;
12264             int ret;
12265             ARMCacheAttrs cacheattrs2 = {};
12266             ARMMMUIdx s2_mmu_idx;
12267             bool is_el0;
12268
12269             ret = get_phys_addr(env, address, access_type, s1_mmu_idx, &ipa,
12270                                 attrs, prot, page_size, fi, cacheattrs);
12271
12272             /* If S1 fails or S2 is disabled, return early.  */
12273             if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
12274                 *phys_ptr = ipa;
12275                 return ret;
12276             }
12277
12278             s2_mmu_idx = attrs->secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
12279             is_el0 = mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_SE10_0;
12280
12281             /* S1 is done. Now do S2 translation.  */
12282             ret = get_phys_addr_lpae(env, ipa, access_type, s2_mmu_idx, is_el0,
12283                                      phys_ptr, attrs, &s2_prot,
12284                                      page_size, fi, &cacheattrs2);
12285             fi->s2addr = ipa;
12286             /* Combine the S1 and S2 perms.  */
12287             *prot &= s2_prot;
12288
12289             /* If S2 fails, return early.  */
12290             if (ret) {
12291                 return ret;
12292             }
12293
12294             /* Combine the S1 and S2 cache attributes. */
12295             if (arm_hcr_el2_eff(env) & HCR_DC) {
12296                 /*
12297                  * HCR.DC forces the first stage attributes to
12298                  *  Normal Non-Shareable,
12299                  *  Inner Write-Back Read-Allocate Write-Allocate,
12300                  *  Outer Write-Back Read-Allocate Write-Allocate.
12301                  * Do not overwrite Tagged within attrs.
12302                  */
12303                 if (cacheattrs->attrs != 0xf0) {
12304                     cacheattrs->attrs = 0xff;
12305                 }
12306                 cacheattrs->shareability = 0;
12307             }
12308             *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
12309
12310             /* Check if IPA translates to secure or non-secure PA space. */
12311             if (arm_is_secure_below_el3(env)) {
12312                 if (attrs->secure) {
12313                     attrs->secure =
12314                         !(env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW));
12315                 } else {
12316                     attrs->secure =
12317                         !((env->cp15.vtcr_el2.raw_tcr & (VTCR_NSA | VTCR_NSW))
12318                         || (env->cp15.vstcr_el2.raw_tcr & VSTCR_SA));
12319                 }
12320             }
12321             return 0;
12322         } else {
12323             /*
12324              * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12325              */
12326             mmu_idx = stage_1_mmu_idx(mmu_idx);
12327         }
12328     }
12329
12330     /* The page table entries may downgrade secure to non-secure, but
12331      * cannot upgrade an non-secure translation regime's attributes
12332      * to secure.
12333      */
12334     attrs->secure = regime_is_secure(env, mmu_idx);
12335     attrs->user = regime_is_user(env, mmu_idx);
12336
12337     /* Fast Context Switch Extension. This doesn't exist at all in v8.
12338      * In v7 and earlier it affects all stage 1 translations.
12339      */
12340     if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
12341         && !arm_feature(env, ARM_FEATURE_V8)) {
12342         if (regime_el(env, mmu_idx) == 3) {
12343             address += env->cp15.fcseidr_s;
12344         } else {
12345             address += env->cp15.fcseidr_ns;
12346         }
12347     }
12348
12349     if (arm_feature(env, ARM_FEATURE_PMSA)) {
12350         bool ret;
12351         *page_size = TARGET_PAGE_SIZE;
12352
12353         if (arm_feature(env, ARM_FEATURE_V8)) {
12354             /* PMSAv8 */
12355             ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
12356                                        phys_ptr, attrs, prot, page_size, fi);
12357         } else if (arm_feature(env, ARM_FEATURE_V7)) {
12358             /* PMSAv7 */
12359             ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
12360                                        phys_ptr, prot, page_size, fi);
12361         } else {
12362             /* Pre-v7 MPU */
12363             ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
12364                                        phys_ptr, prot, fi);
12365         }
12366         qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
12367                       " mmu_idx %u -> %s (prot %c%c%c)\n",
12368                       access_type == MMU_DATA_LOAD ? "reading" :
12369                       (access_type == MMU_DATA_STORE ? "writing" : "execute"),
12370                       (uint32_t)address, mmu_idx,
12371                       ret ? "Miss" : "Hit",
12372                       *prot & PAGE_READ ? 'r' : '-',
12373                       *prot & PAGE_WRITE ? 'w' : '-',
12374                       *prot & PAGE_EXEC ? 'x' : '-');
12375
12376         return ret;
12377     }
12378
12379     /* Definitely a real MMU, not an MPU */
12380
12381     if (regime_translation_disabled(env, mmu_idx)) {
12382         uint64_t hcr;
12383         uint8_t memattr;
12384
12385         /*
12386          * MMU disabled.  S1 addresses within aa64 translation regimes are
12387          * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12388          */
12389         if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
12390             int r_el = regime_el(env, mmu_idx);
12391             if (arm_el_is_aa64(env, r_el)) {
12392                 int pamax = arm_pamax(env_archcpu(env));
12393                 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12394                 int addrtop, tbi;
12395
12396                 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12397                 if (access_type == MMU_INST_FETCH) {
12398                     tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12399                 }
12400                 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12401                 addrtop = (tbi ? 55 : 63);
12402
12403                 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12404                     fi->type = ARMFault_AddressSize;
12405                     fi->level = 0;
12406                     fi->stage2 = false;
12407                     return 1;
12408                 }
12409
12410                 /*
12411                  * When TBI is disabled, we've just validated that all of the
12412                  * bits above PAMax are zero, so logically we only need to
12413                  * clear the top byte for TBI.  But it's clearer to follow
12414                  * the pseudocode set of addrdesc.paddress.
12415                  */
12416                 address = extract64(address, 0, 52);
12417             }
12418         }
12419         *phys_ptr = address;
12420         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12421         *page_size = TARGET_PAGE_SIZE;
12422
12423         /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12424         hcr = arm_hcr_el2_eff(env);
12425         cacheattrs->shareability = 0;
12426         if (hcr & HCR_DC) {
12427             if (hcr & HCR_DCT) {
12428                 memattr = 0xf0;  /* Tagged, Normal, WB, RWA */
12429             } else {
12430                 memattr = 0xff;  /* Normal, WB, RWA */
12431             }
12432         } else if (access_type == MMU_INST_FETCH) {
12433             if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12434                 memattr = 0xee;  /* Normal, WT, RA, NT */
12435             } else {
12436                 memattr = 0x44;  /* Normal, NC, No */
12437             }
12438             cacheattrs->shareability = 2; /* outer sharable */
12439         } else {
12440             memattr = 0x00;      /* Device, nGnRnE */
12441         }
12442         cacheattrs->attrs = memattr;
12443         return 0;
12444     }
12445
12446     if (regime_using_lpae_format(env, mmu_idx)) {
12447         return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
12448                                   phys_ptr, attrs, prot, page_size,
12449                                   fi, cacheattrs);
12450     } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
12451         return get_phys_addr_v6(env, address, access_type, mmu_idx,
12452                                 phys_ptr, attrs, prot, page_size, fi);
12453     } else {
12454         return get_phys_addr_v5(env, address, access_type, mmu_idx,
12455                                     phys_ptr, prot, page_size, fi);
12456     }
12457 }
12458
12459 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12460                                          MemTxAttrs *attrs)
12461 {
12462     ARMCPU *cpu = ARM_CPU(cs);
12463     CPUARMState *env = &cpu->env;
12464     hwaddr phys_addr;
12465     target_ulong page_size;
12466     int prot;
12467     bool ret;
12468     ARMMMUFaultInfo fi = {};
12469     ARMMMUIdx mmu_idx = arm_mmu_idx(env);
12470     ARMCacheAttrs cacheattrs = {};
12471
12472     *attrs = (MemTxAttrs) {};
12473
12474     ret = get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &phys_addr,
12475                         attrs, &prot, &page_size, &fi, &cacheattrs);
12476
12477     if (ret) {
12478         return -1;
12479     }
12480     return phys_addr;
12481 }
12482
12483 #endif
12484
12485 /* Note that signed overflow is undefined in C.  The following routines are
12486    careful to use unsigned types where modulo arithmetic is required.
12487    Failure to do so _will_ break on newer gcc.  */
12488
12489 /* Signed saturating arithmetic.  */
12490
12491 /* Perform 16-bit signed saturating addition.  */
12492 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12493 {
12494     uint16_t res;
12495
12496     res = a + b;
12497     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12498         if (a & 0x8000)
12499             res = 0x8000;
12500         else
12501             res = 0x7fff;
12502     }
12503     return res;
12504 }
12505
12506 /* Perform 8-bit signed saturating addition.  */
12507 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12508 {
12509     uint8_t res;
12510
12511     res = a + b;
12512     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12513         if (a & 0x80)
12514             res = 0x80;
12515         else
12516             res = 0x7f;
12517     }
12518     return res;
12519 }
12520
12521 /* Perform 16-bit signed saturating subtraction.  */
12522 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12523 {
12524     uint16_t res;
12525
12526     res = a - b;
12527     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12528         if (a & 0x8000)
12529             res = 0x8000;
12530         else
12531             res = 0x7fff;
12532     }
12533     return res;
12534 }
12535
12536 /* Perform 8-bit signed saturating subtraction.  */
12537 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12538 {
12539     uint8_t res;
12540
12541     res = a - b;
12542     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12543         if (a & 0x80)
12544             res = 0x80;
12545         else
12546             res = 0x7f;
12547     }
12548     return res;
12549 }
12550
12551 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12552 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12553 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
12554 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
12555 #define PFX q
12556
12557 #include "op_addsub.h"
12558
12559 /* Unsigned saturating arithmetic.  */
12560 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12561 {
12562     uint16_t res;
12563     res = a + b;
12564     if (res < a)
12565         res = 0xffff;
12566     return res;
12567 }
12568
12569 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12570 {
12571     if (a > b)
12572         return a - b;
12573     else
12574         return 0;
12575 }
12576
12577 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12578 {
12579     uint8_t res;
12580     res = a + b;
12581     if (res < a)
12582         res = 0xff;
12583     return res;
12584 }
12585
12586 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12587 {
12588     if (a > b)
12589         return a - b;
12590     else
12591         return 0;
12592 }
12593
12594 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12595 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12596 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
12597 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
12598 #define PFX uq
12599
12600 #include "op_addsub.h"
12601
12602 /* Signed modulo arithmetic.  */
12603 #define SARITH16(a, b, n, op) do { \
12604     int32_t sum; \
12605     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12606     RESULT(sum, n, 16); \
12607     if (sum >= 0) \
12608         ge |= 3 << (n * 2); \
12609     } while(0)
12610
12611 #define SARITH8(a, b, n, op) do { \
12612     int32_t sum; \
12613     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12614     RESULT(sum, n, 8); \
12615     if (sum >= 0) \
12616         ge |= 1 << n; \
12617     } while(0)
12618
12619
12620 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12621 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12622 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
12623 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
12624 #define PFX s
12625 #define ARITH_GE
12626
12627 #include "op_addsub.h"
12628
12629 /* Unsigned modulo arithmetic.  */
12630 #define ADD16(a, b, n) do { \
12631     uint32_t sum; \
12632     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12633     RESULT(sum, n, 16); \
12634     if ((sum >> 16) == 1) \
12635         ge |= 3 << (n * 2); \
12636     } while(0)
12637
12638 #define ADD8(a, b, n) do { \
12639     uint32_t sum; \
12640     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12641     RESULT(sum, n, 8); \
12642     if ((sum >> 8) == 1) \
12643         ge |= 1 << n; \
12644     } while(0)
12645
12646 #define SUB16(a, b, n) do { \
12647     uint32_t sum; \
12648     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12649     RESULT(sum, n, 16); \
12650     if ((sum >> 16) == 0) \
12651         ge |= 3 << (n * 2); \
12652     } while(0)
12653
12654 #define SUB8(a, b, n) do { \
12655     uint32_t sum; \
12656     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12657     RESULT(sum, n, 8); \
12658     if ((sum >> 8) == 0) \
12659         ge |= 1 << n; \
12660     } while(0)
12661
12662 #define PFX u
12663 #define ARITH_GE
12664
12665 #include "op_addsub.h"
12666
12667 /* Halved signed arithmetic.  */
12668 #define ADD16(a, b, n) \
12669   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12670 #define SUB16(a, b, n) \
12671   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12672 #define ADD8(a, b, n) \
12673   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12674 #define SUB8(a, b, n) \
12675   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12676 #define PFX sh
12677
12678 #include "op_addsub.h"
12679
12680 /* Halved unsigned arithmetic.  */
12681 #define ADD16(a, b, n) \
12682   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12683 #define SUB16(a, b, n) \
12684   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12685 #define ADD8(a, b, n) \
12686   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12687 #define SUB8(a, b, n) \
12688   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12689 #define PFX uh
12690
12691 #include "op_addsub.h"
12692
12693 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12694 {
12695     if (a > b)
12696         return a - b;
12697     else
12698         return b - a;
12699 }
12700
12701 /* Unsigned sum of absolute byte differences.  */
12702 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12703 {
12704     uint32_t sum;
12705     sum = do_usad(a, b);
12706     sum += do_usad(a >> 8, b >> 8);
12707     sum += do_usad(a >> 16, b >> 16);
12708     sum += do_usad(a >> 24, b >> 24);
12709     return sum;
12710 }
12711
12712 /* For ARMv6 SEL instruction.  */
12713 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12714 {
12715     uint32_t mask;
12716
12717     mask = 0;
12718     if (flags & 1)
12719         mask |= 0xff;
12720     if (flags & 2)
12721         mask |= 0xff00;
12722     if (flags & 4)
12723         mask |= 0xff0000;
12724     if (flags & 8)
12725         mask |= 0xff000000;
12726     return (a & mask) | (b & ~mask);
12727 }
12728
12729 /* CRC helpers.
12730  * The upper bytes of val (above the number specified by 'bytes') must have
12731  * been zeroed out by the caller.
12732  */
12733 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12734 {
12735     uint8_t buf[4];
12736
12737     stl_le_p(buf, val);
12738
12739     /* zlib crc32 converts the accumulator and output to one's complement.  */
12740     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12741 }
12742
12743 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12744 {
12745     uint8_t buf[4];
12746
12747     stl_le_p(buf, val);
12748
12749     /* Linux crc32c converts the output to one's complement.  */
12750     return crc32c(acc, buf, bytes) ^ 0xffffffff;
12751 }
12752
12753 /* Return the exception level to which FP-disabled exceptions should
12754  * be taken, or 0 if FP is enabled.
12755  */
12756 int fp_exception_el(CPUARMState *env, int cur_el)
12757 {
12758 #ifndef CONFIG_USER_ONLY
12759     /* CPACR and the CPTR registers don't exist before v6, so FP is
12760      * always accessible
12761      */
12762     if (!arm_feature(env, ARM_FEATURE_V6)) {
12763         return 0;
12764     }
12765
12766     if (arm_feature(env, ARM_FEATURE_M)) {
12767         /* CPACR can cause a NOCP UsageFault taken to current security state */
12768         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12769             return 1;
12770         }
12771
12772         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12773             if (!extract32(env->v7m.nsacr, 10, 1)) {
12774                 /* FP insns cause a NOCP UsageFault taken to Secure */
12775                 return 3;
12776             }
12777         }
12778
12779         return 0;
12780     }
12781
12782     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12783      * 0, 2 : trap EL0 and EL1/PL1 accesses
12784      * 1    : trap only EL0 accesses
12785      * 3    : trap no accesses
12786      * This register is ignored if E2H+TGE are both set.
12787      */
12788     if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12789         int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12790
12791         switch (fpen) {
12792         case 0:
12793         case 2:
12794             if (cur_el == 0 || cur_el == 1) {
12795                 /* Trap to PL1, which might be EL1 or EL3 */
12796                 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12797                     return 3;
12798                 }
12799                 return 1;
12800             }
12801             if (cur_el == 3 && !is_a64(env)) {
12802                 /* Secure PL1 running at EL3 */
12803                 return 3;
12804             }
12805             break;
12806         case 1:
12807             if (cur_el == 0) {
12808                 return 1;
12809             }
12810             break;
12811         case 3:
12812             break;
12813         }
12814     }
12815
12816     /*
12817      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12818      * to control non-secure access to the FPU. It doesn't have any
12819      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12820      */
12821     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12822          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12823         if (!extract32(env->cp15.nsacr, 10, 1)) {
12824             /* FP insns act as UNDEF */
12825             return cur_el == 2 ? 2 : 1;
12826         }
12827     }
12828
12829     /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12830      * check because zero bits in the registers mean "don't trap".
12831      */
12832
12833     /* CPTR_EL2 : present in v7VE or v8 */
12834     if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12835         && arm_is_el2_enabled(env)) {
12836         /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12837         return 2;
12838     }
12839
12840     /* CPTR_EL3 : present in v8 */
12841     if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12842         /* Trap all FP ops to EL3 */
12843         return 3;
12844     }
12845 #endif
12846     return 0;
12847 }
12848
12849 /* Return the exception level we're running at if this is our mmu_idx */
12850 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12851 {
12852     if (mmu_idx & ARM_MMU_IDX_M) {
12853         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12854     }
12855
12856     switch (mmu_idx) {
12857     case ARMMMUIdx_E10_0:
12858     case ARMMMUIdx_E20_0:
12859     case ARMMMUIdx_SE10_0:
12860     case ARMMMUIdx_SE20_0:
12861         return 0;
12862     case ARMMMUIdx_E10_1:
12863     case ARMMMUIdx_E10_1_PAN:
12864     case ARMMMUIdx_SE10_1:
12865     case ARMMMUIdx_SE10_1_PAN:
12866         return 1;
12867     case ARMMMUIdx_E2:
12868     case ARMMMUIdx_E20_2:
12869     case ARMMMUIdx_E20_2_PAN:
12870     case ARMMMUIdx_SE2:
12871     case ARMMMUIdx_SE20_2:
12872     case ARMMMUIdx_SE20_2_PAN:
12873         return 2;
12874     case ARMMMUIdx_SE3:
12875         return 3;
12876     default:
12877         g_assert_not_reached();
12878     }
12879 }
12880
12881 #ifndef CONFIG_TCG
12882 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12883 {
12884     g_assert_not_reached();
12885 }
12886 #endif
12887
12888 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12889 {
12890     ARMMMUIdx idx;
12891     uint64_t hcr;
12892
12893     if (arm_feature(env, ARM_FEATURE_M)) {
12894         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12895     }
12896
12897     /* See ARM pseudo-function ELIsInHost.  */
12898     switch (el) {
12899     case 0:
12900         hcr = arm_hcr_el2_eff(env);
12901         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12902             idx = ARMMMUIdx_E20_0;
12903         } else {
12904             idx = ARMMMUIdx_E10_0;
12905         }
12906         break;
12907     case 1:
12908         if (env->pstate & PSTATE_PAN) {
12909             idx = ARMMMUIdx_E10_1_PAN;
12910         } else {
12911             idx = ARMMMUIdx_E10_1;
12912         }
12913         break;
12914     case 2:
12915         /* Note that TGE does not apply at EL2.  */
12916         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12917             if (env->pstate & PSTATE_PAN) {
12918                 idx = ARMMMUIdx_E20_2_PAN;
12919             } else {
12920                 idx = ARMMMUIdx_E20_2;
12921             }
12922         } else {
12923             idx = ARMMMUIdx_E2;
12924         }
12925         break;
12926     case 3:
12927         return ARMMMUIdx_SE3;
12928     default:
12929         g_assert_not_reached();
12930     }
12931
12932     if (arm_is_secure_below_el3(env)) {
12933         idx &= ~ARM_MMU_IDX_A_NS;
12934     }
12935
12936     return idx;
12937 }
12938
12939 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12940 {
12941     return arm_mmu_idx_el(env, arm_current_el(env));
12942 }
12943
12944 #ifndef CONFIG_USER_ONLY
12945 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12946 {
12947     return stage_1_mmu_idx(arm_mmu_idx(env));
12948 }
12949 #endif
12950
12951 static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12952                                       ARMMMUIdx mmu_idx, uint32_t flags)
12953 {
12954     flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12955     flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12956                        arm_to_core_mmu_idx(mmu_idx));
12957
12958     if (arm_singlestep_active(env)) {
12959         flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12960     }
12961     return flags;
12962 }
12963
12964 static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12965                                          ARMMMUIdx mmu_idx, uint32_t flags)
12966 {
12967     bool sctlr_b = arm_sctlr_b(env);
12968
12969     if (sctlr_b) {
12970         flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12971     }
12972     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12973         flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12974     }
12975     flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12976
12977     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12978 }
12979
12980 static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12981                                    ARMMMUIdx mmu_idx)
12982 {
12983     uint32_t flags = 0;
12984
12985     if (arm_v7m_is_handler_mode(env)) {
12986         flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
12987     }
12988
12989     /*
12990      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12991      * is suppressing them because the requested execution priority
12992      * is less than 0.
12993      */
12994     if (arm_feature(env, ARM_FEATURE_V8) &&
12995         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12996           (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
12997         flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
12998     }
12999
13000     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13001 }
13002
13003 static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
13004 {
13005     int flags = 0;
13006
13007     flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
13008                        arm_debug_target_el(env));
13009     return flags;
13010 }
13011
13012 static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
13013                                    ARMMMUIdx mmu_idx)
13014 {
13015     uint32_t flags = rebuild_hflags_aprofile(env);
13016
13017     if (arm_el_is_aa64(env, 1)) {
13018         flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
13019     }
13020
13021     if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
13022         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
13023         flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
13024     }
13025
13026     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13027 }
13028
13029 static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
13030                                    ARMMMUIdx mmu_idx)
13031 {
13032     uint32_t flags = rebuild_hflags_aprofile(env);
13033     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
13034     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
13035     uint64_t sctlr;
13036     int tbii, tbid;
13037
13038     flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
13039
13040     /* Get control bits for tagged addresses.  */
13041     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
13042     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
13043
13044     flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
13045     flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
13046
13047     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
13048         int sve_el = sve_exception_el(env, el);
13049         uint32_t zcr_len;
13050
13051         /*
13052          * If SVE is disabled, but FP is enabled,
13053          * then the effective len is 0.
13054          */
13055         if (sve_el != 0 && fp_el == 0) {
13056             zcr_len = 0;
13057         } else {
13058             zcr_len = sve_zcr_len_for_el(env, el);
13059         }
13060         flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
13061         flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
13062     }
13063
13064     sctlr = regime_sctlr(env, stage1);
13065
13066     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
13067         flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
13068     }
13069
13070     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
13071         /*
13072          * In order to save space in flags, we record only whether
13073          * pauth is "inactive", meaning all insns are implemented as
13074          * a nop, or "active" when some action must be performed.
13075          * The decision of which action to take is left to a helper.
13076          */
13077         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
13078             flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
13079         }
13080     }
13081
13082     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13083         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
13084         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
13085             flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
13086         }
13087     }
13088
13089     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
13090     if (!(env->pstate & PSTATE_UAO)) {
13091         switch (mmu_idx) {
13092         case ARMMMUIdx_E10_1:
13093         case ARMMMUIdx_E10_1_PAN:
13094         case ARMMMUIdx_SE10_1:
13095         case ARMMMUIdx_SE10_1_PAN:
13096             /* TODO: ARMv8.3-NV */
13097             flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
13098             break;
13099         case ARMMMUIdx_E20_2:
13100         case ARMMMUIdx_E20_2_PAN:
13101         case ARMMMUIdx_SE20_2:
13102         case ARMMMUIdx_SE20_2_PAN:
13103             /*
13104              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
13105              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
13106              */
13107             if (env->cp15.hcr_el2 & HCR_TGE) {
13108                 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
13109             }
13110             break;
13111         default:
13112             break;
13113         }
13114     }
13115
13116     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13117         /*
13118          * Set MTE_ACTIVE if any access may be Checked, and leave clear
13119          * if all accesses must be Unchecked:
13120          * 1) If no TBI, then there are no tags in the address to check,
13121          * 2) If Tag Check Override, then all accesses are Unchecked,
13122          * 3) If Tag Check Fail == 0, then Checked access have no effect,
13123          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13124          */
13125         if (allocation_tag_access_enabled(env, el, sctlr)) {
13126             flags = FIELD_DP32(flags, TBFLAG_A64, ATA, 1);
13127             if (tbid
13128                 && !(env->pstate & PSTATE_TCO)
13129                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
13130                 flags = FIELD_DP32(flags, TBFLAG_A64, MTE_ACTIVE, 1);
13131             }
13132         }
13133         /* And again for unprivileged accesses, if required.  */
13134         if (FIELD_EX32(flags, TBFLAG_A64, UNPRIV)
13135             && tbid
13136             && !(env->pstate & PSTATE_TCO)
13137             && (sctlr & SCTLR_TCF)
13138             && allocation_tag_access_enabled(env, 0, sctlr)) {
13139             flags = FIELD_DP32(flags, TBFLAG_A64, MTE0_ACTIVE, 1);
13140         }
13141         /* Cache TCMA as well as TBI. */
13142         flags = FIELD_DP32(flags, TBFLAG_A64, TCMA,
13143                            aa64_va_parameter_tcma(tcr, mmu_idx));
13144     }
13145
13146     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13147 }
13148
13149 static uint32_t rebuild_hflags_internal(CPUARMState *env)
13150 {
13151     int el = arm_current_el(env);
13152     int fp_el = fp_exception_el(env, el);
13153     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13154
13155     if (is_a64(env)) {
13156         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13157     } else if (arm_feature(env, ARM_FEATURE_M)) {
13158         return rebuild_hflags_m32(env, fp_el, mmu_idx);
13159     } else {
13160         return rebuild_hflags_a32(env, fp_el, mmu_idx);
13161     }
13162 }
13163
13164 void arm_rebuild_hflags(CPUARMState *env)
13165 {
13166     env->hflags = rebuild_hflags_internal(env);
13167 }
13168
13169 /*
13170  * If we have triggered a EL state change we can't rely on the
13171  * translator having passed it to us, we need to recompute.
13172  */
13173 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13174 {
13175     int el = arm_current_el(env);
13176     int fp_el = fp_exception_el(env, el);
13177     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13178     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13179 }
13180
13181 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13182 {
13183     int fp_el = fp_exception_el(env, el);
13184     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13185
13186     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13187 }
13188
13189 /*
13190  * If we have triggered a EL state change we can't rely on the
13191  * translator having passed it to us, we need to recompute.
13192  */
13193 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13194 {
13195     int el = arm_current_el(env);
13196     int fp_el = fp_exception_el(env, el);
13197     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13198     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13199 }
13200
13201 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13202 {
13203     int fp_el = fp_exception_el(env, el);
13204     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13205
13206     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13207 }
13208
13209 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13210 {
13211     int fp_el = fp_exception_el(env, el);
13212     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13213
13214     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13215 }
13216
13217 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13218 {
13219 #ifdef CONFIG_DEBUG_TCG
13220     uint32_t env_flags_current = env->hflags;
13221     uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
13222
13223     if (unlikely(env_flags_current != env_flags_rebuilt)) {
13224         fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
13225                 env_flags_current, env_flags_rebuilt);
13226         abort();
13227     }
13228 #endif
13229 }
13230
13231 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13232                           target_ulong *cs_base, uint32_t *pflags)
13233 {
13234     uint32_t flags = env->hflags;
13235
13236     *cs_base = 0;
13237     assert_hflags_rebuild_correctly(env);
13238
13239     if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
13240         *pc = env->pc;
13241         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13242             flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
13243         }
13244     } else {
13245         *pc = env->regs[15];
13246
13247         if (arm_feature(env, ARM_FEATURE_M)) {
13248             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13249                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13250                 != env->v7m.secure) {
13251                 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
13252             }
13253
13254             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13255                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13256                  (env->v7m.secure &&
13257                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13258                 /*
13259                  * ASPEN is set, but FPCA/SFPA indicate that there is no
13260                  * active FP context; we must create a new FP context before
13261                  * executing any FP insn.
13262                  */
13263                 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
13264             }
13265
13266             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13267             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
13268                 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
13269             }
13270         } else {
13271             /*
13272              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13273              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13274              */
13275             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
13276                 flags = FIELD_DP32(flags, TBFLAG_A32,
13277                                    XSCALE_CPAR, env->cp15.c15_cpar);
13278             } else {
13279                 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
13280                                    env->vfp.vec_len);
13281                 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
13282                                    env->vfp.vec_stride);
13283             }
13284             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
13285                 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
13286             }
13287         }
13288
13289         flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
13290         flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
13291     }
13292
13293     /*
13294      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
13295      * states defined in the ARM ARM for software singlestep:
13296      *  SS_ACTIVE   PSTATE.SS   State
13297      *     0            x       Inactive (the TB flag for SS is always 0)
13298      *     1            0       Active-pending
13299      *     1            1       Active-not-pending
13300      * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
13301      */
13302     if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
13303         (env->pstate & PSTATE_SS)) {
13304         flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
13305     }
13306
13307     *pflags = flags;
13308 }
13309
13310 #ifdef TARGET_AARCH64
13311 /*
13312  * The manual says that when SVE is enabled and VQ is widened the
13313  * implementation is allowed to zero the previously inaccessible
13314  * portion of the registers.  The corollary to that is that when
13315  * SVE is enabled and VQ is narrowed we are also allowed to zero
13316  * the now inaccessible portion of the registers.
13317  *
13318  * The intent of this is that no predicate bit beyond VQ is ever set.
13319  * Which means that some operations on predicate registers themselves
13320  * may operate on full uint64_t or even unrolled across the maximum
13321  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
13322  * may well be cheaper than conditionals to restrict the operation
13323  * to the relevant portion of a uint16_t[16].
13324  */
13325 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13326 {
13327     int i, j;
13328     uint64_t pmask;
13329
13330     assert(vq >= 1 && vq <= ARM_MAX_VQ);
13331     assert(vq <= env_archcpu(env)->sve_max_vq);
13332
13333     /* Zap the high bits of the zregs.  */
13334     for (i = 0; i < 32; i++) {
13335         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13336     }
13337
13338     /* Zap the high bits of the pregs and ffr.  */
13339     pmask = 0;
13340     if (vq & 3) {
13341         pmask = ~(-1ULL << (16 * (vq & 3)));
13342     }
13343     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13344         for (i = 0; i < 17; ++i) {
13345             env->vfp.pregs[i].p[j] &= pmask;
13346         }
13347         pmask = 0;
13348     }
13349 }
13350
13351 /*
13352  * Notice a change in SVE vector size when changing EL.
13353  */
13354 void aarch64_sve_change_el(CPUARMState *env, int old_el,
13355                            int new_el, bool el0_a64)
13356 {
13357     ARMCPU *cpu = env_archcpu(env);
13358     int old_len, new_len;
13359     bool old_a64, new_a64;
13360
13361     /* Nothing to do if no SVE.  */
13362     if (!cpu_isar_feature(aa64_sve, cpu)) {
13363         return;
13364     }
13365
13366     /* Nothing to do if FP is disabled in either EL.  */
13367     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13368         return;
13369     }
13370
13371     /*
13372      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13373      * at ELx, or not available because the EL is in AArch32 state, then
13374      * for all purposes other than a direct read, the ZCR_ELx.LEN field
13375      * has an effective value of 0".
13376      *
13377      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13378      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13379      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
13380      * we already have the correct register contents when encountering the
13381      * vq0->vq0 transition between EL0->EL1.
13382      */
13383     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13384     old_len = (old_a64 && !sve_exception_el(env, old_el)
13385                ? sve_zcr_len_for_el(env, old_el) : 0);
13386     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13387     new_len = (new_a64 && !sve_exception_el(env, new_el)
13388                ? sve_zcr_len_for_el(env, new_el) : 0);
13389
13390     /* When changing vector length, clear inaccessible state.  */
13391     if (new_len < old_len) {
13392         aarch64_sve_narrow_vq(env, new_len + 1);
13393     }
13394 }
13395 #endif