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