iostatus: reorganize io error code
[qemu/opensuse.git] / target-mips / op_helper.c
blobce5ddaf050906a997674d06fe141066ba95cb98f
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 "host-utils.h"
23 #include "helper.h"
25 #if !defined(CONFIG_USER_ONLY)
26 #include "softmmu_exec.h"
27 #endif /* !defined(CONFIG_USER_ONLY) */
29 #ifndef CONFIG_USER_ONLY
30 static inline void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global);
31 #endif
33 /*****************************************************************************/
34 /* Exceptions processing helpers */
36 void helper_raise_exception_err(CPUMIPSState *env, uint32_t exception,
37 int error_code)
39 #if 1
40 if (exception < 0x100)
41 qemu_log("%s: %d %d\n", __func__, exception, error_code);
42 #endif
43 env->exception_index = exception;
44 env->error_code = error_code;
45 cpu_loop_exit(env);
48 void helper_raise_exception(CPUMIPSState *env, uint32_t exception)
50 helper_raise_exception_err(env, exception, 0);
53 #if !defined(CONFIG_USER_ONLY)
54 static void do_restore_state(CPUMIPSState *env, uintptr_t pc)
56 TranslationBlock *tb;
58 tb = tb_find_pc (pc);
59 if (tb) {
60 cpu_restore_state(tb, env, pc);
63 #endif
65 #if defined(CONFIG_USER_ONLY)
66 #define HELPER_LD(name, insn, type) \
67 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
68 int mem_idx) \
69 { \
70 return (type) insn##_raw(addr); \
72 #else
73 #define HELPER_LD(name, insn, type) \
74 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
75 int mem_idx) \
76 { \
77 switch (mem_idx) \
78 { \
79 case 0: return (type) cpu_##insn##_kernel(env, addr); break; \
80 case 1: return (type) cpu_##insn##_super(env, addr); break; \
81 default: \
82 case 2: return (type) cpu_##insn##_user(env, addr); break; \
83 } \
85 #endif
86 HELPER_LD(lbu, ldub, uint8_t)
87 HELPER_LD(lw, ldl, int32_t)
88 #ifdef TARGET_MIPS64
89 HELPER_LD(ld, ldq, int64_t)
90 #endif
91 #undef HELPER_LD
93 #if defined(CONFIG_USER_ONLY)
94 #define HELPER_ST(name, insn, type) \
95 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
96 type val, int mem_idx) \
97 { \
98 insn##_raw(addr, val); \
100 #else
101 #define HELPER_ST(name, insn, type) \
102 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
103 type val, int mem_idx) \
105 switch (mem_idx) \
107 case 0: cpu_##insn##_kernel(env, addr, val); break; \
108 case 1: cpu_##insn##_super(env, addr, val); break; \
109 default: \
110 case 2: cpu_##insn##_user(env, addr, val); break; \
113 #endif
114 HELPER_ST(sb, stb, uint8_t)
115 HELPER_ST(sw, stl, uint32_t)
116 #ifdef TARGET_MIPS64
117 HELPER_ST(sd, stq, uint64_t)
118 #endif
119 #undef HELPER_ST
121 target_ulong helper_clo (target_ulong arg1)
123 return clo32(arg1);
126 target_ulong helper_clz (target_ulong arg1)
128 return clz32(arg1);
131 #if defined(TARGET_MIPS64)
132 target_ulong helper_dclo (target_ulong arg1)
134 return clo64(arg1);
137 target_ulong helper_dclz (target_ulong arg1)
139 return clz64(arg1);
141 #endif /* TARGET_MIPS64 */
143 /* 64 bits arithmetic for 32 bits hosts */
144 static inline uint64_t get_HILO(CPUMIPSState *env)
146 return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
149 static inline target_ulong set_HIT0_LO(CPUMIPSState *env, uint64_t HILO)
151 target_ulong tmp;
152 env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
153 tmp = env->active_tc.HI[0] = (int32_t)(HILO >> 32);
154 return tmp;
157 static inline target_ulong set_HI_LOT0(CPUMIPSState *env, uint64_t HILO)
159 target_ulong tmp = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
160 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
161 return tmp;
164 /* Multiplication variants of the vr54xx. */
165 target_ulong helper_muls(CPUMIPSState *env, target_ulong arg1,
166 target_ulong arg2)
168 return set_HI_LOT0(env, 0 - ((int64_t)(int32_t)arg1 *
169 (int64_t)(int32_t)arg2));
172 target_ulong helper_mulsu(CPUMIPSState *env, target_ulong arg1,
173 target_ulong arg2)
175 return set_HI_LOT0(env, 0 - (uint64_t)(uint32_t)arg1 *
176 (uint64_t)(uint32_t)arg2);
179 target_ulong helper_macc(CPUMIPSState *env, target_ulong arg1,
180 target_ulong arg2)
182 return set_HI_LOT0(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
183 (int64_t)(int32_t)arg2);
186 target_ulong helper_macchi(CPUMIPSState *env, target_ulong arg1,
187 target_ulong arg2)
189 return set_HIT0_LO(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
190 (int64_t)(int32_t)arg2);
193 target_ulong helper_maccu(CPUMIPSState *env, target_ulong arg1,
194 target_ulong arg2)
196 return set_HI_LOT0(env, (uint64_t)get_HILO(env) +
197 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
200 target_ulong helper_macchiu(CPUMIPSState *env, target_ulong arg1,
201 target_ulong arg2)
203 return set_HIT0_LO(env, (uint64_t)get_HILO(env) +
204 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
207 target_ulong helper_msac(CPUMIPSState *env, target_ulong arg1,
208 target_ulong arg2)
210 return set_HI_LOT0(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
211 (int64_t)(int32_t)arg2);
214 target_ulong helper_msachi(CPUMIPSState *env, target_ulong arg1,
215 target_ulong arg2)
217 return set_HIT0_LO(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
218 (int64_t)(int32_t)arg2);
221 target_ulong helper_msacu(CPUMIPSState *env, target_ulong arg1,
222 target_ulong arg2)
224 return set_HI_LOT0(env, (uint64_t)get_HILO(env) -
225 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
228 target_ulong helper_msachiu(CPUMIPSState *env, target_ulong arg1,
229 target_ulong arg2)
231 return set_HIT0_LO(env, (uint64_t)get_HILO(env) -
232 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
235 target_ulong helper_mulhi(CPUMIPSState *env, target_ulong arg1,
236 target_ulong arg2)
238 return set_HIT0_LO(env, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
241 target_ulong helper_mulhiu(CPUMIPSState *env, target_ulong arg1,
242 target_ulong arg2)
244 return set_HIT0_LO(env, (uint64_t)(uint32_t)arg1 *
245 (uint64_t)(uint32_t)arg2);
248 target_ulong helper_mulshi(CPUMIPSState *env, target_ulong arg1,
249 target_ulong arg2)
251 return set_HIT0_LO(env, 0 - (int64_t)(int32_t)arg1 *
252 (int64_t)(int32_t)arg2);
255 target_ulong helper_mulshiu(CPUMIPSState *env, target_ulong arg1,
256 target_ulong arg2)
258 return set_HIT0_LO(env, 0 - (uint64_t)(uint32_t)arg1 *
259 (uint64_t)(uint32_t)arg2);
262 #ifdef TARGET_MIPS64
263 void helper_dmult(CPUMIPSState *env, target_ulong arg1, target_ulong arg2)
265 muls64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
268 void helper_dmultu(CPUMIPSState *env, target_ulong arg1, target_ulong arg2)
270 mulu64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
272 #endif
274 #ifndef CONFIG_USER_ONLY
276 static inline target_phys_addr_t do_translate_address(CPUMIPSState *env,
277 target_ulong address,
278 int rw)
280 target_phys_addr_t lladdr;
282 lladdr = cpu_mips_translate_address(env, address, rw);
284 if (lladdr == -1LL) {
285 cpu_loop_exit(env);
286 } else {
287 return lladdr;
291 #define HELPER_LD_ATOMIC(name, insn) \
292 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg, int mem_idx) \
294 env->lladdr = do_translate_address(env, arg, 0); \
295 env->llval = do_##insn(env, arg, mem_idx); \
296 return env->llval; \
298 HELPER_LD_ATOMIC(ll, lw)
299 #ifdef TARGET_MIPS64
300 HELPER_LD_ATOMIC(lld, ld)
301 #endif
302 #undef HELPER_LD_ATOMIC
304 #define HELPER_ST_ATOMIC(name, ld_insn, st_insn, almask) \
305 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg1, \
306 target_ulong arg2, int mem_idx) \
308 target_long tmp; \
310 if (arg2 & almask) { \
311 env->CP0_BadVAddr = arg2; \
312 helper_raise_exception(env, EXCP_AdES); \
314 if (do_translate_address(env, arg2, 1) == env->lladdr) { \
315 tmp = do_##ld_insn(env, arg2, mem_idx); \
316 if (tmp == env->llval) { \
317 do_##st_insn(env, arg2, arg1, mem_idx); \
318 return 1; \
321 return 0; \
323 HELPER_ST_ATOMIC(sc, lw, sw, 0x3)
324 #ifdef TARGET_MIPS64
325 HELPER_ST_ATOMIC(scd, ld, sd, 0x7)
326 #endif
327 #undef HELPER_ST_ATOMIC
328 #endif
330 #ifdef TARGET_WORDS_BIGENDIAN
331 #define GET_LMASK(v) ((v) & 3)
332 #define GET_OFFSET(addr, offset) (addr + (offset))
333 #else
334 #define GET_LMASK(v) (((v) & 3) ^ 3)
335 #define GET_OFFSET(addr, offset) (addr - (offset))
336 #endif
338 target_ulong helper_lwl(CPUMIPSState *env, target_ulong arg1,
339 target_ulong arg2, int mem_idx)
341 target_ulong tmp;
343 tmp = do_lbu(env, arg2, mem_idx);
344 arg1 = (arg1 & 0x00FFFFFF) | (tmp << 24);
346 if (GET_LMASK(arg2) <= 2) {
347 tmp = do_lbu(env, GET_OFFSET(arg2, 1), mem_idx);
348 arg1 = (arg1 & 0xFF00FFFF) | (tmp << 16);
351 if (GET_LMASK(arg2) <= 1) {
352 tmp = do_lbu(env, GET_OFFSET(arg2, 2), mem_idx);
353 arg1 = (arg1 & 0xFFFF00FF) | (tmp << 8);
356 if (GET_LMASK(arg2) == 0) {
357 tmp = do_lbu(env, GET_OFFSET(arg2, 3), mem_idx);
358 arg1 = (arg1 & 0xFFFFFF00) | tmp;
360 return (int32_t)arg1;
363 target_ulong helper_lwr(CPUMIPSState *env, target_ulong arg1,
364 target_ulong arg2, int mem_idx)
366 target_ulong tmp;
368 tmp = do_lbu(env, arg2, mem_idx);
369 arg1 = (arg1 & 0xFFFFFF00) | tmp;
371 if (GET_LMASK(arg2) >= 1) {
372 tmp = do_lbu(env, GET_OFFSET(arg2, -1), mem_idx);
373 arg1 = (arg1 & 0xFFFF00FF) | (tmp << 8);
376 if (GET_LMASK(arg2) >= 2) {
377 tmp = do_lbu(env, GET_OFFSET(arg2, -2), mem_idx);
378 arg1 = (arg1 & 0xFF00FFFF) | (tmp << 16);
381 if (GET_LMASK(arg2) == 3) {
382 tmp = do_lbu(env, GET_OFFSET(arg2, -3), mem_idx);
383 arg1 = (arg1 & 0x00FFFFFF) | (tmp << 24);
385 return (int32_t)arg1;
388 void helper_swl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
389 int mem_idx)
391 do_sb(env, arg2, (uint8_t)(arg1 >> 24), mem_idx);
393 if (GET_LMASK(arg2) <= 2)
394 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16), mem_idx);
396 if (GET_LMASK(arg2) <= 1)
397 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8), mem_idx);
399 if (GET_LMASK(arg2) == 0)
400 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)arg1, mem_idx);
403 void helper_swr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
404 int mem_idx)
406 do_sb(env, arg2, (uint8_t)arg1, mem_idx);
408 if (GET_LMASK(arg2) >= 1)
409 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
411 if (GET_LMASK(arg2) >= 2)
412 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
414 if (GET_LMASK(arg2) == 3)
415 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
418 #if defined(TARGET_MIPS64)
419 /* "half" load and stores. We must do the memory access inline,
420 or fault handling won't work. */
422 #ifdef TARGET_WORDS_BIGENDIAN
423 #define GET_LMASK64(v) ((v) & 7)
424 #else
425 #define GET_LMASK64(v) (((v) & 7) ^ 7)
426 #endif
428 target_ulong helper_ldl(CPUMIPSState *env, target_ulong arg1,
429 target_ulong arg2, int mem_idx)
431 uint64_t tmp;
433 tmp = do_lbu(env, arg2, mem_idx);
434 arg1 = (arg1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
436 if (GET_LMASK64(arg2) <= 6) {
437 tmp = do_lbu(env, GET_OFFSET(arg2, 1), mem_idx);
438 arg1 = (arg1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
441 if (GET_LMASK64(arg2) <= 5) {
442 tmp = do_lbu(env, GET_OFFSET(arg2, 2), mem_idx);
443 arg1 = (arg1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
446 if (GET_LMASK64(arg2) <= 4) {
447 tmp = do_lbu(env, GET_OFFSET(arg2, 3), mem_idx);
448 arg1 = (arg1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
451 if (GET_LMASK64(arg2) <= 3) {
452 tmp = do_lbu(env, GET_OFFSET(arg2, 4), mem_idx);
453 arg1 = (arg1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
456 if (GET_LMASK64(arg2) <= 2) {
457 tmp = do_lbu(env, GET_OFFSET(arg2, 5), mem_idx);
458 arg1 = (arg1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
461 if (GET_LMASK64(arg2) <= 1) {
462 tmp = do_lbu(env, GET_OFFSET(arg2, 6), mem_idx);
463 arg1 = (arg1 & 0xFFFFFFFFFFFF00FFULL) | (tmp << 8);
466 if (GET_LMASK64(arg2) == 0) {
467 tmp = do_lbu(env, GET_OFFSET(arg2, 7), mem_idx);
468 arg1 = (arg1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
471 return arg1;
474 target_ulong helper_ldr(CPUMIPSState *env, target_ulong arg1,
475 target_ulong arg2, int mem_idx)
477 uint64_t tmp;
479 tmp = do_lbu(env, arg2, mem_idx);
480 arg1 = (arg1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
482 if (GET_LMASK64(arg2) >= 1) {
483 tmp = do_lbu(env, GET_OFFSET(arg2, -1), mem_idx);
484 arg1 = (arg1 & 0xFFFFFFFFFFFF00FFULL) | (tmp << 8);
487 if (GET_LMASK64(arg2) >= 2) {
488 tmp = do_lbu(env, GET_OFFSET(arg2, -2), mem_idx);
489 arg1 = (arg1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
492 if (GET_LMASK64(arg2) >= 3) {
493 tmp = do_lbu(env, GET_OFFSET(arg2, -3), mem_idx);
494 arg1 = (arg1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
497 if (GET_LMASK64(arg2) >= 4) {
498 tmp = do_lbu(env, GET_OFFSET(arg2, -4), mem_idx);
499 arg1 = (arg1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
502 if (GET_LMASK64(arg2) >= 5) {
503 tmp = do_lbu(env, GET_OFFSET(arg2, -5), mem_idx);
504 arg1 = (arg1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
507 if (GET_LMASK64(arg2) >= 6) {
508 tmp = do_lbu(env, GET_OFFSET(arg2, -6), mem_idx);
509 arg1 = (arg1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
512 if (GET_LMASK64(arg2) == 7) {
513 tmp = do_lbu(env, GET_OFFSET(arg2, -7), mem_idx);
514 arg1 = (arg1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
517 return arg1;
520 void helper_sdl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
521 int mem_idx)
523 do_sb(env, arg2, (uint8_t)(arg1 >> 56), mem_idx);
525 if (GET_LMASK64(arg2) <= 6)
526 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48), mem_idx);
528 if (GET_LMASK64(arg2) <= 5)
529 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40), mem_idx);
531 if (GET_LMASK64(arg2) <= 4)
532 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32), mem_idx);
534 if (GET_LMASK64(arg2) <= 3)
535 do_sb(env, GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24), mem_idx);
537 if (GET_LMASK64(arg2) <= 2)
538 do_sb(env, GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16), mem_idx);
540 if (GET_LMASK64(arg2) <= 1)
541 do_sb(env, GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8), mem_idx);
543 if (GET_LMASK64(arg2) <= 0)
544 do_sb(env, GET_OFFSET(arg2, 7), (uint8_t)arg1, mem_idx);
547 void helper_sdr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
548 int mem_idx)
550 do_sb(env, arg2, (uint8_t)arg1, mem_idx);
552 if (GET_LMASK64(arg2) >= 1)
553 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
555 if (GET_LMASK64(arg2) >= 2)
556 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
558 if (GET_LMASK64(arg2) >= 3)
559 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
561 if (GET_LMASK64(arg2) >= 4)
562 do_sb(env, GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32), mem_idx);
564 if (GET_LMASK64(arg2) >= 5)
565 do_sb(env, GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40), mem_idx);
567 if (GET_LMASK64(arg2) >= 6)
568 do_sb(env, GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48), mem_idx);
570 if (GET_LMASK64(arg2) == 7)
571 do_sb(env, GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56), mem_idx);
573 #endif /* TARGET_MIPS64 */
575 static const int multiple_regs[] = { 16, 17, 18, 19, 20, 21, 22, 23, 30 };
577 void helper_lwm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
578 uint32_t mem_idx)
580 target_ulong base_reglist = reglist & 0xf;
581 target_ulong do_r31 = reglist & 0x10;
582 #ifdef CONFIG_USER_ONLY
583 #undef ldfun
584 #define ldfun(env, addr) ldl_raw(addr)
585 #else
586 uint32_t (*ldfun)(CPUMIPSState *env, target_ulong);
588 switch (mem_idx)
590 case 0: ldfun = cpu_ldl_kernel; break;
591 case 1: ldfun = cpu_ldl_super; break;
592 default:
593 case 2: ldfun = cpu_ldl_user; break;
595 #endif
597 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
598 target_ulong i;
600 for (i = 0; i < base_reglist; i++) {
601 env->active_tc.gpr[multiple_regs[i]] = (target_long)ldfun(env, addr);
602 addr += 4;
606 if (do_r31) {
607 env->active_tc.gpr[31] = (target_long)ldfun(env, addr);
611 void helper_swm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
612 uint32_t mem_idx)
614 target_ulong base_reglist = reglist & 0xf;
615 target_ulong do_r31 = reglist & 0x10;
616 #ifdef CONFIG_USER_ONLY
617 #undef stfun
618 #define stfun(env, addr, val) stl_raw(addr, val)
619 #else
620 void (*stfun)(CPUMIPSState *env, target_ulong, uint32_t);
622 switch (mem_idx)
624 case 0: stfun = cpu_stl_kernel; break;
625 case 1: stfun = cpu_stl_super; break;
626 default:
627 case 2: stfun = cpu_stl_user; break;
629 #endif
631 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
632 target_ulong i;
634 for (i = 0; i < base_reglist; i++) {
635 stfun(env, addr, env->active_tc.gpr[multiple_regs[i]]);
636 addr += 4;
640 if (do_r31) {
641 stfun(env, addr, env->active_tc.gpr[31]);
645 #if defined(TARGET_MIPS64)
646 void helper_ldm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
647 uint32_t mem_idx)
649 target_ulong base_reglist = reglist & 0xf;
650 target_ulong do_r31 = reglist & 0x10;
651 #ifdef CONFIG_USER_ONLY
652 #undef ldfun
653 #define ldfun(env, addr) ldq_raw(addr)
654 #else
655 uint64_t (*ldfun)(CPUMIPSState *env, target_ulong);
657 switch (mem_idx)
659 case 0: ldfun = cpu_ldq_kernel; break;
660 case 1: ldfun = cpu_ldq_super; break;
661 default:
662 case 2: ldfun = cpu_ldq_user; break;
664 #endif
666 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
667 target_ulong i;
669 for (i = 0; i < base_reglist; i++) {
670 env->active_tc.gpr[multiple_regs[i]] = ldfun(env, addr);
671 addr += 8;
675 if (do_r31) {
676 env->active_tc.gpr[31] = ldfun(env, addr);
680 void helper_sdm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
681 uint32_t mem_idx)
683 target_ulong base_reglist = reglist & 0xf;
684 target_ulong do_r31 = reglist & 0x10;
685 #ifdef CONFIG_USER_ONLY
686 #undef stfun
687 #define stfun(env, addr, val) stq_raw(addr, val)
688 #else
689 void (*stfun)(CPUMIPSState *env, target_ulong, uint64_t);
691 switch (mem_idx)
693 case 0: stfun = cpu_stq_kernel; break;
694 case 1: stfun = cpu_stq_super; break;
695 default:
696 case 2: stfun = cpu_stq_user; break;
698 #endif
700 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
701 target_ulong i;
703 for (i = 0; i < base_reglist; i++) {
704 stfun(env, addr, env->active_tc.gpr[multiple_regs[i]]);
705 addr += 8;
709 if (do_r31) {
710 stfun(env, addr, env->active_tc.gpr[31]);
713 #endif
715 #ifndef CONFIG_USER_ONLY
716 /* SMP helpers. */
717 static int mips_vpe_is_wfi(CPUMIPSState *c)
719 /* If the VPE is halted but otherwise active, it means it's waiting for
720 an interrupt. */
721 return c->halted && mips_vpe_active(c);
724 static inline void mips_vpe_wake(CPUMIPSState *c)
726 /* Dont set ->halted = 0 directly, let it be done via cpu_has_work
727 because there might be other conditions that state that c should
728 be sleeping. */
729 cpu_interrupt(c, CPU_INTERRUPT_WAKE);
732 static inline void mips_vpe_sleep(CPUMIPSState *c)
734 /* The VPE was shut off, really go to bed.
735 Reset any old _WAKE requests. */
736 c->halted = 1;
737 cpu_reset_interrupt(c, CPU_INTERRUPT_WAKE);
740 static inline void mips_tc_wake(CPUMIPSState *c, int tc)
742 /* FIXME: TC reschedule. */
743 if (mips_vpe_active(c) && !mips_vpe_is_wfi(c)) {
744 mips_vpe_wake(c);
748 static inline void mips_tc_sleep(CPUMIPSState *c, int tc)
750 /* FIXME: TC reschedule. */
751 if (!mips_vpe_active(c)) {
752 mips_vpe_sleep(c);
756 /* tc should point to an int with the value of the global TC index.
757 This function will transform it into a local index within the
758 returned CPUMIPSState.
760 FIXME: This code assumes that all VPEs have the same number of TCs,
761 which depends on runtime setup. Can probably be fixed by
762 walking the list of CPUMIPSStates. */
763 static CPUMIPSState *mips_cpu_map_tc(CPUMIPSState *env, int *tc)
765 CPUMIPSState *other;
766 int vpe_idx, nr_threads = env->nr_threads;
767 int tc_idx = *tc;
769 if (!(env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))) {
770 /* Not allowed to address other CPUs. */
771 *tc = env->current_tc;
772 return env;
775 vpe_idx = tc_idx / nr_threads;
776 *tc = tc_idx % nr_threads;
777 other = qemu_get_cpu(vpe_idx);
778 return other ? other : env;
781 /* The per VPE CP0_Status register shares some fields with the per TC
782 CP0_TCStatus registers. These fields are wired to the same registers,
783 so changes to either of them should be reflected on both registers.
785 Also, EntryHi shares the bottom 8 bit ASID with TCStauts.
787 These helper call synchronizes the regs for a given cpu. */
789 /* Called for updates to CP0_Status. */
790 static void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu, int tc)
792 int32_t tcstatus, *tcst;
793 uint32_t v = cpu->CP0_Status;
794 uint32_t cu, mx, asid, ksu;
795 uint32_t mask = ((1 << CP0TCSt_TCU3)
796 | (1 << CP0TCSt_TCU2)
797 | (1 << CP0TCSt_TCU1)
798 | (1 << CP0TCSt_TCU0)
799 | (1 << CP0TCSt_TMX)
800 | (3 << CP0TCSt_TKSU)
801 | (0xff << CP0TCSt_TASID));
803 cu = (v >> CP0St_CU0) & 0xf;
804 mx = (v >> CP0St_MX) & 0x1;
805 ksu = (v >> CP0St_KSU) & 0x3;
806 asid = env->CP0_EntryHi & 0xff;
808 tcstatus = cu << CP0TCSt_TCU0;
809 tcstatus |= mx << CP0TCSt_TMX;
810 tcstatus |= ksu << CP0TCSt_TKSU;
811 tcstatus |= asid;
813 if (tc == cpu->current_tc) {
814 tcst = &cpu->active_tc.CP0_TCStatus;
815 } else {
816 tcst = &cpu->tcs[tc].CP0_TCStatus;
819 *tcst &= ~mask;
820 *tcst |= tcstatus;
821 compute_hflags(cpu);
824 /* Called for updates to CP0_TCStatus. */
825 static void sync_c0_tcstatus(CPUMIPSState *cpu, int tc,
826 target_ulong v)
828 uint32_t status;
829 uint32_t tcu, tmx, tasid, tksu;
830 uint32_t mask = ((1 << CP0St_CU3)
831 | (1 << CP0St_CU2)
832 | (1 << CP0St_CU1)
833 | (1 << CP0St_CU0)
834 | (1 << CP0St_MX)
835 | (3 << CP0St_KSU));
837 tcu = (v >> CP0TCSt_TCU0) & 0xf;
838 tmx = (v >> CP0TCSt_TMX) & 0x1;
839 tasid = v & 0xff;
840 tksu = (v >> CP0TCSt_TKSU) & 0x3;
842 status = tcu << CP0St_CU0;
843 status |= tmx << CP0St_MX;
844 status |= tksu << CP0St_KSU;
846 cpu->CP0_Status &= ~mask;
847 cpu->CP0_Status |= status;
849 /* Sync the TASID with EntryHi. */
850 cpu->CP0_EntryHi &= ~0xff;
851 cpu->CP0_EntryHi = tasid;
853 compute_hflags(cpu);
856 /* Called for updates to CP0_EntryHi. */
857 static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
859 int32_t *tcst;
860 uint32_t asid, v = cpu->CP0_EntryHi;
862 asid = v & 0xff;
864 if (tc == cpu->current_tc) {
865 tcst = &cpu->active_tc.CP0_TCStatus;
866 } else {
867 tcst = &cpu->tcs[tc].CP0_TCStatus;
870 *tcst &= ~0xff;
871 *tcst |= asid;
874 /* CP0 helpers */
875 target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
877 return env->mvp->CP0_MVPControl;
880 target_ulong helper_mfc0_mvpconf0(CPUMIPSState *env)
882 return env->mvp->CP0_MVPConf0;
885 target_ulong helper_mfc0_mvpconf1(CPUMIPSState *env)
887 return env->mvp->CP0_MVPConf1;
890 target_ulong helper_mfc0_random(CPUMIPSState *env)
892 return (int32_t)cpu_mips_get_random(env);
895 target_ulong helper_mfc0_tcstatus(CPUMIPSState *env)
897 return env->active_tc.CP0_TCStatus;
900 target_ulong helper_mftc0_tcstatus(CPUMIPSState *env)
902 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
903 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
905 if (other_tc == other->current_tc)
906 return other->active_tc.CP0_TCStatus;
907 else
908 return other->tcs[other_tc].CP0_TCStatus;
911 target_ulong helper_mfc0_tcbind(CPUMIPSState *env)
913 return env->active_tc.CP0_TCBind;
916 target_ulong helper_mftc0_tcbind(CPUMIPSState *env)
918 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
919 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
921 if (other_tc == other->current_tc)
922 return other->active_tc.CP0_TCBind;
923 else
924 return other->tcs[other_tc].CP0_TCBind;
927 target_ulong helper_mfc0_tcrestart(CPUMIPSState *env)
929 return env->active_tc.PC;
932 target_ulong helper_mftc0_tcrestart(CPUMIPSState *env)
934 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
935 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
937 if (other_tc == other->current_tc)
938 return other->active_tc.PC;
939 else
940 return other->tcs[other_tc].PC;
943 target_ulong helper_mfc0_tchalt(CPUMIPSState *env)
945 return env->active_tc.CP0_TCHalt;
948 target_ulong helper_mftc0_tchalt(CPUMIPSState *env)
950 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
951 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
953 if (other_tc == other->current_tc)
954 return other->active_tc.CP0_TCHalt;
955 else
956 return other->tcs[other_tc].CP0_TCHalt;
959 target_ulong helper_mfc0_tccontext(CPUMIPSState *env)
961 return env->active_tc.CP0_TCContext;
964 target_ulong helper_mftc0_tccontext(CPUMIPSState *env)
966 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
967 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
969 if (other_tc == other->current_tc)
970 return other->active_tc.CP0_TCContext;
971 else
972 return other->tcs[other_tc].CP0_TCContext;
975 target_ulong helper_mfc0_tcschedule(CPUMIPSState *env)
977 return env->active_tc.CP0_TCSchedule;
980 target_ulong helper_mftc0_tcschedule(CPUMIPSState *env)
982 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
983 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
985 if (other_tc == other->current_tc)
986 return other->active_tc.CP0_TCSchedule;
987 else
988 return other->tcs[other_tc].CP0_TCSchedule;
991 target_ulong helper_mfc0_tcschefback(CPUMIPSState *env)
993 return env->active_tc.CP0_TCScheFBack;
996 target_ulong helper_mftc0_tcschefback(CPUMIPSState *env)
998 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
999 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1001 if (other_tc == other->current_tc)
1002 return other->active_tc.CP0_TCScheFBack;
1003 else
1004 return other->tcs[other_tc].CP0_TCScheFBack;
1007 target_ulong helper_mfc0_count(CPUMIPSState *env)
1009 return (int32_t)cpu_mips_get_count(env);
1012 target_ulong helper_mftc0_entryhi(CPUMIPSState *env)
1014 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1015 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1017 return other->CP0_EntryHi;
1020 target_ulong helper_mftc0_cause(CPUMIPSState *env)
1022 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1023 int32_t tccause;
1024 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1026 if (other_tc == other->current_tc) {
1027 tccause = other->CP0_Cause;
1028 } else {
1029 tccause = other->CP0_Cause;
1032 return tccause;
1035 target_ulong helper_mftc0_status(CPUMIPSState *env)
1037 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1038 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1040 return other->CP0_Status;
1043 target_ulong helper_mfc0_lladdr(CPUMIPSState *env)
1045 return (int32_t)(env->lladdr >> env->CP0_LLAddr_shift);
1048 target_ulong helper_mfc0_watchlo(CPUMIPSState *env, uint32_t sel)
1050 return (int32_t)env->CP0_WatchLo[sel];
1053 target_ulong helper_mfc0_watchhi(CPUMIPSState *env, uint32_t sel)
1055 return env->CP0_WatchHi[sel];
1058 target_ulong helper_mfc0_debug(CPUMIPSState *env)
1060 target_ulong t0 = env->CP0_Debug;
1061 if (env->hflags & MIPS_HFLAG_DM)
1062 t0 |= 1 << CP0DB_DM;
1064 return t0;
1067 target_ulong helper_mftc0_debug(CPUMIPSState *env)
1069 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1070 int32_t tcstatus;
1071 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1073 if (other_tc == other->current_tc)
1074 tcstatus = other->active_tc.CP0_Debug_tcstatus;
1075 else
1076 tcstatus = other->tcs[other_tc].CP0_Debug_tcstatus;
1078 /* XXX: Might be wrong, check with EJTAG spec. */
1079 return (other->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1080 (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1083 #if defined(TARGET_MIPS64)
1084 target_ulong helper_dmfc0_tcrestart(CPUMIPSState *env)
1086 return env->active_tc.PC;
1089 target_ulong helper_dmfc0_tchalt(CPUMIPSState *env)
1091 return env->active_tc.CP0_TCHalt;
1094 target_ulong helper_dmfc0_tccontext(CPUMIPSState *env)
1096 return env->active_tc.CP0_TCContext;
1099 target_ulong helper_dmfc0_tcschedule(CPUMIPSState *env)
1101 return env->active_tc.CP0_TCSchedule;
1104 target_ulong helper_dmfc0_tcschefback(CPUMIPSState *env)
1106 return env->active_tc.CP0_TCScheFBack;
1109 target_ulong helper_dmfc0_lladdr(CPUMIPSState *env)
1111 return env->lladdr >> env->CP0_LLAddr_shift;
1114 target_ulong helper_dmfc0_watchlo(CPUMIPSState *env, uint32_t sel)
1116 return env->CP0_WatchLo[sel];
1118 #endif /* TARGET_MIPS64 */
1120 void helper_mtc0_index(CPUMIPSState *env, target_ulong arg1)
1122 int num = 1;
1123 unsigned int tmp = env->tlb->nb_tlb;
1125 do {
1126 tmp >>= 1;
1127 num <<= 1;
1128 } while (tmp);
1129 env->CP0_Index = (env->CP0_Index & 0x80000000) | (arg1 & (num - 1));
1132 void helper_mtc0_mvpcontrol(CPUMIPSState *env, target_ulong arg1)
1134 uint32_t mask = 0;
1135 uint32_t newval;
1137 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
1138 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
1139 (1 << CP0MVPCo_EVP);
1140 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1141 mask |= (1 << CP0MVPCo_STLB);
1142 newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
1144 // TODO: Enable/disable shared TLB, enable/disable VPEs.
1146 env->mvp->CP0_MVPControl = newval;
1149 void helper_mtc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
1151 uint32_t mask;
1152 uint32_t newval;
1154 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
1155 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1156 newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
1158 /* Yield scheduler intercept not implemented. */
1159 /* Gating storage scheduler intercept not implemented. */
1161 // TODO: Enable/disable TCs.
1163 env->CP0_VPEControl = newval;
1166 void helper_mttc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
1168 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1169 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1170 uint32_t mask;
1171 uint32_t newval;
1173 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
1174 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1175 newval = (other->CP0_VPEControl & ~mask) | (arg1 & mask);
1177 /* TODO: Enable/disable TCs. */
1179 other->CP0_VPEControl = newval;
1182 target_ulong helper_mftc0_vpecontrol(CPUMIPSState *env)
1184 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1185 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1186 /* FIXME: Mask away return zero on read bits. */
1187 return other->CP0_VPEControl;
1190 target_ulong helper_mftc0_vpeconf0(CPUMIPSState *env)
1192 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1193 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1195 return other->CP0_VPEConf0;
1198 void helper_mtc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1200 uint32_t mask = 0;
1201 uint32_t newval;
1203 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
1204 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
1205 mask |= (0xff << CP0VPEC0_XTC);
1206 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1208 newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1210 // TODO: TC exclusive handling due to ERL/EXL.
1212 env->CP0_VPEConf0 = newval;
1215 void helper_mttc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1217 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1218 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1219 uint32_t mask = 0;
1220 uint32_t newval;
1222 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1223 newval = (other->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1225 /* TODO: TC exclusive handling due to ERL/EXL. */
1226 other->CP0_VPEConf0 = newval;
1229 void helper_mtc0_vpeconf1(CPUMIPSState *env, target_ulong arg1)
1231 uint32_t mask = 0;
1232 uint32_t newval;
1234 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1235 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
1236 (0xff << CP0VPEC1_NCP1);
1237 newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
1239 /* UDI not implemented. */
1240 /* CP2 not implemented. */
1242 // TODO: Handle FPU (CP1) binding.
1244 env->CP0_VPEConf1 = newval;
1247 void helper_mtc0_yqmask(CPUMIPSState *env, target_ulong arg1)
1249 /* Yield qualifier inputs not implemented. */
1250 env->CP0_YQMask = 0x00000000;
1253 void helper_mtc0_vpeopt(CPUMIPSState *env, target_ulong arg1)
1255 env->CP0_VPEOpt = arg1 & 0x0000ffff;
1258 void helper_mtc0_entrylo0(CPUMIPSState *env, target_ulong arg1)
1260 /* Large physaddr (PABITS) not implemented */
1261 /* 1k pages not implemented */
1262 env->CP0_EntryLo0 = arg1 & 0x3FFFFFFF;
1265 void helper_mtc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1267 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
1268 uint32_t newval;
1270 newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
1272 env->active_tc.CP0_TCStatus = newval;
1273 sync_c0_tcstatus(env, env->current_tc, newval);
1276 void helper_mttc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1278 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1279 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1281 if (other_tc == other->current_tc)
1282 other->active_tc.CP0_TCStatus = arg1;
1283 else
1284 other->tcs[other_tc].CP0_TCStatus = arg1;
1285 sync_c0_tcstatus(other, other_tc, arg1);
1288 void helper_mtc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1290 uint32_t mask = (1 << CP0TCBd_TBE);
1291 uint32_t newval;
1293 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1294 mask |= (1 << CP0TCBd_CurVPE);
1295 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1296 env->active_tc.CP0_TCBind = newval;
1299 void helper_mttc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1301 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1302 uint32_t mask = (1 << CP0TCBd_TBE);
1303 uint32_t newval;
1304 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1306 if (other->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1307 mask |= (1 << CP0TCBd_CurVPE);
1308 if (other_tc == other->current_tc) {
1309 newval = (other->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1310 other->active_tc.CP0_TCBind = newval;
1311 } else {
1312 newval = (other->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
1313 other->tcs[other_tc].CP0_TCBind = newval;
1317 void helper_mtc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1319 env->active_tc.PC = arg1;
1320 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1321 env->lladdr = 0ULL;
1322 /* MIPS16 not implemented. */
1325 void helper_mttc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1327 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1328 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1330 if (other_tc == other->current_tc) {
1331 other->active_tc.PC = arg1;
1332 other->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1333 other->lladdr = 0ULL;
1334 /* MIPS16 not implemented. */
1335 } else {
1336 other->tcs[other_tc].PC = arg1;
1337 other->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1338 other->lladdr = 0ULL;
1339 /* MIPS16 not implemented. */
1343 void helper_mtc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1345 env->active_tc.CP0_TCHalt = arg1 & 0x1;
1347 // TODO: Halt TC / Restart (if allocated+active) TC.
1348 if (env->active_tc.CP0_TCHalt & 1) {
1349 mips_tc_sleep(env, env->current_tc);
1350 } else {
1351 mips_tc_wake(env, env->current_tc);
1355 void helper_mttc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1357 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1358 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1360 // TODO: Halt TC / Restart (if allocated+active) TC.
1362 if (other_tc == other->current_tc)
1363 other->active_tc.CP0_TCHalt = arg1;
1364 else
1365 other->tcs[other_tc].CP0_TCHalt = arg1;
1367 if (arg1 & 1) {
1368 mips_tc_sleep(other, other_tc);
1369 } else {
1370 mips_tc_wake(other, other_tc);
1374 void helper_mtc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1376 env->active_tc.CP0_TCContext = arg1;
1379 void helper_mttc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1381 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1382 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1384 if (other_tc == other->current_tc)
1385 other->active_tc.CP0_TCContext = arg1;
1386 else
1387 other->tcs[other_tc].CP0_TCContext = arg1;
1390 void helper_mtc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1392 env->active_tc.CP0_TCSchedule = arg1;
1395 void helper_mttc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1397 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1398 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1400 if (other_tc == other->current_tc)
1401 other->active_tc.CP0_TCSchedule = arg1;
1402 else
1403 other->tcs[other_tc].CP0_TCSchedule = arg1;
1406 void helper_mtc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1408 env->active_tc.CP0_TCScheFBack = arg1;
1411 void helper_mttc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1413 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1414 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1416 if (other_tc == other->current_tc)
1417 other->active_tc.CP0_TCScheFBack = arg1;
1418 else
1419 other->tcs[other_tc].CP0_TCScheFBack = arg1;
1422 void helper_mtc0_entrylo1(CPUMIPSState *env, target_ulong arg1)
1424 /* Large physaddr (PABITS) not implemented */
1425 /* 1k pages not implemented */
1426 env->CP0_EntryLo1 = arg1 & 0x3FFFFFFF;
1429 void helper_mtc0_context(CPUMIPSState *env, target_ulong arg1)
1431 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1434 void helper_mtc0_pagemask(CPUMIPSState *env, target_ulong arg1)
1436 /* 1k pages not implemented */
1437 env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1440 void helper_mtc0_pagegrain(CPUMIPSState *env, target_ulong arg1)
1442 /* SmartMIPS not implemented */
1443 /* Large physaddr (PABITS) not implemented */
1444 /* 1k pages not implemented */
1445 env->CP0_PageGrain = 0;
1448 void helper_mtc0_wired(CPUMIPSState *env, target_ulong arg1)
1450 env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1453 void helper_mtc0_srsconf0(CPUMIPSState *env, target_ulong arg1)
1455 env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1458 void helper_mtc0_srsconf1(CPUMIPSState *env, target_ulong arg1)
1460 env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1463 void helper_mtc0_srsconf2(CPUMIPSState *env, target_ulong arg1)
1465 env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1468 void helper_mtc0_srsconf3(CPUMIPSState *env, target_ulong arg1)
1470 env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1473 void helper_mtc0_srsconf4(CPUMIPSState *env, target_ulong arg1)
1475 env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1478 void helper_mtc0_hwrena(CPUMIPSState *env, target_ulong arg1)
1480 env->CP0_HWREna = arg1 & 0x0000000F;
1483 void helper_mtc0_count(CPUMIPSState *env, target_ulong arg1)
1485 cpu_mips_store_count(env, arg1);
1488 void helper_mtc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1490 target_ulong old, val;
1492 /* 1k pages not implemented */
1493 val = arg1 & ((TARGET_PAGE_MASK << 1) | 0xFF);
1494 #if defined(TARGET_MIPS64)
1495 val &= env->SEGMask;
1496 #endif
1497 old = env->CP0_EntryHi;
1498 env->CP0_EntryHi = val;
1499 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1500 sync_c0_entryhi(env, env->current_tc);
1502 /* If the ASID changes, flush qemu's TLB. */
1503 if ((old & 0xFF) != (val & 0xFF))
1504 cpu_mips_tlb_flush(env, 1);
1507 void helper_mttc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1509 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1510 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1512 other->CP0_EntryHi = arg1;
1513 sync_c0_entryhi(other, other_tc);
1516 void helper_mtc0_compare(CPUMIPSState *env, target_ulong arg1)
1518 cpu_mips_store_compare(env, arg1);
1521 void helper_mtc0_status(CPUMIPSState *env, target_ulong arg1)
1523 uint32_t val, old;
1524 uint32_t mask = env->CP0_Status_rw_bitmask;
1526 val = arg1 & mask;
1527 old = env->CP0_Status;
1528 env->CP0_Status = (env->CP0_Status & ~mask) | val;
1529 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1530 sync_c0_status(env, env, env->current_tc);
1531 } else {
1532 compute_hflags(env);
1535 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1536 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1537 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1538 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1539 env->CP0_Cause);
1540 switch (env->hflags & MIPS_HFLAG_KSU) {
1541 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1542 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1543 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1544 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
1549 void helper_mttc0_status(CPUMIPSState *env, target_ulong arg1)
1551 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1552 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1554 other->CP0_Status = arg1 & ~0xf1000018;
1555 sync_c0_status(env, other, other_tc);
1558 void helper_mtc0_intctl(CPUMIPSState *env, target_ulong arg1)
1560 /* vectored interrupts not implemented, no performance counters. */
1561 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000003e0) | (arg1 & 0x000003e0);
1564 void helper_mtc0_srsctl(CPUMIPSState *env, target_ulong arg1)
1566 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1567 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1570 static void mtc0_cause(CPUMIPSState *cpu, target_ulong arg1)
1572 uint32_t mask = 0x00C00300;
1573 uint32_t old = cpu->CP0_Cause;
1574 int i;
1576 if (cpu->insn_flags & ISA_MIPS32R2) {
1577 mask |= 1 << CP0Ca_DC;
1580 cpu->CP0_Cause = (cpu->CP0_Cause & ~mask) | (arg1 & mask);
1582 if ((old ^ cpu->CP0_Cause) & (1 << CP0Ca_DC)) {
1583 if (cpu->CP0_Cause & (1 << CP0Ca_DC)) {
1584 cpu_mips_stop_count(cpu);
1585 } else {
1586 cpu_mips_start_count(cpu);
1590 /* Set/reset software interrupts */
1591 for (i = 0 ; i < 2 ; i++) {
1592 if ((old ^ cpu->CP0_Cause) & (1 << (CP0Ca_IP + i))) {
1593 cpu_mips_soft_irq(cpu, i, cpu->CP0_Cause & (1 << (CP0Ca_IP + i)));
1598 void helper_mtc0_cause(CPUMIPSState *env, target_ulong arg1)
1600 mtc0_cause(env, arg1);
1603 void helper_mttc0_cause(CPUMIPSState *env, target_ulong arg1)
1605 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1606 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1608 mtc0_cause(other, arg1);
1611 target_ulong helper_mftc0_epc(CPUMIPSState *env)
1613 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1614 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1616 return other->CP0_EPC;
1619 target_ulong helper_mftc0_ebase(CPUMIPSState *env)
1621 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1622 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1624 return other->CP0_EBase;
1627 void helper_mtc0_ebase(CPUMIPSState *env, target_ulong arg1)
1629 /* vectored interrupts not implemented */
1630 env->CP0_EBase = (env->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1633 void helper_mttc0_ebase(CPUMIPSState *env, target_ulong arg1)
1635 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1636 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1637 other->CP0_EBase = (other->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1640 target_ulong helper_mftc0_configx(CPUMIPSState *env, target_ulong idx)
1642 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1643 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1645 switch (idx) {
1646 case 0: return other->CP0_Config0;
1647 case 1: return other->CP0_Config1;
1648 case 2: return other->CP0_Config2;
1649 case 3: return other->CP0_Config3;
1650 /* 4 and 5 are reserved. */
1651 case 6: return other->CP0_Config6;
1652 case 7: return other->CP0_Config7;
1653 default:
1654 break;
1656 return 0;
1659 void helper_mtc0_config0(CPUMIPSState *env, target_ulong arg1)
1661 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1664 void helper_mtc0_config2(CPUMIPSState *env, target_ulong arg1)
1666 /* tertiary/secondary caches not implemented */
1667 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1670 void helper_mtc0_lladdr(CPUMIPSState *env, target_ulong arg1)
1672 target_long mask = env->CP0_LLAddr_rw_bitmask;
1673 arg1 = arg1 << env->CP0_LLAddr_shift;
1674 env->lladdr = (env->lladdr & ~mask) | (arg1 & mask);
1677 void helper_mtc0_watchlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1679 /* Watch exceptions for instructions, data loads, data stores
1680 not implemented. */
1681 env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1684 void helper_mtc0_watchhi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1686 env->CP0_WatchHi[sel] = (arg1 & 0x40FF0FF8);
1687 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1690 void helper_mtc0_xcontext(CPUMIPSState *env, target_ulong arg1)
1692 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1693 env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1696 void helper_mtc0_framemask(CPUMIPSState *env, target_ulong arg1)
1698 env->CP0_Framemask = arg1; /* XXX */
1701 void helper_mtc0_debug(CPUMIPSState *env, target_ulong arg1)
1703 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
1704 if (arg1 & (1 << CP0DB_DM))
1705 env->hflags |= MIPS_HFLAG_DM;
1706 else
1707 env->hflags &= ~MIPS_HFLAG_DM;
1710 void helper_mttc0_debug(CPUMIPSState *env, target_ulong arg1)
1712 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1713 uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1714 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1716 /* XXX: Might be wrong, check with EJTAG spec. */
1717 if (other_tc == other->current_tc)
1718 other->active_tc.CP0_Debug_tcstatus = val;
1719 else
1720 other->tcs[other_tc].CP0_Debug_tcstatus = val;
1721 other->CP0_Debug = (other->CP0_Debug &
1722 ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1723 (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1726 void helper_mtc0_performance0(CPUMIPSState *env, target_ulong arg1)
1728 env->CP0_Performance0 = arg1 & 0x000007ff;
1731 void helper_mtc0_taglo(CPUMIPSState *env, target_ulong arg1)
1733 env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1736 void helper_mtc0_datalo(CPUMIPSState *env, target_ulong arg1)
1738 env->CP0_DataLo = arg1; /* XXX */
1741 void helper_mtc0_taghi(CPUMIPSState *env, target_ulong arg1)
1743 env->CP0_TagHi = arg1; /* XXX */
1746 void helper_mtc0_datahi(CPUMIPSState *env, target_ulong arg1)
1748 env->CP0_DataHi = arg1; /* XXX */
1751 /* MIPS MT functions */
1752 target_ulong helper_mftgpr(CPUMIPSState *env, uint32_t sel)
1754 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1755 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1757 if (other_tc == other->current_tc)
1758 return other->active_tc.gpr[sel];
1759 else
1760 return other->tcs[other_tc].gpr[sel];
1763 target_ulong helper_mftlo(CPUMIPSState *env, uint32_t sel)
1765 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1766 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1768 if (other_tc == other->current_tc)
1769 return other->active_tc.LO[sel];
1770 else
1771 return other->tcs[other_tc].LO[sel];
1774 target_ulong helper_mfthi(CPUMIPSState *env, uint32_t sel)
1776 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1777 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1779 if (other_tc == other->current_tc)
1780 return other->active_tc.HI[sel];
1781 else
1782 return other->tcs[other_tc].HI[sel];
1785 target_ulong helper_mftacx(CPUMIPSState *env, uint32_t sel)
1787 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1788 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1790 if (other_tc == other->current_tc)
1791 return other->active_tc.ACX[sel];
1792 else
1793 return other->tcs[other_tc].ACX[sel];
1796 target_ulong helper_mftdsp(CPUMIPSState *env)
1798 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1799 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1801 if (other_tc == other->current_tc)
1802 return other->active_tc.DSPControl;
1803 else
1804 return other->tcs[other_tc].DSPControl;
1807 void helper_mttgpr(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1809 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1810 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1812 if (other_tc == other->current_tc)
1813 other->active_tc.gpr[sel] = arg1;
1814 else
1815 other->tcs[other_tc].gpr[sel] = arg1;
1818 void helper_mttlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1820 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1821 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1823 if (other_tc == other->current_tc)
1824 other->active_tc.LO[sel] = arg1;
1825 else
1826 other->tcs[other_tc].LO[sel] = arg1;
1829 void helper_mtthi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1831 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1832 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1834 if (other_tc == other->current_tc)
1835 other->active_tc.HI[sel] = arg1;
1836 else
1837 other->tcs[other_tc].HI[sel] = arg1;
1840 void helper_mttacx(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1842 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1843 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1845 if (other_tc == other->current_tc)
1846 other->active_tc.ACX[sel] = arg1;
1847 else
1848 other->tcs[other_tc].ACX[sel] = arg1;
1851 void helper_mttdsp(CPUMIPSState *env, target_ulong arg1)
1853 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1854 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1856 if (other_tc == other->current_tc)
1857 other->active_tc.DSPControl = arg1;
1858 else
1859 other->tcs[other_tc].DSPControl = arg1;
1862 /* MIPS MT functions */
1863 target_ulong helper_dmt(void)
1865 // TODO
1866 return 0;
1869 target_ulong helper_emt(void)
1871 // TODO
1872 return 0;
1875 target_ulong helper_dvpe(CPUMIPSState *env)
1877 CPUMIPSState *other_cpu = first_cpu;
1878 target_ulong prev = env->mvp->CP0_MVPControl;
1880 do {
1881 /* Turn off all VPEs except the one executing the dvpe. */
1882 if (other_cpu != env) {
1883 other_cpu->mvp->CP0_MVPControl &= ~(1 << CP0MVPCo_EVP);
1884 mips_vpe_sleep(other_cpu);
1886 other_cpu = other_cpu->next_cpu;
1887 } while (other_cpu);
1888 return prev;
1891 target_ulong helper_evpe(CPUMIPSState *env)
1893 CPUMIPSState *other_cpu = first_cpu;
1894 target_ulong prev = env->mvp->CP0_MVPControl;
1896 do {
1897 if (other_cpu != env
1898 /* If the VPE is WFI, don't disturb its sleep. */
1899 && !mips_vpe_is_wfi(other_cpu)) {
1900 /* Enable the VPE. */
1901 other_cpu->mvp->CP0_MVPControl |= (1 << CP0MVPCo_EVP);
1902 mips_vpe_wake(other_cpu); /* And wake it up. */
1904 other_cpu = other_cpu->next_cpu;
1905 } while (other_cpu);
1906 return prev;
1908 #endif /* !CONFIG_USER_ONLY */
1910 void helper_fork(target_ulong arg1, target_ulong arg2)
1912 // arg1 = rt, arg2 = rs
1913 arg1 = 0;
1914 // TODO: store to TC register
1917 target_ulong helper_yield(CPUMIPSState *env, target_ulong arg)
1919 target_long arg1 = arg;
1921 if (arg1 < 0) {
1922 /* No scheduling policy implemented. */
1923 if (arg1 != -2) {
1924 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1925 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
1926 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1927 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1928 helper_raise_exception(env, EXCP_THREAD);
1931 } else if (arg1 == 0) {
1932 if (0 /* TODO: TC underflow */) {
1933 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1934 helper_raise_exception(env, EXCP_THREAD);
1935 } else {
1936 // TODO: Deallocate TC
1938 } else if (arg1 > 0) {
1939 /* Yield qualifier inputs not implemented. */
1940 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1941 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1942 helper_raise_exception(env, EXCP_THREAD);
1944 return env->CP0_YQMask;
1947 #ifndef CONFIG_USER_ONLY
1948 /* TLB management */
1949 static void cpu_mips_tlb_flush (CPUMIPSState *env, int flush_global)
1951 /* Flush qemu's TLB and discard all shadowed entries. */
1952 tlb_flush (env, flush_global);
1953 env->tlb->tlb_in_use = env->tlb->nb_tlb;
1956 static void r4k_mips_tlb_flush_extra (CPUMIPSState *env, int first)
1958 /* Discard entries from env->tlb[first] onwards. */
1959 while (env->tlb->tlb_in_use > first) {
1960 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1964 static void r4k_fill_tlb(CPUMIPSState *env, int idx)
1966 r4k_tlb_t *tlb;
1968 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1969 tlb = &env->tlb->mmu.r4k.tlb[idx];
1970 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1971 #if defined(TARGET_MIPS64)
1972 tlb->VPN &= env->SEGMask;
1973 #endif
1974 tlb->ASID = env->CP0_EntryHi & 0xFF;
1975 tlb->PageMask = env->CP0_PageMask;
1976 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1977 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
1978 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
1979 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
1980 tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
1981 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
1982 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
1983 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
1984 tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
1987 void r4k_helper_tlbwi(CPUMIPSState *env)
1989 int idx;
1991 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1993 /* Discard cached TLB entries. We could avoid doing this if the
1994 tlbwi is just upgrading access permissions on the current entry;
1995 that might be a further win. */
1996 r4k_mips_tlb_flush_extra (env, env->tlb->nb_tlb);
1998 r4k_invalidate_tlb(env, idx, 0);
1999 r4k_fill_tlb(env, idx);
2002 void r4k_helper_tlbwr(CPUMIPSState *env)
2004 int r = cpu_mips_get_random(env);
2006 r4k_invalidate_tlb(env, r, 1);
2007 r4k_fill_tlb(env, r);
2010 void r4k_helper_tlbp(CPUMIPSState *env)
2012 r4k_tlb_t *tlb;
2013 target_ulong mask;
2014 target_ulong tag;
2015 target_ulong VPN;
2016 uint8_t ASID;
2017 int i;
2019 ASID = env->CP0_EntryHi & 0xFF;
2020 for (i = 0; i < env->tlb->nb_tlb; i++) {
2021 tlb = &env->tlb->mmu.r4k.tlb[i];
2022 /* 1k pages are not supported. */
2023 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
2024 tag = env->CP0_EntryHi & ~mask;
2025 VPN = tlb->VPN & ~mask;
2026 /* Check ASID, virtual page number & size */
2027 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
2028 /* TLB match */
2029 env->CP0_Index = i;
2030 break;
2033 if (i == env->tlb->nb_tlb) {
2034 /* No match. Discard any shadow entries, if any of them match. */
2035 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
2036 tlb = &env->tlb->mmu.r4k.tlb[i];
2037 /* 1k pages are not supported. */
2038 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
2039 tag = env->CP0_EntryHi & ~mask;
2040 VPN = tlb->VPN & ~mask;
2041 /* Check ASID, virtual page number & size */
2042 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
2043 r4k_mips_tlb_flush_extra (env, i);
2044 break;
2048 env->CP0_Index |= 0x80000000;
2052 void r4k_helper_tlbr(CPUMIPSState *env)
2054 r4k_tlb_t *tlb;
2055 uint8_t ASID;
2056 int idx;
2058 ASID = env->CP0_EntryHi & 0xFF;
2059 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
2060 tlb = &env->tlb->mmu.r4k.tlb[idx];
2062 /* If this will change the current ASID, flush qemu's TLB. */
2063 if (ASID != tlb->ASID)
2064 cpu_mips_tlb_flush (env, 1);
2066 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
2068 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
2069 env->CP0_PageMask = tlb->PageMask;
2070 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
2071 (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
2072 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
2073 (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
2076 void helper_tlbwi(CPUMIPSState *env)
2078 env->tlb->helper_tlbwi(env);
2081 void helper_tlbwr(CPUMIPSState *env)
2083 env->tlb->helper_tlbwr(env);
2086 void helper_tlbp(CPUMIPSState *env)
2088 env->tlb->helper_tlbp(env);
2091 void helper_tlbr(CPUMIPSState *env)
2093 env->tlb->helper_tlbr(env);
2096 /* Specials */
2097 target_ulong helper_di(CPUMIPSState *env)
2099 target_ulong t0 = env->CP0_Status;
2101 env->CP0_Status = t0 & ~(1 << CP0St_IE);
2102 return t0;
2105 target_ulong helper_ei(CPUMIPSState *env)
2107 target_ulong t0 = env->CP0_Status;
2109 env->CP0_Status = t0 | (1 << CP0St_IE);
2110 return t0;
2113 static void debug_pre_eret(CPUMIPSState *env)
2115 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2116 qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2117 env->active_tc.PC, env->CP0_EPC);
2118 if (env->CP0_Status & (1 << CP0St_ERL))
2119 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2120 if (env->hflags & MIPS_HFLAG_DM)
2121 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2122 qemu_log("\n");
2126 static void debug_post_eret(CPUMIPSState *env)
2128 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2129 qemu_log(" => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2130 env->active_tc.PC, env->CP0_EPC);
2131 if (env->CP0_Status & (1 << CP0St_ERL))
2132 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2133 if (env->hflags & MIPS_HFLAG_DM)
2134 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2135 switch (env->hflags & MIPS_HFLAG_KSU) {
2136 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
2137 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
2138 case MIPS_HFLAG_KM: qemu_log("\n"); break;
2139 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
2144 static void set_pc(CPUMIPSState *env, target_ulong error_pc)
2146 env->active_tc.PC = error_pc & ~(target_ulong)1;
2147 if (error_pc & 1) {
2148 env->hflags |= MIPS_HFLAG_M16;
2149 } else {
2150 env->hflags &= ~(MIPS_HFLAG_M16);
2154 void helper_eret(CPUMIPSState *env)
2156 debug_pre_eret(env);
2157 if (env->CP0_Status & (1 << CP0St_ERL)) {
2158 set_pc(env, env->CP0_ErrorEPC);
2159 env->CP0_Status &= ~(1 << CP0St_ERL);
2160 } else {
2161 set_pc(env, env->CP0_EPC);
2162 env->CP0_Status &= ~(1 << CP0St_EXL);
2164 compute_hflags(env);
2165 debug_post_eret(env);
2166 env->lladdr = 1;
2169 void helper_deret(CPUMIPSState *env)
2171 debug_pre_eret(env);
2172 set_pc(env, env->CP0_DEPC);
2174 env->hflags &= MIPS_HFLAG_DM;
2175 compute_hflags(env);
2176 debug_post_eret(env);
2177 env->lladdr = 1;
2179 #endif /* !CONFIG_USER_ONLY */
2181 target_ulong helper_rdhwr_cpunum(CPUMIPSState *env)
2183 if ((env->hflags & MIPS_HFLAG_CP0) ||
2184 (env->CP0_HWREna & (1 << 0)))
2185 return env->CP0_EBase & 0x3ff;
2186 else
2187 helper_raise_exception(env, EXCP_RI);
2189 return 0;
2192 target_ulong helper_rdhwr_synci_step(CPUMIPSState *env)
2194 if ((env->hflags & MIPS_HFLAG_CP0) ||
2195 (env->CP0_HWREna & (1 << 1)))
2196 return env->SYNCI_Step;
2197 else
2198 helper_raise_exception(env, EXCP_RI);
2200 return 0;
2203 target_ulong helper_rdhwr_cc(CPUMIPSState *env)
2205 if ((env->hflags & MIPS_HFLAG_CP0) ||
2206 (env->CP0_HWREna & (1 << 2)))
2207 return env->CP0_Count;
2208 else
2209 helper_raise_exception(env, EXCP_RI);
2211 return 0;
2214 target_ulong helper_rdhwr_ccres(CPUMIPSState *env)
2216 if ((env->hflags & MIPS_HFLAG_CP0) ||
2217 (env->CP0_HWREna & (1 << 3)))
2218 return env->CCRes;
2219 else
2220 helper_raise_exception(env, EXCP_RI);
2222 return 0;
2225 void helper_pmon(CPUMIPSState *env, int function)
2227 function /= 2;
2228 switch (function) {
2229 case 2: /* TODO: char inbyte(int waitflag); */
2230 if (env->active_tc.gpr[4] == 0)
2231 env->active_tc.gpr[2] = -1;
2232 /* Fall through */
2233 case 11: /* TODO: char inbyte (void); */
2234 env->active_tc.gpr[2] = -1;
2235 break;
2236 case 3:
2237 case 12:
2238 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
2239 break;
2240 case 17:
2241 break;
2242 case 158:
2244 unsigned char *fmt = (void *)(uintptr_t)env->active_tc.gpr[4];
2245 printf("%s", fmt);
2247 break;
2251 void helper_wait(CPUMIPSState *env)
2253 env->halted = 1;
2254 cpu_reset_interrupt(env, CPU_INTERRUPT_WAKE);
2255 helper_raise_exception(env, EXCP_HLT);
2258 #if !defined(CONFIG_USER_ONLY)
2260 static void QEMU_NORETURN do_unaligned_access(CPUMIPSState *env,
2261 target_ulong addr, int is_write,
2262 int is_user, uintptr_t retaddr);
2264 #define MMUSUFFIX _mmu
2265 #define ALIGNED_ONLY
2267 #define SHIFT 0
2268 #include "softmmu_template.h"
2270 #define SHIFT 1
2271 #include "softmmu_template.h"
2273 #define SHIFT 2
2274 #include "softmmu_template.h"
2276 #define SHIFT 3
2277 #include "softmmu_template.h"
2279 static void do_unaligned_access(CPUMIPSState *env, target_ulong addr,
2280 int is_write, int is_user, uintptr_t retaddr)
2282 env->CP0_BadVAddr = addr;
2283 do_restore_state(env, retaddr);
2284 helper_raise_exception(env, (is_write == 1) ? EXCP_AdES : EXCP_AdEL);
2287 void tlb_fill(CPUMIPSState *env, target_ulong addr, int is_write, int mmu_idx,
2288 uintptr_t retaddr)
2290 TranslationBlock *tb;
2291 int ret;
2293 ret = cpu_mips_handle_mmu_fault(env, addr, is_write, mmu_idx);
2294 if (ret) {
2295 if (retaddr) {
2296 /* now we have a real cpu fault */
2297 tb = tb_find_pc(retaddr);
2298 if (tb) {
2299 /* the PC is inside the translated code. It means that we have
2300 a virtual CPU fault */
2301 cpu_restore_state(tb, env, retaddr);
2304 helper_raise_exception_err(env, env->exception_index, env->error_code);
2308 void cpu_unassigned_access(CPUMIPSState *env, target_phys_addr_t addr,
2309 int is_write, int is_exec, int unused, int size)
2311 if (is_exec)
2312 helper_raise_exception(env, EXCP_IBE);
2313 else
2314 helper_raise_exception(env, EXCP_DBE);
2316 #endif /* !CONFIG_USER_ONLY */
2318 /* Complex FPU operations which may need stack space. */
2320 #define FLOAT_ONE32 make_float32(0x3f8 << 20)
2321 #define FLOAT_ONE64 make_float64(0x3ffULL << 52)
2322 #define FLOAT_TWO32 make_float32(1 << 30)
2323 #define FLOAT_TWO64 make_float64(1ULL << 62)
2324 #define FLOAT_QNAN32 0x7fbfffff
2325 #define FLOAT_QNAN64 0x7ff7ffffffffffffULL
2326 #define FLOAT_SNAN32 0x7fffffff
2327 #define FLOAT_SNAN64 0x7fffffffffffffffULL
2329 /* convert MIPS rounding mode in FCR31 to IEEE library */
2330 static unsigned int ieee_rm[] = {
2331 float_round_nearest_even,
2332 float_round_to_zero,
2333 float_round_up,
2334 float_round_down
2337 #define RESTORE_ROUNDING_MODE \
2338 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
2340 #define RESTORE_FLUSH_MODE \
2341 set_flush_to_zero((env->active_fpu.fcr31 & (1 << 24)) != 0, &env->active_fpu.fp_status);
2343 target_ulong helper_cfc1(CPUMIPSState *env, uint32_t reg)
2345 target_ulong arg1;
2347 switch (reg) {
2348 case 0:
2349 arg1 = (int32_t)env->active_fpu.fcr0;
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 reg)
2370 switch(reg) {
2371 case 25:
2372 if (arg1 & 0xffffff00)
2373 return;
2374 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
2375 ((arg1 & 0x1) << 23);
2376 break;
2377 case 26:
2378 if (arg1 & 0x007c0000)
2379 return;
2380 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
2381 break;
2382 case 28:
2383 if (arg1 & 0x007c0000)
2384 return;
2385 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
2386 ((arg1 & 0x4) << 22);
2387 break;
2388 case 31:
2389 if (arg1 & 0x007c0000)
2390 return;
2391 env->active_fpu.fcr31 = arg1;
2392 break;
2393 default:
2394 return;
2396 /* set rounding mode */
2397 RESTORE_ROUNDING_MODE;
2398 /* set flush-to-zero mode */
2399 RESTORE_FLUSH_MODE;
2400 set_float_exception_flags(0, &env->active_fpu.fp_status);
2401 if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
2402 helper_raise_exception(env, EXCP_FPE);
2405 static inline int ieee_ex_to_mips(int xcpt)
2407 int ret = 0;
2408 if (xcpt) {
2409 if (xcpt & float_flag_invalid) {
2410 ret |= FP_INVALID;
2412 if (xcpt & float_flag_overflow) {
2413 ret |= FP_OVERFLOW;
2415 if (xcpt & float_flag_underflow) {
2416 ret |= FP_UNDERFLOW;
2418 if (xcpt & float_flag_divbyzero) {
2419 ret |= FP_DIV0;
2421 if (xcpt & float_flag_inexact) {
2422 ret |= FP_INEXACT;
2425 return ret;
2428 static inline void update_fcr31(CPUMIPSState *env)
2430 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
2432 SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
2433 if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp)
2434 helper_raise_exception(env, EXCP_FPE);
2435 else
2436 UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
2439 /* Float support.
2440 Single precition routines have a "s" suffix, double precision a
2441 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
2442 paired single lower "pl", paired single upper "pu". */
2444 /* unary operations, modifying fp status */
2445 uint64_t helper_float_sqrt_d(CPUMIPSState *env, uint64_t fdt0)
2447 return float64_sqrt(fdt0, &env->active_fpu.fp_status);
2450 uint32_t helper_float_sqrt_s(CPUMIPSState *env, uint32_t fst0)
2452 return float32_sqrt(fst0, &env->active_fpu.fp_status);
2455 uint64_t helper_float_cvtd_s(CPUMIPSState *env, uint32_t fst0)
2457 uint64_t fdt2;
2459 set_float_exception_flags(0, &env->active_fpu.fp_status);
2460 fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
2461 update_fcr31(env);
2462 return fdt2;
2465 uint64_t helper_float_cvtd_w(CPUMIPSState *env, uint32_t wt0)
2467 uint64_t fdt2;
2469 set_float_exception_flags(0, &env->active_fpu.fp_status);
2470 fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2471 update_fcr31(env);
2472 return fdt2;
2475 uint64_t helper_float_cvtd_l(CPUMIPSState *env, uint64_t dt0)
2477 uint64_t fdt2;
2479 set_float_exception_flags(0, &env->active_fpu.fp_status);
2480 fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2481 update_fcr31(env);
2482 return fdt2;
2485 uint64_t helper_float_cvtl_d(CPUMIPSState *env, uint64_t fdt0)
2487 uint64_t dt2;
2489 set_float_exception_flags(0, &env->active_fpu.fp_status);
2490 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2491 update_fcr31(env);
2492 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2493 dt2 = FLOAT_SNAN64;
2494 return dt2;
2497 uint64_t helper_float_cvtl_s(CPUMIPSState *env, uint32_t fst0)
2499 uint64_t dt2;
2501 set_float_exception_flags(0, &env->active_fpu.fp_status);
2502 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2503 update_fcr31(env);
2504 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2505 dt2 = FLOAT_SNAN64;
2506 return dt2;
2509 uint64_t helper_float_cvtps_pw(CPUMIPSState *env, uint64_t dt0)
2511 uint32_t fst2;
2512 uint32_t fsth2;
2514 set_float_exception_flags(0, &env->active_fpu.fp_status);
2515 fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2516 fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
2517 update_fcr31(env);
2518 return ((uint64_t)fsth2 << 32) | fst2;
2521 uint64_t helper_float_cvtpw_ps(CPUMIPSState *env, uint64_t fdt0)
2523 uint32_t wt2;
2524 uint32_t wth2;
2526 set_float_exception_flags(0, &env->active_fpu.fp_status);
2527 wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2528 wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
2529 update_fcr31(env);
2530 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID)) {
2531 wt2 = FLOAT_SNAN32;
2532 wth2 = FLOAT_SNAN32;
2534 return ((uint64_t)wth2 << 32) | wt2;
2537 uint32_t helper_float_cvts_d(CPUMIPSState *env, uint64_t fdt0)
2539 uint32_t fst2;
2541 set_float_exception_flags(0, &env->active_fpu.fp_status);
2542 fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
2543 update_fcr31(env);
2544 return fst2;
2547 uint32_t helper_float_cvts_w(CPUMIPSState *env, uint32_t wt0)
2549 uint32_t fst2;
2551 set_float_exception_flags(0, &env->active_fpu.fp_status);
2552 fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
2553 update_fcr31(env);
2554 return fst2;
2557 uint32_t helper_float_cvts_l(CPUMIPSState *env, uint64_t dt0)
2559 uint32_t fst2;
2561 set_float_exception_flags(0, &env->active_fpu.fp_status);
2562 fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
2563 update_fcr31(env);
2564 return fst2;
2567 uint32_t helper_float_cvts_pl(CPUMIPSState *env, uint32_t wt0)
2569 uint32_t wt2;
2571 set_float_exception_flags(0, &env->active_fpu.fp_status);
2572 wt2 = wt0;
2573 update_fcr31(env);
2574 return wt2;
2577 uint32_t helper_float_cvts_pu(CPUMIPSState *env, uint32_t wth0)
2579 uint32_t wt2;
2581 set_float_exception_flags(0, &env->active_fpu.fp_status);
2582 wt2 = wth0;
2583 update_fcr31(env);
2584 return wt2;
2587 uint32_t helper_float_cvtw_s(CPUMIPSState *env, uint32_t fst0)
2589 uint32_t wt2;
2591 set_float_exception_flags(0, &env->active_fpu.fp_status);
2592 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2593 update_fcr31(env);
2594 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2595 wt2 = FLOAT_SNAN32;
2596 return wt2;
2599 uint32_t helper_float_cvtw_d(CPUMIPSState *env, uint64_t fdt0)
2601 uint32_t wt2;
2603 set_float_exception_flags(0, &env->active_fpu.fp_status);
2604 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2605 update_fcr31(env);
2606 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2607 wt2 = FLOAT_SNAN32;
2608 return wt2;
2611 uint64_t helper_float_roundl_d(CPUMIPSState *env, uint64_t fdt0)
2613 uint64_t dt2;
2615 set_float_exception_flags(0, &env->active_fpu.fp_status);
2616 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2617 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2618 RESTORE_ROUNDING_MODE;
2619 update_fcr31(env);
2620 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2621 dt2 = FLOAT_SNAN64;
2622 return dt2;
2625 uint64_t helper_float_roundl_s(CPUMIPSState *env, uint32_t fst0)
2627 uint64_t dt2;
2629 set_float_exception_flags(0, &env->active_fpu.fp_status);
2630 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2631 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2632 RESTORE_ROUNDING_MODE;
2633 update_fcr31(env);
2634 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2635 dt2 = FLOAT_SNAN64;
2636 return dt2;
2639 uint32_t helper_float_roundw_d(CPUMIPSState *env, uint64_t fdt0)
2641 uint32_t wt2;
2643 set_float_exception_flags(0, &env->active_fpu.fp_status);
2644 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2645 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2646 RESTORE_ROUNDING_MODE;
2647 update_fcr31(env);
2648 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2649 wt2 = FLOAT_SNAN32;
2650 return wt2;
2653 uint32_t helper_float_roundw_s(CPUMIPSState *env, uint32_t fst0)
2655 uint32_t wt2;
2657 set_float_exception_flags(0, &env->active_fpu.fp_status);
2658 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2659 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2660 RESTORE_ROUNDING_MODE;
2661 update_fcr31(env);
2662 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2663 wt2 = FLOAT_SNAN32;
2664 return wt2;
2667 uint64_t helper_float_truncl_d(CPUMIPSState *env, uint64_t fdt0)
2669 uint64_t dt2;
2671 set_float_exception_flags(0, &env->active_fpu.fp_status);
2672 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
2673 update_fcr31(env);
2674 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2675 dt2 = FLOAT_SNAN64;
2676 return dt2;
2679 uint64_t helper_float_truncl_s(CPUMIPSState *env, uint32_t fst0)
2681 uint64_t dt2;
2683 set_float_exception_flags(0, &env->active_fpu.fp_status);
2684 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
2685 update_fcr31(env);
2686 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2687 dt2 = FLOAT_SNAN64;
2688 return dt2;
2691 uint32_t helper_float_truncw_d(CPUMIPSState *env, uint64_t fdt0)
2693 uint32_t wt2;
2695 set_float_exception_flags(0, &env->active_fpu.fp_status);
2696 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
2697 update_fcr31(env);
2698 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2699 wt2 = FLOAT_SNAN32;
2700 return wt2;
2703 uint32_t helper_float_truncw_s(CPUMIPSState *env, uint32_t fst0)
2705 uint32_t wt2;
2707 set_float_exception_flags(0, &env->active_fpu.fp_status);
2708 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
2709 update_fcr31(env);
2710 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2711 wt2 = FLOAT_SNAN32;
2712 return wt2;
2715 uint64_t helper_float_ceill_d(CPUMIPSState *env, uint64_t fdt0)
2717 uint64_t dt2;
2719 set_float_exception_flags(0, &env->active_fpu.fp_status);
2720 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2721 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2722 RESTORE_ROUNDING_MODE;
2723 update_fcr31(env);
2724 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2725 dt2 = FLOAT_SNAN64;
2726 return dt2;
2729 uint64_t helper_float_ceill_s(CPUMIPSState *env, uint32_t fst0)
2731 uint64_t dt2;
2733 set_float_exception_flags(0, &env->active_fpu.fp_status);
2734 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2735 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2736 RESTORE_ROUNDING_MODE;
2737 update_fcr31(env);
2738 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2739 dt2 = FLOAT_SNAN64;
2740 return dt2;
2743 uint32_t helper_float_ceilw_d(CPUMIPSState *env, uint64_t fdt0)
2745 uint32_t wt2;
2747 set_float_exception_flags(0, &env->active_fpu.fp_status);
2748 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2749 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2750 RESTORE_ROUNDING_MODE;
2751 update_fcr31(env);
2752 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2753 wt2 = FLOAT_SNAN32;
2754 return wt2;
2757 uint32_t helper_float_ceilw_s(CPUMIPSState *env, uint32_t fst0)
2759 uint32_t wt2;
2761 set_float_exception_flags(0, &env->active_fpu.fp_status);
2762 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2763 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2764 RESTORE_ROUNDING_MODE;
2765 update_fcr31(env);
2766 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2767 wt2 = FLOAT_SNAN32;
2768 return wt2;
2771 uint64_t helper_float_floorl_d(CPUMIPSState *env, uint64_t fdt0)
2773 uint64_t dt2;
2775 set_float_exception_flags(0, &env->active_fpu.fp_status);
2776 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2777 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2778 RESTORE_ROUNDING_MODE;
2779 update_fcr31(env);
2780 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2781 dt2 = FLOAT_SNAN64;
2782 return dt2;
2785 uint64_t helper_float_floorl_s(CPUMIPSState *env, uint32_t fst0)
2787 uint64_t dt2;
2789 set_float_exception_flags(0, &env->active_fpu.fp_status);
2790 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2791 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2792 RESTORE_ROUNDING_MODE;
2793 update_fcr31(env);
2794 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2795 dt2 = FLOAT_SNAN64;
2796 return dt2;
2799 uint32_t helper_float_floorw_d(CPUMIPSState *env, uint64_t fdt0)
2801 uint32_t wt2;
2803 set_float_exception_flags(0, &env->active_fpu.fp_status);
2804 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2805 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2806 RESTORE_ROUNDING_MODE;
2807 update_fcr31(env);
2808 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2809 wt2 = FLOAT_SNAN32;
2810 return wt2;
2813 uint32_t helper_float_floorw_s(CPUMIPSState *env, uint32_t fst0)
2815 uint32_t wt2;
2817 set_float_exception_flags(0, &env->active_fpu.fp_status);
2818 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2819 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2820 RESTORE_ROUNDING_MODE;
2821 update_fcr31(env);
2822 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2823 wt2 = FLOAT_SNAN32;
2824 return wt2;
2827 /* unary operations, not modifying fp status */
2828 #define FLOAT_UNOP(name) \
2829 uint64_t helper_float_ ## name ## _d(uint64_t fdt0) \
2831 return float64_ ## name(fdt0); \
2833 uint32_t helper_float_ ## name ## _s(uint32_t fst0) \
2835 return float32_ ## name(fst0); \
2837 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0) \
2839 uint32_t wt0; \
2840 uint32_t wth0; \
2842 wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF); \
2843 wth0 = float32_ ## name(fdt0 >> 32); \
2844 return ((uint64_t)wth0 << 32) | wt0; \
2846 FLOAT_UNOP(abs)
2847 FLOAT_UNOP(chs)
2848 #undef FLOAT_UNOP
2850 /* MIPS specific unary operations */
2851 uint64_t helper_float_recip_d(CPUMIPSState *env, uint64_t fdt0)
2853 uint64_t fdt2;
2855 set_float_exception_flags(0, &env->active_fpu.fp_status);
2856 fdt2 = float64_div(FLOAT_ONE64, fdt0, &env->active_fpu.fp_status);
2857 update_fcr31(env);
2858 return fdt2;
2861 uint32_t helper_float_recip_s(CPUMIPSState *env, uint32_t fst0)
2863 uint32_t fst2;
2865 set_float_exception_flags(0, &env->active_fpu.fp_status);
2866 fst2 = float32_div(FLOAT_ONE32, fst0, &env->active_fpu.fp_status);
2867 update_fcr31(env);
2868 return fst2;
2871 uint64_t helper_float_rsqrt_d(CPUMIPSState *env, uint64_t fdt0)
2873 uint64_t fdt2;
2875 set_float_exception_flags(0, &env->active_fpu.fp_status);
2876 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2877 fdt2 = float64_div(FLOAT_ONE64, fdt2, &env->active_fpu.fp_status);
2878 update_fcr31(env);
2879 return fdt2;
2882 uint32_t helper_float_rsqrt_s(CPUMIPSState *env, uint32_t fst0)
2884 uint32_t fst2;
2886 set_float_exception_flags(0, &env->active_fpu.fp_status);
2887 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2888 fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
2889 update_fcr31(env);
2890 return fst2;
2893 uint64_t helper_float_recip1_d(CPUMIPSState *env, uint64_t fdt0)
2895 uint64_t fdt2;
2897 set_float_exception_flags(0, &env->active_fpu.fp_status);
2898 fdt2 = float64_div(FLOAT_ONE64, fdt0, &env->active_fpu.fp_status);
2899 update_fcr31(env);
2900 return fdt2;
2903 uint32_t helper_float_recip1_s(CPUMIPSState *env, uint32_t fst0)
2905 uint32_t fst2;
2907 set_float_exception_flags(0, &env->active_fpu.fp_status);
2908 fst2 = float32_div(FLOAT_ONE32, fst0, &env->active_fpu.fp_status);
2909 update_fcr31(env);
2910 return fst2;
2913 uint64_t helper_float_recip1_ps(CPUMIPSState *env, uint64_t fdt0)
2915 uint32_t fst2;
2916 uint32_t fsth2;
2918 set_float_exception_flags(0, &env->active_fpu.fp_status);
2919 fst2 = float32_div(FLOAT_ONE32, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2920 fsth2 = float32_div(FLOAT_ONE32, fdt0 >> 32, &env->active_fpu.fp_status);
2921 update_fcr31(env);
2922 return ((uint64_t)fsth2 << 32) | fst2;
2925 uint64_t helper_float_rsqrt1_d(CPUMIPSState *env, uint64_t fdt0)
2927 uint64_t fdt2;
2929 set_float_exception_flags(0, &env->active_fpu.fp_status);
2930 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2931 fdt2 = float64_div(FLOAT_ONE64, fdt2, &env->active_fpu.fp_status);
2932 update_fcr31(env);
2933 return fdt2;
2936 uint32_t helper_float_rsqrt1_s(CPUMIPSState *env, uint32_t fst0)
2938 uint32_t fst2;
2940 set_float_exception_flags(0, &env->active_fpu.fp_status);
2941 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2942 fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
2943 update_fcr31(env);
2944 return fst2;
2947 uint64_t helper_float_rsqrt1_ps(CPUMIPSState *env, uint64_t fdt0)
2949 uint32_t fst2;
2950 uint32_t fsth2;
2952 set_float_exception_flags(0, &env->active_fpu.fp_status);
2953 fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2954 fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
2955 fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
2956 fsth2 = float32_div(FLOAT_ONE32, fsth2, &env->active_fpu.fp_status);
2957 update_fcr31(env);
2958 return ((uint64_t)fsth2 << 32) | fst2;
2961 #define FLOAT_OP(name, p) void helper_float_##name##_##p(CPUMIPSState *env)
2963 /* binary operations */
2964 #define FLOAT_BINOP(name) \
2965 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
2966 uint64_t fdt0, uint64_t fdt1) \
2968 uint64_t dt2; \
2970 set_float_exception_flags(0, &env->active_fpu.fp_status); \
2971 dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status); \
2972 update_fcr31(env); \
2973 if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) \
2974 dt2 = FLOAT_QNAN64; \
2975 return dt2; \
2978 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
2979 uint32_t fst0, uint32_t fst1) \
2981 uint32_t wt2; \
2983 set_float_exception_flags(0, &env->active_fpu.fp_status); \
2984 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
2985 update_fcr31(env); \
2986 if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) \
2987 wt2 = FLOAT_QNAN32; \
2988 return wt2; \
2991 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
2992 uint64_t fdt0, \
2993 uint64_t fdt1) \
2995 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
2996 uint32_t fsth0 = fdt0 >> 32; \
2997 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
2998 uint32_t fsth1 = fdt1 >> 32; \
2999 uint32_t wt2; \
3000 uint32_t wth2; \
3002 set_float_exception_flags(0, &env->active_fpu.fp_status); \
3003 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3004 wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status); \
3005 update_fcr31(env); \
3006 if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) { \
3007 wt2 = FLOAT_QNAN32; \
3008 wth2 = FLOAT_QNAN32; \
3010 return ((uint64_t)wth2 << 32) | wt2; \
3013 FLOAT_BINOP(add)
3014 FLOAT_BINOP(sub)
3015 FLOAT_BINOP(mul)
3016 FLOAT_BINOP(div)
3017 #undef FLOAT_BINOP
3019 /* ternary operations */
3020 #define FLOAT_TERNOP(name1, name2) \
3021 uint64_t helper_float_ ## name1 ## name2 ## _d(CPUMIPSState *env, \
3022 uint64_t fdt0, \
3023 uint64_t fdt1, \
3024 uint64_t fdt2) \
3026 fdt0 = float64_ ## name1 (fdt0, fdt1, &env->active_fpu.fp_status); \
3027 return float64_ ## name2 (fdt0, fdt2, &env->active_fpu.fp_status); \
3030 uint32_t helper_float_ ## name1 ## name2 ## _s(CPUMIPSState *env, \
3031 uint32_t fst0, \
3032 uint32_t fst1, \
3033 uint32_t fst2) \
3035 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
3036 return float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
3039 uint64_t helper_float_ ## name1 ## name2 ## _ps(CPUMIPSState *env, \
3040 uint64_t fdt0, \
3041 uint64_t fdt1, \
3042 uint64_t fdt2) \
3044 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3045 uint32_t fsth0 = fdt0 >> 32; \
3046 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3047 uint32_t fsth1 = fdt1 >> 32; \
3048 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
3049 uint32_t fsth2 = fdt2 >> 32; \
3051 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
3052 fsth0 = float32_ ## name1 (fsth0, fsth1, &env->active_fpu.fp_status); \
3053 fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
3054 fsth2 = float32_ ## name2 (fsth0, fsth2, &env->active_fpu.fp_status); \
3055 return ((uint64_t)fsth2 << 32) | fst2; \
3058 FLOAT_TERNOP(mul, add)
3059 FLOAT_TERNOP(mul, sub)
3060 #undef FLOAT_TERNOP
3062 /* negated ternary operations */
3063 #define FLOAT_NTERNOP(name1, name2) \
3064 uint64_t helper_float_n ## name1 ## name2 ## _d(CPUMIPSState *env, \
3065 uint64_t fdt0, \
3066 uint64_t fdt1, \
3067 uint64_t fdt2) \
3069 fdt0 = float64_ ## name1 (fdt0, fdt1, &env->active_fpu.fp_status); \
3070 fdt2 = float64_ ## name2 (fdt0, fdt2, &env->active_fpu.fp_status); \
3071 return float64_chs(fdt2); \
3074 uint32_t helper_float_n ## name1 ## name2 ## _s(CPUMIPSState *env, \
3075 uint32_t fst0, \
3076 uint32_t fst1, \
3077 uint32_t fst2) \
3079 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
3080 fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
3081 return float32_chs(fst2); \
3084 uint64_t helper_float_n ## name1 ## name2 ## _ps(CPUMIPSState *env, \
3085 uint64_t fdt0, \
3086 uint64_t fdt1, \
3087 uint64_t fdt2) \
3089 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3090 uint32_t fsth0 = fdt0 >> 32; \
3091 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3092 uint32_t fsth1 = fdt1 >> 32; \
3093 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
3094 uint32_t fsth2 = fdt2 >> 32; \
3096 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
3097 fsth0 = float32_ ## name1 (fsth0, fsth1, &env->active_fpu.fp_status); \
3098 fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
3099 fsth2 = float32_ ## name2 (fsth0, fsth2, &env->active_fpu.fp_status); \
3100 fst2 = float32_chs(fst2); \
3101 fsth2 = float32_chs(fsth2); \
3102 return ((uint64_t)fsth2 << 32) | fst2; \
3105 FLOAT_NTERNOP(mul, add)
3106 FLOAT_NTERNOP(mul, sub)
3107 #undef FLOAT_NTERNOP
3109 /* MIPS specific binary operations */
3110 uint64_t helper_float_recip2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3112 set_float_exception_flags(0, &env->active_fpu.fp_status);
3113 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3114 fdt2 = float64_chs(float64_sub(fdt2, FLOAT_ONE64, &env->active_fpu.fp_status));
3115 update_fcr31(env);
3116 return fdt2;
3119 uint32_t helper_float_recip2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3121 set_float_exception_flags(0, &env->active_fpu.fp_status);
3122 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3123 fst2 = float32_chs(float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status));
3124 update_fcr31(env);
3125 return fst2;
3128 uint64_t helper_float_recip2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3130 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3131 uint32_t fsth0 = fdt0 >> 32;
3132 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3133 uint32_t fsth2 = fdt2 >> 32;
3135 set_float_exception_flags(0, &env->active_fpu.fp_status);
3136 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3137 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3138 fst2 = float32_chs(float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status));
3139 fsth2 = float32_chs(float32_sub(fsth2, FLOAT_ONE32, &env->active_fpu.fp_status));
3140 update_fcr31(env);
3141 return ((uint64_t)fsth2 << 32) | fst2;
3144 uint64_t helper_float_rsqrt2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3146 set_float_exception_flags(0, &env->active_fpu.fp_status);
3147 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3148 fdt2 = float64_sub(fdt2, FLOAT_ONE64, &env->active_fpu.fp_status);
3149 fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
3150 update_fcr31(env);
3151 return fdt2;
3154 uint32_t helper_float_rsqrt2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3156 set_float_exception_flags(0, &env->active_fpu.fp_status);
3157 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3158 fst2 = float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status);
3159 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3160 update_fcr31(env);
3161 return fst2;
3164 uint64_t helper_float_rsqrt2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3166 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3167 uint32_t fsth0 = fdt0 >> 32;
3168 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3169 uint32_t fsth2 = fdt2 >> 32;
3171 set_float_exception_flags(0, &env->active_fpu.fp_status);
3172 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3173 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3174 fst2 = float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status);
3175 fsth2 = float32_sub(fsth2, FLOAT_ONE32, &env->active_fpu.fp_status);
3176 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3177 fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
3178 update_fcr31(env);
3179 return ((uint64_t)fsth2 << 32) | fst2;
3182 uint64_t helper_float_addr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3184 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3185 uint32_t fsth0 = fdt0 >> 32;
3186 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3187 uint32_t fsth1 = fdt1 >> 32;
3188 uint32_t fst2;
3189 uint32_t fsth2;
3191 set_float_exception_flags(0, &env->active_fpu.fp_status);
3192 fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
3193 fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
3194 update_fcr31(env);
3195 return ((uint64_t)fsth2 << 32) | fst2;
3198 uint64_t helper_float_mulr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3200 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3201 uint32_t fsth0 = fdt0 >> 32;
3202 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3203 uint32_t fsth1 = fdt1 >> 32;
3204 uint32_t fst2;
3205 uint32_t fsth2;
3207 set_float_exception_flags(0, &env->active_fpu.fp_status);
3208 fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
3209 fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
3210 update_fcr31(env);
3211 return ((uint64_t)fsth2 << 32) | fst2;
3214 /* compare operations */
3215 #define FOP_COND_D(op, cond) \
3216 void helper_cmp_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3217 uint64_t fdt1, int cc) \
3219 int c; \
3220 set_float_exception_flags(0, &env->active_fpu.fp_status); \
3221 c = cond; \
3222 update_fcr31(env); \
3223 if (c) \
3224 SET_FP_COND(cc, env->active_fpu); \
3225 else \
3226 CLEAR_FP_COND(cc, env->active_fpu); \
3228 void helper_cmpabs_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3229 uint64_t fdt1, int cc) \
3231 int c; \
3232 set_float_exception_flags(0, &env->active_fpu.fp_status); \
3233 fdt0 = float64_abs(fdt0); \
3234 fdt1 = float64_abs(fdt1); \
3235 c = cond; \
3236 update_fcr31(env); \
3237 if (c) \
3238 SET_FP_COND(cc, env->active_fpu); \
3239 else \
3240 CLEAR_FP_COND(cc, env->active_fpu); \
3243 /* NOTE: the comma operator will make "cond" to eval to false,
3244 * but float64_unordered_quiet() is still called. */
3245 FOP_COND_D(f, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3246 FOP_COND_D(un, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status))
3247 FOP_COND_D(eq, float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3248 FOP_COND_D(ueq, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3249 FOP_COND_D(olt, float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3250 FOP_COND_D(ult, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3251 FOP_COND_D(ole, float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3252 FOP_COND_D(ule, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
3253 /* NOTE: the comma operator will make "cond" to eval to false,
3254 * but float64_unordered() is still called. */
3255 FOP_COND_D(sf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
3256 FOP_COND_D(ngle,float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status))
3257 FOP_COND_D(seq, float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3258 FOP_COND_D(ngl, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
3259 FOP_COND_D(lt, float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3260 FOP_COND_D(nge, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
3261 FOP_COND_D(le, float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3262 FOP_COND_D(ngt, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
3264 #define FOP_COND_S(op, cond) \
3265 void helper_cmp_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3266 uint32_t fst1, int cc) \
3268 int c; \
3269 set_float_exception_flags(0, &env->active_fpu.fp_status); \
3270 c = cond; \
3271 update_fcr31(env); \
3272 if (c) \
3273 SET_FP_COND(cc, env->active_fpu); \
3274 else \
3275 CLEAR_FP_COND(cc, env->active_fpu); \
3277 void helper_cmpabs_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
3278 uint32_t fst1, int cc) \
3280 int c; \
3281 set_float_exception_flags(0, &env->active_fpu.fp_status); \
3282 fst0 = float32_abs(fst0); \
3283 fst1 = float32_abs(fst1); \
3284 c = cond; \
3285 update_fcr31(env); \
3286 if (c) \
3287 SET_FP_COND(cc, env->active_fpu); \
3288 else \
3289 CLEAR_FP_COND(cc, env->active_fpu); \
3292 /* NOTE: the comma operator will make "cond" to eval to false,
3293 * but float32_unordered_quiet() is still called. */
3294 FOP_COND_S(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
3295 FOP_COND_S(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status))
3296 FOP_COND_S(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3297 FOP_COND_S(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
3298 FOP_COND_S(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3299 FOP_COND_S(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
3300 FOP_COND_S(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3301 FOP_COND_S(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
3302 /* NOTE: the comma operator will make "cond" to eval to false,
3303 * but float32_unordered() is still called. */
3304 FOP_COND_S(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
3305 FOP_COND_S(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status))
3306 FOP_COND_S(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3307 FOP_COND_S(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
3308 FOP_COND_S(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3309 FOP_COND_S(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
3310 FOP_COND_S(le, float32_le(fst0, fst1, &env->active_fpu.fp_status))
3311 FOP_COND_S(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
3313 #define FOP_COND_PS(op, condl, condh) \
3314 void helper_cmp_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3315 uint64_t fdt1, int cc) \
3317 uint32_t fst0, fsth0, fst1, fsth1; \
3318 int ch, cl; \
3319 set_float_exception_flags(0, &env->active_fpu.fp_status); \
3320 fst0 = fdt0 & 0XFFFFFFFF; \
3321 fsth0 = fdt0 >> 32; \
3322 fst1 = fdt1 & 0XFFFFFFFF; \
3323 fsth1 = fdt1 >> 32; \
3324 cl = condl; \
3325 ch = condh; \
3326 update_fcr31(env); \
3327 if (cl) \
3328 SET_FP_COND(cc, env->active_fpu); \
3329 else \
3330 CLEAR_FP_COND(cc, env->active_fpu); \
3331 if (ch) \
3332 SET_FP_COND(cc + 1, env->active_fpu); \
3333 else \
3334 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3336 void helper_cmpabs_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
3337 uint64_t fdt1, int cc) \
3339 uint32_t fst0, fsth0, fst1, fsth1; \
3340 int ch, cl; \
3341 fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
3342 fsth0 = float32_abs(fdt0 >> 32); \
3343 fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
3344 fsth1 = float32_abs(fdt1 >> 32); \
3345 cl = condl; \
3346 ch = condh; \
3347 update_fcr31(env); \
3348 if (cl) \
3349 SET_FP_COND(cc, env->active_fpu); \
3350 else \
3351 CLEAR_FP_COND(cc, env->active_fpu); \
3352 if (ch) \
3353 SET_FP_COND(cc + 1, env->active_fpu); \
3354 else \
3355 CLEAR_FP_COND(cc + 1, env->active_fpu); \
3358 /* NOTE: the comma operator will make "cond" to eval to false,
3359 * but float32_unordered_quiet() is still called. */
3360 FOP_COND_PS(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0),
3361 (float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3362 FOP_COND_PS(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status),
3363 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status))
3364 FOP_COND_PS(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3365 float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3366 FOP_COND_PS(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
3367 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3368 FOP_COND_PS(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3369 float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3370 FOP_COND_PS(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
3371 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3372 FOP_COND_PS(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3373 float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3374 FOP_COND_PS(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
3375 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
3376 /* NOTE: the comma operator will make "cond" to eval to false,
3377 * but float32_unordered() is still called. */
3378 FOP_COND_PS(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0),
3379 (float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status), 0))
3380 FOP_COND_PS(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status),
3381 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status))
3382 FOP_COND_PS(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3383 float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3384 FOP_COND_PS(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
3385 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
3386 FOP_COND_PS(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3387 float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3388 FOP_COND_PS(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
3389 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
3390 FOP_COND_PS(le, float32_le(fst0, fst1, &env->active_fpu.fp_status),
3391 float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3392 FOP_COND_PS(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
3393 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))