2
* ARM virtual CPU header
4
* Copyright (c) 2003 Fabrice Bellard
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
24
#if defined(TARGET_AARCH64)
25
/* AArch64 definitions */
26
# define TARGET_LONG_BITS 64
27
# define ELF_MACHINE EM_AARCH64
29
# define TARGET_LONG_BITS 32
30
# define ELF_MACHINE EM_ARM
33
#define CPUArchState struct CPUARMState
35
#include "qemu-common.h"
36
#include "exec/cpu-defs.h"
38
#include "fpu/softfloat.h"
40
#define TARGET_HAS_ICE 1
42
#define EXCP_UDEF 1 /* undefined instruction */
43
#define EXCP_SWI 2 /* software interrupt */
44
#define EXCP_PREFETCH_ABORT 3
45
#define EXCP_DATA_ABORT 4
49
#define EXCP_EXCEPTION_EXIT 8 /* Return from v7M exception. */
50
#define EXCP_KERNEL_TRAP 9 /* Jumped to kernel code page. */
52
#define EXCP_SMC 11 /* secure monitor call */
54
#define ARMV7M_EXCP_RESET 1
55
#define ARMV7M_EXCP_NMI 2
56
#define ARMV7M_EXCP_HARD 3
57
#define ARMV7M_EXCP_MEM 4
58
#define ARMV7M_EXCP_BUS 5
59
#define ARMV7M_EXCP_USAGE 6
60
#define ARMV7M_EXCP_SVC 11
61
#define ARMV7M_EXCP_DEBUG 12
62
#define ARMV7M_EXCP_PENDSV 14
63
#define ARMV7M_EXCP_SYSTICK 15
65
/* ARM-specific interrupt pending bits. */
66
#define CPU_INTERRUPT_FIQ CPU_INTERRUPT_TGT_EXT_1
68
/* Meanings of the ARMCPU object's two inbound GPIO lines */
72
typedef void ARMWriteCPFunc(void *opaque, int cp_info,
73
int srcreg, int operand, uint32_t value);
74
typedef uint32_t ARMReadCPFunc(void *opaque, int cp_info,
75
int dstreg, int operand);
79
#define NB_MMU_MODES 2
81
/* We currently assume float and double are IEEE single and double
82
precision respectively.
83
Doing runtime conversions is tricky because VFP registers may contain
84
integer values (eg. as the result of a FTOSI instruction).
85
s<2n> maps to the least significant half of d<n>
86
s<2n+1> maps to the most significant half of d<n>
89
/* CPU state for each instance of a generic timer (in cp15 c14) */
90
typedef struct ARMGenericTimer {
91
uint64_t cval; /* Timer CompareValue register */
92
uint32_t ctl; /* Timer Control register */
99
/* Scale factor for generic timers, ie number of ns per tick.
100
* This gives a 62.5MHz timer.
102
#define GTIMER_SCALE 16
104
typedef struct CPUARMState {
105
/* Regs for current mode. */
108
/* 32/64 switch only happens when taking and returning from
109
* exceptions so the overlap semantics are taken care of then
110
* instead of having a complicated union.
112
/* Regs for A64 mode. */
115
/* TODO: pstate doesn't correspond to an architectural register;
116
* it would be better modelled as the underlying fields.
119
uint32_t aarch64; /* 1 if CPU is in aarch64 state; inverse of PSTATE.nRW */
121
/* Frequently accessed CPSR bits are stored separately for efficiency.
122
This contains all the other bits. Use cpsr_{read,write} to access
124
uint32_t uncached_cpsr;
127
/* Banked registers. */
128
uint32_t banked_spsr[7];
129
uint32_t banked_r13[7];
130
uint32_t banked_r14[7];
132
/* These hold r8-r12. */
133
uint32_t usr_regs[5];
134
uint32_t fiq_regs[5];
136
/* cpsr flag cache for faster execution */
137
uint32_t CF; /* 0 or 1 */
138
uint32_t VF; /* V is the bit 31. All other bits are undefined */
139
uint32_t NF; /* N is bit 31. All other bits are undefined. */
140
uint32_t ZF; /* Z set if zero. */
141
uint32_t QF; /* 0 or 1 */
142
uint32_t GE; /* cpsr[19:16] */
143
uint32_t thumb; /* cpsr[5]. 0 = arm mode, 1 = thumb mode. */
144
uint32_t condexec_bits; /* IT bits. cpsr[15:10,26:25]. */
146
/* System control coprocessor (cp15) */
149
uint32_t c0_cssel; /* Cache size selection. */
150
uint32_t c1_sys; /* System control register. */
151
uint32_t c1_coproc; /* Coprocessor access register. */
152
uint32_t c1_xscaleauxcr; /* XScale auxiliary control register. */
153
uint32_t c1_scr; /* secure config register. */
154
uint32_t c1_sedbg; /* Secure debug enable register. */
155
uint32_t c1_nseac; /* Non-secure access control register. */
156
uint32_t c2_base0; /* MMU translation table base 0. */
157
uint32_t c2_base0_hi; /* MMU translation table base 0, high 32 bits */
158
uint32_t c2_base1; /* MMU translation table base 0. */
159
uint32_t c2_base1_hi; /* MMU translation table base 1, high 32 bits */
160
uint32_t c2_control; /* MMU translation table base control. */
161
uint32_t c2_mask; /* MMU translation table base selection mask. */
162
uint32_t c2_base_mask; /* MMU translation table base 0 mask. */
163
uint32_t c2_data; /* MPU data cachable bits. */
164
uint32_t c2_insn; /* MPU instruction cachable bits. */
165
uint32_t c3; /* MMU domain access control register
166
MPU write buffer control. */
167
uint32_t c5_insn; /* Fault status registers. */
169
uint32_t c6_region[8]; /* MPU base/size registers. */
170
uint32_t c6_insn; /* Fault address registers. */
172
uint32_t c7_par; /* Translation result. */
173
uint32_t c7_par_hi; /* Translation result, high 32 bits */
174
uint32_t c9_insn; /* Cache lockdown registers. */
176
uint32_t c9_pmcr; /* performance monitor control register */
177
uint32_t c9_pmcnten; /* perf monitor counter enables */
178
uint32_t c9_pmovsr; /* perf monitor overflow status */
179
uint32_t c9_pmxevtyper; /* perf monitor event type */
180
uint32_t c9_pmuserenr; /* perf monitor user enable */
181
uint32_t c9_pminten; /* perf monitor interrupt enables */
182
uint32_t c12_vbar; /* vector base address register */
183
uint32_t c12_mvbar; /* monitor vector base address register. */
184
uint32_t c13_fcse; /* FCSE PID. */
185
uint32_t c13_context; /* Context ID. */
186
uint32_t c13_tls1; /* User RW Thread register. */
187
uint32_t c13_tls2; /* User RO Thread register. */
188
uint32_t c13_tls3; /* Privileged Thread register. */
189
uint32_t c14_cntfrq; /* Counter Frequency register */
190
uint32_t c14_cntkctl; /* Timer Control register */
191
ARMGenericTimer c14_timer[NUM_GTIMERS];
192
uint32_t c15_cpar; /* XScale Coprocessor Access Register */
193
uint32_t c15_ticonfig; /* TI925T configuration byte. */
194
uint32_t c15_i_max; /* Maximum D-cache dirty line index. */
195
uint32_t c15_i_min; /* Minimum D-cache dirty line index. */
196
uint32_t c15_threadid; /* TI debugger thread-ID. */
197
uint32_t c15_config_base_address; /* SCU base address. */
198
uint32_t c15_diagnostic; /* diagnostic register */
199
uint32_t c15_power_diagnostic;
200
uint32_t c15_power_control; /* power control */
203
/* System registers (AArch64) */
215
int pending_exception;
218
/* Thumb-2 EE state. */
222
/* VFP coprocessor state. */
224
/* VFP/Neon register state. Note that the mapping between S, D and Q
225
* views of the register bank differs between AArch64 and AArch32:
227
* Qn = regs[2n+1]:regs[2n]
229
* Sn = regs[n/2] bits 31..0 for even n, and bits 63..32 for odd n
230
* (and regs[32] to regs[63] are inaccessible)
232
* Qn = regs[2n+1]:regs[2n]
234
* Sn = regs[2n] bits 31..0
235
* This corresponds to the architecturally defined mapping between
236
* the two execution states, and means we do not need to explicitly
237
* map these registers when changing states.
242
/* We store these fpcsr fields separately for convenience. */
246
/* scratch space when Tn are not sufficient. */
249
/* fp_status is the "normal" fp status. standard_fp_status retains
250
* values corresponding to the ARM "Standard FPSCR Value", ie
251
* default-NaN, flush-to-zero, round-to-nearest and is used by
252
* any operations (generally Neon) which the architecture defines
253
* as controlled by the standard FPSCR value rather than the FPSCR.
255
* To avoid having to transfer exception bits around, we simply
256
* say that the FPSCR cumulative exception flags are the logical
257
* OR of the flags in the two fp statuses. This relies on the
258
* only thing which needs to read the exception flags being
259
* an explicit FPSCR read.
261
float_status fp_status;
262
float_status standard_fp_status;
264
uint32_t exclusive_addr;
265
uint32_t exclusive_val;
266
uint32_t exclusive_high;
267
#if defined(CONFIG_USER_ONLY)
268
uint32_t exclusive_test;
269
uint32_t exclusive_info;
272
/* iwMMXt coprocessor state. */
280
/* For mixed endian mode. */
283
#if defined(CONFIG_USER_ONLY)
284
/* For usermode syscall translation. */
290
/* These fields after the common ones so they are preserved on reset. */
292
/* Internal CPU feature flags. */
296
const struct arm_boot_info *boot_info;
301
ARMCPU *cpu_arm_init(const char *cpu_model);
302
void arm_translate_init(void);
303
void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu);
304
int cpu_arm_exec(CPUARMState *s);
305
int bank_number(int mode);
306
void switch_mode(CPUARMState *, int);
307
uint32_t do_arm_semihosting(CPUARMState *env);
309
static inline bool is_a64(CPUARMState *env)
314
#define PSTATE_N_SHIFT 3
315
#define PSTATE_N (1 << PSTATE_N_SHIFT)
316
#define PSTATE_Z_SHIFT 2
317
#define PSTATE_Z (1 << PSTATE_Z_SHIFT)
318
#define PSTATE_C_SHIFT 1
319
#define PSTATE_C (1 << PSTATE_C_SHIFT)
320
#define PSTATE_V_SHIFT 0
321
#define PSTATE_V (1 << PSTATE_V_SHIFT)
323
/* you can call this signal handler from your SIGBUS and SIGSEGV
324
signal handlers to inform the virtual CPU of exceptions. non zero
325
is returned if the signal was handled by the virtual CPU. */
326
int cpu_arm_signal_handler(int host_signum, void *pinfo,
328
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
330
#define cpu_handle_mmu_fault cpu_arm_handle_mmu_fault
332
#define CPSR_M (0x1fU)
333
#define CPSR_T (1U << 5)
334
#define CPSR_F (1U << 6)
335
#define CPSR_I (1U << 7)
336
#define CPSR_A (1U << 8)
337
#define CPSR_E (1U << 9)
338
#define CPSR_IT_2_7 (0xfc00U)
339
#define CPSR_GE (0xfU << 16)
340
#define CPSR_RESERVED (0xfU << 20)
341
#define CPSR_J (1U << 24)
342
#define CPSR_IT_0_1 (3U << 25)
343
#define CPSR_Q (1U << 27)
344
#define CPSR_V (1U << 28)
345
#define CPSR_C (1U << 29)
346
#define CPSR_Z (1U << 30)
347
#define CPSR_N (1U << 31)
348
#define CPSR_NZCV (CPSR_N | CPSR_Z | CPSR_C | CPSR_V)
350
#define CPSR_IT (CPSR_IT_0_1 | CPSR_IT_2_7)
351
#define CACHED_CPSR_BITS (CPSR_T | CPSR_GE | CPSR_IT | CPSR_Q | CPSR_NZCV)
352
/* Bits writable in user mode. */
353
#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
354
/* Execution state bits. MRS read as zero, MSR writes ignored. */
355
#define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J)
357
/* Return the current CPSR value. */
358
uint32_t cpsr_read(CPUARMState *env);
359
/* Set the CPSR. Note that some bits of mask must be all-set or all-clear. */
360
void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask);
362
/* Return the current xPSR value. */
363
static inline uint32_t xpsr_read(CPUARMState *env)
367
return (env->NF & 0x80000000) | (ZF << 30)
368
| (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
369
| (env->thumb << 24) | ((env->condexec_bits & 3) << 25)
370
| ((env->condexec_bits & 0xfc) << 8)
371
| env->v7m.exception;
374
/* Set the xPSR. Note that some bits of mask must be all-set or all-clear. */
375
static inline void xpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
377
if (mask & CPSR_NZCV) {
378
env->ZF = (~val) & CPSR_Z;
380
env->CF = (val >> 29) & 1;
381
env->VF = (val << 3) & 0x80000000;
384
env->QF = ((val & CPSR_Q) != 0);
385
if (mask & (1 << 24))
386
env->thumb = ((val & (1 << 24)) != 0);
387
if (mask & CPSR_IT_0_1) {
388
env->condexec_bits &= ~3;
389
env->condexec_bits |= (val >> 25) & 3;
391
if (mask & CPSR_IT_2_7) {
392
env->condexec_bits &= 3;
393
env->condexec_bits |= (val >> 8) & 0xfc;
396
env->v7m.exception = val & 0x1ff;
400
/* Return the current FPSCR value. */
401
uint32_t vfp_get_fpscr(CPUARMState *env);
402
void vfp_set_fpscr(CPUARMState *env, uint32_t val);
405
ARM_CPU_MODE_USR = 0x10,
406
ARM_CPU_MODE_FIQ = 0x11,
407
ARM_CPU_MODE_IRQ = 0x12,
408
ARM_CPU_MODE_SVC = 0x13,
409
ARM_CPU_MODE_SMC = 0x16,
410
ARM_CPU_MODE_ABT = 0x17,
411
ARM_CPU_MODE_UND = 0x1b,
412
ARM_CPU_MODE_SYS = 0x1f
415
/* VFP system registers. */
416
#define ARM_VFP_FPSID 0
417
#define ARM_VFP_FPSCR 1
418
#define ARM_VFP_MVFR1 6
419
#define ARM_VFP_MVFR0 7
420
#define ARM_VFP_FPEXC 8
421
#define ARM_VFP_FPINST 9
422
#define ARM_VFP_FPINST2 10
424
/* iwMMXt coprocessor control registers. */
425
#define ARM_IWMMXT_wCID 0
426
#define ARM_IWMMXT_wCon 1
427
#define ARM_IWMMXT_wCSSF 2
428
#define ARM_IWMMXT_wCASF 3
429
#define ARM_IWMMXT_wCGR0 8
430
#define ARM_IWMMXT_wCGR1 9
431
#define ARM_IWMMXT_wCGR2 10
432
#define ARM_IWMMXT_wCGR3 11
434
/* If adding a feature bit which corresponds to a Linux ELF
435
* HWCAP bit, remember to update the feature-bit-to-hwcap
436
* mapping in linux-user/elfload.c:get_elf_hwcap().
440
ARM_FEATURE_AUXCR, /* ARM1026 Auxiliary control register. */
441
ARM_FEATURE_XSCALE, /* Intel XScale extensions. */
442
ARM_FEATURE_IWMMXT, /* Intel iwMMXt extension. */
447
ARM_FEATURE_MPU, /* Only has Memory Protection Unit, not full MMU. */
449
ARM_FEATURE_VFP_FP16,
451
ARM_FEATURE_THUMB_DIV, /* divide supported in Thumb encoding */
452
ARM_FEATURE_M, /* Microcontroller profile. */
453
ARM_FEATURE_OMAPCP, /* OMAP specific CP15 ops handling. */
454
ARM_FEATURE_THUMB2EE,
455
ARM_FEATURE_V7MP, /* v7 Multiprocessing Extensions */
458
ARM_FEATURE_STRONGARM,
459
ARM_FEATURE_VAPA, /* cp15 VA to PA lookups */
460
ARM_FEATURE_ARM_DIV, /* divide supported in ARM encoding */
461
ARM_FEATURE_VFP4, /* VFPv4 (implies that NEON is v2) */
462
ARM_FEATURE_GENERIC_TIMER,
463
ARM_FEATURE_MVFR, /* Media and VFP Feature Registers 0 and 1 */
464
ARM_FEATURE_DUMMY_C15_REGS, /* RAZ/WI all of cp15 crn=15 */
465
ARM_FEATURE_CACHE_TEST_CLEAN, /* 926/1026 style test-and-clean ops */
466
ARM_FEATURE_CACHE_DIRTY_REG, /* 1136/1176 cache dirty status register */
467
ARM_FEATURE_CACHE_BLOCK_OPS, /* v6 optional cache block operations */
468
ARM_FEATURE_MPIDR, /* has cp15 MPIDR */
469
ARM_FEATURE_PXN, /* has Privileged Execute Never bit */
470
ARM_FEATURE_LPAE, /* has Large Physical Address Extension */
472
ARM_FEATURE_AARCH64, /* supports 64 bit mode */
473
ARM_FEATURE_TRUSTZONE, /* TrustZone Security Extensions. */
476
static inline int arm_feature(CPUARMState *env, int feature)
478
return (env->features & (1ULL << feature)) != 0;
481
void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf);
483
/* Interface between CPU and Interrupt controller. */
484
void armv7m_nvic_set_pending(void *opaque, int irq);
485
int armv7m_nvic_acknowledge_irq(void *opaque);
486
void armv7m_nvic_complete_irq(void *opaque, int irq);
488
/* Interface for defining coprocessor registers.
489
* Registers are defined in tables of arm_cp_reginfo structs
490
* which are passed to define_arm_cp_regs().
493
/* When looking up a coprocessor register we look for it
494
* via an integer which encodes all of:
496
* Crn, Crm, opc1, opc2 fields
497
* 32 or 64 bit register (ie is it accessed via MRC/MCR
499
* We allow 4 bits for opc1 because MRRC/MCRR have a 4 bit field.
500
* (In this case crn and opc2 should be zero.)
502
#define ENCODE_CP_REG(cp, is64, crn, crm, opc1, opc2) \
503
(((cp) << 16) | ((is64) << 15) | ((crn) << 11) | \
504
((crm) << 7) | ((opc1) << 3) | (opc2))
506
/* Note that these must line up with the KVM/ARM register
507
* ID field definitions (kvm.c will check this, but we
508
* can't just use the KVM defines here as the kvm headers
509
* are unavailable to non-KVM-specific files)
511
#define CP_REG_SIZE_SHIFT 52
512
#define CP_REG_SIZE_MASK 0x00f0000000000000ULL
513
#define CP_REG_SIZE_U32 0x0020000000000000ULL
514
#define CP_REG_SIZE_U64 0x0030000000000000ULL
515
#define CP_REG_ARM 0x4000000000000000ULL
517
/* Convert a full 64 bit KVM register ID to the truncated 32 bit
518
* version used as a key for the coprocessor register hashtable
520
static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
522
uint32_t cpregid = kvmid;
523
if ((kvmid & CP_REG_SIZE_MASK) == CP_REG_SIZE_U64) {
524
cpregid |= (1 << 15);
529
/* Convert a truncated 32 bit hashtable key into the full
530
* 64 bit KVM register ID.
532
static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)
534
uint64_t kvmid = cpregid & ~(1 << 15);
535
if (cpregid & (1 << 15)) {
536
kvmid |= CP_REG_SIZE_U64 | CP_REG_ARM;
538
kvmid |= CP_REG_SIZE_U32 | CP_REG_ARM;
543
/* ARMCPRegInfo type field bits. If the SPECIAL bit is set this is a
544
* special-behaviour cp reg and bits [15..8] indicate what behaviour
545
* it has. Otherwise it is a simple cp reg, where CONST indicates that
546
* TCG can assume the value to be constant (ie load at translate time)
547
* and 64BIT indicates a 64 bit wide coprocessor register. SUPPRESS_TB_END
548
* indicates that the TB should not be ended after a write to this register
549
* (the default is that the TB ends after cp writes). OVERRIDE permits
550
* a register definition to override a previous definition for the
551
* same (cp, is64, crn, crm, opc1, opc2) tuple: either the new or the
552
* old must have the OVERRIDE bit set.
553
* NO_MIGRATE indicates that this register should be ignored for migration;
554
* (eg because any state is accessed via some other coprocessor register).
555
* IO indicates that this register does I/O and therefore its accesses
556
* need to be surrounded by gen_io_start()/gen_io_end(). In particular,
557
* registers which implement clocks or timers require this.
559
#define ARM_CP_SPECIAL 1
560
#define ARM_CP_CONST 2
561
#define ARM_CP_64BIT 4
562
#define ARM_CP_SUPPRESS_TB_END 8
563
#define ARM_CP_OVERRIDE 16
564
#define ARM_CP_NO_MIGRATE 32
566
#define ARM_CP_NOP (ARM_CP_SPECIAL | (1 << 8))
567
#define ARM_CP_WFI (ARM_CP_SPECIAL | (2 << 8))
568
#define ARM_LAST_SPECIAL ARM_CP_WFI
569
/* Used only as a terminator for ARMCPRegInfo lists */
570
#define ARM_CP_SENTINEL 0xffff
571
/* Mask of only the flag bits in a type field */
572
#define ARM_CP_FLAG_MASK 0x7f
574
/* Return true if cptype is a valid type field. This is used to try to
575
* catch errors where the sentinel has been accidentally left off the end
576
* of a list of registers.
578
static inline bool cptype_valid(int cptype)
580
return ((cptype & ~ARM_CP_FLAG_MASK) == 0)
581
|| ((cptype & ARM_CP_SPECIAL) &&
582
((cptype & ~ARM_CP_FLAG_MASK) <= ARM_LAST_SPECIAL));
586
* We define bits for Read and Write access for what rev C of the v7-AR ARM ARM
587
* defines as PL0 (user), PL1 (fiq/irq/svc/abt/und/sys, ie privileged), and
588
* PL2 (hyp). The other level which has Read and Write bits is Secure PL1
589
* (ie any of the privileged modes in Secure state, or Monitor mode).
590
* If a register is accessible in one privilege level it's always accessible
591
* in higher privilege levels too. Since "Secure PL1" also follows this rule
592
* (ie anything visible in PL2 is visible in S-PL1, some things are only
593
* visible in S-PL1) but "Secure PL1" is a bit of a mouthful, we bend the
594
* terminology a little and call this PL3.
596
* If access permissions for a register are more complex than can be
597
* described with these bits, then use a laxer set of restrictions, and
598
* do the more restrictive/complex check inside a helper function.
602
#define PL2_R (0x20 | PL3_R)
603
#define PL2_W (0x10 | PL3_W)
604
#define PL1_R (0x08 | PL2_R)
605
#define PL1_W (0x04 | PL2_W)
606
#define PL0_R (0x02 | PL1_R)
607
#define PL0_W (0x01 | PL1_W)
609
#define PL3_RW (PL3_R | PL3_W)
610
#define PL2_RW (PL2_R | PL2_W)
611
#define PL1_RW (PL1_R | PL1_W)
612
#define PL0_RW (PL0_R | PL0_W)
614
static inline int arm_current_pl(CPUARMState *env)
616
if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
619
/* We don't currently implement the Virtualization or TrustZone
620
* extensions, so PL2 and PL3 don't exist for us.
625
typedef struct ARMCPRegInfo ARMCPRegInfo;
627
/* Access functions for coprocessor registers. These should return
628
* 0 on success, or one of the EXCP_* constants if access should cause
629
* an exception (in which case *value is not written).
631
typedef int CPReadFn(CPUARMState *env, const ARMCPRegInfo *opaque,
633
typedef int CPWriteFn(CPUARMState *env, const ARMCPRegInfo *opaque,
635
/* Hook function for register reset */
636
typedef void CPResetFn(CPUARMState *env, const ARMCPRegInfo *opaque);
640
/* Definition of an ARM coprocessor register */
641
struct ARMCPRegInfo {
642
/* Name of register (useful mainly for debugging, need not be unique) */
644
/* Location of register: coprocessor number and (crn,crm,opc1,opc2)
645
* tuple. Any of crm, opc1 and opc2 may be CP_ANY to indicate a
646
* 'wildcard' field -- any value of that field in the MRC/MCR insn
647
* will be decoded to this register. The register read and write
648
* callbacks will be passed an ARMCPRegInfo with the crn/crm/opc1/opc2
649
* used by the program, so it is possible to register a wildcard and
650
* then behave differently on read/write if necessary.
651
* For 64 bit registers, only crm and opc1 are relevant; crn and opc2
659
/* Register type: ARM_CP_* bits/values */
661
/* Access rights: PL*_[RW] */
663
/* The opaque pointer passed to define_arm_cp_regs_with_opaque() when
664
* this register was defined: can be used to hand data through to the
665
* register read/write functions, since they are passed the ARMCPRegInfo*.
668
/* Value of this register, if it is ARM_CP_CONST. Otherwise, if
669
* fieldoffset is non-zero, the reset value of the register.
672
/* Offset of the field in CPUARMState for this register. This is not
674
* 1. type is ARM_CP_CONST or one of the ARM_CP_SPECIALs
675
* 2. both readfn and writefn are specified
677
ptrdiff_t fieldoffset; /* offsetof(CPUARMState, field) */
678
/* Function for handling reads of this register. If NULL, then reads
679
* will be done by loading from the offset into CPUARMState specified
683
/* Function for handling writes of this register. If NULL, then writes
684
* will be done by writing to the offset into CPUARMState specified
688
/* Function for doing a "raw" read; used when we need to copy
689
* coprocessor state to the kernel for KVM or out for
690
* migration. This only needs to be provided if there is also a
691
* readfn and it makes an access permission check.
693
CPReadFn *raw_readfn;
694
/* Function for doing a "raw" write; used when we need to copy KVM
695
* kernel coprocessor state into userspace, or for inbound
696
* migration. This only needs to be provided if there is also a
697
* writefn and it makes an access permission check or masks out
698
* "unwritable" bits or has write-one-to-clear or similar behaviour.
700
CPWriteFn *raw_writefn;
701
/* Function for resetting the register. If NULL, then reset will be done
702
* by writing resetvalue to the field specified in fieldoffset. If
703
* fieldoffset is 0 then no reset will be done.
708
/* Macros which are lvalues for the field in CPUARMState for the
711
#define CPREG_FIELD32(env, ri) \
712
(*(uint32_t *)((char *)(env) + (ri)->fieldoffset))
713
#define CPREG_FIELD64(env, ri) \
714
(*(uint64_t *)((char *)(env) + (ri)->fieldoffset))
716
#define REGINFO_SENTINEL { .type = ARM_CP_SENTINEL }
718
void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
719
const ARMCPRegInfo *regs, void *opaque);
720
void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
721
const ARMCPRegInfo *regs, void *opaque);
722
static inline void define_arm_cp_regs(ARMCPU *cpu, const ARMCPRegInfo *regs)
724
define_arm_cp_regs_with_opaque(cpu, regs, 0);
726
static inline void define_one_arm_cp_reg(ARMCPU *cpu, const ARMCPRegInfo *regs)
728
define_one_arm_cp_reg_with_opaque(cpu, regs, 0);
730
const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp);
732
/* CPWriteFn that can be used to implement writes-ignored behaviour */
733
int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
735
/* CPReadFn that can be used for read-as-zero behaviour */
736
int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value);
738
static inline bool cp_access_ok(CPUARMState *env,
739
const ARMCPRegInfo *ri, int isread)
741
return (ri->access >> ((arm_current_pl(env) * 2) + isread)) & 1;
745
* write_list_to_cpustate
748
* For each register listed in the ARMCPU cpreg_indexes list, write
749
* its value from the cpreg_values list into the ARMCPUState structure.
750
* This updates TCG's working data structures from KVM data or
751
* from incoming migration state.
753
* Returns: true if all register values were updated correctly,
754
* false if some register was unknown or could not be written.
755
* Note that we do not stop early on failure -- we will attempt
756
* writing all registers in the list.
758
bool write_list_to_cpustate(ARMCPU *cpu);
761
* write_cpustate_to_list:
764
* For each register listed in the ARMCPU cpreg_indexes list, write
765
* its value from the ARMCPUState structure into the cpreg_values list.
766
* This is used to copy info from TCG's working data structures into
767
* KVM or for outbound migration.
769
* Returns: true if all register values were read correctly,
770
* false if some register was unknown or could not be read.
771
* Note that we do not stop early on failure -- we will attempt
772
* reading all registers in the list.
774
bool write_cpustate_to_list(ARMCPU *cpu);
776
/* Does the core conform to the the "MicroController" profile. e.g. Cortex-M3.
777
Note the M in older cores (eg. ARM7TDMI) stands for Multiply. These are
778
conventional cores (ie. Application or Realtime profile). */
780
#define IS_M(env) arm_feature(env, ARM_FEATURE_M)
782
#define ARM_CPUID_TI915T 0x54029152
783
#define ARM_CPUID_TI925T 0x54029252
785
#if defined(CONFIG_USER_ONLY)
786
#define TARGET_PAGE_BITS 12
788
/* The ARM MMU allows 1k pages. */
789
/* ??? Linux doesn't actually use these, and they're deprecated in recent
790
architecture revisions. Maybe a configure option to disable them. */
791
#define TARGET_PAGE_BITS 10
794
#if defined(TARGET_AARCH64)
795
# define TARGET_PHYS_ADDR_SPACE_BITS 48
796
# define TARGET_VIRT_ADDR_SPACE_BITS 64
798
# define TARGET_PHYS_ADDR_SPACE_BITS 40
799
# define TARGET_VIRT_ADDR_SPACE_BITS 32
802
static inline CPUARMState *cpu_init(const char *cpu_model)
804
ARMCPU *cpu = cpu_arm_init(cpu_model);
811
#define cpu_exec cpu_arm_exec
812
#define cpu_gen_code cpu_arm_gen_code
813
#define cpu_signal_handler cpu_arm_signal_handler
814
#define cpu_list arm_cpu_list
816
/* MMU modes definitions */
817
#define MMU_MODE0_SUFFIX _kernel
818
#define MMU_MODE1_SUFFIX _user
819
#define MMU_USER_IDX 1
820
static inline int cpu_mmu_index (CPUARMState *env)
822
return (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR ? 1 : 0;
825
#include "exec/cpu-all.h"
827
/* Bit usage in the TB flags field: bit 31 indicates whether we are
828
* in 32 or 64 bit mode. The meaning of the other bits depends on that.
830
#define ARM_TBFLAG_AARCH64_STATE_SHIFT 31
831
#define ARM_TBFLAG_AARCH64_STATE_MASK (1U << ARM_TBFLAG_AARCH64_STATE_SHIFT)
833
/* Bit usage when in AArch32 state: */
834
#define ARM_TBFLAG_THUMB_SHIFT 0
835
#define ARM_TBFLAG_THUMB_MASK (1 << ARM_TBFLAG_THUMB_SHIFT)
836
#define ARM_TBFLAG_VECLEN_SHIFT 1
837
#define ARM_TBFLAG_VECLEN_MASK (0x7 << ARM_TBFLAG_VECLEN_SHIFT)
838
#define ARM_TBFLAG_VECSTRIDE_SHIFT 4
839
#define ARM_TBFLAG_VECSTRIDE_MASK (0x3 << ARM_TBFLAG_VECSTRIDE_SHIFT)
840
#define ARM_TBFLAG_PRIV_SHIFT 6
841
#define ARM_TBFLAG_PRIV_MASK (1 << ARM_TBFLAG_PRIV_SHIFT)
842
#define ARM_TBFLAG_VFPEN_SHIFT 7
843
#define ARM_TBFLAG_VFPEN_MASK (1 << ARM_TBFLAG_VFPEN_SHIFT)
844
#define ARM_TBFLAG_CONDEXEC_SHIFT 8
845
#define ARM_TBFLAG_CONDEXEC_MASK (0xff << ARM_TBFLAG_CONDEXEC_SHIFT)
846
#define ARM_TBFLAG_BSWAP_CODE_SHIFT 16
847
#define ARM_TBFLAG_BSWAP_CODE_MASK (1 << ARM_TBFLAG_BSWAP_CODE_SHIFT)
849
/* Bit usage when in AArch64 state: currently no bits defined */
851
/* some convenience accessor macros */
852
#define ARM_TBFLAG_AARCH64_STATE(F) \
853
(((F) & ARM_TBFLAG_AARCH64_STATE_MASK) >> ARM_TBFLAG_AARCH64_STATE_SHIFT)
854
#define ARM_TBFLAG_THUMB(F) \
855
(((F) & ARM_TBFLAG_THUMB_MASK) >> ARM_TBFLAG_THUMB_SHIFT)
856
#define ARM_TBFLAG_VECLEN(F) \
857
(((F) & ARM_TBFLAG_VECLEN_MASK) >> ARM_TBFLAG_VECLEN_SHIFT)
858
#define ARM_TBFLAG_VECSTRIDE(F) \
859
(((F) & ARM_TBFLAG_VECSTRIDE_MASK) >> ARM_TBFLAG_VECSTRIDE_SHIFT)
860
#define ARM_TBFLAG_PRIV(F) \
861
(((F) & ARM_TBFLAG_PRIV_MASK) >> ARM_TBFLAG_PRIV_SHIFT)
862
#define ARM_TBFLAG_VFPEN(F) \
863
(((F) & ARM_TBFLAG_VFPEN_MASK) >> ARM_TBFLAG_VFPEN_SHIFT)
864
#define ARM_TBFLAG_CONDEXEC(F) \
865
(((F) & ARM_TBFLAG_CONDEXEC_MASK) >> ARM_TBFLAG_CONDEXEC_SHIFT)
866
#define ARM_TBFLAG_BSWAP_CODE(F) \
867
(((F) & ARM_TBFLAG_BSWAP_CODE_MASK) >> ARM_TBFLAG_BSWAP_CODE_SHIFT)
869
static inline void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
870
target_ulong *cs_base, int *flags)
874
*flags = ARM_TBFLAG_AARCH64_STATE_MASK;
878
*flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
879
| (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
880
| (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
881
| (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
882
| (env->bswap_code << ARM_TBFLAG_BSWAP_CODE_SHIFT);
883
if (arm_feature(env, ARM_FEATURE_M)) {
884
privmode = !((env->v7m.exception == 0) && (env->v7m.control & 1));
886
privmode = (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR;
889
*flags |= ARM_TBFLAG_PRIV_MASK;
891
if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
892
*flags |= ARM_TBFLAG_VFPEN_MASK;
899
static inline bool cpu_has_work(CPUState *cpu)
901
return cpu->interrupt_request &
902
(CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB);
905
#include "exec/exec-all.h"
907
static inline void cpu_pc_from_tb(CPUARMState *env, TranslationBlock *tb)
909
if (ARM_TBFLAG_AARCH64_STATE(tb->flags)) {
912
env->regs[15] = tb->pc;
916
/* Load an instruction and return it in the standard little-endian order */
917
static inline uint32_t arm_ldl_code(CPUARMState *env, target_ulong addr,
920
uint32_t insn = cpu_ldl_code(env, addr);
922
return bswap32(insn);
927
/* Ditto, for a halfword (Thumb) instruction */
928
static inline uint16_t arm_lduw_code(CPUARMState *env, target_ulong addr,
931
uint16_t insn = cpu_lduw_code(env, addr);
933
return bswap16(insn);