]> Git Repo - qemu.git/blob - target-arm/helper.c
6d9026d04ce872697b3d9e14eac94bf7fdd106a6
[qemu.git] / target-arm / helper.c
1 #include "cpu.h"
2 #include "exec/gdbstub.h"
3 #include "helper.h"
4 #include "qemu/host-utils.h"
5 #include "sysemu/sysemu.h"
6 #include "qemu/bitops.h"
7
8 #ifndef CONFIG_USER_ONLY
9 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
10                                 int access_type, int is_user,
11                                 hwaddr *phys_ptr, int *prot,
12                                 target_ulong *page_size);
13 #endif
14
15 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
16 {
17     int nregs;
18
19     /* VFP data registers are always little-endian.  */
20     nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
21     if (reg < nregs) {
22         stfq_le_p(buf, env->vfp.regs[reg]);
23         return 8;
24     }
25     if (arm_feature(env, ARM_FEATURE_NEON)) {
26         /* Aliases for Q regs.  */
27         nregs += 16;
28         if (reg < nregs) {
29             stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
30             stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
31             return 16;
32         }
33     }
34     switch (reg - nregs) {
35     case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
36     case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
37     case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
38     }
39     return 0;
40 }
41
42 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
43 {
44     int nregs;
45
46     nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
47     if (reg < nregs) {
48         env->vfp.regs[reg] = ldfq_le_p(buf);
49         return 8;
50     }
51     if (arm_feature(env, ARM_FEATURE_NEON)) {
52         nregs += 16;
53         if (reg < nregs) {
54             env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
55             env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
56             return 16;
57         }
58     }
59     switch (reg - nregs) {
60     case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
61     case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
62     case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
63     }
64     return 0;
65 }
66
67 static int raw_read(CPUARMState *env, const ARMCPRegInfo *ri,
68                     uint64_t *value)
69 {
70     *value = CPREG_FIELD32(env, ri);
71     return 0;
72 }
73
74 static int raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
75                      uint64_t value)
76 {
77     CPREG_FIELD32(env, ri) = value;
78     return 0;
79 }
80
81 static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
82                             uint64_t *v)
83 {
84     /* Raw read of a coprocessor register (as needed for migration, etc)
85      * return true on success, false if the read is impossible for some reason.
86      */
87     if (ri->type & ARM_CP_CONST) {
88         *v = ri->resetvalue;
89     } else if (ri->raw_readfn) {
90         return (ri->raw_readfn(env, ri, v) == 0);
91     } else if (ri->readfn) {
92         return (ri->readfn(env, ri, v) == 0);
93     } else {
94         if (ri->type & ARM_CP_64BIT) {
95             *v = CPREG_FIELD64(env, ri);
96         } else {
97             *v = CPREG_FIELD32(env, ri);
98         }
99     }
100     return true;
101 }
102
103 static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
104                              int64_t v)
105 {
106     /* Raw write of a coprocessor register (as needed for migration, etc).
107      * Return true on success, false if the write is impossible for some reason.
108      * Note that constant registers are treated as write-ignored; the
109      * caller should check for success by whether a readback gives the
110      * value written.
111      */
112     if (ri->type & ARM_CP_CONST) {
113         return true;
114     } else if (ri->raw_writefn) {
115         return (ri->raw_writefn(env, ri, v) == 0);
116     } else if (ri->writefn) {
117         return (ri->writefn(env, ri, v) == 0);
118     } else {
119         if (ri->type & ARM_CP_64BIT) {
120             CPREG_FIELD64(env, ri) = v;
121         } else {
122             CPREG_FIELD32(env, ri) = v;
123         }
124     }
125     return true;
126 }
127
128 bool write_cpustate_to_list(ARMCPU *cpu)
129 {
130     /* Write the coprocessor state from cpu->env to the (index,value) list. */
131     int i;
132     bool ok = true;
133
134     for (i = 0; i < cpu->cpreg_array_len; i++) {
135         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
136         const ARMCPRegInfo *ri;
137         uint64_t v;
138         ri = get_arm_cp_reginfo(cpu, regidx);
139         if (!ri) {
140             ok = false;
141             continue;
142         }
143         if (ri->type & ARM_CP_NO_MIGRATE) {
144             continue;
145         }
146         if (!read_raw_cp_reg(&cpu->env, ri, &v)) {
147             ok = false;
148             continue;
149         }
150         cpu->cpreg_values[i] = v;
151     }
152     return ok;
153 }
154
155 bool write_list_to_cpustate(ARMCPU *cpu)
156 {
157     int i;
158     bool ok = true;
159
160     for (i = 0; i < cpu->cpreg_array_len; i++) {
161         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
162         uint64_t v = cpu->cpreg_values[i];
163         uint64_t readback;
164         const ARMCPRegInfo *ri;
165
166         ri = get_arm_cp_reginfo(cpu, regidx);
167         if (!ri) {
168             ok = false;
169             continue;
170         }
171         if (ri->type & ARM_CP_NO_MIGRATE) {
172             continue;
173         }
174         /* Write value and confirm it reads back as written
175          * (to catch read-only registers and partially read-only
176          * registers where the incoming migration value doesn't match)
177          */
178         if (!write_raw_cp_reg(&cpu->env, ri, v) ||
179             !read_raw_cp_reg(&cpu->env, ri, &readback) ||
180             readback != v) {
181             ok = false;
182         }
183     }
184     return ok;
185 }
186
187 static void add_cpreg_to_list(gpointer key, gpointer opaque)
188 {
189     ARMCPU *cpu = opaque;
190     uint64_t regidx;
191     const ARMCPRegInfo *ri;
192
193     regidx = *(uint32_t *)key;
194     ri = get_arm_cp_reginfo(cpu, regidx);
195
196     if (!(ri->type & ARM_CP_NO_MIGRATE)) {
197         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
198         /* The value array need not be initialized at this point */
199         cpu->cpreg_array_len++;
200     }
201 }
202
203 static void count_cpreg(gpointer key, gpointer opaque)
204 {
205     ARMCPU *cpu = opaque;
206     uint64_t regidx;
207     const ARMCPRegInfo *ri;
208
209     regidx = *(uint32_t *)key;
210     ri = get_arm_cp_reginfo(cpu, regidx);
211
212     if (!(ri->type & ARM_CP_NO_MIGRATE)) {
213         cpu->cpreg_array_len++;
214     }
215 }
216
217 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
218 {
219     uint32_t aidx = *(uint32_t *)a;
220     uint32_t bidx = *(uint32_t *)b;
221
222     return aidx - bidx;
223 }
224
225 static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
226 {
227     GList **plist = udata;
228
229     *plist = g_list_prepend(*plist, key);
230 }
231
232 void init_cpreg_list(ARMCPU *cpu)
233 {
234     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
235      * Note that we require cpreg_tuples[] to be sorted by key ID.
236      */
237     GList *keys = NULL;
238     int arraylen;
239
240     g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
241
242     keys = g_list_sort(keys, cpreg_key_compare);
243
244     cpu->cpreg_array_len = 0;
245
246     g_list_foreach(keys, count_cpreg, cpu);
247
248     arraylen = cpu->cpreg_array_len;
249     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
250     cpu->cpreg_values = g_new(uint64_t, arraylen);
251     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
252     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
253     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
254     cpu->cpreg_array_len = 0;
255
256     g_list_foreach(keys, add_cpreg_to_list, cpu);
257
258     assert(cpu->cpreg_array_len == arraylen);
259
260     g_list_free(keys);
261 }
262
263 static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
264 {
265     env->cp15.c3 = value;
266     tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
267     return 0;
268 }
269
270 static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
271 {
272     if (env->cp15.c13_fcse != value) {
273         /* Unlike real hardware the qemu TLB uses virtual addresses,
274          * not modified virtual addresses, so this causes a TLB flush.
275          */
276         tlb_flush(env, 1);
277         env->cp15.c13_fcse = value;
278     }
279     return 0;
280 }
281 static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
282                             uint64_t value)
283 {
284     if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
285         /* For VMSA (when not using the LPAE long descriptor page table
286          * format) this register includes the ASID, so do a TLB flush.
287          * For PMSA it is purely a process ID and no action is needed.
288          */
289         tlb_flush(env, 1);
290     }
291     env->cp15.c13_context = value;
292     return 0;
293 }
294
295 static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
296                          uint64_t value)
297 {
298     /* Invalidate all (TLBIALL) */
299     tlb_flush(env, 1);
300     return 0;
301 }
302
303 static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
304                          uint64_t value)
305 {
306     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
307     tlb_flush_page(env, value & TARGET_PAGE_MASK);
308     return 0;
309 }
310
311 static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
312                           uint64_t value)
313 {
314     /* Invalidate by ASID (TLBIASID) */
315     tlb_flush(env, value == 0);
316     return 0;
317 }
318
319 static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
320                           uint64_t value)
321 {
322     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
323     tlb_flush_page(env, value & TARGET_PAGE_MASK);
324     return 0;
325 }
326
327 static const ARMCPRegInfo cp_reginfo[] = {
328     /* DBGDIDR: just RAZ. In particular this means the "debug architecture
329      * version" bits will read as a reserved value, which should cause
330      * Linux to not try to use the debug hardware.
331      */
332     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
333       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
334     /* MMU Domain access control / MPU write buffer control */
335     { .name = "DACR", .cp = 15,
336       .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
337       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
338       .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
339     { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
340       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
341       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
342     { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
343       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
344       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
345     /* ??? This covers not just the impdef TLB lockdown registers but also
346      * some v7VMSA registers relating to TEX remap, so it is overly broad.
347      */
348     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
349       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
350     /* MMU TLB control. Note that the wildcarding means we cover not just
351      * the unified TLB ops but also the dside/iside/inner-shareable variants.
352      */
353     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
354       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
355       .type = ARM_CP_NO_MIGRATE },
356     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
357       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
358       .type = ARM_CP_NO_MIGRATE },
359     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
360       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
361       .type = ARM_CP_NO_MIGRATE },
362     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
363       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
364       .type = ARM_CP_NO_MIGRATE },
365     /* Cache maintenance ops; some of this space may be overridden later. */
366     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
367       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
368       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
369     REGINFO_SENTINEL
370 };
371
372 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
373     /* Not all pre-v6 cores implemented this WFI, so this is slightly
374      * over-broad.
375      */
376     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
377       .access = PL1_W, .type = ARM_CP_WFI },
378     REGINFO_SENTINEL
379 };
380
381 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
382     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
383      * is UNPREDICTABLE; we choose to NOP as most implementations do).
384      */
385     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
386       .access = PL1_W, .type = ARM_CP_WFI },
387     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
388      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
389      * OMAPCP will override this space.
390      */
391     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
392       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
393       .resetvalue = 0 },
394     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
395       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
396       .resetvalue = 0 },
397     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
398     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
399       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
400       .resetvalue = 0 },
401     REGINFO_SENTINEL
402 };
403
404 static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
405 {
406     if (env->cp15.c1_coproc != value) {
407         env->cp15.c1_coproc = value;
408         /* ??? Is this safe when called from within a TB?  */
409         tb_flush(env);
410     }
411     return 0;
412 }
413
414 static const ARMCPRegInfo v6_cp_reginfo[] = {
415     /* prefetch by MVA in v6, NOP in v7 */
416     { .name = "MVA_prefetch",
417       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
418       .access = PL1_W, .type = ARM_CP_NOP },
419     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
420       .access = PL0_W, .type = ARM_CP_NOP },
421     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
422       .access = PL0_W, .type = ARM_CP_NOP },
423     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
424       .access = PL0_W, .type = ARM_CP_NOP },
425     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
426       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
427       .resetvalue = 0, },
428     /* Watchpoint Fault Address Register : should actually only be present
429      * for 1136, 1176, 11MPCore.
430      */
431     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
432       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
433     { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
434       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
435       .resetvalue = 0, .writefn = cpacr_write },
436     REGINFO_SENTINEL
437 };
438
439
440 static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
441                       uint64_t *value)
442 {
443     /* Generic performance monitor register read function for where
444      * user access may be allowed by PMUSERENR.
445      */
446     if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
447         return EXCP_UDEF;
448     }
449     *value = CPREG_FIELD32(env, ri);
450     return 0;
451 }
452
453 static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
454                       uint64_t value)
455 {
456     if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
457         return EXCP_UDEF;
458     }
459     /* only the DP, X, D and E bits are writable */
460     env->cp15.c9_pmcr &= ~0x39;
461     env->cp15.c9_pmcr |= (value & 0x39);
462     return 0;
463 }
464
465 static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
466                             uint64_t value)
467 {
468     if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
469         return EXCP_UDEF;
470     }
471     value &= (1 << 31);
472     env->cp15.c9_pmcnten |= value;
473     return 0;
474 }
475
476 static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
477                             uint64_t value)
478 {
479     if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
480         return EXCP_UDEF;
481     }
482     value &= (1 << 31);
483     env->cp15.c9_pmcnten &= ~value;
484     return 0;
485 }
486
487 static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
488                         uint64_t value)
489 {
490     if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
491         return EXCP_UDEF;
492     }
493     env->cp15.c9_pmovsr &= ~value;
494     return 0;
495 }
496
497 static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
498                             uint64_t value)
499 {
500     if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
501         return EXCP_UDEF;
502     }
503     env->cp15.c9_pmxevtyper = value & 0xff;
504     return 0;
505 }
506
507 static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
508                             uint64_t value)
509 {
510     env->cp15.c9_pmuserenr = value & 1;
511     return 0;
512 }
513
514 static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
515                             uint64_t value)
516 {
517     /* We have no event counters so only the C bit can be changed */
518     value &= (1 << 31);
519     env->cp15.c9_pminten |= value;
520     return 0;
521 }
522
523 static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
524                             uint64_t value)
525 {
526     value &= (1 << 31);
527     env->cp15.c9_pminten &= ~value;
528     return 0;
529 }
530
531 static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
532                        uint64_t *value)
533 {
534     ARMCPU *cpu = arm_env_get_cpu(env);
535     *value = cpu->ccsidr[env->cp15.c0_cssel];
536     return 0;
537 }
538
539 static int csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
540                         uint64_t value)
541 {
542     env->cp15.c0_cssel = value & 0xf;
543     return 0;
544 }
545
546 static const ARMCPRegInfo v7_cp_reginfo[] = {
547     /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
548      * debug components
549      */
550     { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
551       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
552     { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
553       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
554     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
555     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
556       .access = PL1_W, .type = ARM_CP_NOP },
557     /* Performance monitors are implementation defined in v7,
558      * but with an ARM recommended set of registers, which we
559      * follow (although we don't actually implement any counters)
560      *
561      * Performance registers fall into three categories:
562      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
563      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
564      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
565      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
566      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
567      */
568     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
569       .access = PL0_RW, .resetvalue = 0,
570       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
571       .readfn = pmreg_read, .writefn = pmcntenset_write,
572       .raw_readfn = raw_read, .raw_writefn = raw_write },
573     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
574       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
575       .readfn = pmreg_read, .writefn = pmcntenclr_write,
576       .type = ARM_CP_NO_MIGRATE },
577     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
578       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
579       .readfn = pmreg_read, .writefn = pmovsr_write,
580       .raw_readfn = raw_read, .raw_writefn = raw_write },
581     /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
582      * respect PMUSERENR.
583      */
584     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
585       .access = PL0_W, .type = ARM_CP_NOP },
586     /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
587      * We choose to RAZ/WI. XXX should respect PMUSERENR.
588      */
589     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
590       .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
591     /* Unimplemented, RAZ/WI. XXX PMUSERENR */
592     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
593       .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
594     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
595       .access = PL0_RW,
596       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
597       .readfn = pmreg_read, .writefn = pmxevtyper_write,
598       .raw_readfn = raw_read, .raw_writefn = raw_write },
599     /* Unimplemented, RAZ/WI. XXX PMUSERENR */
600     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
601       .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
602     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
603       .access = PL0_R | PL1_RW,
604       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
605       .resetvalue = 0,
606       .writefn = pmuserenr_write, .raw_writefn = raw_write },
607     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
608       .access = PL1_RW,
609       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
610       .resetvalue = 0,
611       .writefn = pmintenset_write, .raw_writefn = raw_write },
612     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
613       .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
614       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
615       .resetvalue = 0, .writefn = pmintenclr_write, },
616     { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
617       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
618       .resetvalue = 0, },
619     { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
620       .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
621     { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
622       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
623       .writefn = csselr_write, .resetvalue = 0 },
624     /* Auxiliary ID register: this actually has an IMPDEF value but for now
625      * just RAZ for all cores:
626      */
627     { .name = "AIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 7,
628       .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
629     REGINFO_SENTINEL
630 };
631
632 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
633 {
634     value &= 1;
635     env->teecr = value;
636     return 0;
637 }
638
639 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
640                        uint64_t *value)
641 {
642     /* This is a helper function because the user access rights
643      * depend on the value of the TEECR.
644      */
645     if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
646         return EXCP_UDEF;
647     }
648     *value = env->teehbr;
649     return 0;
650 }
651
652 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
653                         uint64_t value)
654 {
655     if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
656         return EXCP_UDEF;
657     }
658     env->teehbr = value;
659     return 0;
660 }
661
662 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
663     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
664       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
665       .resetvalue = 0,
666       .writefn = teecr_write },
667     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
668       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
669       .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write,
670       .readfn = teehbr_read, .writefn = teehbr_write },
671     REGINFO_SENTINEL
672 };
673
674 static const ARMCPRegInfo v6k_cp_reginfo[] = {
675     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
676       .access = PL0_RW,
677       .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
678       .resetvalue = 0 },
679     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
680       .access = PL0_R|PL1_W,
681       .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
682       .resetvalue = 0 },
683     { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
684       .access = PL1_RW,
685       .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
686       .resetvalue = 0 },
687     REGINFO_SENTINEL
688 };
689
690 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
691     /* Dummy implementation: RAZ/WI the whole crn=14 space */
692     { .name = "GENERIC_TIMER", .cp = 15, .crn = 14,
693       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
694       .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
695       .resetvalue = 0 },
696     REGINFO_SENTINEL
697 };
698
699 static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
700 {
701     if (arm_feature(env, ARM_FEATURE_LPAE)) {
702         env->cp15.c7_par = value;
703     } else if (arm_feature(env, ARM_FEATURE_V7)) {
704         env->cp15.c7_par = value & 0xfffff6ff;
705     } else {
706         env->cp15.c7_par = value & 0xfffff1ff;
707     }
708     return 0;
709 }
710
711 #ifndef CONFIG_USER_ONLY
712 /* get_phys_addr() isn't present for user-mode-only targets */
713
714 /* Return true if extended addresses are enabled, ie this is an
715  * LPAE implementation and we are using the long-descriptor translation
716  * table format because the TTBCR EAE bit is set.
717  */
718 static inline bool extended_addresses_enabled(CPUARMState *env)
719 {
720     return arm_feature(env, ARM_FEATURE_LPAE)
721         && (env->cp15.c2_control & (1 << 31));
722 }
723
724 static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
725 {
726     hwaddr phys_addr;
727     target_ulong page_size;
728     int prot;
729     int ret, is_user = ri->opc2 & 2;
730     int access_type = ri->opc2 & 1;
731
732     if (ri->opc2 & 4) {
733         /* Other states are only available with TrustZone */
734         return EXCP_UDEF;
735     }
736     ret = get_phys_addr(env, value, access_type, is_user,
737                         &phys_addr, &prot, &page_size);
738     if (extended_addresses_enabled(env)) {
739         /* ret is a DFSR/IFSR value for the long descriptor
740          * translation table format, but with WnR always clear.
741          * Convert it to a 64-bit PAR.
742          */
743         uint64_t par64 = (1 << 11); /* LPAE bit always set */
744         if (ret == 0) {
745             par64 |= phys_addr & ~0xfffULL;
746             /* We don't set the ATTR or SH fields in the PAR. */
747         } else {
748             par64 |= 1; /* F */
749             par64 |= (ret & 0x3f) << 1; /* FS */
750             /* Note that S2WLK and FSTAGE are always zero, because we don't
751              * implement virtualization and therefore there can't be a stage 2
752              * fault.
753              */
754         }
755         env->cp15.c7_par = par64;
756         env->cp15.c7_par_hi = par64 >> 32;
757     } else {
758         /* ret is a DFSR/IFSR value for the short descriptor
759          * translation table format (with WnR always clear).
760          * Convert it to a 32-bit PAR.
761          */
762         if (ret == 0) {
763             /* We do not set any attribute bits in the PAR */
764             if (page_size == (1 << 24)
765                 && arm_feature(env, ARM_FEATURE_V7)) {
766                 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
767             } else {
768                 env->cp15.c7_par = phys_addr & 0xfffff000;
769             }
770         } else {
771             env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
772                 ((ret & (12 << 1)) >> 6) |
773                 ((ret & 0xf) << 1) | 1;
774         }
775         env->cp15.c7_par_hi = 0;
776     }
777     return 0;
778 }
779 #endif
780
781 static const ARMCPRegInfo vapa_cp_reginfo[] = {
782     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
783       .access = PL1_RW, .resetvalue = 0,
784       .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
785       .writefn = par_write },
786 #ifndef CONFIG_USER_ONLY
787     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
788       .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
789 #endif
790     REGINFO_SENTINEL
791 };
792
793 /* Return basic MPU access permission bits.  */
794 static uint32_t simple_mpu_ap_bits(uint32_t val)
795 {
796     uint32_t ret;
797     uint32_t mask;
798     int i;
799     ret = 0;
800     mask = 3;
801     for (i = 0; i < 16; i += 2) {
802         ret |= (val >> i) & mask;
803         mask <<= 2;
804     }
805     return ret;
806 }
807
808 /* Pad basic MPU access permission bits to extended format.  */
809 static uint32_t extended_mpu_ap_bits(uint32_t val)
810 {
811     uint32_t ret;
812     uint32_t mask;
813     int i;
814     ret = 0;
815     mask = 3;
816     for (i = 0; i < 16; i += 2) {
817         ret |= (val & mask) << i;
818         mask <<= 2;
819     }
820     return ret;
821 }
822
823 static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
824                                 uint64_t value)
825 {
826     env->cp15.c5_data = extended_mpu_ap_bits(value);
827     return 0;
828 }
829
830 static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
831                                uint64_t *value)
832 {
833     *value = simple_mpu_ap_bits(env->cp15.c5_data);
834     return 0;
835 }
836
837 static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
838                                 uint64_t value)
839 {
840     env->cp15.c5_insn = extended_mpu_ap_bits(value);
841     return 0;
842 }
843
844 static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
845                                uint64_t *value)
846 {
847     *value = simple_mpu_ap_bits(env->cp15.c5_insn);
848     return 0;
849 }
850
851 static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
852                             uint64_t *value)
853 {
854     if (ri->crm >= 8) {
855         return EXCP_UDEF;
856     }
857     *value = env->cp15.c6_region[ri->crm];
858     return 0;
859 }
860
861 static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
862                              uint64_t value)
863 {
864     if (ri->crm >= 8) {
865         return EXCP_UDEF;
866     }
867     env->cp15.c6_region[ri->crm] = value;
868     return 0;
869 }
870
871 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
872     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
873       .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
874       .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
875       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
876     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
877       .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
878       .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
879       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
880     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
881       .access = PL1_RW,
882       .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
883     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
884       .access = PL1_RW,
885       .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
886     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
887       .access = PL1_RW,
888       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
889     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
890       .access = PL1_RW,
891       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
892     /* Protection region base and size registers */
893     { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
894       .opc2 = CP_ANY, .access = PL1_RW,
895       .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
896     REGINFO_SENTINEL
897 };
898
899 static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
900                                 uint64_t value)
901 {
902     int maskshift = extract32(value, 0, 3);
903
904     if (arm_feature(env, ARM_FEATURE_LPAE)) {
905         value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
906     } else {
907         value &= 7;
908     }
909     /* Note that we always calculate c2_mask and c2_base_mask, but
910      * they are only used for short-descriptor tables (ie if EAE is 0);
911      * for long-descriptor tables the TTBCR fields are used differently
912      * and the c2_mask and c2_base_mask values are meaningless.
913      */
914     env->cp15.c2_control = value;
915     env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
916     env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
917     return 0;
918 }
919
920 static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
921                             uint64_t value)
922 {
923     if (arm_feature(env, ARM_FEATURE_LPAE)) {
924         /* With LPAE the TTBCR could result in a change of ASID
925          * via the TTBCR.A1 bit, so do a TLB flush.
926          */
927         tlb_flush(env, 1);
928     }
929     return vmsa_ttbcr_raw_write(env, ri, value);
930 }
931
932 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
933 {
934     env->cp15.c2_base_mask = 0xffffc000u;
935     env->cp15.c2_control = 0;
936     env->cp15.c2_mask = 0;
937 }
938
939 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
940     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
941       .access = PL1_RW,
942       .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
943     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
944       .access = PL1_RW,
945       .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
946     { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
947       .access = PL1_RW,
948       .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
949     { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
950       .access = PL1_RW,
951       .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, },
952     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
953       .access = PL1_RW, .writefn = vmsa_ttbcr_write,
954       .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
955       .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
956     { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
957       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
958       .resetvalue = 0, },
959     REGINFO_SENTINEL
960 };
961
962 static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
963                                uint64_t value)
964 {
965     env->cp15.c15_ticonfig = value & 0xe7;
966     /* The OS_TYPE bit in this register changes the reported CPUID! */
967     env->cp15.c0_cpuid = (value & (1 << 5)) ?
968         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
969     return 0;
970 }
971
972 static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
973                                uint64_t value)
974 {
975     env->cp15.c15_threadid = value & 0xffff;
976     return 0;
977 }
978
979 static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
980                           uint64_t value)
981 {
982     /* Wait-for-interrupt (deprecated) */
983     cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
984     return 0;
985 }
986
987 static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
988                                  uint64_t value)
989 {
990     /* On OMAP there are registers indicating the max/min index of dcache lines
991      * containing a dirty line; cache flush operations have to reset these.
992      */
993     env->cp15.c15_i_max = 0x000;
994     env->cp15.c15_i_min = 0xff0;
995     return 0;
996 }
997
998 static const ARMCPRegInfo omap_cp_reginfo[] = {
999     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1000       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1001       .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1002     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1003       .access = PL1_RW, .type = ARM_CP_NOP },
1004     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1005       .access = PL1_RW,
1006       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1007       .writefn = omap_ticonfig_write },
1008     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1009       .access = PL1_RW,
1010       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1011     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1012       .access = PL1_RW, .resetvalue = 0xff0,
1013       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1014     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1015       .access = PL1_RW,
1016       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1017       .writefn = omap_threadid_write },
1018     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1019       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1020       .type = ARM_CP_NO_MIGRATE,
1021       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1022     /* TODO: Peripheral port remap register:
1023      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1024      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1025      * when MMU is off.
1026      */
1027     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
1028       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1029       .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
1030       .writefn = omap_cachemaint_write },
1031     { .name = "C9", .cp = 15, .crn = 9,
1032       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1033       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1034     REGINFO_SENTINEL
1035 };
1036
1037 static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1038                              uint64_t value)
1039 {
1040     value &= 0x3fff;
1041     if (env->cp15.c15_cpar != value) {
1042         /* Changes cp0 to cp13 behavior, so needs a TB flush.  */
1043         tb_flush(env);
1044         env->cp15.c15_cpar = value;
1045     }
1046     return 0;
1047 }
1048
1049 static const ARMCPRegInfo xscale_cp_reginfo[] = {
1050     { .name = "XSCALE_CPAR",
1051       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1052       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1053       .writefn = xscale_cpar_write, },
1054     { .name = "XSCALE_AUXCR",
1055       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1056       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1057       .resetvalue = 0, },
1058     REGINFO_SENTINEL
1059 };
1060
1061 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1062     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1063      * implementation of this implementation-defined space.
1064      * Ideally this should eventually disappear in favour of actually
1065      * implementing the correct behaviour for all cores.
1066      */
1067     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1068       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1069       .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1070       .resetvalue = 0 },
1071     REGINFO_SENTINEL
1072 };
1073
1074 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1075     /* Cache status: RAZ because we have no cache so it's always clean */
1076     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
1077       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1078       .resetvalue = 0 },
1079     REGINFO_SENTINEL
1080 };
1081
1082 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1083     /* We never have a a block transfer operation in progress */
1084     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
1085       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1086       .resetvalue = 0 },
1087     /* The cache ops themselves: these all NOP for QEMU */
1088     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1089       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1090     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1091       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1092     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1093       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1094     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1095       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1096     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1097       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1098     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1099       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1100     REGINFO_SENTINEL
1101 };
1102
1103 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1104     /* The cache test-and-clean instructions always return (1 << 30)
1105      * to indicate that there are no dirty cache lines.
1106      */
1107     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
1108       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1109       .resetvalue = (1 << 30) },
1110     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
1111       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1112       .resetvalue = (1 << 30) },
1113     REGINFO_SENTINEL
1114 };
1115
1116 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1117     /* Ignore ReadBuffer accesses */
1118     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1119       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1120       .access = PL1_RW, .resetvalue = 0,
1121       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
1122     REGINFO_SENTINEL
1123 };
1124
1125 static int mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1126                       uint64_t *value)
1127 {
1128     CPUState *cs = CPU(arm_env_get_cpu(env));
1129     uint32_t mpidr = cs->cpu_index;
1130     /* We don't support setting cluster ID ([8..11])
1131      * so these bits always RAZ.
1132      */
1133     if (arm_feature(env, ARM_FEATURE_V7MP)) {
1134         mpidr |= (1 << 31);
1135         /* Cores which are uniprocessor (non-coherent)
1136          * but still implement the MP extensions set
1137          * bit 30. (For instance, A9UP.) However we do
1138          * not currently model any of those cores.
1139          */
1140     }
1141     *value = mpidr;
1142     return 0;
1143 }
1144
1145 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1146     { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
1147       .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
1148     REGINFO_SENTINEL
1149 };
1150
1151 static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1152 {
1153     *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1154     return 0;
1155 }
1156
1157 static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1158 {
1159     env->cp15.c7_par_hi = value >> 32;
1160     env->cp15.c7_par = value;
1161     return 0;
1162 }
1163
1164 static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1165 {
1166     env->cp15.c7_par_hi = 0;
1167     env->cp15.c7_par = 0;
1168 }
1169
1170 static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri,
1171                         uint64_t *value)
1172 {
1173     *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1174     return 0;
1175 }
1176
1177 static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1178                              uint64_t value)
1179 {
1180     env->cp15.c2_base0_hi = value >> 32;
1181     env->cp15.c2_base0 = value;
1182     return 0;
1183 }
1184
1185 static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1186                          uint64_t value)
1187 {
1188     /* Writes to the 64 bit format TTBRs may change the ASID */
1189     tlb_flush(env, 1);
1190     return ttbr064_raw_write(env, ri, value);
1191 }
1192
1193 static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1194 {
1195     env->cp15.c2_base0_hi = 0;
1196     env->cp15.c2_base0 = 0;
1197 }
1198
1199 static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri,
1200                         uint64_t *value)
1201 {
1202     *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1203     return 0;
1204 }
1205
1206 static int ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri,
1207                          uint64_t value)
1208 {
1209     env->cp15.c2_base1_hi = value >> 32;
1210     env->cp15.c2_base1 = value;
1211     return 0;
1212 }
1213
1214 static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1215 {
1216     env->cp15.c2_base1_hi = 0;
1217     env->cp15.c2_base1 = 0;
1218 }
1219
1220 static const ARMCPRegInfo lpae_cp_reginfo[] = {
1221     /* NOP AMAIR0/1: the override is because these clash with the rather
1222      * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1223      */
1224     { .name = "AMAIR0", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1225       .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1226       .resetvalue = 0 },
1227     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1228       .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1229       .resetvalue = 0 },
1230     /* 64 bit access versions of the (dummy) debug registers */
1231     { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
1232       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1233     { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
1234       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1235     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1236       .access = PL1_RW, .type = ARM_CP_64BIT,
1237       .readfn = par64_read, .writefn = par64_write, .resetfn = par64_reset },
1238     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1239       .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr064_read,
1240       .writefn = ttbr064_write, .raw_writefn = ttbr064_raw_write,
1241       .resetfn = ttbr064_reset },
1242     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1243       .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr164_read,
1244       .writefn = ttbr164_write, .resetfn = ttbr164_reset },
1245     REGINFO_SENTINEL
1246 };
1247
1248 static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1249 {
1250     env->cp15.c1_sys = value;
1251     /* ??? Lots of these bits are not implemented.  */
1252     /* This may enable/disable the MMU, so do a TLB flush.  */
1253     tlb_flush(env, 1);
1254     return 0;
1255 }
1256
1257 void register_cp_regs_for_features(ARMCPU *cpu)
1258 {
1259     /* Register all the coprocessor registers based on feature bits */
1260     CPUARMState *env = &cpu->env;
1261     if (arm_feature(env, ARM_FEATURE_M)) {
1262         /* M profile has no coprocessor registers */
1263         return;
1264     }
1265
1266     define_arm_cp_regs(cpu, cp_reginfo);
1267     if (arm_feature(env, ARM_FEATURE_V6)) {
1268         /* The ID registers all have impdef reset values */
1269         ARMCPRegInfo v6_idregs[] = {
1270             { .name = "ID_PFR0", .cp = 15, .crn = 0, .crm = 1,
1271               .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1272               .resetvalue = cpu->id_pfr0 },
1273             { .name = "ID_PFR1", .cp = 15, .crn = 0, .crm = 1,
1274               .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1275               .resetvalue = cpu->id_pfr1 },
1276             { .name = "ID_DFR0", .cp = 15, .crn = 0, .crm = 1,
1277               .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1278               .resetvalue = cpu->id_dfr0 },
1279             { .name = "ID_AFR0", .cp = 15, .crn = 0, .crm = 1,
1280               .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1281               .resetvalue = cpu->id_afr0 },
1282             { .name = "ID_MMFR0", .cp = 15, .crn = 0, .crm = 1,
1283               .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1284               .resetvalue = cpu->id_mmfr0 },
1285             { .name = "ID_MMFR1", .cp = 15, .crn = 0, .crm = 1,
1286               .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1287               .resetvalue = cpu->id_mmfr1 },
1288             { .name = "ID_MMFR2", .cp = 15, .crn = 0, .crm = 1,
1289               .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1290               .resetvalue = cpu->id_mmfr2 },
1291             { .name = "ID_MMFR3", .cp = 15, .crn = 0, .crm = 1,
1292               .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1293               .resetvalue = cpu->id_mmfr3 },
1294             { .name = "ID_ISAR0", .cp = 15, .crn = 0, .crm = 2,
1295               .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1296               .resetvalue = cpu->id_isar0 },
1297             { .name = "ID_ISAR1", .cp = 15, .crn = 0, .crm = 2,
1298               .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1299               .resetvalue = cpu->id_isar1 },
1300             { .name = "ID_ISAR2", .cp = 15, .crn = 0, .crm = 2,
1301               .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1302               .resetvalue = cpu->id_isar2 },
1303             { .name = "ID_ISAR3", .cp = 15, .crn = 0, .crm = 2,
1304               .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1305               .resetvalue = cpu->id_isar3 },
1306             { .name = "ID_ISAR4", .cp = 15, .crn = 0, .crm = 2,
1307               .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1308               .resetvalue = cpu->id_isar4 },
1309             { .name = "ID_ISAR5", .cp = 15, .crn = 0, .crm = 2,
1310               .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1311               .resetvalue = cpu->id_isar5 },
1312             /* 6..7 are as yet unallocated and must RAZ */
1313             { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
1314               .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1315               .resetvalue = 0 },
1316             { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
1317               .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1318               .resetvalue = 0 },
1319             REGINFO_SENTINEL
1320         };
1321         define_arm_cp_regs(cpu, v6_idregs);
1322         define_arm_cp_regs(cpu, v6_cp_reginfo);
1323     } else {
1324         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
1325     }
1326     if (arm_feature(env, ARM_FEATURE_V6K)) {
1327         define_arm_cp_regs(cpu, v6k_cp_reginfo);
1328     }
1329     if (arm_feature(env, ARM_FEATURE_V7)) {
1330         /* v7 performance monitor control register: same implementor
1331          * field as main ID register, and we implement no event counters.
1332          */
1333         ARMCPRegInfo pmcr = {
1334             .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
1335             .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
1336             .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
1337             .readfn = pmreg_read, .writefn = pmcr_write,
1338             .raw_readfn = raw_read, .raw_writefn = raw_write,
1339         };
1340         ARMCPRegInfo clidr = {
1341             .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
1342             .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
1343         };
1344         define_one_arm_cp_reg(cpu, &pmcr);
1345         define_one_arm_cp_reg(cpu, &clidr);
1346         define_arm_cp_regs(cpu, v7_cp_reginfo);
1347     } else {
1348         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
1349     }
1350     if (arm_feature(env, ARM_FEATURE_MPU)) {
1351         /* These are the MPU registers prior to PMSAv6. Any new
1352          * PMSA core later than the ARM946 will require that we
1353          * implement the PMSAv6 or PMSAv7 registers, which are
1354          * completely different.
1355          */
1356         assert(!arm_feature(env, ARM_FEATURE_V6));
1357         define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
1358     } else {
1359         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
1360     }
1361     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
1362         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
1363     }
1364     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1365         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
1366     }
1367     if (arm_feature(env, ARM_FEATURE_VAPA)) {
1368         define_arm_cp_regs(cpu, vapa_cp_reginfo);
1369     }
1370     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
1371         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
1372     }
1373     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
1374         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
1375     }
1376     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
1377         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
1378     }
1379     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1380         define_arm_cp_regs(cpu, omap_cp_reginfo);
1381     }
1382     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
1383         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
1384     }
1385     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1386         define_arm_cp_regs(cpu, xscale_cp_reginfo);
1387     }
1388     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
1389         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
1390     }
1391     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1392         define_arm_cp_regs(cpu, lpae_cp_reginfo);
1393     }
1394     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
1395      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
1396      * be read-only (ie write causes UNDEF exception).
1397      */
1398     {
1399         ARMCPRegInfo id_cp_reginfo[] = {
1400             /* Note that the MIDR isn't a simple constant register because
1401              * of the TI925 behaviour where writes to another register can
1402              * cause the MIDR value to change.
1403              *
1404              * Unimplemented registers in the c15 0 0 0 space default to
1405              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
1406              * and friends override accordingly.
1407              */
1408             { .name = "MIDR",
1409               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
1410               .access = PL1_R, .resetvalue = cpu->midr,
1411               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
1412               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
1413               .type = ARM_CP_OVERRIDE },
1414             { .name = "CTR",
1415               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
1416               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
1417             { .name = "TCMTR",
1418               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
1419               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1420             { .name = "TLBTR",
1421               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
1422               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1423             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
1424             { .name = "DUMMY",
1425               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
1426               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1427             { .name = "DUMMY",
1428               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
1429               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1430             { .name = "DUMMY",
1431               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
1432               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1433             { .name = "DUMMY",
1434               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
1435               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1436             { .name = "DUMMY",
1437               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
1438               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1439             REGINFO_SENTINEL
1440         };
1441         ARMCPRegInfo crn0_wi_reginfo = {
1442             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
1443             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
1444             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
1445         };
1446         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
1447             arm_feature(env, ARM_FEATURE_STRONGARM)) {
1448             ARMCPRegInfo *r;
1449             /* Register the blanket "writes ignored" value first to cover the
1450              * whole space. Then update the specific ID registers to allow write
1451              * access, so that they ignore writes rather than causing them to
1452              * UNDEF.
1453              */
1454             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
1455             for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
1456                 r->access = PL1_RW;
1457             }
1458         }
1459         define_arm_cp_regs(cpu, id_cp_reginfo);
1460     }
1461
1462     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
1463         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
1464     }
1465
1466     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
1467         ARMCPRegInfo auxcr = {
1468             .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
1469             .access = PL1_RW, .type = ARM_CP_CONST,
1470             .resetvalue = cpu->reset_auxcr
1471         };
1472         define_one_arm_cp_reg(cpu, &auxcr);
1473     }
1474
1475     /* Generic registers whose values depend on the implementation */
1476     {
1477         ARMCPRegInfo sctlr = {
1478             .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
1479             .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
1480             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
1481             .raw_writefn = raw_write,
1482         };
1483         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1484             /* Normally we would always end the TB on an SCTLR write, but Linux
1485              * arch/arm/mach-pxa/sleep.S expects two instructions following
1486              * an MMU enable to execute from cache.  Imitate this behaviour.
1487              */
1488             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
1489         }
1490         define_one_arm_cp_reg(cpu, &sctlr);
1491     }
1492 }
1493
1494 ARMCPU *cpu_arm_init(const char *cpu_model)
1495 {
1496     ARMCPU *cpu;
1497     CPUARMState *env;
1498     ObjectClass *oc;
1499
1500     oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1501     if (!oc) {
1502         return NULL;
1503     }
1504     cpu = ARM_CPU(object_new(object_class_get_name(oc)));
1505     env = &cpu->env;
1506     env->cpu_model_str = cpu_model;
1507
1508     /* TODO this should be set centrally, once possible */
1509     object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
1510
1511     return cpu;
1512 }
1513
1514 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
1515 {
1516     CPUState *cs = CPU(cpu);
1517     CPUARMState *env = &cpu->env;
1518
1519     if (arm_feature(env, ARM_FEATURE_NEON)) {
1520         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1521                                  51, "arm-neon.xml", 0);
1522     } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
1523         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1524                                  35, "arm-vfp3.xml", 0);
1525     } else if (arm_feature(env, ARM_FEATURE_VFP)) {
1526         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1527                                  19, "arm-vfp.xml", 0);
1528     }
1529 }
1530
1531 /* Sort alphabetically by type name, except for "any". */
1532 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
1533 {
1534     ObjectClass *class_a = (ObjectClass *)a;
1535     ObjectClass *class_b = (ObjectClass *)b;
1536     const char *name_a, *name_b;
1537
1538     name_a = object_class_get_name(class_a);
1539     name_b = object_class_get_name(class_b);
1540     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
1541         return 1;
1542     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
1543         return -1;
1544     } else {
1545         return strcmp(name_a, name_b);
1546     }
1547 }
1548
1549 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
1550 {
1551     ObjectClass *oc = data;
1552     CPUListState *s = user_data;
1553     const char *typename;
1554     char *name;
1555
1556     typename = object_class_get_name(oc);
1557     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
1558     (*s->cpu_fprintf)(s->file, "  %s\n",
1559                       name);
1560     g_free(name);
1561 }
1562
1563 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1564 {
1565     CPUListState s = {
1566         .file = f,
1567         .cpu_fprintf = cpu_fprintf,
1568     };
1569     GSList *list;
1570
1571     list = object_class_get_list(TYPE_ARM_CPU, false);
1572     list = g_slist_sort(list, arm_cpu_list_compare);
1573     (*cpu_fprintf)(f, "Available CPUs:\n");
1574     g_slist_foreach(list, arm_cpu_list_entry, &s);
1575     g_slist_free(list);
1576 }
1577
1578 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
1579                                        const ARMCPRegInfo *r, void *opaque)
1580 {
1581     /* Define implementations of coprocessor registers.
1582      * We store these in a hashtable because typically
1583      * there are less than 150 registers in a space which
1584      * is 16*16*16*8*8 = 262144 in size.
1585      * Wildcarding is supported for the crm, opc1 and opc2 fields.
1586      * If a register is defined twice then the second definition is
1587      * used, so this can be used to define some generic registers and
1588      * then override them with implementation specific variations.
1589      * At least one of the original and the second definition should
1590      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
1591      * against accidental use.
1592      */
1593     int crm, opc1, opc2;
1594     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
1595     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
1596     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
1597     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
1598     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
1599     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
1600     /* 64 bit registers have only CRm and Opc1 fields */
1601     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
1602     /* Check that the register definition has enough info to handle
1603      * reads and writes if they are permitted.
1604      */
1605     if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
1606         if (r->access & PL3_R) {
1607             assert(r->fieldoffset || r->readfn);
1608         }
1609         if (r->access & PL3_W) {
1610             assert(r->fieldoffset || r->writefn);
1611         }
1612     }
1613     /* Bad type field probably means missing sentinel at end of reg list */
1614     assert(cptype_valid(r->type));
1615     for (crm = crmmin; crm <= crmmax; crm++) {
1616         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
1617             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
1618                 uint32_t *key = g_new(uint32_t, 1);
1619                 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
1620                 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
1621                 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
1622                 if (opaque) {
1623                     r2->opaque = opaque;
1624                 }
1625                 /* Make sure reginfo passed to helpers for wildcarded regs
1626                  * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
1627                  */
1628                 r2->crm = crm;
1629                 r2->opc1 = opc1;
1630                 r2->opc2 = opc2;
1631                 /* By convention, for wildcarded registers only the first
1632                  * entry is used for migration; the others are marked as
1633                  * NO_MIGRATE so we don't try to transfer the register
1634                  * multiple times. Special registers (ie NOP/WFI) are
1635                  * never migratable.
1636                  */
1637                 if ((r->type & ARM_CP_SPECIAL) ||
1638                     ((r->crm == CP_ANY) && crm != 0) ||
1639                     ((r->opc1 == CP_ANY) && opc1 != 0) ||
1640                     ((r->opc2 == CP_ANY) && opc2 != 0)) {
1641                     r2->type |= ARM_CP_NO_MIGRATE;
1642                 }
1643
1644                 /* Overriding of an existing definition must be explicitly
1645                  * requested.
1646                  */
1647                 if (!(r->type & ARM_CP_OVERRIDE)) {
1648                     ARMCPRegInfo *oldreg;
1649                     oldreg = g_hash_table_lookup(cpu->cp_regs, key);
1650                     if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
1651                         fprintf(stderr, "Register redefined: cp=%d %d bit "
1652                                 "crn=%d crm=%d opc1=%d opc2=%d, "
1653                                 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
1654                                 r2->crn, r2->crm, r2->opc1, r2->opc2,
1655                                 oldreg->name, r2->name);
1656                         g_assert_not_reached();
1657                     }
1658                 }
1659                 g_hash_table_insert(cpu->cp_regs, key, r2);
1660             }
1661         }
1662     }
1663 }
1664
1665 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1666                                     const ARMCPRegInfo *regs, void *opaque)
1667 {
1668     /* Define a whole list of registers */
1669     const ARMCPRegInfo *r;
1670     for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
1671         define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
1672     }
1673 }
1674
1675 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1676 {
1677     return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1678 }
1679
1680 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1681                         uint64_t value)
1682 {
1683     /* Helper coprocessor write function for write-ignore registers */
1684     return 0;
1685 }
1686
1687 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1688 {
1689     /* Helper coprocessor write function for read-as-zero registers */
1690     *value = 0;
1691     return 0;
1692 }
1693
1694 static int bad_mode_switch(CPUARMState *env, int mode)
1695 {
1696     /* Return true if it is not valid for us to switch to
1697      * this CPU mode (ie all the UNPREDICTABLE cases in
1698      * the ARM ARM CPSRWriteByInstr pseudocode).
1699      */
1700     switch (mode) {
1701     case ARM_CPU_MODE_USR:
1702     case ARM_CPU_MODE_SYS:
1703     case ARM_CPU_MODE_SVC:
1704     case ARM_CPU_MODE_ABT:
1705     case ARM_CPU_MODE_UND:
1706     case ARM_CPU_MODE_IRQ:
1707     case ARM_CPU_MODE_FIQ:
1708         return 0;
1709     default:
1710         return 1;
1711     }
1712 }
1713
1714 uint32_t cpsr_read(CPUARMState *env)
1715 {
1716     int ZF;
1717     ZF = (env->ZF == 0);
1718     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
1719         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
1720         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
1721         | ((env->condexec_bits & 0xfc) << 8)
1722         | (env->GE << 16);
1723 }
1724
1725 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
1726 {
1727     if (mask & CPSR_NZCV) {
1728         env->ZF = (~val) & CPSR_Z;
1729         env->NF = val;
1730         env->CF = (val >> 29) & 1;
1731         env->VF = (val << 3) & 0x80000000;
1732     }
1733     if (mask & CPSR_Q)
1734         env->QF = ((val & CPSR_Q) != 0);
1735     if (mask & CPSR_T)
1736         env->thumb = ((val & CPSR_T) != 0);
1737     if (mask & CPSR_IT_0_1) {
1738         env->condexec_bits &= ~3;
1739         env->condexec_bits |= (val >> 25) & 3;
1740     }
1741     if (mask & CPSR_IT_2_7) {
1742         env->condexec_bits &= 3;
1743         env->condexec_bits |= (val >> 8) & 0xfc;
1744     }
1745     if (mask & CPSR_GE) {
1746         env->GE = (val >> 16) & 0xf;
1747     }
1748
1749     if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
1750         if (bad_mode_switch(env, val & CPSR_M)) {
1751             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
1752              * We choose to ignore the attempt and leave the CPSR M field
1753              * untouched.
1754              */
1755             mask &= ~CPSR_M;
1756         } else {
1757             switch_mode(env, val & CPSR_M);
1758         }
1759     }
1760     mask &= ~CACHED_CPSR_BITS;
1761     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
1762 }
1763
1764 /* Sign/zero extend */
1765 uint32_t HELPER(sxtb16)(uint32_t x)
1766 {
1767     uint32_t res;
1768     res = (uint16_t)(int8_t)x;
1769     res |= (uint32_t)(int8_t)(x >> 16) << 16;
1770     return res;
1771 }
1772
1773 uint32_t HELPER(uxtb16)(uint32_t x)
1774 {
1775     uint32_t res;
1776     res = (uint16_t)(uint8_t)x;
1777     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
1778     return res;
1779 }
1780
1781 uint32_t HELPER(clz)(uint32_t x)
1782 {
1783     return clz32(x);
1784 }
1785
1786 int32_t HELPER(sdiv)(int32_t num, int32_t den)
1787 {
1788     if (den == 0)
1789       return 0;
1790     if (num == INT_MIN && den == -1)
1791       return INT_MIN;
1792     return num / den;
1793 }
1794
1795 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
1796 {
1797     if (den == 0)
1798       return 0;
1799     return num / den;
1800 }
1801
1802 uint32_t HELPER(rbit)(uint32_t x)
1803 {
1804     x =  ((x & 0xff000000) >> 24)
1805        | ((x & 0x00ff0000) >> 8)
1806        | ((x & 0x0000ff00) << 8)
1807        | ((x & 0x000000ff) << 24);
1808     x =  ((x & 0xf0f0f0f0) >> 4)
1809        | ((x & 0x0f0f0f0f) << 4);
1810     x =  ((x & 0x88888888) >> 3)
1811        | ((x & 0x44444444) >> 1)
1812        | ((x & 0x22222222) << 1)
1813        | ((x & 0x11111111) << 3);
1814     return x;
1815 }
1816
1817 #if defined(CONFIG_USER_ONLY)
1818
1819 void arm_cpu_do_interrupt(CPUState *cs)
1820 {
1821     ARMCPU *cpu = ARM_CPU(cs);
1822     CPUARMState *env = &cpu->env;
1823
1824     env->exception_index = -1;
1825 }
1826
1827 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
1828                               int mmu_idx)
1829 {
1830     if (rw == 2) {
1831         env->exception_index = EXCP_PREFETCH_ABORT;
1832         env->cp15.c6_insn = address;
1833     } else {
1834         env->exception_index = EXCP_DATA_ABORT;
1835         env->cp15.c6_data = address;
1836     }
1837     return 1;
1838 }
1839
1840 /* These should probably raise undefined insn exceptions.  */
1841 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
1842 {
1843     cpu_abort(env, "v7m_mrs %d\n", reg);
1844 }
1845
1846 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
1847 {
1848     cpu_abort(env, "v7m_mrs %d\n", reg);
1849     return 0;
1850 }
1851
1852 void switch_mode(CPUARMState *env, int mode)
1853 {
1854     if (mode != ARM_CPU_MODE_USR)
1855         cpu_abort(env, "Tried to switch out of user mode\n");
1856 }
1857
1858 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
1859 {
1860     cpu_abort(env, "banked r13 write\n");
1861 }
1862
1863 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
1864 {
1865     cpu_abort(env, "banked r13 read\n");
1866     return 0;
1867 }
1868
1869 #else
1870
1871 /* Map CPU modes onto saved register banks.  */
1872 int bank_number(int mode)
1873 {
1874     switch (mode) {
1875     case ARM_CPU_MODE_USR:
1876     case ARM_CPU_MODE_SYS:
1877         return 0;
1878     case ARM_CPU_MODE_SVC:
1879         return 1;
1880     case ARM_CPU_MODE_ABT:
1881         return 2;
1882     case ARM_CPU_MODE_UND:
1883         return 3;
1884     case ARM_CPU_MODE_IRQ:
1885         return 4;
1886     case ARM_CPU_MODE_FIQ:
1887         return 5;
1888     }
1889     hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
1890 }
1891
1892 void switch_mode(CPUARMState *env, int mode)
1893 {
1894     int old_mode;
1895     int i;
1896
1897     old_mode = env->uncached_cpsr & CPSR_M;
1898     if (mode == old_mode)
1899         return;
1900
1901     if (old_mode == ARM_CPU_MODE_FIQ) {
1902         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
1903         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
1904     } else if (mode == ARM_CPU_MODE_FIQ) {
1905         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
1906         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
1907     }
1908
1909     i = bank_number(old_mode);
1910     env->banked_r13[i] = env->regs[13];
1911     env->banked_r14[i] = env->regs[14];
1912     env->banked_spsr[i] = env->spsr;
1913
1914     i = bank_number(mode);
1915     env->regs[13] = env->banked_r13[i];
1916     env->regs[14] = env->banked_r14[i];
1917     env->spsr = env->banked_spsr[i];
1918 }
1919
1920 static void v7m_push(CPUARMState *env, uint32_t val)
1921 {
1922     env->regs[13] -= 4;
1923     stl_phys(env->regs[13], val);
1924 }
1925
1926 static uint32_t v7m_pop(CPUARMState *env)
1927 {
1928     uint32_t val;
1929     val = ldl_phys(env->regs[13]);
1930     env->regs[13] += 4;
1931     return val;
1932 }
1933
1934 /* Switch to V7M main or process stack pointer.  */
1935 static void switch_v7m_sp(CPUARMState *env, int process)
1936 {
1937     uint32_t tmp;
1938     if (env->v7m.current_sp != process) {
1939         tmp = env->v7m.other_sp;
1940         env->v7m.other_sp = env->regs[13];
1941         env->regs[13] = tmp;
1942         env->v7m.current_sp = process;
1943     }
1944 }
1945
1946 static void do_v7m_exception_exit(CPUARMState *env)
1947 {
1948     uint32_t type;
1949     uint32_t xpsr;
1950
1951     type = env->regs[15];
1952     if (env->v7m.exception != 0)
1953         armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
1954
1955     /* Switch to the target stack.  */
1956     switch_v7m_sp(env, (type & 4) != 0);
1957     /* Pop registers.  */
1958     env->regs[0] = v7m_pop(env);
1959     env->regs[1] = v7m_pop(env);
1960     env->regs[2] = v7m_pop(env);
1961     env->regs[3] = v7m_pop(env);
1962     env->regs[12] = v7m_pop(env);
1963     env->regs[14] = v7m_pop(env);
1964     env->regs[15] = v7m_pop(env);
1965     xpsr = v7m_pop(env);
1966     xpsr_write(env, xpsr, 0xfffffdff);
1967     /* Undo stack alignment.  */
1968     if (xpsr & 0x200)
1969         env->regs[13] |= 4;
1970     /* ??? The exception return type specifies Thread/Handler mode.  However
1971        this is also implied by the xPSR value. Not sure what to do
1972        if there is a mismatch.  */
1973     /* ??? Likewise for mismatches between the CONTROL register and the stack
1974        pointer.  */
1975 }
1976
1977 /* Exception names for debug logging; note that not all of these
1978  * precisely correspond to architectural exceptions.
1979  */
1980 static const char * const excnames[] = {
1981     [EXCP_UDEF] = "Undefined Instruction",
1982     [EXCP_SWI] = "SVC",
1983     [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
1984     [EXCP_DATA_ABORT] = "Data Abort",
1985     [EXCP_IRQ] = "IRQ",
1986     [EXCP_FIQ] = "FIQ",
1987     [EXCP_BKPT] = "Breakpoint",
1988     [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
1989     [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
1990     [EXCP_STREX] = "QEMU intercept of STREX",
1991 };
1992
1993 static inline void arm_log_exception(int idx)
1994 {
1995     if (qemu_loglevel_mask(CPU_LOG_INT)) {
1996         const char *exc = NULL;
1997
1998         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
1999             exc = excnames[idx];
2000         }
2001         if (!exc) {
2002             exc = "unknown";
2003         }
2004         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
2005     }
2006 }
2007
2008 void arm_v7m_cpu_do_interrupt(CPUState *cs)
2009 {
2010     ARMCPU *cpu = ARM_CPU(cs);
2011     CPUARMState *env = &cpu->env;
2012     uint32_t xpsr = xpsr_read(env);
2013     uint32_t lr;
2014     uint32_t addr;
2015
2016     arm_log_exception(env->exception_index);
2017
2018     lr = 0xfffffff1;
2019     if (env->v7m.current_sp)
2020         lr |= 4;
2021     if (env->v7m.exception == 0)
2022         lr |= 8;
2023
2024     /* For exceptions we just mark as pending on the NVIC, and let that
2025        handle it.  */
2026     /* TODO: Need to escalate if the current priority is higher than the
2027        one we're raising.  */
2028     switch (env->exception_index) {
2029     case EXCP_UDEF:
2030         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
2031         return;
2032     case EXCP_SWI:
2033         /* The PC already points to the next instruction.  */
2034         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
2035         return;
2036     case EXCP_PREFETCH_ABORT:
2037     case EXCP_DATA_ABORT:
2038         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
2039         return;
2040     case EXCP_BKPT:
2041         if (semihosting_enabled) {
2042             int nr;
2043             nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2044             if (nr == 0xab) {
2045                 env->regs[15] += 2;
2046                 env->regs[0] = do_arm_semihosting(env);
2047                 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2048                 return;
2049             }
2050         }
2051         armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
2052         return;
2053     case EXCP_IRQ:
2054         env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
2055         break;
2056     case EXCP_EXCEPTION_EXIT:
2057         do_v7m_exception_exit(env);
2058         return;
2059     default:
2060         cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2061         return; /* Never happens.  Keep compiler happy.  */
2062     }
2063
2064     /* Align stack pointer.  */
2065     /* ??? Should only do this if Configuration Control Register
2066        STACKALIGN bit is set.  */
2067     if (env->regs[13] & 4) {
2068         env->regs[13] -= 4;
2069         xpsr |= 0x200;
2070     }
2071     /* Switch to the handler mode.  */
2072     v7m_push(env, xpsr);
2073     v7m_push(env, env->regs[15]);
2074     v7m_push(env, env->regs[14]);
2075     v7m_push(env, env->regs[12]);
2076     v7m_push(env, env->regs[3]);
2077     v7m_push(env, env->regs[2]);
2078     v7m_push(env, env->regs[1]);
2079     v7m_push(env, env->regs[0]);
2080     switch_v7m_sp(env, 0);
2081     /* Clear IT bits */
2082     env->condexec_bits = 0;
2083     env->regs[14] = lr;
2084     addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
2085     env->regs[15] = addr & 0xfffffffe;
2086     env->thumb = addr & 1;
2087 }
2088
2089 /* Handle a CPU exception.  */
2090 void arm_cpu_do_interrupt(CPUState *cs)
2091 {
2092     ARMCPU *cpu = ARM_CPU(cs);
2093     CPUARMState *env = &cpu->env;
2094     uint32_t addr;
2095     uint32_t mask;
2096     int new_mode;
2097     uint32_t offset;
2098
2099     assert(!IS_M(env));
2100
2101     arm_log_exception(env->exception_index);
2102
2103     /* TODO: Vectored interrupt controller.  */
2104     switch (env->exception_index) {
2105     case EXCP_UDEF:
2106         new_mode = ARM_CPU_MODE_UND;
2107         addr = 0x04;
2108         mask = CPSR_I;
2109         if (env->thumb)
2110             offset = 2;
2111         else
2112             offset = 4;
2113         break;
2114     case EXCP_SWI:
2115         if (semihosting_enabled) {
2116             /* Check for semihosting interrupt.  */
2117             if (env->thumb) {
2118                 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
2119                     & 0xff;
2120             } else {
2121                 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
2122                     & 0xffffff;
2123             }
2124             /* Only intercept calls from privileged modes, to provide some
2125                semblance of security.  */
2126             if (((mask == 0x123456 && !env->thumb)
2127                     || (mask == 0xab && env->thumb))
2128                   && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2129                 env->regs[0] = do_arm_semihosting(env);
2130                 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2131                 return;
2132             }
2133         }
2134         new_mode = ARM_CPU_MODE_SVC;
2135         addr = 0x08;
2136         mask = CPSR_I;
2137         /* The PC already points to the next instruction.  */
2138         offset = 0;
2139         break;
2140     case EXCP_BKPT:
2141         /* See if this is a semihosting syscall.  */
2142         if (env->thumb && semihosting_enabled) {
2143             mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2144             if (mask == 0xab
2145                   && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2146                 env->regs[15] += 2;
2147                 env->regs[0] = do_arm_semihosting(env);
2148                 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2149                 return;
2150             }
2151         }
2152         env->cp15.c5_insn = 2;
2153         /* Fall through to prefetch abort.  */
2154     case EXCP_PREFETCH_ABORT:
2155         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
2156                       env->cp15.c5_insn, env->cp15.c6_insn);
2157         new_mode = ARM_CPU_MODE_ABT;
2158         addr = 0x0c;
2159         mask = CPSR_A | CPSR_I;
2160         offset = 4;
2161         break;
2162     case EXCP_DATA_ABORT:
2163         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
2164                       env->cp15.c5_data, env->cp15.c6_data);
2165         new_mode = ARM_CPU_MODE_ABT;
2166         addr = 0x10;
2167         mask = CPSR_A | CPSR_I;
2168         offset = 8;
2169         break;
2170     case EXCP_IRQ:
2171         new_mode = ARM_CPU_MODE_IRQ;
2172         addr = 0x18;
2173         /* Disable IRQ and imprecise data aborts.  */
2174         mask = CPSR_A | CPSR_I;
2175         offset = 4;
2176         break;
2177     case EXCP_FIQ:
2178         new_mode = ARM_CPU_MODE_FIQ;
2179         addr = 0x1c;
2180         /* Disable FIQ, IRQ and imprecise data aborts.  */
2181         mask = CPSR_A | CPSR_I | CPSR_F;
2182         offset = 4;
2183         break;
2184     default:
2185         cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2186         return; /* Never happens.  Keep compiler happy.  */
2187     }
2188     /* High vectors.  */
2189     if (env->cp15.c1_sys & (1 << 13)) {
2190         addr += 0xffff0000;
2191     }
2192     switch_mode (env, new_mode);
2193     env->spsr = cpsr_read(env);
2194     /* Clear IT bits.  */
2195     env->condexec_bits = 0;
2196     /* Switch to the new mode, and to the correct instruction set.  */
2197     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
2198     env->uncached_cpsr |= mask;
2199     /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
2200      * and we should just guard the thumb mode on V4 */
2201     if (arm_feature(env, ARM_FEATURE_V4T)) {
2202         env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
2203     }
2204     env->regs[14] = env->regs[15] + offset;
2205     env->regs[15] = addr;
2206     cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
2207 }
2208
2209 /* Check section/page access permissions.
2210    Returns the page protection flags, or zero if the access is not
2211    permitted.  */
2212 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
2213                            int access_type, int is_user)
2214 {
2215   int prot_ro;
2216
2217   if (domain_prot == 3) {
2218     return PAGE_READ | PAGE_WRITE;
2219   }
2220
2221   if (access_type == 1)
2222       prot_ro = 0;
2223   else
2224       prot_ro = PAGE_READ;
2225
2226   switch (ap) {
2227   case 0:
2228       if (access_type == 1)
2229           return 0;
2230       switch ((env->cp15.c1_sys >> 8) & 3) {
2231       case 1:
2232           return is_user ? 0 : PAGE_READ;
2233       case 2:
2234           return PAGE_READ;
2235       default:
2236           return 0;
2237       }
2238   case 1:
2239       return is_user ? 0 : PAGE_READ | PAGE_WRITE;
2240   case 2:
2241       if (is_user)
2242           return prot_ro;
2243       else
2244           return PAGE_READ | PAGE_WRITE;
2245   case 3:
2246       return PAGE_READ | PAGE_WRITE;
2247   case 4: /* Reserved.  */
2248       return 0;
2249   case 5:
2250       return is_user ? 0 : prot_ro;
2251   case 6:
2252       return prot_ro;
2253   case 7:
2254       if (!arm_feature (env, ARM_FEATURE_V6K))
2255           return 0;
2256       return prot_ro;
2257   default:
2258       abort();
2259   }
2260 }
2261
2262 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
2263 {
2264     uint32_t table;
2265
2266     if (address & env->cp15.c2_mask)
2267         table = env->cp15.c2_base1 & 0xffffc000;
2268     else
2269         table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
2270
2271     table |= (address >> 18) & 0x3ffc;
2272     return table;
2273 }
2274
2275 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
2276                             int is_user, hwaddr *phys_ptr,
2277                             int *prot, target_ulong *page_size)
2278 {
2279     int code;
2280     uint32_t table;
2281     uint32_t desc;
2282     int type;
2283     int ap;
2284     int domain;
2285     int domain_prot;
2286     hwaddr phys_addr;
2287
2288     /* Pagetable walk.  */
2289     /* Lookup l1 descriptor.  */
2290     table = get_level1_table_address(env, address);
2291     desc = ldl_phys(table);
2292     type = (desc & 3);
2293     domain = (desc >> 5) & 0x0f;
2294     domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2295     if (type == 0) {
2296         /* Section translation fault.  */
2297         code = 5;
2298         goto do_fault;
2299     }
2300     if (domain_prot == 0 || domain_prot == 2) {
2301         if (type == 2)
2302             code = 9; /* Section domain fault.  */
2303         else
2304             code = 11; /* Page domain fault.  */
2305         goto do_fault;
2306     }
2307     if (type == 2) {
2308         /* 1Mb section.  */
2309         phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2310         ap = (desc >> 10) & 3;
2311         code = 13;
2312         *page_size = 1024 * 1024;
2313     } else {
2314         /* Lookup l2 entry.  */
2315         if (type == 1) {
2316             /* Coarse pagetable.  */
2317             table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2318         } else {
2319             /* Fine pagetable.  */
2320             table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
2321         }
2322         desc = ldl_phys(table);
2323         switch (desc & 3) {
2324         case 0: /* Page translation fault.  */
2325             code = 7;
2326             goto do_fault;
2327         case 1: /* 64k page.  */
2328             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2329             ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2330             *page_size = 0x10000;
2331             break;
2332         case 2: /* 4k page.  */
2333             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2334             ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2335             *page_size = 0x1000;
2336             break;
2337         case 3: /* 1k page.  */
2338             if (type == 1) {
2339                 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2340                     phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2341                 } else {
2342                     /* Page translation fault.  */
2343                     code = 7;
2344                     goto do_fault;
2345                 }
2346             } else {
2347                 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
2348             }
2349             ap = (desc >> 4) & 3;
2350             *page_size = 0x400;
2351             break;
2352         default:
2353             /* Never happens, but compiler isn't smart enough to tell.  */
2354             abort();
2355         }
2356         code = 15;
2357     }
2358     *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2359     if (!*prot) {
2360         /* Access permission fault.  */
2361         goto do_fault;
2362     }
2363     *prot |= PAGE_EXEC;
2364     *phys_ptr = phys_addr;
2365     return 0;
2366 do_fault:
2367     return code | (domain << 4);
2368 }
2369
2370 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
2371                             int is_user, hwaddr *phys_ptr,
2372                             int *prot, target_ulong *page_size)
2373 {
2374     int code;
2375     uint32_t table;
2376     uint32_t desc;
2377     uint32_t xn;
2378     uint32_t pxn = 0;
2379     int type;
2380     int ap;
2381     int domain = 0;
2382     int domain_prot;
2383     hwaddr phys_addr;
2384
2385     /* Pagetable walk.  */
2386     /* Lookup l1 descriptor.  */
2387     table = get_level1_table_address(env, address);
2388     desc = ldl_phys(table);
2389     type = (desc & 3);
2390     if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
2391         /* Section translation fault, or attempt to use the encoding
2392          * which is Reserved on implementations without PXN.
2393          */
2394         code = 5;
2395         goto do_fault;
2396     }
2397     if ((type == 1) || !(desc & (1 << 18))) {
2398         /* Page or Section.  */
2399         domain = (desc >> 5) & 0x0f;
2400     }
2401     domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2402     if (domain_prot == 0 || domain_prot == 2) {
2403         if (type != 1) {
2404             code = 9; /* Section domain fault.  */
2405         } else {
2406             code = 11; /* Page domain fault.  */
2407         }
2408         goto do_fault;
2409     }
2410     if (type != 1) {
2411         if (desc & (1 << 18)) {
2412             /* Supersection.  */
2413             phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
2414             *page_size = 0x1000000;
2415         } else {
2416             /* Section.  */
2417             phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2418             *page_size = 0x100000;
2419         }
2420         ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
2421         xn = desc & (1 << 4);
2422         pxn = desc & 1;
2423         code = 13;
2424     } else {
2425         if (arm_feature(env, ARM_FEATURE_PXN)) {
2426             pxn = (desc >> 2) & 1;
2427         }
2428         /* Lookup l2 entry.  */
2429         table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2430         desc = ldl_phys(table);
2431         ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
2432         switch (desc & 3) {
2433         case 0: /* Page translation fault.  */
2434             code = 7;
2435             goto do_fault;
2436         case 1: /* 64k page.  */
2437             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2438             xn = desc & (1 << 15);
2439             *page_size = 0x10000;
2440             break;
2441         case 2: case 3: /* 4k page.  */
2442             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2443             xn = desc & 1;
2444             *page_size = 0x1000;
2445             break;
2446         default:
2447             /* Never happens, but compiler isn't smart enough to tell.  */
2448             abort();
2449         }
2450         code = 15;
2451     }
2452     if (domain_prot == 3) {
2453         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2454     } else {
2455         if (pxn && !is_user) {
2456             xn = 1;
2457         }
2458         if (xn && access_type == 2)
2459             goto do_fault;
2460
2461         /* The simplified model uses AP[0] as an access control bit.  */
2462         if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
2463             /* Access flag fault.  */
2464             code = (code == 15) ? 6 : 3;
2465             goto do_fault;
2466         }
2467         *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2468         if (!*prot) {
2469             /* Access permission fault.  */
2470             goto do_fault;
2471         }
2472         if (!xn) {
2473             *prot |= PAGE_EXEC;
2474         }
2475     }
2476     *phys_ptr = phys_addr;
2477     return 0;
2478 do_fault:
2479     return code | (domain << 4);
2480 }
2481
2482 /* Fault type for long-descriptor MMU fault reporting; this corresponds
2483  * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
2484  */
2485 typedef enum {
2486     translation_fault = 1,
2487     access_fault = 2,
2488     permission_fault = 3,
2489 } MMUFaultType;
2490
2491 static int get_phys_addr_lpae(CPUARMState *env, uint32_t address,
2492                               int access_type, int is_user,
2493                               hwaddr *phys_ptr, int *prot,
2494                               target_ulong *page_size_ptr)
2495 {
2496     /* Read an LPAE long-descriptor translation table. */
2497     MMUFaultType fault_type = translation_fault;
2498     uint32_t level = 1;
2499     uint32_t epd;
2500     uint32_t tsz;
2501     uint64_t ttbr;
2502     int ttbr_select;
2503     int n;
2504     hwaddr descaddr;
2505     uint32_t tableattrs;
2506     target_ulong page_size;
2507     uint32_t attrs;
2508
2509     /* Determine whether this address is in the region controlled by
2510      * TTBR0 or TTBR1 (or if it is in neither region and should fault).
2511      * This is a Non-secure PL0/1 stage 1 translation, so controlled by
2512      * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
2513      */
2514     uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3);
2515     uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3);
2516     if (t0sz && !extract32(address, 32 - t0sz, t0sz)) {
2517         /* there is a ttbr0 region and we are in it (high bits all zero) */
2518         ttbr_select = 0;
2519     } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) {
2520         /* there is a ttbr1 region and we are in it (high bits all one) */
2521         ttbr_select = 1;
2522     } else if (!t0sz) {
2523         /* ttbr0 region is "everything not in the ttbr1 region" */
2524         ttbr_select = 0;
2525     } else if (!t1sz) {
2526         /* ttbr1 region is "everything not in the ttbr0 region" */
2527         ttbr_select = 1;
2528     } else {
2529         /* in the gap between the two regions, this is a Translation fault */
2530         fault_type = translation_fault;
2531         goto do_fault;
2532     }
2533
2534     /* Note that QEMU ignores shareability and cacheability attributes,
2535      * so we don't need to do anything with the SH, ORGN, IRGN fields
2536      * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
2537      * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
2538      * implement any ASID-like capability so we can ignore it (instead
2539      * we will always flush the TLB any time the ASID is changed).
2540      */
2541     if (ttbr_select == 0) {
2542         ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
2543         epd = extract32(env->cp15.c2_control, 7, 1);
2544         tsz = t0sz;
2545     } else {
2546         ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
2547         epd = extract32(env->cp15.c2_control, 23, 1);
2548         tsz = t1sz;
2549     }
2550
2551     if (epd) {
2552         /* Translation table walk disabled => Translation fault on TLB miss */
2553         goto do_fault;
2554     }
2555
2556     /* If the region is small enough we will skip straight to a 2nd level
2557      * lookup. This affects the number of bits of the address used in
2558      * combination with the TTBR to find the first descriptor. ('n' here
2559      * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
2560      * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
2561      */
2562     if (tsz > 1) {
2563         level = 2;
2564         n = 14 - tsz;
2565     } else {
2566         n = 5 - tsz;
2567     }
2568
2569     /* Clear the vaddr bits which aren't part of the within-region address,
2570      * so that we don't have to special case things when calculating the
2571      * first descriptor address.
2572      */
2573     address &= (0xffffffffU >> tsz);
2574
2575     /* Now we can extract the actual base address from the TTBR */
2576     descaddr = extract64(ttbr, 0, 40);
2577     descaddr &= ~((1ULL << n) - 1);
2578
2579     tableattrs = 0;
2580     for (;;) {
2581         uint64_t descriptor;
2582
2583         descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
2584         descriptor = ldq_phys(descaddr);
2585         if (!(descriptor & 1) ||
2586             (!(descriptor & 2) && (level == 3))) {
2587             /* Invalid, or the Reserved level 3 encoding */
2588             goto do_fault;
2589         }
2590         descaddr = descriptor & 0xfffffff000ULL;
2591
2592         if ((descriptor & 2) && (level < 3)) {
2593             /* Table entry. The top five bits are attributes which  may
2594              * propagate down through lower levels of the table (and
2595              * which are all arranged so that 0 means "no effect", so
2596              * we can gather them up by ORing in the bits at each level).
2597              */
2598             tableattrs |= extract64(descriptor, 59, 5);
2599             level++;
2600             continue;
2601         }
2602         /* Block entry at level 1 or 2, or page entry at level 3.
2603          * These are basically the same thing, although the number
2604          * of bits we pull in from the vaddr varies.
2605          */
2606         page_size = (1 << (39 - (9 * level)));
2607         descaddr |= (address & (page_size - 1));
2608         /* Extract attributes from the descriptor and merge with table attrs */
2609         attrs = extract64(descriptor, 2, 10)
2610             | (extract64(descriptor, 52, 12) << 10);
2611         attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
2612         attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
2613         /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
2614          * means "force PL1 access only", which means forcing AP[1] to 0.
2615          */
2616         if (extract32(tableattrs, 2, 1)) {
2617             attrs &= ~(1 << 4);
2618         }
2619         /* Since we're always in the Non-secure state, NSTable is ignored. */
2620         break;
2621     }
2622     /* Here descaddr is the final physical address, and attributes
2623      * are all in attrs.
2624      */
2625     fault_type = access_fault;
2626     if ((attrs & (1 << 8)) == 0) {
2627         /* Access flag */
2628         goto do_fault;
2629     }
2630     fault_type = permission_fault;
2631     if (is_user && !(attrs & (1 << 4))) {
2632         /* Unprivileged access not enabled */
2633         goto do_fault;
2634     }
2635     *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2636     if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) {
2637         /* XN or PXN */
2638         if (access_type == 2) {
2639             goto do_fault;
2640         }
2641         *prot &= ~PAGE_EXEC;
2642     }
2643     if (attrs & (1 << 5)) {
2644         /* Write access forbidden */
2645         if (access_type == 1) {
2646             goto do_fault;
2647         }
2648         *prot &= ~PAGE_WRITE;
2649     }
2650
2651     *phys_ptr = descaddr;
2652     *page_size_ptr = page_size;
2653     return 0;
2654
2655 do_fault:
2656     /* Long-descriptor format IFSR/DFSR value */
2657     return (1 << 9) | (fault_type << 2) | level;
2658 }
2659
2660 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
2661                              int access_type, int is_user,
2662                              hwaddr *phys_ptr, int *prot)
2663 {
2664     int n;
2665     uint32_t mask;
2666     uint32_t base;
2667
2668     *phys_ptr = address;
2669     for (n = 7; n >= 0; n--) {
2670         base = env->cp15.c6_region[n];
2671         if ((base & 1) == 0)
2672             continue;
2673         mask = 1 << ((base >> 1) & 0x1f);
2674         /* Keep this shift separate from the above to avoid an
2675            (undefined) << 32.  */
2676         mask = (mask << 1) - 1;
2677         if (((base ^ address) & ~mask) == 0)
2678             break;
2679     }
2680     if (n < 0)
2681         return 2;
2682
2683     if (access_type == 2) {
2684         mask = env->cp15.c5_insn;
2685     } else {
2686         mask = env->cp15.c5_data;
2687     }
2688     mask = (mask >> (n * 4)) & 0xf;
2689     switch (mask) {
2690     case 0:
2691         return 1;
2692     case 1:
2693         if (is_user)
2694           return 1;
2695         *prot = PAGE_READ | PAGE_WRITE;
2696         break;
2697     case 2:
2698         *prot = PAGE_READ;
2699         if (!is_user)
2700             *prot |= PAGE_WRITE;
2701         break;
2702     case 3:
2703         *prot = PAGE_READ | PAGE_WRITE;
2704         break;
2705     case 5:
2706         if (is_user)
2707             return 1;
2708         *prot = PAGE_READ;
2709         break;
2710     case 6:
2711         *prot = PAGE_READ;
2712         break;
2713     default:
2714         /* Bad permission.  */
2715         return 1;
2716     }
2717     *prot |= PAGE_EXEC;
2718     return 0;
2719 }
2720
2721 /* get_phys_addr - get the physical address for this virtual address
2722  *
2723  * Find the physical address corresponding to the given virtual address,
2724  * by doing a translation table walk on MMU based systems or using the
2725  * MPU state on MPU based systems.
2726  *
2727  * Returns 0 if the translation was successful. Otherwise, phys_ptr,
2728  * prot and page_size are not filled in, and the return value provides
2729  * information on why the translation aborted, in the format of a
2730  * DFSR/IFSR fault register, with the following caveats:
2731  *  * we honour the short vs long DFSR format differences.
2732  *  * the WnR bit is never set (the caller must do this).
2733  *  * for MPU based systems we don't bother to return a full FSR format
2734  *    value.
2735  *
2736  * @env: CPUARMState
2737  * @address: virtual address to get physical address for
2738  * @access_type: 0 for read, 1 for write, 2 for execute
2739  * @is_user: 0 for privileged access, 1 for user
2740  * @phys_ptr: set to the physical address corresponding to the virtual address
2741  * @prot: set to the permissions for the page containing phys_ptr
2742  * @page_size: set to the size of the page containing phys_ptr
2743  */
2744 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
2745                                 int access_type, int is_user,
2746                                 hwaddr *phys_ptr, int *prot,
2747                                 target_ulong *page_size)
2748 {
2749     /* Fast Context Switch Extension.  */
2750     if (address < 0x02000000)
2751         address += env->cp15.c13_fcse;
2752
2753     if ((env->cp15.c1_sys & 1) == 0) {
2754         /* MMU/MPU disabled.  */
2755         *phys_ptr = address;
2756         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2757         *page_size = TARGET_PAGE_SIZE;
2758         return 0;
2759     } else if (arm_feature(env, ARM_FEATURE_MPU)) {
2760         *page_size = TARGET_PAGE_SIZE;
2761         return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
2762                                  prot);
2763     } else if (extended_addresses_enabled(env)) {
2764         return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
2765                                   prot, page_size);
2766     } else if (env->cp15.c1_sys & (1 << 23)) {
2767         return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
2768                                 prot, page_size);
2769     } else {
2770         return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
2771                                 prot, page_size);
2772     }
2773 }
2774
2775 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
2776                               int access_type, int mmu_idx)
2777 {
2778     hwaddr phys_addr;
2779     target_ulong page_size;
2780     int prot;
2781     int ret, is_user;
2782
2783     is_user = mmu_idx == MMU_USER_IDX;
2784     ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
2785                         &page_size);
2786     if (ret == 0) {
2787         /* Map a single [sub]page.  */
2788         phys_addr &= ~(hwaddr)0x3ff;
2789         address &= ~(uint32_t)0x3ff;
2790         tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
2791         return 0;
2792     }
2793
2794     if (access_type == 2) {
2795         env->cp15.c5_insn = ret;
2796         env->cp15.c6_insn = address;
2797         env->exception_index = EXCP_PREFETCH_ABORT;
2798     } else {
2799         env->cp15.c5_data = ret;
2800         if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
2801             env->cp15.c5_data |= (1 << 11);
2802         env->cp15.c6_data = address;
2803         env->exception_index = EXCP_DATA_ABORT;
2804     }
2805     return 1;
2806 }
2807
2808 hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
2809 {
2810     ARMCPU *cpu = ARM_CPU(cs);
2811     hwaddr phys_addr;
2812     target_ulong page_size;
2813     int prot;
2814     int ret;
2815
2816     ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
2817
2818     if (ret != 0) {
2819         return -1;
2820     }
2821
2822     return phys_addr;
2823 }
2824
2825 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2826 {
2827     if ((env->uncached_cpsr & CPSR_M) == mode) {
2828         env->regs[13] = val;
2829     } else {
2830         env->banked_r13[bank_number(mode)] = val;
2831     }
2832 }
2833
2834 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2835 {
2836     if ((env->uncached_cpsr & CPSR_M) == mode) {
2837         return env->regs[13];
2838     } else {
2839         return env->banked_r13[bank_number(mode)];
2840     }
2841 }
2842
2843 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2844 {
2845     switch (reg) {
2846     case 0: /* APSR */
2847         return xpsr_read(env) & 0xf8000000;
2848     case 1: /* IAPSR */
2849         return xpsr_read(env) & 0xf80001ff;
2850     case 2: /* EAPSR */
2851         return xpsr_read(env) & 0xff00fc00;
2852     case 3: /* xPSR */
2853         return xpsr_read(env) & 0xff00fdff;
2854     case 5: /* IPSR */
2855         return xpsr_read(env) & 0x000001ff;
2856     case 6: /* EPSR */
2857         return xpsr_read(env) & 0x0700fc00;
2858     case 7: /* IEPSR */
2859         return xpsr_read(env) & 0x0700edff;
2860     case 8: /* MSP */
2861         return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2862     case 9: /* PSP */
2863         return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2864     case 16: /* PRIMASK */
2865         return (env->uncached_cpsr & CPSR_I) != 0;
2866     case 17: /* BASEPRI */
2867     case 18: /* BASEPRI_MAX */
2868         return env->v7m.basepri;
2869     case 19: /* FAULTMASK */
2870         return (env->uncached_cpsr & CPSR_F) != 0;
2871     case 20: /* CONTROL */
2872         return env->v7m.control;
2873     default:
2874         /* ??? For debugging only.  */
2875         cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2876         return 0;
2877     }
2878 }
2879
2880 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2881 {
2882     switch (reg) {
2883     case 0: /* APSR */
2884         xpsr_write(env, val, 0xf8000000);
2885         break;
2886     case 1: /* IAPSR */
2887         xpsr_write(env, val, 0xf8000000);
2888         break;
2889     case 2: /* EAPSR */
2890         xpsr_write(env, val, 0xfe00fc00);
2891         break;
2892     case 3: /* xPSR */
2893         xpsr_write(env, val, 0xfe00fc00);
2894         break;
2895     case 5: /* IPSR */
2896         /* IPSR bits are readonly.  */
2897         break;
2898     case 6: /* EPSR */
2899         xpsr_write(env, val, 0x0600fc00);
2900         break;
2901     case 7: /* IEPSR */
2902         xpsr_write(env, val, 0x0600fc00);
2903         break;
2904     case 8: /* MSP */
2905         if (env->v7m.current_sp)
2906             env->v7m.other_sp = val;
2907         else
2908             env->regs[13] = val;
2909         break;
2910     case 9: /* PSP */
2911         if (env->v7m.current_sp)
2912             env->regs[13] = val;
2913         else
2914             env->v7m.other_sp = val;
2915         break;
2916     case 16: /* PRIMASK */
2917         if (val & 1)
2918             env->uncached_cpsr |= CPSR_I;
2919         else
2920             env->uncached_cpsr &= ~CPSR_I;
2921         break;
2922     case 17: /* BASEPRI */
2923         env->v7m.basepri = val & 0xff;
2924         break;
2925     case 18: /* BASEPRI_MAX */
2926         val &= 0xff;
2927         if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2928             env->v7m.basepri = val;
2929         break;
2930     case 19: /* FAULTMASK */
2931         if (val & 1)
2932             env->uncached_cpsr |= CPSR_F;
2933         else
2934             env->uncached_cpsr &= ~CPSR_F;
2935         break;
2936     case 20: /* CONTROL */
2937         env->v7m.control = val & 3;
2938         switch_v7m_sp(env, (val & 2) != 0);
2939         break;
2940     default:
2941         /* ??? For debugging only.  */
2942         cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2943         return;
2944     }
2945 }
2946
2947 #endif
2948
2949 /* Note that signed overflow is undefined in C.  The following routines are
2950    careful to use unsigned types where modulo arithmetic is required.
2951    Failure to do so _will_ break on newer gcc.  */
2952
2953 /* Signed saturating arithmetic.  */
2954
2955 /* Perform 16-bit signed saturating addition.  */
2956 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2957 {
2958     uint16_t res;
2959
2960     res = a + b;
2961     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2962         if (a & 0x8000)
2963             res = 0x8000;
2964         else
2965             res = 0x7fff;
2966     }
2967     return res;
2968 }
2969
2970 /* Perform 8-bit signed saturating addition.  */
2971 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2972 {
2973     uint8_t res;
2974
2975     res = a + b;
2976     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2977         if (a & 0x80)
2978             res = 0x80;
2979         else
2980             res = 0x7f;
2981     }
2982     return res;
2983 }
2984
2985 /* Perform 16-bit signed saturating subtraction.  */
2986 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2987 {
2988     uint16_t res;
2989
2990     res = a - b;
2991     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2992         if (a & 0x8000)
2993             res = 0x8000;
2994         else
2995             res = 0x7fff;
2996     }
2997     return res;
2998 }
2999
3000 /* Perform 8-bit signed saturating subtraction.  */
3001 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
3002 {
3003     uint8_t res;
3004
3005     res = a - b;
3006     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
3007         if (a & 0x80)
3008             res = 0x80;
3009         else
3010             res = 0x7f;
3011     }
3012     return res;
3013 }
3014
3015 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3016 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3017 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
3018 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
3019 #define PFX q
3020
3021 #include "op_addsub.h"
3022
3023 /* Unsigned saturating arithmetic.  */
3024 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
3025 {
3026     uint16_t res;
3027     res = a + b;
3028     if (res < a)
3029         res = 0xffff;
3030     return res;
3031 }
3032
3033 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
3034 {
3035     if (a > b)
3036         return a - b;
3037     else
3038         return 0;
3039 }
3040
3041 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
3042 {
3043     uint8_t res;
3044     res = a + b;
3045     if (res < a)
3046         res = 0xff;
3047     return res;
3048 }
3049
3050 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
3051 {
3052     if (a > b)
3053         return a - b;
3054     else
3055         return 0;
3056 }
3057
3058 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3059 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3060 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
3061 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
3062 #define PFX uq
3063
3064 #include "op_addsub.h"
3065
3066 /* Signed modulo arithmetic.  */
3067 #define SARITH16(a, b, n, op) do { \
3068     int32_t sum; \
3069     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
3070     RESULT(sum, n, 16); \
3071     if (sum >= 0) \
3072         ge |= 3 << (n * 2); \
3073     } while(0)
3074
3075 #define SARITH8(a, b, n, op) do { \
3076     int32_t sum; \
3077     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
3078     RESULT(sum, n, 8); \
3079     if (sum >= 0) \
3080         ge |= 1 << n; \
3081     } while(0)
3082
3083
3084 #define ADD16(a, b, n) SARITH16(a, b, n, +)
3085 #define SUB16(a, b, n) SARITH16(a, b, n, -)
3086 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
3087 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
3088 #define PFX s
3089 #define ARITH_GE
3090
3091 #include "op_addsub.h"
3092
3093 /* Unsigned modulo arithmetic.  */
3094 #define ADD16(a, b, n) do { \
3095     uint32_t sum; \
3096     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3097     RESULT(sum, n, 16); \
3098     if ((sum >> 16) == 1) \
3099         ge |= 3 << (n * 2); \
3100     } while(0)
3101
3102 #define ADD8(a, b, n) do { \
3103     uint32_t sum; \
3104     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
3105     RESULT(sum, n, 8); \
3106     if ((sum >> 8) == 1) \
3107         ge |= 1 << n; \
3108     } while(0)
3109
3110 #define SUB16(a, b, n) do { \
3111     uint32_t sum; \
3112     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
3113     RESULT(sum, n, 16); \
3114     if ((sum >> 16) == 0) \
3115         ge |= 3 << (n * 2); \
3116     } while(0)
3117
3118 #define SUB8(a, b, n) do { \
3119     uint32_t sum; \
3120     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
3121     RESULT(sum, n, 8); \
3122     if ((sum >> 8) == 0) \
3123         ge |= 1 << n; \
3124     } while(0)
3125
3126 #define PFX u
3127 #define ARITH_GE
3128
3129 #include "op_addsub.h"
3130
3131 /* Halved signed arithmetic.  */
3132 #define ADD16(a, b, n) \
3133   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
3134 #define SUB16(a, b, n) \
3135   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
3136 #define ADD8(a, b, n) \
3137   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
3138 #define SUB8(a, b, n) \
3139   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
3140 #define PFX sh
3141
3142 #include "op_addsub.h"
3143
3144 /* Halved unsigned arithmetic.  */
3145 #define ADD16(a, b, n) \
3146   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3147 #define SUB16(a, b, n) \
3148   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3149 #define ADD8(a, b, n) \
3150   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3151 #define SUB8(a, b, n) \
3152   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3153 #define PFX uh
3154
3155 #include "op_addsub.h"
3156
3157 static inline uint8_t do_usad(uint8_t a, uint8_t b)
3158 {
3159     if (a > b)
3160         return a - b;
3161     else
3162         return b - a;
3163 }
3164
3165 /* Unsigned sum of absolute byte differences.  */
3166 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
3167 {
3168     uint32_t sum;
3169     sum = do_usad(a, b);
3170     sum += do_usad(a >> 8, b >> 8);
3171     sum += do_usad(a >> 16, b >>16);
3172     sum += do_usad(a >> 24, b >> 24);
3173     return sum;
3174 }
3175
3176 /* For ARMv6 SEL instruction.  */
3177 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
3178 {
3179     uint32_t mask;
3180
3181     mask = 0;
3182     if (flags & 1)
3183         mask |= 0xff;
3184     if (flags & 2)
3185         mask |= 0xff00;
3186     if (flags & 4)
3187         mask |= 0xff0000;
3188     if (flags & 8)
3189         mask |= 0xff000000;
3190     return (a & mask) | (b & ~mask);
3191 }
3192
3193 /* VFP support.  We follow the convention used for VFP instructions:
3194    Single precision routines have a "s" suffix, double precision a
3195    "d" suffix.  */
3196
3197 /* Convert host exception flags to vfp form.  */
3198 static inline int vfp_exceptbits_from_host(int host_bits)
3199 {
3200     int target_bits = 0;
3201
3202     if (host_bits & float_flag_invalid)
3203         target_bits |= 1;
3204     if (host_bits & float_flag_divbyzero)
3205         target_bits |= 2;
3206     if (host_bits & float_flag_overflow)
3207         target_bits |= 4;
3208     if (host_bits & (float_flag_underflow | float_flag_output_denormal))
3209         target_bits |= 8;
3210     if (host_bits & float_flag_inexact)
3211         target_bits |= 0x10;
3212     if (host_bits & float_flag_input_denormal)
3213         target_bits |= 0x80;
3214     return target_bits;
3215 }
3216
3217 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
3218 {
3219     int i;
3220     uint32_t fpscr;
3221
3222     fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
3223             | (env->vfp.vec_len << 16)
3224             | (env->vfp.vec_stride << 20);
3225     i = get_float_exception_flags(&env->vfp.fp_status);
3226     i |= get_float_exception_flags(&env->vfp.standard_fp_status);
3227     fpscr |= vfp_exceptbits_from_host(i);
3228     return fpscr;
3229 }
3230
3231 uint32_t vfp_get_fpscr(CPUARMState *env)
3232 {
3233     return HELPER(vfp_get_fpscr)(env);
3234 }
3235
3236 /* Convert vfp exception flags to target form.  */
3237 static inline int vfp_exceptbits_to_host(int target_bits)
3238 {
3239     int host_bits = 0;
3240
3241     if (target_bits & 1)
3242         host_bits |= float_flag_invalid;
3243     if (target_bits & 2)
3244         host_bits |= float_flag_divbyzero;
3245     if (target_bits & 4)
3246         host_bits |= float_flag_overflow;
3247     if (target_bits & 8)
3248         host_bits |= float_flag_underflow;
3249     if (target_bits & 0x10)
3250         host_bits |= float_flag_inexact;
3251     if (target_bits & 0x80)
3252         host_bits |= float_flag_input_denormal;
3253     return host_bits;
3254 }
3255
3256 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
3257 {
3258     int i;
3259     uint32_t changed;
3260
3261     changed = env->vfp.xregs[ARM_VFP_FPSCR];
3262     env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
3263     env->vfp.vec_len = (val >> 16) & 7;
3264     env->vfp.vec_stride = (val >> 20) & 3;
3265
3266     changed ^= val;
3267     if (changed & (3 << 22)) {
3268         i = (val >> 22) & 3;
3269         switch (i) {
3270         case 0:
3271             i = float_round_nearest_even;
3272             break;
3273         case 1:
3274             i = float_round_up;
3275             break;
3276         case 2:
3277             i = float_round_down;
3278             break;
3279         case 3:
3280             i = float_round_to_zero;
3281             break;
3282         }
3283         set_float_rounding_mode(i, &env->vfp.fp_status);
3284     }
3285     if (changed & (1 << 24)) {
3286         set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3287         set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3288     }
3289     if (changed & (1 << 25))
3290         set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
3291
3292     i = vfp_exceptbits_to_host(val);
3293     set_float_exception_flags(i, &env->vfp.fp_status);
3294     set_float_exception_flags(0, &env->vfp.standard_fp_status);
3295 }
3296
3297 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
3298 {
3299     HELPER(vfp_set_fpscr)(env, val);
3300 }
3301
3302 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
3303
3304 #define VFP_BINOP(name) \
3305 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
3306 { \
3307     float_status *fpst = fpstp; \
3308     return float32_ ## name(a, b, fpst); \
3309 } \
3310 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
3311 { \
3312     float_status *fpst = fpstp; \
3313     return float64_ ## name(a, b, fpst); \
3314 }
3315 VFP_BINOP(add)
3316 VFP_BINOP(sub)
3317 VFP_BINOP(mul)
3318 VFP_BINOP(div)
3319 #undef VFP_BINOP
3320
3321 float32 VFP_HELPER(neg, s)(float32 a)
3322 {
3323     return float32_chs(a);
3324 }
3325
3326 float64 VFP_HELPER(neg, d)(float64 a)
3327 {
3328     return float64_chs(a);
3329 }
3330
3331 float32 VFP_HELPER(abs, s)(float32 a)
3332 {
3333     return float32_abs(a);
3334 }
3335
3336 float64 VFP_HELPER(abs, d)(float64 a)
3337 {
3338     return float64_abs(a);
3339 }
3340
3341 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
3342 {
3343     return float32_sqrt(a, &env->vfp.fp_status);
3344 }
3345
3346 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
3347 {
3348     return float64_sqrt(a, &env->vfp.fp_status);
3349 }
3350
3351 /* XXX: check quiet/signaling case */
3352 #define DO_VFP_cmp(p, type) \
3353 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env)  \
3354 { \
3355     uint32_t flags; \
3356     switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
3357     case 0: flags = 0x6; break; \
3358     case -1: flags = 0x8; break; \
3359     case 1: flags = 0x2; break; \
3360     default: case 2: flags = 0x3; break; \
3361     } \
3362     env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3363         | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3364 } \
3365 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
3366 { \
3367     uint32_t flags; \
3368     switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
3369     case 0: flags = 0x6; break; \
3370     case -1: flags = 0x8; break; \
3371     case 1: flags = 0x2; break; \
3372     default: case 2: flags = 0x3; break; \
3373     } \
3374     env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3375         | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3376 }
3377 DO_VFP_cmp(s, float32)
3378 DO_VFP_cmp(d, float64)
3379 #undef DO_VFP_cmp
3380
3381 /* Integer to float and float to integer conversions */
3382
3383 #define CONV_ITOF(name, fsz, sign) \
3384     float##fsz HELPER(name)(uint32_t x, void *fpstp) \
3385 { \
3386     float_status *fpst = fpstp; \
3387     return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
3388 }
3389
3390 #define CONV_FTOI(name, fsz, sign, round) \
3391 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
3392 { \
3393     float_status *fpst = fpstp; \
3394     if (float##fsz##_is_any_nan(x)) { \
3395         float_raise(float_flag_invalid, fpst); \
3396         return 0; \
3397     } \
3398     return float##fsz##_to_##sign##int32##round(x, fpst); \
3399 }
3400
3401 #define FLOAT_CONVS(name, p, fsz, sign) \
3402 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
3403 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
3404 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
3405
3406 FLOAT_CONVS(si, s, 32, )
3407 FLOAT_CONVS(si, d, 64, )
3408 FLOAT_CONVS(ui, s, 32, u)
3409 FLOAT_CONVS(ui, d, 64, u)
3410
3411 #undef CONV_ITOF
3412 #undef CONV_FTOI
3413 #undef FLOAT_CONVS
3414
3415 /* floating point conversion */
3416 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
3417 {
3418     float64 r = float32_to_float64(x, &env->vfp.fp_status);
3419     /* ARM requires that S<->D conversion of any kind of NaN generates
3420      * a quiet NaN by forcing the most significant frac bit to 1.
3421      */
3422     return float64_maybe_silence_nan(r);
3423 }
3424
3425 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
3426 {
3427     float32 r =  float64_to_float32(x, &env->vfp.fp_status);
3428     /* ARM requires that S<->D conversion of any kind of NaN generates
3429      * a quiet NaN by forcing the most significant frac bit to 1.
3430      */
3431     return float32_maybe_silence_nan(r);
3432 }
3433
3434 /* VFP3 fixed point conversion.  */
3435 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
3436 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t  x, uint32_t shift, \
3437                                     void *fpstp) \
3438 { \
3439     float_status *fpst = fpstp; \
3440     float##fsz tmp; \
3441     tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
3442     return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
3443 } \
3444 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
3445                                        void *fpstp) \
3446 { \
3447     float_status *fpst = fpstp; \
3448     float##fsz tmp; \
3449     if (float##fsz##_is_any_nan(x)) { \
3450         float_raise(float_flag_invalid, fpst); \
3451         return 0; \
3452     } \
3453     tmp = float##fsz##_scalbn(x, shift, fpst); \
3454     return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
3455 }
3456
3457 VFP_CONV_FIX(sh, d, 64, int16, )
3458 VFP_CONV_FIX(sl, d, 64, int32, )
3459 VFP_CONV_FIX(uh, d, 64, uint16, u)
3460 VFP_CONV_FIX(ul, d, 64, uint32, u)
3461 VFP_CONV_FIX(sh, s, 32, int16, )
3462 VFP_CONV_FIX(sl, s, 32, int32, )
3463 VFP_CONV_FIX(uh, s, 32, uint16, u)
3464 VFP_CONV_FIX(ul, s, 32, uint32, u)
3465 #undef VFP_CONV_FIX
3466
3467 /* Half precision conversions.  */
3468 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
3469 {
3470     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3471     float32 r = float16_to_float32(make_float16(a), ieee, s);
3472     if (ieee) {
3473         return float32_maybe_silence_nan(r);
3474     }
3475     return r;
3476 }
3477
3478 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
3479 {
3480     int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3481     float16 r = float32_to_float16(a, ieee, s);
3482     if (ieee) {
3483         r = float16_maybe_silence_nan(r);
3484     }
3485     return float16_val(r);
3486 }
3487
3488 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3489 {
3490     return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
3491 }
3492
3493 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3494 {
3495     return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
3496 }
3497
3498 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3499 {
3500     return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
3501 }
3502
3503 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3504 {
3505     return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
3506 }
3507
3508 #define float32_two make_float32(0x40000000)
3509 #define float32_three make_float32(0x40400000)
3510 #define float32_one_point_five make_float32(0x3fc00000)
3511
3512 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
3513 {
3514     float_status *s = &env->vfp.standard_fp_status;
3515     if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3516         (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3517         if (!(float32_is_zero(a) || float32_is_zero(b))) {
3518             float_raise(float_flag_input_denormal, s);
3519         }
3520         return float32_two;
3521     }
3522     return float32_sub(float32_two, float32_mul(a, b, s), s);
3523 }
3524
3525 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
3526 {
3527     float_status *s = &env->vfp.standard_fp_status;
3528     float32 product;
3529     if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3530         (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3531         if (!(float32_is_zero(a) || float32_is_zero(b))) {
3532             float_raise(float_flag_input_denormal, s);
3533         }
3534         return float32_one_point_five;
3535     }
3536     product = float32_mul(a, b, s);
3537     return float32_div(float32_sub(float32_three, product, s), float32_two, s);
3538 }
3539
3540 /* NEON helpers.  */
3541
3542 /* Constants 256 and 512 are used in some helpers; we avoid relying on
3543  * int->float conversions at run-time.  */
3544 #define float64_256 make_float64(0x4070000000000000LL)
3545 #define float64_512 make_float64(0x4080000000000000LL)
3546
3547 /* The algorithm that must be used to calculate the estimate
3548  * is specified by the ARM ARM.
3549  */
3550 static float64 recip_estimate(float64 a, CPUARMState *env)
3551 {
3552     /* These calculations mustn't set any fp exception flags,
3553      * so we use a local copy of the fp_status.
3554      */
3555     float_status dummy_status = env->vfp.standard_fp_status;
3556     float_status *s = &dummy_status;
3557     /* q = (int)(a * 512.0) */
3558     float64 q = float64_mul(float64_512, a, s);
3559     int64_t q_int = float64_to_int64_round_to_zero(q, s);
3560
3561     /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3562     q = int64_to_float64(q_int, s);
3563     q = float64_add(q, float64_half, s);
3564     q = float64_div(q, float64_512, s);
3565     q = float64_div(float64_one, q, s);
3566
3567     /* s = (int)(256.0 * r + 0.5) */
3568     q = float64_mul(q, float64_256, s);
3569     q = float64_add(q, float64_half, s);
3570     q_int = float64_to_int64_round_to_zero(q, s);
3571
3572     /* return (double)s / 256.0 */
3573     return float64_div(int64_to_float64(q_int, s), float64_256, s);
3574 }
3575
3576 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
3577 {
3578     float_status *s = &env->vfp.standard_fp_status;
3579     float64 f64;
3580     uint32_t val32 = float32_val(a);
3581
3582     int result_exp;
3583     int a_exp = (val32  & 0x7f800000) >> 23;
3584     int sign = val32 & 0x80000000;
3585
3586     if (float32_is_any_nan(a)) {
3587         if (float32_is_signaling_nan(a)) {
3588             float_raise(float_flag_invalid, s);
3589         }
3590         return float32_default_nan;
3591     } else if (float32_is_infinity(a)) {
3592         return float32_set_sign(float32_zero, float32_is_neg(a));
3593     } else if (float32_is_zero_or_denormal(a)) {
3594         if (!float32_is_zero(a)) {
3595             float_raise(float_flag_input_denormal, s);
3596         }
3597         float_raise(float_flag_divbyzero, s);
3598         return float32_set_sign(float32_infinity, float32_is_neg(a));
3599     } else if (a_exp >= 253) {
3600         float_raise(float_flag_underflow, s);
3601         return float32_set_sign(float32_zero, float32_is_neg(a));
3602     }
3603
3604     f64 = make_float64((0x3feULL << 52)
3605                        | ((int64_t)(val32 & 0x7fffff) << 29));
3606
3607     result_exp = 253 - a_exp;
3608
3609     f64 = recip_estimate(f64, env);
3610
3611     val32 = sign
3612         | ((result_exp & 0xff) << 23)
3613         | ((float64_val(f64) >> 29) & 0x7fffff);
3614     return make_float32(val32);
3615 }
3616
3617 /* The algorithm that must be used to calculate the estimate
3618  * is specified by the ARM ARM.
3619  */
3620 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
3621 {
3622     /* These calculations mustn't set any fp exception flags,
3623      * so we use a local copy of the fp_status.
3624      */
3625     float_status dummy_status = env->vfp.standard_fp_status;
3626     float_status *s = &dummy_status;
3627     float64 q;
3628     int64_t q_int;
3629
3630     if (float64_lt(a, float64_half, s)) {
3631         /* range 0.25 <= a < 0.5 */
3632
3633         /* a in units of 1/512 rounded down */
3634         /* q0 = (int)(a * 512.0);  */
3635         q = float64_mul(float64_512, a, s);
3636         q_int = float64_to_int64_round_to_zero(q, s);
3637
3638         /* reciprocal root r */
3639         /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0);  */
3640         q = int64_to_float64(q_int, s);
3641         q = float64_add(q, float64_half, s);
3642         q = float64_div(q, float64_512, s);
3643         q = float64_sqrt(q, s);
3644         q = float64_div(float64_one, q, s);
3645     } else {
3646         /* range 0.5 <= a < 1.0 */
3647
3648         /* a in units of 1/256 rounded down */
3649         /* q1 = (int)(a * 256.0); */
3650         q = float64_mul(float64_256, a, s);
3651         int64_t q_int = float64_to_int64_round_to_zero(q, s);
3652
3653         /* reciprocal root r */
3654         /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3655         q = int64_to_float64(q_int, s);
3656         q = float64_add(q, float64_half, s);
3657         q = float64_div(q, float64_256, s);
3658         q = float64_sqrt(q, s);
3659         q = float64_div(float64_one, q, s);
3660     }
3661     /* r in units of 1/256 rounded to nearest */
3662     /* s = (int)(256.0 * r + 0.5); */
3663
3664     q = float64_mul(q, float64_256,s );
3665     q = float64_add(q, float64_half, s);
3666     q_int = float64_to_int64_round_to_zero(q, s);
3667
3668     /* return (double)s / 256.0;*/
3669     return float64_div(int64_to_float64(q_int, s), float64_256, s);
3670 }
3671
3672 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3673 {
3674     float_status *s = &env->vfp.standard_fp_status;
3675     int result_exp;
3676     float64 f64;
3677     uint32_t val;
3678     uint64_t val64;
3679
3680     val = float32_val(a);
3681
3682     if (float32_is_any_nan(a)) {
3683         if (float32_is_signaling_nan(a)) {
3684             float_raise(float_flag_invalid, s);
3685         }
3686         return float32_default_nan;
3687     } else if (float32_is_zero_or_denormal(a)) {
3688         if (!float32_is_zero(a)) {
3689             float_raise(float_flag_input_denormal, s);
3690         }
3691         float_raise(float_flag_divbyzero, s);
3692         return float32_set_sign(float32_infinity, float32_is_neg(a));
3693     } else if (float32_is_neg(a)) {
3694         float_raise(float_flag_invalid, s);
3695         return float32_default_nan;
3696     } else if (float32_is_infinity(a)) {
3697         return float32_zero;
3698     }
3699
3700     /* Normalize to a double-precision value between 0.25 and 1.0,
3701      * preserving the parity of the exponent.  */
3702     if ((val & 0x800000) == 0) {
3703         f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3704                            | (0x3feULL << 52)
3705                            | ((uint64_t)(val & 0x7fffff) << 29));
3706     } else {
3707         f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3708                            | (0x3fdULL << 52)
3709                            | ((uint64_t)(val & 0x7fffff) << 29));
3710     }
3711
3712     result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3713
3714     f64 = recip_sqrt_estimate(f64, env);
3715
3716     val64 = float64_val(f64);
3717
3718     val = ((result_exp & 0xff) << 23)
3719         | ((val64 >> 29)  & 0x7fffff);
3720     return make_float32(val);
3721 }
3722
3723 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3724 {
3725     float64 f64;
3726
3727     if ((a & 0x80000000) == 0) {
3728         return 0xffffffff;
3729     }
3730
3731     f64 = make_float64((0x3feULL << 52)
3732                        | ((int64_t)(a & 0x7fffffff) << 21));
3733
3734     f64 = recip_estimate (f64, env);
3735
3736     return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3737 }
3738
3739 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3740 {
3741     float64 f64;
3742
3743     if ((a & 0xc0000000) == 0) {
3744         return 0xffffffff;
3745     }
3746
3747     if (a & 0x80000000) {
3748         f64 = make_float64((0x3feULL << 52)
3749                            | ((uint64_t)(a & 0x7fffffff) << 21));
3750     } else { /* bits 31-30 == '01' */
3751         f64 = make_float64((0x3fdULL << 52)
3752                            | ((uint64_t)(a & 0x3fffffff) << 22));
3753     }
3754
3755     f64 = recip_sqrt_estimate(f64, env);
3756
3757     return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3758 }
3759
3760 /* VFPv4 fused multiply-accumulate */
3761 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3762 {
3763     float_status *fpst = fpstp;
3764     return float32_muladd(a, b, c, 0, fpst);
3765 }
3766
3767 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3768 {
3769     float_status *fpst = fpstp;
3770     return float64_muladd(a, b, c, 0, fpst);
3771 }
This page took 0.240708 seconds and 2 git commands to generate.