blob: 9f110f15b6c83b3d89867fe8f8995fa7364091c6 [file] [log] [blame]
bellard2c0262a2003-09-30 20:34:21 +00001/*
2 * ARM virtual CPU header
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard2c0262a2003-09-30 20:34:21 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellard2c0262a2003-09-30 20:34:21 +000018 */
19#ifndef CPU_ARM_H
20#define CPU_ARM_H
21
Alexander Graf3926cc82013-09-03 20:12:09 +010022#include "config.h"
bellard3cf1e032004-01-24 15:19:09 +000023
Alexander Graf3926cc82013-09-03 20:12:09 +010024#if defined(TARGET_AARCH64)
25 /* AArch64 definitions */
26# define TARGET_LONG_BITS 64
27# define ELF_MACHINE EM_AARCH64
28#else
29# define TARGET_LONG_BITS 32
30# define ELF_MACHINE EM_ARM
31#endif
ths9042c0e2006-12-23 14:18:40 +000032
Andreas Färber9349b4f2012-03-14 01:38:32 +010033#define CPUArchState struct CPUARMState
pbrookc2764712009-03-07 15:24:59 +000034
Stefan Weil9a78eea2010-10-22 23:03:33 +020035#include "qemu-common.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010036#include "exec/cpu-defs.h"
bellard2c0262a2003-09-30 20:34:21 +000037
Paolo Bonzini6b4c3052012-10-24 13:12:00 +020038#include "fpu/softfloat.h"
bellard53cd6632005-03-13 18:50:23 +000039
bellard1fddef42005-04-17 19:16:13 +000040#define TARGET_HAS_ICE 1
41
bellardb8a9e8f2005-02-07 23:10:07 +000042#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
bellardb5ff1b32005-11-26 10:38:39 +000046#define EXCP_IRQ 5
47#define EXCP_FIQ 6
pbrook06c949e2006-02-04 19:35:26 +000048#define EXCP_BKPT 7
pbrook9ee6e8b2007-11-11 00:04:49 +000049#define EXCP_EXCEPTION_EXIT 8 /* Return from v7M exception. */
pbrookfbb4a2e2008-05-29 00:20:44 +000050#define EXCP_KERNEL_TRAP 9 /* Jumped to kernel code page. */
Paul Brook426f5ab2009-11-22 21:35:13 +000051#define EXCP_STREX 10
pbrook9ee6e8b2007-11-11 00:04:49 +000052
53#define ARMV7M_EXCP_RESET 1
54#define ARMV7M_EXCP_NMI 2
55#define ARMV7M_EXCP_HARD 3
56#define ARMV7M_EXCP_MEM 4
57#define ARMV7M_EXCP_BUS 5
58#define ARMV7M_EXCP_USAGE 6
59#define ARMV7M_EXCP_SVC 11
60#define ARMV7M_EXCP_DEBUG 12
61#define ARMV7M_EXCP_PENDSV 14
62#define ARMV7M_EXCP_SYSTICK 15
bellard2c0262a2003-09-30 20:34:21 +000063
Richard Henderson403946c2011-05-04 13:34:29 -070064/* ARM-specific interrupt pending bits. */
65#define CPU_INTERRUPT_FIQ CPU_INTERRUPT_TGT_EXT_1
66
Peter Maydell7c1840b2013-08-20 14:54:28 +010067/* Meanings of the ARMCPU object's two inbound GPIO lines */
68#define ARM_CPU_IRQ 0
69#define ARM_CPU_FIQ 1
Richard Henderson403946c2011-05-04 13:34:29 -070070
balrogc1713132007-04-30 01:26:42 +000071typedef void ARMWriteCPFunc(void *opaque, int cp_info,
72 int srcreg, int operand, uint32_t value);
73typedef uint32_t ARMReadCPFunc(void *opaque, int cp_info,
74 int dstreg, int operand);
75
balrogf93eb9f2008-04-14 20:27:51 +000076struct arm_boot_info;
77
j_mayer6ebbf392007-10-14 07:07:08 +000078#define NB_MMU_MODES 2
79
bellardb7bcbe92005-02-22 19:27:29 +000080/* We currently assume float and double are IEEE single and double
81 precision respectively.
82 Doing runtime conversions is tricky because VFP registers may contain
83 integer values (eg. as the result of a FTOSI instruction).
bellard8e960052005-04-07 19:42:46 +000084 s<2n> maps to the least significant half of d<n>
85 s<2n+1> maps to the most significant half of d<n>
86 */
bellardb7bcbe92005-02-22 19:27:29 +000087
Peter Maydell55d284a2013-08-20 14:54:31 +010088/* CPU state for each instance of a generic timer (in cp15 c14) */
89typedef struct ARMGenericTimer {
90 uint64_t cval; /* Timer CompareValue register */
91 uint32_t ctl; /* Timer Control register */
92} ARMGenericTimer;
93
94#define GTIMER_PHYS 0
95#define GTIMER_VIRT 1
96#define NUM_GTIMERS 2
97
98/* Scale factor for generic timers, ie number of ns per tick.
99 * This gives a 62.5MHz timer.
100 */
101#define GTIMER_SCALE 16
102
bellard2c0262a2003-09-30 20:34:21 +0000103typedef struct CPUARMState {
bellardb5ff1b32005-11-26 10:38:39 +0000104 /* Regs for current mode. */
bellard2c0262a2003-09-30 20:34:21 +0000105 uint32_t regs[16];
Alexander Graf3926cc82013-09-03 20:12:09 +0100106
107 /* 32/64 switch only happens when taking and returning from
108 * exceptions so the overlap semantics are taken care of then
109 * instead of having a complicated union.
110 */
111 /* Regs for A64 mode. */
112 uint64_t xregs[32];
113 uint64_t pc;
114 /* TODO: pstate doesn't correspond to an architectural register;
115 * it would be better modelled as the underlying fields.
116 */
117 uint32_t pstate;
118 uint32_t aarch64; /* 1 if CPU is in aarch64 state; inverse of PSTATE.nRW */
119
Peter Maydellb90372a2012-08-06 17:42:18 +0100120 /* Frequently accessed CPSR bits are stored separately for efficiency.
pbrookd37aca62006-10-22 11:54:30 +0000121 This contains all the other bits. Use cpsr_{read,write} to access
bellardb5ff1b32005-11-26 10:38:39 +0000122 the whole CPSR. */
123 uint32_t uncached_cpsr;
124 uint32_t spsr;
125
126 /* Banked registers. */
127 uint32_t banked_spsr[6];
128 uint32_t banked_r13[6];
129 uint32_t banked_r14[6];
ths3b46e622007-09-17 08:09:54 +0000130
bellardb5ff1b32005-11-26 10:38:39 +0000131 /* These hold r8-r12. */
132 uint32_t usr_regs[5];
133 uint32_t fiq_regs[5];
ths3b46e622007-09-17 08:09:54 +0000134
bellard2c0262a2003-09-30 20:34:21 +0000135 /* cpsr flag cache for faster execution */
136 uint32_t CF; /* 0 or 1 */
137 uint32_t VF; /* V is the bit 31. All other bits are undefined */
pbrook6fbe23d2008-04-01 17:19:11 +0000138 uint32_t NF; /* N is bit 31. All other bits are undefined. */
139 uint32_t ZF; /* Z set if zero. */
bellard99c475a2005-01-31 20:45:13 +0000140 uint32_t QF; /* 0 or 1 */
pbrook9ee6e8b2007-11-11 00:04:49 +0000141 uint32_t GE; /* cpsr[19:16] */
pbrookb26eefb2008-03-31 03:44:26 +0000142 uint32_t thumb; /* cpsr[5]. 0 = arm mode, 1 = thumb mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000143 uint32_t condexec_bits; /* IT bits. cpsr[15:10,26:25]. */
bellard2c0262a2003-09-30 20:34:21 +0000144
bellardb5ff1b32005-11-26 10:38:39 +0000145 /* System control coprocessor (cp15) */
146 struct {
pbrook40f137e2006-02-20 00:33:36 +0000147 uint32_t c0_cpuid;
pbrooka49ea272008-12-19 13:37:53 +0000148 uint32_t c0_cssel; /* Cache size selection. */
bellardb5ff1b32005-11-26 10:38:39 +0000149 uint32_t c1_sys; /* System control register. */
150 uint32_t c1_coproc; /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +0000151 uint32_t c1_xscaleauxcr; /* XScale auxiliary control register. */
Rob Herring2be27622012-01-13 17:25:08 +0000152 uint32_t c1_scr; /* secure config register. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000153 uint32_t c2_base0; /* MMU translation table base 0. */
Peter Maydell891a2fe2012-07-12 10:59:09 +0000154 uint32_t c2_base0_hi; /* MMU translation table base 0, high 32 bits */
155 uint32_t c2_base1; /* MMU translation table base 0. */
156 uint32_t c2_base1_hi; /* MMU translation table base 1, high 32 bits */
pbrookb2fa1792008-10-22 19:22:30 +0000157 uint32_t c2_control; /* MMU translation table base control. */
158 uint32_t c2_mask; /* MMU translation table base selection mask. */
159 uint32_t c2_base_mask; /* MMU translation table base 0 mask. */
pbrookce819862007-05-08 02:30:40 +0000160 uint32_t c2_data; /* MPU data cachable bits. */
161 uint32_t c2_insn; /* MPU instruction cachable bits. */
162 uint32_t c3; /* MMU domain access control register
163 MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +0000164 uint32_t c5_insn; /* Fault status registers. */
165 uint32_t c5_data;
pbrookce819862007-05-08 02:30:40 +0000166 uint32_t c6_region[8]; /* MPU base/size registers. */
bellardb5ff1b32005-11-26 10:38:39 +0000167 uint32_t c6_insn; /* Fault address registers. */
168 uint32_t c6_data;
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +0100169 uint32_t c7_par; /* Translation result. */
Peter Maydell891a2fe2012-07-12 10:59:09 +0000170 uint32_t c7_par_hi; /* Translation result, high 32 bits */
bellardb5ff1b32005-11-26 10:38:39 +0000171 uint32_t c9_insn; /* Cache lockdown registers. */
172 uint32_t c9_data;
Peter Maydell74594c92011-03-22 12:16:16 +0000173 uint32_t c9_pmcr; /* performance monitor control register */
174 uint32_t c9_pmcnten; /* perf monitor counter enables */
175 uint32_t c9_pmovsr; /* perf monitor overflow status */
176 uint32_t c9_pmxevtyper; /* perf monitor event type */
177 uint32_t c9_pmuserenr; /* perf monitor user enable */
178 uint32_t c9_pminten; /* perf monitor interrupt enables */
Nathan Rossi86411362013-10-25 15:44:38 +0100179 uint32_t c12_vbar; /* vector base address register */
bellardb5ff1b32005-11-26 10:38:39 +0000180 uint32_t c13_fcse; /* FCSE PID. */
181 uint32_t c13_context; /* Context ID. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000182 uint32_t c13_tls1; /* User RW Thread register. */
183 uint32_t c13_tls2; /* User RO Thread register. */
184 uint32_t c13_tls3; /* Privileged Thread register. */
Peter Maydell55d284a2013-08-20 14:54:31 +0100185 uint32_t c14_cntfrq; /* Counter Frequency register */
186 uint32_t c14_cntkctl; /* Timer Control register */
187 ARMGenericTimer c14_timer[NUM_GTIMERS];
balrogc1713132007-04-30 01:26:42 +0000188 uint32_t c15_cpar; /* XScale Coprocessor Access Register */
balrogc3d26892007-07-29 17:57:26 +0000189 uint32_t c15_ticonfig; /* TI925T configuration byte. */
190 uint32_t c15_i_max; /* Maximum D-cache dirty line index. */
191 uint32_t c15_i_min; /* Minimum D-cache dirty line index. */
192 uint32_t c15_threadid; /* TI debugger thread-ID. */
Mark Langsdorf7da362d2012-01-05 15:49:06 +0000193 uint32_t c15_config_base_address; /* SCU base address. */
194 uint32_t c15_diagnostic; /* diagnostic register */
195 uint32_t c15_power_diagnostic;
196 uint32_t c15_power_control; /* power control */
bellardb5ff1b32005-11-26 10:38:39 +0000197 } cp15;
pbrook40f137e2006-02-20 00:33:36 +0000198
Alexander Graf3926cc82013-09-03 20:12:09 +0100199 /* System registers (AArch64) */
200 struct {
201 uint64_t tpidr_el0;
202 } sr;
203
pbrook9ee6e8b2007-11-11 00:04:49 +0000204 struct {
205 uint32_t other_sp;
206 uint32_t vecbase;
207 uint32_t basepri;
208 uint32_t control;
209 int current_sp;
210 int exception;
211 int pending_exception;
pbrook9ee6e8b2007-11-11 00:04:49 +0000212 } v7m;
213
pbrookfe1479c2008-12-19 13:18:36 +0000214 /* Thumb-2 EE state. */
215 uint32_t teecr;
216 uint32_t teehbr;
217
bellardb7bcbe92005-02-22 19:27:29 +0000218 /* VFP coprocessor state. */
219 struct {
Alexander Graf3926cc82013-09-03 20:12:09 +0100220 /* VFP/Neon register state. Note that the mapping between S, D and Q
221 * views of the register bank differs between AArch64 and AArch32:
222 * In AArch32:
223 * Qn = regs[2n+1]:regs[2n]
224 * Dn = regs[n]
225 * Sn = regs[n/2] bits 31..0 for even n, and bits 63..32 for odd n
226 * (and regs[32] to regs[63] are inaccessible)
227 * In AArch64:
228 * Qn = regs[2n+1]:regs[2n]
229 * Dn = regs[2n]
230 * Sn = regs[2n] bits 31..0
231 * This corresponds to the architecturally defined mapping between
232 * the two execution states, and means we do not need to explicitly
233 * map these registers when changing states.
234 */
235 float64 regs[64];
bellardb7bcbe92005-02-22 19:27:29 +0000236
pbrook40f137e2006-02-20 00:33:36 +0000237 uint32_t xregs[16];
bellardb7bcbe92005-02-22 19:27:29 +0000238 /* We store these fpcsr fields separately for convenience. */
239 int vec_len;
240 int vec_stride;
241
pbrook9ee6e8b2007-11-11 00:04:49 +0000242 /* scratch space when Tn are not sufficient. */
243 uint32_t scratch[8];
ths3b46e622007-09-17 08:09:54 +0000244
Peter Maydell3a492f32011-01-14 20:39:18 +0100245 /* fp_status is the "normal" fp status. standard_fp_status retains
246 * values corresponding to the ARM "Standard FPSCR Value", ie
247 * default-NaN, flush-to-zero, round-to-nearest and is used by
248 * any operations (generally Neon) which the architecture defines
249 * as controlled by the standard FPSCR value rather than the FPSCR.
250 *
251 * To avoid having to transfer exception bits around, we simply
252 * say that the FPSCR cumulative exception flags are the logical
253 * OR of the flags in the two fp statuses. This relies on the
254 * only thing which needs to read the exception flags being
255 * an explicit FPSCR read.
256 */
bellard53cd6632005-03-13 18:50:23 +0000257 float_status fp_status;
Peter Maydell3a492f32011-01-14 20:39:18 +0100258 float_status standard_fp_status;
bellardb7bcbe92005-02-22 19:27:29 +0000259 } vfp;
Paul Brook426f5ab2009-11-22 21:35:13 +0000260 uint32_t exclusive_addr;
261 uint32_t exclusive_val;
262 uint32_t exclusive_high;
pbrook9ee6e8b2007-11-11 00:04:49 +0000263#if defined(CONFIG_USER_ONLY)
Paul Brook426f5ab2009-11-22 21:35:13 +0000264 uint32_t exclusive_test;
265 uint32_t exclusive_info;
pbrook9ee6e8b2007-11-11 00:04:49 +0000266#endif
bellardb7bcbe92005-02-22 19:27:29 +0000267
balrog18c9b562007-04-30 02:02:17 +0000268 /* iwMMXt coprocessor state. */
269 struct {
270 uint64_t regs[16];
271 uint64_t val;
272
273 uint32_t cregs[16];
274 } iwmmxt;
275
Paul Brookd8fd2952012-03-30 18:02:50 +0100276 /* For mixed endian mode. */
277 bool bswap_code;
278
pbrookce4defa2006-02-09 16:49:55 +0000279#if defined(CONFIG_USER_ONLY)
280 /* For usermode syscall translation. */
281 int eabi;
282#endif
283
bellarda316d332005-11-20 10:32:34 +0000284 CPU_COMMON
285
balrog9d551992007-04-30 02:24:42 +0000286 /* These fields after the common ones so they are preserved on reset. */
Lars Munch9ba8c3f2010-05-08 22:42:43 +0200287
Peter Maydell581be092012-04-20 17:58:31 +0000288 /* Internal CPU feature flags. */
Peter Maydell918f5dc2012-07-12 10:59:06 +0000289 uint64_t features;
Peter Maydell581be092012-04-20 17:58:31 +0000290
Paul Brook983fe822010-04-05 19:34:51 +0100291 void *nvic;
Stefan Weil462a8bc2011-06-23 17:53:48 +0200292 const struct arm_boot_info *boot_info;
bellard2c0262a2003-09-30 20:34:21 +0000293} CPUARMState;
294
Andreas Färber778c3a02012-04-20 07:39:14 +0000295#include "cpu-qom.h"
296
297ARMCPU *cpu_arm_init(const char *cpu_model);
pbrookb26eefb2008-03-31 03:44:26 +0000298void arm_translate_init(void);
Andreas Färber14969262013-01-05 10:18:18 +0100299void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu);
bellard2c0262a2003-09-30 20:34:21 +0000300int cpu_arm_exec(CPUARMState *s);
Christoffer Dall494b00c2013-03-05 00:34:41 +0000301int bank_number(int mode);
bellardb5ff1b32005-11-26 10:38:39 +0000302void switch_mode(CPUARMState *, int);
pbrook9ee6e8b2007-11-11 00:04:49 +0000303uint32_t do_arm_semihosting(CPUARMState *env);
bellardb5ff1b32005-11-26 10:38:39 +0000304
Alexander Graf3926cc82013-09-03 20:12:09 +0100305static inline bool is_a64(CPUARMState *env)
306{
307 return env->aarch64;
308}
309
310#define PSTATE_N_SHIFT 3
311#define PSTATE_N (1 << PSTATE_N_SHIFT)
312#define PSTATE_Z_SHIFT 2
313#define PSTATE_Z (1 << PSTATE_Z_SHIFT)
314#define PSTATE_C_SHIFT 1
315#define PSTATE_C (1 << PSTATE_C_SHIFT)
316#define PSTATE_V_SHIFT 0
317#define PSTATE_V (1 << PSTATE_V_SHIFT)
318
bellard2c0262a2003-09-30 20:34:21 +0000319/* you can call this signal handler from your SIGBUS and SIGSEGV
320 signal handlers to inform the virtual CPU of exceptions. non zero
321 is returned if the signal was handled by the virtual CPU. */
ths5fafdf22007-09-16 21:08:06 +0000322int cpu_arm_signal_handler(int host_signum, void *pinfo,
bellard2c0262a2003-09-30 20:34:21 +0000323 void *puc);
aurel3284a031c2009-02-07 15:19:20 +0000324int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +0000325 int mmu_idx);
Nathan Froyd0b5c1ce2009-08-10 13:37:36 -0700326#define cpu_handle_mmu_fault cpu_arm_handle_mmu_fault
bellard2c0262a2003-09-30 20:34:21 +0000327
Peter Maydell78dbbbe2013-09-10 19:09:32 +0100328#define CPSR_M (0x1fU)
329#define CPSR_T (1U << 5)
330#define CPSR_F (1U << 6)
331#define CPSR_I (1U << 7)
332#define CPSR_A (1U << 8)
333#define CPSR_E (1U << 9)
334#define CPSR_IT_2_7 (0xfc00U)
335#define CPSR_GE (0xfU << 16)
336#define CPSR_RESERVED (0xfU << 20)
337#define CPSR_J (1U << 24)
338#define CPSR_IT_0_1 (3U << 25)
339#define CPSR_Q (1U << 27)
340#define CPSR_V (1U << 28)
341#define CPSR_C (1U << 29)
342#define CPSR_Z (1U << 30)
343#define CPSR_N (1U << 31)
pbrook9ee6e8b2007-11-11 00:04:49 +0000344#define CPSR_NZCV (CPSR_N | CPSR_Z | CPSR_C | CPSR_V)
bellardb5ff1b32005-11-26 10:38:39 +0000345
pbrook9ee6e8b2007-11-11 00:04:49 +0000346#define CPSR_IT (CPSR_IT_0_1 | CPSR_IT_2_7)
347#define CACHED_CPSR_BITS (CPSR_T | CPSR_GE | CPSR_IT | CPSR_Q | CPSR_NZCV)
348/* Bits writable in user mode. */
349#define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
350/* Execution state bits. MRS read as zero, MSR writes ignored. */
351#define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J)
352
bellardb5ff1b32005-11-26 10:38:39 +0000353/* Return the current CPSR value. */
balrog2f4a40e2007-11-13 01:50:15 +0000354uint32_t cpsr_read(CPUARMState *env);
355/* Set the CPSR. Note that some bits of mask must be all-set or all-clear. */
356void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask);
pbrook9ee6e8b2007-11-11 00:04:49 +0000357
358/* Return the current xPSR value. */
359static inline uint32_t xpsr_read(CPUARMState *env)
360{
361 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +0000362 ZF = (env->ZF == 0);
363 return (env->NF & 0x80000000) | (ZF << 30)
pbrook9ee6e8b2007-11-11 00:04:49 +0000364 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
365 | (env->thumb << 24) | ((env->condexec_bits & 3) << 25)
366 | ((env->condexec_bits & 0xfc) << 8)
367 | env->v7m.exception;
bellardb5ff1b32005-11-26 10:38:39 +0000368}
369
pbrook9ee6e8b2007-11-11 00:04:49 +0000370/* Set the xPSR. Note that some bits of mask must be all-set or all-clear. */
371static inline void xpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
372{
pbrook9ee6e8b2007-11-11 00:04:49 +0000373 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +0000374 env->ZF = (~val) & CPSR_Z;
375 env->NF = val;
pbrook9ee6e8b2007-11-11 00:04:49 +0000376 env->CF = (val >> 29) & 1;
377 env->VF = (val << 3) & 0x80000000;
378 }
379 if (mask & CPSR_Q)
380 env->QF = ((val & CPSR_Q) != 0);
381 if (mask & (1 << 24))
382 env->thumb = ((val & (1 << 24)) != 0);
383 if (mask & CPSR_IT_0_1) {
384 env->condexec_bits &= ~3;
385 env->condexec_bits |= (val >> 25) & 3;
386 }
387 if (mask & CPSR_IT_2_7) {
388 env->condexec_bits &= 3;
389 env->condexec_bits |= (val >> 8) & 0xfc;
390 }
391 if (mask & 0x1ff) {
392 env->v7m.exception = val & 0x1ff;
393 }
394}
395
Peter Maydell01653292010-11-24 15:20:04 +0000396/* Return the current FPSCR value. */
397uint32_t vfp_get_fpscr(CPUARMState *env);
398void vfp_set_fpscr(CPUARMState *env, uint32_t val);
399
bellardb5ff1b32005-11-26 10:38:39 +0000400enum arm_cpu_mode {
401 ARM_CPU_MODE_USR = 0x10,
402 ARM_CPU_MODE_FIQ = 0x11,
403 ARM_CPU_MODE_IRQ = 0x12,
404 ARM_CPU_MODE_SVC = 0x13,
405 ARM_CPU_MODE_ABT = 0x17,
406 ARM_CPU_MODE_UND = 0x1b,
407 ARM_CPU_MODE_SYS = 0x1f
408};
409
pbrook40f137e2006-02-20 00:33:36 +0000410/* VFP system registers. */
411#define ARM_VFP_FPSID 0
412#define ARM_VFP_FPSCR 1
pbrook9ee6e8b2007-11-11 00:04:49 +0000413#define ARM_VFP_MVFR1 6
414#define ARM_VFP_MVFR0 7
pbrook40f137e2006-02-20 00:33:36 +0000415#define ARM_VFP_FPEXC 8
416#define ARM_VFP_FPINST 9
417#define ARM_VFP_FPINST2 10
418
balrog18c9b562007-04-30 02:02:17 +0000419/* iwMMXt coprocessor control registers. */
420#define ARM_IWMMXT_wCID 0
421#define ARM_IWMMXT_wCon 1
422#define ARM_IWMMXT_wCSSF 2
423#define ARM_IWMMXT_wCASF 3
424#define ARM_IWMMXT_wCGR0 8
425#define ARM_IWMMXT_wCGR1 9
426#define ARM_IWMMXT_wCGR2 10
427#define ARM_IWMMXT_wCGR3 11
428
Benoit Canetce854d72011-11-09 07:32:59 +0000429/* If adding a feature bit which corresponds to a Linux ELF
430 * HWCAP bit, remember to update the feature-bit-to-hwcap
431 * mapping in linux-user/elfload.c:get_elf_hwcap().
432 */
pbrook40f137e2006-02-20 00:33:36 +0000433enum arm_features {
434 ARM_FEATURE_VFP,
balrogc1713132007-04-30 01:26:42 +0000435 ARM_FEATURE_AUXCR, /* ARM1026 Auxiliary control register. */
436 ARM_FEATURE_XSCALE, /* Intel XScale extensions. */
pbrookce819862007-05-08 02:30:40 +0000437 ARM_FEATURE_IWMMXT, /* Intel iwMMXt extension. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000438 ARM_FEATURE_V6,
439 ARM_FEATURE_V6K,
440 ARM_FEATURE_V7,
441 ARM_FEATURE_THUMB2,
balrogc3d26892007-07-29 17:57:26 +0000442 ARM_FEATURE_MPU, /* Only has Memory Protection Unit, not full MMU. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000443 ARM_FEATURE_VFP3,
Paul Brook60011492009-11-19 16:45:20 +0000444 ARM_FEATURE_VFP_FP16,
pbrook9ee6e8b2007-11-11 00:04:49 +0000445 ARM_FEATURE_NEON,
Peter Maydell47789992011-10-19 16:14:06 +0000446 ARM_FEATURE_THUMB_DIV, /* divide supported in Thumb encoding */
pbrook9ee6e8b2007-11-11 00:04:49 +0000447 ARM_FEATURE_M, /* Microcontroller profile. */
pbrookfe1479c2008-12-19 13:18:36 +0000448 ARM_FEATURE_OMAPCP, /* OMAP specific CP15 ops handling. */
Peter Maydelle1bbf442011-02-03 19:43:22 +0000449 ARM_FEATURE_THUMB2EE,
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400450 ARM_FEATURE_V7MP, /* v7 Multiprocessing Extensions */
451 ARM_FEATURE_V4T,
452 ARM_FEATURE_V5,
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400453 ARM_FEATURE_STRONGARM,
Peter Maydell906879a2011-07-20 10:32:55 +0000454 ARM_FEATURE_VAPA, /* cp15 VA to PA lookups */
Peter Maydellb8b8ea02011-10-19 16:14:06 +0000455 ARM_FEATURE_ARM_DIV, /* divide supported in ARM encoding */
Peter Maydellda97f522011-10-19 16:14:07 +0000456 ARM_FEATURE_VFP4, /* VFPv4 (implies that NEON is v2) */
Peter Maydell0383ac02012-01-25 12:42:29 +0000457 ARM_FEATURE_GENERIC_TIMER,
Andrew Towers06ed5d62012-03-29 02:41:08 +0000458 ARM_FEATURE_MVFR, /* Media and VFP Feature Registers 0 and 1 */
Peter Maydell1047b9d2012-06-20 11:57:15 +0000459 ARM_FEATURE_DUMMY_C15_REGS, /* RAZ/WI all of cp15 crn=15 */
Peter Maydellc4804212012-06-20 11:57:17 +0000460 ARM_FEATURE_CACHE_TEST_CLEAN, /* 926/1026 style test-and-clean ops */
461 ARM_FEATURE_CACHE_DIRTY_REG, /* 1136/1176 cache dirty status register */
462 ARM_FEATURE_CACHE_BLOCK_OPS, /* v6 optional cache block operations */
Peter Maydell81bdde92012-06-20 11:57:20 +0000463 ARM_FEATURE_MPIDR, /* has cp15 MPIDR */
Peter Maydellde9b05b2012-07-12 10:59:05 +0000464 ARM_FEATURE_PXN, /* has Privileged Execute Never bit */
465 ARM_FEATURE_LPAE, /* has Large Physical Address Extension */
Mans Rullgard81e69fb2013-07-15 14:35:25 +0100466 ARM_FEATURE_V8,
Alexander Graf3926cc82013-09-03 20:12:09 +0100467 ARM_FEATURE_AARCH64, /* supports 64 bit mode */
pbrook40f137e2006-02-20 00:33:36 +0000468};
469
470static inline int arm_feature(CPUARMState *env, int feature)
471{
Peter Maydell918f5dc2012-07-12 10:59:06 +0000472 return (env->features & (1ULL << feature)) != 0;
pbrook40f137e2006-02-20 00:33:36 +0000473}
474
Stefan Weil9a78eea2010-10-22 23:03:33 +0200475void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf);
pbrook40f137e2006-02-20 00:33:36 +0000476
pbrook9ee6e8b2007-11-11 00:04:49 +0000477/* Interface between CPU and Interrupt controller. */
478void armv7m_nvic_set_pending(void *opaque, int irq);
479int armv7m_nvic_acknowledge_irq(void *opaque);
480void armv7m_nvic_complete_irq(void *opaque, int irq);
481
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000482/* Interface for defining coprocessor registers.
483 * Registers are defined in tables of arm_cp_reginfo structs
484 * which are passed to define_arm_cp_regs().
485 */
486
487/* When looking up a coprocessor register we look for it
488 * via an integer which encodes all of:
489 * coprocessor number
490 * Crn, Crm, opc1, opc2 fields
491 * 32 or 64 bit register (ie is it accessed via MRC/MCR
492 * or via MRRC/MCRR?)
493 * We allow 4 bits for opc1 because MRRC/MCRR have a 4 bit field.
494 * (In this case crn and opc2 should be zero.)
495 */
496#define ENCODE_CP_REG(cp, is64, crn, crm, opc1, opc2) \
497 (((cp) << 16) | ((is64) << 15) | ((crn) << 11) | \
498 ((crm) << 7) | ((opc1) << 3) | (opc2))
499
Peter Maydell721fae12013-06-25 18:16:07 +0100500/* Note that these must line up with the KVM/ARM register
501 * ID field definitions (kvm.c will check this, but we
502 * can't just use the KVM defines here as the kvm headers
503 * are unavailable to non-KVM-specific files)
504 */
505#define CP_REG_SIZE_SHIFT 52
506#define CP_REG_SIZE_MASK 0x00f0000000000000ULL
507#define CP_REG_SIZE_U32 0x0020000000000000ULL
508#define CP_REG_SIZE_U64 0x0030000000000000ULL
509#define CP_REG_ARM 0x4000000000000000ULL
510
511/* Convert a full 64 bit KVM register ID to the truncated 32 bit
512 * version used as a key for the coprocessor register hashtable
513 */
514static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
515{
516 uint32_t cpregid = kvmid;
517 if ((kvmid & CP_REG_SIZE_MASK) == CP_REG_SIZE_U64) {
518 cpregid |= (1 << 15);
519 }
520 return cpregid;
521}
522
523/* Convert a truncated 32 bit hashtable key into the full
524 * 64 bit KVM register ID.
525 */
526static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)
527{
528 uint64_t kvmid = cpregid & ~(1 << 15);
529 if (cpregid & (1 << 15)) {
530 kvmid |= CP_REG_SIZE_U64 | CP_REG_ARM;
531 } else {
532 kvmid |= CP_REG_SIZE_U32 | CP_REG_ARM;
533 }
534 return kvmid;
535}
536
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000537/* ARMCPRegInfo type field bits. If the SPECIAL bit is set this is a
538 * special-behaviour cp reg and bits [15..8] indicate what behaviour
539 * it has. Otherwise it is a simple cp reg, where CONST indicates that
540 * TCG can assume the value to be constant (ie load at translate time)
541 * and 64BIT indicates a 64 bit wide coprocessor register. SUPPRESS_TB_END
542 * indicates that the TB should not be ended after a write to this register
543 * (the default is that the TB ends after cp writes). OVERRIDE permits
544 * a register definition to override a previous definition for the
545 * same (cp, is64, crn, crm, opc1, opc2) tuple: either the new or the
546 * old must have the OVERRIDE bit set.
Peter Maydell7023ec72013-06-25 18:16:06 +0100547 * NO_MIGRATE indicates that this register should be ignored for migration;
548 * (eg because any state is accessed via some other coprocessor register).
Peter Maydell24527312013-08-20 14:54:31 +0100549 * IO indicates that this register does I/O and therefore its accesses
550 * need to be surrounded by gen_io_start()/gen_io_end(). In particular,
551 * registers which implement clocks or timers require this.
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000552 */
553#define ARM_CP_SPECIAL 1
554#define ARM_CP_CONST 2
555#define ARM_CP_64BIT 4
556#define ARM_CP_SUPPRESS_TB_END 8
557#define ARM_CP_OVERRIDE 16
Peter Maydell7023ec72013-06-25 18:16:06 +0100558#define ARM_CP_NO_MIGRATE 32
Peter Maydell24527312013-08-20 14:54:31 +0100559#define ARM_CP_IO 64
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000560#define ARM_CP_NOP (ARM_CP_SPECIAL | (1 << 8))
561#define ARM_CP_WFI (ARM_CP_SPECIAL | (2 << 8))
562#define ARM_LAST_SPECIAL ARM_CP_WFI
563/* Used only as a terminator for ARMCPRegInfo lists */
564#define ARM_CP_SENTINEL 0xffff
565/* Mask of only the flag bits in a type field */
Peter Maydell24527312013-08-20 14:54:31 +0100566#define ARM_CP_FLAG_MASK 0x7f
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000567
568/* Return true if cptype is a valid type field. This is used to try to
569 * catch errors where the sentinel has been accidentally left off the end
570 * of a list of registers.
571 */
572static inline bool cptype_valid(int cptype)
573{
574 return ((cptype & ~ARM_CP_FLAG_MASK) == 0)
575 || ((cptype & ARM_CP_SPECIAL) &&
Peter Maydell34affee2013-06-25 18:16:06 +0100576 ((cptype & ~ARM_CP_FLAG_MASK) <= ARM_LAST_SPECIAL));
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000577}
578
579/* Access rights:
580 * We define bits for Read and Write access for what rev C of the v7-AR ARM ARM
581 * defines as PL0 (user), PL1 (fiq/irq/svc/abt/und/sys, ie privileged), and
582 * PL2 (hyp). The other level which has Read and Write bits is Secure PL1
583 * (ie any of the privileged modes in Secure state, or Monitor mode).
584 * If a register is accessible in one privilege level it's always accessible
585 * in higher privilege levels too. Since "Secure PL1" also follows this rule
586 * (ie anything visible in PL2 is visible in S-PL1, some things are only
587 * visible in S-PL1) but "Secure PL1" is a bit of a mouthful, we bend the
588 * terminology a little and call this PL3.
589 *
590 * If access permissions for a register are more complex than can be
591 * described with these bits, then use a laxer set of restrictions, and
592 * do the more restrictive/complex check inside a helper function.
593 */
594#define PL3_R 0x80
595#define PL3_W 0x40
596#define PL2_R (0x20 | PL3_R)
597#define PL2_W (0x10 | PL3_W)
598#define PL1_R (0x08 | PL2_R)
599#define PL1_W (0x04 | PL2_W)
600#define PL0_R (0x02 | PL1_R)
601#define PL0_W (0x01 | PL1_W)
602
603#define PL3_RW (PL3_R | PL3_W)
604#define PL2_RW (PL2_R | PL2_W)
605#define PL1_RW (PL1_R | PL1_W)
606#define PL0_RW (PL0_R | PL0_W)
607
608static inline int arm_current_pl(CPUARMState *env)
609{
610 if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
611 return 0;
612 }
613 /* We don't currently implement the Virtualization or TrustZone
614 * extensions, so PL2 and PL3 don't exist for us.
615 */
616 return 1;
617}
618
619typedef struct ARMCPRegInfo ARMCPRegInfo;
620
621/* Access functions for coprocessor registers. These should return
622 * 0 on success, or one of the EXCP_* constants if access should cause
623 * an exception (in which case *value is not written).
624 */
625typedef int CPReadFn(CPUARMState *env, const ARMCPRegInfo *opaque,
626 uint64_t *value);
627typedef int CPWriteFn(CPUARMState *env, const ARMCPRegInfo *opaque,
628 uint64_t value);
629/* Hook function for register reset */
630typedef void CPResetFn(CPUARMState *env, const ARMCPRegInfo *opaque);
631
632#define CP_ANY 0xff
633
634/* Definition of an ARM coprocessor register */
635struct ARMCPRegInfo {
636 /* Name of register (useful mainly for debugging, need not be unique) */
637 const char *name;
638 /* Location of register: coprocessor number and (crn,crm,opc1,opc2)
639 * tuple. Any of crm, opc1 and opc2 may be CP_ANY to indicate a
640 * 'wildcard' field -- any value of that field in the MRC/MCR insn
641 * will be decoded to this register. The register read and write
642 * callbacks will be passed an ARMCPRegInfo with the crn/crm/opc1/opc2
643 * used by the program, so it is possible to register a wildcard and
644 * then behave differently on read/write if necessary.
645 * For 64 bit registers, only crm and opc1 are relevant; crn and opc2
646 * must both be zero.
647 */
648 uint8_t cp;
649 uint8_t crn;
650 uint8_t crm;
651 uint8_t opc1;
652 uint8_t opc2;
653 /* Register type: ARM_CP_* bits/values */
654 int type;
655 /* Access rights: PL*_[RW] */
656 int access;
657 /* The opaque pointer passed to define_arm_cp_regs_with_opaque() when
658 * this register was defined: can be used to hand data through to the
659 * register read/write functions, since they are passed the ARMCPRegInfo*.
660 */
661 void *opaque;
662 /* Value of this register, if it is ARM_CP_CONST. Otherwise, if
663 * fieldoffset is non-zero, the reset value of the register.
664 */
665 uint64_t resetvalue;
666 /* Offset of the field in CPUARMState for this register. This is not
667 * needed if either:
668 * 1. type is ARM_CP_CONST or one of the ARM_CP_SPECIALs
669 * 2. both readfn and writefn are specified
670 */
671 ptrdiff_t fieldoffset; /* offsetof(CPUARMState, field) */
672 /* Function for handling reads of this register. If NULL, then reads
673 * will be done by loading from the offset into CPUARMState specified
674 * by fieldoffset.
675 */
676 CPReadFn *readfn;
677 /* Function for handling writes of this register. If NULL, then writes
678 * will be done by writing to the offset into CPUARMState specified
679 * by fieldoffset.
680 */
681 CPWriteFn *writefn;
Peter Maydell7023ec72013-06-25 18:16:06 +0100682 /* Function for doing a "raw" read; used when we need to copy
683 * coprocessor state to the kernel for KVM or out for
684 * migration. This only needs to be provided if there is also a
685 * readfn and it makes an access permission check.
686 */
687 CPReadFn *raw_readfn;
688 /* Function for doing a "raw" write; used when we need to copy KVM
689 * kernel coprocessor state into userspace, or for inbound
690 * migration. This only needs to be provided if there is also a
691 * writefn and it makes an access permission check or masks out
692 * "unwritable" bits or has write-one-to-clear or similar behaviour.
693 */
694 CPWriteFn *raw_writefn;
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000695 /* Function for resetting the register. If NULL, then reset will be done
696 * by writing resetvalue to the field specified in fieldoffset. If
697 * fieldoffset is 0 then no reset will be done.
698 */
699 CPResetFn *resetfn;
700};
701
702/* Macros which are lvalues for the field in CPUARMState for the
703 * ARMCPRegInfo *ri.
704 */
705#define CPREG_FIELD32(env, ri) \
706 (*(uint32_t *)((char *)(env) + (ri)->fieldoffset))
707#define CPREG_FIELD64(env, ri) \
708 (*(uint64_t *)((char *)(env) + (ri)->fieldoffset))
709
710#define REGINFO_SENTINEL { .type = ARM_CP_SENTINEL }
711
712void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
713 const ARMCPRegInfo *regs, void *opaque);
714void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
715 const ARMCPRegInfo *regs, void *opaque);
716static inline void define_arm_cp_regs(ARMCPU *cpu, const ARMCPRegInfo *regs)
717{
718 define_arm_cp_regs_with_opaque(cpu, regs, 0);
719}
720static inline void define_one_arm_cp_reg(ARMCPU *cpu, const ARMCPRegInfo *regs)
721{
722 define_one_arm_cp_reg_with_opaque(cpu, regs, 0);
723}
724const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp);
725
726/* CPWriteFn that can be used to implement writes-ignored behaviour */
727int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
728 uint64_t value);
729/* CPReadFn that can be used for read-as-zero behaviour */
730int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value);
731
732static inline bool cp_access_ok(CPUARMState *env,
733 const ARMCPRegInfo *ri, int isread)
734{
735 return (ri->access >> ((arm_current_pl(env) * 2) + isread)) & 1;
736}
737
Peter Maydell721fae12013-06-25 18:16:07 +0100738/**
739 * write_list_to_cpustate
740 * @cpu: ARMCPU
741 *
742 * For each register listed in the ARMCPU cpreg_indexes list, write
743 * its value from the cpreg_values list into the ARMCPUState structure.
744 * This updates TCG's working data structures from KVM data or
745 * from incoming migration state.
746 *
747 * Returns: true if all register values were updated correctly,
748 * false if some register was unknown or could not be written.
749 * Note that we do not stop early on failure -- we will attempt
750 * writing all registers in the list.
751 */
752bool write_list_to_cpustate(ARMCPU *cpu);
753
754/**
755 * write_cpustate_to_list:
756 * @cpu: ARMCPU
757 *
758 * For each register listed in the ARMCPU cpreg_indexes list, write
759 * its value from the ARMCPUState structure into the cpreg_values list.
760 * This is used to copy info from TCG's working data structures into
761 * KVM or for outbound migration.
762 *
763 * Returns: true if all register values were read correctly,
764 * false if some register was unknown or could not be read.
765 * Note that we do not stop early on failure -- we will attempt
766 * reading all registers in the list.
767 */
768bool write_cpustate_to_list(ARMCPU *cpu);
769
pbrook9ee6e8b2007-11-11 00:04:49 +0000770/* Does the core conform to the the "MicroController" profile. e.g. Cortex-M3.
771 Note the M in older cores (eg. ARM7TDMI) stands for Multiply. These are
772 conventional cores (ie. Application or Realtime profile). */
773
774#define IS_M(env) arm_feature(env, ARM_FEATURE_M)
pbrook9ee6e8b2007-11-11 00:04:49 +0000775
pbrook9ee6e8b2007-11-11 00:04:49 +0000776#define ARM_CPUID_TI915T 0x54029152
777#define ARM_CPUID_TI925T 0x54029252
pbrook40f137e2006-02-20 00:33:36 +0000778
bellardb5ff1b32005-11-26 10:38:39 +0000779#if defined(CONFIG_USER_ONLY)
bellard2c0262a2003-09-30 20:34:21 +0000780#define TARGET_PAGE_BITS 12
bellardb5ff1b32005-11-26 10:38:39 +0000781#else
782/* The ARM MMU allows 1k pages. */
783/* ??? Linux doesn't actually use these, and they're deprecated in recent
balrog82d17972007-07-24 01:07:44 +0000784 architecture revisions. Maybe a configure option to disable them. */
bellardb5ff1b32005-11-26 10:38:39 +0000785#define TARGET_PAGE_BITS 10
786#endif
ths9467d442007-06-03 21:02:38 +0000787
Alexander Graf3926cc82013-09-03 20:12:09 +0100788#if defined(TARGET_AARCH64)
789# define TARGET_PHYS_ADDR_SPACE_BITS 48
790# define TARGET_VIRT_ADDR_SPACE_BITS 64
791#else
792# define TARGET_PHYS_ADDR_SPACE_BITS 40
793# define TARGET_VIRT_ADDR_SPACE_BITS 32
794#endif
Richard Henderson52705892010-03-10 14:33:23 -0800795
Peter Maydellad37ad52012-05-10 12:56:09 +0000796static inline CPUARMState *cpu_init(const char *cpu_model)
797{
798 ARMCPU *cpu = cpu_arm_init(cpu_model);
799 if (cpu) {
800 return &cpu->env;
801 }
802 return NULL;
803}
804
ths9467d442007-06-03 21:02:38 +0000805#define cpu_exec cpu_arm_exec
806#define cpu_gen_code cpu_arm_gen_code
807#define cpu_signal_handler cpu_arm_signal_handler
j_mayerc732abe2007-10-12 06:47:46 +0000808#define cpu_list arm_cpu_list
ths9467d442007-06-03 21:02:38 +0000809
j_mayer6ebbf392007-10-14 07:07:08 +0000810/* MMU modes definitions */
811#define MMU_MODE0_SUFFIX _kernel
812#define MMU_MODE1_SUFFIX _user
813#define MMU_USER_IDX 1
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100814static inline int cpu_mmu_index (CPUARMState *env)
j_mayer6ebbf392007-10-14 07:07:08 +0000815{
816 return (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR ? 1 : 0;
817}
818
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100819#include "exec/cpu-all.h"
aliguori622ed362008-11-18 19:36:03 +0000820
Alexander Graf3926cc82013-09-03 20:12:09 +0100821/* Bit usage in the TB flags field: bit 31 indicates whether we are
822 * in 32 or 64 bit mode. The meaning of the other bits depends on that.
823 */
824#define ARM_TBFLAG_AARCH64_STATE_SHIFT 31
825#define ARM_TBFLAG_AARCH64_STATE_MASK (1U << ARM_TBFLAG_AARCH64_STATE_SHIFT)
826
827/* Bit usage when in AArch32 state: */
Peter Maydella1705762011-01-14 20:39:18 +0100828#define ARM_TBFLAG_THUMB_SHIFT 0
829#define ARM_TBFLAG_THUMB_MASK (1 << ARM_TBFLAG_THUMB_SHIFT)
830#define ARM_TBFLAG_VECLEN_SHIFT 1
831#define ARM_TBFLAG_VECLEN_MASK (0x7 << ARM_TBFLAG_VECLEN_SHIFT)
832#define ARM_TBFLAG_VECSTRIDE_SHIFT 4
833#define ARM_TBFLAG_VECSTRIDE_MASK (0x3 << ARM_TBFLAG_VECSTRIDE_SHIFT)
834#define ARM_TBFLAG_PRIV_SHIFT 6
835#define ARM_TBFLAG_PRIV_MASK (1 << ARM_TBFLAG_PRIV_SHIFT)
836#define ARM_TBFLAG_VFPEN_SHIFT 7
837#define ARM_TBFLAG_VFPEN_MASK (1 << ARM_TBFLAG_VFPEN_SHIFT)
838#define ARM_TBFLAG_CONDEXEC_SHIFT 8
839#define ARM_TBFLAG_CONDEXEC_MASK (0xff << ARM_TBFLAG_CONDEXEC_SHIFT)
Paul Brookd8fd2952012-03-30 18:02:50 +0100840#define ARM_TBFLAG_BSWAP_CODE_SHIFT 16
841#define ARM_TBFLAG_BSWAP_CODE_MASK (1 << ARM_TBFLAG_BSWAP_CODE_SHIFT)
Alexander Graf3926cc82013-09-03 20:12:09 +0100842
843/* Bit usage when in AArch64 state: currently no bits defined */
Peter Maydella1705762011-01-14 20:39:18 +0100844
845/* some convenience accessor macros */
Alexander Graf3926cc82013-09-03 20:12:09 +0100846#define ARM_TBFLAG_AARCH64_STATE(F) \
847 (((F) & ARM_TBFLAG_AARCH64_STATE_MASK) >> ARM_TBFLAG_AARCH64_STATE_SHIFT)
Peter Maydella1705762011-01-14 20:39:18 +0100848#define ARM_TBFLAG_THUMB(F) \
849 (((F) & ARM_TBFLAG_THUMB_MASK) >> ARM_TBFLAG_THUMB_SHIFT)
850#define ARM_TBFLAG_VECLEN(F) \
851 (((F) & ARM_TBFLAG_VECLEN_MASK) >> ARM_TBFLAG_VECLEN_SHIFT)
852#define ARM_TBFLAG_VECSTRIDE(F) \
853 (((F) & ARM_TBFLAG_VECSTRIDE_MASK) >> ARM_TBFLAG_VECSTRIDE_SHIFT)
854#define ARM_TBFLAG_PRIV(F) \
855 (((F) & ARM_TBFLAG_PRIV_MASK) >> ARM_TBFLAG_PRIV_SHIFT)
856#define ARM_TBFLAG_VFPEN(F) \
857 (((F) & ARM_TBFLAG_VFPEN_MASK) >> ARM_TBFLAG_VFPEN_SHIFT)
858#define ARM_TBFLAG_CONDEXEC(F) \
859 (((F) & ARM_TBFLAG_CONDEXEC_MASK) >> ARM_TBFLAG_CONDEXEC_SHIFT)
Paul Brookd8fd2952012-03-30 18:02:50 +0100860#define ARM_TBFLAG_BSWAP_CODE(F) \
861 (((F) & ARM_TBFLAG_BSWAP_CODE_MASK) >> ARM_TBFLAG_BSWAP_CODE_SHIFT)
Peter Maydella1705762011-01-14 20:39:18 +0100862
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100863static inline void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
aliguori6b917542008-11-18 19:46:41 +0000864 target_ulong *cs_base, int *flags)
865{
Alexander Graf3926cc82013-09-03 20:12:09 +0100866 if (is_a64(env)) {
867 *pc = env->pc;
868 *flags = ARM_TBFLAG_AARCH64_STATE_MASK;
Peter Maydell05ed9a92011-01-14 20:39:19 +0100869 } else {
Alexander Graf3926cc82013-09-03 20:12:09 +0100870 int privmode;
871 *pc = env->regs[15];
872 *flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
873 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
874 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
875 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
876 | (env->bswap_code << ARM_TBFLAG_BSWAP_CODE_SHIFT);
877 if (arm_feature(env, ARM_FEATURE_M)) {
878 privmode = !((env->v7m.exception == 0) && (env->v7m.control & 1));
879 } else {
880 privmode = (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR;
881 }
882 if (privmode) {
883 *flags |= ARM_TBFLAG_PRIV_MASK;
884 }
885 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
886 *flags |= ARM_TBFLAG_VFPEN_MASK;
887 }
Peter Maydell05ed9a92011-01-14 20:39:19 +0100888 }
Alexander Graf3926cc82013-09-03 20:12:09 +0100889
890 *cs_base = 0;
aliguori6b917542008-11-18 19:46:41 +0000891}
892
Andreas Färber3993c6b2012-05-03 06:43:49 +0200893static inline bool cpu_has_work(CPUState *cpu)
Blue Swirlf081c762011-05-21 07:10:23 +0000894{
Andreas Färber259186a2013-01-17 18:51:17 +0100895 return cpu->interrupt_request &
Blue Swirlf081c762011-05-21 07:10:23 +0000896 (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB);
897}
898
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100899#include "exec/exec-all.h"
Blue Swirlf081c762011-05-21 07:10:23 +0000900
Alexander Graf3926cc82013-09-03 20:12:09 +0100901static inline void cpu_pc_from_tb(CPUARMState *env, TranslationBlock *tb)
902{
903 if (ARM_TBFLAG_AARCH64_STATE(tb->flags)) {
904 env->pc = tb->pc;
905 } else {
906 env->regs[15] = tb->pc;
907 }
908}
909
Paul Brookd8fd2952012-03-30 18:02:50 +0100910/* Load an instruction and return it in the standard little-endian order */
Alexander Graf0a2461f2013-09-03 20:12:05 +0100911static inline uint32_t arm_ldl_code(CPUARMState *env, target_ulong addr,
Blue Swirld31dd732012-09-04 20:25:59 +0000912 bool do_swap)
Paul Brookd8fd2952012-03-30 18:02:50 +0100913{
Blue Swirld31dd732012-09-04 20:25:59 +0000914 uint32_t insn = cpu_ldl_code(env, addr);
Paul Brookd8fd2952012-03-30 18:02:50 +0100915 if (do_swap) {
916 return bswap32(insn);
917 }
918 return insn;
919}
920
921/* Ditto, for a halfword (Thumb) instruction */
Alexander Graf0a2461f2013-09-03 20:12:05 +0100922static inline uint16_t arm_lduw_code(CPUARMState *env, target_ulong addr,
Blue Swirld31dd732012-09-04 20:25:59 +0000923 bool do_swap)
Paul Brookd8fd2952012-03-30 18:02:50 +0100924{
Blue Swirld31dd732012-09-04 20:25:59 +0000925 uint16_t insn = cpu_lduw_code(env, addr);
Paul Brookd8fd2952012-03-30 18:02:50 +0100926 if (do_swap) {
927 return bswap16(insn);
928 }
929 return insn;
930}
931
bellard2c0262a2003-09-30 20:34:21 +0000932#endif