etsec: Move etsec_can_receive into etsec_receive
[qemu/ar7.git] / target-mips / op_helper.c
blob9c28631dc1ac9622b761fd6ab044c38779c9b5b0
1 /*
2 * MIPS emulation helpers for qemu.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include "cpu.h"
21 #include "qemu/host-utils.h"
22 #include "exec/helper-proto.h"
23 #include "exec/cpu_ldst.h"
24 #include "sysemu/kvm.h"
26 #ifndef CONFIG_USER_ONLY
27 static inline void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global);
28 #endif
30 /*****************************************************************************/
31 /* Exceptions processing helpers */
33 static inline void QEMU_NORETURN do_raise_exception_err(CPUMIPSState *env,
34 uint32_t exception,
35 int error_code,
36 uintptr_t pc)
38 CPUState *cs = CPU(mips_env_get_cpu(env));
40 if (exception < EXCP_SC) {
41 qemu_log("%s: %d %d\n", __func__, exception, error_code);
43 cs->exception_index = exception;
44 env->error_code = error_code;
46 if (pc) {
47 /* now we have a real cpu fault */
48 cpu_restore_state(cs, pc);
51 cpu_loop_exit(cs);
54 static inline void QEMU_NORETURN do_raise_exception(CPUMIPSState *env,
55 uint32_t exception,
56 uintptr_t pc)
58 do_raise_exception_err(env, exception, 0, pc);
61 void helper_raise_exception_err(CPUMIPSState *env, uint32_t exception,
62 int error_code)
64 do_raise_exception_err(env, exception, error_code, 0);
67 void helper_raise_exception(CPUMIPSState *env, uint32_t exception)
69 do_raise_exception(env, exception, 0);
72 #if defined(CONFIG_USER_ONLY)
73 #define HELPER_LD(name, insn, type) \
74 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
75 int mem_idx) \
76 { \
77 return (type) cpu_##insn##_data(env, addr); \
79 #else
80 #define HELPER_LD(name, insn, type) \
81 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
82 int mem_idx) \
83 { \
84 switch (mem_idx) \
85 { \
86 case 0: return (type) cpu_##insn##_kernel(env, addr); break; \
87 case 1: return (type) cpu_##insn##_super(env, addr); break; \
88 default: \
89 case 2: return (type) cpu_##insn##_user(env, addr); break; \
90 } \
92 #endif
93 HELPER_LD(lw, ldl, int32_t)
94 #if defined(TARGET_MIPS64)
95 HELPER_LD(ld, ldq, int64_t)
96 #endif
97 #undef HELPER_LD
99 #if defined(CONFIG_USER_ONLY)
100 #define HELPER_ST(name, insn, type) \
101 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
102 type val, int mem_idx) \
104 cpu_##insn##_data(env, addr, val); \
106 #else
107 #define HELPER_ST(name, insn, type) \
108 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
109 type val, int mem_idx) \
111 switch (mem_idx) \
113 case 0: cpu_##insn##_kernel(env, addr, val); break; \
114 case 1: cpu_##insn##_super(env, addr, val); break; \
115 default: \
116 case 2: cpu_##insn##_user(env, addr, val); break; \
119 #endif
120 HELPER_ST(sb, stb, uint8_t)
121 HELPER_ST(sw, stl, uint32_t)
122 #if defined(TARGET_MIPS64)
123 HELPER_ST(sd, stq, uint64_t)
124 #endif
125 #undef HELPER_ST
127 target_ulong helper_clo (target_ulong arg1)
129 return clo32(arg1);
132 target_ulong helper_clz (target_ulong arg1)
134 return clz32(arg1);
137 #if defined(TARGET_MIPS64)
138 target_ulong helper_dclo (target_ulong arg1)
140 return clo64(arg1);
143 target_ulong helper_dclz (target_ulong arg1)
145 return clz64(arg1);
147 #endif /* TARGET_MIPS64 */
149 /* 64 bits arithmetic for 32 bits hosts */
150 static inline uint64_t get_HILO(CPUMIPSState *env)
152 return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
155 static inline target_ulong set_HIT0_LO(CPUMIPSState *env, uint64_t HILO)
157 target_ulong tmp;
158 env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
159 tmp = env->active_tc.HI[0] = (int32_t)(HILO >> 32);
160 return tmp;
163 static inline target_ulong set_HI_LOT0(CPUMIPSState *env, uint64_t HILO)
165 target_ulong tmp = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
166 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
167 return tmp;
170 /* Multiplication variants of the vr54xx. */
171 target_ulong helper_muls(CPUMIPSState *env, target_ulong arg1,
172 target_ulong arg2)
174 return set_HI_LOT0(env, 0 - ((int64_t)(int32_t)arg1 *
175 (int64_t)(int32_t)arg2));
178 target_ulong helper_mulsu(CPUMIPSState *env, target_ulong arg1,
179 target_ulong arg2)
181 return set_HI_LOT0(env, 0 - (uint64_t)(uint32_t)arg1 *
182 (uint64_t)(uint32_t)arg2);
185 target_ulong helper_macc(CPUMIPSState *env, target_ulong arg1,
186 target_ulong arg2)
188 return set_HI_LOT0(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
189 (int64_t)(int32_t)arg2);
192 target_ulong helper_macchi(CPUMIPSState *env, target_ulong arg1,
193 target_ulong arg2)
195 return set_HIT0_LO(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
196 (int64_t)(int32_t)arg2);
199 target_ulong helper_maccu(CPUMIPSState *env, target_ulong arg1,
200 target_ulong arg2)
202 return set_HI_LOT0(env, (uint64_t)get_HILO(env) +
203 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
206 target_ulong helper_macchiu(CPUMIPSState *env, target_ulong arg1,
207 target_ulong arg2)
209 return set_HIT0_LO(env, (uint64_t)get_HILO(env) +
210 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
213 target_ulong helper_msac(CPUMIPSState *env, target_ulong arg1,
214 target_ulong arg2)
216 return set_HI_LOT0(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
217 (int64_t)(int32_t)arg2);
220 target_ulong helper_msachi(CPUMIPSState *env, target_ulong arg1,
221 target_ulong arg2)
223 return set_HIT0_LO(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
224 (int64_t)(int32_t)arg2);
227 target_ulong helper_msacu(CPUMIPSState *env, target_ulong arg1,
228 target_ulong arg2)
230 return set_HI_LOT0(env, (uint64_t)get_HILO(env) -
231 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
234 target_ulong helper_msachiu(CPUMIPSState *env, target_ulong arg1,
235 target_ulong arg2)
237 return set_HIT0_LO(env, (uint64_t)get_HILO(env) -
238 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
241 target_ulong helper_mulhi(CPUMIPSState *env, target_ulong arg1,
242 target_ulong arg2)
244 return set_HIT0_LO(env, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
247 target_ulong helper_mulhiu(CPUMIPSState *env, target_ulong arg1,
248 target_ulong arg2)
250 return set_HIT0_LO(env, (uint64_t)(uint32_t)arg1 *
251 (uint64_t)(uint32_t)arg2);
254 target_ulong helper_mulshi(CPUMIPSState *env, target_ulong arg1,
255 target_ulong arg2)
257 return set_HIT0_LO(env, 0 - (int64_t)(int32_t)arg1 *
258 (int64_t)(int32_t)arg2);
261 target_ulong helper_mulshiu(CPUMIPSState *env, target_ulong arg1,
262 target_ulong arg2)
264 return set_HIT0_LO(env, 0 - (uint64_t)(uint32_t)arg1 *
265 (uint64_t)(uint32_t)arg2);
268 static inline target_ulong bitswap(target_ulong v)
270 v = ((v >> 1) & (target_ulong)0x5555555555555555ULL) |
271 ((v & (target_ulong)0x5555555555555555ULL) << 1);
272 v = ((v >> 2) & (target_ulong)0x3333333333333333ULL) |
273 ((v & (target_ulong)0x3333333333333333ULL) << 2);
274 v = ((v >> 4) & (target_ulong)0x0F0F0F0F0F0F0F0FULL) |
275 ((v & (target_ulong)0x0F0F0F0F0F0F0F0FULL) << 4);
276 return v;
279 #ifdef TARGET_MIPS64
280 target_ulong helper_dbitswap(target_ulong rt)
282 return bitswap(rt);
284 #endif
286 target_ulong helper_bitswap(target_ulong rt)
288 return (int32_t)bitswap(rt);
291 #ifndef CONFIG_USER_ONLY
293 static inline hwaddr do_translate_address(CPUMIPSState *env,
294 target_ulong address,
295 int rw)
297 hwaddr lladdr;
299 lladdr = cpu_mips_translate_address(env, address, rw);
301 if (lladdr == -1LL) {
302 cpu_loop_exit(CPU(mips_env_get_cpu(env)));
303 } else {
304 return lladdr;
308 #define HELPER_LD_ATOMIC(name, insn, almask) \
309 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg, int mem_idx) \
311 if (arg & almask) { \
312 env->CP0_BadVAddr = arg; \
313 helper_raise_exception(env, EXCP_AdEL); \
315 env->lladdr = do_translate_address(env, arg, 0); \
316 env->llval = do_##insn(env, arg, mem_idx); \
317 return env->llval; \
319 HELPER_LD_ATOMIC(ll, lw, 0x3)
320 #ifdef TARGET_MIPS64
321 HELPER_LD_ATOMIC(lld, ld, 0x7)
322 #endif
323 #undef HELPER_LD_ATOMIC
325 #define HELPER_ST_ATOMIC(name, ld_insn, st_insn, almask) \
326 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg1, \
327 target_ulong arg2, int mem_idx) \
329 target_long tmp; \
331 if (arg2 & almask) { \
332 env->CP0_BadVAddr = arg2; \
333 helper_raise_exception(env, EXCP_AdES); \
335 if (do_translate_address(env, arg2, 1) == env->lladdr) { \
336 tmp = do_##ld_insn(env, arg2, mem_idx); \
337 if (tmp == env->llval) { \
338 do_##st_insn(env, arg2, arg1, mem_idx); \
339 return 1; \
342 return 0; \
344 HELPER_ST_ATOMIC(sc, lw, sw, 0x3)
345 #ifdef TARGET_MIPS64
346 HELPER_ST_ATOMIC(scd, ld, sd, 0x7)
347 #endif
348 #undef HELPER_ST_ATOMIC
349 #endif
351 #ifdef TARGET_WORDS_BIGENDIAN
352 #define GET_LMASK(v) ((v) & 3)
353 #define GET_OFFSET(addr, offset) (addr + (offset))
354 #else
355 #define GET_LMASK(v) (((v) & 3) ^ 3)
356 #define GET_OFFSET(addr, offset) (addr - (offset))
357 #endif
359 void helper_swl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
360 int mem_idx)
362 do_sb(env, arg2, (uint8_t)(arg1 >> 24), mem_idx);
364 if (GET_LMASK(arg2) <= 2)
365 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16), mem_idx);
367 if (GET_LMASK(arg2) <= 1)
368 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8), mem_idx);
370 if (GET_LMASK(arg2) == 0)
371 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)arg1, mem_idx);
374 void helper_swr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
375 int mem_idx)
377 do_sb(env, arg2, (uint8_t)arg1, mem_idx);
379 if (GET_LMASK(arg2) >= 1)
380 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
382 if (GET_LMASK(arg2) >= 2)
383 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
385 if (GET_LMASK(arg2) == 3)
386 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
389 #if defined(TARGET_MIPS64)
390 /* "half" load and stores. We must do the memory access inline,
391 or fault handling won't work. */
393 #ifdef TARGET_WORDS_BIGENDIAN
394 #define GET_LMASK64(v) ((v) & 7)
395 #else
396 #define GET_LMASK64(v) (((v) & 7) ^ 7)
397 #endif
399 void helper_sdl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
400 int mem_idx)
402 do_sb(env, arg2, (uint8_t)(arg1 >> 56), mem_idx);
404 if (GET_LMASK64(arg2) <= 6)
405 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48), mem_idx);
407 if (GET_LMASK64(arg2) <= 5)
408 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40), mem_idx);
410 if (GET_LMASK64(arg2) <= 4)
411 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32), mem_idx);
413 if (GET_LMASK64(arg2) <= 3)
414 do_sb(env, GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24), mem_idx);
416 if (GET_LMASK64(arg2) <= 2)
417 do_sb(env, GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16), mem_idx);
419 if (GET_LMASK64(arg2) <= 1)
420 do_sb(env, GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8), mem_idx);
422 if (GET_LMASK64(arg2) <= 0)
423 do_sb(env, GET_OFFSET(arg2, 7), (uint8_t)arg1, mem_idx);
426 void helper_sdr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
427 int mem_idx)
429 do_sb(env, arg2, (uint8_t)arg1, mem_idx);
431 if (GET_LMASK64(arg2) >= 1)
432 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
434 if (GET_LMASK64(arg2) >= 2)
435 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
437 if (GET_LMASK64(arg2) >= 3)
438 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
440 if (GET_LMASK64(arg2) >= 4)
441 do_sb(env, GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32), mem_idx);
443 if (GET_LMASK64(arg2) >= 5)
444 do_sb(env, GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40), mem_idx);
446 if (GET_LMASK64(arg2) >= 6)
447 do_sb(env, GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48), mem_idx);
449 if (GET_LMASK64(arg2) == 7)
450 do_sb(env, GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56), mem_idx);
452 #endif /* TARGET_MIPS64 */
454 static const int multiple_regs[] = { 16, 17, 18, 19, 20, 21, 22, 23, 30 };
456 void helper_lwm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
457 uint32_t mem_idx)
459 target_ulong base_reglist = reglist & 0xf;
460 target_ulong do_r31 = reglist & 0x10;
462 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
463 target_ulong i;
465 for (i = 0; i < base_reglist; i++) {
466 env->active_tc.gpr[multiple_regs[i]] =
467 (target_long)do_lw(env, addr, mem_idx);
468 addr += 4;
472 if (do_r31) {
473 env->active_tc.gpr[31] = (target_long)do_lw(env, addr, mem_idx);
477 void helper_swm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
478 uint32_t mem_idx)
480 target_ulong base_reglist = reglist & 0xf;
481 target_ulong do_r31 = reglist & 0x10;
483 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
484 target_ulong i;
486 for (i = 0; i < base_reglist; i++) {
487 do_sw(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx);
488 addr += 4;
492 if (do_r31) {
493 do_sw(env, addr, env->active_tc.gpr[31], mem_idx);
497 #if defined(TARGET_MIPS64)
498 void helper_ldm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
499 uint32_t mem_idx)
501 target_ulong base_reglist = reglist & 0xf;
502 target_ulong do_r31 = reglist & 0x10;
504 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
505 target_ulong i;
507 for (i = 0; i < base_reglist; i++) {
508 env->active_tc.gpr[multiple_regs[i]] = do_ld(env, addr, mem_idx);
509 addr += 8;
513 if (do_r31) {
514 env->active_tc.gpr[31] = do_ld(env, addr, mem_idx);
518 void helper_sdm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
519 uint32_t mem_idx)
521 target_ulong base_reglist = reglist & 0xf;
522 target_ulong do_r31 = reglist & 0x10;
524 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
525 target_ulong i;
527 for (i = 0; i < base_reglist; i++) {
528 do_sd(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx);
529 addr += 8;
533 if (do_r31) {
534 do_sd(env, addr, env->active_tc.gpr[31], mem_idx);
537 #endif
539 #ifndef CONFIG_USER_ONLY
540 /* SMP helpers. */
541 static bool mips_vpe_is_wfi(MIPSCPU *c)
543 CPUState *cpu = CPU(c);
544 CPUMIPSState *env = &c->env;
546 /* If the VPE is halted but otherwise active, it means it's waiting for
547 an interrupt. */
548 return cpu->halted && mips_vpe_active(env);
551 static inline void mips_vpe_wake(MIPSCPU *c)
553 /* Dont set ->halted = 0 directly, let it be done via cpu_has_work
554 because there might be other conditions that state that c should
555 be sleeping. */
556 cpu_interrupt(CPU(c), CPU_INTERRUPT_WAKE);
559 static inline void mips_vpe_sleep(MIPSCPU *cpu)
561 CPUState *cs = CPU(cpu);
563 /* The VPE was shut off, really go to bed.
564 Reset any old _WAKE requests. */
565 cs->halted = 1;
566 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
569 static inline void mips_tc_wake(MIPSCPU *cpu, int tc)
571 CPUMIPSState *c = &cpu->env;
573 /* FIXME: TC reschedule. */
574 if (mips_vpe_active(c) && !mips_vpe_is_wfi(cpu)) {
575 mips_vpe_wake(cpu);
579 static inline void mips_tc_sleep(MIPSCPU *cpu, int tc)
581 CPUMIPSState *c = &cpu->env;
583 /* FIXME: TC reschedule. */
584 if (!mips_vpe_active(c)) {
585 mips_vpe_sleep(cpu);
590 * mips_cpu_map_tc:
591 * @env: CPU from which mapping is performed.
592 * @tc: Should point to an int with the value of the global TC index.
594 * This function will transform @tc into a local index within the
595 * returned #CPUMIPSState.
597 /* FIXME: This code assumes that all VPEs have the same number of TCs,
598 which depends on runtime setup. Can probably be fixed by
599 walking the list of CPUMIPSStates. */
600 static CPUMIPSState *mips_cpu_map_tc(CPUMIPSState *env, int *tc)
602 MIPSCPU *cpu;
603 CPUState *cs;
604 CPUState *other_cs;
605 int vpe_idx;
606 int tc_idx = *tc;
608 if (!(env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))) {
609 /* Not allowed to address other CPUs. */
610 *tc = env->current_tc;
611 return env;
614 cs = CPU(mips_env_get_cpu(env));
615 vpe_idx = tc_idx / cs->nr_threads;
616 *tc = tc_idx % cs->nr_threads;
617 other_cs = qemu_get_cpu(vpe_idx);
618 if (other_cs == NULL) {
619 return env;
621 cpu = MIPS_CPU(other_cs);
622 return &cpu->env;
625 /* The per VPE CP0_Status register shares some fields with the per TC
626 CP0_TCStatus registers. These fields are wired to the same registers,
627 so changes to either of them should be reflected on both registers.
629 Also, EntryHi shares the bottom 8 bit ASID with TCStauts.
631 These helper call synchronizes the regs for a given cpu. */
633 /* Called for updates to CP0_Status. Defined in "cpu.h" for gdbstub.c. */
634 /* static inline void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu,
635 int tc); */
637 /* Called for updates to CP0_TCStatus. */
638 static void sync_c0_tcstatus(CPUMIPSState *cpu, int tc,
639 target_ulong v)
641 uint32_t status;
642 uint32_t tcu, tmx, tasid, tksu;
643 uint32_t mask = ((1U << CP0St_CU3)
644 | (1 << CP0St_CU2)
645 | (1 << CP0St_CU1)
646 | (1 << CP0St_CU0)
647 | (1 << CP0St_MX)
648 | (3 << CP0St_KSU));
650 tcu = (v >> CP0TCSt_TCU0) & 0xf;
651 tmx = (v >> CP0TCSt_TMX) & 0x1;
652 tasid = v & 0xff;
653 tksu = (v >> CP0TCSt_TKSU) & 0x3;
655 status = tcu << CP0St_CU0;
656 status |= tmx << CP0St_MX;
657 status |= tksu << CP0St_KSU;
659 cpu->CP0_Status &= ~mask;
660 cpu->CP0_Status |= status;
662 /* Sync the TASID with EntryHi. */
663 cpu->CP0_EntryHi &= ~0xff;
664 cpu->CP0_EntryHi |= tasid;
666 compute_hflags(cpu);
669 /* Called for updates to CP0_EntryHi. */
670 static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
672 int32_t *tcst;
673 uint32_t asid, v = cpu->CP0_EntryHi;
675 asid = v & 0xff;
677 if (tc == cpu->current_tc) {
678 tcst = &cpu->active_tc.CP0_TCStatus;
679 } else {
680 tcst = &cpu->tcs[tc].CP0_TCStatus;
683 *tcst &= ~0xff;
684 *tcst |= asid;
687 /* CP0 helpers */
688 target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
690 return env->mvp->CP0_MVPControl;
693 target_ulong helper_mfc0_mvpconf0(CPUMIPSState *env)
695 return env->mvp->CP0_MVPConf0;
698 target_ulong helper_mfc0_mvpconf1(CPUMIPSState *env)
700 return env->mvp->CP0_MVPConf1;
703 target_ulong helper_mfc0_random(CPUMIPSState *env)
705 return (int32_t)cpu_mips_get_random(env);
708 target_ulong helper_mfc0_tcstatus(CPUMIPSState *env)
710 return env->active_tc.CP0_TCStatus;
713 target_ulong helper_mftc0_tcstatus(CPUMIPSState *env)
715 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
716 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
718 if (other_tc == other->current_tc)
719 return other->active_tc.CP0_TCStatus;
720 else
721 return other->tcs[other_tc].CP0_TCStatus;
724 target_ulong helper_mfc0_tcbind(CPUMIPSState *env)
726 return env->active_tc.CP0_TCBind;
729 target_ulong helper_mftc0_tcbind(CPUMIPSState *env)
731 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
732 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
734 if (other_tc == other->current_tc)
735 return other->active_tc.CP0_TCBind;
736 else
737 return other->tcs[other_tc].CP0_TCBind;
740 target_ulong helper_mfc0_tcrestart(CPUMIPSState *env)
742 return env->active_tc.PC;
745 target_ulong helper_mftc0_tcrestart(CPUMIPSState *env)
747 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
748 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
750 if (other_tc == other->current_tc)
751 return other->active_tc.PC;
752 else
753 return other->tcs[other_tc].PC;
756 target_ulong helper_mfc0_tchalt(CPUMIPSState *env)
758 return env->active_tc.CP0_TCHalt;
761 target_ulong helper_mftc0_tchalt(CPUMIPSState *env)
763 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
764 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
766 if (other_tc == other->current_tc)
767 return other->active_tc.CP0_TCHalt;
768 else
769 return other->tcs[other_tc].CP0_TCHalt;
772 target_ulong helper_mfc0_tccontext(CPUMIPSState *env)
774 return env->active_tc.CP0_TCContext;
777 target_ulong helper_mftc0_tccontext(CPUMIPSState *env)
779 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
780 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
782 if (other_tc == other->current_tc)
783 return other->active_tc.CP0_TCContext;
784 else
785 return other->tcs[other_tc].CP0_TCContext;
788 target_ulong helper_mfc0_tcschedule(CPUMIPSState *env)
790 return env->active_tc.CP0_TCSchedule;
793 target_ulong helper_mftc0_tcschedule(CPUMIPSState *env)
795 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
796 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
798 if (other_tc == other->current_tc)
799 return other->active_tc.CP0_TCSchedule;
800 else
801 return other->tcs[other_tc].CP0_TCSchedule;
804 target_ulong helper_mfc0_tcschefback(CPUMIPSState *env)
806 return env->active_tc.CP0_TCScheFBack;
809 target_ulong helper_mftc0_tcschefback(CPUMIPSState *env)
811 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
812 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
814 if (other_tc == other->current_tc)
815 return other->active_tc.CP0_TCScheFBack;
816 else
817 return other->tcs[other_tc].CP0_TCScheFBack;
820 target_ulong helper_mfc0_count(CPUMIPSState *env)
822 return (int32_t)cpu_mips_get_count(env);
825 target_ulong helper_mftc0_entryhi(CPUMIPSState *env)
827 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
828 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
830 return other->CP0_EntryHi;
833 target_ulong helper_mftc0_cause(CPUMIPSState *env)
835 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
836 int32_t tccause;
837 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
839 if (other_tc == other->current_tc) {
840 tccause = other->CP0_Cause;
841 } else {
842 tccause = other->CP0_Cause;
845 return tccause;
848 target_ulong helper_mftc0_status(CPUMIPSState *env)
850 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
851 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
853 return other->CP0_Status;
856 target_ulong helper_mfc0_lladdr(CPUMIPSState *env)
858 return (int32_t)(env->lladdr >> env->CP0_LLAddr_shift);
861 target_ulong helper_mfc0_watchlo(CPUMIPSState *env, uint32_t sel)
863 return (int32_t)env->CP0_WatchLo[sel];
866 target_ulong helper_mfc0_watchhi(CPUMIPSState *env, uint32_t sel)
868 return env->CP0_WatchHi[sel];
871 target_ulong helper_mfc0_debug(CPUMIPSState *env)
873 target_ulong t0 = env->CP0_Debug;
874 if (env->hflags & MIPS_HFLAG_DM)
875 t0 |= 1 << CP0DB_DM;
877 return t0;
880 target_ulong helper_mftc0_debug(CPUMIPSState *env)
882 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
883 int32_t tcstatus;
884 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
886 if (other_tc == other->current_tc)
887 tcstatus = other->active_tc.CP0_Debug_tcstatus;
888 else
889 tcstatus = other->tcs[other_tc].CP0_Debug_tcstatus;
891 /* XXX: Might be wrong, check with EJTAG spec. */
892 return (other->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
893 (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
896 #if defined(TARGET_MIPS64)
897 target_ulong helper_dmfc0_tcrestart(CPUMIPSState *env)
899 return env->active_tc.PC;
902 target_ulong helper_dmfc0_tchalt(CPUMIPSState *env)
904 return env->active_tc.CP0_TCHalt;
907 target_ulong helper_dmfc0_tccontext(CPUMIPSState *env)
909 return env->active_tc.CP0_TCContext;
912 target_ulong helper_dmfc0_tcschedule(CPUMIPSState *env)
914 return env->active_tc.CP0_TCSchedule;
917 target_ulong helper_dmfc0_tcschefback(CPUMIPSState *env)
919 return env->active_tc.CP0_TCScheFBack;
922 target_ulong helper_dmfc0_lladdr(CPUMIPSState *env)
924 return env->lladdr >> env->CP0_LLAddr_shift;
927 target_ulong helper_dmfc0_watchlo(CPUMIPSState *env, uint32_t sel)
929 return env->CP0_WatchLo[sel];
931 #endif /* TARGET_MIPS64 */
933 void helper_mtc0_index(CPUMIPSState *env, target_ulong arg1)
935 uint32_t index_p = env->CP0_Index & 0x80000000;
936 uint32_t tlb_index = arg1 & 0x7fffffff;
937 if (tlb_index < env->tlb->nb_tlb) {
938 if (env->insn_flags & ISA_MIPS32R6) {
939 index_p |= arg1 & 0x80000000;
941 env->CP0_Index = index_p | tlb_index;
945 void helper_mtc0_mvpcontrol(CPUMIPSState *env, target_ulong arg1)
947 uint32_t mask = 0;
948 uint32_t newval;
950 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
951 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
952 (1 << CP0MVPCo_EVP);
953 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
954 mask |= (1 << CP0MVPCo_STLB);
955 newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
957 // TODO: Enable/disable shared TLB, enable/disable VPEs.
959 env->mvp->CP0_MVPControl = newval;
962 void helper_mtc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
964 uint32_t mask;
965 uint32_t newval;
967 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
968 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
969 newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
971 /* Yield scheduler intercept not implemented. */
972 /* Gating storage scheduler intercept not implemented. */
974 // TODO: Enable/disable TCs.
976 env->CP0_VPEControl = newval;
979 void helper_mttc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
981 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
982 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
983 uint32_t mask;
984 uint32_t newval;
986 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
987 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
988 newval = (other->CP0_VPEControl & ~mask) | (arg1 & mask);
990 /* TODO: Enable/disable TCs. */
992 other->CP0_VPEControl = newval;
995 target_ulong helper_mftc0_vpecontrol(CPUMIPSState *env)
997 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
998 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
999 /* FIXME: Mask away return zero on read bits. */
1000 return other->CP0_VPEControl;
1003 target_ulong helper_mftc0_vpeconf0(CPUMIPSState *env)
1005 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1006 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1008 return other->CP0_VPEConf0;
1011 void helper_mtc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1013 uint32_t mask = 0;
1014 uint32_t newval;
1016 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
1017 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
1018 mask |= (0xff << CP0VPEC0_XTC);
1019 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1021 newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1023 // TODO: TC exclusive handling due to ERL/EXL.
1025 env->CP0_VPEConf0 = newval;
1028 void helper_mttc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1030 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1031 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1032 uint32_t mask = 0;
1033 uint32_t newval;
1035 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1036 newval = (other->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1038 /* TODO: TC exclusive handling due to ERL/EXL. */
1039 other->CP0_VPEConf0 = newval;
1042 void helper_mtc0_vpeconf1(CPUMIPSState *env, target_ulong arg1)
1044 uint32_t mask = 0;
1045 uint32_t newval;
1047 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1048 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
1049 (0xff << CP0VPEC1_NCP1);
1050 newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
1052 /* UDI not implemented. */
1053 /* CP2 not implemented. */
1055 // TODO: Handle FPU (CP1) binding.
1057 env->CP0_VPEConf1 = newval;
1060 void helper_mtc0_yqmask(CPUMIPSState *env, target_ulong arg1)
1062 /* Yield qualifier inputs not implemented. */
1063 env->CP0_YQMask = 0x00000000;
1066 void helper_mtc0_vpeopt(CPUMIPSState *env, target_ulong arg1)
1068 env->CP0_VPEOpt = arg1 & 0x0000ffff;
1071 #define MTC0_ENTRYLO_MASK(env) ((env->PAMask >> 6) & 0x3FFFFFFF)
1073 void helper_mtc0_entrylo0(CPUMIPSState *env, target_ulong arg1)
1075 /* 1k pages not implemented */
1076 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1077 env->CP0_EntryLo0 = (arg1 & MTC0_ENTRYLO_MASK(env))
1078 | (rxi << (CP0EnLo_XI - 30));
1081 #if defined(TARGET_MIPS64)
1082 #define DMTC0_ENTRYLO_MASK(env) (env->PAMask >> 6)
1084 void helper_dmtc0_entrylo0(CPUMIPSState *env, uint64_t arg1)
1086 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1087 env->CP0_EntryLo0 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1089 #endif
1091 void helper_mtc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1093 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
1094 uint32_t newval;
1096 newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
1098 env->active_tc.CP0_TCStatus = newval;
1099 sync_c0_tcstatus(env, env->current_tc, newval);
1102 void helper_mttc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1104 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1105 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1107 if (other_tc == other->current_tc)
1108 other->active_tc.CP0_TCStatus = arg1;
1109 else
1110 other->tcs[other_tc].CP0_TCStatus = arg1;
1111 sync_c0_tcstatus(other, other_tc, arg1);
1114 void helper_mtc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1116 uint32_t mask = (1 << CP0TCBd_TBE);
1117 uint32_t newval;
1119 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1120 mask |= (1 << CP0TCBd_CurVPE);
1121 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1122 env->active_tc.CP0_TCBind = newval;
1125 void helper_mttc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1127 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1128 uint32_t mask = (1 << CP0TCBd_TBE);
1129 uint32_t newval;
1130 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1132 if (other->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1133 mask |= (1 << CP0TCBd_CurVPE);
1134 if (other_tc == other->current_tc) {
1135 newval = (other->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1136 other->active_tc.CP0_TCBind = newval;
1137 } else {
1138 newval = (other->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
1139 other->tcs[other_tc].CP0_TCBind = newval;
1143 void helper_mtc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1145 env->active_tc.PC = arg1;
1146 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1147 env->lladdr = 0ULL;
1148 /* MIPS16 not implemented. */
1151 void helper_mttc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1153 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1154 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1156 if (other_tc == other->current_tc) {
1157 other->active_tc.PC = arg1;
1158 other->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1159 other->lladdr = 0ULL;
1160 /* MIPS16 not implemented. */
1161 } else {
1162 other->tcs[other_tc].PC = arg1;
1163 other->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1164 other->lladdr = 0ULL;
1165 /* MIPS16 not implemented. */
1169 void helper_mtc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1171 MIPSCPU *cpu = mips_env_get_cpu(env);
1173 env->active_tc.CP0_TCHalt = arg1 & 0x1;
1175 // TODO: Halt TC / Restart (if allocated+active) TC.
1176 if (env->active_tc.CP0_TCHalt & 1) {
1177 mips_tc_sleep(cpu, env->current_tc);
1178 } else {
1179 mips_tc_wake(cpu, env->current_tc);
1183 void helper_mttc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1185 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1186 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1187 MIPSCPU *other_cpu = mips_env_get_cpu(other);
1189 // TODO: Halt TC / Restart (if allocated+active) TC.
1191 if (other_tc == other->current_tc)
1192 other->active_tc.CP0_TCHalt = arg1;
1193 else
1194 other->tcs[other_tc].CP0_TCHalt = arg1;
1196 if (arg1 & 1) {
1197 mips_tc_sleep(other_cpu, other_tc);
1198 } else {
1199 mips_tc_wake(other_cpu, other_tc);
1203 void helper_mtc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1205 env->active_tc.CP0_TCContext = arg1;
1208 void helper_mttc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1210 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1211 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1213 if (other_tc == other->current_tc)
1214 other->active_tc.CP0_TCContext = arg1;
1215 else
1216 other->tcs[other_tc].CP0_TCContext = arg1;
1219 void helper_mtc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1221 env->active_tc.CP0_TCSchedule = arg1;
1224 void helper_mttc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1226 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1227 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1229 if (other_tc == other->current_tc)
1230 other->active_tc.CP0_TCSchedule = arg1;
1231 else
1232 other->tcs[other_tc].CP0_TCSchedule = arg1;
1235 void helper_mtc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1237 env->active_tc.CP0_TCScheFBack = arg1;
1240 void helper_mttc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1242 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1243 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1245 if (other_tc == other->current_tc)
1246 other->active_tc.CP0_TCScheFBack = arg1;
1247 else
1248 other->tcs[other_tc].CP0_TCScheFBack = arg1;
1251 void helper_mtc0_entrylo1(CPUMIPSState *env, target_ulong arg1)
1253 /* 1k pages not implemented */
1254 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1255 env->CP0_EntryLo1 = (arg1 & MTC0_ENTRYLO_MASK(env))
1256 | (rxi << (CP0EnLo_XI - 30));
1259 #if defined(TARGET_MIPS64)
1260 void helper_dmtc0_entrylo1(CPUMIPSState *env, uint64_t arg1)
1262 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1263 env->CP0_EntryLo1 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1265 #endif
1267 void helper_mtc0_context(CPUMIPSState *env, target_ulong arg1)
1269 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1272 void helper_mtc0_pagemask(CPUMIPSState *env, target_ulong arg1)
1274 uint64_t mask = arg1 >> (TARGET_PAGE_BITS + 1);
1275 if (!(env->insn_flags & ISA_MIPS32R6) || (arg1 == ~0) ||
1276 (mask == 0x0000 || mask == 0x0003 || mask == 0x000F ||
1277 mask == 0x003F || mask == 0x00FF || mask == 0x03FF ||
1278 mask == 0x0FFF || mask == 0x3FFF || mask == 0xFFFF)) {
1279 env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1283 void helper_mtc0_pagegrain(CPUMIPSState *env, target_ulong arg1)
1285 /* SmartMIPS not implemented */
1286 /* 1k pages not implemented */
1287 env->CP0_PageGrain = (arg1 & env->CP0_PageGrain_rw_bitmask) |
1288 (env->CP0_PageGrain & ~env->CP0_PageGrain_rw_bitmask);
1289 compute_hflags(env);
1290 restore_pamask(env);
1293 void helper_mtc0_wired(CPUMIPSState *env, target_ulong arg1)
1295 if (env->insn_flags & ISA_MIPS32R6) {
1296 if (arg1 < env->tlb->nb_tlb) {
1297 env->CP0_Wired = arg1;
1299 } else {
1300 env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1304 void helper_mtc0_srsconf0(CPUMIPSState *env, target_ulong arg1)
1306 env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1309 void helper_mtc0_srsconf1(CPUMIPSState *env, target_ulong arg1)
1311 env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1314 void helper_mtc0_srsconf2(CPUMIPSState *env, target_ulong arg1)
1316 env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1319 void helper_mtc0_srsconf3(CPUMIPSState *env, target_ulong arg1)
1321 env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1324 void helper_mtc0_srsconf4(CPUMIPSState *env, target_ulong arg1)
1326 env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1329 void helper_mtc0_hwrena(CPUMIPSState *env, target_ulong arg1)
1331 uint32_t mask = 0x0000000F;
1333 if (env->CP0_Config3 & (1 << CP0C3_ULRI)) {
1334 mask |= (1 << 29);
1336 if (arg1 & (1 << 29)) {
1337 env->hflags |= MIPS_HFLAG_HWRENA_ULR;
1338 } else {
1339 env->hflags &= ~MIPS_HFLAG_HWRENA_ULR;
1343 env->CP0_HWREna = arg1 & mask;
1346 void helper_mtc0_count(CPUMIPSState *env, target_ulong arg1)
1348 cpu_mips_store_count(env, arg1);
1351 void helper_mtc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1353 target_ulong old, val, mask;
1354 mask = (TARGET_PAGE_MASK << 1) | 0xFF;
1355 if (((env->CP0_Config4 >> CP0C4_IE) & 0x3) >= 2) {
1356 mask |= 1 << CP0EnHi_EHINV;
1359 /* 1k pages not implemented */
1360 #if defined(TARGET_MIPS64)
1361 if (env->insn_flags & ISA_MIPS32R6) {
1362 int entryhi_r = extract64(arg1, 62, 2);
1363 int config0_at = extract32(env->CP0_Config0, 13, 2);
1364 bool no_supervisor = (env->CP0_Status_rw_bitmask & 0x8) == 0;
1365 if ((entryhi_r == 2) ||
1366 (entryhi_r == 1 && (no_supervisor || config0_at == 1))) {
1367 /* skip EntryHi.R field if new value is reserved */
1368 mask &= ~(0x3ull << 62);
1371 mask &= env->SEGMask;
1372 #endif
1373 old = env->CP0_EntryHi;
1374 val = (arg1 & mask) | (old & ~mask);
1375 env->CP0_EntryHi = val;
1376 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1377 sync_c0_entryhi(env, env->current_tc);
1379 /* If the ASID changes, flush qemu's TLB. */
1380 if ((old & 0xFF) != (val & 0xFF))
1381 cpu_mips_tlb_flush(env, 1);
1384 void helper_mttc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1386 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1387 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1389 other->CP0_EntryHi = arg1;
1390 sync_c0_entryhi(other, other_tc);
1393 void helper_mtc0_compare(CPUMIPSState *env, target_ulong arg1)
1395 cpu_mips_store_compare(env, arg1);
1398 void helper_mtc0_status(CPUMIPSState *env, target_ulong arg1)
1400 MIPSCPU *cpu = mips_env_get_cpu(env);
1401 uint32_t val, old;
1403 old = env->CP0_Status;
1404 cpu_mips_store_status(env, arg1);
1405 val = env->CP0_Status;
1407 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1408 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1409 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1410 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1411 env->CP0_Cause);
1412 switch (env->hflags & MIPS_HFLAG_KSU) {
1413 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1414 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1415 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1416 default:
1417 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
1418 break;
1423 void helper_mttc0_status(CPUMIPSState *env, target_ulong arg1)
1425 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1426 uint32_t mask = env->CP0_Status_rw_bitmask & ~0xf1000018;
1427 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1429 other->CP0_Status = (other->CP0_Status & ~mask) | (arg1 & mask);
1430 sync_c0_status(env, other, other_tc);
1433 void helper_mtc0_intctl(CPUMIPSState *env, target_ulong arg1)
1435 /* vectored interrupts not implemented, no performance counters. */
1436 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000003e0) | (arg1 & 0x000003e0);
1439 void helper_mtc0_srsctl(CPUMIPSState *env, target_ulong arg1)
1441 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1442 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1445 void helper_mtc0_cause(CPUMIPSState *env, target_ulong arg1)
1447 cpu_mips_store_cause(env, arg1);
1450 void helper_mttc0_cause(CPUMIPSState *env, target_ulong arg1)
1452 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1453 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1455 cpu_mips_store_cause(other, arg1);
1458 target_ulong helper_mftc0_epc(CPUMIPSState *env)
1460 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1461 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1463 return other->CP0_EPC;
1466 target_ulong helper_mftc0_ebase(CPUMIPSState *env)
1468 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1469 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1471 return other->CP0_EBase;
1474 void helper_mtc0_ebase(CPUMIPSState *env, target_ulong arg1)
1476 /* vectored interrupts not implemented */
1477 env->CP0_EBase = (env->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1480 void helper_mttc0_ebase(CPUMIPSState *env, target_ulong arg1)
1482 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1483 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1484 other->CP0_EBase = (other->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1487 target_ulong helper_mftc0_configx(CPUMIPSState *env, target_ulong idx)
1489 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1490 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1492 switch (idx) {
1493 case 0: return other->CP0_Config0;
1494 case 1: return other->CP0_Config1;
1495 case 2: return other->CP0_Config2;
1496 case 3: return other->CP0_Config3;
1497 /* 4 and 5 are reserved. */
1498 case 6: return other->CP0_Config6;
1499 case 7: return other->CP0_Config7;
1500 default:
1501 break;
1503 return 0;
1506 void helper_mtc0_config0(CPUMIPSState *env, target_ulong arg1)
1508 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1511 void helper_mtc0_config2(CPUMIPSState *env, target_ulong arg1)
1513 /* tertiary/secondary caches not implemented */
1514 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1517 void helper_mtc0_config3(CPUMIPSState *env, target_ulong arg1)
1519 if (env->insn_flags & ASE_MICROMIPS) {
1520 env->CP0_Config3 = (env->CP0_Config3 & ~(1 << CP0C3_ISA_ON_EXC)) |
1521 (arg1 & (1 << CP0C3_ISA_ON_EXC));
1525 void helper_mtc0_config4(CPUMIPSState *env, target_ulong arg1)
1527 env->CP0_Config4 = (env->CP0_Config4 & (~env->CP0_Config4_rw_bitmask)) |
1528 (arg1 & env->CP0_Config4_rw_bitmask);
1531 void helper_mtc0_config5(CPUMIPSState *env, target_ulong arg1)
1533 env->CP0_Config5 = (env->CP0_Config5 & (~env->CP0_Config5_rw_bitmask)) |
1534 (arg1 & env->CP0_Config5_rw_bitmask);
1535 compute_hflags(env);
1538 void helper_mtc0_lladdr(CPUMIPSState *env, target_ulong arg1)
1540 target_long mask = env->CP0_LLAddr_rw_bitmask;
1541 arg1 = arg1 << env->CP0_LLAddr_shift;
1542 env->lladdr = (env->lladdr & ~mask) | (arg1 & mask);
1545 void helper_mtc0_watchlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1547 /* Watch exceptions for instructions, data loads, data stores
1548 not implemented. */
1549 env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1552 void helper_mtc0_watchhi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1554 env->CP0_WatchHi[sel] = (arg1 & 0x40FF0FF8);
1555 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1558 void helper_mtc0_xcontext(CPUMIPSState *env, target_ulong arg1)
1560 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1561 env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1564 void helper_mtc0_framemask(CPUMIPSState *env, target_ulong arg1)
1566 env->CP0_Framemask = arg1; /* XXX */
1569 void helper_mtc0_debug(CPUMIPSState *env, target_ulong arg1)
1571 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
1572 if (arg1 & (1 << CP0DB_DM))
1573 env->hflags |= MIPS_HFLAG_DM;
1574 else
1575 env->hflags &= ~MIPS_HFLAG_DM;
1578 void helper_mttc0_debug(CPUMIPSState *env, target_ulong arg1)
1580 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1581 uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1582 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1584 /* XXX: Might be wrong, check with EJTAG spec. */
1585 if (other_tc == other->current_tc)
1586 other->active_tc.CP0_Debug_tcstatus = val;
1587 else
1588 other->tcs[other_tc].CP0_Debug_tcstatus = val;
1589 other->CP0_Debug = (other->CP0_Debug &
1590 ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1591 (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1594 void helper_mtc0_performance0(CPUMIPSState *env, target_ulong arg1)
1596 env->CP0_Performance0 = arg1 & 0x000007ff;
1599 void helper_mtc0_taglo(CPUMIPSState *env, target_ulong arg1)
1601 env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1604 void helper_mtc0_datalo(CPUMIPSState *env, target_ulong arg1)
1606 env->CP0_DataLo = arg1; /* XXX */
1609 void helper_mtc0_taghi(CPUMIPSState *env, target_ulong arg1)
1611 env->CP0_TagHi = arg1; /* XXX */
1614 void helper_mtc0_datahi(CPUMIPSState *env, target_ulong arg1)
1616 env->CP0_DataHi = arg1; /* XXX */
1619 /* MIPS MT functions */
1620 target_ulong helper_mftgpr(CPUMIPSState *env, uint32_t sel)
1622 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1623 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1625 if (other_tc == other->current_tc)
1626 return other->active_tc.gpr[sel];
1627 else
1628 return other->tcs[other_tc].gpr[sel];
1631 target_ulong helper_mftlo(CPUMIPSState *env, uint32_t sel)
1633 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1634 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1636 if (other_tc == other->current_tc)
1637 return other->active_tc.LO[sel];
1638 else
1639 return other->tcs[other_tc].LO[sel];
1642 target_ulong helper_mfthi(CPUMIPSState *env, uint32_t sel)
1644 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1645 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1647 if (other_tc == other->current_tc)
1648 return other->active_tc.HI[sel];
1649 else
1650 return other->tcs[other_tc].HI[sel];
1653 target_ulong helper_mftacx(CPUMIPSState *env, uint32_t sel)
1655 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1656 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1658 if (other_tc == other->current_tc)
1659 return other->active_tc.ACX[sel];
1660 else
1661 return other->tcs[other_tc].ACX[sel];
1664 target_ulong helper_mftdsp(CPUMIPSState *env)
1666 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1667 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1669 if (other_tc == other->current_tc)
1670 return other->active_tc.DSPControl;
1671 else
1672 return other->tcs[other_tc].DSPControl;
1675 void helper_mttgpr(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1677 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1678 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1680 if (other_tc == other->current_tc)
1681 other->active_tc.gpr[sel] = arg1;
1682 else
1683 other->tcs[other_tc].gpr[sel] = arg1;
1686 void helper_mttlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1688 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1689 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1691 if (other_tc == other->current_tc)
1692 other->active_tc.LO[sel] = arg1;
1693 else
1694 other->tcs[other_tc].LO[sel] = arg1;
1697 void helper_mtthi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1699 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1700 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1702 if (other_tc == other->current_tc)
1703 other->active_tc.HI[sel] = arg1;
1704 else
1705 other->tcs[other_tc].HI[sel] = arg1;
1708 void helper_mttacx(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1710 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1711 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1713 if (other_tc == other->current_tc)
1714 other->active_tc.ACX[sel] = arg1;
1715 else
1716 other->tcs[other_tc].ACX[sel] = arg1;
1719 void helper_mttdsp(CPUMIPSState *env, target_ulong arg1)
1721 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1722 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1724 if (other_tc == other->current_tc)
1725 other->active_tc.DSPControl = arg1;
1726 else
1727 other->tcs[other_tc].DSPControl = arg1;
1730 /* MIPS MT functions */
1731 target_ulong helper_dmt(void)
1733 // TODO
1734 return 0;
1737 target_ulong helper_emt(void)
1739 // TODO
1740 return 0;
1743 target_ulong helper_dvpe(CPUMIPSState *env)
1745 CPUState *other_cs = first_cpu;
1746 target_ulong prev = env->mvp->CP0_MVPControl;
1748 CPU_FOREACH(other_cs) {
1749 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1750 /* Turn off all VPEs except the one executing the dvpe. */
1751 if (&other_cpu->env != env) {
1752 other_cpu->env.mvp->CP0_MVPControl &= ~(1 << CP0MVPCo_EVP);
1753 mips_vpe_sleep(other_cpu);
1756 return prev;
1759 target_ulong helper_evpe(CPUMIPSState *env)
1761 CPUState *other_cs = first_cpu;
1762 target_ulong prev = env->mvp->CP0_MVPControl;
1764 CPU_FOREACH(other_cs) {
1765 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
1767 if (&other_cpu->env != env
1768 /* If the VPE is WFI, don't disturb its sleep. */
1769 && !mips_vpe_is_wfi(other_cpu)) {
1770 /* Enable the VPE. */
1771 other_cpu->env.mvp->CP0_MVPControl |= (1 << CP0MVPCo_EVP);
1772 mips_vpe_wake(other_cpu); /* And wake it up. */
1775 return prev;
1777 #endif /* !CONFIG_USER_ONLY */
1779 void helper_fork(target_ulong arg1, target_ulong arg2)
1781 // arg1 = rt, arg2 = rs
1782 // TODO: store to TC register
1785 target_ulong helper_yield(CPUMIPSState *env, target_ulong arg)
1787 target_long arg1 = arg;
1789 if (arg1 < 0) {
1790 /* No scheduling policy implemented. */
1791 if (arg1 != -2) {
1792 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1793 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
1794 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1795 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1796 helper_raise_exception(env, EXCP_THREAD);
1799 } else if (arg1 == 0) {
1800 if (0 /* TODO: TC underflow */) {
1801 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1802 helper_raise_exception(env, EXCP_THREAD);
1803 } else {
1804 // TODO: Deallocate TC
1806 } else if (arg1 > 0) {
1807 /* Yield qualifier inputs not implemented. */
1808 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1809 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1810 helper_raise_exception(env, EXCP_THREAD);
1812 return env->CP0_YQMask;
1815 #ifndef CONFIG_USER_ONLY
1816 /* TLB management */
1817 static void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global)
1819 MIPSCPU *cpu = mips_env_get_cpu(env);
1821 /* Flush qemu's TLB and discard all shadowed entries. */
1822 tlb_flush(CPU(cpu), flush_global);
1823 env->tlb->tlb_in_use = env->tlb->nb_tlb;
1826 static void r4k_mips_tlb_flush_extra (CPUMIPSState *env, int first)
1828 /* Discard entries from env->tlb[first] onwards. */
1829 while (env->tlb->tlb_in_use > first) {
1830 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1834 static inline uint64_t get_tlb_pfn_from_entrylo(uint64_t entrylo)
1836 #if defined(TARGET_MIPS64)
1837 return extract64(entrylo, 6, 54);
1838 #else
1839 return extract64(entrylo, 6, 24) | /* PFN */
1840 (extract64(entrylo, 32, 32) << 24); /* PFNX */
1841 #endif
1844 static void r4k_fill_tlb(CPUMIPSState *env, int idx)
1846 r4k_tlb_t *tlb;
1848 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1849 tlb = &env->tlb->mmu.r4k.tlb[idx];
1850 if (env->CP0_EntryHi & (1 << CP0EnHi_EHINV)) {
1851 tlb->EHINV = 1;
1852 return;
1854 tlb->EHINV = 0;
1855 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1856 #if defined(TARGET_MIPS64)
1857 tlb->VPN &= env->SEGMask;
1858 #endif
1859 tlb->ASID = env->CP0_EntryHi & 0xFF;
1860 tlb->PageMask = env->CP0_PageMask;
1861 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1862 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
1863 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
1864 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
1865 tlb->XI0 = (env->CP0_EntryLo0 >> CP0EnLo_XI) & 1;
1866 tlb->RI0 = (env->CP0_EntryLo0 >> CP0EnLo_RI) & 1;
1867 tlb->PFN[0] = get_tlb_pfn_from_entrylo(env->CP0_EntryLo0) << 12;
1868 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
1869 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
1870 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
1871 tlb->XI1 = (env->CP0_EntryLo1 >> CP0EnLo_XI) & 1;
1872 tlb->RI1 = (env->CP0_EntryLo1 >> CP0EnLo_RI) & 1;
1873 tlb->PFN[1] = get_tlb_pfn_from_entrylo(env->CP0_EntryLo1) << 12;
1876 void r4k_helper_tlbinv(CPUMIPSState *env)
1878 int idx;
1879 r4k_tlb_t *tlb;
1880 uint8_t ASID = env->CP0_EntryHi & 0xFF;
1882 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
1883 tlb = &env->tlb->mmu.r4k.tlb[idx];
1884 if (!tlb->G && tlb->ASID == ASID) {
1885 tlb->EHINV = 1;
1888 cpu_mips_tlb_flush(env, 1);
1891 void r4k_helper_tlbinvf(CPUMIPSState *env)
1893 int idx;
1895 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
1896 env->tlb->mmu.r4k.tlb[idx].EHINV = 1;
1898 cpu_mips_tlb_flush(env, 1);
1901 void r4k_helper_tlbwi(CPUMIPSState *env)
1903 r4k_tlb_t *tlb;
1904 int idx;
1905 target_ulong VPN;
1906 uint8_t ASID;
1907 bool G, V0, D0, V1, D1;
1909 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1910 tlb = &env->tlb->mmu.r4k.tlb[idx];
1911 VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1912 #if defined(TARGET_MIPS64)
1913 VPN &= env->SEGMask;
1914 #endif
1915 ASID = env->CP0_EntryHi & 0xff;
1916 G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1917 V0 = (env->CP0_EntryLo0 & 2) != 0;
1918 D0 = (env->CP0_EntryLo0 & 4) != 0;
1919 V1 = (env->CP0_EntryLo1 & 2) != 0;
1920 D1 = (env->CP0_EntryLo1 & 4) != 0;
1922 /* Discard cached TLB entries, unless tlbwi is just upgrading access
1923 permissions on the current entry. */
1924 if (tlb->VPN != VPN || tlb->ASID != ASID || tlb->G != G ||
1925 (tlb->V0 && !V0) || (tlb->D0 && !D0) ||
1926 (tlb->V1 && !V1) || (tlb->D1 && !D1)) {
1927 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
1930 r4k_invalidate_tlb(env, idx, 0);
1931 r4k_fill_tlb(env, idx);
1934 void r4k_helper_tlbwr(CPUMIPSState *env)
1936 int r = cpu_mips_get_random(env);
1938 r4k_invalidate_tlb(env, r, 1);
1939 r4k_fill_tlb(env, r);
1942 void r4k_helper_tlbp(CPUMIPSState *env)
1944 r4k_tlb_t *tlb;
1945 target_ulong mask;
1946 target_ulong tag;
1947 target_ulong VPN;
1948 uint8_t ASID;
1949 int i;
1951 ASID = env->CP0_EntryHi & 0xFF;
1952 for (i = 0; i < env->tlb->nb_tlb; i++) {
1953 tlb = &env->tlb->mmu.r4k.tlb[i];
1954 /* 1k pages are not supported. */
1955 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1956 tag = env->CP0_EntryHi & ~mask;
1957 VPN = tlb->VPN & ~mask;
1958 #if defined(TARGET_MIPS64)
1959 tag &= env->SEGMask;
1960 #endif
1961 /* Check ASID, virtual page number & size */
1962 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
1963 /* TLB match */
1964 env->CP0_Index = i;
1965 break;
1968 if (i == env->tlb->nb_tlb) {
1969 /* No match. Discard any shadow entries, if any of them match. */
1970 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
1971 tlb = &env->tlb->mmu.r4k.tlb[i];
1972 /* 1k pages are not supported. */
1973 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1974 tag = env->CP0_EntryHi & ~mask;
1975 VPN = tlb->VPN & ~mask;
1976 #if defined(TARGET_MIPS64)
1977 tag &= env->SEGMask;
1978 #endif
1979 /* Check ASID, virtual page number & size */
1980 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1981 r4k_mips_tlb_flush_extra (env, i);
1982 break;
1986 env->CP0_Index |= 0x80000000;
1990 static inline uint64_t get_entrylo_pfn_from_tlb(uint64_t tlb_pfn)
1992 #if defined(TARGET_MIPS64)
1993 return tlb_pfn << 6;
1994 #else
1995 return (extract64(tlb_pfn, 0, 24) << 6) | /* PFN */
1996 (extract64(tlb_pfn, 24, 32) << 32); /* PFNX */
1997 #endif
2000 void r4k_helper_tlbr(CPUMIPSState *env)
2002 r4k_tlb_t *tlb;
2003 uint8_t ASID;
2004 int idx;
2006 ASID = env->CP0_EntryHi & 0xFF;
2007 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
2008 tlb = &env->tlb->mmu.r4k.tlb[idx];
2010 /* If this will change the current ASID, flush qemu's TLB. */
2011 if (ASID != tlb->ASID)
2012 cpu_mips_tlb_flush (env, 1);
2014 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
2016 if (tlb->EHINV) {
2017 env->CP0_EntryHi = 1 << CP0EnHi_EHINV;
2018 env->CP0_PageMask = 0;
2019 env->CP0_EntryLo0 = 0;
2020 env->CP0_EntryLo1 = 0;
2021 } else {
2022 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
2023 env->CP0_PageMask = tlb->PageMask;
2024 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
2025 ((uint64_t)tlb->RI0 << CP0EnLo_RI) |
2026 ((uint64_t)tlb->XI0 << CP0EnLo_XI) | (tlb->C0 << 3) |
2027 get_entrylo_pfn_from_tlb(tlb->PFN[0] >> 12);
2028 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
2029 ((uint64_t)tlb->RI1 << CP0EnLo_RI) |
2030 ((uint64_t)tlb->XI1 << CP0EnLo_XI) | (tlb->C1 << 3) |
2031 get_entrylo_pfn_from_tlb(tlb->PFN[1] >> 12);
2035 void helper_tlbwi(CPUMIPSState *env)
2037 env->tlb->helper_tlbwi(env);
2040 void helper_tlbwr(CPUMIPSState *env)
2042 env->tlb->helper_tlbwr(env);
2045 void helper_tlbp(CPUMIPSState *env)
2047 env->tlb->helper_tlbp(env);
2050 void helper_tlbr(CPUMIPSState *env)
2052 env->tlb->helper_tlbr(env);
2055 void helper_tlbinv(CPUMIPSState *env)
2057 env->tlb->helper_tlbinv(env);
2060 void helper_tlbinvf(CPUMIPSState *env)
2062 env->tlb->helper_tlbinvf(env);
2065 /* Specials */
2066 target_ulong helper_di(CPUMIPSState *env)
2068 target_ulong t0 = env->CP0_Status;
2070 env->CP0_Status = t0 & ~(1 << CP0St_IE);
2071 return t0;
2074 target_ulong helper_ei(CPUMIPSState *env)
2076 target_ulong t0 = env->CP0_Status;
2078 env->CP0_Status = t0 | (1 << CP0St_IE);
2079 return t0;
2082 static void debug_pre_eret(CPUMIPSState *env)
2084 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2085 qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2086 env->active_tc.PC, env->CP0_EPC);
2087 if (env->CP0_Status & (1 << CP0St_ERL))
2088 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2089 if (env->hflags & MIPS_HFLAG_DM)
2090 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2091 qemu_log("\n");
2095 static void debug_post_eret(CPUMIPSState *env)
2097 MIPSCPU *cpu = mips_env_get_cpu(env);
2099 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2100 qemu_log(" => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2101 env->active_tc.PC, env->CP0_EPC);
2102 if (env->CP0_Status & (1 << CP0St_ERL))
2103 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2104 if (env->hflags & MIPS_HFLAG_DM)
2105 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2106 switch (env->hflags & MIPS_HFLAG_KSU) {
2107 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
2108 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
2109 case MIPS_HFLAG_KM: qemu_log("\n"); break;
2110 default:
2111 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
2112 break;
2117 static void set_pc(CPUMIPSState *env, target_ulong error_pc)
2119 env->active_tc.PC = error_pc & ~(target_ulong)1;
2120 if (error_pc & 1) {
2121 env->hflags |= MIPS_HFLAG_M16;
2122 } else {
2123 env->hflags &= ~(MIPS_HFLAG_M16);
2127 static inline void exception_return(CPUMIPSState *env)
2129 debug_pre_eret(env);
2130 if (env->CP0_Status & (1 << CP0St_ERL)) {
2131 set_pc(env, env->CP0_ErrorEPC);
2132 env->CP0_Status &= ~(1 << CP0St_ERL);
2133 } else {
2134 set_pc(env, env->CP0_EPC);
2135 env->CP0_Status &= ~(1 << CP0St_EXL);
2137 compute_hflags(env);
2138 debug_post_eret(env);
2141 void helper_eret(CPUMIPSState *env)
2143 exception_return(env);
2144 env->lladdr = 1;
2147 void helper_eretnc(CPUMIPSState *env)
2149 exception_return(env);
2152 void helper_deret(CPUMIPSState *env)
2154 debug_pre_eret(env);
2155 set_pc(env, env->CP0_DEPC);
2157 env->hflags &= ~MIPS_HFLAG_DM;
2158 compute_hflags(env);
2159 debug_post_eret(env);
2161 #endif /* !CONFIG_USER_ONLY */
2163 target_ulong helper_rdhwr_cpunum(CPUMIPSState *env)
2165 if ((env->hflags & MIPS_HFLAG_CP0) ||
2166 (env->CP0_HWREna & (1 << 0)))
2167 return env->CP0_EBase & 0x3ff;
2168 else
2169 helper_raise_exception(env, EXCP_RI);
2171 return 0;
2174 target_ulong helper_rdhwr_synci_step(CPUMIPSState *env)
2176 if ((env->hflags & MIPS_HFLAG_CP0) ||
2177 (env->CP0_HWREna & (1 << 1)))
2178 return env->SYNCI_Step;
2179 else
2180 helper_raise_exception(env, EXCP_RI);
2182 return 0;
2185 target_ulong helper_rdhwr_cc(CPUMIPSState *env)
2187 if ((env->hflags & MIPS_HFLAG_CP0) ||
2188 (env->CP0_HWREna & (1 << 2)))
2189 return env->CP0_Count;
2190 else
2191 helper_raise_exception(env, EXCP_RI);
2193 return 0;
2196 target_ulong helper_rdhwr_ccres(CPUMIPSState *env)
2198 if ((env->hflags & MIPS_HFLAG_CP0) ||
2199 (env->CP0_HWREna & (1 << 3)))
2200 return env->CCRes;
2201 else
2202 helper_raise_exception(env, EXCP_RI);
2204 return 0;
2207 void helper_pmon(CPUMIPSState *env, int function)
2209 function /= 2;
2210 switch (function) {
2211 case 2: /* TODO: char inbyte(int waitflag); */
2212 if (env->active_tc.gpr[4] == 0)
2213 env->active_tc.gpr[2] = -1;
2214 /* Fall through */
2215 case 11: /* TODO: char inbyte (void); */
2216 env->active_tc.gpr[2] = -1;
2217 break;
2218 case 3:
2219 case 12:
2220 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
2221 break;
2222 case 17:
2223 break;
2224 case 158:
2226 unsigned char *fmt = (void *)(uintptr_t)env->active_tc.gpr[4];
2227 printf("%s", fmt);
2229 break;
2233 void helper_wait(CPUMIPSState *env)
2235 CPUState *cs = CPU(mips_env_get_cpu(env));
2237 cs->halted = 1;
2238 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
2239 helper_raise_exception(env, EXCP_HLT);
2242 #if !defined(CONFIG_USER_ONLY)
2244 void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
2245 int access_type, int is_user,
2246 uintptr_t retaddr)
2248 MIPSCPU *cpu = MIPS_CPU(cs);
2249 CPUMIPSState *env = &cpu->env;
2250 int error_code = 0;
2251 int excp;
2253 env->CP0_BadVAddr = addr;
2255 if (access_type == MMU_DATA_STORE) {
2256 excp = EXCP_AdES;
2257 } else {
2258 excp = EXCP_AdEL;
2259 if (access_type == MMU_INST_FETCH) {
2260 error_code |= EXCP_INST_NOTAVAIL;
2264 do_raise_exception_err(env, excp, error_code, retaddr);
2267 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
2268 uintptr_t retaddr)
2270 int ret;
2272 ret = mips_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
2273 if (ret) {
2274 MIPSCPU *cpu = MIPS_CPU(cs);
2275 CPUMIPSState *env = &cpu->env;
2277 do_raise_exception_err(env, cs->exception_index,
2278 env->error_code, retaddr);
2282 void mips_cpu_unassigned_access(CPUState *cs, hwaddr addr,
2283 bool is_write, bool is_exec, int unused,
2284 unsigned size)
2286 MIPSCPU *cpu = MIPS_CPU(cs);
2287 CPUMIPSState *env = &cpu->env;
2290 * Raising an exception with KVM enabled will crash because it won't be from
2291 * the main execution loop so the longjmp won't have a matching setjmp.
2292 * Until we can trigger a bus error exception through KVM lets just ignore
2293 * the access.
2295 if (kvm_enabled()) {
2296 return;
2299 if (is_exec) {
2300 helper_raise_exception(env, EXCP_IBE);
2301 } else {
2302 helper_raise_exception(env, EXCP_DBE);
2305 #endif /* !CONFIG_USER_ONLY */
2307 /* Complex FPU operations which may need stack space. */
2309 #define FLOAT_TWO32 make_float32(1 << 30)
2310 #define FLOAT_TWO64 make_float64(1ULL << 62)
2311 #define FP_TO_INT32_OVERFLOW 0x7fffffff
2312 #define FP_TO_INT64_OVERFLOW 0x7fffffffffffffffULL
2314 /* convert MIPS rounding mode in FCR31 to IEEE library */
2315 unsigned int ieee_rm[] = {
2316 float_round_nearest_even,
2317 float_round_to_zero,
2318 float_round_up,
2319 float_round_down
2322 target_ulong helper_cfc1(CPUMIPSState *env, uint32_t reg)
2324 target_ulong arg1 = 0;
2326 switch (reg) {
2327 case 0:
2328 arg1 = (int32_t)env->active_fpu.fcr0;
2329 break;
2330 case 1:
2331 /* UFR Support - Read Status FR */
2332 if (env->active_fpu.fcr0 & (1 << FCR0_UFRP)) {
2333 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2334 arg1 = (int32_t)
2335 ((env->CP0_Status & (1 << CP0St_FR)) >> CP0St_FR);
2336 } else {
2337 helper_raise_exception(env, EXCP_RI);
2340 break;
2341 case 5:
2342 /* FRE Support - read Config5.FRE bit */
2343 if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
2344 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2345 arg1 = (env->CP0_Config5 >> CP0C5_FRE) & 1;
2346 } else {
2347 helper_raise_exception(env, EXCP_RI);
2350 break;
2351 case 25:
2352 arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);
2353 break;
2354 case 26:
2355 arg1 = env->active_fpu.fcr31 & 0x0003f07c;
2356 break;
2357 case 28:
2358 arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);
2359 break;
2360 default:
2361 arg1 = (int32_t)env->active_fpu.fcr31;
2362 break;
2365 return arg1;
2368 void helper_ctc1(CPUMIPSState *env, target_ulong arg1, uint32_t fs, uint32_t rt)
2370 switch (fs) {
2371 case 1:
2372 /* UFR Alias - Reset Status FR */
2373 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2374 return;
2376 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2377 env->CP0_Status &= ~(1 << CP0St_FR);
2378 compute_hflags(env);
2379 } else {
2380 helper_raise_exception(env, EXCP_RI);
2382 break;
2383 case 4:
2384 /* UNFR Alias - Set Status FR */
2385 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2386 return;
2388 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2389 env->CP0_Status |= (1 << CP0St_FR);
2390 compute_hflags(env);
2391 } else {
2392 helper_raise_exception(env, EXCP_RI);
2394 break;
2395 case 5:
2396 /* FRE Support - clear Config5.FRE bit */
2397 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2398 return;
2400 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2401 env->CP0_Config5 &= ~(1 << CP0C5_FRE);
2402 compute_hflags(env);
2403 } else {
2404 helper_raise_exception(env, EXCP_RI);
2406 break;
2407 case 6:
2408 /* FRE Support - set Config5.FRE bit */
2409 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2410 return;
2412 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2413 env->CP0_Config5 |= (1 << CP0C5_FRE);
2414 compute_hflags(env);
2415 } else {
2416 helper_raise_exception(env, EXCP_RI);
2418 break;
2419 case 25:
2420 if ((env->insn_flags & ISA_MIPS32R6) || (arg1 & 0xffffff00)) {
2421 return;
2423 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
2424 ((arg1 & 0x1) << 23);
2425 break;
2426 case 26:
2427 if (arg1 & 0x007c0000)
2428 return;
2429 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
2430 break;
2431 case 28:
2432 if (arg1 & 0x007c0000)
2433 return;
2434 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
2435 ((arg1 & 0x4) << 22);
2436 break;
2437 case 31:
2438 if (env->insn_flags & ISA_MIPS32R6) {
2439 uint32_t mask = 0xfefc0000;
2440 env->active_fpu.fcr31 = (arg1 & ~mask) |
2441 (env->active_fpu.fcr31 & mask);
2442 } else if (!(arg1 & 0x007c0000)) {
2443 env->active_fpu.fcr31 = arg1;
2445 break;
2446 default:
2447 return;
2449 /* set rounding mode */
2450 restore_rounding_mode(env);
2451 /* set flush-to-zero mode */
2452 restore_flush_mode(env);
2453 set_float_exception_flags(0, &env->active_fpu.fp_status);
2454 if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
2455 do_raise_exception(env, EXCP_FPE, GETPC());
2458 int ieee_ex_to_mips(int xcpt)
2460 int ret = 0;
2461 if (xcpt) {
2462 if (xcpt & float_flag_invalid) {
2463 ret |= FP_INVALID;
2465 if (xcpt & float_flag_overflow) {
2466 ret |= FP_OVERFLOW;
2468 if (xcpt & float_flag_underflow) {
2469 ret |= FP_UNDERFLOW;
2471 if (xcpt & float_flag_divbyzero) {
2472 ret |= FP_DIV0;
2474 if (xcpt & float_flag_inexact) {
2475 ret |= FP_INEXACT;
2478 return ret;
2481 static inline void update_fcr31(CPUMIPSState *env, uintptr_t pc)
2483 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
2485 SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
2487 if (tmp) {
2488 set_float_exception_flags(0, &env->active_fpu.fp_status);
2490 if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp) {
2491 do_raise_exception(env, EXCP_FPE, pc);
2492 } else {
2493 UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
2498 /* Float support.
2499 Single precition routines have a "s" suffix, double precision a
2500 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
2501 paired single lower "pl", paired single upper "pu". */
2503 /* unary operations, modifying fp status */
2504 uint64_t helper_float_sqrt_d(CPUMIPSState *env, uint64_t fdt0)
2506 fdt0 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2507 update_fcr31(env, GETPC());
2508 return fdt0;
2511 uint32_t helper_float_sqrt_s(CPUMIPSState *env, uint32_t fst0)
2513 fst0 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2514 update_fcr31(env, GETPC());
2515 return fst0;
2518 uint64_t helper_float_cvtd_s(CPUMIPSState *env, uint32_t fst0)
2520 uint64_t fdt2;
2522 fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
2523 update_fcr31(env, GETPC());
2524 return fdt2;
2527 uint64_t helper_float_cvtd_w(CPUMIPSState *env, uint32_t wt0)
2529 uint64_t fdt2;
2531 fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2532 update_fcr31(env, GETPC());
2533 return fdt2;
2536 uint64_t helper_float_cvtd_l(CPUMIPSState *env, uint64_t dt0)
2538 uint64_t fdt2;
2540 fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2541 update_fcr31(env, GETPC());
2542 return fdt2;
2545 uint64_t helper_float_cvtl_d(CPUMIPSState *env, uint64_t fdt0)
2547 uint64_t dt2;
2549 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2550 if (get_float_exception_flags(&env->active_fpu.fp_status)
2551 & (float_flag_invalid | float_flag_overflow)) {
2552 dt2 = FP_TO_INT64_OVERFLOW;
2554 update_fcr31(env, GETPC());
2555 return dt2;
2558 uint64_t helper_float_cvtl_s(CPUMIPSState *env, uint32_t fst0)
2560 uint64_t dt2;
2562 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2563 if (get_float_exception_flags(&env->active_fpu.fp_status)
2564 & (float_flag_invalid | float_flag_overflow)) {
2565 dt2 = FP_TO_INT64_OVERFLOW;
2567 update_fcr31(env, GETPC());
2568 return dt2;
2571 uint64_t helper_float_cvtps_pw(CPUMIPSState *env, uint64_t dt0)
2573 uint32_t fst2;
2574 uint32_t fsth2;
2576 fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2577 fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
2578 update_fcr31(env, GETPC());
2579 return ((uint64_t)fsth2 << 32) | fst2;
2582 uint64_t helper_float_cvtpw_ps(CPUMIPSState *env, uint64_t fdt0)
2584 uint32_t wt2;
2585 uint32_t wth2;
2586 int excp, excph;
2588 wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2589 excp = get_float_exception_flags(&env->active_fpu.fp_status);
2590 if (excp & (float_flag_overflow | float_flag_invalid)) {
2591 wt2 = FP_TO_INT32_OVERFLOW;
2594 set_float_exception_flags(0, &env->active_fpu.fp_status);
2595 wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
2596 excph = get_float_exception_flags(&env->active_fpu.fp_status);
2597 if (excph & (float_flag_overflow | float_flag_invalid)) {
2598 wth2 = FP_TO_INT32_OVERFLOW;
2601 set_float_exception_flags(excp | excph, &env->active_fpu.fp_status);
2602 update_fcr31(env, GETPC());
2604 return ((uint64_t)wth2 << 32) | wt2;
2607 uint32_t helper_float_cvts_d(CPUMIPSState *env, uint64_t fdt0)
2609 uint32_t fst2;
2611 fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
2612 update_fcr31(env, GETPC());
2613 return fst2;
2616 uint32_t helper_float_cvts_w(CPUMIPSState *env, uint32_t wt0)
2618 uint32_t fst2;
2620 fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
2621 update_fcr31(env, GETPC());
2622 return fst2;
2625 uint32_t helper_float_cvts_l(CPUMIPSState *env, uint64_t dt0)
2627 uint32_t fst2;
2629 fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
2630 update_fcr31(env, GETPC());
2631 return fst2;
2634 uint32_t helper_float_cvts_pl(CPUMIPSState *env, uint32_t wt0)
2636 uint32_t wt2;
2638 wt2 = wt0;
2639 update_fcr31(env, GETPC());
2640 return wt2;
2643 uint32_t helper_float_cvts_pu(CPUMIPSState *env, uint32_t wth0)
2645 uint32_t wt2;
2647 wt2 = wth0;
2648 update_fcr31(env, GETPC());
2649 return wt2;
2652 uint32_t helper_float_cvtw_s(CPUMIPSState *env, uint32_t fst0)
2654 uint32_t wt2;
2656 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2657 if (get_float_exception_flags(&env->active_fpu.fp_status)
2658 & (float_flag_invalid | float_flag_overflow)) {
2659 wt2 = FP_TO_INT32_OVERFLOW;
2661 update_fcr31(env, GETPC());
2662 return wt2;
2665 uint32_t helper_float_cvtw_d(CPUMIPSState *env, uint64_t fdt0)
2667 uint32_t wt2;
2669 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2670 if (get_float_exception_flags(&env->active_fpu.fp_status)
2671 & (float_flag_invalid | float_flag_overflow)) {
2672 wt2 = FP_TO_INT32_OVERFLOW;
2674 update_fcr31(env, GETPC());
2675 return wt2;
2678 uint64_t helper_float_roundl_d(CPUMIPSState *env, uint64_t fdt0)
2680 uint64_t dt2;
2682 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2683 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2684 restore_rounding_mode(env);
2685 if (get_float_exception_flags(&env->active_fpu.fp_status)
2686 & (float_flag_invalid | float_flag_overflow)) {
2687 dt2 = FP_TO_INT64_OVERFLOW;
2689 update_fcr31(env, GETPC());
2690 return dt2;
2693 uint64_t helper_float_roundl_s(CPUMIPSState *env, uint32_t fst0)
2695 uint64_t dt2;
2697 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2698 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2699 restore_rounding_mode(env);
2700 if (get_float_exception_flags(&env->active_fpu.fp_status)
2701 & (float_flag_invalid | float_flag_overflow)) {
2702 dt2 = FP_TO_INT64_OVERFLOW;
2704 update_fcr31(env, GETPC());
2705 return dt2;
2708 uint32_t helper_float_roundw_d(CPUMIPSState *env, uint64_t fdt0)
2710 uint32_t wt2;
2712 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2713 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2714 restore_rounding_mode(env);
2715 if (get_float_exception_flags(&env->active_fpu.fp_status)
2716 & (float_flag_invalid | float_flag_overflow)) {
2717 wt2 = FP_TO_INT32_OVERFLOW;
2719 update_fcr31(env, GETPC());
2720 return wt2;
2723 uint32_t helper_float_roundw_s(CPUMIPSState *env, uint32_t fst0)
2725 uint32_t wt2;
2727 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2728 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2729 restore_rounding_mode(env);
2730 if (get_float_exception_flags(&env->active_fpu.fp_status)
2731 & (float_flag_invalid | float_flag_overflow)) {
2732 wt2 = FP_TO_INT32_OVERFLOW;
2734 update_fcr31(env, GETPC());
2735 return wt2;
2738 uint64_t helper_float_truncl_d(CPUMIPSState *env, uint64_t fdt0)
2740 uint64_t dt2;
2742 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
2743 if (get_float_exception_flags(&env->active_fpu.fp_status)
2744 & (float_flag_invalid | float_flag_overflow)) {
2745 dt2 = FP_TO_INT64_OVERFLOW;
2747 update_fcr31(env, GETPC());
2748 return dt2;
2751 uint64_t helper_float_truncl_s(CPUMIPSState *env, uint32_t fst0)
2753 uint64_t dt2;
2755 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
2756 if (get_float_exception_flags(&env->active_fpu.fp_status)
2757 & (float_flag_invalid | float_flag_overflow)) {
2758 dt2 = FP_TO_INT64_OVERFLOW;
2760 update_fcr31(env, GETPC());
2761 return dt2;
2764 uint32_t helper_float_truncw_d(CPUMIPSState *env, uint64_t fdt0)
2766 uint32_t wt2;
2768 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
2769 if (get_float_exception_flags(&env->active_fpu.fp_status)
2770 & (float_flag_invalid | float_flag_overflow)) {
2771 wt2 = FP_TO_INT32_OVERFLOW;
2773 update_fcr31(env, GETPC());
2774 return wt2;
2777 uint32_t helper_float_truncw_s(CPUMIPSState *env, uint32_t fst0)
2779 uint32_t wt2;
2781 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
2782 if (get_float_exception_flags(&env->active_fpu.fp_status)
2783 & (float_flag_invalid | float_flag_overflow)) {
2784 wt2 = FP_TO_INT32_OVERFLOW;
2786 update_fcr31(env, GETPC());
2787 return wt2;
2790 uint64_t helper_float_ceill_d(CPUMIPSState *env, uint64_t fdt0)
2792 uint64_t dt2;
2794 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2795 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2796 restore_rounding_mode(env);
2797 if (get_float_exception_flags(&env->active_fpu.fp_status)
2798 & (float_flag_invalid | float_flag_overflow)) {
2799 dt2 = FP_TO_INT64_OVERFLOW;
2801 update_fcr31(env, GETPC());
2802 return dt2;
2805 uint64_t helper_float_ceill_s(CPUMIPSState *env, uint32_t fst0)
2807 uint64_t dt2;
2809 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2810 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2811 restore_rounding_mode(env);
2812 if (get_float_exception_flags(&env->active_fpu.fp_status)
2813 & (float_flag_invalid | float_flag_overflow)) {
2814 dt2 = FP_TO_INT64_OVERFLOW;
2816 update_fcr31(env, GETPC());
2817 return dt2;
2820 uint32_t helper_float_ceilw_d(CPUMIPSState *env, uint64_t fdt0)
2822 uint32_t wt2;
2824 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2825 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2826 restore_rounding_mode(env);
2827 if (get_float_exception_flags(&env->active_fpu.fp_status)
2828 & (float_flag_invalid | float_flag_overflow)) {
2829 wt2 = FP_TO_INT32_OVERFLOW;
2831 update_fcr31(env, GETPC());
2832 return wt2;
2835 uint32_t helper_float_ceilw_s(CPUMIPSState *env, uint32_t fst0)
2837 uint32_t wt2;
2839 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2840 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2841 restore_rounding_mode(env);
2842 if (get_float_exception_flags(&env->active_fpu.fp_status)
2843 & (float_flag_invalid | float_flag_overflow)) {
2844 wt2 = FP_TO_INT32_OVERFLOW;
2846 update_fcr31(env, GETPC());
2847 return wt2;
2850 uint64_t helper_float_floorl_d(CPUMIPSState *env, uint64_t fdt0)
2852 uint64_t dt2;
2854 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2855 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2856 restore_rounding_mode(env);
2857 if (get_float_exception_flags(&env->active_fpu.fp_status)
2858 & (float_flag_invalid | float_flag_overflow)) {
2859 dt2 = FP_TO_INT64_OVERFLOW;
2861 update_fcr31(env, GETPC());
2862 return dt2;
2865 uint64_t helper_float_floorl_s(CPUMIPSState *env, uint32_t fst0)
2867 uint64_t dt2;
2869 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2870 dt2 = float32_to_int64(fst0, &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 uint32_t helper_float_floorw_d(CPUMIPSState *env, uint64_t fdt0)
2882 uint32_t wt2;
2884 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2885 wt2 = float64_to_int32(fdt0, &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 wt2 = FP_TO_INT32_OVERFLOW;
2891 update_fcr31(env, GETPC());
2892 return wt2;
2895 uint32_t helper_float_floorw_s(CPUMIPSState *env, uint32_t fst0)
2897 uint32_t wt2;
2899 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2900 wt2 = float32_to_int32(fst0, &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 /* unary operations, not modifying fp status */
2911 #define FLOAT_UNOP(name) \
2912 uint64_t helper_float_ ## name ## _d(uint64_t fdt0) \
2914 return float64_ ## name(fdt0); \
2916 uint32_t helper_float_ ## name ## _s(uint32_t fst0) \
2918 return float32_ ## name(fst0); \
2920 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0) \
2922 uint32_t wt0; \
2923 uint32_t wth0; \
2925 wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF); \
2926 wth0 = float32_ ## name(fdt0 >> 32); \
2927 return ((uint64_t)wth0 << 32) | wt0; \
2929 FLOAT_UNOP(abs)
2930 FLOAT_UNOP(chs)
2931 #undef FLOAT_UNOP
2933 /* MIPS specific unary operations */
2934 uint64_t helper_float_recip_d(CPUMIPSState *env, uint64_t fdt0)
2936 uint64_t fdt2;
2938 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
2939 update_fcr31(env, GETPC());
2940 return fdt2;
2943 uint32_t helper_float_recip_s(CPUMIPSState *env, uint32_t fst0)
2945 uint32_t fst2;
2947 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
2948 update_fcr31(env, GETPC());
2949 return fst2;
2952 uint64_t helper_float_rsqrt_d(CPUMIPSState *env, uint64_t fdt0)
2954 uint64_t fdt2;
2956 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2957 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
2958 update_fcr31(env, GETPC());
2959 return fdt2;
2962 uint32_t helper_float_rsqrt_s(CPUMIPSState *env, uint32_t fst0)
2964 uint32_t fst2;
2966 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2967 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
2968 update_fcr31(env, GETPC());
2969 return fst2;
2972 uint64_t helper_float_recip1_d(CPUMIPSState *env, uint64_t fdt0)
2974 uint64_t fdt2;
2976 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
2977 update_fcr31(env, GETPC());
2978 return fdt2;
2981 uint32_t helper_float_recip1_s(CPUMIPSState *env, uint32_t fst0)
2983 uint32_t fst2;
2985 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
2986 update_fcr31(env, GETPC());
2987 return fst2;
2990 uint64_t helper_float_recip1_ps(CPUMIPSState *env, uint64_t fdt0)
2992 uint32_t fst2;
2993 uint32_t fsth2;
2995 fst2 = float32_div(float32_one, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2996 fsth2 = float32_div(float32_one, fdt0 >> 32, &env->active_fpu.fp_status);
2997 update_fcr31(env, GETPC());
2998 return ((uint64_t)fsth2 << 32) | fst2;
3001 uint64_t helper_float_rsqrt1_d(CPUMIPSState *env, uint64_t fdt0)
3003 uint64_t fdt2;
3005 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
3006 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
3007 update_fcr31(env, GETPC());
3008 return fdt2;
3011 uint32_t helper_float_rsqrt1_s(CPUMIPSState *env, uint32_t fst0)
3013 uint32_t fst2;
3015 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
3016 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3017 update_fcr31(env, GETPC());
3018 return fst2;
3021 uint64_t helper_float_rsqrt1_ps(CPUMIPSState *env, uint64_t fdt0)
3023 uint32_t fst2;
3024 uint32_t fsth2;
3026 fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3027 fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
3028 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3029 fsth2 = float32_div(float32_one, fsth2, &env->active_fpu.fp_status);
3030 update_fcr31(env, GETPC());
3031 return ((uint64_t)fsth2 << 32) | fst2;
3034 #define FLOAT_RINT(name, bits) \
3035 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3036 uint ## bits ## _t fs) \
3038 uint ## bits ## _t fdret; \
3040 fdret = float ## bits ## _round_to_int(fs, &env->active_fpu.fp_status); \
3041 update_fcr31(env, GETPC()); \
3042 return fdret; \
3045 FLOAT_RINT(rint_s, 32)
3046 FLOAT_RINT(rint_d, 64)
3047 #undef FLOAT_RINT
3049 #define FLOAT_CLASS_SIGNALING_NAN 0x001
3050 #define FLOAT_CLASS_QUIET_NAN 0x002
3051 #define FLOAT_CLASS_NEGATIVE_INFINITY 0x004
3052 #define FLOAT_CLASS_NEGATIVE_NORMAL 0x008
3053 #define FLOAT_CLASS_NEGATIVE_SUBNORMAL 0x010
3054 #define FLOAT_CLASS_NEGATIVE_ZERO 0x020
3055 #define FLOAT_CLASS_POSITIVE_INFINITY 0x040
3056 #define FLOAT_CLASS_POSITIVE_NORMAL 0x080
3057 #define FLOAT_CLASS_POSITIVE_SUBNORMAL 0x100
3058 #define FLOAT_CLASS_POSITIVE_ZERO 0x200
3060 #define FLOAT_CLASS(name, bits) \
3061 uint ## bits ## _t helper_float_ ## name (uint ## bits ## _t arg) \
3063 if (float ## bits ## _is_signaling_nan(arg)) { \
3064 return FLOAT_CLASS_SIGNALING_NAN; \
3065 } else if (float ## bits ## _is_quiet_nan(arg)) { \
3066 return FLOAT_CLASS_QUIET_NAN; \
3067 } else if (float ## bits ## _is_neg(arg)) { \
3068 if (float ## bits ## _is_infinity(arg)) { \
3069 return FLOAT_CLASS_NEGATIVE_INFINITY; \
3070 } else if (float ## bits ## _is_zero(arg)) { \
3071 return FLOAT_CLASS_NEGATIVE_ZERO; \
3072 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3073 return FLOAT_CLASS_NEGATIVE_SUBNORMAL; \
3074 } else { \
3075 return FLOAT_CLASS_NEGATIVE_NORMAL; \
3077 } else { \
3078 if (float ## bits ## _is_infinity(arg)) { \
3079 return FLOAT_CLASS_POSITIVE_INFINITY; \
3080 } else if (float ## bits ## _is_zero(arg)) { \
3081 return FLOAT_CLASS_POSITIVE_ZERO; \
3082 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3083 return FLOAT_CLASS_POSITIVE_SUBNORMAL; \
3084 } else { \
3085 return FLOAT_CLASS_POSITIVE_NORMAL; \
3090 FLOAT_CLASS(class_s, 32)
3091 FLOAT_CLASS(class_d, 64)
3092 #undef FLOAT_CLASS
3094 /* binary operations */
3095 #define FLOAT_BINOP(name) \
3096 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
3097 uint64_t fdt0, uint64_t fdt1) \
3099 uint64_t dt2; \
3101 dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status); \
3102 update_fcr31(env, GETPC()); \
3103 return dt2; \
3106 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
3107 uint32_t fst0, uint32_t fst1) \
3109 uint32_t wt2; \
3111 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3112 update_fcr31(env, GETPC()); \
3113 return wt2; \
3116 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
3117 uint64_t fdt0, \
3118 uint64_t fdt1) \
3120 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3121 uint32_t fsth0 = fdt0 >> 32; \
3122 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3123 uint32_t fsth1 = fdt1 >> 32; \
3124 uint32_t wt2; \
3125 uint32_t wth2; \
3127 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3128 wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status); \
3129 update_fcr31(env, GETPC()); \
3130 return ((uint64_t)wth2 << 32) | wt2; \
3133 FLOAT_BINOP(add)
3134 FLOAT_BINOP(sub)
3135 FLOAT_BINOP(mul)
3136 FLOAT_BINOP(div)
3137 #undef FLOAT_BINOP
3139 /* MIPS specific binary operations */
3140 uint64_t helper_float_recip2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3142 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3143 fdt2 = float64_chs(float64_sub(fdt2, float64_one, &env->active_fpu.fp_status));
3144 update_fcr31(env, GETPC());
3145 return fdt2;
3148 uint32_t helper_float_recip2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3150 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3151 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3152 update_fcr31(env, GETPC());
3153 return fst2;
3156 uint64_t helper_float_recip2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3158 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3159 uint32_t fsth0 = fdt0 >> 32;
3160 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3161 uint32_t fsth2 = fdt2 >> 32;
3163 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3164 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3165 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3166 fsth2 = float32_chs(float32_sub(fsth2, float32_one, &env->active_fpu.fp_status));
3167 update_fcr31(env, GETPC());
3168 return ((uint64_t)fsth2 << 32) | fst2;
3171 uint64_t helper_float_rsqrt2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3173 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3174 fdt2 = float64_sub(fdt2, float64_one, &env->active_fpu.fp_status);
3175 fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
3176 update_fcr31(env, GETPC());
3177 return fdt2;
3180 uint32_t helper_float_rsqrt2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3182 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3183 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3184 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3185 update_fcr31(env, GETPC());
3186 return fst2;
3189 uint64_t helper_float_rsqrt2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3191 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3192 uint32_t fsth0 = fdt0 >> 32;
3193 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3194 uint32_t fsth2 = fdt2 >> 32;
3196 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3197 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3198 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3199 fsth2 = float32_sub(fsth2, float32_one, &env->active_fpu.fp_status);
3200 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3201 fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
3202 update_fcr31(env, GETPC());
3203 return ((uint64_t)fsth2 << 32) | fst2;
3206 uint64_t helper_float_addr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3208 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3209 uint32_t fsth0 = fdt0 >> 32;
3210 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3211 uint32_t fsth1 = fdt1 >> 32;
3212 uint32_t fst2;
3213 uint32_t fsth2;
3215 fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
3216 fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
3217 update_fcr31(env, GETPC());
3218 return ((uint64_t)fsth2 << 32) | fst2;
3221 uint64_t helper_float_mulr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3223 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3224 uint32_t fsth0 = fdt0 >> 32;
3225 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3226 uint32_t fsth1 = fdt1 >> 32;
3227 uint32_t fst2;
3228 uint32_t fsth2;
3230 fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
3231 fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
3232 update_fcr31(env, GETPC());
3233 return ((uint64_t)fsth2 << 32) | fst2;
3236 #define FLOAT_MINMAX(name, bits, minmaxfunc) \
3237 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3238 uint ## bits ## _t fs, \
3239 uint ## bits ## _t ft) \
3241 uint ## bits ## _t fdret; \
3243 fdret = float ## bits ## _ ## minmaxfunc(fs, ft, \
3244 &env->active_fpu.fp_status); \
3245 update_fcr31(env, GETPC()); \
3246 return fdret; \
3249 FLOAT_MINMAX(max_s, 32, maxnum)
3250 FLOAT_MINMAX(max_d, 64, maxnum)
3251 FLOAT_MINMAX(maxa_s, 32, maxnummag)
3252 FLOAT_MINMAX(maxa_d, 64, maxnummag)
3254 FLOAT_MINMAX(min_s, 32, minnum)
3255 FLOAT_MINMAX(min_d, 64, minnum)
3256 FLOAT_MINMAX(mina_s, 32, minnummag)
3257 FLOAT_MINMAX(mina_d, 64, minnummag)
3258 #undef FLOAT_MINMAX
3260 /* ternary operations */
3261 #define UNFUSED_FMA(prefix, a, b, c, flags) \
3263 a = prefix##_mul(a, b, &env->active_fpu.fp_status); \
3264 if ((flags) & float_muladd_negate_c) { \
3265 a = prefix##_sub(a, c, &env->active_fpu.fp_status); \
3266 } else { \
3267 a = prefix##_add(a, c, &env->active_fpu.fp_status); \
3269 if ((flags) & float_muladd_negate_result) { \
3270 a = prefix##_chs(a); \
3274 /* FMA based operations */
3275 #define FLOAT_FMA(name, type) \
3276 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
3277 uint64_t fdt0, uint64_t fdt1, \
3278 uint64_t fdt2) \
3280 UNFUSED_FMA(float64, fdt0, fdt1, fdt2, type); \
3281 update_fcr31(env, GETPC()); \
3282 return fdt0; \
3285 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
3286 uint32_t fst0, uint32_t fst1, \
3287 uint32_t fst2) \
3289 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
3290 update_fcr31(env, GETPC()); \
3291 return fst0; \
3294 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
3295 uint64_t fdt0, uint64_t fdt1, \
3296 uint64_t fdt2) \
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 = fdt2 & 0XFFFFFFFF; \
3303 uint32_t fsth2 = fdt2 >> 32; \
3305 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
3306 UNFUSED_FMA(float32, fsth0, fsth1, fsth2, type); \
3307 update_fcr31(env, GETPC()); \
3308 return ((uint64_t)fsth0 << 32) | fst0; \
3310 FLOAT_FMA(madd, 0)
3311 FLOAT_FMA(msub, float_muladd_negate_c)
3312 FLOAT_FMA(nmadd, float_muladd_negate_result)
3313 FLOAT_FMA(nmsub, float_muladd_negate_result | float_muladd_negate_c)
3314 #undef FLOAT_FMA
3316 #define FLOAT_FMADDSUB(name, bits, muladd_arg) \
3317 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3318 uint ## bits ## _t fs, \
3319 uint ## bits ## _t ft, \
3320 uint ## bits ## _t fd) \
3322 uint ## bits ## _t fdret; \
3324 fdret = float ## bits ## _muladd(fs, ft, fd, muladd_arg, \
3325 &env->active_fpu.fp_status); \
3326 update_fcr31(env, GETPC()); \
3327 return fdret; \
3330 FLOAT_FMADDSUB(maddf_s, 32, 0)
3331 FLOAT_FMADDSUB(maddf_d, 64, 0)
3332 FLOAT_FMADDSUB(msubf_s, 32, float_muladd_negate_product)
3333 FLOAT_FMADDSUB(msubf_d, 64, float_muladd_negate_product)
3334 #undef FLOAT_FMADDSUB
3336 /* compare operations */
3337 #define FOP_COND_D(op, cond) \
3338 void helper_cmp_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3339 uint64_t fdt1, int cc) \
3341 int c; \
3342 c = cond; \
3343 update_fcr31(env, GETPC()); \
3344 if (c) \
3345 SET_FP_COND(cc, env->active_fpu); \
3346 else \
3347 CLEAR_FP_COND(cc, env->active_fpu); \
3349 void helper_cmpabs_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3350 uint64_t fdt1, int cc) \
3352 int c; \
3353 fdt0 = float64_abs(fdt0); \
3354 fdt1 = float64_abs(fdt1); \
3355 c = cond; \
3356 update_fcr31(env, GETPC()); \
3357 if (c) \
3358 SET_FP_COND(cc, env->active_fpu); \
3359 else \
3360 CLEAR_FP_COND(cc, env->active_fpu); \
3363 /* NOTE: the comma operator will make "cond" to eval to false,
3364 * but float64_unordered_quiet() is still called. */
3365 FOP_COND_D(f, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3366 FOP_COND_D(un, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status))
3367 FOP_COND_D(eq, float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3368 FOP_COND_D(ueq, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3369 FOP_COND_D(olt, float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3370 FOP_COND_D(ult, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3371 FOP_COND_D(ole, float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3372 FOP_COND_D(ule, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3373 /* NOTE: the comma operator will make "cond" to eval to false,
3374 * but float64_unordered() is still called. */
3375 FOP_COND_D(sf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3376 FOP_COND_D(ngle,float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status))
3377 FOP_COND_D(seq, float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3378 FOP_COND_D(ngl, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3379 FOP_COND_D(lt, float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3380 FOP_COND_D(nge, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3381 FOP_COND_D(le, float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3382 FOP_COND_D(ngt, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3384 #define FOP_COND_S(op, cond) \
3385 void helper_cmp_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3386 uint32_t fst1, int cc) \
3388 int c; \
3389 c = cond; \
3390 update_fcr31(env, GETPC()); \
3391 if (c) \
3392 SET_FP_COND(cc, env->active_fpu); \
3393 else \
3394 CLEAR_FP_COND(cc, env->active_fpu); \
3396 void helper_cmpabs_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3397 uint32_t fst1, int cc) \
3399 int c; \
3400 fst0 = float32_abs(fst0); \
3401 fst1 = float32_abs(fst1); \
3402 c = cond; \
3403 update_fcr31(env, GETPC()); \
3404 if (c) \
3405 SET_FP_COND(cc, env->active_fpu); \
3406 else \
3407 CLEAR_FP_COND(cc, env->active_fpu); \
3410 /* NOTE: the comma operator will make "cond" to eval to false,
3411 * but float32_unordered_quiet() is still called. */
3412 FOP_COND_S(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3413 FOP_COND_S(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status))
3414 FOP_COND_S(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3415 FOP_COND_S(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3416 FOP_COND_S(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3417 FOP_COND_S(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3418 FOP_COND_S(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3419 FOP_COND_S(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3420 /* NOTE: the comma operator will make "cond" to eval to false,
3421 * but float32_unordered() is still called. */
3422 FOP_COND_S(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3423 FOP_COND_S(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status))
3424 FOP_COND_S(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3425 FOP_COND_S(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3426 FOP_COND_S(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3427 FOP_COND_S(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3428 FOP_COND_S(le, float32_le(fst0, fst1, &env->active_fpu.fp_status))
3429 FOP_COND_S(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
3431 #define FOP_COND_PS(op, condl, condh) \
3432 void helper_cmp_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3433 uint64_t fdt1, int cc) \
3435 uint32_t fst0, fsth0, fst1, fsth1; \
3436 int ch, cl; \
3437 fst0 = fdt0 & 0XFFFFFFFF; \
3438 fsth0 = fdt0 >> 32; \
3439 fst1 = fdt1 & 0XFFFFFFFF; \
3440 fsth1 = fdt1 >> 32; \
3441 cl = condl; \
3442 ch = condh; \
3443 update_fcr31(env, GETPC()); \
3444 if (cl) \
3445 SET_FP_COND(cc, env->active_fpu); \
3446 else \
3447 CLEAR_FP_COND(cc, env->active_fpu); \
3448 if (ch) \
3449 SET_FP_COND(cc + 1, env->active_fpu); \
3450 else \
3451 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3453 void helper_cmpabs_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3454 uint64_t fdt1, int cc) \
3456 uint32_t fst0, fsth0, fst1, fsth1; \
3457 int ch, cl; \
3458 fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
3459 fsth0 = float32_abs(fdt0 >> 32); \
3460 fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
3461 fsth1 = float32_abs(fdt1 >> 32); \
3462 cl = condl; \
3463 ch = condh; \
3464 update_fcr31(env, GETPC()); \
3465 if (cl) \
3466 SET_FP_COND(cc, env->active_fpu); \
3467 else \
3468 CLEAR_FP_COND(cc, env->active_fpu); \
3469 if (ch) \
3470 SET_FP_COND(cc + 1, env->active_fpu); \
3471 else \
3472 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3475 /* NOTE: the comma operator will make "cond" to eval to false,
3476 * but float32_unordered_quiet() is still called. */
3477 FOP_COND_PS(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0),
3478 (float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3479 FOP_COND_PS(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status),
3480 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status))
3481 FOP_COND_PS(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3482 float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3483 FOP_COND_PS(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3484 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3485 FOP_COND_PS(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3486 float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3487 FOP_COND_PS(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3488 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3489 FOP_COND_PS(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3490 float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3491 FOP_COND_PS(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3492 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3493 /* NOTE: the comma operator will make "cond" to eval to false,
3494 * but float32_unordered() is still called. */
3495 FOP_COND_PS(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0),
3496 (float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3497 FOP_COND_PS(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status),
3498 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status))
3499 FOP_COND_PS(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3500 float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3501 FOP_COND_PS(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3502 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3503 FOP_COND_PS(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3504 float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3505 FOP_COND_PS(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3506 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3507 FOP_COND_PS(le, float32_le(fst0, fst1, &env->active_fpu.fp_status),
3508 float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3509 FOP_COND_PS(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
3510 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3512 /* R6 compare operations */
3513 #define FOP_CONDN_D(op, cond) \
3514 uint64_t helper_r6_cmp_d_ ## op(CPUMIPSState * env, uint64_t fdt0, \
3515 uint64_t fdt1) \
3517 uint64_t c; \
3518 c = cond; \
3519 update_fcr31(env, GETPC()); \
3520 if (c) { \
3521 return -1; \
3522 } else { \
3523 return 0; \
3527 /* NOTE: the comma operator will make "cond" to eval to false,
3528 * but float64_unordered_quiet() is still called. */
3529 FOP_CONDN_D(af, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3530 FOP_CONDN_D(un, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)))
3531 FOP_CONDN_D(eq, (float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3532 FOP_CONDN_D(ueq, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3533 || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3534 FOP_CONDN_D(lt, (float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3535 FOP_CONDN_D(ult, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3536 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3537 FOP_CONDN_D(le, (float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3538 FOP_CONDN_D(ule, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3539 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3540 /* NOTE: the comma operator will make "cond" to eval to false,
3541 * but float64_unordered() is still called. */
3542 FOP_CONDN_D(saf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3543 FOP_CONDN_D(sun, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)))
3544 FOP_CONDN_D(seq, (float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
3545 FOP_CONDN_D(sueq, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3546 || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
3547 FOP_CONDN_D(slt, (float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3548 FOP_CONDN_D(sult, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3549 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3550 FOP_CONDN_D(sle, (float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3551 FOP_CONDN_D(sule, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3552 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3553 FOP_CONDN_D(or, (float64_le_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3554 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3555 FOP_CONDN_D(une, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3556 || float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3557 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3558 FOP_CONDN_D(ne, (float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
3559 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
3560 FOP_CONDN_D(sor, (float64_le(fdt1, fdt0, &env->active_fpu.fp_status)
3561 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
3562 FOP_CONDN_D(sune, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
3563 || float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
3564 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3565 FOP_CONDN_D(sne, (float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
3566 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
3568 #define FOP_CONDN_S(op, cond) \
3569 uint32_t helper_r6_cmp_s_ ## op(CPUMIPSState * env, uint32_t fst0, \
3570 uint32_t fst1) \
3572 uint64_t c; \
3573 c = cond; \
3574 update_fcr31(env, GETPC()); \
3575 if (c) { \
3576 return -1; \
3577 } else { \
3578 return 0; \
3582 /* NOTE: the comma operator will make "cond" to eval to false,
3583 * but float32_unordered_quiet() is still called. */
3584 FOP_CONDN_S(af, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3585 FOP_CONDN_S(un, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)))
3586 FOP_CONDN_S(eq, (float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3587 FOP_CONDN_S(ueq, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3588 || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3589 FOP_CONDN_S(lt, (float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3590 FOP_CONDN_S(ult, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3591 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3592 FOP_CONDN_S(le, (float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3593 FOP_CONDN_S(ule, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3594 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3595 /* NOTE: the comma operator will make "cond" to eval to false,
3596 * but float32_unordered() is still called. */
3597 FOP_CONDN_S(saf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3598 FOP_CONDN_S(sun, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)))
3599 FOP_CONDN_S(seq, (float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
3600 FOP_CONDN_S(sueq, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3601 || float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
3602 FOP_CONDN_S(slt, (float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3603 FOP_CONDN_S(sult, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3604 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3605 FOP_CONDN_S(sle, (float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3606 FOP_CONDN_S(sule, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3607 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3608 FOP_CONDN_S(or, (float32_le_quiet(fst1, fst0, &env->active_fpu.fp_status)
3609 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3610 FOP_CONDN_S(une, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
3611 || float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
3612 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3613 FOP_CONDN_S(ne, (float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
3614 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
3615 FOP_CONDN_S(sor, (float32_le(fst1, fst0, &env->active_fpu.fp_status)
3616 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
3617 FOP_CONDN_S(sune, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
3618 || float32_lt(fst1, fst0, &env->active_fpu.fp_status)
3619 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3620 FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status)
3621 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
3623 /* MSA */
3624 /* Data format min and max values */
3625 #define DF_BITS(df) (1 << ((df) + 3))
3627 /* Element-by-element access macros */
3628 #define DF_ELEMENTS(df) (MSA_WRLEN / DF_BITS(df))
3630 #if !defined(CONFIG_USER_ONLY)
3631 #define MEMOP_IDX(DF) \
3632 TCGMemOpIdx oi = make_memop_idx(MO_TE | DF | MO_UNALN, \
3633 cpu_mmu_index(env));
3634 #else
3635 #define MEMOP_IDX(DF)
3636 #endif
3638 #define MSA_LD_DF(DF, TYPE, LD_INSN, ...) \
3639 void helper_msa_ld_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
3640 target_ulong addr) \
3642 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
3643 wr_t wx; \
3644 int i; \
3645 MEMOP_IDX(DF) \
3646 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
3647 wx.TYPE[i] = LD_INSN(env, addr + (i << DF), ##__VA_ARGS__); \
3649 memcpy(pwd, &wx, sizeof(wr_t)); \
3652 #if !defined(CONFIG_USER_ONLY)
3653 MSA_LD_DF(DF_BYTE, b, helper_ret_ldub_mmu, oi, GETRA())
3654 MSA_LD_DF(DF_HALF, h, helper_ret_lduw_mmu, oi, GETRA())
3655 MSA_LD_DF(DF_WORD, w, helper_ret_ldul_mmu, oi, GETRA())
3656 MSA_LD_DF(DF_DOUBLE, d, helper_ret_ldq_mmu, oi, GETRA())
3657 #else
3658 MSA_LD_DF(DF_BYTE, b, cpu_ldub_data)
3659 MSA_LD_DF(DF_HALF, h, cpu_lduw_data)
3660 MSA_LD_DF(DF_WORD, w, cpu_ldl_data)
3661 MSA_LD_DF(DF_DOUBLE, d, cpu_ldq_data)
3662 #endif
3664 #define MSA_PAGESPAN(x) \
3665 ((((x) & ~TARGET_PAGE_MASK) + MSA_WRLEN/8 - 1) >= TARGET_PAGE_SIZE)
3667 static inline void ensure_writable_pages(CPUMIPSState *env,
3668 target_ulong addr,
3669 int mmu_idx,
3670 uintptr_t retaddr)
3672 #if !defined(CONFIG_USER_ONLY)
3673 target_ulong page_addr;
3674 if (unlikely(MSA_PAGESPAN(addr))) {
3675 /* first page */
3676 probe_write(env, addr, mmu_idx, retaddr);
3677 /* second page */
3678 page_addr = (addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
3679 probe_write(env, page_addr, mmu_idx, retaddr);
3681 #endif
3684 #define MSA_ST_DF(DF, TYPE, ST_INSN, ...) \
3685 void helper_msa_st_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
3686 target_ulong addr) \
3688 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
3689 int mmu_idx = cpu_mmu_index(env); \
3690 int i; \
3691 MEMOP_IDX(DF) \
3692 ensure_writable_pages(env, addr, mmu_idx, GETRA()); \
3693 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
3694 ST_INSN(env, addr + (i << DF), pwd->TYPE[i], ##__VA_ARGS__); \
3698 #if !defined(CONFIG_USER_ONLY)
3699 MSA_ST_DF(DF_BYTE, b, helper_ret_stb_mmu, oi, GETRA())
3700 MSA_ST_DF(DF_HALF, h, helper_ret_stw_mmu, oi, GETRA())
3701 MSA_ST_DF(DF_WORD, w, helper_ret_stl_mmu, oi, GETRA())
3702 MSA_ST_DF(DF_DOUBLE, d, helper_ret_stq_mmu, oi, GETRA())
3703 #else
3704 MSA_ST_DF(DF_BYTE, b, cpu_stb_data)
3705 MSA_ST_DF(DF_HALF, h, cpu_stw_data)
3706 MSA_ST_DF(DF_WORD, w, cpu_stl_data)
3707 MSA_ST_DF(DF_DOUBLE, d, cpu_stq_data)
3708 #endif