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