]> Git Repo - qemu.git/blob - target-arm/helper.c
target-arm: Add VMPIDR_EL2
[qemu.git] / target-arm / helper.c
1 #include "cpu.h"
2 #include "internals.h"
3 #include "exec/gdbstub.h"
4 #include "exec/helper-proto.h"
5 #include "qemu/host-utils.h"
6 #include "sysemu/arch_init.h"
7 #include "sysemu/sysemu.h"
8 #include "qemu/bitops.h"
9 #include "qemu/crc32c.h"
10 #include "exec/cpu_ldst.h"
11 #include "arm_ldst.h"
12 #include <zlib.h> /* For crc32 */
13 #include "exec/semihost.h"
14
15 #ifndef CONFIG_USER_ONLY
16 static inline bool get_phys_addr(CPUARMState *env, target_ulong address,
17                                  int access_type, ARMMMUIdx mmu_idx,
18                                  hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
19                                  target_ulong *page_size, uint32_t *fsr);
20
21 /* Definitions for the PMCCNTR and PMCR registers */
22 #define PMCRD   0x8
23 #define PMCRC   0x4
24 #define PMCRE   0x1
25 #endif
26
27 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
28 {
29     int nregs;
30
31     /* VFP data registers are always little-endian.  */
32     nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
33     if (reg < nregs) {
34         stfq_le_p(buf, env->vfp.regs[reg]);
35         return 8;
36     }
37     if (arm_feature(env, ARM_FEATURE_NEON)) {
38         /* Aliases for Q regs.  */
39         nregs += 16;
40         if (reg < nregs) {
41             stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
42             stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
43             return 16;
44         }
45     }
46     switch (reg - nregs) {
47     case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
48     case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
49     case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
50     }
51     return 0;
52 }
53
54 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
55 {
56     int nregs;
57
58     nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
59     if (reg < nregs) {
60         env->vfp.regs[reg] = ldfq_le_p(buf);
61         return 8;
62     }
63     if (arm_feature(env, ARM_FEATURE_NEON)) {
64         nregs += 16;
65         if (reg < nregs) {
66             env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
67             env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
68             return 16;
69         }
70     }
71     switch (reg - nregs) {
72     case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
73     case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
74     case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
75     }
76     return 0;
77 }
78
79 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
80 {
81     switch (reg) {
82     case 0 ... 31:
83         /* 128 bit FP register */
84         stfq_le_p(buf, env->vfp.regs[reg * 2]);
85         stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
86         return 16;
87     case 32:
88         /* FPSR */
89         stl_p(buf, vfp_get_fpsr(env));
90         return 4;
91     case 33:
92         /* FPCR */
93         stl_p(buf, vfp_get_fpcr(env));
94         return 4;
95     default:
96         return 0;
97     }
98 }
99
100 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
101 {
102     switch (reg) {
103     case 0 ... 31:
104         /* 128 bit FP register */
105         env->vfp.regs[reg * 2] = ldfq_le_p(buf);
106         env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
107         return 16;
108     case 32:
109         /* FPSR */
110         vfp_set_fpsr(env, ldl_p(buf));
111         return 4;
112     case 33:
113         /* FPCR */
114         vfp_set_fpcr(env, ldl_p(buf));
115         return 4;
116     default:
117         return 0;
118     }
119 }
120
121 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
122 {
123     assert(ri->fieldoffset);
124     if (cpreg_field_is_64bit(ri)) {
125         return CPREG_FIELD64(env, ri);
126     } else {
127         return CPREG_FIELD32(env, ri);
128     }
129 }
130
131 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
132                       uint64_t value)
133 {
134     assert(ri->fieldoffset);
135     if (cpreg_field_is_64bit(ri)) {
136         CPREG_FIELD64(env, ri) = value;
137     } else {
138         CPREG_FIELD32(env, ri) = value;
139     }
140 }
141
142 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
143 {
144     return (char *)env + ri->fieldoffset;
145 }
146
147 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
148 {
149     /* Raw read of a coprocessor register (as needed for migration, etc). */
150     if (ri->type & ARM_CP_CONST) {
151         return ri->resetvalue;
152     } else if (ri->raw_readfn) {
153         return ri->raw_readfn(env, ri);
154     } else if (ri->readfn) {
155         return ri->readfn(env, ri);
156     } else {
157         return raw_read(env, ri);
158     }
159 }
160
161 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
162                              uint64_t v)
163 {
164     /* Raw write of a coprocessor register (as needed for migration, etc).
165      * Note that constant registers are treated as write-ignored; the
166      * caller should check for success by whether a readback gives the
167      * value written.
168      */
169     if (ri->type & ARM_CP_CONST) {
170         return;
171     } else if (ri->raw_writefn) {
172         ri->raw_writefn(env, ri, v);
173     } else if (ri->writefn) {
174         ri->writefn(env, ri, v);
175     } else {
176         raw_write(env, ri, v);
177     }
178 }
179
180 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
181 {
182    /* Return true if the regdef would cause an assertion if you called
183     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
184     * program bug for it not to have the NO_RAW flag).
185     * NB that returning false here doesn't necessarily mean that calling
186     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
187     * read/write access functions which are safe for raw use" from "has
188     * read/write access functions which have side effects but has forgotten
189     * to provide raw access functions".
190     * The tests here line up with the conditions in read/write_raw_cp_reg()
191     * and assertions in raw_read()/raw_write().
192     */
193     if ((ri->type & ARM_CP_CONST) ||
194         ri->fieldoffset ||
195         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
196         return false;
197     }
198     return true;
199 }
200
201 bool write_cpustate_to_list(ARMCPU *cpu)
202 {
203     /* Write the coprocessor state from cpu->env to the (index,value) list. */
204     int i;
205     bool ok = true;
206
207     for (i = 0; i < cpu->cpreg_array_len; i++) {
208         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
209         const ARMCPRegInfo *ri;
210
211         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
212         if (!ri) {
213             ok = false;
214             continue;
215         }
216         if (ri->type & ARM_CP_NO_RAW) {
217             continue;
218         }
219         cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
220     }
221     return ok;
222 }
223
224 bool write_list_to_cpustate(ARMCPU *cpu)
225 {
226     int i;
227     bool ok = true;
228
229     for (i = 0; i < cpu->cpreg_array_len; i++) {
230         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
231         uint64_t v = cpu->cpreg_values[i];
232         const ARMCPRegInfo *ri;
233
234         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
235         if (!ri) {
236             ok = false;
237             continue;
238         }
239         if (ri->type & ARM_CP_NO_RAW) {
240             continue;
241         }
242         /* Write value and confirm it reads back as written
243          * (to catch read-only registers and partially read-only
244          * registers where the incoming migration value doesn't match)
245          */
246         write_raw_cp_reg(&cpu->env, ri, v);
247         if (read_raw_cp_reg(&cpu->env, ri) != v) {
248             ok = false;
249         }
250     }
251     return ok;
252 }
253
254 static void add_cpreg_to_list(gpointer key, gpointer opaque)
255 {
256     ARMCPU *cpu = opaque;
257     uint64_t regidx;
258     const ARMCPRegInfo *ri;
259
260     regidx = *(uint32_t *)key;
261     ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
262
263     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
264         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
265         /* The value array need not be initialized at this point */
266         cpu->cpreg_array_len++;
267     }
268 }
269
270 static void count_cpreg(gpointer key, gpointer opaque)
271 {
272     ARMCPU *cpu = opaque;
273     uint64_t regidx;
274     const ARMCPRegInfo *ri;
275
276     regidx = *(uint32_t *)key;
277     ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
278
279     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
280         cpu->cpreg_array_len++;
281     }
282 }
283
284 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
285 {
286     uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
287     uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
288
289     if (aidx > bidx) {
290         return 1;
291     }
292     if (aidx < bidx) {
293         return -1;
294     }
295     return 0;
296 }
297
298 void init_cpreg_list(ARMCPU *cpu)
299 {
300     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
301      * Note that we require cpreg_tuples[] to be sorted by key ID.
302      */
303     GList *keys;
304     int arraylen;
305
306     keys = g_hash_table_get_keys(cpu->cp_regs);
307     keys = g_list_sort(keys, cpreg_key_compare);
308
309     cpu->cpreg_array_len = 0;
310
311     g_list_foreach(keys, count_cpreg, cpu);
312
313     arraylen = cpu->cpreg_array_len;
314     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
315     cpu->cpreg_values = g_new(uint64_t, arraylen);
316     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
317     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
318     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
319     cpu->cpreg_array_len = 0;
320
321     g_list_foreach(keys, add_cpreg_to_list, cpu);
322
323     assert(cpu->cpreg_array_len == arraylen);
324
325     g_list_free(keys);
326 }
327
328 /*
329  * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
330  * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
331  *
332  * access_el3_aa32ns: Used to check AArch32 register views.
333  * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
334  */
335 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
336                                         const ARMCPRegInfo *ri)
337 {
338     bool secure = arm_is_secure_below_el3(env);
339
340     assert(!arm_el_is_aa64(env, 3));
341     if (secure) {
342         return CP_ACCESS_TRAP_UNCATEGORIZED;
343     }
344     return CP_ACCESS_OK;
345 }
346
347 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
348                                                 const ARMCPRegInfo *ri)
349 {
350     if (!arm_el_is_aa64(env, 3)) {
351         return access_el3_aa32ns(env, ri);
352     }
353     return CP_ACCESS_OK;
354 }
355
356 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
357 {
358     ARMCPU *cpu = arm_env_get_cpu(env);
359
360     raw_write(env, ri, value);
361     tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
362 }
363
364 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
365 {
366     ARMCPU *cpu = arm_env_get_cpu(env);
367
368     if (raw_read(env, ri) != value) {
369         /* Unlike real hardware the qemu TLB uses virtual addresses,
370          * not modified virtual addresses, so this causes a TLB flush.
371          */
372         tlb_flush(CPU(cpu), 1);
373         raw_write(env, ri, value);
374     }
375 }
376
377 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
378                              uint64_t value)
379 {
380     ARMCPU *cpu = arm_env_get_cpu(env);
381
382     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
383         && !extended_addresses_enabled(env)) {
384         /* For VMSA (when not using the LPAE long descriptor page table
385          * format) this register includes the ASID, so do a TLB flush.
386          * For PMSA it is purely a process ID and no action is needed.
387          */
388         tlb_flush(CPU(cpu), 1);
389     }
390     raw_write(env, ri, value);
391 }
392
393 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
394                           uint64_t value)
395 {
396     /* Invalidate all (TLBIALL) */
397     ARMCPU *cpu = arm_env_get_cpu(env);
398
399     tlb_flush(CPU(cpu), 1);
400 }
401
402 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
403                           uint64_t value)
404 {
405     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
406     ARMCPU *cpu = arm_env_get_cpu(env);
407
408     tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
409 }
410
411 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
412                            uint64_t value)
413 {
414     /* Invalidate by ASID (TLBIASID) */
415     ARMCPU *cpu = arm_env_get_cpu(env);
416
417     tlb_flush(CPU(cpu), value == 0);
418 }
419
420 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
421                            uint64_t value)
422 {
423     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
424     ARMCPU *cpu = arm_env_get_cpu(env);
425
426     tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
427 }
428
429 /* IS variants of TLB operations must affect all cores */
430 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
431                              uint64_t value)
432 {
433     CPUState *other_cs;
434
435     CPU_FOREACH(other_cs) {
436         tlb_flush(other_cs, 1);
437     }
438 }
439
440 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
441                              uint64_t value)
442 {
443     CPUState *other_cs;
444
445     CPU_FOREACH(other_cs) {
446         tlb_flush(other_cs, value == 0);
447     }
448 }
449
450 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
451                              uint64_t value)
452 {
453     CPUState *other_cs;
454
455     CPU_FOREACH(other_cs) {
456         tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
457     }
458 }
459
460 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
461                              uint64_t value)
462 {
463     CPUState *other_cs;
464
465     CPU_FOREACH(other_cs) {
466         tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
467     }
468 }
469
470 static const ARMCPRegInfo cp_reginfo[] = {
471     /* Define the secure and non-secure FCSE identifier CP registers
472      * separately because there is no secure bank in V8 (no _EL3).  This allows
473      * the secure register to be properly reset and migrated. There is also no
474      * v8 EL1 version of the register so the non-secure instance stands alone.
475      */
476     { .name = "FCSEIDR(NS)",
477       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
478       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
479       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
480       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
481     { .name = "FCSEIDR(S)",
482       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
483       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
484       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
485       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
486     /* Define the secure and non-secure context identifier CP registers
487      * separately because there is no secure bank in V8 (no _EL3).  This allows
488      * the secure register to be properly reset and migrated.  In the
489      * non-secure case, the 32-bit register will have reset and migration
490      * disabled during registration as it is handled by the 64-bit instance.
491      */
492     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
493       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
494       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
495       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
496       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
497     { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
498       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
499       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
500       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
501       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
502     REGINFO_SENTINEL
503 };
504
505 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
506     /* NB: Some of these registers exist in v8 but with more precise
507      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
508      */
509     /* MMU Domain access control / MPU write buffer control */
510     { .name = "DACR",
511       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
512       .access = PL1_RW, .resetvalue = 0,
513       .writefn = dacr_write, .raw_writefn = raw_write,
514       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
515                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
516     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
517      * For v6 and v5, these mappings are overly broad.
518      */
519     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
520       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
521     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
522       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
523     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
524       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
525     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
526       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
527     /* Cache maintenance ops; some of this space may be overridden later. */
528     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
529       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
530       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
531     REGINFO_SENTINEL
532 };
533
534 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
535     /* Not all pre-v6 cores implemented this WFI, so this is slightly
536      * over-broad.
537      */
538     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
539       .access = PL1_W, .type = ARM_CP_WFI },
540     REGINFO_SENTINEL
541 };
542
543 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
544     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
545      * is UNPREDICTABLE; we choose to NOP as most implementations do).
546      */
547     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
548       .access = PL1_W, .type = ARM_CP_WFI },
549     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
550      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
551      * OMAPCP will override this space.
552      */
553     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
554       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
555       .resetvalue = 0 },
556     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
557       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
558       .resetvalue = 0 },
559     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
560     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
561       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
562       .resetvalue = 0 },
563     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
564      * implementing it as RAZ means the "debug architecture version" bits
565      * will read as a reserved value, which should cause Linux to not try
566      * to use the debug hardware.
567      */
568     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
569       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
570     /* MMU TLB control. Note that the wildcarding means we cover not just
571      * the unified TLB ops but also the dside/iside/inner-shareable variants.
572      */
573     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
574       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
575       .type = ARM_CP_NO_RAW },
576     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
577       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
578       .type = ARM_CP_NO_RAW },
579     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
580       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
581       .type = ARM_CP_NO_RAW },
582     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
583       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
584       .type = ARM_CP_NO_RAW },
585     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
586       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
587     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
588       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
589     REGINFO_SENTINEL
590 };
591
592 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
593                         uint64_t value)
594 {
595     uint32_t mask = 0;
596
597     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
598     if (!arm_feature(env, ARM_FEATURE_V8)) {
599         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
600          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
601          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
602          */
603         if (arm_feature(env, ARM_FEATURE_VFP)) {
604             /* VFP coprocessor: cp10 & cp11 [23:20] */
605             mask |= (1 << 31) | (1 << 30) | (0xf << 20);
606
607             if (!arm_feature(env, ARM_FEATURE_NEON)) {
608                 /* ASEDIS [31] bit is RAO/WI */
609                 value |= (1 << 31);
610             }
611
612             /* VFPv3 and upwards with NEON implement 32 double precision
613              * registers (D0-D31).
614              */
615             if (!arm_feature(env, ARM_FEATURE_NEON) ||
616                     !arm_feature(env, ARM_FEATURE_VFP3)) {
617                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
618                 value |= (1 << 30);
619             }
620         }
621         value &= mask;
622     }
623     env->cp15.cpacr_el1 = value;
624 }
625
626 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri)
627 {
628     if (arm_feature(env, ARM_FEATURE_V8)) {
629         /* Check if CPACR accesses are to be trapped to EL2 */
630         if (arm_current_el(env) == 1 &&
631             (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
632             return CP_ACCESS_TRAP_EL2;
633         /* Check if CPACR accesses are to be trapped to EL3 */
634         } else if (arm_current_el(env) < 3 &&
635                    (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
636             return CP_ACCESS_TRAP_EL3;
637         }
638     }
639
640     return CP_ACCESS_OK;
641 }
642
643 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri)
644 {
645     /* Check if CPTR accesses are set to trap to EL3 */
646     if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
647         return CP_ACCESS_TRAP_EL3;
648     }
649
650     return CP_ACCESS_OK;
651 }
652
653 static const ARMCPRegInfo v6_cp_reginfo[] = {
654     /* prefetch by MVA in v6, NOP in v7 */
655     { .name = "MVA_prefetch",
656       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
657       .access = PL1_W, .type = ARM_CP_NOP },
658     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
659       .access = PL0_W, .type = ARM_CP_NOP },
660     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
661       .access = PL0_W, .type = ARM_CP_NOP },
662     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
663       .access = PL0_W, .type = ARM_CP_NOP },
664     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
665       .access = PL1_RW,
666       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
667                              offsetof(CPUARMState, cp15.ifar_ns) },
668       .resetvalue = 0, },
669     /* Watchpoint Fault Address Register : should actually only be present
670      * for 1136, 1176, 11MPCore.
671      */
672     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
673       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
674     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
675       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
676       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
677       .resetvalue = 0, .writefn = cpacr_write },
678     REGINFO_SENTINEL
679 };
680
681 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
682 {
683     /* Performance monitor registers user accessibility is controlled
684      * by PMUSERENR.
685      */
686     if (arm_current_el(env) == 0 && !env->cp15.c9_pmuserenr) {
687         return CP_ACCESS_TRAP;
688     }
689     return CP_ACCESS_OK;
690 }
691
692 #ifndef CONFIG_USER_ONLY
693
694 static inline bool arm_ccnt_enabled(CPUARMState *env)
695 {
696     /* This does not support checking PMCCFILTR_EL0 register */
697
698     if (!(env->cp15.c9_pmcr & PMCRE)) {
699         return false;
700     }
701
702     return true;
703 }
704
705 void pmccntr_sync(CPUARMState *env)
706 {
707     uint64_t temp_ticks;
708
709     temp_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
710                           get_ticks_per_sec(), 1000000);
711
712     if (env->cp15.c9_pmcr & PMCRD) {
713         /* Increment once every 64 processor clock cycles */
714         temp_ticks /= 64;
715     }
716
717     if (arm_ccnt_enabled(env)) {
718         env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
719     }
720 }
721
722 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
723                        uint64_t value)
724 {
725     pmccntr_sync(env);
726
727     if (value & PMCRC) {
728         /* The counter has been reset */
729         env->cp15.c15_ccnt = 0;
730     }
731
732     /* only the DP, X, D and E bits are writable */
733     env->cp15.c9_pmcr &= ~0x39;
734     env->cp15.c9_pmcr |= (value & 0x39);
735
736     pmccntr_sync(env);
737 }
738
739 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
740 {
741     uint64_t total_ticks;
742
743     if (!arm_ccnt_enabled(env)) {
744         /* Counter is disabled, do not change value */
745         return env->cp15.c15_ccnt;
746     }
747
748     total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
749                            get_ticks_per_sec(), 1000000);
750
751     if (env->cp15.c9_pmcr & PMCRD) {
752         /* Increment once every 64 processor clock cycles */
753         total_ticks /= 64;
754     }
755     return total_ticks - env->cp15.c15_ccnt;
756 }
757
758 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
759                         uint64_t value)
760 {
761     uint64_t total_ticks;
762
763     if (!arm_ccnt_enabled(env)) {
764         /* Counter is disabled, set the absolute value */
765         env->cp15.c15_ccnt = value;
766         return;
767     }
768
769     total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
770                            get_ticks_per_sec(), 1000000);
771
772     if (env->cp15.c9_pmcr & PMCRD) {
773         /* Increment once every 64 processor clock cycles */
774         total_ticks /= 64;
775     }
776     env->cp15.c15_ccnt = total_ticks - value;
777 }
778
779 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
780                             uint64_t value)
781 {
782     uint64_t cur_val = pmccntr_read(env, NULL);
783
784     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
785 }
786
787 #else /* CONFIG_USER_ONLY */
788
789 void pmccntr_sync(CPUARMState *env)
790 {
791 }
792
793 #endif
794
795 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
796                             uint64_t value)
797 {
798     pmccntr_sync(env);
799     env->cp15.pmccfiltr_el0 = value & 0x7E000000;
800     pmccntr_sync(env);
801 }
802
803 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
804                             uint64_t value)
805 {
806     value &= (1 << 31);
807     env->cp15.c9_pmcnten |= value;
808 }
809
810 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
811                              uint64_t value)
812 {
813     value &= (1 << 31);
814     env->cp15.c9_pmcnten &= ~value;
815 }
816
817 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
818                          uint64_t value)
819 {
820     env->cp15.c9_pmovsr &= ~value;
821 }
822
823 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
824                              uint64_t value)
825 {
826     env->cp15.c9_pmxevtyper = value & 0xff;
827 }
828
829 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
830                             uint64_t value)
831 {
832     env->cp15.c9_pmuserenr = value & 1;
833 }
834
835 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
836                              uint64_t value)
837 {
838     /* We have no event counters so only the C bit can be changed */
839     value &= (1 << 31);
840     env->cp15.c9_pminten |= value;
841 }
842
843 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
844                              uint64_t value)
845 {
846     value &= (1 << 31);
847     env->cp15.c9_pminten &= ~value;
848 }
849
850 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
851                        uint64_t value)
852 {
853     /* Note that even though the AArch64 view of this register has bits
854      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
855      * architectural requirements for bits which are RES0 only in some
856      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
857      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
858      */
859     raw_write(env, ri, value & ~0x1FULL);
860 }
861
862 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
863 {
864     /* We only mask off bits that are RES0 both for AArch64 and AArch32.
865      * For bits that vary between AArch32/64, code needs to check the
866      * current execution mode before directly using the feature bit.
867      */
868     uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
869
870     if (!arm_feature(env, ARM_FEATURE_EL2)) {
871         valid_mask &= ~SCR_HCE;
872
873         /* On ARMv7, SMD (or SCD as it is called in v7) is only
874          * supported if EL2 exists. The bit is UNK/SBZP when
875          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
876          * when EL2 is unavailable.
877          * On ARMv8, this bit is always available.
878          */
879         if (arm_feature(env, ARM_FEATURE_V7) &&
880             !arm_feature(env, ARM_FEATURE_V8)) {
881             valid_mask &= ~SCR_SMD;
882         }
883     }
884
885     /* Clear all-context RES0 bits.  */
886     value &= valid_mask;
887     raw_write(env, ri, value);
888 }
889
890 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
891 {
892     ARMCPU *cpu = arm_env_get_cpu(env);
893
894     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
895      * bank
896      */
897     uint32_t index = A32_BANKED_REG_GET(env, csselr,
898                                         ri->secure & ARM_CP_SECSTATE_S);
899
900     return cpu->ccsidr[index];
901 }
902
903 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
904                          uint64_t value)
905 {
906     raw_write(env, ri, value & 0xf);
907 }
908
909 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
910 {
911     CPUState *cs = ENV_GET_CPU(env);
912     uint64_t ret = 0;
913
914     if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
915         ret |= CPSR_I;
916     }
917     if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
918         ret |= CPSR_F;
919     }
920     /* External aborts are not possible in QEMU so A bit is always clear */
921     return ret;
922 }
923
924 static const ARMCPRegInfo v7_cp_reginfo[] = {
925     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
926     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
927       .access = PL1_W, .type = ARM_CP_NOP },
928     /* Performance monitors are implementation defined in v7,
929      * but with an ARM recommended set of registers, which we
930      * follow (although we don't actually implement any counters)
931      *
932      * Performance registers fall into three categories:
933      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
934      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
935      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
936      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
937      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
938      */
939     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
940       .access = PL0_RW, .type = ARM_CP_ALIAS,
941       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
942       .writefn = pmcntenset_write,
943       .accessfn = pmreg_access,
944       .raw_writefn = raw_write },
945     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
946       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
947       .access = PL0_RW, .accessfn = pmreg_access,
948       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
949       .writefn = pmcntenset_write, .raw_writefn = raw_write },
950     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
951       .access = PL0_RW,
952       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
953       .accessfn = pmreg_access,
954       .writefn = pmcntenclr_write,
955       .type = ARM_CP_ALIAS },
956     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
957       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
958       .access = PL0_RW, .accessfn = pmreg_access,
959       .type = ARM_CP_ALIAS,
960       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
961       .writefn = pmcntenclr_write },
962     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
963       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
964       .accessfn = pmreg_access,
965       .writefn = pmovsr_write,
966       .raw_writefn = raw_write },
967     /* Unimplemented so WI. */
968     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
969       .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
970     /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
971      * We choose to RAZ/WI.
972      */
973     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
974       .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
975       .accessfn = pmreg_access },
976 #ifndef CONFIG_USER_ONLY
977     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
978       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
979       .readfn = pmccntr_read, .writefn = pmccntr_write32,
980       .accessfn = pmreg_access },
981     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
982       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
983       .access = PL0_RW, .accessfn = pmreg_access,
984       .type = ARM_CP_IO,
985       .readfn = pmccntr_read, .writefn = pmccntr_write, },
986 #endif
987     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
988       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
989       .writefn = pmccfiltr_write,
990       .access = PL0_RW, .accessfn = pmreg_access,
991       .type = ARM_CP_IO,
992       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
993       .resetvalue = 0, },
994     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
995       .access = PL0_RW,
996       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
997       .accessfn = pmreg_access, .writefn = pmxevtyper_write,
998       .raw_writefn = raw_write },
999     /* Unimplemented, RAZ/WI. */
1000     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1001       .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1002       .accessfn = pmreg_access },
1003     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1004       .access = PL0_R | PL1_RW,
1005       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1006       .resetvalue = 0,
1007       .writefn = pmuserenr_write, .raw_writefn = raw_write },
1008     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1009       .access = PL1_RW,
1010       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1011       .resetvalue = 0,
1012       .writefn = pmintenset_write, .raw_writefn = raw_write },
1013     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1014       .access = PL1_RW, .type = ARM_CP_ALIAS,
1015       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1016       .writefn = pmintenclr_write, },
1017     { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
1018       .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
1019       .access = PL1_RW, .writefn = vbar_write,
1020       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
1021                              offsetof(CPUARMState, cp15.vbar_ns) },
1022       .resetvalue = 0 },
1023     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1024       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
1025       .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
1026     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1027       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
1028       .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1029       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1030                              offsetof(CPUARMState, cp15.csselr_ns) } },
1031     /* Auxiliary ID register: this actually has an IMPDEF value but for now
1032      * just RAZ for all cores:
1033      */
1034     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1035       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
1036       .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1037     /* Auxiliary fault status registers: these also are IMPDEF, and we
1038      * choose to RAZ/WI for all cores.
1039      */
1040     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1041       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1042       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1043     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1044       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1045       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1046     /* MAIR can just read-as-written because we don't implement caches
1047      * and so don't need to care about memory attributes.
1048      */
1049     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1050       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
1051       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
1052       .resetvalue = 0 },
1053     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1054       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1055       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1056       .resetvalue = 0 },
1057     /* For non-long-descriptor page tables these are PRRR and NMRR;
1058      * regardless they still act as reads-as-written for QEMU.
1059      */
1060      /* MAIR0/1 are defined separately from their 64-bit counterpart which
1061       * allows them to assign the correct fieldoffset based on the endianness
1062       * handled in the field definitions.
1063       */
1064     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
1065       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
1066       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1067                              offsetof(CPUARMState, cp15.mair0_ns) },
1068       .resetfn = arm_cp_reset_ignore },
1069     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
1070       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
1071       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1072                              offsetof(CPUARMState, cp15.mair1_ns) },
1073       .resetfn = arm_cp_reset_ignore },
1074     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1075       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
1076       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
1077     /* 32 bit ITLB invalidates */
1078     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
1079       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1080     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
1081       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1082     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
1083       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1084     /* 32 bit DTLB invalidates */
1085     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
1086       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1087     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
1088       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1089     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
1090       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1091     /* 32 bit TLB invalidates */
1092     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
1093       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1094     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
1095       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1096     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
1097       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1098     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
1099       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
1100     REGINFO_SENTINEL
1101 };
1102
1103 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1104     /* 32 bit TLB invalidates, Inner Shareable */
1105     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1106       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
1107     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1108       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
1109     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1110       .type = ARM_CP_NO_RAW, .access = PL1_W,
1111       .writefn = tlbiasid_is_write },
1112     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1113       .type = ARM_CP_NO_RAW, .access = PL1_W,
1114       .writefn = tlbimvaa_is_write },
1115     REGINFO_SENTINEL
1116 };
1117
1118 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1119                         uint64_t value)
1120 {
1121     value &= 1;
1122     env->teecr = value;
1123 }
1124
1125 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
1126 {
1127     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
1128         return CP_ACCESS_TRAP;
1129     }
1130     return CP_ACCESS_OK;
1131 }
1132
1133 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1134     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1135       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1136       .resetvalue = 0,
1137       .writefn = teecr_write },
1138     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1139       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1140       .accessfn = teehbr_access, .resetvalue = 0 },
1141     REGINFO_SENTINEL
1142 };
1143
1144 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1145     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1146       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1147       .access = PL0_RW,
1148       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
1149     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1150       .access = PL0_RW,
1151       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1152                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
1153       .resetfn = arm_cp_reset_ignore },
1154     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1155       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1156       .access = PL0_R|PL1_W,
1157       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1158       .resetvalue = 0},
1159     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1160       .access = PL0_R|PL1_W,
1161       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1162                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
1163       .resetfn = arm_cp_reset_ignore },
1164     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
1165       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1166       .access = PL1_RW,
1167       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1168     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1169       .access = PL1_RW,
1170       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1171                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1172       .resetvalue = 0 },
1173     REGINFO_SENTINEL
1174 };
1175
1176 #ifndef CONFIG_USER_ONLY
1177
1178 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
1179 {
1180     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
1181     if (arm_current_el(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
1182         return CP_ACCESS_TRAP;
1183     }
1184     return CP_ACCESS_OK;
1185 }
1186
1187 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
1188 {
1189     unsigned int cur_el = arm_current_el(env);
1190     bool secure = arm_is_secure(env);
1191
1192     /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1193     if (cur_el == 0 &&
1194         !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1195         return CP_ACCESS_TRAP;
1196     }
1197
1198     if (arm_feature(env, ARM_FEATURE_EL2) &&
1199         timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1200         !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1201         return CP_ACCESS_TRAP_EL2;
1202     }
1203     return CP_ACCESS_OK;
1204 }
1205
1206 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
1207 {
1208     unsigned int cur_el = arm_current_el(env);
1209     bool secure = arm_is_secure(env);
1210
1211     /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1212      * EL0[PV]TEN is zero.
1213      */
1214     if (cur_el == 0 &&
1215         !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1216         return CP_ACCESS_TRAP;
1217     }
1218
1219     if (arm_feature(env, ARM_FEATURE_EL2) &&
1220         timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1221         !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1222         return CP_ACCESS_TRAP_EL2;
1223     }
1224     return CP_ACCESS_OK;
1225 }
1226
1227 static CPAccessResult gt_pct_access(CPUARMState *env,
1228                                          const ARMCPRegInfo *ri)
1229 {
1230     return gt_counter_access(env, GTIMER_PHYS);
1231 }
1232
1233 static CPAccessResult gt_vct_access(CPUARMState *env,
1234                                          const ARMCPRegInfo *ri)
1235 {
1236     return gt_counter_access(env, GTIMER_VIRT);
1237 }
1238
1239 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1240 {
1241     return gt_timer_access(env, GTIMER_PHYS);
1242 }
1243
1244 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1245 {
1246     return gt_timer_access(env, GTIMER_VIRT);
1247 }
1248
1249 static CPAccessResult gt_stimer_access(CPUARMState *env,
1250                                        const ARMCPRegInfo *ri)
1251 {
1252     /* The AArch64 register view of the secure physical timer is
1253      * always accessible from EL3, and configurably accessible from
1254      * Secure EL1.
1255      */
1256     switch (arm_current_el(env)) {
1257     case 1:
1258         if (!arm_is_secure(env)) {
1259             return CP_ACCESS_TRAP;
1260         }
1261         if (!(env->cp15.scr_el3 & SCR_ST)) {
1262             return CP_ACCESS_TRAP_EL3;
1263         }
1264         return CP_ACCESS_OK;
1265     case 0:
1266     case 2:
1267         return CP_ACCESS_TRAP;
1268     case 3:
1269         return CP_ACCESS_OK;
1270     default:
1271         g_assert_not_reached();
1272     }
1273 }
1274
1275 static uint64_t gt_get_countervalue(CPUARMState *env)
1276 {
1277     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1278 }
1279
1280 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1281 {
1282     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1283
1284     if (gt->ctl & 1) {
1285         /* Timer enabled: calculate and set current ISTATUS, irq, and
1286          * reset timer to when ISTATUS next has to change
1287          */
1288         uint64_t offset = timeridx == GTIMER_VIRT ?
1289                                       cpu->env.cp15.cntvoff_el2 : 0;
1290         uint64_t count = gt_get_countervalue(&cpu->env);
1291         /* Note that this must be unsigned 64 bit arithmetic: */
1292         int istatus = count - offset >= gt->cval;
1293         uint64_t nexttick;
1294
1295         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1296         qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1297                      (istatus && !(gt->ctl & 2)));
1298         if (istatus) {
1299             /* Next transition is when count rolls back over to zero */
1300             nexttick = UINT64_MAX;
1301         } else {
1302             /* Next transition is when we hit cval */
1303             nexttick = gt->cval + offset;
1304         }
1305         /* Note that the desired next expiry time might be beyond the
1306          * signed-64-bit range of a QEMUTimer -- in this case we just
1307          * set the timer for as far in the future as possible. When the
1308          * timer expires we will reset the timer for any remaining period.
1309          */
1310         if (nexttick > INT64_MAX / GTIMER_SCALE) {
1311             nexttick = INT64_MAX / GTIMER_SCALE;
1312         }
1313         timer_mod(cpu->gt_timer[timeridx], nexttick);
1314     } else {
1315         /* Timer disabled: ISTATUS and timer output always clear */
1316         gt->ctl &= ~4;
1317         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1318         timer_del(cpu->gt_timer[timeridx]);
1319     }
1320 }
1321
1322 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1323                            int timeridx)
1324 {
1325     ARMCPU *cpu = arm_env_get_cpu(env);
1326
1327     timer_del(cpu->gt_timer[timeridx]);
1328 }
1329
1330 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1331 {
1332     return gt_get_countervalue(env);
1333 }
1334
1335 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1336 {
1337     return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1338 }
1339
1340 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1341                           int timeridx,
1342                           uint64_t value)
1343 {
1344     env->cp15.c14_timer[timeridx].cval = value;
1345     gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1346 }
1347
1348 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1349                              int timeridx)
1350 {
1351     uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1352
1353     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1354                       (gt_get_countervalue(env) - offset));
1355 }
1356
1357 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1358                           int timeridx,
1359                           uint64_t value)
1360 {
1361     uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1362
1363     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
1364                                          sextract64(value, 0, 32);
1365     gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1366 }
1367
1368 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1369                          int timeridx,
1370                          uint64_t value)
1371 {
1372     ARMCPU *cpu = arm_env_get_cpu(env);
1373     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1374
1375     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1376     if ((oldval ^ value) & 1) {
1377         /* Enable toggled */
1378         gt_recalc_timer(cpu, timeridx);
1379     } else if ((oldval ^ value) & 2) {
1380         /* IMASK toggled: don't need to recalculate,
1381          * just set the interrupt line based on ISTATUS
1382          */
1383         qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1384                      (oldval & 4) && !(value & 2));
1385     }
1386 }
1387
1388 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1389 {
1390     gt_timer_reset(env, ri, GTIMER_PHYS);
1391 }
1392
1393 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1394                                uint64_t value)
1395 {
1396     gt_cval_write(env, ri, GTIMER_PHYS, value);
1397 }
1398
1399 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1400 {
1401     return gt_tval_read(env, ri, GTIMER_PHYS);
1402 }
1403
1404 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1405                                uint64_t value)
1406 {
1407     gt_tval_write(env, ri, GTIMER_PHYS, value);
1408 }
1409
1410 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1411                               uint64_t value)
1412 {
1413     gt_ctl_write(env, ri, GTIMER_PHYS, value);
1414 }
1415
1416 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1417 {
1418     gt_timer_reset(env, ri, GTIMER_VIRT);
1419 }
1420
1421 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1422                                uint64_t value)
1423 {
1424     gt_cval_write(env, ri, GTIMER_VIRT, value);
1425 }
1426
1427 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1428 {
1429     return gt_tval_read(env, ri, GTIMER_VIRT);
1430 }
1431
1432 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1433                                uint64_t value)
1434 {
1435     gt_tval_write(env, ri, GTIMER_VIRT, value);
1436 }
1437
1438 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1439                               uint64_t value)
1440 {
1441     gt_ctl_write(env, ri, GTIMER_VIRT, value);
1442 }
1443
1444 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1445                               uint64_t value)
1446 {
1447     ARMCPU *cpu = arm_env_get_cpu(env);
1448
1449     raw_write(env, ri, value);
1450     gt_recalc_timer(cpu, GTIMER_VIRT);
1451 }
1452
1453 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1454 {
1455     gt_timer_reset(env, ri, GTIMER_HYP);
1456 }
1457
1458 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1459                               uint64_t value)
1460 {
1461     gt_cval_write(env, ri, GTIMER_HYP, value);
1462 }
1463
1464 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1465 {
1466     return gt_tval_read(env, ri, GTIMER_HYP);
1467 }
1468
1469 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1470                               uint64_t value)
1471 {
1472     gt_tval_write(env, ri, GTIMER_HYP, value);
1473 }
1474
1475 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1476                               uint64_t value)
1477 {
1478     gt_ctl_write(env, ri, GTIMER_HYP, value);
1479 }
1480
1481 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1482 {
1483     gt_timer_reset(env, ri, GTIMER_SEC);
1484 }
1485
1486 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1487                               uint64_t value)
1488 {
1489     gt_cval_write(env, ri, GTIMER_SEC, value);
1490 }
1491
1492 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1493 {
1494     return gt_tval_read(env, ri, GTIMER_SEC);
1495 }
1496
1497 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1498                               uint64_t value)
1499 {
1500     gt_tval_write(env, ri, GTIMER_SEC, value);
1501 }
1502
1503 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504                               uint64_t value)
1505 {
1506     gt_ctl_write(env, ri, GTIMER_SEC, value);
1507 }
1508
1509 void arm_gt_ptimer_cb(void *opaque)
1510 {
1511     ARMCPU *cpu = opaque;
1512
1513     gt_recalc_timer(cpu, GTIMER_PHYS);
1514 }
1515
1516 void arm_gt_vtimer_cb(void *opaque)
1517 {
1518     ARMCPU *cpu = opaque;
1519
1520     gt_recalc_timer(cpu, GTIMER_VIRT);
1521 }
1522
1523 void arm_gt_htimer_cb(void *opaque)
1524 {
1525     ARMCPU *cpu = opaque;
1526
1527     gt_recalc_timer(cpu, GTIMER_HYP);
1528 }
1529
1530 void arm_gt_stimer_cb(void *opaque)
1531 {
1532     ARMCPU *cpu = opaque;
1533
1534     gt_recalc_timer(cpu, GTIMER_SEC);
1535 }
1536
1537 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1538     /* Note that CNTFRQ is purely reads-as-written for the benefit
1539      * of software; writing it doesn't actually change the timer frequency.
1540      * Our reset value matches the fixed frequency we implement the timer at.
1541      */
1542     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1543       .type = ARM_CP_ALIAS,
1544       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1545       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1546     },
1547     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1548       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1549       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1550       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1551       .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1552     },
1553     /* overall control: mostly access permissions */
1554     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1555       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1556       .access = PL1_RW,
1557       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1558       .resetvalue = 0,
1559     },
1560     /* per-timer control */
1561     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1562       .secure = ARM_CP_SECSTATE_NS,
1563       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1564       .accessfn = gt_ptimer_access,
1565       .fieldoffset = offsetoflow32(CPUARMState,
1566                                    cp15.c14_timer[GTIMER_PHYS].ctl),
1567       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1568     },
1569     { .name = "CNTP_CTL(S)",
1570       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1571       .secure = ARM_CP_SECSTATE_S,
1572       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1573       .accessfn = gt_ptimer_access,
1574       .fieldoffset = offsetoflow32(CPUARMState,
1575                                    cp15.c14_timer[GTIMER_SEC].ctl),
1576       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1577     },
1578     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1579       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1580       .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1581       .accessfn = gt_ptimer_access,
1582       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1583       .resetvalue = 0,
1584       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1585     },
1586     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1587       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1588       .accessfn = gt_vtimer_access,
1589       .fieldoffset = offsetoflow32(CPUARMState,
1590                                    cp15.c14_timer[GTIMER_VIRT].ctl),
1591       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1592     },
1593     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1594       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1595       .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1596       .accessfn = gt_vtimer_access,
1597       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1598       .resetvalue = 0,
1599       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1600     },
1601     /* TimerValue views: a 32 bit downcounting view of the underlying state */
1602     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1603       .secure = ARM_CP_SECSTATE_NS,
1604       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1605       .accessfn = gt_ptimer_access,
1606       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
1607     },
1608     { .name = "CNTP_TVAL(S)",
1609       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1610       .secure = ARM_CP_SECSTATE_S,
1611       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1612       .accessfn = gt_ptimer_access,
1613       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1614     },
1615     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1616       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
1617       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1618       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
1619       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
1620     },
1621     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
1622       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1623       .accessfn = gt_vtimer_access,
1624       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
1625     },
1626     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1627       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
1628       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1629       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
1630       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
1631     },
1632     /* The counter itself */
1633     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
1634       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1635       .accessfn = gt_pct_access,
1636       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1637     },
1638     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1639       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
1640       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1641       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
1642     },
1643     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
1644       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1645       .accessfn = gt_vct_access,
1646       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
1647     },
1648     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1649       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
1650       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1651       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
1652     },
1653     /* Comparison value, indicating when the timer goes off */
1654     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1655       .secure = ARM_CP_SECSTATE_NS,
1656       .access = PL1_RW | PL0_R,
1657       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1658       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1659       .accessfn = gt_ptimer_access,
1660       .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
1661     },
1662     { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
1663       .secure = ARM_CP_SECSTATE_S,
1664       .access = PL1_RW | PL0_R,
1665       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1666       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1667       .accessfn = gt_ptimer_access,
1668       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1669     },
1670     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1671       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1672       .access = PL1_RW | PL0_R,
1673       .type = ARM_CP_IO,
1674       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1675       .resetvalue = 0, .accessfn = gt_ptimer_access,
1676       .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
1677     },
1678     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1679       .access = PL1_RW | PL0_R,
1680       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1681       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1682       .accessfn = gt_vtimer_access,
1683       .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
1684     },
1685     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1686       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1687       .access = PL1_RW | PL0_R,
1688       .type = ARM_CP_IO,
1689       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1690       .resetvalue = 0, .accessfn = gt_vtimer_access,
1691       .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
1692     },
1693     /* Secure timer -- this is actually restricted to only EL3
1694      * and configurably Secure-EL1 via the accessfn.
1695      */
1696     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
1697       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
1698       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
1699       .accessfn = gt_stimer_access,
1700       .readfn = gt_sec_tval_read,
1701       .writefn = gt_sec_tval_write,
1702       .resetfn = gt_sec_timer_reset,
1703     },
1704     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
1705       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
1706       .type = ARM_CP_IO, .access = PL1_RW,
1707       .accessfn = gt_stimer_access,
1708       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
1709       .resetvalue = 0,
1710       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1711     },
1712     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
1713       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
1714       .type = ARM_CP_IO, .access = PL1_RW,
1715       .accessfn = gt_stimer_access,
1716       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1717       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1718     },
1719     REGINFO_SENTINEL
1720 };
1721
1722 #else
1723 /* In user-mode none of the generic timer registers are accessible,
1724  * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
1725  * so instead just don't register any of them.
1726  */
1727 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1728     REGINFO_SENTINEL
1729 };
1730
1731 #endif
1732
1733 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1734 {
1735     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1736         raw_write(env, ri, value);
1737     } else if (arm_feature(env, ARM_FEATURE_V7)) {
1738         raw_write(env, ri, value & 0xfffff6ff);
1739     } else {
1740         raw_write(env, ri, value & 0xfffff1ff);
1741     }
1742 }
1743
1744 #ifndef CONFIG_USER_ONLY
1745 /* get_phys_addr() isn't present for user-mode-only targets */
1746
1747 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
1748 {
1749     if (ri->opc2 & 4) {
1750         /* The ATS12NSO* operations must trap to EL3 if executed in
1751          * Secure EL1 (which can only happen if EL3 is AArch64).
1752          * They are simply UNDEF if executed from NS EL1.
1753          * They function normally from EL2 or EL3.
1754          */
1755         if (arm_current_el(env) == 1) {
1756             if (arm_is_secure_below_el3(env)) {
1757                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
1758             }
1759             return CP_ACCESS_TRAP_UNCATEGORIZED;
1760         }
1761     }
1762     return CP_ACCESS_OK;
1763 }
1764
1765 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
1766                              int access_type, ARMMMUIdx mmu_idx)
1767 {
1768     hwaddr phys_addr;
1769     target_ulong page_size;
1770     int prot;
1771     uint32_t fsr;
1772     bool ret;
1773     uint64_t par64;
1774     MemTxAttrs attrs = {};
1775
1776     ret = get_phys_addr(env, value, access_type, mmu_idx,
1777                         &phys_addr, &attrs, &prot, &page_size, &fsr);
1778     if (extended_addresses_enabled(env)) {
1779         /* fsr is a DFSR/IFSR value for the long descriptor
1780          * translation table format, but with WnR always clear.
1781          * Convert it to a 64-bit PAR.
1782          */
1783         par64 = (1 << 11); /* LPAE bit always set */
1784         if (!ret) {
1785             par64 |= phys_addr & ~0xfffULL;
1786             if (!attrs.secure) {
1787                 par64 |= (1 << 9); /* NS */
1788             }
1789             /* We don't set the ATTR or SH fields in the PAR. */
1790         } else {
1791             par64 |= 1; /* F */
1792             par64 |= (fsr & 0x3f) << 1; /* FS */
1793             /* Note that S2WLK and FSTAGE are always zero, because we don't
1794              * implement virtualization and therefore there can't be a stage 2
1795              * fault.
1796              */
1797         }
1798     } else {
1799         /* fsr is a DFSR/IFSR value for the short descriptor
1800          * translation table format (with WnR always clear).
1801          * Convert it to a 32-bit PAR.
1802          */
1803         if (!ret) {
1804             /* We do not set any attribute bits in the PAR */
1805             if (page_size == (1 << 24)
1806                 && arm_feature(env, ARM_FEATURE_V7)) {
1807                 par64 = (phys_addr & 0xff000000) | (1 << 1);
1808             } else {
1809                 par64 = phys_addr & 0xfffff000;
1810             }
1811             if (!attrs.secure) {
1812                 par64 |= (1 << 9); /* NS */
1813             }
1814         } else {
1815             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
1816                     ((fsr & 0xf) << 1) | 1;
1817         }
1818     }
1819     return par64;
1820 }
1821
1822 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1823 {
1824     int access_type = ri->opc2 & 1;
1825     uint64_t par64;
1826     ARMMMUIdx mmu_idx;
1827     int el = arm_current_el(env);
1828     bool secure = arm_is_secure_below_el3(env);
1829
1830     switch (ri->opc2 & 6) {
1831     case 0:
1832         /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
1833         switch (el) {
1834         case 3:
1835             mmu_idx = ARMMMUIdx_S1E3;
1836             break;
1837         case 2:
1838             mmu_idx = ARMMMUIdx_S1NSE1;
1839             break;
1840         case 1:
1841             mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
1842             break;
1843         default:
1844             g_assert_not_reached();
1845         }
1846         break;
1847     case 2:
1848         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
1849         switch (el) {
1850         case 3:
1851             mmu_idx = ARMMMUIdx_S1SE0;
1852             break;
1853         case 2:
1854             mmu_idx = ARMMMUIdx_S1NSE0;
1855             break;
1856         case 1:
1857             mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
1858             break;
1859         default:
1860             g_assert_not_reached();
1861         }
1862         break;
1863     case 4:
1864         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
1865         mmu_idx = ARMMMUIdx_S12NSE1;
1866         break;
1867     case 6:
1868         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
1869         mmu_idx = ARMMMUIdx_S12NSE0;
1870         break;
1871     default:
1872         g_assert_not_reached();
1873     }
1874
1875     par64 = do_ats_write(env, value, access_type, mmu_idx);
1876
1877     A32_BANKED_CURRENT_REG_SET(env, par, par64);
1878 }
1879
1880 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
1881                         uint64_t value)
1882 {
1883     int access_type = ri->opc2 & 1;
1884     uint64_t par64;
1885
1886     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
1887
1888     A32_BANKED_CURRENT_REG_SET(env, par, par64);
1889 }
1890
1891 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri)
1892 {
1893     if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
1894         return CP_ACCESS_TRAP;
1895     }
1896     return CP_ACCESS_OK;
1897 }
1898
1899 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
1900                         uint64_t value)
1901 {
1902     int access_type = ri->opc2 & 1;
1903     ARMMMUIdx mmu_idx;
1904     int secure = arm_is_secure_below_el3(env);
1905
1906     switch (ri->opc2 & 6) {
1907     case 0:
1908         switch (ri->opc1) {
1909         case 0: /* AT S1E1R, AT S1E1W */
1910             mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
1911             break;
1912         case 4: /* AT S1E2R, AT S1E2W */
1913             mmu_idx = ARMMMUIdx_S1E2;
1914             break;
1915         case 6: /* AT S1E3R, AT S1E3W */
1916             mmu_idx = ARMMMUIdx_S1E3;
1917             break;
1918         default:
1919             g_assert_not_reached();
1920         }
1921         break;
1922     case 2: /* AT S1E0R, AT S1E0W */
1923         mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
1924         break;
1925     case 4: /* AT S12E1R, AT S12E1W */
1926         mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
1927         break;
1928     case 6: /* AT S12E0R, AT S12E0W */
1929         mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
1930         break;
1931     default:
1932         g_assert_not_reached();
1933     }
1934
1935     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
1936 }
1937 #endif
1938
1939 static const ARMCPRegInfo vapa_cp_reginfo[] = {
1940     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1941       .access = PL1_RW, .resetvalue = 0,
1942       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
1943                              offsetoflow32(CPUARMState, cp15.par_ns) },
1944       .writefn = par_write },
1945 #ifndef CONFIG_USER_ONLY
1946     /* This underdecoding is safe because the reginfo is NO_RAW. */
1947     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1948       .access = PL1_W, .accessfn = ats_access,
1949       .writefn = ats_write, .type = ARM_CP_NO_RAW },
1950 #endif
1951     REGINFO_SENTINEL
1952 };
1953
1954 /* Return basic MPU access permission bits.  */
1955 static uint32_t simple_mpu_ap_bits(uint32_t val)
1956 {
1957     uint32_t ret;
1958     uint32_t mask;
1959     int i;
1960     ret = 0;
1961     mask = 3;
1962     for (i = 0; i < 16; i += 2) {
1963         ret |= (val >> i) & mask;
1964         mask <<= 2;
1965     }
1966     return ret;
1967 }
1968
1969 /* Pad basic MPU access permission bits to extended format.  */
1970 static uint32_t extended_mpu_ap_bits(uint32_t val)
1971 {
1972     uint32_t ret;
1973     uint32_t mask;
1974     int i;
1975     ret = 0;
1976     mask = 3;
1977     for (i = 0; i < 16; i += 2) {
1978         ret |= (val & mask) << i;
1979         mask <<= 2;
1980     }
1981     return ret;
1982 }
1983
1984 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1985                                  uint64_t value)
1986 {
1987     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
1988 }
1989
1990 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1991 {
1992     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
1993 }
1994
1995 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1996                                  uint64_t value)
1997 {
1998     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
1999 }
2000
2001 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2002 {
2003     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
2004 }
2005
2006 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2007 {
2008     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2009
2010     if (!u32p) {
2011         return 0;
2012     }
2013
2014     u32p += env->cp15.c6_rgnr;
2015     return *u32p;
2016 }
2017
2018 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2019                          uint64_t value)
2020 {
2021     ARMCPU *cpu = arm_env_get_cpu(env);
2022     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2023
2024     if (!u32p) {
2025         return;
2026     }
2027
2028     u32p += env->cp15.c6_rgnr;
2029     tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */
2030     *u32p = value;
2031 }
2032
2033 static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2034 {
2035     ARMCPU *cpu = arm_env_get_cpu(env);
2036     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2037
2038     if (!u32p) {
2039         return;
2040     }
2041
2042     memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion);
2043 }
2044
2045 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2046                               uint64_t value)
2047 {
2048     ARMCPU *cpu = arm_env_get_cpu(env);
2049     uint32_t nrgs = cpu->pmsav7_dregion;
2050
2051     if (value >= nrgs) {
2052         qemu_log_mask(LOG_GUEST_ERROR,
2053                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2054                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2055         return;
2056     }
2057
2058     raw_write(env, ri, value);
2059 }
2060
2061 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2062     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2063       .access = PL1_RW, .type = ARM_CP_NO_RAW,
2064       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2065       .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2066     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2067       .access = PL1_RW, .type = ARM_CP_NO_RAW,
2068       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2069       .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2070     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2071       .access = PL1_RW, .type = ARM_CP_NO_RAW,
2072       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2073       .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2074     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2075       .access = PL1_RW,
2076       .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr),
2077       .writefn = pmsav7_rgnr_write },
2078     REGINFO_SENTINEL
2079 };
2080
2081 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2082     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2083       .access = PL1_RW, .type = ARM_CP_ALIAS,
2084       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2085       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2086     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2087       .access = PL1_RW, .type = ARM_CP_ALIAS,
2088       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2089       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2090     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2091       .access = PL1_RW,
2092       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2093       .resetvalue = 0, },
2094     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2095       .access = PL1_RW,
2096       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2097       .resetvalue = 0, },
2098     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2099       .access = PL1_RW,
2100       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2101     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2102       .access = PL1_RW,
2103       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
2104     /* Protection region base and size registers */
2105     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2106       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2107       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2108     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2109       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2110       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2111     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2112       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2113       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2114     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2115       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2116       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2117     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2118       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2119       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2120     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2121       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2122       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2123     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2124       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2125       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2126     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2127       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2128       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
2129     REGINFO_SENTINEL
2130 };
2131
2132 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2133                                  uint64_t value)
2134 {
2135     TCR *tcr = raw_ptr(env, ri);
2136     int maskshift = extract32(value, 0, 3);
2137
2138     if (!arm_feature(env, ARM_FEATURE_V8)) {
2139         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2140             /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2141              * using Long-desciptor translation table format */
2142             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2143         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2144             /* In an implementation that includes the Security Extensions
2145              * TTBCR has additional fields PD0 [4] and PD1 [5] for
2146              * Short-descriptor translation table format.
2147              */
2148             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2149         } else {
2150             value &= TTBCR_N;
2151         }
2152     }
2153
2154     /* Update the masks corresponding to the TCR bank being written
2155      * Note that we always calculate mask and base_mask, but
2156      * they are only used for short-descriptor tables (ie if EAE is 0);
2157      * for long-descriptor tables the TCR fields are used differently
2158      * and the mask and base_mask values are meaningless.
2159      */
2160     tcr->raw_tcr = value;
2161     tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2162     tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
2163 }
2164
2165 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2166                              uint64_t value)
2167 {
2168     ARMCPU *cpu = arm_env_get_cpu(env);
2169
2170     if (arm_feature(env, ARM_FEATURE_LPAE)) {
2171         /* With LPAE the TTBCR could result in a change of ASID
2172          * via the TTBCR.A1 bit, so do a TLB flush.
2173          */
2174         tlb_flush(CPU(cpu), 1);
2175     }
2176     vmsa_ttbcr_raw_write(env, ri, value);
2177 }
2178
2179 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2180 {
2181     TCR *tcr = raw_ptr(env, ri);
2182
2183     /* Reset both the TCR as well as the masks corresponding to the bank of
2184      * the TCR being reset.
2185      */
2186     tcr->raw_tcr = 0;
2187     tcr->mask = 0;
2188     tcr->base_mask = 0xffffc000u;
2189 }
2190
2191 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2192                                uint64_t value)
2193 {
2194     ARMCPU *cpu = arm_env_get_cpu(env);
2195     TCR *tcr = raw_ptr(env, ri);
2196
2197     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
2198     tlb_flush(CPU(cpu), 1);
2199     tcr->raw_tcr = value;
2200 }
2201
2202 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2203                             uint64_t value)
2204 {
2205     /* 64 bit accesses to the TTBRs can change the ASID and so we
2206      * must flush the TLB.
2207      */
2208     if (cpreg_field_is_64bit(ri)) {
2209         ARMCPU *cpu = arm_env_get_cpu(env);
2210
2211         tlb_flush(CPU(cpu), 1);
2212     }
2213     raw_write(env, ri, value);
2214 }
2215
2216 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2217                         uint64_t value)
2218 {
2219     ARMCPU *cpu = arm_env_get_cpu(env);
2220     CPUState *cs = CPU(cpu);
2221
2222     /* Accesses to VTTBR may change the VMID so we must flush the TLB.  */
2223     if (raw_read(env, ri) != value) {
2224         tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2225                             ARMMMUIdx_S2NS, -1);
2226         raw_write(env, ri, value);
2227     }
2228 }
2229
2230 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
2231     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2232       .access = PL1_RW, .type = ARM_CP_ALIAS,
2233       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
2234                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
2235     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2236       .access = PL1_RW, .resetvalue = 0,
2237       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2238                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
2239     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2240       .access = PL1_RW, .resetvalue = 0,
2241       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2242                              offsetof(CPUARMState, cp15.dfar_ns) } },
2243     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2244       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2245       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2246       .resetvalue = 0, },
2247     REGINFO_SENTINEL
2248 };
2249
2250 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
2251     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2252       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2253       .access = PL1_RW,
2254       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
2255     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
2256       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2257       .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2258       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2259                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
2260     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
2261       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2262       .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2263       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2264                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
2265     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2266       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2267       .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2268       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2269       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
2270     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2271       .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
2272       .raw_writefn = vmsa_ttbcr_raw_write,
2273       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2274                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
2275     REGINFO_SENTINEL
2276 };
2277
2278 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2279                                 uint64_t value)
2280 {
2281     env->cp15.c15_ticonfig = value & 0xe7;
2282     /* The OS_TYPE bit in this register changes the reported CPUID! */
2283     env->cp15.c0_cpuid = (value & (1 << 5)) ?
2284         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
2285 }
2286
2287 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2288                                 uint64_t value)
2289 {
2290     env->cp15.c15_threadid = value & 0xffff;
2291 }
2292
2293 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2294                            uint64_t value)
2295 {
2296     /* Wait-for-interrupt (deprecated) */
2297     cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
2298 }
2299
2300 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2301                                   uint64_t value)
2302 {
2303     /* On OMAP there are registers indicating the max/min index of dcache lines
2304      * containing a dirty line; cache flush operations have to reset these.
2305      */
2306     env->cp15.c15_i_max = 0x000;
2307     env->cp15.c15_i_min = 0xff0;
2308 }
2309
2310 static const ARMCPRegInfo omap_cp_reginfo[] = {
2311     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2312       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
2313       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
2314       .resetvalue = 0, },
2315     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2316       .access = PL1_RW, .type = ARM_CP_NOP },
2317     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2318       .access = PL1_RW,
2319       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2320       .writefn = omap_ticonfig_write },
2321     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2322       .access = PL1_RW,
2323       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2324     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2325       .access = PL1_RW, .resetvalue = 0xff0,
2326       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2327     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2328       .access = PL1_RW,
2329       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2330       .writefn = omap_threadid_write },
2331     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2332       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2333       .type = ARM_CP_NO_RAW,
2334       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2335     /* TODO: Peripheral port remap register:
2336      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2337      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2338      * when MMU is off.
2339      */
2340     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
2341       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
2342       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
2343       .writefn = omap_cachemaint_write },
2344     { .name = "C9", .cp = 15, .crn = 9,
2345       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2346       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
2347     REGINFO_SENTINEL
2348 };
2349
2350 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2351                               uint64_t value)
2352 {
2353     env->cp15.c15_cpar = value & 0x3fff;
2354 }
2355
2356 static const ARMCPRegInfo xscale_cp_reginfo[] = {
2357     { .name = "XSCALE_CPAR",
2358       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2359       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2360       .writefn = xscale_cpar_write, },
2361     { .name = "XSCALE_AUXCR",
2362       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2363       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2364       .resetvalue = 0, },
2365     /* XScale specific cache-lockdown: since we have no cache we NOP these
2366      * and hope the guest does not really rely on cache behaviour.
2367      */
2368     { .name = "XSCALE_LOCK_ICACHE_LINE",
2369       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2370       .access = PL1_W, .type = ARM_CP_NOP },
2371     { .name = "XSCALE_UNLOCK_ICACHE",
2372       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2373       .access = PL1_W, .type = ARM_CP_NOP },
2374     { .name = "XSCALE_DCACHE_LOCK",
2375       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2376       .access = PL1_RW, .type = ARM_CP_NOP },
2377     { .name = "XSCALE_UNLOCK_DCACHE",
2378       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2379       .access = PL1_W, .type = ARM_CP_NOP },
2380     REGINFO_SENTINEL
2381 };
2382
2383 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2384     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2385      * implementation of this implementation-defined space.
2386      * Ideally this should eventually disappear in favour of actually
2387      * implementing the correct behaviour for all cores.
2388      */
2389     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2390       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2391       .access = PL1_RW,
2392       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
2393       .resetvalue = 0 },
2394     REGINFO_SENTINEL
2395 };
2396
2397 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2398     /* Cache status: RAZ because we have no cache so it's always clean */
2399     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
2400       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2401       .resetvalue = 0 },
2402     REGINFO_SENTINEL
2403 };
2404
2405 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2406     /* We never have a a block transfer operation in progress */
2407     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
2408       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2409       .resetvalue = 0 },
2410     /* The cache ops themselves: these all NOP for QEMU */
2411     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2412       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2413     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2414       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2415     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2416       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2417     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2418       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2419     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2420       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2421     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2422       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2423     REGINFO_SENTINEL
2424 };
2425
2426 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2427     /* The cache test-and-clean instructions always return (1 << 30)
2428      * to indicate that there are no dirty cache lines.
2429      */
2430     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2431       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2432       .resetvalue = (1 << 30) },
2433     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2434       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2435       .resetvalue = (1 << 30) },
2436     REGINFO_SENTINEL
2437 };
2438
2439 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2440     /* Ignore ReadBuffer accesses */
2441     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2442       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2443       .access = PL1_RW, .resetvalue = 0,
2444       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2445     REGINFO_SENTINEL
2446 };
2447
2448 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2449 {
2450     ARMCPU *cpu = arm_env_get_cpu(env);
2451     unsigned int cur_el = arm_current_el(env);
2452     bool secure = arm_is_secure(env);
2453
2454     if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2455         return env->cp15.vpidr_el2;
2456     }
2457     return raw_read(env, ri);
2458 }
2459
2460 static uint64_t mpidr_read_val(CPUARMState *env)
2461 {
2462     ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2463     uint64_t mpidr = cpu->mp_affinity;
2464
2465     if (arm_feature(env, ARM_FEATURE_V7MP)) {
2466         mpidr |= (1U << 31);
2467         /* Cores which are uniprocessor (non-coherent)
2468          * but still implement the MP extensions set
2469          * bit 30. (For instance, Cortex-R5).
2470          */
2471         if (cpu->mp_is_up) {
2472             mpidr |= (1u << 30);
2473         }
2474     }
2475     return mpidr;
2476 }
2477
2478 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2479 {
2480     unsigned int cur_el = arm_current_el(env);
2481     bool secure = arm_is_secure(env);
2482
2483     if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2484         return env->cp15.vmpidr_el2;
2485     }
2486     return mpidr_read_val(env);
2487 }
2488
2489 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2490     { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2491       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2492       .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2493     REGINFO_SENTINEL
2494 };
2495
2496 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2497     /* NOP AMAIR0/1 */
2498     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2499       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2500       .access = PL1_RW, .type = ARM_CP_CONST,
2501       .resetvalue = 0 },
2502     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2503     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2504       .access = PL1_RW, .type = ARM_CP_CONST,
2505       .resetvalue = 0 },
2506     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2507       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2508       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2509                              offsetof(CPUARMState, cp15.par_ns)} },
2510     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2511       .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2512       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2513                              offsetof(CPUARMState, cp15.ttbr0_ns) },
2514       .writefn = vmsa_ttbr_write, },
2515     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2516       .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2517       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2518                              offsetof(CPUARMState, cp15.ttbr1_ns) },
2519       .writefn = vmsa_ttbr_write, },
2520     REGINFO_SENTINEL
2521 };
2522
2523 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2524 {
2525     return vfp_get_fpcr(env);
2526 }
2527
2528 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2529                             uint64_t value)
2530 {
2531     vfp_set_fpcr(env, value);
2532 }
2533
2534 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2535 {
2536     return vfp_get_fpsr(env);
2537 }
2538
2539 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2540                             uint64_t value)
2541 {
2542     vfp_set_fpsr(env, value);
2543 }
2544
2545 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
2546 {
2547     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2548         return CP_ACCESS_TRAP;
2549     }
2550     return CP_ACCESS_OK;
2551 }
2552
2553 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2554                             uint64_t value)
2555 {
2556     env->daif = value & PSTATE_DAIF;
2557 }
2558
2559 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2560                                           const ARMCPRegInfo *ri)
2561 {
2562     /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2563      * SCTLR_EL1.UCI is set.
2564      */
2565     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2566         return CP_ACCESS_TRAP;
2567     }
2568     return CP_ACCESS_OK;
2569 }
2570
2571 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2572  * Page D4-1736 (DDI0487A.b)
2573  */
2574
2575 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2576                                     uint64_t value)
2577 {
2578     ARMCPU *cpu = arm_env_get_cpu(env);
2579     CPUState *cs = CPU(cpu);
2580
2581     if (arm_is_secure_below_el3(env)) {
2582         tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2583     } else {
2584         tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2585     }
2586 }
2587
2588 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2589                                       uint64_t value)
2590 {
2591     bool sec = arm_is_secure_below_el3(env);
2592     CPUState *other_cs;
2593
2594     CPU_FOREACH(other_cs) {
2595         if (sec) {
2596             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2597         } else {
2598             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2599                                 ARMMMUIdx_S12NSE0, -1);
2600         }
2601     }
2602 }
2603
2604 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2605                                   uint64_t value)
2606 {
2607     /* Note that the 'ALL' scope must invalidate both stage 1 and
2608      * stage 2 translations, whereas most other scopes only invalidate
2609      * stage 1 translations.
2610      */
2611     ARMCPU *cpu = arm_env_get_cpu(env);
2612     CPUState *cs = CPU(cpu);
2613
2614     if (arm_is_secure_below_el3(env)) {
2615         tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2616     } else {
2617         if (arm_feature(env, ARM_FEATURE_EL2)) {
2618             tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2619                                 ARMMMUIdx_S2NS, -1);
2620         } else {
2621             tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2622         }
2623     }
2624 }
2625
2626 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2627                                   uint64_t value)
2628 {
2629     ARMCPU *cpu = arm_env_get_cpu(env);
2630     CPUState *cs = CPU(cpu);
2631
2632     tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2633 }
2634
2635 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2636                                   uint64_t value)
2637 {
2638     ARMCPU *cpu = arm_env_get_cpu(env);
2639     CPUState *cs = CPU(cpu);
2640
2641     tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2642 }
2643
2644 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2645                                     uint64_t value)
2646 {
2647     /* Note that the 'ALL' scope must invalidate both stage 1 and
2648      * stage 2 translations, whereas most other scopes only invalidate
2649      * stage 1 translations.
2650      */
2651     bool sec = arm_is_secure_below_el3(env);
2652     bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
2653     CPUState *other_cs;
2654
2655     CPU_FOREACH(other_cs) {
2656         if (sec) {
2657             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2658         } else if (has_el2) {
2659             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2660                                 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2661         } else {
2662             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2663                                 ARMMMUIdx_S12NSE0, -1);
2664         }
2665     }
2666 }
2667
2668 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2669                                     uint64_t value)
2670 {
2671     CPUState *other_cs;
2672
2673     CPU_FOREACH(other_cs) {
2674         tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2675     }
2676 }
2677
2678 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2679                                     uint64_t value)
2680 {
2681     CPUState *other_cs;
2682
2683     CPU_FOREACH(other_cs) {
2684         tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2685     }
2686 }
2687
2688 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2689                                  uint64_t value)
2690 {
2691     /* Invalidate by VA, EL1&0 (AArch64 version).
2692      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2693      * since we don't support flush-for-specific-ASID-only or
2694      * flush-last-level-only.
2695      */
2696     ARMCPU *cpu = arm_env_get_cpu(env);
2697     CPUState *cs = CPU(cpu);
2698     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2699
2700     if (arm_is_secure_below_el3(env)) {
2701         tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2702                                  ARMMMUIdx_S1SE0, -1);
2703     } else {
2704         tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2705                                  ARMMMUIdx_S12NSE0, -1);
2706     }
2707 }
2708
2709 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2710                                  uint64_t value)
2711 {
2712     /* Invalidate by VA, EL2
2713      * Currently handles both VAE2 and VALE2, since we don't support
2714      * flush-last-level-only.
2715      */
2716     ARMCPU *cpu = arm_env_get_cpu(env);
2717     CPUState *cs = CPU(cpu);
2718     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2719
2720     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2721 }
2722
2723 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2724                                  uint64_t value)
2725 {
2726     /* Invalidate by VA, EL3
2727      * Currently handles both VAE3 and VALE3, since we don't support
2728      * flush-last-level-only.
2729      */
2730     ARMCPU *cpu = arm_env_get_cpu(env);
2731     CPUState *cs = CPU(cpu);
2732     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2733
2734     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2735 }
2736
2737 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2738                                    uint64_t value)
2739 {
2740     bool sec = arm_is_secure_below_el3(env);
2741     CPUState *other_cs;
2742     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2743
2744     CPU_FOREACH(other_cs) {
2745         if (sec) {
2746             tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2747                                      ARMMMUIdx_S1SE0, -1);
2748         } else {
2749             tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2750                                      ARMMMUIdx_S12NSE0, -1);
2751         }
2752     }
2753 }
2754
2755 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2756                                    uint64_t value)
2757 {
2758     CPUState *other_cs;
2759     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2760
2761     CPU_FOREACH(other_cs) {
2762         tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
2763     }
2764 }
2765
2766 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2767                                    uint64_t value)
2768 {
2769     CPUState *other_cs;
2770     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2771
2772     CPU_FOREACH(other_cs) {
2773         tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2774     }
2775 }
2776
2777 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2778                                     uint64_t value)
2779 {
2780     /* Invalidate by IPA. This has to invalidate any structures that
2781      * contain only stage 2 translation information, but does not need
2782      * to apply to structures that contain combined stage 1 and stage 2
2783      * translation information.
2784      * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2785      */
2786     ARMCPU *cpu = arm_env_get_cpu(env);
2787     CPUState *cs = CPU(cpu);
2788     uint64_t pageaddr;
2789
2790     if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2791         return;
2792     }
2793
2794     pageaddr = sextract64(value << 12, 0, 48);
2795
2796     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2797 }
2798
2799 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2800                                       uint64_t value)
2801 {
2802     CPUState *other_cs;
2803     uint64_t pageaddr;
2804
2805     if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2806         return;
2807     }
2808
2809     pageaddr = sextract64(value << 12, 0, 48);
2810
2811     CPU_FOREACH(other_cs) {
2812         tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2813     }
2814 }
2815
2816 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
2817 {
2818     /* We don't implement EL2, so the only control on DC ZVA is the
2819      * bit in the SCTLR which can prohibit access for EL0.
2820      */
2821     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
2822         return CP_ACCESS_TRAP;
2823     }
2824     return CP_ACCESS_OK;
2825 }
2826
2827 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
2828 {
2829     ARMCPU *cpu = arm_env_get_cpu(env);
2830     int dzp_bit = 1 << 4;
2831
2832     /* DZP indicates whether DC ZVA access is allowed */
2833     if (aa64_zva_access(env, NULL) == CP_ACCESS_OK) {
2834         dzp_bit = 0;
2835     }
2836     return cpu->dcz_blocksize | dzp_bit;
2837 }
2838
2839 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2840 {
2841     if (!(env->pstate & PSTATE_SP)) {
2842         /* Access to SP_EL0 is undefined if it's being used as
2843          * the stack pointer.
2844          */
2845         return CP_ACCESS_TRAP_UNCATEGORIZED;
2846     }
2847     return CP_ACCESS_OK;
2848 }
2849
2850 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
2851 {
2852     return env->pstate & PSTATE_SP;
2853 }
2854
2855 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
2856 {
2857     update_spsel(env, val);
2858 }
2859
2860 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2861                         uint64_t value)
2862 {
2863     ARMCPU *cpu = arm_env_get_cpu(env);
2864
2865     if (raw_read(env, ri) == value) {
2866         /* Skip the TLB flush if nothing actually changed; Linux likes
2867          * to do a lot of pointless SCTLR writes.
2868          */
2869         return;
2870     }
2871
2872     raw_write(env, ri, value);
2873     /* ??? Lots of these bits are not implemented.  */
2874     /* This may enable/disable the MMU, so do a TLB flush.  */
2875     tlb_flush(CPU(cpu), 1);
2876 }
2877
2878 static const ARMCPRegInfo v8_cp_reginfo[] = {
2879     /* Minimal set of EL0-visible registers. This will need to be expanded
2880      * significantly for system emulation of AArch64 CPUs.
2881      */
2882     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
2883       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
2884       .access = PL0_RW, .type = ARM_CP_NZCV },
2885     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
2886       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
2887       .type = ARM_CP_NO_RAW,
2888       .access = PL0_RW, .accessfn = aa64_daif_access,
2889       .fieldoffset = offsetof(CPUARMState, daif),
2890       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
2891     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
2892       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
2893       .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
2894     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
2895       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
2896       .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
2897     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
2898       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
2899       .access = PL0_R, .type = ARM_CP_NO_RAW,
2900       .readfn = aa64_dczid_read },
2901     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
2902       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
2903       .access = PL0_W, .type = ARM_CP_DC_ZVA,
2904 #ifndef CONFIG_USER_ONLY
2905       /* Avoid overhead of an access check that always passes in user-mode */
2906       .accessfn = aa64_zva_access,
2907 #endif
2908     },
2909     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
2910       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
2911       .access = PL1_R, .type = ARM_CP_CURRENTEL },
2912     /* Cache ops: all NOPs since we don't emulate caches */
2913     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
2914       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2915       .access = PL1_W, .type = ARM_CP_NOP },
2916     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
2917       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2918       .access = PL1_W, .type = ARM_CP_NOP },
2919     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
2920       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
2921       .access = PL0_W, .type = ARM_CP_NOP,
2922       .accessfn = aa64_cacheop_access },
2923     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
2924       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2925       .access = PL1_W, .type = ARM_CP_NOP },
2926     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
2927       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2928       .access = PL1_W, .type = ARM_CP_NOP },
2929     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
2930       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
2931       .access = PL0_W, .type = ARM_CP_NOP,
2932       .accessfn = aa64_cacheop_access },
2933     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
2934       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2935       .access = PL1_W, .type = ARM_CP_NOP },
2936     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
2937       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
2938       .access = PL0_W, .type = ARM_CP_NOP,
2939       .accessfn = aa64_cacheop_access },
2940     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
2941       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
2942       .access = PL0_W, .type = ARM_CP_NOP,
2943       .accessfn = aa64_cacheop_access },
2944     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
2945       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2946       .access = PL1_W, .type = ARM_CP_NOP },
2947     /* TLBI operations */
2948     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
2949       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2950       .access = PL1_W, .type = ARM_CP_NO_RAW,
2951       .writefn = tlbi_aa64_vmalle1is_write },
2952     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
2953       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2954       .access = PL1_W, .type = ARM_CP_NO_RAW,
2955       .writefn = tlbi_aa64_vae1is_write },
2956     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
2957       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2958       .access = PL1_W, .type = ARM_CP_NO_RAW,
2959       .writefn = tlbi_aa64_vmalle1is_write },
2960     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
2961       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2962       .access = PL1_W, .type = ARM_CP_NO_RAW,
2963       .writefn = tlbi_aa64_vae1is_write },
2964     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
2965       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2966       .access = PL1_W, .type = ARM_CP_NO_RAW,
2967       .writefn = tlbi_aa64_vae1is_write },
2968     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
2969       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2970       .access = PL1_W, .type = ARM_CP_NO_RAW,
2971       .writefn = tlbi_aa64_vae1is_write },
2972     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
2973       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2974       .access = PL1_W, .type = ARM_CP_NO_RAW,
2975       .writefn = tlbi_aa64_vmalle1_write },
2976     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
2977       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2978       .access = PL1_W, .type = ARM_CP_NO_RAW,
2979       .writefn = tlbi_aa64_vae1_write },
2980     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
2981       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2982       .access = PL1_W, .type = ARM_CP_NO_RAW,
2983       .writefn = tlbi_aa64_vmalle1_write },
2984     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
2985       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2986       .access = PL1_W, .type = ARM_CP_NO_RAW,
2987       .writefn = tlbi_aa64_vae1_write },
2988     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
2989       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2990       .access = PL1_W, .type = ARM_CP_NO_RAW,
2991       .writefn = tlbi_aa64_vae1_write },
2992     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
2993       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2994       .access = PL1_W, .type = ARM_CP_NO_RAW,
2995       .writefn = tlbi_aa64_vae1_write },
2996     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
2997       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
2998       .access = PL2_W, .type = ARM_CP_NO_RAW,
2999       .writefn = tlbi_aa64_ipas2e1is_write },
3000     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3001       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3002       .access = PL2_W, .type = ARM_CP_NO_RAW,
3003       .writefn = tlbi_aa64_ipas2e1is_write },
3004     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3005       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3006       .access = PL2_W, .type = ARM_CP_NO_RAW,
3007       .writefn = tlbi_aa64_alle1is_write },
3008     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3009       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3010       .access = PL2_W, .type = ARM_CP_NO_RAW,
3011       .writefn = tlbi_aa64_alle1is_write },
3012     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3013       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3014       .access = PL2_W, .type = ARM_CP_NO_RAW,
3015       .writefn = tlbi_aa64_ipas2e1_write },
3016     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3017       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3018       .access = PL2_W, .type = ARM_CP_NO_RAW,
3019       .writefn = tlbi_aa64_ipas2e1_write },
3020     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3021       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3022       .access = PL2_W, .type = ARM_CP_NO_RAW,
3023       .writefn = tlbi_aa64_alle1_write },
3024     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3025       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3026       .access = PL2_W, .type = ARM_CP_NO_RAW,
3027       .writefn = tlbi_aa64_alle1is_write },
3028 #ifndef CONFIG_USER_ONLY
3029     /* 64 bit address translation operations */
3030     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3031       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3032       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3033     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3034       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3035       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3036     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3037       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3038       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3039     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3040       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3041       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3042     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3043       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3044       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3045     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3046       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3047       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3048     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3049       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3050       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3051     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3052       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3053       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3054     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3055     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3056       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3057       .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3058     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3059       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3060       .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3061     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3062       .type = ARM_CP_ALIAS,
3063       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3064       .access = PL1_RW, .resetvalue = 0,
3065       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3066       .writefn = par_write },
3067 #endif
3068     /* TLB invalidate last level of translation table walk */
3069     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3070       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3071     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3072       .type = ARM_CP_NO_RAW, .access = PL1_W,
3073       .writefn = tlbimvaa_is_write },
3074     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3075       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3076     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3077       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3078     /* 32 bit cache operations */
3079     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3080       .type = ARM_CP_NOP, .access = PL1_W },
3081     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3082       .type = ARM_CP_NOP, .access = PL1_W },
3083     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3084       .type = ARM_CP_NOP, .access = PL1_W },
3085     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3086       .type = ARM_CP_NOP, .access = PL1_W },
3087     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3088       .type = ARM_CP_NOP, .access = PL1_W },
3089     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3090       .type = ARM_CP_NOP, .access = PL1_W },
3091     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3092       .type = ARM_CP_NOP, .access = PL1_W },
3093     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3094       .type = ARM_CP_NOP, .access = PL1_W },
3095     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3096       .type = ARM_CP_NOP, .access = PL1_W },
3097     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3098       .type = ARM_CP_NOP, .access = PL1_W },
3099     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3100       .type = ARM_CP_NOP, .access = PL1_W },
3101     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3102       .type = ARM_CP_NOP, .access = PL1_W },
3103     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3104       .type = ARM_CP_NOP, .access = PL1_W },
3105     /* MMU Domain access control / MPU write buffer control */
3106     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3107       .access = PL1_RW, .resetvalue = 0,
3108       .writefn = dacr_write, .raw_writefn = raw_write,
3109       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3110                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3111     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3112       .type = ARM_CP_ALIAS,
3113       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3114       .access = PL1_RW,
3115       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3116     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3117       .type = ARM_CP_ALIAS,
3118       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3119       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[1]) },
3120     /* We rely on the access checks not allowing the guest to write to the
3121      * state field when SPSel indicates that it's being used as the stack
3122      * pointer.
3123      */
3124     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3125       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3126       .access = PL1_RW, .accessfn = sp_el0_access,
3127       .type = ARM_CP_ALIAS,
3128       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3129     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3130       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3131       .access = PL2_RW, .type = ARM_CP_ALIAS,
3132       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3133     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3134       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3135       .type = ARM_CP_NO_RAW,
3136       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3137     REGINFO_SENTINEL
3138 };
3139
3140 /* Used to describe the behaviour of EL2 regs when EL2 does not exist.  */
3141 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3142     { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3143       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3144       .access = PL2_RW,
3145       .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3146     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3147       .type = ARM_CP_NO_RAW,
3148       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3149       .access = PL2_RW,
3150       .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3151     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3152       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3153       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3154     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3155       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3156       .access = PL2_RW, .type = ARM_CP_CONST,
3157       .resetvalue = 0 },
3158     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3159       .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3160       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3161     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3162       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3163       .access = PL2_RW, .type = ARM_CP_CONST,
3164       .resetvalue = 0 },
3165     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3166       .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3167       .access = PL2_RW, .type = ARM_CP_CONST,
3168       .resetvalue = 0 },
3169     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3170       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3171       .access = PL2_RW, .type = ARM_CP_CONST,
3172       .resetvalue = 0 },
3173     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3174       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3175       .access = PL2_RW, .type = ARM_CP_CONST,
3176       .resetvalue = 0 },
3177     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3178       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3179       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3180     { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3181       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3182       .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3183       .type = ARM_CP_CONST, .resetvalue = 0 },
3184     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3185       .cp = 15, .opc1 = 6, .crm = 2,
3186       .access = PL2_RW, .accessfn = access_el3_aa32ns,
3187       .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3188     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3189       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3190       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3191     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3192       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3193       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3194     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3195       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3196       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3197     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3198       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3199       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3200     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3201       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3202       .resetvalue = 0 },
3203     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3204       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3205       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3206     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3207       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3208       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3209     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3210       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3211       .resetvalue = 0 },
3212     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3213       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3214       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3215     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3216       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3217       .resetvalue = 0 },
3218     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3219       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3220       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3221     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3222       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3223       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3224     REGINFO_SENTINEL
3225 };
3226
3227 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3228 {
3229     ARMCPU *cpu = arm_env_get_cpu(env);
3230     uint64_t valid_mask = HCR_MASK;
3231
3232     if (arm_feature(env, ARM_FEATURE_EL3)) {
3233         valid_mask &= ~HCR_HCD;
3234     } else {
3235         valid_mask &= ~HCR_TSC;
3236     }
3237
3238     /* Clear RES0 bits.  */
3239     value &= valid_mask;
3240
3241     /* These bits change the MMU setup:
3242      * HCR_VM enables stage 2 translation
3243      * HCR_PTW forbids certain page-table setups
3244      * HCR_DC Disables stage1 and enables stage2 translation
3245      */
3246     if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3247         tlb_flush(CPU(cpu), 1);
3248     }
3249     raw_write(env, ri, value);
3250 }
3251
3252 static const ARMCPRegInfo el2_cp_reginfo[] = {
3253     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3254       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3255       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3256       .writefn = hcr_write },
3257     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3258       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3259       .access = PL2_RW, .resetvalue = 0,
3260       .writefn = dacr_write, .raw_writefn = raw_write,
3261       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3262     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3263       .type = ARM_CP_ALIAS,
3264       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3265       .access = PL2_RW,
3266       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3267     { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3268       .type = ARM_CP_ALIAS,
3269       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3270       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3271     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3272       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3273       .access = PL2_RW, .resetvalue = 0,
3274       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3275     { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3276       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3277       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3278     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3279       .type = ARM_CP_ALIAS,
3280       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3281       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
3282     { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3283       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3284       .access = PL2_RW, .writefn = vbar_write,
3285       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3286       .resetvalue = 0 },
3287     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3288       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3289       .access = PL3_RW, .type = ARM_CP_ALIAS,
3290       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3291     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3292       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3293       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3294       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3295     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3296       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3297       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3298       .resetvalue = 0 },
3299     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3300       .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3301       .access = PL2_RW, .type = ARM_CP_ALIAS,
3302       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3303     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3304       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3305       .access = PL2_RW, .type = ARM_CP_CONST,
3306       .resetvalue = 0 },
3307     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3308     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3309       .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3310       .access = PL2_RW, .type = ARM_CP_CONST,
3311       .resetvalue = 0 },
3312     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3313       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3314       .access = PL2_RW, .type = ARM_CP_CONST,
3315       .resetvalue = 0 },
3316     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3317       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3318       .access = PL2_RW, .type = ARM_CP_CONST,
3319       .resetvalue = 0 },
3320     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3321       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3322       .access = PL2_RW, .writefn = vmsa_tcr_el1_write,
3323       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3324       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3325     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3326       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3327       .access = PL2_RW, .accessfn = access_el3_aa32ns,
3328       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3329     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3330       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3331       .access = PL2_RW, .type = ARM_CP_ALIAS,
3332       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3333     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3334       .cp = 15, .opc1 = 6, .crm = 2,
3335       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3336       .access = PL2_RW, .accessfn = access_el3_aa32ns,
3337       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3338       .writefn = vttbr_write },
3339     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3340       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3341       .access = PL2_RW, .writefn = vttbr_write,
3342       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3343     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3344       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3345       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3346       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3347     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3348       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3349       .access = PL2_RW, .resetvalue = 0,
3350       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3351     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3352       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3353       .access = PL2_RW, .resetvalue = 0,
3354       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3355     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3356       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3357       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3358     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3359       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3360       .type = ARM_CP_NO_RAW, .access = PL2_W,
3361       .writefn = tlbi_aa64_alle2_write },
3362     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3363       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3364       .type = ARM_CP_NO_RAW, .access = PL2_W,
3365       .writefn = tlbi_aa64_vae2_write },
3366     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3367       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3368       .access = PL2_W, .type = ARM_CP_NO_RAW,
3369       .writefn = tlbi_aa64_vae2_write },
3370     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3371       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3372       .access = PL2_W, .type = ARM_CP_NO_RAW,
3373       .writefn = tlbi_aa64_alle2is_write },
3374     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3375       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3376       .type = ARM_CP_NO_RAW, .access = PL2_W,
3377       .writefn = tlbi_aa64_vae2is_write },
3378     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3379       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3380       .access = PL2_W, .type = ARM_CP_NO_RAW,
3381       .writefn = tlbi_aa64_vae2is_write },
3382 #ifndef CONFIG_USER_ONLY
3383     /* Unlike the other EL2-related AT operations, these must
3384      * UNDEF from EL3 if EL2 is not implemented, which is why we
3385      * define them here rather than with the rest of the AT ops.
3386      */
3387     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3388       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3389       .access = PL2_W, .accessfn = at_s1e2_access,
3390       .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3391     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3392       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3393       .access = PL2_W, .accessfn = at_s1e2_access,
3394       .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3395     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3396      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3397      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3398      * to behave as if SCR.NS was 1.
3399      */
3400     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3401       .access = PL2_W,
3402       .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3403     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3404       .access = PL2_W,
3405       .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3406     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3407       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3408       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3409        * reset values as IMPDEF. We choose to reset to 3 to comply with
3410        * both ARMv7 and ARMv8.
3411        */
3412       .access = PL2_RW, .resetvalue = 3,
3413       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
3414     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3415       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3416       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3417       .writefn = gt_cntvoff_write,
3418       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3419     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3420       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3421       .writefn = gt_cntvoff_write,
3422       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3423     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3424       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3425       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3426       .type = ARM_CP_IO, .access = PL2_RW,
3427       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3428     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3429       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3430       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3431       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3432     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3433       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3434       .type = ARM_CP_IO, .access = PL2_RW,
3435       .resetfn = gt_hyp_timer_reset,
3436       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3437     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3438       .type = ARM_CP_IO,
3439       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3440       .access = PL2_RW,
3441       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3442       .resetvalue = 0,
3443       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
3444 #endif
3445     REGINFO_SENTINEL
3446 };
3447
3448 static const ARMCPRegInfo el3_cp_reginfo[] = {
3449     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3450       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3451       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3452       .resetvalue = 0, .writefn = scr_write },
3453     { .name = "SCR",  .type = ARM_CP_ALIAS,
3454       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
3455       .access = PL3_RW, .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
3456       .writefn = scr_write },
3457     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3458       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3459       .access = PL3_RW, .resetvalue = 0,
3460       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3461     { .name = "SDER",
3462       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3463       .access = PL3_RW, .resetvalue = 0,
3464       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
3465       /* TODO: Implement NSACR trapping of secure EL1 accesses to EL3 */
3466     { .name = "NSACR", .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
3467       .access = PL3_W | PL1_R, .resetvalue = 0,
3468       .fieldoffset = offsetof(CPUARMState, cp15.nsacr) },
3469     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
3470       .access = PL3_RW, .writefn = vbar_write, .resetvalue = 0,
3471       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
3472     { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
3473       .type = ARM_CP_ALIAS, /* reset handled by AArch32 view */
3474       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
3475       .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3476       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) },
3477     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3478       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3479       .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3480       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
3481     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3482       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
3483       .access = PL3_RW, .writefn = vmsa_tcr_el1_write,
3484       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3485       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
3486     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
3487       .type = ARM_CP_ALIAS,
3488       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3489       .access = PL3_RW,
3490       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
3491     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
3492       .type = ARM_CP_ALIAS,
3493       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3494       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
3495     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3496       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3497       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
3498     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
3499       .type = ARM_CP_ALIAS,
3500       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
3501       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
3502     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3503       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3504       .access = PL3_RW, .writefn = vbar_write,
3505       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3506       .resetvalue = 0 },
3507     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3508       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3509       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3510       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
3511     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3512       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3513       .access = PL3_RW, .resetvalue = 0,
3514       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
3515     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3516       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3517       .access = PL3_RW, .type = ARM_CP_CONST,
3518       .resetvalue = 0 },
3519     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3520       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3521       .access = PL3_RW, .type = ARM_CP_CONST,
3522       .resetvalue = 0 },
3523     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3524       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3525       .access = PL3_RW, .type = ARM_CP_CONST,
3526       .resetvalue = 0 },
3527     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3528       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3529       .access = PL3_W, .type = ARM_CP_NO_RAW,
3530       .writefn = tlbi_aa64_alle3is_write },
3531     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3532       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3533       .access = PL3_W, .type = ARM_CP_NO_RAW,
3534       .writefn = tlbi_aa64_vae3is_write },
3535     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3536       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3537       .access = PL3_W, .type = ARM_CP_NO_RAW,
3538       .writefn = tlbi_aa64_vae3is_write },
3539     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3540       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3541       .access = PL3_W, .type = ARM_CP_NO_RAW,
3542       .writefn = tlbi_aa64_alle3_write },
3543     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3544       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3545       .access = PL3_W, .type = ARM_CP_NO_RAW,
3546       .writefn = tlbi_aa64_vae3_write },
3547     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3548       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3549       .access = PL3_W, .type = ARM_CP_NO_RAW,
3550       .writefn = tlbi_aa64_vae3_write },
3551     REGINFO_SENTINEL
3552 };
3553
3554 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
3555 {
3556     /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3557      * but the AArch32 CTR has its own reginfo struct)
3558      */
3559     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
3560         return CP_ACCESS_TRAP;
3561     }
3562     return CP_ACCESS_OK;
3563 }
3564
3565 static const ARMCPRegInfo debug_cp_reginfo[] = {
3566     /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
3567      * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3568      * unlike DBGDRAR it is never accessible from EL0.
3569      * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3570      * accessor.
3571      */
3572     { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
3573       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3574     { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3575       .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
3576       .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3577     { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3578       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3579     /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
3580     { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3581       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
3582       .access = PL1_RW,
3583       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3584       .resetvalue = 0 },
3585     /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3586      * We don't implement the configurable EL0 access.
3587      */
3588     { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3589       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3590       .type = ARM_CP_ALIAS,
3591       .access = PL1_R,
3592       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
3593     /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
3594     { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3595       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
3596       .access = PL1_W, .type = ARM_CP_NOP },
3597     /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3598     { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3599       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
3600       .access = PL1_RW, .type = ARM_CP_NOP },
3601     /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3602      * implement vector catch debug events yet.
3603      */
3604     { .name = "DBGVCR",
3605       .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
3606       .access = PL1_RW, .type = ARM_CP_NOP },
3607     REGINFO_SENTINEL
3608 };
3609
3610 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3611     /* 64 bit access versions of the (dummy) debug registers */
3612     { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3613       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3614     { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3615       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3616     REGINFO_SENTINEL
3617 };
3618
3619 void hw_watchpoint_update(ARMCPU *cpu, int n)
3620 {
3621     CPUARMState *env = &cpu->env;
3622     vaddr len = 0;
3623     vaddr wvr = env->cp15.dbgwvr[n];
3624     uint64_t wcr = env->cp15.dbgwcr[n];
3625     int mask;
3626     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3627
3628     if (env->cpu_watchpoint[n]) {
3629         cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3630         env->cpu_watchpoint[n] = NULL;
3631     }
3632
3633     if (!extract64(wcr, 0, 1)) {
3634         /* E bit clear : watchpoint disabled */
3635         return;
3636     }
3637
3638     switch (extract64(wcr, 3, 2)) {
3639     case 0:
3640         /* LSC 00 is reserved and must behave as if the wp is disabled */
3641         return;
3642     case 1:
3643         flags |= BP_MEM_READ;
3644         break;
3645     case 2:
3646         flags |= BP_MEM_WRITE;
3647         break;
3648     case 3:
3649         flags |= BP_MEM_ACCESS;
3650         break;
3651     }
3652
3653     /* Attempts to use both MASK and BAS fields simultaneously are
3654      * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3655      * thus generating a watchpoint for every byte in the masked region.
3656      */
3657     mask = extract64(wcr, 24, 4);
3658     if (mask == 1 || mask == 2) {
3659         /* Reserved values of MASK; we must act as if the mask value was
3660          * some non-reserved value, or as if the watchpoint were disabled.
3661          * We choose the latter.
3662          */
3663         return;
3664     } else if (mask) {
3665         /* Watchpoint covers an aligned area up to 2GB in size */
3666         len = 1ULL << mask;
3667         /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3668          * whether the watchpoint fires when the unmasked bits match; we opt
3669          * to generate the exceptions.
3670          */
3671         wvr &= ~(len - 1);
3672     } else {
3673         /* Watchpoint covers bytes defined by the byte address select bits */
3674         int bas = extract64(wcr, 5, 8);
3675         int basstart;
3676
3677         if (bas == 0) {
3678             /* This must act as if the watchpoint is disabled */
3679             return;
3680         }
3681
3682         if (extract64(wvr, 2, 1)) {
3683             /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3684              * ignored, and BAS[3:0] define which bytes to watch.
3685              */
3686             bas &= 0xf;
3687         }
3688         /* The BAS bits are supposed to be programmed to indicate a contiguous
3689          * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3690          * we fire for each byte in the word/doubleword addressed by the WVR.
3691          * We choose to ignore any non-zero bits after the first range of 1s.
3692          */
3693         basstart = ctz32(bas);
3694         len = cto32(bas >> basstart);
3695         wvr += basstart;
3696     }
3697
3698     cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
3699                           &env->cpu_watchpoint[n]);
3700 }
3701
3702 void hw_watchpoint_update_all(ARMCPU *cpu)
3703 {
3704     int i;
3705     CPUARMState *env = &cpu->env;
3706
3707     /* Completely clear out existing QEMU watchpoints and our array, to
3708      * avoid possible stale entries following migration load.
3709      */
3710     cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
3711     memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
3712
3713     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
3714         hw_watchpoint_update(cpu, i);
3715     }
3716 }
3717
3718 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3719                          uint64_t value)
3720 {
3721     ARMCPU *cpu = arm_env_get_cpu(env);
3722     int i = ri->crm;
3723
3724     /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
3725      * register reads and behaves as if values written are sign extended.
3726      * Bits [1:0] are RES0.
3727      */
3728     value = sextract64(value, 0, 49) & ~3ULL;
3729
3730     raw_write(env, ri, value);
3731     hw_watchpoint_update(cpu, i);
3732 }
3733
3734 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3735                          uint64_t value)
3736 {
3737     ARMCPU *cpu = arm_env_get_cpu(env);
3738     int i = ri->crm;
3739
3740     raw_write(env, ri, value);
3741     hw_watchpoint_update(cpu, i);
3742 }
3743
3744 void hw_breakpoint_update(ARMCPU *cpu, int n)
3745 {
3746     CPUARMState *env = &cpu->env;
3747     uint64_t bvr = env->cp15.dbgbvr[n];
3748     uint64_t bcr = env->cp15.dbgbcr[n];
3749     vaddr addr;
3750     int bt;
3751     int flags = BP_CPU;
3752
3753     if (env->cpu_breakpoint[n]) {
3754         cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
3755         env->cpu_breakpoint[n] = NULL;
3756     }
3757
3758     if (!extract64(bcr, 0, 1)) {
3759         /* E bit clear : watchpoint disabled */
3760         return;
3761     }
3762
3763     bt = extract64(bcr, 20, 4);
3764
3765     switch (bt) {
3766     case 4: /* unlinked address mismatch (reserved if AArch64) */
3767     case 5: /* linked address mismatch (reserved if AArch64) */
3768         qemu_log_mask(LOG_UNIMP,
3769                       "arm: address mismatch breakpoint types not implemented");
3770         return;
3771     case 0: /* unlinked address match */
3772     case 1: /* linked address match */
3773     {
3774         /* Bits [63:49] are hardwired to the value of bit [48]; that is,
3775          * we behave as if the register was sign extended. Bits [1:0] are
3776          * RES0. The BAS field is used to allow setting breakpoints on 16
3777          * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
3778          * a bp will fire if the addresses covered by the bp and the addresses
3779          * covered by the insn overlap but the insn doesn't start at the
3780          * start of the bp address range. We choose to require the insn and
3781          * the bp to have the same address. The constraints on writing to
3782          * BAS enforced in dbgbcr_write mean we have only four cases:
3783          *  0b0000  => no breakpoint
3784          *  0b0011  => breakpoint on addr
3785          *  0b1100  => breakpoint on addr + 2
3786          *  0b1111  => breakpoint on addr
3787          * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
3788          */
3789         int bas = extract64(bcr, 5, 4);
3790         addr = sextract64(bvr, 0, 49) & ~3ULL;
3791         if (bas == 0) {
3792             return;
3793         }
3794         if (bas == 0xc) {
3795             addr += 2;
3796         }
3797         break;
3798     }
3799     case 2: /* unlinked context ID match */
3800     case 8: /* unlinked VMID match (reserved if no EL2) */
3801     case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
3802         qemu_log_mask(LOG_UNIMP,
3803                       "arm: unlinked context breakpoint types not implemented");
3804         return;
3805     case 9: /* linked VMID match (reserved if no EL2) */
3806     case 11: /* linked context ID and VMID match (reserved if no EL2) */
3807     case 3: /* linked context ID match */
3808     default:
3809         /* We must generate no events for Linked context matches (unless
3810          * they are linked to by some other bp/wp, which is handled in
3811          * updates for the linking bp/wp). We choose to also generate no events
3812          * for reserved values.
3813          */
3814         return;
3815     }
3816
3817     cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
3818 }
3819
3820 void hw_breakpoint_update_all(ARMCPU *cpu)
3821 {
3822     int i;
3823     CPUARMState *env = &cpu->env;
3824
3825     /* Completely clear out existing QEMU breakpoints and our array, to
3826      * avoid possible stale entries following migration load.
3827      */
3828     cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
3829     memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
3830
3831     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
3832         hw_breakpoint_update(cpu, i);
3833     }
3834 }
3835
3836 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3837                          uint64_t value)
3838 {
3839     ARMCPU *cpu = arm_env_get_cpu(env);
3840     int i = ri->crm;
3841
3842     raw_write(env, ri, value);
3843     hw_breakpoint_update(cpu, i);
3844 }
3845
3846 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3847                          uint64_t value)
3848 {
3849     ARMCPU *cpu = arm_env_get_cpu(env);
3850     int i = ri->crm;
3851
3852     /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
3853      * copy of BAS[0].
3854      */
3855     value = deposit64(value, 6, 1, extract64(value, 5, 1));
3856     value = deposit64(value, 8, 1, extract64(value, 7, 1));
3857
3858     raw_write(env, ri, value);
3859     hw_breakpoint_update(cpu, i);
3860 }
3861
3862 static void define_debug_regs(ARMCPU *cpu)
3863 {
3864     /* Define v7 and v8 architectural debug registers.
3865      * These are just dummy implementations for now.
3866      */
3867     int i;
3868     int wrps, brps, ctx_cmps;
3869     ARMCPRegInfo dbgdidr = {
3870         .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
3871         .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
3872     };
3873
3874     /* Note that all these register fields hold "number of Xs minus 1". */
3875     brps = extract32(cpu->dbgdidr, 24, 4);
3876     wrps = extract32(cpu->dbgdidr, 28, 4);
3877     ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
3878
3879     assert(ctx_cmps <= brps);
3880
3881     /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
3882      * of the debug registers such as number of breakpoints;
3883      * check that if they both exist then they agree.
3884      */
3885     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
3886         assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
3887         assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3888         assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
3889     }
3890
3891     define_one_arm_cp_reg(cpu, &dbgdidr);
3892     define_arm_cp_regs(cpu, debug_cp_reginfo);
3893
3894     if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
3895         define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
3896     }
3897
3898     for (i = 0; i < brps + 1; i++) {
3899         ARMCPRegInfo dbgregs[] = {
3900             { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
3901               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
3902               .access = PL1_RW,
3903               .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
3904               .writefn = dbgbvr_write, .raw_writefn = raw_write
3905             },
3906             { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
3907               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
3908               .access = PL1_RW,
3909               .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
3910               .writefn = dbgbcr_write, .raw_writefn = raw_write
3911             },
3912             REGINFO_SENTINEL
3913         };
3914         define_arm_cp_regs(cpu, dbgregs);
3915     }
3916
3917     for (i = 0; i < wrps + 1; i++) {
3918         ARMCPRegInfo dbgregs[] = {
3919             { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
3920               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
3921               .access = PL1_RW,
3922               .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
3923               .writefn = dbgwvr_write, .raw_writefn = raw_write
3924             },
3925             { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
3926               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
3927               .access = PL1_RW,
3928               .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
3929               .writefn = dbgwcr_write, .raw_writefn = raw_write
3930             },
3931             REGINFO_SENTINEL
3932         };
3933         define_arm_cp_regs(cpu, dbgregs);
3934     }
3935 }
3936
3937 void register_cp_regs_for_features(ARMCPU *cpu)
3938 {
3939     /* Register all the coprocessor registers based on feature bits */
3940     CPUARMState *env = &cpu->env;
3941     if (arm_feature(env, ARM_FEATURE_M)) {
3942         /* M profile has no coprocessor registers */
3943         return;
3944     }
3945
3946     define_arm_cp_regs(cpu, cp_reginfo);
3947     if (!arm_feature(env, ARM_FEATURE_V8)) {
3948         /* Must go early as it is full of wildcards that may be
3949          * overridden by later definitions.
3950          */
3951         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
3952     }
3953
3954     if (arm_feature(env, ARM_FEATURE_V6)) {
3955         /* The ID registers all have impdef reset values */
3956         ARMCPRegInfo v6_idregs[] = {
3957             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
3958               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3959               .access = PL1_R, .type = ARM_CP_CONST,
3960               .resetvalue = cpu->id_pfr0 },
3961             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
3962               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
3963               .access = PL1_R, .type = ARM_CP_CONST,
3964               .resetvalue = cpu->id_pfr1 },
3965             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
3966               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
3967               .access = PL1_R, .type = ARM_CP_CONST,
3968               .resetvalue = cpu->id_dfr0 },
3969             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
3970               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
3971               .access = PL1_R, .type = ARM_CP_CONST,
3972               .resetvalue = cpu->id_afr0 },
3973             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
3974               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
3975               .access = PL1_R, .type = ARM_CP_CONST,
3976               .resetvalue = cpu->id_mmfr0 },
3977             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
3978               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
3979               .access = PL1_R, .type = ARM_CP_CONST,
3980               .resetvalue = cpu->id_mmfr1 },
3981             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
3982               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
3983               .access = PL1_R, .type = ARM_CP_CONST,
3984               .resetvalue = cpu->id_mmfr2 },
3985             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
3986               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
3987               .access = PL1_R, .type = ARM_CP_CONST,
3988               .resetvalue = cpu->id_mmfr3 },
3989             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
3990               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
3991               .access = PL1_R, .type = ARM_CP_CONST,
3992               .resetvalue = cpu->id_isar0 },
3993             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
3994               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
3995               .access = PL1_R, .type = ARM_CP_CONST,
3996               .resetvalue = cpu->id_isar1 },
3997             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
3998               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
3999               .access = PL1_R, .type = ARM_CP_CONST,
4000               .resetvalue = cpu->id_isar2 },
4001             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4002               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4003               .access = PL1_R, .type = ARM_CP_CONST,
4004               .resetvalue = cpu->id_isar3 },
4005             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4006               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4007               .access = PL1_R, .type = ARM_CP_CONST,
4008               .resetvalue = cpu->id_isar4 },
4009             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4010               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4011               .access = PL1_R, .type = ARM_CP_CONST,
4012               .resetvalue = cpu->id_isar5 },
4013             /* 6..7 are as yet unallocated and must RAZ */
4014             { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
4015               .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
4016               .resetvalue = 0 },
4017             { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
4018               .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
4019               .resetvalue = 0 },
4020             REGINFO_SENTINEL
4021         };
4022         define_arm_cp_regs(cpu, v6_idregs);
4023         define_arm_cp_regs(cpu, v6_cp_reginfo);
4024     } else {
4025         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4026     }
4027     if (arm_feature(env, ARM_FEATURE_V6K)) {
4028         define_arm_cp_regs(cpu, v6k_cp_reginfo);
4029     }
4030     if (arm_feature(env, ARM_FEATURE_V7MP) &&
4031         !arm_feature(env, ARM_FEATURE_MPU)) {
4032         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4033     }
4034     if (arm_feature(env, ARM_FEATURE_V7)) {
4035         /* v7 performance monitor control register: same implementor
4036          * field as main ID register, and we implement only the cycle
4037          * count register.
4038          */
4039 #ifndef CONFIG_USER_ONLY
4040         ARMCPRegInfo pmcr = {
4041             .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4042             .access = PL0_RW,
4043             .type = ARM_CP_IO | ARM_CP_ALIAS,
4044             .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4045             .accessfn = pmreg_access, .writefn = pmcr_write,
4046             .raw_writefn = raw_write,
4047         };
4048         ARMCPRegInfo pmcr64 = {
4049             .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4050             .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4051             .access = PL0_RW, .accessfn = pmreg_access,
4052             .type = ARM_CP_IO,
4053             .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4054             .resetvalue = cpu->midr & 0xff000000,
4055             .writefn = pmcr_write, .raw_writefn = raw_write,
4056         };
4057         define_one_arm_cp_reg(cpu, &pmcr);
4058         define_one_arm_cp_reg(cpu, &pmcr64);
4059 #endif
4060         ARMCPRegInfo clidr = {
4061             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4062             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4063             .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4064         };
4065         define_one_arm_cp_reg(cpu, &clidr);
4066         define_arm_cp_regs(cpu, v7_cp_reginfo);
4067         define_debug_regs(cpu);
4068     } else {
4069         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4070     }
4071     if (arm_feature(env, ARM_FEATURE_V8)) {
4072         /* AArch64 ID registers, which all have impdef reset values */
4073         ARMCPRegInfo v8_idregs[] = {
4074             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4075               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4076               .access = PL1_R, .type = ARM_CP_CONST,
4077               .resetvalue = cpu->id_aa64pfr0 },
4078             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4079               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4080               .access = PL1_R, .type = ARM_CP_CONST,
4081               .resetvalue = cpu->id_aa64pfr1},
4082             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4083               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4084               .access = PL1_R, .type = ARM_CP_CONST,
4085               /* We mask out the PMUVer field, because we don't currently
4086                * implement the PMU. Not advertising it prevents the guest
4087                * from trying to use it and getting UNDEFs on registers we
4088                * don't implement.
4089                */
4090               .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
4091             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4092               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4093               .access = PL1_R, .type = ARM_CP_CONST,
4094               .resetvalue = cpu->id_aa64dfr1 },
4095             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4096               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4097               .access = PL1_R, .type = ARM_CP_CONST,
4098               .resetvalue = cpu->id_aa64afr0 },
4099             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4100               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4101               .access = PL1_R, .type = ARM_CP_CONST,
4102               .resetvalue = cpu->id_aa64afr1 },
4103             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4104               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4105               .access = PL1_R, .type = ARM_CP_CONST,
4106               .resetvalue = cpu->id_aa64isar0 },
4107             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4108               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4109               .access = PL1_R, .type = ARM_CP_CONST,
4110               .resetvalue = cpu->id_aa64isar1 },
4111             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4112               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4113               .access = PL1_R, .type = ARM_CP_CONST,
4114               .resetvalue = cpu->id_aa64mmfr0 },
4115             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4116               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4117               .access = PL1_R, .type = ARM_CP_CONST,
4118               .resetvalue = cpu->id_aa64mmfr1 },
4119             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4120               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4121               .access = PL1_R, .type = ARM_CP_CONST,
4122               .resetvalue = cpu->mvfr0 },
4123             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4124               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4125               .access = PL1_R, .type = ARM_CP_CONST,
4126               .resetvalue = cpu->mvfr1 },
4127             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4128               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4129               .access = PL1_R, .type = ARM_CP_CONST,
4130               .resetvalue = cpu->mvfr2 },
4131             REGINFO_SENTINEL
4132         };
4133         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4134         if (!arm_feature(env, ARM_FEATURE_EL3) &&
4135             !arm_feature(env, ARM_FEATURE_EL2)) {
4136             ARMCPRegInfo rvbar = {
4137                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4138                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4139                 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4140             };
4141             define_one_arm_cp_reg(cpu, &rvbar);
4142         }
4143         define_arm_cp_regs(cpu, v8_idregs);
4144         define_arm_cp_regs(cpu, v8_cp_reginfo);
4145     }
4146     if (arm_feature(env, ARM_FEATURE_EL2)) {
4147         uint64_t vmpidr_def = mpidr_read_val(env);
4148         ARMCPRegInfo vpidr_regs[] = {
4149             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4150               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4151               .access = PL2_RW, .accessfn = access_el3_aa32ns,
4152               .resetvalue = cpu->midr,
4153               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4154             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4155               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4156               .access = PL2_RW, .resetvalue = cpu->midr,
4157               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4158             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4159               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4160               .access = PL2_RW, .accessfn = access_el3_aa32ns,
4161               .resetvalue = vmpidr_def,
4162               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4163             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4164               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4165               .access = PL2_RW,
4166               .resetvalue = vmpidr_def,
4167               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4168             REGINFO_SENTINEL
4169         };
4170         define_arm_cp_regs(cpu, vpidr_regs);
4171         define_arm_cp_regs(cpu, el2_cp_reginfo);
4172         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4173         if (!arm_feature(env, ARM_FEATURE_EL3)) {
4174             ARMCPRegInfo rvbar = {
4175                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4176                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4177                 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4178             };
4179             define_one_arm_cp_reg(cpu, &rvbar);
4180         }
4181     } else {
4182         /* If EL2 is missing but higher ELs are enabled, we need to
4183          * register the no_el2 reginfos.
4184          */
4185         if (arm_feature(env, ARM_FEATURE_EL3)) {
4186             /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4187              * of MIDR_EL1 and MPIDR_EL1.
4188              */
4189             ARMCPRegInfo vpidr_regs[] = {
4190                 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4191                   .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4192                   .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4193                   .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4194                   .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4195                 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4196                   .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4197                   .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4198                   .type = ARM_CP_NO_RAW,
4199                   .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
4200                 REGINFO_SENTINEL
4201             };
4202             define_arm_cp_regs(cpu, vpidr_regs);
4203             define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
4204         }
4205     }
4206     if (arm_feature(env, ARM_FEATURE_EL3)) {
4207         define_arm_cp_regs(cpu, el3_cp_reginfo);
4208         ARMCPRegInfo rvbar = {
4209             .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4210             .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4211             .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar
4212         };
4213         define_one_arm_cp_reg(cpu, &rvbar);
4214     }
4215     if (arm_feature(env, ARM_FEATURE_MPU)) {
4216         if (arm_feature(env, ARM_FEATURE_V6)) {
4217             /* PMSAv6 not implemented */
4218             assert(arm_feature(env, ARM_FEATURE_V7));
4219             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4220             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4221         } else {
4222             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4223         }
4224     } else {
4225         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4226         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4227     }
4228     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4229         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4230     }
4231     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4232         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4233     }
4234     if (arm_feature(env, ARM_FEATURE_VAPA)) {
4235         define_arm_cp_regs(cpu, vapa_cp_reginfo);
4236     }
4237     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4238         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4239     }
4240     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4241         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4242     }
4243     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4244         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4245     }
4246     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4247         define_arm_cp_regs(cpu, omap_cp_reginfo);
4248     }
4249     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4250         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4251     }
4252     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4253         define_arm_cp_regs(cpu, xscale_cp_reginfo);
4254     }
4255     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4256         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4257     }
4258     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4259         define_arm_cp_regs(cpu, lpae_cp_reginfo);
4260     }
4261     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4262      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4263      * be read-only (ie write causes UNDEF exception).
4264      */
4265     {
4266         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4267             /* Pre-v8 MIDR space.
4268              * Note that the MIDR isn't a simple constant register because
4269              * of the TI925 behaviour where writes to another register can
4270              * cause the MIDR value to change.
4271              *
4272              * Unimplemented registers in the c15 0 0 0 space default to
4273              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4274              * and friends override accordingly.
4275              */
4276             { .name = "MIDR",
4277               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
4278               .access = PL1_R, .resetvalue = cpu->midr,
4279               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
4280               .readfn = midr_read,
4281               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4282               .type = ARM_CP_OVERRIDE },
4283             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4284             { .name = "DUMMY",
4285               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4286               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4287             { .name = "DUMMY",
4288               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4289               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4290             { .name = "DUMMY",
4291               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4292               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4293             { .name = "DUMMY",
4294               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4295               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4296             { .name = "DUMMY",
4297               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4298               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4299             REGINFO_SENTINEL
4300         };
4301         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
4302             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4303               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
4304               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
4305               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4306               .readfn = midr_read },
4307             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4308             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4309               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4310               .access = PL1_R, .resetvalue = cpu->midr },
4311             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4312               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4313               .access = PL1_R, .resetvalue = cpu->midr },
4314             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4315               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
4316               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
4317             REGINFO_SENTINEL
4318         };
4319         ARMCPRegInfo id_cp_reginfo[] = {
4320             /* These are common to v8 and pre-v8 */
4321             { .name = "CTR",
4322               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4323               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4324             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4325               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4326               .access = PL0_R, .accessfn = ctr_el0_access,
4327               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4328             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4329             { .name = "TCMTR",
4330               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4331               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4332             REGINFO_SENTINEL
4333         };
4334         /* TLBTR is specific to VMSA */
4335         ARMCPRegInfo id_tlbtr_reginfo = {
4336               .name = "TLBTR",
4337               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4338               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4339         };
4340         /* MPUIR is specific to PMSA V6+ */
4341         ARMCPRegInfo id_mpuir_reginfo = {
4342               .name = "MPUIR",
4343               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4344               .access = PL1_R, .type = ARM_CP_CONST,
4345               .resetvalue = cpu->pmsav7_dregion << 8
4346         };
4347         ARMCPRegInfo crn0_wi_reginfo = {
4348             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4349             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4350             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4351         };
4352         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4353             arm_feature(env, ARM_FEATURE_STRONGARM)) {
4354             ARMCPRegInfo *r;
4355             /* Register the blanket "writes ignored" value first to cover the
4356              * whole space. Then update the specific ID registers to allow write
4357              * access, so that they ignore writes rather than causing them to
4358              * UNDEF.
4359              */
4360             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
4361             for (r = id_pre_v8_midr_cp_reginfo;
4362                  r->type != ARM_CP_SENTINEL; r++) {
4363                 r->access = PL1_RW;
4364             }
4365             for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4366                 r->access = PL1_RW;
4367             }
4368             id_tlbtr_reginfo.access = PL1_RW;
4369             id_tlbtr_reginfo.access = PL1_RW;
4370         }
4371         if (arm_feature(env, ARM_FEATURE_V8)) {
4372             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4373         } else {
4374             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4375         }
4376         define_arm_cp_regs(cpu, id_cp_reginfo);
4377         if (!arm_feature(env, ARM_FEATURE_MPU)) {
4378             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
4379         } else if (arm_feature(env, ARM_FEATURE_V7)) {
4380             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
4381         }
4382     }
4383
4384     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4385         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4386     }
4387
4388     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
4389         ARMCPRegInfo auxcr_reginfo[] = {
4390             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4391               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4392               .access = PL1_RW, .type = ARM_CP_CONST,
4393               .resetvalue = cpu->reset_auxcr },
4394             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4395               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4396               .access = PL2_RW, .type = ARM_CP_CONST,
4397               .resetvalue = 0 },
4398             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4399               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4400               .access = PL3_RW, .type = ARM_CP_CONST,
4401               .resetvalue = 0 },
4402             REGINFO_SENTINEL
4403         };
4404         define_arm_cp_regs(cpu, auxcr_reginfo);
4405     }
4406
4407     if (arm_feature(env, ARM_FEATURE_CBAR)) {
4408         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4409             /* 32 bit view is [31:18] 0...0 [43:32]. */
4410             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4411                 | extract64(cpu->reset_cbar, 32, 12);
4412             ARMCPRegInfo cbar_reginfo[] = {
4413                 { .name = "CBAR",
4414                   .type = ARM_CP_CONST,
4415                   .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4416                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
4417                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4418                   .type = ARM_CP_CONST,
4419                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4420                   .access = PL1_R, .resetvalue = cbar32 },
4421                 REGINFO_SENTINEL
4422             };
4423             /* We don't implement a r/w 64 bit CBAR currently */
4424             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4425             define_arm_cp_regs(cpu, cbar_reginfo);
4426         } else {
4427             ARMCPRegInfo cbar = {
4428                 .name = "CBAR",
4429                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4430                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4431                 .fieldoffset = offsetof(CPUARMState,
4432                                         cp15.c15_config_base_address)
4433             };
4434             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4435                 cbar.access = PL1_R;
4436                 cbar.fieldoffset = 0;
4437                 cbar.type = ARM_CP_CONST;
4438             }
4439             define_one_arm_cp_reg(cpu, &cbar);
4440         }
4441     }
4442
4443     /* Generic registers whose values depend on the implementation */
4444     {
4445         ARMCPRegInfo sctlr = {
4446             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
4447             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4448             .access = PL1_RW,
4449             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4450                                    offsetof(CPUARMState, cp15.sctlr_ns) },
4451             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4452             .raw_writefn = raw_write,
4453         };
4454         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4455             /* Normally we would always end the TB on an SCTLR write, but Linux
4456              * arch/arm/mach-pxa/sleep.S expects two instructions following
4457              * an MMU enable to execute from cache.  Imitate this behaviour.
4458              */
4459             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4460         }
4461         define_one_arm_cp_reg(cpu, &sctlr);
4462     }
4463 }
4464
4465 ARMCPU *cpu_arm_init(const char *cpu_model)
4466 {
4467     return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
4468 }
4469
4470 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4471 {
4472     CPUState *cs = CPU(cpu);
4473     CPUARMState *env = &cpu->env;
4474
4475     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4476         gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4477                                  aarch64_fpu_gdb_set_reg,
4478                                  34, "aarch64-fpu.xml", 0);
4479     } else if (arm_feature(env, ARM_FEATURE_NEON)) {
4480         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4481                                  51, "arm-neon.xml", 0);
4482     } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
4483         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4484                                  35, "arm-vfp3.xml", 0);
4485     } else if (arm_feature(env, ARM_FEATURE_VFP)) {
4486         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4487                                  19, "arm-vfp.xml", 0);
4488     }
4489 }
4490
4491 /* Sort alphabetically by type name, except for "any". */
4492 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
4493 {
4494     ObjectClass *class_a = (ObjectClass *)a;
4495     ObjectClass *class_b = (ObjectClass *)b;
4496     const char *name_a, *name_b;
4497
4498     name_a = object_class_get_name(class_a);
4499     name_b = object_class_get_name(class_b);
4500     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
4501         return 1;
4502     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
4503         return -1;
4504     } else {
4505         return strcmp(name_a, name_b);
4506     }
4507 }
4508
4509 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
4510 {
4511     ObjectClass *oc = data;
4512     CPUListState *s = user_data;
4513     const char *typename;
4514     char *name;
4515
4516     typename = object_class_get_name(oc);
4517     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
4518     (*s->cpu_fprintf)(s->file, "  %s\n",
4519                       name);
4520     g_free(name);
4521 }
4522
4523 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
4524 {
4525     CPUListState s = {
4526         .file = f,
4527         .cpu_fprintf = cpu_fprintf,
4528     };
4529     GSList *list;
4530
4531     list = object_class_get_list(TYPE_ARM_CPU, false);
4532     list = g_slist_sort(list, arm_cpu_list_compare);
4533     (*cpu_fprintf)(f, "Available CPUs:\n");
4534     g_slist_foreach(list, arm_cpu_list_entry, &s);
4535     g_slist_free(list);
4536 #ifdef CONFIG_KVM
4537     /* The 'host' CPU type is dynamically registered only if KVM is
4538      * enabled, so we have to special-case it here:
4539      */
4540     (*cpu_fprintf)(f, "  host (only available in KVM mode)\n");
4541 #endif
4542 }
4543
4544 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
4545 {
4546     ObjectClass *oc = data;
4547     CpuDefinitionInfoList **cpu_list = user_data;
4548     CpuDefinitionInfoList *entry;
4549     CpuDefinitionInfo *info;
4550     const char *typename;
4551
4552     typename = object_class_get_name(oc);
4553     info = g_malloc0(sizeof(*info));
4554     info->name = g_strndup(typename,
4555                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
4556
4557     entry = g_malloc0(sizeof(*entry));
4558     entry->value = info;
4559     entry->next = *cpu_list;
4560     *cpu_list = entry;
4561 }
4562
4563 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
4564 {
4565     CpuDefinitionInfoList *cpu_list = NULL;
4566     GSList *list;
4567
4568     list = object_class_get_list(TYPE_ARM_CPU, false);
4569     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
4570     g_slist_free(list);
4571
4572     return cpu_list;
4573 }
4574
4575 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
4576                                    void *opaque, int state, int secstate,
4577                                    int crm, int opc1, int opc2)
4578 {
4579     /* Private utility function for define_one_arm_cp_reg_with_opaque():
4580      * add a single reginfo struct to the hash table.
4581      */
4582     uint32_t *key = g_new(uint32_t, 1);
4583     ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
4584     int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
4585     int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
4586
4587     /* Reset the secure state to the specific incoming state.  This is
4588      * necessary as the register may have been defined with both states.
4589      */
4590     r2->secure = secstate;
4591
4592     if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4593         /* Register is banked (using both entries in array).
4594          * Overwriting fieldoffset as the array is only used to define
4595          * banked registers but later only fieldoffset is used.
4596          */
4597         r2->fieldoffset = r->bank_fieldoffsets[ns];
4598     }
4599
4600     if (state == ARM_CP_STATE_AA32) {
4601         if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4602             /* If the register is banked then we don't need to migrate or
4603              * reset the 32-bit instance in certain cases:
4604              *
4605              * 1) If the register has both 32-bit and 64-bit instances then we
4606              *    can count on the 64-bit instance taking care of the
4607              *    non-secure bank.
4608              * 2) If ARMv8 is enabled then we can count on a 64-bit version
4609              *    taking care of the secure bank.  This requires that separate
4610              *    32 and 64-bit definitions are provided.
4611              */
4612             if ((r->state == ARM_CP_STATE_BOTH && ns) ||
4613                 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
4614                 r2->type |= ARM_CP_ALIAS;
4615             }
4616         } else if ((secstate != r->secure) && !ns) {
4617             /* The register is not banked so we only want to allow migration of
4618              * the non-secure instance.
4619              */
4620             r2->type |= ARM_CP_ALIAS;
4621         }
4622
4623         if (r->state == ARM_CP_STATE_BOTH) {
4624             /* We assume it is a cp15 register if the .cp field is left unset.
4625              */
4626             if (r2->cp == 0) {
4627                 r2->cp = 15;
4628             }
4629
4630 #ifdef HOST_WORDS_BIGENDIAN
4631             if (r2->fieldoffset) {
4632                 r2->fieldoffset += sizeof(uint32_t);
4633             }
4634 #endif
4635         }
4636     }
4637     if (state == ARM_CP_STATE_AA64) {
4638         /* To allow abbreviation of ARMCPRegInfo
4639          * definitions, we treat cp == 0 as equivalent to
4640          * the value for "standard guest-visible sysreg".
4641          * STATE_BOTH definitions are also always "standard
4642          * sysreg" in their AArch64 view (the .cp value may
4643          * be non-zero for the benefit of the AArch32 view).
4644          */
4645         if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
4646             r2->cp = CP_REG_ARM64_SYSREG_CP;
4647         }
4648         *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
4649                                   r2->opc0, opc1, opc2);
4650     } else {
4651         *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
4652     }
4653     if (opaque) {
4654         r2->opaque = opaque;
4655     }
4656     /* reginfo passed to helpers is correct for the actual access,
4657      * and is never ARM_CP_STATE_BOTH:
4658      */
4659     r2->state = state;
4660     /* Make sure reginfo passed to helpers for wildcarded regs
4661      * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
4662      */
4663     r2->crm = crm;
4664     r2->opc1 = opc1;
4665     r2->opc2 = opc2;
4666     /* By convention, for wildcarded registers only the first
4667      * entry is used for migration; the others are marked as
4668      * ALIAS so we don't try to transfer the register
4669      * multiple times. Special registers (ie NOP/WFI) are
4670      * never migratable and not even raw-accessible.
4671      */
4672     if ((r->type & ARM_CP_SPECIAL)) {
4673         r2->type |= ARM_CP_NO_RAW;
4674     }
4675     if (((r->crm == CP_ANY) && crm != 0) ||
4676         ((r->opc1 == CP_ANY) && opc1 != 0) ||
4677         ((r->opc2 == CP_ANY) && opc2 != 0)) {
4678         r2->type |= ARM_CP_ALIAS;
4679     }
4680
4681     /* Check that raw accesses are either forbidden or handled. Note that
4682      * we can't assert this earlier because the setup of fieldoffset for
4683      * banked registers has to be done first.
4684      */
4685     if (!(r2->type & ARM_CP_NO_RAW)) {
4686         assert(!raw_accessors_invalid(r2));
4687     }
4688
4689     /* Overriding of an existing definition must be explicitly
4690      * requested.
4691      */
4692     if (!(r->type & ARM_CP_OVERRIDE)) {
4693         ARMCPRegInfo *oldreg;
4694         oldreg = g_hash_table_lookup(cpu->cp_regs, key);
4695         if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
4696             fprintf(stderr, "Register redefined: cp=%d %d bit "
4697                     "crn=%d crm=%d opc1=%d opc2=%d, "
4698                     "was %s, now %s\n", r2->cp, 32 + 32 * is64,
4699                     r2->crn, r2->crm, r2->opc1, r2->opc2,
4700                     oldreg->name, r2->name);
4701             g_assert_not_reached();
4702         }
4703     }
4704     g_hash_table_insert(cpu->cp_regs, key, r2);
4705 }
4706
4707
4708 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
4709                                        const ARMCPRegInfo *r, void *opaque)
4710 {
4711     /* Define implementations of coprocessor registers.
4712      * We store these in a hashtable because typically
4713      * there are less than 150 registers in a space which
4714      * is 16*16*16*8*8 = 262144 in size.
4715      * Wildcarding is supported for the crm, opc1 and opc2 fields.
4716      * If a register is defined twice then the second definition is
4717      * used, so this can be used to define some generic registers and
4718      * then override them with implementation specific variations.
4719      * At least one of the original and the second definition should
4720      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
4721      * against accidental use.
4722      *
4723      * The state field defines whether the register is to be
4724      * visible in the AArch32 or AArch64 execution state. If the
4725      * state is set to ARM_CP_STATE_BOTH then we synthesise a
4726      * reginfo structure for the AArch32 view, which sees the lower
4727      * 32 bits of the 64 bit register.
4728      *
4729      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
4730      * be wildcarded. AArch64 registers are always considered to be 64
4731      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
4732      * the register, if any.
4733      */
4734     int crm, opc1, opc2, state;
4735     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
4736     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
4737     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
4738     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
4739     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
4740     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
4741     /* 64 bit registers have only CRm and Opc1 fields */
4742     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
4743     /* op0 only exists in the AArch64 encodings */
4744     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
4745     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
4746     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
4747     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
4748      * encodes a minimum access level for the register. We roll this
4749      * runtime check into our general permission check code, so check
4750      * here that the reginfo's specified permissions are strict enough
4751      * to encompass the generic architectural permission check.
4752      */
4753     if (r->state != ARM_CP_STATE_AA32) {
4754         int mask = 0;
4755         switch (r->opc1) {
4756         case 0: case 1: case 2:
4757             /* min_EL EL1 */
4758             mask = PL1_RW;
4759             break;
4760         case 3:
4761             /* min_EL EL0 */
4762             mask = PL0_RW;
4763             break;
4764         case 4:
4765             /* min_EL EL2 */
4766             mask = PL2_RW;
4767             break;
4768         case 5:
4769             /* unallocated encoding, so not possible */
4770             assert(false);
4771             break;
4772         case 6:
4773             /* min_EL EL3 */
4774             mask = PL3_RW;
4775             break;
4776         case 7:
4777             /* min_EL EL1, secure mode only (we don't check the latter) */
4778             mask = PL1_RW;
4779             break;
4780         default:
4781             /* broken reginfo with out-of-range opc1 */
4782             assert(false);
4783             break;
4784         }
4785         /* assert our permissions are not too lax (stricter is fine) */
4786         assert((r->access & ~mask) == 0);
4787     }
4788
4789     /* Check that the register definition has enough info to handle
4790      * reads and writes if they are permitted.
4791      */
4792     if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
4793         if (r->access & PL3_R) {
4794             assert((r->fieldoffset ||
4795                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
4796                    r->readfn);
4797         }
4798         if (r->access & PL3_W) {
4799             assert((r->fieldoffset ||
4800                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
4801                    r->writefn);
4802         }
4803     }
4804     /* Bad type field probably means missing sentinel at end of reg list */
4805     assert(cptype_valid(r->type));
4806     for (crm = crmmin; crm <= crmmax; crm++) {
4807         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
4808             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
4809                 for (state = ARM_CP_STATE_AA32;
4810                      state <= ARM_CP_STATE_AA64; state++) {
4811                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
4812                         continue;
4813                     }
4814                     if (state == ARM_CP_STATE_AA32) {
4815                         /* Under AArch32 CP registers can be common
4816                          * (same for secure and non-secure world) or banked.
4817                          */
4818                         switch (r->secure) {
4819                         case ARM_CP_SECSTATE_S:
4820                         case ARM_CP_SECSTATE_NS:
4821                             add_cpreg_to_hashtable(cpu, r, opaque, state,
4822                                                    r->secure, crm, opc1, opc2);
4823                             break;
4824                         default:
4825                             add_cpreg_to_hashtable(cpu, r, opaque, state,
4826                                                    ARM_CP_SECSTATE_S,
4827                                                    crm, opc1, opc2);
4828                             add_cpreg_to_hashtable(cpu, r, opaque, state,
4829                                                    ARM_CP_SECSTATE_NS,
4830                                                    crm, opc1, opc2);
4831                             break;
4832                         }
4833                     } else {
4834                         /* AArch64 registers get mapped to non-secure instance
4835                          * of AArch32 */
4836                         add_cpreg_to_hashtable(cpu, r, opaque, state,
4837                                                ARM_CP_SECSTATE_NS,
4838                                                crm, opc1, opc2);
4839                     }
4840                 }
4841             }
4842         }
4843     }
4844 }
4845
4846 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
4847                                     const ARMCPRegInfo *regs, void *opaque)
4848 {
4849     /* Define a whole list of registers */
4850     const ARMCPRegInfo *r;
4851     for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
4852         define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
4853     }
4854 }
4855
4856 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4857 {
4858     return g_hash_table_lookup(cpregs, &encoded_cp);
4859 }
4860
4861 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
4862                          uint64_t value)
4863 {
4864     /* Helper coprocessor write function for write-ignore registers */
4865 }
4866
4867 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4868 {
4869     /* Helper coprocessor write function for read-as-zero registers */
4870     return 0;
4871 }
4872
4873 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
4874 {
4875     /* Helper coprocessor reset function for do-nothing-on-reset registers */
4876 }
4877
4878 static int bad_mode_switch(CPUARMState *env, int mode)
4879 {
4880     /* Return true if it is not valid for us to switch to
4881      * this CPU mode (ie all the UNPREDICTABLE cases in
4882      * the ARM ARM CPSRWriteByInstr pseudocode).
4883      */
4884     switch (mode) {
4885     case ARM_CPU_MODE_USR:
4886     case ARM_CPU_MODE_SYS:
4887     case ARM_CPU_MODE_SVC:
4888     case ARM_CPU_MODE_ABT:
4889     case ARM_CPU_MODE_UND:
4890     case ARM_CPU_MODE_IRQ:
4891     case ARM_CPU_MODE_FIQ:
4892         return 0;
4893     case ARM_CPU_MODE_MON:
4894         return !arm_is_secure(env);
4895     default:
4896         return 1;
4897     }
4898 }
4899
4900 uint32_t cpsr_read(CPUARMState *env)
4901 {
4902     int ZF;
4903     ZF = (env->ZF == 0);
4904     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
4905         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
4906         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
4907         | ((env->condexec_bits & 0xfc) << 8)
4908         | (env->GE << 16) | (env->daif & CPSR_AIF);
4909 }
4910
4911 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
4912 {
4913     uint32_t changed_daif;
4914
4915     if (mask & CPSR_NZCV) {
4916         env->ZF = (~val) & CPSR_Z;
4917         env->NF = val;
4918         env->CF = (val >> 29) & 1;
4919         env->VF = (val << 3) & 0x80000000;
4920     }
4921     if (mask & CPSR_Q)
4922         env->QF = ((val & CPSR_Q) != 0);
4923     if (mask & CPSR_T)
4924         env->thumb = ((val & CPSR_T) != 0);
4925     if (mask & CPSR_IT_0_1) {
4926         env->condexec_bits &= ~3;
4927         env->condexec_bits |= (val >> 25) & 3;
4928     }
4929     if (mask & CPSR_IT_2_7) {
4930         env->condexec_bits &= 3;
4931         env->condexec_bits |= (val >> 8) & 0xfc;
4932     }
4933     if (mask & CPSR_GE) {
4934         env->GE = (val >> 16) & 0xf;
4935     }
4936
4937     /* In a V7 implementation that includes the security extensions but does
4938      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
4939      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
4940      * bits respectively.
4941      *
4942      * In a V8 implementation, it is permitted for privileged software to
4943      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
4944      */
4945     if (!arm_feature(env, ARM_FEATURE_V8) &&
4946         arm_feature(env, ARM_FEATURE_EL3) &&
4947         !arm_feature(env, ARM_FEATURE_EL2) &&
4948         !arm_is_secure(env)) {
4949
4950         changed_daif = (env->daif ^ val) & mask;
4951
4952         if (changed_daif & CPSR_A) {
4953             /* Check to see if we are allowed to change the masking of async
4954              * abort exceptions from a non-secure state.
4955              */
4956             if (!(env->cp15.scr_el3 & SCR_AW)) {
4957                 qemu_log_mask(LOG_GUEST_ERROR,
4958                               "Ignoring attempt to switch CPSR_A flag from "
4959                               "non-secure world with SCR.AW bit clear\n");
4960                 mask &= ~CPSR_A;
4961             }
4962         }
4963
4964         if (changed_daif & CPSR_F) {
4965             /* Check to see if we are allowed to change the masking of FIQ
4966              * exceptions from a non-secure state.
4967              */
4968             if (!(env->cp15.scr_el3 & SCR_FW)) {
4969                 qemu_log_mask(LOG_GUEST_ERROR,
4970                               "Ignoring attempt to switch CPSR_F flag from "
4971                               "non-secure world with SCR.FW bit clear\n");
4972                 mask &= ~CPSR_F;
4973             }
4974
4975             /* Check whether non-maskable FIQ (NMFI) support is enabled.
4976              * If this bit is set software is not allowed to mask
4977              * FIQs, but is allowed to set CPSR_F to 0.
4978              */
4979             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
4980                 (val & CPSR_F)) {
4981                 qemu_log_mask(LOG_GUEST_ERROR,
4982                               "Ignoring attempt to enable CPSR_F flag "
4983                               "(non-maskable FIQ [NMFI] support enabled)\n");
4984                 mask &= ~CPSR_F;
4985             }
4986         }
4987     }
4988
4989     env->daif &= ~(CPSR_AIF & mask);
4990     env->daif |= val & CPSR_AIF & mask;
4991
4992     if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
4993         if (bad_mode_switch(env, val & CPSR_M)) {
4994             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
4995              * We choose to ignore the attempt and leave the CPSR M field
4996              * untouched.
4997              */
4998             mask &= ~CPSR_M;
4999         } else {
5000             switch_mode(env, val & CPSR_M);
5001         }
5002     }
5003     mask &= ~CACHED_CPSR_BITS;
5004     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5005 }
5006
5007 /* Sign/zero extend */
5008 uint32_t HELPER(sxtb16)(uint32_t x)
5009 {
5010     uint32_t res;
5011     res = (uint16_t)(int8_t)x;
5012     res |= (uint32_t)(int8_t)(x >> 16) << 16;
5013     return res;
5014 }
5015
5016 uint32_t HELPER(uxtb16)(uint32_t x)
5017 {
5018     uint32_t res;
5019     res = (uint16_t)(uint8_t)x;
5020     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5021     return res;
5022 }
5023
5024 uint32_t HELPER(clz)(uint32_t x)
5025 {
5026     return clz32(x);
5027 }
5028
5029 int32_t HELPER(sdiv)(int32_t num, int32_t den)
5030 {
5031     if (den == 0)
5032       return 0;
5033     if (num == INT_MIN && den == -1)
5034       return INT_MIN;
5035     return num / den;
5036 }
5037
5038 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5039 {
5040     if (den == 0)
5041       return 0;
5042     return num / den;
5043 }
5044
5045 uint32_t HELPER(rbit)(uint32_t x)
5046 {
5047     x =  ((x & 0xff000000) >> 24)
5048        | ((x & 0x00ff0000) >> 8)
5049        | ((x & 0x0000ff00) << 8)
5050        | ((x & 0x000000ff) << 24);
5051     x =  ((x & 0xf0f0f0f0) >> 4)
5052        | ((x & 0x0f0f0f0f) << 4);
5053     x =  ((x & 0x88888888) >> 3)
5054        | ((x & 0x44444444) >> 1)
5055        | ((x & 0x22222222) << 1)
5056        | ((x & 0x11111111) << 3);
5057     return x;
5058 }
5059
5060 #if defined(CONFIG_USER_ONLY)
5061
5062 /* These should probably raise undefined insn exceptions.  */
5063 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
5064 {
5065     ARMCPU *cpu = arm_env_get_cpu(env);
5066
5067     cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
5068 }
5069
5070 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
5071 {
5072     ARMCPU *cpu = arm_env_get_cpu(env);
5073
5074     cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
5075     return 0;
5076 }
5077
5078 void switch_mode(CPUARMState *env, int mode)
5079 {
5080     ARMCPU *cpu = arm_env_get_cpu(env);
5081
5082     if (mode != ARM_CPU_MODE_USR) {
5083         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5084     }
5085 }
5086
5087 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
5088 {
5089     ARMCPU *cpu = arm_env_get_cpu(env);
5090
5091     cpu_abort(CPU(cpu), "banked r13 write\n");
5092 }
5093
5094 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
5095 {
5096     ARMCPU *cpu = arm_env_get_cpu(env);
5097
5098     cpu_abort(CPU(cpu), "banked r13 read\n");
5099     return 0;
5100 }
5101
5102 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5103                                  uint32_t cur_el, bool secure)
5104 {
5105     return 1;
5106 }
5107
5108 void aarch64_sync_64_to_32(CPUARMState *env)
5109 {
5110     g_assert_not_reached();
5111 }
5112
5113 #else
5114
5115 /* Map CPU modes onto saved register banks.  */
5116 int bank_number(int mode)
5117 {
5118     switch (mode) {
5119     case ARM_CPU_MODE_USR:
5120     case ARM_CPU_MODE_SYS:
5121         return 0;
5122     case ARM_CPU_MODE_SVC:
5123         return 1;
5124     case ARM_CPU_MODE_ABT:
5125         return 2;
5126     case ARM_CPU_MODE_UND:
5127         return 3;
5128     case ARM_CPU_MODE_IRQ:
5129         return 4;
5130     case ARM_CPU_MODE_FIQ:
5131         return 5;
5132     case ARM_CPU_MODE_HYP:
5133         return 6;
5134     case ARM_CPU_MODE_MON:
5135         return 7;
5136     }
5137     g_assert_not_reached();
5138 }
5139
5140 void switch_mode(CPUARMState *env, int mode)
5141 {
5142     int old_mode;
5143     int i;
5144
5145     old_mode = env->uncached_cpsr & CPSR_M;
5146     if (mode == old_mode)
5147         return;
5148
5149     if (old_mode == ARM_CPU_MODE_FIQ) {
5150         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
5151         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
5152     } else if (mode == ARM_CPU_MODE_FIQ) {
5153         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
5154         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
5155     }
5156
5157     i = bank_number(old_mode);
5158     env->banked_r13[i] = env->regs[13];
5159     env->banked_r14[i] = env->regs[14];
5160     env->banked_spsr[i] = env->spsr;
5161
5162     i = bank_number(mode);
5163     env->regs[13] = env->banked_r13[i];
5164     env->regs[14] = env->banked_r14[i];
5165     env->spsr = env->banked_spsr[i];
5166 }
5167
5168 /* Physical Interrupt Target EL Lookup Table
5169  *
5170  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5171  *
5172  * The below multi-dimensional table is used for looking up the target
5173  * exception level given numerous condition criteria.  Specifically, the
5174  * target EL is based on SCR and HCR routing controls as well as the
5175  * currently executing EL and secure state.
5176  *
5177  *    Dimensions:
5178  *    target_el_table[2][2][2][2][2][4]
5179  *                    |  |  |  |  |  +--- Current EL
5180  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
5181  *                    |  |  |  +--------- HCR mask override
5182  *                    |  |  +------------ SCR exec state control
5183  *                    |  +--------------- SCR mask override
5184  *                    +------------------ 32-bit(0)/64-bit(1) EL3
5185  *
5186  *    The table values are as such:
5187  *    0-3 = EL0-EL3
5188  *     -1 = Cannot occur
5189  *
5190  * The ARM ARM target EL table includes entries indicating that an "exception
5191  * is not taken".  The two cases where this is applicable are:
5192  *    1) An exception is taken from EL3 but the SCR does not have the exception
5193  *    routed to EL3.
5194  *    2) An exception is taken from EL2 but the HCR does not have the exception
5195  *    routed to EL2.
5196  * In these two cases, the below table contain a target of EL1.  This value is
5197  * returned as it is expected that the consumer of the table data will check
5198  * for "target EL >= current EL" to ensure the exception is not taken.
5199  *
5200  *            SCR     HCR
5201  *         64  EA     AMO                 From
5202  *        BIT IRQ     IMO      Non-secure         Secure
5203  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
5204  */
5205 const int8_t target_el_table[2][2][2][2][2][4] = {
5206     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
5207        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
5208       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
5209        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
5210      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
5211        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
5212       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
5213        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
5214     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
5215        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 1,  1, -1,  1 },},},
5216       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1, -1,  1 },},
5217        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 1,  1, -1,  1 },},},},
5218      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
5219        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
5220       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
5221        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},},},
5222 };
5223
5224 /*
5225  * Determine the target EL for physical exceptions
5226  */
5227 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5228                                  uint32_t cur_el, bool secure)
5229 {
5230     CPUARMState *env = cs->env_ptr;
5231     int rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5232     int scr;
5233     int hcr;
5234     int target_el;
5235     int is64 = arm_el_is_aa64(env, 3);
5236
5237     switch (excp_idx) {
5238     case EXCP_IRQ:
5239         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5240         hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5241         break;
5242     case EXCP_FIQ:
5243         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5244         hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5245         break;
5246     default:
5247         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5248         hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5249         break;
5250     };
5251
5252     /* If HCR.TGE is set then HCR is treated as being 1 */
5253     hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5254
5255     /* Perform a table-lookup for the target EL given the current state */
5256     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5257
5258     assert(target_el > 0);
5259
5260     return target_el;
5261 }
5262
5263 static void v7m_push(CPUARMState *env, uint32_t val)
5264 {
5265     CPUState *cs = CPU(arm_env_get_cpu(env));
5266
5267     env->regs[13] -= 4;
5268     stl_phys(cs->as, env->regs[13], val);
5269 }
5270
5271 static uint32_t v7m_pop(CPUARMState *env)
5272 {
5273     CPUState *cs = CPU(arm_env_get_cpu(env));
5274     uint32_t val;
5275
5276     val = ldl_phys(cs->as, env->regs[13]);
5277     env->regs[13] += 4;
5278     return val;
5279 }
5280
5281 /* Switch to V7M main or process stack pointer.  */
5282 static void switch_v7m_sp(CPUARMState *env, int process)
5283 {
5284     uint32_t tmp;
5285     if (env->v7m.current_sp != process) {
5286         tmp = env->v7m.other_sp;
5287         env->v7m.other_sp = env->regs[13];
5288         env->regs[13] = tmp;
5289         env->v7m.current_sp = process;
5290     }
5291 }
5292
5293 static void do_v7m_exception_exit(CPUARMState *env)
5294 {
5295     uint32_t type;
5296     uint32_t xpsr;
5297
5298     type = env->regs[15];
5299     if (env->v7m.exception != 0)
5300         armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
5301
5302     /* Switch to the target stack.  */
5303     switch_v7m_sp(env, (type & 4) != 0);
5304     /* Pop registers.  */
5305     env->regs[0] = v7m_pop(env);
5306     env->regs[1] = v7m_pop(env);
5307     env->regs[2] = v7m_pop(env);
5308     env->regs[3] = v7m_pop(env);
5309     env->regs[12] = v7m_pop(env);
5310     env->regs[14] = v7m_pop(env);
5311     env->regs[15] = v7m_pop(env);
5312     if (env->regs[15] & 1) {
5313         qemu_log_mask(LOG_GUEST_ERROR,
5314                       "M profile return from interrupt with misaligned "
5315                       "PC is UNPREDICTABLE\n");
5316         /* Actual hardware seems to ignore the lsbit, and there are several
5317          * RTOSes out there which incorrectly assume the r15 in the stack
5318          * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5319          */
5320         env->regs[15] &= ~1U;
5321     }
5322     xpsr = v7m_pop(env);
5323     xpsr_write(env, xpsr, 0xfffffdff);
5324     /* Undo stack alignment.  */
5325     if (xpsr & 0x200)
5326         env->regs[13] |= 4;
5327     /* ??? The exception return type specifies Thread/Handler mode.  However
5328        this is also implied by the xPSR value. Not sure what to do
5329        if there is a mismatch.  */
5330     /* ??? Likewise for mismatches between the CONTROL register and the stack
5331        pointer.  */
5332 }
5333
5334 void arm_v7m_cpu_do_interrupt(CPUState *cs)
5335 {
5336     ARMCPU *cpu = ARM_CPU(cs);
5337     CPUARMState *env = &cpu->env;
5338     uint32_t xpsr = xpsr_read(env);
5339     uint32_t lr;
5340     uint32_t addr;
5341
5342     arm_log_exception(cs->exception_index);
5343
5344     lr = 0xfffffff1;
5345     if (env->v7m.current_sp)
5346         lr |= 4;
5347     if (env->v7m.exception == 0)
5348         lr |= 8;
5349
5350     /* For exceptions we just mark as pending on the NVIC, and let that
5351        handle it.  */
5352     /* TODO: Need to escalate if the current priority is higher than the
5353        one we're raising.  */
5354     switch (cs->exception_index) {
5355     case EXCP_UDEF:
5356         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
5357         return;
5358     case EXCP_SWI:
5359         /* The PC already points to the next instruction.  */
5360         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
5361         return;
5362     case EXCP_PREFETCH_ABORT:
5363     case EXCP_DATA_ABORT:
5364         /* TODO: if we implemented the MPU registers, this is where we
5365          * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5366          */
5367         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
5368         return;
5369     case EXCP_BKPT:
5370         if (semihosting_enabled()) {
5371             int nr;
5372             nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
5373             if (nr == 0xab) {
5374                 env->regs[15] += 2;
5375                 qemu_log_mask(CPU_LOG_INT,
5376                               "...handling as semihosting call 0x%x\n",
5377                               env->regs[0]);
5378                 env->regs[0] = do_arm_semihosting(env);
5379                 return;
5380             }
5381         }
5382         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
5383         return;
5384     case EXCP_IRQ:
5385         env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
5386         break;
5387     case EXCP_EXCEPTION_EXIT:
5388         do_v7m_exception_exit(env);
5389         return;
5390     default:
5391         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
5392         return; /* Never happens.  Keep compiler happy.  */
5393     }
5394
5395     /* Align stack pointer.  */
5396     /* ??? Should only do this if Configuration Control Register
5397        STACKALIGN bit is set.  */
5398     if (env->regs[13] & 4) {
5399         env->regs[13] -= 4;
5400         xpsr |= 0x200;
5401     }
5402     /* Switch to the handler mode.  */
5403     v7m_push(env, xpsr);
5404     v7m_push(env, env->regs[15]);
5405     v7m_push(env, env->regs[14]);
5406     v7m_push(env, env->regs[12]);
5407     v7m_push(env, env->regs[3]);
5408     v7m_push(env, env->regs[2]);
5409     v7m_push(env, env->regs[1]);
5410     v7m_push(env, env->regs[0]);
5411     switch_v7m_sp(env, 0);
5412     /* Clear IT bits */
5413     env->condexec_bits = 0;
5414     env->regs[14] = lr;
5415     addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
5416     env->regs[15] = addr & 0xfffffffe;
5417     env->thumb = addr & 1;
5418 }
5419
5420 /* Function used to synchronize QEMU's AArch64 register set with AArch32
5421  * register set.  This is necessary when switching between AArch32 and AArch64
5422  * execution state.
5423  */
5424 void aarch64_sync_32_to_64(CPUARMState *env)
5425 {
5426     int i;
5427     uint32_t mode = env->uncached_cpsr & CPSR_M;
5428
5429     /* We can blanket copy R[0:7] to X[0:7] */
5430     for (i = 0; i < 8; i++) {
5431         env->xregs[i] = env->regs[i];
5432     }
5433
5434     /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5435      * Otherwise, they come from the banked user regs.
5436      */
5437     if (mode == ARM_CPU_MODE_FIQ) {
5438         for (i = 8; i < 13; i++) {
5439             env->xregs[i] = env->usr_regs[i - 8];
5440         }
5441     } else {
5442         for (i = 8; i < 13; i++) {
5443             env->xregs[i] = env->regs[i];
5444         }
5445     }
5446
5447     /* Registers x13-x23 are the various mode SP and FP registers. Registers
5448      * r13 and r14 are only copied if we are in that mode, otherwise we copy
5449      * from the mode banked register.
5450      */
5451     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5452         env->xregs[13] = env->regs[13];
5453         env->xregs[14] = env->regs[14];
5454     } else {
5455         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5456         /* HYP is an exception in that it is copied from r14 */
5457         if (mode == ARM_CPU_MODE_HYP) {
5458             env->xregs[14] = env->regs[14];
5459         } else {
5460             env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5461         }
5462     }
5463
5464     if (mode == ARM_CPU_MODE_HYP) {
5465         env->xregs[15] = env->regs[13];
5466     } else {
5467         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5468     }
5469
5470     if (mode == ARM_CPU_MODE_IRQ) {
5471         env->xregs[16] = env->regs[14];
5472         env->xregs[17] = env->regs[13];
5473     } else {
5474         env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5475         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
5476     }
5477
5478     if (mode == ARM_CPU_MODE_SVC) {
5479         env->xregs[18] = env->regs[14];
5480         env->xregs[19] = env->regs[13];
5481     } else {
5482         env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5483         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
5484     }
5485
5486     if (mode == ARM_CPU_MODE_ABT) {
5487         env->xregs[20] = env->regs[14];
5488         env->xregs[21] = env->regs[13];
5489     } else {
5490         env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5491         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
5492     }
5493
5494     if (mode == ARM_CPU_MODE_UND) {
5495         env->xregs[22] = env->regs[14];
5496         env->xregs[23] = env->regs[13];
5497     } else {
5498         env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
5499         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
5500     }
5501
5502     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
5503      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
5504      * FIQ bank for r8-r14.
5505      */
5506     if (mode == ARM_CPU_MODE_FIQ) {
5507         for (i = 24; i < 31; i++) {
5508             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
5509         }
5510     } else {
5511         for (i = 24; i < 29; i++) {
5512             env->xregs[i] = env->fiq_regs[i - 24];
5513         }
5514         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
5515         env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
5516     }
5517
5518     env->pc = env->regs[15];
5519 }
5520
5521 /* Function used to synchronize QEMU's AArch32 register set with AArch64
5522  * register set.  This is necessary when switching between AArch32 and AArch64
5523  * execution state.
5524  */
5525 void aarch64_sync_64_to_32(CPUARMState *env)
5526 {
5527     int i;
5528     uint32_t mode = env->uncached_cpsr & CPSR_M;
5529
5530     /* We can blanket copy X[0:7] to R[0:7] */
5531     for (i = 0; i < 8; i++) {
5532         env->regs[i] = env->xregs[i];
5533     }
5534
5535     /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
5536      * Otherwise, we copy x8-x12 into the banked user regs.
5537      */
5538     if (mode == ARM_CPU_MODE_FIQ) {
5539         for (i = 8; i < 13; i++) {
5540             env->usr_regs[i - 8] = env->xregs[i];
5541         }
5542     } else {
5543         for (i = 8; i < 13; i++) {
5544             env->regs[i] = env->xregs[i];
5545         }
5546     }
5547
5548     /* Registers r13 & r14 depend on the current mode.
5549      * If we are in a given mode, we copy the corresponding x registers to r13
5550      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
5551      * for the mode.
5552      */
5553     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5554         env->regs[13] = env->xregs[13];
5555         env->regs[14] = env->xregs[14];
5556     } else {
5557         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
5558
5559         /* HYP is an exception in that it does not have its own banked r14 but
5560          * shares the USR r14
5561          */
5562         if (mode == ARM_CPU_MODE_HYP) {
5563             env->regs[14] = env->xregs[14];
5564         } else {
5565             env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
5566         }
5567     }
5568
5569     if (mode == ARM_CPU_MODE_HYP) {
5570         env->regs[13] = env->xregs[15];
5571     } else {
5572         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
5573     }
5574
5575     if (mode == ARM_CPU_MODE_IRQ) {
5576         env->regs[14] = env->xregs[16];
5577         env->regs[13] = env->xregs[17];
5578     } else {
5579         env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
5580         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
5581     }
5582
5583     if (mode == ARM_CPU_MODE_SVC) {
5584         env->regs[14] = env->xregs[18];
5585         env->regs[13] = env->xregs[19];
5586     } else {
5587         env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
5588         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
5589     }
5590
5591     if (mode == ARM_CPU_MODE_ABT) {
5592         env->regs[14] = env->xregs[20];
5593         env->regs[13] = env->xregs[21];
5594     } else {
5595         env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
5596         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
5597     }
5598
5599     if (mode == ARM_CPU_MODE_UND) {
5600         env->regs[14] = env->xregs[22];
5601         env->regs[13] = env->xregs[23];
5602     } else {
5603         env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
5604         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
5605     }
5606
5607     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
5608      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
5609      * FIQ bank for r8-r14.
5610      */
5611     if (mode == ARM_CPU_MODE_FIQ) {
5612         for (i = 24; i < 31; i++) {
5613             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
5614         }
5615     } else {
5616         for (i = 24; i < 29; i++) {
5617             env->fiq_regs[i - 24] = env->xregs[i];
5618         }
5619         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
5620         env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
5621     }
5622
5623     env->regs[15] = env->pc;
5624 }
5625
5626 /* Handle a CPU exception.  */
5627 void arm_cpu_do_interrupt(CPUState *cs)
5628 {
5629     ARMCPU *cpu = ARM_CPU(cs);
5630     CPUARMState *env = &cpu->env;
5631     uint32_t addr;
5632     uint32_t mask;
5633     int new_mode;
5634     uint32_t offset;
5635     uint32_t moe;
5636
5637     assert(!IS_M(env));
5638
5639     arm_log_exception(cs->exception_index);
5640
5641     if (arm_is_psci_call(cpu, cs->exception_index)) {
5642         arm_handle_psci_call(cpu);
5643         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
5644         return;
5645     }
5646
5647     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
5648     switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
5649     case EC_BREAKPOINT:
5650     case EC_BREAKPOINT_SAME_EL:
5651         moe = 1;
5652         break;
5653     case EC_WATCHPOINT:
5654     case EC_WATCHPOINT_SAME_EL:
5655         moe = 10;
5656         break;
5657     case EC_AA32_BKPT:
5658         moe = 3;
5659         break;
5660     case EC_VECTORCATCH:
5661         moe = 5;
5662         break;
5663     default:
5664         moe = 0;
5665         break;
5666     }
5667
5668     if (moe) {
5669         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
5670     }
5671
5672     /* TODO: Vectored interrupt controller.  */
5673     switch (cs->exception_index) {
5674     case EXCP_UDEF:
5675         new_mode = ARM_CPU_MODE_UND;
5676         addr = 0x04;
5677         mask = CPSR_I;
5678         if (env->thumb)
5679             offset = 2;
5680         else
5681             offset = 4;
5682         break;
5683     case EXCP_SWI:
5684         if (semihosting_enabled()) {
5685             /* Check for semihosting interrupt.  */
5686             if (env->thumb) {
5687                 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
5688                     & 0xff;
5689             } else {
5690                 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
5691                     & 0xffffff;
5692             }
5693             /* Only intercept calls from privileged modes, to provide some
5694                semblance of security.  */
5695             if (((mask == 0x123456 && !env->thumb)
5696                     || (mask == 0xab && env->thumb))
5697                   && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
5698                 qemu_log_mask(CPU_LOG_INT,
5699                               "...handling as semihosting call 0x%x\n",
5700                               env->regs[0]);
5701                 env->regs[0] = do_arm_semihosting(env);
5702                 return;
5703             }
5704         }
5705         new_mode = ARM_CPU_MODE_SVC;
5706         addr = 0x08;
5707         mask = CPSR_I;
5708         /* The PC already points to the next instruction.  */
5709         offset = 0;
5710         break;
5711     case EXCP_BKPT:
5712         /* See if this is a semihosting syscall.  */
5713         if (env->thumb && semihosting_enabled()) {
5714             mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
5715             if (mask == 0xab
5716                   && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
5717                 env->regs[15] += 2;
5718                 qemu_log_mask(CPU_LOG_INT,
5719                               "...handling as semihosting call 0x%x\n",
5720                               env->regs[0]);
5721                 env->regs[0] = do_arm_semihosting(env);
5722                 return;
5723             }
5724         }
5725         env->exception.fsr = 2;
5726         /* Fall through to prefetch abort.  */
5727     case EXCP_PREFETCH_ABORT:
5728         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
5729         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
5730         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
5731                       env->exception.fsr, (uint32_t)env->exception.vaddress);
5732         new_mode = ARM_CPU_MODE_ABT;
5733         addr = 0x0c;
5734         mask = CPSR_A | CPSR_I;
5735         offset = 4;
5736         break;
5737     case EXCP_DATA_ABORT:
5738         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
5739         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
5740         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
5741                       env->exception.fsr,
5742                       (uint32_t)env->exception.vaddress);
5743         new_mode = ARM_CPU_MODE_ABT;
5744         addr = 0x10;
5745         mask = CPSR_A | CPSR_I;
5746         offset = 8;
5747         break;
5748     case EXCP_IRQ:
5749         new_mode = ARM_CPU_MODE_IRQ;
5750         addr = 0x18;
5751         /* Disable IRQ and imprecise data aborts.  */
5752         mask = CPSR_A | CPSR_I;
5753         offset = 4;
5754         if (env->cp15.scr_el3 & SCR_IRQ) {
5755             /* IRQ routed to monitor mode */
5756             new_mode = ARM_CPU_MODE_MON;
5757             mask |= CPSR_F;
5758         }
5759         break;
5760     case EXCP_FIQ:
5761         new_mode = ARM_CPU_MODE_FIQ;
5762         addr = 0x1c;
5763         /* Disable FIQ, IRQ and imprecise data aborts.  */
5764         mask = CPSR_A | CPSR_I | CPSR_F;
5765         if (env->cp15.scr_el3 & SCR_FIQ) {
5766             /* FIQ routed to monitor mode */
5767             new_mode = ARM_CPU_MODE_MON;
5768         }
5769         offset = 4;
5770         break;
5771     case EXCP_SMC:
5772         new_mode = ARM_CPU_MODE_MON;
5773         addr = 0x08;
5774         mask = CPSR_A | CPSR_I | CPSR_F;
5775         offset = 0;
5776         break;
5777     default:
5778         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
5779         return; /* Never happens.  Keep compiler happy.  */
5780     }
5781
5782     if (new_mode == ARM_CPU_MODE_MON) {
5783         addr += env->cp15.mvbar;
5784     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
5785         /* High vectors. When enabled, base address cannot be remapped. */
5786         addr += 0xffff0000;
5787     } else {
5788         /* ARM v7 architectures provide a vector base address register to remap
5789          * the interrupt vector table.
5790          * This register is only followed in non-monitor mode, and is banked.
5791          * Note: only bits 31:5 are valid.
5792          */
5793         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
5794     }
5795
5796     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
5797         env->cp15.scr_el3 &= ~SCR_NS;
5798     }
5799
5800     switch_mode (env, new_mode);
5801     /* For exceptions taken to AArch32 we must clear the SS bit in both
5802      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
5803      */
5804     env->uncached_cpsr &= ~PSTATE_SS;
5805     env->spsr = cpsr_read(env);
5806     /* Clear IT bits.  */
5807     env->condexec_bits = 0;
5808     /* Switch to the new mode, and to the correct instruction set.  */
5809     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
5810     env->daif |= mask;
5811     /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
5812      * and we should just guard the thumb mode on V4 */
5813     if (arm_feature(env, ARM_FEATURE_V4T)) {
5814         env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
5815     }
5816     env->regs[14] = env->regs[15] + offset;
5817     env->regs[15] = addr;
5818     cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
5819 }
5820
5821
5822 /* Return the exception level which controls this address translation regime */
5823 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
5824 {
5825     switch (mmu_idx) {
5826     case ARMMMUIdx_S2NS:
5827     case ARMMMUIdx_S1E2:
5828         return 2;
5829     case ARMMMUIdx_S1E3:
5830         return 3;
5831     case ARMMMUIdx_S1SE0:
5832         return arm_el_is_aa64(env, 3) ? 1 : 3;
5833     case ARMMMUIdx_S1SE1:
5834     case ARMMMUIdx_S1NSE0:
5835     case ARMMMUIdx_S1NSE1:
5836         return 1;
5837     default:
5838         g_assert_not_reached();
5839     }
5840 }
5841
5842 /* Return true if this address translation regime is secure */
5843 static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
5844 {
5845     switch (mmu_idx) {
5846     case ARMMMUIdx_S12NSE0:
5847     case ARMMMUIdx_S12NSE1:
5848     case ARMMMUIdx_S1NSE0:
5849     case ARMMMUIdx_S1NSE1:
5850     case ARMMMUIdx_S1E2:
5851     case ARMMMUIdx_S2NS:
5852         return false;
5853     case ARMMMUIdx_S1E3:
5854     case ARMMMUIdx_S1SE0:
5855     case ARMMMUIdx_S1SE1:
5856         return true;
5857     default:
5858         g_assert_not_reached();
5859     }
5860 }
5861
5862 /* Return the SCTLR value which controls this address translation regime */
5863 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
5864 {
5865     return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
5866 }
5867
5868 /* Return true if the specified stage of address translation is disabled */
5869 static inline bool regime_translation_disabled(CPUARMState *env,
5870                                                ARMMMUIdx mmu_idx)
5871 {
5872     if (mmu_idx == ARMMMUIdx_S2NS) {
5873         return (env->cp15.hcr_el2 & HCR_VM) == 0;
5874     }
5875     return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
5876 }
5877
5878 /* Return the TCR controlling this translation regime */
5879 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
5880 {
5881     if (mmu_idx == ARMMMUIdx_S2NS) {
5882         return &env->cp15.vtcr_el2;
5883     }
5884     return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
5885 }
5886
5887 /* Return the TTBR associated with this translation regime */
5888 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
5889                                    int ttbrn)
5890 {
5891     if (mmu_idx == ARMMMUIdx_S2NS) {
5892         return env->cp15.vttbr_el2;
5893     }
5894     if (ttbrn == 0) {
5895         return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
5896     } else {
5897         return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
5898     }
5899 }
5900
5901 /* Return true if the translation regime is using LPAE format page tables */
5902 static inline bool regime_using_lpae_format(CPUARMState *env,
5903                                             ARMMMUIdx mmu_idx)
5904 {
5905     int el = regime_el(env, mmu_idx);
5906     if (el == 2 || arm_el_is_aa64(env, el)) {
5907         return true;
5908     }
5909     if (arm_feature(env, ARM_FEATURE_LPAE)
5910         && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
5911         return true;
5912     }
5913     return false;
5914 }
5915
5916 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
5917 {
5918     switch (mmu_idx) {
5919     case ARMMMUIdx_S1SE0:
5920     case ARMMMUIdx_S1NSE0:
5921         return true;
5922     default:
5923         return false;
5924     case ARMMMUIdx_S12NSE0:
5925     case ARMMMUIdx_S12NSE1:
5926         g_assert_not_reached();
5927     }
5928 }
5929
5930 /* Translate section/page access permissions to page
5931  * R/W protection flags
5932  *
5933  * @env:         CPUARMState
5934  * @mmu_idx:     MMU index indicating required translation regime
5935  * @ap:          The 3-bit access permissions (AP[2:0])
5936  * @domain_prot: The 2-bit domain access permissions
5937  */
5938 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
5939                                 int ap, int domain_prot)
5940 {
5941     bool is_user = regime_is_user(env, mmu_idx);
5942
5943     if (domain_prot == 3) {
5944         return PAGE_READ | PAGE_WRITE;
5945     }
5946
5947     switch (ap) {
5948     case 0:
5949         if (arm_feature(env, ARM_FEATURE_V7)) {
5950             return 0;
5951         }
5952         switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
5953         case SCTLR_S:
5954             return is_user ? 0 : PAGE_READ;
5955         case SCTLR_R:
5956             return PAGE_READ;
5957         default:
5958             return 0;
5959         }
5960     case 1:
5961         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
5962     case 2:
5963         if (is_user) {
5964             return PAGE_READ;
5965         } else {
5966             return PAGE_READ | PAGE_WRITE;
5967         }
5968     case 3:
5969         return PAGE_READ | PAGE_WRITE;
5970     case 4: /* Reserved.  */
5971         return 0;
5972     case 5:
5973         return is_user ? 0 : PAGE_READ;
5974     case 6:
5975         return PAGE_READ;
5976     case 7:
5977         if (!arm_feature(env, ARM_FEATURE_V6K)) {
5978             return 0;
5979         }
5980         return PAGE_READ;
5981     default:
5982         g_assert_not_reached();
5983     }
5984 }
5985
5986 /* Translate section/page access permissions to page
5987  * R/W protection flags.
5988  *
5989  * @ap:      The 2-bit simple AP (AP[2:1])
5990  * @is_user: TRUE if accessing from PL0
5991  */
5992 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
5993 {
5994     switch (ap) {
5995     case 0:
5996         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
5997     case 1:
5998         return PAGE_READ | PAGE_WRITE;
5999     case 2:
6000         return is_user ? 0 : PAGE_READ;
6001     case 3:
6002         return PAGE_READ;
6003     default:
6004         g_assert_not_reached();
6005     }
6006 }
6007
6008 static inline int
6009 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
6010 {
6011     return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
6012 }
6013
6014 /* Translate section/page access permissions to protection flags
6015  *
6016  * @env:     CPUARMState
6017  * @mmu_idx: MMU index indicating required translation regime
6018  * @is_aa64: TRUE if AArch64
6019  * @ap:      The 2-bit simple AP (AP[2:1])
6020  * @ns:      NS (non-secure) bit
6021  * @xn:      XN (execute-never) bit
6022  * @pxn:     PXN (privileged execute-never) bit
6023  */
6024 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
6025                       int ap, int ns, int xn, int pxn)
6026 {
6027     bool is_user = regime_is_user(env, mmu_idx);
6028     int prot_rw, user_rw;
6029     bool have_wxn;
6030     int wxn = 0;
6031
6032     assert(mmu_idx != ARMMMUIdx_S2NS);
6033
6034     user_rw = simple_ap_to_rw_prot_is_user(ap, true);
6035     if (is_user) {
6036         prot_rw = user_rw;
6037     } else {
6038         prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
6039     }
6040
6041     if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
6042         return prot_rw;
6043     }
6044
6045     /* TODO have_wxn should be replaced with
6046      *   ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
6047      * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
6048      * compatible processors have EL2, which is required for [U]WXN.
6049      */
6050     have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
6051
6052     if (have_wxn) {
6053         wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
6054     }
6055
6056     if (is_aa64) {
6057         switch (regime_el(env, mmu_idx)) {
6058         case 1:
6059             if (!is_user) {
6060                 xn = pxn || (user_rw & PAGE_WRITE);
6061             }
6062             break;
6063         case 2:
6064         case 3:
6065             break;
6066         }
6067     } else if (arm_feature(env, ARM_FEATURE_V7)) {
6068         switch (regime_el(env, mmu_idx)) {
6069         case 1:
6070         case 3:
6071             if (is_user) {
6072                 xn = xn || !(user_rw & PAGE_READ);
6073             } else {
6074                 int uwxn = 0;
6075                 if (have_wxn) {
6076                     uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6077                 }
6078                 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6079                      (uwxn && (user_rw & PAGE_WRITE));
6080             }
6081             break;
6082         case 2:
6083             break;
6084         }
6085     } else {
6086         xn = wxn = 0;
6087     }
6088
6089     if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6090         return prot_rw;
6091     }
6092     return prot_rw | PAGE_EXEC;
6093 }
6094
6095 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6096                                      uint32_t *table, uint32_t address)
6097 {
6098     /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
6099     TCR *tcr = regime_tcr(env, mmu_idx);
6100
6101     if (address & tcr->mask) {
6102         if (tcr->raw_tcr & TTBCR_PD1) {
6103             /* Translation table walk disabled for TTBR1 */
6104             return false;
6105         }
6106         *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
6107     } else {
6108         if (tcr->raw_tcr & TTBCR_PD0) {
6109             /* Translation table walk disabled for TTBR0 */
6110             return false;
6111         }
6112         *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
6113     }
6114     *table |= (address >> 18) & 0x3ffc;
6115     return true;
6116 }
6117
6118 /* All loads done in the course of a page table walk go through here.
6119  * TODO: rather than ignoring errors from physical memory reads (which
6120  * are external aborts in ARM terminology) we should propagate this
6121  * error out so that we can turn it into a Data Abort if this walk
6122  * was being done for a CPU load/store or an address translation instruction
6123  * (but not if it was for a debug access).
6124  */
6125 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure)
6126 {
6127     MemTxAttrs attrs = {};
6128
6129     attrs.secure = is_secure;
6130     return address_space_ldl(cs->as, addr, attrs, NULL);
6131 }
6132
6133 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure)
6134 {
6135     MemTxAttrs attrs = {};
6136
6137     attrs.secure = is_secure;
6138     return address_space_ldq(cs->as, addr, attrs, NULL);
6139 }
6140
6141 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6142                              int access_type, ARMMMUIdx mmu_idx,
6143                              hwaddr *phys_ptr, int *prot,
6144                              target_ulong *page_size, uint32_t *fsr)
6145 {
6146     CPUState *cs = CPU(arm_env_get_cpu(env));
6147     int code;
6148     uint32_t table;
6149     uint32_t desc;
6150     int type;
6151     int ap;
6152     int domain = 0;
6153     int domain_prot;
6154     hwaddr phys_addr;
6155     uint32_t dacr;
6156
6157     /* Pagetable walk.  */
6158     /* Lookup l1 descriptor.  */
6159     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
6160         /* Section translation fault if page walk is disabled by PD0 or PD1 */
6161         code = 5;
6162         goto do_fault;
6163     }
6164     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6165     type = (desc & 3);
6166     domain = (desc >> 5) & 0x0f;
6167     if (regime_el(env, mmu_idx) == 1) {
6168         dacr = env->cp15.dacr_ns;
6169     } else {
6170         dacr = env->cp15.dacr_s;
6171     }
6172     domain_prot = (dacr >> (domain * 2)) & 3;
6173     if (type == 0) {
6174         /* Section translation fault.  */
6175         code = 5;
6176         goto do_fault;
6177     }
6178     if (domain_prot == 0 || domain_prot == 2) {
6179         if (type == 2)
6180             code = 9; /* Section domain fault.  */
6181         else
6182             code = 11; /* Page domain fault.  */
6183         goto do_fault;
6184     }
6185     if (type == 2) {
6186         /* 1Mb section.  */
6187         phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6188         ap = (desc >> 10) & 3;
6189         code = 13;
6190         *page_size = 1024 * 1024;
6191     } else {
6192         /* Lookup l2 entry.  */
6193         if (type == 1) {
6194             /* Coarse pagetable.  */
6195             table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6196         } else {
6197             /* Fine pagetable.  */
6198             table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6199         }
6200         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6201         switch (desc & 3) {
6202         case 0: /* Page translation fault.  */
6203             code = 7;
6204             goto do_fault;
6205         case 1: /* 64k page.  */
6206             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6207             ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
6208             *page_size = 0x10000;
6209             break;
6210         case 2: /* 4k page.  */
6211             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6212             ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
6213             *page_size = 0x1000;
6214             break;
6215         case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
6216             if (type == 1) {
6217                 /* ARMv6/XScale extended small page format */
6218                 if (arm_feature(env, ARM_FEATURE_XSCALE)
6219                     || arm_feature(env, ARM_FEATURE_V6)) {
6220                     phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6221                     *page_size = 0x1000;
6222                 } else {
6223                     /* UNPREDICTABLE in ARMv5; we choose to take a
6224                      * page translation fault.
6225                      */
6226                     code = 7;
6227                     goto do_fault;
6228                 }
6229             } else {
6230                 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
6231                 *page_size = 0x400;
6232             }
6233             ap = (desc >> 4) & 3;
6234             break;
6235         default:
6236             /* Never happens, but compiler isn't smart enough to tell.  */
6237             abort();
6238         }
6239         code = 15;
6240     }
6241     *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6242     *prot |= *prot ? PAGE_EXEC : 0;
6243     if (!(*prot & (1 << access_type))) {
6244         /* Access permission fault.  */
6245         goto do_fault;
6246     }
6247     *phys_ptr = phys_addr;
6248     return false;
6249 do_fault:
6250     *fsr = code | (domain << 4);
6251     return true;
6252 }
6253
6254 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
6255                              int access_type, ARMMMUIdx mmu_idx,
6256                              hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
6257                              target_ulong *page_size, uint32_t *fsr)
6258 {
6259     CPUState *cs = CPU(arm_env_get_cpu(env));
6260     int code;
6261     uint32_t table;
6262     uint32_t desc;
6263     uint32_t xn;
6264     uint32_t pxn = 0;
6265     int type;
6266     int ap;
6267     int domain = 0;
6268     int domain_prot;
6269     hwaddr phys_addr;
6270     uint32_t dacr;
6271     bool ns;
6272
6273     /* Pagetable walk.  */
6274     /* Lookup l1 descriptor.  */
6275     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
6276         /* Section translation fault if page walk is disabled by PD0 or PD1 */
6277         code = 5;
6278         goto do_fault;
6279     }
6280     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6281     type = (desc & 3);
6282     if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
6283         /* Section translation fault, or attempt to use the encoding
6284          * which is Reserved on implementations without PXN.
6285          */
6286         code = 5;
6287         goto do_fault;
6288     }
6289     if ((type == 1) || !(desc & (1 << 18))) {
6290         /* Page or Section.  */
6291         domain = (desc >> 5) & 0x0f;
6292     }
6293     if (regime_el(env, mmu_idx) == 1) {
6294         dacr = env->cp15.dacr_ns;
6295     } else {
6296         dacr = env->cp15.dacr_s;
6297     }
6298     domain_prot = (dacr >> (domain * 2)) & 3;
6299     if (domain_prot == 0 || domain_prot == 2) {
6300         if (type != 1) {
6301             code = 9; /* Section domain fault.  */
6302         } else {
6303             code = 11; /* Page domain fault.  */
6304         }
6305         goto do_fault;
6306     }
6307     if (type != 1) {
6308         if (desc & (1 << 18)) {
6309             /* Supersection.  */
6310             phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
6311             phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
6312             phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
6313             *page_size = 0x1000000;
6314         } else {
6315             /* Section.  */
6316             phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6317             *page_size = 0x100000;
6318         }
6319         ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
6320         xn = desc & (1 << 4);
6321         pxn = desc & 1;
6322         code = 13;
6323         ns = extract32(desc, 19, 1);
6324     } else {
6325         if (arm_feature(env, ARM_FEATURE_PXN)) {
6326             pxn = (desc >> 2) & 1;
6327         }
6328         ns = extract32(desc, 3, 1);
6329         /* Lookup l2 entry.  */
6330         table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6331         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6332         ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
6333         switch (desc & 3) {
6334         case 0: /* Page translation fault.  */
6335             code = 7;
6336             goto do_fault;
6337         case 1: /* 64k page.  */
6338             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6339             xn = desc & (1 << 15);
6340             *page_size = 0x10000;
6341             break;
6342         case 2: case 3: /* 4k page.  */
6343             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6344             xn = desc & 1;
6345             *page_size = 0x1000;
6346             break;
6347         default:
6348             /* Never happens, but compiler isn't smart enough to tell.  */
6349             abort();
6350         }
6351         code = 15;
6352     }
6353     if (domain_prot == 3) {
6354         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
6355     } else {
6356         if (pxn && !regime_is_user(env, mmu_idx)) {
6357             xn = 1;
6358         }
6359         if (xn && access_type == 2)
6360             goto do_fault;
6361
6362         if (arm_feature(env, ARM_FEATURE_V6K) &&
6363                 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
6364             /* The simplified model uses AP[0] as an access control bit.  */
6365             if ((ap & 1) == 0) {
6366                 /* Access flag fault.  */
6367                 code = (code == 15) ? 6 : 3;
6368                 goto do_fault;
6369             }
6370             *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
6371         } else {
6372             *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6373         }
6374         if (*prot && !xn) {
6375             *prot |= PAGE_EXEC;
6376         }
6377         if (!(*prot & (1 << access_type))) {
6378             /* Access permission fault.  */
6379             goto do_fault;
6380         }
6381     }
6382     if (ns) {
6383         /* The NS bit will (as required by the architecture) have no effect if
6384          * the CPU doesn't support TZ or this is a non-secure translation
6385          * regime, because the attribute will already be non-secure.
6386          */
6387         attrs->secure = false;
6388     }
6389     *phys_ptr = phys_addr;
6390     return false;
6391 do_fault:
6392     *fsr = code | (domain << 4);
6393     return true;
6394 }
6395
6396 /* Fault type for long-descriptor MMU fault reporting; this corresponds
6397  * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
6398  */
6399 typedef enum {
6400     translation_fault = 1,
6401     access_fault = 2,
6402     permission_fault = 3,
6403 } MMUFaultType;
6404
6405 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
6406                                int access_type, ARMMMUIdx mmu_idx,
6407                                hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
6408                                target_ulong *page_size_ptr, uint32_t *fsr)
6409 {
6410     CPUState *cs = CPU(arm_env_get_cpu(env));
6411     /* Read an LPAE long-descriptor translation table. */
6412     MMUFaultType fault_type = translation_fault;
6413     uint32_t level = 1;
6414     uint32_t epd = 0;
6415     int32_t tsz;
6416     uint32_t tg;
6417     uint64_t ttbr;
6418     int ttbr_select;
6419     hwaddr descaddr, descmask;
6420     uint32_t tableattrs;
6421     target_ulong page_size;
6422     uint32_t attrs;
6423     int32_t granule_sz = 9;
6424     int32_t va_size = 32;
6425     int32_t tbi = 0;
6426     TCR *tcr = regime_tcr(env, mmu_idx);
6427     int ap, ns, xn, pxn;
6428     uint32_t el = regime_el(env, mmu_idx);
6429     bool ttbr1_valid = true;
6430
6431     /* TODO:
6432      * This code does not handle the different format TCR for VTCR_EL2.
6433      * This code also does not support shareability levels.
6434      * Attribute and permission bit handling should also be checked when adding
6435      * support for those page table walks.
6436      */
6437     if (arm_el_is_aa64(env, el)) {
6438         va_size = 64;
6439         if (el > 1) {
6440             if (mmu_idx != ARMMMUIdx_S2NS) {
6441                 tbi = extract64(tcr->raw_tcr, 20, 1);
6442             }
6443         } else {
6444             if (extract64(address, 55, 1)) {
6445                 tbi = extract64(tcr->raw_tcr, 38, 1);
6446             } else {
6447                 tbi = extract64(tcr->raw_tcr, 37, 1);
6448             }
6449         }
6450         tbi *= 8;
6451
6452         /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
6453          * invalid.
6454          */
6455         if (el > 1) {
6456             ttbr1_valid = false;
6457         }
6458     } else {
6459         /* There is no TTBR1 for EL2 */
6460         if (el == 2) {
6461             ttbr1_valid = false;
6462         }
6463     }
6464
6465     /* Determine whether this address is in the region controlled by
6466      * TTBR0 or TTBR1 (or if it is in neither region and should fault).
6467      * This is a Non-secure PL0/1 stage 1 translation, so controlled by
6468      * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
6469      */
6470     uint32_t t0sz = extract32(tcr->raw_tcr, 0, 6);
6471     if (va_size == 64) {
6472         t0sz = MIN(t0sz, 39);
6473         t0sz = MAX(t0sz, 16);
6474     }
6475     uint32_t t1sz = extract32(tcr->raw_tcr, 16, 6);
6476     if (va_size == 64) {
6477         t1sz = MIN(t1sz, 39);
6478         t1sz = MAX(t1sz, 16);
6479     }
6480     if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
6481         /* there is a ttbr0 region and we are in it (high bits all zero) */
6482         ttbr_select = 0;
6483     } else if (ttbr1_valid && t1sz &&
6484                !extract64(~address, va_size - t1sz, t1sz - tbi)) {
6485         /* there is a ttbr1 region and we are in it (high bits all one) */
6486         ttbr_select = 1;
6487     } else if (!t0sz) {
6488         /* ttbr0 region is "everything not in the ttbr1 region" */
6489         ttbr_select = 0;
6490     } else if (!t1sz && ttbr1_valid) {
6491         /* ttbr1 region is "everything not in the ttbr0 region" */
6492         ttbr_select = 1;
6493     } else {
6494         /* in the gap between the two regions, this is a Translation fault */
6495         fault_type = translation_fault;
6496         goto do_fault;
6497     }
6498
6499     /* Note that QEMU ignores shareability and cacheability attributes,
6500      * so we don't need to do anything with the SH, ORGN, IRGN fields
6501      * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
6502      * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
6503      * implement any ASID-like capability so we can ignore it (instead
6504      * we will always flush the TLB any time the ASID is changed).
6505      */
6506     if (ttbr_select == 0) {
6507         ttbr = regime_ttbr(env, mmu_idx, 0);
6508         if (el < 2) {
6509             epd = extract32(tcr->raw_tcr, 7, 1);
6510         }
6511         tsz = t0sz;
6512
6513         tg = extract32(tcr->raw_tcr, 14, 2);
6514         if (tg == 1) { /* 64KB pages */
6515             granule_sz = 13;
6516         }
6517         if (tg == 2) { /* 16KB pages */
6518             granule_sz = 11;
6519         }
6520     } else {
6521         /* We should only be here if TTBR1 is valid */
6522         assert(ttbr1_valid);
6523
6524         ttbr = regime_ttbr(env, mmu_idx, 1);
6525         epd = extract32(tcr->raw_tcr, 23, 1);
6526         tsz = t1sz;
6527
6528         tg = extract32(tcr->raw_tcr, 30, 2);
6529         if (tg == 3)  { /* 64KB pages */
6530             granule_sz = 13;
6531         }
6532         if (tg == 1) { /* 16KB pages */
6533             granule_sz = 11;
6534         }
6535     }
6536
6537     /* Here we should have set up all the parameters for the translation:
6538      * va_size, ttbr, epd, tsz, granule_sz, tbi
6539      */
6540
6541     if (epd) {
6542         /* Translation table walk disabled => Translation fault on TLB miss
6543          * Note: This is always 0 on 64-bit EL2 and EL3.
6544          */
6545         goto do_fault;
6546     }
6547
6548     /* The starting level depends on the virtual address size (which can be
6549      * up to 48 bits) and the translation granule size. It indicates the number
6550      * of strides (granule_sz bits at a time) needed to consume the bits
6551      * of the input address. In the pseudocode this is:
6552      *  level = 4 - RoundUp((inputsize - grainsize) / stride)
6553      * where their 'inputsize' is our 'va_size - tsz', 'grainsize' is
6554      * our 'granule_sz + 3' and 'stride' is our 'granule_sz'.
6555      * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
6556      *     = 4 - (va_size - tsz - granule_sz - 3 + granule_sz - 1) / granule_sz
6557      *     = 4 - (va_size - tsz - 4) / granule_sz;
6558      */
6559     level = 4 - (va_size - tsz - 4) / granule_sz;
6560
6561     /* Clear the vaddr bits which aren't part of the within-region address,
6562      * so that we don't have to special case things when calculating the
6563      * first descriptor address.
6564      */
6565     if (tsz) {
6566         address &= (1ULL << (va_size - tsz)) - 1;
6567     }
6568
6569     descmask = (1ULL << (granule_sz + 3)) - 1;
6570
6571     /* Now we can extract the actual base address from the TTBR */
6572     descaddr = extract64(ttbr, 0, 48);
6573     descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
6574
6575     /* Secure accesses start with the page table in secure memory and
6576      * can be downgraded to non-secure at any step. Non-secure accesses
6577      * remain non-secure. We implement this by just ORing in the NSTable/NS
6578      * bits at each step.
6579      */
6580     tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
6581     for (;;) {
6582         uint64_t descriptor;
6583         bool nstable;
6584
6585         descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
6586         descaddr &= ~7ULL;
6587         nstable = extract32(tableattrs, 4, 1);
6588         descriptor = arm_ldq_ptw(cs, descaddr, !nstable);
6589         if (!(descriptor & 1) ||
6590             (!(descriptor & 2) && (level == 3))) {
6591             /* Invalid, or the Reserved level 3 encoding */
6592             goto do_fault;
6593         }
6594         descaddr = descriptor & 0xfffffff000ULL;
6595
6596         if ((descriptor & 2) && (level < 3)) {
6597             /* Table entry. The top five bits are attributes which  may
6598              * propagate down through lower levels of the table (and
6599              * which are all arranged so that 0 means "no effect", so
6600              * we can gather them up by ORing in the bits at each level).
6601              */
6602             tableattrs |= extract64(descriptor, 59, 5);
6603             level++;
6604             continue;
6605         }
6606         /* Block entry at level 1 or 2, or page entry at level 3.
6607          * These are basically the same thing, although the number
6608          * of bits we pull in from the vaddr varies.
6609          */
6610         page_size = (1ULL << ((granule_sz * (4 - level)) + 3));
6611         descaddr |= (address & (page_size - 1));
6612         /* Extract attributes from the descriptor and merge with table attrs */
6613         attrs = extract64(descriptor, 2, 10)
6614             | (extract64(descriptor, 52, 12) << 10);
6615         attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
6616         attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
6617         /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
6618          * means "force PL1 access only", which means forcing AP[1] to 0.
6619          */
6620         if (extract32(tableattrs, 2, 1)) {
6621             attrs &= ~(1 << 4);
6622         }
6623         attrs |= nstable << 3; /* NS */
6624         break;
6625     }
6626     /* Here descaddr is the final physical address, and attributes
6627      * are all in attrs.
6628      */
6629     fault_type = access_fault;
6630     if ((attrs & (1 << 8)) == 0) {
6631         /* Access flag */
6632         goto do_fault;
6633     }
6634
6635     ap = extract32(attrs, 4, 2);
6636     ns = extract32(attrs, 3, 1);
6637     xn = extract32(attrs, 12, 1);
6638     pxn = extract32(attrs, 11, 1);
6639
6640     *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
6641
6642     fault_type = permission_fault;
6643     if (!(*prot & (1 << access_type))) {
6644         goto do_fault;
6645     }
6646
6647     if (ns) {
6648         /* The NS bit will (as required by the architecture) have no effect if
6649          * the CPU doesn't support TZ or this is a non-secure translation
6650          * regime, because the attribute will already be non-secure.
6651          */
6652         txattrs->secure = false;
6653     }
6654     *phys_ptr = descaddr;
6655     *page_size_ptr = page_size;
6656     return false;
6657
6658 do_fault:
6659     /* Long-descriptor format IFSR/DFSR value */
6660     *fsr = (1 << 9) | (fault_type << 2) | level;
6661     return true;
6662 }
6663
6664 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
6665                                                 ARMMMUIdx mmu_idx,
6666                                                 int32_t address, int *prot)
6667 {
6668     *prot = PAGE_READ | PAGE_WRITE;
6669     switch (address) {
6670     case 0xF0000000 ... 0xFFFFFFFF:
6671         if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
6672             *prot |= PAGE_EXEC;
6673         }
6674         break;
6675     case 0x00000000 ... 0x7FFFFFFF:
6676         *prot |= PAGE_EXEC;
6677         break;
6678     }
6679
6680 }
6681
6682 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
6683                                  int access_type, ARMMMUIdx mmu_idx,
6684                                  hwaddr *phys_ptr, int *prot, uint32_t *fsr)
6685 {
6686     ARMCPU *cpu = arm_env_get_cpu(env);
6687     int n;
6688     bool is_user = regime_is_user(env, mmu_idx);
6689
6690     *phys_ptr = address;
6691     *prot = 0;
6692
6693     if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
6694         get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
6695     } else { /* MPU enabled */
6696         for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
6697             /* region search */
6698             uint32_t base = env->pmsav7.drbar[n];
6699             uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
6700             uint32_t rmask;
6701             bool srdis = false;
6702
6703             if (!(env->pmsav7.drsr[n] & 0x1)) {
6704                 continue;
6705             }
6706
6707             if (!rsize) {
6708                 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
6709                 continue;
6710             }
6711             rsize++;
6712             rmask = (1ull << rsize) - 1;
6713
6714             if (base & rmask) {
6715                 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
6716                               "to DRSR region size, mask = %" PRIx32,
6717                               base, rmask);
6718                 continue;
6719             }
6720
6721             if (address < base || address > base + rmask) {
6722                 continue;
6723             }
6724
6725             /* Region matched */
6726
6727             if (rsize >= 8) { /* no subregions for regions < 256 bytes */
6728                 int i, snd;
6729                 uint32_t srdis_mask;
6730
6731                 rsize -= 3; /* sub region size (power of 2) */
6732                 snd = ((address - base) >> rsize) & 0x7;
6733                 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
6734
6735                 srdis_mask = srdis ? 0x3 : 0x0;
6736                 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
6737                     /* This will check in groups of 2, 4 and then 8, whether
6738                      * the subregion bits are consistent. rsize is incremented
6739                      * back up to give the region size, considering consistent
6740                      * adjacent subregions as one region. Stop testing if rsize
6741                      * is already big enough for an entire QEMU page.
6742                      */
6743                     int snd_rounded = snd & ~(i - 1);
6744                     uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
6745                                                      snd_rounded + 8, i);
6746                     if (srdis_mask ^ srdis_multi) {
6747                         break;
6748                     }
6749                     srdis_mask = (srdis_mask << i) | srdis_mask;
6750                     rsize++;
6751                 }
6752             }
6753             if (rsize < TARGET_PAGE_BITS) {
6754                 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
6755                               "alignment of %" PRIu32 " bits. Minimum is %d\n",
6756                               rsize, TARGET_PAGE_BITS);
6757                 continue;
6758             }
6759             if (srdis) {
6760                 continue;
6761             }
6762             break;
6763         }
6764
6765         if (n == -1) { /* no hits */
6766             if (cpu->pmsav7_dregion &&
6767                 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
6768                 /* background fault */
6769                 *fsr = 0;
6770                 return true;
6771             }
6772             get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
6773         } else { /* a MPU hit! */
6774             uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
6775
6776             if (is_user) { /* User mode AP bit decoding */
6777                 switch (ap) {
6778                 case 0:
6779                 case 1:
6780                 case 5:
6781                     break; /* no access */
6782                 case 3:
6783                     *prot |= PAGE_WRITE;
6784                     /* fall through */
6785                 case 2:
6786                 case 6:
6787                     *prot |= PAGE_READ | PAGE_EXEC;
6788                     break;
6789                 default:
6790                     qemu_log_mask(LOG_GUEST_ERROR,
6791                                   "Bad value for AP bits in DRACR %"
6792                                   PRIx32 "\n", ap);
6793                 }
6794             } else { /* Priv. mode AP bits decoding */
6795                 switch (ap) {
6796                 case 0:
6797                     break; /* no access */
6798                 case 1:
6799                 case 2:
6800                 case 3:
6801                     *prot |= PAGE_WRITE;
6802                     /* fall through */
6803                 case 5:
6804                 case 6:
6805                     *prot |= PAGE_READ | PAGE_EXEC;
6806                     break;
6807                 default:
6808                     qemu_log_mask(LOG_GUEST_ERROR,
6809                                   "Bad value for AP bits in DRACR %"
6810                                   PRIx32 "\n", ap);
6811                 }
6812             }
6813
6814             /* execute never */
6815             if (env->pmsav7.dracr[n] & (1 << 12)) {
6816                 *prot &= ~PAGE_EXEC;
6817             }
6818         }
6819     }
6820
6821     *fsr = 0x00d; /* Permission fault */
6822     return !(*prot & (1 << access_type));
6823 }
6824
6825 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
6826                                  int access_type, ARMMMUIdx mmu_idx,
6827                                  hwaddr *phys_ptr, int *prot, uint32_t *fsr)
6828 {
6829     int n;
6830     uint32_t mask;
6831     uint32_t base;
6832     bool is_user = regime_is_user(env, mmu_idx);
6833
6834     *phys_ptr = address;
6835     for (n = 7; n >= 0; n--) {
6836         base = env->cp15.c6_region[n];
6837         if ((base & 1) == 0) {
6838             continue;
6839         }
6840         mask = 1 << ((base >> 1) & 0x1f);
6841         /* Keep this shift separate from the above to avoid an
6842            (undefined) << 32.  */
6843         mask = (mask << 1) - 1;
6844         if (((base ^ address) & ~mask) == 0) {
6845             break;
6846         }
6847     }
6848     if (n < 0) {
6849         *fsr = 2;
6850         return true;
6851     }
6852
6853     if (access_type == 2) {
6854         mask = env->cp15.pmsav5_insn_ap;
6855     } else {
6856         mask = env->cp15.pmsav5_data_ap;
6857     }
6858     mask = (mask >> (n * 4)) & 0xf;
6859     switch (mask) {
6860     case 0:
6861         *fsr = 1;
6862         return true;
6863     case 1:
6864         if (is_user) {
6865             *fsr = 1;
6866             return true;
6867         }
6868         *prot = PAGE_READ | PAGE_WRITE;
6869         break;
6870     case 2:
6871         *prot = PAGE_READ;
6872         if (!is_user) {
6873             *prot |= PAGE_WRITE;
6874         }
6875         break;
6876     case 3:
6877         *prot = PAGE_READ | PAGE_WRITE;
6878         break;
6879     case 5:
6880         if (is_user) {
6881             *fsr = 1;
6882             return true;
6883         }
6884         *prot = PAGE_READ;
6885         break;
6886     case 6:
6887         *prot = PAGE_READ;
6888         break;
6889     default:
6890         /* Bad permission.  */
6891         *fsr = 1;
6892         return true;
6893     }
6894     *prot |= PAGE_EXEC;
6895     return false;
6896 }
6897
6898 /* get_phys_addr - get the physical address for this virtual address
6899  *
6900  * Find the physical address corresponding to the given virtual address,
6901  * by doing a translation table walk on MMU based systems or using the
6902  * MPU state on MPU based systems.
6903  *
6904  * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
6905  * prot and page_size may not be filled in, and the populated fsr value provides
6906  * information on why the translation aborted, in the format of a
6907  * DFSR/IFSR fault register, with the following caveats:
6908  *  * we honour the short vs long DFSR format differences.
6909  *  * the WnR bit is never set (the caller must do this).
6910  *  * for PSMAv5 based systems we don't bother to return a full FSR format
6911  *    value.
6912  *
6913  * @env: CPUARMState
6914  * @address: virtual address to get physical address for
6915  * @access_type: 0 for read, 1 for write, 2 for execute
6916  * @mmu_idx: MMU index indicating required translation regime
6917  * @phys_ptr: set to the physical address corresponding to the virtual address
6918  * @attrs: set to the memory transaction attributes to use
6919  * @prot: set to the permissions for the page containing phys_ptr
6920  * @page_size: set to the size of the page containing phys_ptr
6921  * @fsr: set to the DFSR/IFSR value on failure
6922  */
6923 static inline bool get_phys_addr(CPUARMState *env, target_ulong address,
6924                                  int access_type, ARMMMUIdx mmu_idx,
6925                                  hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
6926                                  target_ulong *page_size, uint32_t *fsr)
6927 {
6928     if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6929         /* TODO: when we support EL2 we should here call ourselves recursively
6930          * to do the stage 1 and then stage 2 translations. The arm_ld*_ptw
6931          * functions will also need changing to perform ARMMMUIdx_S2NS loads
6932          * rather than direct physical memory loads when appropriate.
6933          * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
6934          */
6935         assert(!arm_feature(env, ARM_FEATURE_EL2));
6936         mmu_idx += ARMMMUIdx_S1NSE0;
6937     }
6938
6939     /* The page table entries may downgrade secure to non-secure, but
6940      * cannot upgrade an non-secure translation regime's attributes
6941      * to secure.
6942      */
6943     attrs->secure = regime_is_secure(env, mmu_idx);
6944     attrs->user = regime_is_user(env, mmu_idx);
6945
6946     /* Fast Context Switch Extension. This doesn't exist at all in v8.
6947      * In v7 and earlier it affects all stage 1 translations.
6948      */
6949     if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
6950         && !arm_feature(env, ARM_FEATURE_V8)) {
6951         if (regime_el(env, mmu_idx) == 3) {
6952             address += env->cp15.fcseidr_s;
6953         } else {
6954             address += env->cp15.fcseidr_ns;
6955         }
6956     }
6957
6958     /* pmsav7 has special handling for when MPU is disabled so call it before
6959      * the common MMU/MPU disabled check below.
6960      */
6961     if (arm_feature(env, ARM_FEATURE_MPU) &&
6962         arm_feature(env, ARM_FEATURE_V7)) {
6963         *page_size = TARGET_PAGE_SIZE;
6964         return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
6965                                     phys_ptr, prot, fsr);
6966     }
6967
6968     if (regime_translation_disabled(env, mmu_idx)) {
6969         /* MMU/MPU disabled.  */
6970         *phys_ptr = address;
6971         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
6972         *page_size = TARGET_PAGE_SIZE;
6973         return 0;
6974     }
6975
6976     if (arm_feature(env, ARM_FEATURE_MPU)) {
6977         /* Pre-v7 MPU */
6978         *page_size = TARGET_PAGE_SIZE;
6979         return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
6980                                     phys_ptr, prot, fsr);
6981     }
6982
6983     if (regime_using_lpae_format(env, mmu_idx)) {
6984         return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
6985                                   attrs, prot, page_size, fsr);
6986     } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
6987         return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
6988                                 attrs, prot, page_size, fsr);
6989     } else {
6990         return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
6991                                 prot, page_size, fsr);
6992     }
6993 }
6994
6995 /* Walk the page table and (if the mapping exists) add the page
6996  * to the TLB. Return false on success, or true on failure. Populate
6997  * fsr with ARM DFSR/IFSR fault register format value on failure.
6998  */
6999 bool arm_tlb_fill(CPUState *cs, vaddr address,
7000                   int access_type, int mmu_idx, uint32_t *fsr)
7001 {
7002     ARMCPU *cpu = ARM_CPU(cs);
7003     CPUARMState *env = &cpu->env;
7004     hwaddr phys_addr;
7005     target_ulong page_size;
7006     int prot;
7007     int ret;
7008     MemTxAttrs attrs = {};
7009
7010     ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
7011                         &attrs, &prot, &page_size, fsr);
7012     if (!ret) {
7013         /* Map a single [sub]page.  */
7014         phys_addr &= TARGET_PAGE_MASK;
7015         address &= TARGET_PAGE_MASK;
7016         tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
7017                                 prot, mmu_idx, page_size);
7018         return 0;
7019     }
7020
7021     return ret;
7022 }
7023
7024 hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
7025 {
7026     ARMCPU *cpu = ARM_CPU(cs);
7027     CPUARMState *env = &cpu->env;
7028     hwaddr phys_addr;
7029     target_ulong page_size;
7030     int prot;
7031     bool ret;
7032     uint32_t fsr;
7033     MemTxAttrs attrs = {};
7034
7035     ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
7036                         &attrs, &prot, &page_size, &fsr);
7037
7038     if (ret) {
7039         return -1;
7040     }
7041
7042     return phys_addr;
7043 }
7044
7045 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
7046 {
7047     if ((env->uncached_cpsr & CPSR_M) == mode) {
7048         env->regs[13] = val;
7049     } else {
7050         env->banked_r13[bank_number(mode)] = val;
7051     }
7052 }
7053
7054 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
7055 {
7056     if ((env->uncached_cpsr & CPSR_M) == mode) {
7057         return env->regs[13];
7058     } else {
7059         return env->banked_r13[bank_number(mode)];
7060     }
7061 }
7062
7063 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
7064 {
7065     ARMCPU *cpu = arm_env_get_cpu(env);
7066
7067     switch (reg) {
7068     case 0: /* APSR */
7069         return xpsr_read(env) & 0xf8000000;
7070     case 1: /* IAPSR */
7071         return xpsr_read(env) & 0xf80001ff;
7072     case 2: /* EAPSR */
7073         return xpsr_read(env) & 0xff00fc00;
7074     case 3: /* xPSR */
7075         return xpsr_read(env) & 0xff00fdff;
7076     case 5: /* IPSR */
7077         return xpsr_read(env) & 0x000001ff;
7078     case 6: /* EPSR */
7079         return xpsr_read(env) & 0x0700fc00;
7080     case 7: /* IEPSR */
7081         return xpsr_read(env) & 0x0700edff;
7082     case 8: /* MSP */
7083         return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
7084     case 9: /* PSP */
7085         return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
7086     case 16: /* PRIMASK */
7087         return (env->daif & PSTATE_I) != 0;
7088     case 17: /* BASEPRI */
7089     case 18: /* BASEPRI_MAX */
7090         return env->v7m.basepri;
7091     case 19: /* FAULTMASK */
7092         return (env->daif & PSTATE_F) != 0;
7093     case 20: /* CONTROL */
7094         return env->v7m.control;
7095     default:
7096         /* ??? For debugging only.  */
7097         cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
7098         return 0;
7099     }
7100 }
7101
7102 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
7103 {
7104     ARMCPU *cpu = arm_env_get_cpu(env);
7105
7106     switch (reg) {
7107     case 0: /* APSR */
7108         xpsr_write(env, val, 0xf8000000);
7109         break;
7110     case 1: /* IAPSR */
7111         xpsr_write(env, val, 0xf8000000);
7112         break;
7113     case 2: /* EAPSR */
7114         xpsr_write(env, val, 0xfe00fc00);
7115         break;
7116     case 3: /* xPSR */
7117         xpsr_write(env, val, 0xfe00fc00);
7118         break;
7119     case 5: /* IPSR */
7120         /* IPSR bits are readonly.  */
7121         break;
7122     case 6: /* EPSR */
7123         xpsr_write(env, val, 0x0600fc00);
7124         break;
7125     case 7: /* IEPSR */
7126         xpsr_write(env, val, 0x0600fc00);
7127         break;
7128     case 8: /* MSP */
7129         if (env->v7m.current_sp)
7130             env->v7m.other_sp = val;
7131         else
7132             env->regs[13] = val;
7133         break;
7134     case 9: /* PSP */
7135         if (env->v7m.current_sp)
7136             env->regs[13] = val;
7137         else
7138             env->v7m.other_sp = val;
7139         break;
7140     case 16: /* PRIMASK */
7141         if (val & 1) {
7142             env->daif |= PSTATE_I;
7143         } else {
7144             env->daif &= ~PSTATE_I;
7145         }
7146         break;
7147     case 17: /* BASEPRI */
7148         env->v7m.basepri = val & 0xff;
7149         break;
7150     case 18: /* BASEPRI_MAX */
7151         val &= 0xff;
7152         if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
7153             env->v7m.basepri = val;
7154         break;
7155     case 19: /* FAULTMASK */
7156         if (val & 1) {
7157             env->daif |= PSTATE_F;
7158         } else {
7159             env->daif &= ~PSTATE_F;
7160         }
7161         break;
7162     case 20: /* CONTROL */
7163         env->v7m.control = val & 3;
7164         switch_v7m_sp(env, (val & 2) != 0);
7165         break;
7166     default:
7167         /* ??? For debugging only.  */
7168         cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
7169         return;
7170     }
7171 }
7172
7173 #endif
7174
7175 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
7176 {
7177     /* Implement DC ZVA, which zeroes a fixed-length block of memory.
7178      * Note that we do not implement the (architecturally mandated)
7179      * alignment fault for attempts to use this on Device memory
7180      * (which matches the usual QEMU behaviour of not implementing either
7181      * alignment faults or any memory attribute handling).
7182      */
7183
7184     ARMCPU *cpu = arm_env_get_cpu(env);
7185     uint64_t blocklen = 4 << cpu->dcz_blocksize;
7186     uint64_t vaddr = vaddr_in & ~(blocklen - 1);
7187
7188 #ifndef CONFIG_USER_ONLY
7189     {
7190         /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
7191          * the block size so we might have to do more than one TLB lookup.
7192          * We know that in fact for any v8 CPU the page size is at least 4K
7193          * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
7194          * 1K as an artefact of legacy v5 subpage support being present in the
7195          * same QEMU executable.
7196          */
7197         int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
7198         void *hostaddr[maxidx];
7199         int try, i;
7200         unsigned mmu_idx = cpu_mmu_index(env, false);
7201         TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
7202
7203         for (try = 0; try < 2; try++) {
7204
7205             for (i = 0; i < maxidx; i++) {
7206                 hostaddr[i] = tlb_vaddr_to_host(env,
7207                                                 vaddr + TARGET_PAGE_SIZE * i,
7208                                                 1, mmu_idx);
7209                 if (!hostaddr[i]) {
7210                     break;
7211                 }
7212             }
7213             if (i == maxidx) {
7214                 /* If it's all in the TLB it's fair game for just writing to;
7215                  * we know we don't need to update dirty status, etc.
7216                  */
7217                 for (i = 0; i < maxidx - 1; i++) {
7218                     memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
7219                 }
7220                 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
7221                 return;
7222             }
7223             /* OK, try a store and see if we can populate the tlb. This
7224              * might cause an exception if the memory isn't writable,
7225              * in which case we will longjmp out of here. We must for
7226              * this purpose use the actual register value passed to us
7227              * so that we get the fault address right.
7228              */
7229             helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
7230             /* Now we can populate the other TLB entries, if any */
7231             for (i = 0; i < maxidx; i++) {
7232                 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
7233                 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
7234                     helper_ret_stb_mmu(env, va, 0, oi, GETRA());
7235                 }
7236             }
7237         }
7238
7239         /* Slow path (probably attempt to do this to an I/O device or
7240          * similar, or clearing of a block of code we have translations
7241          * cached for). Just do a series of byte writes as the architecture
7242          * demands. It's not worth trying to use a cpu_physical_memory_map(),
7243          * memset(), unmap() sequence here because:
7244          *  + we'd need to account for the blocksize being larger than a page
7245          *  + the direct-RAM access case is almost always going to be dealt
7246          *    with in the fastpath code above, so there's no speed benefit
7247          *  + we would have to deal with the map returning NULL because the
7248          *    bounce buffer was in use
7249          */
7250         for (i = 0; i < blocklen; i++) {
7251             helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
7252         }
7253     }
7254 #else
7255     memset(g2h(vaddr), 0, blocklen);
7256 #endif
7257 }
7258
7259 /* Note that signed overflow is undefined in C.  The following routines are
7260    careful to use unsigned types where modulo arithmetic is required.
7261    Failure to do so _will_ break on newer gcc.  */
7262
7263 /* Signed saturating arithmetic.  */
7264
7265 /* Perform 16-bit signed saturating addition.  */
7266 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
7267 {
7268     uint16_t res;
7269
7270     res = a + b;
7271     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
7272         if (a & 0x8000)
7273             res = 0x8000;
7274         else
7275             res = 0x7fff;
7276     }
7277     return res;
7278 }
7279
7280 /* Perform 8-bit signed saturating addition.  */
7281 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
7282 {
7283     uint8_t res;
7284
7285     res = a + b;
7286     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
7287         if (a & 0x80)
7288             res = 0x80;
7289         else
7290             res = 0x7f;
7291     }
7292     return res;
7293 }
7294
7295 /* Perform 16-bit signed saturating subtraction.  */
7296 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
7297 {
7298     uint16_t res;
7299
7300     res = a - b;
7301     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
7302         if (a & 0x8000)
7303             res = 0x8000;
7304         else
7305             res = 0x7fff;
7306     }
7307     return res;
7308 }
7309
7310 /* Perform 8-bit signed saturating subtraction.  */
7311 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
7312 {
7313     uint8_t res;
7314
7315     res = a - b;
7316     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
7317         if (a & 0x80)
7318             res = 0x80;
7319         else
7320             res = 0x7f;
7321     }
7322     return res;
7323 }
7324
7325 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
7326 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
7327 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
7328 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
7329 #define PFX q
7330
7331 #include "op_addsub.h"
7332
7333 /* Unsigned saturating arithmetic.  */
7334 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
7335 {
7336     uint16_t res;
7337     res = a + b;
7338     if (res < a)
7339         res = 0xffff;
7340     return res;
7341 }
7342
7343 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
7344 {
7345     if (a > b)
7346         return a - b;
7347     else
7348         return 0;
7349 }
7350
7351 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
7352 {
7353     uint8_t res;
7354     res = a + b;
7355     if (res < a)
7356         res = 0xff;
7357     return res;
7358 }
7359
7360 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
7361 {
7362     if (a > b)
7363         return a - b;
7364     else
7365         return 0;
7366 }
7367
7368 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
7369 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
7370 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
7371 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
7372 #define PFX uq
7373
7374 #include "op_addsub.h"
7375
7376 /* Signed modulo arithmetic.  */
7377 #define SARITH16(a, b, n, op) do { \
7378     int32_t sum; \
7379     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
7380     RESULT(sum, n, 16); \
7381     if (sum >= 0) \
7382         ge |= 3 << (n * 2); \
7383     } while(0)
7384
7385 #define SARITH8(a, b, n, op) do { \
7386     int32_t sum; \
7387     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
7388     RESULT(sum, n, 8); \
7389     if (sum >= 0) \
7390         ge |= 1 << n; \
7391     } while(0)
7392
7393
7394 #define ADD16(a, b, n) SARITH16(a, b, n, +)
7395 #define SUB16(a, b, n) SARITH16(a, b, n, -)
7396 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
7397 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
7398 #define PFX s
7399 #define ARITH_GE
7400
7401 #include "op_addsub.h"
7402
7403 /* Unsigned modulo arithmetic.  */
7404 #define ADD16(a, b, n) do { \
7405     uint32_t sum; \
7406     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
7407     RESULT(sum, n, 16); \
7408     if ((sum >> 16) == 1) \
7409         ge |= 3 << (n * 2); \
7410     } while(0)
7411
7412 #define ADD8(a, b, n) do { \
7413     uint32_t sum; \
7414     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
7415     RESULT(sum, n, 8); \
7416     if ((sum >> 8) == 1) \
7417         ge |= 1 << n; \
7418     } while(0)
7419
7420 #define SUB16(a, b, n) do { \
7421     uint32_t sum; \
7422     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
7423     RESULT(sum, n, 16); \
7424     if ((sum >> 16) == 0) \
7425         ge |= 3 << (n * 2); \
7426     } while(0)
7427
7428 #define SUB8(a, b, n) do { \
7429     uint32_t sum; \
7430     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
7431     RESULT(sum, n, 8); \
7432     if ((sum >> 8) == 0) \
7433         ge |= 1 << n; \
7434     } while(0)
7435
7436 #define PFX u
7437 #define ARITH_GE
7438
7439 #include "op_addsub.h"
7440
7441 /* Halved signed arithmetic.  */
7442 #define ADD16(a, b, n) \
7443   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
7444 #define SUB16(a, b, n) \
7445   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
7446 #define ADD8(a, b, n) \
7447   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
7448 #define SUB8(a, b, n) \
7449   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
7450 #define PFX sh
7451
7452 #include "op_addsub.h"
7453
7454 /* Halved unsigned arithmetic.  */
7455 #define ADD16(a, b, n) \
7456   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
7457 #define SUB16(a, b, n) \
7458   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
7459 #define ADD8(a, b, n) \
7460   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
7461 #define SUB8(a, b, n) \
7462   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
7463 #define PFX uh
7464
7465 #include "op_addsub.h"
7466
7467 static inline uint8_t do_usad(uint8_t a, uint8_t b)
7468 {
7469     if (a > b)
7470         return a - b;
7471     else
7472         return b - a;
7473 }
7474
7475 /* Unsigned sum of absolute byte differences.  */
7476 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
7477 {
7478     uint32_t sum;
7479     sum = do_usad(a, b);
7480     sum += do_usad(a >> 8, b >> 8);
7481     sum += do_usad(a >> 16, b >>16);
7482     sum += do_usad(a >> 24, b >> 24);
7483     return sum;
7484 }
7485
7486 /* For ARMv6 SEL instruction.  */
7487 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
7488 {
7489     uint32_t mask;
7490
7491     mask = 0;
7492     if (flags & 1)
7493         mask |= 0xff;
7494     if (flags & 2)
7495         mask |= 0xff00;
7496     if (flags & 4)
7497         mask |= 0xff0000;
7498     if (flags & 8)
7499         mask |= 0xff000000;
7500     return (a & mask) | (b & ~mask);
7501 }
7502
7503 /* VFP support.  We follow the convention used for VFP instructions:
7504    Single precision routines have a "s" suffix, double precision a
7505    "d" suffix.  */
7506
7507 /* Convert host exception flags to vfp form.  */
7508 static inline int vfp_exceptbits_from_host(int host_bits)
7509 {
7510     int target_bits = 0;
7511
7512     if (host_bits & float_flag_invalid)
7513         target_bits |= 1;
7514     if (host_bits & float_flag_divbyzero)
7515         target_bits |= 2;
7516     if (host_bits & float_flag_overflow)
7517         target_bits |= 4;
7518     if (host_bits & (float_flag_underflow | float_flag_output_denormal))
7519         target_bits |= 8;
7520     if (host_bits & float_flag_inexact)
7521         target_bits |= 0x10;
7522     if (host_bits & float_flag_input_denormal)
7523         target_bits |= 0x80;
7524     return target_bits;
7525 }
7526
7527 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
7528 {
7529     int i;
7530     uint32_t fpscr;
7531
7532     fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
7533             | (env->vfp.vec_len << 16)
7534             | (env->vfp.vec_stride << 20);
7535     i = get_float_exception_flags(&env->vfp.fp_status);
7536     i |= get_float_exception_flags(&env->vfp.standard_fp_status);
7537     fpscr |= vfp_exceptbits_from_host(i);
7538     return fpscr;
7539 }
7540
7541 uint32_t vfp_get_fpscr(CPUARMState *env)
7542 {
7543     return HELPER(vfp_get_fpscr)(env);
7544 }
7545
7546 /* Convert vfp exception flags to target form.  */
7547 static inline int vfp_exceptbits_to_host(int target_bits)
7548 {
7549     int host_bits = 0;
7550
7551     if (target_bits & 1)
7552         host_bits |= float_flag_invalid;
7553     if (target_bits & 2)
7554         host_bits |= float_flag_divbyzero;
7555     if (target_bits & 4)
7556         host_bits |= float_flag_overflow;
7557     if (target_bits & 8)
7558         host_bits |= float_flag_underflow;
7559     if (target_bits & 0x10)
7560         host_bits |= float_flag_inexact;
7561     if (target_bits & 0x80)
7562         host_bits |= float_flag_input_denormal;
7563     return host_bits;
7564 }
7565
7566 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
7567 {
7568     int i;
7569     uint32_t changed;
7570
7571     changed = env->vfp.xregs[ARM_VFP_FPSCR];
7572     env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
7573     env->vfp.vec_len = (val >> 16) & 7;
7574     env->vfp.vec_stride = (val >> 20) & 3;
7575
7576     changed ^= val;
7577     if (changed & (3 << 22)) {
7578         i = (val >> 22) & 3;
7579         switch (i) {
7580         case FPROUNDING_TIEEVEN:
7581             i = float_round_nearest_even;
7582             break;
7583         case FPROUNDING_POSINF:
7584             i = float_round_up;
7585             break;
7586         case FPROUNDING_NEGINF:
7587             i = float_round_down;
7588             break;
7589         case FPROUNDING_ZERO:
7590             i = float_round_to_zero;
7591             break;
7592         }
7593         set_float_rounding_mode(i, &env->vfp.fp_status);
7594     }
7595     if (changed & (1 << 24)) {
7596         set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
7597         set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
7598     }
7599     if (changed & (1 << 25))
7600         set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
7601
7602     i = vfp_exceptbits_to_host(val);
7603     set_float_exception_flags(i, &env->vfp.fp_status);
7604     set_float_exception_flags(0, &env->vfp.standard_fp_status);
7605 }
7606
7607 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
7608 {
7609     HELPER(vfp_set_fpscr)(env, val);
7610 }
7611
7612 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
7613
7614 #define VFP_BINOP(name) \
7615 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
7616 { \
7617     float_status *fpst = fpstp; \
7618     return float32_ ## name(a, b, fpst); \
7619 } \
7620 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
7621 { \
7622     float_status *fpst = fpstp; \
7623     return float64_ ## name(a, b, fpst); \
7624 }
7625 VFP_BINOP(add)
7626 VFP_BINOP(sub)
7627 VFP_BINOP(mul)
7628 VFP_BINOP(div)
7629 VFP_BINOP(min)
7630 VFP_BINOP(max)
7631 VFP_BINOP(minnum)
7632 VFP_BINOP(maxnum)
7633 #undef VFP_BINOP
7634
7635 float32 VFP_HELPER(neg, s)(float32 a)
7636 {
7637     return float32_chs(a);
7638 }
7639
7640 float64 VFP_HELPER(neg, d)(float64 a)
7641 {
7642     return float64_chs(a);
7643 }
7644
7645 float32 VFP_HELPER(abs, s)(float32 a)
7646 {
7647     return float32_abs(a);
7648 }
7649
7650 float64 VFP_HELPER(abs, d)(float64 a)
7651 {
7652     return float64_abs(a);
7653 }
7654
7655 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
7656 {
7657     return float32_sqrt(a, &env->vfp.fp_status);
7658 }
7659
7660 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
7661 {
7662     return float64_sqrt(a, &env->vfp.fp_status);
7663 }
7664
7665 /* XXX: check quiet/signaling case */
7666 #define DO_VFP_cmp(p, type) \
7667 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env)  \
7668 { \
7669     uint32_t flags; \
7670     switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
7671     case 0: flags = 0x6; break; \
7672     case -1: flags = 0x8; break; \
7673     case 1: flags = 0x2; break; \
7674     default: case 2: flags = 0x3; break; \
7675     } \
7676     env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
7677         | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
7678 } \
7679 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
7680 { \
7681     uint32_t flags; \
7682     switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
7683     case 0: flags = 0x6; break; \
7684     case -1: flags = 0x8; break; \
7685     case 1: flags = 0x2; break; \
7686     default: case 2: flags = 0x3; break; \
7687     } \
7688     env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
7689         | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
7690 }
7691 DO_VFP_cmp(s, float32)
7692 DO_VFP_cmp(d, float64)
7693 #undef DO_VFP_cmp
7694
7695 /* Integer to float and float to integer conversions */
7696
7697 #define CONV_ITOF(name, fsz, sign) \
7698     float##fsz HELPER(name)(uint32_t x, void *fpstp) \
7699 { \
7700     float_status *fpst = fpstp; \
7701     return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
7702 }
7703
7704 #define CONV_FTOI(name, fsz, sign, round) \
7705 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
7706 { \
7707     float_status *fpst = fpstp; \
7708     if (float##fsz##_is_any_nan(x)) { \
7709         float_raise(float_flag_invalid, fpst); \
7710         return 0; \
7711     } \
7712     return float##fsz##_to_##sign##int32##round(x, fpst); \
7713 }
7714
7715 #define FLOAT_CONVS(name, p, fsz, sign) \
7716 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
7717 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
7718 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
7719
7720 FLOAT_CONVS(si, s, 32, )
7721 FLOAT_CONVS(si, d, 64, )
7722 FLOAT_CONVS(ui, s, 32, u)
7723 FLOAT_CONVS(ui, d, 64, u)
7724
7725 #undef CONV_ITOF
7726 #undef CONV_FTOI
7727 #undef FLOAT_CONVS
7728
7729 /* floating point conversion */
7730 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
7731 {
7732     float64 r = float32_to_float64(x, &env->vfp.fp_status);
7733     /* ARM requires that S<->D conversion of any kind of NaN generates
7734      * a quiet NaN by forcing the most significant frac bit to 1.
7735      */
7736     return float64_maybe_silence_nan(r);
7737 }
7738
7739 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
7740 {
7741     float32 r =  float64_to_float32(x, &env->vfp.fp_status);
7742     /* ARM requires that S<->D conversion of any kind of NaN generates
7743      * a quiet NaN by forcing the most significant frac bit to 1.
7744      */
7745     return float32_maybe_silence_nan(r);
7746 }
7747
7748 /* VFP3 fixed point conversion.  */
7749 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
7750 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t  x, uint32_t shift, \
7751                                      void *fpstp) \
7752 { \
7753     float_status *fpst = fpstp; \
7754     float##fsz tmp; \
7755     tmp = itype##_to_##float##fsz(x, fpst); \
7756     return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
7757 }
7758
7759 /* Notice that we want only input-denormal exception flags from the
7760  * scalbn operation: the other possible flags (overflow+inexact if
7761  * we overflow to infinity, output-denormal) aren't correct for the
7762  * complete scale-and-convert operation.
7763  */
7764 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
7765 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
7766                                              uint32_t shift, \
7767                                              void *fpstp) \
7768 { \
7769     float_status *fpst = fpstp; \
7770     int old_exc_flags = get_float_exception_flags(fpst); \
7771     float##fsz tmp; \
7772     if (float##fsz##_is_any_nan(x)) { \
7773         float_raise(float_flag_invalid, fpst); \
7774         return 0; \
7775     } \
7776     tmp = float##fsz##_scalbn(x, shift, fpst); \
7777     old_exc_flags |= get_float_exception_flags(fpst) \
7778         & float_flag_input_denormal; \
7779     set_float_exception_flags(old_exc_flags, fpst); \
7780     return float##fsz##_to_##itype##round(tmp, fpst); \
7781 }
7782
7783 #define VFP_CONV_FIX(name, p, fsz, isz, itype)                   \
7784 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype)                     \
7785 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
7786 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
7787
7788 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype)               \
7789 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype)                     \
7790 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
7791
7792 VFP_CONV_FIX(sh, d, 64, 64, int16)
7793 VFP_CONV_FIX(sl, d, 64, 64, int32)
7794 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
7795 VFP_CONV_FIX(uh, d, 64, 64, uint16)
7796 VFP_CONV_FIX(ul, d, 64, 64, uint32)
7797 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
7798 VFP_CONV_FIX(sh, s, 32, 32, int16)
7799 VFP_CONV_FIX(sl, s, 32, 32, int32)
7800 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
7801 VFP_CONV_FIX(uh, s, 32, 32, uint16)
7802 VFP_CONV_FIX(ul, s, 32, 32, uint32)
7803 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
7804 #undef VFP_CONV_FIX
7805 #undef VFP_CONV_FIX_FLOAT
7806 #undef VFP_CONV_FLOAT_FIX_ROUND
7807
7808 /* Set the current fp rounding mode and return the old one.
7809  * The argument is a softfloat float_round_ value.
7810  */
7811 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
7812 {
7813     float_status *fp_status = &env->vfp.fp_status;
7814
7815     uint32_t prev_rmode = get_float_rounding_mode(fp_status);
7816     set_float_rounding_mode(rmode, fp_status);
7817
7818     return prev_rmode;
7819 }
7820
7821 /* Set the current fp rounding mode in the standard fp status and return
7822  * the old one. This is for NEON instructions that need to change the
7823  * rounding mode but wish to use the standard FPSCR values for everything
7824  * else. Always set the rounding mode back to the correct value after
7825  * modifying it.
7826  * The argument is a softfloat float_round_ value.
7827  */
7828 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
7829 {
7830     float_status *fp_status = &env->vfp.standard_fp_status;
7831
7832     uint32_t prev_rmode = get_float_rounding_mode(fp_status);
7833     set_float_rounding_mode(rmode, fp_status);
7834
7835     return prev_rmode;
7836 }
7837
7838 /* Half precision conversions.  */
7839 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
7840 {
7841     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7842     float32 r = float16_to_float32(make_float16(a), ieee, s);
7843     if (ieee) {
7844         return float32_maybe_silence_nan(r);
7845     }
7846     return r;
7847 }
7848
7849 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
7850 {
7851     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7852     float16 r = float32_to_float16(a, ieee, s);
7853     if (ieee) {
7854         r = float16_maybe_silence_nan(r);
7855     }
7856     return float16_val(r);
7857 }
7858
7859 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
7860 {
7861     return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
7862 }
7863
7864 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
7865 {
7866     return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
7867 }
7868
7869 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
7870 {
7871     return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
7872 }
7873
7874 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
7875 {
7876     return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
7877 }
7878
7879 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
7880 {
7881     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7882     float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
7883     if (ieee) {
7884         return float64_maybe_silence_nan(r);
7885     }
7886     return r;
7887 }
7888
7889 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
7890 {
7891     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7892     float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
7893     if (ieee) {
7894         r = float16_maybe_silence_nan(r);
7895     }
7896     return float16_val(r);
7897 }
7898
7899 #define float32_two make_float32(0x40000000)
7900 #define float32_three make_float32(0x40400000)
7901 #define float32_one_point_five make_float32(0x3fc00000)
7902
7903 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
7904 {
7905     float_status *s = &env->vfp.standard_fp_status;
7906     if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
7907         (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
7908         if (!(float32_is_zero(a) || float32_is_zero(b))) {
7909             float_raise(float_flag_input_denormal, s);
7910         }
7911         return float32_two;
7912     }
7913     return float32_sub(float32_two, float32_mul(a, b, s), s);
7914 }
7915
7916 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
7917 {
7918     float_status *s = &env->vfp.standard_fp_status;
7919     float32 product;
7920     if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
7921         (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
7922         if (!(float32_is_zero(a) || float32_is_zero(b))) {
7923             float_raise(float_flag_input_denormal, s);
7924         }
7925         return float32_one_point_five;
7926     }
7927     product = float32_mul(a, b, s);
7928     return float32_div(float32_sub(float32_three, product, s), float32_two, s);
7929 }
7930
7931 /* NEON helpers.  */
7932
7933 /* Constants 256 and 512 are used in some helpers; we avoid relying on
7934  * int->float conversions at run-time.  */
7935 #define float64_256 make_float64(0x4070000000000000LL)
7936 #define float64_512 make_float64(0x4080000000000000LL)
7937 #define float32_maxnorm make_float32(0x7f7fffff)
7938 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
7939
7940 /* Reciprocal functions
7941  *
7942  * The algorithm that must be used to calculate the estimate
7943  * is specified by the ARM ARM, see FPRecipEstimate()
7944  */
7945
7946 static float64 recip_estimate(float64 a, float_status *real_fp_status)
7947 {
7948     /* These calculations mustn't set any fp exception flags,
7949      * so we use a local copy of the fp_status.
7950      */
7951     float_status dummy_status = *real_fp_status;
7952     float_status *s = &dummy_status;
7953     /* q = (int)(a * 512.0) */
7954     float64 q = float64_mul(float64_512, a, s);
7955     int64_t q_int = float64_to_int64_round_to_zero(q, s);
7956
7957     /* r = 1.0 / (((double)q + 0.5) / 512.0) */
7958     q = int64_to_float64(q_int, s);
7959     q = float64_add(q, float64_half, s);
7960     q = float64_div(q, float64_512, s);
7961     q = float64_div(float64_one, q, s);
7962
7963     /* s = (int)(256.0 * r + 0.5) */
7964     q = float64_mul(q, float64_256, s);
7965     q = float64_add(q, float64_half, s);
7966     q_int = float64_to_int64_round_to_zero(q, s);
7967
7968     /* return (double)s / 256.0 */
7969     return float64_div(int64_to_float64(q_int, s), float64_256, s);
7970 }
7971
7972 /* Common wrapper to call recip_estimate */
7973 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
7974 {
7975     uint64_t val64 = float64_val(num);
7976     uint64_t frac = extract64(val64, 0, 52);
7977     int64_t exp = extract64(val64, 52, 11);
7978     uint64_t sbit;
7979     float64 scaled, estimate;
7980
7981     /* Generate the scaled number for the estimate function */
7982     if (exp == 0) {
7983         if (extract64(frac, 51, 1) == 0) {
7984             exp = -1;
7985             frac = extract64(frac, 0, 50) << 2;
7986         } else {
7987             frac = extract64(frac, 0, 51) << 1;
7988         }
7989     }
7990
7991     /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
7992     scaled = make_float64((0x3feULL << 52)
7993                           | extract64(frac, 44, 8) << 44);
7994
7995     estimate = recip_estimate(scaled, fpst);
7996
7997     /* Build new result */
7998     val64 = float64_val(estimate);
7999     sbit = 0x8000000000000000ULL & val64;
8000     exp = off - exp;
8001     frac = extract64(val64, 0, 52);
8002
8003     if (exp == 0) {
8004         frac = 1ULL << 51 | extract64(frac, 1, 51);
8005     } else if (exp == -1) {
8006         frac = 1ULL << 50 | extract64(frac, 2, 50);
8007         exp = 0;
8008     }
8009
8010     return make_float64(sbit | (exp << 52) | frac);
8011 }
8012
8013 static bool round_to_inf(float_status *fpst, bool sign_bit)
8014 {
8015     switch (fpst->float_rounding_mode) {
8016     case float_round_nearest_even: /* Round to Nearest */
8017         return true;
8018     case float_round_up: /* Round to +Inf */
8019         return !sign_bit;
8020     case float_round_down: /* Round to -Inf */
8021         return sign_bit;
8022     case float_round_to_zero: /* Round to Zero */
8023         return false;
8024     }
8025
8026     g_assert_not_reached();
8027 }
8028
8029 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
8030 {
8031     float_status *fpst = fpstp;
8032     float32 f32 = float32_squash_input_denormal(input, fpst);
8033     uint32_t f32_val = float32_val(f32);
8034     uint32_t f32_sbit = 0x80000000ULL & f32_val;
8035     int32_t f32_exp = extract32(f32_val, 23, 8);
8036     uint32_t f32_frac = extract32(f32_val, 0, 23);
8037     float64 f64, r64;
8038     uint64_t r64_val;
8039     int64_t r64_exp;
8040     uint64_t r64_frac;
8041
8042     if (float32_is_any_nan(f32)) {
8043         float32 nan = f32;
8044         if (float32_is_signaling_nan(f32)) {
8045             float_raise(float_flag_invalid, fpst);
8046             nan = float32_maybe_silence_nan(f32);
8047         }
8048         if (fpst->default_nan_mode) {
8049             nan =  float32_default_nan;
8050         }
8051         return nan;
8052     } else if (float32_is_infinity(f32)) {
8053         return float32_set_sign(float32_zero, float32_is_neg(f32));
8054     } else if (float32_is_zero(f32)) {
8055         float_raise(float_flag_divbyzero, fpst);
8056         return float32_set_sign(float32_infinity, float32_is_neg(f32));
8057     } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
8058         /* Abs(value) < 2.0^-128 */
8059         float_raise(float_flag_overflow | float_flag_inexact, fpst);
8060         if (round_to_inf(fpst, f32_sbit)) {
8061             return float32_set_sign(float32_infinity, float32_is_neg(f32));
8062         } else {
8063             return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
8064         }
8065     } else if (f32_exp >= 253 && fpst->flush_to_zero) {
8066         float_raise(float_flag_underflow, fpst);
8067         return float32_set_sign(float32_zero, float32_is_neg(f32));
8068     }
8069
8070
8071     f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
8072     r64 = call_recip_estimate(f64, 253, fpst);
8073     r64_val = float64_val(r64);
8074     r64_exp = extract64(r64_val, 52, 11);
8075     r64_frac = extract64(r64_val, 0, 52);
8076
8077     /* result = sign : result_exp<7:0> : fraction<51:29>; */
8078     return make_float32(f32_sbit |
8079                         (r64_exp & 0xff) << 23 |
8080                         extract64(r64_frac, 29, 24));
8081 }
8082
8083 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
8084 {
8085     float_status *fpst = fpstp;
8086     float64 f64 = float64_squash_input_denormal(input, fpst);
8087     uint64_t f64_val = float64_val(f64);
8088     uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
8089     int64_t f64_exp = extract64(f64_val, 52, 11);
8090     float64 r64;
8091     uint64_t r64_val;
8092     int64_t r64_exp;
8093     uint64_t r64_frac;
8094
8095     /* Deal with any special cases */
8096     if (float64_is_any_nan(f64)) {
8097         float64 nan = f64;
8098         if (float64_is_signaling_nan(f64)) {
8099             float_raise(float_flag_invalid, fpst);
8100             nan = float64_maybe_silence_nan(f64);
8101         }
8102         if (fpst->default_nan_mode) {
8103             nan =  float64_default_nan;
8104         }
8105         return nan;
8106     } else if (float64_is_infinity(f64)) {
8107         return float64_set_sign(float64_zero, float64_is_neg(f64));
8108     } else if (float64_is_zero(f64)) {
8109         float_raise(float_flag_divbyzero, fpst);
8110         return float64_set_sign(float64_infinity, float64_is_neg(f64));
8111     } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
8112         /* Abs(value) < 2.0^-1024 */
8113         float_raise(float_flag_overflow | float_flag_inexact, fpst);
8114         if (round_to_inf(fpst, f64_sbit)) {
8115             return float64_set_sign(float64_infinity, float64_is_neg(f64));
8116         } else {
8117             return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
8118         }
8119     } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
8120         float_raise(float_flag_underflow, fpst);
8121         return float64_set_sign(float64_zero, float64_is_neg(f64));
8122     }
8123
8124     r64 = call_recip_estimate(f64, 2045, fpst);
8125     r64_val = float64_val(r64);
8126     r64_exp = extract64(r64_val, 52, 11);
8127     r64_frac = extract64(r64_val, 0, 52);
8128
8129     /* result = sign : result_exp<10:0> : fraction<51:0> */
8130     return make_float64(f64_sbit |
8131                         ((r64_exp & 0x7ff) << 52) |
8132                         r64_frac);
8133 }
8134
8135 /* The algorithm that must be used to calculate the estimate
8136  * is specified by the ARM ARM.
8137  */
8138 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
8139 {
8140     /* These calculations mustn't set any fp exception flags,
8141      * so we use a local copy of the fp_status.
8142      */
8143     float_status dummy_status = *real_fp_status;
8144     float_status *s = &dummy_status;
8145     float64 q;
8146     int64_t q_int;
8147
8148     if (float64_lt(a, float64_half, s)) {
8149         /* range 0.25 <= a < 0.5 */
8150
8151         /* a in units of 1/512 rounded down */
8152         /* q0 = (int)(a * 512.0);  */
8153         q = float64_mul(float64_512, a, s);
8154         q_int = float64_to_int64_round_to_zero(q, s);
8155
8156         /* reciprocal root r */
8157         /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0);  */
8158         q = int64_to_float64(q_int, s);
8159         q = float64_add(q, float64_half, s);
8160         q = float64_div(q, float64_512, s);
8161         q = float64_sqrt(q, s);
8162         q = float64_div(float64_one, q, s);
8163     } else {
8164         /* range 0.5 <= a < 1.0 */
8165
8166         /* a in units of 1/256 rounded down */
8167         /* q1 = (int)(a * 256.0); */
8168         q = float64_mul(float64_256, a, s);
8169         int64_t q_int = float64_to_int64_round_to_zero(q, s);
8170
8171         /* reciprocal root r */
8172         /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
8173         q = int64_to_float64(q_int, s);
8174         q = float64_add(q, float64_half, s);
8175         q = float64_div(q, float64_256, s);
8176         q = float64_sqrt(q, s);
8177         q = float64_div(float64_one, q, s);
8178     }
8179     /* r in units of 1/256 rounded to nearest */
8180     /* s = (int)(256.0 * r + 0.5); */
8181
8182     q = float64_mul(q, float64_256,s );
8183     q = float64_add(q, float64_half, s);
8184     q_int = float64_to_int64_round_to_zero(q, s);
8185
8186     /* return (double)s / 256.0;*/
8187     return float64_div(int64_to_float64(q_int, s), float64_256, s);
8188 }
8189
8190 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
8191 {
8192     float_status *s = fpstp;
8193     float32 f32 = float32_squash_input_denormal(input, s);
8194     uint32_t val = float32_val(f32);
8195     uint32_t f32_sbit = 0x80000000 & val;
8196     int32_t f32_exp = extract32(val, 23, 8);
8197     uint32_t f32_frac = extract32(val, 0, 23);
8198     uint64_t f64_frac;
8199     uint64_t val64;
8200     int result_exp;
8201     float64 f64;
8202
8203     if (float32_is_any_nan(f32)) {
8204         float32 nan = f32;
8205         if (float32_is_signaling_nan(f32)) {
8206             float_raise(float_flag_invalid, s);
8207             nan = float32_maybe_silence_nan(f32);
8208         }
8209         if (s->default_nan_mode) {
8210             nan =  float32_default_nan;
8211         }
8212         return nan;
8213     } else if (float32_is_zero(f32)) {
8214         float_raise(float_flag_divbyzero, s);
8215         return float32_set_sign(float32_infinity, float32_is_neg(f32));
8216     } else if (float32_is_neg(f32)) {
8217         float_raise(float_flag_invalid, s);
8218         return float32_default_nan;
8219     } else if (float32_is_infinity(f32)) {
8220         return float32_zero;
8221     }
8222
8223     /* Scale and normalize to a double-precision value between 0.25 and 1.0,
8224      * preserving the parity of the exponent.  */
8225
8226     f64_frac = ((uint64_t) f32_frac) << 29;
8227     if (f32_exp == 0) {
8228         while (extract64(f64_frac, 51, 1) == 0) {
8229             f64_frac = f64_frac << 1;
8230             f32_exp = f32_exp-1;
8231         }
8232         f64_frac = extract64(f64_frac, 0, 51) << 1;
8233     }
8234
8235     if (extract64(f32_exp, 0, 1) == 0) {
8236         f64 = make_float64(((uint64_t) f32_sbit) << 32
8237                            | (0x3feULL << 52)
8238                            | f64_frac);
8239     } else {
8240         f64 = make_float64(((uint64_t) f32_sbit) << 32
8241                            | (0x3fdULL << 52)
8242                            | f64_frac);
8243     }
8244
8245     result_exp = (380 - f32_exp) / 2;
8246
8247     f64 = recip_sqrt_estimate(f64, s);
8248
8249     val64 = float64_val(f64);
8250
8251     val = ((result_exp & 0xff) << 23)
8252         | ((val64 >> 29)  & 0x7fffff);
8253     return make_float32(val);
8254 }
8255
8256 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
8257 {
8258     float_status *s = fpstp;
8259     float64 f64 = float64_squash_input_denormal(input, s);
8260     uint64_t val = float64_val(f64);
8261     uint64_t f64_sbit = 0x8000000000000000ULL & val;
8262     int64_t f64_exp = extract64(val, 52, 11);
8263     uint64_t f64_frac = extract64(val, 0, 52);
8264     int64_t result_exp;
8265     uint64_t result_frac;
8266
8267     if (float64_is_any_nan(f64)) {
8268         float64 nan = f64;
8269         if (float64_is_signaling_nan(f64)) {
8270             float_raise(float_flag_invalid, s);
8271             nan = float64_maybe_silence_nan(f64);
8272         }
8273         if (s->default_nan_mode) {
8274             nan =  float64_default_nan;
8275         }
8276         return nan;
8277     } else if (float64_is_zero(f64)) {
8278         float_raise(float_flag_divbyzero, s);
8279         return float64_set_sign(float64_infinity, float64_is_neg(f64));
8280     } else if (float64_is_neg(f64)) {
8281         float_raise(float_flag_invalid, s);
8282         return float64_default_nan;
8283     } else if (float64_is_infinity(f64)) {
8284         return float64_zero;
8285     }
8286
8287     /* Scale and normalize to a double-precision value between 0.25 and 1.0,
8288      * preserving the parity of the exponent.  */
8289
8290     if (f64_exp == 0) {
8291         while (extract64(f64_frac, 51, 1) == 0) {
8292             f64_frac = f64_frac << 1;
8293             f64_exp = f64_exp - 1;
8294         }
8295         f64_frac = extract64(f64_frac, 0, 51) << 1;
8296     }
8297
8298     if (extract64(f64_exp, 0, 1) == 0) {
8299         f64 = make_float64(f64_sbit
8300                            | (0x3feULL << 52)
8301                            | f64_frac);
8302     } else {
8303         f64 = make_float64(f64_sbit
8304                            | (0x3fdULL << 52)
8305                            | f64_frac);
8306     }
8307
8308     result_exp = (3068 - f64_exp) / 2;
8309
8310     f64 = recip_sqrt_estimate(f64, s);
8311
8312     result_frac = extract64(float64_val(f64), 0, 52);
8313
8314     return make_float64(f64_sbit |
8315                         ((result_exp & 0x7ff) << 52) |
8316                         result_frac);
8317 }
8318
8319 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
8320 {
8321     float_status *s = fpstp;
8322     float64 f64;
8323
8324     if ((a & 0x80000000) == 0) {
8325         return 0xffffffff;
8326     }
8327
8328     f64 = make_float64((0x3feULL << 52)
8329                        | ((int64_t)(a & 0x7fffffff) << 21));
8330
8331     f64 = recip_estimate(f64, s);
8332
8333     return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
8334 }
8335
8336 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
8337 {
8338     float_status *fpst = fpstp;
8339     float64 f64;
8340
8341     if ((a & 0xc0000000) == 0) {
8342         return 0xffffffff;
8343     }
8344
8345     if (a & 0x80000000) {
8346         f64 = make_float64((0x3feULL << 52)
8347                            | ((uint64_t)(a & 0x7fffffff) << 21));
8348     } else { /* bits 31-30 == '01' */
8349         f64 = make_float64((0x3fdULL << 52)
8350                            | ((uint64_t)(a & 0x3fffffff) << 22));
8351     }
8352
8353     f64 = recip_sqrt_estimate(f64, fpst);
8354
8355     return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
8356 }
8357
8358 /* VFPv4 fused multiply-accumulate */
8359 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
8360 {
8361     float_status *fpst = fpstp;
8362     return float32_muladd(a, b, c, 0, fpst);
8363 }
8364
8365 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
8366 {
8367     float_status *fpst = fpstp;
8368     return float64_muladd(a, b, c, 0, fpst);
8369 }
8370
8371 /* ARMv8 round to integral */
8372 float32 HELPER(rints_exact)(float32 x, void *fp_status)
8373 {
8374     return float32_round_to_int(x, fp_status);
8375 }
8376
8377 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
8378 {
8379     return float64_round_to_int(x, fp_status);
8380 }
8381
8382 float32 HELPER(rints)(float32 x, void *fp_status)
8383 {
8384     int old_flags = get_float_exception_flags(fp_status), new_flags;
8385     float32 ret;
8386
8387     ret = float32_round_to_int(x, fp_status);
8388
8389     /* Suppress any inexact exceptions the conversion produced */
8390     if (!(old_flags & float_flag_inexact)) {
8391         new_flags = get_float_exception_flags(fp_status);
8392         set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
8393     }
8394
8395     return ret;
8396 }
8397
8398 float64 HELPER(rintd)(float64 x, void *fp_status)
8399 {
8400     int old_flags = get_float_exception_flags(fp_status), new_flags;
8401     float64 ret;
8402
8403     ret = float64_round_to_int(x, fp_status);
8404
8405     new_flags = get_float_exception_flags(fp_status);
8406
8407     /* Suppress any inexact exceptions the conversion produced */
8408     if (!(old_flags & float_flag_inexact)) {
8409         new_flags = get_float_exception_flags(fp_status);
8410         set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
8411     }
8412
8413     return ret;
8414 }
8415
8416 /* Convert ARM rounding mode to softfloat */
8417 int arm_rmode_to_sf(int rmode)
8418 {
8419     switch (rmode) {
8420     case FPROUNDING_TIEAWAY:
8421         rmode = float_round_ties_away;
8422         break;
8423     case FPROUNDING_ODD:
8424         /* FIXME: add support for TIEAWAY and ODD */
8425         qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
8426                       rmode);
8427     case FPROUNDING_TIEEVEN:
8428     default:
8429         rmode = float_round_nearest_even;
8430         break;
8431     case FPROUNDING_POSINF:
8432         rmode = float_round_up;
8433         break;
8434     case FPROUNDING_NEGINF:
8435         rmode = float_round_down;
8436         break;
8437     case FPROUNDING_ZERO:
8438         rmode = float_round_to_zero;
8439         break;
8440     }
8441     return rmode;
8442 }
8443
8444 /* CRC helpers.
8445  * The upper bytes of val (above the number specified by 'bytes') must have
8446  * been zeroed out by the caller.
8447  */
8448 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
8449 {
8450     uint8_t buf[4];
8451
8452     stl_le_p(buf, val);
8453
8454     /* zlib crc32 converts the accumulator and output to one's complement.  */
8455     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
8456 }
8457
8458 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
8459 {
8460     uint8_t buf[4];
8461
8462     stl_le_p(buf, val);
8463
8464     /* Linux crc32c converts the output to one's complement.  */
8465     return crc32c(acc, buf, bytes) ^ 0xffffffff;
8466 }
This page took 0.491223 seconds and 4 git commands to generate.