]> Git Repo - qemu.git/blame - target/arm/helper.c
target/arm: Implement BLXNS
[qemu.git] / target / arm / helper.c
CommitLineData
74c21bd0 1#include "qemu/osdep.h"
194cbc49 2#include "trace.h"
b5ff1b31 3#include "cpu.h"
ccd38087 4#include "internals.h"
022c62cb 5#include "exec/gdbstub.h"
2ef6175a 6#include "exec/helper-proto.h"
1de7afc9 7#include "qemu/host-utils.h"
78027bb6 8#include "sysemu/arch_init.h"
9c17d615 9#include "sysemu/sysemu.h"
1de7afc9 10#include "qemu/bitops.h"
eb0ecd5a 11#include "qemu/crc32c.h"
63c91552 12#include "exec/exec-all.h"
f08b6170 13#include "exec/cpu_ldst.h"
1d854765 14#include "arm_ldst.h"
eb0ecd5a 15#include <zlib.h> /* For crc32 */
cfe67cef 16#include "exec/semihost.h"
f3a9b694 17#include "sysemu/kvm.h"
0b03bdfc 18
352c98e5
LV
19#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
20
4a501606 21#ifndef CONFIG_USER_ONLY
af51f566 22static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 23 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 24 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
25 target_ulong *page_size, uint32_t *fsr,
26 ARMMMUFaultInfo *fi);
7c2cb42b 27
37785977 28static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 29 MMUAccessType access_type, ARMMMUIdx mmu_idx,
37785977
EI
30 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
31 target_ulong *page_size_ptr, uint32_t *fsr,
32 ARMMMUFaultInfo *fi);
33
35337cc3
PM
34/* Security attributes for an address, as returned by v8m_security_lookup. */
35typedef struct V8M_SAttributes {
36 bool ns;
37 bool nsc;
38 uint8_t sregion;
39 bool srvalid;
40 uint8_t iregion;
41 bool irvalid;
42} V8M_SAttributes;
43
333e10c5
PM
44static void v8m_security_lookup(CPUARMState *env, uint32_t address,
45 MMUAccessType access_type, ARMMMUIdx mmu_idx,
46 V8M_SAttributes *sattrs);
47
7c2cb42b
AF
48/* Definitions for the PMCCNTR and PMCR registers */
49#define PMCRD 0x8
50#define PMCRC 0x4
51#define PMCRE 0x1
4a501606
PM
52#endif
53
0ecb72a5 54static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
55{
56 int nregs;
57
58 /* VFP data registers are always little-endian. */
59 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
60 if (reg < nregs) {
61 stfq_le_p(buf, env->vfp.regs[reg]);
62 return 8;
63 }
64 if (arm_feature(env, ARM_FEATURE_NEON)) {
65 /* Aliases for Q regs. */
66 nregs += 16;
67 if (reg < nregs) {
68 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
69 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
70 return 16;
71 }
72 }
73 switch (reg - nregs) {
74 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
75 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
76 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
77 }
78 return 0;
79}
80
0ecb72a5 81static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
82{
83 int nregs;
84
85 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
86 if (reg < nregs) {
87 env->vfp.regs[reg] = ldfq_le_p(buf);
88 return 8;
89 }
90 if (arm_feature(env, ARM_FEATURE_NEON)) {
91 nregs += 16;
92 if (reg < nregs) {
93 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
94 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
95 return 16;
96 }
97 }
98 switch (reg - nregs) {
99 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
100 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 101 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
102 }
103 return 0;
104}
105
6a669427
PM
106static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
107{
108 switch (reg) {
109 case 0 ... 31:
110 /* 128 bit FP register */
111 stfq_le_p(buf, env->vfp.regs[reg * 2]);
112 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
113 return 16;
114 case 32:
115 /* FPSR */
116 stl_p(buf, vfp_get_fpsr(env));
117 return 4;
118 case 33:
119 /* FPCR */
120 stl_p(buf, vfp_get_fpcr(env));
121 return 4;
122 default:
123 return 0;
124 }
125}
126
127static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
128{
129 switch (reg) {
130 case 0 ... 31:
131 /* 128 bit FP register */
132 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
133 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
134 return 16;
135 case 32:
136 /* FPSR */
137 vfp_set_fpsr(env, ldl_p(buf));
138 return 4;
139 case 33:
140 /* FPCR */
141 vfp_set_fpcr(env, ldl_p(buf));
142 return 4;
143 default:
144 return 0;
145 }
146}
147
c4241c7d 148static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 149{
375421cc 150 assert(ri->fieldoffset);
67ed771d 151 if (cpreg_field_is_64bit(ri)) {
c4241c7d 152 return CPREG_FIELD64(env, ri);
22d9e1a9 153 } else {
c4241c7d 154 return CPREG_FIELD32(env, ri);
22d9e1a9 155 }
d4e6df63
PM
156}
157
c4241c7d
PM
158static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
159 uint64_t value)
d4e6df63 160{
375421cc 161 assert(ri->fieldoffset);
67ed771d 162 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
163 CPREG_FIELD64(env, ri) = value;
164 } else {
165 CPREG_FIELD32(env, ri) = value;
166 }
d4e6df63
PM
167}
168
11f136ee
FA
169static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
170{
171 return (char *)env + ri->fieldoffset;
172}
173
49a66191 174uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 175{
59a1c327 176 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 177 if (ri->type & ARM_CP_CONST) {
59a1c327 178 return ri->resetvalue;
721fae12 179 } else if (ri->raw_readfn) {
59a1c327 180 return ri->raw_readfn(env, ri);
721fae12 181 } else if (ri->readfn) {
59a1c327 182 return ri->readfn(env, ri);
721fae12 183 } else {
59a1c327 184 return raw_read(env, ri);
721fae12 185 }
721fae12
PM
186}
187
59a1c327 188static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 189 uint64_t v)
721fae12
PM
190{
191 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
192 * Note that constant registers are treated as write-ignored; the
193 * caller should check for success by whether a readback gives the
194 * value written.
195 */
196 if (ri->type & ARM_CP_CONST) {
59a1c327 197 return;
721fae12 198 } else if (ri->raw_writefn) {
c4241c7d 199 ri->raw_writefn(env, ri, v);
721fae12 200 } else if (ri->writefn) {
c4241c7d 201 ri->writefn(env, ri, v);
721fae12 202 } else {
afb2530f 203 raw_write(env, ri, v);
721fae12 204 }
721fae12
PM
205}
206
375421cc
PM
207static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
208{
209 /* Return true if the regdef would cause an assertion if you called
210 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
211 * program bug for it not to have the NO_RAW flag).
212 * NB that returning false here doesn't necessarily mean that calling
213 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
214 * read/write access functions which are safe for raw use" from "has
215 * read/write access functions which have side effects but has forgotten
216 * to provide raw access functions".
217 * The tests here line up with the conditions in read/write_raw_cp_reg()
218 * and assertions in raw_read()/raw_write().
219 */
220 if ((ri->type & ARM_CP_CONST) ||
221 ri->fieldoffset ||
222 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
223 return false;
224 }
225 return true;
226}
227
721fae12
PM
228bool write_cpustate_to_list(ARMCPU *cpu)
229{
230 /* Write the coprocessor state from cpu->env to the (index,value) list. */
231 int i;
232 bool ok = true;
233
234 for (i = 0; i < cpu->cpreg_array_len; i++) {
235 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
236 const ARMCPRegInfo *ri;
59a1c327 237
60322b39 238 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
239 if (!ri) {
240 ok = false;
241 continue;
242 }
7a0e58fa 243 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
244 continue;
245 }
59a1c327 246 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
247 }
248 return ok;
249}
250
251bool write_list_to_cpustate(ARMCPU *cpu)
252{
253 int i;
254 bool ok = true;
255
256 for (i = 0; i < cpu->cpreg_array_len; i++) {
257 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
258 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
259 const ARMCPRegInfo *ri;
260
60322b39 261 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
262 if (!ri) {
263 ok = false;
264 continue;
265 }
7a0e58fa 266 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
267 continue;
268 }
269 /* Write value and confirm it reads back as written
270 * (to catch read-only registers and partially read-only
271 * registers where the incoming migration value doesn't match)
272 */
59a1c327
PM
273 write_raw_cp_reg(&cpu->env, ri, v);
274 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
275 ok = false;
276 }
277 }
278 return ok;
279}
280
281static void add_cpreg_to_list(gpointer key, gpointer opaque)
282{
283 ARMCPU *cpu = opaque;
284 uint64_t regidx;
285 const ARMCPRegInfo *ri;
286
287 regidx = *(uint32_t *)key;
60322b39 288 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 289
7a0e58fa 290 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
291 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
292 /* The value array need not be initialized at this point */
293 cpu->cpreg_array_len++;
294 }
295}
296
297static void count_cpreg(gpointer key, gpointer opaque)
298{
299 ARMCPU *cpu = opaque;
300 uint64_t regidx;
301 const ARMCPRegInfo *ri;
302
303 regidx = *(uint32_t *)key;
60322b39 304 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 305
7a0e58fa 306 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
307 cpu->cpreg_array_len++;
308 }
309}
310
311static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
312{
cbf239b7
AR
313 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
314 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 315
cbf239b7
AR
316 if (aidx > bidx) {
317 return 1;
318 }
319 if (aidx < bidx) {
320 return -1;
321 }
322 return 0;
721fae12
PM
323}
324
325void init_cpreg_list(ARMCPU *cpu)
326{
327 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
328 * Note that we require cpreg_tuples[] to be sorted by key ID.
329 */
57b6d95e 330 GList *keys;
721fae12
PM
331 int arraylen;
332
57b6d95e 333 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
334 keys = g_list_sort(keys, cpreg_key_compare);
335
336 cpu->cpreg_array_len = 0;
337
338 g_list_foreach(keys, count_cpreg, cpu);
339
340 arraylen = cpu->cpreg_array_len;
341 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
342 cpu->cpreg_values = g_new(uint64_t, arraylen);
343 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
344 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
345 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
346 cpu->cpreg_array_len = 0;
347
348 g_list_foreach(keys, add_cpreg_to_list, cpu);
349
350 assert(cpu->cpreg_array_len == arraylen);
351
352 g_list_free(keys);
353}
354
68e9c2fe
EI
355/*
356 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
357 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
358 *
359 * access_el3_aa32ns: Used to check AArch32 register views.
360 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
361 */
362static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
363 const ARMCPRegInfo *ri,
364 bool isread)
68e9c2fe
EI
365{
366 bool secure = arm_is_secure_below_el3(env);
367
368 assert(!arm_el_is_aa64(env, 3));
369 if (secure) {
370 return CP_ACCESS_TRAP_UNCATEGORIZED;
371 }
372 return CP_ACCESS_OK;
373}
374
375static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
376 const ARMCPRegInfo *ri,
377 bool isread)
68e9c2fe
EI
378{
379 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 380 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
381 }
382 return CP_ACCESS_OK;
383}
384
5513c3ab
PM
385/* Some secure-only AArch32 registers trap to EL3 if used from
386 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
387 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
388 * We assume that the .access field is set to PL1_RW.
389 */
390static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
391 const ARMCPRegInfo *ri,
392 bool isread)
5513c3ab
PM
393{
394 if (arm_current_el(env) == 3) {
395 return CP_ACCESS_OK;
396 }
397 if (arm_is_secure_below_el3(env)) {
398 return CP_ACCESS_TRAP_EL3;
399 }
400 /* This will be EL1 NS and EL2 NS, which just UNDEF */
401 return CP_ACCESS_TRAP_UNCATEGORIZED;
402}
403
187f678d
PM
404/* Check for traps to "powerdown debug" registers, which are controlled
405 * by MDCR.TDOSA
406 */
407static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
408 bool isread)
409{
410 int el = arm_current_el(env);
411
412 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
413 && !arm_is_secure_below_el3(env)) {
414 return CP_ACCESS_TRAP_EL2;
415 }
416 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
417 return CP_ACCESS_TRAP_EL3;
418 }
419 return CP_ACCESS_OK;
420}
421
91b0a238
PM
422/* Check for traps to "debug ROM" registers, which are controlled
423 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
424 */
425static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
426 bool isread)
427{
428 int el = arm_current_el(env);
429
430 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
431 && !arm_is_secure_below_el3(env)) {
432 return CP_ACCESS_TRAP_EL2;
433 }
434 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
435 return CP_ACCESS_TRAP_EL3;
436 }
437 return CP_ACCESS_OK;
438}
439
d6c8cf81
PM
440/* Check for traps to general debug registers, which are controlled
441 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
442 */
443static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
444 bool isread)
445{
446 int el = arm_current_el(env);
447
448 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
449 && !arm_is_secure_below_el3(env)) {
450 return CP_ACCESS_TRAP_EL2;
451 }
452 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
453 return CP_ACCESS_TRAP_EL3;
454 }
455 return CP_ACCESS_OK;
456}
457
1fce1ba9
PM
458/* Check for traps to performance monitor registers, which are controlled
459 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
460 */
461static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
462 bool isread)
463{
464 int el = arm_current_el(env);
465
466 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
467 && !arm_is_secure_below_el3(env)) {
468 return CP_ACCESS_TRAP_EL2;
469 }
470 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
471 return CP_ACCESS_TRAP_EL3;
472 }
473 return CP_ACCESS_OK;
474}
475
c4241c7d 476static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 477{
00c8cb0a
AF
478 ARMCPU *cpu = arm_env_get_cpu(env);
479
8d5c773e 480 raw_write(env, ri, value);
d10eb08f 481 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
482}
483
c4241c7d 484static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 485{
00c8cb0a
AF
486 ARMCPU *cpu = arm_env_get_cpu(env);
487
8d5c773e 488 if (raw_read(env, ri) != value) {
08de207b
PM
489 /* Unlike real hardware the qemu TLB uses virtual addresses,
490 * not modified virtual addresses, so this causes a TLB flush.
491 */
d10eb08f 492 tlb_flush(CPU(cpu));
8d5c773e 493 raw_write(env, ri, value);
08de207b 494 }
08de207b 495}
c4241c7d
PM
496
497static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
498 uint64_t value)
08de207b 499{
00c8cb0a
AF
500 ARMCPU *cpu = arm_env_get_cpu(env);
501
452a0955 502 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 503 && !extended_addresses_enabled(env)) {
08de207b
PM
504 /* For VMSA (when not using the LPAE long descriptor page table
505 * format) this register includes the ASID, so do a TLB flush.
506 * For PMSA it is purely a process ID and no action is needed.
507 */
d10eb08f 508 tlb_flush(CPU(cpu));
08de207b 509 }
8d5c773e 510 raw_write(env, ri, value);
08de207b
PM
511}
512
c4241c7d
PM
513static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
514 uint64_t value)
d929823f
PM
515{
516 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
517 ARMCPU *cpu = arm_env_get_cpu(env);
518
d10eb08f 519 tlb_flush(CPU(cpu));
d929823f
PM
520}
521
c4241c7d
PM
522static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
523 uint64_t value)
d929823f
PM
524{
525 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
526 ARMCPU *cpu = arm_env_get_cpu(env);
527
528 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
529}
530
c4241c7d
PM
531static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
532 uint64_t value)
d929823f
PM
533{
534 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
535 ARMCPU *cpu = arm_env_get_cpu(env);
536
d10eb08f 537 tlb_flush(CPU(cpu));
d929823f
PM
538}
539
c4241c7d
PM
540static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
541 uint64_t value)
d929823f
PM
542{
543 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
544 ARMCPU *cpu = arm_env_get_cpu(env);
545
546 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
547}
548
fa439fc5
PM
549/* IS variants of TLB operations must affect all cores */
550static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
551 uint64_t value)
552{
a67cf277 553 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 554
a67cf277 555 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
556}
557
558static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
559 uint64_t value)
560{
a67cf277 561 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 562
a67cf277 563 tlb_flush_all_cpus_synced(cs);
fa439fc5
PM
564}
565
566static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
567 uint64_t value)
568{
a67cf277 569 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 570
a67cf277 571 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
572}
573
574static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
575 uint64_t value)
576{
a67cf277 577 CPUState *cs = ENV_GET_CPU(env);
fa439fc5 578
a67cf277 579 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
fa439fc5
PM
580}
581
541ef8c2
SS
582static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
583 uint64_t value)
584{
585 CPUState *cs = ENV_GET_CPU(env);
586
0336cbf8 587 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
588 ARMMMUIdxBit_S12NSE1 |
589 ARMMMUIdxBit_S12NSE0 |
590 ARMMMUIdxBit_S2NS);
541ef8c2
SS
591}
592
593static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
594 uint64_t value)
595{
a67cf277 596 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 597
a67cf277 598 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
599 ARMMMUIdxBit_S12NSE1 |
600 ARMMMUIdxBit_S12NSE0 |
601 ARMMMUIdxBit_S2NS);
541ef8c2
SS
602}
603
604static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
605 uint64_t value)
606{
607 /* Invalidate by IPA. This has to invalidate any structures that
608 * contain only stage 2 translation information, but does not need
609 * to apply to structures that contain combined stage 1 and stage 2
610 * translation information.
611 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
612 */
613 CPUState *cs = ENV_GET_CPU(env);
614 uint64_t pageaddr;
615
616 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
617 return;
618 }
619
620 pageaddr = sextract64(value << 12, 0, 40);
621
8bd5c820 622 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
541ef8c2
SS
623}
624
625static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
626 uint64_t value)
627{
a67cf277 628 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
629 uint64_t pageaddr;
630
631 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
632 return;
633 }
634
635 pageaddr = sextract64(value << 12, 0, 40);
636
a67cf277 637 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 638 ARMMMUIdxBit_S2NS);
541ef8c2
SS
639}
640
641static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
642 uint64_t value)
643{
644 CPUState *cs = ENV_GET_CPU(env);
645
8bd5c820 646 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
647}
648
649static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
650 uint64_t value)
651{
a67cf277 652 CPUState *cs = ENV_GET_CPU(env);
541ef8c2 653
8bd5c820 654 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
541ef8c2
SS
655}
656
657static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
658 uint64_t value)
659{
660 CPUState *cs = ENV_GET_CPU(env);
661 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
662
8bd5c820 663 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
541ef8c2
SS
664}
665
666static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
667 uint64_t value)
668{
a67cf277 669 CPUState *cs = ENV_GET_CPU(env);
541ef8c2
SS
670 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
671
a67cf277 672 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 673 ARMMMUIdxBit_S1E2);
541ef8c2
SS
674}
675
e9aa6c21 676static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
677 /* Define the secure and non-secure FCSE identifier CP registers
678 * separately because there is no secure bank in V8 (no _EL3). This allows
679 * the secure register to be properly reset and migrated. There is also no
680 * v8 EL1 version of the register so the non-secure instance stands alone.
681 */
682 { .name = "FCSEIDR(NS)",
683 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
684 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
685 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
686 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
687 { .name = "FCSEIDR(S)",
688 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
689 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
690 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 691 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
692 /* Define the secure and non-secure context identifier CP registers
693 * separately because there is no secure bank in V8 (no _EL3). This allows
694 * the secure register to be properly reset and migrated. In the
695 * non-secure case, the 32-bit register will have reset and migration
696 * disabled during registration as it is handled by the 64-bit instance.
697 */
698 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 699 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
700 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
701 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
702 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
703 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
704 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
705 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
706 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 707 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
708 REGINFO_SENTINEL
709};
710
711static const ARMCPRegInfo not_v8_cp_reginfo[] = {
712 /* NB: Some of these registers exist in v8 but with more precise
713 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
714 */
715 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
716 { .name = "DACR",
717 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
718 .access = PL1_RW, .resetvalue = 0,
719 .writefn = dacr_write, .raw_writefn = raw_write,
720 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
721 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
722 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
723 * For v6 and v5, these mappings are overly broad.
4fdd17dd 724 */
a903c449
EI
725 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
726 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
727 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
728 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
729 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
730 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
731 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 732 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
733 /* Cache maintenance ops; some of this space may be overridden later. */
734 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
735 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
736 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
737 REGINFO_SENTINEL
738};
739
7d57f408
PM
740static const ARMCPRegInfo not_v6_cp_reginfo[] = {
741 /* Not all pre-v6 cores implemented this WFI, so this is slightly
742 * over-broad.
743 */
744 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
745 .access = PL1_W, .type = ARM_CP_WFI },
746 REGINFO_SENTINEL
747};
748
749static const ARMCPRegInfo not_v7_cp_reginfo[] = {
750 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
751 * is UNPREDICTABLE; we choose to NOP as most implementations do).
752 */
753 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
754 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
755 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
756 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
757 * OMAPCP will override this space.
758 */
759 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
760 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
761 .resetvalue = 0 },
762 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
763 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
764 .resetvalue = 0 },
776d4e5c
PM
765 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
766 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 767 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 768 .resetvalue = 0 },
50300698
PM
769 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
770 * implementing it as RAZ means the "debug architecture version" bits
771 * will read as a reserved value, which should cause Linux to not try
772 * to use the debug hardware.
773 */
774 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
775 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
776 /* MMU TLB control. Note that the wildcarding means we cover not just
777 * the unified TLB ops but also the dside/iside/inner-shareable variants.
778 */
779 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
780 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 781 .type = ARM_CP_NO_RAW },
995939a6
PM
782 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
783 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 784 .type = ARM_CP_NO_RAW },
995939a6
PM
785 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
786 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 787 .type = ARM_CP_NO_RAW },
995939a6
PM
788 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
789 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 790 .type = ARM_CP_NO_RAW },
a903c449
EI
791 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
792 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
793 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
794 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
795 REGINFO_SENTINEL
796};
797
c4241c7d
PM
798static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
799 uint64_t value)
2771db27 800{
f0aff255
FA
801 uint32_t mask = 0;
802
803 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
804 if (!arm_feature(env, ARM_FEATURE_V8)) {
805 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
806 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
807 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
808 */
809 if (arm_feature(env, ARM_FEATURE_VFP)) {
810 /* VFP coprocessor: cp10 & cp11 [23:20] */
811 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
812
813 if (!arm_feature(env, ARM_FEATURE_NEON)) {
814 /* ASEDIS [31] bit is RAO/WI */
815 value |= (1 << 31);
816 }
817
818 /* VFPv3 and upwards with NEON implement 32 double precision
819 * registers (D0-D31).
820 */
821 if (!arm_feature(env, ARM_FEATURE_NEON) ||
822 !arm_feature(env, ARM_FEATURE_VFP3)) {
823 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
824 value |= (1 << 30);
825 }
826 }
827 value &= mask;
2771db27 828 }
7ebd5f2e 829 env->cp15.cpacr_el1 = value;
2771db27
PM
830}
831
3f208fd7
PM
832static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
833 bool isread)
c6f19164
GB
834{
835 if (arm_feature(env, ARM_FEATURE_V8)) {
836 /* Check if CPACR accesses are to be trapped to EL2 */
837 if (arm_current_el(env) == 1 &&
838 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
839 return CP_ACCESS_TRAP_EL2;
840 /* Check if CPACR accesses are to be trapped to EL3 */
841 } else if (arm_current_el(env) < 3 &&
842 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
843 return CP_ACCESS_TRAP_EL3;
844 }
845 }
846
847 return CP_ACCESS_OK;
848}
849
3f208fd7
PM
850static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
851 bool isread)
c6f19164
GB
852{
853 /* Check if CPTR accesses are set to trap to EL3 */
854 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
855 return CP_ACCESS_TRAP_EL3;
856 }
857
858 return CP_ACCESS_OK;
859}
860
7d57f408
PM
861static const ARMCPRegInfo v6_cp_reginfo[] = {
862 /* prefetch by MVA in v6, NOP in v7 */
863 { .name = "MVA_prefetch",
864 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
865 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
866 /* We need to break the TB after ISB to execute self-modifying code
867 * correctly and also to take any pending interrupts immediately.
868 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
869 */
7d57f408 870 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 871 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 872 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 873 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 874 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 875 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 876 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 877 .access = PL1_RW,
b848ce2b
FA
878 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
879 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
880 .resetvalue = 0, },
881 /* Watchpoint Fault Address Register : should actually only be present
882 * for 1136, 1176, 11MPCore.
883 */
884 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
885 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 886 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 887 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 888 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 889 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
890 REGINFO_SENTINEL
891};
892
3f208fd7
PM
893static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
894 bool isread)
200ac0ef 895{
3b163b01 896 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
897 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
898 * trapping to EL2 or EL3 for other accesses.
200ac0ef 899 */
1fce1ba9
PM
900 int el = arm_current_el(env);
901
6ecd0b6b 902 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 903 return CP_ACCESS_TRAP;
200ac0ef 904 }
1fce1ba9
PM
905 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
906 && !arm_is_secure_below_el3(env)) {
907 return CP_ACCESS_TRAP_EL2;
908 }
909 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
910 return CP_ACCESS_TRAP_EL3;
911 }
912
fcd25206 913 return CP_ACCESS_OK;
200ac0ef
PM
914}
915
6ecd0b6b
AB
916static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
917 const ARMCPRegInfo *ri,
918 bool isread)
919{
920 /* ER: event counter read trap control */
921 if (arm_feature(env, ARM_FEATURE_V8)
922 && arm_current_el(env) == 0
923 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
924 && isread) {
925 return CP_ACCESS_OK;
926 }
927
928 return pmreg_access(env, ri, isread);
929}
930
931static CPAccessResult pmreg_access_swinc(CPUARMState *env,
932 const ARMCPRegInfo *ri,
933 bool isread)
934{
935 /* SW: software increment write trap control */
936 if (arm_feature(env, ARM_FEATURE_V8)
937 && arm_current_el(env) == 0
938 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
939 && !isread) {
940 return CP_ACCESS_OK;
941 }
942
943 return pmreg_access(env, ri, isread);
944}
945
7c2cb42b 946#ifndef CONFIG_USER_ONLY
87124fde 947
6ecd0b6b
AB
948static CPAccessResult pmreg_access_selr(CPUARMState *env,
949 const ARMCPRegInfo *ri,
950 bool isread)
951{
952 /* ER: event counter read trap control */
953 if (arm_feature(env, ARM_FEATURE_V8)
954 && arm_current_el(env) == 0
955 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
956 return CP_ACCESS_OK;
957 }
958
959 return pmreg_access(env, ri, isread);
960}
961
962static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
963 const ARMCPRegInfo *ri,
964 bool isread)
965{
966 /* CR: cycle counter read trap control */
967 if (arm_feature(env, ARM_FEATURE_V8)
968 && arm_current_el(env) == 0
969 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
970 && isread) {
971 return CP_ACCESS_OK;
972 }
973
974 return pmreg_access(env, ri, isread);
975}
976
87124fde
AF
977static inline bool arm_ccnt_enabled(CPUARMState *env)
978{
979 /* This does not support checking PMCCFILTR_EL0 register */
980
981 if (!(env->cp15.c9_pmcr & PMCRE)) {
982 return false;
983 }
984
985 return true;
986}
987
ec7b4ce4
AF
988void pmccntr_sync(CPUARMState *env)
989{
990 uint64_t temp_ticks;
991
352c98e5
LV
992 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
993 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
994
995 if (env->cp15.c9_pmcr & PMCRD) {
996 /* Increment once every 64 processor clock cycles */
997 temp_ticks /= 64;
998 }
999
1000 if (arm_ccnt_enabled(env)) {
1001 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1002 }
1003}
1004
c4241c7d
PM
1005static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1006 uint64_t value)
200ac0ef 1007{
942a155b 1008 pmccntr_sync(env);
7c2cb42b
AF
1009
1010 if (value & PMCRC) {
1011 /* The counter has been reset */
1012 env->cp15.c15_ccnt = 0;
1013 }
1014
200ac0ef
PM
1015 /* only the DP, X, D and E bits are writable */
1016 env->cp15.c9_pmcr &= ~0x39;
1017 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 1018
942a155b 1019 pmccntr_sync(env);
7c2cb42b
AF
1020}
1021
1022static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1023{
c92c0687 1024 uint64_t total_ticks;
7c2cb42b 1025
942a155b 1026 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1027 /* Counter is disabled, do not change value */
1028 return env->cp15.c15_ccnt;
1029 }
1030
352c98e5
LV
1031 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1032 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1033
1034 if (env->cp15.c9_pmcr & PMCRD) {
1035 /* Increment once every 64 processor clock cycles */
1036 total_ticks /= 64;
1037 }
1038 return total_ticks - env->cp15.c15_ccnt;
1039}
1040
6b040780
WH
1041static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1042 uint64_t value)
1043{
1044 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1045 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1046 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1047 * accessed.
1048 */
1049 env->cp15.c9_pmselr = value & 0x1f;
1050}
1051
7c2cb42b
AF
1052static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1053 uint64_t value)
1054{
c92c0687 1055 uint64_t total_ticks;
7c2cb42b 1056
942a155b 1057 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
1058 /* Counter is disabled, set the absolute value */
1059 env->cp15.c15_ccnt = value;
1060 return;
1061 }
1062
352c98e5
LV
1063 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1064 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
1065
1066 if (env->cp15.c9_pmcr & PMCRD) {
1067 /* Increment once every 64 processor clock cycles */
1068 total_ticks /= 64;
1069 }
1070 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 1071}
421c7ebd
PC
1072
1073static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1074 uint64_t value)
1075{
1076 uint64_t cur_val = pmccntr_read(env, NULL);
1077
1078 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1079}
1080
ec7b4ce4
AF
1081#else /* CONFIG_USER_ONLY */
1082
1083void pmccntr_sync(CPUARMState *env)
1084{
1085}
1086
7c2cb42b 1087#endif
200ac0ef 1088
0614601c
AF
1089static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1090 uint64_t value)
1091{
1092 pmccntr_sync(env);
1093 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
1094 pmccntr_sync(env);
1095}
1096
c4241c7d 1097static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1098 uint64_t value)
1099{
200ac0ef
PM
1100 value &= (1 << 31);
1101 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1102}
1103
c4241c7d
PM
1104static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1105 uint64_t value)
200ac0ef 1106{
200ac0ef
PM
1107 value &= (1 << 31);
1108 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1109}
1110
c4241c7d
PM
1111static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1112 uint64_t value)
200ac0ef 1113{
200ac0ef 1114 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
1115}
1116
c4241c7d
PM
1117static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1118 uint64_t value)
200ac0ef 1119{
fdb86656
WH
1120 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1121 * PMSELR value is equal to or greater than the number of implemented
1122 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1123 */
1124 if (env->cp15.c9_pmselr == 0x1f) {
1125 pmccfiltr_write(env, ri, value);
1126 }
1127}
1128
1129static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1130{
1131 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1132 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1133 */
1134 if (env->cp15.c9_pmselr == 0x1f) {
1135 return env->cp15.pmccfiltr_el0;
1136 } else {
1137 return 0;
1138 }
200ac0ef
PM
1139}
1140
c4241c7d 1141static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1142 uint64_t value)
1143{
6ecd0b6b
AB
1144 if (arm_feature(env, ARM_FEATURE_V8)) {
1145 env->cp15.c9_pmuserenr = value & 0xf;
1146 } else {
1147 env->cp15.c9_pmuserenr = value & 1;
1148 }
200ac0ef
PM
1149}
1150
c4241c7d
PM
1151static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1152 uint64_t value)
200ac0ef
PM
1153{
1154 /* We have no event counters so only the C bit can be changed */
1155 value &= (1 << 31);
1156 env->cp15.c9_pminten |= value;
200ac0ef
PM
1157}
1158
c4241c7d
PM
1159static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1160 uint64_t value)
200ac0ef
PM
1161{
1162 value &= (1 << 31);
1163 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
1164}
1165
c4241c7d
PM
1166static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1167 uint64_t value)
8641136c 1168{
a505d7fe
PM
1169 /* Note that even though the AArch64 view of this register has bits
1170 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1171 * architectural requirements for bits which are RES0 only in some
1172 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1173 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1174 */
855ea66d 1175 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1176}
1177
64e0e2de
EI
1178static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1179{
1180 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1181 * For bits that vary between AArch32/64, code needs to check the
1182 * current execution mode before directly using the feature bit.
1183 */
1184 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1185
1186 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1187 valid_mask &= ~SCR_HCE;
1188
1189 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1190 * supported if EL2 exists. The bit is UNK/SBZP when
1191 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1192 * when EL2 is unavailable.
4eb27640 1193 * On ARMv8, this bit is always available.
64e0e2de 1194 */
4eb27640
GB
1195 if (arm_feature(env, ARM_FEATURE_V7) &&
1196 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1197 valid_mask &= ~SCR_SMD;
1198 }
1199 }
1200
1201 /* Clear all-context RES0 bits. */
1202 value &= valid_mask;
1203 raw_write(env, ri, value);
1204}
1205
c4241c7d 1206static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1207{
1208 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1209
1210 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1211 * bank
1212 */
1213 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1214 ri->secure & ARM_CP_SECSTATE_S);
1215
1216 return cpu->ccsidr[index];
776d4e5c
PM
1217}
1218
c4241c7d
PM
1219static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1220 uint64_t value)
776d4e5c 1221{
8d5c773e 1222 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1223}
1224
1090b9c6
PM
1225static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1226{
1227 CPUState *cs = ENV_GET_CPU(env);
1228 uint64_t ret = 0;
1229
1230 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1231 ret |= CPSR_I;
1232 }
1233 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1234 ret |= CPSR_F;
1235 }
1236 /* External aborts are not possible in QEMU so A bit is always clear */
1237 return ret;
1238}
1239
e9aa6c21 1240static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1241 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1242 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1243 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1244 /* Performance monitors are implementation defined in v7,
1245 * but with an ARM recommended set of registers, which we
1246 * follow (although we don't actually implement any counters)
1247 *
1248 * Performance registers fall into three categories:
1249 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1250 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1251 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1252 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1253 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1254 */
1255 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1256 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1257 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1258 .writefn = pmcntenset_write,
1259 .accessfn = pmreg_access,
1260 .raw_writefn = raw_write },
8521466b
AF
1261 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1262 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1263 .access = PL0_RW, .accessfn = pmreg_access,
1264 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1265 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1266 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1267 .access = PL0_RW,
1268 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1269 .accessfn = pmreg_access,
1270 .writefn = pmcntenclr_write,
7a0e58fa 1271 .type = ARM_CP_ALIAS },
8521466b
AF
1272 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1273 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1274 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1275 .type = ARM_CP_ALIAS,
8521466b
AF
1276 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1277 .writefn = pmcntenclr_write },
200ac0ef
PM
1278 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1279 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1280 .accessfn = pmreg_access,
1281 .writefn = pmovsr_write,
1282 .raw_writefn = raw_write },
978364f1
AF
1283 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1284 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1285 .access = PL0_RW, .accessfn = pmreg_access,
1286 .type = ARM_CP_ALIAS,
1287 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1288 .writefn = pmovsr_write,
1289 .raw_writefn = raw_write },
fcd25206 1290 /* Unimplemented so WI. */
200ac0ef 1291 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
6ecd0b6b 1292 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
7c2cb42b 1293#ifndef CONFIG_USER_ONLY
6b040780
WH
1294 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1295 .access = PL0_RW, .type = ARM_CP_ALIAS,
1296 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1297 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1298 .raw_writefn = raw_write},
1299 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1300 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1301 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1302 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1303 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1304 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 1305 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 1306 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1307 .accessfn = pmreg_access_ccntr },
8521466b
AF
1308 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1309 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1310 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b
AF
1311 .type = ARM_CP_IO,
1312 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1313#endif
8521466b
AF
1314 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1315 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1316 .writefn = pmccfiltr_write,
8521466b
AF
1317 .access = PL0_RW, .accessfn = pmreg_access,
1318 .type = ARM_CP_IO,
1319 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1320 .resetvalue = 0, },
200ac0ef 1321 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
fdb86656
WH
1322 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1323 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1324 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1325 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1326 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1327 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
fcd25206 1328 /* Unimplemented, RAZ/WI. */
200ac0ef 1329 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206 1330 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
6ecd0b6b 1331 .accessfn = pmreg_access_xevcntr },
200ac0ef 1332 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1333 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1334 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1335 .resetvalue = 0,
d4e6df63 1336 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1337 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1338 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1339 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1340 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1341 .resetvalue = 0,
1342 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1343 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1344 .access = PL1_RW, .accessfn = access_tpm,
e6ec5457
WH
1345 .type = ARM_CP_ALIAS,
1346 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 1347 .resetvalue = 0,
d4e6df63 1348 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
1349 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1350 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1351 .access = PL1_RW, .accessfn = access_tpm,
1352 .type = ARM_CP_IO,
1353 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1354 .writefn = pmintenset_write, .raw_writefn = raw_write,
1355 .resetvalue = 0x0 },
200ac0ef 1356 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1357 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1358 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1359 .writefn = pmintenclr_write, },
978364f1
AF
1360 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1361 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1362 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1363 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1364 .writefn = pmintenclr_write },
7da845b0
PM
1365 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1366 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1367 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1368 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1369 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1370 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1371 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1372 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1373 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1374 * just RAZ for all cores:
1375 */
0ff644a7
PM
1376 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1377 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1378 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1379 /* Auxiliary fault status registers: these also are IMPDEF, and we
1380 * choose to RAZ/WI for all cores.
1381 */
1382 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1383 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1384 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1385 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1386 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1387 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1388 /* MAIR can just read-as-written because we don't implement caches
1389 * and so don't need to care about memory attributes.
1390 */
1391 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1392 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1393 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1394 .resetvalue = 0 },
4cfb8ad8
PM
1395 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1396 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1397 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1398 .resetvalue = 0 },
b0fe2427
PM
1399 /* For non-long-descriptor page tables these are PRRR and NMRR;
1400 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1401 */
1281f8e3 1402 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1403 * allows them to assign the correct fieldoffset based on the endianness
1404 * handled in the field definitions.
1405 */
a903c449 1406 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1407 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1408 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1409 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1410 .resetfn = arm_cp_reset_ignore },
a903c449 1411 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1412 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1413 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1414 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1415 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1416 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1417 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1418 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1419 /* 32 bit ITLB invalidates */
1420 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1421 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1422 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1423 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1424 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1425 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1426 /* 32 bit DTLB invalidates */
1427 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1428 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1429 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1430 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1431 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1432 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1433 /* 32 bit TLB invalidates */
1434 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1435 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1436 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1437 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1438 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1439 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1440 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1441 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1442 REGINFO_SENTINEL
1443};
1444
1445static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1446 /* 32 bit TLB invalidates, Inner Shareable */
1447 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1448 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1449 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1450 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1451 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1452 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1453 .writefn = tlbiasid_is_write },
995939a6 1454 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1455 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1456 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1457 REGINFO_SENTINEL
1458};
1459
c4241c7d
PM
1460static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1461 uint64_t value)
c326b979
PM
1462{
1463 value &= 1;
1464 env->teecr = value;
c326b979
PM
1465}
1466
3f208fd7
PM
1467static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1468 bool isread)
c326b979 1469{
dcbff19b 1470 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1471 return CP_ACCESS_TRAP;
c326b979 1472 }
92611c00 1473 return CP_ACCESS_OK;
c326b979
PM
1474}
1475
1476static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1477 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1478 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1479 .resetvalue = 0,
1480 .writefn = teecr_write },
1481 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1482 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1483 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1484 REGINFO_SENTINEL
1485};
1486
4d31c596 1487static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1488 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1489 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1490 .access = PL0_RW,
54bf36ed 1491 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1492 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1493 .access = PL0_RW,
54bf36ed
FA
1494 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1495 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1496 .resetfn = arm_cp_reset_ignore },
1497 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1498 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1499 .access = PL0_R|PL1_W,
54bf36ed
FA
1500 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1501 .resetvalue = 0},
4d31c596
PM
1502 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1503 .access = PL0_R|PL1_W,
54bf36ed
FA
1504 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1505 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1506 .resetfn = arm_cp_reset_ignore },
54bf36ed 1507 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1508 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1509 .access = PL1_RW,
54bf36ed
FA
1510 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1511 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1512 .access = PL1_RW,
1513 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1514 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1515 .resetvalue = 0 },
4d31c596
PM
1516 REGINFO_SENTINEL
1517};
1518
55d284af
PM
1519#ifndef CONFIG_USER_ONLY
1520
3f208fd7
PM
1521static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1522 bool isread)
00108f2d 1523{
75502672
PM
1524 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1525 * Writable only at the highest implemented exception level.
1526 */
1527 int el = arm_current_el(env);
1528
1529 switch (el) {
1530 case 0:
1531 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1532 return CP_ACCESS_TRAP;
1533 }
1534 break;
1535 case 1:
1536 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1537 arm_is_secure_below_el3(env)) {
1538 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1539 return CP_ACCESS_TRAP_UNCATEGORIZED;
1540 }
1541 break;
1542 case 2:
1543 case 3:
1544 break;
00108f2d 1545 }
75502672
PM
1546
1547 if (!isread && el < arm_highest_el(env)) {
1548 return CP_ACCESS_TRAP_UNCATEGORIZED;
1549 }
1550
00108f2d
PM
1551 return CP_ACCESS_OK;
1552}
1553
3f208fd7
PM
1554static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1555 bool isread)
00108f2d 1556{
0b6440af
EI
1557 unsigned int cur_el = arm_current_el(env);
1558 bool secure = arm_is_secure(env);
1559
00108f2d 1560 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1561 if (cur_el == 0 &&
00108f2d
PM
1562 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1563 return CP_ACCESS_TRAP;
1564 }
0b6440af
EI
1565
1566 if (arm_feature(env, ARM_FEATURE_EL2) &&
1567 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1568 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1569 return CP_ACCESS_TRAP_EL2;
1570 }
00108f2d
PM
1571 return CP_ACCESS_OK;
1572}
1573
3f208fd7
PM
1574static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1575 bool isread)
00108f2d 1576{
0b6440af
EI
1577 unsigned int cur_el = arm_current_el(env);
1578 bool secure = arm_is_secure(env);
1579
00108f2d
PM
1580 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1581 * EL0[PV]TEN is zero.
1582 */
0b6440af 1583 if (cur_el == 0 &&
00108f2d
PM
1584 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1585 return CP_ACCESS_TRAP;
1586 }
0b6440af
EI
1587
1588 if (arm_feature(env, ARM_FEATURE_EL2) &&
1589 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1590 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1591 return CP_ACCESS_TRAP_EL2;
1592 }
00108f2d
PM
1593 return CP_ACCESS_OK;
1594}
1595
1596static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1597 const ARMCPRegInfo *ri,
1598 bool isread)
00108f2d 1599{
3f208fd7 1600 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1601}
1602
1603static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1604 const ARMCPRegInfo *ri,
1605 bool isread)
00108f2d 1606{
3f208fd7 1607 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1608}
1609
3f208fd7
PM
1610static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1611 bool isread)
00108f2d 1612{
3f208fd7 1613 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1614}
1615
3f208fd7
PM
1616static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1617 bool isread)
00108f2d 1618{
3f208fd7 1619 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1620}
1621
b4d3978c 1622static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1623 const ARMCPRegInfo *ri,
1624 bool isread)
b4d3978c
PM
1625{
1626 /* The AArch64 register view of the secure physical timer is
1627 * always accessible from EL3, and configurably accessible from
1628 * Secure EL1.
1629 */
1630 switch (arm_current_el(env)) {
1631 case 1:
1632 if (!arm_is_secure(env)) {
1633 return CP_ACCESS_TRAP;
1634 }
1635 if (!(env->cp15.scr_el3 & SCR_ST)) {
1636 return CP_ACCESS_TRAP_EL3;
1637 }
1638 return CP_ACCESS_OK;
1639 case 0:
1640 case 2:
1641 return CP_ACCESS_TRAP;
1642 case 3:
1643 return CP_ACCESS_OK;
1644 default:
1645 g_assert_not_reached();
1646 }
1647}
1648
55d284af
PM
1649static uint64_t gt_get_countervalue(CPUARMState *env)
1650{
bc72ad67 1651 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1652}
1653
1654static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1655{
1656 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1657
1658 if (gt->ctl & 1) {
1659 /* Timer enabled: calculate and set current ISTATUS, irq, and
1660 * reset timer to when ISTATUS next has to change
1661 */
edac4d8a
EI
1662 uint64_t offset = timeridx == GTIMER_VIRT ?
1663 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1664 uint64_t count = gt_get_countervalue(&cpu->env);
1665 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1666 int istatus = count - offset >= gt->cval;
55d284af 1667 uint64_t nexttick;
194cbc49 1668 int irqstate;
55d284af
PM
1669
1670 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
1671
1672 irqstate = (istatus && !(gt->ctl & 2));
1673 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1674
55d284af
PM
1675 if (istatus) {
1676 /* Next transition is when count rolls back over to zero */
1677 nexttick = UINT64_MAX;
1678 } else {
1679 /* Next transition is when we hit cval */
edac4d8a 1680 nexttick = gt->cval + offset;
55d284af
PM
1681 }
1682 /* Note that the desired next expiry time might be beyond the
1683 * signed-64-bit range of a QEMUTimer -- in this case we just
1684 * set the timer for as far in the future as possible. When the
1685 * timer expires we will reset the timer for any remaining period.
1686 */
1687 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1688 nexttick = INT64_MAX / GTIMER_SCALE;
1689 }
bc72ad67 1690 timer_mod(cpu->gt_timer[timeridx], nexttick);
194cbc49 1691 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
1692 } else {
1693 /* Timer disabled: ISTATUS and timer output always clear */
1694 gt->ctl &= ~4;
1695 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1696 timer_del(cpu->gt_timer[timeridx]);
194cbc49 1697 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
1698 }
1699}
1700
0e3eca4c
EI
1701static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1702 int timeridx)
55d284af
PM
1703{
1704 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1705
bc72ad67 1706 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1707}
1708
c4241c7d 1709static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1710{
c4241c7d 1711 return gt_get_countervalue(env);
55d284af
PM
1712}
1713
edac4d8a
EI
1714static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1715{
1716 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1717}
1718
c4241c7d 1719static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1720 int timeridx,
c4241c7d 1721 uint64_t value)
55d284af 1722{
194cbc49 1723 trace_arm_gt_cval_write(timeridx, value);
55d284af
PM
1724 env->cp15.c14_timer[timeridx].cval = value;
1725 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1726}
c4241c7d 1727
0e3eca4c
EI
1728static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1729 int timeridx)
55d284af 1730{
edac4d8a 1731 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1732
c4241c7d 1733 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1734 (gt_get_countervalue(env) - offset));
55d284af
PM
1735}
1736
c4241c7d 1737static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1738 int timeridx,
c4241c7d 1739 uint64_t value)
55d284af 1740{
edac4d8a 1741 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1742
194cbc49 1743 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 1744 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1745 sextract64(value, 0, 32);
55d284af 1746 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1747}
1748
c4241c7d 1749static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1750 int timeridx,
c4241c7d 1751 uint64_t value)
55d284af
PM
1752{
1753 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1754 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1755
194cbc49 1756 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 1757 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1758 if ((oldval ^ value) & 1) {
1759 /* Enable toggled */
1760 gt_recalc_timer(cpu, timeridx);
d3afacc7 1761 } else if ((oldval ^ value) & 2) {
55d284af
PM
1762 /* IMASK toggled: don't need to recalculate,
1763 * just set the interrupt line based on ISTATUS
1764 */
194cbc49
PM
1765 int irqstate = (oldval & 4) && !(value & 2);
1766
1767 trace_arm_gt_imask_toggle(timeridx, irqstate);
1768 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 1769 }
55d284af
PM
1770}
1771
0e3eca4c
EI
1772static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1773{
1774 gt_timer_reset(env, ri, GTIMER_PHYS);
1775}
1776
1777static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1778 uint64_t value)
1779{
1780 gt_cval_write(env, ri, GTIMER_PHYS, value);
1781}
1782
1783static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1784{
1785 return gt_tval_read(env, ri, GTIMER_PHYS);
1786}
1787
1788static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1789 uint64_t value)
1790{
1791 gt_tval_write(env, ri, GTIMER_PHYS, value);
1792}
1793
1794static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1795 uint64_t value)
1796{
1797 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1798}
1799
1800static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1801{
1802 gt_timer_reset(env, ri, GTIMER_VIRT);
1803}
1804
1805static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1806 uint64_t value)
1807{
1808 gt_cval_write(env, ri, GTIMER_VIRT, value);
1809}
1810
1811static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1812{
1813 return gt_tval_read(env, ri, GTIMER_VIRT);
1814}
1815
1816static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1817 uint64_t value)
1818{
1819 gt_tval_write(env, ri, GTIMER_VIRT, value);
1820}
1821
1822static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1823 uint64_t value)
1824{
1825 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1826}
1827
edac4d8a
EI
1828static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1829 uint64_t value)
1830{
1831 ARMCPU *cpu = arm_env_get_cpu(env);
1832
194cbc49 1833 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
1834 raw_write(env, ri, value);
1835 gt_recalc_timer(cpu, GTIMER_VIRT);
1836}
1837
b0e66d95
EI
1838static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1839{
1840 gt_timer_reset(env, ri, GTIMER_HYP);
1841}
1842
1843static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844 uint64_t value)
1845{
1846 gt_cval_write(env, ri, GTIMER_HYP, value);
1847}
1848
1849static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1850{
1851 return gt_tval_read(env, ri, GTIMER_HYP);
1852}
1853
1854static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1855 uint64_t value)
1856{
1857 gt_tval_write(env, ri, GTIMER_HYP, value);
1858}
1859
1860static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1861 uint64_t value)
1862{
1863 gt_ctl_write(env, ri, GTIMER_HYP, value);
1864}
1865
b4d3978c
PM
1866static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1867{
1868 gt_timer_reset(env, ri, GTIMER_SEC);
1869}
1870
1871static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1872 uint64_t value)
1873{
1874 gt_cval_write(env, ri, GTIMER_SEC, value);
1875}
1876
1877static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1878{
1879 return gt_tval_read(env, ri, GTIMER_SEC);
1880}
1881
1882static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1883 uint64_t value)
1884{
1885 gt_tval_write(env, ri, GTIMER_SEC, value);
1886}
1887
1888static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1889 uint64_t value)
1890{
1891 gt_ctl_write(env, ri, GTIMER_SEC, value);
1892}
1893
55d284af
PM
1894void arm_gt_ptimer_cb(void *opaque)
1895{
1896 ARMCPU *cpu = opaque;
1897
1898 gt_recalc_timer(cpu, GTIMER_PHYS);
1899}
1900
1901void arm_gt_vtimer_cb(void *opaque)
1902{
1903 ARMCPU *cpu = opaque;
1904
1905 gt_recalc_timer(cpu, GTIMER_VIRT);
1906}
1907
b0e66d95
EI
1908void arm_gt_htimer_cb(void *opaque)
1909{
1910 ARMCPU *cpu = opaque;
1911
1912 gt_recalc_timer(cpu, GTIMER_HYP);
1913}
1914
b4d3978c
PM
1915void arm_gt_stimer_cb(void *opaque)
1916{
1917 ARMCPU *cpu = opaque;
1918
1919 gt_recalc_timer(cpu, GTIMER_SEC);
1920}
1921
55d284af
PM
1922static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1923 /* Note that CNTFRQ is purely reads-as-written for the benefit
1924 * of software; writing it doesn't actually change the timer frequency.
1925 * Our reset value matches the fixed frequency we implement the timer at.
1926 */
1927 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1928 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1929 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1930 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1931 },
1932 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1933 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1934 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1935 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1936 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1937 },
1938 /* overall control: mostly access permissions */
a7adc4b7
PM
1939 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1940 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1941 .access = PL1_RW,
1942 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1943 .resetvalue = 0,
1944 },
1945 /* per-timer control */
1946 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1947 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1948 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1949 .accessfn = gt_ptimer_access,
1950 .fieldoffset = offsetoflow32(CPUARMState,
1951 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1952 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1953 },
9ff9dd3c
PM
1954 { .name = "CNTP_CTL(S)",
1955 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1956 .secure = ARM_CP_SECSTATE_S,
1957 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1958 .accessfn = gt_ptimer_access,
1959 .fieldoffset = offsetoflow32(CPUARMState,
1960 cp15.c14_timer[GTIMER_SEC].ctl),
1961 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1962 },
a7adc4b7
PM
1963 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1964 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1965 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1966 .accessfn = gt_ptimer_access,
55d284af
PM
1967 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1968 .resetvalue = 0,
0e3eca4c 1969 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1970 },
1971 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1972 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1973 .accessfn = gt_vtimer_access,
1974 .fieldoffset = offsetoflow32(CPUARMState,
1975 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1976 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1977 },
1978 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1979 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1980 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1981 .accessfn = gt_vtimer_access,
55d284af
PM
1982 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1983 .resetvalue = 0,
0e3eca4c 1984 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1985 },
1986 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1987 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 1988 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1989 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1990 .accessfn = gt_ptimer_access,
0e3eca4c 1991 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 1992 },
9ff9dd3c
PM
1993 { .name = "CNTP_TVAL(S)",
1994 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1995 .secure = ARM_CP_SECSTATE_S,
1996 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1997 .accessfn = gt_ptimer_access,
1998 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1999 },
a7adc4b7
PM
2000 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2001 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 2002 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2003 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2004 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 2005 },
55d284af 2006 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 2007 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 2008 .accessfn = gt_vtimer_access,
0e3eca4c 2009 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 2010 },
a7adc4b7
PM
2011 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2012 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 2013 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
2014 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2015 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 2016 },
55d284af
PM
2017 /* The counter itself */
2018 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2019 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2020 .accessfn = gt_pct_access,
a7adc4b7
PM
2021 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2022 },
2023 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2024 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2025 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2026 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2027 },
2028 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 2029 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2030 .accessfn = gt_vct_access,
edac4d8a 2031 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
2032 },
2033 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2034 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 2035 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2036 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
2037 },
2038 /* Comparison value, indicating when the timer goes off */
2039 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 2040 .secure = ARM_CP_SECSTATE_NS,
55d284af 2041 .access = PL1_RW | PL0_R,
7a0e58fa 2042 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2043 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 2044 .accessfn = gt_ptimer_access,
0e3eca4c 2045 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 2046 },
9ff9dd3c
PM
2047 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
2048 .secure = ARM_CP_SECSTATE_S,
2049 .access = PL1_RW | PL0_R,
2050 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2051 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2052 .accessfn = gt_ptimer_access,
2053 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2054 },
a7adc4b7
PM
2055 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2056 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2057 .access = PL1_RW | PL0_R,
2058 .type = ARM_CP_IO,
2059 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 2060 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 2061 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
2062 },
2063 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2064 .access = PL1_RW | PL0_R,
7a0e58fa 2065 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 2066 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 2067 .accessfn = gt_vtimer_access,
0e3eca4c 2068 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
2069 },
2070 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2071 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2072 .access = PL1_RW | PL0_R,
2073 .type = ARM_CP_IO,
2074 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2075 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 2076 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 2077 },
b4d3978c
PM
2078 /* Secure timer -- this is actually restricted to only EL3
2079 * and configurably Secure-EL1 via the accessfn.
2080 */
2081 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2082 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2083 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2084 .accessfn = gt_stimer_access,
2085 .readfn = gt_sec_tval_read,
2086 .writefn = gt_sec_tval_write,
2087 .resetfn = gt_sec_timer_reset,
2088 },
2089 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2090 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2091 .type = ARM_CP_IO, .access = PL1_RW,
2092 .accessfn = gt_stimer_access,
2093 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2094 .resetvalue = 0,
2095 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2096 },
2097 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2098 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2099 .type = ARM_CP_IO, .access = PL1_RW,
2100 .accessfn = gt_stimer_access,
2101 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2102 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2103 },
55d284af
PM
2104 REGINFO_SENTINEL
2105};
2106
2107#else
2108/* In user-mode none of the generic timer registers are accessible,
bc72ad67 2109 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
2110 * so instead just don't register any of them.
2111 */
6cc7a3ae 2112static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
2113 REGINFO_SENTINEL
2114};
2115
55d284af
PM
2116#endif
2117
c4241c7d 2118static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 2119{
891a2fe7 2120 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 2121 raw_write(env, ri, value);
891a2fe7 2122 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 2123 raw_write(env, ri, value & 0xfffff6ff);
4a501606 2124 } else {
8d5c773e 2125 raw_write(env, ri, value & 0xfffff1ff);
4a501606 2126 }
4a501606
PM
2127}
2128
2129#ifndef CONFIG_USER_ONLY
2130/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 2131
3f208fd7
PM
2132static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2133 bool isread)
92611c00
PM
2134{
2135 if (ri->opc2 & 4) {
87562e4f
PM
2136 /* The ATS12NSO* operations must trap to EL3 if executed in
2137 * Secure EL1 (which can only happen if EL3 is AArch64).
2138 * They are simply UNDEF if executed from NS EL1.
2139 * They function normally from EL2 or EL3.
92611c00 2140 */
87562e4f
PM
2141 if (arm_current_el(env) == 1) {
2142 if (arm_is_secure_below_el3(env)) {
2143 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2144 }
2145 return CP_ACCESS_TRAP_UNCATEGORIZED;
2146 }
92611c00
PM
2147 }
2148 return CP_ACCESS_OK;
2149}
2150
060e8a48 2151static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 2152 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 2153{
a8170e5e 2154 hwaddr phys_addr;
4a501606
PM
2155 target_ulong page_size;
2156 int prot;
b7cc4e82
PC
2157 uint32_t fsr;
2158 bool ret;
01c097f7 2159 uint64_t par64;
8bf5b6a9 2160 MemTxAttrs attrs = {};
e14b5a23 2161 ARMMMUFaultInfo fi = {};
4a501606 2162
d3649702 2163 ret = get_phys_addr(env, value, access_type, mmu_idx,
e14b5a23 2164 &phys_addr, &attrs, &prot, &page_size, &fsr, &fi);
702a9357 2165 if (extended_addresses_enabled(env)) {
b7cc4e82 2166 /* fsr is a DFSR/IFSR value for the long descriptor
702a9357
PM
2167 * translation table format, but with WnR always clear.
2168 * Convert it to a 64-bit PAR.
2169 */
01c097f7 2170 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 2171 if (!ret) {
702a9357 2172 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
2173 if (!attrs.secure) {
2174 par64 |= (1 << 9); /* NS */
2175 }
702a9357 2176 /* We don't set the ATTR or SH fields in the PAR. */
4a501606 2177 } else {
702a9357 2178 par64 |= 1; /* F */
b7cc4e82 2179 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
2180 /* Note that S2WLK and FSTAGE are always zero, because we don't
2181 * implement virtualization and therefore there can't be a stage 2
2182 * fault.
2183 */
4a501606
PM
2184 }
2185 } else {
b7cc4e82 2186 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
2187 * translation table format (with WnR always clear).
2188 * Convert it to a 32-bit PAR.
2189 */
b7cc4e82 2190 if (!ret) {
702a9357
PM
2191 /* We do not set any attribute bits in the PAR */
2192 if (page_size == (1 << 24)
2193 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 2194 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 2195 } else {
01c097f7 2196 par64 = phys_addr & 0xfffff000;
702a9357 2197 }
8bf5b6a9
PM
2198 if (!attrs.secure) {
2199 par64 |= (1 << 9); /* NS */
2200 }
702a9357 2201 } else {
b7cc4e82
PC
2202 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2203 ((fsr & 0xf) << 1) | 1;
702a9357 2204 }
4a501606 2205 }
060e8a48
PM
2206 return par64;
2207}
2208
2209static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2210{
03ae85f8 2211 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 2212 uint64_t par64;
d3649702
PM
2213 ARMMMUIdx mmu_idx;
2214 int el = arm_current_el(env);
2215 bool secure = arm_is_secure_below_el3(env);
060e8a48 2216
d3649702
PM
2217 switch (ri->opc2 & 6) {
2218 case 0:
2219 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2220 switch (el) {
2221 case 3:
2222 mmu_idx = ARMMMUIdx_S1E3;
2223 break;
2224 case 2:
2225 mmu_idx = ARMMMUIdx_S1NSE1;
2226 break;
2227 case 1:
2228 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2229 break;
2230 default:
2231 g_assert_not_reached();
2232 }
2233 break;
2234 case 2:
2235 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2236 switch (el) {
2237 case 3:
2238 mmu_idx = ARMMMUIdx_S1SE0;
2239 break;
2240 case 2:
2241 mmu_idx = ARMMMUIdx_S1NSE0;
2242 break;
2243 case 1:
2244 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2245 break;
2246 default:
2247 g_assert_not_reached();
2248 }
2249 break;
2250 case 4:
2251 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2252 mmu_idx = ARMMMUIdx_S12NSE1;
2253 break;
2254 case 6:
2255 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2256 mmu_idx = ARMMMUIdx_S12NSE0;
2257 break;
2258 default:
2259 g_assert_not_reached();
2260 }
2261
2262 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2263
2264 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2265}
060e8a48 2266
14db7fe0
PM
2267static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2268 uint64_t value)
2269{
03ae85f8 2270 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
2271 uint64_t par64;
2272
2273 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2274
2275 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2276}
2277
3f208fd7
PM
2278static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2279 bool isread)
2a47df95
PM
2280{
2281 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2282 return CP_ACCESS_TRAP;
2283 }
2284 return CP_ACCESS_OK;
2285}
2286
060e8a48
PM
2287static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2288 uint64_t value)
2289{
03ae85f8 2290 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
2291 ARMMMUIdx mmu_idx;
2292 int secure = arm_is_secure_below_el3(env);
2293
2294 switch (ri->opc2 & 6) {
2295 case 0:
2296 switch (ri->opc1) {
2297 case 0: /* AT S1E1R, AT S1E1W */
2298 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2299 break;
2300 case 4: /* AT S1E2R, AT S1E2W */
2301 mmu_idx = ARMMMUIdx_S1E2;
2302 break;
2303 case 6: /* AT S1E3R, AT S1E3W */
2304 mmu_idx = ARMMMUIdx_S1E3;
2305 break;
2306 default:
2307 g_assert_not_reached();
2308 }
2309 break;
2310 case 2: /* AT S1E0R, AT S1E0W */
2311 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2312 break;
2313 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2314 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2315 break;
2316 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2317 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2318 break;
2319 default:
2320 g_assert_not_reached();
2321 }
060e8a48 2322
d3649702 2323 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2324}
4a501606
PM
2325#endif
2326
2327static const ARMCPRegInfo vapa_cp_reginfo[] = {
2328 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2329 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2330 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2331 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2332 .writefn = par_write },
2333#ifndef CONFIG_USER_ONLY
87562e4f 2334 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2335 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2336 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2337 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2338#endif
2339 REGINFO_SENTINEL
2340};
2341
18032bec
PM
2342/* Return basic MPU access permission bits. */
2343static uint32_t simple_mpu_ap_bits(uint32_t val)
2344{
2345 uint32_t ret;
2346 uint32_t mask;
2347 int i;
2348 ret = 0;
2349 mask = 3;
2350 for (i = 0; i < 16; i += 2) {
2351 ret |= (val >> i) & mask;
2352 mask <<= 2;
2353 }
2354 return ret;
2355}
2356
2357/* Pad basic MPU access permission bits to extended format. */
2358static uint32_t extended_mpu_ap_bits(uint32_t val)
2359{
2360 uint32_t ret;
2361 uint32_t mask;
2362 int i;
2363 ret = 0;
2364 mask = 3;
2365 for (i = 0; i < 16; i += 2) {
2366 ret |= (val & mask) << i;
2367 mask <<= 2;
2368 }
2369 return ret;
2370}
2371
c4241c7d
PM
2372static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2373 uint64_t value)
18032bec 2374{
7e09797c 2375 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2376}
2377
c4241c7d 2378static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2379{
7e09797c 2380 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2381}
2382
c4241c7d
PM
2383static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2384 uint64_t value)
18032bec 2385{
7e09797c 2386 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2387}
2388
c4241c7d 2389static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2390{
7e09797c 2391 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2392}
2393
6cb0b013
PC
2394static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2395{
2396 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2397
2398 if (!u32p) {
2399 return 0;
2400 }
2401
1bc04a88 2402 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
2403 return *u32p;
2404}
2405
2406static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2407 uint64_t value)
2408{
2409 ARMCPU *cpu = arm_env_get_cpu(env);
2410 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2411
2412 if (!u32p) {
2413 return;
2414 }
2415
1bc04a88 2416 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 2417 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
2418 *u32p = value;
2419}
2420
6cb0b013
PC
2421static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2422 uint64_t value)
2423{
2424 ARMCPU *cpu = arm_env_get_cpu(env);
2425 uint32_t nrgs = cpu->pmsav7_dregion;
2426
2427 if (value >= nrgs) {
2428 qemu_log_mask(LOG_GUEST_ERROR,
2429 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2430 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2431 return;
2432 }
2433
2434 raw_write(env, ri, value);
2435}
2436
2437static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
2438 /* Reset for all these registers is handled in arm_cpu_reset(),
2439 * because the PMSAv7 is also used by M-profile CPUs, which do
2440 * not register cpregs but still need the state to be reset.
2441 */
6cb0b013
PC
2442 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2443 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2444 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
2445 .readfn = pmsav7_read, .writefn = pmsav7_write,
2446 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2447 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2448 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2449 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
2450 .readfn = pmsav7_read, .writefn = pmsav7_write,
2451 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2452 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2453 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2454 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
2455 .readfn = pmsav7_read, .writefn = pmsav7_write,
2456 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2457 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2458 .access = PL1_RW,
1bc04a88 2459 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
2460 .writefn = pmsav7_rgnr_write,
2461 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
2462 REGINFO_SENTINEL
2463};
2464
18032bec
PM
2465static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2466 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2467 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2468 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2469 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2470 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2471 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2472 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2473 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2474 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2475 .access = PL1_RW,
7e09797c
PM
2476 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2477 .resetvalue = 0, },
18032bec
PM
2478 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2479 .access = PL1_RW,
7e09797c
PM
2480 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2481 .resetvalue = 0, },
ecce5c3c
PM
2482 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2483 .access = PL1_RW,
2484 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2485 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2486 .access = PL1_RW,
2487 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2488 /* Protection region base and size registers */
e508a92b
PM
2489 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2490 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2491 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2492 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2493 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2494 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2495 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2496 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2497 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2498 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2499 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2500 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2501 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2502 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2503 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2504 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2505 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2506 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2507 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2508 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2509 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2510 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2511 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2512 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2513 REGINFO_SENTINEL
2514};
2515
c4241c7d
PM
2516static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2517 uint64_t value)
ecce5c3c 2518{
11f136ee 2519 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2520 int maskshift = extract32(value, 0, 3);
2521
e389be16
FA
2522 if (!arm_feature(env, ARM_FEATURE_V8)) {
2523 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2524 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2525 * using Long-desciptor translation table format */
2526 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2527 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2528 /* In an implementation that includes the Security Extensions
2529 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2530 * Short-descriptor translation table format.
2531 */
2532 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2533 } else {
2534 value &= TTBCR_N;
2535 }
e42c4db3 2536 }
e389be16 2537
b6af0975 2538 /* Update the masks corresponding to the TCR bank being written
11f136ee 2539 * Note that we always calculate mask and base_mask, but
e42c4db3 2540 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2541 * for long-descriptor tables the TCR fields are used differently
2542 * and the mask and base_mask values are meaningless.
e42c4db3 2543 */
11f136ee
FA
2544 tcr->raw_tcr = value;
2545 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2546 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2547}
2548
c4241c7d
PM
2549static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2550 uint64_t value)
d4e6df63 2551{
00c8cb0a
AF
2552 ARMCPU *cpu = arm_env_get_cpu(env);
2553
d4e6df63
PM
2554 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2555 /* With LPAE the TTBCR could result in a change of ASID
2556 * via the TTBCR.A1 bit, so do a TLB flush.
2557 */
d10eb08f 2558 tlb_flush(CPU(cpu));
d4e6df63 2559 }
c4241c7d 2560 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2561}
2562
ecce5c3c
PM
2563static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2564{
11f136ee
FA
2565 TCR *tcr = raw_ptr(env, ri);
2566
2567 /* Reset both the TCR as well as the masks corresponding to the bank of
2568 * the TCR being reset.
2569 */
2570 tcr->raw_tcr = 0;
2571 tcr->mask = 0;
2572 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2573}
2574
cb2e37df
PM
2575static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2576 uint64_t value)
2577{
00c8cb0a 2578 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2579 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2580
cb2e37df 2581 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 2582 tlb_flush(CPU(cpu));
11f136ee 2583 tcr->raw_tcr = value;
cb2e37df
PM
2584}
2585
327ed10f
PM
2586static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2587 uint64_t value)
2588{
2589 /* 64 bit accesses to the TTBRs can change the ASID and so we
2590 * must flush the TLB.
2591 */
2592 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2593 ARMCPU *cpu = arm_env_get_cpu(env);
2594
d10eb08f 2595 tlb_flush(CPU(cpu));
327ed10f
PM
2596 }
2597 raw_write(env, ri, value);
2598}
2599
b698e9cf
EI
2600static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2601 uint64_t value)
2602{
2603 ARMCPU *cpu = arm_env_get_cpu(env);
2604 CPUState *cs = CPU(cpu);
2605
2606 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2607 if (raw_read(env, ri) != value) {
0336cbf8 2608 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2609 ARMMMUIdxBit_S12NSE1 |
2610 ARMMMUIdxBit_S12NSE0 |
2611 ARMMMUIdxBit_S2NS);
b698e9cf
EI
2612 raw_write(env, ri, value);
2613 }
2614}
2615
8e5d75c9 2616static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2617 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2618 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2619 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2620 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2621 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2622 .access = PL1_RW, .resetvalue = 0,
2623 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2624 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2625 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2626 .access = PL1_RW, .resetvalue = 0,
2627 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2628 offsetof(CPUARMState, cp15.dfar_ns) } },
2629 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2630 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2631 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2632 .resetvalue = 0, },
2633 REGINFO_SENTINEL
2634};
2635
2636static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2637 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2638 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2639 .access = PL1_RW,
d81c519c 2640 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2641 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2642 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2643 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2644 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2645 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2646 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2647 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2648 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2649 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2650 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2651 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2652 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2653 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2654 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2655 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2656 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2657 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2658 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2659 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2660 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2661 REGINFO_SENTINEL
2662};
2663
c4241c7d
PM
2664static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2665 uint64_t value)
1047b9d7
PM
2666{
2667 env->cp15.c15_ticonfig = value & 0xe7;
2668 /* The OS_TYPE bit in this register changes the reported CPUID! */
2669 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2670 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2671}
2672
c4241c7d
PM
2673static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2674 uint64_t value)
1047b9d7
PM
2675{
2676 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2677}
2678
c4241c7d
PM
2679static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2680 uint64_t value)
1047b9d7
PM
2681{
2682 /* Wait-for-interrupt (deprecated) */
c3affe56 2683 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2684}
2685
c4241c7d
PM
2686static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2687 uint64_t value)
c4804214
PM
2688{
2689 /* On OMAP there are registers indicating the max/min index of dcache lines
2690 * containing a dirty line; cache flush operations have to reset these.
2691 */
2692 env->cp15.c15_i_max = 0x000;
2693 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2694}
2695
18032bec
PM
2696static const ARMCPRegInfo omap_cp_reginfo[] = {
2697 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2698 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2699 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2700 .resetvalue = 0, },
1047b9d7
PM
2701 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2702 .access = PL1_RW, .type = ARM_CP_NOP },
2703 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2704 .access = PL1_RW,
2705 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2706 .writefn = omap_ticonfig_write },
2707 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2708 .access = PL1_RW,
2709 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2710 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2711 .access = PL1_RW, .resetvalue = 0xff0,
2712 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2713 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2714 .access = PL1_RW,
2715 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2716 .writefn = omap_threadid_write },
2717 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2718 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2719 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2720 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2721 /* TODO: Peripheral port remap register:
2722 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2723 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2724 * when MMU is off.
2725 */
c4804214 2726 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2727 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2728 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2729 .writefn = omap_cachemaint_write },
34f90529
PM
2730 { .name = "C9", .cp = 15, .crn = 9,
2731 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2732 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2733 REGINFO_SENTINEL
2734};
2735
c4241c7d
PM
2736static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2737 uint64_t value)
1047b9d7 2738{
c0f4af17 2739 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2740}
2741
2742static const ARMCPRegInfo xscale_cp_reginfo[] = {
2743 { .name = "XSCALE_CPAR",
2744 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2745 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2746 .writefn = xscale_cpar_write, },
2771db27
PM
2747 { .name = "XSCALE_AUXCR",
2748 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2749 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2750 .resetvalue = 0, },
3b771579
PM
2751 /* XScale specific cache-lockdown: since we have no cache we NOP these
2752 * and hope the guest does not really rely on cache behaviour.
2753 */
2754 { .name = "XSCALE_LOCK_ICACHE_LINE",
2755 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2756 .access = PL1_W, .type = ARM_CP_NOP },
2757 { .name = "XSCALE_UNLOCK_ICACHE",
2758 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2759 .access = PL1_W, .type = ARM_CP_NOP },
2760 { .name = "XSCALE_DCACHE_LOCK",
2761 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2762 .access = PL1_RW, .type = ARM_CP_NOP },
2763 { .name = "XSCALE_UNLOCK_DCACHE",
2764 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2765 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2766 REGINFO_SENTINEL
2767};
2768
2769static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2770 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2771 * implementation of this implementation-defined space.
2772 * Ideally this should eventually disappear in favour of actually
2773 * implementing the correct behaviour for all cores.
2774 */
2775 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2776 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2777 .access = PL1_RW,
7a0e58fa 2778 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2779 .resetvalue = 0 },
18032bec
PM
2780 REGINFO_SENTINEL
2781};
2782
c4804214
PM
2783static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2784 /* Cache status: RAZ because we have no cache so it's always clean */
2785 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2786 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2787 .resetvalue = 0 },
c4804214
PM
2788 REGINFO_SENTINEL
2789};
2790
2791static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2792 /* We never have a a block transfer operation in progress */
2793 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2794 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2795 .resetvalue = 0 },
30b05bba
PM
2796 /* The cache ops themselves: these all NOP for QEMU */
2797 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2798 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2799 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2800 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2801 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2802 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2803 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2804 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2805 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2806 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2807 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2808 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2809 REGINFO_SENTINEL
2810};
2811
2812static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2813 /* The cache test-and-clean instructions always return (1 << 30)
2814 * to indicate that there are no dirty cache lines.
2815 */
2816 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2817 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2818 .resetvalue = (1 << 30) },
c4804214 2819 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2820 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2821 .resetvalue = (1 << 30) },
c4804214
PM
2822 REGINFO_SENTINEL
2823};
2824
34f90529
PM
2825static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2826 /* Ignore ReadBuffer accesses */
2827 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2828 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2829 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2830 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2831 REGINFO_SENTINEL
2832};
2833
731de9e6
EI
2834static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2835{
2836 ARMCPU *cpu = arm_env_get_cpu(env);
2837 unsigned int cur_el = arm_current_el(env);
2838 bool secure = arm_is_secure(env);
2839
2840 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2841 return env->cp15.vpidr_el2;
2842 }
2843 return raw_read(env, ri);
2844}
2845
06a7e647 2846static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2847{
eb5e1d3c
PF
2848 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2849 uint64_t mpidr = cpu->mp_affinity;
2850
81bdde9d 2851 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2852 mpidr |= (1U << 31);
81bdde9d
PM
2853 /* Cores which are uniprocessor (non-coherent)
2854 * but still implement the MP extensions set
a8e81b31 2855 * bit 30. (For instance, Cortex-R5).
81bdde9d 2856 */
a8e81b31
PC
2857 if (cpu->mp_is_up) {
2858 mpidr |= (1u << 30);
2859 }
81bdde9d 2860 }
c4241c7d 2861 return mpidr;
81bdde9d
PM
2862}
2863
06a7e647
EI
2864static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2865{
f0d574d6
EI
2866 unsigned int cur_el = arm_current_el(env);
2867 bool secure = arm_is_secure(env);
2868
2869 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2870 return env->cp15.vmpidr_el2;
2871 }
06a7e647
EI
2872 return mpidr_read_val(env);
2873}
2874
81bdde9d 2875static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2876 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2877 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2878 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2879 REGINFO_SENTINEL
2880};
2881
7ac681cf 2882static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2883 /* NOP AMAIR0/1 */
b0fe2427
PM
2884 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2885 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2886 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2887 .resetvalue = 0 },
b0fe2427 2888 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2889 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2890 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2891 .resetvalue = 0 },
891a2fe7 2892 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2893 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2894 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2895 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2896 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2897 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2898 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2899 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2900 .writefn = vmsa_ttbr_write, },
891a2fe7 2901 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2902 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2903 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2904 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2905 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2906 REGINFO_SENTINEL
2907};
2908
c4241c7d 2909static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2910{
c4241c7d 2911 return vfp_get_fpcr(env);
b0d2b7d0
PM
2912}
2913
c4241c7d
PM
2914static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915 uint64_t value)
b0d2b7d0
PM
2916{
2917 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2918}
2919
c4241c7d 2920static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2921{
c4241c7d 2922 return vfp_get_fpsr(env);
b0d2b7d0
PM
2923}
2924
c4241c7d
PM
2925static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2926 uint64_t value)
b0d2b7d0
PM
2927{
2928 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2929}
2930
3f208fd7
PM
2931static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2932 bool isread)
c2b820fe 2933{
137feaa9 2934 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2935 return CP_ACCESS_TRAP;
2936 }
2937 return CP_ACCESS_OK;
2938}
2939
2940static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2941 uint64_t value)
2942{
2943 env->daif = value & PSTATE_DAIF;
2944}
2945
8af35c37 2946static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
2947 const ARMCPRegInfo *ri,
2948 bool isread)
8af35c37
PM
2949{
2950 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2951 * SCTLR_EL1.UCI is set.
2952 */
137feaa9 2953 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2954 return CP_ACCESS_TRAP;
2955 }
2956 return CP_ACCESS_OK;
2957}
2958
dbb1fb27
AB
2959/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2960 * Page D4-1736 (DDI0487A.b)
2961 */
2962
fd3ed969
PM
2963static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2964 uint64_t value)
168aa23b 2965{
a67cf277 2966 CPUState *cs = ENV_GET_CPU(env);
dbb1fb27 2967
fd3ed969 2968 if (arm_is_secure_below_el3(env)) {
0336cbf8 2969 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2970 ARMMMUIdxBit_S1SE1 |
2971 ARMMMUIdxBit_S1SE0);
fd3ed969 2972 } else {
0336cbf8 2973 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
2974 ARMMMUIdxBit_S12NSE1 |
2975 ARMMMUIdxBit_S12NSE0);
fd3ed969 2976 }
168aa23b
PM
2977}
2978
fd3ed969
PM
2979static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2980 uint64_t value)
168aa23b 2981{
a67cf277 2982 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 2983 bool sec = arm_is_secure_below_el3(env);
dbb1fb27 2984
a67cf277
AB
2985 if (sec) {
2986 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
2987 ARMMMUIdxBit_S1SE1 |
2988 ARMMMUIdxBit_S1SE0);
a67cf277
AB
2989 } else {
2990 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
2991 ARMMMUIdxBit_S12NSE1 |
2992 ARMMMUIdxBit_S12NSE0);
fd3ed969 2993 }
168aa23b
PM
2994}
2995
fd3ed969
PM
2996static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2997 uint64_t value)
168aa23b 2998{
fd3ed969
PM
2999 /* Note that the 'ALL' scope must invalidate both stage 1 and
3000 * stage 2 translations, whereas most other scopes only invalidate
3001 * stage 1 translations.
3002 */
00c8cb0a 3003 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
3004 CPUState *cs = CPU(cpu);
3005
3006 if (arm_is_secure_below_el3(env)) {
0336cbf8 3007 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3008 ARMMMUIdxBit_S1SE1 |
3009 ARMMMUIdxBit_S1SE0);
fd3ed969
PM
3010 } else {
3011 if (arm_feature(env, ARM_FEATURE_EL2)) {
0336cbf8 3012 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3013 ARMMMUIdxBit_S12NSE1 |
3014 ARMMMUIdxBit_S12NSE0 |
3015 ARMMMUIdxBit_S2NS);
fd3ed969 3016 } else {
0336cbf8 3017 tlb_flush_by_mmuidx(cs,
8bd5c820
PM
3018 ARMMMUIdxBit_S12NSE1 |
3019 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3020 }
3021 }
168aa23b
PM
3022}
3023
fd3ed969 3024static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
3025 uint64_t value)
3026{
fd3ed969
PM
3027 ARMCPU *cpu = arm_env_get_cpu(env);
3028 CPUState *cs = CPU(cpu);
3029
8bd5c820 3030 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3031}
3032
43efaa33
PM
3033static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3034 uint64_t value)
3035{
3036 ARMCPU *cpu = arm_env_get_cpu(env);
3037 CPUState *cs = CPU(cpu);
3038
8bd5c820 3039 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3040}
3041
fd3ed969
PM
3042static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3043 uint64_t value)
3044{
3045 /* Note that the 'ALL' scope must invalidate both stage 1 and
3046 * stage 2 translations, whereas most other scopes only invalidate
3047 * stage 1 translations.
3048 */
a67cf277 3049 CPUState *cs = ENV_GET_CPU(env);
fd3ed969
PM
3050 bool sec = arm_is_secure_below_el3(env);
3051 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
a67cf277
AB
3052
3053 if (sec) {
3054 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3055 ARMMMUIdxBit_S1SE1 |
3056 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3057 } else if (has_el2) {
3058 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3059 ARMMMUIdxBit_S12NSE1 |
3060 ARMMMUIdxBit_S12NSE0 |
3061 ARMMMUIdxBit_S2NS);
a67cf277
AB
3062 } else {
3063 tlb_flush_by_mmuidx_all_cpus_synced(cs,
8bd5c820
PM
3064 ARMMMUIdxBit_S12NSE1 |
3065 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3066 }
3067}
3068
2bfb9d75
PM
3069static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3070 uint64_t value)
3071{
a67cf277 3072 CPUState *cs = ENV_GET_CPU(env);
2bfb9d75 3073
8bd5c820 3074 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
2bfb9d75
PM
3075}
3076
43efaa33
PM
3077static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078 uint64_t value)
3079{
a67cf277 3080 CPUState *cs = ENV_GET_CPU(env);
43efaa33 3081
8bd5c820 3082 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
43efaa33
PM
3083}
3084
fd3ed969
PM
3085static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3086 uint64_t value)
3087{
3088 /* Invalidate by VA, EL1&0 (AArch64 version).
3089 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3090 * since we don't support flush-for-specific-ASID-only or
3091 * flush-last-level-only.
3092 */
3093 ARMCPU *cpu = arm_env_get_cpu(env);
3094 CPUState *cs = CPU(cpu);
3095 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3096
3097 if (arm_is_secure_below_el3(env)) {
0336cbf8 3098 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3099 ARMMMUIdxBit_S1SE1 |
3100 ARMMMUIdxBit_S1SE0);
fd3ed969 3101 } else {
0336cbf8 3102 tlb_flush_page_by_mmuidx(cs, pageaddr,
8bd5c820
PM
3103 ARMMMUIdxBit_S12NSE1 |
3104 ARMMMUIdxBit_S12NSE0);
fd3ed969
PM
3105 }
3106}
3107
3108static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3109 uint64_t value)
fa439fc5 3110{
fd3ed969
PM
3111 /* Invalidate by VA, EL2
3112 * Currently handles both VAE2 and VALE2, since we don't support
3113 * flush-last-level-only.
3114 */
3115 ARMCPU *cpu = arm_env_get_cpu(env);
3116 CPUState *cs = CPU(cpu);
3117 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3118
8bd5c820 3119 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
fd3ed969
PM
3120}
3121
43efaa33
PM
3122static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3123 uint64_t value)
3124{
3125 /* Invalidate by VA, EL3
3126 * Currently handles both VAE3 and VALE3, since we don't support
3127 * flush-last-level-only.
3128 */
3129 ARMCPU *cpu = arm_env_get_cpu(env);
3130 CPUState *cs = CPU(cpu);
3131 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3132
8bd5c820 3133 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
43efaa33
PM
3134}
3135
fd3ed969
PM
3136static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3137 uint64_t value)
3138{
a67cf277
AB
3139 ARMCPU *cpu = arm_env_get_cpu(env);
3140 CPUState *cs = CPU(cpu);
fd3ed969 3141 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
3142 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3143
a67cf277
AB
3144 if (sec) {
3145 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3146 ARMMMUIdxBit_S1SE1 |
3147 ARMMMUIdxBit_S1SE0);
a67cf277
AB
3148 } else {
3149 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820
PM
3150 ARMMMUIdxBit_S12NSE1 |
3151 ARMMMUIdxBit_S12NSE0);
fa439fc5
PM
3152 }
3153}
3154
fd3ed969
PM
3155static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3156 uint64_t value)
fa439fc5 3157{
a67cf277 3158 CPUState *cs = ENV_GET_CPU(env);
fd3ed969 3159 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5 3160
a67cf277 3161 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3162 ARMMMUIdxBit_S1E2);
fa439fc5
PM
3163}
3164
43efaa33
PM
3165static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3166 uint64_t value)
3167{
a67cf277 3168 CPUState *cs = ENV_GET_CPU(env);
43efaa33
PM
3169 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3170
a67cf277 3171 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3172 ARMMMUIdxBit_S1E3);
43efaa33
PM
3173}
3174
cea66e91
PM
3175static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3176 uint64_t value)
3177{
3178 /* Invalidate by IPA. This has to invalidate any structures that
3179 * contain only stage 2 translation information, but does not need
3180 * to apply to structures that contain combined stage 1 and stage 2
3181 * translation information.
3182 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3183 */
3184 ARMCPU *cpu = arm_env_get_cpu(env);
3185 CPUState *cs = CPU(cpu);
3186 uint64_t pageaddr;
3187
3188 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3189 return;
3190 }
3191
3192 pageaddr = sextract64(value << 12, 0, 48);
3193
8bd5c820 3194 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
cea66e91
PM
3195}
3196
3197static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3198 uint64_t value)
3199{
a67cf277 3200 CPUState *cs = ENV_GET_CPU(env);
cea66e91
PM
3201 uint64_t pageaddr;
3202
3203 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3204 return;
3205 }
3206
3207 pageaddr = sextract64(value << 12, 0, 48);
3208
a67cf277 3209 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
8bd5c820 3210 ARMMMUIdxBit_S2NS);
cea66e91
PM
3211}
3212
3f208fd7
PM
3213static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3214 bool isread)
aca3f40b
PM
3215{
3216 /* We don't implement EL2, so the only control on DC ZVA is the
3217 * bit in the SCTLR which can prohibit access for EL0.
3218 */
137feaa9 3219 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3220 return CP_ACCESS_TRAP;
3221 }
3222 return CP_ACCESS_OK;
3223}
3224
3225static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3226{
3227 ARMCPU *cpu = arm_env_get_cpu(env);
3228 int dzp_bit = 1 << 4;
3229
3230 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3231 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3232 dzp_bit = 0;
3233 }
3234 return cpu->dcz_blocksize | dzp_bit;
3235}
3236
3f208fd7
PM
3237static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3238 bool isread)
f502cfc2 3239{
cdcf1405 3240 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3241 /* Access to SP_EL0 is undefined if it's being used as
3242 * the stack pointer.
3243 */
3244 return CP_ACCESS_TRAP_UNCATEGORIZED;
3245 }
3246 return CP_ACCESS_OK;
3247}
3248
3249static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3250{
3251 return env->pstate & PSTATE_SP;
3252}
3253
3254static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3255{
3256 update_spsel(env, val);
3257}
3258
137feaa9
FA
3259static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3260 uint64_t value)
3261{
3262 ARMCPU *cpu = arm_env_get_cpu(env);
3263
3264 if (raw_read(env, ri) == value) {
3265 /* Skip the TLB flush if nothing actually changed; Linux likes
3266 * to do a lot of pointless SCTLR writes.
3267 */
3268 return;
3269 }
3270
06312feb
PM
3271 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3272 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3273 value &= ~SCTLR_M;
3274 }
3275
137feaa9
FA
3276 raw_write(env, ri, value);
3277 /* ??? Lots of these bits are not implemented. */
3278 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 3279 tlb_flush(CPU(cpu));
137feaa9
FA
3280}
3281
3f208fd7
PM
3282static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3283 bool isread)
03fbf20f
PM
3284{
3285 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3286 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3287 }
3288 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3289 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3290 }
3291 return CP_ACCESS_OK;
3292}
3293
a8d64e73
PM
3294static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3295 uint64_t value)
3296{
3297 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3298}
3299
b0d2b7d0
PM
3300static const ARMCPRegInfo v8_cp_reginfo[] = {
3301 /* Minimal set of EL0-visible registers. This will need to be expanded
3302 * significantly for system emulation of AArch64 CPUs.
3303 */
3304 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3305 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3306 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3307 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3308 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3309 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3310 .access = PL0_RW, .accessfn = aa64_daif_access,
3311 .fieldoffset = offsetof(CPUARMState, daif),
3312 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3313 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3314 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3315 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3316 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3317 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3318 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3319 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3320 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3321 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3322 .readfn = aa64_dczid_read },
3323 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3324 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3325 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3326#ifndef CONFIG_USER_ONLY
3327 /* Avoid overhead of an access check that always passes in user-mode */
3328 .accessfn = aa64_zva_access,
3329#endif
3330 },
0eef9d98
PM
3331 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3332 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3333 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3334 /* Cache ops: all NOPs since we don't emulate caches */
3335 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3336 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3337 .access = PL1_W, .type = ARM_CP_NOP },
3338 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3339 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3340 .access = PL1_W, .type = ARM_CP_NOP },
3341 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3342 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3343 .access = PL0_W, .type = ARM_CP_NOP,
3344 .accessfn = aa64_cacheop_access },
3345 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3346 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3347 .access = PL1_W, .type = ARM_CP_NOP },
3348 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3349 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3350 .access = PL1_W, .type = ARM_CP_NOP },
3351 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3352 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3353 .access = PL0_W, .type = ARM_CP_NOP,
3354 .accessfn = aa64_cacheop_access },
3355 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3356 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3357 .access = PL1_W, .type = ARM_CP_NOP },
3358 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3359 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3360 .access = PL0_W, .type = ARM_CP_NOP,
3361 .accessfn = aa64_cacheop_access },
3362 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3363 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3364 .access = PL0_W, .type = ARM_CP_NOP,
3365 .accessfn = aa64_cacheop_access },
3366 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3367 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3368 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3369 /* TLBI operations */
3370 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3371 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3372 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3373 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3374 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3375 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3376 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3377 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3378 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3379 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3380 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3381 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3382 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3383 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3384 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3385 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3386 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3387 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3388 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3389 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3390 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3391 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3392 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3393 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3394 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3395 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3396 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3397 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3398 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3399 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3400 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3401 .writefn = tlbi_aa64_vae1_write },
168aa23b 3402 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3403 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3404 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3405 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3406 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3407 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3408 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3409 .writefn = tlbi_aa64_vae1_write },
168aa23b 3410 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3411 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3412 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3413 .writefn = tlbi_aa64_vae1_write },
168aa23b 3414 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3415 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3416 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3417 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3418 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3419 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3420 .access = PL2_W, .type = ARM_CP_NO_RAW,
3421 .writefn = tlbi_aa64_ipas2e1is_write },
3422 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3423 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3424 .access = PL2_W, .type = ARM_CP_NO_RAW,
3425 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3426 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3427 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3428 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3429 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3430 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3432 .access = PL2_W, .type = ARM_CP_NO_RAW,
3433 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3434 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3435 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3436 .access = PL2_W, .type = ARM_CP_NO_RAW,
3437 .writefn = tlbi_aa64_ipas2e1_write },
3438 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3439 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3440 .access = PL2_W, .type = ARM_CP_NO_RAW,
3441 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3442 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3443 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3444 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3445 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3446 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3447 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3448 .access = PL2_W, .type = ARM_CP_NO_RAW,
3449 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3450#ifndef CONFIG_USER_ONLY
3451 /* 64 bit address translation operations */
3452 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3453 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3454 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3455 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3456 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3457 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3458 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3459 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3460 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3461 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3462 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3463 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3464 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3465 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3466 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3467 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3468 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3469 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3470 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3471 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3472 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3473 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3474 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3475 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3476 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3477 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3478 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3479 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3480 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3481 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3482 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3483 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3484 .type = ARM_CP_ALIAS,
3485 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3486 .access = PL1_RW, .resetvalue = 0,
3487 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3488 .writefn = par_write },
19525524 3489#endif
995939a6 3490 /* TLB invalidate last level of translation table walk */
9449fdf6 3491 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3492 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3493 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3494 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3495 .writefn = tlbimvaa_is_write },
9449fdf6 3496 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3497 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3498 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3499 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
541ef8c2
SS
3500 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3501 .type = ARM_CP_NO_RAW, .access = PL2_W,
3502 .writefn = tlbimva_hyp_write },
3503 { .name = "TLBIMVALHIS",
3504 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3505 .type = ARM_CP_NO_RAW, .access = PL2_W,
3506 .writefn = tlbimva_hyp_is_write },
3507 { .name = "TLBIIPAS2",
3508 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3509 .type = ARM_CP_NO_RAW, .access = PL2_W,
3510 .writefn = tlbiipas2_write },
3511 { .name = "TLBIIPAS2IS",
3512 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3513 .type = ARM_CP_NO_RAW, .access = PL2_W,
3514 .writefn = tlbiipas2_is_write },
3515 { .name = "TLBIIPAS2L",
3516 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3517 .type = ARM_CP_NO_RAW, .access = PL2_W,
3518 .writefn = tlbiipas2_write },
3519 { .name = "TLBIIPAS2LIS",
3520 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3521 .type = ARM_CP_NO_RAW, .access = PL2_W,
3522 .writefn = tlbiipas2_is_write },
9449fdf6
PM
3523 /* 32 bit cache operations */
3524 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3525 .type = ARM_CP_NOP, .access = PL1_W },
3526 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3527 .type = ARM_CP_NOP, .access = PL1_W },
3528 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3529 .type = ARM_CP_NOP, .access = PL1_W },
3530 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3531 .type = ARM_CP_NOP, .access = PL1_W },
3532 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3533 .type = ARM_CP_NOP, .access = PL1_W },
3534 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3535 .type = ARM_CP_NOP, .access = PL1_W },
3536 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3537 .type = ARM_CP_NOP, .access = PL1_W },
3538 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3539 .type = ARM_CP_NOP, .access = PL1_W },
3540 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3541 .type = ARM_CP_NOP, .access = PL1_W },
3542 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3543 .type = ARM_CP_NOP, .access = PL1_W },
3544 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3545 .type = ARM_CP_NOP, .access = PL1_W },
3546 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3547 .type = ARM_CP_NOP, .access = PL1_W },
3548 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3549 .type = ARM_CP_NOP, .access = PL1_W },
3550 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3551 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3552 .access = PL1_RW, .resetvalue = 0,
3553 .writefn = dacr_write, .raw_writefn = raw_write,
3554 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3555 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3556 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3557 .type = ARM_CP_ALIAS,
a0618a19 3558 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3559 .access = PL1_RW,
3560 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3561 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3562 .type = ARM_CP_ALIAS,
a65f1de9 3563 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3564 .access = PL1_RW,
3565 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3566 /* We rely on the access checks not allowing the guest to write to the
3567 * state field when SPSel indicates that it's being used as the stack
3568 * pointer.
3569 */
3570 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3571 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3572 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3573 .type = ARM_CP_ALIAS,
f502cfc2 3574 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3575 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3576 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3577 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3578 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3579 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3580 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3581 .type = ARM_CP_NO_RAW,
f502cfc2 3582 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3583 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3584 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3585 .type = ARM_CP_ALIAS,
3586 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3587 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3588 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3589 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3590 .access = PL2_RW, .resetvalue = 0,
3591 .writefn = dacr_write, .raw_writefn = raw_write,
3592 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3593 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3594 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3595 .access = PL2_RW, .resetvalue = 0,
3596 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3597 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3598 .type = ARM_CP_ALIAS,
3599 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3600 .access = PL2_RW,
3601 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3602 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3603 .type = ARM_CP_ALIAS,
3604 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3605 .access = PL2_RW,
3606 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3607 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3608 .type = ARM_CP_ALIAS,
3609 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3610 .access = PL2_RW,
3611 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3612 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3613 .type = ARM_CP_ALIAS,
3614 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3615 .access = PL2_RW,
3616 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3617 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3618 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3619 .resetvalue = 0,
3620 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3621 { .name = "SDCR", .type = ARM_CP_ALIAS,
3622 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3623 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3624 .writefn = sdcr_write,
3625 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3626 REGINFO_SENTINEL
3627};
3628
d42e3c26 3629/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3630static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3631 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3632 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3633 .access = PL2_RW,
3634 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3635 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3636 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3637 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3638 .access = PL2_RW,
3639 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3640 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3641 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3642 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3643 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3644 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3645 .access = PL2_RW, .type = ARM_CP_CONST,
3646 .resetvalue = 0 },
3647 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3648 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3649 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3650 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3651 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3652 .access = PL2_RW, .type = ARM_CP_CONST,
3653 .resetvalue = 0 },
3654 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3655 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3656 .access = PL2_RW, .type = ARM_CP_CONST,
3657 .resetvalue = 0 },
37cd6c24
PM
3658 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3659 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3660 .access = PL2_RW, .type = ARM_CP_CONST,
3661 .resetvalue = 0 },
3662 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3663 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3664 .access = PL2_RW, .type = ARM_CP_CONST,
3665 .resetvalue = 0 },
06ec4c8c
EI
3666 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3667 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3668 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3669 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3670 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3671 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3672 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3673 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3674 .cp = 15, .opc1 = 6, .crm = 2,
3675 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3676 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3677 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3678 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3679 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3680 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3681 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3682 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3683 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3684 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3685 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3686 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3687 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3688 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3689 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3690 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3691 .resetvalue = 0 },
0b6440af
EI
3692 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3693 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3694 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3695 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3696 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3697 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3698 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3699 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3700 .resetvalue = 0 },
b0e66d95
EI
3701 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3702 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3703 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3704 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3705 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3706 .resetvalue = 0 },
3707 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3708 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3709 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3710 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3711 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3712 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3713 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3714 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3715 .access = PL2_RW, .accessfn = access_tda,
3716 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3717 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3718 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3719 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3720 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
3721 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3722 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3723 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3724 REGINFO_SENTINEL
3725};
3726
f149e3e8
EI
3727static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3728{
3729 ARMCPU *cpu = arm_env_get_cpu(env);
3730 uint64_t valid_mask = HCR_MASK;
3731
3732 if (arm_feature(env, ARM_FEATURE_EL3)) {
3733 valid_mask &= ~HCR_HCD;
77077a83
JK
3734 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3735 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3736 * However, if we're using the SMC PSCI conduit then QEMU is
3737 * effectively acting like EL3 firmware and so the guest at
3738 * EL2 should retain the ability to prevent EL1 from being
3739 * able to make SMC calls into the ersatz firmware, so in
3740 * that case HCR.TSC should be read/write.
3741 */
f149e3e8
EI
3742 valid_mask &= ~HCR_TSC;
3743 }
3744
3745 /* Clear RES0 bits. */
3746 value &= valid_mask;
3747
3748 /* These bits change the MMU setup:
3749 * HCR_VM enables stage 2 translation
3750 * HCR_PTW forbids certain page-table setups
3751 * HCR_DC Disables stage1 and enables stage2 translation
3752 */
3753 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
d10eb08f 3754 tlb_flush(CPU(cpu));
f149e3e8
EI
3755 }
3756 raw_write(env, ri, value);
3757}
3758
4771cd01 3759static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3760 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3761 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3762 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3763 .writefn = hcr_write },
3b685ba7 3764 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3765 .type = ARM_CP_ALIAS,
3b685ba7
EI
3766 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3767 .access = PL2_RW,
3768 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3769 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
3770 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3771 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3772 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3773 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3774 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3775 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3776 .type = ARM_CP_ALIAS,
3b685ba7 3777 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3778 .access = PL2_RW,
3779 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3780 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3781 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3782 .access = PL2_RW, .writefn = vbar_write,
3783 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3784 .resetvalue = 0 },
884b4dee
GB
3785 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3786 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3787 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3788 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3789 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3790 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3791 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3792 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3793 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3794 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3795 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3796 .resetvalue = 0 },
3797 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3798 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3799 .access = PL2_RW, .type = ARM_CP_ALIAS,
3800 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3801 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3802 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3803 .access = PL2_RW, .type = ARM_CP_CONST,
3804 .resetvalue = 0 },
3805 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3806 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3807 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3808 .access = PL2_RW, .type = ARM_CP_CONST,
3809 .resetvalue = 0 },
37cd6c24
PM
3810 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3811 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3812 .access = PL2_RW, .type = ARM_CP_CONST,
3813 .resetvalue = 0 },
3814 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3815 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3816 .access = PL2_RW, .type = ARM_CP_CONST,
3817 .resetvalue = 0 },
06ec4c8c
EI
3818 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3819 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
3820 .access = PL2_RW,
3821 /* no .writefn needed as this can't cause an ASID change;
3822 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3823 */
06ec4c8c 3824 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3825 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3826 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 3827 .type = ARM_CP_ALIAS,
68e9c2fe
EI
3828 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3829 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3830 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3831 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
3832 .access = PL2_RW,
3833 /* no .writefn needed as this can't cause an ASID change;
3834 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3835 */
68e9c2fe 3836 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3837 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3838 .cp = 15, .opc1 = 6, .crm = 2,
3839 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3840 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3841 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3842 .writefn = vttbr_write },
3843 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3844 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3845 .access = PL2_RW, .writefn = vttbr_write,
3846 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3847 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3848 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3849 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3850 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3851 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3852 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3853 .access = PL2_RW, .resetvalue = 0,
3854 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3855 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3856 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3857 .access = PL2_RW, .resetvalue = 0,
3858 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3859 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3860 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3861 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
3862 { .name = "TLBIALLNSNH",
3863 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3864 .type = ARM_CP_NO_RAW, .access = PL2_W,
3865 .writefn = tlbiall_nsnh_write },
3866 { .name = "TLBIALLNSNHIS",
3867 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3868 .type = ARM_CP_NO_RAW, .access = PL2_W,
3869 .writefn = tlbiall_nsnh_is_write },
3870 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3871 .type = ARM_CP_NO_RAW, .access = PL2_W,
3872 .writefn = tlbiall_hyp_write },
3873 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3874 .type = ARM_CP_NO_RAW, .access = PL2_W,
3875 .writefn = tlbiall_hyp_is_write },
3876 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3877 .type = ARM_CP_NO_RAW, .access = PL2_W,
3878 .writefn = tlbimva_hyp_write },
3879 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3880 .type = ARM_CP_NO_RAW, .access = PL2_W,
3881 .writefn = tlbimva_hyp_is_write },
51da9014
EI
3882 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3883 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3884 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3885 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3886 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3887 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3888 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3889 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3890 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3892 .access = PL2_W, .type = ARM_CP_NO_RAW,
3893 .writefn = tlbi_aa64_vae2_write },
3894 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3895 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3896 .access = PL2_W, .type = ARM_CP_NO_RAW,
3897 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3898 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3899 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3900 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3901 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3902 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3903 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3904 .access = PL2_W, .type = ARM_CP_NO_RAW,
3905 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3906#ifndef CONFIG_USER_ONLY
2a47df95
PM
3907 /* Unlike the other EL2-related AT operations, these must
3908 * UNDEF from EL3 if EL2 is not implemented, which is why we
3909 * define them here rather than with the rest of the AT ops.
3910 */
3911 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3912 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3913 .access = PL2_W, .accessfn = at_s1e2_access,
3914 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3915 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3916 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3917 .access = PL2_W, .accessfn = at_s1e2_access,
3918 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3919 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3920 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3921 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3922 * to behave as if SCR.NS was 1.
3923 */
3924 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3925 .access = PL2_W,
3926 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3927 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3928 .access = PL2_W,
3929 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3930 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3931 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3932 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3933 * reset values as IMPDEF. We choose to reset to 3 to comply with
3934 * both ARMv7 and ARMv8.
3935 */
3936 .access = PL2_RW, .resetvalue = 3,
3937 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3938 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3939 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3940 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3941 .writefn = gt_cntvoff_write,
3942 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3943 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3944 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3945 .writefn = gt_cntvoff_write,
3946 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3947 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3948 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3949 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3950 .type = ARM_CP_IO, .access = PL2_RW,
3951 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3952 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3953 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3954 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3955 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3956 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3957 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 3958 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
3959 .resetfn = gt_hyp_timer_reset,
3960 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3961 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3962 .type = ARM_CP_IO,
3963 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3964 .access = PL2_RW,
3965 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3966 .resetvalue = 0,
3967 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 3968#endif
14cc7b54
SF
3969 /* The only field of MDCR_EL2 that has a defined architectural reset value
3970 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3971 * don't impelment any PMU event counters, so using zero as a reset
3972 * value for MDCR_EL2 is okay
3973 */
3974 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3975 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3976 .access = PL2_RW, .resetvalue = 0,
3977 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
3978 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3979 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3980 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3981 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3982 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3983 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3984 .access = PL2_RW,
3985 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
3986 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3987 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3988 .access = PL2_RW,
3989 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
3990 REGINFO_SENTINEL
3991};
3992
2f027fc5
PM
3993static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
3994 bool isread)
3995{
3996 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
3997 * At Secure EL1 it traps to EL3.
3998 */
3999 if (arm_current_el(env) == 3) {
4000 return CP_ACCESS_OK;
4001 }
4002 if (arm_is_secure_below_el3(env)) {
4003 return CP_ACCESS_TRAP_EL3;
4004 }
4005 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4006 if (isread) {
4007 return CP_ACCESS_OK;
4008 }
4009 return CP_ACCESS_TRAP_UNCATEGORIZED;
4010}
4011
60fb1a87
GB
4012static const ARMCPRegInfo el3_cp_reginfo[] = {
4013 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4014 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4015 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4016 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 4017 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 4018 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
4019 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4020 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 4021 .writefn = scr_write },
60fb1a87
GB
4022 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4023 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4024 .access = PL3_RW, .resetvalue = 0,
4025 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4026 { .name = "SDER",
4027 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4028 .access = PL3_RW, .resetvalue = 0,
4029 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 4030 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
4031 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4032 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 4033 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
4034 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4035 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4036 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4037 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
4038 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4039 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
4040 .access = PL3_RW,
4041 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
4042 * we must provide a .raw_writefn and .resetfn because we handle
4043 * reset and migration for the AArch32 TTBCR(S), which might be
4044 * using mask and base_mask.
6459b94c 4045 */
811595a2 4046 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 4047 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 4048 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4049 .type = ARM_CP_ALIAS,
81547d66
EI
4050 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4051 .access = PL3_RW,
4052 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 4053 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
4054 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4055 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
4056 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4057 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4058 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 4059 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 4060 .type = ARM_CP_ALIAS,
81547d66 4061 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
4062 .access = PL3_RW,
4063 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
4064 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4065 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4066 .access = PL3_RW, .writefn = vbar_write,
4067 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4068 .resetvalue = 0 },
c6f19164
GB
4069 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4070 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4071 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4072 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
4073 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4074 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4075 .access = PL3_RW, .resetvalue = 0,
4076 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
4077 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4078 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4079 .access = PL3_RW, .type = ARM_CP_CONST,
4080 .resetvalue = 0 },
37cd6c24
PM
4081 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4082 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4083 .access = PL3_RW, .type = ARM_CP_CONST,
4084 .resetvalue = 0 },
4085 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4086 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4087 .access = PL3_RW, .type = ARM_CP_CONST,
4088 .resetvalue = 0 },
43efaa33
PM
4089 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4090 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4091 .access = PL3_W, .type = ARM_CP_NO_RAW,
4092 .writefn = tlbi_aa64_alle3is_write },
4093 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4094 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4095 .access = PL3_W, .type = ARM_CP_NO_RAW,
4096 .writefn = tlbi_aa64_vae3is_write },
4097 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4098 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4099 .access = PL3_W, .type = ARM_CP_NO_RAW,
4100 .writefn = tlbi_aa64_vae3is_write },
4101 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4102 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4103 .access = PL3_W, .type = ARM_CP_NO_RAW,
4104 .writefn = tlbi_aa64_alle3_write },
4105 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4106 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4107 .access = PL3_W, .type = ARM_CP_NO_RAW,
4108 .writefn = tlbi_aa64_vae3_write },
4109 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4110 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4111 .access = PL3_W, .type = ARM_CP_NO_RAW,
4112 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
4113 REGINFO_SENTINEL
4114};
4115
3f208fd7
PM
4116static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4117 bool isread)
7da845b0
PM
4118{
4119 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4120 * but the AArch32 CTR has its own reginfo struct)
4121 */
137feaa9 4122 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
4123 return CP_ACCESS_TRAP;
4124 }
4125 return CP_ACCESS_OK;
4126}
4127
1424ca8d
DM
4128static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4129 uint64_t value)
4130{
4131 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4132 * read via a bit in OSLSR_EL1.
4133 */
4134 int oslock;
4135
4136 if (ri->state == ARM_CP_STATE_AA32) {
4137 oslock = (value == 0xC5ACCE55);
4138 } else {
4139 oslock = value & 1;
4140 }
4141
4142 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4143}
4144
50300698 4145static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 4146 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
4147 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4148 * unlike DBGDRAR it is never accessible from EL0.
4149 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4150 * accessor.
50300698
PM
4151 */
4152 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4153 .access = PL0_R, .accessfn = access_tdra,
4154 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
4155 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4156 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
4157 .access = PL1_R, .accessfn = access_tdra,
4158 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 4159 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
4160 .access = PL0_R, .accessfn = access_tdra,
4161 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 4162 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
4163 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4164 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 4165 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
4166 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4167 .resetvalue = 0 },
5e8b12ff
PM
4168 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4169 * We don't implement the configurable EL0 access.
4170 */
4171 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4172 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 4173 .type = ARM_CP_ALIAS,
d6c8cf81 4174 .access = PL1_R, .accessfn = access_tda,
b061a82b 4175 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
4176 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4177 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 4178 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 4179 .accessfn = access_tdosa,
1424ca8d
DM
4180 .writefn = oslar_write },
4181 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4182 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4183 .access = PL1_R, .resetvalue = 10,
187f678d 4184 .accessfn = access_tdosa,
1424ca8d 4185 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
4186 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4187 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4188 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
4189 .access = PL1_RW, .accessfn = access_tdosa,
4190 .type = ARM_CP_NOP },
5e8b12ff
PM
4191 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4192 * implement vector catch debug events yet.
4193 */
4194 { .name = "DBGVCR",
4195 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
4196 .access = PL1_RW, .accessfn = access_tda,
4197 .type = ARM_CP_NOP },
4d2ec4da
PM
4198 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4199 * to save and restore a 32-bit guest's DBGVCR)
4200 */
4201 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4202 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4203 .access = PL2_RW, .accessfn = access_tda,
4204 .type = ARM_CP_NOP },
5dbdc434
PM
4205 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4206 * Channel but Linux may try to access this register. The 32-bit
4207 * alias is DBGDCCINT.
4208 */
4209 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4210 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4211 .access = PL1_RW, .accessfn = access_tda,
4212 .type = ARM_CP_NOP },
50300698
PM
4213 REGINFO_SENTINEL
4214};
4215
4216static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4217 /* 64 bit access versions of the (dummy) debug registers */
4218 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4219 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4220 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4221 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4222 REGINFO_SENTINEL
4223};
4224
9ee98ce8
PM
4225void hw_watchpoint_update(ARMCPU *cpu, int n)
4226{
4227 CPUARMState *env = &cpu->env;
4228 vaddr len = 0;
4229 vaddr wvr = env->cp15.dbgwvr[n];
4230 uint64_t wcr = env->cp15.dbgwcr[n];
4231 int mask;
4232 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4233
4234 if (env->cpu_watchpoint[n]) {
4235 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4236 env->cpu_watchpoint[n] = NULL;
4237 }
4238
4239 if (!extract64(wcr, 0, 1)) {
4240 /* E bit clear : watchpoint disabled */
4241 return;
4242 }
4243
4244 switch (extract64(wcr, 3, 2)) {
4245 case 0:
4246 /* LSC 00 is reserved and must behave as if the wp is disabled */
4247 return;
4248 case 1:
4249 flags |= BP_MEM_READ;
4250 break;
4251 case 2:
4252 flags |= BP_MEM_WRITE;
4253 break;
4254 case 3:
4255 flags |= BP_MEM_ACCESS;
4256 break;
4257 }
4258
4259 /* Attempts to use both MASK and BAS fields simultaneously are
4260 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4261 * thus generating a watchpoint for every byte in the masked region.
4262 */
4263 mask = extract64(wcr, 24, 4);
4264 if (mask == 1 || mask == 2) {
4265 /* Reserved values of MASK; we must act as if the mask value was
4266 * some non-reserved value, or as if the watchpoint were disabled.
4267 * We choose the latter.
4268 */
4269 return;
4270 } else if (mask) {
4271 /* Watchpoint covers an aligned area up to 2GB in size */
4272 len = 1ULL << mask;
4273 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4274 * whether the watchpoint fires when the unmasked bits match; we opt
4275 * to generate the exceptions.
4276 */
4277 wvr &= ~(len - 1);
4278 } else {
4279 /* Watchpoint covers bytes defined by the byte address select bits */
4280 int bas = extract64(wcr, 5, 8);
4281 int basstart;
4282
4283 if (bas == 0) {
4284 /* This must act as if the watchpoint is disabled */
4285 return;
4286 }
4287
4288 if (extract64(wvr, 2, 1)) {
4289 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4290 * ignored, and BAS[3:0] define which bytes to watch.
4291 */
4292 bas &= 0xf;
4293 }
4294 /* The BAS bits are supposed to be programmed to indicate a contiguous
4295 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4296 * we fire for each byte in the word/doubleword addressed by the WVR.
4297 * We choose to ignore any non-zero bits after the first range of 1s.
4298 */
4299 basstart = ctz32(bas);
4300 len = cto32(bas >> basstart);
4301 wvr += basstart;
4302 }
4303
4304 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4305 &env->cpu_watchpoint[n]);
4306}
4307
4308void hw_watchpoint_update_all(ARMCPU *cpu)
4309{
4310 int i;
4311 CPUARMState *env = &cpu->env;
4312
4313 /* Completely clear out existing QEMU watchpoints and our array, to
4314 * avoid possible stale entries following migration load.
4315 */
4316 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4317 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4318
4319 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4320 hw_watchpoint_update(cpu, i);
4321 }
4322}
4323
4324static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4325 uint64_t value)
4326{
4327 ARMCPU *cpu = arm_env_get_cpu(env);
4328 int i = ri->crm;
4329
4330 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4331 * register reads and behaves as if values written are sign extended.
4332 * Bits [1:0] are RES0.
4333 */
4334 value = sextract64(value, 0, 49) & ~3ULL;
4335
4336 raw_write(env, ri, value);
4337 hw_watchpoint_update(cpu, i);
4338}
4339
4340static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4341 uint64_t value)
4342{
4343 ARMCPU *cpu = arm_env_get_cpu(env);
4344 int i = ri->crm;
4345
4346 raw_write(env, ri, value);
4347 hw_watchpoint_update(cpu, i);
4348}
4349
46747d15
PM
4350void hw_breakpoint_update(ARMCPU *cpu, int n)
4351{
4352 CPUARMState *env = &cpu->env;
4353 uint64_t bvr = env->cp15.dbgbvr[n];
4354 uint64_t bcr = env->cp15.dbgbcr[n];
4355 vaddr addr;
4356 int bt;
4357 int flags = BP_CPU;
4358
4359 if (env->cpu_breakpoint[n]) {
4360 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4361 env->cpu_breakpoint[n] = NULL;
4362 }
4363
4364 if (!extract64(bcr, 0, 1)) {
4365 /* E bit clear : watchpoint disabled */
4366 return;
4367 }
4368
4369 bt = extract64(bcr, 20, 4);
4370
4371 switch (bt) {
4372 case 4: /* unlinked address mismatch (reserved if AArch64) */
4373 case 5: /* linked address mismatch (reserved if AArch64) */
4374 qemu_log_mask(LOG_UNIMP,
4375 "arm: address mismatch breakpoint types not implemented");
4376 return;
4377 case 0: /* unlinked address match */
4378 case 1: /* linked address match */
4379 {
4380 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4381 * we behave as if the register was sign extended. Bits [1:0] are
4382 * RES0. The BAS field is used to allow setting breakpoints on 16
4383 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4384 * a bp will fire if the addresses covered by the bp and the addresses
4385 * covered by the insn overlap but the insn doesn't start at the
4386 * start of the bp address range. We choose to require the insn and
4387 * the bp to have the same address. The constraints on writing to
4388 * BAS enforced in dbgbcr_write mean we have only four cases:
4389 * 0b0000 => no breakpoint
4390 * 0b0011 => breakpoint on addr
4391 * 0b1100 => breakpoint on addr + 2
4392 * 0b1111 => breakpoint on addr
4393 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4394 */
4395 int bas = extract64(bcr, 5, 4);
4396 addr = sextract64(bvr, 0, 49) & ~3ULL;
4397 if (bas == 0) {
4398 return;
4399 }
4400 if (bas == 0xc) {
4401 addr += 2;
4402 }
4403 break;
4404 }
4405 case 2: /* unlinked context ID match */
4406 case 8: /* unlinked VMID match (reserved if no EL2) */
4407 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4408 qemu_log_mask(LOG_UNIMP,
4409 "arm: unlinked context breakpoint types not implemented");
4410 return;
4411 case 9: /* linked VMID match (reserved if no EL2) */
4412 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4413 case 3: /* linked context ID match */
4414 default:
4415 /* We must generate no events for Linked context matches (unless
4416 * they are linked to by some other bp/wp, which is handled in
4417 * updates for the linking bp/wp). We choose to also generate no events
4418 * for reserved values.
4419 */
4420 return;
4421 }
4422
4423 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4424}
4425
4426void hw_breakpoint_update_all(ARMCPU *cpu)
4427{
4428 int i;
4429 CPUARMState *env = &cpu->env;
4430
4431 /* Completely clear out existing QEMU breakpoints and our array, to
4432 * avoid possible stale entries following migration load.
4433 */
4434 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4435 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4436
4437 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4438 hw_breakpoint_update(cpu, i);
4439 }
4440}
4441
4442static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4443 uint64_t value)
4444{
4445 ARMCPU *cpu = arm_env_get_cpu(env);
4446 int i = ri->crm;
4447
4448 raw_write(env, ri, value);
4449 hw_breakpoint_update(cpu, i);
4450}
4451
4452static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4453 uint64_t value)
4454{
4455 ARMCPU *cpu = arm_env_get_cpu(env);
4456 int i = ri->crm;
4457
4458 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4459 * copy of BAS[0].
4460 */
4461 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4462 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4463
4464 raw_write(env, ri, value);
4465 hw_breakpoint_update(cpu, i);
4466}
4467
50300698 4468static void define_debug_regs(ARMCPU *cpu)
0b45451e 4469{
50300698
PM
4470 /* Define v7 and v8 architectural debug registers.
4471 * These are just dummy implementations for now.
0b45451e
PM
4472 */
4473 int i;
3ff6fc91 4474 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4475 ARMCPRegInfo dbgdidr = {
4476 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4477 .access = PL0_R, .accessfn = access_tda,
4478 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4479 };
4480
3ff6fc91 4481 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4482 brps = extract32(cpu->dbgdidr, 24, 4);
4483 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4484 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4485
4486 assert(ctx_cmps <= brps);
48eb3ae6
PM
4487
4488 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4489 * of the debug registers such as number of breakpoints;
4490 * check that if they both exist then they agree.
4491 */
4492 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4493 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4494 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4495 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4496 }
0b45451e 4497
48eb3ae6 4498 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4499 define_arm_cp_regs(cpu, debug_cp_reginfo);
4500
4501 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4502 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4503 }
4504
48eb3ae6 4505 for (i = 0; i < brps + 1; i++) {
0b45451e 4506 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4507 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4508 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4509 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4510 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4511 .writefn = dbgbvr_write, .raw_writefn = raw_write
4512 },
10aae104
PM
4513 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4514 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4515 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4516 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4517 .writefn = dbgbcr_write, .raw_writefn = raw_write
4518 },
48eb3ae6
PM
4519 REGINFO_SENTINEL
4520 };
4521 define_arm_cp_regs(cpu, dbgregs);
4522 }
4523
4524 for (i = 0; i < wrps + 1; i++) {
4525 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4526 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4527 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4528 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4529 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4530 .writefn = dbgwvr_write, .raw_writefn = raw_write
4531 },
10aae104
PM
4532 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4533 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4534 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4535 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4536 .writefn = dbgwcr_write, .raw_writefn = raw_write
4537 },
4538 REGINFO_SENTINEL
0b45451e
PM
4539 };
4540 define_arm_cp_regs(cpu, dbgregs);
4541 }
4542}
4543
2ceb98c0
PM
4544void register_cp_regs_for_features(ARMCPU *cpu)
4545{
4546 /* Register all the coprocessor registers based on feature bits */
4547 CPUARMState *env = &cpu->env;
4548 if (arm_feature(env, ARM_FEATURE_M)) {
4549 /* M profile has no coprocessor registers */
4550 return;
4551 }
4552
e9aa6c21 4553 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4554 if (!arm_feature(env, ARM_FEATURE_V8)) {
4555 /* Must go early as it is full of wildcards that may be
4556 * overridden by later definitions.
4557 */
4558 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4559 }
4560
7d57f408 4561 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4562 /* The ID registers all have impdef reset values */
4563 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4564 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4565 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4566 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4567 .resetvalue = cpu->id_pfr0 },
0ff644a7
PM
4568 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4569 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4570 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4571 .resetvalue = cpu->id_pfr1 },
0ff644a7
PM
4572 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4573 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4574 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4575 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4576 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4577 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4578 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4579 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4580 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4581 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4582 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4583 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4584 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4585 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4586 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4587 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4588 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4589 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4590 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4591 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4592 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4593 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4594 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4595 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4596 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4597 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4598 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4599 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4600 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4601 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4602 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4603 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4604 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4605 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4606 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4607 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4608 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4609 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4610 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4611 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4612 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4613 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4614 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4615 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4616 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4617 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4618 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4619 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4620 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4621 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4622 .access = PL1_R, .type = ARM_CP_CONST,
4623 .resetvalue = cpu->id_mmfr4 },
4624 /* 7 is as yet unallocated and must RAZ */
4625 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4626 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4627 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4628 .resetvalue = 0 },
4629 REGINFO_SENTINEL
4630 };
4631 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4632 define_arm_cp_regs(cpu, v6_cp_reginfo);
4633 } else {
4634 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4635 }
4d31c596
PM
4636 if (arm_feature(env, ARM_FEATURE_V6K)) {
4637 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4638 }
5e5cf9e3 4639 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 4640 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
4641 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4642 }
e9aa6c21 4643 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4644 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4645 * field as main ID register, and we implement only the cycle
4646 * count register.
200ac0ef 4647 */
7c2cb42b 4648#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4649 ARMCPRegInfo pmcr = {
4650 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4651 .access = PL0_RW,
7a0e58fa 4652 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4653 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4654 .accessfn = pmreg_access, .writefn = pmcr_write,
4655 .raw_writefn = raw_write,
200ac0ef 4656 };
8521466b
AF
4657 ARMCPRegInfo pmcr64 = {
4658 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4659 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4660 .access = PL0_RW, .accessfn = pmreg_access,
4661 .type = ARM_CP_IO,
4662 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4663 .resetvalue = cpu->midr & 0xff000000,
4664 .writefn = pmcr_write, .raw_writefn = raw_write,
4665 };
7c2cb42b 4666 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4667 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4668#endif
776d4e5c 4669 ARMCPRegInfo clidr = {
7da845b0
PM
4670 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4671 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4672 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4673 };
776d4e5c 4674 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4675 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4676 define_debug_regs(cpu);
7d57f408
PM
4677 } else {
4678 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4679 }
b0d2b7d0 4680 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4681 /* AArch64 ID registers, which all have impdef reset values.
4682 * Note that within the ID register ranges the unused slots
4683 * must all RAZ, not UNDEF; future architecture versions may
4684 * define new registers here.
4685 */
e60cef86
PM
4686 ARMCPRegInfo v8_idregs[] = {
4687 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4688 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4689 .access = PL1_R, .type = ARM_CP_CONST,
4690 .resetvalue = cpu->id_aa64pfr0 },
4691 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4692 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4693 .access = PL1_R, .type = ARM_CP_CONST,
4694 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4695 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4696 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4697 .access = PL1_R, .type = ARM_CP_CONST,
4698 .resetvalue = 0 },
4699 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4700 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4701 .access = PL1_R, .type = ARM_CP_CONST,
4702 .resetvalue = 0 },
4703 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4704 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4705 .access = PL1_R, .type = ARM_CP_CONST,
4706 .resetvalue = 0 },
4707 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4708 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4709 .access = PL1_R, .type = ARM_CP_CONST,
4710 .resetvalue = 0 },
4711 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4712 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4713 .access = PL1_R, .type = ARM_CP_CONST,
4714 .resetvalue = 0 },
4715 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4716 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4717 .access = PL1_R, .type = ARM_CP_CONST,
4718 .resetvalue = 0 },
e60cef86
PM
4719 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4720 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4721 .access = PL1_R, .type = ARM_CP_CONST,
d6f02ce3 4722 .resetvalue = cpu->id_aa64dfr0 },
e60cef86
PM
4723 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4724 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4725 .access = PL1_R, .type = ARM_CP_CONST,
4726 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4727 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4728 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4729 .access = PL1_R, .type = ARM_CP_CONST,
4730 .resetvalue = 0 },
4731 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4732 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4733 .access = PL1_R, .type = ARM_CP_CONST,
4734 .resetvalue = 0 },
e60cef86
PM
4735 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4736 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4737 .access = PL1_R, .type = ARM_CP_CONST,
4738 .resetvalue = cpu->id_aa64afr0 },
4739 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4740 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4741 .access = PL1_R, .type = ARM_CP_CONST,
4742 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4743 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4745 .access = PL1_R, .type = ARM_CP_CONST,
4746 .resetvalue = 0 },
4747 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4748 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4749 .access = PL1_R, .type = ARM_CP_CONST,
4750 .resetvalue = 0 },
e60cef86
PM
4751 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4752 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4753 .access = PL1_R, .type = ARM_CP_CONST,
4754 .resetvalue = cpu->id_aa64isar0 },
4755 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4756 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4757 .access = PL1_R, .type = ARM_CP_CONST,
4758 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4759 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4760 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4761 .access = PL1_R, .type = ARM_CP_CONST,
4762 .resetvalue = 0 },
4763 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4765 .access = PL1_R, .type = ARM_CP_CONST,
4766 .resetvalue = 0 },
4767 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4769 .access = PL1_R, .type = ARM_CP_CONST,
4770 .resetvalue = 0 },
4771 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4773 .access = PL1_R, .type = ARM_CP_CONST,
4774 .resetvalue = 0 },
4775 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4777 .access = PL1_R, .type = ARM_CP_CONST,
4778 .resetvalue = 0 },
4779 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4780 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4781 .access = PL1_R, .type = ARM_CP_CONST,
4782 .resetvalue = 0 },
e60cef86
PM
4783 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4784 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4785 .access = PL1_R, .type = ARM_CP_CONST,
4786 .resetvalue = cpu->id_aa64mmfr0 },
4787 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4789 .access = PL1_R, .type = ARM_CP_CONST,
4790 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
4791 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4793 .access = PL1_R, .type = ARM_CP_CONST,
4794 .resetvalue = 0 },
4795 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4797 .access = PL1_R, .type = ARM_CP_CONST,
4798 .resetvalue = 0 },
4799 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4801 .access = PL1_R, .type = ARM_CP_CONST,
4802 .resetvalue = 0 },
4803 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4805 .access = PL1_R, .type = ARM_CP_CONST,
4806 .resetvalue = 0 },
4807 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4809 .access = PL1_R, .type = ARM_CP_CONST,
4810 .resetvalue = 0 },
4811 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4812 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4813 .access = PL1_R, .type = ARM_CP_CONST,
4814 .resetvalue = 0 },
a50c0f51
PM
4815 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4816 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4817 .access = PL1_R, .type = ARM_CP_CONST,
4818 .resetvalue = cpu->mvfr0 },
4819 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4820 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4821 .access = PL1_R, .type = ARM_CP_CONST,
4822 .resetvalue = cpu->mvfr1 },
4823 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4824 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4825 .access = PL1_R, .type = ARM_CP_CONST,
4826 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
4827 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4829 .access = PL1_R, .type = ARM_CP_CONST,
4830 .resetvalue = 0 },
4831 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4833 .access = PL1_R, .type = ARM_CP_CONST,
4834 .resetvalue = 0 },
4835 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4836 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4837 .access = PL1_R, .type = ARM_CP_CONST,
4838 .resetvalue = 0 },
4839 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4840 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4841 .access = PL1_R, .type = ARM_CP_CONST,
4842 .resetvalue = 0 },
4843 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4844 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4845 .access = PL1_R, .type = ARM_CP_CONST,
4846 .resetvalue = 0 },
4054bfa9
AF
4847 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4848 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4849 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4850 .resetvalue = cpu->pmceid0 },
4851 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4853 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4854 .resetvalue = cpu->pmceid0 },
4855 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4856 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4857 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4858 .resetvalue = cpu->pmceid1 },
4859 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4861 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4862 .resetvalue = cpu->pmceid1 },
e60cef86
PM
4863 REGINFO_SENTINEL
4864 };
be8e8128
GB
4865 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4866 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4867 !arm_feature(env, ARM_FEATURE_EL2)) {
4868 ARMCPRegInfo rvbar = {
4869 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4870 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4871 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4872 };
4873 define_one_arm_cp_reg(cpu, &rvbar);
4874 }
e60cef86 4875 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
4876 define_arm_cp_regs(cpu, v8_cp_reginfo);
4877 }
3b685ba7 4878 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 4879 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
4880 ARMCPRegInfo vpidr_regs[] = {
4881 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4882 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4883 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4884 .resetvalue = cpu->midr,
4885 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4886 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4887 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4888 .access = PL2_RW, .resetvalue = cpu->midr,
4889 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4890 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4891 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4892 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4893 .resetvalue = vmpidr_def,
4894 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4895 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4896 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4897 .access = PL2_RW,
4898 .resetvalue = vmpidr_def,
4899 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
4900 REGINFO_SENTINEL
4901 };
4902 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4903 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
4904 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4905 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4906 ARMCPRegInfo rvbar = {
4907 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4909 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4910 };
4911 define_one_arm_cp_reg(cpu, &rvbar);
4912 }
d42e3c26
EI
4913 } else {
4914 /* If EL2 is missing but higher ELs are enabled, we need to
4915 * register the no_el2 reginfos.
4916 */
4917 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
4918 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4919 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
4920 */
4921 ARMCPRegInfo vpidr_regs[] = {
4922 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4923 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4924 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4925 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4926 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4927 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4928 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4929 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4930 .type = ARM_CP_NO_RAW,
4931 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
4932 REGINFO_SENTINEL
4933 };
4934 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4935 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 4936 }
3b685ba7 4937 }
81547d66 4938 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 4939 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
4940 ARMCPRegInfo el3_regs[] = {
4941 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4942 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4943 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
4944 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
4945 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
4946 .access = PL3_RW,
4947 .raw_writefn = raw_write, .writefn = sctlr_write,
4948 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
4949 .resetvalue = cpu->reset_sctlr },
4950 REGINFO_SENTINEL
be8e8128 4951 };
e24fdd23
PM
4952
4953 define_arm_cp_regs(cpu, el3_regs);
81547d66 4954 }
2f027fc5
PM
4955 /* The behaviour of NSACR is sufficiently various that we don't
4956 * try to describe it in a single reginfo:
4957 * if EL3 is 64 bit, then trap to EL3 from S EL1,
4958 * reads as constant 0xc00 from NS EL1 and NS EL2
4959 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
4960 * if v7 without EL3, register doesn't exist
4961 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
4962 */
4963 if (arm_feature(env, ARM_FEATURE_EL3)) {
4964 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4965 ARMCPRegInfo nsacr = {
4966 .name = "NSACR", .type = ARM_CP_CONST,
4967 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4968 .access = PL1_RW, .accessfn = nsacr_access,
4969 .resetvalue = 0xc00
4970 };
4971 define_one_arm_cp_reg(cpu, &nsacr);
4972 } else {
4973 ARMCPRegInfo nsacr = {
4974 .name = "NSACR",
4975 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4976 .access = PL3_RW | PL1_R,
4977 .resetvalue = 0,
4978 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
4979 };
4980 define_one_arm_cp_reg(cpu, &nsacr);
4981 }
4982 } else {
4983 if (arm_feature(env, ARM_FEATURE_V8)) {
4984 ARMCPRegInfo nsacr = {
4985 .name = "NSACR", .type = ARM_CP_CONST,
4986 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4987 .access = PL1_R,
4988 .resetvalue = 0xc00
4989 };
4990 define_one_arm_cp_reg(cpu, &nsacr);
4991 }
4992 }
4993
452a0955 4994 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
4995 if (arm_feature(env, ARM_FEATURE_V6)) {
4996 /* PMSAv6 not implemented */
4997 assert(arm_feature(env, ARM_FEATURE_V7));
4998 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4999 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5000 } else {
5001 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5002 }
18032bec 5003 } else {
8e5d75c9 5004 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
5005 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5006 }
c326b979
PM
5007 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5008 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5009 }
6cc7a3ae
PM
5010 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5011 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5012 }
4a501606
PM
5013 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5014 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5015 }
c4804214
PM
5016 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5017 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5018 }
5019 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5020 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5021 }
5022 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5023 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5024 }
18032bec
PM
5025 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5026 define_arm_cp_regs(cpu, omap_cp_reginfo);
5027 }
34f90529
PM
5028 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5029 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5030 }
1047b9d7
PM
5031 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5032 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5033 }
5034 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5035 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5036 }
7ac681cf
PM
5037 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5038 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5039 }
7884849c
PM
5040 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5041 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5042 * be read-only (ie write causes UNDEF exception).
5043 */
5044 {
00a29f3d
PM
5045 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5046 /* Pre-v8 MIDR space.
5047 * Note that the MIDR isn't a simple constant register because
7884849c
PM
5048 * of the TI925 behaviour where writes to another register can
5049 * cause the MIDR value to change.
97ce8d61
PC
5050 *
5051 * Unimplemented registers in the c15 0 0 0 space default to
5052 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5053 * and friends override accordingly.
7884849c
PM
5054 */
5055 { .name = "MIDR",
97ce8d61 5056 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 5057 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 5058 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 5059 .readfn = midr_read,
97ce8d61
PC
5060 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5061 .type = ARM_CP_OVERRIDE },
7884849c
PM
5062 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5063 { .name = "DUMMY",
5064 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5065 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5066 { .name = "DUMMY",
5067 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5068 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5069 { .name = "DUMMY",
5070 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5071 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5072 { .name = "DUMMY",
5073 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5074 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5075 { .name = "DUMMY",
5076 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5077 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5078 REGINFO_SENTINEL
5079 };
00a29f3d 5080 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
5081 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5082 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
5083 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5084 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5085 .readfn = midr_read },
ac00c79f
SF
5086 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5087 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5088 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5089 .access = PL1_R, .resetvalue = cpu->midr },
5090 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5091 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5092 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
5093 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5094 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 5095 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
5096 REGINFO_SENTINEL
5097 };
5098 ARMCPRegInfo id_cp_reginfo[] = {
5099 /* These are common to v8 and pre-v8 */
5100 { .name = "CTR",
5101 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5102 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5103 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5104 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5105 .access = PL0_R, .accessfn = ctr_el0_access,
5106 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5107 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5108 { .name = "TCMTR",
5109 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5110 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
5111 REGINFO_SENTINEL
5112 };
8085ce63
PC
5113 /* TLBTR is specific to VMSA */
5114 ARMCPRegInfo id_tlbtr_reginfo = {
5115 .name = "TLBTR",
5116 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5117 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5118 };
3281af81
PC
5119 /* MPUIR is specific to PMSA V6+ */
5120 ARMCPRegInfo id_mpuir_reginfo = {
5121 .name = "MPUIR",
5122 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5123 .access = PL1_R, .type = ARM_CP_CONST,
5124 .resetvalue = cpu->pmsav7_dregion << 8
5125 };
7884849c
PM
5126 ARMCPRegInfo crn0_wi_reginfo = {
5127 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5128 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5129 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5130 };
5131 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5132 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5133 ARMCPRegInfo *r;
5134 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
5135 * whole space. Then update the specific ID registers to allow write
5136 * access, so that they ignore writes rather than causing them to
5137 * UNDEF.
7884849c
PM
5138 */
5139 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
5140 for (r = id_pre_v8_midr_cp_reginfo;
5141 r->type != ARM_CP_SENTINEL; r++) {
5142 r->access = PL1_RW;
5143 }
7884849c
PM
5144 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5145 r->access = PL1_RW;
7884849c 5146 }
8085ce63 5147 id_tlbtr_reginfo.access = PL1_RW;
3281af81 5148 id_tlbtr_reginfo.access = PL1_RW;
7884849c 5149 }
00a29f3d
PM
5150 if (arm_feature(env, ARM_FEATURE_V8)) {
5151 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5152 } else {
5153 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5154 }
a703eda1 5155 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 5156 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 5157 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
5158 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5159 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 5160 }
7884849c
PM
5161 }
5162
97ce8d61
PC
5163 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5164 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5165 }
5166
2771db27 5167 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
5168 ARMCPRegInfo auxcr_reginfo[] = {
5169 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5170 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5171 .access = PL1_RW, .type = ARM_CP_CONST,
5172 .resetvalue = cpu->reset_auxcr },
5173 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5174 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5175 .access = PL2_RW, .type = ARM_CP_CONST,
5176 .resetvalue = 0 },
5177 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5178 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5179 .access = PL3_RW, .type = ARM_CP_CONST,
5180 .resetvalue = 0 },
5181 REGINFO_SENTINEL
2771db27 5182 };
834a6c69 5183 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
5184 }
5185
d8ba780b 5186 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
5187 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5188 /* 32 bit view is [31:18] 0...0 [43:32]. */
5189 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5190 | extract64(cpu->reset_cbar, 32, 12);
5191 ARMCPRegInfo cbar_reginfo[] = {
5192 { .name = "CBAR",
5193 .type = ARM_CP_CONST,
5194 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5195 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5196 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5197 .type = ARM_CP_CONST,
5198 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5199 .access = PL1_R, .resetvalue = cbar32 },
5200 REGINFO_SENTINEL
5201 };
5202 /* We don't implement a r/w 64 bit CBAR currently */
5203 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5204 define_arm_cp_regs(cpu, cbar_reginfo);
5205 } else {
5206 ARMCPRegInfo cbar = {
5207 .name = "CBAR",
5208 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5209 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5210 .fieldoffset = offsetof(CPUARMState,
5211 cp15.c15_config_base_address)
5212 };
5213 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5214 cbar.access = PL1_R;
5215 cbar.fieldoffset = 0;
5216 cbar.type = ARM_CP_CONST;
5217 }
5218 define_one_arm_cp_reg(cpu, &cbar);
5219 }
d8ba780b
PC
5220 }
5221
91db4642
CLG
5222 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5223 ARMCPRegInfo vbar_cp_reginfo[] = {
5224 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5225 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5226 .access = PL1_RW, .writefn = vbar_write,
5227 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5228 offsetof(CPUARMState, cp15.vbar_ns) },
5229 .resetvalue = 0 },
5230 REGINFO_SENTINEL
5231 };
5232 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5233 }
5234
2771db27
PM
5235 /* Generic registers whose values depend on the implementation */
5236 {
5237 ARMCPRegInfo sctlr = {
5ebafdf3 5238 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
5239 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5240 .access = PL1_RW,
5241 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5242 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
5243 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5244 .raw_writefn = raw_write,
2771db27
PM
5245 };
5246 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5247 /* Normally we would always end the TB on an SCTLR write, but Linux
5248 * arch/arm/mach-pxa/sleep.S expects two instructions following
5249 * an MMU enable to execute from cache. Imitate this behaviour.
5250 */
5251 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5252 }
5253 define_one_arm_cp_reg(cpu, &sctlr);
5254 }
2ceb98c0
PM
5255}
5256
14969266
AF
5257void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5258{
22169d41 5259 CPUState *cs = CPU(cpu);
14969266
AF
5260 CPUARMState *env = &cpu->env;
5261
6a669427
PM
5262 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5263 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5264 aarch64_fpu_gdb_set_reg,
5265 34, "aarch64-fpu.xml", 0);
5266 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 5267 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5268 51, "arm-neon.xml", 0);
5269 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 5270 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5271 35, "arm-vfp3.xml", 0);
5272 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 5273 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
5274 19, "arm-vfp.xml", 0);
5275 }
40f137e1
PB
5276}
5277
777dc784
PM
5278/* Sort alphabetically by type name, except for "any". */
5279static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 5280{
777dc784
PM
5281 ObjectClass *class_a = (ObjectClass *)a;
5282 ObjectClass *class_b = (ObjectClass *)b;
5283 const char *name_a, *name_b;
5adb4839 5284
777dc784
PM
5285 name_a = object_class_get_name(class_a);
5286 name_b = object_class_get_name(class_b);
51492fd1 5287 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 5288 return 1;
51492fd1 5289 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
5290 return -1;
5291 } else {
5292 return strcmp(name_a, name_b);
5adb4839
PB
5293 }
5294}
5295
777dc784 5296static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 5297{
777dc784 5298 ObjectClass *oc = data;
92a31361 5299 CPUListState *s = user_data;
51492fd1
AF
5300 const char *typename;
5301 char *name;
3371d272 5302
51492fd1
AF
5303 typename = object_class_get_name(oc);
5304 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 5305 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
5306 name);
5307 g_free(name);
777dc784
PM
5308}
5309
5310void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5311{
92a31361 5312 CPUListState s = {
777dc784
PM
5313 .file = f,
5314 .cpu_fprintf = cpu_fprintf,
5315 };
5316 GSList *list;
5317
5318 list = object_class_get_list(TYPE_ARM_CPU, false);
5319 list = g_slist_sort(list, arm_cpu_list_compare);
5320 (*cpu_fprintf)(f, "Available CPUs:\n");
5321 g_slist_foreach(list, arm_cpu_list_entry, &s);
5322 g_slist_free(list);
a96c0514
PM
5323#ifdef CONFIG_KVM
5324 /* The 'host' CPU type is dynamically registered only if KVM is
5325 * enabled, so we have to special-case it here:
5326 */
5327 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5328#endif
40f137e1
PB
5329}
5330
78027bb6
CR
5331static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5332{
5333 ObjectClass *oc = data;
5334 CpuDefinitionInfoList **cpu_list = user_data;
5335 CpuDefinitionInfoList *entry;
5336 CpuDefinitionInfo *info;
5337 const char *typename;
5338
5339 typename = object_class_get_name(oc);
5340 info = g_malloc0(sizeof(*info));
5341 info->name = g_strndup(typename,
5342 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 5343 info->q_typename = g_strdup(typename);
78027bb6
CR
5344
5345 entry = g_malloc0(sizeof(*entry));
5346 entry->value = info;
5347 entry->next = *cpu_list;
5348 *cpu_list = entry;
5349}
5350
5351CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5352{
5353 CpuDefinitionInfoList *cpu_list = NULL;
5354 GSList *list;
5355
5356 list = object_class_get_list(TYPE_ARM_CPU, false);
5357 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5358 g_slist_free(list);
5359
5360 return cpu_list;
5361}
5362
6e6efd61 5363static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5364 void *opaque, int state, int secstate,
f5a0a5a5 5365 int crm, int opc1, int opc2)
6e6efd61
PM
5366{
5367 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5368 * add a single reginfo struct to the hash table.
5369 */
5370 uint32_t *key = g_new(uint32_t, 1);
5371 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5372 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5373 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5374
5375 /* Reset the secure state to the specific incoming state. This is
5376 * necessary as the register may have been defined with both states.
5377 */
5378 r2->secure = secstate;
5379
5380 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5381 /* Register is banked (using both entries in array).
5382 * Overwriting fieldoffset as the array is only used to define
5383 * banked registers but later only fieldoffset is used.
f5a0a5a5 5384 */
3f3c82a5
FA
5385 r2->fieldoffset = r->bank_fieldoffsets[ns];
5386 }
5387
5388 if (state == ARM_CP_STATE_AA32) {
5389 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5390 /* If the register is banked then we don't need to migrate or
5391 * reset the 32-bit instance in certain cases:
5392 *
5393 * 1) If the register has both 32-bit and 64-bit instances then we
5394 * can count on the 64-bit instance taking care of the
5395 * non-secure bank.
5396 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5397 * taking care of the secure bank. This requires that separate
5398 * 32 and 64-bit definitions are provided.
5399 */
5400 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5401 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5402 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5403 }
5404 } else if ((secstate != r->secure) && !ns) {
5405 /* The register is not banked so we only want to allow migration of
5406 * the non-secure instance.
5407 */
7a0e58fa 5408 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5409 }
3f3c82a5
FA
5410
5411 if (r->state == ARM_CP_STATE_BOTH) {
5412 /* We assume it is a cp15 register if the .cp field is left unset.
5413 */
5414 if (r2->cp == 0) {
5415 r2->cp = 15;
5416 }
5417
f5a0a5a5 5418#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5419 if (r2->fieldoffset) {
5420 r2->fieldoffset += sizeof(uint32_t);
5421 }
f5a0a5a5 5422#endif
3f3c82a5 5423 }
f5a0a5a5
PM
5424 }
5425 if (state == ARM_CP_STATE_AA64) {
5426 /* To allow abbreviation of ARMCPRegInfo
5427 * definitions, we treat cp == 0 as equivalent to
5428 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5429 * STATE_BOTH definitions are also always "standard
5430 * sysreg" in their AArch64 view (the .cp value may
5431 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5432 */
58a1d8ce 5433 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5434 r2->cp = CP_REG_ARM64_SYSREG_CP;
5435 }
5436 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5437 r2->opc0, opc1, opc2);
5438 } else {
51a79b03 5439 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5440 }
6e6efd61
PM
5441 if (opaque) {
5442 r2->opaque = opaque;
5443 }
67ed771d
PM
5444 /* reginfo passed to helpers is correct for the actual access,
5445 * and is never ARM_CP_STATE_BOTH:
5446 */
5447 r2->state = state;
6e6efd61
PM
5448 /* Make sure reginfo passed to helpers for wildcarded regs
5449 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5450 */
5451 r2->crm = crm;
5452 r2->opc1 = opc1;
5453 r2->opc2 = opc2;
5454 /* By convention, for wildcarded registers only the first
5455 * entry is used for migration; the others are marked as
7a0e58fa 5456 * ALIAS so we don't try to transfer the register
6e6efd61 5457 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5458 * never migratable and not even raw-accessible.
6e6efd61 5459 */
7a0e58fa
PM
5460 if ((r->type & ARM_CP_SPECIAL)) {
5461 r2->type |= ARM_CP_NO_RAW;
5462 }
5463 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5464 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5465 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 5466 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
5467 }
5468
375421cc
PM
5469 /* Check that raw accesses are either forbidden or handled. Note that
5470 * we can't assert this earlier because the setup of fieldoffset for
5471 * banked registers has to be done first.
5472 */
5473 if (!(r2->type & ARM_CP_NO_RAW)) {
5474 assert(!raw_accessors_invalid(r2));
5475 }
5476
6e6efd61
PM
5477 /* Overriding of an existing definition must be explicitly
5478 * requested.
5479 */
5480 if (!(r->type & ARM_CP_OVERRIDE)) {
5481 ARMCPRegInfo *oldreg;
5482 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5483 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5484 fprintf(stderr, "Register redefined: cp=%d %d bit "
5485 "crn=%d crm=%d opc1=%d opc2=%d, "
5486 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5487 r2->crn, r2->crm, r2->opc1, r2->opc2,
5488 oldreg->name, r2->name);
5489 g_assert_not_reached();
5490 }
5491 }
5492 g_hash_table_insert(cpu->cp_regs, key, r2);
5493}
5494
5495
4b6a83fb
PM
5496void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5497 const ARMCPRegInfo *r, void *opaque)
5498{
5499 /* Define implementations of coprocessor registers.
5500 * We store these in a hashtable because typically
5501 * there are less than 150 registers in a space which
5502 * is 16*16*16*8*8 = 262144 in size.
5503 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5504 * If a register is defined twice then the second definition is
5505 * used, so this can be used to define some generic registers and
5506 * then override them with implementation specific variations.
5507 * At least one of the original and the second definition should
5508 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5509 * against accidental use.
f5a0a5a5
PM
5510 *
5511 * The state field defines whether the register is to be
5512 * visible in the AArch32 or AArch64 execution state. If the
5513 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5514 * reginfo structure for the AArch32 view, which sees the lower
5515 * 32 bits of the 64 bit register.
5516 *
5517 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5518 * be wildcarded. AArch64 registers are always considered to be 64
5519 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5520 * the register, if any.
4b6a83fb 5521 */
f5a0a5a5 5522 int crm, opc1, opc2, state;
4b6a83fb
PM
5523 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5524 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5525 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5526 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5527 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5528 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5529 /* 64 bit registers have only CRm and Opc1 fields */
5530 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5531 /* op0 only exists in the AArch64 encodings */
5532 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5533 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5534 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5535 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5536 * encodes a minimum access level for the register. We roll this
5537 * runtime check into our general permission check code, so check
5538 * here that the reginfo's specified permissions are strict enough
5539 * to encompass the generic architectural permission check.
5540 */
5541 if (r->state != ARM_CP_STATE_AA32) {
5542 int mask = 0;
5543 switch (r->opc1) {
5544 case 0: case 1: case 2:
5545 /* min_EL EL1 */
5546 mask = PL1_RW;
5547 break;
5548 case 3:
5549 /* min_EL EL0 */
5550 mask = PL0_RW;
5551 break;
5552 case 4:
5553 /* min_EL EL2 */
5554 mask = PL2_RW;
5555 break;
5556 case 5:
5557 /* unallocated encoding, so not possible */
5558 assert(false);
5559 break;
5560 case 6:
5561 /* min_EL EL3 */
5562 mask = PL3_RW;
5563 break;
5564 case 7:
5565 /* min_EL EL1, secure mode only (we don't check the latter) */
5566 mask = PL1_RW;
5567 break;
5568 default:
5569 /* broken reginfo with out-of-range opc1 */
5570 assert(false);
5571 break;
5572 }
5573 /* assert our permissions are not too lax (stricter is fine) */
5574 assert((r->access & ~mask) == 0);
5575 }
5576
4b6a83fb
PM
5577 /* Check that the register definition has enough info to handle
5578 * reads and writes if they are permitted.
5579 */
5580 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5581 if (r->access & PL3_R) {
3f3c82a5
FA
5582 assert((r->fieldoffset ||
5583 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5584 r->readfn);
4b6a83fb
PM
5585 }
5586 if (r->access & PL3_W) {
3f3c82a5
FA
5587 assert((r->fieldoffset ||
5588 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5589 r->writefn);
4b6a83fb
PM
5590 }
5591 }
5592 /* Bad type field probably means missing sentinel at end of reg list */
5593 assert(cptype_valid(r->type));
5594 for (crm = crmmin; crm <= crmmax; crm++) {
5595 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5596 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5597 for (state = ARM_CP_STATE_AA32;
5598 state <= ARM_CP_STATE_AA64; state++) {
5599 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5600 continue;
5601 }
3f3c82a5
FA
5602 if (state == ARM_CP_STATE_AA32) {
5603 /* Under AArch32 CP registers can be common
5604 * (same for secure and non-secure world) or banked.
5605 */
5606 switch (r->secure) {
5607 case ARM_CP_SECSTATE_S:
5608 case ARM_CP_SECSTATE_NS:
5609 add_cpreg_to_hashtable(cpu, r, opaque, state,
5610 r->secure, crm, opc1, opc2);
5611 break;
5612 default:
5613 add_cpreg_to_hashtable(cpu, r, opaque, state,
5614 ARM_CP_SECSTATE_S,
5615 crm, opc1, opc2);
5616 add_cpreg_to_hashtable(cpu, r, opaque, state,
5617 ARM_CP_SECSTATE_NS,
5618 crm, opc1, opc2);
5619 break;
5620 }
5621 } else {
5622 /* AArch64 registers get mapped to non-secure instance
5623 * of AArch32 */
5624 add_cpreg_to_hashtable(cpu, r, opaque, state,
5625 ARM_CP_SECSTATE_NS,
5626 crm, opc1, opc2);
5627 }
f5a0a5a5 5628 }
4b6a83fb
PM
5629 }
5630 }
5631 }
5632}
5633
5634void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5635 const ARMCPRegInfo *regs, void *opaque)
5636{
5637 /* Define a whole list of registers */
5638 const ARMCPRegInfo *r;
5639 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5640 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5641 }
5642}
5643
60322b39 5644const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5645{
60322b39 5646 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5647}
5648
c4241c7d
PM
5649void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5650 uint64_t value)
4b6a83fb
PM
5651{
5652 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5653}
5654
c4241c7d 5655uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5656{
5657 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5658 return 0;
5659}
5660
f5a0a5a5
PM
5661void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5662{
5663 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5664}
5665
af393ffc 5666static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5667{
5668 /* Return true if it is not valid for us to switch to
5669 * this CPU mode (ie all the UNPREDICTABLE cases in
5670 * the ARM ARM CPSRWriteByInstr pseudocode).
5671 */
af393ffc
PM
5672
5673 /* Changes to or from Hyp via MSR and CPS are illegal. */
5674 if (write_type == CPSRWriteByInstr &&
5675 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5676 mode == ARM_CPU_MODE_HYP)) {
5677 return 1;
5678 }
5679
37064a8b
PM
5680 switch (mode) {
5681 case ARM_CPU_MODE_USR:
10eacda7 5682 return 0;
37064a8b
PM
5683 case ARM_CPU_MODE_SYS:
5684 case ARM_CPU_MODE_SVC:
5685 case ARM_CPU_MODE_ABT:
5686 case ARM_CPU_MODE_UND:
5687 case ARM_CPU_MODE_IRQ:
5688 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5689 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5690 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5691 */
10eacda7
PM
5692 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5693 * and CPS are treated as illegal mode changes.
5694 */
5695 if (write_type == CPSRWriteByInstr &&
5696 (env->cp15.hcr_el2 & HCR_TGE) &&
5697 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5698 !arm_is_secure_below_el3(env)) {
5699 return 1;
5700 }
37064a8b 5701 return 0;
e6c8fc07
PM
5702 case ARM_CPU_MODE_HYP:
5703 return !arm_feature(env, ARM_FEATURE_EL2)
5704 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5705 case ARM_CPU_MODE_MON:
58ae2d1f 5706 return arm_current_el(env) < 3;
37064a8b
PM
5707 default:
5708 return 1;
5709 }
5710}
5711
2f4a40e5
AZ
5712uint32_t cpsr_read(CPUARMState *env)
5713{
5714 int ZF;
6fbe23d5
PB
5715 ZF = (env->ZF == 0);
5716 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5717 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5718 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5719 | ((env->condexec_bits & 0xfc) << 8)
af519934 5720 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5721}
5722
50866ba5
PM
5723void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5724 CPSRWriteType write_type)
2f4a40e5 5725{
6e8801f9
FA
5726 uint32_t changed_daif;
5727
2f4a40e5 5728 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5729 env->ZF = (~val) & CPSR_Z;
5730 env->NF = val;
2f4a40e5
AZ
5731 env->CF = (val >> 29) & 1;
5732 env->VF = (val << 3) & 0x80000000;
5733 }
5734 if (mask & CPSR_Q)
5735 env->QF = ((val & CPSR_Q) != 0);
5736 if (mask & CPSR_T)
5737 env->thumb = ((val & CPSR_T) != 0);
5738 if (mask & CPSR_IT_0_1) {
5739 env->condexec_bits &= ~3;
5740 env->condexec_bits |= (val >> 25) & 3;
5741 }
5742 if (mask & CPSR_IT_2_7) {
5743 env->condexec_bits &= 3;
5744 env->condexec_bits |= (val >> 8) & 0xfc;
5745 }
5746 if (mask & CPSR_GE) {
5747 env->GE = (val >> 16) & 0xf;
5748 }
5749
6e8801f9
FA
5750 /* In a V7 implementation that includes the security extensions but does
5751 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5752 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5753 * bits respectively.
5754 *
5755 * In a V8 implementation, it is permitted for privileged software to
5756 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5757 */
f8c88bbc 5758 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
5759 arm_feature(env, ARM_FEATURE_EL3) &&
5760 !arm_feature(env, ARM_FEATURE_EL2) &&
5761 !arm_is_secure(env)) {
5762
5763 changed_daif = (env->daif ^ val) & mask;
5764
5765 if (changed_daif & CPSR_A) {
5766 /* Check to see if we are allowed to change the masking of async
5767 * abort exceptions from a non-secure state.
5768 */
5769 if (!(env->cp15.scr_el3 & SCR_AW)) {
5770 qemu_log_mask(LOG_GUEST_ERROR,
5771 "Ignoring attempt to switch CPSR_A flag from "
5772 "non-secure world with SCR.AW bit clear\n");
5773 mask &= ~CPSR_A;
5774 }
5775 }
5776
5777 if (changed_daif & CPSR_F) {
5778 /* Check to see if we are allowed to change the masking of FIQ
5779 * exceptions from a non-secure state.
5780 */
5781 if (!(env->cp15.scr_el3 & SCR_FW)) {
5782 qemu_log_mask(LOG_GUEST_ERROR,
5783 "Ignoring attempt to switch CPSR_F flag from "
5784 "non-secure world with SCR.FW bit clear\n");
5785 mask &= ~CPSR_F;
5786 }
5787
5788 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5789 * If this bit is set software is not allowed to mask
5790 * FIQs, but is allowed to set CPSR_F to 0.
5791 */
5792 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5793 (val & CPSR_F)) {
5794 qemu_log_mask(LOG_GUEST_ERROR,
5795 "Ignoring attempt to enable CPSR_F flag "
5796 "(non-maskable FIQ [NMFI] support enabled)\n");
5797 mask &= ~CPSR_F;
5798 }
5799 }
5800 }
5801
4cc35614
PM
5802 env->daif &= ~(CPSR_AIF & mask);
5803 env->daif |= val & CPSR_AIF & mask;
5804
f8c88bbc
PM
5805 if (write_type != CPSRWriteRaw &&
5806 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
5807 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5808 /* Note that we can only get here in USR mode if this is a
5809 * gdb stub write; for this case we follow the architectural
5810 * behaviour for guest writes in USR mode of ignoring an attempt
5811 * to switch mode. (Those are caught by translate.c for writes
5812 * triggered by guest instructions.)
5813 */
5814 mask &= ~CPSR_M;
5815 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
5816 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5817 * v7, and has defined behaviour in v8:
5818 * + leave CPSR.M untouched
5819 * + allow changes to the other CPSR fields
5820 * + set PSTATE.IL
5821 * For user changes via the GDB stub, we don't set PSTATE.IL,
5822 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
5823 */
5824 mask &= ~CPSR_M;
81907a58
PM
5825 if (write_type != CPSRWriteByGDBStub &&
5826 arm_feature(env, ARM_FEATURE_V8)) {
5827 mask |= CPSR_IL;
5828 val |= CPSR_IL;
5829 }
37064a8b
PM
5830 } else {
5831 switch_mode(env, val & CPSR_M);
5832 }
2f4a40e5
AZ
5833 }
5834 mask &= ~CACHED_CPSR_BITS;
5835 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5836}
5837
b26eefb6
PB
5838/* Sign/zero extend */
5839uint32_t HELPER(sxtb16)(uint32_t x)
5840{
5841 uint32_t res;
5842 res = (uint16_t)(int8_t)x;
5843 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5844 return res;
5845}
5846
5847uint32_t HELPER(uxtb16)(uint32_t x)
5848{
5849 uint32_t res;
5850 res = (uint16_t)(uint8_t)x;
5851 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5852 return res;
5853}
5854
3670669c
PB
5855int32_t HELPER(sdiv)(int32_t num, int32_t den)
5856{
5857 if (den == 0)
5858 return 0;
686eeb93
AJ
5859 if (num == INT_MIN && den == -1)
5860 return INT_MIN;
3670669c
PB
5861 return num / den;
5862}
5863
5864uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5865{
5866 if (den == 0)
5867 return 0;
5868 return num / den;
5869}
5870
5871uint32_t HELPER(rbit)(uint32_t x)
5872{
42fedbca 5873 return revbit32(x);
3670669c
PB
5874}
5875
5fafdf24 5876#if defined(CONFIG_USER_ONLY)
b5ff1b31 5877
9ee6e8bb 5878/* These should probably raise undefined insn exceptions. */
0ecb72a5 5879void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 5880{
a47dddd7
AF
5881 ARMCPU *cpu = arm_env_get_cpu(env);
5882
5883 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
5884}
5885
0ecb72a5 5886uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 5887{
a47dddd7
AF
5888 ARMCPU *cpu = arm_env_get_cpu(env);
5889
5890 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
5891 return 0;
5892}
5893
fb602cb7
PM
5894void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
5895{
5896 /* translate.c should never generate calls here in user-only mode */
5897 g_assert_not_reached();
5898}
5899
3e3fa230
PM
5900void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
5901{
5902 /* translate.c should never generate calls here in user-only mode */
5903 g_assert_not_reached();
5904}
5905
0ecb72a5 5906void switch_mode(CPUARMState *env, int mode)
b5ff1b31 5907{
a47dddd7
AF
5908 ARMCPU *cpu = arm_env_get_cpu(env);
5909
5910 if (mode != ARM_CPU_MODE_USR) {
5911 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5912 }
b5ff1b31
FB
5913}
5914
012a906b
GB
5915uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5916 uint32_t cur_el, bool secure)
9e729b57
EI
5917{
5918 return 1;
5919}
5920
ce02049d
GB
5921void aarch64_sync_64_to_32(CPUARMState *env)
5922{
5923 g_assert_not_reached();
5924}
5925
b5ff1b31
FB
5926#else
5927
0ecb72a5 5928void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
5929{
5930 int old_mode;
5931 int i;
5932
5933 old_mode = env->uncached_cpsr & CPSR_M;
5934 if (mode == old_mode)
5935 return;
5936
5937 if (old_mode == ARM_CPU_MODE_FIQ) {
5938 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5939 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5940 } else if (mode == ARM_CPU_MODE_FIQ) {
5941 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5942 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5943 }
5944
f5206413 5945 i = bank_number(old_mode);
b5ff1b31
FB
5946 env->banked_r13[i] = env->regs[13];
5947 env->banked_r14[i] = env->regs[14];
5948 env->banked_spsr[i] = env->spsr;
5949
f5206413 5950 i = bank_number(mode);
b5ff1b31
FB
5951 env->regs[13] = env->banked_r13[i];
5952 env->regs[14] = env->banked_r14[i];
5953 env->spsr = env->banked_spsr[i];
5954}
5955
0eeb17d6
GB
5956/* Physical Interrupt Target EL Lookup Table
5957 *
5958 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5959 *
5960 * The below multi-dimensional table is used for looking up the target
5961 * exception level given numerous condition criteria. Specifically, the
5962 * target EL is based on SCR and HCR routing controls as well as the
5963 * currently executing EL and secure state.
5964 *
5965 * Dimensions:
5966 * target_el_table[2][2][2][2][2][4]
5967 * | | | | | +--- Current EL
5968 * | | | | +------ Non-secure(0)/Secure(1)
5969 * | | | +--------- HCR mask override
5970 * | | +------------ SCR exec state control
5971 * | +--------------- SCR mask override
5972 * +------------------ 32-bit(0)/64-bit(1) EL3
5973 *
5974 * The table values are as such:
5975 * 0-3 = EL0-EL3
5976 * -1 = Cannot occur
5977 *
5978 * The ARM ARM target EL table includes entries indicating that an "exception
5979 * is not taken". The two cases where this is applicable are:
5980 * 1) An exception is taken from EL3 but the SCR does not have the exception
5981 * routed to EL3.
5982 * 2) An exception is taken from EL2 but the HCR does not have the exception
5983 * routed to EL2.
5984 * In these two cases, the below table contain a target of EL1. This value is
5985 * returned as it is expected that the consumer of the table data will check
5986 * for "target EL >= current EL" to ensure the exception is not taken.
5987 *
5988 * SCR HCR
5989 * 64 EA AMO From
5990 * BIT IRQ IMO Non-secure Secure
5991 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
5992 */
82c39f6a 5993static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
5994 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5995 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
5996 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5997 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
5998 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5999 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6000 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6001 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6002 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6003 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6004 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6005 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6006 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6007 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6008 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6009 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6010};
6011
6012/*
6013 * Determine the target EL for physical exceptions
6014 */
012a906b
GB
6015uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6016 uint32_t cur_el, bool secure)
0eeb17d6
GB
6017{
6018 CPUARMState *env = cs->env_ptr;
2cde031f 6019 int rw;
0eeb17d6
GB
6020 int scr;
6021 int hcr;
6022 int target_el;
2cde031f
SS
6023 /* Is the highest EL AArch64? */
6024 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6025
6026 if (arm_feature(env, ARM_FEATURE_EL3)) {
6027 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6028 } else {
6029 /* Either EL2 is the highest EL (and so the EL2 register width
6030 * is given by is64); or there is no EL2 or EL3, in which case
6031 * the value of 'rw' does not affect the table lookup anyway.
6032 */
6033 rw = is64;
6034 }
0eeb17d6
GB
6035
6036 switch (excp_idx) {
6037 case EXCP_IRQ:
6038 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6039 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6040 break;
6041 case EXCP_FIQ:
6042 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6043 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6044 break;
6045 default:
6046 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6047 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6048 break;
6049 };
6050
6051 /* If HCR.TGE is set then HCR is treated as being 1 */
6052 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6053
6054 /* Perform a table-lookup for the target EL given the current state */
6055 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6056
6057 assert(target_el > 0);
6058
6059 return target_el;
6060}
6061
9ee6e8bb
PB
6062static void v7m_push(CPUARMState *env, uint32_t val)
6063{
70d74660
AF
6064 CPUState *cs = CPU(arm_env_get_cpu(env));
6065
9ee6e8bb 6066 env->regs[13] -= 4;
ab1da857 6067 stl_phys(cs->as, env->regs[13], val);
9ee6e8bb
PB
6068}
6069
fb602cb7
PM
6070/* Return true if we're using the process stack pointer (not the MSP) */
6071static bool v7m_using_psp(CPUARMState *env)
6072{
6073 /* Handler mode always uses the main stack; for thread mode
6074 * the CONTROL.SPSEL bit determines the answer.
6075 * Note that in v7M it is not possible to be in Handler mode with
6076 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6077 */
6078 return !arm_v7m_is_handler_mode(env) &&
6079 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6080}
6081
3f0cddee
PM
6082/* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6083 * This may change the current stack pointer between Main and Process
6084 * stack pointers if it is done for the CONTROL register for the current
6085 * security state.
de2db7ec 6086 */
3f0cddee
PM
6087static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6088 bool new_spsel,
6089 bool secstate)
9ee6e8bb 6090{
3f0cddee 6091 bool old_is_psp = v7m_using_psp(env);
de2db7ec 6092
3f0cddee
PM
6093 env->v7m.control[secstate] =
6094 deposit32(env->v7m.control[secstate],
de2db7ec
PM
6095 R_V7M_CONTROL_SPSEL_SHIFT,
6096 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6097
3f0cddee
PM
6098 if (secstate == env->v7m.secure) {
6099 bool new_is_psp = v7m_using_psp(env);
6100 uint32_t tmp;
abc24d86 6101
3f0cddee
PM
6102 if (old_is_psp != new_is_psp) {
6103 tmp = env->v7m.other_sp;
6104 env->v7m.other_sp = env->regs[13];
6105 env->regs[13] = tmp;
6106 }
de2db7ec
PM
6107 }
6108}
6109
3f0cddee
PM
6110/* Write to v7M CONTROL.SPSEL bit. This may change the current
6111 * stack pointer between Main and Process stack pointers.
6112 */
6113static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6114{
6115 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6116}
6117
de2db7ec
PM
6118void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6119{
6120 /* Write a new value to v7m.exception, thus transitioning into or out
6121 * of Handler mode; this may result in a change of active stack pointer.
6122 */
6123 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6124 uint32_t tmp;
abc24d86 6125
de2db7ec
PM
6126 env->v7m.exception = new_exc;
6127
6128 new_is_psp = v7m_using_psp(env);
6129
6130 if (old_is_psp != new_is_psp) {
6131 tmp = env->v7m.other_sp;
6132 env->v7m.other_sp = env->regs[13];
6133 env->regs[13] = tmp;
9ee6e8bb
PB
6134 }
6135}
6136
fb602cb7
PM
6137/* Switch M profile security state between NS and S */
6138static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6139{
6140 uint32_t new_ss_msp, new_ss_psp;
6141
6142 if (env->v7m.secure == new_secstate) {
6143 return;
6144 }
6145
6146 /* All the banked state is accessed by looking at env->v7m.secure
6147 * except for the stack pointer; rearrange the SP appropriately.
6148 */
6149 new_ss_msp = env->v7m.other_ss_msp;
6150 new_ss_psp = env->v7m.other_ss_psp;
6151
6152 if (v7m_using_psp(env)) {
6153 env->v7m.other_ss_psp = env->regs[13];
6154 env->v7m.other_ss_msp = env->v7m.other_sp;
6155 } else {
6156 env->v7m.other_ss_msp = env->regs[13];
6157 env->v7m.other_ss_psp = env->v7m.other_sp;
6158 }
6159
6160 env->v7m.secure = new_secstate;
6161
6162 if (v7m_using_psp(env)) {
6163 env->regs[13] = new_ss_psp;
6164 env->v7m.other_sp = new_ss_msp;
6165 } else {
6166 env->regs[13] = new_ss_msp;
6167 env->v7m.other_sp = new_ss_psp;
6168 }
6169}
6170
6171void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6172{
6173 /* Handle v7M BXNS:
6174 * - if the return value is a magic value, do exception return (like BX)
6175 * - otherwise bit 0 of the return value is the target security state
6176 */
6177 if (dest >= 0xff000000) {
6178 /* This is an exception return magic value; put it where
6179 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6180 * Note that if we ever add gen_ss_advance() singlestep support to
6181 * M profile this should count as an "instruction execution complete"
6182 * event (compare gen_bx_excret_final_code()).
6183 */
6184 env->regs[15] = dest & ~1;
6185 env->thumb = dest & 1;
6186 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6187 /* notreached */
6188 }
6189
6190 /* translate.c should have made BXNS UNDEF unless we're secure */
6191 assert(env->v7m.secure);
6192
6193 switch_v7m_security_state(env, dest & 1);
6194 env->thumb = 1;
6195 env->regs[15] = dest & ~1;
6196}
6197
3e3fa230
PM
6198void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6199{
6200 /* Handle v7M BLXNS:
6201 * - bit 0 of the destination address is the target security state
6202 */
6203
6204 /* At this point regs[15] is the address just after the BLXNS */
6205 uint32_t nextinst = env->regs[15] | 1;
6206 uint32_t sp = env->regs[13] - 8;
6207 uint32_t saved_psr;
6208
6209 /* translate.c will have made BLXNS UNDEF unless we're secure */
6210 assert(env->v7m.secure);
6211
6212 if (dest & 1) {
6213 /* target is Secure, so this is just a normal BLX,
6214 * except that the low bit doesn't indicate Thumb/not.
6215 */
6216 env->regs[14] = nextinst;
6217 env->thumb = 1;
6218 env->regs[15] = dest & ~1;
6219 return;
6220 }
6221
6222 /* Target is non-secure: first push a stack frame */
6223 if (!QEMU_IS_ALIGNED(sp, 8)) {
6224 qemu_log_mask(LOG_GUEST_ERROR,
6225 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6226 }
6227
6228 saved_psr = env->v7m.exception;
6229 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6230 saved_psr |= XPSR_SFPA;
6231 }
6232
6233 /* Note that these stores can throw exceptions on MPU faults */
6234 cpu_stl_data(env, sp, nextinst);
6235 cpu_stl_data(env, sp + 4, saved_psr);
6236
6237 env->regs[13] = sp;
6238 env->regs[14] = 0xfeffffff;
6239 if (arm_v7m_is_handler_mode(env)) {
6240 /* Write a dummy value to IPSR, to avoid leaking the current secure
6241 * exception number to non-secure code. This is guaranteed not
6242 * to cause write_v7m_exception() to actually change stacks.
6243 */
6244 write_v7m_exception(env, 1);
6245 }
6246 switch_v7m_security_state(env, 0);
6247 env->thumb = 1;
6248 env->regs[15] = dest;
6249}
6250
5b522399
PM
6251static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6252 bool spsel)
6253{
6254 /* Return a pointer to the location where we currently store the
6255 * stack pointer for the requested security state and thread mode.
6256 * This pointer will become invalid if the CPU state is updated
6257 * such that the stack pointers are switched around (eg changing
6258 * the SPSEL control bit).
6259 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6260 * Unlike that pseudocode, we require the caller to pass us in the
6261 * SPSEL control bit value; this is because we also use this
6262 * function in handling of pushing of the callee-saves registers
6263 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6264 * and in the tailchain codepath the SPSEL bit comes from the exception
6265 * return magic LR value from the previous exception. The pseudocode
6266 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6267 * to make this utility function generic enough to do the job.
6268 */
6269 bool want_psp = threadmode && spsel;
6270
6271 if (secure == env->v7m.secure) {
de2db7ec
PM
6272 if (want_psp == v7m_using_psp(env)) {
6273 return &env->regs[13];
6274 } else {
6275 return &env->v7m.other_sp;
6276 }
5b522399
PM
6277 } else {
6278 if (want_psp) {
6279 return &env->v7m.other_ss_psp;
6280 } else {
6281 return &env->v7m.other_ss_msp;
6282 }
6283 }
6284}
6285
d3392718 6286static uint32_t arm_v7m_load_vector(ARMCPU *cpu, bool targets_secure)
39ae2474
PM
6287{
6288 CPUState *cs = CPU(cpu);
6289 CPUARMState *env = &cpu->env;
6290 MemTxResult result;
d3392718 6291 hwaddr vec = env->v7m.vecbase[targets_secure] + env->v7m.exception * 4;
39ae2474
PM
6292 uint32_t addr;
6293
6294 addr = address_space_ldl(cs->as, vec,
6295 MEMTXATTRS_UNSPECIFIED, &result);
6296 if (result != MEMTX_OK) {
6297 /* Architecturally this should cause a HardFault setting HSFR.VECTTBL,
6298 * which would then be immediately followed by our failing to load
6299 * the entry vector for that HardFault, which is a Lockup case.
6300 * Since we don't model Lockup, we just report this guest error
6301 * via cpu_abort().
6302 */
d3392718
PM
6303 cpu_abort(cs, "Failed to read from %s exception vector table "
6304 "entry %08x\n", targets_secure ? "secure" : "nonsecure",
6305 (unsigned)vec);
39ae2474
PM
6306 }
6307 return addr;
6308}
6309
d3392718
PM
6310static void v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain)
6311{
6312 /* For v8M, push the callee-saves register part of the stack frame.
6313 * Compare the v8M pseudocode PushCalleeStack().
6314 * In the tailchaining case this may not be the current stack.
6315 */
6316 CPUARMState *env = &cpu->env;
6317 CPUState *cs = CPU(cpu);
6318 uint32_t *frame_sp_p;
6319 uint32_t frameptr;
6320
6321 if (dotailchain) {
6322 frame_sp_p = get_v7m_sp_ptr(env, true,
6323 lr & R_V7M_EXCRET_MODE_MASK,
6324 lr & R_V7M_EXCRET_SPSEL_MASK);
6325 } else {
6326 frame_sp_p = &env->regs[13];
6327 }
6328
6329 frameptr = *frame_sp_p - 0x28;
6330
6331 stl_phys(cs->as, frameptr, 0xfefa125b);
6332 stl_phys(cs->as, frameptr + 0x8, env->regs[4]);
6333 stl_phys(cs->as, frameptr + 0xc, env->regs[5]);
6334 stl_phys(cs->as, frameptr + 0x10, env->regs[6]);
6335 stl_phys(cs->as, frameptr + 0x14, env->regs[7]);
6336 stl_phys(cs->as, frameptr + 0x18, env->regs[8]);
6337 stl_phys(cs->as, frameptr + 0x1c, env->regs[9]);
6338 stl_phys(cs->as, frameptr + 0x20, env->regs[10]);
6339 stl_phys(cs->as, frameptr + 0x24, env->regs[11]);
6340
6341 *frame_sp_p = frameptr;
6342}
6343
6344static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain)
39ae2474
PM
6345{
6346 /* Do the "take the exception" parts of exception entry,
6347 * but not the pushing of state to the stack. This is
6348 * similar to the pseudocode ExceptionTaken() function.
6349 */
6350 CPUARMState *env = &cpu->env;
6351 uint32_t addr;
d3392718
PM
6352 bool targets_secure;
6353
6354 targets_secure = armv7m_nvic_acknowledge_irq(env->nvic);
6355
6356 if (arm_feature(env, ARM_FEATURE_V8)) {
6357 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6358 (lr & R_V7M_EXCRET_S_MASK)) {
6359 /* The background code (the owner of the registers in the
6360 * exception frame) is Secure. This means it may either already
6361 * have or now needs to push callee-saves registers.
6362 */
6363 if (targets_secure) {
6364 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6365 /* We took an exception from Secure to NonSecure
6366 * (which means the callee-saved registers got stacked)
6367 * and are now tailchaining to a Secure exception.
6368 * Clear DCRS so eventual return from this Secure
6369 * exception unstacks the callee-saved registers.
6370 */
6371 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6372 }
6373 } else {
6374 /* We're going to a non-secure exception; push the
6375 * callee-saves registers to the stack now, if they're
6376 * not already saved.
6377 */
6378 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6379 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
6380 v7m_push_callee_stack(cpu, lr, dotailchain);
6381 }
6382 lr |= R_V7M_EXCRET_DCRS_MASK;
6383 }
6384 }
6385
6386 lr &= ~R_V7M_EXCRET_ES_MASK;
6387 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6388 lr |= R_V7M_EXCRET_ES_MASK;
6389 }
6390 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6391 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6392 lr |= R_V7M_EXCRET_SPSEL_MASK;
6393 }
6394
6395 /* Clear registers if necessary to prevent non-secure exception
6396 * code being able to see register values from secure code.
6397 * Where register values become architecturally UNKNOWN we leave
6398 * them with their previous values.
6399 */
6400 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6401 if (!targets_secure) {
6402 /* Always clear the caller-saved registers (they have been
6403 * pushed to the stack earlier in v7m_push_stack()).
6404 * Clear callee-saved registers if the background code is
6405 * Secure (in which case these regs were saved in
6406 * v7m_push_callee_stack()).
6407 */
6408 int i;
6409
6410 for (i = 0; i < 13; i++) {
6411 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6412 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6413 env->regs[i] = 0;
6414 }
6415 }
6416 /* Clear EAPSR */
6417 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6418 }
6419 }
6420 }
39ae2474 6421
d3392718
PM
6422 /* Switch to target security state -- must do this before writing SPSEL */
6423 switch_v7m_security_state(env, targets_secure);
de2db7ec 6424 write_v7m_control_spsel(env, 0);
dc3c4c14 6425 arm_clear_exclusive(env);
39ae2474
PM
6426 /* Clear IT bits */
6427 env->condexec_bits = 0;
6428 env->regs[14] = lr;
d3392718 6429 addr = arm_v7m_load_vector(cpu, targets_secure);
39ae2474
PM
6430 env->regs[15] = addr & 0xfffffffe;
6431 env->thumb = addr & 1;
6432}
6433
6434static void v7m_push_stack(ARMCPU *cpu)
6435{
6436 /* Do the "set up stack frame" part of exception entry,
6437 * similar to pseudocode PushStack().
6438 */
6439 CPUARMState *env = &cpu->env;
6440 uint32_t xpsr = xpsr_read(env);
6441
6442 /* Align stack pointer if the guest wants that */
9d40cd8a
PM
6443 if ((env->regs[13] & 4) &&
6444 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
39ae2474 6445 env->regs[13] -= 4;
987ab45e 6446 xpsr |= XPSR_SPREALIGN;
39ae2474
PM
6447 }
6448 /* Switch to the handler mode. */
6449 v7m_push(env, xpsr);
6450 v7m_push(env, env->regs[15]);
6451 v7m_push(env, env->regs[14]);
6452 v7m_push(env, env->regs[12]);
6453 v7m_push(env, env->regs[3]);
6454 v7m_push(env, env->regs[2]);
6455 v7m_push(env, env->regs[1]);
6456 v7m_push(env, env->regs[0]);
6457}
6458
aa488fe3 6459static void do_v7m_exception_exit(ARMCPU *cpu)
9ee6e8bb 6460{
aa488fe3 6461 CPUARMState *env = &cpu->env;
5b522399 6462 CPUState *cs = CPU(cpu);
351e527a 6463 uint32_t excret;
9ee6e8bb 6464 uint32_t xpsr;
aa488fe3 6465 bool ufault = false;
bfb2eb52
PM
6466 bool sfault = false;
6467 bool return_to_sp_process;
6468 bool return_to_handler;
aa488fe3 6469 bool rettobase = false;
5cb18069 6470 bool exc_secure = false;
5b522399 6471 bool return_to_secure;
aa488fe3
PM
6472
6473 /* We can only get here from an EXCP_EXCEPTION_EXIT, and
9d17da4b 6474 * gen_bx_excret() enforces the architectural rule
aa488fe3
PM
6475 * that jumps to magic addresses don't have magic behaviour unless
6476 * we're in Handler mode (compare pseudocode BXWritePC()).
6477 */
15b3f556 6478 assert(arm_v7m_is_handler_mode(env));
aa488fe3
PM
6479
6480 /* In the spec pseudocode ExceptionReturn() is called directly
6481 * from BXWritePC() and gets the full target PC value including
6482 * bit zero. In QEMU's implementation we treat it as a normal
6483 * jump-to-register (which is then caught later on), and so split
6484 * the target value up between env->regs[15] and env->thumb in
6485 * gen_bx(). Reconstitute it.
6486 */
351e527a 6487 excret = env->regs[15];
aa488fe3 6488 if (env->thumb) {
351e527a 6489 excret |= 1;
aa488fe3
PM
6490 }
6491
6492 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
6493 " previous exception %d\n",
351e527a 6494 excret, env->v7m.exception);
aa488fe3 6495
351e527a 6496 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
aa488fe3 6497 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
351e527a
PM
6498 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
6499 excret);
aa488fe3
PM
6500 }
6501
bfb2eb52
PM
6502 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6503 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
6504 * we pick which FAULTMASK to clear.
6505 */
6506 if (!env->v7m.secure &&
6507 ((excret & R_V7M_EXCRET_ES_MASK) ||
6508 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
6509 sfault = 1;
6510 /* For all other purposes, treat ES as 0 (R_HXSR) */
6511 excret &= ~R_V7M_EXCRET_ES_MASK;
6512 }
6513 }
6514
a20ee600 6515 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
42a6686b
PM
6516 /* Auto-clear FAULTMASK on return from other than NMI.
6517 * If the security extension is implemented then this only
6518 * happens if the raw execution priority is >= 0; the
6519 * value of the ES bit in the exception return value indicates
6520 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
6521 */
6522 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5cb18069 6523 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
42a6686b 6524 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
5cb18069 6525 env->v7m.faultmask[exc_secure] = 0;
42a6686b
PM
6526 }
6527 } else {
6528 env->v7m.faultmask[M_REG_NS] = 0;
6529 }
a20ee600 6530 }
aa488fe3 6531
5cb18069
PM
6532 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
6533 exc_secure)) {
aa488fe3
PM
6534 case -1:
6535 /* attempt to exit an exception that isn't active */
6536 ufault = true;
6537 break;
6538 case 0:
6539 /* still an irq active now */
6540 break;
6541 case 1:
6542 /* we returned to base exception level, no nesting.
6543 * (In the pseudocode this is written using "NestedActivation != 1"
6544 * where we have 'rettobase == false'.)
6545 */
6546 rettobase = true;
6547 break;
6548 default:
6549 g_assert_not_reached();
6550 }
6551
bfb2eb52
PM
6552 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
6553 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
5b522399
PM
6554 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6555 (excret & R_V7M_EXCRET_S_MASK);
6556
bfb2eb52
PM
6557 if (arm_feature(env, ARM_FEATURE_V8)) {
6558 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6559 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
6560 * we choose to take the UsageFault.
6561 */
6562 if ((excret & R_V7M_EXCRET_S_MASK) ||
6563 (excret & R_V7M_EXCRET_ES_MASK) ||
6564 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
6565 ufault = true;
6566 }
6567 }
6568 if (excret & R_V7M_EXCRET_RES0_MASK) {
aa488fe3
PM
6569 ufault = true;
6570 }
bfb2eb52
PM
6571 } else {
6572 /* For v7M we only recognize certain combinations of the low bits */
6573 switch (excret & 0xf) {
6574 case 1: /* Return to Handler */
6575 break;
6576 case 13: /* Return to Thread using Process stack */
6577 case 9: /* Return to Thread using Main stack */
6578 /* We only need to check NONBASETHRDENA for v7M, because in
6579 * v8M this bit does not exist (it is RES1).
6580 */
6581 if (!rettobase &&
6582 !(env->v7m.ccr[env->v7m.secure] &
6583 R_V7M_CCR_NONBASETHRDENA_MASK)) {
6584 ufault = true;
6585 }
6586 break;
6587 default:
6588 ufault = true;
6589 }
6590 }
6591
6592 if (sfault) {
6593 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
6594 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
d3392718 6595 v7m_exception_taken(cpu, excret, true);
bfb2eb52
PM
6596 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6597 "stackframe: failed EXC_RETURN.ES validity check\n");
6598 return;
aa488fe3
PM
6599 }
6600
6601 if (ufault) {
6602 /* Bad exception return: instead of popping the exception
6603 * stack, directly take a usage fault on the current stack.
6604 */
334e8dad 6605 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
2fb50a33 6606 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
d3392718 6607 v7m_exception_taken(cpu, excret, true);
aa488fe3
PM
6608 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6609 "stackframe: failed exception return integrity check\n");
6610 return;
a20ee600 6611 }
9ee6e8bb 6612
de2db7ec
PM
6613 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
6614 * Handler mode (and will be until we write the new XPSR.Interrupt
6615 * field) this does not switch around the current stack pointer.
5b522399 6616 */
3f0cddee 6617 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
5b522399 6618
3919e60b
PM
6619 switch_v7m_security_state(env, return_to_secure);
6620
5b522399
PM
6621 {
6622 /* The stack pointer we should be reading the exception frame from
6623 * depends on bits in the magic exception return type value (and
6624 * for v8M isn't necessarily the stack pointer we will eventually
6625 * end up resuming execution with). Get a pointer to the location
6626 * in the CPU state struct where the SP we need is currently being
6627 * stored; we will use and modify it in place.
6628 * We use this limited C variable scope so we don't accidentally
6629 * use 'frame_sp_p' after we do something that makes it invalid.
fcf83ab1 6630 */
5b522399
PM
6631 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
6632 return_to_secure,
6633 !return_to_handler,
6634 return_to_sp_process);
6635 uint32_t frameptr = *frame_sp_p;
6636
cb484f9a
PM
6637 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
6638 arm_feature(env, ARM_FEATURE_V8)) {
6639 qemu_log_mask(LOG_GUEST_ERROR,
6640 "M profile exception return with non-8-aligned SP "
6641 "for destination state is UNPREDICTABLE\n");
6642 }
6643
907bedb3
PM
6644 /* Do we need to pop callee-saved registers? */
6645 if (return_to_secure &&
6646 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
6647 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
6648 uint32_t expected_sig = 0xfefa125b;
6649 uint32_t actual_sig = ldl_phys(cs->as, frameptr);
6650
6651 if (expected_sig != actual_sig) {
6652 /* Take a SecureFault on the current stack */
6653 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
6654 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
d3392718 6655 v7m_exception_taken(cpu, excret, true);
907bedb3
PM
6656 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
6657 "stackframe: failed exception return integrity "
6658 "signature check\n");
6659 return;
6660 }
6661
6662 env->regs[4] = ldl_phys(cs->as, frameptr + 0x8);
6663 env->regs[5] = ldl_phys(cs->as, frameptr + 0xc);
6664 env->regs[6] = ldl_phys(cs->as, frameptr + 0x10);
6665 env->regs[7] = ldl_phys(cs->as, frameptr + 0x14);
6666 env->regs[8] = ldl_phys(cs->as, frameptr + 0x18);
6667 env->regs[9] = ldl_phys(cs->as, frameptr + 0x1c);
6668 env->regs[10] = ldl_phys(cs->as, frameptr + 0x20);
6669 env->regs[11] = ldl_phys(cs->as, frameptr + 0x24);
6670
6671 frameptr += 0x28;
6672 }
6673
5b522399
PM
6674 /* Pop registers. TODO: make these accesses use the correct
6675 * attributes and address space (S/NS, priv/unpriv) and handle
6676 * memory transaction failures.
6677 */
6678 env->regs[0] = ldl_phys(cs->as, frameptr);
6679 env->regs[1] = ldl_phys(cs->as, frameptr + 0x4);
6680 env->regs[2] = ldl_phys(cs->as, frameptr + 0x8);
6681 env->regs[3] = ldl_phys(cs->as, frameptr + 0xc);
6682 env->regs[12] = ldl_phys(cs->as, frameptr + 0x10);
6683 env->regs[14] = ldl_phys(cs->as, frameptr + 0x14);
6684 env->regs[15] = ldl_phys(cs->as, frameptr + 0x18);
4e4259d3
PM
6685
6686 /* Returning from an exception with a PC with bit 0 set is defined
6687 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
6688 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
6689 * the lsbit, and there are several RTOSes out there which incorrectly
6690 * assume the r15 in the stack frame should be a Thumb-style "lsbit
6691 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
6692 * complain about the badly behaved guest.
6693 */
5b522399 6694 if (env->regs[15] & 1) {
5b522399 6695 env->regs[15] &= ~1U;
4e4259d3
PM
6696 if (!arm_feature(env, ARM_FEATURE_V8)) {
6697 qemu_log_mask(LOG_GUEST_ERROR,
6698 "M profile return from interrupt with misaligned "
6699 "PC is UNPREDICTABLE on v7M\n");
6700 }
5b522399 6701 }
4e4259d3 6702
5b522399
PM
6703 xpsr = ldl_phys(cs->as, frameptr + 0x1c);
6704
224e0c30
PM
6705 if (arm_feature(env, ARM_FEATURE_V8)) {
6706 /* For v8M we have to check whether the xPSR exception field
6707 * matches the EXCRET value for return to handler/thread
6708 * before we commit to changing the SP and xPSR.
6709 */
6710 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
6711 if (return_to_handler != will_be_handler) {
6712 /* Take an INVPC UsageFault on the current stack.
6713 * By this point we will have switched to the security state
6714 * for the background state, so this UsageFault will target
6715 * that state.
6716 */
6717 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
6718 env->v7m.secure);
6719 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
d3392718 6720 v7m_exception_taken(cpu, excret, true);
224e0c30
PM
6721 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
6722 "stackframe: failed exception return integrity "
6723 "check\n");
6724 return;
6725 }
6726 }
6727
5b522399
PM
6728 /* Commit to consuming the stack frame */
6729 frameptr += 0x20;
6730 /* Undo stack alignment (the SPREALIGN bit indicates that the original
6731 * pre-exception SP was not 8-aligned and we added a padding word to
6732 * align it, so we undo this by ORing in the bit that increases it
6733 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
6734 * would work too but a logical OR is how the pseudocode specifies it.)
6735 */
6736 if (xpsr & XPSR_SPREALIGN) {
6737 frameptr |= 4;
6738 }
6739 *frame_sp_p = frameptr;
fcf83ab1 6740 }
5b522399 6741 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
987ab45e 6742 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
aa488fe3
PM
6743
6744 /* The restored xPSR exception field will be zero if we're
6745 * resuming in Thread mode. If that doesn't match what the
351e527a 6746 * exception return excret specified then this is a UsageFault.
224e0c30 6747 * v7M requires we make this check here; v8M did it earlier.
aa488fe3 6748 */
15b3f556 6749 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
224e0c30
PM
6750 /* Take an INVPC UsageFault by pushing the stack again;
6751 * we know we're v7M so this is never a Secure UsageFault.
2fb50a33 6752 */
224e0c30 6753 assert(!arm_feature(env, ARM_FEATURE_V8));
2fb50a33 6754 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
334e8dad 6755 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
aa488fe3 6756 v7m_push_stack(cpu);
d3392718 6757 v7m_exception_taken(cpu, excret, false);
aa488fe3
PM
6758 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
6759 "failed exception return integrity check\n");
6760 return;
6761 }
6762
6763 /* Otherwise, we have a successful exception exit. */
dc3c4c14 6764 arm_clear_exclusive(env);
aa488fe3 6765 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
9ee6e8bb
PB
6766}
6767
27a7ea8a
PB
6768static void arm_log_exception(int idx)
6769{
6770 if (qemu_loglevel_mask(CPU_LOG_INT)) {
6771 const char *exc = NULL;
2c4a7cc5
PM
6772 static const char * const excnames[] = {
6773 [EXCP_UDEF] = "Undefined Instruction",
6774 [EXCP_SWI] = "SVC",
6775 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
6776 [EXCP_DATA_ABORT] = "Data Abort",
6777 [EXCP_IRQ] = "IRQ",
6778 [EXCP_FIQ] = "FIQ",
6779 [EXCP_BKPT] = "Breakpoint",
6780 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
6781 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
6782 [EXCP_HVC] = "Hypervisor Call",
6783 [EXCP_HYP_TRAP] = "Hypervisor Trap",
6784 [EXCP_SMC] = "Secure Monitor Call",
6785 [EXCP_VIRQ] = "Virtual IRQ",
6786 [EXCP_VFIQ] = "Virtual FIQ",
6787 [EXCP_SEMIHOST] = "Semihosting call",
6788 [EXCP_NOCP] = "v7M NOCP UsageFault",
6789 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
6790 };
27a7ea8a
PB
6791
6792 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
6793 exc = excnames[idx];
6794 }
6795 if (!exc) {
6796 exc = "unknown";
6797 }
6798 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
6799 }
6800}
6801
333e10c5
PM
6802static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
6803 uint32_t addr, uint16_t *insn)
6804{
6805 /* Load a 16-bit portion of a v7M instruction, returning true on success,
6806 * or false on failure (in which case we will have pended the appropriate
6807 * exception).
6808 * We need to do the instruction fetch's MPU and SAU checks
6809 * like this because there is no MMU index that would allow
6810 * doing the load with a single function call. Instead we must
6811 * first check that the security attributes permit the load
6812 * and that they don't mismatch on the two halves of the instruction,
6813 * and then we do the load as a secure load (ie using the security
6814 * attributes of the address, not the CPU, as architecturally required).
6815 */
6816 CPUState *cs = CPU(cpu);
6817 CPUARMState *env = &cpu->env;
6818 V8M_SAttributes sattrs = {};
6819 MemTxAttrs attrs = {};
6820 ARMMMUFaultInfo fi = {};
6821 MemTxResult txres;
6822 target_ulong page_size;
6823 hwaddr physaddr;
6824 int prot;
6825 uint32_t fsr;
6826
6827 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
6828 if (!sattrs.nsc || sattrs.ns) {
6829 /* This must be the second half of the insn, and it straddles a
6830 * region boundary with the second half not being S&NSC.
6831 */
6832 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
6833 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6834 qemu_log_mask(CPU_LOG_INT,
6835 "...really SecureFault with SFSR.INVEP\n");
6836 return false;
6837 }
6838 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
6839 &physaddr, &attrs, &prot, &page_size, &fsr, &fi)) {
6840 /* the MPU lookup failed */
6841 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
6842 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
6843 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
6844 return false;
6845 }
6846 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
6847 attrs, &txres);
6848 if (txres != MEMTX_OK) {
6849 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
6850 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
6851 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
6852 return false;
6853 }
6854 return true;
6855}
6856
6857static bool v7m_handle_execute_nsc(ARMCPU *cpu)
6858{
6859 /* Check whether this attempt to execute code in a Secure & NS-Callable
6860 * memory region is for an SG instruction; if so, then emulate the
6861 * effect of the SG instruction and return true. Otherwise pend
6862 * the correct kind of exception and return false.
6863 */
6864 CPUARMState *env = &cpu->env;
6865 ARMMMUIdx mmu_idx;
6866 uint16_t insn;
6867
6868 /* We should never get here unless get_phys_addr_pmsav8() caused
6869 * an exception for NS executing in S&NSC memory.
6870 */
6871 assert(!env->v7m.secure);
6872 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
6873
6874 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
6875 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
6876
6877 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
6878 return false;
6879 }
6880
6881 if (!env->thumb) {
6882 goto gen_invep;
6883 }
6884
6885 if (insn != 0xe97f) {
6886 /* Not an SG instruction first half (we choose the IMPDEF
6887 * early-SG-check option).
6888 */
6889 goto gen_invep;
6890 }
6891
6892 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
6893 return false;
6894 }
6895
6896 if (insn != 0xe97f) {
6897 /* Not an SG instruction second half (yes, both halves of the SG
6898 * insn have the same hex value)
6899 */
6900 goto gen_invep;
6901 }
6902
6903 /* OK, we have confirmed that we really have an SG instruction.
6904 * We know we're NS in S memory so don't need to repeat those checks.
6905 */
6906 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
6907 ", executing it\n", env->regs[15]);
6908 env->regs[14] &= ~1;
6909 switch_v7m_security_state(env, true);
6910 xpsr_write(env, 0, XPSR_IT);
6911 env->regs[15] += 4;
6912 return true;
6913
6914gen_invep:
6915 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
6916 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6917 qemu_log_mask(CPU_LOG_INT,
6918 "...really SecureFault with SFSR.INVEP\n");
6919 return false;
6920}
6921
e6f010cc 6922void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 6923{
e6f010cc
AF
6924 ARMCPU *cpu = ARM_CPU(cs);
6925 CPUARMState *env = &cpu->env;
9ee6e8bb 6926 uint32_t lr;
9ee6e8bb 6927
27103424 6928 arm_log_exception(cs->exception_index);
3f1beaca 6929
9ee6e8bb
PB
6930 /* For exceptions we just mark as pending on the NVIC, and let that
6931 handle it. */
27103424 6932 switch (cs->exception_index) {
9ee6e8bb 6933 case EXCP_UDEF:
2fb50a33 6934 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 6935 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
a25dc805 6936 break;
7517748e 6937 case EXCP_NOCP:
2fb50a33 6938 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 6939 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
a25dc805 6940 break;
e13886e3 6941 case EXCP_INVSTATE:
2fb50a33 6942 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
334e8dad 6943 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
e13886e3 6944 break;
9ee6e8bb 6945 case EXCP_SWI:
314e2296 6946 /* The PC already points to the next instruction. */
2fb50a33 6947 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
a25dc805 6948 break;
9ee6e8bb
PB
6949 case EXCP_PREFETCH_ABORT:
6950 case EXCP_DATA_ABORT:
5dd0641d
MD
6951 /* Note that for M profile we don't have a guest facing FSR, but
6952 * the env->exception.fsr will be populated by the code that
6953 * raises the fault, in the A profile short-descriptor format.
abf1172f 6954 */
5dd0641d 6955 switch (env->exception.fsr & 0xf) {
35337cc3
PM
6956 case M_FAKE_FSR_NSC_EXEC:
6957 /* Exception generated when we try to execute code at an address
6958 * which is marked as Secure & Non-Secure Callable and the CPU
6959 * is in the Non-Secure state. The only instruction which can
6960 * be executed like this is SG (and that only if both halves of
6961 * the SG instruction have the same security attributes.)
6962 * Everything else must generate an INVEP SecureFault, so we
6963 * emulate the SG instruction here.
35337cc3 6964 */
333e10c5
PM
6965 if (v7m_handle_execute_nsc(cpu)) {
6966 return;
6967 }
35337cc3
PM
6968 break;
6969 case M_FAKE_FSR_SFAULT:
6970 /* Various flavours of SecureFault for attempts to execute or
6971 * access data in the wrong security state.
6972 */
6973 switch (cs->exception_index) {
6974 case EXCP_PREFETCH_ABORT:
6975 if (env->v7m.secure) {
6976 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
6977 qemu_log_mask(CPU_LOG_INT,
6978 "...really SecureFault with SFSR.INVTRAN\n");
6979 } else {
6980 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
6981 qemu_log_mask(CPU_LOG_INT,
6982 "...really SecureFault with SFSR.INVEP\n");
6983 }
6984 break;
6985 case EXCP_DATA_ABORT:
6986 /* This must be an NS access to S memory */
6987 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
6988 qemu_log_mask(CPU_LOG_INT,
6989 "...really SecureFault with SFSR.AUVIOL\n");
6990 break;
6991 }
6992 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
6993 break;
5dd0641d
MD
6994 case 0x8: /* External Abort */
6995 switch (cs->exception_index) {
6996 case EXCP_PREFETCH_ABORT:
c6158878
PM
6997 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
6998 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
5dd0641d
MD
6999 break;
7000 case EXCP_DATA_ABORT:
334e8dad 7001 env->v7m.cfsr[M_REG_NS] |=
c6158878 7002 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
5dd0641d
MD
7003 env->v7m.bfar = env->exception.vaddress;
7004 qemu_log_mask(CPU_LOG_INT,
c6158878 7005 "...with CFSR.PRECISERR and BFAR 0x%x\n",
5dd0641d
MD
7006 env->v7m.bfar);
7007 break;
7008 }
2fb50a33 7009 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
5dd0641d
MD
7010 break;
7011 default:
7012 /* All other FSR values are either MPU faults or "can't happen
7013 * for M profile" cases.
7014 */
7015 switch (cs->exception_index) {
7016 case EXCP_PREFETCH_ABORT:
334e8dad 7017 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
5dd0641d
MD
7018 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7019 break;
7020 case EXCP_DATA_ABORT:
334e8dad 7021 env->v7m.cfsr[env->v7m.secure] |=
5dd0641d 7022 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
c51a5cfc 7023 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
5dd0641d
MD
7024 qemu_log_mask(CPU_LOG_INT,
7025 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
c51a5cfc 7026 env->v7m.mmfar[env->v7m.secure]);
5dd0641d
MD
7027 break;
7028 }
2fb50a33
PM
7029 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7030 env->v7m.secure);
5dd0641d
MD
7031 break;
7032 }
a25dc805 7033 break;
9ee6e8bb 7034 case EXCP_BKPT:
cfe67cef 7035 if (semihosting_enabled()) {
2ad207d4 7036 int nr;
f9fd40eb 7037 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
7038 if (nr == 0xab) {
7039 env->regs[15] += 2;
205ace55
CC
7040 qemu_log_mask(CPU_LOG_INT,
7041 "...handling as semihosting call 0x%x\n",
7042 env->regs[0]);
2ad207d4
PB
7043 env->regs[0] = do_arm_semihosting(env);
7044 return;
7045 }
7046 }
2fb50a33 7047 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
a25dc805 7048 break;
9ee6e8bb 7049 case EXCP_IRQ:
9ee6e8bb
PB
7050 break;
7051 case EXCP_EXCEPTION_EXIT:
aa488fe3 7052 do_v7m_exception_exit(cpu);
9ee6e8bb
PB
7053 return;
7054 default:
a47dddd7 7055 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
7056 return; /* Never happens. Keep compiler happy. */
7057 }
7058
d3392718
PM
7059 if (arm_feature(env, ARM_FEATURE_V8)) {
7060 lr = R_V7M_EXCRET_RES1_MASK |
7061 R_V7M_EXCRET_DCRS_MASK |
7062 R_V7M_EXCRET_FTYPE_MASK;
7063 /* The S bit indicates whether we should return to Secure
7064 * or NonSecure (ie our current state).
7065 * The ES bit indicates whether we're taking this exception
7066 * to Secure or NonSecure (ie our target state). We set it
7067 * later, in v7m_exception_taken().
7068 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7069 * This corresponds to the ARM ARM pseudocode for v8M setting
7070 * some LR bits in PushStack() and some in ExceptionTaken();
7071 * the distinction matters for the tailchain cases where we
7072 * can take an exception without pushing the stack.
7073 */
7074 if (env->v7m.secure) {
7075 lr |= R_V7M_EXCRET_S_MASK;
7076 }
7077 } else {
7078 lr = R_V7M_EXCRET_RES1_MASK |
7079 R_V7M_EXCRET_S_MASK |
7080 R_V7M_EXCRET_DCRS_MASK |
7081 R_V7M_EXCRET_FTYPE_MASK |
7082 R_V7M_EXCRET_ES_MASK;
7083 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7084 lr |= R_V7M_EXCRET_SPSEL_MASK;
7085 }
bd70b29b 7086 }
15b3f556 7087 if (!arm_v7m_is_handler_mode(env)) {
4d1e7a47 7088 lr |= R_V7M_EXCRET_MODE_MASK;
bd70b29b
PM
7089 }
7090
39ae2474 7091 v7m_push_stack(cpu);
d3392718 7092 v7m_exception_taken(cpu, lr, false);
a25dc805 7093 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
9ee6e8bb
PB
7094}
7095
ce02049d
GB
7096/* Function used to synchronize QEMU's AArch64 register set with AArch32
7097 * register set. This is necessary when switching between AArch32 and AArch64
7098 * execution state.
7099 */
7100void aarch64_sync_32_to_64(CPUARMState *env)
7101{
7102 int i;
7103 uint32_t mode = env->uncached_cpsr & CPSR_M;
7104
7105 /* We can blanket copy R[0:7] to X[0:7] */
7106 for (i = 0; i < 8; i++) {
7107 env->xregs[i] = env->regs[i];
7108 }
7109
7110 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7111 * Otherwise, they come from the banked user regs.
7112 */
7113 if (mode == ARM_CPU_MODE_FIQ) {
7114 for (i = 8; i < 13; i++) {
7115 env->xregs[i] = env->usr_regs[i - 8];
7116 }
7117 } else {
7118 for (i = 8; i < 13; i++) {
7119 env->xregs[i] = env->regs[i];
7120 }
7121 }
7122
7123 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7124 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7125 * from the mode banked register.
7126 */
7127 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7128 env->xregs[13] = env->regs[13];
7129 env->xregs[14] = env->regs[14];
7130 } else {
7131 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7132 /* HYP is an exception in that it is copied from r14 */
7133 if (mode == ARM_CPU_MODE_HYP) {
7134 env->xregs[14] = env->regs[14];
7135 } else {
7136 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7137 }
7138 }
7139
7140 if (mode == ARM_CPU_MODE_HYP) {
7141 env->xregs[15] = env->regs[13];
7142 } else {
7143 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7144 }
7145
7146 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7147 env->xregs[16] = env->regs[14];
7148 env->xregs[17] = env->regs[13];
ce02049d 7149 } else {
3a9148d0
SS
7150 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7151 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
7152 }
7153
7154 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7155 env->xregs[18] = env->regs[14];
7156 env->xregs[19] = env->regs[13];
ce02049d 7157 } else {
3a9148d0
SS
7158 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7159 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
7160 }
7161
7162 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7163 env->xregs[20] = env->regs[14];
7164 env->xregs[21] = env->regs[13];
ce02049d 7165 } else {
3a9148d0
SS
7166 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7167 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
7168 }
7169
7170 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7171 env->xregs[22] = env->regs[14];
7172 env->xregs[23] = env->regs[13];
ce02049d 7173 } else {
3a9148d0
SS
7174 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7175 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
7176 }
7177
7178 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7179 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7180 * FIQ bank for r8-r14.
7181 */
7182 if (mode == ARM_CPU_MODE_FIQ) {
7183 for (i = 24; i < 31; i++) {
7184 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7185 }
7186 } else {
7187 for (i = 24; i < 29; i++) {
7188 env->xregs[i] = env->fiq_regs[i - 24];
7189 }
7190 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7191 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7192 }
7193
7194 env->pc = env->regs[15];
7195}
7196
7197/* Function used to synchronize QEMU's AArch32 register set with AArch64
7198 * register set. This is necessary when switching between AArch32 and AArch64
7199 * execution state.
7200 */
7201void aarch64_sync_64_to_32(CPUARMState *env)
7202{
7203 int i;
7204 uint32_t mode = env->uncached_cpsr & CPSR_M;
7205
7206 /* We can blanket copy X[0:7] to R[0:7] */
7207 for (i = 0; i < 8; i++) {
7208 env->regs[i] = env->xregs[i];
7209 }
7210
7211 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7212 * Otherwise, we copy x8-x12 into the banked user regs.
7213 */
7214 if (mode == ARM_CPU_MODE_FIQ) {
7215 for (i = 8; i < 13; i++) {
7216 env->usr_regs[i - 8] = env->xregs[i];
7217 }
7218 } else {
7219 for (i = 8; i < 13; i++) {
7220 env->regs[i] = env->xregs[i];
7221 }
7222 }
7223
7224 /* Registers r13 & r14 depend on the current mode.
7225 * If we are in a given mode, we copy the corresponding x registers to r13
7226 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7227 * for the mode.
7228 */
7229 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7230 env->regs[13] = env->xregs[13];
7231 env->regs[14] = env->xregs[14];
7232 } else {
7233 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7234
7235 /* HYP is an exception in that it does not have its own banked r14 but
7236 * shares the USR r14
7237 */
7238 if (mode == ARM_CPU_MODE_HYP) {
7239 env->regs[14] = env->xregs[14];
7240 } else {
7241 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7242 }
7243 }
7244
7245 if (mode == ARM_CPU_MODE_HYP) {
7246 env->regs[13] = env->xregs[15];
7247 } else {
7248 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7249 }
7250
7251 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
7252 env->regs[14] = env->xregs[16];
7253 env->regs[13] = env->xregs[17];
ce02049d 7254 } else {
3a9148d0
SS
7255 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7256 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
7257 }
7258
7259 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
7260 env->regs[14] = env->xregs[18];
7261 env->regs[13] = env->xregs[19];
ce02049d 7262 } else {
3a9148d0
SS
7263 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7264 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
7265 }
7266
7267 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
7268 env->regs[14] = env->xregs[20];
7269 env->regs[13] = env->xregs[21];
ce02049d 7270 } else {
3a9148d0
SS
7271 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7272 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
7273 }
7274
7275 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
7276 env->regs[14] = env->xregs[22];
7277 env->regs[13] = env->xregs[23];
ce02049d 7278 } else {
3a9148d0
SS
7279 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7280 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
7281 }
7282
7283 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7284 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7285 * FIQ bank for r8-r14.
7286 */
7287 if (mode == ARM_CPU_MODE_FIQ) {
7288 for (i = 24; i < 31; i++) {
7289 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7290 }
7291 } else {
7292 for (i = 24; i < 29; i++) {
7293 env->fiq_regs[i - 24] = env->xregs[i];
7294 }
7295 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7296 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7297 }
7298
7299 env->regs[15] = env->pc;
7300}
7301
966f758c 7302static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 7303{
97a8ea5a
AF
7304 ARMCPU *cpu = ARM_CPU(cs);
7305 CPUARMState *env = &cpu->env;
b5ff1b31
FB
7306 uint32_t addr;
7307 uint32_t mask;
7308 int new_mode;
7309 uint32_t offset;
16a906fd 7310 uint32_t moe;
b5ff1b31 7311
16a906fd
PM
7312 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7313 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7314 case EC_BREAKPOINT:
7315 case EC_BREAKPOINT_SAME_EL:
7316 moe = 1;
7317 break;
7318 case EC_WATCHPOINT:
7319 case EC_WATCHPOINT_SAME_EL:
7320 moe = 10;
7321 break;
7322 case EC_AA32_BKPT:
7323 moe = 3;
7324 break;
7325 case EC_VECTORCATCH:
7326 moe = 5;
7327 break;
7328 default:
7329 moe = 0;
7330 break;
7331 }
7332
7333 if (moe) {
7334 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7335 }
7336
b5ff1b31 7337 /* TODO: Vectored interrupt controller. */
27103424 7338 switch (cs->exception_index) {
b5ff1b31
FB
7339 case EXCP_UDEF:
7340 new_mode = ARM_CPU_MODE_UND;
7341 addr = 0x04;
7342 mask = CPSR_I;
7343 if (env->thumb)
7344 offset = 2;
7345 else
7346 offset = 4;
7347 break;
7348 case EXCP_SWI:
7349 new_mode = ARM_CPU_MODE_SVC;
7350 addr = 0x08;
7351 mask = CPSR_I;
601d70b9 7352 /* The PC already points to the next instruction. */
b5ff1b31
FB
7353 offset = 0;
7354 break;
06c949e6 7355 case EXCP_BKPT:
abf1172f 7356 env->exception.fsr = 2;
9ee6e8bb
PB
7357 /* Fall through to prefetch abort. */
7358 case EXCP_PREFETCH_ABORT:
88ca1c2d 7359 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 7360 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 7361 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 7362 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7363 new_mode = ARM_CPU_MODE_ABT;
7364 addr = 0x0c;
7365 mask = CPSR_A | CPSR_I;
7366 offset = 4;
7367 break;
7368 case EXCP_DATA_ABORT:
4a7e2d73 7369 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 7370 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 7371 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 7372 env->exception.fsr,
6cd8a264 7373 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
7374 new_mode = ARM_CPU_MODE_ABT;
7375 addr = 0x10;
7376 mask = CPSR_A | CPSR_I;
7377 offset = 8;
7378 break;
7379 case EXCP_IRQ:
7380 new_mode = ARM_CPU_MODE_IRQ;
7381 addr = 0x18;
7382 /* Disable IRQ and imprecise data aborts. */
7383 mask = CPSR_A | CPSR_I;
7384 offset = 4;
de38d23b
FA
7385 if (env->cp15.scr_el3 & SCR_IRQ) {
7386 /* IRQ routed to monitor mode */
7387 new_mode = ARM_CPU_MODE_MON;
7388 mask |= CPSR_F;
7389 }
b5ff1b31
FB
7390 break;
7391 case EXCP_FIQ:
7392 new_mode = ARM_CPU_MODE_FIQ;
7393 addr = 0x1c;
7394 /* Disable FIQ, IRQ and imprecise data aborts. */
7395 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
7396 if (env->cp15.scr_el3 & SCR_FIQ) {
7397 /* FIQ routed to monitor mode */
7398 new_mode = ARM_CPU_MODE_MON;
7399 }
b5ff1b31
FB
7400 offset = 4;
7401 break;
87a4b270
PM
7402 case EXCP_VIRQ:
7403 new_mode = ARM_CPU_MODE_IRQ;
7404 addr = 0x18;
7405 /* Disable IRQ and imprecise data aborts. */
7406 mask = CPSR_A | CPSR_I;
7407 offset = 4;
7408 break;
7409 case EXCP_VFIQ:
7410 new_mode = ARM_CPU_MODE_FIQ;
7411 addr = 0x1c;
7412 /* Disable FIQ, IRQ and imprecise data aborts. */
7413 mask = CPSR_A | CPSR_I | CPSR_F;
7414 offset = 4;
7415 break;
dbe9d163
FA
7416 case EXCP_SMC:
7417 new_mode = ARM_CPU_MODE_MON;
7418 addr = 0x08;
7419 mask = CPSR_A | CPSR_I | CPSR_F;
7420 offset = 0;
7421 break;
b5ff1b31 7422 default:
a47dddd7 7423 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
7424 return; /* Never happens. Keep compiler happy. */
7425 }
e89e51a1
FA
7426
7427 if (new_mode == ARM_CPU_MODE_MON) {
7428 addr += env->cp15.mvbar;
137feaa9 7429 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 7430 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 7431 addr += 0xffff0000;
8641136c
NR
7432 } else {
7433 /* ARM v7 architectures provide a vector base address register to remap
7434 * the interrupt vector table.
e89e51a1 7435 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
7436 * Note: only bits 31:5 are valid.
7437 */
fb6c91ba 7438 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 7439 }
dbe9d163
FA
7440
7441 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
7442 env->cp15.scr_el3 &= ~SCR_NS;
7443 }
7444
b5ff1b31 7445 switch_mode (env, new_mode);
662cefb7
PM
7446 /* For exceptions taken to AArch32 we must clear the SS bit in both
7447 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
7448 */
7449 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 7450 env->spsr = cpsr_read(env);
9ee6e8bb
PB
7451 /* Clear IT bits. */
7452 env->condexec_bits = 0;
30a8cac1 7453 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 7454 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
7455 /* Set new mode endianness */
7456 env->uncached_cpsr &= ~CPSR_E;
7457 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
3823b9db 7458 env->uncached_cpsr |= CPSR_E;
73462ddd 7459 }
4cc35614 7460 env->daif |= mask;
be5e7a76
DES
7461 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
7462 * and we should just guard the thumb mode on V4 */
7463 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 7464 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 7465 }
b5ff1b31
FB
7466 env->regs[14] = env->regs[15] + offset;
7467 env->regs[15] = addr;
b5ff1b31
FB
7468}
7469
966f758c
PM
7470/* Handle exception entry to a target EL which is using AArch64 */
7471static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
7472{
7473 ARMCPU *cpu = ARM_CPU(cs);
7474 CPUARMState *env = &cpu->env;
7475 unsigned int new_el = env->exception.target_el;
7476 target_ulong addr = env->cp15.vbar_el[new_el];
7477 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
7478
7479 if (arm_current_el(env) < new_el) {
3d6f7617
PM
7480 /* Entry vector offset depends on whether the implemented EL
7481 * immediately lower than the target level is using AArch32 or AArch64
7482 */
7483 bool is_aa64;
7484
7485 switch (new_el) {
7486 case 3:
7487 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
7488 break;
7489 case 2:
7490 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
7491 break;
7492 case 1:
7493 is_aa64 = is_a64(env);
7494 break;
7495 default:
7496 g_assert_not_reached();
7497 }
7498
7499 if (is_aa64) {
f3a9b694
PM
7500 addr += 0x400;
7501 } else {
7502 addr += 0x600;
7503 }
7504 } else if (pstate_read(env) & PSTATE_SP) {
7505 addr += 0x200;
7506 }
7507
f3a9b694
PM
7508 switch (cs->exception_index) {
7509 case EXCP_PREFETCH_ABORT:
7510 case EXCP_DATA_ABORT:
7511 env->cp15.far_el[new_el] = env->exception.vaddress;
7512 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
7513 env->cp15.far_el[new_el]);
7514 /* fall through */
7515 case EXCP_BKPT:
7516 case EXCP_UDEF:
7517 case EXCP_SWI:
7518 case EXCP_HVC:
7519 case EXCP_HYP_TRAP:
7520 case EXCP_SMC:
7521 env->cp15.esr_el[new_el] = env->exception.syndrome;
7522 break;
7523 case EXCP_IRQ:
7524 case EXCP_VIRQ:
7525 addr += 0x80;
7526 break;
7527 case EXCP_FIQ:
7528 case EXCP_VFIQ:
7529 addr += 0x100;
7530 break;
7531 case EXCP_SEMIHOST:
7532 qemu_log_mask(CPU_LOG_INT,
7533 "...handling as semihosting call 0x%" PRIx64 "\n",
7534 env->xregs[0]);
7535 env->xregs[0] = do_arm_semihosting(env);
7536 return;
7537 default:
7538 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7539 }
7540
7541 if (is_a64(env)) {
7542 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
7543 aarch64_save_sp(env, arm_current_el(env));
7544 env->elr_el[new_el] = env->pc;
7545 } else {
7546 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
f3a9b694
PM
7547 env->elr_el[new_el] = env->regs[15];
7548
7549 aarch64_sync_32_to_64(env);
7550
7551 env->condexec_bits = 0;
7552 }
7553 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
7554 env->elr_el[new_el]);
7555
7556 pstate_write(env, PSTATE_DAIF | new_mode);
7557 env->aarch64 = 1;
7558 aarch64_restore_sp(env, new_el);
7559
7560 env->pc = addr;
7561
7562 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
7563 new_el, env->pc, pstate_read(env));
966f758c
PM
7564}
7565
904c04de
PM
7566static inline bool check_for_semihosting(CPUState *cs)
7567{
7568 /* Check whether this exception is a semihosting call; if so
7569 * then handle it and return true; otherwise return false.
7570 */
7571 ARMCPU *cpu = ARM_CPU(cs);
7572 CPUARMState *env = &cpu->env;
7573
7574 if (is_a64(env)) {
7575 if (cs->exception_index == EXCP_SEMIHOST) {
7576 /* This is always the 64-bit semihosting exception.
7577 * The "is this usermode" and "is semihosting enabled"
7578 * checks have been done at translate time.
7579 */
7580 qemu_log_mask(CPU_LOG_INT,
7581 "...handling as semihosting call 0x%" PRIx64 "\n",
7582 env->xregs[0]);
7583 env->xregs[0] = do_arm_semihosting(env);
7584 return true;
7585 }
7586 return false;
7587 } else {
7588 uint32_t imm;
7589
7590 /* Only intercept calls from privileged modes, to provide some
7591 * semblance of security.
7592 */
19a6e31c
PM
7593 if (cs->exception_index != EXCP_SEMIHOST &&
7594 (!semihosting_enabled() ||
7595 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
904c04de
PM
7596 return false;
7597 }
7598
7599 switch (cs->exception_index) {
19a6e31c
PM
7600 case EXCP_SEMIHOST:
7601 /* This is always a semihosting call; the "is this usermode"
7602 * and "is semihosting enabled" checks have been done at
7603 * translate time.
7604 */
7605 break;
904c04de
PM
7606 case EXCP_SWI:
7607 /* Check for semihosting interrupt. */
7608 if (env->thumb) {
f9fd40eb 7609 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
7610 & 0xff;
7611 if (imm == 0xab) {
7612 break;
7613 }
7614 } else {
f9fd40eb 7615 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
7616 & 0xffffff;
7617 if (imm == 0x123456) {
7618 break;
7619 }
7620 }
7621 return false;
7622 case EXCP_BKPT:
7623 /* See if this is a semihosting syscall. */
7624 if (env->thumb) {
f9fd40eb 7625 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
7626 & 0xff;
7627 if (imm == 0xab) {
7628 env->regs[15] += 2;
7629 break;
7630 }
7631 }
7632 return false;
7633 default:
7634 return false;
7635 }
7636
7637 qemu_log_mask(CPU_LOG_INT,
7638 "...handling as semihosting call 0x%x\n",
7639 env->regs[0]);
7640 env->regs[0] = do_arm_semihosting(env);
7641 return true;
7642 }
7643}
7644
966f758c
PM
7645/* Handle a CPU exception for A and R profile CPUs.
7646 * Do any appropriate logging, handle PSCI calls, and then hand off
7647 * to the AArch64-entry or AArch32-entry function depending on the
7648 * target exception level's register width.
7649 */
7650void arm_cpu_do_interrupt(CPUState *cs)
7651{
7652 ARMCPU *cpu = ARM_CPU(cs);
7653 CPUARMState *env = &cpu->env;
7654 unsigned int new_el = env->exception.target_el;
7655
531c60a9 7656 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c
PM
7657
7658 arm_log_exception(cs->exception_index);
7659 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
7660 new_el);
7661 if (qemu_loglevel_mask(CPU_LOG_INT)
7662 && !excp_is_internal(cs->exception_index)) {
6568da45 7663 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
966f758c
PM
7664 env->exception.syndrome >> ARM_EL_EC_SHIFT,
7665 env->exception.syndrome);
7666 }
7667
7668 if (arm_is_psci_call(cpu, cs->exception_index)) {
7669 arm_handle_psci_call(cpu);
7670 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
7671 return;
7672 }
7673
904c04de
PM
7674 /* Semihosting semantics depend on the register width of the
7675 * code that caused the exception, not the target exception level,
7676 * so must be handled here.
966f758c 7677 */
904c04de
PM
7678 if (check_for_semihosting(cs)) {
7679 return;
7680 }
7681
7682 assert(!excp_is_internal(cs->exception_index));
7683 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
7684 arm_cpu_do_interrupt_aarch64(cs);
7685 } else {
7686 arm_cpu_do_interrupt_aarch32(cs);
7687 }
f3a9b694 7688
8d04fb55
JK
7689 /* Hooks may change global state so BQL should be held, also the
7690 * BQL needs to be held for any modification of
7691 * cs->interrupt_request.
7692 */
7693 g_assert(qemu_mutex_iothread_locked());
7694
bd7d00fc
PM
7695 arm_call_el_change_hook(cpu);
7696
f3a9b694
PM
7697 if (!kvm_enabled()) {
7698 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
7699 }
7700}
0480f69a
PM
7701
7702/* Return the exception level which controls this address translation regime */
7703static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
7704{
7705 switch (mmu_idx) {
7706 case ARMMMUIdx_S2NS:
7707 case ARMMMUIdx_S1E2:
7708 return 2;
7709 case ARMMMUIdx_S1E3:
7710 return 3;
7711 case ARMMMUIdx_S1SE0:
7712 return arm_el_is_aa64(env, 3) ? 1 : 3;
7713 case ARMMMUIdx_S1SE1:
7714 case ARMMMUIdx_S1NSE0:
7715 case ARMMMUIdx_S1NSE1:
e7b921c2 7716 case ARMMMUIdx_MPriv:
3bef7012 7717 case ARMMMUIdx_MNegPri:
e7b921c2 7718 case ARMMMUIdx_MUser:
66787c78
PM
7719 case ARMMMUIdx_MSPriv:
7720 case ARMMMUIdx_MSNegPri:
7721 case ARMMMUIdx_MSUser:
0480f69a
PM
7722 return 1;
7723 default:
7724 g_assert_not_reached();
7725 }
7726}
7727
7728/* Return the SCTLR value which controls this address translation regime */
7729static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
7730{
7731 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
7732}
7733
7734/* Return true if the specified stage of address translation is disabled */
7735static inline bool regime_translation_disabled(CPUARMState *env,
7736 ARMMMUIdx mmu_idx)
7737{
29c483a5 7738 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 7739 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
7740 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
7741 case R_V7M_MPU_CTRL_ENABLE_MASK:
7742 /* Enabled, but not for HardFault and NMI */
66787c78
PM
7743 return mmu_idx == ARMMMUIdx_MNegPri ||
7744 mmu_idx == ARMMMUIdx_MSNegPri;
3bef7012
PM
7745 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
7746 /* Enabled for all cases */
7747 return false;
7748 case 0:
7749 default:
7750 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
7751 * we warned about that in armv7m_nvic.c when the guest set it.
7752 */
7753 return true;
7754 }
29c483a5
MD
7755 }
7756
0480f69a
PM
7757 if (mmu_idx == ARMMMUIdx_S2NS) {
7758 return (env->cp15.hcr_el2 & HCR_VM) == 0;
7759 }
7760 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
7761}
7762
73462ddd
PC
7763static inline bool regime_translation_big_endian(CPUARMState *env,
7764 ARMMMUIdx mmu_idx)
7765{
7766 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
7767}
7768
0480f69a
PM
7769/* Return the TCR controlling this translation regime */
7770static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
7771{
7772 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 7773 return &env->cp15.vtcr_el2;
0480f69a
PM
7774 }
7775 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
7776}
7777
8bd5c820
PM
7778/* Convert a possible stage1+2 MMU index into the appropriate
7779 * stage 1 MMU index
7780 */
7781static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
7782{
7783 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
7784 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
7785 }
7786 return mmu_idx;
7787}
7788
86fb3fa4
TH
7789/* Returns TBI0 value for current regime el */
7790uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
7791{
7792 TCR *tcr;
7793 uint32_t el;
7794
7795 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
7796 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7797 */
7798 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
7799
7800 tcr = regime_tcr(env, mmu_idx);
7801 el = regime_el(env, mmu_idx);
7802
7803 if (el > 1) {
7804 return extract64(tcr->raw_tcr, 20, 1);
7805 } else {
7806 return extract64(tcr->raw_tcr, 37, 1);
7807 }
7808}
7809
7810/* Returns TBI1 value for current regime el */
7811uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
7812{
7813 TCR *tcr;
7814 uint32_t el;
7815
7816 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8bd5c820
PM
7817 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
7818 */
7819 mmu_idx = stage_1_mmu_idx(mmu_idx);
86fb3fa4
TH
7820
7821 tcr = regime_tcr(env, mmu_idx);
7822 el = regime_el(env, mmu_idx);
7823
7824 if (el > 1) {
7825 return 0;
7826 } else {
7827 return extract64(tcr->raw_tcr, 38, 1);
7828 }
7829}
7830
aef878be
GB
7831/* Return the TTBR associated with this translation regime */
7832static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
7833 int ttbrn)
7834{
7835 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 7836 return env->cp15.vttbr_el2;
aef878be
GB
7837 }
7838 if (ttbrn == 0) {
7839 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
7840 } else {
7841 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
7842 }
7843}
7844
0480f69a
PM
7845/* Return true if the translation regime is using LPAE format page tables */
7846static inline bool regime_using_lpae_format(CPUARMState *env,
7847 ARMMMUIdx mmu_idx)
7848{
7849 int el = regime_el(env, mmu_idx);
7850 if (el == 2 || arm_el_is_aa64(env, el)) {
7851 return true;
7852 }
7853 if (arm_feature(env, ARM_FEATURE_LPAE)
7854 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
7855 return true;
7856 }
7857 return false;
7858}
7859
deb2db99
AR
7860/* Returns true if the stage 1 translation regime is using LPAE format page
7861 * tables. Used when raising alignment exceptions, whose FSR changes depending
7862 * on whether the long or short descriptor format is in use. */
7863bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 7864{
8bd5c820 7865 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 7866
30901475
AB
7867 return regime_using_lpae_format(env, mmu_idx);
7868}
7869
0480f69a
PM
7870static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
7871{
7872 switch (mmu_idx) {
7873 case ARMMMUIdx_S1SE0:
7874 case ARMMMUIdx_S1NSE0:
e7b921c2 7875 case ARMMMUIdx_MUser:
0480f69a
PM
7876 return true;
7877 default:
7878 return false;
7879 case ARMMMUIdx_S12NSE0:
7880 case ARMMMUIdx_S12NSE1:
7881 g_assert_not_reached();
7882 }
7883}
7884
0fbf5238
AJ
7885/* Translate section/page access permissions to page
7886 * R/W protection flags
d76951b6
AJ
7887 *
7888 * @env: CPUARMState
7889 * @mmu_idx: MMU index indicating required translation regime
7890 * @ap: The 3-bit access permissions (AP[2:0])
7891 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
7892 */
7893static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
7894 int ap, int domain_prot)
7895{
554b0b09
PM
7896 bool is_user = regime_is_user(env, mmu_idx);
7897
7898 if (domain_prot == 3) {
7899 return PAGE_READ | PAGE_WRITE;
7900 }
7901
554b0b09
PM
7902 switch (ap) {
7903 case 0:
7904 if (arm_feature(env, ARM_FEATURE_V7)) {
7905 return 0;
7906 }
554b0b09
PM
7907 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
7908 case SCTLR_S:
7909 return is_user ? 0 : PAGE_READ;
7910 case SCTLR_R:
7911 return PAGE_READ;
7912 default:
7913 return 0;
7914 }
7915 case 1:
7916 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
7917 case 2:
87c3d486 7918 if (is_user) {
0fbf5238 7919 return PAGE_READ;
87c3d486 7920 } else {
554b0b09 7921 return PAGE_READ | PAGE_WRITE;
87c3d486 7922 }
554b0b09
PM
7923 case 3:
7924 return PAGE_READ | PAGE_WRITE;
7925 case 4: /* Reserved. */
7926 return 0;
7927 case 5:
0fbf5238 7928 return is_user ? 0 : PAGE_READ;
554b0b09 7929 case 6:
0fbf5238 7930 return PAGE_READ;
554b0b09 7931 case 7:
87c3d486 7932 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 7933 return 0;
87c3d486 7934 }
0fbf5238 7935 return PAGE_READ;
554b0b09 7936 default:
0fbf5238 7937 g_assert_not_reached();
554b0b09 7938 }
b5ff1b31
FB
7939}
7940
d76951b6
AJ
7941/* Translate section/page access permissions to page
7942 * R/W protection flags.
7943 *
d76951b6 7944 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 7945 * @is_user: TRUE if accessing from PL0
d76951b6 7946 */
d8e052b3 7947static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 7948{
d76951b6
AJ
7949 switch (ap) {
7950 case 0:
7951 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
7952 case 1:
7953 return PAGE_READ | PAGE_WRITE;
7954 case 2:
7955 return is_user ? 0 : PAGE_READ;
7956 case 3:
7957 return PAGE_READ;
7958 default:
7959 g_assert_not_reached();
7960 }
7961}
7962
d8e052b3
AJ
7963static inline int
7964simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
7965{
7966 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
7967}
7968
6ab1a5ee
EI
7969/* Translate S2 section/page access permissions to protection flags
7970 *
7971 * @env: CPUARMState
7972 * @s2ap: The 2-bit stage2 access permissions (S2AP)
7973 * @xn: XN (execute-never) bit
7974 */
7975static int get_S2prot(CPUARMState *env, int s2ap, int xn)
7976{
7977 int prot = 0;
7978
7979 if (s2ap & 1) {
7980 prot |= PAGE_READ;
7981 }
7982 if (s2ap & 2) {
7983 prot |= PAGE_WRITE;
7984 }
7985 if (!xn) {
dfda6837
SS
7986 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
7987 prot |= PAGE_EXEC;
7988 }
6ab1a5ee
EI
7989 }
7990 return prot;
7991}
7992
d8e052b3
AJ
7993/* Translate section/page access permissions to protection flags
7994 *
7995 * @env: CPUARMState
7996 * @mmu_idx: MMU index indicating required translation regime
7997 * @is_aa64: TRUE if AArch64
7998 * @ap: The 2-bit simple AP (AP[2:1])
7999 * @ns: NS (non-secure) bit
8000 * @xn: XN (execute-never) bit
8001 * @pxn: PXN (privileged execute-never) bit
8002 */
8003static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8004 int ap, int ns, int xn, int pxn)
8005{
8006 bool is_user = regime_is_user(env, mmu_idx);
8007 int prot_rw, user_rw;
8008 bool have_wxn;
8009 int wxn = 0;
8010
8011 assert(mmu_idx != ARMMMUIdx_S2NS);
8012
8013 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8014 if (is_user) {
8015 prot_rw = user_rw;
8016 } else {
8017 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8018 }
8019
8020 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8021 return prot_rw;
8022 }
8023
8024 /* TODO have_wxn should be replaced with
8025 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8026 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8027 * compatible processors have EL2, which is required for [U]WXN.
8028 */
8029 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8030
8031 if (have_wxn) {
8032 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8033 }
8034
8035 if (is_aa64) {
8036 switch (regime_el(env, mmu_idx)) {
8037 case 1:
8038 if (!is_user) {
8039 xn = pxn || (user_rw & PAGE_WRITE);
8040 }
8041 break;
8042 case 2:
8043 case 3:
8044 break;
8045 }
8046 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8047 switch (regime_el(env, mmu_idx)) {
8048 case 1:
8049 case 3:
8050 if (is_user) {
8051 xn = xn || !(user_rw & PAGE_READ);
8052 } else {
8053 int uwxn = 0;
8054 if (have_wxn) {
8055 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8056 }
8057 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8058 (uwxn && (user_rw & PAGE_WRITE));
8059 }
8060 break;
8061 case 2:
8062 break;
8063 }
8064 } else {
8065 xn = wxn = 0;
8066 }
8067
8068 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8069 return prot_rw;
8070 }
8071 return prot_rw | PAGE_EXEC;
8072}
8073
0480f69a
PM
8074static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8075 uint32_t *table, uint32_t address)
b2fa1797 8076{
0480f69a 8077 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 8078 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 8079
11f136ee
FA
8080 if (address & tcr->mask) {
8081 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
8082 /* Translation table walk disabled for TTBR1 */
8083 return false;
8084 }
aef878be 8085 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 8086 } else {
11f136ee 8087 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
8088 /* Translation table walk disabled for TTBR0 */
8089 return false;
8090 }
aef878be 8091 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
8092 }
8093 *table |= (address >> 18) & 0x3ffc;
8094 return true;
b2fa1797
PB
8095}
8096
37785977
EI
8097/* Translate a S1 pagetable walk through S2 if needed. */
8098static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8099 hwaddr addr, MemTxAttrs txattrs,
8100 uint32_t *fsr,
8101 ARMMMUFaultInfo *fi)
8102{
8103 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8104 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8105 target_ulong s2size;
8106 hwaddr s2pa;
8107 int s2prot;
8108 int ret;
8109
8110 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
8111 &txattrs, &s2prot, &s2size, fsr, fi);
8112 if (ret) {
8113 fi->s2addr = addr;
8114 fi->stage2 = true;
8115 fi->s1ptw = true;
8116 return ~0;
8117 }
8118 addr = s2pa;
8119 }
8120 return addr;
8121}
8122
ebca90e4
PM
8123/* All loads done in the course of a page table walk go through here.
8124 * TODO: rather than ignoring errors from physical memory reads (which
8125 * are external aborts in ARM terminology) we should propagate this
8126 * error out so that we can turn it into a Data Abort if this walk
8127 * was being done for a CPU load/store or an address translation instruction
8128 * (but not if it was for a debug access).
8129 */
a614e698
EI
8130static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8131 ARMMMUIdx mmu_idx, uint32_t *fsr,
8132 ARMMMUFaultInfo *fi)
ebca90e4 8133{
a614e698
EI
8134 ARMCPU *cpu = ARM_CPU(cs);
8135 CPUARMState *env = &cpu->env;
ebca90e4 8136 MemTxAttrs attrs = {};
5ce4ff65 8137 AddressSpace *as;
ebca90e4
PM
8138
8139 attrs.secure = is_secure;
5ce4ff65 8140 as = arm_addressspace(cs, attrs);
a614e698
EI
8141 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
8142 if (fi->s1ptw) {
8143 return 0;
8144 }
73462ddd
PC
8145 if (regime_translation_big_endian(env, mmu_idx)) {
8146 return address_space_ldl_be(as, addr, attrs, NULL);
8147 } else {
8148 return address_space_ldl_le(as, addr, attrs, NULL);
8149 }
ebca90e4
PM
8150}
8151
37785977
EI
8152static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8153 ARMMMUIdx mmu_idx, uint32_t *fsr,
8154 ARMMMUFaultInfo *fi)
ebca90e4 8155{
37785977
EI
8156 ARMCPU *cpu = ARM_CPU(cs);
8157 CPUARMState *env = &cpu->env;
ebca90e4 8158 MemTxAttrs attrs = {};
5ce4ff65 8159 AddressSpace *as;
ebca90e4
PM
8160
8161 attrs.secure = is_secure;
5ce4ff65 8162 as = arm_addressspace(cs, attrs);
37785977
EI
8163 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
8164 if (fi->s1ptw) {
8165 return 0;
8166 }
73462ddd
PC
8167 if (regime_translation_big_endian(env, mmu_idx)) {
8168 return address_space_ldq_be(as, addr, attrs, NULL);
8169 } else {
8170 return address_space_ldq_le(as, addr, attrs, NULL);
8171 }
ebca90e4
PM
8172}
8173
b7cc4e82 8174static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 8175 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8176 hwaddr *phys_ptr, int *prot,
e14b5a23
EI
8177 target_ulong *page_size, uint32_t *fsr,
8178 ARMMMUFaultInfo *fi)
b5ff1b31 8179{
70d74660 8180 CPUState *cs = CPU(arm_env_get_cpu(env));
b5ff1b31
FB
8181 int code;
8182 uint32_t table;
8183 uint32_t desc;
8184 int type;
8185 int ap;
e389be16 8186 int domain = 0;
dd4ebc2e 8187 int domain_prot;
a8170e5e 8188 hwaddr phys_addr;
0480f69a 8189 uint32_t dacr;
b5ff1b31 8190
9ee6e8bb
PB
8191 /* Pagetable walk. */
8192 /* Lookup l1 descriptor. */
0480f69a 8193 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
8194 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8195 code = 5;
8196 goto do_fault;
8197 }
a614e698
EI
8198 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8199 mmu_idx, fsr, fi);
9ee6e8bb 8200 type = (desc & 3);
dd4ebc2e 8201 domain = (desc >> 5) & 0x0f;
0480f69a
PM
8202 if (regime_el(env, mmu_idx) == 1) {
8203 dacr = env->cp15.dacr_ns;
8204 } else {
8205 dacr = env->cp15.dacr_s;
8206 }
8207 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 8208 if (type == 0) {
601d70b9 8209 /* Section translation fault. */
9ee6e8bb
PB
8210 code = 5;
8211 goto do_fault;
8212 }
dd4ebc2e 8213 if (domain_prot == 0 || domain_prot == 2) {
9ee6e8bb
PB
8214 if (type == 2)
8215 code = 9; /* Section domain fault. */
8216 else
8217 code = 11; /* Page domain fault. */
8218 goto do_fault;
8219 }
8220 if (type == 2) {
8221 /* 1Mb section. */
8222 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8223 ap = (desc >> 10) & 3;
8224 code = 13;
d4c430a8 8225 *page_size = 1024 * 1024;
9ee6e8bb
PB
8226 } else {
8227 /* Lookup l2 entry. */
554b0b09
PM
8228 if (type == 1) {
8229 /* Coarse pagetable. */
8230 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8231 } else {
8232 /* Fine pagetable. */
8233 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8234 }
a614e698
EI
8235 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8236 mmu_idx, fsr, fi);
9ee6e8bb
PB
8237 switch (desc & 3) {
8238 case 0: /* Page translation fault. */
8239 code = 7;
8240 goto do_fault;
8241 case 1: /* 64k page. */
8242 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8243 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 8244 *page_size = 0x10000;
ce819861 8245 break;
9ee6e8bb
PB
8246 case 2: /* 4k page. */
8247 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 8248 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 8249 *page_size = 0x1000;
ce819861 8250 break;
fc1891c7 8251 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 8252 if (type == 1) {
fc1891c7
PM
8253 /* ARMv6/XScale extended small page format */
8254 if (arm_feature(env, ARM_FEATURE_XSCALE)
8255 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 8256 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 8257 *page_size = 0x1000;
554b0b09 8258 } else {
fc1891c7
PM
8259 /* UNPREDICTABLE in ARMv5; we choose to take a
8260 * page translation fault.
8261 */
554b0b09
PM
8262 code = 7;
8263 goto do_fault;
8264 }
8265 } else {
8266 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 8267 *page_size = 0x400;
554b0b09 8268 }
9ee6e8bb 8269 ap = (desc >> 4) & 3;
ce819861
PB
8270 break;
8271 default:
9ee6e8bb
PB
8272 /* Never happens, but compiler isn't smart enough to tell. */
8273 abort();
ce819861 8274 }
9ee6e8bb
PB
8275 code = 15;
8276 }
0fbf5238
AJ
8277 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8278 *prot |= *prot ? PAGE_EXEC : 0;
8279 if (!(*prot & (1 << access_type))) {
9ee6e8bb
PB
8280 /* Access permission fault. */
8281 goto do_fault;
8282 }
8283 *phys_ptr = phys_addr;
b7cc4e82 8284 return false;
9ee6e8bb 8285do_fault:
b7cc4e82
PC
8286 *fsr = code | (domain << 4);
8287 return true;
9ee6e8bb
PB
8288}
8289
b7cc4e82 8290static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 8291 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8292 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
8293 target_ulong *page_size, uint32_t *fsr,
8294 ARMMMUFaultInfo *fi)
9ee6e8bb 8295{
70d74660 8296 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb
PB
8297 int code;
8298 uint32_t table;
8299 uint32_t desc;
8300 uint32_t xn;
de9b05b8 8301 uint32_t pxn = 0;
9ee6e8bb
PB
8302 int type;
8303 int ap;
de9b05b8 8304 int domain = 0;
dd4ebc2e 8305 int domain_prot;
a8170e5e 8306 hwaddr phys_addr;
0480f69a 8307 uint32_t dacr;
8bf5b6a9 8308 bool ns;
9ee6e8bb
PB
8309
8310 /* Pagetable walk. */
8311 /* Lookup l1 descriptor. */
0480f69a 8312 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
8313 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8314 code = 5;
8315 goto do_fault;
8316 }
a614e698
EI
8317 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8318 mmu_idx, fsr, fi);
9ee6e8bb 8319 type = (desc & 3);
de9b05b8
PM
8320 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8321 /* Section translation fault, or attempt to use the encoding
8322 * which is Reserved on implementations without PXN.
8323 */
9ee6e8bb 8324 code = 5;
9ee6e8bb 8325 goto do_fault;
de9b05b8
PM
8326 }
8327 if ((type == 1) || !(desc & (1 << 18))) {
8328 /* Page or Section. */
dd4ebc2e 8329 domain = (desc >> 5) & 0x0f;
9ee6e8bb 8330 }
0480f69a
PM
8331 if (regime_el(env, mmu_idx) == 1) {
8332 dacr = env->cp15.dacr_ns;
8333 } else {
8334 dacr = env->cp15.dacr_s;
8335 }
8336 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 8337 if (domain_prot == 0 || domain_prot == 2) {
de9b05b8 8338 if (type != 1) {
9ee6e8bb 8339 code = 9; /* Section domain fault. */
de9b05b8 8340 } else {
9ee6e8bb 8341 code = 11; /* Page domain fault. */
de9b05b8 8342 }
9ee6e8bb
PB
8343 goto do_fault;
8344 }
de9b05b8 8345 if (type != 1) {
9ee6e8bb
PB
8346 if (desc & (1 << 18)) {
8347 /* Supersection. */
8348 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
8349 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
8350 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 8351 *page_size = 0x1000000;
b5ff1b31 8352 } else {
9ee6e8bb
PB
8353 /* Section. */
8354 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 8355 *page_size = 0x100000;
b5ff1b31 8356 }
9ee6e8bb
PB
8357 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
8358 xn = desc & (1 << 4);
de9b05b8 8359 pxn = desc & 1;
9ee6e8bb 8360 code = 13;
8bf5b6a9 8361 ns = extract32(desc, 19, 1);
9ee6e8bb 8362 } else {
de9b05b8
PM
8363 if (arm_feature(env, ARM_FEATURE_PXN)) {
8364 pxn = (desc >> 2) & 1;
8365 }
8bf5b6a9 8366 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
8367 /* Lookup l2 entry. */
8368 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698
EI
8369 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8370 mmu_idx, fsr, fi);
9ee6e8bb
PB
8371 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
8372 switch (desc & 3) {
8373 case 0: /* Page translation fault. */
8374 code = 7;
b5ff1b31 8375 goto do_fault;
9ee6e8bb
PB
8376 case 1: /* 64k page. */
8377 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8378 xn = desc & (1 << 15);
d4c430a8 8379 *page_size = 0x10000;
9ee6e8bb
PB
8380 break;
8381 case 2: case 3: /* 4k page. */
8382 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8383 xn = desc & 1;
d4c430a8 8384 *page_size = 0x1000;
9ee6e8bb
PB
8385 break;
8386 default:
8387 /* Never happens, but compiler isn't smart enough to tell. */
8388 abort();
b5ff1b31 8389 }
9ee6e8bb
PB
8390 code = 15;
8391 }
dd4ebc2e 8392 if (domain_prot == 3) {
c0034328
JR
8393 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8394 } else {
0480f69a 8395 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
8396 xn = 1;
8397 }
03ae85f8 8398 if (xn && access_type == MMU_INST_FETCH)
c0034328 8399 goto do_fault;
9ee6e8bb 8400
d76951b6
AJ
8401 if (arm_feature(env, ARM_FEATURE_V6K) &&
8402 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
8403 /* The simplified model uses AP[0] as an access control bit. */
8404 if ((ap & 1) == 0) {
8405 /* Access flag fault. */
8406 code = (code == 15) ? 6 : 3;
8407 goto do_fault;
8408 }
8409 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
8410 } else {
8411 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 8412 }
0fbf5238
AJ
8413 if (*prot && !xn) {
8414 *prot |= PAGE_EXEC;
8415 }
8416 if (!(*prot & (1 << access_type))) {
c0034328
JR
8417 /* Access permission fault. */
8418 goto do_fault;
8419 }
3ad493fc 8420 }
8bf5b6a9
PM
8421 if (ns) {
8422 /* The NS bit will (as required by the architecture) have no effect if
8423 * the CPU doesn't support TZ or this is a non-secure translation
8424 * regime, because the attribute will already be non-secure.
8425 */
8426 attrs->secure = false;
8427 }
9ee6e8bb 8428 *phys_ptr = phys_addr;
b7cc4e82 8429 return false;
b5ff1b31 8430do_fault:
b7cc4e82
PC
8431 *fsr = code | (domain << 4);
8432 return true;
b5ff1b31
FB
8433}
8434
3dde962f
PM
8435/* Fault type for long-descriptor MMU fault reporting; this corresponds
8436 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
8437 */
8438typedef enum {
8439 translation_fault = 1,
8440 access_fault = 2,
8441 permission_fault = 3,
8442} MMUFaultType;
8443
1853d5a9 8444/*
a0e966c9 8445 * check_s2_mmu_setup
1853d5a9
EI
8446 * @cpu: ARMCPU
8447 * @is_aa64: True if the translation regime is in AArch64 state
8448 * @startlevel: Suggested starting level
8449 * @inputsize: Bitsize of IPAs
8450 * @stride: Page-table stride (See the ARM ARM)
8451 *
a0e966c9
EI
8452 * Returns true if the suggested S2 translation parameters are OK and
8453 * false otherwise.
1853d5a9 8454 */
a0e966c9
EI
8455static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
8456 int inputsize, int stride)
1853d5a9 8457{
98d68ec2
EI
8458 const int grainsize = stride + 3;
8459 int startsizecheck;
8460
1853d5a9
EI
8461 /* Negative levels are never allowed. */
8462 if (level < 0) {
8463 return false;
8464 }
8465
98d68ec2
EI
8466 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
8467 if (startsizecheck < 1 || startsizecheck > stride + 4) {
8468 return false;
8469 }
8470
1853d5a9 8471 if (is_aa64) {
3526423e 8472 CPUARMState *env = &cpu->env;
1853d5a9
EI
8473 unsigned int pamax = arm_pamax(cpu);
8474
8475 switch (stride) {
8476 case 13: /* 64KB Pages. */
8477 if (level == 0 || (level == 1 && pamax <= 42)) {
8478 return false;
8479 }
8480 break;
8481 case 11: /* 16KB Pages. */
8482 if (level == 0 || (level == 1 && pamax <= 40)) {
8483 return false;
8484 }
8485 break;
8486 case 9: /* 4KB Pages. */
8487 if (level == 0 && pamax <= 42) {
8488 return false;
8489 }
8490 break;
8491 default:
8492 g_assert_not_reached();
8493 }
3526423e
EI
8494
8495 /* Inputsize checks. */
8496 if (inputsize > pamax &&
8497 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
8498 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
8499 return false;
8500 }
1853d5a9 8501 } else {
1853d5a9
EI
8502 /* AArch32 only supports 4KB pages. Assert on that. */
8503 assert(stride == 9);
8504
8505 if (level == 0) {
8506 return false;
8507 }
1853d5a9
EI
8508 }
8509 return true;
8510}
8511
b7cc4e82 8512static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
03ae85f8 8513 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 8514 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
e14b5a23
EI
8515 target_ulong *page_size_ptr, uint32_t *fsr,
8516 ARMMMUFaultInfo *fi)
3dde962f 8517{
1853d5a9
EI
8518 ARMCPU *cpu = arm_env_get_cpu(env);
8519 CPUState *cs = CPU(cpu);
3dde962f
PM
8520 /* Read an LPAE long-descriptor translation table. */
8521 MMUFaultType fault_type = translation_fault;
1b4093ea 8522 uint32_t level;
0c5fbf3b 8523 uint32_t epd = 0;
1f4c8c18 8524 int32_t t0sz, t1sz;
2c8dd318 8525 uint32_t tg;
3dde962f
PM
8526 uint64_t ttbr;
8527 int ttbr_select;
dddb5223 8528 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f
PM
8529 uint32_t tableattrs;
8530 target_ulong page_size;
8531 uint32_t attrs;
973a5434 8532 int32_t stride = 9;
6e99f762 8533 int32_t addrsize;
4ca6a051 8534 int inputsize;
2c8dd318 8535 int32_t tbi = 0;
0480f69a 8536 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 8537 int ap, ns, xn, pxn;
88e8add8
GB
8538 uint32_t el = regime_el(env, mmu_idx);
8539 bool ttbr1_valid = true;
6109769a 8540 uint64_t descaddrmask;
6e99f762 8541 bool aarch64 = arm_el_is_aa64(env, el);
0480f69a
PM
8542
8543 /* TODO:
88e8add8
GB
8544 * This code does not handle the different format TCR for VTCR_EL2.
8545 * This code also does not support shareability levels.
8546 * Attribute and permission bit handling should also be checked when adding
8547 * support for those page table walks.
0480f69a 8548 */
6e99f762 8549 if (aarch64) {
1b4093ea 8550 level = 0;
6e99f762 8551 addrsize = 64;
88e8add8 8552 if (el > 1) {
1edee470
EI
8553 if (mmu_idx != ARMMMUIdx_S2NS) {
8554 tbi = extract64(tcr->raw_tcr, 20, 1);
8555 }
88e8add8
GB
8556 } else {
8557 if (extract64(address, 55, 1)) {
8558 tbi = extract64(tcr->raw_tcr, 38, 1);
8559 } else {
8560 tbi = extract64(tcr->raw_tcr, 37, 1);
8561 }
8562 }
2c8dd318 8563 tbi *= 8;
88e8add8
GB
8564
8565 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
8566 * invalid.
8567 */
8568 if (el > 1) {
8569 ttbr1_valid = false;
8570 }
d0a2cbce 8571 } else {
1b4093ea 8572 level = 1;
6e99f762 8573 addrsize = 32;
d0a2cbce
PM
8574 /* There is no TTBR1 for EL2 */
8575 if (el == 2) {
8576 ttbr1_valid = false;
8577 }
2c8dd318 8578 }
3dde962f
PM
8579
8580 /* Determine whether this address is in the region controlled by
8581 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
8582 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
8583 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
8584 */
6e99f762 8585 if (aarch64) {
4ee38098
EI
8586 /* AArch64 translation. */
8587 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
8588 t0sz = MIN(t0sz, 39);
8589 t0sz = MAX(t0sz, 16);
4ee38098
EI
8590 } else if (mmu_idx != ARMMMUIdx_S2NS) {
8591 /* AArch32 stage 1 translation. */
8592 t0sz = extract32(tcr->raw_tcr, 0, 3);
8593 } else {
8594 /* AArch32 stage 2 translation. */
8595 bool sext = extract32(tcr->raw_tcr, 4, 1);
8596 bool sign = extract32(tcr->raw_tcr, 3, 1);
6e99f762
SS
8597 /* Address size is 40-bit for a stage 2 translation,
8598 * and t0sz can be negative (from -8 to 7),
8599 * so we need to adjust it to use the TTBR selecting logic below.
8600 */
8601 addrsize = 40;
8602 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
4ee38098
EI
8603
8604 /* If the sign-extend bit is not the same as t0sz[3], the result
8605 * is unpredictable. Flag this as a guest error. */
8606 if (sign != sext) {
8607 qemu_log_mask(LOG_GUEST_ERROR,
39cba610 8608 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
4ee38098 8609 }
2c8dd318 8610 }
1f4c8c18 8611 t1sz = extract32(tcr->raw_tcr, 16, 6);
6e99f762 8612 if (aarch64) {
2c8dd318
RH
8613 t1sz = MIN(t1sz, 39);
8614 t1sz = MAX(t1sz, 16);
8615 }
6e99f762 8616 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
3dde962f
PM
8617 /* there is a ttbr0 region and we are in it (high bits all zero) */
8618 ttbr_select = 0;
88e8add8 8619 } else if (ttbr1_valid && t1sz &&
6e99f762 8620 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
3dde962f
PM
8621 /* there is a ttbr1 region and we are in it (high bits all one) */
8622 ttbr_select = 1;
8623 } else if (!t0sz) {
8624 /* ttbr0 region is "everything not in the ttbr1 region" */
8625 ttbr_select = 0;
88e8add8 8626 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
8627 /* ttbr1 region is "everything not in the ttbr0 region" */
8628 ttbr_select = 1;
8629 } else {
8630 /* in the gap between the two regions, this is a Translation fault */
8631 fault_type = translation_fault;
8632 goto do_fault;
8633 }
8634
8635 /* Note that QEMU ignores shareability and cacheability attributes,
8636 * so we don't need to do anything with the SH, ORGN, IRGN fields
8637 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
8638 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
8639 * implement any ASID-like capability so we can ignore it (instead
8640 * we will always flush the TLB any time the ASID is changed).
8641 */
8642 if (ttbr_select == 0) {
aef878be 8643 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
8644 if (el < 2) {
8645 epd = extract32(tcr->raw_tcr, 7, 1);
8646 }
6e99f762 8647 inputsize = addrsize - t0sz;
2c8dd318 8648
11f136ee 8649 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 8650 if (tg == 1) { /* 64KB pages */
973a5434 8651 stride = 13;
2c8dd318
RH
8652 }
8653 if (tg == 2) { /* 16KB pages */
973a5434 8654 stride = 11;
2c8dd318 8655 }
3dde962f 8656 } else {
88e8add8
GB
8657 /* We should only be here if TTBR1 is valid */
8658 assert(ttbr1_valid);
8659
aef878be 8660 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 8661 epd = extract32(tcr->raw_tcr, 23, 1);
6e99f762 8662 inputsize = addrsize - t1sz;
2c8dd318 8663
11f136ee 8664 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 8665 if (tg == 3) { /* 64KB pages */
973a5434 8666 stride = 13;
2c8dd318
RH
8667 }
8668 if (tg == 1) { /* 16KB pages */
973a5434 8669 stride = 11;
2c8dd318 8670 }
3dde962f
PM
8671 }
8672
0480f69a 8673 /* Here we should have set up all the parameters for the translation:
6e99f762 8674 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
8675 */
8676
3dde962f 8677 if (epd) {
88e8add8
GB
8678 /* Translation table walk disabled => Translation fault on TLB miss
8679 * Note: This is always 0 on 64-bit EL2 and EL3.
8680 */
3dde962f
PM
8681 goto do_fault;
8682 }
8683
1853d5a9
EI
8684 if (mmu_idx != ARMMMUIdx_S2NS) {
8685 /* The starting level depends on the virtual address size (which can
8686 * be up to 48 bits) and the translation granule size. It indicates
8687 * the number of strides (stride bits at a time) needed to
8688 * consume the bits of the input address. In the pseudocode this is:
8689 * level = 4 - RoundUp((inputsize - grainsize) / stride)
8690 * where their 'inputsize' is our 'inputsize', 'grainsize' is
8691 * our 'stride + 3' and 'stride' is our 'stride'.
8692 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
8693 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
8694 * = 4 - (inputsize - 4) / stride;
8695 */
8696 level = 4 - (inputsize - 4) / stride;
8697 } else {
8698 /* For stage 2 translations the starting level is specified by the
8699 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
8700 */
1b4093ea
SS
8701 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
8702 uint32_t startlevel;
1853d5a9
EI
8703 bool ok;
8704
6e99f762 8705 if (!aarch64 || stride == 9) {
1853d5a9 8706 /* AArch32 or 4KB pages */
1b4093ea 8707 startlevel = 2 - sl0;
1853d5a9
EI
8708 } else {
8709 /* 16KB or 64KB pages */
1b4093ea 8710 startlevel = 3 - sl0;
1853d5a9
EI
8711 }
8712
8713 /* Check that the starting level is valid. */
6e99f762 8714 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
1b4093ea 8715 inputsize, stride);
1853d5a9 8716 if (!ok) {
1853d5a9
EI
8717 fault_type = translation_fault;
8718 goto do_fault;
8719 }
1b4093ea 8720 level = startlevel;
1853d5a9 8721 }
3dde962f 8722
dddb5223
SS
8723 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
8724 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
3dde962f
PM
8725
8726 /* Now we can extract the actual base address from the TTBR */
2c8dd318 8727 descaddr = extract64(ttbr, 0, 48);
dddb5223 8728 descaddr &= ~indexmask;
3dde962f 8729
6109769a 8730 /* The address field in the descriptor goes up to bit 39 for ARMv7
dddb5223
SS
8731 * but up to bit 47 for ARMv8, but we use the descaddrmask
8732 * up to bit 39 for AArch32, because we don't need other bits in that case
8733 * to construct next descriptor address (anyway they should be all zeroes).
6109769a 8734 */
6e99f762 8735 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
dddb5223 8736 ~indexmask_grainsize;
6109769a 8737
ebca90e4
PM
8738 /* Secure accesses start with the page table in secure memory and
8739 * can be downgraded to non-secure at any step. Non-secure accesses
8740 * remain non-secure. We implement this by just ORing in the NSTable/NS
8741 * bits at each step.
8742 */
8743 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
8744 for (;;) {
8745 uint64_t descriptor;
ebca90e4 8746 bool nstable;
3dde962f 8747
dddb5223 8748 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 8749 descaddr &= ~7ULL;
ebca90e4 8750 nstable = extract32(tableattrs, 4, 1);
37785977
EI
8751 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi);
8752 if (fi->s1ptw) {
8753 goto do_fault;
8754 }
8755
3dde962f
PM
8756 if (!(descriptor & 1) ||
8757 (!(descriptor & 2) && (level == 3))) {
8758 /* Invalid, or the Reserved level 3 encoding */
8759 goto do_fault;
8760 }
6109769a 8761 descaddr = descriptor & descaddrmask;
3dde962f
PM
8762
8763 if ((descriptor & 2) && (level < 3)) {
8764 /* Table entry. The top five bits are attributes which may
8765 * propagate down through lower levels of the table (and
8766 * which are all arranged so that 0 means "no effect", so
8767 * we can gather them up by ORing in the bits at each level).
8768 */
8769 tableattrs |= extract64(descriptor, 59, 5);
8770 level++;
dddb5223 8771 indexmask = indexmask_grainsize;
3dde962f
PM
8772 continue;
8773 }
8774 /* Block entry at level 1 or 2, or page entry at level 3.
8775 * These are basically the same thing, although the number
8776 * of bits we pull in from the vaddr varies.
8777 */
973a5434 8778 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 8779 descaddr |= (address & (page_size - 1));
6ab1a5ee 8780 /* Extract attributes from the descriptor */
d615efac
IC
8781 attrs = extract64(descriptor, 2, 10)
8782 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
8783
8784 if (mmu_idx == ARMMMUIdx_S2NS) {
8785 /* Stage 2 table descriptors do not include any attribute fields */
8786 break;
8787 }
8788 /* Merge in attributes from table descriptors */
3dde962f
PM
8789 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
8790 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
8791 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
8792 * means "force PL1 access only", which means forcing AP[1] to 0.
8793 */
8794 if (extract32(tableattrs, 2, 1)) {
8795 attrs &= ~(1 << 4);
8796 }
ebca90e4 8797 attrs |= nstable << 3; /* NS */
3dde962f
PM
8798 break;
8799 }
8800 /* Here descaddr is the final physical address, and attributes
8801 * are all in attrs.
8802 */
8803 fault_type = access_fault;
8804 if ((attrs & (1 << 8)) == 0) {
8805 /* Access flag */
8806 goto do_fault;
8807 }
d8e052b3
AJ
8808
8809 ap = extract32(attrs, 4, 2);
d8e052b3 8810 xn = extract32(attrs, 12, 1);
d8e052b3 8811
6ab1a5ee
EI
8812 if (mmu_idx == ARMMMUIdx_S2NS) {
8813 ns = true;
8814 *prot = get_S2prot(env, ap, xn);
8815 } else {
8816 ns = extract32(attrs, 3, 1);
8817 pxn = extract32(attrs, 11, 1);
6e99f762 8818 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 8819 }
d8e052b3 8820
3dde962f 8821 fault_type = permission_fault;
d8e052b3 8822 if (!(*prot & (1 << access_type))) {
3dde962f
PM
8823 goto do_fault;
8824 }
3dde962f 8825
8bf5b6a9
PM
8826 if (ns) {
8827 /* The NS bit will (as required by the architecture) have no effect if
8828 * the CPU doesn't support TZ or this is a non-secure translation
8829 * regime, because the attribute will already be non-secure.
8830 */
8831 txattrs->secure = false;
8832 }
3dde962f
PM
8833 *phys_ptr = descaddr;
8834 *page_size_ptr = page_size;
b7cc4e82 8835 return false;
3dde962f
PM
8836
8837do_fault:
8838 /* Long-descriptor format IFSR/DFSR value */
b7cc4e82 8839 *fsr = (1 << 9) | (fault_type << 2) | level;
37785977
EI
8840 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
8841 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 8842 return true;
3dde962f
PM
8843}
8844
f6bda88f
PC
8845static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
8846 ARMMMUIdx mmu_idx,
8847 int32_t address, int *prot)
8848{
3a00d560
MD
8849 if (!arm_feature(env, ARM_FEATURE_M)) {
8850 *prot = PAGE_READ | PAGE_WRITE;
8851 switch (address) {
8852 case 0xF0000000 ... 0xFFFFFFFF:
8853 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
8854 /* hivecs execing is ok */
8855 *prot |= PAGE_EXEC;
8856 }
8857 break;
8858 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 8859 *prot |= PAGE_EXEC;
3a00d560
MD
8860 break;
8861 }
8862 } else {
8863 /* Default system address map for M profile cores.
8864 * The architecture specifies which regions are execute-never;
8865 * at the MPU level no other checks are defined.
8866 */
8867 switch (address) {
8868 case 0x00000000 ... 0x1fffffff: /* ROM */
8869 case 0x20000000 ... 0x3fffffff: /* SRAM */
8870 case 0x60000000 ... 0x7fffffff: /* RAM */
8871 case 0x80000000 ... 0x9fffffff: /* RAM */
8872 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
8873 break;
8874 case 0x40000000 ... 0x5fffffff: /* Peripheral */
8875 case 0xa0000000 ... 0xbfffffff: /* Device */
8876 case 0xc0000000 ... 0xdfffffff: /* Device */
8877 case 0xe0000000 ... 0xffffffff: /* System */
8878 *prot = PAGE_READ | PAGE_WRITE;
8879 break;
8880 default:
8881 g_assert_not_reached();
f6bda88f 8882 }
f6bda88f 8883 }
f6bda88f
PC
8884}
8885
29c483a5
MD
8886static bool pmsav7_use_background_region(ARMCPU *cpu,
8887 ARMMMUIdx mmu_idx, bool is_user)
8888{
8889 /* Return true if we should use the default memory map as a
8890 * "background" region if there are no hits against any MPU regions.
8891 */
8892 CPUARMState *env = &cpu->env;
8893
8894 if (is_user) {
8895 return false;
8896 }
8897
8898 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
8899 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
8900 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
8901 } else {
8902 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
8903 }
8904}
8905
38aaa60c
PM
8906static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
8907{
8908 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
8909 return arm_feature(env, ARM_FEATURE_M) &&
8910 extract32(address, 20, 12) == 0xe00;
8911}
8912
bf446a11
PM
8913static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
8914{
8915 /* True if address is in the M profile system region
8916 * 0xe0000000 - 0xffffffff
8917 */
8918 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
8919}
8920
f6bda88f 8921static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 8922 MMUAccessType access_type, ARMMMUIdx mmu_idx,
f6bda88f
PC
8923 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
8924{
8925 ARMCPU *cpu = arm_env_get_cpu(env);
8926 int n;
8927 bool is_user = regime_is_user(env, mmu_idx);
8928
8929 *phys_ptr = address;
8930 *prot = 0;
8931
38aaa60c
PM
8932 if (regime_translation_disabled(env, mmu_idx) ||
8933 m_is_ppb_region(env, address)) {
8934 /* MPU disabled or M profile PPB access: use default memory map.
8935 * The other case which uses the default memory map in the
8936 * v7M ARM ARM pseudocode is exception vector reads from the vector
8937 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
8938 * which always does a direct read using address_space_ldl(), rather
8939 * than going via this function, so we don't need to check that here.
8940 */
f6bda88f
PC
8941 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
8942 } else { /* MPU enabled */
8943 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
8944 /* region search */
8945 uint32_t base = env->pmsav7.drbar[n];
8946 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
8947 uint32_t rmask;
8948 bool srdis = false;
8949
8950 if (!(env->pmsav7.drsr[n] & 0x1)) {
8951 continue;
8952 }
8953
8954 if (!rsize) {
c9f9f124
MD
8955 qemu_log_mask(LOG_GUEST_ERROR,
8956 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
8957 continue;
8958 }
8959 rsize++;
8960 rmask = (1ull << rsize) - 1;
8961
8962 if (base & rmask) {
c9f9f124
MD
8963 qemu_log_mask(LOG_GUEST_ERROR,
8964 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
8965 "to DRSR region size, mask = 0x%" PRIx32 "\n",
8966 n, base, rmask);
f6bda88f
PC
8967 continue;
8968 }
8969
8970 if (address < base || address > base + rmask) {
8971 continue;
8972 }
8973
8974 /* Region matched */
8975
8976 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
8977 int i, snd;
8978 uint32_t srdis_mask;
8979
8980 rsize -= 3; /* sub region size (power of 2) */
8981 snd = ((address - base) >> rsize) & 0x7;
8982 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
8983
8984 srdis_mask = srdis ? 0x3 : 0x0;
8985 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
8986 /* This will check in groups of 2, 4 and then 8, whether
8987 * the subregion bits are consistent. rsize is incremented
8988 * back up to give the region size, considering consistent
8989 * adjacent subregions as one region. Stop testing if rsize
8990 * is already big enough for an entire QEMU page.
8991 */
8992 int snd_rounded = snd & ~(i - 1);
8993 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
8994 snd_rounded + 8, i);
8995 if (srdis_mask ^ srdis_multi) {
8996 break;
8997 }
8998 srdis_mask = (srdis_mask << i) | srdis_mask;
8999 rsize++;
9000 }
9001 }
9002 if (rsize < TARGET_PAGE_BITS) {
c9f9f124
MD
9003 qemu_log_mask(LOG_UNIMP,
9004 "DRSR[%d]: No support for MPU (sub)region "
f6bda88f 9005 "alignment of %" PRIu32 " bits. Minimum is %d\n",
c9f9f124 9006 n, rsize, TARGET_PAGE_BITS);
f6bda88f
PC
9007 continue;
9008 }
9009 if (srdis) {
9010 continue;
9011 }
9012 break;
9013 }
9014
9015 if (n == -1) { /* no hits */
29c483a5 9016 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f
PC
9017 /* background fault */
9018 *fsr = 0;
9019 return true;
9020 }
9021 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9022 } else { /* a MPU hit! */
9023 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
9024 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9025
9026 if (m_is_system_region(env, address)) {
9027 /* System space is always execute never */
9028 xn = 1;
9029 }
f6bda88f
PC
9030
9031 if (is_user) { /* User mode AP bit decoding */
9032 switch (ap) {
9033 case 0:
9034 case 1:
9035 case 5:
9036 break; /* no access */
9037 case 3:
9038 *prot |= PAGE_WRITE;
9039 /* fall through */
9040 case 2:
9041 case 6:
9042 *prot |= PAGE_READ | PAGE_EXEC;
9043 break;
9044 default:
9045 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9046 "DRACR[%d]: Bad value for AP bits: 0x%"
9047 PRIx32 "\n", n, ap);
f6bda88f
PC
9048 }
9049 } else { /* Priv. mode AP bits decoding */
9050 switch (ap) {
9051 case 0:
9052 break; /* no access */
9053 case 1:
9054 case 2:
9055 case 3:
9056 *prot |= PAGE_WRITE;
9057 /* fall through */
9058 case 5:
9059 case 6:
9060 *prot |= PAGE_READ | PAGE_EXEC;
9061 break;
9062 default:
9063 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
9064 "DRACR[%d]: Bad value for AP bits: 0x%"
9065 PRIx32 "\n", n, ap);
f6bda88f
PC
9066 }
9067 }
9068
9069 /* execute never */
bf446a11 9070 if (xn) {
f6bda88f
PC
9071 *prot &= ~PAGE_EXEC;
9072 }
9073 }
9074 }
9075
9076 *fsr = 0x00d; /* Permission fault */
9077 return !(*prot & (1 << access_type));
9078}
9079
35337cc3
PM
9080static bool v8m_is_sau_exempt(CPUARMState *env,
9081 uint32_t address, MMUAccessType access_type)
9082{
9083 /* The architecture specifies that certain address ranges are
9084 * exempt from v8M SAU/IDAU checks.
9085 */
9086 return
9087 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9088 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9089 (address >= 0xe000e000 && address <= 0xe000efff) ||
9090 (address >= 0xe002e000 && address <= 0xe002efff) ||
9091 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9092 (address >= 0xe00ff000 && address <= 0xe00fffff);
9093}
9094
9095static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9096 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9097 V8M_SAttributes *sattrs)
9098{
9099 /* Look up the security attributes for this address. Compare the
9100 * pseudocode SecurityCheck() function.
9101 * We assume the caller has zero-initialized *sattrs.
9102 */
9103 ARMCPU *cpu = arm_env_get_cpu(env);
9104 int r;
9105
9106 /* TODO: implement IDAU */
9107
9108 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9109 /* 0xf0000000..0xffffffff is always S for insn fetches */
9110 return;
9111 }
9112
9113 if (v8m_is_sau_exempt(env, address, access_type)) {
9114 sattrs->ns = !regime_is_secure(env, mmu_idx);
9115 return;
9116 }
9117
9118 switch (env->sau.ctrl & 3) {
9119 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9120 break;
9121 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9122 sattrs->ns = true;
9123 break;
9124 default: /* SAU.ENABLE == 1 */
9125 for (r = 0; r < cpu->sau_sregion; r++) {
9126 if (env->sau.rlar[r] & 1) {
9127 uint32_t base = env->sau.rbar[r] & ~0x1f;
9128 uint32_t limit = env->sau.rlar[r] | 0x1f;
9129
9130 if (base <= address && limit >= address) {
9131 if (sattrs->srvalid) {
9132 /* If we hit in more than one region then we must report
9133 * as Secure, not NS-Callable, with no valid region
9134 * number info.
9135 */
9136 sattrs->ns = false;
9137 sattrs->nsc = false;
9138 sattrs->sregion = 0;
9139 sattrs->srvalid = false;
9140 break;
9141 } else {
9142 if (env->sau.rlar[r] & 2) {
9143 sattrs->nsc = true;
9144 } else {
9145 sattrs->ns = true;
9146 }
9147 sattrs->srvalid = true;
9148 sattrs->sregion = r;
9149 }
9150 }
9151 }
9152 }
9153
9154 /* TODO when we support the IDAU then it may override the result here */
9155 break;
9156 }
9157}
9158
504e3cc3
PM
9159static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
9160 MMUAccessType access_type, ARMMMUIdx mmu_idx,
35337cc3
PM
9161 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9162 int *prot, uint32_t *fsr)
504e3cc3
PM
9163{
9164 ARMCPU *cpu = arm_env_get_cpu(env);
9165 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 9166 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
9167 int n;
9168 int matchregion = -1;
9169 bool hit = false;
35337cc3 9170 V8M_SAttributes sattrs = {};
504e3cc3
PM
9171
9172 *phys_ptr = address;
9173 *prot = 0;
9174
35337cc3
PM
9175 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9176 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
9177 if (access_type == MMU_INST_FETCH) {
9178 /* Instruction fetches always use the MMU bank and the
9179 * transaction attribute determined by the fetch address,
9180 * regardless of CPU state. This is painful for QEMU
9181 * to handle, because it would mean we need to encode
9182 * into the mmu_idx not just the (user, negpri) information
9183 * for the current security state but also that for the
9184 * other security state, which would balloon the number
9185 * of mmu_idx values needed alarmingly.
9186 * Fortunately we can avoid this because it's not actually
9187 * possible to arbitrarily execute code from memory with
9188 * the wrong security attribute: it will always generate
9189 * an exception of some kind or another, apart from the
9190 * special case of an NS CPU executing an SG instruction
9191 * in S&NSC memory. So we always just fail the translation
9192 * here and sort things out in the exception handler
9193 * (including possibly emulating an SG instruction).
9194 */
9195 if (sattrs.ns != !secure) {
9196 *fsr = sattrs.nsc ? M_FAKE_FSR_NSC_EXEC : M_FAKE_FSR_SFAULT;
9197 return true;
9198 }
9199 } else {
9200 /* For data accesses we always use the MMU bank indicated
9201 * by the current CPU state, but the security attributes
9202 * might downgrade a secure access to nonsecure.
9203 */
9204 if (sattrs.ns) {
9205 txattrs->secure = false;
9206 } else if (!secure) {
9207 /* NS access to S memory must fault.
9208 * Architecturally we should first check whether the
9209 * MPU information for this address indicates that we
9210 * are doing an unaligned access to Device memory, which
9211 * should generate a UsageFault instead. QEMU does not
9212 * currently check for that kind of unaligned access though.
9213 * If we added it we would need to do so as a special case
9214 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
9215 */
9216 *fsr = M_FAKE_FSR_SFAULT;
9217 return true;
9218 }
9219 }
9220 }
9221
504e3cc3
PM
9222 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9223 * was an exception vector read from the vector table (which is always
9224 * done using the default system address map), because those accesses
9225 * are done in arm_v7m_load_vector(), which always does a direct
9226 * read using address_space_ldl(), rather than going via this function.
9227 */
9228 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9229 hit = true;
9230 } else if (m_is_ppb_region(env, address)) {
9231 hit = true;
9232 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9233 hit = true;
9234 } else {
9235 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9236 /* region search */
9237 /* Note that the base address is bits [31:5] from the register
9238 * with bits [4:0] all zeroes, but the limit address is bits
9239 * [31:5] from the register with bits [4:0] all ones.
9240 */
62c58ee0
PM
9241 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9242 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 9243
62c58ee0 9244 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
9245 /* Region disabled */
9246 continue;
9247 }
9248
9249 if (address < base || address > limit) {
9250 continue;
9251 }
9252
9253 if (hit) {
9254 /* Multiple regions match -- always a failure (unlike
9255 * PMSAv7 where highest-numbered-region wins)
9256 */
9257 *fsr = 0x00d; /* permission fault */
9258 return true;
9259 }
9260
9261 matchregion = n;
9262 hit = true;
9263
9264 if (base & ~TARGET_PAGE_MASK) {
9265 qemu_log_mask(LOG_UNIMP,
9266 "MPU_RBAR[%d]: No support for MPU region base"
9267 "address of 0x%" PRIx32 ". Minimum alignment is "
9268 "%d\n",
9269 n, base, TARGET_PAGE_BITS);
9270 continue;
9271 }
9272 if ((limit + 1) & ~TARGET_PAGE_MASK) {
9273 qemu_log_mask(LOG_UNIMP,
9274 "MPU_RBAR[%d]: No support for MPU region limit"
9275 "address of 0x%" PRIx32 ". Minimum alignment is "
9276 "%d\n",
9277 n, limit, TARGET_PAGE_BITS);
9278 continue;
9279 }
9280 }
9281 }
9282
9283 if (!hit) {
9284 /* background fault */
9285 *fsr = 0;
9286 return true;
9287 }
9288
9289 if (matchregion == -1) {
9290 /* hit using the background region */
9291 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9292 } else {
62c58ee0
PM
9293 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
9294 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
504e3cc3
PM
9295
9296 if (m_is_system_region(env, address)) {
9297 /* System space is always execute never */
9298 xn = 1;
9299 }
9300
9301 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
9302 if (*prot && !xn) {
9303 *prot |= PAGE_EXEC;
9304 }
9305 /* We don't need to look the attribute up in the MAIR0/MAIR1
9306 * registers because that only tells us about cacheability.
9307 */
9308 }
9309
9310 *fsr = 0x00d; /* Permission fault */
9311 return !(*prot & (1 << access_type));
9312}
9313
13689d43 9314static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 9315 MMUAccessType access_type, ARMMMUIdx mmu_idx,
13689d43 9316 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9ee6e8bb
PB
9317{
9318 int n;
9319 uint32_t mask;
9320 uint32_t base;
0480f69a 9321 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 9322
3279adb9
PM
9323 if (regime_translation_disabled(env, mmu_idx)) {
9324 /* MPU disabled. */
9325 *phys_ptr = address;
9326 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9327 return false;
9328 }
9329
9ee6e8bb
PB
9330 *phys_ptr = address;
9331 for (n = 7; n >= 0; n--) {
554b0b09 9332 base = env->cp15.c6_region[n];
87c3d486 9333 if ((base & 1) == 0) {
554b0b09 9334 continue;
87c3d486 9335 }
554b0b09
PM
9336 mask = 1 << ((base >> 1) & 0x1f);
9337 /* Keep this shift separate from the above to avoid an
9338 (undefined) << 32. */
9339 mask = (mask << 1) - 1;
87c3d486 9340 if (((base ^ address) & ~mask) == 0) {
554b0b09 9341 break;
87c3d486 9342 }
9ee6e8bb 9343 }
87c3d486 9344 if (n < 0) {
b7cc4e82
PC
9345 *fsr = 2;
9346 return true;
87c3d486 9347 }
9ee6e8bb 9348
03ae85f8 9349 if (access_type == MMU_INST_FETCH) {
7e09797c 9350 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 9351 } else {
7e09797c 9352 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
9353 }
9354 mask = (mask >> (n * 4)) & 0xf;
9355 switch (mask) {
9356 case 0:
b7cc4e82
PC
9357 *fsr = 1;
9358 return true;
9ee6e8bb 9359 case 1:
87c3d486 9360 if (is_user) {
b7cc4e82
PC
9361 *fsr = 1;
9362 return true;
87c3d486 9363 }
554b0b09
PM
9364 *prot = PAGE_READ | PAGE_WRITE;
9365 break;
9ee6e8bb 9366 case 2:
554b0b09 9367 *prot = PAGE_READ;
87c3d486 9368 if (!is_user) {
554b0b09 9369 *prot |= PAGE_WRITE;
87c3d486 9370 }
554b0b09 9371 break;
9ee6e8bb 9372 case 3:
554b0b09
PM
9373 *prot = PAGE_READ | PAGE_WRITE;
9374 break;
9ee6e8bb 9375 case 5:
87c3d486 9376 if (is_user) {
b7cc4e82
PC
9377 *fsr = 1;
9378 return true;
87c3d486 9379 }
554b0b09
PM
9380 *prot = PAGE_READ;
9381 break;
9ee6e8bb 9382 case 6:
554b0b09
PM
9383 *prot = PAGE_READ;
9384 break;
9ee6e8bb 9385 default:
554b0b09 9386 /* Bad permission. */
b7cc4e82
PC
9387 *fsr = 1;
9388 return true;
9ee6e8bb 9389 }
3ad493fc 9390 *prot |= PAGE_EXEC;
b7cc4e82 9391 return false;
9ee6e8bb
PB
9392}
9393
702a9357
PM
9394/* get_phys_addr - get the physical address for this virtual address
9395 *
9396 * Find the physical address corresponding to the given virtual address,
9397 * by doing a translation table walk on MMU based systems or using the
9398 * MPU state on MPU based systems.
9399 *
b7cc4e82
PC
9400 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
9401 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
9402 * information on why the translation aborted, in the format of a
9403 * DFSR/IFSR fault register, with the following caveats:
9404 * * we honour the short vs long DFSR format differences.
9405 * * the WnR bit is never set (the caller must do this).
f6bda88f 9406 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
9407 * value.
9408 *
9409 * @env: CPUARMState
9410 * @address: virtual address to get physical address for
9411 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 9412 * @mmu_idx: MMU index indicating required translation regime
702a9357 9413 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 9414 * @attrs: set to the memory transaction attributes to use
702a9357
PM
9415 * @prot: set to the permissions for the page containing phys_ptr
9416 * @page_size: set to the size of the page containing phys_ptr
b7cc4e82 9417 * @fsr: set to the DFSR/IFSR value on failure
702a9357 9418 */
af51f566 9419static bool get_phys_addr(CPUARMState *env, target_ulong address,
03ae85f8 9420 MMUAccessType access_type, ARMMMUIdx mmu_idx,
af51f566 9421 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
9422 target_ulong *page_size, uint32_t *fsr,
9423 ARMMMUFaultInfo *fi)
9ee6e8bb 9424{
0480f69a 9425 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
9426 /* Call ourselves recursively to do the stage 1 and then stage 2
9427 * translations.
0480f69a 9428 */
9b539263
EI
9429 if (arm_feature(env, ARM_FEATURE_EL2)) {
9430 hwaddr ipa;
9431 int s2_prot;
9432 int ret;
9433
9434 ret = get_phys_addr(env, address, access_type,
8bd5c820 9435 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
9b539263
EI
9436 prot, page_size, fsr, fi);
9437
9438 /* If S1 fails or S2 is disabled, return early. */
9439 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
9440 *phys_ptr = ipa;
9441 return ret;
9442 }
9443
9444 /* S1 is done. Now do S2 translation. */
9445 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
9446 phys_ptr, attrs, &s2_prot,
9447 page_size, fsr, fi);
9448 fi->s2addr = ipa;
9449 /* Combine the S1 and S2 perms. */
9450 *prot &= s2_prot;
9451 return ret;
9452 } else {
9453 /*
9454 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
9455 */
8bd5c820 9456 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 9457 }
0480f69a 9458 }
d3649702 9459
8bf5b6a9
PM
9460 /* The page table entries may downgrade secure to non-secure, but
9461 * cannot upgrade an non-secure translation regime's attributes
9462 * to secure.
9463 */
9464 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 9465 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 9466
0480f69a
PM
9467 /* Fast Context Switch Extension. This doesn't exist at all in v8.
9468 * In v7 and earlier it affects all stage 1 translations.
9469 */
9470 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
9471 && !arm_feature(env, ARM_FEATURE_V8)) {
9472 if (regime_el(env, mmu_idx) == 3) {
9473 address += env->cp15.fcseidr_s;
9474 } else {
9475 address += env->cp15.fcseidr_ns;
9476 }
54bf36ed 9477 }
9ee6e8bb 9478
3279adb9 9479 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 9480 bool ret;
f6bda88f 9481 *page_size = TARGET_PAGE_SIZE;
3279adb9 9482
504e3cc3
PM
9483 if (arm_feature(env, ARM_FEATURE_V8)) {
9484 /* PMSAv8 */
9485 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
35337cc3 9486 phys_ptr, attrs, prot, fsr);
504e3cc3 9487 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
9488 /* PMSAv7 */
9489 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
9490 phys_ptr, prot, fsr);
9491 } else {
9492 /* Pre-v7 MPU */
9493 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
9494 phys_ptr, prot, fsr);
9495 }
9496 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 9497 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
9498 access_type == MMU_DATA_LOAD ? "reading" :
9499 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
9500 (uint32_t)address, mmu_idx,
9501 ret ? "Miss" : "Hit",
9502 *prot & PAGE_READ ? 'r' : '-',
9503 *prot & PAGE_WRITE ? 'w' : '-',
9504 *prot & PAGE_EXEC ? 'x' : '-');
9505
9506 return ret;
f6bda88f
PC
9507 }
9508
3279adb9
PM
9509 /* Definitely a real MMU, not an MPU */
9510
0480f69a 9511 if (regime_translation_disabled(env, mmu_idx)) {
3279adb9 9512 /* MMU disabled. */
9ee6e8bb 9513 *phys_ptr = address;
3ad493fc 9514 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 9515 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 9516 return 0;
0480f69a
PM
9517 }
9518
0480f69a
PM
9519 if (regime_using_lpae_format(env, mmu_idx)) {
9520 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 9521 attrs, prot, page_size, fsr, fi);
0480f69a
PM
9522 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
9523 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 9524 attrs, prot, page_size, fsr, fi);
9ee6e8bb 9525 } else {
0480f69a 9526 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 9527 prot, page_size, fsr, fi);
9ee6e8bb
PB
9528 }
9529}
9530
8c6084bf 9531/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
9532 * to the TLB. Return false on success, or true on failure. Populate
9533 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 9534 */
b7cc4e82 9535bool arm_tlb_fill(CPUState *cs, vaddr address,
03ae85f8 9536 MMUAccessType access_type, int mmu_idx, uint32_t *fsr,
e14b5a23 9537 ARMMMUFaultInfo *fi)
b5ff1b31 9538{
7510454e
AF
9539 ARMCPU *cpu = ARM_CPU(cs);
9540 CPUARMState *env = &cpu->env;
a8170e5e 9541 hwaddr phys_addr;
d4c430a8 9542 target_ulong page_size;
b5ff1b31 9543 int prot;
d3649702 9544 int ret;
8bf5b6a9 9545 MemTxAttrs attrs = {};
b5ff1b31 9546
8bd5c820
PM
9547 ret = get_phys_addr(env, address, access_type,
9548 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
e14b5a23 9549 &attrs, &prot, &page_size, fsr, fi);
b7cc4e82 9550 if (!ret) {
b5ff1b31 9551 /* Map a single [sub]page. */
dcd82c11
AB
9552 phys_addr &= TARGET_PAGE_MASK;
9553 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
9554 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
9555 prot, mmu_idx, page_size);
d4c430a8 9556 return 0;
b5ff1b31
FB
9557 }
9558
8c6084bf 9559 return ret;
b5ff1b31
FB
9560}
9561
0faea0c7
PM
9562hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
9563 MemTxAttrs *attrs)
b5ff1b31 9564{
00b941e5 9565 ARMCPU *cpu = ARM_CPU(cs);
d3649702 9566 CPUARMState *env = &cpu->env;
a8170e5e 9567 hwaddr phys_addr;
d4c430a8 9568 target_ulong page_size;
b5ff1b31 9569 int prot;
b7cc4e82
PC
9570 bool ret;
9571 uint32_t fsr;
e14b5a23 9572 ARMMMUFaultInfo fi = {};
8bd5c820 9573 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
b5ff1b31 9574
0faea0c7
PM
9575 *attrs = (MemTxAttrs) {};
9576
8bd5c820 9577 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
0faea0c7 9578 attrs, &prot, &page_size, &fsr, &fi);
b5ff1b31 9579
b7cc4e82 9580 if (ret) {
b5ff1b31 9581 return -1;
00b941e5 9582 }
b5ff1b31
FB
9583 return phys_addr;
9584}
9585
0ecb72a5 9586uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 9587{
58117c9b
MD
9588 uint32_t mask;
9589 unsigned el = arm_current_el(env);
9590
9591 /* First handle registers which unprivileged can read */
9592
9593 switch (reg) {
9594 case 0 ... 7: /* xPSR sub-fields */
9595 mask = 0;
9596 if ((reg & 1) && el) {
987ab45e 9597 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
58117c9b
MD
9598 }
9599 if (!(reg & 4)) {
987ab45e 9600 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
58117c9b
MD
9601 }
9602 /* EPSR reads as zero */
9603 return xpsr_read(env) & mask;
9604 break;
9605 case 20: /* CONTROL */
8bfc26ea 9606 return env->v7m.control[env->v7m.secure];
50f11062
PM
9607 case 0x94: /* CONTROL_NS */
9608 /* We have to handle this here because unprivileged Secure code
9609 * can read the NS CONTROL register.
9610 */
9611 if (!env->v7m.secure) {
9612 return 0;
9613 }
9614 return env->v7m.control[M_REG_NS];
58117c9b
MD
9615 }
9616
9617 if (el == 0) {
9618 return 0; /* unprivileged reads others as zero */
9619 }
a47dddd7 9620
50f11062
PM
9621 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9622 switch (reg) {
9623 case 0x88: /* MSP_NS */
9624 if (!env->v7m.secure) {
9625 return 0;
9626 }
9627 return env->v7m.other_ss_msp;
9628 case 0x89: /* PSP_NS */
9629 if (!env->v7m.secure) {
9630 return 0;
9631 }
9632 return env->v7m.other_ss_psp;
9633 case 0x90: /* PRIMASK_NS */
9634 if (!env->v7m.secure) {
9635 return 0;
9636 }
9637 return env->v7m.primask[M_REG_NS];
9638 case 0x91: /* BASEPRI_NS */
9639 if (!env->v7m.secure) {
9640 return 0;
9641 }
9642 return env->v7m.basepri[M_REG_NS];
9643 case 0x93: /* FAULTMASK_NS */
9644 if (!env->v7m.secure) {
9645 return 0;
9646 }
9647 return env->v7m.faultmask[M_REG_NS];
9648 case 0x98: /* SP_NS */
9649 {
9650 /* This gives the non-secure SP selected based on whether we're
9651 * currently in handler mode or not, using the NS CONTROL.SPSEL.
9652 */
9653 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
9654
9655 if (!env->v7m.secure) {
9656 return 0;
9657 }
9658 if (!arm_v7m_is_handler_mode(env) && spsel) {
9659 return env->v7m.other_ss_psp;
9660 } else {
9661 return env->v7m.other_ss_msp;
9662 }
9663 }
9664 default:
9665 break;
9666 }
9667 }
9668
9ee6e8bb 9669 switch (reg) {
9ee6e8bb 9670 case 8: /* MSP */
8bfc26ea 9671 return (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) ?
abc24d86 9672 env->v7m.other_sp : env->regs[13];
9ee6e8bb 9673 case 9: /* PSP */
8bfc26ea 9674 return (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) ?
abc24d86 9675 env->regs[13] : env->v7m.other_sp;
9ee6e8bb 9676 case 16: /* PRIMASK */
6d804834 9677 return env->v7m.primask[env->v7m.secure];
82845826
SH
9678 case 17: /* BASEPRI */
9679 case 18: /* BASEPRI_MAX */
acf94941 9680 return env->v7m.basepri[env->v7m.secure];
82845826 9681 case 19: /* FAULTMASK */
42a6686b 9682 return env->v7m.faultmask[env->v7m.secure];
9ee6e8bb 9683 default:
58117c9b
MD
9684 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
9685 " register %d\n", reg);
9ee6e8bb
PB
9686 return 0;
9687 }
9688}
9689
b28b3377
PM
9690void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
9691{
9692 /* We're passed bits [11..0] of the instruction; extract
9693 * SYSm and the mask bits.
9694 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
9695 * we choose to treat them as if the mask bits were valid.
9696 * NB that the pseudocode 'mask' variable is bits [11..10],
9697 * whereas ours is [11..8].
9698 */
9699 uint32_t mask = extract32(maskreg, 8, 4);
9700 uint32_t reg = extract32(maskreg, 0, 8);
9701
58117c9b
MD
9702 if (arm_current_el(env) == 0 && reg > 7) {
9703 /* only xPSR sub-fields may be written by unprivileged */
9704 return;
9705 }
a47dddd7 9706
50f11062
PM
9707 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
9708 switch (reg) {
9709 case 0x88: /* MSP_NS */
9710 if (!env->v7m.secure) {
9711 return;
9712 }
9713 env->v7m.other_ss_msp = val;
9714 return;
9715 case 0x89: /* PSP_NS */
9716 if (!env->v7m.secure) {
9717 return;
9718 }
9719 env->v7m.other_ss_psp = val;
9720 return;
9721 case 0x90: /* PRIMASK_NS */
9722 if (!env->v7m.secure) {
9723 return;
9724 }
9725 env->v7m.primask[M_REG_NS] = val & 1;
9726 return;
9727 case 0x91: /* BASEPRI_NS */
9728 if (!env->v7m.secure) {
9729 return;
9730 }
9731 env->v7m.basepri[M_REG_NS] = val & 0xff;
9732 return;
9733 case 0x93: /* FAULTMASK_NS */
9734 if (!env->v7m.secure) {
9735 return;
9736 }
9737 env->v7m.faultmask[M_REG_NS] = val & 1;
9738 return;
9739 case 0x98: /* SP_NS */
9740 {
9741 /* This gives the non-secure SP selected based on whether we're
9742 * currently in handler mode or not, using the NS CONTROL.SPSEL.
9743 */
9744 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
9745
9746 if (!env->v7m.secure) {
9747 return;
9748 }
9749 if (!arm_v7m_is_handler_mode(env) && spsel) {
9750 env->v7m.other_ss_psp = val;
9751 } else {
9752 env->v7m.other_ss_msp = val;
9753 }
9754 return;
9755 }
9756 default:
9757 break;
9758 }
9759 }
9760
9ee6e8bb 9761 switch (reg) {
58117c9b
MD
9762 case 0 ... 7: /* xPSR sub-fields */
9763 /* only APSR is actually writable */
b28b3377
PM
9764 if (!(reg & 4)) {
9765 uint32_t apsrmask = 0;
9766
9767 if (mask & 8) {
987ab45e 9768 apsrmask |= XPSR_NZCV | XPSR_Q;
b28b3377
PM
9769 }
9770 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
987ab45e 9771 apsrmask |= XPSR_GE;
b28b3377
PM
9772 }
9773 xpsr_write(env, val, apsrmask);
58117c9b 9774 }
9ee6e8bb
PB
9775 break;
9776 case 8: /* MSP */
8bfc26ea 9777 if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) {
9ee6e8bb 9778 env->v7m.other_sp = val;
abc24d86 9779 } else {
9ee6e8bb 9780 env->regs[13] = val;
abc24d86 9781 }
9ee6e8bb
PB
9782 break;
9783 case 9: /* PSP */
8bfc26ea 9784 if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK) {
9ee6e8bb 9785 env->regs[13] = val;
abc24d86 9786 } else {
9ee6e8bb 9787 env->v7m.other_sp = val;
abc24d86 9788 }
9ee6e8bb
PB
9789 break;
9790 case 16: /* PRIMASK */
6d804834 9791 env->v7m.primask[env->v7m.secure] = val & 1;
9ee6e8bb 9792 break;
82845826 9793 case 17: /* BASEPRI */
acf94941 9794 env->v7m.basepri[env->v7m.secure] = val & 0xff;
9ee6e8bb 9795 break;
82845826 9796 case 18: /* BASEPRI_MAX */
9ee6e8bb 9797 val &= 0xff;
acf94941
PM
9798 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
9799 || env->v7m.basepri[env->v7m.secure] == 0)) {
9800 env->v7m.basepri[env->v7m.secure] = val;
9801 }
9ee6e8bb 9802 break;
82845826 9803 case 19: /* FAULTMASK */
42a6686b 9804 env->v7m.faultmask[env->v7m.secure] = val & 1;
82845826 9805 break;
9ee6e8bb 9806 case 20: /* CONTROL */
792dac30
PM
9807 /* Writing to the SPSEL bit only has an effect if we are in
9808 * thread mode; other bits can be updated by any privileged code.
de2db7ec 9809 * write_v7m_control_spsel() deals with updating the SPSEL bit in
792dac30
PM
9810 * env->v7m.control, so we only need update the others.
9811 */
15b3f556 9812 if (!arm_v7m_is_handler_mode(env)) {
de2db7ec 9813 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
792dac30 9814 }
8bfc26ea
PM
9815 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
9816 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
9ee6e8bb
PB
9817 break;
9818 default:
58117c9b
MD
9819 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
9820 " register %d\n", reg);
9ee6e8bb
PB
9821 return;
9822 }
9823}
9824
b5ff1b31 9825#endif
6ddbc6e4 9826
aca3f40b
PM
9827void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
9828{
9829 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
9830 * Note that we do not implement the (architecturally mandated)
9831 * alignment fault for attempts to use this on Device memory
9832 * (which matches the usual QEMU behaviour of not implementing either
9833 * alignment faults or any memory attribute handling).
9834 */
9835
9836 ARMCPU *cpu = arm_env_get_cpu(env);
9837 uint64_t blocklen = 4 << cpu->dcz_blocksize;
9838 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
9839
9840#ifndef CONFIG_USER_ONLY
9841 {
9842 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
9843 * the block size so we might have to do more than one TLB lookup.
9844 * We know that in fact for any v8 CPU the page size is at least 4K
9845 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
9846 * 1K as an artefact of legacy v5 subpage support being present in the
9847 * same QEMU executable.
9848 */
9849 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
9850 void *hostaddr[maxidx];
9851 int try, i;
97ed5ccd 9852 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 9853 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
9854
9855 for (try = 0; try < 2; try++) {
9856
9857 for (i = 0; i < maxidx; i++) {
9858 hostaddr[i] = tlb_vaddr_to_host(env,
9859 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 9860 1, mmu_idx);
aca3f40b
PM
9861 if (!hostaddr[i]) {
9862 break;
9863 }
9864 }
9865 if (i == maxidx) {
9866 /* If it's all in the TLB it's fair game for just writing to;
9867 * we know we don't need to update dirty status, etc.
9868 */
9869 for (i = 0; i < maxidx - 1; i++) {
9870 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
9871 }
9872 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
9873 return;
9874 }
9875 /* OK, try a store and see if we can populate the tlb. This
9876 * might cause an exception if the memory isn't writable,
9877 * in which case we will longjmp out of here. We must for
9878 * this purpose use the actual register value passed to us
9879 * so that we get the fault address right.
9880 */
01ecaf43 9881 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
aca3f40b
PM
9882 /* Now we can populate the other TLB entries, if any */
9883 for (i = 0; i < maxidx; i++) {
9884 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
9885 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
01ecaf43 9886 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
aca3f40b
PM
9887 }
9888 }
9889 }
9890
9891 /* Slow path (probably attempt to do this to an I/O device or
9892 * similar, or clearing of a block of code we have translations
9893 * cached for). Just do a series of byte writes as the architecture
9894 * demands. It's not worth trying to use a cpu_physical_memory_map(),
9895 * memset(), unmap() sequence here because:
9896 * + we'd need to account for the blocksize being larger than a page
9897 * + the direct-RAM access case is almost always going to be dealt
9898 * with in the fastpath code above, so there's no speed benefit
9899 * + we would have to deal with the map returning NULL because the
9900 * bounce buffer was in use
9901 */
9902 for (i = 0; i < blocklen; i++) {
01ecaf43 9903 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
aca3f40b
PM
9904 }
9905 }
9906#else
9907 memset(g2h(vaddr), 0, blocklen);
9908#endif
9909}
9910
6ddbc6e4
PB
9911/* Note that signed overflow is undefined in C. The following routines are
9912 careful to use unsigned types where modulo arithmetic is required.
9913 Failure to do so _will_ break on newer gcc. */
9914
9915/* Signed saturating arithmetic. */
9916
1654b2d6 9917/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
9918static inline uint16_t add16_sat(uint16_t a, uint16_t b)
9919{
9920 uint16_t res;
9921
9922 res = a + b;
9923 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
9924 if (a & 0x8000)
9925 res = 0x8000;
9926 else
9927 res = 0x7fff;
9928 }
9929 return res;
9930}
9931
1654b2d6 9932/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
9933static inline uint8_t add8_sat(uint8_t a, uint8_t b)
9934{
9935 uint8_t res;
9936
9937 res = a + b;
9938 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
9939 if (a & 0x80)
9940 res = 0x80;
9941 else
9942 res = 0x7f;
9943 }
9944 return res;
9945}
9946
1654b2d6 9947/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
9948static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
9949{
9950 uint16_t res;
9951
9952 res = a - b;
9953 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
9954 if (a & 0x8000)
9955 res = 0x8000;
9956 else
9957 res = 0x7fff;
9958 }
9959 return res;
9960}
9961
1654b2d6 9962/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
9963static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
9964{
9965 uint8_t res;
9966
9967 res = a - b;
9968 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
9969 if (a & 0x80)
9970 res = 0x80;
9971 else
9972 res = 0x7f;
9973 }
9974 return res;
9975}
9976
9977#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
9978#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
9979#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
9980#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
9981#define PFX q
9982
9983#include "op_addsub.h"
9984
9985/* Unsigned saturating arithmetic. */
460a09c1 9986static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
9987{
9988 uint16_t res;
9989 res = a + b;
9990 if (res < a)
9991 res = 0xffff;
9992 return res;
9993}
9994
460a09c1 9995static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 9996{
4c4fd3f8 9997 if (a > b)
6ddbc6e4
PB
9998 return a - b;
9999 else
10000 return 0;
10001}
10002
10003static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10004{
10005 uint8_t res;
10006 res = a + b;
10007 if (res < a)
10008 res = 0xff;
10009 return res;
10010}
10011
10012static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10013{
4c4fd3f8 10014 if (a > b)
6ddbc6e4
PB
10015 return a - b;
10016 else
10017 return 0;
10018}
10019
10020#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10021#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10022#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10023#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10024#define PFX uq
10025
10026#include "op_addsub.h"
10027
10028/* Signed modulo arithmetic. */
10029#define SARITH16(a, b, n, op) do { \
10030 int32_t sum; \
db6e2e65 10031 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
10032 RESULT(sum, n, 16); \
10033 if (sum >= 0) \
10034 ge |= 3 << (n * 2); \
10035 } while(0)
10036
10037#define SARITH8(a, b, n, op) do { \
10038 int32_t sum; \
db6e2e65 10039 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
10040 RESULT(sum, n, 8); \
10041 if (sum >= 0) \
10042 ge |= 1 << n; \
10043 } while(0)
10044
10045
10046#define ADD16(a, b, n) SARITH16(a, b, n, +)
10047#define SUB16(a, b, n) SARITH16(a, b, n, -)
10048#define ADD8(a, b, n) SARITH8(a, b, n, +)
10049#define SUB8(a, b, n) SARITH8(a, b, n, -)
10050#define PFX s
10051#define ARITH_GE
10052
10053#include "op_addsub.h"
10054
10055/* Unsigned modulo arithmetic. */
10056#define ADD16(a, b, n) do { \
10057 uint32_t sum; \
10058 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10059 RESULT(sum, n, 16); \
a87aa10b 10060 if ((sum >> 16) == 1) \
6ddbc6e4
PB
10061 ge |= 3 << (n * 2); \
10062 } while(0)
10063
10064#define ADD8(a, b, n) do { \
10065 uint32_t sum; \
10066 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10067 RESULT(sum, n, 8); \
a87aa10b
AZ
10068 if ((sum >> 8) == 1) \
10069 ge |= 1 << n; \
6ddbc6e4
PB
10070 } while(0)
10071
10072#define SUB16(a, b, n) do { \
10073 uint32_t sum; \
10074 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10075 RESULT(sum, n, 16); \
10076 if ((sum >> 16) == 0) \
10077 ge |= 3 << (n * 2); \
10078 } while(0)
10079
10080#define SUB8(a, b, n) do { \
10081 uint32_t sum; \
10082 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10083 RESULT(sum, n, 8); \
10084 if ((sum >> 8) == 0) \
a87aa10b 10085 ge |= 1 << n; \
6ddbc6e4
PB
10086 } while(0)
10087
10088#define PFX u
10089#define ARITH_GE
10090
10091#include "op_addsub.h"
10092
10093/* Halved signed arithmetic. */
10094#define ADD16(a, b, n) \
10095 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10096#define SUB16(a, b, n) \
10097 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10098#define ADD8(a, b, n) \
10099 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10100#define SUB8(a, b, n) \
10101 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10102#define PFX sh
10103
10104#include "op_addsub.h"
10105
10106/* Halved unsigned arithmetic. */
10107#define ADD16(a, b, n) \
10108 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10109#define SUB16(a, b, n) \
10110 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10111#define ADD8(a, b, n) \
10112 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10113#define SUB8(a, b, n) \
10114 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10115#define PFX uh
10116
10117#include "op_addsub.h"
10118
10119static inline uint8_t do_usad(uint8_t a, uint8_t b)
10120{
10121 if (a > b)
10122 return a - b;
10123 else
10124 return b - a;
10125}
10126
10127/* Unsigned sum of absolute byte differences. */
10128uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10129{
10130 uint32_t sum;
10131 sum = do_usad(a, b);
10132 sum += do_usad(a >> 8, b >> 8);
10133 sum += do_usad(a >> 16, b >>16);
10134 sum += do_usad(a >> 24, b >> 24);
10135 return sum;
10136}
10137
10138/* For ARMv6 SEL instruction. */
10139uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10140{
10141 uint32_t mask;
10142
10143 mask = 0;
10144 if (flags & 1)
10145 mask |= 0xff;
10146 if (flags & 2)
10147 mask |= 0xff00;
10148 if (flags & 4)
10149 mask |= 0xff0000;
10150 if (flags & 8)
10151 mask |= 0xff000000;
10152 return (a & mask) | (b & ~mask);
10153}
10154
b90372ad
PM
10155/* VFP support. We follow the convention used for VFP instructions:
10156 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
10157 "d" suffix. */
10158
10159/* Convert host exception flags to vfp form. */
10160static inline int vfp_exceptbits_from_host(int host_bits)
10161{
10162 int target_bits = 0;
10163
10164 if (host_bits & float_flag_invalid)
10165 target_bits |= 1;
10166 if (host_bits & float_flag_divbyzero)
10167 target_bits |= 2;
10168 if (host_bits & float_flag_overflow)
10169 target_bits |= 4;
36802b6b 10170 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
10171 target_bits |= 8;
10172 if (host_bits & float_flag_inexact)
10173 target_bits |= 0x10;
cecd8504
PM
10174 if (host_bits & float_flag_input_denormal)
10175 target_bits |= 0x80;
4373f3ce
PB
10176 return target_bits;
10177}
10178
0ecb72a5 10179uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
10180{
10181 int i;
10182 uint32_t fpscr;
10183
10184 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
10185 | (env->vfp.vec_len << 16)
10186 | (env->vfp.vec_stride << 20);
10187 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 10188 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
10189 fpscr |= vfp_exceptbits_from_host(i);
10190 return fpscr;
10191}
10192
0ecb72a5 10193uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
10194{
10195 return HELPER(vfp_get_fpscr)(env);
10196}
10197
4373f3ce
PB
10198/* Convert vfp exception flags to target form. */
10199static inline int vfp_exceptbits_to_host(int target_bits)
10200{
10201 int host_bits = 0;
10202
10203 if (target_bits & 1)
10204 host_bits |= float_flag_invalid;
10205 if (target_bits & 2)
10206 host_bits |= float_flag_divbyzero;
10207 if (target_bits & 4)
10208 host_bits |= float_flag_overflow;
10209 if (target_bits & 8)
10210 host_bits |= float_flag_underflow;
10211 if (target_bits & 0x10)
10212 host_bits |= float_flag_inexact;
cecd8504
PM
10213 if (target_bits & 0x80)
10214 host_bits |= float_flag_input_denormal;
4373f3ce
PB
10215 return host_bits;
10216}
10217
0ecb72a5 10218void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
10219{
10220 int i;
10221 uint32_t changed;
10222
10223 changed = env->vfp.xregs[ARM_VFP_FPSCR];
10224 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
10225 env->vfp.vec_len = (val >> 16) & 7;
10226 env->vfp.vec_stride = (val >> 20) & 3;
10227
10228 changed ^= val;
10229 if (changed & (3 << 22)) {
10230 i = (val >> 22) & 3;
10231 switch (i) {
4d3da0f3 10232 case FPROUNDING_TIEEVEN:
4373f3ce
PB
10233 i = float_round_nearest_even;
10234 break;
4d3da0f3 10235 case FPROUNDING_POSINF:
4373f3ce
PB
10236 i = float_round_up;
10237 break;
4d3da0f3 10238 case FPROUNDING_NEGINF:
4373f3ce
PB
10239 i = float_round_down;
10240 break;
4d3da0f3 10241 case FPROUNDING_ZERO:
4373f3ce
PB
10242 i = float_round_to_zero;
10243 break;
10244 }
10245 set_float_rounding_mode(i, &env->vfp.fp_status);
10246 }
cecd8504 10247 if (changed & (1 << 24)) {
fe76d976 10248 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
10249 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
10250 }
5c7908ed
PB
10251 if (changed & (1 << 25))
10252 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 10253
b12c390b 10254 i = vfp_exceptbits_to_host(val);
4373f3ce 10255 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 10256 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
10257}
10258
0ecb72a5 10259void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
10260{
10261 HELPER(vfp_set_fpscr)(env, val);
10262}
10263
4373f3ce
PB
10264#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
10265
10266#define VFP_BINOP(name) \
ae1857ec 10267float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 10268{ \
ae1857ec
PM
10269 float_status *fpst = fpstp; \
10270 return float32_ ## name(a, b, fpst); \
4373f3ce 10271} \
ae1857ec 10272float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 10273{ \
ae1857ec
PM
10274 float_status *fpst = fpstp; \
10275 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
10276}
10277VFP_BINOP(add)
10278VFP_BINOP(sub)
10279VFP_BINOP(mul)
10280VFP_BINOP(div)
f71a2ae5
PM
10281VFP_BINOP(min)
10282VFP_BINOP(max)
10283VFP_BINOP(minnum)
10284VFP_BINOP(maxnum)
4373f3ce
PB
10285#undef VFP_BINOP
10286
10287float32 VFP_HELPER(neg, s)(float32 a)
10288{
10289 return float32_chs(a);
10290}
10291
10292float64 VFP_HELPER(neg, d)(float64 a)
10293{
66230e0d 10294 return float64_chs(a);
4373f3ce
PB
10295}
10296
10297float32 VFP_HELPER(abs, s)(float32 a)
10298{
10299 return float32_abs(a);
10300}
10301
10302float64 VFP_HELPER(abs, d)(float64 a)
10303{
66230e0d 10304 return float64_abs(a);
4373f3ce
PB
10305}
10306
0ecb72a5 10307float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
10308{
10309 return float32_sqrt(a, &env->vfp.fp_status);
10310}
10311
0ecb72a5 10312float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
10313{
10314 return float64_sqrt(a, &env->vfp.fp_status);
10315}
10316
10317/* XXX: check quiet/signaling case */
10318#define DO_VFP_cmp(p, type) \
0ecb72a5 10319void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
10320{ \
10321 uint32_t flags; \
10322 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
10323 case 0: flags = 0x6; break; \
10324 case -1: flags = 0x8; break; \
10325 case 1: flags = 0x2; break; \
10326 default: case 2: flags = 0x3; break; \
10327 } \
10328 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10329 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10330} \
0ecb72a5 10331void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
10332{ \
10333 uint32_t flags; \
10334 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
10335 case 0: flags = 0x6; break; \
10336 case -1: flags = 0x8; break; \
10337 case 1: flags = 0x2; break; \
10338 default: case 2: flags = 0x3; break; \
10339 } \
10340 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
10341 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
10342}
10343DO_VFP_cmp(s, float32)
10344DO_VFP_cmp(d, float64)
10345#undef DO_VFP_cmp
10346
5500b06c 10347/* Integer to float and float to integer conversions */
4373f3ce 10348
5500b06c
PM
10349#define CONV_ITOF(name, fsz, sign) \
10350 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
10351{ \
10352 float_status *fpst = fpstp; \
85836979 10353 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
10354}
10355
5500b06c
PM
10356#define CONV_FTOI(name, fsz, sign, round) \
10357uint32_t HELPER(name)(float##fsz x, void *fpstp) \
10358{ \
10359 float_status *fpst = fpstp; \
10360 if (float##fsz##_is_any_nan(x)) { \
10361 float_raise(float_flag_invalid, fpst); \
10362 return 0; \
10363 } \
10364 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
10365}
10366
5500b06c
PM
10367#define FLOAT_CONVS(name, p, fsz, sign) \
10368CONV_ITOF(vfp_##name##to##p, fsz, sign) \
10369CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
10370CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 10371
5500b06c
PM
10372FLOAT_CONVS(si, s, 32, )
10373FLOAT_CONVS(si, d, 64, )
10374FLOAT_CONVS(ui, s, 32, u)
10375FLOAT_CONVS(ui, d, 64, u)
4373f3ce 10376
5500b06c
PM
10377#undef CONV_ITOF
10378#undef CONV_FTOI
10379#undef FLOAT_CONVS
4373f3ce
PB
10380
10381/* floating point conversion */
0ecb72a5 10382float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 10383{
2d627737
PM
10384 float64 r = float32_to_float64(x, &env->vfp.fp_status);
10385 /* ARM requires that S<->D conversion of any kind of NaN generates
10386 * a quiet NaN by forcing the most significant frac bit to 1.
10387 */
af39bc8c 10388 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
10389}
10390
0ecb72a5 10391float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 10392{
2d627737
PM
10393 float32 r = float64_to_float32(x, &env->vfp.fp_status);
10394 /* ARM requires that S<->D conversion of any kind of NaN generates
10395 * a quiet NaN by forcing the most significant frac bit to 1.
10396 */
af39bc8c 10397 return float32_maybe_silence_nan(r, &env->vfp.fp_status);
4373f3ce
PB
10398}
10399
10400/* VFP3 fixed point conversion. */
16d5b3ca 10401#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
10402float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
10403 void *fpstp) \
4373f3ce 10404{ \
5500b06c 10405 float_status *fpst = fpstp; \
622465e1 10406 float##fsz tmp; \
8ed697e8 10407 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 10408 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
10409}
10410
abe66f70
PM
10411/* Notice that we want only input-denormal exception flags from the
10412 * scalbn operation: the other possible flags (overflow+inexact if
10413 * we overflow to infinity, output-denormal) aren't correct for the
10414 * complete scale-and-convert operation.
10415 */
16d5b3ca
WN
10416#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
10417uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
10418 uint32_t shift, \
10419 void *fpstp) \
4373f3ce 10420{ \
5500b06c 10421 float_status *fpst = fpstp; \
abe66f70 10422 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
10423 float##fsz tmp; \
10424 if (float##fsz##_is_any_nan(x)) { \
5500b06c 10425 float_raise(float_flag_invalid, fpst); \
622465e1 10426 return 0; \
09d9487f 10427 } \
5500b06c 10428 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
10429 old_exc_flags |= get_float_exception_flags(fpst) \
10430 & float_flag_input_denormal; \
10431 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 10432 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
10433}
10434
16d5b3ca
WN
10435#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
10436VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
10437VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
10438VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
10439
10440#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
10441VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
10442VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 10443
8ed697e8
WN
10444VFP_CONV_FIX(sh, d, 64, 64, int16)
10445VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 10446VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
10447VFP_CONV_FIX(uh, d, 64, 64, uint16)
10448VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 10449VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
10450VFP_CONV_FIX(sh, s, 32, 32, int16)
10451VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 10452VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
10453VFP_CONV_FIX(uh, s, 32, 32, uint16)
10454VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 10455VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 10456#undef VFP_CONV_FIX
16d5b3ca
WN
10457#undef VFP_CONV_FIX_FLOAT
10458#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 10459
52a1f6a3
AG
10460/* Set the current fp rounding mode and return the old one.
10461 * The argument is a softfloat float_round_ value.
10462 */
10463uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
10464{
10465 float_status *fp_status = &env->vfp.fp_status;
10466
10467 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10468 set_float_rounding_mode(rmode, fp_status);
10469
10470 return prev_rmode;
10471}
10472
43630e58
WN
10473/* Set the current fp rounding mode in the standard fp status and return
10474 * the old one. This is for NEON instructions that need to change the
10475 * rounding mode but wish to use the standard FPSCR values for everything
10476 * else. Always set the rounding mode back to the correct value after
10477 * modifying it.
10478 * The argument is a softfloat float_round_ value.
10479 */
10480uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
10481{
10482 float_status *fp_status = &env->vfp.standard_fp_status;
10483
10484 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
10485 set_float_rounding_mode(rmode, fp_status);
10486
10487 return prev_rmode;
10488}
10489
60011498 10490/* Half precision conversions. */
0ecb72a5 10491static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 10492{
60011498 10493 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
10494 float32 r = float16_to_float32(make_float16(a), ieee, s);
10495 if (ieee) {
af39bc8c 10496 return float32_maybe_silence_nan(r, s);
fb91678d
PM
10497 }
10498 return r;
60011498
PB
10499}
10500
0ecb72a5 10501static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 10502{
60011498 10503 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
10504 float16 r = float32_to_float16(a, ieee, s);
10505 if (ieee) {
af39bc8c 10506 r = float16_maybe_silence_nan(r, s);
fb91678d
PM
10507 }
10508 return float16_val(r);
60011498
PB
10509}
10510
0ecb72a5 10511float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
10512{
10513 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
10514}
10515
0ecb72a5 10516uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
10517{
10518 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
10519}
10520
0ecb72a5 10521float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
10522{
10523 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
10524}
10525
0ecb72a5 10526uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
10527{
10528 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
10529}
10530
8900aad2
PM
10531float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
10532{
10533 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10534 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
10535 if (ieee) {
af39bc8c 10536 return float64_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
10537 }
10538 return r;
10539}
10540
10541uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
10542{
10543 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
10544 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
10545 if (ieee) {
af39bc8c 10546 r = float16_maybe_silence_nan(r, &env->vfp.fp_status);
8900aad2
PM
10547 }
10548 return float16_val(r);
10549}
10550
dda3ec49 10551#define float32_two make_float32(0x40000000)
6aae3df1
PM
10552#define float32_three make_float32(0x40400000)
10553#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 10554
0ecb72a5 10555float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 10556{
dda3ec49
PM
10557 float_status *s = &env->vfp.standard_fp_status;
10558 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
10559 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
10560 if (!(float32_is_zero(a) || float32_is_zero(b))) {
10561 float_raise(float_flag_input_denormal, s);
10562 }
dda3ec49
PM
10563 return float32_two;
10564 }
10565 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
10566}
10567
0ecb72a5 10568float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 10569{
71826966 10570 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
10571 float32 product;
10572 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
10573 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
10574 if (!(float32_is_zero(a) || float32_is_zero(b))) {
10575 float_raise(float_flag_input_denormal, s);
10576 }
6aae3df1 10577 return float32_one_point_five;
9ea62f57 10578 }
6aae3df1
PM
10579 product = float32_mul(a, b, s);
10580 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
10581}
10582
8f8e3aa4
PB
10583/* NEON helpers. */
10584
56bf4fe2
CL
10585/* Constants 256 and 512 are used in some helpers; we avoid relying on
10586 * int->float conversions at run-time. */
10587#define float64_256 make_float64(0x4070000000000000LL)
10588#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
10589#define float32_maxnorm make_float32(0x7f7fffff)
10590#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 10591
b6d4443a
AB
10592/* Reciprocal functions
10593 *
10594 * The algorithm that must be used to calculate the estimate
10595 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 10596 */
b6d4443a
AB
10597
10598static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 10599{
1146a817
PM
10600 /* These calculations mustn't set any fp exception flags,
10601 * so we use a local copy of the fp_status.
10602 */
b6d4443a 10603 float_status dummy_status = *real_fp_status;
1146a817 10604 float_status *s = &dummy_status;
fe0e4872
CL
10605 /* q = (int)(a * 512.0) */
10606 float64 q = float64_mul(float64_512, a, s);
10607 int64_t q_int = float64_to_int64_round_to_zero(q, s);
10608
10609 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
10610 q = int64_to_float64(q_int, s);
10611 q = float64_add(q, float64_half, s);
10612 q = float64_div(q, float64_512, s);
10613 q = float64_div(float64_one, q, s);
10614
10615 /* s = (int)(256.0 * r + 0.5) */
10616 q = float64_mul(q, float64_256, s);
10617 q = float64_add(q, float64_half, s);
10618 q_int = float64_to_int64_round_to_zero(q, s);
10619
10620 /* return (double)s / 256.0 */
10621 return float64_div(int64_to_float64(q_int, s), float64_256, s);
10622}
10623
b6d4443a
AB
10624/* Common wrapper to call recip_estimate */
10625static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 10626{
b6d4443a
AB
10627 uint64_t val64 = float64_val(num);
10628 uint64_t frac = extract64(val64, 0, 52);
10629 int64_t exp = extract64(val64, 52, 11);
10630 uint64_t sbit;
10631 float64 scaled, estimate;
fe0e4872 10632
b6d4443a
AB
10633 /* Generate the scaled number for the estimate function */
10634 if (exp == 0) {
10635 if (extract64(frac, 51, 1) == 0) {
10636 exp = -1;
10637 frac = extract64(frac, 0, 50) << 2;
10638 } else {
10639 frac = extract64(frac, 0, 51) << 1;
10640 }
10641 }
fe0e4872 10642
b6d4443a
AB
10643 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
10644 scaled = make_float64((0x3feULL << 52)
10645 | extract64(frac, 44, 8) << 44);
10646
10647 estimate = recip_estimate(scaled, fpst);
10648
10649 /* Build new result */
10650 val64 = float64_val(estimate);
10651 sbit = 0x8000000000000000ULL & val64;
10652 exp = off - exp;
10653 frac = extract64(val64, 0, 52);
10654
10655 if (exp == 0) {
10656 frac = 1ULL << 51 | extract64(frac, 1, 51);
10657 } else if (exp == -1) {
10658 frac = 1ULL << 50 | extract64(frac, 2, 50);
10659 exp = 0;
10660 }
10661
10662 return make_float64(sbit | (exp << 52) | frac);
10663}
10664
10665static bool round_to_inf(float_status *fpst, bool sign_bit)
10666{
10667 switch (fpst->float_rounding_mode) {
10668 case float_round_nearest_even: /* Round to Nearest */
10669 return true;
10670 case float_round_up: /* Round to +Inf */
10671 return !sign_bit;
10672 case float_round_down: /* Round to -Inf */
10673 return sign_bit;
10674 case float_round_to_zero: /* Round to Zero */
10675 return false;
10676 }
10677
10678 g_assert_not_reached();
10679}
10680
10681float32 HELPER(recpe_f32)(float32 input, void *fpstp)
10682{
10683 float_status *fpst = fpstp;
10684 float32 f32 = float32_squash_input_denormal(input, fpst);
10685 uint32_t f32_val = float32_val(f32);
10686 uint32_t f32_sbit = 0x80000000ULL & f32_val;
10687 int32_t f32_exp = extract32(f32_val, 23, 8);
10688 uint32_t f32_frac = extract32(f32_val, 0, 23);
10689 float64 f64, r64;
10690 uint64_t r64_val;
10691 int64_t r64_exp;
10692 uint64_t r64_frac;
10693
10694 if (float32_is_any_nan(f32)) {
10695 float32 nan = f32;
af39bc8c 10696 if (float32_is_signaling_nan(f32, fpst)) {
b6d4443a 10697 float_raise(float_flag_invalid, fpst);
af39bc8c 10698 nan = float32_maybe_silence_nan(f32, fpst);
fe0e4872 10699 }
b6d4443a 10700 if (fpst->default_nan_mode) {
af39bc8c 10701 nan = float32_default_nan(fpst);
43fe9bdb 10702 }
b6d4443a
AB
10703 return nan;
10704 } else if (float32_is_infinity(f32)) {
10705 return float32_set_sign(float32_zero, float32_is_neg(f32));
10706 } else if (float32_is_zero(f32)) {
10707 float_raise(float_flag_divbyzero, fpst);
10708 return float32_set_sign(float32_infinity, float32_is_neg(f32));
10709 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
10710 /* Abs(value) < 2.0^-128 */
10711 float_raise(float_flag_overflow | float_flag_inexact, fpst);
10712 if (round_to_inf(fpst, f32_sbit)) {
10713 return float32_set_sign(float32_infinity, float32_is_neg(f32));
10714 } else {
10715 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
10716 }
10717 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
10718 float_raise(float_flag_underflow, fpst);
10719 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
10720 }
10721
fe0e4872 10722
b6d4443a
AB
10723 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
10724 r64 = call_recip_estimate(f64, 253, fpst);
10725 r64_val = float64_val(r64);
10726 r64_exp = extract64(r64_val, 52, 11);
10727 r64_frac = extract64(r64_val, 0, 52);
10728
10729 /* result = sign : result_exp<7:0> : fraction<51:29>; */
10730 return make_float32(f32_sbit |
10731 (r64_exp & 0xff) << 23 |
10732 extract64(r64_frac, 29, 24));
10733}
10734
10735float64 HELPER(recpe_f64)(float64 input, void *fpstp)
10736{
10737 float_status *fpst = fpstp;
10738 float64 f64 = float64_squash_input_denormal(input, fpst);
10739 uint64_t f64_val = float64_val(f64);
10740 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
10741 int64_t f64_exp = extract64(f64_val, 52, 11);
10742 float64 r64;
10743 uint64_t r64_val;
10744 int64_t r64_exp;
10745 uint64_t r64_frac;
10746
10747 /* Deal with any special cases */
10748 if (float64_is_any_nan(f64)) {
10749 float64 nan = f64;
af39bc8c 10750 if (float64_is_signaling_nan(f64, fpst)) {
b6d4443a 10751 float_raise(float_flag_invalid, fpst);
af39bc8c 10752 nan = float64_maybe_silence_nan(f64, fpst);
b6d4443a
AB
10753 }
10754 if (fpst->default_nan_mode) {
af39bc8c 10755 nan = float64_default_nan(fpst);
b6d4443a
AB
10756 }
10757 return nan;
10758 } else if (float64_is_infinity(f64)) {
10759 return float64_set_sign(float64_zero, float64_is_neg(f64));
10760 } else if (float64_is_zero(f64)) {
10761 float_raise(float_flag_divbyzero, fpst);
10762 return float64_set_sign(float64_infinity, float64_is_neg(f64));
10763 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
10764 /* Abs(value) < 2.0^-1024 */
10765 float_raise(float_flag_overflow | float_flag_inexact, fpst);
10766 if (round_to_inf(fpst, f64_sbit)) {
10767 return float64_set_sign(float64_infinity, float64_is_neg(f64));
10768 } else {
10769 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
10770 }
fc1792e9 10771 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
10772 float_raise(float_flag_underflow, fpst);
10773 return float64_set_sign(float64_zero, float64_is_neg(f64));
10774 }
fe0e4872 10775
b6d4443a
AB
10776 r64 = call_recip_estimate(f64, 2045, fpst);
10777 r64_val = float64_val(r64);
10778 r64_exp = extract64(r64_val, 52, 11);
10779 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 10780
b6d4443a
AB
10781 /* result = sign : result_exp<10:0> : fraction<51:0> */
10782 return make_float64(f64_sbit |
10783 ((r64_exp & 0x7ff) << 52) |
10784 r64_frac);
4373f3ce
PB
10785}
10786
e07be5d2
CL
10787/* The algorithm that must be used to calculate the estimate
10788 * is specified by the ARM ARM.
10789 */
c2fb418e 10790static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 10791{
1146a817
PM
10792 /* These calculations mustn't set any fp exception flags,
10793 * so we use a local copy of the fp_status.
10794 */
c2fb418e 10795 float_status dummy_status = *real_fp_status;
1146a817 10796 float_status *s = &dummy_status;
e07be5d2
CL
10797 float64 q;
10798 int64_t q_int;
10799
10800 if (float64_lt(a, float64_half, s)) {
10801 /* range 0.25 <= a < 0.5 */
10802
10803 /* a in units of 1/512 rounded down */
10804 /* q0 = (int)(a * 512.0); */
10805 q = float64_mul(float64_512, a, s);
10806 q_int = float64_to_int64_round_to_zero(q, s);
10807
10808 /* reciprocal root r */
10809 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
10810 q = int64_to_float64(q_int, s);
10811 q = float64_add(q, float64_half, s);
10812 q = float64_div(q, float64_512, s);
10813 q = float64_sqrt(q, s);
10814 q = float64_div(float64_one, q, s);
10815 } else {
10816 /* range 0.5 <= a < 1.0 */
10817
10818 /* a in units of 1/256 rounded down */
10819 /* q1 = (int)(a * 256.0); */
10820 q = float64_mul(float64_256, a, s);
10821 int64_t q_int = float64_to_int64_round_to_zero(q, s);
10822
10823 /* reciprocal root r */
10824 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
10825 q = int64_to_float64(q_int, s);
10826 q = float64_add(q, float64_half, s);
10827 q = float64_div(q, float64_256, s);
10828 q = float64_sqrt(q, s);
10829 q = float64_div(float64_one, q, s);
10830 }
10831 /* r in units of 1/256 rounded to nearest */
10832 /* s = (int)(256.0 * r + 0.5); */
10833
10834 q = float64_mul(q, float64_256,s );
10835 q = float64_add(q, float64_half, s);
10836 q_int = float64_to_int64_round_to_zero(q, s);
10837
10838 /* return (double)s / 256.0;*/
10839 return float64_div(int64_to_float64(q_int, s), float64_256, s);
10840}
10841
c2fb418e 10842float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 10843{
c2fb418e
AB
10844 float_status *s = fpstp;
10845 float32 f32 = float32_squash_input_denormal(input, s);
10846 uint32_t val = float32_val(f32);
10847 uint32_t f32_sbit = 0x80000000 & val;
10848 int32_t f32_exp = extract32(val, 23, 8);
10849 uint32_t f32_frac = extract32(val, 0, 23);
10850 uint64_t f64_frac;
10851 uint64_t val64;
e07be5d2
CL
10852 int result_exp;
10853 float64 f64;
e07be5d2 10854
c2fb418e
AB
10855 if (float32_is_any_nan(f32)) {
10856 float32 nan = f32;
af39bc8c 10857 if (float32_is_signaling_nan(f32, s)) {
e07be5d2 10858 float_raise(float_flag_invalid, s);
af39bc8c 10859 nan = float32_maybe_silence_nan(f32, s);
e07be5d2 10860 }
c2fb418e 10861 if (s->default_nan_mode) {
af39bc8c 10862 nan = float32_default_nan(s);
43fe9bdb 10863 }
c2fb418e
AB
10864 return nan;
10865 } else if (float32_is_zero(f32)) {
e07be5d2 10866 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
10867 return float32_set_sign(float32_infinity, float32_is_neg(f32));
10868 } else if (float32_is_neg(f32)) {
e07be5d2 10869 float_raise(float_flag_invalid, s);
af39bc8c 10870 return float32_default_nan(s);
c2fb418e 10871 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
10872 return float32_zero;
10873 }
10874
c2fb418e 10875 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 10876 * preserving the parity of the exponent. */
c2fb418e
AB
10877
10878 f64_frac = ((uint64_t) f32_frac) << 29;
10879 if (f32_exp == 0) {
10880 while (extract64(f64_frac, 51, 1) == 0) {
10881 f64_frac = f64_frac << 1;
10882 f32_exp = f32_exp-1;
10883 }
10884 f64_frac = extract64(f64_frac, 0, 51) << 1;
10885 }
10886
10887 if (extract64(f32_exp, 0, 1) == 0) {
10888 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 10889 | (0x3feULL << 52)
c2fb418e 10890 | f64_frac);
e07be5d2 10891 } else {
c2fb418e 10892 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 10893 | (0x3fdULL << 52)
c2fb418e 10894 | f64_frac);
e07be5d2
CL
10895 }
10896
c2fb418e 10897 result_exp = (380 - f32_exp) / 2;
e07be5d2 10898
c2fb418e 10899 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
10900
10901 val64 = float64_val(f64);
10902
26cc6abf 10903 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
10904 | ((val64 >> 29) & 0x7fffff);
10905 return make_float32(val);
4373f3ce
PB
10906}
10907
c2fb418e
AB
10908float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
10909{
10910 float_status *s = fpstp;
10911 float64 f64 = float64_squash_input_denormal(input, s);
10912 uint64_t val = float64_val(f64);
10913 uint64_t f64_sbit = 0x8000000000000000ULL & val;
10914 int64_t f64_exp = extract64(val, 52, 11);
10915 uint64_t f64_frac = extract64(val, 0, 52);
10916 int64_t result_exp;
10917 uint64_t result_frac;
10918
10919 if (float64_is_any_nan(f64)) {
10920 float64 nan = f64;
af39bc8c 10921 if (float64_is_signaling_nan(f64, s)) {
c2fb418e 10922 float_raise(float_flag_invalid, s);
af39bc8c 10923 nan = float64_maybe_silence_nan(f64, s);
c2fb418e
AB
10924 }
10925 if (s->default_nan_mode) {
af39bc8c 10926 nan = float64_default_nan(s);
c2fb418e
AB
10927 }
10928 return nan;
10929 } else if (float64_is_zero(f64)) {
10930 float_raise(float_flag_divbyzero, s);
10931 return float64_set_sign(float64_infinity, float64_is_neg(f64));
10932 } else if (float64_is_neg(f64)) {
10933 float_raise(float_flag_invalid, s);
af39bc8c 10934 return float64_default_nan(s);
c2fb418e
AB
10935 } else if (float64_is_infinity(f64)) {
10936 return float64_zero;
10937 }
10938
10939 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
10940 * preserving the parity of the exponent. */
10941
10942 if (f64_exp == 0) {
10943 while (extract64(f64_frac, 51, 1) == 0) {
10944 f64_frac = f64_frac << 1;
10945 f64_exp = f64_exp - 1;
10946 }
10947 f64_frac = extract64(f64_frac, 0, 51) << 1;
10948 }
10949
10950 if (extract64(f64_exp, 0, 1) == 0) {
10951 f64 = make_float64(f64_sbit
10952 | (0x3feULL << 52)
10953 | f64_frac);
10954 } else {
10955 f64 = make_float64(f64_sbit
10956 | (0x3fdULL << 52)
10957 | f64_frac);
10958 }
10959
10960 result_exp = (3068 - f64_exp) / 2;
10961
10962 f64 = recip_sqrt_estimate(f64, s);
10963
10964 result_frac = extract64(float64_val(f64), 0, 52);
10965
10966 return make_float64(f64_sbit |
10967 ((result_exp & 0x7ff) << 52) |
10968 result_frac);
10969}
10970
b6d4443a 10971uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 10972{
b6d4443a 10973 float_status *s = fpstp;
fe0e4872
CL
10974 float64 f64;
10975
10976 if ((a & 0x80000000) == 0) {
10977 return 0xffffffff;
10978 }
10979
10980 f64 = make_float64((0x3feULL << 52)
10981 | ((int64_t)(a & 0x7fffffff) << 21));
10982
b6d4443a 10983 f64 = recip_estimate(f64, s);
fe0e4872
CL
10984
10985 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
10986}
10987
c2fb418e 10988uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 10989{
c2fb418e 10990 float_status *fpst = fpstp;
e07be5d2
CL
10991 float64 f64;
10992
10993 if ((a & 0xc0000000) == 0) {
10994 return 0xffffffff;
10995 }
10996
10997 if (a & 0x80000000) {
10998 f64 = make_float64((0x3feULL << 52)
10999 | ((uint64_t)(a & 0x7fffffff) << 21));
11000 } else { /* bits 31-30 == '01' */
11001 f64 = make_float64((0x3fdULL << 52)
11002 | ((uint64_t)(a & 0x3fffffff) << 22));
11003 }
11004
c2fb418e 11005 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
11006
11007 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 11008}
fe1479c3 11009
da97f52c
PM
11010/* VFPv4 fused multiply-accumulate */
11011float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
11012{
11013 float_status *fpst = fpstp;
11014 return float32_muladd(a, b, c, 0, fpst);
11015}
11016
11017float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
11018{
11019 float_status *fpst = fpstp;
11020 return float64_muladd(a, b, c, 0, fpst);
11021}
d9b0848d
PM
11022
11023/* ARMv8 round to integral */
11024float32 HELPER(rints_exact)(float32 x, void *fp_status)
11025{
11026 return float32_round_to_int(x, fp_status);
11027}
11028
11029float64 HELPER(rintd_exact)(float64 x, void *fp_status)
11030{
11031 return float64_round_to_int(x, fp_status);
11032}
11033
11034float32 HELPER(rints)(float32 x, void *fp_status)
11035{
11036 int old_flags = get_float_exception_flags(fp_status), new_flags;
11037 float32 ret;
11038
11039 ret = float32_round_to_int(x, fp_status);
11040
11041 /* Suppress any inexact exceptions the conversion produced */
11042 if (!(old_flags & float_flag_inexact)) {
11043 new_flags = get_float_exception_flags(fp_status);
11044 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11045 }
11046
11047 return ret;
11048}
11049
11050float64 HELPER(rintd)(float64 x, void *fp_status)
11051{
11052 int old_flags = get_float_exception_flags(fp_status), new_flags;
11053 float64 ret;
11054
11055 ret = float64_round_to_int(x, fp_status);
11056
11057 new_flags = get_float_exception_flags(fp_status);
11058
11059 /* Suppress any inexact exceptions the conversion produced */
11060 if (!(old_flags & float_flag_inexact)) {
11061 new_flags = get_float_exception_flags(fp_status);
11062 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
11063 }
11064
11065 return ret;
11066}
9972da66
WN
11067
11068/* Convert ARM rounding mode to softfloat */
11069int arm_rmode_to_sf(int rmode)
11070{
11071 switch (rmode) {
11072 case FPROUNDING_TIEAWAY:
11073 rmode = float_round_ties_away;
11074 break;
11075 case FPROUNDING_ODD:
11076 /* FIXME: add support for TIEAWAY and ODD */
11077 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
11078 rmode);
11079 case FPROUNDING_TIEEVEN:
11080 default:
11081 rmode = float_round_nearest_even;
11082 break;
11083 case FPROUNDING_POSINF:
11084 rmode = float_round_up;
11085 break;
11086 case FPROUNDING_NEGINF:
11087 rmode = float_round_down;
11088 break;
11089 case FPROUNDING_ZERO:
11090 rmode = float_round_to_zero;
11091 break;
11092 }
11093 return rmode;
11094}
eb0ecd5a 11095
aa633469
PM
11096/* CRC helpers.
11097 * The upper bytes of val (above the number specified by 'bytes') must have
11098 * been zeroed out by the caller.
11099 */
eb0ecd5a
WN
11100uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11101{
11102 uint8_t buf[4];
11103
aa633469 11104 stl_le_p(buf, val);
eb0ecd5a
WN
11105
11106 /* zlib crc32 converts the accumulator and output to one's complement. */
11107 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11108}
11109
11110uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11111{
11112 uint8_t buf[4];
11113
aa633469 11114 stl_le_p(buf, val);
eb0ecd5a
WN
11115
11116 /* Linux crc32c converts the output to one's complement. */
11117 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11118}
This page took 2.848933 seconds and 4 git commands to generate.