]> git.proxmox.com Git - mirror_qemu.git/blob - target/arm/helper.c
Merge remote-tracking branch 'remotes/berrange/tags/pull-2017-11-06-2' into staging
[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, uint32_t *fsr,
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;
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, &fi, &cacheattrs);
2172 if (extended_addresses_enabled(env)) {
2173 /* fsr is a DFSR/IFSR value for the long descriptor
2174 * translation table format, but with WnR always clear.
2175 * Convert it to a 64-bit PAR.
2176 */
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 par64 |= 1; /* F */
2187 par64 |= (fsr & 0x3f) << 1; /* FS */
2188 /* Note that S2WLK and FSTAGE are always zero, because we don't
2189 * implement virtualization and therefore there can't be a stage 2
2190 * fault.
2191 */
2192 }
2193 } else {
2194 /* fsr is a DFSR/IFSR value for the short descriptor
2195 * translation table format (with WnR always clear).
2196 * Convert it to a 32-bit PAR.
2197 */
2198 if (!ret) {
2199 /* We do not set any attribute bits in the PAR */
2200 if (page_size == (1 << 24)
2201 && arm_feature(env, ARM_FEATURE_V7)) {
2202 par64 = (phys_addr & 0xff000000) | (1 << 1);
2203 } else {
2204 par64 = phys_addr & 0xfffff000;
2205 }
2206 if (!attrs.secure) {
2207 par64 |= (1 << 9); /* NS */
2208 }
2209 } else {
2210 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2211 ((fsr & 0xf) << 1) | 1;
2212 }
2213 }
2214 return par64;
2215 }
2216
2217 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2218 {
2219 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2220 uint64_t par64;
2221 ARMMMUIdx mmu_idx;
2222 int el = arm_current_el(env);
2223 bool secure = arm_is_secure_below_el3(env);
2224
2225 switch (ri->opc2 & 6) {
2226 case 0:
2227 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2228 switch (el) {
2229 case 3:
2230 mmu_idx = ARMMMUIdx_S1E3;
2231 break;
2232 case 2:
2233 mmu_idx = ARMMMUIdx_S1NSE1;
2234 break;
2235 case 1:
2236 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2237 break;
2238 default:
2239 g_assert_not_reached();
2240 }
2241 break;
2242 case 2:
2243 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2244 switch (el) {
2245 case 3:
2246 mmu_idx = ARMMMUIdx_S1SE0;
2247 break;
2248 case 2:
2249 mmu_idx = ARMMMUIdx_S1NSE0;
2250 break;
2251 case 1:
2252 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2253 break;
2254 default:
2255 g_assert_not_reached();
2256 }
2257 break;
2258 case 4:
2259 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2260 mmu_idx = ARMMMUIdx_S12NSE1;
2261 break;
2262 case 6:
2263 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2264 mmu_idx = ARMMMUIdx_S12NSE0;
2265 break;
2266 default:
2267 g_assert_not_reached();
2268 }
2269
2270 par64 = do_ats_write(env, value, access_type, mmu_idx);
2271
2272 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2273 }
2274
2275 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2276 uint64_t value)
2277 {
2278 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2279 uint64_t par64;
2280
2281 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2282
2283 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2284 }
2285
2286 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2287 bool isread)
2288 {
2289 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2290 return CP_ACCESS_TRAP;
2291 }
2292 return CP_ACCESS_OK;
2293 }
2294
2295 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2296 uint64_t value)
2297 {
2298 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2299 ARMMMUIdx mmu_idx;
2300 int secure = arm_is_secure_below_el3(env);
2301
2302 switch (ri->opc2 & 6) {
2303 case 0:
2304 switch (ri->opc1) {
2305 case 0: /* AT S1E1R, AT S1E1W */
2306 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2307 break;
2308 case 4: /* AT S1E2R, AT S1E2W */
2309 mmu_idx = ARMMMUIdx_S1E2;
2310 break;
2311 case 6: /* AT S1E3R, AT S1E3W */
2312 mmu_idx = ARMMMUIdx_S1E3;
2313 break;
2314 default:
2315 g_assert_not_reached();
2316 }
2317 break;
2318 case 2: /* AT S1E0R, AT S1E0W */
2319 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2320 break;
2321 case 4: /* AT S12E1R, AT S12E1W */
2322 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
2323 break;
2324 case 6: /* AT S12E0R, AT S12E0W */
2325 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
2326 break;
2327 default:
2328 g_assert_not_reached();
2329 }
2330
2331 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
2332 }
2333 #endif
2334
2335 static const ARMCPRegInfo vapa_cp_reginfo[] = {
2336 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2337 .access = PL1_RW, .resetvalue = 0,
2338 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2339 offsetoflow32(CPUARMState, cp15.par_ns) },
2340 .writefn = par_write },
2341 #ifndef CONFIG_USER_ONLY
2342 /* This underdecoding is safe because the reginfo is NO_RAW. */
2343 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
2344 .access = PL1_W, .accessfn = ats_access,
2345 .writefn = ats_write, .type = ARM_CP_NO_RAW },
2346 #endif
2347 REGINFO_SENTINEL
2348 };
2349
2350 /* Return basic MPU access permission bits. */
2351 static uint32_t simple_mpu_ap_bits(uint32_t val)
2352 {
2353 uint32_t ret;
2354 uint32_t mask;
2355 int i;
2356 ret = 0;
2357 mask = 3;
2358 for (i = 0; i < 16; i += 2) {
2359 ret |= (val >> i) & mask;
2360 mask <<= 2;
2361 }
2362 return ret;
2363 }
2364
2365 /* Pad basic MPU access permission bits to extended format. */
2366 static uint32_t extended_mpu_ap_bits(uint32_t val)
2367 {
2368 uint32_t ret;
2369 uint32_t mask;
2370 int i;
2371 ret = 0;
2372 mask = 3;
2373 for (i = 0; i < 16; i += 2) {
2374 ret |= (val & mask) << i;
2375 mask <<= 2;
2376 }
2377 return ret;
2378 }
2379
2380 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2381 uint64_t value)
2382 {
2383 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
2384 }
2385
2386 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2387 {
2388 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
2389 }
2390
2391 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2392 uint64_t value)
2393 {
2394 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
2395 }
2396
2397 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2398 {
2399 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
2400 }
2401
2402 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2403 {
2404 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2405
2406 if (!u32p) {
2407 return 0;
2408 }
2409
2410 u32p += env->pmsav7.rnr[M_REG_NS];
2411 return *u32p;
2412 }
2413
2414 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2415 uint64_t value)
2416 {
2417 ARMCPU *cpu = arm_env_get_cpu(env);
2418 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2419
2420 if (!u32p) {
2421 return;
2422 }
2423
2424 u32p += env->pmsav7.rnr[M_REG_NS];
2425 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
2426 *u32p = value;
2427 }
2428
2429 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2430 uint64_t value)
2431 {
2432 ARMCPU *cpu = arm_env_get_cpu(env);
2433 uint32_t nrgs = cpu->pmsav7_dregion;
2434
2435 if (value >= nrgs) {
2436 qemu_log_mask(LOG_GUEST_ERROR,
2437 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2438 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2439 return;
2440 }
2441
2442 raw_write(env, ri, value);
2443 }
2444
2445 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2446 /* Reset for all these registers is handled in arm_cpu_reset(),
2447 * because the PMSAv7 is also used by M-profile CPUs, which do
2448 * not register cpregs but still need the state to be reset.
2449 */
2450 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2451 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2452 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2453 .readfn = pmsav7_read, .writefn = pmsav7_write,
2454 .resetfn = arm_cp_reset_ignore },
2455 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2456 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2457 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2458 .readfn = pmsav7_read, .writefn = pmsav7_write,
2459 .resetfn = arm_cp_reset_ignore },
2460 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2461 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2462 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2463 .readfn = pmsav7_read, .writefn = pmsav7_write,
2464 .resetfn = arm_cp_reset_ignore },
2465 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2466 .access = PL1_RW,
2467 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
2468 .writefn = pmsav7_rgnr_write,
2469 .resetfn = arm_cp_reset_ignore },
2470 REGINFO_SENTINEL
2471 };
2472
2473 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2474 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2475 .access = PL1_RW, .type = ARM_CP_ALIAS,
2476 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2477 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2478 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2479 .access = PL1_RW, .type = ARM_CP_ALIAS,
2480 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2481 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2482 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2483 .access = PL1_RW,
2484 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2485 .resetvalue = 0, },
2486 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2487 .access = PL1_RW,
2488 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2489 .resetvalue = 0, },
2490 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2491 .access = PL1_RW,
2492 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2493 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2494 .access = PL1_RW,
2495 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
2496 /* Protection region base and size registers */
2497 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2498 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2499 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2500 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2501 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2502 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2503 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2504 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2505 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2506 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2507 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2508 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2509 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2510 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2511 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2512 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2513 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2514 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2515 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2516 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2517 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2518 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2519 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2520 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
2521 REGINFO_SENTINEL
2522 };
2523
2524 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2525 uint64_t value)
2526 {
2527 TCR *tcr = raw_ptr(env, ri);
2528 int maskshift = extract32(value, 0, 3);
2529
2530 if (!arm_feature(env, ARM_FEATURE_V8)) {
2531 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2532 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2533 * using Long-desciptor translation table format */
2534 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2535 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2536 /* In an implementation that includes the Security Extensions
2537 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2538 * Short-descriptor translation table format.
2539 */
2540 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2541 } else {
2542 value &= TTBCR_N;
2543 }
2544 }
2545
2546 /* Update the masks corresponding to the TCR bank being written
2547 * Note that we always calculate mask and base_mask, but
2548 * they are only used for short-descriptor tables (ie if EAE is 0);
2549 * for long-descriptor tables the TCR fields are used differently
2550 * and the mask and base_mask values are meaningless.
2551 */
2552 tcr->raw_tcr = value;
2553 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2554 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
2555 }
2556
2557 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2558 uint64_t value)
2559 {
2560 ARMCPU *cpu = arm_env_get_cpu(env);
2561
2562 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2563 /* With LPAE the TTBCR could result in a change of ASID
2564 * via the TTBCR.A1 bit, so do a TLB flush.
2565 */
2566 tlb_flush(CPU(cpu));
2567 }
2568 vmsa_ttbcr_raw_write(env, ri, value);
2569 }
2570
2571 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2572 {
2573 TCR *tcr = raw_ptr(env, ri);
2574
2575 /* Reset both the TCR as well as the masks corresponding to the bank of
2576 * the TCR being reset.
2577 */
2578 tcr->raw_tcr = 0;
2579 tcr->mask = 0;
2580 tcr->base_mask = 0xffffc000u;
2581 }
2582
2583 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2584 uint64_t value)
2585 {
2586 ARMCPU *cpu = arm_env_get_cpu(env);
2587 TCR *tcr = raw_ptr(env, ri);
2588
2589 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
2590 tlb_flush(CPU(cpu));
2591 tcr->raw_tcr = value;
2592 }
2593
2594 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2595 uint64_t value)
2596 {
2597 /* 64 bit accesses to the TTBRs can change the ASID and so we
2598 * must flush the TLB.
2599 */
2600 if (cpreg_field_is_64bit(ri)) {
2601 ARMCPU *cpu = arm_env_get_cpu(env);
2602
2603 tlb_flush(CPU(cpu));
2604 }
2605 raw_write(env, ri, value);
2606 }
2607
2608 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2609 uint64_t value)
2610 {
2611 ARMCPU *cpu = arm_env_get_cpu(env);
2612 CPUState *cs = CPU(cpu);
2613
2614 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2615 if (raw_read(env, ri) != value) {
2616 tlb_flush_by_mmuidx(cs,
2617 ARMMMUIdxBit_S12NSE1 |
2618 ARMMMUIdxBit_S12NSE0 |
2619 ARMMMUIdxBit_S2NS);
2620 raw_write(env, ri, value);
2621 }
2622 }
2623
2624 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
2625 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2626 .access = PL1_RW, .type = ARM_CP_ALIAS,
2627 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
2628 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
2629 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2630 .access = PL1_RW, .resetvalue = 0,
2631 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2632 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
2633 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2634 .access = PL1_RW, .resetvalue = 0,
2635 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2636 offsetof(CPUARMState, cp15.dfar_ns) } },
2637 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2638 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2639 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2640 .resetvalue = 0, },
2641 REGINFO_SENTINEL
2642 };
2643
2644 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
2645 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2646 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2647 .access = PL1_RW,
2648 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
2649 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
2650 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2651 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2652 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2653 offsetof(CPUARMState, cp15.ttbr0_ns) } },
2654 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
2655 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2656 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2657 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2658 offsetof(CPUARMState, cp15.ttbr1_ns) } },
2659 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2660 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2661 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2662 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2663 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
2664 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2665 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
2666 .raw_writefn = vmsa_ttbcr_raw_write,
2667 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2668 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
2669 REGINFO_SENTINEL
2670 };
2671
2672 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2673 uint64_t value)
2674 {
2675 env->cp15.c15_ticonfig = value & 0xe7;
2676 /* The OS_TYPE bit in this register changes the reported CPUID! */
2677 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2678 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
2679 }
2680
2681 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2682 uint64_t value)
2683 {
2684 env->cp15.c15_threadid = value & 0xffff;
2685 }
2686
2687 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2688 uint64_t value)
2689 {
2690 /* Wait-for-interrupt (deprecated) */
2691 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
2692 }
2693
2694 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2695 uint64_t value)
2696 {
2697 /* On OMAP there are registers indicating the max/min index of dcache lines
2698 * containing a dirty line; cache flush operations have to reset these.
2699 */
2700 env->cp15.c15_i_max = 0x000;
2701 env->cp15.c15_i_min = 0xff0;
2702 }
2703
2704 static const ARMCPRegInfo omap_cp_reginfo[] = {
2705 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2706 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
2707 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
2708 .resetvalue = 0, },
2709 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2710 .access = PL1_RW, .type = ARM_CP_NOP },
2711 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2712 .access = PL1_RW,
2713 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2714 .writefn = omap_ticonfig_write },
2715 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2716 .access = PL1_RW,
2717 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2718 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2719 .access = PL1_RW, .resetvalue = 0xff0,
2720 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2721 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2722 .access = PL1_RW,
2723 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2724 .writefn = omap_threadid_write },
2725 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2726 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2727 .type = ARM_CP_NO_RAW,
2728 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2729 /* TODO: Peripheral port remap register:
2730 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2731 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2732 * when MMU is off.
2733 */
2734 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
2735 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
2736 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
2737 .writefn = omap_cachemaint_write },
2738 { .name = "C9", .cp = 15, .crn = 9,
2739 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2740 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
2741 REGINFO_SENTINEL
2742 };
2743
2744 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2745 uint64_t value)
2746 {
2747 env->cp15.c15_cpar = value & 0x3fff;
2748 }
2749
2750 static const ARMCPRegInfo xscale_cp_reginfo[] = {
2751 { .name = "XSCALE_CPAR",
2752 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2753 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2754 .writefn = xscale_cpar_write, },
2755 { .name = "XSCALE_AUXCR",
2756 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2757 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2758 .resetvalue = 0, },
2759 /* XScale specific cache-lockdown: since we have no cache we NOP these
2760 * and hope the guest does not really rely on cache behaviour.
2761 */
2762 { .name = "XSCALE_LOCK_ICACHE_LINE",
2763 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2764 .access = PL1_W, .type = ARM_CP_NOP },
2765 { .name = "XSCALE_UNLOCK_ICACHE",
2766 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2767 .access = PL1_W, .type = ARM_CP_NOP },
2768 { .name = "XSCALE_DCACHE_LOCK",
2769 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2770 .access = PL1_RW, .type = ARM_CP_NOP },
2771 { .name = "XSCALE_UNLOCK_DCACHE",
2772 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2773 .access = PL1_W, .type = ARM_CP_NOP },
2774 REGINFO_SENTINEL
2775 };
2776
2777 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2778 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2779 * implementation of this implementation-defined space.
2780 * Ideally this should eventually disappear in favour of actually
2781 * implementing the correct behaviour for all cores.
2782 */
2783 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2784 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2785 .access = PL1_RW,
2786 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
2787 .resetvalue = 0 },
2788 REGINFO_SENTINEL
2789 };
2790
2791 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2792 /* Cache status: RAZ because we have no cache so it's always clean */
2793 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
2794 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2795 .resetvalue = 0 },
2796 REGINFO_SENTINEL
2797 };
2798
2799 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2800 /* We never have a a block transfer operation in progress */
2801 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
2802 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2803 .resetvalue = 0 },
2804 /* The cache ops themselves: these all NOP for QEMU */
2805 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2806 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2807 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2808 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2809 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2810 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2811 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2812 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2813 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2814 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2815 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2816 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2817 REGINFO_SENTINEL
2818 };
2819
2820 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2821 /* The cache test-and-clean instructions always return (1 << 30)
2822 * to indicate that there are no dirty cache lines.
2823 */
2824 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2825 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2826 .resetvalue = (1 << 30) },
2827 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2828 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2829 .resetvalue = (1 << 30) },
2830 REGINFO_SENTINEL
2831 };
2832
2833 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2834 /* Ignore ReadBuffer accesses */
2835 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2836 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2837 .access = PL1_RW, .resetvalue = 0,
2838 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2839 REGINFO_SENTINEL
2840 };
2841
2842 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2843 {
2844 ARMCPU *cpu = arm_env_get_cpu(env);
2845 unsigned int cur_el = arm_current_el(env);
2846 bool secure = arm_is_secure(env);
2847
2848 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2849 return env->cp15.vpidr_el2;
2850 }
2851 return raw_read(env, ri);
2852 }
2853
2854 static uint64_t mpidr_read_val(CPUARMState *env)
2855 {
2856 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2857 uint64_t mpidr = cpu->mp_affinity;
2858
2859 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2860 mpidr |= (1U << 31);
2861 /* Cores which are uniprocessor (non-coherent)
2862 * but still implement the MP extensions set
2863 * bit 30. (For instance, Cortex-R5).
2864 */
2865 if (cpu->mp_is_up) {
2866 mpidr |= (1u << 30);
2867 }
2868 }
2869 return mpidr;
2870 }
2871
2872 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2873 {
2874 unsigned int cur_el = arm_current_el(env);
2875 bool secure = arm_is_secure(env);
2876
2877 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2878 return env->cp15.vmpidr_el2;
2879 }
2880 return mpidr_read_val(env);
2881 }
2882
2883 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2884 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2885 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2886 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2887 REGINFO_SENTINEL
2888 };
2889
2890 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2891 /* NOP AMAIR0/1 */
2892 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2893 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2894 .access = PL1_RW, .type = ARM_CP_CONST,
2895 .resetvalue = 0 },
2896 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2897 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2898 .access = PL1_RW, .type = ARM_CP_CONST,
2899 .resetvalue = 0 },
2900 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2901 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2902 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2903 offsetof(CPUARMState, cp15.par_ns)} },
2904 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2905 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2906 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2907 offsetof(CPUARMState, cp15.ttbr0_ns) },
2908 .writefn = vmsa_ttbr_write, },
2909 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2910 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2911 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2912 offsetof(CPUARMState, cp15.ttbr1_ns) },
2913 .writefn = vmsa_ttbr_write, },
2914 REGINFO_SENTINEL
2915 };
2916
2917 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2918 {
2919 return vfp_get_fpcr(env);
2920 }
2921
2922 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2923 uint64_t value)
2924 {
2925 vfp_set_fpcr(env, value);
2926 }
2927
2928 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2929 {
2930 return vfp_get_fpsr(env);
2931 }
2932
2933 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2934 uint64_t value)
2935 {
2936 vfp_set_fpsr(env, value);
2937 }
2938
2939 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2940 bool isread)
2941 {
2942 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2943 return CP_ACCESS_TRAP;
2944 }
2945 return CP_ACCESS_OK;
2946 }
2947
2948 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2949 uint64_t value)
2950 {
2951 env->daif = value & PSTATE_DAIF;
2952 }
2953
2954 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2955 const ARMCPRegInfo *ri,
2956 bool isread)
2957 {
2958 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2959 * SCTLR_EL1.UCI is set.
2960 */
2961 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2962 return CP_ACCESS_TRAP;
2963 }
2964 return CP_ACCESS_OK;
2965 }
2966
2967 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2968 * Page D4-1736 (DDI0487A.b)
2969 */
2970
2971 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972 uint64_t value)
2973 {
2974 CPUState *cs = ENV_GET_CPU(env);
2975
2976 if (arm_is_secure_below_el3(env)) {
2977 tlb_flush_by_mmuidx(cs,
2978 ARMMMUIdxBit_S1SE1 |
2979 ARMMMUIdxBit_S1SE0);
2980 } else {
2981 tlb_flush_by_mmuidx(cs,
2982 ARMMMUIdxBit_S12NSE1 |
2983 ARMMMUIdxBit_S12NSE0);
2984 }
2985 }
2986
2987 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2988 uint64_t value)
2989 {
2990 CPUState *cs = ENV_GET_CPU(env);
2991 bool sec = arm_is_secure_below_el3(env);
2992
2993 if (sec) {
2994 tlb_flush_by_mmuidx_all_cpus_synced(cs,
2995 ARMMMUIdxBit_S1SE1 |
2996 ARMMMUIdxBit_S1SE0);
2997 } else {
2998 tlb_flush_by_mmuidx_all_cpus_synced(cs,
2999 ARMMMUIdxBit_S12NSE1 |
3000 ARMMMUIdxBit_S12NSE0);
3001 }
3002 }
3003
3004 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005 uint64_t value)
3006 {
3007 /* Note that the 'ALL' scope must invalidate both stage 1 and
3008 * stage 2 translations, whereas most other scopes only invalidate
3009 * stage 1 translations.
3010 */
3011 ARMCPU *cpu = arm_env_get_cpu(env);
3012 CPUState *cs = CPU(cpu);
3013
3014 if (arm_is_secure_below_el3(env)) {
3015 tlb_flush_by_mmuidx(cs,
3016 ARMMMUIdxBit_S1SE1 |
3017 ARMMMUIdxBit_S1SE0);
3018 } else {
3019 if (arm_feature(env, ARM_FEATURE_EL2)) {
3020 tlb_flush_by_mmuidx(cs,
3021 ARMMMUIdxBit_S12NSE1 |
3022 ARMMMUIdxBit_S12NSE0 |
3023 ARMMMUIdxBit_S2NS);
3024 } else {
3025 tlb_flush_by_mmuidx(cs,
3026 ARMMMUIdxBit_S12NSE1 |
3027 ARMMMUIdxBit_S12NSE0);
3028 }
3029 }
3030 }
3031
3032 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3033 uint64_t value)
3034 {
3035 ARMCPU *cpu = arm_env_get_cpu(env);
3036 CPUState *cs = CPU(cpu);
3037
3038 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
3039 }
3040
3041 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3042 uint64_t value)
3043 {
3044 ARMCPU *cpu = arm_env_get_cpu(env);
3045 CPUState *cs = CPU(cpu);
3046
3047 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
3048 }
3049
3050 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3051 uint64_t value)
3052 {
3053 /* Note that the 'ALL' scope must invalidate both stage 1 and
3054 * stage 2 translations, whereas most other scopes only invalidate
3055 * stage 1 translations.
3056 */
3057 CPUState *cs = ENV_GET_CPU(env);
3058 bool sec = arm_is_secure_below_el3(env);
3059 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
3060
3061 if (sec) {
3062 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3063 ARMMMUIdxBit_S1SE1 |
3064 ARMMMUIdxBit_S1SE0);
3065 } else if (has_el2) {
3066 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3067 ARMMMUIdxBit_S12NSE1 |
3068 ARMMMUIdxBit_S12NSE0 |
3069 ARMMMUIdxBit_S2NS);
3070 } else {
3071 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3072 ARMMMUIdxBit_S12NSE1 |
3073 ARMMMUIdxBit_S12NSE0);
3074 }
3075 }
3076
3077 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078 uint64_t value)
3079 {
3080 CPUState *cs = ENV_GET_CPU(env);
3081
3082 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
3083 }
3084
3085 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3086 uint64_t value)
3087 {
3088 CPUState *cs = ENV_GET_CPU(env);
3089
3090 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
3091 }
3092
3093 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3094 uint64_t value)
3095 {
3096 /* Invalidate by VA, EL1&0 (AArch64 version).
3097 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3098 * since we don't support flush-for-specific-ASID-only or
3099 * flush-last-level-only.
3100 */
3101 ARMCPU *cpu = arm_env_get_cpu(env);
3102 CPUState *cs = CPU(cpu);
3103 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3104
3105 if (arm_is_secure_below_el3(env)) {
3106 tlb_flush_page_by_mmuidx(cs, pageaddr,
3107 ARMMMUIdxBit_S1SE1 |
3108 ARMMMUIdxBit_S1SE0);
3109 } else {
3110 tlb_flush_page_by_mmuidx(cs, pageaddr,
3111 ARMMMUIdxBit_S12NSE1 |
3112 ARMMMUIdxBit_S12NSE0);
3113 }
3114 }
3115
3116 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3117 uint64_t value)
3118 {
3119 /* Invalidate by VA, EL2
3120 * Currently handles both VAE2 and VALE2, since we don't support
3121 * flush-last-level-only.
3122 */
3123 ARMCPU *cpu = arm_env_get_cpu(env);
3124 CPUState *cs = CPU(cpu);
3125 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3126
3127 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
3128 }
3129
3130 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3131 uint64_t value)
3132 {
3133 /* Invalidate by VA, EL3
3134 * Currently handles both VAE3 and VALE3, since we don't support
3135 * flush-last-level-only.
3136 */
3137 ARMCPU *cpu = arm_env_get_cpu(env);
3138 CPUState *cs = CPU(cpu);
3139 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3140
3141 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
3142 }
3143
3144 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3145 uint64_t value)
3146 {
3147 ARMCPU *cpu = arm_env_get_cpu(env);
3148 CPUState *cs = CPU(cpu);
3149 bool sec = arm_is_secure_below_el3(env);
3150 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3151
3152 if (sec) {
3153 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3154 ARMMMUIdxBit_S1SE1 |
3155 ARMMMUIdxBit_S1SE0);
3156 } else {
3157 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3158 ARMMMUIdxBit_S12NSE1 |
3159 ARMMMUIdxBit_S12NSE0);
3160 }
3161 }
3162
3163 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3164 uint64_t value)
3165 {
3166 CPUState *cs = ENV_GET_CPU(env);
3167 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3168
3169 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3170 ARMMMUIdxBit_S1E2);
3171 }
3172
3173 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3174 uint64_t value)
3175 {
3176 CPUState *cs = ENV_GET_CPU(env);
3177 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3178
3179 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3180 ARMMMUIdxBit_S1E3);
3181 }
3182
3183 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3184 uint64_t value)
3185 {
3186 /* Invalidate by IPA. This has to invalidate any structures that
3187 * contain only stage 2 translation information, but does not need
3188 * to apply to structures that contain combined stage 1 and stage 2
3189 * translation information.
3190 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3191 */
3192 ARMCPU *cpu = arm_env_get_cpu(env);
3193 CPUState *cs = CPU(cpu);
3194 uint64_t pageaddr;
3195
3196 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3197 return;
3198 }
3199
3200 pageaddr = sextract64(value << 12, 0, 48);
3201
3202 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
3203 }
3204
3205 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3206 uint64_t value)
3207 {
3208 CPUState *cs = ENV_GET_CPU(env);
3209 uint64_t pageaddr;
3210
3211 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3212 return;
3213 }
3214
3215 pageaddr = sextract64(value << 12, 0, 48);
3216
3217 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3218 ARMMMUIdxBit_S2NS);
3219 }
3220
3221 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3222 bool isread)
3223 {
3224 /* We don't implement EL2, so the only control on DC ZVA is the
3225 * bit in the SCTLR which can prohibit access for EL0.
3226 */
3227 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
3228 return CP_ACCESS_TRAP;
3229 }
3230 return CP_ACCESS_OK;
3231 }
3232
3233 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3234 {
3235 ARMCPU *cpu = arm_env_get_cpu(env);
3236 int dzp_bit = 1 << 4;
3237
3238 /* DZP indicates whether DC ZVA access is allowed */
3239 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
3240 dzp_bit = 0;
3241 }
3242 return cpu->dcz_blocksize | dzp_bit;
3243 }
3244
3245 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3246 bool isread)
3247 {
3248 if (!(env->pstate & PSTATE_SP)) {
3249 /* Access to SP_EL0 is undefined if it's being used as
3250 * the stack pointer.
3251 */
3252 return CP_ACCESS_TRAP_UNCATEGORIZED;
3253 }
3254 return CP_ACCESS_OK;
3255 }
3256
3257 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3258 {
3259 return env->pstate & PSTATE_SP;
3260 }
3261
3262 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3263 {
3264 update_spsel(env, val);
3265 }
3266
3267 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3268 uint64_t value)
3269 {
3270 ARMCPU *cpu = arm_env_get_cpu(env);
3271
3272 if (raw_read(env, ri) == value) {
3273 /* Skip the TLB flush if nothing actually changed; Linux likes
3274 * to do a lot of pointless SCTLR writes.
3275 */
3276 return;
3277 }
3278
3279 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3280 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3281 value &= ~SCTLR_M;
3282 }
3283
3284 raw_write(env, ri, value);
3285 /* ??? Lots of these bits are not implemented. */
3286 /* This may enable/disable the MMU, so do a TLB flush. */
3287 tlb_flush(CPU(cpu));
3288 }
3289
3290 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3291 bool isread)
3292 {
3293 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
3294 return CP_ACCESS_TRAP_FP_EL2;
3295 }
3296 if (env->cp15.cptr_el[3] & CPTR_TFP) {
3297 return CP_ACCESS_TRAP_FP_EL3;
3298 }
3299 return CP_ACCESS_OK;
3300 }
3301
3302 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3303 uint64_t value)
3304 {
3305 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3306 }
3307
3308 static const ARMCPRegInfo v8_cp_reginfo[] = {
3309 /* Minimal set of EL0-visible registers. This will need to be expanded
3310 * significantly for system emulation of AArch64 CPUs.
3311 */
3312 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3313 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3314 .access = PL0_RW, .type = ARM_CP_NZCV },
3315 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3316 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
3317 .type = ARM_CP_NO_RAW,
3318 .access = PL0_RW, .accessfn = aa64_daif_access,
3319 .fieldoffset = offsetof(CPUARMState, daif),
3320 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
3321 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3322 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3323 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3324 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3325 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3326 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
3327 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3328 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
3329 .access = PL0_R, .type = ARM_CP_NO_RAW,
3330 .readfn = aa64_dczid_read },
3331 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3332 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3333 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3334 #ifndef CONFIG_USER_ONLY
3335 /* Avoid overhead of an access check that always passes in user-mode */
3336 .accessfn = aa64_zva_access,
3337 #endif
3338 },
3339 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3340 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3341 .access = PL1_R, .type = ARM_CP_CURRENTEL },
3342 /* Cache ops: all NOPs since we don't emulate caches */
3343 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3344 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3345 .access = PL1_W, .type = ARM_CP_NOP },
3346 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3347 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3348 .access = PL1_W, .type = ARM_CP_NOP },
3349 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3350 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3351 .access = PL0_W, .type = ARM_CP_NOP,
3352 .accessfn = aa64_cacheop_access },
3353 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3354 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3355 .access = PL1_W, .type = ARM_CP_NOP },
3356 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3357 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3358 .access = PL1_W, .type = ARM_CP_NOP },
3359 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3360 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3361 .access = PL0_W, .type = ARM_CP_NOP,
3362 .accessfn = aa64_cacheop_access },
3363 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3364 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3365 .access = PL1_W, .type = ARM_CP_NOP },
3366 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3367 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3368 .access = PL0_W, .type = ARM_CP_NOP,
3369 .accessfn = aa64_cacheop_access },
3370 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3372 .access = PL0_W, .type = ARM_CP_NOP,
3373 .accessfn = aa64_cacheop_access },
3374 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3375 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3376 .access = PL1_W, .type = ARM_CP_NOP },
3377 /* TLBI operations */
3378 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
3379 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
3380 .access = PL1_W, .type = ARM_CP_NO_RAW,
3381 .writefn = tlbi_aa64_vmalle1is_write },
3382 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
3383 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
3384 .access = PL1_W, .type = ARM_CP_NO_RAW,
3385 .writefn = tlbi_aa64_vae1is_write },
3386 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
3387 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
3388 .access = PL1_W, .type = ARM_CP_NO_RAW,
3389 .writefn = tlbi_aa64_vmalle1is_write },
3390 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
3391 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
3392 .access = PL1_W, .type = ARM_CP_NO_RAW,
3393 .writefn = tlbi_aa64_vae1is_write },
3394 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
3395 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3396 .access = PL1_W, .type = ARM_CP_NO_RAW,
3397 .writefn = tlbi_aa64_vae1is_write },
3398 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
3399 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3400 .access = PL1_W, .type = ARM_CP_NO_RAW,
3401 .writefn = tlbi_aa64_vae1is_write },
3402 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
3403 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
3404 .access = PL1_W, .type = ARM_CP_NO_RAW,
3405 .writefn = tlbi_aa64_vmalle1_write },
3406 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
3407 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
3408 .access = PL1_W, .type = ARM_CP_NO_RAW,
3409 .writefn = tlbi_aa64_vae1_write },
3410 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
3411 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
3412 .access = PL1_W, .type = ARM_CP_NO_RAW,
3413 .writefn = tlbi_aa64_vmalle1_write },
3414 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
3415 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
3416 .access = PL1_W, .type = ARM_CP_NO_RAW,
3417 .writefn = tlbi_aa64_vae1_write },
3418 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
3419 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3420 .access = PL1_W, .type = ARM_CP_NO_RAW,
3421 .writefn = tlbi_aa64_vae1_write },
3422 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
3423 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3424 .access = PL1_W, .type = ARM_CP_NO_RAW,
3425 .writefn = tlbi_aa64_vae1_write },
3426 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3427 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3428 .access = PL2_W, .type = ARM_CP_NO_RAW,
3429 .writefn = tlbi_aa64_ipas2e1is_write },
3430 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3432 .access = PL2_W, .type = ARM_CP_NO_RAW,
3433 .writefn = tlbi_aa64_ipas2e1is_write },
3434 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3435 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3436 .access = PL2_W, .type = ARM_CP_NO_RAW,
3437 .writefn = tlbi_aa64_alle1is_write },
3438 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3439 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3440 .access = PL2_W, .type = ARM_CP_NO_RAW,
3441 .writefn = tlbi_aa64_alle1is_write },
3442 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3443 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3444 .access = PL2_W, .type = ARM_CP_NO_RAW,
3445 .writefn = tlbi_aa64_ipas2e1_write },
3446 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3447 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3448 .access = PL2_W, .type = ARM_CP_NO_RAW,
3449 .writefn = tlbi_aa64_ipas2e1_write },
3450 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3451 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3452 .access = PL2_W, .type = ARM_CP_NO_RAW,
3453 .writefn = tlbi_aa64_alle1_write },
3454 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3455 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3456 .access = PL2_W, .type = ARM_CP_NO_RAW,
3457 .writefn = tlbi_aa64_alle1is_write },
3458 #ifndef CONFIG_USER_ONLY
3459 /* 64 bit address translation operations */
3460 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3461 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3462 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3463 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3464 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3465 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3466 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3467 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3468 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3469 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3470 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3471 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3472 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3473 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3474 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3475 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3476 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3477 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3478 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3479 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3480 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3481 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3482 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3483 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3484 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3485 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3486 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3487 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3488 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3489 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3490 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3491 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3492 .type = ARM_CP_ALIAS,
3493 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3494 .access = PL1_RW, .resetvalue = 0,
3495 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3496 .writefn = par_write },
3497 #endif
3498 /* TLB invalidate last level of translation table walk */
3499 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3500 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3501 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3502 .type = ARM_CP_NO_RAW, .access = PL1_W,
3503 .writefn = tlbimvaa_is_write },
3504 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3505 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3506 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3507 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3508 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3509 .type = ARM_CP_NO_RAW, .access = PL2_W,
3510 .writefn = tlbimva_hyp_write },
3511 { .name = "TLBIMVALHIS",
3512 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3513 .type = ARM_CP_NO_RAW, .access = PL2_W,
3514 .writefn = tlbimva_hyp_is_write },
3515 { .name = "TLBIIPAS2",
3516 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3517 .type = ARM_CP_NO_RAW, .access = PL2_W,
3518 .writefn = tlbiipas2_write },
3519 { .name = "TLBIIPAS2IS",
3520 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3521 .type = ARM_CP_NO_RAW, .access = PL2_W,
3522 .writefn = tlbiipas2_is_write },
3523 { .name = "TLBIIPAS2L",
3524 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3525 .type = ARM_CP_NO_RAW, .access = PL2_W,
3526 .writefn = tlbiipas2_write },
3527 { .name = "TLBIIPAS2LIS",
3528 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3529 .type = ARM_CP_NO_RAW, .access = PL2_W,
3530 .writefn = tlbiipas2_is_write },
3531 /* 32 bit cache operations */
3532 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3533 .type = ARM_CP_NOP, .access = PL1_W },
3534 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3535 .type = ARM_CP_NOP, .access = PL1_W },
3536 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3537 .type = ARM_CP_NOP, .access = PL1_W },
3538 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3539 .type = ARM_CP_NOP, .access = PL1_W },
3540 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3541 .type = ARM_CP_NOP, .access = PL1_W },
3542 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3543 .type = ARM_CP_NOP, .access = PL1_W },
3544 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3545 .type = ARM_CP_NOP, .access = PL1_W },
3546 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3547 .type = ARM_CP_NOP, .access = PL1_W },
3548 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3549 .type = ARM_CP_NOP, .access = PL1_W },
3550 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3551 .type = ARM_CP_NOP, .access = PL1_W },
3552 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3553 .type = ARM_CP_NOP, .access = PL1_W },
3554 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3555 .type = ARM_CP_NOP, .access = PL1_W },
3556 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3557 .type = ARM_CP_NOP, .access = PL1_W },
3558 /* MMU Domain access control / MPU write buffer control */
3559 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3560 .access = PL1_RW, .resetvalue = 0,
3561 .writefn = dacr_write, .raw_writefn = raw_write,
3562 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3563 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3564 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3565 .type = ARM_CP_ALIAS,
3566 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3567 .access = PL1_RW,
3568 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3569 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3570 .type = ARM_CP_ALIAS,
3571 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3572 .access = PL1_RW,
3573 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
3574 /* We rely on the access checks not allowing the guest to write to the
3575 * state field when SPSel indicates that it's being used as the stack
3576 * pointer.
3577 */
3578 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3579 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3580 .access = PL1_RW, .accessfn = sp_el0_access,
3581 .type = ARM_CP_ALIAS,
3582 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3583 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3584 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3585 .access = PL2_RW, .type = ARM_CP_ALIAS,
3586 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3587 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3588 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3589 .type = ARM_CP_NO_RAW,
3590 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3591 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3592 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3593 .type = ARM_CP_ALIAS,
3594 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3595 .access = PL2_RW, .accessfn = fpexc32_access },
3596 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3597 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3598 .access = PL2_RW, .resetvalue = 0,
3599 .writefn = dacr_write, .raw_writefn = raw_write,
3600 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3601 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3602 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3603 .access = PL2_RW, .resetvalue = 0,
3604 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3605 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3606 .type = ARM_CP_ALIAS,
3607 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3608 .access = PL2_RW,
3609 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3610 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3611 .type = ARM_CP_ALIAS,
3612 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3613 .access = PL2_RW,
3614 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3615 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3616 .type = ARM_CP_ALIAS,
3617 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3618 .access = PL2_RW,
3619 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3620 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3621 .type = ARM_CP_ALIAS,
3622 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3623 .access = PL2_RW,
3624 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
3625 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3626 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3627 .resetvalue = 0,
3628 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3629 { .name = "SDCR", .type = ARM_CP_ALIAS,
3630 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3631 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3632 .writefn = sdcr_write,
3633 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
3634 REGINFO_SENTINEL
3635 };
3636
3637 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
3638 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3639 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3640 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3641 .access = PL2_RW,
3642 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3643 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3644 .type = ARM_CP_NO_RAW,
3645 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3646 .access = PL2_RW,
3647 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3648 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3649 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3650 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3651 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3652 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3653 .access = PL2_RW, .type = ARM_CP_CONST,
3654 .resetvalue = 0 },
3655 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3656 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3657 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3658 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3659 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3660 .access = PL2_RW, .type = ARM_CP_CONST,
3661 .resetvalue = 0 },
3662 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3663 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3664 .access = PL2_RW, .type = ARM_CP_CONST,
3665 .resetvalue = 0 },
3666 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3667 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3668 .access = PL2_RW, .type = ARM_CP_CONST,
3669 .resetvalue = 0 },
3670 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3671 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3672 .access = PL2_RW, .type = ARM_CP_CONST,
3673 .resetvalue = 0 },
3674 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3675 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3676 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3677 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3678 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3679 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3680 .type = ARM_CP_CONST, .resetvalue = 0 },
3681 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3682 .cp = 15, .opc1 = 6, .crm = 2,
3683 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3684 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3685 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3686 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3687 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3688 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3689 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3690 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3691 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3692 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3693 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3694 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3695 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3696 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3697 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3698 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3699 .resetvalue = 0 },
3700 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3701 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3702 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3703 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3704 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3705 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3706 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3707 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3708 .resetvalue = 0 },
3709 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3710 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3711 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3712 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3713 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3714 .resetvalue = 0 },
3715 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3716 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3717 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3718 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3719 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3720 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3721 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3722 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3723 .access = PL2_RW, .accessfn = access_tda,
3724 .type = ARM_CP_CONST, .resetvalue = 0 },
3725 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3726 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3727 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3728 .type = ARM_CP_CONST, .resetvalue = 0 },
3729 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3730 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3731 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3732 REGINFO_SENTINEL
3733 };
3734
3735 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3736 {
3737 ARMCPU *cpu = arm_env_get_cpu(env);
3738 uint64_t valid_mask = HCR_MASK;
3739
3740 if (arm_feature(env, ARM_FEATURE_EL3)) {
3741 valid_mask &= ~HCR_HCD;
3742 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3743 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3744 * However, if we're using the SMC PSCI conduit then QEMU is
3745 * effectively acting like EL3 firmware and so the guest at
3746 * EL2 should retain the ability to prevent EL1 from being
3747 * able to make SMC calls into the ersatz firmware, so in
3748 * that case HCR.TSC should be read/write.
3749 */
3750 valid_mask &= ~HCR_TSC;
3751 }
3752
3753 /* Clear RES0 bits. */
3754 value &= valid_mask;
3755
3756 /* These bits change the MMU setup:
3757 * HCR_VM enables stage 2 translation
3758 * HCR_PTW forbids certain page-table setups
3759 * HCR_DC Disables stage1 and enables stage2 translation
3760 */
3761 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3762 tlb_flush(CPU(cpu));
3763 }
3764 raw_write(env, ri, value);
3765 }
3766
3767 static const ARMCPRegInfo el2_cp_reginfo[] = {
3768 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3769 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3770 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3771 .writefn = hcr_write },
3772 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3773 .type = ARM_CP_ALIAS,
3774 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3775 .access = PL2_RW,
3776 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3777 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3778 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3779 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3780 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3781 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3782 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3783 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3784 .type = ARM_CP_ALIAS,
3785 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3786 .access = PL2_RW,
3787 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
3788 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3789 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3790 .access = PL2_RW, .writefn = vbar_write,
3791 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3792 .resetvalue = 0 },
3793 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3794 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3795 .access = PL3_RW, .type = ARM_CP_ALIAS,
3796 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3797 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3798 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3799 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3800 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3801 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3802 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3803 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3804 .resetvalue = 0 },
3805 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3806 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3807 .access = PL2_RW, .type = ARM_CP_ALIAS,
3808 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3809 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3810 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3811 .access = PL2_RW, .type = ARM_CP_CONST,
3812 .resetvalue = 0 },
3813 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3814 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3815 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3816 .access = PL2_RW, .type = ARM_CP_CONST,
3817 .resetvalue = 0 },
3818 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3819 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3820 .access = PL2_RW, .type = ARM_CP_CONST,
3821 .resetvalue = 0 },
3822 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3823 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3824 .access = PL2_RW, .type = ARM_CP_CONST,
3825 .resetvalue = 0 },
3826 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3827 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3828 .access = PL2_RW,
3829 /* no .writefn needed as this can't cause an ASID change;
3830 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3831 */
3832 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3833 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3834 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3835 .type = ARM_CP_ALIAS,
3836 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3837 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3838 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3839 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3840 .access = PL2_RW,
3841 /* no .writefn needed as this can't cause an ASID change;
3842 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3843 */
3844 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3845 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3846 .cp = 15, .opc1 = 6, .crm = 2,
3847 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3848 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3849 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3850 .writefn = vttbr_write },
3851 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3852 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3853 .access = PL2_RW, .writefn = vttbr_write,
3854 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3855 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3856 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3857 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3858 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3859 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3860 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3861 .access = PL2_RW, .resetvalue = 0,
3862 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3863 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3864 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3865 .access = PL2_RW, .resetvalue = 0,
3866 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3867 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3868 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3869 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3870 { .name = "TLBIALLNSNH",
3871 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3872 .type = ARM_CP_NO_RAW, .access = PL2_W,
3873 .writefn = tlbiall_nsnh_write },
3874 { .name = "TLBIALLNSNHIS",
3875 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3876 .type = ARM_CP_NO_RAW, .access = PL2_W,
3877 .writefn = tlbiall_nsnh_is_write },
3878 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3879 .type = ARM_CP_NO_RAW, .access = PL2_W,
3880 .writefn = tlbiall_hyp_write },
3881 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3882 .type = ARM_CP_NO_RAW, .access = PL2_W,
3883 .writefn = tlbiall_hyp_is_write },
3884 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3885 .type = ARM_CP_NO_RAW, .access = PL2_W,
3886 .writefn = tlbimva_hyp_write },
3887 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3888 .type = ARM_CP_NO_RAW, .access = PL2_W,
3889 .writefn = tlbimva_hyp_is_write },
3890 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3892 .type = ARM_CP_NO_RAW, .access = PL2_W,
3893 .writefn = tlbi_aa64_alle2_write },
3894 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3895 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3896 .type = ARM_CP_NO_RAW, .access = PL2_W,
3897 .writefn = tlbi_aa64_vae2_write },
3898 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3899 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3900 .access = PL2_W, .type = ARM_CP_NO_RAW,
3901 .writefn = tlbi_aa64_vae2_write },
3902 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3903 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3904 .access = PL2_W, .type = ARM_CP_NO_RAW,
3905 .writefn = tlbi_aa64_alle2is_write },
3906 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3907 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3908 .type = ARM_CP_NO_RAW, .access = PL2_W,
3909 .writefn = tlbi_aa64_vae2is_write },
3910 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3911 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3912 .access = PL2_W, .type = ARM_CP_NO_RAW,
3913 .writefn = tlbi_aa64_vae2is_write },
3914 #ifndef CONFIG_USER_ONLY
3915 /* Unlike the other EL2-related AT operations, these must
3916 * UNDEF from EL3 if EL2 is not implemented, which is why we
3917 * define them here rather than with the rest of the AT ops.
3918 */
3919 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3920 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3921 .access = PL2_W, .accessfn = at_s1e2_access,
3922 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3923 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3924 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3925 .access = PL2_W, .accessfn = at_s1e2_access,
3926 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3927 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3928 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3929 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3930 * to behave as if SCR.NS was 1.
3931 */
3932 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3933 .access = PL2_W,
3934 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3935 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3936 .access = PL2_W,
3937 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3938 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3939 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3940 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3941 * reset values as IMPDEF. We choose to reset to 3 to comply with
3942 * both ARMv7 and ARMv8.
3943 */
3944 .access = PL2_RW, .resetvalue = 3,
3945 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
3946 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3947 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3948 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3949 .writefn = gt_cntvoff_write,
3950 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3951 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3952 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3953 .writefn = gt_cntvoff_write,
3954 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3955 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3956 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3957 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3958 .type = ARM_CP_IO, .access = PL2_RW,
3959 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3960 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3961 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3962 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3963 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3964 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3965 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3966 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
3967 .resetfn = gt_hyp_timer_reset,
3968 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3969 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3970 .type = ARM_CP_IO,
3971 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3972 .access = PL2_RW,
3973 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3974 .resetvalue = 0,
3975 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
3976 #endif
3977 /* The only field of MDCR_EL2 that has a defined architectural reset value
3978 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3979 * don't impelment any PMU event counters, so using zero as a reset
3980 * value for MDCR_EL2 is okay
3981 */
3982 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3983 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3984 .access = PL2_RW, .resetvalue = 0,
3985 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
3986 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3987 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3988 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3989 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3990 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3991 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3992 .access = PL2_RW,
3993 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3994 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3995 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3996 .access = PL2_RW,
3997 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3998 REGINFO_SENTINEL
3999 };
4000
4001 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4002 bool isread)
4003 {
4004 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4005 * At Secure EL1 it traps to EL3.
4006 */
4007 if (arm_current_el(env) == 3) {
4008 return CP_ACCESS_OK;
4009 }
4010 if (arm_is_secure_below_el3(env)) {
4011 return CP_ACCESS_TRAP_EL3;
4012 }
4013 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4014 if (isread) {
4015 return CP_ACCESS_OK;
4016 }
4017 return CP_ACCESS_TRAP_UNCATEGORIZED;
4018 }
4019
4020 static const ARMCPRegInfo el3_cp_reginfo[] = {
4021 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4022 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4023 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4024 .resetvalue = 0, .writefn = scr_write },
4025 { .name = "SCR", .type = ARM_CP_ALIAS,
4026 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
4027 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4028 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
4029 .writefn = scr_write },
4030 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4031 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4032 .access = PL3_RW, .resetvalue = 0,
4033 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4034 { .name = "SDER",
4035 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4036 .access = PL3_RW, .resetvalue = 0,
4037 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
4038 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4039 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4040 .writefn = vbar_write, .resetvalue = 0,
4041 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
4042 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4043 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4044 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4045 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
4046 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4047 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
4048 .access = PL3_RW,
4049 /* no .writefn needed as this can't cause an ASID change;
4050 * we must provide a .raw_writefn and .resetfn because we handle
4051 * reset and migration for the AArch32 TTBCR(S), which might be
4052 * using mask and base_mask.
4053 */
4054 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
4055 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
4056 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
4057 .type = ARM_CP_ALIAS,
4058 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4059 .access = PL3_RW,
4060 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
4061 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
4062 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4063 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
4064 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4065 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4066 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
4067 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
4068 .type = ARM_CP_ALIAS,
4069 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
4070 .access = PL3_RW,
4071 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
4072 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4073 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4074 .access = PL3_RW, .writefn = vbar_write,
4075 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4076 .resetvalue = 0 },
4077 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4078 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4079 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4080 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4081 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4082 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4083 .access = PL3_RW, .resetvalue = 0,
4084 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
4085 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4086 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4087 .access = PL3_RW, .type = ARM_CP_CONST,
4088 .resetvalue = 0 },
4089 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4090 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4091 .access = PL3_RW, .type = ARM_CP_CONST,
4092 .resetvalue = 0 },
4093 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4094 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4095 .access = PL3_RW, .type = ARM_CP_CONST,
4096 .resetvalue = 0 },
4097 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4098 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4099 .access = PL3_W, .type = ARM_CP_NO_RAW,
4100 .writefn = tlbi_aa64_alle3is_write },
4101 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4102 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4103 .access = PL3_W, .type = ARM_CP_NO_RAW,
4104 .writefn = tlbi_aa64_vae3is_write },
4105 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4106 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4107 .access = PL3_W, .type = ARM_CP_NO_RAW,
4108 .writefn = tlbi_aa64_vae3is_write },
4109 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4110 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4111 .access = PL3_W, .type = ARM_CP_NO_RAW,
4112 .writefn = tlbi_aa64_alle3_write },
4113 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4114 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4115 .access = PL3_W, .type = ARM_CP_NO_RAW,
4116 .writefn = tlbi_aa64_vae3_write },
4117 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4118 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4119 .access = PL3_W, .type = ARM_CP_NO_RAW,
4120 .writefn = tlbi_aa64_vae3_write },
4121 REGINFO_SENTINEL
4122 };
4123
4124 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4125 bool isread)
4126 {
4127 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4128 * but the AArch32 CTR has its own reginfo struct)
4129 */
4130 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
4131 return CP_ACCESS_TRAP;
4132 }
4133 return CP_ACCESS_OK;
4134 }
4135
4136 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4137 uint64_t value)
4138 {
4139 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4140 * read via a bit in OSLSR_EL1.
4141 */
4142 int oslock;
4143
4144 if (ri->state == ARM_CP_STATE_AA32) {
4145 oslock = (value == 0xC5ACCE55);
4146 } else {
4147 oslock = value & 1;
4148 }
4149
4150 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4151 }
4152
4153 static const ARMCPRegInfo debug_cp_reginfo[] = {
4154 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
4155 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4156 * unlike DBGDRAR it is never accessible from EL0.
4157 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4158 * accessor.
4159 */
4160 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
4161 .access = PL0_R, .accessfn = access_tdra,
4162 .type = ARM_CP_CONST, .resetvalue = 0 },
4163 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4164 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4165 .access = PL1_R, .accessfn = access_tdra,
4166 .type = ARM_CP_CONST, .resetvalue = 0 },
4167 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4168 .access = PL0_R, .accessfn = access_tdra,
4169 .type = ARM_CP_CONST, .resetvalue = 0 },
4170 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
4171 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4172 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4173 .access = PL1_RW, .accessfn = access_tda,
4174 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4175 .resetvalue = 0 },
4176 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4177 * We don't implement the configurable EL0 access.
4178 */
4179 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4180 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4181 .type = ARM_CP_ALIAS,
4182 .access = PL1_R, .accessfn = access_tda,
4183 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
4184 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4185 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
4186 .access = PL1_W, .type = ARM_CP_NO_RAW,
4187 .accessfn = access_tdosa,
4188 .writefn = oslar_write },
4189 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4190 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4191 .access = PL1_R, .resetvalue = 10,
4192 .accessfn = access_tdosa,
4193 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
4194 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4195 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4196 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
4197 .access = PL1_RW, .accessfn = access_tdosa,
4198 .type = ARM_CP_NOP },
4199 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4200 * implement vector catch debug events yet.
4201 */
4202 { .name = "DBGVCR",
4203 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4204 .access = PL1_RW, .accessfn = access_tda,
4205 .type = ARM_CP_NOP },
4206 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4207 * to save and restore a 32-bit guest's DBGVCR)
4208 */
4209 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4210 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4211 .access = PL2_RW, .accessfn = access_tda,
4212 .type = ARM_CP_NOP },
4213 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4214 * Channel but Linux may try to access this register. The 32-bit
4215 * alias is DBGDCCINT.
4216 */
4217 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4218 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4219 .access = PL1_RW, .accessfn = access_tda,
4220 .type = ARM_CP_NOP },
4221 REGINFO_SENTINEL
4222 };
4223
4224 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4225 /* 64 bit access versions of the (dummy) debug registers */
4226 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4227 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4228 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4229 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4230 REGINFO_SENTINEL
4231 };
4232
4233 void hw_watchpoint_update(ARMCPU *cpu, int n)
4234 {
4235 CPUARMState *env = &cpu->env;
4236 vaddr len = 0;
4237 vaddr wvr = env->cp15.dbgwvr[n];
4238 uint64_t wcr = env->cp15.dbgwcr[n];
4239 int mask;
4240 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4241
4242 if (env->cpu_watchpoint[n]) {
4243 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4244 env->cpu_watchpoint[n] = NULL;
4245 }
4246
4247 if (!extract64(wcr, 0, 1)) {
4248 /* E bit clear : watchpoint disabled */
4249 return;
4250 }
4251
4252 switch (extract64(wcr, 3, 2)) {
4253 case 0:
4254 /* LSC 00 is reserved and must behave as if the wp is disabled */
4255 return;
4256 case 1:
4257 flags |= BP_MEM_READ;
4258 break;
4259 case 2:
4260 flags |= BP_MEM_WRITE;
4261 break;
4262 case 3:
4263 flags |= BP_MEM_ACCESS;
4264 break;
4265 }
4266
4267 /* Attempts to use both MASK and BAS fields simultaneously are
4268 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4269 * thus generating a watchpoint for every byte in the masked region.
4270 */
4271 mask = extract64(wcr, 24, 4);
4272 if (mask == 1 || mask == 2) {
4273 /* Reserved values of MASK; we must act as if the mask value was
4274 * some non-reserved value, or as if the watchpoint were disabled.
4275 * We choose the latter.
4276 */
4277 return;
4278 } else if (mask) {
4279 /* Watchpoint covers an aligned area up to 2GB in size */
4280 len = 1ULL << mask;
4281 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4282 * whether the watchpoint fires when the unmasked bits match; we opt
4283 * to generate the exceptions.
4284 */
4285 wvr &= ~(len - 1);
4286 } else {
4287 /* Watchpoint covers bytes defined by the byte address select bits */
4288 int bas = extract64(wcr, 5, 8);
4289 int basstart;
4290
4291 if (bas == 0) {
4292 /* This must act as if the watchpoint is disabled */
4293 return;
4294 }
4295
4296 if (extract64(wvr, 2, 1)) {
4297 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4298 * ignored, and BAS[3:0] define which bytes to watch.
4299 */
4300 bas &= 0xf;
4301 }
4302 /* The BAS bits are supposed to be programmed to indicate a contiguous
4303 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4304 * we fire for each byte in the word/doubleword addressed by the WVR.
4305 * We choose to ignore any non-zero bits after the first range of 1s.
4306 */
4307 basstart = ctz32(bas);
4308 len = cto32(bas >> basstart);
4309 wvr += basstart;
4310 }
4311
4312 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4313 &env->cpu_watchpoint[n]);
4314 }
4315
4316 void hw_watchpoint_update_all(ARMCPU *cpu)
4317 {
4318 int i;
4319 CPUARMState *env = &cpu->env;
4320
4321 /* Completely clear out existing QEMU watchpoints and our array, to
4322 * avoid possible stale entries following migration load.
4323 */
4324 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4325 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4326
4327 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4328 hw_watchpoint_update(cpu, i);
4329 }
4330 }
4331
4332 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4333 uint64_t value)
4334 {
4335 ARMCPU *cpu = arm_env_get_cpu(env);
4336 int i = ri->crm;
4337
4338 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4339 * register reads and behaves as if values written are sign extended.
4340 * Bits [1:0] are RES0.
4341 */
4342 value = sextract64(value, 0, 49) & ~3ULL;
4343
4344 raw_write(env, ri, value);
4345 hw_watchpoint_update(cpu, i);
4346 }
4347
4348 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4349 uint64_t value)
4350 {
4351 ARMCPU *cpu = arm_env_get_cpu(env);
4352 int i = ri->crm;
4353
4354 raw_write(env, ri, value);
4355 hw_watchpoint_update(cpu, i);
4356 }
4357
4358 void hw_breakpoint_update(ARMCPU *cpu, int n)
4359 {
4360 CPUARMState *env = &cpu->env;
4361 uint64_t bvr = env->cp15.dbgbvr[n];
4362 uint64_t bcr = env->cp15.dbgbcr[n];
4363 vaddr addr;
4364 int bt;
4365 int flags = BP_CPU;
4366
4367 if (env->cpu_breakpoint[n]) {
4368 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4369 env->cpu_breakpoint[n] = NULL;
4370 }
4371
4372 if (!extract64(bcr, 0, 1)) {
4373 /* E bit clear : watchpoint disabled */
4374 return;
4375 }
4376
4377 bt = extract64(bcr, 20, 4);
4378
4379 switch (bt) {
4380 case 4: /* unlinked address mismatch (reserved if AArch64) */
4381 case 5: /* linked address mismatch (reserved if AArch64) */
4382 qemu_log_mask(LOG_UNIMP,
4383 "arm: address mismatch breakpoint types not implemented");
4384 return;
4385 case 0: /* unlinked address match */
4386 case 1: /* linked address match */
4387 {
4388 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4389 * we behave as if the register was sign extended. Bits [1:0] are
4390 * RES0. The BAS field is used to allow setting breakpoints on 16
4391 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4392 * a bp will fire if the addresses covered by the bp and the addresses
4393 * covered by the insn overlap but the insn doesn't start at the
4394 * start of the bp address range. We choose to require the insn and
4395 * the bp to have the same address. The constraints on writing to
4396 * BAS enforced in dbgbcr_write mean we have only four cases:
4397 * 0b0000 => no breakpoint
4398 * 0b0011 => breakpoint on addr
4399 * 0b1100 => breakpoint on addr + 2
4400 * 0b1111 => breakpoint on addr
4401 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4402 */
4403 int bas = extract64(bcr, 5, 4);
4404 addr = sextract64(bvr, 0, 49) & ~3ULL;
4405 if (bas == 0) {
4406 return;
4407 }
4408 if (bas == 0xc) {
4409 addr += 2;
4410 }
4411 break;
4412 }
4413 case 2: /* unlinked context ID match */
4414 case 8: /* unlinked VMID match (reserved if no EL2) */
4415 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4416 qemu_log_mask(LOG_UNIMP,
4417 "arm: unlinked context breakpoint types not implemented");
4418 return;
4419 case 9: /* linked VMID match (reserved if no EL2) */
4420 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4421 case 3: /* linked context ID match */
4422 default:
4423 /* We must generate no events for Linked context matches (unless
4424 * they are linked to by some other bp/wp, which is handled in
4425 * updates for the linking bp/wp). We choose to also generate no events
4426 * for reserved values.
4427 */
4428 return;
4429 }
4430
4431 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4432 }
4433
4434 void hw_breakpoint_update_all(ARMCPU *cpu)
4435 {
4436 int i;
4437 CPUARMState *env = &cpu->env;
4438
4439 /* Completely clear out existing QEMU breakpoints and our array, to
4440 * avoid possible stale entries following migration load.
4441 */
4442 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4443 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4444
4445 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4446 hw_breakpoint_update(cpu, i);
4447 }
4448 }
4449
4450 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4451 uint64_t value)
4452 {
4453 ARMCPU *cpu = arm_env_get_cpu(env);
4454 int i = ri->crm;
4455
4456 raw_write(env, ri, value);
4457 hw_breakpoint_update(cpu, i);
4458 }
4459
4460 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4461 uint64_t value)
4462 {
4463 ARMCPU *cpu = arm_env_get_cpu(env);
4464 int i = ri->crm;
4465
4466 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4467 * copy of BAS[0].
4468 */
4469 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4470 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4471
4472 raw_write(env, ri, value);
4473 hw_breakpoint_update(cpu, i);
4474 }
4475
4476 static void define_debug_regs(ARMCPU *cpu)
4477 {
4478 /* Define v7 and v8 architectural debug registers.
4479 * These are just dummy implementations for now.
4480 */
4481 int i;
4482 int wrps, brps, ctx_cmps;
4483 ARMCPRegInfo dbgdidr = {
4484 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
4485 .access = PL0_R, .accessfn = access_tda,
4486 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
4487 };
4488
4489 /* Note that all these register fields hold "number of Xs minus 1". */
4490 brps = extract32(cpu->dbgdidr, 24, 4);
4491 wrps = extract32(cpu->dbgdidr, 28, 4);
4492 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4493
4494 assert(ctx_cmps <= brps);
4495
4496 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4497 * of the debug registers such as number of breakpoints;
4498 * check that if they both exist then they agree.
4499 */
4500 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4501 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4502 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
4503 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
4504 }
4505
4506 define_one_arm_cp_reg(cpu, &dbgdidr);
4507 define_arm_cp_regs(cpu, debug_cp_reginfo);
4508
4509 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4510 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4511 }
4512
4513 for (i = 0; i < brps + 1; i++) {
4514 ARMCPRegInfo dbgregs[] = {
4515 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4516 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
4517 .access = PL1_RW, .accessfn = access_tda,
4518 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4519 .writefn = dbgbvr_write, .raw_writefn = raw_write
4520 },
4521 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4522 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
4523 .access = PL1_RW, .accessfn = access_tda,
4524 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4525 .writefn = dbgbcr_write, .raw_writefn = raw_write
4526 },
4527 REGINFO_SENTINEL
4528 };
4529 define_arm_cp_regs(cpu, dbgregs);
4530 }
4531
4532 for (i = 0; i < wrps + 1; i++) {
4533 ARMCPRegInfo dbgregs[] = {
4534 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4535 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
4536 .access = PL1_RW, .accessfn = access_tda,
4537 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4538 .writefn = dbgwvr_write, .raw_writefn = raw_write
4539 },
4540 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4541 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
4542 .access = PL1_RW, .accessfn = access_tda,
4543 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4544 .writefn = dbgwcr_write, .raw_writefn = raw_write
4545 },
4546 REGINFO_SENTINEL
4547 };
4548 define_arm_cp_regs(cpu, dbgregs);
4549 }
4550 }
4551
4552 void register_cp_regs_for_features(ARMCPU *cpu)
4553 {
4554 /* Register all the coprocessor registers based on feature bits */
4555 CPUARMState *env = &cpu->env;
4556 if (arm_feature(env, ARM_FEATURE_M)) {
4557 /* M profile has no coprocessor registers */
4558 return;
4559 }
4560
4561 define_arm_cp_regs(cpu, cp_reginfo);
4562 if (!arm_feature(env, ARM_FEATURE_V8)) {
4563 /* Must go early as it is full of wildcards that may be
4564 * overridden by later definitions.
4565 */
4566 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4567 }
4568
4569 if (arm_feature(env, ARM_FEATURE_V6)) {
4570 /* The ID registers all have impdef reset values */
4571 ARMCPRegInfo v6_idregs[] = {
4572 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4573 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4574 .access = PL1_R, .type = ARM_CP_CONST,
4575 .resetvalue = cpu->id_pfr0 },
4576 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4577 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4578 .access = PL1_R, .type = ARM_CP_CONST,
4579 .resetvalue = cpu->id_pfr1 },
4580 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4581 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4582 .access = PL1_R, .type = ARM_CP_CONST,
4583 .resetvalue = cpu->id_dfr0 },
4584 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4585 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4586 .access = PL1_R, .type = ARM_CP_CONST,
4587 .resetvalue = cpu->id_afr0 },
4588 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4589 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4590 .access = PL1_R, .type = ARM_CP_CONST,
4591 .resetvalue = cpu->id_mmfr0 },
4592 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4593 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4594 .access = PL1_R, .type = ARM_CP_CONST,
4595 .resetvalue = cpu->id_mmfr1 },
4596 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4597 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4598 .access = PL1_R, .type = ARM_CP_CONST,
4599 .resetvalue = cpu->id_mmfr2 },
4600 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4601 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4602 .access = PL1_R, .type = ARM_CP_CONST,
4603 .resetvalue = cpu->id_mmfr3 },
4604 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4605 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4606 .access = PL1_R, .type = ARM_CP_CONST,
4607 .resetvalue = cpu->id_isar0 },
4608 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4609 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4610 .access = PL1_R, .type = ARM_CP_CONST,
4611 .resetvalue = cpu->id_isar1 },
4612 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4613 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4614 .access = PL1_R, .type = ARM_CP_CONST,
4615 .resetvalue = cpu->id_isar2 },
4616 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4617 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4618 .access = PL1_R, .type = ARM_CP_CONST,
4619 .resetvalue = cpu->id_isar3 },
4620 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4621 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4622 .access = PL1_R, .type = ARM_CP_CONST,
4623 .resetvalue = cpu->id_isar4 },
4624 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4625 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4626 .access = PL1_R, .type = ARM_CP_CONST,
4627 .resetvalue = cpu->id_isar5 },
4628 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4629 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4630 .access = PL1_R, .type = ARM_CP_CONST,
4631 .resetvalue = cpu->id_mmfr4 },
4632 /* 7 is as yet unallocated and must RAZ */
4633 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4634 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4635 .access = PL1_R, .type = ARM_CP_CONST,
4636 .resetvalue = 0 },
4637 REGINFO_SENTINEL
4638 };
4639 define_arm_cp_regs(cpu, v6_idregs);
4640 define_arm_cp_regs(cpu, v6_cp_reginfo);
4641 } else {
4642 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4643 }
4644 if (arm_feature(env, ARM_FEATURE_V6K)) {
4645 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4646 }
4647 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4648 !arm_feature(env, ARM_FEATURE_PMSA)) {
4649 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4650 }
4651 if (arm_feature(env, ARM_FEATURE_V7)) {
4652 /* v7 performance monitor control register: same implementor
4653 * field as main ID register, and we implement only the cycle
4654 * count register.
4655 */
4656 #ifndef CONFIG_USER_ONLY
4657 ARMCPRegInfo pmcr = {
4658 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4659 .access = PL0_RW,
4660 .type = ARM_CP_IO | ARM_CP_ALIAS,
4661 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4662 .accessfn = pmreg_access, .writefn = pmcr_write,
4663 .raw_writefn = raw_write,
4664 };
4665 ARMCPRegInfo pmcr64 = {
4666 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4667 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4668 .access = PL0_RW, .accessfn = pmreg_access,
4669 .type = ARM_CP_IO,
4670 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4671 .resetvalue = cpu->midr & 0xff000000,
4672 .writefn = pmcr_write, .raw_writefn = raw_write,
4673 };
4674 define_one_arm_cp_reg(cpu, &pmcr);
4675 define_one_arm_cp_reg(cpu, &pmcr64);
4676 #endif
4677 ARMCPRegInfo clidr = {
4678 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4679 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4680 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4681 };
4682 define_one_arm_cp_reg(cpu, &clidr);
4683 define_arm_cp_regs(cpu, v7_cp_reginfo);
4684 define_debug_regs(cpu);
4685 } else {
4686 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4687 }
4688 if (arm_feature(env, ARM_FEATURE_V8)) {
4689 /* AArch64 ID registers, which all have impdef reset values.
4690 * Note that within the ID register ranges the unused slots
4691 * must all RAZ, not UNDEF; future architecture versions may
4692 * define new registers here.
4693 */
4694 ARMCPRegInfo v8_idregs[] = {
4695 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4696 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4697 .access = PL1_R, .type = ARM_CP_CONST,
4698 .resetvalue = cpu->id_aa64pfr0 },
4699 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4700 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4701 .access = PL1_R, .type = ARM_CP_CONST,
4702 .resetvalue = cpu->id_aa64pfr1},
4703 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4704 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4705 .access = PL1_R, .type = ARM_CP_CONST,
4706 .resetvalue = 0 },
4707 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4708 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4709 .access = PL1_R, .type = ARM_CP_CONST,
4710 .resetvalue = 0 },
4711 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4712 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4713 .access = PL1_R, .type = ARM_CP_CONST,
4714 .resetvalue = 0 },
4715 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4716 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4717 .access = PL1_R, .type = ARM_CP_CONST,
4718 .resetvalue = 0 },
4719 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4720 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4721 .access = PL1_R, .type = ARM_CP_CONST,
4722 .resetvalue = 0 },
4723 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4724 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4725 .access = PL1_R, .type = ARM_CP_CONST,
4726 .resetvalue = 0 },
4727 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4728 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4729 .access = PL1_R, .type = ARM_CP_CONST,
4730 .resetvalue = cpu->id_aa64dfr0 },
4731 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4732 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4733 .access = PL1_R, .type = ARM_CP_CONST,
4734 .resetvalue = cpu->id_aa64dfr1 },
4735 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4736 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4737 .access = PL1_R, .type = ARM_CP_CONST,
4738 .resetvalue = 0 },
4739 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4740 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4741 .access = PL1_R, .type = ARM_CP_CONST,
4742 .resetvalue = 0 },
4743 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4745 .access = PL1_R, .type = ARM_CP_CONST,
4746 .resetvalue = cpu->id_aa64afr0 },
4747 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4748 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4749 .access = PL1_R, .type = ARM_CP_CONST,
4750 .resetvalue = cpu->id_aa64afr1 },
4751 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4752 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4753 .access = PL1_R, .type = ARM_CP_CONST,
4754 .resetvalue = 0 },
4755 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4756 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4757 .access = PL1_R, .type = ARM_CP_CONST,
4758 .resetvalue = 0 },
4759 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4760 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4761 .access = PL1_R, .type = ARM_CP_CONST,
4762 .resetvalue = cpu->id_aa64isar0 },
4763 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4765 .access = PL1_R, .type = ARM_CP_CONST,
4766 .resetvalue = cpu->id_aa64isar1 },
4767 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4769 .access = PL1_R, .type = ARM_CP_CONST,
4770 .resetvalue = 0 },
4771 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4773 .access = PL1_R, .type = ARM_CP_CONST,
4774 .resetvalue = 0 },
4775 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4777 .access = PL1_R, .type = ARM_CP_CONST,
4778 .resetvalue = 0 },
4779 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4780 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4781 .access = PL1_R, .type = ARM_CP_CONST,
4782 .resetvalue = 0 },
4783 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4784 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4785 .access = PL1_R, .type = ARM_CP_CONST,
4786 .resetvalue = 0 },
4787 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4789 .access = PL1_R, .type = ARM_CP_CONST,
4790 .resetvalue = 0 },
4791 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4793 .access = PL1_R, .type = ARM_CP_CONST,
4794 .resetvalue = cpu->id_aa64mmfr0 },
4795 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4797 .access = PL1_R, .type = ARM_CP_CONST,
4798 .resetvalue = cpu->id_aa64mmfr1 },
4799 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4801 .access = PL1_R, .type = ARM_CP_CONST,
4802 .resetvalue = 0 },
4803 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4805 .access = PL1_R, .type = ARM_CP_CONST,
4806 .resetvalue = 0 },
4807 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4809 .access = PL1_R, .type = ARM_CP_CONST,
4810 .resetvalue = 0 },
4811 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4812 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4813 .access = PL1_R, .type = ARM_CP_CONST,
4814 .resetvalue = 0 },
4815 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4816 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4817 .access = PL1_R, .type = ARM_CP_CONST,
4818 .resetvalue = 0 },
4819 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4820 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4821 .access = PL1_R, .type = ARM_CP_CONST,
4822 .resetvalue = 0 },
4823 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4824 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4825 .access = PL1_R, .type = ARM_CP_CONST,
4826 .resetvalue = cpu->mvfr0 },
4827 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4829 .access = PL1_R, .type = ARM_CP_CONST,
4830 .resetvalue = cpu->mvfr1 },
4831 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4833 .access = PL1_R, .type = ARM_CP_CONST,
4834 .resetvalue = cpu->mvfr2 },
4835 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4836 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4837 .access = PL1_R, .type = ARM_CP_CONST,
4838 .resetvalue = 0 },
4839 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4840 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4841 .access = PL1_R, .type = ARM_CP_CONST,
4842 .resetvalue = 0 },
4843 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4844 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4845 .access = PL1_R, .type = ARM_CP_CONST,
4846 .resetvalue = 0 },
4847 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4849 .access = PL1_R, .type = ARM_CP_CONST,
4850 .resetvalue = 0 },
4851 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4853 .access = PL1_R, .type = ARM_CP_CONST,
4854 .resetvalue = 0 },
4855 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4856 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4857 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4858 .resetvalue = cpu->pmceid0 },
4859 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4861 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4862 .resetvalue = cpu->pmceid0 },
4863 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4864 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4865 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4866 .resetvalue = cpu->pmceid1 },
4867 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4868 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4869 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4870 .resetvalue = cpu->pmceid1 },
4871 REGINFO_SENTINEL
4872 };
4873 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4874 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4875 !arm_feature(env, ARM_FEATURE_EL2)) {
4876 ARMCPRegInfo rvbar = {
4877 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4878 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4879 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4880 };
4881 define_one_arm_cp_reg(cpu, &rvbar);
4882 }
4883 define_arm_cp_regs(cpu, v8_idregs);
4884 define_arm_cp_regs(cpu, v8_cp_reginfo);
4885 }
4886 if (arm_feature(env, ARM_FEATURE_EL2)) {
4887 uint64_t vmpidr_def = mpidr_read_val(env);
4888 ARMCPRegInfo vpidr_regs[] = {
4889 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4890 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4891 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4892 .resetvalue = cpu->midr,
4893 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4894 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4895 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4896 .access = PL2_RW, .resetvalue = cpu->midr,
4897 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4898 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4899 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4900 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4901 .resetvalue = vmpidr_def,
4902 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4903 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4904 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4905 .access = PL2_RW,
4906 .resetvalue = vmpidr_def,
4907 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4908 REGINFO_SENTINEL
4909 };
4910 define_arm_cp_regs(cpu, vpidr_regs);
4911 define_arm_cp_regs(cpu, el2_cp_reginfo);
4912 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4913 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4914 ARMCPRegInfo rvbar = {
4915 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4916 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4917 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4918 };
4919 define_one_arm_cp_reg(cpu, &rvbar);
4920 }
4921 } else {
4922 /* If EL2 is missing but higher ELs are enabled, we need to
4923 * register the no_el2 reginfos.
4924 */
4925 if (arm_feature(env, ARM_FEATURE_EL3)) {
4926 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4927 * of MIDR_EL1 and MPIDR_EL1.
4928 */
4929 ARMCPRegInfo vpidr_regs[] = {
4930 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4931 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4932 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4933 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4934 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4935 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4936 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4937 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4938 .type = ARM_CP_NO_RAW,
4939 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
4940 REGINFO_SENTINEL
4941 };
4942 define_arm_cp_regs(cpu, vpidr_regs);
4943 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
4944 }
4945 }
4946 if (arm_feature(env, ARM_FEATURE_EL3)) {
4947 define_arm_cp_regs(cpu, el3_cp_reginfo);
4948 ARMCPRegInfo el3_regs[] = {
4949 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4950 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4951 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
4952 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
4953 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
4954 .access = PL3_RW,
4955 .raw_writefn = raw_write, .writefn = sctlr_write,
4956 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
4957 .resetvalue = cpu->reset_sctlr },
4958 REGINFO_SENTINEL
4959 };
4960
4961 define_arm_cp_regs(cpu, el3_regs);
4962 }
4963 /* The behaviour of NSACR is sufficiently various that we don't
4964 * try to describe it in a single reginfo:
4965 * if EL3 is 64 bit, then trap to EL3 from S EL1,
4966 * reads as constant 0xc00 from NS EL1 and NS EL2
4967 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
4968 * if v7 without EL3, register doesn't exist
4969 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
4970 */
4971 if (arm_feature(env, ARM_FEATURE_EL3)) {
4972 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4973 ARMCPRegInfo nsacr = {
4974 .name = "NSACR", .type = ARM_CP_CONST,
4975 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4976 .access = PL1_RW, .accessfn = nsacr_access,
4977 .resetvalue = 0xc00
4978 };
4979 define_one_arm_cp_reg(cpu, &nsacr);
4980 } else {
4981 ARMCPRegInfo nsacr = {
4982 .name = "NSACR",
4983 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4984 .access = PL3_RW | PL1_R,
4985 .resetvalue = 0,
4986 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
4987 };
4988 define_one_arm_cp_reg(cpu, &nsacr);
4989 }
4990 } else {
4991 if (arm_feature(env, ARM_FEATURE_V8)) {
4992 ARMCPRegInfo nsacr = {
4993 .name = "NSACR", .type = ARM_CP_CONST,
4994 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4995 .access = PL1_R,
4996 .resetvalue = 0xc00
4997 };
4998 define_one_arm_cp_reg(cpu, &nsacr);
4999 }
5000 }
5001
5002 if (arm_feature(env, ARM_FEATURE_PMSA)) {
5003 if (arm_feature(env, ARM_FEATURE_V6)) {
5004 /* PMSAv6 not implemented */
5005 assert(arm_feature(env, ARM_FEATURE_V7));
5006 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5007 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5008 } else {
5009 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5010 }
5011 } else {
5012 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5013 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5014 }
5015 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5016 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5017 }
5018 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5019 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5020 }
5021 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5022 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5023 }
5024 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5025 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5026 }
5027 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5028 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5029 }
5030 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5031 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5032 }
5033 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5034 define_arm_cp_regs(cpu, omap_cp_reginfo);
5035 }
5036 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5037 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5038 }
5039 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5040 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5041 }
5042 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5043 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5044 }
5045 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5046 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5047 }
5048 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5049 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5050 * be read-only (ie write causes UNDEF exception).
5051 */
5052 {
5053 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5054 /* Pre-v8 MIDR space.
5055 * Note that the MIDR isn't a simple constant register because
5056 * of the TI925 behaviour where writes to another register can
5057 * cause the MIDR value to change.
5058 *
5059 * Unimplemented registers in the c15 0 0 0 space default to
5060 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5061 * and friends override accordingly.
5062 */
5063 { .name = "MIDR",
5064 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
5065 .access = PL1_R, .resetvalue = cpu->midr,
5066 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
5067 .readfn = midr_read,
5068 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5069 .type = ARM_CP_OVERRIDE },
5070 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5071 { .name = "DUMMY",
5072 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5073 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5074 { .name = "DUMMY",
5075 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5076 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5077 { .name = "DUMMY",
5078 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5079 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5080 { .name = "DUMMY",
5081 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5082 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5083 { .name = "DUMMY",
5084 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5085 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5086 REGINFO_SENTINEL
5087 };
5088 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
5089 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5090 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
5091 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5092 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5093 .readfn = midr_read },
5094 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5095 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5096 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5097 .access = PL1_R, .resetvalue = cpu->midr },
5098 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5099 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5100 .access = PL1_R, .resetvalue = cpu->midr },
5101 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5102 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
5103 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
5104 REGINFO_SENTINEL
5105 };
5106 ARMCPRegInfo id_cp_reginfo[] = {
5107 /* These are common to v8 and pre-v8 */
5108 { .name = "CTR",
5109 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5110 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5111 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5112 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5113 .access = PL0_R, .accessfn = ctr_el0_access,
5114 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5115 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5116 { .name = "TCMTR",
5117 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5118 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5119 REGINFO_SENTINEL
5120 };
5121 /* TLBTR is specific to VMSA */
5122 ARMCPRegInfo id_tlbtr_reginfo = {
5123 .name = "TLBTR",
5124 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5125 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5126 };
5127 /* MPUIR is specific to PMSA V6+ */
5128 ARMCPRegInfo id_mpuir_reginfo = {
5129 .name = "MPUIR",
5130 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5131 .access = PL1_R, .type = ARM_CP_CONST,
5132 .resetvalue = cpu->pmsav7_dregion << 8
5133 };
5134 ARMCPRegInfo crn0_wi_reginfo = {
5135 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5136 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5137 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5138 };
5139 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5140 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5141 ARMCPRegInfo *r;
5142 /* Register the blanket "writes ignored" value first to cover the
5143 * whole space. Then update the specific ID registers to allow write
5144 * access, so that they ignore writes rather than causing them to
5145 * UNDEF.
5146 */
5147 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5148 for (r = id_pre_v8_midr_cp_reginfo;
5149 r->type != ARM_CP_SENTINEL; r++) {
5150 r->access = PL1_RW;
5151 }
5152 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5153 r->access = PL1_RW;
5154 }
5155 id_tlbtr_reginfo.access = PL1_RW;
5156 id_tlbtr_reginfo.access = PL1_RW;
5157 }
5158 if (arm_feature(env, ARM_FEATURE_V8)) {
5159 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5160 } else {
5161 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5162 }
5163 define_arm_cp_regs(cpu, id_cp_reginfo);
5164 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
5165 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
5166 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5167 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
5168 }
5169 }
5170
5171 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5172 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5173 }
5174
5175 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
5176 ARMCPRegInfo auxcr_reginfo[] = {
5177 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5178 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5179 .access = PL1_RW, .type = ARM_CP_CONST,
5180 .resetvalue = cpu->reset_auxcr },
5181 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5182 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5183 .access = PL2_RW, .type = ARM_CP_CONST,
5184 .resetvalue = 0 },
5185 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5186 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5187 .access = PL3_RW, .type = ARM_CP_CONST,
5188 .resetvalue = 0 },
5189 REGINFO_SENTINEL
5190 };
5191 define_arm_cp_regs(cpu, auxcr_reginfo);
5192 }
5193
5194 if (arm_feature(env, ARM_FEATURE_CBAR)) {
5195 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5196 /* 32 bit view is [31:18] 0...0 [43:32]. */
5197 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5198 | extract64(cpu->reset_cbar, 32, 12);
5199 ARMCPRegInfo cbar_reginfo[] = {
5200 { .name = "CBAR",
5201 .type = ARM_CP_CONST,
5202 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5203 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5204 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5205 .type = ARM_CP_CONST,
5206 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5207 .access = PL1_R, .resetvalue = cbar32 },
5208 REGINFO_SENTINEL
5209 };
5210 /* We don't implement a r/w 64 bit CBAR currently */
5211 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5212 define_arm_cp_regs(cpu, cbar_reginfo);
5213 } else {
5214 ARMCPRegInfo cbar = {
5215 .name = "CBAR",
5216 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5217 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5218 .fieldoffset = offsetof(CPUARMState,
5219 cp15.c15_config_base_address)
5220 };
5221 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5222 cbar.access = PL1_R;
5223 cbar.fieldoffset = 0;
5224 cbar.type = ARM_CP_CONST;
5225 }
5226 define_one_arm_cp_reg(cpu, &cbar);
5227 }
5228 }
5229
5230 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5231 ARMCPRegInfo vbar_cp_reginfo[] = {
5232 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5233 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5234 .access = PL1_RW, .writefn = vbar_write,
5235 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5236 offsetof(CPUARMState, cp15.vbar_ns) },
5237 .resetvalue = 0 },
5238 REGINFO_SENTINEL
5239 };
5240 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5241 }
5242
5243 /* Generic registers whose values depend on the implementation */
5244 {
5245 ARMCPRegInfo sctlr = {
5246 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
5247 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5248 .access = PL1_RW,
5249 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5250 offsetof(CPUARMState, cp15.sctlr_ns) },
5251 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5252 .raw_writefn = raw_write,
5253 };
5254 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5255 /* Normally we would always end the TB on an SCTLR write, but Linux
5256 * arch/arm/mach-pxa/sleep.S expects two instructions following
5257 * an MMU enable to execute from cache. Imitate this behaviour.
5258 */
5259 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5260 }
5261 define_one_arm_cp_reg(cpu, &sctlr);
5262 }
5263 }
5264
5265 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5266 {
5267 CPUState *cs = CPU(cpu);
5268 CPUARMState *env = &cpu->env;
5269
5270 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5271 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5272 aarch64_fpu_gdb_set_reg,
5273 34, "aarch64-fpu.xml", 0);
5274 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
5275 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5276 51, "arm-neon.xml", 0);
5277 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
5278 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5279 35, "arm-vfp3.xml", 0);
5280 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
5281 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5282 19, "arm-vfp.xml", 0);
5283 }
5284 }
5285
5286 /* Sort alphabetically by type name, except for "any". */
5287 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5288 {
5289 ObjectClass *class_a = (ObjectClass *)a;
5290 ObjectClass *class_b = (ObjectClass *)b;
5291 const char *name_a, *name_b;
5292
5293 name_a = object_class_get_name(class_a);
5294 name_b = object_class_get_name(class_b);
5295 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
5296 return 1;
5297 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
5298 return -1;
5299 } else {
5300 return strcmp(name_a, name_b);
5301 }
5302 }
5303
5304 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
5305 {
5306 ObjectClass *oc = data;
5307 CPUListState *s = user_data;
5308 const char *typename;
5309 char *name;
5310
5311 typename = object_class_get_name(oc);
5312 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
5313 (*s->cpu_fprintf)(s->file, " %s\n",
5314 name);
5315 g_free(name);
5316 }
5317
5318 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5319 {
5320 CPUListState s = {
5321 .file = f,
5322 .cpu_fprintf = cpu_fprintf,
5323 };
5324 GSList *list;
5325
5326 list = object_class_get_list(TYPE_ARM_CPU, false);
5327 list = g_slist_sort(list, arm_cpu_list_compare);
5328 (*cpu_fprintf)(f, "Available CPUs:\n");
5329 g_slist_foreach(list, arm_cpu_list_entry, &s);
5330 g_slist_free(list);
5331 #ifdef CONFIG_KVM
5332 /* The 'host' CPU type is dynamically registered only if KVM is
5333 * enabled, so we have to special-case it here:
5334 */
5335 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5336 #endif
5337 }
5338
5339 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5340 {
5341 ObjectClass *oc = data;
5342 CpuDefinitionInfoList **cpu_list = user_data;
5343 CpuDefinitionInfoList *entry;
5344 CpuDefinitionInfo *info;
5345 const char *typename;
5346
5347 typename = object_class_get_name(oc);
5348 info = g_malloc0(sizeof(*info));
5349 info->name = g_strndup(typename,
5350 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5351 info->q_typename = g_strdup(typename);
5352
5353 entry = g_malloc0(sizeof(*entry));
5354 entry->value = info;
5355 entry->next = *cpu_list;
5356 *cpu_list = entry;
5357 }
5358
5359 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5360 {
5361 CpuDefinitionInfoList *cpu_list = NULL;
5362 GSList *list;
5363
5364 list = object_class_get_list(TYPE_ARM_CPU, false);
5365 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5366 g_slist_free(list);
5367
5368 return cpu_list;
5369 }
5370
5371 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
5372 void *opaque, int state, int secstate,
5373 int crm, int opc1, int opc2)
5374 {
5375 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5376 * add a single reginfo struct to the hash table.
5377 */
5378 uint32_t *key = g_new(uint32_t, 1);
5379 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5380 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
5381 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5382
5383 /* Reset the secure state to the specific incoming state. This is
5384 * necessary as the register may have been defined with both states.
5385 */
5386 r2->secure = secstate;
5387
5388 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5389 /* Register is banked (using both entries in array).
5390 * Overwriting fieldoffset as the array is only used to define
5391 * banked registers but later only fieldoffset is used.
5392 */
5393 r2->fieldoffset = r->bank_fieldoffsets[ns];
5394 }
5395
5396 if (state == ARM_CP_STATE_AA32) {
5397 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5398 /* If the register is banked then we don't need to migrate or
5399 * reset the 32-bit instance in certain cases:
5400 *
5401 * 1) If the register has both 32-bit and 64-bit instances then we
5402 * can count on the 64-bit instance taking care of the
5403 * non-secure bank.
5404 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5405 * taking care of the secure bank. This requires that separate
5406 * 32 and 64-bit definitions are provided.
5407 */
5408 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5409 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
5410 r2->type |= ARM_CP_ALIAS;
5411 }
5412 } else if ((secstate != r->secure) && !ns) {
5413 /* The register is not banked so we only want to allow migration of
5414 * the non-secure instance.
5415 */
5416 r2->type |= ARM_CP_ALIAS;
5417 }
5418
5419 if (r->state == ARM_CP_STATE_BOTH) {
5420 /* We assume it is a cp15 register if the .cp field is left unset.
5421 */
5422 if (r2->cp == 0) {
5423 r2->cp = 15;
5424 }
5425
5426 #ifdef HOST_WORDS_BIGENDIAN
5427 if (r2->fieldoffset) {
5428 r2->fieldoffset += sizeof(uint32_t);
5429 }
5430 #endif
5431 }
5432 }
5433 if (state == ARM_CP_STATE_AA64) {
5434 /* To allow abbreviation of ARMCPRegInfo
5435 * definitions, we treat cp == 0 as equivalent to
5436 * the value for "standard guest-visible sysreg".
5437 * STATE_BOTH definitions are also always "standard
5438 * sysreg" in their AArch64 view (the .cp value may
5439 * be non-zero for the benefit of the AArch32 view).
5440 */
5441 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
5442 r2->cp = CP_REG_ARM64_SYSREG_CP;
5443 }
5444 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5445 r2->opc0, opc1, opc2);
5446 } else {
5447 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
5448 }
5449 if (opaque) {
5450 r2->opaque = opaque;
5451 }
5452 /* reginfo passed to helpers is correct for the actual access,
5453 * and is never ARM_CP_STATE_BOTH:
5454 */
5455 r2->state = state;
5456 /* Make sure reginfo passed to helpers for wildcarded regs
5457 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5458 */
5459 r2->crm = crm;
5460 r2->opc1 = opc1;
5461 r2->opc2 = opc2;
5462 /* By convention, for wildcarded registers only the first
5463 * entry is used for migration; the others are marked as
5464 * ALIAS so we don't try to transfer the register
5465 * multiple times. Special registers (ie NOP/WFI) are
5466 * never migratable and not even raw-accessible.
5467 */
5468 if ((r->type & ARM_CP_SPECIAL)) {
5469 r2->type |= ARM_CP_NO_RAW;
5470 }
5471 if (((r->crm == CP_ANY) && crm != 0) ||
5472 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5473 ((r->opc2 == CP_ANY) && opc2 != 0)) {
5474 r2->type |= ARM_CP_ALIAS;
5475 }
5476
5477 /* Check that raw accesses are either forbidden or handled. Note that
5478 * we can't assert this earlier because the setup of fieldoffset for
5479 * banked registers has to be done first.
5480 */
5481 if (!(r2->type & ARM_CP_NO_RAW)) {
5482 assert(!raw_accessors_invalid(r2));
5483 }
5484
5485 /* Overriding of an existing definition must be explicitly
5486 * requested.
5487 */
5488 if (!(r->type & ARM_CP_OVERRIDE)) {
5489 ARMCPRegInfo *oldreg;
5490 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5491 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5492 fprintf(stderr, "Register redefined: cp=%d %d bit "
5493 "crn=%d crm=%d opc1=%d opc2=%d, "
5494 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5495 r2->crn, r2->crm, r2->opc1, r2->opc2,
5496 oldreg->name, r2->name);
5497 g_assert_not_reached();
5498 }
5499 }
5500 g_hash_table_insert(cpu->cp_regs, key, r2);
5501 }
5502
5503
5504 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5505 const ARMCPRegInfo *r, void *opaque)
5506 {
5507 /* Define implementations of coprocessor registers.
5508 * We store these in a hashtable because typically
5509 * there are less than 150 registers in a space which
5510 * is 16*16*16*8*8 = 262144 in size.
5511 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5512 * If a register is defined twice then the second definition is
5513 * used, so this can be used to define some generic registers and
5514 * then override them with implementation specific variations.
5515 * At least one of the original and the second definition should
5516 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5517 * against accidental use.
5518 *
5519 * The state field defines whether the register is to be
5520 * visible in the AArch32 or AArch64 execution state. If the
5521 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5522 * reginfo structure for the AArch32 view, which sees the lower
5523 * 32 bits of the 64 bit register.
5524 *
5525 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5526 * be wildcarded. AArch64 registers are always considered to be 64
5527 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5528 * the register, if any.
5529 */
5530 int crm, opc1, opc2, state;
5531 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5532 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5533 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5534 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5535 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5536 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5537 /* 64 bit registers have only CRm and Opc1 fields */
5538 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
5539 /* op0 only exists in the AArch64 encodings */
5540 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5541 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5542 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5543 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5544 * encodes a minimum access level for the register. We roll this
5545 * runtime check into our general permission check code, so check
5546 * here that the reginfo's specified permissions are strict enough
5547 * to encompass the generic architectural permission check.
5548 */
5549 if (r->state != ARM_CP_STATE_AA32) {
5550 int mask = 0;
5551 switch (r->opc1) {
5552 case 0: case 1: case 2:
5553 /* min_EL EL1 */
5554 mask = PL1_RW;
5555 break;
5556 case 3:
5557 /* min_EL EL0 */
5558 mask = PL0_RW;
5559 break;
5560 case 4:
5561 /* min_EL EL2 */
5562 mask = PL2_RW;
5563 break;
5564 case 5:
5565 /* unallocated encoding, so not possible */
5566 assert(false);
5567 break;
5568 case 6:
5569 /* min_EL EL3 */
5570 mask = PL3_RW;
5571 break;
5572 case 7:
5573 /* min_EL EL1, secure mode only (we don't check the latter) */
5574 mask = PL1_RW;
5575 break;
5576 default:
5577 /* broken reginfo with out-of-range opc1 */
5578 assert(false);
5579 break;
5580 }
5581 /* assert our permissions are not too lax (stricter is fine) */
5582 assert((r->access & ~mask) == 0);
5583 }
5584
5585 /* Check that the register definition has enough info to handle
5586 * reads and writes if they are permitted.
5587 */
5588 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5589 if (r->access & PL3_R) {
5590 assert((r->fieldoffset ||
5591 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5592 r->readfn);
5593 }
5594 if (r->access & PL3_W) {
5595 assert((r->fieldoffset ||
5596 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5597 r->writefn);
5598 }
5599 }
5600 /* Bad type field probably means missing sentinel at end of reg list */
5601 assert(cptype_valid(r->type));
5602 for (crm = crmmin; crm <= crmmax; crm++) {
5603 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5604 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
5605 for (state = ARM_CP_STATE_AA32;
5606 state <= ARM_CP_STATE_AA64; state++) {
5607 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5608 continue;
5609 }
5610 if (state == ARM_CP_STATE_AA32) {
5611 /* Under AArch32 CP registers can be common
5612 * (same for secure and non-secure world) or banked.
5613 */
5614 switch (r->secure) {
5615 case ARM_CP_SECSTATE_S:
5616 case ARM_CP_SECSTATE_NS:
5617 add_cpreg_to_hashtable(cpu, r, opaque, state,
5618 r->secure, crm, opc1, opc2);
5619 break;
5620 default:
5621 add_cpreg_to_hashtable(cpu, r, opaque, state,
5622 ARM_CP_SECSTATE_S,
5623 crm, opc1, opc2);
5624 add_cpreg_to_hashtable(cpu, r, opaque, state,
5625 ARM_CP_SECSTATE_NS,
5626 crm, opc1, opc2);
5627 break;
5628 }
5629 } else {
5630 /* AArch64 registers get mapped to non-secure instance
5631 * of AArch32 */
5632 add_cpreg_to_hashtable(cpu, r, opaque, state,
5633 ARM_CP_SECSTATE_NS,
5634 crm, opc1, opc2);
5635 }
5636 }
5637 }
5638 }
5639 }
5640 }
5641
5642 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5643 const ARMCPRegInfo *regs, void *opaque)
5644 {
5645 /* Define a whole list of registers */
5646 const ARMCPRegInfo *r;
5647 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5648 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5649 }
5650 }
5651
5652 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
5653 {
5654 return g_hash_table_lookup(cpregs, &encoded_cp);
5655 }
5656
5657 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5658 uint64_t value)
5659 {
5660 /* Helper coprocessor write function for write-ignore registers */
5661 }
5662
5663 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
5664 {
5665 /* Helper coprocessor write function for read-as-zero registers */
5666 return 0;
5667 }
5668
5669 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5670 {
5671 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5672 }
5673
5674 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
5675 {
5676 /* Return true if it is not valid for us to switch to
5677 * this CPU mode (ie all the UNPREDICTABLE cases in
5678 * the ARM ARM CPSRWriteByInstr pseudocode).
5679 */
5680
5681 /* Changes to or from Hyp via MSR and CPS are illegal. */
5682 if (write_type == CPSRWriteByInstr &&
5683 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5684 mode == ARM_CPU_MODE_HYP)) {
5685 return 1;
5686 }
5687
5688 switch (mode) {
5689 case ARM_CPU_MODE_USR:
5690 return 0;
5691 case ARM_CPU_MODE_SYS:
5692 case ARM_CPU_MODE_SVC:
5693 case ARM_CPU_MODE_ABT:
5694 case ARM_CPU_MODE_UND:
5695 case ARM_CPU_MODE_IRQ:
5696 case ARM_CPU_MODE_FIQ:
5697 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5698 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5699 */
5700 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5701 * and CPS are treated as illegal mode changes.
5702 */
5703 if (write_type == CPSRWriteByInstr &&
5704 (env->cp15.hcr_el2 & HCR_TGE) &&
5705 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5706 !arm_is_secure_below_el3(env)) {
5707 return 1;
5708 }
5709 return 0;
5710 case ARM_CPU_MODE_HYP:
5711 return !arm_feature(env, ARM_FEATURE_EL2)
5712 || arm_current_el(env) < 2 || arm_is_secure(env);
5713 case ARM_CPU_MODE_MON:
5714 return arm_current_el(env) < 3;
5715 default:
5716 return 1;
5717 }
5718 }
5719
5720 uint32_t cpsr_read(CPUARMState *env)
5721 {
5722 int ZF;
5723 ZF = (env->ZF == 0);
5724 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
5725 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5726 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5727 | ((env->condexec_bits & 0xfc) << 8)
5728 | (env->GE << 16) | (env->daif & CPSR_AIF);
5729 }
5730
5731 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5732 CPSRWriteType write_type)
5733 {
5734 uint32_t changed_daif;
5735
5736 if (mask & CPSR_NZCV) {
5737 env->ZF = (~val) & CPSR_Z;
5738 env->NF = val;
5739 env->CF = (val >> 29) & 1;
5740 env->VF = (val << 3) & 0x80000000;
5741 }
5742 if (mask & CPSR_Q)
5743 env->QF = ((val & CPSR_Q) != 0);
5744 if (mask & CPSR_T)
5745 env->thumb = ((val & CPSR_T) != 0);
5746 if (mask & CPSR_IT_0_1) {
5747 env->condexec_bits &= ~3;
5748 env->condexec_bits |= (val >> 25) & 3;
5749 }
5750 if (mask & CPSR_IT_2_7) {
5751 env->condexec_bits &= 3;
5752 env->condexec_bits |= (val >> 8) & 0xfc;
5753 }
5754 if (mask & CPSR_GE) {
5755 env->GE = (val >> 16) & 0xf;
5756 }
5757
5758 /* In a V7 implementation that includes the security extensions but does
5759 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5760 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5761 * bits respectively.
5762 *
5763 * In a V8 implementation, it is permitted for privileged software to
5764 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5765 */
5766 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
5767 arm_feature(env, ARM_FEATURE_EL3) &&
5768 !arm_feature(env, ARM_FEATURE_EL2) &&
5769 !arm_is_secure(env)) {
5770
5771 changed_daif = (env->daif ^ val) & mask;
5772
5773 if (changed_daif & CPSR_A) {
5774 /* Check to see if we are allowed to change the masking of async
5775 * abort exceptions from a non-secure state.
5776 */
5777 if (!(env->cp15.scr_el3 & SCR_AW)) {
5778 qemu_log_mask(LOG_GUEST_ERROR,
5779 "Ignoring attempt to switch CPSR_A flag from "
5780 "non-secure world with SCR.AW bit clear\n");
5781 mask &= ~CPSR_A;
5782 }
5783 }
5784
5785 if (changed_daif & CPSR_F) {
5786 /* Check to see if we are allowed to change the masking of FIQ
5787 * exceptions from a non-secure state.
5788 */
5789 if (!(env->cp15.scr_el3 & SCR_FW)) {
5790 qemu_log_mask(LOG_GUEST_ERROR,
5791 "Ignoring attempt to switch CPSR_F flag from "
5792 "non-secure world with SCR.FW bit clear\n");
5793 mask &= ~CPSR_F;
5794 }
5795
5796 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5797 * If this bit is set software is not allowed to mask
5798 * FIQs, but is allowed to set CPSR_F to 0.
5799 */
5800 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5801 (val & CPSR_F)) {
5802 qemu_log_mask(LOG_GUEST_ERROR,
5803 "Ignoring attempt to enable CPSR_F flag "
5804 "(non-maskable FIQ [NMFI] support enabled)\n");
5805 mask &= ~CPSR_F;
5806 }
5807 }
5808 }
5809
5810 env->daif &= ~(CPSR_AIF & mask);
5811 env->daif |= val & CPSR_AIF & mask;
5812
5813 if (write_type != CPSRWriteRaw &&
5814 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
5815 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5816 /* Note that we can only get here in USR mode if this is a
5817 * gdb stub write; for this case we follow the architectural
5818 * behaviour for guest writes in USR mode of ignoring an attempt
5819 * to switch mode. (Those are caught by translate.c for writes
5820 * triggered by guest instructions.)
5821 */
5822 mask &= ~CPSR_M;
5823 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
5824 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5825 * v7, and has defined behaviour in v8:
5826 * + leave CPSR.M untouched
5827 * + allow changes to the other CPSR fields
5828 * + set PSTATE.IL
5829 * For user changes via the GDB stub, we don't set PSTATE.IL,
5830 * as this would be unnecessarily harsh for a user error.
5831 */
5832 mask &= ~CPSR_M;
5833 if (write_type != CPSRWriteByGDBStub &&
5834 arm_feature(env, ARM_FEATURE_V8)) {
5835 mask |= CPSR_IL;
5836 val |= CPSR_IL;
5837 }
5838 } else {
5839 switch_mode(env, val & CPSR_M);
5840 }
5841 }
5842 mask &= ~CACHED_CPSR_BITS;
5843 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5844 }
5845
5846 /* Sign/zero extend */
5847 uint32_t HELPER(sxtb16)(uint32_t x)
5848 {
5849 uint32_t res;
5850 res = (uint16_t)(int8_t)x;
5851 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5852 return res;
5853 }
5854
5855 uint32_t HELPER(uxtb16)(uint32_t x)
5856 {
5857 uint32_t res;
5858 res = (uint16_t)(uint8_t)x;
5859 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5860 return res;
5861 }
5862
5863 int32_t HELPER(sdiv)(int32_t num, int32_t den)
5864 {
5865 if (den == 0)
5866 return 0;
5867 if (num == INT_MIN && den == -1)
5868 return INT_MIN;
5869 return num / den;
5870 }
5871
5872 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5873 {
5874 if (den == 0)
5875 return 0;
5876 return num / den;
5877 }
5878
5879 uint32_t HELPER(rbit)(uint32_t x)
5880 {
5881 return revbit32(x);
5882 }
5883
5884 #if defined(CONFIG_USER_ONLY)
5885
5886 /* These should probably raise undefined insn exceptions. */
5887 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
5888 {
5889 ARMCPU *cpu = arm_env_get_cpu(env);
5890
5891 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
5892 }
5893
5894 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
5895 {
5896 ARMCPU *cpu = arm_env_get_cpu(env);
5897
5898 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
5899 return 0;
5900 }
5901
5902 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
5903 {
5904 /* translate.c should never generate calls here in user-only mode */
5905 g_assert_not_reached();
5906 }
5907
5908 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
5909 {
5910 /* translate.c should never generate calls here in user-only mode */
5911 g_assert_not_reached();
5912 }
5913
5914 void switch_mode(CPUARMState *env, int mode)
5915 {
5916 ARMCPU *cpu = arm_env_get_cpu(env);
5917
5918 if (mode != ARM_CPU_MODE_USR) {
5919 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5920 }
5921 }
5922
5923 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5924 uint32_t cur_el, bool secure)
5925 {
5926 return 1;
5927 }
5928
5929 void aarch64_sync_64_to_32(CPUARMState *env)
5930 {
5931 g_assert_not_reached();
5932 }
5933
5934 #else
5935
5936 void switch_mode(CPUARMState *env, int mode)
5937 {
5938 int old_mode;
5939 int i;
5940
5941 old_mode = env->uncached_cpsr & CPSR_M;
5942 if (mode == old_mode)
5943 return;
5944
5945 if (old_mode == ARM_CPU_MODE_FIQ) {
5946 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
5947 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
5948 } else if (mode == ARM_CPU_MODE_FIQ) {
5949 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
5950 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
5951 }
5952
5953 i = bank_number(old_mode);
5954 env->banked_r13[i] = env->regs[13];
5955 env->banked_r14[i] = env->regs[14];
5956 env->banked_spsr[i] = env->spsr;
5957
5958 i = bank_number(mode);
5959 env->regs[13] = env->banked_r13[i];
5960 env->regs[14] = env->banked_r14[i];
5961 env->spsr = env->banked_spsr[i];
5962 }
5963
5964 /* Physical Interrupt Target EL Lookup Table
5965 *
5966 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5967 *
5968 * The below multi-dimensional table is used for looking up the target
5969 * exception level given numerous condition criteria. Specifically, the
5970 * target EL is based on SCR and HCR routing controls as well as the
5971 * currently executing EL and secure state.
5972 *
5973 * Dimensions:
5974 * target_el_table[2][2][2][2][2][4]
5975 * | | | | | +--- Current EL
5976 * | | | | +------ Non-secure(0)/Secure(1)
5977 * | | | +--------- HCR mask override
5978 * | | +------------ SCR exec state control
5979 * | +--------------- SCR mask override
5980 * +------------------ 32-bit(0)/64-bit(1) EL3
5981 *
5982 * The table values are as such:
5983 * 0-3 = EL0-EL3
5984 * -1 = Cannot occur
5985 *
5986 * The ARM ARM target EL table includes entries indicating that an "exception
5987 * is not taken". The two cases where this is applicable are:
5988 * 1) An exception is taken from EL3 but the SCR does not have the exception
5989 * routed to EL3.
5990 * 2) An exception is taken from EL2 but the HCR does not have the exception
5991 * routed to EL2.
5992 * In these two cases, the below table contain a target of EL1. This value is
5993 * returned as it is expected that the consumer of the table data will check
5994 * for "target EL >= current EL" to ensure the exception is not taken.
5995 *
5996 * SCR HCR
5997 * 64 EA AMO From
5998 * BIT IRQ IMO Non-secure Secure
5999 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6000 */
6001 static const int8_t target_el_table[2][2][2][2][2][4] = {
6002 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6003 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6004 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6005 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6006 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6007 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6008 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6009 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6010 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6011 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6012 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6013 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6014 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6015 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6016 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6017 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6018 };
6019
6020 /*
6021 * Determine the target EL for physical exceptions
6022 */
6023 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6024 uint32_t cur_el, bool secure)
6025 {
6026 CPUARMState *env = cs->env_ptr;
6027 int rw;
6028 int scr;
6029 int hcr;
6030 int target_el;
6031 /* Is the highest EL AArch64? */
6032 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6033
6034 if (arm_feature(env, ARM_FEATURE_EL3)) {
6035 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6036 } else {
6037 /* Either EL2 is the highest EL (and so the EL2 register width
6038 * is given by is64); or there is no EL2 or EL3, in which case
6039 * the value of 'rw' does not affect the table lookup anyway.
6040 */
6041 rw = is64;
6042 }
6043
6044 switch (excp_idx) {
6045 case EXCP_IRQ:
6046 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6047 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6048 break;
6049 case EXCP_FIQ:
6050 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6051 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6052 break;
6053 default:
6054 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6055 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6056 break;
6057 };
6058
6059 /* If HCR.TGE is set then HCR is treated as being 1 */
6060 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6061
6062 /* Perform a table-lookup for the target EL given the current state */
6063 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6064
6065 assert(target_el > 0);
6066
6067 return target_el;
6068 }
6069
6070 static void v7m_push(CPUARMState *env, uint32_t val)
6071 {
6072 CPUState *cs = CPU(arm_env_get_cpu(env));
6073
6074 env->regs[13] -= 4;
6075 stl_phys(cs->as, env->regs[13], val);
6076 }
6077
6078 /* Return true if we're using the process stack pointer (not the MSP) */
6079 static bool v7m_using_psp(CPUARMState *env)
6080 {
6081 /* Handler mode always uses the main stack; for thread mode
6082 * the CONTROL.SPSEL bit determines the answer.
6083 * Note that in v7M it is not possible to be in Handler mode with
6084 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6085 */
6086 return !arm_v7m_is_handler_mode(env) &&
6087 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6088 }
6089
6090 /* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6091 * This may change the current stack pointer between Main and Process
6092 * stack pointers if it is done for the CONTROL register for the current
6093 * security state.
6094 */
6095 static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6096 bool new_spsel,
6097 bool secstate)
6098 {
6099 bool old_is_psp = v7m_using_psp(env);
6100
6101 env->v7m.control[secstate] =
6102 deposit32(env->v7m.control[secstate],
6103 R_V7M_CONTROL_SPSEL_SHIFT,
6104 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6105
6106 if (secstate == env->v7m.secure) {
6107 bool new_is_psp = v7m_using_psp(env);
6108 uint32_t tmp;
6109
6110 if (old_is_psp != new_is_psp) {
6111 tmp = env->v7m.other_sp;
6112 env->v7m.other_sp = env->regs[13];
6113 env->regs[13] = tmp;
6114 }
6115 }
6116 }
6117
6118 /* Write to v7M CONTROL.SPSEL bit. This may change the current
6119 * stack pointer between Main and Process stack pointers.
6120 */
6121 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6122 {
6123 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6124 }
6125
6126 void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6127 {
6128 /* Write a new value to v7m.exception, thus transitioning into or out
6129 * of Handler mode; this may result in a change of active stack pointer.
6130 */
6131 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6132 uint32_t tmp;
6133
6134 env->v7m.exception = new_exc;
6135
6136 new_is_psp = v7m_using_psp(env);
6137
6138 if (old_is_psp != new_is_psp) {
6139 tmp = env->v7m.other_sp;
6140 env->v7m.other_sp = env->regs[13];
6141 env->regs[13] = tmp;
6142 }
6143 }
6144
6145 /* Switch M profile security state between NS and S */
6146 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6147 {
6148 uint32_t new_ss_msp, new_ss_psp;
6149
6150 if (env->v7m.secure == new_secstate) {
6151 return;
6152 }
6153
6154 /* All the banked state is accessed by looking at env->v7m.secure
6155 * except for the stack pointer; rearrange the SP appropriately.
6156 */
6157 new_ss_msp = env->v7m.other_ss_msp;
6158 new_ss_psp = env->v7m.other_ss_psp;
6159
6160 if (v7m_using_psp(env)) {
6161 env->v7m.other_ss_psp = env->regs[13];
6162 env->v7m.other_ss_msp = env->v7m.other_sp;
6163 } else {
6164 env->v7m.other_ss_msp = env->regs[13];
6165 env->v7m.other_ss_psp = env->v7m.other_sp;
6166 }
6167
6168 env->v7m.secure = new_secstate;
6169
6170 if (v7m_using_psp(env)) {
6171 env->regs[13] = new_ss_psp;
6172 env->v7m.other_sp = new_ss_msp;
6173 } else {
6174 env->regs[13] = new_ss_msp;
6175 env->v7m.other_sp = new_ss_psp;
6176 }
6177 }
6178
6179 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6180 {
6181 /* Handle v7M BXNS:
6182 * - if the return value is a magic value, do exception return (like BX)
6183 * - otherwise bit 0 of the return value is the target security state
6184 */
6185 uint32_t min_magic;
6186
6187 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6188 /* Covers FNC_RETURN and EXC_RETURN magic */
6189 min_magic = FNC_RETURN_MIN_MAGIC;
6190 } else {
6191 /* EXC_RETURN magic only */
6192 min_magic = EXC_RETURN_MIN_MAGIC;
6193 }
6194
6195 if (dest >= min_magic) {
6196 /* This is an exception return magic value; put it where
6197 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6198 * Note that if we ever add gen_ss_advance() singlestep support to
6199 * M profile this should count as an "instruction execution complete"
6200 * event (compare gen_bx_excret_final_code()).
6201 */
6202 env->regs[15] = dest & ~1;
6203 env->thumb = dest & 1;
6204 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6205 /* notreached */
6206 }
6207
6208 /* translate.c should have made BXNS UNDEF unless we're secure */
6209 assert(env->v7m.secure);
6210
6211 switch_v7m_security_state(env, dest & 1);
6212 env->thumb = 1;
6213 env->regs[15] = dest & ~1;
6214 }
6215
6216 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6217 {
6218 /* Handle v7M BLXNS:
6219 * - bit 0 of the destination address is the target security state
6220 */
6221
6222 /* At this point regs[15] is the address just after the BLXNS */
6223 uint32_t nextinst = env->regs[15] | 1;
6224 uint32_t sp = env->regs[13] - 8;
6225 uint32_t saved_psr;
6226
6227 /* translate.c will have made BLXNS UNDEF unless we're secure */
6228 assert(env->v7m.secure);
6229
6230 if (dest & 1) {
6231 /* target is Secure, so this is just a normal BLX,
6232 * except that the low bit doesn't indicate Thumb/not.
6233 */
6234 env->regs[14] = nextinst;
6235 env->thumb = 1;
6236 env->regs[15] = dest & ~1;
6237 return;
6238 }
6239
6240 /* Target is non-secure: first push a stack frame */
6241 if (!QEMU_IS_ALIGNED(sp, 8)) {
6242 qemu_log_mask(LOG_GUEST_ERROR,
6243 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6244 }
6245
6246 saved_psr = env->v7m.exception;
6247 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6248 saved_psr |= XPSR_SFPA;
6249 }
6250
6251 /* Note that these stores can throw exceptions on MPU faults */
6252 cpu_stl_data(env, sp, nextinst);
6253 cpu_stl_data(env, sp + 4, saved_psr);
6254
6255 env->regs[13] = sp;
6256 env->regs[14] = 0xfeffffff;
6257 if (arm_v7m_is_handler_mode(env)) {
6258 /* Write a dummy value to IPSR, to avoid leaking the current secure
6259 * exception number to non-secure code. This is guaranteed not
6260 * to cause write_v7m_exception() to actually change stacks.
6261 */
6262 write_v7m_exception(env, 1);
6263 }
6264 switch_v7m_security_state(env, 0);
6265 env->thumb = 1;
6266 env->regs[15] = dest;
6267 }
6268
6269 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6270 bool spsel)
6271 {
6272 /* Return a pointer to the location where we currently store the
6273 * stack pointer for the requested security state and thread mode.
6274 * This pointer will become invalid if the CPU state is updated
6275 * such that the stack pointers are switched around (eg changing
6276 * the SPSEL control bit).
6277 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6278 * Unlike that pseudocode, we require the caller to pass us in the
6279 * SPSEL control bit value; this is because we also use this
6280 * function in handling of pushing of the callee-saves registers
6281 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6282 * and in the tailchain codepath the SPSEL bit comes from the exception
6283 * return magic LR value from the previous exception. The pseudocode
6284 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6285 * to make this utility function generic enough to do the job.
6286 */
6287 bool want_psp = threadmode && spsel;
6288
6289 if (secure == env->v7m.secure) {
6290 if (want_psp == v7m_using_psp(env)) {
6291 return &env->regs[13];
6292 } else {
6293 return &env->v7m.other_sp;
6294 }
6295 } else {
6296 if (want_psp) {
6297 return &env->v7m.other_ss_psp;
6298 } else {
6299 return &env->v7m.other_ss_msp;
6300 }
6301 }
6302 }
6303
6304 static uint32_t arm_v7m_load_vector(ARMCPU *cpu, bool targets_secure)
6305 {
6306 CPUState *cs = CPU(cpu);
6307 CPUARMState *env = &cpu->env;
6308 MemTxResult result;
6309 hwaddr vec = env->v7m.vecbase[targets_secure] + env->v7m.exception * 4;
6310 uint32_t addr;
6311
6312 addr = address_space_ldl(cs->as, vec,
6313 MEMTXATTRS_UNSPECIFIED, &result);
6314 if (result != MEMTX_OK) {
6315 /* Architecturally this should cause a HardFault setting HSFR.VECTTBL,
6316 * which would then be immediately followed by our failing to load
6317 * the entry vector for that HardFault, which is a Lockup case.
6318 * Since we don't model Lockup, we just report this guest error
6319 * via cpu_abort().
6320 */
6321 cpu_abort(cs, "Failed to read from %s exception vector table "
6322 "entry %08x\n", targets_secure ? "secure" : "nonsecure",
6323 (unsigned)vec);
6324 }
6325 return addr;
6326 }
6327
6328 static void v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain)
6329 {
6330 /* For v8M, push the callee-saves register part of the stack frame.
6331 * Compare the v8M pseudocode PushCalleeStack().
6332 * In the tailchaining case this may not be the current stack.
6333 */
6334 CPUARMState *env = &cpu->env;
6335 CPUState *cs = CPU(cpu);
6336 uint32_t *frame_sp_p;
6337 uint32_t frameptr;
6338
6339 if (dotailchain) {
6340 frame_sp_p = get_v7m_sp_ptr(env, true,
6341 lr & R_V7M_EXCRET_MODE_MASK,
6342 lr & R_V7M_EXCRET_SPSEL_MASK);
6343 } else {
6344 frame_sp_p = &env->regs[13];
6345 }
6346
6347 frameptr = *frame_sp_p - 0x28;
6348
6349 stl_phys(cs->as, frameptr, 0xfefa125b);
6350 stl_phys(cs->as, frameptr + 0x8, env->regs[4]);
6351 stl_phys(cs->as, frameptr + 0xc, env->regs[5]);
6352 stl_phys(cs->as, frameptr + 0x10, env->regs[6]);
6353 stl_phys(cs->as, frameptr + 0x14, env->regs[7]);
6354 stl_phys(cs->as, frameptr + 0x18, env->regs[8]);
6355 stl_phys(cs->as, frameptr + 0x1c, env->regs[9]);
6356 stl_phys(cs->as, frameptr + 0x20, env->regs[10]);
6357 stl_phys(cs->as, frameptr + 0x24, env->regs[11]);
6358
6359 *frame_sp_p = frameptr;
6360 }
6361
6362 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain)
6363 {
6364 /* Do the "take the exception" parts of exception entry,
6365 * but not the pushing of state to the stack. This is
6366 * similar to the pseudocode ExceptionTaken() function.
6367 */
6368 CPUARMState *env = &cpu->env;
6369 uint32_t addr;
6370 bool targets_secure;
6371
6372 targets_secure = armv7m_nvic_acknowledge_irq(env->nvic);
6373
6374 if (arm_feature(env, ARM_FEATURE_V8)) {
6375 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6376 (lr & R_V7M_EXCRET_S_MASK)) {
6377 /* The background code (the owner of the registers in the
6378 * exception frame) is Secure. This means it may either already
6379 * have or now needs to push callee-saves registers.
6380 */
6381 if (targets_secure) {
6382 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6383 /* We took an exception from Secure to NonSecure
6384 * (which means the callee-saved registers got stacked)
6385 * and are now tailchaining to a Secure exception.
6386 * Clear DCRS so eventual return from this Secure
6387 * exception unstacks the callee-saved registers.
6388 */
6389 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6390 }
6391 } else {
6392 /* We're going to a non-secure exception; push the
6393 * callee-saves registers to the stack now, if they're
6394 * not already saved.
6395 */
6396 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6397 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
6398 v7m_push_callee_stack(cpu, lr, dotailchain);
6399 }
6400 lr |= R_V7M_EXCRET_DCRS_MASK;
6401 }
6402 }
6403
6404 lr &= ~R_V7M_EXCRET_ES_MASK;
6405 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6406 lr |= R_V7M_EXCRET_ES_MASK;
6407 }
6408 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6409 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6410 lr |= R_V7M_EXCRET_SPSEL_MASK;
6411 }
6412
6413 /* Clear registers if necessary to prevent non-secure exception
6414 * code being able to see register values from secure code.
6415 * Where register values become architecturally UNKNOWN we leave
6416 * them with their previous values.
6417 */
6418 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6419 if (!targets_secure) {
6420 /* Always clear the caller-saved registers (they have been
6421 * pushed to the stack earlier in v7m_push_stack()).
6422 * Clear callee-saved registers if the background code is
6423 * Secure (in which case these regs were saved in
6424 * v7m_push_callee_stack()).
6425 */
6426 int i;
6427
6428 for (i = 0; i < 13; i++) {
6429 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6430 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6431 env->regs[i] = 0;
6432 }
6433 }
6434 /* Clear EAPSR */
6435 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6436 }
6437 }
6438 }
6439
6440 /* Switch to target security state -- must do this before writing SPSEL */
6441 switch_v7m_security_state(env, targets_secure);
6442 write_v7m_control_spsel(env, 0);
6443 arm_clear_exclusive(env);
6444 /* Clear IT bits */
6445 env->condexec_bits = 0;
6446 env->regs[14] = lr;
6447 addr = arm_v7m_load_vector(cpu, targets_secure);
6448 env->regs[15] = addr & 0xfffffffe;
6449 env->thumb = addr & 1;
6450 }
6451
6452 static void v7m_push_stack(ARMCPU *cpu)
6453 {
6454 /* Do the "set up stack frame" part of exception entry,
6455 * similar to pseudocode PushStack().
6456 */
6457 CPUARMState *env = &cpu->env;
6458 uint32_t xpsr = xpsr_read(env);
6459
6460 /* Align stack pointer if the guest wants that */
6461 if ((env->regs[13] & 4) &&
6462 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
6463 env->regs[13] -= 4;
6464 xpsr |= XPSR_SPREALIGN;
6465 }
6466 /* Switch to the handler mode. */
6467 v7m_push(env, xpsr);
6468 v7m_push(env, env->regs[15]);
6469 v7m_push(env, env->regs[14]);
6470 v7m_push(env, env->regs[12]);
6471 v7m_push(env, env->regs[3]);
6472 v7m_push(env, env->regs[2]);
6473 v7m_push(env, env->regs[1]);
6474 v7m_push(env, env->regs[0]);
6475 }
6476
6477 static void do_v7m_exception_exit(ARMCPU *cpu)
6478 {
6479 CPUARMState *env = &cpu->env;
6480 CPUState *cs = CPU(cpu);
6481 uint32_t excret;
6482 uint32_t xpsr;
6483 bool ufault = false;
6484 bool sfault = false;
6485 bool return_to_sp_process;
6486 bool return_to_handler;
6487 bool rettobase = false;
6488 bool exc_secure = false;
6489 bool return_to_secure;
6490
6491 /* If we're not in Handler mode then jumps to magic exception-exit
6492 * addresses don't have magic behaviour. However for the v8M
6493 * security extensions the magic secure-function-return has to
6494 * work in thread mode too, so to avoid doing an extra check in
6495 * the generated code we allow exception-exit magic to also cause the
6496 * internal exception and bring us here in thread mode. Correct code
6497 * will never try to do this (the following insn fetch will always
6498 * fault) so we the overhead of having taken an unnecessary exception
6499 * doesn't matter.
6500 */
6501 if (!arm_v7m_is_handler_mode(env)) {
6502 return;
6503 }
6504
6505 /* In the spec pseudocode ExceptionReturn() is called directly
6506 * from BXWritePC() and gets the full target PC value including
6507 * bit zero. In QEMU's implementation we treat it as a normal
6508 * jump-to-register (which is then caught later on), and so split
6509 * the target value up between env->regs[15] and env->thumb in
6510 * gen_bx(). Reconstitute it.
6511 */
6512 excret = env->regs[15];
6513 if (env->thumb) {
6514 excret |= 1;
6515 }
6516
6517 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
6518 " previous exception %d\n",
6519 excret, env->v7m.exception);
6520
6521 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
6522 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
6523 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
6524 excret);
6525 }
6526
6527 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6528 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
6529 * we pick which FAULTMASK to clear.
6530 */
6531 if (!env->v7m.secure &&
6532 ((excret & R_V7M_EXCRET_ES_MASK) ||
6533 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
6534 sfault = 1;
6535 /* For all other purposes, treat ES as 0 (R_HXSR) */
6536 excret &= ~R_V7M_EXCRET_ES_MASK;
6537 }
6538 }
6539
6540 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
6541 /* Auto-clear FAULTMASK on return from other than NMI.
6542 * If the security extension is implemented then this only
6543 * happens if the raw execution priority is >= 0; the
6544 * value of the ES bit in the exception return value indicates
6545 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
6546 */
6547 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6548 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
6549 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
6550 env->v7m.faultmask[exc_secure] = 0;
6551 }
6552 } else {
6553 env->v7m.faultmask[M_REG_NS] = 0;
6554 }
6555 }
6556
6557 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
6558 exc_secure)) {
6559 case -1:
6560 /* attempt to exit an exception that isn't active */
6561 ufault = true;
6562 break;
6563 case 0:
6564 /* still an irq active now */
6565 break;
6566 case 1:
6567 /* we returned to base exception level, no nesting.
6568 * (In the pseudocode this is written using "NestedActivation != 1"
6569 * where we have 'rettobase == false'.)
6570 */
6571 rettobase = true;
6572 break;
6573 default:
6574 g_assert_not_reached();
6575 }
6576
6577 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
6578 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
6579 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6580 (excret & R_V7M_EXCRET_S_MASK);
6581
6582 if (arm_feature(env, ARM_FEATURE_V8)) {
6583 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6584 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
6585 * we choose to take the UsageFault.
6586 */
6587 if ((excret & R_V7M_EXCRET_S_MASK) ||
6588 (excret & R_V7M_EXCRET_ES_MASK) ||
6589 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
6590 ufault = true;
6591 }
6592 }
6593 if (excret & R_V7M_EXCRET_RES0_MASK) {
6594 ufault = true;
6595 }
6596 } else {
6597 /* For v7M we only recognize certain combinations of the low bits */
6598 switch (excret & 0xf) {
6599 case 1: /* Return to Handler */
6600 break;
6601 case 13: /* Return to Thread using Process stack */
6602 case 9: /* Return to Thread using Main stack */
6603 /* We only need to check NONBASETHRDENA for v7M, because in
6604 * v8M this bit does not exist (it is RES1).
6605 */
6606 if (!rettobase &&
6607 !(env->v7m.ccr[env->v7m.secure] &
6608 R_V7M_CCR_NONBASETHRDENA_MASK)) {
6609 ufault = true;
6610 }
6611 break;
6612 default:
6613 ufault = true;
6614 }
6615 }
6616
6617 if (sfault) {
6618 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
6619 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6620 v7m_exception_taken(cpu, excret, true);
6621 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6622 "stackframe: failed EXC_RETURN.ES validity check\n");
6623 return;
6624 }
6625
6626 if (ufault) {
6627 /* Bad exception return: instead of popping the exception
6628 * stack, directly take a usage fault on the current stack.
6629 */
6630 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6631 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
6632 v7m_exception_taken(cpu, excret, true);
6633 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6634 "stackframe: failed exception return integrity check\n");
6635 return;
6636 }
6637
6638 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
6639 * Handler mode (and will be until we write the new XPSR.Interrupt
6640 * field) this does not switch around the current stack pointer.
6641 */
6642 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
6643
6644 switch_v7m_security_state(env, return_to_secure);
6645
6646 {
6647 /* The stack pointer we should be reading the exception frame from
6648 * depends on bits in the magic exception return type value (and
6649 * for v8M isn't necessarily the stack pointer we will eventually
6650 * end up resuming execution with). Get a pointer to the location
6651 * in the CPU state struct where the SP we need is currently being
6652 * stored; we will use and modify it in place.
6653 * We use this limited C variable scope so we don't accidentally
6654 * use 'frame_sp_p' after we do something that makes it invalid.
6655 */
6656 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
6657 return_to_secure,
6658 !return_to_handler,
6659 return_to_sp_process);
6660 uint32_t frameptr = *frame_sp_p;
6661
6662 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
6663 arm_feature(env, ARM_FEATURE_V8)) {
6664 qemu_log_mask(LOG_GUEST_ERROR,
6665 "M profile exception return with non-8-aligned SP "
6666 "for destination state is UNPREDICTABLE\n");
6667 }
6668
6669 /* Do we need to pop callee-saved registers? */
6670 if (return_to_secure &&
6671 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
6672 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
6673 uint32_t expected_sig = 0xfefa125b;
6674 uint32_t actual_sig = ldl_phys(cs->as, frameptr);
6675
6676 if (expected_sig != actual_sig) {
6677 /* Take a SecureFault on the current stack */
6678 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
6679 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6680 v7m_exception_taken(cpu, excret, true);
6681 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6682 "stackframe: failed exception return integrity "
6683 "signature check\n");
6684 return;
6685 }
6686
6687 env->regs[4] = ldl_phys(cs->as, frameptr + 0x8);
6688 env->regs[5] = ldl_phys(cs->as, frameptr + 0xc);
6689 env->regs[6] = ldl_phys(cs->as, frameptr + 0x10);
6690 env->regs[7] = ldl_phys(cs->as, frameptr + 0x14);
6691 env->regs[8] = ldl_phys(cs->as, frameptr + 0x18);
6692 env->regs[9] = ldl_phys(cs->as, frameptr + 0x1c);
6693 env->regs[10] = ldl_phys(cs->as, frameptr + 0x20);
6694 env->regs[11] = ldl_phys(cs->as, frameptr + 0x24);
6695
6696 frameptr += 0x28;
6697 }
6698
6699 /* Pop registers. TODO: make these accesses use the correct
6700 * attributes and address space (S/NS, priv/unpriv) and handle
6701 * memory transaction failures.
6702 */
6703 env->regs[0] = ldl_phys(cs->as, frameptr);
6704 env->regs[1] = ldl_phys(cs->as, frameptr + 0x4);
6705 env->regs[2] = ldl_phys(cs->as, frameptr + 0x8);
6706 env->regs[3] = ldl_phys(cs->as, frameptr + 0xc);
6707 env->regs[12] = ldl_phys(cs->as, frameptr + 0x10);
6708 env->regs[14] = ldl_phys(cs->as, frameptr + 0x14);
6709 env->regs[15] = ldl_phys(cs->as, frameptr + 0x18);
6710
6711 /* Returning from an exception with a PC with bit 0 set is defined
6712 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
6713 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
6714 * the lsbit, and there are several RTOSes out there which incorrectly
6715 * assume the r15 in the stack frame should be a Thumb-style "lsbit
6716 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
6717 * complain about the badly behaved guest.
6718 */
6719 if (env->regs[15] & 1) {
6720 env->regs[15] &= ~1U;
6721 if (!arm_feature(env, ARM_FEATURE_V8)) {
6722 qemu_log_mask(LOG_GUEST_ERROR,
6723 "M profile return from interrupt with misaligned "
6724 "PC is UNPREDICTABLE on v7M\n");
6725 }
6726 }
6727
6728 xpsr = ldl_phys(cs->as, frameptr + 0x1c);
6729
6730 if (arm_feature(env, ARM_FEATURE_V8)) {
6731 /* For v8M we have to check whether the xPSR exception field
6732 * matches the EXCRET value for return to handler/thread
6733 * before we commit to changing the SP and xPSR.
6734 */
6735 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
6736 if (return_to_handler != will_be_handler) {
6737 /* Take an INVPC UsageFault on the current stack.
6738 * By this point we will have switched to the security state
6739 * for the background state, so this UsageFault will target
6740 * that state.
6741 */
6742 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
6743 env->v7m.secure);
6744 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6745 v7m_exception_taken(cpu, excret, true);
6746 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6747 "stackframe: failed exception return integrity "
6748 "check\n");
6749 return;
6750 }
6751 }
6752
6753 /* Commit to consuming the stack frame */
6754 frameptr += 0x20;
6755 /* Undo stack alignment (the SPREALIGN bit indicates that the original
6756 * pre-exception SP was not 8-aligned and we added a padding word to
6757 * align it, so we undo this by ORing in the bit that increases it
6758 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
6759 * would work too but a logical OR is how the pseudocode specifies it.)
6760 */
6761 if (xpsr & XPSR_SPREALIGN) {
6762 frameptr |= 4;
6763 }
6764 *frame_sp_p = frameptr;
6765 }
6766 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
6767 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
6768
6769 /* The restored xPSR exception field will be zero if we're
6770 * resuming in Thread mode. If that doesn't match what the
6771 * exception return excret specified then this is a UsageFault.
6772 * v7M requires we make this check here; v8M did it earlier.
6773 */
6774 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
6775 /* Take an INVPC UsageFault by pushing the stack again;
6776 * we know we're v7M so this is never a Secure UsageFault.
6777 */
6778 assert(!arm_feature(env, ARM_FEATURE_V8));
6779 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
6780 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6781 v7m_push_stack(cpu);
6782 v7m_exception_taken(cpu, excret, false);
6783 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
6784 "failed exception return integrity check\n");
6785 return;
6786 }
6787
6788 /* Otherwise, we have a successful exception exit. */
6789 arm_clear_exclusive(env);
6790 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
6791 }
6792
6793 static bool do_v7m_function_return(ARMCPU *cpu)
6794 {
6795 /* v8M security extensions magic function return.
6796 * We may either:
6797 * (1) throw an exception (longjump)
6798 * (2) return true if we successfully handled the function return
6799 * (3) return false if we failed a consistency check and have
6800 * pended a UsageFault that needs to be taken now
6801 *
6802 * At this point the magic return value is split between env->regs[15]
6803 * and env->thumb. We don't bother to reconstitute it because we don't
6804 * need it (all values are handled the same way).
6805 */
6806 CPUARMState *env = &cpu->env;
6807 uint32_t newpc, newpsr, newpsr_exc;
6808
6809 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
6810
6811 {
6812 bool threadmode, spsel;
6813 TCGMemOpIdx oi;
6814 ARMMMUIdx mmu_idx;
6815 uint32_t *frame_sp_p;
6816 uint32_t frameptr;
6817
6818 /* Pull the return address and IPSR from the Secure stack */
6819 threadmode = !arm_v7m_is_handler_mode(env);
6820 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
6821
6822 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
6823 frameptr = *frame_sp_p;
6824
6825 /* These loads may throw an exception (for MPU faults). We want to
6826 * do them as secure, so work out what MMU index that is.
6827 */
6828 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
6829 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
6830 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
6831 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
6832
6833 /* Consistency checks on new IPSR */
6834 newpsr_exc = newpsr & XPSR_EXCP;
6835 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
6836 (env->v7m.exception == 1 && newpsr_exc != 0))) {
6837 /* Pend the fault and tell our caller to take it */
6838 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
6839 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
6840 env->v7m.secure);
6841 qemu_log_mask(CPU_LOG_INT,
6842 "...taking INVPC UsageFault: "
6843 "IPSR consistency check failed\n");
6844 return false;
6845 }
6846
6847 *frame_sp_p = frameptr + 8;
6848 }
6849
6850 /* This invalidates frame_sp_p */
6851 switch_v7m_security_state(env, true);
6852 env->v7m.exception = newpsr_exc;
6853 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
6854 if (newpsr & XPSR_SFPA) {
6855 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
6856 }
6857 xpsr_write(env, 0, XPSR_IT);
6858 env->thumb = newpc & 1;
6859 env->regs[15] = newpc & ~1;
6860
6861 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
6862 return true;
6863 }
6864
6865 static void arm_log_exception(int idx)
6866 {
6867 if (qemu_loglevel_mask(CPU_LOG_INT)) {
6868 const char *exc = NULL;
6869 static const char * const excnames[] = {
6870 [EXCP_UDEF] = "Undefined Instruction",
6871 [EXCP_SWI] = "SVC",
6872 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
6873 [EXCP_DATA_ABORT] = "Data Abort",
6874 [EXCP_IRQ] = "IRQ",
6875 [EXCP_FIQ] = "FIQ",
6876 [EXCP_BKPT] = "Breakpoint",
6877 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
6878 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
6879 [EXCP_HVC] = "Hypervisor Call",
6880 [EXCP_HYP_TRAP] = "Hypervisor Trap",
6881 [EXCP_SMC] = "Secure Monitor Call",
6882 [EXCP_VIRQ] = "Virtual IRQ",
6883 [EXCP_VFIQ] = "Virtual FIQ",
6884 [EXCP_SEMIHOST] = "Semihosting call",
6885 [EXCP_NOCP] = "v7M NOCP UsageFault",
6886 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
6887 };
6888
6889 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
6890 exc = excnames[idx];
6891 }
6892 if (!exc) {
6893 exc = "unknown";
6894 }
6895 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
6896 }
6897 }
6898
6899 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
6900 uint32_t addr, uint16_t *insn)
6901 {
6902 /* Load a 16-bit portion of a v7M instruction, returning true on success,
6903 * or false on failure (in which case we will have pended the appropriate
6904 * exception).
6905 * We need to do the instruction fetch's MPU and SAU checks
6906 * like this because there is no MMU index that would allow
6907 * doing the load with a single function call. Instead we must
6908 * first check that the security attributes permit the load
6909 * and that they don't mismatch on the two halves of the instruction,
6910 * and then we do the load as a secure load (ie using the security
6911 * attributes of the address, not the CPU, as architecturally required).
6912 */
6913 CPUState *cs = CPU(cpu);
6914 CPUARMState *env = &cpu->env;
6915 V8M_SAttributes sattrs = {};
6916 MemTxAttrs attrs = {};
6917 ARMMMUFaultInfo fi = {};
6918 MemTxResult txres;
6919 target_ulong page_size;
6920 hwaddr physaddr;
6921 int prot;
6922 uint32_t fsr;
6923
6924 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
6925 if (!sattrs.nsc || sattrs.ns) {
6926 /* This must be the second half of the insn, and it straddles a
6927 * region boundary with the second half not being S&NSC.
6928 */
6929 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
6930 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6931 qemu_log_mask(CPU_LOG_INT,
6932 "...really SecureFault with SFSR.INVEP\n");
6933 return false;
6934 }
6935 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
6936 &physaddr, &attrs, &prot, &page_size, &fsr, &fi, NULL)) {
6937 /* the MPU lookup failed */
6938 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
6939 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
6940 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
6941 return false;
6942 }
6943 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
6944 attrs, &txres);
6945 if (txres != MEMTX_OK) {
6946 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
6947 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
6948 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
6949 return false;
6950 }
6951 return true;
6952 }
6953
6954 static bool v7m_handle_execute_nsc(ARMCPU *cpu)
6955 {
6956 /* Check whether this attempt to execute code in a Secure & NS-Callable
6957 * memory region is for an SG instruction; if so, then emulate the
6958 * effect of the SG instruction and return true. Otherwise pend
6959 * the correct kind of exception and return false.
6960 */
6961 CPUARMState *env = &cpu->env;
6962 ARMMMUIdx mmu_idx;
6963 uint16_t insn;
6964
6965 /* We should never get here unless get_phys_addr_pmsav8() caused
6966 * an exception for NS executing in S&NSC memory.
6967 */
6968 assert(!env->v7m.secure);
6969 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
6970
6971 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
6972 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
6973
6974 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
6975 return false;
6976 }
6977
6978 if (!env->thumb) {
6979 goto gen_invep;
6980 }
6981
6982 if (insn != 0xe97f) {
6983 /* Not an SG instruction first half (we choose the IMPDEF
6984 * early-SG-check option).
6985 */
6986 goto gen_invep;
6987 }
6988
6989 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
6990 return false;
6991 }
6992
6993 if (insn != 0xe97f) {
6994 /* Not an SG instruction second half (yes, both halves of the SG
6995 * insn have the same hex value)
6996 */
6997 goto gen_invep;
6998 }
6999
7000 /* OK, we have confirmed that we really have an SG instruction.
7001 * We know we're NS in S memory so don't need to repeat those checks.
7002 */
7003 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7004 ", executing it\n", env->regs[15]);
7005 env->regs[14] &= ~1;
7006 switch_v7m_security_state(env, true);
7007 xpsr_write(env, 0, XPSR_IT);
7008 env->regs[15] += 4;
7009 return true;
7010
7011 gen_invep:
7012 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7013 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7014 qemu_log_mask(CPU_LOG_INT,
7015 "...really SecureFault with SFSR.INVEP\n");
7016 return false;
7017 }
7018
7019 void arm_v7m_cpu_do_interrupt(CPUState *cs)
7020 {
7021 ARMCPU *cpu = ARM_CPU(cs);
7022 CPUARMState *env = &cpu->env;
7023 uint32_t lr;
7024
7025 arm_log_exception(cs->exception_index);
7026
7027 /* For exceptions we just mark as pending on the NVIC, and let that
7028 handle it. */
7029 switch (cs->exception_index) {
7030 case EXCP_UDEF:
7031 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7032 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
7033 break;
7034 case EXCP_NOCP:
7035 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7036 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
7037 break;
7038 case EXCP_INVSTATE:
7039 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7040 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
7041 break;
7042 case EXCP_SWI:
7043 /* The PC already points to the next instruction. */
7044 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
7045 break;
7046 case EXCP_PREFETCH_ABORT:
7047 case EXCP_DATA_ABORT:
7048 /* Note that for M profile we don't have a guest facing FSR, but
7049 * the env->exception.fsr will be populated by the code that
7050 * raises the fault, in the A profile short-descriptor format.
7051 */
7052 switch (env->exception.fsr & 0xf) {
7053 case M_FAKE_FSR_NSC_EXEC:
7054 /* Exception generated when we try to execute code at an address
7055 * which is marked as Secure & Non-Secure Callable and the CPU
7056 * is in the Non-Secure state. The only instruction which can
7057 * be executed like this is SG (and that only if both halves of
7058 * the SG instruction have the same security attributes.)
7059 * Everything else must generate an INVEP SecureFault, so we
7060 * emulate the SG instruction here.
7061 */
7062 if (v7m_handle_execute_nsc(cpu)) {
7063 return;
7064 }
7065 break;
7066 case M_FAKE_FSR_SFAULT:
7067 /* Various flavours of SecureFault for attempts to execute or
7068 * access data in the wrong security state.
7069 */
7070 switch (cs->exception_index) {
7071 case EXCP_PREFETCH_ABORT:
7072 if (env->v7m.secure) {
7073 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7074 qemu_log_mask(CPU_LOG_INT,
7075 "...really SecureFault with SFSR.INVTRAN\n");
7076 } else {
7077 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7078 qemu_log_mask(CPU_LOG_INT,
7079 "...really SecureFault with SFSR.INVEP\n");
7080 }
7081 break;
7082 case EXCP_DATA_ABORT:
7083 /* This must be an NS access to S memory */
7084 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7085 qemu_log_mask(CPU_LOG_INT,
7086 "...really SecureFault with SFSR.AUVIOL\n");
7087 break;
7088 }
7089 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7090 break;
7091 case 0x8: /* External Abort */
7092 switch (cs->exception_index) {
7093 case EXCP_PREFETCH_ABORT:
7094 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7095 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
7096 break;
7097 case EXCP_DATA_ABORT:
7098 env->v7m.cfsr[M_REG_NS] |=
7099 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
7100 env->v7m.bfar = env->exception.vaddress;
7101 qemu_log_mask(CPU_LOG_INT,
7102 "...with CFSR.PRECISERR and BFAR 0x%x\n",
7103 env->v7m.bfar);
7104 break;
7105 }
7106 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7107 break;
7108 default:
7109 /* All other FSR values are either MPU faults or "can't happen
7110 * for M profile" cases.
7111 */
7112 switch (cs->exception_index) {
7113 case EXCP_PREFETCH_ABORT:
7114 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7115 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7116 break;
7117 case EXCP_DATA_ABORT:
7118 env->v7m.cfsr[env->v7m.secure] |=
7119 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
7120 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
7121 qemu_log_mask(CPU_LOG_INT,
7122 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
7123 env->v7m.mmfar[env->v7m.secure]);
7124 break;
7125 }
7126 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7127 env->v7m.secure);
7128 break;
7129 }
7130 break;
7131 case EXCP_BKPT:
7132 if (semihosting_enabled()) {
7133 int nr;
7134 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
7135 if (nr == 0xab) {
7136 env->regs[15] += 2;
7137 qemu_log_mask(CPU_LOG_INT,
7138 "...handling as semihosting call 0x%x\n",
7139 env->regs[0]);
7140 env->regs[0] = do_arm_semihosting(env);
7141 return;
7142 }
7143 }
7144 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
7145 break;
7146 case EXCP_IRQ:
7147 break;
7148 case EXCP_EXCEPTION_EXIT:
7149 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7150 /* Must be v8M security extension function return */
7151 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7152 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7153 if (do_v7m_function_return(cpu)) {
7154 return;
7155 }
7156 } else {
7157 do_v7m_exception_exit(cpu);
7158 return;
7159 }
7160 break;
7161 default:
7162 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7163 return; /* Never happens. Keep compiler happy. */
7164 }
7165
7166 if (arm_feature(env, ARM_FEATURE_V8)) {
7167 lr = R_V7M_EXCRET_RES1_MASK |
7168 R_V7M_EXCRET_DCRS_MASK |
7169 R_V7M_EXCRET_FTYPE_MASK;
7170 /* The S bit indicates whether we should return to Secure
7171 * or NonSecure (ie our current state).
7172 * The ES bit indicates whether we're taking this exception
7173 * to Secure or NonSecure (ie our target state). We set it
7174 * later, in v7m_exception_taken().
7175 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7176 * This corresponds to the ARM ARM pseudocode for v8M setting
7177 * some LR bits in PushStack() and some in ExceptionTaken();
7178 * the distinction matters for the tailchain cases where we
7179 * can take an exception without pushing the stack.
7180 */
7181 if (env->v7m.secure) {
7182 lr |= R_V7M_EXCRET_S_MASK;
7183 }
7184 } else {
7185 lr = R_V7M_EXCRET_RES1_MASK |
7186 R_V7M_EXCRET_S_MASK |
7187 R_V7M_EXCRET_DCRS_MASK |
7188 R_V7M_EXCRET_FTYPE_MASK |
7189 R_V7M_EXCRET_ES_MASK;
7190 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7191 lr |= R_V7M_EXCRET_SPSEL_MASK;
7192 }
7193 }
7194 if (!arm_v7m_is_handler_mode(env)) {
7195 lr |= R_V7M_EXCRET_MODE_MASK;
7196 }
7197
7198 v7m_push_stack(cpu);
7199 v7m_exception_taken(cpu, lr, false);
7200 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
7201 }
7202
7203 /* Function used to synchronize QEMU's AArch64 register set with AArch32
7204 * register set. This is necessary when switching between AArch32 and AArch64
7205 * execution state.
7206 */
7207 void aarch64_sync_32_to_64(CPUARMState *env)
7208 {
7209 int i;
7210 uint32_t mode = env->uncached_cpsr & CPSR_M;
7211
7212 /* We can blanket copy R[0:7] to X[0:7] */
7213 for (i = 0; i < 8; i++) {
7214 env->xregs[i] = env->regs[i];
7215 }
7216
7217 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7218 * Otherwise, they come from the banked user regs.
7219 */
7220 if (mode == ARM_CPU_MODE_FIQ) {
7221 for (i = 8; i < 13; i++) {
7222 env->xregs[i] = env->usr_regs[i - 8];
7223 }
7224 } else {
7225 for (i = 8; i < 13; i++) {
7226 env->xregs[i] = env->regs[i];
7227 }
7228 }
7229
7230 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7231 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7232 * from the mode banked register.
7233 */
7234 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7235 env->xregs[13] = env->regs[13];
7236 env->xregs[14] = env->regs[14];
7237 } else {
7238 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7239 /* HYP is an exception in that it is copied from r14 */
7240 if (mode == ARM_CPU_MODE_HYP) {
7241 env->xregs[14] = env->regs[14];
7242 } else {
7243 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7244 }
7245 }
7246
7247 if (mode == ARM_CPU_MODE_HYP) {
7248 env->xregs[15] = env->regs[13];
7249 } else {
7250 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7251 }
7252
7253 if (mode == ARM_CPU_MODE_IRQ) {
7254 env->xregs[16] = env->regs[14];
7255 env->xregs[17] = env->regs[13];
7256 } else {
7257 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7258 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
7259 }
7260
7261 if (mode == ARM_CPU_MODE_SVC) {
7262 env->xregs[18] = env->regs[14];
7263 env->xregs[19] = env->regs[13];
7264 } else {
7265 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7266 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
7267 }
7268
7269 if (mode == ARM_CPU_MODE_ABT) {
7270 env->xregs[20] = env->regs[14];
7271 env->xregs[21] = env->regs[13];
7272 } else {
7273 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7274 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
7275 }
7276
7277 if (mode == ARM_CPU_MODE_UND) {
7278 env->xregs[22] = env->regs[14];
7279 env->xregs[23] = env->regs[13];
7280 } else {
7281 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7282 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
7283 }
7284
7285 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7286 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7287 * FIQ bank for r8-r14.
7288 */
7289 if (mode == ARM_CPU_MODE_FIQ) {
7290 for (i = 24; i < 31; i++) {
7291 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7292 }
7293 } else {
7294 for (i = 24; i < 29; i++) {
7295 env->xregs[i] = env->fiq_regs[i - 24];
7296 }
7297 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7298 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7299 }
7300
7301 env->pc = env->regs[15];
7302 }
7303
7304 /* Function used to synchronize QEMU's AArch32 register set with AArch64
7305 * register set. This is necessary when switching between AArch32 and AArch64
7306 * execution state.
7307 */
7308 void aarch64_sync_64_to_32(CPUARMState *env)
7309 {
7310 int i;
7311 uint32_t mode = env->uncached_cpsr & CPSR_M;
7312
7313 /* We can blanket copy X[0:7] to R[0:7] */
7314 for (i = 0; i < 8; i++) {
7315 env->regs[i] = env->xregs[i];
7316 }
7317
7318 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7319 * Otherwise, we copy x8-x12 into the banked user regs.
7320 */
7321 if (mode == ARM_CPU_MODE_FIQ) {
7322 for (i = 8; i < 13; i++) {
7323 env->usr_regs[i - 8] = env->xregs[i];
7324 }
7325 } else {
7326 for (i = 8; i < 13; i++) {
7327 env->regs[i] = env->xregs[i];
7328 }
7329 }
7330
7331 /* Registers r13 & r14 depend on the current mode.
7332 * If we are in a given mode, we copy the corresponding x registers to r13
7333 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7334 * for the mode.
7335 */
7336 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7337 env->regs[13] = env->xregs[13];
7338 env->regs[14] = env->xregs[14];
7339 } else {
7340 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7341
7342 /* HYP is an exception in that it does not have its own banked r14 but
7343 * shares the USR r14
7344 */
7345 if (mode == ARM_CPU_MODE_HYP) {
7346 env->regs[14] = env->xregs[14];
7347 } else {
7348 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7349 }
7350 }
7351
7352 if (mode == ARM_CPU_MODE_HYP) {
7353 env->regs[13] = env->xregs[15];
7354 } else {
7355 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7356 }
7357
7358 if (mode == ARM_CPU_MODE_IRQ) {
7359 env->regs[14] = env->xregs[16];
7360 env->regs[13] = env->xregs[17];
7361 } else {
7362 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7363 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
7364 }
7365
7366 if (mode == ARM_CPU_MODE_SVC) {
7367 env->regs[14] = env->xregs[18];
7368 env->regs[13] = env->xregs[19];
7369 } else {
7370 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7371 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
7372 }
7373
7374 if (mode == ARM_CPU_MODE_ABT) {
7375 env->regs[14] = env->xregs[20];
7376 env->regs[13] = env->xregs[21];
7377 } else {
7378 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7379 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
7380 }
7381
7382 if (mode == ARM_CPU_MODE_UND) {
7383 env->regs[14] = env->xregs[22];
7384 env->regs[13] = env->xregs[23];
7385 } else {
7386 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7387 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
7388 }
7389
7390 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7391 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7392 * FIQ bank for r8-r14.
7393 */
7394 if (mode == ARM_CPU_MODE_FIQ) {
7395 for (i = 24; i < 31; i++) {
7396 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7397 }
7398 } else {
7399 for (i = 24; i < 29; i++) {
7400 env->fiq_regs[i - 24] = env->xregs[i];
7401 }
7402 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7403 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7404 }
7405
7406 env->regs[15] = env->pc;
7407 }
7408
7409 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
7410 {
7411 ARMCPU *cpu = ARM_CPU(cs);
7412 CPUARMState *env = &cpu->env;
7413 uint32_t addr;
7414 uint32_t mask;
7415 int new_mode;
7416 uint32_t offset;
7417 uint32_t moe;
7418
7419 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7420 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7421 case EC_BREAKPOINT:
7422 case EC_BREAKPOINT_SAME_EL:
7423 moe = 1;
7424 break;
7425 case EC_WATCHPOINT:
7426 case EC_WATCHPOINT_SAME_EL:
7427 moe = 10;
7428 break;
7429 case EC_AA32_BKPT:
7430 moe = 3;
7431 break;
7432 case EC_VECTORCATCH:
7433 moe = 5;
7434 break;
7435 default:
7436 moe = 0;
7437 break;
7438 }
7439
7440 if (moe) {
7441 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7442 }
7443
7444 /* TODO: Vectored interrupt controller. */
7445 switch (cs->exception_index) {
7446 case EXCP_UDEF:
7447 new_mode = ARM_CPU_MODE_UND;
7448 addr = 0x04;
7449 mask = CPSR_I;
7450 if (env->thumb)
7451 offset = 2;
7452 else
7453 offset = 4;
7454 break;
7455 case EXCP_SWI:
7456 new_mode = ARM_CPU_MODE_SVC;
7457 addr = 0x08;
7458 mask = CPSR_I;
7459 /* The PC already points to the next instruction. */
7460 offset = 0;
7461 break;
7462 case EXCP_BKPT:
7463 env->exception.fsr = 2;
7464 /* Fall through to prefetch abort. */
7465 case EXCP_PREFETCH_ABORT:
7466 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
7467 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
7468 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
7469 env->exception.fsr, (uint32_t)env->exception.vaddress);
7470 new_mode = ARM_CPU_MODE_ABT;
7471 addr = 0x0c;
7472 mask = CPSR_A | CPSR_I;
7473 offset = 4;
7474 break;
7475 case EXCP_DATA_ABORT:
7476 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
7477 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
7478 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
7479 env->exception.fsr,
7480 (uint32_t)env->exception.vaddress);
7481 new_mode = ARM_CPU_MODE_ABT;
7482 addr = 0x10;
7483 mask = CPSR_A | CPSR_I;
7484 offset = 8;
7485 break;
7486 case EXCP_IRQ:
7487 new_mode = ARM_CPU_MODE_IRQ;
7488 addr = 0x18;
7489 /* Disable IRQ and imprecise data aborts. */
7490 mask = CPSR_A | CPSR_I;
7491 offset = 4;
7492 if (env->cp15.scr_el3 & SCR_IRQ) {
7493 /* IRQ routed to monitor mode */
7494 new_mode = ARM_CPU_MODE_MON;
7495 mask |= CPSR_F;
7496 }
7497 break;
7498 case EXCP_FIQ:
7499 new_mode = ARM_CPU_MODE_FIQ;
7500 addr = 0x1c;
7501 /* Disable FIQ, IRQ and imprecise data aborts. */
7502 mask = CPSR_A | CPSR_I | CPSR_F;
7503 if (env->cp15.scr_el3 & SCR_FIQ) {
7504 /* FIQ routed to monitor mode */
7505 new_mode = ARM_CPU_MODE_MON;
7506 }
7507 offset = 4;
7508 break;
7509 case EXCP_VIRQ:
7510 new_mode = ARM_CPU_MODE_IRQ;
7511 addr = 0x18;
7512 /* Disable IRQ and imprecise data aborts. */
7513 mask = CPSR_A | CPSR_I;
7514 offset = 4;
7515 break;
7516 case EXCP_VFIQ:
7517 new_mode = ARM_CPU_MODE_FIQ;
7518 addr = 0x1c;
7519 /* Disable FIQ, IRQ and imprecise data aborts. */
7520 mask = CPSR_A | CPSR_I | CPSR_F;
7521 offset = 4;
7522 break;
7523 case EXCP_SMC:
7524 new_mode = ARM_CPU_MODE_MON;
7525 addr = 0x08;
7526 mask = CPSR_A | CPSR_I | CPSR_F;
7527 offset = 0;
7528 break;
7529 default:
7530 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7531 return; /* Never happens. Keep compiler happy. */
7532 }
7533
7534 if (new_mode == ARM_CPU_MODE_MON) {
7535 addr += env->cp15.mvbar;
7536 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
7537 /* High vectors. When enabled, base address cannot be remapped. */
7538 addr += 0xffff0000;
7539 } else {
7540 /* ARM v7 architectures provide a vector base address register to remap
7541 * the interrupt vector table.
7542 * This register is only followed in non-monitor mode, and is banked.
7543 * Note: only bits 31:5 are valid.
7544 */
7545 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
7546 }
7547
7548 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
7549 env->cp15.scr_el3 &= ~SCR_NS;
7550 }
7551
7552 switch_mode (env, new_mode);
7553 /* For exceptions taken to AArch32 we must clear the SS bit in both
7554 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
7555 */
7556 env->uncached_cpsr &= ~PSTATE_SS;
7557 env->spsr = cpsr_read(env);
7558 /* Clear IT bits. */
7559 env->condexec_bits = 0;
7560 /* Switch to the new mode, and to the correct instruction set. */
7561 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
7562 /* Set new mode endianness */
7563 env->uncached_cpsr &= ~CPSR_E;
7564 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
7565 env->uncached_cpsr |= CPSR_E;
7566 }
7567 env->daif |= mask;
7568 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
7569 * and we should just guard the thumb mode on V4 */
7570 if (arm_feature(env, ARM_FEATURE_V4T)) {
7571 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
7572 }
7573 env->regs[14] = env->regs[15] + offset;
7574 env->regs[15] = addr;
7575 }
7576
7577 /* Handle exception entry to a target EL which is using AArch64 */
7578 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
7579 {
7580 ARMCPU *cpu = ARM_CPU(cs);
7581 CPUARMState *env = &cpu->env;
7582 unsigned int new_el = env->exception.target_el;
7583 target_ulong addr = env->cp15.vbar_el[new_el];
7584 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
7585
7586 if (arm_current_el(env) < new_el) {
7587 /* Entry vector offset depends on whether the implemented EL
7588 * immediately lower than the target level is using AArch32 or AArch64
7589 */
7590 bool is_aa64;
7591
7592 switch (new_el) {
7593 case 3:
7594 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
7595 break;
7596 case 2:
7597 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
7598 break;
7599 case 1:
7600 is_aa64 = is_a64(env);
7601 break;
7602 default:
7603 g_assert_not_reached();
7604 }
7605
7606 if (is_aa64) {
7607 addr += 0x400;
7608 } else {
7609 addr += 0x600;
7610 }
7611 } else if (pstate_read(env) & PSTATE_SP) {
7612 addr += 0x200;
7613 }
7614
7615 switch (cs->exception_index) {
7616 case EXCP_PREFETCH_ABORT:
7617 case EXCP_DATA_ABORT:
7618 env->cp15.far_el[new_el] = env->exception.vaddress;
7619 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
7620 env->cp15.far_el[new_el]);
7621 /* fall through */
7622 case EXCP_BKPT:
7623 case EXCP_UDEF:
7624 case EXCP_SWI:
7625 case EXCP_HVC:
7626 case EXCP_HYP_TRAP:
7627 case EXCP_SMC:
7628 env->cp15.esr_el[new_el] = env->exception.syndrome;
7629 break;
7630 case EXCP_IRQ:
7631 case EXCP_VIRQ:
7632 addr += 0x80;
7633 break;
7634 case EXCP_FIQ:
7635 case EXCP_VFIQ:
7636 addr += 0x100;
7637 break;
7638 case EXCP_SEMIHOST:
7639 qemu_log_mask(CPU_LOG_INT,
7640 "...handling as semihosting call 0x%" PRIx64 "\n",
7641 env->xregs[0]);
7642 env->xregs[0] = do_arm_semihosting(env);
7643 return;
7644 default:
7645 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7646 }
7647
7648 if (is_a64(env)) {
7649 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
7650 aarch64_save_sp(env, arm_current_el(env));
7651 env->elr_el[new_el] = env->pc;
7652 } else {
7653 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
7654 env->elr_el[new_el] = env->regs[15];
7655
7656 aarch64_sync_32_to_64(env);
7657
7658 env->condexec_bits = 0;
7659 }
7660 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
7661 env->elr_el[new_el]);
7662
7663 pstate_write(env, PSTATE_DAIF | new_mode);
7664 env->aarch64 = 1;
7665 aarch64_restore_sp(env, new_el);
7666
7667 env->pc = addr;
7668
7669 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
7670 new_el, env->pc, pstate_read(env));
7671 }
7672
7673 static inline bool check_for_semihosting(CPUState *cs)
7674 {
7675 /* Check whether this exception is a semihosting call; if so
7676 * then handle it and return true; otherwise return false.
7677 */
7678 ARMCPU *cpu = ARM_CPU(cs);
7679 CPUARMState *env = &cpu->env;
7680
7681 if (is_a64(env)) {
7682 if (cs->exception_index == EXCP_SEMIHOST) {
7683 /* This is always the 64-bit semihosting exception.
7684 * The "is this usermode" and "is semihosting enabled"
7685 * checks have been done at translate time.
7686 */
7687 qemu_log_mask(CPU_LOG_INT,
7688 "...handling as semihosting call 0x%" PRIx64 "\n",
7689 env->xregs[0]);
7690 env->xregs[0] = do_arm_semihosting(env);
7691 return true;
7692 }
7693 return false;
7694 } else {
7695 uint32_t imm;
7696
7697 /* Only intercept calls from privileged modes, to provide some
7698 * semblance of security.
7699 */
7700 if (cs->exception_index != EXCP_SEMIHOST &&
7701 (!semihosting_enabled() ||
7702 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
7703 return false;
7704 }
7705
7706 switch (cs->exception_index) {
7707 case EXCP_SEMIHOST:
7708 /* This is always a semihosting call; the "is this usermode"
7709 * and "is semihosting enabled" checks have been done at
7710 * translate time.
7711 */
7712 break;
7713 case EXCP_SWI:
7714 /* Check for semihosting interrupt. */
7715 if (env->thumb) {
7716 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
7717 & 0xff;
7718 if (imm == 0xab) {
7719 break;
7720 }
7721 } else {
7722 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
7723 & 0xffffff;
7724 if (imm == 0x123456) {
7725 break;
7726 }
7727 }
7728 return false;
7729 case EXCP_BKPT:
7730 /* See if this is a semihosting syscall. */
7731 if (env->thumb) {
7732 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
7733 & 0xff;
7734 if (imm == 0xab) {
7735 env->regs[15] += 2;
7736 break;
7737 }
7738 }
7739 return false;
7740 default:
7741 return false;
7742 }
7743
7744 qemu_log_mask(CPU_LOG_INT,
7745 "...handling as semihosting call 0x%x\n",
7746 env->regs[0]);
7747 env->regs[0] = do_arm_semihosting(env);
7748 return true;
7749 }
7750 }
7751
7752 /* Handle a CPU exception for A and R profile CPUs.
7753 * Do any appropriate logging, handle PSCI calls, and then hand off
7754 * to the AArch64-entry or AArch32-entry function depending on the
7755 * target exception level's register width.
7756 */
7757 void arm_cpu_do_interrupt(CPUState *cs)
7758 {
7759 ARMCPU *cpu = ARM_CPU(cs);
7760 CPUARMState *env = &cpu->env;
7761 unsigned int new_el = env->exception.target_el;
7762
7763 assert(!arm_feature(env, ARM_FEATURE_M));
7764
7765 arm_log_exception(cs->exception_index);
7766 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
7767 new_el);
7768 if (qemu_loglevel_mask(CPU_LOG_INT)
7769 && !excp_is_internal(cs->exception_index)) {
7770 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
7771 env->exception.syndrome >> ARM_EL_EC_SHIFT,
7772 env->exception.syndrome);
7773 }
7774
7775 if (arm_is_psci_call(cpu, cs->exception_index)) {
7776 arm_handle_psci_call(cpu);
7777 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
7778 return;
7779 }
7780
7781 /* Semihosting semantics depend on the register width of the
7782 * code that caused the exception, not the target exception level,
7783 * so must be handled here.
7784 */
7785 if (check_for_semihosting(cs)) {
7786 return;
7787 }
7788
7789 assert(!excp_is_internal(cs->exception_index));
7790 if (arm_el_is_aa64(env, new_el)) {
7791 arm_cpu_do_interrupt_aarch64(cs);
7792 } else {
7793 arm_cpu_do_interrupt_aarch32(cs);
7794 }
7795
7796 /* Hooks may change global state so BQL should be held, also the
7797 * BQL needs to be held for any modification of
7798 * cs->interrupt_request.
7799 */
7800 g_assert(qemu_mutex_iothread_locked());
7801
7802 arm_call_el_change_hook(cpu);
7803
7804 if (!kvm_enabled()) {
7805 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
7806 }
7807 }
7808
7809 /* Return the exception level which controls this address translation regime */
7810 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
7811 {
7812 switch (mmu_idx) {
7813 case ARMMMUIdx_S2NS:
7814 case ARMMMUIdx_S1E2:
7815 return 2;
7816 case ARMMMUIdx_S1E3:
7817 return 3;
7818 case ARMMMUIdx_S1SE0:
7819 return arm_el_is_aa64(env, 3) ? 1 : 3;
7820 case ARMMMUIdx_S1SE1:
7821 case ARMMMUIdx_S1NSE0:
7822 case ARMMMUIdx_S1NSE1:
7823 case ARMMMUIdx_MPriv:
7824 case ARMMMUIdx_MNegPri:
7825 case ARMMMUIdx_MUser:
7826 case ARMMMUIdx_MSPriv:
7827 case ARMMMUIdx_MSNegPri:
7828 case ARMMMUIdx_MSUser:
7829 return 1;
7830 default:
7831 g_assert_not_reached();
7832 }
7833 }
7834
7835 /* Return the SCTLR value which controls this address translation regime */
7836 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
7837 {
7838 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
7839 }
7840
7841 /* Return true if the specified stage of address translation is disabled */
7842 static inline bool regime_translation_disabled(CPUARMState *env,
7843 ARMMMUIdx mmu_idx)
7844 {
7845 if (arm_feature(env, ARM_FEATURE_M)) {
7846 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
7847 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
7848 case R_V7M_MPU_CTRL_ENABLE_MASK:
7849 /* Enabled, but not for HardFault and NMI */
7850 return mmu_idx == ARMMMUIdx_MNegPri ||
7851 mmu_idx == ARMMMUIdx_MSNegPri;
7852 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
7853 /* Enabled for all cases */
7854 return false;
7855 case 0:
7856 default:
7857 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
7858 * we warned about that in armv7m_nvic.c when the guest set it.
7859 */
7860 return true;
7861 }
7862 }
7863
7864 if (mmu_idx == ARMMMUIdx_S2NS) {
7865 return (env->cp15.hcr_el2 & HCR_VM) == 0;
7866 }
7867 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
7868 }
7869
7870 static inline bool regime_translation_big_endian(CPUARMState *env,
7871 ARMMMUIdx mmu_idx)
7872 {
7873 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
7874 }
7875
7876 /* Return the TCR controlling this translation regime */
7877 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
7878 {
7879 if (mmu_idx == ARMMMUIdx_S2NS) {
7880 return &env->cp15.vtcr_el2;
7881 }
7882 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
7883 }
7884
7885 /* Convert a possible stage1+2 MMU index into the appropriate
7886 * stage 1 MMU index
7887 */
7888 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
7889 {
7890 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
7891 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
7892 }
7893 return mmu_idx;
7894 }
7895
7896 /* Returns TBI0 value for current regime el */
7897 uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
7898 {
7899 TCR *tcr;
7900 uint32_t el;
7901
7902 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
7903 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7904 */
7905 mmu_idx = stage_1_mmu_idx(mmu_idx);
7906
7907 tcr = regime_tcr(env, mmu_idx);
7908 el = regime_el(env, mmu_idx);
7909
7910 if (el > 1) {
7911 return extract64(tcr->raw_tcr, 20, 1);
7912 } else {
7913 return extract64(tcr->raw_tcr, 37, 1);
7914 }
7915 }
7916
7917 /* Returns TBI1 value for current regime el */
7918 uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
7919 {
7920 TCR *tcr;
7921 uint32_t el;
7922
7923 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
7924 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7925 */
7926 mmu_idx = stage_1_mmu_idx(mmu_idx);
7927
7928 tcr = regime_tcr(env, mmu_idx);
7929 el = regime_el(env, mmu_idx);
7930
7931 if (el > 1) {
7932 return 0;
7933 } else {
7934 return extract64(tcr->raw_tcr, 38, 1);
7935 }
7936 }
7937
7938 /* Return the TTBR associated with this translation regime */
7939 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
7940 int ttbrn)
7941 {
7942 if (mmu_idx == ARMMMUIdx_S2NS) {
7943 return env->cp15.vttbr_el2;
7944 }
7945 if (ttbrn == 0) {
7946 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
7947 } else {
7948 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
7949 }
7950 }
7951
7952 /* Return true if the translation regime is using LPAE format page tables */
7953 static inline bool regime_using_lpae_format(CPUARMState *env,
7954 ARMMMUIdx mmu_idx)
7955 {
7956 int el = regime_el(env, mmu_idx);
7957 if (el == 2 || arm_el_is_aa64(env, el)) {
7958 return true;
7959 }
7960 if (arm_feature(env, ARM_FEATURE_LPAE)
7961 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
7962 return true;
7963 }
7964 return false;
7965 }
7966
7967 /* Returns true if the stage 1 translation regime is using LPAE format page
7968 * tables. Used when raising alignment exceptions, whose FSR changes depending
7969 * on whether the long or short descriptor format is in use. */
7970 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
7971 {
7972 mmu_idx = stage_1_mmu_idx(mmu_idx);
7973
7974 return regime_using_lpae_format(env, mmu_idx);
7975 }
7976
7977 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
7978 {
7979 switch (mmu_idx) {
7980 case ARMMMUIdx_S1SE0:
7981 case ARMMMUIdx_S1NSE0:
7982 case ARMMMUIdx_MUser:
7983 return true;
7984 default:
7985 return false;
7986 case ARMMMUIdx_S12NSE0:
7987 case ARMMMUIdx_S12NSE1:
7988 g_assert_not_reached();
7989 }
7990 }
7991
7992 /* Translate section/page access permissions to page
7993 * R/W protection flags
7994 *
7995 * @env: CPUARMState
7996 * @mmu_idx: MMU index indicating required translation regime
7997 * @ap: The 3-bit access permissions (AP[2:0])
7998 * @domain_prot: The 2-bit domain access permissions
7999 */
8000 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8001 int ap, int domain_prot)
8002 {
8003 bool is_user = regime_is_user(env, mmu_idx);
8004
8005 if (domain_prot == 3) {
8006 return PAGE_READ | PAGE_WRITE;
8007 }
8008
8009 switch (ap) {
8010 case 0:
8011 if (arm_feature(env, ARM_FEATURE_V7)) {
8012 return 0;
8013 }
8014 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8015 case SCTLR_S:
8016 return is_user ? 0 : PAGE_READ;
8017 case SCTLR_R:
8018 return PAGE_READ;
8019 default:
8020 return 0;
8021 }
8022 case 1:
8023 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8024 case 2:
8025 if (is_user) {
8026 return PAGE_READ;
8027 } else {
8028 return PAGE_READ | PAGE_WRITE;
8029 }
8030 case 3:
8031 return PAGE_READ | PAGE_WRITE;
8032 case 4: /* Reserved. */
8033 return 0;
8034 case 5:
8035 return is_user ? 0 : PAGE_READ;
8036 case 6:
8037 return PAGE_READ;
8038 case 7:
8039 if (!arm_feature(env, ARM_FEATURE_V6K)) {
8040 return 0;
8041 }
8042 return PAGE_READ;
8043 default:
8044 g_assert_not_reached();
8045 }
8046 }
8047
8048 /* Translate section/page access permissions to page
8049 * R/W protection flags.
8050 *
8051 * @ap: The 2-bit simple AP (AP[2:1])
8052 * @is_user: TRUE if accessing from PL0
8053 */
8054 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
8055 {
8056 switch (ap) {
8057 case 0:
8058 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8059 case 1:
8060 return PAGE_READ | PAGE_WRITE;
8061 case 2:
8062 return is_user ? 0 : PAGE_READ;
8063 case 3:
8064 return PAGE_READ;
8065 default:
8066 g_assert_not_reached();
8067 }
8068 }
8069
8070 static inline int
8071 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8072 {
8073 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8074 }
8075
8076 /* Translate S2 section/page access permissions to protection flags
8077 *
8078 * @env: CPUARMState
8079 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8080 * @xn: XN (execute-never) bit
8081 */
8082 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8083 {
8084 int prot = 0;
8085
8086 if (s2ap & 1) {
8087 prot |= PAGE_READ;
8088 }
8089 if (s2ap & 2) {
8090 prot |= PAGE_WRITE;
8091 }
8092 if (!xn) {
8093 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8094 prot |= PAGE_EXEC;
8095 }
8096 }
8097 return prot;
8098 }
8099
8100 /* Translate section/page access permissions to protection flags
8101 *
8102 * @env: CPUARMState
8103 * @mmu_idx: MMU index indicating required translation regime
8104 * @is_aa64: TRUE if AArch64
8105 * @ap: The 2-bit simple AP (AP[2:1])
8106 * @ns: NS (non-secure) bit
8107 * @xn: XN (execute-never) bit
8108 * @pxn: PXN (privileged execute-never) bit
8109 */
8110 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8111 int ap, int ns, int xn, int pxn)
8112 {
8113 bool is_user = regime_is_user(env, mmu_idx);
8114 int prot_rw, user_rw;
8115 bool have_wxn;
8116 int wxn = 0;
8117
8118 assert(mmu_idx != ARMMMUIdx_S2NS);
8119
8120 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8121 if (is_user) {
8122 prot_rw = user_rw;
8123 } else {
8124 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8125 }
8126
8127 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8128 return prot_rw;
8129 }
8130
8131 /* TODO have_wxn should be replaced with
8132 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8133 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8134 * compatible processors have EL2, which is required for [U]WXN.
8135 */
8136 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8137
8138 if (have_wxn) {
8139 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8140 }
8141
8142 if (is_aa64) {
8143 switch (regime_el(env, mmu_idx)) {
8144 case 1:
8145 if (!is_user) {
8146 xn = pxn || (user_rw & PAGE_WRITE);
8147 }
8148 break;
8149 case 2:
8150 case 3:
8151 break;
8152 }
8153 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8154 switch (regime_el(env, mmu_idx)) {
8155 case 1:
8156 case 3:
8157 if (is_user) {
8158 xn = xn || !(user_rw & PAGE_READ);
8159 } else {
8160 int uwxn = 0;
8161 if (have_wxn) {
8162 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8163 }
8164 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8165 (uwxn && (user_rw & PAGE_WRITE));
8166 }
8167 break;
8168 case 2:
8169 break;
8170 }
8171 } else {
8172 xn = wxn = 0;
8173 }
8174
8175 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8176 return prot_rw;
8177 }
8178 return prot_rw | PAGE_EXEC;
8179 }
8180
8181 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8182 uint32_t *table, uint32_t address)
8183 {
8184 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
8185 TCR *tcr = regime_tcr(env, mmu_idx);
8186
8187 if (address & tcr->mask) {
8188 if (tcr->raw_tcr & TTBCR_PD1) {
8189 /* Translation table walk disabled for TTBR1 */
8190 return false;
8191 }
8192 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
8193 } else {
8194 if (tcr->raw_tcr & TTBCR_PD0) {
8195 /* Translation table walk disabled for TTBR0 */
8196 return false;
8197 }
8198 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
8199 }
8200 *table |= (address >> 18) & 0x3ffc;
8201 return true;
8202 }
8203
8204 /* Translate a S1 pagetable walk through S2 if needed. */
8205 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8206 hwaddr addr, MemTxAttrs txattrs,
8207 uint32_t *fsr,
8208 ARMMMUFaultInfo *fi)
8209 {
8210 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8211 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8212 target_ulong s2size;
8213 hwaddr s2pa;
8214 int s2prot;
8215 int ret;
8216
8217 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
8218 &txattrs, &s2prot, &s2size, fsr, fi, NULL);
8219 if (ret) {
8220 fi->s2addr = addr;
8221 fi->stage2 = true;
8222 fi->s1ptw = true;
8223 return ~0;
8224 }
8225 addr = s2pa;
8226 }
8227 return addr;
8228 }
8229
8230 /* All loads done in the course of a page table walk go through here.
8231 * TODO: rather than ignoring errors from physical memory reads (which
8232 * are external aborts in ARM terminology) we should propagate this
8233 * error out so that we can turn it into a Data Abort if this walk
8234 * was being done for a CPU load/store or an address translation instruction
8235 * (but not if it was for a debug access).
8236 */
8237 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8238 ARMMMUIdx mmu_idx, uint32_t *fsr,
8239 ARMMMUFaultInfo *fi)
8240 {
8241 ARMCPU *cpu = ARM_CPU(cs);
8242 CPUARMState *env = &cpu->env;
8243 MemTxAttrs attrs = {};
8244 AddressSpace *as;
8245
8246 attrs.secure = is_secure;
8247 as = arm_addressspace(cs, attrs);
8248 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
8249 if (fi->s1ptw) {
8250 return 0;
8251 }
8252 if (regime_translation_big_endian(env, mmu_idx)) {
8253 return address_space_ldl_be(as, addr, attrs, NULL);
8254 } else {
8255 return address_space_ldl_le(as, addr, attrs, NULL);
8256 }
8257 }
8258
8259 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8260 ARMMMUIdx mmu_idx, uint32_t *fsr,
8261 ARMMMUFaultInfo *fi)
8262 {
8263 ARMCPU *cpu = ARM_CPU(cs);
8264 CPUARMState *env = &cpu->env;
8265 MemTxAttrs attrs = {};
8266 AddressSpace *as;
8267
8268 attrs.secure = is_secure;
8269 as = arm_addressspace(cs, attrs);
8270 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
8271 if (fi->s1ptw) {
8272 return 0;
8273 }
8274 if (regime_translation_big_endian(env, mmu_idx)) {
8275 return address_space_ldq_be(as, addr, attrs, NULL);
8276 } else {
8277 return address_space_ldq_le(as, addr, attrs, NULL);
8278 }
8279 }
8280
8281 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
8282 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8283 hwaddr *phys_ptr, int *prot,
8284 target_ulong *page_size, uint32_t *fsr,
8285 ARMMMUFaultInfo *fi)
8286 {
8287 CPUState *cs = CPU(arm_env_get_cpu(env));
8288 int code;
8289 uint32_t table;
8290 uint32_t desc;
8291 int type;
8292 int ap;
8293 int domain = 0;
8294 int domain_prot;
8295 hwaddr phys_addr;
8296 uint32_t dacr;
8297
8298 /* Pagetable walk. */
8299 /* Lookup l1 descriptor. */
8300 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8301 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8302 code = 5;
8303 goto do_fault;
8304 }
8305 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8306 mmu_idx, fsr, fi);
8307 type = (desc & 3);
8308 domain = (desc >> 5) & 0x0f;
8309 if (regime_el(env, mmu_idx) == 1) {
8310 dacr = env->cp15.dacr_ns;
8311 } else {
8312 dacr = env->cp15.dacr_s;
8313 }
8314 domain_prot = (dacr >> (domain * 2)) & 3;
8315 if (type == 0) {
8316 /* Section translation fault. */
8317 code = 5;
8318 goto do_fault;
8319 }
8320 if (domain_prot == 0 || domain_prot == 2) {
8321 if (type == 2)
8322 code = 9; /* Section domain fault. */
8323 else
8324 code = 11; /* Page domain fault. */
8325 goto do_fault;
8326 }
8327 if (type == 2) {
8328 /* 1Mb section. */
8329 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8330 ap = (desc >> 10) & 3;
8331 code = 13;
8332 *page_size = 1024 * 1024;
8333 } else {
8334 /* Lookup l2 entry. */
8335 if (type == 1) {
8336 /* Coarse pagetable. */
8337 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8338 } else {
8339 /* Fine pagetable. */
8340 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8341 }
8342 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8343 mmu_idx, fsr, fi);
8344 switch (desc & 3) {
8345 case 0: /* Page translation fault. */
8346 code = 7;
8347 goto do_fault;
8348 case 1: /* 64k page. */
8349 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8350 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
8351 *page_size = 0x10000;
8352 break;
8353 case 2: /* 4k page. */
8354 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8355 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
8356 *page_size = 0x1000;
8357 break;
8358 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
8359 if (type == 1) {
8360 /* ARMv6/XScale extended small page format */
8361 if (arm_feature(env, ARM_FEATURE_XSCALE)
8362 || arm_feature(env, ARM_FEATURE_V6)) {
8363 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8364 *page_size = 0x1000;
8365 } else {
8366 /* UNPREDICTABLE in ARMv5; we choose to take a
8367 * page translation fault.
8368 */
8369 code = 7;
8370 goto do_fault;
8371 }
8372 } else {
8373 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
8374 *page_size = 0x400;
8375 }
8376 ap = (desc >> 4) & 3;
8377 break;
8378 default:
8379 /* Never happens, but compiler isn't smart enough to tell. */
8380 abort();
8381 }
8382 code = 15;
8383 }
8384 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8385 *prot |= *prot ? PAGE_EXEC : 0;
8386 if (!(*prot & (1 << access_type))) {
8387 /* Access permission fault. */
8388 goto do_fault;
8389 }
8390 *phys_ptr = phys_addr;
8391 return false;
8392 do_fault:
8393 *fsr = code | (domain << 4);
8394 return true;
8395 }
8396
8397 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
8398 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8399 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
8400 target_ulong *page_size, uint32_t *fsr,
8401 ARMMMUFaultInfo *fi)
8402 {
8403 CPUState *cs = CPU(arm_env_get_cpu(env));
8404 int code;
8405 uint32_t table;
8406 uint32_t desc;
8407 uint32_t xn;
8408 uint32_t pxn = 0;
8409 int type;
8410 int ap;
8411 int domain = 0;
8412 int domain_prot;
8413 hwaddr phys_addr;
8414 uint32_t dacr;
8415 bool ns;
8416
8417 /* Pagetable walk. */
8418 /* Lookup l1 descriptor. */
8419 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8420 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8421 code = 5;
8422 goto do_fault;
8423 }
8424 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8425 mmu_idx, fsr, fi);
8426 type = (desc & 3);
8427 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8428 /* Section translation fault, or attempt to use the encoding
8429 * which is Reserved on implementations without PXN.
8430 */
8431 code = 5;
8432 goto do_fault;
8433 }
8434 if ((type == 1) || !(desc & (1 << 18))) {
8435 /* Page or Section. */
8436 domain = (desc >> 5) & 0x0f;
8437 }
8438 if (regime_el(env, mmu_idx) == 1) {
8439 dacr = env->cp15.dacr_ns;
8440 } else {
8441 dacr = env->cp15.dacr_s;
8442 }
8443 domain_prot = (dacr >> (domain * 2)) & 3;
8444 if (domain_prot == 0 || domain_prot == 2) {
8445 if (type != 1) {
8446 code = 9; /* Section domain fault. */
8447 } else {
8448 code = 11; /* Page domain fault. */
8449 }
8450 goto do_fault;
8451 }
8452 if (type != 1) {
8453 if (desc & (1 << 18)) {
8454 /* Supersection. */
8455 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
8456 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8457 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
8458 *page_size = 0x1000000;
8459 } else {
8460 /* Section. */
8461 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8462 *page_size = 0x100000;
8463 }
8464 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8465 xn = desc & (1 << 4);
8466 pxn = desc & 1;
8467 code = 13;
8468 ns = extract32(desc, 19, 1);
8469 } else {
8470 if (arm_feature(env, ARM_FEATURE_PXN)) {
8471 pxn = (desc >> 2) & 1;
8472 }
8473 ns = extract32(desc, 3, 1);
8474 /* Lookup l2 entry. */
8475 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8476 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8477 mmu_idx, fsr, fi);
8478 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8479 switch (desc & 3) {
8480 case 0: /* Page translation fault. */
8481 code = 7;
8482 goto do_fault;
8483 case 1: /* 64k page. */
8484 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8485 xn = desc & (1 << 15);
8486 *page_size = 0x10000;
8487 break;
8488 case 2: case 3: /* 4k page. */
8489 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8490 xn = desc & 1;
8491 *page_size = 0x1000;
8492 break;
8493 default:
8494 /* Never happens, but compiler isn't smart enough to tell. */
8495 abort();
8496 }
8497 code = 15;
8498 }
8499 if (domain_prot == 3) {
8500 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8501 } else {
8502 if (pxn && !regime_is_user(env, mmu_idx)) {
8503 xn = 1;
8504 }
8505 if (xn && access_type == MMU_INST_FETCH)
8506 goto do_fault;
8507
8508 if (arm_feature(env, ARM_FEATURE_V6K) &&
8509 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
8510 /* The simplified model uses AP[0] as an access control bit. */
8511 if ((ap & 1) == 0) {
8512 /* Access flag fault. */
8513 code = (code == 15) ? 6 : 3;
8514 goto do_fault;
8515 }
8516 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
8517 } else {
8518 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8519 }
8520 if (*prot && !xn) {
8521 *prot |= PAGE_EXEC;
8522 }
8523 if (!(*prot & (1 << access_type))) {
8524 /* Access permission fault. */
8525 goto do_fault;
8526 }
8527 }
8528 if (ns) {
8529 /* The NS bit will (as required by the architecture) have no effect if
8530 * the CPU doesn't support TZ or this is a non-secure translation
8531 * regime, because the attribute will already be non-secure.
8532 */
8533 attrs->secure = false;
8534 }
8535 *phys_ptr = phys_addr;
8536 return false;
8537 do_fault:
8538 *fsr = code | (domain << 4);
8539 return true;
8540 }
8541
8542 /* Fault type for long-descriptor MMU fault reporting; this corresponds
8543 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
8544 */
8545 typedef enum {
8546 translation_fault = 1,
8547 access_fault = 2,
8548 permission_fault = 3,
8549 } MMUFaultType;
8550
8551 /*
8552 * check_s2_mmu_setup
8553 * @cpu: ARMCPU
8554 * @is_aa64: True if the translation regime is in AArch64 state
8555 * @startlevel: Suggested starting level
8556 * @inputsize: Bitsize of IPAs
8557 * @stride: Page-table stride (See the ARM ARM)
8558 *
8559 * Returns true if the suggested S2 translation parameters are OK and
8560 * false otherwise.
8561 */
8562 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
8563 int inputsize, int stride)
8564 {
8565 const int grainsize = stride + 3;
8566 int startsizecheck;
8567
8568 /* Negative levels are never allowed. */
8569 if (level < 0) {
8570 return false;
8571 }
8572
8573 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
8574 if (startsizecheck < 1 || startsizecheck > stride + 4) {
8575 return false;
8576 }
8577
8578 if (is_aa64) {
8579 CPUARMState *env = &cpu->env;
8580 unsigned int pamax = arm_pamax(cpu);
8581
8582 switch (stride) {
8583 case 13: /* 64KB Pages. */
8584 if (level == 0 || (level == 1 && pamax <= 42)) {
8585 return false;
8586 }
8587 break;
8588 case 11: /* 16KB Pages. */
8589 if (level == 0 || (level == 1 && pamax <= 40)) {
8590 return false;
8591 }
8592 break;
8593 case 9: /* 4KB Pages. */
8594 if (level == 0 && pamax <= 42) {
8595 return false;
8596 }
8597 break;
8598 default:
8599 g_assert_not_reached();
8600 }
8601
8602 /* Inputsize checks. */
8603 if (inputsize > pamax &&
8604 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
8605 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
8606 return false;
8607 }
8608 } else {
8609 /* AArch32 only supports 4KB pages. Assert on that. */
8610 assert(stride == 9);
8611
8612 if (level == 0) {
8613 return false;
8614 }
8615 }
8616 return true;
8617 }
8618
8619 /* Translate from the 4-bit stage 2 representation of
8620 * memory attributes (without cache-allocation hints) to
8621 * the 8-bit representation of the stage 1 MAIR registers
8622 * (which includes allocation hints).
8623 *
8624 * ref: shared/translation/attrs/S2AttrDecode()
8625 * .../S2ConvertAttrsHints()
8626 */
8627 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
8628 {
8629 uint8_t hiattr = extract32(s2attrs, 2, 2);
8630 uint8_t loattr = extract32(s2attrs, 0, 2);
8631 uint8_t hihint = 0, lohint = 0;
8632
8633 if (hiattr != 0) { /* normal memory */
8634 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
8635 hiattr = loattr = 1; /* non-cacheable */
8636 } else {
8637 if (hiattr != 1) { /* Write-through or write-back */
8638 hihint = 3; /* RW allocate */
8639 }
8640 if (loattr != 1) { /* Write-through or write-back */
8641 lohint = 3; /* RW allocate */
8642 }
8643 }
8644 }
8645
8646 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
8647 }
8648
8649 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
8650 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8651 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
8652 target_ulong *page_size_ptr, uint32_t *fsr,
8653 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
8654 {
8655 ARMCPU *cpu = arm_env_get_cpu(env);
8656 CPUState *cs = CPU(cpu);
8657 /* Read an LPAE long-descriptor translation table. */
8658 MMUFaultType fault_type = translation_fault;
8659 uint32_t level;
8660 uint32_t epd = 0;
8661 int32_t t0sz, t1sz;
8662 uint32_t tg;
8663 uint64_t ttbr;
8664 int ttbr_select;
8665 hwaddr descaddr, indexmask, indexmask_grainsize;
8666 uint32_t tableattrs;
8667 target_ulong page_size;
8668 uint32_t attrs;
8669 int32_t stride = 9;
8670 int32_t addrsize;
8671 int inputsize;
8672 int32_t tbi = 0;
8673 TCR *tcr = regime_tcr(env, mmu_idx);
8674 int ap, ns, xn, pxn;
8675 uint32_t el = regime_el(env, mmu_idx);
8676 bool ttbr1_valid = true;
8677 uint64_t descaddrmask;
8678 bool aarch64 = arm_el_is_aa64(env, el);
8679
8680 /* TODO:
8681 * This code does not handle the different format TCR for VTCR_EL2.
8682 * This code also does not support shareability levels.
8683 * Attribute and permission bit handling should also be checked when adding
8684 * support for those page table walks.
8685 */
8686 if (aarch64) {
8687 level = 0;
8688 addrsize = 64;
8689 if (el > 1) {
8690 if (mmu_idx != ARMMMUIdx_S2NS) {
8691 tbi = extract64(tcr->raw_tcr, 20, 1);
8692 }
8693 } else {
8694 if (extract64(address, 55, 1)) {
8695 tbi = extract64(tcr->raw_tcr, 38, 1);
8696 } else {
8697 tbi = extract64(tcr->raw_tcr, 37, 1);
8698 }
8699 }
8700 tbi *= 8;
8701
8702 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
8703 * invalid.
8704 */
8705 if (el > 1) {
8706 ttbr1_valid = false;
8707 }
8708 } else {
8709 level = 1;
8710 addrsize = 32;
8711 /* There is no TTBR1 for EL2 */
8712 if (el == 2) {
8713 ttbr1_valid = false;
8714 }
8715 }
8716
8717 /* Determine whether this address is in the region controlled by
8718 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
8719 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
8720 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
8721 */
8722 if (aarch64) {
8723 /* AArch64 translation. */
8724 t0sz = extract32(tcr->raw_tcr, 0, 6);
8725 t0sz = MIN(t0sz, 39);
8726 t0sz = MAX(t0sz, 16);
8727 } else if (mmu_idx != ARMMMUIdx_S2NS) {
8728 /* AArch32 stage 1 translation. */
8729 t0sz = extract32(tcr->raw_tcr, 0, 3);
8730 } else {
8731 /* AArch32 stage 2 translation. */
8732 bool sext = extract32(tcr->raw_tcr, 4, 1);
8733 bool sign = extract32(tcr->raw_tcr, 3, 1);
8734 /* Address size is 40-bit for a stage 2 translation,
8735 * and t0sz can be negative (from -8 to 7),
8736 * so we need to adjust it to use the TTBR selecting logic below.
8737 */
8738 addrsize = 40;
8739 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
8740
8741 /* If the sign-extend bit is not the same as t0sz[3], the result
8742 * is unpredictable. Flag this as a guest error. */
8743 if (sign != sext) {
8744 qemu_log_mask(LOG_GUEST_ERROR,
8745 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
8746 }
8747 }
8748 t1sz = extract32(tcr->raw_tcr, 16, 6);
8749 if (aarch64) {
8750 t1sz = MIN(t1sz, 39);
8751 t1sz = MAX(t1sz, 16);
8752 }
8753 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
8754 /* there is a ttbr0 region and we are in it (high bits all zero) */
8755 ttbr_select = 0;
8756 } else if (ttbr1_valid && t1sz &&
8757 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
8758 /* there is a ttbr1 region and we are in it (high bits all one) */
8759 ttbr_select = 1;
8760 } else if (!t0sz) {
8761 /* ttbr0 region is "everything not in the ttbr1 region" */
8762 ttbr_select = 0;
8763 } else if (!t1sz && ttbr1_valid) {
8764 /* ttbr1 region is "everything not in the ttbr0 region" */
8765 ttbr_select = 1;
8766 } else {
8767 /* in the gap between the two regions, this is a Translation fault */
8768 fault_type = translation_fault;
8769 goto do_fault;
8770 }
8771
8772 /* Note that QEMU ignores shareability and cacheability attributes,
8773 * so we don't need to do anything with the SH, ORGN, IRGN fields
8774 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
8775 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
8776 * implement any ASID-like capability so we can ignore it (instead
8777 * we will always flush the TLB any time the ASID is changed).
8778 */
8779 if (ttbr_select == 0) {
8780 ttbr = regime_ttbr(env, mmu_idx, 0);
8781 if (el < 2) {
8782 epd = extract32(tcr->raw_tcr, 7, 1);
8783 }
8784 inputsize = addrsize - t0sz;
8785
8786 tg = extract32(tcr->raw_tcr, 14, 2);
8787 if (tg == 1) { /* 64KB pages */
8788 stride = 13;
8789 }
8790 if (tg == 2) { /* 16KB pages */
8791 stride = 11;
8792 }
8793 } else {
8794 /* We should only be here if TTBR1 is valid */
8795 assert(ttbr1_valid);
8796
8797 ttbr = regime_ttbr(env, mmu_idx, 1);
8798 epd = extract32(tcr->raw_tcr, 23, 1);
8799 inputsize = addrsize - t1sz;
8800
8801 tg = extract32(tcr->raw_tcr, 30, 2);
8802 if (tg == 3) { /* 64KB pages */
8803 stride = 13;
8804 }
8805 if (tg == 1) { /* 16KB pages */
8806 stride = 11;
8807 }
8808 }
8809
8810 /* Here we should have set up all the parameters for the translation:
8811 * inputsize, ttbr, epd, stride, tbi
8812 */
8813
8814 if (epd) {
8815 /* Translation table walk disabled => Translation fault on TLB miss
8816 * Note: This is always 0 on 64-bit EL2 and EL3.
8817 */
8818 goto do_fault;
8819 }
8820
8821 if (mmu_idx != ARMMMUIdx_S2NS) {
8822 /* The starting level depends on the virtual address size (which can
8823 * be up to 48 bits) and the translation granule size. It indicates
8824 * the number of strides (stride bits at a time) needed to
8825 * consume the bits of the input address. In the pseudocode this is:
8826 * level = 4 - RoundUp((inputsize - grainsize) / stride)
8827 * where their 'inputsize' is our 'inputsize', 'grainsize' is
8828 * our 'stride + 3' and 'stride' is our 'stride'.
8829 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
8830 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
8831 * = 4 - (inputsize - 4) / stride;
8832 */
8833 level = 4 - (inputsize - 4) / stride;
8834 } else {
8835 /* For stage 2 translations the starting level is specified by the
8836 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
8837 */
8838 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
8839 uint32_t startlevel;
8840 bool ok;
8841
8842 if (!aarch64 || stride == 9) {
8843 /* AArch32 or 4KB pages */
8844 startlevel = 2 - sl0;
8845 } else {
8846 /* 16KB or 64KB pages */
8847 startlevel = 3 - sl0;
8848 }
8849
8850 /* Check that the starting level is valid. */
8851 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
8852 inputsize, stride);
8853 if (!ok) {
8854 fault_type = translation_fault;
8855 goto do_fault;
8856 }
8857 level = startlevel;
8858 }
8859
8860 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
8861 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
8862
8863 /* Now we can extract the actual base address from the TTBR */
8864 descaddr = extract64(ttbr, 0, 48);
8865 descaddr &= ~indexmask;
8866
8867 /* The address field in the descriptor goes up to bit 39 for ARMv7
8868 * but up to bit 47 for ARMv8, but we use the descaddrmask
8869 * up to bit 39 for AArch32, because we don't need other bits in that case
8870 * to construct next descriptor address (anyway they should be all zeroes).
8871 */
8872 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
8873 ~indexmask_grainsize;
8874
8875 /* Secure accesses start with the page table in secure memory and
8876 * can be downgraded to non-secure at any step. Non-secure accesses
8877 * remain non-secure. We implement this by just ORing in the NSTable/NS
8878 * bits at each step.
8879 */
8880 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
8881 for (;;) {
8882 uint64_t descriptor;
8883 bool nstable;
8884
8885 descaddr |= (address >> (stride * (4 - level))) & indexmask;
8886 descaddr &= ~7ULL;
8887 nstable = extract32(tableattrs, 4, 1);
8888 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi);
8889 if (fi->s1ptw) {
8890 goto do_fault;
8891 }
8892
8893 if (!(descriptor & 1) ||
8894 (!(descriptor & 2) && (level == 3))) {
8895 /* Invalid, or the Reserved level 3 encoding */
8896 goto do_fault;
8897 }
8898 descaddr = descriptor & descaddrmask;
8899
8900 if ((descriptor & 2) && (level < 3)) {
8901 /* Table entry. The top five bits are attributes which may
8902 * propagate down through lower levels of the table (and
8903 * which are all arranged so that 0 means "no effect", so
8904 * we can gather them up by ORing in the bits at each level).
8905 */
8906 tableattrs |= extract64(descriptor, 59, 5);
8907 level++;
8908 indexmask = indexmask_grainsize;
8909 continue;
8910 }
8911 /* Block entry at level 1 or 2, or page entry at level 3.
8912 * These are basically the same thing, although the number
8913 * of bits we pull in from the vaddr varies.
8914 */
8915 page_size = (1ULL << ((stride * (4 - level)) + 3));
8916 descaddr |= (address & (page_size - 1));
8917 /* Extract attributes from the descriptor */
8918 attrs = extract64(descriptor, 2, 10)
8919 | (extract64(descriptor, 52, 12) << 10);
8920
8921 if (mmu_idx == ARMMMUIdx_S2NS) {
8922 /* Stage 2 table descriptors do not include any attribute fields */
8923 break;
8924 }
8925 /* Merge in attributes from table descriptors */
8926 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
8927 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
8928 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
8929 * means "force PL1 access only", which means forcing AP[1] to 0.
8930 */
8931 if (extract32(tableattrs, 2, 1)) {
8932 attrs &= ~(1 << 4);
8933 }
8934 attrs |= nstable << 3; /* NS */
8935 break;
8936 }
8937 /* Here descaddr is the final physical address, and attributes
8938 * are all in attrs.
8939 */
8940 fault_type = access_fault;
8941 if ((attrs & (1 << 8)) == 0) {
8942 /* Access flag */
8943 goto do_fault;
8944 }
8945
8946 ap = extract32(attrs, 4, 2);
8947 xn = extract32(attrs, 12, 1);
8948
8949 if (mmu_idx == ARMMMUIdx_S2NS) {
8950 ns = true;
8951 *prot = get_S2prot(env, ap, xn);
8952 } else {
8953 ns = extract32(attrs, 3, 1);
8954 pxn = extract32(attrs, 11, 1);
8955 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
8956 }
8957
8958 fault_type = permission_fault;
8959 if (!(*prot & (1 << access_type))) {
8960 goto do_fault;
8961 }
8962
8963 if (ns) {
8964 /* The NS bit will (as required by the architecture) have no effect if
8965 * the CPU doesn't support TZ or this is a non-secure translation
8966 * regime, because the attribute will already be non-secure.
8967 */
8968 txattrs->secure = false;
8969 }
8970
8971 if (cacheattrs != NULL) {
8972 if (mmu_idx == ARMMMUIdx_S2NS) {
8973 cacheattrs->attrs = convert_stage2_attrs(env,
8974 extract32(attrs, 0, 4));
8975 } else {
8976 /* Index into MAIR registers for cache attributes */
8977 uint8_t attrindx = extract32(attrs, 0, 3);
8978 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
8979 assert(attrindx <= 7);
8980 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
8981 }
8982 cacheattrs->shareability = extract32(attrs, 6, 2);
8983 }
8984
8985 *phys_ptr = descaddr;
8986 *page_size_ptr = page_size;
8987 return false;
8988
8989 do_fault:
8990 /* Long-descriptor format IFSR/DFSR value */
8991 *fsr = (1 << 9) | (fault_type << 2) | level;
8992 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
8993 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
8994 return true;
8995 }
8996
8997 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
8998 ARMMMUIdx mmu_idx,
8999 int32_t address, int *prot)
9000 {
9001 if (!arm_feature(env, ARM_FEATURE_M)) {
9002 *prot = PAGE_READ | PAGE_WRITE;
9003 switch (address) {
9004 case 0xF0000000 ... 0xFFFFFFFF:
9005 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9006 /* hivecs execing is ok */
9007 *prot |= PAGE_EXEC;
9008 }
9009 break;
9010 case 0x00000000 ... 0x7FFFFFFF:
9011 *prot |= PAGE_EXEC;
9012 break;
9013 }
9014 } else {
9015 /* Default system address map for M profile cores.
9016 * The architecture specifies which regions are execute-never;
9017 * at the MPU level no other checks are defined.
9018 */
9019 switch (address) {
9020 case 0x00000000 ... 0x1fffffff: /* ROM */
9021 case 0x20000000 ... 0x3fffffff: /* SRAM */
9022 case 0x60000000 ... 0x7fffffff: /* RAM */
9023 case 0x80000000 ... 0x9fffffff: /* RAM */
9024 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9025 break;
9026 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9027 case 0xa0000000 ... 0xbfffffff: /* Device */
9028 case 0xc0000000 ... 0xdfffffff: /* Device */
9029 case 0xe0000000 ... 0xffffffff: /* System */
9030 *prot = PAGE_READ | PAGE_WRITE;
9031 break;
9032 default:
9033 g_assert_not_reached();
9034 }
9035 }
9036 }
9037
9038 static bool pmsav7_use_background_region(ARMCPU *cpu,
9039 ARMMMUIdx mmu_idx, bool is_user)
9040 {
9041 /* Return true if we should use the default memory map as a
9042 * "background" region if there are no hits against any MPU regions.
9043 */
9044 CPUARMState *env = &cpu->env;
9045
9046 if (is_user) {
9047 return false;
9048 }
9049
9050 if (arm_feature(env, ARM_FEATURE_M)) {
9051 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9052 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
9053 } else {
9054 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9055 }
9056 }
9057
9058 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9059 {
9060 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9061 return arm_feature(env, ARM_FEATURE_M) &&
9062 extract32(address, 20, 12) == 0xe00;
9063 }
9064
9065 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9066 {
9067 /* True if address is in the M profile system region
9068 * 0xe0000000 - 0xffffffff
9069 */
9070 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9071 }
9072
9073 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
9074 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9075 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9076 {
9077 ARMCPU *cpu = arm_env_get_cpu(env);
9078 int n;
9079 bool is_user = regime_is_user(env, mmu_idx);
9080
9081 *phys_ptr = address;
9082 *prot = 0;
9083
9084 if (regime_translation_disabled(env, mmu_idx) ||
9085 m_is_ppb_region(env, address)) {
9086 /* MPU disabled or M profile PPB access: use default memory map.
9087 * The other case which uses the default memory map in the
9088 * v7M ARM ARM pseudocode is exception vector reads from the vector
9089 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9090 * which always does a direct read using address_space_ldl(), rather
9091 * than going via this function, so we don't need to check that here.
9092 */
9093 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9094 } else { /* MPU enabled */
9095 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9096 /* region search */
9097 uint32_t base = env->pmsav7.drbar[n];
9098 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9099 uint32_t rmask;
9100 bool srdis = false;
9101
9102 if (!(env->pmsav7.drsr[n] & 0x1)) {
9103 continue;
9104 }
9105
9106 if (!rsize) {
9107 qemu_log_mask(LOG_GUEST_ERROR,
9108 "DRSR[%d]: Rsize field cannot be 0\n", n);
9109 continue;
9110 }
9111 rsize++;
9112 rmask = (1ull << rsize) - 1;
9113
9114 if (base & rmask) {
9115 qemu_log_mask(LOG_GUEST_ERROR,
9116 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9117 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9118 n, base, rmask);
9119 continue;
9120 }
9121
9122 if (address < base || address > base + rmask) {
9123 continue;
9124 }
9125
9126 /* Region matched */
9127
9128 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9129 int i, snd;
9130 uint32_t srdis_mask;
9131
9132 rsize -= 3; /* sub region size (power of 2) */
9133 snd = ((address - base) >> rsize) & 0x7;
9134 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9135
9136 srdis_mask = srdis ? 0x3 : 0x0;
9137 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9138 /* This will check in groups of 2, 4 and then 8, whether
9139 * the subregion bits are consistent. rsize is incremented
9140 * back up to give the region size, considering consistent
9141 * adjacent subregions as one region. Stop testing if rsize
9142 * is already big enough for an entire QEMU page.
9143 */
9144 int snd_rounded = snd & ~(i - 1);
9145 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9146 snd_rounded + 8, i);
9147 if (srdis_mask ^ srdis_multi) {
9148 break;
9149 }
9150 srdis_mask = (srdis_mask << i) | srdis_mask;
9151 rsize++;
9152 }
9153 }
9154 if (rsize < TARGET_PAGE_BITS) {
9155 qemu_log_mask(LOG_UNIMP,
9156 "DRSR[%d]: No support for MPU (sub)region "
9157 "alignment of %" PRIu32 " bits. Minimum is %d\n",
9158 n, rsize, TARGET_PAGE_BITS);
9159 continue;
9160 }
9161 if (srdis) {
9162 continue;
9163 }
9164 break;
9165 }
9166
9167 if (n == -1) { /* no hits */
9168 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9169 /* background fault */
9170 *fsr = 0;
9171 return true;
9172 }
9173 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9174 } else { /* a MPU hit! */
9175 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
9176 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9177
9178 if (m_is_system_region(env, address)) {
9179 /* System space is always execute never */
9180 xn = 1;
9181 }
9182
9183 if (is_user) { /* User mode AP bit decoding */
9184 switch (ap) {
9185 case 0:
9186 case 1:
9187 case 5:
9188 break; /* no access */
9189 case 3:
9190 *prot |= PAGE_WRITE;
9191 /* fall through */
9192 case 2:
9193 case 6:
9194 *prot |= PAGE_READ | PAGE_EXEC;
9195 break;
9196 default:
9197 qemu_log_mask(LOG_GUEST_ERROR,
9198 "DRACR[%d]: Bad value for AP bits: 0x%"
9199 PRIx32 "\n", n, ap);
9200 }
9201 } else { /* Priv. mode AP bits decoding */
9202 switch (ap) {
9203 case 0:
9204 break; /* no access */
9205 case 1:
9206 case 2:
9207 case 3:
9208 *prot |= PAGE_WRITE;
9209 /* fall through */
9210 case 5:
9211 case 6:
9212 *prot |= PAGE_READ | PAGE_EXEC;
9213 break;
9214 default:
9215 qemu_log_mask(LOG_GUEST_ERROR,
9216 "DRACR[%d]: Bad value for AP bits: 0x%"
9217 PRIx32 "\n", n, ap);
9218 }
9219 }
9220
9221 /* execute never */
9222 if (xn) {
9223 *prot &= ~PAGE_EXEC;
9224 }
9225 }
9226 }
9227
9228 *fsr = 0x00d; /* Permission fault */
9229 return !(*prot & (1 << access_type));
9230 }
9231
9232 static bool v8m_is_sau_exempt(CPUARMState *env,
9233 uint32_t address, MMUAccessType access_type)
9234 {
9235 /* The architecture specifies that certain address ranges are
9236 * exempt from v8M SAU/IDAU checks.
9237 */
9238 return
9239 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9240 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9241 (address >= 0xe000e000 && address <= 0xe000efff) ||
9242 (address >= 0xe002e000 && address <= 0xe002efff) ||
9243 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9244 (address >= 0xe00ff000 && address <= 0xe00fffff);
9245 }
9246
9247 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9248 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9249 V8M_SAttributes *sattrs)
9250 {
9251 /* Look up the security attributes for this address. Compare the
9252 * pseudocode SecurityCheck() function.
9253 * We assume the caller has zero-initialized *sattrs.
9254 */
9255 ARMCPU *cpu = arm_env_get_cpu(env);
9256 int r;
9257
9258 /* TODO: implement IDAU */
9259
9260 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9261 /* 0xf0000000..0xffffffff is always S for insn fetches */
9262 return;
9263 }
9264
9265 if (v8m_is_sau_exempt(env, address, access_type)) {
9266 sattrs->ns = !regime_is_secure(env, mmu_idx);
9267 return;
9268 }
9269
9270 switch (env->sau.ctrl & 3) {
9271 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9272 break;
9273 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9274 sattrs->ns = true;
9275 break;
9276 default: /* SAU.ENABLE == 1 */
9277 for (r = 0; r < cpu->sau_sregion; r++) {
9278 if (env->sau.rlar[r] & 1) {
9279 uint32_t base = env->sau.rbar[r] & ~0x1f;
9280 uint32_t limit = env->sau.rlar[r] | 0x1f;
9281
9282 if (base <= address && limit >= address) {
9283 if (sattrs->srvalid) {
9284 /* If we hit in more than one region then we must report
9285 * as Secure, not NS-Callable, with no valid region
9286 * number info.
9287 */
9288 sattrs->ns = false;
9289 sattrs->nsc = false;
9290 sattrs->sregion = 0;
9291 sattrs->srvalid = false;
9292 break;
9293 } else {
9294 if (env->sau.rlar[r] & 2) {
9295 sattrs->nsc = true;
9296 } else {
9297 sattrs->ns = true;
9298 }
9299 sattrs->srvalid = true;
9300 sattrs->sregion = r;
9301 }
9302 }
9303 }
9304 }
9305
9306 /* TODO when we support the IDAU then it may override the result here */
9307 break;
9308 }
9309 }
9310
9311 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9312 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9313 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9314 int *prot, uint32_t *fsr)
9315 {
9316 ARMCPU *cpu = arm_env_get_cpu(env);
9317 bool is_user = regime_is_user(env, mmu_idx);
9318 uint32_t secure = regime_is_secure(env, mmu_idx);
9319 int n;
9320 int matchregion = -1;
9321 bool hit = false;
9322 V8M_SAttributes sattrs = {};
9323
9324 *phys_ptr = address;
9325 *prot = 0;
9326
9327 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9328 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9329 if (access_type == MMU_INST_FETCH) {
9330 /* Instruction fetches always use the MMU bank and the
9331 * transaction attribute determined by the fetch address,
9332 * regardless of CPU state. This is painful for QEMU
9333 * to handle, because it would mean we need to encode
9334 * into the mmu_idx not just the (user, negpri) information
9335 * for the current security state but also that for the
9336 * other security state, which would balloon the number
9337 * of mmu_idx values needed alarmingly.
9338 * Fortunately we can avoid this because it's not actually
9339 * possible to arbitrarily execute code from memory with
9340 * the wrong security attribute: it will always generate
9341 * an exception of some kind or another, apart from the
9342 * special case of an NS CPU executing an SG instruction
9343 * in S&NSC memory. So we always just fail the translation
9344 * here and sort things out in the exception handler
9345 * (including possibly emulating an SG instruction).
9346 */
9347 if (sattrs.ns != !secure) {
9348 *fsr = sattrs.nsc ? M_FAKE_FSR_NSC_EXEC : M_FAKE_FSR_SFAULT;
9349 return true;
9350 }
9351 } else {
9352 /* For data accesses we always use the MMU bank indicated
9353 * by the current CPU state, but the security attributes
9354 * might downgrade a secure access to nonsecure.
9355 */
9356 if (sattrs.ns) {
9357 txattrs->secure = false;
9358 } else if (!secure) {
9359 /* NS access to S memory must fault.
9360 * Architecturally we should first check whether the
9361 * MPU information for this address indicates that we
9362 * are doing an unaligned access to Device memory, which
9363 * should generate a UsageFault instead. QEMU does not
9364 * currently check for that kind of unaligned access though.
9365 * If we added it we would need to do so as a special case
9366 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9367 */
9368 *fsr = M_FAKE_FSR_SFAULT;
9369 return true;
9370 }
9371 }
9372 }
9373
9374 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9375 * was an exception vector read from the vector table (which is always
9376 * done using the default system address map), because those accesses
9377 * are done in arm_v7m_load_vector(), which always does a direct
9378 * read using address_space_ldl(), rather than going via this function.
9379 */
9380 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9381 hit = true;
9382 } else if (m_is_ppb_region(env, address)) {
9383 hit = true;
9384 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9385 hit = true;
9386 } else {
9387 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9388 /* region search */
9389 /* Note that the base address is bits [31:5] from the register
9390 * with bits [4:0] all zeroes, but the limit address is bits
9391 * [31:5] from the register with bits [4:0] all ones.
9392 */
9393 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9394 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
9395
9396 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
9397 /* Region disabled */
9398 continue;
9399 }
9400
9401 if (address < base || address > limit) {
9402 continue;
9403 }
9404
9405 if (hit) {
9406 /* Multiple regions match -- always a failure (unlike
9407 * PMSAv7 where highest-numbered-region wins)
9408 */
9409 *fsr = 0x00d; /* permission fault */
9410 return true;
9411 }
9412
9413 matchregion = n;
9414 hit = true;
9415
9416 if (base & ~TARGET_PAGE_MASK) {
9417 qemu_log_mask(LOG_UNIMP,
9418 "MPU_RBAR[%d]: No support for MPU region base"
9419 "address of 0x%" PRIx32 ". Minimum alignment is "
9420 "%d\n",
9421 n, base, TARGET_PAGE_BITS);
9422 continue;
9423 }
9424 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9425 qemu_log_mask(LOG_UNIMP,
9426 "MPU_RBAR[%d]: No support for MPU region limit"
9427 "address of 0x%" PRIx32 ". Minimum alignment is "
9428 "%d\n",
9429 n, limit, TARGET_PAGE_BITS);
9430 continue;
9431 }
9432 }
9433 }
9434
9435 if (!hit) {
9436 /* background fault */
9437 *fsr = 0;
9438 return true;
9439 }
9440
9441 if (matchregion == -1) {
9442 /* hit using the background region */
9443 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9444 } else {
9445 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9446 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
9447
9448 if (m_is_system_region(env, address)) {
9449 /* System space is always execute never */
9450 xn = 1;
9451 }
9452
9453 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9454 if (*prot && !xn) {
9455 *prot |= PAGE_EXEC;
9456 }
9457 /* We don't need to look the attribute up in the MAIR0/MAIR1
9458 * registers because that only tells us about cacheability.
9459 */
9460 }
9461
9462 *fsr = 0x00d; /* Permission fault */
9463 return !(*prot & (1 << access_type));
9464 }
9465
9466 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
9467 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9468 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9469 {
9470 int n;
9471 uint32_t mask;
9472 uint32_t base;
9473 bool is_user = regime_is_user(env, mmu_idx);
9474
9475 if (regime_translation_disabled(env, mmu_idx)) {
9476 /* MPU disabled. */
9477 *phys_ptr = address;
9478 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9479 return false;
9480 }
9481
9482 *phys_ptr = address;
9483 for (n = 7; n >= 0; n--) {
9484 base = env->cp15.c6_region[n];
9485 if ((base & 1) == 0) {
9486 continue;
9487 }
9488 mask = 1 << ((base >> 1) & 0x1f);
9489 /* Keep this shift separate from the above to avoid an
9490 (undefined) << 32. */
9491 mask = (mask << 1) - 1;
9492 if (((base ^ address) & ~mask) == 0) {
9493 break;
9494 }
9495 }
9496 if (n < 0) {
9497 *fsr = 2;
9498 return true;
9499 }
9500
9501 if (access_type == MMU_INST_FETCH) {
9502 mask = env->cp15.pmsav5_insn_ap;
9503 } else {
9504 mask = env->cp15.pmsav5_data_ap;
9505 }
9506 mask = (mask >> (n * 4)) & 0xf;
9507 switch (mask) {
9508 case 0:
9509 *fsr = 1;
9510 return true;
9511 case 1:
9512 if (is_user) {
9513 *fsr = 1;
9514 return true;
9515 }
9516 *prot = PAGE_READ | PAGE_WRITE;
9517 break;
9518 case 2:
9519 *prot = PAGE_READ;
9520 if (!is_user) {
9521 *prot |= PAGE_WRITE;
9522 }
9523 break;
9524 case 3:
9525 *prot = PAGE_READ | PAGE_WRITE;
9526 break;
9527 case 5:
9528 if (is_user) {
9529 *fsr = 1;
9530 return true;
9531 }
9532 *prot = PAGE_READ;
9533 break;
9534 case 6:
9535 *prot = PAGE_READ;
9536 break;
9537 default:
9538 /* Bad permission. */
9539 *fsr = 1;
9540 return true;
9541 }
9542 *prot |= PAGE_EXEC;
9543 return false;
9544 }
9545
9546 /* Combine either inner or outer cacheability attributes for normal
9547 * memory, according to table D4-42 and pseudocode procedure
9548 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
9549 *
9550 * NB: only stage 1 includes allocation hints (RW bits), leading to
9551 * some asymmetry.
9552 */
9553 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
9554 {
9555 if (s1 == 4 || s2 == 4) {
9556 /* non-cacheable has precedence */
9557 return 4;
9558 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
9559 /* stage 1 write-through takes precedence */
9560 return s1;
9561 } else if (extract32(s2, 2, 2) == 2) {
9562 /* stage 2 write-through takes precedence, but the allocation hint
9563 * is still taken from stage 1
9564 */
9565 return (2 << 2) | extract32(s1, 0, 2);
9566 } else { /* write-back */
9567 return s1;
9568 }
9569 }
9570
9571 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
9572 * and CombineS1S2Desc()
9573 *
9574 * @s1: Attributes from stage 1 walk
9575 * @s2: Attributes from stage 2 walk
9576 */
9577 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
9578 {
9579 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
9580 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
9581 ARMCacheAttrs ret;
9582
9583 /* Combine shareability attributes (table D4-43) */
9584 if (s1.shareability == 2 || s2.shareability == 2) {
9585 /* if either are outer-shareable, the result is outer-shareable */
9586 ret.shareability = 2;
9587 } else if (s1.shareability == 3 || s2.shareability == 3) {
9588 /* if either are inner-shareable, the result is inner-shareable */
9589 ret.shareability = 3;
9590 } else {
9591 /* both non-shareable */
9592 ret.shareability = 0;
9593 }
9594
9595 /* Combine memory type and cacheability attributes */
9596 if (s1hi == 0 || s2hi == 0) {
9597 /* Device has precedence over normal */
9598 if (s1lo == 0 || s2lo == 0) {
9599 /* nGnRnE has precedence over anything */
9600 ret.attrs = 0;
9601 } else if (s1lo == 4 || s2lo == 4) {
9602 /* non-Reordering has precedence over Reordering */
9603 ret.attrs = 4; /* nGnRE */
9604 } else if (s1lo == 8 || s2lo == 8) {
9605 /* non-Gathering has precedence over Gathering */
9606 ret.attrs = 8; /* nGRE */
9607 } else {
9608 ret.attrs = 0xc; /* GRE */
9609 }
9610
9611 /* Any location for which the resultant memory type is any
9612 * type of Device memory is always treated as Outer Shareable.
9613 */
9614 ret.shareability = 2;
9615 } else { /* Normal memory */
9616 /* Outer/inner cacheability combine independently */
9617 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
9618 | combine_cacheattr_nibble(s1lo, s2lo);
9619
9620 if (ret.attrs == 0x44) {
9621 /* Any location for which the resultant memory type is Normal
9622 * Inner Non-cacheable, Outer Non-cacheable is always treated
9623 * as Outer Shareable.
9624 */
9625 ret.shareability = 2;
9626 }
9627 }
9628
9629 return ret;
9630 }
9631
9632
9633 /* get_phys_addr - get the physical address for this virtual address
9634 *
9635 * Find the physical address corresponding to the given virtual address,
9636 * by doing a translation table walk on MMU based systems or using the
9637 * MPU state on MPU based systems.
9638 *
9639 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
9640 * prot and page_size may not be filled in, and the populated fsr value provides
9641 * information on why the translation aborted, in the format of a
9642 * DFSR/IFSR fault register, with the following caveats:
9643 * * we honour the short vs long DFSR format differences.
9644 * * the WnR bit is never set (the caller must do this).
9645 * * for PSMAv5 based systems we don't bother to return a full FSR format
9646 * value.
9647 *
9648 * @env: CPUARMState
9649 * @address: virtual address to get physical address for
9650 * @access_type: 0 for read, 1 for write, 2 for execute
9651 * @mmu_idx: MMU index indicating required translation regime
9652 * @phys_ptr: set to the physical address corresponding to the virtual address
9653 * @attrs: set to the memory transaction attributes to use
9654 * @prot: set to the permissions for the page containing phys_ptr
9655 * @page_size: set to the size of the page containing phys_ptr
9656 * @fsr: set to the DFSR/IFSR value on failure
9657 * @fi: set to fault info if the translation fails
9658 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
9659 */
9660 static bool get_phys_addr(CPUARMState *env, target_ulong address,
9661 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9662 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
9663 target_ulong *page_size, uint32_t *fsr,
9664 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9665 {
9666 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9667 /* Call ourselves recursively to do the stage 1 and then stage 2
9668 * translations.
9669 */
9670 if (arm_feature(env, ARM_FEATURE_EL2)) {
9671 hwaddr ipa;
9672 int s2_prot;
9673 int ret;
9674 ARMCacheAttrs cacheattrs2 = {};
9675
9676 ret = get_phys_addr(env, address, access_type,
9677 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
9678 prot, page_size, fsr, fi, cacheattrs);
9679
9680 /* If S1 fails or S2 is disabled, return early. */
9681 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
9682 *phys_ptr = ipa;
9683 return ret;
9684 }
9685
9686 /* S1 is done. Now do S2 translation. */
9687 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
9688 phys_ptr, attrs, &s2_prot,
9689 page_size, fsr, fi,
9690 cacheattrs != NULL ? &cacheattrs2 : NULL);
9691 fi->s2addr = ipa;
9692 /* Combine the S1 and S2 perms. */
9693 *prot &= s2_prot;
9694
9695 /* Combine the S1 and S2 cache attributes, if needed */
9696 if (!ret && cacheattrs != NULL) {
9697 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
9698 }
9699
9700 return ret;
9701 } else {
9702 /*
9703 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
9704 */
9705 mmu_idx = stage_1_mmu_idx(mmu_idx);
9706 }
9707 }
9708
9709 /* The page table entries may downgrade secure to non-secure, but
9710 * cannot upgrade an non-secure translation regime's attributes
9711 * to secure.
9712 */
9713 attrs->secure = regime_is_secure(env, mmu_idx);
9714 attrs->user = regime_is_user(env, mmu_idx);
9715
9716 /* Fast Context Switch Extension. This doesn't exist at all in v8.
9717 * In v7 and earlier it affects all stage 1 translations.
9718 */
9719 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
9720 && !arm_feature(env, ARM_FEATURE_V8)) {
9721 if (regime_el(env, mmu_idx) == 3) {
9722 address += env->cp15.fcseidr_s;
9723 } else {
9724 address += env->cp15.fcseidr_ns;
9725 }
9726 }
9727
9728 if (arm_feature(env, ARM_FEATURE_PMSA)) {
9729 bool ret;
9730 *page_size = TARGET_PAGE_SIZE;
9731
9732 if (arm_feature(env, ARM_FEATURE_V8)) {
9733 /* PMSAv8 */
9734 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
9735 phys_ptr, attrs, prot, fsr);
9736 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9737 /* PMSAv7 */
9738 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9739 phys_ptr, prot, fsr);
9740 } else {
9741 /* Pre-v7 MPU */
9742 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
9743 phys_ptr, prot, fsr);
9744 }
9745 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
9746 " mmu_idx %u -> %s (prot %c%c%c)\n",
9747 access_type == MMU_DATA_LOAD ? "reading" :
9748 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
9749 (uint32_t)address, mmu_idx,
9750 ret ? "Miss" : "Hit",
9751 *prot & PAGE_READ ? 'r' : '-',
9752 *prot & PAGE_WRITE ? 'w' : '-',
9753 *prot & PAGE_EXEC ? 'x' : '-');
9754
9755 return ret;
9756 }
9757
9758 /* Definitely a real MMU, not an MPU */
9759
9760 if (regime_translation_disabled(env, mmu_idx)) {
9761 /* MMU disabled. */
9762 *phys_ptr = address;
9763 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9764 *page_size = TARGET_PAGE_SIZE;
9765 return 0;
9766 }
9767
9768 if (regime_using_lpae_format(env, mmu_idx)) {
9769 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
9770 attrs, prot, page_size, fsr, fi, cacheattrs);
9771 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
9772 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
9773 attrs, prot, page_size, fsr, fi);
9774 } else {
9775 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
9776 prot, page_size, fsr, fi);
9777 }
9778 }
9779
9780 /* Walk the page table and (if the mapping exists) add the page
9781 * to the TLB. Return false on success, or true on failure. Populate
9782 * fsr with ARM DFSR/IFSR fault register format value on failure.
9783 */
9784 bool arm_tlb_fill(CPUState *cs, vaddr address,
9785 MMUAccessType access_type, int mmu_idx, uint32_t *fsr,
9786 ARMMMUFaultInfo *fi)
9787 {
9788 ARMCPU *cpu = ARM_CPU(cs);
9789 CPUARMState *env = &cpu->env;
9790 hwaddr phys_addr;
9791 target_ulong page_size;
9792 int prot;
9793 int ret;
9794 MemTxAttrs attrs = {};
9795
9796 ret = get_phys_addr(env, address, access_type,
9797 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
9798 &attrs, &prot, &page_size, fsr, fi, NULL);
9799 if (!ret) {
9800 /* Map a single [sub]page. */
9801 phys_addr &= TARGET_PAGE_MASK;
9802 address &= TARGET_PAGE_MASK;
9803 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
9804 prot, mmu_idx, page_size);
9805 return 0;
9806 }
9807
9808 return ret;
9809 }
9810
9811 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
9812 MemTxAttrs *attrs)
9813 {
9814 ARMCPU *cpu = ARM_CPU(cs);
9815 CPUARMState *env = &cpu->env;
9816 hwaddr phys_addr;
9817 target_ulong page_size;
9818 int prot;
9819 bool ret;
9820 uint32_t fsr;
9821 ARMMMUFaultInfo fi = {};
9822 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
9823
9824 *attrs = (MemTxAttrs) {};
9825
9826 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
9827 attrs, &prot, &page_size, &fsr, &fi, NULL);
9828
9829 if (ret) {
9830 return -1;
9831 }
9832 return phys_addr;
9833 }
9834
9835 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9836 {
9837 uint32_t mask;
9838 unsigned el = arm_current_el(env);
9839
9840 /* First handle registers which unprivileged can read */
9841
9842 switch (reg) {
9843 case 0 ... 7: /* xPSR sub-fields */
9844 mask = 0;
9845 if ((reg & 1) && el) {
9846 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
9847 }
9848 if (!(reg & 4)) {
9849 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
9850 }
9851 /* EPSR reads as zero */
9852 return xpsr_read(env) & mask;
9853 break;
9854 case 20: /* CONTROL */
9855 return env->v7m.control[env->v7m.secure];
9856 case 0x94: /* CONTROL_NS */
9857 /* We have to handle this here because unprivileged Secure code
9858 * can read the NS CONTROL register.
9859 */
9860 if (!env->v7m.secure) {
9861 return 0;
9862 }
9863 return env->v7m.control[M_REG_NS];
9864 }
9865
9866 if (el == 0) {
9867 return 0; /* unprivileged reads others as zero */
9868 }
9869
9870 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9871 switch (reg) {
9872 case 0x88: /* MSP_NS */
9873 if (!env->v7m.secure) {
9874 return 0;
9875 }
9876 return env->v7m.other_ss_msp;
9877 case 0x89: /* PSP_NS */
9878 if (!env->v7m.secure) {
9879 return 0;
9880 }
9881 return env->v7m.other_ss_psp;
9882 case 0x90: /* PRIMASK_NS */
9883 if (!env->v7m.secure) {
9884 return 0;
9885 }
9886 return env->v7m.primask[M_REG_NS];
9887 case 0x91: /* BASEPRI_NS */
9888 if (!env->v7m.secure) {
9889 return 0;
9890 }
9891 return env->v7m.basepri[M_REG_NS];
9892 case 0x93: /* FAULTMASK_NS */
9893 if (!env->v7m.secure) {
9894 return 0;
9895 }
9896 return env->v7m.faultmask[M_REG_NS];
9897 case 0x98: /* SP_NS */
9898 {
9899 /* This gives the non-secure SP selected based on whether we're
9900 * currently in handler mode or not, using the NS CONTROL.SPSEL.
9901 */
9902 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
9903
9904 if (!env->v7m.secure) {
9905 return 0;
9906 }
9907 if (!arm_v7m_is_handler_mode(env) && spsel) {
9908 return env->v7m.other_ss_psp;
9909 } else {
9910 return env->v7m.other_ss_msp;
9911 }
9912 }
9913 default:
9914 break;
9915 }
9916 }
9917
9918 switch (reg) {
9919 case 8: /* MSP */
9920 return (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) ?
9921 env->v7m.other_sp : env->regs[13];
9922 case 9: /* PSP */
9923 return (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) ?
9924 env->regs[13] : env->v7m.other_sp;
9925 case 16: /* PRIMASK */
9926 return env->v7m.primask[env->v7m.secure];
9927 case 17: /* BASEPRI */
9928 case 18: /* BASEPRI_MAX */
9929 return env->v7m.basepri[env->v7m.secure];
9930 case 19: /* FAULTMASK */
9931 return env->v7m.faultmask[env->v7m.secure];
9932 default:
9933 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
9934 " register %d\n", reg);
9935 return 0;
9936 }
9937 }
9938
9939 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
9940 {
9941 /* We're passed bits [11..0] of the instruction; extract
9942 * SYSm and the mask bits.
9943 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
9944 * we choose to treat them as if the mask bits were valid.
9945 * NB that the pseudocode 'mask' variable is bits [11..10],
9946 * whereas ours is [11..8].
9947 */
9948 uint32_t mask = extract32(maskreg, 8, 4);
9949 uint32_t reg = extract32(maskreg, 0, 8);
9950
9951 if (arm_current_el(env) == 0 && reg > 7) {
9952 /* only xPSR sub-fields may be written by unprivileged */
9953 return;
9954 }
9955
9956 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9957 switch (reg) {
9958 case 0x88: /* MSP_NS */
9959 if (!env->v7m.secure) {
9960 return;
9961 }
9962 env->v7m.other_ss_msp = val;
9963 return;
9964 case 0x89: /* PSP_NS */
9965 if (!env->v7m.secure) {
9966 return;
9967 }
9968 env->v7m.other_ss_psp = val;
9969 return;
9970 case 0x90: /* PRIMASK_NS */
9971 if (!env->v7m.secure) {
9972 return;
9973 }
9974 env->v7m.primask[M_REG_NS] = val & 1;
9975 return;
9976 case 0x91: /* BASEPRI_NS */
9977 if (!env->v7m.secure) {
9978 return;
9979 }
9980 env->v7m.basepri[M_REG_NS] = val & 0xff;
9981 return;
9982 case 0x93: /* FAULTMASK_NS */
9983 if (!env->v7m.secure) {
9984 return;
9985 }
9986 env->v7m.faultmask[M_REG_NS] = val & 1;
9987 return;
9988 case 0x98: /* SP_NS */
9989 {
9990 /* This gives the non-secure SP selected based on whether we're
9991 * currently in handler mode or not, using the NS CONTROL.SPSEL.
9992 */
9993 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
9994
9995 if (!env->v7m.secure) {
9996 return;
9997 }
9998 if (!arm_v7m_is_handler_mode(env) && spsel) {
9999 env->v7m.other_ss_psp = val;
10000 } else {
10001 env->v7m.other_ss_msp = val;
10002 }
10003 return;
10004 }
10005 default:
10006 break;
10007 }
10008 }
10009
10010 switch (reg) {
10011 case 0 ... 7: /* xPSR sub-fields */
10012 /* only APSR is actually writable */
10013 if (!(reg & 4)) {
10014 uint32_t apsrmask = 0;
10015
10016 if (mask & 8) {
10017 apsrmask |= XPSR_NZCV | XPSR_Q;
10018 }
10019 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
10020 apsrmask |= XPSR_GE;
10021 }
10022 xpsr_write(env, val, apsrmask);
10023 }
10024 break;
10025 case 8: /* MSP */
10026 if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) {
10027 env->v7m.other_sp = val;
10028 } else {
10029 env->regs[13] = val;
10030 }
10031 break;
10032 case 9: /* PSP */
10033 if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) {
10034 env->regs[13] = val;
10035 } else {
10036 env->v7m.other_sp = val;
10037 }
10038 break;
10039 case 16: /* PRIMASK */
10040 env->v7m.primask[env->v7m.secure] = val & 1;
10041 break;
10042 case 17: /* BASEPRI */
10043 env->v7m.basepri[env->v7m.secure] = val & 0xff;
10044 break;
10045 case 18: /* BASEPRI_MAX */
10046 val &= 0xff;
10047 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10048 || env->v7m.basepri[env->v7m.secure] == 0)) {
10049 env->v7m.basepri[env->v7m.secure] = val;
10050 }
10051 break;
10052 case 19: /* FAULTMASK */
10053 env->v7m.faultmask[env->v7m.secure] = val & 1;
10054 break;
10055 case 20: /* CONTROL */
10056 /* Writing to the SPSEL bit only has an effect if we are in
10057 * thread mode; other bits can be updated by any privileged code.
10058 * write_v7m_control_spsel() deals with updating the SPSEL bit in
10059 * env->v7m.control, so we only need update the others.
10060 */
10061 if (!arm_v7m_is_handler_mode(env)) {
10062 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
10063 }
10064 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10065 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
10066 break;
10067 default:
10068 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10069 " register %d\n", reg);
10070 return;
10071 }
10072 }
10073
10074 #endif
10075
10076 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10077 {
10078 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10079 * Note that we do not implement the (architecturally mandated)
10080 * alignment fault for attempts to use this on Device memory
10081 * (which matches the usual QEMU behaviour of not implementing either
10082 * alignment faults or any memory attribute handling).
10083 */
10084
10085 ARMCPU *cpu = arm_env_get_cpu(env);
10086 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10087 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10088
10089 #ifndef CONFIG_USER_ONLY
10090 {
10091 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10092 * the block size so we might have to do more than one TLB lookup.
10093 * We know that in fact for any v8 CPU the page size is at least 4K
10094 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10095 * 1K as an artefact of legacy v5 subpage support being present in the
10096 * same QEMU executable.
10097 */
10098 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10099 void *hostaddr[maxidx];
10100 int try, i;
10101 unsigned mmu_idx = cpu_mmu_index(env, false);
10102 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
10103
10104 for (try = 0; try < 2; try++) {
10105
10106 for (i = 0; i < maxidx; i++) {
10107 hostaddr[i] = tlb_vaddr_to_host(env,
10108 vaddr + TARGET_PAGE_SIZE * i,
10109 1, mmu_idx);
10110 if (!hostaddr[i]) {
10111 break;
10112 }
10113 }
10114 if (i == maxidx) {
10115 /* If it's all in the TLB it's fair game for just writing to;
10116 * we know we don't need to update dirty status, etc.
10117 */
10118 for (i = 0; i < maxidx - 1; i++) {
10119 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10120 }
10121 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10122 return;
10123 }
10124 /* OK, try a store and see if we can populate the tlb. This
10125 * might cause an exception if the memory isn't writable,
10126 * in which case we will longjmp out of here. We must for
10127 * this purpose use the actual register value passed to us
10128 * so that we get the fault address right.
10129 */
10130 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
10131 /* Now we can populate the other TLB entries, if any */
10132 for (i = 0; i < maxidx; i++) {
10133 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10134 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
10135 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
10136 }
10137 }
10138 }
10139
10140 /* Slow path (probably attempt to do this to an I/O device or
10141 * similar, or clearing of a block of code we have translations
10142 * cached for). Just do a series of byte writes as the architecture
10143 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10144 * memset(), unmap() sequence here because:
10145 * + we'd need to account for the blocksize being larger than a page
10146 * + the direct-RAM access case is almost always going to be dealt
10147 * with in the fastpath code above, so there's no speed benefit
10148 * + we would have to deal with the map returning NULL because the
10149 * bounce buffer was in use
10150 */
10151 for (i = 0; i < blocklen; i++) {
10152 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
10153 }
10154 }
10155 #else
10156 memset(g2h(vaddr), 0, blocklen);
10157 #endif
10158 }
10159
10160 /* Note that signed overflow is undefined in C. The following routines are
10161 careful to use unsigned types where modulo arithmetic is required.
10162 Failure to do so _will_ break on newer gcc. */
10163
10164 /* Signed saturating arithmetic. */
10165
10166 /* Perform 16-bit signed saturating addition. */
10167 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10168 {
10169 uint16_t res;
10170
10171 res = a + b;
10172 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10173 if (a & 0x8000)
10174 res = 0x8000;
10175 else
10176 res = 0x7fff;
10177 }
10178 return res;
10179 }
10180
10181 /* Perform 8-bit signed saturating addition. */
10182 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10183 {
10184 uint8_t res;
10185
10186 res = a + b;
10187 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10188 if (a & 0x80)
10189 res = 0x80;
10190 else
10191 res = 0x7f;
10192 }
10193 return res;
10194 }
10195
10196 /* Perform 16-bit signed saturating subtraction. */
10197 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10198 {
10199 uint16_t res;
10200
10201 res = a - b;
10202 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10203 if (a & 0x8000)
10204 res = 0x8000;
10205 else
10206 res = 0x7fff;
10207 }
10208 return res;
10209 }
10210
10211 /* Perform 8-bit signed saturating subtraction. */
10212 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10213 {
10214 uint8_t res;
10215
10216 res = a - b;
10217 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10218 if (a & 0x80)
10219 res = 0x80;
10220 else
10221 res = 0x7f;
10222 }
10223 return res;
10224 }
10225
10226 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10227 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10228 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10229 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10230 #define PFX q
10231
10232 #include "op_addsub.h"
10233
10234 /* Unsigned saturating arithmetic. */
10235 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10236 {
10237 uint16_t res;
10238 res = a + b;
10239 if (res < a)
10240 res = 0xffff;
10241 return res;
10242 }
10243
10244 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10245 {
10246 if (a > b)
10247 return a - b;
10248 else
10249 return 0;
10250 }
10251
10252 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10253 {
10254 uint8_t res;
10255 res = a + b;
10256 if (res < a)
10257 res = 0xff;
10258 return res;
10259 }
10260
10261 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10262 {
10263 if (a > b)
10264 return a - b;
10265 else
10266 return 0;
10267 }
10268
10269 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10270 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10271 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10272 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10273 #define PFX uq
10274
10275 #include "op_addsub.h"
10276
10277 /* Signed modulo arithmetic. */
10278 #define SARITH16(a, b, n, op) do { \
10279 int32_t sum; \
10280 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10281 RESULT(sum, n, 16); \
10282 if (sum >= 0) \
10283 ge |= 3 << (n * 2); \
10284 } while(0)
10285
10286 #define SARITH8(a, b, n, op) do { \
10287 int32_t sum; \
10288 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10289 RESULT(sum, n, 8); \
10290 if (sum >= 0) \
10291 ge |= 1 << n; \
10292 } while(0)
10293
10294
10295 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10296 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10297 #define ADD8(a, b, n) SARITH8(a, b, n, +)
10298 #define SUB8(a, b, n) SARITH8(a, b, n, -)
10299 #define PFX s
10300 #define ARITH_GE
10301
10302 #include "op_addsub.h"
10303
10304 /* Unsigned modulo arithmetic. */
10305 #define ADD16(a, b, n) do { \
10306 uint32_t sum; \
10307 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10308 RESULT(sum, n, 16); \
10309 if ((sum >> 16) == 1) \
10310 ge |= 3 << (n * 2); \
10311 } while(0)
10312
10313 #define ADD8(a, b, n) do { \
10314 uint32_t sum; \
10315 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10316 RESULT(sum, n, 8); \
10317 if ((sum >> 8) == 1) \
10318 ge |= 1 << n; \
10319 } while(0)
10320
10321 #define SUB16(a, b, n) do { \
10322 uint32_t sum; \
10323 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10324 RESULT(sum, n, 16); \
10325 if ((sum >> 16) == 0) \
10326 ge |= 3 << (n * 2); \
10327 } while(0)
10328
10329 #define SUB8(a, b, n) do { \
10330 uint32_t sum; \
10331 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10332 RESULT(sum, n, 8); \
10333 if ((sum >> 8) == 0) \
10334 ge |= 1 << n; \
10335 } while(0)
10336
10337 #define PFX u
10338 #define ARITH_GE
10339
10340 #include "op_addsub.h"
10341
10342 /* Halved signed arithmetic. */
10343 #define ADD16(a, b, n) \
10344 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10345 #define SUB16(a, b, n) \
10346 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10347 #define ADD8(a, b, n) \
10348 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10349 #define SUB8(a, b, n) \
10350 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10351 #define PFX sh
10352
10353 #include "op_addsub.h"
10354
10355 /* Halved unsigned arithmetic. */
10356 #define ADD16(a, b, n) \
10357 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10358 #define SUB16(a, b, n) \
10359 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10360 #define ADD8(a, b, n) \
10361 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10362 #define SUB8(a, b, n) \
10363 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10364 #define PFX uh
10365
10366 #include "op_addsub.h"
10367
10368 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10369 {
10370 if (a > b)
10371 return a - b;
10372 else
10373 return b - a;
10374 }
10375
10376 /* Unsigned sum of absolute byte differences. */
10377 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10378 {
10379 uint32_t sum;
10380 sum = do_usad(a, b);
10381 sum += do_usad(a >> 8, b >> 8);
10382 sum += do_usad(a >> 16, b >>16);
10383 sum += do_usad(a >> 24, b >> 24);
10384 return sum;
10385 }
10386
10387 /* For ARMv6 SEL instruction. */
10388 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10389 {
10390 uint32_t mask;
10391
10392 mask = 0;
10393 if (flags & 1)
10394 mask |= 0xff;
10395 if (flags & 2)
10396 mask |= 0xff00;
10397 if (flags & 4)
10398 mask |= 0xff0000;
10399 if (flags & 8)
10400 mask |= 0xff000000;
10401 return (a & mask) | (b & ~mask);
10402 }
10403
10404 /* VFP support. We follow the convention used for VFP instructions:
10405 Single precision routines have a "s" suffix, double precision a
10406 "d" suffix. */
10407
10408 /* Convert host exception flags to vfp form. */
10409 static inline int vfp_exceptbits_from_host(int host_bits)
10410 {
10411 int target_bits = 0;
10412
10413 if (host_bits & float_flag_invalid)
10414 target_bits |= 1;
10415 if (host_bits & float_flag_divbyzero)
10416 target_bits |= 2;
10417 if (host_bits & float_flag_overflow)
10418 target_bits |= 4;
10419 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
10420 target_bits |= 8;
10421 if (host_bits & float_flag_inexact)
10422 target_bits |= 0x10;
10423 if (host_bits & float_flag_input_denormal)
10424 target_bits |= 0x80;
10425 return target_bits;
10426 }
10427
10428 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
10429 {
10430 int i;
10431 uint32_t fpscr;
10432
10433 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
10434 | (env->vfp.vec_len << 16)
10435 | (env->vfp.vec_stride << 20);
10436 i = get_float_exception_flags(&env->vfp.fp_status);
10437 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
10438 fpscr |= vfp_exceptbits_from_host(i);
10439 return fpscr;
10440 }
10441
10442 uint32_t vfp_get_fpscr(CPUARMState *env)
10443 {
10444 return HELPER(vfp_get_fpscr)(env);
10445 }
10446
10447 /* Convert vfp exception flags to target form. */
10448 static inline int vfp_exceptbits_to_host(int target_bits)
10449 {
10450 int host_bits = 0;
10451
10452 if (target_bits & 1)
10453 host_bits |= float_flag_invalid;
10454 if (target_bits & 2)
10455 host_bits |= float_flag_divbyzero;
10456 if (target_bits & 4)
10457 host_bits |= float_flag_overflow;
10458 if (target_bits & 8)
10459 host_bits |= float_flag_underflow;
10460 if (target_bits & 0x10)
10461 host_bits |= float_flag_inexact;
10462 if (target_bits & 0x80)
10463 host_bits |= float_flag_input_denormal;
10464 return host_bits;
10465 }
10466
10467 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
10468 {
10469 int i;
10470 uint32_t changed;
10471
10472 changed = env->vfp.xregs[ARM_VFP_FPSCR];
10473 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
10474 env->vfp.vec_len = (val >> 16) & 7;
10475 env->vfp.vec_stride = (val >> 20) & 3;
10476
10477 changed ^= val;
10478 if (changed & (3 << 22)) {
10479 i = (val >> 22) & 3;
10480 switch (i) {
10481 case FPROUNDING_TIEEVEN:
10482 i = float_round_nearest_even;
10483 break;
10484 case FPROUNDING_POSINF:
10485 i = float_round_up;
10486 break;
10487 case FPROUNDING_NEGINF:
10488 i = float_round_down;
10489 break;
10490 case FPROUNDING_ZERO:
10491 i = float_round_to_zero;
10492 break;
10493 }
10494 set_float_rounding_mode(i, &env->vfp.fp_status);
10495 }
10496 if (changed & (1 << 24)) {
10497 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
10498 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
10499 }
10500 if (changed & (1 << 25))
10501 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
10502
10503 i = vfp_exceptbits_to_host(val);
10504 set_float_exception_flags(i, &env->vfp.fp_status);
10505 set_float_exception_flags(0, &env->vfp.standard_fp_status);
10506 }
10507
10508 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
10509 {
10510 HELPER(vfp_set_fpscr)(env, val);
10511 }
10512
10513 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
10514
10515 #define VFP_BINOP(name) \
10516 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
10517 { \
10518 float_status *fpst = fpstp; \
10519 return float32_ ## name(a, b, fpst); \
10520 } \
10521 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
10522 { \
10523 float_status *fpst = fpstp; \
10524 return float64_ ## name(a, b, fpst); \
10525 }
10526 VFP_BINOP(add)
10527 VFP_BINOP(sub)
10528 VFP_BINOP(mul)
10529 VFP_BINOP(div)
10530 VFP_BINOP(min)
10531 VFP_BINOP(max)
10532 VFP_BINOP(minnum)
10533 VFP_BINOP(maxnum)
10534 #undef VFP_BINOP
10535
10536 float32 VFP_HELPER(neg, s)(float32 a)
10537 {
10538 return float32_chs(a);
10539 }
10540
10541 float64 VFP_HELPER(neg, d)(float64 a)
10542 {
10543 return float64_chs(a);
10544 }
10545
10546 float32 VFP_HELPER(abs, s)(float32 a)
10547 {
10548 return float32_abs(a);
10549 }
10550
10551 float64 VFP_HELPER(abs, d)(float64 a)
10552 {
10553 return float64_abs(a);
10554 }
10555
10556 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
10557 {
10558 return float32_sqrt(a, &env->vfp.fp_status);
10559 }
10560
10561 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
10562 {
10563 return float64_sqrt(a, &env->vfp.fp_status);
10564 }
10565
10566 /* XXX: check quiet/signaling case */
10567 #define DO_VFP_cmp(p, type) \
10568 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
10569 { \
10570 uint32_t flags; \
10571 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
10572 case 0: flags = 0x6; break; \
10573 case -1: flags = 0x8; break; \
10574 case 1: flags = 0x2; break; \
10575 default: case 2: flags = 0x3; break; \
10576 } \
10577 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10578 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10579 } \
10580 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
10581 { \
10582 uint32_t flags; \
10583 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
10584 case 0: flags = 0x6; break; \
10585 case -1: flags = 0x8; break; \
10586 case 1: flags = 0x2; break; \
10587 default: case 2: flags = 0x3; break; \
10588 } \
10589 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10590 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10591 }
10592 DO_VFP_cmp(s, float32)
10593 DO_VFP_cmp(d, float64)
10594 #undef DO_VFP_cmp
10595
10596 /* Integer to float and float to integer conversions */
10597
10598 #define CONV_ITOF(name, fsz, sign) \
10599 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
10600 { \
10601 float_status *fpst = fpstp; \
10602 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
10603 }
10604
10605 #define CONV_FTOI(name, fsz, sign, round) \
10606 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
10607 { \
10608 float_status *fpst = fpstp; \
10609 if (float##fsz##_is_any_nan(x)) { \
10610 float_raise(float_flag_invalid, fpst); \
10611 return 0; \
10612 } \
10613 return float##fsz##_to_##sign##int32##round(x, fpst); \
10614 }
10615
10616 #define FLOAT_CONVS(name, p, fsz, sign) \
10617 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
10618 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
10619 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
10620
10621 FLOAT_CONVS(si, s, 32, )
10622 FLOAT_CONVS(si, d, 64, )
10623 FLOAT_CONVS(ui, s, 32, u)
10624 FLOAT_CONVS(ui, d, 64, u)
10625
10626 #undef CONV_ITOF
10627 #undef CONV_FTOI
10628 #undef FLOAT_CONVS
10629
10630 /* floating point conversion */
10631 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
10632 {
10633 float64 r = float32_to_float64(x, &env->vfp.fp_status);
10634 /* ARM requires that S<->D conversion of any kind of NaN generates
10635 * a quiet NaN by forcing the most significant frac bit to 1.
10636 */
10637 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
10638 }
10639
10640 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
10641 {
10642 float32 r = float64_to_float32(x, &env->vfp.fp_status);
10643 /* ARM requires that S<->D conversion of any kind of NaN generates
10644 * a quiet NaN by forcing the most significant frac bit to 1.
10645 */
10646 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
10647 }
10648
10649 /* VFP3 fixed point conversion. */
10650 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10651 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
10652 void *fpstp) \
10653 { \
10654 float_status *fpst = fpstp; \
10655 float##fsz tmp; \
10656 tmp = itype##_to_##float##fsz(x, fpst); \
10657 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
10658 }
10659
10660 /* Notice that we want only input-denormal exception flags from the
10661 * scalbn operation: the other possible flags (overflow+inexact if
10662 * we overflow to infinity, output-denormal) aren't correct for the
10663 * complete scale-and-convert operation.
10664 */
10665 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
10666 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
10667 uint32_t shift, \
10668 void *fpstp) \
10669 { \
10670 float_status *fpst = fpstp; \
10671 int old_exc_flags = get_float_exception_flags(fpst); \
10672 float##fsz tmp; \
10673 if (float##fsz##_is_any_nan(x)) { \
10674 float_raise(float_flag_invalid, fpst); \
10675 return 0; \
10676 } \
10677 tmp = float##fsz##_scalbn(x, shift, fpst); \
10678 old_exc_flags |= get_float_exception_flags(fpst) \
10679 & float_flag_input_denormal; \
10680 set_float_exception_flags(old_exc_flags, fpst); \
10681 return float##fsz##_to_##itype##round(tmp, fpst); \
10682 }
10683
10684 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
10685 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10686 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
10687 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
10688
10689 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
10690 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10691 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
10692
10693 VFP_CONV_FIX(sh, d, 64, 64, int16)
10694 VFP_CONV_FIX(sl, d, 64, 64, int32)
10695 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
10696 VFP_CONV_FIX(uh, d, 64, 64, uint16)
10697 VFP_CONV_FIX(ul, d, 64, 64, uint32)
10698 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
10699 VFP_CONV_FIX(sh, s, 32, 32, int16)
10700 VFP_CONV_FIX(sl, s, 32, 32, int32)
10701 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
10702 VFP_CONV_FIX(uh, s, 32, 32, uint16)
10703 VFP_CONV_FIX(ul, s, 32, 32, uint32)
10704 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
10705 #undef VFP_CONV_FIX
10706 #undef VFP_CONV_FIX_FLOAT
10707 #undef VFP_CONV_FLOAT_FIX_ROUND
10708
10709 /* Set the current fp rounding mode and return the old one.
10710 * The argument is a softfloat float_round_ value.
10711 */
10712 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
10713 {
10714 float_status *fp_status = &env->vfp.fp_status;
10715
10716 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10717 set_float_rounding_mode(rmode, fp_status);
10718
10719 return prev_rmode;
10720 }
10721
10722 /* Set the current fp rounding mode in the standard fp status and return
10723 * the old one. This is for NEON instructions that need to change the
10724 * rounding mode but wish to use the standard FPSCR values for everything
10725 * else. Always set the rounding mode back to the correct value after
10726 * modifying it.
10727 * The argument is a softfloat float_round_ value.
10728 */
10729 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
10730 {
10731 float_status *fp_status = &env->vfp.standard_fp_status;
10732
10733 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10734 set_float_rounding_mode(rmode, fp_status);
10735
10736 return prev_rmode;
10737 }
10738
10739 /* Half precision conversions. */
10740 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
10741 {
10742 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10743 float32 r = float16_to_float32(make_float16(a), ieee, s);
10744 if (ieee) {
10745 return float32_maybe_silence_nan(r, s);
10746 }
10747 return r;
10748 }
10749
10750 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
10751 {
10752 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10753 float16 r = float32_to_float16(a, ieee, s);
10754 if (ieee) {
10755 r = float16_maybe_silence_nan(r, s);
10756 }
10757 return float16_val(r);
10758 }
10759
10760 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
10761 {
10762 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
10763 }
10764
10765 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
10766 {
10767 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
10768 }
10769
10770 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
10771 {
10772 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
10773 }
10774
10775 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
10776 {
10777 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
10778 }
10779
10780 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
10781 {
10782 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10783 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
10784 if (ieee) {
10785 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
10786 }
10787 return r;
10788 }
10789
10790 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
10791 {
10792 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10793 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
10794 if (ieee) {
10795 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
10796 }
10797 return float16_val(r);
10798 }
10799
10800 #define float32_two make_float32(0x40000000)
10801 #define float32_three make_float32(0x40400000)
10802 #define float32_one_point_five make_float32(0x3fc00000)
10803
10804 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
10805 {
10806 float_status *s = &env->vfp.standard_fp_status;
10807 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
10808 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
10809 if (!(float32_is_zero(a) || float32_is_zero(b))) {
10810 float_raise(float_flag_input_denormal, s);
10811 }
10812 return float32_two;
10813 }
10814 return float32_sub(float32_two, float32_mul(a, b, s), s);
10815 }
10816
10817 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
10818 {
10819 float_status *s = &env->vfp.standard_fp_status;
10820 float32 product;
10821 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
10822 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
10823 if (!(float32_is_zero(a) || float32_is_zero(b))) {
10824 float_raise(float_flag_input_denormal, s);
10825 }
10826 return float32_one_point_five;
10827 }
10828 product = float32_mul(a, b, s);
10829 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
10830 }
10831
10832 /* NEON helpers. */
10833
10834 /* Constants 256 and 512 are used in some helpers; we avoid relying on
10835 * int->float conversions at run-time. */
10836 #define float64_256 make_float64(0x4070000000000000LL)
10837 #define float64_512 make_float64(0x4080000000000000LL)
10838 #define float32_maxnorm make_float32(0x7f7fffff)
10839 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
10840
10841 /* Reciprocal functions
10842 *
10843 * The algorithm that must be used to calculate the estimate
10844 * is specified by the ARM ARM, see FPRecipEstimate()
10845 */
10846
10847 static float64 recip_estimate(float64 a, float_status *real_fp_status)
10848 {
10849 /* These calculations mustn't set any fp exception flags,
10850 * so we use a local copy of the fp_status.
10851 */
10852 float_status dummy_status = *real_fp_status;
10853 float_status *s = &dummy_status;
10854 /* q = (int)(a * 512.0) */
10855 float64 q = float64_mul(float64_512, a, s);
10856 int64_t q_int = float64_to_int64_round_to_zero(q, s);
10857
10858 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
10859 q = int64_to_float64(q_int, s);
10860 q = float64_add(q, float64_half, s);
10861 q = float64_div(q, float64_512, s);
10862 q = float64_div(float64_one, q, s);
10863
10864 /* s = (int)(256.0 * r + 0.5) */
10865 q = float64_mul(q, float64_256, s);
10866 q = float64_add(q, float64_half, s);
10867 q_int = float64_to_int64_round_to_zero(q, s);
10868
10869 /* return (double)s / 256.0 */
10870 return float64_div(int64_to_float64(q_int, s), float64_256, s);
10871 }
10872
10873 /* Common wrapper to call recip_estimate */
10874 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
10875 {
10876 uint64_t val64 = float64_val(num);
10877 uint64_t frac = extract64(val64, 0, 52);
10878 int64_t exp = extract64(val64, 52, 11);
10879 uint64_t sbit;
10880 float64 scaled, estimate;
10881
10882 /* Generate the scaled number for the estimate function */
10883 if (exp == 0) {
10884 if (extract64(frac, 51, 1) == 0) {
10885 exp = -1;
10886 frac = extract64(frac, 0, 50) << 2;
10887 } else {
10888 frac = extract64(frac, 0, 51) << 1;
10889 }
10890 }
10891
10892 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
10893 scaled = make_float64((0x3feULL << 52)
10894 | extract64(frac, 44, 8) << 44);
10895
10896 estimate = recip_estimate(scaled, fpst);
10897
10898 /* Build new result */
10899 val64 = float64_val(estimate);
10900 sbit = 0x8000000000000000ULL & val64;
10901 exp = off - exp;
10902 frac = extract64(val64, 0, 52);
10903
10904 if (exp == 0) {
10905 frac = 1ULL << 51 | extract64(frac, 1, 51);
10906 } else if (exp == -1) {
10907 frac = 1ULL << 50 | extract64(frac, 2, 50);
10908 exp = 0;
10909 }
10910
10911 return make_float64(sbit | (exp << 52) | frac);
10912 }
10913
10914 static bool round_to_inf(float_status *fpst, bool sign_bit)
10915 {
10916 switch (fpst->float_rounding_mode) {
10917 case float_round_nearest_even: /* Round to Nearest */
10918 return true;
10919 case float_round_up: /* Round to +Inf */
10920 return !sign_bit;
10921 case float_round_down: /* Round to -Inf */
10922 return sign_bit;
10923 case float_round_to_zero: /* Round to Zero */
10924 return false;
10925 }
10926
10927 g_assert_not_reached();
10928 }
10929
10930 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
10931 {
10932 float_status *fpst = fpstp;
10933 float32 f32 = float32_squash_input_denormal(input, fpst);
10934 uint32_t f32_val = float32_val(f32);
10935 uint32_t f32_sbit = 0x80000000ULL & f32_val;
10936 int32_t f32_exp = extract32(f32_val, 23, 8);
10937 uint32_t f32_frac = extract32(f32_val, 0, 23);
10938 float64 f64, r64;
10939 uint64_t r64_val;
10940 int64_t r64_exp;
10941 uint64_t r64_frac;
10942
10943 if (float32_is_any_nan(f32)) {
10944 float32 nan = f32;
10945 if (float32_is_signaling_nan(f32, fpst)) {
10946 float_raise(float_flag_invalid, fpst);
10947 nan = float32_maybe_silence_nan(f32, fpst);
10948 }
10949 if (fpst->default_nan_mode) {
10950 nan = float32_default_nan(fpst);
10951 }
10952 return nan;
10953 } else if (float32_is_infinity(f32)) {
10954 return float32_set_sign(float32_zero, float32_is_neg(f32));
10955 } else if (float32_is_zero(f32)) {
10956 float_raise(float_flag_divbyzero, fpst);
10957 return float32_set_sign(float32_infinity, float32_is_neg(f32));
10958 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
10959 /* Abs(value) < 2.0^-128 */
10960 float_raise(float_flag_overflow | float_flag_inexact, fpst);
10961 if (round_to_inf(fpst, f32_sbit)) {
10962 return float32_set_sign(float32_infinity, float32_is_neg(f32));
10963 } else {
10964 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
10965 }
10966 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
10967 float_raise(float_flag_underflow, fpst);
10968 return float32_set_sign(float32_zero, float32_is_neg(f32));
10969 }
10970
10971
10972 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
10973 r64 = call_recip_estimate(f64, 253, fpst);
10974 r64_val = float64_val(r64);
10975 r64_exp = extract64(r64_val, 52, 11);
10976 r64_frac = extract64(r64_val, 0, 52);
10977
10978 /* result = sign : result_exp<7:0> : fraction<51:29>; */
10979 return make_float32(f32_sbit |
10980 (r64_exp & 0xff) << 23 |
10981 extract64(r64_frac, 29, 24));
10982 }
10983
10984 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
10985 {
10986 float_status *fpst = fpstp;
10987 float64 f64 = float64_squash_input_denormal(input, fpst);
10988 uint64_t f64_val = float64_val(f64);
10989 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
10990 int64_t f64_exp = extract64(f64_val, 52, 11);
10991 float64 r64;
10992 uint64_t r64_val;
10993 int64_t r64_exp;
10994 uint64_t r64_frac;
10995
10996 /* Deal with any special cases */
10997 if (float64_is_any_nan(f64)) {
10998 float64 nan = f64;
10999 if (float64_is_signaling_nan(f64, fpst)) {
11000 float_raise(float_flag_invalid, fpst);
11001 nan = float64_maybe_silence_nan(f64, fpst);
11002 }
11003 if (fpst->default_nan_mode) {
11004 nan = float64_default_nan(fpst);
11005 }
11006 return nan;
11007 } else if (float64_is_infinity(f64)) {
11008 return float64_set_sign(float64_zero, float64_is_neg(f64));
11009 } else if (float64_is_zero(f64)) {
11010 float_raise(float_flag_divbyzero, fpst);
11011 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11012 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11013 /* Abs(value) < 2.0^-1024 */
11014 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11015 if (round_to_inf(fpst, f64_sbit)) {
11016 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11017 } else {
11018 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
11019 }
11020 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
11021 float_raise(float_flag_underflow, fpst);
11022 return float64_set_sign(float64_zero, float64_is_neg(f64));
11023 }
11024
11025 r64 = call_recip_estimate(f64, 2045, fpst);
11026 r64_val = float64_val(r64);
11027 r64_exp = extract64(r64_val, 52, 11);
11028 r64_frac = extract64(r64_val, 0, 52);
11029
11030 /* result = sign : result_exp<10:0> : fraction<51:0> */
11031 return make_float64(f64_sbit |
11032 ((r64_exp & 0x7ff) << 52) |
11033 r64_frac);
11034 }
11035
11036 /* The algorithm that must be used to calculate the estimate
11037 * is specified by the ARM ARM.
11038 */
11039 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
11040 {
11041 /* These calculations mustn't set any fp exception flags,
11042 * so we use a local copy of the fp_status.
11043 */
11044 float_status dummy_status = *real_fp_status;
11045 float_status *s = &dummy_status;
11046 float64 q;
11047 int64_t q_int;
11048
11049 if (float64_lt(a, float64_half, s)) {
11050 /* range 0.25 <= a < 0.5 */
11051
11052 /* a in units of 1/512 rounded down */
11053 /* q0 = (int)(a * 512.0); */
11054 q = float64_mul(float64_512, a, s);
11055 q_int = float64_to_int64_round_to_zero(q, s);
11056
11057 /* reciprocal root r */
11058 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
11059 q = int64_to_float64(q_int, s);
11060 q = float64_add(q, float64_half, s);
11061 q = float64_div(q, float64_512, s);
11062 q = float64_sqrt(q, s);
11063 q = float64_div(float64_one, q, s);
11064 } else {
11065 /* range 0.5 <= a < 1.0 */
11066
11067 /* a in units of 1/256 rounded down */
11068 /* q1 = (int)(a * 256.0); */
11069 q = float64_mul(float64_256, a, s);
11070 int64_t q_int = float64_to_int64_round_to_zero(q, s);
11071
11072 /* reciprocal root r */
11073 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
11074 q = int64_to_float64(q_int, s);
11075 q = float64_add(q, float64_half, s);
11076 q = float64_div(q, float64_256, s);
11077 q = float64_sqrt(q, s);
11078 q = float64_div(float64_one, q, s);
11079 }
11080 /* r in units of 1/256 rounded to nearest */
11081 /* s = (int)(256.0 * r + 0.5); */
11082
11083 q = float64_mul(q, float64_256,s );
11084 q = float64_add(q, float64_half, s);
11085 q_int = float64_to_int64_round_to_zero(q, s);
11086
11087 /* return (double)s / 256.0;*/
11088 return float64_div(int64_to_float64(q_int, s), float64_256, s);
11089 }
11090
11091 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
11092 {
11093 float_status *s = fpstp;
11094 float32 f32 = float32_squash_input_denormal(input, s);
11095 uint32_t val = float32_val(f32);
11096 uint32_t f32_sbit = 0x80000000 & val;
11097 int32_t f32_exp = extract32(val, 23, 8);
11098 uint32_t f32_frac = extract32(val, 0, 23);
11099 uint64_t f64_frac;
11100 uint64_t val64;
11101 int result_exp;
11102 float64 f64;
11103
11104 if (float32_is_any_nan(f32)) {
11105 float32 nan = f32;
11106 if (float32_is_signaling_nan(f32, s)) {
11107 float_raise(float_flag_invalid, s);
11108 nan = float32_maybe_silence_nan(f32, s);
11109 }
11110 if (s->default_nan_mode) {
11111 nan = float32_default_nan(s);
11112 }
11113 return nan;
11114 } else if (float32_is_zero(f32)) {
11115 float_raise(float_flag_divbyzero, s);
11116 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11117 } else if (float32_is_neg(f32)) {
11118 float_raise(float_flag_invalid, s);
11119 return float32_default_nan(s);
11120 } else if (float32_is_infinity(f32)) {
11121 return float32_zero;
11122 }
11123
11124 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11125 * preserving the parity of the exponent. */
11126
11127 f64_frac = ((uint64_t) f32_frac) << 29;
11128 if (f32_exp == 0) {
11129 while (extract64(f64_frac, 51, 1) == 0) {
11130 f64_frac = f64_frac << 1;
11131 f32_exp = f32_exp-1;
11132 }
11133 f64_frac = extract64(f64_frac, 0, 51) << 1;
11134 }
11135
11136 if (extract64(f32_exp, 0, 1) == 0) {
11137 f64 = make_float64(((uint64_t) f32_sbit) << 32
11138 | (0x3feULL << 52)
11139 | f64_frac);
11140 } else {
11141 f64 = make_float64(((uint64_t) f32_sbit) << 32
11142 | (0x3fdULL << 52)
11143 | f64_frac);
11144 }
11145
11146 result_exp = (380 - f32_exp) / 2;
11147
11148 f64 = recip_sqrt_estimate(f64, s);
11149
11150 val64 = float64_val(f64);
11151
11152 val = ((result_exp & 0xff) << 23)
11153 | ((val64 >> 29) & 0x7fffff);
11154 return make_float32(val);
11155 }
11156
11157 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
11158 {
11159 float_status *s = fpstp;
11160 float64 f64 = float64_squash_input_denormal(input, s);
11161 uint64_t val = float64_val(f64);
11162 uint64_t f64_sbit = 0x8000000000000000ULL & val;
11163 int64_t f64_exp = extract64(val, 52, 11);
11164 uint64_t f64_frac = extract64(val, 0, 52);
11165 int64_t result_exp;
11166 uint64_t result_frac;
11167
11168 if (float64_is_any_nan(f64)) {
11169 float64 nan = f64;
11170 if (float64_is_signaling_nan(f64, s)) {
11171 float_raise(float_flag_invalid, s);
11172 nan = float64_maybe_silence_nan(f64, s);
11173 }
11174 if (s->default_nan_mode) {
11175 nan = float64_default_nan(s);
11176 }
11177 return nan;
11178 } else if (float64_is_zero(f64)) {
11179 float_raise(float_flag_divbyzero, s);
11180 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11181 } else if (float64_is_neg(f64)) {
11182 float_raise(float_flag_invalid, s);
11183 return float64_default_nan(s);
11184 } else if (float64_is_infinity(f64)) {
11185 return float64_zero;
11186 }
11187
11188 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
11189 * preserving the parity of the exponent. */
11190
11191 if (f64_exp == 0) {
11192 while (extract64(f64_frac, 51, 1) == 0) {
11193 f64_frac = f64_frac << 1;
11194 f64_exp = f64_exp - 1;
11195 }
11196 f64_frac = extract64(f64_frac, 0, 51) << 1;
11197 }
11198
11199 if (extract64(f64_exp, 0, 1) == 0) {
11200 f64 = make_float64(f64_sbit
11201 | (0x3feULL << 52)
11202 | f64_frac);
11203 } else {
11204 f64 = make_float64(f64_sbit
11205 | (0x3fdULL << 52)
11206 | f64_frac);
11207 }
11208
11209 result_exp = (3068 - f64_exp) / 2;
11210
11211 f64 = recip_sqrt_estimate(f64, s);
11212
11213 result_frac = extract64(float64_val(f64), 0, 52);
11214
11215 return make_float64(f64_sbit |
11216 ((result_exp & 0x7ff) << 52) |
11217 result_frac);
11218 }
11219
11220 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
11221 {
11222 float_status *s = fpstp;
11223 float64 f64;
11224
11225 if ((a & 0x80000000) == 0) {
11226 return 0xffffffff;
11227 }
11228
11229 f64 = make_float64((0x3feULL << 52)
11230 | ((int64_t)(a & 0x7fffffff) << 21));
11231
11232 f64 = recip_estimate(f64, s);
11233
11234 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
11235 }
11236
11237 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
11238 {
11239 float_status *fpst = fpstp;
11240 float64 f64;
11241
11242 if ((a & 0xc0000000) == 0) {
11243 return 0xffffffff;
11244 }
11245
11246 if (a & 0x80000000) {
11247 f64 = make_float64((0x3feULL << 52)
11248 | ((uint64_t)(a & 0x7fffffff) << 21));
11249 } else { /* bits 31-30 == '01' */
11250 f64 = make_float64((0x3fdULL << 52)
11251 | ((uint64_t)(a & 0x3fffffff) << 22));
11252 }
11253
11254 f64 = recip_sqrt_estimate(f64, fpst);
11255
11256 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
11257 }
11258
11259 /* VFPv4 fused multiply-accumulate */
11260 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11261 {
11262 float_status *fpst = fpstp;
11263 return float32_muladd(a, b, c, 0, fpst);
11264 }
11265
11266 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11267 {
11268 float_status *fpst = fpstp;
11269 return float64_muladd(a, b, c, 0, fpst);
11270 }
11271
11272 /* ARMv8 round to integral */
11273 float32 HELPER(rints_exact)(float32 x, void *fp_status)
11274 {
11275 return float32_round_to_int(x, fp_status);
11276 }
11277
11278 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11279 {
11280 return float64_round_to_int(x, fp_status);
11281 }
11282
11283 float32 HELPER(rints)(float32 x, void *fp_status)
11284 {
11285 int old_flags = get_float_exception_flags(fp_status), new_flags;
11286 float32 ret;
11287
11288 ret = float32_round_to_int(x, fp_status);
11289
11290 /* Suppress any inexact exceptions the conversion produced */
11291 if (!(old_flags & float_flag_inexact)) {
11292 new_flags = get_float_exception_flags(fp_status);
11293 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11294 }
11295
11296 return ret;
11297 }
11298
11299 float64 HELPER(rintd)(float64 x, void *fp_status)
11300 {
11301 int old_flags = get_float_exception_flags(fp_status), new_flags;
11302 float64 ret;
11303
11304 ret = float64_round_to_int(x, fp_status);
11305
11306 new_flags = get_float_exception_flags(fp_status);
11307
11308 /* Suppress any inexact exceptions the conversion produced */
11309 if (!(old_flags & float_flag_inexact)) {
11310 new_flags = get_float_exception_flags(fp_status);
11311 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11312 }
11313
11314 return ret;
11315 }
11316
11317 /* Convert ARM rounding mode to softfloat */
11318 int arm_rmode_to_sf(int rmode)
11319 {
11320 switch (rmode) {
11321 case FPROUNDING_TIEAWAY:
11322 rmode = float_round_ties_away;
11323 break;
11324 case FPROUNDING_ODD:
11325 /* FIXME: add support for TIEAWAY and ODD */
11326 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
11327 rmode);
11328 case FPROUNDING_TIEEVEN:
11329 default:
11330 rmode = float_round_nearest_even;
11331 break;
11332 case FPROUNDING_POSINF:
11333 rmode = float_round_up;
11334 break;
11335 case FPROUNDING_NEGINF:
11336 rmode = float_round_down;
11337 break;
11338 case FPROUNDING_ZERO:
11339 rmode = float_round_to_zero;
11340 break;
11341 }
11342 return rmode;
11343 }
11344
11345 /* CRC helpers.
11346 * The upper bytes of val (above the number specified by 'bytes') must have
11347 * been zeroed out by the caller.
11348 */
11349 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11350 {
11351 uint8_t buf[4];
11352
11353 stl_le_p(buf, val);
11354
11355 /* zlib crc32 converts the accumulator and output to one's complement. */
11356 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11357 }
11358
11359 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11360 {
11361 uint8_t buf[4];
11362
11363 stl_le_p(buf, val);
11364
11365 /* Linux crc32c converts the output to one's complement. */
11366 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11367 }