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