]> git.proxmox.com Git - mirror_qemu.git/blob - target/arm/helper.c
Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20180301' into...
[mirror_qemu.git] / target / arm / helper.c
1 #include "qemu/osdep.h"
2 #include "trace.h"
3 #include "cpu.h"
4 #include "internals.h"
5 #include "exec/gdbstub.h"
6 #include "exec/helper-proto.h"
7 #include "qemu/host-utils.h"
8 #include "sysemu/arch_init.h"
9 #include "sysemu/sysemu.h"
10 #include "qemu/bitops.h"
11 #include "qemu/crc32c.h"
12 #include "exec/exec-all.h"
13 #include "exec/cpu_ldst.h"
14 #include "arm_ldst.h"
15 #include <zlib.h> /* For crc32 */
16 #include "exec/semihost.h"
17 #include "sysemu/kvm.h"
18 #include "fpu/softfloat.h"
19
20 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
21
22 #ifndef CONFIG_USER_ONLY
23 /* Cacheability and shareability attributes for a memory access */
24 typedef struct ARMCacheAttrs {
25 unsigned int attrs:8; /* as in the MAIR register encoding */
26 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
27 } ARMCacheAttrs;
28
29 static bool get_phys_addr(CPUARMState *env, target_ulong address,
30 MMUAccessType access_type, ARMMMUIdx mmu_idx,
31 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
32 target_ulong *page_size,
33 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
34
35 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
36 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
38 target_ulong *page_size_ptr,
39 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
40
41 /* Security attributes for an address, as returned by v8m_security_lookup. */
42 typedef struct V8M_SAttributes {
43 bool ns;
44 bool nsc;
45 uint8_t sregion;
46 bool srvalid;
47 uint8_t iregion;
48 bool irvalid;
49 } V8M_SAttributes;
50
51 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
52 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53 V8M_SAttributes *sattrs);
54
55 /* Definitions for the PMCCNTR and PMCR registers */
56 #define PMCRD 0x8
57 #define PMCRC 0x4
58 #define PMCRE 0x1
59 #endif
60
61 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
62 {
63 int nregs;
64
65 /* VFP data registers are always little-endian. */
66 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
67 if (reg < nregs) {
68 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
69 return 8;
70 }
71 if (arm_feature(env, ARM_FEATURE_NEON)) {
72 /* Aliases for Q regs. */
73 nregs += 16;
74 if (reg < nregs) {
75 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
76 stq_le_p(buf, q[0]);
77 stq_le_p(buf + 8, q[1]);
78 return 16;
79 }
80 }
81 switch (reg - nregs) {
82 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
83 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
84 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
85 }
86 return 0;
87 }
88
89 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
90 {
91 int nregs;
92
93 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
94 if (reg < nregs) {
95 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
96 return 8;
97 }
98 if (arm_feature(env, ARM_FEATURE_NEON)) {
99 nregs += 16;
100 if (reg < nregs) {
101 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
102 q[0] = ldq_le_p(buf);
103 q[1] = ldq_le_p(buf + 8);
104 return 16;
105 }
106 }
107 switch (reg - nregs) {
108 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
109 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
110 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
111 }
112 return 0;
113 }
114
115 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
116 {
117 switch (reg) {
118 case 0 ... 31:
119 /* 128 bit FP register */
120 {
121 uint64_t *q = aa64_vfp_qreg(env, reg);
122 stq_le_p(buf, q[0]);
123 stq_le_p(buf + 8, q[1]);
124 return 16;
125 }
126 case 32:
127 /* FPSR */
128 stl_p(buf, vfp_get_fpsr(env));
129 return 4;
130 case 33:
131 /* FPCR */
132 stl_p(buf, vfp_get_fpcr(env));
133 return 4;
134 default:
135 return 0;
136 }
137 }
138
139 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
140 {
141 switch (reg) {
142 case 0 ... 31:
143 /* 128 bit FP register */
144 {
145 uint64_t *q = aa64_vfp_qreg(env, reg);
146 q[0] = ldq_le_p(buf);
147 q[1] = ldq_le_p(buf + 8);
148 return 16;
149 }
150 case 32:
151 /* FPSR */
152 vfp_set_fpsr(env, ldl_p(buf));
153 return 4;
154 case 33:
155 /* FPCR */
156 vfp_set_fpcr(env, ldl_p(buf));
157 return 4;
158 default:
159 return 0;
160 }
161 }
162
163 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
164 {
165 assert(ri->fieldoffset);
166 if (cpreg_field_is_64bit(ri)) {
167 return CPREG_FIELD64(env, ri);
168 } else {
169 return CPREG_FIELD32(env, ri);
170 }
171 }
172
173 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
174 uint64_t value)
175 {
176 assert(ri->fieldoffset);
177 if (cpreg_field_is_64bit(ri)) {
178 CPREG_FIELD64(env, ri) = value;
179 } else {
180 CPREG_FIELD32(env, ri) = value;
181 }
182 }
183
184 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
185 {
186 return (char *)env + ri->fieldoffset;
187 }
188
189 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
190 {
191 /* Raw read of a coprocessor register (as needed for migration, etc). */
192 if (ri->type & ARM_CP_CONST) {
193 return ri->resetvalue;
194 } else if (ri->raw_readfn) {
195 return ri->raw_readfn(env, ri);
196 } else if (ri->readfn) {
197 return ri->readfn(env, ri);
198 } else {
199 return raw_read(env, ri);
200 }
201 }
202
203 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
204 uint64_t v)
205 {
206 /* Raw write of a coprocessor register (as needed for migration, etc).
207 * Note that constant registers are treated as write-ignored; the
208 * caller should check for success by whether a readback gives the
209 * value written.
210 */
211 if (ri->type & ARM_CP_CONST) {
212 return;
213 } else if (ri->raw_writefn) {
214 ri->raw_writefn(env, ri, v);
215 } else if (ri->writefn) {
216 ri->writefn(env, ri, v);
217 } else {
218 raw_write(env, ri, v);
219 }
220 }
221
222 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
223 {
224 /* Return true if the regdef would cause an assertion if you called
225 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
226 * program bug for it not to have the NO_RAW flag).
227 * NB that returning false here doesn't necessarily mean that calling
228 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
229 * read/write access functions which are safe for raw use" from "has
230 * read/write access functions which have side effects but has forgotten
231 * to provide raw access functions".
232 * The tests here line up with the conditions in read/write_raw_cp_reg()
233 * and assertions in raw_read()/raw_write().
234 */
235 if ((ri->type & ARM_CP_CONST) ||
236 ri->fieldoffset ||
237 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
238 return false;
239 }
240 return true;
241 }
242
243 bool write_cpustate_to_list(ARMCPU *cpu)
244 {
245 /* Write the coprocessor state from cpu->env to the (index,value) list. */
246 int i;
247 bool ok = true;
248
249 for (i = 0; i < cpu->cpreg_array_len; i++) {
250 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
251 const ARMCPRegInfo *ri;
252
253 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
254 if (!ri) {
255 ok = false;
256 continue;
257 }
258 if (ri->type & ARM_CP_NO_RAW) {
259 continue;
260 }
261 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
262 }
263 return ok;
264 }
265
266 bool write_list_to_cpustate(ARMCPU *cpu)
267 {
268 int i;
269 bool ok = true;
270
271 for (i = 0; i < cpu->cpreg_array_len; i++) {
272 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
273 uint64_t v = cpu->cpreg_values[i];
274 const ARMCPRegInfo *ri;
275
276 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
277 if (!ri) {
278 ok = false;
279 continue;
280 }
281 if (ri->type & ARM_CP_NO_RAW) {
282 continue;
283 }
284 /* Write value and confirm it reads back as written
285 * (to catch read-only registers and partially read-only
286 * registers where the incoming migration value doesn't match)
287 */
288 write_raw_cp_reg(&cpu->env, ri, v);
289 if (read_raw_cp_reg(&cpu->env, ri) != v) {
290 ok = false;
291 }
292 }
293 return ok;
294 }
295
296 static void add_cpreg_to_list(gpointer key, gpointer opaque)
297 {
298 ARMCPU *cpu = opaque;
299 uint64_t regidx;
300 const ARMCPRegInfo *ri;
301
302 regidx = *(uint32_t *)key;
303 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
304
305 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
306 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
307 /* The value array need not be initialized at this point */
308 cpu->cpreg_array_len++;
309 }
310 }
311
312 static void count_cpreg(gpointer key, gpointer opaque)
313 {
314 ARMCPU *cpu = opaque;
315 uint64_t regidx;
316 const ARMCPRegInfo *ri;
317
318 regidx = *(uint32_t *)key;
319 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
320
321 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
322 cpu->cpreg_array_len++;
323 }
324 }
325
326 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
327 {
328 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
329 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
330
331 if (aidx > bidx) {
332 return 1;
333 }
334 if (aidx < bidx) {
335 return -1;
336 }
337 return 0;
338 }
339
340 void init_cpreg_list(ARMCPU *cpu)
341 {
342 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
343 * Note that we require cpreg_tuples[] to be sorted by key ID.
344 */
345 GList *keys;
346 int arraylen;
347
348 keys = g_hash_table_get_keys(cpu->cp_regs);
349 keys = g_list_sort(keys, cpreg_key_compare);
350
351 cpu->cpreg_array_len = 0;
352
353 g_list_foreach(keys, count_cpreg, cpu);
354
355 arraylen = cpu->cpreg_array_len;
356 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
357 cpu->cpreg_values = g_new(uint64_t, arraylen);
358 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
359 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
360 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
361 cpu->cpreg_array_len = 0;
362
363 g_list_foreach(keys, add_cpreg_to_list, cpu);
364
365 assert(cpu->cpreg_array_len == arraylen);
366
367 g_list_free(keys);
368 }
369
370 /*
371 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
372 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
373 *
374 * access_el3_aa32ns: Used to check AArch32 register views.
375 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
376 */
377 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
378 const ARMCPRegInfo *ri,
379 bool isread)
380 {
381 bool secure = arm_is_secure_below_el3(env);
382
383 assert(!arm_el_is_aa64(env, 3));
384 if (secure) {
385 return CP_ACCESS_TRAP_UNCATEGORIZED;
386 }
387 return CP_ACCESS_OK;
388 }
389
390 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
391 const ARMCPRegInfo *ri,
392 bool isread)
393 {
394 if (!arm_el_is_aa64(env, 3)) {
395 return access_el3_aa32ns(env, ri, isread);
396 }
397 return CP_ACCESS_OK;
398 }
399
400 /* Some secure-only AArch32 registers trap to EL3 if used from
401 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
402 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
403 * We assume that the .access field is set to PL1_RW.
404 */
405 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
406 const ARMCPRegInfo *ri,
407 bool isread)
408 {
409 if (arm_current_el(env) == 3) {
410 return CP_ACCESS_OK;
411 }
412 if (arm_is_secure_below_el3(env)) {
413 return CP_ACCESS_TRAP_EL3;
414 }
415 /* This will be EL1 NS and EL2 NS, which just UNDEF */
416 return CP_ACCESS_TRAP_UNCATEGORIZED;
417 }
418
419 /* Check for traps to "powerdown debug" registers, which are controlled
420 * by MDCR.TDOSA
421 */
422 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
423 bool isread)
424 {
425 int el = arm_current_el(env);
426
427 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
428 && !arm_is_secure_below_el3(env)) {
429 return CP_ACCESS_TRAP_EL2;
430 }
431 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
432 return CP_ACCESS_TRAP_EL3;
433 }
434 return CP_ACCESS_OK;
435 }
436
437 /* Check for traps to "debug ROM" registers, which are controlled
438 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
439 */
440 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
441 bool isread)
442 {
443 int el = arm_current_el(env);
444
445 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
446 && !arm_is_secure_below_el3(env)) {
447 return CP_ACCESS_TRAP_EL2;
448 }
449 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
450 return CP_ACCESS_TRAP_EL3;
451 }
452 return CP_ACCESS_OK;
453 }
454
455 /* Check for traps to general debug registers, which are controlled
456 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
457 */
458 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
459 bool isread)
460 {
461 int el = arm_current_el(env);
462
463 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
464 && !arm_is_secure_below_el3(env)) {
465 return CP_ACCESS_TRAP_EL2;
466 }
467 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
468 return CP_ACCESS_TRAP_EL3;
469 }
470 return CP_ACCESS_OK;
471 }
472
473 /* Check for traps to performance monitor registers, which are controlled
474 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
475 */
476 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
477 bool isread)
478 {
479 int el = arm_current_el(env);
480
481 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
482 && !arm_is_secure_below_el3(env)) {
483 return CP_ACCESS_TRAP_EL2;
484 }
485 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
486 return CP_ACCESS_TRAP_EL3;
487 }
488 return CP_ACCESS_OK;
489 }
490
491 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
492 {
493 ARMCPU *cpu = arm_env_get_cpu(env);
494
495 raw_write(env, ri, value);
496 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
497 }
498
499 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
500 {
501 ARMCPU *cpu = arm_env_get_cpu(env);
502
503 if (raw_read(env, ri) != value) {
504 /* Unlike real hardware the qemu TLB uses virtual addresses,
505 * not modified virtual addresses, so this causes a TLB flush.
506 */
507 tlb_flush(CPU(cpu));
508 raw_write(env, ri, value);
509 }
510 }
511
512 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 uint64_t value)
514 {
515 ARMCPU *cpu = arm_env_get_cpu(env);
516
517 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
518 && !extended_addresses_enabled(env)) {
519 /* For VMSA (when not using the LPAE long descriptor page table
520 * format) this register includes the ASID, so do a TLB flush.
521 * For PMSA it is purely a process ID and no action is needed.
522 */
523 tlb_flush(CPU(cpu));
524 }
525 raw_write(env, ri, value);
526 }
527
528 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
529 uint64_t value)
530 {
531 /* Invalidate all (TLBIALL) */
532 ARMCPU *cpu = arm_env_get_cpu(env);
533
534 tlb_flush(CPU(cpu));
535 }
536
537 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
538 uint64_t value)
539 {
540 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
541 ARMCPU *cpu = arm_env_get_cpu(env);
542
543 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
544 }
545
546 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
547 uint64_t value)
548 {
549 /* Invalidate by ASID (TLBIASID) */
550 ARMCPU *cpu = arm_env_get_cpu(env);
551
552 tlb_flush(CPU(cpu));
553 }
554
555 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
556 uint64_t value)
557 {
558 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
559 ARMCPU *cpu = arm_env_get_cpu(env);
560
561 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
562 }
563
564 /* IS variants of TLB operations must affect all cores */
565 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
567 {
568 CPUState *cs = ENV_GET_CPU(env);
569
570 tlb_flush_all_cpus_synced(cs);
571 }
572
573 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
574 uint64_t value)
575 {
576 CPUState *cs = ENV_GET_CPU(env);
577
578 tlb_flush_all_cpus_synced(cs);
579 }
580
581 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
582 uint64_t value)
583 {
584 CPUState *cs = ENV_GET_CPU(env);
585
586 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
587 }
588
589 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
590 uint64_t value)
591 {
592 CPUState *cs = ENV_GET_CPU(env);
593
594 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
595 }
596
597 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
598 uint64_t value)
599 {
600 CPUState *cs = ENV_GET_CPU(env);
601
602 tlb_flush_by_mmuidx(cs,
603 ARMMMUIdxBit_S12NSE1 |
604 ARMMMUIdxBit_S12NSE0 |
605 ARMMMUIdxBit_S2NS);
606 }
607
608 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
609 uint64_t value)
610 {
611 CPUState *cs = ENV_GET_CPU(env);
612
613 tlb_flush_by_mmuidx_all_cpus_synced(cs,
614 ARMMMUIdxBit_S12NSE1 |
615 ARMMMUIdxBit_S12NSE0 |
616 ARMMMUIdxBit_S2NS);
617 }
618
619 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
620 uint64_t value)
621 {
622 /* Invalidate by IPA. This has to invalidate any structures that
623 * contain only stage 2 translation information, but does not need
624 * to apply to structures that contain combined stage 1 and stage 2
625 * translation information.
626 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
627 */
628 CPUState *cs = ENV_GET_CPU(env);
629 uint64_t pageaddr;
630
631 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
632 return;
633 }
634
635 pageaddr = sextract64(value << 12, 0, 40);
636
637 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
638 }
639
640 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
641 uint64_t value)
642 {
643 CPUState *cs = ENV_GET_CPU(env);
644 uint64_t pageaddr;
645
646 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
647 return;
648 }
649
650 pageaddr = sextract64(value << 12, 0, 40);
651
652 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
653 ARMMMUIdxBit_S2NS);
654 }
655
656 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
657 uint64_t value)
658 {
659 CPUState *cs = ENV_GET_CPU(env);
660
661 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
662 }
663
664 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
665 uint64_t value)
666 {
667 CPUState *cs = ENV_GET_CPU(env);
668
669 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
670 }
671
672 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
673 uint64_t value)
674 {
675 CPUState *cs = ENV_GET_CPU(env);
676 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
677
678 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
679 }
680
681 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
682 uint64_t value)
683 {
684 CPUState *cs = ENV_GET_CPU(env);
685 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
686
687 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
688 ARMMMUIdxBit_S1E2);
689 }
690
691 static const ARMCPRegInfo cp_reginfo[] = {
692 /* Define the secure and non-secure FCSE identifier CP registers
693 * separately because there is no secure bank in V8 (no _EL3). This allows
694 * the secure register to be properly reset and migrated. There is also no
695 * v8 EL1 version of the register so the non-secure instance stands alone.
696 */
697 { .name = "FCSEIDR(NS)",
698 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
699 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
700 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
701 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
702 { .name = "FCSEIDR(S)",
703 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
704 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
705 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
706 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
707 /* Define the secure and non-secure context identifier CP registers
708 * separately because there is no secure bank in V8 (no _EL3). This allows
709 * the secure register to be properly reset and migrated. In the
710 * non-secure case, the 32-bit register will have reset and migration
711 * disabled during registration as it is handled by the 64-bit instance.
712 */
713 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
714 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
715 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
716 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
717 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
718 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
719 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
720 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
721 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
722 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
723 REGINFO_SENTINEL
724 };
725
726 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
727 /* NB: Some of these registers exist in v8 but with more precise
728 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
729 */
730 /* MMU Domain access control / MPU write buffer control */
731 { .name = "DACR",
732 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
733 .access = PL1_RW, .resetvalue = 0,
734 .writefn = dacr_write, .raw_writefn = raw_write,
735 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
736 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
737 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
738 * For v6 and v5, these mappings are overly broad.
739 */
740 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
741 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
742 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
743 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
744 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
745 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
746 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
747 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
748 /* Cache maintenance ops; some of this space may be overridden later. */
749 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
750 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
751 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
752 REGINFO_SENTINEL
753 };
754
755 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
756 /* Not all pre-v6 cores implemented this WFI, so this is slightly
757 * over-broad.
758 */
759 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
760 .access = PL1_W, .type = ARM_CP_WFI },
761 REGINFO_SENTINEL
762 };
763
764 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
765 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
766 * is UNPREDICTABLE; we choose to NOP as most implementations do).
767 */
768 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
769 .access = PL1_W, .type = ARM_CP_WFI },
770 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
771 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
772 * OMAPCP will override this space.
773 */
774 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
775 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
776 .resetvalue = 0 },
777 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
778 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
779 .resetvalue = 0 },
780 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
781 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
782 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
783 .resetvalue = 0 },
784 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
785 * implementing it as RAZ means the "debug architecture version" bits
786 * will read as a reserved value, which should cause Linux to not try
787 * to use the debug hardware.
788 */
789 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
790 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
791 /* MMU TLB control. Note that the wildcarding means we cover not just
792 * the unified TLB ops but also the dside/iside/inner-shareable variants.
793 */
794 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
795 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
796 .type = ARM_CP_NO_RAW },
797 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
798 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
799 .type = ARM_CP_NO_RAW },
800 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
801 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
802 .type = ARM_CP_NO_RAW },
803 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
804 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
805 .type = ARM_CP_NO_RAW },
806 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
807 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
808 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
809 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
810 REGINFO_SENTINEL
811 };
812
813 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
814 uint64_t value)
815 {
816 uint32_t mask = 0;
817
818 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
819 if (!arm_feature(env, ARM_FEATURE_V8)) {
820 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
821 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
822 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
823 */
824 if (arm_feature(env, ARM_FEATURE_VFP)) {
825 /* VFP coprocessor: cp10 & cp11 [23:20] */
826 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
827
828 if (!arm_feature(env, ARM_FEATURE_NEON)) {
829 /* ASEDIS [31] bit is RAO/WI */
830 value |= (1 << 31);
831 }
832
833 /* VFPv3 and upwards with NEON implement 32 double precision
834 * registers (D0-D31).
835 */
836 if (!arm_feature(env, ARM_FEATURE_NEON) ||
837 !arm_feature(env, ARM_FEATURE_VFP3)) {
838 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
839 value |= (1 << 30);
840 }
841 }
842 value &= mask;
843 }
844 env->cp15.cpacr_el1 = value;
845 }
846
847 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
848 bool isread)
849 {
850 if (arm_feature(env, ARM_FEATURE_V8)) {
851 /* Check if CPACR accesses are to be trapped to EL2 */
852 if (arm_current_el(env) == 1 &&
853 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
854 return CP_ACCESS_TRAP_EL2;
855 /* Check if CPACR accesses are to be trapped to EL3 */
856 } else if (arm_current_el(env) < 3 &&
857 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
858 return CP_ACCESS_TRAP_EL3;
859 }
860 }
861
862 return CP_ACCESS_OK;
863 }
864
865 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
866 bool isread)
867 {
868 /* Check if CPTR accesses are set to trap to EL3 */
869 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
870 return CP_ACCESS_TRAP_EL3;
871 }
872
873 return CP_ACCESS_OK;
874 }
875
876 static const ARMCPRegInfo v6_cp_reginfo[] = {
877 /* prefetch by MVA in v6, NOP in v7 */
878 { .name = "MVA_prefetch",
879 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
880 .access = PL1_W, .type = ARM_CP_NOP },
881 /* We need to break the TB after ISB to execute self-modifying code
882 * correctly and also to take any pending interrupts immediately.
883 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
884 */
885 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
886 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
887 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
888 .access = PL0_W, .type = ARM_CP_NOP },
889 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
890 .access = PL0_W, .type = ARM_CP_NOP },
891 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
892 .access = PL1_RW,
893 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
894 offsetof(CPUARMState, cp15.ifar_ns) },
895 .resetvalue = 0, },
896 /* Watchpoint Fault Address Register : should actually only be present
897 * for 1136, 1176, 11MPCore.
898 */
899 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
900 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
901 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
902 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
903 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
904 .resetvalue = 0, .writefn = cpacr_write },
905 REGINFO_SENTINEL
906 };
907
908 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
909 bool isread)
910 {
911 /* Performance monitor registers user accessibility is controlled
912 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
913 * trapping to EL2 or EL3 for other accesses.
914 */
915 int el = arm_current_el(env);
916
917 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
918 return CP_ACCESS_TRAP;
919 }
920 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
921 && !arm_is_secure_below_el3(env)) {
922 return CP_ACCESS_TRAP_EL2;
923 }
924 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
925 return CP_ACCESS_TRAP_EL3;
926 }
927
928 return CP_ACCESS_OK;
929 }
930
931 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
932 const ARMCPRegInfo *ri,
933 bool isread)
934 {
935 /* ER: event counter read trap control */
936 if (arm_feature(env, ARM_FEATURE_V8)
937 && arm_current_el(env) == 0
938 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
939 && isread) {
940 return CP_ACCESS_OK;
941 }
942
943 return pmreg_access(env, ri, isread);
944 }
945
946 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
947 const ARMCPRegInfo *ri,
948 bool isread)
949 {
950 /* SW: software increment write trap control */
951 if (arm_feature(env, ARM_FEATURE_V8)
952 && arm_current_el(env) == 0
953 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
954 && !isread) {
955 return CP_ACCESS_OK;
956 }
957
958 return pmreg_access(env, ri, isread);
959 }
960
961 #ifndef CONFIG_USER_ONLY
962
963 static CPAccessResult pmreg_access_selr(CPUARMState *env,
964 const ARMCPRegInfo *ri,
965 bool isread)
966 {
967 /* ER: event counter read trap control */
968 if (arm_feature(env, ARM_FEATURE_V8)
969 && arm_current_el(env) == 0
970 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
971 return CP_ACCESS_OK;
972 }
973
974 return pmreg_access(env, ri, isread);
975 }
976
977 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
978 const ARMCPRegInfo *ri,
979 bool isread)
980 {
981 /* CR: cycle counter read trap control */
982 if (arm_feature(env, ARM_FEATURE_V8)
983 && arm_current_el(env) == 0
984 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
985 && isread) {
986 return CP_ACCESS_OK;
987 }
988
989 return pmreg_access(env, ri, isread);
990 }
991
992 static inline bool arm_ccnt_enabled(CPUARMState *env)
993 {
994 /* This does not support checking PMCCFILTR_EL0 register */
995
996 if (!(env->cp15.c9_pmcr & PMCRE)) {
997 return false;
998 }
999
1000 return true;
1001 }
1002
1003 void pmccntr_sync(CPUARMState *env)
1004 {
1005 uint64_t temp_ticks;
1006
1007 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1008 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1009
1010 if (env->cp15.c9_pmcr & PMCRD) {
1011 /* Increment once every 64 processor clock cycles */
1012 temp_ticks /= 64;
1013 }
1014
1015 if (arm_ccnt_enabled(env)) {
1016 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1017 }
1018 }
1019
1020 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1021 uint64_t value)
1022 {
1023 pmccntr_sync(env);
1024
1025 if (value & PMCRC) {
1026 /* The counter has been reset */
1027 env->cp15.c15_ccnt = 0;
1028 }
1029
1030 /* only the DP, X, D and E bits are writable */
1031 env->cp15.c9_pmcr &= ~0x39;
1032 env->cp15.c9_pmcr |= (value & 0x39);
1033
1034 pmccntr_sync(env);
1035 }
1036
1037 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1038 {
1039 uint64_t total_ticks;
1040
1041 if (!arm_ccnt_enabled(env)) {
1042 /* Counter is disabled, do not change value */
1043 return env->cp15.c15_ccnt;
1044 }
1045
1046 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1047 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1048
1049 if (env->cp15.c9_pmcr & PMCRD) {
1050 /* Increment once every 64 processor clock cycles */
1051 total_ticks /= 64;
1052 }
1053 return total_ticks - env->cp15.c15_ccnt;
1054 }
1055
1056 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1057 uint64_t value)
1058 {
1059 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1060 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1061 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1062 * accessed.
1063 */
1064 env->cp15.c9_pmselr = value & 0x1f;
1065 }
1066
1067 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1068 uint64_t value)
1069 {
1070 uint64_t total_ticks;
1071
1072 if (!arm_ccnt_enabled(env)) {
1073 /* Counter is disabled, set the absolute value */
1074 env->cp15.c15_ccnt = value;
1075 return;
1076 }
1077
1078 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1079 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1080
1081 if (env->cp15.c9_pmcr & PMCRD) {
1082 /* Increment once every 64 processor clock cycles */
1083 total_ticks /= 64;
1084 }
1085 env->cp15.c15_ccnt = total_ticks - value;
1086 }
1087
1088 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1089 uint64_t value)
1090 {
1091 uint64_t cur_val = pmccntr_read(env, NULL);
1092
1093 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1094 }
1095
1096 #else /* CONFIG_USER_ONLY */
1097
1098 void pmccntr_sync(CPUARMState *env)
1099 {
1100 }
1101
1102 #endif
1103
1104 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1105 uint64_t value)
1106 {
1107 pmccntr_sync(env);
1108 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
1109 pmccntr_sync(env);
1110 }
1111
1112 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1113 uint64_t value)
1114 {
1115 value &= (1 << 31);
1116 env->cp15.c9_pmcnten |= value;
1117 }
1118
1119 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1120 uint64_t value)
1121 {
1122 value &= (1 << 31);
1123 env->cp15.c9_pmcnten &= ~value;
1124 }
1125
1126 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1127 uint64_t value)
1128 {
1129 env->cp15.c9_pmovsr &= ~value;
1130 }
1131
1132 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1133 uint64_t value)
1134 {
1135 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1136 * PMSELR value is equal to or greater than the number of implemented
1137 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1138 */
1139 if (env->cp15.c9_pmselr == 0x1f) {
1140 pmccfiltr_write(env, ri, value);
1141 }
1142 }
1143
1144 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1145 {
1146 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1147 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1148 */
1149 if (env->cp15.c9_pmselr == 0x1f) {
1150 return env->cp15.pmccfiltr_el0;
1151 } else {
1152 return 0;
1153 }
1154 }
1155
1156 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1157 uint64_t value)
1158 {
1159 if (arm_feature(env, ARM_FEATURE_V8)) {
1160 env->cp15.c9_pmuserenr = value & 0xf;
1161 } else {
1162 env->cp15.c9_pmuserenr = value & 1;
1163 }
1164 }
1165
1166 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1167 uint64_t value)
1168 {
1169 /* We have no event counters so only the C bit can be changed */
1170 value &= (1 << 31);
1171 env->cp15.c9_pminten |= value;
1172 }
1173
1174 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1175 uint64_t value)
1176 {
1177 value &= (1 << 31);
1178 env->cp15.c9_pminten &= ~value;
1179 }
1180
1181 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1182 uint64_t value)
1183 {
1184 /* Note that even though the AArch64 view of this register has bits
1185 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1186 * architectural requirements for bits which are RES0 only in some
1187 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1188 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1189 */
1190 raw_write(env, ri, value & ~0x1FULL);
1191 }
1192
1193 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1194 {
1195 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1196 * For bits that vary between AArch32/64, code needs to check the
1197 * current execution mode before directly using the feature bit.
1198 */
1199 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1200
1201 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1202 valid_mask &= ~SCR_HCE;
1203
1204 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1205 * supported if EL2 exists. The bit is UNK/SBZP when
1206 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1207 * when EL2 is unavailable.
1208 * On ARMv8, this bit is always available.
1209 */
1210 if (arm_feature(env, ARM_FEATURE_V7) &&
1211 !arm_feature(env, ARM_FEATURE_V8)) {
1212 valid_mask &= ~SCR_SMD;
1213 }
1214 }
1215
1216 /* Clear all-context RES0 bits. */
1217 value &= valid_mask;
1218 raw_write(env, ri, value);
1219 }
1220
1221 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1222 {
1223 ARMCPU *cpu = arm_env_get_cpu(env);
1224
1225 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1226 * bank
1227 */
1228 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1229 ri->secure & ARM_CP_SECSTATE_S);
1230
1231 return cpu->ccsidr[index];
1232 }
1233
1234 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1235 uint64_t value)
1236 {
1237 raw_write(env, ri, value & 0xf);
1238 }
1239
1240 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1241 {
1242 CPUState *cs = ENV_GET_CPU(env);
1243 uint64_t ret = 0;
1244
1245 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1246 ret |= CPSR_I;
1247 }
1248 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1249 ret |= CPSR_F;
1250 }
1251 /* External aborts are not possible in QEMU so A bit is always clear */
1252 return ret;
1253 }
1254
1255 static const ARMCPRegInfo v7_cp_reginfo[] = {
1256 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1257 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1258 .access = PL1_W, .type = ARM_CP_NOP },
1259 /* Performance monitors are implementation defined in v7,
1260 * but with an ARM recommended set of registers, which we
1261 * follow (although we don't actually implement any counters)
1262 *
1263 * Performance registers fall into three categories:
1264 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1265 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1266 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1267 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1268 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1269 */
1270 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1271 .access = PL0_RW, .type = ARM_CP_ALIAS,
1272 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1273 .writefn = pmcntenset_write,
1274 .accessfn = pmreg_access,
1275 .raw_writefn = raw_write },
1276 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1277 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1278 .access = PL0_RW, .accessfn = pmreg_access,
1279 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1280 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1281 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1282 .access = PL0_RW,
1283 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1284 .accessfn = pmreg_access,
1285 .writefn = pmcntenclr_write,
1286 .type = ARM_CP_ALIAS },
1287 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1288 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1289 .access = PL0_RW, .accessfn = pmreg_access,
1290 .type = ARM_CP_ALIAS,
1291 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1292 .writefn = pmcntenclr_write },
1293 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1294 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1295 .accessfn = pmreg_access,
1296 .writefn = pmovsr_write,
1297 .raw_writefn = raw_write },
1298 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1299 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1300 .access = PL0_RW, .accessfn = pmreg_access,
1301 .type = ARM_CP_ALIAS,
1302 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1303 .writefn = pmovsr_write,
1304 .raw_writefn = raw_write },
1305 /* Unimplemented so WI. */
1306 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1307 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
1308 #ifndef CONFIG_USER_ONLY
1309 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1310 .access = PL0_RW, .type = ARM_CP_ALIAS,
1311 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1312 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1313 .raw_writefn = raw_write},
1314 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1315 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1316 .access = PL0_RW, .accessfn = pmreg_access_selr,
1317 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1318 .writefn = pmselr_write, .raw_writefn = raw_write, },
1319 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1320 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
1321 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1322 .accessfn = pmreg_access_ccntr },
1323 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1324 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1325 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1326 .type = ARM_CP_IO,
1327 .readfn = pmccntr_read, .writefn = pmccntr_write, },
1328 #endif
1329 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1330 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1331 .writefn = pmccfiltr_write,
1332 .access = PL0_RW, .accessfn = pmreg_access,
1333 .type = ARM_CP_IO,
1334 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1335 .resetvalue = 0, },
1336 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1337 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1338 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1339 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1340 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1341 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1342 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1343 /* Unimplemented, RAZ/WI. */
1344 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1345 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1346 .accessfn = pmreg_access_xevcntr },
1347 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1348 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
1349 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1350 .resetvalue = 0,
1351 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1352 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1353 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1354 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1355 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1356 .resetvalue = 0,
1357 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1358 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1359 .access = PL1_RW, .accessfn = access_tpm,
1360 .type = ARM_CP_ALIAS,
1361 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
1362 .resetvalue = 0,
1363 .writefn = pmintenset_write, .raw_writefn = raw_write },
1364 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1365 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1366 .access = PL1_RW, .accessfn = access_tpm,
1367 .type = ARM_CP_IO,
1368 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1369 .writefn = pmintenset_write, .raw_writefn = raw_write,
1370 .resetvalue = 0x0 },
1371 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1372 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1373 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1374 .writefn = pmintenclr_write, },
1375 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1376 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1377 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1378 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1379 .writefn = pmintenclr_write },
1380 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1381 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
1382 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
1383 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1384 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
1385 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1386 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1387 offsetof(CPUARMState, cp15.csselr_ns) } },
1388 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1389 * just RAZ for all cores:
1390 */
1391 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1392 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
1393 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1394 /* Auxiliary fault status registers: these also are IMPDEF, and we
1395 * choose to RAZ/WI for all cores.
1396 */
1397 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1398 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1399 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1400 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1401 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1402 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1403 /* MAIR can just read-as-written because we don't implement caches
1404 * and so don't need to care about memory attributes.
1405 */
1406 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1407 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
1408 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
1409 .resetvalue = 0 },
1410 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1411 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1412 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1413 .resetvalue = 0 },
1414 /* For non-long-descriptor page tables these are PRRR and NMRR;
1415 * regardless they still act as reads-as-written for QEMU.
1416 */
1417 /* MAIR0/1 are defined separately from their 64-bit counterpart which
1418 * allows them to assign the correct fieldoffset based on the endianness
1419 * handled in the field definitions.
1420 */
1421 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
1422 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
1423 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1424 offsetof(CPUARMState, cp15.mair0_ns) },
1425 .resetfn = arm_cp_reset_ignore },
1426 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
1427 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
1428 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1429 offsetof(CPUARMState, cp15.mair1_ns) },
1430 .resetfn = arm_cp_reset_ignore },
1431 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1432 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
1433 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
1434 /* 32 bit ITLB invalidates */
1435 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
1436 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1437 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
1438 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1439 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
1440 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1441 /* 32 bit DTLB invalidates */
1442 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
1443 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1444 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
1445 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1446 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
1447 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1448 /* 32 bit TLB invalidates */
1449 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
1450 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1451 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
1452 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1453 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
1454 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1455 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
1456 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
1457 REGINFO_SENTINEL
1458 };
1459
1460 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1461 /* 32 bit TLB invalidates, Inner Shareable */
1462 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1463 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
1464 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1465 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
1466 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1467 .type = ARM_CP_NO_RAW, .access = PL1_W,
1468 .writefn = tlbiasid_is_write },
1469 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1470 .type = ARM_CP_NO_RAW, .access = PL1_W,
1471 .writefn = tlbimvaa_is_write },
1472 REGINFO_SENTINEL
1473 };
1474
1475 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1476 uint64_t value)
1477 {
1478 value &= 1;
1479 env->teecr = value;
1480 }
1481
1482 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1483 bool isread)
1484 {
1485 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
1486 return CP_ACCESS_TRAP;
1487 }
1488 return CP_ACCESS_OK;
1489 }
1490
1491 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1492 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1493 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1494 .resetvalue = 0,
1495 .writefn = teecr_write },
1496 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1497 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1498 .accessfn = teehbr_access, .resetvalue = 0 },
1499 REGINFO_SENTINEL
1500 };
1501
1502 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1503 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1504 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1505 .access = PL0_RW,
1506 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
1507 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1508 .access = PL0_RW,
1509 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1510 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
1511 .resetfn = arm_cp_reset_ignore },
1512 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1513 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1514 .access = PL0_R|PL1_W,
1515 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1516 .resetvalue = 0},
1517 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1518 .access = PL0_R|PL1_W,
1519 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1520 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
1521 .resetfn = arm_cp_reset_ignore },
1522 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
1523 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1524 .access = PL1_RW,
1525 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1526 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1527 .access = PL1_RW,
1528 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1529 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1530 .resetvalue = 0 },
1531 REGINFO_SENTINEL
1532 };
1533
1534 #ifndef CONFIG_USER_ONLY
1535
1536 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1537 bool isread)
1538 {
1539 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1540 * Writable only at the highest implemented exception level.
1541 */
1542 int el = arm_current_el(env);
1543
1544 switch (el) {
1545 case 0:
1546 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1547 return CP_ACCESS_TRAP;
1548 }
1549 break;
1550 case 1:
1551 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1552 arm_is_secure_below_el3(env)) {
1553 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1554 return CP_ACCESS_TRAP_UNCATEGORIZED;
1555 }
1556 break;
1557 case 2:
1558 case 3:
1559 break;
1560 }
1561
1562 if (!isread && el < arm_highest_el(env)) {
1563 return CP_ACCESS_TRAP_UNCATEGORIZED;
1564 }
1565
1566 return CP_ACCESS_OK;
1567 }
1568
1569 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1570 bool isread)
1571 {
1572 unsigned int cur_el = arm_current_el(env);
1573 bool secure = arm_is_secure(env);
1574
1575 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1576 if (cur_el == 0 &&
1577 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1578 return CP_ACCESS_TRAP;
1579 }
1580
1581 if (arm_feature(env, ARM_FEATURE_EL2) &&
1582 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1583 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1584 return CP_ACCESS_TRAP_EL2;
1585 }
1586 return CP_ACCESS_OK;
1587 }
1588
1589 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1590 bool isread)
1591 {
1592 unsigned int cur_el = arm_current_el(env);
1593 bool secure = arm_is_secure(env);
1594
1595 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1596 * EL0[PV]TEN is zero.
1597 */
1598 if (cur_el == 0 &&
1599 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1600 return CP_ACCESS_TRAP;
1601 }
1602
1603 if (arm_feature(env, ARM_FEATURE_EL2) &&
1604 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1605 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1606 return CP_ACCESS_TRAP_EL2;
1607 }
1608 return CP_ACCESS_OK;
1609 }
1610
1611 static CPAccessResult gt_pct_access(CPUARMState *env,
1612 const ARMCPRegInfo *ri,
1613 bool isread)
1614 {
1615 return gt_counter_access(env, GTIMER_PHYS, isread);
1616 }
1617
1618 static CPAccessResult gt_vct_access(CPUARMState *env,
1619 const ARMCPRegInfo *ri,
1620 bool isread)
1621 {
1622 return gt_counter_access(env, GTIMER_VIRT, isread);
1623 }
1624
1625 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1626 bool isread)
1627 {
1628 return gt_timer_access(env, GTIMER_PHYS, isread);
1629 }
1630
1631 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1632 bool isread)
1633 {
1634 return gt_timer_access(env, GTIMER_VIRT, isread);
1635 }
1636
1637 static CPAccessResult gt_stimer_access(CPUARMState *env,
1638 const ARMCPRegInfo *ri,
1639 bool isread)
1640 {
1641 /* The AArch64 register view of the secure physical timer is
1642 * always accessible from EL3, and configurably accessible from
1643 * Secure EL1.
1644 */
1645 switch (arm_current_el(env)) {
1646 case 1:
1647 if (!arm_is_secure(env)) {
1648 return CP_ACCESS_TRAP;
1649 }
1650 if (!(env->cp15.scr_el3 & SCR_ST)) {
1651 return CP_ACCESS_TRAP_EL3;
1652 }
1653 return CP_ACCESS_OK;
1654 case 0:
1655 case 2:
1656 return CP_ACCESS_TRAP;
1657 case 3:
1658 return CP_ACCESS_OK;
1659 default:
1660 g_assert_not_reached();
1661 }
1662 }
1663
1664 static uint64_t gt_get_countervalue(CPUARMState *env)
1665 {
1666 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1667 }
1668
1669 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1670 {
1671 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1672
1673 if (gt->ctl & 1) {
1674 /* Timer enabled: calculate and set current ISTATUS, irq, and
1675 * reset timer to when ISTATUS next has to change
1676 */
1677 uint64_t offset = timeridx == GTIMER_VIRT ?
1678 cpu->env.cp15.cntvoff_el2 : 0;
1679 uint64_t count = gt_get_countervalue(&cpu->env);
1680 /* Note that this must be unsigned 64 bit arithmetic: */
1681 int istatus = count - offset >= gt->cval;
1682 uint64_t nexttick;
1683 int irqstate;
1684
1685 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1686
1687 irqstate = (istatus && !(gt->ctl & 2));
1688 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1689
1690 if (istatus) {
1691 /* Next transition is when count rolls back over to zero */
1692 nexttick = UINT64_MAX;
1693 } else {
1694 /* Next transition is when we hit cval */
1695 nexttick = gt->cval + offset;
1696 }
1697 /* Note that the desired next expiry time might be beyond the
1698 * signed-64-bit range of a QEMUTimer -- in this case we just
1699 * set the timer for as far in the future as possible. When the
1700 * timer expires we will reset the timer for any remaining period.
1701 */
1702 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1703 nexttick = INT64_MAX / GTIMER_SCALE;
1704 }
1705 timer_mod(cpu->gt_timer[timeridx], nexttick);
1706 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
1707 } else {
1708 /* Timer disabled: ISTATUS and timer output always clear */
1709 gt->ctl &= ~4;
1710 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1711 timer_del(cpu->gt_timer[timeridx]);
1712 trace_arm_gt_recalc_disabled(timeridx);
1713 }
1714 }
1715
1716 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1717 int timeridx)
1718 {
1719 ARMCPU *cpu = arm_env_get_cpu(env);
1720
1721 timer_del(cpu->gt_timer[timeridx]);
1722 }
1723
1724 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1725 {
1726 return gt_get_countervalue(env);
1727 }
1728
1729 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1730 {
1731 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1732 }
1733
1734 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1735 int timeridx,
1736 uint64_t value)
1737 {
1738 trace_arm_gt_cval_write(timeridx, value);
1739 env->cp15.c14_timer[timeridx].cval = value;
1740 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1741 }
1742
1743 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1744 int timeridx)
1745 {
1746 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1747
1748 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1749 (gt_get_countervalue(env) - offset));
1750 }
1751
1752 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1753 int timeridx,
1754 uint64_t value)
1755 {
1756 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1757
1758 trace_arm_gt_tval_write(timeridx, value);
1759 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
1760 sextract64(value, 0, 32);
1761 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1762 }
1763
1764 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1765 int timeridx,
1766 uint64_t value)
1767 {
1768 ARMCPU *cpu = arm_env_get_cpu(env);
1769 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1770
1771 trace_arm_gt_ctl_write(timeridx, value);
1772 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1773 if ((oldval ^ value) & 1) {
1774 /* Enable toggled */
1775 gt_recalc_timer(cpu, timeridx);
1776 } else if ((oldval ^ value) & 2) {
1777 /* IMASK toggled: don't need to recalculate,
1778 * just set the interrupt line based on ISTATUS
1779 */
1780 int irqstate = (oldval & 4) && !(value & 2);
1781
1782 trace_arm_gt_imask_toggle(timeridx, irqstate);
1783 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1784 }
1785 }
1786
1787 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1788 {
1789 gt_timer_reset(env, ri, GTIMER_PHYS);
1790 }
1791
1792 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1793 uint64_t value)
1794 {
1795 gt_cval_write(env, ri, GTIMER_PHYS, value);
1796 }
1797
1798 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1799 {
1800 return gt_tval_read(env, ri, GTIMER_PHYS);
1801 }
1802
1803 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1804 uint64_t value)
1805 {
1806 gt_tval_write(env, ri, GTIMER_PHYS, value);
1807 }
1808
1809 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1810 uint64_t value)
1811 {
1812 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1813 }
1814
1815 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1816 {
1817 gt_timer_reset(env, ri, GTIMER_VIRT);
1818 }
1819
1820 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1821 uint64_t value)
1822 {
1823 gt_cval_write(env, ri, GTIMER_VIRT, value);
1824 }
1825
1826 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1827 {
1828 return gt_tval_read(env, ri, GTIMER_VIRT);
1829 }
1830
1831 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1832 uint64_t value)
1833 {
1834 gt_tval_write(env, ri, GTIMER_VIRT, value);
1835 }
1836
1837 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1838 uint64_t value)
1839 {
1840 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1841 }
1842
1843 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844 uint64_t value)
1845 {
1846 ARMCPU *cpu = arm_env_get_cpu(env);
1847
1848 trace_arm_gt_cntvoff_write(value);
1849 raw_write(env, ri, value);
1850 gt_recalc_timer(cpu, GTIMER_VIRT);
1851 }
1852
1853 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1854 {
1855 gt_timer_reset(env, ri, GTIMER_HYP);
1856 }
1857
1858 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1859 uint64_t value)
1860 {
1861 gt_cval_write(env, ri, GTIMER_HYP, value);
1862 }
1863
1864 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1865 {
1866 return gt_tval_read(env, ri, GTIMER_HYP);
1867 }
1868
1869 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1870 uint64_t value)
1871 {
1872 gt_tval_write(env, ri, GTIMER_HYP, value);
1873 }
1874
1875 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1876 uint64_t value)
1877 {
1878 gt_ctl_write(env, ri, GTIMER_HYP, value);
1879 }
1880
1881 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1882 {
1883 gt_timer_reset(env, ri, GTIMER_SEC);
1884 }
1885
1886 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1887 uint64_t value)
1888 {
1889 gt_cval_write(env, ri, GTIMER_SEC, value);
1890 }
1891
1892 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1893 {
1894 return gt_tval_read(env, ri, GTIMER_SEC);
1895 }
1896
1897 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1898 uint64_t value)
1899 {
1900 gt_tval_write(env, ri, GTIMER_SEC, value);
1901 }
1902
1903 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1904 uint64_t value)
1905 {
1906 gt_ctl_write(env, ri, GTIMER_SEC, value);
1907 }
1908
1909 void arm_gt_ptimer_cb(void *opaque)
1910 {
1911 ARMCPU *cpu = opaque;
1912
1913 gt_recalc_timer(cpu, GTIMER_PHYS);
1914 }
1915
1916 void arm_gt_vtimer_cb(void *opaque)
1917 {
1918 ARMCPU *cpu = opaque;
1919
1920 gt_recalc_timer(cpu, GTIMER_VIRT);
1921 }
1922
1923 void arm_gt_htimer_cb(void *opaque)
1924 {
1925 ARMCPU *cpu = opaque;
1926
1927 gt_recalc_timer(cpu, GTIMER_HYP);
1928 }
1929
1930 void arm_gt_stimer_cb(void *opaque)
1931 {
1932 ARMCPU *cpu = opaque;
1933
1934 gt_recalc_timer(cpu, GTIMER_SEC);
1935 }
1936
1937 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1938 /* Note that CNTFRQ is purely reads-as-written for the benefit
1939 * of software; writing it doesn't actually change the timer frequency.
1940 * Our reset value matches the fixed frequency we implement the timer at.
1941 */
1942 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1943 .type = ARM_CP_ALIAS,
1944 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1945 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1946 },
1947 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1948 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1949 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1950 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1951 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1952 },
1953 /* overall control: mostly access permissions */
1954 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1955 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1956 .access = PL1_RW,
1957 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1958 .resetvalue = 0,
1959 },
1960 /* per-timer control */
1961 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1962 .secure = ARM_CP_SECSTATE_NS,
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_PHYS].ctl),
1967 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1968 },
1969 { .name = "CNTP_CTL(S)",
1970 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1971 .secure = ARM_CP_SECSTATE_S,
1972 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1973 .accessfn = gt_ptimer_access,
1974 .fieldoffset = offsetoflow32(CPUARMState,
1975 cp15.c14_timer[GTIMER_SEC].ctl),
1976 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1977 },
1978 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1979 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1980 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1981 .accessfn = gt_ptimer_access,
1982 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1983 .resetvalue = 0,
1984 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1985 },
1986 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1987 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1988 .accessfn = gt_vtimer_access,
1989 .fieldoffset = offsetoflow32(CPUARMState,
1990 cp15.c14_timer[GTIMER_VIRT].ctl),
1991 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1992 },
1993 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1994 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1995 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1996 .accessfn = gt_vtimer_access,
1997 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1998 .resetvalue = 0,
1999 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2000 },
2001 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2002 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2003 .secure = ARM_CP_SECSTATE_NS,
2004 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2005 .accessfn = gt_ptimer_access,
2006 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2007 },
2008 { .name = "CNTP_TVAL(S)",
2009 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2010 .secure = ARM_CP_SECSTATE_S,
2011 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2012 .accessfn = gt_ptimer_access,
2013 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2014 },
2015 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2016 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2017 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2018 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2019 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2020 },
2021 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2022 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2023 .accessfn = gt_vtimer_access,
2024 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2025 },
2026 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2028 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2029 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2030 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2031 },
2032 /* The counter itself */
2033 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2034 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2035 .accessfn = gt_pct_access,
2036 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2037 },
2038 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2039 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2040 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2041 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2042 },
2043 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2044 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2045 .accessfn = gt_vct_access,
2046 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2047 },
2048 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2049 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2050 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2051 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2052 },
2053 /* Comparison value, indicating when the timer goes off */
2054 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2055 .secure = ARM_CP_SECSTATE_NS,
2056 .access = PL1_RW | PL0_R,
2057 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2058 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2059 .accessfn = gt_ptimer_access,
2060 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2061 },
2062 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
2063 .secure = ARM_CP_SECSTATE_S,
2064 .access = PL1_RW | PL0_R,
2065 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2066 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2067 .accessfn = gt_ptimer_access,
2068 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2069 },
2070 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2071 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2072 .access = PL1_RW | PL0_R,
2073 .type = ARM_CP_IO,
2074 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2075 .resetvalue = 0, .accessfn = gt_ptimer_access,
2076 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2077 },
2078 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2079 .access = PL1_RW | PL0_R,
2080 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2081 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2082 .accessfn = gt_vtimer_access,
2083 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2084 },
2085 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2086 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2087 .access = PL1_RW | PL0_R,
2088 .type = ARM_CP_IO,
2089 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2090 .resetvalue = 0, .accessfn = gt_vtimer_access,
2091 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2092 },
2093 /* Secure timer -- this is actually restricted to only EL3
2094 * and configurably Secure-EL1 via the accessfn.
2095 */
2096 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2097 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2098 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2099 .accessfn = gt_stimer_access,
2100 .readfn = gt_sec_tval_read,
2101 .writefn = gt_sec_tval_write,
2102 .resetfn = gt_sec_timer_reset,
2103 },
2104 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2105 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2106 .type = ARM_CP_IO, .access = PL1_RW,
2107 .accessfn = gt_stimer_access,
2108 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2109 .resetvalue = 0,
2110 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2111 },
2112 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2113 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2114 .type = ARM_CP_IO, .access = PL1_RW,
2115 .accessfn = gt_stimer_access,
2116 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2117 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2118 },
2119 REGINFO_SENTINEL
2120 };
2121
2122 #else
2123 /* In user-mode none of the generic timer registers are accessible,
2124 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
2125 * so instead just don't register any of them.
2126 */
2127 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2128 REGINFO_SENTINEL
2129 };
2130
2131 #endif
2132
2133 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2134 {
2135 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2136 raw_write(env, ri, value);
2137 } else if (arm_feature(env, ARM_FEATURE_V7)) {
2138 raw_write(env, ri, value & 0xfffff6ff);
2139 } else {
2140 raw_write(env, ri, value & 0xfffff1ff);
2141 }
2142 }
2143
2144 #ifndef CONFIG_USER_ONLY
2145 /* get_phys_addr() isn't present for user-mode-only targets */
2146
2147 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2148 bool isread)
2149 {
2150 if (ri->opc2 & 4) {
2151 /* The ATS12NSO* operations must trap to EL3 if executed in
2152 * Secure EL1 (which can only happen if EL3 is AArch64).
2153 * They are simply UNDEF if executed from NS EL1.
2154 * They function normally from EL2 or EL3.
2155 */
2156 if (arm_current_el(env) == 1) {
2157 if (arm_is_secure_below_el3(env)) {
2158 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2159 }
2160 return CP_ACCESS_TRAP_UNCATEGORIZED;
2161 }
2162 }
2163 return CP_ACCESS_OK;
2164 }
2165
2166 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
2167 MMUAccessType access_type, ARMMMUIdx mmu_idx)
2168 {
2169 hwaddr phys_addr;
2170 target_ulong page_size;
2171 int prot;
2172 bool ret;
2173 uint64_t par64;
2174 bool format64 = false;
2175 MemTxAttrs attrs = {};
2176 ARMMMUFaultInfo fi = {};
2177 ARMCacheAttrs cacheattrs = {};
2178
2179 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
2180 &prot, &page_size, &fi, &cacheattrs);
2181
2182 if (is_a64(env)) {
2183 format64 = true;
2184 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2185 /*
2186 * ATS1Cxx:
2187 * * TTBCR.EAE determines whether the result is returned using the
2188 * 32-bit or the 64-bit PAR format
2189 * * Instructions executed in Hyp mode always use the 64bit format
2190 *
2191 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2192 * * The Non-secure TTBCR.EAE bit is set to 1
2193 * * The implementation includes EL2, and the value of HCR.VM is 1
2194 *
2195 * ATS1Hx always uses the 64bit format (not supported yet).
2196 */
2197 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2198
2199 if (arm_feature(env, ARM_FEATURE_EL2)) {
2200 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2201 format64 |= env->cp15.hcr_el2 & HCR_VM;
2202 } else {
2203 format64 |= arm_current_el(env) == 2;
2204 }
2205 }
2206 }
2207
2208 if (format64) {
2209 /* Create a 64-bit PAR */
2210 par64 = (1 << 11); /* LPAE bit always set */
2211 if (!ret) {
2212 par64 |= phys_addr & ~0xfffULL;
2213 if (!attrs.secure) {
2214 par64 |= (1 << 9); /* NS */
2215 }
2216 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2217 par64 |= cacheattrs.shareability << 7; /* SH */
2218 } else {
2219 uint32_t fsr = arm_fi_to_lfsc(&fi);
2220
2221 par64 |= 1; /* F */
2222 par64 |= (fsr & 0x3f) << 1; /* FS */
2223 /* Note that S2WLK and FSTAGE are always zero, because we don't
2224 * implement virtualization and therefore there can't be a stage 2
2225 * fault.
2226 */
2227 }
2228 } else {
2229 /* fsr is a DFSR/IFSR value for the short descriptor
2230 * translation table format (with WnR always clear).
2231 * Convert it to a 32-bit PAR.
2232 */
2233 if (!ret) {
2234 /* We do not set any attribute bits in the PAR */
2235 if (page_size == (1 << 24)
2236 && arm_feature(env, ARM_FEATURE_V7)) {
2237 par64 = (phys_addr & 0xff000000) | (1 << 1);
2238 } else {
2239 par64 = phys_addr & 0xfffff000;
2240 }
2241 if (!attrs.secure) {
2242 par64 |= (1 << 9); /* NS */
2243 }
2244 } else {
2245 uint32_t fsr = arm_fi_to_sfsc(&fi);
2246
2247 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2248 ((fsr & 0xf) << 1) | 1;
2249 }
2250 }
2251 return par64;
2252 }
2253
2254 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2255 {
2256 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2257 uint64_t par64;
2258 ARMMMUIdx mmu_idx;
2259 int el = arm_current_el(env);
2260 bool secure = arm_is_secure_below_el3(env);
2261
2262 switch (ri->opc2 & 6) {
2263 case 0:
2264 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2265 switch (el) {
2266 case 3:
2267 mmu_idx = ARMMMUIdx_S1E3;
2268 break;
2269 case 2:
2270 mmu_idx = ARMMMUIdx_S1NSE1;
2271 break;
2272 case 1:
2273 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2274 break;
2275 default:
2276 g_assert_not_reached();
2277 }
2278 break;
2279 case 2:
2280 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2281 switch (el) {
2282 case 3:
2283 mmu_idx = ARMMMUIdx_S1SE0;
2284 break;
2285 case 2:
2286 mmu_idx = ARMMMUIdx_S1NSE0;
2287 break;
2288 case 1:
2289 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2290 break;
2291 default:
2292 g_assert_not_reached();
2293 }
2294 break;
2295 case 4:
2296 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2297 mmu_idx = ARMMMUIdx_S12NSE1;
2298 break;
2299 case 6:
2300 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2301 mmu_idx = ARMMMUIdx_S12NSE0;
2302 break;
2303 default:
2304 g_assert_not_reached();
2305 }
2306
2307 par64 = do_ats_write(env, value, access_type, mmu_idx);
2308
2309 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2310 }
2311
2312 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2313 uint64_t value)
2314 {
2315 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2316 uint64_t par64;
2317
2318 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2319
2320 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2321 }
2322
2323 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2324 bool isread)
2325 {
2326 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2327 return CP_ACCESS_TRAP;
2328 }
2329 return CP_ACCESS_OK;
2330 }
2331
2332 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2333 uint64_t value)
2334 {
2335 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2336 ARMMMUIdx mmu_idx;
2337 int secure = arm_is_secure_below_el3(env);
2338
2339 switch (ri->opc2 & 6) {
2340 case 0:
2341 switch (ri->opc1) {
2342 case 0: /* AT S1E1R, AT S1E1W */
2343 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2344 break;
2345 case 4: /* AT S1E2R, AT S1E2W */
2346 mmu_idx = ARMMMUIdx_S1E2;
2347 break;
2348 case 6: /* AT S1E3R, AT S1E3W */
2349 mmu_idx = ARMMMUIdx_S1E3;
2350 break;
2351 default:
2352 g_assert_not_reached();
2353 }
2354 break;
2355 case 2: /* AT S1E0R, AT S1E0W */
2356 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2357 break;
2358 case 4: /* AT S12E1R, AT S12E1W */
2359 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
2360 break;
2361 case 6: /* AT S12E0R, AT S12E0W */
2362 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
2363 break;
2364 default:
2365 g_assert_not_reached();
2366 }
2367
2368 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
2369 }
2370 #endif
2371
2372 static const ARMCPRegInfo vapa_cp_reginfo[] = {
2373 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2374 .access = PL1_RW, .resetvalue = 0,
2375 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2376 offsetoflow32(CPUARMState, cp15.par_ns) },
2377 .writefn = par_write },
2378 #ifndef CONFIG_USER_ONLY
2379 /* This underdecoding is safe because the reginfo is NO_RAW. */
2380 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
2381 .access = PL1_W, .accessfn = ats_access,
2382 .writefn = ats_write, .type = ARM_CP_NO_RAW },
2383 #endif
2384 REGINFO_SENTINEL
2385 };
2386
2387 /* Return basic MPU access permission bits. */
2388 static uint32_t simple_mpu_ap_bits(uint32_t val)
2389 {
2390 uint32_t ret;
2391 uint32_t mask;
2392 int i;
2393 ret = 0;
2394 mask = 3;
2395 for (i = 0; i < 16; i += 2) {
2396 ret |= (val >> i) & mask;
2397 mask <<= 2;
2398 }
2399 return ret;
2400 }
2401
2402 /* Pad basic MPU access permission bits to extended format. */
2403 static uint32_t extended_mpu_ap_bits(uint32_t val)
2404 {
2405 uint32_t ret;
2406 uint32_t mask;
2407 int i;
2408 ret = 0;
2409 mask = 3;
2410 for (i = 0; i < 16; i += 2) {
2411 ret |= (val & mask) << i;
2412 mask <<= 2;
2413 }
2414 return ret;
2415 }
2416
2417 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2418 uint64_t value)
2419 {
2420 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
2421 }
2422
2423 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2424 {
2425 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
2426 }
2427
2428 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2429 uint64_t value)
2430 {
2431 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
2432 }
2433
2434 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2435 {
2436 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
2437 }
2438
2439 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2440 {
2441 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2442
2443 if (!u32p) {
2444 return 0;
2445 }
2446
2447 u32p += env->pmsav7.rnr[M_REG_NS];
2448 return *u32p;
2449 }
2450
2451 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2452 uint64_t value)
2453 {
2454 ARMCPU *cpu = arm_env_get_cpu(env);
2455 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2456
2457 if (!u32p) {
2458 return;
2459 }
2460
2461 u32p += env->pmsav7.rnr[M_REG_NS];
2462 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
2463 *u32p = value;
2464 }
2465
2466 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2467 uint64_t value)
2468 {
2469 ARMCPU *cpu = arm_env_get_cpu(env);
2470 uint32_t nrgs = cpu->pmsav7_dregion;
2471
2472 if (value >= nrgs) {
2473 qemu_log_mask(LOG_GUEST_ERROR,
2474 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2475 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2476 return;
2477 }
2478
2479 raw_write(env, ri, value);
2480 }
2481
2482 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2483 /* Reset for all these registers is handled in arm_cpu_reset(),
2484 * because the PMSAv7 is also used by M-profile CPUs, which do
2485 * not register cpregs but still need the state to be reset.
2486 */
2487 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2488 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2489 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2490 .readfn = pmsav7_read, .writefn = pmsav7_write,
2491 .resetfn = arm_cp_reset_ignore },
2492 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2493 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2494 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2495 .readfn = pmsav7_read, .writefn = pmsav7_write,
2496 .resetfn = arm_cp_reset_ignore },
2497 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2498 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2499 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2500 .readfn = pmsav7_read, .writefn = pmsav7_write,
2501 .resetfn = arm_cp_reset_ignore },
2502 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2503 .access = PL1_RW,
2504 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
2505 .writefn = pmsav7_rgnr_write,
2506 .resetfn = arm_cp_reset_ignore },
2507 REGINFO_SENTINEL
2508 };
2509
2510 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2511 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2512 .access = PL1_RW, .type = ARM_CP_ALIAS,
2513 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2514 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2515 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2516 .access = PL1_RW, .type = ARM_CP_ALIAS,
2517 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2518 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2519 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2520 .access = PL1_RW,
2521 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2522 .resetvalue = 0, },
2523 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2524 .access = PL1_RW,
2525 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2526 .resetvalue = 0, },
2527 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2528 .access = PL1_RW,
2529 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2530 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2531 .access = PL1_RW,
2532 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
2533 /* Protection region base and size registers */
2534 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2535 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2536 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2537 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2538 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2539 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2540 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2541 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2542 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2543 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2544 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2545 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2546 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2547 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2548 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2549 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2550 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2551 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2552 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2553 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2554 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2555 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2556 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2557 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
2558 REGINFO_SENTINEL
2559 };
2560
2561 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2562 uint64_t value)
2563 {
2564 TCR *tcr = raw_ptr(env, ri);
2565 int maskshift = extract32(value, 0, 3);
2566
2567 if (!arm_feature(env, ARM_FEATURE_V8)) {
2568 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2569 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2570 * using Long-desciptor translation table format */
2571 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2572 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2573 /* In an implementation that includes the Security Extensions
2574 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2575 * Short-descriptor translation table format.
2576 */
2577 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2578 } else {
2579 value &= TTBCR_N;
2580 }
2581 }
2582
2583 /* Update the masks corresponding to the TCR bank being written
2584 * Note that we always calculate mask and base_mask, but
2585 * they are only used for short-descriptor tables (ie if EAE is 0);
2586 * for long-descriptor tables the TCR fields are used differently
2587 * and the mask and base_mask values are meaningless.
2588 */
2589 tcr->raw_tcr = value;
2590 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2591 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
2592 }
2593
2594 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2595 uint64_t value)
2596 {
2597 ARMCPU *cpu = arm_env_get_cpu(env);
2598
2599 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2600 /* With LPAE the TTBCR could result in a change of ASID
2601 * via the TTBCR.A1 bit, so do a TLB flush.
2602 */
2603 tlb_flush(CPU(cpu));
2604 }
2605 vmsa_ttbcr_raw_write(env, ri, value);
2606 }
2607
2608 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2609 {
2610 TCR *tcr = raw_ptr(env, ri);
2611
2612 /* Reset both the TCR as well as the masks corresponding to the bank of
2613 * the TCR being reset.
2614 */
2615 tcr->raw_tcr = 0;
2616 tcr->mask = 0;
2617 tcr->base_mask = 0xffffc000u;
2618 }
2619
2620 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2621 uint64_t value)
2622 {
2623 ARMCPU *cpu = arm_env_get_cpu(env);
2624 TCR *tcr = raw_ptr(env, ri);
2625
2626 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
2627 tlb_flush(CPU(cpu));
2628 tcr->raw_tcr = value;
2629 }
2630
2631 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2632 uint64_t value)
2633 {
2634 /* 64 bit accesses to the TTBRs can change the ASID and so we
2635 * must flush the TLB.
2636 */
2637 if (cpreg_field_is_64bit(ri)) {
2638 ARMCPU *cpu = arm_env_get_cpu(env);
2639
2640 tlb_flush(CPU(cpu));
2641 }
2642 raw_write(env, ri, value);
2643 }
2644
2645 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2646 uint64_t value)
2647 {
2648 ARMCPU *cpu = arm_env_get_cpu(env);
2649 CPUState *cs = CPU(cpu);
2650
2651 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2652 if (raw_read(env, ri) != value) {
2653 tlb_flush_by_mmuidx(cs,
2654 ARMMMUIdxBit_S12NSE1 |
2655 ARMMMUIdxBit_S12NSE0 |
2656 ARMMMUIdxBit_S2NS);
2657 raw_write(env, ri, value);
2658 }
2659 }
2660
2661 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
2662 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2663 .access = PL1_RW, .type = ARM_CP_ALIAS,
2664 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
2665 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
2666 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2667 .access = PL1_RW, .resetvalue = 0,
2668 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2669 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
2670 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2671 .access = PL1_RW, .resetvalue = 0,
2672 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2673 offsetof(CPUARMState, cp15.dfar_ns) } },
2674 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2675 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2676 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2677 .resetvalue = 0, },
2678 REGINFO_SENTINEL
2679 };
2680
2681 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
2682 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2683 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2684 .access = PL1_RW,
2685 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
2686 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
2687 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2688 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2689 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2690 offsetof(CPUARMState, cp15.ttbr0_ns) } },
2691 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
2692 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2693 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2694 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2695 offsetof(CPUARMState, cp15.ttbr1_ns) } },
2696 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2697 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2698 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2699 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2700 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
2701 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2702 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
2703 .raw_writefn = vmsa_ttbcr_raw_write,
2704 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2705 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
2706 REGINFO_SENTINEL
2707 };
2708
2709 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2710 uint64_t value)
2711 {
2712 env->cp15.c15_ticonfig = value & 0xe7;
2713 /* The OS_TYPE bit in this register changes the reported CPUID! */
2714 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2715 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
2716 }
2717
2718 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2719 uint64_t value)
2720 {
2721 env->cp15.c15_threadid = value & 0xffff;
2722 }
2723
2724 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725 uint64_t value)
2726 {
2727 /* Wait-for-interrupt (deprecated) */
2728 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
2729 }
2730
2731 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2732 uint64_t value)
2733 {
2734 /* On OMAP there are registers indicating the max/min index of dcache lines
2735 * containing a dirty line; cache flush operations have to reset these.
2736 */
2737 env->cp15.c15_i_max = 0x000;
2738 env->cp15.c15_i_min = 0xff0;
2739 }
2740
2741 static const ARMCPRegInfo omap_cp_reginfo[] = {
2742 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2743 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
2744 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
2745 .resetvalue = 0, },
2746 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2747 .access = PL1_RW, .type = ARM_CP_NOP },
2748 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2749 .access = PL1_RW,
2750 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2751 .writefn = omap_ticonfig_write },
2752 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2753 .access = PL1_RW,
2754 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2755 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2756 .access = PL1_RW, .resetvalue = 0xff0,
2757 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2758 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2759 .access = PL1_RW,
2760 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2761 .writefn = omap_threadid_write },
2762 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2763 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2764 .type = ARM_CP_NO_RAW,
2765 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2766 /* TODO: Peripheral port remap register:
2767 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2768 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2769 * when MMU is off.
2770 */
2771 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
2772 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
2773 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
2774 .writefn = omap_cachemaint_write },
2775 { .name = "C9", .cp = 15, .crn = 9,
2776 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2777 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
2778 REGINFO_SENTINEL
2779 };
2780
2781 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782 uint64_t value)
2783 {
2784 env->cp15.c15_cpar = value & 0x3fff;
2785 }
2786
2787 static const ARMCPRegInfo xscale_cp_reginfo[] = {
2788 { .name = "XSCALE_CPAR",
2789 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2790 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2791 .writefn = xscale_cpar_write, },
2792 { .name = "XSCALE_AUXCR",
2793 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2794 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2795 .resetvalue = 0, },
2796 /* XScale specific cache-lockdown: since we have no cache we NOP these
2797 * and hope the guest does not really rely on cache behaviour.
2798 */
2799 { .name = "XSCALE_LOCK_ICACHE_LINE",
2800 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2801 .access = PL1_W, .type = ARM_CP_NOP },
2802 { .name = "XSCALE_UNLOCK_ICACHE",
2803 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2804 .access = PL1_W, .type = ARM_CP_NOP },
2805 { .name = "XSCALE_DCACHE_LOCK",
2806 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2807 .access = PL1_RW, .type = ARM_CP_NOP },
2808 { .name = "XSCALE_UNLOCK_DCACHE",
2809 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2810 .access = PL1_W, .type = ARM_CP_NOP },
2811 REGINFO_SENTINEL
2812 };
2813
2814 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2815 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2816 * implementation of this implementation-defined space.
2817 * Ideally this should eventually disappear in favour of actually
2818 * implementing the correct behaviour for all cores.
2819 */
2820 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2821 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2822 .access = PL1_RW,
2823 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
2824 .resetvalue = 0 },
2825 REGINFO_SENTINEL
2826 };
2827
2828 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2829 /* Cache status: RAZ because we have no cache so it's always clean */
2830 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
2831 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2832 .resetvalue = 0 },
2833 REGINFO_SENTINEL
2834 };
2835
2836 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2837 /* We never have a a block transfer operation in progress */
2838 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
2839 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2840 .resetvalue = 0 },
2841 /* The cache ops themselves: these all NOP for QEMU */
2842 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2843 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2844 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2845 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2846 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2847 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2848 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2849 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2850 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2851 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2852 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2853 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2854 REGINFO_SENTINEL
2855 };
2856
2857 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2858 /* The cache test-and-clean instructions always return (1 << 30)
2859 * to indicate that there are no dirty cache lines.
2860 */
2861 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2862 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2863 .resetvalue = (1 << 30) },
2864 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2865 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2866 .resetvalue = (1 << 30) },
2867 REGINFO_SENTINEL
2868 };
2869
2870 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2871 /* Ignore ReadBuffer accesses */
2872 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2873 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2874 .access = PL1_RW, .resetvalue = 0,
2875 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2876 REGINFO_SENTINEL
2877 };
2878
2879 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2880 {
2881 ARMCPU *cpu = arm_env_get_cpu(env);
2882 unsigned int cur_el = arm_current_el(env);
2883 bool secure = arm_is_secure(env);
2884
2885 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2886 return env->cp15.vpidr_el2;
2887 }
2888 return raw_read(env, ri);
2889 }
2890
2891 static uint64_t mpidr_read_val(CPUARMState *env)
2892 {
2893 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2894 uint64_t mpidr = cpu->mp_affinity;
2895
2896 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2897 mpidr |= (1U << 31);
2898 /* Cores which are uniprocessor (non-coherent)
2899 * but still implement the MP extensions set
2900 * bit 30. (For instance, Cortex-R5).
2901 */
2902 if (cpu->mp_is_up) {
2903 mpidr |= (1u << 30);
2904 }
2905 }
2906 return mpidr;
2907 }
2908
2909 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2910 {
2911 unsigned int cur_el = arm_current_el(env);
2912 bool secure = arm_is_secure(env);
2913
2914 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2915 return env->cp15.vmpidr_el2;
2916 }
2917 return mpidr_read_val(env);
2918 }
2919
2920 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2921 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2922 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2923 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2924 REGINFO_SENTINEL
2925 };
2926
2927 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2928 /* NOP AMAIR0/1 */
2929 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2930 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2931 .access = PL1_RW, .type = ARM_CP_CONST,
2932 .resetvalue = 0 },
2933 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2934 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2935 .access = PL1_RW, .type = ARM_CP_CONST,
2936 .resetvalue = 0 },
2937 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2938 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2939 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2940 offsetof(CPUARMState, cp15.par_ns)} },
2941 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2942 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2943 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2944 offsetof(CPUARMState, cp15.ttbr0_ns) },
2945 .writefn = vmsa_ttbr_write, },
2946 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2947 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2948 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2949 offsetof(CPUARMState, cp15.ttbr1_ns) },
2950 .writefn = vmsa_ttbr_write, },
2951 REGINFO_SENTINEL
2952 };
2953
2954 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2955 {
2956 return vfp_get_fpcr(env);
2957 }
2958
2959 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2960 uint64_t value)
2961 {
2962 vfp_set_fpcr(env, value);
2963 }
2964
2965 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2966 {
2967 return vfp_get_fpsr(env);
2968 }
2969
2970 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2971 uint64_t value)
2972 {
2973 vfp_set_fpsr(env, value);
2974 }
2975
2976 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2977 bool isread)
2978 {
2979 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2980 return CP_ACCESS_TRAP;
2981 }
2982 return CP_ACCESS_OK;
2983 }
2984
2985 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2986 uint64_t value)
2987 {
2988 env->daif = value & PSTATE_DAIF;
2989 }
2990
2991 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2992 const ARMCPRegInfo *ri,
2993 bool isread)
2994 {
2995 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2996 * SCTLR_EL1.UCI is set.
2997 */
2998 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2999 return CP_ACCESS_TRAP;
3000 }
3001 return CP_ACCESS_OK;
3002 }
3003
3004 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3005 * Page D4-1736 (DDI0487A.b)
3006 */
3007
3008 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3009 uint64_t value)
3010 {
3011 CPUState *cs = ENV_GET_CPU(env);
3012
3013 if (arm_is_secure_below_el3(env)) {
3014 tlb_flush_by_mmuidx(cs,
3015 ARMMMUIdxBit_S1SE1 |
3016 ARMMMUIdxBit_S1SE0);
3017 } else {
3018 tlb_flush_by_mmuidx(cs,
3019 ARMMMUIdxBit_S12NSE1 |
3020 ARMMMUIdxBit_S12NSE0);
3021 }
3022 }
3023
3024 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3025 uint64_t value)
3026 {
3027 CPUState *cs = ENV_GET_CPU(env);
3028 bool sec = arm_is_secure_below_el3(env);
3029
3030 if (sec) {
3031 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3032 ARMMMUIdxBit_S1SE1 |
3033 ARMMMUIdxBit_S1SE0);
3034 } else {
3035 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3036 ARMMMUIdxBit_S12NSE1 |
3037 ARMMMUIdxBit_S12NSE0);
3038 }
3039 }
3040
3041 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3042 uint64_t value)
3043 {
3044 /* Note that the 'ALL' scope must invalidate both stage 1 and
3045 * stage 2 translations, whereas most other scopes only invalidate
3046 * stage 1 translations.
3047 */
3048 ARMCPU *cpu = arm_env_get_cpu(env);
3049 CPUState *cs = CPU(cpu);
3050
3051 if (arm_is_secure_below_el3(env)) {
3052 tlb_flush_by_mmuidx(cs,
3053 ARMMMUIdxBit_S1SE1 |
3054 ARMMMUIdxBit_S1SE0);
3055 } else {
3056 if (arm_feature(env, ARM_FEATURE_EL2)) {
3057 tlb_flush_by_mmuidx(cs,
3058 ARMMMUIdxBit_S12NSE1 |
3059 ARMMMUIdxBit_S12NSE0 |
3060 ARMMMUIdxBit_S2NS);
3061 } else {
3062 tlb_flush_by_mmuidx(cs,
3063 ARMMMUIdxBit_S12NSE1 |
3064 ARMMMUIdxBit_S12NSE0);
3065 }
3066 }
3067 }
3068
3069 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3070 uint64_t value)
3071 {
3072 ARMCPU *cpu = arm_env_get_cpu(env);
3073 CPUState *cs = CPU(cpu);
3074
3075 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
3076 }
3077
3078 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3079 uint64_t value)
3080 {
3081 ARMCPU *cpu = arm_env_get_cpu(env);
3082 CPUState *cs = CPU(cpu);
3083
3084 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
3085 }
3086
3087 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3088 uint64_t value)
3089 {
3090 /* Note that the 'ALL' scope must invalidate both stage 1 and
3091 * stage 2 translations, whereas most other scopes only invalidate
3092 * stage 1 translations.
3093 */
3094 CPUState *cs = ENV_GET_CPU(env);
3095 bool sec = arm_is_secure_below_el3(env);
3096 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
3097
3098 if (sec) {
3099 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3100 ARMMMUIdxBit_S1SE1 |
3101 ARMMMUIdxBit_S1SE0);
3102 } else if (has_el2) {
3103 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3104 ARMMMUIdxBit_S12NSE1 |
3105 ARMMMUIdxBit_S12NSE0 |
3106 ARMMMUIdxBit_S2NS);
3107 } else {
3108 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3109 ARMMMUIdxBit_S12NSE1 |
3110 ARMMMUIdxBit_S12NSE0);
3111 }
3112 }
3113
3114 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3115 uint64_t value)
3116 {
3117 CPUState *cs = ENV_GET_CPU(env);
3118
3119 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
3120 }
3121
3122 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3123 uint64_t value)
3124 {
3125 CPUState *cs = ENV_GET_CPU(env);
3126
3127 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
3128 }
3129
3130 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3131 uint64_t value)
3132 {
3133 /* Invalidate by VA, EL1&0 (AArch64 version).
3134 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3135 * since we don't support flush-for-specific-ASID-only or
3136 * flush-last-level-only.
3137 */
3138 ARMCPU *cpu = arm_env_get_cpu(env);
3139 CPUState *cs = CPU(cpu);
3140 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3141
3142 if (arm_is_secure_below_el3(env)) {
3143 tlb_flush_page_by_mmuidx(cs, pageaddr,
3144 ARMMMUIdxBit_S1SE1 |
3145 ARMMMUIdxBit_S1SE0);
3146 } else {
3147 tlb_flush_page_by_mmuidx(cs, pageaddr,
3148 ARMMMUIdxBit_S12NSE1 |
3149 ARMMMUIdxBit_S12NSE0);
3150 }
3151 }
3152
3153 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3154 uint64_t value)
3155 {
3156 /* Invalidate by VA, EL2
3157 * Currently handles both VAE2 and VALE2, since we don't support
3158 * flush-last-level-only.
3159 */
3160 ARMCPU *cpu = arm_env_get_cpu(env);
3161 CPUState *cs = CPU(cpu);
3162 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3163
3164 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
3165 }
3166
3167 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3168 uint64_t value)
3169 {
3170 /* Invalidate by VA, EL3
3171 * Currently handles both VAE3 and VALE3, since we don't support
3172 * flush-last-level-only.
3173 */
3174 ARMCPU *cpu = arm_env_get_cpu(env);
3175 CPUState *cs = CPU(cpu);
3176 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3177
3178 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
3179 }
3180
3181 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3182 uint64_t value)
3183 {
3184 ARMCPU *cpu = arm_env_get_cpu(env);
3185 CPUState *cs = CPU(cpu);
3186 bool sec = arm_is_secure_below_el3(env);
3187 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3188
3189 if (sec) {
3190 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3191 ARMMMUIdxBit_S1SE1 |
3192 ARMMMUIdxBit_S1SE0);
3193 } else {
3194 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3195 ARMMMUIdxBit_S12NSE1 |
3196 ARMMMUIdxBit_S12NSE0);
3197 }
3198 }
3199
3200 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3201 uint64_t value)
3202 {
3203 CPUState *cs = ENV_GET_CPU(env);
3204 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3205
3206 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3207 ARMMMUIdxBit_S1E2);
3208 }
3209
3210 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3211 uint64_t value)
3212 {
3213 CPUState *cs = ENV_GET_CPU(env);
3214 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3215
3216 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3217 ARMMMUIdxBit_S1E3);
3218 }
3219
3220 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3221 uint64_t value)
3222 {
3223 /* Invalidate by IPA. This has to invalidate any structures that
3224 * contain only stage 2 translation information, but does not need
3225 * to apply to structures that contain combined stage 1 and stage 2
3226 * translation information.
3227 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3228 */
3229 ARMCPU *cpu = arm_env_get_cpu(env);
3230 CPUState *cs = CPU(cpu);
3231 uint64_t pageaddr;
3232
3233 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3234 return;
3235 }
3236
3237 pageaddr = sextract64(value << 12, 0, 48);
3238
3239 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
3240 }
3241
3242 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3243 uint64_t value)
3244 {
3245 CPUState *cs = ENV_GET_CPU(env);
3246 uint64_t pageaddr;
3247
3248 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3249 return;
3250 }
3251
3252 pageaddr = sextract64(value << 12, 0, 48);
3253
3254 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3255 ARMMMUIdxBit_S2NS);
3256 }
3257
3258 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3259 bool isread)
3260 {
3261 /* We don't implement EL2, so the only control on DC ZVA is the
3262 * bit in the SCTLR which can prohibit access for EL0.
3263 */
3264 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
3265 return CP_ACCESS_TRAP;
3266 }
3267 return CP_ACCESS_OK;
3268 }
3269
3270 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3271 {
3272 ARMCPU *cpu = arm_env_get_cpu(env);
3273 int dzp_bit = 1 << 4;
3274
3275 /* DZP indicates whether DC ZVA access is allowed */
3276 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
3277 dzp_bit = 0;
3278 }
3279 return cpu->dcz_blocksize | dzp_bit;
3280 }
3281
3282 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3283 bool isread)
3284 {
3285 if (!(env->pstate & PSTATE_SP)) {
3286 /* Access to SP_EL0 is undefined if it's being used as
3287 * the stack pointer.
3288 */
3289 return CP_ACCESS_TRAP_UNCATEGORIZED;
3290 }
3291 return CP_ACCESS_OK;
3292 }
3293
3294 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3295 {
3296 return env->pstate & PSTATE_SP;
3297 }
3298
3299 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3300 {
3301 update_spsel(env, val);
3302 }
3303
3304 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3305 uint64_t value)
3306 {
3307 ARMCPU *cpu = arm_env_get_cpu(env);
3308
3309 if (raw_read(env, ri) == value) {
3310 /* Skip the TLB flush if nothing actually changed; Linux likes
3311 * to do a lot of pointless SCTLR writes.
3312 */
3313 return;
3314 }
3315
3316 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3317 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3318 value &= ~SCTLR_M;
3319 }
3320
3321 raw_write(env, ri, value);
3322 /* ??? Lots of these bits are not implemented. */
3323 /* This may enable/disable the MMU, so do a TLB flush. */
3324 tlb_flush(CPU(cpu));
3325 }
3326
3327 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3328 bool isread)
3329 {
3330 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
3331 return CP_ACCESS_TRAP_FP_EL2;
3332 }
3333 if (env->cp15.cptr_el[3] & CPTR_TFP) {
3334 return CP_ACCESS_TRAP_FP_EL3;
3335 }
3336 return CP_ACCESS_OK;
3337 }
3338
3339 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3340 uint64_t value)
3341 {
3342 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3343 }
3344
3345 static const ARMCPRegInfo v8_cp_reginfo[] = {
3346 /* Minimal set of EL0-visible registers. This will need to be expanded
3347 * significantly for system emulation of AArch64 CPUs.
3348 */
3349 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3350 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3351 .access = PL0_RW, .type = ARM_CP_NZCV },
3352 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3353 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
3354 .type = ARM_CP_NO_RAW,
3355 .access = PL0_RW, .accessfn = aa64_daif_access,
3356 .fieldoffset = offsetof(CPUARMState, daif),
3357 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
3358 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3359 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3360 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
3361 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3362 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3363 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3364 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
3365 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
3366 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3367 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
3368 .access = PL0_R, .type = ARM_CP_NO_RAW,
3369 .readfn = aa64_dczid_read },
3370 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3372 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3373 #ifndef CONFIG_USER_ONLY
3374 /* Avoid overhead of an access check that always passes in user-mode */
3375 .accessfn = aa64_zva_access,
3376 #endif
3377 },
3378 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3379 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3380 .access = PL1_R, .type = ARM_CP_CURRENTEL },
3381 /* Cache ops: all NOPs since we don't emulate caches */
3382 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3383 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3384 .access = PL1_W, .type = ARM_CP_NOP },
3385 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3386 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3387 .access = PL1_W, .type = ARM_CP_NOP },
3388 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3389 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3390 .access = PL0_W, .type = ARM_CP_NOP,
3391 .accessfn = aa64_cacheop_access },
3392 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3393 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3394 .access = PL1_W, .type = ARM_CP_NOP },
3395 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3396 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3397 .access = PL1_W, .type = ARM_CP_NOP },
3398 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3399 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3400 .access = PL0_W, .type = ARM_CP_NOP,
3401 .accessfn = aa64_cacheop_access },
3402 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3403 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3404 .access = PL1_W, .type = ARM_CP_NOP },
3405 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3406 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3407 .access = PL0_W, .type = ARM_CP_NOP,
3408 .accessfn = aa64_cacheop_access },
3409 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3410 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3411 .access = PL0_W, .type = ARM_CP_NOP,
3412 .accessfn = aa64_cacheop_access },
3413 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3414 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3415 .access = PL1_W, .type = ARM_CP_NOP },
3416 /* TLBI operations */
3417 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
3418 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
3419 .access = PL1_W, .type = ARM_CP_NO_RAW,
3420 .writefn = tlbi_aa64_vmalle1is_write },
3421 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
3422 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
3423 .access = PL1_W, .type = ARM_CP_NO_RAW,
3424 .writefn = tlbi_aa64_vae1is_write },
3425 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
3426 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
3427 .access = PL1_W, .type = ARM_CP_NO_RAW,
3428 .writefn = tlbi_aa64_vmalle1is_write },
3429 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
3430 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
3431 .access = PL1_W, .type = ARM_CP_NO_RAW,
3432 .writefn = tlbi_aa64_vae1is_write },
3433 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
3434 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3435 .access = PL1_W, .type = ARM_CP_NO_RAW,
3436 .writefn = tlbi_aa64_vae1is_write },
3437 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
3438 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3439 .access = PL1_W, .type = ARM_CP_NO_RAW,
3440 .writefn = tlbi_aa64_vae1is_write },
3441 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
3442 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
3443 .access = PL1_W, .type = ARM_CP_NO_RAW,
3444 .writefn = tlbi_aa64_vmalle1_write },
3445 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
3446 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
3447 .access = PL1_W, .type = ARM_CP_NO_RAW,
3448 .writefn = tlbi_aa64_vae1_write },
3449 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
3450 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
3451 .access = PL1_W, .type = ARM_CP_NO_RAW,
3452 .writefn = tlbi_aa64_vmalle1_write },
3453 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
3454 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
3455 .access = PL1_W, .type = ARM_CP_NO_RAW,
3456 .writefn = tlbi_aa64_vae1_write },
3457 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
3458 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3459 .access = PL1_W, .type = ARM_CP_NO_RAW,
3460 .writefn = tlbi_aa64_vae1_write },
3461 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
3462 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3463 .access = PL1_W, .type = ARM_CP_NO_RAW,
3464 .writefn = tlbi_aa64_vae1_write },
3465 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3466 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3467 .access = PL2_W, .type = ARM_CP_NO_RAW,
3468 .writefn = tlbi_aa64_ipas2e1is_write },
3469 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3470 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3471 .access = PL2_W, .type = ARM_CP_NO_RAW,
3472 .writefn = tlbi_aa64_ipas2e1is_write },
3473 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3474 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3475 .access = PL2_W, .type = ARM_CP_NO_RAW,
3476 .writefn = tlbi_aa64_alle1is_write },
3477 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3478 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3479 .access = PL2_W, .type = ARM_CP_NO_RAW,
3480 .writefn = tlbi_aa64_alle1is_write },
3481 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3482 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3483 .access = PL2_W, .type = ARM_CP_NO_RAW,
3484 .writefn = tlbi_aa64_ipas2e1_write },
3485 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3486 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3487 .access = PL2_W, .type = ARM_CP_NO_RAW,
3488 .writefn = tlbi_aa64_ipas2e1_write },
3489 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3490 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3491 .access = PL2_W, .type = ARM_CP_NO_RAW,
3492 .writefn = tlbi_aa64_alle1_write },
3493 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3494 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3495 .access = PL2_W, .type = ARM_CP_NO_RAW,
3496 .writefn = tlbi_aa64_alle1is_write },
3497 #ifndef CONFIG_USER_ONLY
3498 /* 64 bit address translation operations */
3499 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3500 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3501 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3502 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3503 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3504 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3505 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3506 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3507 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3508 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3509 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3510 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3511 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3512 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3513 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3514 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3515 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3516 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3517 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3518 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3519 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3520 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3521 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3522 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3523 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3524 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3525 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3526 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3527 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3528 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3529 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3530 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3531 .type = ARM_CP_ALIAS,
3532 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3533 .access = PL1_RW, .resetvalue = 0,
3534 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3535 .writefn = par_write },
3536 #endif
3537 /* TLB invalidate last level of translation table walk */
3538 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3539 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3540 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3541 .type = ARM_CP_NO_RAW, .access = PL1_W,
3542 .writefn = tlbimvaa_is_write },
3543 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3544 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3545 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3546 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3547 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3548 .type = ARM_CP_NO_RAW, .access = PL2_W,
3549 .writefn = tlbimva_hyp_write },
3550 { .name = "TLBIMVALHIS",
3551 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3552 .type = ARM_CP_NO_RAW, .access = PL2_W,
3553 .writefn = tlbimva_hyp_is_write },
3554 { .name = "TLBIIPAS2",
3555 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3556 .type = ARM_CP_NO_RAW, .access = PL2_W,
3557 .writefn = tlbiipas2_write },
3558 { .name = "TLBIIPAS2IS",
3559 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3560 .type = ARM_CP_NO_RAW, .access = PL2_W,
3561 .writefn = tlbiipas2_is_write },
3562 { .name = "TLBIIPAS2L",
3563 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3564 .type = ARM_CP_NO_RAW, .access = PL2_W,
3565 .writefn = tlbiipas2_write },
3566 { .name = "TLBIIPAS2LIS",
3567 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3568 .type = ARM_CP_NO_RAW, .access = PL2_W,
3569 .writefn = tlbiipas2_is_write },
3570 /* 32 bit cache operations */
3571 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3572 .type = ARM_CP_NOP, .access = PL1_W },
3573 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3574 .type = ARM_CP_NOP, .access = PL1_W },
3575 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3576 .type = ARM_CP_NOP, .access = PL1_W },
3577 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3578 .type = ARM_CP_NOP, .access = PL1_W },
3579 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3580 .type = ARM_CP_NOP, .access = PL1_W },
3581 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3582 .type = ARM_CP_NOP, .access = PL1_W },
3583 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3584 .type = ARM_CP_NOP, .access = PL1_W },
3585 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3586 .type = ARM_CP_NOP, .access = PL1_W },
3587 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3588 .type = ARM_CP_NOP, .access = PL1_W },
3589 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3590 .type = ARM_CP_NOP, .access = PL1_W },
3591 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3592 .type = ARM_CP_NOP, .access = PL1_W },
3593 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3594 .type = ARM_CP_NOP, .access = PL1_W },
3595 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3596 .type = ARM_CP_NOP, .access = PL1_W },
3597 /* MMU Domain access control / MPU write buffer control */
3598 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3599 .access = PL1_RW, .resetvalue = 0,
3600 .writefn = dacr_write, .raw_writefn = raw_write,
3601 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3602 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3603 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3604 .type = ARM_CP_ALIAS,
3605 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3606 .access = PL1_RW,
3607 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3608 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3609 .type = ARM_CP_ALIAS,
3610 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3611 .access = PL1_RW,
3612 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
3613 /* We rely on the access checks not allowing the guest to write to the
3614 * state field when SPSel indicates that it's being used as the stack
3615 * pointer.
3616 */
3617 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3618 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3619 .access = PL1_RW, .accessfn = sp_el0_access,
3620 .type = ARM_CP_ALIAS,
3621 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3622 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3623 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3624 .access = PL2_RW, .type = ARM_CP_ALIAS,
3625 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3626 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3627 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3628 .type = ARM_CP_NO_RAW,
3629 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3630 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3631 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3632 .type = ARM_CP_ALIAS,
3633 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3634 .access = PL2_RW, .accessfn = fpexc32_access },
3635 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3636 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3637 .access = PL2_RW, .resetvalue = 0,
3638 .writefn = dacr_write, .raw_writefn = raw_write,
3639 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3640 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3641 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3642 .access = PL2_RW, .resetvalue = 0,
3643 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3644 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3645 .type = ARM_CP_ALIAS,
3646 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3647 .access = PL2_RW,
3648 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3649 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3650 .type = ARM_CP_ALIAS,
3651 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3652 .access = PL2_RW,
3653 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3654 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3655 .type = ARM_CP_ALIAS,
3656 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3657 .access = PL2_RW,
3658 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3659 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3660 .type = ARM_CP_ALIAS,
3661 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3662 .access = PL2_RW,
3663 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
3664 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3665 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3666 .resetvalue = 0,
3667 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3668 { .name = "SDCR", .type = ARM_CP_ALIAS,
3669 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3670 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3671 .writefn = sdcr_write,
3672 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
3673 REGINFO_SENTINEL
3674 };
3675
3676 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
3677 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3678 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3679 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3680 .access = PL2_RW,
3681 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3682 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3683 .type = ARM_CP_NO_RAW,
3684 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3685 .access = PL2_RW,
3686 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3687 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3688 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3689 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3690 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3691 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3692 .access = PL2_RW, .type = ARM_CP_CONST,
3693 .resetvalue = 0 },
3694 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3695 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3696 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3697 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3698 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3699 .access = PL2_RW, .type = ARM_CP_CONST,
3700 .resetvalue = 0 },
3701 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3702 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3703 .access = PL2_RW, .type = ARM_CP_CONST,
3704 .resetvalue = 0 },
3705 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3706 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3707 .access = PL2_RW, .type = ARM_CP_CONST,
3708 .resetvalue = 0 },
3709 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3710 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3711 .access = PL2_RW, .type = ARM_CP_CONST,
3712 .resetvalue = 0 },
3713 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3714 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3715 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3716 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3717 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3718 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3719 .type = ARM_CP_CONST, .resetvalue = 0 },
3720 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3721 .cp = 15, .opc1 = 6, .crm = 2,
3722 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3723 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3724 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3725 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3726 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3727 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3728 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3729 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3730 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3731 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3732 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3733 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3734 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3735 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3736 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3737 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3738 .resetvalue = 0 },
3739 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3740 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3741 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3742 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3743 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3744 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3745 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3746 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3747 .resetvalue = 0 },
3748 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3749 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3750 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3751 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3752 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3753 .resetvalue = 0 },
3754 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3755 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3756 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3757 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3758 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3759 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3760 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3761 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3762 .access = PL2_RW, .accessfn = access_tda,
3763 .type = ARM_CP_CONST, .resetvalue = 0 },
3764 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3765 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3766 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3767 .type = ARM_CP_CONST, .resetvalue = 0 },
3768 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3769 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3770 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3771 REGINFO_SENTINEL
3772 };
3773
3774 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3775 {
3776 ARMCPU *cpu = arm_env_get_cpu(env);
3777 uint64_t valid_mask = HCR_MASK;
3778
3779 if (arm_feature(env, ARM_FEATURE_EL3)) {
3780 valid_mask &= ~HCR_HCD;
3781 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3782 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3783 * However, if we're using the SMC PSCI conduit then QEMU is
3784 * effectively acting like EL3 firmware and so the guest at
3785 * EL2 should retain the ability to prevent EL1 from being
3786 * able to make SMC calls into the ersatz firmware, so in
3787 * that case HCR.TSC should be read/write.
3788 */
3789 valid_mask &= ~HCR_TSC;
3790 }
3791
3792 /* Clear RES0 bits. */
3793 value &= valid_mask;
3794
3795 /* These bits change the MMU setup:
3796 * HCR_VM enables stage 2 translation
3797 * HCR_PTW forbids certain page-table setups
3798 * HCR_DC Disables stage1 and enables stage2 translation
3799 */
3800 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3801 tlb_flush(CPU(cpu));
3802 }
3803 raw_write(env, ri, value);
3804 }
3805
3806 static const ARMCPRegInfo el2_cp_reginfo[] = {
3807 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3808 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3809 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3810 .writefn = hcr_write },
3811 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3812 .type = ARM_CP_ALIAS,
3813 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3814 .access = PL2_RW,
3815 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3816 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3817 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3818 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3819 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3820 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3821 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3822 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3823 .type = ARM_CP_ALIAS,
3824 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3825 .access = PL2_RW,
3826 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
3827 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3828 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3829 .access = PL2_RW, .writefn = vbar_write,
3830 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3831 .resetvalue = 0 },
3832 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3833 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3834 .access = PL3_RW, .type = ARM_CP_ALIAS,
3835 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3836 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3837 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3838 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3839 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3840 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3841 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3842 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3843 .resetvalue = 0 },
3844 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3845 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3846 .access = PL2_RW, .type = ARM_CP_ALIAS,
3847 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3848 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3849 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3850 .access = PL2_RW, .type = ARM_CP_CONST,
3851 .resetvalue = 0 },
3852 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3853 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3854 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3855 .access = PL2_RW, .type = ARM_CP_CONST,
3856 .resetvalue = 0 },
3857 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3858 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3859 .access = PL2_RW, .type = ARM_CP_CONST,
3860 .resetvalue = 0 },
3861 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3862 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3863 .access = PL2_RW, .type = ARM_CP_CONST,
3864 .resetvalue = 0 },
3865 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3866 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3867 .access = PL2_RW,
3868 /* no .writefn needed as this can't cause an ASID change;
3869 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3870 */
3871 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3872 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3873 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3874 .type = ARM_CP_ALIAS,
3875 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3876 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3877 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3878 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3879 .access = PL2_RW,
3880 /* no .writefn needed as this can't cause an ASID change;
3881 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3882 */
3883 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3884 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3885 .cp = 15, .opc1 = 6, .crm = 2,
3886 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3887 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3888 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3889 .writefn = vttbr_write },
3890 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3891 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3892 .access = PL2_RW, .writefn = vttbr_write,
3893 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3894 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3895 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3896 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3897 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3898 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3899 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3900 .access = PL2_RW, .resetvalue = 0,
3901 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3902 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3903 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3904 .access = PL2_RW, .resetvalue = 0,
3905 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3906 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3907 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3908 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3909 { .name = "TLBIALLNSNH",
3910 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3911 .type = ARM_CP_NO_RAW, .access = PL2_W,
3912 .writefn = tlbiall_nsnh_write },
3913 { .name = "TLBIALLNSNHIS",
3914 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3915 .type = ARM_CP_NO_RAW, .access = PL2_W,
3916 .writefn = tlbiall_nsnh_is_write },
3917 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3918 .type = ARM_CP_NO_RAW, .access = PL2_W,
3919 .writefn = tlbiall_hyp_write },
3920 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3921 .type = ARM_CP_NO_RAW, .access = PL2_W,
3922 .writefn = tlbiall_hyp_is_write },
3923 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3924 .type = ARM_CP_NO_RAW, .access = PL2_W,
3925 .writefn = tlbimva_hyp_write },
3926 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3927 .type = ARM_CP_NO_RAW, .access = PL2_W,
3928 .writefn = tlbimva_hyp_is_write },
3929 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3930 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3931 .type = ARM_CP_NO_RAW, .access = PL2_W,
3932 .writefn = tlbi_aa64_alle2_write },
3933 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3934 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3935 .type = ARM_CP_NO_RAW, .access = PL2_W,
3936 .writefn = tlbi_aa64_vae2_write },
3937 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3938 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3939 .access = PL2_W, .type = ARM_CP_NO_RAW,
3940 .writefn = tlbi_aa64_vae2_write },
3941 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3942 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3943 .access = PL2_W, .type = ARM_CP_NO_RAW,
3944 .writefn = tlbi_aa64_alle2is_write },
3945 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3946 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3947 .type = ARM_CP_NO_RAW, .access = PL2_W,
3948 .writefn = tlbi_aa64_vae2is_write },
3949 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3950 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3951 .access = PL2_W, .type = ARM_CP_NO_RAW,
3952 .writefn = tlbi_aa64_vae2is_write },
3953 #ifndef CONFIG_USER_ONLY
3954 /* Unlike the other EL2-related AT operations, these must
3955 * UNDEF from EL3 if EL2 is not implemented, which is why we
3956 * define them here rather than with the rest of the AT ops.
3957 */
3958 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3959 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3960 .access = PL2_W, .accessfn = at_s1e2_access,
3961 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3962 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3963 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3964 .access = PL2_W, .accessfn = at_s1e2_access,
3965 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3966 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3967 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3968 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3969 * to behave as if SCR.NS was 1.
3970 */
3971 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3972 .access = PL2_W,
3973 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3974 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3975 .access = PL2_W,
3976 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3977 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3978 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3979 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3980 * reset values as IMPDEF. We choose to reset to 3 to comply with
3981 * both ARMv7 and ARMv8.
3982 */
3983 .access = PL2_RW, .resetvalue = 3,
3984 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
3985 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3986 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3987 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3988 .writefn = gt_cntvoff_write,
3989 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3990 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3991 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3992 .writefn = gt_cntvoff_write,
3993 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3994 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3995 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3996 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3997 .type = ARM_CP_IO, .access = PL2_RW,
3998 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3999 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4000 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4001 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4002 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4003 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4004 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4005 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
4006 .resetfn = gt_hyp_timer_reset,
4007 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4008 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4009 .type = ARM_CP_IO,
4010 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4011 .access = PL2_RW,
4012 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4013 .resetvalue = 0,
4014 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
4015 #endif
4016 /* The only field of MDCR_EL2 that has a defined architectural reset value
4017 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4018 * don't impelment any PMU event counters, so using zero as a reset
4019 * value for MDCR_EL2 is okay
4020 */
4021 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4022 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4023 .access = PL2_RW, .resetvalue = 0,
4024 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
4025 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4026 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4027 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4028 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4029 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4030 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4031 .access = PL2_RW,
4032 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4033 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4034 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4035 .access = PL2_RW,
4036 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
4037 REGINFO_SENTINEL
4038 };
4039
4040 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4041 bool isread)
4042 {
4043 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4044 * At Secure EL1 it traps to EL3.
4045 */
4046 if (arm_current_el(env) == 3) {
4047 return CP_ACCESS_OK;
4048 }
4049 if (arm_is_secure_below_el3(env)) {
4050 return CP_ACCESS_TRAP_EL3;
4051 }
4052 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4053 if (isread) {
4054 return CP_ACCESS_OK;
4055 }
4056 return CP_ACCESS_TRAP_UNCATEGORIZED;
4057 }
4058
4059 static const ARMCPRegInfo el3_cp_reginfo[] = {
4060 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4061 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4062 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4063 .resetvalue = 0, .writefn = scr_write },
4064 { .name = "SCR", .type = ARM_CP_ALIAS,
4065 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
4066 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4067 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
4068 .writefn = scr_write },
4069 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4070 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4071 .access = PL3_RW, .resetvalue = 0,
4072 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4073 { .name = "SDER",
4074 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4075 .access = PL3_RW, .resetvalue = 0,
4076 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
4077 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4078 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4079 .writefn = vbar_write, .resetvalue = 0,
4080 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
4081 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4082 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4083 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4084 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
4085 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4086 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
4087 .access = PL3_RW,
4088 /* no .writefn needed as this can't cause an ASID change;
4089 * we must provide a .raw_writefn and .resetfn because we handle
4090 * reset and migration for the AArch32 TTBCR(S), which might be
4091 * using mask and base_mask.
4092 */
4093 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
4094 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
4095 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
4096 .type = ARM_CP_ALIAS,
4097 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4098 .access = PL3_RW,
4099 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
4100 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
4101 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4102 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
4103 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4104 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4105 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
4106 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
4107 .type = ARM_CP_ALIAS,
4108 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
4109 .access = PL3_RW,
4110 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
4111 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4112 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4113 .access = PL3_RW, .writefn = vbar_write,
4114 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4115 .resetvalue = 0 },
4116 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4117 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4118 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4119 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4120 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4121 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4122 .access = PL3_RW, .resetvalue = 0,
4123 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
4124 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4125 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4126 .access = PL3_RW, .type = ARM_CP_CONST,
4127 .resetvalue = 0 },
4128 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4129 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4130 .access = PL3_RW, .type = ARM_CP_CONST,
4131 .resetvalue = 0 },
4132 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4133 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4134 .access = PL3_RW, .type = ARM_CP_CONST,
4135 .resetvalue = 0 },
4136 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4137 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4138 .access = PL3_W, .type = ARM_CP_NO_RAW,
4139 .writefn = tlbi_aa64_alle3is_write },
4140 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4141 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4142 .access = PL3_W, .type = ARM_CP_NO_RAW,
4143 .writefn = tlbi_aa64_vae3is_write },
4144 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4145 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4146 .access = PL3_W, .type = ARM_CP_NO_RAW,
4147 .writefn = tlbi_aa64_vae3is_write },
4148 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4149 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4150 .access = PL3_W, .type = ARM_CP_NO_RAW,
4151 .writefn = tlbi_aa64_alle3_write },
4152 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4153 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4154 .access = PL3_W, .type = ARM_CP_NO_RAW,
4155 .writefn = tlbi_aa64_vae3_write },
4156 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4157 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4158 .access = PL3_W, .type = ARM_CP_NO_RAW,
4159 .writefn = tlbi_aa64_vae3_write },
4160 REGINFO_SENTINEL
4161 };
4162
4163 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4164 bool isread)
4165 {
4166 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4167 * but the AArch32 CTR has its own reginfo struct)
4168 */
4169 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
4170 return CP_ACCESS_TRAP;
4171 }
4172 return CP_ACCESS_OK;
4173 }
4174
4175 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4176 uint64_t value)
4177 {
4178 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4179 * read via a bit in OSLSR_EL1.
4180 */
4181 int oslock;
4182
4183 if (ri->state == ARM_CP_STATE_AA32) {
4184 oslock = (value == 0xC5ACCE55);
4185 } else {
4186 oslock = value & 1;
4187 }
4188
4189 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4190 }
4191
4192 static const ARMCPRegInfo debug_cp_reginfo[] = {
4193 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
4194 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4195 * unlike DBGDRAR it is never accessible from EL0.
4196 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4197 * accessor.
4198 */
4199 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
4200 .access = PL0_R, .accessfn = access_tdra,
4201 .type = ARM_CP_CONST, .resetvalue = 0 },
4202 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4203 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4204 .access = PL1_R, .accessfn = access_tdra,
4205 .type = ARM_CP_CONST, .resetvalue = 0 },
4206 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4207 .access = PL0_R, .accessfn = access_tdra,
4208 .type = ARM_CP_CONST, .resetvalue = 0 },
4209 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
4210 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4211 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4212 .access = PL1_RW, .accessfn = access_tda,
4213 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4214 .resetvalue = 0 },
4215 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4216 * We don't implement the configurable EL0 access.
4217 */
4218 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4219 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4220 .type = ARM_CP_ALIAS,
4221 .access = PL1_R, .accessfn = access_tda,
4222 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
4223 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4224 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
4225 .access = PL1_W, .type = ARM_CP_NO_RAW,
4226 .accessfn = access_tdosa,
4227 .writefn = oslar_write },
4228 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4229 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4230 .access = PL1_R, .resetvalue = 10,
4231 .accessfn = access_tdosa,
4232 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
4233 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4234 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4235 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
4236 .access = PL1_RW, .accessfn = access_tdosa,
4237 .type = ARM_CP_NOP },
4238 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4239 * implement vector catch debug events yet.
4240 */
4241 { .name = "DBGVCR",
4242 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4243 .access = PL1_RW, .accessfn = access_tda,
4244 .type = ARM_CP_NOP },
4245 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4246 * to save and restore a 32-bit guest's DBGVCR)
4247 */
4248 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4249 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4250 .access = PL2_RW, .accessfn = access_tda,
4251 .type = ARM_CP_NOP },
4252 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4253 * Channel but Linux may try to access this register. The 32-bit
4254 * alias is DBGDCCINT.
4255 */
4256 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4257 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4258 .access = PL1_RW, .accessfn = access_tda,
4259 .type = ARM_CP_NOP },
4260 REGINFO_SENTINEL
4261 };
4262
4263 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4264 /* 64 bit access versions of the (dummy) debug registers */
4265 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4266 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4267 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4268 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4269 REGINFO_SENTINEL
4270 };
4271
4272 /* Return the exception level to which SVE-disabled exceptions should
4273 * be taken, or 0 if SVE is enabled.
4274 */
4275 static int sve_exception_el(CPUARMState *env)
4276 {
4277 #ifndef CONFIG_USER_ONLY
4278 unsigned current_el = arm_current_el(env);
4279
4280 /* The CPACR.ZEN controls traps to EL1:
4281 * 0, 2 : trap EL0 and EL1 accesses
4282 * 1 : trap only EL0 accesses
4283 * 3 : trap no accesses
4284 */
4285 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
4286 default:
4287 if (current_el <= 1) {
4288 /* Trap to PL1, which might be EL1 or EL3 */
4289 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4290 return 3;
4291 }
4292 return 1;
4293 }
4294 break;
4295 case 1:
4296 if (current_el == 0) {
4297 return 1;
4298 }
4299 break;
4300 case 3:
4301 break;
4302 }
4303
4304 /* Similarly for CPACR.FPEN, after having checked ZEN. */
4305 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
4306 default:
4307 if (current_el <= 1) {
4308 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4309 return 3;
4310 }
4311 return 1;
4312 }
4313 break;
4314 case 1:
4315 if (current_el == 0) {
4316 return 1;
4317 }
4318 break;
4319 case 3:
4320 break;
4321 }
4322
4323 /* CPTR_EL2. Check both TZ and TFP. */
4324 if (current_el <= 2
4325 && (env->cp15.cptr_el[2] & (CPTR_TFP | CPTR_TZ))
4326 && !arm_is_secure_below_el3(env)) {
4327 return 2;
4328 }
4329
4330 /* CPTR_EL3. Check both EZ and TFP. */
4331 if (!(env->cp15.cptr_el[3] & CPTR_EZ)
4332 || (env->cp15.cptr_el[3] & CPTR_TFP)) {
4333 return 3;
4334 }
4335 #endif
4336 return 0;
4337 }
4338
4339 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4340 uint64_t value)
4341 {
4342 /* Bits other than [3:0] are RAZ/WI. */
4343 raw_write(env, ri, value & 0xf);
4344 }
4345
4346 static const ARMCPRegInfo zcr_el1_reginfo = {
4347 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
4348 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
4349 .access = PL1_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
4350 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
4351 .writefn = zcr_write, .raw_writefn = raw_write
4352 };
4353
4354 static const ARMCPRegInfo zcr_el2_reginfo = {
4355 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4356 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
4357 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
4358 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
4359 .writefn = zcr_write, .raw_writefn = raw_write
4360 };
4361
4362 static const ARMCPRegInfo zcr_no_el2_reginfo = {
4363 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4364 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
4365 .access = PL2_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
4366 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
4367 };
4368
4369 static const ARMCPRegInfo zcr_el3_reginfo = {
4370 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
4371 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
4372 .access = PL3_RW, .type = ARM_CP_SVE | ARM_CP_FPU,
4373 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
4374 .writefn = zcr_write, .raw_writefn = raw_write
4375 };
4376
4377 void hw_watchpoint_update(ARMCPU *cpu, int n)
4378 {
4379 CPUARMState *env = &cpu->env;
4380 vaddr len = 0;
4381 vaddr wvr = env->cp15.dbgwvr[n];
4382 uint64_t wcr = env->cp15.dbgwcr[n];
4383 int mask;
4384 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4385
4386 if (env->cpu_watchpoint[n]) {
4387 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4388 env->cpu_watchpoint[n] = NULL;
4389 }
4390
4391 if (!extract64(wcr, 0, 1)) {
4392 /* E bit clear : watchpoint disabled */
4393 return;
4394 }
4395
4396 switch (extract64(wcr, 3, 2)) {
4397 case 0:
4398 /* LSC 00 is reserved and must behave as if the wp is disabled */
4399 return;
4400 case 1:
4401 flags |= BP_MEM_READ;
4402 break;
4403 case 2:
4404 flags |= BP_MEM_WRITE;
4405 break;
4406 case 3:
4407 flags |= BP_MEM_ACCESS;
4408 break;
4409 }
4410
4411 /* Attempts to use both MASK and BAS fields simultaneously are
4412 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4413 * thus generating a watchpoint for every byte in the masked region.
4414 */
4415 mask = extract64(wcr, 24, 4);
4416 if (mask == 1 || mask == 2) {
4417 /* Reserved values of MASK; we must act as if the mask value was
4418 * some non-reserved value, or as if the watchpoint were disabled.
4419 * We choose the latter.
4420 */
4421 return;
4422 } else if (mask) {
4423 /* Watchpoint covers an aligned area up to 2GB in size */
4424 len = 1ULL << mask;
4425 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4426 * whether the watchpoint fires when the unmasked bits match; we opt
4427 * to generate the exceptions.
4428 */
4429 wvr &= ~(len - 1);
4430 } else {
4431 /* Watchpoint covers bytes defined by the byte address select bits */
4432 int bas = extract64(wcr, 5, 8);
4433 int basstart;
4434
4435 if (bas == 0) {
4436 /* This must act as if the watchpoint is disabled */
4437 return;
4438 }
4439
4440 if (extract64(wvr, 2, 1)) {
4441 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4442 * ignored, and BAS[3:0] define which bytes to watch.
4443 */
4444 bas &= 0xf;
4445 }
4446 /* The BAS bits are supposed to be programmed to indicate a contiguous
4447 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4448 * we fire for each byte in the word/doubleword addressed by the WVR.
4449 * We choose to ignore any non-zero bits after the first range of 1s.
4450 */
4451 basstart = ctz32(bas);
4452 len = cto32(bas >> basstart);
4453 wvr += basstart;
4454 }
4455
4456 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4457 &env->cpu_watchpoint[n]);
4458 }
4459
4460 void hw_watchpoint_update_all(ARMCPU *cpu)
4461 {
4462 int i;
4463 CPUARMState *env = &cpu->env;
4464
4465 /* Completely clear out existing QEMU watchpoints and our array, to
4466 * avoid possible stale entries following migration load.
4467 */
4468 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4469 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4470
4471 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4472 hw_watchpoint_update(cpu, i);
4473 }
4474 }
4475
4476 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4477 uint64_t value)
4478 {
4479 ARMCPU *cpu = arm_env_get_cpu(env);
4480 int i = ri->crm;
4481
4482 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4483 * register reads and behaves as if values written are sign extended.
4484 * Bits [1:0] are RES0.
4485 */
4486 value = sextract64(value, 0, 49) & ~3ULL;
4487
4488 raw_write(env, ri, value);
4489 hw_watchpoint_update(cpu, i);
4490 }
4491
4492 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4493 uint64_t value)
4494 {
4495 ARMCPU *cpu = arm_env_get_cpu(env);
4496 int i = ri->crm;
4497
4498 raw_write(env, ri, value);
4499 hw_watchpoint_update(cpu, i);
4500 }
4501
4502 void hw_breakpoint_update(ARMCPU *cpu, int n)
4503 {
4504 CPUARMState *env = &cpu->env;
4505 uint64_t bvr = env->cp15.dbgbvr[n];
4506 uint64_t bcr = env->cp15.dbgbcr[n];
4507 vaddr addr;
4508 int bt;
4509 int flags = BP_CPU;
4510
4511 if (env->cpu_breakpoint[n]) {
4512 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4513 env->cpu_breakpoint[n] = NULL;
4514 }
4515
4516 if (!extract64(bcr, 0, 1)) {
4517 /* E bit clear : watchpoint disabled */
4518 return;
4519 }
4520
4521 bt = extract64(bcr, 20, 4);
4522
4523 switch (bt) {
4524 case 4: /* unlinked address mismatch (reserved if AArch64) */
4525 case 5: /* linked address mismatch (reserved if AArch64) */
4526 qemu_log_mask(LOG_UNIMP,
4527 "arm: address mismatch breakpoint types not implemented");
4528 return;
4529 case 0: /* unlinked address match */
4530 case 1: /* linked address match */
4531 {
4532 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4533 * we behave as if the register was sign extended. Bits [1:0] are
4534 * RES0. The BAS field is used to allow setting breakpoints on 16
4535 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4536 * a bp will fire if the addresses covered by the bp and the addresses
4537 * covered by the insn overlap but the insn doesn't start at the
4538 * start of the bp address range. We choose to require the insn and
4539 * the bp to have the same address. The constraints on writing to
4540 * BAS enforced in dbgbcr_write mean we have only four cases:
4541 * 0b0000 => no breakpoint
4542 * 0b0011 => breakpoint on addr
4543 * 0b1100 => breakpoint on addr + 2
4544 * 0b1111 => breakpoint on addr
4545 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4546 */
4547 int bas = extract64(bcr, 5, 4);
4548 addr = sextract64(bvr, 0, 49) & ~3ULL;
4549 if (bas == 0) {
4550 return;
4551 }
4552 if (bas == 0xc) {
4553 addr += 2;
4554 }
4555 break;
4556 }
4557 case 2: /* unlinked context ID match */
4558 case 8: /* unlinked VMID match (reserved if no EL2) */
4559 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4560 qemu_log_mask(LOG_UNIMP,
4561 "arm: unlinked context breakpoint types not implemented");
4562 return;
4563 case 9: /* linked VMID match (reserved if no EL2) */
4564 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4565 case 3: /* linked context ID match */
4566 default:
4567 /* We must generate no events for Linked context matches (unless
4568 * they are linked to by some other bp/wp, which is handled in
4569 * updates for the linking bp/wp). We choose to also generate no events
4570 * for reserved values.
4571 */
4572 return;
4573 }
4574
4575 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4576 }
4577
4578 void hw_breakpoint_update_all(ARMCPU *cpu)
4579 {
4580 int i;
4581 CPUARMState *env = &cpu->env;
4582
4583 /* Completely clear out existing QEMU breakpoints and our array, to
4584 * avoid possible stale entries following migration load.
4585 */
4586 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4587 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4588
4589 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4590 hw_breakpoint_update(cpu, i);
4591 }
4592 }
4593
4594 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4595 uint64_t value)
4596 {
4597 ARMCPU *cpu = arm_env_get_cpu(env);
4598 int i = ri->crm;
4599
4600 raw_write(env, ri, value);
4601 hw_breakpoint_update(cpu, i);
4602 }
4603
4604 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4605 uint64_t value)
4606 {
4607 ARMCPU *cpu = arm_env_get_cpu(env);
4608 int i = ri->crm;
4609
4610 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4611 * copy of BAS[0].
4612 */
4613 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4614 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4615
4616 raw_write(env, ri, value);
4617 hw_breakpoint_update(cpu, i);
4618 }
4619
4620 static void define_debug_regs(ARMCPU *cpu)
4621 {
4622 /* Define v7 and v8 architectural debug registers.
4623 * These are just dummy implementations for now.
4624 */
4625 int i;
4626 int wrps, brps, ctx_cmps;
4627 ARMCPRegInfo dbgdidr = {
4628 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
4629 .access = PL0_R, .accessfn = access_tda,
4630 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
4631 };
4632
4633 /* Note that all these register fields hold "number of Xs minus 1". */
4634 brps = extract32(cpu->dbgdidr, 24, 4);
4635 wrps = extract32(cpu->dbgdidr, 28, 4);
4636 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4637
4638 assert(ctx_cmps <= brps);
4639
4640 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4641 * of the debug registers such as number of breakpoints;
4642 * check that if they both exist then they agree.
4643 */
4644 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4645 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4646 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
4647 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
4648 }
4649
4650 define_one_arm_cp_reg(cpu, &dbgdidr);
4651 define_arm_cp_regs(cpu, debug_cp_reginfo);
4652
4653 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4654 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4655 }
4656
4657 for (i = 0; i < brps + 1; i++) {
4658 ARMCPRegInfo dbgregs[] = {
4659 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4660 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
4661 .access = PL1_RW, .accessfn = access_tda,
4662 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4663 .writefn = dbgbvr_write, .raw_writefn = raw_write
4664 },
4665 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4666 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
4667 .access = PL1_RW, .accessfn = access_tda,
4668 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4669 .writefn = dbgbcr_write, .raw_writefn = raw_write
4670 },
4671 REGINFO_SENTINEL
4672 };
4673 define_arm_cp_regs(cpu, dbgregs);
4674 }
4675
4676 for (i = 0; i < wrps + 1; i++) {
4677 ARMCPRegInfo dbgregs[] = {
4678 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4679 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
4680 .access = PL1_RW, .accessfn = access_tda,
4681 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4682 .writefn = dbgwvr_write, .raw_writefn = raw_write
4683 },
4684 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4685 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
4686 .access = PL1_RW, .accessfn = access_tda,
4687 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4688 .writefn = dbgwcr_write, .raw_writefn = raw_write
4689 },
4690 REGINFO_SENTINEL
4691 };
4692 define_arm_cp_regs(cpu, dbgregs);
4693 }
4694 }
4695
4696 /* We don't know until after realize whether there's a GICv3
4697 * attached, and that is what registers the gicv3 sysregs.
4698 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4699 * at runtime.
4700 */
4701 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4702 {
4703 ARMCPU *cpu = arm_env_get_cpu(env);
4704 uint64_t pfr1 = cpu->id_pfr1;
4705
4706 if (env->gicv3state) {
4707 pfr1 |= 1 << 28;
4708 }
4709 return pfr1;
4710 }
4711
4712 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4713 {
4714 ARMCPU *cpu = arm_env_get_cpu(env);
4715 uint64_t pfr0 = cpu->id_aa64pfr0;
4716
4717 if (env->gicv3state) {
4718 pfr0 |= 1 << 24;
4719 }
4720 return pfr0;
4721 }
4722
4723 void register_cp_regs_for_features(ARMCPU *cpu)
4724 {
4725 /* Register all the coprocessor registers based on feature bits */
4726 CPUARMState *env = &cpu->env;
4727 if (arm_feature(env, ARM_FEATURE_M)) {
4728 /* M profile has no coprocessor registers */
4729 return;
4730 }
4731
4732 define_arm_cp_regs(cpu, cp_reginfo);
4733 if (!arm_feature(env, ARM_FEATURE_V8)) {
4734 /* Must go early as it is full of wildcards that may be
4735 * overridden by later definitions.
4736 */
4737 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4738 }
4739
4740 if (arm_feature(env, ARM_FEATURE_V6)) {
4741 /* The ID registers all have impdef reset values */
4742 ARMCPRegInfo v6_idregs[] = {
4743 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4745 .access = PL1_R, .type = ARM_CP_CONST,
4746 .resetvalue = cpu->id_pfr0 },
4747 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4748 * the value of the GIC field until after we define these regs.
4749 */
4750 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4751 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4752 .access = PL1_R, .type = ARM_CP_NO_RAW,
4753 .readfn = id_pfr1_read,
4754 .writefn = arm_cp_write_ignore },
4755 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4756 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4757 .access = PL1_R, .type = ARM_CP_CONST,
4758 .resetvalue = cpu->id_dfr0 },
4759 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4760 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4761 .access = PL1_R, .type = ARM_CP_CONST,
4762 .resetvalue = cpu->id_afr0 },
4763 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4765 .access = PL1_R, .type = ARM_CP_CONST,
4766 .resetvalue = cpu->id_mmfr0 },
4767 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4769 .access = PL1_R, .type = ARM_CP_CONST,
4770 .resetvalue = cpu->id_mmfr1 },
4771 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4773 .access = PL1_R, .type = ARM_CP_CONST,
4774 .resetvalue = cpu->id_mmfr2 },
4775 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4777 .access = PL1_R, .type = ARM_CP_CONST,
4778 .resetvalue = cpu->id_mmfr3 },
4779 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4780 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4781 .access = PL1_R, .type = ARM_CP_CONST,
4782 .resetvalue = cpu->id_isar0 },
4783 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4784 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4785 .access = PL1_R, .type = ARM_CP_CONST,
4786 .resetvalue = cpu->id_isar1 },
4787 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4789 .access = PL1_R, .type = ARM_CP_CONST,
4790 .resetvalue = cpu->id_isar2 },
4791 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4792 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4793 .access = PL1_R, .type = ARM_CP_CONST,
4794 .resetvalue = cpu->id_isar3 },
4795 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4797 .access = PL1_R, .type = ARM_CP_CONST,
4798 .resetvalue = cpu->id_isar4 },
4799 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4801 .access = PL1_R, .type = ARM_CP_CONST,
4802 .resetvalue = cpu->id_isar5 },
4803 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4804 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4805 .access = PL1_R, .type = ARM_CP_CONST,
4806 .resetvalue = cpu->id_mmfr4 },
4807 /* 7 is as yet unallocated and must RAZ */
4808 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4809 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4810 .access = PL1_R, .type = ARM_CP_CONST,
4811 .resetvalue = 0 },
4812 REGINFO_SENTINEL
4813 };
4814 define_arm_cp_regs(cpu, v6_idregs);
4815 define_arm_cp_regs(cpu, v6_cp_reginfo);
4816 } else {
4817 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4818 }
4819 if (arm_feature(env, ARM_FEATURE_V6K)) {
4820 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4821 }
4822 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4823 !arm_feature(env, ARM_FEATURE_PMSA)) {
4824 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4825 }
4826 if (arm_feature(env, ARM_FEATURE_V7)) {
4827 /* v7 performance monitor control register: same implementor
4828 * field as main ID register, and we implement only the cycle
4829 * count register.
4830 */
4831 #ifndef CONFIG_USER_ONLY
4832 ARMCPRegInfo pmcr = {
4833 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4834 .access = PL0_RW,
4835 .type = ARM_CP_IO | ARM_CP_ALIAS,
4836 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4837 .accessfn = pmreg_access, .writefn = pmcr_write,
4838 .raw_writefn = raw_write,
4839 };
4840 ARMCPRegInfo pmcr64 = {
4841 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4842 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4843 .access = PL0_RW, .accessfn = pmreg_access,
4844 .type = ARM_CP_IO,
4845 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4846 .resetvalue = cpu->midr & 0xff000000,
4847 .writefn = pmcr_write, .raw_writefn = raw_write,
4848 };
4849 define_one_arm_cp_reg(cpu, &pmcr);
4850 define_one_arm_cp_reg(cpu, &pmcr64);
4851 #endif
4852 ARMCPRegInfo clidr = {
4853 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4854 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4855 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4856 };
4857 define_one_arm_cp_reg(cpu, &clidr);
4858 define_arm_cp_regs(cpu, v7_cp_reginfo);
4859 define_debug_regs(cpu);
4860 } else {
4861 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4862 }
4863 if (arm_feature(env, ARM_FEATURE_V8)) {
4864 /* AArch64 ID registers, which all have impdef reset values.
4865 * Note that within the ID register ranges the unused slots
4866 * must all RAZ, not UNDEF; future architecture versions may
4867 * define new registers here.
4868 */
4869 ARMCPRegInfo v8_idregs[] = {
4870 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4871 * know the right value for the GIC field until after we
4872 * define these regs.
4873 */
4874 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4875 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4876 .access = PL1_R, .type = ARM_CP_NO_RAW,
4877 .readfn = id_aa64pfr0_read,
4878 .writefn = arm_cp_write_ignore },
4879 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4880 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4881 .access = PL1_R, .type = ARM_CP_CONST,
4882 .resetvalue = cpu->id_aa64pfr1},
4883 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4885 .access = PL1_R, .type = ARM_CP_CONST,
4886 .resetvalue = 0 },
4887 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4889 .access = PL1_R, .type = ARM_CP_CONST,
4890 .resetvalue = 0 },
4891 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4892 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4893 .access = PL1_R, .type = ARM_CP_CONST,
4894 .resetvalue = 0 },
4895 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4896 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4897 .access = PL1_R, .type = ARM_CP_CONST,
4898 .resetvalue = 0 },
4899 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4900 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4901 .access = PL1_R, .type = ARM_CP_CONST,
4902 .resetvalue = 0 },
4903 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4904 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4905 .access = PL1_R, .type = ARM_CP_CONST,
4906 .resetvalue = 0 },
4907 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4909 .access = PL1_R, .type = ARM_CP_CONST,
4910 .resetvalue = cpu->id_aa64dfr0 },
4911 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4912 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4913 .access = PL1_R, .type = ARM_CP_CONST,
4914 .resetvalue = cpu->id_aa64dfr1 },
4915 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4916 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4917 .access = PL1_R, .type = ARM_CP_CONST,
4918 .resetvalue = 0 },
4919 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4920 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4921 .access = PL1_R, .type = ARM_CP_CONST,
4922 .resetvalue = 0 },
4923 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4924 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4925 .access = PL1_R, .type = ARM_CP_CONST,
4926 .resetvalue = cpu->id_aa64afr0 },
4927 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4928 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4929 .access = PL1_R, .type = ARM_CP_CONST,
4930 .resetvalue = cpu->id_aa64afr1 },
4931 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4932 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4933 .access = PL1_R, .type = ARM_CP_CONST,
4934 .resetvalue = 0 },
4935 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4936 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4937 .access = PL1_R, .type = ARM_CP_CONST,
4938 .resetvalue = 0 },
4939 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4940 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4941 .access = PL1_R, .type = ARM_CP_CONST,
4942 .resetvalue = cpu->id_aa64isar0 },
4943 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4945 .access = PL1_R, .type = ARM_CP_CONST,
4946 .resetvalue = cpu->id_aa64isar1 },
4947 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4948 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4949 .access = PL1_R, .type = ARM_CP_CONST,
4950 .resetvalue = 0 },
4951 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4952 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4953 .access = PL1_R, .type = ARM_CP_CONST,
4954 .resetvalue = 0 },
4955 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4956 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4957 .access = PL1_R, .type = ARM_CP_CONST,
4958 .resetvalue = 0 },
4959 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4961 .access = PL1_R, .type = ARM_CP_CONST,
4962 .resetvalue = 0 },
4963 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4965 .access = PL1_R, .type = ARM_CP_CONST,
4966 .resetvalue = 0 },
4967 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4968 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4969 .access = PL1_R, .type = ARM_CP_CONST,
4970 .resetvalue = 0 },
4971 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4973 .access = PL1_R, .type = ARM_CP_CONST,
4974 .resetvalue = cpu->id_aa64mmfr0 },
4975 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4977 .access = PL1_R, .type = ARM_CP_CONST,
4978 .resetvalue = cpu->id_aa64mmfr1 },
4979 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4981 .access = PL1_R, .type = ARM_CP_CONST,
4982 .resetvalue = 0 },
4983 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4985 .access = PL1_R, .type = ARM_CP_CONST,
4986 .resetvalue = 0 },
4987 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4988 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4989 .access = PL1_R, .type = ARM_CP_CONST,
4990 .resetvalue = 0 },
4991 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4993 .access = PL1_R, .type = ARM_CP_CONST,
4994 .resetvalue = 0 },
4995 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4997 .access = PL1_R, .type = ARM_CP_CONST,
4998 .resetvalue = 0 },
4999 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
5001 .access = PL1_R, .type = ARM_CP_CONST,
5002 .resetvalue = 0 },
5003 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
5004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
5005 .access = PL1_R, .type = ARM_CP_CONST,
5006 .resetvalue = cpu->mvfr0 },
5007 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
5008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
5009 .access = PL1_R, .type = ARM_CP_CONST,
5010 .resetvalue = cpu->mvfr1 },
5011 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
5012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
5013 .access = PL1_R, .type = ARM_CP_CONST,
5014 .resetvalue = cpu->mvfr2 },
5015 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
5017 .access = PL1_R, .type = ARM_CP_CONST,
5018 .resetvalue = 0 },
5019 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
5021 .access = PL1_R, .type = ARM_CP_CONST,
5022 .resetvalue = 0 },
5023 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
5025 .access = PL1_R, .type = ARM_CP_CONST,
5026 .resetvalue = 0 },
5027 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5028 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
5029 .access = PL1_R, .type = ARM_CP_CONST,
5030 .resetvalue = 0 },
5031 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
5033 .access = PL1_R, .type = ARM_CP_CONST,
5034 .resetvalue = 0 },
5035 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
5036 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
5037 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5038 .resetvalue = cpu->pmceid0 },
5039 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
5040 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
5041 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5042 .resetvalue = cpu->pmceid0 },
5043 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
5044 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
5045 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5046 .resetvalue = cpu->pmceid1 },
5047 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
5048 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
5049 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5050 .resetvalue = cpu->pmceid1 },
5051 REGINFO_SENTINEL
5052 };
5053 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
5054 if (!arm_feature(env, ARM_FEATURE_EL3) &&
5055 !arm_feature(env, ARM_FEATURE_EL2)) {
5056 ARMCPRegInfo rvbar = {
5057 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
5058 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5059 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
5060 };
5061 define_one_arm_cp_reg(cpu, &rvbar);
5062 }
5063 define_arm_cp_regs(cpu, v8_idregs);
5064 define_arm_cp_regs(cpu, v8_cp_reginfo);
5065 }
5066 if (arm_feature(env, ARM_FEATURE_EL2)) {
5067 uint64_t vmpidr_def = mpidr_read_val(env);
5068 ARMCPRegInfo vpidr_regs[] = {
5069 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
5070 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5071 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5072 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
5073 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
5074 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
5075 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5076 .access = PL2_RW, .resetvalue = cpu->midr,
5077 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
5078 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
5079 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5080 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5081 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
5082 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
5083 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
5084 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5085 .access = PL2_RW,
5086 .resetvalue = vmpidr_def,
5087 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
5088 REGINFO_SENTINEL
5089 };
5090 define_arm_cp_regs(cpu, vpidr_regs);
5091 define_arm_cp_regs(cpu, el2_cp_reginfo);
5092 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
5093 if (!arm_feature(env, ARM_FEATURE_EL3)) {
5094 ARMCPRegInfo rvbar = {
5095 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
5096 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
5097 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
5098 };
5099 define_one_arm_cp_reg(cpu, &rvbar);
5100 }
5101 } else {
5102 /* If EL2 is missing but higher ELs are enabled, we need to
5103 * register the no_el2 reginfos.
5104 */
5105 if (arm_feature(env, ARM_FEATURE_EL3)) {
5106 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
5107 * of MIDR_EL1 and MPIDR_EL1.
5108 */
5109 ARMCPRegInfo vpidr_regs[] = {
5110 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5111 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5112 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5113 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
5114 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
5115 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5116 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5117 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5118 .type = ARM_CP_NO_RAW,
5119 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
5120 REGINFO_SENTINEL
5121 };
5122 define_arm_cp_regs(cpu, vpidr_regs);
5123 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
5124 }
5125 }
5126 if (arm_feature(env, ARM_FEATURE_EL3)) {
5127 define_arm_cp_regs(cpu, el3_cp_reginfo);
5128 ARMCPRegInfo el3_regs[] = {
5129 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
5130 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
5131 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
5132 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
5133 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
5134 .access = PL3_RW,
5135 .raw_writefn = raw_write, .writefn = sctlr_write,
5136 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
5137 .resetvalue = cpu->reset_sctlr },
5138 REGINFO_SENTINEL
5139 };
5140
5141 define_arm_cp_regs(cpu, el3_regs);
5142 }
5143 /* The behaviour of NSACR is sufficiently various that we don't
5144 * try to describe it in a single reginfo:
5145 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5146 * reads as constant 0xc00 from NS EL1 and NS EL2
5147 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5148 * if v7 without EL3, register doesn't exist
5149 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5150 */
5151 if (arm_feature(env, ARM_FEATURE_EL3)) {
5152 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5153 ARMCPRegInfo nsacr = {
5154 .name = "NSACR", .type = ARM_CP_CONST,
5155 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5156 .access = PL1_RW, .accessfn = nsacr_access,
5157 .resetvalue = 0xc00
5158 };
5159 define_one_arm_cp_reg(cpu, &nsacr);
5160 } else {
5161 ARMCPRegInfo nsacr = {
5162 .name = "NSACR",
5163 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5164 .access = PL3_RW | PL1_R,
5165 .resetvalue = 0,
5166 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5167 };
5168 define_one_arm_cp_reg(cpu, &nsacr);
5169 }
5170 } else {
5171 if (arm_feature(env, ARM_FEATURE_V8)) {
5172 ARMCPRegInfo nsacr = {
5173 .name = "NSACR", .type = ARM_CP_CONST,
5174 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5175 .access = PL1_R,
5176 .resetvalue = 0xc00
5177 };
5178 define_one_arm_cp_reg(cpu, &nsacr);
5179 }
5180 }
5181
5182 if (arm_feature(env, ARM_FEATURE_PMSA)) {
5183 if (arm_feature(env, ARM_FEATURE_V6)) {
5184 /* PMSAv6 not implemented */
5185 assert(arm_feature(env, ARM_FEATURE_V7));
5186 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5187 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5188 } else {
5189 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5190 }
5191 } else {
5192 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5193 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5194 }
5195 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5196 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5197 }
5198 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5199 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5200 }
5201 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5202 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5203 }
5204 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5205 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5206 }
5207 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5208 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5209 }
5210 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5211 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5212 }
5213 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5214 define_arm_cp_regs(cpu, omap_cp_reginfo);
5215 }
5216 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5217 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5218 }
5219 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5220 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5221 }
5222 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5223 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5224 }
5225 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5226 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5227 }
5228 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5229 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5230 * be read-only (ie write causes UNDEF exception).
5231 */
5232 {
5233 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5234 /* Pre-v8 MIDR space.
5235 * Note that the MIDR isn't a simple constant register because
5236 * of the TI925 behaviour where writes to another register can
5237 * cause the MIDR value to change.
5238 *
5239 * Unimplemented registers in the c15 0 0 0 space default to
5240 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5241 * and friends override accordingly.
5242 */
5243 { .name = "MIDR",
5244 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
5245 .access = PL1_R, .resetvalue = cpu->midr,
5246 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
5247 .readfn = midr_read,
5248 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5249 .type = ARM_CP_OVERRIDE },
5250 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5251 { .name = "DUMMY",
5252 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5253 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5254 { .name = "DUMMY",
5255 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5256 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5257 { .name = "DUMMY",
5258 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5259 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5260 { .name = "DUMMY",
5261 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5262 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5263 { .name = "DUMMY",
5264 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5265 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5266 REGINFO_SENTINEL
5267 };
5268 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
5269 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5270 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
5271 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5272 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5273 .readfn = midr_read },
5274 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5275 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5276 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5277 .access = PL1_R, .resetvalue = cpu->midr },
5278 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5279 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5280 .access = PL1_R, .resetvalue = cpu->midr },
5281 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5282 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
5283 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
5284 REGINFO_SENTINEL
5285 };
5286 ARMCPRegInfo id_cp_reginfo[] = {
5287 /* These are common to v8 and pre-v8 */
5288 { .name = "CTR",
5289 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5290 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5291 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5292 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5293 .access = PL0_R, .accessfn = ctr_el0_access,
5294 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5295 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5296 { .name = "TCMTR",
5297 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5298 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5299 REGINFO_SENTINEL
5300 };
5301 /* TLBTR is specific to VMSA */
5302 ARMCPRegInfo id_tlbtr_reginfo = {
5303 .name = "TLBTR",
5304 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5305 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5306 };
5307 /* MPUIR is specific to PMSA V6+ */
5308 ARMCPRegInfo id_mpuir_reginfo = {
5309 .name = "MPUIR",
5310 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5311 .access = PL1_R, .type = ARM_CP_CONST,
5312 .resetvalue = cpu->pmsav7_dregion << 8
5313 };
5314 ARMCPRegInfo crn0_wi_reginfo = {
5315 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5316 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5317 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5318 };
5319 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5320 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5321 ARMCPRegInfo *r;
5322 /* Register the blanket "writes ignored" value first to cover the
5323 * whole space. Then update the specific ID registers to allow write
5324 * access, so that they ignore writes rather than causing them to
5325 * UNDEF.
5326 */
5327 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5328 for (r = id_pre_v8_midr_cp_reginfo;
5329 r->type != ARM_CP_SENTINEL; r++) {
5330 r->access = PL1_RW;
5331 }
5332 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5333 r->access = PL1_RW;
5334 }
5335 id_tlbtr_reginfo.access = PL1_RW;
5336 id_tlbtr_reginfo.access = PL1_RW;
5337 }
5338 if (arm_feature(env, ARM_FEATURE_V8)) {
5339 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5340 } else {
5341 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5342 }
5343 define_arm_cp_regs(cpu, id_cp_reginfo);
5344 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
5345 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
5346 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5347 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
5348 }
5349 }
5350
5351 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5352 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5353 }
5354
5355 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
5356 ARMCPRegInfo auxcr_reginfo[] = {
5357 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5358 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5359 .access = PL1_RW, .type = ARM_CP_CONST,
5360 .resetvalue = cpu->reset_auxcr },
5361 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5362 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5363 .access = PL2_RW, .type = ARM_CP_CONST,
5364 .resetvalue = 0 },
5365 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5366 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5367 .access = PL3_RW, .type = ARM_CP_CONST,
5368 .resetvalue = 0 },
5369 REGINFO_SENTINEL
5370 };
5371 define_arm_cp_regs(cpu, auxcr_reginfo);
5372 }
5373
5374 if (arm_feature(env, ARM_FEATURE_CBAR)) {
5375 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5376 /* 32 bit view is [31:18] 0...0 [43:32]. */
5377 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5378 | extract64(cpu->reset_cbar, 32, 12);
5379 ARMCPRegInfo cbar_reginfo[] = {
5380 { .name = "CBAR",
5381 .type = ARM_CP_CONST,
5382 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5383 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5384 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5385 .type = ARM_CP_CONST,
5386 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5387 .access = PL1_R, .resetvalue = cbar32 },
5388 REGINFO_SENTINEL
5389 };
5390 /* We don't implement a r/w 64 bit CBAR currently */
5391 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5392 define_arm_cp_regs(cpu, cbar_reginfo);
5393 } else {
5394 ARMCPRegInfo cbar = {
5395 .name = "CBAR",
5396 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5397 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5398 .fieldoffset = offsetof(CPUARMState,
5399 cp15.c15_config_base_address)
5400 };
5401 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5402 cbar.access = PL1_R;
5403 cbar.fieldoffset = 0;
5404 cbar.type = ARM_CP_CONST;
5405 }
5406 define_one_arm_cp_reg(cpu, &cbar);
5407 }
5408 }
5409
5410 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5411 ARMCPRegInfo vbar_cp_reginfo[] = {
5412 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5413 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5414 .access = PL1_RW, .writefn = vbar_write,
5415 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5416 offsetof(CPUARMState, cp15.vbar_ns) },
5417 .resetvalue = 0 },
5418 REGINFO_SENTINEL
5419 };
5420 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5421 }
5422
5423 /* Generic registers whose values depend on the implementation */
5424 {
5425 ARMCPRegInfo sctlr = {
5426 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
5427 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5428 .access = PL1_RW,
5429 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5430 offsetof(CPUARMState, cp15.sctlr_ns) },
5431 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5432 .raw_writefn = raw_write,
5433 };
5434 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5435 /* Normally we would always end the TB on an SCTLR write, but Linux
5436 * arch/arm/mach-pxa/sleep.S expects two instructions following
5437 * an MMU enable to execute from cache. Imitate this behaviour.
5438 */
5439 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5440 }
5441 define_one_arm_cp_reg(cpu, &sctlr);
5442 }
5443
5444 if (arm_feature(env, ARM_FEATURE_SVE)) {
5445 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
5446 if (arm_feature(env, ARM_FEATURE_EL2)) {
5447 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
5448 } else {
5449 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
5450 }
5451 if (arm_feature(env, ARM_FEATURE_EL3)) {
5452 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
5453 }
5454 }
5455 }
5456
5457 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5458 {
5459 CPUState *cs = CPU(cpu);
5460 CPUARMState *env = &cpu->env;
5461
5462 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5463 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5464 aarch64_fpu_gdb_set_reg,
5465 34, "aarch64-fpu.xml", 0);
5466 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
5467 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5468 51, "arm-neon.xml", 0);
5469 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
5470 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5471 35, "arm-vfp3.xml", 0);
5472 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
5473 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5474 19, "arm-vfp.xml", 0);
5475 }
5476 }
5477
5478 /* Sort alphabetically by type name, except for "any". */
5479 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5480 {
5481 ObjectClass *class_a = (ObjectClass *)a;
5482 ObjectClass *class_b = (ObjectClass *)b;
5483 const char *name_a, *name_b;
5484
5485 name_a = object_class_get_name(class_a);
5486 name_b = object_class_get_name(class_b);
5487 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
5488 return 1;
5489 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
5490 return -1;
5491 } else {
5492 return strcmp(name_a, name_b);
5493 }
5494 }
5495
5496 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
5497 {
5498 ObjectClass *oc = data;
5499 CPUListState *s = user_data;
5500 const char *typename;
5501 char *name;
5502
5503 typename = object_class_get_name(oc);
5504 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
5505 (*s->cpu_fprintf)(s->file, " %s\n",
5506 name);
5507 g_free(name);
5508 }
5509
5510 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5511 {
5512 CPUListState s = {
5513 .file = f,
5514 .cpu_fprintf = cpu_fprintf,
5515 };
5516 GSList *list;
5517
5518 list = object_class_get_list(TYPE_ARM_CPU, false);
5519 list = g_slist_sort(list, arm_cpu_list_compare);
5520 (*cpu_fprintf)(f, "Available CPUs:\n");
5521 g_slist_foreach(list, arm_cpu_list_entry, &s);
5522 g_slist_free(list);
5523 #ifdef CONFIG_KVM
5524 /* The 'host' CPU type is dynamically registered only if KVM is
5525 * enabled, so we have to special-case it here:
5526 */
5527 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5528 #endif
5529 }
5530
5531 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5532 {
5533 ObjectClass *oc = data;
5534 CpuDefinitionInfoList **cpu_list = user_data;
5535 CpuDefinitionInfoList *entry;
5536 CpuDefinitionInfo *info;
5537 const char *typename;
5538
5539 typename = object_class_get_name(oc);
5540 info = g_malloc0(sizeof(*info));
5541 info->name = g_strndup(typename,
5542 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5543 info->q_typename = g_strdup(typename);
5544
5545 entry = g_malloc0(sizeof(*entry));
5546 entry->value = info;
5547 entry->next = *cpu_list;
5548 *cpu_list = entry;
5549 }
5550
5551 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5552 {
5553 CpuDefinitionInfoList *cpu_list = NULL;
5554 GSList *list;
5555
5556 list = object_class_get_list(TYPE_ARM_CPU, false);
5557 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5558 g_slist_free(list);
5559
5560 return cpu_list;
5561 }
5562
5563 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
5564 void *opaque, int state, int secstate,
5565 int crm, int opc1, int opc2)
5566 {
5567 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5568 * add a single reginfo struct to the hash table.
5569 */
5570 uint32_t *key = g_new(uint32_t, 1);
5571 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5572 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
5573 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5574
5575 /* Reset the secure state to the specific incoming state. This is
5576 * necessary as the register may have been defined with both states.
5577 */
5578 r2->secure = secstate;
5579
5580 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5581 /* Register is banked (using both entries in array).
5582 * Overwriting fieldoffset as the array is only used to define
5583 * banked registers but later only fieldoffset is used.
5584 */
5585 r2->fieldoffset = r->bank_fieldoffsets[ns];
5586 }
5587
5588 if (state == ARM_CP_STATE_AA32) {
5589 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5590 /* If the register is banked then we don't need to migrate or
5591 * reset the 32-bit instance in certain cases:
5592 *
5593 * 1) If the register has both 32-bit and 64-bit instances then we
5594 * can count on the 64-bit instance taking care of the
5595 * non-secure bank.
5596 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5597 * taking care of the secure bank. This requires that separate
5598 * 32 and 64-bit definitions are provided.
5599 */
5600 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5601 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
5602 r2->type |= ARM_CP_ALIAS;
5603 }
5604 } else if ((secstate != r->secure) && !ns) {
5605 /* The register is not banked so we only want to allow migration of
5606 * the non-secure instance.
5607 */
5608 r2->type |= ARM_CP_ALIAS;
5609 }
5610
5611 if (r->state == ARM_CP_STATE_BOTH) {
5612 /* We assume it is a cp15 register if the .cp field is left unset.
5613 */
5614 if (r2->cp == 0) {
5615 r2->cp = 15;
5616 }
5617
5618 #ifdef HOST_WORDS_BIGENDIAN
5619 if (r2->fieldoffset) {
5620 r2->fieldoffset += sizeof(uint32_t);
5621 }
5622 #endif
5623 }
5624 }
5625 if (state == ARM_CP_STATE_AA64) {
5626 /* To allow abbreviation of ARMCPRegInfo
5627 * definitions, we treat cp == 0 as equivalent to
5628 * the value for "standard guest-visible sysreg".
5629 * STATE_BOTH definitions are also always "standard
5630 * sysreg" in their AArch64 view (the .cp value may
5631 * be non-zero for the benefit of the AArch32 view).
5632 */
5633 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
5634 r2->cp = CP_REG_ARM64_SYSREG_CP;
5635 }
5636 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5637 r2->opc0, opc1, opc2);
5638 } else {
5639 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
5640 }
5641 if (opaque) {
5642 r2->opaque = opaque;
5643 }
5644 /* reginfo passed to helpers is correct for the actual access,
5645 * and is never ARM_CP_STATE_BOTH:
5646 */
5647 r2->state = state;
5648 /* Make sure reginfo passed to helpers for wildcarded regs
5649 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5650 */
5651 r2->crm = crm;
5652 r2->opc1 = opc1;
5653 r2->opc2 = opc2;
5654 /* By convention, for wildcarded registers only the first
5655 * entry is used for migration; the others are marked as
5656 * ALIAS so we don't try to transfer the register
5657 * multiple times. Special registers (ie NOP/WFI) are
5658 * never migratable and not even raw-accessible.
5659 */
5660 if ((r->type & ARM_CP_SPECIAL)) {
5661 r2->type |= ARM_CP_NO_RAW;
5662 }
5663 if (((r->crm == CP_ANY) && crm != 0) ||
5664 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5665 ((r->opc2 == CP_ANY) && opc2 != 0)) {
5666 r2->type |= ARM_CP_ALIAS;
5667 }
5668
5669 /* Check that raw accesses are either forbidden or handled. Note that
5670 * we can't assert this earlier because the setup of fieldoffset for
5671 * banked registers has to be done first.
5672 */
5673 if (!(r2->type & ARM_CP_NO_RAW)) {
5674 assert(!raw_accessors_invalid(r2));
5675 }
5676
5677 /* Overriding of an existing definition must be explicitly
5678 * requested.
5679 */
5680 if (!(r->type & ARM_CP_OVERRIDE)) {
5681 ARMCPRegInfo *oldreg;
5682 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5683 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5684 fprintf(stderr, "Register redefined: cp=%d %d bit "
5685 "crn=%d crm=%d opc1=%d opc2=%d, "
5686 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5687 r2->crn, r2->crm, r2->opc1, r2->opc2,
5688 oldreg->name, r2->name);
5689 g_assert_not_reached();
5690 }
5691 }
5692 g_hash_table_insert(cpu->cp_regs, key, r2);
5693 }
5694
5695
5696 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5697 const ARMCPRegInfo *r, void *opaque)
5698 {
5699 /* Define implementations of coprocessor registers.
5700 * We store these in a hashtable because typically
5701 * there are less than 150 registers in a space which
5702 * is 16*16*16*8*8 = 262144 in size.
5703 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5704 * If a register is defined twice then the second definition is
5705 * used, so this can be used to define some generic registers and
5706 * then override them with implementation specific variations.
5707 * At least one of the original and the second definition should
5708 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5709 * against accidental use.
5710 *
5711 * The state field defines whether the register is to be
5712 * visible in the AArch32 or AArch64 execution state. If the
5713 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5714 * reginfo structure for the AArch32 view, which sees the lower
5715 * 32 bits of the 64 bit register.
5716 *
5717 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5718 * be wildcarded. AArch64 registers are always considered to be 64
5719 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5720 * the register, if any.
5721 */
5722 int crm, opc1, opc2, state;
5723 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5724 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5725 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5726 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5727 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5728 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5729 /* 64 bit registers have only CRm and Opc1 fields */
5730 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
5731 /* op0 only exists in the AArch64 encodings */
5732 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5733 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5734 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5735 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5736 * encodes a minimum access level for the register. We roll this
5737 * runtime check into our general permission check code, so check
5738 * here that the reginfo's specified permissions are strict enough
5739 * to encompass the generic architectural permission check.
5740 */
5741 if (r->state != ARM_CP_STATE_AA32) {
5742 int mask = 0;
5743 switch (r->opc1) {
5744 case 0: case 1: case 2:
5745 /* min_EL EL1 */
5746 mask = PL1_RW;
5747 break;
5748 case 3:
5749 /* min_EL EL0 */
5750 mask = PL0_RW;
5751 break;
5752 case 4:
5753 /* min_EL EL2 */
5754 mask = PL2_RW;
5755 break;
5756 case 5:
5757 /* unallocated encoding, so not possible */
5758 assert(false);
5759 break;
5760 case 6:
5761 /* min_EL EL3 */
5762 mask = PL3_RW;
5763 break;
5764 case 7:
5765 /* min_EL EL1, secure mode only (we don't check the latter) */
5766 mask = PL1_RW;
5767 break;
5768 default:
5769 /* broken reginfo with out-of-range opc1 */
5770 assert(false);
5771 break;
5772 }
5773 /* assert our permissions are not too lax (stricter is fine) */
5774 assert((r->access & ~mask) == 0);
5775 }
5776
5777 /* Check that the register definition has enough info to handle
5778 * reads and writes if they are permitted.
5779 */
5780 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5781 if (r->access & PL3_R) {
5782 assert((r->fieldoffset ||
5783 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5784 r->readfn);
5785 }
5786 if (r->access & PL3_W) {
5787 assert((r->fieldoffset ||
5788 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5789 r->writefn);
5790 }
5791 }
5792 /* Bad type field probably means missing sentinel at end of reg list */
5793 assert(cptype_valid(r->type));
5794 for (crm = crmmin; crm <= crmmax; crm++) {
5795 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5796 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
5797 for (state = ARM_CP_STATE_AA32;
5798 state <= ARM_CP_STATE_AA64; state++) {
5799 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5800 continue;
5801 }
5802 if (state == ARM_CP_STATE_AA32) {
5803 /* Under AArch32 CP registers can be common
5804 * (same for secure and non-secure world) or banked.
5805 */
5806 switch (r->secure) {
5807 case ARM_CP_SECSTATE_S:
5808 case ARM_CP_SECSTATE_NS:
5809 add_cpreg_to_hashtable(cpu, r, opaque, state,
5810 r->secure, crm, opc1, opc2);
5811 break;
5812 default:
5813 add_cpreg_to_hashtable(cpu, r, opaque, state,
5814 ARM_CP_SECSTATE_S,
5815 crm, opc1, opc2);
5816 add_cpreg_to_hashtable(cpu, r, opaque, state,
5817 ARM_CP_SECSTATE_NS,
5818 crm, opc1, opc2);
5819 break;
5820 }
5821 } else {
5822 /* AArch64 registers get mapped to non-secure instance
5823 * of AArch32 */
5824 add_cpreg_to_hashtable(cpu, r, opaque, state,
5825 ARM_CP_SECSTATE_NS,
5826 crm, opc1, opc2);
5827 }
5828 }
5829 }
5830 }
5831 }
5832 }
5833
5834 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5835 const ARMCPRegInfo *regs, void *opaque)
5836 {
5837 /* Define a whole list of registers */
5838 const ARMCPRegInfo *r;
5839 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5840 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5841 }
5842 }
5843
5844 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
5845 {
5846 return g_hash_table_lookup(cpregs, &encoded_cp);
5847 }
5848
5849 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5850 uint64_t value)
5851 {
5852 /* Helper coprocessor write function for write-ignore registers */
5853 }
5854
5855 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
5856 {
5857 /* Helper coprocessor write function for read-as-zero registers */
5858 return 0;
5859 }
5860
5861 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5862 {
5863 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5864 }
5865
5866 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
5867 {
5868 /* Return true if it is not valid for us to switch to
5869 * this CPU mode (ie all the UNPREDICTABLE cases in
5870 * the ARM ARM CPSRWriteByInstr pseudocode).
5871 */
5872
5873 /* Changes to or from Hyp via MSR and CPS are illegal. */
5874 if (write_type == CPSRWriteByInstr &&
5875 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5876 mode == ARM_CPU_MODE_HYP)) {
5877 return 1;
5878 }
5879
5880 switch (mode) {
5881 case ARM_CPU_MODE_USR:
5882 return 0;
5883 case ARM_CPU_MODE_SYS:
5884 case ARM_CPU_MODE_SVC:
5885 case ARM_CPU_MODE_ABT:
5886 case ARM_CPU_MODE_UND:
5887 case ARM_CPU_MODE_IRQ:
5888 case ARM_CPU_MODE_FIQ:
5889 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5890 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5891 */
5892 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5893 * and CPS are treated as illegal mode changes.
5894 */
5895 if (write_type == CPSRWriteByInstr &&
5896 (env->cp15.hcr_el2 & HCR_TGE) &&
5897 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5898 !arm_is_secure_below_el3(env)) {
5899 return 1;
5900 }
5901 return 0;
5902 case ARM_CPU_MODE_HYP:
5903 return !arm_feature(env, ARM_FEATURE_EL2)
5904 || arm_current_el(env) < 2 || arm_is_secure(env);
5905 case ARM_CPU_MODE_MON:
5906 return arm_current_el(env) < 3;
5907 default:
5908 return 1;
5909 }
5910 }
5911
5912 uint32_t cpsr_read(CPUARMState *env)
5913 {
5914 int ZF;
5915 ZF = (env->ZF == 0);
5916 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
5917 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5918 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5919 | ((env->condexec_bits & 0xfc) << 8)
5920 | (env->GE << 16) | (env->daif & CPSR_AIF);
5921 }
5922
5923 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5924 CPSRWriteType write_type)
5925 {
5926 uint32_t changed_daif;
5927
5928 if (mask & CPSR_NZCV) {
5929 env->ZF = (~val) & CPSR_Z;
5930 env->NF = val;
5931 env->CF = (val >> 29) & 1;
5932 env->VF = (val << 3) & 0x80000000;
5933 }
5934 if (mask & CPSR_Q)
5935 env->QF = ((val & CPSR_Q) != 0);
5936 if (mask & CPSR_T)
5937 env->thumb = ((val & CPSR_T) != 0);
5938 if (mask & CPSR_IT_0_1) {
5939 env->condexec_bits &= ~3;
5940 env->condexec_bits |= (val >> 25) & 3;
5941 }
5942 if (mask & CPSR_IT_2_7) {
5943 env->condexec_bits &= 3;
5944 env->condexec_bits |= (val >> 8) & 0xfc;
5945 }
5946 if (mask & CPSR_GE) {
5947 env->GE = (val >> 16) & 0xf;
5948 }
5949
5950 /* In a V7 implementation that includes the security extensions but does
5951 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5952 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5953 * bits respectively.
5954 *
5955 * In a V8 implementation, it is permitted for privileged software to
5956 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5957 */
5958 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
5959 arm_feature(env, ARM_FEATURE_EL3) &&
5960 !arm_feature(env, ARM_FEATURE_EL2) &&
5961 !arm_is_secure(env)) {
5962
5963 changed_daif = (env->daif ^ val) & mask;
5964
5965 if (changed_daif & CPSR_A) {
5966 /* Check to see if we are allowed to change the masking of async
5967 * abort exceptions from a non-secure state.
5968 */
5969 if (!(env->cp15.scr_el3 & SCR_AW)) {
5970 qemu_log_mask(LOG_GUEST_ERROR,
5971 "Ignoring attempt to switch CPSR_A flag from "
5972 "non-secure world with SCR.AW bit clear\n");
5973 mask &= ~CPSR_A;
5974 }
5975 }
5976
5977 if (changed_daif & CPSR_F) {
5978 /* Check to see if we are allowed to change the masking of FIQ
5979 * exceptions from a non-secure state.
5980 */
5981 if (!(env->cp15.scr_el3 & SCR_FW)) {
5982 qemu_log_mask(LOG_GUEST_ERROR,
5983 "Ignoring attempt to switch CPSR_F flag from "
5984 "non-secure world with SCR.FW bit clear\n");
5985 mask &= ~CPSR_F;
5986 }
5987
5988 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5989 * If this bit is set software is not allowed to mask
5990 * FIQs, but is allowed to set CPSR_F to 0.
5991 */
5992 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5993 (val & CPSR_F)) {
5994 qemu_log_mask(LOG_GUEST_ERROR,
5995 "Ignoring attempt to enable CPSR_F flag "
5996 "(non-maskable FIQ [NMFI] support enabled)\n");
5997 mask &= ~CPSR_F;
5998 }
5999 }
6000 }
6001
6002 env->daif &= ~(CPSR_AIF & mask);
6003 env->daif |= val & CPSR_AIF & mask;
6004
6005 if (write_type != CPSRWriteRaw &&
6006 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
6007 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
6008 /* Note that we can only get here in USR mode if this is a
6009 * gdb stub write; for this case we follow the architectural
6010 * behaviour for guest writes in USR mode of ignoring an attempt
6011 * to switch mode. (Those are caught by translate.c for writes
6012 * triggered by guest instructions.)
6013 */
6014 mask &= ~CPSR_M;
6015 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
6016 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
6017 * v7, and has defined behaviour in v8:
6018 * + leave CPSR.M untouched
6019 * + allow changes to the other CPSR fields
6020 * + set PSTATE.IL
6021 * For user changes via the GDB stub, we don't set PSTATE.IL,
6022 * as this would be unnecessarily harsh for a user error.
6023 */
6024 mask &= ~CPSR_M;
6025 if (write_type != CPSRWriteByGDBStub &&
6026 arm_feature(env, ARM_FEATURE_V8)) {
6027 mask |= CPSR_IL;
6028 val |= CPSR_IL;
6029 }
6030 } else {
6031 switch_mode(env, val & CPSR_M);
6032 }
6033 }
6034 mask &= ~CACHED_CPSR_BITS;
6035 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
6036 }
6037
6038 /* Sign/zero extend */
6039 uint32_t HELPER(sxtb16)(uint32_t x)
6040 {
6041 uint32_t res;
6042 res = (uint16_t)(int8_t)x;
6043 res |= (uint32_t)(int8_t)(x >> 16) << 16;
6044 return res;
6045 }
6046
6047 uint32_t HELPER(uxtb16)(uint32_t x)
6048 {
6049 uint32_t res;
6050 res = (uint16_t)(uint8_t)x;
6051 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
6052 return res;
6053 }
6054
6055 int32_t HELPER(sdiv)(int32_t num, int32_t den)
6056 {
6057 if (den == 0)
6058 return 0;
6059 if (num == INT_MIN && den == -1)
6060 return INT_MIN;
6061 return num / den;
6062 }
6063
6064 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
6065 {
6066 if (den == 0)
6067 return 0;
6068 return num / den;
6069 }
6070
6071 uint32_t HELPER(rbit)(uint32_t x)
6072 {
6073 return revbit32(x);
6074 }
6075
6076 #if defined(CONFIG_USER_ONLY)
6077
6078 /* These should probably raise undefined insn exceptions. */
6079 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
6080 {
6081 ARMCPU *cpu = arm_env_get_cpu(env);
6082
6083 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
6084 }
6085
6086 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
6087 {
6088 ARMCPU *cpu = arm_env_get_cpu(env);
6089
6090 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
6091 return 0;
6092 }
6093
6094 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6095 {
6096 /* translate.c should never generate calls here in user-only mode */
6097 g_assert_not_reached();
6098 }
6099
6100 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6101 {
6102 /* translate.c should never generate calls here in user-only mode */
6103 g_assert_not_reached();
6104 }
6105
6106 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
6107 {
6108 /* The TT instructions can be used by unprivileged code, but in
6109 * user-only emulation we don't have the MPU.
6110 * Luckily since we know we are NonSecure unprivileged (and that in
6111 * turn means that the A flag wasn't specified), all the bits in the
6112 * register must be zero:
6113 * IREGION: 0 because IRVALID is 0
6114 * IRVALID: 0 because NS
6115 * S: 0 because NS
6116 * NSRW: 0 because NS
6117 * NSR: 0 because NS
6118 * RW: 0 because unpriv and A flag not set
6119 * R: 0 because unpriv and A flag not set
6120 * SRVALID: 0 because NS
6121 * MRVALID: 0 because unpriv and A flag not set
6122 * SREGION: 0 becaus SRVALID is 0
6123 * MREGION: 0 because MRVALID is 0
6124 */
6125 return 0;
6126 }
6127
6128 void switch_mode(CPUARMState *env, int mode)
6129 {
6130 ARMCPU *cpu = arm_env_get_cpu(env);
6131
6132 if (mode != ARM_CPU_MODE_USR) {
6133 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
6134 }
6135 }
6136
6137 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6138 uint32_t cur_el, bool secure)
6139 {
6140 return 1;
6141 }
6142
6143 void aarch64_sync_64_to_32(CPUARMState *env)
6144 {
6145 g_assert_not_reached();
6146 }
6147
6148 #else
6149
6150 void switch_mode(CPUARMState *env, int mode)
6151 {
6152 int old_mode;
6153 int i;
6154
6155 old_mode = env->uncached_cpsr & CPSR_M;
6156 if (mode == old_mode)
6157 return;
6158
6159 if (old_mode == ARM_CPU_MODE_FIQ) {
6160 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
6161 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
6162 } else if (mode == ARM_CPU_MODE_FIQ) {
6163 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
6164 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
6165 }
6166
6167 i = bank_number(old_mode);
6168 env->banked_r13[i] = env->regs[13];
6169 env->banked_r14[i] = env->regs[14];
6170 env->banked_spsr[i] = env->spsr;
6171
6172 i = bank_number(mode);
6173 env->regs[13] = env->banked_r13[i];
6174 env->regs[14] = env->banked_r14[i];
6175 env->spsr = env->banked_spsr[i];
6176 }
6177
6178 /* Physical Interrupt Target EL Lookup Table
6179 *
6180 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6181 *
6182 * The below multi-dimensional table is used for looking up the target
6183 * exception level given numerous condition criteria. Specifically, the
6184 * target EL is based on SCR and HCR routing controls as well as the
6185 * currently executing EL and secure state.
6186 *
6187 * Dimensions:
6188 * target_el_table[2][2][2][2][2][4]
6189 * | | | | | +--- Current EL
6190 * | | | | +------ Non-secure(0)/Secure(1)
6191 * | | | +--------- HCR mask override
6192 * | | +------------ SCR exec state control
6193 * | +--------------- SCR mask override
6194 * +------------------ 32-bit(0)/64-bit(1) EL3
6195 *
6196 * The table values are as such:
6197 * 0-3 = EL0-EL3
6198 * -1 = Cannot occur
6199 *
6200 * The ARM ARM target EL table includes entries indicating that an "exception
6201 * is not taken". The two cases where this is applicable are:
6202 * 1) An exception is taken from EL3 but the SCR does not have the exception
6203 * routed to EL3.
6204 * 2) An exception is taken from EL2 but the HCR does not have the exception
6205 * routed to EL2.
6206 * In these two cases, the below table contain a target of EL1. This value is
6207 * returned as it is expected that the consumer of the table data will check
6208 * for "target EL >= current EL" to ensure the exception is not taken.
6209 *
6210 * SCR HCR
6211 * 64 EA AMO From
6212 * BIT IRQ IMO Non-secure Secure
6213 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6214 */
6215 static const int8_t target_el_table[2][2][2][2][2][4] = {
6216 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6217 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6218 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6219 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6220 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6221 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6222 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6223 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6224 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6225 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6226 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6227 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6228 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6229 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6230 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6231 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6232 };
6233
6234 /*
6235 * Determine the target EL for physical exceptions
6236 */
6237 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6238 uint32_t cur_el, bool secure)
6239 {
6240 CPUARMState *env = cs->env_ptr;
6241 int rw;
6242 int scr;
6243 int hcr;
6244 int target_el;
6245 /* Is the highest EL AArch64? */
6246 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6247
6248 if (arm_feature(env, ARM_FEATURE_EL3)) {
6249 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6250 } else {
6251 /* Either EL2 is the highest EL (and so the EL2 register width
6252 * is given by is64); or there is no EL2 or EL3, in which case
6253 * the value of 'rw' does not affect the table lookup anyway.
6254 */
6255 rw = is64;
6256 }
6257
6258 switch (excp_idx) {
6259 case EXCP_IRQ:
6260 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6261 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6262 break;
6263 case EXCP_FIQ:
6264 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6265 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6266 break;
6267 default:
6268 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6269 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6270 break;
6271 };
6272
6273 /* If HCR.TGE is set then HCR is treated as being 1 */
6274 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6275
6276 /* Perform a table-lookup for the target EL given the current state */
6277 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6278
6279 assert(target_el > 0);
6280
6281 return target_el;
6282 }
6283
6284 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
6285 ARMMMUIdx mmu_idx, bool ignfault)
6286 {
6287 CPUState *cs = CPU(cpu);
6288 CPUARMState *env = &cpu->env;
6289 MemTxAttrs attrs = {};
6290 MemTxResult txres;
6291 target_ulong page_size;
6292 hwaddr physaddr;
6293 int prot;
6294 ARMMMUFaultInfo fi;
6295 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6296 int exc;
6297 bool exc_secure;
6298
6299 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
6300 &attrs, &prot, &page_size, &fi, NULL)) {
6301 /* MPU/SAU lookup failed */
6302 if (fi.type == ARMFault_QEMU_SFault) {
6303 qemu_log_mask(CPU_LOG_INT,
6304 "...SecureFault with SFSR.AUVIOL during stacking\n");
6305 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6306 env->v7m.sfar = addr;
6307 exc = ARMV7M_EXCP_SECURE;
6308 exc_secure = false;
6309 } else {
6310 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n");
6311 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
6312 exc = ARMV7M_EXCP_MEM;
6313 exc_secure = secure;
6314 }
6315 goto pend_fault;
6316 }
6317 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
6318 attrs, &txres);
6319 if (txres != MEMTX_OK) {
6320 /* BusFault trying to write the data */
6321 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
6322 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
6323 exc = ARMV7M_EXCP_BUS;
6324 exc_secure = false;
6325 goto pend_fault;
6326 }
6327 return true;
6328
6329 pend_fault:
6330 /* By pending the exception at this point we are making
6331 * the IMPDEF choice "overridden exceptions pended" (see the
6332 * MergeExcInfo() pseudocode). The other choice would be to not
6333 * pend them now and then make a choice about which to throw away
6334 * later if we have two derived exceptions.
6335 * The only case when we must not pend the exception but instead
6336 * throw it away is if we are doing the push of the callee registers
6337 * and we've already generated a derived exception. Even in this
6338 * case we will still update the fault status registers.
6339 */
6340 if (!ignfault) {
6341 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
6342 }
6343 return false;
6344 }
6345
6346 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
6347 ARMMMUIdx mmu_idx)
6348 {
6349 CPUState *cs = CPU(cpu);
6350 CPUARMState *env = &cpu->env;
6351 MemTxAttrs attrs = {};
6352 MemTxResult txres;
6353 target_ulong page_size;
6354 hwaddr physaddr;
6355 int prot;
6356 ARMMMUFaultInfo fi;
6357 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6358 int exc;
6359 bool exc_secure;
6360 uint32_t value;
6361
6362 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
6363 &attrs, &prot, &page_size, &fi, NULL)) {
6364 /* MPU/SAU lookup failed */
6365 if (fi.type == ARMFault_QEMU_SFault) {
6366 qemu_log_mask(CPU_LOG_INT,
6367 "...SecureFault with SFSR.AUVIOL during unstack\n");
6368 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6369 env->v7m.sfar = addr;
6370 exc = ARMV7M_EXCP_SECURE;
6371 exc_secure = false;
6372 } else {
6373 qemu_log_mask(CPU_LOG_INT,
6374 "...MemManageFault with CFSR.MUNSTKERR\n");
6375 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
6376 exc = ARMV7M_EXCP_MEM;
6377 exc_secure = secure;
6378 }
6379 goto pend_fault;
6380 }
6381
6382 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
6383 attrs, &txres);
6384 if (txres != MEMTX_OK) {
6385 /* BusFault trying to read the data */
6386 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
6387 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
6388 exc = ARMV7M_EXCP_BUS;
6389 exc_secure = false;
6390 goto pend_fault;
6391 }
6392
6393 *dest = value;
6394 return true;
6395
6396 pend_fault:
6397 /* By pending the exception at this point we are making
6398 * the IMPDEF choice "overridden exceptions pended" (see the
6399 * MergeExcInfo() pseudocode). The other choice would be to not
6400 * pend them now and then make a choice about which to throw away
6401 * later if we have two derived exceptions.
6402 */
6403 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
6404 return false;
6405 }
6406
6407 /* Return true if we're using the process stack pointer (not the MSP) */
6408 static bool v7m_using_psp(CPUARMState *env)
6409 {
6410 /* Handler mode always uses the main stack; for thread mode
6411 * the CONTROL.SPSEL bit determines the answer.
6412 * Note that in v7M it is not possible to be in Handler mode with
6413 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6414 */
6415 return !arm_v7m_is_handler_mode(env) &&
6416 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6417 }
6418
6419 /* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6420 * This may change the current stack pointer between Main and Process
6421 * stack pointers if it is done for the CONTROL register for the current
6422 * security state.
6423 */
6424 static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6425 bool new_spsel,
6426 bool secstate)
6427 {
6428 bool old_is_psp = v7m_using_psp(env);
6429
6430 env->v7m.control[secstate] =
6431 deposit32(env->v7m.control[secstate],
6432 R_V7M_CONTROL_SPSEL_SHIFT,
6433 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6434
6435 if (secstate == env->v7m.secure) {
6436 bool new_is_psp = v7m_using_psp(env);
6437 uint32_t tmp;
6438
6439 if (old_is_psp != new_is_psp) {
6440 tmp = env->v7m.other_sp;
6441 env->v7m.other_sp = env->regs[13];
6442 env->regs[13] = tmp;
6443 }
6444 }
6445 }
6446
6447 /* Write to v7M CONTROL.SPSEL bit. This may change the current
6448 * stack pointer between Main and Process stack pointers.
6449 */
6450 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6451 {
6452 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6453 }
6454
6455 void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6456 {
6457 /* Write a new value to v7m.exception, thus transitioning into or out
6458 * of Handler mode; this may result in a change of active stack pointer.
6459 */
6460 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6461 uint32_t tmp;
6462
6463 env->v7m.exception = new_exc;
6464
6465 new_is_psp = v7m_using_psp(env);
6466
6467 if (old_is_psp != new_is_psp) {
6468 tmp = env->v7m.other_sp;
6469 env->v7m.other_sp = env->regs[13];
6470 env->regs[13] = tmp;
6471 }
6472 }
6473
6474 /* Switch M profile security state between NS and S */
6475 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6476 {
6477 uint32_t new_ss_msp, new_ss_psp;
6478
6479 if (env->v7m.secure == new_secstate) {
6480 return;
6481 }
6482
6483 /* All the banked state is accessed by looking at env->v7m.secure
6484 * except for the stack pointer; rearrange the SP appropriately.
6485 */
6486 new_ss_msp = env->v7m.other_ss_msp;
6487 new_ss_psp = env->v7m.other_ss_psp;
6488
6489 if (v7m_using_psp(env)) {
6490 env->v7m.other_ss_psp = env->regs[13];
6491 env->v7m.other_ss_msp = env->v7m.other_sp;
6492 } else {
6493 env->v7m.other_ss_msp = env->regs[13];
6494 env->v7m.other_ss_psp = env->v7m.other_sp;
6495 }
6496
6497 env->v7m.secure = new_secstate;
6498
6499 if (v7m_using_psp(env)) {
6500 env->regs[13] = new_ss_psp;
6501 env->v7m.other_sp = new_ss_msp;
6502 } else {
6503 env->regs[13] = new_ss_msp;
6504 env->v7m.other_sp = new_ss_psp;
6505 }
6506 }
6507
6508 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6509 {
6510 /* Handle v7M BXNS:
6511 * - if the return value is a magic value, do exception return (like BX)
6512 * - otherwise bit 0 of the return value is the target security state
6513 */
6514 uint32_t min_magic;
6515
6516 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6517 /* Covers FNC_RETURN and EXC_RETURN magic */
6518 min_magic = FNC_RETURN_MIN_MAGIC;
6519 } else {
6520 /* EXC_RETURN magic only */
6521 min_magic = EXC_RETURN_MIN_MAGIC;
6522 }
6523
6524 if (dest >= min_magic) {
6525 /* This is an exception return magic value; put it where
6526 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6527 * Note that if we ever add gen_ss_advance() singlestep support to
6528 * M profile this should count as an "instruction execution complete"
6529 * event (compare gen_bx_excret_final_code()).
6530 */
6531 env->regs[15] = dest & ~1;
6532 env->thumb = dest & 1;
6533 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6534 /* notreached */
6535 }
6536
6537 /* translate.c should have made BXNS UNDEF unless we're secure */
6538 assert(env->v7m.secure);
6539
6540 switch_v7m_security_state(env, dest & 1);
6541 env->thumb = 1;
6542 env->regs[15] = dest & ~1;
6543 }
6544
6545 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6546 {
6547 /* Handle v7M BLXNS:
6548 * - bit 0 of the destination address is the target security state
6549 */
6550
6551 /* At this point regs[15] is the address just after the BLXNS */
6552 uint32_t nextinst = env->regs[15] | 1;
6553 uint32_t sp = env->regs[13] - 8;
6554 uint32_t saved_psr;
6555
6556 /* translate.c will have made BLXNS UNDEF unless we're secure */
6557 assert(env->v7m.secure);
6558
6559 if (dest & 1) {
6560 /* target is Secure, so this is just a normal BLX,
6561 * except that the low bit doesn't indicate Thumb/not.
6562 */
6563 env->regs[14] = nextinst;
6564 env->thumb = 1;
6565 env->regs[15] = dest & ~1;
6566 return;
6567 }
6568
6569 /* Target is non-secure: first push a stack frame */
6570 if (!QEMU_IS_ALIGNED(sp, 8)) {
6571 qemu_log_mask(LOG_GUEST_ERROR,
6572 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6573 }
6574
6575 saved_psr = env->v7m.exception;
6576 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6577 saved_psr |= XPSR_SFPA;
6578 }
6579
6580 /* Note that these stores can throw exceptions on MPU faults */
6581 cpu_stl_data(env, sp, nextinst);
6582 cpu_stl_data(env, sp + 4, saved_psr);
6583
6584 env->regs[13] = sp;
6585 env->regs[14] = 0xfeffffff;
6586 if (arm_v7m_is_handler_mode(env)) {
6587 /* Write a dummy value to IPSR, to avoid leaking the current secure
6588 * exception number to non-secure code. This is guaranteed not
6589 * to cause write_v7m_exception() to actually change stacks.
6590 */
6591 write_v7m_exception(env, 1);
6592 }
6593 switch_v7m_security_state(env, 0);
6594 env->thumb = 1;
6595 env->regs[15] = dest;
6596 }
6597
6598 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6599 bool spsel)
6600 {
6601 /* Return a pointer to the location where we currently store the
6602 * stack pointer for the requested security state and thread mode.
6603 * This pointer will become invalid if the CPU state is updated
6604 * such that the stack pointers are switched around (eg changing
6605 * the SPSEL control bit).
6606 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6607 * Unlike that pseudocode, we require the caller to pass us in the
6608 * SPSEL control bit value; this is because we also use this
6609 * function in handling of pushing of the callee-saves registers
6610 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6611 * and in the tailchain codepath the SPSEL bit comes from the exception
6612 * return magic LR value from the previous exception. The pseudocode
6613 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6614 * to make this utility function generic enough to do the job.
6615 */
6616 bool want_psp = threadmode && spsel;
6617
6618 if (secure == env->v7m.secure) {
6619 if (want_psp == v7m_using_psp(env)) {
6620 return &env->regs[13];
6621 } else {
6622 return &env->v7m.other_sp;
6623 }
6624 } else {
6625 if (want_psp) {
6626 return &env->v7m.other_ss_psp;
6627 } else {
6628 return &env->v7m.other_ss_msp;
6629 }
6630 }
6631 }
6632
6633 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
6634 uint32_t *pvec)
6635 {
6636 CPUState *cs = CPU(cpu);
6637 CPUARMState *env = &cpu->env;
6638 MemTxResult result;
6639 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
6640 uint32_t vector_entry;
6641 MemTxAttrs attrs = {};
6642 ARMMMUIdx mmu_idx;
6643 bool exc_secure;
6644
6645 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
6646
6647 /* We don't do a get_phys_addr() here because the rules for vector
6648 * loads are special: they always use the default memory map, and
6649 * the default memory map permits reads from all addresses.
6650 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
6651 * that we want this special case which would always say "yes",
6652 * we just do the SAU lookup here followed by a direct physical load.
6653 */
6654 attrs.secure = targets_secure;
6655 attrs.user = false;
6656
6657 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6658 V8M_SAttributes sattrs = {};
6659
6660 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
6661 if (sattrs.ns) {
6662 attrs.secure = false;
6663 } else if (!targets_secure) {
6664 /* NS access to S memory */
6665 goto load_fail;
6666 }
6667 }
6668
6669 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
6670 attrs, &result);
6671 if (result != MEMTX_OK) {
6672 goto load_fail;
6673 }
6674 *pvec = vector_entry;
6675 return true;
6676
6677 load_fail:
6678 /* All vector table fetch fails are reported as HardFault, with
6679 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
6680 * technically the underlying exception is a MemManage or BusFault
6681 * that is escalated to HardFault.) This is a terminal exception,
6682 * so we will either take the HardFault immediately or else enter
6683 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
6684 */
6685 exc_secure = targets_secure ||
6686 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
6687 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
6688 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
6689 return false;
6690 }
6691
6692 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6693 bool ignore_faults)
6694 {
6695 /* For v8M, push the callee-saves register part of the stack frame.
6696 * Compare the v8M pseudocode PushCalleeStack().
6697 * In the tailchaining case this may not be the current stack.
6698 */
6699 CPUARMState *env = &cpu->env;
6700 uint32_t *frame_sp_p;
6701 uint32_t frameptr;
6702 ARMMMUIdx mmu_idx;
6703 bool stacked_ok;
6704
6705 if (dotailchain) {
6706 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
6707 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
6708 !mode;
6709
6710 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
6711 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
6712 lr & R_V7M_EXCRET_SPSEL_MASK);
6713 } else {
6714 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
6715 frame_sp_p = &env->regs[13];
6716 }
6717
6718 frameptr = *frame_sp_p - 0x28;
6719
6720 /* Write as much of the stack frame as we can. A write failure may
6721 * cause us to pend a derived exception.
6722 */
6723 stacked_ok =
6724 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) &&
6725 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx,
6726 ignore_faults) &&
6727 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx,
6728 ignore_faults) &&
6729 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx,
6730 ignore_faults) &&
6731 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx,
6732 ignore_faults) &&
6733 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx,
6734 ignore_faults) &&
6735 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx,
6736 ignore_faults) &&
6737 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx,
6738 ignore_faults) &&
6739 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx,
6740 ignore_faults);
6741
6742 /* Update SP regardless of whether any of the stack accesses failed.
6743 * When we implement v8M stack limit checking then this attempt to
6744 * update SP might also fail and result in a derived exception.
6745 */
6746 *frame_sp_p = frameptr;
6747
6748 return !stacked_ok;
6749 }
6750
6751 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6752 bool ignore_stackfaults)
6753 {
6754 /* Do the "take the exception" parts of exception entry,
6755 * but not the pushing of state to the stack. This is
6756 * similar to the pseudocode ExceptionTaken() function.
6757 */
6758 CPUARMState *env = &cpu->env;
6759 uint32_t addr;
6760 bool targets_secure;
6761 int exc;
6762 bool push_failed = false;
6763
6764 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
6765
6766 if (arm_feature(env, ARM_FEATURE_V8)) {
6767 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6768 (lr & R_V7M_EXCRET_S_MASK)) {
6769 /* The background code (the owner of the registers in the
6770 * exception frame) is Secure. This means it may either already
6771 * have or now needs to push callee-saves registers.
6772 */
6773 if (targets_secure) {
6774 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6775 /* We took an exception from Secure to NonSecure
6776 * (which means the callee-saved registers got stacked)
6777 * and are now tailchaining to a Secure exception.
6778 * Clear DCRS so eventual return from this Secure
6779 * exception unstacks the callee-saved registers.
6780 */
6781 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6782 }
6783 } else {
6784 /* We're going to a non-secure exception; push the
6785 * callee-saves registers to the stack now, if they're
6786 * not already saved.
6787 */
6788 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6789 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
6790 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
6791 ignore_stackfaults);
6792 }
6793 lr |= R_V7M_EXCRET_DCRS_MASK;
6794 }
6795 }
6796
6797 lr &= ~R_V7M_EXCRET_ES_MASK;
6798 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6799 lr |= R_V7M_EXCRET_ES_MASK;
6800 }
6801 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6802 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6803 lr |= R_V7M_EXCRET_SPSEL_MASK;
6804 }
6805
6806 /* Clear registers if necessary to prevent non-secure exception
6807 * code being able to see register values from secure code.
6808 * Where register values become architecturally UNKNOWN we leave
6809 * them with their previous values.
6810 */
6811 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6812 if (!targets_secure) {
6813 /* Always clear the caller-saved registers (they have been
6814 * pushed to the stack earlier in v7m_push_stack()).
6815 * Clear callee-saved registers if the background code is
6816 * Secure (in which case these regs were saved in
6817 * v7m_push_callee_stack()).
6818 */
6819 int i;
6820
6821 for (i = 0; i < 13; i++) {
6822 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6823 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6824 env->regs[i] = 0;
6825 }
6826 }
6827 /* Clear EAPSR */
6828 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6829 }
6830 }
6831 }
6832
6833 if (push_failed && !ignore_stackfaults) {
6834 /* Derived exception on callee-saves register stacking:
6835 * we might now want to take a different exception which
6836 * targets a different security state, so try again from the top.
6837 */
6838 v7m_exception_taken(cpu, lr, true, true);
6839 return;
6840 }
6841
6842 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
6843 /* Vector load failed: derived exception */
6844 v7m_exception_taken(cpu, lr, true, true);
6845 return;
6846 }
6847
6848 /* Now we've done everything that might cause a derived exception
6849 * we can go ahead and activate whichever exception we're going to
6850 * take (which might now be the derived exception).
6851 */
6852 armv7m_nvic_acknowledge_irq(env->nvic);
6853
6854 /* Switch to target security state -- must do this before writing SPSEL */
6855 switch_v7m_security_state(env, targets_secure);
6856 write_v7m_control_spsel(env, 0);
6857 arm_clear_exclusive(env);
6858 /* Clear IT bits */
6859 env->condexec_bits = 0;
6860 env->regs[14] = lr;
6861 env->regs[15] = addr & 0xfffffffe;
6862 env->thumb = addr & 1;
6863 }
6864
6865 static bool v7m_push_stack(ARMCPU *cpu)
6866 {
6867 /* Do the "set up stack frame" part of exception entry,
6868 * similar to pseudocode PushStack().
6869 * Return true if we generate a derived exception (and so
6870 * should ignore further stack faults trying to process
6871 * that derived exception.)
6872 */
6873 bool stacked_ok;
6874 CPUARMState *env = &cpu->env;
6875 uint32_t xpsr = xpsr_read(env);
6876 uint32_t frameptr = env->regs[13];
6877 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
6878
6879 /* Align stack pointer if the guest wants that */
6880 if ((frameptr & 4) &&
6881 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
6882 frameptr -= 4;
6883 xpsr |= XPSR_SPREALIGN;
6884 }
6885
6886 frameptr -= 0x20;
6887
6888 /* Write as much of the stack frame as we can. If we fail a stack
6889 * write this will result in a derived exception being pended
6890 * (which may be taken in preference to the one we started with
6891 * if it has higher priority).
6892 */
6893 stacked_ok =
6894 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) &&
6895 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) &&
6896 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) &&
6897 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) &&
6898 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) &&
6899 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) &&
6900 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
6901 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
6902
6903 /* Update SP regardless of whether any of the stack accesses failed.
6904 * When we implement v8M stack limit checking then this attempt to
6905 * update SP might also fail and result in a derived exception.
6906 */
6907 env->regs[13] = frameptr;
6908
6909 return !stacked_ok;
6910 }
6911
6912 static void do_v7m_exception_exit(ARMCPU *cpu)
6913 {
6914 CPUARMState *env = &cpu->env;
6915 CPUState *cs = CPU(cpu);
6916 uint32_t excret;
6917 uint32_t xpsr;
6918 bool ufault = false;
6919 bool sfault = false;
6920 bool return_to_sp_process;
6921 bool return_to_handler;
6922 bool rettobase = false;
6923 bool exc_secure = false;
6924 bool return_to_secure;
6925
6926 /* If we're not in Handler mode then jumps to magic exception-exit
6927 * addresses don't have magic behaviour. However for the v8M
6928 * security extensions the magic secure-function-return has to
6929 * work in thread mode too, so to avoid doing an extra check in
6930 * the generated code we allow exception-exit magic to also cause the
6931 * internal exception and bring us here in thread mode. Correct code
6932 * will never try to do this (the following insn fetch will always
6933 * fault) so we the overhead of having taken an unnecessary exception
6934 * doesn't matter.
6935 */
6936 if (!arm_v7m_is_handler_mode(env)) {
6937 return;
6938 }
6939
6940 /* In the spec pseudocode ExceptionReturn() is called directly
6941 * from BXWritePC() and gets the full target PC value including
6942 * bit zero. In QEMU's implementation we treat it as a normal
6943 * jump-to-register (which is then caught later on), and so split
6944 * the target value up between env->regs[15] and env->thumb in
6945 * gen_bx(). Reconstitute it.
6946 */
6947 excret = env->regs[15];
6948 if (env->thumb) {
6949 excret |= 1;
6950 }
6951
6952 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
6953 " previous exception %d\n",
6954 excret, env->v7m.exception);
6955
6956 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
6957 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
6958 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
6959 excret);
6960 }
6961
6962 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6963 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
6964 * we pick which FAULTMASK to clear.
6965 */
6966 if (!env->v7m.secure &&
6967 ((excret & R_V7M_EXCRET_ES_MASK) ||
6968 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
6969 sfault = 1;
6970 /* For all other purposes, treat ES as 0 (R_HXSR) */
6971 excret &= ~R_V7M_EXCRET_ES_MASK;
6972 }
6973 }
6974
6975 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
6976 /* Auto-clear FAULTMASK on return from other than NMI.
6977 * If the security extension is implemented then this only
6978 * happens if the raw execution priority is >= 0; the
6979 * value of the ES bit in the exception return value indicates
6980 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
6981 */
6982 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6983 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
6984 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
6985 env->v7m.faultmask[exc_secure] = 0;
6986 }
6987 } else {
6988 env->v7m.faultmask[M_REG_NS] = 0;
6989 }
6990 }
6991
6992 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
6993 exc_secure)) {
6994 case -1:
6995 /* attempt to exit an exception that isn't active */
6996 ufault = true;
6997 break;
6998 case 0:
6999 /* still an irq active now */
7000 break;
7001 case 1:
7002 /* we returned to base exception level, no nesting.
7003 * (In the pseudocode this is written using "NestedActivation != 1"
7004 * where we have 'rettobase == false'.)
7005 */
7006 rettobase = true;
7007 break;
7008 default:
7009 g_assert_not_reached();
7010 }
7011
7012 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
7013 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
7014 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
7015 (excret & R_V7M_EXCRET_S_MASK);
7016
7017 if (arm_feature(env, ARM_FEATURE_V8)) {
7018 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7019 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
7020 * we choose to take the UsageFault.
7021 */
7022 if ((excret & R_V7M_EXCRET_S_MASK) ||
7023 (excret & R_V7M_EXCRET_ES_MASK) ||
7024 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
7025 ufault = true;
7026 }
7027 }
7028 if (excret & R_V7M_EXCRET_RES0_MASK) {
7029 ufault = true;
7030 }
7031 } else {
7032 /* For v7M we only recognize certain combinations of the low bits */
7033 switch (excret & 0xf) {
7034 case 1: /* Return to Handler */
7035 break;
7036 case 13: /* Return to Thread using Process stack */
7037 case 9: /* Return to Thread using Main stack */
7038 /* We only need to check NONBASETHRDENA for v7M, because in
7039 * v8M this bit does not exist (it is RES1).
7040 */
7041 if (!rettobase &&
7042 !(env->v7m.ccr[env->v7m.secure] &
7043 R_V7M_CCR_NONBASETHRDENA_MASK)) {
7044 ufault = true;
7045 }
7046 break;
7047 default:
7048 ufault = true;
7049 }
7050 }
7051
7052 if (sfault) {
7053 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
7054 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7055 v7m_exception_taken(cpu, excret, true, false);
7056 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7057 "stackframe: failed EXC_RETURN.ES validity check\n");
7058 return;
7059 }
7060
7061 if (ufault) {
7062 /* Bad exception return: instead of popping the exception
7063 * stack, directly take a usage fault on the current stack.
7064 */
7065 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7066 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7067 v7m_exception_taken(cpu, excret, true, false);
7068 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7069 "stackframe: failed exception return integrity check\n");
7070 return;
7071 }
7072
7073 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
7074 * Handler mode (and will be until we write the new XPSR.Interrupt
7075 * field) this does not switch around the current stack pointer.
7076 */
7077 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
7078
7079 switch_v7m_security_state(env, return_to_secure);
7080
7081 {
7082 /* The stack pointer we should be reading the exception frame from
7083 * depends on bits in the magic exception return type value (and
7084 * for v8M isn't necessarily the stack pointer we will eventually
7085 * end up resuming execution with). Get a pointer to the location
7086 * in the CPU state struct where the SP we need is currently being
7087 * stored; we will use and modify it in place.
7088 * We use this limited C variable scope so we don't accidentally
7089 * use 'frame_sp_p' after we do something that makes it invalid.
7090 */
7091 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
7092 return_to_secure,
7093 !return_to_handler,
7094 return_to_sp_process);
7095 uint32_t frameptr = *frame_sp_p;
7096 bool pop_ok = true;
7097 ARMMMUIdx mmu_idx;
7098
7099 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
7100 !return_to_handler);
7101
7102 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
7103 arm_feature(env, ARM_FEATURE_V8)) {
7104 qemu_log_mask(LOG_GUEST_ERROR,
7105 "M profile exception return with non-8-aligned SP "
7106 "for destination state is UNPREDICTABLE\n");
7107 }
7108
7109 /* Do we need to pop callee-saved registers? */
7110 if (return_to_secure &&
7111 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
7112 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
7113 uint32_t expected_sig = 0xfefa125b;
7114 uint32_t actual_sig = ldl_phys(cs->as, frameptr);
7115
7116 if (expected_sig != actual_sig) {
7117 /* Take a SecureFault on the current stack */
7118 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
7119 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7120 v7m_exception_taken(cpu, excret, true, false);
7121 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7122 "stackframe: failed exception return integrity "
7123 "signature check\n");
7124 return;
7125 }
7126
7127 pop_ok =
7128 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7129 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7130 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
7131 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
7132 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
7133 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
7134 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
7135 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
7136 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
7137
7138 frameptr += 0x28;
7139 }
7140
7141 /* Pop registers */
7142 pop_ok = pop_ok &&
7143 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
7144 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
7145 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
7146 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
7147 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
7148 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
7149 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
7150 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
7151
7152 if (!pop_ok) {
7153 /* v7m_stack_read() pended a fault, so take it (as a tail
7154 * chained exception on the same stack frame)
7155 */
7156 v7m_exception_taken(cpu, excret, true, false);
7157 return;
7158 }
7159
7160 /* Returning from an exception with a PC with bit 0 set is defined
7161 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
7162 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
7163 * the lsbit, and there are several RTOSes out there which incorrectly
7164 * assume the r15 in the stack frame should be a Thumb-style "lsbit
7165 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
7166 * complain about the badly behaved guest.
7167 */
7168 if (env->regs[15] & 1) {
7169 env->regs[15] &= ~1U;
7170 if (!arm_feature(env, ARM_FEATURE_V8)) {
7171 qemu_log_mask(LOG_GUEST_ERROR,
7172 "M profile return from interrupt with misaligned "
7173 "PC is UNPREDICTABLE on v7M\n");
7174 }
7175 }
7176
7177 if (arm_feature(env, ARM_FEATURE_V8)) {
7178 /* For v8M we have to check whether the xPSR exception field
7179 * matches the EXCRET value for return to handler/thread
7180 * before we commit to changing the SP and xPSR.
7181 */
7182 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
7183 if (return_to_handler != will_be_handler) {
7184 /* Take an INVPC UsageFault on the current stack.
7185 * By this point we will have switched to the security state
7186 * for the background state, so this UsageFault will target
7187 * that state.
7188 */
7189 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7190 env->v7m.secure);
7191 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7192 v7m_exception_taken(cpu, excret, true, false);
7193 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7194 "stackframe: failed exception return integrity "
7195 "check\n");
7196 return;
7197 }
7198 }
7199
7200 /* Commit to consuming the stack frame */
7201 frameptr += 0x20;
7202 /* Undo stack alignment (the SPREALIGN bit indicates that the original
7203 * pre-exception SP was not 8-aligned and we added a padding word to
7204 * align it, so we undo this by ORing in the bit that increases it
7205 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
7206 * would work too but a logical OR is how the pseudocode specifies it.)
7207 */
7208 if (xpsr & XPSR_SPREALIGN) {
7209 frameptr |= 4;
7210 }
7211 *frame_sp_p = frameptr;
7212 }
7213 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
7214 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
7215
7216 /* The restored xPSR exception field will be zero if we're
7217 * resuming in Thread mode. If that doesn't match what the
7218 * exception return excret specified then this is a UsageFault.
7219 * v7M requires we make this check here; v8M did it earlier.
7220 */
7221 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
7222 /* Take an INVPC UsageFault by pushing the stack again;
7223 * we know we're v7M so this is never a Secure UsageFault.
7224 */
7225 bool ignore_stackfaults;
7226
7227 assert(!arm_feature(env, ARM_FEATURE_V8));
7228 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
7229 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7230 ignore_stackfaults = v7m_push_stack(cpu);
7231 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
7232 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
7233 "failed exception return integrity check\n");
7234 return;
7235 }
7236
7237 /* Otherwise, we have a successful exception exit. */
7238 arm_clear_exclusive(env);
7239 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
7240 }
7241
7242 static bool do_v7m_function_return(ARMCPU *cpu)
7243 {
7244 /* v8M security extensions magic function return.
7245 * We may either:
7246 * (1) throw an exception (longjump)
7247 * (2) return true if we successfully handled the function return
7248 * (3) return false if we failed a consistency check and have
7249 * pended a UsageFault that needs to be taken now
7250 *
7251 * At this point the magic return value is split between env->regs[15]
7252 * and env->thumb. We don't bother to reconstitute it because we don't
7253 * need it (all values are handled the same way).
7254 */
7255 CPUARMState *env = &cpu->env;
7256 uint32_t newpc, newpsr, newpsr_exc;
7257
7258 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
7259
7260 {
7261 bool threadmode, spsel;
7262 TCGMemOpIdx oi;
7263 ARMMMUIdx mmu_idx;
7264 uint32_t *frame_sp_p;
7265 uint32_t frameptr;
7266
7267 /* Pull the return address and IPSR from the Secure stack */
7268 threadmode = !arm_v7m_is_handler_mode(env);
7269 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
7270
7271 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
7272 frameptr = *frame_sp_p;
7273
7274 /* These loads may throw an exception (for MPU faults). We want to
7275 * do them as secure, so work out what MMU index that is.
7276 */
7277 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7278 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
7279 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
7280 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
7281
7282 /* Consistency checks on new IPSR */
7283 newpsr_exc = newpsr & XPSR_EXCP;
7284 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
7285 (env->v7m.exception == 1 && newpsr_exc != 0))) {
7286 /* Pend the fault and tell our caller to take it */
7287 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7288 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7289 env->v7m.secure);
7290 qemu_log_mask(CPU_LOG_INT,
7291 "...taking INVPC UsageFault: "
7292 "IPSR consistency check failed\n");
7293 return false;
7294 }
7295
7296 *frame_sp_p = frameptr + 8;
7297 }
7298
7299 /* This invalidates frame_sp_p */
7300 switch_v7m_security_state(env, true);
7301 env->v7m.exception = newpsr_exc;
7302 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
7303 if (newpsr & XPSR_SFPA) {
7304 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
7305 }
7306 xpsr_write(env, 0, XPSR_IT);
7307 env->thumb = newpc & 1;
7308 env->regs[15] = newpc & ~1;
7309
7310 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
7311 return true;
7312 }
7313
7314 static void arm_log_exception(int idx)
7315 {
7316 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7317 const char *exc = NULL;
7318 static const char * const excnames[] = {
7319 [EXCP_UDEF] = "Undefined Instruction",
7320 [EXCP_SWI] = "SVC",
7321 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7322 [EXCP_DATA_ABORT] = "Data Abort",
7323 [EXCP_IRQ] = "IRQ",
7324 [EXCP_FIQ] = "FIQ",
7325 [EXCP_BKPT] = "Breakpoint",
7326 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7327 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7328 [EXCP_HVC] = "Hypervisor Call",
7329 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7330 [EXCP_SMC] = "Secure Monitor Call",
7331 [EXCP_VIRQ] = "Virtual IRQ",
7332 [EXCP_VFIQ] = "Virtual FIQ",
7333 [EXCP_SEMIHOST] = "Semihosting call",
7334 [EXCP_NOCP] = "v7M NOCP UsageFault",
7335 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7336 };
7337
7338 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
7339 exc = excnames[idx];
7340 }
7341 if (!exc) {
7342 exc = "unknown";
7343 }
7344 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
7345 }
7346 }
7347
7348 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
7349 uint32_t addr, uint16_t *insn)
7350 {
7351 /* Load a 16-bit portion of a v7M instruction, returning true on success,
7352 * or false on failure (in which case we will have pended the appropriate
7353 * exception).
7354 * We need to do the instruction fetch's MPU and SAU checks
7355 * like this because there is no MMU index that would allow
7356 * doing the load with a single function call. Instead we must
7357 * first check that the security attributes permit the load
7358 * and that they don't mismatch on the two halves of the instruction,
7359 * and then we do the load as a secure load (ie using the security
7360 * attributes of the address, not the CPU, as architecturally required).
7361 */
7362 CPUState *cs = CPU(cpu);
7363 CPUARMState *env = &cpu->env;
7364 V8M_SAttributes sattrs = {};
7365 MemTxAttrs attrs = {};
7366 ARMMMUFaultInfo fi = {};
7367 MemTxResult txres;
7368 target_ulong page_size;
7369 hwaddr physaddr;
7370 int prot;
7371
7372 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
7373 if (!sattrs.nsc || sattrs.ns) {
7374 /* This must be the second half of the insn, and it straddles a
7375 * region boundary with the second half not being S&NSC.
7376 */
7377 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7378 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7379 qemu_log_mask(CPU_LOG_INT,
7380 "...really SecureFault with SFSR.INVEP\n");
7381 return false;
7382 }
7383 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
7384 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
7385 /* the MPU lookup failed */
7386 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7387 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
7388 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
7389 return false;
7390 }
7391 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7392 attrs, &txres);
7393 if (txres != MEMTX_OK) {
7394 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7395 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7396 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7397 return false;
7398 }
7399 return true;
7400 }
7401
7402 static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7403 {
7404 /* Check whether this attempt to execute code in a Secure & NS-Callable
7405 * memory region is for an SG instruction; if so, then emulate the
7406 * effect of the SG instruction and return true. Otherwise pend
7407 * the correct kind of exception and return false.
7408 */
7409 CPUARMState *env = &cpu->env;
7410 ARMMMUIdx mmu_idx;
7411 uint16_t insn;
7412
7413 /* We should never get here unless get_phys_addr_pmsav8() caused
7414 * an exception for NS executing in S&NSC memory.
7415 */
7416 assert(!env->v7m.secure);
7417 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7418
7419 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7420 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7421
7422 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7423 return false;
7424 }
7425
7426 if (!env->thumb) {
7427 goto gen_invep;
7428 }
7429
7430 if (insn != 0xe97f) {
7431 /* Not an SG instruction first half (we choose the IMPDEF
7432 * early-SG-check option).
7433 */
7434 goto gen_invep;
7435 }
7436
7437 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7438 return false;
7439 }
7440
7441 if (insn != 0xe97f) {
7442 /* Not an SG instruction second half (yes, both halves of the SG
7443 * insn have the same hex value)
7444 */
7445 goto gen_invep;
7446 }
7447
7448 /* OK, we have confirmed that we really have an SG instruction.
7449 * We know we're NS in S memory so don't need to repeat those checks.
7450 */
7451 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7452 ", executing it\n", env->regs[15]);
7453 env->regs[14] &= ~1;
7454 switch_v7m_security_state(env, true);
7455 xpsr_write(env, 0, XPSR_IT);
7456 env->regs[15] += 4;
7457 return true;
7458
7459 gen_invep:
7460 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7461 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7462 qemu_log_mask(CPU_LOG_INT,
7463 "...really SecureFault with SFSR.INVEP\n");
7464 return false;
7465 }
7466
7467 void arm_v7m_cpu_do_interrupt(CPUState *cs)
7468 {
7469 ARMCPU *cpu = ARM_CPU(cs);
7470 CPUARMState *env = &cpu->env;
7471 uint32_t lr;
7472 bool ignore_stackfaults;
7473
7474 arm_log_exception(cs->exception_index);
7475
7476 /* For exceptions we just mark as pending on the NVIC, and let that
7477 handle it. */
7478 switch (cs->exception_index) {
7479 case EXCP_UDEF:
7480 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7481 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
7482 break;
7483 case EXCP_NOCP:
7484 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7485 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
7486 break;
7487 case EXCP_INVSTATE:
7488 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7489 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
7490 break;
7491 case EXCP_SWI:
7492 /* The PC already points to the next instruction. */
7493 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
7494 break;
7495 case EXCP_PREFETCH_ABORT:
7496 case EXCP_DATA_ABORT:
7497 /* Note that for M profile we don't have a guest facing FSR, but
7498 * the env->exception.fsr will be populated by the code that
7499 * raises the fault, in the A profile short-descriptor format.
7500 */
7501 switch (env->exception.fsr & 0xf) {
7502 case M_FAKE_FSR_NSC_EXEC:
7503 /* Exception generated when we try to execute code at an address
7504 * which is marked as Secure & Non-Secure Callable and the CPU
7505 * is in the Non-Secure state. The only instruction which can
7506 * be executed like this is SG (and that only if both halves of
7507 * the SG instruction have the same security attributes.)
7508 * Everything else must generate an INVEP SecureFault, so we
7509 * emulate the SG instruction here.
7510 */
7511 if (v7m_handle_execute_nsc(cpu)) {
7512 return;
7513 }
7514 break;
7515 case M_FAKE_FSR_SFAULT:
7516 /* Various flavours of SecureFault for attempts to execute or
7517 * access data in the wrong security state.
7518 */
7519 switch (cs->exception_index) {
7520 case EXCP_PREFETCH_ABORT:
7521 if (env->v7m.secure) {
7522 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7523 qemu_log_mask(CPU_LOG_INT,
7524 "...really SecureFault with SFSR.INVTRAN\n");
7525 } else {
7526 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7527 qemu_log_mask(CPU_LOG_INT,
7528 "...really SecureFault with SFSR.INVEP\n");
7529 }
7530 break;
7531 case EXCP_DATA_ABORT:
7532 /* This must be an NS access to S memory */
7533 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7534 qemu_log_mask(CPU_LOG_INT,
7535 "...really SecureFault with SFSR.AUVIOL\n");
7536 break;
7537 }
7538 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7539 break;
7540 case 0x8: /* External Abort */
7541 switch (cs->exception_index) {
7542 case EXCP_PREFETCH_ABORT:
7543 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7544 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
7545 break;
7546 case EXCP_DATA_ABORT:
7547 env->v7m.cfsr[M_REG_NS] |=
7548 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
7549 env->v7m.bfar = env->exception.vaddress;
7550 qemu_log_mask(CPU_LOG_INT,
7551 "...with CFSR.PRECISERR and BFAR 0x%x\n",
7552 env->v7m.bfar);
7553 break;
7554 }
7555 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7556 break;
7557 default:
7558 /* All other FSR values are either MPU faults or "can't happen
7559 * for M profile" cases.
7560 */
7561 switch (cs->exception_index) {
7562 case EXCP_PREFETCH_ABORT:
7563 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7564 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7565 break;
7566 case EXCP_DATA_ABORT:
7567 env->v7m.cfsr[env->v7m.secure] |=
7568 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
7569 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
7570 qemu_log_mask(CPU_LOG_INT,
7571 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
7572 env->v7m.mmfar[env->v7m.secure]);
7573 break;
7574 }
7575 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7576 env->v7m.secure);
7577 break;
7578 }
7579 break;
7580 case EXCP_BKPT:
7581 if (semihosting_enabled()) {
7582 int nr;
7583 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
7584 if (nr == 0xab) {
7585 env->regs[15] += 2;
7586 qemu_log_mask(CPU_LOG_INT,
7587 "...handling as semihosting call 0x%x\n",
7588 env->regs[0]);
7589 env->regs[0] = do_arm_semihosting(env);
7590 return;
7591 }
7592 }
7593 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
7594 break;
7595 case EXCP_IRQ:
7596 break;
7597 case EXCP_EXCEPTION_EXIT:
7598 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7599 /* Must be v8M security extension function return */
7600 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7601 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7602 if (do_v7m_function_return(cpu)) {
7603 return;
7604 }
7605 } else {
7606 do_v7m_exception_exit(cpu);
7607 return;
7608 }
7609 break;
7610 default:
7611 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7612 return; /* Never happens. Keep compiler happy. */
7613 }
7614
7615 if (arm_feature(env, ARM_FEATURE_V8)) {
7616 lr = R_V7M_EXCRET_RES1_MASK |
7617 R_V7M_EXCRET_DCRS_MASK |
7618 R_V7M_EXCRET_FTYPE_MASK;
7619 /* The S bit indicates whether we should return to Secure
7620 * or NonSecure (ie our current state).
7621 * The ES bit indicates whether we're taking this exception
7622 * to Secure or NonSecure (ie our target state). We set it
7623 * later, in v7m_exception_taken().
7624 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7625 * This corresponds to the ARM ARM pseudocode for v8M setting
7626 * some LR bits in PushStack() and some in ExceptionTaken();
7627 * the distinction matters for the tailchain cases where we
7628 * can take an exception without pushing the stack.
7629 */
7630 if (env->v7m.secure) {
7631 lr |= R_V7M_EXCRET_S_MASK;
7632 }
7633 } else {
7634 lr = R_V7M_EXCRET_RES1_MASK |
7635 R_V7M_EXCRET_S_MASK |
7636 R_V7M_EXCRET_DCRS_MASK |
7637 R_V7M_EXCRET_FTYPE_MASK |
7638 R_V7M_EXCRET_ES_MASK;
7639 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7640 lr |= R_V7M_EXCRET_SPSEL_MASK;
7641 }
7642 }
7643 if (!arm_v7m_is_handler_mode(env)) {
7644 lr |= R_V7M_EXCRET_MODE_MASK;
7645 }
7646
7647 ignore_stackfaults = v7m_push_stack(cpu);
7648 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
7649 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
7650 }
7651
7652 /* Function used to synchronize QEMU's AArch64 register set with AArch32
7653 * register set. This is necessary when switching between AArch32 and AArch64
7654 * execution state.
7655 */
7656 void aarch64_sync_32_to_64(CPUARMState *env)
7657 {
7658 int i;
7659 uint32_t mode = env->uncached_cpsr & CPSR_M;
7660
7661 /* We can blanket copy R[0:7] to X[0:7] */
7662 for (i = 0; i < 8; i++) {
7663 env->xregs[i] = env->regs[i];
7664 }
7665
7666 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7667 * Otherwise, they come from the banked user regs.
7668 */
7669 if (mode == ARM_CPU_MODE_FIQ) {
7670 for (i = 8; i < 13; i++) {
7671 env->xregs[i] = env->usr_regs[i - 8];
7672 }
7673 } else {
7674 for (i = 8; i < 13; i++) {
7675 env->xregs[i] = env->regs[i];
7676 }
7677 }
7678
7679 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7680 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7681 * from the mode banked register.
7682 */
7683 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7684 env->xregs[13] = env->regs[13];
7685 env->xregs[14] = env->regs[14];
7686 } else {
7687 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7688 /* HYP is an exception in that it is copied from r14 */
7689 if (mode == ARM_CPU_MODE_HYP) {
7690 env->xregs[14] = env->regs[14];
7691 } else {
7692 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7693 }
7694 }
7695
7696 if (mode == ARM_CPU_MODE_HYP) {
7697 env->xregs[15] = env->regs[13];
7698 } else {
7699 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7700 }
7701
7702 if (mode == ARM_CPU_MODE_IRQ) {
7703 env->xregs[16] = env->regs[14];
7704 env->xregs[17] = env->regs[13];
7705 } else {
7706 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7707 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
7708 }
7709
7710 if (mode == ARM_CPU_MODE_SVC) {
7711 env->xregs[18] = env->regs[14];
7712 env->xregs[19] = env->regs[13];
7713 } else {
7714 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7715 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
7716 }
7717
7718 if (mode == ARM_CPU_MODE_ABT) {
7719 env->xregs[20] = env->regs[14];
7720 env->xregs[21] = env->regs[13];
7721 } else {
7722 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7723 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
7724 }
7725
7726 if (mode == ARM_CPU_MODE_UND) {
7727 env->xregs[22] = env->regs[14];
7728 env->xregs[23] = env->regs[13];
7729 } else {
7730 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7731 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
7732 }
7733
7734 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7735 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7736 * FIQ bank for r8-r14.
7737 */
7738 if (mode == ARM_CPU_MODE_FIQ) {
7739 for (i = 24; i < 31; i++) {
7740 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7741 }
7742 } else {
7743 for (i = 24; i < 29; i++) {
7744 env->xregs[i] = env->fiq_regs[i - 24];
7745 }
7746 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7747 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7748 }
7749
7750 env->pc = env->regs[15];
7751 }
7752
7753 /* Function used to synchronize QEMU's AArch32 register set with AArch64
7754 * register set. This is necessary when switching between AArch32 and AArch64
7755 * execution state.
7756 */
7757 void aarch64_sync_64_to_32(CPUARMState *env)
7758 {
7759 int i;
7760 uint32_t mode = env->uncached_cpsr & CPSR_M;
7761
7762 /* We can blanket copy X[0:7] to R[0:7] */
7763 for (i = 0; i < 8; i++) {
7764 env->regs[i] = env->xregs[i];
7765 }
7766
7767 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7768 * Otherwise, we copy x8-x12 into the banked user regs.
7769 */
7770 if (mode == ARM_CPU_MODE_FIQ) {
7771 for (i = 8; i < 13; i++) {
7772 env->usr_regs[i - 8] = env->xregs[i];
7773 }
7774 } else {
7775 for (i = 8; i < 13; i++) {
7776 env->regs[i] = env->xregs[i];
7777 }
7778 }
7779
7780 /* Registers r13 & r14 depend on the current mode.
7781 * If we are in a given mode, we copy the corresponding x registers to r13
7782 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7783 * for the mode.
7784 */
7785 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7786 env->regs[13] = env->xregs[13];
7787 env->regs[14] = env->xregs[14];
7788 } else {
7789 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7790
7791 /* HYP is an exception in that it does not have its own banked r14 but
7792 * shares the USR r14
7793 */
7794 if (mode == ARM_CPU_MODE_HYP) {
7795 env->regs[14] = env->xregs[14];
7796 } else {
7797 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7798 }
7799 }
7800
7801 if (mode == ARM_CPU_MODE_HYP) {
7802 env->regs[13] = env->xregs[15];
7803 } else {
7804 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7805 }
7806
7807 if (mode == ARM_CPU_MODE_IRQ) {
7808 env->regs[14] = env->xregs[16];
7809 env->regs[13] = env->xregs[17];
7810 } else {
7811 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7812 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
7813 }
7814
7815 if (mode == ARM_CPU_MODE_SVC) {
7816 env->regs[14] = env->xregs[18];
7817 env->regs[13] = env->xregs[19];
7818 } else {
7819 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7820 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
7821 }
7822
7823 if (mode == ARM_CPU_MODE_ABT) {
7824 env->regs[14] = env->xregs[20];
7825 env->regs[13] = env->xregs[21];
7826 } else {
7827 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7828 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
7829 }
7830
7831 if (mode == ARM_CPU_MODE_UND) {
7832 env->regs[14] = env->xregs[22];
7833 env->regs[13] = env->xregs[23];
7834 } else {
7835 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7836 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
7837 }
7838
7839 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7840 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7841 * FIQ bank for r8-r14.
7842 */
7843 if (mode == ARM_CPU_MODE_FIQ) {
7844 for (i = 24; i < 31; i++) {
7845 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7846 }
7847 } else {
7848 for (i = 24; i < 29; i++) {
7849 env->fiq_regs[i - 24] = env->xregs[i];
7850 }
7851 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7852 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7853 }
7854
7855 env->regs[15] = env->pc;
7856 }
7857
7858 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
7859 {
7860 ARMCPU *cpu = ARM_CPU(cs);
7861 CPUARMState *env = &cpu->env;
7862 uint32_t addr;
7863 uint32_t mask;
7864 int new_mode;
7865 uint32_t offset;
7866 uint32_t moe;
7867
7868 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7869 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7870 case EC_BREAKPOINT:
7871 case EC_BREAKPOINT_SAME_EL:
7872 moe = 1;
7873 break;
7874 case EC_WATCHPOINT:
7875 case EC_WATCHPOINT_SAME_EL:
7876 moe = 10;
7877 break;
7878 case EC_AA32_BKPT:
7879 moe = 3;
7880 break;
7881 case EC_VECTORCATCH:
7882 moe = 5;
7883 break;
7884 default:
7885 moe = 0;
7886 break;
7887 }
7888
7889 if (moe) {
7890 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7891 }
7892
7893 /* TODO: Vectored interrupt controller. */
7894 switch (cs->exception_index) {
7895 case EXCP_UDEF:
7896 new_mode = ARM_CPU_MODE_UND;
7897 addr = 0x04;
7898 mask = CPSR_I;
7899 if (env->thumb)
7900 offset = 2;
7901 else
7902 offset = 4;
7903 break;
7904 case EXCP_SWI:
7905 new_mode = ARM_CPU_MODE_SVC;
7906 addr = 0x08;
7907 mask = CPSR_I;
7908 /* The PC already points to the next instruction. */
7909 offset = 0;
7910 break;
7911 case EXCP_BKPT:
7912 env->exception.fsr = 2;
7913 /* Fall through to prefetch abort. */
7914 case EXCP_PREFETCH_ABORT:
7915 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
7916 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
7917 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
7918 env->exception.fsr, (uint32_t)env->exception.vaddress);
7919 new_mode = ARM_CPU_MODE_ABT;
7920 addr = 0x0c;
7921 mask = CPSR_A | CPSR_I;
7922 offset = 4;
7923 break;
7924 case EXCP_DATA_ABORT:
7925 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
7926 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
7927 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
7928 env->exception.fsr,
7929 (uint32_t)env->exception.vaddress);
7930 new_mode = ARM_CPU_MODE_ABT;
7931 addr = 0x10;
7932 mask = CPSR_A | CPSR_I;
7933 offset = 8;
7934 break;
7935 case EXCP_IRQ:
7936 new_mode = ARM_CPU_MODE_IRQ;
7937 addr = 0x18;
7938 /* Disable IRQ and imprecise data aborts. */
7939 mask = CPSR_A | CPSR_I;
7940 offset = 4;
7941 if (env->cp15.scr_el3 & SCR_IRQ) {
7942 /* IRQ routed to monitor mode */
7943 new_mode = ARM_CPU_MODE_MON;
7944 mask |= CPSR_F;
7945 }
7946 break;
7947 case EXCP_FIQ:
7948 new_mode = ARM_CPU_MODE_FIQ;
7949 addr = 0x1c;
7950 /* Disable FIQ, IRQ and imprecise data aborts. */
7951 mask = CPSR_A | CPSR_I | CPSR_F;
7952 if (env->cp15.scr_el3 & SCR_FIQ) {
7953 /* FIQ routed to monitor mode */
7954 new_mode = ARM_CPU_MODE_MON;
7955 }
7956 offset = 4;
7957 break;
7958 case EXCP_VIRQ:
7959 new_mode = ARM_CPU_MODE_IRQ;
7960 addr = 0x18;
7961 /* Disable IRQ and imprecise data aborts. */
7962 mask = CPSR_A | CPSR_I;
7963 offset = 4;
7964 break;
7965 case EXCP_VFIQ:
7966 new_mode = ARM_CPU_MODE_FIQ;
7967 addr = 0x1c;
7968 /* Disable FIQ, IRQ and imprecise data aborts. */
7969 mask = CPSR_A | CPSR_I | CPSR_F;
7970 offset = 4;
7971 break;
7972 case EXCP_SMC:
7973 new_mode = ARM_CPU_MODE_MON;
7974 addr = 0x08;
7975 mask = CPSR_A | CPSR_I | CPSR_F;
7976 offset = 0;
7977 break;
7978 default:
7979 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7980 return; /* Never happens. Keep compiler happy. */
7981 }
7982
7983 if (new_mode == ARM_CPU_MODE_MON) {
7984 addr += env->cp15.mvbar;
7985 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
7986 /* High vectors. When enabled, base address cannot be remapped. */
7987 addr += 0xffff0000;
7988 } else {
7989 /* ARM v7 architectures provide a vector base address register to remap
7990 * the interrupt vector table.
7991 * This register is only followed in non-monitor mode, and is banked.
7992 * Note: only bits 31:5 are valid.
7993 */
7994 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
7995 }
7996
7997 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
7998 env->cp15.scr_el3 &= ~SCR_NS;
7999 }
8000
8001 switch_mode (env, new_mode);
8002 /* For exceptions taken to AArch32 we must clear the SS bit in both
8003 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8004 */
8005 env->uncached_cpsr &= ~PSTATE_SS;
8006 env->spsr = cpsr_read(env);
8007 /* Clear IT bits. */
8008 env->condexec_bits = 0;
8009 /* Switch to the new mode, and to the correct instruction set. */
8010 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
8011 /* Set new mode endianness */
8012 env->uncached_cpsr &= ~CPSR_E;
8013 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
8014 env->uncached_cpsr |= CPSR_E;
8015 }
8016 env->daif |= mask;
8017 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
8018 * and we should just guard the thumb mode on V4 */
8019 if (arm_feature(env, ARM_FEATURE_V4T)) {
8020 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
8021 }
8022 env->regs[14] = env->regs[15] + offset;
8023 env->regs[15] = addr;
8024 }
8025
8026 /* Handle exception entry to a target EL which is using AArch64 */
8027 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
8028 {
8029 ARMCPU *cpu = ARM_CPU(cs);
8030 CPUARMState *env = &cpu->env;
8031 unsigned int new_el = env->exception.target_el;
8032 target_ulong addr = env->cp15.vbar_el[new_el];
8033 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
8034
8035 if (arm_current_el(env) < new_el) {
8036 /* Entry vector offset depends on whether the implemented EL
8037 * immediately lower than the target level is using AArch32 or AArch64
8038 */
8039 bool is_aa64;
8040
8041 switch (new_el) {
8042 case 3:
8043 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8044 break;
8045 case 2:
8046 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8047 break;
8048 case 1:
8049 is_aa64 = is_a64(env);
8050 break;
8051 default:
8052 g_assert_not_reached();
8053 }
8054
8055 if (is_aa64) {
8056 addr += 0x400;
8057 } else {
8058 addr += 0x600;
8059 }
8060 } else if (pstate_read(env) & PSTATE_SP) {
8061 addr += 0x200;
8062 }
8063
8064 switch (cs->exception_index) {
8065 case EXCP_PREFETCH_ABORT:
8066 case EXCP_DATA_ABORT:
8067 env->cp15.far_el[new_el] = env->exception.vaddress;
8068 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8069 env->cp15.far_el[new_el]);
8070 /* fall through */
8071 case EXCP_BKPT:
8072 case EXCP_UDEF:
8073 case EXCP_SWI:
8074 case EXCP_HVC:
8075 case EXCP_HYP_TRAP:
8076 case EXCP_SMC:
8077 env->cp15.esr_el[new_el] = env->exception.syndrome;
8078 break;
8079 case EXCP_IRQ:
8080 case EXCP_VIRQ:
8081 addr += 0x80;
8082 break;
8083 case EXCP_FIQ:
8084 case EXCP_VFIQ:
8085 addr += 0x100;
8086 break;
8087 case EXCP_SEMIHOST:
8088 qemu_log_mask(CPU_LOG_INT,
8089 "...handling as semihosting call 0x%" PRIx64 "\n",
8090 env->xregs[0]);
8091 env->xregs[0] = do_arm_semihosting(env);
8092 return;
8093 default:
8094 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8095 }
8096
8097 if (is_a64(env)) {
8098 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8099 aarch64_save_sp(env, arm_current_el(env));
8100 env->elr_el[new_el] = env->pc;
8101 } else {
8102 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
8103 env->elr_el[new_el] = env->regs[15];
8104
8105 aarch64_sync_32_to_64(env);
8106
8107 env->condexec_bits = 0;
8108 }
8109 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8110 env->elr_el[new_el]);
8111
8112 pstate_write(env, PSTATE_DAIF | new_mode);
8113 env->aarch64 = 1;
8114 aarch64_restore_sp(env, new_el);
8115
8116 env->pc = addr;
8117
8118 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8119 new_el, env->pc, pstate_read(env));
8120 }
8121
8122 static inline bool check_for_semihosting(CPUState *cs)
8123 {
8124 /* Check whether this exception is a semihosting call; if so
8125 * then handle it and return true; otherwise return false.
8126 */
8127 ARMCPU *cpu = ARM_CPU(cs);
8128 CPUARMState *env = &cpu->env;
8129
8130 if (is_a64(env)) {
8131 if (cs->exception_index == EXCP_SEMIHOST) {
8132 /* This is always the 64-bit semihosting exception.
8133 * The "is this usermode" and "is semihosting enabled"
8134 * checks have been done at translate time.
8135 */
8136 qemu_log_mask(CPU_LOG_INT,
8137 "...handling as semihosting call 0x%" PRIx64 "\n",
8138 env->xregs[0]);
8139 env->xregs[0] = do_arm_semihosting(env);
8140 return true;
8141 }
8142 return false;
8143 } else {
8144 uint32_t imm;
8145
8146 /* Only intercept calls from privileged modes, to provide some
8147 * semblance of security.
8148 */
8149 if (cs->exception_index != EXCP_SEMIHOST &&
8150 (!semihosting_enabled() ||
8151 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
8152 return false;
8153 }
8154
8155 switch (cs->exception_index) {
8156 case EXCP_SEMIHOST:
8157 /* This is always a semihosting call; the "is this usermode"
8158 * and "is semihosting enabled" checks have been done at
8159 * translate time.
8160 */
8161 break;
8162 case EXCP_SWI:
8163 /* Check for semihosting interrupt. */
8164 if (env->thumb) {
8165 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
8166 & 0xff;
8167 if (imm == 0xab) {
8168 break;
8169 }
8170 } else {
8171 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
8172 & 0xffffff;
8173 if (imm == 0x123456) {
8174 break;
8175 }
8176 }
8177 return false;
8178 case EXCP_BKPT:
8179 /* See if this is a semihosting syscall. */
8180 if (env->thumb) {
8181 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
8182 & 0xff;
8183 if (imm == 0xab) {
8184 env->regs[15] += 2;
8185 break;
8186 }
8187 }
8188 return false;
8189 default:
8190 return false;
8191 }
8192
8193 qemu_log_mask(CPU_LOG_INT,
8194 "...handling as semihosting call 0x%x\n",
8195 env->regs[0]);
8196 env->regs[0] = do_arm_semihosting(env);
8197 return true;
8198 }
8199 }
8200
8201 /* Handle a CPU exception for A and R profile CPUs.
8202 * Do any appropriate logging, handle PSCI calls, and then hand off
8203 * to the AArch64-entry or AArch32-entry function depending on the
8204 * target exception level's register width.
8205 */
8206 void arm_cpu_do_interrupt(CPUState *cs)
8207 {
8208 ARMCPU *cpu = ARM_CPU(cs);
8209 CPUARMState *env = &cpu->env;
8210 unsigned int new_el = env->exception.target_el;
8211
8212 assert(!arm_feature(env, ARM_FEATURE_M));
8213
8214 arm_log_exception(cs->exception_index);
8215 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8216 new_el);
8217 if (qemu_loglevel_mask(CPU_LOG_INT)
8218 && !excp_is_internal(cs->exception_index)) {
8219 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
8220 env->exception.syndrome >> ARM_EL_EC_SHIFT,
8221 env->exception.syndrome);
8222 }
8223
8224 if (arm_is_psci_call(cpu, cs->exception_index)) {
8225 arm_handle_psci_call(cpu);
8226 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8227 return;
8228 }
8229
8230 /* Semihosting semantics depend on the register width of the
8231 * code that caused the exception, not the target exception level,
8232 * so must be handled here.
8233 */
8234 if (check_for_semihosting(cs)) {
8235 return;
8236 }
8237
8238 assert(!excp_is_internal(cs->exception_index));
8239 if (arm_el_is_aa64(env, new_el)) {
8240 arm_cpu_do_interrupt_aarch64(cs);
8241 } else {
8242 arm_cpu_do_interrupt_aarch32(cs);
8243 }
8244
8245 /* Hooks may change global state so BQL should be held, also the
8246 * BQL needs to be held for any modification of
8247 * cs->interrupt_request.
8248 */
8249 g_assert(qemu_mutex_iothread_locked());
8250
8251 arm_call_el_change_hook(cpu);
8252
8253 if (!kvm_enabled()) {
8254 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8255 }
8256 }
8257
8258 /* Return the exception level which controls this address translation regime */
8259 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8260 {
8261 switch (mmu_idx) {
8262 case ARMMMUIdx_S2NS:
8263 case ARMMMUIdx_S1E2:
8264 return 2;
8265 case ARMMMUIdx_S1E3:
8266 return 3;
8267 case ARMMMUIdx_S1SE0:
8268 return arm_el_is_aa64(env, 3) ? 1 : 3;
8269 case ARMMMUIdx_S1SE1:
8270 case ARMMMUIdx_S1NSE0:
8271 case ARMMMUIdx_S1NSE1:
8272 case ARMMMUIdx_MPrivNegPri:
8273 case ARMMMUIdx_MUserNegPri:
8274 case ARMMMUIdx_MPriv:
8275 case ARMMMUIdx_MUser:
8276 case ARMMMUIdx_MSPrivNegPri:
8277 case ARMMMUIdx_MSUserNegPri:
8278 case ARMMMUIdx_MSPriv:
8279 case ARMMMUIdx_MSUser:
8280 return 1;
8281 default:
8282 g_assert_not_reached();
8283 }
8284 }
8285
8286 /* Return the SCTLR value which controls this address translation regime */
8287 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8288 {
8289 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8290 }
8291
8292 /* Return true if the specified stage of address translation is disabled */
8293 static inline bool regime_translation_disabled(CPUARMState *env,
8294 ARMMMUIdx mmu_idx)
8295 {
8296 if (arm_feature(env, ARM_FEATURE_M)) {
8297 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
8298 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8299 case R_V7M_MPU_CTRL_ENABLE_MASK:
8300 /* Enabled, but not for HardFault and NMI */
8301 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
8302 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8303 /* Enabled for all cases */
8304 return false;
8305 case 0:
8306 default:
8307 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8308 * we warned about that in armv7m_nvic.c when the guest set it.
8309 */
8310 return true;
8311 }
8312 }
8313
8314 if (mmu_idx == ARMMMUIdx_S2NS) {
8315 return (env->cp15.hcr_el2 & HCR_VM) == 0;
8316 }
8317 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8318 }
8319
8320 static inline bool regime_translation_big_endian(CPUARMState *env,
8321 ARMMMUIdx mmu_idx)
8322 {
8323 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8324 }
8325
8326 /* Return the TCR controlling this translation regime */
8327 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8328 {
8329 if (mmu_idx == ARMMMUIdx_S2NS) {
8330 return &env->cp15.vtcr_el2;
8331 }
8332 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8333 }
8334
8335 /* Convert a possible stage1+2 MMU index into the appropriate
8336 * stage 1 MMU index
8337 */
8338 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8339 {
8340 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8341 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8342 }
8343 return mmu_idx;
8344 }
8345
8346 /* Returns TBI0 value for current regime el */
8347 uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
8348 {
8349 TCR *tcr;
8350 uint32_t el;
8351
8352 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8353 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8354 */
8355 mmu_idx = stage_1_mmu_idx(mmu_idx);
8356
8357 tcr = regime_tcr(env, mmu_idx);
8358 el = regime_el(env, mmu_idx);
8359
8360 if (el > 1) {
8361 return extract64(tcr->raw_tcr, 20, 1);
8362 } else {
8363 return extract64(tcr->raw_tcr, 37, 1);
8364 }
8365 }
8366
8367 /* Returns TBI1 value for current regime el */
8368 uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
8369 {
8370 TCR *tcr;
8371 uint32_t el;
8372
8373 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8374 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8375 */
8376 mmu_idx = stage_1_mmu_idx(mmu_idx);
8377
8378 tcr = regime_tcr(env, mmu_idx);
8379 el = regime_el(env, mmu_idx);
8380
8381 if (el > 1) {
8382 return 0;
8383 } else {
8384 return extract64(tcr->raw_tcr, 38, 1);
8385 }
8386 }
8387
8388 /* Return the TTBR associated with this translation regime */
8389 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8390 int ttbrn)
8391 {
8392 if (mmu_idx == ARMMMUIdx_S2NS) {
8393 return env->cp15.vttbr_el2;
8394 }
8395 if (ttbrn == 0) {
8396 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8397 } else {
8398 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8399 }
8400 }
8401
8402 /* Return true if the translation regime is using LPAE format page tables */
8403 static inline bool regime_using_lpae_format(CPUARMState *env,
8404 ARMMMUIdx mmu_idx)
8405 {
8406 int el = regime_el(env, mmu_idx);
8407 if (el == 2 || arm_el_is_aa64(env, el)) {
8408 return true;
8409 }
8410 if (arm_feature(env, ARM_FEATURE_LPAE)
8411 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8412 return true;
8413 }
8414 return false;
8415 }
8416
8417 /* Returns true if the stage 1 translation regime is using LPAE format page
8418 * tables. Used when raising alignment exceptions, whose FSR changes depending
8419 * on whether the long or short descriptor format is in use. */
8420 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
8421 {
8422 mmu_idx = stage_1_mmu_idx(mmu_idx);
8423
8424 return regime_using_lpae_format(env, mmu_idx);
8425 }
8426
8427 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8428 {
8429 switch (mmu_idx) {
8430 case ARMMMUIdx_S1SE0:
8431 case ARMMMUIdx_S1NSE0:
8432 case ARMMMUIdx_MUser:
8433 case ARMMMUIdx_MSUser:
8434 case ARMMMUIdx_MUserNegPri:
8435 case ARMMMUIdx_MSUserNegPri:
8436 return true;
8437 default:
8438 return false;
8439 case ARMMMUIdx_S12NSE0:
8440 case ARMMMUIdx_S12NSE1:
8441 g_assert_not_reached();
8442 }
8443 }
8444
8445 /* Translate section/page access permissions to page
8446 * R/W protection flags
8447 *
8448 * @env: CPUARMState
8449 * @mmu_idx: MMU index indicating required translation regime
8450 * @ap: The 3-bit access permissions (AP[2:0])
8451 * @domain_prot: The 2-bit domain access permissions
8452 */
8453 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8454 int ap, int domain_prot)
8455 {
8456 bool is_user = regime_is_user(env, mmu_idx);
8457
8458 if (domain_prot == 3) {
8459 return PAGE_READ | PAGE_WRITE;
8460 }
8461
8462 switch (ap) {
8463 case 0:
8464 if (arm_feature(env, ARM_FEATURE_V7)) {
8465 return 0;
8466 }
8467 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8468 case SCTLR_S:
8469 return is_user ? 0 : PAGE_READ;
8470 case SCTLR_R:
8471 return PAGE_READ;
8472 default:
8473 return 0;
8474 }
8475 case 1:
8476 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8477 case 2:
8478 if (is_user) {
8479 return PAGE_READ;
8480 } else {
8481 return PAGE_READ | PAGE_WRITE;
8482 }
8483 case 3:
8484 return PAGE_READ | PAGE_WRITE;
8485 case 4: /* Reserved. */
8486 return 0;
8487 case 5:
8488 return is_user ? 0 : PAGE_READ;
8489 case 6:
8490 return PAGE_READ;
8491 case 7:
8492 if (!arm_feature(env, ARM_FEATURE_V6K)) {
8493 return 0;
8494 }
8495 return PAGE_READ;
8496 default:
8497 g_assert_not_reached();
8498 }
8499 }
8500
8501 /* Translate section/page access permissions to page
8502 * R/W protection flags.
8503 *
8504 * @ap: The 2-bit simple AP (AP[2:1])
8505 * @is_user: TRUE if accessing from PL0
8506 */
8507 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
8508 {
8509 switch (ap) {
8510 case 0:
8511 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8512 case 1:
8513 return PAGE_READ | PAGE_WRITE;
8514 case 2:
8515 return is_user ? 0 : PAGE_READ;
8516 case 3:
8517 return PAGE_READ;
8518 default:
8519 g_assert_not_reached();
8520 }
8521 }
8522
8523 static inline int
8524 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8525 {
8526 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8527 }
8528
8529 /* Translate S2 section/page access permissions to protection flags
8530 *
8531 * @env: CPUARMState
8532 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8533 * @xn: XN (execute-never) bit
8534 */
8535 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8536 {
8537 int prot = 0;
8538
8539 if (s2ap & 1) {
8540 prot |= PAGE_READ;
8541 }
8542 if (s2ap & 2) {
8543 prot |= PAGE_WRITE;
8544 }
8545 if (!xn) {
8546 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8547 prot |= PAGE_EXEC;
8548 }
8549 }
8550 return prot;
8551 }
8552
8553 /* Translate section/page access permissions to protection flags
8554 *
8555 * @env: CPUARMState
8556 * @mmu_idx: MMU index indicating required translation regime
8557 * @is_aa64: TRUE if AArch64
8558 * @ap: The 2-bit simple AP (AP[2:1])
8559 * @ns: NS (non-secure) bit
8560 * @xn: XN (execute-never) bit
8561 * @pxn: PXN (privileged execute-never) bit
8562 */
8563 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8564 int ap, int ns, int xn, int pxn)
8565 {
8566 bool is_user = regime_is_user(env, mmu_idx);
8567 int prot_rw, user_rw;
8568 bool have_wxn;
8569 int wxn = 0;
8570
8571 assert(mmu_idx != ARMMMUIdx_S2NS);
8572
8573 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8574 if (is_user) {
8575 prot_rw = user_rw;
8576 } else {
8577 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8578 }
8579
8580 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8581 return prot_rw;
8582 }
8583
8584 /* TODO have_wxn should be replaced with
8585 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8586 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8587 * compatible processors have EL2, which is required for [U]WXN.
8588 */
8589 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8590
8591 if (have_wxn) {
8592 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8593 }
8594
8595 if (is_aa64) {
8596 switch (regime_el(env, mmu_idx)) {
8597 case 1:
8598 if (!is_user) {
8599 xn = pxn || (user_rw & PAGE_WRITE);
8600 }
8601 break;
8602 case 2:
8603 case 3:
8604 break;
8605 }
8606 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8607 switch (regime_el(env, mmu_idx)) {
8608 case 1:
8609 case 3:
8610 if (is_user) {
8611 xn = xn || !(user_rw & PAGE_READ);
8612 } else {
8613 int uwxn = 0;
8614 if (have_wxn) {
8615 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8616 }
8617 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8618 (uwxn && (user_rw & PAGE_WRITE));
8619 }
8620 break;
8621 case 2:
8622 break;
8623 }
8624 } else {
8625 xn = wxn = 0;
8626 }
8627
8628 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8629 return prot_rw;
8630 }
8631 return prot_rw | PAGE_EXEC;
8632 }
8633
8634 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8635 uint32_t *table, uint32_t address)
8636 {
8637 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
8638 TCR *tcr = regime_tcr(env, mmu_idx);
8639
8640 if (address & tcr->mask) {
8641 if (tcr->raw_tcr & TTBCR_PD1) {
8642 /* Translation table walk disabled for TTBR1 */
8643 return false;
8644 }
8645 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
8646 } else {
8647 if (tcr->raw_tcr & TTBCR_PD0) {
8648 /* Translation table walk disabled for TTBR0 */
8649 return false;
8650 }
8651 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
8652 }
8653 *table |= (address >> 18) & 0x3ffc;
8654 return true;
8655 }
8656
8657 /* Translate a S1 pagetable walk through S2 if needed. */
8658 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8659 hwaddr addr, MemTxAttrs txattrs,
8660 ARMMMUFaultInfo *fi)
8661 {
8662 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8663 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8664 target_ulong s2size;
8665 hwaddr s2pa;
8666 int s2prot;
8667 int ret;
8668
8669 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
8670 &txattrs, &s2prot, &s2size, fi, NULL);
8671 if (ret) {
8672 assert(fi->type != ARMFault_None);
8673 fi->s2addr = addr;
8674 fi->stage2 = true;
8675 fi->s1ptw = true;
8676 return ~0;
8677 }
8678 addr = s2pa;
8679 }
8680 return addr;
8681 }
8682
8683 /* All loads done in the course of a page table walk go through here.
8684 * TODO: rather than ignoring errors from physical memory reads (which
8685 * are external aborts in ARM terminology) we should propagate this
8686 * error out so that we can turn it into a Data Abort if this walk
8687 * was being done for a CPU load/store or an address translation instruction
8688 * (but not if it was for a debug access).
8689 */
8690 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8691 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8692 {
8693 ARMCPU *cpu = ARM_CPU(cs);
8694 CPUARMState *env = &cpu->env;
8695 MemTxAttrs attrs = {};
8696 MemTxResult result = MEMTX_OK;
8697 AddressSpace *as;
8698 uint32_t data;
8699
8700 attrs.secure = is_secure;
8701 as = arm_addressspace(cs, attrs);
8702 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8703 if (fi->s1ptw) {
8704 return 0;
8705 }
8706 if (regime_translation_big_endian(env, mmu_idx)) {
8707 data = address_space_ldl_be(as, addr, attrs, &result);
8708 } else {
8709 data = address_space_ldl_le(as, addr, attrs, &result);
8710 }
8711 if (result == MEMTX_OK) {
8712 return data;
8713 }
8714 fi->type = ARMFault_SyncExternalOnWalk;
8715 fi->ea = arm_extabort_type(result);
8716 return 0;
8717 }
8718
8719 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8720 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8721 {
8722 ARMCPU *cpu = ARM_CPU(cs);
8723 CPUARMState *env = &cpu->env;
8724 MemTxAttrs attrs = {};
8725 MemTxResult result = MEMTX_OK;
8726 AddressSpace *as;
8727 uint64_t data;
8728
8729 attrs.secure = is_secure;
8730 as = arm_addressspace(cs, attrs);
8731 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8732 if (fi->s1ptw) {
8733 return 0;
8734 }
8735 if (regime_translation_big_endian(env, mmu_idx)) {
8736 data = address_space_ldq_be(as, addr, attrs, &result);
8737 } else {
8738 data = address_space_ldq_le(as, addr, attrs, &result);
8739 }
8740 if (result == MEMTX_OK) {
8741 return data;
8742 }
8743 fi->type = ARMFault_SyncExternalOnWalk;
8744 fi->ea = arm_extabort_type(result);
8745 return 0;
8746 }
8747
8748 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
8749 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8750 hwaddr *phys_ptr, int *prot,
8751 target_ulong *page_size,
8752 ARMMMUFaultInfo *fi)
8753 {
8754 CPUState *cs = CPU(arm_env_get_cpu(env));
8755 int level = 1;
8756 uint32_t table;
8757 uint32_t desc;
8758 int type;
8759 int ap;
8760 int domain = 0;
8761 int domain_prot;
8762 hwaddr phys_addr;
8763 uint32_t dacr;
8764
8765 /* Pagetable walk. */
8766 /* Lookup l1 descriptor. */
8767 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8768 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8769 fi->type = ARMFault_Translation;
8770 goto do_fault;
8771 }
8772 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8773 mmu_idx, fi);
8774 if (fi->type != ARMFault_None) {
8775 goto do_fault;
8776 }
8777 type = (desc & 3);
8778 domain = (desc >> 5) & 0x0f;
8779 if (regime_el(env, mmu_idx) == 1) {
8780 dacr = env->cp15.dacr_ns;
8781 } else {
8782 dacr = env->cp15.dacr_s;
8783 }
8784 domain_prot = (dacr >> (domain * 2)) & 3;
8785 if (type == 0) {
8786 /* Section translation fault. */
8787 fi->type = ARMFault_Translation;
8788 goto do_fault;
8789 }
8790 if (type != 2) {
8791 level = 2;
8792 }
8793 if (domain_prot == 0 || domain_prot == 2) {
8794 fi->type = ARMFault_Domain;
8795 goto do_fault;
8796 }
8797 if (type == 2) {
8798 /* 1Mb section. */
8799 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8800 ap = (desc >> 10) & 3;
8801 *page_size = 1024 * 1024;
8802 } else {
8803 /* Lookup l2 entry. */
8804 if (type == 1) {
8805 /* Coarse pagetable. */
8806 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8807 } else {
8808 /* Fine pagetable. */
8809 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8810 }
8811 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8812 mmu_idx, fi);
8813 if (fi->type != ARMFault_None) {
8814 goto do_fault;
8815 }
8816 switch (desc & 3) {
8817 case 0: /* Page translation fault. */
8818 fi->type = ARMFault_Translation;
8819 goto do_fault;
8820 case 1: /* 64k page. */
8821 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8822 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
8823 *page_size = 0x10000;
8824 break;
8825 case 2: /* 4k page. */
8826 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8827 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
8828 *page_size = 0x1000;
8829 break;
8830 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
8831 if (type == 1) {
8832 /* ARMv6/XScale extended small page format */
8833 if (arm_feature(env, ARM_FEATURE_XSCALE)
8834 || arm_feature(env, ARM_FEATURE_V6)) {
8835 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8836 *page_size = 0x1000;
8837 } else {
8838 /* UNPREDICTABLE in ARMv5; we choose to take a
8839 * page translation fault.
8840 */
8841 fi->type = ARMFault_Translation;
8842 goto do_fault;
8843 }
8844 } else {
8845 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
8846 *page_size = 0x400;
8847 }
8848 ap = (desc >> 4) & 3;
8849 break;
8850 default:
8851 /* Never happens, but compiler isn't smart enough to tell. */
8852 abort();
8853 }
8854 }
8855 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8856 *prot |= *prot ? PAGE_EXEC : 0;
8857 if (!(*prot & (1 << access_type))) {
8858 /* Access permission fault. */
8859 fi->type = ARMFault_Permission;
8860 goto do_fault;
8861 }
8862 *phys_ptr = phys_addr;
8863 return false;
8864 do_fault:
8865 fi->domain = domain;
8866 fi->level = level;
8867 return true;
8868 }
8869
8870 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
8871 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8872 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
8873 target_ulong *page_size, ARMMMUFaultInfo *fi)
8874 {
8875 CPUState *cs = CPU(arm_env_get_cpu(env));
8876 int level = 1;
8877 uint32_t table;
8878 uint32_t desc;
8879 uint32_t xn;
8880 uint32_t pxn = 0;
8881 int type;
8882 int ap;
8883 int domain = 0;
8884 int domain_prot;
8885 hwaddr phys_addr;
8886 uint32_t dacr;
8887 bool ns;
8888
8889 /* Pagetable walk. */
8890 /* Lookup l1 descriptor. */
8891 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8892 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8893 fi->type = ARMFault_Translation;
8894 goto do_fault;
8895 }
8896 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8897 mmu_idx, fi);
8898 if (fi->type != ARMFault_None) {
8899 goto do_fault;
8900 }
8901 type = (desc & 3);
8902 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8903 /* Section translation fault, or attempt to use the encoding
8904 * which is Reserved on implementations without PXN.
8905 */
8906 fi->type = ARMFault_Translation;
8907 goto do_fault;
8908 }
8909 if ((type == 1) || !(desc & (1 << 18))) {
8910 /* Page or Section. */
8911 domain = (desc >> 5) & 0x0f;
8912 }
8913 if (regime_el(env, mmu_idx) == 1) {
8914 dacr = env->cp15.dacr_ns;
8915 } else {
8916 dacr = env->cp15.dacr_s;
8917 }
8918 if (type == 1) {
8919 level = 2;
8920 }
8921 domain_prot = (dacr >> (domain * 2)) & 3;
8922 if (domain_prot == 0 || domain_prot == 2) {
8923 /* Section or Page domain fault */
8924 fi->type = ARMFault_Domain;
8925 goto do_fault;
8926 }
8927 if (type != 1) {
8928 if (desc & (1 << 18)) {
8929 /* Supersection. */
8930 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
8931 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8932 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
8933 *page_size = 0x1000000;
8934 } else {
8935 /* Section. */
8936 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8937 *page_size = 0x100000;
8938 }
8939 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8940 xn = desc & (1 << 4);
8941 pxn = desc & 1;
8942 ns = extract32(desc, 19, 1);
8943 } else {
8944 if (arm_feature(env, ARM_FEATURE_PXN)) {
8945 pxn = (desc >> 2) & 1;
8946 }
8947 ns = extract32(desc, 3, 1);
8948 /* Lookup l2 entry. */
8949 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8950 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8951 mmu_idx, fi);
8952 if (fi->type != ARMFault_None) {
8953 goto do_fault;
8954 }
8955 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8956 switch (desc & 3) {
8957 case 0: /* Page translation fault. */
8958 fi->type = ARMFault_Translation;
8959 goto do_fault;
8960 case 1: /* 64k page. */
8961 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8962 xn = desc & (1 << 15);
8963 *page_size = 0x10000;
8964 break;
8965 case 2: case 3: /* 4k page. */
8966 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8967 xn = desc & 1;
8968 *page_size = 0x1000;
8969 break;
8970 default:
8971 /* Never happens, but compiler isn't smart enough to tell. */
8972 abort();
8973 }
8974 }
8975 if (domain_prot == 3) {
8976 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8977 } else {
8978 if (pxn && !regime_is_user(env, mmu_idx)) {
8979 xn = 1;
8980 }
8981 if (xn && access_type == MMU_INST_FETCH) {
8982 fi->type = ARMFault_Permission;
8983 goto do_fault;
8984 }
8985
8986 if (arm_feature(env, ARM_FEATURE_V6K) &&
8987 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
8988 /* The simplified model uses AP[0] as an access control bit. */
8989 if ((ap & 1) == 0) {
8990 /* Access flag fault. */
8991 fi->type = ARMFault_AccessFlag;
8992 goto do_fault;
8993 }
8994 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
8995 } else {
8996 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8997 }
8998 if (*prot && !xn) {
8999 *prot |= PAGE_EXEC;
9000 }
9001 if (!(*prot & (1 << access_type))) {
9002 /* Access permission fault. */
9003 fi->type = ARMFault_Permission;
9004 goto do_fault;
9005 }
9006 }
9007 if (ns) {
9008 /* The NS bit will (as required by the architecture) have no effect if
9009 * the CPU doesn't support TZ or this is a non-secure translation
9010 * regime, because the attribute will already be non-secure.
9011 */
9012 attrs->secure = false;
9013 }
9014 *phys_ptr = phys_addr;
9015 return false;
9016 do_fault:
9017 fi->domain = domain;
9018 fi->level = level;
9019 return true;
9020 }
9021
9022 /*
9023 * check_s2_mmu_setup
9024 * @cpu: ARMCPU
9025 * @is_aa64: True if the translation regime is in AArch64 state
9026 * @startlevel: Suggested starting level
9027 * @inputsize: Bitsize of IPAs
9028 * @stride: Page-table stride (See the ARM ARM)
9029 *
9030 * Returns true if the suggested S2 translation parameters are OK and
9031 * false otherwise.
9032 */
9033 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9034 int inputsize, int stride)
9035 {
9036 const int grainsize = stride + 3;
9037 int startsizecheck;
9038
9039 /* Negative levels are never allowed. */
9040 if (level < 0) {
9041 return false;
9042 }
9043
9044 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9045 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9046 return false;
9047 }
9048
9049 if (is_aa64) {
9050 CPUARMState *env = &cpu->env;
9051 unsigned int pamax = arm_pamax(cpu);
9052
9053 switch (stride) {
9054 case 13: /* 64KB Pages. */
9055 if (level == 0 || (level == 1 && pamax <= 42)) {
9056 return false;
9057 }
9058 break;
9059 case 11: /* 16KB Pages. */
9060 if (level == 0 || (level == 1 && pamax <= 40)) {
9061 return false;
9062 }
9063 break;
9064 case 9: /* 4KB Pages. */
9065 if (level == 0 && pamax <= 42) {
9066 return false;
9067 }
9068 break;
9069 default:
9070 g_assert_not_reached();
9071 }
9072
9073 /* Inputsize checks. */
9074 if (inputsize > pamax &&
9075 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9076 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9077 return false;
9078 }
9079 } else {
9080 /* AArch32 only supports 4KB pages. Assert on that. */
9081 assert(stride == 9);
9082
9083 if (level == 0) {
9084 return false;
9085 }
9086 }
9087 return true;
9088 }
9089
9090 /* Translate from the 4-bit stage 2 representation of
9091 * memory attributes (without cache-allocation hints) to
9092 * the 8-bit representation of the stage 1 MAIR registers
9093 * (which includes allocation hints).
9094 *
9095 * ref: shared/translation/attrs/S2AttrDecode()
9096 * .../S2ConvertAttrsHints()
9097 */
9098 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9099 {
9100 uint8_t hiattr = extract32(s2attrs, 2, 2);
9101 uint8_t loattr = extract32(s2attrs, 0, 2);
9102 uint8_t hihint = 0, lohint = 0;
9103
9104 if (hiattr != 0) { /* normal memory */
9105 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9106 hiattr = loattr = 1; /* non-cacheable */
9107 } else {
9108 if (hiattr != 1) { /* Write-through or write-back */
9109 hihint = 3; /* RW allocate */
9110 }
9111 if (loattr != 1) { /* Write-through or write-back */
9112 lohint = 3; /* RW allocate */
9113 }
9114 }
9115 }
9116
9117 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9118 }
9119
9120 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
9121 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9122 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
9123 target_ulong *page_size_ptr,
9124 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9125 {
9126 ARMCPU *cpu = arm_env_get_cpu(env);
9127 CPUState *cs = CPU(cpu);
9128 /* Read an LPAE long-descriptor translation table. */
9129 ARMFaultType fault_type = ARMFault_Translation;
9130 uint32_t level;
9131 uint32_t epd = 0;
9132 int32_t t0sz, t1sz;
9133 uint32_t tg;
9134 uint64_t ttbr;
9135 int ttbr_select;
9136 hwaddr descaddr, indexmask, indexmask_grainsize;
9137 uint32_t tableattrs;
9138 target_ulong page_size;
9139 uint32_t attrs;
9140 int32_t stride = 9;
9141 int32_t addrsize;
9142 int inputsize;
9143 int32_t tbi = 0;
9144 TCR *tcr = regime_tcr(env, mmu_idx);
9145 int ap, ns, xn, pxn;
9146 uint32_t el = regime_el(env, mmu_idx);
9147 bool ttbr1_valid = true;
9148 uint64_t descaddrmask;
9149 bool aarch64 = arm_el_is_aa64(env, el);
9150
9151 /* TODO:
9152 * This code does not handle the different format TCR for VTCR_EL2.
9153 * This code also does not support shareability levels.
9154 * Attribute and permission bit handling should also be checked when adding
9155 * support for those page table walks.
9156 */
9157 if (aarch64) {
9158 level = 0;
9159 addrsize = 64;
9160 if (el > 1) {
9161 if (mmu_idx != ARMMMUIdx_S2NS) {
9162 tbi = extract64(tcr->raw_tcr, 20, 1);
9163 }
9164 } else {
9165 if (extract64(address, 55, 1)) {
9166 tbi = extract64(tcr->raw_tcr, 38, 1);
9167 } else {
9168 tbi = extract64(tcr->raw_tcr, 37, 1);
9169 }
9170 }
9171 tbi *= 8;
9172
9173 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9174 * invalid.
9175 */
9176 if (el > 1) {
9177 ttbr1_valid = false;
9178 }
9179 } else {
9180 level = 1;
9181 addrsize = 32;
9182 /* There is no TTBR1 for EL2 */
9183 if (el == 2) {
9184 ttbr1_valid = false;
9185 }
9186 }
9187
9188 /* Determine whether this address is in the region controlled by
9189 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
9190 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
9191 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
9192 */
9193 if (aarch64) {
9194 /* AArch64 translation. */
9195 t0sz = extract32(tcr->raw_tcr, 0, 6);
9196 t0sz = MIN(t0sz, 39);
9197 t0sz = MAX(t0sz, 16);
9198 } else if (mmu_idx != ARMMMUIdx_S2NS) {
9199 /* AArch32 stage 1 translation. */
9200 t0sz = extract32(tcr->raw_tcr, 0, 3);
9201 } else {
9202 /* AArch32 stage 2 translation. */
9203 bool sext = extract32(tcr->raw_tcr, 4, 1);
9204 bool sign = extract32(tcr->raw_tcr, 3, 1);
9205 /* Address size is 40-bit for a stage 2 translation,
9206 * and t0sz can be negative (from -8 to 7),
9207 * so we need to adjust it to use the TTBR selecting logic below.
9208 */
9209 addrsize = 40;
9210 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
9211
9212 /* If the sign-extend bit is not the same as t0sz[3], the result
9213 * is unpredictable. Flag this as a guest error. */
9214 if (sign != sext) {
9215 qemu_log_mask(LOG_GUEST_ERROR,
9216 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
9217 }
9218 }
9219 t1sz = extract32(tcr->raw_tcr, 16, 6);
9220 if (aarch64) {
9221 t1sz = MIN(t1sz, 39);
9222 t1sz = MAX(t1sz, 16);
9223 }
9224 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
9225 /* there is a ttbr0 region and we are in it (high bits all zero) */
9226 ttbr_select = 0;
9227 } else if (ttbr1_valid && t1sz &&
9228 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
9229 /* there is a ttbr1 region and we are in it (high bits all one) */
9230 ttbr_select = 1;
9231 } else if (!t0sz) {
9232 /* ttbr0 region is "everything not in the ttbr1 region" */
9233 ttbr_select = 0;
9234 } else if (!t1sz && ttbr1_valid) {
9235 /* ttbr1 region is "everything not in the ttbr0 region" */
9236 ttbr_select = 1;
9237 } else {
9238 /* in the gap between the two regions, this is a Translation fault */
9239 fault_type = ARMFault_Translation;
9240 goto do_fault;
9241 }
9242
9243 /* Note that QEMU ignores shareability and cacheability attributes,
9244 * so we don't need to do anything with the SH, ORGN, IRGN fields
9245 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9246 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9247 * implement any ASID-like capability so we can ignore it (instead
9248 * we will always flush the TLB any time the ASID is changed).
9249 */
9250 if (ttbr_select == 0) {
9251 ttbr = regime_ttbr(env, mmu_idx, 0);
9252 if (el < 2) {
9253 epd = extract32(tcr->raw_tcr, 7, 1);
9254 }
9255 inputsize = addrsize - t0sz;
9256
9257 tg = extract32(tcr->raw_tcr, 14, 2);
9258 if (tg == 1) { /* 64KB pages */
9259 stride = 13;
9260 }
9261 if (tg == 2) { /* 16KB pages */
9262 stride = 11;
9263 }
9264 } else {
9265 /* We should only be here if TTBR1 is valid */
9266 assert(ttbr1_valid);
9267
9268 ttbr = regime_ttbr(env, mmu_idx, 1);
9269 epd = extract32(tcr->raw_tcr, 23, 1);
9270 inputsize = addrsize - t1sz;
9271
9272 tg = extract32(tcr->raw_tcr, 30, 2);
9273 if (tg == 3) { /* 64KB pages */
9274 stride = 13;
9275 }
9276 if (tg == 1) { /* 16KB pages */
9277 stride = 11;
9278 }
9279 }
9280
9281 /* Here we should have set up all the parameters for the translation:
9282 * inputsize, ttbr, epd, stride, tbi
9283 */
9284
9285 if (epd) {
9286 /* Translation table walk disabled => Translation fault on TLB miss
9287 * Note: This is always 0 on 64-bit EL2 and EL3.
9288 */
9289 goto do_fault;
9290 }
9291
9292 if (mmu_idx != ARMMMUIdx_S2NS) {
9293 /* The starting level depends on the virtual address size (which can
9294 * be up to 48 bits) and the translation granule size. It indicates
9295 * the number of strides (stride bits at a time) needed to
9296 * consume the bits of the input address. In the pseudocode this is:
9297 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9298 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9299 * our 'stride + 3' and 'stride' is our 'stride'.
9300 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9301 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9302 * = 4 - (inputsize - 4) / stride;
9303 */
9304 level = 4 - (inputsize - 4) / stride;
9305 } else {
9306 /* For stage 2 translations the starting level is specified by the
9307 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9308 */
9309 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9310 uint32_t startlevel;
9311 bool ok;
9312
9313 if (!aarch64 || stride == 9) {
9314 /* AArch32 or 4KB pages */
9315 startlevel = 2 - sl0;
9316 } else {
9317 /* 16KB or 64KB pages */
9318 startlevel = 3 - sl0;
9319 }
9320
9321 /* Check that the starting level is valid. */
9322 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
9323 inputsize, stride);
9324 if (!ok) {
9325 fault_type = ARMFault_Translation;
9326 goto do_fault;
9327 }
9328 level = startlevel;
9329 }
9330
9331 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9332 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
9333
9334 /* Now we can extract the actual base address from the TTBR */
9335 descaddr = extract64(ttbr, 0, 48);
9336 descaddr &= ~indexmask;
9337
9338 /* The address field in the descriptor goes up to bit 39 for ARMv7
9339 * but up to bit 47 for ARMv8, but we use the descaddrmask
9340 * up to bit 39 for AArch32, because we don't need other bits in that case
9341 * to construct next descriptor address (anyway they should be all zeroes).
9342 */
9343 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
9344 ~indexmask_grainsize;
9345
9346 /* Secure accesses start with the page table in secure memory and
9347 * can be downgraded to non-secure at any step. Non-secure accesses
9348 * remain non-secure. We implement this by just ORing in the NSTable/NS
9349 * bits at each step.
9350 */
9351 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
9352 for (;;) {
9353 uint64_t descriptor;
9354 bool nstable;
9355
9356 descaddr |= (address >> (stride * (4 - level))) & indexmask;
9357 descaddr &= ~7ULL;
9358 nstable = extract32(tableattrs, 4, 1);
9359 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
9360 if (fi->type != ARMFault_None) {
9361 goto do_fault;
9362 }
9363
9364 if (!(descriptor & 1) ||
9365 (!(descriptor & 2) && (level == 3))) {
9366 /* Invalid, or the Reserved level 3 encoding */
9367 goto do_fault;
9368 }
9369 descaddr = descriptor & descaddrmask;
9370
9371 if ((descriptor & 2) && (level < 3)) {
9372 /* Table entry. The top five bits are attributes which may
9373 * propagate down through lower levels of the table (and
9374 * which are all arranged so that 0 means "no effect", so
9375 * we can gather them up by ORing in the bits at each level).
9376 */
9377 tableattrs |= extract64(descriptor, 59, 5);
9378 level++;
9379 indexmask = indexmask_grainsize;
9380 continue;
9381 }
9382 /* Block entry at level 1 or 2, or page entry at level 3.
9383 * These are basically the same thing, although the number
9384 * of bits we pull in from the vaddr varies.
9385 */
9386 page_size = (1ULL << ((stride * (4 - level)) + 3));
9387 descaddr |= (address & (page_size - 1));
9388 /* Extract attributes from the descriptor */
9389 attrs = extract64(descriptor, 2, 10)
9390 | (extract64(descriptor, 52, 12) << 10);
9391
9392 if (mmu_idx == ARMMMUIdx_S2NS) {
9393 /* Stage 2 table descriptors do not include any attribute fields */
9394 break;
9395 }
9396 /* Merge in attributes from table descriptors */
9397 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
9398 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
9399 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9400 * means "force PL1 access only", which means forcing AP[1] to 0.
9401 */
9402 if (extract32(tableattrs, 2, 1)) {
9403 attrs &= ~(1 << 4);
9404 }
9405 attrs |= nstable << 3; /* NS */
9406 break;
9407 }
9408 /* Here descaddr is the final physical address, and attributes
9409 * are all in attrs.
9410 */
9411 fault_type = ARMFault_AccessFlag;
9412 if ((attrs & (1 << 8)) == 0) {
9413 /* Access flag */
9414 goto do_fault;
9415 }
9416
9417 ap = extract32(attrs, 4, 2);
9418 xn = extract32(attrs, 12, 1);
9419
9420 if (mmu_idx == ARMMMUIdx_S2NS) {
9421 ns = true;
9422 *prot = get_S2prot(env, ap, xn);
9423 } else {
9424 ns = extract32(attrs, 3, 1);
9425 pxn = extract32(attrs, 11, 1);
9426 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
9427 }
9428
9429 fault_type = ARMFault_Permission;
9430 if (!(*prot & (1 << access_type))) {
9431 goto do_fault;
9432 }
9433
9434 if (ns) {
9435 /* The NS bit will (as required by the architecture) have no effect if
9436 * the CPU doesn't support TZ or this is a non-secure translation
9437 * regime, because the attribute will already be non-secure.
9438 */
9439 txattrs->secure = false;
9440 }
9441
9442 if (cacheattrs != NULL) {
9443 if (mmu_idx == ARMMMUIdx_S2NS) {
9444 cacheattrs->attrs = convert_stage2_attrs(env,
9445 extract32(attrs, 0, 4));
9446 } else {
9447 /* Index into MAIR registers for cache attributes */
9448 uint8_t attrindx = extract32(attrs, 0, 3);
9449 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9450 assert(attrindx <= 7);
9451 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9452 }
9453 cacheattrs->shareability = extract32(attrs, 6, 2);
9454 }
9455
9456 *phys_ptr = descaddr;
9457 *page_size_ptr = page_size;
9458 return false;
9459
9460 do_fault:
9461 fi->type = fault_type;
9462 fi->level = level;
9463 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9464 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
9465 return true;
9466 }
9467
9468 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9469 ARMMMUIdx mmu_idx,
9470 int32_t address, int *prot)
9471 {
9472 if (!arm_feature(env, ARM_FEATURE_M)) {
9473 *prot = PAGE_READ | PAGE_WRITE;
9474 switch (address) {
9475 case 0xF0000000 ... 0xFFFFFFFF:
9476 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9477 /* hivecs execing is ok */
9478 *prot |= PAGE_EXEC;
9479 }
9480 break;
9481 case 0x00000000 ... 0x7FFFFFFF:
9482 *prot |= PAGE_EXEC;
9483 break;
9484 }
9485 } else {
9486 /* Default system address map for M profile cores.
9487 * The architecture specifies which regions are execute-never;
9488 * at the MPU level no other checks are defined.
9489 */
9490 switch (address) {
9491 case 0x00000000 ... 0x1fffffff: /* ROM */
9492 case 0x20000000 ... 0x3fffffff: /* SRAM */
9493 case 0x60000000 ... 0x7fffffff: /* RAM */
9494 case 0x80000000 ... 0x9fffffff: /* RAM */
9495 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9496 break;
9497 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9498 case 0xa0000000 ... 0xbfffffff: /* Device */
9499 case 0xc0000000 ... 0xdfffffff: /* Device */
9500 case 0xe0000000 ... 0xffffffff: /* System */
9501 *prot = PAGE_READ | PAGE_WRITE;
9502 break;
9503 default:
9504 g_assert_not_reached();
9505 }
9506 }
9507 }
9508
9509 static bool pmsav7_use_background_region(ARMCPU *cpu,
9510 ARMMMUIdx mmu_idx, bool is_user)
9511 {
9512 /* Return true if we should use the default memory map as a
9513 * "background" region if there are no hits against any MPU regions.
9514 */
9515 CPUARMState *env = &cpu->env;
9516
9517 if (is_user) {
9518 return false;
9519 }
9520
9521 if (arm_feature(env, ARM_FEATURE_M)) {
9522 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9523 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
9524 } else {
9525 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9526 }
9527 }
9528
9529 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9530 {
9531 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9532 return arm_feature(env, ARM_FEATURE_M) &&
9533 extract32(address, 20, 12) == 0xe00;
9534 }
9535
9536 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9537 {
9538 /* True if address is in the M profile system region
9539 * 0xe0000000 - 0xffffffff
9540 */
9541 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9542 }
9543
9544 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
9545 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9546 hwaddr *phys_ptr, int *prot,
9547 ARMMMUFaultInfo *fi)
9548 {
9549 ARMCPU *cpu = arm_env_get_cpu(env);
9550 int n;
9551 bool is_user = regime_is_user(env, mmu_idx);
9552
9553 *phys_ptr = address;
9554 *prot = 0;
9555
9556 if (regime_translation_disabled(env, mmu_idx) ||
9557 m_is_ppb_region(env, address)) {
9558 /* MPU disabled or M profile PPB access: use default memory map.
9559 * The other case which uses the default memory map in the
9560 * v7M ARM ARM pseudocode is exception vector reads from the vector
9561 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9562 * which always does a direct read using address_space_ldl(), rather
9563 * than going via this function, so we don't need to check that here.
9564 */
9565 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9566 } else { /* MPU enabled */
9567 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9568 /* region search */
9569 uint32_t base = env->pmsav7.drbar[n];
9570 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9571 uint32_t rmask;
9572 bool srdis = false;
9573
9574 if (!(env->pmsav7.drsr[n] & 0x1)) {
9575 continue;
9576 }
9577
9578 if (!rsize) {
9579 qemu_log_mask(LOG_GUEST_ERROR,
9580 "DRSR[%d]: Rsize field cannot be 0\n", n);
9581 continue;
9582 }
9583 rsize++;
9584 rmask = (1ull << rsize) - 1;
9585
9586 if (base & rmask) {
9587 qemu_log_mask(LOG_GUEST_ERROR,
9588 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9589 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9590 n, base, rmask);
9591 continue;
9592 }
9593
9594 if (address < base || address > base + rmask) {
9595 continue;
9596 }
9597
9598 /* Region matched */
9599
9600 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9601 int i, snd;
9602 uint32_t srdis_mask;
9603
9604 rsize -= 3; /* sub region size (power of 2) */
9605 snd = ((address - base) >> rsize) & 0x7;
9606 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9607
9608 srdis_mask = srdis ? 0x3 : 0x0;
9609 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9610 /* This will check in groups of 2, 4 and then 8, whether
9611 * the subregion bits are consistent. rsize is incremented
9612 * back up to give the region size, considering consistent
9613 * adjacent subregions as one region. Stop testing if rsize
9614 * is already big enough for an entire QEMU page.
9615 */
9616 int snd_rounded = snd & ~(i - 1);
9617 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9618 snd_rounded + 8, i);
9619 if (srdis_mask ^ srdis_multi) {
9620 break;
9621 }
9622 srdis_mask = (srdis_mask << i) | srdis_mask;
9623 rsize++;
9624 }
9625 }
9626 if (rsize < TARGET_PAGE_BITS) {
9627 qemu_log_mask(LOG_UNIMP,
9628 "DRSR[%d]: No support for MPU (sub)region "
9629 "alignment of %" PRIu32 " bits. Minimum is %d\n",
9630 n, rsize, TARGET_PAGE_BITS);
9631 continue;
9632 }
9633 if (srdis) {
9634 continue;
9635 }
9636 break;
9637 }
9638
9639 if (n == -1) { /* no hits */
9640 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9641 /* background fault */
9642 fi->type = ARMFault_Background;
9643 return true;
9644 }
9645 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9646 } else { /* a MPU hit! */
9647 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
9648 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9649
9650 if (m_is_system_region(env, address)) {
9651 /* System space is always execute never */
9652 xn = 1;
9653 }
9654
9655 if (is_user) { /* User mode AP bit decoding */
9656 switch (ap) {
9657 case 0:
9658 case 1:
9659 case 5:
9660 break; /* no access */
9661 case 3:
9662 *prot |= PAGE_WRITE;
9663 /* fall through */
9664 case 2:
9665 case 6:
9666 *prot |= PAGE_READ | PAGE_EXEC;
9667 break;
9668 case 7:
9669 /* for v7M, same as 6; for R profile a reserved value */
9670 if (arm_feature(env, ARM_FEATURE_M)) {
9671 *prot |= PAGE_READ | PAGE_EXEC;
9672 break;
9673 }
9674 /* fall through */
9675 default:
9676 qemu_log_mask(LOG_GUEST_ERROR,
9677 "DRACR[%d]: Bad value for AP bits: 0x%"
9678 PRIx32 "\n", n, ap);
9679 }
9680 } else { /* Priv. mode AP bits decoding */
9681 switch (ap) {
9682 case 0:
9683 break; /* no access */
9684 case 1:
9685 case 2:
9686 case 3:
9687 *prot |= PAGE_WRITE;
9688 /* fall through */
9689 case 5:
9690 case 6:
9691 *prot |= PAGE_READ | PAGE_EXEC;
9692 break;
9693 case 7:
9694 /* for v7M, same as 6; for R profile a reserved value */
9695 if (arm_feature(env, ARM_FEATURE_M)) {
9696 *prot |= PAGE_READ | PAGE_EXEC;
9697 break;
9698 }
9699 /* fall through */
9700 default:
9701 qemu_log_mask(LOG_GUEST_ERROR,
9702 "DRACR[%d]: Bad value for AP bits: 0x%"
9703 PRIx32 "\n", n, ap);
9704 }
9705 }
9706
9707 /* execute never */
9708 if (xn) {
9709 *prot &= ~PAGE_EXEC;
9710 }
9711 }
9712 }
9713
9714 fi->type = ARMFault_Permission;
9715 fi->level = 1;
9716 return !(*prot & (1 << access_type));
9717 }
9718
9719 static bool v8m_is_sau_exempt(CPUARMState *env,
9720 uint32_t address, MMUAccessType access_type)
9721 {
9722 /* The architecture specifies that certain address ranges are
9723 * exempt from v8M SAU/IDAU checks.
9724 */
9725 return
9726 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9727 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9728 (address >= 0xe000e000 && address <= 0xe000efff) ||
9729 (address >= 0xe002e000 && address <= 0xe002efff) ||
9730 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9731 (address >= 0xe00ff000 && address <= 0xe00fffff);
9732 }
9733
9734 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9735 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9736 V8M_SAttributes *sattrs)
9737 {
9738 /* Look up the security attributes for this address. Compare the
9739 * pseudocode SecurityCheck() function.
9740 * We assume the caller has zero-initialized *sattrs.
9741 */
9742 ARMCPU *cpu = arm_env_get_cpu(env);
9743 int r;
9744
9745 /* TODO: implement IDAU */
9746
9747 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9748 /* 0xf0000000..0xffffffff is always S for insn fetches */
9749 return;
9750 }
9751
9752 if (v8m_is_sau_exempt(env, address, access_type)) {
9753 sattrs->ns = !regime_is_secure(env, mmu_idx);
9754 return;
9755 }
9756
9757 switch (env->sau.ctrl & 3) {
9758 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9759 break;
9760 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9761 sattrs->ns = true;
9762 break;
9763 default: /* SAU.ENABLE == 1 */
9764 for (r = 0; r < cpu->sau_sregion; r++) {
9765 if (env->sau.rlar[r] & 1) {
9766 uint32_t base = env->sau.rbar[r] & ~0x1f;
9767 uint32_t limit = env->sau.rlar[r] | 0x1f;
9768
9769 if (base <= address && limit >= address) {
9770 if (sattrs->srvalid) {
9771 /* If we hit in more than one region then we must report
9772 * as Secure, not NS-Callable, with no valid region
9773 * number info.
9774 */
9775 sattrs->ns = false;
9776 sattrs->nsc = false;
9777 sattrs->sregion = 0;
9778 sattrs->srvalid = false;
9779 break;
9780 } else {
9781 if (env->sau.rlar[r] & 2) {
9782 sattrs->nsc = true;
9783 } else {
9784 sattrs->ns = true;
9785 }
9786 sattrs->srvalid = true;
9787 sattrs->sregion = r;
9788 }
9789 }
9790 }
9791 }
9792
9793 /* TODO when we support the IDAU then it may override the result here */
9794 break;
9795 }
9796 }
9797
9798 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9799 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9800 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9801 int *prot, ARMMMUFaultInfo *fi, uint32_t *mregion)
9802 {
9803 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9804 * that a full phys-to-virt translation does).
9805 * mregion is (if not NULL) set to the region number which matched,
9806 * or -1 if no region number is returned (MPU off, address did not
9807 * hit a region, address hit in multiple regions).
9808 */
9809 ARMCPU *cpu = arm_env_get_cpu(env);
9810 bool is_user = regime_is_user(env, mmu_idx);
9811 uint32_t secure = regime_is_secure(env, mmu_idx);
9812 int n;
9813 int matchregion = -1;
9814 bool hit = false;
9815
9816 *phys_ptr = address;
9817 *prot = 0;
9818 if (mregion) {
9819 *mregion = -1;
9820 }
9821
9822 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9823 * was an exception vector read from the vector table (which is always
9824 * done using the default system address map), because those accesses
9825 * are done in arm_v7m_load_vector(), which always does a direct
9826 * read using address_space_ldl(), rather than going via this function.
9827 */
9828 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9829 hit = true;
9830 } else if (m_is_ppb_region(env, address)) {
9831 hit = true;
9832 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9833 hit = true;
9834 } else {
9835 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9836 /* region search */
9837 /* Note that the base address is bits [31:5] from the register
9838 * with bits [4:0] all zeroes, but the limit address is bits
9839 * [31:5] from the register with bits [4:0] all ones.
9840 */
9841 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9842 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
9843
9844 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
9845 /* Region disabled */
9846 continue;
9847 }
9848
9849 if (address < base || address > limit) {
9850 continue;
9851 }
9852
9853 if (hit) {
9854 /* Multiple regions match -- always a failure (unlike
9855 * PMSAv7 where highest-numbered-region wins)
9856 */
9857 fi->type = ARMFault_Permission;
9858 fi->level = 1;
9859 return true;
9860 }
9861
9862 matchregion = n;
9863 hit = true;
9864
9865 if (base & ~TARGET_PAGE_MASK) {
9866 qemu_log_mask(LOG_UNIMP,
9867 "MPU_RBAR[%d]: No support for MPU region base"
9868 "address of 0x%" PRIx32 ". Minimum alignment is "
9869 "%d\n",
9870 n, base, TARGET_PAGE_BITS);
9871 continue;
9872 }
9873 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9874 qemu_log_mask(LOG_UNIMP,
9875 "MPU_RBAR[%d]: No support for MPU region limit"
9876 "address of 0x%" PRIx32 ". Minimum alignment is "
9877 "%d\n",
9878 n, limit, TARGET_PAGE_BITS);
9879 continue;
9880 }
9881 }
9882 }
9883
9884 if (!hit) {
9885 /* background fault */
9886 fi->type = ARMFault_Background;
9887 return true;
9888 }
9889
9890 if (matchregion == -1) {
9891 /* hit using the background region */
9892 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9893 } else {
9894 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9895 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
9896
9897 if (m_is_system_region(env, address)) {
9898 /* System space is always execute never */
9899 xn = 1;
9900 }
9901
9902 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9903 if (*prot && !xn) {
9904 *prot |= PAGE_EXEC;
9905 }
9906 /* We don't need to look the attribute up in the MAIR0/MAIR1
9907 * registers because that only tells us about cacheability.
9908 */
9909 if (mregion) {
9910 *mregion = matchregion;
9911 }
9912 }
9913
9914 fi->type = ARMFault_Permission;
9915 fi->level = 1;
9916 return !(*prot & (1 << access_type));
9917 }
9918
9919
9920 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9921 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9922 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9923 int *prot, ARMMMUFaultInfo *fi)
9924 {
9925 uint32_t secure = regime_is_secure(env, mmu_idx);
9926 V8M_SAttributes sattrs = {};
9927
9928 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9929 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9930 if (access_type == MMU_INST_FETCH) {
9931 /* Instruction fetches always use the MMU bank and the
9932 * transaction attribute determined by the fetch address,
9933 * regardless of CPU state. This is painful for QEMU
9934 * to handle, because it would mean we need to encode
9935 * into the mmu_idx not just the (user, negpri) information
9936 * for the current security state but also that for the
9937 * other security state, which would balloon the number
9938 * of mmu_idx values needed alarmingly.
9939 * Fortunately we can avoid this because it's not actually
9940 * possible to arbitrarily execute code from memory with
9941 * the wrong security attribute: it will always generate
9942 * an exception of some kind or another, apart from the
9943 * special case of an NS CPU executing an SG instruction
9944 * in S&NSC memory. So we always just fail the translation
9945 * here and sort things out in the exception handler
9946 * (including possibly emulating an SG instruction).
9947 */
9948 if (sattrs.ns != !secure) {
9949 if (sattrs.nsc) {
9950 fi->type = ARMFault_QEMU_NSCExec;
9951 } else {
9952 fi->type = ARMFault_QEMU_SFault;
9953 }
9954 *phys_ptr = address;
9955 *prot = 0;
9956 return true;
9957 }
9958 } else {
9959 /* For data accesses we always use the MMU bank indicated
9960 * by the current CPU state, but the security attributes
9961 * might downgrade a secure access to nonsecure.
9962 */
9963 if (sattrs.ns) {
9964 txattrs->secure = false;
9965 } else if (!secure) {
9966 /* NS access to S memory must fault.
9967 * Architecturally we should first check whether the
9968 * MPU information for this address indicates that we
9969 * are doing an unaligned access to Device memory, which
9970 * should generate a UsageFault instead. QEMU does not
9971 * currently check for that kind of unaligned access though.
9972 * If we added it we would need to do so as a special case
9973 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9974 */
9975 fi->type = ARMFault_QEMU_SFault;
9976 *phys_ptr = address;
9977 *prot = 0;
9978 return true;
9979 }
9980 }
9981 }
9982
9983 return pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
9984 txattrs, prot, fi, NULL);
9985 }
9986
9987 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
9988 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9989 hwaddr *phys_ptr, int *prot,
9990 ARMMMUFaultInfo *fi)
9991 {
9992 int n;
9993 uint32_t mask;
9994 uint32_t base;
9995 bool is_user = regime_is_user(env, mmu_idx);
9996
9997 if (regime_translation_disabled(env, mmu_idx)) {
9998 /* MPU disabled. */
9999 *phys_ptr = address;
10000 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10001 return false;
10002 }
10003
10004 *phys_ptr = address;
10005 for (n = 7; n >= 0; n--) {
10006 base = env->cp15.c6_region[n];
10007 if ((base & 1) == 0) {
10008 continue;
10009 }
10010 mask = 1 << ((base >> 1) & 0x1f);
10011 /* Keep this shift separate from the above to avoid an
10012 (undefined) << 32. */
10013 mask = (mask << 1) - 1;
10014 if (((base ^ address) & ~mask) == 0) {
10015 break;
10016 }
10017 }
10018 if (n < 0) {
10019 fi->type = ARMFault_Background;
10020 return true;
10021 }
10022
10023 if (access_type == MMU_INST_FETCH) {
10024 mask = env->cp15.pmsav5_insn_ap;
10025 } else {
10026 mask = env->cp15.pmsav5_data_ap;
10027 }
10028 mask = (mask >> (n * 4)) & 0xf;
10029 switch (mask) {
10030 case 0:
10031 fi->type = ARMFault_Permission;
10032 fi->level = 1;
10033 return true;
10034 case 1:
10035 if (is_user) {
10036 fi->type = ARMFault_Permission;
10037 fi->level = 1;
10038 return true;
10039 }
10040 *prot = PAGE_READ | PAGE_WRITE;
10041 break;
10042 case 2:
10043 *prot = PAGE_READ;
10044 if (!is_user) {
10045 *prot |= PAGE_WRITE;
10046 }
10047 break;
10048 case 3:
10049 *prot = PAGE_READ | PAGE_WRITE;
10050 break;
10051 case 5:
10052 if (is_user) {
10053 fi->type = ARMFault_Permission;
10054 fi->level = 1;
10055 return true;
10056 }
10057 *prot = PAGE_READ;
10058 break;
10059 case 6:
10060 *prot = PAGE_READ;
10061 break;
10062 default:
10063 /* Bad permission. */
10064 fi->type = ARMFault_Permission;
10065 fi->level = 1;
10066 return true;
10067 }
10068 *prot |= PAGE_EXEC;
10069 return false;
10070 }
10071
10072 /* Combine either inner or outer cacheability attributes for normal
10073 * memory, according to table D4-42 and pseudocode procedure
10074 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10075 *
10076 * NB: only stage 1 includes allocation hints (RW bits), leading to
10077 * some asymmetry.
10078 */
10079 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10080 {
10081 if (s1 == 4 || s2 == 4) {
10082 /* non-cacheable has precedence */
10083 return 4;
10084 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10085 /* stage 1 write-through takes precedence */
10086 return s1;
10087 } else if (extract32(s2, 2, 2) == 2) {
10088 /* stage 2 write-through takes precedence, but the allocation hint
10089 * is still taken from stage 1
10090 */
10091 return (2 << 2) | extract32(s1, 0, 2);
10092 } else { /* write-back */
10093 return s1;
10094 }
10095 }
10096
10097 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10098 * and CombineS1S2Desc()
10099 *
10100 * @s1: Attributes from stage 1 walk
10101 * @s2: Attributes from stage 2 walk
10102 */
10103 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10104 {
10105 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10106 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10107 ARMCacheAttrs ret;
10108
10109 /* Combine shareability attributes (table D4-43) */
10110 if (s1.shareability == 2 || s2.shareability == 2) {
10111 /* if either are outer-shareable, the result is outer-shareable */
10112 ret.shareability = 2;
10113 } else if (s1.shareability == 3 || s2.shareability == 3) {
10114 /* if either are inner-shareable, the result is inner-shareable */
10115 ret.shareability = 3;
10116 } else {
10117 /* both non-shareable */
10118 ret.shareability = 0;
10119 }
10120
10121 /* Combine memory type and cacheability attributes */
10122 if (s1hi == 0 || s2hi == 0) {
10123 /* Device has precedence over normal */
10124 if (s1lo == 0 || s2lo == 0) {
10125 /* nGnRnE has precedence over anything */
10126 ret.attrs = 0;
10127 } else if (s1lo == 4 || s2lo == 4) {
10128 /* non-Reordering has precedence over Reordering */
10129 ret.attrs = 4; /* nGnRE */
10130 } else if (s1lo == 8 || s2lo == 8) {
10131 /* non-Gathering has precedence over Gathering */
10132 ret.attrs = 8; /* nGRE */
10133 } else {
10134 ret.attrs = 0xc; /* GRE */
10135 }
10136
10137 /* Any location for which the resultant memory type is any
10138 * type of Device memory is always treated as Outer Shareable.
10139 */
10140 ret.shareability = 2;
10141 } else { /* Normal memory */
10142 /* Outer/inner cacheability combine independently */
10143 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10144 | combine_cacheattr_nibble(s1lo, s2lo);
10145
10146 if (ret.attrs == 0x44) {
10147 /* Any location for which the resultant memory type is Normal
10148 * Inner Non-cacheable, Outer Non-cacheable is always treated
10149 * as Outer Shareable.
10150 */
10151 ret.shareability = 2;
10152 }
10153 }
10154
10155 return ret;
10156 }
10157
10158
10159 /* get_phys_addr - get the physical address for this virtual address
10160 *
10161 * Find the physical address corresponding to the given virtual address,
10162 * by doing a translation table walk on MMU based systems or using the
10163 * MPU state on MPU based systems.
10164 *
10165 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10166 * prot and page_size may not be filled in, and the populated fsr value provides
10167 * information on why the translation aborted, in the format of a
10168 * DFSR/IFSR fault register, with the following caveats:
10169 * * we honour the short vs long DFSR format differences.
10170 * * the WnR bit is never set (the caller must do this).
10171 * * for PSMAv5 based systems we don't bother to return a full FSR format
10172 * value.
10173 *
10174 * @env: CPUARMState
10175 * @address: virtual address to get physical address for
10176 * @access_type: 0 for read, 1 for write, 2 for execute
10177 * @mmu_idx: MMU index indicating required translation regime
10178 * @phys_ptr: set to the physical address corresponding to the virtual address
10179 * @attrs: set to the memory transaction attributes to use
10180 * @prot: set to the permissions for the page containing phys_ptr
10181 * @page_size: set to the size of the page containing phys_ptr
10182 * @fi: set to fault info if the translation fails
10183 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
10184 */
10185 static bool get_phys_addr(CPUARMState *env, target_ulong address,
10186 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10187 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10188 target_ulong *page_size,
10189 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
10190 {
10191 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
10192 /* Call ourselves recursively to do the stage 1 and then stage 2
10193 * translations.
10194 */
10195 if (arm_feature(env, ARM_FEATURE_EL2)) {
10196 hwaddr ipa;
10197 int s2_prot;
10198 int ret;
10199 ARMCacheAttrs cacheattrs2 = {};
10200
10201 ret = get_phys_addr(env, address, access_type,
10202 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
10203 prot, page_size, fi, cacheattrs);
10204
10205 /* If S1 fails or S2 is disabled, return early. */
10206 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10207 *phys_ptr = ipa;
10208 return ret;
10209 }
10210
10211 /* S1 is done. Now do S2 translation. */
10212 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10213 phys_ptr, attrs, &s2_prot,
10214 page_size, fi,
10215 cacheattrs != NULL ? &cacheattrs2 : NULL);
10216 fi->s2addr = ipa;
10217 /* Combine the S1 and S2 perms. */
10218 *prot &= s2_prot;
10219
10220 /* Combine the S1 and S2 cache attributes, if needed */
10221 if (!ret && cacheattrs != NULL) {
10222 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10223 }
10224
10225 return ret;
10226 } else {
10227 /*
10228 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10229 */
10230 mmu_idx = stage_1_mmu_idx(mmu_idx);
10231 }
10232 }
10233
10234 /* The page table entries may downgrade secure to non-secure, but
10235 * cannot upgrade an non-secure translation regime's attributes
10236 * to secure.
10237 */
10238 attrs->secure = regime_is_secure(env, mmu_idx);
10239 attrs->user = regime_is_user(env, mmu_idx);
10240
10241 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10242 * In v7 and earlier it affects all stage 1 translations.
10243 */
10244 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10245 && !arm_feature(env, ARM_FEATURE_V8)) {
10246 if (regime_el(env, mmu_idx) == 3) {
10247 address += env->cp15.fcseidr_s;
10248 } else {
10249 address += env->cp15.fcseidr_ns;
10250 }
10251 }
10252
10253 if (arm_feature(env, ARM_FEATURE_PMSA)) {
10254 bool ret;
10255 *page_size = TARGET_PAGE_SIZE;
10256
10257 if (arm_feature(env, ARM_FEATURE_V8)) {
10258 /* PMSAv8 */
10259 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
10260 phys_ptr, attrs, prot, fi);
10261 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10262 /* PMSAv7 */
10263 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
10264 phys_ptr, prot, fi);
10265 } else {
10266 /* Pre-v7 MPU */
10267 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
10268 phys_ptr, prot, fi);
10269 }
10270 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
10271 " mmu_idx %u -> %s (prot %c%c%c)\n",
10272 access_type == MMU_DATA_LOAD ? "reading" :
10273 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
10274 (uint32_t)address, mmu_idx,
10275 ret ? "Miss" : "Hit",
10276 *prot & PAGE_READ ? 'r' : '-',
10277 *prot & PAGE_WRITE ? 'w' : '-',
10278 *prot & PAGE_EXEC ? 'x' : '-');
10279
10280 return ret;
10281 }
10282
10283 /* Definitely a real MMU, not an MPU */
10284
10285 if (regime_translation_disabled(env, mmu_idx)) {
10286 /* MMU disabled. */
10287 *phys_ptr = address;
10288 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10289 *page_size = TARGET_PAGE_SIZE;
10290 return 0;
10291 }
10292
10293 if (regime_using_lpae_format(env, mmu_idx)) {
10294 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10295 phys_ptr, attrs, prot, page_size,
10296 fi, cacheattrs);
10297 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
10298 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10299 phys_ptr, attrs, prot, page_size, fi);
10300 } else {
10301 return get_phys_addr_v5(env, address, access_type, mmu_idx,
10302 phys_ptr, prot, page_size, fi);
10303 }
10304 }
10305
10306 /* Walk the page table and (if the mapping exists) add the page
10307 * to the TLB. Return false on success, or true on failure. Populate
10308 * fsr with ARM DFSR/IFSR fault register format value on failure.
10309 */
10310 bool arm_tlb_fill(CPUState *cs, vaddr address,
10311 MMUAccessType access_type, int mmu_idx,
10312 ARMMMUFaultInfo *fi)
10313 {
10314 ARMCPU *cpu = ARM_CPU(cs);
10315 CPUARMState *env = &cpu->env;
10316 hwaddr phys_addr;
10317 target_ulong page_size;
10318 int prot;
10319 int ret;
10320 MemTxAttrs attrs = {};
10321
10322 ret = get_phys_addr(env, address, access_type,
10323 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
10324 &attrs, &prot, &page_size, fi, NULL);
10325 if (!ret) {
10326 /* Map a single [sub]page. */
10327 phys_addr &= TARGET_PAGE_MASK;
10328 address &= TARGET_PAGE_MASK;
10329 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
10330 prot, mmu_idx, page_size);
10331 return 0;
10332 }
10333
10334 return ret;
10335 }
10336
10337 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10338 MemTxAttrs *attrs)
10339 {
10340 ARMCPU *cpu = ARM_CPU(cs);
10341 CPUARMState *env = &cpu->env;
10342 hwaddr phys_addr;
10343 target_ulong page_size;
10344 int prot;
10345 bool ret;
10346 ARMMMUFaultInfo fi = {};
10347 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
10348
10349 *attrs = (MemTxAttrs) {};
10350
10351 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
10352 attrs, &prot, &page_size, &fi, NULL);
10353
10354 if (ret) {
10355 return -1;
10356 }
10357 return phys_addr;
10358 }
10359
10360 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
10361 {
10362 uint32_t mask;
10363 unsigned el = arm_current_el(env);
10364
10365 /* First handle registers which unprivileged can read */
10366
10367 switch (reg) {
10368 case 0 ... 7: /* xPSR sub-fields */
10369 mask = 0;
10370 if ((reg & 1) && el) {
10371 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
10372 }
10373 if (!(reg & 4)) {
10374 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
10375 }
10376 /* EPSR reads as zero */
10377 return xpsr_read(env) & mask;
10378 break;
10379 case 20: /* CONTROL */
10380 return env->v7m.control[env->v7m.secure];
10381 case 0x94: /* CONTROL_NS */
10382 /* We have to handle this here because unprivileged Secure code
10383 * can read the NS CONTROL register.
10384 */
10385 if (!env->v7m.secure) {
10386 return 0;
10387 }
10388 return env->v7m.control[M_REG_NS];
10389 }
10390
10391 if (el == 0) {
10392 return 0; /* unprivileged reads others as zero */
10393 }
10394
10395 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10396 switch (reg) {
10397 case 0x88: /* MSP_NS */
10398 if (!env->v7m.secure) {
10399 return 0;
10400 }
10401 return env->v7m.other_ss_msp;
10402 case 0x89: /* PSP_NS */
10403 if (!env->v7m.secure) {
10404 return 0;
10405 }
10406 return env->v7m.other_ss_psp;
10407 case 0x8a: /* MSPLIM_NS */
10408 if (!env->v7m.secure) {
10409 return 0;
10410 }
10411 return env->v7m.msplim[M_REG_NS];
10412 case 0x8b: /* PSPLIM_NS */
10413 if (!env->v7m.secure) {
10414 return 0;
10415 }
10416 return env->v7m.psplim[M_REG_NS];
10417 case 0x90: /* PRIMASK_NS */
10418 if (!env->v7m.secure) {
10419 return 0;
10420 }
10421 return env->v7m.primask[M_REG_NS];
10422 case 0x91: /* BASEPRI_NS */
10423 if (!env->v7m.secure) {
10424 return 0;
10425 }
10426 return env->v7m.basepri[M_REG_NS];
10427 case 0x93: /* FAULTMASK_NS */
10428 if (!env->v7m.secure) {
10429 return 0;
10430 }
10431 return env->v7m.faultmask[M_REG_NS];
10432 case 0x98: /* SP_NS */
10433 {
10434 /* This gives the non-secure SP selected based on whether we're
10435 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10436 */
10437 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10438
10439 if (!env->v7m.secure) {
10440 return 0;
10441 }
10442 if (!arm_v7m_is_handler_mode(env) && spsel) {
10443 return env->v7m.other_ss_psp;
10444 } else {
10445 return env->v7m.other_ss_msp;
10446 }
10447 }
10448 default:
10449 break;
10450 }
10451 }
10452
10453 switch (reg) {
10454 case 8: /* MSP */
10455 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
10456 case 9: /* PSP */
10457 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
10458 case 10: /* MSPLIM */
10459 if (!arm_feature(env, ARM_FEATURE_V8)) {
10460 goto bad_reg;
10461 }
10462 return env->v7m.msplim[env->v7m.secure];
10463 case 11: /* PSPLIM */
10464 if (!arm_feature(env, ARM_FEATURE_V8)) {
10465 goto bad_reg;
10466 }
10467 return env->v7m.psplim[env->v7m.secure];
10468 case 16: /* PRIMASK */
10469 return env->v7m.primask[env->v7m.secure];
10470 case 17: /* BASEPRI */
10471 case 18: /* BASEPRI_MAX */
10472 return env->v7m.basepri[env->v7m.secure];
10473 case 19: /* FAULTMASK */
10474 return env->v7m.faultmask[env->v7m.secure];
10475 default:
10476 bad_reg:
10477 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10478 " register %d\n", reg);
10479 return 0;
10480 }
10481 }
10482
10483 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10484 {
10485 /* We're passed bits [11..0] of the instruction; extract
10486 * SYSm and the mask bits.
10487 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10488 * we choose to treat them as if the mask bits were valid.
10489 * NB that the pseudocode 'mask' variable is bits [11..10],
10490 * whereas ours is [11..8].
10491 */
10492 uint32_t mask = extract32(maskreg, 8, 4);
10493 uint32_t reg = extract32(maskreg, 0, 8);
10494
10495 if (arm_current_el(env) == 0 && reg > 7) {
10496 /* only xPSR sub-fields may be written by unprivileged */
10497 return;
10498 }
10499
10500 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10501 switch (reg) {
10502 case 0x88: /* MSP_NS */
10503 if (!env->v7m.secure) {
10504 return;
10505 }
10506 env->v7m.other_ss_msp = val;
10507 return;
10508 case 0x89: /* PSP_NS */
10509 if (!env->v7m.secure) {
10510 return;
10511 }
10512 env->v7m.other_ss_psp = val;
10513 return;
10514 case 0x8a: /* MSPLIM_NS */
10515 if (!env->v7m.secure) {
10516 return;
10517 }
10518 env->v7m.msplim[M_REG_NS] = val & ~7;
10519 return;
10520 case 0x8b: /* PSPLIM_NS */
10521 if (!env->v7m.secure) {
10522 return;
10523 }
10524 env->v7m.psplim[M_REG_NS] = val & ~7;
10525 return;
10526 case 0x90: /* PRIMASK_NS */
10527 if (!env->v7m.secure) {
10528 return;
10529 }
10530 env->v7m.primask[M_REG_NS] = val & 1;
10531 return;
10532 case 0x91: /* BASEPRI_NS */
10533 if (!env->v7m.secure) {
10534 return;
10535 }
10536 env->v7m.basepri[M_REG_NS] = val & 0xff;
10537 return;
10538 case 0x93: /* FAULTMASK_NS */
10539 if (!env->v7m.secure) {
10540 return;
10541 }
10542 env->v7m.faultmask[M_REG_NS] = val & 1;
10543 return;
10544 case 0x94: /* CONTROL_NS */
10545 if (!env->v7m.secure) {
10546 return;
10547 }
10548 write_v7m_control_spsel_for_secstate(env,
10549 val & R_V7M_CONTROL_SPSEL_MASK,
10550 M_REG_NS);
10551 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
10552 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
10553 return;
10554 case 0x98: /* SP_NS */
10555 {
10556 /* This gives the non-secure SP selected based on whether we're
10557 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10558 */
10559 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10560
10561 if (!env->v7m.secure) {
10562 return;
10563 }
10564 if (!arm_v7m_is_handler_mode(env) && spsel) {
10565 env->v7m.other_ss_psp = val;
10566 } else {
10567 env->v7m.other_ss_msp = val;
10568 }
10569 return;
10570 }
10571 default:
10572 break;
10573 }
10574 }
10575
10576 switch (reg) {
10577 case 0 ... 7: /* xPSR sub-fields */
10578 /* only APSR is actually writable */
10579 if (!(reg & 4)) {
10580 uint32_t apsrmask = 0;
10581
10582 if (mask & 8) {
10583 apsrmask |= XPSR_NZCV | XPSR_Q;
10584 }
10585 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
10586 apsrmask |= XPSR_GE;
10587 }
10588 xpsr_write(env, val, apsrmask);
10589 }
10590 break;
10591 case 8: /* MSP */
10592 if (v7m_using_psp(env)) {
10593 env->v7m.other_sp = val;
10594 } else {
10595 env->regs[13] = val;
10596 }
10597 break;
10598 case 9: /* PSP */
10599 if (v7m_using_psp(env)) {
10600 env->regs[13] = val;
10601 } else {
10602 env->v7m.other_sp = val;
10603 }
10604 break;
10605 case 10: /* MSPLIM */
10606 if (!arm_feature(env, ARM_FEATURE_V8)) {
10607 goto bad_reg;
10608 }
10609 env->v7m.msplim[env->v7m.secure] = val & ~7;
10610 break;
10611 case 11: /* PSPLIM */
10612 if (!arm_feature(env, ARM_FEATURE_V8)) {
10613 goto bad_reg;
10614 }
10615 env->v7m.psplim[env->v7m.secure] = val & ~7;
10616 break;
10617 case 16: /* PRIMASK */
10618 env->v7m.primask[env->v7m.secure] = val & 1;
10619 break;
10620 case 17: /* BASEPRI */
10621 env->v7m.basepri[env->v7m.secure] = val & 0xff;
10622 break;
10623 case 18: /* BASEPRI_MAX */
10624 val &= 0xff;
10625 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10626 || env->v7m.basepri[env->v7m.secure] == 0)) {
10627 env->v7m.basepri[env->v7m.secure] = val;
10628 }
10629 break;
10630 case 19: /* FAULTMASK */
10631 env->v7m.faultmask[env->v7m.secure] = val & 1;
10632 break;
10633 case 20: /* CONTROL */
10634 /* Writing to the SPSEL bit only has an effect if we are in
10635 * thread mode; other bits can be updated by any privileged code.
10636 * write_v7m_control_spsel() deals with updating the SPSEL bit in
10637 * env->v7m.control, so we only need update the others.
10638 * For v7M, we must just ignore explicit writes to SPSEL in handler
10639 * mode; for v8M the write is permitted but will have no effect.
10640 */
10641 if (arm_feature(env, ARM_FEATURE_V8) ||
10642 !arm_v7m_is_handler_mode(env)) {
10643 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
10644 }
10645 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10646 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
10647 break;
10648 default:
10649 bad_reg:
10650 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10651 " register %d\n", reg);
10652 return;
10653 }
10654 }
10655
10656 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10657 {
10658 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10659 bool forceunpriv = op & 1;
10660 bool alt = op & 2;
10661 V8M_SAttributes sattrs = {};
10662 uint32_t tt_resp;
10663 bool r, rw, nsr, nsrw, mrvalid;
10664 int prot;
10665 ARMMMUFaultInfo fi = {};
10666 MemTxAttrs attrs = {};
10667 hwaddr phys_addr;
10668 ARMMMUIdx mmu_idx;
10669 uint32_t mregion;
10670 bool targetpriv;
10671 bool targetsec = env->v7m.secure;
10672
10673 /* Work out what the security state and privilege level we're
10674 * interested in is...
10675 */
10676 if (alt) {
10677 targetsec = !targetsec;
10678 }
10679
10680 if (forceunpriv) {
10681 targetpriv = false;
10682 } else {
10683 targetpriv = arm_v7m_is_handler_mode(env) ||
10684 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10685 }
10686
10687 /* ...and then figure out which MMU index this is */
10688 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10689
10690 /* We know that the MPU and SAU don't care about the access type
10691 * for our purposes beyond that we don't want to claim to be
10692 * an insn fetch, so we arbitrarily call this a read.
10693 */
10694
10695 /* MPU region info only available for privileged or if
10696 * inspecting the other MPU state.
10697 */
10698 if (arm_current_el(env) != 0 || alt) {
10699 /* We can ignore the return value as prot is always set */
10700 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
10701 &phys_addr, &attrs, &prot, &fi, &mregion);
10702 if (mregion == -1) {
10703 mrvalid = false;
10704 mregion = 0;
10705 } else {
10706 mrvalid = true;
10707 }
10708 r = prot & PAGE_READ;
10709 rw = prot & PAGE_WRITE;
10710 } else {
10711 r = false;
10712 rw = false;
10713 mrvalid = false;
10714 mregion = 0;
10715 }
10716
10717 if (env->v7m.secure) {
10718 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10719 nsr = sattrs.ns && r;
10720 nsrw = sattrs.ns && rw;
10721 } else {
10722 sattrs.ns = true;
10723 nsr = false;
10724 nsrw = false;
10725 }
10726
10727 tt_resp = (sattrs.iregion << 24) |
10728 (sattrs.irvalid << 23) |
10729 ((!sattrs.ns) << 22) |
10730 (nsrw << 21) |
10731 (nsr << 20) |
10732 (rw << 19) |
10733 (r << 18) |
10734 (sattrs.srvalid << 17) |
10735 (mrvalid << 16) |
10736 (sattrs.sregion << 8) |
10737 mregion;
10738
10739 return tt_resp;
10740 }
10741
10742 #endif
10743
10744 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10745 {
10746 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10747 * Note that we do not implement the (architecturally mandated)
10748 * alignment fault for attempts to use this on Device memory
10749 * (which matches the usual QEMU behaviour of not implementing either
10750 * alignment faults or any memory attribute handling).
10751 */
10752
10753 ARMCPU *cpu = arm_env_get_cpu(env);
10754 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10755 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10756
10757 #ifndef CONFIG_USER_ONLY
10758 {
10759 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10760 * the block size so we might have to do more than one TLB lookup.
10761 * We know that in fact for any v8 CPU the page size is at least 4K
10762 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10763 * 1K as an artefact of legacy v5 subpage support being present in the
10764 * same QEMU executable.
10765 */
10766 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10767 void *hostaddr[maxidx];
10768 int try, i;
10769 unsigned mmu_idx = cpu_mmu_index(env, false);
10770 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
10771
10772 for (try = 0; try < 2; try++) {
10773
10774 for (i = 0; i < maxidx; i++) {
10775 hostaddr[i] = tlb_vaddr_to_host(env,
10776 vaddr + TARGET_PAGE_SIZE * i,
10777 1, mmu_idx);
10778 if (!hostaddr[i]) {
10779 break;
10780 }
10781 }
10782 if (i == maxidx) {
10783 /* If it's all in the TLB it's fair game for just writing to;
10784 * we know we don't need to update dirty status, etc.
10785 */
10786 for (i = 0; i < maxidx - 1; i++) {
10787 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10788 }
10789 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10790 return;
10791 }
10792 /* OK, try a store and see if we can populate the tlb. This
10793 * might cause an exception if the memory isn't writable,
10794 * in which case we will longjmp out of here. We must for
10795 * this purpose use the actual register value passed to us
10796 * so that we get the fault address right.
10797 */
10798 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
10799 /* Now we can populate the other TLB entries, if any */
10800 for (i = 0; i < maxidx; i++) {
10801 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10802 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
10803 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
10804 }
10805 }
10806 }
10807
10808 /* Slow path (probably attempt to do this to an I/O device or
10809 * similar, or clearing of a block of code we have translations
10810 * cached for). Just do a series of byte writes as the architecture
10811 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10812 * memset(), unmap() sequence here because:
10813 * + we'd need to account for the blocksize being larger than a page
10814 * + the direct-RAM access case is almost always going to be dealt
10815 * with in the fastpath code above, so there's no speed benefit
10816 * + we would have to deal with the map returning NULL because the
10817 * bounce buffer was in use
10818 */
10819 for (i = 0; i < blocklen; i++) {
10820 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
10821 }
10822 }
10823 #else
10824 memset(g2h(vaddr), 0, blocklen);
10825 #endif
10826 }
10827
10828 /* Note that signed overflow is undefined in C. The following routines are
10829 careful to use unsigned types where modulo arithmetic is required.
10830 Failure to do so _will_ break on newer gcc. */
10831
10832 /* Signed saturating arithmetic. */
10833
10834 /* Perform 16-bit signed saturating addition. */
10835 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10836 {
10837 uint16_t res;
10838
10839 res = a + b;
10840 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10841 if (a & 0x8000)
10842 res = 0x8000;
10843 else
10844 res = 0x7fff;
10845 }
10846 return res;
10847 }
10848
10849 /* Perform 8-bit signed saturating addition. */
10850 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10851 {
10852 uint8_t res;
10853
10854 res = a + b;
10855 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10856 if (a & 0x80)
10857 res = 0x80;
10858 else
10859 res = 0x7f;
10860 }
10861 return res;
10862 }
10863
10864 /* Perform 16-bit signed saturating subtraction. */
10865 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10866 {
10867 uint16_t res;
10868
10869 res = a - b;
10870 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10871 if (a & 0x8000)
10872 res = 0x8000;
10873 else
10874 res = 0x7fff;
10875 }
10876 return res;
10877 }
10878
10879 /* Perform 8-bit signed saturating subtraction. */
10880 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10881 {
10882 uint8_t res;
10883
10884 res = a - b;
10885 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10886 if (a & 0x80)
10887 res = 0x80;
10888 else
10889 res = 0x7f;
10890 }
10891 return res;
10892 }
10893
10894 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10895 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10896 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10897 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10898 #define PFX q
10899
10900 #include "op_addsub.h"
10901
10902 /* Unsigned saturating arithmetic. */
10903 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10904 {
10905 uint16_t res;
10906 res = a + b;
10907 if (res < a)
10908 res = 0xffff;
10909 return res;
10910 }
10911
10912 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10913 {
10914 if (a > b)
10915 return a - b;
10916 else
10917 return 0;
10918 }
10919
10920 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10921 {
10922 uint8_t res;
10923 res = a + b;
10924 if (res < a)
10925 res = 0xff;
10926 return res;
10927 }
10928
10929 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10930 {
10931 if (a > b)
10932 return a - b;
10933 else
10934 return 0;
10935 }
10936
10937 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10938 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10939 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10940 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10941 #define PFX uq
10942
10943 #include "op_addsub.h"
10944
10945 /* Signed modulo arithmetic. */
10946 #define SARITH16(a, b, n, op) do { \
10947 int32_t sum; \
10948 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10949 RESULT(sum, n, 16); \
10950 if (sum >= 0) \
10951 ge |= 3 << (n * 2); \
10952 } while(0)
10953
10954 #define SARITH8(a, b, n, op) do { \
10955 int32_t sum; \
10956 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10957 RESULT(sum, n, 8); \
10958 if (sum >= 0) \
10959 ge |= 1 << n; \
10960 } while(0)
10961
10962
10963 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10964 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10965 #define ADD8(a, b, n) SARITH8(a, b, n, +)
10966 #define SUB8(a, b, n) SARITH8(a, b, n, -)
10967 #define PFX s
10968 #define ARITH_GE
10969
10970 #include "op_addsub.h"
10971
10972 /* Unsigned modulo arithmetic. */
10973 #define ADD16(a, b, n) do { \
10974 uint32_t sum; \
10975 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10976 RESULT(sum, n, 16); \
10977 if ((sum >> 16) == 1) \
10978 ge |= 3 << (n * 2); \
10979 } while(0)
10980
10981 #define ADD8(a, b, n) do { \
10982 uint32_t sum; \
10983 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10984 RESULT(sum, n, 8); \
10985 if ((sum >> 8) == 1) \
10986 ge |= 1 << n; \
10987 } while(0)
10988
10989 #define SUB16(a, b, n) do { \
10990 uint32_t sum; \
10991 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10992 RESULT(sum, n, 16); \
10993 if ((sum >> 16) == 0) \
10994 ge |= 3 << (n * 2); \
10995 } while(0)
10996
10997 #define SUB8(a, b, n) do { \
10998 uint32_t sum; \
10999 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11000 RESULT(sum, n, 8); \
11001 if ((sum >> 8) == 0) \
11002 ge |= 1 << n; \
11003 } while(0)
11004
11005 #define PFX u
11006 #define ARITH_GE
11007
11008 #include "op_addsub.h"
11009
11010 /* Halved signed arithmetic. */
11011 #define ADD16(a, b, n) \
11012 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11013 #define SUB16(a, b, n) \
11014 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11015 #define ADD8(a, b, n) \
11016 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11017 #define SUB8(a, b, n) \
11018 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11019 #define PFX sh
11020
11021 #include "op_addsub.h"
11022
11023 /* Halved unsigned arithmetic. */
11024 #define ADD16(a, b, n) \
11025 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11026 #define SUB16(a, b, n) \
11027 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11028 #define ADD8(a, b, n) \
11029 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11030 #define SUB8(a, b, n) \
11031 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11032 #define PFX uh
11033
11034 #include "op_addsub.h"
11035
11036 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11037 {
11038 if (a > b)
11039 return a - b;
11040 else
11041 return b - a;
11042 }
11043
11044 /* Unsigned sum of absolute byte differences. */
11045 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11046 {
11047 uint32_t sum;
11048 sum = do_usad(a, b);
11049 sum += do_usad(a >> 8, b >> 8);
11050 sum += do_usad(a >> 16, b >>16);
11051 sum += do_usad(a >> 24, b >> 24);
11052 return sum;
11053 }
11054
11055 /* For ARMv6 SEL instruction. */
11056 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11057 {
11058 uint32_t mask;
11059
11060 mask = 0;
11061 if (flags & 1)
11062 mask |= 0xff;
11063 if (flags & 2)
11064 mask |= 0xff00;
11065 if (flags & 4)
11066 mask |= 0xff0000;
11067 if (flags & 8)
11068 mask |= 0xff000000;
11069 return (a & mask) | (b & ~mask);
11070 }
11071
11072 /* VFP support. We follow the convention used for VFP instructions:
11073 Single precision routines have a "s" suffix, double precision a
11074 "d" suffix. */
11075
11076 /* Convert host exception flags to vfp form. */
11077 static inline int vfp_exceptbits_from_host(int host_bits)
11078 {
11079 int target_bits = 0;
11080
11081 if (host_bits & float_flag_invalid)
11082 target_bits |= 1;
11083 if (host_bits & float_flag_divbyzero)
11084 target_bits |= 2;
11085 if (host_bits & float_flag_overflow)
11086 target_bits |= 4;
11087 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
11088 target_bits |= 8;
11089 if (host_bits & float_flag_inexact)
11090 target_bits |= 0x10;
11091 if (host_bits & float_flag_input_denormal)
11092 target_bits |= 0x80;
11093 return target_bits;
11094 }
11095
11096 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
11097 {
11098 int i;
11099 uint32_t fpscr;
11100
11101 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
11102 | (env->vfp.vec_len << 16)
11103 | (env->vfp.vec_stride << 20);
11104 i = get_float_exception_flags(&env->vfp.fp_status);
11105 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
11106 i |= get_float_exception_flags(&env->vfp.fp_status_f16);
11107 fpscr |= vfp_exceptbits_from_host(i);
11108 return fpscr;
11109 }
11110
11111 uint32_t vfp_get_fpscr(CPUARMState *env)
11112 {
11113 return HELPER(vfp_get_fpscr)(env);
11114 }
11115
11116 /* Convert vfp exception flags to target form. */
11117 static inline int vfp_exceptbits_to_host(int target_bits)
11118 {
11119 int host_bits = 0;
11120
11121 if (target_bits & 1)
11122 host_bits |= float_flag_invalid;
11123 if (target_bits & 2)
11124 host_bits |= float_flag_divbyzero;
11125 if (target_bits & 4)
11126 host_bits |= float_flag_overflow;
11127 if (target_bits & 8)
11128 host_bits |= float_flag_underflow;
11129 if (target_bits & 0x10)
11130 host_bits |= float_flag_inexact;
11131 if (target_bits & 0x80)
11132 host_bits |= float_flag_input_denormal;
11133 return host_bits;
11134 }
11135
11136 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
11137 {
11138 int i;
11139 uint32_t changed;
11140
11141 changed = env->vfp.xregs[ARM_VFP_FPSCR];
11142 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
11143 env->vfp.vec_len = (val >> 16) & 7;
11144 env->vfp.vec_stride = (val >> 20) & 3;
11145
11146 changed ^= val;
11147 if (changed & (3 << 22)) {
11148 i = (val >> 22) & 3;
11149 switch (i) {
11150 case FPROUNDING_TIEEVEN:
11151 i = float_round_nearest_even;
11152 break;
11153 case FPROUNDING_POSINF:
11154 i = float_round_up;
11155 break;
11156 case FPROUNDING_NEGINF:
11157 i = float_round_down;
11158 break;
11159 case FPROUNDING_ZERO:
11160 i = float_round_to_zero;
11161 break;
11162 }
11163 set_float_rounding_mode(i, &env->vfp.fp_status);
11164 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
11165 }
11166 if (changed & FPCR_FZ16) {
11167 bool ftz_enabled = val & FPCR_FZ16;
11168 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11169 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11170 }
11171 if (changed & FPCR_FZ) {
11172 bool ftz_enabled = val & FPCR_FZ;
11173 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
11174 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
11175 }
11176 if (changed & FPCR_DN) {
11177 bool dnan_enabled = val & FPCR_DN;
11178 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
11179 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
11180 }
11181
11182 /* The exception flags are ORed together when we read fpscr so we
11183 * only need to preserve the current state in one of our
11184 * float_status values.
11185 */
11186 i = vfp_exceptbits_to_host(val);
11187 set_float_exception_flags(i, &env->vfp.fp_status);
11188 set_float_exception_flags(0, &env->vfp.fp_status_f16);
11189 set_float_exception_flags(0, &env->vfp.standard_fp_status);
11190 }
11191
11192 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
11193 {
11194 HELPER(vfp_set_fpscr)(env, val);
11195 }
11196
11197 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
11198
11199 #define VFP_BINOP(name) \
11200 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
11201 { \
11202 float_status *fpst = fpstp; \
11203 return float32_ ## name(a, b, fpst); \
11204 } \
11205 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
11206 { \
11207 float_status *fpst = fpstp; \
11208 return float64_ ## name(a, b, fpst); \
11209 }
11210 VFP_BINOP(add)
11211 VFP_BINOP(sub)
11212 VFP_BINOP(mul)
11213 VFP_BINOP(div)
11214 VFP_BINOP(min)
11215 VFP_BINOP(max)
11216 VFP_BINOP(minnum)
11217 VFP_BINOP(maxnum)
11218 #undef VFP_BINOP
11219
11220 float32 VFP_HELPER(neg, s)(float32 a)
11221 {
11222 return float32_chs(a);
11223 }
11224
11225 float64 VFP_HELPER(neg, d)(float64 a)
11226 {
11227 return float64_chs(a);
11228 }
11229
11230 float32 VFP_HELPER(abs, s)(float32 a)
11231 {
11232 return float32_abs(a);
11233 }
11234
11235 float64 VFP_HELPER(abs, d)(float64 a)
11236 {
11237 return float64_abs(a);
11238 }
11239
11240 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
11241 {
11242 return float32_sqrt(a, &env->vfp.fp_status);
11243 }
11244
11245 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
11246 {
11247 return float64_sqrt(a, &env->vfp.fp_status);
11248 }
11249
11250 /* XXX: check quiet/signaling case */
11251 #define DO_VFP_cmp(p, type) \
11252 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
11253 { \
11254 uint32_t flags; \
11255 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
11256 case 0: flags = 0x6; break; \
11257 case -1: flags = 0x8; break; \
11258 case 1: flags = 0x2; break; \
11259 default: case 2: flags = 0x3; break; \
11260 } \
11261 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11262 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11263 } \
11264 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
11265 { \
11266 uint32_t flags; \
11267 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
11268 case 0: flags = 0x6; break; \
11269 case -1: flags = 0x8; break; \
11270 case 1: flags = 0x2; break; \
11271 default: case 2: flags = 0x3; break; \
11272 } \
11273 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11274 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11275 }
11276 DO_VFP_cmp(s, float32)
11277 DO_VFP_cmp(d, float64)
11278 #undef DO_VFP_cmp
11279
11280 /* Integer to float and float to integer conversions */
11281
11282 #define CONV_ITOF(name, fsz, sign) \
11283 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
11284 { \
11285 float_status *fpst = fpstp; \
11286 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
11287 }
11288
11289 #define CONV_FTOI(name, fsz, sign, round) \
11290 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
11291 { \
11292 float_status *fpst = fpstp; \
11293 if (float##fsz##_is_any_nan(x)) { \
11294 float_raise(float_flag_invalid, fpst); \
11295 return 0; \
11296 } \
11297 return float##fsz##_to_##sign##int32##round(x, fpst); \
11298 }
11299
11300 #define FLOAT_CONVS(name, p, fsz, sign) \
11301 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
11302 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
11303 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
11304
11305 FLOAT_CONVS(si, h, 16, )
11306 FLOAT_CONVS(si, s, 32, )
11307 FLOAT_CONVS(si, d, 64, )
11308 FLOAT_CONVS(ui, h, 16, u)
11309 FLOAT_CONVS(ui, s, 32, u)
11310 FLOAT_CONVS(ui, d, 64, u)
11311
11312 #undef CONV_ITOF
11313 #undef CONV_FTOI
11314 #undef FLOAT_CONVS
11315
11316 /* floating point conversion */
11317 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
11318 {
11319 float64 r = float32_to_float64(x, &env->vfp.fp_status);
11320 /* ARM requires that S<->D conversion of any kind of NaN generates
11321 * a quiet NaN by forcing the most significant frac bit to 1.
11322 */
11323 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
11324 }
11325
11326 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
11327 {
11328 float32 r = float64_to_float32(x, &env->vfp.fp_status);
11329 /* ARM requires that S<->D conversion of any kind of NaN generates
11330 * a quiet NaN by forcing the most significant frac bit to 1.
11331 */
11332 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
11333 }
11334
11335 /* VFP3 fixed point conversion. */
11336 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11337 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
11338 void *fpstp) \
11339 { \
11340 float_status *fpst = fpstp; \
11341 float##fsz tmp; \
11342 tmp = itype##_to_##float##fsz(x, fpst); \
11343 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
11344 }
11345
11346 /* Notice that we want only input-denormal exception flags from the
11347 * scalbn operation: the other possible flags (overflow+inexact if
11348 * we overflow to infinity, output-denormal) aren't correct for the
11349 * complete scale-and-convert operation.
11350 */
11351 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
11352 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
11353 uint32_t shift, \
11354 void *fpstp) \
11355 { \
11356 float_status *fpst = fpstp; \
11357 int old_exc_flags = get_float_exception_flags(fpst); \
11358 float##fsz tmp; \
11359 if (float##fsz##_is_any_nan(x)) { \
11360 float_raise(float_flag_invalid, fpst); \
11361 return 0; \
11362 } \
11363 tmp = float##fsz##_scalbn(x, shift, fpst); \
11364 old_exc_flags |= get_float_exception_flags(fpst) \
11365 & float_flag_input_denormal; \
11366 set_float_exception_flags(old_exc_flags, fpst); \
11367 return float##fsz##_to_##itype##round(tmp, fpst); \
11368 }
11369
11370 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
11371 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11372 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
11373 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11374
11375 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
11376 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11377 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11378
11379 VFP_CONV_FIX(sh, d, 64, 64, int16)
11380 VFP_CONV_FIX(sl, d, 64, 64, int32)
11381 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
11382 VFP_CONV_FIX(uh, d, 64, 64, uint16)
11383 VFP_CONV_FIX(ul, d, 64, 64, uint32)
11384 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
11385 VFP_CONV_FIX(sh, s, 32, 32, int16)
11386 VFP_CONV_FIX(sl, s, 32, 32, int32)
11387 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
11388 VFP_CONV_FIX(uh, s, 32, 32, uint16)
11389 VFP_CONV_FIX(ul, s, 32, 32, uint32)
11390 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
11391 VFP_CONV_FIX_A64(sl, h, 16, 32, int32)
11392 VFP_CONV_FIX_A64(ul, h, 16, 32, uint32)
11393 #undef VFP_CONV_FIX
11394 #undef VFP_CONV_FIX_FLOAT
11395 #undef VFP_CONV_FLOAT_FIX_ROUND
11396
11397 /* Set the current fp rounding mode and return the old one.
11398 * The argument is a softfloat float_round_ value.
11399 */
11400 uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
11401 {
11402 float_status *fp_status = fpstp;
11403
11404 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11405 set_float_rounding_mode(rmode, fp_status);
11406
11407 return prev_rmode;
11408 }
11409
11410 /* Set the current fp rounding mode in the standard fp status and return
11411 * the old one. This is for NEON instructions that need to change the
11412 * rounding mode but wish to use the standard FPSCR values for everything
11413 * else. Always set the rounding mode back to the correct value after
11414 * modifying it.
11415 * The argument is a softfloat float_round_ value.
11416 */
11417 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
11418 {
11419 float_status *fp_status = &env->vfp.standard_fp_status;
11420
11421 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11422 set_float_rounding_mode(rmode, fp_status);
11423
11424 return prev_rmode;
11425 }
11426
11427 /* Half precision conversions. */
11428 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
11429 {
11430 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11431 float32 r = float16_to_float32(make_float16(a), ieee, s);
11432 if (ieee) {
11433 return float32_maybe_silence_nan(r, s);
11434 }
11435 return r;
11436 }
11437
11438 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
11439 {
11440 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11441 float16 r = float32_to_float16(a, ieee, s);
11442 if (ieee) {
11443 r = float16_maybe_silence_nan(r, s);
11444 }
11445 return float16_val(r);
11446 }
11447
11448 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
11449 {
11450 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
11451 }
11452
11453 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
11454 {
11455 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
11456 }
11457
11458 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
11459 {
11460 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
11461 }
11462
11463 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
11464 {
11465 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
11466 }
11467
11468 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
11469 {
11470 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11471 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
11472 if (ieee) {
11473 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
11474 }
11475 return r;
11476 }
11477
11478 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
11479 {
11480 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
11481 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
11482 if (ieee) {
11483 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
11484 }
11485 return float16_val(r);
11486 }
11487
11488 #define float32_two make_float32(0x40000000)
11489 #define float32_three make_float32(0x40400000)
11490 #define float32_one_point_five make_float32(0x3fc00000)
11491
11492 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
11493 {
11494 float_status *s = &env->vfp.standard_fp_status;
11495 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11496 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
11497 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11498 float_raise(float_flag_input_denormal, s);
11499 }
11500 return float32_two;
11501 }
11502 return float32_sub(float32_two, float32_mul(a, b, s), s);
11503 }
11504
11505 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
11506 {
11507 float_status *s = &env->vfp.standard_fp_status;
11508 float32 product;
11509 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11510 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
11511 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11512 float_raise(float_flag_input_denormal, s);
11513 }
11514 return float32_one_point_five;
11515 }
11516 product = float32_mul(a, b, s);
11517 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
11518 }
11519
11520 /* NEON helpers. */
11521
11522 /* Constants 256 and 512 are used in some helpers; we avoid relying on
11523 * int->float conversions at run-time. */
11524 #define float64_256 make_float64(0x4070000000000000LL)
11525 #define float64_512 make_float64(0x4080000000000000LL)
11526 #define float16_maxnorm make_float16(0x7bff)
11527 #define float32_maxnorm make_float32(0x7f7fffff)
11528 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
11529
11530 /* Reciprocal functions
11531 *
11532 * The algorithm that must be used to calculate the estimate
11533 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
11534 */
11535
11536 /* See RecipEstimate()
11537 *
11538 * input is a 9 bit fixed point number
11539 * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
11540 * result range 256 .. 511 for a number from 1.0 to 511/256.
11541 */
11542
11543 static int recip_estimate(int input)
11544 {
11545 int a, b, r;
11546 assert(256 <= input && input < 512);
11547 a = (input * 2) + 1;
11548 b = (1 << 19) / a;
11549 r = (b + 1) >> 1;
11550 assert(256 <= r && r < 512);
11551 return r;
11552 }
11553
11554 /*
11555 * Common wrapper to call recip_estimate
11556 *
11557 * The parameters are exponent and 64 bit fraction (without implicit
11558 * bit) where the binary point is nominally at bit 52. Returns a
11559 * float64 which can then be rounded to the appropriate size by the
11560 * callee.
11561 */
11562
11563 static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
11564 {
11565 uint32_t scaled, estimate;
11566 uint64_t result_frac;
11567 int result_exp;
11568
11569 /* Handle sub-normals */
11570 if (*exp == 0) {
11571 if (extract64(frac, 51, 1) == 0) {
11572 *exp = -1;
11573 frac <<= 2;
11574 } else {
11575 frac <<= 1;
11576 }
11577 }
11578
11579 /* scaled = UInt('1':fraction<51:44>) */
11580 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11581 estimate = recip_estimate(scaled);
11582
11583 result_exp = exp_off - *exp;
11584 result_frac = deposit64(0, 44, 8, estimate);
11585 if (result_exp == 0) {
11586 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
11587 } else if (result_exp == -1) {
11588 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
11589 result_exp = 0;
11590 }
11591
11592 *exp = result_exp;
11593
11594 return result_frac;
11595 }
11596
11597 static bool round_to_inf(float_status *fpst, bool sign_bit)
11598 {
11599 switch (fpst->float_rounding_mode) {
11600 case float_round_nearest_even: /* Round to Nearest */
11601 return true;
11602 case float_round_up: /* Round to +Inf */
11603 return !sign_bit;
11604 case float_round_down: /* Round to -Inf */
11605 return sign_bit;
11606 case float_round_to_zero: /* Round to Zero */
11607 return false;
11608 }
11609
11610 g_assert_not_reached();
11611 }
11612
11613 float16 HELPER(recpe_f16)(float16 input, void *fpstp)
11614 {
11615 float_status *fpst = fpstp;
11616 float16 f16 = float16_squash_input_denormal(input, fpst);
11617 uint32_t f16_val = float16_val(f16);
11618 uint32_t f16_sign = float16_is_neg(f16);
11619 int f16_exp = extract32(f16_val, 10, 5);
11620 uint32_t f16_frac = extract32(f16_val, 0, 10);
11621 uint64_t f64_frac;
11622
11623 if (float16_is_any_nan(f16)) {
11624 float16 nan = f16;
11625 if (float16_is_signaling_nan(f16, fpst)) {
11626 float_raise(float_flag_invalid, fpst);
11627 nan = float16_maybe_silence_nan(f16, fpst);
11628 }
11629 if (fpst->default_nan_mode) {
11630 nan = float16_default_nan(fpst);
11631 }
11632 return nan;
11633 } else if (float16_is_infinity(f16)) {
11634 return float16_set_sign(float16_zero, float16_is_neg(f16));
11635 } else if (float16_is_zero(f16)) {
11636 float_raise(float_flag_divbyzero, fpst);
11637 return float16_set_sign(float16_infinity, float16_is_neg(f16));
11638 } else if (float16_abs(f16) < (1 << 8)) {
11639 /* Abs(value) < 2.0^-16 */
11640 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11641 if (round_to_inf(fpst, f16_sign)) {
11642 return float16_set_sign(float16_infinity, f16_sign);
11643 } else {
11644 return float16_set_sign(float16_maxnorm, f16_sign);
11645 }
11646 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
11647 float_raise(float_flag_underflow, fpst);
11648 return float16_set_sign(float16_zero, float16_is_neg(f16));
11649 }
11650
11651 f64_frac = call_recip_estimate(&f16_exp, 29,
11652 ((uint64_t) f16_frac) << (52 - 10));
11653
11654 /* result = sign : result_exp<4:0> : fraction<51:42> */
11655 f16_val = deposit32(0, 15, 1, f16_sign);
11656 f16_val = deposit32(f16_val, 10, 5, f16_exp);
11657 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
11658 return make_float16(f16_val);
11659 }
11660
11661 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11662 {
11663 float_status *fpst = fpstp;
11664 float32 f32 = float32_squash_input_denormal(input, fpst);
11665 uint32_t f32_val = float32_val(f32);
11666 bool f32_sign = float32_is_neg(f32);
11667 int f32_exp = extract32(f32_val, 23, 8);
11668 uint32_t f32_frac = extract32(f32_val, 0, 23);
11669 uint64_t f64_frac;
11670
11671 if (float32_is_any_nan(f32)) {
11672 float32 nan = f32;
11673 if (float32_is_signaling_nan(f32, fpst)) {
11674 float_raise(float_flag_invalid, fpst);
11675 nan = float32_maybe_silence_nan(f32, fpst);
11676 }
11677 if (fpst->default_nan_mode) {
11678 nan = float32_default_nan(fpst);
11679 }
11680 return nan;
11681 } else if (float32_is_infinity(f32)) {
11682 return float32_set_sign(float32_zero, float32_is_neg(f32));
11683 } else if (float32_is_zero(f32)) {
11684 float_raise(float_flag_divbyzero, fpst);
11685 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11686 } else if (float32_abs(f32) < (1ULL << 21)) {
11687 /* Abs(value) < 2.0^-128 */
11688 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11689 if (round_to_inf(fpst, f32_sign)) {
11690 return float32_set_sign(float32_infinity, f32_sign);
11691 } else {
11692 return float32_set_sign(float32_maxnorm, f32_sign);
11693 }
11694 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11695 float_raise(float_flag_underflow, fpst);
11696 return float32_set_sign(float32_zero, float32_is_neg(f32));
11697 }
11698
11699 f64_frac = call_recip_estimate(&f32_exp, 253,
11700 ((uint64_t) f32_frac) << (52 - 23));
11701
11702 /* result = sign : result_exp<7:0> : fraction<51:29> */
11703 f32_val = deposit32(0, 31, 1, f32_sign);
11704 f32_val = deposit32(f32_val, 23, 8, f32_exp);
11705 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
11706 return make_float32(f32_val);
11707 }
11708
11709 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11710 {
11711 float_status *fpst = fpstp;
11712 float64 f64 = float64_squash_input_denormal(input, fpst);
11713 uint64_t f64_val = float64_val(f64);
11714 bool f64_sign = float64_is_neg(f64);
11715 int f64_exp = extract64(f64_val, 52, 11);
11716 uint64_t f64_frac = extract64(f64_val, 0, 52);
11717
11718 /* Deal with any special cases */
11719 if (float64_is_any_nan(f64)) {
11720 float64 nan = f64;
11721 if (float64_is_signaling_nan(f64, fpst)) {
11722 float_raise(float_flag_invalid, fpst);
11723 nan = float64_maybe_silence_nan(f64, fpst);
11724 }
11725 if (fpst->default_nan_mode) {
11726 nan = float64_default_nan(fpst);
11727 }
11728 return nan;
11729 } else if (float64_is_infinity(f64)) {
11730 return float64_set_sign(float64_zero, float64_is_neg(f64));
11731 } else if (float64_is_zero(f64)) {
11732 float_raise(float_flag_divbyzero, fpst);
11733 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11734 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11735 /* Abs(value) < 2.0^-1024 */
11736 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11737 if (round_to_inf(fpst, f64_sign)) {
11738 return float64_set_sign(float64_infinity, f64_sign);
11739 } else {
11740 return float64_set_sign(float64_maxnorm, f64_sign);
11741 }
11742 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
11743 float_raise(float_flag_underflow, fpst);
11744 return float64_set_sign(float64_zero, float64_is_neg(f64));
11745 }
11746
11747 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
11748
11749 /* result = sign : result_exp<10:0> : fraction<51:0>; */
11750 f64_val = deposit64(0, 63, 1, f64_sign);
11751 f64_val = deposit64(f64_val, 52, 11, f64_exp);
11752 f64_val = deposit64(f64_val, 0, 52, f64_frac);
11753 return make_float64(f64_val);
11754 }
11755
11756 /* The algorithm that must be used to calculate the estimate
11757 * is specified by the ARM ARM.
11758 */
11759
11760 static int do_recip_sqrt_estimate(int a)
11761 {
11762 int b, estimate;
11763
11764 assert(128 <= a && a < 512);
11765 if (a < 256) {
11766 a = a * 2 + 1;
11767 } else {
11768 a = (a >> 1) << 1;
11769 a = (a + 1) * 2;
11770 }
11771 b = 512;
11772 while (a * (b + 1) * (b + 1) < (1 << 28)) {
11773 b += 1;
11774 }
11775 estimate = (b + 1) / 2;
11776 assert(256 <= estimate && estimate < 512);
11777
11778 return estimate;
11779 }
11780
11781
11782 static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
11783 {
11784 int estimate;
11785 uint32_t scaled;
11786
11787 if (*exp == 0) {
11788 while (extract64(frac, 51, 1) == 0) {
11789 frac = frac << 1;
11790 *exp -= 1;
11791 }
11792 frac = extract64(frac, 0, 51) << 1;
11793 }
11794
11795 if (*exp & 1) {
11796 /* scaled = UInt('01':fraction<51:45>) */
11797 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
11798 } else {
11799 /* scaled = UInt('1':fraction<51:44>) */
11800 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11801 }
11802 estimate = do_recip_sqrt_estimate(scaled);
11803
11804 *exp = (exp_off - *exp) / 2;
11805 return extract64(estimate, 0, 8) << 44;
11806 }
11807
11808 float16 HELPER(rsqrte_f16)(float16 input, void *fpstp)
11809 {
11810 float_status *s = fpstp;
11811 float16 f16 = float16_squash_input_denormal(input, s);
11812 uint16_t val = float16_val(f16);
11813 bool f16_sign = float16_is_neg(f16);
11814 int f16_exp = extract32(val, 10, 5);
11815 uint16_t f16_frac = extract32(val, 0, 10);
11816 uint64_t f64_frac;
11817
11818 if (float16_is_any_nan(f16)) {
11819 float16 nan = f16;
11820 if (float16_is_signaling_nan(f16, s)) {
11821 float_raise(float_flag_invalid, s);
11822 nan = float16_maybe_silence_nan(f16, s);
11823 }
11824 if (s->default_nan_mode) {
11825 nan = float16_default_nan(s);
11826 }
11827 return nan;
11828 } else if (float16_is_zero(f16)) {
11829 float_raise(float_flag_divbyzero, s);
11830 return float16_set_sign(float16_infinity, f16_sign);
11831 } else if (f16_sign) {
11832 float_raise(float_flag_invalid, s);
11833 return float16_default_nan(s);
11834 } else if (float16_is_infinity(f16)) {
11835 return float16_zero;
11836 }
11837
11838 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11839 * preserving the parity of the exponent. */
11840
11841 f64_frac = ((uint64_t) f16_frac) << (52 - 10);
11842
11843 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
11844
11845 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */
11846 val = deposit32(0, 15, 1, f16_sign);
11847 val = deposit32(val, 10, 5, f16_exp);
11848 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
11849 return make_float16(val);
11850 }
11851
11852 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
11853 {
11854 float_status *s = fpstp;
11855 float32 f32 = float32_squash_input_denormal(input, s);
11856 uint32_t val = float32_val(f32);
11857 uint32_t f32_sign = float32_is_neg(f32);
11858 int f32_exp = extract32(val, 23, 8);
11859 uint32_t f32_frac = extract32(val, 0, 23);
11860 uint64_t f64_frac;
11861
11862 if (float32_is_any_nan(f32)) {
11863 float32 nan = f32;
11864 if (float32_is_signaling_nan(f32, s)) {
11865 float_raise(float_flag_invalid, s);
11866 nan = float32_maybe_silence_nan(f32, s);
11867 }
11868 if (s->default_nan_mode) {
11869 nan = float32_default_nan(s);
11870 }
11871 return nan;
11872 } else if (float32_is_zero(f32)) {
11873 float_raise(float_flag_divbyzero, s);
11874 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11875 } else if (float32_is_neg(f32)) {
11876 float_raise(float_flag_invalid, s);
11877 return float32_default_nan(s);
11878 } else if (float32_is_infinity(f32)) {
11879 return float32_zero;
11880 }
11881
11882 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11883 * preserving the parity of the exponent. */
11884
11885 f64_frac = ((uint64_t) f32_frac) << 29;
11886
11887 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
11888
11889 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */
11890 val = deposit32(0, 31, 1, f32_sign);
11891 val = deposit32(val, 23, 8, f32_exp);
11892 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
11893 return make_float32(val);
11894 }
11895
11896 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
11897 {
11898 float_status *s = fpstp;
11899 float64 f64 = float64_squash_input_denormal(input, s);
11900 uint64_t val = float64_val(f64);
11901 bool f64_sign = float64_is_neg(f64);
11902 int f64_exp = extract64(val, 52, 11);
11903 uint64_t f64_frac = extract64(val, 0, 52);
11904
11905 if (float64_is_any_nan(f64)) {
11906 float64 nan = f64;
11907 if (float64_is_signaling_nan(f64, s)) {
11908 float_raise(float_flag_invalid, s);
11909 nan = float64_maybe_silence_nan(f64, s);
11910 }
11911 if (s->default_nan_mode) {
11912 nan = float64_default_nan(s);
11913 }
11914 return nan;
11915 } else if (float64_is_zero(f64)) {
11916 float_raise(float_flag_divbyzero, s);
11917 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11918 } else if (float64_is_neg(f64)) {
11919 float_raise(float_flag_invalid, s);
11920 return float64_default_nan(s);
11921 } else if (float64_is_infinity(f64)) {
11922 return float64_zero;
11923 }
11924
11925 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
11926
11927 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
11928 val = deposit64(0, 61, 1, f64_sign);
11929 val = deposit64(val, 52, 11, f64_exp);
11930 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
11931 return make_float64(val);
11932 }
11933
11934 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
11935 {
11936 /* float_status *s = fpstp; */
11937 int input, estimate;
11938
11939 if ((a & 0x80000000) == 0) {
11940 return 0xffffffff;
11941 }
11942
11943 input = extract32(a, 23, 9);
11944 estimate = recip_estimate(input);
11945
11946 return deposit32(0, (32 - 9), 9, estimate);
11947 }
11948
11949 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
11950 {
11951 int estimate;
11952
11953 if ((a & 0xc0000000) == 0) {
11954 return 0xffffffff;
11955 }
11956
11957 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
11958
11959 return deposit32(0, 23, 9, estimate);
11960 }
11961
11962 /* VFPv4 fused multiply-accumulate */
11963 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11964 {
11965 float_status *fpst = fpstp;
11966 return float32_muladd(a, b, c, 0, fpst);
11967 }
11968
11969 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11970 {
11971 float_status *fpst = fpstp;
11972 return float64_muladd(a, b, c, 0, fpst);
11973 }
11974
11975 /* ARMv8 round to integral */
11976 float32 HELPER(rints_exact)(float32 x, void *fp_status)
11977 {
11978 return float32_round_to_int(x, fp_status);
11979 }
11980
11981 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11982 {
11983 return float64_round_to_int(x, fp_status);
11984 }
11985
11986 float32 HELPER(rints)(float32 x, void *fp_status)
11987 {
11988 int old_flags = get_float_exception_flags(fp_status), new_flags;
11989 float32 ret;
11990
11991 ret = float32_round_to_int(x, fp_status);
11992
11993 /* Suppress any inexact exceptions the conversion produced */
11994 if (!(old_flags & float_flag_inexact)) {
11995 new_flags = get_float_exception_flags(fp_status);
11996 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11997 }
11998
11999 return ret;
12000 }
12001
12002 float64 HELPER(rintd)(float64 x, void *fp_status)
12003 {
12004 int old_flags = get_float_exception_flags(fp_status), new_flags;
12005 float64 ret;
12006
12007 ret = float64_round_to_int(x, fp_status);
12008
12009 new_flags = get_float_exception_flags(fp_status);
12010
12011 /* Suppress any inexact exceptions the conversion produced */
12012 if (!(old_flags & float_flag_inexact)) {
12013 new_flags = get_float_exception_flags(fp_status);
12014 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12015 }
12016
12017 return ret;
12018 }
12019
12020 /* Convert ARM rounding mode to softfloat */
12021 int arm_rmode_to_sf(int rmode)
12022 {
12023 switch (rmode) {
12024 case FPROUNDING_TIEAWAY:
12025 rmode = float_round_ties_away;
12026 break;
12027 case FPROUNDING_ODD:
12028 /* FIXME: add support for TIEAWAY and ODD */
12029 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
12030 rmode);
12031 case FPROUNDING_TIEEVEN:
12032 default:
12033 rmode = float_round_nearest_even;
12034 break;
12035 case FPROUNDING_POSINF:
12036 rmode = float_round_up;
12037 break;
12038 case FPROUNDING_NEGINF:
12039 rmode = float_round_down;
12040 break;
12041 case FPROUNDING_ZERO:
12042 rmode = float_round_to_zero;
12043 break;
12044 }
12045 return rmode;
12046 }
12047
12048 /* CRC helpers.
12049 * The upper bytes of val (above the number specified by 'bytes') must have
12050 * been zeroed out by the caller.
12051 */
12052 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12053 {
12054 uint8_t buf[4];
12055
12056 stl_le_p(buf, val);
12057
12058 /* zlib crc32 converts the accumulator and output to one's complement. */
12059 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12060 }
12061
12062 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12063 {
12064 uint8_t buf[4];
12065
12066 stl_le_p(buf, val);
12067
12068 /* Linux crc32c converts the output to one's complement. */
12069 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12070 }
12071
12072 /* Return the exception level to which FP-disabled exceptions should
12073 * be taken, or 0 if FP is enabled.
12074 */
12075 static inline int fp_exception_el(CPUARMState *env)
12076 {
12077 #ifndef CONFIG_USER_ONLY
12078 int fpen;
12079 int cur_el = arm_current_el(env);
12080
12081 /* CPACR and the CPTR registers don't exist before v6, so FP is
12082 * always accessible
12083 */
12084 if (!arm_feature(env, ARM_FEATURE_V6)) {
12085 return 0;
12086 }
12087
12088 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12089 * 0, 2 : trap EL0 and EL1/PL1 accesses
12090 * 1 : trap only EL0 accesses
12091 * 3 : trap no accesses
12092 */
12093 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12094 switch (fpen) {
12095 case 0:
12096 case 2:
12097 if (cur_el == 0 || cur_el == 1) {
12098 /* Trap to PL1, which might be EL1 or EL3 */
12099 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12100 return 3;
12101 }
12102 return 1;
12103 }
12104 if (cur_el == 3 && !is_a64(env)) {
12105 /* Secure PL1 running at EL3 */
12106 return 3;
12107 }
12108 break;
12109 case 1:
12110 if (cur_el == 0) {
12111 return 1;
12112 }
12113 break;
12114 case 3:
12115 break;
12116 }
12117
12118 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12119 * check because zero bits in the registers mean "don't trap".
12120 */
12121
12122 /* CPTR_EL2 : present in v7VE or v8 */
12123 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12124 && !arm_is_secure_below_el3(env)) {
12125 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12126 return 2;
12127 }
12128
12129 /* CPTR_EL3 : present in v8 */
12130 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12131 /* Trap all FP ops to EL3 */
12132 return 3;
12133 }
12134 #endif
12135 return 0;
12136 }
12137
12138 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12139 target_ulong *cs_base, uint32_t *pflags)
12140 {
12141 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
12142 int fp_el = fp_exception_el(env);
12143 uint32_t flags;
12144
12145 if (is_a64(env)) {
12146 int sve_el = sve_exception_el(env);
12147 uint32_t zcr_len;
12148
12149 *pc = env->pc;
12150 flags = ARM_TBFLAG_AARCH64_STATE_MASK;
12151 /* Get control bits for tagged addresses */
12152 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT);
12153 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT);
12154 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT;
12155
12156 /* If SVE is disabled, but FP is enabled,
12157 then the effective len is 0. */
12158 if (sve_el != 0 && fp_el == 0) {
12159 zcr_len = 0;
12160 } else {
12161 int current_el = arm_current_el(env);
12162
12163 zcr_len = env->vfp.zcr_el[current_el <= 1 ? 1 : current_el];
12164 zcr_len &= 0xf;
12165 if (current_el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
12166 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
12167 }
12168 if (current_el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
12169 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
12170 }
12171 }
12172 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT;
12173 } else {
12174 *pc = env->regs[15];
12175 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
12176 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
12177 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
12178 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
12179 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT);
12180 if (!(access_secure_reg(env))) {
12181 flags |= ARM_TBFLAG_NS_MASK;
12182 }
12183 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
12184 || arm_el_is_aa64(env, 1)) {
12185 flags |= ARM_TBFLAG_VFPEN_MASK;
12186 }
12187 flags |= (extract32(env->cp15.c15_cpar, 0, 2)
12188 << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
12189 }
12190
12191 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT);
12192
12193 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12194 * states defined in the ARM ARM for software singlestep:
12195 * SS_ACTIVE PSTATE.SS State
12196 * 0 x Inactive (the TB flag for SS is always 0)
12197 * 1 0 Active-pending
12198 * 1 1 Active-not-pending
12199 */
12200 if (arm_singlestep_active(env)) {
12201 flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
12202 if (is_a64(env)) {
12203 if (env->pstate & PSTATE_SS) {
12204 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
12205 }
12206 } else {
12207 if (env->uncached_cpsr & PSTATE_SS) {
12208 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
12209 }
12210 }
12211 }
12212 if (arm_cpu_data_is_big_endian(env)) {
12213 flags |= ARM_TBFLAG_BE_DATA_MASK;
12214 }
12215 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT;
12216
12217 if (arm_v7m_is_handler_mode(env)) {
12218 flags |= ARM_TBFLAG_HANDLER_MASK;
12219 }
12220
12221 *pflags = flags;
12222 *cs_base = 0;
12223 }