]> Git Repo - qemu.git/blame - target-arm/helper.c
hw/arm/virt: smbios: inform guest of kvm
[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 */
cfe67cef 13#include "exec/semihost.h"
0b03bdfc 14
352c98e5
LV
15#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
16
4a501606 17#ifndef CONFIG_USER_ONLY
b7cc4e82
PC
18static inline bool get_phys_addr(CPUARMState *env, target_ulong address,
19 int access_type, ARMMMUIdx mmu_idx,
20 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
21 target_ulong *page_size, uint32_t *fsr);
7c2cb42b
AF
22
23/* Definitions for the PMCCNTR and PMCR registers */
24#define PMCRD 0x8
25#define PMCRC 0x4
26#define PMCRE 0x1
4a501606
PM
27#endif
28
0ecb72a5 29static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
30{
31 int nregs;
32
33 /* VFP data registers are always little-endian. */
34 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
35 if (reg < nregs) {
36 stfq_le_p(buf, env->vfp.regs[reg]);
37 return 8;
38 }
39 if (arm_feature(env, ARM_FEATURE_NEON)) {
40 /* Aliases for Q regs. */
41 nregs += 16;
42 if (reg < nregs) {
43 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
44 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
45 return 16;
46 }
47 }
48 switch (reg - nregs) {
49 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
50 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
51 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
52 }
53 return 0;
54}
55
0ecb72a5 56static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
57{
58 int nregs;
59
60 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
61 if (reg < nregs) {
62 env->vfp.regs[reg] = ldfq_le_p(buf);
63 return 8;
64 }
65 if (arm_feature(env, ARM_FEATURE_NEON)) {
66 nregs += 16;
67 if (reg < nregs) {
68 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
69 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
70 return 16;
71 }
72 }
73 switch (reg - nregs) {
74 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
75 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 76 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
77 }
78 return 0;
79}
80
6a669427
PM
81static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
82{
83 switch (reg) {
84 case 0 ... 31:
85 /* 128 bit FP register */
86 stfq_le_p(buf, env->vfp.regs[reg * 2]);
87 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
88 return 16;
89 case 32:
90 /* FPSR */
91 stl_p(buf, vfp_get_fpsr(env));
92 return 4;
93 case 33:
94 /* FPCR */
95 stl_p(buf, vfp_get_fpcr(env));
96 return 4;
97 default:
98 return 0;
99 }
100}
101
102static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
103{
104 switch (reg) {
105 case 0 ... 31:
106 /* 128 bit FP register */
107 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
108 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
109 return 16;
110 case 32:
111 /* FPSR */
112 vfp_set_fpsr(env, ldl_p(buf));
113 return 4;
114 case 33:
115 /* FPCR */
116 vfp_set_fpcr(env, ldl_p(buf));
117 return 4;
118 default:
119 return 0;
120 }
121}
122
c4241c7d 123static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 124{
375421cc 125 assert(ri->fieldoffset);
67ed771d 126 if (cpreg_field_is_64bit(ri)) {
c4241c7d 127 return CPREG_FIELD64(env, ri);
22d9e1a9 128 } else {
c4241c7d 129 return CPREG_FIELD32(env, ri);
22d9e1a9 130 }
d4e6df63
PM
131}
132
c4241c7d
PM
133static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
134 uint64_t value)
d4e6df63 135{
375421cc 136 assert(ri->fieldoffset);
67ed771d 137 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
138 CPREG_FIELD64(env, ri) = value;
139 } else {
140 CPREG_FIELD32(env, ri) = value;
141 }
d4e6df63
PM
142}
143
11f136ee
FA
144static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
145{
146 return (char *)env + ri->fieldoffset;
147}
148
49a66191 149uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 150{
59a1c327 151 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 152 if (ri->type & ARM_CP_CONST) {
59a1c327 153 return ri->resetvalue;
721fae12 154 } else if (ri->raw_readfn) {
59a1c327 155 return ri->raw_readfn(env, ri);
721fae12 156 } else if (ri->readfn) {
59a1c327 157 return ri->readfn(env, ri);
721fae12 158 } else {
59a1c327 159 return raw_read(env, ri);
721fae12 160 }
721fae12
PM
161}
162
59a1c327 163static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 164 uint64_t v)
721fae12
PM
165{
166 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
167 * Note that constant registers are treated as write-ignored; the
168 * caller should check for success by whether a readback gives the
169 * value written.
170 */
171 if (ri->type & ARM_CP_CONST) {
59a1c327 172 return;
721fae12 173 } else if (ri->raw_writefn) {
c4241c7d 174 ri->raw_writefn(env, ri, v);
721fae12 175 } else if (ri->writefn) {
c4241c7d 176 ri->writefn(env, ri, v);
721fae12 177 } else {
afb2530f 178 raw_write(env, ri, v);
721fae12 179 }
721fae12
PM
180}
181
375421cc
PM
182static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
183{
184 /* Return true if the regdef would cause an assertion if you called
185 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
186 * program bug for it not to have the NO_RAW flag).
187 * NB that returning false here doesn't necessarily mean that calling
188 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
189 * read/write access functions which are safe for raw use" from "has
190 * read/write access functions which have side effects but has forgotten
191 * to provide raw access functions".
192 * The tests here line up with the conditions in read/write_raw_cp_reg()
193 * and assertions in raw_read()/raw_write().
194 */
195 if ((ri->type & ARM_CP_CONST) ||
196 ri->fieldoffset ||
197 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
198 return false;
199 }
200 return true;
201}
202
721fae12
PM
203bool write_cpustate_to_list(ARMCPU *cpu)
204{
205 /* Write the coprocessor state from cpu->env to the (index,value) list. */
206 int i;
207 bool ok = true;
208
209 for (i = 0; i < cpu->cpreg_array_len; i++) {
210 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
211 const ARMCPRegInfo *ri;
59a1c327 212
60322b39 213 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
214 if (!ri) {
215 ok = false;
216 continue;
217 }
7a0e58fa 218 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
219 continue;
220 }
59a1c327 221 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
222 }
223 return ok;
224}
225
226bool write_list_to_cpustate(ARMCPU *cpu)
227{
228 int i;
229 bool ok = true;
230
231 for (i = 0; i < cpu->cpreg_array_len; i++) {
232 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
233 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
234 const ARMCPRegInfo *ri;
235
60322b39 236 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
237 if (!ri) {
238 ok = false;
239 continue;
240 }
7a0e58fa 241 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
242 continue;
243 }
244 /* Write value and confirm it reads back as written
245 * (to catch read-only registers and partially read-only
246 * registers where the incoming migration value doesn't match)
247 */
59a1c327
PM
248 write_raw_cp_reg(&cpu->env, ri, v);
249 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
250 ok = false;
251 }
252 }
253 return ok;
254}
255
256static void add_cpreg_to_list(gpointer key, gpointer opaque)
257{
258 ARMCPU *cpu = opaque;
259 uint64_t regidx;
260 const ARMCPRegInfo *ri;
261
262 regidx = *(uint32_t *)key;
60322b39 263 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 264
7a0e58fa 265 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
266 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
267 /* The value array need not be initialized at this point */
268 cpu->cpreg_array_len++;
269 }
270}
271
272static void count_cpreg(gpointer key, gpointer opaque)
273{
274 ARMCPU *cpu = opaque;
275 uint64_t regidx;
276 const ARMCPRegInfo *ri;
277
278 regidx = *(uint32_t *)key;
60322b39 279 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 280
7a0e58fa 281 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
282 cpu->cpreg_array_len++;
283 }
284}
285
286static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
287{
cbf239b7
AR
288 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
289 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 290
cbf239b7
AR
291 if (aidx > bidx) {
292 return 1;
293 }
294 if (aidx < bidx) {
295 return -1;
296 }
297 return 0;
721fae12
PM
298}
299
300void init_cpreg_list(ARMCPU *cpu)
301{
302 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
303 * Note that we require cpreg_tuples[] to be sorted by key ID.
304 */
57b6d95e 305 GList *keys;
721fae12
PM
306 int arraylen;
307
57b6d95e 308 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
309 keys = g_list_sort(keys, cpreg_key_compare);
310
311 cpu->cpreg_array_len = 0;
312
313 g_list_foreach(keys, count_cpreg, cpu);
314
315 arraylen = cpu->cpreg_array_len;
316 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
317 cpu->cpreg_values = g_new(uint64_t, arraylen);
318 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
319 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
320 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
321 cpu->cpreg_array_len = 0;
322
323 g_list_foreach(keys, add_cpreg_to_list, cpu);
324
325 assert(cpu->cpreg_array_len == arraylen);
326
327 g_list_free(keys);
328}
329
68e9c2fe
EI
330/*
331 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
332 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
333 *
334 * access_el3_aa32ns: Used to check AArch32 register views.
335 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
336 */
337static CPAccessResult access_el3_aa32ns(CPUARMState *env,
338 const ARMCPRegInfo *ri)
339{
340 bool secure = arm_is_secure_below_el3(env);
341
342 assert(!arm_el_is_aa64(env, 3));
343 if (secure) {
344 return CP_ACCESS_TRAP_UNCATEGORIZED;
345 }
346 return CP_ACCESS_OK;
347}
348
349static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
350 const ARMCPRegInfo *ri)
351{
352 if (!arm_el_is_aa64(env, 3)) {
353 return access_el3_aa32ns(env, ri);
354 }
355 return CP_ACCESS_OK;
356}
357
c4241c7d 358static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 359{
00c8cb0a
AF
360 ARMCPU *cpu = arm_env_get_cpu(env);
361
8d5c773e 362 raw_write(env, ri, value);
00c8cb0a 363 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
364}
365
c4241c7d 366static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 367{
00c8cb0a
AF
368 ARMCPU *cpu = arm_env_get_cpu(env);
369
8d5c773e 370 if (raw_read(env, ri) != value) {
08de207b
PM
371 /* Unlike real hardware the qemu TLB uses virtual addresses,
372 * not modified virtual addresses, so this causes a TLB flush.
373 */
00c8cb0a 374 tlb_flush(CPU(cpu), 1);
8d5c773e 375 raw_write(env, ri, value);
08de207b 376 }
08de207b 377}
c4241c7d
PM
378
379static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
380 uint64_t value)
08de207b 381{
00c8cb0a
AF
382 ARMCPU *cpu = arm_env_get_cpu(env);
383
8d5c773e 384 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
014406b5 385 && !extended_addresses_enabled(env)) {
08de207b
PM
386 /* For VMSA (when not using the LPAE long descriptor page table
387 * format) this register includes the ASID, so do a TLB flush.
388 * For PMSA it is purely a process ID and no action is needed.
389 */
00c8cb0a 390 tlb_flush(CPU(cpu), 1);
08de207b 391 }
8d5c773e 392 raw_write(env, ri, value);
08de207b
PM
393}
394
c4241c7d
PM
395static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
396 uint64_t value)
d929823f
PM
397{
398 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
399 ARMCPU *cpu = arm_env_get_cpu(env);
400
401 tlb_flush(CPU(cpu), 1);
d929823f
PM
402}
403
c4241c7d
PM
404static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
405 uint64_t value)
d929823f
PM
406{
407 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
408 ARMCPU *cpu = arm_env_get_cpu(env);
409
410 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
411}
412
c4241c7d
PM
413static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
414 uint64_t value)
d929823f
PM
415{
416 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
417 ARMCPU *cpu = arm_env_get_cpu(env);
418
419 tlb_flush(CPU(cpu), value == 0);
d929823f
PM
420}
421
c4241c7d
PM
422static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
423 uint64_t value)
d929823f
PM
424{
425 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
426 ARMCPU *cpu = arm_env_get_cpu(env);
427
428 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
429}
430
fa439fc5
PM
431/* IS variants of TLB operations must affect all cores */
432static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
433 uint64_t value)
434{
435 CPUState *other_cs;
436
437 CPU_FOREACH(other_cs) {
438 tlb_flush(other_cs, 1);
439 }
440}
441
442static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
443 uint64_t value)
444{
445 CPUState *other_cs;
446
447 CPU_FOREACH(other_cs) {
448 tlb_flush(other_cs, value == 0);
449 }
450}
451
452static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
453 uint64_t value)
454{
455 CPUState *other_cs;
456
457 CPU_FOREACH(other_cs) {
458 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
459 }
460}
461
462static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
463 uint64_t value)
464{
465 CPUState *other_cs;
466
467 CPU_FOREACH(other_cs) {
468 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
469 }
470}
471
e9aa6c21 472static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
473 /* Define the secure and non-secure FCSE identifier CP registers
474 * separately because there is no secure bank in V8 (no _EL3). This allows
475 * the secure register to be properly reset and migrated. There is also no
476 * v8 EL1 version of the register so the non-secure instance stands alone.
477 */
478 { .name = "FCSEIDR(NS)",
479 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
480 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
481 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
482 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
483 { .name = "FCSEIDR(S)",
484 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
485 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
486 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 487 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
488 /* Define the secure and non-secure context identifier CP registers
489 * separately because there is no secure bank in V8 (no _EL3). This allows
490 * the secure register to be properly reset and migrated. In the
491 * non-secure case, the 32-bit register will have reset and migration
492 * disabled during registration as it is handled by the 64-bit instance.
493 */
494 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 495 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
496 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
497 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
498 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
499 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
500 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
501 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
502 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 503 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
504 REGINFO_SENTINEL
505};
506
507static const ARMCPRegInfo not_v8_cp_reginfo[] = {
508 /* NB: Some of these registers exist in v8 but with more precise
509 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
510 */
511 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
512 { .name = "DACR",
513 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
514 .access = PL1_RW, .resetvalue = 0,
515 .writefn = dacr_write, .raw_writefn = raw_write,
516 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
517 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
518 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
519 * For v6 and v5, these mappings are overly broad.
4fdd17dd 520 */
a903c449
EI
521 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
522 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
523 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
524 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
525 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
526 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
527 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 528 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
529 /* Cache maintenance ops; some of this space may be overridden later. */
530 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
531 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
532 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
533 REGINFO_SENTINEL
534};
535
7d57f408
PM
536static const ARMCPRegInfo not_v6_cp_reginfo[] = {
537 /* Not all pre-v6 cores implemented this WFI, so this is slightly
538 * over-broad.
539 */
540 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
541 .access = PL1_W, .type = ARM_CP_WFI },
542 REGINFO_SENTINEL
543};
544
545static const ARMCPRegInfo not_v7_cp_reginfo[] = {
546 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
547 * is UNPREDICTABLE; we choose to NOP as most implementations do).
548 */
549 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
550 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
551 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
552 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
553 * OMAPCP will override this space.
554 */
555 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
556 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
557 .resetvalue = 0 },
558 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
559 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
560 .resetvalue = 0 },
776d4e5c
PM
561 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
562 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 563 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 564 .resetvalue = 0 },
50300698
PM
565 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
566 * implementing it as RAZ means the "debug architecture version" bits
567 * will read as a reserved value, which should cause Linux to not try
568 * to use the debug hardware.
569 */
570 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
571 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
572 /* MMU TLB control. Note that the wildcarding means we cover not just
573 * the unified TLB ops but also the dside/iside/inner-shareable variants.
574 */
575 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
576 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 577 .type = ARM_CP_NO_RAW },
995939a6
PM
578 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
579 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 580 .type = ARM_CP_NO_RAW },
995939a6
PM
581 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
582 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 583 .type = ARM_CP_NO_RAW },
995939a6
PM
584 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
585 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 586 .type = ARM_CP_NO_RAW },
a903c449
EI
587 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
588 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
589 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
590 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
591 REGINFO_SENTINEL
592};
593
c4241c7d
PM
594static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
595 uint64_t value)
2771db27 596{
f0aff255
FA
597 uint32_t mask = 0;
598
599 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
600 if (!arm_feature(env, ARM_FEATURE_V8)) {
601 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
602 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
603 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
604 */
605 if (arm_feature(env, ARM_FEATURE_VFP)) {
606 /* VFP coprocessor: cp10 & cp11 [23:20] */
607 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
608
609 if (!arm_feature(env, ARM_FEATURE_NEON)) {
610 /* ASEDIS [31] bit is RAO/WI */
611 value |= (1 << 31);
612 }
613
614 /* VFPv3 and upwards with NEON implement 32 double precision
615 * registers (D0-D31).
616 */
617 if (!arm_feature(env, ARM_FEATURE_NEON) ||
618 !arm_feature(env, ARM_FEATURE_VFP3)) {
619 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
620 value |= (1 << 30);
621 }
622 }
623 value &= mask;
2771db27 624 }
7ebd5f2e 625 env->cp15.cpacr_el1 = value;
2771db27
PM
626}
627
c6f19164
GB
628static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri)
629{
630 if (arm_feature(env, ARM_FEATURE_V8)) {
631 /* Check if CPACR accesses are to be trapped to EL2 */
632 if (arm_current_el(env) == 1 &&
633 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
634 return CP_ACCESS_TRAP_EL2;
635 /* Check if CPACR accesses are to be trapped to EL3 */
636 } else if (arm_current_el(env) < 3 &&
637 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
638 return CP_ACCESS_TRAP_EL3;
639 }
640 }
641
642 return CP_ACCESS_OK;
643}
644
645static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri)
646{
647 /* Check if CPTR accesses are set to trap to EL3 */
648 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
649 return CP_ACCESS_TRAP_EL3;
650 }
651
652 return CP_ACCESS_OK;
653}
654
7d57f408
PM
655static const ARMCPRegInfo v6_cp_reginfo[] = {
656 /* prefetch by MVA in v6, NOP in v7 */
657 { .name = "MVA_prefetch",
658 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
659 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
660 /* We need to break the TB after ISB to execute self-modifying code
661 * correctly and also to take any pending interrupts immediately.
662 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
663 */
7d57f408 664 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 665 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 666 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 667 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 668 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 669 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 670 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 671 .access = PL1_RW,
b848ce2b
FA
672 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
673 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
674 .resetvalue = 0, },
675 /* Watchpoint Fault Address Register : should actually only be present
676 * for 1136, 1176, 11MPCore.
677 */
678 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
679 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 680 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 681 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 682 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 683 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
684 REGINFO_SENTINEL
685};
686
fcd25206 687static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
200ac0ef 688{
3b163b01 689 /* Performance monitor registers user accessibility is controlled
fcd25206 690 * by PMUSERENR.
200ac0ef 691 */
dcbff19b 692 if (arm_current_el(env) == 0 && !env->cp15.c9_pmuserenr) {
fcd25206 693 return CP_ACCESS_TRAP;
200ac0ef 694 }
fcd25206 695 return CP_ACCESS_OK;
200ac0ef
PM
696}
697
7c2cb42b 698#ifndef CONFIG_USER_ONLY
87124fde
AF
699
700static inline bool arm_ccnt_enabled(CPUARMState *env)
701{
702 /* This does not support checking PMCCFILTR_EL0 register */
703
704 if (!(env->cp15.c9_pmcr & PMCRE)) {
705 return false;
706 }
707
708 return true;
709}
710
ec7b4ce4
AF
711void pmccntr_sync(CPUARMState *env)
712{
713 uint64_t temp_ticks;
714
352c98e5
LV
715 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
716 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
717
718 if (env->cp15.c9_pmcr & PMCRD) {
719 /* Increment once every 64 processor clock cycles */
720 temp_ticks /= 64;
721 }
722
723 if (arm_ccnt_enabled(env)) {
724 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
725 }
726}
727
c4241c7d
PM
728static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
729 uint64_t value)
200ac0ef 730{
942a155b 731 pmccntr_sync(env);
7c2cb42b
AF
732
733 if (value & PMCRC) {
734 /* The counter has been reset */
735 env->cp15.c15_ccnt = 0;
736 }
737
200ac0ef
PM
738 /* only the DP, X, D and E bits are writable */
739 env->cp15.c9_pmcr &= ~0x39;
740 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 741
942a155b 742 pmccntr_sync(env);
7c2cb42b
AF
743}
744
745static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
746{
c92c0687 747 uint64_t total_ticks;
7c2cb42b 748
942a155b 749 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
750 /* Counter is disabled, do not change value */
751 return env->cp15.c15_ccnt;
752 }
753
352c98e5
LV
754 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
755 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
756
757 if (env->cp15.c9_pmcr & PMCRD) {
758 /* Increment once every 64 processor clock cycles */
759 total_ticks /= 64;
760 }
761 return total_ticks - env->cp15.c15_ccnt;
762}
763
764static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
765 uint64_t value)
766{
c92c0687 767 uint64_t total_ticks;
7c2cb42b 768
942a155b 769 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
770 /* Counter is disabled, set the absolute value */
771 env->cp15.c15_ccnt = value;
772 return;
773 }
774
352c98e5
LV
775 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
776 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
777
778 if (env->cp15.c9_pmcr & PMCRD) {
779 /* Increment once every 64 processor clock cycles */
780 total_ticks /= 64;
781 }
782 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 783}
421c7ebd
PC
784
785static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
786 uint64_t value)
787{
788 uint64_t cur_val = pmccntr_read(env, NULL);
789
790 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
791}
792
ec7b4ce4
AF
793#else /* CONFIG_USER_ONLY */
794
795void pmccntr_sync(CPUARMState *env)
796{
797}
798
7c2cb42b 799#endif
200ac0ef 800
0614601c
AF
801static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
802 uint64_t value)
803{
804 pmccntr_sync(env);
805 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
806 pmccntr_sync(env);
807}
808
c4241c7d 809static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
810 uint64_t value)
811{
200ac0ef
PM
812 value &= (1 << 31);
813 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
814}
815
c4241c7d
PM
816static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
817 uint64_t value)
200ac0ef 818{
200ac0ef
PM
819 value &= (1 << 31);
820 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
821}
822
c4241c7d
PM
823static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
824 uint64_t value)
200ac0ef 825{
200ac0ef 826 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
827}
828
c4241c7d
PM
829static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
830 uint64_t value)
200ac0ef 831{
200ac0ef 832 env->cp15.c9_pmxevtyper = value & 0xff;
200ac0ef
PM
833}
834
c4241c7d 835static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
836 uint64_t value)
837{
838 env->cp15.c9_pmuserenr = value & 1;
200ac0ef
PM
839}
840
c4241c7d
PM
841static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
842 uint64_t value)
200ac0ef
PM
843{
844 /* We have no event counters so only the C bit can be changed */
845 value &= (1 << 31);
846 env->cp15.c9_pminten |= value;
200ac0ef
PM
847}
848
c4241c7d
PM
849static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
850 uint64_t value)
200ac0ef
PM
851{
852 value &= (1 << 31);
853 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
854}
855
c4241c7d
PM
856static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
857 uint64_t value)
8641136c 858{
a505d7fe
PM
859 /* Note that even though the AArch64 view of this register has bits
860 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
861 * architectural requirements for bits which are RES0 only in some
862 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
863 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
864 */
855ea66d 865 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
866}
867
64e0e2de
EI
868static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
869{
870 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
871 * For bits that vary between AArch32/64, code needs to check the
872 * current execution mode before directly using the feature bit.
873 */
874 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
875
876 if (!arm_feature(env, ARM_FEATURE_EL2)) {
877 valid_mask &= ~SCR_HCE;
878
879 /* On ARMv7, SMD (or SCD as it is called in v7) is only
880 * supported if EL2 exists. The bit is UNK/SBZP when
881 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
882 * when EL2 is unavailable.
4eb27640 883 * On ARMv8, this bit is always available.
64e0e2de 884 */
4eb27640
GB
885 if (arm_feature(env, ARM_FEATURE_V7) &&
886 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
887 valid_mask &= ~SCR_SMD;
888 }
889 }
890
891 /* Clear all-context RES0 bits. */
892 value &= valid_mask;
893 raw_write(env, ri, value);
894}
895
c4241c7d 896static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
897{
898 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
899
900 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
901 * bank
902 */
903 uint32_t index = A32_BANKED_REG_GET(env, csselr,
904 ri->secure & ARM_CP_SECSTATE_S);
905
906 return cpu->ccsidr[index];
776d4e5c
PM
907}
908
c4241c7d
PM
909static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
910 uint64_t value)
776d4e5c 911{
8d5c773e 912 raw_write(env, ri, value & 0xf);
776d4e5c
PM
913}
914
1090b9c6
PM
915static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
916{
917 CPUState *cs = ENV_GET_CPU(env);
918 uint64_t ret = 0;
919
920 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
921 ret |= CPSR_I;
922 }
923 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
924 ret |= CPSR_F;
925 }
926 /* External aborts are not possible in QEMU so A bit is always clear */
927 return ret;
928}
929
e9aa6c21 930static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
931 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
932 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
933 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
934 /* Performance monitors are implementation defined in v7,
935 * but with an ARM recommended set of registers, which we
936 * follow (although we don't actually implement any counters)
937 *
938 * Performance registers fall into three categories:
939 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
940 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
941 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
942 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
943 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
944 */
945 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 946 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 947 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
948 .writefn = pmcntenset_write,
949 .accessfn = pmreg_access,
950 .raw_writefn = raw_write },
8521466b
AF
951 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
952 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
953 .access = PL0_RW, .accessfn = pmreg_access,
954 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
955 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 956 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
957 .access = PL0_RW,
958 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
959 .accessfn = pmreg_access,
960 .writefn = pmcntenclr_write,
7a0e58fa 961 .type = ARM_CP_ALIAS },
8521466b
AF
962 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
963 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
964 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 965 .type = ARM_CP_ALIAS,
8521466b
AF
966 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
967 .writefn = pmcntenclr_write },
200ac0ef
PM
968 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
969 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
970 .accessfn = pmreg_access,
971 .writefn = pmovsr_write,
972 .raw_writefn = raw_write },
973 /* Unimplemented so WI. */
200ac0ef 974 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
fcd25206 975 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
200ac0ef 976 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
fcd25206 977 * We choose to RAZ/WI.
200ac0ef
PM
978 */
979 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
fcd25206
PM
980 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
981 .accessfn = pmreg_access },
7c2cb42b 982#ifndef CONFIG_USER_ONLY
200ac0ef 983 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 984 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 985 .readfn = pmccntr_read, .writefn = pmccntr_write32,
fcd25206 986 .accessfn = pmreg_access },
8521466b
AF
987 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
988 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
989 .access = PL0_RW, .accessfn = pmreg_access,
990 .type = ARM_CP_IO,
991 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 992#endif
8521466b
AF
993 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
994 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 995 .writefn = pmccfiltr_write,
8521466b
AF
996 .access = PL0_RW, .accessfn = pmreg_access,
997 .type = ARM_CP_IO,
998 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
999 .resetvalue = 0, },
200ac0ef
PM
1000 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1001 .access = PL0_RW,
1002 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
fcd25206
PM
1003 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
1004 .raw_writefn = raw_write },
1005 /* Unimplemented, RAZ/WI. */
200ac0ef 1006 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206
PM
1007 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1008 .accessfn = pmreg_access },
200ac0ef
PM
1009 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1010 .access = PL0_R | PL1_RW,
1011 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1012 .resetvalue = 0,
d4e6df63 1013 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef
PM
1014 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1015 .access = PL1_RW,
1016 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1017 .resetvalue = 0,
d4e6df63 1018 .writefn = pmintenset_write, .raw_writefn = raw_write },
200ac0ef 1019 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
7a0e58fa 1020 .access = PL1_RW, .type = ARM_CP_ALIAS,
200ac0ef 1021 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1022 .writefn = pmintenclr_write, },
a505d7fe
PM
1023 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
1024 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8641136c 1025 .access = PL1_RW, .writefn = vbar_write,
fb6c91ba
GB
1026 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
1027 offsetof(CPUARMState, cp15.vbar_ns) },
8641136c 1028 .resetvalue = 0 },
7da845b0
PM
1029 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1030 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1031 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1032 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1033 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1034 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1035 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1036 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1037 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1038 * just RAZ for all cores:
1039 */
0ff644a7
PM
1040 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1041 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1042 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1043 /* Auxiliary fault status registers: these also are IMPDEF, and we
1044 * choose to RAZ/WI for all cores.
1045 */
1046 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1047 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1048 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1049 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1050 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1051 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1052 /* MAIR can just read-as-written because we don't implement caches
1053 * and so don't need to care about memory attributes.
1054 */
1055 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1056 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1057 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1058 .resetvalue = 0 },
4cfb8ad8
PM
1059 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1060 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1061 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1062 .resetvalue = 0 },
b0fe2427
PM
1063 /* For non-long-descriptor page tables these are PRRR and NMRR;
1064 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1065 */
1281f8e3 1066 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1067 * allows them to assign the correct fieldoffset based on the endianness
1068 * handled in the field definitions.
1069 */
a903c449 1070 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1071 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1072 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1073 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1074 .resetfn = arm_cp_reset_ignore },
a903c449 1075 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1076 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1077 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1078 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1079 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1080 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1081 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1082 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1083 /* 32 bit ITLB invalidates */
1084 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1085 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1086 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1087 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1088 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1089 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1090 /* 32 bit DTLB invalidates */
1091 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1092 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1093 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1094 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1095 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1096 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1097 /* 32 bit TLB invalidates */
1098 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1099 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1100 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1101 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1102 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1103 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1104 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1105 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1106 REGINFO_SENTINEL
1107};
1108
1109static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1110 /* 32 bit TLB invalidates, Inner Shareable */
1111 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1112 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1113 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1114 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1115 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1116 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1117 .writefn = tlbiasid_is_write },
995939a6 1118 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1119 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1120 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1121 REGINFO_SENTINEL
1122};
1123
c4241c7d
PM
1124static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1125 uint64_t value)
c326b979
PM
1126{
1127 value &= 1;
1128 env->teecr = value;
c326b979
PM
1129}
1130
c4241c7d 1131static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
c326b979 1132{
dcbff19b 1133 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1134 return CP_ACCESS_TRAP;
c326b979 1135 }
92611c00 1136 return CP_ACCESS_OK;
c326b979
PM
1137}
1138
1139static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1140 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1141 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1142 .resetvalue = 0,
1143 .writefn = teecr_write },
1144 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1145 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1146 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1147 REGINFO_SENTINEL
1148};
1149
4d31c596 1150static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1151 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1152 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1153 .access = PL0_RW,
54bf36ed 1154 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1155 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1156 .access = PL0_RW,
54bf36ed
FA
1157 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1158 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1159 .resetfn = arm_cp_reset_ignore },
1160 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1161 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1162 .access = PL0_R|PL1_W,
54bf36ed
FA
1163 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1164 .resetvalue = 0},
4d31c596
PM
1165 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1166 .access = PL0_R|PL1_W,
54bf36ed
FA
1167 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1168 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1169 .resetfn = arm_cp_reset_ignore },
54bf36ed 1170 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1171 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1172 .access = PL1_RW,
54bf36ed
FA
1173 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1174 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1175 .access = PL1_RW,
1176 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1177 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1178 .resetvalue = 0 },
4d31c596
PM
1179 REGINFO_SENTINEL
1180};
1181
55d284af
PM
1182#ifndef CONFIG_USER_ONLY
1183
00108f2d
PM
1184static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
1185{
1186 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
dcbff19b 1187 if (arm_current_el(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
00108f2d
PM
1188 return CP_ACCESS_TRAP;
1189 }
1190 return CP_ACCESS_OK;
1191}
1192
1193static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
1194{
0b6440af
EI
1195 unsigned int cur_el = arm_current_el(env);
1196 bool secure = arm_is_secure(env);
1197
00108f2d 1198 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1199 if (cur_el == 0 &&
00108f2d
PM
1200 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1201 return CP_ACCESS_TRAP;
1202 }
0b6440af
EI
1203
1204 if (arm_feature(env, ARM_FEATURE_EL2) &&
1205 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1206 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1207 return CP_ACCESS_TRAP_EL2;
1208 }
00108f2d
PM
1209 return CP_ACCESS_OK;
1210}
1211
1212static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
1213{
0b6440af
EI
1214 unsigned int cur_el = arm_current_el(env);
1215 bool secure = arm_is_secure(env);
1216
00108f2d
PM
1217 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1218 * EL0[PV]TEN is zero.
1219 */
0b6440af 1220 if (cur_el == 0 &&
00108f2d
PM
1221 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1222 return CP_ACCESS_TRAP;
1223 }
0b6440af
EI
1224
1225 if (arm_feature(env, ARM_FEATURE_EL2) &&
1226 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1227 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1228 return CP_ACCESS_TRAP_EL2;
1229 }
00108f2d
PM
1230 return CP_ACCESS_OK;
1231}
1232
1233static CPAccessResult gt_pct_access(CPUARMState *env,
1234 const ARMCPRegInfo *ri)
1235{
1236 return gt_counter_access(env, GTIMER_PHYS);
1237}
1238
1239static CPAccessResult gt_vct_access(CPUARMState *env,
1240 const ARMCPRegInfo *ri)
1241{
1242 return gt_counter_access(env, GTIMER_VIRT);
1243}
1244
1245static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1246{
1247 return gt_timer_access(env, GTIMER_PHYS);
1248}
1249
1250static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1251{
1252 return gt_timer_access(env, GTIMER_VIRT);
1253}
1254
b4d3978c
PM
1255static CPAccessResult gt_stimer_access(CPUARMState *env,
1256 const ARMCPRegInfo *ri)
1257{
1258 /* The AArch64 register view of the secure physical timer is
1259 * always accessible from EL3, and configurably accessible from
1260 * Secure EL1.
1261 */
1262 switch (arm_current_el(env)) {
1263 case 1:
1264 if (!arm_is_secure(env)) {
1265 return CP_ACCESS_TRAP;
1266 }
1267 if (!(env->cp15.scr_el3 & SCR_ST)) {
1268 return CP_ACCESS_TRAP_EL3;
1269 }
1270 return CP_ACCESS_OK;
1271 case 0:
1272 case 2:
1273 return CP_ACCESS_TRAP;
1274 case 3:
1275 return CP_ACCESS_OK;
1276 default:
1277 g_assert_not_reached();
1278 }
1279}
1280
55d284af
PM
1281static uint64_t gt_get_countervalue(CPUARMState *env)
1282{
bc72ad67 1283 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1284}
1285
1286static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1287{
1288 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1289
1290 if (gt->ctl & 1) {
1291 /* Timer enabled: calculate and set current ISTATUS, irq, and
1292 * reset timer to when ISTATUS next has to change
1293 */
edac4d8a
EI
1294 uint64_t offset = timeridx == GTIMER_VIRT ?
1295 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1296 uint64_t count = gt_get_countervalue(&cpu->env);
1297 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1298 int istatus = count - offset >= gt->cval;
55d284af
PM
1299 uint64_t nexttick;
1300
1301 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1302 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1303 (istatus && !(gt->ctl & 2)));
1304 if (istatus) {
1305 /* Next transition is when count rolls back over to zero */
1306 nexttick = UINT64_MAX;
1307 } else {
1308 /* Next transition is when we hit cval */
edac4d8a 1309 nexttick = gt->cval + offset;
55d284af
PM
1310 }
1311 /* Note that the desired next expiry time might be beyond the
1312 * signed-64-bit range of a QEMUTimer -- in this case we just
1313 * set the timer for as far in the future as possible. When the
1314 * timer expires we will reset the timer for any remaining period.
1315 */
1316 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1317 nexttick = INT64_MAX / GTIMER_SCALE;
1318 }
bc72ad67 1319 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af
PM
1320 } else {
1321 /* Timer disabled: ISTATUS and timer output always clear */
1322 gt->ctl &= ~4;
1323 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1324 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1325 }
1326}
1327
0e3eca4c
EI
1328static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1329 int timeridx)
55d284af
PM
1330{
1331 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1332
bc72ad67 1333 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1334}
1335
c4241c7d 1336static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1337{
c4241c7d 1338 return gt_get_countervalue(env);
55d284af
PM
1339}
1340
edac4d8a
EI
1341static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1342{
1343 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1344}
1345
c4241c7d 1346static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1347 int timeridx,
c4241c7d 1348 uint64_t value)
55d284af 1349{
55d284af
PM
1350 env->cp15.c14_timer[timeridx].cval = value;
1351 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1352}
c4241c7d 1353
0e3eca4c
EI
1354static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1355 int timeridx)
55d284af 1356{
edac4d8a 1357 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1358
c4241c7d 1359 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1360 (gt_get_countervalue(env) - offset));
55d284af
PM
1361}
1362
c4241c7d 1363static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1364 int timeridx,
c4241c7d 1365 uint64_t value)
55d284af 1366{
edac4d8a 1367 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1368
edac4d8a 1369 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1370 sextract64(value, 0, 32);
55d284af 1371 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1372}
1373
c4241c7d 1374static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1375 int timeridx,
c4241c7d 1376 uint64_t value)
55d284af
PM
1377{
1378 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1379 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1380
d3afacc7 1381 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1382 if ((oldval ^ value) & 1) {
1383 /* Enable toggled */
1384 gt_recalc_timer(cpu, timeridx);
d3afacc7 1385 } else if ((oldval ^ value) & 2) {
55d284af
PM
1386 /* IMASK toggled: don't need to recalculate,
1387 * just set the interrupt line based on ISTATUS
1388 */
1389 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
d3afacc7 1390 (oldval & 4) && !(value & 2));
55d284af 1391 }
55d284af
PM
1392}
1393
0e3eca4c
EI
1394static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1395{
1396 gt_timer_reset(env, ri, GTIMER_PHYS);
1397}
1398
1399static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1400 uint64_t value)
1401{
1402 gt_cval_write(env, ri, GTIMER_PHYS, value);
1403}
1404
1405static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1406{
1407 return gt_tval_read(env, ri, GTIMER_PHYS);
1408}
1409
1410static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1411 uint64_t value)
1412{
1413 gt_tval_write(env, ri, GTIMER_PHYS, value);
1414}
1415
1416static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1417 uint64_t value)
1418{
1419 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1420}
1421
1422static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1423{
1424 gt_timer_reset(env, ri, GTIMER_VIRT);
1425}
1426
1427static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1428 uint64_t value)
1429{
1430 gt_cval_write(env, ri, GTIMER_VIRT, value);
1431}
1432
1433static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1434{
1435 return gt_tval_read(env, ri, GTIMER_VIRT);
1436}
1437
1438static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1439 uint64_t value)
1440{
1441 gt_tval_write(env, ri, GTIMER_VIRT, value);
1442}
1443
1444static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1445 uint64_t value)
1446{
1447 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1448}
1449
edac4d8a
EI
1450static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1451 uint64_t value)
1452{
1453 ARMCPU *cpu = arm_env_get_cpu(env);
1454
1455 raw_write(env, ri, value);
1456 gt_recalc_timer(cpu, GTIMER_VIRT);
1457}
1458
b0e66d95
EI
1459static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1460{
1461 gt_timer_reset(env, ri, GTIMER_HYP);
1462}
1463
1464static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1465 uint64_t value)
1466{
1467 gt_cval_write(env, ri, GTIMER_HYP, value);
1468}
1469
1470static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1471{
1472 return gt_tval_read(env, ri, GTIMER_HYP);
1473}
1474
1475static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1476 uint64_t value)
1477{
1478 gt_tval_write(env, ri, GTIMER_HYP, value);
1479}
1480
1481static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1482 uint64_t value)
1483{
1484 gt_ctl_write(env, ri, GTIMER_HYP, value);
1485}
1486
b4d3978c
PM
1487static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1488{
1489 gt_timer_reset(env, ri, GTIMER_SEC);
1490}
1491
1492static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1493 uint64_t value)
1494{
1495 gt_cval_write(env, ri, GTIMER_SEC, value);
1496}
1497
1498static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1499{
1500 return gt_tval_read(env, ri, GTIMER_SEC);
1501}
1502
1503static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504 uint64_t value)
1505{
1506 gt_tval_write(env, ri, GTIMER_SEC, value);
1507}
1508
1509static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1510 uint64_t value)
1511{
1512 gt_ctl_write(env, ri, GTIMER_SEC, value);
1513}
1514
55d284af
PM
1515void arm_gt_ptimer_cb(void *opaque)
1516{
1517 ARMCPU *cpu = opaque;
1518
1519 gt_recalc_timer(cpu, GTIMER_PHYS);
1520}
1521
1522void arm_gt_vtimer_cb(void *opaque)
1523{
1524 ARMCPU *cpu = opaque;
1525
1526 gt_recalc_timer(cpu, GTIMER_VIRT);
1527}
1528
b0e66d95
EI
1529void arm_gt_htimer_cb(void *opaque)
1530{
1531 ARMCPU *cpu = opaque;
1532
1533 gt_recalc_timer(cpu, GTIMER_HYP);
1534}
1535
b4d3978c
PM
1536void arm_gt_stimer_cb(void *opaque)
1537{
1538 ARMCPU *cpu = opaque;
1539
1540 gt_recalc_timer(cpu, GTIMER_SEC);
1541}
1542
55d284af
PM
1543static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1544 /* Note that CNTFRQ is purely reads-as-written for the benefit
1545 * of software; writing it doesn't actually change the timer frequency.
1546 * Our reset value matches the fixed frequency we implement the timer at.
1547 */
1548 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1549 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1550 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1551 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1552 },
1553 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1554 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1555 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1556 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1557 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1558 },
1559 /* overall control: mostly access permissions */
a7adc4b7
PM
1560 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1561 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1562 .access = PL1_RW,
1563 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1564 .resetvalue = 0,
1565 },
1566 /* per-timer control */
1567 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1568 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1569 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1570 .accessfn = gt_ptimer_access,
1571 .fieldoffset = offsetoflow32(CPUARMState,
1572 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1573 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1574 },
9ff9dd3c
PM
1575 { .name = "CNTP_CTL(S)",
1576 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1577 .secure = ARM_CP_SECSTATE_S,
1578 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1579 .accessfn = gt_ptimer_access,
1580 .fieldoffset = offsetoflow32(CPUARMState,
1581 cp15.c14_timer[GTIMER_SEC].ctl),
1582 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1583 },
a7adc4b7
PM
1584 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1585 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1586 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1587 .accessfn = gt_ptimer_access,
55d284af
PM
1588 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1589 .resetvalue = 0,
0e3eca4c 1590 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1591 },
1592 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1593 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1594 .accessfn = gt_vtimer_access,
1595 .fieldoffset = offsetoflow32(CPUARMState,
1596 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1597 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1598 },
1599 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1600 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1601 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1602 .accessfn = gt_vtimer_access,
55d284af
PM
1603 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1604 .resetvalue = 0,
0e3eca4c 1605 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1606 },
1607 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1608 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 1609 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1610 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1611 .accessfn = gt_ptimer_access,
0e3eca4c 1612 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 1613 },
9ff9dd3c
PM
1614 { .name = "CNTP_TVAL(S)",
1615 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1616 .secure = ARM_CP_SECSTATE_S,
1617 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1618 .accessfn = gt_ptimer_access,
1619 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1620 },
a7adc4b7
PM
1621 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1622 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 1623 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
1624 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
1625 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 1626 },
55d284af 1627 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 1628 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1629 .accessfn = gt_vtimer_access,
0e3eca4c 1630 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 1631 },
a7adc4b7
PM
1632 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1633 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 1634 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
1635 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
1636 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 1637 },
55d284af
PM
1638 /* The counter itself */
1639 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 1640 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1641 .accessfn = gt_pct_access,
a7adc4b7
PM
1642 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1643 },
1644 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1645 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 1646 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 1647 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
1648 },
1649 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 1650 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1651 .accessfn = gt_vct_access,
edac4d8a 1652 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
1653 },
1654 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1655 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 1656 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 1657 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
1658 },
1659 /* Comparison value, indicating when the timer goes off */
1660 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 1661 .secure = ARM_CP_SECSTATE_NS,
55d284af 1662 .access = PL1_RW | PL0_R,
7a0e58fa 1663 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1664 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 1665 .accessfn = gt_ptimer_access,
0e3eca4c 1666 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 1667 },
9ff9dd3c
PM
1668 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
1669 .secure = ARM_CP_SECSTATE_S,
1670 .access = PL1_RW | PL0_R,
1671 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1672 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1673 .accessfn = gt_ptimer_access,
1674 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1675 },
a7adc4b7
PM
1676 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1677 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1678 .access = PL1_RW | PL0_R,
1679 .type = ARM_CP_IO,
1680 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 1681 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 1682 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
1683 },
1684 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1685 .access = PL1_RW | PL0_R,
7a0e58fa 1686 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1687 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 1688 .accessfn = gt_vtimer_access,
0e3eca4c 1689 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
1690 },
1691 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1692 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1693 .access = PL1_RW | PL0_R,
1694 .type = ARM_CP_IO,
1695 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1696 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 1697 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 1698 },
b4d3978c
PM
1699 /* Secure timer -- this is actually restricted to only EL3
1700 * and configurably Secure-EL1 via the accessfn.
1701 */
1702 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
1703 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
1704 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
1705 .accessfn = gt_stimer_access,
1706 .readfn = gt_sec_tval_read,
1707 .writefn = gt_sec_tval_write,
1708 .resetfn = gt_sec_timer_reset,
1709 },
1710 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
1711 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
1712 .type = ARM_CP_IO, .access = PL1_RW,
1713 .accessfn = gt_stimer_access,
1714 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
1715 .resetvalue = 0,
1716 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1717 },
1718 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
1719 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
1720 .type = ARM_CP_IO, .access = PL1_RW,
1721 .accessfn = gt_stimer_access,
1722 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1723 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1724 },
55d284af
PM
1725 REGINFO_SENTINEL
1726};
1727
1728#else
1729/* In user-mode none of the generic timer registers are accessible,
bc72ad67 1730 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
1731 * so instead just don't register any of them.
1732 */
6cc7a3ae 1733static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
1734 REGINFO_SENTINEL
1735};
1736
55d284af
PM
1737#endif
1738
c4241c7d 1739static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 1740{
891a2fe7 1741 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 1742 raw_write(env, ri, value);
891a2fe7 1743 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 1744 raw_write(env, ri, value & 0xfffff6ff);
4a501606 1745 } else {
8d5c773e 1746 raw_write(env, ri, value & 0xfffff1ff);
4a501606 1747 }
4a501606
PM
1748}
1749
1750#ifndef CONFIG_USER_ONLY
1751/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 1752
92611c00
PM
1753static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
1754{
1755 if (ri->opc2 & 4) {
87562e4f
PM
1756 /* The ATS12NSO* operations must trap to EL3 if executed in
1757 * Secure EL1 (which can only happen if EL3 is AArch64).
1758 * They are simply UNDEF if executed from NS EL1.
1759 * They function normally from EL2 or EL3.
92611c00 1760 */
87562e4f
PM
1761 if (arm_current_el(env) == 1) {
1762 if (arm_is_secure_below_el3(env)) {
1763 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
1764 }
1765 return CP_ACCESS_TRAP_UNCATEGORIZED;
1766 }
92611c00
PM
1767 }
1768 return CP_ACCESS_OK;
1769}
1770
060e8a48 1771static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
d3649702 1772 int access_type, ARMMMUIdx mmu_idx)
4a501606 1773{
a8170e5e 1774 hwaddr phys_addr;
4a501606
PM
1775 target_ulong page_size;
1776 int prot;
b7cc4e82
PC
1777 uint32_t fsr;
1778 bool ret;
01c097f7 1779 uint64_t par64;
8bf5b6a9 1780 MemTxAttrs attrs = {};
4a501606 1781
d3649702 1782 ret = get_phys_addr(env, value, access_type, mmu_idx,
b7cc4e82 1783 &phys_addr, &attrs, &prot, &page_size, &fsr);
702a9357 1784 if (extended_addresses_enabled(env)) {
b7cc4e82 1785 /* fsr is a DFSR/IFSR value for the long descriptor
702a9357
PM
1786 * translation table format, but with WnR always clear.
1787 * Convert it to a 64-bit PAR.
1788 */
01c097f7 1789 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 1790 if (!ret) {
702a9357 1791 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
1792 if (!attrs.secure) {
1793 par64 |= (1 << 9); /* NS */
1794 }
702a9357 1795 /* We don't set the ATTR or SH fields in the PAR. */
4a501606 1796 } else {
702a9357 1797 par64 |= 1; /* F */
b7cc4e82 1798 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
1799 /* Note that S2WLK and FSTAGE are always zero, because we don't
1800 * implement virtualization and therefore there can't be a stage 2
1801 * fault.
1802 */
4a501606
PM
1803 }
1804 } else {
b7cc4e82 1805 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
1806 * translation table format (with WnR always clear).
1807 * Convert it to a 32-bit PAR.
1808 */
b7cc4e82 1809 if (!ret) {
702a9357
PM
1810 /* We do not set any attribute bits in the PAR */
1811 if (page_size == (1 << 24)
1812 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 1813 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 1814 } else {
01c097f7 1815 par64 = phys_addr & 0xfffff000;
702a9357 1816 }
8bf5b6a9
PM
1817 if (!attrs.secure) {
1818 par64 |= (1 << 9); /* NS */
1819 }
702a9357 1820 } else {
b7cc4e82
PC
1821 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
1822 ((fsr & 0xf) << 1) | 1;
702a9357 1823 }
4a501606 1824 }
060e8a48
PM
1825 return par64;
1826}
1827
1828static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1829{
060e8a48
PM
1830 int access_type = ri->opc2 & 1;
1831 uint64_t par64;
d3649702
PM
1832 ARMMMUIdx mmu_idx;
1833 int el = arm_current_el(env);
1834 bool secure = arm_is_secure_below_el3(env);
060e8a48 1835
d3649702
PM
1836 switch (ri->opc2 & 6) {
1837 case 0:
1838 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
1839 switch (el) {
1840 case 3:
1841 mmu_idx = ARMMMUIdx_S1E3;
1842 break;
1843 case 2:
1844 mmu_idx = ARMMMUIdx_S1NSE1;
1845 break;
1846 case 1:
1847 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
1848 break;
1849 default:
1850 g_assert_not_reached();
1851 }
1852 break;
1853 case 2:
1854 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
1855 switch (el) {
1856 case 3:
1857 mmu_idx = ARMMMUIdx_S1SE0;
1858 break;
1859 case 2:
1860 mmu_idx = ARMMMUIdx_S1NSE0;
1861 break;
1862 case 1:
1863 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
1864 break;
1865 default:
1866 g_assert_not_reached();
1867 }
1868 break;
1869 case 4:
1870 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
1871 mmu_idx = ARMMMUIdx_S12NSE1;
1872 break;
1873 case 6:
1874 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
1875 mmu_idx = ARMMMUIdx_S12NSE0;
1876 break;
1877 default:
1878 g_assert_not_reached();
1879 }
1880
1881 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
1882
1883 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 1884}
060e8a48 1885
14db7fe0
PM
1886static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
1887 uint64_t value)
1888{
1889 int access_type = ri->opc2 & 1;
1890 uint64_t par64;
1891
1892 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
1893
1894 A32_BANKED_CURRENT_REG_SET(env, par, par64);
1895}
1896
2a47df95
PM
1897static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri)
1898{
1899 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
1900 return CP_ACCESS_TRAP;
1901 }
1902 return CP_ACCESS_OK;
1903}
1904
060e8a48
PM
1905static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
1906 uint64_t value)
1907{
060e8a48 1908 int access_type = ri->opc2 & 1;
d3649702
PM
1909 ARMMMUIdx mmu_idx;
1910 int secure = arm_is_secure_below_el3(env);
1911
1912 switch (ri->opc2 & 6) {
1913 case 0:
1914 switch (ri->opc1) {
1915 case 0: /* AT S1E1R, AT S1E1W */
1916 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
1917 break;
1918 case 4: /* AT S1E2R, AT S1E2W */
1919 mmu_idx = ARMMMUIdx_S1E2;
1920 break;
1921 case 6: /* AT S1E3R, AT S1E3W */
1922 mmu_idx = ARMMMUIdx_S1E3;
1923 break;
1924 default:
1925 g_assert_not_reached();
1926 }
1927 break;
1928 case 2: /* AT S1E0R, AT S1E0W */
1929 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
1930 break;
1931 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 1932 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
1933 break;
1934 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 1935 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
1936 break;
1937 default:
1938 g_assert_not_reached();
1939 }
060e8a48 1940
d3649702 1941 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 1942}
4a501606
PM
1943#endif
1944
1945static const ARMCPRegInfo vapa_cp_reginfo[] = {
1946 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1947 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
1948 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
1949 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
1950 .writefn = par_write },
1951#ifndef CONFIG_USER_ONLY
87562e4f 1952 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 1953 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 1954 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 1955 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
1956#endif
1957 REGINFO_SENTINEL
1958};
1959
18032bec
PM
1960/* Return basic MPU access permission bits. */
1961static uint32_t simple_mpu_ap_bits(uint32_t val)
1962{
1963 uint32_t ret;
1964 uint32_t mask;
1965 int i;
1966 ret = 0;
1967 mask = 3;
1968 for (i = 0; i < 16; i += 2) {
1969 ret |= (val >> i) & mask;
1970 mask <<= 2;
1971 }
1972 return ret;
1973}
1974
1975/* Pad basic MPU access permission bits to extended format. */
1976static uint32_t extended_mpu_ap_bits(uint32_t val)
1977{
1978 uint32_t ret;
1979 uint32_t mask;
1980 int i;
1981 ret = 0;
1982 mask = 3;
1983 for (i = 0; i < 16; i += 2) {
1984 ret |= (val & mask) << i;
1985 mask <<= 2;
1986 }
1987 return ret;
1988}
1989
c4241c7d
PM
1990static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1991 uint64_t value)
18032bec 1992{
7e09797c 1993 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
1994}
1995
c4241c7d 1996static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 1997{
7e09797c 1998 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
1999}
2000
c4241c7d
PM
2001static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2002 uint64_t value)
18032bec 2003{
7e09797c 2004 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2005}
2006
c4241c7d 2007static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2008{
7e09797c 2009 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2010}
2011
6cb0b013
PC
2012static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2013{
2014 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2015
2016 if (!u32p) {
2017 return 0;
2018 }
2019
2020 u32p += env->cp15.c6_rgnr;
2021 return *u32p;
2022}
2023
2024static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2025 uint64_t value)
2026{
2027 ARMCPU *cpu = arm_env_get_cpu(env);
2028 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2029
2030 if (!u32p) {
2031 return;
2032 }
2033
2034 u32p += env->cp15.c6_rgnr;
2035 tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */
2036 *u32p = value;
2037}
2038
2039static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2040{
2041 ARMCPU *cpu = arm_env_get_cpu(env);
2042 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2043
2044 if (!u32p) {
2045 return;
2046 }
2047
2048 memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion);
2049}
2050
2051static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2052 uint64_t value)
2053{
2054 ARMCPU *cpu = arm_env_get_cpu(env);
2055 uint32_t nrgs = cpu->pmsav7_dregion;
2056
2057 if (value >= nrgs) {
2058 qemu_log_mask(LOG_GUEST_ERROR,
2059 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2060 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2061 return;
2062 }
2063
2064 raw_write(env, ri, value);
2065}
2066
2067static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2068 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2069 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2070 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2071 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2072 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2073 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2074 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2075 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2076 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2077 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2078 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2079 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2080 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2081 .access = PL1_RW,
2082 .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr),
2083 .writefn = pmsav7_rgnr_write },
2084 REGINFO_SENTINEL
2085};
2086
18032bec
PM
2087static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2088 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2089 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2090 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2091 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2092 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2093 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2094 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2095 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2096 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2097 .access = PL1_RW,
7e09797c
PM
2098 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2099 .resetvalue = 0, },
18032bec
PM
2100 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2101 .access = PL1_RW,
7e09797c
PM
2102 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2103 .resetvalue = 0, },
ecce5c3c
PM
2104 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2105 .access = PL1_RW,
2106 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2107 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2108 .access = PL1_RW,
2109 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2110 /* Protection region base and size registers */
e508a92b
PM
2111 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2112 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2113 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2114 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2115 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2116 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2117 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2118 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2119 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2120 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2121 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2122 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2123 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2124 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2125 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2126 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2127 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2128 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2129 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2130 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2131 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2132 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2133 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2134 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2135 REGINFO_SENTINEL
2136};
2137
c4241c7d
PM
2138static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2139 uint64_t value)
ecce5c3c 2140{
11f136ee 2141 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2142 int maskshift = extract32(value, 0, 3);
2143
e389be16
FA
2144 if (!arm_feature(env, ARM_FEATURE_V8)) {
2145 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2146 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2147 * using Long-desciptor translation table format */
2148 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2149 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2150 /* In an implementation that includes the Security Extensions
2151 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2152 * Short-descriptor translation table format.
2153 */
2154 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2155 } else {
2156 value &= TTBCR_N;
2157 }
e42c4db3 2158 }
e389be16 2159
b6af0975 2160 /* Update the masks corresponding to the TCR bank being written
11f136ee 2161 * Note that we always calculate mask and base_mask, but
e42c4db3 2162 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2163 * for long-descriptor tables the TCR fields are used differently
2164 * and the mask and base_mask values are meaningless.
e42c4db3 2165 */
11f136ee
FA
2166 tcr->raw_tcr = value;
2167 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2168 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2169}
2170
c4241c7d
PM
2171static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2172 uint64_t value)
d4e6df63 2173{
00c8cb0a
AF
2174 ARMCPU *cpu = arm_env_get_cpu(env);
2175
d4e6df63
PM
2176 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2177 /* With LPAE the TTBCR could result in a change of ASID
2178 * via the TTBCR.A1 bit, so do a TLB flush.
2179 */
00c8cb0a 2180 tlb_flush(CPU(cpu), 1);
d4e6df63 2181 }
c4241c7d 2182 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2183}
2184
ecce5c3c
PM
2185static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2186{
11f136ee
FA
2187 TCR *tcr = raw_ptr(env, ri);
2188
2189 /* Reset both the TCR as well as the masks corresponding to the bank of
2190 * the TCR being reset.
2191 */
2192 tcr->raw_tcr = 0;
2193 tcr->mask = 0;
2194 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2195}
2196
cb2e37df
PM
2197static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2198 uint64_t value)
2199{
00c8cb0a 2200 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2201 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2202
cb2e37df 2203 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
00c8cb0a 2204 tlb_flush(CPU(cpu), 1);
11f136ee 2205 tcr->raw_tcr = value;
cb2e37df
PM
2206}
2207
327ed10f
PM
2208static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2209 uint64_t value)
2210{
2211 /* 64 bit accesses to the TTBRs can change the ASID and so we
2212 * must flush the TLB.
2213 */
2214 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2215 ARMCPU *cpu = arm_env_get_cpu(env);
2216
2217 tlb_flush(CPU(cpu), 1);
327ed10f
PM
2218 }
2219 raw_write(env, ri, value);
2220}
2221
b698e9cf
EI
2222static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2223 uint64_t value)
2224{
2225 ARMCPU *cpu = arm_env_get_cpu(env);
2226 CPUState *cs = CPU(cpu);
2227
2228 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2229 if (raw_read(env, ri) != value) {
2230 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2231 ARMMMUIdx_S2NS, -1);
2232 raw_write(env, ri, value);
2233 }
2234}
2235
8e5d75c9 2236static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2237 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2238 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2239 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2240 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2241 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2242 .access = PL1_RW, .resetvalue = 0,
2243 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2244 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2245 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2246 .access = PL1_RW, .resetvalue = 0,
2247 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2248 offsetof(CPUARMState, cp15.dfar_ns) } },
2249 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2250 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2251 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2252 .resetvalue = 0, },
2253 REGINFO_SENTINEL
2254};
2255
2256static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2257 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2258 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2259 .access = PL1_RW,
d81c519c 2260 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2261 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2262 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2263 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2264 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2265 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2266 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2267 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2268 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2269 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2270 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2271 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2272 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2273 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2274 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2275 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2276 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2277 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2278 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2279 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2280 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2281 REGINFO_SENTINEL
2282};
2283
c4241c7d
PM
2284static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2285 uint64_t value)
1047b9d7
PM
2286{
2287 env->cp15.c15_ticonfig = value & 0xe7;
2288 /* The OS_TYPE bit in this register changes the reported CPUID! */
2289 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2290 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2291}
2292
c4241c7d
PM
2293static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2294 uint64_t value)
1047b9d7
PM
2295{
2296 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2297}
2298
c4241c7d
PM
2299static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2300 uint64_t value)
1047b9d7
PM
2301{
2302 /* Wait-for-interrupt (deprecated) */
c3affe56 2303 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2304}
2305
c4241c7d
PM
2306static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2307 uint64_t value)
c4804214
PM
2308{
2309 /* On OMAP there are registers indicating the max/min index of dcache lines
2310 * containing a dirty line; cache flush operations have to reset these.
2311 */
2312 env->cp15.c15_i_max = 0x000;
2313 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2314}
2315
18032bec
PM
2316static const ARMCPRegInfo omap_cp_reginfo[] = {
2317 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2318 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2319 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2320 .resetvalue = 0, },
1047b9d7
PM
2321 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2322 .access = PL1_RW, .type = ARM_CP_NOP },
2323 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2324 .access = PL1_RW,
2325 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2326 .writefn = omap_ticonfig_write },
2327 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2328 .access = PL1_RW,
2329 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2330 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2331 .access = PL1_RW, .resetvalue = 0xff0,
2332 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2333 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2334 .access = PL1_RW,
2335 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2336 .writefn = omap_threadid_write },
2337 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2338 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2339 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2340 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2341 /* TODO: Peripheral port remap register:
2342 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2343 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2344 * when MMU is off.
2345 */
c4804214 2346 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2347 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2348 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2349 .writefn = omap_cachemaint_write },
34f90529
PM
2350 { .name = "C9", .cp = 15, .crn = 9,
2351 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2352 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2353 REGINFO_SENTINEL
2354};
2355
c4241c7d
PM
2356static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2357 uint64_t value)
1047b9d7 2358{
c0f4af17 2359 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2360}
2361
2362static const ARMCPRegInfo xscale_cp_reginfo[] = {
2363 { .name = "XSCALE_CPAR",
2364 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2365 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2366 .writefn = xscale_cpar_write, },
2771db27
PM
2367 { .name = "XSCALE_AUXCR",
2368 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2369 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2370 .resetvalue = 0, },
3b771579
PM
2371 /* XScale specific cache-lockdown: since we have no cache we NOP these
2372 * and hope the guest does not really rely on cache behaviour.
2373 */
2374 { .name = "XSCALE_LOCK_ICACHE_LINE",
2375 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2376 .access = PL1_W, .type = ARM_CP_NOP },
2377 { .name = "XSCALE_UNLOCK_ICACHE",
2378 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2379 .access = PL1_W, .type = ARM_CP_NOP },
2380 { .name = "XSCALE_DCACHE_LOCK",
2381 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2382 .access = PL1_RW, .type = ARM_CP_NOP },
2383 { .name = "XSCALE_UNLOCK_DCACHE",
2384 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2385 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2386 REGINFO_SENTINEL
2387};
2388
2389static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2390 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2391 * implementation of this implementation-defined space.
2392 * Ideally this should eventually disappear in favour of actually
2393 * implementing the correct behaviour for all cores.
2394 */
2395 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2396 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2397 .access = PL1_RW,
7a0e58fa 2398 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2399 .resetvalue = 0 },
18032bec
PM
2400 REGINFO_SENTINEL
2401};
2402
c4804214
PM
2403static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2404 /* Cache status: RAZ because we have no cache so it's always clean */
2405 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2406 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2407 .resetvalue = 0 },
c4804214
PM
2408 REGINFO_SENTINEL
2409};
2410
2411static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2412 /* We never have a a block transfer operation in progress */
2413 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2414 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2415 .resetvalue = 0 },
30b05bba
PM
2416 /* The cache ops themselves: these all NOP for QEMU */
2417 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2418 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2419 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2420 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2421 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2422 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2423 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2424 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2425 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2426 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2427 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2428 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2429 REGINFO_SENTINEL
2430};
2431
2432static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2433 /* The cache test-and-clean instructions always return (1 << 30)
2434 * to indicate that there are no dirty cache lines.
2435 */
2436 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2437 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2438 .resetvalue = (1 << 30) },
c4804214 2439 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2440 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2441 .resetvalue = (1 << 30) },
c4804214
PM
2442 REGINFO_SENTINEL
2443};
2444
34f90529
PM
2445static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2446 /* Ignore ReadBuffer accesses */
2447 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2448 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2449 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2450 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2451 REGINFO_SENTINEL
2452};
2453
731de9e6
EI
2454static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2455{
2456 ARMCPU *cpu = arm_env_get_cpu(env);
2457 unsigned int cur_el = arm_current_el(env);
2458 bool secure = arm_is_secure(env);
2459
2460 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2461 return env->cp15.vpidr_el2;
2462 }
2463 return raw_read(env, ri);
2464}
2465
06a7e647 2466static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2467{
eb5e1d3c
PF
2468 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2469 uint64_t mpidr = cpu->mp_affinity;
2470
81bdde9d 2471 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2472 mpidr |= (1U << 31);
81bdde9d
PM
2473 /* Cores which are uniprocessor (non-coherent)
2474 * but still implement the MP extensions set
a8e81b31 2475 * bit 30. (For instance, Cortex-R5).
81bdde9d 2476 */
a8e81b31
PC
2477 if (cpu->mp_is_up) {
2478 mpidr |= (1u << 30);
2479 }
81bdde9d 2480 }
c4241c7d 2481 return mpidr;
81bdde9d
PM
2482}
2483
06a7e647
EI
2484static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2485{
f0d574d6
EI
2486 unsigned int cur_el = arm_current_el(env);
2487 bool secure = arm_is_secure(env);
2488
2489 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2490 return env->cp15.vmpidr_el2;
2491 }
06a7e647
EI
2492 return mpidr_read_val(env);
2493}
2494
81bdde9d 2495static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2496 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2497 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2498 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2499 REGINFO_SENTINEL
2500};
2501
7ac681cf 2502static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2503 /* NOP AMAIR0/1 */
b0fe2427
PM
2504 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2505 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2506 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2507 .resetvalue = 0 },
b0fe2427 2508 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2509 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2510 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2511 .resetvalue = 0 },
891a2fe7 2512 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2513 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2514 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2515 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2516 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2517 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2518 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2519 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2520 .writefn = vmsa_ttbr_write, },
891a2fe7 2521 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2522 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2523 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2524 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2525 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2526 REGINFO_SENTINEL
2527};
2528
c4241c7d 2529static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2530{
c4241c7d 2531 return vfp_get_fpcr(env);
b0d2b7d0
PM
2532}
2533
c4241c7d
PM
2534static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2535 uint64_t value)
b0d2b7d0
PM
2536{
2537 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2538}
2539
c4241c7d 2540static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2541{
c4241c7d 2542 return vfp_get_fpsr(env);
b0d2b7d0
PM
2543}
2544
c4241c7d
PM
2545static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2546 uint64_t value)
b0d2b7d0
PM
2547{
2548 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2549}
2550
c2b820fe
PM
2551static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
2552{
137feaa9 2553 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2554 return CP_ACCESS_TRAP;
2555 }
2556 return CP_ACCESS_OK;
2557}
2558
2559static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2560 uint64_t value)
2561{
2562 env->daif = value & PSTATE_DAIF;
2563}
2564
8af35c37
PM
2565static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2566 const ARMCPRegInfo *ri)
2567{
2568 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2569 * SCTLR_EL1.UCI is set.
2570 */
137feaa9 2571 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2572 return CP_ACCESS_TRAP;
2573 }
2574 return CP_ACCESS_OK;
2575}
2576
dbb1fb27
AB
2577/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2578 * Page D4-1736 (DDI0487A.b)
2579 */
2580
fd3ed969
PM
2581static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2582 uint64_t value)
168aa23b 2583{
31b030d4 2584 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969 2585 CPUState *cs = CPU(cpu);
dbb1fb27 2586
fd3ed969
PM
2587 if (arm_is_secure_below_el3(env)) {
2588 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2589 } else {
2590 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2591 }
168aa23b
PM
2592}
2593
fd3ed969
PM
2594static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2595 uint64_t value)
168aa23b 2596{
fd3ed969
PM
2597 bool sec = arm_is_secure_below_el3(env);
2598 CPUState *other_cs;
dbb1fb27 2599
fd3ed969
PM
2600 CPU_FOREACH(other_cs) {
2601 if (sec) {
2602 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2603 } else {
2604 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2605 ARMMMUIdx_S12NSE0, -1);
2606 }
2607 }
168aa23b
PM
2608}
2609
fd3ed969
PM
2610static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2611 uint64_t value)
168aa23b 2612{
fd3ed969
PM
2613 /* Note that the 'ALL' scope must invalidate both stage 1 and
2614 * stage 2 translations, whereas most other scopes only invalidate
2615 * stage 1 translations.
2616 */
00c8cb0a 2617 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
2618 CPUState *cs = CPU(cpu);
2619
2620 if (arm_is_secure_below_el3(env)) {
2621 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2622 } else {
2623 if (arm_feature(env, ARM_FEATURE_EL2)) {
2624 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2625 ARMMMUIdx_S2NS, -1);
2626 } else {
2627 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2628 }
2629 }
168aa23b
PM
2630}
2631
fd3ed969 2632static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
2633 uint64_t value)
2634{
fd3ed969
PM
2635 ARMCPU *cpu = arm_env_get_cpu(env);
2636 CPUState *cs = CPU(cpu);
2637
2638 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2639}
2640
43efaa33
PM
2641static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2642 uint64_t value)
2643{
2644 ARMCPU *cpu = arm_env_get_cpu(env);
2645 CPUState *cs = CPU(cpu);
2646
2647 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2648}
2649
fd3ed969
PM
2650static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2651 uint64_t value)
2652{
2653 /* Note that the 'ALL' scope must invalidate both stage 1 and
2654 * stage 2 translations, whereas most other scopes only invalidate
2655 * stage 1 translations.
2656 */
2657 bool sec = arm_is_secure_below_el3(env);
2658 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
fa439fc5 2659 CPUState *other_cs;
fa439fc5
PM
2660
2661 CPU_FOREACH(other_cs) {
fd3ed969
PM
2662 if (sec) {
2663 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2664 } else if (has_el2) {
2665 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2666 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2667 } else {
2668 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2669 ARMMMUIdx_S12NSE0, -1);
2670 }
fa439fc5
PM
2671 }
2672}
2673
2bfb9d75
PM
2674static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2675 uint64_t value)
2676{
2677 CPUState *other_cs;
2678
2679 CPU_FOREACH(other_cs) {
2680 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2681 }
2682}
2683
43efaa33
PM
2684static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2685 uint64_t value)
2686{
2687 CPUState *other_cs;
2688
2689 CPU_FOREACH(other_cs) {
2690 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2691 }
2692}
2693
fd3ed969
PM
2694static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2695 uint64_t value)
2696{
2697 /* Invalidate by VA, EL1&0 (AArch64 version).
2698 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2699 * since we don't support flush-for-specific-ASID-only or
2700 * flush-last-level-only.
2701 */
2702 ARMCPU *cpu = arm_env_get_cpu(env);
2703 CPUState *cs = CPU(cpu);
2704 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2705
2706 if (arm_is_secure_below_el3(env)) {
2707 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2708 ARMMMUIdx_S1SE0, -1);
2709 } else {
2710 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2711 ARMMMUIdx_S12NSE0, -1);
2712 }
2713}
2714
2715static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2716 uint64_t value)
fa439fc5 2717{
fd3ed969
PM
2718 /* Invalidate by VA, EL2
2719 * Currently handles both VAE2 and VALE2, since we don't support
2720 * flush-last-level-only.
2721 */
2722 ARMCPU *cpu = arm_env_get_cpu(env);
2723 CPUState *cs = CPU(cpu);
2724 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2725
2726 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2727}
2728
43efaa33
PM
2729static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2730 uint64_t value)
2731{
2732 /* Invalidate by VA, EL3
2733 * Currently handles both VAE3 and VALE3, since we don't support
2734 * flush-last-level-only.
2735 */
2736 ARMCPU *cpu = arm_env_get_cpu(env);
2737 CPUState *cs = CPU(cpu);
2738 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2739
2740 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2741}
2742
fd3ed969
PM
2743static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2744 uint64_t value)
2745{
2746 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
2747 CPUState *other_cs;
2748 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2749
2750 CPU_FOREACH(other_cs) {
fd3ed969
PM
2751 if (sec) {
2752 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2753 ARMMMUIdx_S1SE0, -1);
2754 } else {
2755 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2756 ARMMMUIdx_S12NSE0, -1);
2757 }
fa439fc5
PM
2758 }
2759}
2760
fd3ed969
PM
2761static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2762 uint64_t value)
fa439fc5
PM
2763{
2764 CPUState *other_cs;
fd3ed969 2765 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5
PM
2766
2767 CPU_FOREACH(other_cs) {
fd3ed969 2768 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
fa439fc5
PM
2769 }
2770}
2771
43efaa33
PM
2772static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2773 uint64_t value)
2774{
2775 CPUState *other_cs;
2776 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2777
2778 CPU_FOREACH(other_cs) {
2779 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2780 }
2781}
2782
cea66e91
PM
2783static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2784 uint64_t value)
2785{
2786 /* Invalidate by IPA. This has to invalidate any structures that
2787 * contain only stage 2 translation information, but does not need
2788 * to apply to structures that contain combined stage 1 and stage 2
2789 * translation information.
2790 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2791 */
2792 ARMCPU *cpu = arm_env_get_cpu(env);
2793 CPUState *cs = CPU(cpu);
2794 uint64_t pageaddr;
2795
2796 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2797 return;
2798 }
2799
2800 pageaddr = sextract64(value << 12, 0, 48);
2801
2802 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2803}
2804
2805static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2806 uint64_t value)
2807{
2808 CPUState *other_cs;
2809 uint64_t pageaddr;
2810
2811 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2812 return;
2813 }
2814
2815 pageaddr = sextract64(value << 12, 0, 48);
2816
2817 CPU_FOREACH(other_cs) {
2818 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2819 }
2820}
2821
aca3f40b
PM
2822static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
2823{
2824 /* We don't implement EL2, so the only control on DC ZVA is the
2825 * bit in the SCTLR which can prohibit access for EL0.
2826 */
137feaa9 2827 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
2828 return CP_ACCESS_TRAP;
2829 }
2830 return CP_ACCESS_OK;
2831}
2832
2833static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
2834{
2835 ARMCPU *cpu = arm_env_get_cpu(env);
2836 int dzp_bit = 1 << 4;
2837
2838 /* DZP indicates whether DC ZVA access is allowed */
14e5f106 2839 if (aa64_zva_access(env, NULL) == CP_ACCESS_OK) {
aca3f40b
PM
2840 dzp_bit = 0;
2841 }
2842 return cpu->dcz_blocksize | dzp_bit;
2843}
2844
f502cfc2
PM
2845static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2846{
cdcf1405 2847 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
2848 /* Access to SP_EL0 is undefined if it's being used as
2849 * the stack pointer.
2850 */
2851 return CP_ACCESS_TRAP_UNCATEGORIZED;
2852 }
2853 return CP_ACCESS_OK;
2854}
2855
2856static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
2857{
2858 return env->pstate & PSTATE_SP;
2859}
2860
2861static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
2862{
2863 update_spsel(env, val);
2864}
2865
137feaa9
FA
2866static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2867 uint64_t value)
2868{
2869 ARMCPU *cpu = arm_env_get_cpu(env);
2870
2871 if (raw_read(env, ri) == value) {
2872 /* Skip the TLB flush if nothing actually changed; Linux likes
2873 * to do a lot of pointless SCTLR writes.
2874 */
2875 return;
2876 }
2877
2878 raw_write(env, ri, value);
2879 /* ??? Lots of these bits are not implemented. */
2880 /* This may enable/disable the MMU, so do a TLB flush. */
2881 tlb_flush(CPU(cpu), 1);
2882}
2883
b0d2b7d0
PM
2884static const ARMCPRegInfo v8_cp_reginfo[] = {
2885 /* Minimal set of EL0-visible registers. This will need to be expanded
2886 * significantly for system emulation of AArch64 CPUs.
2887 */
2888 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
2889 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
2890 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
2891 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
2892 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 2893 .type = ARM_CP_NO_RAW,
c2b820fe
PM
2894 .access = PL0_RW, .accessfn = aa64_daif_access,
2895 .fieldoffset = offsetof(CPUARMState, daif),
2896 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
2897 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
2898 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
2899 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
2900 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
2901 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
2902 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
2903 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
2904 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 2905 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
2906 .readfn = aa64_dczid_read },
2907 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
2908 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
2909 .access = PL0_W, .type = ARM_CP_DC_ZVA,
2910#ifndef CONFIG_USER_ONLY
2911 /* Avoid overhead of an access check that always passes in user-mode */
2912 .accessfn = aa64_zva_access,
2913#endif
2914 },
0eef9d98
PM
2915 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
2916 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
2917 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
2918 /* Cache ops: all NOPs since we don't emulate caches */
2919 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
2920 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2921 .access = PL1_W, .type = ARM_CP_NOP },
2922 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
2923 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2924 .access = PL1_W, .type = ARM_CP_NOP },
2925 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
2926 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
2927 .access = PL0_W, .type = ARM_CP_NOP,
2928 .accessfn = aa64_cacheop_access },
2929 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
2930 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2931 .access = PL1_W, .type = ARM_CP_NOP },
2932 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
2933 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2934 .access = PL1_W, .type = ARM_CP_NOP },
2935 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
2936 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
2937 .access = PL0_W, .type = ARM_CP_NOP,
2938 .accessfn = aa64_cacheop_access },
2939 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
2940 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2941 .access = PL1_W, .type = ARM_CP_NOP },
2942 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
2943 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
2944 .access = PL0_W, .type = ARM_CP_NOP,
2945 .accessfn = aa64_cacheop_access },
2946 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
2947 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
2948 .access = PL0_W, .type = ARM_CP_NOP,
2949 .accessfn = aa64_cacheop_access },
2950 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
2951 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2952 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
2953 /* TLBI operations */
2954 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2955 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 2956 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2957 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 2958 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2959 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 2960 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2961 .writefn = tlbi_aa64_vae1is_write },
168aa23b 2962 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2963 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 2964 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2965 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 2966 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2967 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 2968 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2969 .writefn = tlbi_aa64_vae1is_write },
168aa23b 2970 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2971 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 2972 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2973 .writefn = tlbi_aa64_vae1is_write },
168aa23b 2974 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2975 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 2976 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2977 .writefn = tlbi_aa64_vae1is_write },
168aa23b 2978 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2979 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 2980 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2981 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 2982 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2983 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 2984 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2985 .writefn = tlbi_aa64_vae1_write },
168aa23b 2986 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2987 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 2988 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2989 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 2990 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2991 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 2992 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2993 .writefn = tlbi_aa64_vae1_write },
168aa23b 2994 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2995 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 2996 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 2997 .writefn = tlbi_aa64_vae1_write },
168aa23b 2998 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2999 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3000 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3001 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3002 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3003 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3004 .access = PL2_W, .type = ARM_CP_NO_RAW,
3005 .writefn = tlbi_aa64_ipas2e1is_write },
3006 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3007 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3008 .access = PL2_W, .type = ARM_CP_NO_RAW,
3009 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3010 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3011 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3012 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3013 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3014 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3015 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3016 .access = PL2_W, .type = ARM_CP_NO_RAW,
3017 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3018 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3019 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3020 .access = PL2_W, .type = ARM_CP_NO_RAW,
3021 .writefn = tlbi_aa64_ipas2e1_write },
3022 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3023 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3024 .access = PL2_W, .type = ARM_CP_NO_RAW,
3025 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3026 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3027 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3028 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3029 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3030 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3031 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3032 .access = PL2_W, .type = ARM_CP_NO_RAW,
3033 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3034#ifndef CONFIG_USER_ONLY
3035 /* 64 bit address translation operations */
3036 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3037 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3038 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3039 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3040 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3041 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3042 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3043 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3044 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3045 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3046 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3047 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3048 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3049 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3050 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3051 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3052 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3053 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3054 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3055 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3056 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3057 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3058 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3059 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3060 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3061 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3062 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3063 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3064 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3065 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3066 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3067 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3068 .type = ARM_CP_ALIAS,
3069 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3070 .access = PL1_RW, .resetvalue = 0,
3071 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3072 .writefn = par_write },
19525524 3073#endif
995939a6 3074 /* TLB invalidate last level of translation table walk */
9449fdf6 3075 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3076 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3077 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3078 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3079 .writefn = tlbimvaa_is_write },
9449fdf6 3080 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3081 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3082 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3083 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
9449fdf6
PM
3084 /* 32 bit cache operations */
3085 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3086 .type = ARM_CP_NOP, .access = PL1_W },
3087 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3088 .type = ARM_CP_NOP, .access = PL1_W },
3089 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3090 .type = ARM_CP_NOP, .access = PL1_W },
3091 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3092 .type = ARM_CP_NOP, .access = PL1_W },
3093 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3094 .type = ARM_CP_NOP, .access = PL1_W },
3095 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3096 .type = ARM_CP_NOP, .access = PL1_W },
3097 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3098 .type = ARM_CP_NOP, .access = PL1_W },
3099 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3100 .type = ARM_CP_NOP, .access = PL1_W },
3101 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3102 .type = ARM_CP_NOP, .access = PL1_W },
3103 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3104 .type = ARM_CP_NOP, .access = PL1_W },
3105 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3106 .type = ARM_CP_NOP, .access = PL1_W },
3107 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3108 .type = ARM_CP_NOP, .access = PL1_W },
3109 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3110 .type = ARM_CP_NOP, .access = PL1_W },
3111 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3112 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3113 .access = PL1_RW, .resetvalue = 0,
3114 .writefn = dacr_write, .raw_writefn = raw_write,
3115 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3116 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3117 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3118 .type = ARM_CP_ALIAS,
a0618a19 3119 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3120 .access = PL1_RW,
3121 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3122 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3123 .type = ARM_CP_ALIAS,
a65f1de9 3124 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
7847f9ea 3125 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[1]) },
f502cfc2
PM
3126 /* We rely on the access checks not allowing the guest to write to the
3127 * state field when SPSel indicates that it's being used as the stack
3128 * pointer.
3129 */
3130 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3131 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3132 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3133 .type = ARM_CP_ALIAS,
f502cfc2 3134 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3135 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3136 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3137 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3138 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3139 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3140 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3141 .type = ARM_CP_NO_RAW,
f502cfc2 3142 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
b0d2b7d0
PM
3143 REGINFO_SENTINEL
3144};
3145
d42e3c26 3146/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3147static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3148 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3149 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3150 .access = PL2_RW,
3151 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3152 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3153 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3154 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3155 .access = PL2_RW,
3156 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3157 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3158 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3159 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3160 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3161 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3162 .access = PL2_RW, .type = ARM_CP_CONST,
3163 .resetvalue = 0 },
3164 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3165 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3166 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3167 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3168 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3169 .access = PL2_RW, .type = ARM_CP_CONST,
3170 .resetvalue = 0 },
3171 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3172 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3173 .access = PL2_RW, .type = ARM_CP_CONST,
3174 .resetvalue = 0 },
37cd6c24
PM
3175 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3176 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3177 .access = PL2_RW, .type = ARM_CP_CONST,
3178 .resetvalue = 0 },
3179 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3180 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3181 .access = PL2_RW, .type = ARM_CP_CONST,
3182 .resetvalue = 0 },
06ec4c8c
EI
3183 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3184 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3185 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3186 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3187 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3188 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3189 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3190 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3191 .cp = 15, .opc1 = 6, .crm = 2,
3192 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3193 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3194 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3195 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3196 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3197 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3198 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3199 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3200 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3201 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3202 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3203 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3204 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3205 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3206 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3207 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3208 .resetvalue = 0 },
0b6440af
EI
3209 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3210 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3211 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3212 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3213 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3214 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3215 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3216 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3217 .resetvalue = 0 },
b0e66d95
EI
3218 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3219 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3220 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3221 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3222 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3223 .resetvalue = 0 },
3224 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3225 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3226 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3227 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3228 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3229 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3230 REGINFO_SENTINEL
3231};
3232
f149e3e8
EI
3233static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3234{
3235 ARMCPU *cpu = arm_env_get_cpu(env);
3236 uint64_t valid_mask = HCR_MASK;
3237
3238 if (arm_feature(env, ARM_FEATURE_EL3)) {
3239 valid_mask &= ~HCR_HCD;
3240 } else {
3241 valid_mask &= ~HCR_TSC;
3242 }
3243
3244 /* Clear RES0 bits. */
3245 value &= valid_mask;
3246
3247 /* These bits change the MMU setup:
3248 * HCR_VM enables stage 2 translation
3249 * HCR_PTW forbids certain page-table setups
3250 * HCR_DC Disables stage1 and enables stage2 translation
3251 */
3252 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3253 tlb_flush(CPU(cpu), 1);
3254 }
3255 raw_write(env, ri, value);
3256}
3257
4771cd01 3258static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3259 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3260 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3261 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3262 .writefn = hcr_write },
0c17d68c
FA
3263 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3264 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3265 .access = PL2_RW, .resetvalue = 0,
3266 .writefn = dacr_write, .raw_writefn = raw_write,
3267 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3b685ba7 3268 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3269 .type = ARM_CP_ALIAS,
3b685ba7
EI
3270 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3271 .access = PL2_RW,
3272 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3273 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3274 .type = ARM_CP_ALIAS,
f2c30f42
EI
3275 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3276 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
88ca1c2d
FA
3277 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3278 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3279 .access = PL2_RW, .resetvalue = 0,
3280 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
63b60551
EI
3281 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3282 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3283 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3284 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3285 .type = ARM_CP_ALIAS,
3b685ba7
EI
3286 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3287 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
d42e3c26
EI
3288 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3289 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3290 .access = PL2_RW, .writefn = vbar_write,
3291 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3292 .resetvalue = 0 },
884b4dee
GB
3293 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3294 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3295 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3296 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3297 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3298 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3299 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3300 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3301 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3302 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3303 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3304 .resetvalue = 0 },
3305 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3306 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3307 .access = PL2_RW, .type = ARM_CP_ALIAS,
3308 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3309 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3310 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3311 .access = PL2_RW, .type = ARM_CP_CONST,
3312 .resetvalue = 0 },
3313 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3314 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3315 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3316 .access = PL2_RW, .type = ARM_CP_CONST,
3317 .resetvalue = 0 },
37cd6c24
PM
3318 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3319 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3320 .access = PL2_RW, .type = ARM_CP_CONST,
3321 .resetvalue = 0 },
3322 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3323 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3324 .access = PL2_RW, .type = ARM_CP_CONST,
3325 .resetvalue = 0 },
06ec4c8c
EI
3326 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3327 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3328 .access = PL2_RW, .writefn = vmsa_tcr_el1_write,
3329 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3330 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3331 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3332 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3333 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3334 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3335 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3336 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3337 .access = PL2_RW, .type = ARM_CP_ALIAS,
3338 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3339 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3340 .cp = 15, .opc1 = 6, .crm = 2,
3341 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3342 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3343 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3344 .writefn = vttbr_write },
3345 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3346 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3347 .access = PL2_RW, .writefn = vttbr_write,
3348 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3349 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3350 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3351 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3352 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3353 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3354 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3355 .access = PL2_RW, .resetvalue = 0,
3356 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3357 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3358 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3359 .access = PL2_RW, .resetvalue = 0,
3360 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3361 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3362 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3363 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
51da9014
EI
3364 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3365 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3366 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3367 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3368 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3369 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3370 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3371 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3372 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3373 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3374 .access = PL2_W, .type = ARM_CP_NO_RAW,
3375 .writefn = tlbi_aa64_vae2_write },
3376 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3377 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3378 .access = PL2_W, .type = ARM_CP_NO_RAW,
3379 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3380 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3381 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3382 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3383 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3384 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3385 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3386 .access = PL2_W, .type = ARM_CP_NO_RAW,
3387 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3388#ifndef CONFIG_USER_ONLY
2a47df95
PM
3389 /* Unlike the other EL2-related AT operations, these must
3390 * UNDEF from EL3 if EL2 is not implemented, which is why we
3391 * define them here rather than with the rest of the AT ops.
3392 */
3393 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3394 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3395 .access = PL2_W, .accessfn = at_s1e2_access,
3396 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3397 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3398 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3399 .access = PL2_W, .accessfn = at_s1e2_access,
3400 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3401 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3402 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3403 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3404 * to behave as if SCR.NS was 1.
3405 */
3406 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3407 .access = PL2_W,
3408 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3409 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3410 .access = PL2_W,
3411 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3412 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3413 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3414 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3415 * reset values as IMPDEF. We choose to reset to 3 to comply with
3416 * both ARMv7 and ARMv8.
3417 */
3418 .access = PL2_RW, .resetvalue = 3,
3419 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3420 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3421 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3422 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3423 .writefn = gt_cntvoff_write,
3424 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3425 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3426 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3427 .writefn = gt_cntvoff_write,
3428 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3429 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3430 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3431 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3432 .type = ARM_CP_IO, .access = PL2_RW,
3433 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3434 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3435 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3436 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3437 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3438 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3439 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3440 .type = ARM_CP_IO, .access = PL2_RW,
3441 .resetfn = gt_hyp_timer_reset,
3442 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3443 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3444 .type = ARM_CP_IO,
3445 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3446 .access = PL2_RW,
3447 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3448 .resetvalue = 0,
3449 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 3450#endif
3b685ba7
EI
3451 REGINFO_SENTINEL
3452};
3453
60fb1a87
GB
3454static const ARMCPRegInfo el3_cp_reginfo[] = {
3455 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3456 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3457 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3458 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 3459 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87
GB
3460 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
3461 .access = PL3_RW, .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 3462 .writefn = scr_write },
60fb1a87
GB
3463 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3464 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3465 .access = PL3_RW, .resetvalue = 0,
3466 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3467 { .name = "SDER",
3468 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3469 .access = PL3_RW, .resetvalue = 0,
3470 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
3471 /* TODO: Implement NSACR trapping of secure EL1 accesses to EL3 */
3472 { .name = "NSACR", .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
3473 .access = PL3_W | PL1_R, .resetvalue = 0,
3474 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) },
3475 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
3476 .access = PL3_RW, .writefn = vbar_write, .resetvalue = 0,
3477 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
137feaa9 3478 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
e46e1a74 3479 .type = ARM_CP_ALIAS, /* reset handled by AArch32 view */
137feaa9
FA
3480 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
3481 .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3482 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) },
7dd8c9af
FA
3483 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3484 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3485 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3486 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
3487 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3488 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
3489 .access = PL3_RW, .writefn = vmsa_tcr_el1_write,
3490 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3491 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 3492 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3493 .type = ARM_CP_ALIAS,
81547d66
EI
3494 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3495 .access = PL3_RW,
3496 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 3497 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3498 .type = ARM_CP_ALIAS,
f2c30f42
EI
3499 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3500 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
3501 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3502 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3503 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 3504 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3505 .type = ARM_CP_ALIAS,
81547d66
EI
3506 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
3507 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
a1ba125c
EI
3508 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3509 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3510 .access = PL3_RW, .writefn = vbar_write,
3511 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3512 .resetvalue = 0 },
c6f19164
GB
3513 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3514 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3515 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3516 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
3517 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3518 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3519 .access = PL3_RW, .resetvalue = 0,
3520 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
3521 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3522 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3523 .access = PL3_RW, .type = ARM_CP_CONST,
3524 .resetvalue = 0 },
37cd6c24
PM
3525 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3526 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3527 .access = PL3_RW, .type = ARM_CP_CONST,
3528 .resetvalue = 0 },
3529 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3530 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3531 .access = PL3_RW, .type = ARM_CP_CONST,
3532 .resetvalue = 0 },
43efaa33
PM
3533 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3534 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3535 .access = PL3_W, .type = ARM_CP_NO_RAW,
3536 .writefn = tlbi_aa64_alle3is_write },
3537 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3538 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3539 .access = PL3_W, .type = ARM_CP_NO_RAW,
3540 .writefn = tlbi_aa64_vae3is_write },
3541 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3542 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3543 .access = PL3_W, .type = ARM_CP_NO_RAW,
3544 .writefn = tlbi_aa64_vae3is_write },
3545 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3546 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3547 .access = PL3_W, .type = ARM_CP_NO_RAW,
3548 .writefn = tlbi_aa64_alle3_write },
3549 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3550 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3551 .access = PL3_W, .type = ARM_CP_NO_RAW,
3552 .writefn = tlbi_aa64_vae3_write },
3553 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3554 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3555 .access = PL3_W, .type = ARM_CP_NO_RAW,
3556 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
3557 REGINFO_SENTINEL
3558};
3559
7da845b0
PM
3560static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
3561{
3562 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3563 * but the AArch32 CTR has its own reginfo struct)
3564 */
137feaa9 3565 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
3566 return CP_ACCESS_TRAP;
3567 }
3568 return CP_ACCESS_OK;
3569}
3570
50300698 3571static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 3572 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
3573 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3574 * unlike DBGDRAR it is never accessible from EL0.
3575 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3576 * accessor.
50300698
PM
3577 */
3578 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
3579 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
3580 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3581 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
3582 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
50300698
PM
3583 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3584 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 3585 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
3586 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3587 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
0e5e8935
PM
3588 .access = PL1_RW,
3589 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3590 .resetvalue = 0 },
5e8b12ff
PM
3591 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3592 * We don't implement the configurable EL0 access.
3593 */
3594 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3595 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 3596 .type = ARM_CP_ALIAS,
5e8b12ff 3597 .access = PL1_R,
b061a82b 3598 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
50300698 3599 /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
10aae104
PM
3600 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3601 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
50300698 3602 .access = PL1_W, .type = ARM_CP_NOP },
5e8b12ff
PM
3603 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3604 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3605 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
3606 .access = PL1_RW, .type = ARM_CP_NOP },
3607 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3608 * implement vector catch debug events yet.
3609 */
3610 { .name = "DBGVCR",
3611 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
3612 .access = PL1_RW, .type = ARM_CP_NOP },
50300698
PM
3613 REGINFO_SENTINEL
3614};
3615
3616static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3617 /* 64 bit access versions of the (dummy) debug registers */
3618 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3619 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3620 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3621 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3622 REGINFO_SENTINEL
3623};
3624
9ee98ce8
PM
3625void hw_watchpoint_update(ARMCPU *cpu, int n)
3626{
3627 CPUARMState *env = &cpu->env;
3628 vaddr len = 0;
3629 vaddr wvr = env->cp15.dbgwvr[n];
3630 uint64_t wcr = env->cp15.dbgwcr[n];
3631 int mask;
3632 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3633
3634 if (env->cpu_watchpoint[n]) {
3635 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3636 env->cpu_watchpoint[n] = NULL;
3637 }
3638
3639 if (!extract64(wcr, 0, 1)) {
3640 /* E bit clear : watchpoint disabled */
3641 return;
3642 }
3643
3644 switch (extract64(wcr, 3, 2)) {
3645 case 0:
3646 /* LSC 00 is reserved and must behave as if the wp is disabled */
3647 return;
3648 case 1:
3649 flags |= BP_MEM_READ;
3650 break;
3651 case 2:
3652 flags |= BP_MEM_WRITE;
3653 break;
3654 case 3:
3655 flags |= BP_MEM_ACCESS;
3656 break;
3657 }
3658
3659 /* Attempts to use both MASK and BAS fields simultaneously are
3660 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3661 * thus generating a watchpoint for every byte in the masked region.
3662 */
3663 mask = extract64(wcr, 24, 4);
3664 if (mask == 1 || mask == 2) {
3665 /* Reserved values of MASK; we must act as if the mask value was
3666 * some non-reserved value, or as if the watchpoint were disabled.
3667 * We choose the latter.
3668 */
3669 return;
3670 } else if (mask) {
3671 /* Watchpoint covers an aligned area up to 2GB in size */
3672 len = 1ULL << mask;
3673 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3674 * whether the watchpoint fires when the unmasked bits match; we opt
3675 * to generate the exceptions.
3676 */
3677 wvr &= ~(len - 1);
3678 } else {
3679 /* Watchpoint covers bytes defined by the byte address select bits */
3680 int bas = extract64(wcr, 5, 8);
3681 int basstart;
3682
3683 if (bas == 0) {
3684 /* This must act as if the watchpoint is disabled */
3685 return;
3686 }
3687
3688 if (extract64(wvr, 2, 1)) {
3689 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3690 * ignored, and BAS[3:0] define which bytes to watch.
3691 */
3692 bas &= 0xf;
3693 }
3694 /* The BAS bits are supposed to be programmed to indicate a contiguous
3695 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3696 * we fire for each byte in the word/doubleword addressed by the WVR.
3697 * We choose to ignore any non-zero bits after the first range of 1s.
3698 */
3699 basstart = ctz32(bas);
3700 len = cto32(bas >> basstart);
3701 wvr += basstart;
3702 }
3703
3704 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
3705 &env->cpu_watchpoint[n]);
3706}
3707
3708void hw_watchpoint_update_all(ARMCPU *cpu)
3709{
3710 int i;
3711 CPUARMState *env = &cpu->env;
3712
3713 /* Completely clear out existing QEMU watchpoints and our array, to
3714 * avoid possible stale entries following migration load.
3715 */
3716 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
3717 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
3718
3719 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
3720 hw_watchpoint_update(cpu, i);
3721 }
3722}
3723
3724static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3725 uint64_t value)
3726{
3727 ARMCPU *cpu = arm_env_get_cpu(env);
3728 int i = ri->crm;
3729
3730 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
3731 * register reads and behaves as if values written are sign extended.
3732 * Bits [1:0] are RES0.
3733 */
3734 value = sextract64(value, 0, 49) & ~3ULL;
3735
3736 raw_write(env, ri, value);
3737 hw_watchpoint_update(cpu, i);
3738}
3739
3740static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3741 uint64_t value)
3742{
3743 ARMCPU *cpu = arm_env_get_cpu(env);
3744 int i = ri->crm;
3745
3746 raw_write(env, ri, value);
3747 hw_watchpoint_update(cpu, i);
3748}
3749
46747d15
PM
3750void hw_breakpoint_update(ARMCPU *cpu, int n)
3751{
3752 CPUARMState *env = &cpu->env;
3753 uint64_t bvr = env->cp15.dbgbvr[n];
3754 uint64_t bcr = env->cp15.dbgbcr[n];
3755 vaddr addr;
3756 int bt;
3757 int flags = BP_CPU;
3758
3759 if (env->cpu_breakpoint[n]) {
3760 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
3761 env->cpu_breakpoint[n] = NULL;
3762 }
3763
3764 if (!extract64(bcr, 0, 1)) {
3765 /* E bit clear : watchpoint disabled */
3766 return;
3767 }
3768
3769 bt = extract64(bcr, 20, 4);
3770
3771 switch (bt) {
3772 case 4: /* unlinked address mismatch (reserved if AArch64) */
3773 case 5: /* linked address mismatch (reserved if AArch64) */
3774 qemu_log_mask(LOG_UNIMP,
3775 "arm: address mismatch breakpoint types not implemented");
3776 return;
3777 case 0: /* unlinked address match */
3778 case 1: /* linked address match */
3779 {
3780 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
3781 * we behave as if the register was sign extended. Bits [1:0] are
3782 * RES0. The BAS field is used to allow setting breakpoints on 16
3783 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
3784 * a bp will fire if the addresses covered by the bp and the addresses
3785 * covered by the insn overlap but the insn doesn't start at the
3786 * start of the bp address range. We choose to require the insn and
3787 * the bp to have the same address. The constraints on writing to
3788 * BAS enforced in dbgbcr_write mean we have only four cases:
3789 * 0b0000 => no breakpoint
3790 * 0b0011 => breakpoint on addr
3791 * 0b1100 => breakpoint on addr + 2
3792 * 0b1111 => breakpoint on addr
3793 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
3794 */
3795 int bas = extract64(bcr, 5, 4);
3796 addr = sextract64(bvr, 0, 49) & ~3ULL;
3797 if (bas == 0) {
3798 return;
3799 }
3800 if (bas == 0xc) {
3801 addr += 2;
3802 }
3803 break;
3804 }
3805 case 2: /* unlinked context ID match */
3806 case 8: /* unlinked VMID match (reserved if no EL2) */
3807 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
3808 qemu_log_mask(LOG_UNIMP,
3809 "arm: unlinked context breakpoint types not implemented");
3810 return;
3811 case 9: /* linked VMID match (reserved if no EL2) */
3812 case 11: /* linked context ID and VMID match (reserved if no EL2) */
3813 case 3: /* linked context ID match */
3814 default:
3815 /* We must generate no events for Linked context matches (unless
3816 * they are linked to by some other bp/wp, which is handled in
3817 * updates for the linking bp/wp). We choose to also generate no events
3818 * for reserved values.
3819 */
3820 return;
3821 }
3822
3823 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
3824}
3825
3826void hw_breakpoint_update_all(ARMCPU *cpu)
3827{
3828 int i;
3829 CPUARMState *env = &cpu->env;
3830
3831 /* Completely clear out existing QEMU breakpoints and our array, to
3832 * avoid possible stale entries following migration load.
3833 */
3834 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
3835 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
3836
3837 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
3838 hw_breakpoint_update(cpu, i);
3839 }
3840}
3841
3842static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3843 uint64_t value)
3844{
3845 ARMCPU *cpu = arm_env_get_cpu(env);
3846 int i = ri->crm;
3847
3848 raw_write(env, ri, value);
3849 hw_breakpoint_update(cpu, i);
3850}
3851
3852static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3853 uint64_t value)
3854{
3855 ARMCPU *cpu = arm_env_get_cpu(env);
3856 int i = ri->crm;
3857
3858 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
3859 * copy of BAS[0].
3860 */
3861 value = deposit64(value, 6, 1, extract64(value, 5, 1));
3862 value = deposit64(value, 8, 1, extract64(value, 7, 1));
3863
3864 raw_write(env, ri, value);
3865 hw_breakpoint_update(cpu, i);
3866}
3867
50300698 3868static void define_debug_regs(ARMCPU *cpu)
0b45451e 3869{
50300698
PM
3870 /* Define v7 and v8 architectural debug registers.
3871 * These are just dummy implementations for now.
0b45451e
PM
3872 */
3873 int i;
3ff6fc91 3874 int wrps, brps, ctx_cmps;
48eb3ae6
PM
3875 ARMCPRegInfo dbgdidr = {
3876 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
3877 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
3878 };
3879
3ff6fc91 3880 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
3881 brps = extract32(cpu->dbgdidr, 24, 4);
3882 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
3883 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
3884
3885 assert(ctx_cmps <= brps);
48eb3ae6
PM
3886
3887 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
3888 * of the debug registers such as number of breakpoints;
3889 * check that if they both exist then they agree.
3890 */
3891 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
3892 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
3893 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 3894 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 3895 }
0b45451e 3896
48eb3ae6 3897 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
3898 define_arm_cp_regs(cpu, debug_cp_reginfo);
3899
3900 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
3901 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
3902 }
3903
48eb3ae6 3904 for (i = 0; i < brps + 1; i++) {
0b45451e 3905 ARMCPRegInfo dbgregs[] = {
10aae104
PM
3906 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
3907 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
0b45451e 3908 .access = PL1_RW,
46747d15
PM
3909 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
3910 .writefn = dbgbvr_write, .raw_writefn = raw_write
3911 },
10aae104
PM
3912 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
3913 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
0b45451e 3914 .access = PL1_RW,
46747d15
PM
3915 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
3916 .writefn = dbgbcr_write, .raw_writefn = raw_write
3917 },
48eb3ae6
PM
3918 REGINFO_SENTINEL
3919 };
3920 define_arm_cp_regs(cpu, dbgregs);
3921 }
3922
3923 for (i = 0; i < wrps + 1; i++) {
3924 ARMCPRegInfo dbgregs[] = {
10aae104
PM
3925 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
3926 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
0b45451e 3927 .access = PL1_RW,
9ee98ce8
PM
3928 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
3929 .writefn = dbgwvr_write, .raw_writefn = raw_write
3930 },
10aae104
PM
3931 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
3932 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
0b45451e 3933 .access = PL1_RW,
9ee98ce8
PM
3934 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
3935 .writefn = dbgwcr_write, .raw_writefn = raw_write
3936 },
3937 REGINFO_SENTINEL
0b45451e
PM
3938 };
3939 define_arm_cp_regs(cpu, dbgregs);
3940 }
3941}
3942
2ceb98c0
PM
3943void register_cp_regs_for_features(ARMCPU *cpu)
3944{
3945 /* Register all the coprocessor registers based on feature bits */
3946 CPUARMState *env = &cpu->env;
3947 if (arm_feature(env, ARM_FEATURE_M)) {
3948 /* M profile has no coprocessor registers */
3949 return;
3950 }
3951
e9aa6c21 3952 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
3953 if (!arm_feature(env, ARM_FEATURE_V8)) {
3954 /* Must go early as it is full of wildcards that may be
3955 * overridden by later definitions.
3956 */
3957 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
3958 }
3959
7d57f408 3960 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
3961 /* The ID registers all have impdef reset values */
3962 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
3963 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
3964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3965 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3966 .resetvalue = cpu->id_pfr0 },
0ff644a7
PM
3967 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
3968 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
3969 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3970 .resetvalue = cpu->id_pfr1 },
0ff644a7
PM
3971 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
3972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
3973 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3974 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
3975 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
3976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
3977 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3978 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
3979 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
3980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
3981 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3982 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
3983 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
3984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
3985 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3986 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
3987 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
3988 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
3989 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3990 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
3991 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
3992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
3993 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3994 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
3995 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
3996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
3997 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 3998 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
3999 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4001 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4002 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4003 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4005 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4006 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4007 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4009 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4010 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4011 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4013 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4014 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4015 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4017 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4018 .resetvalue = cpu->id_isar5 },
4019 /* 6..7 are as yet unallocated and must RAZ */
4020 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
4021 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
4022 .resetvalue = 0 },
4023 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
4024 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
4025 .resetvalue = 0 },
4026 REGINFO_SENTINEL
4027 };
4028 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4029 define_arm_cp_regs(cpu, v6_cp_reginfo);
4030 } else {
4031 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4032 }
4d31c596
PM
4033 if (arm_feature(env, ARM_FEATURE_V6K)) {
4034 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4035 }
5e5cf9e3
PC
4036 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4037 !arm_feature(env, ARM_FEATURE_MPU)) {
995939a6
PM
4038 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4039 }
e9aa6c21 4040 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4041 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4042 * field as main ID register, and we implement only the cycle
4043 * count register.
200ac0ef 4044 */
7c2cb42b 4045#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4046 ARMCPRegInfo pmcr = {
4047 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4048 .access = PL0_RW,
7a0e58fa 4049 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4050 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4051 .accessfn = pmreg_access, .writefn = pmcr_write,
4052 .raw_writefn = raw_write,
200ac0ef 4053 };
8521466b
AF
4054 ARMCPRegInfo pmcr64 = {
4055 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4056 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4057 .access = PL0_RW, .accessfn = pmreg_access,
4058 .type = ARM_CP_IO,
4059 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4060 .resetvalue = cpu->midr & 0xff000000,
4061 .writefn = pmcr_write, .raw_writefn = raw_write,
4062 };
7c2cb42b 4063 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4064 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4065#endif
776d4e5c 4066 ARMCPRegInfo clidr = {
7da845b0
PM
4067 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4068 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4069 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4070 };
776d4e5c 4071 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4072 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4073 define_debug_regs(cpu);
7d57f408
PM
4074 } else {
4075 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4076 }
b0d2b7d0 4077 if (arm_feature(env, ARM_FEATURE_V8)) {
e60cef86
PM
4078 /* AArch64 ID registers, which all have impdef reset values */
4079 ARMCPRegInfo v8_idregs[] = {
4080 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4081 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4082 .access = PL1_R, .type = ARM_CP_CONST,
4083 .resetvalue = cpu->id_aa64pfr0 },
4084 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4085 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4086 .access = PL1_R, .type = ARM_CP_CONST,
4087 .resetvalue = cpu->id_aa64pfr1},
4088 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4089 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4090 .access = PL1_R, .type = ARM_CP_CONST,
5d831be2 4091 /* We mask out the PMUVer field, because we don't currently
9225d739
PM
4092 * implement the PMU. Not advertising it prevents the guest
4093 * from trying to use it and getting UNDEFs on registers we
4094 * don't implement.
4095 */
4096 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
e60cef86
PM
4097 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4098 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4099 .access = PL1_R, .type = ARM_CP_CONST,
4100 .resetvalue = cpu->id_aa64dfr1 },
4101 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4102 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4103 .access = PL1_R, .type = ARM_CP_CONST,
4104 .resetvalue = cpu->id_aa64afr0 },
4105 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4106 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4107 .access = PL1_R, .type = ARM_CP_CONST,
4108 .resetvalue = cpu->id_aa64afr1 },
4109 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4110 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4111 .access = PL1_R, .type = ARM_CP_CONST,
4112 .resetvalue = cpu->id_aa64isar0 },
4113 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4114 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4115 .access = PL1_R, .type = ARM_CP_CONST,
4116 .resetvalue = cpu->id_aa64isar1 },
4117 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4118 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4119 .access = PL1_R, .type = ARM_CP_CONST,
4120 .resetvalue = cpu->id_aa64mmfr0 },
4121 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4122 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4123 .access = PL1_R, .type = ARM_CP_CONST,
4124 .resetvalue = cpu->id_aa64mmfr1 },
a50c0f51
PM
4125 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4126 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4127 .access = PL1_R, .type = ARM_CP_CONST,
4128 .resetvalue = cpu->mvfr0 },
4129 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4130 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4131 .access = PL1_R, .type = ARM_CP_CONST,
4132 .resetvalue = cpu->mvfr1 },
4133 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4134 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4135 .access = PL1_R, .type = ARM_CP_CONST,
4136 .resetvalue = cpu->mvfr2 },
e60cef86
PM
4137 REGINFO_SENTINEL
4138 };
be8e8128
GB
4139 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4140 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4141 !arm_feature(env, ARM_FEATURE_EL2)) {
4142 ARMCPRegInfo rvbar = {
4143 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4144 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4145 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4146 };
4147 define_one_arm_cp_reg(cpu, &rvbar);
4148 }
e60cef86 4149 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
4150 define_arm_cp_regs(cpu, v8_cp_reginfo);
4151 }
3b685ba7 4152 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 4153 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
4154 ARMCPRegInfo vpidr_regs[] = {
4155 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4156 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4157 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4158 .resetvalue = cpu->midr,
4159 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4160 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4161 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4162 .access = PL2_RW, .resetvalue = cpu->midr,
4163 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4164 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4165 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4166 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4167 .resetvalue = vmpidr_def,
4168 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4169 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4170 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4171 .access = PL2_RW,
4172 .resetvalue = vmpidr_def,
4173 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
4174 REGINFO_SENTINEL
4175 };
4176 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4177 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
4178 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4179 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4180 ARMCPRegInfo rvbar = {
4181 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4182 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4183 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4184 };
4185 define_one_arm_cp_reg(cpu, &rvbar);
4186 }
d42e3c26
EI
4187 } else {
4188 /* If EL2 is missing but higher ELs are enabled, we need to
4189 * register the no_el2 reginfos.
4190 */
4191 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
4192 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4193 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
4194 */
4195 ARMCPRegInfo vpidr_regs[] = {
4196 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4197 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4198 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4199 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4200 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4201 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4202 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4203 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4204 .type = ARM_CP_NO_RAW,
4205 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
4206 REGINFO_SENTINEL
4207 };
4208 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4209 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 4210 }
3b685ba7 4211 }
81547d66 4212 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 4213 define_arm_cp_regs(cpu, el3_cp_reginfo);
be8e8128
GB
4214 ARMCPRegInfo rvbar = {
4215 .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4216 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4217 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar
4218 };
4219 define_one_arm_cp_reg(cpu, &rvbar);
81547d66 4220 }
18032bec 4221 if (arm_feature(env, ARM_FEATURE_MPU)) {
6cb0b013
PC
4222 if (arm_feature(env, ARM_FEATURE_V6)) {
4223 /* PMSAv6 not implemented */
4224 assert(arm_feature(env, ARM_FEATURE_V7));
4225 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4226 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4227 } else {
4228 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4229 }
18032bec 4230 } else {
8e5d75c9 4231 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
4232 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4233 }
c326b979
PM
4234 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4235 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4236 }
6cc7a3ae
PM
4237 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4238 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4239 }
4a501606
PM
4240 if (arm_feature(env, ARM_FEATURE_VAPA)) {
4241 define_arm_cp_regs(cpu, vapa_cp_reginfo);
4242 }
c4804214
PM
4243 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4244 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4245 }
4246 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4247 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4248 }
4249 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4250 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4251 }
18032bec
PM
4252 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4253 define_arm_cp_regs(cpu, omap_cp_reginfo);
4254 }
34f90529
PM
4255 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4256 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4257 }
1047b9d7
PM
4258 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4259 define_arm_cp_regs(cpu, xscale_cp_reginfo);
4260 }
4261 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4262 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4263 }
7ac681cf
PM
4264 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4265 define_arm_cp_regs(cpu, lpae_cp_reginfo);
4266 }
7884849c
PM
4267 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4268 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4269 * be read-only (ie write causes UNDEF exception).
4270 */
4271 {
00a29f3d
PM
4272 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4273 /* Pre-v8 MIDR space.
4274 * Note that the MIDR isn't a simple constant register because
7884849c
PM
4275 * of the TI925 behaviour where writes to another register can
4276 * cause the MIDR value to change.
97ce8d61
PC
4277 *
4278 * Unimplemented registers in the c15 0 0 0 space default to
4279 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4280 * and friends override accordingly.
7884849c
PM
4281 */
4282 { .name = "MIDR",
97ce8d61 4283 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 4284 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 4285 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 4286 .readfn = midr_read,
97ce8d61
PC
4287 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4288 .type = ARM_CP_OVERRIDE },
7884849c
PM
4289 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4290 { .name = "DUMMY",
4291 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4292 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4293 { .name = "DUMMY",
4294 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4295 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4296 { .name = "DUMMY",
4297 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4298 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4299 { .name = "DUMMY",
4300 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4301 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4302 { .name = "DUMMY",
4303 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4304 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4305 REGINFO_SENTINEL
4306 };
00a29f3d 4307 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
4308 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4309 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
4310 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
4311 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4312 .readfn = midr_read },
ac00c79f
SF
4313 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4314 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4315 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4316 .access = PL1_R, .resetvalue = cpu->midr },
4317 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4318 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4319 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
4320 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4321 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 4322 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
4323 REGINFO_SENTINEL
4324 };
4325 ARMCPRegInfo id_cp_reginfo[] = {
4326 /* These are common to v8 and pre-v8 */
4327 { .name = "CTR",
4328 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4329 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4330 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4331 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4332 .access = PL0_R, .accessfn = ctr_el0_access,
4333 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4334 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4335 { .name = "TCMTR",
4336 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4337 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
4338 REGINFO_SENTINEL
4339 };
8085ce63
PC
4340 /* TLBTR is specific to VMSA */
4341 ARMCPRegInfo id_tlbtr_reginfo = {
4342 .name = "TLBTR",
4343 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4344 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4345 };
3281af81
PC
4346 /* MPUIR is specific to PMSA V6+ */
4347 ARMCPRegInfo id_mpuir_reginfo = {
4348 .name = "MPUIR",
4349 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4350 .access = PL1_R, .type = ARM_CP_CONST,
4351 .resetvalue = cpu->pmsav7_dregion << 8
4352 };
7884849c
PM
4353 ARMCPRegInfo crn0_wi_reginfo = {
4354 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4355 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4356 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4357 };
4358 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4359 arm_feature(env, ARM_FEATURE_STRONGARM)) {
4360 ARMCPRegInfo *r;
4361 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
4362 * whole space. Then update the specific ID registers to allow write
4363 * access, so that they ignore writes rather than causing them to
4364 * UNDEF.
7884849c
PM
4365 */
4366 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
4367 for (r = id_pre_v8_midr_cp_reginfo;
4368 r->type != ARM_CP_SENTINEL; r++) {
4369 r->access = PL1_RW;
4370 }
7884849c
PM
4371 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4372 r->access = PL1_RW;
7884849c 4373 }
8085ce63 4374 id_tlbtr_reginfo.access = PL1_RW;
3281af81 4375 id_tlbtr_reginfo.access = PL1_RW;
7884849c 4376 }
00a29f3d
PM
4377 if (arm_feature(env, ARM_FEATURE_V8)) {
4378 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4379 } else {
4380 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4381 }
a703eda1 4382 define_arm_cp_regs(cpu, id_cp_reginfo);
8085ce63
PC
4383 if (!arm_feature(env, ARM_FEATURE_MPU)) {
4384 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
4385 } else if (arm_feature(env, ARM_FEATURE_V7)) {
4386 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 4387 }
7884849c
PM
4388 }
4389
97ce8d61
PC
4390 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4391 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4392 }
4393
2771db27 4394 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
4395 ARMCPRegInfo auxcr_reginfo[] = {
4396 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4397 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4398 .access = PL1_RW, .type = ARM_CP_CONST,
4399 .resetvalue = cpu->reset_auxcr },
4400 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4401 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4402 .access = PL2_RW, .type = ARM_CP_CONST,
4403 .resetvalue = 0 },
4404 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4405 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4406 .access = PL3_RW, .type = ARM_CP_CONST,
4407 .resetvalue = 0 },
4408 REGINFO_SENTINEL
2771db27 4409 };
834a6c69 4410 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
4411 }
4412
d8ba780b 4413 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
4414 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4415 /* 32 bit view is [31:18] 0...0 [43:32]. */
4416 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4417 | extract64(cpu->reset_cbar, 32, 12);
4418 ARMCPRegInfo cbar_reginfo[] = {
4419 { .name = "CBAR",
4420 .type = ARM_CP_CONST,
4421 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4422 .access = PL1_R, .resetvalue = cpu->reset_cbar },
4423 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4424 .type = ARM_CP_CONST,
4425 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4426 .access = PL1_R, .resetvalue = cbar32 },
4427 REGINFO_SENTINEL
4428 };
4429 /* We don't implement a r/w 64 bit CBAR currently */
4430 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4431 define_arm_cp_regs(cpu, cbar_reginfo);
4432 } else {
4433 ARMCPRegInfo cbar = {
4434 .name = "CBAR",
4435 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4436 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4437 .fieldoffset = offsetof(CPUARMState,
4438 cp15.c15_config_base_address)
4439 };
4440 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4441 cbar.access = PL1_R;
4442 cbar.fieldoffset = 0;
4443 cbar.type = ARM_CP_CONST;
4444 }
4445 define_one_arm_cp_reg(cpu, &cbar);
4446 }
d8ba780b
PC
4447 }
4448
2771db27
PM
4449 /* Generic registers whose values depend on the implementation */
4450 {
4451 ARMCPRegInfo sctlr = {
5ebafdf3 4452 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
4453 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4454 .access = PL1_RW,
4455 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4456 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
4457 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4458 .raw_writefn = raw_write,
2771db27
PM
4459 };
4460 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4461 /* Normally we would always end the TB on an SCTLR write, but Linux
4462 * arch/arm/mach-pxa/sleep.S expects two instructions following
4463 * an MMU enable to execute from cache. Imitate this behaviour.
4464 */
4465 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4466 }
4467 define_one_arm_cp_reg(cpu, &sctlr);
4468 }
2ceb98c0
PM
4469}
4470
778c3a06 4471ARMCPU *cpu_arm_init(const char *cpu_model)
40f137e1 4472{
9262685b 4473 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
14969266
AF
4474}
4475
4476void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4477{
22169d41 4478 CPUState *cs = CPU(cpu);
14969266
AF
4479 CPUARMState *env = &cpu->env;
4480
6a669427
PM
4481 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4482 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4483 aarch64_fpu_gdb_set_reg,
4484 34, "aarch64-fpu.xml", 0);
4485 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 4486 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4487 51, "arm-neon.xml", 0);
4488 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 4489 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4490 35, "arm-vfp3.xml", 0);
4491 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 4492 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4493 19, "arm-vfp.xml", 0);
4494 }
40f137e1
PB
4495}
4496
777dc784
PM
4497/* Sort alphabetically by type name, except for "any". */
4498static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 4499{
777dc784
PM
4500 ObjectClass *class_a = (ObjectClass *)a;
4501 ObjectClass *class_b = (ObjectClass *)b;
4502 const char *name_a, *name_b;
5adb4839 4503
777dc784
PM
4504 name_a = object_class_get_name(class_a);
4505 name_b = object_class_get_name(class_b);
51492fd1 4506 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 4507 return 1;
51492fd1 4508 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
4509 return -1;
4510 } else {
4511 return strcmp(name_a, name_b);
5adb4839
PB
4512 }
4513}
4514
777dc784 4515static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 4516{
777dc784 4517 ObjectClass *oc = data;
92a31361 4518 CPUListState *s = user_data;
51492fd1
AF
4519 const char *typename;
4520 char *name;
3371d272 4521
51492fd1
AF
4522 typename = object_class_get_name(oc);
4523 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 4524 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
4525 name);
4526 g_free(name);
777dc784
PM
4527}
4528
4529void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
4530{
92a31361 4531 CPUListState s = {
777dc784
PM
4532 .file = f,
4533 .cpu_fprintf = cpu_fprintf,
4534 };
4535 GSList *list;
4536
4537 list = object_class_get_list(TYPE_ARM_CPU, false);
4538 list = g_slist_sort(list, arm_cpu_list_compare);
4539 (*cpu_fprintf)(f, "Available CPUs:\n");
4540 g_slist_foreach(list, arm_cpu_list_entry, &s);
4541 g_slist_free(list);
a96c0514
PM
4542#ifdef CONFIG_KVM
4543 /* The 'host' CPU type is dynamically registered only if KVM is
4544 * enabled, so we have to special-case it here:
4545 */
4546 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
4547#endif
40f137e1
PB
4548}
4549
78027bb6
CR
4550static void arm_cpu_add_definition(gpointer data, gpointer user_data)
4551{
4552 ObjectClass *oc = data;
4553 CpuDefinitionInfoList **cpu_list = user_data;
4554 CpuDefinitionInfoList *entry;
4555 CpuDefinitionInfo *info;
4556 const char *typename;
4557
4558 typename = object_class_get_name(oc);
4559 info = g_malloc0(sizeof(*info));
4560 info->name = g_strndup(typename,
4561 strlen(typename) - strlen("-" TYPE_ARM_CPU));
4562
4563 entry = g_malloc0(sizeof(*entry));
4564 entry->value = info;
4565 entry->next = *cpu_list;
4566 *cpu_list = entry;
4567}
4568
4569CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
4570{
4571 CpuDefinitionInfoList *cpu_list = NULL;
4572 GSList *list;
4573
4574 list = object_class_get_list(TYPE_ARM_CPU, false);
4575 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
4576 g_slist_free(list);
4577
4578 return cpu_list;
4579}
4580
6e6efd61 4581static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 4582 void *opaque, int state, int secstate,
f5a0a5a5 4583 int crm, int opc1, int opc2)
6e6efd61
PM
4584{
4585 /* Private utility function for define_one_arm_cp_reg_with_opaque():
4586 * add a single reginfo struct to the hash table.
4587 */
4588 uint32_t *key = g_new(uint32_t, 1);
4589 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
4590 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
4591 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
4592
4593 /* Reset the secure state to the specific incoming state. This is
4594 * necessary as the register may have been defined with both states.
4595 */
4596 r2->secure = secstate;
4597
4598 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4599 /* Register is banked (using both entries in array).
4600 * Overwriting fieldoffset as the array is only used to define
4601 * banked registers but later only fieldoffset is used.
f5a0a5a5 4602 */
3f3c82a5
FA
4603 r2->fieldoffset = r->bank_fieldoffsets[ns];
4604 }
4605
4606 if (state == ARM_CP_STATE_AA32) {
4607 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4608 /* If the register is banked then we don't need to migrate or
4609 * reset the 32-bit instance in certain cases:
4610 *
4611 * 1) If the register has both 32-bit and 64-bit instances then we
4612 * can count on the 64-bit instance taking care of the
4613 * non-secure bank.
4614 * 2) If ARMv8 is enabled then we can count on a 64-bit version
4615 * taking care of the secure bank. This requires that separate
4616 * 32 and 64-bit definitions are provided.
4617 */
4618 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
4619 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 4620 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
4621 }
4622 } else if ((secstate != r->secure) && !ns) {
4623 /* The register is not banked so we only want to allow migration of
4624 * the non-secure instance.
4625 */
7a0e58fa 4626 r2->type |= ARM_CP_ALIAS;
58a1d8ce 4627 }
3f3c82a5
FA
4628
4629 if (r->state == ARM_CP_STATE_BOTH) {
4630 /* We assume it is a cp15 register if the .cp field is left unset.
4631 */
4632 if (r2->cp == 0) {
4633 r2->cp = 15;
4634 }
4635
f5a0a5a5 4636#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
4637 if (r2->fieldoffset) {
4638 r2->fieldoffset += sizeof(uint32_t);
4639 }
f5a0a5a5 4640#endif
3f3c82a5 4641 }
f5a0a5a5
PM
4642 }
4643 if (state == ARM_CP_STATE_AA64) {
4644 /* To allow abbreviation of ARMCPRegInfo
4645 * definitions, we treat cp == 0 as equivalent to
4646 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
4647 * STATE_BOTH definitions are also always "standard
4648 * sysreg" in their AArch64 view (the .cp value may
4649 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 4650 */
58a1d8ce 4651 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
4652 r2->cp = CP_REG_ARM64_SYSREG_CP;
4653 }
4654 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
4655 r2->opc0, opc1, opc2);
4656 } else {
51a79b03 4657 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 4658 }
6e6efd61
PM
4659 if (opaque) {
4660 r2->opaque = opaque;
4661 }
67ed771d
PM
4662 /* reginfo passed to helpers is correct for the actual access,
4663 * and is never ARM_CP_STATE_BOTH:
4664 */
4665 r2->state = state;
6e6efd61
PM
4666 /* Make sure reginfo passed to helpers for wildcarded regs
4667 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
4668 */
4669 r2->crm = crm;
4670 r2->opc1 = opc1;
4671 r2->opc2 = opc2;
4672 /* By convention, for wildcarded registers only the first
4673 * entry is used for migration; the others are marked as
7a0e58fa 4674 * ALIAS so we don't try to transfer the register
6e6efd61 4675 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 4676 * never migratable and not even raw-accessible.
6e6efd61 4677 */
7a0e58fa
PM
4678 if ((r->type & ARM_CP_SPECIAL)) {
4679 r2->type |= ARM_CP_NO_RAW;
4680 }
4681 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
4682 ((r->opc1 == CP_ANY) && opc1 != 0) ||
4683 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 4684 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
4685 }
4686
375421cc
PM
4687 /* Check that raw accesses are either forbidden or handled. Note that
4688 * we can't assert this earlier because the setup of fieldoffset for
4689 * banked registers has to be done first.
4690 */
4691 if (!(r2->type & ARM_CP_NO_RAW)) {
4692 assert(!raw_accessors_invalid(r2));
4693 }
4694
6e6efd61
PM
4695 /* Overriding of an existing definition must be explicitly
4696 * requested.
4697 */
4698 if (!(r->type & ARM_CP_OVERRIDE)) {
4699 ARMCPRegInfo *oldreg;
4700 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
4701 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
4702 fprintf(stderr, "Register redefined: cp=%d %d bit "
4703 "crn=%d crm=%d opc1=%d opc2=%d, "
4704 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
4705 r2->crn, r2->crm, r2->opc1, r2->opc2,
4706 oldreg->name, r2->name);
4707 g_assert_not_reached();
4708 }
4709 }
4710 g_hash_table_insert(cpu->cp_regs, key, r2);
4711}
4712
4713
4b6a83fb
PM
4714void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
4715 const ARMCPRegInfo *r, void *opaque)
4716{
4717 /* Define implementations of coprocessor registers.
4718 * We store these in a hashtable because typically
4719 * there are less than 150 registers in a space which
4720 * is 16*16*16*8*8 = 262144 in size.
4721 * Wildcarding is supported for the crm, opc1 and opc2 fields.
4722 * If a register is defined twice then the second definition is
4723 * used, so this can be used to define some generic registers and
4724 * then override them with implementation specific variations.
4725 * At least one of the original and the second definition should
4726 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
4727 * against accidental use.
f5a0a5a5
PM
4728 *
4729 * The state field defines whether the register is to be
4730 * visible in the AArch32 or AArch64 execution state. If the
4731 * state is set to ARM_CP_STATE_BOTH then we synthesise a
4732 * reginfo structure for the AArch32 view, which sees the lower
4733 * 32 bits of the 64 bit register.
4734 *
4735 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
4736 * be wildcarded. AArch64 registers are always considered to be 64
4737 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
4738 * the register, if any.
4b6a83fb 4739 */
f5a0a5a5 4740 int crm, opc1, opc2, state;
4b6a83fb
PM
4741 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
4742 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
4743 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
4744 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
4745 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
4746 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
4747 /* 64 bit registers have only CRm and Opc1 fields */
4748 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
4749 /* op0 only exists in the AArch64 encodings */
4750 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
4751 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
4752 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
4753 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
4754 * encodes a minimum access level for the register. We roll this
4755 * runtime check into our general permission check code, so check
4756 * here that the reginfo's specified permissions are strict enough
4757 * to encompass the generic architectural permission check.
4758 */
4759 if (r->state != ARM_CP_STATE_AA32) {
4760 int mask = 0;
4761 switch (r->opc1) {
4762 case 0: case 1: case 2:
4763 /* min_EL EL1 */
4764 mask = PL1_RW;
4765 break;
4766 case 3:
4767 /* min_EL EL0 */
4768 mask = PL0_RW;
4769 break;
4770 case 4:
4771 /* min_EL EL2 */
4772 mask = PL2_RW;
4773 break;
4774 case 5:
4775 /* unallocated encoding, so not possible */
4776 assert(false);
4777 break;
4778 case 6:
4779 /* min_EL EL3 */
4780 mask = PL3_RW;
4781 break;
4782 case 7:
4783 /* min_EL EL1, secure mode only (we don't check the latter) */
4784 mask = PL1_RW;
4785 break;
4786 default:
4787 /* broken reginfo with out-of-range opc1 */
4788 assert(false);
4789 break;
4790 }
4791 /* assert our permissions are not too lax (stricter is fine) */
4792 assert((r->access & ~mask) == 0);
4793 }
4794
4b6a83fb
PM
4795 /* Check that the register definition has enough info to handle
4796 * reads and writes if they are permitted.
4797 */
4798 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
4799 if (r->access & PL3_R) {
3f3c82a5
FA
4800 assert((r->fieldoffset ||
4801 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
4802 r->readfn);
4b6a83fb
PM
4803 }
4804 if (r->access & PL3_W) {
3f3c82a5
FA
4805 assert((r->fieldoffset ||
4806 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
4807 r->writefn);
4b6a83fb
PM
4808 }
4809 }
4810 /* Bad type field probably means missing sentinel at end of reg list */
4811 assert(cptype_valid(r->type));
4812 for (crm = crmmin; crm <= crmmax; crm++) {
4813 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
4814 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
4815 for (state = ARM_CP_STATE_AA32;
4816 state <= ARM_CP_STATE_AA64; state++) {
4817 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
4818 continue;
4819 }
3f3c82a5
FA
4820 if (state == ARM_CP_STATE_AA32) {
4821 /* Under AArch32 CP registers can be common
4822 * (same for secure and non-secure world) or banked.
4823 */
4824 switch (r->secure) {
4825 case ARM_CP_SECSTATE_S:
4826 case ARM_CP_SECSTATE_NS:
4827 add_cpreg_to_hashtable(cpu, r, opaque, state,
4828 r->secure, crm, opc1, opc2);
4829 break;
4830 default:
4831 add_cpreg_to_hashtable(cpu, r, opaque, state,
4832 ARM_CP_SECSTATE_S,
4833 crm, opc1, opc2);
4834 add_cpreg_to_hashtable(cpu, r, opaque, state,
4835 ARM_CP_SECSTATE_NS,
4836 crm, opc1, opc2);
4837 break;
4838 }
4839 } else {
4840 /* AArch64 registers get mapped to non-secure instance
4841 * of AArch32 */
4842 add_cpreg_to_hashtable(cpu, r, opaque, state,
4843 ARM_CP_SECSTATE_NS,
4844 crm, opc1, opc2);
4845 }
f5a0a5a5 4846 }
4b6a83fb
PM
4847 }
4848 }
4849 }
4850}
4851
4852void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
4853 const ARMCPRegInfo *regs, void *opaque)
4854{
4855 /* Define a whole list of registers */
4856 const ARMCPRegInfo *r;
4857 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
4858 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
4859 }
4860}
4861
60322b39 4862const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 4863{
60322b39 4864 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
4865}
4866
c4241c7d
PM
4867void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
4868 uint64_t value)
4b6a83fb
PM
4869{
4870 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
4871}
4872
c4241c7d 4873uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
4874{
4875 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
4876 return 0;
4877}
4878
f5a0a5a5
PM
4879void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
4880{
4881 /* Helper coprocessor reset function for do-nothing-on-reset registers */
4882}
4883
0ecb72a5 4884static int bad_mode_switch(CPUARMState *env, int mode)
37064a8b
PM
4885{
4886 /* Return true if it is not valid for us to switch to
4887 * this CPU mode (ie all the UNPREDICTABLE cases in
4888 * the ARM ARM CPSRWriteByInstr pseudocode).
4889 */
4890 switch (mode) {
4891 case ARM_CPU_MODE_USR:
4892 case ARM_CPU_MODE_SYS:
4893 case ARM_CPU_MODE_SVC:
4894 case ARM_CPU_MODE_ABT:
4895 case ARM_CPU_MODE_UND:
4896 case ARM_CPU_MODE_IRQ:
4897 case ARM_CPU_MODE_FIQ:
4898 return 0;
027fc527
SF
4899 case ARM_CPU_MODE_MON:
4900 return !arm_is_secure(env);
37064a8b
PM
4901 default:
4902 return 1;
4903 }
4904}
4905
2f4a40e5
AZ
4906uint32_t cpsr_read(CPUARMState *env)
4907{
4908 int ZF;
6fbe23d5
PB
4909 ZF = (env->ZF == 0);
4910 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
4911 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
4912 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
4913 | ((env->condexec_bits & 0xfc) << 8)
af519934 4914 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
4915}
4916
4917void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
4918{
6e8801f9
FA
4919 uint32_t changed_daif;
4920
2f4a40e5 4921 if (mask & CPSR_NZCV) {
6fbe23d5
PB
4922 env->ZF = (~val) & CPSR_Z;
4923 env->NF = val;
2f4a40e5
AZ
4924 env->CF = (val >> 29) & 1;
4925 env->VF = (val << 3) & 0x80000000;
4926 }
4927 if (mask & CPSR_Q)
4928 env->QF = ((val & CPSR_Q) != 0);
4929 if (mask & CPSR_T)
4930 env->thumb = ((val & CPSR_T) != 0);
4931 if (mask & CPSR_IT_0_1) {
4932 env->condexec_bits &= ~3;
4933 env->condexec_bits |= (val >> 25) & 3;
4934 }
4935 if (mask & CPSR_IT_2_7) {
4936 env->condexec_bits &= 3;
4937 env->condexec_bits |= (val >> 8) & 0xfc;
4938 }
4939 if (mask & CPSR_GE) {
4940 env->GE = (val >> 16) & 0xf;
4941 }
4942
6e8801f9
FA
4943 /* In a V7 implementation that includes the security extensions but does
4944 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
4945 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
4946 * bits respectively.
4947 *
4948 * In a V8 implementation, it is permitted for privileged software to
4949 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
4950 */
4951 if (!arm_feature(env, ARM_FEATURE_V8) &&
4952 arm_feature(env, ARM_FEATURE_EL3) &&
4953 !arm_feature(env, ARM_FEATURE_EL2) &&
4954 !arm_is_secure(env)) {
4955
4956 changed_daif = (env->daif ^ val) & mask;
4957
4958 if (changed_daif & CPSR_A) {
4959 /* Check to see if we are allowed to change the masking of async
4960 * abort exceptions from a non-secure state.
4961 */
4962 if (!(env->cp15.scr_el3 & SCR_AW)) {
4963 qemu_log_mask(LOG_GUEST_ERROR,
4964 "Ignoring attempt to switch CPSR_A flag from "
4965 "non-secure world with SCR.AW bit clear\n");
4966 mask &= ~CPSR_A;
4967 }
4968 }
4969
4970 if (changed_daif & CPSR_F) {
4971 /* Check to see if we are allowed to change the masking of FIQ
4972 * exceptions from a non-secure state.
4973 */
4974 if (!(env->cp15.scr_el3 & SCR_FW)) {
4975 qemu_log_mask(LOG_GUEST_ERROR,
4976 "Ignoring attempt to switch CPSR_F flag from "
4977 "non-secure world with SCR.FW bit clear\n");
4978 mask &= ~CPSR_F;
4979 }
4980
4981 /* Check whether non-maskable FIQ (NMFI) support is enabled.
4982 * If this bit is set software is not allowed to mask
4983 * FIQs, but is allowed to set CPSR_F to 0.
4984 */
4985 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
4986 (val & CPSR_F)) {
4987 qemu_log_mask(LOG_GUEST_ERROR,
4988 "Ignoring attempt to enable CPSR_F flag "
4989 "(non-maskable FIQ [NMFI] support enabled)\n");
4990 mask &= ~CPSR_F;
4991 }
4992 }
4993 }
4994
4cc35614
PM
4995 env->daif &= ~(CPSR_AIF & mask);
4996 env->daif |= val & CPSR_AIF & mask;
4997
2f4a40e5 4998 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
37064a8b
PM
4999 if (bad_mode_switch(env, val & CPSR_M)) {
5000 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
5001 * We choose to ignore the attempt and leave the CPSR M field
5002 * untouched.
5003 */
5004 mask &= ~CPSR_M;
5005 } else {
5006 switch_mode(env, val & CPSR_M);
5007 }
2f4a40e5
AZ
5008 }
5009 mask &= ~CACHED_CPSR_BITS;
5010 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5011}
5012
b26eefb6
PB
5013/* Sign/zero extend */
5014uint32_t HELPER(sxtb16)(uint32_t x)
5015{
5016 uint32_t res;
5017 res = (uint16_t)(int8_t)x;
5018 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5019 return res;
5020}
5021
5022uint32_t HELPER(uxtb16)(uint32_t x)
5023{
5024 uint32_t res;
5025 res = (uint16_t)(uint8_t)x;
5026 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5027 return res;
5028}
5029
f51bbbfe
PB
5030uint32_t HELPER(clz)(uint32_t x)
5031{
7bbcb0af 5032 return clz32(x);
f51bbbfe
PB
5033}
5034
3670669c
PB
5035int32_t HELPER(sdiv)(int32_t num, int32_t den)
5036{
5037 if (den == 0)
5038 return 0;
686eeb93
AJ
5039 if (num == INT_MIN && den == -1)
5040 return INT_MIN;
3670669c
PB
5041 return num / den;
5042}
5043
5044uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5045{
5046 if (den == 0)
5047 return 0;
5048 return num / den;
5049}
5050
5051uint32_t HELPER(rbit)(uint32_t x)
5052{
42fedbca 5053 return revbit32(x);
3670669c
PB
5054}
5055
5fafdf24 5056#if defined(CONFIG_USER_ONLY)
b5ff1b31 5057
9ee6e8bb 5058/* These should probably raise undefined insn exceptions. */
0ecb72a5 5059void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 5060{
a47dddd7
AF
5061 ARMCPU *cpu = arm_env_get_cpu(env);
5062
5063 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
5064}
5065
0ecb72a5 5066uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 5067{
a47dddd7
AF
5068 ARMCPU *cpu = arm_env_get_cpu(env);
5069
5070 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
5071 return 0;
5072}
5073
0ecb72a5 5074void switch_mode(CPUARMState *env, int mode)
b5ff1b31 5075{
a47dddd7
AF
5076 ARMCPU *cpu = arm_env_get_cpu(env);
5077
5078 if (mode != ARM_CPU_MODE_USR) {
5079 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5080 }
b5ff1b31
FB
5081}
5082
0ecb72a5 5083void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
9ee6e8bb 5084{
a47dddd7
AF
5085 ARMCPU *cpu = arm_env_get_cpu(env);
5086
5087 cpu_abort(CPU(cpu), "banked r13 write\n");
9ee6e8bb
PB
5088}
5089
0ecb72a5 5090uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
9ee6e8bb 5091{
a47dddd7
AF
5092 ARMCPU *cpu = arm_env_get_cpu(env);
5093
5094 cpu_abort(CPU(cpu), "banked r13 read\n");
9ee6e8bb
PB
5095 return 0;
5096}
5097
012a906b
GB
5098uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5099 uint32_t cur_el, bool secure)
9e729b57
EI
5100{
5101 return 1;
5102}
5103
ce02049d
GB
5104void aarch64_sync_64_to_32(CPUARMState *env)
5105{
5106 g_assert_not_reached();
5107}
5108
b5ff1b31
FB
5109#else
5110
5111/* Map CPU modes onto saved register banks. */
494b00c7 5112int bank_number(int mode)
b5ff1b31
FB
5113{
5114 switch (mode) {
5115 case ARM_CPU_MODE_USR:
5116 case ARM_CPU_MODE_SYS:
5117 return 0;
5118 case ARM_CPU_MODE_SVC:
5119 return 1;
5120 case ARM_CPU_MODE_ABT:
5121 return 2;
5122 case ARM_CPU_MODE_UND:
5123 return 3;
5124 case ARM_CPU_MODE_IRQ:
5125 return 4;
5126 case ARM_CPU_MODE_FIQ:
5127 return 5;
28c9457d
EI
5128 case ARM_CPU_MODE_HYP:
5129 return 6;
5130 case ARM_CPU_MODE_MON:
5131 return 7;
b5ff1b31 5132 }
8f6fd322 5133 g_assert_not_reached();
b5ff1b31
FB
5134}
5135
0ecb72a5 5136void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
5137{
5138 int old_mode;
5139 int i;
5140
5141 old_mode = env->uncached_cpsr & CPSR_M;
5142 if (mode == old_mode)
5143 return;
5144
5145 if (old_mode == ARM_CPU_MODE_FIQ) {
5146 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5147 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5148 } else if (mode == ARM_CPU_MODE_FIQ) {
5149 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5150 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5151 }
5152
f5206413 5153 i = bank_number(old_mode);
b5ff1b31
FB
5154 env->banked_r13[i] = env->regs[13];
5155 env->banked_r14[i] = env->regs[14];
5156 env->banked_spsr[i] = env->spsr;
5157
f5206413 5158 i = bank_number(mode);
b5ff1b31
FB
5159 env->regs[13] = env->banked_r13[i];
5160 env->regs[14] = env->banked_r14[i];
5161 env->spsr = env->banked_spsr[i];
5162}
5163
0eeb17d6
GB
5164/* Physical Interrupt Target EL Lookup Table
5165 *
5166 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5167 *
5168 * The below multi-dimensional table is used for looking up the target
5169 * exception level given numerous condition criteria. Specifically, the
5170 * target EL is based on SCR and HCR routing controls as well as the
5171 * currently executing EL and secure state.
5172 *
5173 * Dimensions:
5174 * target_el_table[2][2][2][2][2][4]
5175 * | | | | | +--- Current EL
5176 * | | | | +------ Non-secure(0)/Secure(1)
5177 * | | | +--------- HCR mask override
5178 * | | +------------ SCR exec state control
5179 * | +--------------- SCR mask override
5180 * +------------------ 32-bit(0)/64-bit(1) EL3
5181 *
5182 * The table values are as such:
5183 * 0-3 = EL0-EL3
5184 * -1 = Cannot occur
5185 *
5186 * The ARM ARM target EL table includes entries indicating that an "exception
5187 * is not taken". The two cases where this is applicable are:
5188 * 1) An exception is taken from EL3 but the SCR does not have the exception
5189 * routed to EL3.
5190 * 2) An exception is taken from EL2 but the HCR does not have the exception
5191 * routed to EL2.
5192 * In these two cases, the below table contain a target of EL1. This value is
5193 * returned as it is expected that the consumer of the table data will check
5194 * for "target EL >= current EL" to ensure the exception is not taken.
5195 *
5196 * SCR HCR
5197 * 64 EA AMO From
5198 * BIT IRQ IMO Non-secure Secure
5199 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
5200 */
82c39f6a 5201static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
5202 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5203 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
5204 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5205 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
5206 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5207 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
5208 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5209 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
5210 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
5211 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
5212 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
5213 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
5214 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5215 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
5216 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5217 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
5218};
5219
5220/*
5221 * Determine the target EL for physical exceptions
5222 */
012a906b
GB
5223uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5224 uint32_t cur_el, bool secure)
0eeb17d6
GB
5225{
5226 CPUARMState *env = cs->env_ptr;
2cde031f 5227 int rw;
0eeb17d6
GB
5228 int scr;
5229 int hcr;
5230 int target_el;
2cde031f
SS
5231 /* Is the highest EL AArch64? */
5232 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
5233
5234 if (arm_feature(env, ARM_FEATURE_EL3)) {
5235 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5236 } else {
5237 /* Either EL2 is the highest EL (and so the EL2 register width
5238 * is given by is64); or there is no EL2 or EL3, in which case
5239 * the value of 'rw' does not affect the table lookup anyway.
5240 */
5241 rw = is64;
5242 }
0eeb17d6
GB
5243
5244 switch (excp_idx) {
5245 case EXCP_IRQ:
5246 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5247 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5248 break;
5249 case EXCP_FIQ:
5250 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5251 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5252 break;
5253 default:
5254 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5255 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5256 break;
5257 };
5258
5259 /* If HCR.TGE is set then HCR is treated as being 1 */
5260 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5261
5262 /* Perform a table-lookup for the target EL given the current state */
5263 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5264
5265 assert(target_el > 0);
5266
5267 return target_el;
5268}
5269
9ee6e8bb
PB
5270static void v7m_push(CPUARMState *env, uint32_t val)
5271{
70d74660
AF
5272 CPUState *cs = CPU(arm_env_get_cpu(env));
5273
9ee6e8bb 5274 env->regs[13] -= 4;
ab1da857 5275 stl_phys(cs->as, env->regs[13], val);
9ee6e8bb
PB
5276}
5277
5278static uint32_t v7m_pop(CPUARMState *env)
5279{
70d74660 5280 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb 5281 uint32_t val;
70d74660 5282
fdfba1a2 5283 val = ldl_phys(cs->as, env->regs[13]);
9ee6e8bb
PB
5284 env->regs[13] += 4;
5285 return val;
5286}
5287
5288/* Switch to V7M main or process stack pointer. */
5289static void switch_v7m_sp(CPUARMState *env, int process)
5290{
5291 uint32_t tmp;
5292 if (env->v7m.current_sp != process) {
5293 tmp = env->v7m.other_sp;
5294 env->v7m.other_sp = env->regs[13];
5295 env->regs[13] = tmp;
5296 env->v7m.current_sp = process;
5297 }
5298}
5299
5300static void do_v7m_exception_exit(CPUARMState *env)
5301{
5302 uint32_t type;
5303 uint32_t xpsr;
5304
5305 type = env->regs[15];
5306 if (env->v7m.exception != 0)
983fe826 5307 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
9ee6e8bb
PB
5308
5309 /* Switch to the target stack. */
5310 switch_v7m_sp(env, (type & 4) != 0);
5311 /* Pop registers. */
5312 env->regs[0] = v7m_pop(env);
5313 env->regs[1] = v7m_pop(env);
5314 env->regs[2] = v7m_pop(env);
5315 env->regs[3] = v7m_pop(env);
5316 env->regs[12] = v7m_pop(env);
5317 env->regs[14] = v7m_pop(env);
5318 env->regs[15] = v7m_pop(env);
fcf83ab1
PM
5319 if (env->regs[15] & 1) {
5320 qemu_log_mask(LOG_GUEST_ERROR,
5321 "M profile return from interrupt with misaligned "
5322 "PC is UNPREDICTABLE\n");
5323 /* Actual hardware seems to ignore the lsbit, and there are several
5324 * RTOSes out there which incorrectly assume the r15 in the stack
5325 * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5326 */
5327 env->regs[15] &= ~1U;
5328 }
9ee6e8bb
PB
5329 xpsr = v7m_pop(env);
5330 xpsr_write(env, xpsr, 0xfffffdff);
5331 /* Undo stack alignment. */
5332 if (xpsr & 0x200)
5333 env->regs[13] |= 4;
5334 /* ??? The exception return type specifies Thread/Handler mode. However
5335 this is also implied by the xPSR value. Not sure what to do
5336 if there is a mismatch. */
5337 /* ??? Likewise for mismatches between the CONTROL register and the stack
5338 pointer. */
5339}
5340
e6f010cc 5341void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 5342{
e6f010cc
AF
5343 ARMCPU *cpu = ARM_CPU(cs);
5344 CPUARMState *env = &cpu->env;
9ee6e8bb
PB
5345 uint32_t xpsr = xpsr_read(env);
5346 uint32_t lr;
5347 uint32_t addr;
5348
27103424 5349 arm_log_exception(cs->exception_index);
3f1beaca 5350
9ee6e8bb
PB
5351 lr = 0xfffffff1;
5352 if (env->v7m.current_sp)
5353 lr |= 4;
5354 if (env->v7m.exception == 0)
5355 lr |= 8;
5356
5357 /* For exceptions we just mark as pending on the NVIC, and let that
5358 handle it. */
5359 /* TODO: Need to escalate if the current priority is higher than the
5360 one we're raising. */
27103424 5361 switch (cs->exception_index) {
9ee6e8bb 5362 case EXCP_UDEF:
983fe826 5363 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
9ee6e8bb
PB
5364 return;
5365 case EXCP_SWI:
314e2296 5366 /* The PC already points to the next instruction. */
983fe826 5367 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
9ee6e8bb
PB
5368 return;
5369 case EXCP_PREFETCH_ABORT:
5370 case EXCP_DATA_ABORT:
abf1172f
PM
5371 /* TODO: if we implemented the MPU registers, this is where we
5372 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5373 */
983fe826 5374 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
9ee6e8bb
PB
5375 return;
5376 case EXCP_BKPT:
cfe67cef 5377 if (semihosting_enabled()) {
2ad207d4 5378 int nr;
d31dd73e 5379 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2ad207d4
PB
5380 if (nr == 0xab) {
5381 env->regs[15] += 2;
205ace55
CC
5382 qemu_log_mask(CPU_LOG_INT,
5383 "...handling as semihosting call 0x%x\n",
5384 env->regs[0]);
2ad207d4
PB
5385 env->regs[0] = do_arm_semihosting(env);
5386 return;
5387 }
5388 }
983fe826 5389 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
9ee6e8bb
PB
5390 return;
5391 case EXCP_IRQ:
983fe826 5392 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
9ee6e8bb
PB
5393 break;
5394 case EXCP_EXCEPTION_EXIT:
5395 do_v7m_exception_exit(env);
5396 return;
5397 default:
a47dddd7 5398 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
5399 return; /* Never happens. Keep compiler happy. */
5400 }
5401
5402 /* Align stack pointer. */
5403 /* ??? Should only do this if Configuration Control Register
5404 STACKALIGN bit is set. */
5405 if (env->regs[13] & 4) {
ab19b0ec 5406 env->regs[13] -= 4;
9ee6e8bb
PB
5407 xpsr |= 0x200;
5408 }
6c95676b 5409 /* Switch to the handler mode. */
9ee6e8bb
PB
5410 v7m_push(env, xpsr);
5411 v7m_push(env, env->regs[15]);
5412 v7m_push(env, env->regs[14]);
5413 v7m_push(env, env->regs[12]);
5414 v7m_push(env, env->regs[3]);
5415 v7m_push(env, env->regs[2]);
5416 v7m_push(env, env->regs[1]);
5417 v7m_push(env, env->regs[0]);
5418 switch_v7m_sp(env, 0);
c98d174c
PM
5419 /* Clear IT bits */
5420 env->condexec_bits = 0;
9ee6e8bb 5421 env->regs[14] = lr;
fdfba1a2 5422 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
9ee6e8bb
PB
5423 env->regs[15] = addr & 0xfffffffe;
5424 env->thumb = addr & 1;
5425}
5426
ce02049d
GB
5427/* Function used to synchronize QEMU's AArch64 register set with AArch32
5428 * register set. This is necessary when switching between AArch32 and AArch64
5429 * execution state.
5430 */
5431void aarch64_sync_32_to_64(CPUARMState *env)
5432{
5433 int i;
5434 uint32_t mode = env->uncached_cpsr & CPSR_M;
5435
5436 /* We can blanket copy R[0:7] to X[0:7] */
5437 for (i = 0; i < 8; i++) {
5438 env->xregs[i] = env->regs[i];
5439 }
5440
5441 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5442 * Otherwise, they come from the banked user regs.
5443 */
5444 if (mode == ARM_CPU_MODE_FIQ) {
5445 for (i = 8; i < 13; i++) {
5446 env->xregs[i] = env->usr_regs[i - 8];
5447 }
5448 } else {
5449 for (i = 8; i < 13; i++) {
5450 env->xregs[i] = env->regs[i];
5451 }
5452 }
5453
5454 /* Registers x13-x23 are the various mode SP and FP registers. Registers
5455 * r13 and r14 are only copied if we are in that mode, otherwise we copy
5456 * from the mode banked register.
5457 */
5458 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5459 env->xregs[13] = env->regs[13];
5460 env->xregs[14] = env->regs[14];
5461 } else {
5462 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5463 /* HYP is an exception in that it is copied from r14 */
5464 if (mode == ARM_CPU_MODE_HYP) {
5465 env->xregs[14] = env->regs[14];
5466 } else {
5467 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5468 }
5469 }
5470
5471 if (mode == ARM_CPU_MODE_HYP) {
5472 env->xregs[15] = env->regs[13];
5473 } else {
5474 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5475 }
5476
5477 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
5478 env->xregs[16] = env->regs[14];
5479 env->xregs[17] = env->regs[13];
ce02049d 5480 } else {
3a9148d0
SS
5481 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5482 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
5483 }
5484
5485 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
5486 env->xregs[18] = env->regs[14];
5487 env->xregs[19] = env->regs[13];
ce02049d 5488 } else {
3a9148d0
SS
5489 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5490 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
5491 }
5492
5493 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
5494 env->xregs[20] = env->regs[14];
5495 env->xregs[21] = env->regs[13];
ce02049d 5496 } else {
3a9148d0
SS
5497 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5498 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
5499 }
5500
5501 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
5502 env->xregs[22] = env->regs[14];
5503 env->xregs[23] = env->regs[13];
ce02049d 5504 } else {
3a9148d0
SS
5505 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
5506 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
5507 }
5508
5509 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
5510 * mode, then we can copy from r8-r14. Otherwise, we copy from the
5511 * FIQ bank for r8-r14.
5512 */
5513 if (mode == ARM_CPU_MODE_FIQ) {
5514 for (i = 24; i < 31; i++) {
5515 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
5516 }
5517 } else {
5518 for (i = 24; i < 29; i++) {
5519 env->xregs[i] = env->fiq_regs[i - 24];
5520 }
5521 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
5522 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
5523 }
5524
5525 env->pc = env->regs[15];
5526}
5527
5528/* Function used to synchronize QEMU's AArch32 register set with AArch64
5529 * register set. This is necessary when switching between AArch32 and AArch64
5530 * execution state.
5531 */
5532void aarch64_sync_64_to_32(CPUARMState *env)
5533{
5534 int i;
5535 uint32_t mode = env->uncached_cpsr & CPSR_M;
5536
5537 /* We can blanket copy X[0:7] to R[0:7] */
5538 for (i = 0; i < 8; i++) {
5539 env->regs[i] = env->xregs[i];
5540 }
5541
5542 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
5543 * Otherwise, we copy x8-x12 into the banked user regs.
5544 */
5545 if (mode == ARM_CPU_MODE_FIQ) {
5546 for (i = 8; i < 13; i++) {
5547 env->usr_regs[i - 8] = env->xregs[i];
5548 }
5549 } else {
5550 for (i = 8; i < 13; i++) {
5551 env->regs[i] = env->xregs[i];
5552 }
5553 }
5554
5555 /* Registers r13 & r14 depend on the current mode.
5556 * If we are in a given mode, we copy the corresponding x registers to r13
5557 * and r14. Otherwise, we copy the x register to the banked r13 and r14
5558 * for the mode.
5559 */
5560 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5561 env->regs[13] = env->xregs[13];
5562 env->regs[14] = env->xregs[14];
5563 } else {
5564 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
5565
5566 /* HYP is an exception in that it does not have its own banked r14 but
5567 * shares the USR r14
5568 */
5569 if (mode == ARM_CPU_MODE_HYP) {
5570 env->regs[14] = env->xregs[14];
5571 } else {
5572 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
5573 }
5574 }
5575
5576 if (mode == ARM_CPU_MODE_HYP) {
5577 env->regs[13] = env->xregs[15];
5578 } else {
5579 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
5580 }
5581
5582 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
5583 env->regs[14] = env->xregs[16];
5584 env->regs[13] = env->xregs[17];
ce02049d 5585 } else {
3a9148d0
SS
5586 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
5587 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
5588 }
5589
5590 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
5591 env->regs[14] = env->xregs[18];
5592 env->regs[13] = env->xregs[19];
ce02049d 5593 } else {
3a9148d0
SS
5594 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
5595 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
5596 }
5597
5598 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
5599 env->regs[14] = env->xregs[20];
5600 env->regs[13] = env->xregs[21];
ce02049d 5601 } else {
3a9148d0
SS
5602 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
5603 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
5604 }
5605
5606 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
5607 env->regs[14] = env->xregs[22];
5608 env->regs[13] = env->xregs[23];
ce02049d 5609 } else {
3a9148d0
SS
5610 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
5611 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
5612 }
5613
5614 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
5615 * mode, then we can copy to r8-r14. Otherwise, we copy to the
5616 * FIQ bank for r8-r14.
5617 */
5618 if (mode == ARM_CPU_MODE_FIQ) {
5619 for (i = 24; i < 31; i++) {
5620 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
5621 }
5622 } else {
5623 for (i = 24; i < 29; i++) {
5624 env->fiq_regs[i - 24] = env->xregs[i];
5625 }
5626 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
5627 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
5628 }
5629
5630 env->regs[15] = env->pc;
5631}
5632
b5ff1b31 5633/* Handle a CPU exception. */
97a8ea5a 5634void arm_cpu_do_interrupt(CPUState *cs)
b5ff1b31 5635{
97a8ea5a
AF
5636 ARMCPU *cpu = ARM_CPU(cs);
5637 CPUARMState *env = &cpu->env;
b5ff1b31
FB
5638 uint32_t addr;
5639 uint32_t mask;
5640 int new_mode;
5641 uint32_t offset;
16a906fd 5642 uint32_t moe;
b5ff1b31 5643
e6f010cc
AF
5644 assert(!IS_M(env));
5645
27103424 5646 arm_log_exception(cs->exception_index);
3f1beaca 5647
98128601
RH
5648 if (arm_is_psci_call(cpu, cs->exception_index)) {
5649 arm_handle_psci_call(cpu);
5650 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
5651 return;
5652 }
5653
16a906fd
PM
5654 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
5655 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
5656 case EC_BREAKPOINT:
5657 case EC_BREAKPOINT_SAME_EL:
5658 moe = 1;
5659 break;
5660 case EC_WATCHPOINT:
5661 case EC_WATCHPOINT_SAME_EL:
5662 moe = 10;
5663 break;
5664 case EC_AA32_BKPT:
5665 moe = 3;
5666 break;
5667 case EC_VECTORCATCH:
5668 moe = 5;
5669 break;
5670 default:
5671 moe = 0;
5672 break;
5673 }
5674
5675 if (moe) {
5676 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
5677 }
5678
b5ff1b31 5679 /* TODO: Vectored interrupt controller. */
27103424 5680 switch (cs->exception_index) {
b5ff1b31
FB
5681 case EXCP_UDEF:
5682 new_mode = ARM_CPU_MODE_UND;
5683 addr = 0x04;
5684 mask = CPSR_I;
5685 if (env->thumb)
5686 offset = 2;
5687 else
5688 offset = 4;
5689 break;
5690 case EXCP_SWI:
cfe67cef 5691 if (semihosting_enabled()) {
8e71621f
PB
5692 /* Check for semihosting interrupt. */
5693 if (env->thumb) {
d31dd73e
BS
5694 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
5695 & 0xff;
8e71621f 5696 } else {
d31dd73e 5697 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
d8fd2954 5698 & 0xffffff;
8e71621f
PB
5699 }
5700 /* Only intercept calls from privileged modes, to provide some
5701 semblance of security. */
5702 if (((mask == 0x123456 && !env->thumb)
5703 || (mask == 0xab && env->thumb))
5704 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
205ace55
CC
5705 qemu_log_mask(CPU_LOG_INT,
5706 "...handling as semihosting call 0x%x\n",
5707 env->regs[0]);
8e71621f
PB
5708 env->regs[0] = do_arm_semihosting(env);
5709 return;
5710 }
5711 }
b5ff1b31
FB
5712 new_mode = ARM_CPU_MODE_SVC;
5713 addr = 0x08;
5714 mask = CPSR_I;
601d70b9 5715 /* The PC already points to the next instruction. */
b5ff1b31
FB
5716 offset = 0;
5717 break;
06c949e6 5718 case EXCP_BKPT:
9ee6e8bb 5719 /* See if this is a semihosting syscall. */
cfe67cef 5720 if (env->thumb && semihosting_enabled()) {
d31dd73e 5721 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
9ee6e8bb
PB
5722 if (mask == 0xab
5723 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
5724 env->regs[15] += 2;
205ace55
CC
5725 qemu_log_mask(CPU_LOG_INT,
5726 "...handling as semihosting call 0x%x\n",
5727 env->regs[0]);
9ee6e8bb
PB
5728 env->regs[0] = do_arm_semihosting(env);
5729 return;
5730 }
5731 }
abf1172f 5732 env->exception.fsr = 2;
9ee6e8bb
PB
5733 /* Fall through to prefetch abort. */
5734 case EXCP_PREFETCH_ABORT:
88ca1c2d 5735 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 5736 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 5737 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 5738 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
5739 new_mode = ARM_CPU_MODE_ABT;
5740 addr = 0x0c;
5741 mask = CPSR_A | CPSR_I;
5742 offset = 4;
5743 break;
5744 case EXCP_DATA_ABORT:
4a7e2d73 5745 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 5746 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 5747 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 5748 env->exception.fsr,
6cd8a264 5749 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
5750 new_mode = ARM_CPU_MODE_ABT;
5751 addr = 0x10;
5752 mask = CPSR_A | CPSR_I;
5753 offset = 8;
5754 break;
5755 case EXCP_IRQ:
5756 new_mode = ARM_CPU_MODE_IRQ;
5757 addr = 0x18;
5758 /* Disable IRQ and imprecise data aborts. */
5759 mask = CPSR_A | CPSR_I;
5760 offset = 4;
de38d23b
FA
5761 if (env->cp15.scr_el3 & SCR_IRQ) {
5762 /* IRQ routed to monitor mode */
5763 new_mode = ARM_CPU_MODE_MON;
5764 mask |= CPSR_F;
5765 }
b5ff1b31
FB
5766 break;
5767 case EXCP_FIQ:
5768 new_mode = ARM_CPU_MODE_FIQ;
5769 addr = 0x1c;
5770 /* Disable FIQ, IRQ and imprecise data aborts. */
5771 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
5772 if (env->cp15.scr_el3 & SCR_FIQ) {
5773 /* FIQ routed to monitor mode */
5774 new_mode = ARM_CPU_MODE_MON;
5775 }
b5ff1b31
FB
5776 offset = 4;
5777 break;
dbe9d163
FA
5778 case EXCP_SMC:
5779 new_mode = ARM_CPU_MODE_MON;
5780 addr = 0x08;
5781 mask = CPSR_A | CPSR_I | CPSR_F;
5782 offset = 0;
5783 break;
b5ff1b31 5784 default:
a47dddd7 5785 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
5786 return; /* Never happens. Keep compiler happy. */
5787 }
e89e51a1
FA
5788
5789 if (new_mode == ARM_CPU_MODE_MON) {
5790 addr += env->cp15.mvbar;
137feaa9 5791 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 5792 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 5793 addr += 0xffff0000;
8641136c
NR
5794 } else {
5795 /* ARM v7 architectures provide a vector base address register to remap
5796 * the interrupt vector table.
e89e51a1 5797 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
5798 * Note: only bits 31:5 are valid.
5799 */
fb6c91ba 5800 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 5801 }
dbe9d163
FA
5802
5803 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
5804 env->cp15.scr_el3 &= ~SCR_NS;
5805 }
5806
b5ff1b31 5807 switch_mode (env, new_mode);
662cefb7
PM
5808 /* For exceptions taken to AArch32 we must clear the SS bit in both
5809 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
5810 */
5811 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 5812 env->spsr = cpsr_read(env);
9ee6e8bb
PB
5813 /* Clear IT bits. */
5814 env->condexec_bits = 0;
30a8cac1 5815 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 5816 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
4cc35614 5817 env->daif |= mask;
be5e7a76
DES
5818 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
5819 * and we should just guard the thumb mode on V4 */
5820 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 5821 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 5822 }
b5ff1b31
FB
5823 env->regs[14] = env->regs[15] + offset;
5824 env->regs[15] = addr;
259186a7 5825 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
b5ff1b31
FB
5826}
5827
0480f69a
PM
5828
5829/* Return the exception level which controls this address translation regime */
5830static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
5831{
5832 switch (mmu_idx) {
5833 case ARMMMUIdx_S2NS:
5834 case ARMMMUIdx_S1E2:
5835 return 2;
5836 case ARMMMUIdx_S1E3:
5837 return 3;
5838 case ARMMMUIdx_S1SE0:
5839 return arm_el_is_aa64(env, 3) ? 1 : 3;
5840 case ARMMMUIdx_S1SE1:
5841 case ARMMMUIdx_S1NSE0:
5842 case ARMMMUIdx_S1NSE1:
5843 return 1;
5844 default:
5845 g_assert_not_reached();
5846 }
5847}
5848
8bf5b6a9
PM
5849/* Return true if this address translation regime is secure */
5850static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
5851{
5852 switch (mmu_idx) {
5853 case ARMMMUIdx_S12NSE0:
5854 case ARMMMUIdx_S12NSE1:
5855 case ARMMMUIdx_S1NSE0:
5856 case ARMMMUIdx_S1NSE1:
5857 case ARMMMUIdx_S1E2:
5858 case ARMMMUIdx_S2NS:
5859 return false;
5860 case ARMMMUIdx_S1E3:
5861 case ARMMMUIdx_S1SE0:
5862 case ARMMMUIdx_S1SE1:
5863 return true;
5864 default:
5865 g_assert_not_reached();
5866 }
5867}
5868
0480f69a
PM
5869/* Return the SCTLR value which controls this address translation regime */
5870static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
5871{
5872 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
5873}
5874
5875/* Return true if the specified stage of address translation is disabled */
5876static inline bool regime_translation_disabled(CPUARMState *env,
5877 ARMMMUIdx mmu_idx)
5878{
5879 if (mmu_idx == ARMMMUIdx_S2NS) {
5880 return (env->cp15.hcr_el2 & HCR_VM) == 0;
5881 }
5882 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
5883}
5884
5885/* Return the TCR controlling this translation regime */
5886static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
5887{
5888 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 5889 return &env->cp15.vtcr_el2;
0480f69a
PM
5890 }
5891 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
5892}
5893
aef878be
GB
5894/* Return the TTBR associated with this translation regime */
5895static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
5896 int ttbrn)
5897{
5898 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 5899 return env->cp15.vttbr_el2;
aef878be
GB
5900 }
5901 if (ttbrn == 0) {
5902 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
5903 } else {
5904 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
5905 }
5906}
5907
0480f69a
PM
5908/* Return true if the translation regime is using LPAE format page tables */
5909static inline bool regime_using_lpae_format(CPUARMState *env,
5910 ARMMMUIdx mmu_idx)
5911{
5912 int el = regime_el(env, mmu_idx);
5913 if (el == 2 || arm_el_is_aa64(env, el)) {
5914 return true;
5915 }
5916 if (arm_feature(env, ARM_FEATURE_LPAE)
5917 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
5918 return true;
5919 }
5920 return false;
5921}
5922
5923static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
5924{
5925 switch (mmu_idx) {
5926 case ARMMMUIdx_S1SE0:
5927 case ARMMMUIdx_S1NSE0:
5928 return true;
5929 default:
5930 return false;
5931 case ARMMMUIdx_S12NSE0:
5932 case ARMMMUIdx_S12NSE1:
5933 g_assert_not_reached();
5934 }
5935}
5936
0fbf5238
AJ
5937/* Translate section/page access permissions to page
5938 * R/W protection flags
d76951b6
AJ
5939 *
5940 * @env: CPUARMState
5941 * @mmu_idx: MMU index indicating required translation regime
5942 * @ap: The 3-bit access permissions (AP[2:0])
5943 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
5944 */
5945static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
5946 int ap, int domain_prot)
5947{
554b0b09
PM
5948 bool is_user = regime_is_user(env, mmu_idx);
5949
5950 if (domain_prot == 3) {
5951 return PAGE_READ | PAGE_WRITE;
5952 }
5953
554b0b09
PM
5954 switch (ap) {
5955 case 0:
5956 if (arm_feature(env, ARM_FEATURE_V7)) {
5957 return 0;
5958 }
554b0b09
PM
5959 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
5960 case SCTLR_S:
5961 return is_user ? 0 : PAGE_READ;
5962 case SCTLR_R:
5963 return PAGE_READ;
5964 default:
5965 return 0;
5966 }
5967 case 1:
5968 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
5969 case 2:
87c3d486 5970 if (is_user) {
0fbf5238 5971 return PAGE_READ;
87c3d486 5972 } else {
554b0b09 5973 return PAGE_READ | PAGE_WRITE;
87c3d486 5974 }
554b0b09
PM
5975 case 3:
5976 return PAGE_READ | PAGE_WRITE;
5977 case 4: /* Reserved. */
5978 return 0;
5979 case 5:
0fbf5238 5980 return is_user ? 0 : PAGE_READ;
554b0b09 5981 case 6:
0fbf5238 5982 return PAGE_READ;
554b0b09 5983 case 7:
87c3d486 5984 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 5985 return 0;
87c3d486 5986 }
0fbf5238 5987 return PAGE_READ;
554b0b09 5988 default:
0fbf5238 5989 g_assert_not_reached();
554b0b09 5990 }
b5ff1b31
FB
5991}
5992
d76951b6
AJ
5993/* Translate section/page access permissions to page
5994 * R/W protection flags.
5995 *
d76951b6 5996 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 5997 * @is_user: TRUE if accessing from PL0
d76951b6 5998 */
d8e052b3 5999static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 6000{
d76951b6
AJ
6001 switch (ap) {
6002 case 0:
6003 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6004 case 1:
6005 return PAGE_READ | PAGE_WRITE;
6006 case 2:
6007 return is_user ? 0 : PAGE_READ;
6008 case 3:
6009 return PAGE_READ;
6010 default:
6011 g_assert_not_reached();
6012 }
6013}
6014
d8e052b3
AJ
6015static inline int
6016simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
6017{
6018 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
6019}
6020
6021/* Translate section/page access permissions to protection flags
6022 *
6023 * @env: CPUARMState
6024 * @mmu_idx: MMU index indicating required translation regime
6025 * @is_aa64: TRUE if AArch64
6026 * @ap: The 2-bit simple AP (AP[2:1])
6027 * @ns: NS (non-secure) bit
6028 * @xn: XN (execute-never) bit
6029 * @pxn: PXN (privileged execute-never) bit
6030 */
6031static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
6032 int ap, int ns, int xn, int pxn)
6033{
6034 bool is_user = regime_is_user(env, mmu_idx);
6035 int prot_rw, user_rw;
6036 bool have_wxn;
6037 int wxn = 0;
6038
6039 assert(mmu_idx != ARMMMUIdx_S2NS);
6040
6041 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
6042 if (is_user) {
6043 prot_rw = user_rw;
6044 } else {
6045 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
6046 }
6047
6048 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
6049 return prot_rw;
6050 }
6051
6052 /* TODO have_wxn should be replaced with
6053 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
6054 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
6055 * compatible processors have EL2, which is required for [U]WXN.
6056 */
6057 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
6058
6059 if (have_wxn) {
6060 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
6061 }
6062
6063 if (is_aa64) {
6064 switch (regime_el(env, mmu_idx)) {
6065 case 1:
6066 if (!is_user) {
6067 xn = pxn || (user_rw & PAGE_WRITE);
6068 }
6069 break;
6070 case 2:
6071 case 3:
6072 break;
6073 }
6074 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6075 switch (regime_el(env, mmu_idx)) {
6076 case 1:
6077 case 3:
6078 if (is_user) {
6079 xn = xn || !(user_rw & PAGE_READ);
6080 } else {
6081 int uwxn = 0;
6082 if (have_wxn) {
6083 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6084 }
6085 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6086 (uwxn && (user_rw & PAGE_WRITE));
6087 }
6088 break;
6089 case 2:
6090 break;
6091 }
6092 } else {
6093 xn = wxn = 0;
6094 }
6095
6096 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6097 return prot_rw;
6098 }
6099 return prot_rw | PAGE_EXEC;
6100}
6101
0480f69a
PM
6102static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6103 uint32_t *table, uint32_t address)
b2fa1797 6104{
0480f69a 6105 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 6106 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 6107
11f136ee
FA
6108 if (address & tcr->mask) {
6109 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
6110 /* Translation table walk disabled for TTBR1 */
6111 return false;
6112 }
aef878be 6113 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 6114 } else {
11f136ee 6115 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
6116 /* Translation table walk disabled for TTBR0 */
6117 return false;
6118 }
aef878be 6119 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
6120 }
6121 *table |= (address >> 18) & 0x3ffc;
6122 return true;
b2fa1797
PB
6123}
6124
ebca90e4
PM
6125/* All loads done in the course of a page table walk go through here.
6126 * TODO: rather than ignoring errors from physical memory reads (which
6127 * are external aborts in ARM terminology) we should propagate this
6128 * error out so that we can turn it into a Data Abort if this walk
6129 * was being done for a CPU load/store or an address translation instruction
6130 * (but not if it was for a debug access).
6131 */
6132static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure)
6133{
6134 MemTxAttrs attrs = {};
6135
6136 attrs.secure = is_secure;
6137 return address_space_ldl(cs->as, addr, attrs, NULL);
6138}
6139
6140static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure)
6141{
6142 MemTxAttrs attrs = {};
6143
6144 attrs.secure = is_secure;
6145 return address_space_ldq(cs->as, addr, attrs, NULL);
6146}
6147
b7cc4e82
PC
6148static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6149 int access_type, ARMMMUIdx mmu_idx,
6150 hwaddr *phys_ptr, int *prot,
6151 target_ulong *page_size, uint32_t *fsr)
b5ff1b31 6152{
70d74660 6153 CPUState *cs = CPU(arm_env_get_cpu(env));
b5ff1b31
FB
6154 int code;
6155 uint32_t table;
6156 uint32_t desc;
6157 int type;
6158 int ap;
e389be16 6159 int domain = 0;
dd4ebc2e 6160 int domain_prot;
a8170e5e 6161 hwaddr phys_addr;
0480f69a 6162 uint32_t dacr;
b5ff1b31 6163
9ee6e8bb
PB
6164 /* Pagetable walk. */
6165 /* Lookup l1 descriptor. */
0480f69a 6166 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
6167 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6168 code = 5;
6169 goto do_fault;
6170 }
ebca90e4 6171 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
9ee6e8bb 6172 type = (desc & 3);
dd4ebc2e 6173 domain = (desc >> 5) & 0x0f;
0480f69a
PM
6174 if (regime_el(env, mmu_idx) == 1) {
6175 dacr = env->cp15.dacr_ns;
6176 } else {
6177 dacr = env->cp15.dacr_s;
6178 }
6179 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 6180 if (type == 0) {
601d70b9 6181 /* Section translation fault. */
9ee6e8bb
PB
6182 code = 5;
6183 goto do_fault;
6184 }
dd4ebc2e 6185 if (domain_prot == 0 || domain_prot == 2) {
9ee6e8bb
PB
6186 if (type == 2)
6187 code = 9; /* Section domain fault. */
6188 else
6189 code = 11; /* Page domain fault. */
6190 goto do_fault;
6191 }
6192 if (type == 2) {
6193 /* 1Mb section. */
6194 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6195 ap = (desc >> 10) & 3;
6196 code = 13;
d4c430a8 6197 *page_size = 1024 * 1024;
9ee6e8bb
PB
6198 } else {
6199 /* Lookup l2 entry. */
554b0b09
PM
6200 if (type == 1) {
6201 /* Coarse pagetable. */
6202 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6203 } else {
6204 /* Fine pagetable. */
6205 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6206 }
ebca90e4 6207 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
9ee6e8bb
PB
6208 switch (desc & 3) {
6209 case 0: /* Page translation fault. */
6210 code = 7;
6211 goto do_fault;
6212 case 1: /* 64k page. */
6213 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6214 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 6215 *page_size = 0x10000;
ce819861 6216 break;
9ee6e8bb
PB
6217 case 2: /* 4k page. */
6218 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 6219 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 6220 *page_size = 0x1000;
ce819861 6221 break;
fc1891c7 6222 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 6223 if (type == 1) {
fc1891c7
PM
6224 /* ARMv6/XScale extended small page format */
6225 if (arm_feature(env, ARM_FEATURE_XSCALE)
6226 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 6227 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 6228 *page_size = 0x1000;
554b0b09 6229 } else {
fc1891c7
PM
6230 /* UNPREDICTABLE in ARMv5; we choose to take a
6231 * page translation fault.
6232 */
554b0b09
PM
6233 code = 7;
6234 goto do_fault;
6235 }
6236 } else {
6237 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 6238 *page_size = 0x400;
554b0b09 6239 }
9ee6e8bb 6240 ap = (desc >> 4) & 3;
ce819861
PB
6241 break;
6242 default:
9ee6e8bb
PB
6243 /* Never happens, but compiler isn't smart enough to tell. */
6244 abort();
ce819861 6245 }
9ee6e8bb
PB
6246 code = 15;
6247 }
0fbf5238
AJ
6248 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6249 *prot |= *prot ? PAGE_EXEC : 0;
6250 if (!(*prot & (1 << access_type))) {
9ee6e8bb
PB
6251 /* Access permission fault. */
6252 goto do_fault;
6253 }
6254 *phys_ptr = phys_addr;
b7cc4e82 6255 return false;
9ee6e8bb 6256do_fault:
b7cc4e82
PC
6257 *fsr = code | (domain << 4);
6258 return true;
9ee6e8bb
PB
6259}
6260
b7cc4e82
PC
6261static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
6262 int access_type, ARMMMUIdx mmu_idx,
6263 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
6264 target_ulong *page_size, uint32_t *fsr)
9ee6e8bb 6265{
70d74660 6266 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb
PB
6267 int code;
6268 uint32_t table;
6269 uint32_t desc;
6270 uint32_t xn;
de9b05b8 6271 uint32_t pxn = 0;
9ee6e8bb
PB
6272 int type;
6273 int ap;
de9b05b8 6274 int domain = 0;
dd4ebc2e 6275 int domain_prot;
a8170e5e 6276 hwaddr phys_addr;
0480f69a 6277 uint32_t dacr;
8bf5b6a9 6278 bool ns;
9ee6e8bb
PB
6279
6280 /* Pagetable walk. */
6281 /* Lookup l1 descriptor. */
0480f69a 6282 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
6283 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6284 code = 5;
6285 goto do_fault;
6286 }
ebca90e4 6287 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
9ee6e8bb 6288 type = (desc & 3);
de9b05b8
PM
6289 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
6290 /* Section translation fault, or attempt to use the encoding
6291 * which is Reserved on implementations without PXN.
6292 */
9ee6e8bb 6293 code = 5;
9ee6e8bb 6294 goto do_fault;
de9b05b8
PM
6295 }
6296 if ((type == 1) || !(desc & (1 << 18))) {
6297 /* Page or Section. */
dd4ebc2e 6298 domain = (desc >> 5) & 0x0f;
9ee6e8bb 6299 }
0480f69a
PM
6300 if (regime_el(env, mmu_idx) == 1) {
6301 dacr = env->cp15.dacr_ns;
6302 } else {
6303 dacr = env->cp15.dacr_s;
6304 }
6305 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 6306 if (domain_prot == 0 || domain_prot == 2) {
de9b05b8 6307 if (type != 1) {
9ee6e8bb 6308 code = 9; /* Section domain fault. */
de9b05b8 6309 } else {
9ee6e8bb 6310 code = 11; /* Page domain fault. */
de9b05b8 6311 }
9ee6e8bb
PB
6312 goto do_fault;
6313 }
de9b05b8 6314 if (type != 1) {
9ee6e8bb
PB
6315 if (desc & (1 << 18)) {
6316 /* Supersection. */
6317 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
6318 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
6319 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 6320 *page_size = 0x1000000;
b5ff1b31 6321 } else {
9ee6e8bb
PB
6322 /* Section. */
6323 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 6324 *page_size = 0x100000;
b5ff1b31 6325 }
9ee6e8bb
PB
6326 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
6327 xn = desc & (1 << 4);
de9b05b8 6328 pxn = desc & 1;
9ee6e8bb 6329 code = 13;
8bf5b6a9 6330 ns = extract32(desc, 19, 1);
9ee6e8bb 6331 } else {
de9b05b8
PM
6332 if (arm_feature(env, ARM_FEATURE_PXN)) {
6333 pxn = (desc >> 2) & 1;
6334 }
8bf5b6a9 6335 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
6336 /* Lookup l2 entry. */
6337 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
ebca90e4 6338 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
9ee6e8bb
PB
6339 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
6340 switch (desc & 3) {
6341 case 0: /* Page translation fault. */
6342 code = 7;
b5ff1b31 6343 goto do_fault;
9ee6e8bb
PB
6344 case 1: /* 64k page. */
6345 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6346 xn = desc & (1 << 15);
d4c430a8 6347 *page_size = 0x10000;
9ee6e8bb
PB
6348 break;
6349 case 2: case 3: /* 4k page. */
6350 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6351 xn = desc & 1;
d4c430a8 6352 *page_size = 0x1000;
9ee6e8bb
PB
6353 break;
6354 default:
6355 /* Never happens, but compiler isn't smart enough to tell. */
6356 abort();
b5ff1b31 6357 }
9ee6e8bb
PB
6358 code = 15;
6359 }
dd4ebc2e 6360 if (domain_prot == 3) {
c0034328
JR
6361 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
6362 } else {
0480f69a 6363 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
6364 xn = 1;
6365 }
c0034328
JR
6366 if (xn && access_type == 2)
6367 goto do_fault;
9ee6e8bb 6368
d76951b6
AJ
6369 if (arm_feature(env, ARM_FEATURE_V6K) &&
6370 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
6371 /* The simplified model uses AP[0] as an access control bit. */
6372 if ((ap & 1) == 0) {
6373 /* Access flag fault. */
6374 code = (code == 15) ? 6 : 3;
6375 goto do_fault;
6376 }
6377 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
6378 } else {
6379 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 6380 }
0fbf5238
AJ
6381 if (*prot && !xn) {
6382 *prot |= PAGE_EXEC;
6383 }
6384 if (!(*prot & (1 << access_type))) {
c0034328
JR
6385 /* Access permission fault. */
6386 goto do_fault;
6387 }
3ad493fc 6388 }
8bf5b6a9
PM
6389 if (ns) {
6390 /* The NS bit will (as required by the architecture) have no effect if
6391 * the CPU doesn't support TZ or this is a non-secure translation
6392 * regime, because the attribute will already be non-secure.
6393 */
6394 attrs->secure = false;
6395 }
9ee6e8bb 6396 *phys_ptr = phys_addr;
b7cc4e82 6397 return false;
b5ff1b31 6398do_fault:
b7cc4e82
PC
6399 *fsr = code | (domain << 4);
6400 return true;
b5ff1b31
FB
6401}
6402
3dde962f
PM
6403/* Fault type for long-descriptor MMU fault reporting; this corresponds
6404 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
6405 */
6406typedef enum {
6407 translation_fault = 1,
6408 access_fault = 2,
6409 permission_fault = 3,
6410} MMUFaultType;
6411
b7cc4e82
PC
6412static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
6413 int access_type, ARMMMUIdx mmu_idx,
6414 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
6415 target_ulong *page_size_ptr, uint32_t *fsr)
3dde962f 6416{
70d74660 6417 CPUState *cs = CPU(arm_env_get_cpu(env));
3dde962f
PM
6418 /* Read an LPAE long-descriptor translation table. */
6419 MMUFaultType fault_type = translation_fault;
6420 uint32_t level = 1;
0c5fbf3b 6421 uint32_t epd = 0;
2c8dd318
RH
6422 int32_t tsz;
6423 uint32_t tg;
3dde962f
PM
6424 uint64_t ttbr;
6425 int ttbr_select;
2c8dd318 6426 hwaddr descaddr, descmask;
3dde962f
PM
6427 uint32_t tableattrs;
6428 target_ulong page_size;
6429 uint32_t attrs;
2c8dd318
RH
6430 int32_t granule_sz = 9;
6431 int32_t va_size = 32;
6432 int32_t tbi = 0;
0480f69a 6433 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 6434 int ap, ns, xn, pxn;
88e8add8
GB
6435 uint32_t el = regime_el(env, mmu_idx);
6436 bool ttbr1_valid = true;
0480f69a
PM
6437
6438 /* TODO:
88e8add8
GB
6439 * This code does not handle the different format TCR for VTCR_EL2.
6440 * This code also does not support shareability levels.
6441 * Attribute and permission bit handling should also be checked when adding
6442 * support for those page table walks.
0480f69a 6443 */
88e8add8 6444 if (arm_el_is_aa64(env, el)) {
2c8dd318 6445 va_size = 64;
88e8add8 6446 if (el > 1) {
1edee470
EI
6447 if (mmu_idx != ARMMMUIdx_S2NS) {
6448 tbi = extract64(tcr->raw_tcr, 20, 1);
6449 }
88e8add8
GB
6450 } else {
6451 if (extract64(address, 55, 1)) {
6452 tbi = extract64(tcr->raw_tcr, 38, 1);
6453 } else {
6454 tbi = extract64(tcr->raw_tcr, 37, 1);
6455 }
6456 }
2c8dd318 6457 tbi *= 8;
88e8add8
GB
6458
6459 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
6460 * invalid.
6461 */
6462 if (el > 1) {
6463 ttbr1_valid = false;
6464 }
d0a2cbce
PM
6465 } else {
6466 /* There is no TTBR1 for EL2 */
6467 if (el == 2) {
6468 ttbr1_valid = false;
6469 }
2c8dd318 6470 }
3dde962f
PM
6471
6472 /* Determine whether this address is in the region controlled by
6473 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
6474 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
6475 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
6476 */
11f136ee 6477 uint32_t t0sz = extract32(tcr->raw_tcr, 0, 6);
0480f69a 6478 if (va_size == 64) {
2c8dd318
RH
6479 t0sz = MIN(t0sz, 39);
6480 t0sz = MAX(t0sz, 16);
6481 }
11f136ee 6482 uint32_t t1sz = extract32(tcr->raw_tcr, 16, 6);
0480f69a 6483 if (va_size == 64) {
2c8dd318
RH
6484 t1sz = MIN(t1sz, 39);
6485 t1sz = MAX(t1sz, 16);
6486 }
6487 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
3dde962f
PM
6488 /* there is a ttbr0 region and we are in it (high bits all zero) */
6489 ttbr_select = 0;
88e8add8
GB
6490 } else if (ttbr1_valid && t1sz &&
6491 !extract64(~address, va_size - t1sz, t1sz - tbi)) {
3dde962f
PM
6492 /* there is a ttbr1 region and we are in it (high bits all one) */
6493 ttbr_select = 1;
6494 } else if (!t0sz) {
6495 /* ttbr0 region is "everything not in the ttbr1 region" */
6496 ttbr_select = 0;
88e8add8 6497 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
6498 /* ttbr1 region is "everything not in the ttbr0 region" */
6499 ttbr_select = 1;
6500 } else {
6501 /* in the gap between the two regions, this is a Translation fault */
6502 fault_type = translation_fault;
6503 goto do_fault;
6504 }
6505
6506 /* Note that QEMU ignores shareability and cacheability attributes,
6507 * so we don't need to do anything with the SH, ORGN, IRGN fields
6508 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
6509 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
6510 * implement any ASID-like capability so we can ignore it (instead
6511 * we will always flush the TLB any time the ASID is changed).
6512 */
6513 if (ttbr_select == 0) {
aef878be 6514 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
6515 if (el < 2) {
6516 epd = extract32(tcr->raw_tcr, 7, 1);
6517 }
3dde962f 6518 tsz = t0sz;
2c8dd318 6519
11f136ee 6520 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318
RH
6521 if (tg == 1) { /* 64KB pages */
6522 granule_sz = 13;
6523 }
6524 if (tg == 2) { /* 16KB pages */
6525 granule_sz = 11;
6526 }
3dde962f 6527 } else {
88e8add8
GB
6528 /* We should only be here if TTBR1 is valid */
6529 assert(ttbr1_valid);
6530
aef878be 6531 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 6532 epd = extract32(tcr->raw_tcr, 23, 1);
3dde962f 6533 tsz = t1sz;
2c8dd318 6534
11f136ee 6535 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318
RH
6536 if (tg == 3) { /* 64KB pages */
6537 granule_sz = 13;
6538 }
6539 if (tg == 1) { /* 16KB pages */
6540 granule_sz = 11;
6541 }
3dde962f
PM
6542 }
6543
0480f69a
PM
6544 /* Here we should have set up all the parameters for the translation:
6545 * va_size, ttbr, epd, tsz, granule_sz, tbi
6546 */
6547
3dde962f 6548 if (epd) {
88e8add8
GB
6549 /* Translation table walk disabled => Translation fault on TLB miss
6550 * Note: This is always 0 on 64-bit EL2 and EL3.
6551 */
3dde962f
PM
6552 goto do_fault;
6553 }
6554
d6be29e3
PM
6555 /* The starting level depends on the virtual address size (which can be
6556 * up to 48 bits) and the translation granule size. It indicates the number
6557 * of strides (granule_sz bits at a time) needed to consume the bits
6558 * of the input address. In the pseudocode this is:
6559 * level = 4 - RoundUp((inputsize - grainsize) / stride)
6560 * where their 'inputsize' is our 'va_size - tsz', 'grainsize' is
6561 * our 'granule_sz + 3' and 'stride' is our 'granule_sz'.
6562 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
6563 * = 4 - (va_size - tsz - granule_sz - 3 + granule_sz - 1) / granule_sz
6564 * = 4 - (va_size - tsz - 4) / granule_sz;
3dde962f 6565 */
d6be29e3 6566 level = 4 - (va_size - tsz - 4) / granule_sz;
3dde962f
PM
6567
6568 /* Clear the vaddr bits which aren't part of the within-region address,
6569 * so that we don't have to special case things when calculating the
6570 * first descriptor address.
6571 */
2c8dd318
RH
6572 if (tsz) {
6573 address &= (1ULL << (va_size - tsz)) - 1;
6574 }
6575
6576 descmask = (1ULL << (granule_sz + 3)) - 1;
3dde962f
PM
6577
6578 /* Now we can extract the actual base address from the TTBR */
2c8dd318
RH
6579 descaddr = extract64(ttbr, 0, 48);
6580 descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
3dde962f 6581
ebca90e4
PM
6582 /* Secure accesses start with the page table in secure memory and
6583 * can be downgraded to non-secure at any step. Non-secure accesses
6584 * remain non-secure. We implement this by just ORing in the NSTable/NS
6585 * bits at each step.
6586 */
6587 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
6588 for (;;) {
6589 uint64_t descriptor;
ebca90e4 6590 bool nstable;
3dde962f 6591
2c8dd318
RH
6592 descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
6593 descaddr &= ~7ULL;
ebca90e4
PM
6594 nstable = extract32(tableattrs, 4, 1);
6595 descriptor = arm_ldq_ptw(cs, descaddr, !nstable);
3dde962f
PM
6596 if (!(descriptor & 1) ||
6597 (!(descriptor & 2) && (level == 3))) {
6598 /* Invalid, or the Reserved level 3 encoding */
6599 goto do_fault;
6600 }
6601 descaddr = descriptor & 0xfffffff000ULL;
6602
6603 if ((descriptor & 2) && (level < 3)) {
6604 /* Table entry. The top five bits are attributes which may
6605 * propagate down through lower levels of the table (and
6606 * which are all arranged so that 0 means "no effect", so
6607 * we can gather them up by ORing in the bits at each level).
6608 */
6609 tableattrs |= extract64(descriptor, 59, 5);
6610 level++;
6611 continue;
6612 }
6613 /* Block entry at level 1 or 2, or page entry at level 3.
6614 * These are basically the same thing, although the number
6615 * of bits we pull in from the vaddr varies.
6616 */
5661ae6b 6617 page_size = (1ULL << ((granule_sz * (4 - level)) + 3));
3dde962f
PM
6618 descaddr |= (address & (page_size - 1));
6619 /* Extract attributes from the descriptor and merge with table attrs */
d615efac
IC
6620 attrs = extract64(descriptor, 2, 10)
6621 | (extract64(descriptor, 52, 12) << 10);
3dde962f
PM
6622 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
6623 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
6624 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
6625 * means "force PL1 access only", which means forcing AP[1] to 0.
6626 */
6627 if (extract32(tableattrs, 2, 1)) {
6628 attrs &= ~(1 << 4);
6629 }
ebca90e4 6630 attrs |= nstable << 3; /* NS */
3dde962f
PM
6631 break;
6632 }
6633 /* Here descaddr is the final physical address, and attributes
6634 * are all in attrs.
6635 */
6636 fault_type = access_fault;
6637 if ((attrs & (1 << 8)) == 0) {
6638 /* Access flag */
6639 goto do_fault;
6640 }
d8e052b3
AJ
6641
6642 ap = extract32(attrs, 4, 2);
6643 ns = extract32(attrs, 3, 1);
6644 xn = extract32(attrs, 12, 1);
6645 pxn = extract32(attrs, 11, 1);
6646
6647 *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
6648
3dde962f 6649 fault_type = permission_fault;
d8e052b3 6650 if (!(*prot & (1 << access_type))) {
3dde962f
PM
6651 goto do_fault;
6652 }
3dde962f 6653
8bf5b6a9
PM
6654 if (ns) {
6655 /* The NS bit will (as required by the architecture) have no effect if
6656 * the CPU doesn't support TZ or this is a non-secure translation
6657 * regime, because the attribute will already be non-secure.
6658 */
6659 txattrs->secure = false;
6660 }
3dde962f
PM
6661 *phys_ptr = descaddr;
6662 *page_size_ptr = page_size;
b7cc4e82 6663 return false;
3dde962f
PM
6664
6665do_fault:
6666 /* Long-descriptor format IFSR/DFSR value */
b7cc4e82
PC
6667 *fsr = (1 << 9) | (fault_type << 2) | level;
6668 return true;
3dde962f
PM
6669}
6670
f6bda88f
PC
6671static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
6672 ARMMMUIdx mmu_idx,
6673 int32_t address, int *prot)
6674{
6675 *prot = PAGE_READ | PAGE_WRITE;
6676 switch (address) {
6677 case 0xF0000000 ... 0xFFFFFFFF:
6678 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
6679 *prot |= PAGE_EXEC;
6680 }
6681 break;
6682 case 0x00000000 ... 0x7FFFFFFF:
6683 *prot |= PAGE_EXEC;
6684 break;
6685 }
6686
6687}
6688
6689static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
6690 int access_type, ARMMMUIdx mmu_idx,
6691 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
6692{
6693 ARMCPU *cpu = arm_env_get_cpu(env);
6694 int n;
6695 bool is_user = regime_is_user(env, mmu_idx);
6696
6697 *phys_ptr = address;
6698 *prot = 0;
6699
6700 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
6701 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
6702 } else { /* MPU enabled */
6703 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
6704 /* region search */
6705 uint32_t base = env->pmsav7.drbar[n];
6706 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
6707 uint32_t rmask;
6708 bool srdis = false;
6709
6710 if (!(env->pmsav7.drsr[n] & 0x1)) {
6711 continue;
6712 }
6713
6714 if (!rsize) {
6715 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
6716 continue;
6717 }
6718 rsize++;
6719 rmask = (1ull << rsize) - 1;
6720
6721 if (base & rmask) {
6722 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
6723 "to DRSR region size, mask = %" PRIx32,
6724 base, rmask);
6725 continue;
6726 }
6727
6728 if (address < base || address > base + rmask) {
6729 continue;
6730 }
6731
6732 /* Region matched */
6733
6734 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
6735 int i, snd;
6736 uint32_t srdis_mask;
6737
6738 rsize -= 3; /* sub region size (power of 2) */
6739 snd = ((address - base) >> rsize) & 0x7;
6740 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
6741
6742 srdis_mask = srdis ? 0x3 : 0x0;
6743 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
6744 /* This will check in groups of 2, 4 and then 8, whether
6745 * the subregion bits are consistent. rsize is incremented
6746 * back up to give the region size, considering consistent
6747 * adjacent subregions as one region. Stop testing if rsize
6748 * is already big enough for an entire QEMU page.
6749 */
6750 int snd_rounded = snd & ~(i - 1);
6751 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
6752 snd_rounded + 8, i);
6753 if (srdis_mask ^ srdis_multi) {
6754 break;
6755 }
6756 srdis_mask = (srdis_mask << i) | srdis_mask;
6757 rsize++;
6758 }
6759 }
6760 if (rsize < TARGET_PAGE_BITS) {
6761 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
6762 "alignment of %" PRIu32 " bits. Minimum is %d\n",
6763 rsize, TARGET_PAGE_BITS);
6764 continue;
6765 }
6766 if (srdis) {
6767 continue;
6768 }
6769 break;
6770 }
6771
6772 if (n == -1) { /* no hits */
6773 if (cpu->pmsav7_dregion &&
6774 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
6775 /* background fault */
6776 *fsr = 0;
6777 return true;
6778 }
6779 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
6780 } else { /* a MPU hit! */
6781 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
6782
6783 if (is_user) { /* User mode AP bit decoding */
6784 switch (ap) {
6785 case 0:
6786 case 1:
6787 case 5:
6788 break; /* no access */
6789 case 3:
6790 *prot |= PAGE_WRITE;
6791 /* fall through */
6792 case 2:
6793 case 6:
6794 *prot |= PAGE_READ | PAGE_EXEC;
6795 break;
6796 default:
6797 qemu_log_mask(LOG_GUEST_ERROR,
6798 "Bad value for AP bits in DRACR %"
6799 PRIx32 "\n", ap);
6800 }
6801 } else { /* Priv. mode AP bits decoding */
6802 switch (ap) {
6803 case 0:
6804 break; /* no access */
6805 case 1:
6806 case 2:
6807 case 3:
6808 *prot |= PAGE_WRITE;
6809 /* fall through */
6810 case 5:
6811 case 6:
6812 *prot |= PAGE_READ | PAGE_EXEC;
6813 break;
6814 default:
6815 qemu_log_mask(LOG_GUEST_ERROR,
6816 "Bad value for AP bits in DRACR %"
6817 PRIx32 "\n", ap);
6818 }
6819 }
6820
6821 /* execute never */
6822 if (env->pmsav7.dracr[n] & (1 << 12)) {
6823 *prot &= ~PAGE_EXEC;
6824 }
6825 }
6826 }
6827
6828 *fsr = 0x00d; /* Permission fault */
6829 return !(*prot & (1 << access_type));
6830}
6831
13689d43
PC
6832static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
6833 int access_type, ARMMMUIdx mmu_idx,
6834 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9ee6e8bb
PB
6835{
6836 int n;
6837 uint32_t mask;
6838 uint32_t base;
0480f69a 6839 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb
PB
6840
6841 *phys_ptr = address;
6842 for (n = 7; n >= 0; n--) {
554b0b09 6843 base = env->cp15.c6_region[n];
87c3d486 6844 if ((base & 1) == 0) {
554b0b09 6845 continue;
87c3d486 6846 }
554b0b09
PM
6847 mask = 1 << ((base >> 1) & 0x1f);
6848 /* Keep this shift separate from the above to avoid an
6849 (undefined) << 32. */
6850 mask = (mask << 1) - 1;
87c3d486 6851 if (((base ^ address) & ~mask) == 0) {
554b0b09 6852 break;
87c3d486 6853 }
9ee6e8bb 6854 }
87c3d486 6855 if (n < 0) {
b7cc4e82
PC
6856 *fsr = 2;
6857 return true;
87c3d486 6858 }
9ee6e8bb
PB
6859
6860 if (access_type == 2) {
7e09797c 6861 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 6862 } else {
7e09797c 6863 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
6864 }
6865 mask = (mask >> (n * 4)) & 0xf;
6866 switch (mask) {
6867 case 0:
b7cc4e82
PC
6868 *fsr = 1;
6869 return true;
9ee6e8bb 6870 case 1:
87c3d486 6871 if (is_user) {
b7cc4e82
PC
6872 *fsr = 1;
6873 return true;
87c3d486 6874 }
554b0b09
PM
6875 *prot = PAGE_READ | PAGE_WRITE;
6876 break;
9ee6e8bb 6877 case 2:
554b0b09 6878 *prot = PAGE_READ;
87c3d486 6879 if (!is_user) {
554b0b09 6880 *prot |= PAGE_WRITE;
87c3d486 6881 }
554b0b09 6882 break;
9ee6e8bb 6883 case 3:
554b0b09
PM
6884 *prot = PAGE_READ | PAGE_WRITE;
6885 break;
9ee6e8bb 6886 case 5:
87c3d486 6887 if (is_user) {
b7cc4e82
PC
6888 *fsr = 1;
6889 return true;
87c3d486 6890 }
554b0b09
PM
6891 *prot = PAGE_READ;
6892 break;
9ee6e8bb 6893 case 6:
554b0b09
PM
6894 *prot = PAGE_READ;
6895 break;
9ee6e8bb 6896 default:
554b0b09 6897 /* Bad permission. */
b7cc4e82
PC
6898 *fsr = 1;
6899 return true;
9ee6e8bb 6900 }
3ad493fc 6901 *prot |= PAGE_EXEC;
b7cc4e82 6902 return false;
9ee6e8bb
PB
6903}
6904
702a9357
PM
6905/* get_phys_addr - get the physical address for this virtual address
6906 *
6907 * Find the physical address corresponding to the given virtual address,
6908 * by doing a translation table walk on MMU based systems or using the
6909 * MPU state on MPU based systems.
6910 *
b7cc4e82
PC
6911 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
6912 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
6913 * information on why the translation aborted, in the format of a
6914 * DFSR/IFSR fault register, with the following caveats:
6915 * * we honour the short vs long DFSR format differences.
6916 * * the WnR bit is never set (the caller must do this).
f6bda88f 6917 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
6918 * value.
6919 *
6920 * @env: CPUARMState
6921 * @address: virtual address to get physical address for
6922 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 6923 * @mmu_idx: MMU index indicating required translation regime
702a9357 6924 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 6925 * @attrs: set to the memory transaction attributes to use
702a9357
PM
6926 * @prot: set to the permissions for the page containing phys_ptr
6927 * @page_size: set to the size of the page containing phys_ptr
b7cc4e82 6928 * @fsr: set to the DFSR/IFSR value on failure
702a9357 6929 */
b7cc4e82
PC
6930static inline bool get_phys_addr(CPUARMState *env, target_ulong address,
6931 int access_type, ARMMMUIdx mmu_idx,
6932 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
6933 target_ulong *page_size, uint32_t *fsr)
9ee6e8bb 6934{
0480f69a
PM
6935 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6936 /* TODO: when we support EL2 we should here call ourselves recursively
ebca90e4
PM
6937 * to do the stage 1 and then stage 2 translations. The arm_ld*_ptw
6938 * functions will also need changing to perform ARMMMUIdx_S2NS loads
6939 * rather than direct physical memory loads when appropriate.
0480f69a
PM
6940 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
6941 */
6942 assert(!arm_feature(env, ARM_FEATURE_EL2));
6943 mmu_idx += ARMMMUIdx_S1NSE0;
6944 }
d3649702 6945
8bf5b6a9
PM
6946 /* The page table entries may downgrade secure to non-secure, but
6947 * cannot upgrade an non-secure translation regime's attributes
6948 * to secure.
6949 */
6950 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 6951 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 6952
0480f69a
PM
6953 /* Fast Context Switch Extension. This doesn't exist at all in v8.
6954 * In v7 and earlier it affects all stage 1 translations.
6955 */
6956 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
6957 && !arm_feature(env, ARM_FEATURE_V8)) {
6958 if (regime_el(env, mmu_idx) == 3) {
6959 address += env->cp15.fcseidr_s;
6960 } else {
6961 address += env->cp15.fcseidr_ns;
6962 }
54bf36ed 6963 }
9ee6e8bb 6964
f6bda88f
PC
6965 /* pmsav7 has special handling for when MPU is disabled so call it before
6966 * the common MMU/MPU disabled check below.
6967 */
6968 if (arm_feature(env, ARM_FEATURE_MPU) &&
6969 arm_feature(env, ARM_FEATURE_V7)) {
6970 *page_size = TARGET_PAGE_SIZE;
6971 return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
6972 phys_ptr, prot, fsr);
6973 }
6974
0480f69a 6975 if (regime_translation_disabled(env, mmu_idx)) {
9ee6e8bb
PB
6976 /* MMU/MPU disabled. */
6977 *phys_ptr = address;
3ad493fc 6978 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 6979 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 6980 return 0;
0480f69a
PM
6981 }
6982
6983 if (arm_feature(env, ARM_FEATURE_MPU)) {
f6bda88f 6984 /* Pre-v7 MPU */
d4c430a8 6985 *page_size = TARGET_PAGE_SIZE;
13689d43
PC
6986 return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
6987 phys_ptr, prot, fsr);
0480f69a
PM
6988 }
6989
6990 if (regime_using_lpae_format(env, mmu_idx)) {
6991 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
b7cc4e82 6992 attrs, prot, page_size, fsr);
0480f69a
PM
6993 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
6994 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
b7cc4e82 6995 attrs, prot, page_size, fsr);
9ee6e8bb 6996 } else {
0480f69a 6997 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
b7cc4e82 6998 prot, page_size, fsr);
9ee6e8bb
PB
6999 }
7000}
7001
8c6084bf 7002/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
7003 * to the TLB. Return false on success, or true on failure. Populate
7004 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 7005 */
b7cc4e82
PC
7006bool arm_tlb_fill(CPUState *cs, vaddr address,
7007 int access_type, int mmu_idx, uint32_t *fsr)
b5ff1b31 7008{
7510454e
AF
7009 ARMCPU *cpu = ARM_CPU(cs);
7010 CPUARMState *env = &cpu->env;
a8170e5e 7011 hwaddr phys_addr;
d4c430a8 7012 target_ulong page_size;
b5ff1b31 7013 int prot;
d3649702 7014 int ret;
8bf5b6a9 7015 MemTxAttrs attrs = {};
b5ff1b31 7016
8bf5b6a9 7017 ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
b7cc4e82
PC
7018 &attrs, &prot, &page_size, fsr);
7019 if (!ret) {
b5ff1b31 7020 /* Map a single [sub]page. */
dcd82c11
AB
7021 phys_addr &= TARGET_PAGE_MASK;
7022 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
7023 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
7024 prot, mmu_idx, page_size);
d4c430a8 7025 return 0;
b5ff1b31
FB
7026 }
7027
8c6084bf 7028 return ret;
b5ff1b31
FB
7029}
7030
00b941e5 7031hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
b5ff1b31 7032{
00b941e5 7033 ARMCPU *cpu = ARM_CPU(cs);
d3649702 7034 CPUARMState *env = &cpu->env;
a8170e5e 7035 hwaddr phys_addr;
d4c430a8 7036 target_ulong page_size;
b5ff1b31 7037 int prot;
b7cc4e82
PC
7038 bool ret;
7039 uint32_t fsr;
8bf5b6a9 7040 MemTxAttrs attrs = {};
b5ff1b31 7041
97ed5ccd 7042 ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
b7cc4e82 7043 &attrs, &prot, &page_size, &fsr);
b5ff1b31 7044
b7cc4e82 7045 if (ret) {
b5ff1b31 7046 return -1;
00b941e5 7047 }
b5ff1b31
FB
7048
7049 return phys_addr;
7050}
7051
0ecb72a5 7052void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
9ee6e8bb 7053{
39ea3d4e
PM
7054 if ((env->uncached_cpsr & CPSR_M) == mode) {
7055 env->regs[13] = val;
7056 } else {
f5206413 7057 env->banked_r13[bank_number(mode)] = val;
39ea3d4e 7058 }
9ee6e8bb
PB
7059}
7060
0ecb72a5 7061uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
9ee6e8bb 7062{
39ea3d4e
PM
7063 if ((env->uncached_cpsr & CPSR_M) == mode) {
7064 return env->regs[13];
7065 } else {
f5206413 7066 return env->banked_r13[bank_number(mode)];
39ea3d4e 7067 }
9ee6e8bb
PB
7068}
7069
0ecb72a5 7070uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 7071{
a47dddd7
AF
7072 ARMCPU *cpu = arm_env_get_cpu(env);
7073
9ee6e8bb
PB
7074 switch (reg) {
7075 case 0: /* APSR */
7076 return xpsr_read(env) & 0xf8000000;
7077 case 1: /* IAPSR */
7078 return xpsr_read(env) & 0xf80001ff;
7079 case 2: /* EAPSR */
7080 return xpsr_read(env) & 0xff00fc00;
7081 case 3: /* xPSR */
7082 return xpsr_read(env) & 0xff00fdff;
7083 case 5: /* IPSR */
7084 return xpsr_read(env) & 0x000001ff;
7085 case 6: /* EPSR */
7086 return xpsr_read(env) & 0x0700fc00;
7087 case 7: /* IEPSR */
7088 return xpsr_read(env) & 0x0700edff;
7089 case 8: /* MSP */
7090 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
7091 case 9: /* PSP */
7092 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
7093 case 16: /* PRIMASK */
4cc35614 7094 return (env->daif & PSTATE_I) != 0;
82845826
SH
7095 case 17: /* BASEPRI */
7096 case 18: /* BASEPRI_MAX */
9ee6e8bb 7097 return env->v7m.basepri;
82845826 7098 case 19: /* FAULTMASK */
4cc35614 7099 return (env->daif & PSTATE_F) != 0;
9ee6e8bb
PB
7100 case 20: /* CONTROL */
7101 return env->v7m.control;
7102 default:
7103 /* ??? For debugging only. */
a47dddd7 7104 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
9ee6e8bb
PB
7105 return 0;
7106 }
7107}
7108
0ecb72a5 7109void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 7110{
a47dddd7
AF
7111 ARMCPU *cpu = arm_env_get_cpu(env);
7112
9ee6e8bb
PB
7113 switch (reg) {
7114 case 0: /* APSR */
7115 xpsr_write(env, val, 0xf8000000);
7116 break;
7117 case 1: /* IAPSR */
7118 xpsr_write(env, val, 0xf8000000);
7119 break;
7120 case 2: /* EAPSR */
7121 xpsr_write(env, val, 0xfe00fc00);
7122 break;
7123 case 3: /* xPSR */
7124 xpsr_write(env, val, 0xfe00fc00);
7125 break;
7126 case 5: /* IPSR */
7127 /* IPSR bits are readonly. */
7128 break;
7129 case 6: /* EPSR */
7130 xpsr_write(env, val, 0x0600fc00);
7131 break;
7132 case 7: /* IEPSR */
7133 xpsr_write(env, val, 0x0600fc00);
7134 break;
7135 case 8: /* MSP */
7136 if (env->v7m.current_sp)
7137 env->v7m.other_sp = val;
7138 else
7139 env->regs[13] = val;
7140 break;
7141 case 9: /* PSP */
7142 if (env->v7m.current_sp)
7143 env->regs[13] = val;
7144 else
7145 env->v7m.other_sp = val;
7146 break;
7147 case 16: /* PRIMASK */
4cc35614
PM
7148 if (val & 1) {
7149 env->daif |= PSTATE_I;
7150 } else {
7151 env->daif &= ~PSTATE_I;
7152 }
9ee6e8bb 7153 break;
82845826 7154 case 17: /* BASEPRI */
9ee6e8bb
PB
7155 env->v7m.basepri = val & 0xff;
7156 break;
82845826 7157 case 18: /* BASEPRI_MAX */
9ee6e8bb
PB
7158 val &= 0xff;
7159 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
7160 env->v7m.basepri = val;
7161 break;
82845826 7162 case 19: /* FAULTMASK */
4cc35614
PM
7163 if (val & 1) {
7164 env->daif |= PSTATE_F;
7165 } else {
7166 env->daif &= ~PSTATE_F;
7167 }
82845826 7168 break;
9ee6e8bb
PB
7169 case 20: /* CONTROL */
7170 env->v7m.control = val & 3;
7171 switch_v7m_sp(env, (val & 2) != 0);
7172 break;
7173 default:
7174 /* ??? For debugging only. */
a47dddd7 7175 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
9ee6e8bb
PB
7176 return;
7177 }
7178}
7179
b5ff1b31 7180#endif
6ddbc6e4 7181
aca3f40b
PM
7182void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
7183{
7184 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
7185 * Note that we do not implement the (architecturally mandated)
7186 * alignment fault for attempts to use this on Device memory
7187 * (which matches the usual QEMU behaviour of not implementing either
7188 * alignment faults or any memory attribute handling).
7189 */
7190
7191 ARMCPU *cpu = arm_env_get_cpu(env);
7192 uint64_t blocklen = 4 << cpu->dcz_blocksize;
7193 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
7194
7195#ifndef CONFIG_USER_ONLY
7196 {
7197 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
7198 * the block size so we might have to do more than one TLB lookup.
7199 * We know that in fact for any v8 CPU the page size is at least 4K
7200 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
7201 * 1K as an artefact of legacy v5 subpage support being present in the
7202 * same QEMU executable.
7203 */
7204 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
7205 void *hostaddr[maxidx];
7206 int try, i;
97ed5ccd 7207 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 7208 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
7209
7210 for (try = 0; try < 2; try++) {
7211
7212 for (i = 0; i < maxidx; i++) {
7213 hostaddr[i] = tlb_vaddr_to_host(env,
7214 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 7215 1, mmu_idx);
aca3f40b
PM
7216 if (!hostaddr[i]) {
7217 break;
7218 }
7219 }
7220 if (i == maxidx) {
7221 /* If it's all in the TLB it's fair game for just writing to;
7222 * we know we don't need to update dirty status, etc.
7223 */
7224 for (i = 0; i < maxidx - 1; i++) {
7225 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
7226 }
7227 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
7228 return;
7229 }
7230 /* OK, try a store and see if we can populate the tlb. This
7231 * might cause an exception if the memory isn't writable,
7232 * in which case we will longjmp out of here. We must for
7233 * this purpose use the actual register value passed to us
7234 * so that we get the fault address right.
7235 */
3972ef6f 7236 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
aca3f40b
PM
7237 /* Now we can populate the other TLB entries, if any */
7238 for (i = 0; i < maxidx; i++) {
7239 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
7240 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
3972ef6f 7241 helper_ret_stb_mmu(env, va, 0, oi, GETRA());
aca3f40b
PM
7242 }
7243 }
7244 }
7245
7246 /* Slow path (probably attempt to do this to an I/O device or
7247 * similar, or clearing of a block of code we have translations
7248 * cached for). Just do a series of byte writes as the architecture
7249 * demands. It's not worth trying to use a cpu_physical_memory_map(),
7250 * memset(), unmap() sequence here because:
7251 * + we'd need to account for the blocksize being larger than a page
7252 * + the direct-RAM access case is almost always going to be dealt
7253 * with in the fastpath code above, so there's no speed benefit
7254 * + we would have to deal with the map returning NULL because the
7255 * bounce buffer was in use
7256 */
7257 for (i = 0; i < blocklen; i++) {
3972ef6f 7258 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
aca3f40b
PM
7259 }
7260 }
7261#else
7262 memset(g2h(vaddr), 0, blocklen);
7263#endif
7264}
7265
6ddbc6e4
PB
7266/* Note that signed overflow is undefined in C. The following routines are
7267 careful to use unsigned types where modulo arithmetic is required.
7268 Failure to do so _will_ break on newer gcc. */
7269
7270/* Signed saturating arithmetic. */
7271
1654b2d6 7272/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
7273static inline uint16_t add16_sat(uint16_t a, uint16_t b)
7274{
7275 uint16_t res;
7276
7277 res = a + b;
7278 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
7279 if (a & 0x8000)
7280 res = 0x8000;
7281 else
7282 res = 0x7fff;
7283 }
7284 return res;
7285}
7286
1654b2d6 7287/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
7288static inline uint8_t add8_sat(uint8_t a, uint8_t b)
7289{
7290 uint8_t res;
7291
7292 res = a + b;
7293 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
7294 if (a & 0x80)
7295 res = 0x80;
7296 else
7297 res = 0x7f;
7298 }
7299 return res;
7300}
7301
1654b2d6 7302/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
7303static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
7304{
7305 uint16_t res;
7306
7307 res = a - b;
7308 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
7309 if (a & 0x8000)
7310 res = 0x8000;
7311 else
7312 res = 0x7fff;
7313 }
7314 return res;
7315}
7316
1654b2d6 7317/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
7318static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
7319{
7320 uint8_t res;
7321
7322 res = a - b;
7323 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
7324 if (a & 0x80)
7325 res = 0x80;
7326 else
7327 res = 0x7f;
7328 }
7329 return res;
7330}
7331
7332#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
7333#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
7334#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
7335#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
7336#define PFX q
7337
7338#include "op_addsub.h"
7339
7340/* Unsigned saturating arithmetic. */
460a09c1 7341static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
7342{
7343 uint16_t res;
7344 res = a + b;
7345 if (res < a)
7346 res = 0xffff;
7347 return res;
7348}
7349
460a09c1 7350static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 7351{
4c4fd3f8 7352 if (a > b)
6ddbc6e4
PB
7353 return a - b;
7354 else
7355 return 0;
7356}
7357
7358static inline uint8_t add8_usat(uint8_t a, uint8_t b)
7359{
7360 uint8_t res;
7361 res = a + b;
7362 if (res < a)
7363 res = 0xff;
7364 return res;
7365}
7366
7367static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
7368{
4c4fd3f8 7369 if (a > b)
6ddbc6e4
PB
7370 return a - b;
7371 else
7372 return 0;
7373}
7374
7375#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
7376#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
7377#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
7378#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
7379#define PFX uq
7380
7381#include "op_addsub.h"
7382
7383/* Signed modulo arithmetic. */
7384#define SARITH16(a, b, n, op) do { \
7385 int32_t sum; \
db6e2e65 7386 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
7387 RESULT(sum, n, 16); \
7388 if (sum >= 0) \
7389 ge |= 3 << (n * 2); \
7390 } while(0)
7391
7392#define SARITH8(a, b, n, op) do { \
7393 int32_t sum; \
db6e2e65 7394 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
7395 RESULT(sum, n, 8); \
7396 if (sum >= 0) \
7397 ge |= 1 << n; \
7398 } while(0)
7399
7400
7401#define ADD16(a, b, n) SARITH16(a, b, n, +)
7402#define SUB16(a, b, n) SARITH16(a, b, n, -)
7403#define ADD8(a, b, n) SARITH8(a, b, n, +)
7404#define SUB8(a, b, n) SARITH8(a, b, n, -)
7405#define PFX s
7406#define ARITH_GE
7407
7408#include "op_addsub.h"
7409
7410/* Unsigned modulo arithmetic. */
7411#define ADD16(a, b, n) do { \
7412 uint32_t sum; \
7413 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
7414 RESULT(sum, n, 16); \
a87aa10b 7415 if ((sum >> 16) == 1) \
6ddbc6e4
PB
7416 ge |= 3 << (n * 2); \
7417 } while(0)
7418
7419#define ADD8(a, b, n) do { \
7420 uint32_t sum; \
7421 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
7422 RESULT(sum, n, 8); \
a87aa10b
AZ
7423 if ((sum >> 8) == 1) \
7424 ge |= 1 << n; \
6ddbc6e4
PB
7425 } while(0)
7426
7427#define SUB16(a, b, n) do { \
7428 uint32_t sum; \
7429 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
7430 RESULT(sum, n, 16); \
7431 if ((sum >> 16) == 0) \
7432 ge |= 3 << (n * 2); \
7433 } while(0)
7434
7435#define SUB8(a, b, n) do { \
7436 uint32_t sum; \
7437 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
7438 RESULT(sum, n, 8); \
7439 if ((sum >> 8) == 0) \
a87aa10b 7440 ge |= 1 << n; \
6ddbc6e4
PB
7441 } while(0)
7442
7443#define PFX u
7444#define ARITH_GE
7445
7446#include "op_addsub.h"
7447
7448/* Halved signed arithmetic. */
7449#define ADD16(a, b, n) \
7450 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
7451#define SUB16(a, b, n) \
7452 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
7453#define ADD8(a, b, n) \
7454 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
7455#define SUB8(a, b, n) \
7456 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
7457#define PFX sh
7458
7459#include "op_addsub.h"
7460
7461/* Halved unsigned arithmetic. */
7462#define ADD16(a, b, n) \
7463 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
7464#define SUB16(a, b, n) \
7465 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
7466#define ADD8(a, b, n) \
7467 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
7468#define SUB8(a, b, n) \
7469 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
7470#define PFX uh
7471
7472#include "op_addsub.h"
7473
7474static inline uint8_t do_usad(uint8_t a, uint8_t b)
7475{
7476 if (a > b)
7477 return a - b;
7478 else
7479 return b - a;
7480}
7481
7482/* Unsigned sum of absolute byte differences. */
7483uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
7484{
7485 uint32_t sum;
7486 sum = do_usad(a, b);
7487 sum += do_usad(a >> 8, b >> 8);
7488 sum += do_usad(a >> 16, b >>16);
7489 sum += do_usad(a >> 24, b >> 24);
7490 return sum;
7491}
7492
7493/* For ARMv6 SEL instruction. */
7494uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
7495{
7496 uint32_t mask;
7497
7498 mask = 0;
7499 if (flags & 1)
7500 mask |= 0xff;
7501 if (flags & 2)
7502 mask |= 0xff00;
7503 if (flags & 4)
7504 mask |= 0xff0000;
7505 if (flags & 8)
7506 mask |= 0xff000000;
7507 return (a & mask) | (b & ~mask);
7508}
7509
b90372ad
PM
7510/* VFP support. We follow the convention used for VFP instructions:
7511 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
7512 "d" suffix. */
7513
7514/* Convert host exception flags to vfp form. */
7515static inline int vfp_exceptbits_from_host(int host_bits)
7516{
7517 int target_bits = 0;
7518
7519 if (host_bits & float_flag_invalid)
7520 target_bits |= 1;
7521 if (host_bits & float_flag_divbyzero)
7522 target_bits |= 2;
7523 if (host_bits & float_flag_overflow)
7524 target_bits |= 4;
36802b6b 7525 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
7526 target_bits |= 8;
7527 if (host_bits & float_flag_inexact)
7528 target_bits |= 0x10;
cecd8504
PM
7529 if (host_bits & float_flag_input_denormal)
7530 target_bits |= 0x80;
4373f3ce
PB
7531 return target_bits;
7532}
7533
0ecb72a5 7534uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
7535{
7536 int i;
7537 uint32_t fpscr;
7538
7539 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
7540 | (env->vfp.vec_len << 16)
7541 | (env->vfp.vec_stride << 20);
7542 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 7543 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
7544 fpscr |= vfp_exceptbits_from_host(i);
7545 return fpscr;
7546}
7547
0ecb72a5 7548uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
7549{
7550 return HELPER(vfp_get_fpscr)(env);
7551}
7552
4373f3ce
PB
7553/* Convert vfp exception flags to target form. */
7554static inline int vfp_exceptbits_to_host(int target_bits)
7555{
7556 int host_bits = 0;
7557
7558 if (target_bits & 1)
7559 host_bits |= float_flag_invalid;
7560 if (target_bits & 2)
7561 host_bits |= float_flag_divbyzero;
7562 if (target_bits & 4)
7563 host_bits |= float_flag_overflow;
7564 if (target_bits & 8)
7565 host_bits |= float_flag_underflow;
7566 if (target_bits & 0x10)
7567 host_bits |= float_flag_inexact;
cecd8504
PM
7568 if (target_bits & 0x80)
7569 host_bits |= float_flag_input_denormal;
4373f3ce
PB
7570 return host_bits;
7571}
7572
0ecb72a5 7573void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
7574{
7575 int i;
7576 uint32_t changed;
7577
7578 changed = env->vfp.xregs[ARM_VFP_FPSCR];
7579 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
7580 env->vfp.vec_len = (val >> 16) & 7;
7581 env->vfp.vec_stride = (val >> 20) & 3;
7582
7583 changed ^= val;
7584 if (changed & (3 << 22)) {
7585 i = (val >> 22) & 3;
7586 switch (i) {
4d3da0f3 7587 case FPROUNDING_TIEEVEN:
4373f3ce
PB
7588 i = float_round_nearest_even;
7589 break;
4d3da0f3 7590 case FPROUNDING_POSINF:
4373f3ce
PB
7591 i = float_round_up;
7592 break;
4d3da0f3 7593 case FPROUNDING_NEGINF:
4373f3ce
PB
7594 i = float_round_down;
7595 break;
4d3da0f3 7596 case FPROUNDING_ZERO:
4373f3ce
PB
7597 i = float_round_to_zero;
7598 break;
7599 }
7600 set_float_rounding_mode(i, &env->vfp.fp_status);
7601 }
cecd8504 7602 if (changed & (1 << 24)) {
fe76d976 7603 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
7604 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
7605 }
5c7908ed
PB
7606 if (changed & (1 << 25))
7607 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 7608
b12c390b 7609 i = vfp_exceptbits_to_host(val);
4373f3ce 7610 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 7611 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
7612}
7613
0ecb72a5 7614void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
7615{
7616 HELPER(vfp_set_fpscr)(env, val);
7617}
7618
4373f3ce
PB
7619#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
7620
7621#define VFP_BINOP(name) \
ae1857ec 7622float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 7623{ \
ae1857ec
PM
7624 float_status *fpst = fpstp; \
7625 return float32_ ## name(a, b, fpst); \
4373f3ce 7626} \
ae1857ec 7627float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 7628{ \
ae1857ec
PM
7629 float_status *fpst = fpstp; \
7630 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
7631}
7632VFP_BINOP(add)
7633VFP_BINOP(sub)
7634VFP_BINOP(mul)
7635VFP_BINOP(div)
f71a2ae5
PM
7636VFP_BINOP(min)
7637VFP_BINOP(max)
7638VFP_BINOP(minnum)
7639VFP_BINOP(maxnum)
4373f3ce
PB
7640#undef VFP_BINOP
7641
7642float32 VFP_HELPER(neg, s)(float32 a)
7643{
7644 return float32_chs(a);
7645}
7646
7647float64 VFP_HELPER(neg, d)(float64 a)
7648{
66230e0d 7649 return float64_chs(a);
4373f3ce
PB
7650}
7651
7652float32 VFP_HELPER(abs, s)(float32 a)
7653{
7654 return float32_abs(a);
7655}
7656
7657float64 VFP_HELPER(abs, d)(float64 a)
7658{
66230e0d 7659 return float64_abs(a);
4373f3ce
PB
7660}
7661
0ecb72a5 7662float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
7663{
7664 return float32_sqrt(a, &env->vfp.fp_status);
7665}
7666
0ecb72a5 7667float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
7668{
7669 return float64_sqrt(a, &env->vfp.fp_status);
7670}
7671
7672/* XXX: check quiet/signaling case */
7673#define DO_VFP_cmp(p, type) \
0ecb72a5 7674void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
7675{ \
7676 uint32_t flags; \
7677 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
7678 case 0: flags = 0x6; break; \
7679 case -1: flags = 0x8; break; \
7680 case 1: flags = 0x2; break; \
7681 default: case 2: flags = 0x3; break; \
7682 } \
7683 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
7684 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
7685} \
0ecb72a5 7686void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
7687{ \
7688 uint32_t flags; \
7689 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
7690 case 0: flags = 0x6; break; \
7691 case -1: flags = 0x8; break; \
7692 case 1: flags = 0x2; break; \
7693 default: case 2: flags = 0x3; break; \
7694 } \
7695 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
7696 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
7697}
7698DO_VFP_cmp(s, float32)
7699DO_VFP_cmp(d, float64)
7700#undef DO_VFP_cmp
7701
5500b06c 7702/* Integer to float and float to integer conversions */
4373f3ce 7703
5500b06c
PM
7704#define CONV_ITOF(name, fsz, sign) \
7705 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
7706{ \
7707 float_status *fpst = fpstp; \
85836979 7708 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
7709}
7710
5500b06c
PM
7711#define CONV_FTOI(name, fsz, sign, round) \
7712uint32_t HELPER(name)(float##fsz x, void *fpstp) \
7713{ \
7714 float_status *fpst = fpstp; \
7715 if (float##fsz##_is_any_nan(x)) { \
7716 float_raise(float_flag_invalid, fpst); \
7717 return 0; \
7718 } \
7719 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
7720}
7721
5500b06c
PM
7722#define FLOAT_CONVS(name, p, fsz, sign) \
7723CONV_ITOF(vfp_##name##to##p, fsz, sign) \
7724CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
7725CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 7726
5500b06c
PM
7727FLOAT_CONVS(si, s, 32, )
7728FLOAT_CONVS(si, d, 64, )
7729FLOAT_CONVS(ui, s, 32, u)
7730FLOAT_CONVS(ui, d, 64, u)
4373f3ce 7731
5500b06c
PM
7732#undef CONV_ITOF
7733#undef CONV_FTOI
7734#undef FLOAT_CONVS
4373f3ce
PB
7735
7736/* floating point conversion */
0ecb72a5 7737float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 7738{
2d627737
PM
7739 float64 r = float32_to_float64(x, &env->vfp.fp_status);
7740 /* ARM requires that S<->D conversion of any kind of NaN generates
7741 * a quiet NaN by forcing the most significant frac bit to 1.
7742 */
7743 return float64_maybe_silence_nan(r);
4373f3ce
PB
7744}
7745
0ecb72a5 7746float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 7747{
2d627737
PM
7748 float32 r = float64_to_float32(x, &env->vfp.fp_status);
7749 /* ARM requires that S<->D conversion of any kind of NaN generates
7750 * a quiet NaN by forcing the most significant frac bit to 1.
7751 */
7752 return float32_maybe_silence_nan(r);
4373f3ce
PB
7753}
7754
7755/* VFP3 fixed point conversion. */
16d5b3ca 7756#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
7757float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
7758 void *fpstp) \
4373f3ce 7759{ \
5500b06c 7760 float_status *fpst = fpstp; \
622465e1 7761 float##fsz tmp; \
8ed697e8 7762 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 7763 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
7764}
7765
abe66f70
PM
7766/* Notice that we want only input-denormal exception flags from the
7767 * scalbn operation: the other possible flags (overflow+inexact if
7768 * we overflow to infinity, output-denormal) aren't correct for the
7769 * complete scale-and-convert operation.
7770 */
16d5b3ca
WN
7771#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
7772uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
7773 uint32_t shift, \
7774 void *fpstp) \
4373f3ce 7775{ \
5500b06c 7776 float_status *fpst = fpstp; \
abe66f70 7777 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
7778 float##fsz tmp; \
7779 if (float##fsz##_is_any_nan(x)) { \
5500b06c 7780 float_raise(float_flag_invalid, fpst); \
622465e1 7781 return 0; \
09d9487f 7782 } \
5500b06c 7783 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
7784 old_exc_flags |= get_float_exception_flags(fpst) \
7785 & float_flag_input_denormal; \
7786 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 7787 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
7788}
7789
16d5b3ca
WN
7790#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
7791VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
7792VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
7793VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
7794
7795#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
7796VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
7797VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 7798
8ed697e8
WN
7799VFP_CONV_FIX(sh, d, 64, 64, int16)
7800VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 7801VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
7802VFP_CONV_FIX(uh, d, 64, 64, uint16)
7803VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 7804VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
7805VFP_CONV_FIX(sh, s, 32, 32, int16)
7806VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 7807VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
7808VFP_CONV_FIX(uh, s, 32, 32, uint16)
7809VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 7810VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 7811#undef VFP_CONV_FIX
16d5b3ca
WN
7812#undef VFP_CONV_FIX_FLOAT
7813#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 7814
52a1f6a3
AG
7815/* Set the current fp rounding mode and return the old one.
7816 * The argument is a softfloat float_round_ value.
7817 */
7818uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
7819{
7820 float_status *fp_status = &env->vfp.fp_status;
7821
7822 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
7823 set_float_rounding_mode(rmode, fp_status);
7824
7825 return prev_rmode;
7826}
7827
43630e58
WN
7828/* Set the current fp rounding mode in the standard fp status and return
7829 * the old one. This is for NEON instructions that need to change the
7830 * rounding mode but wish to use the standard FPSCR values for everything
7831 * else. Always set the rounding mode back to the correct value after
7832 * modifying it.
7833 * The argument is a softfloat float_round_ value.
7834 */
7835uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
7836{
7837 float_status *fp_status = &env->vfp.standard_fp_status;
7838
7839 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
7840 set_float_rounding_mode(rmode, fp_status);
7841
7842 return prev_rmode;
7843}
7844
60011498 7845/* Half precision conversions. */
0ecb72a5 7846static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 7847{
60011498 7848 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
7849 float32 r = float16_to_float32(make_float16(a), ieee, s);
7850 if (ieee) {
7851 return float32_maybe_silence_nan(r);
7852 }
7853 return r;
60011498
PB
7854}
7855
0ecb72a5 7856static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 7857{
60011498 7858 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
7859 float16 r = float32_to_float16(a, ieee, s);
7860 if (ieee) {
7861 r = float16_maybe_silence_nan(r);
7862 }
7863 return float16_val(r);
60011498
PB
7864}
7865
0ecb72a5 7866float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
7867{
7868 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
7869}
7870
0ecb72a5 7871uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
7872{
7873 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
7874}
7875
0ecb72a5 7876float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
7877{
7878 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
7879}
7880
0ecb72a5 7881uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
7882{
7883 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
7884}
7885
8900aad2
PM
7886float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
7887{
7888 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7889 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
7890 if (ieee) {
7891 return float64_maybe_silence_nan(r);
7892 }
7893 return r;
7894}
7895
7896uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
7897{
7898 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
7899 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
7900 if (ieee) {
7901 r = float16_maybe_silence_nan(r);
7902 }
7903 return float16_val(r);
7904}
7905
dda3ec49 7906#define float32_two make_float32(0x40000000)
6aae3df1
PM
7907#define float32_three make_float32(0x40400000)
7908#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 7909
0ecb72a5 7910float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 7911{
dda3ec49
PM
7912 float_status *s = &env->vfp.standard_fp_status;
7913 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
7914 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
7915 if (!(float32_is_zero(a) || float32_is_zero(b))) {
7916 float_raise(float_flag_input_denormal, s);
7917 }
dda3ec49
PM
7918 return float32_two;
7919 }
7920 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
7921}
7922
0ecb72a5 7923float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 7924{
71826966 7925 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
7926 float32 product;
7927 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
7928 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
7929 if (!(float32_is_zero(a) || float32_is_zero(b))) {
7930 float_raise(float_flag_input_denormal, s);
7931 }
6aae3df1 7932 return float32_one_point_five;
9ea62f57 7933 }
6aae3df1
PM
7934 product = float32_mul(a, b, s);
7935 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
7936}
7937
8f8e3aa4
PB
7938/* NEON helpers. */
7939
56bf4fe2
CL
7940/* Constants 256 and 512 are used in some helpers; we avoid relying on
7941 * int->float conversions at run-time. */
7942#define float64_256 make_float64(0x4070000000000000LL)
7943#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
7944#define float32_maxnorm make_float32(0x7f7fffff)
7945#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 7946
b6d4443a
AB
7947/* Reciprocal functions
7948 *
7949 * The algorithm that must be used to calculate the estimate
7950 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 7951 */
b6d4443a
AB
7952
7953static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 7954{
1146a817
PM
7955 /* These calculations mustn't set any fp exception flags,
7956 * so we use a local copy of the fp_status.
7957 */
b6d4443a 7958 float_status dummy_status = *real_fp_status;
1146a817 7959 float_status *s = &dummy_status;
fe0e4872
CL
7960 /* q = (int)(a * 512.0) */
7961 float64 q = float64_mul(float64_512, a, s);
7962 int64_t q_int = float64_to_int64_round_to_zero(q, s);
7963
7964 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
7965 q = int64_to_float64(q_int, s);
7966 q = float64_add(q, float64_half, s);
7967 q = float64_div(q, float64_512, s);
7968 q = float64_div(float64_one, q, s);
7969
7970 /* s = (int)(256.0 * r + 0.5) */
7971 q = float64_mul(q, float64_256, s);
7972 q = float64_add(q, float64_half, s);
7973 q_int = float64_to_int64_round_to_zero(q, s);
7974
7975 /* return (double)s / 256.0 */
7976 return float64_div(int64_to_float64(q_int, s), float64_256, s);
7977}
7978
b6d4443a
AB
7979/* Common wrapper to call recip_estimate */
7980static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 7981{
b6d4443a
AB
7982 uint64_t val64 = float64_val(num);
7983 uint64_t frac = extract64(val64, 0, 52);
7984 int64_t exp = extract64(val64, 52, 11);
7985 uint64_t sbit;
7986 float64 scaled, estimate;
fe0e4872 7987
b6d4443a
AB
7988 /* Generate the scaled number for the estimate function */
7989 if (exp == 0) {
7990 if (extract64(frac, 51, 1) == 0) {
7991 exp = -1;
7992 frac = extract64(frac, 0, 50) << 2;
7993 } else {
7994 frac = extract64(frac, 0, 51) << 1;
7995 }
7996 }
fe0e4872 7997
b6d4443a
AB
7998 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
7999 scaled = make_float64((0x3feULL << 52)
8000 | extract64(frac, 44, 8) << 44);
8001
8002 estimate = recip_estimate(scaled, fpst);
8003
8004 /* Build new result */
8005 val64 = float64_val(estimate);
8006 sbit = 0x8000000000000000ULL & val64;
8007 exp = off - exp;
8008 frac = extract64(val64, 0, 52);
8009
8010 if (exp == 0) {
8011 frac = 1ULL << 51 | extract64(frac, 1, 51);
8012 } else if (exp == -1) {
8013 frac = 1ULL << 50 | extract64(frac, 2, 50);
8014 exp = 0;
8015 }
8016
8017 return make_float64(sbit | (exp << 52) | frac);
8018}
8019
8020static bool round_to_inf(float_status *fpst, bool sign_bit)
8021{
8022 switch (fpst->float_rounding_mode) {
8023 case float_round_nearest_even: /* Round to Nearest */
8024 return true;
8025 case float_round_up: /* Round to +Inf */
8026 return !sign_bit;
8027 case float_round_down: /* Round to -Inf */
8028 return sign_bit;
8029 case float_round_to_zero: /* Round to Zero */
8030 return false;
8031 }
8032
8033 g_assert_not_reached();
8034}
8035
8036float32 HELPER(recpe_f32)(float32 input, void *fpstp)
8037{
8038 float_status *fpst = fpstp;
8039 float32 f32 = float32_squash_input_denormal(input, fpst);
8040 uint32_t f32_val = float32_val(f32);
8041 uint32_t f32_sbit = 0x80000000ULL & f32_val;
8042 int32_t f32_exp = extract32(f32_val, 23, 8);
8043 uint32_t f32_frac = extract32(f32_val, 0, 23);
8044 float64 f64, r64;
8045 uint64_t r64_val;
8046 int64_t r64_exp;
8047 uint64_t r64_frac;
8048
8049 if (float32_is_any_nan(f32)) {
8050 float32 nan = f32;
8051 if (float32_is_signaling_nan(f32)) {
8052 float_raise(float_flag_invalid, fpst);
8053 nan = float32_maybe_silence_nan(f32);
fe0e4872 8054 }
b6d4443a
AB
8055 if (fpst->default_nan_mode) {
8056 nan = float32_default_nan;
43fe9bdb 8057 }
b6d4443a
AB
8058 return nan;
8059 } else if (float32_is_infinity(f32)) {
8060 return float32_set_sign(float32_zero, float32_is_neg(f32));
8061 } else if (float32_is_zero(f32)) {
8062 float_raise(float_flag_divbyzero, fpst);
8063 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8064 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
8065 /* Abs(value) < 2.0^-128 */
8066 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8067 if (round_to_inf(fpst, f32_sbit)) {
8068 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8069 } else {
8070 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
8071 }
8072 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
8073 float_raise(float_flag_underflow, fpst);
8074 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
8075 }
8076
fe0e4872 8077
b6d4443a
AB
8078 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
8079 r64 = call_recip_estimate(f64, 253, fpst);
8080 r64_val = float64_val(r64);
8081 r64_exp = extract64(r64_val, 52, 11);
8082 r64_frac = extract64(r64_val, 0, 52);
8083
8084 /* result = sign : result_exp<7:0> : fraction<51:29>; */
8085 return make_float32(f32_sbit |
8086 (r64_exp & 0xff) << 23 |
8087 extract64(r64_frac, 29, 24));
8088}
8089
8090float64 HELPER(recpe_f64)(float64 input, void *fpstp)
8091{
8092 float_status *fpst = fpstp;
8093 float64 f64 = float64_squash_input_denormal(input, fpst);
8094 uint64_t f64_val = float64_val(f64);
8095 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
8096 int64_t f64_exp = extract64(f64_val, 52, 11);
8097 float64 r64;
8098 uint64_t r64_val;
8099 int64_t r64_exp;
8100 uint64_t r64_frac;
8101
8102 /* Deal with any special cases */
8103 if (float64_is_any_nan(f64)) {
8104 float64 nan = f64;
8105 if (float64_is_signaling_nan(f64)) {
8106 float_raise(float_flag_invalid, fpst);
8107 nan = float64_maybe_silence_nan(f64);
8108 }
8109 if (fpst->default_nan_mode) {
8110 nan = float64_default_nan;
8111 }
8112 return nan;
8113 } else if (float64_is_infinity(f64)) {
8114 return float64_set_sign(float64_zero, float64_is_neg(f64));
8115 } else if (float64_is_zero(f64)) {
8116 float_raise(float_flag_divbyzero, fpst);
8117 return float64_set_sign(float64_infinity, float64_is_neg(f64));
8118 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
8119 /* Abs(value) < 2.0^-1024 */
8120 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8121 if (round_to_inf(fpst, f64_sbit)) {
8122 return float64_set_sign(float64_infinity, float64_is_neg(f64));
8123 } else {
8124 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
8125 }
fc1792e9 8126 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
8127 float_raise(float_flag_underflow, fpst);
8128 return float64_set_sign(float64_zero, float64_is_neg(f64));
8129 }
fe0e4872 8130
b6d4443a
AB
8131 r64 = call_recip_estimate(f64, 2045, fpst);
8132 r64_val = float64_val(r64);
8133 r64_exp = extract64(r64_val, 52, 11);
8134 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 8135
b6d4443a
AB
8136 /* result = sign : result_exp<10:0> : fraction<51:0> */
8137 return make_float64(f64_sbit |
8138 ((r64_exp & 0x7ff) << 52) |
8139 r64_frac);
4373f3ce
PB
8140}
8141
e07be5d2
CL
8142/* The algorithm that must be used to calculate the estimate
8143 * is specified by the ARM ARM.
8144 */
c2fb418e 8145static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 8146{
1146a817
PM
8147 /* These calculations mustn't set any fp exception flags,
8148 * so we use a local copy of the fp_status.
8149 */
c2fb418e 8150 float_status dummy_status = *real_fp_status;
1146a817 8151 float_status *s = &dummy_status;
e07be5d2
CL
8152 float64 q;
8153 int64_t q_int;
8154
8155 if (float64_lt(a, float64_half, s)) {
8156 /* range 0.25 <= a < 0.5 */
8157
8158 /* a in units of 1/512 rounded down */
8159 /* q0 = (int)(a * 512.0); */
8160 q = float64_mul(float64_512, a, s);
8161 q_int = float64_to_int64_round_to_zero(q, s);
8162
8163 /* reciprocal root r */
8164 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
8165 q = int64_to_float64(q_int, s);
8166 q = float64_add(q, float64_half, s);
8167 q = float64_div(q, float64_512, s);
8168 q = float64_sqrt(q, s);
8169 q = float64_div(float64_one, q, s);
8170 } else {
8171 /* range 0.5 <= a < 1.0 */
8172
8173 /* a in units of 1/256 rounded down */
8174 /* q1 = (int)(a * 256.0); */
8175 q = float64_mul(float64_256, a, s);
8176 int64_t q_int = float64_to_int64_round_to_zero(q, s);
8177
8178 /* reciprocal root r */
8179 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
8180 q = int64_to_float64(q_int, s);
8181 q = float64_add(q, float64_half, s);
8182 q = float64_div(q, float64_256, s);
8183 q = float64_sqrt(q, s);
8184 q = float64_div(float64_one, q, s);
8185 }
8186 /* r in units of 1/256 rounded to nearest */
8187 /* s = (int)(256.0 * r + 0.5); */
8188
8189 q = float64_mul(q, float64_256,s );
8190 q = float64_add(q, float64_half, s);
8191 q_int = float64_to_int64_round_to_zero(q, s);
8192
8193 /* return (double)s / 256.0;*/
8194 return float64_div(int64_to_float64(q_int, s), float64_256, s);
8195}
8196
c2fb418e 8197float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 8198{
c2fb418e
AB
8199 float_status *s = fpstp;
8200 float32 f32 = float32_squash_input_denormal(input, s);
8201 uint32_t val = float32_val(f32);
8202 uint32_t f32_sbit = 0x80000000 & val;
8203 int32_t f32_exp = extract32(val, 23, 8);
8204 uint32_t f32_frac = extract32(val, 0, 23);
8205 uint64_t f64_frac;
8206 uint64_t val64;
e07be5d2
CL
8207 int result_exp;
8208 float64 f64;
e07be5d2 8209
c2fb418e
AB
8210 if (float32_is_any_nan(f32)) {
8211 float32 nan = f32;
8212 if (float32_is_signaling_nan(f32)) {
e07be5d2 8213 float_raise(float_flag_invalid, s);
c2fb418e 8214 nan = float32_maybe_silence_nan(f32);
e07be5d2 8215 }
c2fb418e
AB
8216 if (s->default_nan_mode) {
8217 nan = float32_default_nan;
43fe9bdb 8218 }
c2fb418e
AB
8219 return nan;
8220 } else if (float32_is_zero(f32)) {
e07be5d2 8221 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
8222 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8223 } else if (float32_is_neg(f32)) {
e07be5d2
CL
8224 float_raise(float_flag_invalid, s);
8225 return float32_default_nan;
c2fb418e 8226 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
8227 return float32_zero;
8228 }
8229
c2fb418e 8230 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 8231 * preserving the parity of the exponent. */
c2fb418e
AB
8232
8233 f64_frac = ((uint64_t) f32_frac) << 29;
8234 if (f32_exp == 0) {
8235 while (extract64(f64_frac, 51, 1) == 0) {
8236 f64_frac = f64_frac << 1;
8237 f32_exp = f32_exp-1;
8238 }
8239 f64_frac = extract64(f64_frac, 0, 51) << 1;
8240 }
8241
8242 if (extract64(f32_exp, 0, 1) == 0) {
8243 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 8244 | (0x3feULL << 52)
c2fb418e 8245 | f64_frac);
e07be5d2 8246 } else {
c2fb418e 8247 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 8248 | (0x3fdULL << 52)
c2fb418e 8249 | f64_frac);
e07be5d2
CL
8250 }
8251
c2fb418e 8252 result_exp = (380 - f32_exp) / 2;
e07be5d2 8253
c2fb418e 8254 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
8255
8256 val64 = float64_val(f64);
8257
26cc6abf 8258 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
8259 | ((val64 >> 29) & 0x7fffff);
8260 return make_float32(val);
4373f3ce
PB
8261}
8262
c2fb418e
AB
8263float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
8264{
8265 float_status *s = fpstp;
8266 float64 f64 = float64_squash_input_denormal(input, s);
8267 uint64_t val = float64_val(f64);
8268 uint64_t f64_sbit = 0x8000000000000000ULL & val;
8269 int64_t f64_exp = extract64(val, 52, 11);
8270 uint64_t f64_frac = extract64(val, 0, 52);
8271 int64_t result_exp;
8272 uint64_t result_frac;
8273
8274 if (float64_is_any_nan(f64)) {
8275 float64 nan = f64;
8276 if (float64_is_signaling_nan(f64)) {
8277 float_raise(float_flag_invalid, s);
8278 nan = float64_maybe_silence_nan(f64);
8279 }
8280 if (s->default_nan_mode) {
8281 nan = float64_default_nan;
8282 }
8283 return nan;
8284 } else if (float64_is_zero(f64)) {
8285 float_raise(float_flag_divbyzero, s);
8286 return float64_set_sign(float64_infinity, float64_is_neg(f64));
8287 } else if (float64_is_neg(f64)) {
8288 float_raise(float_flag_invalid, s);
8289 return float64_default_nan;
8290 } else if (float64_is_infinity(f64)) {
8291 return float64_zero;
8292 }
8293
8294 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
8295 * preserving the parity of the exponent. */
8296
8297 if (f64_exp == 0) {
8298 while (extract64(f64_frac, 51, 1) == 0) {
8299 f64_frac = f64_frac << 1;
8300 f64_exp = f64_exp - 1;
8301 }
8302 f64_frac = extract64(f64_frac, 0, 51) << 1;
8303 }
8304
8305 if (extract64(f64_exp, 0, 1) == 0) {
8306 f64 = make_float64(f64_sbit
8307 | (0x3feULL << 52)
8308 | f64_frac);
8309 } else {
8310 f64 = make_float64(f64_sbit
8311 | (0x3fdULL << 52)
8312 | f64_frac);
8313 }
8314
8315 result_exp = (3068 - f64_exp) / 2;
8316
8317 f64 = recip_sqrt_estimate(f64, s);
8318
8319 result_frac = extract64(float64_val(f64), 0, 52);
8320
8321 return make_float64(f64_sbit |
8322 ((result_exp & 0x7ff) << 52) |
8323 result_frac);
8324}
8325
b6d4443a 8326uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 8327{
b6d4443a 8328 float_status *s = fpstp;
fe0e4872
CL
8329 float64 f64;
8330
8331 if ((a & 0x80000000) == 0) {
8332 return 0xffffffff;
8333 }
8334
8335 f64 = make_float64((0x3feULL << 52)
8336 | ((int64_t)(a & 0x7fffffff) << 21));
8337
b6d4443a 8338 f64 = recip_estimate(f64, s);
fe0e4872
CL
8339
8340 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
8341}
8342
c2fb418e 8343uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 8344{
c2fb418e 8345 float_status *fpst = fpstp;
e07be5d2
CL
8346 float64 f64;
8347
8348 if ((a & 0xc0000000) == 0) {
8349 return 0xffffffff;
8350 }
8351
8352 if (a & 0x80000000) {
8353 f64 = make_float64((0x3feULL << 52)
8354 | ((uint64_t)(a & 0x7fffffff) << 21));
8355 } else { /* bits 31-30 == '01' */
8356 f64 = make_float64((0x3fdULL << 52)
8357 | ((uint64_t)(a & 0x3fffffff) << 22));
8358 }
8359
c2fb418e 8360 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
8361
8362 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 8363}
fe1479c3 8364
da97f52c
PM
8365/* VFPv4 fused multiply-accumulate */
8366float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
8367{
8368 float_status *fpst = fpstp;
8369 return float32_muladd(a, b, c, 0, fpst);
8370}
8371
8372float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
8373{
8374 float_status *fpst = fpstp;
8375 return float64_muladd(a, b, c, 0, fpst);
8376}
d9b0848d
PM
8377
8378/* ARMv8 round to integral */
8379float32 HELPER(rints_exact)(float32 x, void *fp_status)
8380{
8381 return float32_round_to_int(x, fp_status);
8382}
8383
8384float64 HELPER(rintd_exact)(float64 x, void *fp_status)
8385{
8386 return float64_round_to_int(x, fp_status);
8387}
8388
8389float32 HELPER(rints)(float32 x, void *fp_status)
8390{
8391 int old_flags = get_float_exception_flags(fp_status), new_flags;
8392 float32 ret;
8393
8394 ret = float32_round_to_int(x, fp_status);
8395
8396 /* Suppress any inexact exceptions the conversion produced */
8397 if (!(old_flags & float_flag_inexact)) {
8398 new_flags = get_float_exception_flags(fp_status);
8399 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
8400 }
8401
8402 return ret;
8403}
8404
8405float64 HELPER(rintd)(float64 x, void *fp_status)
8406{
8407 int old_flags = get_float_exception_flags(fp_status), new_flags;
8408 float64 ret;
8409
8410 ret = float64_round_to_int(x, fp_status);
8411
8412 new_flags = get_float_exception_flags(fp_status);
8413
8414 /* Suppress any inexact exceptions the conversion produced */
8415 if (!(old_flags & float_flag_inexact)) {
8416 new_flags = get_float_exception_flags(fp_status);
8417 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
8418 }
8419
8420 return ret;
8421}
9972da66
WN
8422
8423/* Convert ARM rounding mode to softfloat */
8424int arm_rmode_to_sf(int rmode)
8425{
8426 switch (rmode) {
8427 case FPROUNDING_TIEAWAY:
8428 rmode = float_round_ties_away;
8429 break;
8430 case FPROUNDING_ODD:
8431 /* FIXME: add support for TIEAWAY and ODD */
8432 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
8433 rmode);
8434 case FPROUNDING_TIEEVEN:
8435 default:
8436 rmode = float_round_nearest_even;
8437 break;
8438 case FPROUNDING_POSINF:
8439 rmode = float_round_up;
8440 break;
8441 case FPROUNDING_NEGINF:
8442 rmode = float_round_down;
8443 break;
8444 case FPROUNDING_ZERO:
8445 rmode = float_round_to_zero;
8446 break;
8447 }
8448 return rmode;
8449}
eb0ecd5a 8450
aa633469
PM
8451/* CRC helpers.
8452 * The upper bytes of val (above the number specified by 'bytes') must have
8453 * been zeroed out by the caller.
8454 */
eb0ecd5a
WN
8455uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
8456{
8457 uint8_t buf[4];
8458
aa633469 8459 stl_le_p(buf, val);
eb0ecd5a
WN
8460
8461 /* zlib crc32 converts the accumulator and output to one's complement. */
8462 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
8463}
8464
8465uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
8466{
8467 uint8_t buf[4];
8468
aa633469 8469 stl_le_p(buf, val);
eb0ecd5a
WN
8470
8471 /* Linux crc32c converts the output to one's complement. */
8472 return crc32c(acc, buf, bytes) ^ 0xffffffff;
8473}
This page took 2.163719 seconds and 4 git commands to generate.