qemu-gdb: extract parts of "qemu coroutine" implementation
[qemu.git] / target-mips / op_helper.c
blob6739fff216421a97ab7314ae93418dc8b371f34c
1 /*
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 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/>.
19 #include <stdlib.h>
20 #include "cpu.h"
21 #include "qemu/host-utils.h"
22 #include "exec/helper-proto.h"
23 #include "exec/cpu_ldst.h"
24 #include "sysemu/kvm.h"
26 #ifndef CONFIG_USER_ONLY
27 static inline void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global);
28 #endif
30 /*****************************************************************************/
31 /* Exceptions processing helpers */
33 void helper_raise_exception_err(CPUMIPSState *env, uint32_t exception,
34 int error_code)
36 do_raise_exception_err(env, exception, error_code, 0);
39 void helper_raise_exception(CPUMIPSState *env, uint32_t exception)
41 do_raise_exception(env, exception, GETPC());
44 void helper_raise_exception_debug(CPUMIPSState *env)
46 do_raise_exception(env, EXCP_DEBUG, 0);
49 static void raise_exception(CPUMIPSState *env, uint32_t exception)
51 do_raise_exception(env, exception, 0);
54 #if defined(CONFIG_USER_ONLY)
55 #define HELPER_LD(name, insn, type) \
56 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
57 int mem_idx, uintptr_t retaddr) \
58 { \
59 return (type) cpu_##insn##_data_ra(env, addr, retaddr); \
61 #else
62 #define HELPER_LD(name, insn, type) \
63 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
64 int mem_idx, uintptr_t retaddr) \
65 { \
66 switch (mem_idx) \
67 { \
68 case 0: return (type) cpu_##insn##_kernel_ra(env, addr, retaddr); \
69 case 1: return (type) cpu_##insn##_super_ra(env, addr, retaddr); \
70 default: \
71 case 2: return (type) cpu_##insn##_user_ra(env, addr, retaddr); \
72 } \
74 #endif
75 HELPER_LD(lw, ldl, int32_t)
76 #if defined(TARGET_MIPS64)
77 HELPER_LD(ld, ldq, int64_t)
78 #endif
79 #undef HELPER_LD
81 #if defined(CONFIG_USER_ONLY)
82 #define HELPER_ST(name, insn, type) \
83 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
84 type val, int mem_idx, uintptr_t retaddr) \
85 { \
86 cpu_##insn##_data_ra(env, addr, val, retaddr); \
88 #else
89 #define HELPER_ST(name, insn, type) \
90 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
91 type val, int mem_idx, uintptr_t retaddr) \
92 { \
93 switch (mem_idx) \
94 { \
95 case 0: cpu_##insn##_kernel_ra(env, addr, val, retaddr); break; \
96 case 1: cpu_##insn##_super_ra(env, addr, val, retaddr); break; \
97 default: \
98 case 2: cpu_##insn##_user_ra(env, addr, val, retaddr); break; \
99 } \
101 #endif
102 HELPER_ST(sb, stb, uint8_t)
103 HELPER_ST(sw, stl, uint32_t)
104 #if defined(TARGET_MIPS64)
105 HELPER_ST(sd, stq, uint64_t)
106 #endif
107 #undef HELPER_ST
109 target_ulong helper_clo (target_ulong arg1)
111 return clo32(arg1);
114 target_ulong helper_clz (target_ulong arg1)
116 return clz32(arg1);
119 #if defined(TARGET_MIPS64)
120 target_ulong helper_dclo (target_ulong arg1)
122 return clo64(arg1);
125 target_ulong helper_dclz (target_ulong arg1)
127 return clz64(arg1);
129 #endif /* TARGET_MIPS64 */
131 /* 64 bits arithmetic for 32 bits hosts */
132 static inline uint64_t get_HILO(CPUMIPSState *env)
134 return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
137 static inline target_ulong set_HIT0_LO(CPUMIPSState *env, uint64_t HILO)
139 target_ulong tmp;
140 env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
141 tmp = env->active_tc.HI[0] = (int32_t)(HILO >> 32);
142 return tmp;
145 static inline target_ulong set_HI_LOT0(CPUMIPSState *env, uint64_t HILO)
147 target_ulong tmp = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
148 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
149 return tmp;
152 /* Multiplication variants of the vr54xx. */
153 target_ulong helper_muls(CPUMIPSState *env, target_ulong arg1,
154 target_ulong arg2)
156 return set_HI_LOT0(env, 0 - ((int64_t)(int32_t)arg1 *
157 (int64_t)(int32_t)arg2));
160 target_ulong helper_mulsu(CPUMIPSState *env, target_ulong arg1,
161 target_ulong arg2)
163 return set_HI_LOT0(env, 0 - (uint64_t)(uint32_t)arg1 *
164 (uint64_t)(uint32_t)arg2);
167 target_ulong helper_macc(CPUMIPSState *env, target_ulong arg1,
168 target_ulong arg2)
170 return set_HI_LOT0(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
171 (int64_t)(int32_t)arg2);
174 target_ulong helper_macchi(CPUMIPSState *env, target_ulong arg1,
175 target_ulong arg2)
177 return set_HIT0_LO(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
178 (int64_t)(int32_t)arg2);
181 target_ulong helper_maccu(CPUMIPSState *env, target_ulong arg1,
182 target_ulong arg2)
184 return set_HI_LOT0(env, (uint64_t)get_HILO(env) +
185 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
188 target_ulong helper_macchiu(CPUMIPSState *env, target_ulong arg1,
189 target_ulong arg2)
191 return set_HIT0_LO(env, (uint64_t)get_HILO(env) +
192 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
195 target_ulong helper_msac(CPUMIPSState *env, target_ulong arg1,
196 target_ulong arg2)
198 return set_HI_LOT0(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
199 (int64_t)(int32_t)arg2);
202 target_ulong helper_msachi(CPUMIPSState *env, target_ulong arg1,
203 target_ulong arg2)
205 return set_HIT0_LO(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
206 (int64_t)(int32_t)arg2);
209 target_ulong helper_msacu(CPUMIPSState *env, target_ulong arg1,
210 target_ulong arg2)
212 return set_HI_LOT0(env, (uint64_t)get_HILO(env) -
213 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
216 target_ulong helper_msachiu(CPUMIPSState *env, target_ulong arg1,
217 target_ulong arg2)
219 return set_HIT0_LO(env, (uint64_t)get_HILO(env) -
220 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
223 target_ulong helper_mulhi(CPUMIPSState *env, target_ulong arg1,
224 target_ulong arg2)
226 return set_HIT0_LO(env, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
229 target_ulong helper_mulhiu(CPUMIPSState *env, target_ulong arg1,
230 target_ulong arg2)
232 return set_HIT0_LO(env, (uint64_t)(uint32_t)arg1 *
233 (uint64_t)(uint32_t)arg2);
236 target_ulong helper_mulshi(CPUMIPSState *env, target_ulong arg1,
237 target_ulong arg2)
239 return set_HIT0_LO(env, 0 - (int64_t)(int32_t)arg1 *
240 (int64_t)(int32_t)arg2);
243 target_ulong helper_mulshiu(CPUMIPSState *env, target_ulong arg1,
244 target_ulong arg2)
246 return set_HIT0_LO(env, 0 - (uint64_t)(uint32_t)arg1 *
247 (uint64_t)(uint32_t)arg2);
250 static inline target_ulong bitswap(target_ulong v)
252 v = ((v >> 1) & (target_ulong)0x5555555555555555ULL) |
253 ((v & (target_ulong)0x5555555555555555ULL) << 1);
254 v = ((v >> 2) & (target_ulong)0x3333333333333333ULL) |
255 ((v & (target_ulong)0x3333333333333333ULL) << 2);
256 v = ((v >> 4) & (target_ulong)0x0F0F0F0F0F0F0F0FULL) |
257 ((v & (target_ulong)0x0F0F0F0F0F0F0F0FULL) << 4);
258 return v;
261 #ifdef TARGET_MIPS64
262 target_ulong helper_dbitswap(target_ulong rt)
264 return bitswap(rt);
266 #endif
268 target_ulong helper_bitswap(target_ulong rt)
270 return (int32_t)bitswap(rt);
273 #ifndef CONFIG_USER_ONLY
275 static inline hwaddr do_translate_address(CPUMIPSState *env,
276 target_ulong address,
277 int rw, uintptr_t retaddr)
279 hwaddr lladdr;
280 CPUState *cs = CPU(mips_env_get_cpu(env));
282 lladdr = cpu_mips_translate_address(env, address, rw);
284 if (lladdr == -1LL) {
285 cpu_loop_exit_restore(cs, retaddr);
286 } else {
287 return lladdr;
291 #define HELPER_LD_ATOMIC(name, insn, almask) \
292 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg, int mem_idx) \
294 if (arg & almask) { \
295 env->CP0_BadVAddr = arg; \
296 do_raise_exception(env, EXCP_AdEL, GETPC()); \
298 env->lladdr = do_translate_address(env, arg, 0, GETPC()); \
299 env->llval = do_##insn(env, arg, mem_idx, GETPC()); \
300 return env->llval; \
302 HELPER_LD_ATOMIC(ll, lw, 0x3)
303 #ifdef TARGET_MIPS64
304 HELPER_LD_ATOMIC(lld, ld, 0x7)
305 #endif
306 #undef HELPER_LD_ATOMIC
308 #define HELPER_ST_ATOMIC(name, ld_insn, st_insn, almask) \
309 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg1, \
310 target_ulong arg2, int mem_idx) \
312 target_long tmp; \
314 if (arg2 & almask) { \
315 env->CP0_BadVAddr = arg2; \
316 do_raise_exception(env, EXCP_AdES, GETPC()); \
318 if (do_translate_address(env, arg2, 1, GETPC()) == env->lladdr) { \
319 tmp = do_##ld_insn(env, arg2, mem_idx, GETPC()); \
320 if (tmp == env->llval) { \
321 do_##st_insn(env, arg2, arg1, mem_idx, GETPC()); \
322 return 1; \
325 return 0; \
327 HELPER_ST_ATOMIC(sc, lw, sw, 0x3)
328 #ifdef TARGET_MIPS64
329 HELPER_ST_ATOMIC(scd, ld, sd, 0x7)
330 #endif
331 #undef HELPER_ST_ATOMIC
332 #endif
334 #ifdef TARGET_WORDS_BIGENDIAN
335 #define GET_LMASK(v) ((v) & 3)
336 #define GET_OFFSET(addr, offset) (addr + (offset))
337 #else
338 #define GET_LMASK(v) (((v) & 3) ^ 3)
339 #define GET_OFFSET(addr, offset) (addr - (offset))
340 #endif
342 void helper_swl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
343 int mem_idx)
345 do_sb(env, arg2, (uint8_t)(arg1 >> 24), mem_idx, GETPC());
347 if (GET_LMASK(arg2) <= 2) {
348 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16), mem_idx,
349 GETPC());
352 if (GET_LMASK(arg2) <= 1) {
353 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8), mem_idx,
354 GETPC());
357 if (GET_LMASK(arg2) == 0) {
358 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)arg1, mem_idx,
359 GETPC());
363 void helper_swr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
364 int mem_idx)
366 do_sb(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
368 if (GET_LMASK(arg2) >= 1) {
369 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx,
370 GETPC());
373 if (GET_LMASK(arg2) >= 2) {
374 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx,
375 GETPC());
378 if (GET_LMASK(arg2) == 3) {
379 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx,
380 GETPC());
384 #if defined(TARGET_MIPS64)
385 /* "half" load and stores. We must do the memory access inline,
386 or fault handling won't work. */
388 #ifdef TARGET_WORDS_BIGENDIAN
389 #define GET_LMASK64(v) ((v) & 7)
390 #else
391 #define GET_LMASK64(v) (((v) & 7) ^ 7)
392 #endif
394 void helper_sdl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
395 int mem_idx)
397 do_sb(env, arg2, (uint8_t)(arg1 >> 56), mem_idx, GETPC());
399 if (GET_LMASK64(arg2) <= 6) {
400 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48), mem_idx,
401 GETPC());
404 if (GET_LMASK64(arg2) <= 5) {
405 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40), mem_idx,
406 GETPC());
409 if (GET_LMASK64(arg2) <= 4) {
410 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32), mem_idx,
411 GETPC());
414 if (GET_LMASK64(arg2) <= 3) {
415 do_sb(env, GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24), mem_idx,
416 GETPC());
419 if (GET_LMASK64(arg2) <= 2) {
420 do_sb(env, GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16), mem_idx,
421 GETPC());
424 if (GET_LMASK64(arg2) <= 1) {
425 do_sb(env, GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8), mem_idx,
426 GETPC());
429 if (GET_LMASK64(arg2) <= 0) {
430 do_sb(env, GET_OFFSET(arg2, 7), (uint8_t)arg1, mem_idx,
431 GETPC());
435 void helper_sdr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
436 int mem_idx)
438 do_sb(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
440 if (GET_LMASK64(arg2) >= 1) {
441 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx,
442 GETPC());
445 if (GET_LMASK64(arg2) >= 2) {
446 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx,
447 GETPC());
450 if (GET_LMASK64(arg2) >= 3) {
451 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx,
452 GETPC());
455 if (GET_LMASK64(arg2) >= 4) {
456 do_sb(env, GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32), mem_idx,
457 GETPC());
460 if (GET_LMASK64(arg2) >= 5) {
461 do_sb(env, GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40), mem_idx,
462 GETPC());
465 if (GET_LMASK64(arg2) >= 6) {
466 do_sb(env, GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48), mem_idx,
467 GETPC());
470 if (GET_LMASK64(arg2) == 7) {
471 do_sb(env, GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56), mem_idx,
472 GETPC());
475 #endif /* TARGET_MIPS64 */
477 static const int multiple_regs[] = { 16, 17, 18, 19, 20, 21, 22, 23, 30 };
479 void helper_lwm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
480 uint32_t mem_idx)
482 target_ulong base_reglist = reglist & 0xf;
483 target_ulong do_r31 = reglist & 0x10;
485 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
486 target_ulong i;
488 for (i = 0; i < base_reglist; i++) {
489 env->active_tc.gpr[multiple_regs[i]] =
490 (target_long)do_lw(env, addr, mem_idx, GETPC());
491 addr += 4;
495 if (do_r31) {
496 env->active_tc.gpr[31] = (target_long)do_lw(env, addr, mem_idx,
497 GETPC());
501 void helper_swm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
502 uint32_t mem_idx)
504 target_ulong base_reglist = reglist & 0xf;
505 target_ulong do_r31 = reglist & 0x10;
507 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
508 target_ulong i;
510 for (i = 0; i < base_reglist; i++) {
511 do_sw(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx,
512 GETPC());
513 addr += 4;
517 if (do_r31) {
518 do_sw(env, addr, env->active_tc.gpr[31], mem_idx, GETPC());
522 #if defined(TARGET_MIPS64)
523 void helper_ldm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
524 uint32_t mem_idx)
526 target_ulong base_reglist = reglist & 0xf;
527 target_ulong do_r31 = reglist & 0x10;
529 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
530 target_ulong i;
532 for (i = 0; i < base_reglist; i++) {
533 env->active_tc.gpr[multiple_regs[i]] = do_ld(env, addr, mem_idx,
534 GETPC());
535 addr += 8;
539 if (do_r31) {
540 env->active_tc.gpr[31] = do_ld(env, addr, mem_idx, GETPC());
544 void helper_sdm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
545 uint32_t mem_idx)
547 target_ulong base_reglist = reglist & 0xf;
548 target_ulong do_r31 = reglist & 0x10;
550 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
551 target_ulong i;
553 for (i = 0; i < base_reglist; i++) {
554 do_sd(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx,
555 GETPC());
556 addr += 8;
560 if (do_r31) {
561 do_sd(env, addr, env->active_tc.gpr[31], mem_idx, GETPC());
564 #endif
566 #ifndef CONFIG_USER_ONLY
567 /* SMP helpers. */
568 static bool mips_vpe_is_wfi(MIPSCPU *c)
570 CPUState *cpu = CPU(c);
571 CPUMIPSState *env = &c->env;
573 /* If the VPE is halted but otherwise active, it means it's waiting for
574 an interrupt. */
575 return cpu->halted && mips_vpe_active(env);
578 static inline void mips_vpe_wake(MIPSCPU *c)
580 /* Dont set ->halted = 0 directly, let it be done via cpu_has_work
581 because there might be other conditions that state that c should
582 be sleeping. */
583 cpu_interrupt(CPU(c), CPU_INTERRUPT_WAKE);
586 static inline void mips_vpe_sleep(MIPSCPU *cpu)
588 CPUState *cs = CPU(cpu);
590 /* The VPE was shut off, really go to bed.
591 Reset any old _WAKE requests. */
592 cs->halted = 1;
593 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
596 static inline void mips_tc_wake(MIPSCPU *cpu, int tc)
598 CPUMIPSState *c = &cpu->env;
600 /* FIXME: TC reschedule. */
601 if (mips_vpe_active(c) && !mips_vpe_is_wfi(cpu)) {
602 mips_vpe_wake(cpu);
606 static inline void mips_tc_sleep(MIPSCPU *cpu, int tc)
608 CPUMIPSState *c = &cpu->env;
610 /* FIXME: TC reschedule. */
611 if (!mips_vpe_active(c)) {
612 mips_vpe_sleep(cpu);
617 * mips_cpu_map_tc:
618 * @env: CPU from which mapping is performed.
619 * @tc: Should point to an int with the value of the global TC index.
621 * This function will transform @tc into a local index within the
622 * returned #CPUMIPSState.
624 /* FIXME: This code assumes that all VPEs have the same number of TCs,
625 which depends on runtime setup. Can probably be fixed by
626 walking the list of CPUMIPSStates. */
627 static CPUMIPSState *mips_cpu_map_tc(CPUMIPSState *env, int *tc)
629 MIPSCPU *cpu;
630 CPUState *cs;
631 CPUState *other_cs;
632 int vpe_idx;
633 int tc_idx = *tc;
635 if (!(env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))) {
636 /* Not allowed to address other CPUs. */
637 *tc = env->current_tc;
638 return env;
641 cs = CPU(mips_env_get_cpu(env));
642 vpe_idx = tc_idx / cs->nr_threads;
643 *tc = tc_idx % cs->nr_threads;
644 other_cs = qemu_get_cpu(vpe_idx);
645 if (other_cs == NULL) {
646 return env;
648 cpu = MIPS_CPU(other_cs);
649 return &cpu->env;
652 /* The per VPE CP0_Status register shares some fields with the per TC
653 CP0_TCStatus registers. These fields are wired to the same registers,
654 so changes to either of them should be reflected on both registers.
656 Also, EntryHi shares the bottom 8 bit ASID with TCStauts.
658 These helper call synchronizes the regs for a given cpu. */
660 /* Called for updates to CP0_Status. Defined in "cpu.h" for gdbstub.c. */
661 /* static inline void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu,
662 int tc); */
664 /* Called for updates to CP0_TCStatus. */
665 static void sync_c0_tcstatus(CPUMIPSState *cpu, int tc,
666 target_ulong v)
668 uint32_t status;
669 uint32_t tcu, tmx, tasid, tksu;
670 uint32_t mask = ((1U << CP0St_CU3)
671 | (1 << CP0St_CU2)
672 | (1 << CP0St_CU1)
673 | (1 << CP0St_CU0)
674 | (1 << CP0St_MX)
675 | (3 << CP0St_KSU));
677 tcu = (v >> CP0TCSt_TCU0) & 0xf;
678 tmx = (v >> CP0TCSt_TMX) & 0x1;
679 tasid = v & 0xff;
680 tksu = (v >> CP0TCSt_TKSU) & 0x3;
682 status = tcu << CP0St_CU0;
683 status |= tmx << CP0St_MX;
684 status |= tksu << CP0St_KSU;
686 cpu->CP0_Status &= ~mask;
687 cpu->CP0_Status |= status;
689 /* Sync the TASID with EntryHi. */
690 cpu->CP0_EntryHi &= ~0xff;
691 cpu->CP0_EntryHi |= tasid;
693 compute_hflags(cpu);
696 /* Called for updates to CP0_EntryHi. */
697 static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
699 int32_t *tcst;
700 uint32_t asid, v = cpu->CP0_EntryHi;
702 asid = v & 0xff;
704 if (tc == cpu->current_tc) {
705 tcst = &cpu->active_tc.CP0_TCStatus;
706 } else {
707 tcst = &cpu->tcs[tc].CP0_TCStatus;
710 *tcst &= ~0xff;
711 *tcst |= asid;
714 /* CP0 helpers */
715 target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
717 return env->mvp->CP0_MVPControl;
720 target_ulong helper_mfc0_mvpconf0(CPUMIPSState *env)
722 return env->mvp->CP0_MVPConf0;
725 target_ulong helper_mfc0_mvpconf1(CPUMIPSState *env)
727 return env->mvp->CP0_MVPConf1;
730 target_ulong helper_mfc0_random(CPUMIPSState *env)
732 return (int32_t)cpu_mips_get_random(env);
735 target_ulong helper_mfc0_tcstatus(CPUMIPSState *env)
737 return env->active_tc.CP0_TCStatus;
740 target_ulong helper_mftc0_tcstatus(CPUMIPSState *env)
742 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
743 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
745 if (other_tc == other->current_tc)
746 return other->active_tc.CP0_TCStatus;
747 else
748 return other->tcs[other_tc].CP0_TCStatus;
751 target_ulong helper_mfc0_tcbind(CPUMIPSState *env)
753 return env->active_tc.CP0_TCBind;
756 target_ulong helper_mftc0_tcbind(CPUMIPSState *env)
758 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
759 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
761 if (other_tc == other->current_tc)
762 return other->active_tc.CP0_TCBind;
763 else
764 return other->tcs[other_tc].CP0_TCBind;
767 target_ulong helper_mfc0_tcrestart(CPUMIPSState *env)
769 return env->active_tc.PC;
772 target_ulong helper_mftc0_tcrestart(CPUMIPSState *env)
774 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
775 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
777 if (other_tc == other->current_tc)
778 return other->active_tc.PC;
779 else
780 return other->tcs[other_tc].PC;
783 target_ulong helper_mfc0_tchalt(CPUMIPSState *env)
785 return env->active_tc.CP0_TCHalt;
788 target_ulong helper_mftc0_tchalt(CPUMIPSState *env)
790 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
791 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
793 if (other_tc == other->current_tc)
794 return other->active_tc.CP0_TCHalt;
795 else
796 return other->tcs[other_tc].CP0_TCHalt;
799 target_ulong helper_mfc0_tccontext(CPUMIPSState *env)
801 return env->active_tc.CP0_TCContext;
804 target_ulong helper_mftc0_tccontext(CPUMIPSState *env)
806 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
807 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
809 if (other_tc == other->current_tc)
810 return other->active_tc.CP0_TCContext;
811 else
812 return other->tcs[other_tc].CP0_TCContext;
815 target_ulong helper_mfc0_tcschedule(CPUMIPSState *env)
817 return env->active_tc.CP0_TCSchedule;
820 target_ulong helper_mftc0_tcschedule(CPUMIPSState *env)
822 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
823 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
825 if (other_tc == other->current_tc)
826 return other->active_tc.CP0_TCSchedule;
827 else
828 return other->tcs[other_tc].CP0_TCSchedule;
831 target_ulong helper_mfc0_tcschefback(CPUMIPSState *env)
833 return env->active_tc.CP0_TCScheFBack;
836 target_ulong helper_mftc0_tcschefback(CPUMIPSState *env)
838 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
839 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
841 if (other_tc == other->current_tc)
842 return other->active_tc.CP0_TCScheFBack;
843 else
844 return other->tcs[other_tc].CP0_TCScheFBack;
847 target_ulong helper_mfc0_count(CPUMIPSState *env)
849 return (int32_t)cpu_mips_get_count(env);
852 target_ulong helper_mftc0_entryhi(CPUMIPSState *env)
854 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
855 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
857 return other->CP0_EntryHi;
860 target_ulong helper_mftc0_cause(CPUMIPSState *env)
862 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
863 int32_t tccause;
864 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
866 if (other_tc == other->current_tc) {
867 tccause = other->CP0_Cause;
868 } else {
869 tccause = other->CP0_Cause;
872 return tccause;
875 target_ulong helper_mftc0_status(CPUMIPSState *env)
877 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
878 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
880 return other->CP0_Status;
883 target_ulong helper_mfc0_lladdr(CPUMIPSState *env)
885 return (int32_t)(env->lladdr >> env->CP0_LLAddr_shift);
888 target_ulong helper_mfc0_watchlo(CPUMIPSState *env, uint32_t sel)
890 return (int32_t)env->CP0_WatchLo[sel];
893 target_ulong helper_mfc0_watchhi(CPUMIPSState *env, uint32_t sel)
895 return env->CP0_WatchHi[sel];
898 target_ulong helper_mfc0_debug(CPUMIPSState *env)
900 target_ulong t0 = env->CP0_Debug;
901 if (env->hflags & MIPS_HFLAG_DM)
902 t0 |= 1 << CP0DB_DM;
904 return t0;
907 target_ulong helper_mftc0_debug(CPUMIPSState *env)
909 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
910 int32_t tcstatus;
911 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
913 if (other_tc == other->current_tc)
914 tcstatus = other->active_tc.CP0_Debug_tcstatus;
915 else
916 tcstatus = other->tcs[other_tc].CP0_Debug_tcstatus;
918 /* XXX: Might be wrong, check with EJTAG spec. */
919 return (other->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
920 (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
923 #if defined(TARGET_MIPS64)
924 target_ulong helper_dmfc0_tcrestart(CPUMIPSState *env)
926 return env->active_tc.PC;
929 target_ulong helper_dmfc0_tchalt(CPUMIPSState *env)
931 return env->active_tc.CP0_TCHalt;
934 target_ulong helper_dmfc0_tccontext(CPUMIPSState *env)
936 return env->active_tc.CP0_TCContext;
939 target_ulong helper_dmfc0_tcschedule(CPUMIPSState *env)
941 return env->active_tc.CP0_TCSchedule;
944 target_ulong helper_dmfc0_tcschefback(CPUMIPSState *env)
946 return env->active_tc.CP0_TCScheFBack;
949 target_ulong helper_dmfc0_lladdr(CPUMIPSState *env)
951 return env->lladdr >> env->CP0_LLAddr_shift;
954 target_ulong helper_dmfc0_watchlo(CPUMIPSState *env, uint32_t sel)
956 return env->CP0_WatchLo[sel];
958 #endif /* TARGET_MIPS64 */
960 void helper_mtc0_index(CPUMIPSState *env, target_ulong arg1)
962 uint32_t index_p = env->CP0_Index & 0x80000000;
963 uint32_t tlb_index = arg1 & 0x7fffffff;
964 if (tlb_index < env->tlb->nb_tlb) {
965 if (env->insn_flags & ISA_MIPS32R6) {
966 index_p |= arg1 & 0x80000000;
968 env->CP0_Index = index_p | tlb_index;
972 void helper_mtc0_mvpcontrol(CPUMIPSState *env, target_ulong arg1)
974 uint32_t mask = 0;
975 uint32_t newval;
977 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
978 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
979 (1 << CP0MVPCo_EVP);
980 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
981 mask |= (1 << CP0MVPCo_STLB);
982 newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
984 // TODO: Enable/disable shared TLB, enable/disable VPEs.
986 env->mvp->CP0_MVPControl = newval;
989 void helper_mtc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
991 uint32_t mask;
992 uint32_t newval;
994 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
995 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
996 newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
998 /* Yield scheduler intercept not implemented. */
999 /* Gating storage scheduler intercept not implemented. */
1001 // TODO: Enable/disable TCs.
1003 env->CP0_VPEControl = newval;
1006 void helper_mttc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
1008 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1009 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1010 uint32_t mask;
1011 uint32_t newval;
1013 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
1014 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1015 newval = (other->CP0_VPEControl & ~mask) | (arg1 & mask);
1017 /* TODO: Enable/disable TCs. */
1019 other->CP0_VPEControl = newval;
1022 target_ulong helper_mftc0_vpecontrol(CPUMIPSState *env)
1024 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1025 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1026 /* FIXME: Mask away return zero on read bits. */
1027 return other->CP0_VPEControl;
1030 target_ulong helper_mftc0_vpeconf0(CPUMIPSState *env)
1032 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1033 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1035 return other->CP0_VPEConf0;
1038 void helper_mtc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1040 uint32_t mask = 0;
1041 uint32_t newval;
1043 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
1044 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
1045 mask |= (0xff << CP0VPEC0_XTC);
1046 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1048 newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1050 // TODO: TC exclusive handling due to ERL/EXL.
1052 env->CP0_VPEConf0 = newval;
1055 void helper_mttc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1057 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1058 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1059 uint32_t mask = 0;
1060 uint32_t newval;
1062 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1063 newval = (other->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1065 /* TODO: TC exclusive handling due to ERL/EXL. */
1066 other->CP0_VPEConf0 = newval;
1069 void helper_mtc0_vpeconf1(CPUMIPSState *env, target_ulong arg1)
1071 uint32_t mask = 0;
1072 uint32_t newval;
1074 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1075 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
1076 (0xff << CP0VPEC1_NCP1);
1077 newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
1079 /* UDI not implemented. */
1080 /* CP2 not implemented. */
1082 // TODO: Handle FPU (CP1) binding.
1084 env->CP0_VPEConf1 = newval;
1087 void helper_mtc0_yqmask(CPUMIPSState *env, target_ulong arg1)
1089 /* Yield qualifier inputs not implemented. */
1090 env->CP0_YQMask = 0x00000000;
1093 void helper_mtc0_vpeopt(CPUMIPSState *env, target_ulong arg1)
1095 env->CP0_VPEOpt = arg1 & 0x0000ffff;
1098 #define MTC0_ENTRYLO_MASK(env) ((env->PAMask >> 6) & 0x3FFFFFFF)
1100 void helper_mtc0_entrylo0(CPUMIPSState *env, target_ulong arg1)
1102 /* 1k pages not implemented */
1103 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1104 env->CP0_EntryLo0 = (arg1 & MTC0_ENTRYLO_MASK(env))
1105 | (rxi << (CP0EnLo_XI - 30));
1108 #if defined(TARGET_MIPS64)
1109 #define DMTC0_ENTRYLO_MASK(env) (env->PAMask >> 6)
1111 void helper_dmtc0_entrylo0(CPUMIPSState *env, uint64_t arg1)
1113 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1114 env->CP0_EntryLo0 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1116 #endif
1118 void helper_mtc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1120 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
1121 uint32_t newval;
1123 newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
1125 env->active_tc.CP0_TCStatus = newval;
1126 sync_c0_tcstatus(env, env->current_tc, newval);
1129 void helper_mttc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1131 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1132 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1134 if (other_tc == other->current_tc)
1135 other->active_tc.CP0_TCStatus = arg1;
1136 else
1137 other->tcs[other_tc].CP0_TCStatus = arg1;
1138 sync_c0_tcstatus(other, other_tc, arg1);
1141 void helper_mtc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1143 uint32_t mask = (1 << CP0TCBd_TBE);
1144 uint32_t newval;
1146 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1147 mask |= (1 << CP0TCBd_CurVPE);
1148 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1149 env->active_tc.CP0_TCBind = newval;
1152 void helper_mttc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1154 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1155 uint32_t mask = (1 << CP0TCBd_TBE);
1156 uint32_t newval;
1157 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1159 if (other->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1160 mask |= (1 << CP0TCBd_CurVPE);
1161 if (other_tc == other->current_tc) {
1162 newval = (other->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1163 other->active_tc.CP0_TCBind = newval;
1164 } else {
1165 newval = (other->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
1166 other->tcs[other_tc].CP0_TCBind = newval;
1170 void helper_mtc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1172 env->active_tc.PC = arg1;
1173 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1174 env->lladdr = 0ULL;
1175 /* MIPS16 not implemented. */
1178 void helper_mttc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1180 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1181 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1183 if (other_tc == other->current_tc) {
1184 other->active_tc.PC = arg1;
1185 other->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1186 other->lladdr = 0ULL;
1187 /* MIPS16 not implemented. */
1188 } else {
1189 other->tcs[other_tc].PC = arg1;
1190 other->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1191 other->lladdr = 0ULL;
1192 /* MIPS16 not implemented. */
1196 void helper_mtc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1198 MIPSCPU *cpu = mips_env_get_cpu(env);
1200 env->active_tc.CP0_TCHalt = arg1 & 0x1;
1202 // TODO: Halt TC / Restart (if allocated+active) TC.
1203 if (env->active_tc.CP0_TCHalt & 1) {
1204 mips_tc_sleep(cpu, env->current_tc);
1205 } else {
1206 mips_tc_wake(cpu, env->current_tc);
1210 void helper_mttc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1212 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1213 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1214 MIPSCPU *other_cpu = mips_env_get_cpu(other);
1216 // TODO: Halt TC / Restart (if allocated+active) TC.
1218 if (other_tc == other->current_tc)
1219 other->active_tc.CP0_TCHalt = arg1;
1220 else
1221 other->tcs[other_tc].CP0_TCHalt = arg1;
1223 if (arg1 & 1) {
1224 mips_tc_sleep(other_cpu, other_tc);
1225 } else {
1226 mips_tc_wake(other_cpu, other_tc);
1230 void helper_mtc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1232 env->active_tc.CP0_TCContext = arg1;
1235 void helper_mttc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1237 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1238 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1240 if (other_tc == other->current_tc)
1241 other->active_tc.CP0_TCContext = arg1;
1242 else
1243 other->tcs[other_tc].CP0_TCContext = arg1;
1246 void helper_mtc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1248 env->active_tc.CP0_TCSchedule = arg1;
1251 void helper_mttc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1253 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1254 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1256 if (other_tc == other->current_tc)
1257 other->active_tc.CP0_TCSchedule = arg1;
1258 else
1259 other->tcs[other_tc].CP0_TCSchedule = arg1;
1262 void helper_mtc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1264 env->active_tc.CP0_TCScheFBack = arg1;
1267 void helper_mttc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1269 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1270 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1272 if (other_tc == other->current_tc)
1273 other->active_tc.CP0_TCScheFBack = arg1;
1274 else
1275 other->tcs[other_tc].CP0_TCScheFBack = arg1;
1278 void helper_mtc0_entrylo1(CPUMIPSState *env, target_ulong arg1)
1280 /* 1k pages not implemented */
1281 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1282 env->CP0_EntryLo1 = (arg1 & MTC0_ENTRYLO_MASK(env))
1283 | (rxi << (CP0EnLo_XI - 30));
1286 #if defined(TARGET_MIPS64)
1287 void helper_dmtc0_entrylo1(CPUMIPSState *env, uint64_t arg1)
1289 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1290 env->CP0_EntryLo1 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1292 #endif
1294 void helper_mtc0_context(CPUMIPSState *env, target_ulong arg1)
1296 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1299 void helper_mtc0_pagemask(CPUMIPSState *env, target_ulong arg1)
1301 uint64_t mask = arg1 >> (TARGET_PAGE_BITS + 1);
1302 if (!(env->insn_flags & ISA_MIPS32R6) || (arg1 == ~0) ||
1303 (mask == 0x0000 || mask == 0x0003 || mask == 0x000F ||
1304 mask == 0x003F || mask == 0x00FF || mask == 0x03FF ||
1305 mask == 0x0FFF || mask == 0x3FFF || mask == 0xFFFF)) {
1306 env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1310 void helper_mtc0_pagegrain(CPUMIPSState *env, target_ulong arg1)
1312 /* SmartMIPS not implemented */
1313 /* 1k pages not implemented */
1314 env->CP0_PageGrain = (arg1 & env->CP0_PageGrain_rw_bitmask) |
1315 (env->CP0_PageGrain & ~env->CP0_PageGrain_rw_bitmask);
1316 compute_hflags(env);
1317 restore_pamask(env);
1320 void helper_mtc0_wired(CPUMIPSState *env, target_ulong arg1)
1322 if (env->insn_flags & ISA_MIPS32R6) {
1323 if (arg1 < env->tlb->nb_tlb) {
1324 env->CP0_Wired = arg1;
1326 } else {
1327 env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1331 void helper_mtc0_srsconf0(CPUMIPSState *env, target_ulong arg1)
1333 env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1336 void helper_mtc0_srsconf1(CPUMIPSState *env, target_ulong arg1)
1338 env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1341 void helper_mtc0_srsconf2(CPUMIPSState *env, target_ulong arg1)
1343 env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1346 void helper_mtc0_srsconf3(CPUMIPSState *env, target_ulong arg1)
1348 env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1351 void helper_mtc0_srsconf4(CPUMIPSState *env, target_ulong arg1)
1353 env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1356 void helper_mtc0_hwrena(CPUMIPSState *env, target_ulong arg1)
1358 uint32_t mask = 0x0000000F;
1360 if (env->CP0_Config3 & (1 << CP0C3_ULRI)) {
1361 mask |= (1 << 29);
1363 if (arg1 & (1 << 29)) {
1364 env->hflags |= MIPS_HFLAG_HWRENA_ULR;
1365 } else {
1366 env->hflags &= ~MIPS_HFLAG_HWRENA_ULR;
1370 env->CP0_HWREna = arg1 & mask;
1373 void helper_mtc0_count(CPUMIPSState *env, target_ulong arg1)
1375 cpu_mips_store_count(env, arg1);
1378 void helper_mtc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1380 target_ulong old, val, mask;
1381 mask = (TARGET_PAGE_MASK << 1) | 0xFF;
1382 if (((env->CP0_Config4 >> CP0C4_IE) & 0x3) >= 2) {
1383 mask |= 1 << CP0EnHi_EHINV;
1386 /* 1k pages not implemented */
1387 #if defined(TARGET_MIPS64)
1388 if (env->insn_flags & ISA_MIPS32R6) {
1389 int entryhi_r = extract64(arg1, 62, 2);
1390 int config0_at = extract32(env->CP0_Config0, 13, 2);
1391 bool no_supervisor = (env->CP0_Status_rw_bitmask & 0x8) == 0;
1392 if ((entryhi_r == 2) ||
1393 (entryhi_r == 1 && (no_supervisor || config0_at == 1))) {
1394 /* skip EntryHi.R field if new value is reserved */
1395 mask &= ~(0x3ull << 62);
1398 mask &= env->SEGMask;
1399 #endif
1400 old = env->CP0_EntryHi;
1401 val = (arg1 & mask) | (old & ~mask);
1402 env->CP0_EntryHi = val;
1403 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1404 sync_c0_entryhi(env, env->current_tc);
1406 /* If the ASID changes, flush qemu's TLB. */
1407 if ((old & 0xFF) != (val & 0xFF))
1408 cpu_mips_tlb_flush(env, 1);
1411 void helper_mttc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1413 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1414 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1416 other->CP0_EntryHi = arg1;
1417 sync_c0_entryhi(other, other_tc);
1420 void helper_mtc0_compare(CPUMIPSState *env, target_ulong arg1)
1422 cpu_mips_store_compare(env, arg1);
1425 void helper_mtc0_status(CPUMIPSState *env, target_ulong arg1)
1427 MIPSCPU *cpu = mips_env_get_cpu(env);
1428 uint32_t val, old;
1430 old = env->CP0_Status;
1431 cpu_mips_store_status(env, arg1);
1432 val = env->CP0_Status;
1434 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1435 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1436 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1437 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1438 env->CP0_Cause);
1439 switch (env->hflags & MIPS_HFLAG_KSU) {
1440 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1441 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1442 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1443 default:
1444 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
1445 break;
1450 void helper_mttc0_status(CPUMIPSState *env, target_ulong arg1)
1452 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1453 uint32_t mask = env->CP0_Status_rw_bitmask & ~0xf1000018;
1454 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1456 other->CP0_Status = (other->CP0_Status & ~mask) | (arg1 & mask);
1457 sync_c0_status(env, other, other_tc);
1460 void helper_mtc0_intctl(CPUMIPSState *env, target_ulong arg1)
1462 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000003e0) | (arg1 & 0x000003e0);
1465 void helper_mtc0_srsctl(CPUMIPSState *env, target_ulong arg1)
1467 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1468 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1471 void helper_mtc0_cause(CPUMIPSState *env, target_ulong arg1)
1473 cpu_mips_store_cause(env, arg1);
1476 void helper_mttc0_cause(CPUMIPSState *env, target_ulong arg1)
1478 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1479 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1481 cpu_mips_store_cause(other, arg1);
1484 target_ulong helper_mftc0_epc(CPUMIPSState *env)
1486 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1487 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1489 return other->CP0_EPC;
1492 target_ulong helper_mftc0_ebase(CPUMIPSState *env)
1494 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1495 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1497 return other->CP0_EBase;
1500 void helper_mtc0_ebase(CPUMIPSState *env, target_ulong arg1)
1502 env->CP0_EBase = (env->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1505 void helper_mttc0_ebase(CPUMIPSState *env, target_ulong arg1)
1507 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1508 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1509 other->CP0_EBase = (other->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1512 target_ulong helper_mftc0_configx(CPUMIPSState *env, target_ulong idx)
1514 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1515 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1517 switch (idx) {
1518 case 0: return other->CP0_Config0;
1519 case 1: return other->CP0_Config1;
1520 case 2: return other->CP0_Config2;
1521 case 3: return other->CP0_Config3;
1522 /* 4 and 5 are reserved. */
1523 case 6: return other->CP0_Config6;
1524 case 7: return other->CP0_Config7;
1525 default:
1526 break;
1528 return 0;
1531 void helper_mtc0_config0(CPUMIPSState *env, target_ulong arg1)
1533 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1536 void helper_mtc0_config2(CPUMIPSState *env, target_ulong arg1)
1538 /* tertiary/secondary caches not implemented */
1539 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1542 void helper_mtc0_config3(CPUMIPSState *env, target_ulong arg1)
1544 if (env->insn_flags & ASE_MICROMIPS) {
1545 env->CP0_Config3 = (env->CP0_Config3 & ~(1 << CP0C3_ISA_ON_EXC)) |
1546 (arg1 & (1 << CP0C3_ISA_ON_EXC));
1550 void helper_mtc0_config4(CPUMIPSState *env, target_ulong arg1)
1552 env->CP0_Config4 = (env->CP0_Config4 & (~env->CP0_Config4_rw_bitmask)) |
1553 (arg1 & env->CP0_Config4_rw_bitmask);
1556 void helper_mtc0_config5(CPUMIPSState *env, target_ulong arg1)
1558 env->CP0_Config5 = (env->CP0_Config5 & (~env->CP0_Config5_rw_bitmask)) |
1559 (arg1 & env->CP0_Config5_rw_bitmask);
1560 compute_hflags(env);
1563 void helper_mtc0_lladdr(CPUMIPSState *env, target_ulong arg1)
1565 target_long mask = env->CP0_LLAddr_rw_bitmask;
1566 arg1 = arg1 << env->CP0_LLAddr_shift;
1567 env->lladdr = (env->lladdr & ~mask) | (arg1 & mask);
1570 void helper_mtc0_watchlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1572 /* Watch exceptions for instructions, data loads, data stores
1573 not implemented. */
1574 env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1577 void helper_mtc0_watchhi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1579 env->CP0_WatchHi[sel] = (arg1 & 0x40FF0FF8);
1580 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1583 void helper_mtc0_xcontext(CPUMIPSState *env, target_ulong arg1)
1585 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1586 env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1589 void helper_mtc0_framemask(CPUMIPSState *env, target_ulong arg1)
1591 env->CP0_Framemask = arg1; /* XXX */
1594 void helper_mtc0_debug(CPUMIPSState *env, target_ulong arg1)
1596 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
1597 if (arg1 & (1 << CP0DB_DM))
1598 env->hflags |= MIPS_HFLAG_DM;
1599 else
1600 env->hflags &= ~MIPS_HFLAG_DM;
1603 void helper_mttc0_debug(CPUMIPSState *env, target_ulong arg1)
1605 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1606 uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1607 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1609 /* XXX: Might be wrong, check with EJTAG spec. */
1610 if (other_tc == other->current_tc)
1611 other->active_tc.CP0_Debug_tcstatus = val;
1612 else
1613 other->tcs[other_tc].CP0_Debug_tcstatus = val;
1614 other->CP0_Debug = (other->CP0_Debug &
1615 ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1616 (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1619 void helper_mtc0_performance0(CPUMIPSState *env, target_ulong arg1)
1621 env->CP0_Performance0 = arg1 & 0x000007ff;
1624 void helper_mtc0_taglo(CPUMIPSState *env, target_ulong arg1)
1626 env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1629 void helper_mtc0_datalo(CPUMIPSState *env, target_ulong arg1)
1631 env->CP0_DataLo = arg1; /* XXX */
1634 void helper_mtc0_taghi(CPUMIPSState *env, target_ulong arg1)
1636 env->CP0_TagHi = arg1; /* XXX */
1639 void helper_mtc0_datahi(CPUMIPSState *env, target_ulong arg1)
1641 env->CP0_DataHi = arg1; /* XXX */
1644 /* MIPS MT functions */
1645 target_ulong helper_mftgpr(CPUMIPSState *env, uint32_t sel)
1647 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1648 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1650 if (other_tc == other->current_tc)
1651 return other->active_tc.gpr[sel];
1652 else
1653 return other->tcs[other_tc].gpr[sel];
1656 target_ulong helper_mftlo(CPUMIPSState *env, uint32_t sel)
1658 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1659 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1661 if (other_tc == other->current_tc)
1662 return other->active_tc.LO[sel];
1663 else
1664 return other->tcs[other_tc].LO[sel];
1667 target_ulong helper_mfthi(CPUMIPSState *env, uint32_t sel)
1669 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1670 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1672 if (other_tc == other->current_tc)
1673 return other->active_tc.HI[sel];
1674 else
1675 return other->tcs[other_tc].HI[sel];
1678 target_ulong helper_mftacx(CPUMIPSState *env, uint32_t sel)
1680 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1681 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1683 if (other_tc == other->current_tc)
1684 return other->active_tc.ACX[sel];
1685 else
1686 return other->tcs[other_tc].ACX[sel];
1689 target_ulong helper_mftdsp(CPUMIPSState *env)
1691 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1692 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1694 if (other_tc == other->current_tc)
1695 return other->active_tc.DSPControl;
1696 else
1697 return other->tcs[other_tc].DSPControl;
1700 void helper_mttgpr(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1702 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1703 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1705 if (other_tc == other->current_tc)
1706 other->active_tc.gpr[sel] = arg1;
1707 else
1708 other->tcs[other_tc].gpr[sel] = arg1;
1711 void helper_mttlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1713 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1714 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1716 if (other_tc == other->current_tc)
1717 other->active_tc.LO[sel] = arg1;
1718 else
1719 other->tcs[other_tc].LO[sel] = arg1;
1722 void helper_mtthi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1724 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1725 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1727 if (other_tc == other->current_tc)
1728 other->active_tc.HI[sel] = arg1;
1729 else
1730 other->tcs[other_tc].HI[sel] = arg1;
1733 void helper_mttacx(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1735 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1736 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1738 if (other_tc == other->current_tc)
1739 other->active_tc.ACX[sel] = arg1;
1740 else
1741 other->tcs[other_tc].ACX[sel] = arg1;
1744 void helper_mttdsp(CPUMIPSState *env, target_ulong arg1)
1746 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1747 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1749 if (other_tc == other->current_tc)
1750 other->active_tc.DSPControl = arg1;
1751 else
1752 other->tcs[other_tc].DSPControl = arg1;
1755 /* MIPS MT functions */
1756 target_ulong helper_dmt(void)
1758 // TODO
1759 return 0;
1762 target_ulong helper_emt(void)
1764 // TODO
1765 return 0;
1768 target_ulong helper_dvpe(CPUMIPSState *env)
1770 CPUState *other_cs = first_cpu;
1771 target_ulong prev = env->mvp->CP0_MVPControl;
1773 CPU_FOREACH(other_cs) {
1774 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1775 /* Turn off all VPEs except the one executing the dvpe. */
1776 if (&other_cpu->env != env) {
1777 other_cpu->env.mvp->CP0_MVPControl &= ~(1 << CP0MVPCo_EVP);
1778 mips_vpe_sleep(other_cpu);
1781 return prev;
1784 target_ulong helper_evpe(CPUMIPSState *env)
1786 CPUState *other_cs = first_cpu;
1787 target_ulong prev = env->mvp->CP0_MVPControl;
1789 CPU_FOREACH(other_cs) {
1790 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1792 if (&other_cpu->env != env
1793 /* If the VPE is WFI, don't disturb its sleep. */
1794 && !mips_vpe_is_wfi(other_cpu)) {
1795 /* Enable the VPE. */
1796 other_cpu->env.mvp->CP0_MVPControl |= (1 << CP0MVPCo_EVP);
1797 mips_vpe_wake(other_cpu); /* And wake it up. */
1800 return prev;
1802 #endif /* !CONFIG_USER_ONLY */
1804 void helper_fork(target_ulong arg1, target_ulong arg2)
1806 // arg1 = rt, arg2 = rs
1807 // TODO: store to TC register
1810 target_ulong helper_yield(CPUMIPSState *env, target_ulong arg)
1812 target_long arg1 = arg;
1814 if (arg1 < 0) {
1815 /* No scheduling policy implemented. */
1816 if (arg1 != -2) {
1817 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1818 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
1819 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1820 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1821 do_raise_exception(env, EXCP_THREAD, GETPC());
1824 } else if (arg1 == 0) {
1825 if (0 /* TODO: TC underflow */) {
1826 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1827 do_raise_exception(env, EXCP_THREAD, GETPC());
1828 } else {
1829 // TODO: Deallocate TC
1831 } else if (arg1 > 0) {
1832 /* Yield qualifier inputs not implemented. */
1833 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1834 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1835 do_raise_exception(env, EXCP_THREAD, GETPC());
1837 return env->CP0_YQMask;
1840 #ifndef CONFIG_USER_ONLY
1841 /* TLB management */
1842 static void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global)
1844 MIPSCPU *cpu = mips_env_get_cpu(env);
1846 /* Flush qemu's TLB and discard all shadowed entries. */
1847 tlb_flush(CPU(cpu), flush_global);
1848 env->tlb->tlb_in_use = env->tlb->nb_tlb;
1851 static void r4k_mips_tlb_flush_extra (CPUMIPSState *env, int first)
1853 /* Discard entries from env->tlb[first] onwards. */
1854 while (env->tlb->tlb_in_use > first) {
1855 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1859 static inline uint64_t get_tlb_pfn_from_entrylo(uint64_t entrylo)
1861 #if defined(TARGET_MIPS64)
1862 return extract64(entrylo, 6, 54);
1863 #else
1864 return extract64(entrylo, 6, 24) | /* PFN */
1865 (extract64(entrylo, 32, 32) << 24); /* PFNX */
1866 #endif
1869 static void r4k_fill_tlb(CPUMIPSState *env, int idx)
1871 r4k_tlb_t *tlb;
1873 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1874 tlb = &env->tlb->mmu.r4k.tlb[idx];
1875 if (env->CP0_EntryHi & (1 << CP0EnHi_EHINV)) {
1876 tlb->EHINV = 1;
1877 return;
1879 tlb->EHINV = 0;
1880 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1881 #if defined(TARGET_MIPS64)
1882 tlb->VPN &= env->SEGMask;
1883 #endif
1884 tlb->ASID = env->CP0_EntryHi & 0xFF;
1885 tlb->PageMask = env->CP0_PageMask;
1886 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1887 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
1888 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
1889 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
1890 tlb->XI0 = (env->CP0_EntryLo0 >> CP0EnLo_XI) & 1;
1891 tlb->RI0 = (env->CP0_EntryLo0 >> CP0EnLo_RI) & 1;
1892 tlb->PFN[0] = get_tlb_pfn_from_entrylo(env->CP0_EntryLo0) << 12;
1893 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
1894 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
1895 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
1896 tlb->XI1 = (env->CP0_EntryLo1 >> CP0EnLo_XI) & 1;
1897 tlb->RI1 = (env->CP0_EntryLo1 >> CP0EnLo_RI) & 1;
1898 tlb->PFN[1] = get_tlb_pfn_from_entrylo(env->CP0_EntryLo1) << 12;
1901 void r4k_helper_tlbinv(CPUMIPSState *env)
1903 int idx;
1904 r4k_tlb_t *tlb;
1905 uint8_t ASID = env->CP0_EntryHi & 0xFF;
1907 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
1908 tlb = &env->tlb->mmu.r4k.tlb[idx];
1909 if (!tlb->G && tlb->ASID == ASID) {
1910 tlb->EHINV = 1;
1913 cpu_mips_tlb_flush(env, 1);
1916 void r4k_helper_tlbinvf(CPUMIPSState *env)
1918 int idx;
1920 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
1921 env->tlb->mmu.r4k.tlb[idx].EHINV = 1;
1923 cpu_mips_tlb_flush(env, 1);
1926 void r4k_helper_tlbwi(CPUMIPSState *env)
1928 r4k_tlb_t *tlb;
1929 int idx;
1930 target_ulong VPN;
1931 uint8_t ASID;
1932 bool G, V0, D0, V1, D1;
1934 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1935 tlb = &env->tlb->mmu.r4k.tlb[idx];
1936 VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1937 #if defined(TARGET_MIPS64)
1938 VPN &= env->SEGMask;
1939 #endif
1940 ASID = env->CP0_EntryHi & 0xff;
1941 G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1942 V0 = (env->CP0_EntryLo0 & 2) != 0;
1943 D0 = (env->CP0_EntryLo0 & 4) != 0;
1944 V1 = (env->CP0_EntryLo1 & 2) != 0;
1945 D1 = (env->CP0_EntryLo1 & 4) != 0;
1947 /* Discard cached TLB entries, unless tlbwi is just upgrading access
1948 permissions on the current entry. */
1949 if (tlb->VPN != VPN || tlb->ASID != ASID || tlb->G != G ||
1950 (tlb->V0 && !V0) || (tlb->D0 && !D0) ||
1951 (tlb->V1 && !V1) || (tlb->D1 && !D1)) {
1952 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
1955 r4k_invalidate_tlb(env, idx, 0);
1956 r4k_fill_tlb(env, idx);
1959 void r4k_helper_tlbwr(CPUMIPSState *env)
1961 int r = cpu_mips_get_random(env);
1963 r4k_invalidate_tlb(env, r, 1);
1964 r4k_fill_tlb(env, r);
1967 void r4k_helper_tlbp(CPUMIPSState *env)
1969 r4k_tlb_t *tlb;
1970 target_ulong mask;
1971 target_ulong tag;
1972 target_ulong VPN;
1973 uint8_t ASID;
1974 int i;
1976 ASID = env->CP0_EntryHi & 0xFF;
1977 for (i = 0; i < env->tlb->nb_tlb; i++) {
1978 tlb = &env->tlb->mmu.r4k.tlb[i];
1979 /* 1k pages are not supported. */
1980 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1981 tag = env->CP0_EntryHi & ~mask;
1982 VPN = tlb->VPN & ~mask;
1983 #if defined(TARGET_MIPS64)
1984 tag &= env->SEGMask;
1985 #endif
1986 /* Check ASID, virtual page number & size */
1987 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
1988 /* TLB match */
1989 env->CP0_Index = i;
1990 break;
1993 if (i == env->tlb->nb_tlb) {
1994 /* No match. Discard any shadow entries, if any of them match. */
1995 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
1996 tlb = &env->tlb->mmu.r4k.tlb[i];
1997 /* 1k pages are not supported. */
1998 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1999 tag = env->CP0_EntryHi & ~mask;
2000 VPN = tlb->VPN & ~mask;
2001 #if defined(TARGET_MIPS64)
2002 tag &= env->SEGMask;
2003 #endif
2004 /* Check ASID, virtual page number & size */
2005 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
2006 r4k_mips_tlb_flush_extra (env, i);
2007 break;
2011 env->CP0_Index |= 0x80000000;
2015 static inline uint64_t get_entrylo_pfn_from_tlb(uint64_t tlb_pfn)
2017 #if defined(TARGET_MIPS64)
2018 return tlb_pfn << 6;
2019 #else
2020 return (extract64(tlb_pfn, 0, 24) << 6) | /* PFN */
2021 (extract64(tlb_pfn, 24, 32) << 32); /* PFNX */
2022 #endif
2025 void r4k_helper_tlbr(CPUMIPSState *env)
2027 r4k_tlb_t *tlb;
2028 uint8_t ASID;
2029 int idx;
2031 ASID = env->CP0_EntryHi & 0xFF;
2032 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
2033 tlb = &env->tlb->mmu.r4k.tlb[idx];
2035 /* If this will change the current ASID, flush qemu's TLB. */
2036 if (ASID != tlb->ASID)
2037 cpu_mips_tlb_flush (env, 1);
2039 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
2041 if (tlb->EHINV) {
2042 env->CP0_EntryHi = 1 << CP0EnHi_EHINV;
2043 env->CP0_PageMask = 0;
2044 env->CP0_EntryLo0 = 0;
2045 env->CP0_EntryLo1 = 0;
2046 } else {
2047 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
2048 env->CP0_PageMask = tlb->PageMask;
2049 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
2050 ((uint64_t)tlb->RI0 << CP0EnLo_RI) |
2051 ((uint64_t)tlb->XI0 << CP0EnLo_XI) | (tlb->C0 << 3) |
2052 get_entrylo_pfn_from_tlb(tlb->PFN[0] >> 12);
2053 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
2054 ((uint64_t)tlb->RI1 << CP0EnLo_RI) |
2055 ((uint64_t)tlb->XI1 << CP0EnLo_XI) | (tlb->C1 << 3) |
2056 get_entrylo_pfn_from_tlb(tlb->PFN[1] >> 12);
2060 void helper_tlbwi(CPUMIPSState *env)
2062 env->tlb->helper_tlbwi(env);
2065 void helper_tlbwr(CPUMIPSState *env)
2067 env->tlb->helper_tlbwr(env);
2070 void helper_tlbp(CPUMIPSState *env)
2072 env->tlb->helper_tlbp(env);
2075 void helper_tlbr(CPUMIPSState *env)
2077 env->tlb->helper_tlbr(env);
2080 void helper_tlbinv(CPUMIPSState *env)
2082 env->tlb->helper_tlbinv(env);
2085 void helper_tlbinvf(CPUMIPSState *env)
2087 env->tlb->helper_tlbinvf(env);
2090 /* Specials */
2091 target_ulong helper_di(CPUMIPSState *env)
2093 target_ulong t0 = env->CP0_Status;
2095 env->CP0_Status = t0 & ~(1 << CP0St_IE);
2096 return t0;
2099 target_ulong helper_ei(CPUMIPSState *env)
2101 target_ulong t0 = env->CP0_Status;
2103 env->CP0_Status = t0 | (1 << CP0St_IE);
2104 return t0;
2107 static void debug_pre_eret(CPUMIPSState *env)
2109 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2110 qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2111 env->active_tc.PC, env->CP0_EPC);
2112 if (env->CP0_Status & (1 << CP0St_ERL))
2113 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2114 if (env->hflags & MIPS_HFLAG_DM)
2115 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2116 qemu_log("\n");
2120 static void debug_post_eret(CPUMIPSState *env)
2122 MIPSCPU *cpu = mips_env_get_cpu(env);
2124 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2125 qemu_log(" => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2126 env->active_tc.PC, env->CP0_EPC);
2127 if (env->CP0_Status & (1 << CP0St_ERL))
2128 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2129 if (env->hflags & MIPS_HFLAG_DM)
2130 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2131 switch (env->hflags & MIPS_HFLAG_KSU) {
2132 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
2133 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
2134 case MIPS_HFLAG_KM: qemu_log("\n"); break;
2135 default:
2136 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
2137 break;
2142 static void set_pc(CPUMIPSState *env, target_ulong error_pc)
2144 env->active_tc.PC = error_pc & ~(target_ulong)1;
2145 if (error_pc & 1) {
2146 env->hflags |= MIPS_HFLAG_M16;
2147 } else {
2148 env->hflags &= ~(MIPS_HFLAG_M16);
2152 static inline void exception_return(CPUMIPSState *env)
2154 debug_pre_eret(env);
2155 if (env->CP0_Status & (1 << CP0St_ERL)) {
2156 set_pc(env, env->CP0_ErrorEPC);
2157 env->CP0_Status &= ~(1 << CP0St_ERL);
2158 } else {
2159 set_pc(env, env->CP0_EPC);
2160 env->CP0_Status &= ~(1 << CP0St_EXL);
2162 compute_hflags(env);
2163 debug_post_eret(env);
2166 void helper_eret(CPUMIPSState *env)
2168 exception_return(env);
2169 env->lladdr = 1;
2172 void helper_eretnc(CPUMIPSState *env)
2174 exception_return(env);
2177 void helper_deret(CPUMIPSState *env)
2179 debug_pre_eret(env);
2180 set_pc(env, env->CP0_DEPC);
2182 env->hflags &= ~MIPS_HFLAG_DM;
2183 compute_hflags(env);
2184 debug_post_eret(env);
2186 #endif /* !CONFIG_USER_ONLY */
2188 target_ulong helper_rdhwr_cpunum(CPUMIPSState *env)
2190 if ((env->hflags & MIPS_HFLAG_CP0) ||
2191 (env->CP0_HWREna & (1 << 0)))
2192 return env->CP0_EBase & 0x3ff;
2193 else
2194 do_raise_exception(env, EXCP_RI, GETPC());
2196 return 0;
2199 target_ulong helper_rdhwr_synci_step(CPUMIPSState *env)
2201 if ((env->hflags & MIPS_HFLAG_CP0) ||
2202 (env->CP0_HWREna & (1 << 1)))
2203 return env->SYNCI_Step;
2204 else
2205 do_raise_exception(env, EXCP_RI, GETPC());
2207 return 0;
2210 target_ulong helper_rdhwr_cc(CPUMIPSState *env)
2212 if ((env->hflags & MIPS_HFLAG_CP0) ||
2213 (env->CP0_HWREna & (1 << 2))) {
2214 #ifdef CONFIG_USER_ONLY
2215 return env->CP0_Count;
2216 #else
2217 return (int32_t)cpu_mips_get_count(env);
2218 #endif
2219 } else {
2220 do_raise_exception(env, EXCP_RI, GETPC());
2223 return 0;
2226 target_ulong helper_rdhwr_ccres(CPUMIPSState *env)
2228 if ((env->hflags & MIPS_HFLAG_CP0) ||
2229 (env->CP0_HWREna & (1 << 3)))
2230 return env->CCRes;
2231 else
2232 do_raise_exception(env, EXCP_RI, GETPC());
2234 return 0;
2237 void helper_pmon(CPUMIPSState *env, int function)
2239 function /= 2;
2240 switch (function) {
2241 case 2: /* TODO: char inbyte(int waitflag); */
2242 if (env->active_tc.gpr[4] == 0)
2243 env->active_tc.gpr[2] = -1;
2244 /* Fall through */
2245 case 11: /* TODO: char inbyte (void); */
2246 env->active_tc.gpr[2] = -1;
2247 break;
2248 case 3:
2249 case 12:
2250 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
2251 break;
2252 case 17:
2253 break;
2254 case 158:
2256 unsigned char *fmt = (void *)(uintptr_t)env->active_tc.gpr[4];
2257 printf("%s", fmt);
2259 break;
2263 void helper_wait(CPUMIPSState *env)
2265 CPUState *cs = CPU(mips_env_get_cpu(env));
2267 cs->halted = 1;
2268 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
2269 /* Last instruction in the block, PC was updated before
2270 - no need to recover PC and icount */
2271 raise_exception(env, EXCP_HLT);
2274 #if !defined(CONFIG_USER_ONLY)
2276 void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
2277 int access_type, int is_user,
2278 uintptr_t retaddr)
2280 MIPSCPU *cpu = MIPS_CPU(cs);
2281 CPUMIPSState *env = &cpu->env;
2282 int error_code = 0;
2283 int excp;
2285 env->CP0_BadVAddr = addr;
2287 if (access_type == MMU_DATA_STORE) {
2288 excp = EXCP_AdES;
2289 } else {
2290 excp = EXCP_AdEL;
2291 if (access_type == MMU_INST_FETCH) {
2292 error_code |= EXCP_INST_NOTAVAIL;
2296 do_raise_exception_err(env, excp, error_code, retaddr);
2299 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
2300 uintptr_t retaddr)
2302 int ret;
2304 ret = mips_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
2305 if (ret) {
2306 MIPSCPU *cpu = MIPS_CPU(cs);
2307 CPUMIPSState *env = &cpu->env;
2309 do_raise_exception_err(env, cs->exception_index,
2310 env->error_code, retaddr);
2314 void mips_cpu_unassigned_access(CPUState *cs, hwaddr addr,
2315 bool is_write, bool is_exec, int unused,
2316 unsigned size)
2318 MIPSCPU *cpu = MIPS_CPU(cs);
2319 CPUMIPSState *env = &cpu->env;
2322 * Raising an exception with KVM enabled will crash because it won't be from
2323 * the main execution loop so the longjmp won't have a matching setjmp.
2324 * Until we can trigger a bus error exception through KVM lets just ignore
2325 * the access.
2327 if (kvm_enabled()) {
2328 return;
2331 if (is_exec) {
2332 raise_exception(env, EXCP_IBE);
2333 } else {
2334 raise_exception(env, EXCP_DBE);
2337 #endif /* !CONFIG_USER_ONLY */
2339 /* Complex FPU operations which may need stack space. */
2341 #define FLOAT_TWO32 make_float32(1 << 30)
2342 #define FLOAT_TWO64 make_float64(1ULL << 62)
2343 #define FP_TO_INT32_OVERFLOW 0x7fffffff
2344 #define FP_TO_INT64_OVERFLOW 0x7fffffffffffffffULL
2346 /* convert MIPS rounding mode in FCR31 to IEEE library */
2347 unsigned int ieee_rm[] = {
2348 float_round_nearest_even,
2349 float_round_to_zero,
2350 float_round_up,
2351 float_round_down
2354 target_ulong helper_cfc1(CPUMIPSState *env, uint32_t reg)
2356 target_ulong arg1 = 0;
2358 switch (reg) {
2359 case 0:
2360 arg1 = (int32_t)env->active_fpu.fcr0;
2361 break;
2362 case 1:
2363 /* UFR Support - Read Status FR */
2364 if (env->active_fpu.fcr0 & (1 << FCR0_UFRP)) {
2365 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2366 arg1 = (int32_t)
2367 ((env->CP0_Status & (1 << CP0St_FR)) >> CP0St_FR);
2368 } else {
2369 do_raise_exception(env, EXCP_RI, GETPC());
2372 break;
2373 case 5:
2374 /* FRE Support - read Config5.FRE bit */
2375 if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
2376 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2377 arg1 = (env->CP0_Config5 >> CP0C5_FRE) & 1;
2378 } else {
2379 helper_raise_exception(env, EXCP_RI);
2382 break;
2383 case 25:
2384 arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);
2385 break;
2386 case 26:
2387 arg1 = env->active_fpu.fcr31 & 0x0003f07c;
2388 break;
2389 case 28:
2390 arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);
2391 break;
2392 default:
2393 arg1 = (int32_t)env->active_fpu.fcr31;
2394 break;
2397 return arg1;
2400 void helper_ctc1(CPUMIPSState *env, target_ulong arg1, uint32_t fs, uint32_t rt)
2402 switch (fs) {
2403 case 1:
2404 /* UFR Alias - Reset Status FR */
2405 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2406 return;
2408 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2409 env->CP0_Status &= ~(1 << CP0St_FR);
2410 compute_hflags(env);
2411 } else {
2412 do_raise_exception(env, EXCP_RI, GETPC());
2414 break;
2415 case 4:
2416 /* UNFR Alias - Set Status FR */
2417 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2418 return;
2420 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2421 env->CP0_Status |= (1 << CP0St_FR);
2422 compute_hflags(env);
2423 } else {
2424 do_raise_exception(env, EXCP_RI, GETPC());
2426 break;
2427 case 5:
2428 /* FRE Support - clear Config5.FRE bit */
2429 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2430 return;
2432 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2433 env->CP0_Config5 &= ~(1 << CP0C5_FRE);
2434 compute_hflags(env);
2435 } else {
2436 helper_raise_exception(env, EXCP_RI);
2438 break;
2439 case 6:
2440 /* FRE Support - set Config5.FRE bit */
2441 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2442 return;
2444 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2445 env->CP0_Config5 |= (1 << CP0C5_FRE);
2446 compute_hflags(env);
2447 } else {
2448 helper_raise_exception(env, EXCP_RI);
2450 break;
2451 case 25:
2452 if ((env->insn_flags & ISA_MIPS32R6) || (arg1 & 0xffffff00)) {
2453 return;
2455 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
2456 ((arg1 & 0x1) << 23);
2457 break;
2458 case 26:
2459 if (arg1 & 0x007c0000)
2460 return;
2461 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
2462 break;
2463 case 28:
2464 if (arg1 & 0x007c0000)
2465 return;
2466 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
2467 ((arg1 & 0x4) << 22);
2468 break;
2469 case 31:
2470 if (env->insn_flags & ISA_MIPS32R6) {
2471 uint32_t mask = 0xfefc0000;
2472 env->active_fpu.fcr31 = (arg1 & ~mask) |
2473 (env->active_fpu.fcr31 & mask);
2474 } else if (!(arg1 & 0x007c0000)) {
2475 env->active_fpu.fcr31 = arg1;
2477 break;
2478 default:
2479 return;
2481 /* set rounding mode */
2482 restore_rounding_mode(env);
2483 /* set flush-to-zero mode */
2484 restore_flush_mode(env);
2485 set_float_exception_flags(0, &env->active_fpu.fp_status);
2486 if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
2487 do_raise_exception(env, EXCP_FPE, GETPC());
2490 int ieee_ex_to_mips(int xcpt)
2492 int ret = 0;
2493 if (xcpt) {
2494 if (xcpt & float_flag_invalid) {
2495 ret |= FP_INVALID;
2497 if (xcpt & float_flag_overflow) {
2498 ret |= FP_OVERFLOW;
2500 if (xcpt & float_flag_underflow) {
2501 ret |= FP_UNDERFLOW;
2503 if (xcpt & float_flag_divbyzero) {
2504 ret |= FP_DIV0;
2506 if (xcpt & float_flag_inexact) {
2507 ret |= FP_INEXACT;
2510 return ret;
2513 static inline void update_fcr31(CPUMIPSState *env, uintptr_t pc)
2515 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
2517 SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
2519 if (tmp) {
2520 set_float_exception_flags(0, &env->active_fpu.fp_status);
2522 if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp) {
2523 do_raise_exception(env, EXCP_FPE, pc);
2524 } else {
2525 UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
2530 /* Float support.
2531 Single precition routines have a "s" suffix, double precision a
2532 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
2533 paired single lower "pl", paired single upper "pu". */
2535 /* unary operations, modifying fp status */
2536 uint64_t helper_float_sqrt_d(CPUMIPSState *env, uint64_t fdt0)
2538 fdt0 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2539 update_fcr31(env, GETPC());
2540 return fdt0;
2543 uint32_t helper_float_sqrt_s(CPUMIPSState *env, uint32_t fst0)
2545 fst0 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2546 update_fcr31(env, GETPC());
2547 return fst0;
2550 uint64_t helper_float_cvtd_s(CPUMIPSState *env, uint32_t fst0)
2552 uint64_t fdt2;
2554 fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
2555 update_fcr31(env, GETPC());
2556 return fdt2;
2559 uint64_t helper_float_cvtd_w(CPUMIPSState *env, uint32_t wt0)
2561 uint64_t fdt2;
2563 fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2564 update_fcr31(env, GETPC());
2565 return fdt2;
2568 uint64_t helper_float_cvtd_l(CPUMIPSState *env, uint64_t dt0)
2570 uint64_t fdt2;
2572 fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2573 update_fcr31(env, GETPC());
2574 return fdt2;
2577 uint64_t helper_float_cvtl_d(CPUMIPSState *env, uint64_t fdt0)
2579 uint64_t dt2;
2581 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2582 if (get_float_exception_flags(&env->active_fpu.fp_status)
2583 & (float_flag_invalid | float_flag_overflow)) {
2584 dt2 = FP_TO_INT64_OVERFLOW;
2586 update_fcr31(env, GETPC());
2587 return dt2;
2590 uint64_t helper_float_cvtl_s(CPUMIPSState *env, uint32_t fst0)
2592 uint64_t dt2;
2594 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2595 if (get_float_exception_flags(&env->active_fpu.fp_status)
2596 & (float_flag_invalid | float_flag_overflow)) {
2597 dt2 = FP_TO_INT64_OVERFLOW;
2599 update_fcr31(env, GETPC());
2600 return dt2;
2603 uint64_t helper_float_cvtps_pw(CPUMIPSState *env, uint64_t dt0)
2605 uint32_t fst2;
2606 uint32_t fsth2;
2608 fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2609 fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
2610 update_fcr31(env, GETPC());
2611 return ((uint64_t)fsth2 << 32) | fst2;
2614 uint64_t helper_float_cvtpw_ps(CPUMIPSState *env, uint64_t fdt0)
2616 uint32_t wt2;
2617 uint32_t wth2;
2618 int excp, excph;
2620 wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2621 excp = get_float_exception_flags(&env->active_fpu.fp_status);
2622 if (excp & (float_flag_overflow | float_flag_invalid)) {
2623 wt2 = FP_TO_INT32_OVERFLOW;
2626 set_float_exception_flags(0, &env->active_fpu.fp_status);
2627 wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
2628 excph = get_float_exception_flags(&env->active_fpu.fp_status);
2629 if (excph & (float_flag_overflow | float_flag_invalid)) {
2630 wth2 = FP_TO_INT32_OVERFLOW;
2633 set_float_exception_flags(excp | excph, &env->active_fpu.fp_status);
2634 update_fcr31(env, GETPC());
2636 return ((uint64_t)wth2 << 32) | wt2;
2639 uint32_t helper_float_cvts_d(CPUMIPSState *env, uint64_t fdt0)
2641 uint32_t fst2;
2643 fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
2644 update_fcr31(env, GETPC());
2645 return fst2;
2648 uint32_t helper_float_cvts_w(CPUMIPSState *env, uint32_t wt0)
2650 uint32_t fst2;
2652 fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
2653 update_fcr31(env, GETPC());
2654 return fst2;
2657 uint32_t helper_float_cvts_l(CPUMIPSState *env, uint64_t dt0)
2659 uint32_t fst2;
2661 fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
2662 update_fcr31(env, GETPC());
2663 return fst2;
2666 uint32_t helper_float_cvts_pl(CPUMIPSState *env, uint32_t wt0)
2668 uint32_t wt2;
2670 wt2 = wt0;
2671 update_fcr31(env, GETPC());
2672 return wt2;
2675 uint32_t helper_float_cvts_pu(CPUMIPSState *env, uint32_t wth0)
2677 uint32_t wt2;
2679 wt2 = wth0;
2680 update_fcr31(env, GETPC());
2681 return wt2;
2684 uint32_t helper_float_cvtw_s(CPUMIPSState *env, uint32_t fst0)
2686 uint32_t wt2;
2688 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2689 if (get_float_exception_flags(&env->active_fpu.fp_status)
2690 & (float_flag_invalid | float_flag_overflow)) {
2691 wt2 = FP_TO_INT32_OVERFLOW;
2693 update_fcr31(env, GETPC());
2694 return wt2;
2697 uint32_t helper_float_cvtw_d(CPUMIPSState *env, uint64_t fdt0)
2699 uint32_t wt2;
2701 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2702 if (get_float_exception_flags(&env->active_fpu.fp_status)
2703 & (float_flag_invalid | float_flag_overflow)) {
2704 wt2 = FP_TO_INT32_OVERFLOW;
2706 update_fcr31(env, GETPC());
2707 return wt2;
2710 uint64_t helper_float_roundl_d(CPUMIPSState *env, uint64_t fdt0)
2712 uint64_t dt2;
2714 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2715 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2716 restore_rounding_mode(env);
2717 if (get_float_exception_flags(&env->active_fpu.fp_status)
2718 & (float_flag_invalid | float_flag_overflow)) {
2719 dt2 = FP_TO_INT64_OVERFLOW;
2721 update_fcr31(env, GETPC());
2722 return dt2;
2725 uint64_t helper_float_roundl_s(CPUMIPSState *env, uint32_t fst0)
2727 uint64_t dt2;
2729 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2730 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2731 restore_rounding_mode(env);
2732 if (get_float_exception_flags(&env->active_fpu.fp_status)
2733 & (float_flag_invalid | float_flag_overflow)) {
2734 dt2 = FP_TO_INT64_OVERFLOW;
2736 update_fcr31(env, GETPC());
2737 return dt2;
2740 uint32_t helper_float_roundw_d(CPUMIPSState *env, uint64_t fdt0)
2742 uint32_t wt2;
2744 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2745 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2746 restore_rounding_mode(env);
2747 if (get_float_exception_flags(&env->active_fpu.fp_status)
2748 & (float_flag_invalid | float_flag_overflow)) {
2749 wt2 = FP_TO_INT32_OVERFLOW;
2751 update_fcr31(env, GETPC());
2752 return wt2;
2755 uint32_t helper_float_roundw_s(CPUMIPSState *env, uint32_t fst0)
2757 uint32_t wt2;
2759 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2760 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2761 restore_rounding_mode(env);
2762 if (get_float_exception_flags(&env->active_fpu.fp_status)
2763 & (float_flag_invalid | float_flag_overflow)) {
2764 wt2 = FP_TO_INT32_OVERFLOW;
2766 update_fcr31(env, GETPC());
2767 return wt2;
2770 uint64_t helper_float_truncl_d(CPUMIPSState *env, uint64_t fdt0)
2772 uint64_t dt2;
2774 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
2775 if (get_float_exception_flags(&env->active_fpu.fp_status)
2776 & (float_flag_invalid | float_flag_overflow)) {
2777 dt2 = FP_TO_INT64_OVERFLOW;
2779 update_fcr31(env, GETPC());
2780 return dt2;
2783 uint64_t helper_float_truncl_s(CPUMIPSState *env, uint32_t fst0)
2785 uint64_t dt2;
2787 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
2788 if (get_float_exception_flags(&env->active_fpu.fp_status)
2789 & (float_flag_invalid | float_flag_overflow)) {
2790 dt2 = FP_TO_INT64_OVERFLOW;
2792 update_fcr31(env, GETPC());
2793 return dt2;
2796 uint32_t helper_float_truncw_d(CPUMIPSState *env, uint64_t fdt0)
2798 uint32_t wt2;
2800 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
2801 if (get_float_exception_flags(&env->active_fpu.fp_status)
2802 & (float_flag_invalid | float_flag_overflow)) {
2803 wt2 = FP_TO_INT32_OVERFLOW;
2805 update_fcr31(env, GETPC());
2806 return wt2;
2809 uint32_t helper_float_truncw_s(CPUMIPSState *env, uint32_t fst0)
2811 uint32_t wt2;
2813 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
2814 if (get_float_exception_flags(&env->active_fpu.fp_status)
2815 & (float_flag_invalid | float_flag_overflow)) {
2816 wt2 = FP_TO_INT32_OVERFLOW;
2818 update_fcr31(env, GETPC());
2819 return wt2;
2822 uint64_t helper_float_ceill_d(CPUMIPSState *env, uint64_t fdt0)
2824 uint64_t dt2;
2826 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2827 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2828 restore_rounding_mode(env);
2829 if (get_float_exception_flags(&env->active_fpu.fp_status)
2830 & (float_flag_invalid | float_flag_overflow)) {
2831 dt2 = FP_TO_INT64_OVERFLOW;
2833 update_fcr31(env, GETPC());
2834 return dt2;
2837 uint64_t helper_float_ceill_s(CPUMIPSState *env, uint32_t fst0)
2839 uint64_t dt2;
2841 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2842 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2843 restore_rounding_mode(env);
2844 if (get_float_exception_flags(&env->active_fpu.fp_status)
2845 & (float_flag_invalid | float_flag_overflow)) {
2846 dt2 = FP_TO_INT64_OVERFLOW;
2848 update_fcr31(env, GETPC());
2849 return dt2;
2852 uint32_t helper_float_ceilw_d(CPUMIPSState *env, uint64_t fdt0)
2854 uint32_t wt2;
2856 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2857 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2858 restore_rounding_mode(env);
2859 if (get_float_exception_flags(&env->active_fpu.fp_status)
2860 & (float_flag_invalid | float_flag_overflow)) {
2861 wt2 = FP_TO_INT32_OVERFLOW;
2863 update_fcr31(env, GETPC());
2864 return wt2;
2867 uint32_t helper_float_ceilw_s(CPUMIPSState *env, uint32_t fst0)
2869 uint32_t wt2;
2871 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2872 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2873 restore_rounding_mode(env);
2874 if (get_float_exception_flags(&env->active_fpu.fp_status)
2875 & (float_flag_invalid | float_flag_overflow)) {
2876 wt2 = FP_TO_INT32_OVERFLOW;
2878 update_fcr31(env, GETPC());
2879 return wt2;
2882 uint64_t helper_float_floorl_d(CPUMIPSState *env, uint64_t fdt0)
2884 uint64_t dt2;
2886 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2887 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2888 restore_rounding_mode(env);
2889 if (get_float_exception_flags(&env->active_fpu.fp_status)
2890 & (float_flag_invalid | float_flag_overflow)) {
2891 dt2 = FP_TO_INT64_OVERFLOW;
2893 update_fcr31(env, GETPC());
2894 return dt2;
2897 uint64_t helper_float_floorl_s(CPUMIPSState *env, uint32_t fst0)
2899 uint64_t dt2;
2901 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2902 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2903 restore_rounding_mode(env);
2904 if (get_float_exception_flags(&env->active_fpu.fp_status)
2905 & (float_flag_invalid | float_flag_overflow)) {
2906 dt2 = FP_TO_INT64_OVERFLOW;
2908 update_fcr31(env, GETPC());
2909 return dt2;
2912 uint32_t helper_float_floorw_d(CPUMIPSState *env, uint64_t fdt0)
2914 uint32_t wt2;
2916 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2917 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2918 restore_rounding_mode(env);
2919 if (get_float_exception_flags(&env->active_fpu.fp_status)
2920 & (float_flag_invalid | float_flag_overflow)) {
2921 wt2 = FP_TO_INT32_OVERFLOW;
2923 update_fcr31(env, GETPC());
2924 return wt2;
2927 uint32_t helper_float_floorw_s(CPUMIPSState *env, uint32_t fst0)
2929 uint32_t wt2;
2931 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2932 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2933 restore_rounding_mode(env);
2934 if (get_float_exception_flags(&env->active_fpu.fp_status)
2935 & (float_flag_invalid | float_flag_overflow)) {
2936 wt2 = FP_TO_INT32_OVERFLOW;
2938 update_fcr31(env, GETPC());
2939 return wt2;
2942 /* unary operations, not modifying fp status */
2943 #define FLOAT_UNOP(name) \
2944 uint64_t helper_float_ ## name ## _d(uint64_t fdt0) \
2946 return float64_ ## name(fdt0); \
2948 uint32_t helper_float_ ## name ## _s(uint32_t fst0) \
2950 return float32_ ## name(fst0); \
2952 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0) \
2954 uint32_t wt0; \
2955 uint32_t wth0; \
2957 wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF); \
2958 wth0 = float32_ ## name(fdt0 >> 32); \
2959 return ((uint64_t)wth0 << 32) | wt0; \
2961 FLOAT_UNOP(abs)
2962 FLOAT_UNOP(chs)
2963 #undef FLOAT_UNOP
2965 /* MIPS specific unary operations */
2966 uint64_t helper_float_recip_d(CPUMIPSState *env, uint64_t fdt0)
2968 uint64_t fdt2;
2970 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
2971 update_fcr31(env, GETPC());
2972 return fdt2;
2975 uint32_t helper_float_recip_s(CPUMIPSState *env, uint32_t fst0)
2977 uint32_t fst2;
2979 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
2980 update_fcr31(env, GETPC());
2981 return fst2;
2984 uint64_t helper_float_rsqrt_d(CPUMIPSState *env, uint64_t fdt0)
2986 uint64_t fdt2;
2988 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2989 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
2990 update_fcr31(env, GETPC());
2991 return fdt2;
2994 uint32_t helper_float_rsqrt_s(CPUMIPSState *env, uint32_t fst0)
2996 uint32_t fst2;
2998 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2999 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3000 update_fcr31(env, GETPC());
3001 return fst2;
3004 uint64_t helper_float_recip1_d(CPUMIPSState *env, uint64_t fdt0)
3006 uint64_t fdt2;
3008 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
3009 update_fcr31(env, GETPC());
3010 return fdt2;
3013 uint32_t helper_float_recip1_s(CPUMIPSState *env, uint32_t fst0)
3015 uint32_t fst2;
3017 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
3018 update_fcr31(env, GETPC());
3019 return fst2;
3022 uint64_t helper_float_recip1_ps(CPUMIPSState *env, uint64_t fdt0)
3024 uint32_t fst2;
3025 uint32_t fsth2;
3027 fst2 = float32_div(float32_one, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3028 fsth2 = float32_div(float32_one, fdt0 >> 32, &env->active_fpu.fp_status);
3029 update_fcr31(env, GETPC());
3030 return ((uint64_t)fsth2 << 32) | fst2;
3033 uint64_t helper_float_rsqrt1_d(CPUMIPSState *env, uint64_t fdt0)
3035 uint64_t fdt2;
3037 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
3038 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
3039 update_fcr31(env, GETPC());
3040 return fdt2;
3043 uint32_t helper_float_rsqrt1_s(CPUMIPSState *env, uint32_t fst0)
3045 uint32_t fst2;
3047 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
3048 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3049 update_fcr31(env, GETPC());
3050 return fst2;
3053 uint64_t helper_float_rsqrt1_ps(CPUMIPSState *env, uint64_t fdt0)
3055 uint32_t fst2;
3056 uint32_t fsth2;
3058 fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3059 fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
3060 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3061 fsth2 = float32_div(float32_one, fsth2, &env->active_fpu.fp_status);
3062 update_fcr31(env, GETPC());
3063 return ((uint64_t)fsth2 << 32) | fst2;
3066 #define FLOAT_RINT(name, bits) \
3067 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3068 uint ## bits ## _t fs) \
3070 uint ## bits ## _t fdret; \
3072 fdret = float ## bits ## _round_to_int(fs, &env->active_fpu.fp_status); \
3073 update_fcr31(env, GETPC()); \
3074 return fdret; \
3077 FLOAT_RINT(rint_s, 32)
3078 FLOAT_RINT(rint_d, 64)
3079 #undef FLOAT_RINT
3081 #define FLOAT_CLASS_SIGNALING_NAN 0x001
3082 #define FLOAT_CLASS_QUIET_NAN 0x002
3083 #define FLOAT_CLASS_NEGATIVE_INFINITY 0x004
3084 #define FLOAT_CLASS_NEGATIVE_NORMAL 0x008
3085 #define FLOAT_CLASS_NEGATIVE_SUBNORMAL 0x010
3086 #define FLOAT_CLASS_NEGATIVE_ZERO 0x020
3087 #define FLOAT_CLASS_POSITIVE_INFINITY 0x040
3088 #define FLOAT_CLASS_POSITIVE_NORMAL 0x080
3089 #define FLOAT_CLASS_POSITIVE_SUBNORMAL 0x100
3090 #define FLOAT_CLASS_POSITIVE_ZERO 0x200
3092 #define FLOAT_CLASS(name, bits) \
3093 uint ## bits ## _t helper_float_ ## name (uint ## bits ## _t arg) \
3095 if (float ## bits ## _is_signaling_nan(arg)) { \
3096 return FLOAT_CLASS_SIGNALING_NAN; \
3097 } else if (float ## bits ## _is_quiet_nan(arg)) { \
3098 return FLOAT_CLASS_QUIET_NAN; \
3099 } else if (float ## bits ## _is_neg(arg)) { \
3100 if (float ## bits ## _is_infinity(arg)) { \
3101 return FLOAT_CLASS_NEGATIVE_INFINITY; \
3102 } else if (float ## bits ## _is_zero(arg)) { \
3103 return FLOAT_CLASS_NEGATIVE_ZERO; \
3104 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3105 return FLOAT_CLASS_NEGATIVE_SUBNORMAL; \
3106 } else { \
3107 return FLOAT_CLASS_NEGATIVE_NORMAL; \
3109 } else { \
3110 if (float ## bits ## _is_infinity(arg)) { \
3111 return FLOAT_CLASS_POSITIVE_INFINITY; \
3112 } else if (float ## bits ## _is_zero(arg)) { \
3113 return FLOAT_CLASS_POSITIVE_ZERO; \
3114 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3115 return FLOAT_CLASS_POSITIVE_SUBNORMAL; \
3116 } else { \
3117 return FLOAT_CLASS_POSITIVE_NORMAL; \
3122 FLOAT_CLASS(class_s, 32)
3123 FLOAT_CLASS(class_d, 64)
3124 #undef FLOAT_CLASS
3126 /* binary operations */
3127 #define FLOAT_BINOP(name) \
3128 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
3129 uint64_t fdt0, uint64_t fdt1) \
3131 uint64_t dt2; \
3133 dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status); \
3134 update_fcr31(env, GETPC()); \
3135 return dt2; \
3138 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
3139 uint32_t fst0, uint32_t fst1) \
3141 uint32_t wt2; \
3143 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3144 update_fcr31(env, GETPC()); \
3145 return wt2; \
3148 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
3149 uint64_t fdt0, \
3150 uint64_t fdt1) \
3152 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3153 uint32_t fsth0 = fdt0 >> 32; \
3154 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3155 uint32_t fsth1 = fdt1 >> 32; \
3156 uint32_t wt2; \
3157 uint32_t wth2; \
3159 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3160 wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status); \
3161 update_fcr31(env, GETPC()); \
3162 return ((uint64_t)wth2 << 32) | wt2; \
3165 FLOAT_BINOP(add)
3166 FLOAT_BINOP(sub)
3167 FLOAT_BINOP(mul)
3168 FLOAT_BINOP(div)
3169 #undef FLOAT_BINOP
3171 /* MIPS specific binary operations */
3172 uint64_t helper_float_recip2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3174 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3175 fdt2 = float64_chs(float64_sub(fdt2, float64_one, &env->active_fpu.fp_status));
3176 update_fcr31(env, GETPC());
3177 return fdt2;
3180 uint32_t helper_float_recip2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3182 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3183 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3184 update_fcr31(env, GETPC());
3185 return fst2;
3188 uint64_t helper_float_recip2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3190 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3191 uint32_t fsth0 = fdt0 >> 32;
3192 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3193 uint32_t fsth2 = fdt2 >> 32;
3195 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3196 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3197 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3198 fsth2 = float32_chs(float32_sub(fsth2, float32_one, &env->active_fpu.fp_status));
3199 update_fcr31(env, GETPC());
3200 return ((uint64_t)fsth2 << 32) | fst2;
3203 uint64_t helper_float_rsqrt2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3205 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3206 fdt2 = float64_sub(fdt2, float64_one, &env->active_fpu.fp_status);
3207 fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
3208 update_fcr31(env, GETPC());
3209 return fdt2;
3212 uint32_t helper_float_rsqrt2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3214 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3215 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3216 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3217 update_fcr31(env, GETPC());
3218 return fst2;
3221 uint64_t helper_float_rsqrt2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3223 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3224 uint32_t fsth0 = fdt0 >> 32;
3225 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3226 uint32_t fsth2 = fdt2 >> 32;
3228 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3229 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3230 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3231 fsth2 = float32_sub(fsth2, float32_one, &env->active_fpu.fp_status);
3232 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3233 fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
3234 update_fcr31(env, GETPC());
3235 return ((uint64_t)fsth2 << 32) | fst2;
3238 uint64_t helper_float_addr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3240 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3241 uint32_t fsth0 = fdt0 >> 32;
3242 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3243 uint32_t fsth1 = fdt1 >> 32;
3244 uint32_t fst2;
3245 uint32_t fsth2;
3247 fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
3248 fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
3249 update_fcr31(env, GETPC());
3250 return ((uint64_t)fsth2 << 32) | fst2;
3253 uint64_t helper_float_mulr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3255 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3256 uint32_t fsth0 = fdt0 >> 32;
3257 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3258 uint32_t fsth1 = fdt1 >> 32;
3259 uint32_t fst2;
3260 uint32_t fsth2;
3262 fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
3263 fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
3264 update_fcr31(env, GETPC());
3265 return ((uint64_t)fsth2 << 32) | fst2;
3268 #define FLOAT_MINMAX(name, bits, minmaxfunc) \
3269 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3270 uint ## bits ## _t fs, \
3271 uint ## bits ## _t ft) \
3273 uint ## bits ## _t fdret; \
3275 fdret = float ## bits ## _ ## minmaxfunc(fs, ft, \
3276 &env->active_fpu.fp_status); \
3277 update_fcr31(env, GETPC()); \
3278 return fdret; \
3281 FLOAT_MINMAX(max_s, 32, maxnum)
3282 FLOAT_MINMAX(max_d, 64, maxnum)
3283 FLOAT_MINMAX(maxa_s, 32, maxnummag)
3284 FLOAT_MINMAX(maxa_d, 64, maxnummag)
3286 FLOAT_MINMAX(min_s, 32, minnum)
3287 FLOAT_MINMAX(min_d, 64, minnum)
3288 FLOAT_MINMAX(mina_s, 32, minnummag)
3289 FLOAT_MINMAX(mina_d, 64, minnummag)
3290 #undef FLOAT_MINMAX
3292 /* ternary operations */
3293 #define UNFUSED_FMA(prefix, a, b, c, flags) \
3295 a = prefix##_mul(a, b, &env->active_fpu.fp_status); \
3296 if ((flags) & float_muladd_negate_c) { \
3297 a = prefix##_sub(a, c, &env->active_fpu.fp_status); \
3298 } else { \
3299 a = prefix##_add(a, c, &env->active_fpu.fp_status); \
3301 if ((flags) & float_muladd_negate_result) { \
3302 a = prefix##_chs(a); \
3306 /* FMA based operations */
3307 #define FLOAT_FMA(name, type) \
3308 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
3309 uint64_t fdt0, uint64_t fdt1, \
3310 uint64_t fdt2) \
3312 UNFUSED_FMA(float64, fdt0, fdt1, fdt2, type); \
3313 update_fcr31(env, GETPC()); \
3314 return fdt0; \
3317 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
3318 uint32_t fst0, uint32_t fst1, \
3319 uint32_t fst2) \
3321 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
3322 update_fcr31(env, GETPC()); \
3323 return fst0; \
3326 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
3327 uint64_t fdt0, uint64_t fdt1, \
3328 uint64_t fdt2) \
3330 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3331 uint32_t fsth0 = fdt0 >> 32; \
3332 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3333 uint32_t fsth1 = fdt1 >> 32; \
3334 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
3335 uint32_t fsth2 = fdt2 >> 32; \
3337 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
3338 UNFUSED_FMA(float32, fsth0, fsth1, fsth2, type); \
3339 update_fcr31(env, GETPC()); \
3340 return ((uint64_t)fsth0 << 32) | fst0; \
3342 FLOAT_FMA(madd, 0)
3343 FLOAT_FMA(msub, float_muladd_negate_c)
3344 FLOAT_FMA(nmadd, float_muladd_negate_result)
3345 FLOAT_FMA(nmsub, float_muladd_negate_result | float_muladd_negate_c)
3346 #undef FLOAT_FMA
3348 #define FLOAT_FMADDSUB(name, bits, muladd_arg) \
3349 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3350 uint ## bits ## _t fs, \
3351 uint ## bits ## _t ft, \
3352 uint ## bits ## _t fd) \
3354 uint ## bits ## _t fdret; \
3356 fdret = float ## bits ## _muladd(fs, ft, fd, muladd_arg, \
3357 &env->active_fpu.fp_status); \
3358 update_fcr31(env, GETPC()); \
3359 return fdret; \
3362 FLOAT_FMADDSUB(maddf_s, 32, 0)
3363 FLOAT_FMADDSUB(maddf_d, 64, 0)
3364 FLOAT_FMADDSUB(msubf_s, 32, float_muladd_negate_product)
3365 FLOAT_FMADDSUB(msubf_d, 64, float_muladd_negate_product)
3366 #undef FLOAT_FMADDSUB
3368 /* compare operations */
3369 #define FOP_COND_D(op, cond) \
3370 void helper_cmp_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3371 uint64_t fdt1, int cc) \
3373 int c; \
3374 c = cond; \
3375 update_fcr31(env, GETPC()); \
3376 if (c) \
3377 SET_FP_COND(cc, env->active_fpu); \
3378 else \
3379 CLEAR_FP_COND(cc, env->active_fpu); \
3381 void helper_cmpabs_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3382 uint64_t fdt1, int cc) \
3384 int c; \
3385 fdt0 = float64_abs(fdt0); \
3386 fdt1 = float64_abs(fdt1); \
3387 c = cond; \
3388 update_fcr31(env, GETPC()); \
3389 if (c) \
3390 SET_FP_COND(cc, env->active_fpu); \
3391 else \
3392 CLEAR_FP_COND(cc, env->active_fpu); \
3395 /* NOTE: the comma operator will make "cond" to eval to false,
3396 * but float64_unordered_quiet() is still called. */
3397 FOP_COND_D(f, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3398 FOP_COND_D(un, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status))
3399 FOP_COND_D(eq, float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3400 FOP_COND_D(ueq, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3401 FOP_COND_D(olt, float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3402 FOP_COND_D(ult, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3403 FOP_COND_D(ole, float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3404 FOP_COND_D(ule, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3405 /* NOTE: the comma operator will make "cond" to eval to false,
3406 * but float64_unordered() is still called. */
3407 FOP_COND_D(sf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3408 FOP_COND_D(ngle,float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status))
3409 FOP_COND_D(seq, float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3410 FOP_COND_D(ngl, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3411 FOP_COND_D(lt, float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3412 FOP_COND_D(nge, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3413 FOP_COND_D(le, float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3414 FOP_COND_D(ngt, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3416 #define FOP_COND_S(op, cond) \
3417 void helper_cmp_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3418 uint32_t fst1, int cc) \
3420 int c; \
3421 c = cond; \
3422 update_fcr31(env, GETPC()); \
3423 if (c) \
3424 SET_FP_COND(cc, env->active_fpu); \
3425 else \
3426 CLEAR_FP_COND(cc, env->active_fpu); \
3428 void helper_cmpabs_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3429 uint32_t fst1, int cc) \
3431 int c; \
3432 fst0 = float32_abs(fst0); \
3433 fst1 = float32_abs(fst1); \
3434 c = cond; \
3435 update_fcr31(env, GETPC()); \
3436 if (c) \
3437 SET_FP_COND(cc, env->active_fpu); \
3438 else \
3439 CLEAR_FP_COND(cc, env->active_fpu); \
3442 /* NOTE: the comma operator will make "cond" to eval to false,
3443 * but float32_unordered_quiet() is still called. */
3444 FOP_COND_S(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3445 FOP_COND_S(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status))
3446 FOP_COND_S(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3447 FOP_COND_S(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3448 FOP_COND_S(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3449 FOP_COND_S(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3450 FOP_COND_S(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3451 FOP_COND_S(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3452 /* NOTE: the comma operator will make "cond" to eval to false,
3453 * but float32_unordered() is still called. */
3454 FOP_COND_S(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3455 FOP_COND_S(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status))
3456 FOP_COND_S(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3457 FOP_COND_S(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3458 FOP_COND_S(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3459 FOP_COND_S(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3460 FOP_COND_S(le, float32_le(fst0, fst1, &env->active_fpu.fp_status))
3461 FOP_COND_S(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
3463 #define FOP_COND_PS(op, condl, condh) \
3464 void helper_cmp_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3465 uint64_t fdt1, int cc) \
3467 uint32_t fst0, fsth0, fst1, fsth1; \
3468 int ch, cl; \
3469 fst0 = fdt0 & 0XFFFFFFFF; \
3470 fsth0 = fdt0 >> 32; \
3471 fst1 = fdt1 & 0XFFFFFFFF; \
3472 fsth1 = fdt1 >> 32; \
3473 cl = condl; \
3474 ch = condh; \
3475 update_fcr31(env, GETPC()); \
3476 if (cl) \
3477 SET_FP_COND(cc, env->active_fpu); \
3478 else \
3479 CLEAR_FP_COND(cc, env->active_fpu); \
3480 if (ch) \
3481 SET_FP_COND(cc + 1, env->active_fpu); \
3482 else \
3483 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3485 void helper_cmpabs_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3486 uint64_t fdt1, int cc) \
3488 uint32_t fst0, fsth0, fst1, fsth1; \
3489 int ch, cl; \
3490 fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
3491 fsth0 = float32_abs(fdt0 >> 32); \
3492 fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
3493 fsth1 = float32_abs(fdt1 >> 32); \
3494 cl = condl; \
3495 ch = condh; \
3496 update_fcr31(env, GETPC()); \
3497 if (cl) \
3498 SET_FP_COND(cc, env->active_fpu); \
3499 else \
3500 CLEAR_FP_COND(cc, env->active_fpu); \
3501 if (ch) \
3502 SET_FP_COND(cc + 1, env->active_fpu); \
3503 else \
3504 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3507 /* NOTE: the comma operator will make "cond" to eval to false,
3508 * but float32_unordered_quiet() is still called. */
3509 FOP_COND_PS(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0),
3510 (float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3511 FOP_COND_PS(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status),
3512 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status))
3513 FOP_COND_PS(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3514 float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3515 FOP_COND_PS(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3516 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3517 FOP_COND_PS(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3518 float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3519 FOP_COND_PS(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3520 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3521 FOP_COND_PS(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3522 float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3523 FOP_COND_PS(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3524 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3525 /* NOTE: the comma operator will make "cond" to eval to false,
3526 * but float32_unordered() is still called. */
3527 FOP_COND_PS(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0),
3528 (float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3529 FOP_COND_PS(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status),
3530 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status))
3531 FOP_COND_PS(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3532 float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3533 FOP_COND_PS(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3534 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3535 FOP_COND_PS(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3536 float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3537 FOP_COND_PS(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3538 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3539 FOP_COND_PS(le, float32_le(fst0, fst1, &env->active_fpu.fp_status),
3540 float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3541 FOP_COND_PS(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
3542 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3544 /* R6 compare operations */
3545 #define FOP_CONDN_D(op, cond) \
3546 uint64_t helper_r6_cmp_d_ ## op(CPUMIPSState * env, uint64_t fdt0, \
3547 uint64_t fdt1) \
3549 uint64_t c; \
3550 c = cond; \
3551 update_fcr31(env, GETPC()); \
3552 if (c) { \
3553 return -1; \
3554 } else { \
3555 return 0; \
3559 /* NOTE: the comma operator will make "cond" to eval to false,
3560 * but float64_unordered_quiet() is still called. */
3561 FOP_CONDN_D(af, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3562 FOP_CONDN_D(un, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)))
3563 FOP_CONDN_D(eq, (float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3564 FOP_CONDN_D(ueq, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3565 || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3566 FOP_CONDN_D(lt, (float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3567 FOP_CONDN_D(ult, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3568 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3569 FOP_CONDN_D(le, (float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3570 FOP_CONDN_D(ule, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3571 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3572 /* NOTE: the comma operator will make "cond" to eval to false,
3573 * but float64_unordered() is still called. */
3574 FOP_CONDN_D(saf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3575 FOP_CONDN_D(sun, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)))
3576 FOP_CONDN_D(seq, (float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
3577 FOP_CONDN_D(sueq, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3578 || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
3579 FOP_CONDN_D(slt, (float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3580 FOP_CONDN_D(sult, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3581 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3582 FOP_CONDN_D(sle, (float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3583 FOP_CONDN_D(sule, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3584 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3585 FOP_CONDN_D(or, (float64_le_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3586 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3587 FOP_CONDN_D(une, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3588 || float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3589 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3590 FOP_CONDN_D(ne, (float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3591 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3592 FOP_CONDN_D(sor, (float64_le(fdt1, fdt0, &env->active_fpu.fp_status)
3593 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3594 FOP_CONDN_D(sune, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3595 || float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
3596 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3597 FOP_CONDN_D(sne, (float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
3598 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3600 #define FOP_CONDN_S(op, cond) \
3601 uint32_t helper_r6_cmp_s_ ## op(CPUMIPSState * env, uint32_t fst0, \
3602 uint32_t fst1) \
3604 uint64_t c; \
3605 c = cond; \
3606 update_fcr31(env, GETPC()); \
3607 if (c) { \
3608 return -1; \
3609 } else { \
3610 return 0; \
3614 /* NOTE: the comma operator will make "cond" to eval to false,
3615 * but float32_unordered_quiet() is still called. */
3616 FOP_CONDN_S(af, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3617 FOP_CONDN_S(un, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)))
3618 FOP_CONDN_S(eq, (float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3619 FOP_CONDN_S(ueq, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3620 || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3621 FOP_CONDN_S(lt, (float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3622 FOP_CONDN_S(ult, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3623 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3624 FOP_CONDN_S(le, (float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3625 FOP_CONDN_S(ule, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3626 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3627 /* NOTE: the comma operator will make "cond" to eval to false,
3628 * but float32_unordered() is still called. */
3629 FOP_CONDN_S(saf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3630 FOP_CONDN_S(sun, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)))
3631 FOP_CONDN_S(seq, (float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
3632 FOP_CONDN_S(sueq, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3633 || float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
3634 FOP_CONDN_S(slt, (float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3635 FOP_CONDN_S(sult, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3636 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3637 FOP_CONDN_S(sle, (float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3638 FOP_CONDN_S(sule, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3639 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3640 FOP_CONDN_S(or, (float32_le_quiet(fst1, fst0, &env->active_fpu.fp_status)
3641 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3642 FOP_CONDN_S(une, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3643 || float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
3644 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3645 FOP_CONDN_S(ne, (float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
3646 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3647 FOP_CONDN_S(sor, (float32_le(fst1, fst0, &env->active_fpu.fp_status)
3648 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3649 FOP_CONDN_S(sune, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3650 || float32_lt(fst1, fst0, &env->active_fpu.fp_status)
3651 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3652 FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status)
3653 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3655 /* MSA */
3656 /* Data format min and max values */
3657 #define DF_BITS(df) (1 << ((df) + 3))
3659 /* Element-by-element access macros */
3660 #define DF_ELEMENTS(df) (MSA_WRLEN / DF_BITS(df))
3662 #if !defined(CONFIG_USER_ONLY)
3663 #define MEMOP_IDX(DF) \
3664 TCGMemOpIdx oi = make_memop_idx(MO_TE | DF | MO_UNALN, \
3665 cpu_mmu_index(env, false));
3666 #else
3667 #define MEMOP_IDX(DF)
3668 #endif
3670 #define MSA_LD_DF(DF, TYPE, LD_INSN, ...) \
3671 void helper_msa_ld_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
3672 target_ulong addr) \
3674 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
3675 wr_t wx; \
3676 int i; \
3677 MEMOP_IDX(DF) \
3678 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
3679 wx.TYPE[i] = LD_INSN(env, addr + (i << DF), ##__VA_ARGS__); \
3681 memcpy(pwd, &wx, sizeof(wr_t)); \
3684 #if !defined(CONFIG_USER_ONLY)
3685 MSA_LD_DF(DF_BYTE, b, helper_ret_ldub_mmu, oi, GETRA())
3686 MSA_LD_DF(DF_HALF, h, helper_ret_lduw_mmu, oi, GETRA())
3687 MSA_LD_DF(DF_WORD, w, helper_ret_ldul_mmu, oi, GETRA())
3688 MSA_LD_DF(DF_DOUBLE, d, helper_ret_ldq_mmu, oi, GETRA())
3689 #else
3690 MSA_LD_DF(DF_BYTE, b, cpu_ldub_data)
3691 MSA_LD_DF(DF_HALF, h, cpu_lduw_data)
3692 MSA_LD_DF(DF_WORD, w, cpu_ldl_data)
3693 MSA_LD_DF(DF_DOUBLE, d, cpu_ldq_data)
3694 #endif
3696 #define MSA_PAGESPAN(x) \
3697 ((((x) & ~TARGET_PAGE_MASK) + MSA_WRLEN/8 - 1) >= TARGET_PAGE_SIZE)
3699 static inline void ensure_writable_pages(CPUMIPSState *env,
3700 target_ulong addr,
3701 int mmu_idx,
3702 uintptr_t retaddr)
3704 #if !defined(CONFIG_USER_ONLY)
3705 target_ulong page_addr;
3706 if (unlikely(MSA_PAGESPAN(addr))) {
3707 /* first page */
3708 probe_write(env, addr, mmu_idx, retaddr);
3709 /* second page */
3710 page_addr = (addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
3711 probe_write(env, page_addr, mmu_idx, retaddr);
3713 #endif
3716 #define MSA_ST_DF(DF, TYPE, ST_INSN, ...) \
3717 void helper_msa_st_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
3718 target_ulong addr) \
3720 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
3721 int mmu_idx = cpu_mmu_index(env, false); \
3722 int i; \
3723 MEMOP_IDX(DF) \
3724 ensure_writable_pages(env, addr, mmu_idx, GETRA()); \
3725 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
3726 ST_INSN(env, addr + (i << DF), pwd->TYPE[i], ##__VA_ARGS__); \
3730 #if !defined(CONFIG_USER_ONLY)
3731 MSA_ST_DF(DF_BYTE, b, helper_ret_stb_mmu, oi, GETRA())
3732 MSA_ST_DF(DF_HALF, h, helper_ret_stw_mmu, oi, GETRA())
3733 MSA_ST_DF(DF_WORD, w, helper_ret_stl_mmu, oi, GETRA())
3734 MSA_ST_DF(DF_DOUBLE, d, helper_ret_stq_mmu, oi, GETRA())
3735 #else
3736 MSA_ST_DF(DF_BYTE, b, cpu_stb_data)
3737 MSA_ST_DF(DF_HALF, h, cpu_stw_data)
3738 MSA_ST_DF(DF_WORD, w, cpu_stl_data)
3739 MSA_ST_DF(DF_DOUBLE, d, cpu_stq_data)
3740 #endif