]> git.proxmox.com Git - mirror_qemu.git/blame - target-arm/helper.c
linux-user: arm: Handle (ignore) EXCP_YIELD in ARM cpu_loop()
[mirror_qemu.git] / target-arm / helper.c
CommitLineData
74c21bd0 1#include "qemu/osdep.h"
b5ff1b31 2#include "cpu.h"
ccd38087 3#include "internals.h"
022c62cb 4#include "exec/gdbstub.h"
2ef6175a 5#include "exec/helper-proto.h"
1de7afc9 6#include "qemu/host-utils.h"
78027bb6 7#include "sysemu/arch_init.h"
9c17d615 8#include "sysemu/sysemu.h"
1de7afc9 9#include "qemu/bitops.h"
eb0ecd5a 10#include "qemu/crc32c.h"
f08b6170 11#include "exec/cpu_ldst.h"
1d854765 12#include "arm_ldst.h"
eb0ecd5a 13#include <zlib.h> /* For crc32 */
cfe67cef 14#include "exec/semihost.h"
f3a9b694 15#include "sysemu/kvm.h"
0b03bdfc 16
352c98e5
LV
17#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
18
4a501606 19#ifndef CONFIG_USER_ONLY
af51f566
EI
20static bool get_phys_addr(CPUARMState *env, target_ulong address,
21 int access_type, ARMMMUIdx mmu_idx,
22 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
23 target_ulong *page_size, uint32_t *fsr,
24 ARMMMUFaultInfo *fi);
7c2cb42b 25
37785977
EI
26static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
27 int access_type, ARMMMUIdx mmu_idx,
28 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
29 target_ulong *page_size_ptr, uint32_t *fsr,
30 ARMMMUFaultInfo *fi);
31
7c2cb42b
AF
32/* Definitions for the PMCCNTR and PMCR registers */
33#define PMCRD 0x8
34#define PMCRC 0x4
35#define PMCRE 0x1
4a501606
PM
36#endif
37
0ecb72a5 38static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
39{
40 int nregs;
41
42 /* VFP data registers are always little-endian. */
43 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
44 if (reg < nregs) {
45 stfq_le_p(buf, env->vfp.regs[reg]);
46 return 8;
47 }
48 if (arm_feature(env, ARM_FEATURE_NEON)) {
49 /* Aliases for Q regs. */
50 nregs += 16;
51 if (reg < nregs) {
52 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
53 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
54 return 16;
55 }
56 }
57 switch (reg - nregs) {
58 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
59 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
60 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
61 }
62 return 0;
63}
64
0ecb72a5 65static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
66{
67 int nregs;
68
69 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
70 if (reg < nregs) {
71 env->vfp.regs[reg] = ldfq_le_p(buf);
72 return 8;
73 }
74 if (arm_feature(env, ARM_FEATURE_NEON)) {
75 nregs += 16;
76 if (reg < nregs) {
77 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
78 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
79 return 16;
80 }
81 }
82 switch (reg - nregs) {
83 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
84 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 85 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
86 }
87 return 0;
88}
89
6a669427
PM
90static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
91{
92 switch (reg) {
93 case 0 ... 31:
94 /* 128 bit FP register */
95 stfq_le_p(buf, env->vfp.regs[reg * 2]);
96 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
97 return 16;
98 case 32:
99 /* FPSR */
100 stl_p(buf, vfp_get_fpsr(env));
101 return 4;
102 case 33:
103 /* FPCR */
104 stl_p(buf, vfp_get_fpcr(env));
105 return 4;
106 default:
107 return 0;
108 }
109}
110
111static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
112{
113 switch (reg) {
114 case 0 ... 31:
115 /* 128 bit FP register */
116 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
117 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
118 return 16;
119 case 32:
120 /* FPSR */
121 vfp_set_fpsr(env, ldl_p(buf));
122 return 4;
123 case 33:
124 /* FPCR */
125 vfp_set_fpcr(env, ldl_p(buf));
126 return 4;
127 default:
128 return 0;
129 }
130}
131
c4241c7d 132static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 133{
375421cc 134 assert(ri->fieldoffset);
67ed771d 135 if (cpreg_field_is_64bit(ri)) {
c4241c7d 136 return CPREG_FIELD64(env, ri);
22d9e1a9 137 } else {
c4241c7d 138 return CPREG_FIELD32(env, ri);
22d9e1a9 139 }
d4e6df63
PM
140}
141
c4241c7d
PM
142static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
143 uint64_t value)
d4e6df63 144{
375421cc 145 assert(ri->fieldoffset);
67ed771d 146 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
147 CPREG_FIELD64(env, ri) = value;
148 } else {
149 CPREG_FIELD32(env, ri) = value;
150 }
d4e6df63
PM
151}
152
11f136ee
FA
153static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
154{
155 return (char *)env + ri->fieldoffset;
156}
157
49a66191 158uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 159{
59a1c327 160 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 161 if (ri->type & ARM_CP_CONST) {
59a1c327 162 return ri->resetvalue;
721fae12 163 } else if (ri->raw_readfn) {
59a1c327 164 return ri->raw_readfn(env, ri);
721fae12 165 } else if (ri->readfn) {
59a1c327 166 return ri->readfn(env, ri);
721fae12 167 } else {
59a1c327 168 return raw_read(env, ri);
721fae12 169 }
721fae12
PM
170}
171
59a1c327 172static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 173 uint64_t v)
721fae12
PM
174{
175 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
176 * Note that constant registers are treated as write-ignored; the
177 * caller should check for success by whether a readback gives the
178 * value written.
179 */
180 if (ri->type & ARM_CP_CONST) {
59a1c327 181 return;
721fae12 182 } else if (ri->raw_writefn) {
c4241c7d 183 ri->raw_writefn(env, ri, v);
721fae12 184 } else if (ri->writefn) {
c4241c7d 185 ri->writefn(env, ri, v);
721fae12 186 } else {
afb2530f 187 raw_write(env, ri, v);
721fae12 188 }
721fae12
PM
189}
190
375421cc
PM
191static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
192{
193 /* Return true if the regdef would cause an assertion if you called
194 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
195 * program bug for it not to have the NO_RAW flag).
196 * NB that returning false here doesn't necessarily mean that calling
197 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
198 * read/write access functions which are safe for raw use" from "has
199 * read/write access functions which have side effects but has forgotten
200 * to provide raw access functions".
201 * The tests here line up with the conditions in read/write_raw_cp_reg()
202 * and assertions in raw_read()/raw_write().
203 */
204 if ((ri->type & ARM_CP_CONST) ||
205 ri->fieldoffset ||
206 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
207 return false;
208 }
209 return true;
210}
211
721fae12
PM
212bool write_cpustate_to_list(ARMCPU *cpu)
213{
214 /* Write the coprocessor state from cpu->env to the (index,value) list. */
215 int i;
216 bool ok = true;
217
218 for (i = 0; i < cpu->cpreg_array_len; i++) {
219 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
220 const ARMCPRegInfo *ri;
59a1c327 221
60322b39 222 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
223 if (!ri) {
224 ok = false;
225 continue;
226 }
7a0e58fa 227 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
228 continue;
229 }
59a1c327 230 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
231 }
232 return ok;
233}
234
235bool write_list_to_cpustate(ARMCPU *cpu)
236{
237 int i;
238 bool ok = true;
239
240 for (i = 0; i < cpu->cpreg_array_len; i++) {
241 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
242 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
243 const ARMCPRegInfo *ri;
244
60322b39 245 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
246 if (!ri) {
247 ok = false;
248 continue;
249 }
7a0e58fa 250 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
251 continue;
252 }
253 /* Write value and confirm it reads back as written
254 * (to catch read-only registers and partially read-only
255 * registers where the incoming migration value doesn't match)
256 */
59a1c327
PM
257 write_raw_cp_reg(&cpu->env, ri, v);
258 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
259 ok = false;
260 }
261 }
262 return ok;
263}
264
265static void add_cpreg_to_list(gpointer key, gpointer opaque)
266{
267 ARMCPU *cpu = opaque;
268 uint64_t regidx;
269 const ARMCPRegInfo *ri;
270
271 regidx = *(uint32_t *)key;
60322b39 272 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 273
7a0e58fa 274 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
275 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
276 /* The value array need not be initialized at this point */
277 cpu->cpreg_array_len++;
278 }
279}
280
281static void count_cpreg(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_array_len++;
292 }
293}
294
295static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
296{
cbf239b7
AR
297 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
298 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 299
cbf239b7
AR
300 if (aidx > bidx) {
301 return 1;
302 }
303 if (aidx < bidx) {
304 return -1;
305 }
306 return 0;
721fae12
PM
307}
308
309void init_cpreg_list(ARMCPU *cpu)
310{
311 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
312 * Note that we require cpreg_tuples[] to be sorted by key ID.
313 */
57b6d95e 314 GList *keys;
721fae12
PM
315 int arraylen;
316
57b6d95e 317 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
318 keys = g_list_sort(keys, cpreg_key_compare);
319
320 cpu->cpreg_array_len = 0;
321
322 g_list_foreach(keys, count_cpreg, cpu);
323
324 arraylen = cpu->cpreg_array_len;
325 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
326 cpu->cpreg_values = g_new(uint64_t, arraylen);
327 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
328 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
329 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
330 cpu->cpreg_array_len = 0;
331
332 g_list_foreach(keys, add_cpreg_to_list, cpu);
333
334 assert(cpu->cpreg_array_len == arraylen);
335
336 g_list_free(keys);
337}
338
68e9c2fe
EI
339/*
340 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
341 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
342 *
343 * access_el3_aa32ns: Used to check AArch32 register views.
344 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
345 */
346static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
347 const ARMCPRegInfo *ri,
348 bool isread)
68e9c2fe
EI
349{
350 bool secure = arm_is_secure_below_el3(env);
351
352 assert(!arm_el_is_aa64(env, 3));
353 if (secure) {
354 return CP_ACCESS_TRAP_UNCATEGORIZED;
355 }
356 return CP_ACCESS_OK;
357}
358
359static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
3f208fd7
PM
360 const ARMCPRegInfo *ri,
361 bool isread)
68e9c2fe
EI
362{
363 if (!arm_el_is_aa64(env, 3)) {
3f208fd7 364 return access_el3_aa32ns(env, ri, isread);
68e9c2fe
EI
365 }
366 return CP_ACCESS_OK;
367}
368
5513c3ab
PM
369/* Some secure-only AArch32 registers trap to EL3 if used from
370 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
371 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
372 * We assume that the .access field is set to PL1_RW.
373 */
374static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
375 const ARMCPRegInfo *ri,
376 bool isread)
5513c3ab
PM
377{
378 if (arm_current_el(env) == 3) {
379 return CP_ACCESS_OK;
380 }
381 if (arm_is_secure_below_el3(env)) {
382 return CP_ACCESS_TRAP_EL3;
383 }
384 /* This will be EL1 NS and EL2 NS, which just UNDEF */
385 return CP_ACCESS_TRAP_UNCATEGORIZED;
386}
387
187f678d
PM
388/* Check for traps to "powerdown debug" registers, which are controlled
389 * by MDCR.TDOSA
390 */
391static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
392 bool isread)
393{
394 int el = arm_current_el(env);
395
396 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
397 && !arm_is_secure_below_el3(env)) {
398 return CP_ACCESS_TRAP_EL2;
399 }
400 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
401 return CP_ACCESS_TRAP_EL3;
402 }
403 return CP_ACCESS_OK;
404}
405
91b0a238
PM
406/* Check for traps to "debug ROM" registers, which are controlled
407 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
408 */
409static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
410 bool isread)
411{
412 int el = arm_current_el(env);
413
414 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
415 && !arm_is_secure_below_el3(env)) {
416 return CP_ACCESS_TRAP_EL2;
417 }
418 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
419 return CP_ACCESS_TRAP_EL3;
420 }
421 return CP_ACCESS_OK;
422}
423
d6c8cf81
PM
424/* Check for traps to general debug registers, which are controlled
425 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
426 */
427static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
428 bool isread)
429{
430 int el = arm_current_el(env);
431
432 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
433 && !arm_is_secure_below_el3(env)) {
434 return CP_ACCESS_TRAP_EL2;
435 }
436 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
437 return CP_ACCESS_TRAP_EL3;
438 }
439 return CP_ACCESS_OK;
440}
441
1fce1ba9
PM
442/* Check for traps to performance monitor registers, which are controlled
443 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
444 */
445static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
446 bool isread)
447{
448 int el = arm_current_el(env);
449
450 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
451 && !arm_is_secure_below_el3(env)) {
452 return CP_ACCESS_TRAP_EL2;
453 }
454 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
455 return CP_ACCESS_TRAP_EL3;
456 }
457 return CP_ACCESS_OK;
458}
459
c4241c7d 460static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 461{
00c8cb0a
AF
462 ARMCPU *cpu = arm_env_get_cpu(env);
463
8d5c773e 464 raw_write(env, ri, value);
00c8cb0a 465 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
466}
467
c4241c7d 468static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 469{
00c8cb0a
AF
470 ARMCPU *cpu = arm_env_get_cpu(env);
471
8d5c773e 472 if (raw_read(env, ri) != value) {
08de207b
PM
473 /* Unlike real hardware the qemu TLB uses virtual addresses,
474 * not modified virtual addresses, so this causes a TLB flush.
475 */
00c8cb0a 476 tlb_flush(CPU(cpu), 1);
8d5c773e 477 raw_write(env, ri, value);
08de207b 478 }
08de207b 479}
c4241c7d
PM
480
481static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
482 uint64_t value)
08de207b 483{
00c8cb0a
AF
484 ARMCPU *cpu = arm_env_get_cpu(env);
485
8d5c773e 486 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
014406b5 487 && !extended_addresses_enabled(env)) {
08de207b
PM
488 /* For VMSA (when not using the LPAE long descriptor page table
489 * format) this register includes the ASID, so do a TLB flush.
490 * For PMSA it is purely a process ID and no action is needed.
491 */
00c8cb0a 492 tlb_flush(CPU(cpu), 1);
08de207b 493 }
8d5c773e 494 raw_write(env, ri, value);
08de207b
PM
495}
496
c4241c7d
PM
497static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
498 uint64_t value)
d929823f
PM
499{
500 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
501 ARMCPU *cpu = arm_env_get_cpu(env);
502
503 tlb_flush(CPU(cpu), 1);
d929823f
PM
504}
505
c4241c7d
PM
506static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
507 uint64_t value)
d929823f
PM
508{
509 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
510 ARMCPU *cpu = arm_env_get_cpu(env);
511
512 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
513}
514
c4241c7d
PM
515static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
516 uint64_t value)
d929823f
PM
517{
518 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
519 ARMCPU *cpu = arm_env_get_cpu(env);
520
521 tlb_flush(CPU(cpu), value == 0);
d929823f
PM
522}
523
c4241c7d
PM
524static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
525 uint64_t value)
d929823f
PM
526{
527 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
528 ARMCPU *cpu = arm_env_get_cpu(env);
529
530 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
531}
532
fa439fc5
PM
533/* IS variants of TLB operations must affect all cores */
534static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
535 uint64_t value)
536{
537 CPUState *other_cs;
538
539 CPU_FOREACH(other_cs) {
540 tlb_flush(other_cs, 1);
541 }
542}
543
544static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
545 uint64_t value)
546{
547 CPUState *other_cs;
548
549 CPU_FOREACH(other_cs) {
550 tlb_flush(other_cs, value == 0);
551 }
552}
553
554static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
555 uint64_t value)
556{
557 CPUState *other_cs;
558
559 CPU_FOREACH(other_cs) {
560 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
561 }
562}
563
564static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566{
567 CPUState *other_cs;
568
569 CPU_FOREACH(other_cs) {
570 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
571 }
572}
573
e9aa6c21 574static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
575 /* Define the secure and non-secure FCSE identifier CP registers
576 * separately because there is no secure bank in V8 (no _EL3). This allows
577 * the secure register to be properly reset and migrated. There is also no
578 * v8 EL1 version of the register so the non-secure instance stands alone.
579 */
580 { .name = "FCSEIDR(NS)",
581 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
582 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
583 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
584 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
585 { .name = "FCSEIDR(S)",
586 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
587 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
588 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 589 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
590 /* Define the secure and non-secure context identifier CP registers
591 * separately because there is no secure bank in V8 (no _EL3). This allows
592 * the secure register to be properly reset and migrated. In the
593 * non-secure case, the 32-bit register will have reset and migration
594 * disabled during registration as it is handled by the 64-bit instance.
595 */
596 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 597 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
598 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
599 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
600 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
601 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
602 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
603 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
604 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 605 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
606 REGINFO_SENTINEL
607};
608
609static const ARMCPRegInfo not_v8_cp_reginfo[] = {
610 /* NB: Some of these registers exist in v8 but with more precise
611 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
612 */
613 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
614 { .name = "DACR",
615 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
616 .access = PL1_RW, .resetvalue = 0,
617 .writefn = dacr_write, .raw_writefn = raw_write,
618 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
619 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
620 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
621 * For v6 and v5, these mappings are overly broad.
4fdd17dd 622 */
a903c449
EI
623 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
624 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
625 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
626 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
627 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
628 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
629 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 630 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
631 /* Cache maintenance ops; some of this space may be overridden later. */
632 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
633 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
634 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
635 REGINFO_SENTINEL
636};
637
7d57f408
PM
638static const ARMCPRegInfo not_v6_cp_reginfo[] = {
639 /* Not all pre-v6 cores implemented this WFI, so this is slightly
640 * over-broad.
641 */
642 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
643 .access = PL1_W, .type = ARM_CP_WFI },
644 REGINFO_SENTINEL
645};
646
647static const ARMCPRegInfo not_v7_cp_reginfo[] = {
648 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
649 * is UNPREDICTABLE; we choose to NOP as most implementations do).
650 */
651 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
652 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
653 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
654 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
655 * OMAPCP will override this space.
656 */
657 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
658 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
659 .resetvalue = 0 },
660 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
661 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
662 .resetvalue = 0 },
776d4e5c
PM
663 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
664 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 665 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 666 .resetvalue = 0 },
50300698
PM
667 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
668 * implementing it as RAZ means the "debug architecture version" bits
669 * will read as a reserved value, which should cause Linux to not try
670 * to use the debug hardware.
671 */
672 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
673 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
674 /* MMU TLB control. Note that the wildcarding means we cover not just
675 * the unified TLB ops but also the dside/iside/inner-shareable variants.
676 */
677 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
678 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 679 .type = ARM_CP_NO_RAW },
995939a6
PM
680 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
681 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 682 .type = ARM_CP_NO_RAW },
995939a6
PM
683 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
684 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 685 .type = ARM_CP_NO_RAW },
995939a6
PM
686 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
687 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 688 .type = ARM_CP_NO_RAW },
a903c449
EI
689 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
690 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
691 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
692 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
693 REGINFO_SENTINEL
694};
695
c4241c7d
PM
696static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
697 uint64_t value)
2771db27 698{
f0aff255
FA
699 uint32_t mask = 0;
700
701 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
702 if (!arm_feature(env, ARM_FEATURE_V8)) {
703 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
704 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
705 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
706 */
707 if (arm_feature(env, ARM_FEATURE_VFP)) {
708 /* VFP coprocessor: cp10 & cp11 [23:20] */
709 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
710
711 if (!arm_feature(env, ARM_FEATURE_NEON)) {
712 /* ASEDIS [31] bit is RAO/WI */
713 value |= (1 << 31);
714 }
715
716 /* VFPv3 and upwards with NEON implement 32 double precision
717 * registers (D0-D31).
718 */
719 if (!arm_feature(env, ARM_FEATURE_NEON) ||
720 !arm_feature(env, ARM_FEATURE_VFP3)) {
721 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
722 value |= (1 << 30);
723 }
724 }
725 value &= mask;
2771db27 726 }
7ebd5f2e 727 env->cp15.cpacr_el1 = value;
2771db27
PM
728}
729
3f208fd7
PM
730static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
731 bool isread)
c6f19164
GB
732{
733 if (arm_feature(env, ARM_FEATURE_V8)) {
734 /* Check if CPACR accesses are to be trapped to EL2 */
735 if (arm_current_el(env) == 1 &&
736 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
737 return CP_ACCESS_TRAP_EL2;
738 /* Check if CPACR accesses are to be trapped to EL3 */
739 } else if (arm_current_el(env) < 3 &&
740 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
741 return CP_ACCESS_TRAP_EL3;
742 }
743 }
744
745 return CP_ACCESS_OK;
746}
747
3f208fd7
PM
748static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
749 bool isread)
c6f19164
GB
750{
751 /* Check if CPTR accesses are set to trap to EL3 */
752 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
753 return CP_ACCESS_TRAP_EL3;
754 }
755
756 return CP_ACCESS_OK;
757}
758
7d57f408
PM
759static const ARMCPRegInfo v6_cp_reginfo[] = {
760 /* prefetch by MVA in v6, NOP in v7 */
761 { .name = "MVA_prefetch",
762 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
763 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
764 /* We need to break the TB after ISB to execute self-modifying code
765 * correctly and also to take any pending interrupts immediately.
766 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
767 */
7d57f408 768 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 769 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 770 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 771 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 772 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 773 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 774 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 775 .access = PL1_RW,
b848ce2b
FA
776 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
777 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
778 .resetvalue = 0, },
779 /* Watchpoint Fault Address Register : should actually only be present
780 * for 1136, 1176, 11MPCore.
781 */
782 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
783 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 784 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 785 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 786 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
2771db27 787 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
788 REGINFO_SENTINEL
789};
790
3f208fd7
PM
791static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
792 bool isread)
200ac0ef 793{
3b163b01 794 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
795 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
796 * trapping to EL2 or EL3 for other accesses.
200ac0ef 797 */
1fce1ba9
PM
798 int el = arm_current_el(env);
799
800 if (el == 0 && !env->cp15.c9_pmuserenr) {
fcd25206 801 return CP_ACCESS_TRAP;
200ac0ef 802 }
1fce1ba9
PM
803 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
804 && !arm_is_secure_below_el3(env)) {
805 return CP_ACCESS_TRAP_EL2;
806 }
807 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
808 return CP_ACCESS_TRAP_EL3;
809 }
810
fcd25206 811 return CP_ACCESS_OK;
200ac0ef
PM
812}
813
7c2cb42b 814#ifndef CONFIG_USER_ONLY
87124fde
AF
815
816static inline bool arm_ccnt_enabled(CPUARMState *env)
817{
818 /* This does not support checking PMCCFILTR_EL0 register */
819
820 if (!(env->cp15.c9_pmcr & PMCRE)) {
821 return false;
822 }
823
824 return true;
825}
826
ec7b4ce4
AF
827void pmccntr_sync(CPUARMState *env)
828{
829 uint64_t temp_ticks;
830
352c98e5
LV
831 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
832 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
ec7b4ce4
AF
833
834 if (env->cp15.c9_pmcr & PMCRD) {
835 /* Increment once every 64 processor clock cycles */
836 temp_ticks /= 64;
837 }
838
839 if (arm_ccnt_enabled(env)) {
840 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
841 }
842}
843
c4241c7d
PM
844static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
845 uint64_t value)
200ac0ef 846{
942a155b 847 pmccntr_sync(env);
7c2cb42b
AF
848
849 if (value & PMCRC) {
850 /* The counter has been reset */
851 env->cp15.c15_ccnt = 0;
852 }
853
200ac0ef
PM
854 /* only the DP, X, D and E bits are writable */
855 env->cp15.c9_pmcr &= ~0x39;
856 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 857
942a155b 858 pmccntr_sync(env);
7c2cb42b
AF
859}
860
861static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
862{
c92c0687 863 uint64_t total_ticks;
7c2cb42b 864
942a155b 865 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
866 /* Counter is disabled, do not change value */
867 return env->cp15.c15_ccnt;
868 }
869
352c98e5
LV
870 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
871 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
872
873 if (env->cp15.c9_pmcr & PMCRD) {
874 /* Increment once every 64 processor clock cycles */
875 total_ticks /= 64;
876 }
877 return total_ticks - env->cp15.c15_ccnt;
878}
879
880static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
881 uint64_t value)
882{
c92c0687 883 uint64_t total_ticks;
7c2cb42b 884
942a155b 885 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
886 /* Counter is disabled, set the absolute value */
887 env->cp15.c15_ccnt = value;
888 return;
889 }
890
352c98e5
LV
891 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
892 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
7c2cb42b
AF
893
894 if (env->cp15.c9_pmcr & PMCRD) {
895 /* Increment once every 64 processor clock cycles */
896 total_ticks /= 64;
897 }
898 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 899}
421c7ebd
PC
900
901static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
902 uint64_t value)
903{
904 uint64_t cur_val = pmccntr_read(env, NULL);
905
906 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
907}
908
ec7b4ce4
AF
909#else /* CONFIG_USER_ONLY */
910
911void pmccntr_sync(CPUARMState *env)
912{
913}
914
7c2cb42b 915#endif
200ac0ef 916
0614601c
AF
917static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
918 uint64_t value)
919{
920 pmccntr_sync(env);
921 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
922 pmccntr_sync(env);
923}
924
c4241c7d 925static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
926 uint64_t value)
927{
200ac0ef
PM
928 value &= (1 << 31);
929 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
930}
931
c4241c7d
PM
932static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
933 uint64_t value)
200ac0ef 934{
200ac0ef
PM
935 value &= (1 << 31);
936 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
937}
938
c4241c7d
PM
939static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
940 uint64_t value)
200ac0ef 941{
200ac0ef 942 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
943}
944
c4241c7d
PM
945static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
946 uint64_t value)
200ac0ef 947{
200ac0ef 948 env->cp15.c9_pmxevtyper = value & 0xff;
200ac0ef
PM
949}
950
c4241c7d 951static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
952 uint64_t value)
953{
954 env->cp15.c9_pmuserenr = value & 1;
200ac0ef
PM
955}
956
c4241c7d
PM
957static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
958 uint64_t value)
200ac0ef
PM
959{
960 /* We have no event counters so only the C bit can be changed */
961 value &= (1 << 31);
962 env->cp15.c9_pminten |= value;
200ac0ef
PM
963}
964
c4241c7d
PM
965static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
966 uint64_t value)
200ac0ef
PM
967{
968 value &= (1 << 31);
969 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
970}
971
c4241c7d
PM
972static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
973 uint64_t value)
8641136c 974{
a505d7fe
PM
975 /* Note that even though the AArch64 view of this register has bits
976 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
977 * architectural requirements for bits which are RES0 only in some
978 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
979 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
980 */
855ea66d 981 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
982}
983
64e0e2de
EI
984static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
985{
986 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
987 * For bits that vary between AArch32/64, code needs to check the
988 * current execution mode before directly using the feature bit.
989 */
990 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
991
992 if (!arm_feature(env, ARM_FEATURE_EL2)) {
993 valid_mask &= ~SCR_HCE;
994
995 /* On ARMv7, SMD (or SCD as it is called in v7) is only
996 * supported if EL2 exists. The bit is UNK/SBZP when
997 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
998 * when EL2 is unavailable.
4eb27640 999 * On ARMv8, this bit is always available.
64e0e2de 1000 */
4eb27640
GB
1001 if (arm_feature(env, ARM_FEATURE_V7) &&
1002 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1003 valid_mask &= ~SCR_SMD;
1004 }
1005 }
1006
1007 /* Clear all-context RES0 bits. */
1008 value &= valid_mask;
1009 raw_write(env, ri, value);
1010}
1011
c4241c7d 1012static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
1013{
1014 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
1015
1016 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1017 * bank
1018 */
1019 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1020 ri->secure & ARM_CP_SECSTATE_S);
1021
1022 return cpu->ccsidr[index];
776d4e5c
PM
1023}
1024
c4241c7d
PM
1025static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1026 uint64_t value)
776d4e5c 1027{
8d5c773e 1028 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1029}
1030
1090b9c6
PM
1031static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1032{
1033 CPUState *cs = ENV_GET_CPU(env);
1034 uint64_t ret = 0;
1035
1036 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1037 ret |= CPSR_I;
1038 }
1039 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1040 ret |= CPSR_F;
1041 }
1042 /* External aborts are not possible in QEMU so A bit is always clear */
1043 return ret;
1044}
1045
e9aa6c21 1046static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1047 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1048 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1049 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1050 /* Performance monitors are implementation defined in v7,
1051 * but with an ARM recommended set of registers, which we
1052 * follow (although we don't actually implement any counters)
1053 *
1054 * Performance registers fall into three categories:
1055 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1056 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1057 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1058 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1059 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1060 */
1061 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1062 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1063 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1064 .writefn = pmcntenset_write,
1065 .accessfn = pmreg_access,
1066 .raw_writefn = raw_write },
8521466b
AF
1067 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1068 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1069 .access = PL0_RW, .accessfn = pmreg_access,
1070 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1071 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1072 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1073 .access = PL0_RW,
1074 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1075 .accessfn = pmreg_access,
1076 .writefn = pmcntenclr_write,
7a0e58fa 1077 .type = ARM_CP_ALIAS },
8521466b
AF
1078 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1079 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1080 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1081 .type = ARM_CP_ALIAS,
8521466b
AF
1082 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1083 .writefn = pmcntenclr_write },
200ac0ef
PM
1084 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1085 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1086 .accessfn = pmreg_access,
1087 .writefn = pmovsr_write,
1088 .raw_writefn = raw_write },
978364f1
AF
1089 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1090 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1091 .access = PL0_RW, .accessfn = pmreg_access,
1092 .type = ARM_CP_ALIAS,
1093 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1094 .writefn = pmovsr_write,
1095 .raw_writefn = raw_write },
fcd25206 1096 /* Unimplemented so WI. */
200ac0ef 1097 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
fcd25206 1098 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
200ac0ef 1099 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
fcd25206 1100 * We choose to RAZ/WI.
200ac0ef
PM
1101 */
1102 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
fcd25206
PM
1103 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1104 .accessfn = pmreg_access },
7c2cb42b 1105#ifndef CONFIG_USER_ONLY
200ac0ef 1106 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 1107 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 1108 .readfn = pmccntr_read, .writefn = pmccntr_write32,
fcd25206 1109 .accessfn = pmreg_access },
8521466b
AF
1110 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1112 .access = PL0_RW, .accessfn = pmreg_access,
1113 .type = ARM_CP_IO,
1114 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 1115#endif
8521466b
AF
1116 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1117 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 1118 .writefn = pmccfiltr_write,
8521466b
AF
1119 .access = PL0_RW, .accessfn = pmreg_access,
1120 .type = ARM_CP_IO,
1121 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1122 .resetvalue = 0, },
200ac0ef
PM
1123 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1124 .access = PL0_RW,
1125 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
fcd25206
PM
1126 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
1127 .raw_writefn = raw_write },
1128 /* Unimplemented, RAZ/WI. */
200ac0ef 1129 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206
PM
1130 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1131 .accessfn = pmreg_access },
200ac0ef 1132 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1133 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1134 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1135 .resetvalue = 0,
d4e6df63 1136 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
1137 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1138 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 1139 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
1140 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1141 .resetvalue = 0,
1142 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 1143 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 1144 .access = PL1_RW, .accessfn = access_tpm,
200ac0ef
PM
1145 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1146 .resetvalue = 0,
d4e6df63 1147 .writefn = pmintenset_write, .raw_writefn = raw_write },
200ac0ef 1148 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1fce1ba9 1149 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
200ac0ef 1150 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 1151 .writefn = pmintenclr_write, },
978364f1
AF
1152 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1153 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1fce1ba9 1154 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
978364f1
AF
1155 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1156 .writefn = pmintenclr_write },
a505d7fe
PM
1157 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
1158 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8641136c 1159 .access = PL1_RW, .writefn = vbar_write,
fb6c91ba
GB
1160 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
1161 offsetof(CPUARMState, cp15.vbar_ns) },
8641136c 1162 .resetvalue = 0 },
7da845b0
PM
1163 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1164 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 1165 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
1166 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1167 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
1168 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1169 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1170 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
1171 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1172 * just RAZ for all cores:
1173 */
0ff644a7
PM
1174 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1175 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 1176 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
1177 /* Auxiliary fault status registers: these also are IMPDEF, and we
1178 * choose to RAZ/WI for all cores.
1179 */
1180 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1181 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1182 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1183 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1184 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1185 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
1186 /* MAIR can just read-as-written because we don't implement caches
1187 * and so don't need to care about memory attributes.
1188 */
1189 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1190 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 1191 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 1192 .resetvalue = 0 },
4cfb8ad8
PM
1193 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1194 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1195 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1196 .resetvalue = 0 },
b0fe2427
PM
1197 /* For non-long-descriptor page tables these are PRRR and NMRR;
1198 * regardless they still act as reads-as-written for QEMU.
b0fe2427 1199 */
1281f8e3 1200 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
1201 * allows them to assign the correct fieldoffset based on the endianness
1202 * handled in the field definitions.
1203 */
a903c449 1204 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
b0fe2427 1205 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1206 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1207 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 1208 .resetfn = arm_cp_reset_ignore },
a903c449 1209 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
b0fe2427 1210 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1211 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1212 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1213 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1214 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1215 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1216 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1217 /* 32 bit ITLB invalidates */
1218 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1219 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1220 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1221 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1222 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1223 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1224 /* 32 bit DTLB invalidates */
1225 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1226 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1227 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1228 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1229 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1230 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1231 /* 32 bit TLB invalidates */
1232 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1233 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1234 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1235 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1236 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1237 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1238 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1239 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1240 REGINFO_SENTINEL
1241};
1242
1243static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1244 /* 32 bit TLB invalidates, Inner Shareable */
1245 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1246 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1247 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1248 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1249 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1250 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1251 .writefn = tlbiasid_is_write },
995939a6 1252 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1253 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1254 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1255 REGINFO_SENTINEL
1256};
1257
c4241c7d
PM
1258static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1259 uint64_t value)
c326b979
PM
1260{
1261 value &= 1;
1262 env->teecr = value;
c326b979
PM
1263}
1264
3f208fd7
PM
1265static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1266 bool isread)
c326b979 1267{
dcbff19b 1268 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1269 return CP_ACCESS_TRAP;
c326b979 1270 }
92611c00 1271 return CP_ACCESS_OK;
c326b979
PM
1272}
1273
1274static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1275 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1276 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1277 .resetvalue = 0,
1278 .writefn = teecr_write },
1279 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1280 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1281 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1282 REGINFO_SENTINEL
1283};
1284
4d31c596 1285static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1286 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1287 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1288 .access = PL0_RW,
54bf36ed 1289 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1290 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1291 .access = PL0_RW,
54bf36ed
FA
1292 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1293 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1294 .resetfn = arm_cp_reset_ignore },
1295 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1296 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1297 .access = PL0_R|PL1_W,
54bf36ed
FA
1298 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1299 .resetvalue = 0},
4d31c596
PM
1300 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1301 .access = PL0_R|PL1_W,
54bf36ed
FA
1302 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1303 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1304 .resetfn = arm_cp_reset_ignore },
54bf36ed 1305 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1306 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1307 .access = PL1_RW,
54bf36ed
FA
1308 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1309 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1310 .access = PL1_RW,
1311 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1312 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1313 .resetvalue = 0 },
4d31c596
PM
1314 REGINFO_SENTINEL
1315};
1316
55d284af
PM
1317#ifndef CONFIG_USER_ONLY
1318
3f208fd7
PM
1319static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1320 bool isread)
00108f2d 1321{
75502672
PM
1322 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1323 * Writable only at the highest implemented exception level.
1324 */
1325 int el = arm_current_el(env);
1326
1327 switch (el) {
1328 case 0:
1329 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1330 return CP_ACCESS_TRAP;
1331 }
1332 break;
1333 case 1:
1334 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1335 arm_is_secure_below_el3(env)) {
1336 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1337 return CP_ACCESS_TRAP_UNCATEGORIZED;
1338 }
1339 break;
1340 case 2:
1341 case 3:
1342 break;
00108f2d 1343 }
75502672
PM
1344
1345 if (!isread && el < arm_highest_el(env)) {
1346 return CP_ACCESS_TRAP_UNCATEGORIZED;
1347 }
1348
00108f2d
PM
1349 return CP_ACCESS_OK;
1350}
1351
3f208fd7
PM
1352static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1353 bool isread)
00108f2d 1354{
0b6440af
EI
1355 unsigned int cur_el = arm_current_el(env);
1356 bool secure = arm_is_secure(env);
1357
00108f2d 1358 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
0b6440af 1359 if (cur_el == 0 &&
00108f2d
PM
1360 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1361 return CP_ACCESS_TRAP;
1362 }
0b6440af
EI
1363
1364 if (arm_feature(env, ARM_FEATURE_EL2) &&
1365 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1366 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1367 return CP_ACCESS_TRAP_EL2;
1368 }
00108f2d
PM
1369 return CP_ACCESS_OK;
1370}
1371
3f208fd7
PM
1372static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1373 bool isread)
00108f2d 1374{
0b6440af
EI
1375 unsigned int cur_el = arm_current_el(env);
1376 bool secure = arm_is_secure(env);
1377
00108f2d
PM
1378 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1379 * EL0[PV]TEN is zero.
1380 */
0b6440af 1381 if (cur_el == 0 &&
00108f2d
PM
1382 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1383 return CP_ACCESS_TRAP;
1384 }
0b6440af
EI
1385
1386 if (arm_feature(env, ARM_FEATURE_EL2) &&
1387 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1388 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1389 return CP_ACCESS_TRAP_EL2;
1390 }
00108f2d
PM
1391 return CP_ACCESS_OK;
1392}
1393
1394static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
1395 const ARMCPRegInfo *ri,
1396 bool isread)
00108f2d 1397{
3f208fd7 1398 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1399}
1400
1401static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
1402 const ARMCPRegInfo *ri,
1403 bool isread)
00108f2d 1404{
3f208fd7 1405 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1406}
1407
3f208fd7
PM
1408static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1409 bool isread)
00108f2d 1410{
3f208fd7 1411 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
1412}
1413
3f208fd7
PM
1414static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1415 bool isread)
00108f2d 1416{
3f208fd7 1417 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
1418}
1419
b4d3978c 1420static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
1421 const ARMCPRegInfo *ri,
1422 bool isread)
b4d3978c
PM
1423{
1424 /* The AArch64 register view of the secure physical timer is
1425 * always accessible from EL3, and configurably accessible from
1426 * Secure EL1.
1427 */
1428 switch (arm_current_el(env)) {
1429 case 1:
1430 if (!arm_is_secure(env)) {
1431 return CP_ACCESS_TRAP;
1432 }
1433 if (!(env->cp15.scr_el3 & SCR_ST)) {
1434 return CP_ACCESS_TRAP_EL3;
1435 }
1436 return CP_ACCESS_OK;
1437 case 0:
1438 case 2:
1439 return CP_ACCESS_TRAP;
1440 case 3:
1441 return CP_ACCESS_OK;
1442 default:
1443 g_assert_not_reached();
1444 }
1445}
1446
55d284af
PM
1447static uint64_t gt_get_countervalue(CPUARMState *env)
1448{
bc72ad67 1449 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1450}
1451
1452static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1453{
1454 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1455
1456 if (gt->ctl & 1) {
1457 /* Timer enabled: calculate and set current ISTATUS, irq, and
1458 * reset timer to when ISTATUS next has to change
1459 */
edac4d8a
EI
1460 uint64_t offset = timeridx == GTIMER_VIRT ?
1461 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
1462 uint64_t count = gt_get_countervalue(&cpu->env);
1463 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 1464 int istatus = count - offset >= gt->cval;
55d284af
PM
1465 uint64_t nexttick;
1466
1467 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1468 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1469 (istatus && !(gt->ctl & 2)));
1470 if (istatus) {
1471 /* Next transition is when count rolls back over to zero */
1472 nexttick = UINT64_MAX;
1473 } else {
1474 /* Next transition is when we hit cval */
edac4d8a 1475 nexttick = gt->cval + offset;
55d284af
PM
1476 }
1477 /* Note that the desired next expiry time might be beyond the
1478 * signed-64-bit range of a QEMUTimer -- in this case we just
1479 * set the timer for as far in the future as possible. When the
1480 * timer expires we will reset the timer for any remaining period.
1481 */
1482 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1483 nexttick = INT64_MAX / GTIMER_SCALE;
1484 }
bc72ad67 1485 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af
PM
1486 } else {
1487 /* Timer disabled: ISTATUS and timer output always clear */
1488 gt->ctl &= ~4;
1489 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1490 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1491 }
1492}
1493
0e3eca4c
EI
1494static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1495 int timeridx)
55d284af
PM
1496{
1497 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af 1498
bc72ad67 1499 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1500}
1501
c4241c7d 1502static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1503{
c4241c7d 1504 return gt_get_countervalue(env);
55d284af
PM
1505}
1506
edac4d8a
EI
1507static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1508{
1509 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1510}
1511
c4241c7d 1512static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1513 int timeridx,
c4241c7d 1514 uint64_t value)
55d284af 1515{
55d284af
PM
1516 env->cp15.c14_timer[timeridx].cval = value;
1517 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1518}
c4241c7d 1519
0e3eca4c
EI
1520static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1521 int timeridx)
55d284af 1522{
edac4d8a 1523 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1524
c4241c7d 1525 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 1526 (gt_get_countervalue(env) - offset));
55d284af
PM
1527}
1528
c4241c7d 1529static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1530 int timeridx,
c4241c7d 1531 uint64_t value)
55d284af 1532{
edac4d8a 1533 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
55d284af 1534
edac4d8a 1535 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 1536 sextract64(value, 0, 32);
55d284af 1537 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1538}
1539
c4241c7d 1540static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 1541 int timeridx,
c4241c7d 1542 uint64_t value)
55d284af
PM
1543{
1544 ARMCPU *cpu = arm_env_get_cpu(env);
55d284af
PM
1545 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1546
d3afacc7 1547 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1548 if ((oldval ^ value) & 1) {
1549 /* Enable toggled */
1550 gt_recalc_timer(cpu, timeridx);
d3afacc7 1551 } else if ((oldval ^ value) & 2) {
55d284af
PM
1552 /* IMASK toggled: don't need to recalculate,
1553 * just set the interrupt line based on ISTATUS
1554 */
1555 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
d3afacc7 1556 (oldval & 4) && !(value & 2));
55d284af 1557 }
55d284af
PM
1558}
1559
0e3eca4c
EI
1560static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1561{
1562 gt_timer_reset(env, ri, GTIMER_PHYS);
1563}
1564
1565static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1566 uint64_t value)
1567{
1568 gt_cval_write(env, ri, GTIMER_PHYS, value);
1569}
1570
1571static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1572{
1573 return gt_tval_read(env, ri, GTIMER_PHYS);
1574}
1575
1576static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1577 uint64_t value)
1578{
1579 gt_tval_write(env, ri, GTIMER_PHYS, value);
1580}
1581
1582static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1583 uint64_t value)
1584{
1585 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1586}
1587
1588static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1589{
1590 gt_timer_reset(env, ri, GTIMER_VIRT);
1591}
1592
1593static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594 uint64_t value)
1595{
1596 gt_cval_write(env, ri, GTIMER_VIRT, value);
1597}
1598
1599static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1600{
1601 return gt_tval_read(env, ri, GTIMER_VIRT);
1602}
1603
1604static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1605 uint64_t value)
1606{
1607 gt_tval_write(env, ri, GTIMER_VIRT, value);
1608}
1609
1610static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1611 uint64_t value)
1612{
1613 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1614}
1615
edac4d8a
EI
1616static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1617 uint64_t value)
1618{
1619 ARMCPU *cpu = arm_env_get_cpu(env);
1620
1621 raw_write(env, ri, value);
1622 gt_recalc_timer(cpu, GTIMER_VIRT);
1623}
1624
b0e66d95
EI
1625static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1626{
1627 gt_timer_reset(env, ri, GTIMER_HYP);
1628}
1629
1630static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1631 uint64_t value)
1632{
1633 gt_cval_write(env, ri, GTIMER_HYP, value);
1634}
1635
1636static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1637{
1638 return gt_tval_read(env, ri, GTIMER_HYP);
1639}
1640
1641static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1642 uint64_t value)
1643{
1644 gt_tval_write(env, ri, GTIMER_HYP, value);
1645}
1646
1647static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1648 uint64_t value)
1649{
1650 gt_ctl_write(env, ri, GTIMER_HYP, value);
1651}
1652
b4d3978c
PM
1653static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1654{
1655 gt_timer_reset(env, ri, GTIMER_SEC);
1656}
1657
1658static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1659 uint64_t value)
1660{
1661 gt_cval_write(env, ri, GTIMER_SEC, value);
1662}
1663
1664static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1665{
1666 return gt_tval_read(env, ri, GTIMER_SEC);
1667}
1668
1669static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1670 uint64_t value)
1671{
1672 gt_tval_write(env, ri, GTIMER_SEC, value);
1673}
1674
1675static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1676 uint64_t value)
1677{
1678 gt_ctl_write(env, ri, GTIMER_SEC, value);
1679}
1680
55d284af
PM
1681void arm_gt_ptimer_cb(void *opaque)
1682{
1683 ARMCPU *cpu = opaque;
1684
1685 gt_recalc_timer(cpu, GTIMER_PHYS);
1686}
1687
1688void arm_gt_vtimer_cb(void *opaque)
1689{
1690 ARMCPU *cpu = opaque;
1691
1692 gt_recalc_timer(cpu, GTIMER_VIRT);
1693}
1694
b0e66d95
EI
1695void arm_gt_htimer_cb(void *opaque)
1696{
1697 ARMCPU *cpu = opaque;
1698
1699 gt_recalc_timer(cpu, GTIMER_HYP);
1700}
1701
b4d3978c
PM
1702void arm_gt_stimer_cb(void *opaque)
1703{
1704 ARMCPU *cpu = opaque;
1705
1706 gt_recalc_timer(cpu, GTIMER_SEC);
1707}
1708
55d284af
PM
1709static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1710 /* Note that CNTFRQ is purely reads-as-written for the benefit
1711 * of software; writing it doesn't actually change the timer frequency.
1712 * Our reset value matches the fixed frequency we implement the timer at.
1713 */
1714 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1715 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1716 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1717 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
1718 },
1719 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1720 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1721 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1722 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1723 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1724 },
1725 /* overall control: mostly access permissions */
a7adc4b7
PM
1726 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1727 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1728 .access = PL1_RW,
1729 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1730 .resetvalue = 0,
1731 },
1732 /* per-timer control */
1733 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 1734 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1735 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1736 .accessfn = gt_ptimer_access,
1737 .fieldoffset = offsetoflow32(CPUARMState,
1738 cp15.c14_timer[GTIMER_PHYS].ctl),
0e3eca4c 1739 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
a7adc4b7 1740 },
9ff9dd3c
PM
1741 { .name = "CNTP_CTL(S)",
1742 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1743 .secure = ARM_CP_SECSTATE_S,
1744 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1745 .accessfn = gt_ptimer_access,
1746 .fieldoffset = offsetoflow32(CPUARMState,
1747 cp15.c14_timer[GTIMER_SEC].ctl),
1748 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1749 },
a7adc4b7
PM
1750 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1751 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1752 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1753 .accessfn = gt_ptimer_access,
55d284af
PM
1754 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1755 .resetvalue = 0,
0e3eca4c 1756 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1757 },
1758 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1759 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1760 .accessfn = gt_vtimer_access,
1761 .fieldoffset = offsetoflow32(CPUARMState,
1762 cp15.c14_timer[GTIMER_VIRT].ctl),
0e3eca4c 1763 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
1764 },
1765 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1766 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1767 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1768 .accessfn = gt_vtimer_access,
55d284af
PM
1769 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1770 .resetvalue = 0,
0e3eca4c 1771 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1772 },
1773 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1774 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 1775 .secure = ARM_CP_SECSTATE_NS,
7a0e58fa 1776 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1777 .accessfn = gt_ptimer_access,
0e3eca4c 1778 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
55d284af 1779 },
9ff9dd3c
PM
1780 { .name = "CNTP_TVAL(S)",
1781 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1782 .secure = ARM_CP_SECSTATE_S,
1783 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1784 .accessfn = gt_ptimer_access,
1785 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1786 },
a7adc4b7
PM
1787 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1788 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 1789 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
1790 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
1791 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
a7adc4b7 1792 },
55d284af 1793 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 1794 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1795 .accessfn = gt_vtimer_access,
0e3eca4c 1796 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
55d284af 1797 },
a7adc4b7
PM
1798 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1799 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 1800 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
0e3eca4c
EI
1801 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
1802 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
a7adc4b7 1803 },
55d284af
PM
1804 /* The counter itself */
1805 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 1806 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1807 .accessfn = gt_pct_access,
a7adc4b7
PM
1808 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1809 },
1810 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1811 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 1812 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 1813 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
1814 },
1815 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 1816 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1817 .accessfn = gt_vct_access,
edac4d8a 1818 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
1819 },
1820 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1821 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 1822 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 1823 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
1824 },
1825 /* Comparison value, indicating when the timer goes off */
1826 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 1827 .secure = ARM_CP_SECSTATE_NS,
55d284af 1828 .access = PL1_RW | PL0_R,
7a0e58fa 1829 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1830 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 1831 .accessfn = gt_ptimer_access,
0e3eca4c 1832 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
a7adc4b7 1833 },
9ff9dd3c
PM
1834 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
1835 .secure = ARM_CP_SECSTATE_S,
1836 .access = PL1_RW | PL0_R,
1837 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1838 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1839 .accessfn = gt_ptimer_access,
1840 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1841 },
a7adc4b7
PM
1842 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1843 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1844 .access = PL1_RW | PL0_R,
1845 .type = ARM_CP_IO,
1846 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 1847 .resetvalue = 0, .accessfn = gt_ptimer_access,
0e3eca4c 1848 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
55d284af
PM
1849 },
1850 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1851 .access = PL1_RW | PL0_R,
7a0e58fa 1852 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1853 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 1854 .accessfn = gt_vtimer_access,
0e3eca4c 1855 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
1856 },
1857 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1858 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1859 .access = PL1_RW | PL0_R,
1860 .type = ARM_CP_IO,
1861 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1862 .resetvalue = 0, .accessfn = gt_vtimer_access,
0e3eca4c 1863 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
55d284af 1864 },
b4d3978c
PM
1865 /* Secure timer -- this is actually restricted to only EL3
1866 * and configurably Secure-EL1 via the accessfn.
1867 */
1868 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
1869 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
1870 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
1871 .accessfn = gt_stimer_access,
1872 .readfn = gt_sec_tval_read,
1873 .writefn = gt_sec_tval_write,
1874 .resetfn = gt_sec_timer_reset,
1875 },
1876 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
1877 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
1878 .type = ARM_CP_IO, .access = PL1_RW,
1879 .accessfn = gt_stimer_access,
1880 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
1881 .resetvalue = 0,
1882 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1883 },
1884 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
1885 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
1886 .type = ARM_CP_IO, .access = PL1_RW,
1887 .accessfn = gt_stimer_access,
1888 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1889 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1890 },
55d284af
PM
1891 REGINFO_SENTINEL
1892};
1893
1894#else
1895/* In user-mode none of the generic timer registers are accessible,
bc72ad67 1896 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
1897 * so instead just don't register any of them.
1898 */
6cc7a3ae 1899static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
1900 REGINFO_SENTINEL
1901};
1902
55d284af
PM
1903#endif
1904
c4241c7d 1905static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 1906{
891a2fe7 1907 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 1908 raw_write(env, ri, value);
891a2fe7 1909 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 1910 raw_write(env, ri, value & 0xfffff6ff);
4a501606 1911 } else {
8d5c773e 1912 raw_write(env, ri, value & 0xfffff1ff);
4a501606 1913 }
4a501606
PM
1914}
1915
1916#ifndef CONFIG_USER_ONLY
1917/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 1918
3f208fd7
PM
1919static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
1920 bool isread)
92611c00
PM
1921{
1922 if (ri->opc2 & 4) {
87562e4f
PM
1923 /* The ATS12NSO* operations must trap to EL3 if executed in
1924 * Secure EL1 (which can only happen if EL3 is AArch64).
1925 * They are simply UNDEF if executed from NS EL1.
1926 * They function normally from EL2 or EL3.
92611c00 1927 */
87562e4f
PM
1928 if (arm_current_el(env) == 1) {
1929 if (arm_is_secure_below_el3(env)) {
1930 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
1931 }
1932 return CP_ACCESS_TRAP_UNCATEGORIZED;
1933 }
92611c00
PM
1934 }
1935 return CP_ACCESS_OK;
1936}
1937
060e8a48 1938static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
d3649702 1939 int access_type, ARMMMUIdx mmu_idx)
4a501606 1940{
a8170e5e 1941 hwaddr phys_addr;
4a501606
PM
1942 target_ulong page_size;
1943 int prot;
b7cc4e82
PC
1944 uint32_t fsr;
1945 bool ret;
01c097f7 1946 uint64_t par64;
8bf5b6a9 1947 MemTxAttrs attrs = {};
e14b5a23 1948 ARMMMUFaultInfo fi = {};
4a501606 1949
d3649702 1950 ret = get_phys_addr(env, value, access_type, mmu_idx,
e14b5a23 1951 &phys_addr, &attrs, &prot, &page_size, &fsr, &fi);
702a9357 1952 if (extended_addresses_enabled(env)) {
b7cc4e82 1953 /* fsr is a DFSR/IFSR value for the long descriptor
702a9357
PM
1954 * translation table format, but with WnR always clear.
1955 * Convert it to a 64-bit PAR.
1956 */
01c097f7 1957 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 1958 if (!ret) {
702a9357 1959 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
1960 if (!attrs.secure) {
1961 par64 |= (1 << 9); /* NS */
1962 }
702a9357 1963 /* We don't set the ATTR or SH fields in the PAR. */
4a501606 1964 } else {
702a9357 1965 par64 |= 1; /* F */
b7cc4e82 1966 par64 |= (fsr & 0x3f) << 1; /* FS */
702a9357
PM
1967 /* Note that S2WLK and FSTAGE are always zero, because we don't
1968 * implement virtualization and therefore there can't be a stage 2
1969 * fault.
1970 */
4a501606
PM
1971 }
1972 } else {
b7cc4e82 1973 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
1974 * translation table format (with WnR always clear).
1975 * Convert it to a 32-bit PAR.
1976 */
b7cc4e82 1977 if (!ret) {
702a9357
PM
1978 /* We do not set any attribute bits in the PAR */
1979 if (page_size == (1 << 24)
1980 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 1981 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 1982 } else {
01c097f7 1983 par64 = phys_addr & 0xfffff000;
702a9357 1984 }
8bf5b6a9
PM
1985 if (!attrs.secure) {
1986 par64 |= (1 << 9); /* NS */
1987 }
702a9357 1988 } else {
b7cc4e82
PC
1989 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
1990 ((fsr & 0xf) << 1) | 1;
702a9357 1991 }
4a501606 1992 }
060e8a48
PM
1993 return par64;
1994}
1995
1996static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1997{
060e8a48
PM
1998 int access_type = ri->opc2 & 1;
1999 uint64_t par64;
d3649702
PM
2000 ARMMMUIdx mmu_idx;
2001 int el = arm_current_el(env);
2002 bool secure = arm_is_secure_below_el3(env);
060e8a48 2003
d3649702
PM
2004 switch (ri->opc2 & 6) {
2005 case 0:
2006 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2007 switch (el) {
2008 case 3:
2009 mmu_idx = ARMMMUIdx_S1E3;
2010 break;
2011 case 2:
2012 mmu_idx = ARMMMUIdx_S1NSE1;
2013 break;
2014 case 1:
2015 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2016 break;
2017 default:
2018 g_assert_not_reached();
2019 }
2020 break;
2021 case 2:
2022 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2023 switch (el) {
2024 case 3:
2025 mmu_idx = ARMMMUIdx_S1SE0;
2026 break;
2027 case 2:
2028 mmu_idx = ARMMMUIdx_S1NSE0;
2029 break;
2030 case 1:
2031 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2032 break;
2033 default:
2034 g_assert_not_reached();
2035 }
2036 break;
2037 case 4:
2038 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2039 mmu_idx = ARMMMUIdx_S12NSE1;
2040 break;
2041 case 6:
2042 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2043 mmu_idx = ARMMMUIdx_S12NSE0;
2044 break;
2045 default:
2046 g_assert_not_reached();
2047 }
2048
2049 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
2050
2051 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606 2052}
060e8a48 2053
14db7fe0
PM
2054static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2055 uint64_t value)
2056{
2057 int access_type = ri->opc2 & 1;
2058 uint64_t par64;
2059
2060 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2061
2062 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2063}
2064
3f208fd7
PM
2065static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2066 bool isread)
2a47df95
PM
2067{
2068 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2069 return CP_ACCESS_TRAP;
2070 }
2071 return CP_ACCESS_OK;
2072}
2073
060e8a48
PM
2074static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2075 uint64_t value)
2076{
060e8a48 2077 int access_type = ri->opc2 & 1;
d3649702
PM
2078 ARMMMUIdx mmu_idx;
2079 int secure = arm_is_secure_below_el3(env);
2080
2081 switch (ri->opc2 & 6) {
2082 case 0:
2083 switch (ri->opc1) {
2084 case 0: /* AT S1E1R, AT S1E1W */
2085 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2086 break;
2087 case 4: /* AT S1E2R, AT S1E2W */
2088 mmu_idx = ARMMMUIdx_S1E2;
2089 break;
2090 case 6: /* AT S1E3R, AT S1E3W */
2091 mmu_idx = ARMMMUIdx_S1E3;
2092 break;
2093 default:
2094 g_assert_not_reached();
2095 }
2096 break;
2097 case 2: /* AT S1E0R, AT S1E0W */
2098 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2099 break;
2100 case 4: /* AT S12E1R, AT S12E1W */
2a47df95 2101 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
d3649702
PM
2102 break;
2103 case 6: /* AT S12E0R, AT S12E0W */
2a47df95 2104 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
d3649702
PM
2105 break;
2106 default:
2107 g_assert_not_reached();
2108 }
060e8a48 2109
d3649702 2110 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
060e8a48 2111}
4a501606
PM
2112#endif
2113
2114static const ARMCPRegInfo vapa_cp_reginfo[] = {
2115 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2116 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
2117 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2118 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
2119 .writefn = par_write },
2120#ifndef CONFIG_USER_ONLY
87562e4f 2121 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 2122 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 2123 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 2124 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
2125#endif
2126 REGINFO_SENTINEL
2127};
2128
18032bec
PM
2129/* Return basic MPU access permission bits. */
2130static uint32_t simple_mpu_ap_bits(uint32_t val)
2131{
2132 uint32_t ret;
2133 uint32_t mask;
2134 int i;
2135 ret = 0;
2136 mask = 3;
2137 for (i = 0; i < 16; i += 2) {
2138 ret |= (val >> i) & mask;
2139 mask <<= 2;
2140 }
2141 return ret;
2142}
2143
2144/* Pad basic MPU access permission bits to extended format. */
2145static uint32_t extended_mpu_ap_bits(uint32_t val)
2146{
2147 uint32_t ret;
2148 uint32_t mask;
2149 int i;
2150 ret = 0;
2151 mask = 3;
2152 for (i = 0; i < 16; i += 2) {
2153 ret |= (val & mask) << i;
2154 mask <<= 2;
2155 }
2156 return ret;
2157}
2158
c4241c7d
PM
2159static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2160 uint64_t value)
18032bec 2161{
7e09797c 2162 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
2163}
2164
c4241c7d 2165static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2166{
7e09797c 2167 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
2168}
2169
c4241c7d
PM
2170static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2171 uint64_t value)
18032bec 2172{
7e09797c 2173 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
2174}
2175
c4241c7d 2176static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 2177{
7e09797c 2178 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
2179}
2180
6cb0b013
PC
2181static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2182{
2183 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2184
2185 if (!u32p) {
2186 return 0;
2187 }
2188
2189 u32p += env->cp15.c6_rgnr;
2190 return *u32p;
2191}
2192
2193static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2194 uint64_t value)
2195{
2196 ARMCPU *cpu = arm_env_get_cpu(env);
2197 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2198
2199 if (!u32p) {
2200 return;
2201 }
2202
2203 u32p += env->cp15.c6_rgnr;
2204 tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */
2205 *u32p = value;
2206}
2207
2208static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2209{
2210 ARMCPU *cpu = arm_env_get_cpu(env);
2211 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2212
2213 if (!u32p) {
2214 return;
2215 }
2216
2217 memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion);
2218}
2219
2220static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2221 uint64_t value)
2222{
2223 ARMCPU *cpu = arm_env_get_cpu(env);
2224 uint32_t nrgs = cpu->pmsav7_dregion;
2225
2226 if (value >= nrgs) {
2227 qemu_log_mask(LOG_GUEST_ERROR,
2228 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2229 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2230 return;
2231 }
2232
2233 raw_write(env, ri, value);
2234}
2235
2236static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2237 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2238 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2239 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2240 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2241 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2242 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2243 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2244 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2245 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2246 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2247 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2248 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2249 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2250 .access = PL1_RW,
2251 .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr),
2252 .writefn = pmsav7_rgnr_write },
2253 REGINFO_SENTINEL
2254};
2255
18032bec
PM
2256static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2257 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2258 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2259 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
2260 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2261 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 2262 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 2263 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
2264 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2265 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2266 .access = PL1_RW,
7e09797c
PM
2267 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2268 .resetvalue = 0, },
18032bec
PM
2269 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2270 .access = PL1_RW,
7e09797c
PM
2271 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2272 .resetvalue = 0, },
ecce5c3c
PM
2273 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2274 .access = PL1_RW,
2275 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2276 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2277 .access = PL1_RW,
2278 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 2279 /* Protection region base and size registers */
e508a92b
PM
2280 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2281 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2282 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2283 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2284 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2285 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2286 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2287 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2288 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2289 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2290 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2291 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2292 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2293 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2294 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2295 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2296 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2297 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2298 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2299 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2300 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2301 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2302 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2303 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
2304 REGINFO_SENTINEL
2305};
2306
c4241c7d
PM
2307static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2308 uint64_t value)
ecce5c3c 2309{
11f136ee 2310 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
2311 int maskshift = extract32(value, 0, 3);
2312
e389be16
FA
2313 if (!arm_feature(env, ARM_FEATURE_V8)) {
2314 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2315 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2316 * using Long-desciptor translation table format */
2317 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2318 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2319 /* In an implementation that includes the Security Extensions
2320 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2321 * Short-descriptor translation table format.
2322 */
2323 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2324 } else {
2325 value &= TTBCR_N;
2326 }
e42c4db3 2327 }
e389be16 2328
b6af0975 2329 /* Update the masks corresponding to the TCR bank being written
11f136ee 2330 * Note that we always calculate mask and base_mask, but
e42c4db3 2331 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
2332 * for long-descriptor tables the TCR fields are used differently
2333 * and the mask and base_mask values are meaningless.
e42c4db3 2334 */
11f136ee
FA
2335 tcr->raw_tcr = value;
2336 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2337 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
2338}
2339
c4241c7d
PM
2340static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2341 uint64_t value)
d4e6df63 2342{
00c8cb0a
AF
2343 ARMCPU *cpu = arm_env_get_cpu(env);
2344
d4e6df63
PM
2345 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2346 /* With LPAE the TTBCR could result in a change of ASID
2347 * via the TTBCR.A1 bit, so do a TLB flush.
2348 */
00c8cb0a 2349 tlb_flush(CPU(cpu), 1);
d4e6df63 2350 }
c4241c7d 2351 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
2352}
2353
ecce5c3c
PM
2354static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2355{
11f136ee
FA
2356 TCR *tcr = raw_ptr(env, ri);
2357
2358 /* Reset both the TCR as well as the masks corresponding to the bank of
2359 * the TCR being reset.
2360 */
2361 tcr->raw_tcr = 0;
2362 tcr->mask = 0;
2363 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
2364}
2365
cb2e37df
PM
2366static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2367 uint64_t value)
2368{
00c8cb0a 2369 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 2370 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 2371
cb2e37df 2372 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
00c8cb0a 2373 tlb_flush(CPU(cpu), 1);
11f136ee 2374 tcr->raw_tcr = value;
cb2e37df
PM
2375}
2376
327ed10f
PM
2377static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2378 uint64_t value)
2379{
2380 /* 64 bit accesses to the TTBRs can change the ASID and so we
2381 * must flush the TLB.
2382 */
2383 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
2384 ARMCPU *cpu = arm_env_get_cpu(env);
2385
2386 tlb_flush(CPU(cpu), 1);
327ed10f
PM
2387 }
2388 raw_write(env, ri, value);
2389}
2390
b698e9cf
EI
2391static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2392 uint64_t value)
2393{
2394 ARMCPU *cpu = arm_env_get_cpu(env);
2395 CPUState *cs = CPU(cpu);
2396
2397 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2398 if (raw_read(env, ri) != value) {
2399 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2400 ARMMMUIdx_S2NS, -1);
2401 raw_write(env, ri, value);
2402 }
2403}
2404
8e5d75c9 2405static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 2406 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2407 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73 2408 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 2409 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 2410 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
2411 .access = PL1_RW, .resetvalue = 0,
2412 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2413 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9
PC
2414 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2415 .access = PL1_RW, .resetvalue = 0,
2416 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2417 offsetof(CPUARMState, cp15.dfar_ns) } },
2418 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2419 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2420 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2421 .resetvalue = 0, },
2422 REGINFO_SENTINEL
2423};
2424
2425static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
2426 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2427 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2428 .access = PL1_RW,
d81c519c 2429 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 2430 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2431 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2432 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2433 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2434 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 2435 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
2436 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2437 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2438 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2439 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
2440 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2441 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2442 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2443 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 2444 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 2445 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 2446 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 2447 .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
2448 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2449 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
2450 REGINFO_SENTINEL
2451};
2452
c4241c7d
PM
2453static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2454 uint64_t value)
1047b9d7
PM
2455{
2456 env->cp15.c15_ticonfig = value & 0xe7;
2457 /* The OS_TYPE bit in this register changes the reported CPUID! */
2458 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2459 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
2460}
2461
c4241c7d
PM
2462static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2463 uint64_t value)
1047b9d7
PM
2464{
2465 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
2466}
2467
c4241c7d
PM
2468static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2469 uint64_t value)
1047b9d7
PM
2470{
2471 /* Wait-for-interrupt (deprecated) */
c3affe56 2472 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
2473}
2474
c4241c7d
PM
2475static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2476 uint64_t value)
c4804214
PM
2477{
2478 /* On OMAP there are registers indicating the max/min index of dcache lines
2479 * containing a dirty line; cache flush operations have to reset these.
2480 */
2481 env->cp15.c15_i_max = 0x000;
2482 env->cp15.c15_i_min = 0xff0;
c4804214
PM
2483}
2484
18032bec
PM
2485static const ARMCPRegInfo omap_cp_reginfo[] = {
2486 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2487 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 2488 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 2489 .resetvalue = 0, },
1047b9d7
PM
2490 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2491 .access = PL1_RW, .type = ARM_CP_NOP },
2492 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2493 .access = PL1_RW,
2494 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2495 .writefn = omap_ticonfig_write },
2496 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2497 .access = PL1_RW,
2498 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2499 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2500 .access = PL1_RW, .resetvalue = 0xff0,
2501 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2502 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2503 .access = PL1_RW,
2504 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2505 .writefn = omap_threadid_write },
2506 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2507 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 2508 .type = ARM_CP_NO_RAW,
1047b9d7
PM
2509 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2510 /* TODO: Peripheral port remap register:
2511 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2512 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2513 * when MMU is off.
2514 */
c4804214 2515 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 2516 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 2517 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 2518 .writefn = omap_cachemaint_write },
34f90529
PM
2519 { .name = "C9", .cp = 15, .crn = 9,
2520 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2521 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
2522 REGINFO_SENTINEL
2523};
2524
c4241c7d
PM
2525static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2526 uint64_t value)
1047b9d7 2527{
c0f4af17 2528 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
2529}
2530
2531static const ARMCPRegInfo xscale_cp_reginfo[] = {
2532 { .name = "XSCALE_CPAR",
2533 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2534 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2535 .writefn = xscale_cpar_write, },
2771db27
PM
2536 { .name = "XSCALE_AUXCR",
2537 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2538 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2539 .resetvalue = 0, },
3b771579
PM
2540 /* XScale specific cache-lockdown: since we have no cache we NOP these
2541 * and hope the guest does not really rely on cache behaviour.
2542 */
2543 { .name = "XSCALE_LOCK_ICACHE_LINE",
2544 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2545 .access = PL1_W, .type = ARM_CP_NOP },
2546 { .name = "XSCALE_UNLOCK_ICACHE",
2547 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2548 .access = PL1_W, .type = ARM_CP_NOP },
2549 { .name = "XSCALE_DCACHE_LOCK",
2550 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2551 .access = PL1_RW, .type = ARM_CP_NOP },
2552 { .name = "XSCALE_UNLOCK_DCACHE",
2553 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2554 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
2555 REGINFO_SENTINEL
2556};
2557
2558static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2559 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2560 * implementation of this implementation-defined space.
2561 * Ideally this should eventually disappear in favour of actually
2562 * implementing the correct behaviour for all cores.
2563 */
2564 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2565 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 2566 .access = PL1_RW,
7a0e58fa 2567 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 2568 .resetvalue = 0 },
18032bec
PM
2569 REGINFO_SENTINEL
2570};
2571
c4804214
PM
2572static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2573 /* Cache status: RAZ because we have no cache so it's always clean */
2574 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 2575 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2576 .resetvalue = 0 },
c4804214
PM
2577 REGINFO_SENTINEL
2578};
2579
2580static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2581 /* We never have a a block transfer operation in progress */
2582 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 2583 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2584 .resetvalue = 0 },
30b05bba
PM
2585 /* The cache ops themselves: these all NOP for QEMU */
2586 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2587 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2588 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2589 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2590 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2591 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2592 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2593 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2594 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2595 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2596 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2597 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
2598 REGINFO_SENTINEL
2599};
2600
2601static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2602 /* The cache test-and-clean instructions always return (1 << 30)
2603 * to indicate that there are no dirty cache lines.
2604 */
2605 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 2606 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2607 .resetvalue = (1 << 30) },
c4804214 2608 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 2609 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 2610 .resetvalue = (1 << 30) },
c4804214
PM
2611 REGINFO_SENTINEL
2612};
2613
34f90529
PM
2614static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2615 /* Ignore ReadBuffer accesses */
2616 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2617 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 2618 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 2619 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
2620 REGINFO_SENTINEL
2621};
2622
731de9e6
EI
2623static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2624{
2625 ARMCPU *cpu = arm_env_get_cpu(env);
2626 unsigned int cur_el = arm_current_el(env);
2627 bool secure = arm_is_secure(env);
2628
2629 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2630 return env->cp15.vpidr_el2;
2631 }
2632 return raw_read(env, ri);
2633}
2634
06a7e647 2635static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 2636{
eb5e1d3c
PF
2637 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2638 uint64_t mpidr = cpu->mp_affinity;
2639
81bdde9d 2640 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 2641 mpidr |= (1U << 31);
81bdde9d
PM
2642 /* Cores which are uniprocessor (non-coherent)
2643 * but still implement the MP extensions set
a8e81b31 2644 * bit 30. (For instance, Cortex-R5).
81bdde9d 2645 */
a8e81b31
PC
2646 if (cpu->mp_is_up) {
2647 mpidr |= (1u << 30);
2648 }
81bdde9d 2649 }
c4241c7d 2650 return mpidr;
81bdde9d
PM
2651}
2652
06a7e647
EI
2653static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2654{
f0d574d6
EI
2655 unsigned int cur_el = arm_current_el(env);
2656 bool secure = arm_is_secure(env);
2657
2658 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2659 return env->cp15.vmpidr_el2;
2660 }
06a7e647
EI
2661 return mpidr_read_val(env);
2662}
2663
81bdde9d 2664static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
2665 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2666 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 2667 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
2668 REGINFO_SENTINEL
2669};
2670
7ac681cf 2671static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 2672 /* NOP AMAIR0/1 */
b0fe2427
PM
2673 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2674 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
a903c449 2675 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2676 .resetvalue = 0 },
b0fe2427 2677 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 2678 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
a903c449 2679 .access = PL1_RW, .type = ARM_CP_CONST,
7ac681cf 2680 .resetvalue = 0 },
891a2fe7 2681 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
2682 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2683 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2684 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 2685 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 2686 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2687 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2688 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 2689 .writefn = vmsa_ttbr_write, },
891a2fe7 2690 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 2691 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
2692 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2693 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 2694 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
2695 REGINFO_SENTINEL
2696};
2697
c4241c7d 2698static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2699{
c4241c7d 2700 return vfp_get_fpcr(env);
b0d2b7d0
PM
2701}
2702
c4241c7d
PM
2703static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2704 uint64_t value)
b0d2b7d0
PM
2705{
2706 vfp_set_fpcr(env, value);
b0d2b7d0
PM
2707}
2708
c4241c7d 2709static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 2710{
c4241c7d 2711 return vfp_get_fpsr(env);
b0d2b7d0
PM
2712}
2713
c4241c7d
PM
2714static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2715 uint64_t value)
b0d2b7d0
PM
2716{
2717 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2718}
2719
3f208fd7
PM
2720static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2721 bool isread)
c2b820fe 2722{
137feaa9 2723 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2724 return CP_ACCESS_TRAP;
2725 }
2726 return CP_ACCESS_OK;
2727}
2728
2729static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2730 uint64_t value)
2731{
2732 env->daif = value & PSTATE_DAIF;
2733}
2734
8af35c37 2735static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3f208fd7
PM
2736 const ARMCPRegInfo *ri,
2737 bool isread)
8af35c37
PM
2738{
2739 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2740 * SCTLR_EL1.UCI is set.
2741 */
137feaa9 2742 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2743 return CP_ACCESS_TRAP;
2744 }
2745 return CP_ACCESS_OK;
2746}
2747
dbb1fb27
AB
2748/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2749 * Page D4-1736 (DDI0487A.b)
2750 */
2751
fd3ed969
PM
2752static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2753 uint64_t value)
168aa23b 2754{
31b030d4 2755 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969 2756 CPUState *cs = CPU(cpu);
dbb1fb27 2757
fd3ed969
PM
2758 if (arm_is_secure_below_el3(env)) {
2759 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2760 } else {
2761 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2762 }
168aa23b
PM
2763}
2764
fd3ed969
PM
2765static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2766 uint64_t value)
168aa23b 2767{
fd3ed969
PM
2768 bool sec = arm_is_secure_below_el3(env);
2769 CPUState *other_cs;
dbb1fb27 2770
fd3ed969
PM
2771 CPU_FOREACH(other_cs) {
2772 if (sec) {
2773 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2774 } else {
2775 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2776 ARMMMUIdx_S12NSE0, -1);
2777 }
2778 }
168aa23b
PM
2779}
2780
fd3ed969
PM
2781static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782 uint64_t value)
168aa23b 2783{
fd3ed969
PM
2784 /* Note that the 'ALL' scope must invalidate both stage 1 and
2785 * stage 2 translations, whereas most other scopes only invalidate
2786 * stage 1 translations.
2787 */
00c8cb0a 2788 ARMCPU *cpu = arm_env_get_cpu(env);
fd3ed969
PM
2789 CPUState *cs = CPU(cpu);
2790
2791 if (arm_is_secure_below_el3(env)) {
2792 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2793 } else {
2794 if (arm_feature(env, ARM_FEATURE_EL2)) {
2795 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2796 ARMMMUIdx_S2NS, -1);
2797 } else {
2798 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2799 }
2800 }
168aa23b
PM
2801}
2802
fd3ed969 2803static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
2804 uint64_t value)
2805{
fd3ed969
PM
2806 ARMCPU *cpu = arm_env_get_cpu(env);
2807 CPUState *cs = CPU(cpu);
2808
2809 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2810}
2811
43efaa33
PM
2812static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2813 uint64_t value)
2814{
2815 ARMCPU *cpu = arm_env_get_cpu(env);
2816 CPUState *cs = CPU(cpu);
2817
2818 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2819}
2820
fd3ed969
PM
2821static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822 uint64_t value)
2823{
2824 /* Note that the 'ALL' scope must invalidate both stage 1 and
2825 * stage 2 translations, whereas most other scopes only invalidate
2826 * stage 1 translations.
2827 */
2828 bool sec = arm_is_secure_below_el3(env);
2829 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
fa439fc5 2830 CPUState *other_cs;
fa439fc5
PM
2831
2832 CPU_FOREACH(other_cs) {
fd3ed969
PM
2833 if (sec) {
2834 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2835 } else if (has_el2) {
2836 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2837 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2838 } else {
2839 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2840 ARMMMUIdx_S12NSE0, -1);
2841 }
fa439fc5
PM
2842 }
2843}
2844
2bfb9d75
PM
2845static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2846 uint64_t value)
2847{
2848 CPUState *other_cs;
2849
2850 CPU_FOREACH(other_cs) {
2851 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2852 }
2853}
2854
43efaa33
PM
2855static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856 uint64_t value)
2857{
2858 CPUState *other_cs;
2859
2860 CPU_FOREACH(other_cs) {
2861 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2862 }
2863}
2864
fd3ed969
PM
2865static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2866 uint64_t value)
2867{
2868 /* Invalidate by VA, EL1&0 (AArch64 version).
2869 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2870 * since we don't support flush-for-specific-ASID-only or
2871 * flush-last-level-only.
2872 */
2873 ARMCPU *cpu = arm_env_get_cpu(env);
2874 CPUState *cs = CPU(cpu);
2875 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2876
2877 if (arm_is_secure_below_el3(env)) {
2878 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2879 ARMMMUIdx_S1SE0, -1);
2880 } else {
2881 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2882 ARMMMUIdx_S12NSE0, -1);
2883 }
2884}
2885
2886static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887 uint64_t value)
fa439fc5 2888{
fd3ed969
PM
2889 /* Invalidate by VA, EL2
2890 * Currently handles both VAE2 and VALE2, since we don't support
2891 * flush-last-level-only.
2892 */
2893 ARMCPU *cpu = arm_env_get_cpu(env);
2894 CPUState *cs = CPU(cpu);
2895 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2896
2897 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2898}
2899
43efaa33
PM
2900static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2901 uint64_t value)
2902{
2903 /* Invalidate by VA, EL3
2904 * Currently handles both VAE3 and VALE3, since we don't support
2905 * flush-last-level-only.
2906 */
2907 ARMCPU *cpu = arm_env_get_cpu(env);
2908 CPUState *cs = CPU(cpu);
2909 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2910
2911 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2912}
2913
fd3ed969
PM
2914static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915 uint64_t value)
2916{
2917 bool sec = arm_is_secure_below_el3(env);
fa439fc5
PM
2918 CPUState *other_cs;
2919 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2920
2921 CPU_FOREACH(other_cs) {
fd3ed969
PM
2922 if (sec) {
2923 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2924 ARMMMUIdx_S1SE0, -1);
2925 } else {
2926 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2927 ARMMMUIdx_S12NSE0, -1);
2928 }
fa439fc5
PM
2929 }
2930}
2931
fd3ed969
PM
2932static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2933 uint64_t value)
fa439fc5
PM
2934{
2935 CPUState *other_cs;
fd3ed969 2936 uint64_t pageaddr = sextract64(value << 12, 0, 56);
fa439fc5
PM
2937
2938 CPU_FOREACH(other_cs) {
fd3ed969 2939 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
fa439fc5
PM
2940 }
2941}
2942
43efaa33
PM
2943static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944 uint64_t value)
2945{
2946 CPUState *other_cs;
2947 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2948
2949 CPU_FOREACH(other_cs) {
2950 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2951 }
2952}
2953
cea66e91
PM
2954static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955 uint64_t value)
2956{
2957 /* Invalidate by IPA. This has to invalidate any structures that
2958 * contain only stage 2 translation information, but does not need
2959 * to apply to structures that contain combined stage 1 and stage 2
2960 * translation information.
2961 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2962 */
2963 ARMCPU *cpu = arm_env_get_cpu(env);
2964 CPUState *cs = CPU(cpu);
2965 uint64_t pageaddr;
2966
2967 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2968 return;
2969 }
2970
2971 pageaddr = sextract64(value << 12, 0, 48);
2972
2973 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2974}
2975
2976static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2977 uint64_t value)
2978{
2979 CPUState *other_cs;
2980 uint64_t pageaddr;
2981
2982 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2983 return;
2984 }
2985
2986 pageaddr = sextract64(value << 12, 0, 48);
2987
2988 CPU_FOREACH(other_cs) {
2989 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2990 }
2991}
2992
3f208fd7
PM
2993static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
2994 bool isread)
aca3f40b
PM
2995{
2996 /* We don't implement EL2, so the only control on DC ZVA is the
2997 * bit in the SCTLR which can prohibit access for EL0.
2998 */
137feaa9 2999 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
3000 return CP_ACCESS_TRAP;
3001 }
3002 return CP_ACCESS_OK;
3003}
3004
3005static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3006{
3007 ARMCPU *cpu = arm_env_get_cpu(env);
3008 int dzp_bit = 1 << 4;
3009
3010 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 3011 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
3012 dzp_bit = 0;
3013 }
3014 return cpu->dcz_blocksize | dzp_bit;
3015}
3016
3f208fd7
PM
3017static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3018 bool isread)
f502cfc2 3019{
cdcf1405 3020 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
3021 /* Access to SP_EL0 is undefined if it's being used as
3022 * the stack pointer.
3023 */
3024 return CP_ACCESS_TRAP_UNCATEGORIZED;
3025 }
3026 return CP_ACCESS_OK;
3027}
3028
3029static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3030{
3031 return env->pstate & PSTATE_SP;
3032}
3033
3034static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3035{
3036 update_spsel(env, val);
3037}
3038
137feaa9
FA
3039static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3040 uint64_t value)
3041{
3042 ARMCPU *cpu = arm_env_get_cpu(env);
3043
3044 if (raw_read(env, ri) == value) {
3045 /* Skip the TLB flush if nothing actually changed; Linux likes
3046 * to do a lot of pointless SCTLR writes.
3047 */
3048 return;
3049 }
3050
3051 raw_write(env, ri, value);
3052 /* ??? Lots of these bits are not implemented. */
3053 /* This may enable/disable the MMU, so do a TLB flush. */
3054 tlb_flush(CPU(cpu), 1);
3055}
3056
3f208fd7
PM
3057static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3058 bool isread)
03fbf20f
PM
3059{
3060 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 3061 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
3062 }
3063 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 3064 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
3065 }
3066 return CP_ACCESS_OK;
3067}
3068
a8d64e73
PM
3069static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3070 uint64_t value)
3071{
3072 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3073}
3074
b0d2b7d0
PM
3075static const ARMCPRegInfo v8_cp_reginfo[] = {
3076 /* Minimal set of EL0-visible registers. This will need to be expanded
3077 * significantly for system emulation of AArch64 CPUs.
3078 */
3079 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3080 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3081 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
3082 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3083 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 3084 .type = ARM_CP_NO_RAW,
c2b820fe
PM
3085 .access = PL0_RW, .accessfn = aa64_daif_access,
3086 .fieldoffset = offsetof(CPUARMState, daif),
3087 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
3088 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3089 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3090 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3091 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3092 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3093 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
3094 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3095 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 3096 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
3097 .readfn = aa64_dczid_read },
3098 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3099 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3100 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3101#ifndef CONFIG_USER_ONLY
3102 /* Avoid overhead of an access check that always passes in user-mode */
3103 .accessfn = aa64_zva_access,
3104#endif
3105 },
0eef9d98
PM
3106 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3107 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3108 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
3109 /* Cache ops: all NOPs since we don't emulate caches */
3110 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3111 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3112 .access = PL1_W, .type = ARM_CP_NOP },
3113 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3114 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3115 .access = PL1_W, .type = ARM_CP_NOP },
3116 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3117 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3118 .access = PL0_W, .type = ARM_CP_NOP,
3119 .accessfn = aa64_cacheop_access },
3120 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3121 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3122 .access = PL1_W, .type = ARM_CP_NOP },
3123 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3124 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3125 .access = PL1_W, .type = ARM_CP_NOP },
3126 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3127 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3128 .access = PL0_W, .type = ARM_CP_NOP,
3129 .accessfn = aa64_cacheop_access },
3130 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3131 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3132 .access = PL1_W, .type = ARM_CP_NOP },
3133 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3134 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3135 .access = PL0_W, .type = ARM_CP_NOP,
3136 .accessfn = aa64_cacheop_access },
3137 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3138 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3139 .access = PL0_W, .type = ARM_CP_NOP,
3140 .accessfn = aa64_cacheop_access },
3141 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3142 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3143 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
3144 /* TLBI operations */
3145 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3146 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 3147 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3148 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3149 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3150 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 3151 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3152 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3153 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3154 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 3155 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3156 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 3157 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3158 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 3159 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3160 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3161 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3162 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3163 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3164 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3165 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 3166 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3167 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3168 .writefn = tlbi_aa64_vae1is_write },
168aa23b 3169 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3170 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 3171 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3172 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3173 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3174 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 3175 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3176 .writefn = tlbi_aa64_vae1_write },
168aa23b 3177 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3178 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 3179 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3180 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 3181 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3182 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 3183 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3184 .writefn = tlbi_aa64_vae1_write },
168aa23b 3185 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3186 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3187 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3188 .writefn = tlbi_aa64_vae1_write },
168aa23b 3189 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 3190 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3191 .access = PL1_W, .type = ARM_CP_NO_RAW,
fd3ed969 3192 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
3193 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3194 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3195 .access = PL2_W, .type = ARM_CP_NO_RAW,
3196 .writefn = tlbi_aa64_ipas2e1is_write },
3197 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3198 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3199 .access = PL2_W, .type = ARM_CP_NO_RAW,
3200 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
3201 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3202 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3203 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3204 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
3205 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3206 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3207 .access = PL2_W, .type = ARM_CP_NO_RAW,
3208 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
3209 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3210 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3211 .access = PL2_W, .type = ARM_CP_NO_RAW,
3212 .writefn = tlbi_aa64_ipas2e1_write },
3213 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3214 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3215 .access = PL2_W, .type = ARM_CP_NO_RAW,
3216 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
3217 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3218 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3219 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 3220 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
3221 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3222 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3223 .access = PL2_W, .type = ARM_CP_NO_RAW,
3224 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
3225#ifndef CONFIG_USER_ONLY
3226 /* 64 bit address translation operations */
3227 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3228 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
060e8a48 3229 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3230 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3231 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
060e8a48 3232 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3233 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3234 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
060e8a48 3235 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
19525524
PM
3236 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3237 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
060e8a48 3238 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2a47df95 3239 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 3240 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
2a47df95
PM
3241 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3242 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 3243 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
2a47df95
PM
3244 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3245 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 3246 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
2a47df95
PM
3247 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3248 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 3249 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
2a47df95
PM
3250 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3251 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3252 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3253 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3254 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3255 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3256 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3257 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
c96fc9b5
EI
3258 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3259 .type = ARM_CP_ALIAS,
3260 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3261 .access = PL1_RW, .resetvalue = 0,
3262 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3263 .writefn = par_write },
19525524 3264#endif
995939a6 3265 /* TLB invalidate last level of translation table walk */
9449fdf6 3266 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 3267 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 3268 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 3269 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 3270 .writefn = tlbimvaa_is_write },
9449fdf6 3271 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 3272 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 3273 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 3274 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
9449fdf6
PM
3275 /* 32 bit cache operations */
3276 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3277 .type = ARM_CP_NOP, .access = PL1_W },
3278 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3279 .type = ARM_CP_NOP, .access = PL1_W },
3280 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3281 .type = ARM_CP_NOP, .access = PL1_W },
3282 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3283 .type = ARM_CP_NOP, .access = PL1_W },
3284 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3285 .type = ARM_CP_NOP, .access = PL1_W },
3286 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3287 .type = ARM_CP_NOP, .access = PL1_W },
3288 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3289 .type = ARM_CP_NOP, .access = PL1_W },
3290 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3291 .type = ARM_CP_NOP, .access = PL1_W },
3292 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3293 .type = ARM_CP_NOP, .access = PL1_W },
3294 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3295 .type = ARM_CP_NOP, .access = PL1_W },
3296 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3297 .type = ARM_CP_NOP, .access = PL1_W },
3298 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3299 .type = ARM_CP_NOP, .access = PL1_W },
3300 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3301 .type = ARM_CP_NOP, .access = PL1_W },
3302 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
3303 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3304 .access = PL1_RW, .resetvalue = 0,
3305 .writefn = dacr_write, .raw_writefn = raw_write,
3306 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3307 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 3308 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3309 .type = ARM_CP_ALIAS,
a0618a19 3310 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
3311 .access = PL1_RW,
3312 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 3313 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 3314 .type = ARM_CP_ALIAS,
a65f1de9 3315 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3316 .access = PL1_RW,
3317 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
3318 /* We rely on the access checks not allowing the guest to write to the
3319 * state field when SPSel indicates that it's being used as the stack
3320 * pointer.
3321 */
3322 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3323 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3324 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 3325 .type = ARM_CP_ALIAS,
f502cfc2 3326 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
3327 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3328 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3329 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 3330 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
3331 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3332 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 3333 .type = ARM_CP_NO_RAW,
f502cfc2 3334 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
3335 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3336 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3337 .type = ARM_CP_ALIAS,
3338 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3339 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
3340 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3341 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3342 .access = PL2_RW, .resetvalue = 0,
3343 .writefn = dacr_write, .raw_writefn = raw_write,
3344 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3345 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3346 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3347 .access = PL2_RW, .resetvalue = 0,
3348 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3349 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3350 .type = ARM_CP_ALIAS,
3351 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3352 .access = PL2_RW,
3353 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3354 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3355 .type = ARM_CP_ALIAS,
3356 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3357 .access = PL2_RW,
3358 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3359 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3360 .type = ARM_CP_ALIAS,
3361 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3362 .access = PL2_RW,
3363 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3364 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3365 .type = ARM_CP_ALIAS,
3366 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3367 .access = PL2_RW,
3368 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
3369 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3370 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3371 .resetvalue = 0,
3372 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3373 { .name = "SDCR", .type = ARM_CP_ALIAS,
3374 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3375 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3376 .writefn = sdcr_write,
3377 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
3378 REGINFO_SENTINEL
3379};
3380
d42e3c26 3381/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 3382static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d42e3c26
EI
3383 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3384 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3385 .access = PL2_RW,
3386 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 3387 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3388 .type = ARM_CP_NO_RAW,
f149e3e8
EI
3389 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3390 .access = PL2_RW,
3391 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
c6f19164
GB
3392 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3393 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3394 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
3395 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3396 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3397 .access = PL2_RW, .type = ARM_CP_CONST,
3398 .resetvalue = 0 },
3399 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3400 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3401 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
3402 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3403 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3404 .access = PL2_RW, .type = ARM_CP_CONST,
3405 .resetvalue = 0 },
3406 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3407 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3408 .access = PL2_RW, .type = ARM_CP_CONST,
3409 .resetvalue = 0 },
37cd6c24
PM
3410 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3411 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3412 .access = PL2_RW, .type = ARM_CP_CONST,
3413 .resetvalue = 0 },
3414 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3415 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3416 .access = PL2_RW, .type = ARM_CP_CONST,
3417 .resetvalue = 0 },
06ec4c8c
EI
3418 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3419 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3420 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
3421 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3422 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3423 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3424 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
3425 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3426 .cp = 15, .opc1 = 6, .crm = 2,
3427 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3428 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3429 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3430 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3431 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
3432 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3433 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3434 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
3435 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3436 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3437 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
3438 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3439 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3440 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3441 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3442 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3443 .resetvalue = 0 },
0b6440af
EI
3444 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3445 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3446 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
3447 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3448 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3449 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3450 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3451 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3452 .resetvalue = 0 },
b0e66d95
EI
3453 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3454 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3455 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3456 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3457 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3458 .resetvalue = 0 },
3459 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3460 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3461 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3462 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3463 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3464 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
3465 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3466 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
3467 .access = PL2_RW, .accessfn = access_tda,
3468 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
3469 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3470 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3471 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3472 .type = ARM_CP_CONST, .resetvalue = 0 },
d42e3c26
EI
3473 REGINFO_SENTINEL
3474};
3475
f149e3e8
EI
3476static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3477{
3478 ARMCPU *cpu = arm_env_get_cpu(env);
3479 uint64_t valid_mask = HCR_MASK;
3480
3481 if (arm_feature(env, ARM_FEATURE_EL3)) {
3482 valid_mask &= ~HCR_HCD;
3483 } else {
3484 valid_mask &= ~HCR_TSC;
3485 }
3486
3487 /* Clear RES0 bits. */
3488 value &= valid_mask;
3489
3490 /* These bits change the MMU setup:
3491 * HCR_VM enables stage 2 translation
3492 * HCR_PTW forbids certain page-table setups
3493 * HCR_DC Disables stage1 and enables stage2 translation
3494 */
3495 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3496 tlb_flush(CPU(cpu), 1);
3497 }
3498 raw_write(env, ri, value);
3499}
3500
4771cd01 3501static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8
EI
3502 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3503 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3504 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3505 .writefn = hcr_write },
3b685ba7 3506 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3507 .type = ARM_CP_ALIAS,
3b685ba7
EI
3508 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3509 .access = PL2_RW,
3510 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 3511 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3512 .type = ARM_CP_ALIAS,
f2c30f42
EI
3513 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3514 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
63b60551
EI
3515 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3516 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3517 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 3518 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 3519 .type = ARM_CP_ALIAS,
3b685ba7 3520 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3521 .access = PL2_RW,
3522 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d42e3c26
EI
3523 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3524 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3525 .access = PL2_RW, .writefn = vbar_write,
3526 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3527 .resetvalue = 0 },
884b4dee
GB
3528 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3529 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 3530 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 3531 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
3532 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3533 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3534 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3535 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
95f949ac
EI
3536 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3537 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3538 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3539 .resetvalue = 0 },
3540 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3541 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3542 .access = PL2_RW, .type = ARM_CP_ALIAS,
3543 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
3544 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3545 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3546 .access = PL2_RW, .type = ARM_CP_CONST,
3547 .resetvalue = 0 },
3548 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3549 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3550 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3551 .access = PL2_RW, .type = ARM_CP_CONST,
3552 .resetvalue = 0 },
37cd6c24
PM
3553 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3554 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3555 .access = PL2_RW, .type = ARM_CP_CONST,
3556 .resetvalue = 0 },
3557 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3558 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3559 .access = PL2_RW, .type = ARM_CP_CONST,
3560 .resetvalue = 0 },
06ec4c8c
EI
3561 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3562 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3563 .access = PL2_RW, .writefn = vmsa_tcr_el1_write,
3564 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3565 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
3566 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3567 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3568 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3569 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3570 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3571 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3572 .access = PL2_RW, .type = ARM_CP_ALIAS,
3573 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
3574 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3575 .cp = 15, .opc1 = 6, .crm = 2,
3576 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3577 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3578 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3579 .writefn = vttbr_write },
3580 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3581 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3582 .access = PL2_RW, .writefn = vttbr_write,
3583 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
3584 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3585 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3586 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3587 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
3588 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3589 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3590 .access = PL2_RW, .resetvalue = 0,
3591 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
3592 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3593 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3594 .access = PL2_RW, .resetvalue = 0,
3595 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3596 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3597 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 3598 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
51da9014
EI
3599 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3600 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3601 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3602 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
3603 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3604 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3605 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3606 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
3607 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3608 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3609 .access = PL2_W, .type = ARM_CP_NO_RAW,
3610 .writefn = tlbi_aa64_vae2_write },
3611 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3612 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3613 .access = PL2_W, .type = ARM_CP_NO_RAW,
3614 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
3615 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3616 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3617 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 3618 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
3619 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3620 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3621 .access = PL2_W, .type = ARM_CP_NO_RAW,
3622 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 3623#ifndef CONFIG_USER_ONLY
2a47df95
PM
3624 /* Unlike the other EL2-related AT operations, these must
3625 * UNDEF from EL3 if EL2 is not implemented, which is why we
3626 * define them here rather than with the rest of the AT ops.
3627 */
3628 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3629 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3630 .access = PL2_W, .accessfn = at_s1e2_access,
3631 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3632 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3633 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3634 .access = PL2_W, .accessfn = at_s1e2_access,
3635 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
14db7fe0
PM
3636 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3637 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3638 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3639 * to behave as if SCR.NS was 1.
3640 */
3641 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3642 .access = PL2_W,
3643 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3644 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3645 .access = PL2_W,
3646 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
0b6440af
EI
3647 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3648 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3649 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3650 * reset values as IMPDEF. We choose to reset to 3 to comply with
3651 * both ARMv7 and ARMv8.
3652 */
3653 .access = PL2_RW, .resetvalue = 3,
3654 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
3655 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3656 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3657 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3658 .writefn = gt_cntvoff_write,
3659 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3660 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3661 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3662 .writefn = gt_cntvoff_write,
3663 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
3664 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3665 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3666 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3667 .type = ARM_CP_IO, .access = PL2_RW,
3668 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3669 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3670 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3671 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3672 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3673 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3674 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 3675 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
3676 .resetfn = gt_hyp_timer_reset,
3677 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3678 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3679 .type = ARM_CP_IO,
3680 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3681 .access = PL2_RW,
3682 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3683 .resetvalue = 0,
3684 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 3685#endif
14cc7b54
SF
3686 /* The only field of MDCR_EL2 that has a defined architectural reset value
3687 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3688 * don't impelment any PMU event counters, so using zero as a reset
3689 * value for MDCR_EL2 is okay
3690 */
3691 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3692 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3693 .access = PL2_RW, .resetvalue = 0,
3694 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
3695 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3696 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3697 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3698 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3699 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3700 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3701 .access = PL2_RW,
3702 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3b685ba7
EI
3703 REGINFO_SENTINEL
3704};
3705
2f027fc5
PM
3706static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
3707 bool isread)
3708{
3709 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
3710 * At Secure EL1 it traps to EL3.
3711 */
3712 if (arm_current_el(env) == 3) {
3713 return CP_ACCESS_OK;
3714 }
3715 if (arm_is_secure_below_el3(env)) {
3716 return CP_ACCESS_TRAP_EL3;
3717 }
3718 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
3719 if (isread) {
3720 return CP_ACCESS_OK;
3721 }
3722 return CP_ACCESS_TRAP_UNCATEGORIZED;
3723}
3724
60fb1a87
GB
3725static const ARMCPRegInfo el3_cp_reginfo[] = {
3726 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3727 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3728 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3729 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 3730 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87 3731 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
3732 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3733 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 3734 .writefn = scr_write },
60fb1a87
GB
3735 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3736 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3737 .access = PL3_RW, .resetvalue = 0,
3738 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3739 { .name = "SDER",
3740 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3741 .access = PL3_RW, .resetvalue = 0,
3742 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 3743 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
3744 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3745 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 3746 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
137feaa9 3747 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
e46e1a74 3748 .type = ARM_CP_ALIAS, /* reset handled by AArch32 view */
137feaa9
FA
3749 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
3750 .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3751 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) },
7dd8c9af
FA
3752 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3753 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3754 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3755 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
3756 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3757 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
3758 .access = PL3_RW, .writefn = vmsa_tcr_el1_write,
3759 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3760 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 3761 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3762 .type = ARM_CP_ALIAS,
81547d66
EI
3763 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3764 .access = PL3_RW,
3765 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 3766 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3767 .type = ARM_CP_ALIAS,
f2c30f42
EI
3768 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3769 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
3770 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3771 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3772 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 3773 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 3774 .type = ARM_CP_ALIAS,
81547d66 3775 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
3776 .access = PL3_RW,
3777 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
3778 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3779 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3780 .access = PL3_RW, .writefn = vbar_write,
3781 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3782 .resetvalue = 0 },
c6f19164
GB
3783 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3784 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3785 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3786 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
3787 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3788 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3789 .access = PL3_RW, .resetvalue = 0,
3790 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
3791 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3792 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3793 .access = PL3_RW, .type = ARM_CP_CONST,
3794 .resetvalue = 0 },
37cd6c24
PM
3795 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3796 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3797 .access = PL3_RW, .type = ARM_CP_CONST,
3798 .resetvalue = 0 },
3799 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3800 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3801 .access = PL3_RW, .type = ARM_CP_CONST,
3802 .resetvalue = 0 },
43efaa33
PM
3803 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3804 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3805 .access = PL3_W, .type = ARM_CP_NO_RAW,
3806 .writefn = tlbi_aa64_alle3is_write },
3807 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3808 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3809 .access = PL3_W, .type = ARM_CP_NO_RAW,
3810 .writefn = tlbi_aa64_vae3is_write },
3811 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3812 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3813 .access = PL3_W, .type = ARM_CP_NO_RAW,
3814 .writefn = tlbi_aa64_vae3is_write },
3815 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3816 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3817 .access = PL3_W, .type = ARM_CP_NO_RAW,
3818 .writefn = tlbi_aa64_alle3_write },
3819 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3820 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3821 .access = PL3_W, .type = ARM_CP_NO_RAW,
3822 .writefn = tlbi_aa64_vae3_write },
3823 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3824 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3825 .access = PL3_W, .type = ARM_CP_NO_RAW,
3826 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
3827 REGINFO_SENTINEL
3828};
3829
3f208fd7
PM
3830static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3831 bool isread)
7da845b0
PM
3832{
3833 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3834 * but the AArch32 CTR has its own reginfo struct)
3835 */
137feaa9 3836 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
3837 return CP_ACCESS_TRAP;
3838 }
3839 return CP_ACCESS_OK;
3840}
3841
1424ca8d
DM
3842static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3843 uint64_t value)
3844{
3845 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
3846 * read via a bit in OSLSR_EL1.
3847 */
3848 int oslock;
3849
3850 if (ri->state == ARM_CP_STATE_AA32) {
3851 oslock = (value == 0xC5ACCE55);
3852 } else {
3853 oslock = value & 1;
3854 }
3855
3856 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
3857}
3858
50300698 3859static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 3860 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
3861 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3862 * unlike DBGDRAR it is never accessible from EL0.
3863 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3864 * accessor.
50300698
PM
3865 */
3866 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
3867 .access = PL0_R, .accessfn = access_tdra,
3868 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
3869 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3870 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
3871 .access = PL1_R, .accessfn = access_tdra,
3872 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 3873 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
3874 .access = PL0_R, .accessfn = access_tdra,
3875 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 3876 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
3877 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3878 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 3879 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
3880 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3881 .resetvalue = 0 },
5e8b12ff
PM
3882 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3883 * We don't implement the configurable EL0 access.
3884 */
3885 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3886 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 3887 .type = ARM_CP_ALIAS,
d6c8cf81 3888 .access = PL1_R, .accessfn = access_tda,
b061a82b 3889 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
3890 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3891 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 3892 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 3893 .accessfn = access_tdosa,
1424ca8d
DM
3894 .writefn = oslar_write },
3895 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
3896 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
3897 .access = PL1_R, .resetvalue = 10,
187f678d 3898 .accessfn = access_tdosa,
1424ca8d 3899 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
3900 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3901 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3902 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
3903 .access = PL1_RW, .accessfn = access_tdosa,
3904 .type = ARM_CP_NOP },
5e8b12ff
PM
3905 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3906 * implement vector catch debug events yet.
3907 */
3908 { .name = "DBGVCR",
3909 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
3910 .access = PL1_RW, .accessfn = access_tda,
3911 .type = ARM_CP_NOP },
50300698
PM
3912 REGINFO_SENTINEL
3913};
3914
3915static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3916 /* 64 bit access versions of the (dummy) debug registers */
3917 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3918 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3919 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3920 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3921 REGINFO_SENTINEL
3922};
3923
9ee98ce8
PM
3924void hw_watchpoint_update(ARMCPU *cpu, int n)
3925{
3926 CPUARMState *env = &cpu->env;
3927 vaddr len = 0;
3928 vaddr wvr = env->cp15.dbgwvr[n];
3929 uint64_t wcr = env->cp15.dbgwcr[n];
3930 int mask;
3931 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3932
3933 if (env->cpu_watchpoint[n]) {
3934 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3935 env->cpu_watchpoint[n] = NULL;
3936 }
3937
3938 if (!extract64(wcr, 0, 1)) {
3939 /* E bit clear : watchpoint disabled */
3940 return;
3941 }
3942
3943 switch (extract64(wcr, 3, 2)) {
3944 case 0:
3945 /* LSC 00 is reserved and must behave as if the wp is disabled */
3946 return;
3947 case 1:
3948 flags |= BP_MEM_READ;
3949 break;
3950 case 2:
3951 flags |= BP_MEM_WRITE;
3952 break;
3953 case 3:
3954 flags |= BP_MEM_ACCESS;
3955 break;
3956 }
3957
3958 /* Attempts to use both MASK and BAS fields simultaneously are
3959 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3960 * thus generating a watchpoint for every byte in the masked region.
3961 */
3962 mask = extract64(wcr, 24, 4);
3963 if (mask == 1 || mask == 2) {
3964 /* Reserved values of MASK; we must act as if the mask value was
3965 * some non-reserved value, or as if the watchpoint were disabled.
3966 * We choose the latter.
3967 */
3968 return;
3969 } else if (mask) {
3970 /* Watchpoint covers an aligned area up to 2GB in size */
3971 len = 1ULL << mask;
3972 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3973 * whether the watchpoint fires when the unmasked bits match; we opt
3974 * to generate the exceptions.
3975 */
3976 wvr &= ~(len - 1);
3977 } else {
3978 /* Watchpoint covers bytes defined by the byte address select bits */
3979 int bas = extract64(wcr, 5, 8);
3980 int basstart;
3981
3982 if (bas == 0) {
3983 /* This must act as if the watchpoint is disabled */
3984 return;
3985 }
3986
3987 if (extract64(wvr, 2, 1)) {
3988 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3989 * ignored, and BAS[3:0] define which bytes to watch.
3990 */
3991 bas &= 0xf;
3992 }
3993 /* The BAS bits are supposed to be programmed to indicate a contiguous
3994 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3995 * we fire for each byte in the word/doubleword addressed by the WVR.
3996 * We choose to ignore any non-zero bits after the first range of 1s.
3997 */
3998 basstart = ctz32(bas);
3999 len = cto32(bas >> basstart);
4000 wvr += basstart;
4001 }
4002
4003 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4004 &env->cpu_watchpoint[n]);
4005}
4006
4007void hw_watchpoint_update_all(ARMCPU *cpu)
4008{
4009 int i;
4010 CPUARMState *env = &cpu->env;
4011
4012 /* Completely clear out existing QEMU watchpoints and our array, to
4013 * avoid possible stale entries following migration load.
4014 */
4015 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4016 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4017
4018 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4019 hw_watchpoint_update(cpu, i);
4020 }
4021}
4022
4023static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4024 uint64_t value)
4025{
4026 ARMCPU *cpu = arm_env_get_cpu(env);
4027 int i = ri->crm;
4028
4029 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4030 * register reads and behaves as if values written are sign extended.
4031 * Bits [1:0] are RES0.
4032 */
4033 value = sextract64(value, 0, 49) & ~3ULL;
4034
4035 raw_write(env, ri, value);
4036 hw_watchpoint_update(cpu, i);
4037}
4038
4039static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4040 uint64_t value)
4041{
4042 ARMCPU *cpu = arm_env_get_cpu(env);
4043 int i = ri->crm;
4044
4045 raw_write(env, ri, value);
4046 hw_watchpoint_update(cpu, i);
4047}
4048
46747d15
PM
4049void hw_breakpoint_update(ARMCPU *cpu, int n)
4050{
4051 CPUARMState *env = &cpu->env;
4052 uint64_t bvr = env->cp15.dbgbvr[n];
4053 uint64_t bcr = env->cp15.dbgbcr[n];
4054 vaddr addr;
4055 int bt;
4056 int flags = BP_CPU;
4057
4058 if (env->cpu_breakpoint[n]) {
4059 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4060 env->cpu_breakpoint[n] = NULL;
4061 }
4062
4063 if (!extract64(bcr, 0, 1)) {
4064 /* E bit clear : watchpoint disabled */
4065 return;
4066 }
4067
4068 bt = extract64(bcr, 20, 4);
4069
4070 switch (bt) {
4071 case 4: /* unlinked address mismatch (reserved if AArch64) */
4072 case 5: /* linked address mismatch (reserved if AArch64) */
4073 qemu_log_mask(LOG_UNIMP,
4074 "arm: address mismatch breakpoint types not implemented");
4075 return;
4076 case 0: /* unlinked address match */
4077 case 1: /* linked address match */
4078 {
4079 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4080 * we behave as if the register was sign extended. Bits [1:0] are
4081 * RES0. The BAS field is used to allow setting breakpoints on 16
4082 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4083 * a bp will fire if the addresses covered by the bp and the addresses
4084 * covered by the insn overlap but the insn doesn't start at the
4085 * start of the bp address range. We choose to require the insn and
4086 * the bp to have the same address. The constraints on writing to
4087 * BAS enforced in dbgbcr_write mean we have only four cases:
4088 * 0b0000 => no breakpoint
4089 * 0b0011 => breakpoint on addr
4090 * 0b1100 => breakpoint on addr + 2
4091 * 0b1111 => breakpoint on addr
4092 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4093 */
4094 int bas = extract64(bcr, 5, 4);
4095 addr = sextract64(bvr, 0, 49) & ~3ULL;
4096 if (bas == 0) {
4097 return;
4098 }
4099 if (bas == 0xc) {
4100 addr += 2;
4101 }
4102 break;
4103 }
4104 case 2: /* unlinked context ID match */
4105 case 8: /* unlinked VMID match (reserved if no EL2) */
4106 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4107 qemu_log_mask(LOG_UNIMP,
4108 "arm: unlinked context breakpoint types not implemented");
4109 return;
4110 case 9: /* linked VMID match (reserved if no EL2) */
4111 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4112 case 3: /* linked context ID match */
4113 default:
4114 /* We must generate no events for Linked context matches (unless
4115 * they are linked to by some other bp/wp, which is handled in
4116 * updates for the linking bp/wp). We choose to also generate no events
4117 * for reserved values.
4118 */
4119 return;
4120 }
4121
4122 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4123}
4124
4125void hw_breakpoint_update_all(ARMCPU *cpu)
4126{
4127 int i;
4128 CPUARMState *env = &cpu->env;
4129
4130 /* Completely clear out existing QEMU breakpoints and our array, to
4131 * avoid possible stale entries following migration load.
4132 */
4133 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4134 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4135
4136 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4137 hw_breakpoint_update(cpu, i);
4138 }
4139}
4140
4141static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4142 uint64_t value)
4143{
4144 ARMCPU *cpu = arm_env_get_cpu(env);
4145 int i = ri->crm;
4146
4147 raw_write(env, ri, value);
4148 hw_breakpoint_update(cpu, i);
4149}
4150
4151static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4152 uint64_t value)
4153{
4154 ARMCPU *cpu = arm_env_get_cpu(env);
4155 int i = ri->crm;
4156
4157 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4158 * copy of BAS[0].
4159 */
4160 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4161 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4162
4163 raw_write(env, ri, value);
4164 hw_breakpoint_update(cpu, i);
4165}
4166
50300698 4167static void define_debug_regs(ARMCPU *cpu)
0b45451e 4168{
50300698
PM
4169 /* Define v7 and v8 architectural debug registers.
4170 * These are just dummy implementations for now.
0b45451e
PM
4171 */
4172 int i;
3ff6fc91 4173 int wrps, brps, ctx_cmps;
48eb3ae6
PM
4174 ARMCPRegInfo dbgdidr = {
4175 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
d6c8cf81
PM
4176 .access = PL0_R, .accessfn = access_tda,
4177 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
48eb3ae6
PM
4178 };
4179
3ff6fc91 4180 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
4181 brps = extract32(cpu->dbgdidr, 24, 4);
4182 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
4183 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4184
4185 assert(ctx_cmps <= brps);
48eb3ae6
PM
4186
4187 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4188 * of the debug registers such as number of breakpoints;
4189 * check that if they both exist then they agree.
4190 */
4191 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4192 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4193 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 4194 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 4195 }
0b45451e 4196
48eb3ae6 4197 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
4198 define_arm_cp_regs(cpu, debug_cp_reginfo);
4199
4200 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4201 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4202 }
4203
48eb3ae6 4204 for (i = 0; i < brps + 1; i++) {
0b45451e 4205 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4206 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4207 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 4208 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4209 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4210 .writefn = dbgbvr_write, .raw_writefn = raw_write
4211 },
10aae104
PM
4212 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4213 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 4214 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
4215 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4216 .writefn = dbgbcr_write, .raw_writefn = raw_write
4217 },
48eb3ae6
PM
4218 REGINFO_SENTINEL
4219 };
4220 define_arm_cp_regs(cpu, dbgregs);
4221 }
4222
4223 for (i = 0; i < wrps + 1; i++) {
4224 ARMCPRegInfo dbgregs[] = {
10aae104
PM
4225 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4226 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 4227 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4228 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4229 .writefn = dbgwvr_write, .raw_writefn = raw_write
4230 },
10aae104
PM
4231 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4232 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 4233 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
4234 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4235 .writefn = dbgwcr_write, .raw_writefn = raw_write
4236 },
4237 REGINFO_SENTINEL
0b45451e
PM
4238 };
4239 define_arm_cp_regs(cpu, dbgregs);
4240 }
4241}
4242
2ceb98c0
PM
4243void register_cp_regs_for_features(ARMCPU *cpu)
4244{
4245 /* Register all the coprocessor registers based on feature bits */
4246 CPUARMState *env = &cpu->env;
4247 if (arm_feature(env, ARM_FEATURE_M)) {
4248 /* M profile has no coprocessor registers */
4249 return;
4250 }
4251
e9aa6c21 4252 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
4253 if (!arm_feature(env, ARM_FEATURE_V8)) {
4254 /* Must go early as it is full of wildcards that may be
4255 * overridden by later definitions.
4256 */
4257 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4258 }
4259
7d57f408 4260 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
4261 /* The ID registers all have impdef reset values */
4262 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
4263 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4264 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4265 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4266 .resetvalue = cpu->id_pfr0 },
0ff644a7
PM
4267 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4268 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4269 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4270 .resetvalue = cpu->id_pfr1 },
0ff644a7
PM
4271 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4272 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4273 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4274 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
4275 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4276 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4277 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4278 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
4279 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4280 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4281 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4282 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
4283 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4284 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4285 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4286 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
4287 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4288 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4289 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4290 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
4291 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4292 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4293 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4294 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
4295 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4296 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4297 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4298 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
4299 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4300 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4301 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4302 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
4303 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4304 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4305 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4306 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
4307 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4308 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4309 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4310 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
4311 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4312 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4313 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4314 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
4315 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4316 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4317 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 4318 .resetvalue = cpu->id_isar5 },
e20d84c1
PM
4319 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4320 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4321 .access = PL1_R, .type = ARM_CP_CONST,
4322 .resetvalue = cpu->id_mmfr4 },
4323 /* 7 is as yet unallocated and must RAZ */
4324 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4325 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4326 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
4327 .resetvalue = 0 },
4328 REGINFO_SENTINEL
4329 };
4330 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
4331 define_arm_cp_regs(cpu, v6_cp_reginfo);
4332 } else {
4333 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4334 }
4d31c596
PM
4335 if (arm_feature(env, ARM_FEATURE_V6K)) {
4336 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4337 }
5e5cf9e3
PC
4338 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4339 !arm_feature(env, ARM_FEATURE_MPU)) {
995939a6
PM
4340 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4341 }
e9aa6c21 4342 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 4343 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
4344 * field as main ID register, and we implement only the cycle
4345 * count register.
200ac0ef 4346 */
7c2cb42b 4347#ifndef CONFIG_USER_ONLY
200ac0ef
PM
4348 ARMCPRegInfo pmcr = {
4349 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 4350 .access = PL0_RW,
7a0e58fa 4351 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 4352 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
4353 .accessfn = pmreg_access, .writefn = pmcr_write,
4354 .raw_writefn = raw_write,
200ac0ef 4355 };
8521466b
AF
4356 ARMCPRegInfo pmcr64 = {
4357 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4358 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4359 .access = PL0_RW, .accessfn = pmreg_access,
4360 .type = ARM_CP_IO,
4361 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4362 .resetvalue = cpu->midr & 0xff000000,
4363 .writefn = pmcr_write, .raw_writefn = raw_write,
4364 };
7c2cb42b 4365 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 4366 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 4367#endif
776d4e5c 4368 ARMCPRegInfo clidr = {
7da845b0
PM
4369 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4370 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
4371 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4372 };
776d4e5c 4373 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 4374 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 4375 define_debug_regs(cpu);
7d57f408
PM
4376 } else {
4377 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 4378 }
b0d2b7d0 4379 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
4380 /* AArch64 ID registers, which all have impdef reset values.
4381 * Note that within the ID register ranges the unused slots
4382 * must all RAZ, not UNDEF; future architecture versions may
4383 * define new registers here.
4384 */
e60cef86
PM
4385 ARMCPRegInfo v8_idregs[] = {
4386 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4387 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4388 .access = PL1_R, .type = ARM_CP_CONST,
4389 .resetvalue = cpu->id_aa64pfr0 },
4390 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4391 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4392 .access = PL1_R, .type = ARM_CP_CONST,
4393 .resetvalue = cpu->id_aa64pfr1},
e20d84c1
PM
4394 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4395 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4396 .access = PL1_R, .type = ARM_CP_CONST,
4397 .resetvalue = 0 },
4398 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4399 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4400 .access = PL1_R, .type = ARM_CP_CONST,
4401 .resetvalue = 0 },
4402 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4403 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4404 .access = PL1_R, .type = ARM_CP_CONST,
4405 .resetvalue = 0 },
4406 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4407 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4408 .access = PL1_R, .type = ARM_CP_CONST,
4409 .resetvalue = 0 },
4410 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4411 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4412 .access = PL1_R, .type = ARM_CP_CONST,
4413 .resetvalue = 0 },
4414 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4415 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4416 .access = PL1_R, .type = ARM_CP_CONST,
4417 .resetvalue = 0 },
e60cef86
PM
4418 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4419 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4420 .access = PL1_R, .type = ARM_CP_CONST,
5d831be2 4421 /* We mask out the PMUVer field, because we don't currently
9225d739
PM
4422 * implement the PMU. Not advertising it prevents the guest
4423 * from trying to use it and getting UNDEFs on registers we
4424 * don't implement.
4425 */
4426 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
e60cef86
PM
4427 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4428 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4429 .access = PL1_R, .type = ARM_CP_CONST,
4430 .resetvalue = cpu->id_aa64dfr1 },
e20d84c1
PM
4431 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4432 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4433 .access = PL1_R, .type = ARM_CP_CONST,
4434 .resetvalue = 0 },
4435 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4436 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4437 .access = PL1_R, .type = ARM_CP_CONST,
4438 .resetvalue = 0 },
e60cef86
PM
4439 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4440 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4441 .access = PL1_R, .type = ARM_CP_CONST,
4442 .resetvalue = cpu->id_aa64afr0 },
4443 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4444 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4445 .access = PL1_R, .type = ARM_CP_CONST,
4446 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
4447 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4448 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4449 .access = PL1_R, .type = ARM_CP_CONST,
4450 .resetvalue = 0 },
4451 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4452 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4453 .access = PL1_R, .type = ARM_CP_CONST,
4454 .resetvalue = 0 },
e60cef86
PM
4455 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4456 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4457 .access = PL1_R, .type = ARM_CP_CONST,
4458 .resetvalue = cpu->id_aa64isar0 },
4459 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4460 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4461 .access = PL1_R, .type = ARM_CP_CONST,
4462 .resetvalue = cpu->id_aa64isar1 },
e20d84c1
PM
4463 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4464 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4465 .access = PL1_R, .type = ARM_CP_CONST,
4466 .resetvalue = 0 },
4467 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4468 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4469 .access = PL1_R, .type = ARM_CP_CONST,
4470 .resetvalue = 0 },
4471 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4472 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4473 .access = PL1_R, .type = ARM_CP_CONST,
4474 .resetvalue = 0 },
4475 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4476 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4477 .access = PL1_R, .type = ARM_CP_CONST,
4478 .resetvalue = 0 },
4479 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4480 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4481 .access = PL1_R, .type = ARM_CP_CONST,
4482 .resetvalue = 0 },
4483 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4484 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4485 .access = PL1_R, .type = ARM_CP_CONST,
4486 .resetvalue = 0 },
e60cef86
PM
4487 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4488 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4489 .access = PL1_R, .type = ARM_CP_CONST,
4490 .resetvalue = cpu->id_aa64mmfr0 },
4491 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4492 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4493 .access = PL1_R, .type = ARM_CP_CONST,
4494 .resetvalue = cpu->id_aa64mmfr1 },
e20d84c1
PM
4495 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4496 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4497 .access = PL1_R, .type = ARM_CP_CONST,
4498 .resetvalue = 0 },
4499 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4500 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4501 .access = PL1_R, .type = ARM_CP_CONST,
4502 .resetvalue = 0 },
4503 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4504 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4505 .access = PL1_R, .type = ARM_CP_CONST,
4506 .resetvalue = 0 },
4507 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4508 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4509 .access = PL1_R, .type = ARM_CP_CONST,
4510 .resetvalue = 0 },
4511 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4512 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4513 .access = PL1_R, .type = ARM_CP_CONST,
4514 .resetvalue = 0 },
4515 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4516 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4517 .access = PL1_R, .type = ARM_CP_CONST,
4518 .resetvalue = 0 },
a50c0f51
PM
4519 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4520 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4521 .access = PL1_R, .type = ARM_CP_CONST,
4522 .resetvalue = cpu->mvfr0 },
4523 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4524 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4525 .access = PL1_R, .type = ARM_CP_CONST,
4526 .resetvalue = cpu->mvfr1 },
4527 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4528 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4529 .access = PL1_R, .type = ARM_CP_CONST,
4530 .resetvalue = cpu->mvfr2 },
e20d84c1
PM
4531 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4532 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4533 .access = PL1_R, .type = ARM_CP_CONST,
4534 .resetvalue = 0 },
4535 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4536 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4537 .access = PL1_R, .type = ARM_CP_CONST,
4538 .resetvalue = 0 },
4539 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4540 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4541 .access = PL1_R, .type = ARM_CP_CONST,
4542 .resetvalue = 0 },
4543 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4544 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4545 .access = PL1_R, .type = ARM_CP_CONST,
4546 .resetvalue = 0 },
4547 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4548 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4549 .access = PL1_R, .type = ARM_CP_CONST,
4550 .resetvalue = 0 },
4054bfa9
AF
4551 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4552 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4553 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4554 .resetvalue = cpu->pmceid0 },
4555 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4556 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4557 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4558 .resetvalue = cpu->pmceid0 },
4559 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4560 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4561 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4562 .resetvalue = cpu->pmceid1 },
4563 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4564 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4565 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4566 .resetvalue = cpu->pmceid1 },
e60cef86
PM
4567 REGINFO_SENTINEL
4568 };
be8e8128
GB
4569 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4570 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4571 !arm_feature(env, ARM_FEATURE_EL2)) {
4572 ARMCPRegInfo rvbar = {
4573 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4574 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4575 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4576 };
4577 define_one_arm_cp_reg(cpu, &rvbar);
4578 }
e60cef86 4579 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
4580 define_arm_cp_regs(cpu, v8_cp_reginfo);
4581 }
3b685ba7 4582 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 4583 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
4584 ARMCPRegInfo vpidr_regs[] = {
4585 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4586 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4587 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4588 .resetvalue = cpu->midr,
4589 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4590 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4591 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4592 .access = PL2_RW, .resetvalue = cpu->midr,
4593 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4594 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4595 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4596 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4597 .resetvalue = vmpidr_def,
4598 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4599 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4600 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4601 .access = PL2_RW,
4602 .resetvalue = vmpidr_def,
4603 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
4604 REGINFO_SENTINEL
4605 };
4606 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4607 define_arm_cp_regs(cpu, el2_cp_reginfo);
be8e8128
GB
4608 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4609 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4610 ARMCPRegInfo rvbar = {
4611 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4612 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4613 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4614 };
4615 define_one_arm_cp_reg(cpu, &rvbar);
4616 }
d42e3c26
EI
4617 } else {
4618 /* If EL2 is missing but higher ELs are enabled, we need to
4619 * register the no_el2 reginfos.
4620 */
4621 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
4622 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4623 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
4624 */
4625 ARMCPRegInfo vpidr_regs[] = {
4626 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4627 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4628 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4629 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4630 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
4631 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4632 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4633 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4634 .type = ARM_CP_NO_RAW,
4635 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
4636 REGINFO_SENTINEL
4637 };
4638 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 4639 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
d42e3c26 4640 }
3b685ba7 4641 }
81547d66 4642 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 4643 define_arm_cp_regs(cpu, el3_cp_reginfo);
be8e8128
GB
4644 ARMCPRegInfo rvbar = {
4645 .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4646 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4647 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar
4648 };
4649 define_one_arm_cp_reg(cpu, &rvbar);
81547d66 4650 }
2f027fc5
PM
4651 /* The behaviour of NSACR is sufficiently various that we don't
4652 * try to describe it in a single reginfo:
4653 * if EL3 is 64 bit, then trap to EL3 from S EL1,
4654 * reads as constant 0xc00 from NS EL1 and NS EL2
4655 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
4656 * if v7 without EL3, register doesn't exist
4657 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
4658 */
4659 if (arm_feature(env, ARM_FEATURE_EL3)) {
4660 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4661 ARMCPRegInfo nsacr = {
4662 .name = "NSACR", .type = ARM_CP_CONST,
4663 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4664 .access = PL1_RW, .accessfn = nsacr_access,
4665 .resetvalue = 0xc00
4666 };
4667 define_one_arm_cp_reg(cpu, &nsacr);
4668 } else {
4669 ARMCPRegInfo nsacr = {
4670 .name = "NSACR",
4671 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4672 .access = PL3_RW | PL1_R,
4673 .resetvalue = 0,
4674 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
4675 };
4676 define_one_arm_cp_reg(cpu, &nsacr);
4677 }
4678 } else {
4679 if (arm_feature(env, ARM_FEATURE_V8)) {
4680 ARMCPRegInfo nsacr = {
4681 .name = "NSACR", .type = ARM_CP_CONST,
4682 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4683 .access = PL1_R,
4684 .resetvalue = 0xc00
4685 };
4686 define_one_arm_cp_reg(cpu, &nsacr);
4687 }
4688 }
4689
18032bec 4690 if (arm_feature(env, ARM_FEATURE_MPU)) {
6cb0b013
PC
4691 if (arm_feature(env, ARM_FEATURE_V6)) {
4692 /* PMSAv6 not implemented */
4693 assert(arm_feature(env, ARM_FEATURE_V7));
4694 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4695 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4696 } else {
4697 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4698 }
18032bec 4699 } else {
8e5d75c9 4700 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec
PM
4701 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4702 }
c326b979
PM
4703 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4704 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4705 }
6cc7a3ae
PM
4706 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4707 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4708 }
4a501606
PM
4709 if (arm_feature(env, ARM_FEATURE_VAPA)) {
4710 define_arm_cp_regs(cpu, vapa_cp_reginfo);
4711 }
c4804214
PM
4712 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4713 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4714 }
4715 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4716 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4717 }
4718 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4719 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4720 }
18032bec
PM
4721 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4722 define_arm_cp_regs(cpu, omap_cp_reginfo);
4723 }
34f90529
PM
4724 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4725 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4726 }
1047b9d7
PM
4727 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4728 define_arm_cp_regs(cpu, xscale_cp_reginfo);
4729 }
4730 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4731 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4732 }
7ac681cf
PM
4733 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4734 define_arm_cp_regs(cpu, lpae_cp_reginfo);
4735 }
7884849c
PM
4736 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4737 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4738 * be read-only (ie write causes UNDEF exception).
4739 */
4740 {
00a29f3d
PM
4741 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4742 /* Pre-v8 MIDR space.
4743 * Note that the MIDR isn't a simple constant register because
7884849c
PM
4744 * of the TI925 behaviour where writes to another register can
4745 * cause the MIDR value to change.
97ce8d61
PC
4746 *
4747 * Unimplemented registers in the c15 0 0 0 space default to
4748 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4749 * and friends override accordingly.
7884849c
PM
4750 */
4751 { .name = "MIDR",
97ce8d61 4752 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 4753 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 4754 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 4755 .readfn = midr_read,
97ce8d61
PC
4756 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4757 .type = ARM_CP_OVERRIDE },
7884849c
PM
4758 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4759 { .name = "DUMMY",
4760 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4761 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4762 { .name = "DUMMY",
4763 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4764 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4765 { .name = "DUMMY",
4766 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4767 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4768 { .name = "DUMMY",
4769 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4770 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4771 { .name = "DUMMY",
4772 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4773 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4774 REGINFO_SENTINEL
4775 };
00a29f3d 4776 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
4777 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4778 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
4779 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
4780 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4781 .readfn = midr_read },
ac00c79f
SF
4782 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4783 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4784 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4785 .access = PL1_R, .resetvalue = cpu->midr },
4786 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4787 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4788 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
4789 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4790 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
13b72b2b 4791 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
4792 REGINFO_SENTINEL
4793 };
4794 ARMCPRegInfo id_cp_reginfo[] = {
4795 /* These are common to v8 and pre-v8 */
4796 { .name = "CTR",
4797 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4798 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4799 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4801 .access = PL0_R, .accessfn = ctr_el0_access,
4802 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4803 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4804 { .name = "TCMTR",
4805 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4806 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
4807 REGINFO_SENTINEL
4808 };
8085ce63
PC
4809 /* TLBTR is specific to VMSA */
4810 ARMCPRegInfo id_tlbtr_reginfo = {
4811 .name = "TLBTR",
4812 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4813 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4814 };
3281af81
PC
4815 /* MPUIR is specific to PMSA V6+ */
4816 ARMCPRegInfo id_mpuir_reginfo = {
4817 .name = "MPUIR",
4818 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4819 .access = PL1_R, .type = ARM_CP_CONST,
4820 .resetvalue = cpu->pmsav7_dregion << 8
4821 };
7884849c
PM
4822 ARMCPRegInfo crn0_wi_reginfo = {
4823 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4824 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4825 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4826 };
4827 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4828 arm_feature(env, ARM_FEATURE_STRONGARM)) {
4829 ARMCPRegInfo *r;
4830 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
4831 * whole space. Then update the specific ID registers to allow write
4832 * access, so that they ignore writes rather than causing them to
4833 * UNDEF.
7884849c
PM
4834 */
4835 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
4836 for (r = id_pre_v8_midr_cp_reginfo;
4837 r->type != ARM_CP_SENTINEL; r++) {
4838 r->access = PL1_RW;
4839 }
7884849c
PM
4840 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4841 r->access = PL1_RW;
7884849c 4842 }
8085ce63 4843 id_tlbtr_reginfo.access = PL1_RW;
3281af81 4844 id_tlbtr_reginfo.access = PL1_RW;
7884849c 4845 }
00a29f3d
PM
4846 if (arm_feature(env, ARM_FEATURE_V8)) {
4847 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4848 } else {
4849 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4850 }
a703eda1 4851 define_arm_cp_regs(cpu, id_cp_reginfo);
8085ce63
PC
4852 if (!arm_feature(env, ARM_FEATURE_MPU)) {
4853 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
4854 } else if (arm_feature(env, ARM_FEATURE_V7)) {
4855 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 4856 }
7884849c
PM
4857 }
4858
97ce8d61
PC
4859 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4860 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4861 }
4862
2771db27 4863 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
4864 ARMCPRegInfo auxcr_reginfo[] = {
4865 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4866 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4867 .access = PL1_RW, .type = ARM_CP_CONST,
4868 .resetvalue = cpu->reset_auxcr },
4869 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4870 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4871 .access = PL2_RW, .type = ARM_CP_CONST,
4872 .resetvalue = 0 },
4873 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4874 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4875 .access = PL3_RW, .type = ARM_CP_CONST,
4876 .resetvalue = 0 },
4877 REGINFO_SENTINEL
2771db27 4878 };
834a6c69 4879 define_arm_cp_regs(cpu, auxcr_reginfo);
2771db27
PM
4880 }
4881
d8ba780b 4882 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
4883 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4884 /* 32 bit view is [31:18] 0...0 [43:32]. */
4885 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4886 | extract64(cpu->reset_cbar, 32, 12);
4887 ARMCPRegInfo cbar_reginfo[] = {
4888 { .name = "CBAR",
4889 .type = ARM_CP_CONST,
4890 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4891 .access = PL1_R, .resetvalue = cpu->reset_cbar },
4892 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4893 .type = ARM_CP_CONST,
4894 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4895 .access = PL1_R, .resetvalue = cbar32 },
4896 REGINFO_SENTINEL
4897 };
4898 /* We don't implement a r/w 64 bit CBAR currently */
4899 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4900 define_arm_cp_regs(cpu, cbar_reginfo);
4901 } else {
4902 ARMCPRegInfo cbar = {
4903 .name = "CBAR",
4904 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4905 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4906 .fieldoffset = offsetof(CPUARMState,
4907 cp15.c15_config_base_address)
4908 };
4909 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4910 cbar.access = PL1_R;
4911 cbar.fieldoffset = 0;
4912 cbar.type = ARM_CP_CONST;
4913 }
4914 define_one_arm_cp_reg(cpu, &cbar);
4915 }
d8ba780b
PC
4916 }
4917
2771db27
PM
4918 /* Generic registers whose values depend on the implementation */
4919 {
4920 ARMCPRegInfo sctlr = {
5ebafdf3 4921 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
4922 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4923 .access = PL1_RW,
4924 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4925 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
4926 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4927 .raw_writefn = raw_write,
2771db27
PM
4928 };
4929 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4930 /* Normally we would always end the TB on an SCTLR write, but Linux
4931 * arch/arm/mach-pxa/sleep.S expects two instructions following
4932 * an MMU enable to execute from cache. Imitate this behaviour.
4933 */
4934 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4935 }
4936 define_one_arm_cp_reg(cpu, &sctlr);
4937 }
2ceb98c0
PM
4938}
4939
778c3a06 4940ARMCPU *cpu_arm_init(const char *cpu_model)
40f137e1 4941{
9262685b 4942 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
14969266
AF
4943}
4944
4945void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4946{
22169d41 4947 CPUState *cs = CPU(cpu);
14969266
AF
4948 CPUARMState *env = &cpu->env;
4949
6a669427
PM
4950 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4951 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4952 aarch64_fpu_gdb_set_reg,
4953 34, "aarch64-fpu.xml", 0);
4954 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 4955 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4956 51, "arm-neon.xml", 0);
4957 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 4958 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4959 35, "arm-vfp3.xml", 0);
4960 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 4961 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
4962 19, "arm-vfp.xml", 0);
4963 }
40f137e1
PB
4964}
4965
777dc784
PM
4966/* Sort alphabetically by type name, except for "any". */
4967static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 4968{
777dc784
PM
4969 ObjectClass *class_a = (ObjectClass *)a;
4970 ObjectClass *class_b = (ObjectClass *)b;
4971 const char *name_a, *name_b;
5adb4839 4972
777dc784
PM
4973 name_a = object_class_get_name(class_a);
4974 name_b = object_class_get_name(class_b);
51492fd1 4975 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 4976 return 1;
51492fd1 4977 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
4978 return -1;
4979 } else {
4980 return strcmp(name_a, name_b);
5adb4839
PB
4981 }
4982}
4983
777dc784 4984static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 4985{
777dc784 4986 ObjectClass *oc = data;
92a31361 4987 CPUListState *s = user_data;
51492fd1
AF
4988 const char *typename;
4989 char *name;
3371d272 4990
51492fd1
AF
4991 typename = object_class_get_name(oc);
4992 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 4993 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
4994 name);
4995 g_free(name);
777dc784
PM
4996}
4997
4998void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
4999{
92a31361 5000 CPUListState s = {
777dc784
PM
5001 .file = f,
5002 .cpu_fprintf = cpu_fprintf,
5003 };
5004 GSList *list;
5005
5006 list = object_class_get_list(TYPE_ARM_CPU, false);
5007 list = g_slist_sort(list, arm_cpu_list_compare);
5008 (*cpu_fprintf)(f, "Available CPUs:\n");
5009 g_slist_foreach(list, arm_cpu_list_entry, &s);
5010 g_slist_free(list);
a96c0514
PM
5011#ifdef CONFIG_KVM
5012 /* The 'host' CPU type is dynamically registered only if KVM is
5013 * enabled, so we have to special-case it here:
5014 */
5015 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5016#endif
40f137e1
PB
5017}
5018
78027bb6
CR
5019static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5020{
5021 ObjectClass *oc = data;
5022 CpuDefinitionInfoList **cpu_list = user_data;
5023 CpuDefinitionInfoList *entry;
5024 CpuDefinitionInfo *info;
5025 const char *typename;
5026
5027 typename = object_class_get_name(oc);
5028 info = g_malloc0(sizeof(*info));
5029 info->name = g_strndup(typename,
5030 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5031
5032 entry = g_malloc0(sizeof(*entry));
5033 entry->value = info;
5034 entry->next = *cpu_list;
5035 *cpu_list = entry;
5036}
5037
5038CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5039{
5040 CpuDefinitionInfoList *cpu_list = NULL;
5041 GSList *list;
5042
5043 list = object_class_get_list(TYPE_ARM_CPU, false);
5044 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5045 g_slist_free(list);
5046
5047 return cpu_list;
5048}
5049
6e6efd61 5050static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 5051 void *opaque, int state, int secstate,
f5a0a5a5 5052 int crm, int opc1, int opc2)
6e6efd61
PM
5053{
5054 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5055 * add a single reginfo struct to the hash table.
5056 */
5057 uint32_t *key = g_new(uint32_t, 1);
5058 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5059 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
5060 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5061
5062 /* Reset the secure state to the specific incoming state. This is
5063 * necessary as the register may have been defined with both states.
5064 */
5065 r2->secure = secstate;
5066
5067 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5068 /* Register is banked (using both entries in array).
5069 * Overwriting fieldoffset as the array is only used to define
5070 * banked registers but later only fieldoffset is used.
f5a0a5a5 5071 */
3f3c82a5
FA
5072 r2->fieldoffset = r->bank_fieldoffsets[ns];
5073 }
5074
5075 if (state == ARM_CP_STATE_AA32) {
5076 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5077 /* If the register is banked then we don't need to migrate or
5078 * reset the 32-bit instance in certain cases:
5079 *
5080 * 1) If the register has both 32-bit and 64-bit instances then we
5081 * can count on the 64-bit instance taking care of the
5082 * non-secure bank.
5083 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5084 * taking care of the secure bank. This requires that separate
5085 * 32 and 64-bit definitions are provided.
5086 */
5087 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5088 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 5089 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
5090 }
5091 } else if ((secstate != r->secure) && !ns) {
5092 /* The register is not banked so we only want to allow migration of
5093 * the non-secure instance.
5094 */
7a0e58fa 5095 r2->type |= ARM_CP_ALIAS;
58a1d8ce 5096 }
3f3c82a5
FA
5097
5098 if (r->state == ARM_CP_STATE_BOTH) {
5099 /* We assume it is a cp15 register if the .cp field is left unset.
5100 */
5101 if (r2->cp == 0) {
5102 r2->cp = 15;
5103 }
5104
f5a0a5a5 5105#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
5106 if (r2->fieldoffset) {
5107 r2->fieldoffset += sizeof(uint32_t);
5108 }
f5a0a5a5 5109#endif
3f3c82a5 5110 }
f5a0a5a5
PM
5111 }
5112 if (state == ARM_CP_STATE_AA64) {
5113 /* To allow abbreviation of ARMCPRegInfo
5114 * definitions, we treat cp == 0 as equivalent to
5115 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
5116 * STATE_BOTH definitions are also always "standard
5117 * sysreg" in their AArch64 view (the .cp value may
5118 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 5119 */
58a1d8ce 5120 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
5121 r2->cp = CP_REG_ARM64_SYSREG_CP;
5122 }
5123 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5124 r2->opc0, opc1, opc2);
5125 } else {
51a79b03 5126 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 5127 }
6e6efd61
PM
5128 if (opaque) {
5129 r2->opaque = opaque;
5130 }
67ed771d
PM
5131 /* reginfo passed to helpers is correct for the actual access,
5132 * and is never ARM_CP_STATE_BOTH:
5133 */
5134 r2->state = state;
6e6efd61
PM
5135 /* Make sure reginfo passed to helpers for wildcarded regs
5136 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5137 */
5138 r2->crm = crm;
5139 r2->opc1 = opc1;
5140 r2->opc2 = opc2;
5141 /* By convention, for wildcarded registers only the first
5142 * entry is used for migration; the others are marked as
7a0e58fa 5143 * ALIAS so we don't try to transfer the register
6e6efd61 5144 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 5145 * never migratable and not even raw-accessible.
6e6efd61 5146 */
7a0e58fa
PM
5147 if ((r->type & ARM_CP_SPECIAL)) {
5148 r2->type |= ARM_CP_NO_RAW;
5149 }
5150 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
5151 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5152 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 5153 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
5154 }
5155
375421cc
PM
5156 /* Check that raw accesses are either forbidden or handled. Note that
5157 * we can't assert this earlier because the setup of fieldoffset for
5158 * banked registers has to be done first.
5159 */
5160 if (!(r2->type & ARM_CP_NO_RAW)) {
5161 assert(!raw_accessors_invalid(r2));
5162 }
5163
6e6efd61
PM
5164 /* Overriding of an existing definition must be explicitly
5165 * requested.
5166 */
5167 if (!(r->type & ARM_CP_OVERRIDE)) {
5168 ARMCPRegInfo *oldreg;
5169 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5170 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5171 fprintf(stderr, "Register redefined: cp=%d %d bit "
5172 "crn=%d crm=%d opc1=%d opc2=%d, "
5173 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5174 r2->crn, r2->crm, r2->opc1, r2->opc2,
5175 oldreg->name, r2->name);
5176 g_assert_not_reached();
5177 }
5178 }
5179 g_hash_table_insert(cpu->cp_regs, key, r2);
5180}
5181
5182
4b6a83fb
PM
5183void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5184 const ARMCPRegInfo *r, void *opaque)
5185{
5186 /* Define implementations of coprocessor registers.
5187 * We store these in a hashtable because typically
5188 * there are less than 150 registers in a space which
5189 * is 16*16*16*8*8 = 262144 in size.
5190 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5191 * If a register is defined twice then the second definition is
5192 * used, so this can be used to define some generic registers and
5193 * then override them with implementation specific variations.
5194 * At least one of the original and the second definition should
5195 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5196 * against accidental use.
f5a0a5a5
PM
5197 *
5198 * The state field defines whether the register is to be
5199 * visible in the AArch32 or AArch64 execution state. If the
5200 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5201 * reginfo structure for the AArch32 view, which sees the lower
5202 * 32 bits of the 64 bit register.
5203 *
5204 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5205 * be wildcarded. AArch64 registers are always considered to be 64
5206 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5207 * the register, if any.
4b6a83fb 5208 */
f5a0a5a5 5209 int crm, opc1, opc2, state;
4b6a83fb
PM
5210 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5211 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5212 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5213 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5214 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5215 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5216 /* 64 bit registers have only CRm and Opc1 fields */
5217 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
5218 /* op0 only exists in the AArch64 encodings */
5219 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5220 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5221 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5222 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5223 * encodes a minimum access level for the register. We roll this
5224 * runtime check into our general permission check code, so check
5225 * here that the reginfo's specified permissions are strict enough
5226 * to encompass the generic architectural permission check.
5227 */
5228 if (r->state != ARM_CP_STATE_AA32) {
5229 int mask = 0;
5230 switch (r->opc1) {
5231 case 0: case 1: case 2:
5232 /* min_EL EL1 */
5233 mask = PL1_RW;
5234 break;
5235 case 3:
5236 /* min_EL EL0 */
5237 mask = PL0_RW;
5238 break;
5239 case 4:
5240 /* min_EL EL2 */
5241 mask = PL2_RW;
5242 break;
5243 case 5:
5244 /* unallocated encoding, so not possible */
5245 assert(false);
5246 break;
5247 case 6:
5248 /* min_EL EL3 */
5249 mask = PL3_RW;
5250 break;
5251 case 7:
5252 /* min_EL EL1, secure mode only (we don't check the latter) */
5253 mask = PL1_RW;
5254 break;
5255 default:
5256 /* broken reginfo with out-of-range opc1 */
5257 assert(false);
5258 break;
5259 }
5260 /* assert our permissions are not too lax (stricter is fine) */
5261 assert((r->access & ~mask) == 0);
5262 }
5263
4b6a83fb
PM
5264 /* Check that the register definition has enough info to handle
5265 * reads and writes if they are permitted.
5266 */
5267 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5268 if (r->access & PL3_R) {
3f3c82a5
FA
5269 assert((r->fieldoffset ||
5270 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5271 r->readfn);
4b6a83fb
PM
5272 }
5273 if (r->access & PL3_W) {
3f3c82a5
FA
5274 assert((r->fieldoffset ||
5275 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5276 r->writefn);
4b6a83fb
PM
5277 }
5278 }
5279 /* Bad type field probably means missing sentinel at end of reg list */
5280 assert(cptype_valid(r->type));
5281 for (crm = crmmin; crm <= crmmax; crm++) {
5282 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5283 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
5284 for (state = ARM_CP_STATE_AA32;
5285 state <= ARM_CP_STATE_AA64; state++) {
5286 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5287 continue;
5288 }
3f3c82a5
FA
5289 if (state == ARM_CP_STATE_AA32) {
5290 /* Under AArch32 CP registers can be common
5291 * (same for secure and non-secure world) or banked.
5292 */
5293 switch (r->secure) {
5294 case ARM_CP_SECSTATE_S:
5295 case ARM_CP_SECSTATE_NS:
5296 add_cpreg_to_hashtable(cpu, r, opaque, state,
5297 r->secure, crm, opc1, opc2);
5298 break;
5299 default:
5300 add_cpreg_to_hashtable(cpu, r, opaque, state,
5301 ARM_CP_SECSTATE_S,
5302 crm, opc1, opc2);
5303 add_cpreg_to_hashtable(cpu, r, opaque, state,
5304 ARM_CP_SECSTATE_NS,
5305 crm, opc1, opc2);
5306 break;
5307 }
5308 } else {
5309 /* AArch64 registers get mapped to non-secure instance
5310 * of AArch32 */
5311 add_cpreg_to_hashtable(cpu, r, opaque, state,
5312 ARM_CP_SECSTATE_NS,
5313 crm, opc1, opc2);
5314 }
f5a0a5a5 5315 }
4b6a83fb
PM
5316 }
5317 }
5318 }
5319}
5320
5321void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5322 const ARMCPRegInfo *regs, void *opaque)
5323{
5324 /* Define a whole list of registers */
5325 const ARMCPRegInfo *r;
5326 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5327 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5328 }
5329}
5330
60322b39 5331const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 5332{
60322b39 5333 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
5334}
5335
c4241c7d
PM
5336void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5337 uint64_t value)
4b6a83fb
PM
5338{
5339 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
5340}
5341
c4241c7d 5342uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
5343{
5344 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
5345 return 0;
5346}
5347
f5a0a5a5
PM
5348void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5349{
5350 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5351}
5352
af393ffc 5353static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
5354{
5355 /* Return true if it is not valid for us to switch to
5356 * this CPU mode (ie all the UNPREDICTABLE cases in
5357 * the ARM ARM CPSRWriteByInstr pseudocode).
5358 */
af393ffc
PM
5359
5360 /* Changes to or from Hyp via MSR and CPS are illegal. */
5361 if (write_type == CPSRWriteByInstr &&
5362 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5363 mode == ARM_CPU_MODE_HYP)) {
5364 return 1;
5365 }
5366
37064a8b
PM
5367 switch (mode) {
5368 case ARM_CPU_MODE_USR:
10eacda7 5369 return 0;
37064a8b
PM
5370 case ARM_CPU_MODE_SYS:
5371 case ARM_CPU_MODE_SVC:
5372 case ARM_CPU_MODE_ABT:
5373 case ARM_CPU_MODE_UND:
5374 case ARM_CPU_MODE_IRQ:
5375 case ARM_CPU_MODE_FIQ:
52ff951b
PM
5376 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5377 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5378 */
10eacda7
PM
5379 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5380 * and CPS are treated as illegal mode changes.
5381 */
5382 if (write_type == CPSRWriteByInstr &&
5383 (env->cp15.hcr_el2 & HCR_TGE) &&
5384 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5385 !arm_is_secure_below_el3(env)) {
5386 return 1;
5387 }
37064a8b 5388 return 0;
e6c8fc07
PM
5389 case ARM_CPU_MODE_HYP:
5390 return !arm_feature(env, ARM_FEATURE_EL2)
5391 || arm_current_el(env) < 2 || arm_is_secure(env);
027fc527 5392 case ARM_CPU_MODE_MON:
58ae2d1f 5393 return arm_current_el(env) < 3;
37064a8b
PM
5394 default:
5395 return 1;
5396 }
5397}
5398
2f4a40e5
AZ
5399uint32_t cpsr_read(CPUARMState *env)
5400{
5401 int ZF;
6fbe23d5
PB
5402 ZF = (env->ZF == 0);
5403 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
5404 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5405 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5406 | ((env->condexec_bits & 0xfc) << 8)
af519934 5407 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
5408}
5409
50866ba5
PM
5410void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5411 CPSRWriteType write_type)
2f4a40e5 5412{
6e8801f9
FA
5413 uint32_t changed_daif;
5414
2f4a40e5 5415 if (mask & CPSR_NZCV) {
6fbe23d5
PB
5416 env->ZF = (~val) & CPSR_Z;
5417 env->NF = val;
2f4a40e5
AZ
5418 env->CF = (val >> 29) & 1;
5419 env->VF = (val << 3) & 0x80000000;
5420 }
5421 if (mask & CPSR_Q)
5422 env->QF = ((val & CPSR_Q) != 0);
5423 if (mask & CPSR_T)
5424 env->thumb = ((val & CPSR_T) != 0);
5425 if (mask & CPSR_IT_0_1) {
5426 env->condexec_bits &= ~3;
5427 env->condexec_bits |= (val >> 25) & 3;
5428 }
5429 if (mask & CPSR_IT_2_7) {
5430 env->condexec_bits &= 3;
5431 env->condexec_bits |= (val >> 8) & 0xfc;
5432 }
5433 if (mask & CPSR_GE) {
5434 env->GE = (val >> 16) & 0xf;
5435 }
5436
6e8801f9
FA
5437 /* In a V7 implementation that includes the security extensions but does
5438 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5439 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5440 * bits respectively.
5441 *
5442 * In a V8 implementation, it is permitted for privileged software to
5443 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5444 */
f8c88bbc 5445 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
5446 arm_feature(env, ARM_FEATURE_EL3) &&
5447 !arm_feature(env, ARM_FEATURE_EL2) &&
5448 !arm_is_secure(env)) {
5449
5450 changed_daif = (env->daif ^ val) & mask;
5451
5452 if (changed_daif & CPSR_A) {
5453 /* Check to see if we are allowed to change the masking of async
5454 * abort exceptions from a non-secure state.
5455 */
5456 if (!(env->cp15.scr_el3 & SCR_AW)) {
5457 qemu_log_mask(LOG_GUEST_ERROR,
5458 "Ignoring attempt to switch CPSR_A flag from "
5459 "non-secure world with SCR.AW bit clear\n");
5460 mask &= ~CPSR_A;
5461 }
5462 }
5463
5464 if (changed_daif & CPSR_F) {
5465 /* Check to see if we are allowed to change the masking of FIQ
5466 * exceptions from a non-secure state.
5467 */
5468 if (!(env->cp15.scr_el3 & SCR_FW)) {
5469 qemu_log_mask(LOG_GUEST_ERROR,
5470 "Ignoring attempt to switch CPSR_F flag from "
5471 "non-secure world with SCR.FW bit clear\n");
5472 mask &= ~CPSR_F;
5473 }
5474
5475 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5476 * If this bit is set software is not allowed to mask
5477 * FIQs, but is allowed to set CPSR_F to 0.
5478 */
5479 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5480 (val & CPSR_F)) {
5481 qemu_log_mask(LOG_GUEST_ERROR,
5482 "Ignoring attempt to enable CPSR_F flag "
5483 "(non-maskable FIQ [NMFI] support enabled)\n");
5484 mask &= ~CPSR_F;
5485 }
5486 }
5487 }
5488
4cc35614
PM
5489 env->daif &= ~(CPSR_AIF & mask);
5490 env->daif |= val & CPSR_AIF & mask;
5491
f8c88bbc
PM
5492 if (write_type != CPSRWriteRaw &&
5493 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
5494 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5495 /* Note that we can only get here in USR mode if this is a
5496 * gdb stub write; for this case we follow the architectural
5497 * behaviour for guest writes in USR mode of ignoring an attempt
5498 * to switch mode. (Those are caught by translate.c for writes
5499 * triggered by guest instructions.)
5500 */
5501 mask &= ~CPSR_M;
5502 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
5503 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5504 * v7, and has defined behaviour in v8:
5505 * + leave CPSR.M untouched
5506 * + allow changes to the other CPSR fields
5507 * + set PSTATE.IL
5508 * For user changes via the GDB stub, we don't set PSTATE.IL,
5509 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
5510 */
5511 mask &= ~CPSR_M;
81907a58
PM
5512 if (write_type != CPSRWriteByGDBStub &&
5513 arm_feature(env, ARM_FEATURE_V8)) {
5514 mask |= CPSR_IL;
5515 val |= CPSR_IL;
5516 }
37064a8b
PM
5517 } else {
5518 switch_mode(env, val & CPSR_M);
5519 }
2f4a40e5
AZ
5520 }
5521 mask &= ~CACHED_CPSR_BITS;
5522 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5523}
5524
b26eefb6
PB
5525/* Sign/zero extend */
5526uint32_t HELPER(sxtb16)(uint32_t x)
5527{
5528 uint32_t res;
5529 res = (uint16_t)(int8_t)x;
5530 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5531 return res;
5532}
5533
5534uint32_t HELPER(uxtb16)(uint32_t x)
5535{
5536 uint32_t res;
5537 res = (uint16_t)(uint8_t)x;
5538 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5539 return res;
5540}
5541
f51bbbfe
PB
5542uint32_t HELPER(clz)(uint32_t x)
5543{
7bbcb0af 5544 return clz32(x);
f51bbbfe
PB
5545}
5546
3670669c
PB
5547int32_t HELPER(sdiv)(int32_t num, int32_t den)
5548{
5549 if (den == 0)
5550 return 0;
686eeb93
AJ
5551 if (num == INT_MIN && den == -1)
5552 return INT_MIN;
3670669c
PB
5553 return num / den;
5554}
5555
5556uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5557{
5558 if (den == 0)
5559 return 0;
5560 return num / den;
5561}
5562
5563uint32_t HELPER(rbit)(uint32_t x)
5564{
42fedbca 5565 return revbit32(x);
3670669c
PB
5566}
5567
5fafdf24 5568#if defined(CONFIG_USER_ONLY)
b5ff1b31 5569
9ee6e8bb 5570/* These should probably raise undefined insn exceptions. */
0ecb72a5 5571void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 5572{
a47dddd7
AF
5573 ARMCPU *cpu = arm_env_get_cpu(env);
5574
5575 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
5576}
5577
0ecb72a5 5578uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 5579{
a47dddd7
AF
5580 ARMCPU *cpu = arm_env_get_cpu(env);
5581
5582 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
5583 return 0;
5584}
5585
0ecb72a5 5586void switch_mode(CPUARMState *env, int mode)
b5ff1b31 5587{
a47dddd7
AF
5588 ARMCPU *cpu = arm_env_get_cpu(env);
5589
5590 if (mode != ARM_CPU_MODE_USR) {
5591 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5592 }
b5ff1b31
FB
5593}
5594
012a906b
GB
5595uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5596 uint32_t cur_el, bool secure)
9e729b57
EI
5597{
5598 return 1;
5599}
5600
ce02049d
GB
5601void aarch64_sync_64_to_32(CPUARMState *env)
5602{
5603 g_assert_not_reached();
5604}
5605
b5ff1b31
FB
5606#else
5607
0ecb72a5 5608void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
5609{
5610 int old_mode;
5611 int i;
5612
5613 old_mode = env->uncached_cpsr & CPSR_M;
5614 if (mode == old_mode)
5615 return;
5616
5617 if (old_mode == ARM_CPU_MODE_FIQ) {
5618 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5619 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5620 } else if (mode == ARM_CPU_MODE_FIQ) {
5621 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 5622 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
5623 }
5624
f5206413 5625 i = bank_number(old_mode);
b5ff1b31
FB
5626 env->banked_r13[i] = env->regs[13];
5627 env->banked_r14[i] = env->regs[14];
5628 env->banked_spsr[i] = env->spsr;
5629
f5206413 5630 i = bank_number(mode);
b5ff1b31
FB
5631 env->regs[13] = env->banked_r13[i];
5632 env->regs[14] = env->banked_r14[i];
5633 env->spsr = env->banked_spsr[i];
5634}
5635
0eeb17d6
GB
5636/* Physical Interrupt Target EL Lookup Table
5637 *
5638 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5639 *
5640 * The below multi-dimensional table is used for looking up the target
5641 * exception level given numerous condition criteria. Specifically, the
5642 * target EL is based on SCR and HCR routing controls as well as the
5643 * currently executing EL and secure state.
5644 *
5645 * Dimensions:
5646 * target_el_table[2][2][2][2][2][4]
5647 * | | | | | +--- Current EL
5648 * | | | | +------ Non-secure(0)/Secure(1)
5649 * | | | +--------- HCR mask override
5650 * | | +------------ SCR exec state control
5651 * | +--------------- SCR mask override
5652 * +------------------ 32-bit(0)/64-bit(1) EL3
5653 *
5654 * The table values are as such:
5655 * 0-3 = EL0-EL3
5656 * -1 = Cannot occur
5657 *
5658 * The ARM ARM target EL table includes entries indicating that an "exception
5659 * is not taken". The two cases where this is applicable are:
5660 * 1) An exception is taken from EL3 but the SCR does not have the exception
5661 * routed to EL3.
5662 * 2) An exception is taken from EL2 but the HCR does not have the exception
5663 * routed to EL2.
5664 * In these two cases, the below table contain a target of EL1. This value is
5665 * returned as it is expected that the consumer of the table data will check
5666 * for "target EL >= current EL" to ensure the exception is not taken.
5667 *
5668 * SCR HCR
5669 * 64 EA AMO From
5670 * BIT IRQ IMO Non-secure Secure
5671 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
5672 */
82c39f6a 5673static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
5674 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5675 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
5676 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5677 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
5678 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5679 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
5680 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5681 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
5682 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
5683 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
5684 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
5685 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
5686 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5687 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
5688 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5689 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
5690};
5691
5692/*
5693 * Determine the target EL for physical exceptions
5694 */
012a906b
GB
5695uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5696 uint32_t cur_el, bool secure)
0eeb17d6
GB
5697{
5698 CPUARMState *env = cs->env_ptr;
2cde031f 5699 int rw;
0eeb17d6
GB
5700 int scr;
5701 int hcr;
5702 int target_el;
2cde031f
SS
5703 /* Is the highest EL AArch64? */
5704 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
5705
5706 if (arm_feature(env, ARM_FEATURE_EL3)) {
5707 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5708 } else {
5709 /* Either EL2 is the highest EL (and so the EL2 register width
5710 * is given by is64); or there is no EL2 or EL3, in which case
5711 * the value of 'rw' does not affect the table lookup anyway.
5712 */
5713 rw = is64;
5714 }
0eeb17d6
GB
5715
5716 switch (excp_idx) {
5717 case EXCP_IRQ:
5718 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5719 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5720 break;
5721 case EXCP_FIQ:
5722 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5723 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5724 break;
5725 default:
5726 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5727 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5728 break;
5729 };
5730
5731 /* If HCR.TGE is set then HCR is treated as being 1 */
5732 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5733
5734 /* Perform a table-lookup for the target EL given the current state */
5735 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5736
5737 assert(target_el > 0);
5738
5739 return target_el;
5740}
5741
9ee6e8bb
PB
5742static void v7m_push(CPUARMState *env, uint32_t val)
5743{
70d74660
AF
5744 CPUState *cs = CPU(arm_env_get_cpu(env));
5745
9ee6e8bb 5746 env->regs[13] -= 4;
ab1da857 5747 stl_phys(cs->as, env->regs[13], val);
9ee6e8bb
PB
5748}
5749
5750static uint32_t v7m_pop(CPUARMState *env)
5751{
70d74660 5752 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb 5753 uint32_t val;
70d74660 5754
fdfba1a2 5755 val = ldl_phys(cs->as, env->regs[13]);
9ee6e8bb
PB
5756 env->regs[13] += 4;
5757 return val;
5758}
5759
5760/* Switch to V7M main or process stack pointer. */
5761static void switch_v7m_sp(CPUARMState *env, int process)
5762{
5763 uint32_t tmp;
5764 if (env->v7m.current_sp != process) {
5765 tmp = env->v7m.other_sp;
5766 env->v7m.other_sp = env->regs[13];
5767 env->regs[13] = tmp;
5768 env->v7m.current_sp = process;
5769 }
5770}
5771
5772static void do_v7m_exception_exit(CPUARMState *env)
5773{
5774 uint32_t type;
5775 uint32_t xpsr;
5776
5777 type = env->regs[15];
5778 if (env->v7m.exception != 0)
983fe826 5779 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
9ee6e8bb
PB
5780
5781 /* Switch to the target stack. */
5782 switch_v7m_sp(env, (type & 4) != 0);
5783 /* Pop registers. */
5784 env->regs[0] = v7m_pop(env);
5785 env->regs[1] = v7m_pop(env);
5786 env->regs[2] = v7m_pop(env);
5787 env->regs[3] = v7m_pop(env);
5788 env->regs[12] = v7m_pop(env);
5789 env->regs[14] = v7m_pop(env);
5790 env->regs[15] = v7m_pop(env);
fcf83ab1
PM
5791 if (env->regs[15] & 1) {
5792 qemu_log_mask(LOG_GUEST_ERROR,
5793 "M profile return from interrupt with misaligned "
5794 "PC is UNPREDICTABLE\n");
5795 /* Actual hardware seems to ignore the lsbit, and there are several
5796 * RTOSes out there which incorrectly assume the r15 in the stack
5797 * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5798 */
5799 env->regs[15] &= ~1U;
5800 }
9ee6e8bb
PB
5801 xpsr = v7m_pop(env);
5802 xpsr_write(env, xpsr, 0xfffffdff);
5803 /* Undo stack alignment. */
5804 if (xpsr & 0x200)
5805 env->regs[13] |= 4;
5806 /* ??? The exception return type specifies Thread/Handler mode. However
5807 this is also implied by the xPSR value. Not sure what to do
5808 if there is a mismatch. */
5809 /* ??? Likewise for mismatches between the CONTROL register and the stack
5810 pointer. */
5811}
5812
e6f010cc 5813void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 5814{
e6f010cc
AF
5815 ARMCPU *cpu = ARM_CPU(cs);
5816 CPUARMState *env = &cpu->env;
9ee6e8bb
PB
5817 uint32_t xpsr = xpsr_read(env);
5818 uint32_t lr;
5819 uint32_t addr;
5820
27103424 5821 arm_log_exception(cs->exception_index);
3f1beaca 5822
9ee6e8bb
PB
5823 lr = 0xfffffff1;
5824 if (env->v7m.current_sp)
5825 lr |= 4;
5826 if (env->v7m.exception == 0)
5827 lr |= 8;
5828
5829 /* For exceptions we just mark as pending on the NVIC, and let that
5830 handle it. */
5831 /* TODO: Need to escalate if the current priority is higher than the
5832 one we're raising. */
27103424 5833 switch (cs->exception_index) {
9ee6e8bb 5834 case EXCP_UDEF:
983fe826 5835 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
9ee6e8bb
PB
5836 return;
5837 case EXCP_SWI:
314e2296 5838 /* The PC already points to the next instruction. */
983fe826 5839 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
9ee6e8bb
PB
5840 return;
5841 case EXCP_PREFETCH_ABORT:
5842 case EXCP_DATA_ABORT:
abf1172f
PM
5843 /* TODO: if we implemented the MPU registers, this is where we
5844 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5845 */
983fe826 5846 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
9ee6e8bb
PB
5847 return;
5848 case EXCP_BKPT:
cfe67cef 5849 if (semihosting_enabled()) {
2ad207d4 5850 int nr;
f9fd40eb 5851 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
2ad207d4
PB
5852 if (nr == 0xab) {
5853 env->regs[15] += 2;
205ace55
CC
5854 qemu_log_mask(CPU_LOG_INT,
5855 "...handling as semihosting call 0x%x\n",
5856 env->regs[0]);
2ad207d4
PB
5857 env->regs[0] = do_arm_semihosting(env);
5858 return;
5859 }
5860 }
983fe826 5861 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
9ee6e8bb
PB
5862 return;
5863 case EXCP_IRQ:
983fe826 5864 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
9ee6e8bb
PB
5865 break;
5866 case EXCP_EXCEPTION_EXIT:
5867 do_v7m_exception_exit(env);
5868 return;
5869 default:
a47dddd7 5870 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
5871 return; /* Never happens. Keep compiler happy. */
5872 }
5873
5874 /* Align stack pointer. */
5875 /* ??? Should only do this if Configuration Control Register
5876 STACKALIGN bit is set. */
5877 if (env->regs[13] & 4) {
ab19b0ec 5878 env->regs[13] -= 4;
9ee6e8bb
PB
5879 xpsr |= 0x200;
5880 }
6c95676b 5881 /* Switch to the handler mode. */
9ee6e8bb
PB
5882 v7m_push(env, xpsr);
5883 v7m_push(env, env->regs[15]);
5884 v7m_push(env, env->regs[14]);
5885 v7m_push(env, env->regs[12]);
5886 v7m_push(env, env->regs[3]);
5887 v7m_push(env, env->regs[2]);
5888 v7m_push(env, env->regs[1]);
5889 v7m_push(env, env->regs[0]);
5890 switch_v7m_sp(env, 0);
c98d174c
PM
5891 /* Clear IT bits */
5892 env->condexec_bits = 0;
9ee6e8bb 5893 env->regs[14] = lr;
fdfba1a2 5894 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
9ee6e8bb
PB
5895 env->regs[15] = addr & 0xfffffffe;
5896 env->thumb = addr & 1;
5897}
5898
ce02049d
GB
5899/* Function used to synchronize QEMU's AArch64 register set with AArch32
5900 * register set. This is necessary when switching between AArch32 and AArch64
5901 * execution state.
5902 */
5903void aarch64_sync_32_to_64(CPUARMState *env)
5904{
5905 int i;
5906 uint32_t mode = env->uncached_cpsr & CPSR_M;
5907
5908 /* We can blanket copy R[0:7] to X[0:7] */
5909 for (i = 0; i < 8; i++) {
5910 env->xregs[i] = env->regs[i];
5911 }
5912
5913 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5914 * Otherwise, they come from the banked user regs.
5915 */
5916 if (mode == ARM_CPU_MODE_FIQ) {
5917 for (i = 8; i < 13; i++) {
5918 env->xregs[i] = env->usr_regs[i - 8];
5919 }
5920 } else {
5921 for (i = 8; i < 13; i++) {
5922 env->xregs[i] = env->regs[i];
5923 }
5924 }
5925
5926 /* Registers x13-x23 are the various mode SP and FP registers. Registers
5927 * r13 and r14 are only copied if we are in that mode, otherwise we copy
5928 * from the mode banked register.
5929 */
5930 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5931 env->xregs[13] = env->regs[13];
5932 env->xregs[14] = env->regs[14];
5933 } else {
5934 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5935 /* HYP is an exception in that it is copied from r14 */
5936 if (mode == ARM_CPU_MODE_HYP) {
5937 env->xregs[14] = env->regs[14];
5938 } else {
5939 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5940 }
5941 }
5942
5943 if (mode == ARM_CPU_MODE_HYP) {
5944 env->xregs[15] = env->regs[13];
5945 } else {
5946 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5947 }
5948
5949 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
5950 env->xregs[16] = env->regs[14];
5951 env->xregs[17] = env->regs[13];
ce02049d 5952 } else {
3a9148d0
SS
5953 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5954 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
ce02049d
GB
5955 }
5956
5957 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
5958 env->xregs[18] = env->regs[14];
5959 env->xregs[19] = env->regs[13];
ce02049d 5960 } else {
3a9148d0
SS
5961 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5962 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
ce02049d
GB
5963 }
5964
5965 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
5966 env->xregs[20] = env->regs[14];
5967 env->xregs[21] = env->regs[13];
ce02049d 5968 } else {
3a9148d0
SS
5969 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5970 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
ce02049d
GB
5971 }
5972
5973 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
5974 env->xregs[22] = env->regs[14];
5975 env->xregs[23] = env->regs[13];
ce02049d 5976 } else {
3a9148d0
SS
5977 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
5978 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
ce02049d
GB
5979 }
5980
5981 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
5982 * mode, then we can copy from r8-r14. Otherwise, we copy from the
5983 * FIQ bank for r8-r14.
5984 */
5985 if (mode == ARM_CPU_MODE_FIQ) {
5986 for (i = 24; i < 31; i++) {
5987 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
5988 }
5989 } else {
5990 for (i = 24; i < 29; i++) {
5991 env->xregs[i] = env->fiq_regs[i - 24];
5992 }
5993 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
5994 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
5995 }
5996
5997 env->pc = env->regs[15];
5998}
5999
6000/* Function used to synchronize QEMU's AArch32 register set with AArch64
6001 * register set. This is necessary when switching between AArch32 and AArch64
6002 * execution state.
6003 */
6004void aarch64_sync_64_to_32(CPUARMState *env)
6005{
6006 int i;
6007 uint32_t mode = env->uncached_cpsr & CPSR_M;
6008
6009 /* We can blanket copy X[0:7] to R[0:7] */
6010 for (i = 0; i < 8; i++) {
6011 env->regs[i] = env->xregs[i];
6012 }
6013
6014 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
6015 * Otherwise, we copy x8-x12 into the banked user regs.
6016 */
6017 if (mode == ARM_CPU_MODE_FIQ) {
6018 for (i = 8; i < 13; i++) {
6019 env->usr_regs[i - 8] = env->xregs[i];
6020 }
6021 } else {
6022 for (i = 8; i < 13; i++) {
6023 env->regs[i] = env->xregs[i];
6024 }
6025 }
6026
6027 /* Registers r13 & r14 depend on the current mode.
6028 * If we are in a given mode, we copy the corresponding x registers to r13
6029 * and r14. Otherwise, we copy the x register to the banked r13 and r14
6030 * for the mode.
6031 */
6032 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
6033 env->regs[13] = env->xregs[13];
6034 env->regs[14] = env->xregs[14];
6035 } else {
6036 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
6037
6038 /* HYP is an exception in that it does not have its own banked r14 but
6039 * shares the USR r14
6040 */
6041 if (mode == ARM_CPU_MODE_HYP) {
6042 env->regs[14] = env->xregs[14];
6043 } else {
6044 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
6045 }
6046 }
6047
6048 if (mode == ARM_CPU_MODE_HYP) {
6049 env->regs[13] = env->xregs[15];
6050 } else {
6051 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
6052 }
6053
6054 if (mode == ARM_CPU_MODE_IRQ) {
3a9148d0
SS
6055 env->regs[14] = env->xregs[16];
6056 env->regs[13] = env->xregs[17];
ce02049d 6057 } else {
3a9148d0
SS
6058 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
6059 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
ce02049d
GB
6060 }
6061
6062 if (mode == ARM_CPU_MODE_SVC) {
3a9148d0
SS
6063 env->regs[14] = env->xregs[18];
6064 env->regs[13] = env->xregs[19];
ce02049d 6065 } else {
3a9148d0
SS
6066 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
6067 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
ce02049d
GB
6068 }
6069
6070 if (mode == ARM_CPU_MODE_ABT) {
3a9148d0
SS
6071 env->regs[14] = env->xregs[20];
6072 env->regs[13] = env->xregs[21];
ce02049d 6073 } else {
3a9148d0
SS
6074 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
6075 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
6076 }
6077
6078 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
6079 env->regs[14] = env->xregs[22];
6080 env->regs[13] = env->xregs[23];
ce02049d 6081 } else {
3a9148d0
SS
6082 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
6083 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
6084 }
6085
6086 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
6087 * mode, then we can copy to r8-r14. Otherwise, we copy to the
6088 * FIQ bank for r8-r14.
6089 */
6090 if (mode == ARM_CPU_MODE_FIQ) {
6091 for (i = 24; i < 31; i++) {
6092 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
6093 }
6094 } else {
6095 for (i = 24; i < 29; i++) {
6096 env->fiq_regs[i - 24] = env->xregs[i];
6097 }
6098 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
6099 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
6100 }
6101
6102 env->regs[15] = env->pc;
6103}
6104
966f758c 6105static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 6106{
97a8ea5a
AF
6107 ARMCPU *cpu = ARM_CPU(cs);
6108 CPUARMState *env = &cpu->env;
b5ff1b31
FB
6109 uint32_t addr;
6110 uint32_t mask;
6111 int new_mode;
6112 uint32_t offset;
16a906fd 6113 uint32_t moe;
b5ff1b31 6114
16a906fd
PM
6115 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
6116 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
6117 case EC_BREAKPOINT:
6118 case EC_BREAKPOINT_SAME_EL:
6119 moe = 1;
6120 break;
6121 case EC_WATCHPOINT:
6122 case EC_WATCHPOINT_SAME_EL:
6123 moe = 10;
6124 break;
6125 case EC_AA32_BKPT:
6126 moe = 3;
6127 break;
6128 case EC_VECTORCATCH:
6129 moe = 5;
6130 break;
6131 default:
6132 moe = 0;
6133 break;
6134 }
6135
6136 if (moe) {
6137 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
6138 }
6139
b5ff1b31 6140 /* TODO: Vectored interrupt controller. */
27103424 6141 switch (cs->exception_index) {
b5ff1b31
FB
6142 case EXCP_UDEF:
6143 new_mode = ARM_CPU_MODE_UND;
6144 addr = 0x04;
6145 mask = CPSR_I;
6146 if (env->thumb)
6147 offset = 2;
6148 else
6149 offset = 4;
6150 break;
6151 case EXCP_SWI:
6152 new_mode = ARM_CPU_MODE_SVC;
6153 addr = 0x08;
6154 mask = CPSR_I;
601d70b9 6155 /* The PC already points to the next instruction. */
b5ff1b31
FB
6156 offset = 0;
6157 break;
06c949e6 6158 case EXCP_BKPT:
abf1172f 6159 env->exception.fsr = 2;
9ee6e8bb
PB
6160 /* Fall through to prefetch abort. */
6161 case EXCP_PREFETCH_ABORT:
88ca1c2d 6162 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 6163 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 6164 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 6165 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
6166 new_mode = ARM_CPU_MODE_ABT;
6167 addr = 0x0c;
6168 mask = CPSR_A | CPSR_I;
6169 offset = 4;
6170 break;
6171 case EXCP_DATA_ABORT:
4a7e2d73 6172 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 6173 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 6174 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 6175 env->exception.fsr,
6cd8a264 6176 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
6177 new_mode = ARM_CPU_MODE_ABT;
6178 addr = 0x10;
6179 mask = CPSR_A | CPSR_I;
6180 offset = 8;
6181 break;
6182 case EXCP_IRQ:
6183 new_mode = ARM_CPU_MODE_IRQ;
6184 addr = 0x18;
6185 /* Disable IRQ and imprecise data aborts. */
6186 mask = CPSR_A | CPSR_I;
6187 offset = 4;
de38d23b
FA
6188 if (env->cp15.scr_el3 & SCR_IRQ) {
6189 /* IRQ routed to monitor mode */
6190 new_mode = ARM_CPU_MODE_MON;
6191 mask |= CPSR_F;
6192 }
b5ff1b31
FB
6193 break;
6194 case EXCP_FIQ:
6195 new_mode = ARM_CPU_MODE_FIQ;
6196 addr = 0x1c;
6197 /* Disable FIQ, IRQ and imprecise data aborts. */
6198 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
6199 if (env->cp15.scr_el3 & SCR_FIQ) {
6200 /* FIQ routed to monitor mode */
6201 new_mode = ARM_CPU_MODE_MON;
6202 }
b5ff1b31
FB
6203 offset = 4;
6204 break;
dbe9d163
FA
6205 case EXCP_SMC:
6206 new_mode = ARM_CPU_MODE_MON;
6207 addr = 0x08;
6208 mask = CPSR_A | CPSR_I | CPSR_F;
6209 offset = 0;
6210 break;
b5ff1b31 6211 default:
a47dddd7 6212 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
6213 return; /* Never happens. Keep compiler happy. */
6214 }
e89e51a1
FA
6215
6216 if (new_mode == ARM_CPU_MODE_MON) {
6217 addr += env->cp15.mvbar;
137feaa9 6218 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 6219 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 6220 addr += 0xffff0000;
8641136c
NR
6221 } else {
6222 /* ARM v7 architectures provide a vector base address register to remap
6223 * the interrupt vector table.
e89e51a1 6224 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
6225 * Note: only bits 31:5 are valid.
6226 */
fb6c91ba 6227 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 6228 }
dbe9d163
FA
6229
6230 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
6231 env->cp15.scr_el3 &= ~SCR_NS;
6232 }
6233
b5ff1b31 6234 switch_mode (env, new_mode);
662cefb7
PM
6235 /* For exceptions taken to AArch32 we must clear the SS bit in both
6236 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
6237 */
6238 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 6239 env->spsr = cpsr_read(env);
9ee6e8bb
PB
6240 /* Clear IT bits. */
6241 env->condexec_bits = 0;
30a8cac1 6242 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 6243 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
73462ddd
PC
6244 /* Set new mode endianness */
6245 env->uncached_cpsr &= ~CPSR_E;
6246 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
6247 env->uncached_cpsr |= ~CPSR_E;
6248 }
4cc35614 6249 env->daif |= mask;
be5e7a76
DES
6250 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
6251 * and we should just guard the thumb mode on V4 */
6252 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 6253 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 6254 }
b5ff1b31
FB
6255 env->regs[14] = env->regs[15] + offset;
6256 env->regs[15] = addr;
b5ff1b31
FB
6257}
6258
966f758c
PM
6259/* Handle exception entry to a target EL which is using AArch64 */
6260static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
6261{
6262 ARMCPU *cpu = ARM_CPU(cs);
6263 CPUARMState *env = &cpu->env;
6264 unsigned int new_el = env->exception.target_el;
6265 target_ulong addr = env->cp15.vbar_el[new_el];
6266 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
6267
6268 if (arm_current_el(env) < new_el) {
3d6f7617
PM
6269 /* Entry vector offset depends on whether the implemented EL
6270 * immediately lower than the target level is using AArch32 or AArch64
6271 */
6272 bool is_aa64;
6273
6274 switch (new_el) {
6275 case 3:
6276 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
6277 break;
6278 case 2:
6279 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
6280 break;
6281 case 1:
6282 is_aa64 = is_a64(env);
6283 break;
6284 default:
6285 g_assert_not_reached();
6286 }
6287
6288 if (is_aa64) {
f3a9b694
PM
6289 addr += 0x400;
6290 } else {
6291 addr += 0x600;
6292 }
6293 } else if (pstate_read(env) & PSTATE_SP) {
6294 addr += 0x200;
6295 }
6296
f3a9b694
PM
6297 switch (cs->exception_index) {
6298 case EXCP_PREFETCH_ABORT:
6299 case EXCP_DATA_ABORT:
6300 env->cp15.far_el[new_el] = env->exception.vaddress;
6301 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
6302 env->cp15.far_el[new_el]);
6303 /* fall through */
6304 case EXCP_BKPT:
6305 case EXCP_UDEF:
6306 case EXCP_SWI:
6307 case EXCP_HVC:
6308 case EXCP_HYP_TRAP:
6309 case EXCP_SMC:
6310 env->cp15.esr_el[new_el] = env->exception.syndrome;
6311 break;
6312 case EXCP_IRQ:
6313 case EXCP_VIRQ:
6314 addr += 0x80;
6315 break;
6316 case EXCP_FIQ:
6317 case EXCP_VFIQ:
6318 addr += 0x100;
6319 break;
6320 case EXCP_SEMIHOST:
6321 qemu_log_mask(CPU_LOG_INT,
6322 "...handling as semihosting call 0x%" PRIx64 "\n",
6323 env->xregs[0]);
6324 env->xregs[0] = do_arm_semihosting(env);
6325 return;
6326 default:
6327 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
6328 }
6329
6330 if (is_a64(env)) {
6331 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
6332 aarch64_save_sp(env, arm_current_el(env));
6333 env->elr_el[new_el] = env->pc;
6334 } else {
6335 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
6336 if (!env->thumb) {
6337 env->cp15.esr_el[new_el] |= 1 << 25;
6338 }
6339 env->elr_el[new_el] = env->regs[15];
6340
6341 aarch64_sync_32_to_64(env);
6342
6343 env->condexec_bits = 0;
6344 }
6345 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
6346 env->elr_el[new_el]);
6347
6348 pstate_write(env, PSTATE_DAIF | new_mode);
6349 env->aarch64 = 1;
6350 aarch64_restore_sp(env, new_el);
6351
6352 env->pc = addr;
6353
6354 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
6355 new_el, env->pc, pstate_read(env));
966f758c
PM
6356}
6357
904c04de
PM
6358static inline bool check_for_semihosting(CPUState *cs)
6359{
6360 /* Check whether this exception is a semihosting call; if so
6361 * then handle it and return true; otherwise return false.
6362 */
6363 ARMCPU *cpu = ARM_CPU(cs);
6364 CPUARMState *env = &cpu->env;
6365
6366 if (is_a64(env)) {
6367 if (cs->exception_index == EXCP_SEMIHOST) {
6368 /* This is always the 64-bit semihosting exception.
6369 * The "is this usermode" and "is semihosting enabled"
6370 * checks have been done at translate time.
6371 */
6372 qemu_log_mask(CPU_LOG_INT,
6373 "...handling as semihosting call 0x%" PRIx64 "\n",
6374 env->xregs[0]);
6375 env->xregs[0] = do_arm_semihosting(env);
6376 return true;
6377 }
6378 return false;
6379 } else {
6380 uint32_t imm;
6381
6382 /* Only intercept calls from privileged modes, to provide some
6383 * semblance of security.
6384 */
6385 if (!semihosting_enabled() ||
6386 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)) {
6387 return false;
6388 }
6389
6390 switch (cs->exception_index) {
6391 case EXCP_SWI:
6392 /* Check for semihosting interrupt. */
6393 if (env->thumb) {
f9fd40eb 6394 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
904c04de
PM
6395 & 0xff;
6396 if (imm == 0xab) {
6397 break;
6398 }
6399 } else {
f9fd40eb 6400 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
904c04de
PM
6401 & 0xffffff;
6402 if (imm == 0x123456) {
6403 break;
6404 }
6405 }
6406 return false;
6407 case EXCP_BKPT:
6408 /* See if this is a semihosting syscall. */
6409 if (env->thumb) {
f9fd40eb 6410 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
904c04de
PM
6411 & 0xff;
6412 if (imm == 0xab) {
6413 env->regs[15] += 2;
6414 break;
6415 }
6416 }
6417 return false;
6418 default:
6419 return false;
6420 }
6421
6422 qemu_log_mask(CPU_LOG_INT,
6423 "...handling as semihosting call 0x%x\n",
6424 env->regs[0]);
6425 env->regs[0] = do_arm_semihosting(env);
6426 return true;
6427 }
6428}
6429
966f758c
PM
6430/* Handle a CPU exception for A and R profile CPUs.
6431 * Do any appropriate logging, handle PSCI calls, and then hand off
6432 * to the AArch64-entry or AArch32-entry function depending on the
6433 * target exception level's register width.
6434 */
6435void arm_cpu_do_interrupt(CPUState *cs)
6436{
6437 ARMCPU *cpu = ARM_CPU(cs);
6438 CPUARMState *env = &cpu->env;
6439 unsigned int new_el = env->exception.target_el;
6440
6441 assert(!IS_M(env));
6442
6443 arm_log_exception(cs->exception_index);
6444 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
6445 new_el);
6446 if (qemu_loglevel_mask(CPU_LOG_INT)
6447 && !excp_is_internal(cs->exception_index)) {
6448 qemu_log_mask(CPU_LOG_INT, "...with ESR %x/0x%" PRIx32 "\n",
6449 env->exception.syndrome >> ARM_EL_EC_SHIFT,
6450 env->exception.syndrome);
6451 }
6452
6453 if (arm_is_psci_call(cpu, cs->exception_index)) {
6454 arm_handle_psci_call(cpu);
6455 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
6456 return;
6457 }
6458
904c04de
PM
6459 /* Semihosting semantics depend on the register width of the
6460 * code that caused the exception, not the target exception level,
6461 * so must be handled here.
966f758c 6462 */
904c04de
PM
6463 if (check_for_semihosting(cs)) {
6464 return;
6465 }
6466
6467 assert(!excp_is_internal(cs->exception_index));
6468 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
6469 arm_cpu_do_interrupt_aarch64(cs);
6470 } else {
6471 arm_cpu_do_interrupt_aarch32(cs);
6472 }
f3a9b694
PM
6473
6474 if (!kvm_enabled()) {
6475 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
6476 }
6477}
0480f69a
PM
6478
6479/* Return the exception level which controls this address translation regime */
6480static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
6481{
6482 switch (mmu_idx) {
6483 case ARMMMUIdx_S2NS:
6484 case ARMMMUIdx_S1E2:
6485 return 2;
6486 case ARMMMUIdx_S1E3:
6487 return 3;
6488 case ARMMMUIdx_S1SE0:
6489 return arm_el_is_aa64(env, 3) ? 1 : 3;
6490 case ARMMMUIdx_S1SE1:
6491 case ARMMMUIdx_S1NSE0:
6492 case ARMMMUIdx_S1NSE1:
6493 return 1;
6494 default:
6495 g_assert_not_reached();
6496 }
6497}
6498
8bf5b6a9
PM
6499/* Return true if this address translation regime is secure */
6500static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
6501{
6502 switch (mmu_idx) {
6503 case ARMMMUIdx_S12NSE0:
6504 case ARMMMUIdx_S12NSE1:
6505 case ARMMMUIdx_S1NSE0:
6506 case ARMMMUIdx_S1NSE1:
6507 case ARMMMUIdx_S1E2:
6508 case ARMMMUIdx_S2NS:
6509 return false;
6510 case ARMMMUIdx_S1E3:
6511 case ARMMMUIdx_S1SE0:
6512 case ARMMMUIdx_S1SE1:
6513 return true;
6514 default:
6515 g_assert_not_reached();
6516 }
6517}
6518
0480f69a
PM
6519/* Return the SCTLR value which controls this address translation regime */
6520static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
6521{
6522 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
6523}
6524
6525/* Return true if the specified stage of address translation is disabled */
6526static inline bool regime_translation_disabled(CPUARMState *env,
6527 ARMMMUIdx mmu_idx)
6528{
6529 if (mmu_idx == ARMMMUIdx_S2NS) {
6530 return (env->cp15.hcr_el2 & HCR_VM) == 0;
6531 }
6532 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
6533}
6534
73462ddd
PC
6535static inline bool regime_translation_big_endian(CPUARMState *env,
6536 ARMMMUIdx mmu_idx)
6537{
6538 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
6539}
6540
0480f69a
PM
6541/* Return the TCR controlling this translation regime */
6542static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
6543{
6544 if (mmu_idx == ARMMMUIdx_S2NS) {
68e9c2fe 6545 return &env->cp15.vtcr_el2;
0480f69a
PM
6546 }
6547 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
6548}
6549
aef878be
GB
6550/* Return the TTBR associated with this translation regime */
6551static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
6552 int ttbrn)
6553{
6554 if (mmu_idx == ARMMMUIdx_S2NS) {
b698e9cf 6555 return env->cp15.vttbr_el2;
aef878be
GB
6556 }
6557 if (ttbrn == 0) {
6558 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
6559 } else {
6560 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
6561 }
6562}
6563
0480f69a
PM
6564/* Return true if the translation regime is using LPAE format page tables */
6565static inline bool regime_using_lpae_format(CPUARMState *env,
6566 ARMMMUIdx mmu_idx)
6567{
6568 int el = regime_el(env, mmu_idx);
6569 if (el == 2 || arm_el_is_aa64(env, el)) {
6570 return true;
6571 }
6572 if (arm_feature(env, ARM_FEATURE_LPAE)
6573 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
6574 return true;
6575 }
6576 return false;
6577}
6578
deb2db99
AR
6579/* Returns true if the stage 1 translation regime is using LPAE format page
6580 * tables. Used when raising alignment exceptions, whose FSR changes depending
6581 * on whether the long or short descriptor format is in use. */
6582bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 6583{
deb2db99
AR
6584 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6585 mmu_idx += ARMMMUIdx_S1NSE0;
6586 }
6587
30901475
AB
6588 return regime_using_lpae_format(env, mmu_idx);
6589}
6590
0480f69a
PM
6591static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
6592{
6593 switch (mmu_idx) {
6594 case ARMMMUIdx_S1SE0:
6595 case ARMMMUIdx_S1NSE0:
6596 return true;
6597 default:
6598 return false;
6599 case ARMMMUIdx_S12NSE0:
6600 case ARMMMUIdx_S12NSE1:
6601 g_assert_not_reached();
6602 }
6603}
6604
0fbf5238
AJ
6605/* Translate section/page access permissions to page
6606 * R/W protection flags
d76951b6
AJ
6607 *
6608 * @env: CPUARMState
6609 * @mmu_idx: MMU index indicating required translation regime
6610 * @ap: The 3-bit access permissions (AP[2:0])
6611 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
6612 */
6613static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
6614 int ap, int domain_prot)
6615{
554b0b09
PM
6616 bool is_user = regime_is_user(env, mmu_idx);
6617
6618 if (domain_prot == 3) {
6619 return PAGE_READ | PAGE_WRITE;
6620 }
6621
554b0b09
PM
6622 switch (ap) {
6623 case 0:
6624 if (arm_feature(env, ARM_FEATURE_V7)) {
6625 return 0;
6626 }
554b0b09
PM
6627 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
6628 case SCTLR_S:
6629 return is_user ? 0 : PAGE_READ;
6630 case SCTLR_R:
6631 return PAGE_READ;
6632 default:
6633 return 0;
6634 }
6635 case 1:
6636 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6637 case 2:
87c3d486 6638 if (is_user) {
0fbf5238 6639 return PAGE_READ;
87c3d486 6640 } else {
554b0b09 6641 return PAGE_READ | PAGE_WRITE;
87c3d486 6642 }
554b0b09
PM
6643 case 3:
6644 return PAGE_READ | PAGE_WRITE;
6645 case 4: /* Reserved. */
6646 return 0;
6647 case 5:
0fbf5238 6648 return is_user ? 0 : PAGE_READ;
554b0b09 6649 case 6:
0fbf5238 6650 return PAGE_READ;
554b0b09 6651 case 7:
87c3d486 6652 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 6653 return 0;
87c3d486 6654 }
0fbf5238 6655 return PAGE_READ;
554b0b09 6656 default:
0fbf5238 6657 g_assert_not_reached();
554b0b09 6658 }
b5ff1b31
FB
6659}
6660
d76951b6
AJ
6661/* Translate section/page access permissions to page
6662 * R/W protection flags.
6663 *
d76951b6 6664 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 6665 * @is_user: TRUE if accessing from PL0
d76951b6 6666 */
d8e052b3 6667static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 6668{
d76951b6
AJ
6669 switch (ap) {
6670 case 0:
6671 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6672 case 1:
6673 return PAGE_READ | PAGE_WRITE;
6674 case 2:
6675 return is_user ? 0 : PAGE_READ;
6676 case 3:
6677 return PAGE_READ;
6678 default:
6679 g_assert_not_reached();
6680 }
6681}
6682
d8e052b3
AJ
6683static inline int
6684simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
6685{
6686 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
6687}
6688
6ab1a5ee
EI
6689/* Translate S2 section/page access permissions to protection flags
6690 *
6691 * @env: CPUARMState
6692 * @s2ap: The 2-bit stage2 access permissions (S2AP)
6693 * @xn: XN (execute-never) bit
6694 */
6695static int get_S2prot(CPUARMState *env, int s2ap, int xn)
6696{
6697 int prot = 0;
6698
6699 if (s2ap & 1) {
6700 prot |= PAGE_READ;
6701 }
6702 if (s2ap & 2) {
6703 prot |= PAGE_WRITE;
6704 }
6705 if (!xn) {
6706 prot |= PAGE_EXEC;
6707 }
6708 return prot;
6709}
6710
d8e052b3
AJ
6711/* Translate section/page access permissions to protection flags
6712 *
6713 * @env: CPUARMState
6714 * @mmu_idx: MMU index indicating required translation regime
6715 * @is_aa64: TRUE if AArch64
6716 * @ap: The 2-bit simple AP (AP[2:1])
6717 * @ns: NS (non-secure) bit
6718 * @xn: XN (execute-never) bit
6719 * @pxn: PXN (privileged execute-never) bit
6720 */
6721static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
6722 int ap, int ns, int xn, int pxn)
6723{
6724 bool is_user = regime_is_user(env, mmu_idx);
6725 int prot_rw, user_rw;
6726 bool have_wxn;
6727 int wxn = 0;
6728
6729 assert(mmu_idx != ARMMMUIdx_S2NS);
6730
6731 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
6732 if (is_user) {
6733 prot_rw = user_rw;
6734 } else {
6735 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
6736 }
6737
6738 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
6739 return prot_rw;
6740 }
6741
6742 /* TODO have_wxn should be replaced with
6743 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
6744 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
6745 * compatible processors have EL2, which is required for [U]WXN.
6746 */
6747 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
6748
6749 if (have_wxn) {
6750 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
6751 }
6752
6753 if (is_aa64) {
6754 switch (regime_el(env, mmu_idx)) {
6755 case 1:
6756 if (!is_user) {
6757 xn = pxn || (user_rw & PAGE_WRITE);
6758 }
6759 break;
6760 case 2:
6761 case 3:
6762 break;
6763 }
6764 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6765 switch (regime_el(env, mmu_idx)) {
6766 case 1:
6767 case 3:
6768 if (is_user) {
6769 xn = xn || !(user_rw & PAGE_READ);
6770 } else {
6771 int uwxn = 0;
6772 if (have_wxn) {
6773 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6774 }
6775 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6776 (uwxn && (user_rw & PAGE_WRITE));
6777 }
6778 break;
6779 case 2:
6780 break;
6781 }
6782 } else {
6783 xn = wxn = 0;
6784 }
6785
6786 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6787 return prot_rw;
6788 }
6789 return prot_rw | PAGE_EXEC;
6790}
6791
0480f69a
PM
6792static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6793 uint32_t *table, uint32_t address)
b2fa1797 6794{
0480f69a 6795 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 6796 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 6797
11f136ee
FA
6798 if (address & tcr->mask) {
6799 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
6800 /* Translation table walk disabled for TTBR1 */
6801 return false;
6802 }
aef878be 6803 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 6804 } else {
11f136ee 6805 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
6806 /* Translation table walk disabled for TTBR0 */
6807 return false;
6808 }
aef878be 6809 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
6810 }
6811 *table |= (address >> 18) & 0x3ffc;
6812 return true;
b2fa1797
PB
6813}
6814
37785977
EI
6815/* Translate a S1 pagetable walk through S2 if needed. */
6816static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
6817 hwaddr addr, MemTxAttrs txattrs,
6818 uint32_t *fsr,
6819 ARMMMUFaultInfo *fi)
6820{
6821 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
6822 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
6823 target_ulong s2size;
6824 hwaddr s2pa;
6825 int s2prot;
6826 int ret;
6827
6828 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
6829 &txattrs, &s2prot, &s2size, fsr, fi);
6830 if (ret) {
6831 fi->s2addr = addr;
6832 fi->stage2 = true;
6833 fi->s1ptw = true;
6834 return ~0;
6835 }
6836 addr = s2pa;
6837 }
6838 return addr;
6839}
6840
ebca90e4
PM
6841/* All loads done in the course of a page table walk go through here.
6842 * TODO: rather than ignoring errors from physical memory reads (which
6843 * are external aborts in ARM terminology) we should propagate this
6844 * error out so that we can turn it into a Data Abort if this walk
6845 * was being done for a CPU load/store or an address translation instruction
6846 * (but not if it was for a debug access).
6847 */
a614e698
EI
6848static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6849 ARMMMUIdx mmu_idx, uint32_t *fsr,
6850 ARMMMUFaultInfo *fi)
ebca90e4 6851{
a614e698
EI
6852 ARMCPU *cpu = ARM_CPU(cs);
6853 CPUARMState *env = &cpu->env;
ebca90e4 6854 MemTxAttrs attrs = {};
5ce4ff65 6855 AddressSpace *as;
ebca90e4
PM
6856
6857 attrs.secure = is_secure;
5ce4ff65 6858 as = arm_addressspace(cs, attrs);
a614e698
EI
6859 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6860 if (fi->s1ptw) {
6861 return 0;
6862 }
73462ddd
PC
6863 if (regime_translation_big_endian(env, mmu_idx)) {
6864 return address_space_ldl_be(as, addr, attrs, NULL);
6865 } else {
6866 return address_space_ldl_le(as, addr, attrs, NULL);
6867 }
ebca90e4
PM
6868}
6869
37785977
EI
6870static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6871 ARMMMUIdx mmu_idx, uint32_t *fsr,
6872 ARMMMUFaultInfo *fi)
ebca90e4 6873{
37785977
EI
6874 ARMCPU *cpu = ARM_CPU(cs);
6875 CPUARMState *env = &cpu->env;
ebca90e4 6876 MemTxAttrs attrs = {};
5ce4ff65 6877 AddressSpace *as;
ebca90e4
PM
6878
6879 attrs.secure = is_secure;
5ce4ff65 6880 as = arm_addressspace(cs, attrs);
37785977
EI
6881 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6882 if (fi->s1ptw) {
6883 return 0;
6884 }
73462ddd
PC
6885 if (regime_translation_big_endian(env, mmu_idx)) {
6886 return address_space_ldq_be(as, addr, attrs, NULL);
6887 } else {
6888 return address_space_ldq_le(as, addr, attrs, NULL);
6889 }
ebca90e4
PM
6890}
6891
b7cc4e82
PC
6892static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6893 int access_type, ARMMMUIdx mmu_idx,
6894 hwaddr *phys_ptr, int *prot,
e14b5a23
EI
6895 target_ulong *page_size, uint32_t *fsr,
6896 ARMMMUFaultInfo *fi)
b5ff1b31 6897{
70d74660 6898 CPUState *cs = CPU(arm_env_get_cpu(env));
b5ff1b31
FB
6899 int code;
6900 uint32_t table;
6901 uint32_t desc;
6902 int type;
6903 int ap;
e389be16 6904 int domain = 0;
dd4ebc2e 6905 int domain_prot;
a8170e5e 6906 hwaddr phys_addr;
0480f69a 6907 uint32_t dacr;
b5ff1b31 6908
9ee6e8bb
PB
6909 /* Pagetable walk. */
6910 /* Lookup l1 descriptor. */
0480f69a 6911 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
6912 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6913 code = 5;
6914 goto do_fault;
6915 }
a614e698
EI
6916 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6917 mmu_idx, fsr, fi);
9ee6e8bb 6918 type = (desc & 3);
dd4ebc2e 6919 domain = (desc >> 5) & 0x0f;
0480f69a
PM
6920 if (regime_el(env, mmu_idx) == 1) {
6921 dacr = env->cp15.dacr_ns;
6922 } else {
6923 dacr = env->cp15.dacr_s;
6924 }
6925 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 6926 if (type == 0) {
601d70b9 6927 /* Section translation fault. */
9ee6e8bb
PB
6928 code = 5;
6929 goto do_fault;
6930 }
dd4ebc2e 6931 if (domain_prot == 0 || domain_prot == 2) {
9ee6e8bb
PB
6932 if (type == 2)
6933 code = 9; /* Section domain fault. */
6934 else
6935 code = 11; /* Page domain fault. */
6936 goto do_fault;
6937 }
6938 if (type == 2) {
6939 /* 1Mb section. */
6940 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6941 ap = (desc >> 10) & 3;
6942 code = 13;
d4c430a8 6943 *page_size = 1024 * 1024;
9ee6e8bb
PB
6944 } else {
6945 /* Lookup l2 entry. */
554b0b09
PM
6946 if (type == 1) {
6947 /* Coarse pagetable. */
6948 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6949 } else {
6950 /* Fine pagetable. */
6951 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6952 }
a614e698
EI
6953 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6954 mmu_idx, fsr, fi);
9ee6e8bb
PB
6955 switch (desc & 3) {
6956 case 0: /* Page translation fault. */
6957 code = 7;
6958 goto do_fault;
6959 case 1: /* 64k page. */
6960 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6961 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 6962 *page_size = 0x10000;
ce819861 6963 break;
9ee6e8bb
PB
6964 case 2: /* 4k page. */
6965 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 6966 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 6967 *page_size = 0x1000;
ce819861 6968 break;
fc1891c7 6969 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 6970 if (type == 1) {
fc1891c7
PM
6971 /* ARMv6/XScale extended small page format */
6972 if (arm_feature(env, ARM_FEATURE_XSCALE)
6973 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 6974 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 6975 *page_size = 0x1000;
554b0b09 6976 } else {
fc1891c7
PM
6977 /* UNPREDICTABLE in ARMv5; we choose to take a
6978 * page translation fault.
6979 */
554b0b09
PM
6980 code = 7;
6981 goto do_fault;
6982 }
6983 } else {
6984 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 6985 *page_size = 0x400;
554b0b09 6986 }
9ee6e8bb 6987 ap = (desc >> 4) & 3;
ce819861
PB
6988 break;
6989 default:
9ee6e8bb
PB
6990 /* Never happens, but compiler isn't smart enough to tell. */
6991 abort();
ce819861 6992 }
9ee6e8bb
PB
6993 code = 15;
6994 }
0fbf5238
AJ
6995 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6996 *prot |= *prot ? PAGE_EXEC : 0;
6997 if (!(*prot & (1 << access_type))) {
9ee6e8bb
PB
6998 /* Access permission fault. */
6999 goto do_fault;
7000 }
7001 *phys_ptr = phys_addr;
b7cc4e82 7002 return false;
9ee6e8bb 7003do_fault:
b7cc4e82
PC
7004 *fsr = code | (domain << 4);
7005 return true;
9ee6e8bb
PB
7006}
7007
b7cc4e82
PC
7008static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
7009 int access_type, ARMMMUIdx mmu_idx,
7010 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
7011 target_ulong *page_size, uint32_t *fsr,
7012 ARMMMUFaultInfo *fi)
9ee6e8bb 7013{
70d74660 7014 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb
PB
7015 int code;
7016 uint32_t table;
7017 uint32_t desc;
7018 uint32_t xn;
de9b05b8 7019 uint32_t pxn = 0;
9ee6e8bb
PB
7020 int type;
7021 int ap;
de9b05b8 7022 int domain = 0;
dd4ebc2e 7023 int domain_prot;
a8170e5e 7024 hwaddr phys_addr;
0480f69a 7025 uint32_t dacr;
8bf5b6a9 7026 bool ns;
9ee6e8bb
PB
7027
7028 /* Pagetable walk. */
7029 /* Lookup l1 descriptor. */
0480f69a 7030 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16
FA
7031 /* Section translation fault if page walk is disabled by PD0 or PD1 */
7032 code = 5;
7033 goto do_fault;
7034 }
a614e698
EI
7035 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7036 mmu_idx, fsr, fi);
9ee6e8bb 7037 type = (desc & 3);
de9b05b8
PM
7038 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
7039 /* Section translation fault, or attempt to use the encoding
7040 * which is Reserved on implementations without PXN.
7041 */
9ee6e8bb 7042 code = 5;
9ee6e8bb 7043 goto do_fault;
de9b05b8
PM
7044 }
7045 if ((type == 1) || !(desc & (1 << 18))) {
7046 /* Page or Section. */
dd4ebc2e 7047 domain = (desc >> 5) & 0x0f;
9ee6e8bb 7048 }
0480f69a
PM
7049 if (regime_el(env, mmu_idx) == 1) {
7050 dacr = env->cp15.dacr_ns;
7051 } else {
7052 dacr = env->cp15.dacr_s;
7053 }
7054 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 7055 if (domain_prot == 0 || domain_prot == 2) {
de9b05b8 7056 if (type != 1) {
9ee6e8bb 7057 code = 9; /* Section domain fault. */
de9b05b8 7058 } else {
9ee6e8bb 7059 code = 11; /* Page domain fault. */
de9b05b8 7060 }
9ee6e8bb
PB
7061 goto do_fault;
7062 }
de9b05b8 7063 if (type != 1) {
9ee6e8bb
PB
7064 if (desc & (1 << 18)) {
7065 /* Supersection. */
7066 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
7067 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
7068 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 7069 *page_size = 0x1000000;
b5ff1b31 7070 } else {
9ee6e8bb
PB
7071 /* Section. */
7072 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 7073 *page_size = 0x100000;
b5ff1b31 7074 }
9ee6e8bb
PB
7075 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
7076 xn = desc & (1 << 4);
de9b05b8 7077 pxn = desc & 1;
9ee6e8bb 7078 code = 13;
8bf5b6a9 7079 ns = extract32(desc, 19, 1);
9ee6e8bb 7080 } else {
de9b05b8
PM
7081 if (arm_feature(env, ARM_FEATURE_PXN)) {
7082 pxn = (desc >> 2) & 1;
7083 }
8bf5b6a9 7084 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
7085 /* Lookup l2 entry. */
7086 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698
EI
7087 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7088 mmu_idx, fsr, fi);
9ee6e8bb
PB
7089 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
7090 switch (desc & 3) {
7091 case 0: /* Page translation fault. */
7092 code = 7;
b5ff1b31 7093 goto do_fault;
9ee6e8bb
PB
7094 case 1: /* 64k page. */
7095 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
7096 xn = desc & (1 << 15);
d4c430a8 7097 *page_size = 0x10000;
9ee6e8bb
PB
7098 break;
7099 case 2: case 3: /* 4k page. */
7100 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
7101 xn = desc & 1;
d4c430a8 7102 *page_size = 0x1000;
9ee6e8bb
PB
7103 break;
7104 default:
7105 /* Never happens, but compiler isn't smart enough to tell. */
7106 abort();
b5ff1b31 7107 }
9ee6e8bb
PB
7108 code = 15;
7109 }
dd4ebc2e 7110 if (domain_prot == 3) {
c0034328
JR
7111 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
7112 } else {
0480f69a 7113 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
7114 xn = 1;
7115 }
c0034328
JR
7116 if (xn && access_type == 2)
7117 goto do_fault;
9ee6e8bb 7118
d76951b6
AJ
7119 if (arm_feature(env, ARM_FEATURE_V6K) &&
7120 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
7121 /* The simplified model uses AP[0] as an access control bit. */
7122 if ((ap & 1) == 0) {
7123 /* Access flag fault. */
7124 code = (code == 15) ? 6 : 3;
7125 goto do_fault;
7126 }
7127 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
7128 } else {
7129 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 7130 }
0fbf5238
AJ
7131 if (*prot && !xn) {
7132 *prot |= PAGE_EXEC;
7133 }
7134 if (!(*prot & (1 << access_type))) {
c0034328
JR
7135 /* Access permission fault. */
7136 goto do_fault;
7137 }
3ad493fc 7138 }
8bf5b6a9
PM
7139 if (ns) {
7140 /* The NS bit will (as required by the architecture) have no effect if
7141 * the CPU doesn't support TZ or this is a non-secure translation
7142 * regime, because the attribute will already be non-secure.
7143 */
7144 attrs->secure = false;
7145 }
9ee6e8bb 7146 *phys_ptr = phys_addr;
b7cc4e82 7147 return false;
b5ff1b31 7148do_fault:
b7cc4e82
PC
7149 *fsr = code | (domain << 4);
7150 return true;
b5ff1b31
FB
7151}
7152
3dde962f
PM
7153/* Fault type for long-descriptor MMU fault reporting; this corresponds
7154 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
7155 */
7156typedef enum {
7157 translation_fault = 1,
7158 access_fault = 2,
7159 permission_fault = 3,
7160} MMUFaultType;
7161
1853d5a9 7162/*
a0e966c9 7163 * check_s2_mmu_setup
1853d5a9
EI
7164 * @cpu: ARMCPU
7165 * @is_aa64: True if the translation regime is in AArch64 state
7166 * @startlevel: Suggested starting level
7167 * @inputsize: Bitsize of IPAs
7168 * @stride: Page-table stride (See the ARM ARM)
7169 *
a0e966c9
EI
7170 * Returns true if the suggested S2 translation parameters are OK and
7171 * false otherwise.
1853d5a9 7172 */
a0e966c9
EI
7173static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
7174 int inputsize, int stride)
1853d5a9 7175{
98d68ec2
EI
7176 const int grainsize = stride + 3;
7177 int startsizecheck;
7178
1853d5a9
EI
7179 /* Negative levels are never allowed. */
7180 if (level < 0) {
7181 return false;
7182 }
7183
98d68ec2
EI
7184 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
7185 if (startsizecheck < 1 || startsizecheck > stride + 4) {
7186 return false;
7187 }
7188
1853d5a9 7189 if (is_aa64) {
3526423e 7190 CPUARMState *env = &cpu->env;
1853d5a9
EI
7191 unsigned int pamax = arm_pamax(cpu);
7192
7193 switch (stride) {
7194 case 13: /* 64KB Pages. */
7195 if (level == 0 || (level == 1 && pamax <= 42)) {
7196 return false;
7197 }
7198 break;
7199 case 11: /* 16KB Pages. */
7200 if (level == 0 || (level == 1 && pamax <= 40)) {
7201 return false;
7202 }
7203 break;
7204 case 9: /* 4KB Pages. */
7205 if (level == 0 && pamax <= 42) {
7206 return false;
7207 }
7208 break;
7209 default:
7210 g_assert_not_reached();
7211 }
3526423e
EI
7212
7213 /* Inputsize checks. */
7214 if (inputsize > pamax &&
7215 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
7216 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
7217 return false;
7218 }
1853d5a9 7219 } else {
1853d5a9
EI
7220 /* AArch32 only supports 4KB pages. Assert on that. */
7221 assert(stride == 9);
7222
7223 if (level == 0) {
7224 return false;
7225 }
1853d5a9
EI
7226 }
7227 return true;
7228}
7229
b7cc4e82
PC
7230static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
7231 int access_type, ARMMMUIdx mmu_idx,
7232 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
e14b5a23
EI
7233 target_ulong *page_size_ptr, uint32_t *fsr,
7234 ARMMMUFaultInfo *fi)
3dde962f 7235{
1853d5a9
EI
7236 ARMCPU *cpu = arm_env_get_cpu(env);
7237 CPUState *cs = CPU(cpu);
3dde962f
PM
7238 /* Read an LPAE long-descriptor translation table. */
7239 MMUFaultType fault_type = translation_fault;
1b4093ea 7240 uint32_t level;
0c5fbf3b 7241 uint32_t epd = 0;
1f4c8c18 7242 int32_t t0sz, t1sz;
2c8dd318 7243 uint32_t tg;
3dde962f
PM
7244 uint64_t ttbr;
7245 int ttbr_select;
2c8dd318 7246 hwaddr descaddr, descmask;
3dde962f
PM
7247 uint32_t tableattrs;
7248 target_ulong page_size;
7249 uint32_t attrs;
973a5434 7250 int32_t stride = 9;
1b4093ea 7251 int32_t va_size;
4ca6a051 7252 int inputsize;
2c8dd318 7253 int32_t tbi = 0;
0480f69a 7254 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 7255 int ap, ns, xn, pxn;
88e8add8
GB
7256 uint32_t el = regime_el(env, mmu_idx);
7257 bool ttbr1_valid = true;
6109769a 7258 uint64_t descaddrmask;
0480f69a
PM
7259
7260 /* TODO:
88e8add8
GB
7261 * This code does not handle the different format TCR for VTCR_EL2.
7262 * This code also does not support shareability levels.
7263 * Attribute and permission bit handling should also be checked when adding
7264 * support for those page table walks.
0480f69a 7265 */
88e8add8 7266 if (arm_el_is_aa64(env, el)) {
1b4093ea 7267 level = 0;
2c8dd318 7268 va_size = 64;
88e8add8 7269 if (el > 1) {
1edee470
EI
7270 if (mmu_idx != ARMMMUIdx_S2NS) {
7271 tbi = extract64(tcr->raw_tcr, 20, 1);
7272 }
88e8add8
GB
7273 } else {
7274 if (extract64(address, 55, 1)) {
7275 tbi = extract64(tcr->raw_tcr, 38, 1);
7276 } else {
7277 tbi = extract64(tcr->raw_tcr, 37, 1);
7278 }
7279 }
2c8dd318 7280 tbi *= 8;
88e8add8
GB
7281
7282 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
7283 * invalid.
7284 */
7285 if (el > 1) {
7286 ttbr1_valid = false;
7287 }
d0a2cbce 7288 } else {
1b4093ea
SS
7289 level = 1;
7290 va_size = 32;
d0a2cbce
PM
7291 /* There is no TTBR1 for EL2 */
7292 if (el == 2) {
7293 ttbr1_valid = false;
7294 }
2c8dd318 7295 }
3dde962f
PM
7296
7297 /* Determine whether this address is in the region controlled by
7298 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
7299 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
7300 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
7301 */
0480f69a 7302 if (va_size == 64) {
4ee38098
EI
7303 /* AArch64 translation. */
7304 t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
7305 t0sz = MIN(t0sz, 39);
7306 t0sz = MAX(t0sz, 16);
4ee38098
EI
7307 } else if (mmu_idx != ARMMMUIdx_S2NS) {
7308 /* AArch32 stage 1 translation. */
7309 t0sz = extract32(tcr->raw_tcr, 0, 3);
7310 } else {
7311 /* AArch32 stage 2 translation. */
7312 bool sext = extract32(tcr->raw_tcr, 4, 1);
7313 bool sign = extract32(tcr->raw_tcr, 3, 1);
7314 t0sz = sextract32(tcr->raw_tcr, 0, 4);
7315
7316 /* If the sign-extend bit is not the same as t0sz[3], the result
7317 * is unpredictable. Flag this as a guest error. */
7318 if (sign != sext) {
7319 qemu_log_mask(LOG_GUEST_ERROR,
7320 "AArch32: VTCR.S / VTCR.T0SZ[3] missmatch\n");
7321 }
2c8dd318 7322 }
1f4c8c18 7323 t1sz = extract32(tcr->raw_tcr, 16, 6);
0480f69a 7324 if (va_size == 64) {
2c8dd318
RH
7325 t1sz = MIN(t1sz, 39);
7326 t1sz = MAX(t1sz, 16);
7327 }
7328 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
3dde962f
PM
7329 /* there is a ttbr0 region and we are in it (high bits all zero) */
7330 ttbr_select = 0;
88e8add8
GB
7331 } else if (ttbr1_valid && t1sz &&
7332 !extract64(~address, va_size - t1sz, t1sz - tbi)) {
3dde962f
PM
7333 /* there is a ttbr1 region and we are in it (high bits all one) */
7334 ttbr_select = 1;
7335 } else if (!t0sz) {
7336 /* ttbr0 region is "everything not in the ttbr1 region" */
7337 ttbr_select = 0;
88e8add8 7338 } else if (!t1sz && ttbr1_valid) {
3dde962f
PM
7339 /* ttbr1 region is "everything not in the ttbr0 region" */
7340 ttbr_select = 1;
7341 } else {
7342 /* in the gap between the two regions, this is a Translation fault */
7343 fault_type = translation_fault;
7344 goto do_fault;
7345 }
7346
7347 /* Note that QEMU ignores shareability and cacheability attributes,
7348 * so we don't need to do anything with the SH, ORGN, IRGN fields
7349 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
7350 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
7351 * implement any ASID-like capability so we can ignore it (instead
7352 * we will always flush the TLB any time the ASID is changed).
7353 */
7354 if (ttbr_select == 0) {
aef878be 7355 ttbr = regime_ttbr(env, mmu_idx, 0);
0c5fbf3b
EI
7356 if (el < 2) {
7357 epd = extract32(tcr->raw_tcr, 7, 1);
7358 }
4ca6a051 7359 inputsize = va_size - t0sz;
2c8dd318 7360
11f136ee 7361 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318 7362 if (tg == 1) { /* 64KB pages */
973a5434 7363 stride = 13;
2c8dd318
RH
7364 }
7365 if (tg == 2) { /* 16KB pages */
973a5434 7366 stride = 11;
2c8dd318 7367 }
3dde962f 7368 } else {
88e8add8
GB
7369 /* We should only be here if TTBR1 is valid */
7370 assert(ttbr1_valid);
7371
aef878be 7372 ttbr = regime_ttbr(env, mmu_idx, 1);
11f136ee 7373 epd = extract32(tcr->raw_tcr, 23, 1);
4ca6a051 7374 inputsize = va_size - t1sz;
2c8dd318 7375
11f136ee 7376 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318 7377 if (tg == 3) { /* 64KB pages */
973a5434 7378 stride = 13;
2c8dd318
RH
7379 }
7380 if (tg == 1) { /* 16KB pages */
973a5434 7381 stride = 11;
2c8dd318 7382 }
3dde962f
PM
7383 }
7384
0480f69a 7385 /* Here we should have set up all the parameters for the translation:
973a5434 7386 * va_size, inputsize, ttbr, epd, stride, tbi
0480f69a
PM
7387 */
7388
3dde962f 7389 if (epd) {
88e8add8
GB
7390 /* Translation table walk disabled => Translation fault on TLB miss
7391 * Note: This is always 0 on 64-bit EL2 and EL3.
7392 */
3dde962f
PM
7393 goto do_fault;
7394 }
7395
1853d5a9
EI
7396 if (mmu_idx != ARMMMUIdx_S2NS) {
7397 /* The starting level depends on the virtual address size (which can
7398 * be up to 48 bits) and the translation granule size. It indicates
7399 * the number of strides (stride bits at a time) needed to
7400 * consume the bits of the input address. In the pseudocode this is:
7401 * level = 4 - RoundUp((inputsize - grainsize) / stride)
7402 * where their 'inputsize' is our 'inputsize', 'grainsize' is
7403 * our 'stride + 3' and 'stride' is our 'stride'.
7404 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
7405 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
7406 * = 4 - (inputsize - 4) / stride;
7407 */
7408 level = 4 - (inputsize - 4) / stride;
7409 } else {
7410 /* For stage 2 translations the starting level is specified by the
7411 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
7412 */
1b4093ea
SS
7413 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
7414 uint32_t startlevel;
1853d5a9
EI
7415 bool ok;
7416
7417 if (va_size == 32 || stride == 9) {
7418 /* AArch32 or 4KB pages */
1b4093ea 7419 startlevel = 2 - sl0;
1853d5a9
EI
7420 } else {
7421 /* 16KB or 64KB pages */
1b4093ea 7422 startlevel = 3 - sl0;
1853d5a9
EI
7423 }
7424
7425 /* Check that the starting level is valid. */
1b4093ea
SS
7426 ok = check_s2_mmu_setup(cpu, va_size == 64, startlevel,
7427 inputsize, stride);
1853d5a9 7428 if (!ok) {
1853d5a9
EI
7429 fault_type = translation_fault;
7430 goto do_fault;
7431 }
1b4093ea 7432 level = startlevel;
1853d5a9 7433 }
3dde962f
PM
7434
7435 /* Clear the vaddr bits which aren't part of the within-region address,
7436 * so that we don't have to special case things when calculating the
7437 * first descriptor address.
7438 */
4ca6a051
EI
7439 if (va_size != inputsize) {
7440 address &= (1ULL << inputsize) - 1;
2c8dd318
RH
7441 }
7442
973a5434 7443 descmask = (1ULL << (stride + 3)) - 1;
3dde962f
PM
7444
7445 /* Now we can extract the actual base address from the TTBR */
2c8dd318 7446 descaddr = extract64(ttbr, 0, 48);
973a5434 7447 descaddr &= ~((1ULL << (inputsize - (stride * (4 - level)))) - 1);
3dde962f 7448
6109769a
PM
7449 /* The address field in the descriptor goes up to bit 39 for ARMv7
7450 * but up to bit 47 for ARMv8.
7451 */
7452 if (arm_feature(env, ARM_FEATURE_V8)) {
7453 descaddrmask = 0xfffffffff000ULL;
7454 } else {
7455 descaddrmask = 0xfffffff000ULL;
7456 }
7457
ebca90e4
PM
7458 /* Secure accesses start with the page table in secure memory and
7459 * can be downgraded to non-secure at any step. Non-secure accesses
7460 * remain non-secure. We implement this by just ORing in the NSTable/NS
7461 * bits at each step.
7462 */
7463 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
7464 for (;;) {
7465 uint64_t descriptor;
ebca90e4 7466 bool nstable;
3dde962f 7467
973a5434 7468 descaddr |= (address >> (stride * (4 - level))) & descmask;
2c8dd318 7469 descaddr &= ~7ULL;
ebca90e4 7470 nstable = extract32(tableattrs, 4, 1);
37785977
EI
7471 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi);
7472 if (fi->s1ptw) {
7473 goto do_fault;
7474 }
7475
3dde962f
PM
7476 if (!(descriptor & 1) ||
7477 (!(descriptor & 2) && (level == 3))) {
7478 /* Invalid, or the Reserved level 3 encoding */
7479 goto do_fault;
7480 }
6109769a 7481 descaddr = descriptor & descaddrmask;
3dde962f
PM
7482
7483 if ((descriptor & 2) && (level < 3)) {
7484 /* Table entry. The top five bits are attributes which may
7485 * propagate down through lower levels of the table (and
7486 * which are all arranged so that 0 means "no effect", so
7487 * we can gather them up by ORing in the bits at each level).
7488 */
7489 tableattrs |= extract64(descriptor, 59, 5);
7490 level++;
7491 continue;
7492 }
7493 /* Block entry at level 1 or 2, or page entry at level 3.
7494 * These are basically the same thing, although the number
7495 * of bits we pull in from the vaddr varies.
7496 */
973a5434 7497 page_size = (1ULL << ((stride * (4 - level)) + 3));
3dde962f 7498 descaddr |= (address & (page_size - 1));
6ab1a5ee 7499 /* Extract attributes from the descriptor */
d615efac
IC
7500 attrs = extract64(descriptor, 2, 10)
7501 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee
EI
7502
7503 if (mmu_idx == ARMMMUIdx_S2NS) {
7504 /* Stage 2 table descriptors do not include any attribute fields */
7505 break;
7506 }
7507 /* Merge in attributes from table descriptors */
3dde962f
PM
7508 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
7509 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
7510 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
7511 * means "force PL1 access only", which means forcing AP[1] to 0.
7512 */
7513 if (extract32(tableattrs, 2, 1)) {
7514 attrs &= ~(1 << 4);
7515 }
ebca90e4 7516 attrs |= nstable << 3; /* NS */
3dde962f
PM
7517 break;
7518 }
7519 /* Here descaddr is the final physical address, and attributes
7520 * are all in attrs.
7521 */
7522 fault_type = access_fault;
7523 if ((attrs & (1 << 8)) == 0) {
7524 /* Access flag */
7525 goto do_fault;
7526 }
d8e052b3
AJ
7527
7528 ap = extract32(attrs, 4, 2);
d8e052b3 7529 xn = extract32(attrs, 12, 1);
d8e052b3 7530
6ab1a5ee
EI
7531 if (mmu_idx == ARMMMUIdx_S2NS) {
7532 ns = true;
7533 *prot = get_S2prot(env, ap, xn);
7534 } else {
7535 ns = extract32(attrs, 3, 1);
7536 pxn = extract32(attrs, 11, 1);
7537 *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
7538 }
d8e052b3 7539
3dde962f 7540 fault_type = permission_fault;
d8e052b3 7541 if (!(*prot & (1 << access_type))) {
3dde962f
PM
7542 goto do_fault;
7543 }
3dde962f 7544
8bf5b6a9
PM
7545 if (ns) {
7546 /* The NS bit will (as required by the architecture) have no effect if
7547 * the CPU doesn't support TZ or this is a non-secure translation
7548 * regime, because the attribute will already be non-secure.
7549 */
7550 txattrs->secure = false;
7551 }
3dde962f
PM
7552 *phys_ptr = descaddr;
7553 *page_size_ptr = page_size;
b7cc4e82 7554 return false;
3dde962f
PM
7555
7556do_fault:
7557 /* Long-descriptor format IFSR/DFSR value */
b7cc4e82 7558 *fsr = (1 << 9) | (fault_type << 2) | level;
37785977
EI
7559 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
7560 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
b7cc4e82 7561 return true;
3dde962f
PM
7562}
7563
f6bda88f
PC
7564static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
7565 ARMMMUIdx mmu_idx,
7566 int32_t address, int *prot)
7567{
7568 *prot = PAGE_READ | PAGE_WRITE;
7569 switch (address) {
7570 case 0xF0000000 ... 0xFFFFFFFF:
7571 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
7572 *prot |= PAGE_EXEC;
7573 }
7574 break;
7575 case 0x00000000 ... 0x7FFFFFFF:
7576 *prot |= PAGE_EXEC;
7577 break;
7578 }
7579
7580}
7581
7582static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
7583 int access_type, ARMMMUIdx mmu_idx,
7584 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
7585{
7586 ARMCPU *cpu = arm_env_get_cpu(env);
7587 int n;
7588 bool is_user = regime_is_user(env, mmu_idx);
7589
7590 *phys_ptr = address;
7591 *prot = 0;
7592
7593 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
7594 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7595 } else { /* MPU enabled */
7596 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
7597 /* region search */
7598 uint32_t base = env->pmsav7.drbar[n];
7599 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
7600 uint32_t rmask;
7601 bool srdis = false;
7602
7603 if (!(env->pmsav7.drsr[n] & 0x1)) {
7604 continue;
7605 }
7606
7607 if (!rsize) {
7608 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
7609 continue;
7610 }
7611 rsize++;
7612 rmask = (1ull << rsize) - 1;
7613
7614 if (base & rmask) {
7615 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
7616 "to DRSR region size, mask = %" PRIx32,
7617 base, rmask);
7618 continue;
7619 }
7620
7621 if (address < base || address > base + rmask) {
7622 continue;
7623 }
7624
7625 /* Region matched */
7626
7627 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
7628 int i, snd;
7629 uint32_t srdis_mask;
7630
7631 rsize -= 3; /* sub region size (power of 2) */
7632 snd = ((address - base) >> rsize) & 0x7;
7633 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
7634
7635 srdis_mask = srdis ? 0x3 : 0x0;
7636 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
7637 /* This will check in groups of 2, 4 and then 8, whether
7638 * the subregion bits are consistent. rsize is incremented
7639 * back up to give the region size, considering consistent
7640 * adjacent subregions as one region. Stop testing if rsize
7641 * is already big enough for an entire QEMU page.
7642 */
7643 int snd_rounded = snd & ~(i - 1);
7644 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
7645 snd_rounded + 8, i);
7646 if (srdis_mask ^ srdis_multi) {
7647 break;
7648 }
7649 srdis_mask = (srdis_mask << i) | srdis_mask;
7650 rsize++;
7651 }
7652 }
7653 if (rsize < TARGET_PAGE_BITS) {
7654 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
7655 "alignment of %" PRIu32 " bits. Minimum is %d\n",
7656 rsize, TARGET_PAGE_BITS);
7657 continue;
7658 }
7659 if (srdis) {
7660 continue;
7661 }
7662 break;
7663 }
7664
7665 if (n == -1) { /* no hits */
7666 if (cpu->pmsav7_dregion &&
7667 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
7668 /* background fault */
7669 *fsr = 0;
7670 return true;
7671 }
7672 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7673 } else { /* a MPU hit! */
7674 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
7675
7676 if (is_user) { /* User mode AP bit decoding */
7677 switch (ap) {
7678 case 0:
7679 case 1:
7680 case 5:
7681 break; /* no access */
7682 case 3:
7683 *prot |= PAGE_WRITE;
7684 /* fall through */
7685 case 2:
7686 case 6:
7687 *prot |= PAGE_READ | PAGE_EXEC;
7688 break;
7689 default:
7690 qemu_log_mask(LOG_GUEST_ERROR,
7691 "Bad value for AP bits in DRACR %"
7692 PRIx32 "\n", ap);
7693 }
7694 } else { /* Priv. mode AP bits decoding */
7695 switch (ap) {
7696 case 0:
7697 break; /* no access */
7698 case 1:
7699 case 2:
7700 case 3:
7701 *prot |= PAGE_WRITE;
7702 /* fall through */
7703 case 5:
7704 case 6:
7705 *prot |= PAGE_READ | PAGE_EXEC;
7706 break;
7707 default:
7708 qemu_log_mask(LOG_GUEST_ERROR,
7709 "Bad value for AP bits in DRACR %"
7710 PRIx32 "\n", ap);
7711 }
7712 }
7713
7714 /* execute never */
7715 if (env->pmsav7.dracr[n] & (1 << 12)) {
7716 *prot &= ~PAGE_EXEC;
7717 }
7718 }
7719 }
7720
7721 *fsr = 0x00d; /* Permission fault */
7722 return !(*prot & (1 << access_type));
7723}
7724
13689d43
PC
7725static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
7726 int access_type, ARMMMUIdx mmu_idx,
7727 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
9ee6e8bb
PB
7728{
7729 int n;
7730 uint32_t mask;
7731 uint32_t base;
0480f69a 7732 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb
PB
7733
7734 *phys_ptr = address;
7735 for (n = 7; n >= 0; n--) {
554b0b09 7736 base = env->cp15.c6_region[n];
87c3d486 7737 if ((base & 1) == 0) {
554b0b09 7738 continue;
87c3d486 7739 }
554b0b09
PM
7740 mask = 1 << ((base >> 1) & 0x1f);
7741 /* Keep this shift separate from the above to avoid an
7742 (undefined) << 32. */
7743 mask = (mask << 1) - 1;
87c3d486 7744 if (((base ^ address) & ~mask) == 0) {
554b0b09 7745 break;
87c3d486 7746 }
9ee6e8bb 7747 }
87c3d486 7748 if (n < 0) {
b7cc4e82
PC
7749 *fsr = 2;
7750 return true;
87c3d486 7751 }
9ee6e8bb
PB
7752
7753 if (access_type == 2) {
7e09797c 7754 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 7755 } else {
7e09797c 7756 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
7757 }
7758 mask = (mask >> (n * 4)) & 0xf;
7759 switch (mask) {
7760 case 0:
b7cc4e82
PC
7761 *fsr = 1;
7762 return true;
9ee6e8bb 7763 case 1:
87c3d486 7764 if (is_user) {
b7cc4e82
PC
7765 *fsr = 1;
7766 return true;
87c3d486 7767 }
554b0b09
PM
7768 *prot = PAGE_READ | PAGE_WRITE;
7769 break;
9ee6e8bb 7770 case 2:
554b0b09 7771 *prot = PAGE_READ;
87c3d486 7772 if (!is_user) {
554b0b09 7773 *prot |= PAGE_WRITE;
87c3d486 7774 }
554b0b09 7775 break;
9ee6e8bb 7776 case 3:
554b0b09
PM
7777 *prot = PAGE_READ | PAGE_WRITE;
7778 break;
9ee6e8bb 7779 case 5:
87c3d486 7780 if (is_user) {
b7cc4e82
PC
7781 *fsr = 1;
7782 return true;
87c3d486 7783 }
554b0b09
PM
7784 *prot = PAGE_READ;
7785 break;
9ee6e8bb 7786 case 6:
554b0b09
PM
7787 *prot = PAGE_READ;
7788 break;
9ee6e8bb 7789 default:
554b0b09 7790 /* Bad permission. */
b7cc4e82
PC
7791 *fsr = 1;
7792 return true;
9ee6e8bb 7793 }
3ad493fc 7794 *prot |= PAGE_EXEC;
b7cc4e82 7795 return false;
9ee6e8bb
PB
7796}
7797
702a9357
PM
7798/* get_phys_addr - get the physical address for this virtual address
7799 *
7800 * Find the physical address corresponding to the given virtual address,
7801 * by doing a translation table walk on MMU based systems or using the
7802 * MPU state on MPU based systems.
7803 *
b7cc4e82
PC
7804 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
7805 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
7806 * information on why the translation aborted, in the format of a
7807 * DFSR/IFSR fault register, with the following caveats:
7808 * * we honour the short vs long DFSR format differences.
7809 * * the WnR bit is never set (the caller must do this).
f6bda88f 7810 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
7811 * value.
7812 *
7813 * @env: CPUARMState
7814 * @address: virtual address to get physical address for
7815 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 7816 * @mmu_idx: MMU index indicating required translation regime
702a9357 7817 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 7818 * @attrs: set to the memory transaction attributes to use
702a9357
PM
7819 * @prot: set to the permissions for the page containing phys_ptr
7820 * @page_size: set to the size of the page containing phys_ptr
b7cc4e82 7821 * @fsr: set to the DFSR/IFSR value on failure
702a9357 7822 */
af51f566
EI
7823static bool get_phys_addr(CPUARMState *env, target_ulong address,
7824 int access_type, ARMMMUIdx mmu_idx,
7825 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
e14b5a23
EI
7826 target_ulong *page_size, uint32_t *fsr,
7827 ARMMMUFaultInfo *fi)
9ee6e8bb 7828{
0480f69a 7829 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9b539263
EI
7830 /* Call ourselves recursively to do the stage 1 and then stage 2
7831 * translations.
0480f69a 7832 */
9b539263
EI
7833 if (arm_feature(env, ARM_FEATURE_EL2)) {
7834 hwaddr ipa;
7835 int s2_prot;
7836 int ret;
7837
7838 ret = get_phys_addr(env, address, access_type,
7839 mmu_idx + ARMMMUIdx_S1NSE0, &ipa, attrs,
7840 prot, page_size, fsr, fi);
7841
7842 /* If S1 fails or S2 is disabled, return early. */
7843 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
7844 *phys_ptr = ipa;
7845 return ret;
7846 }
7847
7848 /* S1 is done. Now do S2 translation. */
7849 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
7850 phys_ptr, attrs, &s2_prot,
7851 page_size, fsr, fi);
7852 fi->s2addr = ipa;
7853 /* Combine the S1 and S2 perms. */
7854 *prot &= s2_prot;
7855 return ret;
7856 } else {
7857 /*
7858 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
7859 */
7860 mmu_idx += ARMMMUIdx_S1NSE0;
7861 }
0480f69a 7862 }
d3649702 7863
8bf5b6a9
PM
7864 /* The page table entries may downgrade secure to non-secure, but
7865 * cannot upgrade an non-secure translation regime's attributes
7866 * to secure.
7867 */
7868 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 7869 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 7870
0480f69a
PM
7871 /* Fast Context Switch Extension. This doesn't exist at all in v8.
7872 * In v7 and earlier it affects all stage 1 translations.
7873 */
7874 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
7875 && !arm_feature(env, ARM_FEATURE_V8)) {
7876 if (regime_el(env, mmu_idx) == 3) {
7877 address += env->cp15.fcseidr_s;
7878 } else {
7879 address += env->cp15.fcseidr_ns;
7880 }
54bf36ed 7881 }
9ee6e8bb 7882
f6bda88f
PC
7883 /* pmsav7 has special handling for when MPU is disabled so call it before
7884 * the common MMU/MPU disabled check below.
7885 */
7886 if (arm_feature(env, ARM_FEATURE_MPU) &&
7887 arm_feature(env, ARM_FEATURE_V7)) {
7888 *page_size = TARGET_PAGE_SIZE;
7889 return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
7890 phys_ptr, prot, fsr);
7891 }
7892
0480f69a 7893 if (regime_translation_disabled(env, mmu_idx)) {
9ee6e8bb
PB
7894 /* MMU/MPU disabled. */
7895 *phys_ptr = address;
3ad493fc 7896 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 7897 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb 7898 return 0;
0480f69a
PM
7899 }
7900
7901 if (arm_feature(env, ARM_FEATURE_MPU)) {
f6bda88f 7902 /* Pre-v7 MPU */
d4c430a8 7903 *page_size = TARGET_PAGE_SIZE;
13689d43
PC
7904 return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
7905 phys_ptr, prot, fsr);
0480f69a
PM
7906 }
7907
7908 if (regime_using_lpae_format(env, mmu_idx)) {
7909 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 7910 attrs, prot, page_size, fsr, fi);
0480f69a
PM
7911 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
7912 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 7913 attrs, prot, page_size, fsr, fi);
9ee6e8bb 7914 } else {
0480f69a 7915 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
e14b5a23 7916 prot, page_size, fsr, fi);
9ee6e8bb
PB
7917 }
7918}
7919
8c6084bf 7920/* Walk the page table and (if the mapping exists) add the page
b7cc4e82
PC
7921 * to the TLB. Return false on success, or true on failure. Populate
7922 * fsr with ARM DFSR/IFSR fault register format value on failure.
8c6084bf 7923 */
b7cc4e82 7924bool arm_tlb_fill(CPUState *cs, vaddr address,
e14b5a23
EI
7925 int access_type, int mmu_idx, uint32_t *fsr,
7926 ARMMMUFaultInfo *fi)
b5ff1b31 7927{
7510454e
AF
7928 ARMCPU *cpu = ARM_CPU(cs);
7929 CPUARMState *env = &cpu->env;
a8170e5e 7930 hwaddr phys_addr;
d4c430a8 7931 target_ulong page_size;
b5ff1b31 7932 int prot;
d3649702 7933 int ret;
8bf5b6a9 7934 MemTxAttrs attrs = {};
b5ff1b31 7935
8bf5b6a9 7936 ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
e14b5a23 7937 &attrs, &prot, &page_size, fsr, fi);
b7cc4e82 7938 if (!ret) {
b5ff1b31 7939 /* Map a single [sub]page. */
dcd82c11
AB
7940 phys_addr &= TARGET_PAGE_MASK;
7941 address &= TARGET_PAGE_MASK;
8bf5b6a9
PM
7942 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
7943 prot, mmu_idx, page_size);
d4c430a8 7944 return 0;
b5ff1b31
FB
7945 }
7946
8c6084bf 7947 return ret;
b5ff1b31
FB
7948}
7949
0faea0c7
PM
7950hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
7951 MemTxAttrs *attrs)
b5ff1b31 7952{
00b941e5 7953 ARMCPU *cpu = ARM_CPU(cs);
d3649702 7954 CPUARMState *env = &cpu->env;
a8170e5e 7955 hwaddr phys_addr;
d4c430a8 7956 target_ulong page_size;
b5ff1b31 7957 int prot;
b7cc4e82
PC
7958 bool ret;
7959 uint32_t fsr;
e14b5a23 7960 ARMMMUFaultInfo fi = {};
b5ff1b31 7961
0faea0c7
PM
7962 *attrs = (MemTxAttrs) {};
7963
97ed5ccd 7964 ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
0faea0c7 7965 attrs, &prot, &page_size, &fsr, &fi);
b5ff1b31 7966
b7cc4e82 7967 if (ret) {
b5ff1b31 7968 return -1;
00b941e5 7969 }
b5ff1b31
FB
7970 return phys_addr;
7971}
7972
0ecb72a5 7973uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 7974{
a47dddd7
AF
7975 ARMCPU *cpu = arm_env_get_cpu(env);
7976
9ee6e8bb
PB
7977 switch (reg) {
7978 case 0: /* APSR */
7979 return xpsr_read(env) & 0xf8000000;
7980 case 1: /* IAPSR */
7981 return xpsr_read(env) & 0xf80001ff;
7982 case 2: /* EAPSR */
7983 return xpsr_read(env) & 0xff00fc00;
7984 case 3: /* xPSR */
7985 return xpsr_read(env) & 0xff00fdff;
7986 case 5: /* IPSR */
7987 return xpsr_read(env) & 0x000001ff;
7988 case 6: /* EPSR */
7989 return xpsr_read(env) & 0x0700fc00;
7990 case 7: /* IEPSR */
7991 return xpsr_read(env) & 0x0700edff;
7992 case 8: /* MSP */
7993 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
7994 case 9: /* PSP */
7995 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
7996 case 16: /* PRIMASK */
4cc35614 7997 return (env->daif & PSTATE_I) != 0;
82845826
SH
7998 case 17: /* BASEPRI */
7999 case 18: /* BASEPRI_MAX */
9ee6e8bb 8000 return env->v7m.basepri;
82845826 8001 case 19: /* FAULTMASK */
4cc35614 8002 return (env->daif & PSTATE_F) != 0;
9ee6e8bb
PB
8003 case 20: /* CONTROL */
8004 return env->v7m.control;
8005 default:
8006 /* ??? For debugging only. */
a47dddd7 8007 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
9ee6e8bb
PB
8008 return 0;
8009 }
8010}
8011
0ecb72a5 8012void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 8013{
a47dddd7
AF
8014 ARMCPU *cpu = arm_env_get_cpu(env);
8015
9ee6e8bb
PB
8016 switch (reg) {
8017 case 0: /* APSR */
8018 xpsr_write(env, val, 0xf8000000);
8019 break;
8020 case 1: /* IAPSR */
8021 xpsr_write(env, val, 0xf8000000);
8022 break;
8023 case 2: /* EAPSR */
8024 xpsr_write(env, val, 0xfe00fc00);
8025 break;
8026 case 3: /* xPSR */
8027 xpsr_write(env, val, 0xfe00fc00);
8028 break;
8029 case 5: /* IPSR */
8030 /* IPSR bits are readonly. */
8031 break;
8032 case 6: /* EPSR */
8033 xpsr_write(env, val, 0x0600fc00);
8034 break;
8035 case 7: /* IEPSR */
8036 xpsr_write(env, val, 0x0600fc00);
8037 break;
8038 case 8: /* MSP */
8039 if (env->v7m.current_sp)
8040 env->v7m.other_sp = val;
8041 else
8042 env->regs[13] = val;
8043 break;
8044 case 9: /* PSP */
8045 if (env->v7m.current_sp)
8046 env->regs[13] = val;
8047 else
8048 env->v7m.other_sp = val;
8049 break;
8050 case 16: /* PRIMASK */
4cc35614
PM
8051 if (val & 1) {
8052 env->daif |= PSTATE_I;
8053 } else {
8054 env->daif &= ~PSTATE_I;
8055 }
9ee6e8bb 8056 break;
82845826 8057 case 17: /* BASEPRI */
9ee6e8bb
PB
8058 env->v7m.basepri = val & 0xff;
8059 break;
82845826 8060 case 18: /* BASEPRI_MAX */
9ee6e8bb
PB
8061 val &= 0xff;
8062 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
8063 env->v7m.basepri = val;
8064 break;
82845826 8065 case 19: /* FAULTMASK */
4cc35614
PM
8066 if (val & 1) {
8067 env->daif |= PSTATE_F;
8068 } else {
8069 env->daif &= ~PSTATE_F;
8070 }
82845826 8071 break;
9ee6e8bb
PB
8072 case 20: /* CONTROL */
8073 env->v7m.control = val & 3;
8074 switch_v7m_sp(env, (val & 2) != 0);
8075 break;
8076 default:
8077 /* ??? For debugging only. */
a47dddd7 8078 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
9ee6e8bb
PB
8079 return;
8080 }
8081}
8082
b5ff1b31 8083#endif
6ddbc6e4 8084
aca3f40b
PM
8085void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
8086{
8087 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
8088 * Note that we do not implement the (architecturally mandated)
8089 * alignment fault for attempts to use this on Device memory
8090 * (which matches the usual QEMU behaviour of not implementing either
8091 * alignment faults or any memory attribute handling).
8092 */
8093
8094 ARMCPU *cpu = arm_env_get_cpu(env);
8095 uint64_t blocklen = 4 << cpu->dcz_blocksize;
8096 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
8097
8098#ifndef CONFIG_USER_ONLY
8099 {
8100 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
8101 * the block size so we might have to do more than one TLB lookup.
8102 * We know that in fact for any v8 CPU the page size is at least 4K
8103 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
8104 * 1K as an artefact of legacy v5 subpage support being present in the
8105 * same QEMU executable.
8106 */
8107 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
8108 void *hostaddr[maxidx];
8109 int try, i;
97ed5ccd 8110 unsigned mmu_idx = cpu_mmu_index(env, false);
3972ef6f 8111 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
aca3f40b
PM
8112
8113 for (try = 0; try < 2; try++) {
8114
8115 for (i = 0; i < maxidx; i++) {
8116 hostaddr[i] = tlb_vaddr_to_host(env,
8117 vaddr + TARGET_PAGE_SIZE * i,
3972ef6f 8118 1, mmu_idx);
aca3f40b
PM
8119 if (!hostaddr[i]) {
8120 break;
8121 }
8122 }
8123 if (i == maxidx) {
8124 /* If it's all in the TLB it's fair game for just writing to;
8125 * we know we don't need to update dirty status, etc.
8126 */
8127 for (i = 0; i < maxidx - 1; i++) {
8128 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
8129 }
8130 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
8131 return;
8132 }
8133 /* OK, try a store and see if we can populate the tlb. This
8134 * might cause an exception if the memory isn't writable,
8135 * in which case we will longjmp out of here. We must for
8136 * this purpose use the actual register value passed to us
8137 * so that we get the fault address right.
8138 */
3972ef6f 8139 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
aca3f40b
PM
8140 /* Now we can populate the other TLB entries, if any */
8141 for (i = 0; i < maxidx; i++) {
8142 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
8143 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
3972ef6f 8144 helper_ret_stb_mmu(env, va, 0, oi, GETRA());
aca3f40b
PM
8145 }
8146 }
8147 }
8148
8149 /* Slow path (probably attempt to do this to an I/O device or
8150 * similar, or clearing of a block of code we have translations
8151 * cached for). Just do a series of byte writes as the architecture
8152 * demands. It's not worth trying to use a cpu_physical_memory_map(),
8153 * memset(), unmap() sequence here because:
8154 * + we'd need to account for the blocksize being larger than a page
8155 * + the direct-RAM access case is almost always going to be dealt
8156 * with in the fastpath code above, so there's no speed benefit
8157 * + we would have to deal with the map returning NULL because the
8158 * bounce buffer was in use
8159 */
8160 for (i = 0; i < blocklen; i++) {
3972ef6f 8161 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
aca3f40b
PM
8162 }
8163 }
8164#else
8165 memset(g2h(vaddr), 0, blocklen);
8166#endif
8167}
8168
6ddbc6e4
PB
8169/* Note that signed overflow is undefined in C. The following routines are
8170 careful to use unsigned types where modulo arithmetic is required.
8171 Failure to do so _will_ break on newer gcc. */
8172
8173/* Signed saturating arithmetic. */
8174
1654b2d6 8175/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
8176static inline uint16_t add16_sat(uint16_t a, uint16_t b)
8177{
8178 uint16_t res;
8179
8180 res = a + b;
8181 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
8182 if (a & 0x8000)
8183 res = 0x8000;
8184 else
8185 res = 0x7fff;
8186 }
8187 return res;
8188}
8189
1654b2d6 8190/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
8191static inline uint8_t add8_sat(uint8_t a, uint8_t b)
8192{
8193 uint8_t res;
8194
8195 res = a + b;
8196 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
8197 if (a & 0x80)
8198 res = 0x80;
8199 else
8200 res = 0x7f;
8201 }
8202 return res;
8203}
8204
1654b2d6 8205/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
8206static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
8207{
8208 uint16_t res;
8209
8210 res = a - b;
8211 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
8212 if (a & 0x8000)
8213 res = 0x8000;
8214 else
8215 res = 0x7fff;
8216 }
8217 return res;
8218}
8219
1654b2d6 8220/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
8221static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
8222{
8223 uint8_t res;
8224
8225 res = a - b;
8226 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
8227 if (a & 0x80)
8228 res = 0x80;
8229 else
8230 res = 0x7f;
8231 }
8232 return res;
8233}
8234
8235#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
8236#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
8237#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
8238#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
8239#define PFX q
8240
8241#include "op_addsub.h"
8242
8243/* Unsigned saturating arithmetic. */
460a09c1 8244static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
8245{
8246 uint16_t res;
8247 res = a + b;
8248 if (res < a)
8249 res = 0xffff;
8250 return res;
8251}
8252
460a09c1 8253static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 8254{
4c4fd3f8 8255 if (a > b)
6ddbc6e4
PB
8256 return a - b;
8257 else
8258 return 0;
8259}
8260
8261static inline uint8_t add8_usat(uint8_t a, uint8_t b)
8262{
8263 uint8_t res;
8264 res = a + b;
8265 if (res < a)
8266 res = 0xff;
8267 return res;
8268}
8269
8270static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
8271{
4c4fd3f8 8272 if (a > b)
6ddbc6e4
PB
8273 return a - b;
8274 else
8275 return 0;
8276}
8277
8278#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
8279#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
8280#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
8281#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
8282#define PFX uq
8283
8284#include "op_addsub.h"
8285
8286/* Signed modulo arithmetic. */
8287#define SARITH16(a, b, n, op) do { \
8288 int32_t sum; \
db6e2e65 8289 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
8290 RESULT(sum, n, 16); \
8291 if (sum >= 0) \
8292 ge |= 3 << (n * 2); \
8293 } while(0)
8294
8295#define SARITH8(a, b, n, op) do { \
8296 int32_t sum; \
db6e2e65 8297 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
8298 RESULT(sum, n, 8); \
8299 if (sum >= 0) \
8300 ge |= 1 << n; \
8301 } while(0)
8302
8303
8304#define ADD16(a, b, n) SARITH16(a, b, n, +)
8305#define SUB16(a, b, n) SARITH16(a, b, n, -)
8306#define ADD8(a, b, n) SARITH8(a, b, n, +)
8307#define SUB8(a, b, n) SARITH8(a, b, n, -)
8308#define PFX s
8309#define ARITH_GE
8310
8311#include "op_addsub.h"
8312
8313/* Unsigned modulo arithmetic. */
8314#define ADD16(a, b, n) do { \
8315 uint32_t sum; \
8316 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
8317 RESULT(sum, n, 16); \
a87aa10b 8318 if ((sum >> 16) == 1) \
6ddbc6e4
PB
8319 ge |= 3 << (n * 2); \
8320 } while(0)
8321
8322#define ADD8(a, b, n) do { \
8323 uint32_t sum; \
8324 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
8325 RESULT(sum, n, 8); \
a87aa10b
AZ
8326 if ((sum >> 8) == 1) \
8327 ge |= 1 << n; \
6ddbc6e4
PB
8328 } while(0)
8329
8330#define SUB16(a, b, n) do { \
8331 uint32_t sum; \
8332 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
8333 RESULT(sum, n, 16); \
8334 if ((sum >> 16) == 0) \
8335 ge |= 3 << (n * 2); \
8336 } while(0)
8337
8338#define SUB8(a, b, n) do { \
8339 uint32_t sum; \
8340 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
8341 RESULT(sum, n, 8); \
8342 if ((sum >> 8) == 0) \
a87aa10b 8343 ge |= 1 << n; \
6ddbc6e4
PB
8344 } while(0)
8345
8346#define PFX u
8347#define ARITH_GE
8348
8349#include "op_addsub.h"
8350
8351/* Halved signed arithmetic. */
8352#define ADD16(a, b, n) \
8353 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
8354#define SUB16(a, b, n) \
8355 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
8356#define ADD8(a, b, n) \
8357 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
8358#define SUB8(a, b, n) \
8359 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
8360#define PFX sh
8361
8362#include "op_addsub.h"
8363
8364/* Halved unsigned arithmetic. */
8365#define ADD16(a, b, n) \
8366 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8367#define SUB16(a, b, n) \
8368 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8369#define ADD8(a, b, n) \
8370 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8371#define SUB8(a, b, n) \
8372 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8373#define PFX uh
8374
8375#include "op_addsub.h"
8376
8377static inline uint8_t do_usad(uint8_t a, uint8_t b)
8378{
8379 if (a > b)
8380 return a - b;
8381 else
8382 return b - a;
8383}
8384
8385/* Unsigned sum of absolute byte differences. */
8386uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
8387{
8388 uint32_t sum;
8389 sum = do_usad(a, b);
8390 sum += do_usad(a >> 8, b >> 8);
8391 sum += do_usad(a >> 16, b >>16);
8392 sum += do_usad(a >> 24, b >> 24);
8393 return sum;
8394}
8395
8396/* For ARMv6 SEL instruction. */
8397uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
8398{
8399 uint32_t mask;
8400
8401 mask = 0;
8402 if (flags & 1)
8403 mask |= 0xff;
8404 if (flags & 2)
8405 mask |= 0xff00;
8406 if (flags & 4)
8407 mask |= 0xff0000;
8408 if (flags & 8)
8409 mask |= 0xff000000;
8410 return (a & mask) | (b & ~mask);
8411}
8412
b90372ad
PM
8413/* VFP support. We follow the convention used for VFP instructions:
8414 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
8415 "d" suffix. */
8416
8417/* Convert host exception flags to vfp form. */
8418static inline int vfp_exceptbits_from_host(int host_bits)
8419{
8420 int target_bits = 0;
8421
8422 if (host_bits & float_flag_invalid)
8423 target_bits |= 1;
8424 if (host_bits & float_flag_divbyzero)
8425 target_bits |= 2;
8426 if (host_bits & float_flag_overflow)
8427 target_bits |= 4;
36802b6b 8428 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
8429 target_bits |= 8;
8430 if (host_bits & float_flag_inexact)
8431 target_bits |= 0x10;
cecd8504
PM
8432 if (host_bits & float_flag_input_denormal)
8433 target_bits |= 0x80;
4373f3ce
PB
8434 return target_bits;
8435}
8436
0ecb72a5 8437uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
8438{
8439 int i;
8440 uint32_t fpscr;
8441
8442 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
8443 | (env->vfp.vec_len << 16)
8444 | (env->vfp.vec_stride << 20);
8445 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 8446 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
8447 fpscr |= vfp_exceptbits_from_host(i);
8448 return fpscr;
8449}
8450
0ecb72a5 8451uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
8452{
8453 return HELPER(vfp_get_fpscr)(env);
8454}
8455
4373f3ce
PB
8456/* Convert vfp exception flags to target form. */
8457static inline int vfp_exceptbits_to_host(int target_bits)
8458{
8459 int host_bits = 0;
8460
8461 if (target_bits & 1)
8462 host_bits |= float_flag_invalid;
8463 if (target_bits & 2)
8464 host_bits |= float_flag_divbyzero;
8465 if (target_bits & 4)
8466 host_bits |= float_flag_overflow;
8467 if (target_bits & 8)
8468 host_bits |= float_flag_underflow;
8469 if (target_bits & 0x10)
8470 host_bits |= float_flag_inexact;
cecd8504
PM
8471 if (target_bits & 0x80)
8472 host_bits |= float_flag_input_denormal;
4373f3ce
PB
8473 return host_bits;
8474}
8475
0ecb72a5 8476void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
8477{
8478 int i;
8479 uint32_t changed;
8480
8481 changed = env->vfp.xregs[ARM_VFP_FPSCR];
8482 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
8483 env->vfp.vec_len = (val >> 16) & 7;
8484 env->vfp.vec_stride = (val >> 20) & 3;
8485
8486 changed ^= val;
8487 if (changed & (3 << 22)) {
8488 i = (val >> 22) & 3;
8489 switch (i) {
4d3da0f3 8490 case FPROUNDING_TIEEVEN:
4373f3ce
PB
8491 i = float_round_nearest_even;
8492 break;
4d3da0f3 8493 case FPROUNDING_POSINF:
4373f3ce
PB
8494 i = float_round_up;
8495 break;
4d3da0f3 8496 case FPROUNDING_NEGINF:
4373f3ce
PB
8497 i = float_round_down;
8498 break;
4d3da0f3 8499 case FPROUNDING_ZERO:
4373f3ce
PB
8500 i = float_round_to_zero;
8501 break;
8502 }
8503 set_float_rounding_mode(i, &env->vfp.fp_status);
8504 }
cecd8504 8505 if (changed & (1 << 24)) {
fe76d976 8506 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
8507 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
8508 }
5c7908ed
PB
8509 if (changed & (1 << 25))
8510 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 8511
b12c390b 8512 i = vfp_exceptbits_to_host(val);
4373f3ce 8513 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 8514 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
8515}
8516
0ecb72a5 8517void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
8518{
8519 HELPER(vfp_set_fpscr)(env, val);
8520}
8521
4373f3ce
PB
8522#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
8523
8524#define VFP_BINOP(name) \
ae1857ec 8525float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 8526{ \
ae1857ec
PM
8527 float_status *fpst = fpstp; \
8528 return float32_ ## name(a, b, fpst); \
4373f3ce 8529} \
ae1857ec 8530float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 8531{ \
ae1857ec
PM
8532 float_status *fpst = fpstp; \
8533 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
8534}
8535VFP_BINOP(add)
8536VFP_BINOP(sub)
8537VFP_BINOP(mul)
8538VFP_BINOP(div)
f71a2ae5
PM
8539VFP_BINOP(min)
8540VFP_BINOP(max)
8541VFP_BINOP(minnum)
8542VFP_BINOP(maxnum)
4373f3ce
PB
8543#undef VFP_BINOP
8544
8545float32 VFP_HELPER(neg, s)(float32 a)
8546{
8547 return float32_chs(a);
8548}
8549
8550float64 VFP_HELPER(neg, d)(float64 a)
8551{
66230e0d 8552 return float64_chs(a);
4373f3ce
PB
8553}
8554
8555float32 VFP_HELPER(abs, s)(float32 a)
8556{
8557 return float32_abs(a);
8558}
8559
8560float64 VFP_HELPER(abs, d)(float64 a)
8561{
66230e0d 8562 return float64_abs(a);
4373f3ce
PB
8563}
8564
0ecb72a5 8565float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
8566{
8567 return float32_sqrt(a, &env->vfp.fp_status);
8568}
8569
0ecb72a5 8570float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
8571{
8572 return float64_sqrt(a, &env->vfp.fp_status);
8573}
8574
8575/* XXX: check quiet/signaling case */
8576#define DO_VFP_cmp(p, type) \
0ecb72a5 8577void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
8578{ \
8579 uint32_t flags; \
8580 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
8581 case 0: flags = 0x6; break; \
8582 case -1: flags = 0x8; break; \
8583 case 1: flags = 0x2; break; \
8584 default: case 2: flags = 0x3; break; \
8585 } \
8586 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8587 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8588} \
0ecb72a5 8589void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
8590{ \
8591 uint32_t flags; \
8592 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
8593 case 0: flags = 0x6; break; \
8594 case -1: flags = 0x8; break; \
8595 case 1: flags = 0x2; break; \
8596 default: case 2: flags = 0x3; break; \
8597 } \
8598 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8599 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8600}
8601DO_VFP_cmp(s, float32)
8602DO_VFP_cmp(d, float64)
8603#undef DO_VFP_cmp
8604
5500b06c 8605/* Integer to float and float to integer conversions */
4373f3ce 8606
5500b06c
PM
8607#define CONV_ITOF(name, fsz, sign) \
8608 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
8609{ \
8610 float_status *fpst = fpstp; \
85836979 8611 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
8612}
8613
5500b06c
PM
8614#define CONV_FTOI(name, fsz, sign, round) \
8615uint32_t HELPER(name)(float##fsz x, void *fpstp) \
8616{ \
8617 float_status *fpst = fpstp; \
8618 if (float##fsz##_is_any_nan(x)) { \
8619 float_raise(float_flag_invalid, fpst); \
8620 return 0; \
8621 } \
8622 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
8623}
8624
5500b06c
PM
8625#define FLOAT_CONVS(name, p, fsz, sign) \
8626CONV_ITOF(vfp_##name##to##p, fsz, sign) \
8627CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
8628CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 8629
5500b06c
PM
8630FLOAT_CONVS(si, s, 32, )
8631FLOAT_CONVS(si, d, 64, )
8632FLOAT_CONVS(ui, s, 32, u)
8633FLOAT_CONVS(ui, d, 64, u)
4373f3ce 8634
5500b06c
PM
8635#undef CONV_ITOF
8636#undef CONV_FTOI
8637#undef FLOAT_CONVS
4373f3ce
PB
8638
8639/* floating point conversion */
0ecb72a5 8640float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 8641{
2d627737
PM
8642 float64 r = float32_to_float64(x, &env->vfp.fp_status);
8643 /* ARM requires that S<->D conversion of any kind of NaN generates
8644 * a quiet NaN by forcing the most significant frac bit to 1.
8645 */
8646 return float64_maybe_silence_nan(r);
4373f3ce
PB
8647}
8648
0ecb72a5 8649float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 8650{
2d627737
PM
8651 float32 r = float64_to_float32(x, &env->vfp.fp_status);
8652 /* ARM requires that S<->D conversion of any kind of NaN generates
8653 * a quiet NaN by forcing the most significant frac bit to 1.
8654 */
8655 return float32_maybe_silence_nan(r);
4373f3ce
PB
8656}
8657
8658/* VFP3 fixed point conversion. */
16d5b3ca 8659#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
8660float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
8661 void *fpstp) \
4373f3ce 8662{ \
5500b06c 8663 float_status *fpst = fpstp; \
622465e1 8664 float##fsz tmp; \
8ed697e8 8665 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 8666 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
8667}
8668
abe66f70
PM
8669/* Notice that we want only input-denormal exception flags from the
8670 * scalbn operation: the other possible flags (overflow+inexact if
8671 * we overflow to infinity, output-denormal) aren't correct for the
8672 * complete scale-and-convert operation.
8673 */
16d5b3ca
WN
8674#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
8675uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
8676 uint32_t shift, \
8677 void *fpstp) \
4373f3ce 8678{ \
5500b06c 8679 float_status *fpst = fpstp; \
abe66f70 8680 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
8681 float##fsz tmp; \
8682 if (float##fsz##_is_any_nan(x)) { \
5500b06c 8683 float_raise(float_flag_invalid, fpst); \
622465e1 8684 return 0; \
09d9487f 8685 } \
5500b06c 8686 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
8687 old_exc_flags |= get_float_exception_flags(fpst) \
8688 & float_flag_input_denormal; \
8689 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 8690 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
8691}
8692
16d5b3ca
WN
8693#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
8694VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
8695VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
8696VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
8697
8698#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
8699VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8700VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 8701
8ed697e8
WN
8702VFP_CONV_FIX(sh, d, 64, 64, int16)
8703VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 8704VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
8705VFP_CONV_FIX(uh, d, 64, 64, uint16)
8706VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 8707VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
8708VFP_CONV_FIX(sh, s, 32, 32, int16)
8709VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 8710VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
8711VFP_CONV_FIX(uh, s, 32, 32, uint16)
8712VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 8713VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 8714#undef VFP_CONV_FIX
16d5b3ca
WN
8715#undef VFP_CONV_FIX_FLOAT
8716#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 8717
52a1f6a3
AG
8718/* Set the current fp rounding mode and return the old one.
8719 * The argument is a softfloat float_round_ value.
8720 */
8721uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
8722{
8723 float_status *fp_status = &env->vfp.fp_status;
8724
8725 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8726 set_float_rounding_mode(rmode, fp_status);
8727
8728 return prev_rmode;
8729}
8730
43630e58
WN
8731/* Set the current fp rounding mode in the standard fp status and return
8732 * the old one. This is for NEON instructions that need to change the
8733 * rounding mode but wish to use the standard FPSCR values for everything
8734 * else. Always set the rounding mode back to the correct value after
8735 * modifying it.
8736 * The argument is a softfloat float_round_ value.
8737 */
8738uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
8739{
8740 float_status *fp_status = &env->vfp.standard_fp_status;
8741
8742 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8743 set_float_rounding_mode(rmode, fp_status);
8744
8745 return prev_rmode;
8746}
8747
60011498 8748/* Half precision conversions. */
0ecb72a5 8749static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 8750{
60011498 8751 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
8752 float32 r = float16_to_float32(make_float16(a), ieee, s);
8753 if (ieee) {
8754 return float32_maybe_silence_nan(r);
8755 }
8756 return r;
60011498
PB
8757}
8758
0ecb72a5 8759static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 8760{
60011498 8761 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
8762 float16 r = float32_to_float16(a, ieee, s);
8763 if (ieee) {
8764 r = float16_maybe_silence_nan(r);
8765 }
8766 return float16_val(r);
60011498
PB
8767}
8768
0ecb72a5 8769float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
8770{
8771 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
8772}
8773
0ecb72a5 8774uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
8775{
8776 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
8777}
8778
0ecb72a5 8779float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
8780{
8781 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
8782}
8783
0ecb72a5 8784uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
8785{
8786 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
8787}
8788
8900aad2
PM
8789float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
8790{
8791 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8792 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
8793 if (ieee) {
8794 return float64_maybe_silence_nan(r);
8795 }
8796 return r;
8797}
8798
8799uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
8800{
8801 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8802 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
8803 if (ieee) {
8804 r = float16_maybe_silence_nan(r);
8805 }
8806 return float16_val(r);
8807}
8808
dda3ec49 8809#define float32_two make_float32(0x40000000)
6aae3df1
PM
8810#define float32_three make_float32(0x40400000)
8811#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 8812
0ecb72a5 8813float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 8814{
dda3ec49
PM
8815 float_status *s = &env->vfp.standard_fp_status;
8816 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8817 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
8818 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8819 float_raise(float_flag_input_denormal, s);
8820 }
dda3ec49
PM
8821 return float32_two;
8822 }
8823 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
8824}
8825
0ecb72a5 8826float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 8827{
71826966 8828 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
8829 float32 product;
8830 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8831 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
8832 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8833 float_raise(float_flag_input_denormal, s);
8834 }
6aae3df1 8835 return float32_one_point_five;
9ea62f57 8836 }
6aae3df1
PM
8837 product = float32_mul(a, b, s);
8838 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
8839}
8840
8f8e3aa4
PB
8841/* NEON helpers. */
8842
56bf4fe2
CL
8843/* Constants 256 and 512 are used in some helpers; we avoid relying on
8844 * int->float conversions at run-time. */
8845#define float64_256 make_float64(0x4070000000000000LL)
8846#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
8847#define float32_maxnorm make_float32(0x7f7fffff)
8848#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 8849
b6d4443a
AB
8850/* Reciprocal functions
8851 *
8852 * The algorithm that must be used to calculate the estimate
8853 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 8854 */
b6d4443a
AB
8855
8856static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 8857{
1146a817
PM
8858 /* These calculations mustn't set any fp exception flags,
8859 * so we use a local copy of the fp_status.
8860 */
b6d4443a 8861 float_status dummy_status = *real_fp_status;
1146a817 8862 float_status *s = &dummy_status;
fe0e4872
CL
8863 /* q = (int)(a * 512.0) */
8864 float64 q = float64_mul(float64_512, a, s);
8865 int64_t q_int = float64_to_int64_round_to_zero(q, s);
8866
8867 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
8868 q = int64_to_float64(q_int, s);
8869 q = float64_add(q, float64_half, s);
8870 q = float64_div(q, float64_512, s);
8871 q = float64_div(float64_one, q, s);
8872
8873 /* s = (int)(256.0 * r + 0.5) */
8874 q = float64_mul(q, float64_256, s);
8875 q = float64_add(q, float64_half, s);
8876 q_int = float64_to_int64_round_to_zero(q, s);
8877
8878 /* return (double)s / 256.0 */
8879 return float64_div(int64_to_float64(q_int, s), float64_256, s);
8880}
8881
b6d4443a
AB
8882/* Common wrapper to call recip_estimate */
8883static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 8884{
b6d4443a
AB
8885 uint64_t val64 = float64_val(num);
8886 uint64_t frac = extract64(val64, 0, 52);
8887 int64_t exp = extract64(val64, 52, 11);
8888 uint64_t sbit;
8889 float64 scaled, estimate;
fe0e4872 8890
b6d4443a
AB
8891 /* Generate the scaled number for the estimate function */
8892 if (exp == 0) {
8893 if (extract64(frac, 51, 1) == 0) {
8894 exp = -1;
8895 frac = extract64(frac, 0, 50) << 2;
8896 } else {
8897 frac = extract64(frac, 0, 51) << 1;
8898 }
8899 }
fe0e4872 8900
b6d4443a
AB
8901 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
8902 scaled = make_float64((0x3feULL << 52)
8903 | extract64(frac, 44, 8) << 44);
8904
8905 estimate = recip_estimate(scaled, fpst);
8906
8907 /* Build new result */
8908 val64 = float64_val(estimate);
8909 sbit = 0x8000000000000000ULL & val64;
8910 exp = off - exp;
8911 frac = extract64(val64, 0, 52);
8912
8913 if (exp == 0) {
8914 frac = 1ULL << 51 | extract64(frac, 1, 51);
8915 } else if (exp == -1) {
8916 frac = 1ULL << 50 | extract64(frac, 2, 50);
8917 exp = 0;
8918 }
8919
8920 return make_float64(sbit | (exp << 52) | frac);
8921}
8922
8923static bool round_to_inf(float_status *fpst, bool sign_bit)
8924{
8925 switch (fpst->float_rounding_mode) {
8926 case float_round_nearest_even: /* Round to Nearest */
8927 return true;
8928 case float_round_up: /* Round to +Inf */
8929 return !sign_bit;
8930 case float_round_down: /* Round to -Inf */
8931 return sign_bit;
8932 case float_round_to_zero: /* Round to Zero */
8933 return false;
8934 }
8935
8936 g_assert_not_reached();
8937}
8938
8939float32 HELPER(recpe_f32)(float32 input, void *fpstp)
8940{
8941 float_status *fpst = fpstp;
8942 float32 f32 = float32_squash_input_denormal(input, fpst);
8943 uint32_t f32_val = float32_val(f32);
8944 uint32_t f32_sbit = 0x80000000ULL & f32_val;
8945 int32_t f32_exp = extract32(f32_val, 23, 8);
8946 uint32_t f32_frac = extract32(f32_val, 0, 23);
8947 float64 f64, r64;
8948 uint64_t r64_val;
8949 int64_t r64_exp;
8950 uint64_t r64_frac;
8951
8952 if (float32_is_any_nan(f32)) {
8953 float32 nan = f32;
8954 if (float32_is_signaling_nan(f32)) {
8955 float_raise(float_flag_invalid, fpst);
8956 nan = float32_maybe_silence_nan(f32);
fe0e4872 8957 }
b6d4443a
AB
8958 if (fpst->default_nan_mode) {
8959 nan = float32_default_nan;
43fe9bdb 8960 }
b6d4443a
AB
8961 return nan;
8962 } else if (float32_is_infinity(f32)) {
8963 return float32_set_sign(float32_zero, float32_is_neg(f32));
8964 } else if (float32_is_zero(f32)) {
8965 float_raise(float_flag_divbyzero, fpst);
8966 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8967 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
8968 /* Abs(value) < 2.0^-128 */
8969 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8970 if (round_to_inf(fpst, f32_sbit)) {
8971 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8972 } else {
8973 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
8974 }
8975 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
8976 float_raise(float_flag_underflow, fpst);
8977 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
8978 }
8979
fe0e4872 8980
b6d4443a
AB
8981 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
8982 r64 = call_recip_estimate(f64, 253, fpst);
8983 r64_val = float64_val(r64);
8984 r64_exp = extract64(r64_val, 52, 11);
8985 r64_frac = extract64(r64_val, 0, 52);
8986
8987 /* result = sign : result_exp<7:0> : fraction<51:29>; */
8988 return make_float32(f32_sbit |
8989 (r64_exp & 0xff) << 23 |
8990 extract64(r64_frac, 29, 24));
8991}
8992
8993float64 HELPER(recpe_f64)(float64 input, void *fpstp)
8994{
8995 float_status *fpst = fpstp;
8996 float64 f64 = float64_squash_input_denormal(input, fpst);
8997 uint64_t f64_val = float64_val(f64);
8998 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
8999 int64_t f64_exp = extract64(f64_val, 52, 11);
9000 float64 r64;
9001 uint64_t r64_val;
9002 int64_t r64_exp;
9003 uint64_t r64_frac;
9004
9005 /* Deal with any special cases */
9006 if (float64_is_any_nan(f64)) {
9007 float64 nan = f64;
9008 if (float64_is_signaling_nan(f64)) {
9009 float_raise(float_flag_invalid, fpst);
9010 nan = float64_maybe_silence_nan(f64);
9011 }
9012 if (fpst->default_nan_mode) {
9013 nan = float64_default_nan;
9014 }
9015 return nan;
9016 } else if (float64_is_infinity(f64)) {
9017 return float64_set_sign(float64_zero, float64_is_neg(f64));
9018 } else if (float64_is_zero(f64)) {
9019 float_raise(float_flag_divbyzero, fpst);
9020 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9021 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
9022 /* Abs(value) < 2.0^-1024 */
9023 float_raise(float_flag_overflow | float_flag_inexact, fpst);
9024 if (round_to_inf(fpst, f64_sbit)) {
9025 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9026 } else {
9027 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
9028 }
fc1792e9 9029 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
b6d4443a
AB
9030 float_raise(float_flag_underflow, fpst);
9031 return float64_set_sign(float64_zero, float64_is_neg(f64));
9032 }
fe0e4872 9033
b6d4443a
AB
9034 r64 = call_recip_estimate(f64, 2045, fpst);
9035 r64_val = float64_val(r64);
9036 r64_exp = extract64(r64_val, 52, 11);
9037 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 9038
b6d4443a
AB
9039 /* result = sign : result_exp<10:0> : fraction<51:0> */
9040 return make_float64(f64_sbit |
9041 ((r64_exp & 0x7ff) << 52) |
9042 r64_frac);
4373f3ce
PB
9043}
9044
e07be5d2
CL
9045/* The algorithm that must be used to calculate the estimate
9046 * is specified by the ARM ARM.
9047 */
c2fb418e 9048static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 9049{
1146a817
PM
9050 /* These calculations mustn't set any fp exception flags,
9051 * so we use a local copy of the fp_status.
9052 */
c2fb418e 9053 float_status dummy_status = *real_fp_status;
1146a817 9054 float_status *s = &dummy_status;
e07be5d2
CL
9055 float64 q;
9056 int64_t q_int;
9057
9058 if (float64_lt(a, float64_half, s)) {
9059 /* range 0.25 <= a < 0.5 */
9060
9061 /* a in units of 1/512 rounded down */
9062 /* q0 = (int)(a * 512.0); */
9063 q = float64_mul(float64_512, a, s);
9064 q_int = float64_to_int64_round_to_zero(q, s);
9065
9066 /* reciprocal root r */
9067 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
9068 q = int64_to_float64(q_int, s);
9069 q = float64_add(q, float64_half, s);
9070 q = float64_div(q, float64_512, s);
9071 q = float64_sqrt(q, s);
9072 q = float64_div(float64_one, q, s);
9073 } else {
9074 /* range 0.5 <= a < 1.0 */
9075
9076 /* a in units of 1/256 rounded down */
9077 /* q1 = (int)(a * 256.0); */
9078 q = float64_mul(float64_256, a, s);
9079 int64_t q_int = float64_to_int64_round_to_zero(q, s);
9080
9081 /* reciprocal root r */
9082 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
9083 q = int64_to_float64(q_int, s);
9084 q = float64_add(q, float64_half, s);
9085 q = float64_div(q, float64_256, s);
9086 q = float64_sqrt(q, s);
9087 q = float64_div(float64_one, q, s);
9088 }
9089 /* r in units of 1/256 rounded to nearest */
9090 /* s = (int)(256.0 * r + 0.5); */
9091
9092 q = float64_mul(q, float64_256,s );
9093 q = float64_add(q, float64_half, s);
9094 q_int = float64_to_int64_round_to_zero(q, s);
9095
9096 /* return (double)s / 256.0;*/
9097 return float64_div(int64_to_float64(q_int, s), float64_256, s);
9098}
9099
c2fb418e 9100float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 9101{
c2fb418e
AB
9102 float_status *s = fpstp;
9103 float32 f32 = float32_squash_input_denormal(input, s);
9104 uint32_t val = float32_val(f32);
9105 uint32_t f32_sbit = 0x80000000 & val;
9106 int32_t f32_exp = extract32(val, 23, 8);
9107 uint32_t f32_frac = extract32(val, 0, 23);
9108 uint64_t f64_frac;
9109 uint64_t val64;
e07be5d2
CL
9110 int result_exp;
9111 float64 f64;
e07be5d2 9112
c2fb418e
AB
9113 if (float32_is_any_nan(f32)) {
9114 float32 nan = f32;
9115 if (float32_is_signaling_nan(f32)) {
e07be5d2 9116 float_raise(float_flag_invalid, s);
c2fb418e 9117 nan = float32_maybe_silence_nan(f32);
e07be5d2 9118 }
c2fb418e
AB
9119 if (s->default_nan_mode) {
9120 nan = float32_default_nan;
43fe9bdb 9121 }
c2fb418e
AB
9122 return nan;
9123 } else if (float32_is_zero(f32)) {
e07be5d2 9124 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
9125 return float32_set_sign(float32_infinity, float32_is_neg(f32));
9126 } else if (float32_is_neg(f32)) {
e07be5d2
CL
9127 float_raise(float_flag_invalid, s);
9128 return float32_default_nan;
c2fb418e 9129 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
9130 return float32_zero;
9131 }
9132
c2fb418e 9133 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 9134 * preserving the parity of the exponent. */
c2fb418e
AB
9135
9136 f64_frac = ((uint64_t) f32_frac) << 29;
9137 if (f32_exp == 0) {
9138 while (extract64(f64_frac, 51, 1) == 0) {
9139 f64_frac = f64_frac << 1;
9140 f32_exp = f32_exp-1;
9141 }
9142 f64_frac = extract64(f64_frac, 0, 51) << 1;
9143 }
9144
9145 if (extract64(f32_exp, 0, 1) == 0) {
9146 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 9147 | (0x3feULL << 52)
c2fb418e 9148 | f64_frac);
e07be5d2 9149 } else {
c2fb418e 9150 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 9151 | (0x3fdULL << 52)
c2fb418e 9152 | f64_frac);
e07be5d2
CL
9153 }
9154
c2fb418e 9155 result_exp = (380 - f32_exp) / 2;
e07be5d2 9156
c2fb418e 9157 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
9158
9159 val64 = float64_val(f64);
9160
26cc6abf 9161 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
9162 | ((val64 >> 29) & 0x7fffff);
9163 return make_float32(val);
4373f3ce
PB
9164}
9165
c2fb418e
AB
9166float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
9167{
9168 float_status *s = fpstp;
9169 float64 f64 = float64_squash_input_denormal(input, s);
9170 uint64_t val = float64_val(f64);
9171 uint64_t f64_sbit = 0x8000000000000000ULL & val;
9172 int64_t f64_exp = extract64(val, 52, 11);
9173 uint64_t f64_frac = extract64(val, 0, 52);
9174 int64_t result_exp;
9175 uint64_t result_frac;
9176
9177 if (float64_is_any_nan(f64)) {
9178 float64 nan = f64;
9179 if (float64_is_signaling_nan(f64)) {
9180 float_raise(float_flag_invalid, s);
9181 nan = float64_maybe_silence_nan(f64);
9182 }
9183 if (s->default_nan_mode) {
9184 nan = float64_default_nan;
9185 }
9186 return nan;
9187 } else if (float64_is_zero(f64)) {
9188 float_raise(float_flag_divbyzero, s);
9189 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9190 } else if (float64_is_neg(f64)) {
9191 float_raise(float_flag_invalid, s);
9192 return float64_default_nan;
9193 } else if (float64_is_infinity(f64)) {
9194 return float64_zero;
9195 }
9196
9197 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
9198 * preserving the parity of the exponent. */
9199
9200 if (f64_exp == 0) {
9201 while (extract64(f64_frac, 51, 1) == 0) {
9202 f64_frac = f64_frac << 1;
9203 f64_exp = f64_exp - 1;
9204 }
9205 f64_frac = extract64(f64_frac, 0, 51) << 1;
9206 }
9207
9208 if (extract64(f64_exp, 0, 1) == 0) {
9209 f64 = make_float64(f64_sbit
9210 | (0x3feULL << 52)
9211 | f64_frac);
9212 } else {
9213 f64 = make_float64(f64_sbit
9214 | (0x3fdULL << 52)
9215 | f64_frac);
9216 }
9217
9218 result_exp = (3068 - f64_exp) / 2;
9219
9220 f64 = recip_sqrt_estimate(f64, s);
9221
9222 result_frac = extract64(float64_val(f64), 0, 52);
9223
9224 return make_float64(f64_sbit |
9225 ((result_exp & 0x7ff) << 52) |
9226 result_frac);
9227}
9228
b6d4443a 9229uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 9230{
b6d4443a 9231 float_status *s = fpstp;
fe0e4872
CL
9232 float64 f64;
9233
9234 if ((a & 0x80000000) == 0) {
9235 return 0xffffffff;
9236 }
9237
9238 f64 = make_float64((0x3feULL << 52)
9239 | ((int64_t)(a & 0x7fffffff) << 21));
9240
b6d4443a 9241 f64 = recip_estimate(f64, s);
fe0e4872
CL
9242
9243 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
9244}
9245
c2fb418e 9246uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 9247{
c2fb418e 9248 float_status *fpst = fpstp;
e07be5d2
CL
9249 float64 f64;
9250
9251 if ((a & 0xc0000000) == 0) {
9252 return 0xffffffff;
9253 }
9254
9255 if (a & 0x80000000) {
9256 f64 = make_float64((0x3feULL << 52)
9257 | ((uint64_t)(a & 0x7fffffff) << 21));
9258 } else { /* bits 31-30 == '01' */
9259 f64 = make_float64((0x3fdULL << 52)
9260 | ((uint64_t)(a & 0x3fffffff) << 22));
9261 }
9262
c2fb418e 9263 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
9264
9265 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 9266}
fe1479c3 9267
da97f52c
PM
9268/* VFPv4 fused multiply-accumulate */
9269float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
9270{
9271 float_status *fpst = fpstp;
9272 return float32_muladd(a, b, c, 0, fpst);
9273}
9274
9275float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
9276{
9277 float_status *fpst = fpstp;
9278 return float64_muladd(a, b, c, 0, fpst);
9279}
d9b0848d
PM
9280
9281/* ARMv8 round to integral */
9282float32 HELPER(rints_exact)(float32 x, void *fp_status)
9283{
9284 return float32_round_to_int(x, fp_status);
9285}
9286
9287float64 HELPER(rintd_exact)(float64 x, void *fp_status)
9288{
9289 return float64_round_to_int(x, fp_status);
9290}
9291
9292float32 HELPER(rints)(float32 x, void *fp_status)
9293{
9294 int old_flags = get_float_exception_flags(fp_status), new_flags;
9295 float32 ret;
9296
9297 ret = float32_round_to_int(x, fp_status);
9298
9299 /* Suppress any inexact exceptions the conversion produced */
9300 if (!(old_flags & float_flag_inexact)) {
9301 new_flags = get_float_exception_flags(fp_status);
9302 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9303 }
9304
9305 return ret;
9306}
9307
9308float64 HELPER(rintd)(float64 x, void *fp_status)
9309{
9310 int old_flags = get_float_exception_flags(fp_status), new_flags;
9311 float64 ret;
9312
9313 ret = float64_round_to_int(x, fp_status);
9314
9315 new_flags = get_float_exception_flags(fp_status);
9316
9317 /* Suppress any inexact exceptions the conversion produced */
9318 if (!(old_flags & float_flag_inexact)) {
9319 new_flags = get_float_exception_flags(fp_status);
9320 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9321 }
9322
9323 return ret;
9324}
9972da66
WN
9325
9326/* Convert ARM rounding mode to softfloat */
9327int arm_rmode_to_sf(int rmode)
9328{
9329 switch (rmode) {
9330 case FPROUNDING_TIEAWAY:
9331 rmode = float_round_ties_away;
9332 break;
9333 case FPROUNDING_ODD:
9334 /* FIXME: add support for TIEAWAY and ODD */
9335 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
9336 rmode);
9337 case FPROUNDING_TIEEVEN:
9338 default:
9339 rmode = float_round_nearest_even;
9340 break;
9341 case FPROUNDING_POSINF:
9342 rmode = float_round_up;
9343 break;
9344 case FPROUNDING_NEGINF:
9345 rmode = float_round_down;
9346 break;
9347 case FPROUNDING_ZERO:
9348 rmode = float_round_to_zero;
9349 break;
9350 }
9351 return rmode;
9352}
eb0ecd5a 9353
aa633469
PM
9354/* CRC helpers.
9355 * The upper bytes of val (above the number specified by 'bytes') must have
9356 * been zeroed out by the caller.
9357 */
eb0ecd5a
WN
9358uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
9359{
9360 uint8_t buf[4];
9361
aa633469 9362 stl_le_p(buf, val);
eb0ecd5a
WN
9363
9364 /* zlib crc32 converts the accumulator and output to one's complement. */
9365 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
9366}
9367
9368uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
9369{
9370 uint8_t buf[4];
9371
aa633469 9372 stl_le_p(buf, val);
eb0ecd5a
WN
9373
9374 /* Linux crc32c converts the output to one's complement. */
9375 return crc32c(acc, buf, bytes) ^ 0xffffffff;
9376}