2 * MIPS emulation helpers for qemu.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
24 #include "exec/helper-proto.h"
25 #include "exec/exec-all.h"
26 #include "exec/memop.h"
27 #include "fpu_helper.h"
29 static inline target_ulong
bitswap(target_ulong v
)
31 v
= ((v
>> 1) & (target_ulong
)0x5555555555555555ULL
) |
32 ((v
& (target_ulong
)0x5555555555555555ULL
) << 1);
33 v
= ((v
>> 2) & (target_ulong
)0x3333333333333333ULL
) |
34 ((v
& (target_ulong
)0x3333333333333333ULL
) << 2);
35 v
= ((v
>> 4) & (target_ulong
)0x0F0F0F0F0F0F0F0FULL
) |
36 ((v
& (target_ulong
)0x0F0F0F0F0F0F0F0FULL
) << 4);
41 target_ulong
helper_dbitswap(target_ulong rt
)
47 target_ulong
helper_bitswap(target_ulong rt
)
49 return (int32_t)bitswap(rt
);
52 target_ulong
helper_rotx(target_ulong rs
, uint32_t shift
, uint32_t shiftx
,
56 uint64_t tmp0
= ((uint64_t)rs
) << 32 | ((uint64_t)rs
& 0xffffffff);
58 for (i
= 0; i
<= 46; i
++) {
66 if (stripe
!= 0 && !(i
& 0x4)) {
70 if (tmp0
& (1LL << (i
+ 16))) {
79 for (i
= 0; i
<= 38; i
++) {
88 if (tmp1
& (1LL << (i
+ 8))) {
97 for (i
= 0; i
<= 34; i
++) {
105 if (tmp2
& (1LL << (i
+ 4))) {
113 uint64_t tmp4
= tmp3
;
114 for (i
= 0; i
<= 32; i
++) {
122 if (tmp3
& (1LL << (i
+ 2))) {
130 uint64_t tmp5
= tmp4
;
131 for (i
= 0; i
<= 31; i
++) {
135 if (tmp4
& (1LL << (i
+ 1))) {
143 return (int64_t)(int32_t)(uint32_t)tmp5
;
146 void helper_fork(target_ulong arg1
, target_ulong arg2
)
149 * arg1 = rt, arg2 = rs
150 * TODO: store to TC register
154 target_ulong
helper_yield(CPUMIPSState
*env
, target_ulong arg
)
156 target_long arg1
= arg
;
159 /* No scheduling policy implemented. */
161 if (env
->CP0_VPEControl
& (1 << CP0VPECo_YSI
) &&
162 env
->active_tc
.CP0_TCStatus
& (1 << CP0TCSt_DT
)) {
163 env
->CP0_VPEControl
&= ~(0x7 << CP0VPECo_EXCPT
);
164 env
->CP0_VPEControl
|= 4 << CP0VPECo_EXCPT
;
165 do_raise_exception(env
, EXCP_THREAD
, GETPC());
168 } else if (arg1
== 0) {
170 /* TODO: TC underflow */
171 env
->CP0_VPEControl
&= ~(0x7 << CP0VPECo_EXCPT
);
172 do_raise_exception(env
, EXCP_THREAD
, GETPC());
174 /* TODO: Deallocate TC */
176 } else if (arg1
> 0) {
177 /* Yield qualifier inputs not implemented. */
178 env
->CP0_VPEControl
&= ~(0x7 << CP0VPECo_EXCPT
);
179 env
->CP0_VPEControl
|= 2 << CP0VPECo_EXCPT
;
180 do_raise_exception(env
, EXCP_THREAD
, GETPC());
182 return env
->CP0_YQMask
;
185 static inline void check_hwrena(CPUMIPSState
*env
, int reg
, uintptr_t pc
)
187 if ((env
->hflags
& MIPS_HFLAG_CP0
) || (env
->CP0_HWREna
& (1 << reg
))) {
190 do_raise_exception(env
, EXCP_RI
, pc
);
193 target_ulong
helper_rdhwr_cpunum(CPUMIPSState
*env
)
195 check_hwrena(env
, 0, GETPC());
196 return env
->CP0_EBase
& 0x3ff;
199 target_ulong
helper_rdhwr_synci_step(CPUMIPSState
*env
)
201 check_hwrena(env
, 1, GETPC());
202 return env
->SYNCI_Step
;
205 target_ulong
helper_rdhwr_cc(CPUMIPSState
*env
)
207 check_hwrena(env
, 2, GETPC());
208 #ifdef CONFIG_USER_ONLY
209 return env
->CP0_Count
;
211 return (int32_t)cpu_mips_get_count(env
);
215 target_ulong
helper_rdhwr_ccres(CPUMIPSState
*env
)
217 check_hwrena(env
, 3, GETPC());
221 target_ulong
helper_rdhwr_performance(CPUMIPSState
*env
)
223 check_hwrena(env
, 4, GETPC());
224 return env
->CP0_Performance0
;
227 target_ulong
helper_rdhwr_xnp(CPUMIPSState
*env
)
229 check_hwrena(env
, 5, GETPC());
230 return (env
->CP0_Config5
>> CP0C5_XNP
) & 1;
233 void helper_pmon(CPUMIPSState
*env
, int function
)
237 case 2: /* TODO: char inbyte(int waitflag); */
238 if (env
->active_tc
.gpr
[4] == 0) {
239 env
->active_tc
.gpr
[2] = -1;
242 case 11: /* TODO: char inbyte (void); */
243 env
->active_tc
.gpr
[2] = -1;
247 printf("%c", (char)(env
->active_tc
.gpr
[4] & 0xFF));
253 unsigned char *fmt
= (void *)(uintptr_t)env
->active_tc
.gpr
[4];
261 target_ulong
helper_lcsr_cpucfg(CPUMIPSState
*env
, target_ulong rs
)
265 return env
->CP0_PRid
;
267 return env
->lcsr_cpucfg1
;
269 return env
->lcsr_cpucfg2
;
276 #if !defined(CONFIG_USER_ONLY)
278 void mips_cpu_do_unaligned_access(CPUState
*cs
, vaddr addr
,
279 MMUAccessType access_type
,
280 int mmu_idx
, uintptr_t retaddr
)
282 MIPSCPU
*cpu
= MIPS_CPU(cs
);
283 CPUMIPSState
*env
= &cpu
->env
;
287 if (!(env
->hflags
& MIPS_HFLAG_DM
)) {
288 env
->CP0_BadVAddr
= addr
;
291 if (access_type
== MMU_DATA_STORE
) {
295 if (access_type
== MMU_INST_FETCH
) {
296 error_code
|= EXCP_INST_NOTAVAIL
;
300 do_raise_exception_err(env
, excp
, error_code
, retaddr
);
303 void mips_cpu_do_transaction_failed(CPUState
*cs
, hwaddr physaddr
,
304 vaddr addr
, unsigned size
,
305 MMUAccessType access_type
,
306 int mmu_idx
, MemTxAttrs attrs
,
307 MemTxResult response
, uintptr_t retaddr
)
309 MIPSCPU
*cpu
= MIPS_CPU(cs
);
310 MIPSCPUClass
*mcc
= MIPS_CPU_GET_CLASS(cpu
);
311 CPUMIPSState
*env
= &cpu
->env
;
313 if (access_type
== MMU_INST_FETCH
) {
314 do_raise_exception(env
, EXCP_IBE
, retaddr
);
315 } else if (!mcc
->no_data_aborts
) {
316 do_raise_exception(env
, EXCP_DBE
, retaddr
);
319 #endif /* !CONFIG_USER_ONLY */