9 #include "qemu-common.h"
10 #include "host-utils.h"
11 #if !defined(CONFIG_USER_ONLY)
12 #include "hw/loader.h"
15 static uint32_t cortexa9_cp15_c0_c1
[8] =
16 { 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
18 static uint32_t cortexa9_cp15_c0_c2
[8] =
19 { 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
21 static uint32_t cortexa8_cp15_c0_c1
[8] =
22 { 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
24 static uint32_t cortexa8_cp15_c0_c2
[8] =
25 { 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
27 static uint32_t mpcore_cp15_c0_c1
[8] =
28 { 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
30 static uint32_t mpcore_cp15_c0_c2
[8] =
31 { 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
33 static uint32_t arm1136_cp15_c0_c1
[8] =
34 { 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
36 static uint32_t arm1136_cp15_c0_c2
[8] =
37 { 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
39 static uint32_t cpu_arm_find_by_name(const char *name
);
41 static inline void set_feature(CPUARMState
*env
, int feature
)
43 env
->features
|= 1u << feature
;
46 static void cpu_reset_model_id(CPUARMState
*env
, uint32_t id
)
48 env
->cp15
.c0_cpuid
= id
;
50 case ARM_CPUID_ARM926
:
51 set_feature(env
, ARM_FEATURE_VFP
);
52 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x41011090;
53 env
->cp15
.c0_cachetype
= 0x1dd20d2;
54 env
->cp15
.c1_sys
= 0x00090078;
56 case ARM_CPUID_ARM946
:
57 set_feature(env
, ARM_FEATURE_MPU
);
58 env
->cp15
.c0_cachetype
= 0x0f004006;
59 env
->cp15
.c1_sys
= 0x00000078;
61 case ARM_CPUID_ARM1026
:
62 set_feature(env
, ARM_FEATURE_VFP
);
63 set_feature(env
, ARM_FEATURE_AUXCR
);
64 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410110a0;
65 env
->cp15
.c0_cachetype
= 0x1dd20d2;
66 env
->cp15
.c1_sys
= 0x00090078;
68 case ARM_CPUID_ARM1136_R2
:
69 case ARM_CPUID_ARM1136
:
70 set_feature(env
, ARM_FEATURE_V6
);
71 set_feature(env
, ARM_FEATURE_VFP
);
72 set_feature(env
, ARM_FEATURE_AUXCR
);
73 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410120b4;
74 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11111111;
75 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00000000;
76 memcpy(env
->cp15
.c0_c1
, arm1136_cp15_c0_c1
, 8 * sizeof(uint32_t));
77 memcpy(env
->cp15
.c0_c2
, arm1136_cp15_c0_c2
, 8 * sizeof(uint32_t));
78 env
->cp15
.c0_cachetype
= 0x1dd20d2;
79 env
->cp15
.c1_sys
= 0x00050078;
81 case ARM_CPUID_ARM11MPCORE
:
82 set_feature(env
, ARM_FEATURE_V6
);
83 set_feature(env
, ARM_FEATURE_V6K
);
84 set_feature(env
, ARM_FEATURE_VFP
);
85 set_feature(env
, ARM_FEATURE_AUXCR
);
86 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410120b4;
87 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11111111;
88 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00000000;
89 memcpy(env
->cp15
.c0_c1
, mpcore_cp15_c0_c1
, 8 * sizeof(uint32_t));
90 memcpy(env
->cp15
.c0_c2
, mpcore_cp15_c0_c2
, 8 * sizeof(uint32_t));
91 env
->cp15
.c0_cachetype
= 0x1dd20d2;
93 case ARM_CPUID_CORTEXA8
:
94 set_feature(env
, ARM_FEATURE_V6
);
95 set_feature(env
, ARM_FEATURE_V6K
);
96 set_feature(env
, ARM_FEATURE_V7
);
97 set_feature(env
, ARM_FEATURE_AUXCR
);
98 set_feature(env
, ARM_FEATURE_THUMB2
);
99 set_feature(env
, ARM_FEATURE_VFP
);
100 set_feature(env
, ARM_FEATURE_VFP3
);
101 set_feature(env
, ARM_FEATURE_NEON
);
102 set_feature(env
, ARM_FEATURE_THUMB2EE
);
103 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410330c0;
104 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11110222;
105 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00011100;
106 memcpy(env
->cp15
.c0_c1
, cortexa8_cp15_c0_c1
, 8 * sizeof(uint32_t));
107 memcpy(env
->cp15
.c0_c2
, cortexa8_cp15_c0_c2
, 8 * sizeof(uint32_t));
108 env
->cp15
.c0_cachetype
= 0x82048004;
109 env
->cp15
.c0_clid
= (1 << 27) | (2 << 24) | 3;
110 env
->cp15
.c0_ccsid
[0] = 0xe007e01a; /* 16k L1 dcache. */
111 env
->cp15
.c0_ccsid
[1] = 0x2007e01a; /* 16k L1 icache. */
112 env
->cp15
.c0_ccsid
[2] = 0xf0000000; /* No L2 icache. */
113 env
->cp15
.c1_sys
= 0x00c50078;
115 case ARM_CPUID_CORTEXA9
:
116 set_feature(env
, ARM_FEATURE_V6
);
117 set_feature(env
, ARM_FEATURE_V6K
);
118 set_feature(env
, ARM_FEATURE_V7
);
119 set_feature(env
, ARM_FEATURE_AUXCR
);
120 set_feature(env
, ARM_FEATURE_THUMB2
);
121 set_feature(env
, ARM_FEATURE_VFP
);
122 set_feature(env
, ARM_FEATURE_VFP3
);
123 set_feature(env
, ARM_FEATURE_VFP_FP16
);
124 set_feature(env
, ARM_FEATURE_NEON
);
125 set_feature(env
, ARM_FEATURE_THUMB2EE
);
126 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x41034000; /* Guess */
127 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11110222;
128 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x01111111;
129 memcpy(env
->cp15
.c0_c1
, cortexa9_cp15_c0_c1
, 8 * sizeof(uint32_t));
130 memcpy(env
->cp15
.c0_c2
, cortexa9_cp15_c0_c2
, 8 * sizeof(uint32_t));
131 env
->cp15
.c0_cachetype
= 0x80038003;
132 env
->cp15
.c0_clid
= (1 << 27) | (1 << 24) | 3;
133 env
->cp15
.c0_ccsid
[0] = 0xe00fe015; /* 16k L1 dcache. */
134 env
->cp15
.c0_ccsid
[1] = 0x200fe015; /* 16k L1 icache. */
135 env
->cp15
.c1_sys
= 0x00c50078;
137 case ARM_CPUID_CORTEXM3
:
138 set_feature(env
, ARM_FEATURE_V6
);
139 set_feature(env
, ARM_FEATURE_THUMB2
);
140 set_feature(env
, ARM_FEATURE_V7
);
141 set_feature(env
, ARM_FEATURE_M
);
142 set_feature(env
, ARM_FEATURE_DIV
);
144 case ARM_CPUID_ANY
: /* For userspace emulation. */
145 set_feature(env
, ARM_FEATURE_V6
);
146 set_feature(env
, ARM_FEATURE_V6K
);
147 set_feature(env
, ARM_FEATURE_V7
);
148 set_feature(env
, ARM_FEATURE_THUMB2
);
149 set_feature(env
, ARM_FEATURE_VFP
);
150 set_feature(env
, ARM_FEATURE_VFP3
);
151 set_feature(env
, ARM_FEATURE_VFP_FP16
);
152 set_feature(env
, ARM_FEATURE_NEON
);
153 set_feature(env
, ARM_FEATURE_THUMB2EE
);
154 set_feature(env
, ARM_FEATURE_DIV
);
156 case ARM_CPUID_TI915T
:
157 case ARM_CPUID_TI925T
:
158 set_feature(env
, ARM_FEATURE_OMAPCP
);
159 env
->cp15
.c0_cpuid
= ARM_CPUID_TI925T
; /* Depends on wiring. */
160 env
->cp15
.c0_cachetype
= 0x5109149;
161 env
->cp15
.c1_sys
= 0x00000070;
162 env
->cp15
.c15_i_max
= 0x000;
163 env
->cp15
.c15_i_min
= 0xff0;
165 case ARM_CPUID_PXA250
:
166 case ARM_CPUID_PXA255
:
167 case ARM_CPUID_PXA260
:
168 case ARM_CPUID_PXA261
:
169 case ARM_CPUID_PXA262
:
170 set_feature(env
, ARM_FEATURE_XSCALE
);
171 /* JTAG_ID is ((id << 28) | 0x09265013) */
172 env
->cp15
.c0_cachetype
= 0xd172172;
173 env
->cp15
.c1_sys
= 0x00000078;
175 case ARM_CPUID_PXA270_A0
:
176 case ARM_CPUID_PXA270_A1
:
177 case ARM_CPUID_PXA270_B0
:
178 case ARM_CPUID_PXA270_B1
:
179 case ARM_CPUID_PXA270_C0
:
180 case ARM_CPUID_PXA270_C5
:
181 set_feature(env
, ARM_FEATURE_XSCALE
);
182 /* JTAG_ID is ((id << 28) | 0x09265013) */
183 set_feature(env
, ARM_FEATURE_IWMMXT
);
184 env
->iwmmxt
.cregs
[ARM_IWMMXT_wCID
] = 0x69051000 | 'Q';
185 env
->cp15
.c0_cachetype
= 0xd172172;
186 env
->cp15
.c1_sys
= 0x00000078;
189 cpu_abort(env
, "Bad CPU ID: %x\n", id
);
194 void cpu_reset(CPUARMState
*env
)
198 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
199 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
200 log_cpu_state(env
, 0);
203 id
= env
->cp15
.c0_cpuid
;
204 memset(env
, 0, offsetof(CPUARMState
, breakpoints
));
206 cpu_reset_model_id(env
, id
);
207 #if defined (CONFIG_USER_ONLY)
208 env
->uncached_cpsr
= ARM_CPU_MODE_USR
;
209 /* For user mode we must enable access to coprocessors */
210 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = 1 << 30;
211 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
212 env
->cp15
.c15_cpar
= 3;
213 } else if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
214 env
->cp15
.c15_cpar
= 1;
217 /* SVC mode with interrupts disabled. */
218 env
->uncached_cpsr
= ARM_CPU_MODE_SVC
| CPSR_A
| CPSR_F
| CPSR_I
;
219 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
220 clear at reset. Initial SP and PC are loaded from ROM. */
224 env
->uncached_cpsr
&= ~CPSR_I
;
227 /* We should really use ldl_phys here, in case the guest
228 modified flash and reset itself. However images
229 loaded via -kenrel have not been copied yet, so load the
230 values directly from there. */
231 env
->regs
[13] = ldl_p(rom
);
234 env
->regs
[15] = pc
& ~1;
237 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = 0;
238 env
->cp15
.c2_base_mask
= 0xffffc000u
;
240 set_flush_to_zero(1, &env
->vfp
.standard_fp_status
);
241 set_flush_inputs_to_zero(1, &env
->vfp
.standard_fp_status
);
242 set_default_nan_mode(1, &env
->vfp
.standard_fp_status
);
246 static int vfp_gdb_get_reg(CPUState
*env
, uint8_t *buf
, int reg
)
250 /* VFP data registers are always little-endian. */
251 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
253 stfq_le_p(buf
, env
->vfp
.regs
[reg
]);
256 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
257 /* Aliases for Q regs. */
260 stfq_le_p(buf
, env
->vfp
.regs
[(reg
- 32) * 2]);
261 stfq_le_p(buf
+ 8, env
->vfp
.regs
[(reg
- 32) * 2 + 1]);
265 switch (reg
- nregs
) {
266 case 0: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]); return 4;
267 case 1: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSCR
]); return 4;
268 case 2: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]); return 4;
273 static int vfp_gdb_set_reg(CPUState
*env
, uint8_t *buf
, int reg
)
277 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
279 env
->vfp
.regs
[reg
] = ldfq_le_p(buf
);
282 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
285 env
->vfp
.regs
[(reg
- 32) * 2] = ldfq_le_p(buf
);
286 env
->vfp
.regs
[(reg
- 32) * 2 + 1] = ldfq_le_p(buf
+ 8);
290 switch (reg
- nregs
) {
291 case 0: env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
); return 4;
292 case 1: env
->vfp
.xregs
[ARM_VFP_FPSCR
] = ldl_p(buf
); return 4;
293 case 2: env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30); return 4;
298 CPUARMState
*cpu_arm_init(const char *cpu_model
)
302 static int inited
= 0;
304 id
= cpu_arm_find_by_name(cpu_model
);
307 env
= qemu_mallocz(sizeof(CPUARMState
));
311 arm_translate_init();
314 env
->cpu_model_str
= cpu_model
;
315 env
->cp15
.c0_cpuid
= id
;
317 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
318 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
319 51, "arm-neon.xml", 0);
320 } else if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
321 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
322 35, "arm-vfp3.xml", 0);
323 } else if (arm_feature(env
, ARM_FEATURE_VFP
)) {
324 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
325 19, "arm-vfp.xml", 0);
336 static const struct arm_cpu_t arm_cpu_names
[] = {
337 { ARM_CPUID_ARM926
, "arm926"},
338 { ARM_CPUID_ARM946
, "arm946"},
339 { ARM_CPUID_ARM1026
, "arm1026"},
340 { ARM_CPUID_ARM1136
, "arm1136"},
341 { ARM_CPUID_ARM1136_R2
, "arm1136-r2"},
342 { ARM_CPUID_ARM11MPCORE
, "arm11mpcore"},
343 { ARM_CPUID_CORTEXM3
, "cortex-m3"},
344 { ARM_CPUID_CORTEXA8
, "cortex-a8"},
345 { ARM_CPUID_CORTEXA9
, "cortex-a9"},
346 { ARM_CPUID_TI925T
, "ti925t" },
347 { ARM_CPUID_PXA250
, "pxa250" },
348 { ARM_CPUID_PXA255
, "pxa255" },
349 { ARM_CPUID_PXA260
, "pxa260" },
350 { ARM_CPUID_PXA261
, "pxa261" },
351 { ARM_CPUID_PXA262
, "pxa262" },
352 { ARM_CPUID_PXA270
, "pxa270" },
353 { ARM_CPUID_PXA270_A0
, "pxa270-a0" },
354 { ARM_CPUID_PXA270_A1
, "pxa270-a1" },
355 { ARM_CPUID_PXA270_B0
, "pxa270-b0" },
356 { ARM_CPUID_PXA270_B1
, "pxa270-b1" },
357 { ARM_CPUID_PXA270_C0
, "pxa270-c0" },
358 { ARM_CPUID_PXA270_C5
, "pxa270-c5" },
359 { ARM_CPUID_ANY
, "any"},
363 void arm_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
367 (*cpu_fprintf
)(f
, "Available CPUs:\n");
368 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
369 (*cpu_fprintf
)(f
, " %s\n", arm_cpu_names
[i
].name
);
373 /* return 0 if not found */
374 static uint32_t cpu_arm_find_by_name(const char *name
)
380 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
381 if (strcmp(name
, arm_cpu_names
[i
].name
) == 0) {
382 id
= arm_cpu_names
[i
].id
;
389 void cpu_arm_close(CPUARMState
*env
)
394 uint32_t cpsr_read(CPUARMState
*env
)
398 return env
->uncached_cpsr
| (env
->NF
& 0x80000000) | (ZF
<< 30) |
399 (env
->CF
<< 29) | ((env
->VF
& 0x80000000) >> 3) | (env
->QF
<< 27)
400 | (env
->thumb
<< 5) | ((env
->condexec_bits
& 3) << 25)
401 | ((env
->condexec_bits
& 0xfc) << 8)
405 void cpsr_write(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
407 if (mask
& CPSR_NZCV
) {
408 env
->ZF
= (~val
) & CPSR_Z
;
410 env
->CF
= (val
>> 29) & 1;
411 env
->VF
= (val
<< 3) & 0x80000000;
414 env
->QF
= ((val
& CPSR_Q
) != 0);
416 env
->thumb
= ((val
& CPSR_T
) != 0);
417 if (mask
& CPSR_IT_0_1
) {
418 env
->condexec_bits
&= ~3;
419 env
->condexec_bits
|= (val
>> 25) & 3;
421 if (mask
& CPSR_IT_2_7
) {
422 env
->condexec_bits
&= 3;
423 env
->condexec_bits
|= (val
>> 8) & 0xfc;
425 if (mask
& CPSR_GE
) {
426 env
->GE
= (val
>> 16) & 0xf;
429 if ((env
->uncached_cpsr
^ val
) & mask
& CPSR_M
) {
430 switch_mode(env
, val
& CPSR_M
);
432 mask
&= ~CACHED_CPSR_BITS
;
433 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~mask
) | (val
& mask
);
436 /* Sign/zero extend */
437 uint32_t HELPER(sxtb16
)(uint32_t x
)
440 res
= (uint16_t)(int8_t)x
;
441 res
|= (uint32_t)(int8_t)(x
>> 16) << 16;
445 uint32_t HELPER(uxtb16
)(uint32_t x
)
448 res
= (uint16_t)(uint8_t)x
;
449 res
|= (uint32_t)(uint8_t)(x
>> 16) << 16;
453 uint32_t HELPER(clz
)(uint32_t x
)
458 int32_t HELPER(sdiv
)(int32_t num
, int32_t den
)
462 if (num
== INT_MIN
&& den
== -1)
467 uint32_t HELPER(udiv
)(uint32_t num
, uint32_t den
)
474 uint32_t HELPER(rbit
)(uint32_t x
)
476 x
= ((x
& 0xff000000) >> 24)
477 | ((x
& 0x00ff0000) >> 8)
478 | ((x
& 0x0000ff00) << 8)
479 | ((x
& 0x000000ff) << 24);
480 x
= ((x
& 0xf0f0f0f0) >> 4)
481 | ((x
& 0x0f0f0f0f) << 4);
482 x
= ((x
& 0x88888888) >> 3)
483 | ((x
& 0x44444444) >> 1)
484 | ((x
& 0x22222222) << 1)
485 | ((x
& 0x11111111) << 3);
489 uint32_t HELPER(abs
)(uint32_t x
)
491 return ((int32_t)x
< 0) ? -x
: x
;
494 #if defined(CONFIG_USER_ONLY)
496 void do_interrupt (CPUState
*env
)
498 env
->exception_index
= -1;
501 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
502 int mmu_idx
, int is_softmmu
)
505 env
->exception_index
= EXCP_PREFETCH_ABORT
;
506 env
->cp15
.c6_insn
= address
;
508 env
->exception_index
= EXCP_DATA_ABORT
;
509 env
->cp15
.c6_data
= address
;
514 /* These should probably raise undefined insn exceptions. */
515 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
517 int op1
= (insn
>> 8) & 0xf;
518 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
522 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
524 int op1
= (insn
>> 8) & 0xf;
525 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
529 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
531 cpu_abort(env
, "cp15 insn %08x\n", insn
);
534 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
536 cpu_abort(env
, "cp15 insn %08x\n", insn
);
539 /* These should probably raise undefined insn exceptions. */
540 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
542 cpu_abort(env
, "v7m_mrs %d\n", reg
);
545 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
547 cpu_abort(env
, "v7m_mrs %d\n", reg
);
551 void switch_mode(CPUState
*env
, int mode
)
553 if (mode
!= ARM_CPU_MODE_USR
)
554 cpu_abort(env
, "Tried to switch out of user mode\n");
557 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
559 cpu_abort(env
, "banked r13 write\n");
562 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
564 cpu_abort(env
, "banked r13 read\n");
570 extern int semihosting_enabled
;
572 /* Map CPU modes onto saved register banks. */
573 static inline int bank_number (int mode
)
576 case ARM_CPU_MODE_USR
:
577 case ARM_CPU_MODE_SYS
:
579 case ARM_CPU_MODE_SVC
:
581 case ARM_CPU_MODE_ABT
:
583 case ARM_CPU_MODE_UND
:
585 case ARM_CPU_MODE_IRQ
:
587 case ARM_CPU_MODE_FIQ
:
590 cpu_abort(cpu_single_env
, "Bad mode %x\n", mode
);
594 void switch_mode(CPUState
*env
, int mode
)
599 old_mode
= env
->uncached_cpsr
& CPSR_M
;
600 if (mode
== old_mode
)
603 if (old_mode
== ARM_CPU_MODE_FIQ
) {
604 memcpy (env
->fiq_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
605 memcpy (env
->regs
+ 8, env
->usr_regs
, 5 * sizeof(uint32_t));
606 } else if (mode
== ARM_CPU_MODE_FIQ
) {
607 memcpy (env
->usr_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
608 memcpy (env
->regs
+ 8, env
->fiq_regs
, 5 * sizeof(uint32_t));
611 i
= bank_number(old_mode
);
612 env
->banked_r13
[i
] = env
->regs
[13];
613 env
->banked_r14
[i
] = env
->regs
[14];
614 env
->banked_spsr
[i
] = env
->spsr
;
616 i
= bank_number(mode
);
617 env
->regs
[13] = env
->banked_r13
[i
];
618 env
->regs
[14] = env
->banked_r14
[i
];
619 env
->spsr
= env
->banked_spsr
[i
];
622 static void v7m_push(CPUARMState
*env
, uint32_t val
)
625 stl_phys(env
->regs
[13], val
);
628 static uint32_t v7m_pop(CPUARMState
*env
)
631 val
= ldl_phys(env
->regs
[13]);
636 /* Switch to V7M main or process stack pointer. */
637 static void switch_v7m_sp(CPUARMState
*env
, int process
)
640 if (env
->v7m
.current_sp
!= process
) {
641 tmp
= env
->v7m
.other_sp
;
642 env
->v7m
.other_sp
= env
->regs
[13];
644 env
->v7m
.current_sp
= process
;
648 static void do_v7m_exception_exit(CPUARMState
*env
)
653 type
= env
->regs
[15];
654 if (env
->v7m
.exception
!= 0)
655 armv7m_nvic_complete_irq(env
->nvic
, env
->v7m
.exception
);
657 /* Switch to the target stack. */
658 switch_v7m_sp(env
, (type
& 4) != 0);
660 env
->regs
[0] = v7m_pop(env
);
661 env
->regs
[1] = v7m_pop(env
);
662 env
->regs
[2] = v7m_pop(env
);
663 env
->regs
[3] = v7m_pop(env
);
664 env
->regs
[12] = v7m_pop(env
);
665 env
->regs
[14] = v7m_pop(env
);
666 env
->regs
[15] = v7m_pop(env
);
668 xpsr_write(env
, xpsr
, 0xfffffdff);
669 /* Undo stack alignment. */
672 /* ??? The exception return type specifies Thread/Handler mode. However
673 this is also implied by the xPSR value. Not sure what to do
674 if there is a mismatch. */
675 /* ??? Likewise for mismatches between the CONTROL register and the stack
679 static void do_interrupt_v7m(CPUARMState
*env
)
681 uint32_t xpsr
= xpsr_read(env
);
686 if (env
->v7m
.current_sp
)
688 if (env
->v7m
.exception
== 0)
691 /* For exceptions we just mark as pending on the NVIC, and let that
693 /* TODO: Need to escalate if the current priority is higher than the
694 one we're raising. */
695 switch (env
->exception_index
) {
697 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
);
701 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SVC
);
703 case EXCP_PREFETCH_ABORT
:
704 case EXCP_DATA_ABORT
:
705 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
);
708 if (semihosting_enabled
) {
710 nr
= lduw_code(env
->regs
[15]) & 0xff;
713 env
->regs
[0] = do_arm_semihosting(env
);
717 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_DEBUG
);
720 env
->v7m
.exception
= armv7m_nvic_acknowledge_irq(env
->nvic
);
722 case EXCP_EXCEPTION_EXIT
:
723 do_v7m_exception_exit(env
);
726 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
727 return; /* Never happens. Keep compiler happy. */
730 /* Align stack pointer. */
731 /* ??? Should only do this if Configuration Control Register
732 STACKALIGN bit is set. */
733 if (env
->regs
[13] & 4) {
737 /* Switch to the handler mode. */
739 v7m_push(env
, env
->regs
[15]);
740 v7m_push(env
, env
->regs
[14]);
741 v7m_push(env
, env
->regs
[12]);
742 v7m_push(env
, env
->regs
[3]);
743 v7m_push(env
, env
->regs
[2]);
744 v7m_push(env
, env
->regs
[1]);
745 v7m_push(env
, env
->regs
[0]);
746 switch_v7m_sp(env
, 0);
747 env
->uncached_cpsr
&= ~CPSR_IT
;
749 addr
= ldl_phys(env
->v7m
.vecbase
+ env
->v7m
.exception
* 4);
750 env
->regs
[15] = addr
& 0xfffffffe;
751 env
->thumb
= addr
& 1;
754 /* Handle a CPU exception. */
755 void do_interrupt(CPUARMState
*env
)
763 do_interrupt_v7m(env
);
766 /* TODO: Vectored interrupt controller. */
767 switch (env
->exception_index
) {
769 new_mode
= ARM_CPU_MODE_UND
;
778 if (semihosting_enabled
) {
779 /* Check for semihosting interrupt. */
781 mask
= lduw_code(env
->regs
[15] - 2) & 0xff;
783 mask
= ldl_code(env
->regs
[15] - 4) & 0xffffff;
785 /* Only intercept calls from privileged modes, to provide some
786 semblance of security. */
787 if (((mask
== 0x123456 && !env
->thumb
)
788 || (mask
== 0xab && env
->thumb
))
789 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
790 env
->regs
[0] = do_arm_semihosting(env
);
794 new_mode
= ARM_CPU_MODE_SVC
;
797 /* The PC already points to the next instruction. */
801 /* See if this is a semihosting syscall. */
802 if (env
->thumb
&& semihosting_enabled
) {
803 mask
= lduw_code(env
->regs
[15]) & 0xff;
805 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
807 env
->regs
[0] = do_arm_semihosting(env
);
811 /* Fall through to prefetch abort. */
812 case EXCP_PREFETCH_ABORT
:
813 new_mode
= ARM_CPU_MODE_ABT
;
815 mask
= CPSR_A
| CPSR_I
;
818 case EXCP_DATA_ABORT
:
819 new_mode
= ARM_CPU_MODE_ABT
;
821 mask
= CPSR_A
| CPSR_I
;
825 new_mode
= ARM_CPU_MODE_IRQ
;
827 /* Disable IRQ and imprecise data aborts. */
828 mask
= CPSR_A
| CPSR_I
;
832 new_mode
= ARM_CPU_MODE_FIQ
;
834 /* Disable FIQ, IRQ and imprecise data aborts. */
835 mask
= CPSR_A
| CPSR_I
| CPSR_F
;
839 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
840 return; /* Never happens. Keep compiler happy. */
843 if (env
->cp15
.c1_sys
& (1 << 13)) {
846 switch_mode (env
, new_mode
);
847 env
->spsr
= cpsr_read(env
);
849 env
->condexec_bits
= 0;
850 /* Switch to the new mode, and to the correct instruction set. */
851 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~CPSR_M
) | new_mode
;
852 env
->uncached_cpsr
|= mask
;
853 env
->thumb
= (env
->cp15
.c1_sys
& (1 << 30)) != 0;
854 env
->regs
[14] = env
->regs
[15] + offset
;
855 env
->regs
[15] = addr
;
856 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
859 /* Check section/page access permissions.
860 Returns the page protection flags, or zero if the access is not
862 static inline int check_ap(CPUState
*env
, int ap
, int domain
, int access_type
,
868 return PAGE_READ
| PAGE_WRITE
;
870 if (access_type
== 1)
877 if (access_type
== 1)
879 switch ((env
->cp15
.c1_sys
>> 8) & 3) {
881 return is_user
? 0 : PAGE_READ
;
888 return is_user
? 0 : PAGE_READ
| PAGE_WRITE
;
893 return PAGE_READ
| PAGE_WRITE
;
895 return PAGE_READ
| PAGE_WRITE
;
896 case 4: /* Reserved. */
899 return is_user
? 0 : prot_ro
;
903 if (!arm_feature (env
, ARM_FEATURE_V7
))
911 static uint32_t get_level1_table_address(CPUState
*env
, uint32_t address
)
915 if (address
& env
->cp15
.c2_mask
)
916 table
= env
->cp15
.c2_base1
& 0xffffc000;
918 table
= env
->cp15
.c2_base0
& env
->cp15
.c2_base_mask
;
920 table
|= (address
>> 18) & 0x3ffc;
924 static int get_phys_addr_v5(CPUState
*env
, uint32_t address
, int access_type
,
925 int is_user
, uint32_t *phys_ptr
, int *prot
,
926 target_ulong
*page_size
)
936 /* Pagetable walk. */
937 /* Lookup l1 descriptor. */
938 table
= get_level1_table_address(env
, address
);
939 desc
= ldl_phys(table
);
941 domain
= (env
->cp15
.c3
>> ((desc
>> 4) & 0x1e)) & 3;
943 /* Section translation fault. */
947 if (domain
== 0 || domain
== 2) {
949 code
= 9; /* Section domain fault. */
951 code
= 11; /* Page domain fault. */
956 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
957 ap
= (desc
>> 10) & 3;
959 *page_size
= 1024 * 1024;
961 /* Lookup l2 entry. */
963 /* Coarse pagetable. */
964 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
966 /* Fine pagetable. */
967 table
= (desc
& 0xfffff000) | ((address
>> 8) & 0xffc);
969 desc
= ldl_phys(table
);
971 case 0: /* Page translation fault. */
974 case 1: /* 64k page. */
975 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
976 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
977 *page_size
= 0x10000;
979 case 2: /* 4k page. */
980 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
981 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
984 case 3: /* 1k page. */
986 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
987 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
989 /* Page translation fault. */
994 phys_addr
= (desc
& 0xfffffc00) | (address
& 0x3ff);
996 ap
= (desc
>> 4) & 3;
1000 /* Never happens, but compiler isn't smart enough to tell. */
1005 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1007 /* Access permission fault. */
1011 *phys_ptr
= phys_addr
;
1014 return code
| (domain
<< 4);
1017 static int get_phys_addr_v6(CPUState
*env
, uint32_t address
, int access_type
,
1018 int is_user
, uint32_t *phys_ptr
, int *prot
,
1019 target_ulong
*page_size
)
1030 /* Pagetable walk. */
1031 /* Lookup l1 descriptor. */
1032 table
= get_level1_table_address(env
, address
);
1033 desc
= ldl_phys(table
);
1036 /* Section translation fault. */
1040 } else if (type
== 2 && (desc
& (1 << 18))) {
1044 /* Section or page. */
1045 domain
= (desc
>> 4) & 0x1e;
1047 domain
= (env
->cp15
.c3
>> domain
) & 3;
1048 if (domain
== 0 || domain
== 2) {
1050 code
= 9; /* Section domain fault. */
1052 code
= 11; /* Page domain fault. */
1056 if (desc
& (1 << 18)) {
1058 phys_addr
= (desc
& 0xff000000) | (address
& 0x00ffffff);
1059 *page_size
= 0x1000000;
1062 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
1063 *page_size
= 0x100000;
1065 ap
= ((desc
>> 10) & 3) | ((desc
>> 13) & 4);
1066 xn
= desc
& (1 << 4);
1069 /* Lookup l2 entry. */
1070 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
1071 desc
= ldl_phys(table
);
1072 ap
= ((desc
>> 4) & 3) | ((desc
>> 7) & 4);
1074 case 0: /* Page translation fault. */
1077 case 1: /* 64k page. */
1078 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
1079 xn
= desc
& (1 << 15);
1080 *page_size
= 0x10000;
1082 case 2: case 3: /* 4k page. */
1083 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
1085 *page_size
= 0x1000;
1088 /* Never happens, but compiler isn't smart enough to tell. */
1094 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1096 if (xn
&& access_type
== 2)
1099 /* The simplified model uses AP[0] as an access control bit. */
1100 if ((env
->cp15
.c1_sys
& (1 << 29)) && (ap
& 1) == 0) {
1101 /* Access flag fault. */
1102 code
= (code
== 15) ? 6 : 3;
1105 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1107 /* Access permission fault. */
1114 *phys_ptr
= phys_addr
;
1117 return code
| (domain
<< 4);
1120 static int get_phys_addr_mpu(CPUState
*env
, uint32_t address
, int access_type
,
1121 int is_user
, uint32_t *phys_ptr
, int *prot
)
1127 *phys_ptr
= address
;
1128 for (n
= 7; n
>= 0; n
--) {
1129 base
= env
->cp15
.c6_region
[n
];
1130 if ((base
& 1) == 0)
1132 mask
= 1 << ((base
>> 1) & 0x1f);
1133 /* Keep this shift separate from the above to avoid an
1134 (undefined) << 32. */
1135 mask
= (mask
<< 1) - 1;
1136 if (((base
^ address
) & ~mask
) == 0)
1142 if (access_type
== 2) {
1143 mask
= env
->cp15
.c5_insn
;
1145 mask
= env
->cp15
.c5_data
;
1147 mask
= (mask
>> (n
* 4)) & 0xf;
1154 *prot
= PAGE_READ
| PAGE_WRITE
;
1159 *prot
|= PAGE_WRITE
;
1162 *prot
= PAGE_READ
| PAGE_WRITE
;
1173 /* Bad permission. */
1180 static inline int get_phys_addr(CPUState
*env
, uint32_t address
,
1181 int access_type
, int is_user
,
1182 uint32_t *phys_ptr
, int *prot
,
1183 target_ulong
*page_size
)
1185 /* Fast Context Switch Extension. */
1186 if (address
< 0x02000000)
1187 address
+= env
->cp15
.c13_fcse
;
1189 if ((env
->cp15
.c1_sys
& 1) == 0) {
1190 /* MMU/MPU disabled. */
1191 *phys_ptr
= address
;
1192 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1193 *page_size
= TARGET_PAGE_SIZE
;
1195 } else if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1196 *page_size
= TARGET_PAGE_SIZE
;
1197 return get_phys_addr_mpu(env
, address
, access_type
, is_user
, phys_ptr
,
1199 } else if (env
->cp15
.c1_sys
& (1 << 23)) {
1200 return get_phys_addr_v6(env
, address
, access_type
, is_user
, phys_ptr
,
1203 return get_phys_addr_v5(env
, address
, access_type
, is_user
, phys_ptr
,
1208 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
,
1209 int access_type
, int mmu_idx
, int is_softmmu
)
1212 target_ulong page_size
;
1216 is_user
= mmu_idx
== MMU_USER_IDX
;
1217 ret
= get_phys_addr(env
, address
, access_type
, is_user
, &phys_addr
, &prot
,
1220 /* Map a single [sub]page. */
1221 phys_addr
&= ~(uint32_t)0x3ff;
1222 address
&= ~(uint32_t)0x3ff;
1223 tlb_set_page (env
, address
, phys_addr
, prot
, mmu_idx
, page_size
);
1227 if (access_type
== 2) {
1228 env
->cp15
.c5_insn
= ret
;
1229 env
->cp15
.c6_insn
= address
;
1230 env
->exception_index
= EXCP_PREFETCH_ABORT
;
1232 env
->cp15
.c5_data
= ret
;
1233 if (access_type
== 1 && arm_feature(env
, ARM_FEATURE_V6
))
1234 env
->cp15
.c5_data
|= (1 << 11);
1235 env
->cp15
.c6_data
= address
;
1236 env
->exception_index
= EXCP_DATA_ABORT
;
1241 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1244 target_ulong page_size
;
1248 ret
= get_phys_addr(env
, addr
, 0, 0, &phys_addr
, &prot
, &page_size
);
1256 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1258 int cp_num
= (insn
>> 8) & 0xf;
1259 int cp_info
= (insn
>> 5) & 7;
1260 int src
= (insn
>> 16) & 0xf;
1261 int operand
= insn
& 0xf;
1263 if (env
->cp
[cp_num
].cp_write
)
1264 env
->cp
[cp_num
].cp_write(env
->cp
[cp_num
].opaque
,
1265 cp_info
, src
, operand
, val
);
1268 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
1270 int cp_num
= (insn
>> 8) & 0xf;
1271 int cp_info
= (insn
>> 5) & 7;
1272 int dest
= (insn
>> 16) & 0xf;
1273 int operand
= insn
& 0xf;
1275 if (env
->cp
[cp_num
].cp_read
)
1276 return env
->cp
[cp_num
].cp_read(env
->cp
[cp_num
].opaque
,
1277 cp_info
, dest
, operand
);
1281 /* Return basic MPU access permission bits. */
1282 static uint32_t simple_mpu_ap_bits(uint32_t val
)
1289 for (i
= 0; i
< 16; i
+= 2) {
1290 ret
|= (val
>> i
) & mask
;
1296 /* Pad basic MPU access permission bits to extended format. */
1297 static uint32_t extended_mpu_ap_bits(uint32_t val
)
1304 for (i
= 0; i
< 16; i
+= 2) {
1305 ret
|= (val
& mask
) << i
;
1311 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1317 op1
= (insn
>> 21) & 7;
1318 op2
= (insn
>> 5) & 7;
1320 switch ((insn
>> 16) & 0xf) {
1323 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1325 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1327 if (arm_feature(env
, ARM_FEATURE_V7
)
1328 && op1
== 2 && crm
== 0 && op2
== 0) {
1329 env
->cp15
.c0_cssel
= val
& 0xf;
1333 case 1: /* System configuration. */
1334 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1338 if (!arm_feature(env
, ARM_FEATURE_XSCALE
) || crm
== 0)
1339 env
->cp15
.c1_sys
= val
;
1340 /* ??? Lots of these bits are not implemented. */
1341 /* This may enable/disable the MMU, so do a TLB flush. */
1344 case 1: /* Auxiliary cotrol register. */
1345 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1346 env
->cp15
.c1_xscaleauxcr
= val
;
1349 /* Not implemented. */
1352 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1354 if (env
->cp15
.c1_coproc
!= val
) {
1355 env
->cp15
.c1_coproc
= val
;
1356 /* ??? Is this safe when called from within a TB? */
1364 case 2: /* MMU Page table control / MPU cache control. */
1365 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1368 env
->cp15
.c2_data
= val
;
1371 env
->cp15
.c2_insn
= val
;
1379 env
->cp15
.c2_base0
= val
;
1382 env
->cp15
.c2_base1
= val
;
1386 env
->cp15
.c2_control
= val
;
1387 env
->cp15
.c2_mask
= ~(((uint32_t)0xffffffffu
) >> val
);
1388 env
->cp15
.c2_base_mask
= ~((uint32_t)0x3fffu
>> val
);
1395 case 3: /* MMU Domain access control / MPU write buffer control. */
1397 tlb_flush(env
, 1); /* Flush TLB as domain not tracked in TLB */
1399 case 4: /* Reserved. */
1401 case 5: /* MMU Fault status / MPU access permission. */
1402 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1406 if (arm_feature(env
, ARM_FEATURE_MPU
))
1407 val
= extended_mpu_ap_bits(val
);
1408 env
->cp15
.c5_data
= val
;
1411 if (arm_feature(env
, ARM_FEATURE_MPU
))
1412 val
= extended_mpu_ap_bits(val
);
1413 env
->cp15
.c5_insn
= val
;
1416 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1418 env
->cp15
.c5_data
= val
;
1421 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1423 env
->cp15
.c5_insn
= val
;
1429 case 6: /* MMU Fault address / MPU base/size. */
1430 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1433 env
->cp15
.c6_region
[crm
] = val
;
1435 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1439 env
->cp15
.c6_data
= val
;
1441 case 1: /* ??? This is WFAR on armv6 */
1443 env
->cp15
.c6_insn
= val
;
1450 case 7: /* Cache control. */
1451 env
->cp15
.c15_i_max
= 0x000;
1452 env
->cp15
.c15_i_min
= 0xff0;
1453 /* No cache, so nothing to do. */
1454 /* ??? MPCore has VA to PA translation functions. */
1456 case 8: /* MMU TLB control. */
1458 case 0: /* Invalidate all. */
1461 case 1: /* Invalidate single TLB entry. */
1462 tlb_flush_page(env
, val
& TARGET_PAGE_MASK
);
1464 case 2: /* Invalidate on ASID. */
1465 tlb_flush(env
, val
== 0);
1467 case 3: /* Invalidate single entry on MVA. */
1468 /* ??? This is like case 1, but ignores ASID. */
1476 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1479 case 0: /* Cache lockdown. */
1481 case 0: /* L1 cache. */
1484 env
->cp15
.c9_data
= val
;
1487 env
->cp15
.c9_insn
= val
;
1493 case 1: /* L2 cache. */
1494 /* Ignore writes to L2 lockdown/auxiliary registers. */
1500 case 1: /* TCM memory region registers. */
1501 /* Not implemented. */
1507 case 10: /* MMU TLB lockdown. */
1508 /* ??? TLB lockdown not implemented. */
1510 case 12: /* Reserved. */
1512 case 13: /* Process ID. */
1515 /* Unlike real hardware the qemu TLB uses virtual addresses,
1516 not modified virtual addresses, so this causes a TLB flush.
1518 if (env
->cp15
.c13_fcse
!= val
)
1520 env
->cp15
.c13_fcse
= val
;
1523 /* This changes the ASID, so do a TLB flush. */
1524 if (env
->cp15
.c13_context
!= val
1525 && !arm_feature(env
, ARM_FEATURE_MPU
))
1527 env
->cp15
.c13_context
= val
;
1533 case 14: /* Reserved. */
1535 case 15: /* Implementation specific. */
1536 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1537 if (op2
== 0 && crm
== 1) {
1538 if (env
->cp15
.c15_cpar
!= (val
& 0x3fff)) {
1539 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1541 env
->cp15
.c15_cpar
= val
& 0x3fff;
1547 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1551 case 1: /* Set TI925T configuration. */
1552 env
->cp15
.c15_ticonfig
= val
& 0xe7;
1553 env
->cp15
.c0_cpuid
= (val
& (1 << 5)) ? /* OS_TYPE bit */
1554 ARM_CPUID_TI915T
: ARM_CPUID_TI925T
;
1556 case 2: /* Set I_max. */
1557 env
->cp15
.c15_i_max
= val
;
1559 case 3: /* Set I_min. */
1560 env
->cp15
.c15_i_min
= val
;
1562 case 4: /* Set thread-ID. */
1563 env
->cp15
.c15_threadid
= val
& 0xffff;
1565 case 8: /* Wait-for-interrupt (deprecated). */
1566 cpu_interrupt(env
, CPU_INTERRUPT_HALT
);
1576 /* ??? For debugging only. Should raise illegal instruction exception. */
1577 cpu_abort(env
, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1578 (insn
>> 16) & 0xf, crm
, op1
, op2
);
1581 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
1587 op1
= (insn
>> 21) & 7;
1588 op2
= (insn
>> 5) & 7;
1590 switch ((insn
>> 16) & 0xf) {
1591 case 0: /* ID codes. */
1597 case 0: /* Device ID. */
1598 return env
->cp15
.c0_cpuid
;
1599 case 1: /* Cache Type. */
1600 return env
->cp15
.c0_cachetype
;
1601 case 2: /* TCM status. */
1603 case 3: /* TLB type register. */
1604 return 0; /* No lockable TLB entries. */
1605 case 5: /* CPU ID */
1606 if (ARM_CPUID(env
) == ARM_CPUID_CORTEXA9
) {
1607 return env
->cpu_index
| 0x80000900;
1609 return env
->cpu_index
;
1615 if (!arm_feature(env
, ARM_FEATURE_V6
))
1617 return env
->cp15
.c0_c1
[op2
];
1619 if (!arm_feature(env
, ARM_FEATURE_V6
))
1621 return env
->cp15
.c0_c2
[op2
];
1622 case 3: case 4: case 5: case 6: case 7:
1628 /* These registers aren't documented on arm11 cores. However
1629 Linux looks at them anyway. */
1630 if (!arm_feature(env
, ARM_FEATURE_V6
))
1634 if (!arm_feature(env
, ARM_FEATURE_V7
))
1639 return env
->cp15
.c0_ccsid
[env
->cp15
.c0_cssel
];
1641 return env
->cp15
.c0_clid
;
1647 if (op2
!= 0 || crm
!= 0)
1649 return env
->cp15
.c0_cssel
;
1653 case 1: /* System configuration. */
1654 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1657 case 0: /* Control register. */
1658 return env
->cp15
.c1_sys
;
1659 case 1: /* Auxiliary control register. */
1660 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1661 return env
->cp15
.c1_xscaleauxcr
;
1662 if (!arm_feature(env
, ARM_FEATURE_AUXCR
))
1664 switch (ARM_CPUID(env
)) {
1665 case ARM_CPUID_ARM1026
:
1667 case ARM_CPUID_ARM1136
:
1668 case ARM_CPUID_ARM1136_R2
:
1670 case ARM_CPUID_ARM11MPCORE
:
1672 case ARM_CPUID_CORTEXA8
:
1674 case ARM_CPUID_CORTEXA9
:
1679 case 2: /* Coprocessor access register. */
1680 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1682 return env
->cp15
.c1_coproc
;
1686 case 2: /* MMU Page table control / MPU cache control. */
1687 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1690 return env
->cp15
.c2_data
;
1693 return env
->cp15
.c2_insn
;
1701 return env
->cp15
.c2_base0
;
1703 return env
->cp15
.c2_base1
;
1705 return env
->cp15
.c2_control
;
1710 case 3: /* MMU Domain access control / MPU write buffer control. */
1711 return env
->cp15
.c3
;
1712 case 4: /* Reserved. */
1714 case 5: /* MMU Fault status / MPU access permission. */
1715 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1719 if (arm_feature(env
, ARM_FEATURE_MPU
))
1720 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1721 return env
->cp15
.c5_data
;
1723 if (arm_feature(env
, ARM_FEATURE_MPU
))
1724 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1725 return env
->cp15
.c5_insn
;
1727 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1729 return env
->cp15
.c5_data
;
1731 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1733 return env
->cp15
.c5_insn
;
1737 case 6: /* MMU Fault address. */
1738 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1741 return env
->cp15
.c6_region
[crm
];
1743 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1747 return env
->cp15
.c6_data
;
1749 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1750 /* Watchpoint Fault Adrress. */
1751 return 0; /* Not implemented. */
1753 /* Instruction Fault Adrress. */
1754 /* Arm9 doesn't have an IFAR, but implementing it anyway
1755 shouldn't do any harm. */
1756 return env
->cp15
.c6_insn
;
1759 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1760 /* Instruction Fault Adrress. */
1761 return env
->cp15
.c6_insn
;
1769 case 7: /* Cache control. */
1770 /* FIXME: Should only clear Z flag if destination is r15. */
1773 case 8: /* MMU TLB control. */
1775 case 9: /* Cache lockdown. */
1777 case 0: /* L1 cache. */
1778 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1782 return env
->cp15
.c9_data
;
1784 return env
->cp15
.c9_insn
;
1788 case 1: /* L2 cache */
1791 /* L2 Lockdown and Auxiliary control. */
1796 case 10: /* MMU TLB lockdown. */
1797 /* ??? TLB lockdown not implemented. */
1799 case 11: /* TCM DMA control. */
1800 case 12: /* Reserved. */
1802 case 13: /* Process ID. */
1805 return env
->cp15
.c13_fcse
;
1807 return env
->cp15
.c13_context
;
1811 case 14: /* Reserved. */
1813 case 15: /* Implementation specific. */
1814 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1815 if (op2
== 0 && crm
== 1)
1816 return env
->cp15
.c15_cpar
;
1820 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1824 case 1: /* Read TI925T configuration. */
1825 return env
->cp15
.c15_ticonfig
;
1826 case 2: /* Read I_max. */
1827 return env
->cp15
.c15_i_max
;
1828 case 3: /* Read I_min. */
1829 return env
->cp15
.c15_i_min
;
1830 case 4: /* Read thread-ID. */
1831 return env
->cp15
.c15_threadid
;
1832 case 8: /* TI925T_status */
1835 /* TODO: Peripheral port remap register:
1836 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1837 * controller base address at $rn & ~0xfff and map size of
1838 * 0x200 << ($rn & 0xfff), when MMU is off. */
1844 /* ??? For debugging only. Should raise illegal instruction exception. */
1845 cpu_abort(env
, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1846 (insn
>> 16) & 0xf, crm
, op1
, op2
);
1850 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
1852 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
1853 env
->regs
[13] = val
;
1855 env
->banked_r13
[bank_number(mode
)] = val
;
1859 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
1861 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
1862 return env
->regs
[13];
1864 return env
->banked_r13
[bank_number(mode
)];
1868 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
1872 return xpsr_read(env
) & 0xf8000000;
1874 return xpsr_read(env
) & 0xf80001ff;
1876 return xpsr_read(env
) & 0xff00fc00;
1878 return xpsr_read(env
) & 0xff00fdff;
1880 return xpsr_read(env
) & 0x000001ff;
1882 return xpsr_read(env
) & 0x0700fc00;
1884 return xpsr_read(env
) & 0x0700edff;
1886 return env
->v7m
.current_sp
? env
->v7m
.other_sp
: env
->regs
[13];
1888 return env
->v7m
.current_sp
? env
->regs
[13] : env
->v7m
.other_sp
;
1889 case 16: /* PRIMASK */
1890 return (env
->uncached_cpsr
& CPSR_I
) != 0;
1891 case 17: /* FAULTMASK */
1892 return (env
->uncached_cpsr
& CPSR_F
) != 0;
1893 case 18: /* BASEPRI */
1894 case 19: /* BASEPRI_MAX */
1895 return env
->v7m
.basepri
;
1896 case 20: /* CONTROL */
1897 return env
->v7m
.control
;
1899 /* ??? For debugging only. */
1900 cpu_abort(env
, "Unimplemented system register read (%d)\n", reg
);
1905 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
1909 xpsr_write(env
, val
, 0xf8000000);
1912 xpsr_write(env
, val
, 0xf8000000);
1915 xpsr_write(env
, val
, 0xfe00fc00);
1918 xpsr_write(env
, val
, 0xfe00fc00);
1921 /* IPSR bits are readonly. */
1924 xpsr_write(env
, val
, 0x0600fc00);
1927 xpsr_write(env
, val
, 0x0600fc00);
1930 if (env
->v7m
.current_sp
)
1931 env
->v7m
.other_sp
= val
;
1933 env
->regs
[13] = val
;
1936 if (env
->v7m
.current_sp
)
1937 env
->regs
[13] = val
;
1939 env
->v7m
.other_sp
= val
;
1941 case 16: /* PRIMASK */
1943 env
->uncached_cpsr
|= CPSR_I
;
1945 env
->uncached_cpsr
&= ~CPSR_I
;
1947 case 17: /* FAULTMASK */
1949 env
->uncached_cpsr
|= CPSR_F
;
1951 env
->uncached_cpsr
&= ~CPSR_F
;
1953 case 18: /* BASEPRI */
1954 env
->v7m
.basepri
= val
& 0xff;
1956 case 19: /* BASEPRI_MAX */
1958 if (val
!= 0 && (val
< env
->v7m
.basepri
|| env
->v7m
.basepri
== 0))
1959 env
->v7m
.basepri
= val
;
1961 case 20: /* CONTROL */
1962 env
->v7m
.control
= val
& 3;
1963 switch_v7m_sp(env
, (val
& 2) != 0);
1966 /* ??? For debugging only. */
1967 cpu_abort(env
, "Unimplemented system register write (%d)\n", reg
);
1972 void cpu_arm_set_cp_io(CPUARMState
*env
, int cpnum
,
1973 ARMReadCPFunc
*cp_read
, ARMWriteCPFunc
*cp_write
,
1976 if (cpnum
< 0 || cpnum
> 14) {
1977 cpu_abort(env
, "Bad coprocessor number: %i\n", cpnum
);
1981 env
->cp
[cpnum
].cp_read
= cp_read
;
1982 env
->cp
[cpnum
].cp_write
= cp_write
;
1983 env
->cp
[cpnum
].opaque
= opaque
;
1988 /* Note that signed overflow is undefined in C. The following routines are
1989 careful to use unsigned types where modulo arithmetic is required.
1990 Failure to do so _will_ break on newer gcc. */
1992 /* Signed saturating arithmetic. */
1994 /* Perform 16-bit signed saturating addition. */
1995 static inline uint16_t add16_sat(uint16_t a
, uint16_t b
)
2000 if (((res
^ a
) & 0x8000) && !((a
^ b
) & 0x8000)) {
2009 /* Perform 8-bit signed saturating addition. */
2010 static inline uint8_t add8_sat(uint8_t a
, uint8_t b
)
2015 if (((res
^ a
) & 0x80) && !((a
^ b
) & 0x80)) {
2024 /* Perform 16-bit signed saturating subtraction. */
2025 static inline uint16_t sub16_sat(uint16_t a
, uint16_t b
)
2030 if (((res
^ a
) & 0x8000) && ((a
^ b
) & 0x8000)) {
2039 /* Perform 8-bit signed saturating subtraction. */
2040 static inline uint8_t sub8_sat(uint8_t a
, uint8_t b
)
2045 if (((res
^ a
) & 0x80) && ((a
^ b
) & 0x80)) {
2054 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2055 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2056 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2057 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2060 #include "op_addsub.h"
2062 /* Unsigned saturating arithmetic. */
2063 static inline uint16_t add16_usat(uint16_t a
, uint16_t b
)
2072 static inline uint16_t sub16_usat(uint16_t a
, uint16_t b
)
2080 static inline uint8_t add8_usat(uint8_t a
, uint8_t b
)
2089 static inline uint8_t sub8_usat(uint8_t a
, uint8_t b
)
2097 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2098 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2099 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2100 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2103 #include "op_addsub.h"
2105 /* Signed modulo arithmetic. */
2106 #define SARITH16(a, b, n, op) do { \
2108 sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2109 RESULT(sum, n, 16); \
2111 ge |= 3 << (n * 2); \
2114 #define SARITH8(a, b, n, op) do { \
2116 sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2117 RESULT(sum, n, 8); \
2123 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2124 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2125 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2126 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2130 #include "op_addsub.h"
2132 /* Unsigned modulo arithmetic. */
2133 #define ADD16(a, b, n) do { \
2135 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2136 RESULT(sum, n, 16); \
2137 if ((sum >> 16) == 1) \
2138 ge |= 3 << (n * 2); \
2141 #define ADD8(a, b, n) do { \
2143 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2144 RESULT(sum, n, 8); \
2145 if ((sum >> 8) == 1) \
2149 #define SUB16(a, b, n) do { \
2151 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2152 RESULT(sum, n, 16); \
2153 if ((sum >> 16) == 0) \
2154 ge |= 3 << (n * 2); \
2157 #define SUB8(a, b, n) do { \
2159 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2160 RESULT(sum, n, 8); \
2161 if ((sum >> 8) == 0) \
2168 #include "op_addsub.h"
2170 /* Halved signed arithmetic. */
2171 #define ADD16(a, b, n) \
2172 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2173 #define SUB16(a, b, n) \
2174 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2175 #define ADD8(a, b, n) \
2176 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2177 #define SUB8(a, b, n) \
2178 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2181 #include "op_addsub.h"
2183 /* Halved unsigned arithmetic. */
2184 #define ADD16(a, b, n) \
2185 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2186 #define SUB16(a, b, n) \
2187 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2188 #define ADD8(a, b, n) \
2189 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2190 #define SUB8(a, b, n) \
2191 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2194 #include "op_addsub.h"
2196 static inline uint8_t do_usad(uint8_t a
, uint8_t b
)
2204 /* Unsigned sum of absolute byte differences. */
2205 uint32_t HELPER(usad8
)(uint32_t a
, uint32_t b
)
2208 sum
= do_usad(a
, b
);
2209 sum
+= do_usad(a
>> 8, b
>> 8);
2210 sum
+= do_usad(a
>> 16, b
>>16);
2211 sum
+= do_usad(a
>> 24, b
>> 24);
2215 /* For ARMv6 SEL instruction. */
2216 uint32_t HELPER(sel_flags
)(uint32_t flags
, uint32_t a
, uint32_t b
)
2229 return (a
& mask
) | (b
& ~mask
);
2232 uint32_t HELPER(logicq_cc
)(uint64_t val
)
2234 return (val
>> 32) | (val
!= 0);
2237 /* VFP support. We follow the convention used for VFP instrunctions:
2238 Single precition routines have a "s" suffix, double precision a
2241 /* Convert host exception flags to vfp form. */
2242 static inline int vfp_exceptbits_from_host(int host_bits
)
2244 int target_bits
= 0;
2246 if (host_bits
& float_flag_invalid
)
2248 if (host_bits
& float_flag_divbyzero
)
2250 if (host_bits
& float_flag_overflow
)
2252 if (host_bits
& float_flag_underflow
)
2254 if (host_bits
& float_flag_inexact
)
2255 target_bits
|= 0x10;
2256 if (host_bits
& float_flag_input_denormal
)
2257 target_bits
|= 0x80;
2261 uint32_t HELPER(vfp_get_fpscr
)(CPUState
*env
)
2266 fpscr
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & 0xffc8ffff)
2267 | (env
->vfp
.vec_len
<< 16)
2268 | (env
->vfp
.vec_stride
<< 20);
2269 i
= get_float_exception_flags(&env
->vfp
.fp_status
);
2270 i
|= get_float_exception_flags(&env
->vfp
.standard_fp_status
);
2271 fpscr
|= vfp_exceptbits_from_host(i
);
2275 uint32_t vfp_get_fpscr(CPUState
*env
)
2277 return HELPER(vfp_get_fpscr
)(env
);
2280 /* Convert vfp exception flags to target form. */
2281 static inline int vfp_exceptbits_to_host(int target_bits
)
2285 if (target_bits
& 1)
2286 host_bits
|= float_flag_invalid
;
2287 if (target_bits
& 2)
2288 host_bits
|= float_flag_divbyzero
;
2289 if (target_bits
& 4)
2290 host_bits
|= float_flag_overflow
;
2291 if (target_bits
& 8)
2292 host_bits
|= float_flag_underflow
;
2293 if (target_bits
& 0x10)
2294 host_bits
|= float_flag_inexact
;
2295 if (target_bits
& 0x80)
2296 host_bits
|= float_flag_input_denormal
;
2300 void HELPER(vfp_set_fpscr
)(CPUState
*env
, uint32_t val
)
2305 changed
= env
->vfp
.xregs
[ARM_VFP_FPSCR
];
2306 env
->vfp
.xregs
[ARM_VFP_FPSCR
] = (val
& 0xffc8ffff);
2307 env
->vfp
.vec_len
= (val
>> 16) & 7;
2308 env
->vfp
.vec_stride
= (val
>> 20) & 3;
2311 if (changed
& (3 << 22)) {
2312 i
= (val
>> 22) & 3;
2315 i
= float_round_nearest_even
;
2321 i
= float_round_down
;
2324 i
= float_round_to_zero
;
2327 set_float_rounding_mode(i
, &env
->vfp
.fp_status
);
2329 if (changed
& (1 << 24)) {
2330 set_flush_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
2331 set_flush_inputs_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
2333 if (changed
& (1 << 25))
2334 set_default_nan_mode((val
& (1 << 25)) != 0, &env
->vfp
.fp_status
);
2336 i
= vfp_exceptbits_to_host(val
);
2337 set_float_exception_flags(i
, &env
->vfp
.fp_status
);
2338 set_float_exception_flags(0, &env
->vfp
.standard_fp_status
);
2341 void vfp_set_fpscr(CPUState
*env
, uint32_t val
)
2343 HELPER(vfp_set_fpscr
)(env
, val
);
2346 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2348 #define VFP_BINOP(name) \
2349 float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2351 return float32_ ## name (a, b, &env->vfp.fp_status); \
2353 float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2355 return float64_ ## name (a, b, &env->vfp.fp_status); \
2363 float32
VFP_HELPER(neg
, s
)(float32 a
)
2365 return float32_chs(a
);
2368 float64
VFP_HELPER(neg
, d
)(float64 a
)
2370 return float64_chs(a
);
2373 float32
VFP_HELPER(abs
, s
)(float32 a
)
2375 return float32_abs(a
);
2378 float64
VFP_HELPER(abs
, d
)(float64 a
)
2380 return float64_abs(a
);
2383 float32
VFP_HELPER(sqrt
, s
)(float32 a
, CPUState
*env
)
2385 return float32_sqrt(a
, &env
->vfp
.fp_status
);
2388 float64
VFP_HELPER(sqrt
, d
)(float64 a
, CPUState
*env
)
2390 return float64_sqrt(a
, &env
->vfp
.fp_status
);
2393 /* XXX: check quiet/signaling case */
2394 #define DO_VFP_cmp(p, type) \
2395 void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2398 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2399 case 0: flags = 0x6; break; \
2400 case -1: flags = 0x8; break; \
2401 case 1: flags = 0x2; break; \
2402 default: case 2: flags = 0x3; break; \
2404 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2405 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2407 void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2410 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2411 case 0: flags = 0x6; break; \
2412 case -1: flags = 0x8; break; \
2413 case 1: flags = 0x2; break; \
2414 default: case 2: flags = 0x3; break; \
2416 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2417 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2419 DO_VFP_cmp(s
, float32
)
2420 DO_VFP_cmp(d
, float64
)
2423 /* Helper routines to perform bitwise copies between float and int. */
2424 static inline float32
vfp_itos(uint32_t i
)
2435 static inline uint32_t vfp_stoi(float32 s
)
2446 static inline float64
vfp_itod(uint64_t i
)
2457 static inline uint64_t vfp_dtoi(float64 d
)
2468 /* Integer to float conversion. */
2469 float32
VFP_HELPER(uito
, s
)(float32 x
, CPUState
*env
)
2471 return uint32_to_float32(vfp_stoi(x
), &env
->vfp
.fp_status
);
2474 float64
VFP_HELPER(uito
, d
)(float32 x
, CPUState
*env
)
2476 return uint32_to_float64(vfp_stoi(x
), &env
->vfp
.fp_status
);
2479 float32
VFP_HELPER(sito
, s
)(float32 x
, CPUState
*env
)
2481 return int32_to_float32(vfp_stoi(x
), &env
->vfp
.fp_status
);
2484 float64
VFP_HELPER(sito
, d
)(float32 x
, CPUState
*env
)
2486 return int32_to_float64(vfp_stoi(x
), &env
->vfp
.fp_status
);
2489 /* Float to integer conversion. */
2490 float32
VFP_HELPER(toui
, s
)(float32 x
, CPUState
*env
)
2492 if (float32_is_any_nan(x
)) {
2493 return float32_zero
;
2495 return vfp_itos(float32_to_uint32(x
, &env
->vfp
.fp_status
));
2498 float32
VFP_HELPER(toui
, d
)(float64 x
, CPUState
*env
)
2500 if (float64_is_any_nan(x
)) {
2501 return float32_zero
;
2503 return vfp_itos(float64_to_uint32(x
, &env
->vfp
.fp_status
));
2506 float32
VFP_HELPER(tosi
, s
)(float32 x
, CPUState
*env
)
2508 if (float32_is_any_nan(x
)) {
2509 return float32_zero
;
2511 return vfp_itos(float32_to_int32(x
, &env
->vfp
.fp_status
));
2514 float32
VFP_HELPER(tosi
, d
)(float64 x
, CPUState
*env
)
2516 if (float64_is_any_nan(x
)) {
2517 return float32_zero
;
2519 return vfp_itos(float64_to_int32(x
, &env
->vfp
.fp_status
));
2522 float32
VFP_HELPER(touiz
, s
)(float32 x
, CPUState
*env
)
2524 if (float32_is_any_nan(x
)) {
2525 return float32_zero
;
2527 return vfp_itos(float32_to_uint32_round_to_zero(x
, &env
->vfp
.fp_status
));
2530 float32
VFP_HELPER(touiz
, d
)(float64 x
, CPUState
*env
)
2532 if (float64_is_any_nan(x
)) {
2533 return float32_zero
;
2535 return vfp_itos(float64_to_uint32_round_to_zero(x
, &env
->vfp
.fp_status
));
2538 float32
VFP_HELPER(tosiz
, s
)(float32 x
, CPUState
*env
)
2540 if (float32_is_any_nan(x
)) {
2541 return float32_zero
;
2543 return vfp_itos(float32_to_int32_round_to_zero(x
, &env
->vfp
.fp_status
));
2546 float32
VFP_HELPER(tosiz
, d
)(float64 x
, CPUState
*env
)
2548 if (float64_is_any_nan(x
)) {
2549 return float32_zero
;
2551 return vfp_itos(float64_to_int32_round_to_zero(x
, &env
->vfp
.fp_status
));
2554 /* floating point conversion */
2555 float64
VFP_HELPER(fcvtd
, s
)(float32 x
, CPUState
*env
)
2557 float64 r
= float32_to_float64(x
, &env
->vfp
.fp_status
);
2558 /* ARM requires that S<->D conversion of any kind of NaN generates
2559 * a quiet NaN by forcing the most significant frac bit to 1.
2561 return float64_maybe_silence_nan(r
);
2564 float32
VFP_HELPER(fcvts
, d
)(float64 x
, CPUState
*env
)
2566 float32 r
= float64_to_float32(x
, &env
->vfp
.fp_status
);
2567 /* ARM requires that S<->D conversion of any kind of NaN generates
2568 * a quiet NaN by forcing the most significant frac bit to 1.
2570 return float32_maybe_silence_nan(r
);
2573 /* VFP3 fixed point conversion. */
2574 #define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2575 ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2578 tmp = sign##int32_to_##ftype ((itype##_t)vfp_##p##toi(x), \
2579 &env->vfp.fp_status); \
2580 return ftype##_scalbn(tmp, -(int)shift, &env->vfp.fp_status); \
2582 ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2585 if (ftype##_is_any_nan(x)) { \
2586 return ftype##_zero; \
2588 tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2589 return vfp_ito##p(ftype##_to_##itype##_round_to_zero(tmp, \
2590 &env->vfp.fp_status)); \
2593 VFP_CONV_FIX(sh
, d
, float64
, int16
, )
2594 VFP_CONV_FIX(sl
, d
, float64
, int32
, )
2595 VFP_CONV_FIX(uh
, d
, float64
, uint16
, u
)
2596 VFP_CONV_FIX(ul
, d
, float64
, uint32
, u
)
2597 VFP_CONV_FIX(sh
, s
, float32
, int16
, )
2598 VFP_CONV_FIX(sl
, s
, float32
, int32
, )
2599 VFP_CONV_FIX(uh
, s
, float32
, uint16
, u
)
2600 VFP_CONV_FIX(ul
, s
, float32
, uint32
, u
)
2603 /* Half precision conversions. */
2604 float32
HELPER(vfp_fcvt_f16_to_f32
)(uint32_t a
, CPUState
*env
)
2606 float_status
*s
= &env
->vfp
.fp_status
;
2607 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2608 return float16_to_float32(a
, ieee
, s
);
2611 uint32_t HELPER(vfp_fcvt_f32_to_f16
)(float32 a
, CPUState
*env
)
2613 float_status
*s
= &env
->vfp
.fp_status
;
2614 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2615 return float32_to_float16(a
, ieee
, s
);
2618 float32
HELPER(recps_f32
)(float32 a
, float32 b
, CPUState
*env
)
2620 float_status
*s
= &env
->vfp
.fp_status
;
2621 float32 two
= int32_to_float32(2, s
);
2622 return float32_sub(two
, float32_mul(a
, b
, s
), s
);
2625 float32
HELPER(rsqrts_f32
)(float32 a
, float32 b
, CPUState
*env
)
2627 float_status
*s
= &env
->vfp
.standard_fp_status
;
2628 float32 two
= int32_to_float32(2, s
);
2629 float32 three
= int32_to_float32(3, s
);
2631 if ((float32_is_infinity(a
) && float32_is_zero_or_denormal(b
)) ||
2632 (float32_is_infinity(b
) && float32_is_zero_or_denormal(a
))) {
2633 product
= float32_zero
;
2635 product
= float32_mul(a
, b
, s
);
2637 return float32_div(float32_sub(three
, product
, s
), two
, s
);
2642 /* TODO: The architecture specifies the value that the estimate functions
2643 should return. We return the exact reciprocal/root instead. */
2644 float32
HELPER(recpe_f32
)(float32 a
, CPUState
*env
)
2646 float_status
*s
= &env
->vfp
.fp_status
;
2647 float32 one
= int32_to_float32(1, s
);
2648 return float32_div(one
, a
, s
);
2651 float32
HELPER(rsqrte_f32
)(float32 a
, CPUState
*env
)
2653 float_status
*s
= &env
->vfp
.fp_status
;
2654 float32 one
= int32_to_float32(1, s
);
2655 return float32_div(one
, float32_sqrt(a
, s
), s
);
2658 uint32_t HELPER(recpe_u32
)(uint32_t a
, CPUState
*env
)
2660 float_status
*s
= &env
->vfp
.fp_status
;
2662 tmp
= int32_to_float32(a
, s
);
2663 tmp
= float32_scalbn(tmp
, -32, s
);
2664 tmp
= helper_recpe_f32(tmp
, env
);
2665 tmp
= float32_scalbn(tmp
, 31, s
);
2666 return float32_to_int32(tmp
, s
);
2669 uint32_t HELPER(rsqrte_u32
)(uint32_t a
, CPUState
*env
)
2671 float_status
*s
= &env
->vfp
.fp_status
;
2673 tmp
= int32_to_float32(a
, s
);
2674 tmp
= float32_scalbn(tmp
, -32, s
);
2675 tmp
= helper_rsqrte_f32(tmp
, env
);
2676 tmp
= float32_scalbn(tmp
, 31, s
);
2677 return float32_to_int32(tmp
, s
);
2680 void HELPER(set_teecr
)(CPUState
*env
, uint32_t val
)
2683 if (env
->teecr
!= val
) {