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/>.
21 #include "qemu/host-utils.h"
25 #if !defined(CONFIG_USER_ONLY)
26 #include "exec/softmmu_exec.h"
27 #endif /* !defined(CONFIG_USER_ONLY) */
29 #ifndef CONFIG_USER_ONLY
30 static inline void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global);
33 /*****************************************************************************/
34 /* Exceptions processing helpers */
36 static inline void QEMU_NORETURN do_raise_exception_err(CPUMIPSState *env,
41 if (exception < EXCP_SC) {
42 qemu_log("%s: %d %d\n", __func__, exception, error_code);
44 env->exception_index = exception;
45 env->error_code = error_code;
48 /* now we have a real cpu fault */
49 cpu_restore_state(env, pc);
55 static inline void QEMU_NORETURN do_raise_exception(CPUMIPSState *env,
59 do_raise_exception_err(env, exception, 0, pc);
62 void helper_raise_exception_err(CPUMIPSState *env, uint32_t exception,
65 do_raise_exception_err(env, exception, error_code, 0);
68 void helper_raise_exception(CPUMIPSState *env, uint32_t exception)
70 do_raise_exception(env, exception, 0);
73 #if defined(CONFIG_USER_ONLY)
74 #define HELPER_LD(name, insn, type) \
75 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
78 return (type) insn##_raw(addr); \
81 #define HELPER_LD(name, insn, type) \
82 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
87 case 0: return (type) cpu_##insn##_kernel(env, addr); break; \
88 case 1: return (type) cpu_##insn##_super(env, addr); break; \
90 case 2: return (type) cpu_##insn##_user(env, addr); break; \
94 HELPER_LD(lbu, ldub, uint8_t)
95 HELPER_LD(lw, ldl, int32_t)
97 HELPER_LD(ld, ldq, int64_t)
101 #if defined(CONFIG_USER_ONLY)
102 #define HELPER_ST(name, insn, type) \
103 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
104 type val, int mem_idx) \
106 insn##_raw(addr, val); \
109 #define HELPER_ST(name, insn, type) \
110 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
111 type val, int mem_idx) \
115 case 0: cpu_##insn##_kernel(env, addr, val); break; \
116 case 1: cpu_##insn##_super(env, addr, val); break; \
118 case 2: cpu_##insn##_user(env, addr, val); break; \
122 HELPER_ST(sb, stb, uint8_t)
123 HELPER_ST(sw, stl, uint32_t)
125 HELPER_ST(sd, stq, uint64_t)
129 target_ulong helper_clo (target_ulong arg1)
134 target_ulong helper_clz (target_ulong arg1)
139 #if defined(TARGET_MIPS64)
140 target_ulong helper_dclo (target_ulong arg1)
145 target_ulong helper_dclz (target_ulong arg1)
149 #endif /* TARGET_MIPS64 */
151 /* 64 bits arithmetic for 32 bits hosts */
152 static inline uint64_t get_HILO(CPUMIPSState *env)
154 return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
157 static inline target_ulong set_HIT0_LO(CPUMIPSState *env, uint64_t HILO)
160 env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
161 tmp = env->active_tc.HI[0] = (int32_t)(HILO >> 32);
165 static inline target_ulong set_HI_LOT0(CPUMIPSState *env, uint64_t HILO)
167 target_ulong tmp = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
168 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
172 /* Multiplication variants of the vr54xx. */
173 target_ulong helper_muls(CPUMIPSState *env, target_ulong arg1,
176 return set_HI_LOT0(env, 0 - ((int64_t)(int32_t)arg1 *
177 (int64_t)(int32_t)arg2));
180 target_ulong helper_mulsu(CPUMIPSState *env, target_ulong arg1,
183 return set_HI_LOT0(env, 0 - (uint64_t)(uint32_t)arg1 *
184 (uint64_t)(uint32_t)arg2);
187 target_ulong helper_macc(CPUMIPSState *env, target_ulong arg1,
190 return set_HI_LOT0(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
191 (int64_t)(int32_t)arg2);
194 target_ulong helper_macchi(CPUMIPSState *env, target_ulong arg1,
197 return set_HIT0_LO(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
198 (int64_t)(int32_t)arg2);
201 target_ulong helper_maccu(CPUMIPSState *env, target_ulong arg1,
204 return set_HI_LOT0(env, (uint64_t)get_HILO(env) +
205 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
208 target_ulong helper_macchiu(CPUMIPSState *env, target_ulong arg1,
211 return set_HIT0_LO(env, (uint64_t)get_HILO(env) +
212 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
215 target_ulong helper_msac(CPUMIPSState *env, target_ulong arg1,
218 return set_HI_LOT0(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
219 (int64_t)(int32_t)arg2);
222 target_ulong helper_msachi(CPUMIPSState *env, target_ulong arg1,
225 return set_HIT0_LO(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
226 (int64_t)(int32_t)arg2);
229 target_ulong helper_msacu(CPUMIPSState *env, target_ulong arg1,
232 return set_HI_LOT0(env, (uint64_t)get_HILO(env) -
233 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
236 target_ulong helper_msachiu(CPUMIPSState *env, target_ulong arg1,
239 return set_HIT0_LO(env, (uint64_t)get_HILO(env) -
240 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
243 target_ulong helper_mulhi(CPUMIPSState *env, target_ulong arg1,
246 return set_HIT0_LO(env, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
249 target_ulong helper_mulhiu(CPUMIPSState *env, target_ulong arg1,
252 return set_HIT0_LO(env, (uint64_t)(uint32_t)arg1 *
253 (uint64_t)(uint32_t)arg2);
256 target_ulong helper_mulshi(CPUMIPSState *env, target_ulong arg1,
259 return set_HIT0_LO(env, 0 - (int64_t)(int32_t)arg1 *
260 (int64_t)(int32_t)arg2);
263 target_ulong helper_mulshiu(CPUMIPSState *env, target_ulong arg1,
266 return set_HIT0_LO(env, 0 - (uint64_t)(uint32_t)arg1 *
267 (uint64_t)(uint32_t)arg2);
271 void helper_dmult(CPUMIPSState *env, target_ulong arg1, target_ulong arg2)
273 muls64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
276 void helper_dmultu(CPUMIPSState *env, target_ulong arg1, target_ulong arg2)
278 mulu64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
282 #ifndef CONFIG_USER_ONLY
284 static inline hwaddr do_translate_address(CPUMIPSState *env,
285 target_ulong address,
290 lladdr = cpu_mips_translate_address(env, address, rw);
292 if (lladdr == -1LL) {
299 #define HELPER_LD_ATOMIC(name, insn) \
300 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg, int mem_idx) \
302 env->lladdr = do_translate_address(env, arg, 0); \
303 env->llval = do_##insn(env, arg, mem_idx); \
306 HELPER_LD_ATOMIC(ll, lw)
308 HELPER_LD_ATOMIC(lld, ld)
310 #undef HELPER_LD_ATOMIC
312 #define HELPER_ST_ATOMIC(name, ld_insn, st_insn, almask) \
313 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg1, \
314 target_ulong arg2, int mem_idx) \
318 if (arg2 & almask) { \
319 env->CP0_BadVAddr = arg2; \
320 helper_raise_exception(env, EXCP_AdES); \
322 if (do_translate_address(env, arg2, 1) == env->lladdr) { \
323 tmp = do_##ld_insn(env, arg2, mem_idx); \
324 if (tmp == env->llval) { \
325 do_##st_insn(env, arg2, arg1, mem_idx); \
331 HELPER_ST_ATOMIC(sc, lw, sw, 0x3)
333 HELPER_ST_ATOMIC(scd, ld, sd, 0x7)
335 #undef HELPER_ST_ATOMIC
338 #ifdef TARGET_WORDS_BIGENDIAN
339 #define GET_LMASK(v) ((v) & 3)
340 #define GET_OFFSET(addr, offset) (addr + (offset))
342 #define GET_LMASK(v) (((v) & 3) ^ 3)
343 #define GET_OFFSET(addr, offset) (addr - (offset))
346 void helper_swl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
349 do_sb(env, arg2, (uint8_t)(arg1 >> 24), mem_idx);
351 if (GET_LMASK(arg2) <= 2)
352 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16), mem_idx);
354 if (GET_LMASK(arg2) <= 1)
355 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8), mem_idx);
357 if (GET_LMASK(arg2) == 0)
358 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)arg1, mem_idx);
361 void helper_swr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
364 do_sb(env, arg2, (uint8_t)arg1, mem_idx);
366 if (GET_LMASK(arg2) >= 1)
367 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
369 if (GET_LMASK(arg2) >= 2)
370 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
372 if (GET_LMASK(arg2) == 3)
373 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
376 #if defined(TARGET_MIPS64)
377 /* "half" load and stores. We must do the memory access inline,
378 or fault handling won't work. */
380 #ifdef TARGET_WORDS_BIGENDIAN
381 #define GET_LMASK64(v) ((v) & 7)
383 #define GET_LMASK64(v) (((v) & 7) ^ 7)
386 void helper_sdl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
389 do_sb(env, arg2, (uint8_t)(arg1 >> 56), mem_idx);
391 if (GET_LMASK64(arg2) <= 6)
392 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48), mem_idx);
394 if (GET_LMASK64(arg2) <= 5)
395 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40), mem_idx);
397 if (GET_LMASK64(arg2) <= 4)
398 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32), mem_idx);
400 if (GET_LMASK64(arg2) <= 3)
401 do_sb(env, GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24), mem_idx);
403 if (GET_LMASK64(arg2) <= 2)
404 do_sb(env, GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16), mem_idx);
406 if (GET_LMASK64(arg2) <= 1)
407 do_sb(env, GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8), mem_idx);
409 if (GET_LMASK64(arg2) <= 0)
410 do_sb(env, GET_OFFSET(arg2, 7), (uint8_t)arg1, mem_idx);
413 void helper_sdr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
416 do_sb(env, arg2, (uint8_t)arg1, mem_idx);
418 if (GET_LMASK64(arg2) >= 1)
419 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
421 if (GET_LMASK64(arg2) >= 2)
422 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
424 if (GET_LMASK64(arg2) >= 3)
425 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
427 if (GET_LMASK64(arg2) >= 4)
428 do_sb(env, GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32), mem_idx);
430 if (GET_LMASK64(arg2) >= 5)
431 do_sb(env, GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40), mem_idx);
433 if (GET_LMASK64(arg2) >= 6)
434 do_sb(env, GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48), mem_idx);
436 if (GET_LMASK64(arg2) == 7)
437 do_sb(env, GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56), mem_idx);
439 #endif /* TARGET_MIPS64 */
441 static const int multiple_regs[] = { 16, 17, 18, 19, 20, 21, 22, 23, 30 };
443 void helper_lwm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
446 target_ulong base_reglist = reglist & 0xf;
447 target_ulong do_r31 = reglist & 0x10;
449 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
452 for (i = 0; i < base_reglist; i++) {
453 env->active_tc.gpr[multiple_regs[i]] =
454 (target_long)do_lw(env, addr, mem_idx);
460 env->active_tc.gpr[31] = (target_long)do_lw(env, addr, mem_idx);
464 void helper_swm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
467 target_ulong base_reglist = reglist & 0xf;
468 target_ulong do_r31 = reglist & 0x10;
470 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
473 for (i = 0; i < base_reglist; i++) {
474 do_sw(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx);
480 do_sw(env, addr, env->active_tc.gpr[31], mem_idx);
484 #if defined(TARGET_MIPS64)
485 void helper_ldm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
488 target_ulong base_reglist = reglist & 0xf;
489 target_ulong do_r31 = reglist & 0x10;
491 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
494 for (i = 0; i < base_reglist; i++) {
495 env->active_tc.gpr[multiple_regs[i]] = do_ld(env, addr, mem_idx);
501 env->active_tc.gpr[31] = do_ld(env, addr, mem_idx);
505 void helper_sdm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
508 target_ulong base_reglist = reglist & 0xf;
509 target_ulong do_r31 = reglist & 0x10;
511 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
514 for (i = 0; i < base_reglist; i++) {
515 do_sd(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx);
521 do_sd(env, addr, env->active_tc.gpr[31], mem_idx);
526 #ifndef CONFIG_USER_ONLY
528 static bool mips_vpe_is_wfi(MIPSCPU *c)
530 CPUMIPSState *env = &c->env;
532 /* If the VPE is halted but otherwise active, it means it's waiting for
534 return env->halted && mips_vpe_active(env);
537 static inline void mips_vpe_wake(CPUMIPSState *c)
539 /* Dont set ->halted = 0 directly, let it be done via cpu_has_work
540 because there might be other conditions that state that c should
542 cpu_interrupt(c, CPU_INTERRUPT_WAKE);
545 static inline void mips_vpe_sleep(MIPSCPU *cpu)
547 CPUMIPSState *c = &cpu->env;
549 /* The VPE was shut off, really go to bed.
550 Reset any old _WAKE requests. */
552 cpu_reset_interrupt(c, CPU_INTERRUPT_WAKE);
555 static inline void mips_tc_wake(MIPSCPU *cpu, int tc)
557 CPUMIPSState *c = &cpu->env;
559 /* FIXME: TC reschedule. */
560 if (mips_vpe_active(c) && !mips_vpe_is_wfi(cpu)) {
565 static inline void mips_tc_sleep(MIPSCPU *cpu, int tc)
567 CPUMIPSState *c = &cpu->env;
569 /* FIXME: TC reschedule. */
570 if (!mips_vpe_active(c)) {
577 * @env: CPU from which mapping is performed.
578 * @tc: Should point to an int with the value of the global TC index.
580 * This function will transform @tc into a local index within the
581 * returned #CPUMIPSState.
583 /* FIXME: This code assumes that all VPEs have the same number of TCs,
584 which depends on runtime setup. Can probably be fixed by
585 walking the list of CPUMIPSStates. */
586 static CPUMIPSState *mips_cpu_map_tc(CPUMIPSState *env, int *tc)
593 if (!(env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))) {
594 /* Not allowed to address other CPUs. */
595 *tc = env->current_tc;
599 cs = CPU(mips_env_get_cpu(env));
600 vpe_idx = tc_idx / cs->nr_threads;
601 *tc = tc_idx % cs->nr_threads;
602 other = qemu_get_cpu(vpe_idx);
603 return other ? other : env;
606 /* The per VPE CP0_Status register shares some fields with the per TC
607 CP0_TCStatus registers. These fields are wired to the same registers,
608 so changes to either of them should be reflected on both registers.
610 Also, EntryHi shares the bottom 8 bit ASID with TCStauts.
612 These helper call synchronizes the regs for a given cpu. */
614 /* Called for updates to CP0_Status. */
615 static void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu, int tc)
617 int32_t tcstatus, *tcst;
618 uint32_t v = cpu->CP0_Status;
619 uint32_t cu, mx, asid, ksu;
620 uint32_t mask = ((1 << CP0TCSt_TCU3)
621 | (1 << CP0TCSt_TCU2)
622 | (1 << CP0TCSt_TCU1)
623 | (1 << CP0TCSt_TCU0)
625 | (3 << CP0TCSt_TKSU)
626 | (0xff << CP0TCSt_TASID));
628 cu = (v >> CP0St_CU0) & 0xf;
629 mx = (v >> CP0St_MX) & 0x1;
630 ksu = (v >> CP0St_KSU) & 0x3;
631 asid = env->CP0_EntryHi & 0xff;
633 tcstatus = cu << CP0TCSt_TCU0;
634 tcstatus |= mx << CP0TCSt_TMX;
635 tcstatus |= ksu << CP0TCSt_TKSU;
638 if (tc == cpu->current_tc) {
639 tcst = &cpu->active_tc.CP0_TCStatus;
641 tcst = &cpu->tcs[tc].CP0_TCStatus;
649 /* Called for updates to CP0_TCStatus. */
650 static void sync_c0_tcstatus(CPUMIPSState *cpu, int tc,
654 uint32_t tcu, tmx, tasid, tksu;
655 uint32_t mask = ((1 << CP0St_CU3)
662 tcu = (v >> CP0TCSt_TCU0) & 0xf;
663 tmx = (v >> CP0TCSt_TMX) & 0x1;
665 tksu = (v >> CP0TCSt_TKSU) & 0x3;
667 status = tcu << CP0St_CU0;
668 status |= tmx << CP0St_MX;
669 status |= tksu << CP0St_KSU;
671 cpu->CP0_Status &= ~mask;
672 cpu->CP0_Status |= status;
674 /* Sync the TASID with EntryHi. */
675 cpu->CP0_EntryHi &= ~0xff;
676 cpu->CP0_EntryHi = tasid;
681 /* Called for updates to CP0_EntryHi. */
682 static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
685 uint32_t asid, v = cpu->CP0_EntryHi;
689 if (tc == cpu->current_tc) {
690 tcst = &cpu->active_tc.CP0_TCStatus;
692 tcst = &cpu->tcs[tc].CP0_TCStatus;
700 target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
702 return env->mvp->CP0_MVPControl;
705 target_ulong helper_mfc0_mvpconf0(CPUMIPSState *env)
707 return env->mvp->CP0_MVPConf0;
710 target_ulong helper_mfc0_mvpconf1(CPUMIPSState *env)
712 return env->mvp->CP0_MVPConf1;
715 target_ulong helper_mfc0_random(CPUMIPSState *env)
717 return (int32_t)cpu_mips_get_random(env);
720 target_ulong helper_mfc0_tcstatus(CPUMIPSState *env)
722 return env->active_tc.CP0_TCStatus;
725 target_ulong helper_mftc0_tcstatus(CPUMIPSState *env)
727 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
728 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
730 if (other_tc == other->current_tc)
731 return other->active_tc.CP0_TCStatus;
733 return other->tcs[other_tc].CP0_TCStatus;
736 target_ulong helper_mfc0_tcbind(CPUMIPSState *env)
738 return env->active_tc.CP0_TCBind;
741 target_ulong helper_mftc0_tcbind(CPUMIPSState *env)
743 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
744 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
746 if (other_tc == other->current_tc)
747 return other->active_tc.CP0_TCBind;
749 return other->tcs[other_tc].CP0_TCBind;
752 target_ulong helper_mfc0_tcrestart(CPUMIPSState *env)
754 return env->active_tc.PC;
757 target_ulong helper_mftc0_tcrestart(CPUMIPSState *env)
759 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
760 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
762 if (other_tc == other->current_tc)
763 return other->active_tc.PC;
765 return other->tcs[other_tc].PC;
768 target_ulong helper_mfc0_tchalt(CPUMIPSState *env)
770 return env->active_tc.CP0_TCHalt;
773 target_ulong helper_mftc0_tchalt(CPUMIPSState *env)
775 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
776 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
778 if (other_tc == other->current_tc)
779 return other->active_tc.CP0_TCHalt;
781 return other->tcs[other_tc].CP0_TCHalt;
784 target_ulong helper_mfc0_tccontext(CPUMIPSState *env)
786 return env->active_tc.CP0_TCContext;
789 target_ulong helper_mftc0_tccontext(CPUMIPSState *env)
791 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
792 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
794 if (other_tc == other->current_tc)
795 return other->active_tc.CP0_TCContext;
797 return other->tcs[other_tc].CP0_TCContext;
800 target_ulong helper_mfc0_tcschedule(CPUMIPSState *env)
802 return env->active_tc.CP0_TCSchedule;
805 target_ulong helper_mftc0_tcschedule(CPUMIPSState *env)
807 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
808 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
810 if (other_tc == other->current_tc)
811 return other->active_tc.CP0_TCSchedule;
813 return other->tcs[other_tc].CP0_TCSchedule;
816 target_ulong helper_mfc0_tcschefback(CPUMIPSState *env)
818 return env->active_tc.CP0_TCScheFBack;
821 target_ulong helper_mftc0_tcschefback(CPUMIPSState *env)
823 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
824 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
826 if (other_tc == other->current_tc)
827 return other->active_tc.CP0_TCScheFBack;
829 return other->tcs[other_tc].CP0_TCScheFBack;
832 target_ulong helper_mfc0_count(CPUMIPSState *env)
834 return (int32_t)cpu_mips_get_count(env);
837 target_ulong helper_mftc0_entryhi(CPUMIPSState *env)
839 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
840 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
842 return other->CP0_EntryHi;
845 target_ulong helper_mftc0_cause(CPUMIPSState *env)
847 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
849 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
851 if (other_tc == other->current_tc) {
852 tccause = other->CP0_Cause;
854 tccause = other->CP0_Cause;
860 target_ulong helper_mftc0_status(CPUMIPSState *env)
862 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
863 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
865 return other->CP0_Status;
868 target_ulong helper_mfc0_lladdr(CPUMIPSState *env)
870 return (int32_t)(env->lladdr >> env->CP0_LLAddr_shift);
873 target_ulong helper_mfc0_watchlo(CPUMIPSState *env, uint32_t sel)
875 return (int32_t)env->CP0_WatchLo[sel];
878 target_ulong helper_mfc0_watchhi(CPUMIPSState *env, uint32_t sel)
880 return env->CP0_WatchHi[sel];
883 target_ulong helper_mfc0_debug(CPUMIPSState *env)
885 target_ulong t0 = env->CP0_Debug;
886 if (env->hflags & MIPS_HFLAG_DM)
892 target_ulong helper_mftc0_debug(CPUMIPSState *env)
894 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
896 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
898 if (other_tc == other->current_tc)
899 tcstatus = other->active_tc.CP0_Debug_tcstatus;
901 tcstatus = other->tcs[other_tc].CP0_Debug_tcstatus;
903 /* XXX: Might be wrong, check with EJTAG spec. */
904 return (other->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
905 (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
908 #if defined(TARGET_MIPS64)
909 target_ulong helper_dmfc0_tcrestart(CPUMIPSState *env)
911 return env->active_tc.PC;
914 target_ulong helper_dmfc0_tchalt(CPUMIPSState *env)
916 return env->active_tc.CP0_TCHalt;
919 target_ulong helper_dmfc0_tccontext(CPUMIPSState *env)
921 return env->active_tc.CP0_TCContext;
924 target_ulong helper_dmfc0_tcschedule(CPUMIPSState *env)
926 return env->active_tc.CP0_TCSchedule;
929 target_ulong helper_dmfc0_tcschefback(CPUMIPSState *env)
931 return env->active_tc.CP0_TCScheFBack;
934 target_ulong helper_dmfc0_lladdr(CPUMIPSState *env)
936 return env->lladdr >> env->CP0_LLAddr_shift;
939 target_ulong helper_dmfc0_watchlo(CPUMIPSState *env, uint32_t sel)
941 return env->CP0_WatchLo[sel];
943 #endif /* TARGET_MIPS64 */
945 void helper_mtc0_index(CPUMIPSState *env, target_ulong arg1)
948 unsigned int tmp = env->tlb->nb_tlb;
954 env->CP0_Index = (env->CP0_Index & 0x80000000) | (arg1 & (num - 1));
957 void helper_mtc0_mvpcontrol(CPUMIPSState *env, target_ulong arg1)
962 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
963 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
965 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
966 mask |= (1 << CP0MVPCo_STLB);
967 newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
969 // TODO: Enable/disable shared TLB, enable/disable VPEs.
971 env->mvp->CP0_MVPControl = newval;
974 void helper_mtc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
979 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
980 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
981 newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
983 /* Yield scheduler intercept not implemented. */
984 /* Gating storage scheduler intercept not implemented. */
986 // TODO: Enable/disable TCs.
988 env->CP0_VPEControl = newval;
991 void helper_mttc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
993 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
994 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
998 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
999 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1000 newval = (other->CP0_VPEControl & ~mask) | (arg1 & mask);
1002 /* TODO: Enable/disable TCs. */
1004 other->CP0_VPEControl = newval;
1007 target_ulong helper_mftc0_vpecontrol(CPUMIPSState *env)
1009 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1010 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1011 /* FIXME: Mask away return zero on read bits. */
1012 return other->CP0_VPEControl;
1015 target_ulong helper_mftc0_vpeconf0(CPUMIPSState *env)
1017 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1018 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1020 return other->CP0_VPEConf0;
1023 void helper_mtc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1028 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
1029 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
1030 mask |= (0xff << CP0VPEC0_XTC);
1031 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1033 newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1035 // TODO: TC exclusive handling due to ERL/EXL.
1037 env->CP0_VPEConf0 = newval;
1040 void helper_mttc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1042 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1043 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1047 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1048 newval = (other->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1050 /* TODO: TC exclusive handling due to ERL/EXL. */
1051 other->CP0_VPEConf0 = newval;
1054 void helper_mtc0_vpeconf1(CPUMIPSState *env, target_ulong arg1)
1059 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1060 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
1061 (0xff << CP0VPEC1_NCP1);
1062 newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
1064 /* UDI not implemented. */
1065 /* CP2 not implemented. */
1067 // TODO: Handle FPU (CP1) binding.
1069 env->CP0_VPEConf1 = newval;
1072 void helper_mtc0_yqmask(CPUMIPSState *env, target_ulong arg1)
1074 /* Yield qualifier inputs not implemented. */
1075 env->CP0_YQMask = 0x00000000;
1078 void helper_mtc0_vpeopt(CPUMIPSState *env, target_ulong arg1)
1080 env->CP0_VPEOpt = arg1 & 0x0000ffff;
1083 void helper_mtc0_entrylo0(CPUMIPSState *env, target_ulong arg1)
1085 /* Large physaddr (PABITS) not implemented */
1086 /* 1k pages not implemented */
1087 env->CP0_EntryLo0 = arg1 & 0x3FFFFFFF;
1090 void helper_mtc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1092 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
1095 newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
1097 env->active_tc.CP0_TCStatus = newval;
1098 sync_c0_tcstatus(env, env->current_tc, newval);
1101 void helper_mttc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1103 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1104 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1106 if (other_tc == other->current_tc)
1107 other->active_tc.CP0_TCStatus = arg1;
1109 other->tcs[other_tc].CP0_TCStatus = arg1;
1110 sync_c0_tcstatus(other, other_tc, arg1);
1113 void helper_mtc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1115 uint32_t mask = (1 << CP0TCBd_TBE);
1118 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1119 mask |= (1 << CP0TCBd_CurVPE);
1120 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1121 env->active_tc.CP0_TCBind = newval;
1124 void helper_mttc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1126 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1127 uint32_t mask = (1 << CP0TCBd_TBE);
1129 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1131 if (other->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1132 mask |= (1 << CP0TCBd_CurVPE);
1133 if (other_tc == other->current_tc) {
1134 newval = (other->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1135 other->active_tc.CP0_TCBind = newval;
1137 newval = (other->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
1138 other->tcs[other_tc].CP0_TCBind = newval;
1142 void helper_mtc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1144 env->active_tc.PC = arg1;
1145 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1147 /* MIPS16 not implemented. */
1150 void helper_mttc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1152 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1153 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1155 if (other_tc == other->current_tc) {
1156 other->active_tc.PC = arg1;
1157 other->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1158 other->lladdr = 0ULL;
1159 /* MIPS16 not implemented. */
1161 other->tcs[other_tc].PC = arg1;
1162 other->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1163 other->lladdr = 0ULL;
1164 /* MIPS16 not implemented. */
1168 void helper_mtc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1170 MIPSCPU *cpu = mips_env_get_cpu(env);
1172 env->active_tc.CP0_TCHalt = arg1 & 0x1;
1174 // TODO: Halt TC / Restart (if allocated+active) TC.
1175 if (env->active_tc.CP0_TCHalt & 1) {
1176 mips_tc_sleep(cpu, env->current_tc);
1178 mips_tc_wake(cpu, env->current_tc);
1182 void helper_mttc0_tchalt(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);
1186 MIPSCPU *other_cpu = mips_env_get_cpu(other);
1188 // TODO: Halt TC / Restart (if allocated+active) TC.
1190 if (other_tc == other->current_tc)
1191 other->active_tc.CP0_TCHalt = arg1;
1193 other->tcs[other_tc].CP0_TCHalt = arg1;
1196 mips_tc_sleep(other_cpu, other_tc);
1198 mips_tc_wake(other_cpu, other_tc);
1202 void helper_mtc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1204 env->active_tc.CP0_TCContext = arg1;
1207 void helper_mttc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1209 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1210 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1212 if (other_tc == other->current_tc)
1213 other->active_tc.CP0_TCContext = arg1;
1215 other->tcs[other_tc].CP0_TCContext = arg1;
1218 void helper_mtc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1220 env->active_tc.CP0_TCSchedule = arg1;
1223 void helper_mttc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1225 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1226 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1228 if (other_tc == other->current_tc)
1229 other->active_tc.CP0_TCSchedule = arg1;
1231 other->tcs[other_tc].CP0_TCSchedule = arg1;
1234 void helper_mtc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1236 env->active_tc.CP0_TCScheFBack = arg1;
1239 void helper_mttc0_tcschefback(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_TCScheFBack = arg1;
1247 other->tcs[other_tc].CP0_TCScheFBack = arg1;
1250 void helper_mtc0_entrylo1(CPUMIPSState *env, target_ulong arg1)
1252 /* Large physaddr (PABITS) not implemented */
1253 /* 1k pages not implemented */
1254 env->CP0_EntryLo1 = arg1 & 0x3FFFFFFF;
1257 void helper_mtc0_context(CPUMIPSState *env, target_ulong arg1)
1259 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1262 void helper_mtc0_pagemask(CPUMIPSState *env, target_ulong arg1)
1264 /* 1k pages not implemented */
1265 env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1268 void helper_mtc0_pagegrain(CPUMIPSState *env, target_ulong arg1)
1270 /* SmartMIPS not implemented */
1271 /* Large physaddr (PABITS) not implemented */
1272 /* 1k pages not implemented */
1273 env->CP0_PageGrain = 0;
1276 void helper_mtc0_wired(CPUMIPSState *env, target_ulong arg1)
1278 env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1281 void helper_mtc0_srsconf0(CPUMIPSState *env, target_ulong arg1)
1283 env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1286 void helper_mtc0_srsconf1(CPUMIPSState *env, target_ulong arg1)
1288 env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1291 void helper_mtc0_srsconf2(CPUMIPSState *env, target_ulong arg1)
1293 env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1296 void helper_mtc0_srsconf3(CPUMIPSState *env, target_ulong arg1)
1298 env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1301 void helper_mtc0_srsconf4(CPUMIPSState *env, target_ulong arg1)
1303 env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1306 void helper_mtc0_hwrena(CPUMIPSState *env, target_ulong arg1)
1308 env->CP0_HWREna = arg1 & 0x0000000F;
1311 void helper_mtc0_count(CPUMIPSState *env, target_ulong arg1)
1313 cpu_mips_store_count(env, arg1);
1316 void helper_mtc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1318 target_ulong old, val;
1320 /* 1k pages not implemented */
1321 val = arg1 & ((TARGET_PAGE_MASK << 1) | 0xFF);
1322 #if defined(TARGET_MIPS64)
1323 val &= env->SEGMask;
1325 old = env->CP0_EntryHi;
1326 env->CP0_EntryHi = val;
1327 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1328 sync_c0_entryhi(env, env->current_tc);
1330 /* If the ASID changes, flush qemu's TLB. */
1331 if ((old & 0xFF) != (val & 0xFF))
1332 cpu_mips_tlb_flush(env, 1);
1335 void helper_mttc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1337 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1338 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1340 other->CP0_EntryHi = arg1;
1341 sync_c0_entryhi(other, other_tc);
1344 void helper_mtc0_compare(CPUMIPSState *env, target_ulong arg1)
1346 cpu_mips_store_compare(env, arg1);
1349 void helper_mtc0_status(CPUMIPSState *env, target_ulong arg1)
1352 uint32_t mask = env->CP0_Status_rw_bitmask;
1355 old = env->CP0_Status;
1356 env->CP0_Status = (env->CP0_Status & ~mask) | val;
1357 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1358 sync_c0_status(env, env, env->current_tc);
1360 compute_hflags(env);
1363 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1364 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1365 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1366 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1368 switch (env->hflags & MIPS_HFLAG_KSU) {
1369 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1370 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1371 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1372 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
1377 void helper_mttc0_status(CPUMIPSState *env, target_ulong arg1)
1379 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1380 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1382 other->CP0_Status = arg1 & ~0xf1000018;
1383 sync_c0_status(env, other, other_tc);
1386 void helper_mtc0_intctl(CPUMIPSState *env, target_ulong arg1)
1388 /* vectored interrupts not implemented, no performance counters. */
1389 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000003e0) | (arg1 & 0x000003e0);
1392 void helper_mtc0_srsctl(CPUMIPSState *env, target_ulong arg1)
1394 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1395 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1398 static void mtc0_cause(CPUMIPSState *cpu, target_ulong arg1)
1400 uint32_t mask = 0x00C00300;
1401 uint32_t old = cpu->CP0_Cause;
1404 if (cpu->insn_flags & ISA_MIPS32R2) {
1405 mask |= 1 << CP0Ca_DC;
1408 cpu->CP0_Cause = (cpu->CP0_Cause & ~mask) | (arg1 & mask);
1410 if ((old ^ cpu->CP0_Cause) & (1 << CP0Ca_DC)) {
1411 if (cpu->CP0_Cause & (1 << CP0Ca_DC)) {
1412 cpu_mips_stop_count(cpu);
1414 cpu_mips_start_count(cpu);
1418 /* Set/reset software interrupts */
1419 for (i = 0 ; i < 2 ; i++) {
1420 if ((old ^ cpu->CP0_Cause) & (1 << (CP0Ca_IP + i))) {
1421 cpu_mips_soft_irq(cpu, i, cpu->CP0_Cause & (1 << (CP0Ca_IP + i)));
1426 void helper_mtc0_cause(CPUMIPSState *env, target_ulong arg1)
1428 mtc0_cause(env, arg1);
1431 void helper_mttc0_cause(CPUMIPSState *env, target_ulong arg1)
1433 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1434 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1436 mtc0_cause(other, arg1);
1439 target_ulong helper_mftc0_epc(CPUMIPSState *env)
1441 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1442 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1444 return other->CP0_EPC;
1447 target_ulong helper_mftc0_ebase(CPUMIPSState *env)
1449 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1450 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1452 return other->CP0_EBase;
1455 void helper_mtc0_ebase(CPUMIPSState *env, target_ulong arg1)
1457 /* vectored interrupts not implemented */
1458 env->CP0_EBase = (env->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1461 void helper_mttc0_ebase(CPUMIPSState *env, target_ulong arg1)
1463 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1464 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1465 other->CP0_EBase = (other->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1468 target_ulong helper_mftc0_configx(CPUMIPSState *env, target_ulong idx)
1470 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1471 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1474 case 0: return other->CP0_Config0;
1475 case 1: return other->CP0_Config1;
1476 case 2: return other->CP0_Config2;
1477 case 3: return other->CP0_Config3;
1478 /* 4 and 5 are reserved. */
1479 case 6: return other->CP0_Config6;
1480 case 7: return other->CP0_Config7;
1487 void helper_mtc0_config0(CPUMIPSState *env, target_ulong arg1)
1489 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1492 void helper_mtc0_config2(CPUMIPSState *env, target_ulong arg1)
1494 /* tertiary/secondary caches not implemented */
1495 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1498 void helper_mtc0_lladdr(CPUMIPSState *env, target_ulong arg1)
1500 target_long mask = env->CP0_LLAddr_rw_bitmask;
1501 arg1 = arg1 << env->CP0_LLAddr_shift;
1502 env->lladdr = (env->lladdr & ~mask) | (arg1 & mask);
1505 void helper_mtc0_watchlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1507 /* Watch exceptions for instructions, data loads, data stores
1509 env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1512 void helper_mtc0_watchhi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1514 env->CP0_WatchHi[sel] = (arg1 & 0x40FF0FF8);
1515 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1518 void helper_mtc0_xcontext(CPUMIPSState *env, target_ulong arg1)
1520 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1521 env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1524 void helper_mtc0_framemask(CPUMIPSState *env, target_ulong arg1)
1526 env->CP0_Framemask = arg1; /* XXX */
1529 void helper_mtc0_debug(CPUMIPSState *env, target_ulong arg1)
1531 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
1532 if (arg1 & (1 << CP0DB_DM))
1533 env->hflags |= MIPS_HFLAG_DM;
1535 env->hflags &= ~MIPS_HFLAG_DM;
1538 void helper_mttc0_debug(CPUMIPSState *env, target_ulong arg1)
1540 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1541 uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1542 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1544 /* XXX: Might be wrong, check with EJTAG spec. */
1545 if (other_tc == other->current_tc)
1546 other->active_tc.CP0_Debug_tcstatus = val;
1548 other->tcs[other_tc].CP0_Debug_tcstatus = val;
1549 other->CP0_Debug = (other->CP0_Debug &
1550 ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1551 (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1554 void helper_mtc0_performance0(CPUMIPSState *env, target_ulong arg1)
1556 env->CP0_Performance0 = arg1 & 0x000007ff;
1559 void helper_mtc0_taglo(CPUMIPSState *env, target_ulong arg1)
1561 env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1564 void helper_mtc0_datalo(CPUMIPSState *env, target_ulong arg1)
1566 env->CP0_DataLo = arg1; /* XXX */
1569 void helper_mtc0_taghi(CPUMIPSState *env, target_ulong arg1)
1571 env->CP0_TagHi = arg1; /* XXX */
1574 void helper_mtc0_datahi(CPUMIPSState *env, target_ulong arg1)
1576 env->CP0_DataHi = arg1; /* XXX */
1579 /* MIPS MT functions */
1580 target_ulong helper_mftgpr(CPUMIPSState *env, uint32_t sel)
1582 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1583 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1585 if (other_tc == other->current_tc)
1586 return other->active_tc.gpr[sel];
1588 return other->tcs[other_tc].gpr[sel];
1591 target_ulong helper_mftlo(CPUMIPSState *env, uint32_t sel)
1593 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1594 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1596 if (other_tc == other->current_tc)
1597 return other->active_tc.LO[sel];
1599 return other->tcs[other_tc].LO[sel];
1602 target_ulong helper_mfthi(CPUMIPSState *env, uint32_t sel)
1604 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1605 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1607 if (other_tc == other->current_tc)
1608 return other->active_tc.HI[sel];
1610 return other->tcs[other_tc].HI[sel];
1613 target_ulong helper_mftacx(CPUMIPSState *env, uint32_t sel)
1615 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1616 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1618 if (other_tc == other->current_tc)
1619 return other->active_tc.ACX[sel];
1621 return other->tcs[other_tc].ACX[sel];
1624 target_ulong helper_mftdsp(CPUMIPSState *env)
1626 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1627 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1629 if (other_tc == other->current_tc)
1630 return other->active_tc.DSPControl;
1632 return other->tcs[other_tc].DSPControl;
1635 void helper_mttgpr(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1637 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1638 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1640 if (other_tc == other->current_tc)
1641 other->active_tc.gpr[sel] = arg1;
1643 other->tcs[other_tc].gpr[sel] = arg1;
1646 void helper_mttlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1648 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1649 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1651 if (other_tc == other->current_tc)
1652 other->active_tc.LO[sel] = arg1;
1654 other->tcs[other_tc].LO[sel] = arg1;
1657 void helper_mtthi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1659 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1660 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1662 if (other_tc == other->current_tc)
1663 other->active_tc.HI[sel] = arg1;
1665 other->tcs[other_tc].HI[sel] = arg1;
1668 void helper_mttacx(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1670 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1671 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1673 if (other_tc == other->current_tc)
1674 other->active_tc.ACX[sel] = arg1;
1676 other->tcs[other_tc].ACX[sel] = arg1;
1679 void helper_mttdsp(CPUMIPSState *env, target_ulong arg1)
1681 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1682 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1684 if (other_tc == other->current_tc)
1685 other->active_tc.DSPControl = arg1;
1687 other->tcs[other_tc].DSPControl = arg1;
1690 /* MIPS MT functions */
1691 target_ulong helper_dmt(void)
1697 target_ulong helper_emt(void)
1703 target_ulong helper_dvpe(CPUMIPSState *env)
1705 CPUMIPSState *other_cpu_env = first_cpu;
1706 target_ulong prev = env->mvp->CP0_MVPControl;
1709 /* Turn off all VPEs except the one executing the dvpe. */
1710 if (other_cpu_env != env) {
1711 MIPSCPU *other_cpu = mips_env_get_cpu(other_cpu_env);
1713 other_cpu_env->mvp->CP0_MVPControl &= ~(1 << CP0MVPCo_EVP);
1714 mips_vpe_sleep(other_cpu);
1716 other_cpu_env = other_cpu_env->next_cpu;
1717 } while (other_cpu_env);
1721 target_ulong helper_evpe(CPUMIPSState *env)
1723 CPUMIPSState *other_cpu_env = first_cpu;
1724 target_ulong prev = env->mvp->CP0_MVPControl;
1727 MIPSCPU *other_cpu = mips_env_get_cpu(other_cpu_env);
1729 if (other_cpu_env != env
1730 /* If the VPE is WFI, don't disturb its sleep. */
1731 && !mips_vpe_is_wfi(other_cpu)) {
1732 /* Enable the VPE. */
1733 other_cpu_env->mvp->CP0_MVPControl |= (1 << CP0MVPCo_EVP);
1734 mips_vpe_wake(other_cpu_env); /* And wake it up. */
1736 other_cpu_env = other_cpu_env->next_cpu;
1737 } while (other_cpu_env);
1740 #endif /* !CONFIG_USER_ONLY */
1742 void helper_fork(target_ulong arg1, target_ulong arg2)
1744 // arg1 = rt, arg2 = rs
1746 // TODO: store to TC register
1749 target_ulong helper_yield(CPUMIPSState *env, target_ulong arg)
1751 target_long arg1 = arg;
1754 /* No scheduling policy implemented. */
1756 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1757 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
1758 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1759 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1760 helper_raise_exception(env, EXCP_THREAD);
1763 } else if (arg1 == 0) {
1764 if (0 /* TODO: TC underflow */) {
1765 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1766 helper_raise_exception(env, EXCP_THREAD);
1768 // TODO: Deallocate TC
1770 } else if (arg1 > 0) {
1771 /* Yield qualifier inputs not implemented. */
1772 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1773 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1774 helper_raise_exception(env, EXCP_THREAD);
1776 return env->CP0_YQMask;
1779 #ifndef CONFIG_USER_ONLY
1780 /* TLB management */
1781 static void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global)
1783 /* Flush qemu's TLB and discard all shadowed entries. */
1784 tlb_flush (env, flush_global);
1785 env->tlb->tlb_in_use = env->tlb->nb_tlb;
1788 static void r4k_mips_tlb_flush_extra (CPUMIPSState *env, int first)
1790 /* Discard entries from env->tlb[first] onwards. */
1791 while (env->tlb->tlb_in_use > first) {
1792 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1796 static void r4k_fill_tlb(CPUMIPSState *env, int idx)
1800 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1801 tlb = &env->tlb->mmu.r4k.tlb[idx];
1802 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1803 #if defined(TARGET_MIPS64)
1804 tlb->VPN &= env->SEGMask;
1806 tlb->ASID = env->CP0_EntryHi & 0xFF;
1807 tlb->PageMask = env->CP0_PageMask;
1808 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1809 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
1810 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
1811 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
1812 tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
1813 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
1814 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
1815 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
1816 tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
1819 void r4k_helper_tlbwi(CPUMIPSState *env)
1825 bool G, V0, D0, V1, D1;
1827 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1828 tlb = &env->tlb->mmu.r4k.tlb[idx];
1829 VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1830 #if defined(TARGET_MIPS64)
1831 VPN &= env->SEGMask;
1833 ASID = env->CP0_EntryHi & 0xff;
1834 G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1835 V0 = (env->CP0_EntryLo0 & 2) != 0;
1836 D0 = (env->CP0_EntryLo0 & 4) != 0;
1837 V1 = (env->CP0_EntryLo1 & 2) != 0;
1838 D1 = (env->CP0_EntryLo1 & 4) != 0;
1840 /* Discard cached TLB entries, unless tlbwi is just upgrading access
1841 permissions on the current entry. */
1842 if (tlb->VPN != VPN || tlb->ASID != ASID || tlb->G != G ||
1843 (tlb->V0 && !V0) || (tlb->D0 && !D0) ||
1844 (tlb->V1 && !V1) || (tlb->D1 && !D1)) {
1845 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
1848 r4k_invalidate_tlb(env, idx, 0);
1849 r4k_fill_tlb(env, idx);
1852 void r4k_helper_tlbwr(CPUMIPSState *env)
1854 int r = cpu_mips_get_random(env);
1856 r4k_invalidate_tlb(env, r, 1);
1857 r4k_fill_tlb(env, r);
1860 void r4k_helper_tlbp(CPUMIPSState *env)
1869 ASID = env->CP0_EntryHi & 0xFF;
1870 for (i = 0; i < env->tlb->nb_tlb; i++) {
1871 tlb = &env->tlb->mmu.r4k.tlb[i];
1872 /* 1k pages are not supported. */
1873 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1874 tag = env->CP0_EntryHi & ~mask;
1875 VPN = tlb->VPN & ~mask;
1876 #if defined(TARGET_MIPS64)
1877 tag &= env->SEGMask;
1879 /* Check ASID, virtual page number & size */
1880 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1886 if (i == env->tlb->nb_tlb) {
1887 /* No match. Discard any shadow entries, if any of them match. */
1888 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
1889 tlb = &env->tlb->mmu.r4k.tlb[i];
1890 /* 1k pages are not supported. */
1891 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1892 tag = env->CP0_EntryHi & ~mask;
1893 VPN = tlb->VPN & ~mask;
1894 #if defined(TARGET_MIPS64)
1895 tag &= env->SEGMask;
1897 /* Check ASID, virtual page number & size */
1898 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1899 r4k_mips_tlb_flush_extra (env, i);
1904 env->CP0_Index |= 0x80000000;
1908 void r4k_helper_tlbr(CPUMIPSState *env)
1914 ASID = env->CP0_EntryHi & 0xFF;
1915 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1916 tlb = &env->tlb->mmu.r4k.tlb[idx];
1918 /* If this will change the current ASID, flush qemu's TLB. */
1919 if (ASID != tlb->ASID)
1920 cpu_mips_tlb_flush (env, 1);
1922 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
1924 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
1925 env->CP0_PageMask = tlb->PageMask;
1926 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
1927 (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
1928 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
1929 (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
1932 void helper_tlbwi(CPUMIPSState *env)
1934 env->tlb->helper_tlbwi(env);
1937 void helper_tlbwr(CPUMIPSState *env)
1939 env->tlb->helper_tlbwr(env);
1942 void helper_tlbp(CPUMIPSState *env)
1944 env->tlb->helper_tlbp(env);
1947 void helper_tlbr(CPUMIPSState *env)
1949 env->tlb->helper_tlbr(env);
1953 target_ulong helper_di(CPUMIPSState *env)
1955 target_ulong t0 = env->CP0_Status;
1957 env->CP0_Status = t0 & ~(1 << CP0St_IE);
1961 target_ulong helper_ei(CPUMIPSState *env)
1963 target_ulong t0 = env->CP0_Status;
1965 env->CP0_Status = t0 | (1 << CP0St_IE);
1969 static void debug_pre_eret(CPUMIPSState *env)
1971 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1972 qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
1973 env->active_tc.PC, env->CP0_EPC);
1974 if (env->CP0_Status & (1 << CP0St_ERL))
1975 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
1976 if (env->hflags & MIPS_HFLAG_DM)
1977 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
1982 static void debug_post_eret(CPUMIPSState *env)
1984 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1985 qemu_log(" => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
1986 env->active_tc.PC, env->CP0_EPC);
1987 if (env->CP0_Status & (1 << CP0St_ERL))
1988 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
1989 if (env->hflags & MIPS_HFLAG_DM)
1990 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
1991 switch (env->hflags & MIPS_HFLAG_KSU) {
1992 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1993 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1994 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1995 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
2000 static void set_pc(CPUMIPSState *env, target_ulong error_pc)
2002 env->active_tc.PC = error_pc & ~(target_ulong)1;
2004 env->hflags |= MIPS_HFLAG_M16;
2006 env->hflags &= ~(MIPS_HFLAG_M16);
2010 void helper_eret(CPUMIPSState *env)
2012 debug_pre_eret(env);
2013 if (env->CP0_Status & (1 << CP0St_ERL)) {
2014 set_pc(env, env->CP0_ErrorEPC);
2015 env->CP0_Status &= ~(1 << CP0St_ERL);
2017 set_pc(env, env->CP0_EPC);
2018 env->CP0_Status &= ~(1 << CP0St_EXL);
2020 compute_hflags(env);
2021 debug_post_eret(env);
2025 void helper_deret(CPUMIPSState *env)
2027 debug_pre_eret(env);
2028 set_pc(env, env->CP0_DEPC);
2030 env->hflags &= MIPS_HFLAG_DM;
2031 compute_hflags(env);
2032 debug_post_eret(env);
2035 #endif /* !CONFIG_USER_ONLY */
2037 target_ulong helper_rdhwr_cpunum(CPUMIPSState *env)
2039 if ((env->hflags & MIPS_HFLAG_CP0) ||
2040 (env->CP0_HWREna & (1 << 0)))
2041 return env->CP0_EBase & 0x3ff;
2043 helper_raise_exception(env, EXCP_RI);
2048 target_ulong helper_rdhwr_synci_step(CPUMIPSState *env)
2050 if ((env->hflags & MIPS_HFLAG_CP0) ||
2051 (env->CP0_HWREna & (1 << 1)))
2052 return env->SYNCI_Step;
2054 helper_raise_exception(env, EXCP_RI);
2059 target_ulong helper_rdhwr_cc(CPUMIPSState *env)
2061 if ((env->hflags & MIPS_HFLAG_CP0) ||
2062 (env->CP0_HWREna & (1 << 2)))
2063 return env->CP0_Count;
2065 helper_raise_exception(env, EXCP_RI);
2070 target_ulong helper_rdhwr_ccres(CPUMIPSState *env)
2072 if ((env->hflags & MIPS_HFLAG_CP0) ||
2073 (env->CP0_HWREna & (1 << 3)))
2076 helper_raise_exception(env, EXCP_RI);
2081 void helper_pmon(CPUMIPSState *env, int function)
2085 case 2: /* TODO: char inbyte(int waitflag); */
2086 if (env->active_tc.gpr[4] == 0)
2087 env->active_tc.gpr[2] = -1;
2089 case 11: /* TODO: char inbyte (void); */
2090 env->active_tc.gpr[2] = -1;
2094 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
2100 unsigned char *fmt = (void *)(uintptr_t)env->active_tc.gpr[4];
2107 void helper_wait(CPUMIPSState *env)
2110 cpu_reset_interrupt(env, CPU_INTERRUPT_WAKE);
2111 helper_raise_exception(env, EXCP_HLT);
2114 #if !defined(CONFIG_USER_ONLY)
2116 static void QEMU_NORETURN do_unaligned_access(CPUMIPSState *env,
2117 target_ulong addr, int is_write,
2118 int is_user, uintptr_t retaddr);
2120 #define MMUSUFFIX _mmu
2121 #define ALIGNED_ONLY
2124 #include "exec/softmmu_template.h"
2127 #include "exec/softmmu_template.h"
2130 #include "exec/softmmu_template.h"
2133 #include "exec/softmmu_template.h"
2135 static void do_unaligned_access(CPUMIPSState *env, target_ulong addr,
2136 int is_write, int is_user, uintptr_t retaddr)
2138 env->CP0_BadVAddr = addr;
2139 do_raise_exception(env, (is_write == 1) ? EXCP_AdES : EXCP_AdEL, retaddr);
2142 void tlb_fill(CPUMIPSState *env, target_ulong addr, int is_write, int mmu_idx,
2147 ret = cpu_mips_handle_mmu_fault(env, addr, is_write, mmu_idx);
2149 do_raise_exception_err(env, env->exception_index,
2150 env->error_code, retaddr);
2154 void cpu_unassigned_access(CPUMIPSState *env, hwaddr addr,
2155 int is_write, int is_exec, int unused, int size)
2158 helper_raise_exception(env, EXCP_IBE);
2160 helper_raise_exception(env, EXCP_DBE);
2162 #endif /* !CONFIG_USER_ONLY */
2164 /* Complex FPU operations which may need stack space. */
2166 #define FLOAT_TWO32 make_float32(1 << 30)
2167 #define FLOAT_TWO64 make_float64(1ULL << 62)
2168 #define FP_TO_INT32_OVERFLOW 0x7fffffff
2169 #define FP_TO_INT64_OVERFLOW 0x7fffffffffffffffULL
2171 /* convert MIPS rounding mode in FCR31 to IEEE library */
2172 static unsigned int ieee_rm[] = {
2173 float_round_nearest_even,
2174 float_round_to_zero,
2179 static inline void restore_rounding_mode(CPUMIPSState *env)
2181 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3],
2182 &env->active_fpu.fp_status);
2185 static inline void restore_flush_mode(CPUMIPSState *env)
2187 set_flush_to_zero((env->active_fpu.fcr31 & (1 << 24)) != 0,
2188 &env->active_fpu.fp_status);
2191 target_ulong helper_cfc1(CPUMIPSState *env, uint32_t reg)
2197 arg1 = (int32_t)env->active_fpu.fcr0;
2200 arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);
2203 arg1 = env->active_fpu.fcr31 & 0x0003f07c;
2206 arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);
2209 arg1 = (int32_t)env->active_fpu.fcr31;
2216 void helper_ctc1(CPUMIPSState *env, target_ulong arg1, uint32_t reg)
2220 if (arg1 & 0xffffff00)
2222 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
2223 ((arg1 & 0x1) << 23);
2226 if (arg1 & 0x007c0000)
2228 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
2231 if (arg1 & 0x007c0000)
2233 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
2234 ((arg1 & 0x4) << 22);
2237 if (arg1 & 0x007c0000)
2239 env->active_fpu.fcr31 = arg1;
2244 /* set rounding mode */
2245 restore_rounding_mode(env);
2246 /* set flush-to-zero mode */
2247 restore_flush_mode(env);
2248 set_float_exception_flags(0, &env->active_fpu.fp_status);
2249 if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
2250 do_raise_exception(env, EXCP_FPE, GETPC());
2253 static inline int ieee_ex_to_mips(int xcpt)
2257 if (xcpt & float_flag_invalid) {
2260 if (xcpt & float_flag_overflow) {
2263 if (xcpt & float_flag_underflow) {
2264 ret |= FP_UNDERFLOW;
2266 if (xcpt & float_flag_divbyzero) {
2269 if (xcpt & float_flag_inexact) {
2276 static inline void update_fcr31(CPUMIPSState *env, uintptr_t pc)
2278 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
2280 SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
2283 set_float_exception_flags(0, &env->active_fpu.fp_status);
2285 if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp) {
2286 do_raise_exception(env, EXCP_FPE, pc);
2288 UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
2294 Single precition routines have a "s" suffix, double precision a
2295 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
2296 paired single lower "pl", paired single upper "pu". */
2298 /* unary operations, modifying fp status */
2299 uint64_t helper_float_sqrt_d(CPUMIPSState *env, uint64_t fdt0)
2301 fdt0 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2302 update_fcr31(env, GETPC());
2306 uint32_t helper_float_sqrt_s(CPUMIPSState *env, uint32_t fst0)
2308 fst0 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2309 update_fcr31(env, GETPC());
2313 uint64_t helper_float_cvtd_s(CPUMIPSState *env, uint32_t fst0)
2317 fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
2318 update_fcr31(env, GETPC());
2322 uint64_t helper_float_cvtd_w(CPUMIPSState *env, uint32_t wt0)
2326 fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2327 update_fcr31(env, GETPC());
2331 uint64_t helper_float_cvtd_l(CPUMIPSState *env, uint64_t dt0)
2335 fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2336 update_fcr31(env, GETPC());
2340 uint64_t helper_float_cvtl_d(CPUMIPSState *env, uint64_t fdt0)
2344 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2345 if (get_float_exception_flags(&env->active_fpu.fp_status)
2346 & (float_flag_invalid | float_flag_overflow)) {
2347 dt2 = FP_TO_INT64_OVERFLOW;
2349 update_fcr31(env, GETPC());
2353 uint64_t helper_float_cvtl_s(CPUMIPSState *env, uint32_t fst0)
2357 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2358 if (get_float_exception_flags(&env->active_fpu.fp_status)
2359 & (float_flag_invalid | float_flag_overflow)) {
2360 dt2 = FP_TO_INT64_OVERFLOW;
2362 update_fcr31(env, GETPC());
2366 uint64_t helper_float_cvtps_pw(CPUMIPSState *env, uint64_t dt0)
2371 fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2372 fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
2373 update_fcr31(env, GETPC());
2374 return ((uint64_t)fsth2 << 32) | fst2;
2377 uint64_t helper_float_cvtpw_ps(CPUMIPSState *env, uint64_t fdt0)
2383 wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2384 excp = get_float_exception_flags(&env->active_fpu.fp_status);
2385 if (excp & (float_flag_overflow | float_flag_invalid)) {
2386 wt2 = FP_TO_INT32_OVERFLOW;
2389 set_float_exception_flags(0, &env->active_fpu.fp_status);
2390 wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
2391 excph = get_float_exception_flags(&env->active_fpu.fp_status);
2392 if (excph & (float_flag_overflow | float_flag_invalid)) {
2393 wth2 = FP_TO_INT32_OVERFLOW;
2396 set_float_exception_flags(excp | excph, &env->active_fpu.fp_status);
2397 update_fcr31(env, GETPC());
2399 return ((uint64_t)wth2 << 32) | wt2;
2402 uint32_t helper_float_cvts_d(CPUMIPSState *env, uint64_t fdt0)
2406 fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
2407 update_fcr31(env, GETPC());
2411 uint32_t helper_float_cvts_w(CPUMIPSState *env, uint32_t wt0)
2415 fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
2416 update_fcr31(env, GETPC());
2420 uint32_t helper_float_cvts_l(CPUMIPSState *env, uint64_t dt0)
2424 fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
2425 update_fcr31(env, GETPC());
2429 uint32_t helper_float_cvts_pl(CPUMIPSState *env, uint32_t wt0)
2434 update_fcr31(env, GETPC());
2438 uint32_t helper_float_cvts_pu(CPUMIPSState *env, uint32_t wth0)
2443 update_fcr31(env, GETPC());
2447 uint32_t helper_float_cvtw_s(CPUMIPSState *env, uint32_t fst0)
2451 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2452 update_fcr31(env, GETPC());
2453 if (get_float_exception_flags(&env->active_fpu.fp_status)
2454 & (float_flag_invalid | float_flag_overflow)) {
2455 wt2 = FP_TO_INT32_OVERFLOW;
2460 uint32_t helper_float_cvtw_d(CPUMIPSState *env, uint64_t fdt0)
2464 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2465 if (get_float_exception_flags(&env->active_fpu.fp_status)
2466 & (float_flag_invalid | float_flag_overflow)) {
2467 wt2 = FP_TO_INT32_OVERFLOW;
2469 update_fcr31(env, GETPC());
2473 uint64_t helper_float_roundl_d(CPUMIPSState *env, uint64_t fdt0)
2477 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2478 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2479 restore_rounding_mode(env);
2480 if (get_float_exception_flags(&env->active_fpu.fp_status)
2481 & (float_flag_invalid | float_flag_overflow)) {
2482 dt2 = FP_TO_INT64_OVERFLOW;
2484 update_fcr31(env, GETPC());
2488 uint64_t helper_float_roundl_s(CPUMIPSState *env, uint32_t fst0)
2492 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2493 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2494 restore_rounding_mode(env);
2495 if (get_float_exception_flags(&env->active_fpu.fp_status)
2496 & (float_flag_invalid | float_flag_overflow)) {
2497 dt2 = FP_TO_INT64_OVERFLOW;
2499 update_fcr31(env, GETPC());
2503 uint32_t helper_float_roundw_d(CPUMIPSState *env, uint64_t fdt0)
2507 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2508 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2509 restore_rounding_mode(env);
2510 if (get_float_exception_flags(&env->active_fpu.fp_status)
2511 & (float_flag_invalid | float_flag_overflow)) {
2512 wt2 = FP_TO_INT32_OVERFLOW;
2514 update_fcr31(env, GETPC());
2518 uint32_t helper_float_roundw_s(CPUMIPSState *env, uint32_t fst0)
2522 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2523 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2524 restore_rounding_mode(env);
2525 if (get_float_exception_flags(&env->active_fpu.fp_status)
2526 & (float_flag_invalid | float_flag_overflow)) {
2527 wt2 = FP_TO_INT32_OVERFLOW;
2529 update_fcr31(env, GETPC());
2533 uint64_t helper_float_truncl_d(CPUMIPSState *env, uint64_t fdt0)
2537 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
2538 if (get_float_exception_flags(&env->active_fpu.fp_status)
2539 & (float_flag_invalid | float_flag_overflow)) {
2540 dt2 = FP_TO_INT64_OVERFLOW;
2542 update_fcr31(env, GETPC());
2546 uint64_t helper_float_truncl_s(CPUMIPSState *env, uint32_t fst0)
2550 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
2551 if (get_float_exception_flags(&env->active_fpu.fp_status)
2552 & (float_flag_invalid | float_flag_overflow)) {
2553 dt2 = FP_TO_INT64_OVERFLOW;
2555 update_fcr31(env, GETPC());
2559 uint32_t helper_float_truncw_d(CPUMIPSState *env, uint64_t fdt0)
2563 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
2564 if (get_float_exception_flags(&env->active_fpu.fp_status)
2565 & (float_flag_invalid | float_flag_overflow)) {
2566 wt2 = FP_TO_INT32_OVERFLOW;
2568 update_fcr31(env, GETPC());
2572 uint32_t helper_float_truncw_s(CPUMIPSState *env, uint32_t fst0)
2576 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
2577 if (get_float_exception_flags(&env->active_fpu.fp_status)
2578 & (float_flag_invalid | float_flag_overflow)) {
2579 wt2 = FP_TO_INT32_OVERFLOW;
2581 update_fcr31(env, GETPC());
2585 uint64_t helper_float_ceill_d(CPUMIPSState *env, uint64_t fdt0)
2589 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2590 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2591 restore_rounding_mode(env);
2592 if (get_float_exception_flags(&env->active_fpu.fp_status)
2593 & (float_flag_invalid | float_flag_overflow)) {
2594 dt2 = FP_TO_INT64_OVERFLOW;
2596 update_fcr31(env, GETPC());
2600 uint64_t helper_float_ceill_s(CPUMIPSState *env, uint32_t fst0)
2604 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2605 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2606 restore_rounding_mode(env);
2607 if (get_float_exception_flags(&env->active_fpu.fp_status)
2608 & (float_flag_invalid | float_flag_overflow)) {
2609 dt2 = FP_TO_INT64_OVERFLOW;
2611 update_fcr31(env, GETPC());
2615 uint32_t helper_float_ceilw_d(CPUMIPSState *env, uint64_t fdt0)
2619 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2620 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2621 restore_rounding_mode(env);
2622 if (get_float_exception_flags(&env->active_fpu.fp_status)
2623 & (float_flag_invalid | float_flag_overflow)) {
2624 wt2 = FP_TO_INT32_OVERFLOW;
2626 update_fcr31(env, GETPC());
2630 uint32_t helper_float_ceilw_s(CPUMIPSState *env, uint32_t fst0)
2634 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2635 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2636 restore_rounding_mode(env);
2637 if (get_float_exception_flags(&env->active_fpu.fp_status)
2638 & (float_flag_invalid | float_flag_overflow)) {
2639 wt2 = FP_TO_INT32_OVERFLOW;
2641 update_fcr31(env, GETPC());
2645 uint64_t helper_float_floorl_d(CPUMIPSState *env, uint64_t fdt0)
2649 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2650 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2651 restore_rounding_mode(env);
2652 if (get_float_exception_flags(&env->active_fpu.fp_status)
2653 & (float_flag_invalid | float_flag_overflow)) {
2654 dt2 = FP_TO_INT64_OVERFLOW;
2656 update_fcr31(env, GETPC());
2660 uint64_t helper_float_floorl_s(CPUMIPSState *env, uint32_t fst0)
2664 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2665 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2666 restore_rounding_mode(env);
2667 if (get_float_exception_flags(&env->active_fpu.fp_status)
2668 & (float_flag_invalid | float_flag_overflow)) {
2669 dt2 = FP_TO_INT64_OVERFLOW;
2671 update_fcr31(env, GETPC());
2675 uint32_t helper_float_floorw_d(CPUMIPSState *env, uint64_t fdt0)
2679 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2680 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2681 restore_rounding_mode(env);
2682 if (get_float_exception_flags(&env->active_fpu.fp_status)
2683 & (float_flag_invalid | float_flag_overflow)) {
2684 wt2 = FP_TO_INT32_OVERFLOW;
2686 update_fcr31(env, GETPC());
2690 uint32_t helper_float_floorw_s(CPUMIPSState *env, uint32_t fst0)
2694 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2695 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2696 restore_rounding_mode(env);
2697 if (get_float_exception_flags(&env->active_fpu.fp_status)
2698 & (float_flag_invalid | float_flag_overflow)) {
2699 wt2 = FP_TO_INT32_OVERFLOW;
2701 update_fcr31(env, GETPC());
2705 /* unary operations, not modifying fp status */
2706 #define FLOAT_UNOP(name) \
2707 uint64_t helper_float_ ## name ## _d(uint64_t fdt0) \
2709 return float64_ ## name(fdt0); \
2711 uint32_t helper_float_ ## name ## _s(uint32_t fst0) \
2713 return float32_ ## name(fst0); \
2715 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0) \
2720 wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF); \
2721 wth0 = float32_ ## name(fdt0 >> 32); \
2722 return ((uint64_t)wth0 << 32) | wt0; \
2728 /* MIPS specific unary operations */
2729 uint64_t helper_float_recip_d(CPUMIPSState *env, uint64_t fdt0)
2733 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
2734 update_fcr31(env, GETPC());
2738 uint32_t helper_float_recip_s(CPUMIPSState *env, uint32_t fst0)
2742 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
2743 update_fcr31(env, GETPC());
2747 uint64_t helper_float_rsqrt_d(CPUMIPSState *env, uint64_t fdt0)
2751 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2752 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
2753 update_fcr31(env, GETPC());
2757 uint32_t helper_float_rsqrt_s(CPUMIPSState *env, uint32_t fst0)
2761 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2762 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
2763 update_fcr31(env, GETPC());
2767 uint64_t helper_float_recip1_d(CPUMIPSState *env, uint64_t fdt0)
2771 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
2772 update_fcr31(env, GETPC());
2776 uint32_t helper_float_recip1_s(CPUMIPSState *env, uint32_t fst0)
2780 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
2781 update_fcr31(env, GETPC());
2785 uint64_t helper_float_recip1_ps(CPUMIPSState *env, uint64_t fdt0)
2790 fst2 = float32_div(float32_one, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2791 fsth2 = float32_div(float32_one, fdt0 >> 32, &env->active_fpu.fp_status);
2792 update_fcr31(env, GETPC());
2793 return ((uint64_t)fsth2 << 32) | fst2;
2796 uint64_t helper_float_rsqrt1_d(CPUMIPSState *env, uint64_t fdt0)
2800 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2801 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
2802 update_fcr31(env, GETPC());
2806 uint32_t helper_float_rsqrt1_s(CPUMIPSState *env, uint32_t fst0)
2810 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2811 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
2812 update_fcr31(env, GETPC());
2816 uint64_t helper_float_rsqrt1_ps(CPUMIPSState *env, uint64_t fdt0)
2821 fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2822 fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
2823 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
2824 fsth2 = float32_div(float32_one, fsth2, &env->active_fpu.fp_status);
2825 update_fcr31(env, GETPC());
2826 return ((uint64_t)fsth2 << 32) | fst2;
2829 #define FLOAT_OP(name, p) void helper_float_##name##_##p(CPUMIPSState *env)
2831 /* binary operations */
2832 #define FLOAT_BINOP(name) \
2833 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
2834 uint64_t fdt0, uint64_t fdt1) \
2838 dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status); \
2839 update_fcr31(env, GETPC()); \
2843 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
2844 uint32_t fst0, uint32_t fst1) \
2848 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
2849 update_fcr31(env, GETPC()); \
2853 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
2857 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
2858 uint32_t fsth0 = fdt0 >> 32; \
2859 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
2860 uint32_t fsth1 = fdt1 >> 32; \
2864 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
2865 wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status); \
2866 update_fcr31(env, GETPC()); \
2867 return ((uint64_t)wth2 << 32) | wt2; \
2876 /* FMA based operations */
2877 #define FLOAT_FMA(name, type) \
2878 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
2879 uint64_t fdt0, uint64_t fdt1, \
2882 fdt0 = float64_muladd(fdt0, fdt1, fdt2, type, \
2883 &env->active_fpu.fp_status); \
2884 update_fcr31(env, GETPC()); \
2888 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
2889 uint32_t fst0, uint32_t fst1, \
2892 fst0 = float32_muladd(fst0, fst1, fst2, type, \
2893 &env->active_fpu.fp_status); \
2894 update_fcr31(env, GETPC()); \
2898 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
2899 uint64_t fdt0, uint64_t fdt1, \
2902 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
2903 uint32_t fsth0 = fdt0 >> 32; \
2904 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
2905 uint32_t fsth1 = fdt1 >> 32; \
2906 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
2907 uint32_t fsth2 = fdt2 >> 32; \
2909 fst0 = float32_muladd(fst0, fst1, fst2, type, \
2910 &env->active_fpu.fp_status); \
2911 fsth0 = float32_muladd(fsth0, fsth1, fsth2, type, \
2912 &env->active_fpu.fp_status); \
2913 update_fcr31(env, GETPC()); \
2914 return ((uint64_t)fsth0 << 32) | fst0; \
2917 FLOAT_FMA(msub, float_muladd_negate_c)
2918 FLOAT_FMA(nmadd, float_muladd_negate_result)
2919 FLOAT_FMA(nmsub, float_muladd_negate_result | float_muladd_negate_c)
2922 /* MIPS specific binary operations */
2923 uint64_t helper_float_recip2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
2925 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
2926 fdt2 = float64_chs(float64_sub(fdt2, float64_one, &env->active_fpu.fp_status));
2927 update_fcr31(env, GETPC());
2931 uint32_t helper_float_recip2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
2933 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2934 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
2935 update_fcr31(env, GETPC());
2939 uint64_t helper_float_recip2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
2941 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
2942 uint32_t fsth0 = fdt0 >> 32;
2943 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
2944 uint32_t fsth2 = fdt2 >> 32;
2946 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2947 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
2948 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
2949 fsth2 = float32_chs(float32_sub(fsth2, float32_one, &env->active_fpu.fp_status));
2950 update_fcr31(env, GETPC());
2951 return ((uint64_t)fsth2 << 32) | fst2;
2954 uint64_t helper_float_rsqrt2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
2956 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
2957 fdt2 = float64_sub(fdt2, float64_one, &env->active_fpu.fp_status);
2958 fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
2959 update_fcr31(env, GETPC());
2963 uint32_t helper_float_rsqrt2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
2965 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2966 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
2967 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
2968 update_fcr31(env, GETPC());
2972 uint64_t helper_float_rsqrt2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
2974 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
2975 uint32_t fsth0 = fdt0 >> 32;
2976 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
2977 uint32_t fsth2 = fdt2 >> 32;
2979 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2980 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
2981 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
2982 fsth2 = float32_sub(fsth2, float32_one, &env->active_fpu.fp_status);
2983 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
2984 fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
2985 update_fcr31(env, GETPC());
2986 return ((uint64_t)fsth2 << 32) | fst2;
2989 uint64_t helper_float_addr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
2991 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
2992 uint32_t fsth0 = fdt0 >> 32;
2993 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
2994 uint32_t fsth1 = fdt1 >> 32;
2998 fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
2999 fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
3000 update_fcr31(env, GETPC());
3001 return ((uint64_t)fsth2 << 32) | fst2;
3004 uint64_t helper_float_mulr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3006 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3007 uint32_t fsth0 = fdt0 >> 32;
3008 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3009 uint32_t fsth1 = fdt1 >> 32;
3013 fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
3014 fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
3015 update_fcr31(env, GETPC());
3016 return ((uint64_t)fsth2 << 32) | fst2;
3019 /* compare operations */
3020 #define FOP_COND_D(op, cond) \
3021 void helper_cmp_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3022 uint64_t fdt1, int cc) \
3026 update_fcr31(env, GETPC()); \
3028 SET_FP_COND(cc, env->active_fpu); \
3030 CLEAR_FP_COND(cc, env->active_fpu); \
3032 void helper_cmpabs_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3033 uint64_t fdt1, int cc) \
3036 fdt0 = float64_abs(fdt0); \
3037 fdt1 = float64_abs(fdt1); \
3039 update_fcr31(env, GETPC()); \
3041 SET_FP_COND(cc, env->active_fpu); \
3043 CLEAR_FP_COND(cc, env->active_fpu); \
3046 /* NOTE: the comma operator will make "cond" to eval to false,
3047 * but float64_unordered_quiet() is still called. */
3048 FOP_COND_D(f, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3049 FOP_COND_D(un, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status))
3050 FOP_COND_D(eq, float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3051 FOP_COND_D(ueq, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3052 FOP_COND_D(olt, float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3053 FOP_COND_D(ult, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3054 FOP_COND_D(ole, float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3055 FOP_COND_D(ule, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3056 /* NOTE: the comma operator will make "cond" to eval to false,
3057 * but float64_unordered() is still called. */
3058 FOP_COND_D(sf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3059 FOP_COND_D(ngle,float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status))
3060 FOP_COND_D(seq, float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3061 FOP_COND_D(ngl, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3062 FOP_COND_D(lt, float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3063 FOP_COND_D(nge, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3064 FOP_COND_D(le, float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3065 FOP_COND_D(ngt, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3067 #define FOP_COND_S(op, cond) \
3068 void helper_cmp_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3069 uint32_t fst1, int cc) \
3073 update_fcr31(env, GETPC()); \
3075 SET_FP_COND(cc, env->active_fpu); \
3077 CLEAR_FP_COND(cc, env->active_fpu); \
3079 void helper_cmpabs_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3080 uint32_t fst1, int cc) \
3083 fst0 = float32_abs(fst0); \
3084 fst1 = float32_abs(fst1); \
3086 update_fcr31(env, GETPC()); \
3088 SET_FP_COND(cc, env->active_fpu); \
3090 CLEAR_FP_COND(cc, env->active_fpu); \
3093 /* NOTE: the comma operator will make "cond" to eval to false,
3094 * but float32_unordered_quiet() is still called. */
3095 FOP_COND_S(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3096 FOP_COND_S(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status))
3097 FOP_COND_S(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3098 FOP_COND_S(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3099 FOP_COND_S(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3100 FOP_COND_S(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3101 FOP_COND_S(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3102 FOP_COND_S(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3103 /* NOTE: the comma operator will make "cond" to eval to false,
3104 * but float32_unordered() is still called. */
3105 FOP_COND_S(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3106 FOP_COND_S(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status))
3107 FOP_COND_S(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3108 FOP_COND_S(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3109 FOP_COND_S(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3110 FOP_COND_S(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3111 FOP_COND_S(le, float32_le(fst0, fst1, &env->active_fpu.fp_status))
3112 FOP_COND_S(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
3114 #define FOP_COND_PS(op, condl, condh) \
3115 void helper_cmp_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3116 uint64_t fdt1, int cc) \
3118 uint32_t fst0, fsth0, fst1, fsth1; \
3120 fst0 = fdt0 & 0XFFFFFFFF; \
3121 fsth0 = fdt0 >> 32; \
3122 fst1 = fdt1 & 0XFFFFFFFF; \
3123 fsth1 = fdt1 >> 32; \
3126 update_fcr31(env, GETPC()); \
3128 SET_FP_COND(cc, env->active_fpu); \
3130 CLEAR_FP_COND(cc, env->active_fpu); \
3132 SET_FP_COND(cc + 1, env->active_fpu); \
3134 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3136 void helper_cmpabs_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3137 uint64_t fdt1, int cc) \
3139 uint32_t fst0, fsth0, fst1, fsth1; \
3141 fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
3142 fsth0 = float32_abs(fdt0 >> 32); \
3143 fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
3144 fsth1 = float32_abs(fdt1 >> 32); \
3147 update_fcr31(env, GETPC()); \
3149 SET_FP_COND(cc, env->active_fpu); \
3151 CLEAR_FP_COND(cc, env->active_fpu); \
3153 SET_FP_COND(cc + 1, env->active_fpu); \
3155 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3158 /* NOTE: the comma operator will make "cond" to eval to false,
3159 * but float32_unordered_quiet() is still called. */
3160 FOP_COND_PS(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0),
3161 (float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3162 FOP_COND_PS(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status),
3163 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status))
3164 FOP_COND_PS(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3165 float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3166 FOP_COND_PS(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3167 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3168 FOP_COND_PS(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3169 float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3170 FOP_COND_PS(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3171 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3172 FOP_COND_PS(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3173 float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3174 FOP_COND_PS(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3175 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3176 /* NOTE: the comma operator will make "cond" to eval to false,
3177 * but float32_unordered() is still called. */
3178 FOP_COND_PS(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0),
3179 (float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3180 FOP_COND_PS(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status),
3181 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status))
3182 FOP_COND_PS(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3183 float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3184 FOP_COND_PS(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3185 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3186 FOP_COND_PS(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3187 float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3188 FOP_COND_PS(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3189 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3190 FOP_COND_PS(le, float32_le(fst0, fst1, &env->active_fpu.fp_status),
3191 float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3192 FOP_COND_PS(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
3193 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))