]> git.proxmox.com Git - mirror_qemu.git/blob - target/arm/helper.c
target/arm: Ignore fsr from get_phys_addr() in do_ats_write()
[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
19 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
20
21 #ifndef CONFIG_USER_ONLY
22 /* Cacheability and shareability attributes for a memory access */
23 typedef struct ARMCacheAttrs {
24 unsigned int attrs:8; /* as in the MAIR register encoding */
25 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
26 } ARMCacheAttrs;
27
28 static bool get_phys_addr(CPUARMState *env, target_ulong address,
29 MMUAccessType access_type, ARMMMUIdx mmu_idx,
30 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
31 target_ulong *page_size, uint32_t *fsr,
32 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
33
34 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
35 MMUAccessType access_type, ARMMMUIdx mmu_idx,
36 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
37 target_ulong *page_size_ptr,
38 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
39
40 /* Security attributes for an address, as returned by v8m_security_lookup. */
41 typedef struct V8M_SAttributes {
42 bool ns;
43 bool nsc;
44 uint8_t sregion;
45 bool srvalid;
46 uint8_t iregion;
47 bool irvalid;
48 } V8M_SAttributes;
49
50 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
51 MMUAccessType access_type, ARMMMUIdx mmu_idx,
52 V8M_SAttributes *sattrs);
53
54 /* Definitions for the PMCCNTR and PMCR registers */
55 #define PMCRD 0x8
56 #define PMCRC 0x4
57 #define PMCRE 0x1
58 #endif
59
60 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
61 {
62 int nregs;
63
64 /* VFP data registers are always little-endian. */
65 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
66 if (reg < nregs) {
67 stfq_le_p(buf, env->vfp.regs[reg]);
68 return 8;
69 }
70 if (arm_feature(env, ARM_FEATURE_NEON)) {
71 /* Aliases for Q regs. */
72 nregs += 16;
73 if (reg < nregs) {
74 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
75 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
76 return 16;
77 }
78 }
79 switch (reg - nregs) {
80 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
81 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
82 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
83 }
84 return 0;
85 }
86
87 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
88 {
89 int nregs;
90
91 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
92 if (reg < nregs) {
93 env->vfp.regs[reg] = ldfq_le_p(buf);
94 return 8;
95 }
96 if (arm_feature(env, ARM_FEATURE_NEON)) {
97 nregs += 16;
98 if (reg < nregs) {
99 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
100 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
101 return 16;
102 }
103 }
104 switch (reg - nregs) {
105 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
106 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
107 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
108 }
109 return 0;
110 }
111
112 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
113 {
114 switch (reg) {
115 case 0 ... 31:
116 /* 128 bit FP register */
117 stfq_le_p(buf, env->vfp.regs[reg * 2]);
118 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
119 return 16;
120 case 32:
121 /* FPSR */
122 stl_p(buf, vfp_get_fpsr(env));
123 return 4;
124 case 33:
125 /* FPCR */
126 stl_p(buf, vfp_get_fpcr(env));
127 return 4;
128 default:
129 return 0;
130 }
131 }
132
133 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
134 {
135 switch (reg) {
136 case 0 ... 31:
137 /* 128 bit FP register */
138 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
139 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
140 return 16;
141 case 32:
142 /* FPSR */
143 vfp_set_fpsr(env, ldl_p(buf));
144 return 4;
145 case 33:
146 /* FPCR */
147 vfp_set_fpcr(env, ldl_p(buf));
148 return 4;
149 default:
150 return 0;
151 }
152 }
153
154 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
155 {
156 assert(ri->fieldoffset);
157 if (cpreg_field_is_64bit(ri)) {
158 return CPREG_FIELD64(env, ri);
159 } else {
160 return CPREG_FIELD32(env, ri);
161 }
162 }
163
164 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
165 uint64_t value)
166 {
167 assert(ri->fieldoffset);
168 if (cpreg_field_is_64bit(ri)) {
169 CPREG_FIELD64(env, ri) = value;
170 } else {
171 CPREG_FIELD32(env, ri) = value;
172 }
173 }
174
175 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
176 {
177 return (char *)env + ri->fieldoffset;
178 }
179
180 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
181 {
182 /* Raw read of a coprocessor register (as needed for migration, etc). */
183 if (ri->type & ARM_CP_CONST) {
184 return ri->resetvalue;
185 } else if (ri->raw_readfn) {
186 return ri->raw_readfn(env, ri);
187 } else if (ri->readfn) {
188 return ri->readfn(env, ri);
189 } else {
190 return raw_read(env, ri);
191 }
192 }
193
194 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
195 uint64_t v)
196 {
197 /* Raw write of a coprocessor register (as needed for migration, etc).
198 * Note that constant registers are treated as write-ignored; the
199 * caller should check for success by whether a readback gives the
200 * value written.
201 */
202 if (ri->type & ARM_CP_CONST) {
203 return;
204 } else if (ri->raw_writefn) {
205 ri->raw_writefn(env, ri, v);
206 } else if (ri->writefn) {
207 ri->writefn(env, ri, v);
208 } else {
209 raw_write(env, ri, v);
210 }
211 }
212
213 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
214 {
215 /* Return true if the regdef would cause an assertion if you called
216 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
217 * program bug for it not to have the NO_RAW flag).
218 * NB that returning false here doesn't necessarily mean that calling
219 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
220 * read/write access functions which are safe for raw use" from "has
221 * read/write access functions which have side effects but has forgotten
222 * to provide raw access functions".
223 * The tests here line up with the conditions in read/write_raw_cp_reg()
224 * and assertions in raw_read()/raw_write().
225 */
226 if ((ri->type & ARM_CP_CONST) ||
227 ri->fieldoffset ||
228 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
229 return false;
230 }
231 return true;
232 }
233
234 bool write_cpustate_to_list(ARMCPU *cpu)
235 {
236 /* Write the coprocessor state from cpu->env to the (index,value) list. */
237 int i;
238 bool ok = true;
239
240 for (i = 0; i < cpu->cpreg_array_len; i++) {
241 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
242 const ARMCPRegInfo *ri;
243
244 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
245 if (!ri) {
246 ok = false;
247 continue;
248 }
249 if (ri->type & ARM_CP_NO_RAW) {
250 continue;
251 }
252 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
253 }
254 return ok;
255 }
256
257 bool write_list_to_cpustate(ARMCPU *cpu)
258 {
259 int i;
260 bool ok = true;
261
262 for (i = 0; i < cpu->cpreg_array_len; i++) {
263 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
264 uint64_t v = cpu->cpreg_values[i];
265 const ARMCPRegInfo *ri;
266
267 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
268 if (!ri) {
269 ok = false;
270 continue;
271 }
272 if (ri->type & ARM_CP_NO_RAW) {
273 continue;
274 }
275 /* Write value and confirm it reads back as written
276 * (to catch read-only registers and partially read-only
277 * registers where the incoming migration value doesn't match)
278 */
279 write_raw_cp_reg(&cpu->env, ri, v);
280 if (read_raw_cp_reg(&cpu->env, ri) != v) {
281 ok = false;
282 }
283 }
284 return ok;
285 }
286
287 static void add_cpreg_to_list(gpointer key, gpointer opaque)
288 {
289 ARMCPU *cpu = opaque;
290 uint64_t regidx;
291 const ARMCPRegInfo *ri;
292
293 regidx = *(uint32_t *)key;
294 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
295
296 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
297 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
298 /* The value array need not be initialized at this point */
299 cpu->cpreg_array_len++;
300 }
301 }
302
303 static void count_cpreg(gpointer key, gpointer opaque)
304 {
305 ARMCPU *cpu = opaque;
306 uint64_t regidx;
307 const ARMCPRegInfo *ri;
308
309 regidx = *(uint32_t *)key;
310 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
311
312 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
313 cpu->cpreg_array_len++;
314 }
315 }
316
317 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
318 {
319 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
320 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
321
322 if (aidx > bidx) {
323 return 1;
324 }
325 if (aidx < bidx) {
326 return -1;
327 }
328 return 0;
329 }
330
331 void init_cpreg_list(ARMCPU *cpu)
332 {
333 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
334 * Note that we require cpreg_tuples[] to be sorted by key ID.
335 */
336 GList *keys;
337 int arraylen;
338
339 keys = g_hash_table_get_keys(cpu->cp_regs);
340 keys = g_list_sort(keys, cpreg_key_compare);
341
342 cpu->cpreg_array_len = 0;
343
344 g_list_foreach(keys, count_cpreg, cpu);
345
346 arraylen = cpu->cpreg_array_len;
347 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
348 cpu->cpreg_values = g_new(uint64_t, arraylen);
349 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
350 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
351 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
352 cpu->cpreg_array_len = 0;
353
354 g_list_foreach(keys, add_cpreg_to_list, cpu);
355
356 assert(cpu->cpreg_array_len == arraylen);
357
358 g_list_free(keys);
359 }
360
361 /*
362 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
363 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
364 *
365 * access_el3_aa32ns: Used to check AArch32 register views.
366 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
367 */
368 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
369 const ARMCPRegInfo *ri,
370 bool isread)
371 {
372 bool secure = arm_is_secure_below_el3(env);
373
374 assert(!arm_el_is_aa64(env, 3));
375 if (secure) {
376 return CP_ACCESS_TRAP_UNCATEGORIZED;
377 }
378 return CP_ACCESS_OK;
379 }
380
381 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
382 const ARMCPRegInfo *ri,
383 bool isread)
384 {
385 if (!arm_el_is_aa64(env, 3)) {
386 return access_el3_aa32ns(env, ri, isread);
387 }
388 return CP_ACCESS_OK;
389 }
390
391 /* Some secure-only AArch32 registers trap to EL3 if used from
392 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
393 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
394 * We assume that the .access field is set to PL1_RW.
395 */
396 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
397 const ARMCPRegInfo *ri,
398 bool isread)
399 {
400 if (arm_current_el(env) == 3) {
401 return CP_ACCESS_OK;
402 }
403 if (arm_is_secure_below_el3(env)) {
404 return CP_ACCESS_TRAP_EL3;
405 }
406 /* This will be EL1 NS and EL2 NS, which just UNDEF */
407 return CP_ACCESS_TRAP_UNCATEGORIZED;
408 }
409
410 /* Check for traps to "powerdown debug" registers, which are controlled
411 * by MDCR.TDOSA
412 */
413 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
414 bool isread)
415 {
416 int el = arm_current_el(env);
417
418 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
419 && !arm_is_secure_below_el3(env)) {
420 return CP_ACCESS_TRAP_EL2;
421 }
422 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
423 return CP_ACCESS_TRAP_EL3;
424 }
425 return CP_ACCESS_OK;
426 }
427
428 /* Check for traps to "debug ROM" registers, which are controlled
429 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
430 */
431 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
432 bool isread)
433 {
434 int el = arm_current_el(env);
435
436 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
437 && !arm_is_secure_below_el3(env)) {
438 return CP_ACCESS_TRAP_EL2;
439 }
440 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
441 return CP_ACCESS_TRAP_EL3;
442 }
443 return CP_ACCESS_OK;
444 }
445
446 /* Check for traps to general debug registers, which are controlled
447 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
448 */
449 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
450 bool isread)
451 {
452 int el = arm_current_el(env);
453
454 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
455 && !arm_is_secure_below_el3(env)) {
456 return CP_ACCESS_TRAP_EL2;
457 }
458 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
459 return CP_ACCESS_TRAP_EL3;
460 }
461 return CP_ACCESS_OK;
462 }
463
464 /* Check for traps to performance monitor registers, which are controlled
465 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
466 */
467 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
468 bool isread)
469 {
470 int el = arm_current_el(env);
471
472 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
473 && !arm_is_secure_below_el3(env)) {
474 return CP_ACCESS_TRAP_EL2;
475 }
476 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
477 return CP_ACCESS_TRAP_EL3;
478 }
479 return CP_ACCESS_OK;
480 }
481
482 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
483 {
484 ARMCPU *cpu = arm_env_get_cpu(env);
485
486 raw_write(env, ri, value);
487 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
488 }
489
490 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
491 {
492 ARMCPU *cpu = arm_env_get_cpu(env);
493
494 if (raw_read(env, ri) != value) {
495 /* Unlike real hardware the qemu TLB uses virtual addresses,
496 * not modified virtual addresses, so this causes a TLB flush.
497 */
498 tlb_flush(CPU(cpu));
499 raw_write(env, ri, value);
500 }
501 }
502
503 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
504 uint64_t value)
505 {
506 ARMCPU *cpu = arm_env_get_cpu(env);
507
508 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
509 && !extended_addresses_enabled(env)) {
510 /* For VMSA (when not using the LPAE long descriptor page table
511 * format) this register includes the ASID, so do a TLB flush.
512 * For PMSA it is purely a process ID and no action is needed.
513 */
514 tlb_flush(CPU(cpu));
515 }
516 raw_write(env, ri, value);
517 }
518
519 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
520 uint64_t value)
521 {
522 /* Invalidate all (TLBIALL) */
523 ARMCPU *cpu = arm_env_get_cpu(env);
524
525 tlb_flush(CPU(cpu));
526 }
527
528 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
529 uint64_t value)
530 {
531 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
532 ARMCPU *cpu = arm_env_get_cpu(env);
533
534 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
535 }
536
537 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
538 uint64_t value)
539 {
540 /* Invalidate by ASID (TLBIASID) */
541 ARMCPU *cpu = arm_env_get_cpu(env);
542
543 tlb_flush(CPU(cpu));
544 }
545
546 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
547 uint64_t value)
548 {
549 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
550 ARMCPU *cpu = arm_env_get_cpu(env);
551
552 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
553 }
554
555 /* IS variants of TLB operations must affect all cores */
556 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
557 uint64_t value)
558 {
559 CPUState *cs = ENV_GET_CPU(env);
560
561 tlb_flush_all_cpus_synced(cs);
562 }
563
564 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566 {
567 CPUState *cs = ENV_GET_CPU(env);
568
569 tlb_flush_all_cpus_synced(cs);
570 }
571
572 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
573 uint64_t value)
574 {
575 CPUState *cs = ENV_GET_CPU(env);
576
577 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
578 }
579
580 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
581 uint64_t value)
582 {
583 CPUState *cs = ENV_GET_CPU(env);
584
585 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
586 }
587
588 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
589 uint64_t value)
590 {
591 CPUState *cs = ENV_GET_CPU(env);
592
593 tlb_flush_by_mmuidx(cs,
594 ARMMMUIdxBit_S12NSE1 |
595 ARMMMUIdxBit_S12NSE0 |
596 ARMMMUIdxBit_S2NS);
597 }
598
599 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
600 uint64_t value)
601 {
602 CPUState *cs = ENV_GET_CPU(env);
603
604 tlb_flush_by_mmuidx_all_cpus_synced(cs,
605 ARMMMUIdxBit_S12NSE1 |
606 ARMMMUIdxBit_S12NSE0 |
607 ARMMMUIdxBit_S2NS);
608 }
609
610 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
611 uint64_t value)
612 {
613 /* Invalidate by IPA. This has to invalidate any structures that
614 * contain only stage 2 translation information, but does not need
615 * to apply to structures that contain combined stage 1 and stage 2
616 * translation information.
617 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
618 */
619 CPUState *cs = ENV_GET_CPU(env);
620 uint64_t pageaddr;
621
622 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
623 return;
624 }
625
626 pageaddr = sextract64(value << 12, 0, 40);
627
628 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
629 }
630
631 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
632 uint64_t value)
633 {
634 CPUState *cs = ENV_GET_CPU(env);
635 uint64_t pageaddr;
636
637 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
638 return;
639 }
640
641 pageaddr = sextract64(value << 12, 0, 40);
642
643 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
644 ARMMMUIdxBit_S2NS);
645 }
646
647 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
648 uint64_t value)
649 {
650 CPUState *cs = ENV_GET_CPU(env);
651
652 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
653 }
654
655 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
656 uint64_t value)
657 {
658 CPUState *cs = ENV_GET_CPU(env);
659
660 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
661 }
662
663 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
664 uint64_t value)
665 {
666 CPUState *cs = ENV_GET_CPU(env);
667 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
668
669 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
670 }
671
672 static void tlbimva_hyp_is_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_all_cpus_synced(cs, pageaddr,
679 ARMMMUIdxBit_S1E2);
680 }
681
682 static const ARMCPRegInfo cp_reginfo[] = {
683 /* Define the secure and non-secure FCSE identifier CP registers
684 * separately because there is no secure bank in V8 (no _EL3). This allows
685 * the secure register to be properly reset and migrated. There is also no
686 * v8 EL1 version of the register so the non-secure instance stands alone.
687 */
688 { .name = "FCSEIDR(NS)",
689 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
690 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
691 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
692 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
693 { .name = "FCSEIDR(S)",
694 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
695 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
696 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
697 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
698 /* Define the secure and non-secure context identifier CP registers
699 * separately because there is no secure bank in V8 (no _EL3). This allows
700 * the secure register to be properly reset and migrated. In the
701 * non-secure case, the 32-bit register will have reset and migration
702 * disabled during registration as it is handled by the 64-bit instance.
703 */
704 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
705 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
706 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
707 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
708 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
709 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
710 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
711 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
712 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
713 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
714 REGINFO_SENTINEL
715 };
716
717 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
718 /* NB: Some of these registers exist in v8 but with more precise
719 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
720 */
721 /* MMU Domain access control / MPU write buffer control */
722 { .name = "DACR",
723 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
724 .access = PL1_RW, .resetvalue = 0,
725 .writefn = dacr_write, .raw_writefn = raw_write,
726 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
727 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
728 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
729 * For v6 and v5, these mappings are overly broad.
730 */
731 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
732 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
733 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
734 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
735 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
736 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
737 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
738 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
739 /* Cache maintenance ops; some of this space may be overridden later. */
740 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
741 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
742 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
743 REGINFO_SENTINEL
744 };
745
746 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
747 /* Not all pre-v6 cores implemented this WFI, so this is slightly
748 * over-broad.
749 */
750 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
751 .access = PL1_W, .type = ARM_CP_WFI },
752 REGINFO_SENTINEL
753 };
754
755 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
756 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
757 * is UNPREDICTABLE; we choose to NOP as most implementations do).
758 */
759 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
760 .access = PL1_W, .type = ARM_CP_WFI },
761 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
762 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
763 * OMAPCP will override this space.
764 */
765 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
766 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
767 .resetvalue = 0 },
768 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
769 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
770 .resetvalue = 0 },
771 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
772 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
773 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
774 .resetvalue = 0 },
775 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
776 * implementing it as RAZ means the "debug architecture version" bits
777 * will read as a reserved value, which should cause Linux to not try
778 * to use the debug hardware.
779 */
780 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
781 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
782 /* MMU TLB control. Note that the wildcarding means we cover not just
783 * the unified TLB ops but also the dside/iside/inner-shareable variants.
784 */
785 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
786 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
787 .type = ARM_CP_NO_RAW },
788 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
789 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
790 .type = ARM_CP_NO_RAW },
791 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
792 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
793 .type = ARM_CP_NO_RAW },
794 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
795 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
796 .type = ARM_CP_NO_RAW },
797 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
798 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
799 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
800 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
801 REGINFO_SENTINEL
802 };
803
804 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
805 uint64_t value)
806 {
807 uint32_t mask = 0;
808
809 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
810 if (!arm_feature(env, ARM_FEATURE_V8)) {
811 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
812 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
813 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
814 */
815 if (arm_feature(env, ARM_FEATURE_VFP)) {
816 /* VFP coprocessor: cp10 & cp11 [23:20] */
817 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
818
819 if (!arm_feature(env, ARM_FEATURE_NEON)) {
820 /* ASEDIS [31] bit is RAO/WI */
821 value |= (1 << 31);
822 }
823
824 /* VFPv3 and upwards with NEON implement 32 double precision
825 * registers (D0-D31).
826 */
827 if (!arm_feature(env, ARM_FEATURE_NEON) ||
828 !arm_feature(env, ARM_FEATURE_VFP3)) {
829 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
830 value |= (1 << 30);
831 }
832 }
833 value &= mask;
834 }
835 env->cp15.cpacr_el1 = value;
836 }
837
838 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
839 bool isread)
840 {
841 if (arm_feature(env, ARM_FEATURE_V8)) {
842 /* Check if CPACR accesses are to be trapped to EL2 */
843 if (arm_current_el(env) == 1 &&
844 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
845 return CP_ACCESS_TRAP_EL2;
846 /* Check if CPACR accesses are to be trapped to EL3 */
847 } else if (arm_current_el(env) < 3 &&
848 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
849 return CP_ACCESS_TRAP_EL3;
850 }
851 }
852
853 return CP_ACCESS_OK;
854 }
855
856 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
857 bool isread)
858 {
859 /* Check if CPTR accesses are set to trap to EL3 */
860 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
861 return CP_ACCESS_TRAP_EL3;
862 }
863
864 return CP_ACCESS_OK;
865 }
866
867 static const ARMCPRegInfo v6_cp_reginfo[] = {
868 /* prefetch by MVA in v6, NOP in v7 */
869 { .name = "MVA_prefetch",
870 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
871 .access = PL1_W, .type = ARM_CP_NOP },
872 /* We need to break the TB after ISB to execute self-modifying code
873 * correctly and also to take any pending interrupts immediately.
874 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
875 */
876 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
877 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
878 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
879 .access = PL0_W, .type = ARM_CP_NOP },
880 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
881 .access = PL0_W, .type = ARM_CP_NOP },
882 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
883 .access = PL1_RW,
884 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
885 offsetof(CPUARMState, cp15.ifar_ns) },
886 .resetvalue = 0, },
887 /* Watchpoint Fault Address Register : should actually only be present
888 * for 1136, 1176, 11MPCore.
889 */
890 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
891 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
892 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
893 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
894 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
895 .resetvalue = 0, .writefn = cpacr_write },
896 REGINFO_SENTINEL
897 };
898
899 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
900 bool isread)
901 {
902 /* Performance monitor registers user accessibility is controlled
903 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
904 * trapping to EL2 or EL3 for other accesses.
905 */
906 int el = arm_current_el(env);
907
908 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
909 return CP_ACCESS_TRAP;
910 }
911 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
912 && !arm_is_secure_below_el3(env)) {
913 return CP_ACCESS_TRAP_EL2;
914 }
915 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
916 return CP_ACCESS_TRAP_EL3;
917 }
918
919 return CP_ACCESS_OK;
920 }
921
922 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
923 const ARMCPRegInfo *ri,
924 bool isread)
925 {
926 /* ER: event counter read trap control */
927 if (arm_feature(env, ARM_FEATURE_V8)
928 && arm_current_el(env) == 0
929 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
930 && isread) {
931 return CP_ACCESS_OK;
932 }
933
934 return pmreg_access(env, ri, isread);
935 }
936
937 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
938 const ARMCPRegInfo *ri,
939 bool isread)
940 {
941 /* SW: software increment write trap control */
942 if (arm_feature(env, ARM_FEATURE_V8)
943 && arm_current_el(env) == 0
944 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
945 && !isread) {
946 return CP_ACCESS_OK;
947 }
948
949 return pmreg_access(env, ri, isread);
950 }
951
952 #ifndef CONFIG_USER_ONLY
953
954 static CPAccessResult pmreg_access_selr(CPUARMState *env,
955 const ARMCPRegInfo *ri,
956 bool isread)
957 {
958 /* ER: event counter read trap control */
959 if (arm_feature(env, ARM_FEATURE_V8)
960 && arm_current_el(env) == 0
961 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
962 return CP_ACCESS_OK;
963 }
964
965 return pmreg_access(env, ri, isread);
966 }
967
968 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
969 const ARMCPRegInfo *ri,
970 bool isread)
971 {
972 /* CR: cycle counter read trap control */
973 if (arm_feature(env, ARM_FEATURE_V8)
974 && arm_current_el(env) == 0
975 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
976 && isread) {
977 return CP_ACCESS_OK;
978 }
979
980 return pmreg_access(env, ri, isread);
981 }
982
983 static inline bool arm_ccnt_enabled(CPUARMState *env)
984 {
985 /* This does not support checking PMCCFILTR_EL0 register */
986
987 if (!(env->cp15.c9_pmcr & PMCRE)) {
988 return false;
989 }
990
991 return true;
992 }
993
994 void pmccntr_sync(CPUARMState *env)
995 {
996 uint64_t temp_ticks;
997
998 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
999 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1000
1001 if (env->cp15.c9_pmcr & PMCRD) {
1002 /* Increment once every 64 processor clock cycles */
1003 temp_ticks /= 64;
1004 }
1005
1006 if (arm_ccnt_enabled(env)) {
1007 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1008 }
1009 }
1010
1011 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1012 uint64_t value)
1013 {
1014 pmccntr_sync(env);
1015
1016 if (value & PMCRC) {
1017 /* The counter has been reset */
1018 env->cp15.c15_ccnt = 0;
1019 }
1020
1021 /* only the DP, X, D and E bits are writable */
1022 env->cp15.c9_pmcr &= ~0x39;
1023 env->cp15.c9_pmcr |= (value & 0x39);
1024
1025 pmccntr_sync(env);
1026 }
1027
1028 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1029 {
1030 uint64_t total_ticks;
1031
1032 if (!arm_ccnt_enabled(env)) {
1033 /* Counter is disabled, do not change value */
1034 return env->cp15.c15_ccnt;
1035 }
1036
1037 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1038 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1039
1040 if (env->cp15.c9_pmcr & PMCRD) {
1041 /* Increment once every 64 processor clock cycles */
1042 total_ticks /= 64;
1043 }
1044 return total_ticks - env->cp15.c15_ccnt;
1045 }
1046
1047 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1048 uint64_t value)
1049 {
1050 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1051 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1052 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1053 * accessed.
1054 */
1055 env->cp15.c9_pmselr = value & 0x1f;
1056 }
1057
1058 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1059 uint64_t value)
1060 {
1061 uint64_t total_ticks;
1062
1063 if (!arm_ccnt_enabled(env)) {
1064 /* Counter is disabled, set the absolute value */
1065 env->cp15.c15_ccnt = value;
1066 return;
1067 }
1068
1069 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1070 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1071
1072 if (env->cp15.c9_pmcr & PMCRD) {
1073 /* Increment once every 64 processor clock cycles */
1074 total_ticks /= 64;
1075 }
1076 env->cp15.c15_ccnt = total_ticks - value;
1077 }
1078
1079 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1080 uint64_t value)
1081 {
1082 uint64_t cur_val = pmccntr_read(env, NULL);
1083
1084 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1085 }
1086
1087 #else /* CONFIG_USER_ONLY */
1088
1089 void pmccntr_sync(CPUARMState *env)
1090 {
1091 }
1092
1093 #endif
1094
1095 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1096 uint64_t value)
1097 {
1098 pmccntr_sync(env);
1099 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
1100 pmccntr_sync(env);
1101 }
1102
1103 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1104 uint64_t value)
1105 {
1106 value &= (1 << 31);
1107 env->cp15.c9_pmcnten |= value;
1108 }
1109
1110 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1111 uint64_t value)
1112 {
1113 value &= (1 << 31);
1114 env->cp15.c9_pmcnten &= ~value;
1115 }
1116
1117 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1118 uint64_t value)
1119 {
1120 env->cp15.c9_pmovsr &= ~value;
1121 }
1122
1123 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1124 uint64_t value)
1125 {
1126 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1127 * PMSELR value is equal to or greater than the number of implemented
1128 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1129 */
1130 if (env->cp15.c9_pmselr == 0x1f) {
1131 pmccfiltr_write(env, ri, value);
1132 }
1133 }
1134
1135 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1136 {
1137 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1138 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1139 */
1140 if (env->cp15.c9_pmselr == 0x1f) {
1141 return env->cp15.pmccfiltr_el0;
1142 } else {
1143 return 0;
1144 }
1145 }
1146
1147 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1148 uint64_t value)
1149 {
1150 if (arm_feature(env, ARM_FEATURE_V8)) {
1151 env->cp15.c9_pmuserenr = value & 0xf;
1152 } else {
1153 env->cp15.c9_pmuserenr = value & 1;
1154 }
1155 }
1156
1157 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1158 uint64_t value)
1159 {
1160 /* We have no event counters so only the C bit can be changed */
1161 value &= (1 << 31);
1162 env->cp15.c9_pminten |= value;
1163 }
1164
1165 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1166 uint64_t value)
1167 {
1168 value &= (1 << 31);
1169 env->cp15.c9_pminten &= ~value;
1170 }
1171
1172 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1173 uint64_t value)
1174 {
1175 /* Note that even though the AArch64 view of this register has bits
1176 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1177 * architectural requirements for bits which are RES0 only in some
1178 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1179 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1180 */
1181 raw_write(env, ri, value & ~0x1FULL);
1182 }
1183
1184 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1185 {
1186 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1187 * For bits that vary between AArch32/64, code needs to check the
1188 * current execution mode before directly using the feature bit.
1189 */
1190 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1191
1192 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1193 valid_mask &= ~SCR_HCE;
1194
1195 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1196 * supported if EL2 exists. The bit is UNK/SBZP when
1197 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1198 * when EL2 is unavailable.
1199 * On ARMv8, this bit is always available.
1200 */
1201 if (arm_feature(env, ARM_FEATURE_V7) &&
1202 !arm_feature(env, ARM_FEATURE_V8)) {
1203 valid_mask &= ~SCR_SMD;
1204 }
1205 }
1206
1207 /* Clear all-context RES0 bits. */
1208 value &= valid_mask;
1209 raw_write(env, ri, value);
1210 }
1211
1212 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1213 {
1214 ARMCPU *cpu = arm_env_get_cpu(env);
1215
1216 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1217 * bank
1218 */
1219 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1220 ri->secure & ARM_CP_SECSTATE_S);
1221
1222 return cpu->ccsidr[index];
1223 }
1224
1225 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1226 uint64_t value)
1227 {
1228 raw_write(env, ri, value & 0xf);
1229 }
1230
1231 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1232 {
1233 CPUState *cs = ENV_GET_CPU(env);
1234 uint64_t ret = 0;
1235
1236 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1237 ret |= CPSR_I;
1238 }
1239 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1240 ret |= CPSR_F;
1241 }
1242 /* External aborts are not possible in QEMU so A bit is always clear */
1243 return ret;
1244 }
1245
1246 static const ARMCPRegInfo v7_cp_reginfo[] = {
1247 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1248 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1249 .access = PL1_W, .type = ARM_CP_NOP },
1250 /* Performance monitors are implementation defined in v7,
1251 * but with an ARM recommended set of registers, which we
1252 * follow (although we don't actually implement any counters)
1253 *
1254 * Performance registers fall into three categories:
1255 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1256 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1257 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1258 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1259 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1260 */
1261 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1262 .access = PL0_RW, .type = ARM_CP_ALIAS,
1263 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1264 .writefn = pmcntenset_write,
1265 .accessfn = pmreg_access,
1266 .raw_writefn = raw_write },
1267 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1268 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1269 .access = PL0_RW, .accessfn = pmreg_access,
1270 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1271 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1272 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1273 .access = PL0_RW,
1274 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1275 .accessfn = pmreg_access,
1276 .writefn = pmcntenclr_write,
1277 .type = ARM_CP_ALIAS },
1278 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1279 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1280 .access = PL0_RW, .accessfn = pmreg_access,
1281 .type = ARM_CP_ALIAS,
1282 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1283 .writefn = pmcntenclr_write },
1284 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1285 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1286 .accessfn = pmreg_access,
1287 .writefn = pmovsr_write,
1288 .raw_writefn = raw_write },
1289 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1290 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1291 .access = PL0_RW, .accessfn = pmreg_access,
1292 .type = ARM_CP_ALIAS,
1293 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1294 .writefn = pmovsr_write,
1295 .raw_writefn = raw_write },
1296 /* Unimplemented so WI. */
1297 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1298 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
1299 #ifndef CONFIG_USER_ONLY
1300 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1301 .access = PL0_RW, .type = ARM_CP_ALIAS,
1302 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1303 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1304 .raw_writefn = raw_write},
1305 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1306 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1307 .access = PL0_RW, .accessfn = pmreg_access_selr,
1308 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1309 .writefn = pmselr_write, .raw_writefn = raw_write, },
1310 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1311 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
1312 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1313 .accessfn = pmreg_access_ccntr },
1314 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1315 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1316 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1317 .type = ARM_CP_IO,
1318 .readfn = pmccntr_read, .writefn = pmccntr_write, },
1319 #endif
1320 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1321 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1322 .writefn = pmccfiltr_write,
1323 .access = PL0_RW, .accessfn = pmreg_access,
1324 .type = ARM_CP_IO,
1325 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1326 .resetvalue = 0, },
1327 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1328 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1329 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1330 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1331 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1332 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1333 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1334 /* Unimplemented, RAZ/WI. */
1335 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1336 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1337 .accessfn = pmreg_access_xevcntr },
1338 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1339 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
1340 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1341 .resetvalue = 0,
1342 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1343 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1344 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1345 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1346 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1347 .resetvalue = 0,
1348 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1349 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1350 .access = PL1_RW, .accessfn = access_tpm,
1351 .type = ARM_CP_ALIAS,
1352 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
1353 .resetvalue = 0,
1354 .writefn = pmintenset_write, .raw_writefn = raw_write },
1355 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1356 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1357 .access = PL1_RW, .accessfn = access_tpm,
1358 .type = ARM_CP_IO,
1359 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1360 .writefn = pmintenset_write, .raw_writefn = raw_write,
1361 .resetvalue = 0x0 },
1362 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1363 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1364 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1365 .writefn = pmintenclr_write, },
1366 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1367 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1368 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1369 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1370 .writefn = pmintenclr_write },
1371 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1372 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
1373 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
1374 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1375 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
1376 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1377 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1378 offsetof(CPUARMState, cp15.csselr_ns) } },
1379 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1380 * just RAZ for all cores:
1381 */
1382 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1383 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
1384 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1385 /* Auxiliary fault status registers: these also are IMPDEF, and we
1386 * choose to RAZ/WI for all cores.
1387 */
1388 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1389 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1390 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1391 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1392 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1393 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1394 /* MAIR can just read-as-written because we don't implement caches
1395 * and so don't need to care about memory attributes.
1396 */
1397 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1398 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
1399 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
1400 .resetvalue = 0 },
1401 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1402 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1403 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1404 .resetvalue = 0 },
1405 /* For non-long-descriptor page tables these are PRRR and NMRR;
1406 * regardless they still act as reads-as-written for QEMU.
1407 */
1408 /* MAIR0/1 are defined separately from their 64-bit counterpart which
1409 * allows them to assign the correct fieldoffset based on the endianness
1410 * handled in the field definitions.
1411 */
1412 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
1413 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
1414 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1415 offsetof(CPUARMState, cp15.mair0_ns) },
1416 .resetfn = arm_cp_reset_ignore },
1417 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
1418 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
1419 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1420 offsetof(CPUARMState, cp15.mair1_ns) },
1421 .resetfn = arm_cp_reset_ignore },
1422 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1423 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
1424 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
1425 /* 32 bit ITLB invalidates */
1426 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
1427 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1428 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
1429 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1430 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
1431 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1432 /* 32 bit DTLB invalidates */
1433 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
1434 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1435 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
1436 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1437 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
1438 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1439 /* 32 bit TLB invalidates */
1440 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
1441 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1442 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
1443 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1444 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
1445 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1446 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
1447 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
1448 REGINFO_SENTINEL
1449 };
1450
1451 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1452 /* 32 bit TLB invalidates, Inner Shareable */
1453 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1454 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
1455 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1456 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
1457 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1458 .type = ARM_CP_NO_RAW, .access = PL1_W,
1459 .writefn = tlbiasid_is_write },
1460 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1461 .type = ARM_CP_NO_RAW, .access = PL1_W,
1462 .writefn = tlbimvaa_is_write },
1463 REGINFO_SENTINEL
1464 };
1465
1466 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1467 uint64_t value)
1468 {
1469 value &= 1;
1470 env->teecr = value;
1471 }
1472
1473 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1474 bool isread)
1475 {
1476 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
1477 return CP_ACCESS_TRAP;
1478 }
1479 return CP_ACCESS_OK;
1480 }
1481
1482 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1483 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1484 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1485 .resetvalue = 0,
1486 .writefn = teecr_write },
1487 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1488 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1489 .accessfn = teehbr_access, .resetvalue = 0 },
1490 REGINFO_SENTINEL
1491 };
1492
1493 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1494 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1495 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1496 .access = PL0_RW,
1497 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
1498 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1499 .access = PL0_RW,
1500 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1501 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
1502 .resetfn = arm_cp_reset_ignore },
1503 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1504 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1505 .access = PL0_R|PL1_W,
1506 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1507 .resetvalue = 0},
1508 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1509 .access = PL0_R|PL1_W,
1510 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1511 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
1512 .resetfn = arm_cp_reset_ignore },
1513 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
1514 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1515 .access = PL1_RW,
1516 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1517 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1518 .access = PL1_RW,
1519 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1520 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1521 .resetvalue = 0 },
1522 REGINFO_SENTINEL
1523 };
1524
1525 #ifndef CONFIG_USER_ONLY
1526
1527 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1528 bool isread)
1529 {
1530 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1531 * Writable only at the highest implemented exception level.
1532 */
1533 int el = arm_current_el(env);
1534
1535 switch (el) {
1536 case 0:
1537 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1538 return CP_ACCESS_TRAP;
1539 }
1540 break;
1541 case 1:
1542 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1543 arm_is_secure_below_el3(env)) {
1544 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1545 return CP_ACCESS_TRAP_UNCATEGORIZED;
1546 }
1547 break;
1548 case 2:
1549 case 3:
1550 break;
1551 }
1552
1553 if (!isread && el < arm_highest_el(env)) {
1554 return CP_ACCESS_TRAP_UNCATEGORIZED;
1555 }
1556
1557 return CP_ACCESS_OK;
1558 }
1559
1560 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1561 bool isread)
1562 {
1563 unsigned int cur_el = arm_current_el(env);
1564 bool secure = arm_is_secure(env);
1565
1566 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1567 if (cur_el == 0 &&
1568 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1569 return CP_ACCESS_TRAP;
1570 }
1571
1572 if (arm_feature(env, ARM_FEATURE_EL2) &&
1573 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1574 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1575 return CP_ACCESS_TRAP_EL2;
1576 }
1577 return CP_ACCESS_OK;
1578 }
1579
1580 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1581 bool isread)
1582 {
1583 unsigned int cur_el = arm_current_el(env);
1584 bool secure = arm_is_secure(env);
1585
1586 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1587 * EL0[PV]TEN is zero.
1588 */
1589 if (cur_el == 0 &&
1590 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1591 return CP_ACCESS_TRAP;
1592 }
1593
1594 if (arm_feature(env, ARM_FEATURE_EL2) &&
1595 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1596 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1597 return CP_ACCESS_TRAP_EL2;
1598 }
1599 return CP_ACCESS_OK;
1600 }
1601
1602 static CPAccessResult gt_pct_access(CPUARMState *env,
1603 const ARMCPRegInfo *ri,
1604 bool isread)
1605 {
1606 return gt_counter_access(env, GTIMER_PHYS, isread);
1607 }
1608
1609 static CPAccessResult gt_vct_access(CPUARMState *env,
1610 const ARMCPRegInfo *ri,
1611 bool isread)
1612 {
1613 return gt_counter_access(env, GTIMER_VIRT, isread);
1614 }
1615
1616 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1617 bool isread)
1618 {
1619 return gt_timer_access(env, GTIMER_PHYS, isread);
1620 }
1621
1622 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1623 bool isread)
1624 {
1625 return gt_timer_access(env, GTIMER_VIRT, isread);
1626 }
1627
1628 static CPAccessResult gt_stimer_access(CPUARMState *env,
1629 const ARMCPRegInfo *ri,
1630 bool isread)
1631 {
1632 /* The AArch64 register view of the secure physical timer is
1633 * always accessible from EL3, and configurably accessible from
1634 * Secure EL1.
1635 */
1636 switch (arm_current_el(env)) {
1637 case 1:
1638 if (!arm_is_secure(env)) {
1639 return CP_ACCESS_TRAP;
1640 }
1641 if (!(env->cp15.scr_el3 & SCR_ST)) {
1642 return CP_ACCESS_TRAP_EL3;
1643 }
1644 return CP_ACCESS_OK;
1645 case 0:
1646 case 2:
1647 return CP_ACCESS_TRAP;
1648 case 3:
1649 return CP_ACCESS_OK;
1650 default:
1651 g_assert_not_reached();
1652 }
1653 }
1654
1655 static uint64_t gt_get_countervalue(CPUARMState *env)
1656 {
1657 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1658 }
1659
1660 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1661 {
1662 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1663
1664 if (gt->ctl & 1) {
1665 /* Timer enabled: calculate and set current ISTATUS, irq, and
1666 * reset timer to when ISTATUS next has to change
1667 */
1668 uint64_t offset = timeridx == GTIMER_VIRT ?
1669 cpu->env.cp15.cntvoff_el2 : 0;
1670 uint64_t count = gt_get_countervalue(&cpu->env);
1671 /* Note that this must be unsigned 64 bit arithmetic: */
1672 int istatus = count - offset >= gt->cval;
1673 uint64_t nexttick;
1674 int irqstate;
1675
1676 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1677
1678 irqstate = (istatus && !(gt->ctl & 2));
1679 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1680
1681 if (istatus) {
1682 /* Next transition is when count rolls back over to zero */
1683 nexttick = UINT64_MAX;
1684 } else {
1685 /* Next transition is when we hit cval */
1686 nexttick = gt->cval + offset;
1687 }
1688 /* Note that the desired next expiry time might be beyond the
1689 * signed-64-bit range of a QEMUTimer -- in this case we just
1690 * set the timer for as far in the future as possible. When the
1691 * timer expires we will reset the timer for any remaining period.
1692 */
1693 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1694 nexttick = INT64_MAX / GTIMER_SCALE;
1695 }
1696 timer_mod(cpu->gt_timer[timeridx], nexttick);
1697 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
1698 } else {
1699 /* Timer disabled: ISTATUS and timer output always clear */
1700 gt->ctl &= ~4;
1701 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1702 timer_del(cpu->gt_timer[timeridx]);
1703 trace_arm_gt_recalc_disabled(timeridx);
1704 }
1705 }
1706
1707 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1708 int timeridx)
1709 {
1710 ARMCPU *cpu = arm_env_get_cpu(env);
1711
1712 timer_del(cpu->gt_timer[timeridx]);
1713 }
1714
1715 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1716 {
1717 return gt_get_countervalue(env);
1718 }
1719
1720 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1721 {
1722 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1723 }
1724
1725 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1726 int timeridx,
1727 uint64_t value)
1728 {
1729 trace_arm_gt_cval_write(timeridx, value);
1730 env->cp15.c14_timer[timeridx].cval = value;
1731 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1732 }
1733
1734 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1735 int timeridx)
1736 {
1737 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1738
1739 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1740 (gt_get_countervalue(env) - offset));
1741 }
1742
1743 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1744 int timeridx,
1745 uint64_t value)
1746 {
1747 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1748
1749 trace_arm_gt_tval_write(timeridx, value);
1750 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
1751 sextract64(value, 0, 32);
1752 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1753 }
1754
1755 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1756 int timeridx,
1757 uint64_t value)
1758 {
1759 ARMCPU *cpu = arm_env_get_cpu(env);
1760 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1761
1762 trace_arm_gt_ctl_write(timeridx, value);
1763 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1764 if ((oldval ^ value) & 1) {
1765 /* Enable toggled */
1766 gt_recalc_timer(cpu, timeridx);
1767 } else if ((oldval ^ value) & 2) {
1768 /* IMASK toggled: don't need to recalculate,
1769 * just set the interrupt line based on ISTATUS
1770 */
1771 int irqstate = (oldval & 4) && !(value & 2);
1772
1773 trace_arm_gt_imask_toggle(timeridx, irqstate);
1774 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1775 }
1776 }
1777
1778 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1779 {
1780 gt_timer_reset(env, ri, GTIMER_PHYS);
1781 }
1782
1783 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1784 uint64_t value)
1785 {
1786 gt_cval_write(env, ri, GTIMER_PHYS, value);
1787 }
1788
1789 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1790 {
1791 return gt_tval_read(env, ri, GTIMER_PHYS);
1792 }
1793
1794 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1795 uint64_t value)
1796 {
1797 gt_tval_write(env, ri, GTIMER_PHYS, value);
1798 }
1799
1800 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1801 uint64_t value)
1802 {
1803 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1804 }
1805
1806 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1807 {
1808 gt_timer_reset(env, ri, GTIMER_VIRT);
1809 }
1810
1811 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1812 uint64_t value)
1813 {
1814 gt_cval_write(env, ri, GTIMER_VIRT, value);
1815 }
1816
1817 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1818 {
1819 return gt_tval_read(env, ri, GTIMER_VIRT);
1820 }
1821
1822 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1823 uint64_t value)
1824 {
1825 gt_tval_write(env, ri, GTIMER_VIRT, value);
1826 }
1827
1828 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1829 uint64_t value)
1830 {
1831 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1832 }
1833
1834 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1835 uint64_t value)
1836 {
1837 ARMCPU *cpu = arm_env_get_cpu(env);
1838
1839 trace_arm_gt_cntvoff_write(value);
1840 raw_write(env, ri, value);
1841 gt_recalc_timer(cpu, GTIMER_VIRT);
1842 }
1843
1844 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1845 {
1846 gt_timer_reset(env, ri, GTIMER_HYP);
1847 }
1848
1849 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1850 uint64_t value)
1851 {
1852 gt_cval_write(env, ri, GTIMER_HYP, value);
1853 }
1854
1855 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1856 {
1857 return gt_tval_read(env, ri, GTIMER_HYP);
1858 }
1859
1860 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1861 uint64_t value)
1862 {
1863 gt_tval_write(env, ri, GTIMER_HYP, value);
1864 }
1865
1866 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1867 uint64_t value)
1868 {
1869 gt_ctl_write(env, ri, GTIMER_HYP, value);
1870 }
1871
1872 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1873 {
1874 gt_timer_reset(env, ri, GTIMER_SEC);
1875 }
1876
1877 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1878 uint64_t value)
1879 {
1880 gt_cval_write(env, ri, GTIMER_SEC, value);
1881 }
1882
1883 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1884 {
1885 return gt_tval_read(env, ri, GTIMER_SEC);
1886 }
1887
1888 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1889 uint64_t value)
1890 {
1891 gt_tval_write(env, ri, GTIMER_SEC, value);
1892 }
1893
1894 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1895 uint64_t value)
1896 {
1897 gt_ctl_write(env, ri, GTIMER_SEC, value);
1898 }
1899
1900 void arm_gt_ptimer_cb(void *opaque)
1901 {
1902 ARMCPU *cpu = opaque;
1903
1904 gt_recalc_timer(cpu, GTIMER_PHYS);
1905 }
1906
1907 void arm_gt_vtimer_cb(void *opaque)
1908 {
1909 ARMCPU *cpu = opaque;
1910
1911 gt_recalc_timer(cpu, GTIMER_VIRT);
1912 }
1913
1914 void arm_gt_htimer_cb(void *opaque)
1915 {
1916 ARMCPU *cpu = opaque;
1917
1918 gt_recalc_timer(cpu, GTIMER_HYP);
1919 }
1920
1921 void arm_gt_stimer_cb(void *opaque)
1922 {
1923 ARMCPU *cpu = opaque;
1924
1925 gt_recalc_timer(cpu, GTIMER_SEC);
1926 }
1927
1928 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1929 /* Note that CNTFRQ is purely reads-as-written for the benefit
1930 * of software; writing it doesn't actually change the timer frequency.
1931 * Our reset value matches the fixed frequency we implement the timer at.
1932 */
1933 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1934 .type = ARM_CP_ALIAS,
1935 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1936 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1937 },
1938 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1939 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1940 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1941 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1942 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1943 },
1944 /* overall control: mostly access permissions */
1945 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1946 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1947 .access = PL1_RW,
1948 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1949 .resetvalue = 0,
1950 },
1951 /* per-timer control */
1952 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1953 .secure = ARM_CP_SECSTATE_NS,
1954 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1955 .accessfn = gt_ptimer_access,
1956 .fieldoffset = offsetoflow32(CPUARMState,
1957 cp15.c14_timer[GTIMER_PHYS].ctl),
1958 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1959 },
1960 { .name = "CNTP_CTL(S)",
1961 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1962 .secure = ARM_CP_SECSTATE_S,
1963 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1964 .accessfn = gt_ptimer_access,
1965 .fieldoffset = offsetoflow32(CPUARMState,
1966 cp15.c14_timer[GTIMER_SEC].ctl),
1967 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1968 },
1969 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1970 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1971 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1972 .accessfn = gt_ptimer_access,
1973 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1974 .resetvalue = 0,
1975 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1976 },
1977 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1978 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1979 .accessfn = gt_vtimer_access,
1980 .fieldoffset = offsetoflow32(CPUARMState,
1981 cp15.c14_timer[GTIMER_VIRT].ctl),
1982 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1983 },
1984 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1985 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1986 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1987 .accessfn = gt_vtimer_access,
1988 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1989 .resetvalue = 0,
1990 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1991 },
1992 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1993 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1994 .secure = ARM_CP_SECSTATE_NS,
1995 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1996 .accessfn = gt_ptimer_access,
1997 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
1998 },
1999 { .name = "CNTP_TVAL(S)",
2000 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2001 .secure = ARM_CP_SECSTATE_S,
2002 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2003 .accessfn = gt_ptimer_access,
2004 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2005 },
2006 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2007 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2008 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2009 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2010 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2011 },
2012 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2013 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2014 .accessfn = gt_vtimer_access,
2015 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2016 },
2017 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2018 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2019 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2020 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2021 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2022 },
2023 /* The counter itself */
2024 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2025 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2026 .accessfn = gt_pct_access,
2027 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2028 },
2029 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2030 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2031 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2032 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2033 },
2034 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2035 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2036 .accessfn = gt_vct_access,
2037 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2038 },
2039 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2040 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2041 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2042 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2043 },
2044 /* Comparison value, indicating when the timer goes off */
2045 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2046 .secure = ARM_CP_SECSTATE_NS,
2047 .access = PL1_RW | PL0_R,
2048 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2049 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2050 .accessfn = gt_ptimer_access,
2051 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2052 },
2053 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
2054 .secure = ARM_CP_SECSTATE_S,
2055 .access = PL1_RW | PL0_R,
2056 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2057 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2058 .accessfn = gt_ptimer_access,
2059 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2060 },
2061 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2062 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2063 .access = PL1_RW | PL0_R,
2064 .type = ARM_CP_IO,
2065 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2066 .resetvalue = 0, .accessfn = gt_ptimer_access,
2067 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2068 },
2069 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2070 .access = PL1_RW | PL0_R,
2071 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2072 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2073 .accessfn = gt_vtimer_access,
2074 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2075 },
2076 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2077 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2078 .access = PL1_RW | PL0_R,
2079 .type = ARM_CP_IO,
2080 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2081 .resetvalue = 0, .accessfn = gt_vtimer_access,
2082 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2083 },
2084 /* Secure timer -- this is actually restricted to only EL3
2085 * and configurably Secure-EL1 via the accessfn.
2086 */
2087 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2088 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2089 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2090 .accessfn = gt_stimer_access,
2091 .readfn = gt_sec_tval_read,
2092 .writefn = gt_sec_tval_write,
2093 .resetfn = gt_sec_timer_reset,
2094 },
2095 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2096 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2097 .type = ARM_CP_IO, .access = PL1_RW,
2098 .accessfn = gt_stimer_access,
2099 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2100 .resetvalue = 0,
2101 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2102 },
2103 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2104 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2105 .type = ARM_CP_IO, .access = PL1_RW,
2106 .accessfn = gt_stimer_access,
2107 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2108 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2109 },
2110 REGINFO_SENTINEL
2111 };
2112
2113 #else
2114 /* In user-mode none of the generic timer registers are accessible,
2115 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
2116 * so instead just don't register any of them.
2117 */
2118 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2119 REGINFO_SENTINEL
2120 };
2121
2122 #endif
2123
2124 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2125 {
2126 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2127 raw_write(env, ri, value);
2128 } else if (arm_feature(env, ARM_FEATURE_V7)) {
2129 raw_write(env, ri, value & 0xfffff6ff);
2130 } else {
2131 raw_write(env, ri, value & 0xfffff1ff);
2132 }
2133 }
2134
2135 #ifndef CONFIG_USER_ONLY
2136 /* get_phys_addr() isn't present for user-mode-only targets */
2137
2138 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2139 bool isread)
2140 {
2141 if (ri->opc2 & 4) {
2142 /* The ATS12NSO* operations must trap to EL3 if executed in
2143 * Secure EL1 (which can only happen if EL3 is AArch64).
2144 * They are simply UNDEF if executed from NS EL1.
2145 * They function normally from EL2 or EL3.
2146 */
2147 if (arm_current_el(env) == 1) {
2148 if (arm_is_secure_below_el3(env)) {
2149 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2150 }
2151 return CP_ACCESS_TRAP_UNCATEGORIZED;
2152 }
2153 }
2154 return CP_ACCESS_OK;
2155 }
2156
2157 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
2158 MMUAccessType access_type, ARMMMUIdx mmu_idx)
2159 {
2160 hwaddr phys_addr;
2161 target_ulong page_size;
2162 int prot;
2163 uint32_t fsr_unused;
2164 bool ret;
2165 uint64_t par64;
2166 MemTxAttrs attrs = {};
2167 ARMMMUFaultInfo fi = {};
2168 ARMCacheAttrs cacheattrs = {};
2169
2170 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
2171 &prot, &page_size, &fsr_unused, &fi, &cacheattrs);
2172 /* TODO: this is not the correct condition to use to decide whether
2173 * to report a PAR in 64-bit or 32-bit format.
2174 */
2175 if (arm_s1_regime_using_lpae_format(env, mmu_idx)) {
2176 /* Create a 64-bit PAR */
2177 par64 = (1 << 11); /* LPAE bit always set */
2178 if (!ret) {
2179 par64 |= phys_addr & ~0xfffULL;
2180 if (!attrs.secure) {
2181 par64 |= (1 << 9); /* NS */
2182 }
2183 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2184 par64 |= cacheattrs.shareability << 7; /* SH */
2185 } else {
2186 uint32_t fsr = arm_fi_to_lfsc(&fi);
2187
2188 par64 |= 1; /* F */
2189 par64 |= (fsr & 0x3f) << 1; /* FS */
2190 /* Note that S2WLK and FSTAGE are always zero, because we don't
2191 * implement virtualization and therefore there can't be a stage 2
2192 * fault.
2193 */
2194 }
2195 } else {
2196 /* fsr is a DFSR/IFSR value for the short descriptor
2197 * translation table format (with WnR always clear).
2198 * Convert it to a 32-bit PAR.
2199 */
2200 if (!ret) {
2201 /* We do not set any attribute bits in the PAR */
2202 if (page_size == (1 << 24)
2203 && arm_feature(env, ARM_FEATURE_V7)) {
2204 par64 = (phys_addr & 0xff000000) | (1 << 1);
2205 } else {
2206 par64 = phys_addr & 0xfffff000;
2207 }
2208 if (!attrs.secure) {
2209 par64 |= (1 << 9); /* NS */
2210 }
2211 } else {
2212 uint32_t fsr = arm_fi_to_sfsc(&fi);
2213
2214 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2215 ((fsr & 0xf) << 1) | 1;
2216 }
2217 }
2218 return par64;
2219 }
2220
2221 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2222 {
2223 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2224 uint64_t par64;
2225 ARMMMUIdx mmu_idx;
2226 int el = arm_current_el(env);
2227 bool secure = arm_is_secure_below_el3(env);
2228
2229 switch (ri->opc2 & 6) {
2230 case 0:
2231 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2232 switch (el) {
2233 case 3:
2234 mmu_idx = ARMMMUIdx_S1E3;
2235 break;
2236 case 2:
2237 mmu_idx = ARMMMUIdx_S1NSE1;
2238 break;
2239 case 1:
2240 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2241 break;
2242 default:
2243 g_assert_not_reached();
2244 }
2245 break;
2246 case 2:
2247 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2248 switch (el) {
2249 case 3:
2250 mmu_idx = ARMMMUIdx_S1SE0;
2251 break;
2252 case 2:
2253 mmu_idx = ARMMMUIdx_S1NSE0;
2254 break;
2255 case 1:
2256 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2257 break;
2258 default:
2259 g_assert_not_reached();
2260 }
2261 break;
2262 case 4:
2263 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2264 mmu_idx = ARMMMUIdx_S12NSE1;
2265 break;
2266 case 6:
2267 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2268 mmu_idx = ARMMMUIdx_S12NSE0;
2269 break;
2270 default:
2271 g_assert_not_reached();
2272 }
2273
2274 par64 = do_ats_write(env, value, access_type, mmu_idx);
2275
2276 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2277 }
2278
2279 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2280 uint64_t value)
2281 {
2282 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2283 uint64_t par64;
2284
2285 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2286
2287 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2288 }
2289
2290 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2291 bool isread)
2292 {
2293 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2294 return CP_ACCESS_TRAP;
2295 }
2296 return CP_ACCESS_OK;
2297 }
2298
2299 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2300 uint64_t value)
2301 {
2302 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2303 ARMMMUIdx mmu_idx;
2304 int secure = arm_is_secure_below_el3(env);
2305
2306 switch (ri->opc2 & 6) {
2307 case 0:
2308 switch (ri->opc1) {
2309 case 0: /* AT S1E1R, AT S1E1W */
2310 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2311 break;
2312 case 4: /* AT S1E2R, AT S1E2W */
2313 mmu_idx = ARMMMUIdx_S1E2;
2314 break;
2315 case 6: /* AT S1E3R, AT S1E3W */
2316 mmu_idx = ARMMMUIdx_S1E3;
2317 break;
2318 default:
2319 g_assert_not_reached();
2320 }
2321 break;
2322 case 2: /* AT S1E0R, AT S1E0W */
2323 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2324 break;
2325 case 4: /* AT S12E1R, AT S12E1W */
2326 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
2327 break;
2328 case 6: /* AT S12E0R, AT S12E0W */
2329 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
2330 break;
2331 default:
2332 g_assert_not_reached();
2333 }
2334
2335 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
2336 }
2337 #endif
2338
2339 static const ARMCPRegInfo vapa_cp_reginfo[] = {
2340 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2341 .access = PL1_RW, .resetvalue = 0,
2342 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2343 offsetoflow32(CPUARMState, cp15.par_ns) },
2344 .writefn = par_write },
2345 #ifndef CONFIG_USER_ONLY
2346 /* This underdecoding is safe because the reginfo is NO_RAW. */
2347 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
2348 .access = PL1_W, .accessfn = ats_access,
2349 .writefn = ats_write, .type = ARM_CP_NO_RAW },
2350 #endif
2351 REGINFO_SENTINEL
2352 };
2353
2354 /* Return basic MPU access permission bits. */
2355 static uint32_t simple_mpu_ap_bits(uint32_t val)
2356 {
2357 uint32_t ret;
2358 uint32_t mask;
2359 int i;
2360 ret = 0;
2361 mask = 3;
2362 for (i = 0; i < 16; i += 2) {
2363 ret |= (val >> i) & mask;
2364 mask <<= 2;
2365 }
2366 return ret;
2367 }
2368
2369 /* Pad basic MPU access permission bits to extended format. */
2370 static uint32_t extended_mpu_ap_bits(uint32_t val)
2371 {
2372 uint32_t ret;
2373 uint32_t mask;
2374 int i;
2375 ret = 0;
2376 mask = 3;
2377 for (i = 0; i < 16; i += 2) {
2378 ret |= (val & mask) << i;
2379 mask <<= 2;
2380 }
2381 return ret;
2382 }
2383
2384 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2385 uint64_t value)
2386 {
2387 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
2388 }
2389
2390 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2391 {
2392 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
2393 }
2394
2395 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2396 uint64_t value)
2397 {
2398 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
2399 }
2400
2401 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2402 {
2403 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
2404 }
2405
2406 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2407 {
2408 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2409
2410 if (!u32p) {
2411 return 0;
2412 }
2413
2414 u32p += env->pmsav7.rnr[M_REG_NS];
2415 return *u32p;
2416 }
2417
2418 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2419 uint64_t value)
2420 {
2421 ARMCPU *cpu = arm_env_get_cpu(env);
2422 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2423
2424 if (!u32p) {
2425 return;
2426 }
2427
2428 u32p += env->pmsav7.rnr[M_REG_NS];
2429 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
2430 *u32p = value;
2431 }
2432
2433 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2434 uint64_t value)
2435 {
2436 ARMCPU *cpu = arm_env_get_cpu(env);
2437 uint32_t nrgs = cpu->pmsav7_dregion;
2438
2439 if (value >= nrgs) {
2440 qemu_log_mask(LOG_GUEST_ERROR,
2441 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2442 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2443 return;
2444 }
2445
2446 raw_write(env, ri, value);
2447 }
2448
2449 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2450 /* Reset for all these registers is handled in arm_cpu_reset(),
2451 * because the PMSAv7 is also used by M-profile CPUs, which do
2452 * not register cpregs but still need the state to be reset.
2453 */
2454 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2455 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2456 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2457 .readfn = pmsav7_read, .writefn = pmsav7_write,
2458 .resetfn = arm_cp_reset_ignore },
2459 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2460 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2461 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2462 .readfn = pmsav7_read, .writefn = pmsav7_write,
2463 .resetfn = arm_cp_reset_ignore },
2464 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2465 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2466 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2467 .readfn = pmsav7_read, .writefn = pmsav7_write,
2468 .resetfn = arm_cp_reset_ignore },
2469 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2470 .access = PL1_RW,
2471 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
2472 .writefn = pmsav7_rgnr_write,
2473 .resetfn = arm_cp_reset_ignore },
2474 REGINFO_SENTINEL
2475 };
2476
2477 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2478 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2479 .access = PL1_RW, .type = ARM_CP_ALIAS,
2480 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2481 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2482 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2483 .access = PL1_RW, .type = ARM_CP_ALIAS,
2484 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2485 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2486 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2487 .access = PL1_RW,
2488 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2489 .resetvalue = 0, },
2490 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2491 .access = PL1_RW,
2492 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2493 .resetvalue = 0, },
2494 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2495 .access = PL1_RW,
2496 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2497 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2498 .access = PL1_RW,
2499 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
2500 /* Protection region base and size registers */
2501 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2502 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2503 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2504 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2505 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2506 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2507 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2508 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2509 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2510 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2511 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2512 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2513 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2514 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2515 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2516 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2517 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2518 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2519 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2520 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2521 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2522 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2523 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2524 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
2525 REGINFO_SENTINEL
2526 };
2527
2528 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2529 uint64_t value)
2530 {
2531 TCR *tcr = raw_ptr(env, ri);
2532 int maskshift = extract32(value, 0, 3);
2533
2534 if (!arm_feature(env, ARM_FEATURE_V8)) {
2535 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2536 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2537 * using Long-desciptor translation table format */
2538 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2539 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2540 /* In an implementation that includes the Security Extensions
2541 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2542 * Short-descriptor translation table format.
2543 */
2544 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2545 } else {
2546 value &= TTBCR_N;
2547 }
2548 }
2549
2550 /* Update the masks corresponding to the TCR bank being written
2551 * Note that we always calculate mask and base_mask, but
2552 * they are only used for short-descriptor tables (ie if EAE is 0);
2553 * for long-descriptor tables the TCR fields are used differently
2554 * and the mask and base_mask values are meaningless.
2555 */
2556 tcr->raw_tcr = value;
2557 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2558 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
2559 }
2560
2561 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2562 uint64_t value)
2563 {
2564 ARMCPU *cpu = arm_env_get_cpu(env);
2565
2566 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2567 /* With LPAE the TTBCR could result in a change of ASID
2568 * via the TTBCR.A1 bit, so do a TLB flush.
2569 */
2570 tlb_flush(CPU(cpu));
2571 }
2572 vmsa_ttbcr_raw_write(env, ri, value);
2573 }
2574
2575 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2576 {
2577 TCR *tcr = raw_ptr(env, ri);
2578
2579 /* Reset both the TCR as well as the masks corresponding to the bank of
2580 * the TCR being reset.
2581 */
2582 tcr->raw_tcr = 0;
2583 tcr->mask = 0;
2584 tcr->base_mask = 0xffffc000u;
2585 }
2586
2587 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2588 uint64_t value)
2589 {
2590 ARMCPU *cpu = arm_env_get_cpu(env);
2591 TCR *tcr = raw_ptr(env, ri);
2592
2593 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
2594 tlb_flush(CPU(cpu));
2595 tcr->raw_tcr = value;
2596 }
2597
2598 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2599 uint64_t value)
2600 {
2601 /* 64 bit accesses to the TTBRs can change the ASID and so we
2602 * must flush the TLB.
2603 */
2604 if (cpreg_field_is_64bit(ri)) {
2605 ARMCPU *cpu = arm_env_get_cpu(env);
2606
2607 tlb_flush(CPU(cpu));
2608 }
2609 raw_write(env, ri, value);
2610 }
2611
2612 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2613 uint64_t value)
2614 {
2615 ARMCPU *cpu = arm_env_get_cpu(env);
2616 CPUState *cs = CPU(cpu);
2617
2618 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2619 if (raw_read(env, ri) != value) {
2620 tlb_flush_by_mmuidx(cs,
2621 ARMMMUIdxBit_S12NSE1 |
2622 ARMMMUIdxBit_S12NSE0 |
2623 ARMMMUIdxBit_S2NS);
2624 raw_write(env, ri, value);
2625 }
2626 }
2627
2628 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
2629 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2630 .access = PL1_RW, .type = ARM_CP_ALIAS,
2631 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
2632 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
2633 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2634 .access = PL1_RW, .resetvalue = 0,
2635 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2636 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
2637 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2638 .access = PL1_RW, .resetvalue = 0,
2639 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2640 offsetof(CPUARMState, cp15.dfar_ns) } },
2641 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2642 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2643 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2644 .resetvalue = 0, },
2645 REGINFO_SENTINEL
2646 };
2647
2648 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
2649 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2650 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2651 .access = PL1_RW,
2652 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
2653 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
2654 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2655 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2656 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2657 offsetof(CPUARMState, cp15.ttbr0_ns) } },
2658 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
2659 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2660 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2661 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2662 offsetof(CPUARMState, cp15.ttbr1_ns) } },
2663 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2664 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2665 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2666 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2667 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
2668 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2669 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
2670 .raw_writefn = vmsa_ttbcr_raw_write,
2671 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2672 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
2673 REGINFO_SENTINEL
2674 };
2675
2676 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2677 uint64_t value)
2678 {
2679 env->cp15.c15_ticonfig = value & 0xe7;
2680 /* The OS_TYPE bit in this register changes the reported CPUID! */
2681 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2682 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
2683 }
2684
2685 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2686 uint64_t value)
2687 {
2688 env->cp15.c15_threadid = value & 0xffff;
2689 }
2690
2691 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2692 uint64_t value)
2693 {
2694 /* Wait-for-interrupt (deprecated) */
2695 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
2696 }
2697
2698 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2699 uint64_t value)
2700 {
2701 /* On OMAP there are registers indicating the max/min index of dcache lines
2702 * containing a dirty line; cache flush operations have to reset these.
2703 */
2704 env->cp15.c15_i_max = 0x000;
2705 env->cp15.c15_i_min = 0xff0;
2706 }
2707
2708 static const ARMCPRegInfo omap_cp_reginfo[] = {
2709 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2710 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
2711 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
2712 .resetvalue = 0, },
2713 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2714 .access = PL1_RW, .type = ARM_CP_NOP },
2715 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2716 .access = PL1_RW,
2717 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2718 .writefn = omap_ticonfig_write },
2719 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2720 .access = PL1_RW,
2721 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2722 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2723 .access = PL1_RW, .resetvalue = 0xff0,
2724 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2725 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2726 .access = PL1_RW,
2727 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2728 .writefn = omap_threadid_write },
2729 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2730 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2731 .type = ARM_CP_NO_RAW,
2732 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2733 /* TODO: Peripheral port remap register:
2734 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2735 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2736 * when MMU is off.
2737 */
2738 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
2739 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
2740 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
2741 .writefn = omap_cachemaint_write },
2742 { .name = "C9", .cp = 15, .crn = 9,
2743 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2744 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
2745 REGINFO_SENTINEL
2746 };
2747
2748 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2749 uint64_t value)
2750 {
2751 env->cp15.c15_cpar = value & 0x3fff;
2752 }
2753
2754 static const ARMCPRegInfo xscale_cp_reginfo[] = {
2755 { .name = "XSCALE_CPAR",
2756 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2757 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2758 .writefn = xscale_cpar_write, },
2759 { .name = "XSCALE_AUXCR",
2760 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2761 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2762 .resetvalue = 0, },
2763 /* XScale specific cache-lockdown: since we have no cache we NOP these
2764 * and hope the guest does not really rely on cache behaviour.
2765 */
2766 { .name = "XSCALE_LOCK_ICACHE_LINE",
2767 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2768 .access = PL1_W, .type = ARM_CP_NOP },
2769 { .name = "XSCALE_UNLOCK_ICACHE",
2770 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2771 .access = PL1_W, .type = ARM_CP_NOP },
2772 { .name = "XSCALE_DCACHE_LOCK",
2773 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2774 .access = PL1_RW, .type = ARM_CP_NOP },
2775 { .name = "XSCALE_UNLOCK_DCACHE",
2776 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2777 .access = PL1_W, .type = ARM_CP_NOP },
2778 REGINFO_SENTINEL
2779 };
2780
2781 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2782 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2783 * implementation of this implementation-defined space.
2784 * Ideally this should eventually disappear in favour of actually
2785 * implementing the correct behaviour for all cores.
2786 */
2787 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2788 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2789 .access = PL1_RW,
2790 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
2791 .resetvalue = 0 },
2792 REGINFO_SENTINEL
2793 };
2794
2795 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2796 /* Cache status: RAZ because we have no cache so it's always clean */
2797 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
2798 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2799 .resetvalue = 0 },
2800 REGINFO_SENTINEL
2801 };
2802
2803 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2804 /* We never have a a block transfer operation in progress */
2805 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
2806 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2807 .resetvalue = 0 },
2808 /* The cache ops themselves: these all NOP for QEMU */
2809 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2810 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2811 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2812 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2813 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2814 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2815 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2816 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2817 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2818 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2819 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2820 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2821 REGINFO_SENTINEL
2822 };
2823
2824 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2825 /* The cache test-and-clean instructions always return (1 << 30)
2826 * to indicate that there are no dirty cache lines.
2827 */
2828 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2829 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2830 .resetvalue = (1 << 30) },
2831 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2832 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2833 .resetvalue = (1 << 30) },
2834 REGINFO_SENTINEL
2835 };
2836
2837 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2838 /* Ignore ReadBuffer accesses */
2839 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2840 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2841 .access = PL1_RW, .resetvalue = 0,
2842 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2843 REGINFO_SENTINEL
2844 };
2845
2846 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2847 {
2848 ARMCPU *cpu = arm_env_get_cpu(env);
2849 unsigned int cur_el = arm_current_el(env);
2850 bool secure = arm_is_secure(env);
2851
2852 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2853 return env->cp15.vpidr_el2;
2854 }
2855 return raw_read(env, ri);
2856 }
2857
2858 static uint64_t mpidr_read_val(CPUARMState *env)
2859 {
2860 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2861 uint64_t mpidr = cpu->mp_affinity;
2862
2863 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2864 mpidr |= (1U << 31);
2865 /* Cores which are uniprocessor (non-coherent)
2866 * but still implement the MP extensions set
2867 * bit 30. (For instance, Cortex-R5).
2868 */
2869 if (cpu->mp_is_up) {
2870 mpidr |= (1u << 30);
2871 }
2872 }
2873 return mpidr;
2874 }
2875
2876 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2877 {
2878 unsigned int cur_el = arm_current_el(env);
2879 bool secure = arm_is_secure(env);
2880
2881 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2882 return env->cp15.vmpidr_el2;
2883 }
2884 return mpidr_read_val(env);
2885 }
2886
2887 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2888 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2889 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2890 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2891 REGINFO_SENTINEL
2892 };
2893
2894 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2895 /* NOP AMAIR0/1 */
2896 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2897 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2898 .access = PL1_RW, .type = ARM_CP_CONST,
2899 .resetvalue = 0 },
2900 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2901 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2902 .access = PL1_RW, .type = ARM_CP_CONST,
2903 .resetvalue = 0 },
2904 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2905 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2906 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2907 offsetof(CPUARMState, cp15.par_ns)} },
2908 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2909 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2910 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2911 offsetof(CPUARMState, cp15.ttbr0_ns) },
2912 .writefn = vmsa_ttbr_write, },
2913 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2914 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2915 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2916 offsetof(CPUARMState, cp15.ttbr1_ns) },
2917 .writefn = vmsa_ttbr_write, },
2918 REGINFO_SENTINEL
2919 };
2920
2921 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2922 {
2923 return vfp_get_fpcr(env);
2924 }
2925
2926 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2927 uint64_t value)
2928 {
2929 vfp_set_fpcr(env, value);
2930 }
2931
2932 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2933 {
2934 return vfp_get_fpsr(env);
2935 }
2936
2937 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938 uint64_t value)
2939 {
2940 vfp_set_fpsr(env, value);
2941 }
2942
2943 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2944 bool isread)
2945 {
2946 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2947 return CP_ACCESS_TRAP;
2948 }
2949 return CP_ACCESS_OK;
2950 }
2951
2952 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2953 uint64_t value)
2954 {
2955 env->daif = value & PSTATE_DAIF;
2956 }
2957
2958 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2959 const ARMCPRegInfo *ri,
2960 bool isread)
2961 {
2962 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2963 * SCTLR_EL1.UCI is set.
2964 */
2965 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2966 return CP_ACCESS_TRAP;
2967 }
2968 return CP_ACCESS_OK;
2969 }
2970
2971 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2972 * Page D4-1736 (DDI0487A.b)
2973 */
2974
2975 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2976 uint64_t value)
2977 {
2978 CPUState *cs = ENV_GET_CPU(env);
2979
2980 if (arm_is_secure_below_el3(env)) {
2981 tlb_flush_by_mmuidx(cs,
2982 ARMMMUIdxBit_S1SE1 |
2983 ARMMMUIdxBit_S1SE0);
2984 } else {
2985 tlb_flush_by_mmuidx(cs,
2986 ARMMMUIdxBit_S12NSE1 |
2987 ARMMMUIdxBit_S12NSE0);
2988 }
2989 }
2990
2991 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2992 uint64_t value)
2993 {
2994 CPUState *cs = ENV_GET_CPU(env);
2995 bool sec = arm_is_secure_below_el3(env);
2996
2997 if (sec) {
2998 tlb_flush_by_mmuidx_all_cpus_synced(cs,
2999 ARMMMUIdxBit_S1SE1 |
3000 ARMMMUIdxBit_S1SE0);
3001 } else {
3002 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3003 ARMMMUIdxBit_S12NSE1 |
3004 ARMMMUIdxBit_S12NSE0);
3005 }
3006 }
3007
3008 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3009 uint64_t value)
3010 {
3011 /* Note that the 'ALL' scope must invalidate both stage 1 and
3012 * stage 2 translations, whereas most other scopes only invalidate
3013 * stage 1 translations.
3014 */
3015 ARMCPU *cpu = arm_env_get_cpu(env);
3016 CPUState *cs = CPU(cpu);
3017
3018 if (arm_is_secure_below_el3(env)) {
3019 tlb_flush_by_mmuidx(cs,
3020 ARMMMUIdxBit_S1SE1 |
3021 ARMMMUIdxBit_S1SE0);
3022 } else {
3023 if (arm_feature(env, ARM_FEATURE_EL2)) {
3024 tlb_flush_by_mmuidx(cs,
3025 ARMMMUIdxBit_S12NSE1 |
3026 ARMMMUIdxBit_S12NSE0 |
3027 ARMMMUIdxBit_S2NS);
3028 } else {
3029 tlb_flush_by_mmuidx(cs,
3030 ARMMMUIdxBit_S12NSE1 |
3031 ARMMMUIdxBit_S12NSE0);
3032 }
3033 }
3034 }
3035
3036 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3037 uint64_t value)
3038 {
3039 ARMCPU *cpu = arm_env_get_cpu(env);
3040 CPUState *cs = CPU(cpu);
3041
3042 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
3043 }
3044
3045 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3046 uint64_t value)
3047 {
3048 ARMCPU *cpu = arm_env_get_cpu(env);
3049 CPUState *cs = CPU(cpu);
3050
3051 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
3052 }
3053
3054 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3055 uint64_t value)
3056 {
3057 /* Note that the 'ALL' scope must invalidate both stage 1 and
3058 * stage 2 translations, whereas most other scopes only invalidate
3059 * stage 1 translations.
3060 */
3061 CPUState *cs = ENV_GET_CPU(env);
3062 bool sec = arm_is_secure_below_el3(env);
3063 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
3064
3065 if (sec) {
3066 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3067 ARMMMUIdxBit_S1SE1 |
3068 ARMMMUIdxBit_S1SE0);
3069 } else if (has_el2) {
3070 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3071 ARMMMUIdxBit_S12NSE1 |
3072 ARMMMUIdxBit_S12NSE0 |
3073 ARMMMUIdxBit_S2NS);
3074 } else {
3075 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3076 ARMMMUIdxBit_S12NSE1 |
3077 ARMMMUIdxBit_S12NSE0);
3078 }
3079 }
3080
3081 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3082 uint64_t value)
3083 {
3084 CPUState *cs = ENV_GET_CPU(env);
3085
3086 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
3087 }
3088
3089 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3090 uint64_t value)
3091 {
3092 CPUState *cs = ENV_GET_CPU(env);
3093
3094 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
3095 }
3096
3097 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3098 uint64_t value)
3099 {
3100 /* Invalidate by VA, EL1&0 (AArch64 version).
3101 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3102 * since we don't support flush-for-specific-ASID-only or
3103 * flush-last-level-only.
3104 */
3105 ARMCPU *cpu = arm_env_get_cpu(env);
3106 CPUState *cs = CPU(cpu);
3107 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3108
3109 if (arm_is_secure_below_el3(env)) {
3110 tlb_flush_page_by_mmuidx(cs, pageaddr,
3111 ARMMMUIdxBit_S1SE1 |
3112 ARMMMUIdxBit_S1SE0);
3113 } else {
3114 tlb_flush_page_by_mmuidx(cs, pageaddr,
3115 ARMMMUIdxBit_S12NSE1 |
3116 ARMMMUIdxBit_S12NSE0);
3117 }
3118 }
3119
3120 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3121 uint64_t value)
3122 {
3123 /* Invalidate by VA, EL2
3124 * Currently handles both VAE2 and VALE2, since we don't support
3125 * flush-last-level-only.
3126 */
3127 ARMCPU *cpu = arm_env_get_cpu(env);
3128 CPUState *cs = CPU(cpu);
3129 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3130
3131 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
3132 }
3133
3134 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3135 uint64_t value)
3136 {
3137 /* Invalidate by VA, EL3
3138 * Currently handles both VAE3 and VALE3, since we don't support
3139 * flush-last-level-only.
3140 */
3141 ARMCPU *cpu = arm_env_get_cpu(env);
3142 CPUState *cs = CPU(cpu);
3143 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3144
3145 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
3146 }
3147
3148 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3149 uint64_t value)
3150 {
3151 ARMCPU *cpu = arm_env_get_cpu(env);
3152 CPUState *cs = CPU(cpu);
3153 bool sec = arm_is_secure_below_el3(env);
3154 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3155
3156 if (sec) {
3157 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3158 ARMMMUIdxBit_S1SE1 |
3159 ARMMMUIdxBit_S1SE0);
3160 } else {
3161 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3162 ARMMMUIdxBit_S12NSE1 |
3163 ARMMMUIdxBit_S12NSE0);
3164 }
3165 }
3166
3167 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3168 uint64_t value)
3169 {
3170 CPUState *cs = ENV_GET_CPU(env);
3171 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3172
3173 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3174 ARMMMUIdxBit_S1E2);
3175 }
3176
3177 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3178 uint64_t value)
3179 {
3180 CPUState *cs = ENV_GET_CPU(env);
3181 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3182
3183 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3184 ARMMMUIdxBit_S1E3);
3185 }
3186
3187 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3188 uint64_t value)
3189 {
3190 /* Invalidate by IPA. This has to invalidate any structures that
3191 * contain only stage 2 translation information, but does not need
3192 * to apply to structures that contain combined stage 1 and stage 2
3193 * translation information.
3194 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3195 */
3196 ARMCPU *cpu = arm_env_get_cpu(env);
3197 CPUState *cs = CPU(cpu);
3198 uint64_t pageaddr;
3199
3200 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3201 return;
3202 }
3203
3204 pageaddr = sextract64(value << 12, 0, 48);
3205
3206 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
3207 }
3208
3209 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3210 uint64_t value)
3211 {
3212 CPUState *cs = ENV_GET_CPU(env);
3213 uint64_t pageaddr;
3214
3215 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3216 return;
3217 }
3218
3219 pageaddr = sextract64(value << 12, 0, 48);
3220
3221 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3222 ARMMMUIdxBit_S2NS);
3223 }
3224
3225 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3226 bool isread)
3227 {
3228 /* We don't implement EL2, so the only control on DC ZVA is the
3229 * bit in the SCTLR which can prohibit access for EL0.
3230 */
3231 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
3232 return CP_ACCESS_TRAP;
3233 }
3234 return CP_ACCESS_OK;
3235 }
3236
3237 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3238 {
3239 ARMCPU *cpu = arm_env_get_cpu(env);
3240 int dzp_bit = 1 << 4;
3241
3242 /* DZP indicates whether DC ZVA access is allowed */
3243 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
3244 dzp_bit = 0;
3245 }
3246 return cpu->dcz_blocksize | dzp_bit;
3247 }
3248
3249 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3250 bool isread)
3251 {
3252 if (!(env->pstate & PSTATE_SP)) {
3253 /* Access to SP_EL0 is undefined if it's being used as
3254 * the stack pointer.
3255 */
3256 return CP_ACCESS_TRAP_UNCATEGORIZED;
3257 }
3258 return CP_ACCESS_OK;
3259 }
3260
3261 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3262 {
3263 return env->pstate & PSTATE_SP;
3264 }
3265
3266 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3267 {
3268 update_spsel(env, val);
3269 }
3270
3271 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3272 uint64_t value)
3273 {
3274 ARMCPU *cpu = arm_env_get_cpu(env);
3275
3276 if (raw_read(env, ri) == value) {
3277 /* Skip the TLB flush if nothing actually changed; Linux likes
3278 * to do a lot of pointless SCTLR writes.
3279 */
3280 return;
3281 }
3282
3283 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3284 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3285 value &= ~SCTLR_M;
3286 }
3287
3288 raw_write(env, ri, value);
3289 /* ??? Lots of these bits are not implemented. */
3290 /* This may enable/disable the MMU, so do a TLB flush. */
3291 tlb_flush(CPU(cpu));
3292 }
3293
3294 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3295 bool isread)
3296 {
3297 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
3298 return CP_ACCESS_TRAP_FP_EL2;
3299 }
3300 if (env->cp15.cptr_el[3] & CPTR_TFP) {
3301 return CP_ACCESS_TRAP_FP_EL3;
3302 }
3303 return CP_ACCESS_OK;
3304 }
3305
3306 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3307 uint64_t value)
3308 {
3309 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3310 }
3311
3312 static const ARMCPRegInfo v8_cp_reginfo[] = {
3313 /* Minimal set of EL0-visible registers. This will need to be expanded
3314 * significantly for system emulation of AArch64 CPUs.
3315 */
3316 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3317 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3318 .access = PL0_RW, .type = ARM_CP_NZCV },
3319 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3320 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
3321 .type = ARM_CP_NO_RAW,
3322 .access = PL0_RW, .accessfn = aa64_daif_access,
3323 .fieldoffset = offsetof(CPUARMState, daif),
3324 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
3325 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3326 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3327 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3328 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3329 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3330 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
3331 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3332 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
3333 .access = PL0_R, .type = ARM_CP_NO_RAW,
3334 .readfn = aa64_dczid_read },
3335 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3336 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3337 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3338 #ifndef CONFIG_USER_ONLY
3339 /* Avoid overhead of an access check that always passes in user-mode */
3340 .accessfn = aa64_zva_access,
3341 #endif
3342 },
3343 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3344 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3345 .access = PL1_R, .type = ARM_CP_CURRENTEL },
3346 /* Cache ops: all NOPs since we don't emulate caches */
3347 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3348 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3349 .access = PL1_W, .type = ARM_CP_NOP },
3350 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3351 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3352 .access = PL1_W, .type = ARM_CP_NOP },
3353 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3354 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3355 .access = PL0_W, .type = ARM_CP_NOP,
3356 .accessfn = aa64_cacheop_access },
3357 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3358 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3359 .access = PL1_W, .type = ARM_CP_NOP },
3360 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3361 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3362 .access = PL1_W, .type = ARM_CP_NOP },
3363 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3364 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3365 .access = PL0_W, .type = ARM_CP_NOP,
3366 .accessfn = aa64_cacheop_access },
3367 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3368 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3369 .access = PL1_W, .type = ARM_CP_NOP },
3370 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3372 .access = PL0_W, .type = ARM_CP_NOP,
3373 .accessfn = aa64_cacheop_access },
3374 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3375 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3376 .access = PL0_W, .type = ARM_CP_NOP,
3377 .accessfn = aa64_cacheop_access },
3378 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3379 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3380 .access = PL1_W, .type = ARM_CP_NOP },
3381 /* TLBI operations */
3382 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
3383 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
3384 .access = PL1_W, .type = ARM_CP_NO_RAW,
3385 .writefn = tlbi_aa64_vmalle1is_write },
3386 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
3387 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
3388 .access = PL1_W, .type = ARM_CP_NO_RAW,
3389 .writefn = tlbi_aa64_vae1is_write },
3390 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
3391 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
3392 .access = PL1_W, .type = ARM_CP_NO_RAW,
3393 .writefn = tlbi_aa64_vmalle1is_write },
3394 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
3395 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
3396 .access = PL1_W, .type = ARM_CP_NO_RAW,
3397 .writefn = tlbi_aa64_vae1is_write },
3398 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
3399 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3400 .access = PL1_W, .type = ARM_CP_NO_RAW,
3401 .writefn = tlbi_aa64_vae1is_write },
3402 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
3403 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3404 .access = PL1_W, .type = ARM_CP_NO_RAW,
3405 .writefn = tlbi_aa64_vae1is_write },
3406 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
3407 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
3408 .access = PL1_W, .type = ARM_CP_NO_RAW,
3409 .writefn = tlbi_aa64_vmalle1_write },
3410 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
3411 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
3412 .access = PL1_W, .type = ARM_CP_NO_RAW,
3413 .writefn = tlbi_aa64_vae1_write },
3414 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
3415 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
3416 .access = PL1_W, .type = ARM_CP_NO_RAW,
3417 .writefn = tlbi_aa64_vmalle1_write },
3418 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
3419 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
3420 .access = PL1_W, .type = ARM_CP_NO_RAW,
3421 .writefn = tlbi_aa64_vae1_write },
3422 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
3423 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3424 .access = PL1_W, .type = ARM_CP_NO_RAW,
3425 .writefn = tlbi_aa64_vae1_write },
3426 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
3427 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3428 .access = PL1_W, .type = ARM_CP_NO_RAW,
3429 .writefn = tlbi_aa64_vae1_write },
3430 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3432 .access = PL2_W, .type = ARM_CP_NO_RAW,
3433 .writefn = tlbi_aa64_ipas2e1is_write },
3434 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3435 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3436 .access = PL2_W, .type = ARM_CP_NO_RAW,
3437 .writefn = tlbi_aa64_ipas2e1is_write },
3438 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3439 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3440 .access = PL2_W, .type = ARM_CP_NO_RAW,
3441 .writefn = tlbi_aa64_alle1is_write },
3442 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3443 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3444 .access = PL2_W, .type = ARM_CP_NO_RAW,
3445 .writefn = tlbi_aa64_alle1is_write },
3446 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3447 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3448 .access = PL2_W, .type = ARM_CP_NO_RAW,
3449 .writefn = tlbi_aa64_ipas2e1_write },
3450 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3451 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3452 .access = PL2_W, .type = ARM_CP_NO_RAW,
3453 .writefn = tlbi_aa64_ipas2e1_write },
3454 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3455 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3456 .access = PL2_W, .type = ARM_CP_NO_RAW,
3457 .writefn = tlbi_aa64_alle1_write },
3458 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3459 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3460 .access = PL2_W, .type = ARM_CP_NO_RAW,
3461 .writefn = tlbi_aa64_alle1is_write },
3462 #ifndef CONFIG_USER_ONLY
3463 /* 64 bit address translation operations */
3464 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3465 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3466 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3467 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3468 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3469 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3470 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3471 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3472 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3473 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3474 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3475 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3476 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3477 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3478 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3479 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3480 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3481 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3482 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3483 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3484 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3485 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3486 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3487 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3488 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3489 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3490 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3491 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3492 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3493 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3494 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3495 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3496 .type = ARM_CP_ALIAS,
3497 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3498 .access = PL1_RW, .resetvalue = 0,
3499 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3500 .writefn = par_write },
3501 #endif
3502 /* TLB invalidate last level of translation table walk */
3503 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3504 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3505 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3506 .type = ARM_CP_NO_RAW, .access = PL1_W,
3507 .writefn = tlbimvaa_is_write },
3508 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3509 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3510 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3511 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3512 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3513 .type = ARM_CP_NO_RAW, .access = PL2_W,
3514 .writefn = tlbimva_hyp_write },
3515 { .name = "TLBIMVALHIS",
3516 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3517 .type = ARM_CP_NO_RAW, .access = PL2_W,
3518 .writefn = tlbimva_hyp_is_write },
3519 { .name = "TLBIIPAS2",
3520 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3521 .type = ARM_CP_NO_RAW, .access = PL2_W,
3522 .writefn = tlbiipas2_write },
3523 { .name = "TLBIIPAS2IS",
3524 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3525 .type = ARM_CP_NO_RAW, .access = PL2_W,
3526 .writefn = tlbiipas2_is_write },
3527 { .name = "TLBIIPAS2L",
3528 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3529 .type = ARM_CP_NO_RAW, .access = PL2_W,
3530 .writefn = tlbiipas2_write },
3531 { .name = "TLBIIPAS2LIS",
3532 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3533 .type = ARM_CP_NO_RAW, .access = PL2_W,
3534 .writefn = tlbiipas2_is_write },
3535 /* 32 bit cache operations */
3536 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3537 .type = ARM_CP_NOP, .access = PL1_W },
3538 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3539 .type = ARM_CP_NOP, .access = PL1_W },
3540 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3541 .type = ARM_CP_NOP, .access = PL1_W },
3542 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3543 .type = ARM_CP_NOP, .access = PL1_W },
3544 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3545 .type = ARM_CP_NOP, .access = PL1_W },
3546 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3547 .type = ARM_CP_NOP, .access = PL1_W },
3548 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3549 .type = ARM_CP_NOP, .access = PL1_W },
3550 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3551 .type = ARM_CP_NOP, .access = PL1_W },
3552 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3553 .type = ARM_CP_NOP, .access = PL1_W },
3554 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3555 .type = ARM_CP_NOP, .access = PL1_W },
3556 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3557 .type = ARM_CP_NOP, .access = PL1_W },
3558 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3559 .type = ARM_CP_NOP, .access = PL1_W },
3560 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3561 .type = ARM_CP_NOP, .access = PL1_W },
3562 /* MMU Domain access control / MPU write buffer control */
3563 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3564 .access = PL1_RW, .resetvalue = 0,
3565 .writefn = dacr_write, .raw_writefn = raw_write,
3566 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3567 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3568 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3569 .type = ARM_CP_ALIAS,
3570 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3571 .access = PL1_RW,
3572 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3573 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3574 .type = ARM_CP_ALIAS,
3575 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3576 .access = PL1_RW,
3577 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
3578 /* We rely on the access checks not allowing the guest to write to the
3579 * state field when SPSel indicates that it's being used as the stack
3580 * pointer.
3581 */
3582 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3583 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3584 .access = PL1_RW, .accessfn = sp_el0_access,
3585 .type = ARM_CP_ALIAS,
3586 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3587 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3588 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3589 .access = PL2_RW, .type = ARM_CP_ALIAS,
3590 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3591 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3592 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3593 .type = ARM_CP_NO_RAW,
3594 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3595 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3596 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3597 .type = ARM_CP_ALIAS,
3598 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3599 .access = PL2_RW, .accessfn = fpexc32_access },
3600 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3601 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3602 .access = PL2_RW, .resetvalue = 0,
3603 .writefn = dacr_write, .raw_writefn = raw_write,
3604 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3605 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3606 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3607 .access = PL2_RW, .resetvalue = 0,
3608 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3609 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3610 .type = ARM_CP_ALIAS,
3611 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3612 .access = PL2_RW,
3613 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3614 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3615 .type = ARM_CP_ALIAS,
3616 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3617 .access = PL2_RW,
3618 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3619 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3620 .type = ARM_CP_ALIAS,
3621 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3622 .access = PL2_RW,
3623 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3624 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3625 .type = ARM_CP_ALIAS,
3626 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3627 .access = PL2_RW,
3628 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
3629 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3630 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3631 .resetvalue = 0,
3632 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3633 { .name = "SDCR", .type = ARM_CP_ALIAS,
3634 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3635 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3636 .writefn = sdcr_write,
3637 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
3638 REGINFO_SENTINEL
3639 };
3640
3641 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
3642 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3643 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3644 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3645 .access = PL2_RW,
3646 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3647 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3648 .type = ARM_CP_NO_RAW,
3649 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3650 .access = PL2_RW,
3651 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3652 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3653 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3654 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3655 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3656 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3657 .access = PL2_RW, .type = ARM_CP_CONST,
3658 .resetvalue = 0 },
3659 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3660 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3661 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3662 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3663 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3664 .access = PL2_RW, .type = ARM_CP_CONST,
3665 .resetvalue = 0 },
3666 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3667 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3668 .access = PL2_RW, .type = ARM_CP_CONST,
3669 .resetvalue = 0 },
3670 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3671 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3672 .access = PL2_RW, .type = ARM_CP_CONST,
3673 .resetvalue = 0 },
3674 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3675 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3676 .access = PL2_RW, .type = ARM_CP_CONST,
3677 .resetvalue = 0 },
3678 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3679 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3680 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3681 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3682 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3683 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3684 .type = ARM_CP_CONST, .resetvalue = 0 },
3685 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3686 .cp = 15, .opc1 = 6, .crm = 2,
3687 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3688 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3689 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3690 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3691 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3692 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3693 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3694 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3695 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3696 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3697 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3698 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3699 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3700 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3701 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3702 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3703 .resetvalue = 0 },
3704 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3705 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3706 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3707 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3708 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3709 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3710 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3711 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3712 .resetvalue = 0 },
3713 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3714 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3715 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3716 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3717 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3718 .resetvalue = 0 },
3719 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3720 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3721 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3722 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3723 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3724 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3725 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3726 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3727 .access = PL2_RW, .accessfn = access_tda,
3728 .type = ARM_CP_CONST, .resetvalue = 0 },
3729 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3730 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3731 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3732 .type = ARM_CP_CONST, .resetvalue = 0 },
3733 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3734 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3735 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3736 REGINFO_SENTINEL
3737 };
3738
3739 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3740 {
3741 ARMCPU *cpu = arm_env_get_cpu(env);
3742 uint64_t valid_mask = HCR_MASK;
3743
3744 if (arm_feature(env, ARM_FEATURE_EL3)) {
3745 valid_mask &= ~HCR_HCD;
3746 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3747 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3748 * However, if we're using the SMC PSCI conduit then QEMU is
3749 * effectively acting like EL3 firmware and so the guest at
3750 * EL2 should retain the ability to prevent EL1 from being
3751 * able to make SMC calls into the ersatz firmware, so in
3752 * that case HCR.TSC should be read/write.
3753 */
3754 valid_mask &= ~HCR_TSC;
3755 }
3756
3757 /* Clear RES0 bits. */
3758 value &= valid_mask;
3759
3760 /* These bits change the MMU setup:
3761 * HCR_VM enables stage 2 translation
3762 * HCR_PTW forbids certain page-table setups
3763 * HCR_DC Disables stage1 and enables stage2 translation
3764 */
3765 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3766 tlb_flush(CPU(cpu));
3767 }
3768 raw_write(env, ri, value);
3769 }
3770
3771 static const ARMCPRegInfo el2_cp_reginfo[] = {
3772 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3773 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3774 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3775 .writefn = hcr_write },
3776 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3777 .type = ARM_CP_ALIAS,
3778 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3779 .access = PL2_RW,
3780 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3781 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3782 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3783 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3784 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3785 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3786 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3787 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3788 .type = ARM_CP_ALIAS,
3789 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3790 .access = PL2_RW,
3791 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
3792 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3793 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3794 .access = PL2_RW, .writefn = vbar_write,
3795 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3796 .resetvalue = 0 },
3797 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3798 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3799 .access = PL3_RW, .type = ARM_CP_ALIAS,
3800 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3801 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3802 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3803 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3804 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3805 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3806 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3807 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3808 .resetvalue = 0 },
3809 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3810 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3811 .access = PL2_RW, .type = ARM_CP_ALIAS,
3812 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3813 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3814 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3815 .access = PL2_RW, .type = ARM_CP_CONST,
3816 .resetvalue = 0 },
3817 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3818 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3819 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3820 .access = PL2_RW, .type = ARM_CP_CONST,
3821 .resetvalue = 0 },
3822 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3823 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3824 .access = PL2_RW, .type = ARM_CP_CONST,
3825 .resetvalue = 0 },
3826 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3827 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3828 .access = PL2_RW, .type = ARM_CP_CONST,
3829 .resetvalue = 0 },
3830 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3831 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3832 .access = PL2_RW,
3833 /* no .writefn needed as this can't cause an ASID change;
3834 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3835 */
3836 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3837 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3838 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3839 .type = ARM_CP_ALIAS,
3840 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3841 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3842 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3843 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3844 .access = PL2_RW,
3845 /* no .writefn needed as this can't cause an ASID change;
3846 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3847 */
3848 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3849 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3850 .cp = 15, .opc1 = 6, .crm = 2,
3851 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3852 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3853 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3854 .writefn = vttbr_write },
3855 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3856 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3857 .access = PL2_RW, .writefn = vttbr_write,
3858 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3859 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3860 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3861 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3862 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3863 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3864 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3865 .access = PL2_RW, .resetvalue = 0,
3866 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3867 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3868 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3869 .access = PL2_RW, .resetvalue = 0,
3870 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3871 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3872 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3873 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3874 { .name = "TLBIALLNSNH",
3875 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3876 .type = ARM_CP_NO_RAW, .access = PL2_W,
3877 .writefn = tlbiall_nsnh_write },
3878 { .name = "TLBIALLNSNHIS",
3879 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3880 .type = ARM_CP_NO_RAW, .access = PL2_W,
3881 .writefn = tlbiall_nsnh_is_write },
3882 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3883 .type = ARM_CP_NO_RAW, .access = PL2_W,
3884 .writefn = tlbiall_hyp_write },
3885 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3886 .type = ARM_CP_NO_RAW, .access = PL2_W,
3887 .writefn = tlbiall_hyp_is_write },
3888 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3889 .type = ARM_CP_NO_RAW, .access = PL2_W,
3890 .writefn = tlbimva_hyp_write },
3891 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3892 .type = ARM_CP_NO_RAW, .access = PL2_W,
3893 .writefn = tlbimva_hyp_is_write },
3894 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3895 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3896 .type = ARM_CP_NO_RAW, .access = PL2_W,
3897 .writefn = tlbi_aa64_alle2_write },
3898 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3899 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3900 .type = ARM_CP_NO_RAW, .access = PL2_W,
3901 .writefn = tlbi_aa64_vae2_write },
3902 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3903 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3904 .access = PL2_W, .type = ARM_CP_NO_RAW,
3905 .writefn = tlbi_aa64_vae2_write },
3906 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3907 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3908 .access = PL2_W, .type = ARM_CP_NO_RAW,
3909 .writefn = tlbi_aa64_alle2is_write },
3910 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3911 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3912 .type = ARM_CP_NO_RAW, .access = PL2_W,
3913 .writefn = tlbi_aa64_vae2is_write },
3914 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3915 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3916 .access = PL2_W, .type = ARM_CP_NO_RAW,
3917 .writefn = tlbi_aa64_vae2is_write },
3918 #ifndef CONFIG_USER_ONLY
3919 /* Unlike the other EL2-related AT operations, these must
3920 * UNDEF from EL3 if EL2 is not implemented, which is why we
3921 * define them here rather than with the rest of the AT ops.
3922 */
3923 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3924 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3925 .access = PL2_W, .accessfn = at_s1e2_access,
3926 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3927 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3928 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3929 .access = PL2_W, .accessfn = at_s1e2_access,
3930 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3931 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3932 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3933 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3934 * to behave as if SCR.NS was 1.
3935 */
3936 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3937 .access = PL2_W,
3938 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3939 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3940 .access = PL2_W,
3941 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3942 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3943 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3944 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3945 * reset values as IMPDEF. We choose to reset to 3 to comply with
3946 * both ARMv7 and ARMv8.
3947 */
3948 .access = PL2_RW, .resetvalue = 3,
3949 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
3950 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3951 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3952 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3953 .writefn = gt_cntvoff_write,
3954 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3955 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3956 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3957 .writefn = gt_cntvoff_write,
3958 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3959 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3960 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3961 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3962 .type = ARM_CP_IO, .access = PL2_RW,
3963 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3964 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3965 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3966 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3967 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3968 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3969 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3970 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
3971 .resetfn = gt_hyp_timer_reset,
3972 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3973 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3974 .type = ARM_CP_IO,
3975 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3976 .access = PL2_RW,
3977 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3978 .resetvalue = 0,
3979 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
3980 #endif
3981 /* The only field of MDCR_EL2 that has a defined architectural reset value
3982 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3983 * don't impelment any PMU event counters, so using zero as a reset
3984 * value for MDCR_EL2 is okay
3985 */
3986 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3987 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3988 .access = PL2_RW, .resetvalue = 0,
3989 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
3990 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3991 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3992 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3993 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3994 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3995 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3996 .access = PL2_RW,
3997 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3998 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3999 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4000 .access = PL2_RW,
4001 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
4002 REGINFO_SENTINEL
4003 };
4004
4005 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4006 bool isread)
4007 {
4008 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4009 * At Secure EL1 it traps to EL3.
4010 */
4011 if (arm_current_el(env) == 3) {
4012 return CP_ACCESS_OK;
4013 }
4014 if (arm_is_secure_below_el3(env)) {
4015 return CP_ACCESS_TRAP_EL3;
4016 }
4017 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4018 if (isread) {
4019 return CP_ACCESS_OK;
4020 }
4021 return CP_ACCESS_TRAP_UNCATEGORIZED;
4022 }
4023
4024 static const ARMCPRegInfo el3_cp_reginfo[] = {
4025 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4026 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4027 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4028 .resetvalue = 0, .writefn = scr_write },
4029 { .name = "SCR", .type = ARM_CP_ALIAS,
4030 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
4031 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4032 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
4033 .writefn = scr_write },
4034 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4035 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4036 .access = PL3_RW, .resetvalue = 0,
4037 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4038 { .name = "SDER",
4039 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4040 .access = PL3_RW, .resetvalue = 0,
4041 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
4042 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4043 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4044 .writefn = vbar_write, .resetvalue = 0,
4045 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
4046 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4047 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4048 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4049 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
4050 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4051 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
4052 .access = PL3_RW,
4053 /* no .writefn needed as this can't cause an ASID change;
4054 * we must provide a .raw_writefn and .resetfn because we handle
4055 * reset and migration for the AArch32 TTBCR(S), which might be
4056 * using mask and base_mask.
4057 */
4058 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
4059 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
4060 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
4061 .type = ARM_CP_ALIAS,
4062 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4063 .access = PL3_RW,
4064 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
4065 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
4066 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4067 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
4068 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4069 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4070 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
4071 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
4072 .type = ARM_CP_ALIAS,
4073 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
4074 .access = PL3_RW,
4075 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
4076 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4077 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4078 .access = PL3_RW, .writefn = vbar_write,
4079 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4080 .resetvalue = 0 },
4081 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4082 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4083 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4084 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4085 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4086 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4087 .access = PL3_RW, .resetvalue = 0,
4088 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
4089 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4090 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4091 .access = PL3_RW, .type = ARM_CP_CONST,
4092 .resetvalue = 0 },
4093 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4094 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4095 .access = PL3_RW, .type = ARM_CP_CONST,
4096 .resetvalue = 0 },
4097 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4098 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4099 .access = PL3_RW, .type = ARM_CP_CONST,
4100 .resetvalue = 0 },
4101 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4102 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4103 .access = PL3_W, .type = ARM_CP_NO_RAW,
4104 .writefn = tlbi_aa64_alle3is_write },
4105 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4106 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4107 .access = PL3_W, .type = ARM_CP_NO_RAW,
4108 .writefn = tlbi_aa64_vae3is_write },
4109 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4110 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4111 .access = PL3_W, .type = ARM_CP_NO_RAW,
4112 .writefn = tlbi_aa64_vae3is_write },
4113 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4114 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4115 .access = PL3_W, .type = ARM_CP_NO_RAW,
4116 .writefn = tlbi_aa64_alle3_write },
4117 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4118 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4119 .access = PL3_W, .type = ARM_CP_NO_RAW,
4120 .writefn = tlbi_aa64_vae3_write },
4121 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4122 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4123 .access = PL3_W, .type = ARM_CP_NO_RAW,
4124 .writefn = tlbi_aa64_vae3_write },
4125 REGINFO_SENTINEL
4126 };
4127
4128 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4129 bool isread)
4130 {
4131 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4132 * but the AArch32 CTR has its own reginfo struct)
4133 */
4134 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
4135 return CP_ACCESS_TRAP;
4136 }
4137 return CP_ACCESS_OK;
4138 }
4139
4140 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4141 uint64_t value)
4142 {
4143 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4144 * read via a bit in OSLSR_EL1.
4145 */
4146 int oslock;
4147
4148 if (ri->state == ARM_CP_STATE_AA32) {
4149 oslock = (value == 0xC5ACCE55);
4150 } else {
4151 oslock = value & 1;
4152 }
4153
4154 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4155 }
4156
4157 static const ARMCPRegInfo debug_cp_reginfo[] = {
4158 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
4159 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4160 * unlike DBGDRAR it is never accessible from EL0.
4161 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4162 * accessor.
4163 */
4164 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
4165 .access = PL0_R, .accessfn = access_tdra,
4166 .type = ARM_CP_CONST, .resetvalue = 0 },
4167 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4168 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4169 .access = PL1_R, .accessfn = access_tdra,
4170 .type = ARM_CP_CONST, .resetvalue = 0 },
4171 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4172 .access = PL0_R, .accessfn = access_tdra,
4173 .type = ARM_CP_CONST, .resetvalue = 0 },
4174 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
4175 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4176 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4177 .access = PL1_RW, .accessfn = access_tda,
4178 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4179 .resetvalue = 0 },
4180 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4181 * We don't implement the configurable EL0 access.
4182 */
4183 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4184 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4185 .type = ARM_CP_ALIAS,
4186 .access = PL1_R, .accessfn = access_tda,
4187 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
4188 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4189 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
4190 .access = PL1_W, .type = ARM_CP_NO_RAW,
4191 .accessfn = access_tdosa,
4192 .writefn = oslar_write },
4193 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4194 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4195 .access = PL1_R, .resetvalue = 10,
4196 .accessfn = access_tdosa,
4197 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
4198 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4199 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4200 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
4201 .access = PL1_RW, .accessfn = access_tdosa,
4202 .type = ARM_CP_NOP },
4203 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4204 * implement vector catch debug events yet.
4205 */
4206 { .name = "DBGVCR",
4207 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4208 .access = PL1_RW, .accessfn = access_tda,
4209 .type = ARM_CP_NOP },
4210 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4211 * to save and restore a 32-bit guest's DBGVCR)
4212 */
4213 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4214 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4215 .access = PL2_RW, .accessfn = access_tda,
4216 .type = ARM_CP_NOP },
4217 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4218 * Channel but Linux may try to access this register. The 32-bit
4219 * alias is DBGDCCINT.
4220 */
4221 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4222 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4223 .access = PL1_RW, .accessfn = access_tda,
4224 .type = ARM_CP_NOP },
4225 REGINFO_SENTINEL
4226 };
4227
4228 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4229 /* 64 bit access versions of the (dummy) debug registers */
4230 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4231 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4232 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4233 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4234 REGINFO_SENTINEL
4235 };
4236
4237 void hw_watchpoint_update(ARMCPU *cpu, int n)
4238 {
4239 CPUARMState *env = &cpu->env;
4240 vaddr len = 0;
4241 vaddr wvr = env->cp15.dbgwvr[n];
4242 uint64_t wcr = env->cp15.dbgwcr[n];
4243 int mask;
4244 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4245
4246 if (env->cpu_watchpoint[n]) {
4247 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4248 env->cpu_watchpoint[n] = NULL;
4249 }
4250
4251 if (!extract64(wcr, 0, 1)) {
4252 /* E bit clear : watchpoint disabled */
4253 return;
4254 }
4255
4256 switch (extract64(wcr, 3, 2)) {
4257 case 0:
4258 /* LSC 00 is reserved and must behave as if the wp is disabled */
4259 return;
4260 case 1:
4261 flags |= BP_MEM_READ;
4262 break;
4263 case 2:
4264 flags |= BP_MEM_WRITE;
4265 break;
4266 case 3:
4267 flags |= BP_MEM_ACCESS;
4268 break;
4269 }
4270
4271 /* Attempts to use both MASK and BAS fields simultaneously are
4272 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4273 * thus generating a watchpoint for every byte in the masked region.
4274 */
4275 mask = extract64(wcr, 24, 4);
4276 if (mask == 1 || mask == 2) {
4277 /* Reserved values of MASK; we must act as if the mask value was
4278 * some non-reserved value, or as if the watchpoint were disabled.
4279 * We choose the latter.
4280 */
4281 return;
4282 } else if (mask) {
4283 /* Watchpoint covers an aligned area up to 2GB in size */
4284 len = 1ULL << mask;
4285 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4286 * whether the watchpoint fires when the unmasked bits match; we opt
4287 * to generate the exceptions.
4288 */
4289 wvr &= ~(len - 1);
4290 } else {
4291 /* Watchpoint covers bytes defined by the byte address select bits */
4292 int bas = extract64(wcr, 5, 8);
4293 int basstart;
4294
4295 if (bas == 0) {
4296 /* This must act as if the watchpoint is disabled */
4297 return;
4298 }
4299
4300 if (extract64(wvr, 2, 1)) {
4301 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4302 * ignored, and BAS[3:0] define which bytes to watch.
4303 */
4304 bas &= 0xf;
4305 }
4306 /* The BAS bits are supposed to be programmed to indicate a contiguous
4307 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4308 * we fire for each byte in the word/doubleword addressed by the WVR.
4309 * We choose to ignore any non-zero bits after the first range of 1s.
4310 */
4311 basstart = ctz32(bas);
4312 len = cto32(bas >> basstart);
4313 wvr += basstart;
4314 }
4315
4316 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4317 &env->cpu_watchpoint[n]);
4318 }
4319
4320 void hw_watchpoint_update_all(ARMCPU *cpu)
4321 {
4322 int i;
4323 CPUARMState *env = &cpu->env;
4324
4325 /* Completely clear out existing QEMU watchpoints and our array, to
4326 * avoid possible stale entries following migration load.
4327 */
4328 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4329 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4330
4331 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4332 hw_watchpoint_update(cpu, i);
4333 }
4334 }
4335
4336 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4337 uint64_t value)
4338 {
4339 ARMCPU *cpu = arm_env_get_cpu(env);
4340 int i = ri->crm;
4341
4342 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4343 * register reads and behaves as if values written are sign extended.
4344 * Bits [1:0] are RES0.
4345 */
4346 value = sextract64(value, 0, 49) & ~3ULL;
4347
4348 raw_write(env, ri, value);
4349 hw_watchpoint_update(cpu, i);
4350 }
4351
4352 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4353 uint64_t value)
4354 {
4355 ARMCPU *cpu = arm_env_get_cpu(env);
4356 int i = ri->crm;
4357
4358 raw_write(env, ri, value);
4359 hw_watchpoint_update(cpu, i);
4360 }
4361
4362 void hw_breakpoint_update(ARMCPU *cpu, int n)
4363 {
4364 CPUARMState *env = &cpu->env;
4365 uint64_t bvr = env->cp15.dbgbvr[n];
4366 uint64_t bcr = env->cp15.dbgbcr[n];
4367 vaddr addr;
4368 int bt;
4369 int flags = BP_CPU;
4370
4371 if (env->cpu_breakpoint[n]) {
4372 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4373 env->cpu_breakpoint[n] = NULL;
4374 }
4375
4376 if (!extract64(bcr, 0, 1)) {
4377 /* E bit clear : watchpoint disabled */
4378 return;
4379 }
4380
4381 bt = extract64(bcr, 20, 4);
4382
4383 switch (bt) {
4384 case 4: /* unlinked address mismatch (reserved if AArch64) */
4385 case 5: /* linked address mismatch (reserved if AArch64) */
4386 qemu_log_mask(LOG_UNIMP,
4387 "arm: address mismatch breakpoint types not implemented");
4388 return;
4389 case 0: /* unlinked address match */
4390 case 1: /* linked address match */
4391 {
4392 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4393 * we behave as if the register was sign extended. Bits [1:0] are
4394 * RES0. The BAS field is used to allow setting breakpoints on 16
4395 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4396 * a bp will fire if the addresses covered by the bp and the addresses
4397 * covered by the insn overlap but the insn doesn't start at the
4398 * start of the bp address range. We choose to require the insn and
4399 * the bp to have the same address. The constraints on writing to
4400 * BAS enforced in dbgbcr_write mean we have only four cases:
4401 * 0b0000 => no breakpoint
4402 * 0b0011 => breakpoint on addr
4403 * 0b1100 => breakpoint on addr + 2
4404 * 0b1111 => breakpoint on addr
4405 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4406 */
4407 int bas = extract64(bcr, 5, 4);
4408 addr = sextract64(bvr, 0, 49) & ~3ULL;
4409 if (bas == 0) {
4410 return;
4411 }
4412 if (bas == 0xc) {
4413 addr += 2;
4414 }
4415 break;
4416 }
4417 case 2: /* unlinked context ID match */
4418 case 8: /* unlinked VMID match (reserved if no EL2) */
4419 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4420 qemu_log_mask(LOG_UNIMP,
4421 "arm: unlinked context breakpoint types not implemented");
4422 return;
4423 case 9: /* linked VMID match (reserved if no EL2) */
4424 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4425 case 3: /* linked context ID match */
4426 default:
4427 /* We must generate no events for Linked context matches (unless
4428 * they are linked to by some other bp/wp, which is handled in
4429 * updates for the linking bp/wp). We choose to also generate no events
4430 * for reserved values.
4431 */
4432 return;
4433 }
4434
4435 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4436 }
4437
4438 void hw_breakpoint_update_all(ARMCPU *cpu)
4439 {
4440 int i;
4441 CPUARMState *env = &cpu->env;
4442
4443 /* Completely clear out existing QEMU breakpoints and our array, to
4444 * avoid possible stale entries following migration load.
4445 */
4446 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4447 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4448
4449 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4450 hw_breakpoint_update(cpu, i);
4451 }
4452 }
4453
4454 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4455 uint64_t value)
4456 {
4457 ARMCPU *cpu = arm_env_get_cpu(env);
4458 int i = ri->crm;
4459
4460 raw_write(env, ri, value);
4461 hw_breakpoint_update(cpu, i);
4462 }
4463
4464 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4465 uint64_t value)
4466 {
4467 ARMCPU *cpu = arm_env_get_cpu(env);
4468 int i = ri->crm;
4469
4470 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4471 * copy of BAS[0].
4472 */
4473 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4474 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4475
4476 raw_write(env, ri, value);
4477 hw_breakpoint_update(cpu, i);
4478 }
4479
4480 static void define_debug_regs(ARMCPU *cpu)
4481 {
4482 /* Define v7 and v8 architectural debug registers.
4483 * These are just dummy implementations for now.
4484 */
4485 int i;
4486 int wrps, brps, ctx_cmps;
4487 ARMCPRegInfo dbgdidr = {
4488 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
4489 .access = PL0_R, .accessfn = access_tda,
4490 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
4491 };
4492
4493 /* Note that all these register fields hold "number of Xs minus 1". */
4494 brps = extract32(cpu->dbgdidr, 24, 4);
4495 wrps = extract32(cpu->dbgdidr, 28, 4);
4496 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4497
4498 assert(ctx_cmps <= brps);
4499
4500 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4501 * of the debug registers such as number of breakpoints;
4502 * check that if they both exist then they agree.
4503 */
4504 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4505 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4506 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
4507 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
4508 }
4509
4510 define_one_arm_cp_reg(cpu, &dbgdidr);
4511 define_arm_cp_regs(cpu, debug_cp_reginfo);
4512
4513 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4514 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4515 }
4516
4517 for (i = 0; i < brps + 1; i++) {
4518 ARMCPRegInfo dbgregs[] = {
4519 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4520 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
4521 .access = PL1_RW, .accessfn = access_tda,
4522 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4523 .writefn = dbgbvr_write, .raw_writefn = raw_write
4524 },
4525 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4526 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
4527 .access = PL1_RW, .accessfn = access_tda,
4528 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4529 .writefn = dbgbcr_write, .raw_writefn = raw_write
4530 },
4531 REGINFO_SENTINEL
4532 };
4533 define_arm_cp_regs(cpu, dbgregs);
4534 }
4535
4536 for (i = 0; i < wrps + 1; i++) {
4537 ARMCPRegInfo dbgregs[] = {
4538 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4539 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
4540 .access = PL1_RW, .accessfn = access_tda,
4541 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4542 .writefn = dbgwvr_write, .raw_writefn = raw_write
4543 },
4544 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4545 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
4546 .access = PL1_RW, .accessfn = access_tda,
4547 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4548 .writefn = dbgwcr_write, .raw_writefn = raw_write
4549 },
4550 REGINFO_SENTINEL
4551 };
4552 define_arm_cp_regs(cpu, dbgregs);
4553 }
4554 }
4555
4556 /* We don't know until after realize whether there's a GICv3
4557 * attached, and that is what registers the gicv3 sysregs.
4558 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4559 * at runtime.
4560 */
4561 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4562 {
4563 ARMCPU *cpu = arm_env_get_cpu(env);
4564 uint64_t pfr1 = cpu->id_pfr1;
4565
4566 if (env->gicv3state) {
4567 pfr1 |= 1 << 28;
4568 }
4569 return pfr1;
4570 }
4571
4572 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4573 {
4574 ARMCPU *cpu = arm_env_get_cpu(env);
4575 uint64_t pfr0 = cpu->id_aa64pfr0;
4576
4577 if (env->gicv3state) {
4578 pfr0 |= 1 << 24;
4579 }
4580 return pfr0;
4581 }
4582
4583 void register_cp_regs_for_features(ARMCPU *cpu)
4584 {
4585 /* Register all the coprocessor registers based on feature bits */
4586 CPUARMState *env = &cpu->env;
4587 if (arm_feature(env, ARM_FEATURE_M)) {
4588 /* M profile has no coprocessor registers */
4589 return;
4590 }
4591
4592 define_arm_cp_regs(cpu, cp_reginfo);
4593 if (!arm_feature(env, ARM_FEATURE_V8)) {
4594 /* Must go early as it is full of wildcards that may be
4595 * overridden by later definitions.
4596 */
4597 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4598 }
4599
4600 if (arm_feature(env, ARM_FEATURE_V6)) {
4601 /* The ID registers all have impdef reset values */
4602 ARMCPRegInfo v6_idregs[] = {
4603 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4604 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4605 .access = PL1_R, .type = ARM_CP_CONST,
4606 .resetvalue = cpu->id_pfr0 },
4607 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4608 * the value of the GIC field until after we define these regs.
4609 */
4610 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4611 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4612 .access = PL1_R, .type = ARM_CP_NO_RAW,
4613 .readfn = id_pfr1_read,
4614 .writefn = arm_cp_write_ignore },
4615 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4616 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4617 .access = PL1_R, .type = ARM_CP_CONST,
4618 .resetvalue = cpu->id_dfr0 },
4619 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4620 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4621 .access = PL1_R, .type = ARM_CP_CONST,
4622 .resetvalue = cpu->id_afr0 },
4623 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4624 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4625 .access = PL1_R, .type = ARM_CP_CONST,
4626 .resetvalue = cpu->id_mmfr0 },
4627 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4628 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4629 .access = PL1_R, .type = ARM_CP_CONST,
4630 .resetvalue = cpu->id_mmfr1 },
4631 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4632 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4633 .access = PL1_R, .type = ARM_CP_CONST,
4634 .resetvalue = cpu->id_mmfr2 },
4635 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4636 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4637 .access = PL1_R, .type = ARM_CP_CONST,
4638 .resetvalue = cpu->id_mmfr3 },
4639 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4640 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4641 .access = PL1_R, .type = ARM_CP_CONST,
4642 .resetvalue = cpu->id_isar0 },
4643 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4644 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4645 .access = PL1_R, .type = ARM_CP_CONST,
4646 .resetvalue = cpu->id_isar1 },
4647 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4648 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4649 .access = PL1_R, .type = ARM_CP_CONST,
4650 .resetvalue = cpu->id_isar2 },
4651 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4652 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4653 .access = PL1_R, .type = ARM_CP_CONST,
4654 .resetvalue = cpu->id_isar3 },
4655 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4656 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4657 .access = PL1_R, .type = ARM_CP_CONST,
4658 .resetvalue = cpu->id_isar4 },
4659 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4660 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4661 .access = PL1_R, .type = ARM_CP_CONST,
4662 .resetvalue = cpu->id_isar5 },
4663 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4664 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4665 .access = PL1_R, .type = ARM_CP_CONST,
4666 .resetvalue = cpu->id_mmfr4 },
4667 /* 7 is as yet unallocated and must RAZ */
4668 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4669 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4670 .access = PL1_R, .type = ARM_CP_CONST,
4671 .resetvalue = 0 },
4672 REGINFO_SENTINEL
4673 };
4674 define_arm_cp_regs(cpu, v6_idregs);
4675 define_arm_cp_regs(cpu, v6_cp_reginfo);
4676 } else {
4677 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4678 }
4679 if (arm_feature(env, ARM_FEATURE_V6K)) {
4680 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4681 }
4682 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4683 !arm_feature(env, ARM_FEATURE_PMSA)) {
4684 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4685 }
4686 if (arm_feature(env, ARM_FEATURE_V7)) {
4687 /* v7 performance monitor control register: same implementor
4688 * field as main ID register, and we implement only the cycle
4689 * count register.
4690 */
4691 #ifndef CONFIG_USER_ONLY
4692 ARMCPRegInfo pmcr = {
4693 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4694 .access = PL0_RW,
4695 .type = ARM_CP_IO | ARM_CP_ALIAS,
4696 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4697 .accessfn = pmreg_access, .writefn = pmcr_write,
4698 .raw_writefn = raw_write,
4699 };
4700 ARMCPRegInfo pmcr64 = {
4701 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4702 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4703 .access = PL0_RW, .accessfn = pmreg_access,
4704 .type = ARM_CP_IO,
4705 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4706 .resetvalue = cpu->midr & 0xff000000,
4707 .writefn = pmcr_write, .raw_writefn = raw_write,
4708 };
4709 define_one_arm_cp_reg(cpu, &pmcr);
4710 define_one_arm_cp_reg(cpu, &pmcr64);
4711 #endif
4712 ARMCPRegInfo clidr = {
4713 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4714 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4715 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4716 };
4717 define_one_arm_cp_reg(cpu, &clidr);
4718 define_arm_cp_regs(cpu, v7_cp_reginfo);
4719 define_debug_regs(cpu);
4720 } else {
4721 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4722 }
4723 if (arm_feature(env, ARM_FEATURE_V8)) {
4724 /* AArch64 ID registers, which all have impdef reset values.
4725 * Note that within the ID register ranges the unused slots
4726 * must all RAZ, not UNDEF; future architecture versions may
4727 * define new registers here.
4728 */
4729 ARMCPRegInfo v8_idregs[] = {
4730 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4731 * know the right value for the GIC field until after we
4732 * define these regs.
4733 */
4734 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4735 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4736 .access = PL1_R, .type = ARM_CP_NO_RAW,
4737 .readfn = id_aa64pfr0_read,
4738 .writefn = arm_cp_write_ignore },
4739 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4740 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4741 .access = PL1_R, .type = ARM_CP_CONST,
4742 .resetvalue = cpu->id_aa64pfr1},
4743 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4745 .access = PL1_R, .type = ARM_CP_CONST,
4746 .resetvalue = 0 },
4747 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4748 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4749 .access = PL1_R, .type = ARM_CP_CONST,
4750 .resetvalue = 0 },
4751 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4752 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4753 .access = PL1_R, .type = ARM_CP_CONST,
4754 .resetvalue = 0 },
4755 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4756 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4757 .access = PL1_R, .type = ARM_CP_CONST,
4758 .resetvalue = 0 },
4759 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4760 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4761 .access = PL1_R, .type = ARM_CP_CONST,
4762 .resetvalue = 0 },
4763 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4765 .access = PL1_R, .type = ARM_CP_CONST,
4766 .resetvalue = 0 },
4767 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4769 .access = PL1_R, .type = ARM_CP_CONST,
4770 .resetvalue = cpu->id_aa64dfr0 },
4771 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4773 .access = PL1_R, .type = ARM_CP_CONST,
4774 .resetvalue = cpu->id_aa64dfr1 },
4775 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4777 .access = PL1_R, .type = ARM_CP_CONST,
4778 .resetvalue = 0 },
4779 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4780 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4781 .access = PL1_R, .type = ARM_CP_CONST,
4782 .resetvalue = 0 },
4783 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4784 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4785 .access = PL1_R, .type = ARM_CP_CONST,
4786 .resetvalue = cpu->id_aa64afr0 },
4787 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4789 .access = PL1_R, .type = ARM_CP_CONST,
4790 .resetvalue = cpu->id_aa64afr1 },
4791 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4793 .access = PL1_R, .type = ARM_CP_CONST,
4794 .resetvalue = 0 },
4795 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4797 .access = PL1_R, .type = ARM_CP_CONST,
4798 .resetvalue = 0 },
4799 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4801 .access = PL1_R, .type = ARM_CP_CONST,
4802 .resetvalue = cpu->id_aa64isar0 },
4803 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4805 .access = PL1_R, .type = ARM_CP_CONST,
4806 .resetvalue = cpu->id_aa64isar1 },
4807 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4809 .access = PL1_R, .type = ARM_CP_CONST,
4810 .resetvalue = 0 },
4811 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4812 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4813 .access = PL1_R, .type = ARM_CP_CONST,
4814 .resetvalue = 0 },
4815 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4816 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4817 .access = PL1_R, .type = ARM_CP_CONST,
4818 .resetvalue = 0 },
4819 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4820 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4821 .access = PL1_R, .type = ARM_CP_CONST,
4822 .resetvalue = 0 },
4823 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4824 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4825 .access = PL1_R, .type = ARM_CP_CONST,
4826 .resetvalue = 0 },
4827 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4829 .access = PL1_R, .type = ARM_CP_CONST,
4830 .resetvalue = 0 },
4831 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4833 .access = PL1_R, .type = ARM_CP_CONST,
4834 .resetvalue = cpu->id_aa64mmfr0 },
4835 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4836 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4837 .access = PL1_R, .type = ARM_CP_CONST,
4838 .resetvalue = cpu->id_aa64mmfr1 },
4839 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4840 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4841 .access = PL1_R, .type = ARM_CP_CONST,
4842 .resetvalue = 0 },
4843 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4844 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4845 .access = PL1_R, .type = ARM_CP_CONST,
4846 .resetvalue = 0 },
4847 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4849 .access = PL1_R, .type = ARM_CP_CONST,
4850 .resetvalue = 0 },
4851 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4853 .access = PL1_R, .type = ARM_CP_CONST,
4854 .resetvalue = 0 },
4855 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4856 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4857 .access = PL1_R, .type = ARM_CP_CONST,
4858 .resetvalue = 0 },
4859 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4861 .access = PL1_R, .type = ARM_CP_CONST,
4862 .resetvalue = 0 },
4863 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4864 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4865 .access = PL1_R, .type = ARM_CP_CONST,
4866 .resetvalue = cpu->mvfr0 },
4867 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4868 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4869 .access = PL1_R, .type = ARM_CP_CONST,
4870 .resetvalue = cpu->mvfr1 },
4871 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4872 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4873 .access = PL1_R, .type = ARM_CP_CONST,
4874 .resetvalue = cpu->mvfr2 },
4875 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4876 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4877 .access = PL1_R, .type = ARM_CP_CONST,
4878 .resetvalue = 0 },
4879 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4880 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4881 .access = PL1_R, .type = ARM_CP_CONST,
4882 .resetvalue = 0 },
4883 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4885 .access = PL1_R, .type = ARM_CP_CONST,
4886 .resetvalue = 0 },
4887 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4889 .access = PL1_R, .type = ARM_CP_CONST,
4890 .resetvalue = 0 },
4891 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4892 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4893 .access = PL1_R, .type = ARM_CP_CONST,
4894 .resetvalue = 0 },
4895 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4896 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4897 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4898 .resetvalue = cpu->pmceid0 },
4899 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4900 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4901 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4902 .resetvalue = cpu->pmceid0 },
4903 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4904 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4905 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4906 .resetvalue = cpu->pmceid1 },
4907 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4909 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4910 .resetvalue = cpu->pmceid1 },
4911 REGINFO_SENTINEL
4912 };
4913 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4914 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4915 !arm_feature(env, ARM_FEATURE_EL2)) {
4916 ARMCPRegInfo rvbar = {
4917 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4918 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4919 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4920 };
4921 define_one_arm_cp_reg(cpu, &rvbar);
4922 }
4923 define_arm_cp_regs(cpu, v8_idregs);
4924 define_arm_cp_regs(cpu, v8_cp_reginfo);
4925 }
4926 if (arm_feature(env, ARM_FEATURE_EL2)) {
4927 uint64_t vmpidr_def = mpidr_read_val(env);
4928 ARMCPRegInfo vpidr_regs[] = {
4929 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4930 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4931 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4932 .resetvalue = cpu->midr,
4933 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4934 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4935 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4936 .access = PL2_RW, .resetvalue = cpu->midr,
4937 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4938 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4939 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4940 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4941 .resetvalue = vmpidr_def,
4942 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4943 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4944 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4945 .access = PL2_RW,
4946 .resetvalue = vmpidr_def,
4947 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4948 REGINFO_SENTINEL
4949 };
4950 define_arm_cp_regs(cpu, vpidr_regs);
4951 define_arm_cp_regs(cpu, el2_cp_reginfo);
4952 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4953 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4954 ARMCPRegInfo rvbar = {
4955 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4956 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4957 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4958 };
4959 define_one_arm_cp_reg(cpu, &rvbar);
4960 }
4961 } else {
4962 /* If EL2 is missing but higher ELs are enabled, we need to
4963 * register the no_el2 reginfos.
4964 */
4965 if (arm_feature(env, ARM_FEATURE_EL3)) {
4966 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4967 * of MIDR_EL1 and MPIDR_EL1.
4968 */
4969 ARMCPRegInfo vpidr_regs[] = {
4970 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4971 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4972 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4973 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4974 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4975 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4976 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4977 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4978 .type = ARM_CP_NO_RAW,
4979 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
4980 REGINFO_SENTINEL
4981 };
4982 define_arm_cp_regs(cpu, vpidr_regs);
4983 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
4984 }
4985 }
4986 if (arm_feature(env, ARM_FEATURE_EL3)) {
4987 define_arm_cp_regs(cpu, el3_cp_reginfo);
4988 ARMCPRegInfo el3_regs[] = {
4989 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4990 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4991 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
4992 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
4993 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
4994 .access = PL3_RW,
4995 .raw_writefn = raw_write, .writefn = sctlr_write,
4996 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
4997 .resetvalue = cpu->reset_sctlr },
4998 REGINFO_SENTINEL
4999 };
5000
5001 define_arm_cp_regs(cpu, el3_regs);
5002 }
5003 /* The behaviour of NSACR is sufficiently various that we don't
5004 * try to describe it in a single reginfo:
5005 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5006 * reads as constant 0xc00 from NS EL1 and NS EL2
5007 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5008 * if v7 without EL3, register doesn't exist
5009 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5010 */
5011 if (arm_feature(env, ARM_FEATURE_EL3)) {
5012 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5013 ARMCPRegInfo nsacr = {
5014 .name = "NSACR", .type = ARM_CP_CONST,
5015 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5016 .access = PL1_RW, .accessfn = nsacr_access,
5017 .resetvalue = 0xc00
5018 };
5019 define_one_arm_cp_reg(cpu, &nsacr);
5020 } else {
5021 ARMCPRegInfo nsacr = {
5022 .name = "NSACR",
5023 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5024 .access = PL3_RW | PL1_R,
5025 .resetvalue = 0,
5026 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5027 };
5028 define_one_arm_cp_reg(cpu, &nsacr);
5029 }
5030 } else {
5031 if (arm_feature(env, ARM_FEATURE_V8)) {
5032 ARMCPRegInfo nsacr = {
5033 .name = "NSACR", .type = ARM_CP_CONST,
5034 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5035 .access = PL1_R,
5036 .resetvalue = 0xc00
5037 };
5038 define_one_arm_cp_reg(cpu, &nsacr);
5039 }
5040 }
5041
5042 if (arm_feature(env, ARM_FEATURE_PMSA)) {
5043 if (arm_feature(env, ARM_FEATURE_V6)) {
5044 /* PMSAv6 not implemented */
5045 assert(arm_feature(env, ARM_FEATURE_V7));
5046 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5047 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5048 } else {
5049 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5050 }
5051 } else {
5052 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5053 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5054 }
5055 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5056 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5057 }
5058 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5059 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5060 }
5061 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5062 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5063 }
5064 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5065 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5066 }
5067 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5068 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5069 }
5070 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5071 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5072 }
5073 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5074 define_arm_cp_regs(cpu, omap_cp_reginfo);
5075 }
5076 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5077 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5078 }
5079 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5080 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5081 }
5082 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5083 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5084 }
5085 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5086 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5087 }
5088 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5089 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5090 * be read-only (ie write causes UNDEF exception).
5091 */
5092 {
5093 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5094 /* Pre-v8 MIDR space.
5095 * Note that the MIDR isn't a simple constant register because
5096 * of the TI925 behaviour where writes to another register can
5097 * cause the MIDR value to change.
5098 *
5099 * Unimplemented registers in the c15 0 0 0 space default to
5100 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5101 * and friends override accordingly.
5102 */
5103 { .name = "MIDR",
5104 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
5105 .access = PL1_R, .resetvalue = cpu->midr,
5106 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
5107 .readfn = midr_read,
5108 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5109 .type = ARM_CP_OVERRIDE },
5110 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5111 { .name = "DUMMY",
5112 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5113 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5114 { .name = "DUMMY",
5115 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5116 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5117 { .name = "DUMMY",
5118 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5119 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5120 { .name = "DUMMY",
5121 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5122 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5123 { .name = "DUMMY",
5124 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5125 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5126 REGINFO_SENTINEL
5127 };
5128 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
5129 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5130 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
5131 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5132 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5133 .readfn = midr_read },
5134 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5135 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5136 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5137 .access = PL1_R, .resetvalue = cpu->midr },
5138 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5139 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5140 .access = PL1_R, .resetvalue = cpu->midr },
5141 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5142 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
5143 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
5144 REGINFO_SENTINEL
5145 };
5146 ARMCPRegInfo id_cp_reginfo[] = {
5147 /* These are common to v8 and pre-v8 */
5148 { .name = "CTR",
5149 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5150 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5151 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5152 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5153 .access = PL0_R, .accessfn = ctr_el0_access,
5154 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5155 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5156 { .name = "TCMTR",
5157 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5158 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5159 REGINFO_SENTINEL
5160 };
5161 /* TLBTR is specific to VMSA */
5162 ARMCPRegInfo id_tlbtr_reginfo = {
5163 .name = "TLBTR",
5164 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5165 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5166 };
5167 /* MPUIR is specific to PMSA V6+ */
5168 ARMCPRegInfo id_mpuir_reginfo = {
5169 .name = "MPUIR",
5170 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5171 .access = PL1_R, .type = ARM_CP_CONST,
5172 .resetvalue = cpu->pmsav7_dregion << 8
5173 };
5174 ARMCPRegInfo crn0_wi_reginfo = {
5175 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5176 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5177 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5178 };
5179 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5180 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5181 ARMCPRegInfo *r;
5182 /* Register the blanket "writes ignored" value first to cover the
5183 * whole space. Then update the specific ID registers to allow write
5184 * access, so that they ignore writes rather than causing them to
5185 * UNDEF.
5186 */
5187 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5188 for (r = id_pre_v8_midr_cp_reginfo;
5189 r->type != ARM_CP_SENTINEL; r++) {
5190 r->access = PL1_RW;
5191 }
5192 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5193 r->access = PL1_RW;
5194 }
5195 id_tlbtr_reginfo.access = PL1_RW;
5196 id_tlbtr_reginfo.access = PL1_RW;
5197 }
5198 if (arm_feature(env, ARM_FEATURE_V8)) {
5199 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5200 } else {
5201 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5202 }
5203 define_arm_cp_regs(cpu, id_cp_reginfo);
5204 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
5205 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
5206 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5207 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
5208 }
5209 }
5210
5211 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5212 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5213 }
5214
5215 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
5216 ARMCPRegInfo auxcr_reginfo[] = {
5217 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5218 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5219 .access = PL1_RW, .type = ARM_CP_CONST,
5220 .resetvalue = cpu->reset_auxcr },
5221 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5222 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5223 .access = PL2_RW, .type = ARM_CP_CONST,
5224 .resetvalue = 0 },
5225 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5226 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5227 .access = PL3_RW, .type = ARM_CP_CONST,
5228 .resetvalue = 0 },
5229 REGINFO_SENTINEL
5230 };
5231 define_arm_cp_regs(cpu, auxcr_reginfo);
5232 }
5233
5234 if (arm_feature(env, ARM_FEATURE_CBAR)) {
5235 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5236 /* 32 bit view is [31:18] 0...0 [43:32]. */
5237 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5238 | extract64(cpu->reset_cbar, 32, 12);
5239 ARMCPRegInfo cbar_reginfo[] = {
5240 { .name = "CBAR",
5241 .type = ARM_CP_CONST,
5242 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5243 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5244 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5245 .type = ARM_CP_CONST,
5246 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5247 .access = PL1_R, .resetvalue = cbar32 },
5248 REGINFO_SENTINEL
5249 };
5250 /* We don't implement a r/w 64 bit CBAR currently */
5251 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5252 define_arm_cp_regs(cpu, cbar_reginfo);
5253 } else {
5254 ARMCPRegInfo cbar = {
5255 .name = "CBAR",
5256 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5257 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5258 .fieldoffset = offsetof(CPUARMState,
5259 cp15.c15_config_base_address)
5260 };
5261 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5262 cbar.access = PL1_R;
5263 cbar.fieldoffset = 0;
5264 cbar.type = ARM_CP_CONST;
5265 }
5266 define_one_arm_cp_reg(cpu, &cbar);
5267 }
5268 }
5269
5270 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5271 ARMCPRegInfo vbar_cp_reginfo[] = {
5272 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5273 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5274 .access = PL1_RW, .writefn = vbar_write,
5275 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5276 offsetof(CPUARMState, cp15.vbar_ns) },
5277 .resetvalue = 0 },
5278 REGINFO_SENTINEL
5279 };
5280 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5281 }
5282
5283 /* Generic registers whose values depend on the implementation */
5284 {
5285 ARMCPRegInfo sctlr = {
5286 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
5287 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5288 .access = PL1_RW,
5289 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5290 offsetof(CPUARMState, cp15.sctlr_ns) },
5291 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5292 .raw_writefn = raw_write,
5293 };
5294 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5295 /* Normally we would always end the TB on an SCTLR write, but Linux
5296 * arch/arm/mach-pxa/sleep.S expects two instructions following
5297 * an MMU enable to execute from cache. Imitate this behaviour.
5298 */
5299 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5300 }
5301 define_one_arm_cp_reg(cpu, &sctlr);
5302 }
5303 }
5304
5305 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5306 {
5307 CPUState *cs = CPU(cpu);
5308 CPUARMState *env = &cpu->env;
5309
5310 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5311 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5312 aarch64_fpu_gdb_set_reg,
5313 34, "aarch64-fpu.xml", 0);
5314 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
5315 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5316 51, "arm-neon.xml", 0);
5317 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
5318 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5319 35, "arm-vfp3.xml", 0);
5320 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
5321 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5322 19, "arm-vfp.xml", 0);
5323 }
5324 }
5325
5326 /* Sort alphabetically by type name, except for "any". */
5327 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5328 {
5329 ObjectClass *class_a = (ObjectClass *)a;
5330 ObjectClass *class_b = (ObjectClass *)b;
5331 const char *name_a, *name_b;
5332
5333 name_a = object_class_get_name(class_a);
5334 name_b = object_class_get_name(class_b);
5335 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
5336 return 1;
5337 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
5338 return -1;
5339 } else {
5340 return strcmp(name_a, name_b);
5341 }
5342 }
5343
5344 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
5345 {
5346 ObjectClass *oc = data;
5347 CPUListState *s = user_data;
5348 const char *typename;
5349 char *name;
5350
5351 typename = object_class_get_name(oc);
5352 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
5353 (*s->cpu_fprintf)(s->file, " %s\n",
5354 name);
5355 g_free(name);
5356 }
5357
5358 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5359 {
5360 CPUListState s = {
5361 .file = f,
5362 .cpu_fprintf = cpu_fprintf,
5363 };
5364 GSList *list;
5365
5366 list = object_class_get_list(TYPE_ARM_CPU, false);
5367 list = g_slist_sort(list, arm_cpu_list_compare);
5368 (*cpu_fprintf)(f, "Available CPUs:\n");
5369 g_slist_foreach(list, arm_cpu_list_entry, &s);
5370 g_slist_free(list);
5371 #ifdef CONFIG_KVM
5372 /* The 'host' CPU type is dynamically registered only if KVM is
5373 * enabled, so we have to special-case it here:
5374 */
5375 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5376 #endif
5377 }
5378
5379 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5380 {
5381 ObjectClass *oc = data;
5382 CpuDefinitionInfoList **cpu_list = user_data;
5383 CpuDefinitionInfoList *entry;
5384 CpuDefinitionInfo *info;
5385 const char *typename;
5386
5387 typename = object_class_get_name(oc);
5388 info = g_malloc0(sizeof(*info));
5389 info->name = g_strndup(typename,
5390 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5391 info->q_typename = g_strdup(typename);
5392
5393 entry = g_malloc0(sizeof(*entry));
5394 entry->value = info;
5395 entry->next = *cpu_list;
5396 *cpu_list = entry;
5397 }
5398
5399 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5400 {
5401 CpuDefinitionInfoList *cpu_list = NULL;
5402 GSList *list;
5403
5404 list = object_class_get_list(TYPE_ARM_CPU, false);
5405 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5406 g_slist_free(list);
5407
5408 return cpu_list;
5409 }
5410
5411 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
5412 void *opaque, int state, int secstate,
5413 int crm, int opc1, int opc2)
5414 {
5415 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5416 * add a single reginfo struct to the hash table.
5417 */
5418 uint32_t *key = g_new(uint32_t, 1);
5419 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5420 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
5421 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5422
5423 /* Reset the secure state to the specific incoming state. This is
5424 * necessary as the register may have been defined with both states.
5425 */
5426 r2->secure = secstate;
5427
5428 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5429 /* Register is banked (using both entries in array).
5430 * Overwriting fieldoffset as the array is only used to define
5431 * banked registers but later only fieldoffset is used.
5432 */
5433 r2->fieldoffset = r->bank_fieldoffsets[ns];
5434 }
5435
5436 if (state == ARM_CP_STATE_AA32) {
5437 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5438 /* If the register is banked then we don't need to migrate or
5439 * reset the 32-bit instance in certain cases:
5440 *
5441 * 1) If the register has both 32-bit and 64-bit instances then we
5442 * can count on the 64-bit instance taking care of the
5443 * non-secure bank.
5444 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5445 * taking care of the secure bank. This requires that separate
5446 * 32 and 64-bit definitions are provided.
5447 */
5448 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5449 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
5450 r2->type |= ARM_CP_ALIAS;
5451 }
5452 } else if ((secstate != r->secure) && !ns) {
5453 /* The register is not banked so we only want to allow migration of
5454 * the non-secure instance.
5455 */
5456 r2->type |= ARM_CP_ALIAS;
5457 }
5458
5459 if (r->state == ARM_CP_STATE_BOTH) {
5460 /* We assume it is a cp15 register if the .cp field is left unset.
5461 */
5462 if (r2->cp == 0) {
5463 r2->cp = 15;
5464 }
5465
5466 #ifdef HOST_WORDS_BIGENDIAN
5467 if (r2->fieldoffset) {
5468 r2->fieldoffset += sizeof(uint32_t);
5469 }
5470 #endif
5471 }
5472 }
5473 if (state == ARM_CP_STATE_AA64) {
5474 /* To allow abbreviation of ARMCPRegInfo
5475 * definitions, we treat cp == 0 as equivalent to
5476 * the value for "standard guest-visible sysreg".
5477 * STATE_BOTH definitions are also always "standard
5478 * sysreg" in their AArch64 view (the .cp value may
5479 * be non-zero for the benefit of the AArch32 view).
5480 */
5481 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
5482 r2->cp = CP_REG_ARM64_SYSREG_CP;
5483 }
5484 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5485 r2->opc0, opc1, opc2);
5486 } else {
5487 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
5488 }
5489 if (opaque) {
5490 r2->opaque = opaque;
5491 }
5492 /* reginfo passed to helpers is correct for the actual access,
5493 * and is never ARM_CP_STATE_BOTH:
5494 */
5495 r2->state = state;
5496 /* Make sure reginfo passed to helpers for wildcarded regs
5497 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5498 */
5499 r2->crm = crm;
5500 r2->opc1 = opc1;
5501 r2->opc2 = opc2;
5502 /* By convention, for wildcarded registers only the first
5503 * entry is used for migration; the others are marked as
5504 * ALIAS so we don't try to transfer the register
5505 * multiple times. Special registers (ie NOP/WFI) are
5506 * never migratable and not even raw-accessible.
5507 */
5508 if ((r->type & ARM_CP_SPECIAL)) {
5509 r2->type |= ARM_CP_NO_RAW;
5510 }
5511 if (((r->crm == CP_ANY) && crm != 0) ||
5512 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5513 ((r->opc2 == CP_ANY) && opc2 != 0)) {
5514 r2->type |= ARM_CP_ALIAS;
5515 }
5516
5517 /* Check that raw accesses are either forbidden or handled. Note that
5518 * we can't assert this earlier because the setup of fieldoffset for
5519 * banked registers has to be done first.
5520 */
5521 if (!(r2->type & ARM_CP_NO_RAW)) {
5522 assert(!raw_accessors_invalid(r2));
5523 }
5524
5525 /* Overriding of an existing definition must be explicitly
5526 * requested.
5527 */
5528 if (!(r->type & ARM_CP_OVERRIDE)) {
5529 ARMCPRegInfo *oldreg;
5530 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5531 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5532 fprintf(stderr, "Register redefined: cp=%d %d bit "
5533 "crn=%d crm=%d opc1=%d opc2=%d, "
5534 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5535 r2->crn, r2->crm, r2->opc1, r2->opc2,
5536 oldreg->name, r2->name);
5537 g_assert_not_reached();
5538 }
5539 }
5540 g_hash_table_insert(cpu->cp_regs, key, r2);
5541 }
5542
5543
5544 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5545 const ARMCPRegInfo *r, void *opaque)
5546 {
5547 /* Define implementations of coprocessor registers.
5548 * We store these in a hashtable because typically
5549 * there are less than 150 registers in a space which
5550 * is 16*16*16*8*8 = 262144 in size.
5551 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5552 * If a register is defined twice then the second definition is
5553 * used, so this can be used to define some generic registers and
5554 * then override them with implementation specific variations.
5555 * At least one of the original and the second definition should
5556 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5557 * against accidental use.
5558 *
5559 * The state field defines whether the register is to be
5560 * visible in the AArch32 or AArch64 execution state. If the
5561 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5562 * reginfo structure for the AArch32 view, which sees the lower
5563 * 32 bits of the 64 bit register.
5564 *
5565 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5566 * be wildcarded. AArch64 registers are always considered to be 64
5567 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5568 * the register, if any.
5569 */
5570 int crm, opc1, opc2, state;
5571 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5572 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5573 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5574 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5575 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5576 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5577 /* 64 bit registers have only CRm and Opc1 fields */
5578 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
5579 /* op0 only exists in the AArch64 encodings */
5580 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5581 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5582 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5583 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5584 * encodes a minimum access level for the register. We roll this
5585 * runtime check into our general permission check code, so check
5586 * here that the reginfo's specified permissions are strict enough
5587 * to encompass the generic architectural permission check.
5588 */
5589 if (r->state != ARM_CP_STATE_AA32) {
5590 int mask = 0;
5591 switch (r->opc1) {
5592 case 0: case 1: case 2:
5593 /* min_EL EL1 */
5594 mask = PL1_RW;
5595 break;
5596 case 3:
5597 /* min_EL EL0 */
5598 mask = PL0_RW;
5599 break;
5600 case 4:
5601 /* min_EL EL2 */
5602 mask = PL2_RW;
5603 break;
5604 case 5:
5605 /* unallocated encoding, so not possible */
5606 assert(false);
5607 break;
5608 case 6:
5609 /* min_EL EL3 */
5610 mask = PL3_RW;
5611 break;
5612 case 7:
5613 /* min_EL EL1, secure mode only (we don't check the latter) */
5614 mask = PL1_RW;
5615 break;
5616 default:
5617 /* broken reginfo with out-of-range opc1 */
5618 assert(false);
5619 break;
5620 }
5621 /* assert our permissions are not too lax (stricter is fine) */
5622 assert((r->access & ~mask) == 0);
5623 }
5624
5625 /* Check that the register definition has enough info to handle
5626 * reads and writes if they are permitted.
5627 */
5628 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5629 if (r->access & PL3_R) {
5630 assert((r->fieldoffset ||
5631 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5632 r->readfn);
5633 }
5634 if (r->access & PL3_W) {
5635 assert((r->fieldoffset ||
5636 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5637 r->writefn);
5638 }
5639 }
5640 /* Bad type field probably means missing sentinel at end of reg list */
5641 assert(cptype_valid(r->type));
5642 for (crm = crmmin; crm <= crmmax; crm++) {
5643 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5644 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
5645 for (state = ARM_CP_STATE_AA32;
5646 state <= ARM_CP_STATE_AA64; state++) {
5647 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5648 continue;
5649 }
5650 if (state == ARM_CP_STATE_AA32) {
5651 /* Under AArch32 CP registers can be common
5652 * (same for secure and non-secure world) or banked.
5653 */
5654 switch (r->secure) {
5655 case ARM_CP_SECSTATE_S:
5656 case ARM_CP_SECSTATE_NS:
5657 add_cpreg_to_hashtable(cpu, r, opaque, state,
5658 r->secure, crm, opc1, opc2);
5659 break;
5660 default:
5661 add_cpreg_to_hashtable(cpu, r, opaque, state,
5662 ARM_CP_SECSTATE_S,
5663 crm, opc1, opc2);
5664 add_cpreg_to_hashtable(cpu, r, opaque, state,
5665 ARM_CP_SECSTATE_NS,
5666 crm, opc1, opc2);
5667 break;
5668 }
5669 } else {
5670 /* AArch64 registers get mapped to non-secure instance
5671 * of AArch32 */
5672 add_cpreg_to_hashtable(cpu, r, opaque, state,
5673 ARM_CP_SECSTATE_NS,
5674 crm, opc1, opc2);
5675 }
5676 }
5677 }
5678 }
5679 }
5680 }
5681
5682 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5683 const ARMCPRegInfo *regs, void *opaque)
5684 {
5685 /* Define a whole list of registers */
5686 const ARMCPRegInfo *r;
5687 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5688 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5689 }
5690 }
5691
5692 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
5693 {
5694 return g_hash_table_lookup(cpregs, &encoded_cp);
5695 }
5696
5697 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5698 uint64_t value)
5699 {
5700 /* Helper coprocessor write function for write-ignore registers */
5701 }
5702
5703 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
5704 {
5705 /* Helper coprocessor write function for read-as-zero registers */
5706 return 0;
5707 }
5708
5709 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5710 {
5711 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5712 }
5713
5714 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
5715 {
5716 /* Return true if it is not valid for us to switch to
5717 * this CPU mode (ie all the UNPREDICTABLE cases in
5718 * the ARM ARM CPSRWriteByInstr pseudocode).
5719 */
5720
5721 /* Changes to or from Hyp via MSR and CPS are illegal. */
5722 if (write_type == CPSRWriteByInstr &&
5723 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5724 mode == ARM_CPU_MODE_HYP)) {
5725 return 1;
5726 }
5727
5728 switch (mode) {
5729 case ARM_CPU_MODE_USR:
5730 return 0;
5731 case ARM_CPU_MODE_SYS:
5732 case ARM_CPU_MODE_SVC:
5733 case ARM_CPU_MODE_ABT:
5734 case ARM_CPU_MODE_UND:
5735 case ARM_CPU_MODE_IRQ:
5736 case ARM_CPU_MODE_FIQ:
5737 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5738 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5739 */
5740 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5741 * and CPS are treated as illegal mode changes.
5742 */
5743 if (write_type == CPSRWriteByInstr &&
5744 (env->cp15.hcr_el2 & HCR_TGE) &&
5745 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5746 !arm_is_secure_below_el3(env)) {
5747 return 1;
5748 }
5749 return 0;
5750 case ARM_CPU_MODE_HYP:
5751 return !arm_feature(env, ARM_FEATURE_EL2)
5752 || arm_current_el(env) < 2 || arm_is_secure(env);
5753 case ARM_CPU_MODE_MON:
5754 return arm_current_el(env) < 3;
5755 default:
5756 return 1;
5757 }
5758 }
5759
5760 uint32_t cpsr_read(CPUARMState *env)
5761 {
5762 int ZF;
5763 ZF = (env->ZF == 0);
5764 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
5765 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5766 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5767 | ((env->condexec_bits & 0xfc) << 8)
5768 | (env->GE << 16) | (env->daif & CPSR_AIF);
5769 }
5770
5771 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5772 CPSRWriteType write_type)
5773 {
5774 uint32_t changed_daif;
5775
5776 if (mask & CPSR_NZCV) {
5777 env->ZF = (~val) & CPSR_Z;
5778 env->NF = val;
5779 env->CF = (val >> 29) & 1;
5780 env->VF = (val << 3) & 0x80000000;
5781 }
5782 if (mask & CPSR_Q)
5783 env->QF = ((val & CPSR_Q) != 0);
5784 if (mask & CPSR_T)
5785 env->thumb = ((val & CPSR_T) != 0);
5786 if (mask & CPSR_IT_0_1) {
5787 env->condexec_bits &= ~3;
5788 env->condexec_bits |= (val >> 25) & 3;
5789 }
5790 if (mask & CPSR_IT_2_7) {
5791 env->condexec_bits &= 3;
5792 env->condexec_bits |= (val >> 8) & 0xfc;
5793 }
5794 if (mask & CPSR_GE) {
5795 env->GE = (val >> 16) & 0xf;
5796 }
5797
5798 /* In a V7 implementation that includes the security extensions but does
5799 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5800 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5801 * bits respectively.
5802 *
5803 * In a V8 implementation, it is permitted for privileged software to
5804 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5805 */
5806 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
5807 arm_feature(env, ARM_FEATURE_EL3) &&
5808 !arm_feature(env, ARM_FEATURE_EL2) &&
5809 !arm_is_secure(env)) {
5810
5811 changed_daif = (env->daif ^ val) & mask;
5812
5813 if (changed_daif & CPSR_A) {
5814 /* Check to see if we are allowed to change the masking of async
5815 * abort exceptions from a non-secure state.
5816 */
5817 if (!(env->cp15.scr_el3 & SCR_AW)) {
5818 qemu_log_mask(LOG_GUEST_ERROR,
5819 "Ignoring attempt to switch CPSR_A flag from "
5820 "non-secure world with SCR.AW bit clear\n");
5821 mask &= ~CPSR_A;
5822 }
5823 }
5824
5825 if (changed_daif & CPSR_F) {
5826 /* Check to see if we are allowed to change the masking of FIQ
5827 * exceptions from a non-secure state.
5828 */
5829 if (!(env->cp15.scr_el3 & SCR_FW)) {
5830 qemu_log_mask(LOG_GUEST_ERROR,
5831 "Ignoring attempt to switch CPSR_F flag from "
5832 "non-secure world with SCR.FW bit clear\n");
5833 mask &= ~CPSR_F;
5834 }
5835
5836 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5837 * If this bit is set software is not allowed to mask
5838 * FIQs, but is allowed to set CPSR_F to 0.
5839 */
5840 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5841 (val & CPSR_F)) {
5842 qemu_log_mask(LOG_GUEST_ERROR,
5843 "Ignoring attempt to enable CPSR_F flag "
5844 "(non-maskable FIQ [NMFI] support enabled)\n");
5845 mask &= ~CPSR_F;
5846 }
5847 }
5848 }
5849
5850 env->daif &= ~(CPSR_AIF & mask);
5851 env->daif |= val & CPSR_AIF & mask;
5852
5853 if (write_type != CPSRWriteRaw &&
5854 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
5855 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5856 /* Note that we can only get here in USR mode if this is a
5857 * gdb stub write; for this case we follow the architectural
5858 * behaviour for guest writes in USR mode of ignoring an attempt
5859 * to switch mode. (Those are caught by translate.c for writes
5860 * triggered by guest instructions.)
5861 */
5862 mask &= ~CPSR_M;
5863 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
5864 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5865 * v7, and has defined behaviour in v8:
5866 * + leave CPSR.M untouched
5867 * + allow changes to the other CPSR fields
5868 * + set PSTATE.IL
5869 * For user changes via the GDB stub, we don't set PSTATE.IL,
5870 * as this would be unnecessarily harsh for a user error.
5871 */
5872 mask &= ~CPSR_M;
5873 if (write_type != CPSRWriteByGDBStub &&
5874 arm_feature(env, ARM_FEATURE_V8)) {
5875 mask |= CPSR_IL;
5876 val |= CPSR_IL;
5877 }
5878 } else {
5879 switch_mode(env, val & CPSR_M);
5880 }
5881 }
5882 mask &= ~CACHED_CPSR_BITS;
5883 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5884 }
5885
5886 /* Sign/zero extend */
5887 uint32_t HELPER(sxtb16)(uint32_t x)
5888 {
5889 uint32_t res;
5890 res = (uint16_t)(int8_t)x;
5891 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5892 return res;
5893 }
5894
5895 uint32_t HELPER(uxtb16)(uint32_t x)
5896 {
5897 uint32_t res;
5898 res = (uint16_t)(uint8_t)x;
5899 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5900 return res;
5901 }
5902
5903 int32_t HELPER(sdiv)(int32_t num, int32_t den)
5904 {
5905 if (den == 0)
5906 return 0;
5907 if (num == INT_MIN && den == -1)
5908 return INT_MIN;
5909 return num / den;
5910 }
5911
5912 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5913 {
5914 if (den == 0)
5915 return 0;
5916 return num / den;
5917 }
5918
5919 uint32_t HELPER(rbit)(uint32_t x)
5920 {
5921 return revbit32(x);
5922 }
5923
5924 #if defined(CONFIG_USER_ONLY)
5925
5926 /* These should probably raise undefined insn exceptions. */
5927 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
5928 {
5929 ARMCPU *cpu = arm_env_get_cpu(env);
5930
5931 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
5932 }
5933
5934 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
5935 {
5936 ARMCPU *cpu = arm_env_get_cpu(env);
5937
5938 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
5939 return 0;
5940 }
5941
5942 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
5943 {
5944 /* translate.c should never generate calls here in user-only mode */
5945 g_assert_not_reached();
5946 }
5947
5948 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
5949 {
5950 /* translate.c should never generate calls here in user-only mode */
5951 g_assert_not_reached();
5952 }
5953
5954 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
5955 {
5956 /* The TT instructions can be used by unprivileged code, but in
5957 * user-only emulation we don't have the MPU.
5958 * Luckily since we know we are NonSecure unprivileged (and that in
5959 * turn means that the A flag wasn't specified), all the bits in the
5960 * register must be zero:
5961 * IREGION: 0 because IRVALID is 0
5962 * IRVALID: 0 because NS
5963 * S: 0 because NS
5964 * NSRW: 0 because NS
5965 * NSR: 0 because NS
5966 * RW: 0 because unpriv and A flag not set
5967 * R: 0 because unpriv and A flag not set
5968 * SRVALID: 0 because NS
5969 * MRVALID: 0 because unpriv and A flag not set
5970 * SREGION: 0 becaus SRVALID is 0
5971 * MREGION: 0 because MRVALID is 0
5972 */
5973 return 0;
5974 }
5975
5976 void switch_mode(CPUARMState *env, int mode)
5977 {
5978 ARMCPU *cpu = arm_env_get_cpu(env);
5979
5980 if (mode != ARM_CPU_MODE_USR) {
5981 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5982 }
5983 }
5984
5985 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5986 uint32_t cur_el, bool secure)
5987 {
5988 return 1;
5989 }
5990
5991 void aarch64_sync_64_to_32(CPUARMState *env)
5992 {
5993 g_assert_not_reached();
5994 }
5995
5996 #else
5997
5998 void switch_mode(CPUARMState *env, int mode)
5999 {
6000 int old_mode;
6001 int i;
6002
6003 old_mode = env->uncached_cpsr & CPSR_M;
6004 if (mode == old_mode)
6005 return;
6006
6007 if (old_mode == ARM_CPU_MODE_FIQ) {
6008 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
6009 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
6010 } else if (mode == ARM_CPU_MODE_FIQ) {
6011 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
6012 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
6013 }
6014
6015 i = bank_number(old_mode);
6016 env->banked_r13[i] = env->regs[13];
6017 env->banked_r14[i] = env->regs[14];
6018 env->banked_spsr[i] = env->spsr;
6019
6020 i = bank_number(mode);
6021 env->regs[13] = env->banked_r13[i];
6022 env->regs[14] = env->banked_r14[i];
6023 env->spsr = env->banked_spsr[i];
6024 }
6025
6026 /* Physical Interrupt Target EL Lookup Table
6027 *
6028 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6029 *
6030 * The below multi-dimensional table is used for looking up the target
6031 * exception level given numerous condition criteria. Specifically, the
6032 * target EL is based on SCR and HCR routing controls as well as the
6033 * currently executing EL and secure state.
6034 *
6035 * Dimensions:
6036 * target_el_table[2][2][2][2][2][4]
6037 * | | | | | +--- Current EL
6038 * | | | | +------ Non-secure(0)/Secure(1)
6039 * | | | +--------- HCR mask override
6040 * | | +------------ SCR exec state control
6041 * | +--------------- SCR mask override
6042 * +------------------ 32-bit(0)/64-bit(1) EL3
6043 *
6044 * The table values are as such:
6045 * 0-3 = EL0-EL3
6046 * -1 = Cannot occur
6047 *
6048 * The ARM ARM target EL table includes entries indicating that an "exception
6049 * is not taken". The two cases where this is applicable are:
6050 * 1) An exception is taken from EL3 but the SCR does not have the exception
6051 * routed to EL3.
6052 * 2) An exception is taken from EL2 but the HCR does not have the exception
6053 * routed to EL2.
6054 * In these two cases, the below table contain a target of EL1. This value is
6055 * returned as it is expected that the consumer of the table data will check
6056 * for "target EL >= current EL" to ensure the exception is not taken.
6057 *
6058 * SCR HCR
6059 * 64 EA AMO From
6060 * BIT IRQ IMO Non-secure Secure
6061 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6062 */
6063 static const int8_t target_el_table[2][2][2][2][2][4] = {
6064 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6065 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6066 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6067 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6068 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6069 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6070 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6071 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6072 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6073 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6074 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6075 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6076 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6077 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6078 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6079 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6080 };
6081
6082 /*
6083 * Determine the target EL for physical exceptions
6084 */
6085 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6086 uint32_t cur_el, bool secure)
6087 {
6088 CPUARMState *env = cs->env_ptr;
6089 int rw;
6090 int scr;
6091 int hcr;
6092 int target_el;
6093 /* Is the highest EL AArch64? */
6094 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6095
6096 if (arm_feature(env, ARM_FEATURE_EL3)) {
6097 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6098 } else {
6099 /* Either EL2 is the highest EL (and so the EL2 register width
6100 * is given by is64); or there is no EL2 or EL3, in which case
6101 * the value of 'rw' does not affect the table lookup anyway.
6102 */
6103 rw = is64;
6104 }
6105
6106 switch (excp_idx) {
6107 case EXCP_IRQ:
6108 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6109 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6110 break;
6111 case EXCP_FIQ:
6112 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6113 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6114 break;
6115 default:
6116 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6117 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6118 break;
6119 };
6120
6121 /* If HCR.TGE is set then HCR is treated as being 1 */
6122 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6123
6124 /* Perform a table-lookup for the target EL given the current state */
6125 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6126
6127 assert(target_el > 0);
6128
6129 return target_el;
6130 }
6131
6132 static void v7m_push(CPUARMState *env, uint32_t val)
6133 {
6134 CPUState *cs = CPU(arm_env_get_cpu(env));
6135
6136 env->regs[13] -= 4;
6137 stl_phys(cs->as, env->regs[13], val);
6138 }
6139
6140 /* Return true if we're using the process stack pointer (not the MSP) */
6141 static bool v7m_using_psp(CPUARMState *env)
6142 {
6143 /* Handler mode always uses the main stack; for thread mode
6144 * the CONTROL.SPSEL bit determines the answer.
6145 * Note that in v7M it is not possible to be in Handler mode with
6146 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6147 */
6148 return !arm_v7m_is_handler_mode(env) &&
6149 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6150 }
6151
6152 /* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6153 * This may change the current stack pointer between Main and Process
6154 * stack pointers if it is done for the CONTROL register for the current
6155 * security state.
6156 */
6157 static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6158 bool new_spsel,
6159 bool secstate)
6160 {
6161 bool old_is_psp = v7m_using_psp(env);
6162
6163 env->v7m.control[secstate] =
6164 deposit32(env->v7m.control[secstate],
6165 R_V7M_CONTROL_SPSEL_SHIFT,
6166 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6167
6168 if (secstate == env->v7m.secure) {
6169 bool new_is_psp = v7m_using_psp(env);
6170 uint32_t tmp;
6171
6172 if (old_is_psp != new_is_psp) {
6173 tmp = env->v7m.other_sp;
6174 env->v7m.other_sp = env->regs[13];
6175 env->regs[13] = tmp;
6176 }
6177 }
6178 }
6179
6180 /* Write to v7M CONTROL.SPSEL bit. This may change the current
6181 * stack pointer between Main and Process stack pointers.
6182 */
6183 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6184 {
6185 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6186 }
6187
6188 void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6189 {
6190 /* Write a new value to v7m.exception, thus transitioning into or out
6191 * of Handler mode; this may result in a change of active stack pointer.
6192 */
6193 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6194 uint32_t tmp;
6195
6196 env->v7m.exception = new_exc;
6197
6198 new_is_psp = v7m_using_psp(env);
6199
6200 if (old_is_psp != new_is_psp) {
6201 tmp = env->v7m.other_sp;
6202 env->v7m.other_sp = env->regs[13];
6203 env->regs[13] = tmp;
6204 }
6205 }
6206
6207 /* Switch M profile security state between NS and S */
6208 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6209 {
6210 uint32_t new_ss_msp, new_ss_psp;
6211
6212 if (env->v7m.secure == new_secstate) {
6213 return;
6214 }
6215
6216 /* All the banked state is accessed by looking at env->v7m.secure
6217 * except for the stack pointer; rearrange the SP appropriately.
6218 */
6219 new_ss_msp = env->v7m.other_ss_msp;
6220 new_ss_psp = env->v7m.other_ss_psp;
6221
6222 if (v7m_using_psp(env)) {
6223 env->v7m.other_ss_psp = env->regs[13];
6224 env->v7m.other_ss_msp = env->v7m.other_sp;
6225 } else {
6226 env->v7m.other_ss_msp = env->regs[13];
6227 env->v7m.other_ss_psp = env->v7m.other_sp;
6228 }
6229
6230 env->v7m.secure = new_secstate;
6231
6232 if (v7m_using_psp(env)) {
6233 env->regs[13] = new_ss_psp;
6234 env->v7m.other_sp = new_ss_msp;
6235 } else {
6236 env->regs[13] = new_ss_msp;
6237 env->v7m.other_sp = new_ss_psp;
6238 }
6239 }
6240
6241 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6242 {
6243 /* Handle v7M BXNS:
6244 * - if the return value is a magic value, do exception return (like BX)
6245 * - otherwise bit 0 of the return value is the target security state
6246 */
6247 uint32_t min_magic;
6248
6249 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6250 /* Covers FNC_RETURN and EXC_RETURN magic */
6251 min_magic = FNC_RETURN_MIN_MAGIC;
6252 } else {
6253 /* EXC_RETURN magic only */
6254 min_magic = EXC_RETURN_MIN_MAGIC;
6255 }
6256
6257 if (dest >= min_magic) {
6258 /* This is an exception return magic value; put it where
6259 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6260 * Note that if we ever add gen_ss_advance() singlestep support to
6261 * M profile this should count as an "instruction execution complete"
6262 * event (compare gen_bx_excret_final_code()).
6263 */
6264 env->regs[15] = dest & ~1;
6265 env->thumb = dest & 1;
6266 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6267 /* notreached */
6268 }
6269
6270 /* translate.c should have made BXNS UNDEF unless we're secure */
6271 assert(env->v7m.secure);
6272
6273 switch_v7m_security_state(env, dest & 1);
6274 env->thumb = 1;
6275 env->regs[15] = dest & ~1;
6276 }
6277
6278 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6279 {
6280 /* Handle v7M BLXNS:
6281 * - bit 0 of the destination address is the target security state
6282 */
6283
6284 /* At this point regs[15] is the address just after the BLXNS */
6285 uint32_t nextinst = env->regs[15] | 1;
6286 uint32_t sp = env->regs[13] - 8;
6287 uint32_t saved_psr;
6288
6289 /* translate.c will have made BLXNS UNDEF unless we're secure */
6290 assert(env->v7m.secure);
6291
6292 if (dest & 1) {
6293 /* target is Secure, so this is just a normal BLX,
6294 * except that the low bit doesn't indicate Thumb/not.
6295 */
6296 env->regs[14] = nextinst;
6297 env->thumb = 1;
6298 env->regs[15] = dest & ~1;
6299 return;
6300 }
6301
6302 /* Target is non-secure: first push a stack frame */
6303 if (!QEMU_IS_ALIGNED(sp, 8)) {
6304 qemu_log_mask(LOG_GUEST_ERROR,
6305 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6306 }
6307
6308 saved_psr = env->v7m.exception;
6309 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6310 saved_psr |= XPSR_SFPA;
6311 }
6312
6313 /* Note that these stores can throw exceptions on MPU faults */
6314 cpu_stl_data(env, sp, nextinst);
6315 cpu_stl_data(env, sp + 4, saved_psr);
6316
6317 env->regs[13] = sp;
6318 env->regs[14] = 0xfeffffff;
6319 if (arm_v7m_is_handler_mode(env)) {
6320 /* Write a dummy value to IPSR, to avoid leaking the current secure
6321 * exception number to non-secure code. This is guaranteed not
6322 * to cause write_v7m_exception() to actually change stacks.
6323 */
6324 write_v7m_exception(env, 1);
6325 }
6326 switch_v7m_security_state(env, 0);
6327 env->thumb = 1;
6328 env->regs[15] = dest;
6329 }
6330
6331 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6332 bool spsel)
6333 {
6334 /* Return a pointer to the location where we currently store the
6335 * stack pointer for the requested security state and thread mode.
6336 * This pointer will become invalid if the CPU state is updated
6337 * such that the stack pointers are switched around (eg changing
6338 * the SPSEL control bit).
6339 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6340 * Unlike that pseudocode, we require the caller to pass us in the
6341 * SPSEL control bit value; this is because we also use this
6342 * function in handling of pushing of the callee-saves registers
6343 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6344 * and in the tailchain codepath the SPSEL bit comes from the exception
6345 * return magic LR value from the previous exception. The pseudocode
6346 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6347 * to make this utility function generic enough to do the job.
6348 */
6349 bool want_psp = threadmode && spsel;
6350
6351 if (secure == env->v7m.secure) {
6352 if (want_psp == v7m_using_psp(env)) {
6353 return &env->regs[13];
6354 } else {
6355 return &env->v7m.other_sp;
6356 }
6357 } else {
6358 if (want_psp) {
6359 return &env->v7m.other_ss_psp;
6360 } else {
6361 return &env->v7m.other_ss_msp;
6362 }
6363 }
6364 }
6365
6366 static uint32_t arm_v7m_load_vector(ARMCPU *cpu, bool targets_secure)
6367 {
6368 CPUState *cs = CPU(cpu);
6369 CPUARMState *env = &cpu->env;
6370 MemTxResult result;
6371 hwaddr vec = env->v7m.vecbase[targets_secure] + env->v7m.exception * 4;
6372 uint32_t addr;
6373
6374 addr = address_space_ldl(cs->as, vec,
6375 MEMTXATTRS_UNSPECIFIED, &result);
6376 if (result != MEMTX_OK) {
6377 /* Architecturally this should cause a HardFault setting HSFR.VECTTBL,
6378 * which would then be immediately followed by our failing to load
6379 * the entry vector for that HardFault, which is a Lockup case.
6380 * Since we don't model Lockup, we just report this guest error
6381 * via cpu_abort().
6382 */
6383 cpu_abort(cs, "Failed to read from %s exception vector table "
6384 "entry %08x\n", targets_secure ? "secure" : "nonsecure",
6385 (unsigned)vec);
6386 }
6387 return addr;
6388 }
6389
6390 static void v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain)
6391 {
6392 /* For v8M, push the callee-saves register part of the stack frame.
6393 * Compare the v8M pseudocode PushCalleeStack().
6394 * In the tailchaining case this may not be the current stack.
6395 */
6396 CPUARMState *env = &cpu->env;
6397 CPUState *cs = CPU(cpu);
6398 uint32_t *frame_sp_p;
6399 uint32_t frameptr;
6400
6401 if (dotailchain) {
6402 frame_sp_p = get_v7m_sp_ptr(env, true,
6403 lr & R_V7M_EXCRET_MODE_MASK,
6404 lr & R_V7M_EXCRET_SPSEL_MASK);
6405 } else {
6406 frame_sp_p = &env->regs[13];
6407 }
6408
6409 frameptr = *frame_sp_p - 0x28;
6410
6411 stl_phys(cs->as, frameptr, 0xfefa125b);
6412 stl_phys(cs->as, frameptr + 0x8, env->regs[4]);
6413 stl_phys(cs->as, frameptr + 0xc, env->regs[5]);
6414 stl_phys(cs->as, frameptr + 0x10, env->regs[6]);
6415 stl_phys(cs->as, frameptr + 0x14, env->regs[7]);
6416 stl_phys(cs->as, frameptr + 0x18, env->regs[8]);
6417 stl_phys(cs->as, frameptr + 0x1c, env->regs[9]);
6418 stl_phys(cs->as, frameptr + 0x20, env->regs[10]);
6419 stl_phys(cs->as, frameptr + 0x24, env->regs[11]);
6420
6421 *frame_sp_p = frameptr;
6422 }
6423
6424 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain)
6425 {
6426 /* Do the "take the exception" parts of exception entry,
6427 * but not the pushing of state to the stack. This is
6428 * similar to the pseudocode ExceptionTaken() function.
6429 */
6430 CPUARMState *env = &cpu->env;
6431 uint32_t addr;
6432 bool targets_secure;
6433
6434 targets_secure = armv7m_nvic_acknowledge_irq(env->nvic);
6435
6436 if (arm_feature(env, ARM_FEATURE_V8)) {
6437 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6438 (lr & R_V7M_EXCRET_S_MASK)) {
6439 /* The background code (the owner of the registers in the
6440 * exception frame) is Secure. This means it may either already
6441 * have or now needs to push callee-saves registers.
6442 */
6443 if (targets_secure) {
6444 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6445 /* We took an exception from Secure to NonSecure
6446 * (which means the callee-saved registers got stacked)
6447 * and are now tailchaining to a Secure exception.
6448 * Clear DCRS so eventual return from this Secure
6449 * exception unstacks the callee-saved registers.
6450 */
6451 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6452 }
6453 } else {
6454 /* We're going to a non-secure exception; push the
6455 * callee-saves registers to the stack now, if they're
6456 * not already saved.
6457 */
6458 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6459 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
6460 v7m_push_callee_stack(cpu, lr, dotailchain);
6461 }
6462 lr |= R_V7M_EXCRET_DCRS_MASK;
6463 }
6464 }
6465
6466 lr &= ~R_V7M_EXCRET_ES_MASK;
6467 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6468 lr |= R_V7M_EXCRET_ES_MASK;
6469 }
6470 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6471 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6472 lr |= R_V7M_EXCRET_SPSEL_MASK;
6473 }
6474
6475 /* Clear registers if necessary to prevent non-secure exception
6476 * code being able to see register values from secure code.
6477 * Where register values become architecturally UNKNOWN we leave
6478 * them with their previous values.
6479 */
6480 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6481 if (!targets_secure) {
6482 /* Always clear the caller-saved registers (they have been
6483 * pushed to the stack earlier in v7m_push_stack()).
6484 * Clear callee-saved registers if the background code is
6485 * Secure (in which case these regs were saved in
6486 * v7m_push_callee_stack()).
6487 */
6488 int i;
6489
6490 for (i = 0; i < 13; i++) {
6491 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6492 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6493 env->regs[i] = 0;
6494 }
6495 }
6496 /* Clear EAPSR */
6497 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6498 }
6499 }
6500 }
6501
6502 /* Switch to target security state -- must do this before writing SPSEL */
6503 switch_v7m_security_state(env, targets_secure);
6504 write_v7m_control_spsel(env, 0);
6505 arm_clear_exclusive(env);
6506 /* Clear IT bits */
6507 env->condexec_bits = 0;
6508 env->regs[14] = lr;
6509 addr = arm_v7m_load_vector(cpu, targets_secure);
6510 env->regs[15] = addr & 0xfffffffe;
6511 env->thumb = addr & 1;
6512 }
6513
6514 static void v7m_push_stack(ARMCPU *cpu)
6515 {
6516 /* Do the "set up stack frame" part of exception entry,
6517 * similar to pseudocode PushStack().
6518 */
6519 CPUARMState *env = &cpu->env;
6520 uint32_t xpsr = xpsr_read(env);
6521
6522 /* Align stack pointer if the guest wants that */
6523 if ((env->regs[13] & 4) &&
6524 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
6525 env->regs[13] -= 4;
6526 xpsr |= XPSR_SPREALIGN;
6527 }
6528 /* Switch to the handler mode. */
6529 v7m_push(env, xpsr);
6530 v7m_push(env, env->regs[15]);
6531 v7m_push(env, env->regs[14]);
6532 v7m_push(env, env->regs[12]);
6533 v7m_push(env, env->regs[3]);
6534 v7m_push(env, env->regs[2]);
6535 v7m_push(env, env->regs[1]);
6536 v7m_push(env, env->regs[0]);
6537 }
6538
6539 static void do_v7m_exception_exit(ARMCPU *cpu)
6540 {
6541 CPUARMState *env = &cpu->env;
6542 CPUState *cs = CPU(cpu);
6543 uint32_t excret;
6544 uint32_t xpsr;
6545 bool ufault = false;
6546 bool sfault = false;
6547 bool return_to_sp_process;
6548 bool return_to_handler;
6549 bool rettobase = false;
6550 bool exc_secure = false;
6551 bool return_to_secure;
6552
6553 /* If we're not in Handler mode then jumps to magic exception-exit
6554 * addresses don't have magic behaviour. However for the v8M
6555 * security extensions the magic secure-function-return has to
6556 * work in thread mode too, so to avoid doing an extra check in
6557 * the generated code we allow exception-exit magic to also cause the
6558 * internal exception and bring us here in thread mode. Correct code
6559 * will never try to do this (the following insn fetch will always
6560 * fault) so we the overhead of having taken an unnecessary exception
6561 * doesn't matter.
6562 */
6563 if (!arm_v7m_is_handler_mode(env)) {
6564 return;
6565 }
6566
6567 /* In the spec pseudocode ExceptionReturn() is called directly
6568 * from BXWritePC() and gets the full target PC value including
6569 * bit zero. In QEMU's implementation we treat it as a normal
6570 * jump-to-register (which is then caught later on), and so split
6571 * the target value up between env->regs[15] and env->thumb in
6572 * gen_bx(). Reconstitute it.
6573 */
6574 excret = env->regs[15];
6575 if (env->thumb) {
6576 excret |= 1;
6577 }
6578
6579 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
6580 " previous exception %d\n",
6581 excret, env->v7m.exception);
6582
6583 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
6584 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
6585 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
6586 excret);
6587 }
6588
6589 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6590 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
6591 * we pick which FAULTMASK to clear.
6592 */
6593 if (!env->v7m.secure &&
6594 ((excret & R_V7M_EXCRET_ES_MASK) ||
6595 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
6596 sfault = 1;
6597 /* For all other purposes, treat ES as 0 (R_HXSR) */
6598 excret &= ~R_V7M_EXCRET_ES_MASK;
6599 }
6600 }
6601
6602 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
6603 /* Auto-clear FAULTMASK on return from other than NMI.
6604 * If the security extension is implemented then this only
6605 * happens if the raw execution priority is >= 0; the
6606 * value of the ES bit in the exception return value indicates
6607 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
6608 */
6609 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6610 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
6611 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
6612 env->v7m.faultmask[exc_secure] = 0;
6613 }
6614 } else {
6615 env->v7m.faultmask[M_REG_NS] = 0;
6616 }
6617 }
6618
6619 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
6620 exc_secure)) {
6621 case -1:
6622 /* attempt to exit an exception that isn't active */
6623 ufault = true;
6624 break;
6625 case 0:
6626 /* still an irq active now */
6627 break;
6628 case 1:
6629 /* we returned to base exception level, no nesting.
6630 * (In the pseudocode this is written using "NestedActivation != 1"
6631 * where we have 'rettobase == false'.)
6632 */
6633 rettobase = true;
6634 break;
6635 default:
6636 g_assert_not_reached();
6637 }
6638
6639 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
6640 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
6641 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6642 (excret & R_V7M_EXCRET_S_MASK);
6643
6644 if (arm_feature(env, ARM_FEATURE_V8)) {
6645 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6646 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
6647 * we choose to take the UsageFault.
6648 */
6649 if ((excret & R_V7M_EXCRET_S_MASK) ||
6650 (excret & R_V7M_EXCRET_ES_MASK) ||
6651 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
6652 ufault = true;
6653 }
6654 }
6655 if (excret & R_V7M_EXCRET_RES0_MASK) {
6656 ufault = true;
6657 }
6658 } else {
6659 /* For v7M we only recognize certain combinations of the low bits */
6660 switch (excret & 0xf) {
6661 case 1: /* Return to Handler */
6662 break;
6663 case 13: /* Return to Thread using Process stack */
6664 case 9: /* Return to Thread using Main stack */
6665 /* We only need to check NONBASETHRDENA for v7M, because in
6666 * v8M this bit does not exist (it is RES1).
6667 */
6668 if (!rettobase &&
6669 !(env->v7m.ccr[env->v7m.secure] &
6670 R_V7M_CCR_NONBASETHRDENA_MASK)) {
6671 ufault = true;
6672 }
6673 break;
6674 default:
6675 ufault = true;
6676 }
6677 }
6678
6679 if (sfault) {
6680 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
6681 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6682 v7m_exception_taken(cpu, excret, true);
6683 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6684 "stackframe: failed EXC_RETURN.ES validity check\n");
6685 return;
6686 }
6687
6688 if (ufault) {
6689 /* Bad exception return: instead of popping the exception
6690 * stack, directly take a usage fault on the current stack.
6691 */
6692 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6693 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
6694 v7m_exception_taken(cpu, excret, true);
6695 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6696 "stackframe: failed exception return integrity check\n");
6697 return;
6698 }
6699
6700 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
6701 * Handler mode (and will be until we write the new XPSR.Interrupt
6702 * field) this does not switch around the current stack pointer.
6703 */
6704 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
6705
6706 switch_v7m_security_state(env, return_to_secure);
6707
6708 {
6709 /* The stack pointer we should be reading the exception frame from
6710 * depends on bits in the magic exception return type value (and
6711 * for v8M isn't necessarily the stack pointer we will eventually
6712 * end up resuming execution with). Get a pointer to the location
6713 * in the CPU state struct where the SP we need is currently being
6714 * stored; we will use and modify it in place.
6715 * We use this limited C variable scope so we don't accidentally
6716 * use 'frame_sp_p' after we do something that makes it invalid.
6717 */
6718 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
6719 return_to_secure,
6720 !return_to_handler,
6721 return_to_sp_process);
6722 uint32_t frameptr = *frame_sp_p;
6723
6724 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
6725 arm_feature(env, ARM_FEATURE_V8)) {
6726 qemu_log_mask(LOG_GUEST_ERROR,
6727 "M profile exception return with non-8-aligned SP "
6728 "for destination state is UNPREDICTABLE\n");
6729 }
6730
6731 /* Do we need to pop callee-saved registers? */
6732 if (return_to_secure &&
6733 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
6734 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
6735 uint32_t expected_sig = 0xfefa125b;
6736 uint32_t actual_sig = ldl_phys(cs->as, frameptr);
6737
6738 if (expected_sig != actual_sig) {
6739 /* Take a SecureFault on the current stack */
6740 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
6741 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6742 v7m_exception_taken(cpu, excret, true);
6743 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6744 "stackframe: failed exception return integrity "
6745 "signature check\n");
6746 return;
6747 }
6748
6749 env->regs[4] = ldl_phys(cs->as, frameptr + 0x8);
6750 env->regs[5] = ldl_phys(cs->as, frameptr + 0xc);
6751 env->regs[6] = ldl_phys(cs->as, frameptr + 0x10);
6752 env->regs[7] = ldl_phys(cs->as, frameptr + 0x14);
6753 env->regs[8] = ldl_phys(cs->as, frameptr + 0x18);
6754 env->regs[9] = ldl_phys(cs->as, frameptr + 0x1c);
6755 env->regs[10] = ldl_phys(cs->as, frameptr + 0x20);
6756 env->regs[11] = ldl_phys(cs->as, frameptr + 0x24);
6757
6758 frameptr += 0x28;
6759 }
6760
6761 /* Pop registers. TODO: make these accesses use the correct
6762 * attributes and address space (S/NS, priv/unpriv) and handle
6763 * memory transaction failures.
6764 */
6765 env->regs[0] = ldl_phys(cs->as, frameptr);
6766 env->regs[1] = ldl_phys(cs->as, frameptr + 0x4);
6767 env->regs[2] = ldl_phys(cs->as, frameptr + 0x8);
6768 env->regs[3] = ldl_phys(cs->as, frameptr + 0xc);
6769 env->regs[12] = ldl_phys(cs->as, frameptr + 0x10);
6770 env->regs[14] = ldl_phys(cs->as, frameptr + 0x14);
6771 env->regs[15] = ldl_phys(cs->as, frameptr + 0x18);
6772
6773 /* Returning from an exception with a PC with bit 0 set is defined
6774 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
6775 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
6776 * the lsbit, and there are several RTOSes out there which incorrectly
6777 * assume the r15 in the stack frame should be a Thumb-style "lsbit
6778 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
6779 * complain about the badly behaved guest.
6780 */
6781 if (env->regs[15] & 1) {
6782 env->regs[15] &= ~1U;
6783 if (!arm_feature(env, ARM_FEATURE_V8)) {
6784 qemu_log_mask(LOG_GUEST_ERROR,
6785 "M profile return from interrupt with misaligned "
6786 "PC is UNPREDICTABLE on v7M\n");
6787 }
6788 }
6789
6790 xpsr = ldl_phys(cs->as, frameptr + 0x1c);
6791
6792 if (arm_feature(env, ARM_FEATURE_V8)) {
6793 /* For v8M we have to check whether the xPSR exception field
6794 * matches the EXCRET value for return to handler/thread
6795 * before we commit to changing the SP and xPSR.
6796 */
6797 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
6798 if (return_to_handler != will_be_handler) {
6799 /* Take an INVPC UsageFault on the current stack.
6800 * By this point we will have switched to the security state
6801 * for the background state, so this UsageFault will target
6802 * that state.
6803 */
6804 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
6805 env->v7m.secure);
6806 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6807 v7m_exception_taken(cpu, excret, true);
6808 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6809 "stackframe: failed exception return integrity "
6810 "check\n");
6811 return;
6812 }
6813 }
6814
6815 /* Commit to consuming the stack frame */
6816 frameptr += 0x20;
6817 /* Undo stack alignment (the SPREALIGN bit indicates that the original
6818 * pre-exception SP was not 8-aligned and we added a padding word to
6819 * align it, so we undo this by ORing in the bit that increases it
6820 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
6821 * would work too but a logical OR is how the pseudocode specifies it.)
6822 */
6823 if (xpsr & XPSR_SPREALIGN) {
6824 frameptr |= 4;
6825 }
6826 *frame_sp_p = frameptr;
6827 }
6828 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
6829 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
6830
6831 /* The restored xPSR exception field will be zero if we're
6832 * resuming in Thread mode. If that doesn't match what the
6833 * exception return excret specified then this is a UsageFault.
6834 * v7M requires we make this check here; v8M did it earlier.
6835 */
6836 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
6837 /* Take an INVPC UsageFault by pushing the stack again;
6838 * we know we're v7M so this is never a Secure UsageFault.
6839 */
6840 assert(!arm_feature(env, ARM_FEATURE_V8));
6841 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
6842 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6843 v7m_push_stack(cpu);
6844 v7m_exception_taken(cpu, excret, false);
6845 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
6846 "failed exception return integrity check\n");
6847 return;
6848 }
6849
6850 /* Otherwise, we have a successful exception exit. */
6851 arm_clear_exclusive(env);
6852 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
6853 }
6854
6855 static bool do_v7m_function_return(ARMCPU *cpu)
6856 {
6857 /* v8M security extensions magic function return.
6858 * We may either:
6859 * (1) throw an exception (longjump)
6860 * (2) return true if we successfully handled the function return
6861 * (3) return false if we failed a consistency check and have
6862 * pended a UsageFault that needs to be taken now
6863 *
6864 * At this point the magic return value is split between env->regs[15]
6865 * and env->thumb. We don't bother to reconstitute it because we don't
6866 * need it (all values are handled the same way).
6867 */
6868 CPUARMState *env = &cpu->env;
6869 uint32_t newpc, newpsr, newpsr_exc;
6870
6871 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
6872
6873 {
6874 bool threadmode, spsel;
6875 TCGMemOpIdx oi;
6876 ARMMMUIdx mmu_idx;
6877 uint32_t *frame_sp_p;
6878 uint32_t frameptr;
6879
6880 /* Pull the return address and IPSR from the Secure stack */
6881 threadmode = !arm_v7m_is_handler_mode(env);
6882 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
6883
6884 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
6885 frameptr = *frame_sp_p;
6886
6887 /* These loads may throw an exception (for MPU faults). We want to
6888 * do them as secure, so work out what MMU index that is.
6889 */
6890 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
6891 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
6892 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
6893 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
6894
6895 /* Consistency checks on new IPSR */
6896 newpsr_exc = newpsr & XPSR_EXCP;
6897 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
6898 (env->v7m.exception == 1 && newpsr_exc != 0))) {
6899 /* Pend the fault and tell our caller to take it */
6900 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6901 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
6902 env->v7m.secure);
6903 qemu_log_mask(CPU_LOG_INT,
6904 "...taking INVPC UsageFault: "
6905 "IPSR consistency check failed\n");
6906 return false;
6907 }
6908
6909 *frame_sp_p = frameptr + 8;
6910 }
6911
6912 /* This invalidates frame_sp_p */
6913 switch_v7m_security_state(env, true);
6914 env->v7m.exception = newpsr_exc;
6915 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
6916 if (newpsr & XPSR_SFPA) {
6917 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
6918 }
6919 xpsr_write(env, 0, XPSR_IT);
6920 env->thumb = newpc & 1;
6921 env->regs[15] = newpc & ~1;
6922
6923 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
6924 return true;
6925 }
6926
6927 static void arm_log_exception(int idx)
6928 {
6929 if (qemu_loglevel_mask(CPU_LOG_INT)) {
6930 const char *exc = NULL;
6931 static const char * const excnames[] = {
6932 [EXCP_UDEF] = "Undefined Instruction",
6933 [EXCP_SWI] = "SVC",
6934 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
6935 [EXCP_DATA_ABORT] = "Data Abort",
6936 [EXCP_IRQ] = "IRQ",
6937 [EXCP_FIQ] = "FIQ",
6938 [EXCP_BKPT] = "Breakpoint",
6939 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
6940 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
6941 [EXCP_HVC] = "Hypervisor Call",
6942 [EXCP_HYP_TRAP] = "Hypervisor Trap",
6943 [EXCP_SMC] = "Secure Monitor Call",
6944 [EXCP_VIRQ] = "Virtual IRQ",
6945 [EXCP_VFIQ] = "Virtual FIQ",
6946 [EXCP_SEMIHOST] = "Semihosting call",
6947 [EXCP_NOCP] = "v7M NOCP UsageFault",
6948 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
6949 };
6950
6951 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
6952 exc = excnames[idx];
6953 }
6954 if (!exc) {
6955 exc = "unknown";
6956 }
6957 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
6958 }
6959 }
6960
6961 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
6962 uint32_t addr, uint16_t *insn)
6963 {
6964 /* Load a 16-bit portion of a v7M instruction, returning true on success,
6965 * or false on failure (in which case we will have pended the appropriate
6966 * exception).
6967 * We need to do the instruction fetch's MPU and SAU checks
6968 * like this because there is no MMU index that would allow
6969 * doing the load with a single function call. Instead we must
6970 * first check that the security attributes permit the load
6971 * and that they don't mismatch on the two halves of the instruction,
6972 * and then we do the load as a secure load (ie using the security
6973 * attributes of the address, not the CPU, as architecturally required).
6974 */
6975 CPUState *cs = CPU(cpu);
6976 CPUARMState *env = &cpu->env;
6977 V8M_SAttributes sattrs = {};
6978 MemTxAttrs attrs = {};
6979 ARMMMUFaultInfo fi = {};
6980 MemTxResult txres;
6981 target_ulong page_size;
6982 hwaddr physaddr;
6983 int prot;
6984 uint32_t fsr;
6985
6986 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
6987 if (!sattrs.nsc || sattrs.ns) {
6988 /* This must be the second half of the insn, and it straddles a
6989 * region boundary with the second half not being S&NSC.
6990 */
6991 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
6992 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6993 qemu_log_mask(CPU_LOG_INT,
6994 "...really SecureFault with SFSR.INVEP\n");
6995 return false;
6996 }
6997 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
6998 &physaddr, &attrs, &prot, &page_size, &fsr, &fi, NULL)) {
6999 /* the MPU lookup failed */
7000 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7001 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
7002 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
7003 return false;
7004 }
7005 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7006 attrs, &txres);
7007 if (txres != MEMTX_OK) {
7008 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7009 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7010 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7011 return false;
7012 }
7013 return true;
7014 }
7015
7016 static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7017 {
7018 /* Check whether this attempt to execute code in a Secure & NS-Callable
7019 * memory region is for an SG instruction; if so, then emulate the
7020 * effect of the SG instruction and return true. Otherwise pend
7021 * the correct kind of exception and return false.
7022 */
7023 CPUARMState *env = &cpu->env;
7024 ARMMMUIdx mmu_idx;
7025 uint16_t insn;
7026
7027 /* We should never get here unless get_phys_addr_pmsav8() caused
7028 * an exception for NS executing in S&NSC memory.
7029 */
7030 assert(!env->v7m.secure);
7031 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7032
7033 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7034 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7035
7036 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7037 return false;
7038 }
7039
7040 if (!env->thumb) {
7041 goto gen_invep;
7042 }
7043
7044 if (insn != 0xe97f) {
7045 /* Not an SG instruction first half (we choose the IMPDEF
7046 * early-SG-check option).
7047 */
7048 goto gen_invep;
7049 }
7050
7051 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7052 return false;
7053 }
7054
7055 if (insn != 0xe97f) {
7056 /* Not an SG instruction second half (yes, both halves of the SG
7057 * insn have the same hex value)
7058 */
7059 goto gen_invep;
7060 }
7061
7062 /* OK, we have confirmed that we really have an SG instruction.
7063 * We know we're NS in S memory so don't need to repeat those checks.
7064 */
7065 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7066 ", executing it\n", env->regs[15]);
7067 env->regs[14] &= ~1;
7068 switch_v7m_security_state(env, true);
7069 xpsr_write(env, 0, XPSR_IT);
7070 env->regs[15] += 4;
7071 return true;
7072
7073 gen_invep:
7074 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7075 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7076 qemu_log_mask(CPU_LOG_INT,
7077 "...really SecureFault with SFSR.INVEP\n");
7078 return false;
7079 }
7080
7081 void arm_v7m_cpu_do_interrupt(CPUState *cs)
7082 {
7083 ARMCPU *cpu = ARM_CPU(cs);
7084 CPUARMState *env = &cpu->env;
7085 uint32_t lr;
7086
7087 arm_log_exception(cs->exception_index);
7088
7089 /* For exceptions we just mark as pending on the NVIC, and let that
7090 handle it. */
7091 switch (cs->exception_index) {
7092 case EXCP_UDEF:
7093 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7094 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
7095 break;
7096 case EXCP_NOCP:
7097 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7098 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
7099 break;
7100 case EXCP_INVSTATE:
7101 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7102 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
7103 break;
7104 case EXCP_SWI:
7105 /* The PC already points to the next instruction. */
7106 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
7107 break;
7108 case EXCP_PREFETCH_ABORT:
7109 case EXCP_DATA_ABORT:
7110 /* Note that for M profile we don't have a guest facing FSR, but
7111 * the env->exception.fsr will be populated by the code that
7112 * raises the fault, in the A profile short-descriptor format.
7113 */
7114 switch (env->exception.fsr & 0xf) {
7115 case M_FAKE_FSR_NSC_EXEC:
7116 /* Exception generated when we try to execute code at an address
7117 * which is marked as Secure & Non-Secure Callable and the CPU
7118 * is in the Non-Secure state. The only instruction which can
7119 * be executed like this is SG (and that only if both halves of
7120 * the SG instruction have the same security attributes.)
7121 * Everything else must generate an INVEP SecureFault, so we
7122 * emulate the SG instruction here.
7123 */
7124 if (v7m_handle_execute_nsc(cpu)) {
7125 return;
7126 }
7127 break;
7128 case M_FAKE_FSR_SFAULT:
7129 /* Various flavours of SecureFault for attempts to execute or
7130 * access data in the wrong security state.
7131 */
7132 switch (cs->exception_index) {
7133 case EXCP_PREFETCH_ABORT:
7134 if (env->v7m.secure) {
7135 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7136 qemu_log_mask(CPU_LOG_INT,
7137 "...really SecureFault with SFSR.INVTRAN\n");
7138 } else {
7139 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7140 qemu_log_mask(CPU_LOG_INT,
7141 "...really SecureFault with SFSR.INVEP\n");
7142 }
7143 break;
7144 case EXCP_DATA_ABORT:
7145 /* This must be an NS access to S memory */
7146 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7147 qemu_log_mask(CPU_LOG_INT,
7148 "...really SecureFault with SFSR.AUVIOL\n");
7149 break;
7150 }
7151 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7152 break;
7153 case 0x8: /* External Abort */
7154 switch (cs->exception_index) {
7155 case EXCP_PREFETCH_ABORT:
7156 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7157 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
7158 break;
7159 case EXCP_DATA_ABORT:
7160 env->v7m.cfsr[M_REG_NS] |=
7161 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
7162 env->v7m.bfar = env->exception.vaddress;
7163 qemu_log_mask(CPU_LOG_INT,
7164 "...with CFSR.PRECISERR and BFAR 0x%x\n",
7165 env->v7m.bfar);
7166 break;
7167 }
7168 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7169 break;
7170 default:
7171 /* All other FSR values are either MPU faults or "can't happen
7172 * for M profile" cases.
7173 */
7174 switch (cs->exception_index) {
7175 case EXCP_PREFETCH_ABORT:
7176 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7177 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7178 break;
7179 case EXCP_DATA_ABORT:
7180 env->v7m.cfsr[env->v7m.secure] |=
7181 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
7182 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
7183 qemu_log_mask(CPU_LOG_INT,
7184 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
7185 env->v7m.mmfar[env->v7m.secure]);
7186 break;
7187 }
7188 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7189 env->v7m.secure);
7190 break;
7191 }
7192 break;
7193 case EXCP_BKPT:
7194 if (semihosting_enabled()) {
7195 int nr;
7196 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
7197 if (nr == 0xab) {
7198 env->regs[15] += 2;
7199 qemu_log_mask(CPU_LOG_INT,
7200 "...handling as semihosting call 0x%x\n",
7201 env->regs[0]);
7202 env->regs[0] = do_arm_semihosting(env);
7203 return;
7204 }
7205 }
7206 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
7207 break;
7208 case EXCP_IRQ:
7209 break;
7210 case EXCP_EXCEPTION_EXIT:
7211 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7212 /* Must be v8M security extension function return */
7213 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7214 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7215 if (do_v7m_function_return(cpu)) {
7216 return;
7217 }
7218 } else {
7219 do_v7m_exception_exit(cpu);
7220 return;
7221 }
7222 break;
7223 default:
7224 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7225 return; /* Never happens. Keep compiler happy. */
7226 }
7227
7228 if (arm_feature(env, ARM_FEATURE_V8)) {
7229 lr = R_V7M_EXCRET_RES1_MASK |
7230 R_V7M_EXCRET_DCRS_MASK |
7231 R_V7M_EXCRET_FTYPE_MASK;
7232 /* The S bit indicates whether we should return to Secure
7233 * or NonSecure (ie our current state).
7234 * The ES bit indicates whether we're taking this exception
7235 * to Secure or NonSecure (ie our target state). We set it
7236 * later, in v7m_exception_taken().
7237 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7238 * This corresponds to the ARM ARM pseudocode for v8M setting
7239 * some LR bits in PushStack() and some in ExceptionTaken();
7240 * the distinction matters for the tailchain cases where we
7241 * can take an exception without pushing the stack.
7242 */
7243 if (env->v7m.secure) {
7244 lr |= R_V7M_EXCRET_S_MASK;
7245 }
7246 } else {
7247 lr = R_V7M_EXCRET_RES1_MASK |
7248 R_V7M_EXCRET_S_MASK |
7249 R_V7M_EXCRET_DCRS_MASK |
7250 R_V7M_EXCRET_FTYPE_MASK |
7251 R_V7M_EXCRET_ES_MASK;
7252 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7253 lr |= R_V7M_EXCRET_SPSEL_MASK;
7254 }
7255 }
7256 if (!arm_v7m_is_handler_mode(env)) {
7257 lr |= R_V7M_EXCRET_MODE_MASK;
7258 }
7259
7260 v7m_push_stack(cpu);
7261 v7m_exception_taken(cpu, lr, false);
7262 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
7263 }
7264
7265 /* Function used to synchronize QEMU's AArch64 register set with AArch32
7266 * register set. This is necessary when switching between AArch32 and AArch64
7267 * execution state.
7268 */
7269 void aarch64_sync_32_to_64(CPUARMState *env)
7270 {
7271 int i;
7272 uint32_t mode = env->uncached_cpsr & CPSR_M;
7273
7274 /* We can blanket copy R[0:7] to X[0:7] */
7275 for (i = 0; i < 8; i++) {
7276 env->xregs[i] = env->regs[i];
7277 }
7278
7279 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7280 * Otherwise, they come from the banked user regs.
7281 */
7282 if (mode == ARM_CPU_MODE_FIQ) {
7283 for (i = 8; i < 13; i++) {
7284 env->xregs[i] = env->usr_regs[i - 8];
7285 }
7286 } else {
7287 for (i = 8; i < 13; i++) {
7288 env->xregs[i] = env->regs[i];
7289 }
7290 }
7291
7292 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7293 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7294 * from the mode banked register.
7295 */
7296 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7297 env->xregs[13] = env->regs[13];
7298 env->xregs[14] = env->regs[14];
7299 } else {
7300 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7301 /* HYP is an exception in that it is copied from r14 */
7302 if (mode == ARM_CPU_MODE_HYP) {
7303 env->xregs[14] = env->regs[14];
7304 } else {
7305 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7306 }
7307 }
7308
7309 if (mode == ARM_CPU_MODE_HYP) {
7310 env->xregs[15] = env->regs[13];
7311 } else {
7312 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7313 }
7314
7315 if (mode == ARM_CPU_MODE_IRQ) {
7316 env->xregs[16] = env->regs[14];
7317 env->xregs[17] = env->regs[13];
7318 } else {
7319 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7320 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
7321 }
7322
7323 if (mode == ARM_CPU_MODE_SVC) {
7324 env->xregs[18] = env->regs[14];
7325 env->xregs[19] = env->regs[13];
7326 } else {
7327 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7328 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
7329 }
7330
7331 if (mode == ARM_CPU_MODE_ABT) {
7332 env->xregs[20] = env->regs[14];
7333 env->xregs[21] = env->regs[13];
7334 } else {
7335 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7336 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
7337 }
7338
7339 if (mode == ARM_CPU_MODE_UND) {
7340 env->xregs[22] = env->regs[14];
7341 env->xregs[23] = env->regs[13];
7342 } else {
7343 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7344 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
7345 }
7346
7347 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7348 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7349 * FIQ bank for r8-r14.
7350 */
7351 if (mode == ARM_CPU_MODE_FIQ) {
7352 for (i = 24; i < 31; i++) {
7353 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7354 }
7355 } else {
7356 for (i = 24; i < 29; i++) {
7357 env->xregs[i] = env->fiq_regs[i - 24];
7358 }
7359 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7360 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7361 }
7362
7363 env->pc = env->regs[15];
7364 }
7365
7366 /* Function used to synchronize QEMU's AArch32 register set with AArch64
7367 * register set. This is necessary when switching between AArch32 and AArch64
7368 * execution state.
7369 */
7370 void aarch64_sync_64_to_32(CPUARMState *env)
7371 {
7372 int i;
7373 uint32_t mode = env->uncached_cpsr & CPSR_M;
7374
7375 /* We can blanket copy X[0:7] to R[0:7] */
7376 for (i = 0; i < 8; i++) {
7377 env->regs[i] = env->xregs[i];
7378 }
7379
7380 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7381 * Otherwise, we copy x8-x12 into the banked user regs.
7382 */
7383 if (mode == ARM_CPU_MODE_FIQ) {
7384 for (i = 8; i < 13; i++) {
7385 env->usr_regs[i - 8] = env->xregs[i];
7386 }
7387 } else {
7388 for (i = 8; i < 13; i++) {
7389 env->regs[i] = env->xregs[i];
7390 }
7391 }
7392
7393 /* Registers r13 & r14 depend on the current mode.
7394 * If we are in a given mode, we copy the corresponding x registers to r13
7395 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7396 * for the mode.
7397 */
7398 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7399 env->regs[13] = env->xregs[13];
7400 env->regs[14] = env->xregs[14];
7401 } else {
7402 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7403
7404 /* HYP is an exception in that it does not have its own banked r14 but
7405 * shares the USR r14
7406 */
7407 if (mode == ARM_CPU_MODE_HYP) {
7408 env->regs[14] = env->xregs[14];
7409 } else {
7410 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7411 }
7412 }
7413
7414 if (mode == ARM_CPU_MODE_HYP) {
7415 env->regs[13] = env->xregs[15];
7416 } else {
7417 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7418 }
7419
7420 if (mode == ARM_CPU_MODE_IRQ) {
7421 env->regs[14] = env->xregs[16];
7422 env->regs[13] = env->xregs[17];
7423 } else {
7424 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7425 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
7426 }
7427
7428 if (mode == ARM_CPU_MODE_SVC) {
7429 env->regs[14] = env->xregs[18];
7430 env->regs[13] = env->xregs[19];
7431 } else {
7432 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7433 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
7434 }
7435
7436 if (mode == ARM_CPU_MODE_ABT) {
7437 env->regs[14] = env->xregs[20];
7438 env->regs[13] = env->xregs[21];
7439 } else {
7440 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7441 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
7442 }
7443
7444 if (mode == ARM_CPU_MODE_UND) {
7445 env->regs[14] = env->xregs[22];
7446 env->regs[13] = env->xregs[23];
7447 } else {
7448 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7449 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
7450 }
7451
7452 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7453 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7454 * FIQ bank for r8-r14.
7455 */
7456 if (mode == ARM_CPU_MODE_FIQ) {
7457 for (i = 24; i < 31; i++) {
7458 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7459 }
7460 } else {
7461 for (i = 24; i < 29; i++) {
7462 env->fiq_regs[i - 24] = env->xregs[i];
7463 }
7464 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7465 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7466 }
7467
7468 env->regs[15] = env->pc;
7469 }
7470
7471 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
7472 {
7473 ARMCPU *cpu = ARM_CPU(cs);
7474 CPUARMState *env = &cpu->env;
7475 uint32_t addr;
7476 uint32_t mask;
7477 int new_mode;
7478 uint32_t offset;
7479 uint32_t moe;
7480
7481 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7482 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7483 case EC_BREAKPOINT:
7484 case EC_BREAKPOINT_SAME_EL:
7485 moe = 1;
7486 break;
7487 case EC_WATCHPOINT:
7488 case EC_WATCHPOINT_SAME_EL:
7489 moe = 10;
7490 break;
7491 case EC_AA32_BKPT:
7492 moe = 3;
7493 break;
7494 case EC_VECTORCATCH:
7495 moe = 5;
7496 break;
7497 default:
7498 moe = 0;
7499 break;
7500 }
7501
7502 if (moe) {
7503 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7504 }
7505
7506 /* TODO: Vectored interrupt controller. */
7507 switch (cs->exception_index) {
7508 case EXCP_UDEF:
7509 new_mode = ARM_CPU_MODE_UND;
7510 addr = 0x04;
7511 mask = CPSR_I;
7512 if (env->thumb)
7513 offset = 2;
7514 else
7515 offset = 4;
7516 break;
7517 case EXCP_SWI:
7518 new_mode = ARM_CPU_MODE_SVC;
7519 addr = 0x08;
7520 mask = CPSR_I;
7521 /* The PC already points to the next instruction. */
7522 offset = 0;
7523 break;
7524 case EXCP_BKPT:
7525 env->exception.fsr = 2;
7526 /* Fall through to prefetch abort. */
7527 case EXCP_PREFETCH_ABORT:
7528 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
7529 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
7530 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
7531 env->exception.fsr, (uint32_t)env->exception.vaddress);
7532 new_mode = ARM_CPU_MODE_ABT;
7533 addr = 0x0c;
7534 mask = CPSR_A | CPSR_I;
7535 offset = 4;
7536 break;
7537 case EXCP_DATA_ABORT:
7538 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
7539 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
7540 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
7541 env->exception.fsr,
7542 (uint32_t)env->exception.vaddress);
7543 new_mode = ARM_CPU_MODE_ABT;
7544 addr = 0x10;
7545 mask = CPSR_A | CPSR_I;
7546 offset = 8;
7547 break;
7548 case EXCP_IRQ:
7549 new_mode = ARM_CPU_MODE_IRQ;
7550 addr = 0x18;
7551 /* Disable IRQ and imprecise data aborts. */
7552 mask = CPSR_A | CPSR_I;
7553 offset = 4;
7554 if (env->cp15.scr_el3 & SCR_IRQ) {
7555 /* IRQ routed to monitor mode */
7556 new_mode = ARM_CPU_MODE_MON;
7557 mask |= CPSR_F;
7558 }
7559 break;
7560 case EXCP_FIQ:
7561 new_mode = ARM_CPU_MODE_FIQ;
7562 addr = 0x1c;
7563 /* Disable FIQ, IRQ and imprecise data aborts. */
7564 mask = CPSR_A | CPSR_I | CPSR_F;
7565 if (env->cp15.scr_el3 & SCR_FIQ) {
7566 /* FIQ routed to monitor mode */
7567 new_mode = ARM_CPU_MODE_MON;
7568 }
7569 offset = 4;
7570 break;
7571 case EXCP_VIRQ:
7572 new_mode = ARM_CPU_MODE_IRQ;
7573 addr = 0x18;
7574 /* Disable IRQ and imprecise data aborts. */
7575 mask = CPSR_A | CPSR_I;
7576 offset = 4;
7577 break;
7578 case EXCP_VFIQ:
7579 new_mode = ARM_CPU_MODE_FIQ;
7580 addr = 0x1c;
7581 /* Disable FIQ, IRQ and imprecise data aborts. */
7582 mask = CPSR_A | CPSR_I | CPSR_F;
7583 offset = 4;
7584 break;
7585 case EXCP_SMC:
7586 new_mode = ARM_CPU_MODE_MON;
7587 addr = 0x08;
7588 mask = CPSR_A | CPSR_I | CPSR_F;
7589 offset = 0;
7590 break;
7591 default:
7592 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7593 return; /* Never happens. Keep compiler happy. */
7594 }
7595
7596 if (new_mode == ARM_CPU_MODE_MON) {
7597 addr += env->cp15.mvbar;
7598 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
7599 /* High vectors. When enabled, base address cannot be remapped. */
7600 addr += 0xffff0000;
7601 } else {
7602 /* ARM v7 architectures provide a vector base address register to remap
7603 * the interrupt vector table.
7604 * This register is only followed in non-monitor mode, and is banked.
7605 * Note: only bits 31:5 are valid.
7606 */
7607 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
7608 }
7609
7610 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
7611 env->cp15.scr_el3 &= ~SCR_NS;
7612 }
7613
7614 switch_mode (env, new_mode);
7615 /* For exceptions taken to AArch32 we must clear the SS bit in both
7616 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
7617 */
7618 env->uncached_cpsr &= ~PSTATE_SS;
7619 env->spsr = cpsr_read(env);
7620 /* Clear IT bits. */
7621 env->condexec_bits = 0;
7622 /* Switch to the new mode, and to the correct instruction set. */
7623 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
7624 /* Set new mode endianness */
7625 env->uncached_cpsr &= ~CPSR_E;
7626 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
7627 env->uncached_cpsr |= CPSR_E;
7628 }
7629 env->daif |= mask;
7630 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
7631 * and we should just guard the thumb mode on V4 */
7632 if (arm_feature(env, ARM_FEATURE_V4T)) {
7633 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
7634 }
7635 env->regs[14] = env->regs[15] + offset;
7636 env->regs[15] = addr;
7637 }
7638
7639 /* Handle exception entry to a target EL which is using AArch64 */
7640 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
7641 {
7642 ARMCPU *cpu = ARM_CPU(cs);
7643 CPUARMState *env = &cpu->env;
7644 unsigned int new_el = env->exception.target_el;
7645 target_ulong addr = env->cp15.vbar_el[new_el];
7646 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
7647
7648 if (arm_current_el(env) < new_el) {
7649 /* Entry vector offset depends on whether the implemented EL
7650 * immediately lower than the target level is using AArch32 or AArch64
7651 */
7652 bool is_aa64;
7653
7654 switch (new_el) {
7655 case 3:
7656 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
7657 break;
7658 case 2:
7659 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
7660 break;
7661 case 1:
7662 is_aa64 = is_a64(env);
7663 break;
7664 default:
7665 g_assert_not_reached();
7666 }
7667
7668 if (is_aa64) {
7669 addr += 0x400;
7670 } else {
7671 addr += 0x600;
7672 }
7673 } else if (pstate_read(env) & PSTATE_SP) {
7674 addr += 0x200;
7675 }
7676
7677 switch (cs->exception_index) {
7678 case EXCP_PREFETCH_ABORT:
7679 case EXCP_DATA_ABORT:
7680 env->cp15.far_el[new_el] = env->exception.vaddress;
7681 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
7682 env->cp15.far_el[new_el]);
7683 /* fall through */
7684 case EXCP_BKPT:
7685 case EXCP_UDEF:
7686 case EXCP_SWI:
7687 case EXCP_HVC:
7688 case EXCP_HYP_TRAP:
7689 case EXCP_SMC:
7690 env->cp15.esr_el[new_el] = env->exception.syndrome;
7691 break;
7692 case EXCP_IRQ:
7693 case EXCP_VIRQ:
7694 addr += 0x80;
7695 break;
7696 case EXCP_FIQ:
7697 case EXCP_VFIQ:
7698 addr += 0x100;
7699 break;
7700 case EXCP_SEMIHOST:
7701 qemu_log_mask(CPU_LOG_INT,
7702 "...handling as semihosting call 0x%" PRIx64 "\n",
7703 env->xregs[0]);
7704 env->xregs[0] = do_arm_semihosting(env);
7705 return;
7706 default:
7707 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7708 }
7709
7710 if (is_a64(env)) {
7711 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
7712 aarch64_save_sp(env, arm_current_el(env));
7713 env->elr_el[new_el] = env->pc;
7714 } else {
7715 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
7716 env->elr_el[new_el] = env->regs[15];
7717
7718 aarch64_sync_32_to_64(env);
7719
7720 env->condexec_bits = 0;
7721 }
7722 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
7723 env->elr_el[new_el]);
7724
7725 pstate_write(env, PSTATE_DAIF | new_mode);
7726 env->aarch64 = 1;
7727 aarch64_restore_sp(env, new_el);
7728
7729 env->pc = addr;
7730
7731 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
7732 new_el, env->pc, pstate_read(env));
7733 }
7734
7735 static inline bool check_for_semihosting(CPUState *cs)
7736 {
7737 /* Check whether this exception is a semihosting call; if so
7738 * then handle it and return true; otherwise return false.
7739 */
7740 ARMCPU *cpu = ARM_CPU(cs);
7741 CPUARMState *env = &cpu->env;
7742
7743 if (is_a64(env)) {
7744 if (cs->exception_index == EXCP_SEMIHOST) {
7745 /* This is always the 64-bit semihosting exception.
7746 * The "is this usermode" and "is semihosting enabled"
7747 * checks have been done at translate time.
7748 */
7749 qemu_log_mask(CPU_LOG_INT,
7750 "...handling as semihosting call 0x%" PRIx64 "\n",
7751 env->xregs[0]);
7752 env->xregs[0] = do_arm_semihosting(env);
7753 return true;
7754 }
7755 return false;
7756 } else {
7757 uint32_t imm;
7758
7759 /* Only intercept calls from privileged modes, to provide some
7760 * semblance of security.
7761 */
7762 if (cs->exception_index != EXCP_SEMIHOST &&
7763 (!semihosting_enabled() ||
7764 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
7765 return false;
7766 }
7767
7768 switch (cs->exception_index) {
7769 case EXCP_SEMIHOST:
7770 /* This is always a semihosting call; the "is this usermode"
7771 * and "is semihosting enabled" checks have been done at
7772 * translate time.
7773 */
7774 break;
7775 case EXCP_SWI:
7776 /* Check for semihosting interrupt. */
7777 if (env->thumb) {
7778 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
7779 & 0xff;
7780 if (imm == 0xab) {
7781 break;
7782 }
7783 } else {
7784 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
7785 & 0xffffff;
7786 if (imm == 0x123456) {
7787 break;
7788 }
7789 }
7790 return false;
7791 case EXCP_BKPT:
7792 /* See if this is a semihosting syscall. */
7793 if (env->thumb) {
7794 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
7795 & 0xff;
7796 if (imm == 0xab) {
7797 env->regs[15] += 2;
7798 break;
7799 }
7800 }
7801 return false;
7802 default:
7803 return false;
7804 }
7805
7806 qemu_log_mask(CPU_LOG_INT,
7807 "...handling as semihosting call 0x%x\n",
7808 env->regs[0]);
7809 env->regs[0] = do_arm_semihosting(env);
7810 return true;
7811 }
7812 }
7813
7814 /* Handle a CPU exception for A and R profile CPUs.
7815 * Do any appropriate logging, handle PSCI calls, and then hand off
7816 * to the AArch64-entry or AArch32-entry function depending on the
7817 * target exception level's register width.
7818 */
7819 void arm_cpu_do_interrupt(CPUState *cs)
7820 {
7821 ARMCPU *cpu = ARM_CPU(cs);
7822 CPUARMState *env = &cpu->env;
7823 unsigned int new_el = env->exception.target_el;
7824
7825 assert(!arm_feature(env, ARM_FEATURE_M));
7826
7827 arm_log_exception(cs->exception_index);
7828 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
7829 new_el);
7830 if (qemu_loglevel_mask(CPU_LOG_INT)
7831 && !excp_is_internal(cs->exception_index)) {
7832 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
7833 env->exception.syndrome >> ARM_EL_EC_SHIFT,
7834 env->exception.syndrome);
7835 }
7836
7837 if (arm_is_psci_call(cpu, cs->exception_index)) {
7838 arm_handle_psci_call(cpu);
7839 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
7840 return;
7841 }
7842
7843 /* Semihosting semantics depend on the register width of the
7844 * code that caused the exception, not the target exception level,
7845 * so must be handled here.
7846 */
7847 if (check_for_semihosting(cs)) {
7848 return;
7849 }
7850
7851 assert(!excp_is_internal(cs->exception_index));
7852 if (arm_el_is_aa64(env, new_el)) {
7853 arm_cpu_do_interrupt_aarch64(cs);
7854 } else {
7855 arm_cpu_do_interrupt_aarch32(cs);
7856 }
7857
7858 /* Hooks may change global state so BQL should be held, also the
7859 * BQL needs to be held for any modification of
7860 * cs->interrupt_request.
7861 */
7862 g_assert(qemu_mutex_iothread_locked());
7863
7864 arm_call_el_change_hook(cpu);
7865
7866 if (!kvm_enabled()) {
7867 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
7868 }
7869 }
7870
7871 /* Return the exception level which controls this address translation regime */
7872 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
7873 {
7874 switch (mmu_idx) {
7875 case ARMMMUIdx_S2NS:
7876 case ARMMMUIdx_S1E2:
7877 return 2;
7878 case ARMMMUIdx_S1E3:
7879 return 3;
7880 case ARMMMUIdx_S1SE0:
7881 return arm_el_is_aa64(env, 3) ? 1 : 3;
7882 case ARMMMUIdx_S1SE1:
7883 case ARMMMUIdx_S1NSE0:
7884 case ARMMMUIdx_S1NSE1:
7885 case ARMMMUIdx_MPrivNegPri:
7886 case ARMMMUIdx_MUserNegPri:
7887 case ARMMMUIdx_MPriv:
7888 case ARMMMUIdx_MUser:
7889 case ARMMMUIdx_MSPrivNegPri:
7890 case ARMMMUIdx_MSUserNegPri:
7891 case ARMMMUIdx_MSPriv:
7892 case ARMMMUIdx_MSUser:
7893 return 1;
7894 default:
7895 g_assert_not_reached();
7896 }
7897 }
7898
7899 /* Return the SCTLR value which controls this address translation regime */
7900 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
7901 {
7902 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
7903 }
7904
7905 /* Return true if the specified stage of address translation is disabled */
7906 static inline bool regime_translation_disabled(CPUARMState *env,
7907 ARMMMUIdx mmu_idx)
7908 {
7909 if (arm_feature(env, ARM_FEATURE_M)) {
7910 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
7911 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
7912 case R_V7M_MPU_CTRL_ENABLE_MASK:
7913 /* Enabled, but not for HardFault and NMI */
7914 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
7915 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
7916 /* Enabled for all cases */
7917 return false;
7918 case 0:
7919 default:
7920 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
7921 * we warned about that in armv7m_nvic.c when the guest set it.
7922 */
7923 return true;
7924 }
7925 }
7926
7927 if (mmu_idx == ARMMMUIdx_S2NS) {
7928 return (env->cp15.hcr_el2 & HCR_VM) == 0;
7929 }
7930 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
7931 }
7932
7933 static inline bool regime_translation_big_endian(CPUARMState *env,
7934 ARMMMUIdx mmu_idx)
7935 {
7936 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
7937 }
7938
7939 /* Return the TCR controlling this translation regime */
7940 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
7941 {
7942 if (mmu_idx == ARMMMUIdx_S2NS) {
7943 return &env->cp15.vtcr_el2;
7944 }
7945 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
7946 }
7947
7948 /* Convert a possible stage1+2 MMU index into the appropriate
7949 * stage 1 MMU index
7950 */
7951 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
7952 {
7953 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
7954 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
7955 }
7956 return mmu_idx;
7957 }
7958
7959 /* Returns TBI0 value for current regime el */
7960 uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
7961 {
7962 TCR *tcr;
7963 uint32_t el;
7964
7965 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
7966 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7967 */
7968 mmu_idx = stage_1_mmu_idx(mmu_idx);
7969
7970 tcr = regime_tcr(env, mmu_idx);
7971 el = regime_el(env, mmu_idx);
7972
7973 if (el > 1) {
7974 return extract64(tcr->raw_tcr, 20, 1);
7975 } else {
7976 return extract64(tcr->raw_tcr, 37, 1);
7977 }
7978 }
7979
7980 /* Returns TBI1 value for current regime el */
7981 uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
7982 {
7983 TCR *tcr;
7984 uint32_t el;
7985
7986 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
7987 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7988 */
7989 mmu_idx = stage_1_mmu_idx(mmu_idx);
7990
7991 tcr = regime_tcr(env, mmu_idx);
7992 el = regime_el(env, mmu_idx);
7993
7994 if (el > 1) {
7995 return 0;
7996 } else {
7997 return extract64(tcr->raw_tcr, 38, 1);
7998 }
7999 }
8000
8001 /* Return the TTBR associated with this translation regime */
8002 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8003 int ttbrn)
8004 {
8005 if (mmu_idx == ARMMMUIdx_S2NS) {
8006 return env->cp15.vttbr_el2;
8007 }
8008 if (ttbrn == 0) {
8009 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8010 } else {
8011 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8012 }
8013 }
8014
8015 /* Return true if the translation regime is using LPAE format page tables */
8016 static inline bool regime_using_lpae_format(CPUARMState *env,
8017 ARMMMUIdx mmu_idx)
8018 {
8019 int el = regime_el(env, mmu_idx);
8020 if (el == 2 || arm_el_is_aa64(env, el)) {
8021 return true;
8022 }
8023 if (arm_feature(env, ARM_FEATURE_LPAE)
8024 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8025 return true;
8026 }
8027 return false;
8028 }
8029
8030 /* Returns true if the stage 1 translation regime is using LPAE format page
8031 * tables. Used when raising alignment exceptions, whose FSR changes depending
8032 * on whether the long or short descriptor format is in use. */
8033 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
8034 {
8035 mmu_idx = stage_1_mmu_idx(mmu_idx);
8036
8037 return regime_using_lpae_format(env, mmu_idx);
8038 }
8039
8040 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8041 {
8042 switch (mmu_idx) {
8043 case ARMMMUIdx_S1SE0:
8044 case ARMMMUIdx_S1NSE0:
8045 case ARMMMUIdx_MUser:
8046 case ARMMMUIdx_MSUser:
8047 case ARMMMUIdx_MUserNegPri:
8048 case ARMMMUIdx_MSUserNegPri:
8049 return true;
8050 default:
8051 return false;
8052 case ARMMMUIdx_S12NSE0:
8053 case ARMMMUIdx_S12NSE1:
8054 g_assert_not_reached();
8055 }
8056 }
8057
8058 /* Translate section/page access permissions to page
8059 * R/W protection flags
8060 *
8061 * @env: CPUARMState
8062 * @mmu_idx: MMU index indicating required translation regime
8063 * @ap: The 3-bit access permissions (AP[2:0])
8064 * @domain_prot: The 2-bit domain access permissions
8065 */
8066 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8067 int ap, int domain_prot)
8068 {
8069 bool is_user = regime_is_user(env, mmu_idx);
8070
8071 if (domain_prot == 3) {
8072 return PAGE_READ | PAGE_WRITE;
8073 }
8074
8075 switch (ap) {
8076 case 0:
8077 if (arm_feature(env, ARM_FEATURE_V7)) {
8078 return 0;
8079 }
8080 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8081 case SCTLR_S:
8082 return is_user ? 0 : PAGE_READ;
8083 case SCTLR_R:
8084 return PAGE_READ;
8085 default:
8086 return 0;
8087 }
8088 case 1:
8089 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8090 case 2:
8091 if (is_user) {
8092 return PAGE_READ;
8093 } else {
8094 return PAGE_READ | PAGE_WRITE;
8095 }
8096 case 3:
8097 return PAGE_READ | PAGE_WRITE;
8098 case 4: /* Reserved. */
8099 return 0;
8100 case 5:
8101 return is_user ? 0 : PAGE_READ;
8102 case 6:
8103 return PAGE_READ;
8104 case 7:
8105 if (!arm_feature(env, ARM_FEATURE_V6K)) {
8106 return 0;
8107 }
8108 return PAGE_READ;
8109 default:
8110 g_assert_not_reached();
8111 }
8112 }
8113
8114 /* Translate section/page access permissions to page
8115 * R/W protection flags.
8116 *
8117 * @ap: The 2-bit simple AP (AP[2:1])
8118 * @is_user: TRUE if accessing from PL0
8119 */
8120 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
8121 {
8122 switch (ap) {
8123 case 0:
8124 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8125 case 1:
8126 return PAGE_READ | PAGE_WRITE;
8127 case 2:
8128 return is_user ? 0 : PAGE_READ;
8129 case 3:
8130 return PAGE_READ;
8131 default:
8132 g_assert_not_reached();
8133 }
8134 }
8135
8136 static inline int
8137 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8138 {
8139 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8140 }
8141
8142 /* Translate S2 section/page access permissions to protection flags
8143 *
8144 * @env: CPUARMState
8145 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8146 * @xn: XN (execute-never) bit
8147 */
8148 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8149 {
8150 int prot = 0;
8151
8152 if (s2ap & 1) {
8153 prot |= PAGE_READ;
8154 }
8155 if (s2ap & 2) {
8156 prot |= PAGE_WRITE;
8157 }
8158 if (!xn) {
8159 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8160 prot |= PAGE_EXEC;
8161 }
8162 }
8163 return prot;
8164 }
8165
8166 /* Translate section/page access permissions to protection flags
8167 *
8168 * @env: CPUARMState
8169 * @mmu_idx: MMU index indicating required translation regime
8170 * @is_aa64: TRUE if AArch64
8171 * @ap: The 2-bit simple AP (AP[2:1])
8172 * @ns: NS (non-secure) bit
8173 * @xn: XN (execute-never) bit
8174 * @pxn: PXN (privileged execute-never) bit
8175 */
8176 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8177 int ap, int ns, int xn, int pxn)
8178 {
8179 bool is_user = regime_is_user(env, mmu_idx);
8180 int prot_rw, user_rw;
8181 bool have_wxn;
8182 int wxn = 0;
8183
8184 assert(mmu_idx != ARMMMUIdx_S2NS);
8185
8186 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8187 if (is_user) {
8188 prot_rw = user_rw;
8189 } else {
8190 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8191 }
8192
8193 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8194 return prot_rw;
8195 }
8196
8197 /* TODO have_wxn should be replaced with
8198 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8199 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8200 * compatible processors have EL2, which is required for [U]WXN.
8201 */
8202 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8203
8204 if (have_wxn) {
8205 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8206 }
8207
8208 if (is_aa64) {
8209 switch (regime_el(env, mmu_idx)) {
8210 case 1:
8211 if (!is_user) {
8212 xn = pxn || (user_rw & PAGE_WRITE);
8213 }
8214 break;
8215 case 2:
8216 case 3:
8217 break;
8218 }
8219 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8220 switch (regime_el(env, mmu_idx)) {
8221 case 1:
8222 case 3:
8223 if (is_user) {
8224 xn = xn || !(user_rw & PAGE_READ);
8225 } else {
8226 int uwxn = 0;
8227 if (have_wxn) {
8228 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8229 }
8230 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8231 (uwxn && (user_rw & PAGE_WRITE));
8232 }
8233 break;
8234 case 2:
8235 break;
8236 }
8237 } else {
8238 xn = wxn = 0;
8239 }
8240
8241 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8242 return prot_rw;
8243 }
8244 return prot_rw | PAGE_EXEC;
8245 }
8246
8247 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8248 uint32_t *table, uint32_t address)
8249 {
8250 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
8251 TCR *tcr = regime_tcr(env, mmu_idx);
8252
8253 if (address & tcr->mask) {
8254 if (tcr->raw_tcr & TTBCR_PD1) {
8255 /* Translation table walk disabled for TTBR1 */
8256 return false;
8257 }
8258 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
8259 } else {
8260 if (tcr->raw_tcr & TTBCR_PD0) {
8261 /* Translation table walk disabled for TTBR0 */
8262 return false;
8263 }
8264 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
8265 }
8266 *table |= (address >> 18) & 0x3ffc;
8267 return true;
8268 }
8269
8270 /* Translate a S1 pagetable walk through S2 if needed. */
8271 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8272 hwaddr addr, MemTxAttrs txattrs,
8273 ARMMMUFaultInfo *fi)
8274 {
8275 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8276 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8277 target_ulong s2size;
8278 hwaddr s2pa;
8279 int s2prot;
8280 int ret;
8281
8282 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
8283 &txattrs, &s2prot, &s2size, fi, NULL);
8284 if (ret) {
8285 fi->s2addr = addr;
8286 fi->stage2 = true;
8287 fi->s1ptw = true;
8288 return ~0;
8289 }
8290 addr = s2pa;
8291 }
8292 return addr;
8293 }
8294
8295 /* All loads done in the course of a page table walk go through here.
8296 * TODO: rather than ignoring errors from physical memory reads (which
8297 * are external aborts in ARM terminology) we should propagate this
8298 * error out so that we can turn it into a Data Abort if this walk
8299 * was being done for a CPU load/store or an address translation instruction
8300 * (but not if it was for a debug access).
8301 */
8302 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8303 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8304 {
8305 ARMCPU *cpu = ARM_CPU(cs);
8306 CPUARMState *env = &cpu->env;
8307 MemTxAttrs attrs = {};
8308 AddressSpace *as;
8309
8310 attrs.secure = is_secure;
8311 as = arm_addressspace(cs, attrs);
8312 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8313 if (fi->s1ptw) {
8314 return 0;
8315 }
8316 if (regime_translation_big_endian(env, mmu_idx)) {
8317 return address_space_ldl_be(as, addr, attrs, NULL);
8318 } else {
8319 return address_space_ldl_le(as, addr, attrs, NULL);
8320 }
8321 }
8322
8323 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8324 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8325 {
8326 ARMCPU *cpu = ARM_CPU(cs);
8327 CPUARMState *env = &cpu->env;
8328 MemTxAttrs attrs = {};
8329 AddressSpace *as;
8330
8331 attrs.secure = is_secure;
8332 as = arm_addressspace(cs, attrs);
8333 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8334 if (fi->s1ptw) {
8335 return 0;
8336 }
8337 if (regime_translation_big_endian(env, mmu_idx)) {
8338 return address_space_ldq_be(as, addr, attrs, NULL);
8339 } else {
8340 return address_space_ldq_le(as, addr, attrs, NULL);
8341 }
8342 }
8343
8344 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
8345 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8346 hwaddr *phys_ptr, int *prot,
8347 target_ulong *page_size,
8348 ARMMMUFaultInfo *fi)
8349 {
8350 CPUState *cs = CPU(arm_env_get_cpu(env));
8351 int level = 1;
8352 uint32_t table;
8353 uint32_t desc;
8354 int type;
8355 int ap;
8356 int domain = 0;
8357 int domain_prot;
8358 hwaddr phys_addr;
8359 uint32_t dacr;
8360
8361 /* Pagetable walk. */
8362 /* Lookup l1 descriptor. */
8363 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8364 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8365 fi->type = ARMFault_Translation;
8366 goto do_fault;
8367 }
8368 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8369 mmu_idx, fi);
8370 type = (desc & 3);
8371 domain = (desc >> 5) & 0x0f;
8372 if (regime_el(env, mmu_idx) == 1) {
8373 dacr = env->cp15.dacr_ns;
8374 } else {
8375 dacr = env->cp15.dacr_s;
8376 }
8377 domain_prot = (dacr >> (domain * 2)) & 3;
8378 if (type == 0) {
8379 /* Section translation fault. */
8380 fi->type = ARMFault_Translation;
8381 goto do_fault;
8382 }
8383 if (type != 2) {
8384 level = 2;
8385 }
8386 if (domain_prot == 0 || domain_prot == 2) {
8387 fi->type = ARMFault_Domain;
8388 goto do_fault;
8389 }
8390 if (type == 2) {
8391 /* 1Mb section. */
8392 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8393 ap = (desc >> 10) & 3;
8394 *page_size = 1024 * 1024;
8395 } else {
8396 /* Lookup l2 entry. */
8397 if (type == 1) {
8398 /* Coarse pagetable. */
8399 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8400 } else {
8401 /* Fine pagetable. */
8402 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8403 }
8404 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8405 mmu_idx, fi);
8406 switch (desc & 3) {
8407 case 0: /* Page translation fault. */
8408 fi->type = ARMFault_Translation;
8409 goto do_fault;
8410 case 1: /* 64k page. */
8411 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8412 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
8413 *page_size = 0x10000;
8414 break;
8415 case 2: /* 4k page. */
8416 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8417 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
8418 *page_size = 0x1000;
8419 break;
8420 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
8421 if (type == 1) {
8422 /* ARMv6/XScale extended small page format */
8423 if (arm_feature(env, ARM_FEATURE_XSCALE)
8424 || arm_feature(env, ARM_FEATURE_V6)) {
8425 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8426 *page_size = 0x1000;
8427 } else {
8428 /* UNPREDICTABLE in ARMv5; we choose to take a
8429 * page translation fault.
8430 */
8431 fi->type = ARMFault_Translation;
8432 goto do_fault;
8433 }
8434 } else {
8435 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
8436 *page_size = 0x400;
8437 }
8438 ap = (desc >> 4) & 3;
8439 break;
8440 default:
8441 /* Never happens, but compiler isn't smart enough to tell. */
8442 abort();
8443 }
8444 }
8445 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8446 *prot |= *prot ? PAGE_EXEC : 0;
8447 if (!(*prot & (1 << access_type))) {
8448 /* Access permission fault. */
8449 fi->type = ARMFault_Permission;
8450 goto do_fault;
8451 }
8452 *phys_ptr = phys_addr;
8453 return false;
8454 do_fault:
8455 fi->domain = domain;
8456 fi->level = level;
8457 return true;
8458 }
8459
8460 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
8461 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8462 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
8463 target_ulong *page_size, ARMMMUFaultInfo *fi)
8464 {
8465 CPUState *cs = CPU(arm_env_get_cpu(env));
8466 int level = 1;
8467 uint32_t table;
8468 uint32_t desc;
8469 uint32_t xn;
8470 uint32_t pxn = 0;
8471 int type;
8472 int ap;
8473 int domain = 0;
8474 int domain_prot;
8475 hwaddr phys_addr;
8476 uint32_t dacr;
8477 bool ns;
8478
8479 /* Pagetable walk. */
8480 /* Lookup l1 descriptor. */
8481 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8482 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8483 fi->type = ARMFault_Translation;
8484 goto do_fault;
8485 }
8486 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8487 mmu_idx, fi);
8488 type = (desc & 3);
8489 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8490 /* Section translation fault, or attempt to use the encoding
8491 * which is Reserved on implementations without PXN.
8492 */
8493 fi->type = ARMFault_Translation;
8494 goto do_fault;
8495 }
8496 if ((type == 1) || !(desc & (1 << 18))) {
8497 /* Page or Section. */
8498 domain = (desc >> 5) & 0x0f;
8499 }
8500 if (regime_el(env, mmu_idx) == 1) {
8501 dacr = env->cp15.dacr_ns;
8502 } else {
8503 dacr = env->cp15.dacr_s;
8504 }
8505 if (type == 1) {
8506 level = 2;
8507 }
8508 domain_prot = (dacr >> (domain * 2)) & 3;
8509 if (domain_prot == 0 || domain_prot == 2) {
8510 /* Section or Page domain fault */
8511 fi->type = ARMFault_Domain;
8512 goto do_fault;
8513 }
8514 if (type != 1) {
8515 if (desc & (1 << 18)) {
8516 /* Supersection. */
8517 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
8518 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8519 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
8520 *page_size = 0x1000000;
8521 } else {
8522 /* Section. */
8523 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8524 *page_size = 0x100000;
8525 }
8526 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8527 xn = desc & (1 << 4);
8528 pxn = desc & 1;
8529 ns = extract32(desc, 19, 1);
8530 } else {
8531 if (arm_feature(env, ARM_FEATURE_PXN)) {
8532 pxn = (desc >> 2) & 1;
8533 }
8534 ns = extract32(desc, 3, 1);
8535 /* Lookup l2 entry. */
8536 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8537 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8538 mmu_idx, fi);
8539 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8540 switch (desc & 3) {
8541 case 0: /* Page translation fault. */
8542 fi->type = ARMFault_Translation;
8543 goto do_fault;
8544 case 1: /* 64k page. */
8545 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8546 xn = desc & (1 << 15);
8547 *page_size = 0x10000;
8548 break;
8549 case 2: case 3: /* 4k page. */
8550 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8551 xn = desc & 1;
8552 *page_size = 0x1000;
8553 break;
8554 default:
8555 /* Never happens, but compiler isn't smart enough to tell. */
8556 abort();
8557 }
8558 }
8559 if (domain_prot == 3) {
8560 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8561 } else {
8562 if (pxn && !regime_is_user(env, mmu_idx)) {
8563 xn = 1;
8564 }
8565 if (xn && access_type == MMU_INST_FETCH) {
8566 fi->type = ARMFault_Permission;
8567 goto do_fault;
8568 }
8569
8570 if (arm_feature(env, ARM_FEATURE_V6K) &&
8571 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
8572 /* The simplified model uses AP[0] as an access control bit. */
8573 if ((ap & 1) == 0) {
8574 /* Access flag fault. */
8575 fi->type = ARMFault_AccessFlag;
8576 goto do_fault;
8577 }
8578 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
8579 } else {
8580 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8581 }
8582 if (*prot && !xn) {
8583 *prot |= PAGE_EXEC;
8584 }
8585 if (!(*prot & (1 << access_type))) {
8586 /* Access permission fault. */
8587 fi->type = ARMFault_Permission;
8588 goto do_fault;
8589 }
8590 }
8591 if (ns) {
8592 /* The NS bit will (as required by the architecture) have no effect if
8593 * the CPU doesn't support TZ or this is a non-secure translation
8594 * regime, because the attribute will already be non-secure.
8595 */
8596 attrs->secure = false;
8597 }
8598 *phys_ptr = phys_addr;
8599 return false;
8600 do_fault:
8601 fi->domain = domain;
8602 fi->level = level;
8603 return true;
8604 }
8605
8606 /*
8607 * check_s2_mmu_setup
8608 * @cpu: ARMCPU
8609 * @is_aa64: True if the translation regime is in AArch64 state
8610 * @startlevel: Suggested starting level
8611 * @inputsize: Bitsize of IPAs
8612 * @stride: Page-table stride (See the ARM ARM)
8613 *
8614 * Returns true if the suggested S2 translation parameters are OK and
8615 * false otherwise.
8616 */
8617 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
8618 int inputsize, int stride)
8619 {
8620 const int grainsize = stride + 3;
8621 int startsizecheck;
8622
8623 /* Negative levels are never allowed. */
8624 if (level < 0) {
8625 return false;
8626 }
8627
8628 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
8629 if (startsizecheck < 1 || startsizecheck > stride + 4) {
8630 return false;
8631 }
8632
8633 if (is_aa64) {
8634 CPUARMState *env = &cpu->env;
8635 unsigned int pamax = arm_pamax(cpu);
8636
8637 switch (stride) {
8638 case 13: /* 64KB Pages. */
8639 if (level == 0 || (level == 1 && pamax <= 42)) {
8640 return false;
8641 }
8642 break;
8643 case 11: /* 16KB Pages. */
8644 if (level == 0 || (level == 1 && pamax <= 40)) {
8645 return false;
8646 }
8647 break;
8648 case 9: /* 4KB Pages. */
8649 if (level == 0 && pamax <= 42) {
8650 return false;
8651 }
8652 break;
8653 default:
8654 g_assert_not_reached();
8655 }
8656
8657 /* Inputsize checks. */
8658 if (inputsize > pamax &&
8659 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
8660 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
8661 return false;
8662 }
8663 } else {
8664 /* AArch32 only supports 4KB pages. Assert on that. */
8665 assert(stride == 9);
8666
8667 if (level == 0) {
8668 return false;
8669 }
8670 }
8671 return true;
8672 }
8673
8674 /* Translate from the 4-bit stage 2 representation of
8675 * memory attributes (without cache-allocation hints) to
8676 * the 8-bit representation of the stage 1 MAIR registers
8677 * (which includes allocation hints).
8678 *
8679 * ref: shared/translation/attrs/S2AttrDecode()
8680 * .../S2ConvertAttrsHints()
8681 */
8682 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
8683 {
8684 uint8_t hiattr = extract32(s2attrs, 2, 2);
8685 uint8_t loattr = extract32(s2attrs, 0, 2);
8686 uint8_t hihint = 0, lohint = 0;
8687
8688 if (hiattr != 0) { /* normal memory */
8689 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
8690 hiattr = loattr = 1; /* non-cacheable */
8691 } else {
8692 if (hiattr != 1) { /* Write-through or write-back */
8693 hihint = 3; /* RW allocate */
8694 }
8695 if (loattr != 1) { /* Write-through or write-back */
8696 lohint = 3; /* RW allocate */
8697 }
8698 }
8699 }
8700
8701 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
8702 }
8703
8704 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
8705 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8706 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
8707 target_ulong *page_size_ptr,
8708 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
8709 {
8710 ARMCPU *cpu = arm_env_get_cpu(env);
8711 CPUState *cs = CPU(cpu);
8712 /* Read an LPAE long-descriptor translation table. */
8713 ARMFaultType fault_type = ARMFault_Translation;
8714 uint32_t level;
8715 uint32_t epd = 0;
8716 int32_t t0sz, t1sz;
8717 uint32_t tg;
8718 uint64_t ttbr;
8719 int ttbr_select;
8720 hwaddr descaddr, indexmask, indexmask_grainsize;
8721 uint32_t tableattrs;
8722 target_ulong page_size;
8723 uint32_t attrs;
8724 int32_t stride = 9;
8725 int32_t addrsize;
8726 int inputsize;
8727 int32_t tbi = 0;
8728 TCR *tcr = regime_tcr(env, mmu_idx);
8729 int ap, ns, xn, pxn;
8730 uint32_t el = regime_el(env, mmu_idx);
8731 bool ttbr1_valid = true;
8732 uint64_t descaddrmask;
8733 bool aarch64 = arm_el_is_aa64(env, el);
8734
8735 /* TODO:
8736 * This code does not handle the different format TCR for VTCR_EL2.
8737 * This code also does not support shareability levels.
8738 * Attribute and permission bit handling should also be checked when adding
8739 * support for those page table walks.
8740 */
8741 if (aarch64) {
8742 level = 0;
8743 addrsize = 64;
8744 if (el > 1) {
8745 if (mmu_idx != ARMMMUIdx_S2NS) {
8746 tbi = extract64(tcr->raw_tcr, 20, 1);
8747 }
8748 } else {
8749 if (extract64(address, 55, 1)) {
8750 tbi = extract64(tcr->raw_tcr, 38, 1);
8751 } else {
8752 tbi = extract64(tcr->raw_tcr, 37, 1);
8753 }
8754 }
8755 tbi *= 8;
8756
8757 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
8758 * invalid.
8759 */
8760 if (el > 1) {
8761 ttbr1_valid = false;
8762 }
8763 } else {
8764 level = 1;
8765 addrsize = 32;
8766 /* There is no TTBR1 for EL2 */
8767 if (el == 2) {
8768 ttbr1_valid = false;
8769 }
8770 }
8771
8772 /* Determine whether this address is in the region controlled by
8773 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
8774 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
8775 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
8776 */
8777 if (aarch64) {
8778 /* AArch64 translation. */
8779 t0sz = extract32(tcr->raw_tcr, 0, 6);
8780 t0sz = MIN(t0sz, 39);
8781 t0sz = MAX(t0sz, 16);
8782 } else if (mmu_idx != ARMMMUIdx_S2NS) {
8783 /* AArch32 stage 1 translation. */
8784 t0sz = extract32(tcr->raw_tcr, 0, 3);
8785 } else {
8786 /* AArch32 stage 2 translation. */
8787 bool sext = extract32(tcr->raw_tcr, 4, 1);
8788 bool sign = extract32(tcr->raw_tcr, 3, 1);
8789 /* Address size is 40-bit for a stage 2 translation,
8790 * and t0sz can be negative (from -8 to 7),
8791 * so we need to adjust it to use the TTBR selecting logic below.
8792 */
8793 addrsize = 40;
8794 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
8795
8796 /* If the sign-extend bit is not the same as t0sz[3], the result
8797 * is unpredictable. Flag this as a guest error. */
8798 if (sign != sext) {
8799 qemu_log_mask(LOG_GUEST_ERROR,
8800 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
8801 }
8802 }
8803 t1sz = extract32(tcr->raw_tcr, 16, 6);
8804 if (aarch64) {
8805 t1sz = MIN(t1sz, 39);
8806 t1sz = MAX(t1sz, 16);
8807 }
8808 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
8809 /* there is a ttbr0 region and we are in it (high bits all zero) */
8810 ttbr_select = 0;
8811 } else if (ttbr1_valid && t1sz &&
8812 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
8813 /* there is a ttbr1 region and we are in it (high bits all one) */
8814 ttbr_select = 1;
8815 } else if (!t0sz) {
8816 /* ttbr0 region is "everything not in the ttbr1 region" */
8817 ttbr_select = 0;
8818 } else if (!t1sz && ttbr1_valid) {
8819 /* ttbr1 region is "everything not in the ttbr0 region" */
8820 ttbr_select = 1;
8821 } else {
8822 /* in the gap between the two regions, this is a Translation fault */
8823 fault_type = ARMFault_Translation;
8824 goto do_fault;
8825 }
8826
8827 /* Note that QEMU ignores shareability and cacheability attributes,
8828 * so we don't need to do anything with the SH, ORGN, IRGN fields
8829 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
8830 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
8831 * implement any ASID-like capability so we can ignore it (instead
8832 * we will always flush the TLB any time the ASID is changed).
8833 */
8834 if (ttbr_select == 0) {
8835 ttbr = regime_ttbr(env, mmu_idx, 0);
8836 if (el < 2) {
8837 epd = extract32(tcr->raw_tcr, 7, 1);
8838 }
8839 inputsize = addrsize - t0sz;
8840
8841 tg = extract32(tcr->raw_tcr, 14, 2);
8842 if (tg == 1) { /* 64KB pages */
8843 stride = 13;
8844 }
8845 if (tg == 2) { /* 16KB pages */
8846 stride = 11;
8847 }
8848 } else {
8849 /* We should only be here if TTBR1 is valid */
8850 assert(ttbr1_valid);
8851
8852 ttbr = regime_ttbr(env, mmu_idx, 1);
8853 epd = extract32(tcr->raw_tcr, 23, 1);
8854 inputsize = addrsize - t1sz;
8855
8856 tg = extract32(tcr->raw_tcr, 30, 2);
8857 if (tg == 3) { /* 64KB pages */
8858 stride = 13;
8859 }
8860 if (tg == 1) { /* 16KB pages */
8861 stride = 11;
8862 }
8863 }
8864
8865 /* Here we should have set up all the parameters for the translation:
8866 * inputsize, ttbr, epd, stride, tbi
8867 */
8868
8869 if (epd) {
8870 /* Translation table walk disabled => Translation fault on TLB miss
8871 * Note: This is always 0 on 64-bit EL2 and EL3.
8872 */
8873 goto do_fault;
8874 }
8875
8876 if (mmu_idx != ARMMMUIdx_S2NS) {
8877 /* The starting level depends on the virtual address size (which can
8878 * be up to 48 bits) and the translation granule size. It indicates
8879 * the number of strides (stride bits at a time) needed to
8880 * consume the bits of the input address. In the pseudocode this is:
8881 * level = 4 - RoundUp((inputsize - grainsize) / stride)
8882 * where their 'inputsize' is our 'inputsize', 'grainsize' is
8883 * our 'stride + 3' and 'stride' is our 'stride'.
8884 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
8885 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
8886 * = 4 - (inputsize - 4) / stride;
8887 */
8888 level = 4 - (inputsize - 4) / stride;
8889 } else {
8890 /* For stage 2 translations the starting level is specified by the
8891 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
8892 */
8893 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
8894 uint32_t startlevel;
8895 bool ok;
8896
8897 if (!aarch64 || stride == 9) {
8898 /* AArch32 or 4KB pages */
8899 startlevel = 2 - sl0;
8900 } else {
8901 /* 16KB or 64KB pages */
8902 startlevel = 3 - sl0;
8903 }
8904
8905 /* Check that the starting level is valid. */
8906 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
8907 inputsize, stride);
8908 if (!ok) {
8909 fault_type = ARMFault_Translation;
8910 goto do_fault;
8911 }
8912 level = startlevel;
8913 }
8914
8915 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
8916 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
8917
8918 /* Now we can extract the actual base address from the TTBR */
8919 descaddr = extract64(ttbr, 0, 48);
8920 descaddr &= ~indexmask;
8921
8922 /* The address field in the descriptor goes up to bit 39 for ARMv7
8923 * but up to bit 47 for ARMv8, but we use the descaddrmask
8924 * up to bit 39 for AArch32, because we don't need other bits in that case
8925 * to construct next descriptor address (anyway they should be all zeroes).
8926 */
8927 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
8928 ~indexmask_grainsize;
8929
8930 /* Secure accesses start with the page table in secure memory and
8931 * can be downgraded to non-secure at any step. Non-secure accesses
8932 * remain non-secure. We implement this by just ORing in the NSTable/NS
8933 * bits at each step.
8934 */
8935 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
8936 for (;;) {
8937 uint64_t descriptor;
8938 bool nstable;
8939
8940 descaddr |= (address >> (stride * (4 - level))) & indexmask;
8941 descaddr &= ~7ULL;
8942 nstable = extract32(tableattrs, 4, 1);
8943 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
8944 if (fi->s1ptw) {
8945 goto do_fault;
8946 }
8947
8948 if (!(descriptor & 1) ||
8949 (!(descriptor & 2) && (level == 3))) {
8950 /* Invalid, or the Reserved level 3 encoding */
8951 goto do_fault;
8952 }
8953 descaddr = descriptor & descaddrmask;
8954
8955 if ((descriptor & 2) && (level < 3)) {
8956 /* Table entry. The top five bits are attributes which may
8957 * propagate down through lower levels of the table (and
8958 * which are all arranged so that 0 means "no effect", so
8959 * we can gather them up by ORing in the bits at each level).
8960 */
8961 tableattrs |= extract64(descriptor, 59, 5);
8962 level++;
8963 indexmask = indexmask_grainsize;
8964 continue;
8965 }
8966 /* Block entry at level 1 or 2, or page entry at level 3.
8967 * These are basically the same thing, although the number
8968 * of bits we pull in from the vaddr varies.
8969 */
8970 page_size = (1ULL << ((stride * (4 - level)) + 3));
8971 descaddr |= (address & (page_size - 1));
8972 /* Extract attributes from the descriptor */
8973 attrs = extract64(descriptor, 2, 10)
8974 | (extract64(descriptor, 52, 12) << 10);
8975
8976 if (mmu_idx == ARMMMUIdx_S2NS) {
8977 /* Stage 2 table descriptors do not include any attribute fields */
8978 break;
8979 }
8980 /* Merge in attributes from table descriptors */
8981 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
8982 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
8983 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
8984 * means "force PL1 access only", which means forcing AP[1] to 0.
8985 */
8986 if (extract32(tableattrs, 2, 1)) {
8987 attrs &= ~(1 << 4);
8988 }
8989 attrs |= nstable << 3; /* NS */
8990 break;
8991 }
8992 /* Here descaddr is the final physical address, and attributes
8993 * are all in attrs.
8994 */
8995 fault_type = ARMFault_AccessFlag;
8996 if ((attrs & (1 << 8)) == 0) {
8997 /* Access flag */
8998 goto do_fault;
8999 }
9000
9001 ap = extract32(attrs, 4, 2);
9002 xn = extract32(attrs, 12, 1);
9003
9004 if (mmu_idx == ARMMMUIdx_S2NS) {
9005 ns = true;
9006 *prot = get_S2prot(env, ap, xn);
9007 } else {
9008 ns = extract32(attrs, 3, 1);
9009 pxn = extract32(attrs, 11, 1);
9010 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
9011 }
9012
9013 fault_type = ARMFault_Permission;
9014 if (!(*prot & (1 << access_type))) {
9015 goto do_fault;
9016 }
9017
9018 if (ns) {
9019 /* The NS bit will (as required by the architecture) have no effect if
9020 * the CPU doesn't support TZ or this is a non-secure translation
9021 * regime, because the attribute will already be non-secure.
9022 */
9023 txattrs->secure = false;
9024 }
9025
9026 if (cacheattrs != NULL) {
9027 if (mmu_idx == ARMMMUIdx_S2NS) {
9028 cacheattrs->attrs = convert_stage2_attrs(env,
9029 extract32(attrs, 0, 4));
9030 } else {
9031 /* Index into MAIR registers for cache attributes */
9032 uint8_t attrindx = extract32(attrs, 0, 3);
9033 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9034 assert(attrindx <= 7);
9035 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9036 }
9037 cacheattrs->shareability = extract32(attrs, 6, 2);
9038 }
9039
9040 *phys_ptr = descaddr;
9041 *page_size_ptr = page_size;
9042 return false;
9043
9044 do_fault:
9045 fi->type = fault_type;
9046 fi->level = level;
9047 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9048 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
9049 return true;
9050 }
9051
9052 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9053 ARMMMUIdx mmu_idx,
9054 int32_t address, int *prot)
9055 {
9056 if (!arm_feature(env, ARM_FEATURE_M)) {
9057 *prot = PAGE_READ | PAGE_WRITE;
9058 switch (address) {
9059 case 0xF0000000 ... 0xFFFFFFFF:
9060 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9061 /* hivecs execing is ok */
9062 *prot |= PAGE_EXEC;
9063 }
9064 break;
9065 case 0x00000000 ... 0x7FFFFFFF:
9066 *prot |= PAGE_EXEC;
9067 break;
9068 }
9069 } else {
9070 /* Default system address map for M profile cores.
9071 * The architecture specifies which regions are execute-never;
9072 * at the MPU level no other checks are defined.
9073 */
9074 switch (address) {
9075 case 0x00000000 ... 0x1fffffff: /* ROM */
9076 case 0x20000000 ... 0x3fffffff: /* SRAM */
9077 case 0x60000000 ... 0x7fffffff: /* RAM */
9078 case 0x80000000 ... 0x9fffffff: /* RAM */
9079 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9080 break;
9081 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9082 case 0xa0000000 ... 0xbfffffff: /* Device */
9083 case 0xc0000000 ... 0xdfffffff: /* Device */
9084 case 0xe0000000 ... 0xffffffff: /* System */
9085 *prot = PAGE_READ | PAGE_WRITE;
9086 break;
9087 default:
9088 g_assert_not_reached();
9089 }
9090 }
9091 }
9092
9093 static bool pmsav7_use_background_region(ARMCPU *cpu,
9094 ARMMMUIdx mmu_idx, bool is_user)
9095 {
9096 /* Return true if we should use the default memory map as a
9097 * "background" region if there are no hits against any MPU regions.
9098 */
9099 CPUARMState *env = &cpu->env;
9100
9101 if (is_user) {
9102 return false;
9103 }
9104
9105 if (arm_feature(env, ARM_FEATURE_M)) {
9106 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9107 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
9108 } else {
9109 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9110 }
9111 }
9112
9113 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9114 {
9115 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9116 return arm_feature(env, ARM_FEATURE_M) &&
9117 extract32(address, 20, 12) == 0xe00;
9118 }
9119
9120 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9121 {
9122 /* True if address is in the M profile system region
9123 * 0xe0000000 - 0xffffffff
9124 */
9125 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9126 }
9127
9128 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
9129 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9130 hwaddr *phys_ptr, int *prot,
9131 ARMMMUFaultInfo *fi)
9132 {
9133 ARMCPU *cpu = arm_env_get_cpu(env);
9134 int n;
9135 bool is_user = regime_is_user(env, mmu_idx);
9136
9137 *phys_ptr = address;
9138 *prot = 0;
9139
9140 if (regime_translation_disabled(env, mmu_idx) ||
9141 m_is_ppb_region(env, address)) {
9142 /* MPU disabled or M profile PPB access: use default memory map.
9143 * The other case which uses the default memory map in the
9144 * v7M ARM ARM pseudocode is exception vector reads from the vector
9145 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9146 * which always does a direct read using address_space_ldl(), rather
9147 * than going via this function, so we don't need to check that here.
9148 */
9149 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9150 } else { /* MPU enabled */
9151 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9152 /* region search */
9153 uint32_t base = env->pmsav7.drbar[n];
9154 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9155 uint32_t rmask;
9156 bool srdis = false;
9157
9158 if (!(env->pmsav7.drsr[n] & 0x1)) {
9159 continue;
9160 }
9161
9162 if (!rsize) {
9163 qemu_log_mask(LOG_GUEST_ERROR,
9164 "DRSR[%d]: Rsize field cannot be 0\n", n);
9165 continue;
9166 }
9167 rsize++;
9168 rmask = (1ull << rsize) - 1;
9169
9170 if (base & rmask) {
9171 qemu_log_mask(LOG_GUEST_ERROR,
9172 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9173 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9174 n, base, rmask);
9175 continue;
9176 }
9177
9178 if (address < base || address > base + rmask) {
9179 continue;
9180 }
9181
9182 /* Region matched */
9183
9184 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9185 int i, snd;
9186 uint32_t srdis_mask;
9187
9188 rsize -= 3; /* sub region size (power of 2) */
9189 snd = ((address - base) >> rsize) & 0x7;
9190 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9191
9192 srdis_mask = srdis ? 0x3 : 0x0;
9193 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9194 /* This will check in groups of 2, 4 and then 8, whether
9195 * the subregion bits are consistent. rsize is incremented
9196 * back up to give the region size, considering consistent
9197 * adjacent subregions as one region. Stop testing if rsize
9198 * is already big enough for an entire QEMU page.
9199 */
9200 int snd_rounded = snd & ~(i - 1);
9201 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9202 snd_rounded + 8, i);
9203 if (srdis_mask ^ srdis_multi) {
9204 break;
9205 }
9206 srdis_mask = (srdis_mask << i) | srdis_mask;
9207 rsize++;
9208 }
9209 }
9210 if (rsize < TARGET_PAGE_BITS) {
9211 qemu_log_mask(LOG_UNIMP,
9212 "DRSR[%d]: No support for MPU (sub)region "
9213 "alignment of %" PRIu32 " bits. Minimum is %d\n",
9214 n, rsize, TARGET_PAGE_BITS);
9215 continue;
9216 }
9217 if (srdis) {
9218 continue;
9219 }
9220 break;
9221 }
9222
9223 if (n == -1) { /* no hits */
9224 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9225 /* background fault */
9226 fi->type = ARMFault_Background;
9227 return true;
9228 }
9229 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9230 } else { /* a MPU hit! */
9231 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
9232 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9233
9234 if (m_is_system_region(env, address)) {
9235 /* System space is always execute never */
9236 xn = 1;
9237 }
9238
9239 if (is_user) { /* User mode AP bit decoding */
9240 switch (ap) {
9241 case 0:
9242 case 1:
9243 case 5:
9244 break; /* no access */
9245 case 3:
9246 *prot |= PAGE_WRITE;
9247 /* fall through */
9248 case 2:
9249 case 6:
9250 *prot |= PAGE_READ | PAGE_EXEC;
9251 break;
9252 default:
9253 qemu_log_mask(LOG_GUEST_ERROR,
9254 "DRACR[%d]: Bad value for AP bits: 0x%"
9255 PRIx32 "\n", n, ap);
9256 }
9257 } else { /* Priv. mode AP bits decoding */
9258 switch (ap) {
9259 case 0:
9260 break; /* no access */
9261 case 1:
9262 case 2:
9263 case 3:
9264 *prot |= PAGE_WRITE;
9265 /* fall through */
9266 case 5:
9267 case 6:
9268 *prot |= PAGE_READ | PAGE_EXEC;
9269 break;
9270 default:
9271 qemu_log_mask(LOG_GUEST_ERROR,
9272 "DRACR[%d]: Bad value for AP bits: 0x%"
9273 PRIx32 "\n", n, ap);
9274 }
9275 }
9276
9277 /* execute never */
9278 if (xn) {
9279 *prot &= ~PAGE_EXEC;
9280 }
9281 }
9282 }
9283
9284 fi->type = ARMFault_Permission;
9285 fi->level = 1;
9286 return !(*prot & (1 << access_type));
9287 }
9288
9289 static bool v8m_is_sau_exempt(CPUARMState *env,
9290 uint32_t address, MMUAccessType access_type)
9291 {
9292 /* The architecture specifies that certain address ranges are
9293 * exempt from v8M SAU/IDAU checks.
9294 */
9295 return
9296 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9297 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9298 (address >= 0xe000e000 && address <= 0xe000efff) ||
9299 (address >= 0xe002e000 && address <= 0xe002efff) ||
9300 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9301 (address >= 0xe00ff000 && address <= 0xe00fffff);
9302 }
9303
9304 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9305 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9306 V8M_SAttributes *sattrs)
9307 {
9308 /* Look up the security attributes for this address. Compare the
9309 * pseudocode SecurityCheck() function.
9310 * We assume the caller has zero-initialized *sattrs.
9311 */
9312 ARMCPU *cpu = arm_env_get_cpu(env);
9313 int r;
9314
9315 /* TODO: implement IDAU */
9316
9317 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9318 /* 0xf0000000..0xffffffff is always S for insn fetches */
9319 return;
9320 }
9321
9322 if (v8m_is_sau_exempt(env, address, access_type)) {
9323 sattrs->ns = !regime_is_secure(env, mmu_idx);
9324 return;
9325 }
9326
9327 switch (env->sau.ctrl & 3) {
9328 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9329 break;
9330 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9331 sattrs->ns = true;
9332 break;
9333 default: /* SAU.ENABLE == 1 */
9334 for (r = 0; r < cpu->sau_sregion; r++) {
9335 if (env->sau.rlar[r] & 1) {
9336 uint32_t base = env->sau.rbar[r] & ~0x1f;
9337 uint32_t limit = env->sau.rlar[r] | 0x1f;
9338
9339 if (base <= address && limit >= address) {
9340 if (sattrs->srvalid) {
9341 /* If we hit in more than one region then we must report
9342 * as Secure, not NS-Callable, with no valid region
9343 * number info.
9344 */
9345 sattrs->ns = false;
9346 sattrs->nsc = false;
9347 sattrs->sregion = 0;
9348 sattrs->srvalid = false;
9349 break;
9350 } else {
9351 if (env->sau.rlar[r] & 2) {
9352 sattrs->nsc = true;
9353 } else {
9354 sattrs->ns = true;
9355 }
9356 sattrs->srvalid = true;
9357 sattrs->sregion = r;
9358 }
9359 }
9360 }
9361 }
9362
9363 /* TODO when we support the IDAU then it may override the result here */
9364 break;
9365 }
9366 }
9367
9368 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9369 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9370 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9371 int *prot, ARMMMUFaultInfo *fi, uint32_t *mregion)
9372 {
9373 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9374 * that a full phys-to-virt translation does).
9375 * mregion is (if not NULL) set to the region number which matched,
9376 * or -1 if no region number is returned (MPU off, address did not
9377 * hit a region, address hit in multiple regions).
9378 */
9379 ARMCPU *cpu = arm_env_get_cpu(env);
9380 bool is_user = regime_is_user(env, mmu_idx);
9381 uint32_t secure = regime_is_secure(env, mmu_idx);
9382 int n;
9383 int matchregion = -1;
9384 bool hit = false;
9385
9386 *phys_ptr = address;
9387 *prot = 0;
9388 if (mregion) {
9389 *mregion = -1;
9390 }
9391
9392 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9393 * was an exception vector read from the vector table (which is always
9394 * done using the default system address map), because those accesses
9395 * are done in arm_v7m_load_vector(), which always does a direct
9396 * read using address_space_ldl(), rather than going via this function.
9397 */
9398 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9399 hit = true;
9400 } else if (m_is_ppb_region(env, address)) {
9401 hit = true;
9402 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9403 hit = true;
9404 } else {
9405 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9406 /* region search */
9407 /* Note that the base address is bits [31:5] from the register
9408 * with bits [4:0] all zeroes, but the limit address is bits
9409 * [31:5] from the register with bits [4:0] all ones.
9410 */
9411 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9412 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
9413
9414 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
9415 /* Region disabled */
9416 continue;
9417 }
9418
9419 if (address < base || address > limit) {
9420 continue;
9421 }
9422
9423 if (hit) {
9424 /* Multiple regions match -- always a failure (unlike
9425 * PMSAv7 where highest-numbered-region wins)
9426 */
9427 fi->type = ARMFault_Permission;
9428 fi->level = 1;
9429 return true;
9430 }
9431
9432 matchregion = n;
9433 hit = true;
9434
9435 if (base & ~TARGET_PAGE_MASK) {
9436 qemu_log_mask(LOG_UNIMP,
9437 "MPU_RBAR[%d]: No support for MPU region base"
9438 "address of 0x%" PRIx32 ". Minimum alignment is "
9439 "%d\n",
9440 n, base, TARGET_PAGE_BITS);
9441 continue;
9442 }
9443 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9444 qemu_log_mask(LOG_UNIMP,
9445 "MPU_RBAR[%d]: No support for MPU region limit"
9446 "address of 0x%" PRIx32 ". Minimum alignment is "
9447 "%d\n",
9448 n, limit, TARGET_PAGE_BITS);
9449 continue;
9450 }
9451 }
9452 }
9453
9454 if (!hit) {
9455 /* background fault */
9456 fi->type = ARMFault_Background;
9457 return true;
9458 }
9459
9460 if (matchregion == -1) {
9461 /* hit using the background region */
9462 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9463 } else {
9464 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9465 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
9466
9467 if (m_is_system_region(env, address)) {
9468 /* System space is always execute never */
9469 xn = 1;
9470 }
9471
9472 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9473 if (*prot && !xn) {
9474 *prot |= PAGE_EXEC;
9475 }
9476 /* We don't need to look the attribute up in the MAIR0/MAIR1
9477 * registers because that only tells us about cacheability.
9478 */
9479 if (mregion) {
9480 *mregion = matchregion;
9481 }
9482 }
9483
9484 fi->type = ARMFault_Permission;
9485 fi->level = 1;
9486 return !(*prot & (1 << access_type));
9487 }
9488
9489
9490 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9491 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9492 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9493 int *prot, ARMMMUFaultInfo *fi)
9494 {
9495 uint32_t secure = regime_is_secure(env, mmu_idx);
9496 V8M_SAttributes sattrs = {};
9497
9498 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9499 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9500 if (access_type == MMU_INST_FETCH) {
9501 /* Instruction fetches always use the MMU bank and the
9502 * transaction attribute determined by the fetch address,
9503 * regardless of CPU state. This is painful for QEMU
9504 * to handle, because it would mean we need to encode
9505 * into the mmu_idx not just the (user, negpri) information
9506 * for the current security state but also that for the
9507 * other security state, which would balloon the number
9508 * of mmu_idx values needed alarmingly.
9509 * Fortunately we can avoid this because it's not actually
9510 * possible to arbitrarily execute code from memory with
9511 * the wrong security attribute: it will always generate
9512 * an exception of some kind or another, apart from the
9513 * special case of an NS CPU executing an SG instruction
9514 * in S&NSC memory. So we always just fail the translation
9515 * here and sort things out in the exception handler
9516 * (including possibly emulating an SG instruction).
9517 */
9518 if (sattrs.ns != !secure) {
9519 if (sattrs.nsc) {
9520 fi->type = ARMFault_QEMU_NSCExec;
9521 } else {
9522 fi->type = ARMFault_QEMU_SFault;
9523 }
9524 *phys_ptr = address;
9525 *prot = 0;
9526 return true;
9527 }
9528 } else {
9529 /* For data accesses we always use the MMU bank indicated
9530 * by the current CPU state, but the security attributes
9531 * might downgrade a secure access to nonsecure.
9532 */
9533 if (sattrs.ns) {
9534 txattrs->secure = false;
9535 } else if (!secure) {
9536 /* NS access to S memory must fault.
9537 * Architecturally we should first check whether the
9538 * MPU information for this address indicates that we
9539 * are doing an unaligned access to Device memory, which
9540 * should generate a UsageFault instead. QEMU does not
9541 * currently check for that kind of unaligned access though.
9542 * If we added it we would need to do so as a special case
9543 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9544 */
9545 fi->type = ARMFault_QEMU_SFault;
9546 *phys_ptr = address;
9547 *prot = 0;
9548 return true;
9549 }
9550 }
9551 }
9552
9553 return pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
9554 txattrs, prot, fi, NULL);
9555 }
9556
9557 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
9558 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9559 hwaddr *phys_ptr, int *prot,
9560 ARMMMUFaultInfo *fi)
9561 {
9562 int n;
9563 uint32_t mask;
9564 uint32_t base;
9565 bool is_user = regime_is_user(env, mmu_idx);
9566
9567 if (regime_translation_disabled(env, mmu_idx)) {
9568 /* MPU disabled. */
9569 *phys_ptr = address;
9570 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9571 return false;
9572 }
9573
9574 *phys_ptr = address;
9575 for (n = 7; n >= 0; n--) {
9576 base = env->cp15.c6_region[n];
9577 if ((base & 1) == 0) {
9578 continue;
9579 }
9580 mask = 1 << ((base >> 1) & 0x1f);
9581 /* Keep this shift separate from the above to avoid an
9582 (undefined) << 32. */
9583 mask = (mask << 1) - 1;
9584 if (((base ^ address) & ~mask) == 0) {
9585 break;
9586 }
9587 }
9588 if (n < 0) {
9589 fi->type = ARMFault_Background;
9590 return true;
9591 }
9592
9593 if (access_type == MMU_INST_FETCH) {
9594 mask = env->cp15.pmsav5_insn_ap;
9595 } else {
9596 mask = env->cp15.pmsav5_data_ap;
9597 }
9598 mask = (mask >> (n * 4)) & 0xf;
9599 switch (mask) {
9600 case 0:
9601 fi->type = ARMFault_Permission;
9602 fi->level = 1;
9603 return true;
9604 case 1:
9605 if (is_user) {
9606 fi->type = ARMFault_Permission;
9607 fi->level = 1;
9608 return true;
9609 }
9610 *prot = PAGE_READ | PAGE_WRITE;
9611 break;
9612 case 2:
9613 *prot = PAGE_READ;
9614 if (!is_user) {
9615 *prot |= PAGE_WRITE;
9616 }
9617 break;
9618 case 3:
9619 *prot = PAGE_READ | PAGE_WRITE;
9620 break;
9621 case 5:
9622 if (is_user) {
9623 fi->type = ARMFault_Permission;
9624 fi->level = 1;
9625 return true;
9626 }
9627 *prot = PAGE_READ;
9628 break;
9629 case 6:
9630 *prot = PAGE_READ;
9631 break;
9632 default:
9633 /* Bad permission. */
9634 fi->type = ARMFault_Permission;
9635 fi->level = 1;
9636 return true;
9637 }
9638 *prot |= PAGE_EXEC;
9639 return false;
9640 }
9641
9642 /* Combine either inner or outer cacheability attributes for normal
9643 * memory, according to table D4-42 and pseudocode procedure
9644 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
9645 *
9646 * NB: only stage 1 includes allocation hints (RW bits), leading to
9647 * some asymmetry.
9648 */
9649 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
9650 {
9651 if (s1 == 4 || s2 == 4) {
9652 /* non-cacheable has precedence */
9653 return 4;
9654 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
9655 /* stage 1 write-through takes precedence */
9656 return s1;
9657 } else if (extract32(s2, 2, 2) == 2) {
9658 /* stage 2 write-through takes precedence, but the allocation hint
9659 * is still taken from stage 1
9660 */
9661 return (2 << 2) | extract32(s1, 0, 2);
9662 } else { /* write-back */
9663 return s1;
9664 }
9665 }
9666
9667 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
9668 * and CombineS1S2Desc()
9669 *
9670 * @s1: Attributes from stage 1 walk
9671 * @s2: Attributes from stage 2 walk
9672 */
9673 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
9674 {
9675 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
9676 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
9677 ARMCacheAttrs ret;
9678
9679 /* Combine shareability attributes (table D4-43) */
9680 if (s1.shareability == 2 || s2.shareability == 2) {
9681 /* if either are outer-shareable, the result is outer-shareable */
9682 ret.shareability = 2;
9683 } else if (s1.shareability == 3 || s2.shareability == 3) {
9684 /* if either are inner-shareable, the result is inner-shareable */
9685 ret.shareability = 3;
9686 } else {
9687 /* both non-shareable */
9688 ret.shareability = 0;
9689 }
9690
9691 /* Combine memory type and cacheability attributes */
9692 if (s1hi == 0 || s2hi == 0) {
9693 /* Device has precedence over normal */
9694 if (s1lo == 0 || s2lo == 0) {
9695 /* nGnRnE has precedence over anything */
9696 ret.attrs = 0;
9697 } else if (s1lo == 4 || s2lo == 4) {
9698 /* non-Reordering has precedence over Reordering */
9699 ret.attrs = 4; /* nGnRE */
9700 } else if (s1lo == 8 || s2lo == 8) {
9701 /* non-Gathering has precedence over Gathering */
9702 ret.attrs = 8; /* nGRE */
9703 } else {
9704 ret.attrs = 0xc; /* GRE */
9705 }
9706
9707 /* Any location for which the resultant memory type is any
9708 * type of Device memory is always treated as Outer Shareable.
9709 */
9710 ret.shareability = 2;
9711 } else { /* Normal memory */
9712 /* Outer/inner cacheability combine independently */
9713 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
9714 | combine_cacheattr_nibble(s1lo, s2lo);
9715
9716 if (ret.attrs == 0x44) {
9717 /* Any location for which the resultant memory type is Normal
9718 * Inner Non-cacheable, Outer Non-cacheable is always treated
9719 * as Outer Shareable.
9720 */
9721 ret.shareability = 2;
9722 }
9723 }
9724
9725 return ret;
9726 }
9727
9728
9729 /* get_phys_addr - get the physical address for this virtual address
9730 *
9731 * Find the physical address corresponding to the given virtual address,
9732 * by doing a translation table walk on MMU based systems or using the
9733 * MPU state on MPU based systems.
9734 *
9735 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
9736 * prot and page_size may not be filled in, and the populated fsr value provides
9737 * information on why the translation aborted, in the format of a
9738 * DFSR/IFSR fault register, with the following caveats:
9739 * * we honour the short vs long DFSR format differences.
9740 * * the WnR bit is never set (the caller must do this).
9741 * * for PSMAv5 based systems we don't bother to return a full FSR format
9742 * value.
9743 *
9744 * @env: CPUARMState
9745 * @address: virtual address to get physical address for
9746 * @access_type: 0 for read, 1 for write, 2 for execute
9747 * @mmu_idx: MMU index indicating required translation regime
9748 * @phys_ptr: set to the physical address corresponding to the virtual address
9749 * @attrs: set to the memory transaction attributes to use
9750 * @prot: set to the permissions for the page containing phys_ptr
9751 * @page_size: set to the size of the page containing phys_ptr
9752 * @fsr: set to the DFSR/IFSR value on failure
9753 * @fi: set to fault info if the translation fails
9754 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
9755 */
9756 static bool get_phys_addr(CPUARMState *env, target_ulong address,
9757 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9758 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
9759 target_ulong *page_size, uint32_t *fsr,
9760 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9761 {
9762 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9763 /* Call ourselves recursively to do the stage 1 and then stage 2
9764 * translations.
9765 */
9766 if (arm_feature(env, ARM_FEATURE_EL2)) {
9767 hwaddr ipa;
9768 int s2_prot;
9769 int ret;
9770 ARMCacheAttrs cacheattrs2 = {};
9771
9772 ret = get_phys_addr(env, address, access_type,
9773 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
9774 prot, page_size, fsr, fi, cacheattrs);
9775
9776 /* If S1 fails or S2 is disabled, return early. */
9777 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
9778 *phys_ptr = ipa;
9779 return ret;
9780 }
9781
9782 /* S1 is done. Now do S2 translation. */
9783 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
9784 phys_ptr, attrs, &s2_prot,
9785 page_size, fi,
9786 cacheattrs != NULL ? &cacheattrs2 : NULL);
9787 *fsr = arm_fi_to_lfsc(fi);
9788 fi->s2addr = ipa;
9789 /* Combine the S1 and S2 perms. */
9790 *prot &= s2_prot;
9791
9792 /* Combine the S1 and S2 cache attributes, if needed */
9793 if (!ret && cacheattrs != NULL) {
9794 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
9795 }
9796
9797 return ret;
9798 } else {
9799 /*
9800 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
9801 */
9802 mmu_idx = stage_1_mmu_idx(mmu_idx);
9803 }
9804 }
9805
9806 /* The page table entries may downgrade secure to non-secure, but
9807 * cannot upgrade an non-secure translation regime's attributes
9808 * to secure.
9809 */
9810 attrs->secure = regime_is_secure(env, mmu_idx);
9811 attrs->user = regime_is_user(env, mmu_idx);
9812
9813 /* Fast Context Switch Extension. This doesn't exist at all in v8.
9814 * In v7 and earlier it affects all stage 1 translations.
9815 */
9816 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
9817 && !arm_feature(env, ARM_FEATURE_V8)) {
9818 if (regime_el(env, mmu_idx) == 3) {
9819 address += env->cp15.fcseidr_s;
9820 } else {
9821 address += env->cp15.fcseidr_ns;
9822 }
9823 }
9824
9825 if (arm_feature(env, ARM_FEATURE_PMSA)) {
9826 bool ret;
9827 *page_size = TARGET_PAGE_SIZE;
9828
9829 if (arm_feature(env, ARM_FEATURE_V8)) {
9830 /* PMSAv8 */
9831 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
9832 phys_ptr, attrs, prot, fi);
9833 *fsr = arm_fi_to_sfsc(fi);
9834 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9835 /* PMSAv7 */
9836 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9837 phys_ptr, prot, fi);
9838 *fsr = arm_fi_to_sfsc(fi);
9839 } else {
9840 /* Pre-v7 MPU */
9841 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
9842 phys_ptr, prot, fi);
9843 *fsr = arm_fi_to_sfsc(fi);
9844 }
9845 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
9846 " mmu_idx %u -> %s (prot %c%c%c)\n",
9847 access_type == MMU_DATA_LOAD ? "reading" :
9848 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
9849 (uint32_t)address, mmu_idx,
9850 ret ? "Miss" : "Hit",
9851 *prot & PAGE_READ ? 'r' : '-',
9852 *prot & PAGE_WRITE ? 'w' : '-',
9853 *prot & PAGE_EXEC ? 'x' : '-');
9854
9855 return ret;
9856 }
9857
9858 /* Definitely a real MMU, not an MPU */
9859
9860 if (regime_translation_disabled(env, mmu_idx)) {
9861 /* MMU disabled. */
9862 *phys_ptr = address;
9863 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9864 *page_size = TARGET_PAGE_SIZE;
9865 return 0;
9866 }
9867
9868 if (regime_using_lpae_format(env, mmu_idx)) {
9869 bool ret = get_phys_addr_lpae(env, address, access_type, mmu_idx,
9870 phys_ptr, attrs, prot, page_size,
9871 fi, cacheattrs);
9872
9873 *fsr = arm_fi_to_lfsc(fi);
9874 return ret;
9875 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
9876 bool ret = get_phys_addr_v6(env, address, access_type, mmu_idx,
9877 phys_ptr, attrs, prot, page_size, fi);
9878
9879 *fsr = arm_fi_to_sfsc(fi);
9880 return ret;
9881 } else {
9882 bool ret = get_phys_addr_v5(env, address, access_type, mmu_idx,
9883 phys_ptr, prot, page_size, fi);
9884
9885 *fsr = arm_fi_to_sfsc(fi);
9886 return ret;
9887 }
9888 }
9889
9890 /* Walk the page table and (if the mapping exists) add the page
9891 * to the TLB. Return false on success, or true on failure. Populate
9892 * fsr with ARM DFSR/IFSR fault register format value on failure.
9893 */
9894 bool arm_tlb_fill(CPUState *cs, vaddr address,
9895 MMUAccessType access_type, int mmu_idx, uint32_t *fsr,
9896 ARMMMUFaultInfo *fi)
9897 {
9898 ARMCPU *cpu = ARM_CPU(cs);
9899 CPUARMState *env = &cpu->env;
9900 hwaddr phys_addr;
9901 target_ulong page_size;
9902 int prot;
9903 int ret;
9904 MemTxAttrs attrs = {};
9905
9906 ret = get_phys_addr(env, address, access_type,
9907 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
9908 &attrs, &prot, &page_size, fsr, fi, NULL);
9909 if (!ret) {
9910 /* Map a single [sub]page. */
9911 phys_addr &= TARGET_PAGE_MASK;
9912 address &= TARGET_PAGE_MASK;
9913 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
9914 prot, mmu_idx, page_size);
9915 return 0;
9916 }
9917
9918 return ret;
9919 }
9920
9921 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
9922 MemTxAttrs *attrs)
9923 {
9924 ARMCPU *cpu = ARM_CPU(cs);
9925 CPUARMState *env = &cpu->env;
9926 hwaddr phys_addr;
9927 target_ulong page_size;
9928 int prot;
9929 bool ret;
9930 uint32_t fsr;
9931 ARMMMUFaultInfo fi = {};
9932 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
9933
9934 *attrs = (MemTxAttrs) {};
9935
9936 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
9937 attrs, &prot, &page_size, &fsr, &fi, NULL);
9938
9939 if (ret) {
9940 return -1;
9941 }
9942 return phys_addr;
9943 }
9944
9945 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9946 {
9947 uint32_t mask;
9948 unsigned el = arm_current_el(env);
9949
9950 /* First handle registers which unprivileged can read */
9951
9952 switch (reg) {
9953 case 0 ... 7: /* xPSR sub-fields */
9954 mask = 0;
9955 if ((reg & 1) && el) {
9956 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
9957 }
9958 if (!(reg & 4)) {
9959 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
9960 }
9961 /* EPSR reads as zero */
9962 return xpsr_read(env) & mask;
9963 break;
9964 case 20: /* CONTROL */
9965 return env->v7m.control[env->v7m.secure];
9966 case 0x94: /* CONTROL_NS */
9967 /* We have to handle this here because unprivileged Secure code
9968 * can read the NS CONTROL register.
9969 */
9970 if (!env->v7m.secure) {
9971 return 0;
9972 }
9973 return env->v7m.control[M_REG_NS];
9974 }
9975
9976 if (el == 0) {
9977 return 0; /* unprivileged reads others as zero */
9978 }
9979
9980 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9981 switch (reg) {
9982 case 0x88: /* MSP_NS */
9983 if (!env->v7m.secure) {
9984 return 0;
9985 }
9986 return env->v7m.other_ss_msp;
9987 case 0x89: /* PSP_NS */
9988 if (!env->v7m.secure) {
9989 return 0;
9990 }
9991 return env->v7m.other_ss_psp;
9992 case 0x90: /* PRIMASK_NS */
9993 if (!env->v7m.secure) {
9994 return 0;
9995 }
9996 return env->v7m.primask[M_REG_NS];
9997 case 0x91: /* BASEPRI_NS */
9998 if (!env->v7m.secure) {
9999 return 0;
10000 }
10001 return env->v7m.basepri[M_REG_NS];
10002 case 0x93: /* FAULTMASK_NS */
10003 if (!env->v7m.secure) {
10004 return 0;
10005 }
10006 return env->v7m.faultmask[M_REG_NS];
10007 case 0x98: /* SP_NS */
10008 {
10009 /* This gives the non-secure SP selected based on whether we're
10010 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10011 */
10012 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10013
10014 if (!env->v7m.secure) {
10015 return 0;
10016 }
10017 if (!arm_v7m_is_handler_mode(env) && spsel) {
10018 return env->v7m.other_ss_psp;
10019 } else {
10020 return env->v7m.other_ss_msp;
10021 }
10022 }
10023 default:
10024 break;
10025 }
10026 }
10027
10028 switch (reg) {
10029 case 8: /* MSP */
10030 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
10031 case 9: /* PSP */
10032 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
10033 case 16: /* PRIMASK */
10034 return env->v7m.primask[env->v7m.secure];
10035 case 17: /* BASEPRI */
10036 case 18: /* BASEPRI_MAX */
10037 return env->v7m.basepri[env->v7m.secure];
10038 case 19: /* FAULTMASK */
10039 return env->v7m.faultmask[env->v7m.secure];
10040 default:
10041 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10042 " register %d\n", reg);
10043 return 0;
10044 }
10045 }
10046
10047 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10048 {
10049 /* We're passed bits [11..0] of the instruction; extract
10050 * SYSm and the mask bits.
10051 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10052 * we choose to treat them as if the mask bits were valid.
10053 * NB that the pseudocode 'mask' variable is bits [11..10],
10054 * whereas ours is [11..8].
10055 */
10056 uint32_t mask = extract32(maskreg, 8, 4);
10057 uint32_t reg = extract32(maskreg, 0, 8);
10058
10059 if (arm_current_el(env) == 0 && reg > 7) {
10060 /* only xPSR sub-fields may be written by unprivileged */
10061 return;
10062 }
10063
10064 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10065 switch (reg) {
10066 case 0x88: /* MSP_NS */
10067 if (!env->v7m.secure) {
10068 return;
10069 }
10070 env->v7m.other_ss_msp = val;
10071 return;
10072 case 0x89: /* PSP_NS */
10073 if (!env->v7m.secure) {
10074 return;
10075 }
10076 env->v7m.other_ss_psp = val;
10077 return;
10078 case 0x90: /* PRIMASK_NS */
10079 if (!env->v7m.secure) {
10080 return;
10081 }
10082 env->v7m.primask[M_REG_NS] = val & 1;
10083 return;
10084 case 0x91: /* BASEPRI_NS */
10085 if (!env->v7m.secure) {
10086 return;
10087 }
10088 env->v7m.basepri[M_REG_NS] = val & 0xff;
10089 return;
10090 case 0x93: /* FAULTMASK_NS */
10091 if (!env->v7m.secure) {
10092 return;
10093 }
10094 env->v7m.faultmask[M_REG_NS] = val & 1;
10095 return;
10096 case 0x98: /* SP_NS */
10097 {
10098 /* This gives the non-secure SP selected based on whether we're
10099 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10100 */
10101 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10102
10103 if (!env->v7m.secure) {
10104 return;
10105 }
10106 if (!arm_v7m_is_handler_mode(env) && spsel) {
10107 env->v7m.other_ss_psp = val;
10108 } else {
10109 env->v7m.other_ss_msp = val;
10110 }
10111 return;
10112 }
10113 default:
10114 break;
10115 }
10116 }
10117
10118 switch (reg) {
10119 case 0 ... 7: /* xPSR sub-fields */
10120 /* only APSR is actually writable */
10121 if (!(reg & 4)) {
10122 uint32_t apsrmask = 0;
10123
10124 if (mask & 8) {
10125 apsrmask |= XPSR_NZCV | XPSR_Q;
10126 }
10127 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
10128 apsrmask |= XPSR_GE;
10129 }
10130 xpsr_write(env, val, apsrmask);
10131 }
10132 break;
10133 case 8: /* MSP */
10134 if (v7m_using_psp(env)) {
10135 env->v7m.other_sp = val;
10136 } else {
10137 env->regs[13] = val;
10138 }
10139 break;
10140 case 9: /* PSP */
10141 if (v7m_using_psp(env)) {
10142 env->regs[13] = val;
10143 } else {
10144 env->v7m.other_sp = val;
10145 }
10146 break;
10147 case 16: /* PRIMASK */
10148 env->v7m.primask[env->v7m.secure] = val & 1;
10149 break;
10150 case 17: /* BASEPRI */
10151 env->v7m.basepri[env->v7m.secure] = val & 0xff;
10152 break;
10153 case 18: /* BASEPRI_MAX */
10154 val &= 0xff;
10155 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10156 || env->v7m.basepri[env->v7m.secure] == 0)) {
10157 env->v7m.basepri[env->v7m.secure] = val;
10158 }
10159 break;
10160 case 19: /* FAULTMASK */
10161 env->v7m.faultmask[env->v7m.secure] = val & 1;
10162 break;
10163 case 20: /* CONTROL */
10164 /* Writing to the SPSEL bit only has an effect if we are in
10165 * thread mode; other bits can be updated by any privileged code.
10166 * write_v7m_control_spsel() deals with updating the SPSEL bit in
10167 * env->v7m.control, so we only need update the others.
10168 * For v7M, we must just ignore explicit writes to SPSEL in handler
10169 * mode; for v8M the write is permitted but will have no effect.
10170 */
10171 if (arm_feature(env, ARM_FEATURE_V8) ||
10172 !arm_v7m_is_handler_mode(env)) {
10173 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
10174 }
10175 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10176 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
10177 break;
10178 default:
10179 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10180 " register %d\n", reg);
10181 return;
10182 }
10183 }
10184
10185 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10186 {
10187 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10188 bool forceunpriv = op & 1;
10189 bool alt = op & 2;
10190 V8M_SAttributes sattrs = {};
10191 uint32_t tt_resp;
10192 bool r, rw, nsr, nsrw, mrvalid;
10193 int prot;
10194 ARMMMUFaultInfo fi = {};
10195 MemTxAttrs attrs = {};
10196 hwaddr phys_addr;
10197 ARMMMUIdx mmu_idx;
10198 uint32_t mregion;
10199 bool targetpriv;
10200 bool targetsec = env->v7m.secure;
10201
10202 /* Work out what the security state and privilege level we're
10203 * interested in is...
10204 */
10205 if (alt) {
10206 targetsec = !targetsec;
10207 }
10208
10209 if (forceunpriv) {
10210 targetpriv = false;
10211 } else {
10212 targetpriv = arm_v7m_is_handler_mode(env) ||
10213 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10214 }
10215
10216 /* ...and then figure out which MMU index this is */
10217 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10218
10219 /* We know that the MPU and SAU don't care about the access type
10220 * for our purposes beyond that we don't want to claim to be
10221 * an insn fetch, so we arbitrarily call this a read.
10222 */
10223
10224 /* MPU region info only available for privileged or if
10225 * inspecting the other MPU state.
10226 */
10227 if (arm_current_el(env) != 0 || alt) {
10228 /* We can ignore the return value as prot is always set */
10229 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
10230 &phys_addr, &attrs, &prot, &fi, &mregion);
10231 if (mregion == -1) {
10232 mrvalid = false;
10233 mregion = 0;
10234 } else {
10235 mrvalid = true;
10236 }
10237 r = prot & PAGE_READ;
10238 rw = prot & PAGE_WRITE;
10239 } else {
10240 r = false;
10241 rw = false;
10242 mrvalid = false;
10243 mregion = 0;
10244 }
10245
10246 if (env->v7m.secure) {
10247 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10248 nsr = sattrs.ns && r;
10249 nsrw = sattrs.ns && rw;
10250 } else {
10251 sattrs.ns = true;
10252 nsr = false;
10253 nsrw = false;
10254 }
10255
10256 tt_resp = (sattrs.iregion << 24) |
10257 (sattrs.irvalid << 23) |
10258 ((!sattrs.ns) << 22) |
10259 (nsrw << 21) |
10260 (nsr << 20) |
10261 (rw << 19) |
10262 (r << 18) |
10263 (sattrs.srvalid << 17) |
10264 (mrvalid << 16) |
10265 (sattrs.sregion << 8) |
10266 mregion;
10267
10268 return tt_resp;
10269 }
10270
10271 #endif
10272
10273 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10274 {
10275 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10276 * Note that we do not implement the (architecturally mandated)
10277 * alignment fault for attempts to use this on Device memory
10278 * (which matches the usual QEMU behaviour of not implementing either
10279 * alignment faults or any memory attribute handling).
10280 */
10281
10282 ARMCPU *cpu = arm_env_get_cpu(env);
10283 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10284 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10285
10286 #ifndef CONFIG_USER_ONLY
10287 {
10288 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10289 * the block size so we might have to do more than one TLB lookup.
10290 * We know that in fact for any v8 CPU the page size is at least 4K
10291 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10292 * 1K as an artefact of legacy v5 subpage support being present in the
10293 * same QEMU executable.
10294 */
10295 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10296 void *hostaddr[maxidx];
10297 int try, i;
10298 unsigned mmu_idx = cpu_mmu_index(env, false);
10299 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
10300
10301 for (try = 0; try < 2; try++) {
10302
10303 for (i = 0; i < maxidx; i++) {
10304 hostaddr[i] = tlb_vaddr_to_host(env,
10305 vaddr + TARGET_PAGE_SIZE * i,
10306 1, mmu_idx);
10307 if (!hostaddr[i]) {
10308 break;
10309 }
10310 }
10311 if (i == maxidx) {
10312 /* If it's all in the TLB it's fair game for just writing to;
10313 * we know we don't need to update dirty status, etc.
10314 */
10315 for (i = 0; i < maxidx - 1; i++) {
10316 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10317 }
10318 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10319 return;
10320 }
10321 /* OK, try a store and see if we can populate the tlb. This
10322 * might cause an exception if the memory isn't writable,
10323 * in which case we will longjmp out of here. We must for
10324 * this purpose use the actual register value passed to us
10325 * so that we get the fault address right.
10326 */
10327 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
10328 /* Now we can populate the other TLB entries, if any */
10329 for (i = 0; i < maxidx; i++) {
10330 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10331 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
10332 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
10333 }
10334 }
10335 }
10336
10337 /* Slow path (probably attempt to do this to an I/O device or
10338 * similar, or clearing of a block of code we have translations
10339 * cached for). Just do a series of byte writes as the architecture
10340 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10341 * memset(), unmap() sequence here because:
10342 * + we'd need to account for the blocksize being larger than a page
10343 * + the direct-RAM access case is almost always going to be dealt
10344 * with in the fastpath code above, so there's no speed benefit
10345 * + we would have to deal with the map returning NULL because the
10346 * bounce buffer was in use
10347 */
10348 for (i = 0; i < blocklen; i++) {
10349 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
10350 }
10351 }
10352 #else
10353 memset(g2h(vaddr), 0, blocklen);
10354 #endif
10355 }
10356
10357 /* Note that signed overflow is undefined in C. The following routines are
10358 careful to use unsigned types where modulo arithmetic is required.
10359 Failure to do so _will_ break on newer gcc. */
10360
10361 /* Signed saturating arithmetic. */
10362
10363 /* Perform 16-bit signed saturating addition. */
10364 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10365 {
10366 uint16_t res;
10367
10368 res = a + b;
10369 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10370 if (a & 0x8000)
10371 res = 0x8000;
10372 else
10373 res = 0x7fff;
10374 }
10375 return res;
10376 }
10377
10378 /* Perform 8-bit signed saturating addition. */
10379 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10380 {
10381 uint8_t res;
10382
10383 res = a + b;
10384 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10385 if (a & 0x80)
10386 res = 0x80;
10387 else
10388 res = 0x7f;
10389 }
10390 return res;
10391 }
10392
10393 /* Perform 16-bit signed saturating subtraction. */
10394 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10395 {
10396 uint16_t res;
10397
10398 res = a - b;
10399 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10400 if (a & 0x8000)
10401 res = 0x8000;
10402 else
10403 res = 0x7fff;
10404 }
10405 return res;
10406 }
10407
10408 /* Perform 8-bit signed saturating subtraction. */
10409 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10410 {
10411 uint8_t res;
10412
10413 res = a - b;
10414 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10415 if (a & 0x80)
10416 res = 0x80;
10417 else
10418 res = 0x7f;
10419 }
10420 return res;
10421 }
10422
10423 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10424 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10425 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10426 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10427 #define PFX q
10428
10429 #include "op_addsub.h"
10430
10431 /* Unsigned saturating arithmetic. */
10432 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10433 {
10434 uint16_t res;
10435 res = a + b;
10436 if (res < a)
10437 res = 0xffff;
10438 return res;
10439 }
10440
10441 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10442 {
10443 if (a > b)
10444 return a - b;
10445 else
10446 return 0;
10447 }
10448
10449 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10450 {
10451 uint8_t res;
10452 res = a + b;
10453 if (res < a)
10454 res = 0xff;
10455 return res;
10456 }
10457
10458 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10459 {
10460 if (a > b)
10461 return a - b;
10462 else
10463 return 0;
10464 }
10465
10466 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10467 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10468 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10469 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10470 #define PFX uq
10471
10472 #include "op_addsub.h"
10473
10474 /* Signed modulo arithmetic. */
10475 #define SARITH16(a, b, n, op) do { \
10476 int32_t sum; \
10477 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10478 RESULT(sum, n, 16); \
10479 if (sum >= 0) \
10480 ge |= 3 << (n * 2); \
10481 } while(0)
10482
10483 #define SARITH8(a, b, n, op) do { \
10484 int32_t sum; \
10485 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10486 RESULT(sum, n, 8); \
10487 if (sum >= 0) \
10488 ge |= 1 << n; \
10489 } while(0)
10490
10491
10492 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10493 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10494 #define ADD8(a, b, n) SARITH8(a, b, n, +)
10495 #define SUB8(a, b, n) SARITH8(a, b, n, -)
10496 #define PFX s
10497 #define ARITH_GE
10498
10499 #include "op_addsub.h"
10500
10501 /* Unsigned modulo arithmetic. */
10502 #define ADD16(a, b, n) do { \
10503 uint32_t sum; \
10504 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10505 RESULT(sum, n, 16); \
10506 if ((sum >> 16) == 1) \
10507 ge |= 3 << (n * 2); \
10508 } while(0)
10509
10510 #define ADD8(a, b, n) do { \
10511 uint32_t sum; \
10512 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10513 RESULT(sum, n, 8); \
10514 if ((sum >> 8) == 1) \
10515 ge |= 1 << n; \
10516 } while(0)
10517
10518 #define SUB16(a, b, n) do { \
10519 uint32_t sum; \
10520 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10521 RESULT(sum, n, 16); \
10522 if ((sum >> 16) == 0) \
10523 ge |= 3 << (n * 2); \
10524 } while(0)
10525
10526 #define SUB8(a, b, n) do { \
10527 uint32_t sum; \
10528 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10529 RESULT(sum, n, 8); \
10530 if ((sum >> 8) == 0) \
10531 ge |= 1 << n; \
10532 } while(0)
10533
10534 #define PFX u
10535 #define ARITH_GE
10536
10537 #include "op_addsub.h"
10538
10539 /* Halved signed arithmetic. */
10540 #define ADD16(a, b, n) \
10541 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10542 #define SUB16(a, b, n) \
10543 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10544 #define ADD8(a, b, n) \
10545 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10546 #define SUB8(a, b, n) \
10547 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10548 #define PFX sh
10549
10550 #include "op_addsub.h"
10551
10552 /* Halved unsigned arithmetic. */
10553 #define ADD16(a, b, n) \
10554 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10555 #define SUB16(a, b, n) \
10556 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10557 #define ADD8(a, b, n) \
10558 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10559 #define SUB8(a, b, n) \
10560 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10561 #define PFX uh
10562
10563 #include "op_addsub.h"
10564
10565 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10566 {
10567 if (a > b)
10568 return a - b;
10569 else
10570 return b - a;
10571 }
10572
10573 /* Unsigned sum of absolute byte differences. */
10574 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10575 {
10576 uint32_t sum;
10577 sum = do_usad(a, b);
10578 sum += do_usad(a >> 8, b >> 8);
10579 sum += do_usad(a >> 16, b >>16);
10580 sum += do_usad(a >> 24, b >> 24);
10581 return sum;
10582 }
10583
10584 /* For ARMv6 SEL instruction. */
10585 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10586 {
10587 uint32_t mask;
10588
10589 mask = 0;
10590 if (flags & 1)
10591 mask |= 0xff;
10592 if (flags & 2)
10593 mask |= 0xff00;
10594 if (flags & 4)
10595 mask |= 0xff0000;
10596 if (flags & 8)
10597 mask |= 0xff000000;
10598 return (a & mask) | (b & ~mask);
10599 }
10600
10601 /* VFP support. We follow the convention used for VFP instructions:
10602 Single precision routines have a "s" suffix, double precision a
10603 "d" suffix. */
10604
10605 /* Convert host exception flags to vfp form. */
10606 static inline int vfp_exceptbits_from_host(int host_bits)
10607 {
10608 int target_bits = 0;
10609
10610 if (host_bits & float_flag_invalid)
10611 target_bits |= 1;
10612 if (host_bits & float_flag_divbyzero)
10613 target_bits |= 2;
10614 if (host_bits & float_flag_overflow)
10615 target_bits |= 4;
10616 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
10617 target_bits |= 8;
10618 if (host_bits & float_flag_inexact)
10619 target_bits |= 0x10;
10620 if (host_bits & float_flag_input_denormal)
10621 target_bits |= 0x80;
10622 return target_bits;
10623 }
10624
10625 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
10626 {
10627 int i;
10628 uint32_t fpscr;
10629
10630 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
10631 | (env->vfp.vec_len << 16)
10632 | (env->vfp.vec_stride << 20);
10633 i = get_float_exception_flags(&env->vfp.fp_status);
10634 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
10635 fpscr |= vfp_exceptbits_from_host(i);
10636 return fpscr;
10637 }
10638
10639 uint32_t vfp_get_fpscr(CPUARMState *env)
10640 {
10641 return HELPER(vfp_get_fpscr)(env);
10642 }
10643
10644 /* Convert vfp exception flags to target form. */
10645 static inline int vfp_exceptbits_to_host(int target_bits)
10646 {
10647 int host_bits = 0;
10648
10649 if (target_bits & 1)
10650 host_bits |= float_flag_invalid;
10651 if (target_bits & 2)
10652 host_bits |= float_flag_divbyzero;
10653 if (target_bits & 4)
10654 host_bits |= float_flag_overflow;
10655 if (target_bits & 8)
10656 host_bits |= float_flag_underflow;
10657 if (target_bits & 0x10)
10658 host_bits |= float_flag_inexact;
10659 if (target_bits & 0x80)
10660 host_bits |= float_flag_input_denormal;
10661 return host_bits;
10662 }
10663
10664 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
10665 {
10666 int i;
10667 uint32_t changed;
10668
10669 changed = env->vfp.xregs[ARM_VFP_FPSCR];
10670 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
10671 env->vfp.vec_len = (val >> 16) & 7;
10672 env->vfp.vec_stride = (val >> 20) & 3;
10673
10674 changed ^= val;
10675 if (changed & (3 << 22)) {
10676 i = (val >> 22) & 3;
10677 switch (i) {
10678 case FPROUNDING_TIEEVEN:
10679 i = float_round_nearest_even;
10680 break;
10681 case FPROUNDING_POSINF:
10682 i = float_round_up;
10683 break;
10684 case FPROUNDING_NEGINF:
10685 i = float_round_down;
10686 break;
10687 case FPROUNDING_ZERO:
10688 i = float_round_to_zero;
10689 break;
10690 }
10691 set_float_rounding_mode(i, &env->vfp.fp_status);
10692 }
10693 if (changed & (1 << 24)) {
10694 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
10695 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
10696 }
10697 if (changed & (1 << 25))
10698 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
10699
10700 i = vfp_exceptbits_to_host(val);
10701 set_float_exception_flags(i, &env->vfp.fp_status);
10702 set_float_exception_flags(0, &env->vfp.standard_fp_status);
10703 }
10704
10705 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
10706 {
10707 HELPER(vfp_set_fpscr)(env, val);
10708 }
10709
10710 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
10711
10712 #define VFP_BINOP(name) \
10713 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
10714 { \
10715 float_status *fpst = fpstp; \
10716 return float32_ ## name(a, b, fpst); \
10717 } \
10718 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
10719 { \
10720 float_status *fpst = fpstp; \
10721 return float64_ ## name(a, b, fpst); \
10722 }
10723 VFP_BINOP(add)
10724 VFP_BINOP(sub)
10725 VFP_BINOP(mul)
10726 VFP_BINOP(div)
10727 VFP_BINOP(min)
10728 VFP_BINOP(max)
10729 VFP_BINOP(minnum)
10730 VFP_BINOP(maxnum)
10731 #undef VFP_BINOP
10732
10733 float32 VFP_HELPER(neg, s)(float32 a)
10734 {
10735 return float32_chs(a);
10736 }
10737
10738 float64 VFP_HELPER(neg, d)(float64 a)
10739 {
10740 return float64_chs(a);
10741 }
10742
10743 float32 VFP_HELPER(abs, s)(float32 a)
10744 {
10745 return float32_abs(a);
10746 }
10747
10748 float64 VFP_HELPER(abs, d)(float64 a)
10749 {
10750 return float64_abs(a);
10751 }
10752
10753 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
10754 {
10755 return float32_sqrt(a, &env->vfp.fp_status);
10756 }
10757
10758 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
10759 {
10760 return float64_sqrt(a, &env->vfp.fp_status);
10761 }
10762
10763 /* XXX: check quiet/signaling case */
10764 #define DO_VFP_cmp(p, type) \
10765 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
10766 { \
10767 uint32_t flags; \
10768 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
10769 case 0: flags = 0x6; break; \
10770 case -1: flags = 0x8; break; \
10771 case 1: flags = 0x2; break; \
10772 default: case 2: flags = 0x3; break; \
10773 } \
10774 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10775 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10776 } \
10777 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
10778 { \
10779 uint32_t flags; \
10780 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
10781 case 0: flags = 0x6; break; \
10782 case -1: flags = 0x8; break; \
10783 case 1: flags = 0x2; break; \
10784 default: case 2: flags = 0x3; break; \
10785 } \
10786 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10787 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10788 }
10789 DO_VFP_cmp(s, float32)
10790 DO_VFP_cmp(d, float64)
10791 #undef DO_VFP_cmp
10792
10793 /* Integer to float and float to integer conversions */
10794
10795 #define CONV_ITOF(name, fsz, sign) \
10796 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
10797 { \
10798 float_status *fpst = fpstp; \
10799 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
10800 }
10801
10802 #define CONV_FTOI(name, fsz, sign, round) \
10803 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
10804 { \
10805 float_status *fpst = fpstp; \
10806 if (float##fsz##_is_any_nan(x)) { \
10807 float_raise(float_flag_invalid, fpst); \
10808 return 0; \
10809 } \
10810 return float##fsz##_to_##sign##int32##round(x, fpst); \
10811 }
10812
10813 #define FLOAT_CONVS(name, p, fsz, sign) \
10814 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
10815 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
10816 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
10817
10818 FLOAT_CONVS(si, s, 32, )
10819 FLOAT_CONVS(si, d, 64, )
10820 FLOAT_CONVS(ui, s, 32, u)
10821 FLOAT_CONVS(ui, d, 64, u)
10822
10823 #undef CONV_ITOF
10824 #undef CONV_FTOI
10825 #undef FLOAT_CONVS
10826
10827 /* floating point conversion */
10828 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
10829 {
10830 float64 r = float32_to_float64(x, &env->vfp.fp_status);
10831 /* ARM requires that S<->D conversion of any kind of NaN generates
10832 * a quiet NaN by forcing the most significant frac bit to 1.
10833 */
10834 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
10835 }
10836
10837 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
10838 {
10839 float32 r = float64_to_float32(x, &env->vfp.fp_status);
10840 /* ARM requires that S<->D conversion of any kind of NaN generates
10841 * a quiet NaN by forcing the most significant frac bit to 1.
10842 */
10843 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
10844 }
10845
10846 /* VFP3 fixed point conversion. */
10847 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10848 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
10849 void *fpstp) \
10850 { \
10851 float_status *fpst = fpstp; \
10852 float##fsz tmp; \
10853 tmp = itype##_to_##float##fsz(x, fpst); \
10854 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
10855 }
10856
10857 /* Notice that we want only input-denormal exception flags from the
10858 * scalbn operation: the other possible flags (overflow+inexact if
10859 * we overflow to infinity, output-denormal) aren't correct for the
10860 * complete scale-and-convert operation.
10861 */
10862 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
10863 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
10864 uint32_t shift, \
10865 void *fpstp) \
10866 { \
10867 float_status *fpst = fpstp; \
10868 int old_exc_flags = get_float_exception_flags(fpst); \
10869 float##fsz tmp; \
10870 if (float##fsz##_is_any_nan(x)) { \
10871 float_raise(float_flag_invalid, fpst); \
10872 return 0; \
10873 } \
10874 tmp = float##fsz##_scalbn(x, shift, fpst); \
10875 old_exc_flags |= get_float_exception_flags(fpst) \
10876 & float_flag_input_denormal; \
10877 set_float_exception_flags(old_exc_flags, fpst); \
10878 return float##fsz##_to_##itype##round(tmp, fpst); \
10879 }
10880
10881 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
10882 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10883 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
10884 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
10885
10886 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
10887 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10888 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
10889
10890 VFP_CONV_FIX(sh, d, 64, 64, int16)
10891 VFP_CONV_FIX(sl, d, 64, 64, int32)
10892 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
10893 VFP_CONV_FIX(uh, d, 64, 64, uint16)
10894 VFP_CONV_FIX(ul, d, 64, 64, uint32)
10895 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
10896 VFP_CONV_FIX(sh, s, 32, 32, int16)
10897 VFP_CONV_FIX(sl, s, 32, 32, int32)
10898 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
10899 VFP_CONV_FIX(uh, s, 32, 32, uint16)
10900 VFP_CONV_FIX(ul, s, 32, 32, uint32)
10901 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
10902 #undef VFP_CONV_FIX
10903 #undef VFP_CONV_FIX_FLOAT
10904 #undef VFP_CONV_FLOAT_FIX_ROUND
10905
10906 /* Set the current fp rounding mode and return the old one.
10907 * The argument is a softfloat float_round_ value.
10908 */
10909 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
10910 {
10911 float_status *fp_status = &env->vfp.fp_status;
10912
10913 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10914 set_float_rounding_mode(rmode, fp_status);
10915
10916 return prev_rmode;
10917 }
10918
10919 /* Set the current fp rounding mode in the standard fp status and return
10920 * the old one. This is for NEON instructions that need to change the
10921 * rounding mode but wish to use the standard FPSCR values for everything
10922 * else. Always set the rounding mode back to the correct value after
10923 * modifying it.
10924 * The argument is a softfloat float_round_ value.
10925 */
10926 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
10927 {
10928 float_status *fp_status = &env->vfp.standard_fp_status;
10929
10930 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10931 set_float_rounding_mode(rmode, fp_status);
10932
10933 return prev_rmode;
10934 }
10935
10936 /* Half precision conversions. */
10937 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
10938 {
10939 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10940 float32 r = float16_to_float32(make_float16(a), ieee, s);
10941 if (ieee) {
10942 return float32_maybe_silence_nan(r, s);
10943 }
10944 return r;
10945 }
10946
10947 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
10948 {
10949 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10950 float16 r = float32_to_float16(a, ieee, s);
10951 if (ieee) {
10952 r = float16_maybe_silence_nan(r, s);
10953 }
10954 return float16_val(r);
10955 }
10956
10957 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
10958 {
10959 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
10960 }
10961
10962 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
10963 {
10964 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
10965 }
10966
10967 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
10968 {
10969 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
10970 }
10971
10972 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
10973 {
10974 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
10975 }
10976
10977 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
10978 {
10979 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10980 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
10981 if (ieee) {
10982 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
10983 }
10984 return r;
10985 }
10986
10987 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
10988 {
10989 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10990 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
10991 if (ieee) {
10992 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
10993 }
10994 return float16_val(r);
10995 }
10996
10997 #define float32_two make_float32(0x40000000)
10998 #define float32_three make_float32(0x40400000)
10999 #define float32_one_point_five make_float32(0x3fc00000)
11000
11001 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
11002 {
11003 float_status *s = &env->vfp.standard_fp_status;
11004 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11005 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
11006 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11007 float_raise(float_flag_input_denormal, s);
11008 }
11009 return float32_two;
11010 }
11011 return float32_sub(float32_two, float32_mul(a, b, s), s);
11012 }
11013
11014 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
11015 {
11016 float_status *s = &env->vfp.standard_fp_status;
11017 float32 product;
11018 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11019 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
11020 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11021 float_raise(float_flag_input_denormal, s);
11022 }
11023 return float32_one_point_five;
11024 }
11025 product = float32_mul(a, b, s);
11026 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
11027 }
11028
11029 /* NEON helpers. */
11030
11031 /* Constants 256 and 512 are used in some helpers; we avoid relying on
11032 * int->float conversions at run-time. */
11033 #define float64_256 make_float64(0x4070000000000000LL)
11034 #define float64_512 make_float64(0x4080000000000000LL)
11035 #define float32_maxnorm make_float32(0x7f7fffff)
11036 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
11037
11038 /* Reciprocal functions
11039 *
11040 * The algorithm that must be used to calculate the estimate
11041 * is specified by the ARM ARM, see FPRecipEstimate()
11042 */
11043
11044 static float64 recip_estimate(float64 a, float_status *real_fp_status)
11045 {
11046 /* These calculations mustn't set any fp exception flags,
11047 * so we use a local copy of the fp_status.
11048 */
11049 float_status dummy_status = *real_fp_status;
11050 float_status *s = &dummy_status;
11051 /* q = (int)(a * 512.0) */
11052 float64 q = float64_mul(float64_512, a, s);
11053 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11054
11055 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
11056 q = int64_to_float64(q_int, s);
11057 q = float64_add(q, float64_half, s);
11058 q = float64_div(q, float64_512, s);
11059 q = float64_div(float64_one, q, s);
11060
11061 /* s = (int)(256.0 * r + 0.5) */
11062 q = float64_mul(q, float64_256, s);
11063 q = float64_add(q, float64_half, s);
11064 q_int = float64_to_int64_round_to_zero(q, s);
11065
11066 /* return (double)s / 256.0 */
11067 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11068 }
11069
11070 /* Common wrapper to call recip_estimate */
11071 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
11072 {
11073 uint64_t val64 = float64_val(num);
11074 uint64_t frac = extract64(val64, 0, 52);
11075 int64_t exp = extract64(val64, 52, 11);
11076 uint64_t sbit;
11077 float64 scaled, estimate;
11078
11079 /* Generate the scaled number for the estimate function */
11080 if (exp == 0) {
11081 if (extract64(frac, 51, 1) == 0) {
11082 exp = -1;
11083 frac = extract64(frac, 0, 50) << 2;
11084 } else {
11085 frac = extract64(frac, 0, 51) << 1;
11086 }
11087 }
11088
11089 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
11090 scaled = make_float64((0x3feULL << 52)
11091 | extract64(frac, 44, 8) << 44);
11092
11093 estimate = recip_estimate(scaled, fpst);
11094
11095 /* Build new result */
11096 val64 = float64_val(estimate);
11097 sbit = 0x8000000000000000ULL & val64;
11098 exp = off - exp;
11099 frac = extract64(val64, 0, 52);
11100
11101 if (exp == 0) {
11102 frac = 1ULL << 51 | extract64(frac, 1, 51);
11103 } else if (exp == -1) {
11104 frac = 1ULL << 50 | extract64(frac, 2, 50);
11105 exp = 0;
11106 }
11107
11108 return make_float64(sbit | (exp << 52) | frac);
11109 }
11110
11111 static bool round_to_inf(float_status *fpst, bool sign_bit)
11112 {
11113 switch (fpst->float_rounding_mode) {
11114 case float_round_nearest_even: /* Round to Nearest */
11115 return true;
11116 case float_round_up: /* Round to +Inf */
11117 return !sign_bit;
11118 case float_round_down: /* Round to -Inf */
11119 return sign_bit;
11120 case float_round_to_zero: /* Round to Zero */
11121 return false;
11122 }
11123
11124 g_assert_not_reached();
11125 }
11126
11127 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11128 {
11129 float_status *fpst = fpstp;
11130 float32 f32 = float32_squash_input_denormal(input, fpst);
11131 uint32_t f32_val = float32_val(f32);
11132 uint32_t f32_sbit = 0x80000000ULL & f32_val;
11133 int32_t f32_exp = extract32(f32_val, 23, 8);
11134 uint32_t f32_frac = extract32(f32_val, 0, 23);
11135 float64 f64, r64;
11136 uint64_t r64_val;
11137 int64_t r64_exp;
11138 uint64_t r64_frac;
11139
11140 if (float32_is_any_nan(f32)) {
11141 float32 nan = f32;
11142 if (float32_is_signaling_nan(f32, fpst)) {
11143 float_raise(float_flag_invalid, fpst);
11144 nan = float32_maybe_silence_nan(f32, fpst);
11145 }
11146 if (fpst->default_nan_mode) {
11147 nan = float32_default_nan(fpst);
11148 }
11149 return nan;
11150 } else if (float32_is_infinity(f32)) {
11151 return float32_set_sign(float32_zero, float32_is_neg(f32));
11152 } else if (float32_is_zero(f32)) {
11153 float_raise(float_flag_divbyzero, fpst);
11154 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11155 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
11156 /* Abs(value) < 2.0^-128 */
11157 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11158 if (round_to_inf(fpst, f32_sbit)) {
11159 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11160 } else {
11161 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
11162 }
11163 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11164 float_raise(float_flag_underflow, fpst);
11165 return float32_set_sign(float32_zero, float32_is_neg(f32));
11166 }
11167
11168
11169 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
11170 r64 = call_recip_estimate(f64, 253, fpst);
11171 r64_val = float64_val(r64);
11172 r64_exp = extract64(r64_val, 52, 11);
11173 r64_frac = extract64(r64_val, 0, 52);
11174
11175 /* result = sign : result_exp<7:0> : fraction<51:29>; */
11176 return make_float32(f32_sbit |
11177 (r64_exp & 0xff) << 23 |
11178 extract64(r64_frac, 29, 24));
11179 }
11180
11181 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11182 {
11183 float_status *fpst = fpstp;
11184 float64 f64 = float64_squash_input_denormal(input, fpst);
11185 uint64_t f64_val = float64_val(f64);
11186 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
11187 int64_t f64_exp = extract64(f64_val, 52, 11);
11188 float64 r64;
11189 uint64_t r64_val;
11190 int64_t r64_exp;
11191 uint64_t r64_frac;
11192
11193 /* Deal with any special cases */
11194 if (float64_is_any_nan(f64)) {
11195 float64 nan = f64;
11196 if (float64_is_signaling_nan(f64, fpst)) {
11197 float_raise(float_flag_invalid, fpst);
11198 nan = float64_maybe_silence_nan(f64, fpst);
11199 }
11200 if (fpst->default_nan_mode) {
11201 nan = float64_default_nan(fpst);
11202 }
11203 return nan;
11204 } else if (float64_is_infinity(f64)) {
11205 return float64_set_sign(float64_zero, float64_is_neg(f64));
11206 } else if (float64_is_zero(f64)) {
11207 float_raise(float_flag_divbyzero, fpst);
11208 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11209 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11210 /* Abs(value) < 2.0^-1024 */
11211 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11212 if (round_to_inf(fpst, f64_sbit)) {
11213 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11214 } else {
11215 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
11216 }
11217 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
11218 float_raise(float_flag_underflow, fpst);
11219 return float64_set_sign(float64_zero, float64_is_neg(f64));
11220 }
11221
11222 r64 = call_recip_estimate(f64, 2045, fpst);
11223 r64_val = float64_val(r64);
11224 r64_exp = extract64(r64_val, 52, 11);
11225 r64_frac = extract64(r64_val, 0, 52);
11226
11227 /* result = sign : result_exp<10:0> : fraction<51:0> */
11228 return make_float64(f64_sbit |
11229 ((r64_exp & 0x7ff) << 52) |
11230 r64_frac);
11231 }
11232
11233 /* The algorithm that must be used to calculate the estimate
11234 * is specified by the ARM ARM.
11235 */
11236 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
11237 {
11238 /* These calculations mustn't set any fp exception flags,
11239 * so we use a local copy of the fp_status.
11240 */
11241 float_status dummy_status = *real_fp_status;
11242 float_status *s = &dummy_status;
11243 float64 q;
11244 int64_t q_int;
11245
11246 if (float64_lt(a, float64_half, s)) {
11247 /* range 0.25 <= a < 0.5 */
11248
11249 /* a in units of 1/512 rounded down */
11250 /* q0 = (int)(a * 512.0); */
11251 q = float64_mul(float64_512, a, s);
11252 q_int = float64_to_int64_round_to_zero(q, s);
11253
11254 /* reciprocal root r */
11255 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
11256 q = int64_to_float64(q_int, s);
11257 q = float64_add(q, float64_half, s);
11258 q = float64_div(q, float64_512, s);
11259 q = float64_sqrt(q, s);
11260 q = float64_div(float64_one, q, s);
11261 } else {
11262 /* range 0.5 <= a < 1.0 */
11263
11264 /* a in units of 1/256 rounded down */
11265 /* q1 = (int)(a * 256.0); */
11266 q = float64_mul(float64_256, a, s);
11267 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11268
11269 /* reciprocal root r */
11270 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
11271 q = int64_to_float64(q_int, s);
11272 q = float64_add(q, float64_half, s);
11273 q = float64_div(q, float64_256, s);
11274 q = float64_sqrt(q, s);
11275 q = float64_div(float64_one, q, s);
11276 }
11277 /* r in units of 1/256 rounded to nearest */
11278 /* s = (int)(256.0 * r + 0.5); */
11279
11280 q = float64_mul(q, float64_256,s );
11281 q = float64_add(q, float64_half, s);
11282 q_int = float64_to_int64_round_to_zero(q, s);
11283
11284 /* return (double)s / 256.0;*/
11285 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11286 }
11287
11288 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
11289 {
11290 float_status *s = fpstp;
11291 float32 f32 = float32_squash_input_denormal(input, s);
11292 uint32_t val = float32_val(f32);
11293 uint32_t f32_sbit = 0x80000000 & val;
11294 int32_t f32_exp = extract32(val, 23, 8);
11295 uint32_t f32_frac = extract32(val, 0, 23);
11296 uint64_t f64_frac;
11297 uint64_t val64;
11298 int result_exp;
11299 float64 f64;
11300
11301 if (float32_is_any_nan(f32)) {
11302 float32 nan = f32;
11303 if (float32_is_signaling_nan(f32, s)) {
11304 float_raise(float_flag_invalid, s);
11305 nan = float32_maybe_silence_nan(f32, s);
11306 }
11307 if (s->default_nan_mode) {
11308 nan = float32_default_nan(s);
11309 }
11310 return nan;
11311 } else if (float32_is_zero(f32)) {
11312 float_raise(float_flag_divbyzero, s);
11313 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11314 } else if (float32_is_neg(f32)) {
11315 float_raise(float_flag_invalid, s);
11316 return float32_default_nan(s);
11317 } else if (float32_is_infinity(f32)) {
11318 return float32_zero;
11319 }
11320
11321 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11322 * preserving the parity of the exponent. */
11323
11324 f64_frac = ((uint64_t) f32_frac) << 29;
11325 if (f32_exp == 0) {
11326 while (extract64(f64_frac, 51, 1) == 0) {
11327 f64_frac = f64_frac << 1;
11328 f32_exp = f32_exp-1;
11329 }
11330 f64_frac = extract64(f64_frac, 0, 51) << 1;
11331 }
11332
11333 if (extract64(f32_exp, 0, 1) == 0) {
11334 f64 = make_float64(((uint64_t) f32_sbit) << 32
11335 | (0x3feULL << 52)
11336 | f64_frac);
11337 } else {
11338 f64 = make_float64(((uint64_t) f32_sbit) << 32
11339 | (0x3fdULL << 52)
11340 | f64_frac);
11341 }
11342
11343 result_exp = (380 - f32_exp) / 2;
11344
11345 f64 = recip_sqrt_estimate(f64, s);
11346
11347 val64 = float64_val(f64);
11348
11349 val = ((result_exp & 0xff) << 23)
11350 | ((val64 >> 29) & 0x7fffff);
11351 return make_float32(val);
11352 }
11353
11354 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
11355 {
11356 float_status *s = fpstp;
11357 float64 f64 = float64_squash_input_denormal(input, s);
11358 uint64_t val = float64_val(f64);
11359 uint64_t f64_sbit = 0x8000000000000000ULL & val;
11360 int64_t f64_exp = extract64(val, 52, 11);
11361 uint64_t f64_frac = extract64(val, 0, 52);
11362 int64_t result_exp;
11363 uint64_t result_frac;
11364
11365 if (float64_is_any_nan(f64)) {
11366 float64 nan = f64;
11367 if (float64_is_signaling_nan(f64, s)) {
11368 float_raise(float_flag_invalid, s);
11369 nan = float64_maybe_silence_nan(f64, s);
11370 }
11371 if (s->default_nan_mode) {
11372 nan = float64_default_nan(s);
11373 }
11374 return nan;
11375 } else if (float64_is_zero(f64)) {
11376 float_raise(float_flag_divbyzero, s);
11377 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11378 } else if (float64_is_neg(f64)) {
11379 float_raise(float_flag_invalid, s);
11380 return float64_default_nan(s);
11381 } else if (float64_is_infinity(f64)) {
11382 return float64_zero;
11383 }
11384
11385 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11386 * preserving the parity of the exponent. */
11387
11388 if (f64_exp == 0) {
11389 while (extract64(f64_frac, 51, 1) == 0) {
11390 f64_frac = f64_frac << 1;
11391 f64_exp = f64_exp - 1;
11392 }
11393 f64_frac = extract64(f64_frac, 0, 51) << 1;
11394 }
11395
11396 if (extract64(f64_exp, 0, 1) == 0) {
11397 f64 = make_float64(f64_sbit
11398 | (0x3feULL << 52)
11399 | f64_frac);
11400 } else {
11401 f64 = make_float64(f64_sbit
11402 | (0x3fdULL << 52)
11403 | f64_frac);
11404 }
11405
11406 result_exp = (3068 - f64_exp) / 2;
11407
11408 f64 = recip_sqrt_estimate(f64, s);
11409
11410 result_frac = extract64(float64_val(f64), 0, 52);
11411
11412 return make_float64(f64_sbit |
11413 ((result_exp & 0x7ff) << 52) |
11414 result_frac);
11415 }
11416
11417 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
11418 {
11419 float_status *s = fpstp;
11420 float64 f64;
11421
11422 if ((a & 0x80000000) == 0) {
11423 return 0xffffffff;
11424 }
11425
11426 f64 = make_float64((0x3feULL << 52)
11427 | ((int64_t)(a & 0x7fffffff) << 21));
11428
11429 f64 = recip_estimate(f64, s);
11430
11431 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
11432 }
11433
11434 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
11435 {
11436 float_status *fpst = fpstp;
11437 float64 f64;
11438
11439 if ((a & 0xc0000000) == 0) {
11440 return 0xffffffff;
11441 }
11442
11443 if (a & 0x80000000) {
11444 f64 = make_float64((0x3feULL << 52)
11445 | ((uint64_t)(a & 0x7fffffff) << 21));
11446 } else { /* bits 31-30 == '01' */
11447 f64 = make_float64((0x3fdULL << 52)
11448 | ((uint64_t)(a & 0x3fffffff) << 22));
11449 }
11450
11451 f64 = recip_sqrt_estimate(f64, fpst);
11452
11453 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
11454 }
11455
11456 /* VFPv4 fused multiply-accumulate */
11457 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11458 {
11459 float_status *fpst = fpstp;
11460 return float32_muladd(a, b, c, 0, fpst);
11461 }
11462
11463 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11464 {
11465 float_status *fpst = fpstp;
11466 return float64_muladd(a, b, c, 0, fpst);
11467 }
11468
11469 /* ARMv8 round to integral */
11470 float32 HELPER(rints_exact)(float32 x, void *fp_status)
11471 {
11472 return float32_round_to_int(x, fp_status);
11473 }
11474
11475 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11476 {
11477 return float64_round_to_int(x, fp_status);
11478 }
11479
11480 float32 HELPER(rints)(float32 x, void *fp_status)
11481 {
11482 int old_flags = get_float_exception_flags(fp_status), new_flags;
11483 float32 ret;
11484
11485 ret = float32_round_to_int(x, fp_status);
11486
11487 /* Suppress any inexact exceptions the conversion produced */
11488 if (!(old_flags & float_flag_inexact)) {
11489 new_flags = get_float_exception_flags(fp_status);
11490 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11491 }
11492
11493 return ret;
11494 }
11495
11496 float64 HELPER(rintd)(float64 x, void *fp_status)
11497 {
11498 int old_flags = get_float_exception_flags(fp_status), new_flags;
11499 float64 ret;
11500
11501 ret = float64_round_to_int(x, fp_status);
11502
11503 new_flags = get_float_exception_flags(fp_status);
11504
11505 /* Suppress any inexact exceptions the conversion produced */
11506 if (!(old_flags & float_flag_inexact)) {
11507 new_flags = get_float_exception_flags(fp_status);
11508 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11509 }
11510
11511 return ret;
11512 }
11513
11514 /* Convert ARM rounding mode to softfloat */
11515 int arm_rmode_to_sf(int rmode)
11516 {
11517 switch (rmode) {
11518 case FPROUNDING_TIEAWAY:
11519 rmode = float_round_ties_away;
11520 break;
11521 case FPROUNDING_ODD:
11522 /* FIXME: add support for TIEAWAY and ODD */
11523 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
11524 rmode);
11525 case FPROUNDING_TIEEVEN:
11526 default:
11527 rmode = float_round_nearest_even;
11528 break;
11529 case FPROUNDING_POSINF:
11530 rmode = float_round_up;
11531 break;
11532 case FPROUNDING_NEGINF:
11533 rmode = float_round_down;
11534 break;
11535 case FPROUNDING_ZERO:
11536 rmode = float_round_to_zero;
11537 break;
11538 }
11539 return rmode;
11540 }
11541
11542 /* CRC helpers.
11543 * The upper bytes of val (above the number specified by 'bytes') must have
11544 * been zeroed out by the caller.
11545 */
11546 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11547 {
11548 uint8_t buf[4];
11549
11550 stl_le_p(buf, val);
11551
11552 /* zlib crc32 converts the accumulator and output to one's complement. */
11553 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11554 }
11555
11556 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11557 {
11558 uint8_t buf[4];
11559
11560 stl_le_p(buf, val);
11561
11562 /* Linux crc32c converts the output to one's complement. */
11563 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11564 }