2 #include "exec/gdbstub.h"
4 #include "qemu/host-utils.h"
5 #include "sysemu/arch_init.h"
6 #include "sysemu/sysemu.h"
7 #include "qemu/bitops.h"
8 #include "qemu/crc32c.h"
9 #include <zlib.h> /* For crc32 */
11 #ifndef CONFIG_USER_ONLY
12 static inline int get_phys_addr(CPUARMState
*env
, uint32_t address
,
13 int access_type
, int is_user
,
14 hwaddr
*phys_ptr
, int *prot
,
15 target_ulong
*page_size
);
17 /* Definitions for the PMCCNTR and PMCR registers */
23 static int vfp_gdb_get_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
27 /* VFP data registers are always little-endian. */
28 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
30 stfq_le_p(buf
, env
->vfp
.regs
[reg
]);
33 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
34 /* Aliases for Q regs. */
37 stfq_le_p(buf
, env
->vfp
.regs
[(reg
- 32) * 2]);
38 stfq_le_p(buf
+ 8, env
->vfp
.regs
[(reg
- 32) * 2 + 1]);
42 switch (reg
- nregs
) {
43 case 0: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]); return 4;
44 case 1: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSCR
]); return 4;
45 case 2: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]); return 4;
50 static int vfp_gdb_set_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
54 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
56 env
->vfp
.regs
[reg
] = ldfq_le_p(buf
);
59 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
62 env
->vfp
.regs
[(reg
- 32) * 2] = ldfq_le_p(buf
);
63 env
->vfp
.regs
[(reg
- 32) * 2 + 1] = ldfq_le_p(buf
+ 8);
67 switch (reg
- nregs
) {
68 case 0: env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
); return 4;
69 case 1: env
->vfp
.xregs
[ARM_VFP_FPSCR
] = ldl_p(buf
); return 4;
70 case 2: env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30); return 4;
75 static int aarch64_fpu_gdb_get_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
79 /* 128 bit FP register */
80 stfq_le_p(buf
, env
->vfp
.regs
[reg
* 2]);
81 stfq_le_p(buf
+ 8, env
->vfp
.regs
[reg
* 2 + 1]);
85 stl_p(buf
, vfp_get_fpsr(env
));
89 stl_p(buf
, vfp_get_fpcr(env
));
96 static int aarch64_fpu_gdb_set_reg(CPUARMState
*env
, uint8_t *buf
, int reg
)
100 /* 128 bit FP register */
101 env
->vfp
.regs
[reg
* 2] = ldfq_le_p(buf
);
102 env
->vfp
.regs
[reg
* 2 + 1] = ldfq_le_p(buf
+ 8);
106 vfp_set_fpsr(env
, ldl_p(buf
));
110 vfp_set_fpcr(env
, ldl_p(buf
));
117 static uint64_t raw_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
119 if (cpreg_field_is_64bit(ri
)) {
120 return CPREG_FIELD64(env
, ri
);
122 return CPREG_FIELD32(env
, ri
);
126 static void raw_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
129 if (cpreg_field_is_64bit(ri
)) {
130 CPREG_FIELD64(env
, ri
) = value
;
132 CPREG_FIELD32(env
, ri
) = value
;
136 static uint64_t read_raw_cp_reg(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
138 /* Raw read of a coprocessor register (as needed for migration, etc). */
139 if (ri
->type
& ARM_CP_CONST
) {
140 return ri
->resetvalue
;
141 } else if (ri
->raw_readfn
) {
142 return ri
->raw_readfn(env
, ri
);
143 } else if (ri
->readfn
) {
144 return ri
->readfn(env
, ri
);
146 return raw_read(env
, ri
);
150 static void write_raw_cp_reg(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
153 /* Raw write of a coprocessor register (as needed for migration, etc).
154 * Note that constant registers are treated as write-ignored; the
155 * caller should check for success by whether a readback gives the
158 if (ri
->type
& ARM_CP_CONST
) {
160 } else if (ri
->raw_writefn
) {
161 ri
->raw_writefn(env
, ri
, v
);
162 } else if (ri
->writefn
) {
163 ri
->writefn(env
, ri
, v
);
165 raw_write(env
, ri
, v
);
169 bool write_cpustate_to_list(ARMCPU
*cpu
)
171 /* Write the coprocessor state from cpu->env to the (index,value) list. */
175 for (i
= 0; i
< cpu
->cpreg_array_len
; i
++) {
176 uint32_t regidx
= kvm_to_cpreg_id(cpu
->cpreg_indexes
[i
]);
177 const ARMCPRegInfo
*ri
;
179 ri
= get_arm_cp_reginfo(cpu
->cp_regs
, regidx
);
184 if (ri
->type
& ARM_CP_NO_MIGRATE
) {
187 cpu
->cpreg_values
[i
] = read_raw_cp_reg(&cpu
->env
, ri
);
192 bool write_list_to_cpustate(ARMCPU
*cpu
)
197 for (i
= 0; i
< cpu
->cpreg_array_len
; i
++) {
198 uint32_t regidx
= kvm_to_cpreg_id(cpu
->cpreg_indexes
[i
]);
199 uint64_t v
= cpu
->cpreg_values
[i
];
200 const ARMCPRegInfo
*ri
;
202 ri
= get_arm_cp_reginfo(cpu
->cp_regs
, regidx
);
207 if (ri
->type
& ARM_CP_NO_MIGRATE
) {
210 /* Write value and confirm it reads back as written
211 * (to catch read-only registers and partially read-only
212 * registers where the incoming migration value doesn't match)
214 write_raw_cp_reg(&cpu
->env
, ri
, v
);
215 if (read_raw_cp_reg(&cpu
->env
, ri
) != v
) {
222 static void add_cpreg_to_list(gpointer key
, gpointer opaque
)
224 ARMCPU
*cpu
= opaque
;
226 const ARMCPRegInfo
*ri
;
228 regidx
= *(uint32_t *)key
;
229 ri
= get_arm_cp_reginfo(cpu
->cp_regs
, regidx
);
231 if (!(ri
->type
& ARM_CP_NO_MIGRATE
)) {
232 cpu
->cpreg_indexes
[cpu
->cpreg_array_len
] = cpreg_to_kvm_id(regidx
);
233 /* The value array need not be initialized at this point */
234 cpu
->cpreg_array_len
++;
238 static void count_cpreg(gpointer key
, gpointer opaque
)
240 ARMCPU
*cpu
= opaque
;
242 const ARMCPRegInfo
*ri
;
244 regidx
= *(uint32_t *)key
;
245 ri
= get_arm_cp_reginfo(cpu
->cp_regs
, regidx
);
247 if (!(ri
->type
& ARM_CP_NO_MIGRATE
)) {
248 cpu
->cpreg_array_len
++;
252 static gint
cpreg_key_compare(gconstpointer a
, gconstpointer b
)
254 uint64_t aidx
= cpreg_to_kvm_id(*(uint32_t *)a
);
255 uint64_t bidx
= cpreg_to_kvm_id(*(uint32_t *)b
);
266 static void cpreg_make_keylist(gpointer key
, gpointer value
, gpointer udata
)
268 GList
**plist
= udata
;
270 *plist
= g_list_prepend(*plist
, key
);
273 void init_cpreg_list(ARMCPU
*cpu
)
275 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
276 * Note that we require cpreg_tuples[] to be sorted by key ID.
281 g_hash_table_foreach(cpu
->cp_regs
, cpreg_make_keylist
, &keys
);
283 keys
= g_list_sort(keys
, cpreg_key_compare
);
285 cpu
->cpreg_array_len
= 0;
287 g_list_foreach(keys
, count_cpreg
, cpu
);
289 arraylen
= cpu
->cpreg_array_len
;
290 cpu
->cpreg_indexes
= g_new(uint64_t, arraylen
);
291 cpu
->cpreg_values
= g_new(uint64_t, arraylen
);
292 cpu
->cpreg_vmstate_indexes
= g_new(uint64_t, arraylen
);
293 cpu
->cpreg_vmstate_values
= g_new(uint64_t, arraylen
);
294 cpu
->cpreg_vmstate_array_len
= cpu
->cpreg_array_len
;
295 cpu
->cpreg_array_len
= 0;
297 g_list_foreach(keys
, add_cpreg_to_list
, cpu
);
299 assert(cpu
->cpreg_array_len
== arraylen
);
304 static void dacr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
, uint64_t value
)
306 ARMCPU
*cpu
= arm_env_get_cpu(env
);
308 env
->cp15
.c3
= value
;
309 tlb_flush(CPU(cpu
), 1); /* Flush TLB as domain not tracked in TLB */
312 static void fcse_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
, uint64_t value
)
314 ARMCPU
*cpu
= arm_env_get_cpu(env
);
316 if (env
->cp15
.c13_fcse
!= value
) {
317 /* Unlike real hardware the qemu TLB uses virtual addresses,
318 * not modified virtual addresses, so this causes a TLB flush.
320 tlb_flush(CPU(cpu
), 1);
321 env
->cp15
.c13_fcse
= value
;
325 static void contextidr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
328 ARMCPU
*cpu
= arm_env_get_cpu(env
);
330 if (env
->cp15
.c13_context
!= value
&& !arm_feature(env
, ARM_FEATURE_MPU
)) {
331 /* For VMSA (when not using the LPAE long descriptor page table
332 * format) this register includes the ASID, so do a TLB flush.
333 * For PMSA it is purely a process ID and no action is needed.
335 tlb_flush(CPU(cpu
), 1);
337 env
->cp15
.c13_context
= value
;
340 static void tlbiall_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
343 /* Invalidate all (TLBIALL) */
344 ARMCPU
*cpu
= arm_env_get_cpu(env
);
346 tlb_flush(CPU(cpu
), 1);
349 static void tlbimva_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
352 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
353 ARMCPU
*cpu
= arm_env_get_cpu(env
);
355 tlb_flush_page(CPU(cpu
), value
& TARGET_PAGE_MASK
);
358 static void tlbiasid_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
361 /* Invalidate by ASID (TLBIASID) */
362 ARMCPU
*cpu
= arm_env_get_cpu(env
);
364 tlb_flush(CPU(cpu
), value
== 0);
367 static void tlbimvaa_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
370 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
371 ARMCPU
*cpu
= arm_env_get_cpu(env
);
373 tlb_flush_page(CPU(cpu
), value
& TARGET_PAGE_MASK
);
376 static const ARMCPRegInfo cp_reginfo
[] = {
377 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
378 * version" bits will read as a reserved value, which should cause
379 * Linux to not try to use the debug hardware.
381 { .name
= "DBGDIDR", .cp
= 14, .crn
= 0, .crm
= 0, .opc1
= 0, .opc2
= 0,
382 .access
= PL0_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
383 /* MMU Domain access control / MPU write buffer control */
384 { .name
= "DACR", .cp
= 15,
385 .crn
= 3, .crm
= CP_ANY
, .opc1
= CP_ANY
, .opc2
= CP_ANY
,
386 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c3
),
387 .resetvalue
= 0, .writefn
= dacr_write
, .raw_writefn
= raw_write
, },
388 { .name
= "FCSEIDR", .cp
= 15, .crn
= 13, .crm
= 0, .opc1
= 0, .opc2
= 0,
389 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c13_fcse
),
390 .resetvalue
= 0, .writefn
= fcse_write
, .raw_writefn
= raw_write
, },
391 { .name
= "CONTEXTIDR", .cp
= 15, .crn
= 13, .crm
= 0, .opc1
= 0, .opc2
= 1,
392 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c13_context
),
393 .resetvalue
= 0, .writefn
= contextidr_write
, .raw_writefn
= raw_write
, },
394 /* ??? This covers not just the impdef TLB lockdown registers but also
395 * some v7VMSA registers relating to TEX remap, so it is overly broad.
397 { .name
= "TLB_LOCKDOWN", .cp
= 15, .crn
= 10, .crm
= CP_ANY
,
398 .opc1
= CP_ANY
, .opc2
= CP_ANY
, .access
= PL1_RW
, .type
= ARM_CP_NOP
},
399 /* MMU TLB control. Note that the wildcarding means we cover not just
400 * the unified TLB ops but also the dside/iside/inner-shareable variants.
402 { .name
= "TLBIALL", .cp
= 15, .crn
= 8, .crm
= CP_ANY
,
403 .opc1
= CP_ANY
, .opc2
= 0, .access
= PL1_W
, .writefn
= tlbiall_write
,
404 .type
= ARM_CP_NO_MIGRATE
},
405 { .name
= "TLBIMVA", .cp
= 15, .crn
= 8, .crm
= CP_ANY
,
406 .opc1
= CP_ANY
, .opc2
= 1, .access
= PL1_W
, .writefn
= tlbimva_write
,
407 .type
= ARM_CP_NO_MIGRATE
},
408 { .name
= "TLBIASID", .cp
= 15, .crn
= 8, .crm
= CP_ANY
,
409 .opc1
= CP_ANY
, .opc2
= 2, .access
= PL1_W
, .writefn
= tlbiasid_write
,
410 .type
= ARM_CP_NO_MIGRATE
},
411 { .name
= "TLBIMVAA", .cp
= 15, .crn
= 8, .crm
= CP_ANY
,
412 .opc1
= CP_ANY
, .opc2
= 3, .access
= PL1_W
, .writefn
= tlbimvaa_write
,
413 .type
= ARM_CP_NO_MIGRATE
},
414 /* Cache maintenance ops; some of this space may be overridden later. */
415 { .name
= "CACHEMAINT", .cp
= 15, .crn
= 7, .crm
= CP_ANY
,
416 .opc1
= 0, .opc2
= CP_ANY
, .access
= PL1_W
,
417 .type
= ARM_CP_NOP
| ARM_CP_OVERRIDE
},
421 static const ARMCPRegInfo not_v6_cp_reginfo
[] = {
422 /* Not all pre-v6 cores implemented this WFI, so this is slightly
425 { .name
= "WFI_v5", .cp
= 15, .crn
= 7, .crm
= 8, .opc1
= 0, .opc2
= 2,
426 .access
= PL1_W
, .type
= ARM_CP_WFI
},
430 static const ARMCPRegInfo not_v7_cp_reginfo
[] = {
431 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
432 * is UNPREDICTABLE; we choose to NOP as most implementations do).
434 { .name
= "WFI_v6", .cp
= 15, .crn
= 7, .crm
= 0, .opc1
= 0, .opc2
= 4,
435 .access
= PL1_W
, .type
= ARM_CP_WFI
},
436 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
437 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
438 * OMAPCP will override this space.
440 { .name
= "DLOCKDOWN", .cp
= 15, .crn
= 9, .crm
= 0, .opc1
= 0, .opc2
= 0,
441 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_data
),
443 { .name
= "ILOCKDOWN", .cp
= 15, .crn
= 9, .crm
= 0, .opc1
= 0, .opc2
= 1,
444 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_insn
),
446 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
447 { .name
= "DUMMY", .cp
= 15, .crn
= 0, .crm
= 0, .opc1
= 1, .opc2
= CP_ANY
,
448 .access
= PL1_R
, .type
= ARM_CP_CONST
| ARM_CP_NO_MIGRATE
,
453 static void cpacr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
456 if (env
->cp15
.c1_coproc
!= value
) {
457 env
->cp15
.c1_coproc
= value
;
458 /* ??? Is this safe when called from within a TB? */
463 static const ARMCPRegInfo v6_cp_reginfo
[] = {
464 /* prefetch by MVA in v6, NOP in v7 */
465 { .name
= "MVA_prefetch",
466 .cp
= 15, .crn
= 7, .crm
= 13, .opc1
= 0, .opc2
= 1,
467 .access
= PL1_W
, .type
= ARM_CP_NOP
},
468 { .name
= "ISB", .cp
= 15, .crn
= 7, .crm
= 5, .opc1
= 0, .opc2
= 4,
469 .access
= PL0_W
, .type
= ARM_CP_NOP
},
470 { .name
= "DSB", .cp
= 15, .crn
= 7, .crm
= 10, .opc1
= 0, .opc2
= 4,
471 .access
= PL0_W
, .type
= ARM_CP_NOP
},
472 { .name
= "DMB", .cp
= 15, .crn
= 7, .crm
= 10, .opc1
= 0, .opc2
= 5,
473 .access
= PL0_W
, .type
= ARM_CP_NOP
},
474 { .name
= "IFAR", .cp
= 15, .crn
= 6, .crm
= 0, .opc1
= 0, .opc2
= 2,
475 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_insn
),
477 /* Watchpoint Fault Address Register : should actually only be present
478 * for 1136, 1176, 11MPCore.
480 { .name
= "WFAR", .cp
= 15, .crn
= 6, .crm
= 0, .opc1
= 0, .opc2
= 1,
481 .access
= PL1_RW
, .type
= ARM_CP_CONST
, .resetvalue
= 0, },
482 { .name
= "CPACR", .state
= ARM_CP_STATE_BOTH
, .opc0
= 3,
483 .crn
= 1, .crm
= 0, .opc1
= 0, .opc2
= 2,
484 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c1_coproc
),
485 .resetvalue
= 0, .writefn
= cpacr_write
},
489 static CPAccessResult
pmreg_access(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
491 /* Performance monitor registers user accessibility is controlled
494 if (arm_current_pl(env
) == 0 && !env
->cp15
.c9_pmuserenr
) {
495 return CP_ACCESS_TRAP
;
500 #ifndef CONFIG_USER_ONLY
501 static void pmcr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
504 /* Don't computer the number of ticks in user mode */
507 temp_ticks
= qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
) *
508 get_ticks_per_sec() / 1000000;
510 if (env
->cp15
.c9_pmcr
& PMCRE
) {
511 /* If the counter is enabled */
512 if (env
->cp15
.c9_pmcr
& PMCRD
) {
513 /* Increment once every 64 processor clock cycles */
514 env
->cp15
.c15_ccnt
= (temp_ticks
/64) - env
->cp15
.c15_ccnt
;
516 env
->cp15
.c15_ccnt
= temp_ticks
- env
->cp15
.c15_ccnt
;
521 /* The counter has been reset */
522 env
->cp15
.c15_ccnt
= 0;
525 /* only the DP, X, D and E bits are writable */
526 env
->cp15
.c9_pmcr
&= ~0x39;
527 env
->cp15
.c9_pmcr
|= (value
& 0x39);
529 if (env
->cp15
.c9_pmcr
& PMCRE
) {
530 if (env
->cp15
.c9_pmcr
& PMCRD
) {
531 /* Increment once every 64 processor clock cycles */
534 env
->cp15
.c15_ccnt
= temp_ticks
- env
->cp15
.c15_ccnt
;
538 static uint64_t pmccntr_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
540 uint32_t total_ticks
;
542 if (!(env
->cp15
.c9_pmcr
& PMCRE
)) {
543 /* Counter is disabled, do not change value */
544 return env
->cp15
.c15_ccnt
;
547 total_ticks
= qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
) *
548 get_ticks_per_sec() / 1000000;
550 if (env
->cp15
.c9_pmcr
& PMCRD
) {
551 /* Increment once every 64 processor clock cycles */
554 return total_ticks
- env
->cp15
.c15_ccnt
;
557 static void pmccntr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
560 uint32_t total_ticks
;
562 if (!(env
->cp15
.c9_pmcr
& PMCRE
)) {
563 /* Counter is disabled, set the absolute value */
564 env
->cp15
.c15_ccnt
= value
;
568 total_ticks
= qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
) *
569 get_ticks_per_sec() / 1000000;
571 if (env
->cp15
.c9_pmcr
& PMCRD
) {
572 /* Increment once every 64 processor clock cycles */
575 env
->cp15
.c15_ccnt
= total_ticks
- value
;
579 static void pmcntenset_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
583 env
->cp15
.c9_pmcnten
|= value
;
586 static void pmcntenclr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
590 env
->cp15
.c9_pmcnten
&= ~value
;
593 static void pmovsr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
596 env
->cp15
.c9_pmovsr
&= ~value
;
599 static void pmxevtyper_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
602 env
->cp15
.c9_pmxevtyper
= value
& 0xff;
605 static void pmuserenr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
608 env
->cp15
.c9_pmuserenr
= value
& 1;
611 static void pmintenset_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
614 /* We have no event counters so only the C bit can be changed */
616 env
->cp15
.c9_pminten
|= value
;
619 static void pmintenclr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
623 env
->cp15
.c9_pminten
&= ~value
;
626 static void vbar_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
629 /* Note that even though the AArch64 view of this register has bits
630 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
631 * architectural requirements for bits which are RES0 only in some
632 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
633 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
635 env
->cp15
.c12_vbar
= value
& ~0x1Ful
;
638 static uint64_t ccsidr_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
640 ARMCPU
*cpu
= arm_env_get_cpu(env
);
641 return cpu
->ccsidr
[env
->cp15
.c0_cssel
];
644 static void csselr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
647 env
->cp15
.c0_cssel
= value
& 0xf;
650 static const ARMCPRegInfo v7_cp_reginfo
[] = {
651 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
654 { .name
= "DBGDRAR", .cp
= 14, .crn
= 1, .crm
= 0, .opc1
= 0, .opc2
= 0,
655 .access
= PL0_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
656 { .name
= "DBGDSAR", .cp
= 14, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 0,
657 .access
= PL0_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
658 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
659 { .name
= "NOP", .cp
= 15, .crn
= 7, .crm
= 0, .opc1
= 0, .opc2
= 4,
660 .access
= PL1_W
, .type
= ARM_CP_NOP
},
661 /* Performance monitors are implementation defined in v7,
662 * but with an ARM recommended set of registers, which we
663 * follow (although we don't actually implement any counters)
665 * Performance registers fall into three categories:
666 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
667 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
668 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
669 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
670 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
672 { .name
= "PMCNTENSET", .cp
= 15, .crn
= 9, .crm
= 12, .opc1
= 0, .opc2
= 1,
673 .access
= PL0_RW
, .resetvalue
= 0,
674 .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pmcnten
),
675 .writefn
= pmcntenset_write
,
676 .accessfn
= pmreg_access
,
677 .raw_writefn
= raw_write
},
678 { .name
= "PMCNTENCLR", .cp
= 15, .crn
= 9, .crm
= 12, .opc1
= 0, .opc2
= 2,
679 .access
= PL0_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pmcnten
),
680 .accessfn
= pmreg_access
,
681 .writefn
= pmcntenclr_write
,
682 .type
= ARM_CP_NO_MIGRATE
},
683 { .name
= "PMOVSR", .cp
= 15, .crn
= 9, .crm
= 12, .opc1
= 0, .opc2
= 3,
684 .access
= PL0_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pmovsr
),
685 .accessfn
= pmreg_access
,
686 .writefn
= pmovsr_write
,
687 .raw_writefn
= raw_write
},
688 /* Unimplemented so WI. */
689 { .name
= "PMSWINC", .cp
= 15, .crn
= 9, .crm
= 12, .opc1
= 0, .opc2
= 4,
690 .access
= PL0_W
, .accessfn
= pmreg_access
, .type
= ARM_CP_NOP
},
691 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
692 * We choose to RAZ/WI.
694 { .name
= "PMSELR", .cp
= 15, .crn
= 9, .crm
= 12, .opc1
= 0, .opc2
= 5,
695 .access
= PL0_RW
, .type
= ARM_CP_CONST
, .resetvalue
= 0,
696 .accessfn
= pmreg_access
},
697 #ifndef CONFIG_USER_ONLY
698 { .name
= "PMCCNTR", .cp
= 15, .crn
= 9, .crm
= 13, .opc1
= 0, .opc2
= 0,
699 .access
= PL0_RW
, .resetvalue
= 0, .type
= ARM_CP_IO
,
700 .readfn
= pmccntr_read
, .writefn
= pmccntr_write
,
701 .accessfn
= pmreg_access
},
703 { .name
= "PMXEVTYPER", .cp
= 15, .crn
= 9, .crm
= 13, .opc1
= 0, .opc2
= 1,
705 .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pmxevtyper
),
706 .accessfn
= pmreg_access
, .writefn
= pmxevtyper_write
,
707 .raw_writefn
= raw_write
},
708 /* Unimplemented, RAZ/WI. */
709 { .name
= "PMXEVCNTR", .cp
= 15, .crn
= 9, .crm
= 13, .opc1
= 0, .opc2
= 2,
710 .access
= PL0_RW
, .type
= ARM_CP_CONST
, .resetvalue
= 0,
711 .accessfn
= pmreg_access
},
712 { .name
= "PMUSERENR", .cp
= 15, .crn
= 9, .crm
= 14, .opc1
= 0, .opc2
= 0,
713 .access
= PL0_R
| PL1_RW
,
714 .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pmuserenr
),
716 .writefn
= pmuserenr_write
, .raw_writefn
= raw_write
},
717 { .name
= "PMINTENSET", .cp
= 15, .crn
= 9, .crm
= 14, .opc1
= 0, .opc2
= 1,
719 .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pminten
),
721 .writefn
= pmintenset_write
, .raw_writefn
= raw_write
},
722 { .name
= "PMINTENCLR", .cp
= 15, .crn
= 9, .crm
= 14, .opc1
= 0, .opc2
= 2,
723 .access
= PL1_RW
, .type
= ARM_CP_NO_MIGRATE
,
724 .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pminten
),
725 .resetvalue
= 0, .writefn
= pmintenclr_write
, },
726 { .name
= "VBAR", .state
= ARM_CP_STATE_BOTH
,
727 .opc0
= 3, .crn
= 12, .crm
= 0, .opc1
= 0, .opc2
= 0,
728 .access
= PL1_RW
, .writefn
= vbar_write
,
729 .fieldoffset
= offsetof(CPUARMState
, cp15
.c12_vbar
),
731 { .name
= "SCR", .cp
= 15, .crn
= 1, .crm
= 1, .opc1
= 0, .opc2
= 0,
732 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c1_scr
),
734 { .name
= "CCSIDR", .state
= ARM_CP_STATE_BOTH
,
735 .opc0
= 3, .crn
= 0, .crm
= 0, .opc1
= 1, .opc2
= 0,
736 .access
= PL1_R
, .readfn
= ccsidr_read
, .type
= ARM_CP_NO_MIGRATE
},
737 { .name
= "CSSELR", .state
= ARM_CP_STATE_BOTH
,
738 .opc0
= 3, .crn
= 0, .crm
= 0, .opc1
= 2, .opc2
= 0,
739 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c0_cssel
),
740 .writefn
= csselr_write
, .resetvalue
= 0 },
741 /* Auxiliary ID register: this actually has an IMPDEF value but for now
742 * just RAZ for all cores:
744 { .name
= "AIDR", .cp
= 15, .crn
= 0, .crm
= 0, .opc1
= 1, .opc2
= 7,
745 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
746 /* MAIR can just read-as-written because we don't implement caches
747 * and so don't need to care about memory attributes.
749 { .name
= "MAIR_EL1", .state
= ARM_CP_STATE_AA64
,
750 .opc0
= 3, .opc1
= 0, .crn
= 10, .crm
= 2, .opc2
= 0,
751 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.mair_el1
),
753 /* For non-long-descriptor page tables these are PRRR and NMRR;
754 * regardless they still act as reads-as-written for QEMU.
755 * The override is necessary because of the overly-broad TLB_LOCKDOWN
758 { .name
= "MAIR0", .state
= ARM_CP_STATE_AA32
, .type
= ARM_CP_OVERRIDE
,
759 .cp
= 15, .opc1
= 0, .crn
= 10, .crm
= 2, .opc2
= 0, .access
= PL1_RW
,
760 .fieldoffset
= offsetoflow32(CPUARMState
, cp15
.mair_el1
),
761 .resetfn
= arm_cp_reset_ignore
},
762 { .name
= "MAIR1", .state
= ARM_CP_STATE_AA32
, .type
= ARM_CP_OVERRIDE
,
763 .cp
= 15, .opc1
= 0, .crn
= 10, .crm
= 2, .opc2
= 1, .access
= PL1_RW
,
764 .fieldoffset
= offsetofhigh32(CPUARMState
, cp15
.mair_el1
),
765 .resetfn
= arm_cp_reset_ignore
},
769 static void teecr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
776 static CPAccessResult
teehbr_access(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
778 if (arm_current_pl(env
) == 0 && (env
->teecr
& 1)) {
779 return CP_ACCESS_TRAP
;
784 static const ARMCPRegInfo t2ee_cp_reginfo
[] = {
785 { .name
= "TEECR", .cp
= 14, .crn
= 0, .crm
= 0, .opc1
= 6, .opc2
= 0,
786 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, teecr
),
788 .writefn
= teecr_write
},
789 { .name
= "TEEHBR", .cp
= 14, .crn
= 1, .crm
= 0, .opc1
= 6, .opc2
= 0,
790 .access
= PL0_RW
, .fieldoffset
= offsetof(CPUARMState
, teehbr
),
791 .accessfn
= teehbr_access
, .resetvalue
= 0 },
795 static const ARMCPRegInfo v6k_cp_reginfo
[] = {
796 { .name
= "TPIDR_EL0", .state
= ARM_CP_STATE_AA64
,
797 .opc0
= 3, .opc1
= 3, .opc2
= 2, .crn
= 13, .crm
= 0,
799 .fieldoffset
= offsetof(CPUARMState
, cp15
.tpidr_el0
), .resetvalue
= 0 },
800 { .name
= "TPIDRURW", .cp
= 15, .crn
= 13, .crm
= 0, .opc1
= 0, .opc2
= 2,
802 .fieldoffset
= offsetoflow32(CPUARMState
, cp15
.tpidr_el0
),
803 .resetfn
= arm_cp_reset_ignore
},
804 { .name
= "TPIDRRO_EL0", .state
= ARM_CP_STATE_AA64
,
805 .opc0
= 3, .opc1
= 3, .opc2
= 3, .crn
= 13, .crm
= 0,
806 .access
= PL0_R
|PL1_W
,
807 .fieldoffset
= offsetof(CPUARMState
, cp15
.tpidrro_el0
), .resetvalue
= 0 },
808 { .name
= "TPIDRURO", .cp
= 15, .crn
= 13, .crm
= 0, .opc1
= 0, .opc2
= 3,
809 .access
= PL0_R
|PL1_W
,
810 .fieldoffset
= offsetoflow32(CPUARMState
, cp15
.tpidrro_el0
),
811 .resetfn
= arm_cp_reset_ignore
},
812 { .name
= "TPIDR_EL1", .state
= ARM_CP_STATE_BOTH
,
813 .opc0
= 3, .opc1
= 0, .opc2
= 4, .crn
= 13, .crm
= 0,
815 .fieldoffset
= offsetof(CPUARMState
, cp15
.tpidr_el1
), .resetvalue
= 0 },
819 #ifndef CONFIG_USER_ONLY
821 static CPAccessResult
gt_cntfrq_access(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
823 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
824 if (arm_current_pl(env
) == 0 && !extract32(env
->cp15
.c14_cntkctl
, 0, 2)) {
825 return CP_ACCESS_TRAP
;
830 static CPAccessResult
gt_counter_access(CPUARMState
*env
, int timeridx
)
832 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
833 if (arm_current_pl(env
) == 0 &&
834 !extract32(env
->cp15
.c14_cntkctl
, timeridx
, 1)) {
835 return CP_ACCESS_TRAP
;
840 static CPAccessResult
gt_timer_access(CPUARMState
*env
, int timeridx
)
842 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
843 * EL0[PV]TEN is zero.
845 if (arm_current_pl(env
) == 0 &&
846 !extract32(env
->cp15
.c14_cntkctl
, 9 - timeridx
, 1)) {
847 return CP_ACCESS_TRAP
;
852 static CPAccessResult
gt_pct_access(CPUARMState
*env
,
853 const ARMCPRegInfo
*ri
)
855 return gt_counter_access(env
, GTIMER_PHYS
);
858 static CPAccessResult
gt_vct_access(CPUARMState
*env
,
859 const ARMCPRegInfo
*ri
)
861 return gt_counter_access(env
, GTIMER_VIRT
);
864 static CPAccessResult
gt_ptimer_access(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
866 return gt_timer_access(env
, GTIMER_PHYS
);
869 static CPAccessResult
gt_vtimer_access(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
871 return gt_timer_access(env
, GTIMER_VIRT
);
874 static uint64_t gt_get_countervalue(CPUARMState
*env
)
876 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) / GTIMER_SCALE
;
879 static void gt_recalc_timer(ARMCPU
*cpu
, int timeridx
)
881 ARMGenericTimer
*gt
= &cpu
->env
.cp15
.c14_timer
[timeridx
];
884 /* Timer enabled: calculate and set current ISTATUS, irq, and
885 * reset timer to when ISTATUS next has to change
887 uint64_t count
= gt_get_countervalue(&cpu
->env
);
888 /* Note that this must be unsigned 64 bit arithmetic: */
889 int istatus
= count
>= gt
->cval
;
892 gt
->ctl
= deposit32(gt
->ctl
, 2, 1, istatus
);
893 qemu_set_irq(cpu
->gt_timer_outputs
[timeridx
],
894 (istatus
&& !(gt
->ctl
& 2)));
896 /* Next transition is when count rolls back over to zero */
897 nexttick
= UINT64_MAX
;
899 /* Next transition is when we hit cval */
902 /* Note that the desired next expiry time might be beyond the
903 * signed-64-bit range of a QEMUTimer -- in this case we just
904 * set the timer for as far in the future as possible. When the
905 * timer expires we will reset the timer for any remaining period.
907 if (nexttick
> INT64_MAX
/ GTIMER_SCALE
) {
908 nexttick
= INT64_MAX
/ GTIMER_SCALE
;
910 timer_mod(cpu
->gt_timer
[timeridx
], nexttick
);
912 /* Timer disabled: ISTATUS and timer output always clear */
914 qemu_set_irq(cpu
->gt_timer_outputs
[timeridx
], 0);
915 timer_del(cpu
->gt_timer
[timeridx
]);
919 static void gt_cnt_reset(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
921 ARMCPU
*cpu
= arm_env_get_cpu(env
);
922 int timeridx
= ri
->opc1
& 1;
924 timer_del(cpu
->gt_timer
[timeridx
]);
927 static uint64_t gt_cnt_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
929 return gt_get_countervalue(env
);
932 static void gt_cval_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
935 int timeridx
= ri
->opc1
& 1;
937 env
->cp15
.c14_timer
[timeridx
].cval
= value
;
938 gt_recalc_timer(arm_env_get_cpu(env
), timeridx
);
941 static uint64_t gt_tval_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
943 int timeridx
= ri
->crm
& 1;
945 return (uint32_t)(env
->cp15
.c14_timer
[timeridx
].cval
-
946 gt_get_countervalue(env
));
949 static void gt_tval_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
952 int timeridx
= ri
->crm
& 1;
954 env
->cp15
.c14_timer
[timeridx
].cval
= gt_get_countervalue(env
) +
955 + sextract64(value
, 0, 32);
956 gt_recalc_timer(arm_env_get_cpu(env
), timeridx
);
959 static void gt_ctl_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
962 ARMCPU
*cpu
= arm_env_get_cpu(env
);
963 int timeridx
= ri
->crm
& 1;
964 uint32_t oldval
= env
->cp15
.c14_timer
[timeridx
].ctl
;
966 env
->cp15
.c14_timer
[timeridx
].ctl
= value
& 3;
967 if ((oldval
^ value
) & 1) {
969 gt_recalc_timer(cpu
, timeridx
);
970 } else if ((oldval
& value
) & 2) {
971 /* IMASK toggled: don't need to recalculate,
972 * just set the interrupt line based on ISTATUS
974 qemu_set_irq(cpu
->gt_timer_outputs
[timeridx
],
975 (oldval
& 4) && (value
& 2));
979 void arm_gt_ptimer_cb(void *opaque
)
981 ARMCPU
*cpu
= opaque
;
983 gt_recalc_timer(cpu
, GTIMER_PHYS
);
986 void arm_gt_vtimer_cb(void *opaque
)
988 ARMCPU
*cpu
= opaque
;
990 gt_recalc_timer(cpu
, GTIMER_VIRT
);
993 static const ARMCPRegInfo generic_timer_cp_reginfo
[] = {
994 /* Note that CNTFRQ is purely reads-as-written for the benefit
995 * of software; writing it doesn't actually change the timer frequency.
996 * Our reset value matches the fixed frequency we implement the timer at.
998 { .name
= "CNTFRQ", .cp
= 15, .crn
= 14, .crm
= 0, .opc1
= 0, .opc2
= 0,
999 .type
= ARM_CP_NO_MIGRATE
,
1000 .access
= PL1_RW
| PL0_R
, .accessfn
= gt_cntfrq_access
,
1001 .fieldoffset
= offsetoflow32(CPUARMState
, cp15
.c14_cntfrq
),
1002 .resetfn
= arm_cp_reset_ignore
,
1004 { .name
= "CNTFRQ_EL0", .state
= ARM_CP_STATE_AA64
,
1005 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 0, .opc2
= 0,
1006 .access
= PL1_RW
| PL0_R
, .accessfn
= gt_cntfrq_access
,
1007 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_cntfrq
),
1008 .resetvalue
= (1000 * 1000 * 1000) / GTIMER_SCALE
,
1010 /* overall control: mostly access permissions */
1011 { .name
= "CNTKCTL", .state
= ARM_CP_STATE_BOTH
,
1012 .opc0
= 3, .opc1
= 0, .crn
= 14, .crm
= 1, .opc2
= 0,
1014 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_cntkctl
),
1017 /* per-timer control */
1018 { .name
= "CNTP_CTL", .cp
= 15, .crn
= 14, .crm
= 2, .opc1
= 0, .opc2
= 1,
1019 .type
= ARM_CP_IO
| ARM_CP_NO_MIGRATE
, .access
= PL1_RW
| PL0_R
,
1020 .accessfn
= gt_ptimer_access
,
1021 .fieldoffset
= offsetoflow32(CPUARMState
,
1022 cp15
.c14_timer
[GTIMER_PHYS
].ctl
),
1023 .resetfn
= arm_cp_reset_ignore
,
1024 .writefn
= gt_ctl_write
, .raw_writefn
= raw_write
,
1026 { .name
= "CNTP_CTL_EL0", .state
= ARM_CP_STATE_AA64
,
1027 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 2, .opc2
= 1,
1028 .type
= ARM_CP_IO
, .access
= PL1_RW
| PL0_R
,
1029 .accessfn
= gt_ptimer_access
,
1030 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_timer
[GTIMER_PHYS
].ctl
),
1032 .writefn
= gt_ctl_write
, .raw_writefn
= raw_write
,
1034 { .name
= "CNTV_CTL", .cp
= 15, .crn
= 14, .crm
= 3, .opc1
= 0, .opc2
= 1,
1035 .type
= ARM_CP_IO
| ARM_CP_NO_MIGRATE
, .access
= PL1_RW
| PL0_R
,
1036 .accessfn
= gt_vtimer_access
,
1037 .fieldoffset
= offsetoflow32(CPUARMState
,
1038 cp15
.c14_timer
[GTIMER_VIRT
].ctl
),
1039 .resetfn
= arm_cp_reset_ignore
,
1040 .writefn
= gt_ctl_write
, .raw_writefn
= raw_write
,
1042 { .name
= "CNTV_CTL_EL0", .state
= ARM_CP_STATE_AA64
,
1043 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 3, .opc2
= 1,
1044 .type
= ARM_CP_IO
, .access
= PL1_RW
| PL0_R
,
1045 .accessfn
= gt_vtimer_access
,
1046 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_timer
[GTIMER_VIRT
].ctl
),
1048 .writefn
= gt_ctl_write
, .raw_writefn
= raw_write
,
1050 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1051 { .name
= "CNTP_TVAL", .cp
= 15, .crn
= 14, .crm
= 2, .opc1
= 0, .opc2
= 0,
1052 .type
= ARM_CP_NO_MIGRATE
| ARM_CP_IO
, .access
= PL1_RW
| PL0_R
,
1053 .accessfn
= gt_ptimer_access
,
1054 .readfn
= gt_tval_read
, .writefn
= gt_tval_write
,
1056 { .name
= "CNTP_TVAL_EL0", .state
= ARM_CP_STATE_AA64
,
1057 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 2, .opc2
= 0,
1058 .type
= ARM_CP_NO_MIGRATE
| ARM_CP_IO
, .access
= PL1_RW
| PL0_R
,
1059 .readfn
= gt_tval_read
, .writefn
= gt_tval_write
,
1061 { .name
= "CNTV_TVAL", .cp
= 15, .crn
= 14, .crm
= 3, .opc1
= 0, .opc2
= 0,
1062 .type
= ARM_CP_NO_MIGRATE
| ARM_CP_IO
, .access
= PL1_RW
| PL0_R
,
1063 .accessfn
= gt_vtimer_access
,
1064 .readfn
= gt_tval_read
, .writefn
= gt_tval_write
,
1066 { .name
= "CNTV_TVAL_EL0", .state
= ARM_CP_STATE_AA64
,
1067 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 3, .opc2
= 0,
1068 .type
= ARM_CP_NO_MIGRATE
| ARM_CP_IO
, .access
= PL1_RW
| PL0_R
,
1069 .readfn
= gt_tval_read
, .writefn
= gt_tval_write
,
1071 /* The counter itself */
1072 { .name
= "CNTPCT", .cp
= 15, .crm
= 14, .opc1
= 0,
1073 .access
= PL0_R
, .type
= ARM_CP_64BIT
| ARM_CP_NO_MIGRATE
| ARM_CP_IO
,
1074 .accessfn
= gt_pct_access
,
1075 .readfn
= gt_cnt_read
, .resetfn
= arm_cp_reset_ignore
,
1077 { .name
= "CNTPCT_EL0", .state
= ARM_CP_STATE_AA64
,
1078 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 0, .opc2
= 1,
1079 .access
= PL0_R
, .type
= ARM_CP_NO_MIGRATE
| ARM_CP_IO
,
1080 .accessfn
= gt_pct_access
,
1081 .readfn
= gt_cnt_read
, .resetfn
= gt_cnt_reset
,
1083 { .name
= "CNTVCT", .cp
= 15, .crm
= 14, .opc1
= 1,
1084 .access
= PL0_R
, .type
= ARM_CP_64BIT
| ARM_CP_NO_MIGRATE
| ARM_CP_IO
,
1085 .accessfn
= gt_vct_access
,
1086 .readfn
= gt_cnt_read
, .resetfn
= arm_cp_reset_ignore
,
1088 { .name
= "CNTVCT_EL0", .state
= ARM_CP_STATE_AA64
,
1089 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 0, .opc2
= 2,
1090 .access
= PL0_R
, .type
= ARM_CP_NO_MIGRATE
| ARM_CP_IO
,
1091 .accessfn
= gt_vct_access
,
1092 .readfn
= gt_cnt_read
, .resetfn
= gt_cnt_reset
,
1094 /* Comparison value, indicating when the timer goes off */
1095 { .name
= "CNTP_CVAL", .cp
= 15, .crm
= 14, .opc1
= 2,
1096 .access
= PL1_RW
| PL0_R
,
1097 .type
= ARM_CP_64BIT
| ARM_CP_IO
| ARM_CP_NO_MIGRATE
,
1098 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_timer
[GTIMER_PHYS
].cval
),
1099 .accessfn
= gt_ptimer_access
, .resetfn
= arm_cp_reset_ignore
,
1100 .writefn
= gt_cval_write
, .raw_writefn
= raw_write
,
1102 { .name
= "CNTP_CVAL_EL0", .state
= ARM_CP_STATE_AA64
,
1103 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 2, .opc2
= 2,
1104 .access
= PL1_RW
| PL0_R
,
1106 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_timer
[GTIMER_PHYS
].cval
),
1107 .resetvalue
= 0, .accessfn
= gt_vtimer_access
,
1108 .writefn
= gt_cval_write
, .raw_writefn
= raw_write
,
1110 { .name
= "CNTV_CVAL", .cp
= 15, .crm
= 14, .opc1
= 3,
1111 .access
= PL1_RW
| PL0_R
,
1112 .type
= ARM_CP_64BIT
| ARM_CP_IO
| ARM_CP_NO_MIGRATE
,
1113 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_timer
[GTIMER_VIRT
].cval
),
1114 .accessfn
= gt_vtimer_access
, .resetfn
= arm_cp_reset_ignore
,
1115 .writefn
= gt_cval_write
, .raw_writefn
= raw_write
,
1117 { .name
= "CNTV_CVAL_EL0", .state
= ARM_CP_STATE_AA64
,
1118 .opc0
= 3, .opc1
= 3, .crn
= 14, .crm
= 3, .opc2
= 2,
1119 .access
= PL1_RW
| PL0_R
,
1121 .fieldoffset
= offsetof(CPUARMState
, cp15
.c14_timer
[GTIMER_VIRT
].cval
),
1122 .resetvalue
= 0, .accessfn
= gt_vtimer_access
,
1123 .writefn
= gt_cval_write
, .raw_writefn
= raw_write
,
1129 /* In user-mode none of the generic timer registers are accessible,
1130 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
1131 * so instead just don't register any of them.
1133 static const ARMCPRegInfo generic_timer_cp_reginfo
[] = {
1139 static void par_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
, uint64_t value
)
1141 if (arm_feature(env
, ARM_FEATURE_LPAE
)) {
1142 env
->cp15
.c7_par
= value
;
1143 } else if (arm_feature(env
, ARM_FEATURE_V7
)) {
1144 env
->cp15
.c7_par
= value
& 0xfffff6ff;
1146 env
->cp15
.c7_par
= value
& 0xfffff1ff;
1150 #ifndef CONFIG_USER_ONLY
1151 /* get_phys_addr() isn't present for user-mode-only targets */
1153 /* Return true if extended addresses are enabled, ie this is an
1154 * LPAE implementation and we are using the long-descriptor translation
1155 * table format because the TTBCR EAE bit is set.
1157 static inline bool extended_addresses_enabled(CPUARMState
*env
)
1159 return arm_feature(env
, ARM_FEATURE_LPAE
)
1160 && (env
->cp15
.c2_control
& (1U << 31));
1163 static CPAccessResult
ats_access(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1166 /* Other states are only available with TrustZone; in
1167 * a non-TZ implementation these registers don't exist
1168 * at all, which is an Uncategorized trap. This underdecoding
1169 * is safe because the reginfo is NO_MIGRATE.
1171 return CP_ACCESS_TRAP_UNCATEGORIZED
;
1173 return CP_ACCESS_OK
;
1176 static void ats_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
, uint64_t value
)
1179 target_ulong page_size
;
1181 int ret
, is_user
= ri
->opc2
& 2;
1182 int access_type
= ri
->opc2
& 1;
1184 ret
= get_phys_addr(env
, value
, access_type
, is_user
,
1185 &phys_addr
, &prot
, &page_size
);
1186 if (extended_addresses_enabled(env
)) {
1187 /* ret is a DFSR/IFSR value for the long descriptor
1188 * translation table format, but with WnR always clear.
1189 * Convert it to a 64-bit PAR.
1191 uint64_t par64
= (1 << 11); /* LPAE bit always set */
1193 par64
|= phys_addr
& ~0xfffULL
;
1194 /* We don't set the ATTR or SH fields in the PAR. */
1197 par64
|= (ret
& 0x3f) << 1; /* FS */
1198 /* Note that S2WLK and FSTAGE are always zero, because we don't
1199 * implement virtualization and therefore there can't be a stage 2
1203 env
->cp15
.c7_par
= par64
;
1204 env
->cp15
.c7_par_hi
= par64
>> 32;
1206 /* ret is a DFSR/IFSR value for the short descriptor
1207 * translation table format (with WnR always clear).
1208 * Convert it to a 32-bit PAR.
1211 /* We do not set any attribute bits in the PAR */
1212 if (page_size
== (1 << 24)
1213 && arm_feature(env
, ARM_FEATURE_V7
)) {
1214 env
->cp15
.c7_par
= (phys_addr
& 0xff000000) | 1 << 1;
1216 env
->cp15
.c7_par
= phys_addr
& 0xfffff000;
1219 env
->cp15
.c7_par
= ((ret
& (1 << 10)) >> 5) |
1220 ((ret
& (1 << 12)) >> 6) |
1221 ((ret
& 0xf) << 1) | 1;
1223 env
->cp15
.c7_par_hi
= 0;
1228 static const ARMCPRegInfo vapa_cp_reginfo
[] = {
1229 { .name
= "PAR", .cp
= 15, .crn
= 7, .crm
= 4, .opc1
= 0, .opc2
= 0,
1230 .access
= PL1_RW
, .resetvalue
= 0,
1231 .fieldoffset
= offsetof(CPUARMState
, cp15
.c7_par
),
1232 .writefn
= par_write
},
1233 #ifndef CONFIG_USER_ONLY
1234 { .name
= "ATS", .cp
= 15, .crn
= 7, .crm
= 8, .opc1
= 0, .opc2
= CP_ANY
,
1235 .access
= PL1_W
, .accessfn
= ats_access
,
1236 .writefn
= ats_write
, .type
= ARM_CP_NO_MIGRATE
},
1241 /* Return basic MPU access permission bits. */
1242 static uint32_t simple_mpu_ap_bits(uint32_t val
)
1249 for (i
= 0; i
< 16; i
+= 2) {
1250 ret
|= (val
>> i
) & mask
;
1256 /* Pad basic MPU access permission bits to extended format. */
1257 static uint32_t extended_mpu_ap_bits(uint32_t val
)
1264 for (i
= 0; i
< 16; i
+= 2) {
1265 ret
|= (val
& mask
) << i
;
1271 static void pmsav5_data_ap_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1274 env
->cp15
.c5_data
= extended_mpu_ap_bits(value
);
1277 static uint64_t pmsav5_data_ap_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1279 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1282 static void pmsav5_insn_ap_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1285 env
->cp15
.c5_insn
= extended_mpu_ap_bits(value
);
1288 static uint64_t pmsav5_insn_ap_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1290 return simple_mpu_ap_bits(env
->cp15
.c5_insn
);
1293 static const ARMCPRegInfo pmsav5_cp_reginfo
[] = {
1294 { .name
= "DATA_AP", .cp
= 15, .crn
= 5, .crm
= 0, .opc1
= 0, .opc2
= 0,
1295 .access
= PL1_RW
, .type
= ARM_CP_NO_MIGRATE
,
1296 .fieldoffset
= offsetof(CPUARMState
, cp15
.c5_data
), .resetvalue
= 0,
1297 .readfn
= pmsav5_data_ap_read
, .writefn
= pmsav5_data_ap_write
, },
1298 { .name
= "INSN_AP", .cp
= 15, .crn
= 5, .crm
= 0, .opc1
= 0, .opc2
= 1,
1299 .access
= PL1_RW
, .type
= ARM_CP_NO_MIGRATE
,
1300 .fieldoffset
= offsetof(CPUARMState
, cp15
.c5_insn
), .resetvalue
= 0,
1301 .readfn
= pmsav5_insn_ap_read
, .writefn
= pmsav5_insn_ap_write
, },
1302 { .name
= "DATA_EXT_AP", .cp
= 15, .crn
= 5, .crm
= 0, .opc1
= 0, .opc2
= 2,
1304 .fieldoffset
= offsetof(CPUARMState
, cp15
.c5_data
), .resetvalue
= 0, },
1305 { .name
= "INSN_EXT_AP", .cp
= 15, .crn
= 5, .crm
= 0, .opc1
= 0, .opc2
= 3,
1307 .fieldoffset
= offsetof(CPUARMState
, cp15
.c5_insn
), .resetvalue
= 0, },
1308 { .name
= "DCACHE_CFG", .cp
= 15, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 0,
1310 .fieldoffset
= offsetof(CPUARMState
, cp15
.c2_data
), .resetvalue
= 0, },
1311 { .name
= "ICACHE_CFG", .cp
= 15, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 1,
1313 .fieldoffset
= offsetof(CPUARMState
, cp15
.c2_insn
), .resetvalue
= 0, },
1314 /* Protection region base and size registers */
1315 { .name
= "946_PRBS0", .cp
= 15, .crn
= 6, .crm
= 0, .opc1
= 0,
1316 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1317 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[0]) },
1318 { .name
= "946_PRBS1", .cp
= 15, .crn
= 6, .crm
= 1, .opc1
= 0,
1319 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1320 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[1]) },
1321 { .name
= "946_PRBS2", .cp
= 15, .crn
= 6, .crm
= 2, .opc1
= 0,
1322 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1323 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[2]) },
1324 { .name
= "946_PRBS3", .cp
= 15, .crn
= 6, .crm
= 3, .opc1
= 0,
1325 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1326 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[3]) },
1327 { .name
= "946_PRBS4", .cp
= 15, .crn
= 6, .crm
= 4, .opc1
= 0,
1328 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1329 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[4]) },
1330 { .name
= "946_PRBS5", .cp
= 15, .crn
= 6, .crm
= 5, .opc1
= 0,
1331 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1332 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[5]) },
1333 { .name
= "946_PRBS6", .cp
= 15, .crn
= 6, .crm
= 6, .opc1
= 0,
1334 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1335 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[6]) },
1336 { .name
= "946_PRBS7", .cp
= 15, .crn
= 6, .crm
= 7, .opc1
= 0,
1337 .opc2
= CP_ANY
, .access
= PL1_RW
, .resetvalue
= 0,
1338 .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_region
[7]) },
1342 static void vmsa_ttbcr_raw_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1345 int maskshift
= extract32(value
, 0, 3);
1347 if (arm_feature(env
, ARM_FEATURE_LPAE
) && (value
& (1 << 31))) {
1348 value
&= ~((7 << 19) | (3 << 14) | (0xf << 3));
1352 /* Note that we always calculate c2_mask and c2_base_mask, but
1353 * they are only used for short-descriptor tables (ie if EAE is 0);
1354 * for long-descriptor tables the TTBCR fields are used differently
1355 * and the c2_mask and c2_base_mask values are meaningless.
1357 env
->cp15
.c2_control
= value
;
1358 env
->cp15
.c2_mask
= ~(((uint32_t)0xffffffffu
) >> maskshift
);
1359 env
->cp15
.c2_base_mask
= ~((uint32_t)0x3fffu
>> maskshift
);
1362 static void vmsa_ttbcr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1365 ARMCPU
*cpu
= arm_env_get_cpu(env
);
1367 if (arm_feature(env
, ARM_FEATURE_LPAE
)) {
1368 /* With LPAE the TTBCR could result in a change of ASID
1369 * via the TTBCR.A1 bit, so do a TLB flush.
1371 tlb_flush(CPU(cpu
), 1);
1373 vmsa_ttbcr_raw_write(env
, ri
, value
);
1376 static void vmsa_ttbcr_reset(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1378 env
->cp15
.c2_base_mask
= 0xffffc000u
;
1379 env
->cp15
.c2_control
= 0;
1380 env
->cp15
.c2_mask
= 0;
1383 static void vmsa_tcr_el1_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1386 ARMCPU
*cpu
= arm_env_get_cpu(env
);
1388 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
1389 tlb_flush(CPU(cpu
), 1);
1390 env
->cp15
.c2_control
= value
;
1393 static void vmsa_ttbr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1396 /* 64 bit accesses to the TTBRs can change the ASID and so we
1397 * must flush the TLB.
1399 if (cpreg_field_is_64bit(ri
)) {
1400 ARMCPU
*cpu
= arm_env_get_cpu(env
);
1402 tlb_flush(CPU(cpu
), 1);
1404 raw_write(env
, ri
, value
);
1407 static const ARMCPRegInfo vmsa_cp_reginfo
[] = {
1408 { .name
= "DFSR", .cp
= 15, .crn
= 5, .crm
= 0, .opc1
= 0, .opc2
= 0,
1410 .fieldoffset
= offsetof(CPUARMState
, cp15
.c5_data
), .resetvalue
= 0, },
1411 { .name
= "IFSR", .cp
= 15, .crn
= 5, .crm
= 0, .opc1
= 0, .opc2
= 1,
1413 .fieldoffset
= offsetof(CPUARMState
, cp15
.c5_insn
), .resetvalue
= 0, },
1414 { .name
= "TTBR0_EL1", .state
= ARM_CP_STATE_BOTH
,
1415 .opc0
= 3, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 0,
1416 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.ttbr0_el1
),
1417 .writefn
= vmsa_ttbr_write
, .resetvalue
= 0 },
1418 { .name
= "TTBR1_EL1", .state
= ARM_CP_STATE_BOTH
,
1419 .opc0
= 3, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 1,
1420 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.ttbr1_el1
),
1421 .writefn
= vmsa_ttbr_write
, .resetvalue
= 0 },
1422 { .name
= "TCR_EL1", .state
= ARM_CP_STATE_AA64
,
1423 .opc0
= 3, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 2,
1424 .access
= PL1_RW
, .writefn
= vmsa_tcr_el1_write
,
1425 .resetfn
= vmsa_ttbcr_reset
, .raw_writefn
= raw_write
,
1426 .fieldoffset
= offsetof(CPUARMState
, cp15
.c2_control
) },
1427 { .name
= "TTBCR", .cp
= 15, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 2,
1428 .access
= PL1_RW
, .type
= ARM_CP_NO_MIGRATE
, .writefn
= vmsa_ttbcr_write
,
1429 .resetfn
= arm_cp_reset_ignore
, .raw_writefn
= vmsa_ttbcr_raw_write
,
1430 .fieldoffset
= offsetoflow32(CPUARMState
, cp15
.c2_control
) },
1431 { .name
= "DFAR", .cp
= 15, .crn
= 6, .crm
= 0, .opc1
= 0, .opc2
= 0,
1432 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c6_data
),
1437 static void omap_ticonfig_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1440 env
->cp15
.c15_ticonfig
= value
& 0xe7;
1441 /* The OS_TYPE bit in this register changes the reported CPUID! */
1442 env
->cp15
.c0_cpuid
= (value
& (1 << 5)) ?
1443 ARM_CPUID_TI915T
: ARM_CPUID_TI925T
;
1446 static void omap_threadid_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1449 env
->cp15
.c15_threadid
= value
& 0xffff;
1452 static void omap_wfi_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1455 /* Wait-for-interrupt (deprecated) */
1456 cpu_interrupt(CPU(arm_env_get_cpu(env
)), CPU_INTERRUPT_HALT
);
1459 static void omap_cachemaint_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1462 /* On OMAP there are registers indicating the max/min index of dcache lines
1463 * containing a dirty line; cache flush operations have to reset these.
1465 env
->cp15
.c15_i_max
= 0x000;
1466 env
->cp15
.c15_i_min
= 0xff0;
1469 static const ARMCPRegInfo omap_cp_reginfo
[] = {
1470 { .name
= "DFSR", .cp
= 15, .crn
= 5, .crm
= CP_ANY
,
1471 .opc1
= CP_ANY
, .opc2
= CP_ANY
, .access
= PL1_RW
, .type
= ARM_CP_OVERRIDE
,
1472 .fieldoffset
= offsetof(CPUARMState
, cp15
.c5_data
), .resetvalue
= 0, },
1473 { .name
= "", .cp
= 15, .crn
= 15, .crm
= 0, .opc1
= 0, .opc2
= 0,
1474 .access
= PL1_RW
, .type
= ARM_CP_NOP
},
1475 { .name
= "TICONFIG", .cp
= 15, .crn
= 15, .crm
= 1, .opc1
= 0, .opc2
= 0,
1477 .fieldoffset
= offsetof(CPUARMState
, cp15
.c15_ticonfig
), .resetvalue
= 0,
1478 .writefn
= omap_ticonfig_write
},
1479 { .name
= "IMAX", .cp
= 15, .crn
= 15, .crm
= 2, .opc1
= 0, .opc2
= 0,
1481 .fieldoffset
= offsetof(CPUARMState
, cp15
.c15_i_max
), .resetvalue
= 0, },
1482 { .name
= "IMIN", .cp
= 15, .crn
= 15, .crm
= 3, .opc1
= 0, .opc2
= 0,
1483 .access
= PL1_RW
, .resetvalue
= 0xff0,
1484 .fieldoffset
= offsetof(CPUARMState
, cp15
.c15_i_min
) },
1485 { .name
= "THREADID", .cp
= 15, .crn
= 15, .crm
= 4, .opc1
= 0, .opc2
= 0,
1487 .fieldoffset
= offsetof(CPUARMState
, cp15
.c15_threadid
), .resetvalue
= 0,
1488 .writefn
= omap_threadid_write
},
1489 { .name
= "TI925T_STATUS", .cp
= 15, .crn
= 15,
1490 .crm
= 8, .opc1
= 0, .opc2
= 0, .access
= PL1_RW
,
1491 .type
= ARM_CP_NO_MIGRATE
,
1492 .readfn
= arm_cp_read_zero
, .writefn
= omap_wfi_write
, },
1493 /* TODO: Peripheral port remap register:
1494 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1495 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1498 { .name
= "OMAP_CACHEMAINT", .cp
= 15, .crn
= 7, .crm
= CP_ANY
,
1499 .opc1
= 0, .opc2
= CP_ANY
, .access
= PL1_W
,
1500 .type
= ARM_CP_OVERRIDE
| ARM_CP_NO_MIGRATE
,
1501 .writefn
= omap_cachemaint_write
},
1502 { .name
= "C9", .cp
= 15, .crn
= 9,
1503 .crm
= CP_ANY
, .opc1
= CP_ANY
, .opc2
= CP_ANY
, .access
= PL1_RW
,
1504 .type
= ARM_CP_CONST
| ARM_CP_OVERRIDE
, .resetvalue
= 0 },
1508 static void xscale_cpar_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1512 if (env
->cp15
.c15_cpar
!= value
) {
1513 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1515 env
->cp15
.c15_cpar
= value
;
1519 static const ARMCPRegInfo xscale_cp_reginfo
[] = {
1520 { .name
= "XSCALE_CPAR",
1521 .cp
= 15, .crn
= 15, .crm
= 1, .opc1
= 0, .opc2
= 0, .access
= PL1_RW
,
1522 .fieldoffset
= offsetof(CPUARMState
, cp15
.c15_cpar
), .resetvalue
= 0,
1523 .writefn
= xscale_cpar_write
, },
1524 { .name
= "XSCALE_AUXCR",
1525 .cp
= 15, .crn
= 1, .crm
= 0, .opc1
= 0, .opc2
= 1, .access
= PL1_RW
,
1526 .fieldoffset
= offsetof(CPUARMState
, cp15
.c1_xscaleauxcr
),
1531 static const ARMCPRegInfo dummy_c15_cp_reginfo
[] = {
1532 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1533 * implementation of this implementation-defined space.
1534 * Ideally this should eventually disappear in favour of actually
1535 * implementing the correct behaviour for all cores.
1537 { .name
= "C15_IMPDEF", .cp
= 15, .crn
= 15,
1538 .crm
= CP_ANY
, .opc1
= CP_ANY
, .opc2
= CP_ANY
,
1540 .type
= ARM_CP_CONST
| ARM_CP_NO_MIGRATE
| ARM_CP_OVERRIDE
,
1545 static const ARMCPRegInfo cache_dirty_status_cp_reginfo
[] = {
1546 /* Cache status: RAZ because we have no cache so it's always clean */
1547 { .name
= "CDSR", .cp
= 15, .crn
= 7, .crm
= 10, .opc1
= 0, .opc2
= 6,
1548 .access
= PL1_R
, .type
= ARM_CP_CONST
| ARM_CP_NO_MIGRATE
,
1553 static const ARMCPRegInfo cache_block_ops_cp_reginfo
[] = {
1554 /* We never have a a block transfer operation in progress */
1555 { .name
= "BXSR", .cp
= 15, .crn
= 7, .crm
= 12, .opc1
= 0, .opc2
= 4,
1556 .access
= PL0_R
, .type
= ARM_CP_CONST
| ARM_CP_NO_MIGRATE
,
1558 /* The cache ops themselves: these all NOP for QEMU */
1559 { .name
= "IICR", .cp
= 15, .crm
= 5, .opc1
= 0,
1560 .access
= PL1_W
, .type
= ARM_CP_NOP
|ARM_CP_64BIT
},
1561 { .name
= "IDCR", .cp
= 15, .crm
= 6, .opc1
= 0,
1562 .access
= PL1_W
, .type
= ARM_CP_NOP
|ARM_CP_64BIT
},
1563 { .name
= "CDCR", .cp
= 15, .crm
= 12, .opc1
= 0,
1564 .access
= PL0_W
, .type
= ARM_CP_NOP
|ARM_CP_64BIT
},
1565 { .name
= "PIR", .cp
= 15, .crm
= 12, .opc1
= 1,
1566 .access
= PL0_W
, .type
= ARM_CP_NOP
|ARM_CP_64BIT
},
1567 { .name
= "PDR", .cp
= 15, .crm
= 12, .opc1
= 2,
1568 .access
= PL0_W
, .type
= ARM_CP_NOP
|ARM_CP_64BIT
},
1569 { .name
= "CIDCR", .cp
= 15, .crm
= 14, .opc1
= 0,
1570 .access
= PL1_W
, .type
= ARM_CP_NOP
|ARM_CP_64BIT
},
1574 static const ARMCPRegInfo cache_test_clean_cp_reginfo
[] = {
1575 /* The cache test-and-clean instructions always return (1 << 30)
1576 * to indicate that there are no dirty cache lines.
1578 { .name
= "TC_DCACHE", .cp
= 15, .crn
= 7, .crm
= 10, .opc1
= 0, .opc2
= 3,
1579 .access
= PL0_R
, .type
= ARM_CP_CONST
| ARM_CP_NO_MIGRATE
,
1580 .resetvalue
= (1 << 30) },
1581 { .name
= "TCI_DCACHE", .cp
= 15, .crn
= 7, .crm
= 14, .opc1
= 0, .opc2
= 3,
1582 .access
= PL0_R
, .type
= ARM_CP_CONST
| ARM_CP_NO_MIGRATE
,
1583 .resetvalue
= (1 << 30) },
1587 static const ARMCPRegInfo strongarm_cp_reginfo
[] = {
1588 /* Ignore ReadBuffer accesses */
1589 { .name
= "C9_READBUFFER", .cp
= 15, .crn
= 9,
1590 .crm
= CP_ANY
, .opc1
= CP_ANY
, .opc2
= CP_ANY
,
1591 .access
= PL1_RW
, .resetvalue
= 0,
1592 .type
= ARM_CP_CONST
| ARM_CP_OVERRIDE
| ARM_CP_NO_MIGRATE
},
1596 static uint64_t mpidr_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1598 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
1599 uint32_t mpidr
= cs
->cpu_index
;
1600 /* We don't support setting cluster ID ([8..11]) (known as Aff1
1601 * in later ARM ARM versions), or any of the higher affinity level fields,
1602 * so these bits always RAZ.
1604 if (arm_feature(env
, ARM_FEATURE_V7MP
)) {
1605 mpidr
|= (1U << 31);
1606 /* Cores which are uniprocessor (non-coherent)
1607 * but still implement the MP extensions set
1608 * bit 30. (For instance, A9UP.) However we do
1609 * not currently model any of those cores.
1615 static const ARMCPRegInfo mpidr_cp_reginfo
[] = {
1616 { .name
= "MPIDR", .state
= ARM_CP_STATE_BOTH
,
1617 .opc0
= 3, .crn
= 0, .crm
= 0, .opc1
= 0, .opc2
= 5,
1618 .access
= PL1_R
, .readfn
= mpidr_read
, .type
= ARM_CP_NO_MIGRATE
},
1622 static uint64_t par64_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1624 return ((uint64_t)env
->cp15
.c7_par_hi
<< 32) | env
->cp15
.c7_par
;
1627 static void par64_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1630 env
->cp15
.c7_par_hi
= value
>> 32;
1631 env
->cp15
.c7_par
= value
;
1634 static void par64_reset(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1636 env
->cp15
.c7_par_hi
= 0;
1637 env
->cp15
.c7_par
= 0;
1640 static const ARMCPRegInfo lpae_cp_reginfo
[] = {
1641 /* NOP AMAIR0/1: the override is because these clash with the rather
1642 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1644 { .name
= "AMAIR0", .state
= ARM_CP_STATE_BOTH
,
1645 .opc0
= 3, .crn
= 10, .crm
= 3, .opc1
= 0, .opc2
= 0,
1646 .access
= PL1_RW
, .type
= ARM_CP_CONST
| ARM_CP_OVERRIDE
,
1648 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
1649 { .name
= "AMAIR1", .cp
= 15, .crn
= 10, .crm
= 3, .opc1
= 0, .opc2
= 1,
1650 .access
= PL1_RW
, .type
= ARM_CP_CONST
| ARM_CP_OVERRIDE
,
1652 /* 64 bit access versions of the (dummy) debug registers */
1653 { .name
= "DBGDRAR", .cp
= 14, .crm
= 1, .opc1
= 0,
1654 .access
= PL0_R
, .type
= ARM_CP_CONST
|ARM_CP_64BIT
, .resetvalue
= 0 },
1655 { .name
= "DBGDSAR", .cp
= 14, .crm
= 2, .opc1
= 0,
1656 .access
= PL0_R
, .type
= ARM_CP_CONST
|ARM_CP_64BIT
, .resetvalue
= 0 },
1657 { .name
= "PAR", .cp
= 15, .crm
= 7, .opc1
= 0,
1658 .access
= PL1_RW
, .type
= ARM_CP_64BIT
,
1659 .readfn
= par64_read
, .writefn
= par64_write
, .resetfn
= par64_reset
},
1660 { .name
= "TTBR0", .cp
= 15, .crm
= 2, .opc1
= 0,
1661 .access
= PL1_RW
, .type
= ARM_CP_64BIT
| ARM_CP_NO_MIGRATE
,
1662 .fieldoffset
= offsetof(CPUARMState
, cp15
.ttbr0_el1
),
1663 .writefn
= vmsa_ttbr_write
, .resetfn
= arm_cp_reset_ignore
},
1664 { .name
= "TTBR1", .cp
= 15, .crm
= 2, .opc1
= 1,
1665 .access
= PL1_RW
, .type
= ARM_CP_64BIT
| ARM_CP_NO_MIGRATE
,
1666 .fieldoffset
= offsetof(CPUARMState
, cp15
.ttbr1_el1
),
1667 .writefn
= vmsa_ttbr_write
, .resetfn
= arm_cp_reset_ignore
},
1671 static uint64_t aa64_fpcr_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1673 return vfp_get_fpcr(env
);
1676 static void aa64_fpcr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1679 vfp_set_fpcr(env
, value
);
1682 static uint64_t aa64_fpsr_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1684 return vfp_get_fpsr(env
);
1687 static void aa64_fpsr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1690 vfp_set_fpsr(env
, value
);
1693 static CPAccessResult
aa64_cacheop_access(CPUARMState
*env
,
1694 const ARMCPRegInfo
*ri
)
1696 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
1697 * SCTLR_EL1.UCI is set.
1699 if (arm_current_pl(env
) == 0 && !(env
->cp15
.c1_sys
& SCTLR_UCI
)) {
1700 return CP_ACCESS_TRAP
;
1702 return CP_ACCESS_OK
;
1705 static void tlbi_aa64_va_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1708 /* Invalidate by VA (AArch64 version) */
1709 ARMCPU
*cpu
= arm_env_get_cpu(env
);
1710 uint64_t pageaddr
= value
<< 12;
1711 tlb_flush_page(CPU(cpu
), pageaddr
);
1714 static void tlbi_aa64_vaa_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1717 /* Invalidate by VA, all ASIDs (AArch64 version) */
1718 ARMCPU
*cpu
= arm_env_get_cpu(env
);
1719 uint64_t pageaddr
= value
<< 12;
1720 tlb_flush_page(CPU(cpu
), pageaddr
);
1723 static void tlbi_aa64_asid_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1726 /* Invalidate by ASID (AArch64 version) */
1727 ARMCPU
*cpu
= arm_env_get_cpu(env
);
1728 int asid
= extract64(value
, 48, 16);
1729 tlb_flush(CPU(cpu
), asid
== 0);
1732 static const ARMCPRegInfo v8_cp_reginfo
[] = {
1733 /* Minimal set of EL0-visible registers. This will need to be expanded
1734 * significantly for system emulation of AArch64 CPUs.
1736 { .name
= "NZCV", .state
= ARM_CP_STATE_AA64
,
1737 .opc0
= 3, .opc1
= 3, .opc2
= 0, .crn
= 4, .crm
= 2,
1738 .access
= PL0_RW
, .type
= ARM_CP_NZCV
},
1739 { .name
= "FPCR", .state
= ARM_CP_STATE_AA64
,
1740 .opc0
= 3, .opc1
= 3, .opc2
= 0, .crn
= 4, .crm
= 4,
1741 .access
= PL0_RW
, .readfn
= aa64_fpcr_read
, .writefn
= aa64_fpcr_write
},
1742 { .name
= "FPSR", .state
= ARM_CP_STATE_AA64
,
1743 .opc0
= 3, .opc1
= 3, .opc2
= 1, .crn
= 4, .crm
= 4,
1744 .access
= PL0_RW
, .readfn
= aa64_fpsr_read
, .writefn
= aa64_fpsr_write
},
1745 /* Prohibit use of DC ZVA. OPTME: implement DC ZVA and allow its use.
1746 * For system mode the DZP bit here will need to be computed, not constant.
1748 { .name
= "DCZID_EL0", .state
= ARM_CP_STATE_AA64
,
1749 .opc0
= 3, .opc1
= 3, .opc2
= 7, .crn
= 0, .crm
= 0,
1750 .access
= PL0_R
, .type
= ARM_CP_CONST
,
1751 .resetvalue
= 0x10 },
1752 { .name
= "CURRENTEL", .state
= ARM_CP_STATE_AA64
,
1753 .opc0
= 3, .opc1
= 0, .opc2
= 2, .crn
= 4, .crm
= 2,
1754 .access
= PL1_R
, .type
= ARM_CP_CURRENTEL
},
1755 /* Cache ops: all NOPs since we don't emulate caches */
1756 { .name
= "IC_IALLUIS", .state
= ARM_CP_STATE_AA64
,
1757 .opc0
= 1, .opc1
= 0, .crn
= 7, .crm
= 1, .opc2
= 0,
1758 .access
= PL1_W
, .type
= ARM_CP_NOP
},
1759 { .name
= "IC_IALLU", .state
= ARM_CP_STATE_AA64
,
1760 .opc0
= 1, .opc1
= 0, .crn
= 7, .crm
= 5, .opc2
= 0,
1761 .access
= PL1_W
, .type
= ARM_CP_NOP
},
1762 { .name
= "IC_IVAU", .state
= ARM_CP_STATE_AA64
,
1763 .opc0
= 1, .opc1
= 3, .crn
= 7, .crm
= 5, .opc2
= 1,
1764 .access
= PL0_W
, .type
= ARM_CP_NOP
,
1765 .accessfn
= aa64_cacheop_access
},
1766 { .name
= "DC_IVAC", .state
= ARM_CP_STATE_AA64
,
1767 .opc0
= 1, .opc1
= 0, .crn
= 7, .crm
= 6, .opc2
= 1,
1768 .access
= PL1_W
, .type
= ARM_CP_NOP
},
1769 { .name
= "DC_ISW", .state
= ARM_CP_STATE_AA64
,
1770 .opc0
= 1, .opc1
= 0, .crn
= 7, .crm
= 6, .opc2
= 2,
1771 .access
= PL1_W
, .type
= ARM_CP_NOP
},
1772 { .name
= "DC_CVAC", .state
= ARM_CP_STATE_AA64
,
1773 .opc0
= 1, .opc1
= 3, .crn
= 7, .crm
= 10, .opc2
= 1,
1774 .access
= PL0_W
, .type
= ARM_CP_NOP
,
1775 .accessfn
= aa64_cacheop_access
},
1776 { .name
= "DC_CSW", .state
= ARM_CP_STATE_AA64
,
1777 .opc0
= 1, .opc1
= 0, .crn
= 7, .crm
= 10, .opc2
= 2,
1778 .access
= PL1_W
, .type
= ARM_CP_NOP
},
1779 { .name
= "DC_CVAU", .state
= ARM_CP_STATE_AA64
,
1780 .opc0
= 1, .opc1
= 3, .crn
= 7, .crm
= 11, .opc2
= 1,
1781 .access
= PL0_W
, .type
= ARM_CP_NOP
,
1782 .accessfn
= aa64_cacheop_access
},
1783 { .name
= "DC_CIVAC", .state
= ARM_CP_STATE_AA64
,
1784 .opc0
= 1, .opc1
= 3, .crn
= 7, .crm
= 14, .opc2
= 1,
1785 .access
= PL0_W
, .type
= ARM_CP_NOP
,
1786 .accessfn
= aa64_cacheop_access
},
1787 { .name
= "DC_CISW", .state
= ARM_CP_STATE_AA64
,
1788 .opc0
= 1, .opc1
= 0, .crn
= 7, .crm
= 14, .opc2
= 2,
1789 .access
= PL1_W
, .type
= ARM_CP_NOP
},
1790 /* TLBI operations */
1791 { .name
= "TLBI_VMALLE1IS", .state
= ARM_CP_STATE_AA64
,
1792 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 3, .opc2
= 0,
1793 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1794 .writefn
= tlbiall_write
},
1795 { .name
= "TLBI_VAE1IS", .state
= ARM_CP_STATE_AA64
,
1796 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 3, .opc2
= 1,
1797 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1798 .writefn
= tlbi_aa64_va_write
},
1799 { .name
= "TLBI_ASIDE1IS", .state
= ARM_CP_STATE_AA64
,
1800 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 3, .opc2
= 2,
1801 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1802 .writefn
= tlbi_aa64_asid_write
},
1803 { .name
= "TLBI_VAAE1IS", .state
= ARM_CP_STATE_AA64
,
1804 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 3, .opc2
= 3,
1805 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1806 .writefn
= tlbi_aa64_vaa_write
},
1807 { .name
= "TLBI_VALE1IS", .state
= ARM_CP_STATE_AA64
,
1808 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 3, .opc2
= 5,
1809 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1810 .writefn
= tlbi_aa64_va_write
},
1811 { .name
= "TLBI_VAALE1IS", .state
= ARM_CP_STATE_AA64
,
1812 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 3, .opc2
= 7,
1813 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1814 .writefn
= tlbi_aa64_vaa_write
},
1815 { .name
= "TLBI_VMALLE1", .state
= ARM_CP_STATE_AA64
,
1816 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 7, .opc2
= 0,
1817 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1818 .writefn
= tlbiall_write
},
1819 { .name
= "TLBI_VAE1", .state
= ARM_CP_STATE_AA64
,
1820 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 7, .opc2
= 1,
1821 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1822 .writefn
= tlbi_aa64_va_write
},
1823 { .name
= "TLBI_ASIDE1", .state
= ARM_CP_STATE_AA64
,
1824 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 7, .opc2
= 2,
1825 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1826 .writefn
= tlbi_aa64_asid_write
},
1827 { .name
= "TLBI_VAAE1", .state
= ARM_CP_STATE_AA64
,
1828 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 7, .opc2
= 3,
1829 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1830 .writefn
= tlbi_aa64_vaa_write
},
1831 { .name
= "TLBI_VALE1", .state
= ARM_CP_STATE_AA64
,
1832 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 7, .opc2
= 5,
1833 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1834 .writefn
= tlbi_aa64_va_write
},
1835 { .name
= "TLBI_VAALE1", .state
= ARM_CP_STATE_AA64
,
1836 .opc0
= 1, .opc2
= 0, .crn
= 8, .crm
= 7, .opc2
= 7,
1837 .access
= PL1_W
, .type
= ARM_CP_NO_MIGRATE
,
1838 .writefn
= tlbi_aa64_vaa_write
},
1839 /* Dummy implementation of monitor debug system control register:
1840 * we don't support debug.
1842 { .name
= "MDSCR_EL1", .state
= ARM_CP_STATE_AA64
,
1843 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= 2, .opc2
= 2,
1844 .access
= PL1_RW
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
1845 /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
1846 { .name
= "OSLAR_EL1", .state
= ARM_CP_STATE_AA64
,
1847 .opc0
= 2, .opc1
= 0, .crn
= 1, .crm
= 0, .opc2
= 4,
1848 .access
= PL1_W
, .type
= ARM_CP_NOP
},
1852 static void sctlr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1855 ARMCPU
*cpu
= arm_env_get_cpu(env
);
1857 env
->cp15
.c1_sys
= value
;
1858 /* ??? Lots of these bits are not implemented. */
1859 /* This may enable/disable the MMU, so do a TLB flush. */
1860 tlb_flush(CPU(cpu
), 1);
1863 static CPAccessResult
ctr_el0_access(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
1865 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
1866 * but the AArch32 CTR has its own reginfo struct)
1868 if (arm_current_pl(env
) == 0 && !(env
->cp15
.c1_sys
& SCTLR_UCT
)) {
1869 return CP_ACCESS_TRAP
;
1871 return CP_ACCESS_OK
;
1874 static void define_aarch64_debug_regs(ARMCPU
*cpu
)
1876 /* Define breakpoint and watchpoint registers. These do nothing
1877 * but read as written, for now.
1881 for (i
= 0; i
< 16; i
++) {
1882 ARMCPRegInfo dbgregs
[] = {
1883 { .name
= "DBGBVR", .state
= ARM_CP_STATE_AA64
,
1884 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 4,
1886 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgbvr
[i
]) },
1887 { .name
= "DBGBCR", .state
= ARM_CP_STATE_AA64
,
1888 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 5,
1890 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgbcr
[i
]) },
1891 { .name
= "DBGWVR", .state
= ARM_CP_STATE_AA64
,
1892 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 6,
1894 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgwvr
[i
]) },
1895 { .name
= "DBGWCR", .state
= ARM_CP_STATE_AA64
,
1896 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 7,
1898 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgwcr
[i
]) },
1901 define_arm_cp_regs(cpu
, dbgregs
);
1905 void register_cp_regs_for_features(ARMCPU
*cpu
)
1907 /* Register all the coprocessor registers based on feature bits */
1908 CPUARMState
*env
= &cpu
->env
;
1909 if (arm_feature(env
, ARM_FEATURE_M
)) {
1910 /* M profile has no coprocessor registers */
1914 define_arm_cp_regs(cpu
, cp_reginfo
);
1915 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1916 /* The ID registers all have impdef reset values */
1917 ARMCPRegInfo v6_idregs
[] = {
1918 { .name
= "ID_PFR0", .cp
= 15, .crn
= 0, .crm
= 1,
1919 .opc1
= 0, .opc2
= 0, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1920 .resetvalue
= cpu
->id_pfr0
},
1921 { .name
= "ID_PFR1", .cp
= 15, .crn
= 0, .crm
= 1,
1922 .opc1
= 0, .opc2
= 1, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1923 .resetvalue
= cpu
->id_pfr1
},
1924 { .name
= "ID_DFR0", .cp
= 15, .crn
= 0, .crm
= 1,
1925 .opc1
= 0, .opc2
= 2, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1926 .resetvalue
= cpu
->id_dfr0
},
1927 { .name
= "ID_AFR0", .cp
= 15, .crn
= 0, .crm
= 1,
1928 .opc1
= 0, .opc2
= 3, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1929 .resetvalue
= cpu
->id_afr0
},
1930 { .name
= "ID_MMFR0", .cp
= 15, .crn
= 0, .crm
= 1,
1931 .opc1
= 0, .opc2
= 4, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1932 .resetvalue
= cpu
->id_mmfr0
},
1933 { .name
= "ID_MMFR1", .cp
= 15, .crn
= 0, .crm
= 1,
1934 .opc1
= 0, .opc2
= 5, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1935 .resetvalue
= cpu
->id_mmfr1
},
1936 { .name
= "ID_MMFR2", .cp
= 15, .crn
= 0, .crm
= 1,
1937 .opc1
= 0, .opc2
= 6, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1938 .resetvalue
= cpu
->id_mmfr2
},
1939 { .name
= "ID_MMFR3", .cp
= 15, .crn
= 0, .crm
= 1,
1940 .opc1
= 0, .opc2
= 7, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1941 .resetvalue
= cpu
->id_mmfr3
},
1942 { .name
= "ID_ISAR0", .cp
= 15, .crn
= 0, .crm
= 2,
1943 .opc1
= 0, .opc2
= 0, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1944 .resetvalue
= cpu
->id_isar0
},
1945 { .name
= "ID_ISAR1", .cp
= 15, .crn
= 0, .crm
= 2,
1946 .opc1
= 0, .opc2
= 1, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1947 .resetvalue
= cpu
->id_isar1
},
1948 { .name
= "ID_ISAR2", .cp
= 15, .crn
= 0, .crm
= 2,
1949 .opc1
= 0, .opc2
= 2, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1950 .resetvalue
= cpu
->id_isar2
},
1951 { .name
= "ID_ISAR3", .cp
= 15, .crn
= 0, .crm
= 2,
1952 .opc1
= 0, .opc2
= 3, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1953 .resetvalue
= cpu
->id_isar3
},
1954 { .name
= "ID_ISAR4", .cp
= 15, .crn
= 0, .crm
= 2,
1955 .opc1
= 0, .opc2
= 4, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1956 .resetvalue
= cpu
->id_isar4
},
1957 { .name
= "ID_ISAR5", .cp
= 15, .crn
= 0, .crm
= 2,
1958 .opc1
= 0, .opc2
= 5, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1959 .resetvalue
= cpu
->id_isar5
},
1960 /* 6..7 are as yet unallocated and must RAZ */
1961 { .name
= "ID_ISAR6", .cp
= 15, .crn
= 0, .crm
= 2,
1962 .opc1
= 0, .opc2
= 6, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1964 { .name
= "ID_ISAR7", .cp
= 15, .crn
= 0, .crm
= 2,
1965 .opc1
= 0, .opc2
= 7, .access
= PL1_R
, .type
= ARM_CP_CONST
,
1969 define_arm_cp_regs(cpu
, v6_idregs
);
1970 define_arm_cp_regs(cpu
, v6_cp_reginfo
);
1972 define_arm_cp_regs(cpu
, not_v6_cp_reginfo
);
1974 if (arm_feature(env
, ARM_FEATURE_V6K
)) {
1975 define_arm_cp_regs(cpu
, v6k_cp_reginfo
);
1977 if (arm_feature(env
, ARM_FEATURE_V7
)) {
1978 /* v7 performance monitor control register: same implementor
1979 * field as main ID register, and we implement only the cycle
1982 #ifndef CONFIG_USER_ONLY
1983 ARMCPRegInfo pmcr
= {
1984 .name
= "PMCR", .cp
= 15, .crn
= 9, .crm
= 12, .opc1
= 0, .opc2
= 0,
1985 .access
= PL0_RW
, .resetvalue
= cpu
->midr
& 0xff000000,
1987 .fieldoffset
= offsetof(CPUARMState
, cp15
.c9_pmcr
),
1988 .accessfn
= pmreg_access
, .writefn
= pmcr_write
,
1989 .raw_writefn
= raw_write
,
1991 define_one_arm_cp_reg(cpu
, &pmcr
);
1993 ARMCPRegInfo clidr
= {
1994 .name
= "CLIDR", .state
= ARM_CP_STATE_BOTH
,
1995 .opc0
= 3, .crn
= 0, .crm
= 0, .opc1
= 1, .opc2
= 1,
1996 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= cpu
->clidr
1998 define_one_arm_cp_reg(cpu
, &clidr
);
1999 define_arm_cp_regs(cpu
, v7_cp_reginfo
);
2001 define_arm_cp_regs(cpu
, not_v7_cp_reginfo
);
2003 if (arm_feature(env
, ARM_FEATURE_V8
)) {
2004 /* AArch64 ID registers, which all have impdef reset values */
2005 ARMCPRegInfo v8_idregs
[] = {
2006 { .name
= "ID_AA64PFR0_EL1", .state
= ARM_CP_STATE_AA64
,
2007 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 4, .opc2
= 0,
2008 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2009 .resetvalue
= cpu
->id_aa64pfr0
},
2010 { .name
= "ID_AA64PFR1_EL1", .state
= ARM_CP_STATE_AA64
,
2011 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 4, .opc2
= 1,
2012 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2013 .resetvalue
= cpu
->id_aa64pfr1
},
2014 { .name
= "ID_AA64DFR0_EL1", .state
= ARM_CP_STATE_AA64
,
2015 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 5, .opc2
= 0,
2016 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2017 .resetvalue
= cpu
->id_aa64dfr0
},
2018 { .name
= "ID_AA64DFR1_EL1", .state
= ARM_CP_STATE_AA64
,
2019 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 5, .opc2
= 1,
2020 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2021 .resetvalue
= cpu
->id_aa64dfr1
},
2022 { .name
= "ID_AA64AFR0_EL1", .state
= ARM_CP_STATE_AA64
,
2023 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 5, .opc2
= 4,
2024 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2025 .resetvalue
= cpu
->id_aa64afr0
},
2026 { .name
= "ID_AA64AFR1_EL1", .state
= ARM_CP_STATE_AA64
,
2027 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 5, .opc2
= 5,
2028 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2029 .resetvalue
= cpu
->id_aa64afr1
},
2030 { .name
= "ID_AA64ISAR0_EL1", .state
= ARM_CP_STATE_AA64
,
2031 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 6, .opc2
= 0,
2032 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2033 .resetvalue
= cpu
->id_aa64isar0
},
2034 { .name
= "ID_AA64ISAR1_EL1", .state
= ARM_CP_STATE_AA64
,
2035 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 6, .opc2
= 1,
2036 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2037 .resetvalue
= cpu
->id_aa64isar1
},
2038 { .name
= "ID_AA64MMFR0_EL1", .state
= ARM_CP_STATE_AA64
,
2039 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 7, .opc2
= 0,
2040 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2041 .resetvalue
= cpu
->id_aa64mmfr0
},
2042 { .name
= "ID_AA64MMFR1_EL1", .state
= ARM_CP_STATE_AA64
,
2043 .opc0
= 3, .opc1
= 0, .crn
= 0, .crm
= 7, .opc2
= 1,
2044 .access
= PL1_R
, .type
= ARM_CP_CONST
,
2045 .resetvalue
= cpu
->id_aa64mmfr1
},
2048 define_arm_cp_regs(cpu
, v8_idregs
);
2049 define_arm_cp_regs(cpu
, v8_cp_reginfo
);
2050 define_aarch64_debug_regs(cpu
);
2052 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
2053 /* These are the MPU registers prior to PMSAv6. Any new
2054 * PMSA core later than the ARM946 will require that we
2055 * implement the PMSAv6 or PMSAv7 registers, which are
2056 * completely different.
2058 assert(!arm_feature(env
, ARM_FEATURE_V6
));
2059 define_arm_cp_regs(cpu
, pmsav5_cp_reginfo
);
2061 define_arm_cp_regs(cpu
, vmsa_cp_reginfo
);
2063 if (arm_feature(env
, ARM_FEATURE_THUMB2EE
)) {
2064 define_arm_cp_regs(cpu
, t2ee_cp_reginfo
);
2066 if (arm_feature(env
, ARM_FEATURE_GENERIC_TIMER
)) {
2067 define_arm_cp_regs(cpu
, generic_timer_cp_reginfo
);
2069 if (arm_feature(env
, ARM_FEATURE_VAPA
)) {
2070 define_arm_cp_regs(cpu
, vapa_cp_reginfo
);
2072 if (arm_feature(env
, ARM_FEATURE_CACHE_TEST_CLEAN
)) {
2073 define_arm_cp_regs(cpu
, cache_test_clean_cp_reginfo
);
2075 if (arm_feature(env
, ARM_FEATURE_CACHE_DIRTY_REG
)) {
2076 define_arm_cp_regs(cpu
, cache_dirty_status_cp_reginfo
);
2078 if (arm_feature(env
, ARM_FEATURE_CACHE_BLOCK_OPS
)) {
2079 define_arm_cp_regs(cpu
, cache_block_ops_cp_reginfo
);
2081 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
2082 define_arm_cp_regs(cpu
, omap_cp_reginfo
);
2084 if (arm_feature(env
, ARM_FEATURE_STRONGARM
)) {
2085 define_arm_cp_regs(cpu
, strongarm_cp_reginfo
);
2087 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
2088 define_arm_cp_regs(cpu
, xscale_cp_reginfo
);
2090 if (arm_feature(env
, ARM_FEATURE_DUMMY_C15_REGS
)) {
2091 define_arm_cp_regs(cpu
, dummy_c15_cp_reginfo
);
2093 if (arm_feature(env
, ARM_FEATURE_LPAE
)) {
2094 define_arm_cp_regs(cpu
, lpae_cp_reginfo
);
2096 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
2097 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
2098 * be read-only (ie write causes UNDEF exception).
2101 ARMCPRegInfo id_cp_reginfo
[] = {
2102 /* Note that the MIDR isn't a simple constant register because
2103 * of the TI925 behaviour where writes to another register can
2104 * cause the MIDR value to change.
2106 * Unimplemented registers in the c15 0 0 0 space default to
2107 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
2108 * and friends override accordingly.
2111 .cp
= 15, .crn
= 0, .crm
= 0, .opc1
= 0, .opc2
= CP_ANY
,
2112 .access
= PL1_R
, .resetvalue
= cpu
->midr
,
2113 .writefn
= arm_cp_write_ignore
, .raw_writefn
= raw_write
,
2114 .fieldoffset
= offsetof(CPUARMState
, cp15
.c0_cpuid
),
2115 .type
= ARM_CP_OVERRIDE
},
2116 { .name
= "MIDR_EL1", .state
= ARM_CP_STATE_AA64
,
2117 .opc0
= 3, .opc1
= 0, .opc2
= 0, .crn
= 0, .crm
= 0,
2118 .access
= PL1_R
, .resetvalue
= cpu
->midr
, .type
= ARM_CP_CONST
},
2120 .cp
= 15, .crn
= 0, .crm
= 0, .opc1
= 0, .opc2
= 1,
2121 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= cpu
->ctr
},
2122 { .name
= "CTR_EL0", .state
= ARM_CP_STATE_AA64
,
2123 .opc0
= 3, .opc1
= 3, .opc2
= 1, .crn
= 0, .crm
= 0,
2124 .access
= PL0_R
, .accessfn
= ctr_el0_access
,
2125 .type
= ARM_CP_CONST
, .resetvalue
= cpu
->ctr
},
2127 .cp
= 15, .crn
= 0, .crm
= 0, .opc1
= 0, .opc2
= 2,
2128 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
2130 .cp
= 15, .crn
= 0, .crm
= 0, .opc1
= 0, .opc2
= 3,
2131 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
2132 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
2134 .cp
= 15, .crn
= 0, .crm
= 3, .opc1
= 0, .opc2
= CP_ANY
,
2135 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
2137 .cp
= 15, .crn
= 0, .crm
= 4, .opc1
= 0, .opc2
= CP_ANY
,
2138 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
2140 .cp
= 15, .crn
= 0, .crm
= 5, .opc1
= 0, .opc2
= CP_ANY
,
2141 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
2143 .cp
= 15, .crn
= 0, .crm
= 6, .opc1
= 0, .opc2
= CP_ANY
,
2144 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
2146 .cp
= 15, .crn
= 0, .crm
= 7, .opc1
= 0, .opc2
= CP_ANY
,
2147 .access
= PL1_R
, .type
= ARM_CP_CONST
, .resetvalue
= 0 },
2150 ARMCPRegInfo crn0_wi_reginfo
= {
2151 .name
= "CRN0_WI", .cp
= 15, .crn
= 0, .crm
= CP_ANY
,
2152 .opc1
= CP_ANY
, .opc2
= CP_ANY
, .access
= PL1_W
,
2153 .type
= ARM_CP_NOP
| ARM_CP_OVERRIDE
2155 if (arm_feature(env
, ARM_FEATURE_OMAPCP
) ||
2156 arm_feature(env
, ARM_FEATURE_STRONGARM
)) {
2158 /* Register the blanket "writes ignored" value first to cover the
2159 * whole space. Then update the specific ID registers to allow write
2160 * access, so that they ignore writes rather than causing them to
2163 define_one_arm_cp_reg(cpu
, &crn0_wi_reginfo
);
2164 for (r
= id_cp_reginfo
; r
->type
!= ARM_CP_SENTINEL
; r
++) {
2168 define_arm_cp_regs(cpu
, id_cp_reginfo
);
2171 if (arm_feature(env
, ARM_FEATURE_MPIDR
)) {
2172 define_arm_cp_regs(cpu
, mpidr_cp_reginfo
);
2175 if (arm_feature(env
, ARM_FEATURE_AUXCR
)) {
2176 ARMCPRegInfo auxcr
= {
2177 .name
= "AUXCR", .cp
= 15, .crn
= 1, .crm
= 0, .opc1
= 0, .opc2
= 1,
2178 .access
= PL1_RW
, .type
= ARM_CP_CONST
,
2179 .resetvalue
= cpu
->reset_auxcr
2181 define_one_arm_cp_reg(cpu
, &auxcr
);
2184 if (arm_feature(env
, ARM_FEATURE_CBAR
)) {
2185 ARMCPRegInfo cbar
= {
2186 .name
= "CBAR", .cp
= 15, .crn
= 15, .crm
= 0, .opc1
= 4, .opc2
= 0,
2187 .access
= PL1_R
|PL3_W
, .resetvalue
= cpu
->reset_cbar
,
2188 .fieldoffset
= offsetof(CPUARMState
, cp15
.c15_config_base_address
)
2190 define_one_arm_cp_reg(cpu
, &cbar
);
2193 /* Generic registers whose values depend on the implementation */
2195 ARMCPRegInfo sctlr
= {
2196 .name
= "SCTLR", .state
= ARM_CP_STATE_BOTH
,
2197 .opc0
= 3, .crn
= 1, .crm
= 0, .opc1
= 0, .opc2
= 0,
2198 .access
= PL1_RW
, .fieldoffset
= offsetof(CPUARMState
, cp15
.c1_sys
),
2199 .writefn
= sctlr_write
, .resetvalue
= cpu
->reset_sctlr
,
2200 .raw_writefn
= raw_write
,
2202 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
2203 /* Normally we would always end the TB on an SCTLR write, but Linux
2204 * arch/arm/mach-pxa/sleep.S expects two instructions following
2205 * an MMU enable to execute from cache. Imitate this behaviour.
2207 sctlr
.type
|= ARM_CP_SUPPRESS_TB_END
;
2209 define_one_arm_cp_reg(cpu
, &sctlr
);
2213 ARMCPU
*cpu_arm_init(const char *cpu_model
)
2215 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU
, cpu_model
));
2218 void arm_cpu_register_gdb_regs_for_features(ARMCPU
*cpu
)
2220 CPUState
*cs
= CPU(cpu
);
2221 CPUARMState
*env
= &cpu
->env
;
2223 if (arm_feature(env
, ARM_FEATURE_AARCH64
)) {
2224 gdb_register_coprocessor(cs
, aarch64_fpu_gdb_get_reg
,
2225 aarch64_fpu_gdb_set_reg
,
2226 34, "aarch64-fpu.xml", 0);
2227 } else if (arm_feature(env
, ARM_FEATURE_NEON
)) {
2228 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
2229 51, "arm-neon.xml", 0);
2230 } else if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
2231 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
2232 35, "arm-vfp3.xml", 0);
2233 } else if (arm_feature(env
, ARM_FEATURE_VFP
)) {
2234 gdb_register_coprocessor(cs
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
2235 19, "arm-vfp.xml", 0);
2239 /* Sort alphabetically by type name, except for "any". */
2240 static gint
arm_cpu_list_compare(gconstpointer a
, gconstpointer b
)
2242 ObjectClass
*class_a
= (ObjectClass
*)a
;
2243 ObjectClass
*class_b
= (ObjectClass
*)b
;
2244 const char *name_a
, *name_b
;
2246 name_a
= object_class_get_name(class_a
);
2247 name_b
= object_class_get_name(class_b
);
2248 if (strcmp(name_a
, "any-" TYPE_ARM_CPU
) == 0) {
2250 } else if (strcmp(name_b
, "any-" TYPE_ARM_CPU
) == 0) {
2253 return strcmp(name_a
, name_b
);
2257 static void arm_cpu_list_entry(gpointer data
, gpointer user_data
)
2259 ObjectClass
*oc
= data
;
2260 CPUListState
*s
= user_data
;
2261 const char *typename
;
2264 typename
= object_class_get_name(oc
);
2265 name
= g_strndup(typename
, strlen(typename
) - strlen("-" TYPE_ARM_CPU
));
2266 (*s
->cpu_fprintf
)(s
->file
, " %s\n",
2271 void arm_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
2275 .cpu_fprintf
= cpu_fprintf
,
2279 list
= object_class_get_list(TYPE_ARM_CPU
, false);
2280 list
= g_slist_sort(list
, arm_cpu_list_compare
);
2281 (*cpu_fprintf
)(f
, "Available CPUs:\n");
2282 g_slist_foreach(list
, arm_cpu_list_entry
, &s
);
2285 /* The 'host' CPU type is dynamically registered only if KVM is
2286 * enabled, so we have to special-case it here:
2288 (*cpu_fprintf
)(f
, " host (only available in KVM mode)\n");
2292 static void arm_cpu_add_definition(gpointer data
, gpointer user_data
)
2294 ObjectClass
*oc
= data
;
2295 CpuDefinitionInfoList
**cpu_list
= user_data
;
2296 CpuDefinitionInfoList
*entry
;
2297 CpuDefinitionInfo
*info
;
2298 const char *typename
;
2300 typename
= object_class_get_name(oc
);
2301 info
= g_malloc0(sizeof(*info
));
2302 info
->name
= g_strndup(typename
,
2303 strlen(typename
) - strlen("-" TYPE_ARM_CPU
));
2305 entry
= g_malloc0(sizeof(*entry
));
2306 entry
->value
= info
;
2307 entry
->next
= *cpu_list
;
2311 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
2313 CpuDefinitionInfoList
*cpu_list
= NULL
;
2316 list
= object_class_get_list(TYPE_ARM_CPU
, false);
2317 g_slist_foreach(list
, arm_cpu_add_definition
, &cpu_list
);
2323 static void add_cpreg_to_hashtable(ARMCPU
*cpu
, const ARMCPRegInfo
*r
,
2324 void *opaque
, int state
,
2325 int crm
, int opc1
, int opc2
)
2327 /* Private utility function for define_one_arm_cp_reg_with_opaque():
2328 * add a single reginfo struct to the hash table.
2330 uint32_t *key
= g_new(uint32_t, 1);
2331 ARMCPRegInfo
*r2
= g_memdup(r
, sizeof(ARMCPRegInfo
));
2332 int is64
= (r
->type
& ARM_CP_64BIT
) ? 1 : 0;
2333 if (r
->state
== ARM_CP_STATE_BOTH
&& state
== ARM_CP_STATE_AA32
) {
2334 /* The AArch32 view of a shared register sees the lower 32 bits
2335 * of a 64 bit backing field. It is not migratable as the AArch64
2336 * view handles that. AArch64 also handles reset.
2337 * We assume it is a cp15 register.
2340 r2
->type
|= ARM_CP_NO_MIGRATE
;
2341 r2
->resetfn
= arm_cp_reset_ignore
;
2342 #ifdef HOST_WORDS_BIGENDIAN
2343 if (r2
->fieldoffset
) {
2344 r2
->fieldoffset
+= sizeof(uint32_t);
2348 if (state
== ARM_CP_STATE_AA64
) {
2349 /* To allow abbreviation of ARMCPRegInfo
2350 * definitions, we treat cp == 0 as equivalent to
2351 * the value for "standard guest-visible sysreg".
2354 r2
->cp
= CP_REG_ARM64_SYSREG_CP
;
2356 *key
= ENCODE_AA64_CP_REG(r2
->cp
, r2
->crn
, crm
,
2357 r2
->opc0
, opc1
, opc2
);
2359 *key
= ENCODE_CP_REG(r2
->cp
, is64
, r2
->crn
, crm
, opc1
, opc2
);
2362 r2
->opaque
= opaque
;
2364 /* reginfo passed to helpers is correct for the actual access,
2365 * and is never ARM_CP_STATE_BOTH:
2368 /* Make sure reginfo passed to helpers for wildcarded regs
2369 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
2374 /* By convention, for wildcarded registers only the first
2375 * entry is used for migration; the others are marked as
2376 * NO_MIGRATE so we don't try to transfer the register
2377 * multiple times. Special registers (ie NOP/WFI) are
2380 if ((r
->type
& ARM_CP_SPECIAL
) ||
2381 ((r
->crm
== CP_ANY
) && crm
!= 0) ||
2382 ((r
->opc1
== CP_ANY
) && opc1
!= 0) ||
2383 ((r
->opc2
== CP_ANY
) && opc2
!= 0)) {
2384 r2
->type
|= ARM_CP_NO_MIGRATE
;
2387 /* Overriding of an existing definition must be explicitly
2390 if (!(r
->type
& ARM_CP_OVERRIDE
)) {
2391 ARMCPRegInfo
*oldreg
;
2392 oldreg
= g_hash_table_lookup(cpu
->cp_regs
, key
);
2393 if (oldreg
&& !(oldreg
->type
& ARM_CP_OVERRIDE
)) {
2394 fprintf(stderr
, "Register redefined: cp=%d %d bit "
2395 "crn=%d crm=%d opc1=%d opc2=%d, "
2396 "was %s, now %s\n", r2
->cp
, 32 + 32 * is64
,
2397 r2
->crn
, r2
->crm
, r2
->opc1
, r2
->opc2
,
2398 oldreg
->name
, r2
->name
);
2399 g_assert_not_reached();
2402 g_hash_table_insert(cpu
->cp_regs
, key
, r2
);
2406 void define_one_arm_cp_reg_with_opaque(ARMCPU
*cpu
,
2407 const ARMCPRegInfo
*r
, void *opaque
)
2409 /* Define implementations of coprocessor registers.
2410 * We store these in a hashtable because typically
2411 * there are less than 150 registers in a space which
2412 * is 16*16*16*8*8 = 262144 in size.
2413 * Wildcarding is supported for the crm, opc1 and opc2 fields.
2414 * If a register is defined twice then the second definition is
2415 * used, so this can be used to define some generic registers and
2416 * then override them with implementation specific variations.
2417 * At least one of the original and the second definition should
2418 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
2419 * against accidental use.
2421 * The state field defines whether the register is to be
2422 * visible in the AArch32 or AArch64 execution state. If the
2423 * state is set to ARM_CP_STATE_BOTH then we synthesise a
2424 * reginfo structure for the AArch32 view, which sees the lower
2425 * 32 bits of the 64 bit register.
2427 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
2428 * be wildcarded. AArch64 registers are always considered to be 64
2429 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
2430 * the register, if any.
2432 int crm
, opc1
, opc2
, state
;
2433 int crmmin
= (r
->crm
== CP_ANY
) ? 0 : r
->crm
;
2434 int crmmax
= (r
->crm
== CP_ANY
) ? 15 : r
->crm
;
2435 int opc1min
= (r
->opc1
== CP_ANY
) ? 0 : r
->opc1
;
2436 int opc1max
= (r
->opc1
== CP_ANY
) ? 7 : r
->opc1
;
2437 int opc2min
= (r
->opc2
== CP_ANY
) ? 0 : r
->opc2
;
2438 int opc2max
= (r
->opc2
== CP_ANY
) ? 7 : r
->opc2
;
2439 /* 64 bit registers have only CRm and Opc1 fields */
2440 assert(!((r
->type
& ARM_CP_64BIT
) && (r
->opc2
|| r
->crn
)));
2441 /* op0 only exists in the AArch64 encodings */
2442 assert((r
->state
!= ARM_CP_STATE_AA32
) || (r
->opc0
== 0));
2443 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
2444 assert((r
->state
!= ARM_CP_STATE_AA64
) || !(r
->type
& ARM_CP_64BIT
));
2445 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
2446 * encodes a minimum access level for the register. We roll this
2447 * runtime check into our general permission check code, so check
2448 * here that the reginfo's specified permissions are strict enough
2449 * to encompass the generic architectural permission check.
2451 if (r
->state
!= ARM_CP_STATE_AA32
) {
2454 case 0: case 1: case 2:
2467 /* unallocated encoding, so not possible */
2475 /* min_EL EL1, secure mode only (we don't check the latter) */
2479 /* broken reginfo with out-of-range opc1 */
2483 /* assert our permissions are not too lax (stricter is fine) */
2484 assert((r
->access
& ~mask
) == 0);
2487 /* Check that the register definition has enough info to handle
2488 * reads and writes if they are permitted.
2490 if (!(r
->type
& (ARM_CP_SPECIAL
|ARM_CP_CONST
))) {
2491 if (r
->access
& PL3_R
) {
2492 assert(r
->fieldoffset
|| r
->readfn
);
2494 if (r
->access
& PL3_W
) {
2495 assert(r
->fieldoffset
|| r
->writefn
);
2498 /* Bad type field probably means missing sentinel at end of reg list */
2499 assert(cptype_valid(r
->type
));
2500 for (crm
= crmmin
; crm
<= crmmax
; crm
++) {
2501 for (opc1
= opc1min
; opc1
<= opc1max
; opc1
++) {
2502 for (opc2
= opc2min
; opc2
<= opc2max
; opc2
++) {
2503 for (state
= ARM_CP_STATE_AA32
;
2504 state
<= ARM_CP_STATE_AA64
; state
++) {
2505 if (r
->state
!= state
&& r
->state
!= ARM_CP_STATE_BOTH
) {
2508 add_cpreg_to_hashtable(cpu
, r
, opaque
, state
,
2516 void define_arm_cp_regs_with_opaque(ARMCPU
*cpu
,
2517 const ARMCPRegInfo
*regs
, void *opaque
)
2519 /* Define a whole list of registers */
2520 const ARMCPRegInfo
*r
;
2521 for (r
= regs
; r
->type
!= ARM_CP_SENTINEL
; r
++) {
2522 define_one_arm_cp_reg_with_opaque(cpu
, r
, opaque
);
2526 const ARMCPRegInfo
*get_arm_cp_reginfo(GHashTable
*cpregs
, uint32_t encoded_cp
)
2528 return g_hash_table_lookup(cpregs
, &encoded_cp
);
2531 void arm_cp_write_ignore(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
2534 /* Helper coprocessor write function for write-ignore registers */
2537 uint64_t arm_cp_read_zero(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
2539 /* Helper coprocessor write function for read-as-zero registers */
2543 void arm_cp_reset_ignore(CPUARMState
*env
, const ARMCPRegInfo
*opaque
)
2545 /* Helper coprocessor reset function for do-nothing-on-reset registers */
2548 static int bad_mode_switch(CPUARMState
*env
, int mode
)
2550 /* Return true if it is not valid for us to switch to
2551 * this CPU mode (ie all the UNPREDICTABLE cases in
2552 * the ARM ARM CPSRWriteByInstr pseudocode).
2555 case ARM_CPU_MODE_USR
:
2556 case ARM_CPU_MODE_SYS
:
2557 case ARM_CPU_MODE_SVC
:
2558 case ARM_CPU_MODE_ABT
:
2559 case ARM_CPU_MODE_UND
:
2560 case ARM_CPU_MODE_IRQ
:
2561 case ARM_CPU_MODE_FIQ
:
2568 uint32_t cpsr_read(CPUARMState
*env
)
2571 ZF
= (env
->ZF
== 0);
2572 return env
->uncached_cpsr
| (env
->NF
& 0x80000000) | (ZF
<< 30) |
2573 (env
->CF
<< 29) | ((env
->VF
& 0x80000000) >> 3) | (env
->QF
<< 27)
2574 | (env
->thumb
<< 5) | ((env
->condexec_bits
& 3) << 25)
2575 | ((env
->condexec_bits
& 0xfc) << 8)
2576 | (env
->GE
<< 16) | (env
->daif
& CPSR_AIF
);
2579 void cpsr_write(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
2581 if (mask
& CPSR_NZCV
) {
2582 env
->ZF
= (~val
) & CPSR_Z
;
2584 env
->CF
= (val
>> 29) & 1;
2585 env
->VF
= (val
<< 3) & 0x80000000;
2588 env
->QF
= ((val
& CPSR_Q
) != 0);
2590 env
->thumb
= ((val
& CPSR_T
) != 0);
2591 if (mask
& CPSR_IT_0_1
) {
2592 env
->condexec_bits
&= ~3;
2593 env
->condexec_bits
|= (val
>> 25) & 3;
2595 if (mask
& CPSR_IT_2_7
) {
2596 env
->condexec_bits
&= 3;
2597 env
->condexec_bits
|= (val
>> 8) & 0xfc;
2599 if (mask
& CPSR_GE
) {
2600 env
->GE
= (val
>> 16) & 0xf;
2603 env
->daif
&= ~(CPSR_AIF
& mask
);
2604 env
->daif
|= val
& CPSR_AIF
& mask
;
2606 if ((env
->uncached_cpsr
^ val
) & mask
& CPSR_M
) {
2607 if (bad_mode_switch(env
, val
& CPSR_M
)) {
2608 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2609 * We choose to ignore the attempt and leave the CPSR M field
2614 switch_mode(env
, val
& CPSR_M
);
2617 mask
&= ~CACHED_CPSR_BITS
;
2618 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~mask
) | (val
& mask
);
2621 /* Sign/zero extend */
2622 uint32_t HELPER(sxtb16
)(uint32_t x
)
2625 res
= (uint16_t)(int8_t)x
;
2626 res
|= (uint32_t)(int8_t)(x
>> 16) << 16;
2630 uint32_t HELPER(uxtb16
)(uint32_t x
)
2633 res
= (uint16_t)(uint8_t)x
;
2634 res
|= (uint32_t)(uint8_t)(x
>> 16) << 16;
2638 uint32_t HELPER(clz
)(uint32_t x
)
2643 int32_t HELPER(sdiv
)(int32_t num
, int32_t den
)
2647 if (num
== INT_MIN
&& den
== -1)
2652 uint32_t HELPER(udiv
)(uint32_t num
, uint32_t den
)
2659 uint32_t HELPER(rbit
)(uint32_t x
)
2661 x
= ((x
& 0xff000000) >> 24)
2662 | ((x
& 0x00ff0000) >> 8)
2663 | ((x
& 0x0000ff00) << 8)
2664 | ((x
& 0x000000ff) << 24);
2665 x
= ((x
& 0xf0f0f0f0) >> 4)
2666 | ((x
& 0x0f0f0f0f) << 4);
2667 x
= ((x
& 0x88888888) >> 3)
2668 | ((x
& 0x44444444) >> 1)
2669 | ((x
& 0x22222222) << 1)
2670 | ((x
& 0x11111111) << 3);
2674 #if defined(CONFIG_USER_ONLY)
2676 void arm_cpu_do_interrupt(CPUState
*cs
)
2678 cs
->exception_index
= -1;
2681 int arm_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int rw
,
2684 ARMCPU
*cpu
= ARM_CPU(cs
);
2685 CPUARMState
*env
= &cpu
->env
;
2688 cs
->exception_index
= EXCP_PREFETCH_ABORT
;
2689 env
->cp15
.c6_insn
= address
;
2691 cs
->exception_index
= EXCP_DATA_ABORT
;
2692 env
->cp15
.c6_data
= address
;
2697 /* These should probably raise undefined insn exceptions. */
2698 void HELPER(v7m_msr
)(CPUARMState
*env
, uint32_t reg
, uint32_t val
)
2700 ARMCPU
*cpu
= arm_env_get_cpu(env
);
2702 cpu_abort(CPU(cpu
), "v7m_msr %d\n", reg
);
2705 uint32_t HELPER(v7m_mrs
)(CPUARMState
*env
, uint32_t reg
)
2707 ARMCPU
*cpu
= arm_env_get_cpu(env
);
2709 cpu_abort(CPU(cpu
), "v7m_mrs %d\n", reg
);
2713 void switch_mode(CPUARMState
*env
, int mode
)
2715 ARMCPU
*cpu
= arm_env_get_cpu(env
);
2717 if (mode
!= ARM_CPU_MODE_USR
) {
2718 cpu_abort(CPU(cpu
), "Tried to switch out of user mode\n");
2722 void HELPER(set_r13_banked
)(CPUARMState
*env
, uint32_t mode
, uint32_t val
)
2724 ARMCPU
*cpu
= arm_env_get_cpu(env
);
2726 cpu_abort(CPU(cpu
), "banked r13 write\n");
2729 uint32_t HELPER(get_r13_banked
)(CPUARMState
*env
, uint32_t mode
)
2731 ARMCPU
*cpu
= arm_env_get_cpu(env
);
2733 cpu_abort(CPU(cpu
), "banked r13 read\n");
2739 /* Map CPU modes onto saved register banks. */
2740 int bank_number(int mode
)
2743 case ARM_CPU_MODE_USR
:
2744 case ARM_CPU_MODE_SYS
:
2746 case ARM_CPU_MODE_SVC
:
2748 case ARM_CPU_MODE_ABT
:
2750 case ARM_CPU_MODE_UND
:
2752 case ARM_CPU_MODE_IRQ
:
2754 case ARM_CPU_MODE_FIQ
:
2757 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode
);
2760 void switch_mode(CPUARMState
*env
, int mode
)
2765 old_mode
= env
->uncached_cpsr
& CPSR_M
;
2766 if (mode
== old_mode
)
2769 if (old_mode
== ARM_CPU_MODE_FIQ
) {
2770 memcpy (env
->fiq_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
2771 memcpy (env
->regs
+ 8, env
->usr_regs
, 5 * sizeof(uint32_t));
2772 } else if (mode
== ARM_CPU_MODE_FIQ
) {
2773 memcpy (env
->usr_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
2774 memcpy (env
->regs
+ 8, env
->fiq_regs
, 5 * sizeof(uint32_t));
2777 i
= bank_number(old_mode
);
2778 env
->banked_r13
[i
] = env
->regs
[13];
2779 env
->banked_r14
[i
] = env
->regs
[14];
2780 env
->banked_spsr
[i
] = env
->spsr
;
2782 i
= bank_number(mode
);
2783 env
->regs
[13] = env
->banked_r13
[i
];
2784 env
->regs
[14] = env
->banked_r14
[i
];
2785 env
->spsr
= env
->banked_spsr
[i
];
2788 static void v7m_push(CPUARMState
*env
, uint32_t val
)
2790 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
2793 stl_phys(cs
->as
, env
->regs
[13], val
);
2796 static uint32_t v7m_pop(CPUARMState
*env
)
2798 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
2801 val
= ldl_phys(cs
->as
, env
->regs
[13]);
2806 /* Switch to V7M main or process stack pointer. */
2807 static void switch_v7m_sp(CPUARMState
*env
, int process
)
2810 if (env
->v7m
.current_sp
!= process
) {
2811 tmp
= env
->v7m
.other_sp
;
2812 env
->v7m
.other_sp
= env
->regs
[13];
2813 env
->regs
[13] = tmp
;
2814 env
->v7m
.current_sp
= process
;
2818 static void do_v7m_exception_exit(CPUARMState
*env
)
2823 type
= env
->regs
[15];
2824 if (env
->v7m
.exception
!= 0)
2825 armv7m_nvic_complete_irq(env
->nvic
, env
->v7m
.exception
);
2827 /* Switch to the target stack. */
2828 switch_v7m_sp(env
, (type
& 4) != 0);
2829 /* Pop registers. */
2830 env
->regs
[0] = v7m_pop(env
);
2831 env
->regs
[1] = v7m_pop(env
);
2832 env
->regs
[2] = v7m_pop(env
);
2833 env
->regs
[3] = v7m_pop(env
);
2834 env
->regs
[12] = v7m_pop(env
);
2835 env
->regs
[14] = v7m_pop(env
);
2836 env
->regs
[15] = v7m_pop(env
);
2837 xpsr
= v7m_pop(env
);
2838 xpsr_write(env
, xpsr
, 0xfffffdff);
2839 /* Undo stack alignment. */
2842 /* ??? The exception return type specifies Thread/Handler mode. However
2843 this is also implied by the xPSR value. Not sure what to do
2844 if there is a mismatch. */
2845 /* ??? Likewise for mismatches between the CONTROL register and the stack
2849 /* Exception names for debug logging; note that not all of these
2850 * precisely correspond to architectural exceptions.
2852 static const char * const excnames
[] = {
2853 [EXCP_UDEF
] = "Undefined Instruction",
2855 [EXCP_PREFETCH_ABORT
] = "Prefetch Abort",
2856 [EXCP_DATA_ABORT
] = "Data Abort",
2859 [EXCP_BKPT
] = "Breakpoint",
2860 [EXCP_EXCEPTION_EXIT
] = "QEMU v7M exception exit",
2861 [EXCP_KERNEL_TRAP
] = "QEMU intercept of kernel commpage",
2862 [EXCP_STREX
] = "QEMU intercept of STREX",
2865 static inline void arm_log_exception(int idx
)
2867 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
2868 const char *exc
= NULL
;
2870 if (idx
>= 0 && idx
< ARRAY_SIZE(excnames
)) {
2871 exc
= excnames
[idx
];
2876 qemu_log_mask(CPU_LOG_INT
, "Taking exception %d [%s]\n", idx
, exc
);
2880 void arm_v7m_cpu_do_interrupt(CPUState
*cs
)
2882 ARMCPU
*cpu
= ARM_CPU(cs
);
2883 CPUARMState
*env
= &cpu
->env
;
2884 uint32_t xpsr
= xpsr_read(env
);
2888 arm_log_exception(cs
->exception_index
);
2891 if (env
->v7m
.current_sp
)
2893 if (env
->v7m
.exception
== 0)
2896 /* For exceptions we just mark as pending on the NVIC, and let that
2898 /* TODO: Need to escalate if the current priority is higher than the
2899 one we're raising. */
2900 switch (cs
->exception_index
) {
2902 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
);
2905 /* The PC already points to the next instruction. */
2906 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SVC
);
2908 case EXCP_PREFETCH_ABORT
:
2909 case EXCP_DATA_ABORT
:
2910 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
);
2913 if (semihosting_enabled
) {
2915 nr
= arm_lduw_code(env
, env
->regs
[15], env
->bswap_code
) & 0xff;
2918 env
->regs
[0] = do_arm_semihosting(env
);
2919 qemu_log_mask(CPU_LOG_INT
, "...handled as semihosting call\n");
2923 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_DEBUG
);
2926 env
->v7m
.exception
= armv7m_nvic_acknowledge_irq(env
->nvic
);
2928 case EXCP_EXCEPTION_EXIT
:
2929 do_v7m_exception_exit(env
);
2932 cpu_abort(cs
, "Unhandled exception 0x%x\n", cs
->exception_index
);
2933 return; /* Never happens. Keep compiler happy. */
2936 /* Align stack pointer. */
2937 /* ??? Should only do this if Configuration Control Register
2938 STACKALIGN bit is set. */
2939 if (env
->regs
[13] & 4) {
2943 /* Switch to the handler mode. */
2944 v7m_push(env
, xpsr
);
2945 v7m_push(env
, env
->regs
[15]);
2946 v7m_push(env
, env
->regs
[14]);
2947 v7m_push(env
, env
->regs
[12]);
2948 v7m_push(env
, env
->regs
[3]);
2949 v7m_push(env
, env
->regs
[2]);
2950 v7m_push(env
, env
->regs
[1]);
2951 v7m_push(env
, env
->regs
[0]);
2952 switch_v7m_sp(env
, 0);
2954 env
->condexec_bits
= 0;
2956 addr
= ldl_phys(cs
->as
, env
->v7m
.vecbase
+ env
->v7m
.exception
* 4);
2957 env
->regs
[15] = addr
& 0xfffffffe;
2958 env
->thumb
= addr
& 1;
2961 /* Handle a CPU exception. */
2962 void arm_cpu_do_interrupt(CPUState
*cs
)
2964 ARMCPU
*cpu
= ARM_CPU(cs
);
2965 CPUARMState
*env
= &cpu
->env
;
2973 arm_log_exception(cs
->exception_index
);
2975 /* TODO: Vectored interrupt controller. */
2976 switch (cs
->exception_index
) {
2978 new_mode
= ARM_CPU_MODE_UND
;
2987 if (semihosting_enabled
) {
2988 /* Check for semihosting interrupt. */
2990 mask
= arm_lduw_code(env
, env
->regs
[15] - 2, env
->bswap_code
)
2993 mask
= arm_ldl_code(env
, env
->regs
[15] - 4, env
->bswap_code
)
2996 /* Only intercept calls from privileged modes, to provide some
2997 semblance of security. */
2998 if (((mask
== 0x123456 && !env
->thumb
)
2999 || (mask
== 0xab && env
->thumb
))
3000 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
3001 env
->regs
[0] = do_arm_semihosting(env
);
3002 qemu_log_mask(CPU_LOG_INT
, "...handled as semihosting call\n");
3006 new_mode
= ARM_CPU_MODE_SVC
;
3009 /* The PC already points to the next instruction. */
3013 /* See if this is a semihosting syscall. */
3014 if (env
->thumb
&& semihosting_enabled
) {
3015 mask
= arm_lduw_code(env
, env
->regs
[15], env
->bswap_code
) & 0xff;
3017 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
3019 env
->regs
[0] = do_arm_semihosting(env
);
3020 qemu_log_mask(CPU_LOG_INT
, "...handled as semihosting call\n");
3024 env
->cp15
.c5_insn
= 2;
3025 /* Fall through to prefetch abort. */
3026 case EXCP_PREFETCH_ABORT
:
3027 qemu_log_mask(CPU_LOG_INT
, "...with IFSR 0x%x IFAR 0x%x\n",
3028 env
->cp15
.c5_insn
, env
->cp15
.c6_insn
);
3029 new_mode
= ARM_CPU_MODE_ABT
;
3031 mask
= CPSR_A
| CPSR_I
;
3034 case EXCP_DATA_ABORT
:
3035 qemu_log_mask(CPU_LOG_INT
, "...with DFSR 0x%x DFAR 0x%x\n",
3036 env
->cp15
.c5_data
, env
->cp15
.c6_data
);
3037 new_mode
= ARM_CPU_MODE_ABT
;
3039 mask
= CPSR_A
| CPSR_I
;
3043 new_mode
= ARM_CPU_MODE_IRQ
;
3045 /* Disable IRQ and imprecise data aborts. */
3046 mask
= CPSR_A
| CPSR_I
;
3050 new_mode
= ARM_CPU_MODE_FIQ
;
3052 /* Disable FIQ, IRQ and imprecise data aborts. */
3053 mask
= CPSR_A
| CPSR_I
| CPSR_F
;
3057 cpu_abort(cs
, "Unhandled exception 0x%x\n", cs
->exception_index
);
3058 return; /* Never happens. Keep compiler happy. */
3061 if (env
->cp15
.c1_sys
& SCTLR_V
) {
3062 /* when enabled, base address cannot be remapped. */
3065 /* ARM v7 architectures provide a vector base address register to remap
3066 * the interrupt vector table.
3067 * This register is only followed in non-monitor mode, and has a secure
3068 * and un-secure copy. Since the cpu is always in a un-secure operation
3069 * and is never in monitor mode this feature is always active.
3070 * Note: only bits 31:5 are valid.
3072 addr
+= env
->cp15
.c12_vbar
;
3074 switch_mode (env
, new_mode
);
3075 env
->spsr
= cpsr_read(env
);
3076 /* Clear IT bits. */
3077 env
->condexec_bits
= 0;
3078 /* Switch to the new mode, and to the correct instruction set. */
3079 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~CPSR_M
) | new_mode
;
3081 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
3082 * and we should just guard the thumb mode on V4 */
3083 if (arm_feature(env
, ARM_FEATURE_V4T
)) {
3084 env
->thumb
= (env
->cp15
.c1_sys
& SCTLR_TE
) != 0;
3086 env
->regs
[14] = env
->regs
[15] + offset
;
3087 env
->regs
[15] = addr
;
3088 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
3091 /* Check section/page access permissions.
3092 Returns the page protection flags, or zero if the access is not
3094 static inline int check_ap(CPUARMState
*env
, int ap
, int domain_prot
,
3095 int access_type
, int is_user
)
3099 if (domain_prot
== 3) {
3100 return PAGE_READ
| PAGE_WRITE
;
3103 if (access_type
== 1)
3106 prot_ro
= PAGE_READ
;
3110 if (arm_feature(env
, ARM_FEATURE_V7
)) {
3113 if (access_type
== 1)
3115 switch (env
->cp15
.c1_sys
& (SCTLR_S
| SCTLR_R
)) {
3117 return is_user
? 0 : PAGE_READ
;
3124 return is_user
? 0 : PAGE_READ
| PAGE_WRITE
;
3129 return PAGE_READ
| PAGE_WRITE
;
3131 return PAGE_READ
| PAGE_WRITE
;
3132 case 4: /* Reserved. */
3135 return is_user
? 0 : prot_ro
;
3139 if (!arm_feature (env
, ARM_FEATURE_V6K
))
3147 static uint32_t get_level1_table_address(CPUARMState
*env
, uint32_t address
)
3151 if (address
& env
->cp15
.c2_mask
)
3152 table
= env
->cp15
.ttbr1_el1
& 0xffffc000;
3154 table
= env
->cp15
.ttbr0_el1
& env
->cp15
.c2_base_mask
;
3156 table
|= (address
>> 18) & 0x3ffc;
3160 static int get_phys_addr_v5(CPUARMState
*env
, uint32_t address
, int access_type
,
3161 int is_user
, hwaddr
*phys_ptr
,
3162 int *prot
, target_ulong
*page_size
)
3164 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
3174 /* Pagetable walk. */
3175 /* Lookup l1 descriptor. */
3176 table
= get_level1_table_address(env
, address
);
3177 desc
= ldl_phys(cs
->as
, table
);
3179 domain
= (desc
>> 5) & 0x0f;
3180 domain_prot
= (env
->cp15
.c3
>> (domain
* 2)) & 3;
3182 /* Section translation fault. */
3186 if (domain_prot
== 0 || domain_prot
== 2) {
3188 code
= 9; /* Section domain fault. */
3190 code
= 11; /* Page domain fault. */
3195 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
3196 ap
= (desc
>> 10) & 3;
3198 *page_size
= 1024 * 1024;
3200 /* Lookup l2 entry. */
3202 /* Coarse pagetable. */
3203 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
3205 /* Fine pagetable. */
3206 table
= (desc
& 0xfffff000) | ((address
>> 8) & 0xffc);
3208 desc
= ldl_phys(cs
->as
, table
);
3210 case 0: /* Page translation fault. */
3213 case 1: /* 64k page. */
3214 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
3215 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
3216 *page_size
= 0x10000;
3218 case 2: /* 4k page. */
3219 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
3220 ap
= (desc
>> (4 + ((address
>> 9) & 6))) & 3;
3221 *page_size
= 0x1000;
3223 case 3: /* 1k page. */
3225 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
3226 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
3228 /* Page translation fault. */
3233 phys_addr
= (desc
& 0xfffffc00) | (address
& 0x3ff);
3235 ap
= (desc
>> 4) & 3;
3239 /* Never happens, but compiler isn't smart enough to tell. */
3244 *prot
= check_ap(env
, ap
, domain_prot
, access_type
, is_user
);
3246 /* Access permission fault. */
3250 *phys_ptr
= phys_addr
;
3253 return code
| (domain
<< 4);
3256 static int get_phys_addr_v6(CPUARMState
*env
, uint32_t address
, int access_type
,
3257 int is_user
, hwaddr
*phys_ptr
,
3258 int *prot
, target_ulong
*page_size
)
3260 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
3272 /* Pagetable walk. */
3273 /* Lookup l1 descriptor. */
3274 table
= get_level1_table_address(env
, address
);
3275 desc
= ldl_phys(cs
->as
, table
);
3277 if (type
== 0 || (type
== 3 && !arm_feature(env
, ARM_FEATURE_PXN
))) {
3278 /* Section translation fault, or attempt to use the encoding
3279 * which is Reserved on implementations without PXN.
3284 if ((type
== 1) || !(desc
& (1 << 18))) {
3285 /* Page or Section. */
3286 domain
= (desc
>> 5) & 0x0f;
3288 domain_prot
= (env
->cp15
.c3
>> (domain
* 2)) & 3;
3289 if (domain_prot
== 0 || domain_prot
== 2) {
3291 code
= 9; /* Section domain fault. */
3293 code
= 11; /* Page domain fault. */
3298 if (desc
& (1 << 18)) {
3300 phys_addr
= (desc
& 0xff000000) | (address
& 0x00ffffff);
3301 *page_size
= 0x1000000;
3304 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
3305 *page_size
= 0x100000;
3307 ap
= ((desc
>> 10) & 3) | ((desc
>> 13) & 4);
3308 xn
= desc
& (1 << 4);
3312 if (arm_feature(env
, ARM_FEATURE_PXN
)) {
3313 pxn
= (desc
>> 2) & 1;
3315 /* Lookup l2 entry. */
3316 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
3317 desc
= ldl_phys(cs
->as
, table
);
3318 ap
= ((desc
>> 4) & 3) | ((desc
>> 7) & 4);
3320 case 0: /* Page translation fault. */
3323 case 1: /* 64k page. */
3324 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
3325 xn
= desc
& (1 << 15);
3326 *page_size
= 0x10000;
3328 case 2: case 3: /* 4k page. */
3329 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
3331 *page_size
= 0x1000;
3334 /* Never happens, but compiler isn't smart enough to tell. */
3339 if (domain_prot
== 3) {
3340 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
3342 if (pxn
&& !is_user
) {
3345 if (xn
&& access_type
== 2)
3348 /* The simplified model uses AP[0] as an access control bit. */
3349 if ((env
->cp15
.c1_sys
& SCTLR_AFE
) && (ap
& 1) == 0) {
3350 /* Access flag fault. */
3351 code
= (code
== 15) ? 6 : 3;
3354 *prot
= check_ap(env
, ap
, domain_prot
, access_type
, is_user
);
3356 /* Access permission fault. */
3363 *phys_ptr
= phys_addr
;
3366 return code
| (domain
<< 4);
3369 /* Fault type for long-descriptor MMU fault reporting; this corresponds
3370 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
3373 translation_fault
= 1,
3375 permission_fault
= 3,
3378 static int get_phys_addr_lpae(CPUARMState
*env
, uint32_t address
,
3379 int access_type
, int is_user
,
3380 hwaddr
*phys_ptr
, int *prot
,
3381 target_ulong
*page_size_ptr
)
3383 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
3384 /* Read an LPAE long-descriptor translation table. */
3385 MMUFaultType fault_type
= translation_fault
;
3393 uint32_t tableattrs
;
3394 target_ulong page_size
;
3397 /* Determine whether this address is in the region controlled by
3398 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
3399 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
3400 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
3402 uint32_t t0sz
= extract32(env
->cp15
.c2_control
, 0, 3);
3403 uint32_t t1sz
= extract32(env
->cp15
.c2_control
, 16, 3);
3404 if (t0sz
&& !extract32(address
, 32 - t0sz
, t0sz
)) {
3405 /* there is a ttbr0 region and we are in it (high bits all zero) */
3407 } else if (t1sz
&& !extract32(~address
, 32 - t1sz
, t1sz
)) {
3408 /* there is a ttbr1 region and we are in it (high bits all one) */
3411 /* ttbr0 region is "everything not in the ttbr1 region" */
3414 /* ttbr1 region is "everything not in the ttbr0 region" */
3417 /* in the gap between the two regions, this is a Translation fault */
3418 fault_type
= translation_fault
;
3422 /* Note that QEMU ignores shareability and cacheability attributes,
3423 * so we don't need to do anything with the SH, ORGN, IRGN fields
3424 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
3425 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
3426 * implement any ASID-like capability so we can ignore it (instead
3427 * we will always flush the TLB any time the ASID is changed).
3429 if (ttbr_select
== 0) {
3430 ttbr
= env
->cp15
.ttbr0_el1
;
3431 epd
= extract32(env
->cp15
.c2_control
, 7, 1);
3434 ttbr
= env
->cp15
.ttbr1_el1
;
3435 epd
= extract32(env
->cp15
.c2_control
, 23, 1);
3440 /* Translation table walk disabled => Translation fault on TLB miss */
3444 /* If the region is small enough we will skip straight to a 2nd level
3445 * lookup. This affects the number of bits of the address used in
3446 * combination with the TTBR to find the first descriptor. ('n' here
3447 * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
3448 * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
3457 /* Clear the vaddr bits which aren't part of the within-region address,
3458 * so that we don't have to special case things when calculating the
3459 * first descriptor address.
3461 address
&= (0xffffffffU
>> tsz
);
3463 /* Now we can extract the actual base address from the TTBR */
3464 descaddr
= extract64(ttbr
, 0, 40);
3465 descaddr
&= ~((1ULL << n
) - 1);
3469 uint64_t descriptor
;
3471 descaddr
|= ((address
>> (9 * (4 - level
))) & 0xff8);
3472 descriptor
= ldq_phys(cs
->as
, descaddr
);
3473 if (!(descriptor
& 1) ||
3474 (!(descriptor
& 2) && (level
== 3))) {
3475 /* Invalid, or the Reserved level 3 encoding */
3478 descaddr
= descriptor
& 0xfffffff000ULL
;
3480 if ((descriptor
& 2) && (level
< 3)) {
3481 /* Table entry. The top five bits are attributes which may
3482 * propagate down through lower levels of the table (and
3483 * which are all arranged so that 0 means "no effect", so
3484 * we can gather them up by ORing in the bits at each level).
3486 tableattrs
|= extract64(descriptor
, 59, 5);
3490 /* Block entry at level 1 or 2, or page entry at level 3.
3491 * These are basically the same thing, although the number
3492 * of bits we pull in from the vaddr varies.
3494 page_size
= (1 << (39 - (9 * level
)));
3495 descaddr
|= (address
& (page_size
- 1));
3496 /* Extract attributes from the descriptor and merge with table attrs */
3497 attrs
= extract64(descriptor
, 2, 10)
3498 | (extract64(descriptor
, 52, 12) << 10);
3499 attrs
|= extract32(tableattrs
, 0, 2) << 11; /* XN, PXN */
3500 attrs
|= extract32(tableattrs
, 3, 1) << 5; /* APTable[1] => AP[2] */
3501 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
3502 * means "force PL1 access only", which means forcing AP[1] to 0.
3504 if (extract32(tableattrs
, 2, 1)) {
3507 /* Since we're always in the Non-secure state, NSTable is ignored. */
3510 /* Here descaddr is the final physical address, and attributes
3513 fault_type
= access_fault
;
3514 if ((attrs
& (1 << 8)) == 0) {
3518 fault_type
= permission_fault
;
3519 if (is_user
&& !(attrs
& (1 << 4))) {
3520 /* Unprivileged access not enabled */
3523 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
3524 if (attrs
& (1 << 12) || (!is_user
&& (attrs
& (1 << 11)))) {
3526 if (access_type
== 2) {
3529 *prot
&= ~PAGE_EXEC
;
3531 if (attrs
& (1 << 5)) {
3532 /* Write access forbidden */
3533 if (access_type
== 1) {
3536 *prot
&= ~PAGE_WRITE
;
3539 *phys_ptr
= descaddr
;
3540 *page_size_ptr
= page_size
;
3544 /* Long-descriptor format IFSR/DFSR value */
3545 return (1 << 9) | (fault_type
<< 2) | level
;
3548 static int get_phys_addr_mpu(CPUARMState
*env
, uint32_t address
,
3549 int access_type
, int is_user
,
3550 hwaddr
*phys_ptr
, int *prot
)
3556 *phys_ptr
= address
;
3557 for (n
= 7; n
>= 0; n
--) {
3558 base
= env
->cp15
.c6_region
[n
];
3559 if ((base
& 1) == 0)
3561 mask
= 1 << ((base
>> 1) & 0x1f);
3562 /* Keep this shift separate from the above to avoid an
3563 (undefined) << 32. */
3564 mask
= (mask
<< 1) - 1;
3565 if (((base
^ address
) & ~mask
) == 0)
3571 if (access_type
== 2) {
3572 mask
= env
->cp15
.c5_insn
;
3574 mask
= env
->cp15
.c5_data
;
3576 mask
= (mask
>> (n
* 4)) & 0xf;
3583 *prot
= PAGE_READ
| PAGE_WRITE
;
3588 *prot
|= PAGE_WRITE
;
3591 *prot
= PAGE_READ
| PAGE_WRITE
;
3602 /* Bad permission. */
3609 /* get_phys_addr - get the physical address for this virtual address
3611 * Find the physical address corresponding to the given virtual address,
3612 * by doing a translation table walk on MMU based systems or using the
3613 * MPU state on MPU based systems.
3615 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
3616 * prot and page_size are not filled in, and the return value provides
3617 * information on why the translation aborted, in the format of a
3618 * DFSR/IFSR fault register, with the following caveats:
3619 * * we honour the short vs long DFSR format differences.
3620 * * the WnR bit is never set (the caller must do this).
3621 * * for MPU based systems we don't bother to return a full FSR format
3625 * @address: virtual address to get physical address for
3626 * @access_type: 0 for read, 1 for write, 2 for execute
3627 * @is_user: 0 for privileged access, 1 for user
3628 * @phys_ptr: set to the physical address corresponding to the virtual address
3629 * @prot: set to the permissions for the page containing phys_ptr
3630 * @page_size: set to the size of the page containing phys_ptr
3632 static inline int get_phys_addr(CPUARMState
*env
, uint32_t address
,
3633 int access_type
, int is_user
,
3634 hwaddr
*phys_ptr
, int *prot
,
3635 target_ulong
*page_size
)
3637 /* Fast Context Switch Extension. */
3638 if (address
< 0x02000000)
3639 address
+= env
->cp15
.c13_fcse
;
3641 if ((env
->cp15
.c1_sys
& SCTLR_M
) == 0) {
3642 /* MMU/MPU disabled. */
3643 *phys_ptr
= address
;
3644 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
3645 *page_size
= TARGET_PAGE_SIZE
;
3647 } else if (arm_feature(env
, ARM_FEATURE_MPU
)) {
3648 *page_size
= TARGET_PAGE_SIZE
;
3649 return get_phys_addr_mpu(env
, address
, access_type
, is_user
, phys_ptr
,
3651 } else if (extended_addresses_enabled(env
)) {
3652 return get_phys_addr_lpae(env
, address
, access_type
, is_user
, phys_ptr
,
3654 } else if (env
->cp15
.c1_sys
& SCTLR_XP
) {
3655 return get_phys_addr_v6(env
, address
, access_type
, is_user
, phys_ptr
,
3658 return get_phys_addr_v5(env
, address
, access_type
, is_user
, phys_ptr
,
3663 int arm_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
,
3664 int access_type
, int mmu_idx
)
3666 ARMCPU
*cpu
= ARM_CPU(cs
);
3667 CPUARMState
*env
= &cpu
->env
;
3669 target_ulong page_size
;
3673 is_user
= mmu_idx
== MMU_USER_IDX
;
3674 ret
= get_phys_addr(env
, address
, access_type
, is_user
, &phys_addr
, &prot
,
3677 /* Map a single [sub]page. */
3678 phys_addr
&= ~(hwaddr
)0x3ff;
3679 address
&= ~(uint32_t)0x3ff;
3680 tlb_set_page(cs
, address
, phys_addr
, prot
, mmu_idx
, page_size
);
3684 if (access_type
== 2) {
3685 env
->cp15
.c5_insn
= ret
;
3686 env
->cp15
.c6_insn
= address
;
3687 cs
->exception_index
= EXCP_PREFETCH_ABORT
;
3689 env
->cp15
.c5_data
= ret
;
3690 if (access_type
== 1 && arm_feature(env
, ARM_FEATURE_V6
))
3691 env
->cp15
.c5_data
|= (1 << 11);
3692 env
->cp15
.c6_data
= address
;
3693 cs
->exception_index
= EXCP_DATA_ABORT
;
3698 hwaddr
arm_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
3700 ARMCPU
*cpu
= ARM_CPU(cs
);
3702 target_ulong page_size
;
3706 ret
= get_phys_addr(&cpu
->env
, addr
, 0, 0, &phys_addr
, &prot
, &page_size
);
3715 void HELPER(set_r13_banked
)(CPUARMState
*env
, uint32_t mode
, uint32_t val
)
3717 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
3718 env
->regs
[13] = val
;
3720 env
->banked_r13
[bank_number(mode
)] = val
;
3724 uint32_t HELPER(get_r13_banked
)(CPUARMState
*env
, uint32_t mode
)
3726 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
3727 return env
->regs
[13];
3729 return env
->banked_r13
[bank_number(mode
)];
3733 uint32_t HELPER(v7m_mrs
)(CPUARMState
*env
, uint32_t reg
)
3735 ARMCPU
*cpu
= arm_env_get_cpu(env
);
3739 return xpsr_read(env
) & 0xf8000000;
3741 return xpsr_read(env
) & 0xf80001ff;
3743 return xpsr_read(env
) & 0xff00fc00;
3745 return xpsr_read(env
) & 0xff00fdff;
3747 return xpsr_read(env
) & 0x000001ff;
3749 return xpsr_read(env
) & 0x0700fc00;
3751 return xpsr_read(env
) & 0x0700edff;
3753 return env
->v7m
.current_sp
? env
->v7m
.other_sp
: env
->regs
[13];
3755 return env
->v7m
.current_sp
? env
->regs
[13] : env
->v7m
.other_sp
;
3756 case 16: /* PRIMASK */
3757 return (env
->daif
& PSTATE_I
) != 0;
3758 case 17: /* BASEPRI */
3759 case 18: /* BASEPRI_MAX */
3760 return env
->v7m
.basepri
;
3761 case 19: /* FAULTMASK */
3762 return (env
->daif
& PSTATE_F
) != 0;
3763 case 20: /* CONTROL */
3764 return env
->v7m
.control
;
3766 /* ??? For debugging only. */
3767 cpu_abort(CPU(cpu
), "Unimplemented system register read (%d)\n", reg
);
3772 void HELPER(v7m_msr
)(CPUARMState
*env
, uint32_t reg
, uint32_t val
)
3774 ARMCPU
*cpu
= arm_env_get_cpu(env
);
3778 xpsr_write(env
, val
, 0xf8000000);
3781 xpsr_write(env
, val
, 0xf8000000);
3784 xpsr_write(env
, val
, 0xfe00fc00);
3787 xpsr_write(env
, val
, 0xfe00fc00);
3790 /* IPSR bits are readonly. */
3793 xpsr_write(env
, val
, 0x0600fc00);
3796 xpsr_write(env
, val
, 0x0600fc00);
3799 if (env
->v7m
.current_sp
)
3800 env
->v7m
.other_sp
= val
;
3802 env
->regs
[13] = val
;
3805 if (env
->v7m
.current_sp
)
3806 env
->regs
[13] = val
;
3808 env
->v7m
.other_sp
= val
;
3810 case 16: /* PRIMASK */
3812 env
->daif
|= PSTATE_I
;
3814 env
->daif
&= ~PSTATE_I
;
3817 case 17: /* BASEPRI */
3818 env
->v7m
.basepri
= val
& 0xff;
3820 case 18: /* BASEPRI_MAX */
3822 if (val
!= 0 && (val
< env
->v7m
.basepri
|| env
->v7m
.basepri
== 0))
3823 env
->v7m
.basepri
= val
;
3825 case 19: /* FAULTMASK */
3827 env
->daif
|= PSTATE_F
;
3829 env
->daif
&= ~PSTATE_F
;
3832 case 20: /* CONTROL */
3833 env
->v7m
.control
= val
& 3;
3834 switch_v7m_sp(env
, (val
& 2) != 0);
3837 /* ??? For debugging only. */
3838 cpu_abort(CPU(cpu
), "Unimplemented system register write (%d)\n", reg
);
3845 /* Note that signed overflow is undefined in C. The following routines are
3846 careful to use unsigned types where modulo arithmetic is required.
3847 Failure to do so _will_ break on newer gcc. */
3849 /* Signed saturating arithmetic. */
3851 /* Perform 16-bit signed saturating addition. */
3852 static inline uint16_t add16_sat(uint16_t a
, uint16_t b
)
3857 if (((res
^ a
) & 0x8000) && !((a
^ b
) & 0x8000)) {
3866 /* Perform 8-bit signed saturating addition. */
3867 static inline uint8_t add8_sat(uint8_t a
, uint8_t b
)
3872 if (((res
^ a
) & 0x80) && !((a
^ b
) & 0x80)) {
3881 /* Perform 16-bit signed saturating subtraction. */
3882 static inline uint16_t sub16_sat(uint16_t a
, uint16_t b
)
3887 if (((res
^ a
) & 0x8000) && ((a
^ b
) & 0x8000)) {
3896 /* Perform 8-bit signed saturating subtraction. */
3897 static inline uint8_t sub8_sat(uint8_t a
, uint8_t b
)
3902 if (((res
^ a
) & 0x80) && ((a
^ b
) & 0x80)) {
3911 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3912 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3913 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
3914 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
3917 #include "op_addsub.h"
3919 /* Unsigned saturating arithmetic. */
3920 static inline uint16_t add16_usat(uint16_t a
, uint16_t b
)
3929 static inline uint16_t sub16_usat(uint16_t a
, uint16_t b
)
3937 static inline uint8_t add8_usat(uint8_t a
, uint8_t b
)
3946 static inline uint8_t sub8_usat(uint8_t a
, uint8_t b
)
3954 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3955 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3956 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
3957 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
3960 #include "op_addsub.h"
3962 /* Signed modulo arithmetic. */
3963 #define SARITH16(a, b, n, op) do { \
3965 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
3966 RESULT(sum, n, 16); \
3968 ge |= 3 << (n * 2); \
3971 #define SARITH8(a, b, n, op) do { \
3973 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
3974 RESULT(sum, n, 8); \
3980 #define ADD16(a, b, n) SARITH16(a, b, n, +)
3981 #define SUB16(a, b, n) SARITH16(a, b, n, -)
3982 #define ADD8(a, b, n) SARITH8(a, b, n, +)
3983 #define SUB8(a, b, n) SARITH8(a, b, n, -)
3987 #include "op_addsub.h"
3989 /* Unsigned modulo arithmetic. */
3990 #define ADD16(a, b, n) do { \
3992 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3993 RESULT(sum, n, 16); \
3994 if ((sum >> 16) == 1) \
3995 ge |= 3 << (n * 2); \
3998 #define ADD8(a, b, n) do { \
4000 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
4001 RESULT(sum, n, 8); \
4002 if ((sum >> 8) == 1) \
4006 #define SUB16(a, b, n) do { \
4008 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
4009 RESULT(sum, n, 16); \
4010 if ((sum >> 16) == 0) \
4011 ge |= 3 << (n * 2); \
4014 #define SUB8(a, b, n) do { \
4016 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
4017 RESULT(sum, n, 8); \
4018 if ((sum >> 8) == 0) \
4025 #include "op_addsub.h"
4027 /* Halved signed arithmetic. */
4028 #define ADD16(a, b, n) \
4029 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
4030 #define SUB16(a, b, n) \
4031 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
4032 #define ADD8(a, b, n) \
4033 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
4034 #define SUB8(a, b, n) \
4035 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
4038 #include "op_addsub.h"
4040 /* Halved unsigned arithmetic. */
4041 #define ADD16(a, b, n) \
4042 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
4043 #define SUB16(a, b, n) \
4044 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
4045 #define ADD8(a, b, n) \
4046 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
4047 #define SUB8(a, b, n) \
4048 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
4051 #include "op_addsub.h"
4053 static inline uint8_t do_usad(uint8_t a
, uint8_t b
)
4061 /* Unsigned sum of absolute byte differences. */
4062 uint32_t HELPER(usad8
)(uint32_t a
, uint32_t b
)
4065 sum
= do_usad(a
, b
);
4066 sum
+= do_usad(a
>> 8, b
>> 8);
4067 sum
+= do_usad(a
>> 16, b
>>16);
4068 sum
+= do_usad(a
>> 24, b
>> 24);
4072 /* For ARMv6 SEL instruction. */
4073 uint32_t HELPER(sel_flags
)(uint32_t flags
, uint32_t a
, uint32_t b
)
4086 return (a
& mask
) | (b
& ~mask
);
4089 /* VFP support. We follow the convention used for VFP instructions:
4090 Single precision routines have a "s" suffix, double precision a
4093 /* Convert host exception flags to vfp form. */
4094 static inline int vfp_exceptbits_from_host(int host_bits
)
4096 int target_bits
= 0;
4098 if (host_bits
& float_flag_invalid
)
4100 if (host_bits
& float_flag_divbyzero
)
4102 if (host_bits
& float_flag_overflow
)
4104 if (host_bits
& (float_flag_underflow
| float_flag_output_denormal
))
4106 if (host_bits
& float_flag_inexact
)
4107 target_bits
|= 0x10;
4108 if (host_bits
& float_flag_input_denormal
)
4109 target_bits
|= 0x80;
4113 uint32_t HELPER(vfp_get_fpscr
)(CPUARMState
*env
)
4118 fpscr
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & 0xffc8ffff)
4119 | (env
->vfp
.vec_len
<< 16)
4120 | (env
->vfp
.vec_stride
<< 20);
4121 i
= get_float_exception_flags(&env
->vfp
.fp_status
);
4122 i
|= get_float_exception_flags(&env
->vfp
.standard_fp_status
);
4123 fpscr
|= vfp_exceptbits_from_host(i
);
4127 uint32_t vfp_get_fpscr(CPUARMState
*env
)
4129 return HELPER(vfp_get_fpscr
)(env
);
4132 /* Convert vfp exception flags to target form. */
4133 static inline int vfp_exceptbits_to_host(int target_bits
)
4137 if (target_bits
& 1)
4138 host_bits
|= float_flag_invalid
;
4139 if (target_bits
& 2)
4140 host_bits
|= float_flag_divbyzero
;
4141 if (target_bits
& 4)
4142 host_bits
|= float_flag_overflow
;
4143 if (target_bits
& 8)
4144 host_bits
|= float_flag_underflow
;
4145 if (target_bits
& 0x10)
4146 host_bits
|= float_flag_inexact
;
4147 if (target_bits
& 0x80)
4148 host_bits
|= float_flag_input_denormal
;
4152 void HELPER(vfp_set_fpscr
)(CPUARMState
*env
, uint32_t val
)
4157 changed
= env
->vfp
.xregs
[ARM_VFP_FPSCR
];
4158 env
->vfp
.xregs
[ARM_VFP_FPSCR
] = (val
& 0xffc8ffff);
4159 env
->vfp
.vec_len
= (val
>> 16) & 7;
4160 env
->vfp
.vec_stride
= (val
>> 20) & 3;
4163 if (changed
& (3 << 22)) {
4164 i
= (val
>> 22) & 3;
4166 case FPROUNDING_TIEEVEN
:
4167 i
= float_round_nearest_even
;
4169 case FPROUNDING_POSINF
:
4172 case FPROUNDING_NEGINF
:
4173 i
= float_round_down
;
4175 case FPROUNDING_ZERO
:
4176 i
= float_round_to_zero
;
4179 set_float_rounding_mode(i
, &env
->vfp
.fp_status
);
4181 if (changed
& (1 << 24)) {
4182 set_flush_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
4183 set_flush_inputs_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
4185 if (changed
& (1 << 25))
4186 set_default_nan_mode((val
& (1 << 25)) != 0, &env
->vfp
.fp_status
);
4188 i
= vfp_exceptbits_to_host(val
);
4189 set_float_exception_flags(i
, &env
->vfp
.fp_status
);
4190 set_float_exception_flags(0, &env
->vfp
.standard_fp_status
);
4193 void vfp_set_fpscr(CPUARMState
*env
, uint32_t val
)
4195 HELPER(vfp_set_fpscr
)(env
, val
);
4198 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
4200 #define VFP_BINOP(name) \
4201 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
4203 float_status *fpst = fpstp; \
4204 return float32_ ## name(a, b, fpst); \
4206 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
4208 float_status *fpst = fpstp; \
4209 return float64_ ## name(a, b, fpst); \
4221 float32
VFP_HELPER(neg
, s
)(float32 a
)
4223 return float32_chs(a
);
4226 float64
VFP_HELPER(neg
, d
)(float64 a
)
4228 return float64_chs(a
);
4231 float32
VFP_HELPER(abs
, s
)(float32 a
)
4233 return float32_abs(a
);
4236 float64
VFP_HELPER(abs
, d
)(float64 a
)
4238 return float64_abs(a
);
4241 float32
VFP_HELPER(sqrt
, s
)(float32 a
, CPUARMState
*env
)
4243 return float32_sqrt(a
, &env
->vfp
.fp_status
);
4246 float64
VFP_HELPER(sqrt
, d
)(float64 a
, CPUARMState
*env
)
4248 return float64_sqrt(a
, &env
->vfp
.fp_status
);
4251 /* XXX: check quiet/signaling case */
4252 #define DO_VFP_cmp(p, type) \
4253 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
4256 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
4257 case 0: flags = 0x6; break; \
4258 case -1: flags = 0x8; break; \
4259 case 1: flags = 0x2; break; \
4260 default: case 2: flags = 0x3; break; \
4262 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
4263 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
4265 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
4268 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
4269 case 0: flags = 0x6; break; \
4270 case -1: flags = 0x8; break; \
4271 case 1: flags = 0x2; break; \
4272 default: case 2: flags = 0x3; break; \
4274 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
4275 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
4277 DO_VFP_cmp(s
, float32
)
4278 DO_VFP_cmp(d
, float64
)
4281 /* Integer to float and float to integer conversions */
4283 #define CONV_ITOF(name, fsz, sign) \
4284 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
4286 float_status *fpst = fpstp; \
4287 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
4290 #define CONV_FTOI(name, fsz, sign, round) \
4291 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
4293 float_status *fpst = fpstp; \
4294 if (float##fsz##_is_any_nan(x)) { \
4295 float_raise(float_flag_invalid, fpst); \
4298 return float##fsz##_to_##sign##int32##round(x, fpst); \
4301 #define FLOAT_CONVS(name, p, fsz, sign) \
4302 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
4303 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
4304 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
4306 FLOAT_CONVS(si
, s
, 32, )
4307 FLOAT_CONVS(si
, d
, 64, )
4308 FLOAT_CONVS(ui
, s
, 32, u
)
4309 FLOAT_CONVS(ui
, d
, 64, u
)
4315 /* floating point conversion */
4316 float64
VFP_HELPER(fcvtd
, s
)(float32 x
, CPUARMState
*env
)
4318 float64 r
= float32_to_float64(x
, &env
->vfp
.fp_status
);
4319 /* ARM requires that S<->D conversion of any kind of NaN generates
4320 * a quiet NaN by forcing the most significant frac bit to 1.
4322 return float64_maybe_silence_nan(r
);
4325 float32
VFP_HELPER(fcvts
, d
)(float64 x
, CPUARMState
*env
)
4327 float32 r
= float64_to_float32(x
, &env
->vfp
.fp_status
);
4328 /* ARM requires that S<->D conversion of any kind of NaN generates
4329 * a quiet NaN by forcing the most significant frac bit to 1.
4331 return float32_maybe_silence_nan(r
);
4334 /* VFP3 fixed point conversion. */
4335 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
4336 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
4339 float_status *fpst = fpstp; \
4341 tmp = itype##_to_##float##fsz(x, fpst); \
4342 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
4345 /* Notice that we want only input-denormal exception flags from the
4346 * scalbn operation: the other possible flags (overflow+inexact if
4347 * we overflow to infinity, output-denormal) aren't correct for the
4348 * complete scale-and-convert operation.
4350 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
4351 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
4355 float_status *fpst = fpstp; \
4356 int old_exc_flags = get_float_exception_flags(fpst); \
4358 if (float##fsz##_is_any_nan(x)) { \
4359 float_raise(float_flag_invalid, fpst); \
4362 tmp = float##fsz##_scalbn(x, shift, fpst); \
4363 old_exc_flags |= get_float_exception_flags(fpst) \
4364 & float_flag_input_denormal; \
4365 set_float_exception_flags(old_exc_flags, fpst); \
4366 return float##fsz##_to_##itype##round(tmp, fpst); \
4369 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
4370 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
4371 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
4372 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
4374 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
4375 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
4376 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
4378 VFP_CONV_FIX(sh
, d
, 64, 64, int16
)
4379 VFP_CONV_FIX(sl
, d
, 64, 64, int32
)
4380 VFP_CONV_FIX_A64(sq
, d
, 64, 64, int64
)
4381 VFP_CONV_FIX(uh
, d
, 64, 64, uint16
)
4382 VFP_CONV_FIX(ul
, d
, 64, 64, uint32
)
4383 VFP_CONV_FIX_A64(uq
, d
, 64, 64, uint64
)
4384 VFP_CONV_FIX(sh
, s
, 32, 32, int16
)
4385 VFP_CONV_FIX(sl
, s
, 32, 32, int32
)
4386 VFP_CONV_FIX_A64(sq
, s
, 32, 64, int64
)
4387 VFP_CONV_FIX(uh
, s
, 32, 32, uint16
)
4388 VFP_CONV_FIX(ul
, s
, 32, 32, uint32
)
4389 VFP_CONV_FIX_A64(uq
, s
, 32, 64, uint64
)
4391 #undef VFP_CONV_FIX_FLOAT
4392 #undef VFP_CONV_FLOAT_FIX_ROUND
4394 /* Set the current fp rounding mode and return the old one.
4395 * The argument is a softfloat float_round_ value.
4397 uint32_t HELPER(set_rmode
)(uint32_t rmode
, CPUARMState
*env
)
4399 float_status
*fp_status
= &env
->vfp
.fp_status
;
4401 uint32_t prev_rmode
= get_float_rounding_mode(fp_status
);
4402 set_float_rounding_mode(rmode
, fp_status
);
4407 /* Set the current fp rounding mode in the standard fp status and return
4408 * the old one. This is for NEON instructions that need to change the
4409 * rounding mode but wish to use the standard FPSCR values for everything
4410 * else. Always set the rounding mode back to the correct value after
4412 * The argument is a softfloat float_round_ value.
4414 uint32_t HELPER(set_neon_rmode
)(uint32_t rmode
, CPUARMState
*env
)
4416 float_status
*fp_status
= &env
->vfp
.standard_fp_status
;
4418 uint32_t prev_rmode
= get_float_rounding_mode(fp_status
);
4419 set_float_rounding_mode(rmode
, fp_status
);
4424 /* Half precision conversions. */
4425 static float32
do_fcvt_f16_to_f32(uint32_t a
, CPUARMState
*env
, float_status
*s
)
4427 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
4428 float32 r
= float16_to_float32(make_float16(a
), ieee
, s
);
4430 return float32_maybe_silence_nan(r
);
4435 static uint32_t do_fcvt_f32_to_f16(float32 a
, CPUARMState
*env
, float_status
*s
)
4437 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
4438 float16 r
= float32_to_float16(a
, ieee
, s
);
4440 r
= float16_maybe_silence_nan(r
);
4442 return float16_val(r
);
4445 float32
HELPER(neon_fcvt_f16_to_f32
)(uint32_t a
, CPUARMState
*env
)
4447 return do_fcvt_f16_to_f32(a
, env
, &env
->vfp
.standard_fp_status
);
4450 uint32_t HELPER(neon_fcvt_f32_to_f16
)(float32 a
, CPUARMState
*env
)
4452 return do_fcvt_f32_to_f16(a
, env
, &env
->vfp
.standard_fp_status
);
4455 float32
HELPER(vfp_fcvt_f16_to_f32
)(uint32_t a
, CPUARMState
*env
)
4457 return do_fcvt_f16_to_f32(a
, env
, &env
->vfp
.fp_status
);
4460 uint32_t HELPER(vfp_fcvt_f32_to_f16
)(float32 a
, CPUARMState
*env
)
4462 return do_fcvt_f32_to_f16(a
, env
, &env
->vfp
.fp_status
);
4465 float64
HELPER(vfp_fcvt_f16_to_f64
)(uint32_t a
, CPUARMState
*env
)
4467 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
4468 float64 r
= float16_to_float64(make_float16(a
), ieee
, &env
->vfp
.fp_status
);
4470 return float64_maybe_silence_nan(r
);
4475 uint32_t HELPER(vfp_fcvt_f64_to_f16
)(float64 a
, CPUARMState
*env
)
4477 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
4478 float16 r
= float64_to_float16(a
, ieee
, &env
->vfp
.fp_status
);
4480 r
= float16_maybe_silence_nan(r
);
4482 return float16_val(r
);
4485 #define float32_two make_float32(0x40000000)
4486 #define float32_three make_float32(0x40400000)
4487 #define float32_one_point_five make_float32(0x3fc00000)
4489 float32
HELPER(recps_f32
)(float32 a
, float32 b
, CPUARMState
*env
)
4491 float_status
*s
= &env
->vfp
.standard_fp_status
;
4492 if ((float32_is_infinity(a
) && float32_is_zero_or_denormal(b
)) ||
4493 (float32_is_infinity(b
) && float32_is_zero_or_denormal(a
))) {
4494 if (!(float32_is_zero(a
) || float32_is_zero(b
))) {
4495 float_raise(float_flag_input_denormal
, s
);
4499 return float32_sub(float32_two
, float32_mul(a
, b
, s
), s
);
4502 float32
HELPER(rsqrts_f32
)(float32 a
, float32 b
, CPUARMState
*env
)
4504 float_status
*s
= &env
->vfp
.standard_fp_status
;
4506 if ((float32_is_infinity(a
) && float32_is_zero_or_denormal(b
)) ||
4507 (float32_is_infinity(b
) && float32_is_zero_or_denormal(a
))) {
4508 if (!(float32_is_zero(a
) || float32_is_zero(b
))) {
4509 float_raise(float_flag_input_denormal
, s
);
4511 return float32_one_point_five
;
4513 product
= float32_mul(a
, b
, s
);
4514 return float32_div(float32_sub(float32_three
, product
, s
), float32_two
, s
);
4519 /* Constants 256 and 512 are used in some helpers; we avoid relying on
4520 * int->float conversions at run-time. */
4521 #define float64_256 make_float64(0x4070000000000000LL)
4522 #define float64_512 make_float64(0x4080000000000000LL)
4523 #define float32_maxnorm make_float32(0x7f7fffff)
4524 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
4526 /* Reciprocal functions
4528 * The algorithm that must be used to calculate the estimate
4529 * is specified by the ARM ARM, see FPRecipEstimate()
4532 static float64
recip_estimate(float64 a
, float_status
*real_fp_status
)
4534 /* These calculations mustn't set any fp exception flags,
4535 * so we use a local copy of the fp_status.
4537 float_status dummy_status
= *real_fp_status
;
4538 float_status
*s
= &dummy_status
;
4539 /* q = (int)(a * 512.0) */
4540 float64 q
= float64_mul(float64_512
, a
, s
);
4541 int64_t q_int
= float64_to_int64_round_to_zero(q
, s
);
4543 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
4544 q
= int64_to_float64(q_int
, s
);
4545 q
= float64_add(q
, float64_half
, s
);
4546 q
= float64_div(q
, float64_512
, s
);
4547 q
= float64_div(float64_one
, q
, s
);
4549 /* s = (int)(256.0 * r + 0.5) */
4550 q
= float64_mul(q
, float64_256
, s
);
4551 q
= float64_add(q
, float64_half
, s
);
4552 q_int
= float64_to_int64_round_to_zero(q
, s
);
4554 /* return (double)s / 256.0 */
4555 return float64_div(int64_to_float64(q_int
, s
), float64_256
, s
);
4558 /* Common wrapper to call recip_estimate */
4559 static float64
call_recip_estimate(float64 num
, int off
, float_status
*fpst
)
4561 uint64_t val64
= float64_val(num
);
4562 uint64_t frac
= extract64(val64
, 0, 52);
4563 int64_t exp
= extract64(val64
, 52, 11);
4565 float64 scaled
, estimate
;
4567 /* Generate the scaled number for the estimate function */
4569 if (extract64(frac
, 51, 1) == 0) {
4571 frac
= extract64(frac
, 0, 50) << 2;
4573 frac
= extract64(frac
, 0, 51) << 1;
4577 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
4578 scaled
= make_float64((0x3feULL
<< 52)
4579 | extract64(frac
, 44, 8) << 44);
4581 estimate
= recip_estimate(scaled
, fpst
);
4583 /* Build new result */
4584 val64
= float64_val(estimate
);
4585 sbit
= 0x8000000000000000ULL
& val64
;
4587 frac
= extract64(val64
, 0, 52);
4590 frac
= 1ULL << 51 | extract64(frac
, 1, 51);
4591 } else if (exp
== -1) {
4592 frac
= 1ULL << 50 | extract64(frac
, 2, 50);
4596 return make_float64(sbit
| (exp
<< 52) | frac
);
4599 static bool round_to_inf(float_status
*fpst
, bool sign_bit
)
4601 switch (fpst
->float_rounding_mode
) {
4602 case float_round_nearest_even
: /* Round to Nearest */
4604 case float_round_up
: /* Round to +Inf */
4606 case float_round_down
: /* Round to -Inf */
4608 case float_round_to_zero
: /* Round to Zero */
4612 g_assert_not_reached();
4615 float32
HELPER(recpe_f32
)(float32 input
, void *fpstp
)
4617 float_status
*fpst
= fpstp
;
4618 float32 f32
= float32_squash_input_denormal(input
, fpst
);
4619 uint32_t f32_val
= float32_val(f32
);
4620 uint32_t f32_sbit
= 0x80000000ULL
& f32_val
;
4621 int32_t f32_exp
= extract32(f32_val
, 23, 8);
4622 uint32_t f32_frac
= extract32(f32_val
, 0, 23);
4628 if (float32_is_any_nan(f32
)) {
4630 if (float32_is_signaling_nan(f32
)) {
4631 float_raise(float_flag_invalid
, fpst
);
4632 nan
= float32_maybe_silence_nan(f32
);
4634 if (fpst
->default_nan_mode
) {
4635 nan
= float32_default_nan
;
4638 } else if (float32_is_infinity(f32
)) {
4639 return float32_set_sign(float32_zero
, float32_is_neg(f32
));
4640 } else if (float32_is_zero(f32
)) {
4641 float_raise(float_flag_divbyzero
, fpst
);
4642 return float32_set_sign(float32_infinity
, float32_is_neg(f32
));
4643 } else if ((f32_val
& ~(1ULL << 31)) < (1ULL << 21)) {
4644 /* Abs(value) < 2.0^-128 */
4645 float_raise(float_flag_overflow
| float_flag_inexact
, fpst
);
4646 if (round_to_inf(fpst
, f32_sbit
)) {
4647 return float32_set_sign(float32_infinity
, float32_is_neg(f32
));
4649 return float32_set_sign(float32_maxnorm
, float32_is_neg(f32
));
4651 } else if (f32_exp
>= 253 && fpst
->flush_to_zero
) {
4652 float_raise(float_flag_underflow
, fpst
);
4653 return float32_set_sign(float32_zero
, float32_is_neg(f32
));
4657 f64
= make_float64(((int64_t)(f32_exp
) << 52) | (int64_t)(f32_frac
) << 29);
4658 r64
= call_recip_estimate(f64
, 253, fpst
);
4659 r64_val
= float64_val(r64
);
4660 r64_exp
= extract64(r64_val
, 52, 11);
4661 r64_frac
= extract64(r64_val
, 0, 52);
4663 /* result = sign : result_exp<7:0> : fraction<51:29>; */
4664 return make_float32(f32_sbit
|
4665 (r64_exp
& 0xff) << 23 |
4666 extract64(r64_frac
, 29, 24));
4669 float64
HELPER(recpe_f64
)(float64 input
, void *fpstp
)
4671 float_status
*fpst
= fpstp
;
4672 float64 f64
= float64_squash_input_denormal(input
, fpst
);
4673 uint64_t f64_val
= float64_val(f64
);
4674 uint64_t f64_sbit
= 0x8000000000000000ULL
& f64_val
;
4675 int64_t f64_exp
= extract64(f64_val
, 52, 11);
4681 /* Deal with any special cases */
4682 if (float64_is_any_nan(f64
)) {
4684 if (float64_is_signaling_nan(f64
)) {
4685 float_raise(float_flag_invalid
, fpst
);
4686 nan
= float64_maybe_silence_nan(f64
);
4688 if (fpst
->default_nan_mode
) {
4689 nan
= float64_default_nan
;
4692 } else if (float64_is_infinity(f64
)) {
4693 return float64_set_sign(float64_zero
, float64_is_neg(f64
));
4694 } else if (float64_is_zero(f64
)) {
4695 float_raise(float_flag_divbyzero
, fpst
);
4696 return float64_set_sign(float64_infinity
, float64_is_neg(f64
));
4697 } else if ((f64_val
& ~(1ULL << 63)) < (1ULL << 50)) {
4698 /* Abs(value) < 2.0^-1024 */
4699 float_raise(float_flag_overflow
| float_flag_inexact
, fpst
);
4700 if (round_to_inf(fpst
, f64_sbit
)) {
4701 return float64_set_sign(float64_infinity
, float64_is_neg(f64
));
4703 return float64_set_sign(float64_maxnorm
, float64_is_neg(f64
));
4705 } else if (f64_exp
>= 1023 && fpst
->flush_to_zero
) {
4706 float_raise(float_flag_underflow
, fpst
);
4707 return float64_set_sign(float64_zero
, float64_is_neg(f64
));
4710 r64
= call_recip_estimate(f64
, 2045, fpst
);
4711 r64_val
= float64_val(r64
);
4712 r64_exp
= extract64(r64_val
, 52, 11);
4713 r64_frac
= extract64(r64_val
, 0, 52);
4715 /* result = sign : result_exp<10:0> : fraction<51:0> */
4716 return make_float64(f64_sbit
|
4717 ((r64_exp
& 0x7ff) << 52) |
4721 /* The algorithm that must be used to calculate the estimate
4722 * is specified by the ARM ARM.
4724 static float64
recip_sqrt_estimate(float64 a
, float_status
*real_fp_status
)
4726 /* These calculations mustn't set any fp exception flags,
4727 * so we use a local copy of the fp_status.
4729 float_status dummy_status
= *real_fp_status
;
4730 float_status
*s
= &dummy_status
;
4734 if (float64_lt(a
, float64_half
, s
)) {
4735 /* range 0.25 <= a < 0.5 */
4737 /* a in units of 1/512 rounded down */
4738 /* q0 = (int)(a * 512.0); */
4739 q
= float64_mul(float64_512
, a
, s
);
4740 q_int
= float64_to_int64_round_to_zero(q
, s
);
4742 /* reciprocal root r */
4743 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
4744 q
= int64_to_float64(q_int
, s
);
4745 q
= float64_add(q
, float64_half
, s
);
4746 q
= float64_div(q
, float64_512
, s
);
4747 q
= float64_sqrt(q
, s
);
4748 q
= float64_div(float64_one
, q
, s
);
4750 /* range 0.5 <= a < 1.0 */
4752 /* a in units of 1/256 rounded down */
4753 /* q1 = (int)(a * 256.0); */
4754 q
= float64_mul(float64_256
, a
, s
);
4755 int64_t q_int
= float64_to_int64_round_to_zero(q
, s
);
4757 /* reciprocal root r */
4758 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
4759 q
= int64_to_float64(q_int
, s
);
4760 q
= float64_add(q
, float64_half
, s
);
4761 q
= float64_div(q
, float64_256
, s
);
4762 q
= float64_sqrt(q
, s
);
4763 q
= float64_div(float64_one
, q
, s
);
4765 /* r in units of 1/256 rounded to nearest */
4766 /* s = (int)(256.0 * r + 0.5); */
4768 q
= float64_mul(q
, float64_256
,s
);
4769 q
= float64_add(q
, float64_half
, s
);
4770 q_int
= float64_to_int64_round_to_zero(q
, s
);
4772 /* return (double)s / 256.0;*/
4773 return float64_div(int64_to_float64(q_int
, s
), float64_256
, s
);
4776 float32
HELPER(rsqrte_f32
)(float32 input
, void *fpstp
)
4778 float_status
*s
= fpstp
;
4779 float32 f32
= float32_squash_input_denormal(input
, s
);
4780 uint32_t val
= float32_val(f32
);
4781 uint32_t f32_sbit
= 0x80000000 & val
;
4782 int32_t f32_exp
= extract32(val
, 23, 8);
4783 uint32_t f32_frac
= extract32(val
, 0, 23);
4789 if (float32_is_any_nan(f32
)) {
4791 if (float32_is_signaling_nan(f32
)) {
4792 float_raise(float_flag_invalid
, s
);
4793 nan
= float32_maybe_silence_nan(f32
);
4795 if (s
->default_nan_mode
) {
4796 nan
= float32_default_nan
;
4799 } else if (float32_is_zero(f32
)) {
4800 float_raise(float_flag_divbyzero
, s
);
4801 return float32_set_sign(float32_infinity
, float32_is_neg(f32
));
4802 } else if (float32_is_neg(f32
)) {
4803 float_raise(float_flag_invalid
, s
);
4804 return float32_default_nan
;
4805 } else if (float32_is_infinity(f32
)) {
4806 return float32_zero
;
4809 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
4810 * preserving the parity of the exponent. */
4812 f64_frac
= ((uint64_t) f32_frac
) << 29;
4814 while (extract64(f64_frac
, 51, 1) == 0) {
4815 f64_frac
= f64_frac
<< 1;
4816 f32_exp
= f32_exp
-1;
4818 f64_frac
= extract64(f64_frac
, 0, 51) << 1;
4821 if (extract64(f32_exp
, 0, 1) == 0) {
4822 f64
= make_float64(((uint64_t) f32_sbit
) << 32
4826 f64
= make_float64(((uint64_t) f32_sbit
) << 32
4831 result_exp
= (380 - f32_exp
) / 2;
4833 f64
= recip_sqrt_estimate(f64
, s
);
4835 val64
= float64_val(f64
);
4837 val
= ((result_exp
& 0xff) << 23)
4838 | ((val64
>> 29) & 0x7fffff);
4839 return make_float32(val
);
4842 float64
HELPER(rsqrte_f64
)(float64 input
, void *fpstp
)
4844 float_status
*s
= fpstp
;
4845 float64 f64
= float64_squash_input_denormal(input
, s
);
4846 uint64_t val
= float64_val(f64
);
4847 uint64_t f64_sbit
= 0x8000000000000000ULL
& val
;
4848 int64_t f64_exp
= extract64(val
, 52, 11);
4849 uint64_t f64_frac
= extract64(val
, 0, 52);
4851 uint64_t result_frac
;
4853 if (float64_is_any_nan(f64
)) {
4855 if (float64_is_signaling_nan(f64
)) {
4856 float_raise(float_flag_invalid
, s
);
4857 nan
= float64_maybe_silence_nan(f64
);
4859 if (s
->default_nan_mode
) {
4860 nan
= float64_default_nan
;
4863 } else if (float64_is_zero(f64
)) {
4864 float_raise(float_flag_divbyzero
, s
);
4865 return float64_set_sign(float64_infinity
, float64_is_neg(f64
));
4866 } else if (float64_is_neg(f64
)) {
4867 float_raise(float_flag_invalid
, s
);
4868 return float64_default_nan
;
4869 } else if (float64_is_infinity(f64
)) {
4870 return float64_zero
;
4873 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
4874 * preserving the parity of the exponent. */
4877 while (extract64(f64_frac
, 51, 1) == 0) {
4878 f64_frac
= f64_frac
<< 1;
4879 f64_exp
= f64_exp
- 1;
4881 f64_frac
= extract64(f64_frac
, 0, 51) << 1;
4884 if (extract64(f64_exp
, 0, 1) == 0) {
4885 f64
= make_float64(f64_sbit
4889 f64
= make_float64(f64_sbit
4894 result_exp
= (3068 - f64_exp
) / 2;
4896 f64
= recip_sqrt_estimate(f64
, s
);
4898 result_frac
= extract64(float64_val(f64
), 0, 52);
4900 return make_float64(f64_sbit
|
4901 ((result_exp
& 0x7ff) << 52) |
4905 uint32_t HELPER(recpe_u32
)(uint32_t a
, void *fpstp
)
4907 float_status
*s
= fpstp
;
4910 if ((a
& 0x80000000) == 0) {
4914 f64
= make_float64((0x3feULL
<< 52)
4915 | ((int64_t)(a
& 0x7fffffff) << 21));
4917 f64
= recip_estimate(f64
, s
);
4919 return 0x80000000 | ((float64_val(f64
) >> 21) & 0x7fffffff);
4922 uint32_t HELPER(rsqrte_u32
)(uint32_t a
, void *fpstp
)
4924 float_status
*fpst
= fpstp
;
4927 if ((a
& 0xc0000000) == 0) {
4931 if (a
& 0x80000000) {
4932 f64
= make_float64((0x3feULL
<< 52)
4933 | ((uint64_t)(a
& 0x7fffffff) << 21));
4934 } else { /* bits 31-30 == '01' */
4935 f64
= make_float64((0x3fdULL
<< 52)
4936 | ((uint64_t)(a
& 0x3fffffff) << 22));
4939 f64
= recip_sqrt_estimate(f64
, fpst
);
4941 return 0x80000000 | ((float64_val(f64
) >> 21) & 0x7fffffff);
4944 /* VFPv4 fused multiply-accumulate */
4945 float32
VFP_HELPER(muladd
, s
)(float32 a
, float32 b
, float32 c
, void *fpstp
)
4947 float_status
*fpst
= fpstp
;
4948 return float32_muladd(a
, b
, c
, 0, fpst
);
4951 float64
VFP_HELPER(muladd
, d
)(float64 a
, float64 b
, float64 c
, void *fpstp
)
4953 float_status
*fpst
= fpstp
;
4954 return float64_muladd(a
, b
, c
, 0, fpst
);
4957 /* ARMv8 round to integral */
4958 float32
HELPER(rints_exact
)(float32 x
, void *fp_status
)
4960 return float32_round_to_int(x
, fp_status
);
4963 float64
HELPER(rintd_exact
)(float64 x
, void *fp_status
)
4965 return float64_round_to_int(x
, fp_status
);
4968 float32
HELPER(rints
)(float32 x
, void *fp_status
)
4970 int old_flags
= get_float_exception_flags(fp_status
), new_flags
;
4973 ret
= float32_round_to_int(x
, fp_status
);
4975 /* Suppress any inexact exceptions the conversion produced */
4976 if (!(old_flags
& float_flag_inexact
)) {
4977 new_flags
= get_float_exception_flags(fp_status
);
4978 set_float_exception_flags(new_flags
& ~float_flag_inexact
, fp_status
);
4984 float64
HELPER(rintd
)(float64 x
, void *fp_status
)
4986 int old_flags
= get_float_exception_flags(fp_status
), new_flags
;
4989 ret
= float64_round_to_int(x
, fp_status
);
4991 new_flags
= get_float_exception_flags(fp_status
);
4993 /* Suppress any inexact exceptions the conversion produced */
4994 if (!(old_flags
& float_flag_inexact
)) {
4995 new_flags
= get_float_exception_flags(fp_status
);
4996 set_float_exception_flags(new_flags
& ~float_flag_inexact
, fp_status
);
5002 /* Convert ARM rounding mode to softfloat */
5003 int arm_rmode_to_sf(int rmode
)
5006 case FPROUNDING_TIEAWAY
:
5007 rmode
= float_round_ties_away
;
5009 case FPROUNDING_ODD
:
5010 /* FIXME: add support for TIEAWAY and ODD */
5011 qemu_log_mask(LOG_UNIMP
, "arm: unimplemented rounding mode: %d\n",
5013 case FPROUNDING_TIEEVEN
:
5015 rmode
= float_round_nearest_even
;
5017 case FPROUNDING_POSINF
:
5018 rmode
= float_round_up
;
5020 case FPROUNDING_NEGINF
:
5021 rmode
= float_round_down
;
5023 case FPROUNDING_ZERO
:
5024 rmode
= float_round_to_zero
;
5030 static void crc_init_buffer(uint8_t *buf
, uint32_t val
, uint32_t bytes
)
5035 buf
[0] = val
& 0xff;
5036 } else if (bytes
== 2) {
5037 buf
[0] = val
& 0xff;
5038 buf
[1] = (val
>> 8) & 0xff;
5040 buf
[0] = val
& 0xff;
5041 buf
[1] = (val
>> 8) & 0xff;
5042 buf
[2] = (val
>> 16) & 0xff;
5043 buf
[3] = (val
>> 24) & 0xff;
5047 uint32_t HELPER(crc32
)(uint32_t acc
, uint32_t val
, uint32_t bytes
)
5051 crc_init_buffer(buf
, val
, bytes
);
5053 /* zlib crc32 converts the accumulator and output to one's complement. */
5054 return crc32(acc
^ 0xffffffff, buf
, bytes
) ^ 0xffffffff;
5057 uint32_t HELPER(crc32c
)(uint32_t acc
, uint32_t val
, uint32_t bytes
)
5061 crc_init_buffer(buf
, val
, bytes
);
5063 /* Linux crc32c converts the output to one's complement. */
5064 return crc32c(acc
, buf
, bytes
) ^ 0xffffffff;