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