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