8 #include "qemu-common.h"
9 #include "host-utils.h"
10 #if !defined(CONFIG_USER_ONLY)
11 #include "hw/loader.h"
14 static uint32_t cortexa9_cp15_c0_c1
[8] =
15 { 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
17 static uint32_t cortexa9_cp15_c0_c2
[8] =
18 { 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
20 static uint32_t cortexa8_cp15_c0_c1
[8] =
21 { 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
23 static uint32_t cortexa8_cp15_c0_c2
[8] =
24 { 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
26 static uint32_t mpcore_cp15_c0_c1
[8] =
27 { 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
29 static uint32_t mpcore_cp15_c0_c2
[8] =
30 { 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
32 static uint32_t arm1136_cp15_c0_c1
[8] =
33 { 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
35 static uint32_t arm1136_cp15_c0_c2
[8] =
36 { 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
38 static uint32_t cpu_arm_find_by_name(const char *name
);
40 static inline void set_feature(CPUARMState
*env
, int feature
)
42 env
->features
|= 1u << feature
;
45 static void cpu_reset_model_id(CPUARMState
*env
, uint32_t id
)
47 env
->cp15
.c0_cpuid
= id
;
49 case ARM_CPUID_ARM926
:
50 set_feature(env
, ARM_FEATURE_V4T
);
51 set_feature(env
, ARM_FEATURE_V5
);
52 set_feature(env
, ARM_FEATURE_VFP
);
53 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x41011090;
54 env
->cp15
.c0_cachetype
= 0x1dd20d2;
55 env
->cp15
.c1_sys
= 0x00090078;
57 case ARM_CPUID_ARM946
:
58 set_feature(env
, ARM_FEATURE_V4T
);
59 set_feature(env
, ARM_FEATURE_V5
);
60 set_feature(env
, ARM_FEATURE_MPU
);
61 env
->cp15
.c0_cachetype
= 0x0f004006;
62 env
->cp15
.c1_sys
= 0x00000078;
64 case ARM_CPUID_ARM1026
:
65 set_feature(env
, ARM_FEATURE_V4T
);
66 set_feature(env
, ARM_FEATURE_V5
);
67 set_feature(env
, ARM_FEATURE_VFP
);
68 set_feature(env
, ARM_FEATURE_AUXCR
);
69 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410110a0;
70 env
->cp15
.c0_cachetype
= 0x1dd20d2;
71 env
->cp15
.c1_sys
= 0x00090078;
73 case ARM_CPUID_ARM1136
:
74 /* This is the 1136 r1, which is a v6K core */
75 set_feature(env
, ARM_FEATURE_V6K
);
77 case ARM_CPUID_ARM1136_R2
:
78 /* What qemu calls "arm1136_r2" is actually the 1136 r0p2, ie an
79 * older core than plain "arm1136". In particular this does not
80 * have the v6K features.
82 set_feature(env
, ARM_FEATURE_V4T
);
83 set_feature(env
, ARM_FEATURE_V5
);
84 set_feature(env
, ARM_FEATURE_V6
);
85 set_feature(env
, ARM_FEATURE_VFP
);
86 set_feature(env
, ARM_FEATURE_AUXCR
);
87 /* These ID register values are correct for 1136 but may be wrong
88 * for 1136_r2 (in particular r0p2 does not actually implement most
89 * of the ID registers).
91 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410120b4;
92 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11111111;
93 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00000000;
94 memcpy(env
->cp15
.c0_c1
, arm1136_cp15_c0_c1
, 8 * sizeof(uint32_t));
95 memcpy(env
->cp15
.c0_c2
, arm1136_cp15_c0_c2
, 8 * sizeof(uint32_t));
96 env
->cp15
.c0_cachetype
= 0x1dd20d2;
97 env
->cp15
.c1_sys
= 0x00050078;
99 case ARM_CPUID_ARM11MPCORE
:
100 set_feature(env
, ARM_FEATURE_V4T
);
101 set_feature(env
, ARM_FEATURE_V5
);
102 set_feature(env
, ARM_FEATURE_V6
);
103 set_feature(env
, ARM_FEATURE_V6K
);
104 set_feature(env
, ARM_FEATURE_VFP
);
105 set_feature(env
, ARM_FEATURE_AUXCR
);
106 set_feature(env
, ARM_FEATURE_VAPA
);
107 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410120b4;
108 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11111111;
109 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00000000;
110 memcpy(env
->cp15
.c0_c1
, mpcore_cp15_c0_c1
, 8 * sizeof(uint32_t));
111 memcpy(env
->cp15
.c0_c2
, mpcore_cp15_c0_c2
, 8 * sizeof(uint32_t));
112 env
->cp15
.c0_cachetype
= 0x1dd20d2;
114 case ARM_CPUID_CORTEXA8
:
115 set_feature(env
, ARM_FEATURE_V4T
);
116 set_feature(env
, ARM_FEATURE_V5
);
117 set_feature(env
, ARM_FEATURE_V6
);
118 set_feature(env
, ARM_FEATURE_V6K
);
119 set_feature(env
, ARM_FEATURE_V7
);
120 set_feature(env
, ARM_FEATURE_AUXCR
);
121 set_feature(env
, ARM_FEATURE_THUMB2
);
122 set_feature(env
, ARM_FEATURE_VFP
);
123 set_feature(env
, ARM_FEATURE_VFP3
);
124 set_feature(env
, ARM_FEATURE_NEON
);
125 set_feature(env
, ARM_FEATURE_THUMB2EE
);
126 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410330c0;
127 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11110222;
128 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00011100;
129 memcpy(env
->cp15
.c0_c1
, cortexa8_cp15_c0_c1
, 8 * sizeof(uint32_t));
130 memcpy(env
->cp15
.c0_c2
, cortexa8_cp15_c0_c2
, 8 * sizeof(uint32_t));
131 env
->cp15
.c0_cachetype
= 0x82048004;
132 env
->cp15
.c0_clid
= (1 << 27) | (2 << 24) | 3;
133 env
->cp15
.c0_ccsid
[0] = 0xe007e01a; /* 16k L1 dcache. */
134 env
->cp15
.c0_ccsid
[1] = 0x2007e01a; /* 16k L1 icache. */
135 env
->cp15
.c0_ccsid
[2] = 0xf0000000; /* No L2 icache. */
136 env
->cp15
.c1_sys
= 0x00c50078;
138 case ARM_CPUID_CORTEXA9
:
139 set_feature(env
, ARM_FEATURE_V4T
);
140 set_feature(env
, ARM_FEATURE_V5
);
141 set_feature(env
, ARM_FEATURE_V6
);
142 set_feature(env
, ARM_FEATURE_V6K
);
143 set_feature(env
, ARM_FEATURE_V7
);
144 set_feature(env
, ARM_FEATURE_AUXCR
);
145 set_feature(env
, ARM_FEATURE_THUMB2
);
146 set_feature(env
, ARM_FEATURE_VFP
);
147 set_feature(env
, ARM_FEATURE_VFP3
);
148 set_feature(env
, ARM_FEATURE_VFP_FP16
);
149 set_feature(env
, ARM_FEATURE_NEON
);
150 set_feature(env
, ARM_FEATURE_THUMB2EE
);
151 /* Note that A9 supports the MP extensions even for
152 * A9UP and single-core A9MP (which are both different
153 * and valid configurations; we don't model A9UP).
155 set_feature(env
, ARM_FEATURE_V7MP
);
156 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x41034000; /* Guess */
157 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11110222;
158 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x01111111;
159 memcpy(env
->cp15
.c0_c1
, cortexa9_cp15_c0_c1
, 8 * sizeof(uint32_t));
160 memcpy(env
->cp15
.c0_c2
, cortexa9_cp15_c0_c2
, 8 * sizeof(uint32_t));
161 env
->cp15
.c0_cachetype
= 0x80038003;
162 env
->cp15
.c0_clid
= (1 << 27) | (1 << 24) | 3;
163 env
->cp15
.c0_ccsid
[0] = 0xe00fe015; /* 16k L1 dcache. */
164 env
->cp15
.c0_ccsid
[1] = 0x200fe015; /* 16k L1 icache. */
165 env
->cp15
.c1_sys
= 0x00c50078;
167 case ARM_CPUID_CORTEXM3
:
168 set_feature(env
, ARM_FEATURE_V4T
);
169 set_feature(env
, ARM_FEATURE_V5
);
170 set_feature(env
, ARM_FEATURE_V6
);
171 set_feature(env
, ARM_FEATURE_THUMB2
);
172 set_feature(env
, ARM_FEATURE_V7
);
173 set_feature(env
, ARM_FEATURE_M
);
174 set_feature(env
, ARM_FEATURE_DIV
);
176 case ARM_CPUID_ANY
: /* For userspace emulation. */
177 set_feature(env
, ARM_FEATURE_V4T
);
178 set_feature(env
, ARM_FEATURE_V5
);
179 set_feature(env
, ARM_FEATURE_V6
);
180 set_feature(env
, ARM_FEATURE_V6K
);
181 set_feature(env
, ARM_FEATURE_V7
);
182 set_feature(env
, ARM_FEATURE_THUMB2
);
183 set_feature(env
, ARM_FEATURE_VFP
);
184 set_feature(env
, ARM_FEATURE_VFP3
);
185 set_feature(env
, ARM_FEATURE_VFP_FP16
);
186 set_feature(env
, ARM_FEATURE_NEON
);
187 set_feature(env
, ARM_FEATURE_THUMB2EE
);
188 set_feature(env
, ARM_FEATURE_DIV
);
189 set_feature(env
, ARM_FEATURE_V7MP
);
191 case ARM_CPUID_TI915T
:
192 case ARM_CPUID_TI925T
:
193 set_feature(env
, ARM_FEATURE_V4T
);
194 set_feature(env
, ARM_FEATURE_OMAPCP
);
195 env
->cp15
.c0_cpuid
= ARM_CPUID_TI925T
; /* Depends on wiring. */
196 env
->cp15
.c0_cachetype
= 0x5109149;
197 env
->cp15
.c1_sys
= 0x00000070;
198 env
->cp15
.c15_i_max
= 0x000;
199 env
->cp15
.c15_i_min
= 0xff0;
201 case ARM_CPUID_PXA250
:
202 case ARM_CPUID_PXA255
:
203 case ARM_CPUID_PXA260
:
204 case ARM_CPUID_PXA261
:
205 case ARM_CPUID_PXA262
:
206 set_feature(env
, ARM_FEATURE_V4T
);
207 set_feature(env
, ARM_FEATURE_V5
);
208 set_feature(env
, ARM_FEATURE_XSCALE
);
209 /* JTAG_ID is ((id << 28) | 0x09265013) */
210 env
->cp15
.c0_cachetype
= 0xd172172;
211 env
->cp15
.c1_sys
= 0x00000078;
213 case ARM_CPUID_PXA270_A0
:
214 case ARM_CPUID_PXA270_A1
:
215 case ARM_CPUID_PXA270_B0
:
216 case ARM_CPUID_PXA270_B1
:
217 case ARM_CPUID_PXA270_C0
:
218 case ARM_CPUID_PXA270_C5
:
219 set_feature(env
, ARM_FEATURE_V4T
);
220 set_feature(env
, ARM_FEATURE_V5
);
221 set_feature(env
, ARM_FEATURE_XSCALE
);
222 /* JTAG_ID is ((id << 28) | 0x09265013) */
223 set_feature(env
, ARM_FEATURE_IWMMXT
);
224 env
->iwmmxt
.cregs
[ARM_IWMMXT_wCID
] = 0x69051000 | 'Q';
225 env
->cp15
.c0_cachetype
= 0xd172172;
226 env
->cp15
.c1_sys
= 0x00000078;
228 case ARM_CPUID_SA1100
:
229 case ARM_CPUID_SA1110
:
230 set_feature(env
, ARM_FEATURE_STRONGARM
);
231 env
->cp15
.c1_sys
= 0x00000070;
234 cpu_abort(env
, "Bad CPU ID: %x\n", id
);
238 /* Some features automatically imply others: */
239 if (arm_feature(env
, ARM_FEATURE_V7
)) {
240 set_feature(env
, ARM_FEATURE_VAPA
);
244 void cpu_reset(CPUARMState
*env
)
248 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
249 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
250 log_cpu_state(env
, 0);
253 id
= env
->cp15
.c0_cpuid
;
254 memset(env
, 0, offsetof(CPUARMState
, breakpoints
));
256 cpu_reset_model_id(env
, id
);
257 #if defined (CONFIG_USER_ONLY)
258 env
->uncached_cpsr
= ARM_CPU_MODE_USR
;
259 /* For user mode we must enable access to coprocessors */
260 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = 1 << 30;
261 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
262 env
->cp15
.c15_cpar
= 3;
263 } else if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
264 env
->cp15
.c15_cpar
= 1;
267 /* SVC mode with interrupts disabled. */
268 env
->uncached_cpsr
= ARM_CPU_MODE_SVC
| CPSR_A
| CPSR_F
| CPSR_I
;
269 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
270 clear at reset. Initial SP and PC are loaded from ROM. */
274 env
->uncached_cpsr
&= ~CPSR_I
;
277 /* We should really use ldl_phys here, in case the guest
278 modified flash and reset itself. However images
279 loaded via -kenrel have not been copied yet, so load the
280 values directly from there. */
281 env
->regs
[13] = ldl_p(rom
);
284 env
->regs
[15] = pc
& ~1;
287 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = 0;
288 env
->cp15
.c2_base_mask
= 0xffffc000u
;
289 /* v7 performance monitor control register: same implementor
290 * field as main ID register, and we implement no event counters.
292 env
->cp15
.c9_pmcr
= (id
& 0xff000000);
294 set_flush_to_zero(1, &env
->vfp
.standard_fp_status
);
295 set_flush_inputs_to_zero(1, &env
->vfp
.standard_fp_status
);
296 set_default_nan_mode(1, &env
->vfp
.standard_fp_status
);
297 set_float_detect_tininess(float_tininess_before_rounding
,
298 &env
->vfp
.fp_status
);
299 set_float_detect_tininess(float_tininess_before_rounding
,
300 &env
->vfp
.standard_fp_status
);
304 static int vfp_gdb_get_reg(CPUState
*env
, uint8_t *buf
, int reg
)
308 /* VFP data registers are always little-endian. */
309 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
311 stfq_le_p(buf
, env
->vfp
.regs
[reg
]);
314 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
315 /* Aliases for Q regs. */
318 stfq_le_p(buf
, env
->vfp
.regs
[(reg
- 32) * 2]);
319 stfq_le_p(buf
+ 8, env
->vfp
.regs
[(reg
- 32) * 2 + 1]);
323 switch (reg
- nregs
) {
324 case 0: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]); return 4;
325 case 1: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSCR
]); return 4;
326 case 2: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]); return 4;
331 static int vfp_gdb_set_reg(CPUState
*env
, uint8_t *buf
, int reg
)
335 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
337 env
->vfp
.regs
[reg
] = ldfq_le_p(buf
);
340 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
343 env
->vfp
.regs
[(reg
- 32) * 2] = ldfq_le_p(buf
);
344 env
->vfp
.regs
[(reg
- 32) * 2 + 1] = ldfq_le_p(buf
+ 8);
348 switch (reg
- nregs
) {
349 case 0: env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
); return 4;
350 case 1: env
->vfp
.xregs
[ARM_VFP_FPSCR
] = ldl_p(buf
); return 4;
351 case 2: env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30); return 4;
356 CPUARMState
*cpu_arm_init(const char *cpu_model
)
360 static int inited
= 0;
362 id
= cpu_arm_find_by_name(cpu_model
);
365 env
= qemu_mallocz(sizeof(CPUARMState
));
369 arm_translate_init();
372 env
->cpu_model_str
= cpu_model
;
373 env
->cp15
.c0_cpuid
= id
;
375 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
376 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
377 51, "arm-neon.xml", 0);
378 } else if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
379 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
380 35, "arm-vfp3.xml", 0);
381 } else if (arm_feature(env
, ARM_FEATURE_VFP
)) {
382 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
383 19, "arm-vfp.xml", 0);
394 static const struct arm_cpu_t arm_cpu_names
[] = {
395 { ARM_CPUID_ARM926
, "arm926"},
396 { ARM_CPUID_ARM946
, "arm946"},
397 { ARM_CPUID_ARM1026
, "arm1026"},
398 { ARM_CPUID_ARM1136
, "arm1136"},
399 { ARM_CPUID_ARM1136_R2
, "arm1136-r2"},
400 { ARM_CPUID_ARM11MPCORE
, "arm11mpcore"},
401 { ARM_CPUID_CORTEXM3
, "cortex-m3"},
402 { ARM_CPUID_CORTEXA8
, "cortex-a8"},
403 { ARM_CPUID_CORTEXA9
, "cortex-a9"},
404 { ARM_CPUID_TI925T
, "ti925t" },
405 { ARM_CPUID_PXA250
, "pxa250" },
406 { ARM_CPUID_SA1100
, "sa1100" },
407 { ARM_CPUID_SA1110
, "sa1110" },
408 { ARM_CPUID_PXA255
, "pxa255" },
409 { ARM_CPUID_PXA260
, "pxa260" },
410 { ARM_CPUID_PXA261
, "pxa261" },
411 { ARM_CPUID_PXA262
, "pxa262" },
412 { ARM_CPUID_PXA270
, "pxa270" },
413 { ARM_CPUID_PXA270_A0
, "pxa270-a0" },
414 { ARM_CPUID_PXA270_A1
, "pxa270-a1" },
415 { ARM_CPUID_PXA270_B0
, "pxa270-b0" },
416 { ARM_CPUID_PXA270_B1
, "pxa270-b1" },
417 { ARM_CPUID_PXA270_C0
, "pxa270-c0" },
418 { ARM_CPUID_PXA270_C5
, "pxa270-c5" },
419 { ARM_CPUID_ANY
, "any"},
423 void arm_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
427 (*cpu_fprintf
)(f
, "Available CPUs:\n");
428 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
429 (*cpu_fprintf
)(f
, " %s\n", arm_cpu_names
[i
].name
);
433 /* return 0 if not found */
434 static uint32_t cpu_arm_find_by_name(const char *name
)
440 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
441 if (strcmp(name
, arm_cpu_names
[i
].name
) == 0) {
442 id
= arm_cpu_names
[i
].id
;
449 void cpu_arm_close(CPUARMState
*env
)
454 uint32_t cpsr_read(CPUARMState
*env
)
458 return env
->uncached_cpsr
| (env
->NF
& 0x80000000) | (ZF
<< 30) |
459 (env
->CF
<< 29) | ((env
->VF
& 0x80000000) >> 3) | (env
->QF
<< 27)
460 | (env
->thumb
<< 5) | ((env
->condexec_bits
& 3) << 25)
461 | ((env
->condexec_bits
& 0xfc) << 8)
465 void cpsr_write(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
467 if (mask
& CPSR_NZCV
) {
468 env
->ZF
= (~val
) & CPSR_Z
;
470 env
->CF
= (val
>> 29) & 1;
471 env
->VF
= (val
<< 3) & 0x80000000;
474 env
->QF
= ((val
& CPSR_Q
) != 0);
476 env
->thumb
= ((val
& CPSR_T
) != 0);
477 if (mask
& CPSR_IT_0_1
) {
478 env
->condexec_bits
&= ~3;
479 env
->condexec_bits
|= (val
>> 25) & 3;
481 if (mask
& CPSR_IT_2_7
) {
482 env
->condexec_bits
&= 3;
483 env
->condexec_bits
|= (val
>> 8) & 0xfc;
485 if (mask
& CPSR_GE
) {
486 env
->GE
= (val
>> 16) & 0xf;
489 if ((env
->uncached_cpsr
^ val
) & mask
& CPSR_M
) {
490 switch_mode(env
, val
& CPSR_M
);
492 mask
&= ~CACHED_CPSR_BITS
;
493 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~mask
) | (val
& mask
);
496 /* Sign/zero extend */
497 uint32_t HELPER(sxtb16
)(uint32_t x
)
500 res
= (uint16_t)(int8_t)x
;
501 res
|= (uint32_t)(int8_t)(x
>> 16) << 16;
505 uint32_t HELPER(uxtb16
)(uint32_t x
)
508 res
= (uint16_t)(uint8_t)x
;
509 res
|= (uint32_t)(uint8_t)(x
>> 16) << 16;
513 uint32_t HELPER(clz
)(uint32_t x
)
518 int32_t HELPER(sdiv
)(int32_t num
, int32_t den
)
522 if (num
== INT_MIN
&& den
== -1)
527 uint32_t HELPER(udiv
)(uint32_t num
, uint32_t den
)
534 uint32_t HELPER(rbit
)(uint32_t x
)
536 x
= ((x
& 0xff000000) >> 24)
537 | ((x
& 0x00ff0000) >> 8)
538 | ((x
& 0x0000ff00) << 8)
539 | ((x
& 0x000000ff) << 24);
540 x
= ((x
& 0xf0f0f0f0) >> 4)
541 | ((x
& 0x0f0f0f0f) << 4);
542 x
= ((x
& 0x88888888) >> 3)
543 | ((x
& 0x44444444) >> 1)
544 | ((x
& 0x22222222) << 1)
545 | ((x
& 0x11111111) << 3);
549 uint32_t HELPER(abs
)(uint32_t x
)
551 return ((int32_t)x
< 0) ? -x
: x
;
554 #if defined(CONFIG_USER_ONLY)
556 void do_interrupt (CPUState
*env
)
558 env
->exception_index
= -1;
561 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
562 int mmu_idx
, int is_softmmu
)
565 env
->exception_index
= EXCP_PREFETCH_ABORT
;
566 env
->cp15
.c6_insn
= address
;
568 env
->exception_index
= EXCP_DATA_ABORT
;
569 env
->cp15
.c6_data
= address
;
574 /* These should probably raise undefined insn exceptions. */
575 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
577 int op1
= (insn
>> 8) & 0xf;
578 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
582 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
584 int op1
= (insn
>> 8) & 0xf;
585 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
589 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
591 cpu_abort(env
, "cp15 insn %08x\n", insn
);
594 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
596 cpu_abort(env
, "cp15 insn %08x\n", insn
);
599 /* These should probably raise undefined insn exceptions. */
600 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
602 cpu_abort(env
, "v7m_mrs %d\n", reg
);
605 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
607 cpu_abort(env
, "v7m_mrs %d\n", reg
);
611 void switch_mode(CPUState
*env
, int mode
)
613 if (mode
!= ARM_CPU_MODE_USR
)
614 cpu_abort(env
, "Tried to switch out of user mode\n");
617 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
619 cpu_abort(env
, "banked r13 write\n");
622 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
624 cpu_abort(env
, "banked r13 read\n");
630 extern int semihosting_enabled
;
632 /* Map CPU modes onto saved register banks. */
633 static inline int bank_number (int mode
)
636 case ARM_CPU_MODE_USR
:
637 case ARM_CPU_MODE_SYS
:
639 case ARM_CPU_MODE_SVC
:
641 case ARM_CPU_MODE_ABT
:
643 case ARM_CPU_MODE_UND
:
645 case ARM_CPU_MODE_IRQ
:
647 case ARM_CPU_MODE_FIQ
:
650 cpu_abort(cpu_single_env
, "Bad mode %x\n", mode
);
654 void switch_mode(CPUState
*env
, int mode
)
659 old_mode
= env
->uncached_cpsr
& CPSR_M
;
660 if (mode
== old_mode
)
663 if (old_mode
== ARM_CPU_MODE_FIQ
) {
664 memcpy (env
->fiq_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
665 memcpy (env
->regs
+ 8, env
->usr_regs
, 5 * sizeof(uint32_t));
666 } else if (mode
== ARM_CPU_MODE_FIQ
) {
667 memcpy (env
->usr_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
668 memcpy (env
->regs
+ 8, env
->fiq_regs
, 5 * sizeof(uint32_t));
671 i
= bank_number(old_mode
);
672 env
->banked_r13
[i
] = env
->regs
[13];
673 env
->banked_r14
[i
] = env
->regs
[14];
674 env
->banked_spsr
[i
] = env
->spsr
;
676 i
= bank_number(mode
);
677 env
->regs
[13] = env
->banked_r13
[i
];
678 env
->regs
[14] = env
->banked_r14
[i
];
679 env
->spsr
= env
->banked_spsr
[i
];
682 static void v7m_push(CPUARMState
*env
, uint32_t val
)
685 stl_phys(env
->regs
[13], val
);
688 static uint32_t v7m_pop(CPUARMState
*env
)
691 val
= ldl_phys(env
->regs
[13]);
696 /* Switch to V7M main or process stack pointer. */
697 static void switch_v7m_sp(CPUARMState
*env
, int process
)
700 if (env
->v7m
.current_sp
!= process
) {
701 tmp
= env
->v7m
.other_sp
;
702 env
->v7m
.other_sp
= env
->regs
[13];
704 env
->v7m
.current_sp
= process
;
708 static void do_v7m_exception_exit(CPUARMState
*env
)
713 type
= env
->regs
[15];
714 if (env
->v7m
.exception
!= 0)
715 armv7m_nvic_complete_irq(env
->nvic
, env
->v7m
.exception
);
717 /* Switch to the target stack. */
718 switch_v7m_sp(env
, (type
& 4) != 0);
720 env
->regs
[0] = v7m_pop(env
);
721 env
->regs
[1] = v7m_pop(env
);
722 env
->regs
[2] = v7m_pop(env
);
723 env
->regs
[3] = v7m_pop(env
);
724 env
->regs
[12] = v7m_pop(env
);
725 env
->regs
[14] = v7m_pop(env
);
726 env
->regs
[15] = v7m_pop(env
);
728 xpsr_write(env
, xpsr
, 0xfffffdff);
729 /* Undo stack alignment. */
732 /* ??? The exception return type specifies Thread/Handler mode. However
733 this is also implied by the xPSR value. Not sure what to do
734 if there is a mismatch. */
735 /* ??? Likewise for mismatches between the CONTROL register and the stack
739 static void do_interrupt_v7m(CPUARMState
*env
)
741 uint32_t xpsr
= xpsr_read(env
);
746 if (env
->v7m
.current_sp
)
748 if (env
->v7m
.exception
== 0)
751 /* For exceptions we just mark as pending on the NVIC, and let that
753 /* TODO: Need to escalate if the current priority is higher than the
754 one we're raising. */
755 switch (env
->exception_index
) {
757 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
);
761 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SVC
);
763 case EXCP_PREFETCH_ABORT
:
764 case EXCP_DATA_ABORT
:
765 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
);
768 if (semihosting_enabled
) {
770 nr
= lduw_code(env
->regs
[15]) & 0xff;
773 env
->regs
[0] = do_arm_semihosting(env
);
777 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_DEBUG
);
780 env
->v7m
.exception
= armv7m_nvic_acknowledge_irq(env
->nvic
);
782 case EXCP_EXCEPTION_EXIT
:
783 do_v7m_exception_exit(env
);
786 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
787 return; /* Never happens. Keep compiler happy. */
790 /* Align stack pointer. */
791 /* ??? Should only do this if Configuration Control Register
792 STACKALIGN bit is set. */
793 if (env
->regs
[13] & 4) {
797 /* Switch to the handler mode. */
799 v7m_push(env
, env
->regs
[15]);
800 v7m_push(env
, env
->regs
[14]);
801 v7m_push(env
, env
->regs
[12]);
802 v7m_push(env
, env
->regs
[3]);
803 v7m_push(env
, env
->regs
[2]);
804 v7m_push(env
, env
->regs
[1]);
805 v7m_push(env
, env
->regs
[0]);
806 switch_v7m_sp(env
, 0);
807 env
->uncached_cpsr
&= ~CPSR_IT
;
809 addr
= ldl_phys(env
->v7m
.vecbase
+ env
->v7m
.exception
* 4);
810 env
->regs
[15] = addr
& 0xfffffffe;
811 env
->thumb
= addr
& 1;
814 /* Handle a CPU exception. */
815 void do_interrupt(CPUARMState
*env
)
823 do_interrupt_v7m(env
);
826 /* TODO: Vectored interrupt controller. */
827 switch (env
->exception_index
) {
829 new_mode
= ARM_CPU_MODE_UND
;
838 if (semihosting_enabled
) {
839 /* Check for semihosting interrupt. */
841 mask
= lduw_code(env
->regs
[15] - 2) & 0xff;
843 mask
= ldl_code(env
->regs
[15] - 4) & 0xffffff;
845 /* Only intercept calls from privileged modes, to provide some
846 semblance of security. */
847 if (((mask
== 0x123456 && !env
->thumb
)
848 || (mask
== 0xab && env
->thumb
))
849 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
850 env
->regs
[0] = do_arm_semihosting(env
);
854 new_mode
= ARM_CPU_MODE_SVC
;
857 /* The PC already points to the next instruction. */
861 /* See if this is a semihosting syscall. */
862 if (env
->thumb
&& semihosting_enabled
) {
863 mask
= lduw_code(env
->regs
[15]) & 0xff;
865 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
867 env
->regs
[0] = do_arm_semihosting(env
);
871 env
->cp15
.c5_insn
= 2;
872 /* Fall through to prefetch abort. */
873 case EXCP_PREFETCH_ABORT
:
874 new_mode
= ARM_CPU_MODE_ABT
;
876 mask
= CPSR_A
| CPSR_I
;
879 case EXCP_DATA_ABORT
:
880 new_mode
= ARM_CPU_MODE_ABT
;
882 mask
= CPSR_A
| CPSR_I
;
886 new_mode
= ARM_CPU_MODE_IRQ
;
888 /* Disable IRQ and imprecise data aborts. */
889 mask
= CPSR_A
| CPSR_I
;
893 new_mode
= ARM_CPU_MODE_FIQ
;
895 /* Disable FIQ, IRQ and imprecise data aborts. */
896 mask
= CPSR_A
| CPSR_I
| CPSR_F
;
900 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
901 return; /* Never happens. Keep compiler happy. */
904 if (env
->cp15
.c1_sys
& (1 << 13)) {
907 switch_mode (env
, new_mode
);
908 env
->spsr
= cpsr_read(env
);
910 env
->condexec_bits
= 0;
911 /* Switch to the new mode, and to the correct instruction set. */
912 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~CPSR_M
) | new_mode
;
913 env
->uncached_cpsr
|= mask
;
914 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
915 * and we should just guard the thumb mode on V4 */
916 if (arm_feature(env
, ARM_FEATURE_V4T
)) {
917 env
->thumb
= (env
->cp15
.c1_sys
& (1 << 30)) != 0;
919 env
->regs
[14] = env
->regs
[15] + offset
;
920 env
->regs
[15] = addr
;
921 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
924 /* Check section/page access permissions.
925 Returns the page protection flags, or zero if the access is not
927 static inline int check_ap(CPUState
*env
, int ap
, int domain
, int access_type
,
933 return PAGE_READ
| PAGE_WRITE
;
935 if (access_type
== 1)
942 if (access_type
== 1)
944 switch ((env
->cp15
.c1_sys
>> 8) & 3) {
946 return is_user
? 0 : PAGE_READ
;
953 return is_user
? 0 : PAGE_READ
| PAGE_WRITE
;
958 return PAGE_READ
| PAGE_WRITE
;
960 return PAGE_READ
| PAGE_WRITE
;
961 case 4: /* Reserved. */
964 return is_user
? 0 : prot_ro
;
968 if (!arm_feature (env
, ARM_FEATURE_V7
))
976 static uint32_t get_level1_table_address(CPUState
*env
, uint32_t address
)
980 if (address
& env
->cp15
.c2_mask
)
981 table
= env
->cp15
.c2_base1
& 0xffffc000;
983 table
= env
->cp15
.c2_base0
& env
->cp15
.c2_base_mask
;
985 table
|= (address
>> 18) & 0x3ffc;
989 static int get_phys_addr_v5(CPUState
*env
, uint32_t address
, int access_type
,
990 int is_user
, uint32_t *phys_ptr
, int *prot
,
991 target_ulong
*page_size
)
1001 /* Pagetable walk. */
1002 /* Lookup l1 descriptor. */
1003 table
= get_level1_table_address(env
, address
);
1004 desc
= ldl_phys(table
);
1006 domain
= (env
->cp15
.c3
>> ((desc
>> 4) & 0x1e)) & 3;
1008 /* Section translation fault. */
1012 if (domain
== 0 || domain
== 2) {
1014 code
= 9; /* Section domain fault. */
1016 code
= 11; /* Page domain fault. */
1021 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
1022 ap
= (desc
>> 10) & 3;
1024 *page_size
= 1024 * 1024;
1026 /* Lookup l2 entry. */
1028 /* Coarse pagetable. */
1029 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
1031 /* Fine pagetable. */
1032 table
= (desc
& 0xfffff000) | ((address
>> 8) & 0xffc);
1034 desc
= ldl_phys(table
);
1036 case 0: /* Page translation fault. */
1039 case 1: /* 64k page. */
1040 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
1041 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
1042 *page_size
= 0x10000;
1044 case 2: /* 4k page. */
1045 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
1046 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
1047 *page_size
= 0x1000;
1049 case 3: /* 1k page. */
1051 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1052 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
1054 /* Page translation fault. */
1059 phys_addr
= (desc
& 0xfffffc00) | (address
& 0x3ff);
1061 ap
= (desc
>> 4) & 3;
1065 /* Never happens, but compiler isn't smart enough to tell. */
1070 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1072 /* Access permission fault. */
1076 *phys_ptr
= phys_addr
;
1079 return code
| (domain
<< 4);
1082 static int get_phys_addr_v6(CPUState
*env
, uint32_t address
, int access_type
,
1083 int is_user
, uint32_t *phys_ptr
, int *prot
,
1084 target_ulong
*page_size
)
1095 /* Pagetable walk. */
1096 /* Lookup l1 descriptor. */
1097 table
= get_level1_table_address(env
, address
);
1098 desc
= ldl_phys(table
);
1101 /* Section translation fault. */
1105 } else if (type
== 2 && (desc
& (1 << 18))) {
1109 /* Section or page. */
1110 domain
= (desc
>> 4) & 0x1e;
1112 domain
= (env
->cp15
.c3
>> domain
) & 3;
1113 if (domain
== 0 || domain
== 2) {
1115 code
= 9; /* Section domain fault. */
1117 code
= 11; /* Page domain fault. */
1121 if (desc
& (1 << 18)) {
1123 phys_addr
= (desc
& 0xff000000) | (address
& 0x00ffffff);
1124 *page_size
= 0x1000000;
1127 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
1128 *page_size
= 0x100000;
1130 ap
= ((desc
>> 10) & 3) | ((desc
>> 13) & 4);
1131 xn
= desc
& (1 << 4);
1134 /* Lookup l2 entry. */
1135 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
1136 desc
= ldl_phys(table
);
1137 ap
= ((desc
>> 4) & 3) | ((desc
>> 7) & 4);
1139 case 0: /* Page translation fault. */
1142 case 1: /* 64k page. */
1143 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
1144 xn
= desc
& (1 << 15);
1145 *page_size
= 0x10000;
1147 case 2: case 3: /* 4k page. */
1148 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
1150 *page_size
= 0x1000;
1153 /* Never happens, but compiler isn't smart enough to tell. */
1159 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1161 if (xn
&& access_type
== 2)
1164 /* The simplified model uses AP[0] as an access control bit. */
1165 if ((env
->cp15
.c1_sys
& (1 << 29)) && (ap
& 1) == 0) {
1166 /* Access flag fault. */
1167 code
= (code
== 15) ? 6 : 3;
1170 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1172 /* Access permission fault. */
1179 *phys_ptr
= phys_addr
;
1182 return code
| (domain
<< 4);
1185 static int get_phys_addr_mpu(CPUState
*env
, uint32_t address
, int access_type
,
1186 int is_user
, uint32_t *phys_ptr
, int *prot
)
1192 *phys_ptr
= address
;
1193 for (n
= 7; n
>= 0; n
--) {
1194 base
= env
->cp15
.c6_region
[n
];
1195 if ((base
& 1) == 0)
1197 mask
= 1 << ((base
>> 1) & 0x1f);
1198 /* Keep this shift separate from the above to avoid an
1199 (undefined) << 32. */
1200 mask
= (mask
<< 1) - 1;
1201 if (((base
^ address
) & ~mask
) == 0)
1207 if (access_type
== 2) {
1208 mask
= env
->cp15
.c5_insn
;
1210 mask
= env
->cp15
.c5_data
;
1212 mask
= (mask
>> (n
* 4)) & 0xf;
1219 *prot
= PAGE_READ
| PAGE_WRITE
;
1224 *prot
|= PAGE_WRITE
;
1227 *prot
= PAGE_READ
| PAGE_WRITE
;
1238 /* Bad permission. */
1245 static inline int get_phys_addr(CPUState
*env
, uint32_t address
,
1246 int access_type
, int is_user
,
1247 uint32_t *phys_ptr
, int *prot
,
1248 target_ulong
*page_size
)
1250 /* Fast Context Switch Extension. */
1251 if (address
< 0x02000000)
1252 address
+= env
->cp15
.c13_fcse
;
1254 if ((env
->cp15
.c1_sys
& 1) == 0) {
1255 /* MMU/MPU disabled. */
1256 *phys_ptr
= address
;
1257 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1258 *page_size
= TARGET_PAGE_SIZE
;
1260 } else if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1261 *page_size
= TARGET_PAGE_SIZE
;
1262 return get_phys_addr_mpu(env
, address
, access_type
, is_user
, phys_ptr
,
1264 } else if (env
->cp15
.c1_sys
& (1 << 23)) {
1265 return get_phys_addr_v6(env
, address
, access_type
, is_user
, phys_ptr
,
1268 return get_phys_addr_v5(env
, address
, access_type
, is_user
, phys_ptr
,
1273 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
,
1274 int access_type
, int mmu_idx
, int is_softmmu
)
1277 target_ulong page_size
;
1281 is_user
= mmu_idx
== MMU_USER_IDX
;
1282 ret
= get_phys_addr(env
, address
, access_type
, is_user
, &phys_addr
, &prot
,
1285 /* Map a single [sub]page. */
1286 phys_addr
&= ~(uint32_t)0x3ff;
1287 address
&= ~(uint32_t)0x3ff;
1288 tlb_set_page (env
, address
, phys_addr
, prot
, mmu_idx
, page_size
);
1292 if (access_type
== 2) {
1293 env
->cp15
.c5_insn
= ret
;
1294 env
->cp15
.c6_insn
= address
;
1295 env
->exception_index
= EXCP_PREFETCH_ABORT
;
1297 env
->cp15
.c5_data
= ret
;
1298 if (access_type
== 1 && arm_feature(env
, ARM_FEATURE_V6
))
1299 env
->cp15
.c5_data
|= (1 << 11);
1300 env
->cp15
.c6_data
= address
;
1301 env
->exception_index
= EXCP_DATA_ABORT
;
1306 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1309 target_ulong page_size
;
1313 ret
= get_phys_addr(env
, addr
, 0, 0, &phys_addr
, &prot
, &page_size
);
1321 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1323 int cp_num
= (insn
>> 8) & 0xf;
1324 int cp_info
= (insn
>> 5) & 7;
1325 int src
= (insn
>> 16) & 0xf;
1326 int operand
= insn
& 0xf;
1328 if (env
->cp
[cp_num
].cp_write
)
1329 env
->cp
[cp_num
].cp_write(env
->cp
[cp_num
].opaque
,
1330 cp_info
, src
, operand
, val
);
1333 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
1335 int cp_num
= (insn
>> 8) & 0xf;
1336 int cp_info
= (insn
>> 5) & 7;
1337 int dest
= (insn
>> 16) & 0xf;
1338 int operand
= insn
& 0xf;
1340 if (env
->cp
[cp_num
].cp_read
)
1341 return env
->cp
[cp_num
].cp_read(env
->cp
[cp_num
].opaque
,
1342 cp_info
, dest
, operand
);
1346 /* Return basic MPU access permission bits. */
1347 static uint32_t simple_mpu_ap_bits(uint32_t val
)
1354 for (i
= 0; i
< 16; i
+= 2) {
1355 ret
|= (val
>> i
) & mask
;
1361 /* Pad basic MPU access permission bits to extended format. */
1362 static uint32_t extended_mpu_ap_bits(uint32_t val
)
1369 for (i
= 0; i
< 16; i
+= 2) {
1370 ret
|= (val
& mask
) << i
;
1376 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1382 op1
= (insn
>> 21) & 7;
1383 op2
= (insn
>> 5) & 7;
1385 switch ((insn
>> 16) & 0xf) {
1388 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1390 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1392 if (arm_feature(env
, ARM_FEATURE_V7
)
1393 && op1
== 2 && crm
== 0 && op2
== 0) {
1394 env
->cp15
.c0_cssel
= val
& 0xf;
1398 case 1: /* System configuration. */
1399 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1403 if (!arm_feature(env
, ARM_FEATURE_XSCALE
) || crm
== 0)
1404 env
->cp15
.c1_sys
= val
;
1405 /* ??? Lots of these bits are not implemented. */
1406 /* This may enable/disable the MMU, so do a TLB flush. */
1409 case 1: /* Auxiliary control register. */
1410 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1411 env
->cp15
.c1_xscaleauxcr
= val
;
1414 /* Not implemented. */
1417 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1419 if (env
->cp15
.c1_coproc
!= val
) {
1420 env
->cp15
.c1_coproc
= val
;
1421 /* ??? Is this safe when called from within a TB? */
1429 case 2: /* MMU Page table control / MPU cache control. */
1430 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1433 env
->cp15
.c2_data
= val
;
1436 env
->cp15
.c2_insn
= val
;
1444 env
->cp15
.c2_base0
= val
;
1447 env
->cp15
.c2_base1
= val
;
1451 env
->cp15
.c2_control
= val
;
1452 env
->cp15
.c2_mask
= ~(((uint32_t)0xffffffffu
) >> val
);
1453 env
->cp15
.c2_base_mask
= ~((uint32_t)0x3fffu
>> val
);
1460 case 3: /* MMU Domain access control / MPU write buffer control. */
1462 tlb_flush(env
, 1); /* Flush TLB as domain not tracked in TLB */
1464 case 4: /* Reserved. */
1466 case 5: /* MMU Fault status / MPU access permission. */
1467 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1471 if (arm_feature(env
, ARM_FEATURE_MPU
))
1472 val
= extended_mpu_ap_bits(val
);
1473 env
->cp15
.c5_data
= val
;
1476 if (arm_feature(env
, ARM_FEATURE_MPU
))
1477 val
= extended_mpu_ap_bits(val
);
1478 env
->cp15
.c5_insn
= val
;
1481 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1483 env
->cp15
.c5_data
= val
;
1486 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1488 env
->cp15
.c5_insn
= val
;
1494 case 6: /* MMU Fault address / MPU base/size. */
1495 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1498 env
->cp15
.c6_region
[crm
] = val
;
1500 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1504 env
->cp15
.c6_data
= val
;
1506 case 1: /* ??? This is WFAR on armv6 */
1508 env
->cp15
.c6_insn
= val
;
1515 case 7: /* Cache control. */
1516 env
->cp15
.c15_i_max
= 0x000;
1517 env
->cp15
.c15_i_min
= 0xff0;
1521 /* No cache, so nothing to do except VA->PA translations. */
1522 if (arm_feature(env
, ARM_FEATURE_VAPA
)) {
1525 if (arm_feature(env
, ARM_FEATURE_V7
)) {
1526 env
->cp15
.c7_par
= val
& 0xfffff6ff;
1528 env
->cp15
.c7_par
= val
& 0xfffff1ff;
1533 target_ulong page_size
;
1535 int ret
, is_user
= op2
& 2;
1536 int access_type
= op2
& 1;
1539 /* Other states are only available with TrustZone */
1542 ret
= get_phys_addr(env
, val
, access_type
, is_user
,
1543 &phys_addr
, &prot
, &page_size
);
1545 /* We do not set any attribute bits in the PAR */
1546 if (page_size
== (1 << 24)
1547 && arm_feature(env
, ARM_FEATURE_V7
)) {
1548 env
->cp15
.c7_par
= (phys_addr
& 0xff000000) | 1 << 1;
1550 env
->cp15
.c7_par
= phys_addr
& 0xfffff000;
1553 env
->cp15
.c7_par
= ((ret
& (10 << 1)) >> 5) |
1554 ((ret
& (12 << 1)) >> 6) |
1555 ((ret
& 0xf) << 1) | 1;
1562 case 8: /* MMU TLB control. */
1564 case 0: /* Invalidate all. */
1567 case 1: /* Invalidate single TLB entry. */
1568 tlb_flush_page(env
, val
& TARGET_PAGE_MASK
);
1570 case 2: /* Invalidate on ASID. */
1571 tlb_flush(env
, val
== 0);
1573 case 3: /* Invalidate single entry on MVA. */
1574 /* ??? This is like case 1, but ignores ASID. */
1582 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1584 if (arm_feature(env
, ARM_FEATURE_STRONGARM
))
1585 break; /* Ignore ReadBuffer access */
1587 case 0: /* Cache lockdown. */
1589 case 0: /* L1 cache. */
1592 env
->cp15
.c9_data
= val
;
1595 env
->cp15
.c9_insn
= val
;
1601 case 1: /* L2 cache. */
1602 /* Ignore writes to L2 lockdown/auxiliary registers. */
1608 case 1: /* TCM memory region registers. */
1609 /* Not implemented. */
1611 case 12: /* Performance monitor control */
1612 /* Performance monitors are implementation defined in v7,
1613 * but with an ARM recommended set of registers, which we
1614 * follow (although we don't actually implement any counters)
1616 if (!arm_feature(env
, ARM_FEATURE_V7
)) {
1620 case 0: /* performance monitor control register */
1621 /* only the DP, X, D and E bits are writable */
1622 env
->cp15
.c9_pmcr
&= ~0x39;
1623 env
->cp15
.c9_pmcr
|= (val
& 0x39);
1625 case 1: /* Count enable set register */
1627 env
->cp15
.c9_pmcnten
|= val
;
1629 case 2: /* Count enable clear */
1631 env
->cp15
.c9_pmcnten
&= ~val
;
1633 case 3: /* Overflow flag status */
1634 env
->cp15
.c9_pmovsr
&= ~val
;
1636 case 4: /* Software increment */
1637 /* RAZ/WI since we don't implement the software-count event */
1639 case 5: /* Event counter selection register */
1640 /* Since we don't implement any events, writing to this register
1641 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1648 case 13: /* Performance counters */
1649 if (!arm_feature(env
, ARM_FEATURE_V7
)) {
1653 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1655 case 1: /* Event type select */
1656 env
->cp15
.c9_pmxevtyper
= val
& 0xff;
1658 case 2: /* Event count register */
1659 /* Unimplemented (we have no events), RAZ/WI */
1665 case 14: /* Performance monitor control */
1666 if (!arm_feature(env
, ARM_FEATURE_V7
)) {
1670 case 0: /* user enable */
1671 env
->cp15
.c9_pmuserenr
= val
& 1;
1672 /* changes access rights for cp registers, so flush tbs */
1675 case 1: /* interrupt enable set */
1676 /* We have no event counters so only the C bit can be changed */
1678 env
->cp15
.c9_pminten
|= val
;
1680 case 2: /* interrupt enable clear */
1682 env
->cp15
.c9_pminten
&= ~val
;
1690 case 10: /* MMU TLB lockdown. */
1691 /* ??? TLB lockdown not implemented. */
1693 case 12: /* Reserved. */
1695 case 13: /* Process ID. */
1698 /* Unlike real hardware the qemu TLB uses virtual addresses,
1699 not modified virtual addresses, so this causes a TLB flush.
1701 if (env
->cp15
.c13_fcse
!= val
)
1703 env
->cp15
.c13_fcse
= val
;
1706 /* This changes the ASID, so do a TLB flush. */
1707 if (env
->cp15
.c13_context
!= val
1708 && !arm_feature(env
, ARM_FEATURE_MPU
))
1710 env
->cp15
.c13_context
= val
;
1716 case 14: /* Reserved. */
1718 case 15: /* Implementation specific. */
1719 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1720 if (op2
== 0 && crm
== 1) {
1721 if (env
->cp15
.c15_cpar
!= (val
& 0x3fff)) {
1722 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1724 env
->cp15
.c15_cpar
= val
& 0x3fff;
1730 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1734 case 1: /* Set TI925T configuration. */
1735 env
->cp15
.c15_ticonfig
= val
& 0xe7;
1736 env
->cp15
.c0_cpuid
= (val
& (1 << 5)) ? /* OS_TYPE bit */
1737 ARM_CPUID_TI915T
: ARM_CPUID_TI925T
;
1739 case 2: /* Set I_max. */
1740 env
->cp15
.c15_i_max
= val
;
1742 case 3: /* Set I_min. */
1743 env
->cp15
.c15_i_min
= val
;
1745 case 4: /* Set thread-ID. */
1746 env
->cp15
.c15_threadid
= val
& 0xffff;
1748 case 8: /* Wait-for-interrupt (deprecated). */
1749 cpu_interrupt(env
, CPU_INTERRUPT_HALT
);
1759 /* ??? For debugging only. Should raise illegal instruction exception. */
1760 cpu_abort(env
, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1761 (insn
>> 16) & 0xf, crm
, op1
, op2
);
1764 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
1770 op1
= (insn
>> 21) & 7;
1771 op2
= (insn
>> 5) & 7;
1773 switch ((insn
>> 16) & 0xf) {
1774 case 0: /* ID codes. */
1780 case 0: /* Device ID. */
1781 return env
->cp15
.c0_cpuid
;
1782 case 1: /* Cache Type. */
1783 return env
->cp15
.c0_cachetype
;
1784 case 2: /* TCM status. */
1786 case 3: /* TLB type register. */
1787 return 0; /* No lockable TLB entries. */
1789 /* The MPIDR was standardised in v7; prior to
1790 * this it was implemented only in the 11MPCore.
1791 * For all other pre-v7 cores it does not exist.
1793 if (arm_feature(env
, ARM_FEATURE_V7
) ||
1794 ARM_CPUID(env
) == ARM_CPUID_ARM11MPCORE
) {
1795 int mpidr
= env
->cpu_index
;
1796 /* We don't support setting cluster ID ([8..11])
1797 * so these bits always RAZ.
1799 if (arm_feature(env
, ARM_FEATURE_V7MP
)) {
1801 /* Cores which are uniprocessor (non-coherent)
1802 * but still implement the MP extensions set
1803 * bit 30. (For instance, A9UP.) However we do
1804 * not currently model any of those cores.
1809 /* otherwise fall through to the unimplemented-reg case */
1814 if (!arm_feature(env
, ARM_FEATURE_V6
))
1816 return env
->cp15
.c0_c1
[op2
];
1818 if (!arm_feature(env
, ARM_FEATURE_V6
))
1820 return env
->cp15
.c0_c2
[op2
];
1821 case 3: case 4: case 5: case 6: case 7:
1827 /* These registers aren't documented on arm11 cores. However
1828 Linux looks at them anyway. */
1829 if (!arm_feature(env
, ARM_FEATURE_V6
))
1833 if (!arm_feature(env
, ARM_FEATURE_V7
))
1838 return env
->cp15
.c0_ccsid
[env
->cp15
.c0_cssel
];
1840 return env
->cp15
.c0_clid
;
1846 if (op2
!= 0 || crm
!= 0)
1848 return env
->cp15
.c0_cssel
;
1852 case 1: /* System configuration. */
1853 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1856 case 0: /* Control register. */
1857 return env
->cp15
.c1_sys
;
1858 case 1: /* Auxiliary control register. */
1859 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1860 return env
->cp15
.c1_xscaleauxcr
;
1861 if (!arm_feature(env
, ARM_FEATURE_AUXCR
))
1863 switch (ARM_CPUID(env
)) {
1864 case ARM_CPUID_ARM1026
:
1866 case ARM_CPUID_ARM1136
:
1867 case ARM_CPUID_ARM1136_R2
:
1869 case ARM_CPUID_ARM11MPCORE
:
1871 case ARM_CPUID_CORTEXA8
:
1873 case ARM_CPUID_CORTEXA9
:
1878 case 2: /* Coprocessor access register. */
1879 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1881 return env
->cp15
.c1_coproc
;
1885 case 2: /* MMU Page table control / MPU cache control. */
1886 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1889 return env
->cp15
.c2_data
;
1892 return env
->cp15
.c2_insn
;
1900 return env
->cp15
.c2_base0
;
1902 return env
->cp15
.c2_base1
;
1904 return env
->cp15
.c2_control
;
1909 case 3: /* MMU Domain access control / MPU write buffer control. */
1910 return env
->cp15
.c3
;
1911 case 4: /* Reserved. */
1913 case 5: /* MMU Fault status / MPU access permission. */
1914 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1918 if (arm_feature(env
, ARM_FEATURE_MPU
))
1919 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1920 return env
->cp15
.c5_data
;
1922 if (arm_feature(env
, ARM_FEATURE_MPU
))
1923 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1924 return env
->cp15
.c5_insn
;
1926 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1928 return env
->cp15
.c5_data
;
1930 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1932 return env
->cp15
.c5_insn
;
1936 case 6: /* MMU Fault address. */
1937 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1940 return env
->cp15
.c6_region
[crm
];
1942 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1946 return env
->cp15
.c6_data
;
1948 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1949 /* Watchpoint Fault Adrress. */
1950 return 0; /* Not implemented. */
1952 /* Instruction Fault Adrress. */
1953 /* Arm9 doesn't have an IFAR, but implementing it anyway
1954 shouldn't do any harm. */
1955 return env
->cp15
.c6_insn
;
1958 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1959 /* Instruction Fault Adrress. */
1960 return env
->cp15
.c6_insn
;
1968 case 7: /* Cache control. */
1969 if (crm
== 4 && op1
== 0 && op2
== 0) {
1970 return env
->cp15
.c7_par
;
1972 /* FIXME: Should only clear Z flag if destination is r15. */
1975 case 8: /* MMU TLB control. */
1979 case 0: /* Cache lockdown */
1981 case 0: /* L1 cache. */
1982 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1987 return env
->cp15
.c9_data
;
1989 return env
->cp15
.c9_insn
;
1993 case 1: /* L2 cache */
1997 /* L2 Lockdown and Auxiliary control. */
2003 case 12: /* Performance monitor control */
2004 if (!arm_feature(env
, ARM_FEATURE_V7
)) {
2008 case 0: /* performance monitor control register */
2009 return env
->cp15
.c9_pmcr
;
2010 case 1: /* count enable set */
2011 case 2: /* count enable clear */
2012 return env
->cp15
.c9_pmcnten
;
2013 case 3: /* overflow flag status */
2014 return env
->cp15
.c9_pmovsr
;
2015 case 4: /* software increment */
2016 case 5: /* event counter selection register */
2017 return 0; /* Unimplemented, RAZ/WI */
2021 case 13: /* Performance counters */
2022 if (!arm_feature(env
, ARM_FEATURE_V7
)) {
2026 case 1: /* Event type select */
2027 return env
->cp15
.c9_pmxevtyper
;
2028 case 0: /* Cycle count register */
2029 case 2: /* Event count register */
2030 /* Unimplemented, so RAZ/WI */
2035 case 14: /* Performance monitor control */
2036 if (!arm_feature(env
, ARM_FEATURE_V7
)) {
2040 case 0: /* user enable */
2041 return env
->cp15
.c9_pmuserenr
;
2042 case 1: /* interrupt enable set */
2043 case 2: /* interrupt enable clear */
2044 return env
->cp15
.c9_pminten
;
2052 case 10: /* MMU TLB lockdown. */
2053 /* ??? TLB lockdown not implemented. */
2055 case 11: /* TCM DMA control. */
2056 case 12: /* Reserved. */
2058 case 13: /* Process ID. */
2061 return env
->cp15
.c13_fcse
;
2063 return env
->cp15
.c13_context
;
2067 case 14: /* Reserved. */
2069 case 15: /* Implementation specific. */
2070 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
2071 if (op2
== 0 && crm
== 1)
2072 return env
->cp15
.c15_cpar
;
2076 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
2080 case 1: /* Read TI925T configuration. */
2081 return env
->cp15
.c15_ticonfig
;
2082 case 2: /* Read I_max. */
2083 return env
->cp15
.c15_i_max
;
2084 case 3: /* Read I_min. */
2085 return env
->cp15
.c15_i_min
;
2086 case 4: /* Read thread-ID. */
2087 return env
->cp15
.c15_threadid
;
2088 case 8: /* TI925T_status */
2091 /* TODO: Peripheral port remap register:
2092 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2093 * controller base address at $rn & ~0xfff and map size of
2094 * 0x200 << ($rn & 0xfff), when MMU is off. */
2100 /* ??? For debugging only. Should raise illegal instruction exception. */
2101 cpu_abort(env
, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2102 (insn
>> 16) & 0xf, crm
, op1
, op2
);
2106 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
2108 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
2109 env
->regs
[13] = val
;
2111 env
->banked_r13
[bank_number(mode
)] = val
;
2115 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
2117 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
2118 return env
->regs
[13];
2120 return env
->banked_r13
[bank_number(mode
)];
2124 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
2128 return xpsr_read(env
) & 0xf8000000;
2130 return xpsr_read(env
) & 0xf80001ff;
2132 return xpsr_read(env
) & 0xff00fc00;
2134 return xpsr_read(env
) & 0xff00fdff;
2136 return xpsr_read(env
) & 0x000001ff;
2138 return xpsr_read(env
) & 0x0700fc00;
2140 return xpsr_read(env
) & 0x0700edff;
2142 return env
->v7m
.current_sp
? env
->v7m
.other_sp
: env
->regs
[13];
2144 return env
->v7m
.current_sp
? env
->regs
[13] : env
->v7m
.other_sp
;
2145 case 16: /* PRIMASK */
2146 return (env
->uncached_cpsr
& CPSR_I
) != 0;
2147 case 17: /* BASEPRI */
2148 case 18: /* BASEPRI_MAX */
2149 return env
->v7m
.basepri
;
2150 case 19: /* FAULTMASK */
2151 return (env
->uncached_cpsr
& CPSR_F
) != 0;
2152 case 20: /* CONTROL */
2153 return env
->v7m
.control
;
2155 /* ??? For debugging only. */
2156 cpu_abort(env
, "Unimplemented system register read (%d)\n", reg
);
2161 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
2165 xpsr_write(env
, val
, 0xf8000000);
2168 xpsr_write(env
, val
, 0xf8000000);
2171 xpsr_write(env
, val
, 0xfe00fc00);
2174 xpsr_write(env
, val
, 0xfe00fc00);
2177 /* IPSR bits are readonly. */
2180 xpsr_write(env
, val
, 0x0600fc00);
2183 xpsr_write(env
, val
, 0x0600fc00);
2186 if (env
->v7m
.current_sp
)
2187 env
->v7m
.other_sp
= val
;
2189 env
->regs
[13] = val
;
2192 if (env
->v7m
.current_sp
)
2193 env
->regs
[13] = val
;
2195 env
->v7m
.other_sp
= val
;
2197 case 16: /* PRIMASK */
2199 env
->uncached_cpsr
|= CPSR_I
;
2201 env
->uncached_cpsr
&= ~CPSR_I
;
2203 case 17: /* BASEPRI */
2204 env
->v7m
.basepri
= val
& 0xff;
2206 case 18: /* BASEPRI_MAX */
2208 if (val
!= 0 && (val
< env
->v7m
.basepri
|| env
->v7m
.basepri
== 0))
2209 env
->v7m
.basepri
= val
;
2211 case 19: /* FAULTMASK */
2213 env
->uncached_cpsr
|= CPSR_F
;
2215 env
->uncached_cpsr
&= ~CPSR_F
;
2217 case 20: /* CONTROL */
2218 env
->v7m
.control
= val
& 3;
2219 switch_v7m_sp(env
, (val
& 2) != 0);
2222 /* ??? For debugging only. */
2223 cpu_abort(env
, "Unimplemented system register write (%d)\n", reg
);
2228 void cpu_arm_set_cp_io(CPUARMState
*env
, int cpnum
,
2229 ARMReadCPFunc
*cp_read
, ARMWriteCPFunc
*cp_write
,
2232 if (cpnum
< 0 || cpnum
> 14) {
2233 cpu_abort(env
, "Bad coprocessor number: %i\n", cpnum
);
2237 env
->cp
[cpnum
].cp_read
= cp_read
;
2238 env
->cp
[cpnum
].cp_write
= cp_write
;
2239 env
->cp
[cpnum
].opaque
= opaque
;
2244 /* Note that signed overflow is undefined in C. The following routines are
2245 careful to use unsigned types where modulo arithmetic is required.
2246 Failure to do so _will_ break on newer gcc. */
2248 /* Signed saturating arithmetic. */
2250 /* Perform 16-bit signed saturating addition. */
2251 static inline uint16_t add16_sat(uint16_t a
, uint16_t b
)
2256 if (((res
^ a
) & 0x8000) && !((a
^ b
) & 0x8000)) {
2265 /* Perform 8-bit signed saturating addition. */
2266 static inline uint8_t add8_sat(uint8_t a
, uint8_t b
)
2271 if (((res
^ a
) & 0x80) && !((a
^ b
) & 0x80)) {
2280 /* Perform 16-bit signed saturating subtraction. */
2281 static inline uint16_t sub16_sat(uint16_t a
, uint16_t b
)
2286 if (((res
^ a
) & 0x8000) && ((a
^ b
) & 0x8000)) {
2295 /* Perform 8-bit signed saturating subtraction. */
2296 static inline uint8_t sub8_sat(uint8_t a
, uint8_t b
)
2301 if (((res
^ a
) & 0x80) && ((a
^ b
) & 0x80)) {
2310 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2311 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2312 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2313 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2316 #include "op_addsub.h"
2318 /* Unsigned saturating arithmetic. */
2319 static inline uint16_t add16_usat(uint16_t a
, uint16_t b
)
2328 static inline uint16_t sub16_usat(uint16_t a
, uint16_t b
)
2336 static inline uint8_t add8_usat(uint8_t a
, uint8_t b
)
2345 static inline uint8_t sub8_usat(uint8_t a
, uint8_t b
)
2353 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2354 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2355 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2356 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2359 #include "op_addsub.h"
2361 /* Signed modulo arithmetic. */
2362 #define SARITH16(a, b, n, op) do { \
2364 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2365 RESULT(sum, n, 16); \
2367 ge |= 3 << (n * 2); \
2370 #define SARITH8(a, b, n, op) do { \
2372 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2373 RESULT(sum, n, 8); \
2379 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2380 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2381 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2382 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2386 #include "op_addsub.h"
2388 /* Unsigned modulo arithmetic. */
2389 #define ADD16(a, b, n) do { \
2391 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2392 RESULT(sum, n, 16); \
2393 if ((sum >> 16) == 1) \
2394 ge |= 3 << (n * 2); \
2397 #define ADD8(a, b, n) do { \
2399 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2400 RESULT(sum, n, 8); \
2401 if ((sum >> 8) == 1) \
2405 #define SUB16(a, b, n) do { \
2407 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2408 RESULT(sum, n, 16); \
2409 if ((sum >> 16) == 0) \
2410 ge |= 3 << (n * 2); \
2413 #define SUB8(a, b, n) do { \
2415 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2416 RESULT(sum, n, 8); \
2417 if ((sum >> 8) == 0) \
2424 #include "op_addsub.h"
2426 /* Halved signed arithmetic. */
2427 #define ADD16(a, b, n) \
2428 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2429 #define SUB16(a, b, n) \
2430 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2431 #define ADD8(a, b, n) \
2432 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2433 #define SUB8(a, b, n) \
2434 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2437 #include "op_addsub.h"
2439 /* Halved unsigned arithmetic. */
2440 #define ADD16(a, b, n) \
2441 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2442 #define SUB16(a, b, n) \
2443 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2444 #define ADD8(a, b, n) \
2445 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2446 #define SUB8(a, b, n) \
2447 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2450 #include "op_addsub.h"
2452 static inline uint8_t do_usad(uint8_t a
, uint8_t b
)
2460 /* Unsigned sum of absolute byte differences. */
2461 uint32_t HELPER(usad8
)(uint32_t a
, uint32_t b
)
2464 sum
= do_usad(a
, b
);
2465 sum
+= do_usad(a
>> 8, b
>> 8);
2466 sum
+= do_usad(a
>> 16, b
>>16);
2467 sum
+= do_usad(a
>> 24, b
>> 24);
2471 /* For ARMv6 SEL instruction. */
2472 uint32_t HELPER(sel_flags
)(uint32_t flags
, uint32_t a
, uint32_t b
)
2485 return (a
& mask
) | (b
& ~mask
);
2488 uint32_t HELPER(logicq_cc
)(uint64_t val
)
2490 return (val
>> 32) | (val
!= 0);
2493 /* VFP support. We follow the convention used for VFP instrunctions:
2494 Single precition routines have a "s" suffix, double precision a
2497 /* Convert host exception flags to vfp form. */
2498 static inline int vfp_exceptbits_from_host(int host_bits
)
2500 int target_bits
= 0;
2502 if (host_bits
& float_flag_invalid
)
2504 if (host_bits
& float_flag_divbyzero
)
2506 if (host_bits
& float_flag_overflow
)
2508 if (host_bits
& (float_flag_underflow
| float_flag_output_denormal
))
2510 if (host_bits
& float_flag_inexact
)
2511 target_bits
|= 0x10;
2512 if (host_bits
& float_flag_input_denormal
)
2513 target_bits
|= 0x80;
2517 uint32_t HELPER(vfp_get_fpscr
)(CPUState
*env
)
2522 fpscr
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & 0xffc8ffff)
2523 | (env
->vfp
.vec_len
<< 16)
2524 | (env
->vfp
.vec_stride
<< 20);
2525 i
= get_float_exception_flags(&env
->vfp
.fp_status
);
2526 i
|= get_float_exception_flags(&env
->vfp
.standard_fp_status
);
2527 fpscr
|= vfp_exceptbits_from_host(i
);
2531 uint32_t vfp_get_fpscr(CPUState
*env
)
2533 return HELPER(vfp_get_fpscr
)(env
);
2536 /* Convert vfp exception flags to target form. */
2537 static inline int vfp_exceptbits_to_host(int target_bits
)
2541 if (target_bits
& 1)
2542 host_bits
|= float_flag_invalid
;
2543 if (target_bits
& 2)
2544 host_bits
|= float_flag_divbyzero
;
2545 if (target_bits
& 4)
2546 host_bits
|= float_flag_overflow
;
2547 if (target_bits
& 8)
2548 host_bits
|= float_flag_underflow
;
2549 if (target_bits
& 0x10)
2550 host_bits
|= float_flag_inexact
;
2551 if (target_bits
& 0x80)
2552 host_bits
|= float_flag_input_denormal
;
2556 void HELPER(vfp_set_fpscr
)(CPUState
*env
, uint32_t val
)
2561 changed
= env
->vfp
.xregs
[ARM_VFP_FPSCR
];
2562 env
->vfp
.xregs
[ARM_VFP_FPSCR
] = (val
& 0xffc8ffff);
2563 env
->vfp
.vec_len
= (val
>> 16) & 7;
2564 env
->vfp
.vec_stride
= (val
>> 20) & 3;
2567 if (changed
& (3 << 22)) {
2568 i
= (val
>> 22) & 3;
2571 i
= float_round_nearest_even
;
2577 i
= float_round_down
;
2580 i
= float_round_to_zero
;
2583 set_float_rounding_mode(i
, &env
->vfp
.fp_status
);
2585 if (changed
& (1 << 24)) {
2586 set_flush_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
2587 set_flush_inputs_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
2589 if (changed
& (1 << 25))
2590 set_default_nan_mode((val
& (1 << 25)) != 0, &env
->vfp
.fp_status
);
2592 i
= vfp_exceptbits_to_host(val
);
2593 set_float_exception_flags(i
, &env
->vfp
.fp_status
);
2594 set_float_exception_flags(0, &env
->vfp
.standard_fp_status
);
2597 void vfp_set_fpscr(CPUState
*env
, uint32_t val
)
2599 HELPER(vfp_set_fpscr
)(env
, val
);
2602 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2604 #define VFP_BINOP(name) \
2605 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2607 float_status *fpst = fpstp; \
2608 return float32_ ## name(a, b, fpst); \
2610 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2612 float_status *fpst = fpstp; \
2613 return float64_ ## name(a, b, fpst); \
2621 float32
VFP_HELPER(neg
, s
)(float32 a
)
2623 return float32_chs(a
);
2626 float64
VFP_HELPER(neg
, d
)(float64 a
)
2628 return float64_chs(a
);
2631 float32
VFP_HELPER(abs
, s
)(float32 a
)
2633 return float32_abs(a
);
2636 float64
VFP_HELPER(abs
, d
)(float64 a
)
2638 return float64_abs(a
);
2641 float32
VFP_HELPER(sqrt
, s
)(float32 a
, CPUState
*env
)
2643 return float32_sqrt(a
, &env
->vfp
.fp_status
);
2646 float64
VFP_HELPER(sqrt
, d
)(float64 a
, CPUState
*env
)
2648 return float64_sqrt(a
, &env
->vfp
.fp_status
);
2651 /* XXX: check quiet/signaling case */
2652 #define DO_VFP_cmp(p, type) \
2653 void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2656 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2657 case 0: flags = 0x6; break; \
2658 case -1: flags = 0x8; break; \
2659 case 1: flags = 0x2; break; \
2660 default: case 2: flags = 0x3; break; \
2662 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2663 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2665 void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2668 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2669 case 0: flags = 0x6; break; \
2670 case -1: flags = 0x8; break; \
2671 case 1: flags = 0x2; break; \
2672 default: case 2: flags = 0x3; break; \
2674 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2675 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2677 DO_VFP_cmp(s
, float32
)
2678 DO_VFP_cmp(d
, float64
)
2681 /* Integer to float and float to integer conversions */
2683 #define CONV_ITOF(name, fsz, sign) \
2684 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2686 float_status *fpst = fpstp; \
2687 return sign##int32_to_##float##fsz(x, fpst); \
2690 #define CONV_FTOI(name, fsz, sign, round) \
2691 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2693 float_status *fpst = fpstp; \
2694 if (float##fsz##_is_any_nan(x)) { \
2695 float_raise(float_flag_invalid, fpst); \
2698 return float##fsz##_to_##sign##int32##round(x, fpst); \
2701 #define FLOAT_CONVS(name, p, fsz, sign) \
2702 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2703 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2704 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2706 FLOAT_CONVS(si
, s
, 32, )
2707 FLOAT_CONVS(si
, d
, 64, )
2708 FLOAT_CONVS(ui
, s
, 32, u
)
2709 FLOAT_CONVS(ui
, d
, 64, u
)
2715 /* floating point conversion */
2716 float64
VFP_HELPER(fcvtd
, s
)(float32 x
, CPUState
*env
)
2718 float64 r
= float32_to_float64(x
, &env
->vfp
.fp_status
);
2719 /* ARM requires that S<->D conversion of any kind of NaN generates
2720 * a quiet NaN by forcing the most significant frac bit to 1.
2722 return float64_maybe_silence_nan(r
);
2725 float32
VFP_HELPER(fcvts
, d
)(float64 x
, CPUState
*env
)
2727 float32 r
= float64_to_float32(x
, &env
->vfp
.fp_status
);
2728 /* ARM requires that S<->D conversion of any kind of NaN generates
2729 * a quiet NaN by forcing the most significant frac bit to 1.
2731 return float32_maybe_silence_nan(r
);
2734 /* VFP3 fixed point conversion. */
2735 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2736 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2739 float_status *fpst = fpstp; \
2741 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2742 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2744 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2747 float_status *fpst = fpstp; \
2749 if (float##fsz##_is_any_nan(x)) { \
2750 float_raise(float_flag_invalid, fpst); \
2753 tmp = float##fsz##_scalbn(x, shift, fpst); \
2754 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2757 VFP_CONV_FIX(sh
, d
, 64, int16
, )
2758 VFP_CONV_FIX(sl
, d
, 64, int32
, )
2759 VFP_CONV_FIX(uh
, d
, 64, uint16
, u
)
2760 VFP_CONV_FIX(ul
, d
, 64, uint32
, u
)
2761 VFP_CONV_FIX(sh
, s
, 32, int16
, )
2762 VFP_CONV_FIX(sl
, s
, 32, int32
, )
2763 VFP_CONV_FIX(uh
, s
, 32, uint16
, u
)
2764 VFP_CONV_FIX(ul
, s
, 32, uint32
, u
)
2767 /* Half precision conversions. */
2768 static float32
do_fcvt_f16_to_f32(uint32_t a
, CPUState
*env
, float_status
*s
)
2770 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2771 float32 r
= float16_to_float32(make_float16(a
), ieee
, s
);
2773 return float32_maybe_silence_nan(r
);
2778 static uint32_t do_fcvt_f32_to_f16(float32 a
, CPUState
*env
, float_status
*s
)
2780 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2781 float16 r
= float32_to_float16(a
, ieee
, s
);
2783 r
= float16_maybe_silence_nan(r
);
2785 return float16_val(r
);
2788 float32
HELPER(neon_fcvt_f16_to_f32
)(uint32_t a
, CPUState
*env
)
2790 return do_fcvt_f16_to_f32(a
, env
, &env
->vfp
.standard_fp_status
);
2793 uint32_t HELPER(neon_fcvt_f32_to_f16
)(float32 a
, CPUState
*env
)
2795 return do_fcvt_f32_to_f16(a
, env
, &env
->vfp
.standard_fp_status
);
2798 float32
HELPER(vfp_fcvt_f16_to_f32
)(uint32_t a
, CPUState
*env
)
2800 return do_fcvt_f16_to_f32(a
, env
, &env
->vfp
.fp_status
);
2803 uint32_t HELPER(vfp_fcvt_f32_to_f16
)(float32 a
, CPUState
*env
)
2805 return do_fcvt_f32_to_f16(a
, env
, &env
->vfp
.fp_status
);
2808 #define float32_two make_float32(0x40000000)
2809 #define float32_three make_float32(0x40400000)
2810 #define float32_one_point_five make_float32(0x3fc00000)
2812 float32
HELPER(recps_f32
)(float32 a
, float32 b
, CPUState
*env
)
2814 float_status
*s
= &env
->vfp
.standard_fp_status
;
2815 if ((float32_is_infinity(a
) && float32_is_zero_or_denormal(b
)) ||
2816 (float32_is_infinity(b
) && float32_is_zero_or_denormal(a
))) {
2817 if (!(float32_is_zero(a
) || float32_is_zero(b
))) {
2818 float_raise(float_flag_input_denormal
, s
);
2822 return float32_sub(float32_two
, float32_mul(a
, b
, s
), s
);
2825 float32
HELPER(rsqrts_f32
)(float32 a
, float32 b
, CPUState
*env
)
2827 float_status
*s
= &env
->vfp
.standard_fp_status
;
2829 if ((float32_is_infinity(a
) && float32_is_zero_or_denormal(b
)) ||
2830 (float32_is_infinity(b
) && float32_is_zero_or_denormal(a
))) {
2831 if (!(float32_is_zero(a
) || float32_is_zero(b
))) {
2832 float_raise(float_flag_input_denormal
, s
);
2834 return float32_one_point_five
;
2836 product
= float32_mul(a
, b
, s
);
2837 return float32_div(float32_sub(float32_three
, product
, s
), float32_two
, s
);
2842 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2843 * int->float conversions at run-time. */
2844 #define float64_256 make_float64(0x4070000000000000LL)
2845 #define float64_512 make_float64(0x4080000000000000LL)
2847 /* The algorithm that must be used to calculate the estimate
2848 * is specified by the ARM ARM.
2850 static float64
recip_estimate(float64 a
, CPUState
*env
)
2852 /* These calculations mustn't set any fp exception flags,
2853 * so we use a local copy of the fp_status.
2855 float_status dummy_status
= env
->vfp
.standard_fp_status
;
2856 float_status
*s
= &dummy_status
;
2857 /* q = (int)(a * 512.0) */
2858 float64 q
= float64_mul(float64_512
, a
, s
);
2859 int64_t q_int
= float64_to_int64_round_to_zero(q
, s
);
2861 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2862 q
= int64_to_float64(q_int
, s
);
2863 q
= float64_add(q
, float64_half
, s
);
2864 q
= float64_div(q
, float64_512
, s
);
2865 q
= float64_div(float64_one
, q
, s
);
2867 /* s = (int)(256.0 * r + 0.5) */
2868 q
= float64_mul(q
, float64_256
, s
);
2869 q
= float64_add(q
, float64_half
, s
);
2870 q_int
= float64_to_int64_round_to_zero(q
, s
);
2872 /* return (double)s / 256.0 */
2873 return float64_div(int64_to_float64(q_int
, s
), float64_256
, s
);
2876 float32
HELPER(recpe_f32
)(float32 a
, CPUState
*env
)
2878 float_status
*s
= &env
->vfp
.standard_fp_status
;
2880 uint32_t val32
= float32_val(a
);
2883 int a_exp
= (val32
& 0x7f800000) >> 23;
2884 int sign
= val32
& 0x80000000;
2886 if (float32_is_any_nan(a
)) {
2887 if (float32_is_signaling_nan(a
)) {
2888 float_raise(float_flag_invalid
, s
);
2890 return float32_default_nan
;
2891 } else if (float32_is_infinity(a
)) {
2892 return float32_set_sign(float32_zero
, float32_is_neg(a
));
2893 } else if (float32_is_zero_or_denormal(a
)) {
2894 if (!float32_is_zero(a
)) {
2895 float_raise(float_flag_input_denormal
, s
);
2897 float_raise(float_flag_divbyzero
, s
);
2898 return float32_set_sign(float32_infinity
, float32_is_neg(a
));
2899 } else if (a_exp
>= 253) {
2900 float_raise(float_flag_underflow
, s
);
2901 return float32_set_sign(float32_zero
, float32_is_neg(a
));
2904 f64
= make_float64((0x3feULL
<< 52)
2905 | ((int64_t)(val32
& 0x7fffff) << 29));
2907 result_exp
= 253 - a_exp
;
2909 f64
= recip_estimate(f64
, env
);
2912 | ((result_exp
& 0xff) << 23)
2913 | ((float64_val(f64
) >> 29) & 0x7fffff);
2914 return make_float32(val32
);
2917 /* The algorithm that must be used to calculate the estimate
2918 * is specified by the ARM ARM.
2920 static float64
recip_sqrt_estimate(float64 a
, CPUState
*env
)
2922 /* These calculations mustn't set any fp exception flags,
2923 * so we use a local copy of the fp_status.
2925 float_status dummy_status
= env
->vfp
.standard_fp_status
;
2926 float_status
*s
= &dummy_status
;
2930 if (float64_lt(a
, float64_half
, s
)) {
2931 /* range 0.25 <= a < 0.5 */
2933 /* a in units of 1/512 rounded down */
2934 /* q0 = (int)(a * 512.0); */
2935 q
= float64_mul(float64_512
, a
, s
);
2936 q_int
= float64_to_int64_round_to_zero(q
, s
);
2938 /* reciprocal root r */
2939 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2940 q
= int64_to_float64(q_int
, s
);
2941 q
= float64_add(q
, float64_half
, s
);
2942 q
= float64_div(q
, float64_512
, s
);
2943 q
= float64_sqrt(q
, s
);
2944 q
= float64_div(float64_one
, q
, s
);
2946 /* range 0.5 <= a < 1.0 */
2948 /* a in units of 1/256 rounded down */
2949 /* q1 = (int)(a * 256.0); */
2950 q
= float64_mul(float64_256
, a
, s
);
2951 int64_t q_int
= float64_to_int64_round_to_zero(q
, s
);
2953 /* reciprocal root r */
2954 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2955 q
= int64_to_float64(q_int
, s
);
2956 q
= float64_add(q
, float64_half
, s
);
2957 q
= float64_div(q
, float64_256
, s
);
2958 q
= float64_sqrt(q
, s
);
2959 q
= float64_div(float64_one
, q
, s
);
2961 /* r in units of 1/256 rounded to nearest */
2962 /* s = (int)(256.0 * r + 0.5); */
2964 q
= float64_mul(q
, float64_256
,s
);
2965 q
= float64_add(q
, float64_half
, s
);
2966 q_int
= float64_to_int64_round_to_zero(q
, s
);
2968 /* return (double)s / 256.0;*/
2969 return float64_div(int64_to_float64(q_int
, s
), float64_256
, s
);
2972 float32
HELPER(rsqrte_f32
)(float32 a
, CPUState
*env
)
2974 float_status
*s
= &env
->vfp
.standard_fp_status
;
2980 val
= float32_val(a
);
2982 if (float32_is_any_nan(a
)) {
2983 if (float32_is_signaling_nan(a
)) {
2984 float_raise(float_flag_invalid
, s
);
2986 return float32_default_nan
;
2987 } else if (float32_is_zero_or_denormal(a
)) {
2988 if (!float32_is_zero(a
)) {
2989 float_raise(float_flag_input_denormal
, s
);
2991 float_raise(float_flag_divbyzero
, s
);
2992 return float32_set_sign(float32_infinity
, float32_is_neg(a
));
2993 } else if (float32_is_neg(a
)) {
2994 float_raise(float_flag_invalid
, s
);
2995 return float32_default_nan
;
2996 } else if (float32_is_infinity(a
)) {
2997 return float32_zero
;
3000 /* Normalize to a double-precision value between 0.25 and 1.0,
3001 * preserving the parity of the exponent. */
3002 if ((val
& 0x800000) == 0) {
3003 f64
= make_float64(((uint64_t)(val
& 0x80000000) << 32)
3005 | ((uint64_t)(val
& 0x7fffff) << 29));
3007 f64
= make_float64(((uint64_t)(val
& 0x80000000) << 32)
3009 | ((uint64_t)(val
& 0x7fffff) << 29));
3012 result_exp
= (380 - ((val
& 0x7f800000) >> 23)) / 2;
3014 f64
= recip_sqrt_estimate(f64
, env
);
3016 val64
= float64_val(f64
);
3018 val
= ((val64
>> 63) & 0x80000000)
3019 | ((result_exp
& 0xff) << 23)
3020 | ((val64
>> 29) & 0x7fffff);
3021 return make_float32(val
);
3024 uint32_t HELPER(recpe_u32
)(uint32_t a
, CPUState
*env
)
3028 if ((a
& 0x80000000) == 0) {
3032 f64
= make_float64((0x3feULL
<< 52)
3033 | ((int64_t)(a
& 0x7fffffff) << 21));
3035 f64
= recip_estimate (f64
, env
);
3037 return 0x80000000 | ((float64_val(f64
) >> 21) & 0x7fffffff);
3040 uint32_t HELPER(rsqrte_u32
)(uint32_t a
, CPUState
*env
)
3044 if ((a
& 0xc0000000) == 0) {
3048 if (a
& 0x80000000) {
3049 f64
= make_float64((0x3feULL
<< 52)
3050 | ((uint64_t)(a
& 0x7fffffff) << 21));
3051 } else { /* bits 31-30 == '01' */
3052 f64
= make_float64((0x3fdULL
<< 52)
3053 | ((uint64_t)(a
& 0x3fffffff) << 22));
3056 f64
= recip_sqrt_estimate(f64
, env
);
3058 return 0x80000000 | ((float64_val(f64
) >> 21) & 0x7fffffff);
3061 void HELPER(set_teecr
)(CPUState
*env
, uint32_t val
)
3064 if (env
->teecr
!= val
) {