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
;
243 static int vfp_gdb_get_reg(CPUState
*env
, uint8_t *buf
, int reg
)
247 /* VFP data registers are always little-endian. */
248 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
250 stfq_le_p(buf
, env
->vfp
.regs
[reg
]);
253 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
254 /* Aliases for Q regs. */
257 stfq_le_p(buf
, env
->vfp
.regs
[(reg
- 32) * 2]);
258 stfq_le_p(buf
+ 8, env
->vfp
.regs
[(reg
- 32) * 2 + 1]);
262 switch (reg
- nregs
) {
263 case 0: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]); return 4;
264 case 1: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSCR
]); return 4;
265 case 2: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]); return 4;
270 static int vfp_gdb_set_reg(CPUState
*env
, uint8_t *buf
, int reg
)
274 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
276 env
->vfp
.regs
[reg
] = ldfq_le_p(buf
);
279 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
282 env
->vfp
.regs
[(reg
- 32) * 2] = ldfq_le_p(buf
);
283 env
->vfp
.regs
[(reg
- 32) * 2 + 1] = ldfq_le_p(buf
+ 8);
287 switch (reg
- nregs
) {
288 case 0: env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
); return 4;
289 case 1: env
->vfp
.xregs
[ARM_VFP_FPSCR
] = ldl_p(buf
); return 4;
290 case 2: env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30); return 4;
295 CPUARMState
*cpu_arm_init(const char *cpu_model
)
299 static int inited
= 0;
301 id
= cpu_arm_find_by_name(cpu_model
);
304 env
= qemu_mallocz(sizeof(CPUARMState
));
308 arm_translate_init();
311 env
->cpu_model_str
= cpu_model
;
312 env
->cp15
.c0_cpuid
= id
;
314 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
315 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
316 51, "arm-neon.xml", 0);
317 } else if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
318 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
319 35, "arm-vfp3.xml", 0);
320 } else if (arm_feature(env
, ARM_FEATURE_VFP
)) {
321 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
322 19, "arm-vfp.xml", 0);
333 static const struct arm_cpu_t arm_cpu_names
[] = {
334 { ARM_CPUID_ARM926
, "arm926"},
335 { ARM_CPUID_ARM946
, "arm946"},
336 { ARM_CPUID_ARM1026
, "arm1026"},
337 { ARM_CPUID_ARM1136
, "arm1136"},
338 { ARM_CPUID_ARM1136_R2
, "arm1136-r2"},
339 { ARM_CPUID_ARM11MPCORE
, "arm11mpcore"},
340 { ARM_CPUID_CORTEXM3
, "cortex-m3"},
341 { ARM_CPUID_CORTEXA8
, "cortex-a8"},
342 { ARM_CPUID_CORTEXA9
, "cortex-a9"},
343 { ARM_CPUID_TI925T
, "ti925t" },
344 { ARM_CPUID_PXA250
, "pxa250" },
345 { ARM_CPUID_PXA255
, "pxa255" },
346 { ARM_CPUID_PXA260
, "pxa260" },
347 { ARM_CPUID_PXA261
, "pxa261" },
348 { ARM_CPUID_PXA262
, "pxa262" },
349 { ARM_CPUID_PXA270
, "pxa270" },
350 { ARM_CPUID_PXA270_A0
, "pxa270-a0" },
351 { ARM_CPUID_PXA270_A1
, "pxa270-a1" },
352 { ARM_CPUID_PXA270_B0
, "pxa270-b0" },
353 { ARM_CPUID_PXA270_B1
, "pxa270-b1" },
354 { ARM_CPUID_PXA270_C0
, "pxa270-c0" },
355 { ARM_CPUID_PXA270_C5
, "pxa270-c5" },
356 { ARM_CPUID_ANY
, "any"},
360 void arm_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
364 (*cpu_fprintf
)(f
, "Available CPUs:\n");
365 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
366 (*cpu_fprintf
)(f
, " %s\n", arm_cpu_names
[i
].name
);
370 /* return 0 if not found */
371 static uint32_t cpu_arm_find_by_name(const char *name
)
377 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
378 if (strcmp(name
, arm_cpu_names
[i
].name
) == 0) {
379 id
= arm_cpu_names
[i
].id
;
386 void cpu_arm_close(CPUARMState
*env
)
391 uint32_t cpsr_read(CPUARMState
*env
)
395 return env
->uncached_cpsr
| (env
->NF
& 0x80000000) | (ZF
<< 30) |
396 (env
->CF
<< 29) | ((env
->VF
& 0x80000000) >> 3) | (env
->QF
<< 27)
397 | (env
->thumb
<< 5) | ((env
->condexec_bits
& 3) << 25)
398 | ((env
->condexec_bits
& 0xfc) << 8)
402 void cpsr_write(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
404 if (mask
& CPSR_NZCV
) {
405 env
->ZF
= (~val
) & CPSR_Z
;
407 env
->CF
= (val
>> 29) & 1;
408 env
->VF
= (val
<< 3) & 0x80000000;
411 env
->QF
= ((val
& CPSR_Q
) != 0);
413 env
->thumb
= ((val
& CPSR_T
) != 0);
414 if (mask
& CPSR_IT_0_1
) {
415 env
->condexec_bits
&= ~3;
416 env
->condexec_bits
|= (val
>> 25) & 3;
418 if (mask
& CPSR_IT_2_7
) {
419 env
->condexec_bits
&= 3;
420 env
->condexec_bits
|= (val
>> 8) & 0xfc;
422 if (mask
& CPSR_GE
) {
423 env
->GE
= (val
>> 16) & 0xf;
426 if ((env
->uncached_cpsr
^ val
) & mask
& CPSR_M
) {
427 switch_mode(env
, val
& CPSR_M
);
429 mask
&= ~CACHED_CPSR_BITS
;
430 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~mask
) | (val
& mask
);
433 /* Sign/zero extend */
434 uint32_t HELPER(sxtb16
)(uint32_t x
)
437 res
= (uint16_t)(int8_t)x
;
438 res
|= (uint32_t)(int8_t)(x
>> 16) << 16;
442 uint32_t HELPER(uxtb16
)(uint32_t x
)
445 res
= (uint16_t)(uint8_t)x
;
446 res
|= (uint32_t)(uint8_t)(x
>> 16) << 16;
450 uint32_t HELPER(clz
)(uint32_t x
)
455 int32_t HELPER(sdiv
)(int32_t num
, int32_t den
)
459 if (num
== INT_MIN
&& den
== -1)
464 uint32_t HELPER(udiv
)(uint32_t num
, uint32_t den
)
471 uint32_t HELPER(rbit
)(uint32_t x
)
473 x
= ((x
& 0xff000000) >> 24)
474 | ((x
& 0x00ff0000) >> 8)
475 | ((x
& 0x0000ff00) << 8)
476 | ((x
& 0x000000ff) << 24);
477 x
= ((x
& 0xf0f0f0f0) >> 4)
478 | ((x
& 0x0f0f0f0f) << 4);
479 x
= ((x
& 0x88888888) >> 3)
480 | ((x
& 0x44444444) >> 1)
481 | ((x
& 0x22222222) << 1)
482 | ((x
& 0x11111111) << 3);
486 uint32_t HELPER(abs
)(uint32_t x
)
488 return ((int32_t)x
< 0) ? -x
: x
;
491 #if defined(CONFIG_USER_ONLY)
493 void do_interrupt (CPUState
*env
)
495 env
->exception_index
= -1;
498 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
499 int mmu_idx
, int is_softmmu
)
502 env
->exception_index
= EXCP_PREFETCH_ABORT
;
503 env
->cp15
.c6_insn
= address
;
505 env
->exception_index
= EXCP_DATA_ABORT
;
506 env
->cp15
.c6_data
= address
;
511 /* These should probably raise undefined insn exceptions. */
512 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
514 int op1
= (insn
>> 8) & 0xf;
515 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
519 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
521 int op1
= (insn
>> 8) & 0xf;
522 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
526 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
528 cpu_abort(env
, "cp15 insn %08x\n", insn
);
531 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
533 cpu_abort(env
, "cp15 insn %08x\n", insn
);
536 /* These should probably raise undefined insn exceptions. */
537 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
539 cpu_abort(env
, "v7m_mrs %d\n", reg
);
542 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
544 cpu_abort(env
, "v7m_mrs %d\n", reg
);
548 void switch_mode(CPUState
*env
, int mode
)
550 if (mode
!= ARM_CPU_MODE_USR
)
551 cpu_abort(env
, "Tried to switch out of user mode\n");
554 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
556 cpu_abort(env
, "banked r13 write\n");
559 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
561 cpu_abort(env
, "banked r13 read\n");
567 extern int semihosting_enabled
;
569 /* Map CPU modes onto saved register banks. */
570 static inline int bank_number (int mode
)
573 case ARM_CPU_MODE_USR
:
574 case ARM_CPU_MODE_SYS
:
576 case ARM_CPU_MODE_SVC
:
578 case ARM_CPU_MODE_ABT
:
580 case ARM_CPU_MODE_UND
:
582 case ARM_CPU_MODE_IRQ
:
584 case ARM_CPU_MODE_FIQ
:
587 cpu_abort(cpu_single_env
, "Bad mode %x\n", mode
);
591 void switch_mode(CPUState
*env
, int mode
)
596 old_mode
= env
->uncached_cpsr
& CPSR_M
;
597 if (mode
== old_mode
)
600 if (old_mode
== ARM_CPU_MODE_FIQ
) {
601 memcpy (env
->fiq_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
602 memcpy (env
->regs
+ 8, env
->usr_regs
, 5 * sizeof(uint32_t));
603 } else if (mode
== ARM_CPU_MODE_FIQ
) {
604 memcpy (env
->usr_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
605 memcpy (env
->regs
+ 8, env
->fiq_regs
, 5 * sizeof(uint32_t));
608 i
= bank_number(old_mode
);
609 env
->banked_r13
[i
] = env
->regs
[13];
610 env
->banked_r14
[i
] = env
->regs
[14];
611 env
->banked_spsr
[i
] = env
->spsr
;
613 i
= bank_number(mode
);
614 env
->regs
[13] = env
->banked_r13
[i
];
615 env
->regs
[14] = env
->banked_r14
[i
];
616 env
->spsr
= env
->banked_spsr
[i
];
619 static void v7m_push(CPUARMState
*env
, uint32_t val
)
622 stl_phys(env
->regs
[13], val
);
625 static uint32_t v7m_pop(CPUARMState
*env
)
628 val
= ldl_phys(env
->regs
[13]);
633 /* Switch to V7M main or process stack pointer. */
634 static void switch_v7m_sp(CPUARMState
*env
, int process
)
637 if (env
->v7m
.current_sp
!= process
) {
638 tmp
= env
->v7m
.other_sp
;
639 env
->v7m
.other_sp
= env
->regs
[13];
641 env
->v7m
.current_sp
= process
;
645 static void do_v7m_exception_exit(CPUARMState
*env
)
650 type
= env
->regs
[15];
651 if (env
->v7m
.exception
!= 0)
652 armv7m_nvic_complete_irq(env
->nvic
, env
->v7m
.exception
);
654 /* Switch to the target stack. */
655 switch_v7m_sp(env
, (type
& 4) != 0);
657 env
->regs
[0] = v7m_pop(env
);
658 env
->regs
[1] = v7m_pop(env
);
659 env
->regs
[2] = v7m_pop(env
);
660 env
->regs
[3] = v7m_pop(env
);
661 env
->regs
[12] = v7m_pop(env
);
662 env
->regs
[14] = v7m_pop(env
);
663 env
->regs
[15] = v7m_pop(env
);
665 xpsr_write(env
, xpsr
, 0xfffffdff);
666 /* Undo stack alignment. */
669 /* ??? The exception return type specifies Thread/Handler mode. However
670 this is also implied by the xPSR value. Not sure what to do
671 if there is a mismatch. */
672 /* ??? Likewise for mismatches between the CONTROL register and the stack
676 static void do_interrupt_v7m(CPUARMState
*env
)
678 uint32_t xpsr
= xpsr_read(env
);
683 if (env
->v7m
.current_sp
)
685 if (env
->v7m
.exception
== 0)
688 /* For exceptions we just mark as pending on the NVIC, and let that
690 /* TODO: Need to escalate if the current priority is higher than the
691 one we're raising. */
692 switch (env
->exception_index
) {
694 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
);
698 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SVC
);
700 case EXCP_PREFETCH_ABORT
:
701 case EXCP_DATA_ABORT
:
702 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
);
705 if (semihosting_enabled
) {
707 nr
= lduw_code(env
->regs
[15]) & 0xff;
710 env
->regs
[0] = do_arm_semihosting(env
);
714 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_DEBUG
);
717 env
->v7m
.exception
= armv7m_nvic_acknowledge_irq(env
->nvic
);
719 case EXCP_EXCEPTION_EXIT
:
720 do_v7m_exception_exit(env
);
723 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
724 return; /* Never happens. Keep compiler happy. */
727 /* Align stack pointer. */
728 /* ??? Should only do this if Configuration Control Register
729 STACKALIGN bit is set. */
730 if (env
->regs
[13] & 4) {
734 /* Switch to the handler mode. */
736 v7m_push(env
, env
->regs
[15]);
737 v7m_push(env
, env
->regs
[14]);
738 v7m_push(env
, env
->regs
[12]);
739 v7m_push(env
, env
->regs
[3]);
740 v7m_push(env
, env
->regs
[2]);
741 v7m_push(env
, env
->regs
[1]);
742 v7m_push(env
, env
->regs
[0]);
743 switch_v7m_sp(env
, 0);
744 env
->uncached_cpsr
&= ~CPSR_IT
;
746 addr
= ldl_phys(env
->v7m
.vecbase
+ env
->v7m
.exception
* 4);
747 env
->regs
[15] = addr
& 0xfffffffe;
748 env
->thumb
= addr
& 1;
751 /* Handle a CPU exception. */
752 void do_interrupt(CPUARMState
*env
)
760 do_interrupt_v7m(env
);
763 /* TODO: Vectored interrupt controller. */
764 switch (env
->exception_index
) {
766 new_mode
= ARM_CPU_MODE_UND
;
775 if (semihosting_enabled
) {
776 /* Check for semihosting interrupt. */
778 mask
= lduw_code(env
->regs
[15] - 2) & 0xff;
780 mask
= ldl_code(env
->regs
[15] - 4) & 0xffffff;
782 /* Only intercept calls from privileged modes, to provide some
783 semblance of security. */
784 if (((mask
== 0x123456 && !env
->thumb
)
785 || (mask
== 0xab && env
->thumb
))
786 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
787 env
->regs
[0] = do_arm_semihosting(env
);
791 new_mode
= ARM_CPU_MODE_SVC
;
794 /* The PC already points to the next instruction. */
798 /* See if this is a semihosting syscall. */
799 if (env
->thumb
&& semihosting_enabled
) {
800 mask
= lduw_code(env
->regs
[15]) & 0xff;
802 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
804 env
->regs
[0] = do_arm_semihosting(env
);
808 /* Fall through to prefetch abort. */
809 case EXCP_PREFETCH_ABORT
:
810 new_mode
= ARM_CPU_MODE_ABT
;
812 mask
= CPSR_A
| CPSR_I
;
815 case EXCP_DATA_ABORT
:
816 new_mode
= ARM_CPU_MODE_ABT
;
818 mask
= CPSR_A
| CPSR_I
;
822 new_mode
= ARM_CPU_MODE_IRQ
;
824 /* Disable IRQ and imprecise data aborts. */
825 mask
= CPSR_A
| CPSR_I
;
829 new_mode
= ARM_CPU_MODE_FIQ
;
831 /* Disable FIQ, IRQ and imprecise data aborts. */
832 mask
= CPSR_A
| CPSR_I
| CPSR_F
;
836 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
837 return; /* Never happens. Keep compiler happy. */
840 if (env
->cp15
.c1_sys
& (1 << 13)) {
843 switch_mode (env
, new_mode
);
844 env
->spsr
= cpsr_read(env
);
846 env
->condexec_bits
= 0;
847 /* Switch to the new mode, and to the correct instruction set. */
848 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~CPSR_M
) | new_mode
;
849 env
->uncached_cpsr
|= mask
;
850 env
->thumb
= (env
->cp15
.c1_sys
& (1 << 30)) != 0;
851 env
->regs
[14] = env
->regs
[15] + offset
;
852 env
->regs
[15] = addr
;
853 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
856 /* Check section/page access permissions.
857 Returns the page protection flags, or zero if the access is not
859 static inline int check_ap(CPUState
*env
, int ap
, int domain
, int access_type
,
865 return PAGE_READ
| PAGE_WRITE
;
867 if (access_type
== 1)
874 if (access_type
== 1)
876 switch ((env
->cp15
.c1_sys
>> 8) & 3) {
878 return is_user
? 0 : PAGE_READ
;
885 return is_user
? 0 : PAGE_READ
| PAGE_WRITE
;
890 return PAGE_READ
| PAGE_WRITE
;
892 return PAGE_READ
| PAGE_WRITE
;
893 case 4: /* Reserved. */
896 return is_user
? 0 : prot_ro
;
900 if (!arm_feature (env
, ARM_FEATURE_V7
))
908 static uint32_t get_level1_table_address(CPUState
*env
, uint32_t address
)
912 if (address
& env
->cp15
.c2_mask
)
913 table
= env
->cp15
.c2_base1
& 0xffffc000;
915 table
= env
->cp15
.c2_base0
& env
->cp15
.c2_base_mask
;
917 table
|= (address
>> 18) & 0x3ffc;
921 static int get_phys_addr_v5(CPUState
*env
, uint32_t address
, int access_type
,
922 int is_user
, uint32_t *phys_ptr
, int *prot
,
923 target_ulong
*page_size
)
933 /* Pagetable walk. */
934 /* Lookup l1 descriptor. */
935 table
= get_level1_table_address(env
, address
);
936 desc
= ldl_phys(table
);
938 domain
= (env
->cp15
.c3
>> ((desc
>> 4) & 0x1e)) & 3;
940 /* Section translation fault. */
944 if (domain
== 0 || domain
== 2) {
946 code
= 9; /* Section domain fault. */
948 code
= 11; /* Page domain fault. */
953 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
954 ap
= (desc
>> 10) & 3;
956 *page_size
= 1024 * 1024;
958 /* Lookup l2 entry. */
960 /* Coarse pagetable. */
961 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
963 /* Fine pagetable. */
964 table
= (desc
& 0xfffff000) | ((address
>> 8) & 0xffc);
966 desc
= ldl_phys(table
);
968 case 0: /* Page translation fault. */
971 case 1: /* 64k page. */
972 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
973 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
974 *page_size
= 0x10000;
976 case 2: /* 4k page. */
977 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
978 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
981 case 3: /* 1k page. */
983 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
984 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
986 /* Page translation fault. */
991 phys_addr
= (desc
& 0xfffffc00) | (address
& 0x3ff);
993 ap
= (desc
>> 4) & 3;
997 /* Never happens, but compiler isn't smart enough to tell. */
1002 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1004 /* Access permission fault. */
1008 *phys_ptr
= phys_addr
;
1011 return code
| (domain
<< 4);
1014 static int get_phys_addr_v6(CPUState
*env
, uint32_t address
, int access_type
,
1015 int is_user
, uint32_t *phys_ptr
, int *prot
,
1016 target_ulong
*page_size
)
1027 /* Pagetable walk. */
1028 /* Lookup l1 descriptor. */
1029 table
= get_level1_table_address(env
, address
);
1030 desc
= ldl_phys(table
);
1033 /* Section translation fault. */
1037 } else if (type
== 2 && (desc
& (1 << 18))) {
1041 /* Section or page. */
1042 domain
= (desc
>> 4) & 0x1e;
1044 domain
= (env
->cp15
.c3
>> domain
) & 3;
1045 if (domain
== 0 || domain
== 2) {
1047 code
= 9; /* Section domain fault. */
1049 code
= 11; /* Page domain fault. */
1053 if (desc
& (1 << 18)) {
1055 phys_addr
= (desc
& 0xff000000) | (address
& 0x00ffffff);
1056 *page_size
= 0x1000000;
1059 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
1060 *page_size
= 0x100000;
1062 ap
= ((desc
>> 10) & 3) | ((desc
>> 13) & 4);
1063 xn
= desc
& (1 << 4);
1066 /* Lookup l2 entry. */
1067 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
1068 desc
= ldl_phys(table
);
1069 ap
= ((desc
>> 4) & 3) | ((desc
>> 7) & 4);
1071 case 0: /* Page translation fault. */
1074 case 1: /* 64k page. */
1075 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
1076 xn
= desc
& (1 << 15);
1077 *page_size
= 0x10000;
1079 case 2: case 3: /* 4k page. */
1080 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
1082 *page_size
= 0x1000;
1085 /* Never happens, but compiler isn't smart enough to tell. */
1091 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1093 if (xn
&& access_type
== 2)
1096 /* The simplified model uses AP[0] as an access control bit. */
1097 if ((env
->cp15
.c1_sys
& (1 << 29)) && (ap
& 1) == 0) {
1098 /* Access flag fault. */
1099 code
= (code
== 15) ? 6 : 3;
1102 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1104 /* Access permission fault. */
1111 *phys_ptr
= phys_addr
;
1114 return code
| (domain
<< 4);
1117 static int get_phys_addr_mpu(CPUState
*env
, uint32_t address
, int access_type
,
1118 int is_user
, uint32_t *phys_ptr
, int *prot
)
1124 *phys_ptr
= address
;
1125 for (n
= 7; n
>= 0; n
--) {
1126 base
= env
->cp15
.c6_region
[n
];
1127 if ((base
& 1) == 0)
1129 mask
= 1 << ((base
>> 1) & 0x1f);
1130 /* Keep this shift separate from the above to avoid an
1131 (undefined) << 32. */
1132 mask
= (mask
<< 1) - 1;
1133 if (((base
^ address
) & ~mask
) == 0)
1139 if (access_type
== 2) {
1140 mask
= env
->cp15
.c5_insn
;
1142 mask
= env
->cp15
.c5_data
;
1144 mask
= (mask
>> (n
* 4)) & 0xf;
1151 *prot
= PAGE_READ
| PAGE_WRITE
;
1156 *prot
|= PAGE_WRITE
;
1159 *prot
= PAGE_READ
| PAGE_WRITE
;
1170 /* Bad permission. */
1177 static inline int get_phys_addr(CPUState
*env
, uint32_t address
,
1178 int access_type
, int is_user
,
1179 uint32_t *phys_ptr
, int *prot
,
1180 target_ulong
*page_size
)
1182 /* Fast Context Switch Extension. */
1183 if (address
< 0x02000000)
1184 address
+= env
->cp15
.c13_fcse
;
1186 if ((env
->cp15
.c1_sys
& 1) == 0) {
1187 /* MMU/MPU disabled. */
1188 *phys_ptr
= address
;
1189 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1190 *page_size
= TARGET_PAGE_SIZE
;
1192 } else if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1193 *page_size
= TARGET_PAGE_SIZE
;
1194 return get_phys_addr_mpu(env
, address
, access_type
, is_user
, phys_ptr
,
1196 } else if (env
->cp15
.c1_sys
& (1 << 23)) {
1197 return get_phys_addr_v6(env
, address
, access_type
, is_user
, phys_ptr
,
1200 return get_phys_addr_v5(env
, address
, access_type
, is_user
, phys_ptr
,
1205 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
,
1206 int access_type
, int mmu_idx
, int is_softmmu
)
1209 target_ulong page_size
;
1213 is_user
= mmu_idx
== MMU_USER_IDX
;
1214 ret
= get_phys_addr(env
, address
, access_type
, is_user
, &phys_addr
, &prot
,
1217 /* Map a single [sub]page. */
1218 phys_addr
&= ~(uint32_t)0x3ff;
1219 address
&= ~(uint32_t)0x3ff;
1220 tlb_set_page (env
, address
, phys_addr
, prot
, mmu_idx
, page_size
);
1224 if (access_type
== 2) {
1225 env
->cp15
.c5_insn
= ret
;
1226 env
->cp15
.c6_insn
= address
;
1227 env
->exception_index
= EXCP_PREFETCH_ABORT
;
1229 env
->cp15
.c5_data
= ret
;
1230 if (access_type
== 1 && arm_feature(env
, ARM_FEATURE_V6
))
1231 env
->cp15
.c5_data
|= (1 << 11);
1232 env
->cp15
.c6_data
= address
;
1233 env
->exception_index
= EXCP_DATA_ABORT
;
1238 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1241 target_ulong page_size
;
1245 ret
= get_phys_addr(env
, addr
, 0, 0, &phys_addr
, &prot
, &page_size
);
1253 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1255 int cp_num
= (insn
>> 8) & 0xf;
1256 int cp_info
= (insn
>> 5) & 7;
1257 int src
= (insn
>> 16) & 0xf;
1258 int operand
= insn
& 0xf;
1260 if (env
->cp
[cp_num
].cp_write
)
1261 env
->cp
[cp_num
].cp_write(env
->cp
[cp_num
].opaque
,
1262 cp_info
, src
, operand
, val
);
1265 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
1267 int cp_num
= (insn
>> 8) & 0xf;
1268 int cp_info
= (insn
>> 5) & 7;
1269 int dest
= (insn
>> 16) & 0xf;
1270 int operand
= insn
& 0xf;
1272 if (env
->cp
[cp_num
].cp_read
)
1273 return env
->cp
[cp_num
].cp_read(env
->cp
[cp_num
].opaque
,
1274 cp_info
, dest
, operand
);
1278 /* Return basic MPU access permission bits. */
1279 static uint32_t simple_mpu_ap_bits(uint32_t val
)
1286 for (i
= 0; i
< 16; i
+= 2) {
1287 ret
|= (val
>> i
) & mask
;
1293 /* Pad basic MPU access permission bits to extended format. */
1294 static uint32_t extended_mpu_ap_bits(uint32_t val
)
1301 for (i
= 0; i
< 16; i
+= 2) {
1302 ret
|= (val
& mask
) << i
;
1308 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1314 op1
= (insn
>> 21) & 7;
1315 op2
= (insn
>> 5) & 7;
1317 switch ((insn
>> 16) & 0xf) {
1320 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1322 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1324 if (arm_feature(env
, ARM_FEATURE_V7
)
1325 && op1
== 2 && crm
== 0 && op2
== 0) {
1326 env
->cp15
.c0_cssel
= val
& 0xf;
1330 case 1: /* System configuration. */
1331 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1335 if (!arm_feature(env
, ARM_FEATURE_XSCALE
) || crm
== 0)
1336 env
->cp15
.c1_sys
= val
;
1337 /* ??? Lots of these bits are not implemented. */
1338 /* This may enable/disable the MMU, so do a TLB flush. */
1341 case 1: /* Auxiliary cotrol register. */
1342 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1343 env
->cp15
.c1_xscaleauxcr
= val
;
1346 /* Not implemented. */
1349 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1351 if (env
->cp15
.c1_coproc
!= val
) {
1352 env
->cp15
.c1_coproc
= val
;
1353 /* ??? Is this safe when called from within a TB? */
1361 case 2: /* MMU Page table control / MPU cache control. */
1362 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1365 env
->cp15
.c2_data
= val
;
1368 env
->cp15
.c2_insn
= val
;
1376 env
->cp15
.c2_base0
= val
;
1379 env
->cp15
.c2_base1
= val
;
1383 env
->cp15
.c2_control
= val
;
1384 env
->cp15
.c2_mask
= ~(((uint32_t)0xffffffffu
) >> val
);
1385 env
->cp15
.c2_base_mask
= ~((uint32_t)0x3fffu
>> val
);
1392 case 3: /* MMU Domain access control / MPU write buffer control. */
1394 tlb_flush(env
, 1); /* Flush TLB as domain not tracked in TLB */
1396 case 4: /* Reserved. */
1398 case 5: /* MMU Fault status / MPU access permission. */
1399 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1403 if (arm_feature(env
, ARM_FEATURE_MPU
))
1404 val
= extended_mpu_ap_bits(val
);
1405 env
->cp15
.c5_data
= val
;
1408 if (arm_feature(env
, ARM_FEATURE_MPU
))
1409 val
= extended_mpu_ap_bits(val
);
1410 env
->cp15
.c5_insn
= val
;
1413 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1415 env
->cp15
.c5_data
= val
;
1418 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1420 env
->cp15
.c5_insn
= val
;
1426 case 6: /* MMU Fault address / MPU base/size. */
1427 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1430 env
->cp15
.c6_region
[crm
] = val
;
1432 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1436 env
->cp15
.c6_data
= val
;
1438 case 1: /* ??? This is WFAR on armv6 */
1440 env
->cp15
.c6_insn
= val
;
1447 case 7: /* Cache control. */
1448 env
->cp15
.c15_i_max
= 0x000;
1449 env
->cp15
.c15_i_min
= 0xff0;
1450 /* No cache, so nothing to do. */
1451 /* ??? MPCore has VA to PA translation functions. */
1453 case 8: /* MMU TLB control. */
1455 case 0: /* Invalidate all. */
1458 case 1: /* Invalidate single TLB entry. */
1459 tlb_flush_page(env
, val
& TARGET_PAGE_MASK
);
1461 case 2: /* Invalidate on ASID. */
1462 tlb_flush(env
, val
== 0);
1464 case 3: /* Invalidate single entry on MVA. */
1465 /* ??? This is like case 1, but ignores ASID. */
1473 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1476 case 0: /* Cache lockdown. */
1478 case 0: /* L1 cache. */
1481 env
->cp15
.c9_data
= val
;
1484 env
->cp15
.c9_insn
= val
;
1490 case 1: /* L2 cache. */
1491 /* Ignore writes to L2 lockdown/auxiliary registers. */
1497 case 1: /* TCM memory region registers. */
1498 /* Not implemented. */
1504 case 10: /* MMU TLB lockdown. */
1505 /* ??? TLB lockdown not implemented. */
1507 case 12: /* Reserved. */
1509 case 13: /* Process ID. */
1512 /* Unlike real hardware the qemu TLB uses virtual addresses,
1513 not modified virtual addresses, so this causes a TLB flush.
1515 if (env
->cp15
.c13_fcse
!= val
)
1517 env
->cp15
.c13_fcse
= val
;
1520 /* This changes the ASID, so do a TLB flush. */
1521 if (env
->cp15
.c13_context
!= val
1522 && !arm_feature(env
, ARM_FEATURE_MPU
))
1524 env
->cp15
.c13_context
= val
;
1530 case 14: /* Reserved. */
1532 case 15: /* Implementation specific. */
1533 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1534 if (op2
== 0 && crm
== 1) {
1535 if (env
->cp15
.c15_cpar
!= (val
& 0x3fff)) {
1536 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1538 env
->cp15
.c15_cpar
= val
& 0x3fff;
1544 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1548 case 1: /* Set TI925T configuration. */
1549 env
->cp15
.c15_ticonfig
= val
& 0xe7;
1550 env
->cp15
.c0_cpuid
= (val
& (1 << 5)) ? /* OS_TYPE bit */
1551 ARM_CPUID_TI915T
: ARM_CPUID_TI925T
;
1553 case 2: /* Set I_max. */
1554 env
->cp15
.c15_i_max
= val
;
1556 case 3: /* Set I_min. */
1557 env
->cp15
.c15_i_min
= val
;
1559 case 4: /* Set thread-ID. */
1560 env
->cp15
.c15_threadid
= val
& 0xffff;
1562 case 8: /* Wait-for-interrupt (deprecated). */
1563 cpu_interrupt(env
, CPU_INTERRUPT_HALT
);
1573 /* ??? For debugging only. Should raise illegal instruction exception. */
1574 cpu_abort(env
, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1575 (insn
>> 16) & 0xf, crm
, op1
, op2
);
1578 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
1584 op1
= (insn
>> 21) & 7;
1585 op2
= (insn
>> 5) & 7;
1587 switch ((insn
>> 16) & 0xf) {
1588 case 0: /* ID codes. */
1594 case 0: /* Device ID. */
1595 return env
->cp15
.c0_cpuid
;
1596 case 1: /* Cache Type. */
1597 return env
->cp15
.c0_cachetype
;
1598 case 2: /* TCM status. */
1600 case 3: /* TLB type register. */
1601 return 0; /* No lockable TLB entries. */
1602 case 5: /* CPU ID */
1603 if (ARM_CPUID(env
) == ARM_CPUID_CORTEXA9
) {
1604 return env
->cpu_index
| 0x80000900;
1606 return env
->cpu_index
;
1612 if (!arm_feature(env
, ARM_FEATURE_V6
))
1614 return env
->cp15
.c0_c1
[op2
];
1616 if (!arm_feature(env
, ARM_FEATURE_V6
))
1618 return env
->cp15
.c0_c2
[op2
];
1619 case 3: case 4: case 5: case 6: case 7:
1625 /* These registers aren't documented on arm11 cores. However
1626 Linux looks at them anyway. */
1627 if (!arm_feature(env
, ARM_FEATURE_V6
))
1631 if (!arm_feature(env
, ARM_FEATURE_V7
))
1636 return env
->cp15
.c0_ccsid
[env
->cp15
.c0_cssel
];
1638 return env
->cp15
.c0_clid
;
1644 if (op2
!= 0 || crm
!= 0)
1646 return env
->cp15
.c0_cssel
;
1650 case 1: /* System configuration. */
1651 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1654 case 0: /* Control register. */
1655 return env
->cp15
.c1_sys
;
1656 case 1: /* Auxiliary control register. */
1657 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1658 return env
->cp15
.c1_xscaleauxcr
;
1659 if (!arm_feature(env
, ARM_FEATURE_AUXCR
))
1661 switch (ARM_CPUID(env
)) {
1662 case ARM_CPUID_ARM1026
:
1664 case ARM_CPUID_ARM1136
:
1665 case ARM_CPUID_ARM1136_R2
:
1667 case ARM_CPUID_ARM11MPCORE
:
1669 case ARM_CPUID_CORTEXA8
:
1671 case ARM_CPUID_CORTEXA9
:
1676 case 2: /* Coprocessor access register. */
1677 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1679 return env
->cp15
.c1_coproc
;
1683 case 2: /* MMU Page table control / MPU cache control. */
1684 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1687 return env
->cp15
.c2_data
;
1690 return env
->cp15
.c2_insn
;
1698 return env
->cp15
.c2_base0
;
1700 return env
->cp15
.c2_base1
;
1702 return env
->cp15
.c2_control
;
1707 case 3: /* MMU Domain access control / MPU write buffer control. */
1708 return env
->cp15
.c3
;
1709 case 4: /* Reserved. */
1711 case 5: /* MMU Fault status / MPU access permission. */
1712 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1716 if (arm_feature(env
, ARM_FEATURE_MPU
))
1717 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1718 return env
->cp15
.c5_data
;
1720 if (arm_feature(env
, ARM_FEATURE_MPU
))
1721 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1722 return env
->cp15
.c5_insn
;
1724 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1726 return env
->cp15
.c5_data
;
1728 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1730 return env
->cp15
.c5_insn
;
1734 case 6: /* MMU Fault address. */
1735 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1738 return env
->cp15
.c6_region
[crm
];
1740 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1744 return env
->cp15
.c6_data
;
1746 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1747 /* Watchpoint Fault Adrress. */
1748 return 0; /* Not implemented. */
1750 /* Instruction Fault Adrress. */
1751 /* Arm9 doesn't have an IFAR, but implementing it anyway
1752 shouldn't do any harm. */
1753 return env
->cp15
.c6_insn
;
1756 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1757 /* Instruction Fault Adrress. */
1758 return env
->cp15
.c6_insn
;
1766 case 7: /* Cache control. */
1767 /* FIXME: Should only clear Z flag if destination is r15. */
1770 case 8: /* MMU TLB control. */
1772 case 9: /* Cache lockdown. */
1774 case 0: /* L1 cache. */
1775 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1779 return env
->cp15
.c9_data
;
1781 return env
->cp15
.c9_insn
;
1785 case 1: /* L2 cache */
1788 /* L2 Lockdown and Auxiliary control. */
1793 case 10: /* MMU TLB lockdown. */
1794 /* ??? TLB lockdown not implemented. */
1796 case 11: /* TCM DMA control. */
1797 case 12: /* Reserved. */
1799 case 13: /* Process ID. */
1802 return env
->cp15
.c13_fcse
;
1804 return env
->cp15
.c13_context
;
1808 case 14: /* Reserved. */
1810 case 15: /* Implementation specific. */
1811 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1812 if (op2
== 0 && crm
== 1)
1813 return env
->cp15
.c15_cpar
;
1817 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1821 case 1: /* Read TI925T configuration. */
1822 return env
->cp15
.c15_ticonfig
;
1823 case 2: /* Read I_max. */
1824 return env
->cp15
.c15_i_max
;
1825 case 3: /* Read I_min. */
1826 return env
->cp15
.c15_i_min
;
1827 case 4: /* Read thread-ID. */
1828 return env
->cp15
.c15_threadid
;
1829 case 8: /* TI925T_status */
1832 /* TODO: Peripheral port remap register:
1833 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1834 * controller base address at $rn & ~0xfff and map size of
1835 * 0x200 << ($rn & 0xfff), when MMU is off. */
1841 /* ??? For debugging only. Should raise illegal instruction exception. */
1842 cpu_abort(env
, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1843 (insn
>> 16) & 0xf, crm
, op1
, op2
);
1847 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
1849 env
->banked_r13
[bank_number(mode
)] = val
;
1852 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
1854 return env
->banked_r13
[bank_number(mode
)];
1857 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
1861 return xpsr_read(env
) & 0xf8000000;
1863 return xpsr_read(env
) & 0xf80001ff;
1865 return xpsr_read(env
) & 0xff00fc00;
1867 return xpsr_read(env
) & 0xff00fdff;
1869 return xpsr_read(env
) & 0x000001ff;
1871 return xpsr_read(env
) & 0x0700fc00;
1873 return xpsr_read(env
) & 0x0700edff;
1875 return env
->v7m
.current_sp
? env
->v7m
.other_sp
: env
->regs
[13];
1877 return env
->v7m
.current_sp
? env
->regs
[13] : env
->v7m
.other_sp
;
1878 case 16: /* PRIMASK */
1879 return (env
->uncached_cpsr
& CPSR_I
) != 0;
1880 case 17: /* FAULTMASK */
1881 return (env
->uncached_cpsr
& CPSR_F
) != 0;
1882 case 18: /* BASEPRI */
1883 case 19: /* BASEPRI_MAX */
1884 return env
->v7m
.basepri
;
1885 case 20: /* CONTROL */
1886 return env
->v7m
.control
;
1888 /* ??? For debugging only. */
1889 cpu_abort(env
, "Unimplemented system register read (%d)\n", reg
);
1894 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
1898 xpsr_write(env
, val
, 0xf8000000);
1901 xpsr_write(env
, val
, 0xf8000000);
1904 xpsr_write(env
, val
, 0xfe00fc00);
1907 xpsr_write(env
, val
, 0xfe00fc00);
1910 /* IPSR bits are readonly. */
1913 xpsr_write(env
, val
, 0x0600fc00);
1916 xpsr_write(env
, val
, 0x0600fc00);
1919 if (env
->v7m
.current_sp
)
1920 env
->v7m
.other_sp
= val
;
1922 env
->regs
[13] = val
;
1925 if (env
->v7m
.current_sp
)
1926 env
->regs
[13] = val
;
1928 env
->v7m
.other_sp
= val
;
1930 case 16: /* PRIMASK */
1932 env
->uncached_cpsr
|= CPSR_I
;
1934 env
->uncached_cpsr
&= ~CPSR_I
;
1936 case 17: /* FAULTMASK */
1938 env
->uncached_cpsr
|= CPSR_F
;
1940 env
->uncached_cpsr
&= ~CPSR_F
;
1942 case 18: /* BASEPRI */
1943 env
->v7m
.basepri
= val
& 0xff;
1945 case 19: /* BASEPRI_MAX */
1947 if (val
!= 0 && (val
< env
->v7m
.basepri
|| env
->v7m
.basepri
== 0))
1948 env
->v7m
.basepri
= val
;
1950 case 20: /* CONTROL */
1951 env
->v7m
.control
= val
& 3;
1952 switch_v7m_sp(env
, (val
& 2) != 0);
1955 /* ??? For debugging only. */
1956 cpu_abort(env
, "Unimplemented system register write (%d)\n", reg
);
1961 void cpu_arm_set_cp_io(CPUARMState
*env
, int cpnum
,
1962 ARMReadCPFunc
*cp_read
, ARMWriteCPFunc
*cp_write
,
1965 if (cpnum
< 0 || cpnum
> 14) {
1966 cpu_abort(env
, "Bad coprocessor number: %i\n", cpnum
);
1970 env
->cp
[cpnum
].cp_read
= cp_read
;
1971 env
->cp
[cpnum
].cp_write
= cp_write
;
1972 env
->cp
[cpnum
].opaque
= opaque
;
1977 /* Note that signed overflow is undefined in C. The following routines are
1978 careful to use unsigned types where modulo arithmetic is required.
1979 Failure to do so _will_ break on newer gcc. */
1981 /* Signed saturating arithmetic. */
1983 /* Perform 16-bit signed saturating addition. */
1984 static inline uint16_t add16_sat(uint16_t a
, uint16_t b
)
1989 if (((res
^ a
) & 0x8000) && !((a
^ b
) & 0x8000)) {
1998 /* Perform 8-bit signed saturating addition. */
1999 static inline uint8_t add8_sat(uint8_t a
, uint8_t b
)
2004 if (((res
^ a
) & 0x80) && !((a
^ b
) & 0x80)) {
2013 /* Perform 16-bit signed saturating subtraction. */
2014 static inline uint16_t sub16_sat(uint16_t a
, uint16_t b
)
2019 if (((res
^ a
) & 0x8000) && ((a
^ b
) & 0x8000)) {
2028 /* Perform 8-bit signed saturating subtraction. */
2029 static inline uint8_t sub8_sat(uint8_t a
, uint8_t b
)
2034 if (((res
^ a
) & 0x80) && ((a
^ b
) & 0x80)) {
2043 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2044 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2045 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2046 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2049 #include "op_addsub.h"
2051 /* Unsigned saturating arithmetic. */
2052 static inline uint16_t add16_usat(uint16_t a
, uint16_t b
)
2061 static inline uint16_t sub16_usat(uint16_t a
, uint16_t b
)
2069 static inline uint8_t add8_usat(uint8_t a
, uint8_t b
)
2078 static inline uint8_t sub8_usat(uint8_t a
, uint8_t b
)
2086 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2087 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2088 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2089 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2092 #include "op_addsub.h"
2094 /* Signed modulo arithmetic. */
2095 #define SARITH16(a, b, n, op) do { \
2097 sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2098 RESULT(sum, n, 16); \
2100 ge |= 3 << (n * 2); \
2103 #define SARITH8(a, b, n, op) do { \
2105 sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2106 RESULT(sum, n, 8); \
2112 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2113 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2114 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2115 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2119 #include "op_addsub.h"
2121 /* Unsigned modulo arithmetic. */
2122 #define ADD16(a, b, n) do { \
2124 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2125 RESULT(sum, n, 16); \
2126 if ((sum >> 16) == 1) \
2127 ge |= 3 << (n * 2); \
2130 #define ADD8(a, b, n) do { \
2132 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2133 RESULT(sum, n, 8); \
2134 if ((sum >> 8) == 1) \
2138 #define SUB16(a, b, n) do { \
2140 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2141 RESULT(sum, n, 16); \
2142 if ((sum >> 16) == 0) \
2143 ge |= 3 << (n * 2); \
2146 #define SUB8(a, b, n) do { \
2148 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2149 RESULT(sum, n, 8); \
2150 if ((sum >> 8) == 0) \
2157 #include "op_addsub.h"
2159 /* Halved signed arithmetic. */
2160 #define ADD16(a, b, n) \
2161 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2162 #define SUB16(a, b, n) \
2163 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2164 #define ADD8(a, b, n) \
2165 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2166 #define SUB8(a, b, n) \
2167 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2170 #include "op_addsub.h"
2172 /* Halved unsigned arithmetic. */
2173 #define ADD16(a, b, n) \
2174 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2175 #define SUB16(a, b, n) \
2176 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2177 #define ADD8(a, b, n) \
2178 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2179 #define SUB8(a, b, n) \
2180 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2183 #include "op_addsub.h"
2185 static inline uint8_t do_usad(uint8_t a
, uint8_t b
)
2193 /* Unsigned sum of absolute byte differences. */
2194 uint32_t HELPER(usad8
)(uint32_t a
, uint32_t b
)
2197 sum
= do_usad(a
, b
);
2198 sum
+= do_usad(a
>> 8, b
>> 8);
2199 sum
+= do_usad(a
>> 16, b
>>16);
2200 sum
+= do_usad(a
>> 24, b
>> 24);
2204 /* For ARMv6 SEL instruction. */
2205 uint32_t HELPER(sel_flags
)(uint32_t flags
, uint32_t a
, uint32_t b
)
2218 return (a
& mask
) | (b
& ~mask
);
2221 uint32_t HELPER(logicq_cc
)(uint64_t val
)
2223 return (val
>> 32) | (val
!= 0);
2226 /* VFP support. We follow the convention used for VFP instrunctions:
2227 Single precition routines have a "s" suffix, double precision a
2230 /* Convert host exception flags to vfp form. */
2231 static inline int vfp_exceptbits_from_host(int host_bits
)
2233 int target_bits
= 0;
2235 if (host_bits
& float_flag_invalid
)
2237 if (host_bits
& float_flag_divbyzero
)
2239 if (host_bits
& float_flag_overflow
)
2241 if (host_bits
& float_flag_underflow
)
2243 if (host_bits
& float_flag_inexact
)
2244 target_bits
|= 0x10;
2248 uint32_t HELPER(vfp_get_fpscr
)(CPUState
*env
)
2253 fpscr
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & 0xffc8ffff)
2254 | (env
->vfp
.vec_len
<< 16)
2255 | (env
->vfp
.vec_stride
<< 20);
2256 i
= get_float_exception_flags(&env
->vfp
.fp_status
);
2257 fpscr
|= vfp_exceptbits_from_host(i
);
2261 uint32_t vfp_get_fpscr(CPUState
*env
)
2263 return HELPER(vfp_get_fpscr
)(env
);
2266 /* Convert vfp exception flags to target form. */
2267 static inline int vfp_exceptbits_to_host(int target_bits
)
2271 if (target_bits
& 1)
2272 host_bits
|= float_flag_invalid
;
2273 if (target_bits
& 2)
2274 host_bits
|= float_flag_divbyzero
;
2275 if (target_bits
& 4)
2276 host_bits
|= float_flag_overflow
;
2277 if (target_bits
& 8)
2278 host_bits
|= float_flag_underflow
;
2279 if (target_bits
& 0x10)
2280 host_bits
|= float_flag_inexact
;
2284 void HELPER(vfp_set_fpscr
)(CPUState
*env
, uint32_t val
)
2289 changed
= env
->vfp
.xregs
[ARM_VFP_FPSCR
];
2290 env
->vfp
.xregs
[ARM_VFP_FPSCR
] = (val
& 0xffc8ffff);
2291 env
->vfp
.vec_len
= (val
>> 16) & 7;
2292 env
->vfp
.vec_stride
= (val
>> 20) & 3;
2295 if (changed
& (3 << 22)) {
2296 i
= (val
>> 22) & 3;
2299 i
= float_round_nearest_even
;
2305 i
= float_round_down
;
2308 i
= float_round_to_zero
;
2311 set_float_rounding_mode(i
, &env
->vfp
.fp_status
);
2313 if (changed
& (1 << 24))
2314 set_flush_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
2315 if (changed
& (1 << 25))
2316 set_default_nan_mode((val
& (1 << 25)) != 0, &env
->vfp
.fp_status
);
2318 i
= vfp_exceptbits_to_host((val
>> 8) & 0x1f);
2319 set_float_exception_flags(i
, &env
->vfp
.fp_status
);
2322 void vfp_set_fpscr(CPUState
*env
, uint32_t val
)
2324 HELPER(vfp_set_fpscr
)(env
, val
);
2327 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2329 #define VFP_BINOP(name) \
2330 float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2332 return float32_ ## name (a, b, &env->vfp.fp_status); \
2334 float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2336 return float64_ ## name (a, b, &env->vfp.fp_status); \
2344 float32
VFP_HELPER(neg
, s
)(float32 a
)
2346 return float32_chs(a
);
2349 float64
VFP_HELPER(neg
, d
)(float64 a
)
2351 return float64_chs(a
);
2354 float32
VFP_HELPER(abs
, s
)(float32 a
)
2356 return float32_abs(a
);
2359 float64
VFP_HELPER(abs
, d
)(float64 a
)
2361 return float64_abs(a
);
2364 float32
VFP_HELPER(sqrt
, s
)(float32 a
, CPUState
*env
)
2366 return float32_sqrt(a
, &env
->vfp
.fp_status
);
2369 float64
VFP_HELPER(sqrt
, d
)(float64 a
, CPUState
*env
)
2371 return float64_sqrt(a
, &env
->vfp
.fp_status
);
2374 /* XXX: check quiet/signaling case */
2375 #define DO_VFP_cmp(p, type) \
2376 void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2379 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2380 case 0: flags = 0x6; break; \
2381 case -1: flags = 0x8; break; \
2382 case 1: flags = 0x2; break; \
2383 default: case 2: flags = 0x3; break; \
2385 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2386 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2388 void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2391 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2392 case 0: flags = 0x6; break; \
2393 case -1: flags = 0x8; break; \
2394 case 1: flags = 0x2; break; \
2395 default: case 2: flags = 0x3; break; \
2397 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2398 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2400 DO_VFP_cmp(s
, float32
)
2401 DO_VFP_cmp(d
, float64
)
2404 /* Helper routines to perform bitwise copies between float and int. */
2405 static inline float32
vfp_itos(uint32_t i
)
2416 static inline uint32_t vfp_stoi(float32 s
)
2427 static inline float64
vfp_itod(uint64_t i
)
2438 static inline uint64_t vfp_dtoi(float64 d
)
2449 /* Integer to float conversion. */
2450 float32
VFP_HELPER(uito
, s
)(float32 x
, CPUState
*env
)
2452 return uint32_to_float32(vfp_stoi(x
), &env
->vfp
.fp_status
);
2455 float64
VFP_HELPER(uito
, d
)(float32 x
, CPUState
*env
)
2457 return uint32_to_float64(vfp_stoi(x
), &env
->vfp
.fp_status
);
2460 float32
VFP_HELPER(sito
, s
)(float32 x
, CPUState
*env
)
2462 return int32_to_float32(vfp_stoi(x
), &env
->vfp
.fp_status
);
2465 float64
VFP_HELPER(sito
, d
)(float32 x
, CPUState
*env
)
2467 return int32_to_float64(vfp_stoi(x
), &env
->vfp
.fp_status
);
2470 /* Float to integer conversion. */
2471 float32
VFP_HELPER(toui
, s
)(float32 x
, CPUState
*env
)
2473 if (float32_is_any_nan(x
)) {
2474 return float32_zero
;
2476 return vfp_itos(float32_to_uint32(x
, &env
->vfp
.fp_status
));
2479 float32
VFP_HELPER(toui
, d
)(float64 x
, CPUState
*env
)
2481 if (float64_is_any_nan(x
)) {
2482 return float32_zero
;
2484 return vfp_itos(float64_to_uint32(x
, &env
->vfp
.fp_status
));
2487 float32
VFP_HELPER(tosi
, s
)(float32 x
, CPUState
*env
)
2489 if (float32_is_any_nan(x
)) {
2490 return float32_zero
;
2492 return vfp_itos(float32_to_int32(x
, &env
->vfp
.fp_status
));
2495 float32
VFP_HELPER(tosi
, d
)(float64 x
, CPUState
*env
)
2497 if (float64_is_any_nan(x
)) {
2498 return float32_zero
;
2500 return vfp_itos(float64_to_int32(x
, &env
->vfp
.fp_status
));
2503 float32
VFP_HELPER(touiz
, s
)(float32 x
, CPUState
*env
)
2505 if (float32_is_any_nan(x
)) {
2506 return float32_zero
;
2508 return vfp_itos(float32_to_uint32_round_to_zero(x
, &env
->vfp
.fp_status
));
2511 float32
VFP_HELPER(touiz
, d
)(float64 x
, CPUState
*env
)
2513 if (float64_is_any_nan(x
)) {
2514 return float32_zero
;
2516 return vfp_itos(float64_to_uint32_round_to_zero(x
, &env
->vfp
.fp_status
));
2519 float32
VFP_HELPER(tosiz
, s
)(float32 x
, CPUState
*env
)
2521 if (float32_is_any_nan(x
)) {
2522 return float32_zero
;
2524 return vfp_itos(float32_to_int32_round_to_zero(x
, &env
->vfp
.fp_status
));
2527 float32
VFP_HELPER(tosiz
, d
)(float64 x
, CPUState
*env
)
2529 if (float64_is_any_nan(x
)) {
2530 return float32_zero
;
2532 return vfp_itos(float64_to_int32_round_to_zero(x
, &env
->vfp
.fp_status
));
2535 /* floating point conversion */
2536 float64
VFP_HELPER(fcvtd
, s
)(float32 x
, CPUState
*env
)
2538 float64 r
= float32_to_float64(x
, &env
->vfp
.fp_status
);
2539 /* ARM requires that S<->D conversion of any kind of NaN generates
2540 * a quiet NaN by forcing the most significant frac bit to 1.
2542 return float64_maybe_silence_nan(r
);
2545 float32
VFP_HELPER(fcvts
, d
)(float64 x
, CPUState
*env
)
2547 float32 r
= float64_to_float32(x
, &env
->vfp
.fp_status
);
2548 /* ARM requires that S<->D conversion of any kind of NaN generates
2549 * a quiet NaN by forcing the most significant frac bit to 1.
2551 return float32_maybe_silence_nan(r
);
2554 /* VFP3 fixed point conversion. */
2555 #define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2556 ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2559 tmp = sign##int32_to_##ftype ((itype##_t)vfp_##p##toi(x), \
2560 &env->vfp.fp_status); \
2561 return ftype##_scalbn(tmp, -(int)shift, &env->vfp.fp_status); \
2563 ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2566 if (ftype##_is_any_nan(x)) { \
2567 return ftype##_zero; \
2569 tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2570 return vfp_ito##p(ftype##_to_##itype##_round_to_zero(tmp, \
2571 &env->vfp.fp_status)); \
2574 VFP_CONV_FIX(sh
, d
, float64
, int16
, )
2575 VFP_CONV_FIX(sl
, d
, float64
, int32
, )
2576 VFP_CONV_FIX(uh
, d
, float64
, uint16
, u
)
2577 VFP_CONV_FIX(ul
, d
, float64
, uint32
, u
)
2578 VFP_CONV_FIX(sh
, s
, float32
, int16
, )
2579 VFP_CONV_FIX(sl
, s
, float32
, int32
, )
2580 VFP_CONV_FIX(uh
, s
, float32
, uint16
, u
)
2581 VFP_CONV_FIX(ul
, s
, float32
, uint32
, u
)
2584 /* Half precision conversions. */
2585 float32
HELPER(vfp_fcvt_f16_to_f32
)(uint32_t a
, CPUState
*env
)
2587 float_status
*s
= &env
->vfp
.fp_status
;
2588 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2589 return float16_to_float32(a
, ieee
, s
);
2592 uint32_t HELPER(vfp_fcvt_f32_to_f16
)(float32 a
, CPUState
*env
)
2594 float_status
*s
= &env
->vfp
.fp_status
;
2595 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2596 return float32_to_float16(a
, ieee
, s
);
2599 float32
HELPER(recps_f32
)(float32 a
, float32 b
, CPUState
*env
)
2601 float_status
*s
= &env
->vfp
.fp_status
;
2602 float32 two
= int32_to_float32(2, s
);
2603 return float32_sub(two
, float32_mul(a
, b
, s
), s
);
2606 float32
HELPER(rsqrts_f32
)(float32 a
, float32 b
, CPUState
*env
)
2608 float_status
*s
= &env
->vfp
.fp_status
;
2609 float32 three
= int32_to_float32(3, s
);
2610 return float32_sub(three
, float32_mul(a
, b
, s
), s
);
2615 /* TODO: The architecture specifies the value that the estimate functions
2616 should return. We return the exact reciprocal/root instead. */
2617 float32
HELPER(recpe_f32
)(float32 a
, CPUState
*env
)
2619 float_status
*s
= &env
->vfp
.fp_status
;
2620 float32 one
= int32_to_float32(1, s
);
2621 return float32_div(one
, a
, s
);
2624 float32
HELPER(rsqrte_f32
)(float32 a
, CPUState
*env
)
2626 float_status
*s
= &env
->vfp
.fp_status
;
2627 float32 one
= int32_to_float32(1, s
);
2628 return float32_div(one
, float32_sqrt(a
, s
), s
);
2631 uint32_t HELPER(recpe_u32
)(uint32_t a
, CPUState
*env
)
2633 float_status
*s
= &env
->vfp
.fp_status
;
2635 tmp
= int32_to_float32(a
, s
);
2636 tmp
= float32_scalbn(tmp
, -32, s
);
2637 tmp
= helper_recpe_f32(tmp
, env
);
2638 tmp
= float32_scalbn(tmp
, 31, s
);
2639 return float32_to_int32(tmp
, s
);
2642 uint32_t HELPER(rsqrte_u32
)(uint32_t a
, CPUState
*env
)
2644 float_status
*s
= &env
->vfp
.fp_status
;
2646 tmp
= int32_to_float32(a
, s
);
2647 tmp
= float32_scalbn(tmp
, -32, s
);
2648 tmp
= helper_rsqrte_f32(tmp
, env
);
2649 tmp
= float32_scalbn(tmp
, 31, s
);
2650 return float32_to_int32(tmp
, s
);
2653 void HELPER(set_teecr
)(CPUState
*env
, uint32_t val
)
2656 if (env
->teecr
!= val
) {