]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Remove fsr argument from arm_ld*_ptw()
[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
5b2d261d
AB
22/* Cacheability and shareability attributes for a memory access */
23typedef struct ARMCacheAttrs {
24 unsigned int attrs:8; /* as in the MAIR register encoding */
25 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
26} ARMCacheAttrs;
27
af51f566 28static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 29 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 30 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23 31 target_ulong *page_size, uint32_t *fsr,
5b2d261d 32 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
7c2cb42b 33
37785977 34static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 35 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977
EI
36 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
37 target_ulong *page_size_ptr, uint32_t *fsr,
5b2d261d 38 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
37785977 39
35337cc3
PM
40/* Security attributes for an address, as returned by v8m_security_lookup. */
41typedef struct V8M_SAttributes {
42 bool ns;
43 bool nsc;
44 uint8_t sregion;
45 bool srvalid;
46 uint8_t iregion;
47 bool irvalid;
48} V8M_SAttributes;
49
333e10c5
PM
50static void v8m_security_lookup(CPUARMState *env, uint32_t address,
51 MMUAccessType access_type, ARMMMUIdx mmu_idx,
52 V8M_SAttributes *sattrs);
53
7c2cb42b
AF
54/* Definitions for the PMCCNTR and PMCR registers */
55#define PMCRD 0x8
56#define PMCRC 0x4
57#define PMCRE 0x1
4a501606
PM
58#endif
59
0ecb72a5 60static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
61{
62 int nregs;
63
64 /* VFP data registers are always little-endian. */
65 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
66 if (reg < nregs) {
67 stfq_le_p(buf, env->vfp.regs[reg]);
68 return 8;
69 }
70 if (arm_feature(env, ARM_FEATURE_NEON)) {
71 /* Aliases for Q regs. */
72 nregs += 16;
73 if (reg < nregs) {
74 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
75 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
76 return 16;
77 }
78 }
79 switch (reg - nregs) {
80 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
81 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
82 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
83 }
84 return 0;
85}
86
0ecb72a5 87static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
88{
89 int nregs;
90
91 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
92 if (reg < nregs) {
93 env->vfp.regs[reg] = ldfq_le_p(buf);
94 return 8;
95 }
96 if (arm_feature(env, ARM_FEATURE_NEON)) {
97 nregs += 16;
98 if (reg < nregs) {
99 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
100 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
101 return 16;
102 }
103 }
104 switch (reg - nregs) {
105 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
106 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 107 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
108 }
109 return 0;
110}
111
6a669427
PM
112static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
113{
114 switch (reg) {
115 case 0 ... 31:
116 /* 128 bit FP register */
117 stfq_le_p(buf, env->vfp.regs[reg * 2]);
118 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
119 return 16;
120 case 32:
121 /* FPSR */
122 stl_p(buf, vfp_get_fpsr(env));
123 return 4;
124 case 33:
125 /* FPCR */
126 stl_p(buf, vfp_get_fpcr(env));
127 return 4;
128 default:
129 return 0;
130 }
131}
132
133static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
134{
135 switch (reg) {
136 case 0 ... 31:
137 /* 128 bit FP register */
138 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
139 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
140 return 16;
141 case 32:
142 /* FPSR */
143 vfp_set_fpsr(env, ldl_p(buf));
144 return 4;
145 case 33:
146 /* FPCR */
147 vfp_set_fpcr(env, ldl_p(buf));
148 return 4;
149 default:
150 return 0;
151 }
152}
153
c4241c7d 154static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 155{
375421cc 156 assert(ri->fieldoffset);
67ed771d 157 if (cpreg_field_is_64bit(ri)) {
c4241c7d 158 return CPREG_FIELD64(env, ri);
22d9e1a9 159 } else {
c4241c7d 160 return CPREG_FIELD32(env, ri);
22d9e1a9 161 }
d4e6df63
PM
162}
163
c4241c7d
PM
164static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
165 uint64_t value)
d4e6df63 166{
375421cc 167 assert(ri->fieldoffset);
67ed771d 168 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
169 CPREG_FIELD64(env, ri) = value;
170 } else {
171 CPREG_FIELD32(env, ri) = value;
172 }
d4e6df63
PM
173}
174
11f136ee
FA
175static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
176{
177 return (char *)env + ri->fieldoffset;
178}
179
49a66191 180uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 181{
59a1c327 182 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 183 if (ri->type & ARM_CP_CONST) {
59a1c327 184 return ri->resetvalue;
721fae12 185 } else if (ri->raw_readfn) {
59a1c327 186 return ri->raw_readfn(env, ri);
721fae12 187 } else if (ri->readfn) {
59a1c327 188 return ri->readfn(env, ri);
721fae12 189 } else {
59a1c327 190 return raw_read(env, ri);
721fae12 191 }
721fae12
PM
192}
193
59a1c327 194static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 195 uint64_t v)
721fae12
PM
196{
197 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
198 * Note that constant registers are treated as write-ignored; the
199 * caller should check for success by whether a readback gives the
200 * value written.
201 */
202 if (ri->type & ARM_CP_CONST) {
59a1c327 203 return;
721fae12 204 } else if (ri->raw_writefn) {
c4241c7d 205 ri->raw_writefn(env, ri, v);
721fae12 206 } else if (ri->writefn) {
c4241c7d 207 ri->writefn(env, ri, v);
721fae12 208 } else {
afb2530f 209 raw_write(env, ri, v);
721fae12 210 }
721fae12
PM
211}
212
375421cc
PM
213static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
214{
215 /* Return true if the regdef would cause an assertion if you called
216 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
217 * program bug for it not to have the NO_RAW flag).
218 * NB that returning false here doesn't necessarily mean that calling
219 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
220 * read/write access functions which are safe for raw use" from "has
221 * read/write access functions which have side effects but has forgotten
222 * to provide raw access functions".
223 * The tests here line up with the conditions in read/write_raw_cp_reg()
224 * and assertions in raw_read()/raw_write().
225 */
226 if ((ri->type & ARM_CP_CONST) ||
227 ri->fieldoffset ||
228 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
229 return false;
230 }
231 return true;
232}
233
721fae12
PM
234bool write_cpustate_to_list(ARMCPU *cpu)
235{
236 /* Write the coprocessor state from cpu->env to the (index,value) list. */
237 int i;
238 bool ok = true;
239
240 for (i = 0; i < cpu->cpreg_array_len; i++) {
241 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
242 const ARMCPRegInfo *ri;
59a1c327 243
60322b39 244 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
245 if (!ri) {
246 ok = false;
247 continue;
248 }
7a0e58fa 249 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
250 continue;
251 }
59a1c327 252 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
253 }
254 return ok;
255}
256
257bool write_list_to_cpustate(ARMCPU *cpu)
258{
259 int i;
260 bool ok = true;
261
262 for (i = 0; i < cpu->cpreg_array_len; i++) {
263 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
264 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
265 const ARMCPRegInfo *ri;
266
60322b39 267 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
268 if (!ri) {
269 ok = false;
270 continue;
271 }
7a0e58fa 272 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
273 continue;
274 }
275 /* Write value and confirm it reads back as written
276 * (to catch read-only registers and partially read-only
277 * registers where the incoming migration value doesn't match)
278 */
59a1c327
PM
279 write_raw_cp_reg(&cpu->env, ri, v);
280 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
281 ok = false;
282 }
283 }
284 return ok;
285}
286
287static void add_cpreg_to_list(gpointer key, gpointer opaque)
288{
289 ARMCPU *cpu = opaque;
290 uint64_t regidx;
291 const ARMCPRegInfo *ri;
292
293 regidx = *(uint32_t *)key;
60322b39 294 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 295
7a0e58fa 296 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
297 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
298 /* The value array need not be initialized at this point */
299 cpu->cpreg_array_len++;
300 }
301}
302
303static void count_cpreg(gpointer key, gpointer opaque)
304{
305 ARMCPU *cpu = opaque;
306 uint64_t regidx;
307 const ARMCPRegInfo *ri;
308
309 regidx = *(uint32_t *)key;
60322b39 310 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 311
7a0e58fa 312 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
313 cpu->cpreg_array_len++;
314 }
315}
316
317static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
318{
cbf239b7
AR
319 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
320 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 321
cbf239b7
AR
322 if (aidx > bidx) {
323 return 1;
324 }
325 if (aidx < bidx) {
326 return -1;
327 }
328 return 0;
721fae12
PM
329}
330
331void init_cpreg_list(ARMCPU *cpu)
332{
333 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
334 * Note that we require cpreg_tuples[] to be sorted by key ID.
335 */
57b6d95e 336 GList *keys;
721fae12
PM
337 int arraylen;
338
57b6d95e 339 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
340 keys = g_list_sort(keys, cpreg_key_compare);
341
342 cpu->cpreg_array_len = 0;
343
344 g_list_foreach(keys, count_cpreg, cpu);
345
346 arraylen = cpu->cpreg_array_len;
347 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
348 cpu->cpreg_values = g_new(uint64_t, arraylen);
349 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
350 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
351 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
352 cpu->cpreg_array_len = 0;
353
354 g_list_foreach(keys, add_cpreg_to_list, cpu);
355
356 assert(cpu->cpreg_array_len == arraylen);
357
358 g_list_free(keys);
359}
360
68e9c2fe
EI
361/*
362 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
363 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
364 *
365 * access_el3_aa32ns: Used to check AArch32 register views.
366 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
367 */
368static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
369 const ARMCPRegInfo *ri,
370 bool isread)
68e9c2fe
EI
371{
372 bool secure = arm_is_secure_below_el3(env);
373
374 assert(!arm_el_is_aa64(env, 3));
375 if (secure) {
376 return CP_ACCESS_TRAP_UNCATEGORIZED;
377 }
378 return CP_ACCESS_OK;
379}
380
381static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
382 const ARMCPRegInfo *ri,
383 bool isread)
68e9c2fe
EI
384{
385 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 386 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
387 }
388 return CP_ACCESS_OK;
389}
390
5513c3ab
PM
391/* Some secure-only AArch32 registers trap to EL3 if used from
392 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
393 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
394 * We assume that the .access field is set to PL1_RW.
395 */
396static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
397 const ARMCPRegInfo *ri,
398 bool isread)
5513c3ab
PM
399{
400 if (arm_current_el(env) == 3) {
401 return CP_ACCESS_OK;
402 }
403 if (arm_is_secure_below_el3(env)) {
404 return CP_ACCESS_TRAP_EL3;
405 }
406 /* This will be EL1 NS and EL2 NS, which just UNDEF */
407 return CP_ACCESS_TRAP_UNCATEGORIZED;
408}
409
187f678d
PM
410/* Check for traps to "powerdown debug" registers, which are controlled
411 * by MDCR.TDOSA
412 */
413static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
414 bool isread)
415{
416 int el = arm_current_el(env);
417
418 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
419 && !arm_is_secure_below_el3(env)) {
420 return CP_ACCESS_TRAP_EL2;
421 }
422 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
423 return CP_ACCESS_TRAP_EL3;
424 }
425 return CP_ACCESS_OK;
426}
427
91b0a238
PM
428/* Check for traps to "debug ROM" registers, which are controlled
429 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
430 */
431static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
432 bool isread)
433{
434 int el = arm_current_el(env);
435
436 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
437 && !arm_is_secure_below_el3(env)) {
438 return CP_ACCESS_TRAP_EL2;
439 }
440 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
441 return CP_ACCESS_TRAP_EL3;
442 }
443 return CP_ACCESS_OK;
444}
445
d6c8cf81
PM
446/* Check for traps to general debug registers, which are controlled
447 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
448 */
449static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
450 bool isread)
451{
452 int el = arm_current_el(env);
453
454 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
455 && !arm_is_secure_below_el3(env)) {
456 return CP_ACCESS_TRAP_EL2;
457 }
458 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
459 return CP_ACCESS_TRAP_EL3;
460 }
461 return CP_ACCESS_OK;
462}
463
1fce1ba9
PM
464/* Check for traps to performance monitor registers, which are controlled
465 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
466 */
467static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
468 bool isread)
469{
470 int el = arm_current_el(env);
471
472 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
473 && !arm_is_secure_below_el3(env)) {
474 return CP_ACCESS_TRAP_EL2;
475 }
476 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
477 return CP_ACCESS_TRAP_EL3;
478 }
479 return CP_ACCESS_OK;
480}
481
c4241c7d 482static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 483{
00c8cb0a
AF
484 ARMCPU *cpu = arm_env_get_cpu(env);
485
8d5c773e 486 raw_write(env, ri, value);
d10eb08f 487 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
488}
489
c4241c7d 490static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 491{
00c8cb0a
AF
492 ARMCPU *cpu = arm_env_get_cpu(env);
493
8d5c773e 494 if (raw_read(env, ri) != value) {
08de207b
PM
495 /* Unlike real hardware the qemu TLB uses virtual addresses,
496 * not modified virtual addresses, so this causes a TLB flush.
497 */
d10eb08f 498 tlb_flush(CPU(cpu));
8d5c773e 499 raw_write(env, ri, value);
08de207b 500 }
08de207b 501}
c4241c7d
PM
502
503static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
504 uint64_t value)
08de207b 505{
00c8cb0a
AF
506 ARMCPU *cpu = arm_env_get_cpu(env);
507
452a0955 508 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 509 && !extended_addresses_enabled(env)) {
08de207b
PM
510 /* For VMSA (when not using the LPAE long descriptor page table
511 * format) this register includes the ASID, so do a TLB flush.
512 * For PMSA it is purely a process ID and no action is needed.
513 */
d10eb08f 514 tlb_flush(CPU(cpu));
08de207b 515 }
8d5c773e 516 raw_write(env, ri, value);
08de207b
PM
517}
518
c4241c7d
PM
519static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
520 uint64_t value)
d929823f
PM
521{
522 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
523 ARMCPU *cpu = arm_env_get_cpu(env);
524
d10eb08f 525 tlb_flush(CPU(cpu));
d929823f
PM
526}
527
c4241c7d
PM
528static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
529 uint64_t value)
d929823f
PM
530{
531 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
532 ARMCPU *cpu = arm_env_get_cpu(env);
533
534 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
535}
536
c4241c7d
PM
537static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
538 uint64_t value)
d929823f
PM
539{
540 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
541 ARMCPU *cpu = arm_env_get_cpu(env);
542
d10eb08f 543 tlb_flush(CPU(cpu));
d929823f
PM
544}
545
c4241c7d
PM
546static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
547 uint64_t value)
d929823f
PM
548{
549 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
550 ARMCPU *cpu = arm_env_get_cpu(env);
551
552 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
553}
554
fa439fc5
PM
555/* IS variants of TLB operations must affect all cores */
556static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
557 uint64_t value)
558{
a67cf277 559 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 560
a67cf277 561 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
562}
563
564static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566{
a67cf277 567 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 568
a67cf277 569 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
570}
571
572static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
573 uint64_t value)
574{
a67cf277 575 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 576
a67cf277 577 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
578}
579
580static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
581 uint64_t value)
582{
a67cf277 583 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 584
a67cf277 585 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
586}
587
541ef8c2
SS
588static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
589 uint64_t value)
590{
591 CPUState *cs = ENV_GET_CPU(env);
592
0336cbf8 593 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
594 ARMMMUIdxBit_S12NSE1 |
595 ARMMMUIdxBit_S12NSE0 |
596 ARMMMUIdxBit_S2NS);
541ef8c2
SS
597}
598
599static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
600 uint64_t value)
601{
a67cf277 602 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 603
a67cf277 604 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
605 ARMMMUIdxBit_S12NSE1 |
606 ARMMMUIdxBit_S12NSE0 |
607 ARMMMUIdxBit_S2NS);
541ef8c2
SS
608}
609
610static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
611 uint64_t value)
612{
613 /* Invalidate by IPA. This has to invalidate any structures that
614 * contain only stage 2 translation information, but does not need
615 * to apply to structures that contain combined stage 1 and stage 2
616 * translation information.
617 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
618 */
619 CPUState *cs = ENV_GET_CPU(env);
620 uint64_t pageaddr;
621
622 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
623 return;
624 }
625
626 pageaddr = sextract64(value << 12, 0, 40);
627
8bd5c820 628 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
541ef8c2
SS
629}
630
631static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
632 uint64_t value)
633{
a67cf277 634 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
635 uint64_t pageaddr;
636
637 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
638 return;
639 }
640
641 pageaddr = sextract64(value << 12, 0, 40);
642
a67cf277 643 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 644 ARMMMUIdxBit_S2NS);
541ef8c2
SS
645}
646
647static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
648 uint64_t value)
649{
650 CPUState *cs = ENV_GET_CPU(env);
651
8bd5c820 652 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
653}
654
655static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
656 uint64_t value)
657{
a67cf277 658 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 659
8bd5c820 660 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
661}
662
663static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
664 uint64_t value)
665{
666 CPUState *cs = ENV_GET_CPU(env);
667 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
668
8bd5c820 669 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
541ef8c2
SS
670}
671
672static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
673 uint64_t value)
674{
a67cf277 675 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
676 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
677
a67cf277 678 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 679 ARMMMUIdxBit_S1E2);
541ef8c2
SS
680}
681
e9aa6c21 682static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
683 /* Define the secure and non-secure FCSE identifier CP registers
684 * separately because there is no secure bank in V8 (no _EL3). This allows
685 * the secure register to be properly reset and migrated. There is also no
686 * v8 EL1 version of the register so the non-secure instance stands alone.
687 */
688 { .name = "FCSEIDR(NS)",
689 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
690 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
691 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
692 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
693 { .name = "FCSEIDR(S)",
694 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
695 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
696 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 697 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
698 /* Define the secure and non-secure context identifier CP registers
699 * separately because there is no secure bank in V8 (no _EL3). This allows
700 * the secure register to be properly reset and migrated. In the
701 * non-secure case, the 32-bit register will have reset and migration
702 * disabled during registration as it is handled by the 64-bit instance.
703 */
704 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 705 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
706 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
707 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
708 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
709 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
710 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
711 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
712 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 713 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
714 REGINFO_SENTINEL
715};
716
717static const ARMCPRegInfo not_v8_cp_reginfo[] = {
718 /* NB: Some of these registers exist in v8 but with more precise
719 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
720 */
721 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
722 { .name = "DACR",
723 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
724 .access = PL1_RW, .resetvalue = 0,
725 .writefn = dacr_write, .raw_writefn = raw_write,
726 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
727 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
728 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
729 * For v6 and v5, these mappings are overly broad.
4fdd17dd 730 */
a903c449
EI
731 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
732 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
733 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
734 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
735 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
736 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
737 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 738 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
739 /* Cache maintenance ops; some of this space may be overridden later. */
740 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
741 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
742 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
743 REGINFO_SENTINEL
744};
745
7d57f408
PM
746static const ARMCPRegInfo not_v6_cp_reginfo[] = {
747 /* Not all pre-v6 cores implemented this WFI, so this is slightly
748 * over-broad.
749 */
750 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
751 .access = PL1_W, .type = ARM_CP_WFI },
752 REGINFO_SENTINEL
753};
754
755static const ARMCPRegInfo not_v7_cp_reginfo[] = {
756 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
757 * is UNPREDICTABLE; we choose to NOP as most implementations do).
758 */
759 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
760 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
761 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
762 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
763 * OMAPCP will override this space.
764 */
765 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
766 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
767 .resetvalue = 0 },
768 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
769 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
770 .resetvalue = 0 },
776d4e5c
PM
771 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
772 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 773 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 774 .resetvalue = 0 },
50300698
PM
775 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
776 * implementing it as RAZ means the "debug architecture version" bits
777 * will read as a reserved value, which should cause Linux to not try
778 * to use the debug hardware.
779 */
780 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
781 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
782 /* MMU TLB control. Note that the wildcarding means we cover not just
783 * the unified TLB ops but also the dside/iside/inner-shareable variants.
784 */
785 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
786 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 787 .type = ARM_CP_NO_RAW },
995939a6
PM
788 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
789 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 790 .type = ARM_CP_NO_RAW },
995939a6
PM
791 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
792 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 793 .type = ARM_CP_NO_RAW },
995939a6
PM
794 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
795 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 796 .type = ARM_CP_NO_RAW },
a903c449
EI
797 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
798 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
799 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
800 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
801 REGINFO_SENTINEL
802};
803
c4241c7d
PM
804static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
805 uint64_t value)
2771db27 806{
f0aff255
FA
807 uint32_t mask = 0;
808
809 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
810 if (!arm_feature(env, ARM_FEATURE_V8)) {
811 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
812 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
813 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
814 */
815 if (arm_feature(env, ARM_FEATURE_VFP)) {
816 /* VFP coprocessor: cp10 & cp11 [23:20] */
817 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
818
819 if (!arm_feature(env, ARM_FEATURE_NEON)) {
820 /* ASEDIS [31] bit is RAO/WI */
821 value |= (1 << 31);
822 }
823
824 /* VFPv3 and upwards with NEON implement 32 double precision
825 * registers (D0-D31).
826 */
827 if (!arm_feature(env, ARM_FEATURE_NEON) ||
828 !arm_feature(env, ARM_FEATURE_VFP3)) {
829 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
830 value |= (1 << 30);
831 }
832 }
833 value &= mask;
2771db27 834 }
7ebd5f2e 835 env->cp15.cpacr_el1 = value;
2771db27
PM
836}
837
3f208fd7
PM
838static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
839 bool isread)
c6f19164
GB
840{
841 if (arm_feature(env, ARM_FEATURE_V8)) {
842 /* Check if CPACR accesses are to be trapped to EL2 */
843 if (arm_current_el(env) == 1 &&
844 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
845 return CP_ACCESS_TRAP_EL2;
846 /* Check if CPACR accesses are to be trapped to EL3 */
847 } else if (arm_current_el(env) < 3 &&
848 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
849 return CP_ACCESS_TRAP_EL3;
850 }
851 }
852
853 return CP_ACCESS_OK;
854}
855
3f208fd7
PM
856static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
857 bool isread)
c6f19164
GB
858{
859 /* Check if CPTR accesses are set to trap to EL3 */
860 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
861 return CP_ACCESS_TRAP_EL3;
862 }
863
864 return CP_ACCESS_OK;
865}
866
7d57f408
PM
867static const ARMCPRegInfo v6_cp_reginfo[] = {
868 /* prefetch by MVA in v6, NOP in v7 */
869 { .name = "MVA_prefetch",
870 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
871 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
872 /* We need to break the TB after ISB to execute self-modifying code
873 * correctly and also to take any pending interrupts immediately.
874 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
875 */
7d57f408 876 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 877 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 878 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 879 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 880 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 881 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 882 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 883 .access = PL1_RW,
b848ce2b
FA
884 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
885 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
886 .resetvalue = 0, },
887 /* Watchpoint Fault Address Register : should actually only be present
888 * for 1136, 1176, 11MPCore.
889 */
890 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
891 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 892 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 893 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 894 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 895 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
896 REGINFO_SENTINEL
897};
898
3f208fd7
PM
899static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
900 bool isread)
200ac0ef 901{
3b163b01 902 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
903 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
904 * trapping to EL2 or EL3 for other accesses.
200ac0ef 905 */
1fce1ba9
PM
906 int el = arm_current_el(env);
907
6ecd0b6b 908 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 909 return CP_ACCESS_TRAP;
200ac0ef 910 }
1fce1ba9
PM
911 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
912 && !arm_is_secure_below_el3(env)) {
913 return CP_ACCESS_TRAP_EL2;
914 }
915 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
916 return CP_ACCESS_TRAP_EL3;
917 }
918
fcd25206 919 return CP_ACCESS_OK;
200ac0ef
PM
920}
921
6ecd0b6b
AB
922static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
923 const ARMCPRegInfo *ri,
924 bool isread)
925{
926 /* ER: event counter read trap control */
927 if (arm_feature(env, ARM_FEATURE_V8)
928 && arm_current_el(env) == 0
929 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
930 && isread) {
931 return CP_ACCESS_OK;
932 }
933
934 return pmreg_access(env, ri, isread);
935}
936
937static CPAccessResult pmreg_access_swinc(CPUARMState *env,
938 const ARMCPRegInfo *ri,
939 bool isread)
940{
941 /* SW: software increment write trap control */
942 if (arm_feature(env, ARM_FEATURE_V8)
943 && arm_current_el(env) == 0
944 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
945 && !isread) {
946 return CP_ACCESS_OK;
947 }
948
949 return pmreg_access(env, ri, isread);
950}
951
7c2cb42b 952#ifndef CONFIG_USER_ONLY
87124fde 953
6ecd0b6b
AB
954static CPAccessResult pmreg_access_selr(CPUARMState *env,
955 const ARMCPRegInfo *ri,
956 bool isread)
957{
958 /* ER: event counter read trap control */
959 if (arm_feature(env, ARM_FEATURE_V8)
960 && arm_current_el(env) == 0
961 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
962 return CP_ACCESS_OK;
963 }
964
965 return pmreg_access(env, ri, isread);
966}
967
968static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
969 const ARMCPRegInfo *ri,
970 bool isread)
971{
972 /* CR: cycle counter read trap control */
973 if (arm_feature(env, ARM_FEATURE_V8)
974 && arm_current_el(env) == 0
975 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
976 && isread) {
977 return CP_ACCESS_OK;
978 }
979
980 return pmreg_access(env, ri, isread);
981}
982
87124fde
AF
983static inline bool arm_ccnt_enabled(CPUARMState *env)
984{
985 /* This does not support checking PMCCFILTR_EL0 register */
986
987 if (!(env->cp15.c9_pmcr & PMCRE)) {
988 return false;
989 }
990
991 return true;
992}
993
ec7b4ce4
AF
994void pmccntr_sync(CPUARMState *env)
995{
996 uint64_t temp_ticks;
997
352c98e5
LV
998 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
999 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
1000
1001 if (env->cp15.c9_pmcr & PMCRD) {
1002 /* Increment once every 64 processor clock cycles */
1003 temp_ticks /= 64;
1004 }
1005
1006 if (arm_ccnt_enabled(env)) {
1007 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1008 }
1009}
1010
c4241c7d
PM
1011static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1012 uint64_t value)
200ac0ef 1013{
942a155b 1014 pmccntr_sync(env);
7c2cb42b
AF
1015
1016 if (value & PMCRC) {
1017 /* The counter has been reset */
1018 env->cp15.c15_ccnt = 0;
1019 }
1020
200ac0ef
PM
1021 /* only the DP, X, D and E bits are writable */
1022 env->cp15.c9_pmcr &= ~0x39;
1023 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1024
942a155b 1025 pmccntr_sync(env);
7c2cb42b
AF
1026}
1027
1028static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1029{
c92c0687 1030 uint64_t total_ticks;
7c2cb42b 1031
942a155b 1032 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1033 /* Counter is disabled, do not change value */
1034 return env->cp15.c15_ccnt;
1035 }
1036
352c98e5
LV
1037 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1038 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1039
1040 if (env->cp15.c9_pmcr & PMCRD) {
1041 /* Increment once every 64 processor clock cycles */
1042 total_ticks /= 64;
1043 }
1044 return total_ticks - env->cp15.c15_ccnt;
1045}
1046
6b040780
WH
1047static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1048 uint64_t value)
1049{
1050 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1051 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1052 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1053 * accessed.
1054 */
1055 env->cp15.c9_pmselr = value & 0x1f;
1056}
1057
7c2cb42b
AF
1058static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1059 uint64_t value)
1060{
c92c0687 1061 uint64_t total_ticks;
7c2cb42b 1062
942a155b 1063 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1064 /* Counter is disabled, set the absolute value */
1065 env->cp15.c15_ccnt = value;
1066 return;
1067 }
1068
352c98e5
LV
1069 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1070 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1071
1072 if (env->cp15.c9_pmcr & PMCRD) {
1073 /* Increment once every 64 processor clock cycles */
1074 total_ticks /= 64;
1075 }
1076 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 1077}
421c7ebd
PC
1078
1079static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1080 uint64_t value)
1081{
1082 uint64_t cur_val = pmccntr_read(env, NULL);
1083
1084 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1085}
1086
ec7b4ce4
AF
1087#else /* CONFIG_USER_ONLY */
1088
1089void pmccntr_sync(CPUARMState *env)
1090{
1091}
1092
7c2cb42b 1093#endif
200ac0ef 1094
0614601c
AF
1095static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1096 uint64_t value)
1097{
1098 pmccntr_sync(env);
1099 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
1100 pmccntr_sync(env);
1101}
1102
c4241c7d 1103static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1104 uint64_t value)
1105{
200ac0ef
PM
1106 value &= (1 << 31);
1107 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1108}
1109
c4241c7d
PM
1110static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1111 uint64_t value)
200ac0ef 1112{
200ac0ef
PM
1113 value &= (1 << 31);
1114 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1115}
1116
c4241c7d
PM
1117static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1118 uint64_t value)
200ac0ef 1119{
200ac0ef 1120 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
1121}
1122
c4241c7d
PM
1123static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1124 uint64_t value)
200ac0ef 1125{
fdb86656
WH
1126 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1127 * PMSELR value is equal to or greater than the number of implemented
1128 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1129 */
1130 if (env->cp15.c9_pmselr == 0x1f) {
1131 pmccfiltr_write(env, ri, value);
1132 }
1133}
1134
1135static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1136{
1137 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1138 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1139 */
1140 if (env->cp15.c9_pmselr == 0x1f) {
1141 return env->cp15.pmccfiltr_el0;
1142 } else {
1143 return 0;
1144 }
200ac0ef
PM
1145}
1146
c4241c7d 1147static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1148 uint64_t value)
1149{
6ecd0b6b
AB
1150 if (arm_feature(env, ARM_FEATURE_V8)) {
1151 env->cp15.c9_pmuserenr = value & 0xf;
1152 } else {
1153 env->cp15.c9_pmuserenr = value & 1;
1154 }
200ac0ef
PM
1155}
1156
c4241c7d
PM
1157static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1158 uint64_t value)
200ac0ef
PM
1159{
1160 /* We have no event counters so only the C bit can be changed */
1161 value &= (1 << 31);
1162 env->cp15.c9_pminten |= value;
200ac0ef
PM
1163}
1164
c4241c7d
PM
1165static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1166 uint64_t value)
200ac0ef
PM
1167{
1168 value &= (1 << 31);
1169 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
1170}
1171
c4241c7d
PM
1172static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1173 uint64_t value)
8641136c 1174{
a505d7fe
PM
1175 /* Note that even though the AArch64 view of this register has bits
1176 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1177 * architectural requirements for bits which are RES0 only in some
1178 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1179 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1180 */
855ea66d 1181 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1182}
1183
64e0e2de
EI
1184static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1185{
1186 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1187 * For bits that vary between AArch32/64, code needs to check the
1188 * current execution mode before directly using the feature bit.
1189 */
1190 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1191
1192 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1193 valid_mask &= ~SCR_HCE;
1194
1195 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1196 * supported if EL2 exists. The bit is UNK/SBZP when
1197 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1198 * when EL2 is unavailable.
4eb27640 1199 * On ARMv8, this bit is always available.
64e0e2de 1200 */
4eb27640
GB
1201 if (arm_feature(env, ARM_FEATURE_V7) &&
1202 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1203 valid_mask &= ~SCR_SMD;
1204 }
1205 }
1206
1207 /* Clear all-context RES0 bits. */
1208 value &= valid_mask;
1209 raw_write(env, ri, value);
1210}
1211
c4241c7d 1212static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1213{
1214 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1215
1216 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1217 * bank
1218 */
1219 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1220 ri->secure & ARM_CP_SECSTATE_S);
1221
1222 return cpu->ccsidr[index];
776d4e5c
PM
1223}
1224
c4241c7d
PM
1225static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1226 uint64_t value)
776d4e5c 1227{
8d5c773e 1228 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1229}
1230
1090b9c6
PM
1231static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1232{
1233 CPUState *cs = ENV_GET_CPU(env);
1234 uint64_t ret = 0;
1235
1236 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1237 ret |= CPSR_I;
1238 }
1239 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1240 ret |= CPSR_F;
1241 }
1242 /* External aborts are not possible in QEMU so A bit is always clear */
1243 return ret;
1244}
1245
e9aa6c21 1246static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1247 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1248 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1249 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1250 /* Performance monitors are implementation defined in v7,
1251 * but with an ARM recommended set of registers, which we
1252 * follow (although we don't actually implement any counters)
1253 *
1254 * Performance registers fall into three categories:
1255 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1256 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1257 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1258 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1259 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1260 */
1261 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1262 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1263 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1264 .writefn = pmcntenset_write,
1265 .accessfn = pmreg_access,
1266 .raw_writefn = raw_write },
8521466b
AF
1267 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1268 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1269 .access = PL0_RW, .accessfn = pmreg_access,
1270 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1271 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1272 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1273 .access = PL0_RW,
1274 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1275 .accessfn = pmreg_access,
1276 .writefn = pmcntenclr_write,
7a0e58fa 1277 .type = ARM_CP_ALIAS },
8521466b
AF
1278 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1279 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1280 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1281 .type = ARM_CP_ALIAS,
8521466b
AF
1282 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1283 .writefn = pmcntenclr_write },
200ac0ef
PM
1284 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1285 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1286 .accessfn = pmreg_access,
1287 .writefn = pmovsr_write,
1288 .raw_writefn = raw_write },
978364f1
AF
1289 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1290 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1291 .access = PL0_RW, .accessfn = pmreg_access,
1292 .type = ARM_CP_ALIAS,
1293 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1294 .writefn = pmovsr_write,
1295 .raw_writefn = raw_write },
fcd25206 1296 /* Unimplemented so WI. */
200ac0ef 1297 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
6ecd0b6b 1298 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
7c2cb42b 1299#ifndef CONFIG_USER_ONLY
6b040780
WH
1300 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1301 .access = PL0_RW, .type = ARM_CP_ALIAS,
1302 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1303 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1304 .raw_writefn = raw_write},
1305 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1306 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1307 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1308 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1309 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1310 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 1311 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 1312 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1313 .accessfn = pmreg_access_ccntr },
8521466b
AF
1314 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1315 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1316 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b
AF
1317 .type = ARM_CP_IO,
1318 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1319#endif
8521466b
AF
1320 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1321 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1322 .writefn = pmccfiltr_write,
8521466b
AF
1323 .access = PL0_RW, .accessfn = pmreg_access,
1324 .type = ARM_CP_IO,
1325 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1326 .resetvalue = 0, },
200ac0ef 1327 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
fdb86656
WH
1328 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1329 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1330 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1331 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1332 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1333 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
fcd25206 1334 /* Unimplemented, RAZ/WI. */
200ac0ef 1335 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206 1336 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
6ecd0b6b 1337 .accessfn = pmreg_access_xevcntr },
200ac0ef 1338 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1339 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1340 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1341 .resetvalue = 0,
d4e6df63 1342 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1343 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1344 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1345 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1346 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1347 .resetvalue = 0,
1348 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1349 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1350 .access = PL1_RW, .accessfn = access_tpm,
e6ec5457
WH
1351 .type = ARM_CP_ALIAS,
1352 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 1353 .resetvalue = 0,
d4e6df63 1354 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
1355 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1356 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1357 .access = PL1_RW, .accessfn = access_tpm,
1358 .type = ARM_CP_IO,
1359 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1360 .writefn = pmintenset_write, .raw_writefn = raw_write,
1361 .resetvalue = 0x0 },
200ac0ef 1362 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1363 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1364 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1365 .writefn = pmintenclr_write, },
978364f1
AF
1366 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1367 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1368 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1369 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1370 .writefn = pmintenclr_write },
7da845b0
PM
1371 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1372 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1373 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1374 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1375 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1376 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1377 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1378 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1379 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1380 * just RAZ for all cores:
1381 */
0ff644a7
PM
1382 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1383 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1384 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1385 /* Auxiliary fault status registers: these also are IMPDEF, and we
1386 * choose to RAZ/WI for all cores.
1387 */
1388 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1389 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1390 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1391 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1392 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1393 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1394 /* MAIR can just read-as-written because we don't implement caches
1395 * and so don't need to care about memory attributes.
1396 */
1397 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1398 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1399 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1400 .resetvalue = 0 },
4cfb8ad8
PM
1401 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1402 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1403 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1404 .resetvalue = 0 },
b0fe2427
PM
1405 /* For non-long-descriptor page tables these are PRRR and NMRR;
1406 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1407 */
1281f8e3 1408 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1409 * allows them to assign the correct fieldoffset based on the endianness
1410 * handled in the field definitions.
1411 */
a903c449 1412 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1413 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1414 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1415 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1416 .resetfn = arm_cp_reset_ignore },
a903c449 1417 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1418 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1419 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1420 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1421 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1422 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1423 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1424 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1425 /* 32 bit ITLB invalidates */
1426 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1427 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1428 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1429 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1430 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1431 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1432 /* 32 bit DTLB invalidates */
1433 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1434 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1435 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1436 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1437 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1438 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1439 /* 32 bit TLB invalidates */
1440 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1441 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1442 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1443 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1444 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1445 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1446 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1447 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1448 REGINFO_SENTINEL
1449};
1450
1451static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1452 /* 32 bit TLB invalidates, Inner Shareable */
1453 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1454 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1455 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1456 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1457 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1458 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1459 .writefn = tlbiasid_is_write },
995939a6 1460 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1461 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1462 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1463 REGINFO_SENTINEL
1464};
1465
c4241c7d
PM
1466static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1467 uint64_t value)
c326b979
PM
1468{
1469 value &= 1;
1470 env->teecr = value;
c326b979
PM
1471}
1472
3f208fd7
PM
1473static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1474 bool isread)
c326b979 1475{
dcbff19b 1476 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1477 return CP_ACCESS_TRAP;
c326b979 1478 }
92611c00 1479 return CP_ACCESS_OK;
c326b979
PM
1480}
1481
1482static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1483 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1484 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1485 .resetvalue = 0,
1486 .writefn = teecr_write },
1487 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1488 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1489 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1490 REGINFO_SENTINEL
1491};
1492
4d31c596 1493static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1494 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1495 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1496 .access = PL0_RW,
54bf36ed 1497 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1498 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1499 .access = PL0_RW,
54bf36ed
FA
1500 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1501 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1502 .resetfn = arm_cp_reset_ignore },
1503 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1504 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1505 .access = PL0_R|PL1_W,
54bf36ed
FA
1506 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1507 .resetvalue = 0},
4d31c596
PM
1508 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1509 .access = PL0_R|PL1_W,
54bf36ed
FA
1510 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1511 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1512 .resetfn = arm_cp_reset_ignore },
54bf36ed 1513 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1514 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1515 .access = PL1_RW,
54bf36ed
FA
1516 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1517 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1518 .access = PL1_RW,
1519 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1520 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1521 .resetvalue = 0 },
4d31c596
PM
1522 REGINFO_SENTINEL
1523};
1524
55d284af
PM
1525#ifndef CONFIG_USER_ONLY
1526
3f208fd7
PM
1527static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1528 bool isread)
00108f2d 1529{
75502672
PM
1530 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1531 * Writable only at the highest implemented exception level.
1532 */
1533 int el = arm_current_el(env);
1534
1535 switch (el) {
1536 case 0:
1537 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1538 return CP_ACCESS_TRAP;
1539 }
1540 break;
1541 case 1:
1542 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1543 arm_is_secure_below_el3(env)) {
1544 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1545 return CP_ACCESS_TRAP_UNCATEGORIZED;
1546 }
1547 break;
1548 case 2:
1549 case 3:
1550 break;
00108f2d 1551 }
75502672
PM
1552
1553 if (!isread && el < arm_highest_el(env)) {
1554 return CP_ACCESS_TRAP_UNCATEGORIZED;
1555 }
1556
00108f2d
PM
1557 return CP_ACCESS_OK;
1558}
1559
3f208fd7
PM
1560static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1561 bool isread)
00108f2d 1562{
0b6440af
EI
1563 unsigned int cur_el = arm_current_el(env);
1564 bool secure = arm_is_secure(env);
1565
00108f2d 1566 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1567 if (cur_el == 0 &&
00108f2d
PM
1568 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1569 return CP_ACCESS_TRAP;
1570 }
0b6440af
EI
1571
1572 if (arm_feature(env, ARM_FEATURE_EL2) &&
1573 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1574 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1575 return CP_ACCESS_TRAP_EL2;
1576 }
00108f2d
PM
1577 return CP_ACCESS_OK;
1578}
1579
3f208fd7
PM
1580static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1581 bool isread)
00108f2d 1582{
0b6440af
EI
1583 unsigned int cur_el = arm_current_el(env);
1584 bool secure = arm_is_secure(env);
1585
00108f2d
PM
1586 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1587 * EL0[PV]TEN is zero.
1588 */
0b6440af 1589 if (cur_el == 0 &&
00108f2d
PM
1590 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1591 return CP_ACCESS_TRAP;
1592 }
0b6440af
EI
1593
1594 if (arm_feature(env, ARM_FEATURE_EL2) &&
1595 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1596 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1597 return CP_ACCESS_TRAP_EL2;
1598 }
00108f2d
PM
1599 return CP_ACCESS_OK;
1600}
1601
1602static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1603 const ARMCPRegInfo *ri,
1604 bool isread)
00108f2d 1605{
3f208fd7 1606 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1607}
1608
1609static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1610 const ARMCPRegInfo *ri,
1611 bool isread)
00108f2d 1612{
3f208fd7 1613 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1614}
1615
3f208fd7
PM
1616static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1617 bool isread)
00108f2d 1618{
3f208fd7 1619 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1620}
1621
3f208fd7
PM
1622static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1623 bool isread)
00108f2d 1624{
3f208fd7 1625 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1626}
1627
b4d3978c 1628static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1629 const ARMCPRegInfo *ri,
1630 bool isread)
b4d3978c
PM
1631{
1632 /* The AArch64 register view of the secure physical timer is
1633 * always accessible from EL3, and configurably accessible from
1634 * Secure EL1.
1635 */
1636 switch (arm_current_el(env)) {
1637 case 1:
1638 if (!arm_is_secure(env)) {
1639 return CP_ACCESS_TRAP;
1640 }
1641 if (!(env->cp15.scr_el3 & SCR_ST)) {
1642 return CP_ACCESS_TRAP_EL3;
1643 }
1644 return CP_ACCESS_OK;
1645 case 0:
1646 case 2:
1647 return CP_ACCESS_TRAP;
1648 case 3:
1649 return CP_ACCESS_OK;
1650 default:
1651 g_assert_not_reached();
1652 }
1653}
1654
55d284af
PM
1655static uint64_t gt_get_countervalue(CPUARMState *env)
1656{
bc72ad67 1657 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1658}
1659
1660static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1661{
1662 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1663
1664 if (gt->ctl & 1) {
1665 /* Timer enabled: calculate and set current ISTATUS, irq, and
1666 * reset timer to when ISTATUS next has to change
1667 */
edac4d8a
EI
1668 uint64_t offset = timeridx == GTIMER_VIRT ?
1669 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1670 uint64_t count = gt_get_countervalue(&cpu->env);
1671 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1672 int istatus = count - offset >= gt->cval;
55d284af 1673 uint64_t nexttick;
194cbc49 1674 int irqstate;
55d284af
PM
1675
1676 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
1677
1678 irqstate = (istatus && !(gt->ctl & 2));
1679 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1680
55d284af
PM
1681 if (istatus) {
1682 /* Next transition is when count rolls back over to zero */
1683 nexttick = UINT64_MAX;
1684 } else {
1685 /* Next transition is when we hit cval */
edac4d8a 1686 nexttick = gt->cval + offset;
55d284af
PM
1687 }
1688 /* Note that the desired next expiry time might be beyond the
1689 * signed-64-bit range of a QEMUTimer -- in this case we just
1690 * set the timer for as far in the future as possible. When the
1691 * timer expires we will reset the timer for any remaining period.
1692 */
1693 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1694 nexttick = INT64_MAX / GTIMER_SCALE;
1695 }
bc72ad67 1696 timer_mod(cpu->gt_timer[timeridx], nexttick);
194cbc49 1697 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
1698 } else {
1699 /* Timer disabled: ISTATUS and timer output always clear */
1700 gt->ctl &= ~4;
1701 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1702 timer_del(cpu->gt_timer[timeridx]);
194cbc49 1703 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
1704 }
1705}
1706
0e3eca4c
EI
1707static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1708 int timeridx)
55d284af
PM
1709{
1710 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1711
bc72ad67 1712 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1713}
1714
c4241c7d 1715static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1716{
c4241c7d 1717 return gt_get_countervalue(env);
55d284af
PM
1718}
1719
edac4d8a
EI
1720static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1721{
1722 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1723}
1724
c4241c7d 1725static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1726 int timeridx,
c4241c7d 1727 uint64_t value)
55d284af 1728{
194cbc49 1729 trace_arm_gt_cval_write(timeridx, value);
55d284af
PM
1730 env->cp15.c14_timer[timeridx].cval = value;
1731 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1732}
c4241c7d 1733
0e3eca4c
EI
1734static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1735 int timeridx)
55d284af 1736{
edac4d8a 1737 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1738
c4241c7d 1739 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1740 (gt_get_countervalue(env) - offset));
55d284af
PM
1741}
1742
c4241c7d 1743static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1744 int timeridx,
c4241c7d 1745 uint64_t value)
55d284af 1746{
edac4d8a 1747 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1748
194cbc49 1749 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 1750 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1751 sextract64(value, 0, 32);
55d284af 1752 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1753}
1754
c4241c7d 1755static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1756 int timeridx,
c4241c7d 1757 uint64_t value)
55d284af
PM
1758{
1759 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1760 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1761
194cbc49 1762 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 1763 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1764 if ((oldval ^ value) & 1) {
1765 /* Enable toggled */
1766 gt_recalc_timer(cpu, timeridx);
d3afacc7 1767 } else if ((oldval ^ value) & 2) {
55d284af
PM
1768 /* IMASK toggled: don't need to recalculate,
1769 * just set the interrupt line based on ISTATUS
1770 */
194cbc49
PM
1771 int irqstate = (oldval & 4) && !(value & 2);
1772
1773 trace_arm_gt_imask_toggle(timeridx, irqstate);
1774 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 1775 }
55d284af
PM
1776}
1777
0e3eca4c
EI
1778static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1779{
1780 gt_timer_reset(env, ri, GTIMER_PHYS);
1781}
1782
1783static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1784 uint64_t value)
1785{
1786 gt_cval_write(env, ri, GTIMER_PHYS, value);
1787}
1788
1789static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1790{
1791 return gt_tval_read(env, ri, GTIMER_PHYS);
1792}
1793
1794static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1795 uint64_t value)
1796{
1797 gt_tval_write(env, ri, GTIMER_PHYS, value);
1798}
1799
1800static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1801 uint64_t value)
1802{
1803 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1804}
1805
1806static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1807{
1808 gt_timer_reset(env, ri, GTIMER_VIRT);
1809}
1810
1811static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1812 uint64_t value)
1813{
1814 gt_cval_write(env, ri, GTIMER_VIRT, value);
1815}
1816
1817static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1818{
1819 return gt_tval_read(env, ri, GTIMER_VIRT);
1820}
1821
1822static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1823 uint64_t value)
1824{
1825 gt_tval_write(env, ri, GTIMER_VIRT, value);
1826}
1827
1828static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1829 uint64_t value)
1830{
1831 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1832}
1833
edac4d8a
EI
1834static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1835 uint64_t value)
1836{
1837 ARMCPU *cpu = arm_env_get_cpu(env);
1838
194cbc49 1839 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
1840 raw_write(env, ri, value);
1841 gt_recalc_timer(cpu, GTIMER_VIRT);
1842}
1843
b0e66d95
EI
1844static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1845{
1846 gt_timer_reset(env, ri, GTIMER_HYP);
1847}
1848
1849static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1850 uint64_t value)
1851{
1852 gt_cval_write(env, ri, GTIMER_HYP, value);
1853}
1854
1855static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1856{
1857 return gt_tval_read(env, ri, GTIMER_HYP);
1858}
1859
1860static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1861 uint64_t value)
1862{
1863 gt_tval_write(env, ri, GTIMER_HYP, value);
1864}
1865
1866static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1867 uint64_t value)
1868{
1869 gt_ctl_write(env, ri, GTIMER_HYP, value);
1870}
1871
b4d3978c
PM
1872static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1873{
1874 gt_timer_reset(env, ri, GTIMER_SEC);
1875}
1876
1877static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1878 uint64_t value)
1879{
1880 gt_cval_write(env, ri, GTIMER_SEC, value);
1881}
1882
1883static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1884{
1885 return gt_tval_read(env, ri, GTIMER_SEC);
1886}
1887
1888static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1889 uint64_t value)
1890{
1891 gt_tval_write(env, ri, GTIMER_SEC, value);
1892}
1893
1894static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1895 uint64_t value)
1896{
1897 gt_ctl_write(env, ri, GTIMER_SEC, value);
1898}
1899
55d284af
PM
1900void arm_gt_ptimer_cb(void *opaque)
1901{
1902 ARMCPU *cpu = opaque;
1903
1904 gt_recalc_timer(cpu, GTIMER_PHYS);
1905}
1906
1907void arm_gt_vtimer_cb(void *opaque)
1908{
1909 ARMCPU *cpu = opaque;
1910
1911 gt_recalc_timer(cpu, GTIMER_VIRT);
1912}
1913
b0e66d95
EI
1914void arm_gt_htimer_cb(void *opaque)
1915{
1916 ARMCPU *cpu = opaque;
1917
1918 gt_recalc_timer(cpu, GTIMER_HYP);
1919}
1920
b4d3978c
PM
1921void arm_gt_stimer_cb(void *opaque)
1922{
1923 ARMCPU *cpu = opaque;
1924
1925 gt_recalc_timer(cpu, GTIMER_SEC);
1926}
1927
55d284af
PM
1928static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1929 /* Note that CNTFRQ is purely reads-as-written for the benefit
1930 * of software; writing it doesn't actually change the timer frequency.
1931 * Our reset value matches the fixed frequency we implement the timer at.
1932 */
1933 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1934 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1935 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1936 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1937 },
1938 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1939 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1940 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1941 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1942 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1943 },
1944 /* overall control: mostly access permissions */
a7adc4b7
PM
1945 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1946 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1947 .access = PL1_RW,
1948 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1949 .resetvalue = 0,
1950 },
1951 /* per-timer control */
1952 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1953 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1954 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1955 .accessfn = gt_ptimer_access,
1956 .fieldoffset = offsetoflow32(CPUARMState,
1957 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1958 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1959 },
9ff9dd3c
PM
1960 { .name = "CNTP_CTL(S)",
1961 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1962 .secure = ARM_CP_SECSTATE_S,
1963 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1964 .accessfn = gt_ptimer_access,
1965 .fieldoffset = offsetoflow32(CPUARMState,
1966 cp15.c14_timer[GTIMER_SEC].ctl),
1967 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1968 },
a7adc4b7
PM
1969 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1970 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1971 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1972 .accessfn = gt_ptimer_access,
55d284af
PM
1973 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1974 .resetvalue = 0,
0e3eca4c 1975 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1976 },
1977 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1978 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1979 .accessfn = gt_vtimer_access,
1980 .fieldoffset = offsetoflow32(CPUARMState,
1981 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1982 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1983 },
1984 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1985 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1986 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1987 .accessfn = gt_vtimer_access,
55d284af
PM
1988 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1989 .resetvalue = 0,
0e3eca4c 1990 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1991 },
1992 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1993 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 1994 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1995 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1996 .accessfn = gt_ptimer_access,
0e3eca4c 1997 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 1998 },
9ff9dd3c
PM
1999 { .name = "CNTP_TVAL(S)",
2000 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2001 .secure = ARM_CP_SECSTATE_S,
2002 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2003 .accessfn = gt_ptimer_access,
2004 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2005 },
a7adc4b7
PM
2006 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2007 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 2008 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2009 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2010 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2011 },
55d284af 2012 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 2013 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2014 .accessfn = gt_vtimer_access,
0e3eca4c 2015 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2016 },
a7adc4b7
PM
2017 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2018 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 2019 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2020 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2021 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2022 },
55d284af
PM
2023 /* The counter itself */
2024 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2025 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2026 .accessfn = gt_pct_access,
a7adc4b7
PM
2027 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2028 },
2029 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2030 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2031 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2032 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2033 },
2034 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2035 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2036 .accessfn = gt_vct_access,
edac4d8a 2037 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2038 },
2039 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2040 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2041 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2042 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2043 },
2044 /* Comparison value, indicating when the timer goes off */
2045 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2046 .secure = ARM_CP_SECSTATE_NS,
55d284af 2047 .access = PL1_RW | PL0_R,
7a0e58fa 2048 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2049 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2050 .accessfn = gt_ptimer_access,
0e3eca4c 2051 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2052 },
9ff9dd3c
PM
2053 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
2054 .secure = ARM_CP_SECSTATE_S,
2055 .access = PL1_RW | PL0_R,
2056 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2057 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2058 .accessfn = gt_ptimer_access,
2059 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2060 },
a7adc4b7
PM
2061 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2062 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2063 .access = PL1_RW | PL0_R,
2064 .type = ARM_CP_IO,
2065 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2066 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2067 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2068 },
2069 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2070 .access = PL1_RW | PL0_R,
7a0e58fa 2071 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2072 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2073 .accessfn = gt_vtimer_access,
0e3eca4c 2074 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2075 },
2076 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2077 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2078 .access = PL1_RW | PL0_R,
2079 .type = ARM_CP_IO,
2080 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2081 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2082 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2083 },
b4d3978c
PM
2084 /* Secure timer -- this is actually restricted to only EL3
2085 * and configurably Secure-EL1 via the accessfn.
2086 */
2087 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2088 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2089 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2090 .accessfn = gt_stimer_access,
2091 .readfn = gt_sec_tval_read,
2092 .writefn = gt_sec_tval_write,
2093 .resetfn = gt_sec_timer_reset,
2094 },
2095 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2096 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2097 .type = ARM_CP_IO, .access = PL1_RW,
2098 .accessfn = gt_stimer_access,
2099 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2100 .resetvalue = 0,
2101 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2102 },
2103 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2104 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2105 .type = ARM_CP_IO, .access = PL1_RW,
2106 .accessfn = gt_stimer_access,
2107 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2108 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2109 },
55d284af
PM
2110 REGINFO_SENTINEL
2111};
2112
2113#else
2114/* In user-mode none of the generic timer registers are accessible,
bc72ad67 2115 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
2116 * so instead just don't register any of them.
2117 */
6cc7a3ae 2118static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
2119 REGINFO_SENTINEL
2120};
2121
55d284af
PM
2122#endif
2123
c4241c7d 2124static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2125{
891a2fe7 2126 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2127 raw_write(env, ri, value);
891a2fe7 2128 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2129 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2130 } else {
8d5c773e 2131 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2132 }
4a501606
PM
2133}
2134
2135#ifndef CONFIG_USER_ONLY
2136/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2137
3f208fd7
PM
2138static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2139 bool isread)
92611c00
PM
2140{
2141 if (ri->opc2 & 4) {
87562e4f
PM
2142 /* The ATS12NSO* operations must trap to EL3 if executed in
2143 * Secure EL1 (which can only happen if EL3 is AArch64).
2144 * They are simply UNDEF if executed from NS EL1.
2145 * They function normally from EL2 or EL3.
92611c00 2146 */
87562e4f
PM
2147 if (arm_current_el(env) == 1) {
2148 if (arm_is_secure_below_el3(env)) {
2149 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2150 }
2151 return CP_ACCESS_TRAP_UNCATEGORIZED;
2152 }
92611c00
PM
2153 }
2154 return CP_ACCESS_OK;
2155}
2156
060e8a48 2157static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 2158 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 2159{
a8170e5e 2160 hwaddr phys_addr;
4a501606
PM
2161 target_ulong page_size;
2162 int prot;
b7cc4e82
PC
2163 uint32_t fsr;
2164 bool ret;
01c097f7 2165 uint64_t par64;
8bf5b6a9 2166 MemTxAttrs attrs = {};
e14b5a23 2167 ARMMMUFaultInfo fi = {};
5b2d261d 2168 ARMCacheAttrs cacheattrs = {};
4a501606 2169
5b2d261d
AB
2170 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
2171 &prot, &page_size, &fsr, &fi, &cacheattrs);
50cd71b0 2172 if (arm_s1_regime_using_lpae_format(env, mmu_idx)) {
b7cc4e82 2173 /* fsr is a DFSR/IFSR value for the long descriptor
702a9357
PM
2174 * translation table format, but with WnR always clear.
2175 * Convert it to a 64-bit PAR.
2176 */
01c097f7 2177 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 2178 if (!ret) {
702a9357 2179 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
2180 if (!attrs.secure) {
2181 par64 |= (1 << 9); /* NS */
2182 }
5b2d261d
AB
2183 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2184 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 2185 } else {
702a9357 2186 par64 |= 1; /* F */
b7cc4e82 2187 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
2188 /* Note that S2WLK and FSTAGE are always zero, because we don't
2189 * implement virtualization and therefore there can't be a stage 2
2190 * fault.
2191 */
4a501606
PM
2192 }
2193 } else {
b7cc4e82 2194 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
2195 * translation table format (with WnR always clear).
2196 * Convert it to a 32-bit PAR.
2197 */
b7cc4e82 2198 if (!ret) {
702a9357
PM
2199 /* We do not set any attribute bits in the PAR */
2200 if (page_size == (1 << 24)
2201 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 2202 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 2203 } else {
01c097f7 2204 par64 = phys_addr & 0xfffff000;
702a9357 2205 }
8bf5b6a9
PM
2206 if (!attrs.secure) {
2207 par64 |= (1 << 9); /* NS */
2208 }
702a9357 2209 } else {
b7cc4e82
PC
2210 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2211 ((fsr & 0xf) << 1) | 1;
702a9357 2212 }
4a501606 2213 }
060e8a48
PM
2214 return par64;
2215}
2216
2217static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2218{
03ae85f8 2219 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 2220 uint64_t par64;
d3649702
PM
2221 ARMMMUIdx mmu_idx;
2222 int el = arm_current_el(env);
2223 bool secure = arm_is_secure_below_el3(env);
060e8a48 2224
d3649702
PM
2225 switch (ri->opc2 & 6) {
2226 case 0:
2227 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2228 switch (el) {
2229 case 3:
2230 mmu_idx = ARMMMUIdx_S1E3;
2231 break;
2232 case 2:
2233 mmu_idx = ARMMMUIdx_S1NSE1;
2234 break;
2235 case 1:
2236 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2237 break;
2238 default:
2239 g_assert_not_reached();
2240 }
2241 break;
2242 case 2:
2243 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2244 switch (el) {
2245 case 3:
2246 mmu_idx = ARMMMUIdx_S1SE0;
2247 break;
2248 case 2:
2249 mmu_idx = ARMMMUIdx_S1NSE0;
2250 break;
2251 case 1:
2252 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2253 break;
2254 default:
2255 g_assert_not_reached();
2256 }
2257 break;
2258 case 4:
2259 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2260 mmu_idx = ARMMMUIdx_S12NSE1;
2261 break;
2262 case 6:
2263 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2264 mmu_idx = ARMMMUIdx_S12NSE0;
2265 break;
2266 default:
2267 g_assert_not_reached();
2268 }
2269
2270 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2271
2272 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2273}
060e8a48 2274
14db7fe0
PM
2275static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2276 uint64_t value)
2277{
03ae85f8 2278 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
2279 uint64_t par64;
2280
2281 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2282
2283 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2284}
2285
3f208fd7
PM
2286static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2287 bool isread)
2a47df95
PM
2288{
2289 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2290 return CP_ACCESS_TRAP;
2291 }
2292 return CP_ACCESS_OK;
2293}
2294
060e8a48
PM
2295static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2296 uint64_t value)
2297{
03ae85f8 2298 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
2299 ARMMMUIdx mmu_idx;
2300 int secure = arm_is_secure_below_el3(env);
2301
2302 switch (ri->opc2 & 6) {
2303 case 0:
2304 switch (ri->opc1) {
2305 case 0: /* AT S1E1R, AT S1E1W */
2306 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2307 break;
2308 case 4: /* AT S1E2R, AT S1E2W */
2309 mmu_idx = ARMMMUIdx_S1E2;
2310 break;
2311 case 6: /* AT S1E3R, AT S1E3W */
2312 mmu_idx = ARMMMUIdx_S1E3;
2313 break;
2314 default:
2315 g_assert_not_reached();
2316 }
2317 break;
2318 case 2: /* AT S1E0R, AT S1E0W */
2319 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2320 break;
2321 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2322 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2323 break;
2324 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2325 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2326 break;
2327 default:
2328 g_assert_not_reached();
2329 }
060e8a48 2330
d3649702 2331 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2332}
4a501606
PM
2333#endif
2334
2335static const ARMCPRegInfo vapa_cp_reginfo[] = {
2336 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2337 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2338 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2339 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2340 .writefn = par_write },
2341#ifndef CONFIG_USER_ONLY
87562e4f 2342 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2343 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2344 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2345 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2346#endif
2347 REGINFO_SENTINEL
2348};
2349
18032bec
PM
2350/* Return basic MPU access permission bits. */
2351static uint32_t simple_mpu_ap_bits(uint32_t val)
2352{
2353 uint32_t ret;
2354 uint32_t mask;
2355 int i;
2356 ret = 0;
2357 mask = 3;
2358 for (i = 0; i < 16; i += 2) {
2359 ret |= (val >> i) & mask;
2360 mask <<= 2;
2361 }
2362 return ret;
2363}
2364
2365/* Pad basic MPU access permission bits to extended format. */
2366static uint32_t extended_mpu_ap_bits(uint32_t val)
2367{
2368 uint32_t ret;
2369 uint32_t mask;
2370 int i;
2371 ret = 0;
2372 mask = 3;
2373 for (i = 0; i < 16; i += 2) {
2374 ret |= (val & mask) << i;
2375 mask <<= 2;
2376 }
2377 return ret;
2378}
2379
c4241c7d
PM
2380static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2381 uint64_t value)
18032bec 2382{
7e09797c 2383 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2384}
2385
c4241c7d 2386static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2387{
7e09797c 2388 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2389}
2390
c4241c7d
PM
2391static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2392 uint64_t value)
18032bec 2393{
7e09797c 2394 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2395}
2396
c4241c7d 2397static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2398{
7e09797c 2399 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2400}
2401
6cb0b013
PC
2402static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2403{
2404 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2405
2406 if (!u32p) {
2407 return 0;
2408 }
2409
1bc04a88 2410 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
2411 return *u32p;
2412}
2413
2414static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2415 uint64_t value)
2416{
2417 ARMCPU *cpu = arm_env_get_cpu(env);
2418 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2419
2420 if (!u32p) {
2421 return;
2422 }
2423
1bc04a88 2424 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 2425 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
2426 *u32p = value;
2427}
2428
6cb0b013
PC
2429static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2430 uint64_t value)
2431{
2432 ARMCPU *cpu = arm_env_get_cpu(env);
2433 uint32_t nrgs = cpu->pmsav7_dregion;
2434
2435 if (value >= nrgs) {
2436 qemu_log_mask(LOG_GUEST_ERROR,
2437 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2438 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2439 return;
2440 }
2441
2442 raw_write(env, ri, value);
2443}
2444
2445static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
2446 /* Reset for all these registers is handled in arm_cpu_reset(),
2447 * because the PMSAv7 is also used by M-profile CPUs, which do
2448 * not register cpregs but still need the state to be reset.
2449 */
6cb0b013
PC
2450 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2451 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2452 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
2453 .readfn = pmsav7_read, .writefn = pmsav7_write,
2454 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2455 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2456 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2457 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
2458 .readfn = pmsav7_read, .writefn = pmsav7_write,
2459 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2460 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2461 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2462 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
2463 .readfn = pmsav7_read, .writefn = pmsav7_write,
2464 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2465 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2466 .access = PL1_RW,
1bc04a88 2467 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
2468 .writefn = pmsav7_rgnr_write,
2469 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2470 REGINFO_SENTINEL
2471};
2472
18032bec
PM
2473static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2474 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2475 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2476 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2477 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2478 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2479 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2480 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2481 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2482 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2483 .access = PL1_RW,
7e09797c
PM
2484 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2485 .resetvalue = 0, },
18032bec
PM
2486 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2487 .access = PL1_RW,
7e09797c
PM
2488 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2489 .resetvalue = 0, },
ecce5c3c
PM
2490 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2491 .access = PL1_RW,
2492 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2493 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2494 .access = PL1_RW,
2495 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2496 /* Protection region base and size registers */
e508a92b
PM
2497 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2498 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2499 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2500 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2501 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2502 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2503 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2504 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2505 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2506 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2507 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2508 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2509 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2510 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2511 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2512 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2513 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2514 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2515 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2516 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2517 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2518 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2519 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2520 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2521 REGINFO_SENTINEL
2522};
2523
c4241c7d
PM
2524static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2525 uint64_t value)
ecce5c3c 2526{
11f136ee 2527 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2528 int maskshift = extract32(value, 0, 3);
2529
e389be16
FA
2530 if (!arm_feature(env, ARM_FEATURE_V8)) {
2531 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2532 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2533 * using Long-desciptor translation table format */
2534 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2535 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2536 /* In an implementation that includes the Security Extensions
2537 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2538 * Short-descriptor translation table format.
2539 */
2540 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2541 } else {
2542 value &= TTBCR_N;
2543 }
e42c4db3 2544 }
e389be16 2545
b6af0975 2546 /* Update the masks corresponding to the TCR bank being written
11f136ee 2547 * Note that we always calculate mask and base_mask, but
e42c4db3 2548 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2549 * for long-descriptor tables the TCR fields are used differently
2550 * and the mask and base_mask values are meaningless.
e42c4db3 2551 */
11f136ee
FA
2552 tcr->raw_tcr = value;
2553 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2554 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2555}
2556
c4241c7d
PM
2557static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2558 uint64_t value)
d4e6df63 2559{
00c8cb0a
AF
2560 ARMCPU *cpu = arm_env_get_cpu(env);
2561
d4e6df63
PM
2562 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2563 /* With LPAE the TTBCR could result in a change of ASID
2564 * via the TTBCR.A1 bit, so do a TLB flush.
2565 */
d10eb08f 2566 tlb_flush(CPU(cpu));
d4e6df63 2567 }
c4241c7d 2568 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2569}
2570
ecce5c3c
PM
2571static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2572{
11f136ee
FA
2573 TCR *tcr = raw_ptr(env, ri);
2574
2575 /* Reset both the TCR as well as the masks corresponding to the bank of
2576 * the TCR being reset.
2577 */
2578 tcr->raw_tcr = 0;
2579 tcr->mask = 0;
2580 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2581}
2582
cb2e37df
PM
2583static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2584 uint64_t value)
2585{
00c8cb0a 2586 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2587 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2588
cb2e37df 2589 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 2590 tlb_flush(CPU(cpu));
11f136ee 2591 tcr->raw_tcr = value;
cb2e37df
PM
2592}
2593
327ed10f
PM
2594static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2595 uint64_t value)
2596{
2597 /* 64 bit accesses to the TTBRs can change the ASID and so we
2598 * must flush the TLB.
2599 */
2600 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2601 ARMCPU *cpu = arm_env_get_cpu(env);
2602
d10eb08f 2603 tlb_flush(CPU(cpu));
327ed10f
PM
2604 }
2605 raw_write(env, ri, value);
2606}
2607
b698e9cf
EI
2608static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2609 uint64_t value)
2610{
2611 ARMCPU *cpu = arm_env_get_cpu(env);
2612 CPUState *cs = CPU(cpu);
2613
2614 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2615 if (raw_read(env, ri) != value) {
0336cbf8 2616 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2617 ARMMMUIdxBit_S12NSE1 |
2618 ARMMMUIdxBit_S12NSE0 |
2619 ARMMMUIdxBit_S2NS);
b698e9cf
EI
2620 raw_write(env, ri, value);
2621 }
2622}
2623
8e5d75c9 2624static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2625 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2626 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2627 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2628 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2629 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2630 .access = PL1_RW, .resetvalue = 0,
2631 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2632 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2633 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2634 .access = PL1_RW, .resetvalue = 0,
2635 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2636 offsetof(CPUARMState, cp15.dfar_ns) } },
2637 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2638 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2639 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2640 .resetvalue = 0, },
2641 REGINFO_SENTINEL
2642};
2643
2644static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2645 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2646 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2647 .access = PL1_RW,
d81c519c 2648 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2649 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2650 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2651 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2652 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2653 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2654 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2655 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2656 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2657 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2658 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2659 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2660 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2661 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2662 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2663 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2664 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2665 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2666 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2667 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2668 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2669 REGINFO_SENTINEL
2670};
2671
c4241c7d
PM
2672static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2673 uint64_t value)
1047b9d7
PM
2674{
2675 env->cp15.c15_ticonfig = value & 0xe7;
2676 /* The OS_TYPE bit in this register changes the reported CPUID! */
2677 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2678 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2679}
2680
c4241c7d
PM
2681static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2682 uint64_t value)
1047b9d7
PM
2683{
2684 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2685}
2686
c4241c7d
PM
2687static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2688 uint64_t value)
1047b9d7
PM
2689{
2690 /* Wait-for-interrupt (deprecated) */
c3affe56 2691 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2692}
2693
c4241c7d
PM
2694static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2695 uint64_t value)
c4804214
PM
2696{
2697 /* On OMAP there are registers indicating the max/min index of dcache lines
2698 * containing a dirty line; cache flush operations have to reset these.
2699 */
2700 env->cp15.c15_i_max = 0x000;
2701 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2702}
2703
18032bec
PM
2704static const ARMCPRegInfo omap_cp_reginfo[] = {
2705 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2706 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2707 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2708 .resetvalue = 0, },
1047b9d7
PM
2709 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2710 .access = PL1_RW, .type = ARM_CP_NOP },
2711 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2712 .access = PL1_RW,
2713 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2714 .writefn = omap_ticonfig_write },
2715 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2716 .access = PL1_RW,
2717 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2718 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2719 .access = PL1_RW, .resetvalue = 0xff0,
2720 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2721 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2722 .access = PL1_RW,
2723 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2724 .writefn = omap_threadid_write },
2725 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2726 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2727 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2728 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2729 /* TODO: Peripheral port remap register:
2730 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2731 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2732 * when MMU is off.
2733 */
c4804214 2734 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2735 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2736 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2737 .writefn = omap_cachemaint_write },
34f90529
PM
2738 { .name = "C9", .cp = 15, .crn = 9,
2739 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2740 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2741 REGINFO_SENTINEL
2742};
2743
c4241c7d
PM
2744static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2745 uint64_t value)
1047b9d7 2746{
c0f4af17 2747 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2748}
2749
2750static const ARMCPRegInfo xscale_cp_reginfo[] = {
2751 { .name = "XSCALE_CPAR",
2752 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2753 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2754 .writefn = xscale_cpar_write, },
2771db27
PM
2755 { .name = "XSCALE_AUXCR",
2756 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2757 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2758 .resetvalue = 0, },
3b771579
PM
2759 /* XScale specific cache-lockdown: since we have no cache we NOP these
2760 * and hope the guest does not really rely on cache behaviour.
2761 */
2762 { .name = "XSCALE_LOCK_ICACHE_LINE",
2763 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2764 .access = PL1_W, .type = ARM_CP_NOP },
2765 { .name = "XSCALE_UNLOCK_ICACHE",
2766 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2767 .access = PL1_W, .type = ARM_CP_NOP },
2768 { .name = "XSCALE_DCACHE_LOCK",
2769 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2770 .access = PL1_RW, .type = ARM_CP_NOP },
2771 { .name = "XSCALE_UNLOCK_DCACHE",
2772 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2773 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2774 REGINFO_SENTINEL
2775};
2776
2777static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2778 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2779 * implementation of this implementation-defined space.
2780 * Ideally this should eventually disappear in favour of actually
2781 * implementing the correct behaviour for all cores.
2782 */
2783 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2784 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2785 .access = PL1_RW,
7a0e58fa 2786 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2787 .resetvalue = 0 },
18032bec
PM
2788 REGINFO_SENTINEL
2789};
2790
c4804214
PM
2791static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2792 /* Cache status: RAZ because we have no cache so it's always clean */
2793 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2794 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2795 .resetvalue = 0 },
c4804214
PM
2796 REGINFO_SENTINEL
2797};
2798
2799static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2800 /* We never have a a block transfer operation in progress */
2801 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2802 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2803 .resetvalue = 0 },
30b05bba
PM
2804 /* The cache ops themselves: these all NOP for QEMU */
2805 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2806 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2807 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2808 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2809 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2810 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2811 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2812 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2813 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2814 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2815 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2816 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2817 REGINFO_SENTINEL
2818};
2819
2820static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2821 /* The cache test-and-clean instructions always return (1 << 30)
2822 * to indicate that there are no dirty cache lines.
2823 */
2824 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2825 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2826 .resetvalue = (1 << 30) },
c4804214 2827 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2828 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2829 .resetvalue = (1 << 30) },
c4804214
PM
2830 REGINFO_SENTINEL
2831};
2832
34f90529
PM
2833static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2834 /* Ignore ReadBuffer accesses */
2835 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2836 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2837 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2838 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2839 REGINFO_SENTINEL
2840};
2841
731de9e6
EI
2842static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2843{
2844 ARMCPU *cpu = arm_env_get_cpu(env);
2845 unsigned int cur_el = arm_current_el(env);
2846 bool secure = arm_is_secure(env);
2847
2848 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2849 return env->cp15.vpidr_el2;
2850 }
2851 return raw_read(env, ri);
2852}
2853
06a7e647 2854static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2855{
eb5e1d3c
PF
2856 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2857 uint64_t mpidr = cpu->mp_affinity;
2858
81bdde9d 2859 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2860 mpidr |= (1U << 31);
81bdde9d
PM
2861 /* Cores which are uniprocessor (non-coherent)
2862 * but still implement the MP extensions set
a8e81b31 2863 * bit 30. (For instance, Cortex-R5).
81bdde9d 2864 */
a8e81b31
PC
2865 if (cpu->mp_is_up) {
2866 mpidr |= (1u << 30);
2867 }
81bdde9d 2868 }
c4241c7d 2869 return mpidr;
81bdde9d
PM
2870}
2871
06a7e647
EI
2872static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2873{
f0d574d6
EI
2874 unsigned int cur_el = arm_current_el(env);
2875 bool secure = arm_is_secure(env);
2876
2877 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2878 return env->cp15.vmpidr_el2;
2879 }
06a7e647
EI
2880 return mpidr_read_val(env);
2881}
2882
81bdde9d 2883static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2884 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2885 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2886 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2887 REGINFO_SENTINEL
2888};
2889
7ac681cf 2890static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2891 /* NOP AMAIR0/1 */
b0fe2427
PM
2892 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2893 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2894 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2895 .resetvalue = 0 },
b0fe2427 2896 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2897 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2898 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2899 .resetvalue = 0 },
891a2fe7 2900 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2901 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2902 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2903 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2904 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2905 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2906 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2907 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2908 .writefn = vmsa_ttbr_write, },
891a2fe7 2909 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2910 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2911 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2912 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2913 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2914 REGINFO_SENTINEL
2915};
2916
c4241c7d 2917static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2918{
c4241c7d 2919 return vfp_get_fpcr(env);
b0d2b7d0
PM
2920}
2921
c4241c7d
PM
2922static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2923 uint64_t value)
b0d2b7d0
PM
2924{
2925 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2926}
2927
c4241c7d 2928static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2929{
c4241c7d 2930 return vfp_get_fpsr(env);
b0d2b7d0
PM
2931}
2932
c4241c7d
PM
2933static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2934 uint64_t value)
b0d2b7d0
PM
2935{
2936 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2937}
2938
3f208fd7
PM
2939static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2940 bool isread)
c2b820fe 2941{
137feaa9 2942 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2943 return CP_ACCESS_TRAP;
2944 }
2945 return CP_ACCESS_OK;
2946}
2947
2948static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2949 uint64_t value)
2950{
2951 env->daif = value & PSTATE_DAIF;
2952}
2953
8af35c37 2954static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
2955 const ARMCPRegInfo *ri,
2956 bool isread)
8af35c37
PM
2957{
2958 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2959 * SCTLR_EL1.UCI is set.
2960 */
137feaa9 2961 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2962 return CP_ACCESS_TRAP;
2963 }
2964 return CP_ACCESS_OK;
2965}
2966
dbb1fb27
AB
2967/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2968 * Page D4-1736 (DDI0487A.b)
2969 */
2970
fd3ed969
PM
2971static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972 uint64_t value)
168aa23b 2973{
a67cf277 2974 CPUState *cs = ENV_GET_CPU(env);
dbb1fb27 2975
fd3ed969 2976 if (arm_is_secure_below_el3(env)) {
0336cbf8 2977 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2978 ARMMMUIdxBit_S1SE1 |
2979 ARMMMUIdxBit_S1SE0);
fd3ed969 2980 } else {
0336cbf8 2981 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2982 ARMMMUIdxBit_S12NSE1 |
2983 ARMMMUIdxBit_S12NSE0);
fd3ed969 2984 }
168aa23b
PM
2985}
2986
fd3ed969
PM
2987static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2988 uint64_t value)
168aa23b 2989{
a67cf277 2990 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 2991 bool sec = arm_is_secure_below_el3(env);
dbb1fb27 2992
a67cf277
AB
2993 if (sec) {
2994 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
2995 ARMMMUIdxBit_S1SE1 |
2996 ARMMMUIdxBit_S1SE0);
a67cf277
AB
2997 } else {
2998 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
2999 ARMMMUIdxBit_S12NSE1 |
3000 ARMMMUIdxBit_S12NSE0);
fd3ed969 3001 }
168aa23b
PM
3002}
3003
fd3ed969
PM
3004static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005 uint64_t value)
168aa23b 3006{
fd3ed969
PM
3007 /* Note that the 'ALL' scope must invalidate both stage 1 and
3008 * stage 2 translations, whereas most other scopes only invalidate
3009 * stage 1 translations.
3010 */
00c8cb0a 3011 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
3012 CPUState *cs = CPU(cpu);
3013
3014 if (arm_is_secure_below_el3(env)) {
0336cbf8 3015 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3016 ARMMMUIdxBit_S1SE1 |
3017 ARMMMUIdxBit_S1SE0);
fd3ed969
PM
3018 } else {
3019 if (arm_feature(env, ARM_FEATURE_EL2)) {
0336cbf8 3020 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3021 ARMMMUIdxBit_S12NSE1 |
3022 ARMMMUIdxBit_S12NSE0 |
3023 ARMMMUIdxBit_S2NS);
fd3ed969 3024 } else {
0336cbf8 3025 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3026 ARMMMUIdxBit_S12NSE1 |
3027 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3028 }
3029 }
168aa23b
PM
3030}
3031
fd3ed969 3032static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
3033 uint64_t value)
3034{
fd3ed969
PM
3035 ARMCPU *cpu = arm_env_get_cpu(env);
3036 CPUState *cs = CPU(cpu);
3037
8bd5c820 3038 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3039}
3040
43efaa33
PM
3041static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3042 uint64_t value)
3043{
3044 ARMCPU *cpu = arm_env_get_cpu(env);
3045 CPUState *cs = CPU(cpu);
3046
8bd5c820 3047 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3048}
3049
fd3ed969
PM
3050static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3051 uint64_t value)
3052{
3053 /* Note that the 'ALL' scope must invalidate both stage 1 and
3054 * stage 2 translations, whereas most other scopes only invalidate
3055 * stage 1 translations.
3056 */
a67cf277 3057 CPUState *cs = ENV_GET_CPU(env);
fd3ed969
PM
3058 bool sec = arm_is_secure_below_el3(env);
3059 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
a67cf277
AB
3060
3061 if (sec) {
3062 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3063 ARMMMUIdxBit_S1SE1 |
3064 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3065 } else if (has_el2) {
3066 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3067 ARMMMUIdxBit_S12NSE1 |
3068 ARMMMUIdxBit_S12NSE0 |
3069 ARMMMUIdxBit_S2NS);
a67cf277
AB
3070 } else {
3071 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3072 ARMMMUIdxBit_S12NSE1 |
3073 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3074 }
3075}
3076
2bfb9d75
PM
3077static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078 uint64_t value)
3079{
a67cf277 3080 CPUState *cs = ENV_GET_CPU(env);
2bfb9d75 3081
8bd5c820 3082 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
2bfb9d75
PM
3083}
3084
43efaa33
PM
3085static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3086 uint64_t value)
3087{
a67cf277 3088 CPUState *cs = ENV_GET_CPU(env);
43efaa33 3089
8bd5c820 3090 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3091}
3092
fd3ed969
PM
3093static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3094 uint64_t value)
3095{
3096 /* Invalidate by VA, EL1&0 (AArch64 version).
3097 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3098 * since we don't support flush-for-specific-ASID-only or
3099 * flush-last-level-only.
3100 */
3101 ARMCPU *cpu = arm_env_get_cpu(env);
3102 CPUState *cs = CPU(cpu);
3103 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3104
3105 if (arm_is_secure_below_el3(env)) {
0336cbf8 3106 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3107 ARMMMUIdxBit_S1SE1 |
3108 ARMMMUIdxBit_S1SE0);
fd3ed969 3109 } else {
0336cbf8 3110 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3111 ARMMMUIdxBit_S12NSE1 |
3112 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3113 }
3114}
3115
3116static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3117 uint64_t value)
fa439fc5 3118{
fd3ed969
PM
3119 /* Invalidate by VA, EL2
3120 * Currently handles both VAE2 and VALE2, since we don't support
3121 * flush-last-level-only.
3122 */
3123 ARMCPU *cpu = arm_env_get_cpu(env);
3124 CPUState *cs = CPU(cpu);
3125 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3126
8bd5c820 3127 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3128}
3129
43efaa33
PM
3130static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3131 uint64_t value)
3132{
3133 /* Invalidate by VA, EL3
3134 * Currently handles both VAE3 and VALE3, since we don't support
3135 * flush-last-level-only.
3136 */
3137 ARMCPU *cpu = arm_env_get_cpu(env);
3138 CPUState *cs = CPU(cpu);
3139 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3140
8bd5c820 3141 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
43efaa33
PM
3142}
3143
fd3ed969
PM
3144static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3145 uint64_t value)
3146{
a67cf277
AB
3147 ARMCPU *cpu = arm_env_get_cpu(env);
3148 CPUState *cs = CPU(cpu);
fd3ed969 3149 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
3150 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3151
a67cf277
AB
3152 if (sec) {
3153 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3154 ARMMMUIdxBit_S1SE1 |
3155 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3156 } else {
3157 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3158 ARMMMUIdxBit_S12NSE1 |
3159 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3160 }
3161}
3162
fd3ed969
PM
3163static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3164 uint64_t value)
fa439fc5 3165{
a67cf277 3166 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3167 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 3168
a67cf277 3169 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3170 ARMMMUIdxBit_S1E2);
fa439fc5
PM
3171}
3172
43efaa33
PM
3173static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3174 uint64_t value)
3175{
a67cf277 3176 CPUState *cs = ENV_GET_CPU(env);
43efaa33
PM
3177 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3178
a67cf277 3179 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3180 ARMMMUIdxBit_S1E3);
43efaa33
PM
3181}
3182
cea66e91
PM
3183static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3184 uint64_t value)
3185{
3186 /* Invalidate by IPA. This has to invalidate any structures that
3187 * contain only stage 2 translation information, but does not need
3188 * to apply to structures that contain combined stage 1 and stage 2
3189 * translation information.
3190 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3191 */
3192 ARMCPU *cpu = arm_env_get_cpu(env);
3193 CPUState *cs = CPU(cpu);
3194 uint64_t pageaddr;
3195
3196 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3197 return;
3198 }
3199
3200 pageaddr = sextract64(value << 12, 0, 48);
3201
8bd5c820 3202 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
cea66e91
PM
3203}
3204
3205static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3206 uint64_t value)
3207{
a67cf277 3208 CPUState *cs = ENV_GET_CPU(env);
cea66e91
PM
3209 uint64_t pageaddr;
3210
3211 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3212 return;
3213 }
3214
3215 pageaddr = sextract64(value << 12, 0, 48);
3216
a67cf277 3217 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3218 ARMMMUIdxBit_S2NS);
cea66e91
PM
3219}
3220
3f208fd7
PM
3221static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3222 bool isread)
aca3f40b
PM
3223{
3224 /* We don't implement EL2, so the only control on DC ZVA is the
3225 * bit in the SCTLR which can prohibit access for EL0.
3226 */
137feaa9 3227 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3228 return CP_ACCESS_TRAP;
3229 }
3230 return CP_ACCESS_OK;
3231}
3232
3233static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3234{
3235 ARMCPU *cpu = arm_env_get_cpu(env);
3236 int dzp_bit = 1 << 4;
3237
3238 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3239 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3240 dzp_bit = 0;
3241 }
3242 return cpu->dcz_blocksize | dzp_bit;
3243}
3244
3f208fd7
PM
3245static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3246 bool isread)
f502cfc2 3247{
cdcf1405 3248 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3249 /* Access to SP_EL0 is undefined if it's being used as
3250 * the stack pointer.
3251 */
3252 return CP_ACCESS_TRAP_UNCATEGORIZED;
3253 }
3254 return CP_ACCESS_OK;
3255}
3256
3257static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3258{
3259 return env->pstate & PSTATE_SP;
3260}
3261
3262static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3263{
3264 update_spsel(env, val);
3265}
3266
137feaa9
FA
3267static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3268 uint64_t value)
3269{
3270 ARMCPU *cpu = arm_env_get_cpu(env);
3271
3272 if (raw_read(env, ri) == value) {
3273 /* Skip the TLB flush if nothing actually changed; Linux likes
3274 * to do a lot of pointless SCTLR writes.
3275 */
3276 return;
3277 }
3278
06312feb
PM
3279 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3280 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3281 value &= ~SCTLR_M;
3282 }
3283
137feaa9
FA
3284 raw_write(env, ri, value);
3285 /* ??? Lots of these bits are not implemented. */
3286 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 3287 tlb_flush(CPU(cpu));
137feaa9
FA
3288}
3289
3f208fd7
PM
3290static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3291 bool isread)
03fbf20f
PM
3292{
3293 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3294 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3295 }
3296 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3297 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3298 }
3299 return CP_ACCESS_OK;
3300}
3301
a8d64e73
PM
3302static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3303 uint64_t value)
3304{
3305 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3306}
3307
b0d2b7d0
PM
3308static const ARMCPRegInfo v8_cp_reginfo[] = {
3309 /* Minimal set of EL0-visible registers. This will need to be expanded
3310 * significantly for system emulation of AArch64 CPUs.
3311 */
3312 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3313 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3314 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3315 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3316 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3317 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3318 .access = PL0_RW, .accessfn = aa64_daif_access,
3319 .fieldoffset = offsetof(CPUARMState, daif),
3320 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3321 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3322 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3323 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3324 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3325 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3326 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3327 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3328 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3329 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3330 .readfn = aa64_dczid_read },
3331 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3332 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3333 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3334#ifndef CONFIG_USER_ONLY
3335 /* Avoid overhead of an access check that always passes in user-mode */
3336 .accessfn = aa64_zva_access,
3337#endif
3338 },
0eef9d98
PM
3339 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3340 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3341 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3342 /* Cache ops: all NOPs since we don't emulate caches */
3343 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3344 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3345 .access = PL1_W, .type = ARM_CP_NOP },
3346 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3347 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3348 .access = PL1_W, .type = ARM_CP_NOP },
3349 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3350 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3351 .access = PL0_W, .type = ARM_CP_NOP,
3352 .accessfn = aa64_cacheop_access },
3353 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3354 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3355 .access = PL1_W, .type = ARM_CP_NOP },
3356 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3357 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3358 .access = PL1_W, .type = ARM_CP_NOP },
3359 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3360 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3361 .access = PL0_W, .type = ARM_CP_NOP,
3362 .accessfn = aa64_cacheop_access },
3363 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3364 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3365 .access = PL1_W, .type = ARM_CP_NOP },
3366 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3367 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3368 .access = PL0_W, .type = ARM_CP_NOP,
3369 .accessfn = aa64_cacheop_access },
3370 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3372 .access = PL0_W, .type = ARM_CP_NOP,
3373 .accessfn = aa64_cacheop_access },
3374 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3375 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3376 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3377 /* TLBI operations */
3378 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3379 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3380 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3381 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3382 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3383 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3384 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3385 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3386 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3387 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3388 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3389 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3390 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3391 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3392 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3393 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3394 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3395 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3396 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3397 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3398 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3399 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3400 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3401 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3402 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3403 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3404 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3405 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3406 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3407 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3408 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3409 .writefn = tlbi_aa64_vae1_write },
168aa23b 3410 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3411 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3412 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3413 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3414 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3415 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3416 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3417 .writefn = tlbi_aa64_vae1_write },
168aa23b 3418 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3419 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3420 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3421 .writefn = tlbi_aa64_vae1_write },
168aa23b 3422 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3423 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3424 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3425 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3426 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3427 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3428 .access = PL2_W, .type = ARM_CP_NO_RAW,
3429 .writefn = tlbi_aa64_ipas2e1is_write },
3430 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3432 .access = PL2_W, .type = ARM_CP_NO_RAW,
3433 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3434 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3435 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3436 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3437 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3438 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3439 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3440 .access = PL2_W, .type = ARM_CP_NO_RAW,
3441 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3442 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3443 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3444 .access = PL2_W, .type = ARM_CP_NO_RAW,
3445 .writefn = tlbi_aa64_ipas2e1_write },
3446 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3447 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3448 .access = PL2_W, .type = ARM_CP_NO_RAW,
3449 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3450 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3451 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3452 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3453 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3454 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3455 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3456 .access = PL2_W, .type = ARM_CP_NO_RAW,
3457 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3458#ifndef CONFIG_USER_ONLY
3459 /* 64 bit address translation operations */
3460 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3461 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3462 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3463 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3464 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3465 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3466 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3467 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3468 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3469 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3470 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3471 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3472 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3473 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3474 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3475 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3476 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3477 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3478 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3479 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3480 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3481 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3482 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3483 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3484 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3485 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3486 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3487 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3488 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3489 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3490 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3491 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3492 .type = ARM_CP_ALIAS,
3493 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3494 .access = PL1_RW, .resetvalue = 0,
3495 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3496 .writefn = par_write },
19525524 3497#endif
995939a6 3498 /* TLB invalidate last level of translation table walk */
9449fdf6 3499 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3500 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3501 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3502 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3503 .writefn = tlbimvaa_is_write },
9449fdf6 3504 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3505 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3506 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3507 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
3508 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3509 .type = ARM_CP_NO_RAW, .access = PL2_W,
3510 .writefn = tlbimva_hyp_write },
3511 { .name = "TLBIMVALHIS",
3512 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3513 .type = ARM_CP_NO_RAW, .access = PL2_W,
3514 .writefn = tlbimva_hyp_is_write },
3515 { .name = "TLBIIPAS2",
3516 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3517 .type = ARM_CP_NO_RAW, .access = PL2_W,
3518 .writefn = tlbiipas2_write },
3519 { .name = "TLBIIPAS2IS",
3520 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3521 .type = ARM_CP_NO_RAW, .access = PL2_W,
3522 .writefn = tlbiipas2_is_write },
3523 { .name = "TLBIIPAS2L",
3524 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3525 .type = ARM_CP_NO_RAW, .access = PL2_W,
3526 .writefn = tlbiipas2_write },
3527 { .name = "TLBIIPAS2LIS",
3528 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3529 .type = ARM_CP_NO_RAW, .access = PL2_W,
3530 .writefn = tlbiipas2_is_write },
9449fdf6
PM
3531 /* 32 bit cache operations */
3532 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3533 .type = ARM_CP_NOP, .access = PL1_W },
3534 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3535 .type = ARM_CP_NOP, .access = PL1_W },
3536 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3537 .type = ARM_CP_NOP, .access = PL1_W },
3538 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3539 .type = ARM_CP_NOP, .access = PL1_W },
3540 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3541 .type = ARM_CP_NOP, .access = PL1_W },
3542 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3543 .type = ARM_CP_NOP, .access = PL1_W },
3544 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3545 .type = ARM_CP_NOP, .access = PL1_W },
3546 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3547 .type = ARM_CP_NOP, .access = PL1_W },
3548 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3549 .type = ARM_CP_NOP, .access = PL1_W },
3550 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3551 .type = ARM_CP_NOP, .access = PL1_W },
3552 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3553 .type = ARM_CP_NOP, .access = PL1_W },
3554 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3555 .type = ARM_CP_NOP, .access = PL1_W },
3556 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3557 .type = ARM_CP_NOP, .access = PL1_W },
3558 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3559 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3560 .access = PL1_RW, .resetvalue = 0,
3561 .writefn = dacr_write, .raw_writefn = raw_write,
3562 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3563 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3564 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3565 .type = ARM_CP_ALIAS,
a0618a19 3566 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3567 .access = PL1_RW,
3568 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3569 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3570 .type = ARM_CP_ALIAS,
a65f1de9 3571 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3572 .access = PL1_RW,
3573 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3574 /* We rely on the access checks not allowing the guest to write to the
3575 * state field when SPSel indicates that it's being used as the stack
3576 * pointer.
3577 */
3578 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3579 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3580 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3581 .type = ARM_CP_ALIAS,
f502cfc2 3582 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3583 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3584 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3585 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3586 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3587 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3588 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3589 .type = ARM_CP_NO_RAW,
f502cfc2 3590 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3591 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3592 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3593 .type = ARM_CP_ALIAS,
3594 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3595 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3596 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3597 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3598 .access = PL2_RW, .resetvalue = 0,
3599 .writefn = dacr_write, .raw_writefn = raw_write,
3600 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3601 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3602 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3603 .access = PL2_RW, .resetvalue = 0,
3604 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3605 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3606 .type = ARM_CP_ALIAS,
3607 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3608 .access = PL2_RW,
3609 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3610 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3611 .type = ARM_CP_ALIAS,
3612 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3613 .access = PL2_RW,
3614 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3615 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3616 .type = ARM_CP_ALIAS,
3617 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3618 .access = PL2_RW,
3619 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3620 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3621 .type = ARM_CP_ALIAS,
3622 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3623 .access = PL2_RW,
3624 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3625 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3626 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3627 .resetvalue = 0,
3628 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3629 { .name = "SDCR", .type = ARM_CP_ALIAS,
3630 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3631 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3632 .writefn = sdcr_write,
3633 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3634 REGINFO_SENTINEL
3635};
3636
d42e3c26 3637/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3638static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3639 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3640 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3641 .access = PL2_RW,
3642 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3643 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3644 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3645 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3646 .access = PL2_RW,
3647 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3648 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3649 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3650 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3651 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3652 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3653 .access = PL2_RW, .type = ARM_CP_CONST,
3654 .resetvalue = 0 },
3655 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3656 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3657 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3658 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3659 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3660 .access = PL2_RW, .type = ARM_CP_CONST,
3661 .resetvalue = 0 },
3662 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3663 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3664 .access = PL2_RW, .type = ARM_CP_CONST,
3665 .resetvalue = 0 },
37cd6c24
PM
3666 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3667 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3668 .access = PL2_RW, .type = ARM_CP_CONST,
3669 .resetvalue = 0 },
3670 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3671 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3672 .access = PL2_RW, .type = ARM_CP_CONST,
3673 .resetvalue = 0 },
06ec4c8c
EI
3674 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3675 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3676 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3677 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3678 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3679 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3680 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3681 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3682 .cp = 15, .opc1 = 6, .crm = 2,
3683 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3684 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3685 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3686 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3687 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3688 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3689 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3690 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3691 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3692 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3693 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3694 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3695 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3696 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3697 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3698 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3699 .resetvalue = 0 },
0b6440af
EI
3700 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3701 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3702 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3703 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3704 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3705 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3706 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3707 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3708 .resetvalue = 0 },
b0e66d95
EI
3709 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3710 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3711 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3712 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3713 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3714 .resetvalue = 0 },
3715 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3716 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3717 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3718 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3719 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3720 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3721 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3722 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3723 .access = PL2_RW, .accessfn = access_tda,
3724 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3725 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3726 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3727 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3728 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
3729 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3730 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3731 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3732 REGINFO_SENTINEL
3733};
3734
f149e3e8
EI
3735static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3736{
3737 ARMCPU *cpu = arm_env_get_cpu(env);
3738 uint64_t valid_mask = HCR_MASK;
3739
3740 if (arm_feature(env, ARM_FEATURE_EL3)) {
3741 valid_mask &= ~HCR_HCD;
77077a83
JK
3742 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3743 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3744 * However, if we're using the SMC PSCI conduit then QEMU is
3745 * effectively acting like EL3 firmware and so the guest at
3746 * EL2 should retain the ability to prevent EL1 from being
3747 * able to make SMC calls into the ersatz firmware, so in
3748 * that case HCR.TSC should be read/write.
3749 */
f149e3e8
EI
3750 valid_mask &= ~HCR_TSC;
3751 }
3752
3753 /* Clear RES0 bits. */
3754 value &= valid_mask;
3755
3756 /* These bits change the MMU setup:
3757 * HCR_VM enables stage 2 translation
3758 * HCR_PTW forbids certain page-table setups
3759 * HCR_DC Disables stage1 and enables stage2 translation
3760 */
3761 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 3762 tlb_flush(CPU(cpu));
f149e3e8
EI
3763 }
3764 raw_write(env, ri, value);
3765}
3766
4771cd01 3767static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3768 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3769 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3770 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3771 .writefn = hcr_write },
3b685ba7 3772 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3773 .type = ARM_CP_ALIAS,
3b685ba7
EI
3774 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3775 .access = PL2_RW,
3776 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3777 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3778 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3779 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3780 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3781 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3782 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3783 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3784 .type = ARM_CP_ALIAS,
3b685ba7 3785 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3786 .access = PL2_RW,
3787 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3788 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3789 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3790 .access = PL2_RW, .writefn = vbar_write,
3791 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3792 .resetvalue = 0 },
884b4dee
GB
3793 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3794 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3795 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3796 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3797 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3798 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3799 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3800 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3801 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3802 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3803 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3804 .resetvalue = 0 },
3805 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3806 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3807 .access = PL2_RW, .type = ARM_CP_ALIAS,
3808 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3809 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3810 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3811 .access = PL2_RW, .type = ARM_CP_CONST,
3812 .resetvalue = 0 },
3813 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3814 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3815 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3816 .access = PL2_RW, .type = ARM_CP_CONST,
3817 .resetvalue = 0 },
37cd6c24
PM
3818 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3819 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3820 .access = PL2_RW, .type = ARM_CP_CONST,
3821 .resetvalue = 0 },
3822 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3823 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3824 .access = PL2_RW, .type = ARM_CP_CONST,
3825 .resetvalue = 0 },
06ec4c8c
EI
3826 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3827 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3828 .access = PL2_RW,
3829 /* no .writefn needed as this can't cause an ASID change;
3830 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3831 */
06ec4c8c 3832 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3833 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3834 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 3835 .type = ARM_CP_ALIAS,
68e9c2fe
EI
3836 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3837 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3838 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3839 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
3840 .access = PL2_RW,
3841 /* no .writefn needed as this can't cause an ASID change;
3842 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3843 */
68e9c2fe 3844 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3845 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3846 .cp = 15, .opc1 = 6, .crm = 2,
3847 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3848 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3849 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3850 .writefn = vttbr_write },
3851 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3852 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3853 .access = PL2_RW, .writefn = vttbr_write,
3854 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3855 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3856 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3857 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3858 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3859 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3860 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3861 .access = PL2_RW, .resetvalue = 0,
3862 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3863 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3864 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3865 .access = PL2_RW, .resetvalue = 0,
3866 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3867 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3868 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3869 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
3870 { .name = "TLBIALLNSNH",
3871 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3872 .type = ARM_CP_NO_RAW, .access = PL2_W,
3873 .writefn = tlbiall_nsnh_write },
3874 { .name = "TLBIALLNSNHIS",
3875 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3876 .type = ARM_CP_NO_RAW, .access = PL2_W,
3877 .writefn = tlbiall_nsnh_is_write },
3878 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3879 .type = ARM_CP_NO_RAW, .access = PL2_W,
3880 .writefn = tlbiall_hyp_write },
3881 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3882 .type = ARM_CP_NO_RAW, .access = PL2_W,
3883 .writefn = tlbiall_hyp_is_write },
3884 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3885 .type = ARM_CP_NO_RAW, .access = PL2_W,
3886 .writefn = tlbimva_hyp_write },
3887 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3888 .type = ARM_CP_NO_RAW, .access = PL2_W,
3889 .writefn = tlbimva_hyp_is_write },
51da9014
EI
3890 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3892 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3893 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3894 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3895 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3896 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3897 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3898 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3899 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3900 .access = PL2_W, .type = ARM_CP_NO_RAW,
3901 .writefn = tlbi_aa64_vae2_write },
3902 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3903 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3904 .access = PL2_W, .type = ARM_CP_NO_RAW,
3905 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3906 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3907 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3908 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3909 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3910 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3911 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3912 .access = PL2_W, .type = ARM_CP_NO_RAW,
3913 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3914#ifndef CONFIG_USER_ONLY
2a47df95
PM
3915 /* Unlike the other EL2-related AT operations, these must
3916 * UNDEF from EL3 if EL2 is not implemented, which is why we
3917 * define them here rather than with the rest of the AT ops.
3918 */
3919 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3920 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3921 .access = PL2_W, .accessfn = at_s1e2_access,
3922 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3923 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3924 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3925 .access = PL2_W, .accessfn = at_s1e2_access,
3926 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3927 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3928 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3929 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3930 * to behave as if SCR.NS was 1.
3931 */
3932 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3933 .access = PL2_W,
3934 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3935 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3936 .access = PL2_W,
3937 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3938 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3939 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3940 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3941 * reset values as IMPDEF. We choose to reset to 3 to comply with
3942 * both ARMv7 and ARMv8.
3943 */
3944 .access = PL2_RW, .resetvalue = 3,
3945 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3946 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3947 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3948 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3949 .writefn = gt_cntvoff_write,
3950 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3951 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3952 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3953 .writefn = gt_cntvoff_write,
3954 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3955 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3956 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3957 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3958 .type = ARM_CP_IO, .access = PL2_RW,
3959 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3960 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3961 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3962 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3963 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3964 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3965 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 3966 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
3967 .resetfn = gt_hyp_timer_reset,
3968 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3969 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3970 .type = ARM_CP_IO,
3971 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3972 .access = PL2_RW,
3973 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3974 .resetvalue = 0,
3975 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 3976#endif
14cc7b54
SF
3977 /* The only field of MDCR_EL2 that has a defined architectural reset value
3978 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3979 * don't impelment any PMU event counters, so using zero as a reset
3980 * value for MDCR_EL2 is okay
3981 */
3982 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3983 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3984 .access = PL2_RW, .resetvalue = 0,
3985 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
3986 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3987 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3988 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3989 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3990 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3991 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3992 .access = PL2_RW,
3993 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
3994 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3995 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3996 .access = PL2_RW,
3997 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
3998 REGINFO_SENTINEL
3999};
4000
2f027fc5
PM
4001static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4002 bool isread)
4003{
4004 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4005 * At Secure EL1 it traps to EL3.
4006 */
4007 if (arm_current_el(env) == 3) {
4008 return CP_ACCESS_OK;
4009 }
4010 if (arm_is_secure_below_el3(env)) {
4011 return CP_ACCESS_TRAP_EL3;
4012 }
4013 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4014 if (isread) {
4015 return CP_ACCESS_OK;
4016 }
4017 return CP_ACCESS_TRAP_UNCATEGORIZED;
4018}
4019
60fb1a87
GB
4020static const ARMCPRegInfo el3_cp_reginfo[] = {
4021 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4022 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4023 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4024 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 4025 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 4026 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
4027 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4028 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 4029 .writefn = scr_write },
60fb1a87
GB
4030 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4031 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4032 .access = PL3_RW, .resetvalue = 0,
4033 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4034 { .name = "SDER",
4035 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4036 .access = PL3_RW, .resetvalue = 0,
4037 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 4038 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
4039 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4040 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 4041 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
4042 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4043 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4044 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4045 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
4046 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4047 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
4048 .access = PL3_RW,
4049 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
4050 * we must provide a .raw_writefn and .resetfn because we handle
4051 * reset and migration for the AArch32 TTBCR(S), which might be
4052 * using mask and base_mask.
6459b94c 4053 */
811595a2 4054 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 4055 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 4056 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4057 .type = ARM_CP_ALIAS,
81547d66
EI
4058 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4059 .access = PL3_RW,
4060 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 4061 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
4062 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4063 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
4064 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4065 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4066 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 4067 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4068 .type = ARM_CP_ALIAS,
81547d66 4069 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4070 .access = PL3_RW,
4071 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
4072 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4073 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4074 .access = PL3_RW, .writefn = vbar_write,
4075 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4076 .resetvalue = 0 },
c6f19164
GB
4077 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4078 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4079 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4080 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
4081 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4082 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4083 .access = PL3_RW, .resetvalue = 0,
4084 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
4085 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4086 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4087 .access = PL3_RW, .type = ARM_CP_CONST,
4088 .resetvalue = 0 },
37cd6c24
PM
4089 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4090 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4091 .access = PL3_RW, .type = ARM_CP_CONST,
4092 .resetvalue = 0 },
4093 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4094 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4095 .access = PL3_RW, .type = ARM_CP_CONST,
4096 .resetvalue = 0 },
43efaa33
PM
4097 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4098 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4099 .access = PL3_W, .type = ARM_CP_NO_RAW,
4100 .writefn = tlbi_aa64_alle3is_write },
4101 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4102 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4103 .access = PL3_W, .type = ARM_CP_NO_RAW,
4104 .writefn = tlbi_aa64_vae3is_write },
4105 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4106 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4107 .access = PL3_W, .type = ARM_CP_NO_RAW,
4108 .writefn = tlbi_aa64_vae3is_write },
4109 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4110 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4111 .access = PL3_W, .type = ARM_CP_NO_RAW,
4112 .writefn = tlbi_aa64_alle3_write },
4113 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4114 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4115 .access = PL3_W, .type = ARM_CP_NO_RAW,
4116 .writefn = tlbi_aa64_vae3_write },
4117 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4118 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4119 .access = PL3_W, .type = ARM_CP_NO_RAW,
4120 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
4121 REGINFO_SENTINEL
4122};
4123
3f208fd7
PM
4124static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4125 bool isread)
7da845b0
PM
4126{
4127 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4128 * but the AArch32 CTR has its own reginfo struct)
4129 */
137feaa9 4130 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
4131 return CP_ACCESS_TRAP;
4132 }
4133 return CP_ACCESS_OK;
4134}
4135
1424ca8d
DM
4136static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4137 uint64_t value)
4138{
4139 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4140 * read via a bit in OSLSR_EL1.
4141 */
4142 int oslock;
4143
4144 if (ri->state == ARM_CP_STATE_AA32) {
4145 oslock = (value == 0xC5ACCE55);
4146 } else {
4147 oslock = value & 1;
4148 }
4149
4150 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4151}
4152
50300698 4153static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 4154 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
4155 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4156 * unlike DBGDRAR it is never accessible from EL0.
4157 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4158 * accessor.
50300698
PM
4159 */
4160 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4161 .access = PL0_R, .accessfn = access_tdra,
4162 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
4163 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4164 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
4165 .access = PL1_R, .accessfn = access_tdra,
4166 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 4167 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4168 .access = PL0_R, .accessfn = access_tdra,
4169 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 4170 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
4171 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4172 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 4173 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
4174 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4175 .resetvalue = 0 },
5e8b12ff
PM
4176 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4177 * We don't implement the configurable EL0 access.
4178 */
4179 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4180 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 4181 .type = ARM_CP_ALIAS,
d6c8cf81 4182 .access = PL1_R, .accessfn = access_tda,
b061a82b 4183 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
4184 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4185 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 4186 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 4187 .accessfn = access_tdosa,
1424ca8d
DM
4188 .writefn = oslar_write },
4189 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4190 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4191 .access = PL1_R, .resetvalue = 10,
187f678d 4192 .accessfn = access_tdosa,
1424ca8d 4193 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
4194 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4195 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4196 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
4197 .access = PL1_RW, .accessfn = access_tdosa,
4198 .type = ARM_CP_NOP },
5e8b12ff
PM
4199 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4200 * implement vector catch debug events yet.
4201 */
4202 { .name = "DBGVCR",
4203 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
4204 .access = PL1_RW, .accessfn = access_tda,
4205 .type = ARM_CP_NOP },
4d2ec4da
PM
4206 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4207 * to save and restore a 32-bit guest's DBGVCR)
4208 */
4209 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4210 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4211 .access = PL2_RW, .accessfn = access_tda,
4212 .type = ARM_CP_NOP },
5dbdc434
PM
4213 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4214 * Channel but Linux may try to access this register. The 32-bit
4215 * alias is DBGDCCINT.
4216 */
4217 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4218 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4219 .access = PL1_RW, .accessfn = access_tda,
4220 .type = ARM_CP_NOP },
50300698
PM
4221 REGINFO_SENTINEL
4222};
4223
4224static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4225 /* 64 bit access versions of the (dummy) debug registers */
4226 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4227 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4228 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4229 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4230 REGINFO_SENTINEL
4231};
4232
9ee98ce8
PM
4233void hw_watchpoint_update(ARMCPU *cpu, int n)
4234{
4235 CPUARMState *env = &cpu->env;
4236 vaddr len = 0;
4237 vaddr wvr = env->cp15.dbgwvr[n];
4238 uint64_t wcr = env->cp15.dbgwcr[n];
4239 int mask;
4240 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4241
4242 if (env->cpu_watchpoint[n]) {
4243 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4244 env->cpu_watchpoint[n] = NULL;
4245 }
4246
4247 if (!extract64(wcr, 0, 1)) {
4248 /* E bit clear : watchpoint disabled */
4249 return;
4250 }
4251
4252 switch (extract64(wcr, 3, 2)) {
4253 case 0:
4254 /* LSC 00 is reserved and must behave as if the wp is disabled */
4255 return;
4256 case 1:
4257 flags |= BP_MEM_READ;
4258 break;
4259 case 2:
4260 flags |= BP_MEM_WRITE;
4261 break;
4262 case 3:
4263 flags |= BP_MEM_ACCESS;
4264 break;
4265 }
4266
4267 /* Attempts to use both MASK and BAS fields simultaneously are
4268 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4269 * thus generating a watchpoint for every byte in the masked region.
4270 */
4271 mask = extract64(wcr, 24, 4);
4272 if (mask == 1 || mask == 2) {
4273 /* Reserved values of MASK; we must act as if the mask value was
4274 * some non-reserved value, or as if the watchpoint were disabled.
4275 * We choose the latter.
4276 */
4277 return;
4278 } else if (mask) {
4279 /* Watchpoint covers an aligned area up to 2GB in size */
4280 len = 1ULL << mask;
4281 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4282 * whether the watchpoint fires when the unmasked bits match; we opt
4283 * to generate the exceptions.
4284 */
4285 wvr &= ~(len - 1);
4286 } else {
4287 /* Watchpoint covers bytes defined by the byte address select bits */
4288 int bas = extract64(wcr, 5, 8);
4289 int basstart;
4290
4291 if (bas == 0) {
4292 /* This must act as if the watchpoint is disabled */
4293 return;
4294 }
4295
4296 if (extract64(wvr, 2, 1)) {
4297 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4298 * ignored, and BAS[3:0] define which bytes to watch.
4299 */
4300 bas &= 0xf;
4301 }
4302 /* The BAS bits are supposed to be programmed to indicate a contiguous
4303 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4304 * we fire for each byte in the word/doubleword addressed by the WVR.
4305 * We choose to ignore any non-zero bits after the first range of 1s.
4306 */
4307 basstart = ctz32(bas);
4308 len = cto32(bas >> basstart);
4309 wvr += basstart;
4310 }
4311
4312 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4313 &env->cpu_watchpoint[n]);
4314}
4315
4316void hw_watchpoint_update_all(ARMCPU *cpu)
4317{
4318 int i;
4319 CPUARMState *env = &cpu->env;
4320
4321 /* Completely clear out existing QEMU watchpoints and our array, to
4322 * avoid possible stale entries following migration load.
4323 */
4324 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4325 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4326
4327 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4328 hw_watchpoint_update(cpu, i);
4329 }
4330}
4331
4332static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4333 uint64_t value)
4334{
4335 ARMCPU *cpu = arm_env_get_cpu(env);
4336 int i = ri->crm;
4337
4338 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4339 * register reads and behaves as if values written are sign extended.
4340 * Bits [1:0] are RES0.
4341 */
4342 value = sextract64(value, 0, 49) & ~3ULL;
4343
4344 raw_write(env, ri, value);
4345 hw_watchpoint_update(cpu, i);
4346}
4347
4348static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4349 uint64_t value)
4350{
4351 ARMCPU *cpu = arm_env_get_cpu(env);
4352 int i = ri->crm;
4353
4354 raw_write(env, ri, value);
4355 hw_watchpoint_update(cpu, i);
4356}
4357
46747d15
PM
4358void hw_breakpoint_update(ARMCPU *cpu, int n)
4359{
4360 CPUARMState *env = &cpu->env;
4361 uint64_t bvr = env->cp15.dbgbvr[n];
4362 uint64_t bcr = env->cp15.dbgbcr[n];
4363 vaddr addr;
4364 int bt;
4365 int flags = BP_CPU;
4366
4367 if (env->cpu_breakpoint[n]) {
4368 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4369 env->cpu_breakpoint[n] = NULL;
4370 }
4371
4372 if (!extract64(bcr, 0, 1)) {
4373 /* E bit clear : watchpoint disabled */
4374 return;
4375 }
4376
4377 bt = extract64(bcr, 20, 4);
4378
4379 switch (bt) {
4380 case 4: /* unlinked address mismatch (reserved if AArch64) */
4381 case 5: /* linked address mismatch (reserved if AArch64) */
4382 qemu_log_mask(LOG_UNIMP,
4383 "arm: address mismatch breakpoint types not implemented");
4384 return;
4385 case 0: /* unlinked address match */
4386 case 1: /* linked address match */
4387 {
4388 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4389 * we behave as if the register was sign extended. Bits [1:0] are
4390 * RES0. The BAS field is used to allow setting breakpoints on 16
4391 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4392 * a bp will fire if the addresses covered by the bp and the addresses
4393 * covered by the insn overlap but the insn doesn't start at the
4394 * start of the bp address range. We choose to require the insn and
4395 * the bp to have the same address. The constraints on writing to
4396 * BAS enforced in dbgbcr_write mean we have only four cases:
4397 * 0b0000 => no breakpoint
4398 * 0b0011 => breakpoint on addr
4399 * 0b1100 => breakpoint on addr + 2
4400 * 0b1111 => breakpoint on addr
4401 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4402 */
4403 int bas = extract64(bcr, 5, 4);
4404 addr = sextract64(bvr, 0, 49) & ~3ULL;
4405 if (bas == 0) {
4406 return;
4407 }
4408 if (bas == 0xc) {
4409 addr += 2;
4410 }
4411 break;
4412 }
4413 case 2: /* unlinked context ID match */
4414 case 8: /* unlinked VMID match (reserved if no EL2) */
4415 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4416 qemu_log_mask(LOG_UNIMP,
4417 "arm: unlinked context breakpoint types not implemented");
4418 return;
4419 case 9: /* linked VMID match (reserved if no EL2) */
4420 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4421 case 3: /* linked context ID match */
4422 default:
4423 /* We must generate no events for Linked context matches (unless
4424 * they are linked to by some other bp/wp, which is handled in
4425 * updates for the linking bp/wp). We choose to also generate no events
4426 * for reserved values.
4427 */
4428 return;
4429 }
4430
4431 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4432}
4433
4434void hw_breakpoint_update_all(ARMCPU *cpu)
4435{
4436 int i;
4437 CPUARMState *env = &cpu->env;
4438
4439 /* Completely clear out existing QEMU breakpoints and our array, to
4440 * avoid possible stale entries following migration load.
4441 */
4442 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4443 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4444
4445 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4446 hw_breakpoint_update(cpu, i);
4447 }
4448}
4449
4450static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4451 uint64_t value)
4452{
4453 ARMCPU *cpu = arm_env_get_cpu(env);
4454 int i = ri->crm;
4455
4456 raw_write(env, ri, value);
4457 hw_breakpoint_update(cpu, i);
4458}
4459
4460static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4461 uint64_t value)
4462{
4463 ARMCPU *cpu = arm_env_get_cpu(env);
4464 int i = ri->crm;
4465
4466 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4467 * copy of BAS[0].
4468 */
4469 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4470 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4471
4472 raw_write(env, ri, value);
4473 hw_breakpoint_update(cpu, i);
4474}
4475
50300698 4476static void define_debug_regs(ARMCPU *cpu)
0b45451e 4477{
50300698
PM
4478 /* Define v7 and v8 architectural debug registers.
4479 * These are just dummy implementations for now.
0b45451e
PM
4480 */
4481 int i;
3ff6fc91 4482 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4483 ARMCPRegInfo dbgdidr = {
4484 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4485 .access = PL0_R, .accessfn = access_tda,
4486 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4487 };
4488
3ff6fc91 4489 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4490 brps = extract32(cpu->dbgdidr, 24, 4);
4491 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4492 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4493
4494 assert(ctx_cmps <= brps);
48eb3ae6
PM
4495
4496 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4497 * of the debug registers such as number of breakpoints;
4498 * check that if they both exist then they agree.
4499 */
4500 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4501 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4502 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4503 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4504 }
0b45451e 4505
48eb3ae6 4506 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4507 define_arm_cp_regs(cpu, debug_cp_reginfo);
4508
4509 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4510 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4511 }
4512
48eb3ae6 4513 for (i = 0; i < brps + 1; i++) {
0b45451e 4514 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4515 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4516 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4517 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4518 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4519 .writefn = dbgbvr_write, .raw_writefn = raw_write
4520 },
10aae104
PM
4521 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4522 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4523 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4524 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4525 .writefn = dbgbcr_write, .raw_writefn = raw_write
4526 },
48eb3ae6
PM
4527 REGINFO_SENTINEL
4528 };
4529 define_arm_cp_regs(cpu, dbgregs);
4530 }
4531
4532 for (i = 0; i < wrps + 1; i++) {
4533 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4534 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4535 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4536 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4537 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4538 .writefn = dbgwvr_write, .raw_writefn = raw_write
4539 },
10aae104
PM
4540 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4541 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4542 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4543 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4544 .writefn = dbgwcr_write, .raw_writefn = raw_write
4545 },
4546 REGINFO_SENTINEL
0b45451e
PM
4547 };
4548 define_arm_cp_regs(cpu, dbgregs);
4549 }
4550}
4551
96a8b92e
PM
4552/* We don't know until after realize whether there's a GICv3
4553 * attached, and that is what registers the gicv3 sysregs.
4554 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4555 * at runtime.
4556 */
4557static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4558{
4559 ARMCPU *cpu = arm_env_get_cpu(env);
4560 uint64_t pfr1 = cpu->id_pfr1;
4561
4562 if (env->gicv3state) {
4563 pfr1 |= 1 << 28;
4564 }
4565 return pfr1;
4566}
4567
4568static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4569{
4570 ARMCPU *cpu = arm_env_get_cpu(env);
4571 uint64_t pfr0 = cpu->id_aa64pfr0;
4572
4573 if (env->gicv3state) {
4574 pfr0 |= 1 << 24;
4575 }
4576 return pfr0;
4577}
4578
2ceb98c0
PM
4579void register_cp_regs_for_features(ARMCPU *cpu)
4580{
4581 /* Register all the coprocessor registers based on feature bits */
4582 CPUARMState *env = &cpu->env;
4583 if (arm_feature(env, ARM_FEATURE_M)) {
4584 /* M profile has no coprocessor registers */
4585 return;
4586 }
4587
e9aa6c21 4588 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4589 if (!arm_feature(env, ARM_FEATURE_V8)) {
4590 /* Must go early as it is full of wildcards that may be
4591 * overridden by later definitions.
4592 */
4593 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4594 }
4595
7d57f408 4596 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4597 /* The ID registers all have impdef reset values */
4598 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4599 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4600 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4601 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4602 .resetvalue = cpu->id_pfr0 },
96a8b92e
PM
4603 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4604 * the value of the GIC field until after we define these regs.
4605 */
0ff644a7
PM
4606 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4607 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e
PM
4608 .access = PL1_R, .type = ARM_CP_NO_RAW,
4609 .readfn = id_pfr1_read,
4610 .writefn = arm_cp_write_ignore },
0ff644a7
PM
4611 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4612 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4613 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4614 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4615 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4616 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4617 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4618 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4619 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4620 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4621 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4622 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4623 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4624 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4625 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4626 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4627 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4628 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4629 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4630 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4631 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4632 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4633 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4634 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4635 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4636 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4637 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4638 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4639 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4640 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4641 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4642 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4643 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4644 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4645 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4646 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4647 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4648 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4649 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4650 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4651 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4652 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4653 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4654 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4655 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4656 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4657 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4658 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4659 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4660 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4661 .access = PL1_R, .type = ARM_CP_CONST,
4662 .resetvalue = cpu->id_mmfr4 },
4663 /* 7 is as yet unallocated and must RAZ */
4664 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4665 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4666 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4667 .resetvalue = 0 },
4668 REGINFO_SENTINEL
4669 };
4670 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4671 define_arm_cp_regs(cpu, v6_cp_reginfo);
4672 } else {
4673 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4674 }
4d31c596
PM
4675 if (arm_feature(env, ARM_FEATURE_V6K)) {
4676 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4677 }
5e5cf9e3 4678 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 4679 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
4680 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4681 }
e9aa6c21 4682 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4683 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4684 * field as main ID register, and we implement only the cycle
4685 * count register.
200ac0ef 4686 */
7c2cb42b 4687#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4688 ARMCPRegInfo pmcr = {
4689 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4690 .access = PL0_RW,
7a0e58fa 4691 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4692 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4693 .accessfn = pmreg_access, .writefn = pmcr_write,
4694 .raw_writefn = raw_write,
200ac0ef 4695 };
8521466b
AF
4696 ARMCPRegInfo pmcr64 = {
4697 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4698 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4699 .access = PL0_RW, .accessfn = pmreg_access,
4700 .type = ARM_CP_IO,
4701 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4702 .resetvalue = cpu->midr & 0xff000000,
4703 .writefn = pmcr_write, .raw_writefn = raw_write,
4704 };
7c2cb42b 4705 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4706 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4707#endif
776d4e5c 4708 ARMCPRegInfo clidr = {
7da845b0
PM
4709 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4710 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4711 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4712 };
776d4e5c 4713 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4714 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4715 define_debug_regs(cpu);
7d57f408
PM
4716 } else {
4717 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4718 }
b0d2b7d0 4719 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4720 /* AArch64 ID registers, which all have impdef reset values.
4721 * Note that within the ID register ranges the unused slots
4722 * must all RAZ, not UNDEF; future architecture versions may
4723 * define new registers here.
4724 */
e60cef86 4725 ARMCPRegInfo v8_idregs[] = {
96a8b92e
PM
4726 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4727 * know the right value for the GIC field until after we
4728 * define these regs.
4729 */
e60cef86
PM
4730 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4731 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
96a8b92e
PM
4732 .access = PL1_R, .type = ARM_CP_NO_RAW,
4733 .readfn = id_aa64pfr0_read,
4734 .writefn = arm_cp_write_ignore },
e60cef86
PM
4735 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4736 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4737 .access = PL1_R, .type = ARM_CP_CONST,
4738 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4739 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4740 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4741 .access = PL1_R, .type = ARM_CP_CONST,
4742 .resetvalue = 0 },
4743 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4745 .access = PL1_R, .type = ARM_CP_CONST,
4746 .resetvalue = 0 },
4747 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4748 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4749 .access = PL1_R, .type = ARM_CP_CONST,
4750 .resetvalue = 0 },
4751 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4752 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4753 .access = PL1_R, .type = ARM_CP_CONST,
4754 .resetvalue = 0 },
4755 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4756 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4757 .access = PL1_R, .type = ARM_CP_CONST,
4758 .resetvalue = 0 },
4759 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4760 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4761 .access = PL1_R, .type = ARM_CP_CONST,
4762 .resetvalue = 0 },
e60cef86
PM
4763 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4765 .access = PL1_R, .type = ARM_CP_CONST,
d6f02ce3 4766 .resetvalue = cpu->id_aa64dfr0 },
e60cef86
PM
4767 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4769 .access = PL1_R, .type = ARM_CP_CONST,
4770 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4771 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4773 .access = PL1_R, .type = ARM_CP_CONST,
4774 .resetvalue = 0 },
4775 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4777 .access = PL1_R, .type = ARM_CP_CONST,
4778 .resetvalue = 0 },
e60cef86
PM
4779 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4780 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4781 .access = PL1_R, .type = ARM_CP_CONST,
4782 .resetvalue = cpu->id_aa64afr0 },
4783 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4784 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4785 .access = PL1_R, .type = ARM_CP_CONST,
4786 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4787 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4789 .access = PL1_R, .type = ARM_CP_CONST,
4790 .resetvalue = 0 },
4791 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4793 .access = PL1_R, .type = ARM_CP_CONST,
4794 .resetvalue = 0 },
e60cef86
PM
4795 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4797 .access = PL1_R, .type = ARM_CP_CONST,
4798 .resetvalue = cpu->id_aa64isar0 },
4799 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4801 .access = PL1_R, .type = ARM_CP_CONST,
4802 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4803 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4805 .access = PL1_R, .type = ARM_CP_CONST,
4806 .resetvalue = 0 },
4807 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4809 .access = PL1_R, .type = ARM_CP_CONST,
4810 .resetvalue = 0 },
4811 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4812 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4813 .access = PL1_R, .type = ARM_CP_CONST,
4814 .resetvalue = 0 },
4815 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4816 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4817 .access = PL1_R, .type = ARM_CP_CONST,
4818 .resetvalue = 0 },
4819 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4820 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4821 .access = PL1_R, .type = ARM_CP_CONST,
4822 .resetvalue = 0 },
4823 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4824 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4825 .access = PL1_R, .type = ARM_CP_CONST,
4826 .resetvalue = 0 },
e60cef86
PM
4827 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4829 .access = PL1_R, .type = ARM_CP_CONST,
4830 .resetvalue = cpu->id_aa64mmfr0 },
4831 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4833 .access = PL1_R, .type = ARM_CP_CONST,
4834 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
4835 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4836 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4837 .access = PL1_R, .type = ARM_CP_CONST,
4838 .resetvalue = 0 },
4839 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4840 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4841 .access = PL1_R, .type = ARM_CP_CONST,
4842 .resetvalue = 0 },
4843 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4844 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4845 .access = PL1_R, .type = ARM_CP_CONST,
4846 .resetvalue = 0 },
4847 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4849 .access = PL1_R, .type = ARM_CP_CONST,
4850 .resetvalue = 0 },
4851 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4853 .access = PL1_R, .type = ARM_CP_CONST,
4854 .resetvalue = 0 },
4855 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4856 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4857 .access = PL1_R, .type = ARM_CP_CONST,
4858 .resetvalue = 0 },
a50c0f51
PM
4859 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4861 .access = PL1_R, .type = ARM_CP_CONST,
4862 .resetvalue = cpu->mvfr0 },
4863 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4864 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4865 .access = PL1_R, .type = ARM_CP_CONST,
4866 .resetvalue = cpu->mvfr1 },
4867 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4868 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4869 .access = PL1_R, .type = ARM_CP_CONST,
4870 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
4871 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4872 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4873 .access = PL1_R, .type = ARM_CP_CONST,
4874 .resetvalue = 0 },
4875 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4876 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4877 .access = PL1_R, .type = ARM_CP_CONST,
4878 .resetvalue = 0 },
4879 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4880 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4881 .access = PL1_R, .type = ARM_CP_CONST,
4882 .resetvalue = 0 },
4883 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4885 .access = PL1_R, .type = ARM_CP_CONST,
4886 .resetvalue = 0 },
4887 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4889 .access = PL1_R, .type = ARM_CP_CONST,
4890 .resetvalue = 0 },
4054bfa9
AF
4891 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4892 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4893 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4894 .resetvalue = cpu->pmceid0 },
4895 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4896 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4897 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4898 .resetvalue = cpu->pmceid0 },
4899 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4900 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4901 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4902 .resetvalue = cpu->pmceid1 },
4903 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4904 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4905 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4906 .resetvalue = cpu->pmceid1 },
e60cef86
PM
4907 REGINFO_SENTINEL
4908 };
be8e8128
GB
4909 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4910 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4911 !arm_feature(env, ARM_FEATURE_EL2)) {
4912 ARMCPRegInfo rvbar = {
4913 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4914 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4915 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4916 };
4917 define_one_arm_cp_reg(cpu, &rvbar);
4918 }
e60cef86 4919 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
4920 define_arm_cp_regs(cpu, v8_cp_reginfo);
4921 }
3b685ba7 4922 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 4923 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
4924 ARMCPRegInfo vpidr_regs[] = {
4925 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4926 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4927 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4928 .resetvalue = cpu->midr,
4929 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4930 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4931 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4932 .access = PL2_RW, .resetvalue = cpu->midr,
4933 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4934 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4935 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4936 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4937 .resetvalue = vmpidr_def,
4938 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4939 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4940 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4941 .access = PL2_RW,
4942 .resetvalue = vmpidr_def,
4943 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
4944 REGINFO_SENTINEL
4945 };
4946 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4947 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
4948 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4949 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4950 ARMCPRegInfo rvbar = {
4951 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4952 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4953 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4954 };
4955 define_one_arm_cp_reg(cpu, &rvbar);
4956 }
d42e3c26
EI
4957 } else {
4958 /* If EL2 is missing but higher ELs are enabled, we need to
4959 * register the no_el2 reginfos.
4960 */
4961 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
4962 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4963 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
4964 */
4965 ARMCPRegInfo vpidr_regs[] = {
4966 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4967 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4968 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4969 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4970 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4971 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4972 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4973 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4974 .type = ARM_CP_NO_RAW,
4975 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
4976 REGINFO_SENTINEL
4977 };
4978 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4979 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 4980 }
3b685ba7 4981 }
81547d66 4982 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 4983 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
4984 ARMCPRegInfo el3_regs[] = {
4985 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4986 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4987 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
4988 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
4989 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
4990 .access = PL3_RW,
4991 .raw_writefn = raw_write, .writefn = sctlr_write,
4992 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
4993 .resetvalue = cpu->reset_sctlr },
4994 REGINFO_SENTINEL
be8e8128 4995 };
e24fdd23
PM
4996
4997 define_arm_cp_regs(cpu, el3_regs);
81547d66 4998 }
2f027fc5
PM
4999 /* The behaviour of NSACR is sufficiently various that we don't
5000 * try to describe it in a single reginfo:
5001 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5002 * reads as constant 0xc00 from NS EL1 and NS EL2
5003 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5004 * if v7 without EL3, register doesn't exist
5005 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5006 */
5007 if (arm_feature(env, ARM_FEATURE_EL3)) {
5008 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5009 ARMCPRegInfo nsacr = {
5010 .name = "NSACR", .type = ARM_CP_CONST,
5011 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5012 .access = PL1_RW, .accessfn = nsacr_access,
5013 .resetvalue = 0xc00
5014 };
5015 define_one_arm_cp_reg(cpu, &nsacr);
5016 } else {
5017 ARMCPRegInfo nsacr = {
5018 .name = "NSACR",
5019 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5020 .access = PL3_RW | PL1_R,
5021 .resetvalue = 0,
5022 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5023 };
5024 define_one_arm_cp_reg(cpu, &nsacr);
5025 }
5026 } else {
5027 if (arm_feature(env, ARM_FEATURE_V8)) {
5028 ARMCPRegInfo nsacr = {
5029 .name = "NSACR", .type = ARM_CP_CONST,
5030 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5031 .access = PL1_R,
5032 .resetvalue = 0xc00
5033 };
5034 define_one_arm_cp_reg(cpu, &nsacr);
5035 }
5036 }
5037
452a0955 5038 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
5039 if (arm_feature(env, ARM_FEATURE_V6)) {
5040 /* PMSAv6 not implemented */
5041 assert(arm_feature(env, ARM_FEATURE_V7));
5042 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5043 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5044 } else {
5045 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5046 }
18032bec 5047 } else {
8e5d75c9 5048 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
5049 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5050 }
c326b979
PM
5051 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5052 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5053 }
6cc7a3ae
PM
5054 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5055 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5056 }
4a501606
PM
5057 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5058 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5059 }
c4804214
PM
5060 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5061 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5062 }
5063 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5064 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5065 }
5066 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5067 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5068 }
18032bec
PM
5069 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5070 define_arm_cp_regs(cpu, omap_cp_reginfo);
5071 }
34f90529
PM
5072 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5073 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5074 }
1047b9d7
PM
5075 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5076 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5077 }
5078 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5079 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5080 }
7ac681cf
PM
5081 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5082 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5083 }
7884849c
PM
5084 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5085 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5086 * be read-only (ie write causes UNDEF exception).
5087 */
5088 {
00a29f3d
PM
5089 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5090 /* Pre-v8 MIDR space.
5091 * Note that the MIDR isn't a simple constant register because
7884849c
PM
5092 * of the TI925 behaviour where writes to another register can
5093 * cause the MIDR value to change.
97ce8d61
PC
5094 *
5095 * Unimplemented registers in the c15 0 0 0 space default to
5096 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5097 * and friends override accordingly.
7884849c
PM
5098 */
5099 { .name = "MIDR",
97ce8d61 5100 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 5101 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 5102 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 5103 .readfn = midr_read,
97ce8d61
PC
5104 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5105 .type = ARM_CP_OVERRIDE },
7884849c
PM
5106 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5107 { .name = "DUMMY",
5108 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5109 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5110 { .name = "DUMMY",
5111 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5112 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5113 { .name = "DUMMY",
5114 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5115 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5116 { .name = "DUMMY",
5117 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5118 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5119 { .name = "DUMMY",
5120 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5121 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5122 REGINFO_SENTINEL
5123 };
00a29f3d 5124 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
5125 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5126 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
5127 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5128 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5129 .readfn = midr_read },
ac00c79f
SF
5130 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5131 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5132 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5133 .access = PL1_R, .resetvalue = cpu->midr },
5134 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5135 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5136 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
5137 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5138 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 5139 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
5140 REGINFO_SENTINEL
5141 };
5142 ARMCPRegInfo id_cp_reginfo[] = {
5143 /* These are common to v8 and pre-v8 */
5144 { .name = "CTR",
5145 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5146 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5147 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5148 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5149 .access = PL0_R, .accessfn = ctr_el0_access,
5150 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5151 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5152 { .name = "TCMTR",
5153 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5154 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
5155 REGINFO_SENTINEL
5156 };
8085ce63
PC
5157 /* TLBTR is specific to VMSA */
5158 ARMCPRegInfo id_tlbtr_reginfo = {
5159 .name = "TLBTR",
5160 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5161 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5162 };
3281af81
PC
5163 /* MPUIR is specific to PMSA V6+ */
5164 ARMCPRegInfo id_mpuir_reginfo = {
5165 .name = "MPUIR",
5166 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5167 .access = PL1_R, .type = ARM_CP_CONST,
5168 .resetvalue = cpu->pmsav7_dregion << 8
5169 };
7884849c
PM
5170 ARMCPRegInfo crn0_wi_reginfo = {
5171 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5172 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5173 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5174 };
5175 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5176 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5177 ARMCPRegInfo *r;
5178 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
5179 * whole space. Then update the specific ID registers to allow write
5180 * access, so that they ignore writes rather than causing them to
5181 * UNDEF.
7884849c
PM
5182 */
5183 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
5184 for (r = id_pre_v8_midr_cp_reginfo;
5185 r->type != ARM_CP_SENTINEL; r++) {
5186 r->access = PL1_RW;
5187 }
7884849c
PM
5188 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5189 r->access = PL1_RW;
7884849c 5190 }
8085ce63 5191 id_tlbtr_reginfo.access = PL1_RW;
3281af81 5192 id_tlbtr_reginfo.access = PL1_RW;
7884849c 5193 }
00a29f3d
PM
5194 if (arm_feature(env, ARM_FEATURE_V8)) {
5195 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5196 } else {
5197 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5198 }
a703eda1 5199 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 5200 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 5201 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
5202 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5203 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 5204 }
7884849c
PM
5205 }
5206
97ce8d61
PC
5207 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5208 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5209 }
5210
2771db27 5211 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
5212 ARMCPRegInfo auxcr_reginfo[] = {
5213 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5214 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5215 .access = PL1_RW, .type = ARM_CP_CONST,
5216 .resetvalue = cpu->reset_auxcr },
5217 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5218 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5219 .access = PL2_RW, .type = ARM_CP_CONST,
5220 .resetvalue = 0 },
5221 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5222 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5223 .access = PL3_RW, .type = ARM_CP_CONST,
5224 .resetvalue = 0 },
5225 REGINFO_SENTINEL
2771db27 5226 };
834a6c69 5227 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
5228 }
5229
d8ba780b 5230 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
5231 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5232 /* 32 bit view is [31:18] 0...0 [43:32]. */
5233 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5234 | extract64(cpu->reset_cbar, 32, 12);
5235 ARMCPRegInfo cbar_reginfo[] = {
5236 { .name = "CBAR",
5237 .type = ARM_CP_CONST,
5238 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5239 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5240 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5241 .type = ARM_CP_CONST,
5242 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5243 .access = PL1_R, .resetvalue = cbar32 },
5244 REGINFO_SENTINEL
5245 };
5246 /* We don't implement a r/w 64 bit CBAR currently */
5247 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5248 define_arm_cp_regs(cpu, cbar_reginfo);
5249 } else {
5250 ARMCPRegInfo cbar = {
5251 .name = "CBAR",
5252 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5253 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5254 .fieldoffset = offsetof(CPUARMState,
5255 cp15.c15_config_base_address)
5256 };
5257 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5258 cbar.access = PL1_R;
5259 cbar.fieldoffset = 0;
5260 cbar.type = ARM_CP_CONST;
5261 }
5262 define_one_arm_cp_reg(cpu, &cbar);
5263 }
d8ba780b
PC
5264 }
5265
91db4642
CLG
5266 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5267 ARMCPRegInfo vbar_cp_reginfo[] = {
5268 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5269 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5270 .access = PL1_RW, .writefn = vbar_write,
5271 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5272 offsetof(CPUARMState, cp15.vbar_ns) },
5273 .resetvalue = 0 },
5274 REGINFO_SENTINEL
5275 };
5276 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5277 }
5278
2771db27
PM
5279 /* Generic registers whose values depend on the implementation */
5280 {
5281 ARMCPRegInfo sctlr = {
5ebafdf3 5282 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
5283 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5284 .access = PL1_RW,
5285 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5286 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
5287 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5288 .raw_writefn = raw_write,
2771db27
PM
5289 };
5290 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5291 /* Normally we would always end the TB on an SCTLR write, but Linux
5292 * arch/arm/mach-pxa/sleep.S expects two instructions following
5293 * an MMU enable to execute from cache. Imitate this behaviour.
5294 */
5295 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5296 }
5297 define_one_arm_cp_reg(cpu, &sctlr);
5298 }
2ceb98c0
PM
5299}
5300
14969266
AF
5301void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5302{
22169d41 5303 CPUState *cs = CPU(cpu);
14969266
AF
5304 CPUARMState *env = &cpu->env;
5305
6a669427
PM
5306 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5307 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5308 aarch64_fpu_gdb_set_reg,
5309 34, "aarch64-fpu.xml", 0);
5310 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 5311 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5312 51, "arm-neon.xml", 0);
5313 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 5314 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5315 35, "arm-vfp3.xml", 0);
5316 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 5317 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5318 19, "arm-vfp.xml", 0);
5319 }
40f137e1
PB
5320}
5321
777dc784
PM
5322/* Sort alphabetically by type name, except for "any". */
5323static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 5324{
777dc784
PM
5325 ObjectClass *class_a = (ObjectClass *)a;
5326 ObjectClass *class_b = (ObjectClass *)b;
5327 const char *name_a, *name_b;
5adb4839 5328
777dc784
PM
5329 name_a = object_class_get_name(class_a);
5330 name_b = object_class_get_name(class_b);
51492fd1 5331 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 5332 return 1;
51492fd1 5333 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
5334 return -1;
5335 } else {
5336 return strcmp(name_a, name_b);
5adb4839
PB
5337 }
5338}
5339
777dc784 5340static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 5341{
777dc784 5342 ObjectClass *oc = data;
92a31361 5343 CPUListState *s = user_data;
51492fd1
AF
5344 const char *typename;
5345 char *name;
3371d272 5346
51492fd1
AF
5347 typename = object_class_get_name(oc);
5348 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 5349 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
5350 name);
5351 g_free(name);
777dc784
PM
5352}
5353
5354void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5355{
92a31361 5356 CPUListState s = {
777dc784
PM
5357 .file = f,
5358 .cpu_fprintf = cpu_fprintf,
5359 };
5360 GSList *list;
5361
5362 list = object_class_get_list(TYPE_ARM_CPU, false);
5363 list = g_slist_sort(list, arm_cpu_list_compare);
5364 (*cpu_fprintf)(f, "Available CPUs:\n");
5365 g_slist_foreach(list, arm_cpu_list_entry, &s);
5366 g_slist_free(list);
a96c0514
PM
5367#ifdef CONFIG_KVM
5368 /* The 'host' CPU type is dynamically registered only if KVM is
5369 * enabled, so we have to special-case it here:
5370 */
5371 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5372#endif
40f137e1
PB
5373}
5374
78027bb6
CR
5375static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5376{
5377 ObjectClass *oc = data;
5378 CpuDefinitionInfoList **cpu_list = user_data;
5379 CpuDefinitionInfoList *entry;
5380 CpuDefinitionInfo *info;
5381 const char *typename;
5382
5383 typename = object_class_get_name(oc);
5384 info = g_malloc0(sizeof(*info));
5385 info->name = g_strndup(typename,
5386 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 5387 info->q_typename = g_strdup(typename);
78027bb6
CR
5388
5389 entry = g_malloc0(sizeof(*entry));
5390 entry->value = info;
5391 entry->next = *cpu_list;
5392 *cpu_list = entry;
5393}
5394
5395CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5396{
5397 CpuDefinitionInfoList *cpu_list = NULL;
5398 GSList *list;
5399
5400 list = object_class_get_list(TYPE_ARM_CPU, false);
5401 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5402 g_slist_free(list);
5403
5404 return cpu_list;
5405}
5406
6e6efd61 5407static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5408 void *opaque, int state, int secstate,
f5a0a5a5 5409 int crm, int opc1, int opc2)
6e6efd61
PM
5410{
5411 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5412 * add a single reginfo struct to the hash table.
5413 */
5414 uint32_t *key = g_new(uint32_t, 1);
5415 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5416 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5417 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5418
5419 /* Reset the secure state to the specific incoming state. This is
5420 * necessary as the register may have been defined with both states.
5421 */
5422 r2->secure = secstate;
5423
5424 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5425 /* Register is banked (using both entries in array).
5426 * Overwriting fieldoffset as the array is only used to define
5427 * banked registers but later only fieldoffset is used.
f5a0a5a5 5428 */
3f3c82a5
FA
5429 r2->fieldoffset = r->bank_fieldoffsets[ns];
5430 }
5431
5432 if (state == ARM_CP_STATE_AA32) {
5433 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5434 /* If the register is banked then we don't need to migrate or
5435 * reset the 32-bit instance in certain cases:
5436 *
5437 * 1) If the register has both 32-bit and 64-bit instances then we
5438 * can count on the 64-bit instance taking care of the
5439 * non-secure bank.
5440 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5441 * taking care of the secure bank. This requires that separate
5442 * 32 and 64-bit definitions are provided.
5443 */
5444 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5445 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5446 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5447 }
5448 } else if ((secstate != r->secure) && !ns) {
5449 /* The register is not banked so we only want to allow migration of
5450 * the non-secure instance.
5451 */
7a0e58fa 5452 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5453 }
3f3c82a5
FA
5454
5455 if (r->state == ARM_CP_STATE_BOTH) {
5456 /* We assume it is a cp15 register if the .cp field is left unset.
5457 */
5458 if (r2->cp == 0) {
5459 r2->cp = 15;
5460 }
5461
f5a0a5a5 5462#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5463 if (r2->fieldoffset) {
5464 r2->fieldoffset += sizeof(uint32_t);
5465 }
f5a0a5a5 5466#endif
3f3c82a5 5467 }
f5a0a5a5
PM
5468 }
5469 if (state == ARM_CP_STATE_AA64) {
5470 /* To allow abbreviation of ARMCPRegInfo
5471 * definitions, we treat cp == 0 as equivalent to
5472 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5473 * STATE_BOTH definitions are also always "standard
5474 * sysreg" in their AArch64 view (the .cp value may
5475 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5476 */
58a1d8ce 5477 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5478 r2->cp = CP_REG_ARM64_SYSREG_CP;
5479 }
5480 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5481 r2->opc0, opc1, opc2);
5482 } else {
51a79b03 5483 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5484 }
6e6efd61
PM
5485 if (opaque) {
5486 r2->opaque = opaque;
5487 }
67ed771d
PM
5488 /* reginfo passed to helpers is correct for the actual access,
5489 * and is never ARM_CP_STATE_BOTH:
5490 */
5491 r2->state = state;
6e6efd61
PM
5492 /* Make sure reginfo passed to helpers for wildcarded regs
5493 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5494 */
5495 r2->crm = crm;
5496 r2->opc1 = opc1;
5497 r2->opc2 = opc2;
5498 /* By convention, for wildcarded registers only the first
5499 * entry is used for migration; the others are marked as
7a0e58fa 5500 * ALIAS so we don't try to transfer the register
6e6efd61 5501 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5502 * never migratable and not even raw-accessible.
6e6efd61 5503 */
7a0e58fa
PM
5504 if ((r->type & ARM_CP_SPECIAL)) {
5505 r2->type |= ARM_CP_NO_RAW;
5506 }
5507 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5508 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5509 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 5510 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
5511 }
5512
375421cc
PM
5513 /* Check that raw accesses are either forbidden or handled. Note that
5514 * we can't assert this earlier because the setup of fieldoffset for
5515 * banked registers has to be done first.
5516 */
5517 if (!(r2->type & ARM_CP_NO_RAW)) {
5518 assert(!raw_accessors_invalid(r2));
5519 }
5520
6e6efd61
PM
5521 /* Overriding of an existing definition must be explicitly
5522 * requested.
5523 */
5524 if (!(r->type & ARM_CP_OVERRIDE)) {
5525 ARMCPRegInfo *oldreg;
5526 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5527 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5528 fprintf(stderr, "Register redefined: cp=%d %d bit "
5529 "crn=%d crm=%d opc1=%d opc2=%d, "
5530 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5531 r2->crn, r2->crm, r2->opc1, r2->opc2,
5532 oldreg->name, r2->name);
5533 g_assert_not_reached();
5534 }
5535 }
5536 g_hash_table_insert(cpu->cp_regs, key, r2);
5537}
5538
5539
4b6a83fb
PM
5540void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5541 const ARMCPRegInfo *r, void *opaque)
5542{
5543 /* Define implementations of coprocessor registers.
5544 * We store these in a hashtable because typically
5545 * there are less than 150 registers in a space which
5546 * is 16*16*16*8*8 = 262144 in size.
5547 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5548 * If a register is defined twice then the second definition is
5549 * used, so this can be used to define some generic registers and
5550 * then override them with implementation specific variations.
5551 * At least one of the original and the second definition should
5552 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5553 * against accidental use.
f5a0a5a5
PM
5554 *
5555 * The state field defines whether the register is to be
5556 * visible in the AArch32 or AArch64 execution state. If the
5557 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5558 * reginfo structure for the AArch32 view, which sees the lower
5559 * 32 bits of the 64 bit register.
5560 *
5561 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5562 * be wildcarded. AArch64 registers are always considered to be 64
5563 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5564 * the register, if any.
4b6a83fb 5565 */
f5a0a5a5 5566 int crm, opc1, opc2, state;
4b6a83fb
PM
5567 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5568 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5569 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5570 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5571 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5572 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5573 /* 64 bit registers have only CRm and Opc1 fields */
5574 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5575 /* op0 only exists in the AArch64 encodings */
5576 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5577 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5578 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5579 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5580 * encodes a minimum access level for the register. We roll this
5581 * runtime check into our general permission check code, so check
5582 * here that the reginfo's specified permissions are strict enough
5583 * to encompass the generic architectural permission check.
5584 */
5585 if (r->state != ARM_CP_STATE_AA32) {
5586 int mask = 0;
5587 switch (r->opc1) {
5588 case 0: case 1: case 2:
5589 /* min_EL EL1 */
5590 mask = PL1_RW;
5591 break;
5592 case 3:
5593 /* min_EL EL0 */
5594 mask = PL0_RW;
5595 break;
5596 case 4:
5597 /* min_EL EL2 */
5598 mask = PL2_RW;
5599 break;
5600 case 5:
5601 /* unallocated encoding, so not possible */
5602 assert(false);
5603 break;
5604 case 6:
5605 /* min_EL EL3 */
5606 mask = PL3_RW;
5607 break;
5608 case 7:
5609 /* min_EL EL1, secure mode only (we don't check the latter) */
5610 mask = PL1_RW;
5611 break;
5612 default:
5613 /* broken reginfo with out-of-range opc1 */
5614 assert(false);
5615 break;
5616 }
5617 /* assert our permissions are not too lax (stricter is fine) */
5618 assert((r->access & ~mask) == 0);
5619 }
5620
4b6a83fb
PM
5621 /* Check that the register definition has enough info to handle
5622 * reads and writes if they are permitted.
5623 */
5624 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5625 if (r->access & PL3_R) {
3f3c82a5
FA
5626 assert((r->fieldoffset ||
5627 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5628 r->readfn);
4b6a83fb
PM
5629 }
5630 if (r->access & PL3_W) {
3f3c82a5
FA
5631 assert((r->fieldoffset ||
5632 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5633 r->writefn);
4b6a83fb
PM
5634 }
5635 }
5636 /* Bad type field probably means missing sentinel at end of reg list */
5637 assert(cptype_valid(r->type));
5638 for (crm = crmmin; crm <= crmmax; crm++) {
5639 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5640 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5641 for (state = ARM_CP_STATE_AA32;
5642 state <= ARM_CP_STATE_AA64; state++) {
5643 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5644 continue;
5645 }
3f3c82a5
FA
5646 if (state == ARM_CP_STATE_AA32) {
5647 /* Under AArch32 CP registers can be common
5648 * (same for secure and non-secure world) or banked.
5649 */
5650 switch (r->secure) {
5651 case ARM_CP_SECSTATE_S:
5652 case ARM_CP_SECSTATE_NS:
5653 add_cpreg_to_hashtable(cpu, r, opaque, state,
5654 r->secure, crm, opc1, opc2);
5655 break;
5656 default:
5657 add_cpreg_to_hashtable(cpu, r, opaque, state,
5658 ARM_CP_SECSTATE_S,
5659 crm, opc1, opc2);
5660 add_cpreg_to_hashtable(cpu, r, opaque, state,
5661 ARM_CP_SECSTATE_NS,
5662 crm, opc1, opc2);
5663 break;
5664 }
5665 } else {
5666 /* AArch64 registers get mapped to non-secure instance
5667 * of AArch32 */
5668 add_cpreg_to_hashtable(cpu, r, opaque, state,
5669 ARM_CP_SECSTATE_NS,
5670 crm, opc1, opc2);
5671 }
f5a0a5a5 5672 }
4b6a83fb
PM
5673 }
5674 }
5675 }
5676}
5677
5678void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5679 const ARMCPRegInfo *regs, void *opaque)
5680{
5681 /* Define a whole list of registers */
5682 const ARMCPRegInfo *r;
5683 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5684 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5685 }
5686}
5687
60322b39 5688const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5689{
60322b39 5690 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5691}
5692
c4241c7d
PM
5693void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5694 uint64_t value)
4b6a83fb
PM
5695{
5696 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5697}
5698
c4241c7d 5699uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5700{
5701 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5702 return 0;
5703}
5704
f5a0a5a5
PM
5705void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5706{
5707 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5708}
5709
af393ffc 5710static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5711{
5712 /* Return true if it is not valid for us to switch to
5713 * this CPU mode (ie all the UNPREDICTABLE cases in
5714 * the ARM ARM CPSRWriteByInstr pseudocode).
5715 */
af393ffc
PM
5716
5717 /* Changes to or from Hyp via MSR and CPS are illegal. */
5718 if (write_type == CPSRWriteByInstr &&
5719 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5720 mode == ARM_CPU_MODE_HYP)) {
5721 return 1;
5722 }
5723
37064a8b
PM
5724 switch (mode) {
5725 case ARM_CPU_MODE_USR:
10eacda7 5726 return 0;
37064a8b
PM
5727 case ARM_CPU_MODE_SYS:
5728 case ARM_CPU_MODE_SVC:
5729 case ARM_CPU_MODE_ABT:
5730 case ARM_CPU_MODE_UND:
5731 case ARM_CPU_MODE_IRQ:
5732 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5733 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5734 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5735 */
10eacda7
PM
5736 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5737 * and CPS are treated as illegal mode changes.
5738 */
5739 if (write_type == CPSRWriteByInstr &&
5740 (env->cp15.hcr_el2 & HCR_TGE) &&
5741 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5742 !arm_is_secure_below_el3(env)) {
5743 return 1;
5744 }
37064a8b 5745 return 0;
e6c8fc07
PM
5746 case ARM_CPU_MODE_HYP:
5747 return !arm_feature(env, ARM_FEATURE_EL2)
5748 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5749 case ARM_CPU_MODE_MON:
58ae2d1f 5750 return arm_current_el(env) < 3;
37064a8b
PM
5751 default:
5752 return 1;
5753 }
5754}
5755
2f4a40e5
AZ
5756uint32_t cpsr_read(CPUARMState *env)
5757{
5758 int ZF;
6fbe23d5
PB
5759 ZF = (env->ZF == 0);
5760 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5761 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5762 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5763 | ((env->condexec_bits & 0xfc) << 8)
af519934 5764 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5765}
5766
50866ba5
PM
5767void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5768 CPSRWriteType write_type)
2f4a40e5 5769{
6e8801f9
FA
5770 uint32_t changed_daif;
5771
2f4a40e5 5772 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5773 env->ZF = (~val) & CPSR_Z;
5774 env->NF = val;
2f4a40e5
AZ
5775 env->CF = (val >> 29) & 1;
5776 env->VF = (val << 3) & 0x80000000;
5777 }
5778 if (mask & CPSR_Q)
5779 env->QF = ((val & CPSR_Q) != 0);
5780 if (mask & CPSR_T)
5781 env->thumb = ((val & CPSR_T) != 0);
5782 if (mask & CPSR_IT_0_1) {
5783 env->condexec_bits &= ~3;
5784 env->condexec_bits |= (val >> 25) & 3;
5785 }
5786 if (mask & CPSR_IT_2_7) {
5787 env->condexec_bits &= 3;
5788 env->condexec_bits |= (val >> 8) & 0xfc;
5789 }
5790 if (mask & CPSR_GE) {
5791 env->GE = (val >> 16) & 0xf;
5792 }
5793
6e8801f9
FA
5794 /* In a V7 implementation that includes the security extensions but does
5795 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5796 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5797 * bits respectively.
5798 *
5799 * In a V8 implementation, it is permitted for privileged software to
5800 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5801 */
f8c88bbc 5802 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
5803 arm_feature(env, ARM_FEATURE_EL3) &&
5804 !arm_feature(env, ARM_FEATURE_EL2) &&
5805 !arm_is_secure(env)) {
5806
5807 changed_daif = (env->daif ^ val) & mask;
5808
5809 if (changed_daif & CPSR_A) {
5810 /* Check to see if we are allowed to change the masking of async
5811 * abort exceptions from a non-secure state.
5812 */
5813 if (!(env->cp15.scr_el3 & SCR_AW)) {
5814 qemu_log_mask(LOG_GUEST_ERROR,
5815 "Ignoring attempt to switch CPSR_A flag from "
5816 "non-secure world with SCR.AW bit clear\n");
5817 mask &= ~CPSR_A;
5818 }
5819 }
5820
5821 if (changed_daif & CPSR_F) {
5822 /* Check to see if we are allowed to change the masking of FIQ
5823 * exceptions from a non-secure state.
5824 */
5825 if (!(env->cp15.scr_el3 & SCR_FW)) {
5826 qemu_log_mask(LOG_GUEST_ERROR,
5827 "Ignoring attempt to switch CPSR_F flag from "
5828 "non-secure world with SCR.FW bit clear\n");
5829 mask &= ~CPSR_F;
5830 }
5831
5832 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5833 * If this bit is set software is not allowed to mask
5834 * FIQs, but is allowed to set CPSR_F to 0.
5835 */
5836 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5837 (val & CPSR_F)) {
5838 qemu_log_mask(LOG_GUEST_ERROR,
5839 "Ignoring attempt to enable CPSR_F flag "
5840 "(non-maskable FIQ [NMFI] support enabled)\n");
5841 mask &= ~CPSR_F;
5842 }
5843 }
5844 }
5845
4cc35614
PM
5846 env->daif &= ~(CPSR_AIF & mask);
5847 env->daif |= val & CPSR_AIF & mask;
5848
f8c88bbc
PM
5849 if (write_type != CPSRWriteRaw &&
5850 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
5851 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5852 /* Note that we can only get here in USR mode if this is a
5853 * gdb stub write; for this case we follow the architectural
5854 * behaviour for guest writes in USR mode of ignoring an attempt
5855 * to switch mode. (Those are caught by translate.c for writes
5856 * triggered by guest instructions.)
5857 */
5858 mask &= ~CPSR_M;
5859 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
5860 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5861 * v7, and has defined behaviour in v8:
5862 * + leave CPSR.M untouched
5863 * + allow changes to the other CPSR fields
5864 * + set PSTATE.IL
5865 * For user changes via the GDB stub, we don't set PSTATE.IL,
5866 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
5867 */
5868 mask &= ~CPSR_M;
81907a58
PM
5869 if (write_type != CPSRWriteByGDBStub &&
5870 arm_feature(env, ARM_FEATURE_V8)) {
5871 mask |= CPSR_IL;
5872 val |= CPSR_IL;
5873 }
37064a8b
PM
5874 } else {
5875 switch_mode(env, val & CPSR_M);
5876 }
2f4a40e5
AZ
5877 }
5878 mask &= ~CACHED_CPSR_BITS;
5879 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5880}
5881
b26eefb6
PB
5882/* Sign/zero extend */
5883uint32_t HELPER(sxtb16)(uint32_t x)
5884{
5885 uint32_t res;
5886 res = (uint16_t)(int8_t)x;
5887 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5888 return res;
5889}
5890
5891uint32_t HELPER(uxtb16)(uint32_t x)
5892{
5893 uint32_t res;
5894 res = (uint16_t)(uint8_t)x;
5895 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5896 return res;
5897}
5898
3670669c
PB
5899int32_t HELPER(sdiv)(int32_t num, int32_t den)
5900{
5901 if (den == 0)
5902 return 0;
686eeb93
AJ
5903 if (num == INT_MIN && den == -1)
5904 return INT_MIN;
3670669c
PB
5905 return num / den;
5906}
5907
5908uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5909{
5910 if (den == 0)
5911 return 0;
5912 return num / den;
5913}
5914
5915uint32_t HELPER(rbit)(uint32_t x)
5916{
42fedbca 5917 return revbit32(x);
3670669c
PB
5918}
5919
5fafdf24 5920#if defined(CONFIG_USER_ONLY)
b5ff1b31 5921
9ee6e8bb 5922/* These should probably raise undefined insn exceptions. */
0ecb72a5 5923void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 5924{
a47dddd7
AF
5925 ARMCPU *cpu = arm_env_get_cpu(env);
5926
5927 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
5928}
5929
0ecb72a5 5930uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 5931{
a47dddd7
AF
5932 ARMCPU *cpu = arm_env_get_cpu(env);
5933
5934 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
5935 return 0;
5936}
5937
fb602cb7
PM
5938void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
5939{
5940 /* translate.c should never generate calls here in user-only mode */
5941 g_assert_not_reached();
5942}
5943
3e3fa230
PM
5944void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
5945{
5946 /* translate.c should never generate calls here in user-only mode */
5947 g_assert_not_reached();
5948}
5949
5158de24
PM
5950uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
5951{
5952 /* The TT instructions can be used by unprivileged code, but in
5953 * user-only emulation we don't have the MPU.
5954 * Luckily since we know we are NonSecure unprivileged (and that in
5955 * turn means that the A flag wasn't specified), all the bits in the
5956 * register must be zero:
5957 * IREGION: 0 because IRVALID is 0
5958 * IRVALID: 0 because NS
5959 * S: 0 because NS
5960 * NSRW: 0 because NS
5961 * NSR: 0 because NS
5962 * RW: 0 because unpriv and A flag not set
5963 * R: 0 because unpriv and A flag not set
5964 * SRVALID: 0 because NS
5965 * MRVALID: 0 because unpriv and A flag not set
5966 * SREGION: 0 becaus SRVALID is 0
5967 * MREGION: 0 because MRVALID is 0
5968 */
5969 return 0;
5970}
5971
0ecb72a5 5972void switch_mode(CPUARMState *env, int mode)
b5ff1b31 5973{
a47dddd7
AF
5974 ARMCPU *cpu = arm_env_get_cpu(env);
5975
5976 if (mode != ARM_CPU_MODE_USR) {
5977 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5978 }
b5ff1b31
FB
5979}
5980
012a906b
GB
5981uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5982 uint32_t cur_el, bool secure)
9e729b57
EI
5983{
5984 return 1;
5985}
5986
ce02049d
GB
5987void aarch64_sync_64_to_32(CPUARMState *env)
5988{
5989 g_assert_not_reached();
5990}
5991
b5ff1b31
FB
5992#else
5993
0ecb72a5 5994void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
5995{
5996 int old_mode;
5997 int i;
5998
5999 old_mode = env->uncached_cpsr & CPSR_M;
6000 if (mode == old_mode)
6001 return;
6002
6003 if (old_mode == ARM_CPU_MODE_FIQ) {
6004 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6005 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6006 } else if (mode == ARM_CPU_MODE_FIQ) {
6007 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 6008 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
6009 }
6010
f5206413 6011 i = bank_number(old_mode);
b5ff1b31
FB
6012 env->banked_r13[i] = env->regs[13];
6013 env->banked_r14[i] = env->regs[14];
6014 env->banked_spsr[i] = env->spsr;
6015
f5206413 6016 i = bank_number(mode);
b5ff1b31
FB
6017 env->regs[13] = env->banked_r13[i];
6018 env->regs[14] = env->banked_r14[i];
6019 env->spsr = env->banked_spsr[i];
6020}
6021
0eeb17d6
GB
6022/* Physical Interrupt Target EL Lookup Table
6023 *
6024 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6025 *
6026 * The below multi-dimensional table is used for looking up the target
6027 * exception level given numerous condition criteria. Specifically, the
6028 * target EL is based on SCR and HCR routing controls as well as the
6029 * currently executing EL and secure state.
6030 *
6031 * Dimensions:
6032 * target_el_table[2][2][2][2][2][4]
6033 * | | | | | +--- Current EL
6034 * | | | | +------ Non-secure(0)/Secure(1)
6035 * | | | +--------- HCR mask override
6036 * | | +------------ SCR exec state control
6037 * | +--------------- SCR mask override
6038 * +------------------ 32-bit(0)/64-bit(1) EL3
6039 *
6040 * The table values are as such:
6041 * 0-3 = EL0-EL3
6042 * -1 = Cannot occur
6043 *
6044 * The ARM ARM target EL table includes entries indicating that an "exception
6045 * is not taken". The two cases where this is applicable are:
6046 * 1) An exception is taken from EL3 but the SCR does not have the exception
6047 * routed to EL3.
6048 * 2) An exception is taken from EL2 but the HCR does not have the exception
6049 * routed to EL2.
6050 * In these two cases, the below table contain a target of EL1. This value is
6051 * returned as it is expected that the consumer of the table data will check
6052 * for "target EL >= current EL" to ensure the exception is not taken.
6053 *
6054 * SCR HCR
6055 * 64 EA AMO From
6056 * BIT IRQ IMO Non-secure Secure
6057 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6058 */
82c39f6a 6059static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
6060 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6061 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6062 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6063 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6064 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6065 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6066 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6067 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6068 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6069 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6070 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6071 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6072 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6073 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6074 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6075 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6076};
6077
6078/*
6079 * Determine the target EL for physical exceptions
6080 */
012a906b
GB
6081uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6082 uint32_t cur_el, bool secure)
0eeb17d6
GB
6083{
6084 CPUARMState *env = cs->env_ptr;
2cde031f 6085 int rw;
0eeb17d6
GB
6086 int scr;
6087 int hcr;
6088 int target_el;
2cde031f
SS
6089 /* Is the highest EL AArch64? */
6090 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6091
6092 if (arm_feature(env, ARM_FEATURE_EL3)) {
6093 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6094 } else {
6095 /* Either EL2 is the highest EL (and so the EL2 register width
6096 * is given by is64); or there is no EL2 or EL3, in which case
6097 * the value of 'rw' does not affect the table lookup anyway.
6098 */
6099 rw = is64;
6100 }
0eeb17d6
GB
6101
6102 switch (excp_idx) {
6103 case EXCP_IRQ:
6104 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6105 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6106 break;
6107 case EXCP_FIQ:
6108 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6109 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6110 break;
6111 default:
6112 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6113 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6114 break;
6115 };
6116
6117 /* If HCR.TGE is set then HCR is treated as being 1 */
6118 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6119
6120 /* Perform a table-lookup for the target EL given the current state */
6121 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6122
6123 assert(target_el > 0);
6124
6125 return target_el;
6126}
6127
9ee6e8bb
PB
6128static void v7m_push(CPUARMState *env, uint32_t val)
6129{
70d74660
AF
6130 CPUState *cs = CPU(arm_env_get_cpu(env));
6131
9ee6e8bb 6132 env->regs[13] -= 4;
ab1da857 6133 stl_phys(cs->as, env->regs[13], val);
9ee6e8bb
PB
6134}
6135
fb602cb7
PM
6136/* Return true if we're using the process stack pointer (not the MSP) */
6137static bool v7m_using_psp(CPUARMState *env)
6138{
6139 /* Handler mode always uses the main stack; for thread mode
6140 * the CONTROL.SPSEL bit determines the answer.
6141 * Note that in v7M it is not possible to be in Handler mode with
6142 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6143 */
6144 return !arm_v7m_is_handler_mode(env) &&
6145 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6146}
6147
3f0cddee
PM
6148/* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6149 * This may change the current stack pointer between Main and Process
6150 * stack pointers if it is done for the CONTROL register for the current
6151 * security state.
de2db7ec 6152 */
3f0cddee
PM
6153static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6154 bool new_spsel,
6155 bool secstate)
9ee6e8bb 6156{
3f0cddee 6157 bool old_is_psp = v7m_using_psp(env);
de2db7ec 6158
3f0cddee
PM
6159 env->v7m.control[secstate] =
6160 deposit32(env->v7m.control[secstate],
de2db7ec
PM
6161 R_V7M_CONTROL_SPSEL_SHIFT,
6162 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6163
3f0cddee
PM
6164 if (secstate == env->v7m.secure) {
6165 bool new_is_psp = v7m_using_psp(env);
6166 uint32_t tmp;
abc24d86 6167
3f0cddee
PM
6168 if (old_is_psp != new_is_psp) {
6169 tmp = env->v7m.other_sp;
6170 env->v7m.other_sp = env->regs[13];
6171 env->regs[13] = tmp;
6172 }
de2db7ec
PM
6173 }
6174}
6175
3f0cddee
PM
6176/* Write to v7M CONTROL.SPSEL bit. This may change the current
6177 * stack pointer between Main and Process stack pointers.
6178 */
6179static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6180{
6181 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6182}
6183
de2db7ec
PM
6184void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6185{
6186 /* Write a new value to v7m.exception, thus transitioning into or out
6187 * of Handler mode; this may result in a change of active stack pointer.
6188 */
6189 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6190 uint32_t tmp;
abc24d86 6191
de2db7ec
PM
6192 env->v7m.exception = new_exc;
6193
6194 new_is_psp = v7m_using_psp(env);
6195
6196 if (old_is_psp != new_is_psp) {
6197 tmp = env->v7m.other_sp;
6198 env->v7m.other_sp = env->regs[13];
6199 env->regs[13] = tmp;
9ee6e8bb
PB
6200 }
6201}
6202
fb602cb7
PM
6203/* Switch M profile security state between NS and S */
6204static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6205{
6206 uint32_t new_ss_msp, new_ss_psp;
6207
6208 if (env->v7m.secure == new_secstate) {
6209 return;
6210 }
6211
6212 /* All the banked state is accessed by looking at env->v7m.secure
6213 * except for the stack pointer; rearrange the SP appropriately.
6214 */
6215 new_ss_msp = env->v7m.other_ss_msp;
6216 new_ss_psp = env->v7m.other_ss_psp;
6217
6218 if (v7m_using_psp(env)) {
6219 env->v7m.other_ss_psp = env->regs[13];
6220 env->v7m.other_ss_msp = env->v7m.other_sp;
6221 } else {
6222 env->v7m.other_ss_msp = env->regs[13];
6223 env->v7m.other_ss_psp = env->v7m.other_sp;
6224 }
6225
6226 env->v7m.secure = new_secstate;
6227
6228 if (v7m_using_psp(env)) {
6229 env->regs[13] = new_ss_psp;
6230 env->v7m.other_sp = new_ss_msp;
6231 } else {
6232 env->regs[13] = new_ss_msp;
6233 env->v7m.other_sp = new_ss_psp;
6234 }
6235}
6236
6237void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6238{
6239 /* Handle v7M BXNS:
6240 * - if the return value is a magic value, do exception return (like BX)
6241 * - otherwise bit 0 of the return value is the target security state
6242 */
d02a8698
PM
6243 uint32_t min_magic;
6244
6245 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6246 /* Covers FNC_RETURN and EXC_RETURN magic */
6247 min_magic = FNC_RETURN_MIN_MAGIC;
6248 } else {
6249 /* EXC_RETURN magic only */
6250 min_magic = EXC_RETURN_MIN_MAGIC;
6251 }
6252
6253 if (dest >= min_magic) {
fb602cb7
PM
6254 /* This is an exception return magic value; put it where
6255 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6256 * Note that if we ever add gen_ss_advance() singlestep support to
6257 * M profile this should count as an "instruction execution complete"
6258 * event (compare gen_bx_excret_final_code()).
6259 */
6260 env->regs[15] = dest & ~1;
6261 env->thumb = dest & 1;
6262 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6263 /* notreached */
6264 }
6265
6266 /* translate.c should have made BXNS UNDEF unless we're secure */
6267 assert(env->v7m.secure);
6268
6269 switch_v7m_security_state(env, dest & 1);
6270 env->thumb = 1;
6271 env->regs[15] = dest & ~1;
6272}
6273
3e3fa230
PM
6274void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6275{
6276 /* Handle v7M BLXNS:
6277 * - bit 0 of the destination address is the target security state
6278 */
6279
6280 /* At this point regs[15] is the address just after the BLXNS */
6281 uint32_t nextinst = env->regs[15] | 1;
6282 uint32_t sp = env->regs[13] - 8;
6283 uint32_t saved_psr;
6284
6285 /* translate.c will have made BLXNS UNDEF unless we're secure */
6286 assert(env->v7m.secure);
6287
6288 if (dest & 1) {
6289 /* target is Secure, so this is just a normal BLX,
6290 * except that the low bit doesn't indicate Thumb/not.
6291 */
6292 env->regs[14] = nextinst;
6293 env->thumb = 1;
6294 env->regs[15] = dest & ~1;
6295 return;
6296 }
6297
6298 /* Target is non-secure: first push a stack frame */
6299 if (!QEMU_IS_ALIGNED(sp, 8)) {
6300 qemu_log_mask(LOG_GUEST_ERROR,
6301 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6302 }
6303
6304 saved_psr = env->v7m.exception;
6305 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6306 saved_psr |= XPSR_SFPA;
6307 }
6308
6309 /* Note that these stores can throw exceptions on MPU faults */
6310 cpu_stl_data(env, sp, nextinst);
6311 cpu_stl_data(env, sp + 4, saved_psr);
6312
6313 env->regs[13] = sp;
6314 env->regs[14] = 0xfeffffff;
6315 if (arm_v7m_is_handler_mode(env)) {
6316 /* Write a dummy value to IPSR, to avoid leaking the current secure
6317 * exception number to non-secure code. This is guaranteed not
6318 * to cause write_v7m_exception() to actually change stacks.
6319 */
6320 write_v7m_exception(env, 1);
6321 }
6322 switch_v7m_security_state(env, 0);
6323 env->thumb = 1;
6324 env->regs[15] = dest;
6325}
6326
5b522399
PM
6327static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6328 bool spsel)
6329{
6330 /* Return a pointer to the location where we currently store the
6331 * stack pointer for the requested security state and thread mode.
6332 * This pointer will become invalid if the CPU state is updated
6333 * such that the stack pointers are switched around (eg changing
6334 * the SPSEL control bit).
6335 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6336 * Unlike that pseudocode, we require the caller to pass us in the
6337 * SPSEL control bit value; this is because we also use this
6338 * function in handling of pushing of the callee-saves registers
6339 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6340 * and in the tailchain codepath the SPSEL bit comes from the exception
6341 * return magic LR value from the previous exception. The pseudocode
6342 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6343 * to make this utility function generic enough to do the job.
6344 */
6345 bool want_psp = threadmode && spsel;
6346
6347 if (secure == env->v7m.secure) {
de2db7ec
PM
6348 if (want_psp == v7m_using_psp(env)) {
6349 return &env->regs[13];
6350 } else {
6351 return &env->v7m.other_sp;
6352 }
5b522399
PM
6353 } else {
6354 if (want_psp) {
6355 return &env->v7m.other_ss_psp;
6356 } else {
6357 return &env->v7m.other_ss_msp;
6358 }
6359 }
6360}
6361
d3392718 6362static uint32_t arm_v7m_load_vector(ARMCPU *cpu, bool targets_secure)
39ae2474
PM
6363{
6364 CPUState *cs = CPU(cpu);
6365 CPUARMState *env = &cpu->env;
6366 MemTxResult result;
d3392718 6367 hwaddr vec = env->v7m.vecbase[targets_secure] + env->v7m.exception * 4;
39ae2474
PM
6368 uint32_t addr;
6369
6370 addr = address_space_ldl(cs->as, vec,
6371 MEMTXATTRS_UNSPECIFIED, &result);
6372 if (result != MEMTX_OK) {
6373 /* Architecturally this should cause a HardFault setting HSFR.VECTTBL,
6374 * which would then be immediately followed by our failing to load
6375 * the entry vector for that HardFault, which is a Lockup case.
6376 * Since we don't model Lockup, we just report this guest error
6377 * via cpu_abort().
6378 */
d3392718
PM
6379 cpu_abort(cs, "Failed to read from %s exception vector table "
6380 "entry %08x\n", targets_secure ? "secure" : "nonsecure",
6381 (unsigned)vec);
39ae2474
PM
6382 }
6383 return addr;
6384}
6385
d3392718
PM
6386static void v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain)
6387{
6388 /* For v8M, push the callee-saves register part of the stack frame.
6389 * Compare the v8M pseudocode PushCalleeStack().
6390 * In the tailchaining case this may not be the current stack.
6391 */
6392 CPUARMState *env = &cpu->env;
6393 CPUState *cs = CPU(cpu);
6394 uint32_t *frame_sp_p;
6395 uint32_t frameptr;
6396
6397 if (dotailchain) {
6398 frame_sp_p = get_v7m_sp_ptr(env, true,
6399 lr & R_V7M_EXCRET_MODE_MASK,
6400 lr & R_V7M_EXCRET_SPSEL_MASK);
6401 } else {
6402 frame_sp_p = &env->regs[13];
6403 }
6404
6405 frameptr = *frame_sp_p - 0x28;
6406
6407 stl_phys(cs->as, frameptr, 0xfefa125b);
6408 stl_phys(cs->as, frameptr + 0x8, env->regs[4]);
6409 stl_phys(cs->as, frameptr + 0xc, env->regs[5]);
6410 stl_phys(cs->as, frameptr + 0x10, env->regs[6]);
6411 stl_phys(cs->as, frameptr + 0x14, env->regs[7]);
6412 stl_phys(cs->as, frameptr + 0x18, env->regs[8]);
6413 stl_phys(cs->as, frameptr + 0x1c, env->regs[9]);
6414 stl_phys(cs->as, frameptr + 0x20, env->regs[10]);
6415 stl_phys(cs->as, frameptr + 0x24, env->regs[11]);
6416
6417 *frame_sp_p = frameptr;
6418}
6419
6420static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain)
39ae2474
PM
6421{
6422 /* Do the "take the exception" parts of exception entry,
6423 * but not the pushing of state to the stack. This is
6424 * similar to the pseudocode ExceptionTaken() function.
6425 */
6426 CPUARMState *env = &cpu->env;
6427 uint32_t addr;
d3392718
PM
6428 bool targets_secure;
6429
6430 targets_secure = armv7m_nvic_acknowledge_irq(env->nvic);
6431
6432 if (arm_feature(env, ARM_FEATURE_V8)) {
6433 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6434 (lr & R_V7M_EXCRET_S_MASK)) {
6435 /* The background code (the owner of the registers in the
6436 * exception frame) is Secure. This means it may either already
6437 * have or now needs to push callee-saves registers.
6438 */
6439 if (targets_secure) {
6440 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6441 /* We took an exception from Secure to NonSecure
6442 * (which means the callee-saved registers got stacked)
6443 * and are now tailchaining to a Secure exception.
6444 * Clear DCRS so eventual return from this Secure
6445 * exception unstacks the callee-saved registers.
6446 */
6447 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6448 }
6449 } else {
6450 /* We're going to a non-secure exception; push the
6451 * callee-saves registers to the stack now, if they're
6452 * not already saved.
6453 */
6454 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6455 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
6456 v7m_push_callee_stack(cpu, lr, dotailchain);
6457 }
6458 lr |= R_V7M_EXCRET_DCRS_MASK;
6459 }
6460 }
6461
6462 lr &= ~R_V7M_EXCRET_ES_MASK;
6463 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6464 lr |= R_V7M_EXCRET_ES_MASK;
6465 }
6466 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6467 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6468 lr |= R_V7M_EXCRET_SPSEL_MASK;
6469 }
6470
6471 /* Clear registers if necessary to prevent non-secure exception
6472 * code being able to see register values from secure code.
6473 * Where register values become architecturally UNKNOWN we leave
6474 * them with their previous values.
6475 */
6476 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6477 if (!targets_secure) {
6478 /* Always clear the caller-saved registers (they have been
6479 * pushed to the stack earlier in v7m_push_stack()).
6480 * Clear callee-saved registers if the background code is
6481 * Secure (in which case these regs were saved in
6482 * v7m_push_callee_stack()).
6483 */
6484 int i;
6485
6486 for (i = 0; i < 13; i++) {
6487 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6488 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6489 env->regs[i] = 0;
6490 }
6491 }
6492 /* Clear EAPSR */
6493 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6494 }
6495 }
6496 }
39ae2474 6497
d3392718
PM
6498 /* Switch to target security state -- must do this before writing SPSEL */
6499 switch_v7m_security_state(env, targets_secure);
de2db7ec 6500 write_v7m_control_spsel(env, 0);
dc3c4c14 6501 arm_clear_exclusive(env);
39ae2474
PM
6502 /* Clear IT bits */
6503 env->condexec_bits = 0;
6504 env->regs[14] = lr;
d3392718 6505 addr = arm_v7m_load_vector(cpu, targets_secure);
39ae2474
PM
6506 env->regs[15] = addr & 0xfffffffe;
6507 env->thumb = addr & 1;
6508}
6509
6510static void v7m_push_stack(ARMCPU *cpu)
6511{
6512 /* Do the "set up stack frame" part of exception entry,
6513 * similar to pseudocode PushStack().
6514 */
6515 CPUARMState *env = &cpu->env;
6516 uint32_t xpsr = xpsr_read(env);
6517
6518 /* Align stack pointer if the guest wants that */
9d40cd8a
PM
6519 if ((env->regs[13] & 4) &&
6520 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
39ae2474 6521 env->regs[13] -= 4;
987ab45e 6522 xpsr |= XPSR_SPREALIGN;
39ae2474
PM
6523 }
6524 /* Switch to the handler mode. */
6525 v7m_push(env, xpsr);
6526 v7m_push(env, env->regs[15]);
6527 v7m_push(env, env->regs[14]);
6528 v7m_push(env, env->regs[12]);
6529 v7m_push(env, env->regs[3]);
6530 v7m_push(env, env->regs[2]);
6531 v7m_push(env, env->regs[1]);
6532 v7m_push(env, env->regs[0]);
6533}
6534
aa488fe3 6535static void do_v7m_exception_exit(ARMCPU *cpu)
9ee6e8bb 6536{
aa488fe3 6537 CPUARMState *env = &cpu->env;
5b522399 6538 CPUState *cs = CPU(cpu);
351e527a 6539 uint32_t excret;
9ee6e8bb 6540 uint32_t xpsr;
aa488fe3 6541 bool ufault = false;
bfb2eb52
PM
6542 bool sfault = false;
6543 bool return_to_sp_process;
6544 bool return_to_handler;
aa488fe3 6545 bool rettobase = false;
5cb18069 6546 bool exc_secure = false;
5b522399 6547 bool return_to_secure;
aa488fe3 6548
d02a8698
PM
6549 /* If we're not in Handler mode then jumps to magic exception-exit
6550 * addresses don't have magic behaviour. However for the v8M
6551 * security extensions the magic secure-function-return has to
6552 * work in thread mode too, so to avoid doing an extra check in
6553 * the generated code we allow exception-exit magic to also cause the
6554 * internal exception and bring us here in thread mode. Correct code
6555 * will never try to do this (the following insn fetch will always
6556 * fault) so we the overhead of having taken an unnecessary exception
6557 * doesn't matter.
aa488fe3 6558 */
d02a8698
PM
6559 if (!arm_v7m_is_handler_mode(env)) {
6560 return;
6561 }
aa488fe3
PM
6562
6563 /* In the spec pseudocode ExceptionReturn() is called directly
6564 * from BXWritePC() and gets the full target PC value including
6565 * bit zero. In QEMU's implementation we treat it as a normal
6566 * jump-to-register (which is then caught later on), and so split
6567 * the target value up between env->regs[15] and env->thumb in
6568 * gen_bx(). Reconstitute it.
6569 */
351e527a 6570 excret = env->regs[15];
aa488fe3 6571 if (env->thumb) {
351e527a 6572 excret |= 1;
aa488fe3
PM
6573 }
6574
6575 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
6576 " previous exception %d\n",
351e527a 6577 excret, env->v7m.exception);
aa488fe3 6578
351e527a 6579 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
aa488fe3 6580 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
351e527a
PM
6581 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
6582 excret);
aa488fe3
PM
6583 }
6584
bfb2eb52
PM
6585 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6586 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
6587 * we pick which FAULTMASK to clear.
6588 */
6589 if (!env->v7m.secure &&
6590 ((excret & R_V7M_EXCRET_ES_MASK) ||
6591 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
6592 sfault = 1;
6593 /* For all other purposes, treat ES as 0 (R_HXSR) */
6594 excret &= ~R_V7M_EXCRET_ES_MASK;
6595 }
6596 }
6597
a20ee600 6598 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
42a6686b
PM
6599 /* Auto-clear FAULTMASK on return from other than NMI.
6600 * If the security extension is implemented then this only
6601 * happens if the raw execution priority is >= 0; the
6602 * value of the ES bit in the exception return value indicates
6603 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
6604 */
6605 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5cb18069 6606 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
42a6686b 6607 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
5cb18069 6608 env->v7m.faultmask[exc_secure] = 0;
42a6686b
PM
6609 }
6610 } else {
6611 env->v7m.faultmask[M_REG_NS] = 0;
6612 }
a20ee600 6613 }
aa488fe3 6614
5cb18069
PM
6615 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
6616 exc_secure)) {
aa488fe3
PM
6617 case -1:
6618 /* attempt to exit an exception that isn't active */
6619 ufault = true;
6620 break;
6621 case 0:
6622 /* still an irq active now */
6623 break;
6624 case 1:
6625 /* we returned to base exception level, no nesting.
6626 * (In the pseudocode this is written using "NestedActivation != 1"
6627 * where we have 'rettobase == false'.)
6628 */
6629 rettobase = true;
6630 break;
6631 default:
6632 g_assert_not_reached();
6633 }
6634
bfb2eb52
PM
6635 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
6636 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
5b522399
PM
6637 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6638 (excret & R_V7M_EXCRET_S_MASK);
6639
bfb2eb52
PM
6640 if (arm_feature(env, ARM_FEATURE_V8)) {
6641 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6642 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
6643 * we choose to take the UsageFault.
6644 */
6645 if ((excret & R_V7M_EXCRET_S_MASK) ||
6646 (excret & R_V7M_EXCRET_ES_MASK) ||
6647 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
6648 ufault = true;
6649 }
6650 }
6651 if (excret & R_V7M_EXCRET_RES0_MASK) {
aa488fe3
PM
6652 ufault = true;
6653 }
bfb2eb52
PM
6654 } else {
6655 /* For v7M we only recognize certain combinations of the low bits */
6656 switch (excret & 0xf) {
6657 case 1: /* Return to Handler */
6658 break;
6659 case 13: /* Return to Thread using Process stack */
6660 case 9: /* Return to Thread using Main stack */
6661 /* We only need to check NONBASETHRDENA for v7M, because in
6662 * v8M this bit does not exist (it is RES1).
6663 */
6664 if (!rettobase &&
6665 !(env->v7m.ccr[env->v7m.secure] &
6666 R_V7M_CCR_NONBASETHRDENA_MASK)) {
6667 ufault = true;
6668 }
6669 break;
6670 default:
6671 ufault = true;
6672 }
6673 }
6674
6675 if (sfault) {
6676 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
6677 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
d3392718 6678 v7m_exception_taken(cpu, excret, true);
bfb2eb52
PM
6679 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6680 "stackframe: failed EXC_RETURN.ES validity check\n");
6681 return;
aa488fe3
PM
6682 }
6683
6684 if (ufault) {
6685 /* Bad exception return: instead of popping the exception
6686 * stack, directly take a usage fault on the current stack.
6687 */
334e8dad 6688 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
2fb50a33 6689 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
d3392718 6690 v7m_exception_taken(cpu, excret, true);
aa488fe3
PM
6691 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6692 "stackframe: failed exception return integrity check\n");
6693 return;
a20ee600 6694 }
9ee6e8bb 6695
de2db7ec
PM
6696 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
6697 * Handler mode (and will be until we write the new XPSR.Interrupt
6698 * field) this does not switch around the current stack pointer.
5b522399 6699 */
3f0cddee 6700 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
5b522399 6701
3919e60b
PM
6702 switch_v7m_security_state(env, return_to_secure);
6703
5b522399
PM
6704 {
6705 /* The stack pointer we should be reading the exception frame from
6706 * depends on bits in the magic exception return type value (and
6707 * for v8M isn't necessarily the stack pointer we will eventually
6708 * end up resuming execution with). Get a pointer to the location
6709 * in the CPU state struct where the SP we need is currently being
6710 * stored; we will use and modify it in place.
6711 * We use this limited C variable scope so we don't accidentally
6712 * use 'frame_sp_p' after we do something that makes it invalid.
fcf83ab1 6713 */
5b522399
PM
6714 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
6715 return_to_secure,
6716 !return_to_handler,
6717 return_to_sp_process);
6718 uint32_t frameptr = *frame_sp_p;
6719
cb484f9a
PM
6720 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
6721 arm_feature(env, ARM_FEATURE_V8)) {
6722 qemu_log_mask(LOG_GUEST_ERROR,
6723 "M profile exception return with non-8-aligned SP "
6724 "for destination state is UNPREDICTABLE\n");
6725 }
6726
907bedb3
PM
6727 /* Do we need to pop callee-saved registers? */
6728 if (return_to_secure &&
6729 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
6730 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
6731 uint32_t expected_sig = 0xfefa125b;
6732 uint32_t actual_sig = ldl_phys(cs->as, frameptr);
6733
6734 if (expected_sig != actual_sig) {
6735 /* Take a SecureFault on the current stack */
6736 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
6737 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
d3392718 6738 v7m_exception_taken(cpu, excret, true);
907bedb3
PM
6739 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6740 "stackframe: failed exception return integrity "
6741 "signature check\n");
6742 return;
6743 }
6744
6745 env->regs[4] = ldl_phys(cs->as, frameptr + 0x8);
6746 env->regs[5] = ldl_phys(cs->as, frameptr + 0xc);
6747 env->regs[6] = ldl_phys(cs->as, frameptr + 0x10);
6748 env->regs[7] = ldl_phys(cs->as, frameptr + 0x14);
6749 env->regs[8] = ldl_phys(cs->as, frameptr + 0x18);
6750 env->regs[9] = ldl_phys(cs->as, frameptr + 0x1c);
6751 env->regs[10] = ldl_phys(cs->as, frameptr + 0x20);
6752 env->regs[11] = ldl_phys(cs->as, frameptr + 0x24);
6753
6754 frameptr += 0x28;
6755 }
6756
5b522399
PM
6757 /* Pop registers. TODO: make these accesses use the correct
6758 * attributes and address space (S/NS, priv/unpriv) and handle
6759 * memory transaction failures.
6760 */
6761 env->regs[0] = ldl_phys(cs->as, frameptr);
6762 env->regs[1] = ldl_phys(cs->as, frameptr + 0x4);
6763 env->regs[2] = ldl_phys(cs->as, frameptr + 0x8);
6764 env->regs[3] = ldl_phys(cs->as, frameptr + 0xc);
6765 env->regs[12] = ldl_phys(cs->as, frameptr + 0x10);
6766 env->regs[14] = ldl_phys(cs->as, frameptr + 0x14);
6767 env->regs[15] = ldl_phys(cs->as, frameptr + 0x18);
4e4259d3
PM
6768
6769 /* Returning from an exception with a PC with bit 0 set is defined
6770 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
6771 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
6772 * the lsbit, and there are several RTOSes out there which incorrectly
6773 * assume the r15 in the stack frame should be a Thumb-style "lsbit
6774 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
6775 * complain about the badly behaved guest.
6776 */
5b522399 6777 if (env->regs[15] & 1) {
5b522399 6778 env->regs[15] &= ~1U;
4e4259d3
PM
6779 if (!arm_feature(env, ARM_FEATURE_V8)) {
6780 qemu_log_mask(LOG_GUEST_ERROR,
6781 "M profile return from interrupt with misaligned "
6782 "PC is UNPREDICTABLE on v7M\n");
6783 }
5b522399 6784 }
4e4259d3 6785
5b522399
PM
6786 xpsr = ldl_phys(cs->as, frameptr + 0x1c);
6787
224e0c30
PM
6788 if (arm_feature(env, ARM_FEATURE_V8)) {
6789 /* For v8M we have to check whether the xPSR exception field
6790 * matches the EXCRET value for return to handler/thread
6791 * before we commit to changing the SP and xPSR.
6792 */
6793 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
6794 if (return_to_handler != will_be_handler) {
6795 /* Take an INVPC UsageFault on the current stack.
6796 * By this point we will have switched to the security state
6797 * for the background state, so this UsageFault will target
6798 * that state.
6799 */
6800 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
6801 env->v7m.secure);
6802 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
d3392718 6803 v7m_exception_taken(cpu, excret, true);
224e0c30
PM
6804 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6805 "stackframe: failed exception return integrity "
6806 "check\n");
6807 return;
6808 }
6809 }
6810
5b522399
PM
6811 /* Commit to consuming the stack frame */
6812 frameptr += 0x20;
6813 /* Undo stack alignment (the SPREALIGN bit indicates that the original
6814 * pre-exception SP was not 8-aligned and we added a padding word to
6815 * align it, so we undo this by ORing in the bit that increases it
6816 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
6817 * would work too but a logical OR is how the pseudocode specifies it.)
6818 */
6819 if (xpsr & XPSR_SPREALIGN) {
6820 frameptr |= 4;
6821 }
6822 *frame_sp_p = frameptr;
fcf83ab1 6823 }
5b522399 6824 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
987ab45e 6825 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
aa488fe3
PM
6826
6827 /* The restored xPSR exception field will be zero if we're
6828 * resuming in Thread mode. If that doesn't match what the
351e527a 6829 * exception return excret specified then this is a UsageFault.
224e0c30 6830 * v7M requires we make this check here; v8M did it earlier.
aa488fe3 6831 */
15b3f556 6832 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
224e0c30
PM
6833 /* Take an INVPC UsageFault by pushing the stack again;
6834 * we know we're v7M so this is never a Secure UsageFault.
2fb50a33 6835 */
224e0c30 6836 assert(!arm_feature(env, ARM_FEATURE_V8));
2fb50a33 6837 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
334e8dad 6838 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
aa488fe3 6839 v7m_push_stack(cpu);
d3392718 6840 v7m_exception_taken(cpu, excret, false);
aa488fe3
PM
6841 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
6842 "failed exception return integrity check\n");
6843 return;
6844 }
6845
6846 /* Otherwise, we have a successful exception exit. */
dc3c4c14 6847 arm_clear_exclusive(env);
aa488fe3 6848 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
9ee6e8bb
PB
6849}
6850
d02a8698
PM
6851static bool do_v7m_function_return(ARMCPU *cpu)
6852{
6853 /* v8M security extensions magic function return.
6854 * We may either:
6855 * (1) throw an exception (longjump)
6856 * (2) return true if we successfully handled the function return
6857 * (3) return false if we failed a consistency check and have
6858 * pended a UsageFault that needs to be taken now
6859 *
6860 * At this point the magic return value is split between env->regs[15]
6861 * and env->thumb. We don't bother to reconstitute it because we don't
6862 * need it (all values are handled the same way).
6863 */
6864 CPUARMState *env = &cpu->env;
6865 uint32_t newpc, newpsr, newpsr_exc;
6866
6867 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
6868
6869 {
6870 bool threadmode, spsel;
6871 TCGMemOpIdx oi;
6872 ARMMMUIdx mmu_idx;
6873 uint32_t *frame_sp_p;
6874 uint32_t frameptr;
6875
6876 /* Pull the return address and IPSR from the Secure stack */
6877 threadmode = !arm_v7m_is_handler_mode(env);
6878 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
6879
6880 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
6881 frameptr = *frame_sp_p;
6882
6883 /* These loads may throw an exception (for MPU faults). We want to
6884 * do them as secure, so work out what MMU index that is.
6885 */
6886 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
6887 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
6888 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
6889 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
6890
6891 /* Consistency checks on new IPSR */
6892 newpsr_exc = newpsr & XPSR_EXCP;
6893 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
6894 (env->v7m.exception == 1 && newpsr_exc != 0))) {
6895 /* Pend the fault and tell our caller to take it */
6896 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6897 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
6898 env->v7m.secure);
6899 qemu_log_mask(CPU_LOG_INT,
6900 "...taking INVPC UsageFault: "
6901 "IPSR consistency check failed\n");
6902 return false;
6903 }
6904
6905 *frame_sp_p = frameptr + 8;
6906 }
6907
6908 /* This invalidates frame_sp_p */
6909 switch_v7m_security_state(env, true);
6910 env->v7m.exception = newpsr_exc;
6911 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
6912 if (newpsr & XPSR_SFPA) {
6913 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
6914 }
6915 xpsr_write(env, 0, XPSR_IT);
6916 env->thumb = newpc & 1;
6917 env->regs[15] = newpc & ~1;
6918
6919 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
6920 return true;
6921}
6922
27a7ea8a
PB
6923static void arm_log_exception(int idx)
6924{
6925 if (qemu_loglevel_mask(CPU_LOG_INT)) {
6926 const char *exc = NULL;
2c4a7cc5
PM
6927 static const char * const excnames[] = {
6928 [EXCP_UDEF] = "Undefined Instruction",
6929 [EXCP_SWI] = "SVC",
6930 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
6931 [EXCP_DATA_ABORT] = "Data Abort",
6932 [EXCP_IRQ] = "IRQ",
6933 [EXCP_FIQ] = "FIQ",
6934 [EXCP_BKPT] = "Breakpoint",
6935 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
6936 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
6937 [EXCP_HVC] = "Hypervisor Call",
6938 [EXCP_HYP_TRAP] = "Hypervisor Trap",
6939 [EXCP_SMC] = "Secure Monitor Call",
6940 [EXCP_VIRQ] = "Virtual IRQ",
6941 [EXCP_VFIQ] = "Virtual FIQ",
6942 [EXCP_SEMIHOST] = "Semihosting call",
6943 [EXCP_NOCP] = "v7M NOCP UsageFault",
6944 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
6945 };
27a7ea8a
PB
6946
6947 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
6948 exc = excnames[idx];
6949 }
6950 if (!exc) {
6951 exc = "unknown";
6952 }
6953 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
6954 }
6955}
6956
333e10c5
PM
6957static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
6958 uint32_t addr, uint16_t *insn)
6959{
6960 /* Load a 16-bit portion of a v7M instruction, returning true on success,
6961 * or false on failure (in which case we will have pended the appropriate
6962 * exception).
6963 * We need to do the instruction fetch's MPU and SAU checks
6964 * like this because there is no MMU index that would allow
6965 * doing the load with a single function call. Instead we must
6966 * first check that the security attributes permit the load
6967 * and that they don't mismatch on the two halves of the instruction,
6968 * and then we do the load as a secure load (ie using the security
6969 * attributes of the address, not the CPU, as architecturally required).
6970 */
6971 CPUState *cs = CPU(cpu);
6972 CPUARMState *env = &cpu->env;
6973 V8M_SAttributes sattrs = {};
6974 MemTxAttrs attrs = {};
6975 ARMMMUFaultInfo fi = {};
6976 MemTxResult txres;
6977 target_ulong page_size;
6978 hwaddr physaddr;
6979 int prot;
6980 uint32_t fsr;
6981
6982 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
6983 if (!sattrs.nsc || sattrs.ns) {
6984 /* This must be the second half of the insn, and it straddles a
6985 * region boundary with the second half not being S&NSC.
6986 */
6987 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
6988 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6989 qemu_log_mask(CPU_LOG_INT,
6990 "...really SecureFault with SFSR.INVEP\n");
6991 return false;
6992 }
6993 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
5b2d261d 6994 &physaddr, &attrs, &prot, &page_size, &fsr, &fi, NULL)) {
333e10c5
PM
6995 /* the MPU lookup failed */
6996 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
6997 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
6998 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
6999 return false;
7000 }
7001 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7002 attrs, &txres);
7003 if (txres != MEMTX_OK) {
7004 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7005 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7006 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7007 return false;
7008 }
7009 return true;
7010}
7011
7012static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7013{
7014 /* Check whether this attempt to execute code in a Secure & NS-Callable
7015 * memory region is for an SG instruction; if so, then emulate the
7016 * effect of the SG instruction and return true. Otherwise pend
7017 * the correct kind of exception and return false.
7018 */
7019 CPUARMState *env = &cpu->env;
7020 ARMMMUIdx mmu_idx;
7021 uint16_t insn;
7022
7023 /* We should never get here unless get_phys_addr_pmsav8() caused
7024 * an exception for NS executing in S&NSC memory.
7025 */
7026 assert(!env->v7m.secure);
7027 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7028
7029 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7030 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7031
7032 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7033 return false;
7034 }
7035
7036 if (!env->thumb) {
7037 goto gen_invep;
7038 }
7039
7040 if (insn != 0xe97f) {
7041 /* Not an SG instruction first half (we choose the IMPDEF
7042 * early-SG-check option).
7043 */
7044 goto gen_invep;
7045 }
7046
7047 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7048 return false;
7049 }
7050
7051 if (insn != 0xe97f) {
7052 /* Not an SG instruction second half (yes, both halves of the SG
7053 * insn have the same hex value)
7054 */
7055 goto gen_invep;
7056 }
7057
7058 /* OK, we have confirmed that we really have an SG instruction.
7059 * We know we're NS in S memory so don't need to repeat those checks.
7060 */
7061 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7062 ", executing it\n", env->regs[15]);
7063 env->regs[14] &= ~1;
7064 switch_v7m_security_state(env, true);
7065 xpsr_write(env, 0, XPSR_IT);
7066 env->regs[15] += 4;
7067 return true;
7068
7069gen_invep:
7070 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7071 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7072 qemu_log_mask(CPU_LOG_INT,
7073 "...really SecureFault with SFSR.INVEP\n");
7074 return false;
7075}
7076
e6f010cc 7077void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 7078{
e6f010cc
AF
7079 ARMCPU *cpu = ARM_CPU(cs);
7080 CPUARMState *env = &cpu->env;
9ee6e8bb 7081 uint32_t lr;
9ee6e8bb 7082
27103424 7083 arm_log_exception(cs->exception_index);
3f1beaca 7084
9ee6e8bb
PB
7085 /* For exceptions we just mark as pending on the NVIC, and let that
7086 handle it. */
27103424 7087 switch (cs->exception_index) {
9ee6e8bb 7088 case EXCP_UDEF:
2fb50a33 7089 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7090 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
a25dc805 7091 break;
7517748e 7092 case EXCP_NOCP:
2fb50a33 7093 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7094 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
a25dc805 7095 break;
e13886e3 7096 case EXCP_INVSTATE:
2fb50a33 7097 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 7098 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
e13886e3 7099 break;
9ee6e8bb 7100 case EXCP_SWI:
314e2296 7101 /* The PC already points to the next instruction. */
2fb50a33 7102 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
a25dc805 7103 break;
9ee6e8bb
PB
7104 case EXCP_PREFETCH_ABORT:
7105 case EXCP_DATA_ABORT:
5dd0641d
MD
7106 /* Note that for M profile we don't have a guest facing FSR, but
7107 * the env->exception.fsr will be populated by the code that
7108 * raises the fault, in the A profile short-descriptor format.
abf1172f 7109 */
5dd0641d 7110 switch (env->exception.fsr & 0xf) {
35337cc3
PM
7111 case M_FAKE_FSR_NSC_EXEC:
7112 /* Exception generated when we try to execute code at an address
7113 * which is marked as Secure & Non-Secure Callable and the CPU
7114 * is in the Non-Secure state. The only instruction which can
7115 * be executed like this is SG (and that only if both halves of
7116 * the SG instruction have the same security attributes.)
7117 * Everything else must generate an INVEP SecureFault, so we
7118 * emulate the SG instruction here.
35337cc3 7119 */
333e10c5
PM
7120 if (v7m_handle_execute_nsc(cpu)) {
7121 return;
7122 }
35337cc3
PM
7123 break;
7124 case M_FAKE_FSR_SFAULT:
7125 /* Various flavours of SecureFault for attempts to execute or
7126 * access data in the wrong security state.
7127 */
7128 switch (cs->exception_index) {
7129 case EXCP_PREFETCH_ABORT:
7130 if (env->v7m.secure) {
7131 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7132 qemu_log_mask(CPU_LOG_INT,
7133 "...really SecureFault with SFSR.INVTRAN\n");
7134 } else {
7135 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7136 qemu_log_mask(CPU_LOG_INT,
7137 "...really SecureFault with SFSR.INVEP\n");
7138 }
7139 break;
7140 case EXCP_DATA_ABORT:
7141 /* This must be an NS access to S memory */
7142 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7143 qemu_log_mask(CPU_LOG_INT,
7144 "...really SecureFault with SFSR.AUVIOL\n");
7145 break;
7146 }
7147 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7148 break;
5dd0641d
MD
7149 case 0x8: /* External Abort */
7150 switch (cs->exception_index) {
7151 case EXCP_PREFETCH_ABORT:
c6158878
PM
7152 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7153 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
5dd0641d
MD
7154 break;
7155 case EXCP_DATA_ABORT:
334e8dad 7156 env->v7m.cfsr[M_REG_NS] |=
c6158878 7157 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
5dd0641d
MD
7158 env->v7m.bfar = env->exception.vaddress;
7159 qemu_log_mask(CPU_LOG_INT,
c6158878 7160 "...with CFSR.PRECISERR and BFAR 0x%x\n",
5dd0641d
MD
7161 env->v7m.bfar);
7162 break;
7163 }
2fb50a33 7164 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
5dd0641d
MD
7165 break;
7166 default:
7167 /* All other FSR values are either MPU faults or "can't happen
7168 * for M profile" cases.
7169 */
7170 switch (cs->exception_index) {
7171 case EXCP_PREFETCH_ABORT:
334e8dad 7172 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
5dd0641d
MD
7173 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7174 break;
7175 case EXCP_DATA_ABORT:
334e8dad 7176 env->v7m.cfsr[env->v7m.secure] |=
5dd0641d 7177 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
c51a5cfc 7178 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
5dd0641d
MD
7179 qemu_log_mask(CPU_LOG_INT,
7180 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
c51a5cfc 7181 env->v7m.mmfar[env->v7m.secure]);
5dd0641d
MD
7182 break;
7183 }
2fb50a33
PM
7184 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7185 env->v7m.secure);
5dd0641d
MD
7186 break;
7187 }
a25dc805 7188 break;
9ee6e8bb 7189 case EXCP_BKPT:
cfe67cef 7190 if (semihosting_enabled()) {
2ad207d4 7191 int nr;
f9fd40eb 7192 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
7193 if (nr == 0xab) {
7194 env->regs[15] += 2;
205ace55
CC
7195 qemu_log_mask(CPU_LOG_INT,
7196 "...handling as semihosting call 0x%x\n",
7197 env->regs[0]);
2ad207d4
PB
7198 env->regs[0] = do_arm_semihosting(env);
7199 return;
7200 }
7201 }
2fb50a33 7202 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
a25dc805 7203 break;
9ee6e8bb 7204 case EXCP_IRQ:
9ee6e8bb
PB
7205 break;
7206 case EXCP_EXCEPTION_EXIT:
d02a8698
PM
7207 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7208 /* Must be v8M security extension function return */
7209 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7210 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7211 if (do_v7m_function_return(cpu)) {
7212 return;
7213 }
7214 } else {
7215 do_v7m_exception_exit(cpu);
7216 return;
7217 }
7218 break;
9ee6e8bb 7219 default:
a47dddd7 7220 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
7221 return; /* Never happens. Keep compiler happy. */
7222 }
7223
d3392718
PM
7224 if (arm_feature(env, ARM_FEATURE_V8)) {
7225 lr = R_V7M_EXCRET_RES1_MASK |
7226 R_V7M_EXCRET_DCRS_MASK |
7227 R_V7M_EXCRET_FTYPE_MASK;
7228 /* The S bit indicates whether we should return to Secure
7229 * or NonSecure (ie our current state).
7230 * The ES bit indicates whether we're taking this exception
7231 * to Secure or NonSecure (ie our target state). We set it
7232 * later, in v7m_exception_taken().
7233 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7234 * This corresponds to the ARM ARM pseudocode for v8M setting
7235 * some LR bits in PushStack() and some in ExceptionTaken();
7236 * the distinction matters for the tailchain cases where we
7237 * can take an exception without pushing the stack.
7238 */
7239 if (env->v7m.secure) {
7240 lr |= R_V7M_EXCRET_S_MASK;
7241 }
7242 } else {
7243 lr = R_V7M_EXCRET_RES1_MASK |
7244 R_V7M_EXCRET_S_MASK |
7245 R_V7M_EXCRET_DCRS_MASK |
7246 R_V7M_EXCRET_FTYPE_MASK |
7247 R_V7M_EXCRET_ES_MASK;
7248 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7249 lr |= R_V7M_EXCRET_SPSEL_MASK;
7250 }
bd70b29b 7251 }
15b3f556 7252 if (!arm_v7m_is_handler_mode(env)) {
4d1e7a47 7253 lr |= R_V7M_EXCRET_MODE_MASK;
bd70b29b
PM
7254 }
7255
39ae2474 7256 v7m_push_stack(cpu);
d3392718 7257 v7m_exception_taken(cpu, lr, false);
a25dc805 7258 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
9ee6e8bb
PB
7259}
7260
ce02049d
GB
7261/* Function used to synchronize QEMU's AArch64 register set with AArch32
7262 * register set. This is necessary when switching between AArch32 and AArch64
7263 * execution state.
7264 */
7265void aarch64_sync_32_to_64(CPUARMState *env)
7266{
7267 int i;
7268 uint32_t mode = env->uncached_cpsr & CPSR_M;
7269
7270 /* We can blanket copy R[0:7] to X[0:7] */
7271 for (i = 0; i < 8; i++) {
7272 env->xregs[i] = env->regs[i];
7273 }
7274
7275 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7276 * Otherwise, they come from the banked user regs.
7277 */
7278 if (mode == ARM_CPU_MODE_FIQ) {
7279 for (i = 8; i < 13; i++) {
7280 env->xregs[i] = env->usr_regs[i - 8];
7281 }
7282 } else {
7283 for (i = 8; i < 13; i++) {
7284 env->xregs[i] = env->regs[i];
7285 }
7286 }
7287
7288 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7289 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7290 * from the mode banked register.
7291 */
7292 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7293 env->xregs[13] = env->regs[13];
7294 env->xregs[14] = env->regs[14];
7295 } else {
7296 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7297 /* HYP is an exception in that it is copied from r14 */
7298 if (mode == ARM_CPU_MODE_HYP) {
7299 env->xregs[14] = env->regs[14];
7300 } else {
7301 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7302 }
7303 }
7304
7305 if (mode == ARM_CPU_MODE_HYP) {
7306 env->xregs[15] = env->regs[13];
7307 } else {
7308 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7309 }
7310
7311 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7312 env->xregs[16] = env->regs[14];
7313 env->xregs[17] = env->regs[13];
ce02049d 7314 } else {
3a9148d0
SS
7315 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7316 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
7317 }
7318
7319 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7320 env->xregs[18] = env->regs[14];
7321 env->xregs[19] = env->regs[13];
ce02049d 7322 } else {
3a9148d0
SS
7323 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7324 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
7325 }
7326
7327 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7328 env->xregs[20] = env->regs[14];
7329 env->xregs[21] = env->regs[13];
ce02049d 7330 } else {
3a9148d0
SS
7331 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7332 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
7333 }
7334
7335 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7336 env->xregs[22] = env->regs[14];
7337 env->xregs[23] = env->regs[13];
ce02049d 7338 } else {
3a9148d0
SS
7339 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7340 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
7341 }
7342
7343 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7344 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7345 * FIQ bank for r8-r14.
7346 */
7347 if (mode == ARM_CPU_MODE_FIQ) {
7348 for (i = 24; i < 31; i++) {
7349 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7350 }
7351 } else {
7352 for (i = 24; i < 29; i++) {
7353 env->xregs[i] = env->fiq_regs[i - 24];
7354 }
7355 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7356 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7357 }
7358
7359 env->pc = env->regs[15];
7360}
7361
7362/* Function used to synchronize QEMU's AArch32 register set with AArch64
7363 * register set. This is necessary when switching between AArch32 and AArch64
7364 * execution state.
7365 */
7366void aarch64_sync_64_to_32(CPUARMState *env)
7367{
7368 int i;
7369 uint32_t mode = env->uncached_cpsr & CPSR_M;
7370
7371 /* We can blanket copy X[0:7] to R[0:7] */
7372 for (i = 0; i < 8; i++) {
7373 env->regs[i] = env->xregs[i];
7374 }
7375
7376 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7377 * Otherwise, we copy x8-x12 into the banked user regs.
7378 */
7379 if (mode == ARM_CPU_MODE_FIQ) {
7380 for (i = 8; i < 13; i++) {
7381 env->usr_regs[i - 8] = env->xregs[i];
7382 }
7383 } else {
7384 for (i = 8; i < 13; i++) {
7385 env->regs[i] = env->xregs[i];
7386 }
7387 }
7388
7389 /* Registers r13 & r14 depend on the current mode.
7390 * If we are in a given mode, we copy the corresponding x registers to r13
7391 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7392 * for the mode.
7393 */
7394 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7395 env->regs[13] = env->xregs[13];
7396 env->regs[14] = env->xregs[14];
7397 } else {
7398 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7399
7400 /* HYP is an exception in that it does not have its own banked r14 but
7401 * shares the USR r14
7402 */
7403 if (mode == ARM_CPU_MODE_HYP) {
7404 env->regs[14] = env->xregs[14];
7405 } else {
7406 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7407 }
7408 }
7409
7410 if (mode == ARM_CPU_MODE_HYP) {
7411 env->regs[13] = env->xregs[15];
7412 } else {
7413 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7414 }
7415
7416 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7417 env->regs[14] = env->xregs[16];
7418 env->regs[13] = env->xregs[17];
ce02049d 7419 } else {
3a9148d0
SS
7420 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7421 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
7422 }
7423
7424 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7425 env->regs[14] = env->xregs[18];
7426 env->regs[13] = env->xregs[19];
ce02049d 7427 } else {
3a9148d0
SS
7428 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7429 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
7430 }
7431
7432 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7433 env->regs[14] = env->xregs[20];
7434 env->regs[13] = env->xregs[21];
ce02049d 7435 } else {
3a9148d0
SS
7436 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7437 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
7438 }
7439
7440 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7441 env->regs[14] = env->xregs[22];
7442 env->regs[13] = env->xregs[23];
ce02049d 7443 } else {
3a9148d0
SS
7444 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7445 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
7446 }
7447
7448 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7449 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7450 * FIQ bank for r8-r14.
7451 */
7452 if (mode == ARM_CPU_MODE_FIQ) {
7453 for (i = 24; i < 31; i++) {
7454 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7455 }
7456 } else {
7457 for (i = 24; i < 29; i++) {
7458 env->fiq_regs[i - 24] = env->xregs[i];
7459 }
7460 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7461 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7462 }
7463
7464 env->regs[15] = env->pc;
7465}
7466
966f758c 7467static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 7468{
97a8ea5a
AF
7469 ARMCPU *cpu = ARM_CPU(cs);
7470 CPUARMState *env = &cpu->env;
b5ff1b31
FB
7471 uint32_t addr;
7472 uint32_t mask;
7473 int new_mode;
7474 uint32_t offset;
16a906fd 7475 uint32_t moe;
b5ff1b31 7476
16a906fd
PM
7477 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7478 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7479 case EC_BREAKPOINT:
7480 case EC_BREAKPOINT_SAME_EL:
7481 moe = 1;
7482 break;
7483 case EC_WATCHPOINT:
7484 case EC_WATCHPOINT_SAME_EL:
7485 moe = 10;
7486 break;
7487 case EC_AA32_BKPT:
7488 moe = 3;
7489 break;
7490 case EC_VECTORCATCH:
7491 moe = 5;
7492 break;
7493 default:
7494 moe = 0;
7495 break;
7496 }
7497
7498 if (moe) {
7499 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7500 }
7501
b5ff1b31 7502 /* TODO: Vectored interrupt controller. */
27103424 7503 switch (cs->exception_index) {
b5ff1b31
FB
7504 case EXCP_UDEF:
7505 new_mode = ARM_CPU_MODE_UND;
7506 addr = 0x04;
7507 mask = CPSR_I;
7508 if (env->thumb)
7509 offset = 2;
7510 else
7511 offset = 4;
7512 break;
7513 case EXCP_SWI:
7514 new_mode = ARM_CPU_MODE_SVC;
7515 addr = 0x08;
7516 mask = CPSR_I;
601d70b9 7517 /* The PC already points to the next instruction. */
b5ff1b31
FB
7518 offset = 0;
7519 break;
06c949e6 7520 case EXCP_BKPT:
abf1172f 7521 env->exception.fsr = 2;
9ee6e8bb
PB
7522 /* Fall through to prefetch abort. */
7523 case EXCP_PREFETCH_ABORT:
88ca1c2d 7524 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 7525 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 7526 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 7527 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7528 new_mode = ARM_CPU_MODE_ABT;
7529 addr = 0x0c;
7530 mask = CPSR_A | CPSR_I;
7531 offset = 4;
7532 break;
7533 case EXCP_DATA_ABORT:
4a7e2d73 7534 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 7535 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 7536 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 7537 env->exception.fsr,
6cd8a264 7538 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7539 new_mode = ARM_CPU_MODE_ABT;
7540 addr = 0x10;
7541 mask = CPSR_A | CPSR_I;
7542 offset = 8;
7543 break;
7544 case EXCP_IRQ:
7545 new_mode = ARM_CPU_MODE_IRQ;
7546 addr = 0x18;
7547 /* Disable IRQ and imprecise data aborts. */
7548 mask = CPSR_A | CPSR_I;
7549 offset = 4;
de38d23b
FA
7550 if (env->cp15.scr_el3 & SCR_IRQ) {
7551 /* IRQ routed to monitor mode */
7552 new_mode = ARM_CPU_MODE_MON;
7553 mask |= CPSR_F;
7554 }
b5ff1b31
FB
7555 break;
7556 case EXCP_FIQ:
7557 new_mode = ARM_CPU_MODE_FIQ;
7558 addr = 0x1c;
7559 /* Disable FIQ, IRQ and imprecise data aborts. */
7560 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
7561 if (env->cp15.scr_el3 & SCR_FIQ) {
7562 /* FIQ routed to monitor mode */
7563 new_mode = ARM_CPU_MODE_MON;
7564 }
b5ff1b31
FB
7565 offset = 4;
7566 break;
87a4b270
PM
7567 case EXCP_VIRQ:
7568 new_mode = ARM_CPU_MODE_IRQ;
7569 addr = 0x18;
7570 /* Disable IRQ and imprecise data aborts. */
7571 mask = CPSR_A | CPSR_I;
7572 offset = 4;
7573 break;
7574 case EXCP_VFIQ:
7575 new_mode = ARM_CPU_MODE_FIQ;
7576 addr = 0x1c;
7577 /* Disable FIQ, IRQ and imprecise data aborts. */
7578 mask = CPSR_A | CPSR_I | CPSR_F;
7579 offset = 4;
7580 break;
dbe9d163
FA
7581 case EXCP_SMC:
7582 new_mode = ARM_CPU_MODE_MON;
7583 addr = 0x08;
7584 mask = CPSR_A | CPSR_I | CPSR_F;
7585 offset = 0;
7586 break;
b5ff1b31 7587 default:
a47dddd7 7588 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
7589 return; /* Never happens. Keep compiler happy. */
7590 }
e89e51a1
FA
7591
7592 if (new_mode == ARM_CPU_MODE_MON) {
7593 addr += env->cp15.mvbar;
137feaa9 7594 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 7595 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 7596 addr += 0xffff0000;
8641136c
NR
7597 } else {
7598 /* ARM v7 architectures provide a vector base address register to remap
7599 * the interrupt vector table.
e89e51a1 7600 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
7601 * Note: only bits 31:5 are valid.
7602 */
fb6c91ba 7603 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 7604 }
dbe9d163
FA
7605
7606 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
7607 env->cp15.scr_el3 &= ~SCR_NS;
7608 }
7609
b5ff1b31 7610 switch_mode (env, new_mode);
662cefb7
PM
7611 /* For exceptions taken to AArch32 we must clear the SS bit in both
7612 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
7613 */
7614 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 7615 env->spsr = cpsr_read(env);
9ee6e8bb
PB
7616 /* Clear IT bits. */
7617 env->condexec_bits = 0;
30a8cac1 7618 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 7619 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
7620 /* Set new mode endianness */
7621 env->uncached_cpsr &= ~CPSR_E;
7622 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
3823b9db 7623 env->uncached_cpsr |= CPSR_E;
73462ddd 7624 }
4cc35614 7625 env->daif |= mask;
be5e7a76
DES
7626 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
7627 * and we should just guard the thumb mode on V4 */
7628 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 7629 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 7630 }
b5ff1b31
FB
7631 env->regs[14] = env->regs[15] + offset;
7632 env->regs[15] = addr;
b5ff1b31
FB
7633}
7634
966f758c
PM
7635/* Handle exception entry to a target EL which is using AArch64 */
7636static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
7637{
7638 ARMCPU *cpu = ARM_CPU(cs);
7639 CPUARMState *env = &cpu->env;
7640 unsigned int new_el = env->exception.target_el;
7641 target_ulong addr = env->cp15.vbar_el[new_el];
7642 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
7643
7644 if (arm_current_el(env) < new_el) {
3d6f7617
PM
7645 /* Entry vector offset depends on whether the implemented EL
7646 * immediately lower than the target level is using AArch32 or AArch64
7647 */
7648 bool is_aa64;
7649
7650 switch (new_el) {
7651 case 3:
7652 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
7653 break;
7654 case 2:
7655 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
7656 break;
7657 case 1:
7658 is_aa64 = is_a64(env);
7659 break;
7660 default:
7661 g_assert_not_reached();
7662 }
7663
7664 if (is_aa64) {
f3a9b694
PM
7665 addr += 0x400;
7666 } else {
7667 addr += 0x600;
7668 }
7669 } else if (pstate_read(env) & PSTATE_SP) {
7670 addr += 0x200;
7671 }
7672
f3a9b694
PM
7673 switch (cs->exception_index) {
7674 case EXCP_PREFETCH_ABORT:
7675 case EXCP_DATA_ABORT:
7676 env->cp15.far_el[new_el] = env->exception.vaddress;
7677 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
7678 env->cp15.far_el[new_el]);
7679 /* fall through */
7680 case EXCP_BKPT:
7681 case EXCP_UDEF:
7682 case EXCP_SWI:
7683 case EXCP_HVC:
7684 case EXCP_HYP_TRAP:
7685 case EXCP_SMC:
7686 env->cp15.esr_el[new_el] = env->exception.syndrome;
7687 break;
7688 case EXCP_IRQ:
7689 case EXCP_VIRQ:
7690 addr += 0x80;
7691 break;
7692 case EXCP_FIQ:
7693 case EXCP_VFIQ:
7694 addr += 0x100;
7695 break;
7696 case EXCP_SEMIHOST:
7697 qemu_log_mask(CPU_LOG_INT,
7698 "...handling as semihosting call 0x%" PRIx64 "\n",
7699 env->xregs[0]);
7700 env->xregs[0] = do_arm_semihosting(env);
7701 return;
7702 default:
7703 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7704 }
7705
7706 if (is_a64(env)) {
7707 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
7708 aarch64_save_sp(env, arm_current_el(env));
7709 env->elr_el[new_el] = env->pc;
7710 } else {
7711 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
f3a9b694
PM
7712 env->elr_el[new_el] = env->regs[15];
7713
7714 aarch64_sync_32_to_64(env);
7715
7716 env->condexec_bits = 0;
7717 }
7718 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
7719 env->elr_el[new_el]);
7720
7721 pstate_write(env, PSTATE_DAIF | new_mode);
7722 env->aarch64 = 1;
7723 aarch64_restore_sp(env, new_el);
7724
7725 env->pc = addr;
7726
7727 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
7728 new_el, env->pc, pstate_read(env));
966f758c
PM
7729}
7730
904c04de
PM
7731static inline bool check_for_semihosting(CPUState *cs)
7732{
7733 /* Check whether this exception is a semihosting call; if so
7734 * then handle it and return true; otherwise return false.
7735 */
7736 ARMCPU *cpu = ARM_CPU(cs);
7737 CPUARMState *env = &cpu->env;
7738
7739 if (is_a64(env)) {
7740 if (cs->exception_index == EXCP_SEMIHOST) {
7741 /* This is always the 64-bit semihosting exception.
7742 * The "is this usermode" and "is semihosting enabled"
7743 * checks have been done at translate time.
7744 */
7745 qemu_log_mask(CPU_LOG_INT,
7746 "...handling as semihosting call 0x%" PRIx64 "\n",
7747 env->xregs[0]);
7748 env->xregs[0] = do_arm_semihosting(env);
7749 return true;
7750 }
7751 return false;
7752 } else {
7753 uint32_t imm;
7754
7755 /* Only intercept calls from privileged modes, to provide some
7756 * semblance of security.
7757 */
19a6e31c
PM
7758 if (cs->exception_index != EXCP_SEMIHOST &&
7759 (!semihosting_enabled() ||
7760 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
904c04de
PM
7761 return false;
7762 }
7763
7764 switch (cs->exception_index) {
19a6e31c
PM
7765 case EXCP_SEMIHOST:
7766 /* This is always a semihosting call; the "is this usermode"
7767 * and "is semihosting enabled" checks have been done at
7768 * translate time.
7769 */
7770 break;
904c04de
PM
7771 case EXCP_SWI:
7772 /* Check for semihosting interrupt. */
7773 if (env->thumb) {
f9fd40eb 7774 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
7775 & 0xff;
7776 if (imm == 0xab) {
7777 break;
7778 }
7779 } else {
f9fd40eb 7780 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
7781 & 0xffffff;
7782 if (imm == 0x123456) {
7783 break;
7784 }
7785 }
7786 return false;
7787 case EXCP_BKPT:
7788 /* See if this is a semihosting syscall. */
7789 if (env->thumb) {
f9fd40eb 7790 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
7791 & 0xff;
7792 if (imm == 0xab) {
7793 env->regs[15] += 2;
7794 break;
7795 }
7796 }
7797 return false;
7798 default:
7799 return false;
7800 }
7801
7802 qemu_log_mask(CPU_LOG_INT,
7803 "...handling as semihosting call 0x%x\n",
7804 env->regs[0]);
7805 env->regs[0] = do_arm_semihosting(env);
7806 return true;
7807 }
7808}
7809
966f758c
PM
7810/* Handle a CPU exception for A and R profile CPUs.
7811 * Do any appropriate logging, handle PSCI calls, and then hand off
7812 * to the AArch64-entry or AArch32-entry function depending on the
7813 * target exception level's register width.
7814 */
7815void arm_cpu_do_interrupt(CPUState *cs)
7816{
7817 ARMCPU *cpu = ARM_CPU(cs);
7818 CPUARMState *env = &cpu->env;
7819 unsigned int new_el = env->exception.target_el;
7820
531c60a9 7821 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
7822
7823 arm_log_exception(cs->exception_index);
7824 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
7825 new_el);
7826 if (qemu_loglevel_mask(CPU_LOG_INT)
7827 && !excp_is_internal(cs->exception_index)) {
6568da45 7828 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
966f758c
PM
7829 env->exception.syndrome >> ARM_EL_EC_SHIFT,
7830 env->exception.syndrome);
7831 }
7832
7833 if (arm_is_psci_call(cpu, cs->exception_index)) {
7834 arm_handle_psci_call(cpu);
7835 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
7836 return;
7837 }
7838
904c04de
PM
7839 /* Semihosting semantics depend on the register width of the
7840 * code that caused the exception, not the target exception level,
7841 * so must be handled here.
966f758c 7842 */
904c04de
PM
7843 if (check_for_semihosting(cs)) {
7844 return;
7845 }
7846
7847 assert(!excp_is_internal(cs->exception_index));
7848 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
7849 arm_cpu_do_interrupt_aarch64(cs);
7850 } else {
7851 arm_cpu_do_interrupt_aarch32(cs);
7852 }
f3a9b694 7853
8d04fb55
JK
7854 /* Hooks may change global state so BQL should be held, also the
7855 * BQL needs to be held for any modification of
7856 * cs->interrupt_request.
7857 */
7858 g_assert(qemu_mutex_iothread_locked());
7859
bd7d00fc
PM
7860 arm_call_el_change_hook(cpu);
7861
f3a9b694
PM
7862 if (!kvm_enabled()) {
7863 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
7864 }
7865}
0480f69a
PM
7866
7867/* Return the exception level which controls this address translation regime */
7868static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
7869{
7870 switch (mmu_idx) {
7871 case ARMMMUIdx_S2NS:
7872 case ARMMMUIdx_S1E2:
7873 return 2;
7874 case ARMMMUIdx_S1E3:
7875 return 3;
7876 case ARMMMUIdx_S1SE0:
7877 return arm_el_is_aa64(env, 3) ? 1 : 3;
7878 case ARMMMUIdx_S1SE1:
7879 case ARMMMUIdx_S1NSE0:
7880 case ARMMMUIdx_S1NSE1:
62593718
PM
7881 case ARMMMUIdx_MPrivNegPri:
7882 case ARMMMUIdx_MUserNegPri:
e7b921c2
PM
7883 case ARMMMUIdx_MPriv:
7884 case ARMMMUIdx_MUser:
62593718
PM
7885 case ARMMMUIdx_MSPrivNegPri:
7886 case ARMMMUIdx_MSUserNegPri:
66787c78 7887 case ARMMMUIdx_MSPriv:
66787c78 7888 case ARMMMUIdx_MSUser:
0480f69a
PM
7889 return 1;
7890 default:
7891 g_assert_not_reached();
7892 }
7893}
7894
7895/* Return the SCTLR value which controls this address translation regime */
7896static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
7897{
7898 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
7899}
7900
7901/* Return true if the specified stage of address translation is disabled */
7902static inline bool regime_translation_disabled(CPUARMState *env,
7903 ARMMMUIdx mmu_idx)
7904{
29c483a5 7905 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 7906 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
7907 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
7908 case R_V7M_MPU_CTRL_ENABLE_MASK:
7909 /* Enabled, but not for HardFault and NMI */
62593718 7910 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
7911 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
7912 /* Enabled for all cases */
7913 return false;
7914 case 0:
7915 default:
7916 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
7917 * we warned about that in armv7m_nvic.c when the guest set it.
7918 */
7919 return true;
7920 }
29c483a5
MD
7921 }
7922
0480f69a
PM
7923 if (mmu_idx == ARMMMUIdx_S2NS) {
7924 return (env->cp15.hcr_el2 & HCR_VM) == 0;
7925 }
7926 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
7927}
7928
73462ddd
PC
7929static inline bool regime_translation_big_endian(CPUARMState *env,
7930 ARMMMUIdx mmu_idx)
7931{
7932 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
7933}
7934
0480f69a
PM
7935/* Return the TCR controlling this translation regime */
7936static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
7937{
7938 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 7939 return &env->cp15.vtcr_el2;
0480f69a
PM
7940 }
7941 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
7942}
7943
8bd5c820
PM
7944/* Convert a possible stage1+2 MMU index into the appropriate
7945 * stage 1 MMU index
7946 */
7947static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
7948{
7949 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
7950 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
7951 }
7952 return mmu_idx;
7953}
7954
86fb3fa4
TH
7955/* Returns TBI0 value for current regime el */
7956uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
7957{
7958 TCR *tcr;
7959 uint32_t el;
7960
7961 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
7962 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7963 */
7964 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
7965
7966 tcr = regime_tcr(env, mmu_idx);
7967 el = regime_el(env, mmu_idx);
7968
7969 if (el > 1) {
7970 return extract64(tcr->raw_tcr, 20, 1);
7971 } else {
7972 return extract64(tcr->raw_tcr, 37, 1);
7973 }
7974}
7975
7976/* Returns TBI1 value for current regime el */
7977uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
7978{
7979 TCR *tcr;
7980 uint32_t el;
7981
7982 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
7983 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7984 */
7985 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
7986
7987 tcr = regime_tcr(env, mmu_idx);
7988 el = regime_el(env, mmu_idx);
7989
7990 if (el > 1) {
7991 return 0;
7992 } else {
7993 return extract64(tcr->raw_tcr, 38, 1);
7994 }
7995}
7996
aef878be
GB
7997/* Return the TTBR associated with this translation regime */
7998static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
7999 int ttbrn)
8000{
8001 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 8002 return env->cp15.vttbr_el2;
aef878be
GB
8003 }
8004 if (ttbrn == 0) {
8005 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8006 } else {
8007 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8008 }
8009}
8010
0480f69a
PM
8011/* Return true if the translation regime is using LPAE format page tables */
8012static inline bool regime_using_lpae_format(CPUARMState *env,
8013 ARMMMUIdx mmu_idx)
8014{
8015 int el = regime_el(env, mmu_idx);
8016 if (el == 2 || arm_el_is_aa64(env, el)) {
8017 return true;
8018 }
8019 if (arm_feature(env, ARM_FEATURE_LPAE)
8020 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8021 return true;
8022 }
8023 return false;
8024}
8025
deb2db99
AR
8026/* Returns true if the stage 1 translation regime is using LPAE format page
8027 * tables. Used when raising alignment exceptions, whose FSR changes depending
8028 * on whether the long or short descriptor format is in use. */
8029bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 8030{
8bd5c820 8031 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 8032
30901475
AB
8033 return regime_using_lpae_format(env, mmu_idx);
8034}
8035
0480f69a
PM
8036static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8037{
8038 switch (mmu_idx) {
8039 case ARMMMUIdx_S1SE0:
8040 case ARMMMUIdx_S1NSE0:
e7b921c2 8041 case ARMMMUIdx_MUser:
871bec7c 8042 case ARMMMUIdx_MSUser:
62593718
PM
8043 case ARMMMUIdx_MUserNegPri:
8044 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
8045 return true;
8046 default:
8047 return false;
8048 case ARMMMUIdx_S12NSE0:
8049 case ARMMMUIdx_S12NSE1:
8050 g_assert_not_reached();
8051 }
8052}
8053
0fbf5238
AJ
8054/* Translate section/page access permissions to page
8055 * R/W protection flags
d76951b6
AJ
8056 *
8057 * @env: CPUARMState
8058 * @mmu_idx: MMU index indicating required translation regime
8059 * @ap: The 3-bit access permissions (AP[2:0])
8060 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
8061 */
8062static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8063 int ap, int domain_prot)
8064{
554b0b09
PM
8065 bool is_user = regime_is_user(env, mmu_idx);
8066
8067 if (domain_prot == 3) {
8068 return PAGE_READ | PAGE_WRITE;
8069 }
8070
554b0b09
PM
8071 switch (ap) {
8072 case 0:
8073 if (arm_feature(env, ARM_FEATURE_V7)) {
8074 return 0;
8075 }
554b0b09
PM
8076 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8077 case SCTLR_S:
8078 return is_user ? 0 : PAGE_READ;
8079 case SCTLR_R:
8080 return PAGE_READ;
8081 default:
8082 return 0;
8083 }
8084 case 1:
8085 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8086 case 2:
87c3d486 8087 if (is_user) {
0fbf5238 8088 return PAGE_READ;
87c3d486 8089 } else {
554b0b09 8090 return PAGE_READ | PAGE_WRITE;
87c3d486 8091 }
554b0b09
PM
8092 case 3:
8093 return PAGE_READ | PAGE_WRITE;
8094 case 4: /* Reserved. */
8095 return 0;
8096 case 5:
0fbf5238 8097 return is_user ? 0 : PAGE_READ;
554b0b09 8098 case 6:
0fbf5238 8099 return PAGE_READ;
554b0b09 8100 case 7:
87c3d486 8101 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 8102 return 0;
87c3d486 8103 }
0fbf5238 8104 return PAGE_READ;
554b0b09 8105 default:
0fbf5238 8106 g_assert_not_reached();
554b0b09 8107 }
b5ff1b31
FB
8108}
8109
d76951b6
AJ
8110/* Translate section/page access permissions to page
8111 * R/W protection flags.
8112 *
d76951b6 8113 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 8114 * @is_user: TRUE if accessing from PL0
d76951b6 8115 */
d8e052b3 8116static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 8117{
d76951b6
AJ
8118 switch (ap) {
8119 case 0:
8120 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8121 case 1:
8122 return PAGE_READ | PAGE_WRITE;
8123 case 2:
8124 return is_user ? 0 : PAGE_READ;
8125 case 3:
8126 return PAGE_READ;
8127 default:
8128 g_assert_not_reached();
8129 }
8130}
8131
d8e052b3
AJ
8132static inline int
8133simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8134{
8135 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8136}
8137
6ab1a5ee
EI
8138/* Translate S2 section/page access permissions to protection flags
8139 *
8140 * @env: CPUARMState
8141 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8142 * @xn: XN (execute-never) bit
8143 */
8144static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8145{
8146 int prot = 0;
8147
8148 if (s2ap & 1) {
8149 prot |= PAGE_READ;
8150 }
8151 if (s2ap & 2) {
8152 prot |= PAGE_WRITE;
8153 }
8154 if (!xn) {
dfda6837
SS
8155 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8156 prot |= PAGE_EXEC;
8157 }
6ab1a5ee
EI
8158 }
8159 return prot;
8160}
8161
d8e052b3
AJ
8162/* Translate section/page access permissions to protection flags
8163 *
8164 * @env: CPUARMState
8165 * @mmu_idx: MMU index indicating required translation regime
8166 * @is_aa64: TRUE if AArch64
8167 * @ap: The 2-bit simple AP (AP[2:1])
8168 * @ns: NS (non-secure) bit
8169 * @xn: XN (execute-never) bit
8170 * @pxn: PXN (privileged execute-never) bit
8171 */
8172static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8173 int ap, int ns, int xn, int pxn)
8174{
8175 bool is_user = regime_is_user(env, mmu_idx);
8176 int prot_rw, user_rw;
8177 bool have_wxn;
8178 int wxn = 0;
8179
8180 assert(mmu_idx != ARMMMUIdx_S2NS);
8181
8182 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8183 if (is_user) {
8184 prot_rw = user_rw;
8185 } else {
8186 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8187 }
8188
8189 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8190 return prot_rw;
8191 }
8192
8193 /* TODO have_wxn should be replaced with
8194 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8195 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8196 * compatible processors have EL2, which is required for [U]WXN.
8197 */
8198 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8199
8200 if (have_wxn) {
8201 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8202 }
8203
8204 if (is_aa64) {
8205 switch (regime_el(env, mmu_idx)) {
8206 case 1:
8207 if (!is_user) {
8208 xn = pxn || (user_rw & PAGE_WRITE);
8209 }
8210 break;
8211 case 2:
8212 case 3:
8213 break;
8214 }
8215 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8216 switch (regime_el(env, mmu_idx)) {
8217 case 1:
8218 case 3:
8219 if (is_user) {
8220 xn = xn || !(user_rw & PAGE_READ);
8221 } else {
8222 int uwxn = 0;
8223 if (have_wxn) {
8224 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8225 }
8226 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8227 (uwxn && (user_rw & PAGE_WRITE));
8228 }
8229 break;
8230 case 2:
8231 break;
8232 }
8233 } else {
8234 xn = wxn = 0;
8235 }
8236
8237 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8238 return prot_rw;
8239 }
8240 return prot_rw | PAGE_EXEC;
8241}
8242
0480f69a
PM
8243static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8244 uint32_t *table, uint32_t address)
b2fa1797 8245{
0480f69a 8246 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 8247 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 8248
11f136ee
FA
8249 if (address & tcr->mask) {
8250 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
8251 /* Translation table walk disabled for TTBR1 */
8252 return false;
8253 }
aef878be 8254 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 8255 } else {
11f136ee 8256 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
8257 /* Translation table walk disabled for TTBR0 */
8258 return false;
8259 }
aef878be 8260 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
8261 }
8262 *table |= (address >> 18) & 0x3ffc;
8263 return true;
b2fa1797
PB
8264}
8265
37785977
EI
8266/* Translate a S1 pagetable walk through S2 if needed. */
8267static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8268 hwaddr addr, MemTxAttrs txattrs,
37785977
EI
8269 ARMMMUFaultInfo *fi)
8270{
8271 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8272 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8273 target_ulong s2size;
8274 hwaddr s2pa;
8275 int s2prot;
8276 int ret;
3795a6de 8277 uint32_t fsr;
37785977
EI
8278
8279 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
3795a6de 8280 &txattrs, &s2prot, &s2size, &fsr, fi, NULL);
37785977
EI
8281 if (ret) {
8282 fi->s2addr = addr;
8283 fi->stage2 = true;
8284 fi->s1ptw = true;
8285 return ~0;
8286 }
8287 addr = s2pa;
8288 }
8289 return addr;
8290}
8291
ebca90e4
PM
8292/* All loads done in the course of a page table walk go through here.
8293 * TODO: rather than ignoring errors from physical memory reads (which
8294 * are external aborts in ARM terminology) we should propagate this
8295 * error out so that we can turn it into a Data Abort if this walk
8296 * was being done for a CPU load/store or an address translation instruction
8297 * (but not if it was for a debug access).
8298 */
a614e698 8299static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8300 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8301{
a614e698
EI
8302 ARMCPU *cpu = ARM_CPU(cs);
8303 CPUARMState *env = &cpu->env;
ebca90e4 8304 MemTxAttrs attrs = {};
5ce4ff65 8305 AddressSpace *as;
ebca90e4
PM
8306
8307 attrs.secure = is_secure;
5ce4ff65 8308 as = arm_addressspace(cs, attrs);
3795a6de 8309 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
a614e698
EI
8310 if (fi->s1ptw) {
8311 return 0;
8312 }
73462ddd
PC
8313 if (regime_translation_big_endian(env, mmu_idx)) {
8314 return address_space_ldl_be(as, addr, attrs, NULL);
8315 } else {
8316 return address_space_ldl_le(as, addr, attrs, NULL);
8317 }
ebca90e4
PM
8318}
8319
37785977 8320static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 8321 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 8322{
37785977
EI
8323 ARMCPU *cpu = ARM_CPU(cs);
8324 CPUARMState *env = &cpu->env;
ebca90e4 8325 MemTxAttrs attrs = {};
5ce4ff65 8326 AddressSpace *as;
ebca90e4
PM
8327
8328 attrs.secure = is_secure;
5ce4ff65 8329 as = arm_addressspace(cs, attrs);
3795a6de 8330 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
37785977
EI
8331 if (fi->s1ptw) {
8332 return 0;
8333 }
73462ddd
PC
8334 if (regime_translation_big_endian(env, mmu_idx)) {
8335 return address_space_ldq_be(as, addr, attrs, NULL);
8336 } else {
8337 return address_space_ldq_le(as, addr, attrs, NULL);
8338 }
ebca90e4
PM
8339}
8340
b7cc4e82 8341static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 8342 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8343 hwaddr *phys_ptr, int *prot,
e14b5a23
EI
8344 target_ulong *page_size, uint32_t *fsr,
8345 ARMMMUFaultInfo *fi)
b5ff1b31 8346{
70d74660 8347 CPUState *cs = CPU(arm_env_get_cpu(env));
b5ff1b31
FB
8348 int code;
8349 uint32_t table;
8350 uint32_t desc;
8351 int type;
8352 int ap;
e389be16 8353 int domain = 0;
dd4ebc2e 8354 int domain_prot;
a8170e5e 8355 hwaddr phys_addr;
0480f69a 8356 uint32_t dacr;
b5ff1b31 8357
9ee6e8bb
PB
8358 /* Pagetable walk. */
8359 /* Lookup l1 descriptor. */
0480f69a 8360 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
8361 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8362 code = 5;
8363 goto do_fault;
8364 }
a614e698 8365 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8366 mmu_idx, fi);
9ee6e8bb 8367 type = (desc & 3);
dd4ebc2e 8368 domain = (desc >> 5) & 0x0f;
0480f69a
PM
8369 if (regime_el(env, mmu_idx) == 1) {
8370 dacr = env->cp15.dacr_ns;
8371 } else {
8372 dacr = env->cp15.dacr_s;
8373 }
8374 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 8375 if (type == 0) {
601d70b9 8376 /* Section translation fault. */
9ee6e8bb
PB
8377 code = 5;
8378 goto do_fault;
8379 }
dd4ebc2e 8380 if (domain_prot == 0 || domain_prot == 2) {
9ee6e8bb
PB
8381 if (type == 2)
8382 code = 9; /* Section domain fault. */
8383 else
8384 code = 11; /* Page domain fault. */
8385 goto do_fault;
8386 }
8387 if (type == 2) {
8388 /* 1Mb section. */
8389 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8390 ap = (desc >> 10) & 3;
8391 code = 13;
d4c430a8 8392 *page_size = 1024 * 1024;
9ee6e8bb
PB
8393 } else {
8394 /* Lookup l2 entry. */
554b0b09
PM
8395 if (type == 1) {
8396 /* Coarse pagetable. */
8397 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8398 } else {
8399 /* Fine pagetable. */
8400 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8401 }
a614e698 8402 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8403 mmu_idx, fi);
9ee6e8bb
PB
8404 switch (desc & 3) {
8405 case 0: /* Page translation fault. */
8406 code = 7;
8407 goto do_fault;
8408 case 1: /* 64k page. */
8409 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8410 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 8411 *page_size = 0x10000;
ce819861 8412 break;
9ee6e8bb
PB
8413 case 2: /* 4k page. */
8414 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 8415 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 8416 *page_size = 0x1000;
ce819861 8417 break;
fc1891c7 8418 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 8419 if (type == 1) {
fc1891c7
PM
8420 /* ARMv6/XScale extended small page format */
8421 if (arm_feature(env, ARM_FEATURE_XSCALE)
8422 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 8423 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 8424 *page_size = 0x1000;
554b0b09 8425 } else {
fc1891c7
PM
8426 /* UNPREDICTABLE in ARMv5; we choose to take a
8427 * page translation fault.
8428 */
554b0b09
PM
8429 code = 7;
8430 goto do_fault;
8431 }
8432 } else {
8433 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 8434 *page_size = 0x400;
554b0b09 8435 }
9ee6e8bb 8436 ap = (desc >> 4) & 3;
ce819861
PB
8437 break;
8438 default:
9ee6e8bb
PB
8439 /* Never happens, but compiler isn't smart enough to tell. */
8440 abort();
ce819861 8441 }
9ee6e8bb
PB
8442 code = 15;
8443 }
0fbf5238
AJ
8444 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8445 *prot |= *prot ? PAGE_EXEC : 0;
8446 if (!(*prot & (1 << access_type))) {
9ee6e8bb
PB
8447 /* Access permission fault. */
8448 goto do_fault;
8449 }
8450 *phys_ptr = phys_addr;
b7cc4e82 8451 return false;
9ee6e8bb 8452do_fault:
b7cc4e82
PC
8453 *fsr = code | (domain << 4);
8454 return true;
9ee6e8bb
PB
8455}
8456
b7cc4e82 8457static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 8458 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8459 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
8460 target_ulong *page_size, uint32_t *fsr,
8461 ARMMMUFaultInfo *fi)
9ee6e8bb 8462{
70d74660 8463 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb
PB
8464 int code;
8465 uint32_t table;
8466 uint32_t desc;
8467 uint32_t xn;
de9b05b8 8468 uint32_t pxn = 0;
9ee6e8bb
PB
8469 int type;
8470 int ap;
de9b05b8 8471 int domain = 0;
dd4ebc2e 8472 int domain_prot;
a8170e5e 8473 hwaddr phys_addr;
0480f69a 8474 uint32_t dacr;
8bf5b6a9 8475 bool ns;
9ee6e8bb
PB
8476
8477 /* Pagetable walk. */
8478 /* Lookup l1 descriptor. */
0480f69a 8479 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
8480 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8481 code = 5;
8482 goto do_fault;
8483 }
a614e698 8484 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8485 mmu_idx, fi);
9ee6e8bb 8486 type = (desc & 3);
de9b05b8
PM
8487 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8488 /* Section translation fault, or attempt to use the encoding
8489 * which is Reserved on implementations without PXN.
8490 */
9ee6e8bb 8491 code = 5;
9ee6e8bb 8492 goto do_fault;
de9b05b8
PM
8493 }
8494 if ((type == 1) || !(desc & (1 << 18))) {
8495 /* Page or Section. */
dd4ebc2e 8496 domain = (desc >> 5) & 0x0f;
9ee6e8bb 8497 }
0480f69a
PM
8498 if (regime_el(env, mmu_idx) == 1) {
8499 dacr = env->cp15.dacr_ns;
8500 } else {
8501 dacr = env->cp15.dacr_s;
8502 }
8503 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 8504 if (domain_prot == 0 || domain_prot == 2) {
de9b05b8 8505 if (type != 1) {
9ee6e8bb 8506 code = 9; /* Section domain fault. */
de9b05b8 8507 } else {
9ee6e8bb 8508 code = 11; /* Page domain fault. */
de9b05b8 8509 }
9ee6e8bb
PB
8510 goto do_fault;
8511 }
de9b05b8 8512 if (type != 1) {
9ee6e8bb
PB
8513 if (desc & (1 << 18)) {
8514 /* Supersection. */
8515 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
8516 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8517 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 8518 *page_size = 0x1000000;
b5ff1b31 8519 } else {
9ee6e8bb
PB
8520 /* Section. */
8521 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 8522 *page_size = 0x100000;
b5ff1b31 8523 }
9ee6e8bb
PB
8524 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8525 xn = desc & (1 << 4);
de9b05b8 8526 pxn = desc & 1;
9ee6e8bb 8527 code = 13;
8bf5b6a9 8528 ns = extract32(desc, 19, 1);
9ee6e8bb 8529 } else {
de9b05b8
PM
8530 if (arm_feature(env, ARM_FEATURE_PXN)) {
8531 pxn = (desc >> 2) & 1;
8532 }
8bf5b6a9 8533 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
8534 /* Lookup l2 entry. */
8535 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 8536 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 8537 mmu_idx, fi);
9ee6e8bb
PB
8538 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8539 switch (desc & 3) {
8540 case 0: /* Page translation fault. */
8541 code = 7;
b5ff1b31 8542 goto do_fault;
9ee6e8bb
PB
8543 case 1: /* 64k page. */
8544 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8545 xn = desc & (1 << 15);
d4c430a8 8546 *page_size = 0x10000;
9ee6e8bb
PB
8547 break;
8548 case 2: case 3: /* 4k page. */
8549 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8550 xn = desc & 1;
d4c430a8 8551 *page_size = 0x1000;
9ee6e8bb
PB
8552 break;
8553 default:
8554 /* Never happens, but compiler isn't smart enough to tell. */
8555 abort();
b5ff1b31 8556 }
9ee6e8bb
PB
8557 code = 15;
8558 }
dd4ebc2e 8559 if (domain_prot == 3) {
c0034328
JR
8560 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8561 } else {
0480f69a 8562 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
8563 xn = 1;
8564 }
03ae85f8 8565 if (xn && access_type == MMU_INST_FETCH)
c0034328 8566 goto do_fault;
9ee6e8bb 8567
d76951b6
AJ
8568 if (arm_feature(env, ARM_FEATURE_V6K) &&
8569 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
8570 /* The simplified model uses AP[0] as an access control bit. */
8571 if ((ap & 1) == 0) {
8572 /* Access flag fault. */
8573 code = (code == 15) ? 6 : 3;
8574 goto do_fault;
8575 }
8576 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
8577 } else {
8578 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 8579 }
0fbf5238
AJ
8580 if (*prot && !xn) {
8581 *prot |= PAGE_EXEC;
8582 }
8583 if (!(*prot & (1 << access_type))) {
c0034328
JR
8584 /* Access permission fault. */
8585 goto do_fault;
8586 }
3ad493fc 8587 }
8bf5b6a9
PM
8588 if (ns) {
8589 /* The NS bit will (as required by the architecture) have no effect if
8590 * the CPU doesn't support TZ or this is a non-secure translation
8591 * regime, because the attribute will already be non-secure.
8592 */
8593 attrs->secure = false;
8594 }
9ee6e8bb 8595 *phys_ptr = phys_addr;
b7cc4e82 8596 return false;
b5ff1b31 8597do_fault:
b7cc4e82
PC
8598 *fsr = code | (domain << 4);
8599 return true;
b5ff1b31
FB
8600}
8601
3dde962f
PM
8602/* Fault type for long-descriptor MMU fault reporting; this corresponds
8603 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
8604 */
8605typedef enum {
8606 translation_fault = 1,
8607 access_fault = 2,
8608 permission_fault = 3,
8609} MMUFaultType;
8610
1853d5a9 8611/*
a0e966c9 8612 * check_s2_mmu_setup
1853d5a9
EI
8613 * @cpu: ARMCPU
8614 * @is_aa64: True if the translation regime is in AArch64 state
8615 * @startlevel: Suggested starting level
8616 * @inputsize: Bitsize of IPAs
8617 * @stride: Page-table stride (See the ARM ARM)
8618 *
a0e966c9
EI
8619 * Returns true if the suggested S2 translation parameters are OK and
8620 * false otherwise.
1853d5a9 8621 */
a0e966c9
EI
8622static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
8623 int inputsize, int stride)
1853d5a9 8624{
98d68ec2
EI
8625 const int grainsize = stride + 3;
8626 int startsizecheck;
8627
1853d5a9
EI
8628 /* Negative levels are never allowed. */
8629 if (level < 0) {
8630 return false;
8631 }
8632
98d68ec2
EI
8633 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
8634 if (startsizecheck < 1 || startsizecheck > stride + 4) {
8635 return false;
8636 }
8637
1853d5a9 8638 if (is_aa64) {
3526423e 8639 CPUARMState *env = &cpu->env;
1853d5a9
EI
8640 unsigned int pamax = arm_pamax(cpu);
8641
8642 switch (stride) {
8643 case 13: /* 64KB Pages. */
8644 if (level == 0 || (level == 1 && pamax <= 42)) {
8645 return false;
8646 }
8647 break;
8648 case 11: /* 16KB Pages. */
8649 if (level == 0 || (level == 1 && pamax <= 40)) {
8650 return false;
8651 }
8652 break;
8653 case 9: /* 4KB Pages. */
8654 if (level == 0 && pamax <= 42) {
8655 return false;
8656 }
8657 break;
8658 default:
8659 g_assert_not_reached();
8660 }
3526423e
EI
8661
8662 /* Inputsize checks. */
8663 if (inputsize > pamax &&
8664 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
8665 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
8666 return false;
8667 }
1853d5a9 8668 } else {
1853d5a9
EI
8669 /* AArch32 only supports 4KB pages. Assert on that. */
8670 assert(stride == 9);
8671
8672 if (level == 0) {
8673 return false;
8674 }
1853d5a9
EI
8675 }
8676 return true;
8677}
8678
5b2d261d
AB
8679/* Translate from the 4-bit stage 2 representation of
8680 * memory attributes (without cache-allocation hints) to
8681 * the 8-bit representation of the stage 1 MAIR registers
8682 * (which includes allocation hints).
8683 *
8684 * ref: shared/translation/attrs/S2AttrDecode()
8685 * .../S2ConvertAttrsHints()
8686 */
8687static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
8688{
8689 uint8_t hiattr = extract32(s2attrs, 2, 2);
8690 uint8_t loattr = extract32(s2attrs, 0, 2);
8691 uint8_t hihint = 0, lohint = 0;
8692
8693 if (hiattr != 0) { /* normal memory */
8694 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
8695 hiattr = loattr = 1; /* non-cacheable */
8696 } else {
8697 if (hiattr != 1) { /* Write-through or write-back */
8698 hihint = 3; /* RW allocate */
8699 }
8700 if (loattr != 1) { /* Write-through or write-back */
8701 lohint = 3; /* RW allocate */
8702 }
8703 }
8704 }
8705
8706 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
8707}
8708
b7cc4e82 8709static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 8710 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8711 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
e14b5a23 8712 target_ulong *page_size_ptr, uint32_t *fsr,
5b2d261d 8713 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 8714{
1853d5a9
EI
8715 ARMCPU *cpu = arm_env_get_cpu(env);
8716 CPUState *cs = CPU(cpu);
3dde962f
PM
8717 /* Read an LPAE long-descriptor translation table. */
8718 MMUFaultType fault_type = translation_fault;
1b4093ea 8719 uint32_t level;
0c5fbf3b 8720 uint32_t epd = 0;
1f4c8c18 8721 int32_t t0sz, t1sz;
2c8dd318 8722 uint32_t tg;
3dde962f
PM
8723 uint64_t ttbr;
8724 int ttbr_select;
dddb5223 8725 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f
PM
8726 uint32_t tableattrs;
8727 target_ulong page_size;
8728 uint32_t attrs;
973a5434 8729 int32_t stride = 9;
6e99f762 8730 int32_t addrsize;
4ca6a051 8731 int inputsize;
2c8dd318 8732 int32_t tbi = 0;
0480f69a 8733 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 8734 int ap, ns, xn, pxn;
88e8add8
GB
8735 uint32_t el = regime_el(env, mmu_idx);
8736 bool ttbr1_valid = true;
6109769a 8737 uint64_t descaddrmask;
6e99f762 8738 bool aarch64 = arm_el_is_aa64(env, el);
0480f69a
PM
8739
8740 /* TODO:
88e8add8
GB
8741 * This code does not handle the different format TCR for VTCR_EL2.
8742 * This code also does not support shareability levels.
8743 * Attribute and permission bit handling should also be checked when adding
8744 * support for those page table walks.
0480f69a 8745 */
6e99f762 8746 if (aarch64) {
1b4093ea 8747 level = 0;
6e99f762 8748 addrsize = 64;
88e8add8 8749 if (el > 1) {
1edee470
EI
8750 if (mmu_idx != ARMMMUIdx_S2NS) {
8751 tbi = extract64(tcr->raw_tcr, 20, 1);
8752 }
88e8add8
GB
8753 } else {
8754 if (extract64(address, 55, 1)) {
8755 tbi = extract64(tcr->raw_tcr, 38, 1);
8756 } else {
8757 tbi = extract64(tcr->raw_tcr, 37, 1);
8758 }
8759 }
2c8dd318 8760 tbi *= 8;
88e8add8
GB
8761
8762 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
8763 * invalid.
8764 */
8765 if (el > 1) {
8766 ttbr1_valid = false;
8767 }
d0a2cbce 8768 } else {
1b4093ea 8769 level = 1;
6e99f762 8770 addrsize = 32;
d0a2cbce
PM
8771 /* There is no TTBR1 for EL2 */
8772 if (el == 2) {
8773 ttbr1_valid = false;
8774 }
2c8dd318 8775 }
3dde962f
PM
8776
8777 /* Determine whether this address is in the region controlled by
8778 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
8779 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
8780 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
8781 */
6e99f762 8782 if (aarch64) {
4ee38098
EI
8783 /* AArch64 translation. */
8784 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
8785 t0sz = MIN(t0sz, 39);
8786 t0sz = MAX(t0sz, 16);
4ee38098
EI
8787 } else if (mmu_idx != ARMMMUIdx_S2NS) {
8788 /* AArch32 stage 1 translation. */
8789 t0sz = extract32(tcr->raw_tcr, 0, 3);
8790 } else {
8791 /* AArch32 stage 2 translation. */
8792 bool sext = extract32(tcr->raw_tcr, 4, 1);
8793 bool sign = extract32(tcr->raw_tcr, 3, 1);
6e99f762
SS
8794 /* Address size is 40-bit for a stage 2 translation,
8795 * and t0sz can be negative (from -8 to 7),
8796 * so we need to adjust it to use the TTBR selecting logic below.
8797 */
8798 addrsize = 40;
8799 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
4ee38098
EI
8800
8801 /* If the sign-extend bit is not the same as t0sz[3], the result
8802 * is unpredictable. Flag this as a guest error. */
8803 if (sign != sext) {
8804 qemu_log_mask(LOG_GUEST_ERROR,
39cba610 8805 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
4ee38098 8806 }
2c8dd318 8807 }
1f4c8c18 8808 t1sz = extract32(tcr->raw_tcr, 16, 6);
6e99f762 8809 if (aarch64) {
2c8dd318
RH
8810 t1sz = MIN(t1sz, 39);
8811 t1sz = MAX(t1sz, 16);
8812 }
6e99f762 8813 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
3dde962f
PM
8814 /* there is a ttbr0 region and we are in it (high bits all zero) */
8815 ttbr_select = 0;
88e8add8 8816 } else if (ttbr1_valid && t1sz &&
6e99f762 8817 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
3dde962f
PM
8818 /* there is a ttbr1 region and we are in it (high bits all one) */
8819 ttbr_select = 1;
8820 } else if (!t0sz) {
8821 /* ttbr0 region is "everything not in the ttbr1 region" */
8822 ttbr_select = 0;
88e8add8 8823 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
8824 /* ttbr1 region is "everything not in the ttbr0 region" */
8825 ttbr_select = 1;
8826 } else {
8827 /* in the gap between the two regions, this is a Translation fault */
8828 fault_type = translation_fault;
8829 goto do_fault;
8830 }
8831
8832 /* Note that QEMU ignores shareability and cacheability attributes,
8833 * so we don't need to do anything with the SH, ORGN, IRGN fields
8834 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
8835 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
8836 * implement any ASID-like capability so we can ignore it (instead
8837 * we will always flush the TLB any time the ASID is changed).
8838 */
8839 if (ttbr_select == 0) {
aef878be 8840 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
8841 if (el < 2) {
8842 epd = extract32(tcr->raw_tcr, 7, 1);
8843 }
6e99f762 8844 inputsize = addrsize - t0sz;
2c8dd318 8845
11f136ee 8846 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 8847 if (tg == 1) { /* 64KB pages */
973a5434 8848 stride = 13;
2c8dd318
RH
8849 }
8850 if (tg == 2) { /* 16KB pages */
973a5434 8851 stride = 11;
2c8dd318 8852 }
3dde962f 8853 } else {
88e8add8
GB
8854 /* We should only be here if TTBR1 is valid */
8855 assert(ttbr1_valid);
8856
aef878be 8857 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 8858 epd = extract32(tcr->raw_tcr, 23, 1);
6e99f762 8859 inputsize = addrsize - t1sz;
2c8dd318 8860
11f136ee 8861 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 8862 if (tg == 3) { /* 64KB pages */
973a5434 8863 stride = 13;
2c8dd318
RH
8864 }
8865 if (tg == 1) { /* 16KB pages */
973a5434 8866 stride = 11;
2c8dd318 8867 }
3dde962f
PM
8868 }
8869
0480f69a 8870 /* Here we should have set up all the parameters for the translation:
6e99f762 8871 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
8872 */
8873
3dde962f 8874 if (epd) {
88e8add8
GB
8875 /* Translation table walk disabled => Translation fault on TLB miss
8876 * Note: This is always 0 on 64-bit EL2 and EL3.
8877 */
3dde962f
PM
8878 goto do_fault;
8879 }
8880
1853d5a9
EI
8881 if (mmu_idx != ARMMMUIdx_S2NS) {
8882 /* The starting level depends on the virtual address size (which can
8883 * be up to 48 bits) and the translation granule size. It indicates
8884 * the number of strides (stride bits at a time) needed to
8885 * consume the bits of the input address. In the pseudocode this is:
8886 * level = 4 - RoundUp((inputsize - grainsize) / stride)
8887 * where their 'inputsize' is our 'inputsize', 'grainsize' is
8888 * our 'stride + 3' and 'stride' is our 'stride'.
8889 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
8890 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
8891 * = 4 - (inputsize - 4) / stride;
8892 */
8893 level = 4 - (inputsize - 4) / stride;
8894 } else {
8895 /* For stage 2 translations the starting level is specified by the
8896 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
8897 */
1b4093ea
SS
8898 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
8899 uint32_t startlevel;
1853d5a9
EI
8900 bool ok;
8901
6e99f762 8902 if (!aarch64 || stride == 9) {
1853d5a9 8903 /* AArch32 or 4KB pages */
1b4093ea 8904 startlevel = 2 - sl0;
1853d5a9
EI
8905 } else {
8906 /* 16KB or 64KB pages */
1b4093ea 8907 startlevel = 3 - sl0;
1853d5a9
EI
8908 }
8909
8910 /* Check that the starting level is valid. */
6e99f762 8911 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 8912 inputsize, stride);
1853d5a9 8913 if (!ok) {
1853d5a9
EI
8914 fault_type = translation_fault;
8915 goto do_fault;
8916 }
1b4093ea 8917 level = startlevel;
1853d5a9 8918 }
3dde962f 8919
dddb5223
SS
8920 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
8921 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
8922
8923 /* Now we can extract the actual base address from the TTBR */
2c8dd318 8924 descaddr = extract64(ttbr, 0, 48);
dddb5223 8925 descaddr &= ~indexmask;
3dde962f 8926
6109769a 8927 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
8928 * but up to bit 47 for ARMv8, but we use the descaddrmask
8929 * up to bit 39 for AArch32, because we don't need other bits in that case
8930 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 8931 */
6e99f762 8932 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 8933 ~indexmask_grainsize;
6109769a 8934
ebca90e4
PM
8935 /* Secure accesses start with the page table in secure memory and
8936 * can be downgraded to non-secure at any step. Non-secure accesses
8937 * remain non-secure. We implement this by just ORing in the NSTable/NS
8938 * bits at each step.
8939 */
8940 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
8941 for (;;) {
8942 uint64_t descriptor;
ebca90e4 8943 bool nstable;
3dde962f 8944
dddb5223 8945 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 8946 descaddr &= ~7ULL;
ebca90e4 8947 nstable = extract32(tableattrs, 4, 1);
3795a6de 8948 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
37785977
EI
8949 if (fi->s1ptw) {
8950 goto do_fault;
8951 }
8952
3dde962f
PM
8953 if (!(descriptor & 1) ||
8954 (!(descriptor & 2) && (level == 3))) {
8955 /* Invalid, or the Reserved level 3 encoding */
8956 goto do_fault;
8957 }
6109769a 8958 descaddr = descriptor & descaddrmask;
3dde962f
PM
8959
8960 if ((descriptor & 2) && (level < 3)) {
8961 /* Table entry. The top five bits are attributes which may
8962 * propagate down through lower levels of the table (and
8963 * which are all arranged so that 0 means "no effect", so
8964 * we can gather them up by ORing in the bits at each level).
8965 */
8966 tableattrs |= extract64(descriptor, 59, 5);
8967 level++;
dddb5223 8968 indexmask = indexmask_grainsize;
3dde962f
PM
8969 continue;
8970 }
8971 /* Block entry at level 1 or 2, or page entry at level 3.
8972 * These are basically the same thing, although the number
8973 * of bits we pull in from the vaddr varies.
8974 */
973a5434 8975 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 8976 descaddr |= (address & (page_size - 1));
6ab1a5ee 8977 /* Extract attributes from the descriptor */
d615efac
IC
8978 attrs = extract64(descriptor, 2, 10)
8979 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
8980
8981 if (mmu_idx == ARMMMUIdx_S2NS) {
8982 /* Stage 2 table descriptors do not include any attribute fields */
8983 break;
8984 }
8985 /* Merge in attributes from table descriptors */
3dde962f
PM
8986 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
8987 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
8988 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
8989 * means "force PL1 access only", which means forcing AP[1] to 0.
8990 */
8991 if (extract32(tableattrs, 2, 1)) {
8992 attrs &= ~(1 << 4);
8993 }
ebca90e4 8994 attrs |= nstable << 3; /* NS */
3dde962f
PM
8995 break;
8996 }
8997 /* Here descaddr is the final physical address, and attributes
8998 * are all in attrs.
8999 */
9000 fault_type = access_fault;
9001 if ((attrs & (1 << 8)) == 0) {
9002 /* Access flag */
9003 goto do_fault;
9004 }
d8e052b3
AJ
9005
9006 ap = extract32(attrs, 4, 2);
d8e052b3 9007 xn = extract32(attrs, 12, 1);
d8e052b3 9008
6ab1a5ee
EI
9009 if (mmu_idx == ARMMMUIdx_S2NS) {
9010 ns = true;
9011 *prot = get_S2prot(env, ap, xn);
9012 } else {
9013 ns = extract32(attrs, 3, 1);
9014 pxn = extract32(attrs, 11, 1);
6e99f762 9015 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 9016 }
d8e052b3 9017
3dde962f 9018 fault_type = permission_fault;
d8e052b3 9019 if (!(*prot & (1 << access_type))) {
3dde962f
PM
9020 goto do_fault;
9021 }
3dde962f 9022
8bf5b6a9
PM
9023 if (ns) {
9024 /* The NS bit will (as required by the architecture) have no effect if
9025 * the CPU doesn't support TZ or this is a non-secure translation
9026 * regime, because the attribute will already be non-secure.
9027 */
9028 txattrs->secure = false;
9029 }
5b2d261d
AB
9030
9031 if (cacheattrs != NULL) {
9032 if (mmu_idx == ARMMMUIdx_S2NS) {
9033 cacheattrs->attrs = convert_stage2_attrs(env,
9034 extract32(attrs, 0, 4));
9035 } else {
9036 /* Index into MAIR registers for cache attributes */
9037 uint8_t attrindx = extract32(attrs, 0, 3);
9038 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9039 assert(attrindx <= 7);
9040 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9041 }
9042 cacheattrs->shareability = extract32(attrs, 6, 2);
9043 }
9044
3dde962f
PM
9045 *phys_ptr = descaddr;
9046 *page_size_ptr = page_size;
b7cc4e82 9047 return false;
3dde962f
PM
9048
9049do_fault:
9050 /* Long-descriptor format IFSR/DFSR value */
b7cc4e82 9051 *fsr = (1 << 9) | (fault_type << 2) | level;
37785977
EI
9052 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9053 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 9054 return true;
3dde962f
PM
9055}
9056
f6bda88f
PC
9057static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9058 ARMMMUIdx mmu_idx,
9059 int32_t address, int *prot)
9060{
3a00d560
MD
9061 if (!arm_feature(env, ARM_FEATURE_M)) {
9062 *prot = PAGE_READ | PAGE_WRITE;
9063 switch (address) {
9064 case 0xF0000000 ... 0xFFFFFFFF:
9065 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9066 /* hivecs execing is ok */
9067 *prot |= PAGE_EXEC;
9068 }
9069 break;
9070 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 9071 *prot |= PAGE_EXEC;
3a00d560
MD
9072 break;
9073 }
9074 } else {
9075 /* Default system address map for M profile cores.
9076 * The architecture specifies which regions are execute-never;
9077 * at the MPU level no other checks are defined.
9078 */
9079 switch (address) {
9080 case 0x00000000 ... 0x1fffffff: /* ROM */
9081 case 0x20000000 ... 0x3fffffff: /* SRAM */
9082 case 0x60000000 ... 0x7fffffff: /* RAM */
9083 case 0x80000000 ... 0x9fffffff: /* RAM */
9084 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9085 break;
9086 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9087 case 0xa0000000 ... 0xbfffffff: /* Device */
9088 case 0xc0000000 ... 0xdfffffff: /* Device */
9089 case 0xe0000000 ... 0xffffffff: /* System */
9090 *prot = PAGE_READ | PAGE_WRITE;
9091 break;
9092 default:
9093 g_assert_not_reached();
f6bda88f 9094 }
f6bda88f 9095 }
f6bda88f
PC
9096}
9097
29c483a5
MD
9098static bool pmsav7_use_background_region(ARMCPU *cpu,
9099 ARMMMUIdx mmu_idx, bool is_user)
9100{
9101 /* Return true if we should use the default memory map as a
9102 * "background" region if there are no hits against any MPU regions.
9103 */
9104 CPUARMState *env = &cpu->env;
9105
9106 if (is_user) {
9107 return false;
9108 }
9109
9110 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
9111 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9112 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
9113 } else {
9114 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9115 }
9116}
9117
38aaa60c
PM
9118static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9119{
9120 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9121 return arm_feature(env, ARM_FEATURE_M) &&
9122 extract32(address, 20, 12) == 0xe00;
9123}
9124
bf446a11
PM
9125static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9126{
9127 /* True if address is in the M profile system region
9128 * 0xe0000000 - 0xffffffff
9129 */
9130 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9131}
9132
f6bda88f 9133static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 9134 MMUAccessType access_type, ARMMMUIdx mmu_idx,
f6bda88f
PC
9135 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9136{
9137 ARMCPU *cpu = arm_env_get_cpu(env);
9138 int n;
9139 bool is_user = regime_is_user(env, mmu_idx);
9140
9141 *phys_ptr = address;
9142 *prot = 0;
9143
38aaa60c
PM
9144 if (regime_translation_disabled(env, mmu_idx) ||
9145 m_is_ppb_region(env, address)) {
9146 /* MPU disabled or M profile PPB access: use default memory map.
9147 * The other case which uses the default memory map in the
9148 * v7M ARM ARM pseudocode is exception vector reads from the vector
9149 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9150 * which always does a direct read using address_space_ldl(), rather
9151 * than going via this function, so we don't need to check that here.
9152 */
f6bda88f
PC
9153 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9154 } else { /* MPU enabled */
9155 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9156 /* region search */
9157 uint32_t base = env->pmsav7.drbar[n];
9158 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9159 uint32_t rmask;
9160 bool srdis = false;
9161
9162 if (!(env->pmsav7.drsr[n] & 0x1)) {
9163 continue;
9164 }
9165
9166 if (!rsize) {
c9f9f124
MD
9167 qemu_log_mask(LOG_GUEST_ERROR,
9168 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
9169 continue;
9170 }
9171 rsize++;
9172 rmask = (1ull << rsize) - 1;
9173
9174 if (base & rmask) {
c9f9f124
MD
9175 qemu_log_mask(LOG_GUEST_ERROR,
9176 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9177 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9178 n, base, rmask);
f6bda88f
PC
9179 continue;
9180 }
9181
9182 if (address < base || address > base + rmask) {
9183 continue;
9184 }
9185
9186 /* Region matched */
9187
9188 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9189 int i, snd;
9190 uint32_t srdis_mask;
9191
9192 rsize -= 3; /* sub region size (power of 2) */
9193 snd = ((address - base) >> rsize) & 0x7;
9194 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9195
9196 srdis_mask = srdis ? 0x3 : 0x0;
9197 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9198 /* This will check in groups of 2, 4 and then 8, whether
9199 * the subregion bits are consistent. rsize is incremented
9200 * back up to give the region size, considering consistent
9201 * adjacent subregions as one region. Stop testing if rsize
9202 * is already big enough for an entire QEMU page.
9203 */
9204 int snd_rounded = snd & ~(i - 1);
9205 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9206 snd_rounded + 8, i);
9207 if (srdis_mask ^ srdis_multi) {
9208 break;
9209 }
9210 srdis_mask = (srdis_mask << i) | srdis_mask;
9211 rsize++;
9212 }
9213 }
9214 if (rsize < TARGET_PAGE_BITS) {
c9f9f124
MD
9215 qemu_log_mask(LOG_UNIMP,
9216 "DRSR[%d]: No support for MPU (sub)region "
f6bda88f 9217 "alignment of %" PRIu32 " bits. Minimum is %d\n",
c9f9f124 9218 n, rsize, TARGET_PAGE_BITS);
f6bda88f
PC
9219 continue;
9220 }
9221 if (srdis) {
9222 continue;
9223 }
9224 break;
9225 }
9226
9227 if (n == -1) { /* no hits */
29c483a5 9228 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f
PC
9229 /* background fault */
9230 *fsr = 0;
9231 return true;
9232 }
9233 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9234 } else { /* a MPU hit! */
9235 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
9236 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9237
9238 if (m_is_system_region(env, address)) {
9239 /* System space is always execute never */
9240 xn = 1;
9241 }
f6bda88f
PC
9242
9243 if (is_user) { /* User mode AP bit decoding */
9244 switch (ap) {
9245 case 0:
9246 case 1:
9247 case 5:
9248 break; /* no access */
9249 case 3:
9250 *prot |= PAGE_WRITE;
9251 /* fall through */
9252 case 2:
9253 case 6:
9254 *prot |= PAGE_READ | PAGE_EXEC;
9255 break;
9256 default:
9257 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9258 "DRACR[%d]: Bad value for AP bits: 0x%"
9259 PRIx32 "\n", n, ap);
f6bda88f
PC
9260 }
9261 } else { /* Priv. mode AP bits decoding */
9262 switch (ap) {
9263 case 0:
9264 break; /* no access */
9265 case 1:
9266 case 2:
9267 case 3:
9268 *prot |= PAGE_WRITE;
9269 /* fall through */
9270 case 5:
9271 case 6:
9272 *prot |= PAGE_READ | PAGE_EXEC;
9273 break;
9274 default:
9275 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9276 "DRACR[%d]: Bad value for AP bits: 0x%"
9277 PRIx32 "\n", n, ap);
f6bda88f
PC
9278 }
9279 }
9280
9281 /* execute never */
bf446a11 9282 if (xn) {
f6bda88f
PC
9283 *prot &= ~PAGE_EXEC;
9284 }
9285 }
9286 }
9287
9288 *fsr = 0x00d; /* Permission fault */
9289 return !(*prot & (1 << access_type));
9290}
9291
35337cc3
PM
9292static bool v8m_is_sau_exempt(CPUARMState *env,
9293 uint32_t address, MMUAccessType access_type)
9294{
9295 /* The architecture specifies that certain address ranges are
9296 * exempt from v8M SAU/IDAU checks.
9297 */
9298 return
9299 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9300 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9301 (address >= 0xe000e000 && address <= 0xe000efff) ||
9302 (address >= 0xe002e000 && address <= 0xe002efff) ||
9303 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9304 (address >= 0xe00ff000 && address <= 0xe00fffff);
9305}
9306
9307static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9308 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9309 V8M_SAttributes *sattrs)
9310{
9311 /* Look up the security attributes for this address. Compare the
9312 * pseudocode SecurityCheck() function.
9313 * We assume the caller has zero-initialized *sattrs.
9314 */
9315 ARMCPU *cpu = arm_env_get_cpu(env);
9316 int r;
9317
9318 /* TODO: implement IDAU */
9319
9320 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9321 /* 0xf0000000..0xffffffff is always S for insn fetches */
9322 return;
9323 }
9324
9325 if (v8m_is_sau_exempt(env, address, access_type)) {
9326 sattrs->ns = !regime_is_secure(env, mmu_idx);
9327 return;
9328 }
9329
9330 switch (env->sau.ctrl & 3) {
9331 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9332 break;
9333 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9334 sattrs->ns = true;
9335 break;
9336 default: /* SAU.ENABLE == 1 */
9337 for (r = 0; r < cpu->sau_sregion; r++) {
9338 if (env->sau.rlar[r] & 1) {
9339 uint32_t base = env->sau.rbar[r] & ~0x1f;
9340 uint32_t limit = env->sau.rlar[r] | 0x1f;
9341
9342 if (base <= address && limit >= address) {
9343 if (sattrs->srvalid) {
9344 /* If we hit in more than one region then we must report
9345 * as Secure, not NS-Callable, with no valid region
9346 * number info.
9347 */
9348 sattrs->ns = false;
9349 sattrs->nsc = false;
9350 sattrs->sregion = 0;
9351 sattrs->srvalid = false;
9352 break;
9353 } else {
9354 if (env->sau.rlar[r] & 2) {
9355 sattrs->nsc = true;
9356 } else {
9357 sattrs->ns = true;
9358 }
9359 sattrs->srvalid = true;
9360 sattrs->sregion = r;
9361 }
9362 }
9363 }
9364 }
9365
9366 /* TODO when we support the IDAU then it may override the result here */
9367 break;
9368 }
9369}
9370
54317c0f
PM
9371static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9372 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9373 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9374 int *prot, uint32_t *fsr, uint32_t *mregion)
9375{
9376 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9377 * that a full phys-to-virt translation does).
9378 * mregion is (if not NULL) set to the region number which matched,
9379 * or -1 if no region number is returned (MPU off, address did not
9380 * hit a region, address hit in multiple regions).
9381 */
504e3cc3
PM
9382 ARMCPU *cpu = arm_env_get_cpu(env);
9383 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 9384 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
9385 int n;
9386 int matchregion = -1;
9387 bool hit = false;
9388
9389 *phys_ptr = address;
9390 *prot = 0;
54317c0f
PM
9391 if (mregion) {
9392 *mregion = -1;
35337cc3
PM
9393 }
9394
504e3cc3
PM
9395 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9396 * was an exception vector read from the vector table (which is always
9397 * done using the default system address map), because those accesses
9398 * are done in arm_v7m_load_vector(), which always does a direct
9399 * read using address_space_ldl(), rather than going via this function.
9400 */
9401 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9402 hit = true;
9403 } else if (m_is_ppb_region(env, address)) {
9404 hit = true;
9405 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9406 hit = true;
9407 } else {
9408 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9409 /* region search */
9410 /* Note that the base address is bits [31:5] from the register
9411 * with bits [4:0] all zeroes, but the limit address is bits
9412 * [31:5] from the register with bits [4:0] all ones.
9413 */
62c58ee0
PM
9414 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9415 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 9416
62c58ee0 9417 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
9418 /* Region disabled */
9419 continue;
9420 }
9421
9422 if (address < base || address > limit) {
9423 continue;
9424 }
9425
9426 if (hit) {
9427 /* Multiple regions match -- always a failure (unlike
9428 * PMSAv7 where highest-numbered-region wins)
9429 */
9430 *fsr = 0x00d; /* permission fault */
9431 return true;
9432 }
9433
9434 matchregion = n;
9435 hit = true;
9436
9437 if (base & ~TARGET_PAGE_MASK) {
9438 qemu_log_mask(LOG_UNIMP,
9439 "MPU_RBAR[%d]: No support for MPU region base"
9440 "address of 0x%" PRIx32 ". Minimum alignment is "
9441 "%d\n",
9442 n, base, TARGET_PAGE_BITS);
9443 continue;
9444 }
9445 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9446 qemu_log_mask(LOG_UNIMP,
9447 "MPU_RBAR[%d]: No support for MPU region limit"
9448 "address of 0x%" PRIx32 ". Minimum alignment is "
9449 "%d\n",
9450 n, limit, TARGET_PAGE_BITS);
9451 continue;
9452 }
9453 }
9454 }
9455
9456 if (!hit) {
9457 /* background fault */
9458 *fsr = 0;
9459 return true;
9460 }
9461
9462 if (matchregion == -1) {
9463 /* hit using the background region */
9464 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9465 } else {
62c58ee0
PM
9466 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9467 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
9468
9469 if (m_is_system_region(env, address)) {
9470 /* System space is always execute never */
9471 xn = 1;
9472 }
9473
9474 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9475 if (*prot && !xn) {
9476 *prot |= PAGE_EXEC;
9477 }
9478 /* We don't need to look the attribute up in the MAIR0/MAIR1
9479 * registers because that only tells us about cacheability.
9480 */
54317c0f
PM
9481 if (mregion) {
9482 *mregion = matchregion;
9483 }
504e3cc3
PM
9484 }
9485
9486 *fsr = 0x00d; /* Permission fault */
9487 return !(*prot & (1 << access_type));
9488}
9489
54317c0f
PM
9490
9491static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9492 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9493 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9494 int *prot, uint32_t *fsr)
9495{
9496 uint32_t secure = regime_is_secure(env, mmu_idx);
9497 V8M_SAttributes sattrs = {};
9498
9499 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9500 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9501 if (access_type == MMU_INST_FETCH) {
9502 /* Instruction fetches always use the MMU bank and the
9503 * transaction attribute determined by the fetch address,
9504 * regardless of CPU state. This is painful for QEMU
9505 * to handle, because it would mean we need to encode
9506 * into the mmu_idx not just the (user, negpri) information
9507 * for the current security state but also that for the
9508 * other security state, which would balloon the number
9509 * of mmu_idx values needed alarmingly.
9510 * Fortunately we can avoid this because it's not actually
9511 * possible to arbitrarily execute code from memory with
9512 * the wrong security attribute: it will always generate
9513 * an exception of some kind or another, apart from the
9514 * special case of an NS CPU executing an SG instruction
9515 * in S&NSC memory. So we always just fail the translation
9516 * here and sort things out in the exception handler
9517 * (including possibly emulating an SG instruction).
9518 */
9519 if (sattrs.ns != !secure) {
9520 *fsr = sattrs.nsc ? M_FAKE_FSR_NSC_EXEC : M_FAKE_FSR_SFAULT;
9521 *phys_ptr = address;
9522 *prot = 0;
9523 return true;
9524 }
9525 } else {
9526 /* For data accesses we always use the MMU bank indicated
9527 * by the current CPU state, but the security attributes
9528 * might downgrade a secure access to nonsecure.
9529 */
9530 if (sattrs.ns) {
9531 txattrs->secure = false;
9532 } else if (!secure) {
9533 /* NS access to S memory must fault.
9534 * Architecturally we should first check whether the
9535 * MPU information for this address indicates that we
9536 * are doing an unaligned access to Device memory, which
9537 * should generate a UsageFault instead. QEMU does not
9538 * currently check for that kind of unaligned access though.
9539 * If we added it we would need to do so as a special case
9540 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9541 */
9542 *fsr = M_FAKE_FSR_SFAULT;
9543 *phys_ptr = address;
9544 *prot = 0;
9545 return true;
9546 }
9547 }
9548 }
9549
9550 return pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
9551 txattrs, prot, fsr, NULL);
9552}
9553
13689d43 9554static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 9555 MMUAccessType access_type, ARMMMUIdx mmu_idx,
13689d43 9556 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9ee6e8bb
PB
9557{
9558 int n;
9559 uint32_t mask;
9560 uint32_t base;
0480f69a 9561 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 9562
3279adb9
PM
9563 if (regime_translation_disabled(env, mmu_idx)) {
9564 /* MPU disabled. */
9565 *phys_ptr = address;
9566 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9567 return false;
9568 }
9569
9ee6e8bb
PB
9570 *phys_ptr = address;
9571 for (n = 7; n >= 0; n--) {
554b0b09 9572 base = env->cp15.c6_region[n];
87c3d486 9573 if ((base & 1) == 0) {
554b0b09 9574 continue;
87c3d486 9575 }
554b0b09
PM
9576 mask = 1 << ((base >> 1) & 0x1f);
9577 /* Keep this shift separate from the above to avoid an
9578 (undefined) << 32. */
9579 mask = (mask << 1) - 1;
87c3d486 9580 if (((base ^ address) & ~mask) == 0) {
554b0b09 9581 break;
87c3d486 9582 }
9ee6e8bb 9583 }
87c3d486 9584 if (n < 0) {
b7cc4e82
PC
9585 *fsr = 2;
9586 return true;
87c3d486 9587 }
9ee6e8bb 9588
03ae85f8 9589 if (access_type == MMU_INST_FETCH) {
7e09797c 9590 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 9591 } else {
7e09797c 9592 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
9593 }
9594 mask = (mask >> (n * 4)) & 0xf;
9595 switch (mask) {
9596 case 0:
b7cc4e82
PC
9597 *fsr = 1;
9598 return true;
9ee6e8bb 9599 case 1:
87c3d486 9600 if (is_user) {
b7cc4e82
PC
9601 *fsr = 1;
9602 return true;
87c3d486 9603 }
554b0b09
PM
9604 *prot = PAGE_READ | PAGE_WRITE;
9605 break;
9ee6e8bb 9606 case 2:
554b0b09 9607 *prot = PAGE_READ;
87c3d486 9608 if (!is_user) {
554b0b09 9609 *prot |= PAGE_WRITE;
87c3d486 9610 }
554b0b09 9611 break;
9ee6e8bb 9612 case 3:
554b0b09
PM
9613 *prot = PAGE_READ | PAGE_WRITE;
9614 break;
9ee6e8bb 9615 case 5:
87c3d486 9616 if (is_user) {
b7cc4e82
PC
9617 *fsr = 1;
9618 return true;
87c3d486 9619 }
554b0b09
PM
9620 *prot = PAGE_READ;
9621 break;
9ee6e8bb 9622 case 6:
554b0b09
PM
9623 *prot = PAGE_READ;
9624 break;
9ee6e8bb 9625 default:
554b0b09 9626 /* Bad permission. */
b7cc4e82
PC
9627 *fsr = 1;
9628 return true;
9ee6e8bb 9629 }
3ad493fc 9630 *prot |= PAGE_EXEC;
b7cc4e82 9631 return false;
9ee6e8bb
PB
9632}
9633
5b2d261d
AB
9634/* Combine either inner or outer cacheability attributes for normal
9635 * memory, according to table D4-42 and pseudocode procedure
9636 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
9637 *
9638 * NB: only stage 1 includes allocation hints (RW bits), leading to
9639 * some asymmetry.
9640 */
9641static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
9642{
9643 if (s1 == 4 || s2 == 4) {
9644 /* non-cacheable has precedence */
9645 return 4;
9646 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
9647 /* stage 1 write-through takes precedence */
9648 return s1;
9649 } else if (extract32(s2, 2, 2) == 2) {
9650 /* stage 2 write-through takes precedence, but the allocation hint
9651 * is still taken from stage 1
9652 */
9653 return (2 << 2) | extract32(s1, 0, 2);
9654 } else { /* write-back */
9655 return s1;
9656 }
9657}
9658
9659/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
9660 * and CombineS1S2Desc()
9661 *
9662 * @s1: Attributes from stage 1 walk
9663 * @s2: Attributes from stage 2 walk
9664 */
9665static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
9666{
9667 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
9668 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
9669 ARMCacheAttrs ret;
9670
9671 /* Combine shareability attributes (table D4-43) */
9672 if (s1.shareability == 2 || s2.shareability == 2) {
9673 /* if either are outer-shareable, the result is outer-shareable */
9674 ret.shareability = 2;
9675 } else if (s1.shareability == 3 || s2.shareability == 3) {
9676 /* if either are inner-shareable, the result is inner-shareable */
9677 ret.shareability = 3;
9678 } else {
9679 /* both non-shareable */
9680 ret.shareability = 0;
9681 }
9682
9683 /* Combine memory type and cacheability attributes */
9684 if (s1hi == 0 || s2hi == 0) {
9685 /* Device has precedence over normal */
9686 if (s1lo == 0 || s2lo == 0) {
9687 /* nGnRnE has precedence over anything */
9688 ret.attrs = 0;
9689 } else if (s1lo == 4 || s2lo == 4) {
9690 /* non-Reordering has precedence over Reordering */
9691 ret.attrs = 4; /* nGnRE */
9692 } else if (s1lo == 8 || s2lo == 8) {
9693 /* non-Gathering has precedence over Gathering */
9694 ret.attrs = 8; /* nGRE */
9695 } else {
9696 ret.attrs = 0xc; /* GRE */
9697 }
9698
9699 /* Any location for which the resultant memory type is any
9700 * type of Device memory is always treated as Outer Shareable.
9701 */
9702 ret.shareability = 2;
9703 } else { /* Normal memory */
9704 /* Outer/inner cacheability combine independently */
9705 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
9706 | combine_cacheattr_nibble(s1lo, s2lo);
9707
9708 if (ret.attrs == 0x44) {
9709 /* Any location for which the resultant memory type is Normal
9710 * Inner Non-cacheable, Outer Non-cacheable is always treated
9711 * as Outer Shareable.
9712 */
9713 ret.shareability = 2;
9714 }
9715 }
9716
9717 return ret;
9718}
9719
9720
702a9357
PM
9721/* get_phys_addr - get the physical address for this virtual address
9722 *
9723 * Find the physical address corresponding to the given virtual address,
9724 * by doing a translation table walk on MMU based systems or using the
9725 * MPU state on MPU based systems.
9726 *
b7cc4e82
PC
9727 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
9728 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
9729 * information on why the translation aborted, in the format of a
9730 * DFSR/IFSR fault register, with the following caveats:
9731 * * we honour the short vs long DFSR format differences.
9732 * * the WnR bit is never set (the caller must do this).
f6bda88f 9733 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
9734 * value.
9735 *
9736 * @env: CPUARMState
9737 * @address: virtual address to get physical address for
9738 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 9739 * @mmu_idx: MMU index indicating required translation regime
702a9357 9740 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 9741 * @attrs: set to the memory transaction attributes to use
702a9357
PM
9742 * @prot: set to the permissions for the page containing phys_ptr
9743 * @page_size: set to the size of the page containing phys_ptr
b7cc4e82 9744 * @fsr: set to the DFSR/IFSR value on failure
5b2d261d
AB
9745 * @fi: set to fault info if the translation fails
9746 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 9747 */
af51f566 9748static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 9749 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 9750 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23 9751 target_ulong *page_size, uint32_t *fsr,
5b2d261d 9752 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 9753{
0480f69a 9754 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
9755 /* Call ourselves recursively to do the stage 1 and then stage 2
9756 * translations.
0480f69a 9757 */
9b539263
EI
9758 if (arm_feature(env, ARM_FEATURE_EL2)) {
9759 hwaddr ipa;
9760 int s2_prot;
9761 int ret;
5b2d261d 9762 ARMCacheAttrs cacheattrs2 = {};
9b539263
EI
9763
9764 ret = get_phys_addr(env, address, access_type,
8bd5c820 9765 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
5b2d261d 9766 prot, page_size, fsr, fi, cacheattrs);
9b539263
EI
9767
9768 /* If S1 fails or S2 is disabled, return early. */
9769 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
9770 *phys_ptr = ipa;
9771 return ret;
9772 }
9773
9774 /* S1 is done. Now do S2 translation. */
9775 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
9776 phys_ptr, attrs, &s2_prot,
5b2d261d
AB
9777 page_size, fsr, fi,
9778 cacheattrs != NULL ? &cacheattrs2 : NULL);
9b539263
EI
9779 fi->s2addr = ipa;
9780 /* Combine the S1 and S2 perms. */
9781 *prot &= s2_prot;
5b2d261d
AB
9782
9783 /* Combine the S1 and S2 cache attributes, if needed */
9784 if (!ret && cacheattrs != NULL) {
9785 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
9786 }
9787
9b539263
EI
9788 return ret;
9789 } else {
9790 /*
9791 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
9792 */
8bd5c820 9793 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 9794 }
0480f69a 9795 }
d3649702 9796
8bf5b6a9
PM
9797 /* The page table entries may downgrade secure to non-secure, but
9798 * cannot upgrade an non-secure translation regime's attributes
9799 * to secure.
9800 */
9801 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 9802 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 9803
0480f69a
PM
9804 /* Fast Context Switch Extension. This doesn't exist at all in v8.
9805 * In v7 and earlier it affects all stage 1 translations.
9806 */
9807 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
9808 && !arm_feature(env, ARM_FEATURE_V8)) {
9809 if (regime_el(env, mmu_idx) == 3) {
9810 address += env->cp15.fcseidr_s;
9811 } else {
9812 address += env->cp15.fcseidr_ns;
9813 }
54bf36ed 9814 }
9ee6e8bb 9815
3279adb9 9816 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 9817 bool ret;
f6bda88f 9818 *page_size = TARGET_PAGE_SIZE;
3279adb9 9819
504e3cc3
PM
9820 if (arm_feature(env, ARM_FEATURE_V8)) {
9821 /* PMSAv8 */
9822 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
35337cc3 9823 phys_ptr, attrs, prot, fsr);
504e3cc3 9824 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
9825 /* PMSAv7 */
9826 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9827 phys_ptr, prot, fsr);
9828 } else {
9829 /* Pre-v7 MPU */
9830 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
9831 phys_ptr, prot, fsr);
9832 }
9833 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 9834 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
9835 access_type == MMU_DATA_LOAD ? "reading" :
9836 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
9837 (uint32_t)address, mmu_idx,
9838 ret ? "Miss" : "Hit",
9839 *prot & PAGE_READ ? 'r' : '-',
9840 *prot & PAGE_WRITE ? 'w' : '-',
9841 *prot & PAGE_EXEC ? 'x' : '-');
9842
9843 return ret;
f6bda88f
PC
9844 }
9845
3279adb9
PM
9846 /* Definitely a real MMU, not an MPU */
9847
0480f69a 9848 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 9849 /* MMU disabled. */
9ee6e8bb 9850 *phys_ptr = address;
3ad493fc 9851 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 9852 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 9853 return 0;
0480f69a
PM
9854 }
9855
0480f69a
PM
9856 if (regime_using_lpae_format(env, mmu_idx)) {
9857 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
5b2d261d 9858 attrs, prot, page_size, fsr, fi, cacheattrs);
0480f69a
PM
9859 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
9860 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 9861 attrs, prot, page_size, fsr, fi);
9ee6e8bb 9862 } else {
0480f69a 9863 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 9864 prot, page_size, fsr, fi);
9ee6e8bb
PB
9865 }
9866}
9867
8c6084bf 9868/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
9869 * to the TLB. Return false on success, or true on failure. Populate
9870 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 9871 */
b7cc4e82 9872bool arm_tlb_fill(CPUState *cs, vaddr address,
03ae85f8 9873 MMUAccessType access_type, int mmu_idx, uint32_t *fsr,
e14b5a23 9874 ARMMMUFaultInfo *fi)
b5ff1b31 9875{
7510454e
AF
9876 ARMCPU *cpu = ARM_CPU(cs);
9877 CPUARMState *env = &cpu->env;
a8170e5e 9878 hwaddr phys_addr;
d4c430a8 9879 target_ulong page_size;
b5ff1b31 9880 int prot;
d3649702 9881 int ret;
8bf5b6a9 9882 MemTxAttrs attrs = {};
b5ff1b31 9883
8bd5c820
PM
9884 ret = get_phys_addr(env, address, access_type,
9885 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
5b2d261d 9886 &attrs, &prot, &page_size, fsr, fi, NULL);
b7cc4e82 9887 if (!ret) {
b5ff1b31 9888 /* Map a single [sub]page. */
dcd82c11
AB
9889 phys_addr &= TARGET_PAGE_MASK;
9890 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
9891 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
9892 prot, mmu_idx, page_size);
d4c430a8 9893 return 0;
b5ff1b31
FB
9894 }
9895
8c6084bf 9896 return ret;
b5ff1b31
FB
9897}
9898
0faea0c7
PM
9899hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
9900 MemTxAttrs *attrs)
b5ff1b31 9901{
00b941e5 9902 ARMCPU *cpu = ARM_CPU(cs);
d3649702 9903 CPUARMState *env = &cpu->env;
a8170e5e 9904 hwaddr phys_addr;
d4c430a8 9905 target_ulong page_size;
b5ff1b31 9906 int prot;
b7cc4e82
PC
9907 bool ret;
9908 uint32_t fsr;
e14b5a23 9909 ARMMMUFaultInfo fi = {};
8bd5c820 9910 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
b5ff1b31 9911
0faea0c7
PM
9912 *attrs = (MemTxAttrs) {};
9913
8bd5c820 9914 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
5b2d261d 9915 attrs, &prot, &page_size, &fsr, &fi, NULL);
b5ff1b31 9916
b7cc4e82 9917 if (ret) {
b5ff1b31 9918 return -1;
00b941e5 9919 }
b5ff1b31
FB
9920 return phys_addr;
9921}
9922
0ecb72a5 9923uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 9924{
58117c9b
MD
9925 uint32_t mask;
9926 unsigned el = arm_current_el(env);
9927
9928 /* First handle registers which unprivileged can read */
9929
9930 switch (reg) {
9931 case 0 ... 7: /* xPSR sub-fields */
9932 mask = 0;
9933 if ((reg & 1) && el) {
987ab45e 9934 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
58117c9b
MD
9935 }
9936 if (!(reg & 4)) {
987ab45e 9937 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
58117c9b
MD
9938 }
9939 /* EPSR reads as zero */
9940 return xpsr_read(env) & mask;
9941 break;
9942 case 20: /* CONTROL */
8bfc26ea 9943 return env->v7m.control[env->v7m.secure];
50f11062
PM
9944 case 0x94: /* CONTROL_NS */
9945 /* We have to handle this here because unprivileged Secure code
9946 * can read the NS CONTROL register.
9947 */
9948 if (!env->v7m.secure) {
9949 return 0;
9950 }
9951 return env->v7m.control[M_REG_NS];
58117c9b
MD
9952 }
9953
9954 if (el == 0) {
9955 return 0; /* unprivileged reads others as zero */
9956 }
a47dddd7 9957
50f11062
PM
9958 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9959 switch (reg) {
9960 case 0x88: /* MSP_NS */
9961 if (!env->v7m.secure) {
9962 return 0;
9963 }
9964 return env->v7m.other_ss_msp;
9965 case 0x89: /* PSP_NS */
9966 if (!env->v7m.secure) {
9967 return 0;
9968 }
9969 return env->v7m.other_ss_psp;
9970 case 0x90: /* PRIMASK_NS */
9971 if (!env->v7m.secure) {
9972 return 0;
9973 }
9974 return env->v7m.primask[M_REG_NS];
9975 case 0x91: /* BASEPRI_NS */
9976 if (!env->v7m.secure) {
9977 return 0;
9978 }
9979 return env->v7m.basepri[M_REG_NS];
9980 case 0x93: /* FAULTMASK_NS */
9981 if (!env->v7m.secure) {
9982 return 0;
9983 }
9984 return env->v7m.faultmask[M_REG_NS];
9985 case 0x98: /* SP_NS */
9986 {
9987 /* This gives the non-secure SP selected based on whether we're
9988 * currently in handler mode or not, using the NS CONTROL.SPSEL.
9989 */
9990 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
9991
9992 if (!env->v7m.secure) {
9993 return 0;
9994 }
9995 if (!arm_v7m_is_handler_mode(env) && spsel) {
9996 return env->v7m.other_ss_psp;
9997 } else {
9998 return env->v7m.other_ss_msp;
9999 }
10000 }
10001 default:
10002 break;
10003 }
10004 }
10005
9ee6e8bb 10006 switch (reg) {
9ee6e8bb 10007 case 8: /* MSP */
1169d3aa 10008 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
9ee6e8bb 10009 case 9: /* PSP */
1169d3aa 10010 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
9ee6e8bb 10011 case 16: /* PRIMASK */
6d804834 10012 return env->v7m.primask[env->v7m.secure];
82845826
SH
10013 case 17: /* BASEPRI */
10014 case 18: /* BASEPRI_MAX */
acf94941 10015 return env->v7m.basepri[env->v7m.secure];
82845826 10016 case 19: /* FAULTMASK */
42a6686b 10017 return env->v7m.faultmask[env->v7m.secure];
9ee6e8bb 10018 default:
58117c9b
MD
10019 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10020 " register %d\n", reg);
9ee6e8bb
PB
10021 return 0;
10022 }
10023}
10024
b28b3377
PM
10025void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10026{
10027 /* We're passed bits [11..0] of the instruction; extract
10028 * SYSm and the mask bits.
10029 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10030 * we choose to treat them as if the mask bits were valid.
10031 * NB that the pseudocode 'mask' variable is bits [11..10],
10032 * whereas ours is [11..8].
10033 */
10034 uint32_t mask = extract32(maskreg, 8, 4);
10035 uint32_t reg = extract32(maskreg, 0, 8);
10036
58117c9b
MD
10037 if (arm_current_el(env) == 0 && reg > 7) {
10038 /* only xPSR sub-fields may be written by unprivileged */
10039 return;
10040 }
a47dddd7 10041
50f11062
PM
10042 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10043 switch (reg) {
10044 case 0x88: /* MSP_NS */
10045 if (!env->v7m.secure) {
10046 return;
10047 }
10048 env->v7m.other_ss_msp = val;
10049 return;
10050 case 0x89: /* PSP_NS */
10051 if (!env->v7m.secure) {
10052 return;
10053 }
10054 env->v7m.other_ss_psp = val;
10055 return;
10056 case 0x90: /* PRIMASK_NS */
10057 if (!env->v7m.secure) {
10058 return;
10059 }
10060 env->v7m.primask[M_REG_NS] = val & 1;
10061 return;
10062 case 0x91: /* BASEPRI_NS */
10063 if (!env->v7m.secure) {
10064 return;
10065 }
10066 env->v7m.basepri[M_REG_NS] = val & 0xff;
10067 return;
10068 case 0x93: /* FAULTMASK_NS */
10069 if (!env->v7m.secure) {
10070 return;
10071 }
10072 env->v7m.faultmask[M_REG_NS] = val & 1;
10073 return;
10074 case 0x98: /* SP_NS */
10075 {
10076 /* This gives the non-secure SP selected based on whether we're
10077 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10078 */
10079 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10080
10081 if (!env->v7m.secure) {
10082 return;
10083 }
10084 if (!arm_v7m_is_handler_mode(env) && spsel) {
10085 env->v7m.other_ss_psp = val;
10086 } else {
10087 env->v7m.other_ss_msp = val;
10088 }
10089 return;
10090 }
10091 default:
10092 break;
10093 }
10094 }
10095
9ee6e8bb 10096 switch (reg) {
58117c9b
MD
10097 case 0 ... 7: /* xPSR sub-fields */
10098 /* only APSR is actually writable */
b28b3377
PM
10099 if (!(reg & 4)) {
10100 uint32_t apsrmask = 0;
10101
10102 if (mask & 8) {
987ab45e 10103 apsrmask |= XPSR_NZCV | XPSR_Q;
b28b3377
PM
10104 }
10105 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
987ab45e 10106 apsrmask |= XPSR_GE;
b28b3377
PM
10107 }
10108 xpsr_write(env, val, apsrmask);
58117c9b 10109 }
9ee6e8bb
PB
10110 break;
10111 case 8: /* MSP */
1169d3aa 10112 if (v7m_using_psp(env)) {
9ee6e8bb 10113 env->v7m.other_sp = val;
abc24d86 10114 } else {
9ee6e8bb 10115 env->regs[13] = val;
abc24d86 10116 }
9ee6e8bb
PB
10117 break;
10118 case 9: /* PSP */
1169d3aa 10119 if (v7m_using_psp(env)) {
9ee6e8bb 10120 env->regs[13] = val;
abc24d86 10121 } else {
9ee6e8bb 10122 env->v7m.other_sp = val;
abc24d86 10123 }
9ee6e8bb
PB
10124 break;
10125 case 16: /* PRIMASK */
6d804834 10126 env->v7m.primask[env->v7m.secure] = val & 1;
9ee6e8bb 10127 break;
82845826 10128 case 17: /* BASEPRI */
acf94941 10129 env->v7m.basepri[env->v7m.secure] = val & 0xff;
9ee6e8bb 10130 break;
82845826 10131 case 18: /* BASEPRI_MAX */
9ee6e8bb 10132 val &= 0xff;
acf94941
PM
10133 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10134 || env->v7m.basepri[env->v7m.secure] == 0)) {
10135 env->v7m.basepri[env->v7m.secure] = val;
10136 }
9ee6e8bb 10137 break;
82845826 10138 case 19: /* FAULTMASK */
42a6686b 10139 env->v7m.faultmask[env->v7m.secure] = val & 1;
82845826 10140 break;
9ee6e8bb 10141 case 20: /* CONTROL */
792dac30
PM
10142 /* Writing to the SPSEL bit only has an effect if we are in
10143 * thread mode; other bits can be updated by any privileged code.
de2db7ec 10144 * write_v7m_control_spsel() deals with updating the SPSEL bit in
792dac30 10145 * env->v7m.control, so we only need update the others.
83d7f86d
PM
10146 * For v7M, we must just ignore explicit writes to SPSEL in handler
10147 * mode; for v8M the write is permitted but will have no effect.
792dac30 10148 */
83d7f86d
PM
10149 if (arm_feature(env, ARM_FEATURE_V8) ||
10150 !arm_v7m_is_handler_mode(env)) {
de2db7ec 10151 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
792dac30 10152 }
8bfc26ea
PM
10153 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10154 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
9ee6e8bb
PB
10155 break;
10156 default:
58117c9b
MD
10157 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10158 " register %d\n", reg);
9ee6e8bb
PB
10159 return;
10160 }
10161}
10162
5158de24
PM
10163uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10164{
10165 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10166 bool forceunpriv = op & 1;
10167 bool alt = op & 2;
10168 V8M_SAttributes sattrs = {};
10169 uint32_t tt_resp;
10170 bool r, rw, nsr, nsrw, mrvalid;
10171 int prot;
10172 MemTxAttrs attrs = {};
10173 hwaddr phys_addr;
10174 uint32_t fsr;
10175 ARMMMUIdx mmu_idx;
10176 uint32_t mregion;
10177 bool targetpriv;
10178 bool targetsec = env->v7m.secure;
10179
10180 /* Work out what the security state and privilege level we're
10181 * interested in is...
10182 */
10183 if (alt) {
10184 targetsec = !targetsec;
10185 }
10186
10187 if (forceunpriv) {
10188 targetpriv = false;
10189 } else {
10190 targetpriv = arm_v7m_is_handler_mode(env) ||
10191 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10192 }
10193
10194 /* ...and then figure out which MMU index this is */
10195 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10196
10197 /* We know that the MPU and SAU don't care about the access type
10198 * for our purposes beyond that we don't want to claim to be
10199 * an insn fetch, so we arbitrarily call this a read.
10200 */
10201
10202 /* MPU region info only available for privileged or if
10203 * inspecting the other MPU state.
10204 */
10205 if (arm_current_el(env) != 0 || alt) {
10206 /* We can ignore the return value as prot is always set */
10207 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
10208 &phys_addr, &attrs, &prot, &fsr, &mregion);
10209 if (mregion == -1) {
10210 mrvalid = false;
10211 mregion = 0;
10212 } else {
10213 mrvalid = true;
10214 }
10215 r = prot & PAGE_READ;
10216 rw = prot & PAGE_WRITE;
10217 } else {
10218 r = false;
10219 rw = false;
10220 mrvalid = false;
10221 mregion = 0;
10222 }
10223
10224 if (env->v7m.secure) {
10225 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10226 nsr = sattrs.ns && r;
10227 nsrw = sattrs.ns && rw;
10228 } else {
10229 sattrs.ns = true;
10230 nsr = false;
10231 nsrw = false;
10232 }
10233
10234 tt_resp = (sattrs.iregion << 24) |
10235 (sattrs.irvalid << 23) |
10236 ((!sattrs.ns) << 22) |
10237 (nsrw << 21) |
10238 (nsr << 20) |
10239 (rw << 19) |
10240 (r << 18) |
10241 (sattrs.srvalid << 17) |
10242 (mrvalid << 16) |
10243 (sattrs.sregion << 8) |
10244 mregion;
10245
10246 return tt_resp;
10247}
10248
b5ff1b31 10249#endif
6ddbc6e4 10250
aca3f40b
PM
10251void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10252{
10253 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10254 * Note that we do not implement the (architecturally mandated)
10255 * alignment fault for attempts to use this on Device memory
10256 * (which matches the usual QEMU behaviour of not implementing either
10257 * alignment faults or any memory attribute handling).
10258 */
10259
10260 ARMCPU *cpu = arm_env_get_cpu(env);
10261 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10262 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10263
10264#ifndef CONFIG_USER_ONLY
10265 {
10266 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10267 * the block size so we might have to do more than one TLB lookup.
10268 * We know that in fact for any v8 CPU the page size is at least 4K
10269 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10270 * 1K as an artefact of legacy v5 subpage support being present in the
10271 * same QEMU executable.
10272 */
10273 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10274 void *hostaddr[maxidx];
10275 int try, i;
97ed5ccd 10276 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 10277 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
10278
10279 for (try = 0; try < 2; try++) {
10280
10281 for (i = 0; i < maxidx; i++) {
10282 hostaddr[i] = tlb_vaddr_to_host(env,
10283 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 10284 1, mmu_idx);
aca3f40b
PM
10285 if (!hostaddr[i]) {
10286 break;
10287 }
10288 }
10289 if (i == maxidx) {
10290 /* If it's all in the TLB it's fair game for just writing to;
10291 * we know we don't need to update dirty status, etc.
10292 */
10293 for (i = 0; i < maxidx - 1; i++) {
10294 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10295 }
10296 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10297 return;
10298 }
10299 /* OK, try a store and see if we can populate the tlb. This
10300 * might cause an exception if the memory isn't writable,
10301 * in which case we will longjmp out of here. We must for
10302 * this purpose use the actual register value passed to us
10303 * so that we get the fault address right.
10304 */
01ecaf43 10305 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
aca3f40b
PM
10306 /* Now we can populate the other TLB entries, if any */
10307 for (i = 0; i < maxidx; i++) {
10308 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10309 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
01ecaf43 10310 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
aca3f40b
PM
10311 }
10312 }
10313 }
10314
10315 /* Slow path (probably attempt to do this to an I/O device or
10316 * similar, or clearing of a block of code we have translations
10317 * cached for). Just do a series of byte writes as the architecture
10318 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10319 * memset(), unmap() sequence here because:
10320 * + we'd need to account for the blocksize being larger than a page
10321 * + the direct-RAM access case is almost always going to be dealt
10322 * with in the fastpath code above, so there's no speed benefit
10323 * + we would have to deal with the map returning NULL because the
10324 * bounce buffer was in use
10325 */
10326 for (i = 0; i < blocklen; i++) {
01ecaf43 10327 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
aca3f40b
PM
10328 }
10329 }
10330#else
10331 memset(g2h(vaddr), 0, blocklen);
10332#endif
10333}
10334
6ddbc6e4
PB
10335/* Note that signed overflow is undefined in C. The following routines are
10336 careful to use unsigned types where modulo arithmetic is required.
10337 Failure to do so _will_ break on newer gcc. */
10338
10339/* Signed saturating arithmetic. */
10340
1654b2d6 10341/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10342static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10343{
10344 uint16_t res;
10345
10346 res = a + b;
10347 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10348 if (a & 0x8000)
10349 res = 0x8000;
10350 else
10351 res = 0x7fff;
10352 }
10353 return res;
10354}
10355
1654b2d6 10356/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10357static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10358{
10359 uint8_t res;
10360
10361 res = a + b;
10362 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10363 if (a & 0x80)
10364 res = 0x80;
10365 else
10366 res = 0x7f;
10367 }
10368 return res;
10369}
10370
1654b2d6 10371/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10372static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10373{
10374 uint16_t res;
10375
10376 res = a - b;
10377 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10378 if (a & 0x8000)
10379 res = 0x8000;
10380 else
10381 res = 0x7fff;
10382 }
10383 return res;
10384}
10385
1654b2d6 10386/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10387static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10388{
10389 uint8_t res;
10390
10391 res = a - b;
10392 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10393 if (a & 0x80)
10394 res = 0x80;
10395 else
10396 res = 0x7f;
10397 }
10398 return res;
10399}
10400
10401#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10402#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10403#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10404#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10405#define PFX q
10406
10407#include "op_addsub.h"
10408
10409/* Unsigned saturating arithmetic. */
460a09c1 10410static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
10411{
10412 uint16_t res;
10413 res = a + b;
10414 if (res < a)
10415 res = 0xffff;
10416 return res;
10417}
10418
460a09c1 10419static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 10420{
4c4fd3f8 10421 if (a > b)
6ddbc6e4
PB
10422 return a - b;
10423 else
10424 return 0;
10425}
10426
10427static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10428{
10429 uint8_t res;
10430 res = a + b;
10431 if (res < a)
10432 res = 0xff;
10433 return res;
10434}
10435
10436static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10437{
4c4fd3f8 10438 if (a > b)
6ddbc6e4
PB
10439 return a - b;
10440 else
10441 return 0;
10442}
10443
10444#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10445#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10446#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10447#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10448#define PFX uq
10449
10450#include "op_addsub.h"
10451
10452/* Signed modulo arithmetic. */
10453#define SARITH16(a, b, n, op) do { \
10454 int32_t sum; \
db6e2e65 10455 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
10456 RESULT(sum, n, 16); \
10457 if (sum >= 0) \
10458 ge |= 3 << (n * 2); \
10459 } while(0)
10460
10461#define SARITH8(a, b, n, op) do { \
10462 int32_t sum; \
db6e2e65 10463 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
10464 RESULT(sum, n, 8); \
10465 if (sum >= 0) \
10466 ge |= 1 << n; \
10467 } while(0)
10468
10469
10470#define ADD16(a, b, n) SARITH16(a, b, n, +)
10471#define SUB16(a, b, n) SARITH16(a, b, n, -)
10472#define ADD8(a, b, n) SARITH8(a, b, n, +)
10473#define SUB8(a, b, n) SARITH8(a, b, n, -)
10474#define PFX s
10475#define ARITH_GE
10476
10477#include "op_addsub.h"
10478
10479/* Unsigned modulo arithmetic. */
10480#define ADD16(a, b, n) do { \
10481 uint32_t sum; \
10482 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10483 RESULT(sum, n, 16); \
a87aa10b 10484 if ((sum >> 16) == 1) \
6ddbc6e4
PB
10485 ge |= 3 << (n * 2); \
10486 } while(0)
10487
10488#define ADD8(a, b, n) do { \
10489 uint32_t sum; \
10490 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10491 RESULT(sum, n, 8); \
a87aa10b
AZ
10492 if ((sum >> 8) == 1) \
10493 ge |= 1 << n; \
6ddbc6e4
PB
10494 } while(0)
10495
10496#define SUB16(a, b, n) do { \
10497 uint32_t sum; \
10498 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10499 RESULT(sum, n, 16); \
10500 if ((sum >> 16) == 0) \
10501 ge |= 3 << (n * 2); \
10502 } while(0)
10503
10504#define SUB8(a, b, n) do { \
10505 uint32_t sum; \
10506 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10507 RESULT(sum, n, 8); \
10508 if ((sum >> 8) == 0) \
a87aa10b 10509 ge |= 1 << n; \
6ddbc6e4
PB
10510 } while(0)
10511
10512#define PFX u
10513#define ARITH_GE
10514
10515#include "op_addsub.h"
10516
10517/* Halved signed arithmetic. */
10518#define ADD16(a, b, n) \
10519 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10520#define SUB16(a, b, n) \
10521 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10522#define ADD8(a, b, n) \
10523 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10524#define SUB8(a, b, n) \
10525 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10526#define PFX sh
10527
10528#include "op_addsub.h"
10529
10530/* Halved unsigned arithmetic. */
10531#define ADD16(a, b, n) \
10532 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10533#define SUB16(a, b, n) \
10534 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10535#define ADD8(a, b, n) \
10536 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10537#define SUB8(a, b, n) \
10538 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10539#define PFX uh
10540
10541#include "op_addsub.h"
10542
10543static inline uint8_t do_usad(uint8_t a, uint8_t b)
10544{
10545 if (a > b)
10546 return a - b;
10547 else
10548 return b - a;
10549}
10550
10551/* Unsigned sum of absolute byte differences. */
10552uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10553{
10554 uint32_t sum;
10555 sum = do_usad(a, b);
10556 sum += do_usad(a >> 8, b >> 8);
10557 sum += do_usad(a >> 16, b >>16);
10558 sum += do_usad(a >> 24, b >> 24);
10559 return sum;
10560}
10561
10562/* For ARMv6 SEL instruction. */
10563uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10564{
10565 uint32_t mask;
10566
10567 mask = 0;
10568 if (flags & 1)
10569 mask |= 0xff;
10570 if (flags & 2)
10571 mask |= 0xff00;
10572 if (flags & 4)
10573 mask |= 0xff0000;
10574 if (flags & 8)
10575 mask |= 0xff000000;
10576 return (a & mask) | (b & ~mask);
10577}
10578
b90372ad
PM
10579/* VFP support. We follow the convention used for VFP instructions:
10580 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
10581 "d" suffix. */
10582
10583/* Convert host exception flags to vfp form. */
10584static inline int vfp_exceptbits_from_host(int host_bits)
10585{
10586 int target_bits = 0;
10587
10588 if (host_bits & float_flag_invalid)
10589 target_bits |= 1;
10590 if (host_bits & float_flag_divbyzero)
10591 target_bits |= 2;
10592 if (host_bits & float_flag_overflow)
10593 target_bits |= 4;
36802b6b 10594 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
10595 target_bits |= 8;
10596 if (host_bits & float_flag_inexact)
10597 target_bits |= 0x10;
cecd8504
PM
10598 if (host_bits & float_flag_input_denormal)
10599 target_bits |= 0x80;
4373f3ce
PB
10600 return target_bits;
10601}
10602
0ecb72a5 10603uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
10604{
10605 int i;
10606 uint32_t fpscr;
10607
10608 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
10609 | (env->vfp.vec_len << 16)
10610 | (env->vfp.vec_stride << 20);
10611 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 10612 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
10613 fpscr |= vfp_exceptbits_from_host(i);
10614 return fpscr;
10615}
10616
0ecb72a5 10617uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
10618{
10619 return HELPER(vfp_get_fpscr)(env);
10620}
10621
4373f3ce
PB
10622/* Convert vfp exception flags to target form. */
10623static inline int vfp_exceptbits_to_host(int target_bits)
10624{
10625 int host_bits = 0;
10626
10627 if (target_bits & 1)
10628 host_bits |= float_flag_invalid;
10629 if (target_bits & 2)
10630 host_bits |= float_flag_divbyzero;
10631 if (target_bits & 4)
10632 host_bits |= float_flag_overflow;
10633 if (target_bits & 8)
10634 host_bits |= float_flag_underflow;
10635 if (target_bits & 0x10)
10636 host_bits |= float_flag_inexact;
cecd8504
PM
10637 if (target_bits & 0x80)
10638 host_bits |= float_flag_input_denormal;
4373f3ce
PB
10639 return host_bits;
10640}
10641
0ecb72a5 10642void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
10643{
10644 int i;
10645 uint32_t changed;
10646
10647 changed = env->vfp.xregs[ARM_VFP_FPSCR];
10648 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
10649 env->vfp.vec_len = (val >> 16) & 7;
10650 env->vfp.vec_stride = (val >> 20) & 3;
10651
10652 changed ^= val;
10653 if (changed & (3 << 22)) {
10654 i = (val >> 22) & 3;
10655 switch (i) {
4d3da0f3 10656 case FPROUNDING_TIEEVEN:
4373f3ce
PB
10657 i = float_round_nearest_even;
10658 break;
4d3da0f3 10659 case FPROUNDING_POSINF:
4373f3ce
PB
10660 i = float_round_up;
10661 break;
4d3da0f3 10662 case FPROUNDING_NEGINF:
4373f3ce
PB
10663 i = float_round_down;
10664 break;
4d3da0f3 10665 case FPROUNDING_ZERO:
4373f3ce
PB
10666 i = float_round_to_zero;
10667 break;
10668 }
10669 set_float_rounding_mode(i, &env->vfp.fp_status);
10670 }
cecd8504 10671 if (changed & (1 << 24)) {
fe76d976 10672 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
10673 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
10674 }
5c7908ed
PB
10675 if (changed & (1 << 25))
10676 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 10677
b12c390b 10678 i = vfp_exceptbits_to_host(val);
4373f3ce 10679 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 10680 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
10681}
10682
0ecb72a5 10683void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
10684{
10685 HELPER(vfp_set_fpscr)(env, val);
10686}
10687
4373f3ce
PB
10688#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
10689
10690#define VFP_BINOP(name) \
ae1857ec 10691float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 10692{ \
ae1857ec
PM
10693 float_status *fpst = fpstp; \
10694 return float32_ ## name(a, b, fpst); \
4373f3ce 10695} \
ae1857ec 10696float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 10697{ \
ae1857ec
PM
10698 float_status *fpst = fpstp; \
10699 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
10700}
10701VFP_BINOP(add)
10702VFP_BINOP(sub)
10703VFP_BINOP(mul)
10704VFP_BINOP(div)
f71a2ae5
PM
10705VFP_BINOP(min)
10706VFP_BINOP(max)
10707VFP_BINOP(minnum)
10708VFP_BINOP(maxnum)
4373f3ce
PB
10709#undef VFP_BINOP
10710
10711float32 VFP_HELPER(neg, s)(float32 a)
10712{
10713 return float32_chs(a);
10714}
10715
10716float64 VFP_HELPER(neg, d)(float64 a)
10717{
66230e0d 10718 return float64_chs(a);
4373f3ce
PB
10719}
10720
10721float32 VFP_HELPER(abs, s)(float32 a)
10722{
10723 return float32_abs(a);
10724}
10725
10726float64 VFP_HELPER(abs, d)(float64 a)
10727{
66230e0d 10728 return float64_abs(a);
4373f3ce
PB
10729}
10730
0ecb72a5 10731float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
10732{
10733 return float32_sqrt(a, &env->vfp.fp_status);
10734}
10735
0ecb72a5 10736float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
10737{
10738 return float64_sqrt(a, &env->vfp.fp_status);
10739}
10740
10741/* XXX: check quiet/signaling case */
10742#define DO_VFP_cmp(p, type) \
0ecb72a5 10743void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
10744{ \
10745 uint32_t flags; \
10746 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
10747 case 0: flags = 0x6; break; \
10748 case -1: flags = 0x8; break; \
10749 case 1: flags = 0x2; break; \
10750 default: case 2: flags = 0x3; break; \
10751 } \
10752 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10753 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10754} \
0ecb72a5 10755void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
10756{ \
10757 uint32_t flags; \
10758 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
10759 case 0: flags = 0x6; break; \
10760 case -1: flags = 0x8; break; \
10761 case 1: flags = 0x2; break; \
10762 default: case 2: flags = 0x3; break; \
10763 } \
10764 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10765 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10766}
10767DO_VFP_cmp(s, float32)
10768DO_VFP_cmp(d, float64)
10769#undef DO_VFP_cmp
10770
5500b06c 10771/* Integer to float and float to integer conversions */
4373f3ce 10772
5500b06c
PM
10773#define CONV_ITOF(name, fsz, sign) \
10774 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
10775{ \
10776 float_status *fpst = fpstp; \
85836979 10777 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
10778}
10779
5500b06c
PM
10780#define CONV_FTOI(name, fsz, sign, round) \
10781uint32_t HELPER(name)(float##fsz x, void *fpstp) \
10782{ \
10783 float_status *fpst = fpstp; \
10784 if (float##fsz##_is_any_nan(x)) { \
10785 float_raise(float_flag_invalid, fpst); \
10786 return 0; \
10787 } \
10788 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
10789}
10790
5500b06c
PM
10791#define FLOAT_CONVS(name, p, fsz, sign) \
10792CONV_ITOF(vfp_##name##to##p, fsz, sign) \
10793CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
10794CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 10795
5500b06c
PM
10796FLOAT_CONVS(si, s, 32, )
10797FLOAT_CONVS(si, d, 64, )
10798FLOAT_CONVS(ui, s, 32, u)
10799FLOAT_CONVS(ui, d, 64, u)
4373f3ce 10800
5500b06c
PM
10801#undef CONV_ITOF
10802#undef CONV_FTOI
10803#undef FLOAT_CONVS
4373f3ce
PB
10804
10805/* floating point conversion */
0ecb72a5 10806float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 10807{
2d627737
PM
10808 float64 r = float32_to_float64(x, &env->vfp.fp_status);
10809 /* ARM requires that S<->D conversion of any kind of NaN generates
10810 * a quiet NaN by forcing the most significant frac bit to 1.
10811 */
af39bc8c 10812 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
10813}
10814
0ecb72a5 10815float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 10816{
2d627737
PM
10817 float32 r = float64_to_float32(x, &env->vfp.fp_status);
10818 /* ARM requires that S<->D conversion of any kind of NaN generates
10819 * a quiet NaN by forcing the most significant frac bit to 1.
10820 */
af39bc8c 10821 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
10822}
10823
10824/* VFP3 fixed point conversion. */
16d5b3ca 10825#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
10826float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
10827 void *fpstp) \
4373f3ce 10828{ \
5500b06c 10829 float_status *fpst = fpstp; \
622465e1 10830 float##fsz tmp; \
8ed697e8 10831 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 10832 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
10833}
10834
abe66f70
PM
10835/* Notice that we want only input-denormal exception flags from the
10836 * scalbn operation: the other possible flags (overflow+inexact if
10837 * we overflow to infinity, output-denormal) aren't correct for the
10838 * complete scale-and-convert operation.
10839 */
16d5b3ca
WN
10840#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
10841uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
10842 uint32_t shift, \
10843 void *fpstp) \
4373f3ce 10844{ \
5500b06c 10845 float_status *fpst = fpstp; \
abe66f70 10846 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
10847 float##fsz tmp; \
10848 if (float##fsz##_is_any_nan(x)) { \
5500b06c 10849 float_raise(float_flag_invalid, fpst); \
622465e1 10850 return 0; \
09d9487f 10851 } \
5500b06c 10852 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
10853 old_exc_flags |= get_float_exception_flags(fpst) \
10854 & float_flag_input_denormal; \
10855 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 10856 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
10857}
10858
16d5b3ca
WN
10859#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
10860VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
10861VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
10862VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
10863
10864#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
10865VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10866VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 10867
8ed697e8
WN
10868VFP_CONV_FIX(sh, d, 64, 64, int16)
10869VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 10870VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
10871VFP_CONV_FIX(uh, d, 64, 64, uint16)
10872VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 10873VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
10874VFP_CONV_FIX(sh, s, 32, 32, int16)
10875VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 10876VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
10877VFP_CONV_FIX(uh, s, 32, 32, uint16)
10878VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 10879VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 10880#undef VFP_CONV_FIX
16d5b3ca
WN
10881#undef VFP_CONV_FIX_FLOAT
10882#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 10883
52a1f6a3
AG
10884/* Set the current fp rounding mode and return the old one.
10885 * The argument is a softfloat float_round_ value.
10886 */
10887uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
10888{
10889 float_status *fp_status = &env->vfp.fp_status;
10890
10891 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10892 set_float_rounding_mode(rmode, fp_status);
10893
10894 return prev_rmode;
10895}
10896
43630e58
WN
10897/* Set the current fp rounding mode in the standard fp status and return
10898 * the old one. This is for NEON instructions that need to change the
10899 * rounding mode but wish to use the standard FPSCR values for everything
10900 * else. Always set the rounding mode back to the correct value after
10901 * modifying it.
10902 * The argument is a softfloat float_round_ value.
10903 */
10904uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
10905{
10906 float_status *fp_status = &env->vfp.standard_fp_status;
10907
10908 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10909 set_float_rounding_mode(rmode, fp_status);
10910
10911 return prev_rmode;
10912}
10913
60011498 10914/* Half precision conversions. */
0ecb72a5 10915static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 10916{
60011498 10917 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
10918 float32 r = float16_to_float32(make_float16(a), ieee, s);
10919 if (ieee) {
af39bc8c 10920 return float32_maybe_silence_nan(r, s);
fb91678d
PM
10921 }
10922 return r;
60011498
PB
10923}
10924
0ecb72a5 10925static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 10926{
60011498 10927 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
10928 float16 r = float32_to_float16(a, ieee, s);
10929 if (ieee) {
af39bc8c 10930 r = float16_maybe_silence_nan(r, s);
fb91678d
PM
10931 }
10932 return float16_val(r);
60011498
PB
10933}
10934
0ecb72a5 10935float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
10936{
10937 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
10938}
10939
0ecb72a5 10940uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
10941{
10942 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
10943}
10944
0ecb72a5 10945float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
10946{
10947 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
10948}
10949
0ecb72a5 10950uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
10951{
10952 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
10953}
10954
8900aad2
PM
10955float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
10956{
10957 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10958 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
10959 if (ieee) {
af39bc8c 10960 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
10961 }
10962 return r;
10963}
10964
10965uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
10966{
10967 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10968 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
10969 if (ieee) {
af39bc8c 10970 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
10971 }
10972 return float16_val(r);
10973}
10974
dda3ec49 10975#define float32_two make_float32(0x40000000)
6aae3df1
PM
10976#define float32_three make_float32(0x40400000)
10977#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 10978
0ecb72a5 10979float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 10980{
dda3ec49
PM
10981 float_status *s = &env->vfp.standard_fp_status;
10982 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
10983 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
10984 if (!(float32_is_zero(a) || float32_is_zero(b))) {
10985 float_raise(float_flag_input_denormal, s);
10986 }
dda3ec49
PM
10987 return float32_two;
10988 }
10989 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
10990}
10991
0ecb72a5 10992float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 10993{
71826966 10994 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
10995 float32 product;
10996 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
10997 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
10998 if (!(float32_is_zero(a) || float32_is_zero(b))) {
10999 float_raise(float_flag_input_denormal, s);
11000 }
6aae3df1 11001 return float32_one_point_five;
9ea62f57 11002 }
6aae3df1
PM
11003 product = float32_mul(a, b, s);
11004 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
11005}
11006
8f8e3aa4
PB
11007/* NEON helpers. */
11008
56bf4fe2
CL
11009/* Constants 256 and 512 are used in some helpers; we avoid relying on
11010 * int->float conversions at run-time. */
11011#define float64_256 make_float64(0x4070000000000000LL)
11012#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
11013#define float32_maxnorm make_float32(0x7f7fffff)
11014#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 11015
b6d4443a
AB
11016/* Reciprocal functions
11017 *
11018 * The algorithm that must be used to calculate the estimate
11019 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 11020 */
b6d4443a
AB
11021
11022static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 11023{
1146a817
PM
11024 /* These calculations mustn't set any fp exception flags,
11025 * so we use a local copy of the fp_status.
11026 */
b6d4443a 11027 float_status dummy_status = *real_fp_status;
1146a817 11028 float_status *s = &dummy_status;
fe0e4872
CL
11029 /* q = (int)(a * 512.0) */
11030 float64 q = float64_mul(float64_512, a, s);
11031 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11032
11033 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
11034 q = int64_to_float64(q_int, s);
11035 q = float64_add(q, float64_half, s);
11036 q = float64_div(q, float64_512, s);
11037 q = float64_div(float64_one, q, s);
11038
11039 /* s = (int)(256.0 * r + 0.5) */
11040 q = float64_mul(q, float64_256, s);
11041 q = float64_add(q, float64_half, s);
11042 q_int = float64_to_int64_round_to_zero(q, s);
11043
11044 /* return (double)s / 256.0 */
11045 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11046}
11047
b6d4443a
AB
11048/* Common wrapper to call recip_estimate */
11049static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 11050{
b6d4443a
AB
11051 uint64_t val64 = float64_val(num);
11052 uint64_t frac = extract64(val64, 0, 52);
11053 int64_t exp = extract64(val64, 52, 11);
11054 uint64_t sbit;
11055 float64 scaled, estimate;
fe0e4872 11056
b6d4443a
AB
11057 /* Generate the scaled number for the estimate function */
11058 if (exp == 0) {
11059 if (extract64(frac, 51, 1) == 0) {
11060 exp = -1;
11061 frac = extract64(frac, 0, 50) << 2;
11062 } else {
11063 frac = extract64(frac, 0, 51) << 1;
11064 }
11065 }
fe0e4872 11066
b6d4443a
AB
11067 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
11068 scaled = make_float64((0x3feULL << 52)
11069 | extract64(frac, 44, 8) << 44);
11070
11071 estimate = recip_estimate(scaled, fpst);
11072
11073 /* Build new result */
11074 val64 = float64_val(estimate);
11075 sbit = 0x8000000000000000ULL & val64;
11076 exp = off - exp;
11077 frac = extract64(val64, 0, 52);
11078
11079 if (exp == 0) {
11080 frac = 1ULL << 51 | extract64(frac, 1, 51);
11081 } else if (exp == -1) {
11082 frac = 1ULL << 50 | extract64(frac, 2, 50);
11083 exp = 0;
11084 }
11085
11086 return make_float64(sbit | (exp << 52) | frac);
11087}
11088
11089static bool round_to_inf(float_status *fpst, bool sign_bit)
11090{
11091 switch (fpst->float_rounding_mode) {
11092 case float_round_nearest_even: /* Round to Nearest */
11093 return true;
11094 case float_round_up: /* Round to +Inf */
11095 return !sign_bit;
11096 case float_round_down: /* Round to -Inf */
11097 return sign_bit;
11098 case float_round_to_zero: /* Round to Zero */
11099 return false;
11100 }
11101
11102 g_assert_not_reached();
11103}
11104
11105float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11106{
11107 float_status *fpst = fpstp;
11108 float32 f32 = float32_squash_input_denormal(input, fpst);
11109 uint32_t f32_val = float32_val(f32);
11110 uint32_t f32_sbit = 0x80000000ULL & f32_val;
11111 int32_t f32_exp = extract32(f32_val, 23, 8);
11112 uint32_t f32_frac = extract32(f32_val, 0, 23);
11113 float64 f64, r64;
11114 uint64_t r64_val;
11115 int64_t r64_exp;
11116 uint64_t r64_frac;
11117
11118 if (float32_is_any_nan(f32)) {
11119 float32 nan = f32;
af39bc8c 11120 if (float32_is_signaling_nan(f32, fpst)) {
b6d4443a 11121 float_raise(float_flag_invalid, fpst);
af39bc8c 11122 nan = float32_maybe_silence_nan(f32, fpst);
fe0e4872 11123 }
b6d4443a 11124 if (fpst->default_nan_mode) {
af39bc8c 11125 nan = float32_default_nan(fpst);
43fe9bdb 11126 }
b6d4443a
AB
11127 return nan;
11128 } else if (float32_is_infinity(f32)) {
11129 return float32_set_sign(float32_zero, float32_is_neg(f32));
11130 } else if (float32_is_zero(f32)) {
11131 float_raise(float_flag_divbyzero, fpst);
11132 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11133 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
11134 /* Abs(value) < 2.0^-128 */
11135 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11136 if (round_to_inf(fpst, f32_sbit)) {
11137 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11138 } else {
11139 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
11140 }
11141 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11142 float_raise(float_flag_underflow, fpst);
11143 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
11144 }
11145
fe0e4872 11146
b6d4443a
AB
11147 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
11148 r64 = call_recip_estimate(f64, 253, fpst);
11149 r64_val = float64_val(r64);
11150 r64_exp = extract64(r64_val, 52, 11);
11151 r64_frac = extract64(r64_val, 0, 52);
11152
11153 /* result = sign : result_exp<7:0> : fraction<51:29>; */
11154 return make_float32(f32_sbit |
11155 (r64_exp & 0xff) << 23 |
11156 extract64(r64_frac, 29, 24));
11157}
11158
11159float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11160{
11161 float_status *fpst = fpstp;
11162 float64 f64 = float64_squash_input_denormal(input, fpst);
11163 uint64_t f64_val = float64_val(f64);
11164 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
11165 int64_t f64_exp = extract64(f64_val, 52, 11);
11166 float64 r64;
11167 uint64_t r64_val;
11168 int64_t r64_exp;
11169 uint64_t r64_frac;
11170
11171 /* Deal with any special cases */
11172 if (float64_is_any_nan(f64)) {
11173 float64 nan = f64;
af39bc8c 11174 if (float64_is_signaling_nan(f64, fpst)) {
b6d4443a 11175 float_raise(float_flag_invalid, fpst);
af39bc8c 11176 nan = float64_maybe_silence_nan(f64, fpst);
b6d4443a
AB
11177 }
11178 if (fpst->default_nan_mode) {
af39bc8c 11179 nan = float64_default_nan(fpst);
b6d4443a
AB
11180 }
11181 return nan;
11182 } else if (float64_is_infinity(f64)) {
11183 return float64_set_sign(float64_zero, float64_is_neg(f64));
11184 } else if (float64_is_zero(f64)) {
11185 float_raise(float_flag_divbyzero, fpst);
11186 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11187 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11188 /* Abs(value) < 2.0^-1024 */
11189 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11190 if (round_to_inf(fpst, f64_sbit)) {
11191 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11192 } else {
11193 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
11194 }
fc1792e9 11195 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
11196 float_raise(float_flag_underflow, fpst);
11197 return float64_set_sign(float64_zero, float64_is_neg(f64));
11198 }
fe0e4872 11199
b6d4443a
AB
11200 r64 = call_recip_estimate(f64, 2045, fpst);
11201 r64_val = float64_val(r64);
11202 r64_exp = extract64(r64_val, 52, 11);
11203 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 11204
b6d4443a
AB
11205 /* result = sign : result_exp<10:0> : fraction<51:0> */
11206 return make_float64(f64_sbit |
11207 ((r64_exp & 0x7ff) << 52) |
11208 r64_frac);
4373f3ce
PB
11209}
11210
e07be5d2
CL
11211/* The algorithm that must be used to calculate the estimate
11212 * is specified by the ARM ARM.
11213 */
c2fb418e 11214static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 11215{
1146a817
PM
11216 /* These calculations mustn't set any fp exception flags,
11217 * so we use a local copy of the fp_status.
11218 */
c2fb418e 11219 float_status dummy_status = *real_fp_status;
1146a817 11220 float_status *s = &dummy_status;
e07be5d2
CL
11221 float64 q;
11222 int64_t q_int;
11223
11224 if (float64_lt(a, float64_half, s)) {
11225 /* range 0.25 <= a < 0.5 */
11226
11227 /* a in units of 1/512 rounded down */
11228 /* q0 = (int)(a * 512.0); */
11229 q = float64_mul(float64_512, a, s);
11230 q_int = float64_to_int64_round_to_zero(q, s);
11231
11232 /* reciprocal root r */
11233 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
11234 q = int64_to_float64(q_int, s);
11235 q = float64_add(q, float64_half, s);
11236 q = float64_div(q, float64_512, s);
11237 q = float64_sqrt(q, s);
11238 q = float64_div(float64_one, q, s);
11239 } else {
11240 /* range 0.5 <= a < 1.0 */
11241
11242 /* a in units of 1/256 rounded down */
11243 /* q1 = (int)(a * 256.0); */
11244 q = float64_mul(float64_256, a, s);
11245 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11246
11247 /* reciprocal root r */
11248 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
11249 q = int64_to_float64(q_int, s);
11250 q = float64_add(q, float64_half, s);
11251 q = float64_div(q, float64_256, s);
11252 q = float64_sqrt(q, s);
11253 q = float64_div(float64_one, q, s);
11254 }
11255 /* r in units of 1/256 rounded to nearest */
11256 /* s = (int)(256.0 * r + 0.5); */
11257
11258 q = float64_mul(q, float64_256,s );
11259 q = float64_add(q, float64_half, s);
11260 q_int = float64_to_int64_round_to_zero(q, s);
11261
11262 /* return (double)s / 256.0;*/
11263 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11264}
11265
c2fb418e 11266float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 11267{
c2fb418e
AB
11268 float_status *s = fpstp;
11269 float32 f32 = float32_squash_input_denormal(input, s);
11270 uint32_t val = float32_val(f32);
11271 uint32_t f32_sbit = 0x80000000 & val;
11272 int32_t f32_exp = extract32(val, 23, 8);
11273 uint32_t f32_frac = extract32(val, 0, 23);
11274 uint64_t f64_frac;
11275 uint64_t val64;
e07be5d2
CL
11276 int result_exp;
11277 float64 f64;
e07be5d2 11278
c2fb418e
AB
11279 if (float32_is_any_nan(f32)) {
11280 float32 nan = f32;
af39bc8c 11281 if (float32_is_signaling_nan(f32, s)) {
e07be5d2 11282 float_raise(float_flag_invalid, s);
af39bc8c 11283 nan = float32_maybe_silence_nan(f32, s);
e07be5d2 11284 }
c2fb418e 11285 if (s->default_nan_mode) {
af39bc8c 11286 nan = float32_default_nan(s);
43fe9bdb 11287 }
c2fb418e
AB
11288 return nan;
11289 } else if (float32_is_zero(f32)) {
e07be5d2 11290 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
11291 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11292 } else if (float32_is_neg(f32)) {
e07be5d2 11293 float_raise(float_flag_invalid, s);
af39bc8c 11294 return float32_default_nan(s);
c2fb418e 11295 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
11296 return float32_zero;
11297 }
11298
c2fb418e 11299 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 11300 * preserving the parity of the exponent. */
c2fb418e
AB
11301
11302 f64_frac = ((uint64_t) f32_frac) << 29;
11303 if (f32_exp == 0) {
11304 while (extract64(f64_frac, 51, 1) == 0) {
11305 f64_frac = f64_frac << 1;
11306 f32_exp = f32_exp-1;
11307 }
11308 f64_frac = extract64(f64_frac, 0, 51) << 1;
11309 }
11310
11311 if (extract64(f32_exp, 0, 1) == 0) {
11312 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 11313 | (0x3feULL << 52)
c2fb418e 11314 | f64_frac);
e07be5d2 11315 } else {
c2fb418e 11316 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 11317 | (0x3fdULL << 52)
c2fb418e 11318 | f64_frac);
e07be5d2
CL
11319 }
11320
c2fb418e 11321 result_exp = (380 - f32_exp) / 2;
e07be5d2 11322
c2fb418e 11323 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
11324
11325 val64 = float64_val(f64);
11326
26cc6abf 11327 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
11328 | ((val64 >> 29) & 0x7fffff);
11329 return make_float32(val);
4373f3ce
PB
11330}
11331
c2fb418e
AB
11332float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
11333{
11334 float_status *s = fpstp;
11335 float64 f64 = float64_squash_input_denormal(input, s);
11336 uint64_t val = float64_val(f64);
11337 uint64_t f64_sbit = 0x8000000000000000ULL & val;
11338 int64_t f64_exp = extract64(val, 52, 11);
11339 uint64_t f64_frac = extract64(val, 0, 52);
11340 int64_t result_exp;
11341 uint64_t result_frac;
11342
11343 if (float64_is_any_nan(f64)) {
11344 float64 nan = f64;
af39bc8c 11345 if (float64_is_signaling_nan(f64, s)) {
c2fb418e 11346 float_raise(float_flag_invalid, s);
af39bc8c 11347 nan = float64_maybe_silence_nan(f64, s);
c2fb418e
AB
11348 }
11349 if (s->default_nan_mode) {
af39bc8c 11350 nan = float64_default_nan(s);
c2fb418e
AB
11351 }
11352 return nan;
11353 } else if (float64_is_zero(f64)) {
11354 float_raise(float_flag_divbyzero, s);
11355 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11356 } else if (float64_is_neg(f64)) {
11357 float_raise(float_flag_invalid, s);
af39bc8c 11358 return float64_default_nan(s);
c2fb418e
AB
11359 } else if (float64_is_infinity(f64)) {
11360 return float64_zero;
11361 }
11362
11363 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11364 * preserving the parity of the exponent. */
11365
11366 if (f64_exp == 0) {
11367 while (extract64(f64_frac, 51, 1) == 0) {
11368 f64_frac = f64_frac << 1;
11369 f64_exp = f64_exp - 1;
11370 }
11371 f64_frac = extract64(f64_frac, 0, 51) << 1;
11372 }
11373
11374 if (extract64(f64_exp, 0, 1) == 0) {
11375 f64 = make_float64(f64_sbit
11376 | (0x3feULL << 52)
11377 | f64_frac);
11378 } else {
11379 f64 = make_float64(f64_sbit
11380 | (0x3fdULL << 52)
11381 | f64_frac);
11382 }
11383
11384 result_exp = (3068 - f64_exp) / 2;
11385
11386 f64 = recip_sqrt_estimate(f64, s);
11387
11388 result_frac = extract64(float64_val(f64), 0, 52);
11389
11390 return make_float64(f64_sbit |
11391 ((result_exp & 0x7ff) << 52) |
11392 result_frac);
11393}
11394
b6d4443a 11395uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 11396{
b6d4443a 11397 float_status *s = fpstp;
fe0e4872
CL
11398 float64 f64;
11399
11400 if ((a & 0x80000000) == 0) {
11401 return 0xffffffff;
11402 }
11403
11404 f64 = make_float64((0x3feULL << 52)
11405 | ((int64_t)(a & 0x7fffffff) << 21));
11406
b6d4443a 11407 f64 = recip_estimate(f64, s);
fe0e4872
CL
11408
11409 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
11410}
11411
c2fb418e 11412uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 11413{
c2fb418e 11414 float_status *fpst = fpstp;
e07be5d2
CL
11415 float64 f64;
11416
11417 if ((a & 0xc0000000) == 0) {
11418 return 0xffffffff;
11419 }
11420
11421 if (a & 0x80000000) {
11422 f64 = make_float64((0x3feULL << 52)
11423 | ((uint64_t)(a & 0x7fffffff) << 21));
11424 } else { /* bits 31-30 == '01' */
11425 f64 = make_float64((0x3fdULL << 52)
11426 | ((uint64_t)(a & 0x3fffffff) << 22));
11427 }
11428
c2fb418e 11429 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
11430
11431 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 11432}
fe1479c3 11433
da97f52c
PM
11434/* VFPv4 fused multiply-accumulate */
11435float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11436{
11437 float_status *fpst = fpstp;
11438 return float32_muladd(a, b, c, 0, fpst);
11439}
11440
11441float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11442{
11443 float_status *fpst = fpstp;
11444 return float64_muladd(a, b, c, 0, fpst);
11445}
d9b0848d
PM
11446
11447/* ARMv8 round to integral */
11448float32 HELPER(rints_exact)(float32 x, void *fp_status)
11449{
11450 return float32_round_to_int(x, fp_status);
11451}
11452
11453float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11454{
11455 return float64_round_to_int(x, fp_status);
11456}
11457
11458float32 HELPER(rints)(float32 x, void *fp_status)
11459{
11460 int old_flags = get_float_exception_flags(fp_status), new_flags;
11461 float32 ret;
11462
11463 ret = float32_round_to_int(x, fp_status);
11464
11465 /* Suppress any inexact exceptions the conversion produced */
11466 if (!(old_flags & float_flag_inexact)) {
11467 new_flags = get_float_exception_flags(fp_status);
11468 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11469 }
11470
11471 return ret;
11472}
11473
11474float64 HELPER(rintd)(float64 x, void *fp_status)
11475{
11476 int old_flags = get_float_exception_flags(fp_status), new_flags;
11477 float64 ret;
11478
11479 ret = float64_round_to_int(x, fp_status);
11480
11481 new_flags = get_float_exception_flags(fp_status);
11482
11483 /* Suppress any inexact exceptions the conversion produced */
11484 if (!(old_flags & float_flag_inexact)) {
11485 new_flags = get_float_exception_flags(fp_status);
11486 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11487 }
11488
11489 return ret;
11490}
9972da66
WN
11491
11492/* Convert ARM rounding mode to softfloat */
11493int arm_rmode_to_sf(int rmode)
11494{
11495 switch (rmode) {
11496 case FPROUNDING_TIEAWAY:
11497 rmode = float_round_ties_away;
11498 break;
11499 case FPROUNDING_ODD:
11500 /* FIXME: add support for TIEAWAY and ODD */
11501 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
11502 rmode);
11503 case FPROUNDING_TIEEVEN:
11504 default:
11505 rmode = float_round_nearest_even;
11506 break;
11507 case FPROUNDING_POSINF:
11508 rmode = float_round_up;
11509 break;
11510 case FPROUNDING_NEGINF:
11511 rmode = float_round_down;
11512 break;
11513 case FPROUNDING_ZERO:
11514 rmode = float_round_to_zero;
11515 break;
11516 }
11517 return rmode;
11518}
eb0ecd5a 11519
aa633469
PM
11520/* CRC helpers.
11521 * The upper bytes of val (above the number specified by 'bytes') must have
11522 * been zeroed out by the caller.
11523 */
eb0ecd5a
WN
11524uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11525{
11526 uint8_t buf[4];
11527
aa633469 11528 stl_le_p(buf, val);
eb0ecd5a
WN
11529
11530 /* zlib crc32 converts the accumulator and output to one's complement. */
11531 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11532}
11533
11534uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11535{
11536 uint8_t buf[4];
11537
aa633469 11538 stl_le_p(buf, val);
eb0ecd5a
WN
11539
11540 /* Linux crc32c converts the output to one's complement. */
11541 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11542}