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