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