Merge remote-tracking branch 'remotes/cohuck/tags/s390x-20160324' into staging
[qemu.git] / target-mips / op_helper.c
blob7c5669cc96ee335828aabb40a216b5a82e726973
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 "qemu/osdep.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 /*****************************************************************************/
27 /* Exceptions processing helpers */
29 void helper_raise_exception_err(CPUMIPSState *env, uint32_t exception,
30 int error_code)
32 do_raise_exception_err(env, exception, error_code, 0);
35 void helper_raise_exception(CPUMIPSState *env, uint32_t exception)
37 do_raise_exception(env, exception, GETPC());
40 void helper_raise_exception_debug(CPUMIPSState *env)
42 do_raise_exception(env, EXCP_DEBUG, 0);
45 static void raise_exception(CPUMIPSState *env, uint32_t exception)
47 do_raise_exception(env, exception, 0);
50 #if defined(CONFIG_USER_ONLY)
51 #define HELPER_LD(name, insn, type) \
52 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
53 int mem_idx, uintptr_t retaddr) \
54 { \
55 return (type) cpu_##insn##_data_ra(env, addr, retaddr); \
57 #else
58 #define HELPER_LD(name, insn, type) \
59 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
60 int mem_idx, uintptr_t retaddr) \
61 { \
62 switch (mem_idx) \
63 { \
64 case 0: return (type) cpu_##insn##_kernel_ra(env, addr, retaddr); \
65 case 1: return (type) cpu_##insn##_super_ra(env, addr, retaddr); \
66 default: \
67 case 2: return (type) cpu_##insn##_user_ra(env, addr, retaddr); \
68 } \
70 #endif
71 HELPER_LD(lw, ldl, int32_t)
72 #if defined(TARGET_MIPS64)
73 HELPER_LD(ld, ldq, int64_t)
74 #endif
75 #undef HELPER_LD
77 #if defined(CONFIG_USER_ONLY)
78 #define HELPER_ST(name, insn, type) \
79 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
80 type val, int mem_idx, uintptr_t retaddr) \
81 { \
82 cpu_##insn##_data_ra(env, addr, val, retaddr); \
84 #else
85 #define HELPER_ST(name, insn, type) \
86 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
87 type val, int mem_idx, uintptr_t retaddr) \
88 { \
89 switch (mem_idx) \
90 { \
91 case 0: cpu_##insn##_kernel_ra(env, addr, val, retaddr); break; \
92 case 1: cpu_##insn##_super_ra(env, addr, val, retaddr); break; \
93 default: \
94 case 2: cpu_##insn##_user_ra(env, addr, val, retaddr); break; \
95 } \
97 #endif
98 HELPER_ST(sb, stb, uint8_t)
99 HELPER_ST(sw, stl, uint32_t)
100 #if defined(TARGET_MIPS64)
101 HELPER_ST(sd, stq, uint64_t)
102 #endif
103 #undef HELPER_ST
105 target_ulong helper_clo (target_ulong arg1)
107 return clo32(arg1);
110 target_ulong helper_clz (target_ulong arg1)
112 return clz32(arg1);
115 #if defined(TARGET_MIPS64)
116 target_ulong helper_dclo (target_ulong arg1)
118 return clo64(arg1);
121 target_ulong helper_dclz (target_ulong arg1)
123 return clz64(arg1);
125 #endif /* TARGET_MIPS64 */
127 /* 64 bits arithmetic for 32 bits hosts */
128 static inline uint64_t get_HILO(CPUMIPSState *env)
130 return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
133 static inline target_ulong set_HIT0_LO(CPUMIPSState *env, uint64_t HILO)
135 target_ulong tmp;
136 env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
137 tmp = env->active_tc.HI[0] = (int32_t)(HILO >> 32);
138 return tmp;
141 static inline target_ulong set_HI_LOT0(CPUMIPSState *env, uint64_t HILO)
143 target_ulong tmp = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
144 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
145 return tmp;
148 /* Multiplication variants of the vr54xx. */
149 target_ulong helper_muls(CPUMIPSState *env, target_ulong arg1,
150 target_ulong arg2)
152 return set_HI_LOT0(env, 0 - ((int64_t)(int32_t)arg1 *
153 (int64_t)(int32_t)arg2));
156 target_ulong helper_mulsu(CPUMIPSState *env, target_ulong arg1,
157 target_ulong arg2)
159 return set_HI_LOT0(env, 0 - (uint64_t)(uint32_t)arg1 *
160 (uint64_t)(uint32_t)arg2);
163 target_ulong helper_macc(CPUMIPSState *env, target_ulong arg1,
164 target_ulong arg2)
166 return set_HI_LOT0(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
167 (int64_t)(int32_t)arg2);
170 target_ulong helper_macchi(CPUMIPSState *env, target_ulong arg1,
171 target_ulong arg2)
173 return set_HIT0_LO(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
174 (int64_t)(int32_t)arg2);
177 target_ulong helper_maccu(CPUMIPSState *env, target_ulong arg1,
178 target_ulong arg2)
180 return set_HI_LOT0(env, (uint64_t)get_HILO(env) +
181 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
184 target_ulong helper_macchiu(CPUMIPSState *env, target_ulong arg1,
185 target_ulong arg2)
187 return set_HIT0_LO(env, (uint64_t)get_HILO(env) +
188 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
191 target_ulong helper_msac(CPUMIPSState *env, target_ulong arg1,
192 target_ulong arg2)
194 return set_HI_LOT0(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
195 (int64_t)(int32_t)arg2);
198 target_ulong helper_msachi(CPUMIPSState *env, target_ulong arg1,
199 target_ulong arg2)
201 return set_HIT0_LO(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
202 (int64_t)(int32_t)arg2);
205 target_ulong helper_msacu(CPUMIPSState *env, target_ulong arg1,
206 target_ulong arg2)
208 return set_HI_LOT0(env, (uint64_t)get_HILO(env) -
209 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
212 target_ulong helper_msachiu(CPUMIPSState *env, target_ulong arg1,
213 target_ulong arg2)
215 return set_HIT0_LO(env, (uint64_t)get_HILO(env) -
216 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
219 target_ulong helper_mulhi(CPUMIPSState *env, target_ulong arg1,
220 target_ulong arg2)
222 return set_HIT0_LO(env, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
225 target_ulong helper_mulhiu(CPUMIPSState *env, target_ulong arg1,
226 target_ulong arg2)
228 return set_HIT0_LO(env, (uint64_t)(uint32_t)arg1 *
229 (uint64_t)(uint32_t)arg2);
232 target_ulong helper_mulshi(CPUMIPSState *env, target_ulong arg1,
233 target_ulong arg2)
235 return set_HIT0_LO(env, 0 - (int64_t)(int32_t)arg1 *
236 (int64_t)(int32_t)arg2);
239 target_ulong helper_mulshiu(CPUMIPSState *env, target_ulong arg1,
240 target_ulong arg2)
242 return set_HIT0_LO(env, 0 - (uint64_t)(uint32_t)arg1 *
243 (uint64_t)(uint32_t)arg2);
246 static inline target_ulong bitswap(target_ulong v)
248 v = ((v >> 1) & (target_ulong)0x5555555555555555ULL) |
249 ((v & (target_ulong)0x5555555555555555ULL) << 1);
250 v = ((v >> 2) & (target_ulong)0x3333333333333333ULL) |
251 ((v & (target_ulong)0x3333333333333333ULL) << 2);
252 v = ((v >> 4) & (target_ulong)0x0F0F0F0F0F0F0F0FULL) |
253 ((v & (target_ulong)0x0F0F0F0F0F0F0F0FULL) << 4);
254 return v;
257 #ifdef TARGET_MIPS64
258 target_ulong helper_dbitswap(target_ulong rt)
260 return bitswap(rt);
262 #endif
264 target_ulong helper_bitswap(target_ulong rt)
266 return (int32_t)bitswap(rt);
269 #ifndef CONFIG_USER_ONLY
271 static inline hwaddr do_translate_address(CPUMIPSState *env,
272 target_ulong address,
273 int rw, uintptr_t retaddr)
275 hwaddr lladdr;
276 CPUState *cs = CPU(mips_env_get_cpu(env));
278 lladdr = cpu_mips_translate_address(env, address, rw);
280 if (lladdr == -1LL) {
281 cpu_loop_exit_restore(cs, retaddr);
282 } else {
283 return lladdr;
287 #define HELPER_LD_ATOMIC(name, insn, almask) \
288 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg, int mem_idx) \
290 if (arg & almask) { \
291 env->CP0_BadVAddr = arg; \
292 do_raise_exception(env, EXCP_AdEL, GETPC()); \
294 env->lladdr = do_translate_address(env, arg, 0, GETPC()); \
295 env->llval = do_##insn(env, arg, mem_idx, GETPC()); \
296 return env->llval; \
298 HELPER_LD_ATOMIC(ll, lw, 0x3)
299 #ifdef TARGET_MIPS64
300 HELPER_LD_ATOMIC(lld, ld, 0x7)
301 #endif
302 #undef HELPER_LD_ATOMIC
304 #define HELPER_ST_ATOMIC(name, ld_insn, st_insn, almask) \
305 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg1, \
306 target_ulong arg2, int mem_idx) \
308 target_long tmp; \
310 if (arg2 & almask) { \
311 env->CP0_BadVAddr = arg2; \
312 do_raise_exception(env, EXCP_AdES, GETPC()); \
314 if (do_translate_address(env, arg2, 1, GETPC()) == env->lladdr) { \
315 tmp = do_##ld_insn(env, arg2, mem_idx, GETPC()); \
316 if (tmp == env->llval) { \
317 do_##st_insn(env, arg2, arg1, mem_idx, GETPC()); \
318 return 1; \
321 return 0; \
323 HELPER_ST_ATOMIC(sc, lw, sw, 0x3)
324 #ifdef TARGET_MIPS64
325 HELPER_ST_ATOMIC(scd, ld, sd, 0x7)
326 #endif
327 #undef HELPER_ST_ATOMIC
328 #endif
330 #ifdef TARGET_WORDS_BIGENDIAN
331 #define GET_LMASK(v) ((v) & 3)
332 #define GET_OFFSET(addr, offset) (addr + (offset))
333 #else
334 #define GET_LMASK(v) (((v) & 3) ^ 3)
335 #define GET_OFFSET(addr, offset) (addr - (offset))
336 #endif
338 void helper_swl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
339 int mem_idx)
341 do_sb(env, arg2, (uint8_t)(arg1 >> 24), mem_idx, GETPC());
343 if (GET_LMASK(arg2) <= 2) {
344 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16), mem_idx,
345 GETPC());
348 if (GET_LMASK(arg2) <= 1) {
349 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8), mem_idx,
350 GETPC());
353 if (GET_LMASK(arg2) == 0) {
354 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)arg1, mem_idx,
355 GETPC());
359 void helper_swr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
360 int mem_idx)
362 do_sb(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
364 if (GET_LMASK(arg2) >= 1) {
365 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx,
366 GETPC());
369 if (GET_LMASK(arg2) >= 2) {
370 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx,
371 GETPC());
374 if (GET_LMASK(arg2) == 3) {
375 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx,
376 GETPC());
380 #if defined(TARGET_MIPS64)
381 /* "half" load and stores. We must do the memory access inline,
382 or fault handling won't work. */
384 #ifdef TARGET_WORDS_BIGENDIAN
385 #define GET_LMASK64(v) ((v) & 7)
386 #else
387 #define GET_LMASK64(v) (((v) & 7) ^ 7)
388 #endif
390 void helper_sdl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
391 int mem_idx)
393 do_sb(env, arg2, (uint8_t)(arg1 >> 56), mem_idx, GETPC());
395 if (GET_LMASK64(arg2) <= 6) {
396 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48), mem_idx,
397 GETPC());
400 if (GET_LMASK64(arg2) <= 5) {
401 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40), mem_idx,
402 GETPC());
405 if (GET_LMASK64(arg2) <= 4) {
406 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32), mem_idx,
407 GETPC());
410 if (GET_LMASK64(arg2) <= 3) {
411 do_sb(env, GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24), mem_idx,
412 GETPC());
415 if (GET_LMASK64(arg2) <= 2) {
416 do_sb(env, GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16), mem_idx,
417 GETPC());
420 if (GET_LMASK64(arg2) <= 1) {
421 do_sb(env, GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8), mem_idx,
422 GETPC());
425 if (GET_LMASK64(arg2) <= 0) {
426 do_sb(env, GET_OFFSET(arg2, 7), (uint8_t)arg1, mem_idx,
427 GETPC());
431 void helper_sdr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
432 int mem_idx)
434 do_sb(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
436 if (GET_LMASK64(arg2) >= 1) {
437 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx,
438 GETPC());
441 if (GET_LMASK64(arg2) >= 2) {
442 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx,
443 GETPC());
446 if (GET_LMASK64(arg2) >= 3) {
447 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx,
448 GETPC());
451 if (GET_LMASK64(arg2) >= 4) {
452 do_sb(env, GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32), mem_idx,
453 GETPC());
456 if (GET_LMASK64(arg2) >= 5) {
457 do_sb(env, GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40), mem_idx,
458 GETPC());
461 if (GET_LMASK64(arg2) >= 6) {
462 do_sb(env, GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48), mem_idx,
463 GETPC());
466 if (GET_LMASK64(arg2) == 7) {
467 do_sb(env, GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56), mem_idx,
468 GETPC());
471 #endif /* TARGET_MIPS64 */
473 static const int multiple_regs[] = { 16, 17, 18, 19, 20, 21, 22, 23, 30 };
475 void helper_lwm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
476 uint32_t mem_idx)
478 target_ulong base_reglist = reglist & 0xf;
479 target_ulong do_r31 = reglist & 0x10;
481 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
482 target_ulong i;
484 for (i = 0; i < base_reglist; i++) {
485 env->active_tc.gpr[multiple_regs[i]] =
486 (target_long)do_lw(env, addr, mem_idx, GETPC());
487 addr += 4;
491 if (do_r31) {
492 env->active_tc.gpr[31] = (target_long)do_lw(env, addr, mem_idx,
493 GETPC());
497 void helper_swm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
498 uint32_t mem_idx)
500 target_ulong base_reglist = reglist & 0xf;
501 target_ulong do_r31 = reglist & 0x10;
503 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
504 target_ulong i;
506 for (i = 0; i < base_reglist; i++) {
507 do_sw(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx,
508 GETPC());
509 addr += 4;
513 if (do_r31) {
514 do_sw(env, addr, env->active_tc.gpr[31], mem_idx, GETPC());
518 #if defined(TARGET_MIPS64)
519 void helper_ldm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
520 uint32_t mem_idx)
522 target_ulong base_reglist = reglist & 0xf;
523 target_ulong do_r31 = reglist & 0x10;
525 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
526 target_ulong i;
528 for (i = 0; i < base_reglist; i++) {
529 env->active_tc.gpr[multiple_regs[i]] = do_ld(env, addr, mem_idx,
530 GETPC());
531 addr += 8;
535 if (do_r31) {
536 env->active_tc.gpr[31] = do_ld(env, addr, mem_idx, GETPC());
540 void helper_sdm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
541 uint32_t mem_idx)
543 target_ulong base_reglist = reglist & 0xf;
544 target_ulong do_r31 = reglist & 0x10;
546 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
547 target_ulong i;
549 for (i = 0; i < base_reglist; i++) {
550 do_sd(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx,
551 GETPC());
552 addr += 8;
556 if (do_r31) {
557 do_sd(env, addr, env->active_tc.gpr[31], mem_idx, GETPC());
560 #endif
562 #ifndef CONFIG_USER_ONLY
563 /* SMP helpers. */
564 static bool mips_vpe_is_wfi(MIPSCPU *c)
566 CPUState *cpu = CPU(c);
567 CPUMIPSState *env = &c->env;
569 /* If the VPE is halted but otherwise active, it means it's waiting for
570 an interrupt. */
571 return cpu->halted && mips_vpe_active(env);
574 static bool mips_vp_is_wfi(MIPSCPU *c)
576 CPUState *cpu = CPU(c);
577 CPUMIPSState *env = &c->env;
579 return cpu->halted && mips_vp_active(env);
582 static inline void mips_vpe_wake(MIPSCPU *c)
584 /* Dont set ->halted = 0 directly, let it be done via cpu_has_work
585 because there might be other conditions that state that c should
586 be sleeping. */
587 cpu_interrupt(CPU(c), CPU_INTERRUPT_WAKE);
590 static inline void mips_vpe_sleep(MIPSCPU *cpu)
592 CPUState *cs = CPU(cpu);
594 /* The VPE was shut off, really go to bed.
595 Reset any old _WAKE requests. */
596 cs->halted = 1;
597 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
600 static inline void mips_tc_wake(MIPSCPU *cpu, int tc)
602 CPUMIPSState *c = &cpu->env;
604 /* FIXME: TC reschedule. */
605 if (mips_vpe_active(c) && !mips_vpe_is_wfi(cpu)) {
606 mips_vpe_wake(cpu);
610 static inline void mips_tc_sleep(MIPSCPU *cpu, int tc)
612 CPUMIPSState *c = &cpu->env;
614 /* FIXME: TC reschedule. */
615 if (!mips_vpe_active(c)) {
616 mips_vpe_sleep(cpu);
621 * mips_cpu_map_tc:
622 * @env: CPU from which mapping is performed.
623 * @tc: Should point to an int with the value of the global TC index.
625 * This function will transform @tc into a local index within the
626 * returned #CPUMIPSState.
628 /* FIXME: This code assumes that all VPEs have the same number of TCs,
629 which depends on runtime setup. Can probably be fixed by
630 walking the list of CPUMIPSStates. */
631 static CPUMIPSState *mips_cpu_map_tc(CPUMIPSState *env, int *tc)
633 MIPSCPU *cpu;
634 CPUState *cs;
635 CPUState *other_cs;
636 int vpe_idx;
637 int tc_idx = *tc;
639 if (!(env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))) {
640 /* Not allowed to address other CPUs. */
641 *tc = env->current_tc;
642 return env;
645 cs = CPU(mips_env_get_cpu(env));
646 vpe_idx = tc_idx / cs->nr_threads;
647 *tc = tc_idx % cs->nr_threads;
648 other_cs = qemu_get_cpu(vpe_idx);
649 if (other_cs == NULL) {
650 return env;
652 cpu = MIPS_CPU(other_cs);
653 return &cpu->env;
656 /* The per VPE CP0_Status register shares some fields with the per TC
657 CP0_TCStatus registers. These fields are wired to the same registers,
658 so changes to either of them should be reflected on both registers.
660 Also, EntryHi shares the bottom 8 bit ASID with TCStauts.
662 These helper call synchronizes the regs for a given cpu. */
664 /* Called for updates to CP0_Status. Defined in "cpu.h" for gdbstub.c. */
665 /* static inline void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu,
666 int tc); */
668 /* Called for updates to CP0_TCStatus. */
669 static void sync_c0_tcstatus(CPUMIPSState *cpu, int tc,
670 target_ulong v)
672 uint32_t status;
673 uint32_t tcu, tmx, tasid, tksu;
674 uint32_t mask = ((1U << CP0St_CU3)
675 | (1 << CP0St_CU2)
676 | (1 << CP0St_CU1)
677 | (1 << CP0St_CU0)
678 | (1 << CP0St_MX)
679 | (3 << CP0St_KSU));
681 tcu = (v >> CP0TCSt_TCU0) & 0xf;
682 tmx = (v >> CP0TCSt_TMX) & 0x1;
683 tasid = v & 0xff;
684 tksu = (v >> CP0TCSt_TKSU) & 0x3;
686 status = tcu << CP0St_CU0;
687 status |= tmx << CP0St_MX;
688 status |= tksu << CP0St_KSU;
690 cpu->CP0_Status &= ~mask;
691 cpu->CP0_Status |= status;
693 /* Sync the TASID with EntryHi. */
694 cpu->CP0_EntryHi &= ~0xff;
695 cpu->CP0_EntryHi |= tasid;
697 compute_hflags(cpu);
700 /* Called for updates to CP0_EntryHi. */
701 static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
703 int32_t *tcst;
704 uint32_t asid, v = cpu->CP0_EntryHi;
706 asid = v & 0xff;
708 if (tc == cpu->current_tc) {
709 tcst = &cpu->active_tc.CP0_TCStatus;
710 } else {
711 tcst = &cpu->tcs[tc].CP0_TCStatus;
714 *tcst &= ~0xff;
715 *tcst |= asid;
718 /* CP0 helpers */
719 target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
721 return env->mvp->CP0_MVPControl;
724 target_ulong helper_mfc0_mvpconf0(CPUMIPSState *env)
726 return env->mvp->CP0_MVPConf0;
729 target_ulong helper_mfc0_mvpconf1(CPUMIPSState *env)
731 return env->mvp->CP0_MVPConf1;
734 target_ulong helper_mfc0_random(CPUMIPSState *env)
736 return (int32_t)cpu_mips_get_random(env);
739 target_ulong helper_mfc0_tcstatus(CPUMIPSState *env)
741 return env->active_tc.CP0_TCStatus;
744 target_ulong helper_mftc0_tcstatus(CPUMIPSState *env)
746 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
747 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
749 if (other_tc == other->current_tc)
750 return other->active_tc.CP0_TCStatus;
751 else
752 return other->tcs[other_tc].CP0_TCStatus;
755 target_ulong helper_mfc0_tcbind(CPUMIPSState *env)
757 return env->active_tc.CP0_TCBind;
760 target_ulong helper_mftc0_tcbind(CPUMIPSState *env)
762 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
763 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
765 if (other_tc == other->current_tc)
766 return other->active_tc.CP0_TCBind;
767 else
768 return other->tcs[other_tc].CP0_TCBind;
771 target_ulong helper_mfc0_tcrestart(CPUMIPSState *env)
773 return env->active_tc.PC;
776 target_ulong helper_mftc0_tcrestart(CPUMIPSState *env)
778 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
779 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
781 if (other_tc == other->current_tc)
782 return other->active_tc.PC;
783 else
784 return other->tcs[other_tc].PC;
787 target_ulong helper_mfc0_tchalt(CPUMIPSState *env)
789 return env->active_tc.CP0_TCHalt;
792 target_ulong helper_mftc0_tchalt(CPUMIPSState *env)
794 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
795 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
797 if (other_tc == other->current_tc)
798 return other->active_tc.CP0_TCHalt;
799 else
800 return other->tcs[other_tc].CP0_TCHalt;
803 target_ulong helper_mfc0_tccontext(CPUMIPSState *env)
805 return env->active_tc.CP0_TCContext;
808 target_ulong helper_mftc0_tccontext(CPUMIPSState *env)
810 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
811 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
813 if (other_tc == other->current_tc)
814 return other->active_tc.CP0_TCContext;
815 else
816 return other->tcs[other_tc].CP0_TCContext;
819 target_ulong helper_mfc0_tcschedule(CPUMIPSState *env)
821 return env->active_tc.CP0_TCSchedule;
824 target_ulong helper_mftc0_tcschedule(CPUMIPSState *env)
826 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
827 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
829 if (other_tc == other->current_tc)
830 return other->active_tc.CP0_TCSchedule;
831 else
832 return other->tcs[other_tc].CP0_TCSchedule;
835 target_ulong helper_mfc0_tcschefback(CPUMIPSState *env)
837 return env->active_tc.CP0_TCScheFBack;
840 target_ulong helper_mftc0_tcschefback(CPUMIPSState *env)
842 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
843 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
845 if (other_tc == other->current_tc)
846 return other->active_tc.CP0_TCScheFBack;
847 else
848 return other->tcs[other_tc].CP0_TCScheFBack;
851 target_ulong helper_mfc0_count(CPUMIPSState *env)
853 return (int32_t)cpu_mips_get_count(env);
856 target_ulong helper_mftc0_entryhi(CPUMIPSState *env)
858 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
859 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
861 return other->CP0_EntryHi;
864 target_ulong helper_mftc0_cause(CPUMIPSState *env)
866 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
867 int32_t tccause;
868 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
870 if (other_tc == other->current_tc) {
871 tccause = other->CP0_Cause;
872 } else {
873 tccause = other->CP0_Cause;
876 return tccause;
879 target_ulong helper_mftc0_status(CPUMIPSState *env)
881 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
882 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
884 return other->CP0_Status;
887 target_ulong helper_mfc0_lladdr(CPUMIPSState *env)
889 return (int32_t)(env->lladdr >> env->CP0_LLAddr_shift);
892 target_ulong helper_mfc0_watchlo(CPUMIPSState *env, uint32_t sel)
894 return (int32_t)env->CP0_WatchLo[sel];
897 target_ulong helper_mfc0_watchhi(CPUMIPSState *env, uint32_t sel)
899 return env->CP0_WatchHi[sel];
902 target_ulong helper_mfc0_debug(CPUMIPSState *env)
904 target_ulong t0 = env->CP0_Debug;
905 if (env->hflags & MIPS_HFLAG_DM)
906 t0 |= 1 << CP0DB_DM;
908 return t0;
911 target_ulong helper_mftc0_debug(CPUMIPSState *env)
913 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
914 int32_t tcstatus;
915 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
917 if (other_tc == other->current_tc)
918 tcstatus = other->active_tc.CP0_Debug_tcstatus;
919 else
920 tcstatus = other->tcs[other_tc].CP0_Debug_tcstatus;
922 /* XXX: Might be wrong, check with EJTAG spec. */
923 return (other->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
924 (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
927 #if defined(TARGET_MIPS64)
928 target_ulong helper_dmfc0_tcrestart(CPUMIPSState *env)
930 return env->active_tc.PC;
933 target_ulong helper_dmfc0_tchalt(CPUMIPSState *env)
935 return env->active_tc.CP0_TCHalt;
938 target_ulong helper_dmfc0_tccontext(CPUMIPSState *env)
940 return env->active_tc.CP0_TCContext;
943 target_ulong helper_dmfc0_tcschedule(CPUMIPSState *env)
945 return env->active_tc.CP0_TCSchedule;
948 target_ulong helper_dmfc0_tcschefback(CPUMIPSState *env)
950 return env->active_tc.CP0_TCScheFBack;
953 target_ulong helper_dmfc0_lladdr(CPUMIPSState *env)
955 return env->lladdr >> env->CP0_LLAddr_shift;
958 target_ulong helper_dmfc0_watchlo(CPUMIPSState *env, uint32_t sel)
960 return env->CP0_WatchLo[sel];
962 #endif /* TARGET_MIPS64 */
964 void helper_mtc0_index(CPUMIPSState *env, target_ulong arg1)
966 uint32_t index_p = env->CP0_Index & 0x80000000;
967 uint32_t tlb_index = arg1 & 0x7fffffff;
968 if (tlb_index < env->tlb->nb_tlb) {
969 if (env->insn_flags & ISA_MIPS32R6) {
970 index_p |= arg1 & 0x80000000;
972 env->CP0_Index = index_p | tlb_index;
976 void helper_mtc0_mvpcontrol(CPUMIPSState *env, target_ulong arg1)
978 uint32_t mask = 0;
979 uint32_t newval;
981 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
982 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
983 (1 << CP0MVPCo_EVP);
984 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
985 mask |= (1 << CP0MVPCo_STLB);
986 newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
988 // TODO: Enable/disable shared TLB, enable/disable VPEs.
990 env->mvp->CP0_MVPControl = newval;
993 void helper_mtc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
995 uint32_t mask;
996 uint32_t newval;
998 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
999 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1000 newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
1002 /* Yield scheduler intercept not implemented. */
1003 /* Gating storage scheduler intercept not implemented. */
1005 // TODO: Enable/disable TCs.
1007 env->CP0_VPEControl = newval;
1010 void helper_mttc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
1012 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1013 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1014 uint32_t mask;
1015 uint32_t newval;
1017 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
1018 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1019 newval = (other->CP0_VPEControl & ~mask) | (arg1 & mask);
1021 /* TODO: Enable/disable TCs. */
1023 other->CP0_VPEControl = newval;
1026 target_ulong helper_mftc0_vpecontrol(CPUMIPSState *env)
1028 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1029 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1030 /* FIXME: Mask away return zero on read bits. */
1031 return other->CP0_VPEControl;
1034 target_ulong helper_mftc0_vpeconf0(CPUMIPSState *env)
1036 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1037 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1039 return other->CP0_VPEConf0;
1042 void helper_mtc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1044 uint32_t mask = 0;
1045 uint32_t newval;
1047 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
1048 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
1049 mask |= (0xff << CP0VPEC0_XTC);
1050 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1052 newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1054 // TODO: TC exclusive handling due to ERL/EXL.
1056 env->CP0_VPEConf0 = newval;
1059 void helper_mttc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1061 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1062 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1063 uint32_t mask = 0;
1064 uint32_t newval;
1066 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1067 newval = (other->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1069 /* TODO: TC exclusive handling due to ERL/EXL. */
1070 other->CP0_VPEConf0 = newval;
1073 void helper_mtc0_vpeconf1(CPUMIPSState *env, target_ulong arg1)
1075 uint32_t mask = 0;
1076 uint32_t newval;
1078 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1079 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
1080 (0xff << CP0VPEC1_NCP1);
1081 newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
1083 /* UDI not implemented. */
1084 /* CP2 not implemented. */
1086 // TODO: Handle FPU (CP1) binding.
1088 env->CP0_VPEConf1 = newval;
1091 void helper_mtc0_yqmask(CPUMIPSState *env, target_ulong arg1)
1093 /* Yield qualifier inputs not implemented. */
1094 env->CP0_YQMask = 0x00000000;
1097 void helper_mtc0_vpeopt(CPUMIPSState *env, target_ulong arg1)
1099 env->CP0_VPEOpt = arg1 & 0x0000ffff;
1102 #define MTC0_ENTRYLO_MASK(env) ((env->PAMask >> 6) & 0x3FFFFFFF)
1104 void helper_mtc0_entrylo0(CPUMIPSState *env, target_ulong arg1)
1106 /* 1k pages not implemented */
1107 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1108 env->CP0_EntryLo0 = (arg1 & MTC0_ENTRYLO_MASK(env))
1109 | (rxi << (CP0EnLo_XI - 30));
1112 #if defined(TARGET_MIPS64)
1113 #define DMTC0_ENTRYLO_MASK(env) (env->PAMask >> 6)
1115 void helper_dmtc0_entrylo0(CPUMIPSState *env, uint64_t arg1)
1117 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1118 env->CP0_EntryLo0 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1120 #endif
1122 void helper_mtc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1124 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
1125 uint32_t newval;
1127 newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
1129 env->active_tc.CP0_TCStatus = newval;
1130 sync_c0_tcstatus(env, env->current_tc, newval);
1133 void helper_mttc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1135 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1136 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1138 if (other_tc == other->current_tc)
1139 other->active_tc.CP0_TCStatus = arg1;
1140 else
1141 other->tcs[other_tc].CP0_TCStatus = arg1;
1142 sync_c0_tcstatus(other, other_tc, arg1);
1145 void helper_mtc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1147 uint32_t mask = (1 << CP0TCBd_TBE);
1148 uint32_t newval;
1150 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1151 mask |= (1 << CP0TCBd_CurVPE);
1152 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1153 env->active_tc.CP0_TCBind = newval;
1156 void helper_mttc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1158 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1159 uint32_t mask = (1 << CP0TCBd_TBE);
1160 uint32_t newval;
1161 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1163 if (other->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1164 mask |= (1 << CP0TCBd_CurVPE);
1165 if (other_tc == other->current_tc) {
1166 newval = (other->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1167 other->active_tc.CP0_TCBind = newval;
1168 } else {
1169 newval = (other->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
1170 other->tcs[other_tc].CP0_TCBind = newval;
1174 void helper_mtc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1176 env->active_tc.PC = arg1;
1177 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1178 env->lladdr = 0ULL;
1179 /* MIPS16 not implemented. */
1182 void helper_mttc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1184 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1185 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1187 if (other_tc == other->current_tc) {
1188 other->active_tc.PC = arg1;
1189 other->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1190 other->lladdr = 0ULL;
1191 /* MIPS16 not implemented. */
1192 } else {
1193 other->tcs[other_tc].PC = arg1;
1194 other->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1195 other->lladdr = 0ULL;
1196 /* MIPS16 not implemented. */
1200 void helper_mtc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1202 MIPSCPU *cpu = mips_env_get_cpu(env);
1204 env->active_tc.CP0_TCHalt = arg1 & 0x1;
1206 // TODO: Halt TC / Restart (if allocated+active) TC.
1207 if (env->active_tc.CP0_TCHalt & 1) {
1208 mips_tc_sleep(cpu, env->current_tc);
1209 } else {
1210 mips_tc_wake(cpu, env->current_tc);
1214 void helper_mttc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1216 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1217 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1218 MIPSCPU *other_cpu = mips_env_get_cpu(other);
1220 // TODO: Halt TC / Restart (if allocated+active) TC.
1222 if (other_tc == other->current_tc)
1223 other->active_tc.CP0_TCHalt = arg1;
1224 else
1225 other->tcs[other_tc].CP0_TCHalt = arg1;
1227 if (arg1 & 1) {
1228 mips_tc_sleep(other_cpu, other_tc);
1229 } else {
1230 mips_tc_wake(other_cpu, other_tc);
1234 void helper_mtc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1236 env->active_tc.CP0_TCContext = arg1;
1239 void helper_mttc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1241 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1242 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1244 if (other_tc == other->current_tc)
1245 other->active_tc.CP0_TCContext = arg1;
1246 else
1247 other->tcs[other_tc].CP0_TCContext = arg1;
1250 void helper_mtc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1252 env->active_tc.CP0_TCSchedule = arg1;
1255 void helper_mttc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1257 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1258 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1260 if (other_tc == other->current_tc)
1261 other->active_tc.CP0_TCSchedule = arg1;
1262 else
1263 other->tcs[other_tc].CP0_TCSchedule = arg1;
1266 void helper_mtc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1268 env->active_tc.CP0_TCScheFBack = arg1;
1271 void helper_mttc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1273 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1274 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1276 if (other_tc == other->current_tc)
1277 other->active_tc.CP0_TCScheFBack = arg1;
1278 else
1279 other->tcs[other_tc].CP0_TCScheFBack = arg1;
1282 void helper_mtc0_entrylo1(CPUMIPSState *env, target_ulong arg1)
1284 /* 1k pages not implemented */
1285 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1286 env->CP0_EntryLo1 = (arg1 & MTC0_ENTRYLO_MASK(env))
1287 | (rxi << (CP0EnLo_XI - 30));
1290 #if defined(TARGET_MIPS64)
1291 void helper_dmtc0_entrylo1(CPUMIPSState *env, uint64_t arg1)
1293 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1294 env->CP0_EntryLo1 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1296 #endif
1298 void helper_mtc0_context(CPUMIPSState *env, target_ulong arg1)
1300 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1303 void helper_mtc0_pagemask(CPUMIPSState *env, target_ulong arg1)
1305 uint64_t mask = arg1 >> (TARGET_PAGE_BITS + 1);
1306 if (!(env->insn_flags & ISA_MIPS32R6) || (arg1 == ~0) ||
1307 (mask == 0x0000 || mask == 0x0003 || mask == 0x000F ||
1308 mask == 0x003F || mask == 0x00FF || mask == 0x03FF ||
1309 mask == 0x0FFF || mask == 0x3FFF || mask == 0xFFFF)) {
1310 env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1314 void helper_mtc0_pagegrain(CPUMIPSState *env, target_ulong arg1)
1316 /* SmartMIPS not implemented */
1317 /* 1k pages not implemented */
1318 env->CP0_PageGrain = (arg1 & env->CP0_PageGrain_rw_bitmask) |
1319 (env->CP0_PageGrain & ~env->CP0_PageGrain_rw_bitmask);
1320 compute_hflags(env);
1321 restore_pamask(env);
1324 void helper_mtc0_wired(CPUMIPSState *env, target_ulong arg1)
1326 if (env->insn_flags & ISA_MIPS32R6) {
1327 if (arg1 < env->tlb->nb_tlb) {
1328 env->CP0_Wired = arg1;
1330 } else {
1331 env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1335 void helper_mtc0_srsconf0(CPUMIPSState *env, target_ulong arg1)
1337 env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1340 void helper_mtc0_srsconf1(CPUMIPSState *env, target_ulong arg1)
1342 env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1345 void helper_mtc0_srsconf2(CPUMIPSState *env, target_ulong arg1)
1347 env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1350 void helper_mtc0_srsconf3(CPUMIPSState *env, target_ulong arg1)
1352 env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1355 void helper_mtc0_srsconf4(CPUMIPSState *env, target_ulong arg1)
1357 env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1360 void helper_mtc0_hwrena(CPUMIPSState *env, target_ulong arg1)
1362 uint32_t mask = 0x0000000F;
1364 if ((env->CP0_Config1 & (1 << CP0C1_PC)) &&
1365 (env->insn_flags & ISA_MIPS32R6)) {
1366 mask |= (1 << 4);
1368 if (env->insn_flags & ISA_MIPS32R6) {
1369 mask |= (1 << 5);
1371 if (env->CP0_Config3 & (1 << CP0C3_ULRI)) {
1372 mask |= (1 << 29);
1374 if (arg1 & (1 << 29)) {
1375 env->hflags |= MIPS_HFLAG_HWRENA_ULR;
1376 } else {
1377 env->hflags &= ~MIPS_HFLAG_HWRENA_ULR;
1381 env->CP0_HWREna = arg1 & mask;
1384 void helper_mtc0_count(CPUMIPSState *env, target_ulong arg1)
1386 cpu_mips_store_count(env, arg1);
1389 void helper_mtc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1391 target_ulong old, val, mask;
1392 mask = (TARGET_PAGE_MASK << 1) | 0xFF;
1393 if (((env->CP0_Config4 >> CP0C4_IE) & 0x3) >= 2) {
1394 mask |= 1 << CP0EnHi_EHINV;
1397 /* 1k pages not implemented */
1398 #if defined(TARGET_MIPS64)
1399 if (env->insn_flags & ISA_MIPS32R6) {
1400 int entryhi_r = extract64(arg1, 62, 2);
1401 int config0_at = extract32(env->CP0_Config0, 13, 2);
1402 bool no_supervisor = (env->CP0_Status_rw_bitmask & 0x8) == 0;
1403 if ((entryhi_r == 2) ||
1404 (entryhi_r == 1 && (no_supervisor || config0_at == 1))) {
1405 /* skip EntryHi.R field if new value is reserved */
1406 mask &= ~(0x3ull << 62);
1409 mask &= env->SEGMask;
1410 #endif
1411 old = env->CP0_EntryHi;
1412 val = (arg1 & mask) | (old & ~mask);
1413 env->CP0_EntryHi = val;
1414 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1415 sync_c0_entryhi(env, env->current_tc);
1417 /* If the ASID changes, flush qemu's TLB. */
1418 if ((old & 0xFF) != (val & 0xFF))
1419 cpu_mips_tlb_flush(env, 1);
1422 void helper_mttc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1424 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1425 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1427 other->CP0_EntryHi = arg1;
1428 sync_c0_entryhi(other, other_tc);
1431 void helper_mtc0_compare(CPUMIPSState *env, target_ulong arg1)
1433 cpu_mips_store_compare(env, arg1);
1436 void helper_mtc0_status(CPUMIPSState *env, target_ulong arg1)
1438 MIPSCPU *cpu = mips_env_get_cpu(env);
1439 uint32_t val, old;
1441 old = env->CP0_Status;
1442 cpu_mips_store_status(env, arg1);
1443 val = env->CP0_Status;
1445 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1446 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1447 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1448 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1449 env->CP0_Cause);
1450 switch (env->hflags & MIPS_HFLAG_KSU) {
1451 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1452 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1453 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1454 default:
1455 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
1456 break;
1461 void helper_mttc0_status(CPUMIPSState *env, target_ulong arg1)
1463 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1464 uint32_t mask = env->CP0_Status_rw_bitmask & ~0xf1000018;
1465 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1467 other->CP0_Status = (other->CP0_Status & ~mask) | (arg1 & mask);
1468 sync_c0_status(env, other, other_tc);
1471 void helper_mtc0_intctl(CPUMIPSState *env, target_ulong arg1)
1473 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000003e0) | (arg1 & 0x000003e0);
1476 void helper_mtc0_srsctl(CPUMIPSState *env, target_ulong arg1)
1478 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1479 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1482 void helper_mtc0_cause(CPUMIPSState *env, target_ulong arg1)
1484 cpu_mips_store_cause(env, arg1);
1487 void helper_mttc0_cause(CPUMIPSState *env, target_ulong arg1)
1489 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1490 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1492 cpu_mips_store_cause(other, arg1);
1495 target_ulong helper_mftc0_epc(CPUMIPSState *env)
1497 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1498 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1500 return other->CP0_EPC;
1503 target_ulong helper_mftc0_ebase(CPUMIPSState *env)
1505 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1506 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1508 return other->CP0_EBase;
1511 void helper_mtc0_ebase(CPUMIPSState *env, target_ulong arg1)
1513 env->CP0_EBase = (env->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1516 void helper_mttc0_ebase(CPUMIPSState *env, target_ulong arg1)
1518 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1519 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1520 other->CP0_EBase = (other->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1523 target_ulong helper_mftc0_configx(CPUMIPSState *env, target_ulong idx)
1525 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1526 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1528 switch (idx) {
1529 case 0: return other->CP0_Config0;
1530 case 1: return other->CP0_Config1;
1531 case 2: return other->CP0_Config2;
1532 case 3: return other->CP0_Config3;
1533 /* 4 and 5 are reserved. */
1534 case 6: return other->CP0_Config6;
1535 case 7: return other->CP0_Config7;
1536 default:
1537 break;
1539 return 0;
1542 void helper_mtc0_config0(CPUMIPSState *env, target_ulong arg1)
1544 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1547 void helper_mtc0_config2(CPUMIPSState *env, target_ulong arg1)
1549 /* tertiary/secondary caches not implemented */
1550 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1553 void helper_mtc0_config3(CPUMIPSState *env, target_ulong arg1)
1555 if (env->insn_flags & ASE_MICROMIPS) {
1556 env->CP0_Config3 = (env->CP0_Config3 & ~(1 << CP0C3_ISA_ON_EXC)) |
1557 (arg1 & (1 << CP0C3_ISA_ON_EXC));
1561 void helper_mtc0_config4(CPUMIPSState *env, target_ulong arg1)
1563 env->CP0_Config4 = (env->CP0_Config4 & (~env->CP0_Config4_rw_bitmask)) |
1564 (arg1 & env->CP0_Config4_rw_bitmask);
1567 void helper_mtc0_config5(CPUMIPSState *env, target_ulong arg1)
1569 env->CP0_Config5 = (env->CP0_Config5 & (~env->CP0_Config5_rw_bitmask)) |
1570 (arg1 & env->CP0_Config5_rw_bitmask);
1571 compute_hflags(env);
1574 void helper_mtc0_lladdr(CPUMIPSState *env, target_ulong arg1)
1576 target_long mask = env->CP0_LLAddr_rw_bitmask;
1577 arg1 = arg1 << env->CP0_LLAddr_shift;
1578 env->lladdr = (env->lladdr & ~mask) | (arg1 & mask);
1581 void helper_mtc0_watchlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1583 /* Watch exceptions for instructions, data loads, data stores
1584 not implemented. */
1585 env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1588 void helper_mtc0_watchhi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1590 env->CP0_WatchHi[sel] = (arg1 & 0x40FF0FF8);
1591 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1594 void helper_mtc0_xcontext(CPUMIPSState *env, target_ulong arg1)
1596 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1597 env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1600 void helper_mtc0_framemask(CPUMIPSState *env, target_ulong arg1)
1602 env->CP0_Framemask = arg1; /* XXX */
1605 void helper_mtc0_debug(CPUMIPSState *env, target_ulong arg1)
1607 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
1608 if (arg1 & (1 << CP0DB_DM))
1609 env->hflags |= MIPS_HFLAG_DM;
1610 else
1611 env->hflags &= ~MIPS_HFLAG_DM;
1614 void helper_mttc0_debug(CPUMIPSState *env, target_ulong arg1)
1616 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1617 uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1618 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1620 /* XXX: Might be wrong, check with EJTAG spec. */
1621 if (other_tc == other->current_tc)
1622 other->active_tc.CP0_Debug_tcstatus = val;
1623 else
1624 other->tcs[other_tc].CP0_Debug_tcstatus = val;
1625 other->CP0_Debug = (other->CP0_Debug &
1626 ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1627 (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1630 void helper_mtc0_performance0(CPUMIPSState *env, target_ulong arg1)
1632 env->CP0_Performance0 = arg1 & 0x000007ff;
1635 void helper_mtc0_taglo(CPUMIPSState *env, target_ulong arg1)
1637 env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1640 void helper_mtc0_datalo(CPUMIPSState *env, target_ulong arg1)
1642 env->CP0_DataLo = arg1; /* XXX */
1645 void helper_mtc0_taghi(CPUMIPSState *env, target_ulong arg1)
1647 env->CP0_TagHi = arg1; /* XXX */
1650 void helper_mtc0_datahi(CPUMIPSState *env, target_ulong arg1)
1652 env->CP0_DataHi = arg1; /* XXX */
1655 /* MIPS MT functions */
1656 target_ulong helper_mftgpr(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.gpr[sel];
1663 else
1664 return other->tcs[other_tc].gpr[sel];
1667 target_ulong helper_mftlo(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.LO[sel];
1674 else
1675 return other->tcs[other_tc].LO[sel];
1678 target_ulong helper_mfthi(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.HI[sel];
1685 else
1686 return other->tcs[other_tc].HI[sel];
1689 target_ulong helper_mftacx(CPUMIPSState *env, uint32_t sel)
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.ACX[sel];
1696 else
1697 return other->tcs[other_tc].ACX[sel];
1700 target_ulong helper_mftdsp(CPUMIPSState *env)
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 return other->active_tc.DSPControl;
1707 else
1708 return other->tcs[other_tc].DSPControl;
1711 void helper_mttgpr(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.gpr[sel] = arg1;
1718 else
1719 other->tcs[other_tc].gpr[sel] = arg1;
1722 void helper_mttlo(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.LO[sel] = arg1;
1729 else
1730 other->tcs[other_tc].LO[sel] = arg1;
1733 void helper_mtthi(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.HI[sel] = arg1;
1740 else
1741 other->tcs[other_tc].HI[sel] = arg1;
1744 void helper_mttacx(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
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.ACX[sel] = arg1;
1751 else
1752 other->tcs[other_tc].ACX[sel] = arg1;
1755 void helper_mttdsp(CPUMIPSState *env, target_ulong arg1)
1757 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1758 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1760 if (other_tc == other->current_tc)
1761 other->active_tc.DSPControl = arg1;
1762 else
1763 other->tcs[other_tc].DSPControl = arg1;
1766 /* MIPS MT functions */
1767 target_ulong helper_dmt(void)
1769 // TODO
1770 return 0;
1773 target_ulong helper_emt(void)
1775 // TODO
1776 return 0;
1779 target_ulong helper_dvpe(CPUMIPSState *env)
1781 CPUState *other_cs = first_cpu;
1782 target_ulong prev = env->mvp->CP0_MVPControl;
1784 CPU_FOREACH(other_cs) {
1785 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1786 /* Turn off all VPEs except the one executing the dvpe. */
1787 if (&other_cpu->env != env) {
1788 other_cpu->env.mvp->CP0_MVPControl &= ~(1 << CP0MVPCo_EVP);
1789 mips_vpe_sleep(other_cpu);
1792 return prev;
1795 target_ulong helper_evpe(CPUMIPSState *env)
1797 CPUState *other_cs = first_cpu;
1798 target_ulong prev = env->mvp->CP0_MVPControl;
1800 CPU_FOREACH(other_cs) {
1801 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1803 if (&other_cpu->env != env
1804 /* If the VPE is WFI, don't disturb its sleep. */
1805 && !mips_vpe_is_wfi(other_cpu)) {
1806 /* Enable the VPE. */
1807 other_cpu->env.mvp->CP0_MVPControl |= (1 << CP0MVPCo_EVP);
1808 mips_vpe_wake(other_cpu); /* And wake it up. */
1811 return prev;
1813 #endif /* !CONFIG_USER_ONLY */
1815 void helper_fork(target_ulong arg1, target_ulong arg2)
1817 // arg1 = rt, arg2 = rs
1818 // TODO: store to TC register
1821 target_ulong helper_yield(CPUMIPSState *env, target_ulong arg)
1823 target_long arg1 = arg;
1825 if (arg1 < 0) {
1826 /* No scheduling policy implemented. */
1827 if (arg1 != -2) {
1828 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1829 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
1830 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1831 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1832 do_raise_exception(env, EXCP_THREAD, GETPC());
1835 } else if (arg1 == 0) {
1836 if (0 /* TODO: TC underflow */) {
1837 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1838 do_raise_exception(env, EXCP_THREAD, GETPC());
1839 } else {
1840 // TODO: Deallocate TC
1842 } else if (arg1 > 0) {
1843 /* Yield qualifier inputs not implemented. */
1844 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1845 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1846 do_raise_exception(env, EXCP_THREAD, GETPC());
1848 return env->CP0_YQMask;
1851 /* R6 Multi-threading */
1852 #ifndef CONFIG_USER_ONLY
1853 target_ulong helper_dvp(CPUMIPSState *env)
1855 CPUState *other_cs = first_cpu;
1856 target_ulong prev = env->CP0_VPControl;
1858 if (!((env->CP0_VPControl >> CP0VPCtl_DIS) & 1)) {
1859 CPU_FOREACH(other_cs) {
1860 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1861 /* Turn off all VPs except the one executing the dvp. */
1862 if (&other_cpu->env != env) {
1863 mips_vpe_sleep(other_cpu);
1866 env->CP0_VPControl |= (1 << CP0VPCtl_DIS);
1868 return prev;
1871 target_ulong helper_evp(CPUMIPSState *env)
1873 CPUState *other_cs = first_cpu;
1874 target_ulong prev = env->CP0_VPControl;
1876 if ((env->CP0_VPControl >> CP0VPCtl_DIS) & 1) {
1877 CPU_FOREACH(other_cs) {
1878 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1879 if ((&other_cpu->env != env) && !mips_vp_is_wfi(other_cpu)) {
1880 /* If the VP is WFI, don't disturb its sleep.
1881 * Otherwise, wake it up. */
1882 mips_vpe_wake(other_cpu);
1885 env->CP0_VPControl &= ~(1 << CP0VPCtl_DIS);
1887 return prev;
1889 #endif /* !CONFIG_USER_ONLY */
1891 #ifndef CONFIG_USER_ONLY
1892 /* TLB management */
1893 static void r4k_mips_tlb_flush_extra (CPUMIPSState *env, int first)
1895 /* Discard entries from env->tlb[first] onwards. */
1896 while (env->tlb->tlb_in_use > first) {
1897 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1901 static inline uint64_t get_tlb_pfn_from_entrylo(uint64_t entrylo)
1903 #if defined(TARGET_MIPS64)
1904 return extract64(entrylo, 6, 54);
1905 #else
1906 return extract64(entrylo, 6, 24) | /* PFN */
1907 (extract64(entrylo, 32, 32) << 24); /* PFNX */
1908 #endif
1911 static void r4k_fill_tlb(CPUMIPSState *env, int idx)
1913 r4k_tlb_t *tlb;
1915 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1916 tlb = &env->tlb->mmu.r4k.tlb[idx];
1917 if (env->CP0_EntryHi & (1 << CP0EnHi_EHINV)) {
1918 tlb->EHINV = 1;
1919 return;
1921 tlb->EHINV = 0;
1922 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1923 #if defined(TARGET_MIPS64)
1924 tlb->VPN &= env->SEGMask;
1925 #endif
1926 tlb->ASID = env->CP0_EntryHi & 0xFF;
1927 tlb->PageMask = env->CP0_PageMask;
1928 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1929 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
1930 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
1931 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
1932 tlb->XI0 = (env->CP0_EntryLo0 >> CP0EnLo_XI) & 1;
1933 tlb->RI0 = (env->CP0_EntryLo0 >> CP0EnLo_RI) & 1;
1934 tlb->PFN[0] = get_tlb_pfn_from_entrylo(env->CP0_EntryLo0) << 12;
1935 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
1936 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
1937 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
1938 tlb->XI1 = (env->CP0_EntryLo1 >> CP0EnLo_XI) & 1;
1939 tlb->RI1 = (env->CP0_EntryLo1 >> CP0EnLo_RI) & 1;
1940 tlb->PFN[1] = get_tlb_pfn_from_entrylo(env->CP0_EntryLo1) << 12;
1943 void r4k_helper_tlbinv(CPUMIPSState *env)
1945 int idx;
1946 r4k_tlb_t *tlb;
1947 uint8_t ASID = env->CP0_EntryHi & 0xFF;
1949 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
1950 tlb = &env->tlb->mmu.r4k.tlb[idx];
1951 if (!tlb->G && tlb->ASID == ASID) {
1952 tlb->EHINV = 1;
1955 cpu_mips_tlb_flush(env, 1);
1958 void r4k_helper_tlbinvf(CPUMIPSState *env)
1960 int idx;
1962 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
1963 env->tlb->mmu.r4k.tlb[idx].EHINV = 1;
1965 cpu_mips_tlb_flush(env, 1);
1968 void r4k_helper_tlbwi(CPUMIPSState *env)
1970 r4k_tlb_t *tlb;
1971 int idx;
1972 target_ulong VPN;
1973 uint8_t ASID;
1974 bool G, V0, D0, V1, D1;
1976 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1977 tlb = &env->tlb->mmu.r4k.tlb[idx];
1978 VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1979 #if defined(TARGET_MIPS64)
1980 VPN &= env->SEGMask;
1981 #endif
1982 ASID = env->CP0_EntryHi & 0xff;
1983 G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1984 V0 = (env->CP0_EntryLo0 & 2) != 0;
1985 D0 = (env->CP0_EntryLo0 & 4) != 0;
1986 V1 = (env->CP0_EntryLo1 & 2) != 0;
1987 D1 = (env->CP0_EntryLo1 & 4) != 0;
1989 /* Discard cached TLB entries, unless tlbwi is just upgrading access
1990 permissions on the current entry. */
1991 if (tlb->VPN != VPN || tlb->ASID != ASID || tlb->G != G ||
1992 (tlb->V0 && !V0) || (tlb->D0 && !D0) ||
1993 (tlb->V1 && !V1) || (tlb->D1 && !D1)) {
1994 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
1997 r4k_invalidate_tlb(env, idx, 0);
1998 r4k_fill_tlb(env, idx);
2001 void r4k_helper_tlbwr(CPUMIPSState *env)
2003 int r = cpu_mips_get_random(env);
2005 r4k_invalidate_tlb(env, r, 1);
2006 r4k_fill_tlb(env, r);
2009 void r4k_helper_tlbp(CPUMIPSState *env)
2011 r4k_tlb_t *tlb;
2012 target_ulong mask;
2013 target_ulong tag;
2014 target_ulong VPN;
2015 uint8_t ASID;
2016 int i;
2018 ASID = env->CP0_EntryHi & 0xFF;
2019 for (i = 0; i < env->tlb->nb_tlb; i++) {
2020 tlb = &env->tlb->mmu.r4k.tlb[i];
2021 /* 1k pages are not supported. */
2022 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
2023 tag = env->CP0_EntryHi & ~mask;
2024 VPN = tlb->VPN & ~mask;
2025 #if defined(TARGET_MIPS64)
2026 tag &= env->SEGMask;
2027 #endif
2028 /* Check ASID, virtual page number & size */
2029 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
2030 /* TLB match */
2031 env->CP0_Index = i;
2032 break;
2035 if (i == env->tlb->nb_tlb) {
2036 /* No match. Discard any shadow entries, if any of them match. */
2037 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
2038 tlb = &env->tlb->mmu.r4k.tlb[i];
2039 /* 1k pages are not supported. */
2040 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
2041 tag = env->CP0_EntryHi & ~mask;
2042 VPN = tlb->VPN & ~mask;
2043 #if defined(TARGET_MIPS64)
2044 tag &= env->SEGMask;
2045 #endif
2046 /* Check ASID, virtual page number & size */
2047 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
2048 r4k_mips_tlb_flush_extra (env, i);
2049 break;
2053 env->CP0_Index |= 0x80000000;
2057 static inline uint64_t get_entrylo_pfn_from_tlb(uint64_t tlb_pfn)
2059 #if defined(TARGET_MIPS64)
2060 return tlb_pfn << 6;
2061 #else
2062 return (extract64(tlb_pfn, 0, 24) << 6) | /* PFN */
2063 (extract64(tlb_pfn, 24, 32) << 32); /* PFNX */
2064 #endif
2067 void r4k_helper_tlbr(CPUMIPSState *env)
2069 r4k_tlb_t *tlb;
2070 uint8_t ASID;
2071 int idx;
2073 ASID = env->CP0_EntryHi & 0xFF;
2074 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
2075 tlb = &env->tlb->mmu.r4k.tlb[idx];
2077 /* If this will change the current ASID, flush qemu's TLB. */
2078 if (ASID != tlb->ASID)
2079 cpu_mips_tlb_flush (env, 1);
2081 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
2083 if (tlb->EHINV) {
2084 env->CP0_EntryHi = 1 << CP0EnHi_EHINV;
2085 env->CP0_PageMask = 0;
2086 env->CP0_EntryLo0 = 0;
2087 env->CP0_EntryLo1 = 0;
2088 } else {
2089 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
2090 env->CP0_PageMask = tlb->PageMask;
2091 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
2092 ((uint64_t)tlb->RI0 << CP0EnLo_RI) |
2093 ((uint64_t)tlb->XI0 << CP0EnLo_XI) | (tlb->C0 << 3) |
2094 get_entrylo_pfn_from_tlb(tlb->PFN[0] >> 12);
2095 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
2096 ((uint64_t)tlb->RI1 << CP0EnLo_RI) |
2097 ((uint64_t)tlb->XI1 << CP0EnLo_XI) | (tlb->C1 << 3) |
2098 get_entrylo_pfn_from_tlb(tlb->PFN[1] >> 12);
2102 void helper_tlbwi(CPUMIPSState *env)
2104 env->tlb->helper_tlbwi(env);
2107 void helper_tlbwr(CPUMIPSState *env)
2109 env->tlb->helper_tlbwr(env);
2112 void helper_tlbp(CPUMIPSState *env)
2114 env->tlb->helper_tlbp(env);
2117 void helper_tlbr(CPUMIPSState *env)
2119 env->tlb->helper_tlbr(env);
2122 void helper_tlbinv(CPUMIPSState *env)
2124 env->tlb->helper_tlbinv(env);
2127 void helper_tlbinvf(CPUMIPSState *env)
2129 env->tlb->helper_tlbinvf(env);
2132 /* Specials */
2133 target_ulong helper_di(CPUMIPSState *env)
2135 target_ulong t0 = env->CP0_Status;
2137 env->CP0_Status = t0 & ~(1 << CP0St_IE);
2138 return t0;
2141 target_ulong helper_ei(CPUMIPSState *env)
2143 target_ulong t0 = env->CP0_Status;
2145 env->CP0_Status = t0 | (1 << CP0St_IE);
2146 return t0;
2149 static void debug_pre_eret(CPUMIPSState *env)
2151 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2152 qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2153 env->active_tc.PC, env->CP0_EPC);
2154 if (env->CP0_Status & (1 << CP0St_ERL))
2155 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2156 if (env->hflags & MIPS_HFLAG_DM)
2157 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2158 qemu_log("\n");
2162 static void debug_post_eret(CPUMIPSState *env)
2164 MIPSCPU *cpu = mips_env_get_cpu(env);
2166 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2167 qemu_log(" => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2168 env->active_tc.PC, env->CP0_EPC);
2169 if (env->CP0_Status & (1 << CP0St_ERL))
2170 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2171 if (env->hflags & MIPS_HFLAG_DM)
2172 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2173 switch (env->hflags & MIPS_HFLAG_KSU) {
2174 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
2175 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
2176 case MIPS_HFLAG_KM: qemu_log("\n"); break;
2177 default:
2178 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
2179 break;
2184 static void set_pc(CPUMIPSState *env, target_ulong error_pc)
2186 env->active_tc.PC = error_pc & ~(target_ulong)1;
2187 if (error_pc & 1) {
2188 env->hflags |= MIPS_HFLAG_M16;
2189 } else {
2190 env->hflags &= ~(MIPS_HFLAG_M16);
2194 static inline void exception_return(CPUMIPSState *env)
2196 debug_pre_eret(env);
2197 if (env->CP0_Status & (1 << CP0St_ERL)) {
2198 set_pc(env, env->CP0_ErrorEPC);
2199 env->CP0_Status &= ~(1 << CP0St_ERL);
2200 } else {
2201 set_pc(env, env->CP0_EPC);
2202 env->CP0_Status &= ~(1 << CP0St_EXL);
2204 compute_hflags(env);
2205 debug_post_eret(env);
2208 void helper_eret(CPUMIPSState *env)
2210 exception_return(env);
2211 env->lladdr = 1;
2214 void helper_eretnc(CPUMIPSState *env)
2216 exception_return(env);
2219 void helper_deret(CPUMIPSState *env)
2221 debug_pre_eret(env);
2222 set_pc(env, env->CP0_DEPC);
2224 env->hflags &= ~MIPS_HFLAG_DM;
2225 compute_hflags(env);
2226 debug_post_eret(env);
2228 #endif /* !CONFIG_USER_ONLY */
2230 static inline void check_hwrena(CPUMIPSState *env, int reg)
2232 if ((env->hflags & MIPS_HFLAG_CP0) || (env->CP0_HWREna & (1 << reg))) {
2233 return;
2235 do_raise_exception(env, EXCP_RI, GETPC());
2238 target_ulong helper_rdhwr_cpunum(CPUMIPSState *env)
2240 check_hwrena(env, 0);
2241 return env->CP0_EBase & 0x3ff;
2244 target_ulong helper_rdhwr_synci_step(CPUMIPSState *env)
2246 check_hwrena(env, 1);
2247 return env->SYNCI_Step;
2250 target_ulong helper_rdhwr_cc(CPUMIPSState *env)
2252 check_hwrena(env, 2);
2253 #ifdef CONFIG_USER_ONLY
2254 return env->CP0_Count;
2255 #else
2256 return (int32_t)cpu_mips_get_count(env);
2257 #endif
2260 target_ulong helper_rdhwr_ccres(CPUMIPSState *env)
2262 check_hwrena(env, 3);
2263 return env->CCRes;
2266 target_ulong helper_rdhwr_performance(CPUMIPSState *env)
2268 check_hwrena(env, 4);
2269 return env->CP0_Performance0;
2272 target_ulong helper_rdhwr_xnp(CPUMIPSState *env)
2274 check_hwrena(env, 5);
2275 return (env->CP0_Config5 >> CP0C5_XNP) & 1;
2278 void helper_pmon(CPUMIPSState *env, int function)
2280 function /= 2;
2281 switch (function) {
2282 case 2: /* TODO: char inbyte(int waitflag); */
2283 if (env->active_tc.gpr[4] == 0)
2284 env->active_tc.gpr[2] = -1;
2285 /* Fall through */
2286 case 11: /* TODO: char inbyte (void); */
2287 env->active_tc.gpr[2] = -1;
2288 break;
2289 case 3:
2290 case 12:
2291 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
2292 break;
2293 case 17:
2294 break;
2295 case 158:
2297 unsigned char *fmt = (void *)(uintptr_t)env->active_tc.gpr[4];
2298 printf("%s", fmt);
2300 break;
2304 void helper_wait(CPUMIPSState *env)
2306 CPUState *cs = CPU(mips_env_get_cpu(env));
2308 cs->halted = 1;
2309 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
2310 /* Last instruction in the block, PC was updated before
2311 - no need to recover PC and icount */
2312 raise_exception(env, EXCP_HLT);
2315 #if !defined(CONFIG_USER_ONLY)
2317 void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
2318 int access_type, int is_user,
2319 uintptr_t retaddr)
2321 MIPSCPU *cpu = MIPS_CPU(cs);
2322 CPUMIPSState *env = &cpu->env;
2323 int error_code = 0;
2324 int excp;
2326 env->CP0_BadVAddr = addr;
2328 if (access_type == MMU_DATA_STORE) {
2329 excp = EXCP_AdES;
2330 } else {
2331 excp = EXCP_AdEL;
2332 if (access_type == MMU_INST_FETCH) {
2333 error_code |= EXCP_INST_NOTAVAIL;
2337 do_raise_exception_err(env, excp, error_code, retaddr);
2340 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
2341 uintptr_t retaddr)
2343 int ret;
2345 ret = mips_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
2346 if (ret) {
2347 MIPSCPU *cpu = MIPS_CPU(cs);
2348 CPUMIPSState *env = &cpu->env;
2350 do_raise_exception_err(env, cs->exception_index,
2351 env->error_code, retaddr);
2355 void mips_cpu_unassigned_access(CPUState *cs, hwaddr addr,
2356 bool is_write, bool is_exec, int unused,
2357 unsigned size)
2359 MIPSCPU *cpu = MIPS_CPU(cs);
2360 CPUMIPSState *env = &cpu->env;
2363 * Raising an exception with KVM enabled will crash because it won't be from
2364 * the main execution loop so the longjmp won't have a matching setjmp.
2365 * Until we can trigger a bus error exception through KVM lets just ignore
2366 * the access.
2368 if (kvm_enabled()) {
2369 return;
2372 if (is_exec) {
2373 raise_exception(env, EXCP_IBE);
2374 } else {
2375 raise_exception(env, EXCP_DBE);
2378 #endif /* !CONFIG_USER_ONLY */
2380 /* Complex FPU operations which may need stack space. */
2382 #define FLOAT_TWO32 make_float32(1 << 30)
2383 #define FLOAT_TWO64 make_float64(1ULL << 62)
2384 #define FP_TO_INT32_OVERFLOW 0x7fffffff
2385 #define FP_TO_INT64_OVERFLOW 0x7fffffffffffffffULL
2387 /* convert MIPS rounding mode in FCR31 to IEEE library */
2388 unsigned int ieee_rm[] = {
2389 float_round_nearest_even,
2390 float_round_to_zero,
2391 float_round_up,
2392 float_round_down
2395 target_ulong helper_cfc1(CPUMIPSState *env, uint32_t reg)
2397 target_ulong arg1 = 0;
2399 switch (reg) {
2400 case 0:
2401 arg1 = (int32_t)env->active_fpu.fcr0;
2402 break;
2403 case 1:
2404 /* UFR Support - Read Status FR */
2405 if (env->active_fpu.fcr0 & (1 << FCR0_UFRP)) {
2406 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2407 arg1 = (int32_t)
2408 ((env->CP0_Status & (1 << CP0St_FR)) >> CP0St_FR);
2409 } else {
2410 do_raise_exception(env, EXCP_RI, GETPC());
2413 break;
2414 case 5:
2415 /* FRE Support - read Config5.FRE bit */
2416 if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
2417 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2418 arg1 = (env->CP0_Config5 >> CP0C5_FRE) & 1;
2419 } else {
2420 helper_raise_exception(env, EXCP_RI);
2423 break;
2424 case 25:
2425 arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);
2426 break;
2427 case 26:
2428 arg1 = env->active_fpu.fcr31 & 0x0003f07c;
2429 break;
2430 case 28:
2431 arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);
2432 break;
2433 default:
2434 arg1 = (int32_t)env->active_fpu.fcr31;
2435 break;
2438 return arg1;
2441 void helper_ctc1(CPUMIPSState *env, target_ulong arg1, uint32_t fs, uint32_t rt)
2443 switch (fs) {
2444 case 1:
2445 /* UFR Alias - Reset Status FR */
2446 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2447 return;
2449 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2450 env->CP0_Status &= ~(1 << CP0St_FR);
2451 compute_hflags(env);
2452 } else {
2453 do_raise_exception(env, EXCP_RI, GETPC());
2455 break;
2456 case 4:
2457 /* UNFR Alias - Set Status FR */
2458 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2459 return;
2461 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2462 env->CP0_Status |= (1 << CP0St_FR);
2463 compute_hflags(env);
2464 } else {
2465 do_raise_exception(env, EXCP_RI, GETPC());
2467 break;
2468 case 5:
2469 /* FRE Support - clear Config5.FRE bit */
2470 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2471 return;
2473 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2474 env->CP0_Config5 &= ~(1 << CP0C5_FRE);
2475 compute_hflags(env);
2476 } else {
2477 helper_raise_exception(env, EXCP_RI);
2479 break;
2480 case 6:
2481 /* FRE Support - set Config5.FRE bit */
2482 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2483 return;
2485 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2486 env->CP0_Config5 |= (1 << CP0C5_FRE);
2487 compute_hflags(env);
2488 } else {
2489 helper_raise_exception(env, EXCP_RI);
2491 break;
2492 case 25:
2493 if ((env->insn_flags & ISA_MIPS32R6) || (arg1 & 0xffffff00)) {
2494 return;
2496 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
2497 ((arg1 & 0x1) << 23);
2498 break;
2499 case 26:
2500 if (arg1 & 0x007c0000)
2501 return;
2502 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
2503 break;
2504 case 28:
2505 if (arg1 & 0x007c0000)
2506 return;
2507 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
2508 ((arg1 & 0x4) << 22);
2509 break;
2510 case 31:
2511 if (env->insn_flags & ISA_MIPS32R6) {
2512 uint32_t mask = 0xfefc0000;
2513 env->active_fpu.fcr31 = (arg1 & ~mask) |
2514 (env->active_fpu.fcr31 & mask);
2515 } else if (!(arg1 & 0x007c0000)) {
2516 env->active_fpu.fcr31 = arg1;
2518 break;
2519 default:
2520 return;
2522 /* set rounding mode */
2523 restore_rounding_mode(env);
2524 /* set flush-to-zero mode */
2525 restore_flush_mode(env);
2526 set_float_exception_flags(0, &env->active_fpu.fp_status);
2527 if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
2528 do_raise_exception(env, EXCP_FPE, GETPC());
2531 int ieee_ex_to_mips(int xcpt)
2533 int ret = 0;
2534 if (xcpt) {
2535 if (xcpt & float_flag_invalid) {
2536 ret |= FP_INVALID;
2538 if (xcpt & float_flag_overflow) {
2539 ret |= FP_OVERFLOW;
2541 if (xcpt & float_flag_underflow) {
2542 ret |= FP_UNDERFLOW;
2544 if (xcpt & float_flag_divbyzero) {
2545 ret |= FP_DIV0;
2547 if (xcpt & float_flag_inexact) {
2548 ret |= FP_INEXACT;
2551 return ret;
2554 static inline void update_fcr31(CPUMIPSState *env, uintptr_t pc)
2556 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
2558 SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
2560 if (tmp) {
2561 set_float_exception_flags(0, &env->active_fpu.fp_status);
2563 if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp) {
2564 do_raise_exception(env, EXCP_FPE, pc);
2565 } else {
2566 UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
2571 /* Float support.
2572 Single precition routines have a "s" suffix, double precision a
2573 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
2574 paired single lower "pl", paired single upper "pu". */
2576 /* unary operations, modifying fp status */
2577 uint64_t helper_float_sqrt_d(CPUMIPSState *env, uint64_t fdt0)
2579 fdt0 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2580 update_fcr31(env, GETPC());
2581 return fdt0;
2584 uint32_t helper_float_sqrt_s(CPUMIPSState *env, uint32_t fst0)
2586 fst0 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2587 update_fcr31(env, GETPC());
2588 return fst0;
2591 uint64_t helper_float_cvtd_s(CPUMIPSState *env, uint32_t fst0)
2593 uint64_t fdt2;
2595 fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
2596 fdt2 = float64_maybe_silence_nan(fdt2);
2597 update_fcr31(env, GETPC());
2598 return fdt2;
2601 uint64_t helper_float_cvtd_w(CPUMIPSState *env, uint32_t wt0)
2603 uint64_t fdt2;
2605 fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2606 update_fcr31(env, GETPC());
2607 return fdt2;
2610 uint64_t helper_float_cvtd_l(CPUMIPSState *env, uint64_t dt0)
2612 uint64_t fdt2;
2614 fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2615 update_fcr31(env, GETPC());
2616 return fdt2;
2619 uint64_t helper_float_cvtl_d(CPUMIPSState *env, uint64_t fdt0)
2621 uint64_t dt2;
2623 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2624 if (get_float_exception_flags(&env->active_fpu.fp_status)
2625 & (float_flag_invalid | float_flag_overflow)) {
2626 dt2 = FP_TO_INT64_OVERFLOW;
2628 update_fcr31(env, GETPC());
2629 return dt2;
2632 uint64_t helper_float_cvtl_s(CPUMIPSState *env, uint32_t fst0)
2634 uint64_t dt2;
2636 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2637 if (get_float_exception_flags(&env->active_fpu.fp_status)
2638 & (float_flag_invalid | float_flag_overflow)) {
2639 dt2 = FP_TO_INT64_OVERFLOW;
2641 update_fcr31(env, GETPC());
2642 return dt2;
2645 uint64_t helper_float_cvtps_pw(CPUMIPSState *env, uint64_t dt0)
2647 uint32_t fst2;
2648 uint32_t fsth2;
2650 fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2651 fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
2652 update_fcr31(env, GETPC());
2653 return ((uint64_t)fsth2 << 32) | fst2;
2656 uint64_t helper_float_cvtpw_ps(CPUMIPSState *env, uint64_t fdt0)
2658 uint32_t wt2;
2659 uint32_t wth2;
2660 int excp, excph;
2662 wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2663 excp = get_float_exception_flags(&env->active_fpu.fp_status);
2664 if (excp & (float_flag_overflow | float_flag_invalid)) {
2665 wt2 = FP_TO_INT32_OVERFLOW;
2668 set_float_exception_flags(0, &env->active_fpu.fp_status);
2669 wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
2670 excph = get_float_exception_flags(&env->active_fpu.fp_status);
2671 if (excph & (float_flag_overflow | float_flag_invalid)) {
2672 wth2 = FP_TO_INT32_OVERFLOW;
2675 set_float_exception_flags(excp | excph, &env->active_fpu.fp_status);
2676 update_fcr31(env, GETPC());
2678 return ((uint64_t)wth2 << 32) | wt2;
2681 uint32_t helper_float_cvts_d(CPUMIPSState *env, uint64_t fdt0)
2683 uint32_t fst2;
2685 fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
2686 fst2 = float32_maybe_silence_nan(fst2);
2687 update_fcr31(env, GETPC());
2688 return fst2;
2691 uint32_t helper_float_cvts_w(CPUMIPSState *env, uint32_t wt0)
2693 uint32_t fst2;
2695 fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
2696 update_fcr31(env, GETPC());
2697 return fst2;
2700 uint32_t helper_float_cvts_l(CPUMIPSState *env, uint64_t dt0)
2702 uint32_t fst2;
2704 fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
2705 update_fcr31(env, GETPC());
2706 return fst2;
2709 uint32_t helper_float_cvts_pl(CPUMIPSState *env, uint32_t wt0)
2711 uint32_t wt2;
2713 wt2 = wt0;
2714 update_fcr31(env, GETPC());
2715 return wt2;
2718 uint32_t helper_float_cvts_pu(CPUMIPSState *env, uint32_t wth0)
2720 uint32_t wt2;
2722 wt2 = wth0;
2723 update_fcr31(env, GETPC());
2724 return wt2;
2727 uint32_t helper_float_cvtw_s(CPUMIPSState *env, uint32_t fst0)
2729 uint32_t wt2;
2731 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2732 if (get_float_exception_flags(&env->active_fpu.fp_status)
2733 & (float_flag_invalid | float_flag_overflow)) {
2734 wt2 = FP_TO_INT32_OVERFLOW;
2736 update_fcr31(env, GETPC());
2737 return wt2;
2740 uint32_t helper_float_cvtw_d(CPUMIPSState *env, uint64_t fdt0)
2742 uint32_t wt2;
2744 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2745 if (get_float_exception_flags(&env->active_fpu.fp_status)
2746 & (float_flag_invalid | float_flag_overflow)) {
2747 wt2 = FP_TO_INT32_OVERFLOW;
2749 update_fcr31(env, GETPC());
2750 return wt2;
2753 uint64_t helper_float_roundl_d(CPUMIPSState *env, uint64_t fdt0)
2755 uint64_t dt2;
2757 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2758 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2759 restore_rounding_mode(env);
2760 if (get_float_exception_flags(&env->active_fpu.fp_status)
2761 & (float_flag_invalid | float_flag_overflow)) {
2762 dt2 = FP_TO_INT64_OVERFLOW;
2764 update_fcr31(env, GETPC());
2765 return dt2;
2768 uint64_t helper_float_roundl_s(CPUMIPSState *env, uint32_t fst0)
2770 uint64_t dt2;
2772 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2773 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2774 restore_rounding_mode(env);
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 uint32_t helper_float_roundw_d(CPUMIPSState *env, uint64_t fdt0)
2785 uint32_t wt2;
2787 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2788 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2789 restore_rounding_mode(env);
2790 if (get_float_exception_flags(&env->active_fpu.fp_status)
2791 & (float_flag_invalid | float_flag_overflow)) {
2792 wt2 = FP_TO_INT32_OVERFLOW;
2794 update_fcr31(env, GETPC());
2795 return wt2;
2798 uint32_t helper_float_roundw_s(CPUMIPSState *env, uint32_t fst0)
2800 uint32_t wt2;
2802 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2803 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2804 restore_rounding_mode(env);
2805 if (get_float_exception_flags(&env->active_fpu.fp_status)
2806 & (float_flag_invalid | float_flag_overflow)) {
2807 wt2 = FP_TO_INT32_OVERFLOW;
2809 update_fcr31(env, GETPC());
2810 return wt2;
2813 uint64_t helper_float_truncl_d(CPUMIPSState *env, uint64_t fdt0)
2815 uint64_t dt2;
2817 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
2818 if (get_float_exception_flags(&env->active_fpu.fp_status)
2819 & (float_flag_invalid | float_flag_overflow)) {
2820 dt2 = FP_TO_INT64_OVERFLOW;
2822 update_fcr31(env, GETPC());
2823 return dt2;
2826 uint64_t helper_float_truncl_s(CPUMIPSState *env, uint32_t fst0)
2828 uint64_t dt2;
2830 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
2831 if (get_float_exception_flags(&env->active_fpu.fp_status)
2832 & (float_flag_invalid | float_flag_overflow)) {
2833 dt2 = FP_TO_INT64_OVERFLOW;
2835 update_fcr31(env, GETPC());
2836 return dt2;
2839 uint32_t helper_float_truncw_d(CPUMIPSState *env, uint64_t fdt0)
2841 uint32_t wt2;
2843 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
2844 if (get_float_exception_flags(&env->active_fpu.fp_status)
2845 & (float_flag_invalid | float_flag_overflow)) {
2846 wt2 = FP_TO_INT32_OVERFLOW;
2848 update_fcr31(env, GETPC());
2849 return wt2;
2852 uint32_t helper_float_truncw_s(CPUMIPSState *env, uint32_t fst0)
2854 uint32_t wt2;
2856 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
2857 if (get_float_exception_flags(&env->active_fpu.fp_status)
2858 & (float_flag_invalid | float_flag_overflow)) {
2859 wt2 = FP_TO_INT32_OVERFLOW;
2861 update_fcr31(env, GETPC());
2862 return wt2;
2865 uint64_t helper_float_ceill_d(CPUMIPSState *env, uint64_t fdt0)
2867 uint64_t dt2;
2869 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2870 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2871 restore_rounding_mode(env);
2872 if (get_float_exception_flags(&env->active_fpu.fp_status)
2873 & (float_flag_invalid | float_flag_overflow)) {
2874 dt2 = FP_TO_INT64_OVERFLOW;
2876 update_fcr31(env, GETPC());
2877 return dt2;
2880 uint64_t helper_float_ceill_s(CPUMIPSState *env, uint32_t fst0)
2882 uint64_t dt2;
2884 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2885 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2886 restore_rounding_mode(env);
2887 if (get_float_exception_flags(&env->active_fpu.fp_status)
2888 & (float_flag_invalid | float_flag_overflow)) {
2889 dt2 = FP_TO_INT64_OVERFLOW;
2891 update_fcr31(env, GETPC());
2892 return dt2;
2895 uint32_t helper_float_ceilw_d(CPUMIPSState *env, uint64_t fdt0)
2897 uint32_t wt2;
2899 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2900 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2901 restore_rounding_mode(env);
2902 if (get_float_exception_flags(&env->active_fpu.fp_status)
2903 & (float_flag_invalid | float_flag_overflow)) {
2904 wt2 = FP_TO_INT32_OVERFLOW;
2906 update_fcr31(env, GETPC());
2907 return wt2;
2910 uint32_t helper_float_ceilw_s(CPUMIPSState *env, uint32_t fst0)
2912 uint32_t wt2;
2914 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2915 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2916 restore_rounding_mode(env);
2917 if (get_float_exception_flags(&env->active_fpu.fp_status)
2918 & (float_flag_invalid | float_flag_overflow)) {
2919 wt2 = FP_TO_INT32_OVERFLOW;
2921 update_fcr31(env, GETPC());
2922 return wt2;
2925 uint64_t helper_float_floorl_d(CPUMIPSState *env, uint64_t fdt0)
2927 uint64_t dt2;
2929 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2930 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2931 restore_rounding_mode(env);
2932 if (get_float_exception_flags(&env->active_fpu.fp_status)
2933 & (float_flag_invalid | float_flag_overflow)) {
2934 dt2 = FP_TO_INT64_OVERFLOW;
2936 update_fcr31(env, GETPC());
2937 return dt2;
2940 uint64_t helper_float_floorl_s(CPUMIPSState *env, uint32_t fst0)
2942 uint64_t dt2;
2944 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2945 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2946 restore_rounding_mode(env);
2947 if (get_float_exception_flags(&env->active_fpu.fp_status)
2948 & (float_flag_invalid | float_flag_overflow)) {
2949 dt2 = FP_TO_INT64_OVERFLOW;
2951 update_fcr31(env, GETPC());
2952 return dt2;
2955 uint32_t helper_float_floorw_d(CPUMIPSState *env, uint64_t fdt0)
2957 uint32_t wt2;
2959 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2960 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2961 restore_rounding_mode(env);
2962 if (get_float_exception_flags(&env->active_fpu.fp_status)
2963 & (float_flag_invalid | float_flag_overflow)) {
2964 wt2 = FP_TO_INT32_OVERFLOW;
2966 update_fcr31(env, GETPC());
2967 return wt2;
2970 uint32_t helper_float_floorw_s(CPUMIPSState *env, uint32_t fst0)
2972 uint32_t wt2;
2974 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2975 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2976 restore_rounding_mode(env);
2977 if (get_float_exception_flags(&env->active_fpu.fp_status)
2978 & (float_flag_invalid | float_flag_overflow)) {
2979 wt2 = FP_TO_INT32_OVERFLOW;
2981 update_fcr31(env, GETPC());
2982 return wt2;
2985 /* unary operations, not modifying fp status */
2986 #define FLOAT_UNOP(name) \
2987 uint64_t helper_float_ ## name ## _d(uint64_t fdt0) \
2989 return float64_ ## name(fdt0); \
2991 uint32_t helper_float_ ## name ## _s(uint32_t fst0) \
2993 return float32_ ## name(fst0); \
2995 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0) \
2997 uint32_t wt0; \
2998 uint32_t wth0; \
3000 wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF); \
3001 wth0 = float32_ ## name(fdt0 >> 32); \
3002 return ((uint64_t)wth0 << 32) | wt0; \
3004 FLOAT_UNOP(abs)
3005 FLOAT_UNOP(chs)
3006 #undef FLOAT_UNOP
3008 /* MIPS specific unary operations */
3009 uint64_t helper_float_recip_d(CPUMIPSState *env, uint64_t fdt0)
3011 uint64_t fdt2;
3013 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
3014 update_fcr31(env, GETPC());
3015 return fdt2;
3018 uint32_t helper_float_recip_s(CPUMIPSState *env, uint32_t fst0)
3020 uint32_t fst2;
3022 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
3023 update_fcr31(env, GETPC());
3024 return fst2;
3027 uint64_t helper_float_rsqrt_d(CPUMIPSState *env, uint64_t fdt0)
3029 uint64_t fdt2;
3031 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
3032 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
3033 update_fcr31(env, GETPC());
3034 return fdt2;
3037 uint32_t helper_float_rsqrt_s(CPUMIPSState *env, uint32_t fst0)
3039 uint32_t fst2;
3041 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
3042 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3043 update_fcr31(env, GETPC());
3044 return fst2;
3047 uint64_t helper_float_recip1_d(CPUMIPSState *env, uint64_t fdt0)
3049 uint64_t fdt2;
3051 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
3052 update_fcr31(env, GETPC());
3053 return fdt2;
3056 uint32_t helper_float_recip1_s(CPUMIPSState *env, uint32_t fst0)
3058 uint32_t fst2;
3060 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
3061 update_fcr31(env, GETPC());
3062 return fst2;
3065 uint64_t helper_float_recip1_ps(CPUMIPSState *env, uint64_t fdt0)
3067 uint32_t fst2;
3068 uint32_t fsth2;
3070 fst2 = float32_div(float32_one, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3071 fsth2 = float32_div(float32_one, fdt0 >> 32, &env->active_fpu.fp_status);
3072 update_fcr31(env, GETPC());
3073 return ((uint64_t)fsth2 << 32) | fst2;
3076 uint64_t helper_float_rsqrt1_d(CPUMIPSState *env, uint64_t fdt0)
3078 uint64_t fdt2;
3080 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
3081 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
3082 update_fcr31(env, GETPC());
3083 return fdt2;
3086 uint32_t helper_float_rsqrt1_s(CPUMIPSState *env, uint32_t fst0)
3088 uint32_t fst2;
3090 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
3091 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3092 update_fcr31(env, GETPC());
3093 return fst2;
3096 uint64_t helper_float_rsqrt1_ps(CPUMIPSState *env, uint64_t fdt0)
3098 uint32_t fst2;
3099 uint32_t fsth2;
3101 fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3102 fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
3103 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3104 fsth2 = float32_div(float32_one, fsth2, &env->active_fpu.fp_status);
3105 update_fcr31(env, GETPC());
3106 return ((uint64_t)fsth2 << 32) | fst2;
3109 #define FLOAT_RINT(name, bits) \
3110 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3111 uint ## bits ## _t fs) \
3113 uint ## bits ## _t fdret; \
3115 fdret = float ## bits ## _round_to_int(fs, &env->active_fpu.fp_status); \
3116 update_fcr31(env, GETPC()); \
3117 return fdret; \
3120 FLOAT_RINT(rint_s, 32)
3121 FLOAT_RINT(rint_d, 64)
3122 #undef FLOAT_RINT
3124 #define FLOAT_CLASS_SIGNALING_NAN 0x001
3125 #define FLOAT_CLASS_QUIET_NAN 0x002
3126 #define FLOAT_CLASS_NEGATIVE_INFINITY 0x004
3127 #define FLOAT_CLASS_NEGATIVE_NORMAL 0x008
3128 #define FLOAT_CLASS_NEGATIVE_SUBNORMAL 0x010
3129 #define FLOAT_CLASS_NEGATIVE_ZERO 0x020
3130 #define FLOAT_CLASS_POSITIVE_INFINITY 0x040
3131 #define FLOAT_CLASS_POSITIVE_NORMAL 0x080
3132 #define FLOAT_CLASS_POSITIVE_SUBNORMAL 0x100
3133 #define FLOAT_CLASS_POSITIVE_ZERO 0x200
3135 #define FLOAT_CLASS(name, bits) \
3136 uint ## bits ## _t helper_float_ ## name (uint ## bits ## _t arg) \
3138 if (float ## bits ## _is_signaling_nan(arg)) { \
3139 return FLOAT_CLASS_SIGNALING_NAN; \
3140 } else if (float ## bits ## _is_quiet_nan(arg)) { \
3141 return FLOAT_CLASS_QUIET_NAN; \
3142 } else if (float ## bits ## _is_neg(arg)) { \
3143 if (float ## bits ## _is_infinity(arg)) { \
3144 return FLOAT_CLASS_NEGATIVE_INFINITY; \
3145 } else if (float ## bits ## _is_zero(arg)) { \
3146 return FLOAT_CLASS_NEGATIVE_ZERO; \
3147 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3148 return FLOAT_CLASS_NEGATIVE_SUBNORMAL; \
3149 } else { \
3150 return FLOAT_CLASS_NEGATIVE_NORMAL; \
3152 } else { \
3153 if (float ## bits ## _is_infinity(arg)) { \
3154 return FLOAT_CLASS_POSITIVE_INFINITY; \
3155 } else if (float ## bits ## _is_zero(arg)) { \
3156 return FLOAT_CLASS_POSITIVE_ZERO; \
3157 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3158 return FLOAT_CLASS_POSITIVE_SUBNORMAL; \
3159 } else { \
3160 return FLOAT_CLASS_POSITIVE_NORMAL; \
3165 FLOAT_CLASS(class_s, 32)
3166 FLOAT_CLASS(class_d, 64)
3167 #undef FLOAT_CLASS
3169 /* binary operations */
3170 #define FLOAT_BINOP(name) \
3171 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
3172 uint64_t fdt0, uint64_t fdt1) \
3174 uint64_t dt2; \
3176 dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status); \
3177 update_fcr31(env, GETPC()); \
3178 return dt2; \
3181 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
3182 uint32_t fst0, uint32_t fst1) \
3184 uint32_t wt2; \
3186 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3187 update_fcr31(env, GETPC()); \
3188 return wt2; \
3191 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
3192 uint64_t fdt0, \
3193 uint64_t fdt1) \
3195 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3196 uint32_t fsth0 = fdt0 >> 32; \
3197 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3198 uint32_t fsth1 = fdt1 >> 32; \
3199 uint32_t wt2; \
3200 uint32_t wth2; \
3202 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3203 wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status); \
3204 update_fcr31(env, GETPC()); \
3205 return ((uint64_t)wth2 << 32) | wt2; \
3208 FLOAT_BINOP(add)
3209 FLOAT_BINOP(sub)
3210 FLOAT_BINOP(mul)
3211 FLOAT_BINOP(div)
3212 #undef FLOAT_BINOP
3214 /* MIPS specific binary operations */
3215 uint64_t helper_float_recip2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3217 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3218 fdt2 = float64_chs(float64_sub(fdt2, float64_one, &env->active_fpu.fp_status));
3219 update_fcr31(env, GETPC());
3220 return fdt2;
3223 uint32_t helper_float_recip2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3225 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3226 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3227 update_fcr31(env, GETPC());
3228 return fst2;
3231 uint64_t helper_float_recip2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3233 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3234 uint32_t fsth0 = fdt0 >> 32;
3235 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3236 uint32_t fsth2 = fdt2 >> 32;
3238 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3239 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3240 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3241 fsth2 = float32_chs(float32_sub(fsth2, float32_one, &env->active_fpu.fp_status));
3242 update_fcr31(env, GETPC());
3243 return ((uint64_t)fsth2 << 32) | fst2;
3246 uint64_t helper_float_rsqrt2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3248 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3249 fdt2 = float64_sub(fdt2, float64_one, &env->active_fpu.fp_status);
3250 fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
3251 update_fcr31(env, GETPC());
3252 return fdt2;
3255 uint32_t helper_float_rsqrt2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3257 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3258 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3259 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3260 update_fcr31(env, GETPC());
3261 return fst2;
3264 uint64_t helper_float_rsqrt2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3266 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3267 uint32_t fsth0 = fdt0 >> 32;
3268 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3269 uint32_t fsth2 = fdt2 >> 32;
3271 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3272 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3273 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3274 fsth2 = float32_sub(fsth2, float32_one, &env->active_fpu.fp_status);
3275 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3276 fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
3277 update_fcr31(env, GETPC());
3278 return ((uint64_t)fsth2 << 32) | fst2;
3281 uint64_t helper_float_addr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3283 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3284 uint32_t fsth0 = fdt0 >> 32;
3285 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3286 uint32_t fsth1 = fdt1 >> 32;
3287 uint32_t fst2;
3288 uint32_t fsth2;
3290 fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
3291 fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
3292 update_fcr31(env, GETPC());
3293 return ((uint64_t)fsth2 << 32) | fst2;
3296 uint64_t helper_float_mulr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3298 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3299 uint32_t fsth0 = fdt0 >> 32;
3300 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3301 uint32_t fsth1 = fdt1 >> 32;
3302 uint32_t fst2;
3303 uint32_t fsth2;
3305 fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
3306 fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
3307 update_fcr31(env, GETPC());
3308 return ((uint64_t)fsth2 << 32) | fst2;
3311 #define FLOAT_MINMAX(name, bits, minmaxfunc) \
3312 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3313 uint ## bits ## _t fs, \
3314 uint ## bits ## _t ft) \
3316 uint ## bits ## _t fdret; \
3318 fdret = float ## bits ## _ ## minmaxfunc(fs, ft, \
3319 &env->active_fpu.fp_status); \
3320 update_fcr31(env, GETPC()); \
3321 return fdret; \
3324 FLOAT_MINMAX(max_s, 32, maxnum)
3325 FLOAT_MINMAX(max_d, 64, maxnum)
3326 FLOAT_MINMAX(maxa_s, 32, maxnummag)
3327 FLOAT_MINMAX(maxa_d, 64, maxnummag)
3329 FLOAT_MINMAX(min_s, 32, minnum)
3330 FLOAT_MINMAX(min_d, 64, minnum)
3331 FLOAT_MINMAX(mina_s, 32, minnummag)
3332 FLOAT_MINMAX(mina_d, 64, minnummag)
3333 #undef FLOAT_MINMAX
3335 /* ternary operations */
3336 #define UNFUSED_FMA(prefix, a, b, c, flags) \
3338 a = prefix##_mul(a, b, &env->active_fpu.fp_status); \
3339 if ((flags) & float_muladd_negate_c) { \
3340 a = prefix##_sub(a, c, &env->active_fpu.fp_status); \
3341 } else { \
3342 a = prefix##_add(a, c, &env->active_fpu.fp_status); \
3344 if ((flags) & float_muladd_negate_result) { \
3345 a = prefix##_chs(a); \
3349 /* FMA based operations */
3350 #define FLOAT_FMA(name, type) \
3351 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
3352 uint64_t fdt0, uint64_t fdt1, \
3353 uint64_t fdt2) \
3355 UNFUSED_FMA(float64, fdt0, fdt1, fdt2, type); \
3356 update_fcr31(env, GETPC()); \
3357 return fdt0; \
3360 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
3361 uint32_t fst0, uint32_t fst1, \
3362 uint32_t fst2) \
3364 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
3365 update_fcr31(env, GETPC()); \
3366 return fst0; \
3369 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
3370 uint64_t fdt0, uint64_t fdt1, \
3371 uint64_t fdt2) \
3373 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3374 uint32_t fsth0 = fdt0 >> 32; \
3375 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3376 uint32_t fsth1 = fdt1 >> 32; \
3377 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
3378 uint32_t fsth2 = fdt2 >> 32; \
3380 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
3381 UNFUSED_FMA(float32, fsth0, fsth1, fsth2, type); \
3382 update_fcr31(env, GETPC()); \
3383 return ((uint64_t)fsth0 << 32) | fst0; \
3385 FLOAT_FMA(madd, 0)
3386 FLOAT_FMA(msub, float_muladd_negate_c)
3387 FLOAT_FMA(nmadd, float_muladd_negate_result)
3388 FLOAT_FMA(nmsub, float_muladd_negate_result | float_muladd_negate_c)
3389 #undef FLOAT_FMA
3391 #define FLOAT_FMADDSUB(name, bits, muladd_arg) \
3392 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3393 uint ## bits ## _t fs, \
3394 uint ## bits ## _t ft, \
3395 uint ## bits ## _t fd) \
3397 uint ## bits ## _t fdret; \
3399 fdret = float ## bits ## _muladd(fs, ft, fd, muladd_arg, \
3400 &env->active_fpu.fp_status); \
3401 update_fcr31(env, GETPC()); \
3402 return fdret; \
3405 FLOAT_FMADDSUB(maddf_s, 32, 0)
3406 FLOAT_FMADDSUB(maddf_d, 64, 0)
3407 FLOAT_FMADDSUB(msubf_s, 32, float_muladd_negate_product)
3408 FLOAT_FMADDSUB(msubf_d, 64, float_muladd_negate_product)
3409 #undef FLOAT_FMADDSUB
3411 /* compare operations */
3412 #define FOP_COND_D(op, cond) \
3413 void helper_cmp_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3414 uint64_t fdt1, int cc) \
3416 int c; \
3417 c = cond; \
3418 update_fcr31(env, GETPC()); \
3419 if (c) \
3420 SET_FP_COND(cc, env->active_fpu); \
3421 else \
3422 CLEAR_FP_COND(cc, env->active_fpu); \
3424 void helper_cmpabs_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3425 uint64_t fdt1, int cc) \
3427 int c; \
3428 fdt0 = float64_abs(fdt0); \
3429 fdt1 = float64_abs(fdt1); \
3430 c = cond; \
3431 update_fcr31(env, GETPC()); \
3432 if (c) \
3433 SET_FP_COND(cc, env->active_fpu); \
3434 else \
3435 CLEAR_FP_COND(cc, env->active_fpu); \
3438 /* NOTE: the comma operator will make "cond" to eval to false,
3439 * but float64_unordered_quiet() is still called. */
3440 FOP_COND_D(f, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3441 FOP_COND_D(un, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status))
3442 FOP_COND_D(eq, float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3443 FOP_COND_D(ueq, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3444 FOP_COND_D(olt, float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3445 FOP_COND_D(ult, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3446 FOP_COND_D(ole, float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3447 FOP_COND_D(ule, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3448 /* NOTE: the comma operator will make "cond" to eval to false,
3449 * but float64_unordered() is still called. */
3450 FOP_COND_D(sf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3451 FOP_COND_D(ngle,float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status))
3452 FOP_COND_D(seq, float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3453 FOP_COND_D(ngl, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3454 FOP_COND_D(lt, float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3455 FOP_COND_D(nge, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3456 FOP_COND_D(le, float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3457 FOP_COND_D(ngt, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3459 #define FOP_COND_S(op, cond) \
3460 void helper_cmp_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3461 uint32_t fst1, int cc) \
3463 int c; \
3464 c = cond; \
3465 update_fcr31(env, GETPC()); \
3466 if (c) \
3467 SET_FP_COND(cc, env->active_fpu); \
3468 else \
3469 CLEAR_FP_COND(cc, env->active_fpu); \
3471 void helper_cmpabs_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3472 uint32_t fst1, int cc) \
3474 int c; \
3475 fst0 = float32_abs(fst0); \
3476 fst1 = float32_abs(fst1); \
3477 c = cond; \
3478 update_fcr31(env, GETPC()); \
3479 if (c) \
3480 SET_FP_COND(cc, env->active_fpu); \
3481 else \
3482 CLEAR_FP_COND(cc, env->active_fpu); \
3485 /* NOTE: the comma operator will make "cond" to eval to false,
3486 * but float32_unordered_quiet() is still called. */
3487 FOP_COND_S(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3488 FOP_COND_S(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status))
3489 FOP_COND_S(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3490 FOP_COND_S(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3491 FOP_COND_S(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3492 FOP_COND_S(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3493 FOP_COND_S(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3494 FOP_COND_S(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3495 /* NOTE: the comma operator will make "cond" to eval to false,
3496 * but float32_unordered() is still called. */
3497 FOP_COND_S(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3498 FOP_COND_S(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status))
3499 FOP_COND_S(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3500 FOP_COND_S(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3501 FOP_COND_S(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3502 FOP_COND_S(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3503 FOP_COND_S(le, float32_le(fst0, fst1, &env->active_fpu.fp_status))
3504 FOP_COND_S(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
3506 #define FOP_COND_PS(op, condl, condh) \
3507 void helper_cmp_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3508 uint64_t fdt1, int cc) \
3510 uint32_t fst0, fsth0, fst1, fsth1; \
3511 int ch, cl; \
3512 fst0 = fdt0 & 0XFFFFFFFF; \
3513 fsth0 = fdt0 >> 32; \
3514 fst1 = fdt1 & 0XFFFFFFFF; \
3515 fsth1 = fdt1 >> 32; \
3516 cl = condl; \
3517 ch = condh; \
3518 update_fcr31(env, GETPC()); \
3519 if (cl) \
3520 SET_FP_COND(cc, env->active_fpu); \
3521 else \
3522 CLEAR_FP_COND(cc, env->active_fpu); \
3523 if (ch) \
3524 SET_FP_COND(cc + 1, env->active_fpu); \
3525 else \
3526 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3528 void helper_cmpabs_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3529 uint64_t fdt1, int cc) \
3531 uint32_t fst0, fsth0, fst1, fsth1; \
3532 int ch, cl; \
3533 fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
3534 fsth0 = float32_abs(fdt0 >> 32); \
3535 fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
3536 fsth1 = float32_abs(fdt1 >> 32); \
3537 cl = condl; \
3538 ch = condh; \
3539 update_fcr31(env, GETPC()); \
3540 if (cl) \
3541 SET_FP_COND(cc, env->active_fpu); \
3542 else \
3543 CLEAR_FP_COND(cc, env->active_fpu); \
3544 if (ch) \
3545 SET_FP_COND(cc + 1, env->active_fpu); \
3546 else \
3547 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3550 /* NOTE: the comma operator will make "cond" to eval to false,
3551 * but float32_unordered_quiet() is still called. */
3552 FOP_COND_PS(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0),
3553 (float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3554 FOP_COND_PS(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status),
3555 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status))
3556 FOP_COND_PS(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3557 float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3558 FOP_COND_PS(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3559 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3560 FOP_COND_PS(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3561 float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3562 FOP_COND_PS(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3563 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3564 FOP_COND_PS(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3565 float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3566 FOP_COND_PS(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3567 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3568 /* NOTE: the comma operator will make "cond" to eval to false,
3569 * but float32_unordered() is still called. */
3570 FOP_COND_PS(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0),
3571 (float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3572 FOP_COND_PS(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status),
3573 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status))
3574 FOP_COND_PS(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3575 float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3576 FOP_COND_PS(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3577 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3578 FOP_COND_PS(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3579 float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3580 FOP_COND_PS(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3581 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3582 FOP_COND_PS(le, float32_le(fst0, fst1, &env->active_fpu.fp_status),
3583 float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3584 FOP_COND_PS(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
3585 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3587 /* R6 compare operations */
3588 #define FOP_CONDN_D(op, cond) \
3589 uint64_t helper_r6_cmp_d_ ## op(CPUMIPSState * env, uint64_t fdt0, \
3590 uint64_t fdt1) \
3592 uint64_t c; \
3593 c = cond; \
3594 update_fcr31(env, GETPC()); \
3595 if (c) { \
3596 return -1; \
3597 } else { \
3598 return 0; \
3602 /* NOTE: the comma operator will make "cond" to eval to false,
3603 * but float64_unordered_quiet() is still called. */
3604 FOP_CONDN_D(af, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3605 FOP_CONDN_D(un, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)))
3606 FOP_CONDN_D(eq, (float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3607 FOP_CONDN_D(ueq, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3608 || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3609 FOP_CONDN_D(lt, (float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3610 FOP_CONDN_D(ult, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3611 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3612 FOP_CONDN_D(le, (float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3613 FOP_CONDN_D(ule, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3614 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3615 /* NOTE: the comma operator will make "cond" to eval to false,
3616 * but float64_unordered() is still called. */
3617 FOP_CONDN_D(saf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3618 FOP_CONDN_D(sun, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)))
3619 FOP_CONDN_D(seq, (float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
3620 FOP_CONDN_D(sueq, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3621 || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
3622 FOP_CONDN_D(slt, (float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3623 FOP_CONDN_D(sult, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3624 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3625 FOP_CONDN_D(sle, (float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3626 FOP_CONDN_D(sule, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3627 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3628 FOP_CONDN_D(or, (float64_le_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3629 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3630 FOP_CONDN_D(une, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3631 || float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3632 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3633 FOP_CONDN_D(ne, (float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3634 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3635 FOP_CONDN_D(sor, (float64_le(fdt1, fdt0, &env->active_fpu.fp_status)
3636 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3637 FOP_CONDN_D(sune, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3638 || float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
3639 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3640 FOP_CONDN_D(sne, (float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
3641 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3643 #define FOP_CONDN_S(op, cond) \
3644 uint32_t helper_r6_cmp_s_ ## op(CPUMIPSState * env, uint32_t fst0, \
3645 uint32_t fst1) \
3647 uint64_t c; \
3648 c = cond; \
3649 update_fcr31(env, GETPC()); \
3650 if (c) { \
3651 return -1; \
3652 } else { \
3653 return 0; \
3657 /* NOTE: the comma operator will make "cond" to eval to false,
3658 * but float32_unordered_quiet() is still called. */
3659 FOP_CONDN_S(af, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3660 FOP_CONDN_S(un, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)))
3661 FOP_CONDN_S(eq, (float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3662 FOP_CONDN_S(ueq, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3663 || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3664 FOP_CONDN_S(lt, (float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3665 FOP_CONDN_S(ult, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3666 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3667 FOP_CONDN_S(le, (float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3668 FOP_CONDN_S(ule, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3669 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3670 /* NOTE: the comma operator will make "cond" to eval to false,
3671 * but float32_unordered() is still called. */
3672 FOP_CONDN_S(saf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3673 FOP_CONDN_S(sun, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)))
3674 FOP_CONDN_S(seq, (float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
3675 FOP_CONDN_S(sueq, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3676 || float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
3677 FOP_CONDN_S(slt, (float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3678 FOP_CONDN_S(sult, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3679 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3680 FOP_CONDN_S(sle, (float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3681 FOP_CONDN_S(sule, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3682 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3683 FOP_CONDN_S(or, (float32_le_quiet(fst1, fst0, &env->active_fpu.fp_status)
3684 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3685 FOP_CONDN_S(une, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3686 || float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
3687 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3688 FOP_CONDN_S(ne, (float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
3689 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3690 FOP_CONDN_S(sor, (float32_le(fst1, fst0, &env->active_fpu.fp_status)
3691 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3692 FOP_CONDN_S(sune, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3693 || float32_lt(fst1, fst0, &env->active_fpu.fp_status)
3694 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3695 FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status)
3696 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3698 /* MSA */
3699 /* Data format min and max values */
3700 #define DF_BITS(df) (1 << ((df) + 3))
3702 /* Element-by-element access macros */
3703 #define DF_ELEMENTS(df) (MSA_WRLEN / DF_BITS(df))
3705 #if !defined(CONFIG_USER_ONLY)
3706 #define MEMOP_IDX(DF) \
3707 TCGMemOpIdx oi = make_memop_idx(MO_TE | DF | MO_UNALN, \
3708 cpu_mmu_index(env, false));
3709 #else
3710 #define MEMOP_IDX(DF)
3711 #endif
3713 #define MSA_LD_DF(DF, TYPE, LD_INSN, ...) \
3714 void helper_msa_ld_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
3715 target_ulong addr) \
3717 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
3718 wr_t wx; \
3719 int i; \
3720 MEMOP_IDX(DF) \
3721 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
3722 wx.TYPE[i] = LD_INSN(env, addr + (i << DF), ##__VA_ARGS__); \
3724 memcpy(pwd, &wx, sizeof(wr_t)); \
3727 #if !defined(CONFIG_USER_ONLY)
3728 MSA_LD_DF(DF_BYTE, b, helper_ret_ldub_mmu, oi, GETRA())
3729 MSA_LD_DF(DF_HALF, h, helper_ret_lduw_mmu, oi, GETRA())
3730 MSA_LD_DF(DF_WORD, w, helper_ret_ldul_mmu, oi, GETRA())
3731 MSA_LD_DF(DF_DOUBLE, d, helper_ret_ldq_mmu, oi, GETRA())
3732 #else
3733 MSA_LD_DF(DF_BYTE, b, cpu_ldub_data)
3734 MSA_LD_DF(DF_HALF, h, cpu_lduw_data)
3735 MSA_LD_DF(DF_WORD, w, cpu_ldl_data)
3736 MSA_LD_DF(DF_DOUBLE, d, cpu_ldq_data)
3737 #endif
3739 #define MSA_PAGESPAN(x) \
3740 ((((x) & ~TARGET_PAGE_MASK) + MSA_WRLEN/8 - 1) >= TARGET_PAGE_SIZE)
3742 static inline void ensure_writable_pages(CPUMIPSState *env,
3743 target_ulong addr,
3744 int mmu_idx,
3745 uintptr_t retaddr)
3747 #if !defined(CONFIG_USER_ONLY)
3748 target_ulong page_addr;
3749 if (unlikely(MSA_PAGESPAN(addr))) {
3750 /* first page */
3751 probe_write(env, addr, mmu_idx, retaddr);
3752 /* second page */
3753 page_addr = (addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
3754 probe_write(env, page_addr, mmu_idx, retaddr);
3756 #endif
3759 #define MSA_ST_DF(DF, TYPE, ST_INSN, ...) \
3760 void helper_msa_st_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
3761 target_ulong addr) \
3763 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
3764 int mmu_idx = cpu_mmu_index(env, false); \
3765 int i; \
3766 MEMOP_IDX(DF) \
3767 ensure_writable_pages(env, addr, mmu_idx, GETRA()); \
3768 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
3769 ST_INSN(env, addr + (i << DF), pwd->TYPE[i], ##__VA_ARGS__); \
3773 #if !defined(CONFIG_USER_ONLY)
3774 MSA_ST_DF(DF_BYTE, b, helper_ret_stb_mmu, oi, GETRA())
3775 MSA_ST_DF(DF_HALF, h, helper_ret_stw_mmu, oi, GETRA())
3776 MSA_ST_DF(DF_WORD, w, helper_ret_stl_mmu, oi, GETRA())
3777 MSA_ST_DF(DF_DOUBLE, d, helper_ret_stq_mmu, oi, GETRA())
3778 #else
3779 MSA_ST_DF(DF_BYTE, b, cpu_stb_data)
3780 MSA_ST_DF(DF_HALF, h, cpu_stw_data)
3781 MSA_ST_DF(DF_WORD, w, cpu_stl_data)
3782 MSA_ST_DF(DF_DOUBLE, d, cpu_stq_data)
3783 #endif