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