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