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