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;
80 case ARM_CPUID_ARM11MPCORE
:
81 set_feature(env
, ARM_FEATURE_V6
);
82 set_feature(env
, ARM_FEATURE_V6K
);
83 set_feature(env
, ARM_FEATURE_VFP
);
84 set_feature(env
, ARM_FEATURE_AUXCR
);
85 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410120b4;
86 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11111111;
87 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00000000;
88 memcpy(env
->cp15
.c0_c1
, mpcore_cp15_c0_c1
, 8 * sizeof(uint32_t));
89 memcpy(env
->cp15
.c0_c2
, mpcore_cp15_c0_c2
, 8 * sizeof(uint32_t));
90 env
->cp15
.c0_cachetype
= 0x1dd20d2;
92 case ARM_CPUID_CORTEXA8
:
93 set_feature(env
, ARM_FEATURE_V6
);
94 set_feature(env
, ARM_FEATURE_V6K
);
95 set_feature(env
, ARM_FEATURE_V7
);
96 set_feature(env
, ARM_FEATURE_AUXCR
);
97 set_feature(env
, ARM_FEATURE_THUMB2
);
98 set_feature(env
, ARM_FEATURE_VFP
);
99 set_feature(env
, ARM_FEATURE_VFP3
);
100 set_feature(env
, ARM_FEATURE_NEON
);
101 set_feature(env
, ARM_FEATURE_THUMB2EE
);
102 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x410330c0;
103 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11110222;
104 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x00011100;
105 memcpy(env
->cp15
.c0_c1
, cortexa8_cp15_c0_c1
, 8 * sizeof(uint32_t));
106 memcpy(env
->cp15
.c0_c2
, cortexa8_cp15_c0_c2
, 8 * sizeof(uint32_t));
107 env
->cp15
.c0_cachetype
= 0x82048004;
108 env
->cp15
.c0_clid
= (1 << 27) | (2 << 24) | 3;
109 env
->cp15
.c0_ccsid
[0] = 0xe007e01a; /* 16k L1 dcache. */
110 env
->cp15
.c0_ccsid
[1] = 0x2007e01a; /* 16k L1 icache. */
111 env
->cp15
.c0_ccsid
[2] = 0xf0000000; /* No L2 icache. */
113 case ARM_CPUID_CORTEXA9
:
114 set_feature(env
, ARM_FEATURE_V6
);
115 set_feature(env
, ARM_FEATURE_V6K
);
116 set_feature(env
, ARM_FEATURE_V7
);
117 set_feature(env
, ARM_FEATURE_AUXCR
);
118 set_feature(env
, ARM_FEATURE_THUMB2
);
119 set_feature(env
, ARM_FEATURE_VFP
);
120 set_feature(env
, ARM_FEATURE_VFP3
);
121 set_feature(env
, ARM_FEATURE_VFP_FP16
);
122 set_feature(env
, ARM_FEATURE_NEON
);
123 set_feature(env
, ARM_FEATURE_THUMB2EE
);
124 env
->vfp
.xregs
[ARM_VFP_FPSID
] = 0x41034000; /* Guess */
125 env
->vfp
.xregs
[ARM_VFP_MVFR0
] = 0x11110222;
126 env
->vfp
.xregs
[ARM_VFP_MVFR1
] = 0x01111111;
127 memcpy(env
->cp15
.c0_c1
, cortexa9_cp15_c0_c1
, 8 * sizeof(uint32_t));
128 memcpy(env
->cp15
.c0_c2
, cortexa9_cp15_c0_c2
, 8 * sizeof(uint32_t));
129 env
->cp15
.c0_cachetype
= 0x80038003;
130 env
->cp15
.c0_clid
= (1 << 27) | (1 << 24) | 3;
131 env
->cp15
.c0_ccsid
[0] = 0xe00fe015; /* 16k L1 dcache. */
132 env
->cp15
.c0_ccsid
[1] = 0x200fe015; /* 16k L1 icache. */
134 case ARM_CPUID_CORTEXM3
:
135 set_feature(env
, ARM_FEATURE_V6
);
136 set_feature(env
, ARM_FEATURE_THUMB2
);
137 set_feature(env
, ARM_FEATURE_V7
);
138 set_feature(env
, ARM_FEATURE_M
);
139 set_feature(env
, ARM_FEATURE_DIV
);
141 case ARM_CPUID_ANY
: /* For userspace emulation. */
142 set_feature(env
, ARM_FEATURE_V6
);
143 set_feature(env
, ARM_FEATURE_V6K
);
144 set_feature(env
, ARM_FEATURE_V7
);
145 set_feature(env
, ARM_FEATURE_THUMB2
);
146 set_feature(env
, ARM_FEATURE_VFP
);
147 set_feature(env
, ARM_FEATURE_VFP3
);
148 set_feature(env
, ARM_FEATURE_VFP_FP16
);
149 set_feature(env
, ARM_FEATURE_NEON
);
150 set_feature(env
, ARM_FEATURE_THUMB2EE
);
151 set_feature(env
, ARM_FEATURE_DIV
);
153 case ARM_CPUID_TI915T
:
154 case ARM_CPUID_TI925T
:
155 set_feature(env
, ARM_FEATURE_OMAPCP
);
156 env
->cp15
.c0_cpuid
= ARM_CPUID_TI925T
; /* Depends on wiring. */
157 env
->cp15
.c0_cachetype
= 0x5109149;
158 env
->cp15
.c1_sys
= 0x00000070;
159 env
->cp15
.c15_i_max
= 0x000;
160 env
->cp15
.c15_i_min
= 0xff0;
162 case ARM_CPUID_PXA250
:
163 case ARM_CPUID_PXA255
:
164 case ARM_CPUID_PXA260
:
165 case ARM_CPUID_PXA261
:
166 case ARM_CPUID_PXA262
:
167 set_feature(env
, ARM_FEATURE_XSCALE
);
168 /* JTAG_ID is ((id << 28) | 0x09265013) */
169 env
->cp15
.c0_cachetype
= 0xd172172;
170 env
->cp15
.c1_sys
= 0x00000078;
172 case ARM_CPUID_PXA270_A0
:
173 case ARM_CPUID_PXA270_A1
:
174 case ARM_CPUID_PXA270_B0
:
175 case ARM_CPUID_PXA270_B1
:
176 case ARM_CPUID_PXA270_C0
:
177 case ARM_CPUID_PXA270_C5
:
178 set_feature(env
, ARM_FEATURE_XSCALE
);
179 /* JTAG_ID is ((id << 28) | 0x09265013) */
180 set_feature(env
, ARM_FEATURE_IWMMXT
);
181 env
->iwmmxt
.cregs
[ARM_IWMMXT_wCID
] = 0x69051000 | 'Q';
182 env
->cp15
.c0_cachetype
= 0xd172172;
183 env
->cp15
.c1_sys
= 0x00000078;
186 cpu_abort(env
, "Bad CPU ID: %x\n", id
);
191 void cpu_reset(CPUARMState
*env
)
195 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
196 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
197 log_cpu_state(env
, 0);
200 id
= env
->cp15
.c0_cpuid
;
201 memset(env
, 0, offsetof(CPUARMState
, breakpoints
));
203 cpu_reset_model_id(env
, id
);
204 #if defined (CONFIG_USER_ONLY)
205 env
->uncached_cpsr
= ARM_CPU_MODE_USR
;
206 /* For user mode we must enable access to coprocessors */
207 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = 1 << 30;
208 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
209 env
->cp15
.c15_cpar
= 3;
210 } else if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
211 env
->cp15
.c15_cpar
= 1;
214 /* SVC mode with interrupts disabled. */
215 env
->uncached_cpsr
= ARM_CPU_MODE_SVC
| CPSR_A
| CPSR_F
| CPSR_I
;
216 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
217 clear at reset. Initial SP and PC are loaded from ROM. */
221 env
->uncached_cpsr
&= ~CPSR_I
;
224 /* We should really use ldl_phys here, in case the guest
225 modified flash and reset itself. However images
226 loaded via -kenrel have not been copied yet, so load the
227 values directly from there. */
228 env
->regs
[13] = ldl_p(rom
);
231 env
->regs
[15] = pc
& ~1;
234 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = 0;
235 env
->cp15
.c2_base_mask
= 0xffffc000u
;
240 static int vfp_gdb_get_reg(CPUState
*env
, uint8_t *buf
, int reg
)
244 /* VFP data registers are always little-endian. */
245 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
247 stfq_le_p(buf
, env
->vfp
.regs
[reg
]);
250 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
251 /* Aliases for Q regs. */
254 stfq_le_p(buf
, env
->vfp
.regs
[(reg
- 32) * 2]);
255 stfq_le_p(buf
+ 8, env
->vfp
.regs
[(reg
- 32) * 2 + 1]);
259 switch (reg
- nregs
) {
260 case 0: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSID
]); return 4;
261 case 1: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPSCR
]); return 4;
262 case 2: stl_p(buf
, env
->vfp
.xregs
[ARM_VFP_FPEXC
]); return 4;
267 static int vfp_gdb_set_reg(CPUState
*env
, uint8_t *buf
, int reg
)
271 nregs
= arm_feature(env
, ARM_FEATURE_VFP3
) ? 32 : 16;
273 env
->vfp
.regs
[reg
] = ldfq_le_p(buf
);
276 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
279 env
->vfp
.regs
[(reg
- 32) * 2] = ldfq_le_p(buf
);
280 env
->vfp
.regs
[(reg
- 32) * 2 + 1] = ldfq_le_p(buf
+ 8);
284 switch (reg
- nregs
) {
285 case 0: env
->vfp
.xregs
[ARM_VFP_FPSID
] = ldl_p(buf
); return 4;
286 case 1: env
->vfp
.xregs
[ARM_VFP_FPSCR
] = ldl_p(buf
); return 4;
287 case 2: env
->vfp
.xregs
[ARM_VFP_FPEXC
] = ldl_p(buf
) & (1 << 30); return 4;
292 CPUARMState
*cpu_arm_init(const char *cpu_model
)
296 static int inited
= 0;
298 id
= cpu_arm_find_by_name(cpu_model
);
301 env
= qemu_mallocz(sizeof(CPUARMState
));
305 arm_translate_init();
308 env
->cpu_model_str
= cpu_model
;
309 env
->cp15
.c0_cpuid
= id
;
311 if (arm_feature(env
, ARM_FEATURE_NEON
)) {
312 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
313 51, "arm-neon.xml", 0);
314 } else if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
315 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
316 35, "arm-vfp3.xml", 0);
317 } else if (arm_feature(env
, ARM_FEATURE_VFP
)) {
318 gdb_register_coprocessor(env
, vfp_gdb_get_reg
, vfp_gdb_set_reg
,
319 19, "arm-vfp.xml", 0);
330 static const struct arm_cpu_t arm_cpu_names
[] = {
331 { ARM_CPUID_ARM926
, "arm926"},
332 { ARM_CPUID_ARM946
, "arm946"},
333 { ARM_CPUID_ARM1026
, "arm1026"},
334 { ARM_CPUID_ARM1136
, "arm1136"},
335 { ARM_CPUID_ARM1136_R2
, "arm1136-r2"},
336 { ARM_CPUID_ARM11MPCORE
, "arm11mpcore"},
337 { ARM_CPUID_CORTEXM3
, "cortex-m3"},
338 { ARM_CPUID_CORTEXA8
, "cortex-a8"},
339 { ARM_CPUID_CORTEXA9
, "cortex-a9"},
340 { ARM_CPUID_TI925T
, "ti925t" },
341 { ARM_CPUID_PXA250
, "pxa250" },
342 { ARM_CPUID_PXA255
, "pxa255" },
343 { ARM_CPUID_PXA260
, "pxa260" },
344 { ARM_CPUID_PXA261
, "pxa261" },
345 { ARM_CPUID_PXA262
, "pxa262" },
346 { ARM_CPUID_PXA270
, "pxa270" },
347 { ARM_CPUID_PXA270_A0
, "pxa270-a0" },
348 { ARM_CPUID_PXA270_A1
, "pxa270-a1" },
349 { ARM_CPUID_PXA270_B0
, "pxa270-b0" },
350 { ARM_CPUID_PXA270_B1
, "pxa270-b1" },
351 { ARM_CPUID_PXA270_C0
, "pxa270-c0" },
352 { ARM_CPUID_PXA270_C5
, "pxa270-c5" },
353 { ARM_CPUID_ANY
, "any"},
357 void arm_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
361 (*cpu_fprintf
)(f
, "Available CPUs:\n");
362 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
363 (*cpu_fprintf
)(f
, " %s\n", arm_cpu_names
[i
].name
);
367 /* return 0 if not found */
368 static uint32_t cpu_arm_find_by_name(const char *name
)
374 for (i
= 0; arm_cpu_names
[i
].name
; i
++) {
375 if (strcmp(name
, arm_cpu_names
[i
].name
) == 0) {
376 id
= arm_cpu_names
[i
].id
;
383 void cpu_arm_close(CPUARMState
*env
)
388 uint32_t cpsr_read(CPUARMState
*env
)
392 return env
->uncached_cpsr
| (env
->NF
& 0x80000000) | (ZF
<< 30) |
393 (env
->CF
<< 29) | ((env
->VF
& 0x80000000) >> 3) | (env
->QF
<< 27)
394 | (env
->thumb
<< 5) | ((env
->condexec_bits
& 3) << 25)
395 | ((env
->condexec_bits
& 0xfc) << 8)
399 void cpsr_write(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
401 if (mask
& CPSR_NZCV
) {
402 env
->ZF
= (~val
) & CPSR_Z
;
404 env
->CF
= (val
>> 29) & 1;
405 env
->VF
= (val
<< 3) & 0x80000000;
408 env
->QF
= ((val
& CPSR_Q
) != 0);
410 env
->thumb
= ((val
& CPSR_T
) != 0);
411 if (mask
& CPSR_IT_0_1
) {
412 env
->condexec_bits
&= ~3;
413 env
->condexec_bits
|= (val
>> 25) & 3;
415 if (mask
& CPSR_IT_2_7
) {
416 env
->condexec_bits
&= 3;
417 env
->condexec_bits
|= (val
>> 8) & 0xfc;
419 if (mask
& CPSR_GE
) {
420 env
->GE
= (val
>> 16) & 0xf;
423 if ((env
->uncached_cpsr
^ val
) & mask
& CPSR_M
) {
424 switch_mode(env
, val
& CPSR_M
);
426 mask
&= ~CACHED_CPSR_BITS
;
427 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~mask
) | (val
& mask
);
430 /* Sign/zero extend */
431 uint32_t HELPER(sxtb16
)(uint32_t x
)
434 res
= (uint16_t)(int8_t)x
;
435 res
|= (uint32_t)(int8_t)(x
>> 16) << 16;
439 uint32_t HELPER(uxtb16
)(uint32_t x
)
442 res
= (uint16_t)(uint8_t)x
;
443 res
|= (uint32_t)(uint8_t)(x
>> 16) << 16;
447 uint32_t HELPER(clz
)(uint32_t x
)
452 int32_t HELPER(sdiv
)(int32_t num
, int32_t den
)
456 if (num
== INT_MIN
&& den
== -1)
461 uint32_t HELPER(udiv
)(uint32_t num
, uint32_t den
)
468 uint32_t HELPER(rbit
)(uint32_t x
)
470 x
= ((x
& 0xff000000) >> 24)
471 | ((x
& 0x00ff0000) >> 8)
472 | ((x
& 0x0000ff00) << 8)
473 | ((x
& 0x000000ff) << 24);
474 x
= ((x
& 0xf0f0f0f0) >> 4)
475 | ((x
& 0x0f0f0f0f) << 4);
476 x
= ((x
& 0x88888888) >> 3)
477 | ((x
& 0x44444444) >> 1)
478 | ((x
& 0x22222222) << 1)
479 | ((x
& 0x11111111) << 3);
483 uint32_t HELPER(abs
)(uint32_t x
)
485 return ((int32_t)x
< 0) ? -x
: x
;
488 #if defined(CONFIG_USER_ONLY)
490 void do_interrupt (CPUState
*env
)
492 env
->exception_index
= -1;
495 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
496 int mmu_idx
, int is_softmmu
)
499 env
->exception_index
= EXCP_PREFETCH_ABORT
;
500 env
->cp15
.c6_insn
= address
;
502 env
->exception_index
= EXCP_DATA_ABORT
;
503 env
->cp15
.c6_data
= address
;
508 /* These should probably raise undefined insn exceptions. */
509 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
511 int op1
= (insn
>> 8) & 0xf;
512 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
516 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
518 int op1
= (insn
>> 8) & 0xf;
519 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
523 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
525 cpu_abort(env
, "cp15 insn %08x\n", insn
);
528 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
530 cpu_abort(env
, "cp15 insn %08x\n", insn
);
533 /* These should probably raise undefined insn exceptions. */
534 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
536 cpu_abort(env
, "v7m_mrs %d\n", reg
);
539 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
541 cpu_abort(env
, "v7m_mrs %d\n", reg
);
545 void switch_mode(CPUState
*env
, int mode
)
547 if (mode
!= ARM_CPU_MODE_USR
)
548 cpu_abort(env
, "Tried to switch out of user mode\n");
551 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
553 cpu_abort(env
, "banked r13 write\n");
556 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
558 cpu_abort(env
, "banked r13 read\n");
564 extern int semihosting_enabled
;
566 /* Map CPU modes onto saved register banks. */
567 static inline int bank_number (int mode
)
570 case ARM_CPU_MODE_USR
:
571 case ARM_CPU_MODE_SYS
:
573 case ARM_CPU_MODE_SVC
:
575 case ARM_CPU_MODE_ABT
:
577 case ARM_CPU_MODE_UND
:
579 case ARM_CPU_MODE_IRQ
:
581 case ARM_CPU_MODE_FIQ
:
584 cpu_abort(cpu_single_env
, "Bad mode %x\n", mode
);
588 void switch_mode(CPUState
*env
, int mode
)
593 old_mode
= env
->uncached_cpsr
& CPSR_M
;
594 if (mode
== old_mode
)
597 if (old_mode
== ARM_CPU_MODE_FIQ
) {
598 memcpy (env
->fiq_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
599 memcpy (env
->regs
+ 8, env
->usr_regs
, 5 * sizeof(uint32_t));
600 } else if (mode
== ARM_CPU_MODE_FIQ
) {
601 memcpy (env
->usr_regs
, env
->regs
+ 8, 5 * sizeof(uint32_t));
602 memcpy (env
->regs
+ 8, env
->fiq_regs
, 5 * sizeof(uint32_t));
605 i
= bank_number(old_mode
);
606 env
->banked_r13
[i
] = env
->regs
[13];
607 env
->banked_r14
[i
] = env
->regs
[14];
608 env
->banked_spsr
[i
] = env
->spsr
;
610 i
= bank_number(mode
);
611 env
->regs
[13] = env
->banked_r13
[i
];
612 env
->regs
[14] = env
->banked_r14
[i
];
613 env
->spsr
= env
->banked_spsr
[i
];
616 static void v7m_push(CPUARMState
*env
, uint32_t val
)
619 stl_phys(env
->regs
[13], val
);
622 static uint32_t v7m_pop(CPUARMState
*env
)
625 val
= ldl_phys(env
->regs
[13]);
630 /* Switch to V7M main or process stack pointer. */
631 static void switch_v7m_sp(CPUARMState
*env
, int process
)
634 if (env
->v7m
.current_sp
!= process
) {
635 tmp
= env
->v7m
.other_sp
;
636 env
->v7m
.other_sp
= env
->regs
[13];
638 env
->v7m
.current_sp
= process
;
642 static void do_v7m_exception_exit(CPUARMState
*env
)
647 type
= env
->regs
[15];
648 if (env
->v7m
.exception
!= 0)
649 armv7m_nvic_complete_irq(env
->nvic
, env
->v7m
.exception
);
651 /* Switch to the target stack. */
652 switch_v7m_sp(env
, (type
& 4) != 0);
654 env
->regs
[0] = v7m_pop(env
);
655 env
->regs
[1] = v7m_pop(env
);
656 env
->regs
[2] = v7m_pop(env
);
657 env
->regs
[3] = v7m_pop(env
);
658 env
->regs
[12] = v7m_pop(env
);
659 env
->regs
[14] = v7m_pop(env
);
660 env
->regs
[15] = v7m_pop(env
);
662 xpsr_write(env
, xpsr
, 0xfffffdff);
663 /* Undo stack alignment. */
666 /* ??? The exception return type specifies Thread/Handler mode. However
667 this is also implied by the xPSR value. Not sure what to do
668 if there is a mismatch. */
669 /* ??? Likewise for mismatches between the CONTROL register and the stack
673 static void do_interrupt_v7m(CPUARMState
*env
)
675 uint32_t xpsr
= xpsr_read(env
);
680 if (env
->v7m
.current_sp
)
682 if (env
->v7m
.exception
== 0)
685 /* For exceptions we just mark as pending on the NVIC, and let that
687 /* TODO: Need to escalate if the current priority is higher than the
688 one we're raising. */
689 switch (env
->exception_index
) {
691 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_USAGE
);
695 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_SVC
);
697 case EXCP_PREFETCH_ABORT
:
698 case EXCP_DATA_ABORT
:
699 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_MEM
);
702 if (semihosting_enabled
) {
704 nr
= lduw_code(env
->regs
[15]) & 0xff;
707 env
->regs
[0] = do_arm_semihosting(env
);
711 armv7m_nvic_set_pending(env
->nvic
, ARMV7M_EXCP_DEBUG
);
714 env
->v7m
.exception
= armv7m_nvic_acknowledge_irq(env
->nvic
);
716 case EXCP_EXCEPTION_EXIT
:
717 do_v7m_exception_exit(env
);
720 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
721 return; /* Never happens. Keep compiler happy. */
724 /* Align stack pointer. */
725 /* ??? Should only do this if Configuration Control Register
726 STACKALIGN bit is set. */
727 if (env
->regs
[13] & 4) {
731 /* Switch to the handler mode. */
733 v7m_push(env
, env
->regs
[15]);
734 v7m_push(env
, env
->regs
[14]);
735 v7m_push(env
, env
->regs
[12]);
736 v7m_push(env
, env
->regs
[3]);
737 v7m_push(env
, env
->regs
[2]);
738 v7m_push(env
, env
->regs
[1]);
739 v7m_push(env
, env
->regs
[0]);
740 switch_v7m_sp(env
, 0);
741 env
->uncached_cpsr
&= ~CPSR_IT
;
743 addr
= ldl_phys(env
->v7m
.vecbase
+ env
->v7m
.exception
* 4);
744 env
->regs
[15] = addr
& 0xfffffffe;
745 env
->thumb
= addr
& 1;
748 /* Handle a CPU exception. */
749 void do_interrupt(CPUARMState
*env
)
757 do_interrupt_v7m(env
);
760 /* TODO: Vectored interrupt controller. */
761 switch (env
->exception_index
) {
763 new_mode
= ARM_CPU_MODE_UND
;
772 if (semihosting_enabled
) {
773 /* Check for semihosting interrupt. */
775 mask
= lduw_code(env
->regs
[15] - 2) & 0xff;
777 mask
= ldl_code(env
->regs
[15] - 4) & 0xffffff;
779 /* Only intercept calls from privileged modes, to provide some
780 semblance of security. */
781 if (((mask
== 0x123456 && !env
->thumb
)
782 || (mask
== 0xab && env
->thumb
))
783 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
784 env
->regs
[0] = do_arm_semihosting(env
);
788 new_mode
= ARM_CPU_MODE_SVC
;
791 /* The PC already points to the next instruction. */
795 /* See if this is a semihosting syscall. */
796 if (env
->thumb
&& semihosting_enabled
) {
797 mask
= lduw_code(env
->regs
[15]) & 0xff;
799 && (env
->uncached_cpsr
& CPSR_M
) != ARM_CPU_MODE_USR
) {
801 env
->regs
[0] = do_arm_semihosting(env
);
805 /* Fall through to prefetch abort. */
806 case EXCP_PREFETCH_ABORT
:
807 new_mode
= ARM_CPU_MODE_ABT
;
809 mask
= CPSR_A
| CPSR_I
;
812 case EXCP_DATA_ABORT
:
813 new_mode
= ARM_CPU_MODE_ABT
;
815 mask
= CPSR_A
| CPSR_I
;
819 new_mode
= ARM_CPU_MODE_IRQ
;
821 /* Disable IRQ and imprecise data aborts. */
822 mask
= CPSR_A
| CPSR_I
;
826 new_mode
= ARM_CPU_MODE_FIQ
;
828 /* Disable FIQ, IRQ and imprecise data aborts. */
829 mask
= CPSR_A
| CPSR_I
| CPSR_F
;
833 cpu_abort(env
, "Unhandled exception 0x%x\n", env
->exception_index
);
834 return; /* Never happens. Keep compiler happy. */
837 if (env
->cp15
.c1_sys
& (1 << 13)) {
840 switch_mode (env
, new_mode
);
841 env
->spsr
= cpsr_read(env
);
843 env
->condexec_bits
= 0;
844 /* Switch to the new mode, and to the correct instruction set. */
845 env
->uncached_cpsr
= (env
->uncached_cpsr
& ~CPSR_M
) | new_mode
;
846 env
->uncached_cpsr
|= mask
;
847 env
->thumb
= (env
->cp15
.c1_sys
& (1 << 30)) != 0;
848 env
->regs
[14] = env
->regs
[15] + offset
;
849 env
->regs
[15] = addr
;
850 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
853 /* Check section/page access permissions.
854 Returns the page protection flags, or zero if the access is not
856 static inline int check_ap(CPUState
*env
, int ap
, int domain
, int access_type
,
862 return PAGE_READ
| PAGE_WRITE
;
864 if (access_type
== 1)
871 if (access_type
== 1)
873 switch ((env
->cp15
.c1_sys
>> 8) & 3) {
875 return is_user
? 0 : PAGE_READ
;
882 return is_user
? 0 : PAGE_READ
| PAGE_WRITE
;
887 return PAGE_READ
| PAGE_WRITE
;
889 return PAGE_READ
| PAGE_WRITE
;
890 case 4: /* Reserved. */
893 return is_user
? 0 : prot_ro
;
897 if (!arm_feature (env
, ARM_FEATURE_V7
))
905 static uint32_t get_level1_table_address(CPUState
*env
, uint32_t address
)
909 if (address
& env
->cp15
.c2_mask
)
910 table
= env
->cp15
.c2_base1
& 0xffffc000;
912 table
= env
->cp15
.c2_base0
& env
->cp15
.c2_base_mask
;
914 table
|= (address
>> 18) & 0x3ffc;
918 static int get_phys_addr_v5(CPUState
*env
, uint32_t address
, int access_type
,
919 int is_user
, uint32_t *phys_ptr
, int *prot
,
920 target_ulong
*page_size
)
930 /* Pagetable walk. */
931 /* Lookup l1 descriptor. */
932 table
= get_level1_table_address(env
, address
);
933 desc
= ldl_phys(table
);
935 domain
= (env
->cp15
.c3
>> ((desc
>> 4) & 0x1e)) & 3;
937 /* Section translation fault. */
941 if (domain
== 0 || domain
== 2) {
943 code
= 9; /* Section domain fault. */
945 code
= 11; /* Page domain fault. */
950 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
951 ap
= (desc
>> 10) & 3;
953 *page_size
= 1024 * 1024;
955 /* Lookup l2 entry. */
957 /* Coarse pagetable. */
958 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
960 /* Fine pagetable. */
961 table
= (desc
& 0xfffff000) | ((address
>> 8) & 0xffc);
963 desc
= ldl_phys(table
);
965 case 0: /* Page translation fault. */
968 case 1: /* 64k page. */
969 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
970 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
971 *page_size
= 0x10000;
973 case 2: /* 4k page. */
974 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
975 ap
= (desc
>> (4 + ((address
>> 13) & 6))) & 3;
978 case 3: /* 1k page. */
980 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
981 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
983 /* Page translation fault. */
988 phys_addr
= (desc
& 0xfffffc00) | (address
& 0x3ff);
990 ap
= (desc
>> 4) & 3;
994 /* Never happens, but compiler isn't smart enough to tell. */
999 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1001 /* Access permission fault. */
1005 *phys_ptr
= phys_addr
;
1008 return code
| (domain
<< 4);
1011 static int get_phys_addr_v6(CPUState
*env
, uint32_t address
, int access_type
,
1012 int is_user
, uint32_t *phys_ptr
, int *prot
,
1013 target_ulong
*page_size
)
1024 /* Pagetable walk. */
1025 /* Lookup l1 descriptor. */
1026 table
= get_level1_table_address(env
, address
);
1027 desc
= ldl_phys(table
);
1030 /* Section translation fault. */
1034 } else if (type
== 2 && (desc
& (1 << 18))) {
1038 /* Section or page. */
1039 domain
= (desc
>> 4) & 0x1e;
1041 domain
= (env
->cp15
.c3
>> domain
) & 3;
1042 if (domain
== 0 || domain
== 2) {
1044 code
= 9; /* Section domain fault. */
1046 code
= 11; /* Page domain fault. */
1050 if (desc
& (1 << 18)) {
1052 phys_addr
= (desc
& 0xff000000) | (address
& 0x00ffffff);
1053 *page_size
= 0x1000000;
1056 phys_addr
= (desc
& 0xfff00000) | (address
& 0x000fffff);
1057 *page_size
= 0x100000;
1059 ap
= ((desc
>> 10) & 3) | ((desc
>> 13) & 4);
1060 xn
= desc
& (1 << 4);
1063 /* Lookup l2 entry. */
1064 table
= (desc
& 0xfffffc00) | ((address
>> 10) & 0x3fc);
1065 desc
= ldl_phys(table
);
1066 ap
= ((desc
>> 4) & 3) | ((desc
>> 7) & 4);
1068 case 0: /* Page translation fault. */
1071 case 1: /* 64k page. */
1072 phys_addr
= (desc
& 0xffff0000) | (address
& 0xffff);
1073 xn
= desc
& (1 << 15);
1074 *page_size
= 0x10000;
1076 case 2: case 3: /* 4k page. */
1077 phys_addr
= (desc
& 0xfffff000) | (address
& 0xfff);
1079 *page_size
= 0x1000;
1082 /* Never happens, but compiler isn't smart enough to tell. */
1087 if (xn
&& access_type
== 2)
1090 /* The simplified model uses AP[0] as an access control bit. */
1091 if ((env
->cp15
.c1_sys
& (1 << 29)) && (ap
& 1) == 0) {
1092 /* Access flag fault. */
1093 code
= (code
== 15) ? 6 : 3;
1096 *prot
= check_ap(env
, ap
, domain
, access_type
, is_user
);
1098 /* Access permission fault. */
1104 *phys_ptr
= phys_addr
;
1107 return code
| (domain
<< 4);
1110 static int get_phys_addr_mpu(CPUState
*env
, uint32_t address
, int access_type
,
1111 int is_user
, uint32_t *phys_ptr
, int *prot
)
1117 *phys_ptr
= address
;
1118 for (n
= 7; n
>= 0; n
--) {
1119 base
= env
->cp15
.c6_region
[n
];
1120 if ((base
& 1) == 0)
1122 mask
= 1 << ((base
>> 1) & 0x1f);
1123 /* Keep this shift separate from the above to avoid an
1124 (undefined) << 32. */
1125 mask
= (mask
<< 1) - 1;
1126 if (((base
^ address
) & ~mask
) == 0)
1132 if (access_type
== 2) {
1133 mask
= env
->cp15
.c5_insn
;
1135 mask
= env
->cp15
.c5_data
;
1137 mask
= (mask
>> (n
* 4)) & 0xf;
1144 *prot
= PAGE_READ
| PAGE_WRITE
;
1149 *prot
|= PAGE_WRITE
;
1152 *prot
= PAGE_READ
| PAGE_WRITE
;
1163 /* Bad permission. */
1170 static inline int get_phys_addr(CPUState
*env
, uint32_t address
,
1171 int access_type
, int is_user
,
1172 uint32_t *phys_ptr
, int *prot
,
1173 target_ulong
*page_size
)
1175 /* Fast Context Switch Extension. */
1176 if (address
< 0x02000000)
1177 address
+= env
->cp15
.c13_fcse
;
1179 if ((env
->cp15
.c1_sys
& 1) == 0) {
1180 /* MMU/MPU disabled. */
1181 *phys_ptr
= address
;
1182 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1183 *page_size
= TARGET_PAGE_SIZE
;
1185 } else if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1186 *page_size
= TARGET_PAGE_SIZE
;
1187 return get_phys_addr_mpu(env
, address
, access_type
, is_user
, phys_ptr
,
1189 } else if (env
->cp15
.c1_sys
& (1 << 23)) {
1190 return get_phys_addr_v6(env
, address
, access_type
, is_user
, phys_ptr
,
1193 return get_phys_addr_v5(env
, address
, access_type
, is_user
, phys_ptr
,
1198 int cpu_arm_handle_mmu_fault (CPUState
*env
, target_ulong address
,
1199 int access_type
, int mmu_idx
, int is_softmmu
)
1202 target_ulong page_size
;
1206 is_user
= mmu_idx
== MMU_USER_IDX
;
1207 ret
= get_phys_addr(env
, address
, access_type
, is_user
, &phys_addr
, &prot
,
1210 /* Map a single [sub]page. */
1211 phys_addr
&= ~(uint32_t)0x3ff;
1212 address
&= ~(uint32_t)0x3ff;
1213 tlb_set_page (env
, address
, phys_addr
, prot
, mmu_idx
, page_size
);
1217 if (access_type
== 2) {
1218 env
->cp15
.c5_insn
= ret
;
1219 env
->cp15
.c6_insn
= address
;
1220 env
->exception_index
= EXCP_PREFETCH_ABORT
;
1222 env
->cp15
.c5_data
= ret
;
1223 if (access_type
== 1 && arm_feature(env
, ARM_FEATURE_V6
))
1224 env
->cp15
.c5_data
|= (1 << 11);
1225 env
->cp15
.c6_data
= address
;
1226 env
->exception_index
= EXCP_DATA_ABORT
;
1231 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
*env
, target_ulong addr
)
1234 target_ulong page_size
;
1238 ret
= get_phys_addr(env
, addr
, 0, 0, &phys_addr
, &prot
, &page_size
);
1246 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1248 int cp_num
= (insn
>> 8) & 0xf;
1249 int cp_info
= (insn
>> 5) & 7;
1250 int src
= (insn
>> 16) & 0xf;
1251 int operand
= insn
& 0xf;
1253 if (env
->cp
[cp_num
].cp_write
)
1254 env
->cp
[cp_num
].cp_write(env
->cp
[cp_num
].opaque
,
1255 cp_info
, src
, operand
, val
);
1258 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
1260 int cp_num
= (insn
>> 8) & 0xf;
1261 int cp_info
= (insn
>> 5) & 7;
1262 int dest
= (insn
>> 16) & 0xf;
1263 int operand
= insn
& 0xf;
1265 if (env
->cp
[cp_num
].cp_read
)
1266 return env
->cp
[cp_num
].cp_read(env
->cp
[cp_num
].opaque
,
1267 cp_info
, dest
, operand
);
1271 /* Return basic MPU access permission bits. */
1272 static uint32_t simple_mpu_ap_bits(uint32_t val
)
1279 for (i
= 0; i
< 16; i
+= 2) {
1280 ret
|= (val
>> i
) & mask
;
1286 /* Pad basic MPU access permission bits to extended format. */
1287 static uint32_t extended_mpu_ap_bits(uint32_t val
)
1294 for (i
= 0; i
< 16; i
+= 2) {
1295 ret
|= (val
& mask
) << i
;
1301 void HELPER(set_cp15
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
1307 op1
= (insn
>> 21) & 7;
1308 op2
= (insn
>> 5) & 7;
1310 switch ((insn
>> 16) & 0xf) {
1313 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1315 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1317 if (arm_feature(env
, ARM_FEATURE_V7
)
1318 && op1
== 2 && crm
== 0 && op2
== 0) {
1319 env
->cp15
.c0_cssel
= val
& 0xf;
1323 case 1: /* System configuration. */
1324 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1328 if (!arm_feature(env
, ARM_FEATURE_XSCALE
) || crm
== 0)
1329 env
->cp15
.c1_sys
= val
;
1330 /* ??? Lots of these bits are not implemented. */
1331 /* This may enable/disable the MMU, so do a TLB flush. */
1334 case 1: /* Auxiliary cotrol register. */
1335 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1336 env
->cp15
.c1_xscaleauxcr
= val
;
1339 /* Not implemented. */
1342 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1344 if (env
->cp15
.c1_coproc
!= val
) {
1345 env
->cp15
.c1_coproc
= val
;
1346 /* ??? Is this safe when called from within a TB? */
1354 case 2: /* MMU Page table control / MPU cache control. */
1355 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1358 env
->cp15
.c2_data
= val
;
1361 env
->cp15
.c2_insn
= val
;
1369 env
->cp15
.c2_base0
= val
;
1372 env
->cp15
.c2_base1
= val
;
1376 env
->cp15
.c2_control
= val
;
1377 env
->cp15
.c2_mask
= ~(((uint32_t)0xffffffffu
) >> val
);
1378 env
->cp15
.c2_base_mask
= ~((uint32_t)0x3fffu
>> val
);
1385 case 3: /* MMU Domain access control / MPU write buffer control. */
1387 tlb_flush(env
, 1); /* Flush TLB as domain not tracked in TLB */
1389 case 4: /* Reserved. */
1391 case 5: /* MMU Fault status / MPU access permission. */
1392 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1396 if (arm_feature(env
, ARM_FEATURE_MPU
))
1397 val
= extended_mpu_ap_bits(val
);
1398 env
->cp15
.c5_data
= val
;
1401 if (arm_feature(env
, ARM_FEATURE_MPU
))
1402 val
= extended_mpu_ap_bits(val
);
1403 env
->cp15
.c5_insn
= val
;
1406 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1408 env
->cp15
.c5_data
= val
;
1411 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1413 env
->cp15
.c5_insn
= val
;
1419 case 6: /* MMU Fault address / MPU base/size. */
1420 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1423 env
->cp15
.c6_region
[crm
] = val
;
1425 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1429 env
->cp15
.c6_data
= val
;
1431 case 1: /* ??? This is WFAR on armv6 */
1433 env
->cp15
.c6_insn
= val
;
1440 case 7: /* Cache control. */
1441 env
->cp15
.c15_i_max
= 0x000;
1442 env
->cp15
.c15_i_min
= 0xff0;
1443 /* No cache, so nothing to do. */
1444 /* ??? MPCore has VA to PA translation functions. */
1446 case 8: /* MMU TLB control. */
1448 case 0: /* Invalidate all. */
1451 case 1: /* Invalidate single TLB entry. */
1452 tlb_flush_page(env
, val
& TARGET_PAGE_MASK
);
1454 case 2: /* Invalidate on ASID. */
1455 tlb_flush(env
, val
== 0);
1457 case 3: /* Invalidate single entry on MVA. */
1458 /* ??? This is like case 1, but ignores ASID. */
1466 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1469 case 0: /* Cache lockdown. */
1471 case 0: /* L1 cache. */
1474 env
->cp15
.c9_data
= val
;
1477 env
->cp15
.c9_insn
= val
;
1483 case 1: /* L2 cache. */
1484 /* Ignore writes to L2 lockdown/auxiliary registers. */
1490 case 1: /* TCM memory region registers. */
1491 /* Not implemented. */
1497 case 10: /* MMU TLB lockdown. */
1498 /* ??? TLB lockdown not implemented. */
1500 case 12: /* Reserved. */
1502 case 13: /* Process ID. */
1505 /* Unlike real hardware the qemu TLB uses virtual addresses,
1506 not modified virtual addresses, so this causes a TLB flush.
1508 if (env
->cp15
.c13_fcse
!= val
)
1510 env
->cp15
.c13_fcse
= val
;
1513 /* This changes the ASID, so do a TLB flush. */
1514 if (env
->cp15
.c13_context
!= val
1515 && !arm_feature(env
, ARM_FEATURE_MPU
))
1517 env
->cp15
.c13_context
= val
;
1523 case 14: /* Reserved. */
1525 case 15: /* Implementation specific. */
1526 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1527 if (op2
== 0 && crm
== 1) {
1528 if (env
->cp15
.c15_cpar
!= (val
& 0x3fff)) {
1529 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1531 env
->cp15
.c15_cpar
= val
& 0x3fff;
1537 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1541 case 1: /* Set TI925T configuration. */
1542 env
->cp15
.c15_ticonfig
= val
& 0xe7;
1543 env
->cp15
.c0_cpuid
= (val
& (1 << 5)) ? /* OS_TYPE bit */
1544 ARM_CPUID_TI915T
: ARM_CPUID_TI925T
;
1546 case 2: /* Set I_max. */
1547 env
->cp15
.c15_i_max
= val
;
1549 case 3: /* Set I_min. */
1550 env
->cp15
.c15_i_min
= val
;
1552 case 4: /* Set thread-ID. */
1553 env
->cp15
.c15_threadid
= val
& 0xffff;
1555 case 8: /* Wait-for-interrupt (deprecated). */
1556 cpu_interrupt(env
, CPU_INTERRUPT_HALT
);
1566 /* ??? For debugging only. Should raise illegal instruction exception. */
1567 cpu_abort(env
, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1568 (insn
>> 16) & 0xf, crm
, op1
, op2
);
1571 uint32_t HELPER(get_cp15
)(CPUState
*env
, uint32_t insn
)
1577 op1
= (insn
>> 21) & 7;
1578 op2
= (insn
>> 5) & 7;
1580 switch ((insn
>> 16) & 0xf) {
1581 case 0: /* ID codes. */
1587 case 0: /* Device ID. */
1588 return env
->cp15
.c0_cpuid
;
1589 case 1: /* Cache Type. */
1590 return env
->cp15
.c0_cachetype
;
1591 case 2: /* TCM status. */
1593 case 3: /* TLB type register. */
1594 return 0; /* No lockable TLB entries. */
1595 case 5: /* CPU ID */
1596 if (ARM_CPUID(env
) == ARM_CPUID_CORTEXA9
) {
1597 return env
->cpu_index
| 0x80000900;
1599 return env
->cpu_index
;
1605 if (!arm_feature(env
, ARM_FEATURE_V6
))
1607 return env
->cp15
.c0_c1
[op2
];
1609 if (!arm_feature(env
, ARM_FEATURE_V6
))
1611 return env
->cp15
.c0_c2
[op2
];
1612 case 3: case 4: case 5: case 6: case 7:
1618 /* These registers aren't documented on arm11 cores. However
1619 Linux looks at them anyway. */
1620 if (!arm_feature(env
, ARM_FEATURE_V6
))
1624 if (!arm_feature(env
, ARM_FEATURE_V7
))
1629 return env
->cp15
.c0_ccsid
[env
->cp15
.c0_cssel
];
1631 return env
->cp15
.c0_clid
;
1637 if (op2
!= 0 || crm
!= 0)
1639 return env
->cp15
.c0_cssel
;
1643 case 1: /* System configuration. */
1644 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1647 case 0: /* Control register. */
1648 return env
->cp15
.c1_sys
;
1649 case 1: /* Auxiliary control register. */
1650 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1651 return env
->cp15
.c1_xscaleauxcr
;
1652 if (!arm_feature(env
, ARM_FEATURE_AUXCR
))
1654 switch (ARM_CPUID(env
)) {
1655 case ARM_CPUID_ARM1026
:
1657 case ARM_CPUID_ARM1136
:
1658 case ARM_CPUID_ARM1136_R2
:
1660 case ARM_CPUID_ARM11MPCORE
:
1662 case ARM_CPUID_CORTEXA8
:
1664 case ARM_CPUID_CORTEXA9
:
1669 case 2: /* Coprocessor access register. */
1670 if (arm_feature(env
, ARM_FEATURE_XSCALE
))
1672 return env
->cp15
.c1_coproc
;
1676 case 2: /* MMU Page table control / MPU cache control. */
1677 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1680 return env
->cp15
.c2_data
;
1683 return env
->cp15
.c2_insn
;
1691 return env
->cp15
.c2_base0
;
1693 return env
->cp15
.c2_base1
;
1695 return env
->cp15
.c2_control
;
1700 case 3: /* MMU Domain access control / MPU write buffer control. */
1701 return env
->cp15
.c3
;
1702 case 4: /* Reserved. */
1704 case 5: /* MMU Fault status / MPU access permission. */
1705 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1709 if (arm_feature(env
, ARM_FEATURE_MPU
))
1710 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1711 return env
->cp15
.c5_data
;
1713 if (arm_feature(env
, ARM_FEATURE_MPU
))
1714 return simple_mpu_ap_bits(env
->cp15
.c5_data
);
1715 return env
->cp15
.c5_insn
;
1717 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1719 return env
->cp15
.c5_data
;
1721 if (!arm_feature(env
, ARM_FEATURE_MPU
))
1723 return env
->cp15
.c5_insn
;
1727 case 6: /* MMU Fault address. */
1728 if (arm_feature(env
, ARM_FEATURE_MPU
)) {
1731 return env
->cp15
.c6_region
[crm
];
1733 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1737 return env
->cp15
.c6_data
;
1739 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1740 /* Watchpoint Fault Adrress. */
1741 return 0; /* Not implemented. */
1743 /* Instruction Fault Adrress. */
1744 /* Arm9 doesn't have an IFAR, but implementing it anyway
1745 shouldn't do any harm. */
1746 return env
->cp15
.c6_insn
;
1749 if (arm_feature(env
, ARM_FEATURE_V6
)) {
1750 /* Instruction Fault Adrress. */
1751 return env
->cp15
.c6_insn
;
1759 case 7: /* Cache control. */
1760 /* FIXME: Should only clear Z flag if destination is r15. */
1763 case 8: /* MMU TLB control. */
1765 case 9: /* Cache lockdown. */
1767 case 0: /* L1 cache. */
1768 if (arm_feature(env
, ARM_FEATURE_OMAPCP
))
1772 return env
->cp15
.c9_data
;
1774 return env
->cp15
.c9_insn
;
1778 case 1: /* L2 cache */
1781 /* L2 Lockdown and Auxiliary control. */
1786 case 10: /* MMU TLB lockdown. */
1787 /* ??? TLB lockdown not implemented. */
1789 case 11: /* TCM DMA control. */
1790 case 12: /* Reserved. */
1792 case 13: /* Process ID. */
1795 return env
->cp15
.c13_fcse
;
1797 return env
->cp15
.c13_context
;
1801 case 14: /* Reserved. */
1803 case 15: /* Implementation specific. */
1804 if (arm_feature(env
, ARM_FEATURE_XSCALE
)) {
1805 if (op2
== 0 && crm
== 1)
1806 return env
->cp15
.c15_cpar
;
1810 if (arm_feature(env
, ARM_FEATURE_OMAPCP
)) {
1814 case 1: /* Read TI925T configuration. */
1815 return env
->cp15
.c15_ticonfig
;
1816 case 2: /* Read I_max. */
1817 return env
->cp15
.c15_i_max
;
1818 case 3: /* Read I_min. */
1819 return env
->cp15
.c15_i_min
;
1820 case 4: /* Read thread-ID. */
1821 return env
->cp15
.c15_threadid
;
1822 case 8: /* TI925T_status */
1825 /* TODO: Peripheral port remap register:
1826 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1827 * controller base address at $rn & ~0xfff and map size of
1828 * 0x200 << ($rn & 0xfff), when MMU is off. */
1834 /* ??? For debugging only. Should raise illegal instruction exception. */
1835 cpu_abort(env
, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1836 (insn
>> 16) & 0xf, crm
, op1
, op2
);
1840 void HELPER(set_r13_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
1842 env
->banked_r13
[bank_number(mode
)] = val
;
1845 uint32_t HELPER(get_r13_banked
)(CPUState
*env
, uint32_t mode
)
1847 return env
->banked_r13
[bank_number(mode
)];
1850 uint32_t HELPER(v7m_mrs
)(CPUState
*env
, uint32_t reg
)
1854 return xpsr_read(env
) & 0xf8000000;
1856 return xpsr_read(env
) & 0xf80001ff;
1858 return xpsr_read(env
) & 0xff00fc00;
1860 return xpsr_read(env
) & 0xff00fdff;
1862 return xpsr_read(env
) & 0x000001ff;
1864 return xpsr_read(env
) & 0x0700fc00;
1866 return xpsr_read(env
) & 0x0700edff;
1868 return env
->v7m
.current_sp
? env
->v7m
.other_sp
: env
->regs
[13];
1870 return env
->v7m
.current_sp
? env
->regs
[13] : env
->v7m
.other_sp
;
1871 case 16: /* PRIMASK */
1872 return (env
->uncached_cpsr
& CPSR_I
) != 0;
1873 case 17: /* FAULTMASK */
1874 return (env
->uncached_cpsr
& CPSR_F
) != 0;
1875 case 18: /* BASEPRI */
1876 case 19: /* BASEPRI_MAX */
1877 return env
->v7m
.basepri
;
1878 case 20: /* CONTROL */
1879 return env
->v7m
.control
;
1881 /* ??? For debugging only. */
1882 cpu_abort(env
, "Unimplemented system register read (%d)\n", reg
);
1887 void HELPER(v7m_msr
)(CPUState
*env
, uint32_t reg
, uint32_t val
)
1891 xpsr_write(env
, val
, 0xf8000000);
1894 xpsr_write(env
, val
, 0xf8000000);
1897 xpsr_write(env
, val
, 0xfe00fc00);
1900 xpsr_write(env
, val
, 0xfe00fc00);
1903 /* IPSR bits are readonly. */
1906 xpsr_write(env
, val
, 0x0600fc00);
1909 xpsr_write(env
, val
, 0x0600fc00);
1912 if (env
->v7m
.current_sp
)
1913 env
->v7m
.other_sp
= val
;
1915 env
->regs
[13] = val
;
1918 if (env
->v7m
.current_sp
)
1919 env
->regs
[13] = val
;
1921 env
->v7m
.other_sp
= val
;
1923 case 16: /* PRIMASK */
1925 env
->uncached_cpsr
|= CPSR_I
;
1927 env
->uncached_cpsr
&= ~CPSR_I
;
1929 case 17: /* FAULTMASK */
1931 env
->uncached_cpsr
|= CPSR_F
;
1933 env
->uncached_cpsr
&= ~CPSR_F
;
1935 case 18: /* BASEPRI */
1936 env
->v7m
.basepri
= val
& 0xff;
1938 case 19: /* BASEPRI_MAX */
1940 if (val
!= 0 && (val
< env
->v7m
.basepri
|| env
->v7m
.basepri
== 0))
1941 env
->v7m
.basepri
= val
;
1943 case 20: /* CONTROL */
1944 env
->v7m
.control
= val
& 3;
1945 switch_v7m_sp(env
, (val
& 2) != 0);
1948 /* ??? For debugging only. */
1949 cpu_abort(env
, "Unimplemented system register write (%d)\n", reg
);
1954 void cpu_arm_set_cp_io(CPUARMState
*env
, int cpnum
,
1955 ARMReadCPFunc
*cp_read
, ARMWriteCPFunc
*cp_write
,
1958 if (cpnum
< 0 || cpnum
> 14) {
1959 cpu_abort(env
, "Bad coprocessor number: %i\n", cpnum
);
1963 env
->cp
[cpnum
].cp_read
= cp_read
;
1964 env
->cp
[cpnum
].cp_write
= cp_write
;
1965 env
->cp
[cpnum
].opaque
= opaque
;
1970 /* Note that signed overflow is undefined in C. The following routines are
1971 careful to use unsigned types where modulo arithmetic is required.
1972 Failure to do so _will_ break on newer gcc. */
1974 /* Signed saturating arithmetic. */
1976 /* Perform 16-bit signed saturating addition. */
1977 static inline uint16_t add16_sat(uint16_t a
, uint16_t b
)
1982 if (((res
^ a
) & 0x8000) && !((a
^ b
) & 0x8000)) {
1991 /* Perform 8-bit signed saturating addition. */
1992 static inline uint8_t add8_sat(uint8_t a
, uint8_t b
)
1997 if (((res
^ a
) & 0x80) && !((a
^ b
) & 0x80)) {
2006 /* Perform 16-bit signed saturating subtraction. */
2007 static inline uint16_t sub16_sat(uint16_t a
, uint16_t b
)
2012 if (((res
^ a
) & 0x8000) && ((a
^ b
) & 0x8000)) {
2021 /* Perform 8-bit signed saturating subtraction. */
2022 static inline uint8_t sub8_sat(uint8_t a
, uint8_t b
)
2027 if (((res
^ a
) & 0x80) && ((a
^ b
) & 0x80)) {
2036 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2037 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2038 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2039 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2042 #include "op_addsub.h"
2044 /* Unsigned saturating arithmetic. */
2045 static inline uint16_t add16_usat(uint16_t a
, uint16_t b
)
2054 static inline uint16_t sub16_usat(uint16_t a
, uint16_t b
)
2062 static inline uint8_t add8_usat(uint8_t a
, uint8_t b
)
2071 static inline uint8_t sub8_usat(uint8_t a
, uint8_t b
)
2079 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2080 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2081 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2082 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2085 #include "op_addsub.h"
2087 /* Signed modulo arithmetic. */
2088 #define SARITH16(a, b, n, op) do { \
2090 sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2091 RESULT(sum, n, 16); \
2093 ge |= 3 << (n * 2); \
2096 #define SARITH8(a, b, n, op) do { \
2098 sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2099 RESULT(sum, n, 8); \
2105 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2106 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2107 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2108 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2112 #include "op_addsub.h"
2114 /* Unsigned modulo arithmetic. */
2115 #define ADD16(a, b, n) do { \
2117 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2118 RESULT(sum, n, 16); \
2119 if ((sum >> 16) == 1) \
2120 ge |= 3 << (n * 2); \
2123 #define ADD8(a, b, n) do { \
2125 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2126 RESULT(sum, n, 8); \
2127 if ((sum >> 8) == 1) \
2131 #define SUB16(a, b, n) do { \
2133 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2134 RESULT(sum, n, 16); \
2135 if ((sum >> 16) == 0) \
2136 ge |= 3 << (n * 2); \
2139 #define SUB8(a, b, n) do { \
2141 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2142 RESULT(sum, n, 8); \
2143 if ((sum >> 8) == 0) \
2150 #include "op_addsub.h"
2152 /* Halved signed arithmetic. */
2153 #define ADD16(a, b, n) \
2154 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2155 #define SUB16(a, b, n) \
2156 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2157 #define ADD8(a, b, n) \
2158 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2159 #define SUB8(a, b, n) \
2160 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2163 #include "op_addsub.h"
2165 /* Halved unsigned arithmetic. */
2166 #define ADD16(a, b, n) \
2167 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2168 #define SUB16(a, b, n) \
2169 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2170 #define ADD8(a, b, n) \
2171 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2172 #define SUB8(a, b, n) \
2173 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2176 #include "op_addsub.h"
2178 static inline uint8_t do_usad(uint8_t a
, uint8_t b
)
2186 /* Unsigned sum of absolute byte differences. */
2187 uint32_t HELPER(usad8
)(uint32_t a
, uint32_t b
)
2190 sum
= do_usad(a
, b
);
2191 sum
+= do_usad(a
>> 8, b
>> 8);
2192 sum
+= do_usad(a
>> 16, b
>>16);
2193 sum
+= do_usad(a
>> 24, b
>> 24);
2197 /* For ARMv6 SEL instruction. */
2198 uint32_t HELPER(sel_flags
)(uint32_t flags
, uint32_t a
, uint32_t b
)
2211 return (a
& mask
) | (b
& ~mask
);
2214 uint32_t HELPER(logicq_cc
)(uint64_t val
)
2216 return (val
>> 32) | (val
!= 0);
2219 /* VFP support. We follow the convention used for VFP instrunctions:
2220 Single precition routines have a "s" suffix, double precision a
2223 /* Convert host exception flags to vfp form. */
2224 static inline int vfp_exceptbits_from_host(int host_bits
)
2226 int target_bits
= 0;
2228 if (host_bits
& float_flag_invalid
)
2230 if (host_bits
& float_flag_divbyzero
)
2232 if (host_bits
& float_flag_overflow
)
2234 if (host_bits
& float_flag_underflow
)
2236 if (host_bits
& float_flag_inexact
)
2237 target_bits
|= 0x10;
2241 uint32_t HELPER(vfp_get_fpscr
)(CPUState
*env
)
2246 fpscr
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & 0xffc8ffff)
2247 | (env
->vfp
.vec_len
<< 16)
2248 | (env
->vfp
.vec_stride
<< 20);
2249 i
= get_float_exception_flags(&env
->vfp
.fp_status
);
2250 fpscr
|= vfp_exceptbits_from_host(i
);
2254 uint32_t vfp_get_fpscr(CPUState
*env
)
2256 return HELPER(vfp_get_fpscr
)(env
);
2259 /* Convert vfp exception flags to target form. */
2260 static inline int vfp_exceptbits_to_host(int target_bits
)
2264 if (target_bits
& 1)
2265 host_bits
|= float_flag_invalid
;
2266 if (target_bits
& 2)
2267 host_bits
|= float_flag_divbyzero
;
2268 if (target_bits
& 4)
2269 host_bits
|= float_flag_overflow
;
2270 if (target_bits
& 8)
2271 host_bits
|= float_flag_underflow
;
2272 if (target_bits
& 0x10)
2273 host_bits
|= float_flag_inexact
;
2277 void HELPER(vfp_set_fpscr
)(CPUState
*env
, uint32_t val
)
2282 changed
= env
->vfp
.xregs
[ARM_VFP_FPSCR
];
2283 env
->vfp
.xregs
[ARM_VFP_FPSCR
] = (val
& 0xffc8ffff);
2284 env
->vfp
.vec_len
= (val
>> 16) & 7;
2285 env
->vfp
.vec_stride
= (val
>> 20) & 3;
2288 if (changed
& (3 << 22)) {
2289 i
= (val
>> 22) & 3;
2292 i
= float_round_nearest_even
;
2298 i
= float_round_down
;
2301 i
= float_round_to_zero
;
2304 set_float_rounding_mode(i
, &env
->vfp
.fp_status
);
2306 if (changed
& (1 << 24))
2307 set_flush_to_zero((val
& (1 << 24)) != 0, &env
->vfp
.fp_status
);
2308 if (changed
& (1 << 25))
2309 set_default_nan_mode((val
& (1 << 25)) != 0, &env
->vfp
.fp_status
);
2311 i
= vfp_exceptbits_to_host((val
>> 8) & 0x1f);
2312 set_float_exception_flags(i
, &env
->vfp
.fp_status
);
2315 void vfp_set_fpscr(CPUState
*env
, uint32_t val
)
2317 HELPER(vfp_set_fpscr
)(env
, val
);
2320 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2322 #define VFP_BINOP(name) \
2323 float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2325 return float32_ ## name (a, b, &env->vfp.fp_status); \
2327 float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2329 return float64_ ## name (a, b, &env->vfp.fp_status); \
2337 float32
VFP_HELPER(neg
, s
)(float32 a
)
2339 return float32_chs(a
);
2342 float64
VFP_HELPER(neg
, d
)(float64 a
)
2344 return float64_chs(a
);
2347 float32
VFP_HELPER(abs
, s
)(float32 a
)
2349 return float32_abs(a
);
2352 float64
VFP_HELPER(abs
, d
)(float64 a
)
2354 return float64_abs(a
);
2357 float32
VFP_HELPER(sqrt
, s
)(float32 a
, CPUState
*env
)
2359 return float32_sqrt(a
, &env
->vfp
.fp_status
);
2362 float64
VFP_HELPER(sqrt
, d
)(float64 a
, CPUState
*env
)
2364 return float64_sqrt(a
, &env
->vfp
.fp_status
);
2367 /* XXX: check quiet/signaling case */
2368 #define DO_VFP_cmp(p, type) \
2369 void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2372 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2373 case 0: flags = 0x6; break; \
2374 case -1: flags = 0x8; break; \
2375 case 1: flags = 0x2; break; \
2376 default: case 2: flags = 0x3; break; \
2378 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2379 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2381 void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2384 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2385 case 0: flags = 0x6; break; \
2386 case -1: flags = 0x8; break; \
2387 case 1: flags = 0x2; break; \
2388 default: case 2: flags = 0x3; break; \
2390 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2391 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2393 DO_VFP_cmp(s
, float32
)
2394 DO_VFP_cmp(d
, float64
)
2397 /* Helper routines to perform bitwise copies between float and int. */
2398 static inline float32
vfp_itos(uint32_t i
)
2409 static inline uint32_t vfp_stoi(float32 s
)
2420 static inline float64
vfp_itod(uint64_t i
)
2431 static inline uint64_t vfp_dtoi(float64 d
)
2442 /* Integer to float conversion. */
2443 float32
VFP_HELPER(uito
, s
)(float32 x
, CPUState
*env
)
2445 return uint32_to_float32(vfp_stoi(x
), &env
->vfp
.fp_status
);
2448 float64
VFP_HELPER(uito
, d
)(float32 x
, CPUState
*env
)
2450 return uint32_to_float64(vfp_stoi(x
), &env
->vfp
.fp_status
);
2453 float32
VFP_HELPER(sito
, s
)(float32 x
, CPUState
*env
)
2455 return int32_to_float32(vfp_stoi(x
), &env
->vfp
.fp_status
);
2458 float64
VFP_HELPER(sito
, d
)(float32 x
, CPUState
*env
)
2460 return int32_to_float64(vfp_stoi(x
), &env
->vfp
.fp_status
);
2463 /* Float to integer conversion. */
2464 float32
VFP_HELPER(toui
, s
)(float32 x
, CPUState
*env
)
2466 return vfp_itos(float32_to_uint32(x
, &env
->vfp
.fp_status
));
2469 float32
VFP_HELPER(toui
, d
)(float64 x
, CPUState
*env
)
2471 return vfp_itos(float64_to_uint32(x
, &env
->vfp
.fp_status
));
2474 float32
VFP_HELPER(tosi
, s
)(float32 x
, CPUState
*env
)
2476 return vfp_itos(float32_to_int32(x
, &env
->vfp
.fp_status
));
2479 float32
VFP_HELPER(tosi
, d
)(float64 x
, CPUState
*env
)
2481 return vfp_itos(float64_to_int32(x
, &env
->vfp
.fp_status
));
2484 float32
VFP_HELPER(touiz
, s
)(float32 x
, CPUState
*env
)
2486 return vfp_itos(float32_to_uint32_round_to_zero(x
, &env
->vfp
.fp_status
));
2489 float32
VFP_HELPER(touiz
, d
)(float64 x
, CPUState
*env
)
2491 return vfp_itos(float64_to_uint32_round_to_zero(x
, &env
->vfp
.fp_status
));
2494 float32
VFP_HELPER(tosiz
, s
)(float32 x
, CPUState
*env
)
2496 return vfp_itos(float32_to_int32_round_to_zero(x
, &env
->vfp
.fp_status
));
2499 float32
VFP_HELPER(tosiz
, d
)(float64 x
, CPUState
*env
)
2501 return vfp_itos(float64_to_int32_round_to_zero(x
, &env
->vfp
.fp_status
));
2504 /* floating point conversion */
2505 float64
VFP_HELPER(fcvtd
, s
)(float32 x
, CPUState
*env
)
2507 return float32_to_float64(x
, &env
->vfp
.fp_status
);
2510 float32
VFP_HELPER(fcvts
, d
)(float64 x
, CPUState
*env
)
2512 return float64_to_float32(x
, &env
->vfp
.fp_status
);
2515 /* VFP3 fixed point conversion. */
2516 #define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2517 ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2520 tmp = sign##int32_to_##ftype ((itype)vfp_##p##toi(x), \
2521 &env->vfp.fp_status); \
2522 return ftype##_scalbn(tmp, -(int)shift, &env->vfp.fp_status); \
2524 ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2527 tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2528 return vfp_ito##p((itype)ftype##_to_##sign##int32_round_to_zero(tmp, \
2529 &env->vfp.fp_status)); \
2532 VFP_CONV_FIX(sh
, d
, float64
, int16
, )
2533 VFP_CONV_FIX(sl
, d
, float64
, int32
, )
2534 VFP_CONV_FIX(uh
, d
, float64
, uint16
, u
)
2535 VFP_CONV_FIX(ul
, d
, float64
, uint32
, u
)
2536 VFP_CONV_FIX(sh
, s
, float32
, int16
, )
2537 VFP_CONV_FIX(sl
, s
, float32
, int32
, )
2538 VFP_CONV_FIX(uh
, s
, float32
, uint16
, u
)
2539 VFP_CONV_FIX(ul
, s
, float32
, uint32
, u
)
2542 /* Half precision conversions. */
2543 float32
HELPER(vfp_fcvt_f16_to_f32
)(uint32_t a
, CPUState
*env
)
2545 float_status
*s
= &env
->vfp
.fp_status
;
2546 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2547 return float16_to_float32(a
, ieee
, s
);
2550 uint32_t HELPER(vfp_fcvt_f32_to_f16
)(float32 a
, CPUState
*env
)
2552 float_status
*s
= &env
->vfp
.fp_status
;
2553 int ieee
= (env
->vfp
.xregs
[ARM_VFP_FPSCR
] & (1 << 26)) == 0;
2554 return float32_to_float16(a
, ieee
, s
);
2557 float32
HELPER(recps_f32
)(float32 a
, float32 b
, CPUState
*env
)
2559 float_status
*s
= &env
->vfp
.fp_status
;
2560 float32 two
= int32_to_float32(2, s
);
2561 return float32_sub(two
, float32_mul(a
, b
, s
), s
);
2564 float32
HELPER(rsqrts_f32
)(float32 a
, float32 b
, CPUState
*env
)
2566 float_status
*s
= &env
->vfp
.fp_status
;
2567 float32 three
= int32_to_float32(3, s
);
2568 return float32_sub(three
, float32_mul(a
, b
, s
), s
);
2573 /* TODO: The architecture specifies the value that the estimate functions
2574 should return. We return the exact reciprocal/root instead. */
2575 float32
HELPER(recpe_f32
)(float32 a
, CPUState
*env
)
2577 float_status
*s
= &env
->vfp
.fp_status
;
2578 float32 one
= int32_to_float32(1, s
);
2579 return float32_div(one
, a
, s
);
2582 float32
HELPER(rsqrte_f32
)(float32 a
, CPUState
*env
)
2584 float_status
*s
= &env
->vfp
.fp_status
;
2585 float32 one
= int32_to_float32(1, s
);
2586 return float32_div(one
, float32_sqrt(a
, s
), s
);
2589 uint32_t HELPER(recpe_u32
)(uint32_t a
, CPUState
*env
)
2591 float_status
*s
= &env
->vfp
.fp_status
;
2593 tmp
= int32_to_float32(a
, s
);
2594 tmp
= float32_scalbn(tmp
, -32, s
);
2595 tmp
= helper_recpe_f32(tmp
, env
);
2596 tmp
= float32_scalbn(tmp
, 31, s
);
2597 return float32_to_int32(tmp
, s
);
2600 uint32_t HELPER(rsqrte_u32
)(uint32_t a
, CPUState
*env
)
2602 float_status
*s
= &env
->vfp
.fp_status
;
2604 tmp
= int32_to_float32(a
, s
);
2605 tmp
= float32_scalbn(tmp
, -32, s
);
2606 tmp
= helper_rsqrte_f32(tmp
, env
);
2607 tmp
= float32_scalbn(tmp
, 31, s
);
2608 return float32_to_int32(tmp
, s
);
2611 void HELPER(set_teecr
)(CPUState
*env
, uint32_t val
)
2614 if (env
->teecr
!= val
) {