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