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