]> git.proxmox.com Git - mirror_qemu.git/blame - target-arm/helper.c
target-arm: Don't define any MMU_MODE*_SUFFIXes
[mirror_qemu.git] / target-arm / helper.c
CommitLineData
b5ff1b31 1#include "cpu.h"
ccd38087 2#include "internals.h"
022c62cb 3#include "exec/gdbstub.h"
2ef6175a 4#include "exec/helper-proto.h"
1de7afc9 5#include "qemu/host-utils.h"
78027bb6 6#include "sysemu/arch_init.h"
9c17d615 7#include "sysemu/sysemu.h"
1de7afc9 8#include "qemu/bitops.h"
eb0ecd5a 9#include "qemu/crc32c.h"
f08b6170 10#include "exec/cpu_ldst.h"
1d854765 11#include "arm_ldst.h"
eb0ecd5a 12#include <zlib.h> /* For crc32 */
0b03bdfc 13
4a501606 14#ifndef CONFIG_USER_ONLY
2c8dd318 15static inline int get_phys_addr(CPUARMState *env, target_ulong address,
4a501606 16 int access_type, int is_user,
a8170e5e 17 hwaddr *phys_ptr, int *prot,
4a501606 18 target_ulong *page_size);
7c2cb42b
AF
19
20/* Definitions for the PMCCNTR and PMCR registers */
21#define PMCRD 0x8
22#define PMCRC 0x4
23#define PMCRE 0x1
4a501606
PM
24#endif
25
0ecb72a5 26static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
27{
28 int nregs;
29
30 /* VFP data registers are always little-endian. */
31 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
32 if (reg < nregs) {
33 stfq_le_p(buf, env->vfp.regs[reg]);
34 return 8;
35 }
36 if (arm_feature(env, ARM_FEATURE_NEON)) {
37 /* Aliases for Q regs. */
38 nregs += 16;
39 if (reg < nregs) {
40 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
41 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
42 return 16;
43 }
44 }
45 switch (reg - nregs) {
46 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
47 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
48 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
49 }
50 return 0;
51}
52
0ecb72a5 53static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
56aebc89
PB
54{
55 int nregs;
56
57 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
58 if (reg < nregs) {
59 env->vfp.regs[reg] = ldfq_le_p(buf);
60 return 8;
61 }
62 if (arm_feature(env, ARM_FEATURE_NEON)) {
63 nregs += 16;
64 if (reg < nregs) {
65 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
66 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
67 return 16;
68 }
69 }
70 switch (reg - nregs) {
71 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
72 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 73 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
74 }
75 return 0;
76}
77
6a669427
PM
78static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
79{
80 switch (reg) {
81 case 0 ... 31:
82 /* 128 bit FP register */
83 stfq_le_p(buf, env->vfp.regs[reg * 2]);
84 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
85 return 16;
86 case 32:
87 /* FPSR */
88 stl_p(buf, vfp_get_fpsr(env));
89 return 4;
90 case 33:
91 /* FPCR */
92 stl_p(buf, vfp_get_fpcr(env));
93 return 4;
94 default:
95 return 0;
96 }
97}
98
99static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
100{
101 switch (reg) {
102 case 0 ... 31:
103 /* 128 bit FP register */
104 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
105 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
106 return 16;
107 case 32:
108 /* FPSR */
109 vfp_set_fpsr(env, ldl_p(buf));
110 return 4;
111 case 33:
112 /* FPCR */
113 vfp_set_fpcr(env, ldl_p(buf));
114 return 4;
115 default:
116 return 0;
117 }
118}
119
c4241c7d 120static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 121{
375421cc 122 assert(ri->fieldoffset);
67ed771d 123 if (cpreg_field_is_64bit(ri)) {
c4241c7d 124 return CPREG_FIELD64(env, ri);
22d9e1a9 125 } else {
c4241c7d 126 return CPREG_FIELD32(env, ri);
22d9e1a9 127 }
d4e6df63
PM
128}
129
c4241c7d
PM
130static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
131 uint64_t value)
d4e6df63 132{
375421cc 133 assert(ri->fieldoffset);
67ed771d 134 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
135 CPREG_FIELD64(env, ri) = value;
136 } else {
137 CPREG_FIELD32(env, ri) = value;
138 }
d4e6df63
PM
139}
140
11f136ee
FA
141static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
142{
143 return (char *)env + ri->fieldoffset;
144}
145
59a1c327 146static uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 147{
59a1c327 148 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 149 if (ri->type & ARM_CP_CONST) {
59a1c327 150 return ri->resetvalue;
721fae12 151 } else if (ri->raw_readfn) {
59a1c327 152 return ri->raw_readfn(env, ri);
721fae12 153 } else if (ri->readfn) {
59a1c327 154 return ri->readfn(env, ri);
721fae12 155 } else {
59a1c327 156 return raw_read(env, ri);
721fae12 157 }
721fae12
PM
158}
159
59a1c327 160static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 161 uint64_t v)
721fae12
PM
162{
163 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
164 * Note that constant registers are treated as write-ignored; the
165 * caller should check for success by whether a readback gives the
166 * value written.
167 */
168 if (ri->type & ARM_CP_CONST) {
59a1c327 169 return;
721fae12 170 } else if (ri->raw_writefn) {
c4241c7d 171 ri->raw_writefn(env, ri, v);
721fae12 172 } else if (ri->writefn) {
c4241c7d 173 ri->writefn(env, ri, v);
721fae12 174 } else {
afb2530f 175 raw_write(env, ri, v);
721fae12 176 }
721fae12
PM
177}
178
375421cc
PM
179static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
180{
181 /* Return true if the regdef would cause an assertion if you called
182 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
183 * program bug for it not to have the NO_RAW flag).
184 * NB that returning false here doesn't necessarily mean that calling
185 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
186 * read/write access functions which are safe for raw use" from "has
187 * read/write access functions which have side effects but has forgotten
188 * to provide raw access functions".
189 * The tests here line up with the conditions in read/write_raw_cp_reg()
190 * and assertions in raw_read()/raw_write().
191 */
192 if ((ri->type & ARM_CP_CONST) ||
193 ri->fieldoffset ||
194 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
195 return false;
196 }
197 return true;
198}
199
721fae12
PM
200bool write_cpustate_to_list(ARMCPU *cpu)
201{
202 /* Write the coprocessor state from cpu->env to the (index,value) list. */
203 int i;
204 bool ok = true;
205
206 for (i = 0; i < cpu->cpreg_array_len; i++) {
207 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
208 const ARMCPRegInfo *ri;
59a1c327 209
60322b39 210 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
211 if (!ri) {
212 ok = false;
213 continue;
214 }
7a0e58fa 215 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
216 continue;
217 }
59a1c327 218 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
721fae12
PM
219 }
220 return ok;
221}
222
223bool write_list_to_cpustate(ARMCPU *cpu)
224{
225 int i;
226 bool ok = true;
227
228 for (i = 0; i < cpu->cpreg_array_len; i++) {
229 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
230 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
231 const ARMCPRegInfo *ri;
232
60322b39 233 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
234 if (!ri) {
235 ok = false;
236 continue;
237 }
7a0e58fa 238 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
239 continue;
240 }
241 /* Write value and confirm it reads back as written
242 * (to catch read-only registers and partially read-only
243 * registers where the incoming migration value doesn't match)
244 */
59a1c327
PM
245 write_raw_cp_reg(&cpu->env, ri, v);
246 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
247 ok = false;
248 }
249 }
250 return ok;
251}
252
253static void add_cpreg_to_list(gpointer key, gpointer opaque)
254{
255 ARMCPU *cpu = opaque;
256 uint64_t regidx;
257 const ARMCPRegInfo *ri;
258
259 regidx = *(uint32_t *)key;
60322b39 260 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 261
7a0e58fa 262 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
263 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
264 /* The value array need not be initialized at this point */
265 cpu->cpreg_array_len++;
266 }
267}
268
269static void count_cpreg(gpointer key, gpointer opaque)
270{
271 ARMCPU *cpu = opaque;
272 uint64_t regidx;
273 const ARMCPRegInfo *ri;
274
275 regidx = *(uint32_t *)key;
60322b39 276 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 277
7a0e58fa 278 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
279 cpu->cpreg_array_len++;
280 }
281}
282
283static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
284{
cbf239b7
AR
285 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
286 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 287
cbf239b7
AR
288 if (aidx > bidx) {
289 return 1;
290 }
291 if (aidx < bidx) {
292 return -1;
293 }
294 return 0;
721fae12
PM
295}
296
82a3a118
PM
297static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
298{
299 GList **plist = udata;
300
301 *plist = g_list_prepend(*plist, key);
302}
303
721fae12
PM
304void init_cpreg_list(ARMCPU *cpu)
305{
306 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
307 * Note that we require cpreg_tuples[] to be sorted by key ID.
308 */
82a3a118 309 GList *keys = NULL;
721fae12
PM
310 int arraylen;
311
82a3a118
PM
312 g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
313
721fae12
PM
314 keys = g_list_sort(keys, cpreg_key_compare);
315
316 cpu->cpreg_array_len = 0;
317
318 g_list_foreach(keys, count_cpreg, cpu);
319
320 arraylen = cpu->cpreg_array_len;
321 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
322 cpu->cpreg_values = g_new(uint64_t, arraylen);
323 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
324 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
325 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
326 cpu->cpreg_array_len = 0;
327
328 g_list_foreach(keys, add_cpreg_to_list, cpu);
329
330 assert(cpu->cpreg_array_len == arraylen);
331
332 g_list_free(keys);
333}
334
c4241c7d 335static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 336{
00c8cb0a
AF
337 ARMCPU *cpu = arm_env_get_cpu(env);
338
8d5c773e 339 raw_write(env, ri, value);
00c8cb0a 340 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
341}
342
c4241c7d 343static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 344{
00c8cb0a
AF
345 ARMCPU *cpu = arm_env_get_cpu(env);
346
8d5c773e 347 if (raw_read(env, ri) != value) {
08de207b
PM
348 /* Unlike real hardware the qemu TLB uses virtual addresses,
349 * not modified virtual addresses, so this causes a TLB flush.
350 */
00c8cb0a 351 tlb_flush(CPU(cpu), 1);
8d5c773e 352 raw_write(env, ri, value);
08de207b 353 }
08de207b 354}
c4241c7d
PM
355
356static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
357 uint64_t value)
08de207b 358{
00c8cb0a
AF
359 ARMCPU *cpu = arm_env_get_cpu(env);
360
8d5c773e 361 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
014406b5 362 && !extended_addresses_enabled(env)) {
08de207b
PM
363 /* For VMSA (when not using the LPAE long descriptor page table
364 * format) this register includes the ASID, so do a TLB flush.
365 * For PMSA it is purely a process ID and no action is needed.
366 */
00c8cb0a 367 tlb_flush(CPU(cpu), 1);
08de207b 368 }
8d5c773e 369 raw_write(env, ri, value);
08de207b
PM
370}
371
c4241c7d
PM
372static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
373 uint64_t value)
d929823f
PM
374{
375 /* Invalidate all (TLBIALL) */
00c8cb0a
AF
376 ARMCPU *cpu = arm_env_get_cpu(env);
377
378 tlb_flush(CPU(cpu), 1);
d929823f
PM
379}
380
c4241c7d
PM
381static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
382 uint64_t value)
d929823f
PM
383{
384 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
31b030d4
AF
385 ARMCPU *cpu = arm_env_get_cpu(env);
386
387 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
388}
389
c4241c7d
PM
390static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
391 uint64_t value)
d929823f
PM
392{
393 /* Invalidate by ASID (TLBIASID) */
00c8cb0a
AF
394 ARMCPU *cpu = arm_env_get_cpu(env);
395
396 tlb_flush(CPU(cpu), value == 0);
d929823f
PM
397}
398
c4241c7d
PM
399static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
400 uint64_t value)
d929823f
PM
401{
402 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
31b030d4
AF
403 ARMCPU *cpu = arm_env_get_cpu(env);
404
405 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
d929823f
PM
406}
407
fa439fc5
PM
408/* IS variants of TLB operations must affect all cores */
409static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
410 uint64_t value)
411{
412 CPUState *other_cs;
413
414 CPU_FOREACH(other_cs) {
415 tlb_flush(other_cs, 1);
416 }
417}
418
419static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
420 uint64_t value)
421{
422 CPUState *other_cs;
423
424 CPU_FOREACH(other_cs) {
425 tlb_flush(other_cs, value == 0);
426 }
427}
428
429static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
430 uint64_t value)
431{
432 CPUState *other_cs;
433
434 CPU_FOREACH(other_cs) {
435 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
436 }
437}
438
439static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
440 uint64_t value)
441{
442 CPUState *other_cs;
443
444 CPU_FOREACH(other_cs) {
445 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
446 }
447}
448
e9aa6c21 449static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
450 /* Define the secure and non-secure FCSE identifier CP registers
451 * separately because there is no secure bank in V8 (no _EL3). This allows
452 * the secure register to be properly reset and migrated. There is also no
453 * v8 EL1 version of the register so the non-secure instance stands alone.
454 */
455 { .name = "FCSEIDR(NS)",
456 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
457 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
458 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
459 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
460 { .name = "FCSEIDR(S)",
461 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
462 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
463 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 464 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
465 /* Define the secure and non-secure context identifier CP registers
466 * separately because there is no secure bank in V8 (no _EL3). This allows
467 * the secure register to be properly reset and migrated. In the
468 * non-secure case, the 32-bit register will have reset and migration
469 * disabled during registration as it is handled by the 64-bit instance.
470 */
471 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 472 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
54bf36ed
FA
473 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
474 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
475 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
476 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
477 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
478 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
479 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 480 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
481 REGINFO_SENTINEL
482};
483
484static const ARMCPRegInfo not_v8_cp_reginfo[] = {
485 /* NB: Some of these registers exist in v8 but with more precise
486 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
487 */
488 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
489 { .name = "DACR",
490 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
491 .access = PL1_RW, .resetvalue = 0,
492 .writefn = dacr_write, .raw_writefn = raw_write,
493 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
494 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
4fdd17dd
PM
495 /* ??? This covers not just the impdef TLB lockdown registers but also
496 * some v7VMSA registers relating to TEX remap, so it is overly broad.
497 */
498 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
499 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
500 /* Cache maintenance ops; some of this space may be overridden later. */
501 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
502 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
503 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
504 REGINFO_SENTINEL
505};
506
7d57f408
PM
507static const ARMCPRegInfo not_v6_cp_reginfo[] = {
508 /* Not all pre-v6 cores implemented this WFI, so this is slightly
509 * over-broad.
510 */
511 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
512 .access = PL1_W, .type = ARM_CP_WFI },
513 REGINFO_SENTINEL
514};
515
516static const ARMCPRegInfo not_v7_cp_reginfo[] = {
517 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
518 * is UNPREDICTABLE; we choose to NOP as most implementations do).
519 */
520 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
521 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
522 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
523 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
524 * OMAPCP will override this space.
525 */
526 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
527 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
528 .resetvalue = 0 },
529 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
530 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
531 .resetvalue = 0 },
776d4e5c
PM
532 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
533 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 534 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 535 .resetvalue = 0 },
50300698
PM
536 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
537 * implementing it as RAZ means the "debug architecture version" bits
538 * will read as a reserved value, which should cause Linux to not try
539 * to use the debug hardware.
540 */
541 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
542 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
543 /* MMU TLB control. Note that the wildcarding means we cover not just
544 * the unified TLB ops but also the dside/iside/inner-shareable variants.
545 */
546 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
547 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 548 .type = ARM_CP_NO_RAW },
995939a6
PM
549 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
550 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 551 .type = ARM_CP_NO_RAW },
995939a6
PM
552 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
553 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 554 .type = ARM_CP_NO_RAW },
995939a6
PM
555 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
556 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 557 .type = ARM_CP_NO_RAW },
7d57f408
PM
558 REGINFO_SENTINEL
559};
560
c4241c7d
PM
561static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
562 uint64_t value)
2771db27 563{
f0aff255
FA
564 uint32_t mask = 0;
565
566 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
567 if (!arm_feature(env, ARM_FEATURE_V8)) {
568 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
569 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
570 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
571 */
572 if (arm_feature(env, ARM_FEATURE_VFP)) {
573 /* VFP coprocessor: cp10 & cp11 [23:20] */
574 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
575
576 if (!arm_feature(env, ARM_FEATURE_NEON)) {
577 /* ASEDIS [31] bit is RAO/WI */
578 value |= (1 << 31);
579 }
580
581 /* VFPv3 and upwards with NEON implement 32 double precision
582 * registers (D0-D31).
583 */
584 if (!arm_feature(env, ARM_FEATURE_NEON) ||
585 !arm_feature(env, ARM_FEATURE_VFP3)) {
586 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
587 value |= (1 << 30);
588 }
589 }
590 value &= mask;
2771db27 591 }
f0aff255 592 env->cp15.c1_coproc = value;
2771db27
PM
593}
594
7d57f408
PM
595static const ARMCPRegInfo v6_cp_reginfo[] = {
596 /* prefetch by MVA in v6, NOP in v7 */
597 { .name = "MVA_prefetch",
598 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
599 .access = PL1_W, .type = ARM_CP_NOP },
600 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
601 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 602 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 603 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 604 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 605 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 606 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
6cd8a264 607 .access = PL1_RW,
b848ce2b
FA
608 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
609 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
610 .resetvalue = 0, },
611 /* Watchpoint Fault Address Register : should actually only be present
612 * for 1136, 1176, 11MPCore.
613 */
614 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
615 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8
PM
616 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
617 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
2771db27
PM
618 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
619 .resetvalue = 0, .writefn = cpacr_write },
7d57f408
PM
620 REGINFO_SENTINEL
621};
622
fcd25206 623static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
200ac0ef 624{
3b163b01 625 /* Performance monitor registers user accessibility is controlled
fcd25206 626 * by PMUSERENR.
200ac0ef 627 */
dcbff19b 628 if (arm_current_el(env) == 0 && !env->cp15.c9_pmuserenr) {
fcd25206 629 return CP_ACCESS_TRAP;
200ac0ef 630 }
fcd25206 631 return CP_ACCESS_OK;
200ac0ef
PM
632}
633
7c2cb42b 634#ifndef CONFIG_USER_ONLY
87124fde
AF
635
636static inline bool arm_ccnt_enabled(CPUARMState *env)
637{
638 /* This does not support checking PMCCFILTR_EL0 register */
639
640 if (!(env->cp15.c9_pmcr & PMCRE)) {
641 return false;
642 }
643
644 return true;
645}
646
ec7b4ce4
AF
647void pmccntr_sync(CPUARMState *env)
648{
649 uint64_t temp_ticks;
650
651 temp_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
652 get_ticks_per_sec(), 1000000);
653
654 if (env->cp15.c9_pmcr & PMCRD) {
655 /* Increment once every 64 processor clock cycles */
656 temp_ticks /= 64;
657 }
658
659 if (arm_ccnt_enabled(env)) {
660 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
661 }
662}
663
c4241c7d
PM
664static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
665 uint64_t value)
200ac0ef 666{
942a155b 667 pmccntr_sync(env);
7c2cb42b
AF
668
669 if (value & PMCRC) {
670 /* The counter has been reset */
671 env->cp15.c15_ccnt = 0;
672 }
673
200ac0ef
PM
674 /* only the DP, X, D and E bits are writable */
675 env->cp15.c9_pmcr &= ~0x39;
676 env->cp15.c9_pmcr |= (value & 0x39);
7c2cb42b 677
942a155b 678 pmccntr_sync(env);
7c2cb42b
AF
679}
680
681static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
682{
c92c0687 683 uint64_t total_ticks;
7c2cb42b 684
942a155b 685 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
686 /* Counter is disabled, do not change value */
687 return env->cp15.c15_ccnt;
688 }
689
c92c0687
AF
690 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
691 get_ticks_per_sec(), 1000000);
7c2cb42b
AF
692
693 if (env->cp15.c9_pmcr & PMCRD) {
694 /* Increment once every 64 processor clock cycles */
695 total_ticks /= 64;
696 }
697 return total_ticks - env->cp15.c15_ccnt;
698}
699
700static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
701 uint64_t value)
702{
c92c0687 703 uint64_t total_ticks;
7c2cb42b 704
942a155b 705 if (!arm_ccnt_enabled(env)) {
7c2cb42b
AF
706 /* Counter is disabled, set the absolute value */
707 env->cp15.c15_ccnt = value;
708 return;
709 }
710
c92c0687
AF
711 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
712 get_ticks_per_sec(), 1000000);
7c2cb42b
AF
713
714 if (env->cp15.c9_pmcr & PMCRD) {
715 /* Increment once every 64 processor clock cycles */
716 total_ticks /= 64;
717 }
718 env->cp15.c15_ccnt = total_ticks - value;
200ac0ef 719}
421c7ebd
PC
720
721static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
722 uint64_t value)
723{
724 uint64_t cur_val = pmccntr_read(env, NULL);
725
726 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
727}
728
ec7b4ce4
AF
729#else /* CONFIG_USER_ONLY */
730
731void pmccntr_sync(CPUARMState *env)
732{
733}
734
7c2cb42b 735#endif
200ac0ef 736
0614601c
AF
737static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
738 uint64_t value)
739{
740 pmccntr_sync(env);
741 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
742 pmccntr_sync(env);
743}
744
c4241c7d 745static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
746 uint64_t value)
747{
200ac0ef
PM
748 value &= (1 << 31);
749 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
750}
751
c4241c7d
PM
752static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
753 uint64_t value)
200ac0ef 754{
200ac0ef
PM
755 value &= (1 << 31);
756 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
757}
758
c4241c7d
PM
759static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
760 uint64_t value)
200ac0ef 761{
200ac0ef 762 env->cp15.c9_pmovsr &= ~value;
200ac0ef
PM
763}
764
c4241c7d
PM
765static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
766 uint64_t value)
200ac0ef 767{
200ac0ef 768 env->cp15.c9_pmxevtyper = value & 0xff;
200ac0ef
PM
769}
770
c4241c7d 771static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
772 uint64_t value)
773{
774 env->cp15.c9_pmuserenr = value & 1;
200ac0ef
PM
775}
776
c4241c7d
PM
777static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
778 uint64_t value)
200ac0ef
PM
779{
780 /* We have no event counters so only the C bit can be changed */
781 value &= (1 << 31);
782 env->cp15.c9_pminten |= value;
200ac0ef
PM
783}
784
c4241c7d
PM
785static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
786 uint64_t value)
200ac0ef
PM
787{
788 value &= (1 << 31);
789 env->cp15.c9_pminten &= ~value;
200ac0ef
PM
790}
791
c4241c7d
PM
792static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
793 uint64_t value)
8641136c 794{
a505d7fe
PM
795 /* Note that even though the AArch64 view of this register has bits
796 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
797 * architectural requirements for bits which are RES0 only in some
798 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
799 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
800 */
855ea66d 801 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
802}
803
64e0e2de
EI
804static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
805{
806 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
807 * For bits that vary between AArch32/64, code needs to check the
808 * current execution mode before directly using the feature bit.
809 */
810 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
811
812 if (!arm_feature(env, ARM_FEATURE_EL2)) {
813 valid_mask &= ~SCR_HCE;
814
815 /* On ARMv7, SMD (or SCD as it is called in v7) is only
816 * supported if EL2 exists. The bit is UNK/SBZP when
817 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
818 * when EL2 is unavailable.
819 */
820 if (arm_feature(env, ARM_FEATURE_V7)) {
821 valid_mask &= ~SCR_SMD;
822 }
823 }
824
825 /* Clear all-context RES0 bits. */
826 value &= valid_mask;
827 raw_write(env, ri, value);
828}
829
c4241c7d 830static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c
PM
831{
832 ARMCPU *cpu = arm_env_get_cpu(env);
b85a1fd6
FA
833
834 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
835 * bank
836 */
837 uint32_t index = A32_BANKED_REG_GET(env, csselr,
838 ri->secure & ARM_CP_SECSTATE_S);
839
840 return cpu->ccsidr[index];
776d4e5c
PM
841}
842
c4241c7d
PM
843static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
844 uint64_t value)
776d4e5c 845{
8d5c773e 846 raw_write(env, ri, value & 0xf);
776d4e5c
PM
847}
848
1090b9c6
PM
849static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
850{
851 CPUState *cs = ENV_GET_CPU(env);
852 uint64_t ret = 0;
853
854 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
855 ret |= CPSR_I;
856 }
857 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
858 ret |= CPSR_F;
859 }
860 /* External aborts are not possible in QEMU so A bit is always clear */
861 return ret;
862}
863
e9aa6c21 864static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
865 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
866 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
867 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
868 /* Performance monitors are implementation defined in v7,
869 * but with an ARM recommended set of registers, which we
870 * follow (although we don't actually implement any counters)
871 *
872 * Performance registers fall into three categories:
873 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
874 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
875 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
876 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
877 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
878 */
879 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 880 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 881 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
882 .writefn = pmcntenset_write,
883 .accessfn = pmreg_access,
884 .raw_writefn = raw_write },
8521466b
AF
885 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
886 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
887 .access = PL0_RW, .accessfn = pmreg_access,
888 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
889 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 890 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
891 .access = PL0_RW,
892 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
893 .accessfn = pmreg_access,
894 .writefn = pmcntenclr_write,
7a0e58fa 895 .type = ARM_CP_ALIAS },
8521466b
AF
896 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
897 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
898 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 899 .type = ARM_CP_ALIAS,
8521466b
AF
900 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
901 .writefn = pmcntenclr_write },
200ac0ef
PM
902 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
903 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
904 .accessfn = pmreg_access,
905 .writefn = pmovsr_write,
906 .raw_writefn = raw_write },
907 /* Unimplemented so WI. */
200ac0ef 908 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
fcd25206 909 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
200ac0ef 910 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
fcd25206 911 * We choose to RAZ/WI.
200ac0ef
PM
912 */
913 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
fcd25206
PM
914 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
915 .accessfn = pmreg_access },
7c2cb42b 916#ifndef CONFIG_USER_ONLY
200ac0ef 917 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
7c2cb42b 918 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
421c7ebd 919 .readfn = pmccntr_read, .writefn = pmccntr_write32,
fcd25206 920 .accessfn = pmreg_access },
8521466b
AF
921 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
922 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
923 .access = PL0_RW, .accessfn = pmreg_access,
924 .type = ARM_CP_IO,
925 .readfn = pmccntr_read, .writefn = pmccntr_write, },
7c2cb42b 926#endif
8521466b
AF
927 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
928 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
0614601c 929 .writefn = pmccfiltr_write,
8521466b
AF
930 .access = PL0_RW, .accessfn = pmreg_access,
931 .type = ARM_CP_IO,
932 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
933 .resetvalue = 0, },
200ac0ef
PM
934 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
935 .access = PL0_RW,
936 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
fcd25206
PM
937 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
938 .raw_writefn = raw_write },
939 /* Unimplemented, RAZ/WI. */
200ac0ef 940 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
fcd25206
PM
941 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
942 .accessfn = pmreg_access },
200ac0ef
PM
943 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
944 .access = PL0_R | PL1_RW,
945 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
946 .resetvalue = 0,
d4e6df63 947 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef
PM
948 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
949 .access = PL1_RW,
950 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
951 .resetvalue = 0,
d4e6df63 952 .writefn = pmintenset_write, .raw_writefn = raw_write },
200ac0ef 953 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
7a0e58fa 954 .access = PL1_RW, .type = ARM_CP_ALIAS,
200ac0ef 955 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
d4e6df63 956 .resetvalue = 0, .writefn = pmintenclr_write, },
a505d7fe
PM
957 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
958 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8641136c 959 .access = PL1_RW, .writefn = vbar_write,
fb6c91ba
GB
960 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
961 offsetof(CPUARMState, cp15.vbar_ns) },
8641136c 962 .resetvalue = 0 },
7da845b0
PM
963 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
964 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
7a0e58fa 965 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
966 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
967 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
b85a1fd6
FA
968 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
969 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
970 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
971 /* Auxiliary ID register: this actually has an IMPDEF value but for now
972 * just RAZ for all cores:
973 */
0ff644a7
PM
974 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
975 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
776d4e5c 976 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
977 /* Auxiliary fault status registers: these also are IMPDEF, and we
978 * choose to RAZ/WI for all cores.
979 */
980 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
981 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
982 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
983 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
984 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
985 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
986 /* MAIR can just read-as-written because we don't implement caches
987 * and so don't need to care about memory attributes.
988 */
989 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
990 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
be693c87 991 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427
PM
992 .resetvalue = 0 },
993 /* For non-long-descriptor page tables these are PRRR and NMRR;
994 * regardless they still act as reads-as-written for QEMU.
995 * The override is necessary because of the overly-broad TLB_LOCKDOWN
996 * definition.
997 */
1281f8e3 998 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
999 * allows them to assign the correct fieldoffset based on the endianness
1000 * handled in the field definitions.
1001 */
b0fe2427
PM
1002 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
1003 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
be693c87
GB
1004 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1005 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427
PM
1006 .resetfn = arm_cp_reset_ignore },
1007 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
1008 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
be693c87
GB
1009 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1010 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 1011 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
1012 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1013 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 1014 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
1015 /* 32 bit ITLB invalidates */
1016 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
7a0e58fa 1017 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1018 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7a0e58fa 1019 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1020 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
7a0e58fa 1021 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1022 /* 32 bit DTLB invalidates */
1023 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
7a0e58fa 1024 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1025 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7a0e58fa 1026 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1027 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
7a0e58fa 1028 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6
PM
1029 /* 32 bit TLB invalidates */
1030 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 1031 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
995939a6 1032 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 1033 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
995939a6 1034 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 1035 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
995939a6 1036 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 1037 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
995939a6
PM
1038 REGINFO_SENTINEL
1039};
1040
1041static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1042 /* 32 bit TLB invalidates, Inner Shareable */
1043 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 1044 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
995939a6 1045 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 1046 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
995939a6 1047 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 1048 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1049 .writefn = tlbiasid_is_write },
995939a6 1050 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 1051 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 1052 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
1053 REGINFO_SENTINEL
1054};
1055
c4241c7d
PM
1056static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1057 uint64_t value)
c326b979
PM
1058{
1059 value &= 1;
1060 env->teecr = value;
c326b979
PM
1061}
1062
c4241c7d 1063static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
c326b979 1064{
dcbff19b 1065 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 1066 return CP_ACCESS_TRAP;
c326b979 1067 }
92611c00 1068 return CP_ACCESS_OK;
c326b979
PM
1069}
1070
1071static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1072 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1073 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1074 .resetvalue = 0,
1075 .writefn = teecr_write },
1076 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1077 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 1078 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
1079 REGINFO_SENTINEL
1080};
1081
4d31c596 1082static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
1083 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1084 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1085 .access = PL0_RW,
54bf36ed 1086 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
1087 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1088 .access = PL0_RW,
54bf36ed
FA
1089 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1090 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
1091 .resetfn = arm_cp_reset_ignore },
1092 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1093 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1094 .access = PL0_R|PL1_W,
54bf36ed
FA
1095 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1096 .resetvalue = 0},
4d31c596
PM
1097 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1098 .access = PL0_R|PL1_W,
54bf36ed
FA
1099 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1100 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 1101 .resetfn = arm_cp_reset_ignore },
54bf36ed 1102 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 1103 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 1104 .access = PL1_RW,
54bf36ed
FA
1105 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1106 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1107 .access = PL1_RW,
1108 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1109 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1110 .resetvalue = 0 },
4d31c596
PM
1111 REGINFO_SENTINEL
1112};
1113
55d284af
PM
1114#ifndef CONFIG_USER_ONLY
1115
00108f2d
PM
1116static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
1117{
1118 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
dcbff19b 1119 if (arm_current_el(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
00108f2d
PM
1120 return CP_ACCESS_TRAP;
1121 }
1122 return CP_ACCESS_OK;
1123}
1124
1125static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
1126{
1127 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
dcbff19b 1128 if (arm_current_el(env) == 0 &&
00108f2d
PM
1129 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1130 return CP_ACCESS_TRAP;
1131 }
1132 return CP_ACCESS_OK;
1133}
1134
1135static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
1136{
1137 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1138 * EL0[PV]TEN is zero.
1139 */
dcbff19b 1140 if (arm_current_el(env) == 0 &&
00108f2d
PM
1141 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1142 return CP_ACCESS_TRAP;
1143 }
1144 return CP_ACCESS_OK;
1145}
1146
1147static CPAccessResult gt_pct_access(CPUARMState *env,
1148 const ARMCPRegInfo *ri)
1149{
1150 return gt_counter_access(env, GTIMER_PHYS);
1151}
1152
1153static CPAccessResult gt_vct_access(CPUARMState *env,
1154 const ARMCPRegInfo *ri)
1155{
1156 return gt_counter_access(env, GTIMER_VIRT);
1157}
1158
1159static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1160{
1161 return gt_timer_access(env, GTIMER_PHYS);
1162}
1163
1164static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1165{
1166 return gt_timer_access(env, GTIMER_VIRT);
1167}
1168
55d284af
PM
1169static uint64_t gt_get_countervalue(CPUARMState *env)
1170{
bc72ad67 1171 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
55d284af
PM
1172}
1173
1174static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1175{
1176 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1177
1178 if (gt->ctl & 1) {
1179 /* Timer enabled: calculate and set current ISTATUS, irq, and
1180 * reset timer to when ISTATUS next has to change
1181 */
1182 uint64_t count = gt_get_countervalue(&cpu->env);
1183 /* Note that this must be unsigned 64 bit arithmetic: */
1184 int istatus = count >= gt->cval;
1185 uint64_t nexttick;
1186
1187 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1188 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1189 (istatus && !(gt->ctl & 2)));
1190 if (istatus) {
1191 /* Next transition is when count rolls back over to zero */
1192 nexttick = UINT64_MAX;
1193 } else {
1194 /* Next transition is when we hit cval */
1195 nexttick = gt->cval;
1196 }
1197 /* Note that the desired next expiry time might be beyond the
1198 * signed-64-bit range of a QEMUTimer -- in this case we just
1199 * set the timer for as far in the future as possible. When the
1200 * timer expires we will reset the timer for any remaining period.
1201 */
1202 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1203 nexttick = INT64_MAX / GTIMER_SCALE;
1204 }
bc72ad67 1205 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af
PM
1206 } else {
1207 /* Timer disabled: ISTATUS and timer output always clear */
1208 gt->ctl &= ~4;
1209 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 1210 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1211 }
1212}
1213
55d284af
PM
1214static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1215{
1216 ARMCPU *cpu = arm_env_get_cpu(env);
1217 int timeridx = ri->opc1 & 1;
1218
bc72ad67 1219 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
1220}
1221
c4241c7d 1222static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 1223{
c4241c7d 1224 return gt_get_countervalue(env);
55d284af
PM
1225}
1226
c4241c7d
PM
1227static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1228 uint64_t value)
55d284af
PM
1229{
1230 int timeridx = ri->opc1 & 1;
1231
1232 env->cp15.c14_timer[timeridx].cval = value;
1233 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af 1234}
c4241c7d
PM
1235
1236static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af
PM
1237{
1238 int timeridx = ri->crm & 1;
1239
c4241c7d
PM
1240 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1241 gt_get_countervalue(env));
55d284af
PM
1242}
1243
c4241c7d
PM
1244static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1245 uint64_t value)
55d284af
PM
1246{
1247 int timeridx = ri->crm & 1;
1248
1249 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
1250 + sextract64(value, 0, 32);
1251 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
55d284af
PM
1252}
1253
c4241c7d
PM
1254static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1255 uint64_t value)
55d284af
PM
1256{
1257 ARMCPU *cpu = arm_env_get_cpu(env);
1258 int timeridx = ri->crm & 1;
1259 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1260
d3afacc7 1261 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
1262 if ((oldval ^ value) & 1) {
1263 /* Enable toggled */
1264 gt_recalc_timer(cpu, timeridx);
d3afacc7 1265 } else if ((oldval ^ value) & 2) {
55d284af
PM
1266 /* IMASK toggled: don't need to recalculate,
1267 * just set the interrupt line based on ISTATUS
1268 */
1269 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
d3afacc7 1270 (oldval & 4) && !(value & 2));
55d284af 1271 }
55d284af
PM
1272}
1273
1274void arm_gt_ptimer_cb(void *opaque)
1275{
1276 ARMCPU *cpu = opaque;
1277
1278 gt_recalc_timer(cpu, GTIMER_PHYS);
1279}
1280
1281void arm_gt_vtimer_cb(void *opaque)
1282{
1283 ARMCPU *cpu = opaque;
1284
1285 gt_recalc_timer(cpu, GTIMER_VIRT);
1286}
1287
1288static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1289 /* Note that CNTFRQ is purely reads-as-written for the benefit
1290 * of software; writing it doesn't actually change the timer frequency.
1291 * Our reset value matches the fixed frequency we implement the timer at.
1292 */
1293 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1294 .type = ARM_CP_ALIAS,
a7adc4b7
PM
1295 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1296 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1297 .resetfn = arm_cp_reset_ignore,
1298 },
1299 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1300 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1301 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af
PM
1302 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1303 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
55d284af
PM
1304 },
1305 /* overall control: mostly access permissions */
a7adc4b7
PM
1306 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1307 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
1308 .access = PL1_RW,
1309 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1310 .resetvalue = 0,
1311 },
1312 /* per-timer control */
1313 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
7a0e58fa 1314 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1315 .accessfn = gt_ptimer_access,
1316 .fieldoffset = offsetoflow32(CPUARMState,
1317 cp15.c14_timer[GTIMER_PHYS].ctl),
1318 .resetfn = arm_cp_reset_ignore,
1319 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1320 },
1321 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1322 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
55d284af 1323 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1324 .accessfn = gt_ptimer_access,
55d284af
PM
1325 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1326 .resetvalue = 0,
00108f2d 1327 .writefn = gt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1328 },
1329 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
7a0e58fa 1330 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1331 .accessfn = gt_vtimer_access,
1332 .fieldoffset = offsetoflow32(CPUARMState,
1333 cp15.c14_timer[GTIMER_VIRT].ctl),
1334 .resetfn = arm_cp_reset_ignore,
1335 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1336 },
1337 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1338 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
55d284af 1339 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7 1340 .accessfn = gt_vtimer_access,
55d284af
PM
1341 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1342 .resetvalue = 0,
00108f2d 1343 .writefn = gt_ctl_write, .raw_writefn = raw_write,
55d284af
PM
1344 },
1345 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1346 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
7a0e58fa 1347 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1348 .accessfn = gt_ptimer_access,
55d284af
PM
1349 .readfn = gt_tval_read, .writefn = gt_tval_write,
1350 },
a7adc4b7
PM
1351 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1352 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
7a0e58fa 1353 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1354 .readfn = gt_tval_read, .writefn = gt_tval_write,
1355 },
55d284af 1356 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
7a0e58fa 1357 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
00108f2d 1358 .accessfn = gt_vtimer_access,
55d284af
PM
1359 .readfn = gt_tval_read, .writefn = gt_tval_write,
1360 },
a7adc4b7
PM
1361 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1362 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
7a0e58fa 1363 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
a7adc4b7
PM
1364 .readfn = gt_tval_read, .writefn = gt_tval_write,
1365 },
55d284af
PM
1366 /* The counter itself */
1367 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 1368 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1369 .accessfn = gt_pct_access,
a7adc4b7
PM
1370 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1371 },
1372 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1373 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 1374 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
a7adc4b7 1375 .accessfn = gt_pct_access,
55d284af
PM
1376 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1377 },
1378 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 1379 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 1380 .accessfn = gt_vct_access,
a7adc4b7
PM
1381 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1382 },
1383 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1384 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 1385 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
a7adc4b7 1386 .accessfn = gt_vct_access,
55d284af
PM
1387 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1388 },
1389 /* Comparison value, indicating when the timer goes off */
1390 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1391 .access = PL1_RW | PL0_R,
7a0e58fa 1392 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1393 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
a7adc4b7
PM
1394 .accessfn = gt_ptimer_access, .resetfn = arm_cp_reset_ignore,
1395 .writefn = gt_cval_write, .raw_writefn = raw_write,
1396 },
1397 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1398 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1399 .access = PL1_RW | PL0_R,
1400 .type = ARM_CP_IO,
1401 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1402 .resetvalue = 0, .accessfn = gt_vtimer_access,
00108f2d 1403 .writefn = gt_cval_write, .raw_writefn = raw_write,
55d284af
PM
1404 },
1405 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1406 .access = PL1_RW | PL0_R,
7a0e58fa 1407 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 1408 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
a7adc4b7
PM
1409 .accessfn = gt_vtimer_access, .resetfn = arm_cp_reset_ignore,
1410 .writefn = gt_cval_write, .raw_writefn = raw_write,
1411 },
1412 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1413 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1414 .access = PL1_RW | PL0_R,
1415 .type = ARM_CP_IO,
1416 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1417 .resetvalue = 0, .accessfn = gt_vtimer_access,
00108f2d 1418 .writefn = gt_cval_write, .raw_writefn = raw_write,
55d284af
PM
1419 },
1420 REGINFO_SENTINEL
1421};
1422
1423#else
1424/* In user-mode none of the generic timer registers are accessible,
bc72ad67 1425 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
55d284af
PM
1426 * so instead just don't register any of them.
1427 */
6cc7a3ae 1428static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
6cc7a3ae
PM
1429 REGINFO_SENTINEL
1430};
1431
55d284af
PM
1432#endif
1433
c4241c7d 1434static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 1435{
891a2fe7 1436 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 1437 raw_write(env, ri, value);
891a2fe7 1438 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 1439 raw_write(env, ri, value & 0xfffff6ff);
4a501606 1440 } else {
8d5c773e 1441 raw_write(env, ri, value & 0xfffff1ff);
4a501606 1442 }
4a501606
PM
1443}
1444
1445#ifndef CONFIG_USER_ONLY
1446/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 1447
92611c00
PM
1448static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
1449{
1450 if (ri->opc2 & 4) {
1451 /* Other states are only available with TrustZone; in
1452 * a non-TZ implementation these registers don't exist
1453 * at all, which is an Uncategorized trap. This underdecoding
7a0e58fa 1454 * is safe because the reginfo is NO_RAW.
92611c00
PM
1455 */
1456 return CP_ACCESS_TRAP_UNCATEGORIZED;
1457 }
1458 return CP_ACCESS_OK;
1459}
1460
c4241c7d 1461static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 1462{
a8170e5e 1463 hwaddr phys_addr;
4a501606
PM
1464 target_ulong page_size;
1465 int prot;
1466 int ret, is_user = ri->opc2 & 2;
1467 int access_type = ri->opc2 & 1;
01c097f7 1468 uint64_t par64;
4a501606 1469
4a501606
PM
1470 ret = get_phys_addr(env, value, access_type, is_user,
1471 &phys_addr, &prot, &page_size);
702a9357
PM
1472 if (extended_addresses_enabled(env)) {
1473 /* ret is a DFSR/IFSR value for the long descriptor
1474 * translation table format, but with WnR always clear.
1475 * Convert it to a 64-bit PAR.
1476 */
01c097f7 1477 par64 = (1 << 11); /* LPAE bit always set */
702a9357
PM
1478 if (ret == 0) {
1479 par64 |= phys_addr & ~0xfffULL;
1480 /* We don't set the ATTR or SH fields in the PAR. */
4a501606 1481 } else {
702a9357
PM
1482 par64 |= 1; /* F */
1483 par64 |= (ret & 0x3f) << 1; /* FS */
1484 /* Note that S2WLK and FSTAGE are always zero, because we don't
1485 * implement virtualization and therefore there can't be a stage 2
1486 * fault.
1487 */
4a501606
PM
1488 }
1489 } else {
702a9357
PM
1490 /* ret is a DFSR/IFSR value for the short descriptor
1491 * translation table format (with WnR always clear).
1492 * Convert it to a 32-bit PAR.
1493 */
1494 if (ret == 0) {
1495 /* We do not set any attribute bits in the PAR */
1496 if (page_size == (1 << 24)
1497 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 1498 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 1499 } else {
01c097f7 1500 par64 = phys_addr & 0xfffff000;
702a9357
PM
1501 }
1502 } else {
01c097f7
FA
1503 par64 = ((ret & (1 << 10)) >> 5) | ((ret & (1 << 12)) >> 6) |
1504 ((ret & 0xf) << 1) | 1;
702a9357 1505 }
4a501606 1506 }
01c097f7
FA
1507
1508 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4a501606
PM
1509}
1510#endif
1511
1512static const ARMCPRegInfo vapa_cp_reginfo[] = {
1513 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1514 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
1515 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
1516 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
1517 .writefn = par_write },
1518#ifndef CONFIG_USER_ONLY
1519 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 1520 .access = PL1_W, .accessfn = ats_access,
7a0e58fa 1521 .writefn = ats_write, .type = ARM_CP_NO_RAW },
4a501606
PM
1522#endif
1523 REGINFO_SENTINEL
1524};
1525
18032bec
PM
1526/* Return basic MPU access permission bits. */
1527static uint32_t simple_mpu_ap_bits(uint32_t val)
1528{
1529 uint32_t ret;
1530 uint32_t mask;
1531 int i;
1532 ret = 0;
1533 mask = 3;
1534 for (i = 0; i < 16; i += 2) {
1535 ret |= (val >> i) & mask;
1536 mask <<= 2;
1537 }
1538 return ret;
1539}
1540
1541/* Pad basic MPU access permission bits to extended format. */
1542static uint32_t extended_mpu_ap_bits(uint32_t val)
1543{
1544 uint32_t ret;
1545 uint32_t mask;
1546 int i;
1547 ret = 0;
1548 mask = 3;
1549 for (i = 0; i < 16; i += 2) {
1550 ret |= (val & mask) << i;
1551 mask <<= 2;
1552 }
1553 return ret;
1554}
1555
c4241c7d
PM
1556static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1557 uint64_t value)
18032bec 1558{
7e09797c 1559 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
1560}
1561
c4241c7d 1562static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 1563{
7e09797c 1564 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
1565}
1566
c4241c7d
PM
1567static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1568 uint64_t value)
18032bec 1569{
7e09797c 1570 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
1571}
1572
c4241c7d 1573static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 1574{
7e09797c 1575 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
1576}
1577
1578static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1579 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1580 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c
PM
1581 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1582 .resetvalue = 0,
18032bec
PM
1583 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1584 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 1585 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c
PM
1586 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1587 .resetvalue = 0,
18032bec
PM
1588 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1589 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1590 .access = PL1_RW,
7e09797c
PM
1591 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1592 .resetvalue = 0, },
18032bec
PM
1593 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1594 .access = PL1_RW,
7e09797c
PM
1595 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1596 .resetvalue = 0, },
ecce5c3c
PM
1597 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1598 .access = PL1_RW,
1599 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1600 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1601 .access = PL1_RW,
1602 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 1603 /* Protection region base and size registers */
e508a92b
PM
1604 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
1605 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1606 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
1607 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
1608 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1609 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
1610 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
1611 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1612 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
1613 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
1614 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1615 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
1616 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
1617 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1618 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
1619 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
1620 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1621 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
1622 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
1623 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1624 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
1625 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
1626 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1627 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
1628 REGINFO_SENTINEL
1629};
1630
c4241c7d
PM
1631static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1632 uint64_t value)
ecce5c3c 1633{
11f136ee 1634 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
1635 int maskshift = extract32(value, 0, 3);
1636
e389be16
FA
1637 if (!arm_feature(env, ARM_FEATURE_V8)) {
1638 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
1639 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
1640 * using Long-desciptor translation table format */
1641 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1642 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
1643 /* In an implementation that includes the Security Extensions
1644 * TTBCR has additional fields PD0 [4] and PD1 [5] for
1645 * Short-descriptor translation table format.
1646 */
1647 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
1648 } else {
1649 value &= TTBCR_N;
1650 }
e42c4db3 1651 }
e389be16 1652
11f136ee
FA
1653 /* Update the masks corresponding to the the TCR bank being written
1654 * Note that we always calculate mask and base_mask, but
e42c4db3 1655 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
1656 * for long-descriptor tables the TCR fields are used differently
1657 * and the mask and base_mask values are meaningless.
e42c4db3 1658 */
11f136ee
FA
1659 tcr->raw_tcr = value;
1660 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1661 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
1662}
1663
c4241c7d
PM
1664static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1665 uint64_t value)
d4e6df63 1666{
00c8cb0a
AF
1667 ARMCPU *cpu = arm_env_get_cpu(env);
1668
d4e6df63
PM
1669 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1670 /* With LPAE the TTBCR could result in a change of ASID
1671 * via the TTBCR.A1 bit, so do a TLB flush.
1672 */
00c8cb0a 1673 tlb_flush(CPU(cpu), 1);
d4e6df63 1674 }
c4241c7d 1675 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
1676}
1677
ecce5c3c
PM
1678static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1679{
11f136ee
FA
1680 TCR *tcr = raw_ptr(env, ri);
1681
1682 /* Reset both the TCR as well as the masks corresponding to the bank of
1683 * the TCR being reset.
1684 */
1685 tcr->raw_tcr = 0;
1686 tcr->mask = 0;
1687 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
1688}
1689
cb2e37df
PM
1690static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1691 uint64_t value)
1692{
00c8cb0a 1693 ARMCPU *cpu = arm_env_get_cpu(env);
11f136ee 1694 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 1695
cb2e37df 1696 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
00c8cb0a 1697 tlb_flush(CPU(cpu), 1);
11f136ee 1698 tcr->raw_tcr = value;
cb2e37df
PM
1699}
1700
327ed10f
PM
1701static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1702 uint64_t value)
1703{
1704 /* 64 bit accesses to the TTBRs can change the ASID and so we
1705 * must flush the TLB.
1706 */
1707 if (cpreg_field_is_64bit(ri)) {
00c8cb0a
AF
1708 ARMCPU *cpu = arm_env_get_cpu(env);
1709
1710 tlb_flush(CPU(cpu), 1);
327ed10f
PM
1711 }
1712 raw_write(env, ri, value);
1713}
1714
18032bec
PM
1715static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1716 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 1717 .access = PL1_RW, .type = ARM_CP_ALIAS,
4a7e2d73
FA
1718 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
1719 offsetoflow32(CPUARMState, cp15.dfsr_ns) },
6cd8a264 1720 .resetfn = arm_cp_reset_ignore, },
18032bec 1721 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
88ca1c2d
FA
1722 .access = PL1_RW, .resetvalue = 0,
1723 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
1724 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
6cd8a264
RH
1725 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
1726 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
1727 .access = PL1_RW,
d81c519c 1728 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 1729 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
1730 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
1731 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
1732 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
1733 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 1734 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af
FA
1735 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
1736 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
1737 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
1738 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
1739 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
1740 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1741 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
1742 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 1743 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 1744 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
7a0e58fa 1745 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
cb2e37df 1746 .resetfn = arm_cp_reset_ignore, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee
FA
1747 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
1748 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
b848ce2b 1749 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
6cd8a264 1750 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2f0180c5 1751 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
06d76f31 1752 .resetvalue = 0, },
b848ce2b
FA
1753 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
1754 .access = PL1_RW, .resetvalue = 0,
1755 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
1756 offsetof(CPUARMState, cp15.dfar_ns) } },
18032bec
PM
1757 REGINFO_SENTINEL
1758};
1759
c4241c7d
PM
1760static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1761 uint64_t value)
1047b9d7
PM
1762{
1763 env->cp15.c15_ticonfig = value & 0xe7;
1764 /* The OS_TYPE bit in this register changes the reported CPUID! */
1765 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1766 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
1767}
1768
c4241c7d
PM
1769static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1770 uint64_t value)
1047b9d7
PM
1771{
1772 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
1773}
1774
c4241c7d
PM
1775static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1776 uint64_t value)
1047b9d7
PM
1777{
1778 /* Wait-for-interrupt (deprecated) */
c3affe56 1779 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1047b9d7
PM
1780}
1781
c4241c7d
PM
1782static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1783 uint64_t value)
c4804214
PM
1784{
1785 /* On OMAP there are registers indicating the max/min index of dcache lines
1786 * containing a dirty line; cache flush operations have to reset these.
1787 */
1788 env->cp15.c15_i_max = 0x000;
1789 env->cp15.c15_i_min = 0xff0;
c4804214
PM
1790}
1791
18032bec
PM
1792static const ARMCPRegInfo omap_cp_reginfo[] = {
1793 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1794 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 1795 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 1796 .resetvalue = 0, },
1047b9d7
PM
1797 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1798 .access = PL1_RW, .type = ARM_CP_NOP },
1799 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1800 .access = PL1_RW,
1801 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1802 .writefn = omap_ticonfig_write },
1803 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1804 .access = PL1_RW,
1805 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1806 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1807 .access = PL1_RW, .resetvalue = 0xff0,
1808 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1809 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1810 .access = PL1_RW,
1811 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1812 .writefn = omap_threadid_write },
1813 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1814 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 1815 .type = ARM_CP_NO_RAW,
1047b9d7
PM
1816 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1817 /* TODO: Peripheral port remap register:
1818 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1819 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1820 * when MMU is off.
1821 */
c4804214 1822 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 1823 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 1824 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 1825 .writefn = omap_cachemaint_write },
34f90529
PM
1826 { .name = "C9", .cp = 15, .crn = 9,
1827 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1828 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
1829 REGINFO_SENTINEL
1830};
1831
c4241c7d
PM
1832static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1833 uint64_t value)
1047b9d7 1834{
c0f4af17 1835 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
1836}
1837
1838static const ARMCPRegInfo xscale_cp_reginfo[] = {
1839 { .name = "XSCALE_CPAR",
1840 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1841 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1842 .writefn = xscale_cpar_write, },
2771db27
PM
1843 { .name = "XSCALE_AUXCR",
1844 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1845 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1846 .resetvalue = 0, },
3b771579
PM
1847 /* XScale specific cache-lockdown: since we have no cache we NOP these
1848 * and hope the guest does not really rely on cache behaviour.
1849 */
1850 { .name = "XSCALE_LOCK_ICACHE_LINE",
1851 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
1852 .access = PL1_W, .type = ARM_CP_NOP },
1853 { .name = "XSCALE_UNLOCK_ICACHE",
1854 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
1855 .access = PL1_W, .type = ARM_CP_NOP },
1856 { .name = "XSCALE_DCACHE_LOCK",
1857 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
1858 .access = PL1_RW, .type = ARM_CP_NOP },
1859 { .name = "XSCALE_UNLOCK_DCACHE",
1860 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
1861 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
1862 REGINFO_SENTINEL
1863};
1864
1865static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1866 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1867 * implementation of this implementation-defined space.
1868 * Ideally this should eventually disappear in favour of actually
1869 * implementing the correct behaviour for all cores.
1870 */
1871 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1872 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 1873 .access = PL1_RW,
7a0e58fa 1874 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 1875 .resetvalue = 0 },
18032bec
PM
1876 REGINFO_SENTINEL
1877};
1878
c4804214
PM
1879static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1880 /* Cache status: RAZ because we have no cache so it's always clean */
1881 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 1882 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 1883 .resetvalue = 0 },
c4804214
PM
1884 REGINFO_SENTINEL
1885};
1886
1887static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1888 /* We never have a a block transfer operation in progress */
1889 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 1890 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 1891 .resetvalue = 0 },
30b05bba
PM
1892 /* The cache ops themselves: these all NOP for QEMU */
1893 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1894 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1895 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1896 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1897 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1898 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1899 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1900 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1901 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1902 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1903 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1904 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
1905 REGINFO_SENTINEL
1906};
1907
1908static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1909 /* The cache test-and-clean instructions always return (1 << 30)
1910 * to indicate that there are no dirty cache lines.
1911 */
1912 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 1913 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 1914 .resetvalue = (1 << 30) },
c4804214 1915 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 1916 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 1917 .resetvalue = (1 << 30) },
c4804214
PM
1918 REGINFO_SENTINEL
1919};
1920
34f90529
PM
1921static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1922 /* Ignore ReadBuffer accesses */
1923 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1924 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 1925 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 1926 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
1927 REGINFO_SENTINEL
1928};
1929
c4241c7d 1930static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
81bdde9d 1931{
55e5c285
AF
1932 CPUState *cs = CPU(arm_env_get_cpu(env));
1933 uint32_t mpidr = cs->cpu_index;
4b7fff2f
PM
1934 /* We don't support setting cluster ID ([8..11]) (known as Aff1
1935 * in later ARM ARM versions), or any of the higher affinity level fields,
81bdde9d
PM
1936 * so these bits always RAZ.
1937 */
1938 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 1939 mpidr |= (1U << 31);
81bdde9d
PM
1940 /* Cores which are uniprocessor (non-coherent)
1941 * but still implement the MP extensions set
1942 * bit 30. (For instance, A9UP.) However we do
1943 * not currently model any of those cores.
1944 */
1945 }
c4241c7d 1946 return mpidr;
81bdde9d
PM
1947}
1948
1949static const ARMCPRegInfo mpidr_cp_reginfo[] = {
4b7fff2f
PM
1950 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
1951 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7a0e58fa 1952 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
81bdde9d
PM
1953 REGINFO_SENTINEL
1954};
1955
7ac681cf 1956static const ARMCPRegInfo lpae_cp_reginfo[] = {
b90372ad 1957 /* NOP AMAIR0/1: the override is because these clash with the rather
7ac681cf
PM
1958 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1959 */
b0fe2427
PM
1960 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
1961 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
7ac681cf
PM
1962 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1963 .resetvalue = 0 },
b0fe2427 1964 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf
PM
1965 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1966 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1967 .resetvalue = 0 },
891a2fe7 1968 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
1969 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
1970 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
1971 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 1972 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
7a0e58fa 1973 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
1974 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
1975 offsetof(CPUARMState, cp15.ttbr0_ns) },
327ed10f 1976 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
891a2fe7 1977 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
7a0e58fa 1978 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
1979 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
1980 offsetof(CPUARMState, cp15.ttbr1_ns) },
327ed10f 1981 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
7ac681cf
PM
1982 REGINFO_SENTINEL
1983};
1984
c4241c7d 1985static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 1986{
c4241c7d 1987 return vfp_get_fpcr(env);
b0d2b7d0
PM
1988}
1989
c4241c7d
PM
1990static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1991 uint64_t value)
b0d2b7d0
PM
1992{
1993 vfp_set_fpcr(env, value);
b0d2b7d0
PM
1994}
1995
c4241c7d 1996static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 1997{
c4241c7d 1998 return vfp_get_fpsr(env);
b0d2b7d0
PM
1999}
2000
c4241c7d
PM
2001static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2002 uint64_t value)
b0d2b7d0
PM
2003{
2004 vfp_set_fpsr(env, value);
b0d2b7d0
PM
2005}
2006
c2b820fe
PM
2007static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
2008{
137feaa9 2009 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
c2b820fe
PM
2010 return CP_ACCESS_TRAP;
2011 }
2012 return CP_ACCESS_OK;
2013}
2014
2015static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2016 uint64_t value)
2017{
2018 env->daif = value & PSTATE_DAIF;
2019}
2020
8af35c37
PM
2021static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2022 const ARMCPRegInfo *ri)
2023{
2024 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2025 * SCTLR_EL1.UCI is set.
2026 */
137feaa9 2027 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
8af35c37
PM
2028 return CP_ACCESS_TRAP;
2029 }
2030 return CP_ACCESS_OK;
2031}
2032
dbb1fb27
AB
2033/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2034 * Page D4-1736 (DDI0487A.b)
2035 */
2036
168aa23b
PM
2037static void tlbi_aa64_va_write(CPUARMState *env, const ARMCPRegInfo *ri,
2038 uint64_t value)
2039{
2040 /* Invalidate by VA (AArch64 version) */
31b030d4 2041 ARMCPU *cpu = arm_env_get_cpu(env);
dbb1fb27
AB
2042 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2043
31b030d4 2044 tlb_flush_page(CPU(cpu), pageaddr);
168aa23b
PM
2045}
2046
2047static void tlbi_aa64_vaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
2048 uint64_t value)
2049{
2050 /* Invalidate by VA, all ASIDs (AArch64 version) */
31b030d4 2051 ARMCPU *cpu = arm_env_get_cpu(env);
dbb1fb27
AB
2052 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2053
31b030d4 2054 tlb_flush_page(CPU(cpu), pageaddr);
168aa23b
PM
2055}
2056
2057static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2058 uint64_t value)
2059{
2060 /* Invalidate by ASID (AArch64 version) */
00c8cb0a 2061 ARMCPU *cpu = arm_env_get_cpu(env);
168aa23b 2062 int asid = extract64(value, 48, 16);
00c8cb0a 2063 tlb_flush(CPU(cpu), asid == 0);
168aa23b
PM
2064}
2065
fa439fc5
PM
2066static void tlbi_aa64_va_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2067 uint64_t value)
2068{
2069 CPUState *other_cs;
2070 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2071
2072 CPU_FOREACH(other_cs) {
2073 tlb_flush_page(other_cs, pageaddr);
2074 }
2075}
2076
2077static void tlbi_aa64_vaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2078 uint64_t value)
2079{
2080 CPUState *other_cs;
2081 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2082
2083 CPU_FOREACH(other_cs) {
2084 tlb_flush_page(other_cs, pageaddr);
2085 }
2086}
2087
2088static void tlbi_aa64_asid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2089 uint64_t value)
2090{
2091 CPUState *other_cs;
2092 int asid = extract64(value, 48, 16);
2093
2094 CPU_FOREACH(other_cs) {
2095 tlb_flush(other_cs, asid == 0);
2096 }
2097}
2098
aca3f40b
PM
2099static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
2100{
2101 /* We don't implement EL2, so the only control on DC ZVA is the
2102 * bit in the SCTLR which can prohibit access for EL0.
2103 */
137feaa9 2104 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
aca3f40b
PM
2105 return CP_ACCESS_TRAP;
2106 }
2107 return CP_ACCESS_OK;
2108}
2109
2110static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
2111{
2112 ARMCPU *cpu = arm_env_get_cpu(env);
2113 int dzp_bit = 1 << 4;
2114
2115 /* DZP indicates whether DC ZVA access is allowed */
14e5f106 2116 if (aa64_zva_access(env, NULL) == CP_ACCESS_OK) {
aca3f40b
PM
2117 dzp_bit = 0;
2118 }
2119 return cpu->dcz_blocksize | dzp_bit;
2120}
2121
f502cfc2
PM
2122static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2123{
cdcf1405 2124 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
2125 /* Access to SP_EL0 is undefined if it's being used as
2126 * the stack pointer.
2127 */
2128 return CP_ACCESS_TRAP_UNCATEGORIZED;
2129 }
2130 return CP_ACCESS_OK;
2131}
2132
2133static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
2134{
2135 return env->pstate & PSTATE_SP;
2136}
2137
2138static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
2139{
2140 update_spsel(env, val);
2141}
2142
137feaa9
FA
2143static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2144 uint64_t value)
2145{
2146 ARMCPU *cpu = arm_env_get_cpu(env);
2147
2148 if (raw_read(env, ri) == value) {
2149 /* Skip the TLB flush if nothing actually changed; Linux likes
2150 * to do a lot of pointless SCTLR writes.
2151 */
2152 return;
2153 }
2154
2155 raw_write(env, ri, value);
2156 /* ??? Lots of these bits are not implemented. */
2157 /* This may enable/disable the MMU, so do a TLB flush. */
2158 tlb_flush(CPU(cpu), 1);
2159}
2160
b0d2b7d0
PM
2161static const ARMCPRegInfo v8_cp_reginfo[] = {
2162 /* Minimal set of EL0-visible registers. This will need to be expanded
2163 * significantly for system emulation of AArch64 CPUs.
2164 */
2165 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
2166 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
2167 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
2168 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
2169 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 2170 .type = ARM_CP_NO_RAW,
c2b820fe
PM
2171 .access = PL0_RW, .accessfn = aa64_daif_access,
2172 .fieldoffset = offsetof(CPUARMState, daif),
2173 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
2174 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
2175 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
2176 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
2177 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
2178 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
2179 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
2180 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
2181 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 2182 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
2183 .readfn = aa64_dczid_read },
2184 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
2185 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
2186 .access = PL0_W, .type = ARM_CP_DC_ZVA,
2187#ifndef CONFIG_USER_ONLY
2188 /* Avoid overhead of an access check that always passes in user-mode */
2189 .accessfn = aa64_zva_access,
2190#endif
2191 },
0eef9d98
PM
2192 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
2193 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
2194 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
2195 /* Cache ops: all NOPs since we don't emulate caches */
2196 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
2197 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2198 .access = PL1_W, .type = ARM_CP_NOP },
2199 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
2200 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2201 .access = PL1_W, .type = ARM_CP_NOP },
2202 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
2203 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
2204 .access = PL0_W, .type = ARM_CP_NOP,
2205 .accessfn = aa64_cacheop_access },
2206 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
2207 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2208 .access = PL1_W, .type = ARM_CP_NOP },
2209 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
2210 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2211 .access = PL1_W, .type = ARM_CP_NOP },
2212 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
2213 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
2214 .access = PL0_W, .type = ARM_CP_NOP,
2215 .accessfn = aa64_cacheop_access },
2216 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
2217 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2218 .access = PL1_W, .type = ARM_CP_NOP },
2219 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
2220 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
2221 .access = PL0_W, .type = ARM_CP_NOP,
2222 .accessfn = aa64_cacheop_access },
2223 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
2224 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
2225 .access = PL0_W, .type = ARM_CP_NOP,
2226 .accessfn = aa64_cacheop_access },
2227 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
2228 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2229 .access = PL1_W, .type = ARM_CP_NOP },
168aa23b
PM
2230 /* TLBI operations */
2231 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2232 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
7a0e58fa 2233 .access = PL1_W, .type = ARM_CP_NO_RAW,
fa439fc5 2234 .writefn = tlbiall_is_write },
168aa23b 2235 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2236 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
7a0e58fa 2237 .access = PL1_W, .type = ARM_CP_NO_RAW,
fa439fc5 2238 .writefn = tlbi_aa64_va_is_write },
168aa23b 2239 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2240 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
7a0e58fa 2241 .access = PL1_W, .type = ARM_CP_NO_RAW,
fa439fc5 2242 .writefn = tlbi_aa64_asid_is_write },
168aa23b 2243 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2244 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
7a0e58fa 2245 .access = PL1_W, .type = ARM_CP_NO_RAW,
fa439fc5 2246 .writefn = tlbi_aa64_vaa_is_write },
168aa23b 2247 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2248 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 2249 .access = PL1_W, .type = ARM_CP_NO_RAW,
fa439fc5 2250 .writefn = tlbi_aa64_va_is_write },
168aa23b 2251 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 2252 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 2253 .access = PL1_W, .type = ARM_CP_NO_RAW,
fa439fc5 2254 .writefn = tlbi_aa64_vaa_is_write },
168aa23b 2255 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2256 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
7a0e58fa 2257 .access = PL1_W, .type = ARM_CP_NO_RAW,
168aa23b
PM
2258 .writefn = tlbiall_write },
2259 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2260 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
7a0e58fa 2261 .access = PL1_W, .type = ARM_CP_NO_RAW,
168aa23b
PM
2262 .writefn = tlbi_aa64_va_write },
2263 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2264 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
7a0e58fa 2265 .access = PL1_W, .type = ARM_CP_NO_RAW,
168aa23b
PM
2266 .writefn = tlbi_aa64_asid_write },
2267 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2268 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
7a0e58fa 2269 .access = PL1_W, .type = ARM_CP_NO_RAW,
168aa23b
PM
2270 .writefn = tlbi_aa64_vaa_write },
2271 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2272 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 2273 .access = PL1_W, .type = ARM_CP_NO_RAW,
168aa23b
PM
2274 .writefn = tlbi_aa64_va_write },
2275 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 2276 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 2277 .access = PL1_W, .type = ARM_CP_NO_RAW,
168aa23b 2278 .writefn = tlbi_aa64_vaa_write },
19525524
PM
2279#ifndef CONFIG_USER_ONLY
2280 /* 64 bit address translation operations */
2281 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
2282 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
7a0e58fa 2283 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write },
19525524
PM
2284 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
2285 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
7a0e58fa 2286 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write },
19525524
PM
2287 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
2288 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
7a0e58fa 2289 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write },
19525524
PM
2290 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
2291 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
7a0e58fa 2292 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write },
19525524 2293#endif
995939a6 2294 /* TLB invalidate last level of translation table walk */
9449fdf6 2295 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
7a0e58fa 2296 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
9449fdf6 2297 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
7a0e58fa 2298 .type = ARM_CP_NO_RAW, .access = PL1_W,
fa439fc5 2299 .writefn = tlbimvaa_is_write },
9449fdf6 2300 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
7a0e58fa 2301 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
9449fdf6 2302 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
7a0e58fa 2303 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
9449fdf6
PM
2304 /* 32 bit cache operations */
2305 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2306 .type = ARM_CP_NOP, .access = PL1_W },
2307 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
2308 .type = ARM_CP_NOP, .access = PL1_W },
2309 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2310 .type = ARM_CP_NOP, .access = PL1_W },
2311 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2312 .type = ARM_CP_NOP, .access = PL1_W },
2313 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
2314 .type = ARM_CP_NOP, .access = PL1_W },
2315 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
2316 .type = ARM_CP_NOP, .access = PL1_W },
2317 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2318 .type = ARM_CP_NOP, .access = PL1_W },
2319 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2320 .type = ARM_CP_NOP, .access = PL1_W },
2321 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
2322 .type = ARM_CP_NOP, .access = PL1_W },
2323 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2324 .type = ARM_CP_NOP, .access = PL1_W },
2325 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2326 .type = ARM_CP_NOP, .access = PL1_W },
2327 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
2328 .type = ARM_CP_NOP, .access = PL1_W },
2329 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2330 .type = ARM_CP_NOP, .access = PL1_W },
2331 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
2332 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
2333 .access = PL1_RW, .resetvalue = 0,
2334 .writefn = dacr_write, .raw_writefn = raw_write,
2335 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
2336 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 2337 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 2338 .type = ARM_CP_ALIAS,
a0618a19 2339 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
2340 .access = PL1_RW,
2341 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 2342 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 2343 .type = ARM_CP_ALIAS,
a65f1de9
PM
2344 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
2345 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[0]) },
f502cfc2
PM
2346 /* We rely on the access checks not allowing the guest to write to the
2347 * state field when SPSel indicates that it's being used as the stack
2348 * pointer.
2349 */
2350 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
2351 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
2352 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 2353 .type = ARM_CP_ALIAS,
f502cfc2 2354 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
2355 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
2356 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 2357 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 2358 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
2359 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
2360 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 2361 .type = ARM_CP_NO_RAW,
f502cfc2 2362 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
b0d2b7d0
PM
2363 REGINFO_SENTINEL
2364};
2365
d42e3c26
EI
2366/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
2367static const ARMCPRegInfo v8_el3_no_el2_cp_reginfo[] = {
2368 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2369 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2370 .access = PL2_RW,
2371 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
f149e3e8 2372 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 2373 .type = ARM_CP_NO_RAW,
f149e3e8
EI
2374 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
2375 .access = PL2_RW,
2376 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
d42e3c26
EI
2377 REGINFO_SENTINEL
2378};
2379
f149e3e8
EI
2380static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2381{
2382 ARMCPU *cpu = arm_env_get_cpu(env);
2383 uint64_t valid_mask = HCR_MASK;
2384
2385 if (arm_feature(env, ARM_FEATURE_EL3)) {
2386 valid_mask &= ~HCR_HCD;
2387 } else {
2388 valid_mask &= ~HCR_TSC;
2389 }
2390
2391 /* Clear RES0 bits. */
2392 value &= valid_mask;
2393
2394 /* These bits change the MMU setup:
2395 * HCR_VM enables stage 2 translation
2396 * HCR_PTW forbids certain page-table setups
2397 * HCR_DC Disables stage1 and enables stage2 translation
2398 */
2399 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
2400 tlb_flush(CPU(cpu), 1);
2401 }
2402 raw_write(env, ri, value);
2403}
2404
3b685ba7 2405static const ARMCPRegInfo v8_el2_cp_reginfo[] = {
f149e3e8
EI
2406 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
2407 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
2408 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
2409 .writefn = hcr_write },
0c17d68c
FA
2410 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
2411 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
2412 .access = PL2_RW, .resetvalue = 0,
2413 .writefn = dacr_write, .raw_writefn = raw_write,
2414 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3b685ba7 2415 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 2416 .type = ARM_CP_ALIAS,
3b685ba7
EI
2417 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
2418 .access = PL2_RW,
2419 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
f2c30f42 2420 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 2421 .type = ARM_CP_ALIAS,
f2c30f42
EI
2422 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
2423 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
88ca1c2d
FA
2424 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
2425 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
2426 .access = PL2_RW, .resetvalue = 0,
2427 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
63b60551
EI
2428 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
2429 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
2430 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3b685ba7 2431 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 2432 .type = ARM_CP_ALIAS,
3b685ba7
EI
2433 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
2434 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
d42e3c26
EI
2435 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2436 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2437 .access = PL2_RW, .writefn = vbar_write,
2438 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
2439 .resetvalue = 0 },
884b4dee
GB
2440 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
2441 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 2442 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 2443 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3b685ba7
EI
2444 REGINFO_SENTINEL
2445};
2446
60fb1a87
GB
2447static const ARMCPRegInfo el3_cp_reginfo[] = {
2448 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
2449 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
2450 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
2451 .resetvalue = 0, .writefn = scr_write },
7a0e58fa 2452 { .name = "SCR", .type = ARM_CP_ALIAS,
60fb1a87
GB
2453 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
2454 .access = PL3_RW, .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
2455 .resetfn = arm_cp_reset_ignore, .writefn = scr_write },
2456 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
2457 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
2458 .access = PL3_RW, .resetvalue = 0,
2459 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
2460 { .name = "SDER",
2461 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
2462 .access = PL3_RW, .resetvalue = 0,
2463 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
2464 /* TODO: Implement NSACR trapping of secure EL1 accesses to EL3 */
2465 { .name = "NSACR", .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
2466 .access = PL3_W | PL1_R, .resetvalue = 0,
2467 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) },
2468 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
2469 .access = PL3_RW, .writefn = vbar_write, .resetvalue = 0,
2470 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
137feaa9
FA
2471 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
2472 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
2473 .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
2474 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) },
7dd8c9af
FA
2475 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
2476 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
2477 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2478 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
2479 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
2480 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
2481 .access = PL3_RW, .writefn = vmsa_tcr_el1_write,
2482 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2483 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 2484 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 2485 .type = ARM_CP_ALIAS,
81547d66
EI
2486 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
2487 .access = PL3_RW,
2488 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 2489 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 2490 .type = ARM_CP_ALIAS,
f2c30f42
EI
2491 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
2492 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
2493 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
2494 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
2495 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 2496 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 2497 .type = ARM_CP_ALIAS,
81547d66
EI
2498 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
2499 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
a1ba125c
EI
2500 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
2501 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
2502 .access = PL3_RW, .writefn = vbar_write,
2503 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
2504 .resetvalue = 0 },
0f1a3b24
FA
2505 REGINFO_SENTINEL
2506};
2507
7da845b0
PM
2508static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2509{
2510 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
2511 * but the AArch32 CTR has its own reginfo struct)
2512 */
137feaa9 2513 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7da845b0
PM
2514 return CP_ACCESS_TRAP;
2515 }
2516 return CP_ACCESS_OK;
2517}
2518
50300698 2519static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 2520 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
2521 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
2522 * unlike DBGDRAR it is never accessible from EL0.
2523 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
2524 * accessor.
50300698
PM
2525 */
2526 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
2527 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
2528 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
2529 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
2530 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
50300698
PM
2531 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2532 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 2533 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
2534 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
2535 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
0e5e8935
PM
2536 .access = PL1_RW,
2537 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
2538 .resetvalue = 0 },
5e8b12ff
PM
2539 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
2540 * We don't implement the configurable EL0 access.
2541 */
2542 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
2543 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 2544 .type = ARM_CP_ALIAS,
5e8b12ff
PM
2545 .access = PL1_R,
2546 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
2547 .resetfn = arm_cp_reset_ignore },
50300698 2548 /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
10aae104
PM
2549 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
2550 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
50300698 2551 .access = PL1_W, .type = ARM_CP_NOP },
5e8b12ff
PM
2552 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
2553 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
2554 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
2555 .access = PL1_RW, .type = ARM_CP_NOP },
2556 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
2557 * implement vector catch debug events yet.
2558 */
2559 { .name = "DBGVCR",
2560 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
2561 .access = PL1_RW, .type = ARM_CP_NOP },
50300698
PM
2562 REGINFO_SENTINEL
2563};
2564
2565static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
2566 /* 64 bit access versions of the (dummy) debug registers */
2567 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
2568 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2569 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
2570 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2571 REGINFO_SENTINEL
2572};
2573
9ee98ce8
PM
2574void hw_watchpoint_update(ARMCPU *cpu, int n)
2575{
2576 CPUARMState *env = &cpu->env;
2577 vaddr len = 0;
2578 vaddr wvr = env->cp15.dbgwvr[n];
2579 uint64_t wcr = env->cp15.dbgwcr[n];
2580 int mask;
2581 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
2582
2583 if (env->cpu_watchpoint[n]) {
2584 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
2585 env->cpu_watchpoint[n] = NULL;
2586 }
2587
2588 if (!extract64(wcr, 0, 1)) {
2589 /* E bit clear : watchpoint disabled */
2590 return;
2591 }
2592
2593 switch (extract64(wcr, 3, 2)) {
2594 case 0:
2595 /* LSC 00 is reserved and must behave as if the wp is disabled */
2596 return;
2597 case 1:
2598 flags |= BP_MEM_READ;
2599 break;
2600 case 2:
2601 flags |= BP_MEM_WRITE;
2602 break;
2603 case 3:
2604 flags |= BP_MEM_ACCESS;
2605 break;
2606 }
2607
2608 /* Attempts to use both MASK and BAS fields simultaneously are
2609 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
2610 * thus generating a watchpoint for every byte in the masked region.
2611 */
2612 mask = extract64(wcr, 24, 4);
2613 if (mask == 1 || mask == 2) {
2614 /* Reserved values of MASK; we must act as if the mask value was
2615 * some non-reserved value, or as if the watchpoint were disabled.
2616 * We choose the latter.
2617 */
2618 return;
2619 } else if (mask) {
2620 /* Watchpoint covers an aligned area up to 2GB in size */
2621 len = 1ULL << mask;
2622 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
2623 * whether the watchpoint fires when the unmasked bits match; we opt
2624 * to generate the exceptions.
2625 */
2626 wvr &= ~(len - 1);
2627 } else {
2628 /* Watchpoint covers bytes defined by the byte address select bits */
2629 int bas = extract64(wcr, 5, 8);
2630 int basstart;
2631
2632 if (bas == 0) {
2633 /* This must act as if the watchpoint is disabled */
2634 return;
2635 }
2636
2637 if (extract64(wvr, 2, 1)) {
2638 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
2639 * ignored, and BAS[3:0] define which bytes to watch.
2640 */
2641 bas &= 0xf;
2642 }
2643 /* The BAS bits are supposed to be programmed to indicate a contiguous
2644 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
2645 * we fire for each byte in the word/doubleword addressed by the WVR.
2646 * We choose to ignore any non-zero bits after the first range of 1s.
2647 */
2648 basstart = ctz32(bas);
2649 len = cto32(bas >> basstart);
2650 wvr += basstart;
2651 }
2652
2653 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
2654 &env->cpu_watchpoint[n]);
2655}
2656
2657void hw_watchpoint_update_all(ARMCPU *cpu)
2658{
2659 int i;
2660 CPUARMState *env = &cpu->env;
2661
2662 /* Completely clear out existing QEMU watchpoints and our array, to
2663 * avoid possible stale entries following migration load.
2664 */
2665 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
2666 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
2667
2668 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
2669 hw_watchpoint_update(cpu, i);
2670 }
2671}
2672
2673static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2674 uint64_t value)
2675{
2676 ARMCPU *cpu = arm_env_get_cpu(env);
2677 int i = ri->crm;
2678
2679 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
2680 * register reads and behaves as if values written are sign extended.
2681 * Bits [1:0] are RES0.
2682 */
2683 value = sextract64(value, 0, 49) & ~3ULL;
2684
2685 raw_write(env, ri, value);
2686 hw_watchpoint_update(cpu, i);
2687}
2688
2689static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2690 uint64_t value)
2691{
2692 ARMCPU *cpu = arm_env_get_cpu(env);
2693 int i = ri->crm;
2694
2695 raw_write(env, ri, value);
2696 hw_watchpoint_update(cpu, i);
2697}
2698
46747d15
PM
2699void hw_breakpoint_update(ARMCPU *cpu, int n)
2700{
2701 CPUARMState *env = &cpu->env;
2702 uint64_t bvr = env->cp15.dbgbvr[n];
2703 uint64_t bcr = env->cp15.dbgbcr[n];
2704 vaddr addr;
2705 int bt;
2706 int flags = BP_CPU;
2707
2708 if (env->cpu_breakpoint[n]) {
2709 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
2710 env->cpu_breakpoint[n] = NULL;
2711 }
2712
2713 if (!extract64(bcr, 0, 1)) {
2714 /* E bit clear : watchpoint disabled */
2715 return;
2716 }
2717
2718 bt = extract64(bcr, 20, 4);
2719
2720 switch (bt) {
2721 case 4: /* unlinked address mismatch (reserved if AArch64) */
2722 case 5: /* linked address mismatch (reserved if AArch64) */
2723 qemu_log_mask(LOG_UNIMP,
2724 "arm: address mismatch breakpoint types not implemented");
2725 return;
2726 case 0: /* unlinked address match */
2727 case 1: /* linked address match */
2728 {
2729 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
2730 * we behave as if the register was sign extended. Bits [1:0] are
2731 * RES0. The BAS field is used to allow setting breakpoints on 16
2732 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
2733 * a bp will fire if the addresses covered by the bp and the addresses
2734 * covered by the insn overlap but the insn doesn't start at the
2735 * start of the bp address range. We choose to require the insn and
2736 * the bp to have the same address. The constraints on writing to
2737 * BAS enforced in dbgbcr_write mean we have only four cases:
2738 * 0b0000 => no breakpoint
2739 * 0b0011 => breakpoint on addr
2740 * 0b1100 => breakpoint on addr + 2
2741 * 0b1111 => breakpoint on addr
2742 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
2743 */
2744 int bas = extract64(bcr, 5, 4);
2745 addr = sextract64(bvr, 0, 49) & ~3ULL;
2746 if (bas == 0) {
2747 return;
2748 }
2749 if (bas == 0xc) {
2750 addr += 2;
2751 }
2752 break;
2753 }
2754 case 2: /* unlinked context ID match */
2755 case 8: /* unlinked VMID match (reserved if no EL2) */
2756 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
2757 qemu_log_mask(LOG_UNIMP,
2758 "arm: unlinked context breakpoint types not implemented");
2759 return;
2760 case 9: /* linked VMID match (reserved if no EL2) */
2761 case 11: /* linked context ID and VMID match (reserved if no EL2) */
2762 case 3: /* linked context ID match */
2763 default:
2764 /* We must generate no events for Linked context matches (unless
2765 * they are linked to by some other bp/wp, which is handled in
2766 * updates for the linking bp/wp). We choose to also generate no events
2767 * for reserved values.
2768 */
2769 return;
2770 }
2771
2772 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
2773}
2774
2775void hw_breakpoint_update_all(ARMCPU *cpu)
2776{
2777 int i;
2778 CPUARMState *env = &cpu->env;
2779
2780 /* Completely clear out existing QEMU breakpoints and our array, to
2781 * avoid possible stale entries following migration load.
2782 */
2783 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
2784 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
2785
2786 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
2787 hw_breakpoint_update(cpu, i);
2788 }
2789}
2790
2791static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2792 uint64_t value)
2793{
2794 ARMCPU *cpu = arm_env_get_cpu(env);
2795 int i = ri->crm;
2796
2797 raw_write(env, ri, value);
2798 hw_breakpoint_update(cpu, i);
2799}
2800
2801static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2802 uint64_t value)
2803{
2804 ARMCPU *cpu = arm_env_get_cpu(env);
2805 int i = ri->crm;
2806
2807 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
2808 * copy of BAS[0].
2809 */
2810 value = deposit64(value, 6, 1, extract64(value, 5, 1));
2811 value = deposit64(value, 8, 1, extract64(value, 7, 1));
2812
2813 raw_write(env, ri, value);
2814 hw_breakpoint_update(cpu, i);
2815}
2816
50300698 2817static void define_debug_regs(ARMCPU *cpu)
0b45451e 2818{
50300698
PM
2819 /* Define v7 and v8 architectural debug registers.
2820 * These are just dummy implementations for now.
0b45451e
PM
2821 */
2822 int i;
3ff6fc91 2823 int wrps, brps, ctx_cmps;
48eb3ae6
PM
2824 ARMCPRegInfo dbgdidr = {
2825 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
2826 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
2827 };
2828
3ff6fc91 2829 /* Note that all these register fields hold "number of Xs minus 1". */
48eb3ae6
PM
2830 brps = extract32(cpu->dbgdidr, 24, 4);
2831 wrps = extract32(cpu->dbgdidr, 28, 4);
3ff6fc91
PM
2832 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
2833
2834 assert(ctx_cmps <= brps);
48eb3ae6
PM
2835
2836 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
2837 * of the debug registers such as number of breakpoints;
2838 * check that if they both exist then they agree.
2839 */
2840 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
2841 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
2842 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
3ff6fc91 2843 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
48eb3ae6 2844 }
0b45451e 2845
48eb3ae6 2846 define_one_arm_cp_reg(cpu, &dbgdidr);
50300698
PM
2847 define_arm_cp_regs(cpu, debug_cp_reginfo);
2848
2849 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
2850 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
2851 }
2852
48eb3ae6 2853 for (i = 0; i < brps + 1; i++) {
0b45451e 2854 ARMCPRegInfo dbgregs[] = {
10aae104
PM
2855 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
2856 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
0b45451e 2857 .access = PL1_RW,
46747d15
PM
2858 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
2859 .writefn = dbgbvr_write, .raw_writefn = raw_write
2860 },
10aae104
PM
2861 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
2862 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
0b45451e 2863 .access = PL1_RW,
46747d15
PM
2864 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
2865 .writefn = dbgbcr_write, .raw_writefn = raw_write
2866 },
48eb3ae6
PM
2867 REGINFO_SENTINEL
2868 };
2869 define_arm_cp_regs(cpu, dbgregs);
2870 }
2871
2872 for (i = 0; i < wrps + 1; i++) {
2873 ARMCPRegInfo dbgregs[] = {
10aae104
PM
2874 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
2875 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
0b45451e 2876 .access = PL1_RW,
9ee98ce8
PM
2877 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
2878 .writefn = dbgwvr_write, .raw_writefn = raw_write
2879 },
10aae104
PM
2880 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
2881 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
0b45451e 2882 .access = PL1_RW,
9ee98ce8
PM
2883 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
2884 .writefn = dbgwcr_write, .raw_writefn = raw_write
2885 },
2886 REGINFO_SENTINEL
0b45451e
PM
2887 };
2888 define_arm_cp_regs(cpu, dbgregs);
2889 }
2890}
2891
2ceb98c0
PM
2892void register_cp_regs_for_features(ARMCPU *cpu)
2893{
2894 /* Register all the coprocessor registers based on feature bits */
2895 CPUARMState *env = &cpu->env;
2896 if (arm_feature(env, ARM_FEATURE_M)) {
2897 /* M profile has no coprocessor registers */
2898 return;
2899 }
2900
e9aa6c21 2901 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
2902 if (!arm_feature(env, ARM_FEATURE_V8)) {
2903 /* Must go early as it is full of wildcards that may be
2904 * overridden by later definitions.
2905 */
2906 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
2907 }
2908
7d57f408 2909 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
2910 /* The ID registers all have impdef reset values */
2911 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
2912 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
2913 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
2914 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2915 .resetvalue = cpu->id_pfr0 },
0ff644a7
PM
2916 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
2917 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
2918 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2919 .resetvalue = cpu->id_pfr1 },
0ff644a7
PM
2920 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
2921 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
2922 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2923 .resetvalue = cpu->id_dfr0 },
0ff644a7
PM
2924 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
2925 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
2926 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2927 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
2928 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
2929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
2930 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2931 .resetvalue = cpu->id_mmfr0 },
0ff644a7
PM
2932 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
2933 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
2934 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2935 .resetvalue = cpu->id_mmfr1 },
0ff644a7
PM
2936 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
2937 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
2938 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2939 .resetvalue = cpu->id_mmfr2 },
0ff644a7
PM
2940 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
2941 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
2942 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2943 .resetvalue = cpu->id_mmfr3 },
0ff644a7
PM
2944 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
2945 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
2946 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2947 .resetvalue = cpu->id_isar0 },
0ff644a7
PM
2948 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
2949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
2950 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2951 .resetvalue = cpu->id_isar1 },
0ff644a7
PM
2952 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
2953 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2954 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2955 .resetvalue = cpu->id_isar2 },
0ff644a7
PM
2956 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
2957 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
2958 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2959 .resetvalue = cpu->id_isar3 },
0ff644a7
PM
2960 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
2961 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
2962 .access = PL1_R, .type = ARM_CP_CONST,
8515a092 2963 .resetvalue = cpu->id_isar4 },
0ff644a7
PM
2964 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
2965 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
2966 .access = PL1_R, .type = ARM_CP_CONST,
8515a092
PM
2967 .resetvalue = cpu->id_isar5 },
2968 /* 6..7 are as yet unallocated and must RAZ */
2969 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
2970 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
2971 .resetvalue = 0 },
2972 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
2973 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
2974 .resetvalue = 0 },
2975 REGINFO_SENTINEL
2976 };
2977 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
2978 define_arm_cp_regs(cpu, v6_cp_reginfo);
2979 } else {
2980 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
2981 }
4d31c596
PM
2982 if (arm_feature(env, ARM_FEATURE_V6K)) {
2983 define_arm_cp_regs(cpu, v6k_cp_reginfo);
2984 }
995939a6
PM
2985 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2986 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
2987 }
e9aa6c21 2988 if (arm_feature(env, ARM_FEATURE_V7)) {
200ac0ef 2989 /* v7 performance monitor control register: same implementor
7c2cb42b
AF
2990 * field as main ID register, and we implement only the cycle
2991 * count register.
200ac0ef 2992 */
7c2cb42b 2993#ifndef CONFIG_USER_ONLY
200ac0ef
PM
2994 ARMCPRegInfo pmcr = {
2995 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
8521466b 2996 .access = PL0_RW,
7a0e58fa 2997 .type = ARM_CP_IO | ARM_CP_ALIAS,
8521466b 2998 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
fcd25206
PM
2999 .accessfn = pmreg_access, .writefn = pmcr_write,
3000 .raw_writefn = raw_write,
200ac0ef 3001 };
8521466b
AF
3002 ARMCPRegInfo pmcr64 = {
3003 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
3004 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
3005 .access = PL0_RW, .accessfn = pmreg_access,
3006 .type = ARM_CP_IO,
3007 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
3008 .resetvalue = cpu->midr & 0xff000000,
3009 .writefn = pmcr_write, .raw_writefn = raw_write,
3010 };
7c2cb42b 3011 define_one_arm_cp_reg(cpu, &pmcr);
8521466b 3012 define_one_arm_cp_reg(cpu, &pmcr64);
7c2cb42b 3013#endif
776d4e5c 3014 ARMCPRegInfo clidr = {
7da845b0
PM
3015 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
3016 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
776d4e5c
PM
3017 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
3018 };
776d4e5c 3019 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 3020 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 3021 define_debug_regs(cpu);
7d57f408
PM
3022 } else {
3023 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 3024 }
b0d2b7d0 3025 if (arm_feature(env, ARM_FEATURE_V8)) {
e60cef86
PM
3026 /* AArch64 ID registers, which all have impdef reset values */
3027 ARMCPRegInfo v8_idregs[] = {
3028 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
3029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
3030 .access = PL1_R, .type = ARM_CP_CONST,
3031 .resetvalue = cpu->id_aa64pfr0 },
3032 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
3033 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
3034 .access = PL1_R, .type = ARM_CP_CONST,
3035 .resetvalue = cpu->id_aa64pfr1},
3036 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
3037 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
3038 .access = PL1_R, .type = ARM_CP_CONST,
5d831be2 3039 /* We mask out the PMUVer field, because we don't currently
9225d739
PM
3040 * implement the PMU. Not advertising it prevents the guest
3041 * from trying to use it and getting UNDEFs on registers we
3042 * don't implement.
3043 */
3044 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
e60cef86
PM
3045 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
3046 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
3047 .access = PL1_R, .type = ARM_CP_CONST,
3048 .resetvalue = cpu->id_aa64dfr1 },
3049 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
3050 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
3051 .access = PL1_R, .type = ARM_CP_CONST,
3052 .resetvalue = cpu->id_aa64afr0 },
3053 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
3054 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
3055 .access = PL1_R, .type = ARM_CP_CONST,
3056 .resetvalue = cpu->id_aa64afr1 },
3057 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
3058 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
3059 .access = PL1_R, .type = ARM_CP_CONST,
3060 .resetvalue = cpu->id_aa64isar0 },
3061 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
3062 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
3063 .access = PL1_R, .type = ARM_CP_CONST,
3064 .resetvalue = cpu->id_aa64isar1 },
3065 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
3066 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
3067 .access = PL1_R, .type = ARM_CP_CONST,
3068 .resetvalue = cpu->id_aa64mmfr0 },
3069 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
3070 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
3071 .access = PL1_R, .type = ARM_CP_CONST,
3072 .resetvalue = cpu->id_aa64mmfr1 },
a50c0f51
PM
3073 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
3074 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
3075 .access = PL1_R, .type = ARM_CP_CONST,
3076 .resetvalue = cpu->mvfr0 },
3077 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
3078 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
3079 .access = PL1_R, .type = ARM_CP_CONST,
3080 .resetvalue = cpu->mvfr1 },
3081 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
3082 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
3083 .access = PL1_R, .type = ARM_CP_CONST,
3084 .resetvalue = cpu->mvfr2 },
e60cef86
PM
3085 REGINFO_SENTINEL
3086 };
be8e8128
GB
3087 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
3088 if (!arm_feature(env, ARM_FEATURE_EL3) &&
3089 !arm_feature(env, ARM_FEATURE_EL2)) {
3090 ARMCPRegInfo rvbar = {
3091 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
3092 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
3093 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
3094 };
3095 define_one_arm_cp_reg(cpu, &rvbar);
3096 }
e60cef86 3097 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
3098 define_arm_cp_regs(cpu, v8_cp_reginfo);
3099 }
3b685ba7
EI
3100 if (arm_feature(env, ARM_FEATURE_EL2)) {
3101 define_arm_cp_regs(cpu, v8_el2_cp_reginfo);
be8e8128
GB
3102 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
3103 if (!arm_feature(env, ARM_FEATURE_EL3)) {
3104 ARMCPRegInfo rvbar = {
3105 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
3106 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
3107 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
3108 };
3109 define_one_arm_cp_reg(cpu, &rvbar);
3110 }
d42e3c26
EI
3111 } else {
3112 /* If EL2 is missing but higher ELs are enabled, we need to
3113 * register the no_el2 reginfos.
3114 */
3115 if (arm_feature(env, ARM_FEATURE_EL3)) {
3116 define_arm_cp_regs(cpu, v8_el3_no_el2_cp_reginfo);
3117 }
3b685ba7 3118 }
81547d66 3119 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 3120 define_arm_cp_regs(cpu, el3_cp_reginfo);
be8e8128
GB
3121 ARMCPRegInfo rvbar = {
3122 .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
3123 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
3124 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar
3125 };
3126 define_one_arm_cp_reg(cpu, &rvbar);
81547d66 3127 }
18032bec
PM
3128 if (arm_feature(env, ARM_FEATURE_MPU)) {
3129 /* These are the MPU registers prior to PMSAv6. Any new
3130 * PMSA core later than the ARM946 will require that we
3131 * implement the PMSAv6 or PMSAv7 registers, which are
3132 * completely different.
3133 */
3134 assert(!arm_feature(env, ARM_FEATURE_V6));
3135 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
3136 } else {
3137 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
3138 }
c326b979
PM
3139 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
3140 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
3141 }
6cc7a3ae
PM
3142 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
3143 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
3144 }
4a501606
PM
3145 if (arm_feature(env, ARM_FEATURE_VAPA)) {
3146 define_arm_cp_regs(cpu, vapa_cp_reginfo);
3147 }
c4804214
PM
3148 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
3149 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
3150 }
3151 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
3152 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
3153 }
3154 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
3155 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
3156 }
18032bec
PM
3157 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
3158 define_arm_cp_regs(cpu, omap_cp_reginfo);
3159 }
34f90529
PM
3160 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
3161 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
3162 }
1047b9d7
PM
3163 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3164 define_arm_cp_regs(cpu, xscale_cp_reginfo);
3165 }
3166 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
3167 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
3168 }
7ac681cf
PM
3169 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3170 define_arm_cp_regs(cpu, lpae_cp_reginfo);
3171 }
7884849c
PM
3172 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
3173 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
3174 * be read-only (ie write causes UNDEF exception).
3175 */
3176 {
00a29f3d
PM
3177 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
3178 /* Pre-v8 MIDR space.
3179 * Note that the MIDR isn't a simple constant register because
7884849c
PM
3180 * of the TI925 behaviour where writes to another register can
3181 * cause the MIDR value to change.
97ce8d61
PC
3182 *
3183 * Unimplemented registers in the c15 0 0 0 space default to
3184 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
3185 * and friends override accordingly.
7884849c
PM
3186 */
3187 { .name = "MIDR",
97ce8d61 3188 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 3189 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 3190 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
97ce8d61
PC
3191 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
3192 .type = ARM_CP_OVERRIDE },
7884849c
PM
3193 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
3194 { .name = "DUMMY",
3195 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
3196 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3197 { .name = "DUMMY",
3198 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
3199 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3200 { .name = "DUMMY",
3201 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
3202 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3203 { .name = "DUMMY",
3204 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
3205 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3206 { .name = "DUMMY",
3207 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
3208 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3209 REGINFO_SENTINEL
3210 };
00a29f3d
PM
3211 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
3212 /* v8 MIDR -- the wildcard isn't necessary, and nor is the
3213 * variable-MIDR TI925 behaviour. Instead we have a single
3214 * (strictly speaking IMPDEF) alias of the MIDR, REVIDR.
3215 */
3216 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
3217 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
3218 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
3219 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
3220 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
3221 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
3222 REGINFO_SENTINEL
3223 };
3224 ARMCPRegInfo id_cp_reginfo[] = {
3225 /* These are common to v8 and pre-v8 */
3226 { .name = "CTR",
3227 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
3228 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
3229 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
3230 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
3231 .access = PL0_R, .accessfn = ctr_el0_access,
3232 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
3233 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
3234 { .name = "TCMTR",
3235 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
3236 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3237 { .name = "TLBTR",
3238 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
3239 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3240 REGINFO_SENTINEL
3241 };
7884849c
PM
3242 ARMCPRegInfo crn0_wi_reginfo = {
3243 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
3244 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
3245 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
3246 };
3247 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
3248 arm_feature(env, ARM_FEATURE_STRONGARM)) {
3249 ARMCPRegInfo *r;
3250 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
3251 * whole space. Then update the specific ID registers to allow write
3252 * access, so that they ignore writes rather than causing them to
3253 * UNDEF.
7884849c
PM
3254 */
3255 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
3256 for (r = id_pre_v8_midr_cp_reginfo;
3257 r->type != ARM_CP_SENTINEL; r++) {
3258 r->access = PL1_RW;
3259 }
7884849c
PM
3260 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
3261 r->access = PL1_RW;
7884849c 3262 }
7884849c 3263 }
00a29f3d
PM
3264 if (arm_feature(env, ARM_FEATURE_V8)) {
3265 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
3266 } else {
3267 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
3268 }
a703eda1 3269 define_arm_cp_regs(cpu, id_cp_reginfo);
7884849c
PM
3270 }
3271
97ce8d61
PC
3272 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
3273 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
3274 }
3275
2771db27
PM
3276 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
3277 ARMCPRegInfo auxcr = {
2eef0bf8
PM
3278 .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
3279 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
2771db27
PM
3280 .access = PL1_RW, .type = ARM_CP_CONST,
3281 .resetvalue = cpu->reset_auxcr
3282 };
3283 define_one_arm_cp_reg(cpu, &auxcr);
3284 }
3285
d8ba780b 3286 if (arm_feature(env, ARM_FEATURE_CBAR)) {
f318cec6
PM
3287 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
3288 /* 32 bit view is [31:18] 0...0 [43:32]. */
3289 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
3290 | extract64(cpu->reset_cbar, 32, 12);
3291 ARMCPRegInfo cbar_reginfo[] = {
3292 { .name = "CBAR",
3293 .type = ARM_CP_CONST,
3294 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
3295 .access = PL1_R, .resetvalue = cpu->reset_cbar },
3296 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
3297 .type = ARM_CP_CONST,
3298 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
3299 .access = PL1_R, .resetvalue = cbar32 },
3300 REGINFO_SENTINEL
3301 };
3302 /* We don't implement a r/w 64 bit CBAR currently */
3303 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
3304 define_arm_cp_regs(cpu, cbar_reginfo);
3305 } else {
3306 ARMCPRegInfo cbar = {
3307 .name = "CBAR",
3308 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
3309 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
3310 .fieldoffset = offsetof(CPUARMState,
3311 cp15.c15_config_base_address)
3312 };
3313 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
3314 cbar.access = PL1_R;
3315 cbar.fieldoffset = 0;
3316 cbar.type = ARM_CP_CONST;
3317 }
3318 define_one_arm_cp_reg(cpu, &cbar);
3319 }
d8ba780b
PC
3320 }
3321
2771db27
PM
3322 /* Generic registers whose values depend on the implementation */
3323 {
3324 ARMCPRegInfo sctlr = {
5ebafdf3 3325 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9
FA
3326 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
3327 .access = PL1_RW,
3328 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
3329 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
3330 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
3331 .raw_writefn = raw_write,
2771db27
PM
3332 };
3333 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3334 /* Normally we would always end the TB on an SCTLR write, but Linux
3335 * arch/arm/mach-pxa/sleep.S expects two instructions following
3336 * an MMU enable to execute from cache. Imitate this behaviour.
3337 */
3338 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
3339 }
3340 define_one_arm_cp_reg(cpu, &sctlr);
3341 }
2ceb98c0
PM
3342}
3343
778c3a06 3344ARMCPU *cpu_arm_init(const char *cpu_model)
40f137e1 3345{
9262685b 3346 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
14969266
AF
3347}
3348
3349void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
3350{
22169d41 3351 CPUState *cs = CPU(cpu);
14969266
AF
3352 CPUARMState *env = &cpu->env;
3353
6a669427
PM
3354 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
3355 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
3356 aarch64_fpu_gdb_set_reg,
3357 34, "aarch64-fpu.xml", 0);
3358 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
22169d41 3359 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
3360 51, "arm-neon.xml", 0);
3361 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
22169d41 3362 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
3363 35, "arm-vfp3.xml", 0);
3364 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
22169d41 3365 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
56aebc89
PB
3366 19, "arm-vfp.xml", 0);
3367 }
40f137e1
PB
3368}
3369
777dc784
PM
3370/* Sort alphabetically by type name, except for "any". */
3371static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 3372{
777dc784
PM
3373 ObjectClass *class_a = (ObjectClass *)a;
3374 ObjectClass *class_b = (ObjectClass *)b;
3375 const char *name_a, *name_b;
5adb4839 3376
777dc784
PM
3377 name_a = object_class_get_name(class_a);
3378 name_b = object_class_get_name(class_b);
51492fd1 3379 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 3380 return 1;
51492fd1 3381 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
3382 return -1;
3383 } else {
3384 return strcmp(name_a, name_b);
5adb4839
PB
3385 }
3386}
3387
777dc784 3388static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 3389{
777dc784 3390 ObjectClass *oc = data;
92a31361 3391 CPUListState *s = user_data;
51492fd1
AF
3392 const char *typename;
3393 char *name;
3371d272 3394
51492fd1
AF
3395 typename = object_class_get_name(oc);
3396 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
777dc784 3397 (*s->cpu_fprintf)(s->file, " %s\n",
51492fd1
AF
3398 name);
3399 g_free(name);
777dc784
PM
3400}
3401
3402void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
3403{
92a31361 3404 CPUListState s = {
777dc784
PM
3405 .file = f,
3406 .cpu_fprintf = cpu_fprintf,
3407 };
3408 GSList *list;
3409
3410 list = object_class_get_list(TYPE_ARM_CPU, false);
3411 list = g_slist_sort(list, arm_cpu_list_compare);
3412 (*cpu_fprintf)(f, "Available CPUs:\n");
3413 g_slist_foreach(list, arm_cpu_list_entry, &s);
3414 g_slist_free(list);
a96c0514
PM
3415#ifdef CONFIG_KVM
3416 /* The 'host' CPU type is dynamically registered only if KVM is
3417 * enabled, so we have to special-case it here:
3418 */
3419 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
3420#endif
40f137e1
PB
3421}
3422
78027bb6
CR
3423static void arm_cpu_add_definition(gpointer data, gpointer user_data)
3424{
3425 ObjectClass *oc = data;
3426 CpuDefinitionInfoList **cpu_list = user_data;
3427 CpuDefinitionInfoList *entry;
3428 CpuDefinitionInfo *info;
3429 const char *typename;
3430
3431 typename = object_class_get_name(oc);
3432 info = g_malloc0(sizeof(*info));
3433 info->name = g_strndup(typename,
3434 strlen(typename) - strlen("-" TYPE_ARM_CPU));
3435
3436 entry = g_malloc0(sizeof(*entry));
3437 entry->value = info;
3438 entry->next = *cpu_list;
3439 *cpu_list = entry;
3440}
3441
3442CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
3443{
3444 CpuDefinitionInfoList *cpu_list = NULL;
3445 GSList *list;
3446
3447 list = object_class_get_list(TYPE_ARM_CPU, false);
3448 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
3449 g_slist_free(list);
3450
3451 return cpu_list;
3452}
3453
6e6efd61 3454static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 3455 void *opaque, int state, int secstate,
f5a0a5a5 3456 int crm, int opc1, int opc2)
6e6efd61
PM
3457{
3458 /* Private utility function for define_one_arm_cp_reg_with_opaque():
3459 * add a single reginfo struct to the hash table.
3460 */
3461 uint32_t *key = g_new(uint32_t, 1);
3462 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
3463 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
3464 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
3465
3466 /* Reset the secure state to the specific incoming state. This is
3467 * necessary as the register may have been defined with both states.
3468 */
3469 r2->secure = secstate;
3470
3471 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
3472 /* Register is banked (using both entries in array).
3473 * Overwriting fieldoffset as the array is only used to define
3474 * banked registers but later only fieldoffset is used.
f5a0a5a5 3475 */
3f3c82a5
FA
3476 r2->fieldoffset = r->bank_fieldoffsets[ns];
3477 }
3478
3479 if (state == ARM_CP_STATE_AA32) {
3480 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
3481 /* If the register is banked then we don't need to migrate or
3482 * reset the 32-bit instance in certain cases:
3483 *
3484 * 1) If the register has both 32-bit and 64-bit instances then we
3485 * can count on the 64-bit instance taking care of the
3486 * non-secure bank.
3487 * 2) If ARMv8 is enabled then we can count on a 64-bit version
3488 * taking care of the secure bank. This requires that separate
3489 * 32 and 64-bit definitions are provided.
3490 */
3491 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
3492 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 3493 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
3494 r2->resetfn = arm_cp_reset_ignore;
3495 }
3496 } else if ((secstate != r->secure) && !ns) {
3497 /* The register is not banked so we only want to allow migration of
3498 * the non-secure instance.
3499 */
7a0e58fa 3500 r2->type |= ARM_CP_ALIAS;
3f3c82a5 3501 r2->resetfn = arm_cp_reset_ignore;
58a1d8ce 3502 }
3f3c82a5
FA
3503
3504 if (r->state == ARM_CP_STATE_BOTH) {
3505 /* We assume it is a cp15 register if the .cp field is left unset.
3506 */
3507 if (r2->cp == 0) {
3508 r2->cp = 15;
3509 }
3510
f5a0a5a5 3511#ifdef HOST_WORDS_BIGENDIAN
3f3c82a5
FA
3512 if (r2->fieldoffset) {
3513 r2->fieldoffset += sizeof(uint32_t);
3514 }
f5a0a5a5 3515#endif
3f3c82a5 3516 }
f5a0a5a5
PM
3517 }
3518 if (state == ARM_CP_STATE_AA64) {
3519 /* To allow abbreviation of ARMCPRegInfo
3520 * definitions, we treat cp == 0 as equivalent to
3521 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
3522 * STATE_BOTH definitions are also always "standard
3523 * sysreg" in their AArch64 view (the .cp value may
3524 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 3525 */
58a1d8ce 3526 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
3527 r2->cp = CP_REG_ARM64_SYSREG_CP;
3528 }
3529 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
3530 r2->opc0, opc1, opc2);
3531 } else {
51a79b03 3532 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 3533 }
6e6efd61
PM
3534 if (opaque) {
3535 r2->opaque = opaque;
3536 }
67ed771d
PM
3537 /* reginfo passed to helpers is correct for the actual access,
3538 * and is never ARM_CP_STATE_BOTH:
3539 */
3540 r2->state = state;
6e6efd61
PM
3541 /* Make sure reginfo passed to helpers for wildcarded regs
3542 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
3543 */
3544 r2->crm = crm;
3545 r2->opc1 = opc1;
3546 r2->opc2 = opc2;
3547 /* By convention, for wildcarded registers only the first
3548 * entry is used for migration; the others are marked as
7a0e58fa 3549 * ALIAS so we don't try to transfer the register
6e6efd61 3550 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 3551 * never migratable and not even raw-accessible.
6e6efd61 3552 */
7a0e58fa
PM
3553 if ((r->type & ARM_CP_SPECIAL)) {
3554 r2->type |= ARM_CP_NO_RAW;
3555 }
3556 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
3557 ((r->opc1 == CP_ANY) && opc1 != 0) ||
3558 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7a0e58fa 3559 r2->type |= ARM_CP_ALIAS;
6e6efd61
PM
3560 }
3561
375421cc
PM
3562 /* Check that raw accesses are either forbidden or handled. Note that
3563 * we can't assert this earlier because the setup of fieldoffset for
3564 * banked registers has to be done first.
3565 */
3566 if (!(r2->type & ARM_CP_NO_RAW)) {
3567 assert(!raw_accessors_invalid(r2));
3568 }
3569
6e6efd61
PM
3570 /* Overriding of an existing definition must be explicitly
3571 * requested.
3572 */
3573 if (!(r->type & ARM_CP_OVERRIDE)) {
3574 ARMCPRegInfo *oldreg;
3575 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
3576 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
3577 fprintf(stderr, "Register redefined: cp=%d %d bit "
3578 "crn=%d crm=%d opc1=%d opc2=%d, "
3579 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
3580 r2->crn, r2->crm, r2->opc1, r2->opc2,
3581 oldreg->name, r2->name);
3582 g_assert_not_reached();
3583 }
3584 }
3585 g_hash_table_insert(cpu->cp_regs, key, r2);
3586}
3587
3588
4b6a83fb
PM
3589void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
3590 const ARMCPRegInfo *r, void *opaque)
3591{
3592 /* Define implementations of coprocessor registers.
3593 * We store these in a hashtable because typically
3594 * there are less than 150 registers in a space which
3595 * is 16*16*16*8*8 = 262144 in size.
3596 * Wildcarding is supported for the crm, opc1 and opc2 fields.
3597 * If a register is defined twice then the second definition is
3598 * used, so this can be used to define some generic registers and
3599 * then override them with implementation specific variations.
3600 * At least one of the original and the second definition should
3601 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
3602 * against accidental use.
f5a0a5a5
PM
3603 *
3604 * The state field defines whether the register is to be
3605 * visible in the AArch32 or AArch64 execution state. If the
3606 * state is set to ARM_CP_STATE_BOTH then we synthesise a
3607 * reginfo structure for the AArch32 view, which sees the lower
3608 * 32 bits of the 64 bit register.
3609 *
3610 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
3611 * be wildcarded. AArch64 registers are always considered to be 64
3612 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
3613 * the register, if any.
4b6a83fb 3614 */
f5a0a5a5 3615 int crm, opc1, opc2, state;
4b6a83fb
PM
3616 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
3617 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
3618 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
3619 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
3620 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
3621 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
3622 /* 64 bit registers have only CRm and Opc1 fields */
3623 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
3624 /* op0 only exists in the AArch64 encodings */
3625 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
3626 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
3627 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
3628 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
3629 * encodes a minimum access level for the register. We roll this
3630 * runtime check into our general permission check code, so check
3631 * here that the reginfo's specified permissions are strict enough
3632 * to encompass the generic architectural permission check.
3633 */
3634 if (r->state != ARM_CP_STATE_AA32) {
3635 int mask = 0;
3636 switch (r->opc1) {
3637 case 0: case 1: case 2:
3638 /* min_EL EL1 */
3639 mask = PL1_RW;
3640 break;
3641 case 3:
3642 /* min_EL EL0 */
3643 mask = PL0_RW;
3644 break;
3645 case 4:
3646 /* min_EL EL2 */
3647 mask = PL2_RW;
3648 break;
3649 case 5:
3650 /* unallocated encoding, so not possible */
3651 assert(false);
3652 break;
3653 case 6:
3654 /* min_EL EL3 */
3655 mask = PL3_RW;
3656 break;
3657 case 7:
3658 /* min_EL EL1, secure mode only (we don't check the latter) */
3659 mask = PL1_RW;
3660 break;
3661 default:
3662 /* broken reginfo with out-of-range opc1 */
3663 assert(false);
3664 break;
3665 }
3666 /* assert our permissions are not too lax (stricter is fine) */
3667 assert((r->access & ~mask) == 0);
3668 }
3669
4b6a83fb
PM
3670 /* Check that the register definition has enough info to handle
3671 * reads and writes if they are permitted.
3672 */
3673 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
3674 if (r->access & PL3_R) {
3f3c82a5
FA
3675 assert((r->fieldoffset ||
3676 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
3677 r->readfn);
4b6a83fb
PM
3678 }
3679 if (r->access & PL3_W) {
3f3c82a5
FA
3680 assert((r->fieldoffset ||
3681 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
3682 r->writefn);
4b6a83fb
PM
3683 }
3684 }
3685 /* Bad type field probably means missing sentinel at end of reg list */
3686 assert(cptype_valid(r->type));
3687 for (crm = crmmin; crm <= crmmax; crm++) {
3688 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
3689 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
3690 for (state = ARM_CP_STATE_AA32;
3691 state <= ARM_CP_STATE_AA64; state++) {
3692 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
3693 continue;
3694 }
3f3c82a5
FA
3695 if (state == ARM_CP_STATE_AA32) {
3696 /* Under AArch32 CP registers can be common
3697 * (same for secure and non-secure world) or banked.
3698 */
3699 switch (r->secure) {
3700 case ARM_CP_SECSTATE_S:
3701 case ARM_CP_SECSTATE_NS:
3702 add_cpreg_to_hashtable(cpu, r, opaque, state,
3703 r->secure, crm, opc1, opc2);
3704 break;
3705 default:
3706 add_cpreg_to_hashtable(cpu, r, opaque, state,
3707 ARM_CP_SECSTATE_S,
3708 crm, opc1, opc2);
3709 add_cpreg_to_hashtable(cpu, r, opaque, state,
3710 ARM_CP_SECSTATE_NS,
3711 crm, opc1, opc2);
3712 break;
3713 }
3714 } else {
3715 /* AArch64 registers get mapped to non-secure instance
3716 * of AArch32 */
3717 add_cpreg_to_hashtable(cpu, r, opaque, state,
3718 ARM_CP_SECSTATE_NS,
3719 crm, opc1, opc2);
3720 }
f5a0a5a5 3721 }
4b6a83fb
PM
3722 }
3723 }
3724 }
3725}
3726
3727void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
3728 const ARMCPRegInfo *regs, void *opaque)
3729{
3730 /* Define a whole list of registers */
3731 const ARMCPRegInfo *r;
3732 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
3733 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
3734 }
3735}
3736
60322b39 3737const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 3738{
60322b39 3739 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
3740}
3741
c4241c7d
PM
3742void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
3743 uint64_t value)
4b6a83fb
PM
3744{
3745 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
3746}
3747
c4241c7d 3748uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
3749{
3750 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
3751 return 0;
3752}
3753
f5a0a5a5
PM
3754void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
3755{
3756 /* Helper coprocessor reset function for do-nothing-on-reset registers */
3757}
3758
0ecb72a5 3759static int bad_mode_switch(CPUARMState *env, int mode)
37064a8b
PM
3760{
3761 /* Return true if it is not valid for us to switch to
3762 * this CPU mode (ie all the UNPREDICTABLE cases in
3763 * the ARM ARM CPSRWriteByInstr pseudocode).
3764 */
3765 switch (mode) {
3766 case ARM_CPU_MODE_USR:
3767 case ARM_CPU_MODE_SYS:
3768 case ARM_CPU_MODE_SVC:
3769 case ARM_CPU_MODE_ABT:
3770 case ARM_CPU_MODE_UND:
3771 case ARM_CPU_MODE_IRQ:
3772 case ARM_CPU_MODE_FIQ:
3773 return 0;
027fc527
SF
3774 case ARM_CPU_MODE_MON:
3775 return !arm_is_secure(env);
37064a8b
PM
3776 default:
3777 return 1;
3778 }
3779}
3780
2f4a40e5
AZ
3781uint32_t cpsr_read(CPUARMState *env)
3782{
3783 int ZF;
6fbe23d5
PB
3784 ZF = (env->ZF == 0);
3785 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
3786 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
3787 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
3788 | ((env->condexec_bits & 0xfc) << 8)
af519934 3789 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
3790}
3791
3792void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
3793{
6e8801f9
FA
3794 uint32_t changed_daif;
3795
2f4a40e5 3796 if (mask & CPSR_NZCV) {
6fbe23d5
PB
3797 env->ZF = (~val) & CPSR_Z;
3798 env->NF = val;
2f4a40e5
AZ
3799 env->CF = (val >> 29) & 1;
3800 env->VF = (val << 3) & 0x80000000;
3801 }
3802 if (mask & CPSR_Q)
3803 env->QF = ((val & CPSR_Q) != 0);
3804 if (mask & CPSR_T)
3805 env->thumb = ((val & CPSR_T) != 0);
3806 if (mask & CPSR_IT_0_1) {
3807 env->condexec_bits &= ~3;
3808 env->condexec_bits |= (val >> 25) & 3;
3809 }
3810 if (mask & CPSR_IT_2_7) {
3811 env->condexec_bits &= 3;
3812 env->condexec_bits |= (val >> 8) & 0xfc;
3813 }
3814 if (mask & CPSR_GE) {
3815 env->GE = (val >> 16) & 0xf;
3816 }
3817
6e8801f9
FA
3818 /* In a V7 implementation that includes the security extensions but does
3819 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
3820 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
3821 * bits respectively.
3822 *
3823 * In a V8 implementation, it is permitted for privileged software to
3824 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
3825 */
3826 if (!arm_feature(env, ARM_FEATURE_V8) &&
3827 arm_feature(env, ARM_FEATURE_EL3) &&
3828 !arm_feature(env, ARM_FEATURE_EL2) &&
3829 !arm_is_secure(env)) {
3830
3831 changed_daif = (env->daif ^ val) & mask;
3832
3833 if (changed_daif & CPSR_A) {
3834 /* Check to see if we are allowed to change the masking of async
3835 * abort exceptions from a non-secure state.
3836 */
3837 if (!(env->cp15.scr_el3 & SCR_AW)) {
3838 qemu_log_mask(LOG_GUEST_ERROR,
3839 "Ignoring attempt to switch CPSR_A flag from "
3840 "non-secure world with SCR.AW bit clear\n");
3841 mask &= ~CPSR_A;
3842 }
3843 }
3844
3845 if (changed_daif & CPSR_F) {
3846 /* Check to see if we are allowed to change the masking of FIQ
3847 * exceptions from a non-secure state.
3848 */
3849 if (!(env->cp15.scr_el3 & SCR_FW)) {
3850 qemu_log_mask(LOG_GUEST_ERROR,
3851 "Ignoring attempt to switch CPSR_F flag from "
3852 "non-secure world with SCR.FW bit clear\n");
3853 mask &= ~CPSR_F;
3854 }
3855
3856 /* Check whether non-maskable FIQ (NMFI) support is enabled.
3857 * If this bit is set software is not allowed to mask
3858 * FIQs, but is allowed to set CPSR_F to 0.
3859 */
3860 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
3861 (val & CPSR_F)) {
3862 qemu_log_mask(LOG_GUEST_ERROR,
3863 "Ignoring attempt to enable CPSR_F flag "
3864 "(non-maskable FIQ [NMFI] support enabled)\n");
3865 mask &= ~CPSR_F;
3866 }
3867 }
3868 }
3869
4cc35614
PM
3870 env->daif &= ~(CPSR_AIF & mask);
3871 env->daif |= val & CPSR_AIF & mask;
3872
2f4a40e5 3873 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
37064a8b
PM
3874 if (bad_mode_switch(env, val & CPSR_M)) {
3875 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
3876 * We choose to ignore the attempt and leave the CPSR M field
3877 * untouched.
3878 */
3879 mask &= ~CPSR_M;
3880 } else {
3881 switch_mode(env, val & CPSR_M);
3882 }
2f4a40e5
AZ
3883 }
3884 mask &= ~CACHED_CPSR_BITS;
3885 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
3886}
3887
b26eefb6
PB
3888/* Sign/zero extend */
3889uint32_t HELPER(sxtb16)(uint32_t x)
3890{
3891 uint32_t res;
3892 res = (uint16_t)(int8_t)x;
3893 res |= (uint32_t)(int8_t)(x >> 16) << 16;
3894 return res;
3895}
3896
3897uint32_t HELPER(uxtb16)(uint32_t x)
3898{
3899 uint32_t res;
3900 res = (uint16_t)(uint8_t)x;
3901 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
3902 return res;
3903}
3904
f51bbbfe
PB
3905uint32_t HELPER(clz)(uint32_t x)
3906{
7bbcb0af 3907 return clz32(x);
f51bbbfe
PB
3908}
3909
3670669c
PB
3910int32_t HELPER(sdiv)(int32_t num, int32_t den)
3911{
3912 if (den == 0)
3913 return 0;
686eeb93
AJ
3914 if (num == INT_MIN && den == -1)
3915 return INT_MIN;
3670669c
PB
3916 return num / den;
3917}
3918
3919uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
3920{
3921 if (den == 0)
3922 return 0;
3923 return num / den;
3924}
3925
3926uint32_t HELPER(rbit)(uint32_t x)
3927{
3928 x = ((x & 0xff000000) >> 24)
3929 | ((x & 0x00ff0000) >> 8)
3930 | ((x & 0x0000ff00) << 8)
3931 | ((x & 0x000000ff) << 24);
3932 x = ((x & 0xf0f0f0f0) >> 4)
3933 | ((x & 0x0f0f0f0f) << 4);
3934 x = ((x & 0x88888888) >> 3)
3935 | ((x & 0x44444444) >> 1)
3936 | ((x & 0x22222222) << 1)
3937 | ((x & 0x11111111) << 3);
3938 return x;
3939}
3940
5fafdf24 3941#if defined(CONFIG_USER_ONLY)
b5ff1b31 3942
7510454e
AF
3943int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
3944 int mmu_idx)
b5ff1b31 3945{
7510454e
AF
3946 ARMCPU *cpu = ARM_CPU(cs);
3947 CPUARMState *env = &cpu->env;
3948
abf1172f 3949 env->exception.vaddress = address;
b5ff1b31 3950 if (rw == 2) {
27103424 3951 cs->exception_index = EXCP_PREFETCH_ABORT;
b5ff1b31 3952 } else {
27103424 3953 cs->exception_index = EXCP_DATA_ABORT;
b5ff1b31
FB
3954 }
3955 return 1;
3956}
3957
9ee6e8bb 3958/* These should probably raise undefined insn exceptions. */
0ecb72a5 3959void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 3960{
a47dddd7
AF
3961 ARMCPU *cpu = arm_env_get_cpu(env);
3962
3963 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
9ee6e8bb
PB
3964}
3965
0ecb72a5 3966uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 3967{
a47dddd7
AF
3968 ARMCPU *cpu = arm_env_get_cpu(env);
3969
3970 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
9ee6e8bb
PB
3971 return 0;
3972}
3973
0ecb72a5 3974void switch_mode(CPUARMState *env, int mode)
b5ff1b31 3975{
a47dddd7
AF
3976 ARMCPU *cpu = arm_env_get_cpu(env);
3977
3978 if (mode != ARM_CPU_MODE_USR) {
3979 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
3980 }
b5ff1b31
FB
3981}
3982
0ecb72a5 3983void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
9ee6e8bb 3984{
a47dddd7
AF
3985 ARMCPU *cpu = arm_env_get_cpu(env);
3986
3987 cpu_abort(CPU(cpu), "banked r13 write\n");
9ee6e8bb
PB
3988}
3989
0ecb72a5 3990uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
9ee6e8bb 3991{
a47dddd7
AF
3992 ARMCPU *cpu = arm_env_get_cpu(env);
3993
3994 cpu_abort(CPU(cpu), "banked r13 read\n");
9ee6e8bb
PB
3995 return 0;
3996}
3997
9e729b57
EI
3998unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx)
3999{
4000 return 1;
4001}
4002
b5ff1b31
FB
4003#else
4004
4005/* Map CPU modes onto saved register banks. */
494b00c7 4006int bank_number(int mode)
b5ff1b31
FB
4007{
4008 switch (mode) {
4009 case ARM_CPU_MODE_USR:
4010 case ARM_CPU_MODE_SYS:
4011 return 0;
4012 case ARM_CPU_MODE_SVC:
4013 return 1;
4014 case ARM_CPU_MODE_ABT:
4015 return 2;
4016 case ARM_CPU_MODE_UND:
4017 return 3;
4018 case ARM_CPU_MODE_IRQ:
4019 return 4;
4020 case ARM_CPU_MODE_FIQ:
4021 return 5;
28c9457d
EI
4022 case ARM_CPU_MODE_HYP:
4023 return 6;
4024 case ARM_CPU_MODE_MON:
4025 return 7;
b5ff1b31 4026 }
f5206413 4027 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
b5ff1b31
FB
4028}
4029
0ecb72a5 4030void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
4031{
4032 int old_mode;
4033 int i;
4034
4035 old_mode = env->uncached_cpsr & CPSR_M;
4036 if (mode == old_mode)
4037 return;
4038
4039 if (old_mode == ARM_CPU_MODE_FIQ) {
4040 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 4041 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
4042 } else if (mode == ARM_CPU_MODE_FIQ) {
4043 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 4044 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
4045 }
4046
f5206413 4047 i = bank_number(old_mode);
b5ff1b31
FB
4048 env->banked_r13[i] = env->regs[13];
4049 env->banked_r14[i] = env->regs[14];
4050 env->banked_spsr[i] = env->spsr;
4051
f5206413 4052 i = bank_number(mode);
b5ff1b31
FB
4053 env->regs[13] = env->banked_r13[i];
4054 env->regs[14] = env->banked_r14[i];
4055 env->spsr = env->banked_spsr[i];
4056}
4057
0eeb17d6
GB
4058/* Physical Interrupt Target EL Lookup Table
4059 *
4060 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
4061 *
4062 * The below multi-dimensional table is used for looking up the target
4063 * exception level given numerous condition criteria. Specifically, the
4064 * target EL is based on SCR and HCR routing controls as well as the
4065 * currently executing EL and secure state.
4066 *
4067 * Dimensions:
4068 * target_el_table[2][2][2][2][2][4]
4069 * | | | | | +--- Current EL
4070 * | | | | +------ Non-secure(0)/Secure(1)
4071 * | | | +--------- HCR mask override
4072 * | | +------------ SCR exec state control
4073 * | +--------------- SCR mask override
4074 * +------------------ 32-bit(0)/64-bit(1) EL3
4075 *
4076 * The table values are as such:
4077 * 0-3 = EL0-EL3
4078 * -1 = Cannot occur
4079 *
4080 * The ARM ARM target EL table includes entries indicating that an "exception
4081 * is not taken". The two cases where this is applicable are:
4082 * 1) An exception is taken from EL3 but the SCR does not have the exception
4083 * routed to EL3.
4084 * 2) An exception is taken from EL2 but the HCR does not have the exception
4085 * routed to EL2.
4086 * In these two cases, the below table contain a target of EL1. This value is
4087 * returned as it is expected that the consumer of the table data will check
4088 * for "target EL >= current EL" to ensure the exception is not taken.
4089 *
4090 * SCR HCR
4091 * 64 EA AMO From
4092 * BIT IRQ IMO Non-secure Secure
4093 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
4094 */
4095const int8_t target_el_table[2][2][2][2][2][4] = {
4096 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
4097 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
4098 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
4099 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
4100 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
4101 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
4102 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
4103 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
4104 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
4105 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
4106 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
4107 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
4108 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
4109 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
4110 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
4111 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
4112};
4113
4114/*
4115 * Determine the target EL for physical exceptions
4116 */
4117static inline uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
4118 uint32_t cur_el, bool secure)
4119{
4120 CPUARMState *env = cs->env_ptr;
4121 int rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
4122 int scr;
4123 int hcr;
4124 int target_el;
4125 int is64 = arm_el_is_aa64(env, 3);
4126
4127 switch (excp_idx) {
4128 case EXCP_IRQ:
4129 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
4130 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
4131 break;
4132 case EXCP_FIQ:
4133 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
4134 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
4135 break;
4136 default:
4137 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
4138 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
4139 break;
4140 };
4141
4142 /* If HCR.TGE is set then HCR is treated as being 1 */
4143 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
4144
4145 /* Perform a table-lookup for the target EL given the current state */
4146 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
4147
4148 assert(target_el > 0);
4149
4150 return target_el;
4151}
4152
9e729b57
EI
4153/*
4154 * Determine the target EL for a given exception type.
4155 */
4156unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx)
4157{
35979d71
EI
4158 ARMCPU *cpu = ARM_CPU(cs);
4159 CPUARMState *env = &cpu->env;
dcbff19b 4160 unsigned int cur_el = arm_current_el(env);
35979d71 4161 unsigned int target_el;
0eeb17d6 4162 bool secure = arm_is_secure(env);
35979d71
EI
4163
4164 switch (excp_idx) {
4165 case EXCP_HVC:
607d98b8 4166 case EXCP_HYP_TRAP:
35979d71
EI
4167 target_el = 2;
4168 break;
e0d6e6a5
EI
4169 case EXCP_SMC:
4170 target_el = 3;
4171 break;
041c9666
EI
4172 case EXCP_FIQ:
4173 case EXCP_IRQ:
0eeb17d6 4174 target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
041c9666 4175 break;
136e67e9
EI
4176 case EXCP_VIRQ:
4177 case EXCP_VFIQ:
4178 target_el = 1;
4179 break;
35979d71
EI
4180 default:
4181 target_el = MAX(cur_el, 1);
4182 break;
4183 }
4184 return target_el;
9e729b57
EI
4185}
4186
9ee6e8bb
PB
4187static void v7m_push(CPUARMState *env, uint32_t val)
4188{
70d74660
AF
4189 CPUState *cs = CPU(arm_env_get_cpu(env));
4190
9ee6e8bb 4191 env->regs[13] -= 4;
ab1da857 4192 stl_phys(cs->as, env->regs[13], val);
9ee6e8bb
PB
4193}
4194
4195static uint32_t v7m_pop(CPUARMState *env)
4196{
70d74660 4197 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb 4198 uint32_t val;
70d74660 4199
fdfba1a2 4200 val = ldl_phys(cs->as, env->regs[13]);
9ee6e8bb
PB
4201 env->regs[13] += 4;
4202 return val;
4203}
4204
4205/* Switch to V7M main or process stack pointer. */
4206static void switch_v7m_sp(CPUARMState *env, int process)
4207{
4208 uint32_t tmp;
4209 if (env->v7m.current_sp != process) {
4210 tmp = env->v7m.other_sp;
4211 env->v7m.other_sp = env->regs[13];
4212 env->regs[13] = tmp;
4213 env->v7m.current_sp = process;
4214 }
4215}
4216
4217static void do_v7m_exception_exit(CPUARMState *env)
4218{
4219 uint32_t type;
4220 uint32_t xpsr;
4221
4222 type = env->regs[15];
4223 if (env->v7m.exception != 0)
983fe826 4224 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
9ee6e8bb
PB
4225
4226 /* Switch to the target stack. */
4227 switch_v7m_sp(env, (type & 4) != 0);
4228 /* Pop registers. */
4229 env->regs[0] = v7m_pop(env);
4230 env->regs[1] = v7m_pop(env);
4231 env->regs[2] = v7m_pop(env);
4232 env->regs[3] = v7m_pop(env);
4233 env->regs[12] = v7m_pop(env);
4234 env->regs[14] = v7m_pop(env);
4235 env->regs[15] = v7m_pop(env);
4236 xpsr = v7m_pop(env);
4237 xpsr_write(env, xpsr, 0xfffffdff);
4238 /* Undo stack alignment. */
4239 if (xpsr & 0x200)
4240 env->regs[13] |= 4;
4241 /* ??? The exception return type specifies Thread/Handler mode. However
4242 this is also implied by the xPSR value. Not sure what to do
4243 if there is a mismatch. */
4244 /* ??? Likewise for mismatches between the CONTROL register and the stack
4245 pointer. */
4246}
4247
e6f010cc 4248void arm_v7m_cpu_do_interrupt(CPUState *cs)
9ee6e8bb 4249{
e6f010cc
AF
4250 ARMCPU *cpu = ARM_CPU(cs);
4251 CPUARMState *env = &cpu->env;
9ee6e8bb
PB
4252 uint32_t xpsr = xpsr_read(env);
4253 uint32_t lr;
4254 uint32_t addr;
4255
27103424 4256 arm_log_exception(cs->exception_index);
3f1beaca 4257
9ee6e8bb
PB
4258 lr = 0xfffffff1;
4259 if (env->v7m.current_sp)
4260 lr |= 4;
4261 if (env->v7m.exception == 0)
4262 lr |= 8;
4263
4264 /* For exceptions we just mark as pending on the NVIC, and let that
4265 handle it. */
4266 /* TODO: Need to escalate if the current priority is higher than the
4267 one we're raising. */
27103424 4268 switch (cs->exception_index) {
9ee6e8bb 4269 case EXCP_UDEF:
983fe826 4270 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
9ee6e8bb
PB
4271 return;
4272 case EXCP_SWI:
314e2296 4273 /* The PC already points to the next instruction. */
983fe826 4274 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
9ee6e8bb
PB
4275 return;
4276 case EXCP_PREFETCH_ABORT:
4277 case EXCP_DATA_ABORT:
abf1172f
PM
4278 /* TODO: if we implemented the MPU registers, this is where we
4279 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
4280 */
983fe826 4281 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
9ee6e8bb
PB
4282 return;
4283 case EXCP_BKPT:
2ad207d4
PB
4284 if (semihosting_enabled) {
4285 int nr;
d31dd73e 4286 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2ad207d4
PB
4287 if (nr == 0xab) {
4288 env->regs[15] += 2;
4289 env->regs[0] = do_arm_semihosting(env);
3f1beaca 4290 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2ad207d4
PB
4291 return;
4292 }
4293 }
983fe826 4294 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
9ee6e8bb
PB
4295 return;
4296 case EXCP_IRQ:
983fe826 4297 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
9ee6e8bb
PB
4298 break;
4299 case EXCP_EXCEPTION_EXIT:
4300 do_v7m_exception_exit(env);
4301 return;
4302 default:
a47dddd7 4303 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9ee6e8bb
PB
4304 return; /* Never happens. Keep compiler happy. */
4305 }
4306
4307 /* Align stack pointer. */
4308 /* ??? Should only do this if Configuration Control Register
4309 STACKALIGN bit is set. */
4310 if (env->regs[13] & 4) {
ab19b0ec 4311 env->regs[13] -= 4;
9ee6e8bb
PB
4312 xpsr |= 0x200;
4313 }
6c95676b 4314 /* Switch to the handler mode. */
9ee6e8bb
PB
4315 v7m_push(env, xpsr);
4316 v7m_push(env, env->regs[15]);
4317 v7m_push(env, env->regs[14]);
4318 v7m_push(env, env->regs[12]);
4319 v7m_push(env, env->regs[3]);
4320 v7m_push(env, env->regs[2]);
4321 v7m_push(env, env->regs[1]);
4322 v7m_push(env, env->regs[0]);
4323 switch_v7m_sp(env, 0);
c98d174c
PM
4324 /* Clear IT bits */
4325 env->condexec_bits = 0;
9ee6e8bb 4326 env->regs[14] = lr;
fdfba1a2 4327 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
9ee6e8bb
PB
4328 env->regs[15] = addr & 0xfffffffe;
4329 env->thumb = addr & 1;
4330}
4331
b5ff1b31 4332/* Handle a CPU exception. */
97a8ea5a 4333void arm_cpu_do_interrupt(CPUState *cs)
b5ff1b31 4334{
97a8ea5a
AF
4335 ARMCPU *cpu = ARM_CPU(cs);
4336 CPUARMState *env = &cpu->env;
b5ff1b31
FB
4337 uint32_t addr;
4338 uint32_t mask;
4339 int new_mode;
4340 uint32_t offset;
16a906fd 4341 uint32_t moe;
b5ff1b31 4342
e6f010cc
AF
4343 assert(!IS_M(env));
4344
27103424 4345 arm_log_exception(cs->exception_index);
3f1beaca 4346
98128601
RH
4347 if (arm_is_psci_call(cpu, cs->exception_index)) {
4348 arm_handle_psci_call(cpu);
4349 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
4350 return;
4351 }
4352
16a906fd
PM
4353 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
4354 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
4355 case EC_BREAKPOINT:
4356 case EC_BREAKPOINT_SAME_EL:
4357 moe = 1;
4358 break;
4359 case EC_WATCHPOINT:
4360 case EC_WATCHPOINT_SAME_EL:
4361 moe = 10;
4362 break;
4363 case EC_AA32_BKPT:
4364 moe = 3;
4365 break;
4366 case EC_VECTORCATCH:
4367 moe = 5;
4368 break;
4369 default:
4370 moe = 0;
4371 break;
4372 }
4373
4374 if (moe) {
4375 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
4376 }
4377
b5ff1b31 4378 /* TODO: Vectored interrupt controller. */
27103424 4379 switch (cs->exception_index) {
b5ff1b31
FB
4380 case EXCP_UDEF:
4381 new_mode = ARM_CPU_MODE_UND;
4382 addr = 0x04;
4383 mask = CPSR_I;
4384 if (env->thumb)
4385 offset = 2;
4386 else
4387 offset = 4;
4388 break;
4389 case EXCP_SWI:
8e71621f
PB
4390 if (semihosting_enabled) {
4391 /* Check for semihosting interrupt. */
4392 if (env->thumb) {
d31dd73e
BS
4393 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
4394 & 0xff;
8e71621f 4395 } else {
d31dd73e 4396 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
d8fd2954 4397 & 0xffffff;
8e71621f
PB
4398 }
4399 /* Only intercept calls from privileged modes, to provide some
4400 semblance of security. */
4401 if (((mask == 0x123456 && !env->thumb)
4402 || (mask == 0xab && env->thumb))
4403 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
4404 env->regs[0] = do_arm_semihosting(env);
3f1beaca 4405 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
8e71621f
PB
4406 return;
4407 }
4408 }
b5ff1b31
FB
4409 new_mode = ARM_CPU_MODE_SVC;
4410 addr = 0x08;
4411 mask = CPSR_I;
601d70b9 4412 /* The PC already points to the next instruction. */
b5ff1b31
FB
4413 offset = 0;
4414 break;
06c949e6 4415 case EXCP_BKPT:
9ee6e8bb 4416 /* See if this is a semihosting syscall. */
2ad207d4 4417 if (env->thumb && semihosting_enabled) {
d31dd73e 4418 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
9ee6e8bb
PB
4419 if (mask == 0xab
4420 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
4421 env->regs[15] += 2;
4422 env->regs[0] = do_arm_semihosting(env);
3f1beaca 4423 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
9ee6e8bb
PB
4424 return;
4425 }
4426 }
abf1172f 4427 env->exception.fsr = 2;
9ee6e8bb
PB
4428 /* Fall through to prefetch abort. */
4429 case EXCP_PREFETCH_ABORT:
88ca1c2d 4430 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 4431 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 4432 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 4433 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
4434 new_mode = ARM_CPU_MODE_ABT;
4435 addr = 0x0c;
4436 mask = CPSR_A | CPSR_I;
4437 offset = 4;
4438 break;
4439 case EXCP_DATA_ABORT:
4a7e2d73 4440 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 4441 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 4442 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 4443 env->exception.fsr,
6cd8a264 4444 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
4445 new_mode = ARM_CPU_MODE_ABT;
4446 addr = 0x10;
4447 mask = CPSR_A | CPSR_I;
4448 offset = 8;
4449 break;
4450 case EXCP_IRQ:
4451 new_mode = ARM_CPU_MODE_IRQ;
4452 addr = 0x18;
4453 /* Disable IRQ and imprecise data aborts. */
4454 mask = CPSR_A | CPSR_I;
4455 offset = 4;
de38d23b
FA
4456 if (env->cp15.scr_el3 & SCR_IRQ) {
4457 /* IRQ routed to monitor mode */
4458 new_mode = ARM_CPU_MODE_MON;
4459 mask |= CPSR_F;
4460 }
b5ff1b31
FB
4461 break;
4462 case EXCP_FIQ:
4463 new_mode = ARM_CPU_MODE_FIQ;
4464 addr = 0x1c;
4465 /* Disable FIQ, IRQ and imprecise data aborts. */
4466 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
4467 if (env->cp15.scr_el3 & SCR_FIQ) {
4468 /* FIQ routed to monitor mode */
4469 new_mode = ARM_CPU_MODE_MON;
4470 }
b5ff1b31
FB
4471 offset = 4;
4472 break;
dbe9d163
FA
4473 case EXCP_SMC:
4474 new_mode = ARM_CPU_MODE_MON;
4475 addr = 0x08;
4476 mask = CPSR_A | CPSR_I | CPSR_F;
4477 offset = 0;
4478 break;
b5ff1b31 4479 default:
a47dddd7 4480 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
4481 return; /* Never happens. Keep compiler happy. */
4482 }
e89e51a1
FA
4483
4484 if (new_mode == ARM_CPU_MODE_MON) {
4485 addr += env->cp15.mvbar;
137feaa9 4486 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 4487 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 4488 addr += 0xffff0000;
8641136c
NR
4489 } else {
4490 /* ARM v7 architectures provide a vector base address register to remap
4491 * the interrupt vector table.
e89e51a1 4492 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
4493 * Note: only bits 31:5 are valid.
4494 */
fb6c91ba 4495 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 4496 }
dbe9d163
FA
4497
4498 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
4499 env->cp15.scr_el3 &= ~SCR_NS;
4500 }
4501
b5ff1b31 4502 switch_mode (env, new_mode);
662cefb7
PM
4503 /* For exceptions taken to AArch32 we must clear the SS bit in both
4504 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
4505 */
4506 env->uncached_cpsr &= ~PSTATE_SS;
b5ff1b31 4507 env->spsr = cpsr_read(env);
9ee6e8bb
PB
4508 /* Clear IT bits. */
4509 env->condexec_bits = 0;
30a8cac1 4510 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 4511 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
4cc35614 4512 env->daif |= mask;
be5e7a76
DES
4513 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
4514 * and we should just guard the thumb mode on V4 */
4515 if (arm_feature(env, ARM_FEATURE_V4T)) {
137feaa9 4516 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
be5e7a76 4517 }
b5ff1b31
FB
4518 env->regs[14] = env->regs[15] + offset;
4519 env->regs[15] = addr;
259186a7 4520 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
b5ff1b31
FB
4521}
4522
4523/* Check section/page access permissions.
4524 Returns the page protection flags, or zero if the access is not
4525 permitted. */
0ecb72a5 4526static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
dd4ebc2e 4527 int access_type, int is_user)
b5ff1b31 4528{
9ee6e8bb
PB
4529 int prot_ro;
4530
dd4ebc2e 4531 if (domain_prot == 3) {
b5ff1b31 4532 return PAGE_READ | PAGE_WRITE;
dd4ebc2e 4533 }
b5ff1b31 4534
9ee6e8bb
PB
4535 if (access_type == 1)
4536 prot_ro = 0;
4537 else
4538 prot_ro = PAGE_READ;
4539
b5ff1b31
FB
4540 switch (ap) {
4541 case 0:
99f678a6
PM
4542 if (arm_feature(env, ARM_FEATURE_V7)) {
4543 return 0;
4544 }
78600320 4545 if (access_type == 1)
b5ff1b31 4546 return 0;
137feaa9 4547 switch (A32_BANKED_CURRENT_REG_GET(env, sctlr) & (SCTLR_S | SCTLR_R)) {
76e3e1bc 4548 case SCTLR_S:
b5ff1b31 4549 return is_user ? 0 : PAGE_READ;
76e3e1bc 4550 case SCTLR_R:
b5ff1b31
FB
4551 return PAGE_READ;
4552 default:
4553 return 0;
4554 }
4555 case 1:
4556 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
4557 case 2:
4558 if (is_user)
9ee6e8bb 4559 return prot_ro;
b5ff1b31
FB
4560 else
4561 return PAGE_READ | PAGE_WRITE;
4562 case 3:
4563 return PAGE_READ | PAGE_WRITE;
d4934d18 4564 case 4: /* Reserved. */
9ee6e8bb
PB
4565 return 0;
4566 case 5:
4567 return is_user ? 0 : prot_ro;
4568 case 6:
4569 return prot_ro;
d4934d18 4570 case 7:
0ab06d83 4571 if (!arm_feature (env, ARM_FEATURE_V6K))
d4934d18
PB
4572 return 0;
4573 return prot_ro;
b5ff1b31
FB
4574 default:
4575 abort();
4576 }
4577}
4578
e389be16
FA
4579static bool get_level1_table_address(CPUARMState *env, uint32_t *table,
4580 uint32_t address)
b2fa1797 4581{
11f136ee
FA
4582 /* Get the TCR bank based on our security state */
4583 TCR *tcr = &env->cp15.tcr_el[arm_is_secure(env) ? 3 : 1];
4584
7dd8c9af
FA
4585 /* We only get here if EL1 is running in AArch32. If EL3 is running in
4586 * AArch32 there is a secure and non-secure instance of the translation
4587 * table registers.
4588 */
11f136ee
FA
4589 if (address & tcr->mask) {
4590 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
4591 /* Translation table walk disabled for TTBR1 */
4592 return false;
4593 }
7dd8c9af 4594 *table = A32_BANKED_CURRENT_REG_GET(env, ttbr1) & 0xffffc000;
e389be16 4595 } else {
11f136ee 4596 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
4597 /* Translation table walk disabled for TTBR0 */
4598 return false;
4599 }
11f136ee 4600 *table = A32_BANKED_CURRENT_REG_GET(env, ttbr0) & tcr->base_mask;
e389be16
FA
4601 }
4602 *table |= (address >> 18) & 0x3ffc;
4603 return true;
b2fa1797
PB
4604}
4605
0ecb72a5 4606static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
a8170e5e 4607 int is_user, hwaddr *phys_ptr,
77a71dd1 4608 int *prot, target_ulong *page_size)
b5ff1b31 4609{
70d74660 4610 CPUState *cs = CPU(arm_env_get_cpu(env));
b5ff1b31
FB
4611 int code;
4612 uint32_t table;
4613 uint32_t desc;
4614 int type;
4615 int ap;
e389be16 4616 int domain = 0;
dd4ebc2e 4617 int domain_prot;
a8170e5e 4618 hwaddr phys_addr;
b5ff1b31 4619
9ee6e8bb
PB
4620 /* Pagetable walk. */
4621 /* Lookup l1 descriptor. */
e389be16
FA
4622 if (!get_level1_table_address(env, &table, address)) {
4623 /* Section translation fault if page walk is disabled by PD0 or PD1 */
4624 code = 5;
4625 goto do_fault;
4626 }
fdfba1a2 4627 desc = ldl_phys(cs->as, table);
9ee6e8bb 4628 type = (desc & 3);
dd4ebc2e 4629 domain = (desc >> 5) & 0x0f;
0c17d68c 4630 domain_prot = (A32_BANKED_CURRENT_REG_GET(env, dacr) >> (domain * 2)) & 3;
9ee6e8bb 4631 if (type == 0) {
601d70b9 4632 /* Section translation fault. */
9ee6e8bb
PB
4633 code = 5;
4634 goto do_fault;
4635 }
dd4ebc2e 4636 if (domain_prot == 0 || domain_prot == 2) {
9ee6e8bb
PB
4637 if (type == 2)
4638 code = 9; /* Section domain fault. */
4639 else
4640 code = 11; /* Page domain fault. */
4641 goto do_fault;
4642 }
4643 if (type == 2) {
4644 /* 1Mb section. */
4645 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
4646 ap = (desc >> 10) & 3;
4647 code = 13;
d4c430a8 4648 *page_size = 1024 * 1024;
9ee6e8bb
PB
4649 } else {
4650 /* Lookup l2 entry. */
4651 if (type == 1) {
4652 /* Coarse pagetable. */
4653 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
4654 } else {
4655 /* Fine pagetable. */
4656 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
4657 }
fdfba1a2 4658 desc = ldl_phys(cs->as, table);
9ee6e8bb
PB
4659 switch (desc & 3) {
4660 case 0: /* Page translation fault. */
4661 code = 7;
4662 goto do_fault;
4663 case 1: /* 64k page. */
4664 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
4665 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 4666 *page_size = 0x10000;
ce819861 4667 break;
9ee6e8bb
PB
4668 case 2: /* 4k page. */
4669 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 4670 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 4671 *page_size = 0x1000;
ce819861 4672 break;
9ee6e8bb
PB
4673 case 3: /* 1k page. */
4674 if (type == 1) {
4675 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4676 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
4677 } else {
4678 /* Page translation fault. */
4679 code = 7;
4680 goto do_fault;
4681 }
4682 } else {
4683 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
4684 }
4685 ap = (desc >> 4) & 3;
d4c430a8 4686 *page_size = 0x400;
ce819861
PB
4687 break;
4688 default:
9ee6e8bb
PB
4689 /* Never happens, but compiler isn't smart enough to tell. */
4690 abort();
ce819861 4691 }
9ee6e8bb
PB
4692 code = 15;
4693 }
dd4ebc2e 4694 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
9ee6e8bb
PB
4695 if (!*prot) {
4696 /* Access permission fault. */
4697 goto do_fault;
4698 }
3ad493fc 4699 *prot |= PAGE_EXEC;
9ee6e8bb
PB
4700 *phys_ptr = phys_addr;
4701 return 0;
4702do_fault:
4703 return code | (domain << 4);
4704}
4705
0ecb72a5 4706static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
a8170e5e 4707 int is_user, hwaddr *phys_ptr,
77a71dd1 4708 int *prot, target_ulong *page_size)
9ee6e8bb 4709{
70d74660 4710 CPUState *cs = CPU(arm_env_get_cpu(env));
9ee6e8bb
PB
4711 int code;
4712 uint32_t table;
4713 uint32_t desc;
4714 uint32_t xn;
de9b05b8 4715 uint32_t pxn = 0;
9ee6e8bb
PB
4716 int type;
4717 int ap;
de9b05b8 4718 int domain = 0;
dd4ebc2e 4719 int domain_prot;
a8170e5e 4720 hwaddr phys_addr;
9ee6e8bb
PB
4721
4722 /* Pagetable walk. */
4723 /* Lookup l1 descriptor. */
e389be16
FA
4724 if (!get_level1_table_address(env, &table, address)) {
4725 /* Section translation fault if page walk is disabled by PD0 or PD1 */
4726 code = 5;
4727 goto do_fault;
4728 }
fdfba1a2 4729 desc = ldl_phys(cs->as, table);
9ee6e8bb 4730 type = (desc & 3);
de9b05b8
PM
4731 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
4732 /* Section translation fault, or attempt to use the encoding
4733 * which is Reserved on implementations without PXN.
4734 */
9ee6e8bb 4735 code = 5;
9ee6e8bb 4736 goto do_fault;
de9b05b8
PM
4737 }
4738 if ((type == 1) || !(desc & (1 << 18))) {
4739 /* Page or Section. */
dd4ebc2e 4740 domain = (desc >> 5) & 0x0f;
9ee6e8bb 4741 }
0c17d68c 4742 domain_prot = (A32_BANKED_CURRENT_REG_GET(env, dacr) >> (domain * 2)) & 3;
dd4ebc2e 4743 if (domain_prot == 0 || domain_prot == 2) {
de9b05b8 4744 if (type != 1) {
9ee6e8bb 4745 code = 9; /* Section domain fault. */
de9b05b8 4746 } else {
9ee6e8bb 4747 code = 11; /* Page domain fault. */
de9b05b8 4748 }
9ee6e8bb
PB
4749 goto do_fault;
4750 }
de9b05b8 4751 if (type != 1) {
9ee6e8bb
PB
4752 if (desc & (1 << 18)) {
4753 /* Supersection. */
4754 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
d4c430a8 4755 *page_size = 0x1000000;
b5ff1b31 4756 } else {
9ee6e8bb
PB
4757 /* Section. */
4758 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 4759 *page_size = 0x100000;
b5ff1b31 4760 }
9ee6e8bb
PB
4761 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
4762 xn = desc & (1 << 4);
de9b05b8 4763 pxn = desc & 1;
9ee6e8bb
PB
4764 code = 13;
4765 } else {
de9b05b8
PM
4766 if (arm_feature(env, ARM_FEATURE_PXN)) {
4767 pxn = (desc >> 2) & 1;
4768 }
9ee6e8bb
PB
4769 /* Lookup l2 entry. */
4770 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
fdfba1a2 4771 desc = ldl_phys(cs->as, table);
9ee6e8bb
PB
4772 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
4773 switch (desc & 3) {
4774 case 0: /* Page translation fault. */
4775 code = 7;
b5ff1b31 4776 goto do_fault;
9ee6e8bb
PB
4777 case 1: /* 64k page. */
4778 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
4779 xn = desc & (1 << 15);
d4c430a8 4780 *page_size = 0x10000;
9ee6e8bb
PB
4781 break;
4782 case 2: case 3: /* 4k page. */
4783 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
4784 xn = desc & 1;
d4c430a8 4785 *page_size = 0x1000;
9ee6e8bb
PB
4786 break;
4787 default:
4788 /* Never happens, but compiler isn't smart enough to tell. */
4789 abort();
b5ff1b31 4790 }
9ee6e8bb
PB
4791 code = 15;
4792 }
dd4ebc2e 4793 if (domain_prot == 3) {
c0034328
JR
4794 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4795 } else {
de9b05b8
PM
4796 if (pxn && !is_user) {
4797 xn = 1;
4798 }
c0034328
JR
4799 if (xn && access_type == 2)
4800 goto do_fault;
9ee6e8bb 4801
c0034328 4802 /* The simplified model uses AP[0] as an access control bit. */
137feaa9
FA
4803 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_AFE)
4804 && (ap & 1) == 0) {
c0034328
JR
4805 /* Access flag fault. */
4806 code = (code == 15) ? 6 : 3;
4807 goto do_fault;
4808 }
dd4ebc2e 4809 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
c0034328
JR
4810 if (!*prot) {
4811 /* Access permission fault. */
4812 goto do_fault;
4813 }
4814 if (!xn) {
4815 *prot |= PAGE_EXEC;
4816 }
3ad493fc 4817 }
9ee6e8bb 4818 *phys_ptr = phys_addr;
b5ff1b31
FB
4819 return 0;
4820do_fault:
4821 return code | (domain << 4);
4822}
4823
3dde962f
PM
4824/* Fault type for long-descriptor MMU fault reporting; this corresponds
4825 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
4826 */
4827typedef enum {
4828 translation_fault = 1,
4829 access_fault = 2,
4830 permission_fault = 3,
4831} MMUFaultType;
4832
2c8dd318 4833static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,
3dde962f 4834 int access_type, int is_user,
a8170e5e 4835 hwaddr *phys_ptr, int *prot,
3dde962f
PM
4836 target_ulong *page_size_ptr)
4837{
70d74660 4838 CPUState *cs = CPU(arm_env_get_cpu(env));
3dde962f
PM
4839 /* Read an LPAE long-descriptor translation table. */
4840 MMUFaultType fault_type = translation_fault;
4841 uint32_t level = 1;
4842 uint32_t epd;
2c8dd318
RH
4843 int32_t tsz;
4844 uint32_t tg;
3dde962f
PM
4845 uint64_t ttbr;
4846 int ttbr_select;
2c8dd318 4847 hwaddr descaddr, descmask;
3dde962f
PM
4848 uint32_t tableattrs;
4849 target_ulong page_size;
4850 uint32_t attrs;
2c8dd318
RH
4851 int32_t granule_sz = 9;
4852 int32_t va_size = 32;
4853 int32_t tbi = 0;
11f136ee 4854 TCR *tcr = &env->cp15.tcr_el[arm_is_secure(env) ? 3 : 1];
2c8dd318
RH
4855
4856 if (arm_el_is_aa64(env, 1)) {
4857 va_size = 64;
4858 if (extract64(address, 55, 1))
11f136ee 4859 tbi = extract64(tcr->raw_tcr, 38, 1);
2c8dd318 4860 else
11f136ee 4861 tbi = extract64(tcr->raw_tcr, 37, 1);
2c8dd318
RH
4862 tbi *= 8;
4863 }
3dde962f
PM
4864
4865 /* Determine whether this address is in the region controlled by
4866 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
4867 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
4868 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
4869 */
11f136ee 4870 uint32_t t0sz = extract32(tcr->raw_tcr, 0, 6);
2c8dd318
RH
4871 if (arm_el_is_aa64(env, 1)) {
4872 t0sz = MIN(t0sz, 39);
4873 t0sz = MAX(t0sz, 16);
4874 }
11f136ee 4875 uint32_t t1sz = extract32(tcr->raw_tcr, 16, 6);
2c8dd318
RH
4876 if (arm_el_is_aa64(env, 1)) {
4877 t1sz = MIN(t1sz, 39);
4878 t1sz = MAX(t1sz, 16);
4879 }
4880 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
3dde962f
PM
4881 /* there is a ttbr0 region and we are in it (high bits all zero) */
4882 ttbr_select = 0;
2c8dd318 4883 } else if (t1sz && !extract64(~address, va_size - t1sz, t1sz - tbi)) {
3dde962f
PM
4884 /* there is a ttbr1 region and we are in it (high bits all one) */
4885 ttbr_select = 1;
4886 } else if (!t0sz) {
4887 /* ttbr0 region is "everything not in the ttbr1 region" */
4888 ttbr_select = 0;
4889 } else if (!t1sz) {
4890 /* ttbr1 region is "everything not in the ttbr0 region" */
4891 ttbr_select = 1;
4892 } else {
4893 /* in the gap between the two regions, this is a Translation fault */
4894 fault_type = translation_fault;
4895 goto do_fault;
4896 }
4897
4898 /* Note that QEMU ignores shareability and cacheability attributes,
4899 * so we don't need to do anything with the SH, ORGN, IRGN fields
4900 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
4901 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
4902 * implement any ASID-like capability so we can ignore it (instead
4903 * we will always flush the TLB any time the ASID is changed).
4904 */
4905 if (ttbr_select == 0) {
7dd8c9af 4906 ttbr = A32_BANKED_CURRENT_REG_GET(env, ttbr0);
11f136ee 4907 epd = extract32(tcr->raw_tcr, 7, 1);
3dde962f 4908 tsz = t0sz;
2c8dd318 4909
11f136ee 4910 tg = extract32(tcr->raw_tcr, 14, 2);
2c8dd318
RH
4911 if (tg == 1) { /* 64KB pages */
4912 granule_sz = 13;
4913 }
4914 if (tg == 2) { /* 16KB pages */
4915 granule_sz = 11;
4916 }
3dde962f 4917 } else {
7dd8c9af 4918 ttbr = A32_BANKED_CURRENT_REG_GET(env, ttbr1);
11f136ee 4919 epd = extract32(tcr->raw_tcr, 23, 1);
3dde962f 4920 tsz = t1sz;
2c8dd318 4921
11f136ee 4922 tg = extract32(tcr->raw_tcr, 30, 2);
2c8dd318
RH
4923 if (tg == 3) { /* 64KB pages */
4924 granule_sz = 13;
4925 }
4926 if (tg == 1) { /* 16KB pages */
4927 granule_sz = 11;
4928 }
3dde962f
PM
4929 }
4930
4931 if (epd) {
4932 /* Translation table walk disabled => Translation fault on TLB miss */
4933 goto do_fault;
4934 }
4935
d6be29e3
PM
4936 /* The starting level depends on the virtual address size (which can be
4937 * up to 48 bits) and the translation granule size. It indicates the number
4938 * of strides (granule_sz bits at a time) needed to consume the bits
4939 * of the input address. In the pseudocode this is:
4940 * level = 4 - RoundUp((inputsize - grainsize) / stride)
4941 * where their 'inputsize' is our 'va_size - tsz', 'grainsize' is
4942 * our 'granule_sz + 3' and 'stride' is our 'granule_sz'.
4943 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
4944 * = 4 - (va_size - tsz - granule_sz - 3 + granule_sz - 1) / granule_sz
4945 * = 4 - (va_size - tsz - 4) / granule_sz;
3dde962f 4946 */
d6be29e3 4947 level = 4 - (va_size - tsz - 4) / granule_sz;
3dde962f
PM
4948
4949 /* Clear the vaddr bits which aren't part of the within-region address,
4950 * so that we don't have to special case things when calculating the
4951 * first descriptor address.
4952 */
2c8dd318
RH
4953 if (tsz) {
4954 address &= (1ULL << (va_size - tsz)) - 1;
4955 }
4956
4957 descmask = (1ULL << (granule_sz + 3)) - 1;
3dde962f
PM
4958
4959 /* Now we can extract the actual base address from the TTBR */
2c8dd318
RH
4960 descaddr = extract64(ttbr, 0, 48);
4961 descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
3dde962f
PM
4962
4963 tableattrs = 0;
4964 for (;;) {
4965 uint64_t descriptor;
4966
2c8dd318
RH
4967 descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
4968 descaddr &= ~7ULL;
2c17449b 4969 descriptor = ldq_phys(cs->as, descaddr);
3dde962f
PM
4970 if (!(descriptor & 1) ||
4971 (!(descriptor & 2) && (level == 3))) {
4972 /* Invalid, or the Reserved level 3 encoding */
4973 goto do_fault;
4974 }
4975 descaddr = descriptor & 0xfffffff000ULL;
4976
4977 if ((descriptor & 2) && (level < 3)) {
4978 /* Table entry. The top five bits are attributes which may
4979 * propagate down through lower levels of the table (and
4980 * which are all arranged so that 0 means "no effect", so
4981 * we can gather them up by ORing in the bits at each level).
4982 */
4983 tableattrs |= extract64(descriptor, 59, 5);
4984 level++;
4985 continue;
4986 }
4987 /* Block entry at level 1 or 2, or page entry at level 3.
4988 * These are basically the same thing, although the number
4989 * of bits we pull in from the vaddr varies.
4990 */
5661ae6b 4991 page_size = (1ULL << ((granule_sz * (4 - level)) + 3));
3dde962f
PM
4992 descaddr |= (address & (page_size - 1));
4993 /* Extract attributes from the descriptor and merge with table attrs */
d615efac
IC
4994 attrs = extract64(descriptor, 2, 10)
4995 | (extract64(descriptor, 52, 12) << 10);
3dde962f
PM
4996 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
4997 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
4998 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
4999 * means "force PL1 access only", which means forcing AP[1] to 0.
5000 */
5001 if (extract32(tableattrs, 2, 1)) {
5002 attrs &= ~(1 << 4);
5003 }
5004 /* Since we're always in the Non-secure state, NSTable is ignored. */
5005 break;
5006 }
5007 /* Here descaddr is the final physical address, and attributes
5008 * are all in attrs.
5009 */
5010 fault_type = access_fault;
5011 if ((attrs & (1 << 8)) == 0) {
5012 /* Access flag */
5013 goto do_fault;
5014 }
5015 fault_type = permission_fault;
5016 if (is_user && !(attrs & (1 << 4))) {
5017 /* Unprivileged access not enabled */
5018 goto do_fault;
5019 }
5020 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d615efac
IC
5021 if ((arm_feature(env, ARM_FEATURE_V8) && is_user && (attrs & (1 << 12))) ||
5022 (!arm_feature(env, ARM_FEATURE_V8) && (attrs & (1 << 12))) ||
5023 (!is_user && (attrs & (1 << 11)))) {
5024 /* XN/UXN or PXN. Since we only implement EL0/EL1 we unconditionally
5025 * treat XN/UXN as UXN for v8.
5026 */
3dde962f
PM
5027 if (access_type == 2) {
5028 goto do_fault;
5029 }
5030 *prot &= ~PAGE_EXEC;
5031 }
5032 if (attrs & (1 << 5)) {
5033 /* Write access forbidden */
5034 if (access_type == 1) {
5035 goto do_fault;
5036 }
5037 *prot &= ~PAGE_WRITE;
5038 }
5039
5040 *phys_ptr = descaddr;
5041 *page_size_ptr = page_size;
5042 return 0;
5043
5044do_fault:
5045 /* Long-descriptor format IFSR/DFSR value */
5046 return (1 << 9) | (fault_type << 2) | level;
5047}
5048
77a71dd1
PM
5049static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
5050 int access_type, int is_user,
a8170e5e 5051 hwaddr *phys_ptr, int *prot)
9ee6e8bb
PB
5052{
5053 int n;
5054 uint32_t mask;
5055 uint32_t base;
5056
5057 *phys_ptr = address;
5058 for (n = 7; n >= 0; n--) {
5059 base = env->cp15.c6_region[n];
5060 if ((base & 1) == 0)
5061 continue;
5062 mask = 1 << ((base >> 1) & 0x1f);
5063 /* Keep this shift separate from the above to avoid an
5064 (undefined) << 32. */
5065 mask = (mask << 1) - 1;
5066 if (((base ^ address) & ~mask) == 0)
5067 break;
5068 }
5069 if (n < 0)
5070 return 2;
5071
5072 if (access_type == 2) {
7e09797c 5073 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 5074 } else {
7e09797c 5075 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
5076 }
5077 mask = (mask >> (n * 4)) & 0xf;
5078 switch (mask) {
5079 case 0:
5080 return 1;
5081 case 1:
5082 if (is_user)
5083 return 1;
5084 *prot = PAGE_READ | PAGE_WRITE;
5085 break;
5086 case 2:
5087 *prot = PAGE_READ;
5088 if (!is_user)
5089 *prot |= PAGE_WRITE;
5090 break;
5091 case 3:
5092 *prot = PAGE_READ | PAGE_WRITE;
5093 break;
5094 case 5:
5095 if (is_user)
5096 return 1;
5097 *prot = PAGE_READ;
5098 break;
5099 case 6:
5100 *prot = PAGE_READ;
5101 break;
5102 default:
5103 /* Bad permission. */
5104 return 1;
5105 }
3ad493fc 5106 *prot |= PAGE_EXEC;
9ee6e8bb
PB
5107 return 0;
5108}
5109
702a9357
PM
5110/* get_phys_addr - get the physical address for this virtual address
5111 *
5112 * Find the physical address corresponding to the given virtual address,
5113 * by doing a translation table walk on MMU based systems or using the
5114 * MPU state on MPU based systems.
5115 *
5116 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
5117 * prot and page_size are not filled in, and the return value provides
5118 * information on why the translation aborted, in the format of a
5119 * DFSR/IFSR fault register, with the following caveats:
5120 * * we honour the short vs long DFSR format differences.
5121 * * the WnR bit is never set (the caller must do this).
5122 * * for MPU based systems we don't bother to return a full FSR format
5123 * value.
5124 *
5125 * @env: CPUARMState
5126 * @address: virtual address to get physical address for
5127 * @access_type: 0 for read, 1 for write, 2 for execute
5128 * @is_user: 0 for privileged access, 1 for user
5129 * @phys_ptr: set to the physical address corresponding to the virtual address
5130 * @prot: set to the permissions for the page containing phys_ptr
5131 * @page_size: set to the size of the page containing phys_ptr
5132 */
2c8dd318 5133static inline int get_phys_addr(CPUARMState *env, target_ulong address,
9ee6e8bb 5134 int access_type, int is_user,
a8170e5e 5135 hwaddr *phys_ptr, int *prot,
d4c430a8 5136 target_ulong *page_size)
9ee6e8bb 5137{
137feaa9
FA
5138 /* This is not entirely correct as get_phys_addr() can also be called
5139 * from ats_write() for an address translation of a specific regime.
5140 */
5141 uint32_t sctlr = A32_BANKED_CURRENT_REG_GET(env, sctlr);
5142
9ee6e8bb 5143 /* Fast Context Switch Extension. */
54bf36ed
FA
5144 if (address < 0x02000000) {
5145 address += A32_BANKED_CURRENT_REG_GET(env, fcseidr);
5146 }
9ee6e8bb 5147
137feaa9 5148 if ((sctlr & SCTLR_M) == 0) {
9ee6e8bb
PB
5149 /* MMU/MPU disabled. */
5150 *phys_ptr = address;
3ad493fc 5151 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 5152 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb
PB
5153 return 0;
5154 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
d4c430a8 5155 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb
PB
5156 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
5157 prot);
3dde962f
PM
5158 } else if (extended_addresses_enabled(env)) {
5159 return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
5160 prot, page_size);
137feaa9 5161 } else if (sctlr & SCTLR_XP) {
9ee6e8bb 5162 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
d4c430a8 5163 prot, page_size);
9ee6e8bb
PB
5164 } else {
5165 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
d4c430a8 5166 prot, page_size);
9ee6e8bb
PB
5167 }
5168}
5169
7510454e
AF
5170int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
5171 int access_type, int mmu_idx)
b5ff1b31 5172{
7510454e
AF
5173 ARMCPU *cpu = ARM_CPU(cs);
5174 CPUARMState *env = &cpu->env;
a8170e5e 5175 hwaddr phys_addr;
d4c430a8 5176 target_ulong page_size;
b5ff1b31 5177 int prot;
6ebbf390 5178 int ret, is_user;
00892383 5179 uint32_t syn;
dcbff19b 5180 bool same_el = (arm_current_el(env) != 0);
b5ff1b31 5181
c1e37810
PM
5182 /* TODO: pass the translation regime to get_phys_addr */
5183 is_user = (arm_mmu_idx_to_el(mmu_idx) == 0);
d4c430a8
PB
5184 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
5185 &page_size);
b5ff1b31
FB
5186 if (ret == 0) {
5187 /* Map a single [sub]page. */
dcd82c11
AB
5188 phys_addr &= TARGET_PAGE_MASK;
5189 address &= TARGET_PAGE_MASK;
0c591eb0 5190 tlb_set_page(cs, address, phys_addr, prot, mmu_idx, page_size);
d4c430a8 5191 return 0;
b5ff1b31
FB
5192 }
5193
00892383
RH
5194 /* AArch64 syndrome does not have an LPAE bit */
5195 syn = ret & ~(1 << 9);
5196
5197 /* For insn and data aborts we assume there is no instruction syndrome
5198 * information; this is always true for exceptions reported to EL1.
5199 */
b5ff1b31 5200 if (access_type == 2) {
00892383 5201 syn = syn_insn_abort(same_el, 0, 0, syn);
27103424 5202 cs->exception_index = EXCP_PREFETCH_ABORT;
b5ff1b31 5203 } else {
00892383 5204 syn = syn_data_abort(same_el, 0, 0, 0, access_type == 1, syn);
abf1172f
PM
5205 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) {
5206 ret |= (1 << 11);
5207 }
27103424 5208 cs->exception_index = EXCP_DATA_ABORT;
b5ff1b31 5209 }
00892383
RH
5210
5211 env->exception.syndrome = syn;
abf1172f
PM
5212 env->exception.vaddress = address;
5213 env->exception.fsr = ret;
b5ff1b31
FB
5214 return 1;
5215}
5216
00b941e5 5217hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
b5ff1b31 5218{
00b941e5 5219 ARMCPU *cpu = ARM_CPU(cs);
a8170e5e 5220 hwaddr phys_addr;
d4c430a8 5221 target_ulong page_size;
b5ff1b31
FB
5222 int prot;
5223 int ret;
5224
00b941e5 5225 ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
b5ff1b31 5226
00b941e5 5227 if (ret != 0) {
b5ff1b31 5228 return -1;
00b941e5 5229 }
b5ff1b31
FB
5230
5231 return phys_addr;
5232}
5233
0ecb72a5 5234void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
9ee6e8bb 5235{
39ea3d4e
PM
5236 if ((env->uncached_cpsr & CPSR_M) == mode) {
5237 env->regs[13] = val;
5238 } else {
f5206413 5239 env->banked_r13[bank_number(mode)] = val;
39ea3d4e 5240 }
9ee6e8bb
PB
5241}
5242
0ecb72a5 5243uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
9ee6e8bb 5244{
39ea3d4e
PM
5245 if ((env->uncached_cpsr & CPSR_M) == mode) {
5246 return env->regs[13];
5247 } else {
f5206413 5248 return env->banked_r13[bank_number(mode)];
39ea3d4e 5249 }
9ee6e8bb
PB
5250}
5251
0ecb72a5 5252uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
9ee6e8bb 5253{
a47dddd7
AF
5254 ARMCPU *cpu = arm_env_get_cpu(env);
5255
9ee6e8bb
PB
5256 switch (reg) {
5257 case 0: /* APSR */
5258 return xpsr_read(env) & 0xf8000000;
5259 case 1: /* IAPSR */
5260 return xpsr_read(env) & 0xf80001ff;
5261 case 2: /* EAPSR */
5262 return xpsr_read(env) & 0xff00fc00;
5263 case 3: /* xPSR */
5264 return xpsr_read(env) & 0xff00fdff;
5265 case 5: /* IPSR */
5266 return xpsr_read(env) & 0x000001ff;
5267 case 6: /* EPSR */
5268 return xpsr_read(env) & 0x0700fc00;
5269 case 7: /* IEPSR */
5270 return xpsr_read(env) & 0x0700edff;
5271 case 8: /* MSP */
5272 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
5273 case 9: /* PSP */
5274 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
5275 case 16: /* PRIMASK */
4cc35614 5276 return (env->daif & PSTATE_I) != 0;
82845826
SH
5277 case 17: /* BASEPRI */
5278 case 18: /* BASEPRI_MAX */
9ee6e8bb 5279 return env->v7m.basepri;
82845826 5280 case 19: /* FAULTMASK */
4cc35614 5281 return (env->daif & PSTATE_F) != 0;
9ee6e8bb
PB
5282 case 20: /* CONTROL */
5283 return env->v7m.control;
5284 default:
5285 /* ??? For debugging only. */
a47dddd7 5286 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
9ee6e8bb
PB
5287 return 0;
5288 }
5289}
5290
0ecb72a5 5291void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
9ee6e8bb 5292{
a47dddd7
AF
5293 ARMCPU *cpu = arm_env_get_cpu(env);
5294
9ee6e8bb
PB
5295 switch (reg) {
5296 case 0: /* APSR */
5297 xpsr_write(env, val, 0xf8000000);
5298 break;
5299 case 1: /* IAPSR */
5300 xpsr_write(env, val, 0xf8000000);
5301 break;
5302 case 2: /* EAPSR */
5303 xpsr_write(env, val, 0xfe00fc00);
5304 break;
5305 case 3: /* xPSR */
5306 xpsr_write(env, val, 0xfe00fc00);
5307 break;
5308 case 5: /* IPSR */
5309 /* IPSR bits are readonly. */
5310 break;
5311 case 6: /* EPSR */
5312 xpsr_write(env, val, 0x0600fc00);
5313 break;
5314 case 7: /* IEPSR */
5315 xpsr_write(env, val, 0x0600fc00);
5316 break;
5317 case 8: /* MSP */
5318 if (env->v7m.current_sp)
5319 env->v7m.other_sp = val;
5320 else
5321 env->regs[13] = val;
5322 break;
5323 case 9: /* PSP */
5324 if (env->v7m.current_sp)
5325 env->regs[13] = val;
5326 else
5327 env->v7m.other_sp = val;
5328 break;
5329 case 16: /* PRIMASK */
4cc35614
PM
5330 if (val & 1) {
5331 env->daif |= PSTATE_I;
5332 } else {
5333 env->daif &= ~PSTATE_I;
5334 }
9ee6e8bb 5335 break;
82845826 5336 case 17: /* BASEPRI */
9ee6e8bb
PB
5337 env->v7m.basepri = val & 0xff;
5338 break;
82845826 5339 case 18: /* BASEPRI_MAX */
9ee6e8bb
PB
5340 val &= 0xff;
5341 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
5342 env->v7m.basepri = val;
5343 break;
82845826 5344 case 19: /* FAULTMASK */
4cc35614
PM
5345 if (val & 1) {
5346 env->daif |= PSTATE_F;
5347 } else {
5348 env->daif &= ~PSTATE_F;
5349 }
82845826 5350 break;
9ee6e8bb
PB
5351 case 20: /* CONTROL */
5352 env->v7m.control = val & 3;
5353 switch_v7m_sp(env, (val & 2) != 0);
5354 break;
5355 default:
5356 /* ??? For debugging only. */
a47dddd7 5357 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
9ee6e8bb
PB
5358 return;
5359 }
5360}
5361
b5ff1b31 5362#endif
6ddbc6e4 5363
aca3f40b
PM
5364void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
5365{
5366 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
5367 * Note that we do not implement the (architecturally mandated)
5368 * alignment fault for attempts to use this on Device memory
5369 * (which matches the usual QEMU behaviour of not implementing either
5370 * alignment faults or any memory attribute handling).
5371 */
5372
5373 ARMCPU *cpu = arm_env_get_cpu(env);
5374 uint64_t blocklen = 4 << cpu->dcz_blocksize;
5375 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
5376
5377#ifndef CONFIG_USER_ONLY
5378 {
5379 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
5380 * the block size so we might have to do more than one TLB lookup.
5381 * We know that in fact for any v8 CPU the page size is at least 4K
5382 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
5383 * 1K as an artefact of legacy v5 subpage support being present in the
5384 * same QEMU executable.
5385 */
5386 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
5387 void *hostaddr[maxidx];
5388 int try, i;
5389
5390 for (try = 0; try < 2; try++) {
5391
5392 for (i = 0; i < maxidx; i++) {
5393 hostaddr[i] = tlb_vaddr_to_host(env,
5394 vaddr + TARGET_PAGE_SIZE * i,
5395 1, cpu_mmu_index(env));
5396 if (!hostaddr[i]) {
5397 break;
5398 }
5399 }
5400 if (i == maxidx) {
5401 /* If it's all in the TLB it's fair game for just writing to;
5402 * we know we don't need to update dirty status, etc.
5403 */
5404 for (i = 0; i < maxidx - 1; i++) {
5405 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
5406 }
5407 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
5408 return;
5409 }
5410 /* OK, try a store and see if we can populate the tlb. This
5411 * might cause an exception if the memory isn't writable,
5412 * in which case we will longjmp out of here. We must for
5413 * this purpose use the actual register value passed to us
5414 * so that we get the fault address right.
5415 */
5416 helper_ret_stb_mmu(env, vaddr_in, 0, cpu_mmu_index(env), GETRA());
5417 /* Now we can populate the other TLB entries, if any */
5418 for (i = 0; i < maxidx; i++) {
5419 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
5420 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
5421 helper_ret_stb_mmu(env, va, 0, cpu_mmu_index(env), GETRA());
5422 }
5423 }
5424 }
5425
5426 /* Slow path (probably attempt to do this to an I/O device or
5427 * similar, or clearing of a block of code we have translations
5428 * cached for). Just do a series of byte writes as the architecture
5429 * demands. It's not worth trying to use a cpu_physical_memory_map(),
5430 * memset(), unmap() sequence here because:
5431 * + we'd need to account for the blocksize being larger than a page
5432 * + the direct-RAM access case is almost always going to be dealt
5433 * with in the fastpath code above, so there's no speed benefit
5434 * + we would have to deal with the map returning NULL because the
5435 * bounce buffer was in use
5436 */
5437 for (i = 0; i < blocklen; i++) {
5438 helper_ret_stb_mmu(env, vaddr + i, 0, cpu_mmu_index(env), GETRA());
5439 }
5440 }
5441#else
5442 memset(g2h(vaddr), 0, blocklen);
5443#endif
5444}
5445
6ddbc6e4
PB
5446/* Note that signed overflow is undefined in C. The following routines are
5447 careful to use unsigned types where modulo arithmetic is required.
5448 Failure to do so _will_ break on newer gcc. */
5449
5450/* Signed saturating arithmetic. */
5451
1654b2d6 5452/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
5453static inline uint16_t add16_sat(uint16_t a, uint16_t b)
5454{
5455 uint16_t res;
5456
5457 res = a + b;
5458 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
5459 if (a & 0x8000)
5460 res = 0x8000;
5461 else
5462 res = 0x7fff;
5463 }
5464 return res;
5465}
5466
1654b2d6 5467/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
5468static inline uint8_t add8_sat(uint8_t a, uint8_t b)
5469{
5470 uint8_t res;
5471
5472 res = a + b;
5473 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
5474 if (a & 0x80)
5475 res = 0x80;
5476 else
5477 res = 0x7f;
5478 }
5479 return res;
5480}
5481
1654b2d6 5482/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
5483static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
5484{
5485 uint16_t res;
5486
5487 res = a - b;
5488 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
5489 if (a & 0x8000)
5490 res = 0x8000;
5491 else
5492 res = 0x7fff;
5493 }
5494 return res;
5495}
5496
1654b2d6 5497/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
5498static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
5499{
5500 uint8_t res;
5501
5502 res = a - b;
5503 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
5504 if (a & 0x80)
5505 res = 0x80;
5506 else
5507 res = 0x7f;
5508 }
5509 return res;
5510}
5511
5512#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
5513#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
5514#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
5515#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
5516#define PFX q
5517
5518#include "op_addsub.h"
5519
5520/* Unsigned saturating arithmetic. */
460a09c1 5521static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
5522{
5523 uint16_t res;
5524 res = a + b;
5525 if (res < a)
5526 res = 0xffff;
5527 return res;
5528}
5529
460a09c1 5530static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 5531{
4c4fd3f8 5532 if (a > b)
6ddbc6e4
PB
5533 return a - b;
5534 else
5535 return 0;
5536}
5537
5538static inline uint8_t add8_usat(uint8_t a, uint8_t b)
5539{
5540 uint8_t res;
5541 res = a + b;
5542 if (res < a)
5543 res = 0xff;
5544 return res;
5545}
5546
5547static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
5548{
4c4fd3f8 5549 if (a > b)
6ddbc6e4
PB
5550 return a - b;
5551 else
5552 return 0;
5553}
5554
5555#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
5556#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
5557#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
5558#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
5559#define PFX uq
5560
5561#include "op_addsub.h"
5562
5563/* Signed modulo arithmetic. */
5564#define SARITH16(a, b, n, op) do { \
5565 int32_t sum; \
db6e2e65 5566 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
5567 RESULT(sum, n, 16); \
5568 if (sum >= 0) \
5569 ge |= 3 << (n * 2); \
5570 } while(0)
5571
5572#define SARITH8(a, b, n, op) do { \
5573 int32_t sum; \
db6e2e65 5574 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
5575 RESULT(sum, n, 8); \
5576 if (sum >= 0) \
5577 ge |= 1 << n; \
5578 } while(0)
5579
5580
5581#define ADD16(a, b, n) SARITH16(a, b, n, +)
5582#define SUB16(a, b, n) SARITH16(a, b, n, -)
5583#define ADD8(a, b, n) SARITH8(a, b, n, +)
5584#define SUB8(a, b, n) SARITH8(a, b, n, -)
5585#define PFX s
5586#define ARITH_GE
5587
5588#include "op_addsub.h"
5589
5590/* Unsigned modulo arithmetic. */
5591#define ADD16(a, b, n) do { \
5592 uint32_t sum; \
5593 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
5594 RESULT(sum, n, 16); \
a87aa10b 5595 if ((sum >> 16) == 1) \
6ddbc6e4
PB
5596 ge |= 3 << (n * 2); \
5597 } while(0)
5598
5599#define ADD8(a, b, n) do { \
5600 uint32_t sum; \
5601 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
5602 RESULT(sum, n, 8); \
a87aa10b
AZ
5603 if ((sum >> 8) == 1) \
5604 ge |= 1 << n; \
6ddbc6e4
PB
5605 } while(0)
5606
5607#define SUB16(a, b, n) do { \
5608 uint32_t sum; \
5609 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
5610 RESULT(sum, n, 16); \
5611 if ((sum >> 16) == 0) \
5612 ge |= 3 << (n * 2); \
5613 } while(0)
5614
5615#define SUB8(a, b, n) do { \
5616 uint32_t sum; \
5617 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
5618 RESULT(sum, n, 8); \
5619 if ((sum >> 8) == 0) \
a87aa10b 5620 ge |= 1 << n; \
6ddbc6e4
PB
5621 } while(0)
5622
5623#define PFX u
5624#define ARITH_GE
5625
5626#include "op_addsub.h"
5627
5628/* Halved signed arithmetic. */
5629#define ADD16(a, b, n) \
5630 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
5631#define SUB16(a, b, n) \
5632 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
5633#define ADD8(a, b, n) \
5634 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
5635#define SUB8(a, b, n) \
5636 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
5637#define PFX sh
5638
5639#include "op_addsub.h"
5640
5641/* Halved unsigned arithmetic. */
5642#define ADD16(a, b, n) \
5643 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
5644#define SUB16(a, b, n) \
5645 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
5646#define ADD8(a, b, n) \
5647 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
5648#define SUB8(a, b, n) \
5649 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
5650#define PFX uh
5651
5652#include "op_addsub.h"
5653
5654static inline uint8_t do_usad(uint8_t a, uint8_t b)
5655{
5656 if (a > b)
5657 return a - b;
5658 else
5659 return b - a;
5660}
5661
5662/* Unsigned sum of absolute byte differences. */
5663uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
5664{
5665 uint32_t sum;
5666 sum = do_usad(a, b);
5667 sum += do_usad(a >> 8, b >> 8);
5668 sum += do_usad(a >> 16, b >>16);
5669 sum += do_usad(a >> 24, b >> 24);
5670 return sum;
5671}
5672
5673/* For ARMv6 SEL instruction. */
5674uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
5675{
5676 uint32_t mask;
5677
5678 mask = 0;
5679 if (flags & 1)
5680 mask |= 0xff;
5681 if (flags & 2)
5682 mask |= 0xff00;
5683 if (flags & 4)
5684 mask |= 0xff0000;
5685 if (flags & 8)
5686 mask |= 0xff000000;
5687 return (a & mask) | (b & ~mask);
5688}
5689
b90372ad
PM
5690/* VFP support. We follow the convention used for VFP instructions:
5691 Single precision routines have a "s" suffix, double precision a
4373f3ce
PB
5692 "d" suffix. */
5693
5694/* Convert host exception flags to vfp form. */
5695static inline int vfp_exceptbits_from_host(int host_bits)
5696{
5697 int target_bits = 0;
5698
5699 if (host_bits & float_flag_invalid)
5700 target_bits |= 1;
5701 if (host_bits & float_flag_divbyzero)
5702 target_bits |= 2;
5703 if (host_bits & float_flag_overflow)
5704 target_bits |= 4;
36802b6b 5705 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4373f3ce
PB
5706 target_bits |= 8;
5707 if (host_bits & float_flag_inexact)
5708 target_bits |= 0x10;
cecd8504
PM
5709 if (host_bits & float_flag_input_denormal)
5710 target_bits |= 0x80;
4373f3ce
PB
5711 return target_bits;
5712}
5713
0ecb72a5 5714uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4373f3ce
PB
5715{
5716 int i;
5717 uint32_t fpscr;
5718
5719 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
5720 | (env->vfp.vec_len << 16)
5721 | (env->vfp.vec_stride << 20);
5722 i = get_float_exception_flags(&env->vfp.fp_status);
3a492f3a 5723 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4373f3ce
PB
5724 fpscr |= vfp_exceptbits_from_host(i);
5725 return fpscr;
5726}
5727
0ecb72a5 5728uint32_t vfp_get_fpscr(CPUARMState *env)
01653295
PM
5729{
5730 return HELPER(vfp_get_fpscr)(env);
5731}
5732
4373f3ce
PB
5733/* Convert vfp exception flags to target form. */
5734static inline int vfp_exceptbits_to_host(int target_bits)
5735{
5736 int host_bits = 0;
5737
5738 if (target_bits & 1)
5739 host_bits |= float_flag_invalid;
5740 if (target_bits & 2)
5741 host_bits |= float_flag_divbyzero;
5742 if (target_bits & 4)
5743 host_bits |= float_flag_overflow;
5744 if (target_bits & 8)
5745 host_bits |= float_flag_underflow;
5746 if (target_bits & 0x10)
5747 host_bits |= float_flag_inexact;
cecd8504
PM
5748 if (target_bits & 0x80)
5749 host_bits |= float_flag_input_denormal;
4373f3ce
PB
5750 return host_bits;
5751}
5752
0ecb72a5 5753void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4373f3ce
PB
5754{
5755 int i;
5756 uint32_t changed;
5757
5758 changed = env->vfp.xregs[ARM_VFP_FPSCR];
5759 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
5760 env->vfp.vec_len = (val >> 16) & 7;
5761 env->vfp.vec_stride = (val >> 20) & 3;
5762
5763 changed ^= val;
5764 if (changed & (3 << 22)) {
5765 i = (val >> 22) & 3;
5766 switch (i) {
4d3da0f3 5767 case FPROUNDING_TIEEVEN:
4373f3ce
PB
5768 i = float_round_nearest_even;
5769 break;
4d3da0f3 5770 case FPROUNDING_POSINF:
4373f3ce
PB
5771 i = float_round_up;
5772 break;
4d3da0f3 5773 case FPROUNDING_NEGINF:
4373f3ce
PB
5774 i = float_round_down;
5775 break;
4d3da0f3 5776 case FPROUNDING_ZERO:
4373f3ce
PB
5777 i = float_round_to_zero;
5778 break;
5779 }
5780 set_float_rounding_mode(i, &env->vfp.fp_status);
5781 }
cecd8504 5782 if (changed & (1 << 24)) {
fe76d976 5783 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
5784 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
5785 }
5c7908ed
PB
5786 if (changed & (1 << 25))
5787 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 5788
b12c390b 5789 i = vfp_exceptbits_to_host(val);
4373f3ce 5790 set_float_exception_flags(i, &env->vfp.fp_status);
3a492f3a 5791 set_float_exception_flags(0, &env->vfp.standard_fp_status);
4373f3ce
PB
5792}
5793
0ecb72a5 5794void vfp_set_fpscr(CPUARMState *env, uint32_t val)
01653295
PM
5795{
5796 HELPER(vfp_set_fpscr)(env, val);
5797}
5798
4373f3ce
PB
5799#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
5800
5801#define VFP_BINOP(name) \
ae1857ec 5802float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4373f3ce 5803{ \
ae1857ec
PM
5804 float_status *fpst = fpstp; \
5805 return float32_ ## name(a, b, fpst); \
4373f3ce 5806} \
ae1857ec 5807float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4373f3ce 5808{ \
ae1857ec
PM
5809 float_status *fpst = fpstp; \
5810 return float64_ ## name(a, b, fpst); \
4373f3ce
PB
5811}
5812VFP_BINOP(add)
5813VFP_BINOP(sub)
5814VFP_BINOP(mul)
5815VFP_BINOP(div)
f71a2ae5
PM
5816VFP_BINOP(min)
5817VFP_BINOP(max)
5818VFP_BINOP(minnum)
5819VFP_BINOP(maxnum)
4373f3ce
PB
5820#undef VFP_BINOP
5821
5822float32 VFP_HELPER(neg, s)(float32 a)
5823{
5824 return float32_chs(a);
5825}
5826
5827float64 VFP_HELPER(neg, d)(float64 a)
5828{
66230e0d 5829 return float64_chs(a);
4373f3ce
PB
5830}
5831
5832float32 VFP_HELPER(abs, s)(float32 a)
5833{
5834 return float32_abs(a);
5835}
5836
5837float64 VFP_HELPER(abs, d)(float64 a)
5838{
66230e0d 5839 return float64_abs(a);
4373f3ce
PB
5840}
5841
0ecb72a5 5842float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
4373f3ce
PB
5843{
5844 return float32_sqrt(a, &env->vfp.fp_status);
5845}
5846
0ecb72a5 5847float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
4373f3ce
PB
5848{
5849 return float64_sqrt(a, &env->vfp.fp_status);
5850}
5851
5852/* XXX: check quiet/signaling case */
5853#define DO_VFP_cmp(p, type) \
0ecb72a5 5854void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
5855{ \
5856 uint32_t flags; \
5857 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
5858 case 0: flags = 0x6; break; \
5859 case -1: flags = 0x8; break; \
5860 case 1: flags = 0x2; break; \
5861 default: case 2: flags = 0x3; break; \
5862 } \
5863 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
5864 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
5865} \
0ecb72a5 5866void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4373f3ce
PB
5867{ \
5868 uint32_t flags; \
5869 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
5870 case 0: flags = 0x6; break; \
5871 case -1: flags = 0x8; break; \
5872 case 1: flags = 0x2; break; \
5873 default: case 2: flags = 0x3; break; \
5874 } \
5875 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
5876 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
5877}
5878DO_VFP_cmp(s, float32)
5879DO_VFP_cmp(d, float64)
5880#undef DO_VFP_cmp
5881
5500b06c 5882/* Integer to float and float to integer conversions */
4373f3ce 5883
5500b06c
PM
5884#define CONV_ITOF(name, fsz, sign) \
5885 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
5886{ \
5887 float_status *fpst = fpstp; \
85836979 5888 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4373f3ce
PB
5889}
5890
5500b06c
PM
5891#define CONV_FTOI(name, fsz, sign, round) \
5892uint32_t HELPER(name)(float##fsz x, void *fpstp) \
5893{ \
5894 float_status *fpst = fpstp; \
5895 if (float##fsz##_is_any_nan(x)) { \
5896 float_raise(float_flag_invalid, fpst); \
5897 return 0; \
5898 } \
5899 return float##fsz##_to_##sign##int32##round(x, fpst); \
4373f3ce
PB
5900}
5901
5500b06c
PM
5902#define FLOAT_CONVS(name, p, fsz, sign) \
5903CONV_ITOF(vfp_##name##to##p, fsz, sign) \
5904CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
5905CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4373f3ce 5906
5500b06c
PM
5907FLOAT_CONVS(si, s, 32, )
5908FLOAT_CONVS(si, d, 64, )
5909FLOAT_CONVS(ui, s, 32, u)
5910FLOAT_CONVS(ui, d, 64, u)
4373f3ce 5911
5500b06c
PM
5912#undef CONV_ITOF
5913#undef CONV_FTOI
5914#undef FLOAT_CONVS
4373f3ce
PB
5915
5916/* floating point conversion */
0ecb72a5 5917float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
4373f3ce 5918{
2d627737
PM
5919 float64 r = float32_to_float64(x, &env->vfp.fp_status);
5920 /* ARM requires that S<->D conversion of any kind of NaN generates
5921 * a quiet NaN by forcing the most significant frac bit to 1.
5922 */
5923 return float64_maybe_silence_nan(r);
4373f3ce
PB
5924}
5925
0ecb72a5 5926float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
4373f3ce 5927{
2d627737
PM
5928 float32 r = float64_to_float32(x, &env->vfp.fp_status);
5929 /* ARM requires that S<->D conversion of any kind of NaN generates
5930 * a quiet NaN by forcing the most significant frac bit to 1.
5931 */
5932 return float32_maybe_silence_nan(r);
4373f3ce
PB
5933}
5934
5935/* VFP3 fixed point conversion. */
16d5b3ca 5936#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8ed697e8
WN
5937float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
5938 void *fpstp) \
4373f3ce 5939{ \
5500b06c 5940 float_status *fpst = fpstp; \
622465e1 5941 float##fsz tmp; \
8ed697e8 5942 tmp = itype##_to_##float##fsz(x, fpst); \
5500b06c 5943 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
16d5b3ca
WN
5944}
5945
abe66f70
PM
5946/* Notice that we want only input-denormal exception flags from the
5947 * scalbn operation: the other possible flags (overflow+inexact if
5948 * we overflow to infinity, output-denormal) aren't correct for the
5949 * complete scale-and-convert operation.
5950 */
16d5b3ca
WN
5951#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
5952uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
5953 uint32_t shift, \
5954 void *fpstp) \
4373f3ce 5955{ \
5500b06c 5956 float_status *fpst = fpstp; \
abe66f70 5957 int old_exc_flags = get_float_exception_flags(fpst); \
622465e1
PM
5958 float##fsz tmp; \
5959 if (float##fsz##_is_any_nan(x)) { \
5500b06c 5960 float_raise(float_flag_invalid, fpst); \
622465e1 5961 return 0; \
09d9487f 5962 } \
5500b06c 5963 tmp = float##fsz##_scalbn(x, shift, fpst); \
abe66f70
PM
5964 old_exc_flags |= get_float_exception_flags(fpst) \
5965 & float_flag_input_denormal; \
5966 set_float_exception_flags(old_exc_flags, fpst); \
16d5b3ca 5967 return float##fsz##_to_##itype##round(tmp, fpst); \
622465e1
PM
5968}
5969
16d5b3ca
WN
5970#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
5971VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3c6a074a
WN
5972VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
5973VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
5974
5975#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
5976VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
5977VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
16d5b3ca 5978
8ed697e8
WN
5979VFP_CONV_FIX(sh, d, 64, 64, int16)
5980VFP_CONV_FIX(sl, d, 64, 64, int32)
3c6a074a 5981VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8ed697e8
WN
5982VFP_CONV_FIX(uh, d, 64, 64, uint16)
5983VFP_CONV_FIX(ul, d, 64, 64, uint32)
3c6a074a 5984VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8ed697e8
WN
5985VFP_CONV_FIX(sh, s, 32, 32, int16)
5986VFP_CONV_FIX(sl, s, 32, 32, int32)
3c6a074a 5987VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8ed697e8
WN
5988VFP_CONV_FIX(uh, s, 32, 32, uint16)
5989VFP_CONV_FIX(ul, s, 32, 32, uint32)
3c6a074a 5990VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
4373f3ce 5991#undef VFP_CONV_FIX
16d5b3ca
WN
5992#undef VFP_CONV_FIX_FLOAT
5993#undef VFP_CONV_FLOAT_FIX_ROUND
4373f3ce 5994
52a1f6a3
AG
5995/* Set the current fp rounding mode and return the old one.
5996 * The argument is a softfloat float_round_ value.
5997 */
5998uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
5999{
6000 float_status *fp_status = &env->vfp.fp_status;
6001
6002 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
6003 set_float_rounding_mode(rmode, fp_status);
6004
6005 return prev_rmode;
6006}
6007
43630e58
WN
6008/* Set the current fp rounding mode in the standard fp status and return
6009 * the old one. This is for NEON instructions that need to change the
6010 * rounding mode but wish to use the standard FPSCR values for everything
6011 * else. Always set the rounding mode back to the correct value after
6012 * modifying it.
6013 * The argument is a softfloat float_round_ value.
6014 */
6015uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
6016{
6017 float_status *fp_status = &env->vfp.standard_fp_status;
6018
6019 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
6020 set_float_rounding_mode(rmode, fp_status);
6021
6022 return prev_rmode;
6023}
6024
60011498 6025/* Half precision conversions. */
0ecb72a5 6026static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
60011498 6027{
60011498 6028 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
6029 float32 r = float16_to_float32(make_float16(a), ieee, s);
6030 if (ieee) {
6031 return float32_maybe_silence_nan(r);
6032 }
6033 return r;
60011498
PB
6034}
6035
0ecb72a5 6036static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
60011498 6037{
60011498 6038 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
fb91678d
PM
6039 float16 r = float32_to_float16(a, ieee, s);
6040 if (ieee) {
6041 r = float16_maybe_silence_nan(r);
6042 }
6043 return float16_val(r);
60011498
PB
6044}
6045
0ecb72a5 6046float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
6047{
6048 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
6049}
6050
0ecb72a5 6051uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
6052{
6053 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
6054}
6055
0ecb72a5 6056float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2d981da7
PM
6057{
6058 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
6059}
6060
0ecb72a5 6061uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2d981da7
PM
6062{
6063 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
6064}
6065
8900aad2
PM
6066float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
6067{
6068 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
6069 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
6070 if (ieee) {
6071 return float64_maybe_silence_nan(r);
6072 }
6073 return r;
6074}
6075
6076uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
6077{
6078 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
6079 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
6080 if (ieee) {
6081 r = float16_maybe_silence_nan(r);
6082 }
6083 return float16_val(r);
6084}
6085
dda3ec49 6086#define float32_two make_float32(0x40000000)
6aae3df1
PM
6087#define float32_three make_float32(0x40400000)
6088#define float32_one_point_five make_float32(0x3fc00000)
dda3ec49 6089
0ecb72a5 6090float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 6091{
dda3ec49
PM
6092 float_status *s = &env->vfp.standard_fp_status;
6093 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
6094 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
6095 if (!(float32_is_zero(a) || float32_is_zero(b))) {
6096 float_raise(float_flag_input_denormal, s);
6097 }
dda3ec49
PM
6098 return float32_two;
6099 }
6100 return float32_sub(float32_two, float32_mul(a, b, s), s);
4373f3ce
PB
6101}
6102
0ecb72a5 6103float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4373f3ce 6104{
71826966 6105 float_status *s = &env->vfp.standard_fp_status;
9ea62f57
PM
6106 float32 product;
6107 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
6108 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
43fe9bdb
PM
6109 if (!(float32_is_zero(a) || float32_is_zero(b))) {
6110 float_raise(float_flag_input_denormal, s);
6111 }
6aae3df1 6112 return float32_one_point_five;
9ea62f57 6113 }
6aae3df1
PM
6114 product = float32_mul(a, b, s);
6115 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4373f3ce
PB
6116}
6117
8f8e3aa4
PB
6118/* NEON helpers. */
6119
56bf4fe2
CL
6120/* Constants 256 and 512 are used in some helpers; we avoid relying on
6121 * int->float conversions at run-time. */
6122#define float64_256 make_float64(0x4070000000000000LL)
6123#define float64_512 make_float64(0x4080000000000000LL)
b6d4443a
AB
6124#define float32_maxnorm make_float32(0x7f7fffff)
6125#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
56bf4fe2 6126
b6d4443a
AB
6127/* Reciprocal functions
6128 *
6129 * The algorithm that must be used to calculate the estimate
6130 * is specified by the ARM ARM, see FPRecipEstimate()
fe0e4872 6131 */
b6d4443a
AB
6132
6133static float64 recip_estimate(float64 a, float_status *real_fp_status)
fe0e4872 6134{
1146a817
PM
6135 /* These calculations mustn't set any fp exception flags,
6136 * so we use a local copy of the fp_status.
6137 */
b6d4443a 6138 float_status dummy_status = *real_fp_status;
1146a817 6139 float_status *s = &dummy_status;
fe0e4872
CL
6140 /* q = (int)(a * 512.0) */
6141 float64 q = float64_mul(float64_512, a, s);
6142 int64_t q_int = float64_to_int64_round_to_zero(q, s);
6143
6144 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
6145 q = int64_to_float64(q_int, s);
6146 q = float64_add(q, float64_half, s);
6147 q = float64_div(q, float64_512, s);
6148 q = float64_div(float64_one, q, s);
6149
6150 /* s = (int)(256.0 * r + 0.5) */
6151 q = float64_mul(q, float64_256, s);
6152 q = float64_add(q, float64_half, s);
6153 q_int = float64_to_int64_round_to_zero(q, s);
6154
6155 /* return (double)s / 256.0 */
6156 return float64_div(int64_to_float64(q_int, s), float64_256, s);
6157}
6158
b6d4443a
AB
6159/* Common wrapper to call recip_estimate */
6160static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
4373f3ce 6161{
b6d4443a
AB
6162 uint64_t val64 = float64_val(num);
6163 uint64_t frac = extract64(val64, 0, 52);
6164 int64_t exp = extract64(val64, 52, 11);
6165 uint64_t sbit;
6166 float64 scaled, estimate;
fe0e4872 6167
b6d4443a
AB
6168 /* Generate the scaled number for the estimate function */
6169 if (exp == 0) {
6170 if (extract64(frac, 51, 1) == 0) {
6171 exp = -1;
6172 frac = extract64(frac, 0, 50) << 2;
6173 } else {
6174 frac = extract64(frac, 0, 51) << 1;
6175 }
6176 }
fe0e4872 6177
b6d4443a
AB
6178 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
6179 scaled = make_float64((0x3feULL << 52)
6180 | extract64(frac, 44, 8) << 44);
6181
6182 estimate = recip_estimate(scaled, fpst);
6183
6184 /* Build new result */
6185 val64 = float64_val(estimate);
6186 sbit = 0x8000000000000000ULL & val64;
6187 exp = off - exp;
6188 frac = extract64(val64, 0, 52);
6189
6190 if (exp == 0) {
6191 frac = 1ULL << 51 | extract64(frac, 1, 51);
6192 } else if (exp == -1) {
6193 frac = 1ULL << 50 | extract64(frac, 2, 50);
6194 exp = 0;
6195 }
6196
6197 return make_float64(sbit | (exp << 52) | frac);
6198}
6199
6200static bool round_to_inf(float_status *fpst, bool sign_bit)
6201{
6202 switch (fpst->float_rounding_mode) {
6203 case float_round_nearest_even: /* Round to Nearest */
6204 return true;
6205 case float_round_up: /* Round to +Inf */
6206 return !sign_bit;
6207 case float_round_down: /* Round to -Inf */
6208 return sign_bit;
6209 case float_round_to_zero: /* Round to Zero */
6210 return false;
6211 }
6212
6213 g_assert_not_reached();
6214}
6215
6216float32 HELPER(recpe_f32)(float32 input, void *fpstp)
6217{
6218 float_status *fpst = fpstp;
6219 float32 f32 = float32_squash_input_denormal(input, fpst);
6220 uint32_t f32_val = float32_val(f32);
6221 uint32_t f32_sbit = 0x80000000ULL & f32_val;
6222 int32_t f32_exp = extract32(f32_val, 23, 8);
6223 uint32_t f32_frac = extract32(f32_val, 0, 23);
6224 float64 f64, r64;
6225 uint64_t r64_val;
6226 int64_t r64_exp;
6227 uint64_t r64_frac;
6228
6229 if (float32_is_any_nan(f32)) {
6230 float32 nan = f32;
6231 if (float32_is_signaling_nan(f32)) {
6232 float_raise(float_flag_invalid, fpst);
6233 nan = float32_maybe_silence_nan(f32);
fe0e4872 6234 }
b6d4443a
AB
6235 if (fpst->default_nan_mode) {
6236 nan = float32_default_nan;
43fe9bdb 6237 }
b6d4443a
AB
6238 return nan;
6239 } else if (float32_is_infinity(f32)) {
6240 return float32_set_sign(float32_zero, float32_is_neg(f32));
6241 } else if (float32_is_zero(f32)) {
6242 float_raise(float_flag_divbyzero, fpst);
6243 return float32_set_sign(float32_infinity, float32_is_neg(f32));
6244 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
6245 /* Abs(value) < 2.0^-128 */
6246 float_raise(float_flag_overflow | float_flag_inexact, fpst);
6247 if (round_to_inf(fpst, f32_sbit)) {
6248 return float32_set_sign(float32_infinity, float32_is_neg(f32));
6249 } else {
6250 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
6251 }
6252 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
6253 float_raise(float_flag_underflow, fpst);
6254 return float32_set_sign(float32_zero, float32_is_neg(f32));
fe0e4872
CL
6255 }
6256
fe0e4872 6257
b6d4443a
AB
6258 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
6259 r64 = call_recip_estimate(f64, 253, fpst);
6260 r64_val = float64_val(r64);
6261 r64_exp = extract64(r64_val, 52, 11);
6262 r64_frac = extract64(r64_val, 0, 52);
6263
6264 /* result = sign : result_exp<7:0> : fraction<51:29>; */
6265 return make_float32(f32_sbit |
6266 (r64_exp & 0xff) << 23 |
6267 extract64(r64_frac, 29, 24));
6268}
6269
6270float64 HELPER(recpe_f64)(float64 input, void *fpstp)
6271{
6272 float_status *fpst = fpstp;
6273 float64 f64 = float64_squash_input_denormal(input, fpst);
6274 uint64_t f64_val = float64_val(f64);
6275 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
6276 int64_t f64_exp = extract64(f64_val, 52, 11);
6277 float64 r64;
6278 uint64_t r64_val;
6279 int64_t r64_exp;
6280 uint64_t r64_frac;
6281
6282 /* Deal with any special cases */
6283 if (float64_is_any_nan(f64)) {
6284 float64 nan = f64;
6285 if (float64_is_signaling_nan(f64)) {
6286 float_raise(float_flag_invalid, fpst);
6287 nan = float64_maybe_silence_nan(f64);
6288 }
6289 if (fpst->default_nan_mode) {
6290 nan = float64_default_nan;
6291 }
6292 return nan;
6293 } else if (float64_is_infinity(f64)) {
6294 return float64_set_sign(float64_zero, float64_is_neg(f64));
6295 } else if (float64_is_zero(f64)) {
6296 float_raise(float_flag_divbyzero, fpst);
6297 return float64_set_sign(float64_infinity, float64_is_neg(f64));
6298 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
6299 /* Abs(value) < 2.0^-1024 */
6300 float_raise(float_flag_overflow | float_flag_inexact, fpst);
6301 if (round_to_inf(fpst, f64_sbit)) {
6302 return float64_set_sign(float64_infinity, float64_is_neg(f64));
6303 } else {
6304 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
6305 }
6306 } else if (f64_exp >= 1023 && fpst->flush_to_zero) {
6307 float_raise(float_flag_underflow, fpst);
6308 return float64_set_sign(float64_zero, float64_is_neg(f64));
6309 }
fe0e4872 6310
b6d4443a
AB
6311 r64 = call_recip_estimate(f64, 2045, fpst);
6312 r64_val = float64_val(r64);
6313 r64_exp = extract64(r64_val, 52, 11);
6314 r64_frac = extract64(r64_val, 0, 52);
fe0e4872 6315
b6d4443a
AB
6316 /* result = sign : result_exp<10:0> : fraction<51:0> */
6317 return make_float64(f64_sbit |
6318 ((r64_exp & 0x7ff) << 52) |
6319 r64_frac);
4373f3ce
PB
6320}
6321
e07be5d2
CL
6322/* The algorithm that must be used to calculate the estimate
6323 * is specified by the ARM ARM.
6324 */
c2fb418e 6325static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
e07be5d2 6326{
1146a817
PM
6327 /* These calculations mustn't set any fp exception flags,
6328 * so we use a local copy of the fp_status.
6329 */
c2fb418e 6330 float_status dummy_status = *real_fp_status;
1146a817 6331 float_status *s = &dummy_status;
e07be5d2
CL
6332 float64 q;
6333 int64_t q_int;
6334
6335 if (float64_lt(a, float64_half, s)) {
6336 /* range 0.25 <= a < 0.5 */
6337
6338 /* a in units of 1/512 rounded down */
6339 /* q0 = (int)(a * 512.0); */
6340 q = float64_mul(float64_512, a, s);
6341 q_int = float64_to_int64_round_to_zero(q, s);
6342
6343 /* reciprocal root r */
6344 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
6345 q = int64_to_float64(q_int, s);
6346 q = float64_add(q, float64_half, s);
6347 q = float64_div(q, float64_512, s);
6348 q = float64_sqrt(q, s);
6349 q = float64_div(float64_one, q, s);
6350 } else {
6351 /* range 0.5 <= a < 1.0 */
6352
6353 /* a in units of 1/256 rounded down */
6354 /* q1 = (int)(a * 256.0); */
6355 q = float64_mul(float64_256, a, s);
6356 int64_t q_int = float64_to_int64_round_to_zero(q, s);
6357
6358 /* reciprocal root r */
6359 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
6360 q = int64_to_float64(q_int, s);
6361 q = float64_add(q, float64_half, s);
6362 q = float64_div(q, float64_256, s);
6363 q = float64_sqrt(q, s);
6364 q = float64_div(float64_one, q, s);
6365 }
6366 /* r in units of 1/256 rounded to nearest */
6367 /* s = (int)(256.0 * r + 0.5); */
6368
6369 q = float64_mul(q, float64_256,s );
6370 q = float64_add(q, float64_half, s);
6371 q_int = float64_to_int64_round_to_zero(q, s);
6372
6373 /* return (double)s / 256.0;*/
6374 return float64_div(int64_to_float64(q_int, s), float64_256, s);
6375}
6376
c2fb418e 6377float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4373f3ce 6378{
c2fb418e
AB
6379 float_status *s = fpstp;
6380 float32 f32 = float32_squash_input_denormal(input, s);
6381 uint32_t val = float32_val(f32);
6382 uint32_t f32_sbit = 0x80000000 & val;
6383 int32_t f32_exp = extract32(val, 23, 8);
6384 uint32_t f32_frac = extract32(val, 0, 23);
6385 uint64_t f64_frac;
6386 uint64_t val64;
e07be5d2
CL
6387 int result_exp;
6388 float64 f64;
e07be5d2 6389
c2fb418e
AB
6390 if (float32_is_any_nan(f32)) {
6391 float32 nan = f32;
6392 if (float32_is_signaling_nan(f32)) {
e07be5d2 6393 float_raise(float_flag_invalid, s);
c2fb418e 6394 nan = float32_maybe_silence_nan(f32);
e07be5d2 6395 }
c2fb418e
AB
6396 if (s->default_nan_mode) {
6397 nan = float32_default_nan;
43fe9bdb 6398 }
c2fb418e
AB
6399 return nan;
6400 } else if (float32_is_zero(f32)) {
e07be5d2 6401 float_raise(float_flag_divbyzero, s);
c2fb418e
AB
6402 return float32_set_sign(float32_infinity, float32_is_neg(f32));
6403 } else if (float32_is_neg(f32)) {
e07be5d2
CL
6404 float_raise(float_flag_invalid, s);
6405 return float32_default_nan;
c2fb418e 6406 } else if (float32_is_infinity(f32)) {
e07be5d2
CL
6407 return float32_zero;
6408 }
6409
c2fb418e 6410 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
e07be5d2 6411 * preserving the parity of the exponent. */
c2fb418e
AB
6412
6413 f64_frac = ((uint64_t) f32_frac) << 29;
6414 if (f32_exp == 0) {
6415 while (extract64(f64_frac, 51, 1) == 0) {
6416 f64_frac = f64_frac << 1;
6417 f32_exp = f32_exp-1;
6418 }
6419 f64_frac = extract64(f64_frac, 0, 51) << 1;
6420 }
6421
6422 if (extract64(f32_exp, 0, 1) == 0) {
6423 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 6424 | (0x3feULL << 52)
c2fb418e 6425 | f64_frac);
e07be5d2 6426 } else {
c2fb418e 6427 f64 = make_float64(((uint64_t) f32_sbit) << 32
e07be5d2 6428 | (0x3fdULL << 52)
c2fb418e 6429 | f64_frac);
e07be5d2
CL
6430 }
6431
c2fb418e 6432 result_exp = (380 - f32_exp) / 2;
e07be5d2 6433
c2fb418e 6434 f64 = recip_sqrt_estimate(f64, s);
e07be5d2
CL
6435
6436 val64 = float64_val(f64);
6437
26cc6abf 6438 val = ((result_exp & 0xff) << 23)
e07be5d2
CL
6439 | ((val64 >> 29) & 0x7fffff);
6440 return make_float32(val);
4373f3ce
PB
6441}
6442
c2fb418e
AB
6443float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
6444{
6445 float_status *s = fpstp;
6446 float64 f64 = float64_squash_input_denormal(input, s);
6447 uint64_t val = float64_val(f64);
6448 uint64_t f64_sbit = 0x8000000000000000ULL & val;
6449 int64_t f64_exp = extract64(val, 52, 11);
6450 uint64_t f64_frac = extract64(val, 0, 52);
6451 int64_t result_exp;
6452 uint64_t result_frac;
6453
6454 if (float64_is_any_nan(f64)) {
6455 float64 nan = f64;
6456 if (float64_is_signaling_nan(f64)) {
6457 float_raise(float_flag_invalid, s);
6458 nan = float64_maybe_silence_nan(f64);
6459 }
6460 if (s->default_nan_mode) {
6461 nan = float64_default_nan;
6462 }
6463 return nan;
6464 } else if (float64_is_zero(f64)) {
6465 float_raise(float_flag_divbyzero, s);
6466 return float64_set_sign(float64_infinity, float64_is_neg(f64));
6467 } else if (float64_is_neg(f64)) {
6468 float_raise(float_flag_invalid, s);
6469 return float64_default_nan;
6470 } else if (float64_is_infinity(f64)) {
6471 return float64_zero;
6472 }
6473
6474 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
6475 * preserving the parity of the exponent. */
6476
6477 if (f64_exp == 0) {
6478 while (extract64(f64_frac, 51, 1) == 0) {
6479 f64_frac = f64_frac << 1;
6480 f64_exp = f64_exp - 1;
6481 }
6482 f64_frac = extract64(f64_frac, 0, 51) << 1;
6483 }
6484
6485 if (extract64(f64_exp, 0, 1) == 0) {
6486 f64 = make_float64(f64_sbit
6487 | (0x3feULL << 52)
6488 | f64_frac);
6489 } else {
6490 f64 = make_float64(f64_sbit
6491 | (0x3fdULL << 52)
6492 | f64_frac);
6493 }
6494
6495 result_exp = (3068 - f64_exp) / 2;
6496
6497 f64 = recip_sqrt_estimate(f64, s);
6498
6499 result_frac = extract64(float64_val(f64), 0, 52);
6500
6501 return make_float64(f64_sbit |
6502 ((result_exp & 0x7ff) << 52) |
6503 result_frac);
6504}
6505
b6d4443a 6506uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
4373f3ce 6507{
b6d4443a 6508 float_status *s = fpstp;
fe0e4872
CL
6509 float64 f64;
6510
6511 if ((a & 0x80000000) == 0) {
6512 return 0xffffffff;
6513 }
6514
6515 f64 = make_float64((0x3feULL << 52)
6516 | ((int64_t)(a & 0x7fffffff) << 21));
6517
b6d4443a 6518 f64 = recip_estimate(f64, s);
fe0e4872
CL
6519
6520 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce
PB
6521}
6522
c2fb418e 6523uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4373f3ce 6524{
c2fb418e 6525 float_status *fpst = fpstp;
e07be5d2
CL
6526 float64 f64;
6527
6528 if ((a & 0xc0000000) == 0) {
6529 return 0xffffffff;
6530 }
6531
6532 if (a & 0x80000000) {
6533 f64 = make_float64((0x3feULL << 52)
6534 | ((uint64_t)(a & 0x7fffffff) << 21));
6535 } else { /* bits 31-30 == '01' */
6536 f64 = make_float64((0x3fdULL << 52)
6537 | ((uint64_t)(a & 0x3fffffff) << 22));
6538 }
6539
c2fb418e 6540 f64 = recip_sqrt_estimate(f64, fpst);
e07be5d2
CL
6541
6542 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4373f3ce 6543}
fe1479c3 6544
da97f52c
PM
6545/* VFPv4 fused multiply-accumulate */
6546float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
6547{
6548 float_status *fpst = fpstp;
6549 return float32_muladd(a, b, c, 0, fpst);
6550}
6551
6552float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
6553{
6554 float_status *fpst = fpstp;
6555 return float64_muladd(a, b, c, 0, fpst);
6556}
d9b0848d
PM
6557
6558/* ARMv8 round to integral */
6559float32 HELPER(rints_exact)(float32 x, void *fp_status)
6560{
6561 return float32_round_to_int(x, fp_status);
6562}
6563
6564float64 HELPER(rintd_exact)(float64 x, void *fp_status)
6565{
6566 return float64_round_to_int(x, fp_status);
6567}
6568
6569float32 HELPER(rints)(float32 x, void *fp_status)
6570{
6571 int old_flags = get_float_exception_flags(fp_status), new_flags;
6572 float32 ret;
6573
6574 ret = float32_round_to_int(x, fp_status);
6575
6576 /* Suppress any inexact exceptions the conversion produced */
6577 if (!(old_flags & float_flag_inexact)) {
6578 new_flags = get_float_exception_flags(fp_status);
6579 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
6580 }
6581
6582 return ret;
6583}
6584
6585float64 HELPER(rintd)(float64 x, void *fp_status)
6586{
6587 int old_flags = get_float_exception_flags(fp_status), new_flags;
6588 float64 ret;
6589
6590 ret = float64_round_to_int(x, fp_status);
6591
6592 new_flags = get_float_exception_flags(fp_status);
6593
6594 /* Suppress any inexact exceptions the conversion produced */
6595 if (!(old_flags & float_flag_inexact)) {
6596 new_flags = get_float_exception_flags(fp_status);
6597 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
6598 }
6599
6600 return ret;
6601}
9972da66
WN
6602
6603/* Convert ARM rounding mode to softfloat */
6604int arm_rmode_to_sf(int rmode)
6605{
6606 switch (rmode) {
6607 case FPROUNDING_TIEAWAY:
6608 rmode = float_round_ties_away;
6609 break;
6610 case FPROUNDING_ODD:
6611 /* FIXME: add support for TIEAWAY and ODD */
6612 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
6613 rmode);
6614 case FPROUNDING_TIEEVEN:
6615 default:
6616 rmode = float_round_nearest_even;
6617 break;
6618 case FPROUNDING_POSINF:
6619 rmode = float_round_up;
6620 break;
6621 case FPROUNDING_NEGINF:
6622 rmode = float_round_down;
6623 break;
6624 case FPROUNDING_ZERO:
6625 rmode = float_round_to_zero;
6626 break;
6627 }
6628 return rmode;
6629}
eb0ecd5a 6630
aa633469
PM
6631/* CRC helpers.
6632 * The upper bytes of val (above the number specified by 'bytes') must have
6633 * been zeroed out by the caller.
6634 */
eb0ecd5a
WN
6635uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
6636{
6637 uint8_t buf[4];
6638
aa633469 6639 stl_le_p(buf, val);
eb0ecd5a
WN
6640
6641 /* zlib crc32 converts the accumulator and output to one's complement. */
6642 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
6643}
6644
6645uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
6646{
6647 uint8_t buf[4];
6648
aa633469 6649 stl_le_p(buf, val);
eb0ecd5a
WN
6650
6651 /* Linux crc32c converts the output to one's complement. */
6652 return crc32c(acc, buf, bytes) ^ 0xffffffff;
6653}