]> Git Repo - qemu.git/blob - target-arm/helper.c
target-arm: Add VTTBR_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 mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2449 {
2450     ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2451     uint64_t mpidr = cpu->mp_affinity;
2452
2453     if (arm_feature(env, ARM_FEATURE_V7MP)) {
2454         mpidr |= (1U << 31);
2455         /* Cores which are uniprocessor (non-coherent)
2456          * but still implement the MP extensions set
2457          * bit 30. (For instance, Cortex-R5).
2458          */
2459         if (cpu->mp_is_up) {
2460             mpidr |= (1u << 30);
2461         }
2462     }
2463     return mpidr;
2464 }
2465
2466 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2467     { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2468       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2469       .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2470     REGINFO_SENTINEL
2471 };
2472
2473 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2474     /* NOP AMAIR0/1 */
2475     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2476       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2477       .access = PL1_RW, .type = ARM_CP_CONST,
2478       .resetvalue = 0 },
2479     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2480     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2481       .access = PL1_RW, .type = ARM_CP_CONST,
2482       .resetvalue = 0 },
2483     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2484       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2485       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2486                              offsetof(CPUARMState, cp15.par_ns)} },
2487     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2488       .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2489       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2490                              offsetof(CPUARMState, cp15.ttbr0_ns) },
2491       .writefn = vmsa_ttbr_write, },
2492     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2493       .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2494       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2495                              offsetof(CPUARMState, cp15.ttbr1_ns) },
2496       .writefn = vmsa_ttbr_write, },
2497     REGINFO_SENTINEL
2498 };
2499
2500 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2501 {
2502     return vfp_get_fpcr(env);
2503 }
2504
2505 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2506                             uint64_t value)
2507 {
2508     vfp_set_fpcr(env, value);
2509 }
2510
2511 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2512 {
2513     return vfp_get_fpsr(env);
2514 }
2515
2516 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2517                             uint64_t value)
2518 {
2519     vfp_set_fpsr(env, value);
2520 }
2521
2522 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
2523 {
2524     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2525         return CP_ACCESS_TRAP;
2526     }
2527     return CP_ACCESS_OK;
2528 }
2529
2530 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2531                             uint64_t value)
2532 {
2533     env->daif = value & PSTATE_DAIF;
2534 }
2535
2536 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2537                                           const ARMCPRegInfo *ri)
2538 {
2539     /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2540      * SCTLR_EL1.UCI is set.
2541      */
2542     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2543         return CP_ACCESS_TRAP;
2544     }
2545     return CP_ACCESS_OK;
2546 }
2547
2548 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2549  * Page D4-1736 (DDI0487A.b)
2550  */
2551
2552 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2553                                     uint64_t value)
2554 {
2555     ARMCPU *cpu = arm_env_get_cpu(env);
2556     CPUState *cs = CPU(cpu);
2557
2558     if (arm_is_secure_below_el3(env)) {
2559         tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2560     } else {
2561         tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2562     }
2563 }
2564
2565 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2566                                       uint64_t value)
2567 {
2568     bool sec = arm_is_secure_below_el3(env);
2569     CPUState *other_cs;
2570
2571     CPU_FOREACH(other_cs) {
2572         if (sec) {
2573             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2574         } else {
2575             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2576                                 ARMMMUIdx_S12NSE0, -1);
2577         }
2578     }
2579 }
2580
2581 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2582                                   uint64_t value)
2583 {
2584     /* Note that the 'ALL' scope must invalidate both stage 1 and
2585      * stage 2 translations, whereas most other scopes only invalidate
2586      * stage 1 translations.
2587      */
2588     ARMCPU *cpu = arm_env_get_cpu(env);
2589     CPUState *cs = CPU(cpu);
2590
2591     if (arm_is_secure_below_el3(env)) {
2592         tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2593     } else {
2594         if (arm_feature(env, ARM_FEATURE_EL2)) {
2595             tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2596                                 ARMMMUIdx_S2NS, -1);
2597         } else {
2598             tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2599         }
2600     }
2601 }
2602
2603 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2604                                   uint64_t value)
2605 {
2606     ARMCPU *cpu = arm_env_get_cpu(env);
2607     CPUState *cs = CPU(cpu);
2608
2609     tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2610 }
2611
2612 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2613                                   uint64_t value)
2614 {
2615     ARMCPU *cpu = arm_env_get_cpu(env);
2616     CPUState *cs = CPU(cpu);
2617
2618     tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2619 }
2620
2621 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2622                                     uint64_t value)
2623 {
2624     /* Note that the 'ALL' scope must invalidate both stage 1 and
2625      * stage 2 translations, whereas most other scopes only invalidate
2626      * stage 1 translations.
2627      */
2628     bool sec = arm_is_secure_below_el3(env);
2629     bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
2630     CPUState *other_cs;
2631
2632     CPU_FOREACH(other_cs) {
2633         if (sec) {
2634             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2635         } else if (has_el2) {
2636             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2637                                 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2638         } else {
2639             tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2640                                 ARMMMUIdx_S12NSE0, -1);
2641         }
2642     }
2643 }
2644
2645 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2646                                     uint64_t value)
2647 {
2648     CPUState *other_cs;
2649
2650     CPU_FOREACH(other_cs) {
2651         tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2652     }
2653 }
2654
2655 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2656                                     uint64_t value)
2657 {
2658     CPUState *other_cs;
2659
2660     CPU_FOREACH(other_cs) {
2661         tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2662     }
2663 }
2664
2665 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2666                                  uint64_t value)
2667 {
2668     /* Invalidate by VA, EL1&0 (AArch64 version).
2669      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2670      * since we don't support flush-for-specific-ASID-only or
2671      * flush-last-level-only.
2672      */
2673     ARMCPU *cpu = arm_env_get_cpu(env);
2674     CPUState *cs = CPU(cpu);
2675     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2676
2677     if (arm_is_secure_below_el3(env)) {
2678         tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2679                                  ARMMMUIdx_S1SE0, -1);
2680     } else {
2681         tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2682                                  ARMMMUIdx_S12NSE0, -1);
2683     }
2684 }
2685
2686 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2687                                  uint64_t value)
2688 {
2689     /* Invalidate by VA, EL2
2690      * Currently handles both VAE2 and VALE2, since we don't support
2691      * flush-last-level-only.
2692      */
2693     ARMCPU *cpu = arm_env_get_cpu(env);
2694     CPUState *cs = CPU(cpu);
2695     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2696
2697     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2698 }
2699
2700 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2701                                  uint64_t value)
2702 {
2703     /* Invalidate by VA, EL3
2704      * Currently handles both VAE3 and VALE3, since we don't support
2705      * flush-last-level-only.
2706      */
2707     ARMCPU *cpu = arm_env_get_cpu(env);
2708     CPUState *cs = CPU(cpu);
2709     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2710
2711     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2712 }
2713
2714 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2715                                    uint64_t value)
2716 {
2717     bool sec = arm_is_secure_below_el3(env);
2718     CPUState *other_cs;
2719     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2720
2721     CPU_FOREACH(other_cs) {
2722         if (sec) {
2723             tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2724                                      ARMMMUIdx_S1SE0, -1);
2725         } else {
2726             tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2727                                      ARMMMUIdx_S12NSE0, -1);
2728         }
2729     }
2730 }
2731
2732 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2733                                    uint64_t value)
2734 {
2735     CPUState *other_cs;
2736     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2737
2738     CPU_FOREACH(other_cs) {
2739         tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
2740     }
2741 }
2742
2743 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2744                                    uint64_t value)
2745 {
2746     CPUState *other_cs;
2747     uint64_t pageaddr = sextract64(value << 12, 0, 56);
2748
2749     CPU_FOREACH(other_cs) {
2750         tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2751     }
2752 }
2753
2754 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2755                                     uint64_t value)
2756 {
2757     /* Invalidate by IPA. This has to invalidate any structures that
2758      * contain only stage 2 translation information, but does not need
2759      * to apply to structures that contain combined stage 1 and stage 2
2760      * translation information.
2761      * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2762      */
2763     ARMCPU *cpu = arm_env_get_cpu(env);
2764     CPUState *cs = CPU(cpu);
2765     uint64_t pageaddr;
2766
2767     if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2768         return;
2769     }
2770
2771     pageaddr = sextract64(value << 12, 0, 48);
2772
2773     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2774 }
2775
2776 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2777                                       uint64_t value)
2778 {
2779     CPUState *other_cs;
2780     uint64_t pageaddr;
2781
2782     if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2783         return;
2784     }
2785
2786     pageaddr = sextract64(value << 12, 0, 48);
2787
2788     CPU_FOREACH(other_cs) {
2789         tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2790     }
2791 }
2792
2793 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
2794 {
2795     /* We don't implement EL2, so the only control on DC ZVA is the
2796      * bit in the SCTLR which can prohibit access for EL0.
2797      */
2798     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
2799         return CP_ACCESS_TRAP;
2800     }
2801     return CP_ACCESS_OK;
2802 }
2803
2804 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
2805 {
2806     ARMCPU *cpu = arm_env_get_cpu(env);
2807     int dzp_bit = 1 << 4;
2808
2809     /* DZP indicates whether DC ZVA access is allowed */
2810     if (aa64_zva_access(env, NULL) == CP_ACCESS_OK) {
2811         dzp_bit = 0;
2812     }
2813     return cpu->dcz_blocksize | dzp_bit;
2814 }
2815
2816 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2817 {
2818     if (!(env->pstate & PSTATE_SP)) {
2819         /* Access to SP_EL0 is undefined if it's being used as
2820          * the stack pointer.
2821          */
2822         return CP_ACCESS_TRAP_UNCATEGORIZED;
2823     }
2824     return CP_ACCESS_OK;
2825 }
2826
2827 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
2828 {
2829     return env->pstate & PSTATE_SP;
2830 }
2831
2832 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
2833 {
2834     update_spsel(env, val);
2835 }
2836
2837 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838                         uint64_t value)
2839 {
2840     ARMCPU *cpu = arm_env_get_cpu(env);
2841
2842     if (raw_read(env, ri) == value) {
2843         /* Skip the TLB flush if nothing actually changed; Linux likes
2844          * to do a lot of pointless SCTLR writes.
2845          */
2846         return;
2847     }
2848
2849     raw_write(env, ri, value);
2850     /* ??? Lots of these bits are not implemented.  */
2851     /* This may enable/disable the MMU, so do a TLB flush.  */
2852     tlb_flush(CPU(cpu), 1);
2853 }
2854
2855 static const ARMCPRegInfo v8_cp_reginfo[] = {
2856     /* Minimal set of EL0-visible registers. This will need to be expanded
2857      * significantly for system emulation of AArch64 CPUs.
2858      */
2859     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
2860       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
2861       .access = PL0_RW, .type = ARM_CP_NZCV },
2862     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
2863       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
2864       .type = ARM_CP_NO_RAW,
2865       .access = PL0_RW, .accessfn = aa64_daif_access,
2866       .fieldoffset = offsetof(CPUARMState, daif),
2867       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
2868     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
2869       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
2870       .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
2871     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
2872       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
2873       .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
2874     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
2875       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
2876       .access = PL0_R, .type = ARM_CP_NO_RAW,
2877       .readfn = aa64_dczid_read },
2878     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
2879       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
2880       .access = PL0_W, .type = ARM_CP_DC_ZVA,
2881 #ifndef CONFIG_USER_ONLY
2882       /* Avoid overhead of an access check that always passes in user-mode */
2883       .accessfn = aa64_zva_access,
2884 #endif
2885     },
2886     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
2887       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
2888       .access = PL1_R, .type = ARM_CP_CURRENTEL },
2889     /* Cache ops: all NOPs since we don't emulate caches */
2890     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
2891       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2892       .access = PL1_W, .type = ARM_CP_NOP },
2893     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
2894       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2895       .access = PL1_W, .type = ARM_CP_NOP },
2896     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
2897       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
2898       .access = PL0_W, .type = ARM_CP_NOP,
2899       .accessfn = aa64_cacheop_access },
2900     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
2901       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2902       .access = PL1_W, .type = ARM_CP_NOP },
2903     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
2904       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2905       .access = PL1_W, .type = ARM_CP_NOP },
2906     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
2907       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
2908       .access = PL0_W, .type = ARM_CP_NOP,
2909       .accessfn = aa64_cacheop_access },
2910     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
2911       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2912       .access = PL1_W, .type = ARM_CP_NOP },
2913     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
2914       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
2915       .access = PL0_W, .type = ARM_CP_NOP,
2916       .accessfn = aa64_cacheop_access },
2917     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
2918       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
2919       .access = PL0_W, .type = ARM_CP_NOP,
2920       .accessfn = aa64_cacheop_access },
2921     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
2922       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2923       .access = PL1_W, .type = ARM_CP_NOP },
2924     /* TLBI operations */
2925     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
2926       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2927       .access = PL1_W, .type = ARM_CP_NO_RAW,
2928       .writefn = tlbi_aa64_vmalle1is_write },
2929     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
2930       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2931       .access = PL1_W, .type = ARM_CP_NO_RAW,
2932       .writefn = tlbi_aa64_vae1is_write },
2933     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
2934       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2935       .access = PL1_W, .type = ARM_CP_NO_RAW,
2936       .writefn = tlbi_aa64_vmalle1is_write },
2937     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
2938       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2939       .access = PL1_W, .type = ARM_CP_NO_RAW,
2940       .writefn = tlbi_aa64_vae1is_write },
2941     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
2942       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2943       .access = PL1_W, .type = ARM_CP_NO_RAW,
2944       .writefn = tlbi_aa64_vae1is_write },
2945     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
2946       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2947       .access = PL1_W, .type = ARM_CP_NO_RAW,
2948       .writefn = tlbi_aa64_vae1is_write },
2949     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
2950       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2951       .access = PL1_W, .type = ARM_CP_NO_RAW,
2952       .writefn = tlbi_aa64_vmalle1_write },
2953     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
2954       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2955       .access = PL1_W, .type = ARM_CP_NO_RAW,
2956       .writefn = tlbi_aa64_vae1_write },
2957     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
2958       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2959       .access = PL1_W, .type = ARM_CP_NO_RAW,
2960       .writefn = tlbi_aa64_vmalle1_write },
2961     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
2962       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2963       .access = PL1_W, .type = ARM_CP_NO_RAW,
2964       .writefn = tlbi_aa64_vae1_write },
2965     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
2966       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2967       .access = PL1_W, .type = ARM_CP_NO_RAW,
2968       .writefn = tlbi_aa64_vae1_write },
2969     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
2970       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2971       .access = PL1_W, .type = ARM_CP_NO_RAW,
2972       .writefn = tlbi_aa64_vae1_write },
2973     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
2974       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
2975       .access = PL2_W, .type = ARM_CP_NO_RAW,
2976       .writefn = tlbi_aa64_ipas2e1is_write },
2977     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
2978       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
2979       .access = PL2_W, .type = ARM_CP_NO_RAW,
2980       .writefn = tlbi_aa64_ipas2e1is_write },
2981     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
2982       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
2983       .access = PL2_W, .type = ARM_CP_NO_RAW,
2984       .writefn = tlbi_aa64_alle1is_write },
2985     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
2986       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
2987       .access = PL2_W, .type = ARM_CP_NO_RAW,
2988       .writefn = tlbi_aa64_alle1is_write },
2989     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
2990       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
2991       .access = PL2_W, .type = ARM_CP_NO_RAW,
2992       .writefn = tlbi_aa64_ipas2e1_write },
2993     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
2994       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
2995       .access = PL2_W, .type = ARM_CP_NO_RAW,
2996       .writefn = tlbi_aa64_ipas2e1_write },
2997     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
2998       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
2999       .access = PL2_W, .type = ARM_CP_NO_RAW,
3000       .writefn = tlbi_aa64_alle1_write },
3001     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3002       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3003       .access = PL2_W, .type = ARM_CP_NO_RAW,
3004       .writefn = tlbi_aa64_alle1is_write },
3005 #ifndef CONFIG_USER_ONLY
3006     /* 64 bit address translation operations */
3007     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3008       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3009       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3010     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3011       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3012       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3013     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3014       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3015       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3016     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3017       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3018       .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3019     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3020       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3021       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3022     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3023       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3024       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3025     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3026       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3027       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3028     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3029       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3030       .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3031     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3032     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3033       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3034       .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3035     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3036       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3037       .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3038     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3039       .type = ARM_CP_ALIAS,
3040       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3041       .access = PL1_RW, .resetvalue = 0,
3042       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3043       .writefn = par_write },
3044 #endif
3045     /* TLB invalidate last level of translation table walk */
3046     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3047       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3048     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3049       .type = ARM_CP_NO_RAW, .access = PL1_W,
3050       .writefn = tlbimvaa_is_write },
3051     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3052       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3053     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3054       .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3055     /* 32 bit cache operations */
3056     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3057       .type = ARM_CP_NOP, .access = PL1_W },
3058     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3059       .type = ARM_CP_NOP, .access = PL1_W },
3060     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3061       .type = ARM_CP_NOP, .access = PL1_W },
3062     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3063       .type = ARM_CP_NOP, .access = PL1_W },
3064     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3065       .type = ARM_CP_NOP, .access = PL1_W },
3066     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3067       .type = ARM_CP_NOP, .access = PL1_W },
3068     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3069       .type = ARM_CP_NOP, .access = PL1_W },
3070     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3071       .type = ARM_CP_NOP, .access = PL1_W },
3072     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3073       .type = ARM_CP_NOP, .access = PL1_W },
3074     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3075       .type = ARM_CP_NOP, .access = PL1_W },
3076     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3077       .type = ARM_CP_NOP, .access = PL1_W },
3078     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3079       .type = ARM_CP_NOP, .access = PL1_W },
3080     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3081       .type = ARM_CP_NOP, .access = PL1_W },
3082     /* MMU Domain access control / MPU write buffer control */
3083     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3084       .access = PL1_RW, .resetvalue = 0,
3085       .writefn = dacr_write, .raw_writefn = raw_write,
3086       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3087                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3088     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3089       .type = ARM_CP_ALIAS,
3090       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3091       .access = PL1_RW,
3092       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3093     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3094       .type = ARM_CP_ALIAS,
3095       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3096       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[1]) },
3097     /* We rely on the access checks not allowing the guest to write to the
3098      * state field when SPSel indicates that it's being used as the stack
3099      * pointer.
3100      */
3101     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3102       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3103       .access = PL1_RW, .accessfn = sp_el0_access,
3104       .type = ARM_CP_ALIAS,
3105       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3106     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3107       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3108       .access = PL2_RW, .type = ARM_CP_ALIAS,
3109       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3110     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3111       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3112       .type = ARM_CP_NO_RAW,
3113       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3114     REGINFO_SENTINEL
3115 };
3116
3117 /* Used to describe the behaviour of EL2 regs when EL2 does not exist.  */
3118 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3119     { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3120       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3121       .access = PL2_RW,
3122       .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3123     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3124       .type = ARM_CP_NO_RAW,
3125       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3126       .access = PL2_RW,
3127       .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3128     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3129       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3130       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3131     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3132       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3133       .access = PL2_RW, .type = ARM_CP_CONST,
3134       .resetvalue = 0 },
3135     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3136       .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3137       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3138     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3139       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3140       .access = PL2_RW, .type = ARM_CP_CONST,
3141       .resetvalue = 0 },
3142     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3143       .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3144       .access = PL2_RW, .type = ARM_CP_CONST,
3145       .resetvalue = 0 },
3146     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3147       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3148       .access = PL2_RW, .type = ARM_CP_CONST,
3149       .resetvalue = 0 },
3150     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3151       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3152       .access = PL2_RW, .type = ARM_CP_CONST,
3153       .resetvalue = 0 },
3154     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3155       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3156       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3157     { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3158       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3159       .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3160       .type = ARM_CP_CONST, .resetvalue = 0 },
3161     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3162       .cp = 15, .opc1 = 6, .crm = 2,
3163       .access = PL2_RW, .accessfn = access_el3_aa32ns,
3164       .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3165     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3166       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3167       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3168     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3169       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3170       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3171     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3172       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3173       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3174     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3175       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3176       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3177     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3178       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3179       .resetvalue = 0 },
3180     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3181       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3182       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3183     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3184       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3185       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3186     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3187       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3188       .resetvalue = 0 },
3189     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3190       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3191       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3192     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3193       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3194       .resetvalue = 0 },
3195     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3196       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3197       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3198     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3199       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3200       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3201     REGINFO_SENTINEL
3202 };
3203
3204 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3205 {
3206     ARMCPU *cpu = arm_env_get_cpu(env);
3207     uint64_t valid_mask = HCR_MASK;
3208
3209     if (arm_feature(env, ARM_FEATURE_EL3)) {
3210         valid_mask &= ~HCR_HCD;
3211     } else {
3212         valid_mask &= ~HCR_TSC;
3213     }
3214
3215     /* Clear RES0 bits.  */
3216     value &= valid_mask;
3217
3218     /* These bits change the MMU setup:
3219      * HCR_VM enables stage 2 translation
3220      * HCR_PTW forbids certain page-table setups
3221      * HCR_DC Disables stage1 and enables stage2 translation
3222      */
3223     if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3224         tlb_flush(CPU(cpu), 1);
3225     }
3226     raw_write(env, ri, value);
3227 }
3228
3229 static const ARMCPRegInfo el2_cp_reginfo[] = {
3230     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3231       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3232       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3233       .writefn = hcr_write },
3234     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3235       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3236       .access = PL2_RW, .resetvalue = 0,
3237       .writefn = dacr_write, .raw_writefn = raw_write,
3238       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3239     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3240       .type = ARM_CP_ALIAS,
3241       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3242       .access = PL2_RW,
3243       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3244     { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3245       .type = ARM_CP_ALIAS,
3246       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3247       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3248     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3249       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3250       .access = PL2_RW, .resetvalue = 0,
3251       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3252     { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3253       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3254       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3255     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3256       .type = ARM_CP_ALIAS,
3257       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3258       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
3259     { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3260       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3261       .access = PL2_RW, .writefn = vbar_write,
3262       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3263       .resetvalue = 0 },
3264     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3265       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3266       .access = PL3_RW, .type = ARM_CP_ALIAS,
3267       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3268     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3269       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3270       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3271       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3272     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3273       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3274       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3275       .resetvalue = 0 },
3276     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3277       .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3278       .access = PL2_RW, .type = ARM_CP_ALIAS,
3279       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3280     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3281       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3282       .access = PL2_RW, .type = ARM_CP_CONST,
3283       .resetvalue = 0 },
3284     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3285     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3286       .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3287       .access = PL2_RW, .type = ARM_CP_CONST,
3288       .resetvalue = 0 },
3289     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3290       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3291       .access = PL2_RW, .type = ARM_CP_CONST,
3292       .resetvalue = 0 },
3293     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3294       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3295       .access = PL2_RW, .type = ARM_CP_CONST,
3296       .resetvalue = 0 },
3297     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3298       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3299       .access = PL2_RW, .writefn = vmsa_tcr_el1_write,
3300       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3301       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3302     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3303       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3304       .access = PL2_RW, .accessfn = access_el3_aa32ns,
3305       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3306     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3307       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3308       .access = PL2_RW, .type = ARM_CP_ALIAS,
3309       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3310     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3311       .cp = 15, .opc1 = 6, .crm = 2,
3312       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3313       .access = PL2_RW, .accessfn = access_el3_aa32ns,
3314       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3315       .writefn = vttbr_write },
3316     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3317       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3318       .access = PL2_RW, .writefn = vttbr_write,
3319       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3320     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3321       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3322       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3323       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3324     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3325       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3326       .access = PL2_RW, .resetvalue = 0,
3327       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3328     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3329       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3330       .access = PL2_RW, .resetvalue = 0,
3331       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3332     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3333       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3334       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3335     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3336       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3337       .type = ARM_CP_NO_RAW, .access = PL2_W,
3338       .writefn = tlbi_aa64_alle2_write },
3339     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3340       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3341       .type = ARM_CP_NO_RAW, .access = PL2_W,
3342       .writefn = tlbi_aa64_vae2_write },
3343     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3344       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3345       .access = PL2_W, .type = ARM_CP_NO_RAW,
3346       .writefn = tlbi_aa64_vae2_write },
3347     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3348       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3349       .access = PL2_W, .type = ARM_CP_NO_RAW,
3350       .writefn = tlbi_aa64_alle2is_write },
3351     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3352       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3353       .type = ARM_CP_NO_RAW, .access = PL2_W,
3354       .writefn = tlbi_aa64_vae2is_write },
3355     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3356       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3357       .access = PL2_W, .type = ARM_CP_NO_RAW,
3358       .writefn = tlbi_aa64_vae2is_write },
3359 #ifndef CONFIG_USER_ONLY
3360     /* Unlike the other EL2-related AT operations, these must
3361      * UNDEF from EL3 if EL2 is not implemented, which is why we
3362      * define them here rather than with the rest of the AT ops.
3363      */
3364     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3365       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3366       .access = PL2_W, .accessfn = at_s1e2_access,
3367       .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3368     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3369       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3370       .access = PL2_W, .accessfn = at_s1e2_access,
3371       .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3372     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3373      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3374      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3375      * to behave as if SCR.NS was 1.
3376      */
3377     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3378       .access = PL2_W,
3379       .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3380     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3381       .access = PL2_W,
3382       .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3383     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3384       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3385       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3386        * reset values as IMPDEF. We choose to reset to 3 to comply with
3387        * both ARMv7 and ARMv8.
3388        */
3389       .access = PL2_RW, .resetvalue = 3,
3390       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
3391     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3392       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3393       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3394       .writefn = gt_cntvoff_write,
3395       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3396     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3397       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3398       .writefn = gt_cntvoff_write,
3399       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3400     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3401       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3402       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3403       .type = ARM_CP_IO, .access = PL2_RW,
3404       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3405     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3406       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3407       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3408       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3409     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3410       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3411       .type = ARM_CP_IO, .access = PL2_RW,
3412       .resetfn = gt_hyp_timer_reset,
3413       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3414     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3415       .type = ARM_CP_IO,
3416       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3417       .access = PL2_RW,
3418       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3419       .resetvalue = 0,
3420       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
3421 #endif
3422     REGINFO_SENTINEL
3423 };
3424
3425 static const ARMCPRegInfo el3_cp_reginfo[] = {
3426     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3427       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3428       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3429       .resetvalue = 0, .writefn = scr_write },
3430     { .name = "SCR",  .type = ARM_CP_ALIAS,
3431       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
3432       .access = PL3_RW, .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
3433       .writefn = scr_write },
3434     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3435       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3436       .access = PL3_RW, .resetvalue = 0,
3437       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3438     { .name = "SDER",
3439       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3440       .access = PL3_RW, .resetvalue = 0,
3441       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
3442       /* TODO: Implement NSACR trapping of secure EL1 accesses to EL3 */
3443     { .name = "NSACR", .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
3444       .access = PL3_W | PL1_R, .resetvalue = 0,
3445       .fieldoffset = offsetof(CPUARMState, cp15.nsacr) },
3446     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
3447       .access = PL3_RW, .writefn = vbar_write, .resetvalue = 0,
3448       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
3449     { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
3450       .type = ARM_CP_ALIAS, /* reset handled by AArch32 view */
3451       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
3452       .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3453       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) },
3454     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3455       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3456       .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3457       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
3458     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3459       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
3460       .access = PL3_RW, .writefn = vmsa_tcr_el1_write,
3461       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3462       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
3463     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
3464       .type = ARM_CP_ALIAS,
3465       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3466       .access = PL3_RW,
3467       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
3468     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
3469       .type = ARM_CP_ALIAS,
3470       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3471       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
3472     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3473       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3474       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
3475     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
3476       .type = ARM_CP_ALIAS,
3477       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
3478       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
3479     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3480       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3481       .access = PL3_RW, .writefn = vbar_write,
3482       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3483       .resetvalue = 0 },
3484     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3485       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3486       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3487       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
3488     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3489       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3490       .access = PL3_RW, .resetvalue = 0,
3491       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
3492     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3493       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3494       .access = PL3_RW, .type = ARM_CP_CONST,
3495       .resetvalue = 0 },
3496     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3497       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3498       .access = PL3_RW, .type = ARM_CP_CONST,
3499       .resetvalue = 0 },
3500     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3501       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3502       .access = PL3_RW, .type = ARM_CP_CONST,
3503       .resetvalue = 0 },
3504     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3505       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3506       .access = PL3_W, .type = ARM_CP_NO_RAW,
3507       .writefn = tlbi_aa64_alle3is_write },
3508     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3509       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3510       .access = PL3_W, .type = ARM_CP_NO_RAW,
3511       .writefn = tlbi_aa64_vae3is_write },
3512     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3513       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3514       .access = PL3_W, .type = ARM_CP_NO_RAW,
3515       .writefn = tlbi_aa64_vae3is_write },
3516     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3517       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3518       .access = PL3_W, .type = ARM_CP_NO_RAW,
3519       .writefn = tlbi_aa64_alle3_write },
3520     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3521       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3522       .access = PL3_W, .type = ARM_CP_NO_RAW,
3523       .writefn = tlbi_aa64_vae3_write },
3524     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3525       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3526       .access = PL3_W, .type = ARM_CP_NO_RAW,
3527       .writefn = tlbi_aa64_vae3_write },
3528     REGINFO_SENTINEL
3529 };
3530
3531 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
3532 {
3533     /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3534      * but the AArch32 CTR has its own reginfo struct)
3535      */
3536     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
3537         return CP_ACCESS_TRAP;
3538     }
3539     return CP_ACCESS_OK;
3540 }
3541
3542 static const ARMCPRegInfo debug_cp_reginfo[] = {
3543     /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
3544      * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3545      * unlike DBGDRAR it is never accessible from EL0.
3546      * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3547      * accessor.
3548      */
3549     { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
3550       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3551     { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3552       .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
3553       .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3554     { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3555       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3556     /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
3557     { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3558       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
3559       .access = PL1_RW,
3560       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3561       .resetvalue = 0 },
3562     /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3563      * We don't implement the configurable EL0 access.
3564      */
3565     { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3566       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3567       .type = ARM_CP_ALIAS,
3568       .access = PL1_R,
3569       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
3570     /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
3571     { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3572       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
3573       .access = PL1_W, .type = ARM_CP_NOP },
3574     /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3575     { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3576       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
3577       .access = PL1_RW, .type = ARM_CP_NOP },
3578     /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3579      * implement vector catch debug events yet.
3580      */
3581     { .name = "DBGVCR",
3582       .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
3583       .access = PL1_RW, .type = ARM_CP_NOP },
3584     REGINFO_SENTINEL
3585 };
3586
3587 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3588     /* 64 bit access versions of the (dummy) debug registers */
3589     { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3590       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3591     { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3592       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3593     REGINFO_SENTINEL
3594 };
3595
3596 void hw_watchpoint_update(ARMCPU *cpu, int n)
3597 {
3598     CPUARMState *env = &cpu->env;
3599     vaddr len = 0;
3600     vaddr wvr = env->cp15.dbgwvr[n];
3601     uint64_t wcr = env->cp15.dbgwcr[n];
3602     int mask;
3603     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3604
3605     if (env->cpu_watchpoint[n]) {
3606         cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3607         env->cpu_watchpoint[n] = NULL;
3608     }
3609
3610     if (!extract64(wcr, 0, 1)) {
3611         /* E bit clear : watchpoint disabled */
3612         return;
3613     }
3614
3615     switch (extract64(wcr, 3, 2)) {
3616     case 0:
3617         /* LSC 00 is reserved and must behave as if the wp is disabled */
3618         return;
3619     case 1:
3620         flags |= BP_MEM_READ;
3621         break;
3622     case 2:
3623         flags |= BP_MEM_WRITE;
3624         break;
3625     case 3:
3626         flags |= BP_MEM_ACCESS;
3627         break;
3628     }
3629
3630     /* Attempts to use both MASK and BAS fields simultaneously are
3631      * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3632      * thus generating a watchpoint for every byte in the masked region.
3633      */
3634     mask = extract64(wcr, 24, 4);
3635     if (mask == 1 || mask == 2) {
3636         /* Reserved values of MASK; we must act as if the mask value was
3637          * some non-reserved value, or as if the watchpoint were disabled.
3638          * We choose the latter.
3639          */
3640         return;
3641     } else if (mask) {
3642         /* Watchpoint covers an aligned area up to 2GB in size */
3643         len = 1ULL << mask;
3644         /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3645          * whether the watchpoint fires when the unmasked bits match; we opt
3646          * to generate the exceptions.
3647          */
3648         wvr &= ~(len - 1);
3649     } else {
3650         /* Watchpoint covers bytes defined by the byte address select bits */
3651         int bas = extract64(wcr, 5, 8);
3652         int basstart;
3653
3654         if (bas == 0) {
3655             /* This must act as if the watchpoint is disabled */
3656             return;
3657         }
3658
3659         if (extract64(wvr, 2, 1)) {
3660             /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3661              * ignored, and BAS[3:0] define which bytes to watch.
3662              */
3663             bas &= 0xf;
3664         }
3665         /* The BAS bits are supposed to be programmed to indicate a contiguous
3666          * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3667          * we fire for each byte in the word/doubleword addressed by the WVR.
3668          * We choose to ignore any non-zero bits after the first range of 1s.
3669          */
3670         basstart = ctz32(bas);
3671         len = cto32(bas >> basstart);
3672         wvr += basstart;
3673     }
3674
3675     cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
3676                           &env->cpu_watchpoint[n]);
3677 }
3678
3679 void hw_watchpoint_update_all(ARMCPU *cpu)
3680 {
3681     int i;
3682     CPUARMState *env = &cpu->env;
3683
3684     /* Completely clear out existing QEMU watchpoints and our array, to
3685      * avoid possible stale entries following migration load.
3686      */
3687     cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
3688     memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
3689
3690     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
3691         hw_watchpoint_update(cpu, i);
3692     }
3693 }
3694
3695 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3696                          uint64_t value)
3697 {
3698     ARMCPU *cpu = arm_env_get_cpu(env);
3699     int i = ri->crm;
3700
3701     /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
3702      * register reads and behaves as if values written are sign extended.
3703      * Bits [1:0] are RES0.
3704      */
3705     value = sextract64(value, 0, 49) & ~3ULL;
3706
3707     raw_write(env, ri, value);
3708     hw_watchpoint_update(cpu, i);
3709 }
3710
3711 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3712                          uint64_t value)
3713 {
3714     ARMCPU *cpu = arm_env_get_cpu(env);
3715     int i = ri->crm;
3716
3717     raw_write(env, ri, value);
3718     hw_watchpoint_update(cpu, i);
3719 }
3720
3721 void hw_breakpoint_update(ARMCPU *cpu, int n)
3722 {
3723     CPUARMState *env = &cpu->env;
3724     uint64_t bvr = env->cp15.dbgbvr[n];
3725     uint64_t bcr = env->cp15.dbgbcr[n];
3726     vaddr addr;
3727     int bt;
3728     int flags = BP_CPU;
3729
3730     if (env->cpu_breakpoint[n]) {
3731         cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
3732         env->cpu_breakpoint[n] = NULL;
3733     }
3734
3735     if (!extract64(bcr, 0, 1)) {
3736         /* E bit clear : watchpoint disabled */
3737         return;
3738     }
3739
3740     bt = extract64(bcr, 20, 4);
3741
3742     switch (bt) {
3743     case 4: /* unlinked address mismatch (reserved if AArch64) */
3744     case 5: /* linked address mismatch (reserved if AArch64) */
3745         qemu_log_mask(LOG_UNIMP,
3746                       "arm: address mismatch breakpoint types not implemented");
3747         return;
3748     case 0: /* unlinked address match */
3749     case 1: /* linked address match */
3750     {
3751         /* Bits [63:49] are hardwired to the value of bit [48]; that is,
3752          * we behave as if the register was sign extended. Bits [1:0] are
3753          * RES0. The BAS field is used to allow setting breakpoints on 16
3754          * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
3755          * a bp will fire if the addresses covered by the bp and the addresses
3756          * covered by the insn overlap but the insn doesn't start at the
3757          * start of the bp address range. We choose to require the insn and
3758          * the bp to have the same address. The constraints on writing to
3759          * BAS enforced in dbgbcr_write mean we have only four cases:
3760          *  0b0000  => no breakpoint
3761          *  0b0011  => breakpoint on addr
3762          *  0b1100  => breakpoint on addr + 2
3763          *  0b1111  => breakpoint on addr
3764          * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
3765          */
3766         int bas = extract64(bcr, 5, 4);
3767         addr = sextract64(bvr, 0, 49) & ~3ULL;
3768         if (bas == 0) {
3769             return;
3770         }
3771         if (bas == 0xc) {
3772             addr += 2;
3773         }
3774         break;
3775     }
3776     case 2: /* unlinked context ID match */
3777     case 8: /* unlinked VMID match (reserved if no EL2) */
3778     case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
3779         qemu_log_mask(LOG_UNIMP,
3780                       "arm: unlinked context breakpoint types not implemented");
3781         return;
3782     case 9: /* linked VMID match (reserved if no EL2) */
3783     case 11: /* linked context ID and VMID match (reserved if no EL2) */
3784     case 3: /* linked context ID match */
3785     default:
3786         /* We must generate no events for Linked context matches (unless
3787          * they are linked to by some other bp/wp, which is handled in
3788          * updates for the linking bp/wp). We choose to also generate no events
3789          * for reserved values.
3790          */
3791         return;
3792     }
3793
3794     cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
3795 }
3796
3797 void hw_breakpoint_update_all(ARMCPU *cpu)
3798 {
3799     int i;
3800     CPUARMState *env = &cpu->env;
3801
3802     /* Completely clear out existing QEMU breakpoints and our array, to
3803      * avoid possible stale entries following migration load.
3804      */
3805     cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
3806     memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
3807
3808     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
3809         hw_breakpoint_update(cpu, i);
3810     }
3811 }
3812
3813 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3814                          uint64_t value)
3815 {
3816     ARMCPU *cpu = arm_env_get_cpu(env);
3817     int i = ri->crm;
3818
3819     raw_write(env, ri, value);
3820     hw_breakpoint_update(cpu, i);
3821 }
3822
3823 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3824                          uint64_t value)
3825 {
3826     ARMCPU *cpu = arm_env_get_cpu(env);
3827     int i = ri->crm;
3828
3829     /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
3830      * copy of BAS[0].
3831      */
3832     value = deposit64(value, 6, 1, extract64(value, 5, 1));
3833     value = deposit64(value, 8, 1, extract64(value, 7, 1));
3834
3835     raw_write(env, ri, value);
3836     hw_breakpoint_update(cpu, i);
3837 }
3838
3839 static void define_debug_regs(ARMCPU *cpu)
3840 {
3841     /* Define v7 and v8 architectural debug registers.
3842      * These are just dummy implementations for now.
3843      */
3844     int i;
3845     int wrps, brps, ctx_cmps;
3846     ARMCPRegInfo dbgdidr = {
3847         .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
3848         .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
3849     };
3850
3851     /* Note that all these register fields hold "number of Xs minus 1". */
3852     brps = extract32(cpu->dbgdidr, 24, 4);
3853     wrps = extract32(cpu->dbgdidr, 28, 4);
3854     ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
3855
3856     assert(ctx_cmps <= brps);
3857
3858     /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
3859      * of the debug registers such as number of breakpoints;
3860      * check that if they both exist then they agree.
3861      */
3862     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
3863         assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
3864         assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3865         assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
3866     }
3867
3868     define_one_arm_cp_reg(cpu, &dbgdidr);
3869     define_arm_cp_regs(cpu, debug_cp_reginfo);
3870
3871     if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
3872         define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
3873     }
3874
3875     for (i = 0; i < brps + 1; i++) {
3876         ARMCPRegInfo dbgregs[] = {
3877             { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
3878               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
3879               .access = PL1_RW,
3880               .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
3881               .writefn = dbgbvr_write, .raw_writefn = raw_write
3882             },
3883             { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
3884               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
3885               .access = PL1_RW,
3886               .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
3887               .writefn = dbgbcr_write, .raw_writefn = raw_write
3888             },
3889             REGINFO_SENTINEL
3890         };
3891         define_arm_cp_regs(cpu, dbgregs);
3892     }
3893
3894     for (i = 0; i < wrps + 1; i++) {
3895         ARMCPRegInfo dbgregs[] = {
3896             { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
3897               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
3898               .access = PL1_RW,
3899               .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
3900               .writefn = dbgwvr_write, .raw_writefn = raw_write
3901             },
3902             { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
3903               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
3904               .access = PL1_RW,
3905               .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
3906               .writefn = dbgwcr_write, .raw_writefn = raw_write
3907             },
3908             REGINFO_SENTINEL
3909         };
3910         define_arm_cp_regs(cpu, dbgregs);
3911     }
3912 }
3913
3914 void register_cp_regs_for_features(ARMCPU *cpu)
3915 {
3916     /* Register all the coprocessor registers based on feature bits */
3917     CPUARMState *env = &cpu->env;
3918     if (arm_feature(env, ARM_FEATURE_M)) {
3919         /* M profile has no coprocessor registers */
3920         return;
3921     }
3922
3923     define_arm_cp_regs(cpu, cp_reginfo);
3924     if (!arm_feature(env, ARM_FEATURE_V8)) {
3925         /* Must go early as it is full of wildcards that may be
3926          * overridden by later definitions.
3927          */
3928         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
3929     }
3930
3931     if (arm_feature(env, ARM_FEATURE_V6)) {
3932         /* The ID registers all have impdef reset values */
3933         ARMCPRegInfo v6_idregs[] = {
3934             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
3935               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3936               .access = PL1_R, .type = ARM_CP_CONST,
3937               .resetvalue = cpu->id_pfr0 },
3938             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
3939               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
3940               .access = PL1_R, .type = ARM_CP_CONST,
3941               .resetvalue = cpu->id_pfr1 },
3942             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
3943               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
3944               .access = PL1_R, .type = ARM_CP_CONST,
3945               .resetvalue = cpu->id_dfr0 },
3946             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
3947               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
3948               .access = PL1_R, .type = ARM_CP_CONST,
3949               .resetvalue = cpu->id_afr0 },
3950             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
3951               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
3952               .access = PL1_R, .type = ARM_CP_CONST,
3953               .resetvalue = cpu->id_mmfr0 },
3954             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
3955               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
3956               .access = PL1_R, .type = ARM_CP_CONST,
3957               .resetvalue = cpu->id_mmfr1 },
3958             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
3959               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
3960               .access = PL1_R, .type = ARM_CP_CONST,
3961               .resetvalue = cpu->id_mmfr2 },
3962             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
3963               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
3964               .access = PL1_R, .type = ARM_CP_CONST,
3965               .resetvalue = cpu->id_mmfr3 },
3966             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
3967               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
3968               .access = PL1_R, .type = ARM_CP_CONST,
3969               .resetvalue = cpu->id_isar0 },
3970             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
3971               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
3972               .access = PL1_R, .type = ARM_CP_CONST,
3973               .resetvalue = cpu->id_isar1 },
3974             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
3975               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
3976               .access = PL1_R, .type = ARM_CP_CONST,
3977               .resetvalue = cpu->id_isar2 },
3978             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
3979               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
3980               .access = PL1_R, .type = ARM_CP_CONST,
3981               .resetvalue = cpu->id_isar3 },
3982             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
3983               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
3984               .access = PL1_R, .type = ARM_CP_CONST,
3985               .resetvalue = cpu->id_isar4 },
3986             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
3987               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
3988               .access = PL1_R, .type = ARM_CP_CONST,
3989               .resetvalue = cpu->id_isar5 },
3990             /* 6..7 are as yet unallocated and must RAZ */
3991             { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
3992               .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
3993               .resetvalue = 0 },
3994             { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
3995               .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
3996               .resetvalue = 0 },
3997             REGINFO_SENTINEL
3998         };
3999         define_arm_cp_regs(cpu, v6_idregs);
4000         define_arm_cp_regs(cpu, v6_cp_reginfo);
4001     } else {
4002         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4003     }
4004     if (arm_feature(env, ARM_FEATURE_V6K)) {
4005         define_arm_cp_regs(cpu, v6k_cp_reginfo);
4006     }
4007     if (arm_feature(env, ARM_FEATURE_V7MP) &&
4008         !arm_feature(env, ARM_FEATURE_MPU)) {
4009         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4010     }
4011     if (arm_feature(env, ARM_FEATURE_V7)) {
4012         /* v7 performance monitor control register: same implementor
4013          * field as main ID register, and we implement only the cycle
4014          * count register.
4015          */
4016 #ifndef CONFIG_USER_ONLY
4017         ARMCPRegInfo pmcr = {
4018             .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4019             .access = PL0_RW,
4020             .type = ARM_CP_IO | ARM_CP_ALIAS,
4021             .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4022             .accessfn = pmreg_access, .writefn = pmcr_write,
4023             .raw_writefn = raw_write,
4024         };
4025         ARMCPRegInfo pmcr64 = {
4026             .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4027             .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4028             .access = PL0_RW, .accessfn = pmreg_access,
4029             .type = ARM_CP_IO,
4030             .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4031             .resetvalue = cpu->midr & 0xff000000,
4032             .writefn = pmcr_write, .raw_writefn = raw_write,
4033         };
4034         define_one_arm_cp_reg(cpu, &pmcr);
4035         define_one_arm_cp_reg(cpu, &pmcr64);
4036 #endif
4037         ARMCPRegInfo clidr = {
4038             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4039             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4040             .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4041         };
4042         define_one_arm_cp_reg(cpu, &clidr);
4043         define_arm_cp_regs(cpu, v7_cp_reginfo);
4044         define_debug_regs(cpu);
4045     } else {
4046         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4047     }
4048     if (arm_feature(env, ARM_FEATURE_V8)) {
4049         /* AArch64 ID registers, which all have impdef reset values */
4050         ARMCPRegInfo v8_idregs[] = {
4051             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4052               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4053               .access = PL1_R, .type = ARM_CP_CONST,
4054               .resetvalue = cpu->id_aa64pfr0 },
4055             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4056               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4057               .access = PL1_R, .type = ARM_CP_CONST,
4058               .resetvalue = cpu->id_aa64pfr1},
4059             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4060               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4061               .access = PL1_R, .type = ARM_CP_CONST,
4062               /* We mask out the PMUVer field, because we don't currently
4063                * implement the PMU. Not advertising it prevents the guest
4064                * from trying to use it and getting UNDEFs on registers we
4065                * don't implement.
4066                */
4067               .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
4068             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4069               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4070               .access = PL1_R, .type = ARM_CP_CONST,
4071               .resetvalue = cpu->id_aa64dfr1 },
4072             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4073               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4074               .access = PL1_R, .type = ARM_CP_CONST,
4075               .resetvalue = cpu->id_aa64afr0 },
4076             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4077               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4078               .access = PL1_R, .type = ARM_CP_CONST,
4079               .resetvalue = cpu->id_aa64afr1 },
4080             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4081               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4082               .access = PL1_R, .type = ARM_CP_CONST,
4083               .resetvalue = cpu->id_aa64isar0 },
4084             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4085               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4086               .access = PL1_R, .type = ARM_CP_CONST,
4087               .resetvalue = cpu->id_aa64isar1 },
4088             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4089               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4090               .access = PL1_R, .type = ARM_CP_CONST,
4091               .resetvalue = cpu->id_aa64mmfr0 },
4092             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4093               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4094               .access = PL1_R, .type = ARM_CP_CONST,
4095               .resetvalue = cpu->id_aa64mmfr1 },
4096             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4097               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4098               .access = PL1_R, .type = ARM_CP_CONST,
4099               .resetvalue = cpu->mvfr0 },
4100             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4101               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4102               .access = PL1_R, .type = ARM_CP_CONST,
4103               .resetvalue = cpu->mvfr1 },
4104             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4105               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4106               .access = PL1_R, .type = ARM_CP_CONST,
4107               .resetvalue = cpu->mvfr2 },
4108             REGINFO_SENTINEL
4109         };
4110         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4111         if (!arm_feature(env, ARM_FEATURE_EL3) &&
4112             !arm_feature(env, ARM_FEATURE_EL2)) {
4113             ARMCPRegInfo rvbar = {
4114                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4115                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4116                 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4117             };
4118             define_one_arm_cp_reg(cpu, &rvbar);
4119         }
4120         define_arm_cp_regs(cpu, v8_idregs);
4121         define_arm_cp_regs(cpu, v8_cp_reginfo);
4122     }
4123     if (arm_feature(env, ARM_FEATURE_EL2)) {
4124         define_arm_cp_regs(cpu, el2_cp_reginfo);
4125         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4126         if (!arm_feature(env, ARM_FEATURE_EL3)) {
4127             ARMCPRegInfo rvbar = {
4128                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4129                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4130                 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4131             };
4132             define_one_arm_cp_reg(cpu, &rvbar);
4133         }
4134     } else {
4135         /* If EL2 is missing but higher ELs are enabled, we need to
4136          * register the no_el2 reginfos.
4137          */
4138         if (arm_feature(env, ARM_FEATURE_EL3)) {
4139             define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
4140         }
4141     }
4142     if (arm_feature(env, ARM_FEATURE_EL3)) {
4143         define_arm_cp_regs(cpu, el3_cp_reginfo);
4144         ARMCPRegInfo rvbar = {
4145             .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4146             .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4147             .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar
4148         };
4149         define_one_arm_cp_reg(cpu, &rvbar);
4150     }
4151     if (arm_feature(env, ARM_FEATURE_MPU)) {
4152         if (arm_feature(env, ARM_FEATURE_V6)) {
4153             /* PMSAv6 not implemented */
4154             assert(arm_feature(env, ARM_FEATURE_V7));
4155             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4156             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4157         } else {
4158             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4159         }
4160     } else {
4161         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4162         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4163     }
4164     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4165         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4166     }
4167     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4168         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4169     }
4170     if (arm_feature(env, ARM_FEATURE_VAPA)) {
4171         define_arm_cp_regs(cpu, vapa_cp_reginfo);
4172     }
4173     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4174         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4175     }
4176     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4177         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4178     }
4179     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4180         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4181     }
4182     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4183         define_arm_cp_regs(cpu, omap_cp_reginfo);
4184     }
4185     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4186         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4187     }
4188     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4189         define_arm_cp_regs(cpu, xscale_cp_reginfo);
4190     }
4191     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4192         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4193     }
4194     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4195         define_arm_cp_regs(cpu, lpae_cp_reginfo);
4196     }
4197     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4198      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4199      * be read-only (ie write causes UNDEF exception).
4200      */
4201     {
4202         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4203             /* Pre-v8 MIDR space.
4204              * Note that the MIDR isn't a simple constant register because
4205              * of the TI925 behaviour where writes to another register can
4206              * cause the MIDR value to change.
4207              *
4208              * Unimplemented registers in the c15 0 0 0 space default to
4209              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4210              * and friends override accordingly.
4211              */
4212             { .name = "MIDR",
4213               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
4214               .access = PL1_R, .resetvalue = cpu->midr,
4215               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
4216               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4217               .type = ARM_CP_OVERRIDE },
4218             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4219             { .name = "DUMMY",
4220               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4221               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4222             { .name = "DUMMY",
4223               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4224               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4225             { .name = "DUMMY",
4226               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4227               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4228             { .name = "DUMMY",
4229               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4230               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4231             { .name = "DUMMY",
4232               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4233               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4234             REGINFO_SENTINEL
4235         };
4236         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
4237             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4238               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
4239               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
4240             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4241             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4242               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4243               .access = PL1_R, .resetvalue = cpu->midr },
4244             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4245               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4246               .access = PL1_R, .resetvalue = cpu->midr },
4247             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4248               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
4249               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
4250             REGINFO_SENTINEL
4251         };
4252         ARMCPRegInfo id_cp_reginfo[] = {
4253             /* These are common to v8 and pre-v8 */
4254             { .name = "CTR",
4255               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4256               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4257             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4258               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4259               .access = PL0_R, .accessfn = ctr_el0_access,
4260               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4261             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4262             { .name = "TCMTR",
4263               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4264               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4265             REGINFO_SENTINEL
4266         };
4267         /* TLBTR is specific to VMSA */
4268         ARMCPRegInfo id_tlbtr_reginfo = {
4269               .name = "TLBTR",
4270               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4271               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4272         };
4273         /* MPUIR is specific to PMSA V6+ */
4274         ARMCPRegInfo id_mpuir_reginfo = {
4275               .name = "MPUIR",
4276               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4277               .access = PL1_R, .type = ARM_CP_CONST,
4278               .resetvalue = cpu->pmsav7_dregion << 8
4279         };
4280         ARMCPRegInfo crn0_wi_reginfo = {
4281             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4282             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4283             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4284         };
4285         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4286             arm_feature(env, ARM_FEATURE_STRONGARM)) {
4287             ARMCPRegInfo *r;
4288             /* Register the blanket "writes ignored" value first to cover the
4289              * whole space. Then update the specific ID registers to allow write
4290              * access, so that they ignore writes rather than causing them to
4291              * UNDEF.
4292              */
4293             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
4294             for (r = id_pre_v8_midr_cp_reginfo;
4295                  r->type != ARM_CP_SENTINEL; r++) {
4296                 r->access = PL1_RW;
4297             }
4298             for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4299                 r->access = PL1_RW;
4300             }
4301             id_tlbtr_reginfo.access = PL1_RW;
4302             id_tlbtr_reginfo.access = PL1_RW;
4303         }
4304         if (arm_feature(env, ARM_FEATURE_V8)) {
4305             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4306         } else {
4307             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4308         }
4309         define_arm_cp_regs(cpu, id_cp_reginfo);
4310         if (!arm_feature(env, ARM_FEATURE_MPU)) {
4311             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
4312         } else if (arm_feature(env, ARM_FEATURE_V7)) {
4313             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
4314         }
4315     }
4316
4317     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4318         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4319     }
4320
4321     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
4322         ARMCPRegInfo auxcr_reginfo[] = {
4323             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4324               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4325               .access = PL1_RW, .type = ARM_CP_CONST,
4326               .resetvalue = cpu->reset_auxcr },
4327             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4328               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4329               .access = PL2_RW, .type = ARM_CP_CONST,
4330               .resetvalue = 0 },
4331             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4332               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4333               .access = PL3_RW, .type = ARM_CP_CONST,
4334               .resetvalue = 0 },
4335             REGINFO_SENTINEL
4336         };
4337         define_arm_cp_regs(cpu, auxcr_reginfo);
4338     }
4339
4340     if (arm_feature(env, ARM_FEATURE_CBAR)) {
4341         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4342             /* 32 bit view is [31:18] 0...0 [43:32]. */
4343             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4344                 | extract64(cpu->reset_cbar, 32, 12);
4345             ARMCPRegInfo cbar_reginfo[] = {
4346                 { .name = "CBAR",
4347                   .type = ARM_CP_CONST,
4348                   .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4349                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
4350                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4351                   .type = ARM_CP_CONST,
4352                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4353                   .access = PL1_R, .resetvalue = cbar32 },
4354                 REGINFO_SENTINEL
4355             };
4356             /* We don't implement a r/w 64 bit CBAR currently */
4357             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4358             define_arm_cp_regs(cpu, cbar_reginfo);
4359         } else {
4360             ARMCPRegInfo cbar = {
4361                 .name = "CBAR",
4362                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4363                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4364                 .fieldoffset = offsetof(CPUARMState,
4365                                         cp15.c15_config_base_address)
4366             };
4367             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4368                 cbar.access = PL1_R;
4369                 cbar.fieldoffset = 0;
4370                 cbar.type = ARM_CP_CONST;
4371             }
4372             define_one_arm_cp_reg(cpu, &cbar);
4373         }
4374     }
4375
4376     /* Generic registers whose values depend on the implementation */
4377     {
4378         ARMCPRegInfo sctlr = {
4379             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
4380             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4381             .access = PL1_RW,
4382             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4383                                    offsetof(CPUARMState, cp15.sctlr_ns) },
4384             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4385             .raw_writefn = raw_write,
4386         };
4387         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4388             /* Normally we would always end the TB on an SCTLR write, but Linux
4389              * arch/arm/mach-pxa/sleep.S expects two instructions following
4390              * an MMU enable to execute from cache.  Imitate this behaviour.
4391              */
4392             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4393         }
4394         define_one_arm_cp_reg(cpu, &sctlr);
4395     }
4396 }
4397
4398 ARMCPU *cpu_arm_init(const char *cpu_model)
4399 {
4400     return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
4401 }
4402
4403 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4404 {
4405     CPUState *cs = CPU(cpu);
4406     CPUARMState *env = &cpu->env;
4407
4408     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4409         gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4410                                  aarch64_fpu_gdb_set_reg,
4411                                  34, "aarch64-fpu.xml", 0);
4412     } else if (arm_feature(env, ARM_FEATURE_NEON)) {
4413         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4414                                  51, "arm-neon.xml", 0);
4415     } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
4416         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4417                                  35, "arm-vfp3.xml", 0);
4418     } else if (arm_feature(env, ARM_FEATURE_VFP)) {
4419         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4420                                  19, "arm-vfp.xml", 0);
4421     }
4422 }
4423
4424 /* Sort alphabetically by type name, except for "any". */
4425 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
4426 {
4427     ObjectClass *class_a = (ObjectClass *)a;
4428     ObjectClass *class_b = (ObjectClass *)b;
4429     const char *name_a, *name_b;
4430
4431     name_a = object_class_get_name(class_a);
4432     name_b = object_class_get_name(class_b);
4433     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
4434         return 1;
4435     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
4436         return -1;
4437     } else {
4438         return strcmp(name_a, name_b);
4439     }
4440 }
4441
4442 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
4443 {
4444     ObjectClass *oc = data;
4445     CPUListState *s = user_data;
4446     const char *typename;
4447     char *name;
4448
4449     typename = object_class_get_name(oc);
4450     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
4451     (*s->cpu_fprintf)(s->file, "  %s\n",
4452                       name);
4453     g_free(name);
4454 }
4455
4456 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
4457 {
4458     CPUListState s = {
4459         .file = f,
4460         .cpu_fprintf = cpu_fprintf,
4461     };
4462     GSList *list;
4463
4464     list = object_class_get_list(TYPE_ARM_CPU, false);
4465     list = g_slist_sort(list, arm_cpu_list_compare);
4466     (*cpu_fprintf)(f, "Available CPUs:\n");
4467     g_slist_foreach(list, arm_cpu_list_entry, &s);
4468     g_slist_free(list);
4469 #ifdef CONFIG_KVM
4470     /* The 'host' CPU type is dynamically registered only if KVM is
4471      * enabled, so we have to special-case it here:
4472      */
4473     (*cpu_fprintf)(f, "  host (only available in KVM mode)\n");
4474 #endif
4475 }
4476
4477 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
4478 {
4479     ObjectClass *oc = data;
4480     CpuDefinitionInfoList **cpu_list = user_data;
4481     CpuDefinitionInfoList *entry;
4482     CpuDefinitionInfo *info;
4483     const char *typename;
4484
4485     typename = object_class_get_name(oc);
4486     info = g_malloc0(sizeof(*info));
4487     info->name = g_strndup(typename,
4488                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
4489
4490     entry = g_malloc0(sizeof(*entry));
4491     entry->value = info;
4492     entry->next = *cpu_list;
4493     *cpu_list = entry;
4494 }
4495
4496 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
4497 {
4498     CpuDefinitionInfoList *cpu_list = NULL;
4499     GSList *list;
4500
4501     list = object_class_get_list(TYPE_ARM_CPU, false);
4502     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
4503     g_slist_free(list);
4504
4505     return cpu_list;
4506 }
4507
4508 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
4509                                    void *opaque, int state, int secstate,
4510                                    int crm, int opc1, int opc2)
4511 {
4512     /* Private utility function for define_one_arm_cp_reg_with_opaque():
4513      * add a single reginfo struct to the hash table.
4514      */
4515     uint32_t *key = g_new(uint32_t, 1);
4516     ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
4517     int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
4518     int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
4519
4520     /* Reset the secure state to the specific incoming state.  This is
4521      * necessary as the register may have been defined with both states.
4522      */
4523     r2->secure = secstate;
4524
4525     if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4526         /* Register is banked (using both entries in array).
4527          * Overwriting fieldoffset as the array is only used to define
4528          * banked registers but later only fieldoffset is used.
4529          */
4530         r2->fieldoffset = r->bank_fieldoffsets[ns];
4531     }
4532
4533     if (state == ARM_CP_STATE_AA32) {
4534         if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4535             /* If the register is banked then we don't need to migrate or
4536              * reset the 32-bit instance in certain cases:
4537              *
4538              * 1) If the register has both 32-bit and 64-bit instances then we
4539              *    can count on the 64-bit instance taking care of the
4540              *    non-secure bank.
4541              * 2) If ARMv8 is enabled then we can count on a 64-bit version
4542              *    taking care of the secure bank.  This requires that separate
4543              *    32 and 64-bit definitions are provided.
4544              */
4545             if ((r->state == ARM_CP_STATE_BOTH && ns) ||
4546                 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
4547                 r2->type |= ARM_CP_ALIAS;
4548             }
4549         } else if ((secstate != r->secure) && !ns) {
4550             /* The register is not banked so we only want to allow migration of
4551              * the non-secure instance.
4552              */
4553             r2->type |= ARM_CP_ALIAS;
4554         }
4555
4556         if (r->state == ARM_CP_STATE_BOTH) {
4557             /* We assume it is a cp15 register if the .cp field is left unset.
4558              */
4559             if (r2->cp == 0) {
4560                 r2->cp = 15;
4561             }
4562
4563 #ifdef HOST_WORDS_BIGENDIAN
4564             if (r2->fieldoffset) {
4565                 r2->fieldoffset += sizeof(uint32_t);
4566             }
4567 #endif
4568         }
4569     }
4570     if (state == ARM_CP_STATE_AA64) {
4571         /* To allow abbreviation of ARMCPRegInfo
4572          * definitions, we treat cp == 0 as equivalent to
4573          * the value for "standard guest-visible sysreg".
4574          * STATE_BOTH definitions are also always "standard
4575          * sysreg" in their AArch64 view (the .cp value may
4576          * be non-zero for the benefit of the AArch32 view).
4577          */
4578         if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
4579             r2->cp = CP_REG_ARM64_SYSREG_CP;
4580         }
4581         *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
4582                                   r2->opc0, opc1, opc2);
4583     } else {
4584         *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
4585     }
4586     if (opaque) {
4587         r2->opaque = opaque;
4588     }
4589     /* reginfo passed to helpers is correct for the actual access,
4590      * and is never ARM_CP_STATE_BOTH:
4591      */
4592     r2->state = state;
4593     /* Make sure reginfo passed to helpers for wildcarded regs
4594      * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
4595      */
4596     r2->crm = crm;
4597     r2->opc1 = opc1;
4598     r2->opc2 = opc2;
4599     /* By convention, for wildcarded registers only the first
4600      * entry is used for migration; the others are marked as
4601      * ALIAS so we don't try to transfer the register
4602      * multiple times. Special registers (ie NOP/WFI) are
4603      * never migratable and not even raw-accessible.
4604      */
4605     if ((r->type & ARM_CP_SPECIAL)) {
4606         r2->type |= ARM_CP_NO_RAW;
4607     }
4608     if (((r->crm == CP_ANY) && crm != 0) ||
4609         ((r->opc1 == CP_ANY) && opc1 != 0) ||
4610         ((r->opc2 == CP_ANY) && opc2 != 0)) {
4611         r2->type |= ARM_CP_ALIAS;
4612     }
4613
4614     /* Check that raw accesses are either forbidden or handled. Note that
4615      * we can't assert this earlier because the setup of fieldoffset for
4616      * banked registers has to be done first.
4617      */
4618     if (!(r2->type & ARM_CP_NO_RAW)) {
4619         assert(!raw_accessors_invalid(r2));
4620     }
4621
4622     /* Overriding of an existing definition must be explicitly
4623      * requested.
4624      */
4625     if (!(r->type & ARM_CP_OVERRIDE)) {
4626         ARMCPRegInfo *oldreg;
4627         oldreg = g_hash_table_lookup(cpu->cp_regs, key);
4628         if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
4629             fprintf(stderr, "Register redefined: cp=%d %d bit "
4630                     "crn=%d crm=%d opc1=%d opc2=%d, "
4631                     "was %s, now %s\n", r2->cp, 32 + 32 * is64,
4632                     r2->crn, r2->crm, r2->opc1, r2->opc2,
4633                     oldreg->name, r2->name);
4634             g_assert_not_reached();
4635         }
4636     }
4637     g_hash_table_insert(cpu->cp_regs, key, r2);
4638 }
4639
4640
4641 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
4642                                        const ARMCPRegInfo *r, void *opaque)
4643 {
4644     /* Define implementations of coprocessor registers.
4645      * We store these in a hashtable because typically
4646      * there are less than 150 registers in a space which
4647      * is 16*16*16*8*8 = 262144 in size.
4648      * Wildcarding is supported for the crm, opc1 and opc2 fields.
4649      * If a register is defined twice then the second definition is
4650      * used, so this can be used to define some generic registers and
4651      * then override them with implementation specific variations.
4652      * At least one of the original and the second definition should
4653      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
4654      * against accidental use.
4655      *
4656      * The state field defines whether the register is to be
4657      * visible in the AArch32 or AArch64 execution state. If the
4658      * state is set to ARM_CP_STATE_BOTH then we synthesise a
4659      * reginfo structure for the AArch32 view, which sees the lower
4660      * 32 bits of the 64 bit register.
4661      *
4662      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
4663      * be wildcarded. AArch64 registers are always considered to be 64
4664      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
4665      * the register, if any.
4666      */
4667     int crm, opc1, opc2, state;
4668     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
4669     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
4670     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
4671     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
4672     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
4673     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
4674     /* 64 bit registers have only CRm and Opc1 fields */
4675     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
4676     /* op0 only exists in the AArch64 encodings */
4677     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
4678     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
4679     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
4680     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
4681      * encodes a minimum access level for the register. We roll this
4682      * runtime check into our general permission check code, so check
4683      * here that the reginfo's specified permissions are strict enough
4684      * to encompass the generic architectural permission check.
4685      */
4686     if (r->state != ARM_CP_STATE_AA32) {
4687         int mask = 0;
4688         switch (r->opc1) {
4689         case 0: case 1: case 2:
4690             /* min_EL EL1 */
4691             mask = PL1_RW;
4692             break;
4693         case 3:
4694             /* min_EL EL0 */
4695             mask = PL0_RW;
4696             break;
4697         case 4:
4698             /* min_EL EL2 */
4699             mask = PL2_RW;
4700             break;
4701         case 5:
4702             /* unallocated encoding, so not possible */
4703             assert(false);
4704             break;
4705         case 6:
4706             /* min_EL EL3 */
4707             mask = PL3_RW;
4708             break;
4709         case 7:
4710             /* min_EL EL1, secure mode only (we don't check the latter) */
4711             mask = PL1_RW;
4712             break;
4713         default:
4714             /* broken reginfo with out-of-range opc1 */
4715             assert(false);
4716             break;
4717         }
4718         /* assert our permissions are not too lax (stricter is fine) */
4719         assert((r->access & ~mask) == 0);
4720     }
4721
4722     /* Check that the register definition has enough info to handle
4723      * reads and writes if they are permitted.
4724      */
4725     if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
4726         if (r->access & PL3_R) {
4727             assert((r->fieldoffset ||
4728                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
4729                    r->readfn);
4730         }
4731         if (r->access & PL3_W) {
4732             assert((r->fieldoffset ||
4733                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
4734                    r->writefn);
4735         }
4736     }
4737     /* Bad type field probably means missing sentinel at end of reg list */
4738     assert(cptype_valid(r->type));
4739     for (crm = crmmin; crm <= crmmax; crm++) {
4740         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
4741             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
4742                 for (state = ARM_CP_STATE_AA32;
4743                      state <= ARM_CP_STATE_AA64; state++) {
4744                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
4745                         continue;
4746                     }
4747                     if (state == ARM_CP_STATE_AA32) {
4748                         /* Under AArch32 CP registers can be common
4749                          * (same for secure and non-secure world) or banked.
4750                          */
4751                         switch (r->secure) {
4752                         case ARM_CP_SECSTATE_S:
4753                         case ARM_CP_SECSTATE_NS:
4754                             add_cpreg_to_hashtable(cpu, r, opaque, state,
4755                                                    r->secure, crm, opc1, opc2);
4756                             break;
4757                         default:
4758                             add_cpreg_to_hashtable(cpu, r, opaque, state,
4759                                                    ARM_CP_SECSTATE_S,
4760                                                    crm, opc1, opc2);
4761                             add_cpreg_to_hashtable(cpu, r, opaque, state,
4762                                                    ARM_CP_SECSTATE_NS,
4763                                                    crm, opc1, opc2);
4764                             break;
4765                         }
4766                     } else {
4767                         /* AArch64 registers get mapped to non-secure instance
4768                          * of AArch32 */
4769                         add_cpreg_to_hashtable(cpu, r, opaque, state,
4770                                                ARM_CP_SECSTATE_NS,
4771                                                crm, opc1, opc2);
4772                     }
4773                 }
4774             }
4775         }
4776     }
4777 }
4778
4779 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
4780                                     const ARMCPRegInfo *regs, void *opaque)
4781 {
4782     /* Define a whole list of registers */
4783     const ARMCPRegInfo *r;
4784     for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
4785         define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
4786     }
4787 }
4788
4789 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4790 {
4791     return g_hash_table_lookup(cpregs, &encoded_cp);
4792 }
4793
4794 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
4795                          uint64_t value)
4796 {
4797     /* Helper coprocessor write function for write-ignore registers */
4798 }
4799
4800 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4801 {
4802     /* Helper coprocessor write function for read-as-zero registers */
4803     return 0;
4804 }
4805
4806 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
4807 {
4808     /* Helper coprocessor reset function for do-nothing-on-reset registers */
4809 }
4810
4811 static int bad_mode_switch(CPUARMState *env, int mode)
4812 {
4813     /* Return true if it is not valid for us to switch to
4814      * this CPU mode (ie all the UNPREDICTABLE cases in
4815      * the ARM ARM CPSRWriteByInstr pseudocode).
4816      */
4817     switch (mode) {
4818     case ARM_CPU_MODE_USR:
4819     case ARM_CPU_MODE_SYS:
4820     case ARM_CPU_MODE_SVC:
4821     case ARM_CPU_MODE_ABT:
4822     case ARM_CPU_MODE_UND:
4823     case ARM_CPU_MODE_IRQ:
4824     case ARM_CPU_MODE_FIQ:
4825         return 0;
4826     case ARM_CPU_MODE_MON:
4827         return !arm_is_secure(env);
4828     default:
4829         return 1;
4830     }
4831 }
4832
4833 uint32_t cpsr_read(CPUARMState *env)
4834 {
4835     int ZF;
4836     ZF = (env->ZF == 0);
4837     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
4838         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
4839         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
4840         | ((env->condexec_bits & 0xfc) << 8)
4841         | (env->GE << 16) | (env->daif & CPSR_AIF);
4842 }
4843
4844 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
4845 {
4846     uint32_t changed_daif;
4847
4848     if (mask & CPSR_NZCV) {
4849         env->ZF = (~val) & CPSR_Z;
4850         env->NF = val;
4851         env->CF = (val >> 29) & 1;
4852         env->VF = (val << 3) & 0x80000000;
4853     }
4854     if (mask & CPSR_Q)
4855         env->QF = ((val & CPSR_Q) != 0);
4856     if (mask & CPSR_T)
4857         env->thumb = ((val & CPSR_T) != 0);
4858     if (mask & CPSR_IT_0_1) {
4859         env->condexec_bits &= ~3;
4860         env->condexec_bits |= (val >> 25) & 3;
4861     }
4862     if (mask & CPSR_IT_2_7) {
4863         env->condexec_bits &= 3;
4864         env->condexec_bits |= (val >> 8) & 0xfc;
4865     }
4866     if (mask & CPSR_GE) {
4867         env->GE = (val >> 16) & 0xf;
4868     }
4869
4870     /* In a V7 implementation that includes the security extensions but does
4871      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
4872      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
4873      * bits respectively.
4874      *
4875      * In a V8 implementation, it is permitted for privileged software to
4876      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
4877      */
4878     if (!arm_feature(env, ARM_FEATURE_V8) &&
4879         arm_feature(env, ARM_FEATURE_EL3) &&
4880         !arm_feature(env, ARM_FEATURE_EL2) &&
4881         !arm_is_secure(env)) {
4882
4883         changed_daif = (env->daif ^ val) & mask;
4884
4885         if (changed_daif & CPSR_A) {
4886             /* Check to see if we are allowed to change the masking of async
4887              * abort exceptions from a non-secure state.
4888              */
4889             if (!(env->cp15.scr_el3 & SCR_AW)) {
4890                 qemu_log_mask(LOG_GUEST_ERROR,
4891                               "Ignoring attempt to switch CPSR_A flag from "
4892                               "non-secure world with SCR.AW bit clear\n");
4893                 mask &= ~CPSR_A;
4894             }
4895         }
4896
4897         if (changed_daif & CPSR_F) {
4898             /* Check to see if we are allowed to change the masking of FIQ
4899              * exceptions from a non-secure state.
4900              */
4901             if (!(env->cp15.scr_el3 & SCR_FW)) {
4902                 qemu_log_mask(LOG_GUEST_ERROR,
4903                               "Ignoring attempt to switch CPSR_F flag from "
4904                               "non-secure world with SCR.FW bit clear\n");
4905                 mask &= ~CPSR_F;
4906             }
4907
4908             /* Check whether non-maskable FIQ (NMFI) support is enabled.
4909              * If this bit is set software is not allowed to mask
4910              * FIQs, but is allowed to set CPSR_F to 0.
4911              */
4912             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
4913                 (val & CPSR_F)) {
4914                 qemu_log_mask(LOG_GUEST_ERROR,
4915                               "Ignoring attempt to enable CPSR_F flag "
4916                               "(non-maskable FIQ [NMFI] support enabled)\n");
4917                 mask &= ~CPSR_F;
4918             }
4919         }
4920     }
4921
4922     env->daif &= ~(CPSR_AIF & mask);
4923     env->daif |= val & CPSR_AIF & mask;
4924
4925     if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
4926         if (bad_mode_switch(env, val & CPSR_M)) {
4927             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
4928              * We choose to ignore the attempt and leave the CPSR M field
4929              * untouched.
4930              */
4931             mask &= ~CPSR_M;
4932         } else {
4933             switch_mode(env, val & CPSR_M);
4934         }
4935     }
4936     mask &= ~CACHED_CPSR_BITS;
4937     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
4938 }
4939
4940 /* Sign/zero extend */
4941 uint32_t HELPER(sxtb16)(uint32_t x)
4942 {
4943     uint32_t res;
4944     res = (uint16_t)(int8_t)x;
4945     res |= (uint32_t)(int8_t)(x >> 16) << 16;
4946     return res;
4947 }
4948
4949 uint32_t HELPER(uxtb16)(uint32_t x)
4950 {
4951     uint32_t res;
4952     res = (uint16_t)(uint8_t)x;
4953     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
4954     return res;
4955 }
4956
4957 uint32_t HELPER(clz)(uint32_t x)
4958 {
4959     return clz32(x);
4960 }
4961
4962 int32_t HELPER(sdiv)(int32_t num, int32_t den)
4963 {
4964     if (den == 0)
4965       return 0;
4966     if (num == INT_MIN && den == -1)
4967       return INT_MIN;
4968     return num / den;
4969 }
4970
4971 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
4972 {
4973     if (den == 0)
4974       return 0;
4975     return num / den;
4976 }
4977
4978 uint32_t HELPER(rbit)(uint32_t x)
4979 {
4980     x =  ((x & 0xff000000) >> 24)
4981        | ((x & 0x00ff0000) >> 8)
4982        | ((x & 0x0000ff00) << 8)
4983        | ((x & 0x000000ff) << 24);
4984     x =  ((x & 0xf0f0f0f0) >> 4)
4985        | ((x & 0x0f0f0f0f) << 4);
4986     x =  ((x & 0x88888888) >> 3)
4987        | ((x & 0x44444444) >> 1)
4988        | ((x & 0x22222222) << 1)
4989        | ((x & 0x11111111) << 3);
4990     return x;
4991 }
4992
4993 #if defined(CONFIG_USER_ONLY)
4994
4995 /* These should probably raise undefined insn exceptions.  */
4996 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
4997 {
4998     ARMCPU *cpu = arm_env_get_cpu(env);
4999
5000     cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
5001 }
5002
5003 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
5004 {
5005     ARMCPU *cpu = arm_env_get_cpu(env);
5006
5007     cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
5008     return 0;
5009 }
5010
5011 void switch_mode(CPUARMState *env, int mode)
5012 {
5013     ARMCPU *cpu = arm_env_get_cpu(env);
5014
5015     if (mode != ARM_CPU_MODE_USR) {
5016         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5017     }
5018 }
5019
5020 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
5021 {
5022     ARMCPU *cpu = arm_env_get_cpu(env);
5023
5024     cpu_abort(CPU(cpu), "banked r13 write\n");
5025 }
5026
5027 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
5028 {
5029     ARMCPU *cpu = arm_env_get_cpu(env);
5030
5031     cpu_abort(CPU(cpu), "banked r13 read\n");
5032     return 0;
5033 }
5034
5035 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5036                                  uint32_t cur_el, bool secure)
5037 {
5038     return 1;
5039 }
5040
5041 void aarch64_sync_64_to_32(CPUARMState *env)
5042 {
5043     g_assert_not_reached();
5044 }
5045
5046 #else
5047
5048 /* Map CPU modes onto saved register banks.  */
5049 int bank_number(int mode)
5050 {
5051     switch (mode) {
5052     case ARM_CPU_MODE_USR:
5053     case ARM_CPU_MODE_SYS:
5054         return 0;
5055     case ARM_CPU_MODE_SVC:
5056         return 1;
5057     case ARM_CPU_MODE_ABT:
5058         return 2;
5059     case ARM_CPU_MODE_UND:
5060         return 3;
5061     case ARM_CPU_MODE_IRQ:
5062         return 4;
5063     case ARM_CPU_MODE_FIQ:
5064         return 5;
5065     case ARM_CPU_MODE_HYP:
5066         return 6;
5067     case ARM_CPU_MODE_MON:
5068         return 7;
5069     }
5070     g_assert_not_reached();
5071 }
5072
5073 void switch_mode(CPUARMState *env, int mode)
5074 {
5075     int old_mode;
5076     int i;
5077
5078     old_mode = env->uncached_cpsr & CPSR_M;
5079     if (mode == old_mode)
5080         return;
5081
5082     if (old_mode == ARM_CPU_MODE_FIQ) {
5083         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
5084         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
5085     } else if (mode == ARM_CPU_MODE_FIQ) {
5086         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
5087         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
5088     }
5089
5090     i = bank_number(old_mode);
5091     env->banked_r13[i] = env->regs[13];
5092     env->banked_r14[i] = env->regs[14];
5093     env->banked_spsr[i] = env->spsr;
5094
5095     i = bank_number(mode);
5096     env->regs[13] = env->banked_r13[i];
5097     env->regs[14] = env->banked_r14[i];
5098     env->spsr = env->banked_spsr[i];
5099 }
5100
5101 /* Physical Interrupt Target EL Lookup Table
5102  *
5103  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5104  *
5105  * The below multi-dimensional table is used for looking up the target
5106  * exception level given numerous condition criteria.  Specifically, the
5107  * target EL is based on SCR and HCR routing controls as well as the
5108  * currently executing EL and secure state.
5109  *
5110  *    Dimensions:
5111  *    target_el_table[2][2][2][2][2][4]
5112  *                    |  |  |  |  |  +--- Current EL
5113  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
5114  *                    |  |  |  +--------- HCR mask override
5115  *                    |  |  +------------ SCR exec state control
5116  *                    |  +--------------- SCR mask override
5117  *                    +------------------ 32-bit(0)/64-bit(1) EL3
5118  *
5119  *    The table values are as such:
5120  *    0-3 = EL0-EL3
5121  *     -1 = Cannot occur
5122  *
5123  * The ARM ARM target EL table includes entries indicating that an "exception
5124  * is not taken".  The two cases where this is applicable are:
5125  *    1) An exception is taken from EL3 but the SCR does not have the exception
5126  *    routed to EL3.
5127  *    2) An exception is taken from EL2 but the HCR does not have the exception
5128  *    routed to EL2.
5129  * In these two cases, the below table contain a target of EL1.  This value is
5130  * returned as it is expected that the consumer of the table data will check
5131  * for "target EL >= current EL" to ensure the exception is not taken.
5132  *
5133  *            SCR     HCR
5134  *         64  EA     AMO                 From
5135  *        BIT IRQ     IMO      Non-secure         Secure
5136  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
5137  */
5138 const int8_t target_el_table[2][2][2][2][2][4] = {
5139     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
5140        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
5141       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
5142        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
5143      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
5144        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
5145       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
5146        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
5147     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
5148        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 1,  1, -1,  1 },},},
5149       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1, -1,  1 },},
5150        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 1,  1, -1,  1 },},},},
5151      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
5152        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
5153       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
5154        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},},},
5155 };
5156
5157 /*
5158  * Determine the target EL for physical exceptions
5159  */
5160 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5161                                  uint32_t cur_el, bool secure)
5162 {
5163     CPUARMState *env = cs->env_ptr;
5164     int rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5165     int scr;
5166     int hcr;
5167     int target_el;
5168     int is64 = arm_el_is_aa64(env, 3);
5169
5170     switch (excp_idx) {
5171     case EXCP_IRQ:
5172         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5173         hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5174         break;
5175     case EXCP_FIQ:
5176         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5177         hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5178         break;
5179     default:
5180         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5181         hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5182         break;
5183     };
5184
5185     /* If HCR.TGE is set then HCR is treated as being 1 */
5186     hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5187
5188     /* Perform a table-lookup for the target EL given the current state */
5189     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5190
5191     assert(target_el > 0);
5192
5193     return target_el;
5194 }
5195
5196 static void v7m_push(CPUARMState *env, uint32_t val)
5197 {
5198     CPUState *cs = CPU(arm_env_get_cpu(env));
5199
5200     env->regs[13] -= 4;
5201     stl_phys(cs->as, env->regs[13], val);
5202 }
5203
5204 static uint32_t v7m_pop(CPUARMState *env)
5205 {
5206     CPUState *cs = CPU(arm_env_get_cpu(env));
5207     uint32_t val;
5208
5209     val = ldl_phys(cs->as, env->regs[13]);
5210     env->regs[13] += 4;
5211     return val;
5212 }
5213
5214 /* Switch to V7M main or process stack pointer.  */
5215 static void switch_v7m_sp(CPUARMState *env, int process)
5216 {
5217     uint32_t tmp;
5218     if (env->v7m.current_sp != process) {
5219         tmp = env->v7m.other_sp;
5220         env->v7m.other_sp = env->regs[13];
5221         env->regs[13] = tmp;
5222         env->v7m.current_sp = process;
5223     }
5224 }
5225
5226 static void do_v7m_exception_exit(CPUARMState *env)
5227 {
5228     uint32_t type;
5229     uint32_t xpsr;
5230
5231     type = env->regs[15];
5232     if (env->v7m.exception != 0)
5233         armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
5234
5235     /* Switch to the target stack.  */
5236     switch_v7m_sp(env, (type & 4) != 0);
5237     /* Pop registers.  */
5238     env->regs[0] = v7m_pop(env);
5239     env->regs[1] = v7m_pop(env);
5240     env->regs[2] = v7m_pop(env);
5241     env->regs[3] = v7m_pop(env);
5242     env->regs[12] = v7m_pop(env);
5243     env->regs[14] = v7m_pop(env);
5244     env->regs[15] = v7m_pop(env);
5245     if (env->regs[15] & 1) {
5246         qemu_log_mask(LOG_GUEST_ERROR,
5247                       "M profile return from interrupt with misaligned "
5248                       "PC is UNPREDICTABLE\n");
5249         /* Actual hardware seems to ignore the lsbit, and there are several
5250          * RTOSes out there which incorrectly assume the r15 in the stack
5251          * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5252          */
5253         env->regs[15] &= ~1U;
5254     }
5255     xpsr = v7m_pop(env);
5256     xpsr_write(env, xpsr, 0xfffffdff);
5257     /* Undo stack alignment.  */
5258     if (xpsr & 0x200)
5259         env->regs[13] |= 4;
5260     /* ??? The exception return type specifies Thread/Handler mode.  However
5261        this is also implied by the xPSR value. Not sure what to do
5262        if there is a mismatch.  */
5263     /* ??? Likewise for mismatches between the CONTROL register and the stack
5264        pointer.  */
5265 }
5266
5267 void arm_v7m_cpu_do_interrupt(CPUState *cs)
5268 {
5269     ARMCPU *cpu = ARM_CPU(cs);
5270     CPUARMState *env = &cpu->env;
5271     uint32_t xpsr = xpsr_read(env);
5272     uint32_t lr;
5273     uint32_t addr;
5274
5275     arm_log_exception(cs->exception_index);
5276
5277     lr = 0xfffffff1;
5278     if (env->v7m.current_sp)
5279         lr |= 4;
5280     if (env->v7m.exception == 0)
5281         lr |= 8;
5282
5283     /* For exceptions we just mark as pending on the NVIC, and let that
5284        handle it.  */
5285     /* TODO: Need to escalate if the current priority is higher than the
5286        one we're raising.  */
5287     switch (cs->exception_index) {
5288     case EXCP_UDEF:
5289         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
5290         return;
5291     case EXCP_SWI:
5292         /* The PC already points to the next instruction.  */
5293         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
5294         return;
5295     case EXCP_PREFETCH_ABORT:
5296     case EXCP_DATA_ABORT:
5297         /* TODO: if we implemented the MPU registers, this is where we
5298          * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5299          */
5300         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
5301         return;
5302     case EXCP_BKPT:
5303         if (semihosting_enabled()) {
5304             int nr;
5305             nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
5306             if (nr == 0xab) {
5307                 env->regs[15] += 2;
5308                 qemu_log_mask(CPU_LOG_INT,
5309                               "...handling as semihosting call 0x%x\n",
5310                               env->regs[0]);
5311                 env->regs[0] = do_arm_semihosting(env);
5312                 return;
5313             }
5314         }
5315         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
5316         return;
5317     case EXCP_IRQ:
5318         env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
5319         break;
5320     case EXCP_EXCEPTION_EXIT:
5321         do_v7m_exception_exit(env);
5322         return;
5323     default:
5324         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
5325         return; /* Never happens.  Keep compiler happy.  */
5326     }
5327
5328     /* Align stack pointer.  */
5329     /* ??? Should only do this if Configuration Control Register
5330        STACKALIGN bit is set.  */
5331     if (env->regs[13] & 4) {
5332         env->regs[13] -= 4;
5333         xpsr |= 0x200;
5334     }
5335     /* Switch to the handler mode.  */
5336     v7m_push(env, xpsr);
5337     v7m_push(env, env->regs[15]);
5338     v7m_push(env, env->regs[14]);
5339     v7m_push(env, env->regs[12]);
5340     v7m_push(env, env->regs[3]);
5341     v7m_push(env, env->regs[2]);
5342     v7m_push(env, env->regs[1]);
5343     v7m_push(env, env->regs[0]);
5344     switch_v7m_sp(env, 0);
5345     /* Clear IT bits */
5346     env->condexec_bits = 0;
5347     env->regs[14] = lr;
5348     addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
5349     env->regs[15] = addr & 0xfffffffe;
5350     env->thumb = addr & 1;
5351 }
5352
5353 /* Function used to synchronize QEMU's AArch64 register set with AArch32
5354  * register set.  This is necessary when switching between AArch32 and AArch64
5355  * execution state.
5356  */
5357 void aarch64_sync_32_to_64(CPUARMState *env)
5358 {
5359     int i;
5360     uint32_t mode = env->uncached_cpsr & CPSR_M;
5361
5362     /* We can blanket copy R[0:7] to X[0:7] */
5363     for (i = 0; i < 8; i++) {
5364         env->xregs[i] = env->regs[i];
5365     }
5366
5367     /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5368      * Otherwise, they come from the banked user regs.
5369      */
5370     if (mode == ARM_CPU_MODE_FIQ) {
5371         for (i = 8; i < 13; i++) {
5372             env->xregs[i] = env->usr_regs[i - 8];
5373         }
5374     } else {
5375         for (i = 8; i < 13; i++) {
5376             env->xregs[i] = env->regs[i];
5377         }
5378     }
5379
5380     /* Registers x13-x23 are the various mode SP and FP registers. Registers
5381      * r13 and r14 are only copied if we are in that mode, otherwise we copy
5382      * from the mode banked register.
5383      */
5384     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5385         env->xregs[13] = env->regs[13];
5386         env->xregs[14] = env->regs[14];
5387     } else {
5388         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5389         /* HYP is an exception in that it is copied from r14 */
5390         if (mode == ARM_CPU_MODE_HYP) {
5391             env->xregs[14] = env->regs[14];
5392         } else {
5393             env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5394         }
5395     }
5396
5397     if (mode == ARM_CPU_MODE_HYP) {
5398         env->xregs[15] = env->regs[13];
5399     } else {
5400         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5401     }
5402
5403     if (mode == ARM_CPU_MODE_IRQ) {
5404         env->xregs[16] = env->regs[14];
5405         env->xregs[17] = env->regs[13];
5406     } else {
5407         env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5408         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
5409     }
5410
5411     if (mode == ARM_CPU_MODE_SVC) {
5412         env->xregs[18] = env->regs[14];
5413         env->xregs[19] = env->regs[13];
5414     } else {
5415         env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5416         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
5417     }
5418
5419     if (mode == ARM_CPU_MODE_ABT) {
5420         env->xregs[20] = env->regs[14];
5421         env->xregs[21] = env->regs[13];
5422     } else {
5423         env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5424         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
5425     }
5426
5427     if (mode == ARM_CPU_MODE_UND) {
5428         env->xregs[22] = env->regs[14];
5429         env->xregs[23] = env->regs[13];
5430     } else {
5431         env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
5432         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
5433     }
5434
5435     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
5436      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
5437      * FIQ bank for r8-r14.
5438      */
5439     if (mode == ARM_CPU_MODE_FIQ) {
5440         for (i = 24; i < 31; i++) {
5441             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
5442         }
5443     } else {
5444         for (i = 24; i < 29; i++) {
5445             env->xregs[i] = env->fiq_regs[i - 24];
5446         }
5447         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
5448         env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
5449     }
5450
5451     env->pc = env->regs[15];
5452 }
5453
5454 /* Function used to synchronize QEMU's AArch32 register set with AArch64
5455  * register set.  This is necessary when switching between AArch32 and AArch64
5456  * execution state.
5457  */
5458 void aarch64_sync_64_to_32(CPUARMState *env)
5459 {
5460     int i;
5461     uint32_t mode = env->uncached_cpsr & CPSR_M;
5462
5463     /* We can blanket copy X[0:7] to R[0:7] */
5464     for (i = 0; i < 8; i++) {
5465         env->regs[i] = env->xregs[i];
5466     }
5467
5468     /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
5469      * Otherwise, we copy x8-x12 into the banked user regs.
5470      */
5471     if (mode == ARM_CPU_MODE_FIQ) {
5472         for (i = 8; i < 13; i++) {
5473             env->usr_regs[i - 8] = env->xregs[i];
5474         }
5475     } else {
5476         for (i = 8; i < 13; i++) {
5477             env->regs[i] = env->xregs[i];
5478         }
5479     }
5480
5481     /* Registers r13 & r14 depend on the current mode.
5482      * If we are in a given mode, we copy the corresponding x registers to r13
5483      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
5484      * for the mode.
5485      */
5486     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5487         env->regs[13] = env->xregs[13];
5488         env->regs[14] = env->xregs[14];
5489     } else {
5490         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
5491
5492         /* HYP is an exception in that it does not have its own banked r14 but
5493          * shares the USR r14
5494          */
5495         if (mode == ARM_CPU_MODE_HYP) {
5496             env->regs[14] = env->xregs[14];
5497         } else {
5498             env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
5499         }
5500     }
5501
5502     if (mode == ARM_CPU_MODE_HYP) {
5503         env->regs[13] = env->xregs[15];
5504     } else {
5505         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
5506     }
5507
5508     if (mode == ARM_CPU_MODE_IRQ) {
5509         env->regs[14] = env->xregs[16];
5510         env->regs[13] = env->xregs[17];
5511     } else {
5512         env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
5513         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
5514     }
5515
5516     if (mode == ARM_CPU_MODE_SVC) {
5517         env->regs[14] = env->xregs[18];
5518         env->regs[13] = env->xregs[19];
5519     } else {
5520         env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
5521         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
5522     }
5523
5524     if (mode == ARM_CPU_MODE_ABT) {
5525         env->regs[14] = env->xregs[20];
5526         env->regs[13] = env->xregs[21];
5527     } else {
5528         env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
5529         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
5530     }
5531
5532     if (mode == ARM_CPU_MODE_UND) {
5533         env->regs[14] = env->xregs[22];
5534         env->regs[13] = env->xregs[23];
5535     } else {
5536         env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
5537         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
5538     }
5539
5540     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
5541      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
5542      * FIQ bank for r8-r14.
5543      */
5544     if (mode == ARM_CPU_MODE_FIQ) {
5545         for (i = 24; i < 31; i++) {
5546             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
5547         }
5548     } else {
5549         for (i = 24; i < 29; i++) {
5550             env->fiq_regs[i - 24] = env->xregs[i];
5551         }
5552         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
5553         env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
5554     }
5555
5556     env->regs[15] = env->pc;
5557 }
5558
5559 /* Handle a CPU exception.  */
5560 void arm_cpu_do_interrupt(CPUState *cs)
5561 {
5562     ARMCPU *cpu = ARM_CPU(cs);
5563     CPUARMState *env = &cpu->env;
5564     uint32_t addr;
5565     uint32_t mask;
5566     int new_mode;
5567     uint32_t offset;
5568     uint32_t moe;
5569
5570     assert(!IS_M(env));
5571
5572     arm_log_exception(cs->exception_index);
5573
5574     if (arm_is_psci_call(cpu, cs->exception_index)) {
5575         arm_handle_psci_call(cpu);
5576         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
5577         return;
5578     }
5579
5580     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
5581     switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
5582     case EC_BREAKPOINT:
5583     case EC_BREAKPOINT_SAME_EL:
5584         moe = 1;
5585         break;
5586     case EC_WATCHPOINT:
5587     case EC_WATCHPOINT_SAME_EL:
5588         moe = 10;
5589         break;
5590     case EC_AA32_BKPT:
5591         moe = 3;
5592         break;
5593     case EC_VECTORCATCH:
5594         moe = 5;
5595         break;
5596     default:
5597         moe = 0;
5598         break;
5599     }
5600
5601     if (moe) {
5602         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
5603     }
5604
5605     /* TODO: Vectored interrupt controller.  */
5606     switch (cs->exception_index) {
5607     case EXCP_UDEF:
5608         new_mode = ARM_CPU_MODE_UND;
5609         addr = 0x04;
5610         mask = CPSR_I;
5611         if (env->thumb)
5612             offset = 2;
5613         else
5614             offset = 4;
5615         break;
5616     case EXCP_SWI:
5617         if (semihosting_enabled()) {
5618             /* Check for semihosting interrupt.  */
5619             if (env->thumb) {
5620                 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
5621                     & 0xff;
5622             } else {
5623                 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
5624                     & 0xffffff;
5625             }
5626             /* Only intercept calls from privileged modes, to provide some
5627                semblance of security.  */
5628             if (((mask == 0x123456 && !env->thumb)
5629                     || (mask == 0xab && env->thumb))
5630                   && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
5631                 qemu_log_mask(CPU_LOG_INT,
5632                               "...handling as semihosting call 0x%x\n",
5633                               env->regs[0]);
5634                 env->regs[0] = do_arm_semihosting(env);
5635                 return;
5636             }
5637         }
5638         new_mode = ARM_CPU_MODE_SVC;
5639         addr = 0x08;
5640         mask = CPSR_I;
5641         /* The PC already points to the next instruction.  */
5642         offset = 0;
5643         break;
5644     case EXCP_BKPT:
5645         /* See if this is a semihosting syscall.  */
5646         if (env->thumb && semihosting_enabled()) {
5647             mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
5648             if (mask == 0xab
5649                   && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
5650                 env->regs[15] += 2;
5651                 qemu_log_mask(CPU_LOG_INT,
5652                               "...handling as semihosting call 0x%x\n",
5653                               env->regs[0]);
5654                 env->regs[0] = do_arm_semihosting(env);
5655                 return;
5656             }
5657         }
5658         env->exception.fsr = 2;
5659         /* Fall through to prefetch abort.  */
5660     case EXCP_PREFETCH_ABORT:
5661         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
5662         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
5663         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
5664                       env->exception.fsr, (uint32_t)env->exception.vaddress);
5665         new_mode = ARM_CPU_MODE_ABT;
5666         addr = 0x0c;
5667         mask = CPSR_A | CPSR_I;
5668         offset = 4;
5669         break;
5670     case EXCP_DATA_ABORT:
5671         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
5672         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
5673         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
5674                       env->exception.fsr,
5675                       (uint32_t)env->exception.vaddress);
5676         new_mode = ARM_CPU_MODE_ABT;
5677         addr = 0x10;
5678         mask = CPSR_A | CPSR_I;
5679         offset = 8;
5680         break;
5681     case EXCP_IRQ:
5682         new_mode = ARM_CPU_MODE_IRQ;
5683         addr = 0x18;
5684         /* Disable IRQ and imprecise data aborts.  */
5685         mask = CPSR_A | CPSR_I;
5686         offset = 4;
5687         if (env->cp15.scr_el3 & SCR_IRQ) {
5688             /* IRQ routed to monitor mode */
5689             new_mode = ARM_CPU_MODE_MON;
5690             mask |= CPSR_F;
5691         }
5692         break;
5693     case EXCP_FIQ:
5694         new_mode = ARM_CPU_MODE_FIQ;
5695         addr = 0x1c;
5696         /* Disable FIQ, IRQ and imprecise data aborts.  */
5697         mask = CPSR_A | CPSR_I | CPSR_F;
5698         if (env->cp15.scr_el3 & SCR_FIQ) {
5699             /* FIQ routed to monitor mode */
5700             new_mode = ARM_CPU_MODE_MON;
5701         }
5702         offset = 4;
5703         break;
5704     case EXCP_SMC:
5705         new_mode = ARM_CPU_MODE_MON;
5706         addr = 0x08;
5707         mask = CPSR_A | CPSR_I | CPSR_F;
5708         offset = 0;
5709         break;
5710     default:
5711         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
5712         return; /* Never happens.  Keep compiler happy.  */
5713     }
5714
5715     if (new_mode == ARM_CPU_MODE_MON) {
5716         addr += env->cp15.mvbar;
5717     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
5718         /* High vectors. When enabled, base address cannot be remapped. */
5719         addr += 0xffff0000;
5720     } else {
5721         /* ARM v7 architectures provide a vector base address register to remap
5722          * the interrupt vector table.
5723          * This register is only followed in non-monitor mode, and is banked.
5724          * Note: only bits 31:5 are valid.
5725          */
5726         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
5727     }
5728
5729     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
5730         env->cp15.scr_el3 &= ~SCR_NS;
5731     }
5732
5733     switch_mode (env, new_mode);
5734     /* For exceptions taken to AArch32 we must clear the SS bit in both
5735      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
5736      */
5737     env->uncached_cpsr &= ~PSTATE_SS;
5738     env->spsr = cpsr_read(env);
5739     /* Clear IT bits.  */
5740     env->condexec_bits = 0;
5741     /* Switch to the new mode, and to the correct instruction set.  */
5742     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
5743     env->daif |= mask;
5744     /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
5745      * and we should just guard the thumb mode on V4 */
5746     if (arm_feature(env, ARM_FEATURE_V4T)) {
5747         env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
5748     }
5749     env->regs[14] = env->regs[15] + offset;
5750     env->regs[15] = addr;
5751     cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
5752 }
5753
5754
5755 /* Return the exception level which controls this address translation regime */
5756 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
5757 {
5758     switch (mmu_idx) {
5759     case ARMMMUIdx_S2NS:
5760     case ARMMMUIdx_S1E2:
5761         return 2;
5762     case ARMMMUIdx_S1E3:
5763         return 3;
5764     case ARMMMUIdx_S1SE0:
5765         return arm_el_is_aa64(env, 3) ? 1 : 3;
5766     case ARMMMUIdx_S1SE1:
5767     case ARMMMUIdx_S1NSE0:
5768     case ARMMMUIdx_S1NSE1:
5769         return 1;
5770     default:
5771         g_assert_not_reached();
5772     }
5773 }
5774
5775 /* Return true if this address translation regime is secure */
5776 static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
5777 {
5778     switch (mmu_idx) {
5779     case ARMMMUIdx_S12NSE0:
5780     case ARMMMUIdx_S12NSE1:
5781     case ARMMMUIdx_S1NSE0:
5782     case ARMMMUIdx_S1NSE1:
5783     case ARMMMUIdx_S1E2:
5784     case ARMMMUIdx_S2NS:
5785         return false;
5786     case ARMMMUIdx_S1E3:
5787     case ARMMMUIdx_S1SE0:
5788     case ARMMMUIdx_S1SE1:
5789         return true;
5790     default:
5791         g_assert_not_reached();
5792     }
5793 }
5794
5795 /* Return the SCTLR value which controls this address translation regime */
5796 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
5797 {
5798     return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
5799 }
5800
5801 /* Return true if the specified stage of address translation is disabled */
5802 static inline bool regime_translation_disabled(CPUARMState *env,
5803                                                ARMMMUIdx mmu_idx)
5804 {
5805     if (mmu_idx == ARMMMUIdx_S2NS) {
5806         return (env->cp15.hcr_el2 & HCR_VM) == 0;
5807     }
5808     return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
5809 }
5810
5811 /* Return the TCR controlling this translation regime */
5812 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
5813 {
5814     if (mmu_idx == ARMMMUIdx_S2NS) {
5815         return &env->cp15.vtcr_el2;
5816     }
5817     return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
5818 }
5819
5820 /* Return the TTBR associated with this translation regime */
5821 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
5822                                    int ttbrn)
5823 {
5824     if (mmu_idx == ARMMMUIdx_S2NS) {
5825         return env->cp15.vttbr_el2;
5826     }
5827     if (ttbrn == 0) {
5828         return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
5829     } else {
5830         return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
5831     }
5832 }
5833
5834 /* Return true if the translation regime is using LPAE format page tables */
5835 static inline bool regime_using_lpae_format(CPUARMState *env,
5836                                             ARMMMUIdx mmu_idx)
5837 {
5838     int el = regime_el(env, mmu_idx);
5839     if (el == 2 || arm_el_is_aa64(env, el)) {
5840         return true;
5841     }
5842     if (arm_feature(env, ARM_FEATURE_LPAE)
5843         && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
5844         return true;
5845     }
5846     return false;
5847 }
5848
5849 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
5850 {
5851     switch (mmu_idx) {
5852     case ARMMMUIdx_S1SE0:
5853     case ARMMMUIdx_S1NSE0:
5854         return true;
5855     default:
5856         return false;
5857     case ARMMMUIdx_S12NSE0:
5858     case ARMMMUIdx_S12NSE1:
5859         g_assert_not_reached();
5860     }
5861 }
5862
5863 /* Translate section/page access permissions to page
5864  * R/W protection flags
5865  *
5866  * @env:         CPUARMState
5867  * @mmu_idx:     MMU index indicating required translation regime
5868  * @ap:          The 3-bit access permissions (AP[2:0])
5869  * @domain_prot: The 2-bit domain access permissions
5870  */
5871 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
5872                                 int ap, int domain_prot)
5873 {
5874     bool is_user = regime_is_user(env, mmu_idx);
5875
5876     if (domain_prot == 3) {
5877         return PAGE_READ | PAGE_WRITE;
5878     }
5879
5880     switch (ap) {
5881     case 0:
5882         if (arm_feature(env, ARM_FEATURE_V7)) {
5883             return 0;
5884         }
5885         switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
5886         case SCTLR_S:
5887             return is_user ? 0 : PAGE_READ;
5888         case SCTLR_R:
5889             return PAGE_READ;
5890         default:
5891             return 0;
5892         }
5893     case 1:
5894         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
5895     case 2:
5896         if (is_user) {
5897             return PAGE_READ;
5898         } else {
5899             return PAGE_READ | PAGE_WRITE;
5900         }
5901     case 3:
5902         return PAGE_READ | PAGE_WRITE;
5903     case 4: /* Reserved.  */
5904         return 0;
5905     case 5:
5906         return is_user ? 0 : PAGE_READ;
5907     case 6:
5908         return PAGE_READ;
5909     case 7:
5910         if (!arm_feature(env, ARM_FEATURE_V6K)) {
5911             return 0;
5912         }
5913         return PAGE_READ;
5914     default:
5915         g_assert_not_reached();
5916     }
5917 }
5918
5919 /* Translate section/page access permissions to page
5920  * R/W protection flags.
5921  *
5922  * @ap:      The 2-bit simple AP (AP[2:1])
5923  * @is_user: TRUE if accessing from PL0
5924  */
5925 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
5926 {
5927     switch (ap) {
5928     case 0:
5929         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
5930     case 1:
5931         return PAGE_READ | PAGE_WRITE;
5932     case 2:
5933         return is_user ? 0 : PAGE_READ;
5934     case 3:
5935         return PAGE_READ;
5936     default:
5937         g_assert_not_reached();
5938     }
5939 }
5940
5941 static inline int
5942 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
5943 {
5944     return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
5945 }
5946
5947 /* Translate section/page access permissions to protection flags
5948  *
5949  * @env:     CPUARMState
5950  * @mmu_idx: MMU index indicating required translation regime
5951  * @is_aa64: TRUE if AArch64
5952  * @ap:      The 2-bit simple AP (AP[2:1])
5953  * @ns:      NS (non-secure) bit
5954  * @xn:      XN (execute-never) bit
5955  * @pxn:     PXN (privileged execute-never) bit
5956  */
5957 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
5958                       int ap, int ns, int xn, int pxn)
5959 {
5960     bool is_user = regime_is_user(env, mmu_idx);
5961     int prot_rw, user_rw;
5962     bool have_wxn;
5963     int wxn = 0;
5964
5965     assert(mmu_idx != ARMMMUIdx_S2NS);
5966
5967     user_rw = simple_ap_to_rw_prot_is_user(ap, true);
5968     if (is_user) {
5969         prot_rw = user_rw;
5970     } else {
5971         prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
5972     }
5973
5974     if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
5975         return prot_rw;
5976     }
5977
5978     /* TODO have_wxn should be replaced with
5979      *   ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
5980      * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
5981      * compatible processors have EL2, which is required for [U]WXN.
5982      */
5983     have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
5984
5985     if (have_wxn) {
5986         wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
5987     }
5988
5989     if (is_aa64) {
5990         switch (regime_el(env, mmu_idx)) {
5991         case 1:
5992             if (!is_user) {
5993                 xn = pxn || (user_rw & PAGE_WRITE);
5994             }
5995             break;
5996         case 2:
5997         case 3:
5998             break;
5999         }
6000     } else if (arm_feature(env, ARM_FEATURE_V7)) {
6001         switch (regime_el(env, mmu_idx)) {
6002         case 1:
6003         case 3:
6004             if (is_user) {
6005                 xn = xn || !(user_rw & PAGE_READ);
6006             } else {
6007                 int uwxn = 0;
6008                 if (have_wxn) {
6009                     uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6010                 }
6011                 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6012                      (uwxn && (user_rw & PAGE_WRITE));
6013             }
6014             break;
6015         case 2:
6016             break;
6017         }
6018     } else {
6019         xn = wxn = 0;
6020     }
6021
6022     if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6023         return prot_rw;
6024     }
6025     return prot_rw | PAGE_EXEC;
6026 }
6027
6028 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6029                                      uint32_t *table, uint32_t address)
6030 {
6031     /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
6032     TCR *tcr = regime_tcr(env, mmu_idx);
6033
6034     if (address & tcr->mask) {
6035         if (tcr->raw_tcr & TTBCR_PD1) {
6036             /* Translation table walk disabled for TTBR1 */
6037             return false;
6038         }
6039         *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
6040     } else {
6041         if (tcr->raw_tcr & TTBCR_PD0) {
6042             /* Translation table walk disabled for TTBR0 */
6043             return false;
6044         }
6045         *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
6046     }
6047     *table |= (address >> 18) & 0x3ffc;
6048     return true;
6049 }
6050
6051 /* All loads done in the course of a page table walk go through here.
6052  * TODO: rather than ignoring errors from physical memory reads (which
6053  * are external aborts in ARM terminology) we should propagate this
6054  * error out so that we can turn it into a Data Abort if this walk
6055  * was being done for a CPU load/store or an address translation instruction
6056  * (but not if it was for a debug access).
6057  */
6058 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure)
6059 {
6060     MemTxAttrs attrs = {};
6061
6062     attrs.secure = is_secure;
6063     return address_space_ldl(cs->as, addr, attrs, NULL);
6064 }
6065
6066 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure)
6067 {
6068     MemTxAttrs attrs = {};
6069
6070     attrs.secure = is_secure;
6071     return address_space_ldq(cs->as, addr, attrs, NULL);
6072 }
6073
6074 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6075                              int access_type, ARMMMUIdx mmu_idx,
6076                              hwaddr *phys_ptr, int *prot,
6077                              target_ulong *page_size, uint32_t *fsr)
6078 {
6079     CPUState *cs = CPU(arm_env_get_cpu(env));
6080     int code;
6081     uint32_t table;
6082     uint32_t desc;
6083     int type;
6084     int ap;
6085     int domain = 0;
6086     int domain_prot;
6087     hwaddr phys_addr;
6088     uint32_t dacr;
6089
6090     /* Pagetable walk.  */
6091     /* Lookup l1 descriptor.  */
6092     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
6093         /* Section translation fault if page walk is disabled by PD0 or PD1 */
6094         code = 5;
6095         goto do_fault;
6096     }
6097     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6098     type = (desc & 3);
6099     domain = (desc >> 5) & 0x0f;
6100     if (regime_el(env, mmu_idx) == 1) {
6101         dacr = env->cp15.dacr_ns;
6102     } else {
6103         dacr = env->cp15.dacr_s;
6104     }
6105     domain_prot = (dacr >> (domain * 2)) & 3;
6106     if (type == 0) {
6107         /* Section translation fault.  */
6108         code = 5;
6109         goto do_fault;
6110     }
6111     if (domain_prot == 0 || domain_prot == 2) {
6112         if (type == 2)
6113             code = 9; /* Section domain fault.  */
6114         else
6115             code = 11; /* Page domain fault.  */
6116         goto do_fault;
6117     }
6118     if (type == 2) {
6119         /* 1Mb section.  */
6120         phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6121         ap = (desc >> 10) & 3;
6122         code = 13;
6123         *page_size = 1024 * 1024;
6124     } else {
6125         /* Lookup l2 entry.  */
6126         if (type == 1) {
6127             /* Coarse pagetable.  */
6128             table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6129         } else {
6130             /* Fine pagetable.  */
6131             table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6132         }
6133         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6134         switch (desc & 3) {
6135         case 0: /* Page translation fault.  */
6136             code = 7;
6137             goto do_fault;
6138         case 1: /* 64k page.  */
6139             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6140             ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
6141             *page_size = 0x10000;
6142             break;
6143         case 2: /* 4k page.  */
6144             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6145             ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
6146             *page_size = 0x1000;
6147             break;
6148         case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
6149             if (type == 1) {
6150                 /* ARMv6/XScale extended small page format */
6151                 if (arm_feature(env, ARM_FEATURE_XSCALE)
6152                     || arm_feature(env, ARM_FEATURE_V6)) {
6153                     phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6154                     *page_size = 0x1000;
6155                 } else {
6156                     /* UNPREDICTABLE in ARMv5; we choose to take a
6157                      * page translation fault.
6158                      */
6159                     code = 7;
6160                     goto do_fault;
6161                 }
6162             } else {
6163                 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
6164                 *page_size = 0x400;
6165             }
6166             ap = (desc >> 4) & 3;
6167             break;
6168         default:
6169             /* Never happens, but compiler isn't smart enough to tell.  */
6170             abort();
6171         }
6172         code = 15;
6173     }
6174     *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6175     *prot |= *prot ? PAGE_EXEC : 0;
6176     if (!(*prot & (1 << access_type))) {
6177         /* Access permission fault.  */
6178         goto do_fault;
6179     }
6180     *phys_ptr = phys_addr;
6181     return false;
6182 do_fault:
6183     *fsr = code | (domain << 4);
6184     return true;
6185 }
6186
6187 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
6188                              int access_type, ARMMMUIdx mmu_idx,
6189                              hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
6190                              target_ulong *page_size, uint32_t *fsr)
6191 {
6192     CPUState *cs = CPU(arm_env_get_cpu(env));
6193     int code;
6194     uint32_t table;
6195     uint32_t desc;
6196     uint32_t xn;
6197     uint32_t pxn = 0;
6198     int type;
6199     int ap;
6200     int domain = 0;
6201     int domain_prot;
6202     hwaddr phys_addr;
6203     uint32_t dacr;
6204     bool ns;
6205
6206     /* Pagetable walk.  */
6207     /* Lookup l1 descriptor.  */
6208     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
6209         /* Section translation fault if page walk is disabled by PD0 or PD1 */
6210         code = 5;
6211         goto do_fault;
6212     }
6213     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6214     type = (desc & 3);
6215     if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
6216         /* Section translation fault, or attempt to use the encoding
6217          * which is Reserved on implementations without PXN.
6218          */
6219         code = 5;
6220         goto do_fault;
6221     }
6222     if ((type == 1) || !(desc & (1 << 18))) {
6223         /* Page or Section.  */
6224         domain = (desc >> 5) & 0x0f;
6225     }
6226     if (regime_el(env, mmu_idx) == 1) {
6227         dacr = env->cp15.dacr_ns;
6228     } else {
6229         dacr = env->cp15.dacr_s;
6230     }
6231     domain_prot = (dacr >> (domain * 2)) & 3;
6232     if (domain_prot == 0 || domain_prot == 2) {
6233         if (type != 1) {
6234             code = 9; /* Section domain fault.  */
6235         } else {
6236             code = 11; /* Page domain fault.  */
6237         }
6238         goto do_fault;
6239     }
6240     if (type != 1) {
6241         if (desc & (1 << 18)) {
6242             /* Supersection.  */
6243             phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
6244             phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
6245             phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
6246             *page_size = 0x1000000;
6247         } else {
6248             /* Section.  */
6249             phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6250             *page_size = 0x100000;
6251         }
6252         ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
6253         xn = desc & (1 << 4);
6254         pxn = desc & 1;
6255         code = 13;
6256         ns = extract32(desc, 19, 1);
6257     } else {
6258         if (arm_feature(env, ARM_FEATURE_PXN)) {
6259             pxn = (desc >> 2) & 1;
6260         }
6261         ns = extract32(desc, 3, 1);
6262         /* Lookup l2 entry.  */
6263         table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6264         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
6265         ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
6266         switch (desc & 3) {
6267         case 0: /* Page translation fault.  */
6268             code = 7;
6269             goto do_fault;
6270         case 1: /* 64k page.  */
6271             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6272             xn = desc & (1 << 15);
6273             *page_size = 0x10000;
6274             break;
6275         case 2: case 3: /* 4k page.  */
6276             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6277             xn = desc & 1;
6278             *page_size = 0x1000;
6279             break;
6280         default:
6281             /* Never happens, but compiler isn't smart enough to tell.  */
6282             abort();
6283         }
6284         code = 15;
6285     }
6286     if (domain_prot == 3) {
6287         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
6288     } else {
6289         if (pxn && !regime_is_user(env, mmu_idx)) {
6290             xn = 1;
6291         }
6292         if (xn && access_type == 2)
6293             goto do_fault;
6294
6295         if (arm_feature(env, ARM_FEATURE_V6K) &&
6296                 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
6297             /* The simplified model uses AP[0] as an access control bit.  */
6298             if ((ap & 1) == 0) {
6299                 /* Access flag fault.  */
6300                 code = (code == 15) ? 6 : 3;
6301                 goto do_fault;
6302             }
6303             *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
6304         } else {
6305             *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6306         }
6307         if (*prot && !xn) {
6308             *prot |= PAGE_EXEC;
6309         }
6310         if (!(*prot & (1 << access_type))) {
6311             /* Access permission fault.  */
6312             goto do_fault;
6313         }
6314     }
6315     if (ns) {
6316         /* The NS bit will (as required by the architecture) have no effect if
6317          * the CPU doesn't support TZ or this is a non-secure translation
6318          * regime, because the attribute will already be non-secure.
6319          */
6320         attrs->secure = false;
6321     }
6322     *phys_ptr = phys_addr;
6323     return false;
6324 do_fault:
6325     *fsr = code | (domain << 4);
6326     return true;
6327 }
6328
6329 /* Fault type for long-descriptor MMU fault reporting; this corresponds
6330  * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
6331  */
6332 typedef enum {
6333     translation_fault = 1,
6334     access_fault = 2,
6335     permission_fault = 3,
6336 } MMUFaultType;
6337
6338 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
6339                                int access_type, ARMMMUIdx mmu_idx,
6340                                hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
6341                                target_ulong *page_size_ptr, uint32_t *fsr)
6342 {
6343     CPUState *cs = CPU(arm_env_get_cpu(env));
6344     /* Read an LPAE long-descriptor translation table. */
6345     MMUFaultType fault_type = translation_fault;
6346     uint32_t level = 1;
6347     uint32_t epd;
6348     int32_t tsz;
6349     uint32_t tg;
6350     uint64_t ttbr;
6351     int ttbr_select;
6352     hwaddr descaddr, descmask;
6353     uint32_t tableattrs;
6354     target_ulong page_size;
6355     uint32_t attrs;
6356     int32_t granule_sz = 9;
6357     int32_t va_size = 32;
6358     int32_t tbi = 0;
6359     TCR *tcr = regime_tcr(env, mmu_idx);
6360     int ap, ns, xn, pxn;
6361     uint32_t el = regime_el(env, mmu_idx);
6362     bool ttbr1_valid = true;
6363
6364     /* TODO:
6365      * This code does not handle the different format TCR for VTCR_EL2.
6366      * This code also does not support shareability levels.
6367      * Attribute and permission bit handling should also be checked when adding
6368      * support for those page table walks.
6369      */
6370     if (arm_el_is_aa64(env, el)) {
6371         va_size = 64;
6372         if (el > 1) {
6373             tbi = extract64(tcr->raw_tcr, 20, 1);
6374         } else {
6375             if (extract64(address, 55, 1)) {
6376                 tbi = extract64(tcr->raw_tcr, 38, 1);
6377             } else {
6378                 tbi = extract64(tcr->raw_tcr, 37, 1);
6379             }
6380         }
6381         tbi *= 8;
6382
6383         /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
6384          * invalid.
6385          */
6386         if (el > 1) {
6387             ttbr1_valid = false;
6388         }
6389     } else {
6390         /* There is no TTBR1 for EL2 */
6391         if (el == 2) {
6392             ttbr1_valid = false;
6393         }
6394     }
6395
6396     /* Determine whether this address is in the region controlled by
6397      * TTBR0 or TTBR1 (or if it is in neither region and should fault).
6398      * This is a Non-secure PL0/1 stage 1 translation, so controlled by
6399      * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
6400      */
6401     uint32_t t0sz = extract32(tcr->raw_tcr, 0, 6);
6402     if (va_size == 64) {
6403         t0sz = MIN(t0sz, 39);
6404         t0sz = MAX(t0sz, 16);
6405     }
6406     uint32_t t1sz = extract32(tcr->raw_tcr, 16, 6);
6407     if (va_size == 64) {
6408         t1sz = MIN(t1sz, 39);
6409         t1sz = MAX(t1sz, 16);
6410     }
6411     if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
6412         /* there is a ttbr0 region and we are in it (high bits all zero) */
6413         ttbr_select = 0;
6414     } else if (ttbr1_valid && t1sz &&
6415                !extract64(~address, va_size - t1sz, t1sz - tbi)) {
6416         /* there is a ttbr1 region and we are in it (high bits all one) */
6417         ttbr_select = 1;
6418     } else if (!t0sz) {
6419         /* ttbr0 region is "everything not in the ttbr1 region" */
6420         ttbr_select = 0;
6421     } else if (!t1sz && ttbr1_valid) {
6422         /* ttbr1 region is "everything not in the ttbr0 region" */
6423         ttbr_select = 1;
6424     } else {
6425         /* in the gap between the two regions, this is a Translation fault */
6426         fault_type = translation_fault;
6427         goto do_fault;
6428     }
6429
6430     /* Note that QEMU ignores shareability and cacheability attributes,
6431      * so we don't need to do anything with the SH, ORGN, IRGN fields
6432      * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
6433      * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
6434      * implement any ASID-like capability so we can ignore it (instead
6435      * we will always flush the TLB any time the ASID is changed).
6436      */
6437     if (ttbr_select == 0) {
6438         ttbr = regime_ttbr(env, mmu_idx, 0);
6439         epd = extract32(tcr->raw_tcr, 7, 1);
6440         tsz = t0sz;
6441
6442         tg = extract32(tcr->raw_tcr, 14, 2);
6443         if (tg == 1) { /* 64KB pages */
6444             granule_sz = 13;
6445         }
6446         if (tg == 2) { /* 16KB pages */
6447             granule_sz = 11;
6448         }
6449     } else {
6450         /* We should only be here if TTBR1 is valid */
6451         assert(ttbr1_valid);
6452
6453         ttbr = regime_ttbr(env, mmu_idx, 1);
6454         epd = extract32(tcr->raw_tcr, 23, 1);
6455         tsz = t1sz;
6456
6457         tg = extract32(tcr->raw_tcr, 30, 2);
6458         if (tg == 3)  { /* 64KB pages */
6459             granule_sz = 13;
6460         }
6461         if (tg == 1) { /* 16KB pages */
6462             granule_sz = 11;
6463         }
6464     }
6465
6466     /* Here we should have set up all the parameters for the translation:
6467      * va_size, ttbr, epd, tsz, granule_sz, tbi
6468      */
6469
6470     if (epd) {
6471         /* Translation table walk disabled => Translation fault on TLB miss
6472          * Note: This is always 0 on 64-bit EL2 and EL3.
6473          */
6474         goto do_fault;
6475     }
6476
6477     /* The starting level depends on the virtual address size (which can be
6478      * up to 48 bits) and the translation granule size. It indicates the number
6479      * of strides (granule_sz bits at a time) needed to consume the bits
6480      * of the input address. In the pseudocode this is:
6481      *  level = 4 - RoundUp((inputsize - grainsize) / stride)
6482      * where their 'inputsize' is our 'va_size - tsz', 'grainsize' is
6483      * our 'granule_sz + 3' and 'stride' is our 'granule_sz'.
6484      * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
6485      *     = 4 - (va_size - tsz - granule_sz - 3 + granule_sz - 1) / granule_sz
6486      *     = 4 - (va_size - tsz - 4) / granule_sz;
6487      */
6488     level = 4 - (va_size - tsz - 4) / granule_sz;
6489
6490     /* Clear the vaddr bits which aren't part of the within-region address,
6491      * so that we don't have to special case things when calculating the
6492      * first descriptor address.
6493      */
6494     if (tsz) {
6495         address &= (1ULL << (va_size - tsz)) - 1;
6496     }
6497
6498     descmask = (1ULL << (granule_sz + 3)) - 1;
6499
6500     /* Now we can extract the actual base address from the TTBR */
6501     descaddr = extract64(ttbr, 0, 48);
6502     descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
6503
6504     /* Secure accesses start with the page table in secure memory and
6505      * can be downgraded to non-secure at any step. Non-secure accesses
6506      * remain non-secure. We implement this by just ORing in the NSTable/NS
6507      * bits at each step.
6508      */
6509     tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
6510     for (;;) {
6511         uint64_t descriptor;
6512         bool nstable;
6513
6514         descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
6515         descaddr &= ~7ULL;
6516         nstable = extract32(tableattrs, 4, 1);
6517         descriptor = arm_ldq_ptw(cs, descaddr, !nstable);
6518         if (!(descriptor & 1) ||
6519             (!(descriptor & 2) && (level == 3))) {
6520             /* Invalid, or the Reserved level 3 encoding */
6521             goto do_fault;
6522         }
6523         descaddr = descriptor & 0xfffffff000ULL;
6524
6525         if ((descriptor & 2) && (level < 3)) {
6526             /* Table entry. The top five bits are attributes which  may
6527              * propagate down through lower levels of the table (and
6528              * which are all arranged so that 0 means "no effect", so
6529              * we can gather them up by ORing in the bits at each level).
6530              */
6531             tableattrs |= extract64(descriptor, 59, 5);
6532             level++;
6533             continue;
6534         }
6535         /* Block entry at level 1 or 2, or page entry at level 3.
6536          * These are basically the same thing, although the number
6537          * of bits we pull in from the vaddr varies.
6538          */
6539         page_size = (1ULL << ((granule_sz * (4 - level)) + 3));
6540         descaddr |= (address & (page_size - 1));
6541         /* Extract attributes from the descriptor and merge with table attrs */
6542         attrs = extract64(descriptor, 2, 10)
6543             | (extract64(descriptor, 52, 12) << 10);
6544         attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
6545         attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
6546         /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
6547          * means "force PL1 access only", which means forcing AP[1] to 0.
6548          */
6549         if (extract32(tableattrs, 2, 1)) {
6550             attrs &= ~(1 << 4);
6551         }
6552         attrs |= nstable << 3; /* NS */
6553         break;
6554     }
6555     /* Here descaddr is the final physical address, and attributes
6556      * are all in attrs.
6557      */
6558     fault_type = access_fault;
6559     if ((attrs & (1 << 8)) == 0) {
6560         /* Access flag */
6561         goto do_fault;
6562     }
6563
6564     ap = extract32(attrs, 4, 2);
6565     ns = extract32(attrs, 3, 1);
6566     xn = extract32(attrs, 12, 1);
6567     pxn = extract32(attrs, 11, 1);
6568
6569     *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
6570
6571     fault_type = permission_fault;
6572     if (!(*prot & (1 << access_type))) {
6573         goto do_fault;
6574     }
6575
6576     if (ns) {
6577         /* The NS bit will (as required by the architecture) have no effect if
6578          * the CPU doesn't support TZ or this is a non-secure translation
6579          * regime, because the attribute will already be non-secure.
6580          */
6581         txattrs->secure = false;
6582     }
6583     *phys_ptr = descaddr;
6584     *page_size_ptr = page_size;
6585     return false;
6586
6587 do_fault:
6588     /* Long-descriptor format IFSR/DFSR value */
6589     *fsr = (1 << 9) | (fault_type << 2) | level;
6590     return true;
6591 }
6592
6593 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
6594                                                 ARMMMUIdx mmu_idx,
6595                                                 int32_t address, int *prot)
6596 {
6597     *prot = PAGE_READ | PAGE_WRITE;
6598     switch (address) {
6599     case 0xF0000000 ... 0xFFFFFFFF:
6600         if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
6601             *prot |= PAGE_EXEC;
6602         }
6603         break;
6604     case 0x00000000 ... 0x7FFFFFFF:
6605         *prot |= PAGE_EXEC;
6606         break;
6607     }
6608
6609 }
6610
6611 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
6612                                  int access_type, ARMMMUIdx mmu_idx,
6613                                  hwaddr *phys_ptr, int *prot, uint32_t *fsr)
6614 {
6615     ARMCPU *cpu = arm_env_get_cpu(env);
6616     int n;
6617     bool is_user = regime_is_user(env, mmu_idx);
6618
6619     *phys_ptr = address;
6620     *prot = 0;
6621
6622     if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
6623         get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
6624     } else { /* MPU enabled */
6625         for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
6626             /* region search */
6627             uint32_t base = env->pmsav7.drbar[n];
6628             uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
6629             uint32_t rmask;
6630             bool srdis = false;
6631
6632             if (!(env->pmsav7.drsr[n] & 0x1)) {
6633                 continue;
6634             }
6635
6636             if (!rsize) {
6637                 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
6638                 continue;
6639             }
6640             rsize++;
6641             rmask = (1ull << rsize) - 1;
6642
6643             if (base & rmask) {
6644                 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
6645                               "to DRSR region size, mask = %" PRIx32,
6646                               base, rmask);
6647                 continue;
6648             }
6649
6650             if (address < base || address > base + rmask) {
6651                 continue;
6652             }
6653
6654             /* Region matched */
6655
6656             if (rsize >= 8) { /* no subregions for regions < 256 bytes */
6657                 int i, snd;
6658                 uint32_t srdis_mask;
6659
6660                 rsize -= 3; /* sub region size (power of 2) */
6661                 snd = ((address - base) >> rsize) & 0x7;
6662                 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
6663
6664                 srdis_mask = srdis ? 0x3 : 0x0;
6665                 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
6666                     /* This will check in groups of 2, 4 and then 8, whether
6667                      * the subregion bits are consistent. rsize is incremented
6668                      * back up to give the region size, considering consistent
6669                      * adjacent subregions as one region. Stop testing if rsize
6670                      * is already big enough for an entire QEMU page.
6671                      */
6672                     int snd_rounded = snd & ~(i - 1);
6673                     uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
6674                                                      snd_rounded + 8, i);
6675                     if (srdis_mask ^ srdis_multi) {
6676                         break;
6677                     }
6678                     srdis_mask = (srdis_mask << i) | srdis_mask;
6679                     rsize++;
6680                 }
6681             }
6682             if (rsize < TARGET_PAGE_BITS) {
6683                 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
6684                               "alignment of %" PRIu32 " bits. Minimum is %d\n",
6685                               rsize, TARGET_PAGE_BITS);
6686                 continue;
6687             }
6688             if (srdis) {
6689                 continue;
6690             }
6691             break;
6692         }
6693
6694         if (n == -1) { /* no hits */
6695             if (cpu->pmsav7_dregion &&
6696                 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
6697                 /* background fault */
6698                 *fsr = 0;
6699                 return true;
6700             }
6701             get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
6702         } else { /* a MPU hit! */
6703             uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
6704
6705             if (is_user) { /* User mode AP bit decoding */
6706                 switch (ap) {
6707                 case 0:
6708                 case 1:
6709                 case 5:
6710                     break; /* no access */
6711                 case 3:
6712                     *prot |= PAGE_WRITE;
6713                     /* fall through */
6714                 case 2:
6715                 case 6:
6716                     *prot |= PAGE_READ | PAGE_EXEC;
6717                     break;
6718                 default:
6719                     qemu_log_mask(LOG_GUEST_ERROR,
6720                                   "Bad value for AP bits in DRACR %"
6721                                   PRIx32 "\n", ap);
6722                 }
6723             } else { /* Priv. mode AP bits decoding */
6724                 switch (ap) {
6725                 case 0:
6726                     break; /* no access */
6727                 case 1:
6728                 case 2:
6729                 case 3:
6730                     *prot |= PAGE_WRITE;
6731                     /* fall through */
6732                 case 5:
6733                 case 6:
6734                     *prot |= PAGE_READ | PAGE_EXEC;
6735                     break;
6736                 default:
6737                     qemu_log_mask(LOG_GUEST_ERROR,
6738                                   "Bad value for AP bits in DRACR %"
6739                                   PRIx32 "\n", ap);
6740                 }
6741             }
6742
6743             /* execute never */
6744             if (env->pmsav7.dracr[n] & (1 << 12)) {
6745                 *prot &= ~PAGE_EXEC;
6746             }
6747         }
6748     }
6749
6750     *fsr = 0x00d; /* Permission fault */
6751     return !(*prot & (1 << access_type));
6752 }
6753
6754 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
6755                                  int access_type, ARMMMUIdx mmu_idx,
6756                                  hwaddr *phys_ptr, int *prot, uint32_t *fsr)
6757 {
6758     int n;
6759     uint32_t mask;
6760     uint32_t base;
6761     bool is_user = regime_is_user(env, mmu_idx);
6762
6763     *phys_ptr = address;
6764     for (n = 7; n >= 0; n--) {
6765         base = env->cp15.c6_region[n];
6766         if ((base & 1) == 0) {
6767             continue;
6768         }
6769         mask = 1 << ((base >> 1) & 0x1f);
6770         /* Keep this shift separate from the above to avoid an
6771            (undefined) << 32.  */
6772         mask = (mask << 1) - 1;
6773         if (((base ^ address) & ~mask) == 0) {
6774             break;
6775         }
6776     }
6777     if (n < 0) {
6778         *fsr = 2;
6779         return true;
6780     }
6781
6782     if (access_type == 2) {
6783         mask = env->cp15.pmsav5_insn_ap;
6784     } else {
6785         mask = env->cp15.pmsav5_data_ap;
6786     }
6787     mask = (mask >> (n * 4)) & 0xf;
6788     switch (mask) {
6789     case 0:
6790         *fsr = 1;
6791         return true;
6792     case 1:
6793         if (is_user) {
6794             *fsr = 1;
6795             return true;
6796         }
6797         *prot = PAGE_READ | PAGE_WRITE;
6798         break;
6799     case 2:
6800         *prot = PAGE_READ;
6801         if (!is_user) {
6802             *prot |= PAGE_WRITE;
6803         }
6804         break;
6805     case 3:
6806         *prot = PAGE_READ | PAGE_WRITE;
6807         break;
6808     case 5:
6809         if (is_user) {
6810             *fsr = 1;
6811             return true;
6812         }
6813         *prot = PAGE_READ;
6814         break;
6815     case 6:
6816         *prot = PAGE_READ;
6817         break;
6818     default:
6819         /* Bad permission.  */
6820         *fsr = 1;
6821         return true;
6822     }
6823     *prot |= PAGE_EXEC;
6824     return false;
6825 }
6826
6827 /* get_phys_addr - get the physical address for this virtual address
6828  *
6829  * Find the physical address corresponding to the given virtual address,
6830  * by doing a translation table walk on MMU based systems or using the
6831  * MPU state on MPU based systems.
6832  *
6833  * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
6834  * prot and page_size may not be filled in, and the populated fsr value provides
6835  * information on why the translation aborted, in the format of a
6836  * DFSR/IFSR fault register, with the following caveats:
6837  *  * we honour the short vs long DFSR format differences.
6838  *  * the WnR bit is never set (the caller must do this).
6839  *  * for PSMAv5 based systems we don't bother to return a full FSR format
6840  *    value.
6841  *
6842  * @env: CPUARMState
6843  * @address: virtual address to get physical address for
6844  * @access_type: 0 for read, 1 for write, 2 for execute
6845  * @mmu_idx: MMU index indicating required translation regime
6846  * @phys_ptr: set to the physical address corresponding to the virtual address
6847  * @attrs: set to the memory transaction attributes to use
6848  * @prot: set to the permissions for the page containing phys_ptr
6849  * @page_size: set to the size of the page containing phys_ptr
6850  * @fsr: set to the DFSR/IFSR value on failure
6851  */
6852 static inline bool get_phys_addr(CPUARMState *env, target_ulong address,
6853                                  int access_type, ARMMMUIdx mmu_idx,
6854                                  hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
6855                                  target_ulong *page_size, uint32_t *fsr)
6856 {
6857     if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6858         /* TODO: when we support EL2 we should here call ourselves recursively
6859          * to do the stage 1 and then stage 2 translations. The arm_ld*_ptw
6860          * functions will also need changing to perform ARMMMUIdx_S2NS loads
6861          * rather than direct physical memory loads when appropriate.
6862          * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
6863          */
6864         assert(!arm_feature(env, ARM_FEATURE_EL2));
6865         mmu_idx += ARMMMUIdx_S1NSE0;
6866     }
6867
6868     /* The page table entries may downgrade secure to non-secure, but
6869      * cannot upgrade an non-secure translation regime's attributes
6870      * to secure.
6871      */
6872     attrs->secure = regime_is_secure(env, mmu_idx);
6873     attrs->user = regime_is_user(env, mmu_idx);
6874
6875     /* Fast Context Switch Extension. This doesn't exist at all in v8.
6876      * In v7 and earlier it affects all stage 1 translations.
6877      */
6878     if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
6879         && !arm_feature(env, ARM_FEATURE_V8)) {
6880         if (regime_el(env, mmu_idx) == 3) {
6881             address += env->cp15.fcseidr_s;
6882         } else {
6883             address += env->cp15.fcseidr_ns;
6884         }
6885     }
6886
6887     /* pmsav7 has special handling for when MPU is disabled so call it before
6888      * the common MMU/MPU disabled check below.
6889      */
6890     if (arm_feature(env, ARM_FEATURE_MPU) &&
6891         arm_feature(env, ARM_FEATURE_V7)) {
6892         *page_size = TARGET_PAGE_SIZE;
6893         return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
6894                                     phys_ptr, prot, fsr);
6895     }
6896
6897     if (regime_translation_disabled(env, mmu_idx)) {
6898         /* MMU/MPU disabled.  */
6899         *phys_ptr = address;
6900         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
6901         *page_size = TARGET_PAGE_SIZE;
6902         return 0;
6903     }
6904
6905     if (arm_feature(env, ARM_FEATURE_MPU)) {
6906         /* Pre-v7 MPU */
6907         *page_size = TARGET_PAGE_SIZE;
6908         return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
6909                                     phys_ptr, prot, fsr);
6910     }
6911
6912     if (regime_using_lpae_format(env, mmu_idx)) {
6913         return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
6914                                   attrs, prot, page_size, fsr);
6915     } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
6916         return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
6917                                 attrs, prot, page_size, fsr);
6918     } else {
6919         return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
6920                                 prot, page_size, fsr);
6921     }
6922 }
6923
6924 /* Walk the page table and (if the mapping exists) add the page
6925  * to the TLB. Return false on success, or true on failure. Populate
6926  * fsr with ARM DFSR/IFSR fault register format value on failure.
6927  */
6928 bool arm_tlb_fill(CPUState *cs, vaddr address,
6929                   int access_type, int mmu_idx, uint32_t *fsr)
6930 {
6931     ARMCPU *cpu = ARM_CPU(cs);
6932     CPUARMState *env = &cpu->env;
6933     hwaddr phys_addr;
6934     target_ulong page_size;
6935     int prot;
6936     int ret;
6937     MemTxAttrs attrs = {};
6938
6939     ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
6940                         &attrs, &prot, &page_size, fsr);
6941     if (!ret) {
6942         /* Map a single [sub]page.  */
6943         phys_addr &= TARGET_PAGE_MASK;
6944         address &= TARGET_PAGE_MASK;
6945         tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
6946                                 prot, mmu_idx, page_size);
6947         return 0;
6948     }
6949
6950     return ret;
6951 }
6952
6953 hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
6954 {
6955     ARMCPU *cpu = ARM_CPU(cs);
6956     CPUARMState *env = &cpu->env;
6957     hwaddr phys_addr;
6958     target_ulong page_size;
6959     int prot;
6960     bool ret;
6961     uint32_t fsr;
6962     MemTxAttrs attrs = {};
6963
6964     ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
6965                         &attrs, &prot, &page_size, &fsr);
6966
6967     if (ret) {
6968         return -1;
6969     }
6970
6971     return phys_addr;
6972 }
6973
6974 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
6975 {
6976     if ((env->uncached_cpsr & CPSR_M) == mode) {
6977         env->regs[13] = val;
6978     } else {
6979         env->banked_r13[bank_number(mode)] = val;
6980     }
6981 }
6982
6983 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
6984 {
6985     if ((env->uncached_cpsr & CPSR_M) == mode) {
6986         return env->regs[13];
6987     } else {
6988         return env->banked_r13[bank_number(mode)];
6989     }
6990 }
6991
6992 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
6993 {
6994     ARMCPU *cpu = arm_env_get_cpu(env);
6995
6996     switch (reg) {
6997     case 0: /* APSR */
6998         return xpsr_read(env) & 0xf8000000;
6999     case 1: /* IAPSR */
7000         return xpsr_read(env) & 0xf80001ff;
7001     case 2: /* EAPSR */
7002         return xpsr_read(env) & 0xff00fc00;
7003     case 3: /* xPSR */
7004         return xpsr_read(env) & 0xff00fdff;
7005     case 5: /* IPSR */
7006         return xpsr_read(env) & 0x000001ff;
7007     case 6: /* EPSR */
7008         return xpsr_read(env) & 0x0700fc00;
7009     case 7: /* IEPSR */
7010         return xpsr_read(env) & 0x0700edff;
7011     case 8: /* MSP */
7012         return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
7013     case 9: /* PSP */
7014         return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
7015     case 16: /* PRIMASK */
7016         return (env->daif & PSTATE_I) != 0;
7017     case 17: /* BASEPRI */
7018     case 18: /* BASEPRI_MAX */
7019         return env->v7m.basepri;
7020     case 19: /* FAULTMASK */
7021         return (env->daif & PSTATE_F) != 0;
7022     case 20: /* CONTROL */
7023         return env->v7m.control;
7024     default:
7025         /* ??? For debugging only.  */
7026         cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
7027         return 0;
7028     }
7029 }
7030
7031 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
7032 {
7033     ARMCPU *cpu = arm_env_get_cpu(env);
7034
7035     switch (reg) {
7036     case 0: /* APSR */
7037         xpsr_write(env, val, 0xf8000000);
7038         break;
7039     case 1: /* IAPSR */
7040         xpsr_write(env, val, 0xf8000000);
7041         break;
7042     case 2: /* EAPSR */
7043         xpsr_write(env, val, 0xfe00fc00);
7044         break;
7045     case 3: /* xPSR */
7046         xpsr_write(env, val, 0xfe00fc00);
7047         break;
7048     case 5: /* IPSR */
7049         /* IPSR bits are readonly.  */
7050         break;
7051     case 6: /* EPSR */
7052         xpsr_write(env, val, 0x0600fc00);
7053         break;
7054     case 7: /* IEPSR */
7055         xpsr_write(env, val, 0x0600fc00);
7056         break;
7057     case 8: /* MSP */
7058         if (env->v7m.current_sp)
7059             env->v7m.other_sp = val;
7060         else
7061             env->regs[13] = val;
7062         break;
7063     case 9: /* PSP */
7064         if (env->v7m.current_sp)
7065             env->regs[13] = val;
7066         else
7067             env->v7m.other_sp = val;
7068         break;
7069     case 16: /* PRIMASK */
7070         if (val & 1) {
7071             env->daif |= PSTATE_I;
7072         } else {
7073             env->daif &= ~PSTATE_I;
7074         }
7075         break;
7076     case 17: /* BASEPRI */
7077         env->v7m.basepri = val & 0xff;
7078         break;
7079     case 18: /* BASEPRI_MAX */
7080         val &= 0xff;
7081         if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
7082             env->v7m.basepri = val;
7083         break;
7084     case 19: /* FAULTMASK */
7085         if (val & 1) {
7086             env->daif |= PSTATE_F;
7087         } else {
7088             env->daif &= ~PSTATE_F;
7089         }
7090         break;
7091     case 20: /* CONTROL */
7092         env->v7m.control = val & 3;
7093         switch_v7m_sp(env, (val & 2) != 0);
7094         break;
7095     default:
7096         /* ??? For debugging only.  */
7097         cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
7098         return;
7099     }
7100 }
7101
7102 #endif
7103
7104 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
7105 {
7106     /* Implement DC ZVA, which zeroes a fixed-length block of memory.
7107      * Note that we do not implement the (architecturally mandated)
7108      * alignment fault for attempts to use this on Device memory
7109      * (which matches the usual QEMU behaviour of not implementing either
7110      * alignment faults or any memory attribute handling).
7111      */
7112
7113     ARMCPU *cpu = arm_env_get_cpu(env);
7114     uint64_t blocklen = 4 << cpu->dcz_blocksize;
7115     uint64_t vaddr = vaddr_in & ~(blocklen - 1);
7116
7117 #ifndef CONFIG_USER_ONLY
7118     {
7119         /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
7120          * the block size so we might have to do more than one TLB lookup.
7121          * We know that in fact for any v8 CPU the page size is at least 4K
7122          * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
7123          * 1K as an artefact of legacy v5 subpage support being present in the
7124          * same QEMU executable.
7125          */
7126         int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
7127         void *hostaddr[maxidx];
7128         int try, i;
7129         unsigned mmu_idx = cpu_mmu_index(env, false);
7130         TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
7131
7132         for (try = 0; try < 2; try++) {
7133
7134             for (i = 0; i < maxidx; i++) {
7135                 hostaddr[i] = tlb_vaddr_to_host(env,
7136                                                 vaddr + TARGET_PAGE_SIZE * i,
7137                                                 1, mmu_idx);
7138                 if (!hostaddr[i]) {
7139                     break;
7140                 }
7141             }
7142             if (i == maxidx) {
7143                 /* If it's all in the TLB it's fair game for just writing to;
7144                  * we know we don't need to update dirty status, etc.
7145                  */
7146                 for (i = 0; i < maxidx - 1; i++) {
7147                     memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
7148                 }
7149                 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
7150                 return;
7151             }
7152             /* OK, try a store and see if we can populate the tlb. This
7153              * might cause an exception if the memory isn't writable,
7154              * in which case we will longjmp out of here. We must for
7155              * this purpose use the actual register value passed to us
7156              * so that we get the fault address right.
7157              */
7158             helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
7159             /* Now we can populate the other TLB entries, if any */
7160             for (i = 0; i < maxidx; i++) {
7161                 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
7162                 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
7163                     helper_ret_stb_mmu(env, va, 0, oi, GETRA());
7164                 }
7165             }
7166         }
7167
7168         /* Slow path (probably attempt to do this to an I/O device or
7169          * similar, or clearing of a block of code we have translations
7170          * cached for). Just do a series of byte writes as the architecture
7171          * demands. It's not worth trying to use a cpu_physical_memory_map(),
7172          * memset(), unmap() sequence here because:
7173          *  + we'd need to account for the blocksize being larger than a page
7174          *  + the direct-RAM access case is almost always going to be dealt
7175          *    with in the fastpath code above, so there's no speed benefit
7176          *  + we would have to deal with the map returning NULL because the
7177          *    bounce buffer was in use
7178          */
7179         for (i = 0; i < blocklen; i++) {
7180             helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
7181         }
7182     }
7183 #else
7184     memset(g2h(vaddr), 0, blocklen);
7185 #endif
7186 }
7187
7188 /* Note that signed overflow is undefined in C.  The following routines are
7189    careful to use unsigned types where modulo arithmetic is required.
7190    Failure to do so _will_ break on newer gcc.  */
7191
7192 /* Signed saturating arithmetic.  */
7193
7194 /* Perform 16-bit signed saturating addition.  */
7195 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
7196 {
7197     uint16_t res;
7198
7199     res = a + b;
7200     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
7201         if (a & 0x8000)
7202             res = 0x8000;
7203         else
7204             res = 0x7fff;
7205     }
7206     return res;
7207 }
7208
7209 /* Perform 8-bit signed saturating addition.  */
7210 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
7211 {
7212     uint8_t res;
7213
7214     res = a + b;
7215     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
7216         if (a & 0x80)
7217             res = 0x80;
7218         else
7219             res = 0x7f;
7220     }
7221     return res;
7222 }
7223
7224 /* Perform 16-bit signed saturating subtraction.  */
7225 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
7226 {
7227     uint16_t res;
7228
7229     res = a - b;
7230     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
7231         if (a & 0x8000)
7232             res = 0x8000;
7233         else
7234             res = 0x7fff;
7235     }
7236     return res;
7237 }
7238
7239 /* Perform 8-bit signed saturating subtraction.  */
7240 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
7241 {
7242     uint8_t res;
7243
7244     res = a - b;
7245     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
7246         if (a & 0x80)
7247             res = 0x80;
7248         else
7249             res = 0x7f;
7250     }
7251     return res;
7252 }
7253
7254 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
7255 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
7256 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
7257 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
7258 #define PFX q
7259
7260 #include "op_addsub.h"
7261
7262 /* Unsigned saturating arithmetic.  */
7263 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
7264 {
7265     uint16_t res;
7266     res = a + b;
7267     if (res < a)
7268         res = 0xffff;
7269     return res;
7270 }
7271
7272 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
7273 {
7274     if (a > b)
7275         return a - b;
7276     else
7277         return 0;
7278 }
7279
7280 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
7281 {
7282     uint8_t res;
7283     res = a + b;
7284     if (res < a)
7285         res = 0xff;
7286     return res;
7287 }
7288
7289 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
7290 {
7291     if (a > b)
7292         return a - b;
7293     else
7294         return 0;
7295 }
7296
7297 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
7298 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
7299 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
7300 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
7301 #define PFX uq
7302
7303 #include "op_addsub.h"
7304
7305 /* Signed modulo arithmetic.  */
7306 #define SARITH16(a, b, n, op) do { \
7307     int32_t sum; \
7308     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
7309     RESULT(sum, n, 16); \
7310     if (sum >= 0) \
7311         ge |= 3 << (n * 2); \
7312     } while(0)
7313
7314 #define SARITH8(a, b, n, op) do { \
7315     int32_t sum; \
7316     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
7317     RESULT(sum, n, 8); \
7318     if (sum >= 0) \
7319         ge |= 1 << n; \
7320     } while(0)
7321
7322
7323 #define ADD16(a, b, n) SARITH16(a, b, n, +)
7324 #define SUB16(a, b, n) SARITH16(a, b, n, -)
7325 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
7326 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
7327 #define PFX s
7328 #define ARITH_GE
7329
7330 #include "op_addsub.h"
7331
7332 /* Unsigned modulo arithmetic.  */
7333 #define ADD16(a, b, n) do { \
7334     uint32_t sum; \
7335     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
7336     RESULT(sum, n, 16); \
7337     if ((sum >> 16) == 1) \
7338         ge |= 3 << (n * 2); \
7339     } while(0)
7340
7341 #define ADD8(a, b, n) do { \
7342     uint32_t sum; \
7343     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
7344     RESULT(sum, n, 8); \
7345     if ((sum >> 8) == 1) \
7346         ge |= 1 << n; \
7347     } while(0)
7348
7349 #define SUB16(a, b, n) do { \
7350     uint32_t sum; \
7351     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
7352     RESULT(sum, n, 16); \
7353     if ((sum >> 16) == 0) \
7354         ge |= 3 << (n * 2); \
7355     } while(0)
7356
7357 #define SUB8(a, b, n) do { \
7358     uint32_t sum; \
7359     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
7360     RESULT(sum, n, 8); \
7361     if ((sum >> 8) == 0) \
7362         ge |= 1 << n; \
7363     } while(0)
7364
7365 #define PFX u
7366 #define ARITH_GE
7367
7368 #include "op_addsub.h"
7369
7370 /* Halved signed arithmetic.  */
7371 #define ADD16(a, b, n) \
7372   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
7373 #define SUB16(a, b, n) \
7374   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
7375 #define ADD8(a, b, n) \
7376   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
7377 #define SUB8(a, b, n) \
7378   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
7379 #define PFX sh
7380
7381 #include "op_addsub.h"
7382
7383 /* Halved unsigned arithmetic.  */
7384 #define ADD16(a, b, n) \
7385   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
7386 #define SUB16(a, b, n) \
7387   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
7388 #define ADD8(a, b, n) \
7389   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
7390 #define SUB8(a, b, n) \
7391   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
7392 #define PFX uh
7393
7394 #include "op_addsub.h"
7395
7396 static inline uint8_t do_usad(uint8_t a, uint8_t b)
7397 {
7398     if (a > b)
7399         return a - b;
7400     else
7401         return b - a;
7402 }
7403
7404 /* Unsigned sum of absolute byte differences.  */
7405 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
7406 {
7407     uint32_t sum;
7408     sum = do_usad(a, b);
7409     sum += do_usad(a >> 8, b >> 8);
7410     sum += do_usad(a >> 16, b >>16);
7411     sum += do_usad(a >> 24, b >> 24);
7412     return sum;
7413 }
7414
7415 /* For ARMv6 SEL instruction.  */
7416 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
7417 {
7418     uint32_t mask;
7419
7420     mask = 0;
7421     if (flags & 1)
7422         mask |= 0xff;
7423     if (flags & 2)
7424         mask |= 0xff00;
7425     if (flags & 4)
7426         mask |= 0xff0000;
7427     if (flags & 8)
7428         mask |= 0xff000000;
7429     return (a & mask) | (b & ~mask);
7430 }
7431
7432 /* VFP support.  We follow the convention used for VFP instructions:
7433    Single precision routines have a "s" suffix, double precision a
7434    "d" suffix.  */
7435
7436 /* Convert host exception flags to vfp form.  */
7437 static inline int vfp_exceptbits_from_host(int host_bits)
7438 {
7439     int target_bits = 0;
7440
7441     if (host_bits & float_flag_invalid)
7442         target_bits |= 1;
7443     if (host_bits & float_flag_divbyzero)
7444         target_bits |= 2;
7445     if (host_bits & float_flag_overflow)
7446         target_bits |= 4;
7447     if (host_bits & (float_flag_underflow | float_flag_output_denormal))
7448         target_bits |= 8;
7449     if (host_bits & float_flag_inexact)
7450         target_bits |= 0x10;
7451     if (host_bits & float_flag_input_denormal)
7452         target_bits |= 0x80;
7453     return target_bits;
7454 }
7455
7456 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
7457 {
7458     int i;
7459     uint32_t fpscr;
7460
7461     fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
7462             | (env->vfp.vec_len << 16)
7463             | (env->vfp.vec_stride << 20);
7464     i = get_float_exception_flags(&env->vfp.fp_status);
7465     i |= get_float_exception_flags(&env->vfp.standard_fp_status);
7466     fpscr |= vfp_exceptbits_from_host(i);
7467     return fpscr;
7468 }
7469
7470 uint32_t vfp_get_fpscr(CPUARMState *env)
7471 {
7472     return HELPER(vfp_get_fpscr)(env);
7473 }
7474
7475 /* Convert vfp exception flags to target form.  */
7476 static inline int vfp_exceptbits_to_host(int target_bits)
7477 {
7478     int host_bits = 0;
7479
7480     if (target_bits & 1)
7481         host_bits |= float_flag_invalid;
7482     if (target_bits & 2)
7483         host_bits |= float_flag_divbyzero;
7484     if (target_bits & 4)
7485         host_bits |= float_flag_overflow;
7486     if (target_bits & 8)
7487         host_bits |= float_flag_underflow;
7488     if (target_bits & 0x10)
7489         host_bits |= float_flag_inexact;
7490     if (target_bits & 0x80)
7491         host_bits |= float_flag_input_denormal;
7492     return host_bits;
7493 }
7494
7495 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
7496 {
7497     int i;
7498     uint32_t changed;
7499
7500     changed = env->vfp.xregs[ARM_VFP_FPSCR];
7501     env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
7502     env->vfp.vec_len = (val >> 16) & 7;
7503     env->vfp.vec_stride = (val >> 20) & 3;
7504
7505     changed ^= val;
7506     if (changed & (3 << 22)) {
7507         i = (val >> 22) & 3;
7508         switch (i) {
7509         case FPROUNDING_TIEEVEN:
7510             i = float_round_nearest_even;
7511             break;
7512         case FPROUNDING_POSINF:
7513             i = float_round_up;
7514             break;
7515         case FPROUNDING_NEGINF:
7516             i = float_round_down;
7517             break;
7518         case FPROUNDING_ZERO:
7519             i = float_round_to_zero;
7520             break;
7521         }
7522         set_float_rounding_mode(i, &env->vfp.fp_status);
7523     }
7524     if (changed & (1 << 24)) {
7525         set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
7526         set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
7527     }
7528     if (changed & (1 << 25))
7529         set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
7530
7531     i = vfp_exceptbits_to_host(val);
7532     set_float_exception_flags(i, &env->vfp.fp_status);
7533     set_float_exception_flags(0, &env->vfp.standard_fp_status);
7534 }
7535
7536 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
7537 {
7538     HELPER(vfp_set_fpscr)(env, val);
7539 }
7540
7541 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
7542
7543 #define VFP_BINOP(name) \
7544 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
7545 { \
7546     float_status *fpst = fpstp; \
7547     return float32_ ## name(a, b, fpst); \
7548 } \
7549 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
7550 { \
7551     float_status *fpst = fpstp; \
7552     return float64_ ## name(a, b, fpst); \
7553 }
7554 VFP_BINOP(add)
7555 VFP_BINOP(sub)
7556 VFP_BINOP(mul)
7557 VFP_BINOP(div)
7558 VFP_BINOP(min)
7559 VFP_BINOP(max)
7560 VFP_BINOP(minnum)
7561 VFP_BINOP(maxnum)
7562 #undef VFP_BINOP
7563
7564 float32 VFP_HELPER(neg, s)(float32 a)
7565 {
7566     return float32_chs(a);
7567 }
7568
7569 float64 VFP_HELPER(neg, d)(float64 a)
7570 {
7571     return float64_chs(a);
7572 }
7573
7574 float32 VFP_HELPER(abs, s)(float32 a)
7575 {
7576     return float32_abs(a);
7577 }
7578
7579 float64 VFP_HELPER(abs, d)(float64 a)
7580 {
7581     return float64_abs(a);
7582 }
7583
7584 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
7585 {
7586     return float32_sqrt(a, &env->vfp.fp_status);
7587 }
7588
7589 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
7590 {
7591     return float64_sqrt(a, &env->vfp.fp_status);
7592 }
7593
7594 /* XXX: check quiet/signaling case */
7595 #define DO_VFP_cmp(p, type) \
7596 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env)  \
7597 { \
7598     uint32_t flags; \
7599     switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
7600     case 0: flags = 0x6; break; \
7601     case -1: flags = 0x8; break; \
7602     case 1: flags = 0x2; break; \
7603     default: case 2: flags = 0x3; break; \
7604     } \
7605     env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
7606         | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
7607 } \
7608 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
7609 { \
7610     uint32_t flags; \
7611     switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
7612     case 0: flags = 0x6; break; \
7613     case -1: flags = 0x8; break; \
7614     case 1: flags = 0x2; break; \
7615     default: case 2: flags = 0x3; break; \
7616     } \
7617     env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
7618         | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
7619 }
7620 DO_VFP_cmp(s, float32)
7621 DO_VFP_cmp(d, float64)
7622 #undef DO_VFP_cmp
7623
7624 /* Integer to float and float to integer conversions */
7625
7626 #define CONV_ITOF(name, fsz, sign) \
7627     float##fsz HELPER(name)(uint32_t x, void *fpstp) \
7628 { \
7629     float_status *fpst = fpstp; \
7630     return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
7631 }
7632
7633 #define CONV_FTOI(name, fsz, sign, round) \
7634 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
7635 { \
7636     float_status *fpst = fpstp; \
7637     if (float##fsz##_is_any_nan(x)) { \
7638         float_raise(float_flag_invalid, fpst); \
7639         return 0; \
7640     } \
7641     return float##fsz##_to_##sign##int32##round(x, fpst); \
7642 }
7643
7644 #define FLOAT_CONVS(name, p, fsz, sign) \
7645 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
7646 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
7647 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
7648
7649 FLOAT_CONVS(si, s, 32, )
7650 FLOAT_CONVS(si, d, 64, )
7651 FLOAT_CONVS(ui, s, 32, u)
7652 FLOAT_CONVS(ui, d, 64, u)
7653
7654 #undef CONV_ITOF
7655 #undef CONV_FTOI
7656 #undef FLOAT_CONVS
7657
7658 /* floating point conversion */
7659 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
7660 {
7661     float64 r = float32_to_float64(x, &env->vfp.fp_status);
7662     /* ARM requires that S<->D conversion of any kind of NaN generates
7663      * a quiet NaN by forcing the most significant frac bit to 1.
7664      */
7665     return float64_maybe_silence_nan(r);
7666 }
7667
7668 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
7669 {
7670     float32 r =  float64_to_float32(x, &env->vfp.fp_status);
7671     /* ARM requires that S<->D conversion of any kind of NaN generates
7672      * a quiet NaN by forcing the most significant frac bit to 1.
7673      */
7674     return float32_maybe_silence_nan(r);
7675 }
7676
7677 /* VFP3 fixed point conversion.  */
7678 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
7679 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t  x, uint32_t shift, \
7680                                      void *fpstp) \
7681 { \
7682     float_status *fpst = fpstp; \
7683     float##fsz tmp; \
7684     tmp = itype##_to_##float##fsz(x, fpst); \
7685     return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
7686 }
7687
7688 /* Notice that we want only input-denormal exception flags from the
7689  * scalbn operation: the other possible flags (overflow+inexact if
7690  * we overflow to infinity, output-denormal) aren't correct for the
7691  * complete scale-and-convert operation.
7692  */
7693 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
7694 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
7695                                              uint32_t shift, \
7696                                              void *fpstp) \
7697 { \
7698     float_status *fpst = fpstp; \
7699     int old_exc_flags = get_float_exception_flags(fpst); \
7700     float##fsz tmp; \
7701     if (float##fsz##_is_any_nan(x)) { \
7702         float_raise(float_flag_invalid, fpst); \
7703         return 0; \
7704     } \
7705     tmp = float##fsz##_scalbn(x, shift, fpst); \
7706     old_exc_flags |= get_float_exception_flags(fpst) \
7707         & float_flag_input_denormal; \
7708     set_float_exception_flags(old_exc_flags, fpst); \
7709     return float##fsz##_to_##itype##round(tmp, fpst); \
7710 }
7711
7712 #define VFP_CONV_FIX(name, p, fsz, isz, itype)                   \
7713 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype)                     \
7714 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
7715 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
7716
7717 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype)               \
7718 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype)                     \
7719 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
7720
7721 VFP_CONV_FIX(sh, d, 64, 64, int16)
7722 VFP_CONV_FIX(sl, d, 64, 64, int32)
7723 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
7724 VFP_CONV_FIX(uh, d, 64, 64, uint16)
7725 VFP_CONV_FIX(ul, d, 64, 64, uint32)
7726 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
7727 VFP_CONV_FIX(sh, s, 32, 32, int16)
7728 VFP_CONV_FIX(sl, s, 32, 32, int32)
7729 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
7730 VFP_CONV_FIX(uh, s, 32, 32, uint16)
7731 VFP_CONV_FIX(ul, s, 32, 32, uint32)
7732 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
7733 #undef VFP_CONV_FIX
7734 #undef VFP_CONV_FIX_FLOAT
7735 #undef VFP_CONV_FLOAT_FIX_ROUND
7736
7737 /* Set the current fp rounding mode and return the old one.
7738  * The argument is a softfloat float_round_ value.
7739  */
7740 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
7741 {
7742     float_status *fp_status = &env->vfp.fp_status;
7743
7744     uint32_t prev_rmode = get_float_rounding_mode(fp_status);
7745     set_float_rounding_mode(rmode, fp_status);
7746
7747     return prev_rmode;
7748 }
7749
7750 /* Set the current fp rounding mode in the standard fp status and return
7751  * the old one. This is for NEON instructions that need to change the
7752  * rounding mode but wish to use the standard FPSCR values for everything
7753  * else. Always set the rounding mode back to the correct value after
7754  * modifying it.
7755  * The argument is a softfloat float_round_ value.
7756  */
7757 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
7758 {
7759     float_status *fp_status = &env->vfp.standard_fp_status;
7760
7761     uint32_t prev_rmode = get_float_rounding_mode(fp_status);
7762     set_float_rounding_mode(rmode, fp_status);
7763
7764     return prev_rmode;
7765 }
7766
7767 /* Half precision conversions.  */
7768 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
7769 {
7770     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7771     float32 r = float16_to_float32(make_float16(a), ieee, s);
7772     if (ieee) {
7773         return float32_maybe_silence_nan(r);
7774     }
7775     return r;
7776 }
7777
7778 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
7779 {
7780     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7781     float16 r = float32_to_float16(a, ieee, s);
7782     if (ieee) {
7783         r = float16_maybe_silence_nan(r);
7784     }
7785     return float16_val(r);
7786 }
7787
7788 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
7789 {
7790     return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
7791 }
7792
7793 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
7794 {
7795     return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
7796 }
7797
7798 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
7799 {
7800     return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
7801 }
7802
7803 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
7804 {
7805     return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
7806 }
7807
7808 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
7809 {
7810     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7811     float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
7812     if (ieee) {
7813         return float64_maybe_silence_nan(r);
7814     }
7815     return r;
7816 }
7817
7818 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
7819 {
7820     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7821     float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
7822     if (ieee) {
7823         r = float16_maybe_silence_nan(r);
7824     }
7825     return float16_val(r);
7826 }
7827
7828 #define float32_two make_float32(0x40000000)
7829 #define float32_three make_float32(0x40400000)
7830 #define float32_one_point_five make_float32(0x3fc00000)
7831
7832 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
7833 {
7834     float_status *s = &env->vfp.standard_fp_status;
7835     if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
7836         (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
7837         if (!(float32_is_zero(a) || float32_is_zero(b))) {
7838             float_raise(float_flag_input_denormal, s);
7839         }
7840         return float32_two;
7841     }
7842     return float32_sub(float32_two, float32_mul(a, b, s), s);
7843 }
7844
7845 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
7846 {
7847     float_status *s = &env->vfp.standard_fp_status;
7848     float32 product;
7849     if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
7850         (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
7851         if (!(float32_is_zero(a) || float32_is_zero(b))) {
7852             float_raise(float_flag_input_denormal, s);
7853         }
7854         return float32_one_point_five;
7855     }
7856     product = float32_mul(a, b, s);
7857     return float32_div(float32_sub(float32_three, product, s), float32_two, s);
7858 }
7859
7860 /* NEON helpers.  */
7861
7862 /* Constants 256 and 512 are used in some helpers; we avoid relying on
7863  * int->float conversions at run-time.  */
7864 #define float64_256 make_float64(0x4070000000000000LL)
7865 #define float64_512 make_float64(0x4080000000000000LL)
7866 #define float32_maxnorm make_float32(0x7f7fffff)
7867 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
7868
7869 /* Reciprocal functions
7870  *
7871  * The algorithm that must be used to calculate the estimate
7872  * is specified by the ARM ARM, see FPRecipEstimate()
7873  */
7874
7875 static float64 recip_estimate(float64 a, float_status *real_fp_status)
7876 {
7877     /* These calculations mustn't set any fp exception flags,
7878      * so we use a local copy of the fp_status.
7879      */
7880     float_status dummy_status = *real_fp_status;
7881     float_status *s = &dummy_status;
7882     /* q = (int)(a * 512.0) */
7883     float64 q = float64_mul(float64_512, a, s);
7884     int64_t q_int = float64_to_int64_round_to_zero(q, s);
7885
7886     /* r = 1.0 / (((double)q + 0.5) / 512.0) */
7887     q = int64_to_float64(q_int, s);
7888     q = float64_add(q, float64_half, s);
7889     q = float64_div(q, float64_512, s);
7890     q = float64_div(float64_one, q, s);
7891
7892     /* s = (int)(256.0 * r + 0.5) */
7893     q = float64_mul(q, float64_256, s);
7894     q = float64_add(q, float64_half, s);
7895     q_int = float64_to_int64_round_to_zero(q, s);
7896
7897     /* return (double)s / 256.0 */
7898     return float64_div(int64_to_float64(q_int, s), float64_256, s);
7899 }
7900
7901 /* Common wrapper to call recip_estimate */
7902 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
7903 {
7904     uint64_t val64 = float64_val(num);
7905     uint64_t frac = extract64(val64, 0, 52);
7906     int64_t exp = extract64(val64, 52, 11);
7907     uint64_t sbit;
7908     float64 scaled, estimate;
7909
7910     /* Generate the scaled number for the estimate function */
7911     if (exp == 0) {
7912         if (extract64(frac, 51, 1) == 0) {
7913             exp = -1;
7914             frac = extract64(frac, 0, 50) << 2;
7915         } else {
7916             frac = extract64(frac, 0, 51) << 1;
7917         }
7918     }
7919
7920     /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
7921     scaled = make_float64((0x3feULL << 52)
7922                           | extract64(frac, 44, 8) << 44);
7923
7924     estimate = recip_estimate(scaled, fpst);
7925
7926     /* Build new result */
7927     val64 = float64_val(estimate);
7928     sbit = 0x8000000000000000ULL & val64;
7929     exp = off - exp;
7930     frac = extract64(val64, 0, 52);
7931
7932     if (exp == 0) {
7933         frac = 1ULL << 51 | extract64(frac, 1, 51);
7934     } else if (exp == -1) {
7935         frac = 1ULL << 50 | extract64(frac, 2, 50);
7936         exp = 0;
7937     }
7938
7939     return make_float64(sbit | (exp << 52) | frac);
7940 }
7941
7942 static bool round_to_inf(float_status *fpst, bool sign_bit)
7943 {
7944     switch (fpst->float_rounding_mode) {
7945     case float_round_nearest_even: /* Round to Nearest */
7946         return true;
7947     case float_round_up: /* Round to +Inf */
7948         return !sign_bit;
7949     case float_round_down: /* Round to -Inf */
7950         return sign_bit;
7951     case float_round_to_zero: /* Round to Zero */
7952         return false;
7953     }
7954
7955     g_assert_not_reached();
7956 }
7957
7958 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
7959 {
7960     float_status *fpst = fpstp;
7961     float32 f32 = float32_squash_input_denormal(input, fpst);
7962     uint32_t f32_val = float32_val(f32);
7963     uint32_t f32_sbit = 0x80000000ULL & f32_val;
7964     int32_t f32_exp = extract32(f32_val, 23, 8);
7965     uint32_t f32_frac = extract32(f32_val, 0, 23);
7966     float64 f64, r64;
7967     uint64_t r64_val;
7968     int64_t r64_exp;
7969     uint64_t r64_frac;
7970
7971     if (float32_is_any_nan(f32)) {
7972         float32 nan = f32;
7973         if (float32_is_signaling_nan(f32)) {
7974             float_raise(float_flag_invalid, fpst);
7975             nan = float32_maybe_silence_nan(f32);
7976         }
7977         if (fpst->default_nan_mode) {
7978             nan =  float32_default_nan;
7979         }
7980         return nan;
7981     } else if (float32_is_infinity(f32)) {
7982         return float32_set_sign(float32_zero, float32_is_neg(f32));
7983     } else if (float32_is_zero(f32)) {
7984         float_raise(float_flag_divbyzero, fpst);
7985         return float32_set_sign(float32_infinity, float32_is_neg(f32));
7986     } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
7987         /* Abs(value) < 2.0^-128 */
7988         float_raise(float_flag_overflow | float_flag_inexact, fpst);
7989         if (round_to_inf(fpst, f32_sbit)) {
7990             return float32_set_sign(float32_infinity, float32_is_neg(f32));
7991         } else {
7992             return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
7993         }
7994     } else if (f32_exp >= 253 && fpst->flush_to_zero) {
7995         float_raise(float_flag_underflow, fpst);
7996         return float32_set_sign(float32_zero, float32_is_neg(f32));
7997     }
7998
7999
8000     f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
8001     r64 = call_recip_estimate(f64, 253, fpst);
8002     r64_val = float64_val(r64);
8003     r64_exp = extract64(r64_val, 52, 11);
8004     r64_frac = extract64(r64_val, 0, 52);
8005
8006     /* result = sign : result_exp<7:0> : fraction<51:29>; */
8007     return make_float32(f32_sbit |
8008                         (r64_exp & 0xff) << 23 |
8009                         extract64(r64_frac, 29, 24));
8010 }
8011
8012 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
8013 {
8014     float_status *fpst = fpstp;
8015     float64 f64 = float64_squash_input_denormal(input, fpst);
8016     uint64_t f64_val = float64_val(f64);
8017     uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
8018     int64_t f64_exp = extract64(f64_val, 52, 11);
8019     float64 r64;
8020     uint64_t r64_val;
8021     int64_t r64_exp;
8022     uint64_t r64_frac;
8023
8024     /* Deal with any special cases */
8025     if (float64_is_any_nan(f64)) {
8026         float64 nan = f64;
8027         if (float64_is_signaling_nan(f64)) {
8028             float_raise(float_flag_invalid, fpst);
8029             nan = float64_maybe_silence_nan(f64);
8030         }
8031         if (fpst->default_nan_mode) {
8032             nan =  float64_default_nan;
8033         }
8034         return nan;
8035     } else if (float64_is_infinity(f64)) {
8036         return float64_set_sign(float64_zero, float64_is_neg(f64));
8037     } else if (float64_is_zero(f64)) {
8038         float_raise(float_flag_divbyzero, fpst);
8039         return float64_set_sign(float64_infinity, float64_is_neg(f64));
8040     } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
8041         /* Abs(value) < 2.0^-1024 */
8042         float_raise(float_flag_overflow | float_flag_inexact, fpst);
8043         if (round_to_inf(fpst, f64_sbit)) {
8044             return float64_set_sign(float64_infinity, float64_is_neg(f64));
8045         } else {
8046             return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
8047         }
8048     } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
8049         float_raise(float_flag_underflow, fpst);
8050         return float64_set_sign(float64_zero, float64_is_neg(f64));
8051     }
8052
8053     r64 = call_recip_estimate(f64, 2045, fpst);
8054     r64_val = float64_val(r64);
8055     r64_exp = extract64(r64_val, 52, 11);
8056     r64_frac = extract64(r64_val, 0, 52);
8057
8058     /* result = sign : result_exp<10:0> : fraction<51:0> */
8059     return make_float64(f64_sbit |
8060                         ((r64_exp & 0x7ff) << 52) |
8061                         r64_frac);
8062 }
8063
8064 /* The algorithm that must be used to calculate the estimate
8065  * is specified by the ARM ARM.
8066  */
8067 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
8068 {
8069     /* These calculations mustn't set any fp exception flags,
8070      * so we use a local copy of the fp_status.
8071      */
8072     float_status dummy_status = *real_fp_status;
8073     float_status *s = &dummy_status;
8074     float64 q;
8075     int64_t q_int;
8076
8077     if (float64_lt(a, float64_half, s)) {
8078         /* range 0.25 <= a < 0.5 */
8079
8080         /* a in units of 1/512 rounded down */
8081         /* q0 = (int)(a * 512.0);  */
8082         q = float64_mul(float64_512, a, s);
8083         q_int = float64_to_int64_round_to_zero(q, s);
8084
8085         /* reciprocal root r */
8086         /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0);  */
8087         q = int64_to_float64(q_int, s);
8088         q = float64_add(q, float64_half, s);
8089         q = float64_div(q, float64_512, s);
8090         q = float64_sqrt(q, s);
8091         q = float64_div(float64_one, q, s);
8092     } else {
8093         /* range 0.5 <= a < 1.0 */
8094
8095         /* a in units of 1/256 rounded down */
8096         /* q1 = (int)(a * 256.0); */
8097         q = float64_mul(float64_256, a, s);
8098         int64_t q_int = float64_to_int64_round_to_zero(q, s);
8099
8100         /* reciprocal root r */
8101         /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
8102         q = int64_to_float64(q_int, s);
8103         q = float64_add(q, float64_half, s);
8104         q = float64_div(q, float64_256, s);
8105         q = float64_sqrt(q, s);
8106         q = float64_div(float64_one, q, s);
8107     }
8108     /* r in units of 1/256 rounded to nearest */
8109     /* s = (int)(256.0 * r + 0.5); */
8110
8111     q = float64_mul(q, float64_256,s );
8112     q = float64_add(q, float64_half, s);
8113     q_int = float64_to_int64_round_to_zero(q, s);
8114
8115     /* return (double)s / 256.0;*/
8116     return float64_div(int64_to_float64(q_int, s), float64_256, s);
8117 }
8118
8119 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
8120 {
8121     float_status *s = fpstp;
8122     float32 f32 = float32_squash_input_denormal(input, s);
8123     uint32_t val = float32_val(f32);
8124     uint32_t f32_sbit = 0x80000000 & val;
8125     int32_t f32_exp = extract32(val, 23, 8);
8126     uint32_t f32_frac = extract32(val, 0, 23);
8127     uint64_t f64_frac;
8128     uint64_t val64;
8129     int result_exp;
8130     float64 f64;
8131
8132     if (float32_is_any_nan(f32)) {
8133         float32 nan = f32;
8134         if (float32_is_signaling_nan(f32)) {
8135             float_raise(float_flag_invalid, s);
8136             nan = float32_maybe_silence_nan(f32);
8137         }
8138         if (s->default_nan_mode) {
8139             nan =  float32_default_nan;
8140         }
8141         return nan;
8142     } else if (float32_is_zero(f32)) {
8143         float_raise(float_flag_divbyzero, s);
8144         return float32_set_sign(float32_infinity, float32_is_neg(f32));
8145     } else if (float32_is_neg(f32)) {
8146         float_raise(float_flag_invalid, s);
8147         return float32_default_nan;
8148     } else if (float32_is_infinity(f32)) {
8149         return float32_zero;
8150     }
8151
8152     /* Scale and normalize to a double-precision value between 0.25 and 1.0,
8153      * preserving the parity of the exponent.  */
8154
8155     f64_frac = ((uint64_t) f32_frac) << 29;
8156     if (f32_exp == 0) {
8157         while (extract64(f64_frac, 51, 1) == 0) {
8158             f64_frac = f64_frac << 1;
8159             f32_exp = f32_exp-1;
8160         }
8161         f64_frac = extract64(f64_frac, 0, 51) << 1;
8162     }
8163
8164     if (extract64(f32_exp, 0, 1) == 0) {
8165         f64 = make_float64(((uint64_t) f32_sbit) << 32
8166                            | (0x3feULL << 52)
8167                            | f64_frac);
8168     } else {
8169         f64 = make_float64(((uint64_t) f32_sbit) << 32
8170                            | (0x3fdULL << 52)
8171                            | f64_frac);
8172     }
8173
8174     result_exp = (380 - f32_exp) / 2;
8175
8176     f64 = recip_sqrt_estimate(f64, s);
8177
8178     val64 = float64_val(f64);
8179
8180     val = ((result_exp & 0xff) << 23)
8181         | ((val64 >> 29)  & 0x7fffff);
8182     return make_float32(val);
8183 }
8184
8185 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
8186 {
8187     float_status *s = fpstp;
8188     float64 f64 = float64_squash_input_denormal(input, s);
8189     uint64_t val = float64_val(f64);
8190     uint64_t f64_sbit = 0x8000000000000000ULL & val;
8191     int64_t f64_exp = extract64(val, 52, 11);
8192     uint64_t f64_frac = extract64(val, 0, 52);
8193     int64_t result_exp;
8194     uint64_t result_frac;
8195
8196     if (float64_is_any_nan(f64)) {
8197         float64 nan = f64;
8198         if (float64_is_signaling_nan(f64)) {
8199             float_raise(float_flag_invalid, s);
8200             nan = float64_maybe_silence_nan(f64);
8201         }
8202         if (s->default_nan_mode) {
8203             nan =  float64_default_nan;
8204         }
8205         return nan;
8206     } else if (float64_is_zero(f64)) {
8207         float_raise(float_flag_divbyzero, s);
8208         return float64_set_sign(float64_infinity, float64_is_neg(f64));
8209     } else if (float64_is_neg(f64)) {
8210         float_raise(float_flag_invalid, s);
8211         return float64_default_nan;
8212     } else if (float64_is_infinity(f64)) {
8213         return float64_zero;
8214     }
8215
8216     /* Scale and normalize to a double-precision value between 0.25 and 1.0,
8217      * preserving the parity of the exponent.  */
8218
8219     if (f64_exp == 0) {
8220         while (extract64(f64_frac, 51, 1) == 0) {
8221             f64_frac = f64_frac << 1;
8222             f64_exp = f64_exp - 1;
8223         }
8224         f64_frac = extract64(f64_frac, 0, 51) << 1;
8225     }
8226
8227     if (extract64(f64_exp, 0, 1) == 0) {
8228         f64 = make_float64(f64_sbit
8229                            | (0x3feULL << 52)
8230                            | f64_frac);
8231     } else {
8232         f64 = make_float64(f64_sbit
8233                            | (0x3fdULL << 52)
8234                            | f64_frac);
8235     }
8236
8237     result_exp = (3068 - f64_exp) / 2;
8238
8239     f64 = recip_sqrt_estimate(f64, s);
8240
8241     result_frac = extract64(float64_val(f64), 0, 52);
8242
8243     return make_float64(f64_sbit |
8244                         ((result_exp & 0x7ff) << 52) |
8245                         result_frac);
8246 }
8247
8248 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
8249 {
8250     float_status *s = fpstp;
8251     float64 f64;
8252
8253     if ((a & 0x80000000) == 0) {
8254         return 0xffffffff;
8255     }
8256
8257     f64 = make_float64((0x3feULL << 52)
8258                        | ((int64_t)(a & 0x7fffffff) << 21));
8259
8260     f64 = recip_estimate(f64, s);
8261
8262     return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
8263 }
8264
8265 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
8266 {
8267     float_status *fpst = fpstp;
8268     float64 f64;
8269
8270     if ((a & 0xc0000000) == 0) {
8271         return 0xffffffff;
8272     }
8273
8274     if (a & 0x80000000) {
8275         f64 = make_float64((0x3feULL << 52)
8276                            | ((uint64_t)(a & 0x7fffffff) << 21));
8277     } else { /* bits 31-30 == '01' */
8278         f64 = make_float64((0x3fdULL << 52)
8279                            | ((uint64_t)(a & 0x3fffffff) << 22));
8280     }
8281
8282     f64 = recip_sqrt_estimate(f64, fpst);
8283
8284     return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
8285 }
8286
8287 /* VFPv4 fused multiply-accumulate */
8288 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
8289 {
8290     float_status *fpst = fpstp;
8291     return float32_muladd(a, b, c, 0, fpst);
8292 }
8293
8294 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
8295 {
8296     float_status *fpst = fpstp;
8297     return float64_muladd(a, b, c, 0, fpst);
8298 }
8299
8300 /* ARMv8 round to integral */
8301 float32 HELPER(rints_exact)(float32 x, void *fp_status)
8302 {
8303     return float32_round_to_int(x, fp_status);
8304 }
8305
8306 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
8307 {
8308     return float64_round_to_int(x, fp_status);
8309 }
8310
8311 float32 HELPER(rints)(float32 x, void *fp_status)
8312 {
8313     int old_flags = get_float_exception_flags(fp_status), new_flags;
8314     float32 ret;
8315
8316     ret = float32_round_to_int(x, fp_status);
8317
8318     /* Suppress any inexact exceptions the conversion produced */
8319     if (!(old_flags & float_flag_inexact)) {
8320         new_flags = get_float_exception_flags(fp_status);
8321         set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
8322     }
8323
8324     return ret;
8325 }
8326
8327 float64 HELPER(rintd)(float64 x, void *fp_status)
8328 {
8329     int old_flags = get_float_exception_flags(fp_status), new_flags;
8330     float64 ret;
8331
8332     ret = float64_round_to_int(x, fp_status);
8333
8334     new_flags = get_float_exception_flags(fp_status);
8335
8336     /* Suppress any inexact exceptions the conversion produced */
8337     if (!(old_flags & float_flag_inexact)) {
8338         new_flags = get_float_exception_flags(fp_status);
8339         set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
8340     }
8341
8342     return ret;
8343 }
8344
8345 /* Convert ARM rounding mode to softfloat */
8346 int arm_rmode_to_sf(int rmode)
8347 {
8348     switch (rmode) {
8349     case FPROUNDING_TIEAWAY:
8350         rmode = float_round_ties_away;
8351         break;
8352     case FPROUNDING_ODD:
8353         /* FIXME: add support for TIEAWAY and ODD */
8354         qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
8355                       rmode);
8356     case FPROUNDING_TIEEVEN:
8357     default:
8358         rmode = float_round_nearest_even;
8359         break;
8360     case FPROUNDING_POSINF:
8361         rmode = float_round_up;
8362         break;
8363     case FPROUNDING_NEGINF:
8364         rmode = float_round_down;
8365         break;
8366     case FPROUNDING_ZERO:
8367         rmode = float_round_to_zero;
8368         break;
8369     }
8370     return rmode;
8371 }
8372
8373 /* CRC helpers.
8374  * The upper bytes of val (above the number specified by 'bytes') must have
8375  * been zeroed out by the caller.
8376  */
8377 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
8378 {
8379     uint8_t buf[4];
8380
8381     stl_le_p(buf, val);
8382
8383     /* zlib crc32 converts the accumulator and output to one's complement.  */
8384     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
8385 }
8386
8387 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
8388 {
8389     uint8_t buf[4];
8390
8391     stl_le_p(buf, val);
8392
8393     /* Linux crc32c converts the output to one's complement.  */
8394     return crc32c(acc, buf, bytes) ^ 0xffffffff;
8395 }
This page took 0.478653 seconds and 4 git commands to generate.