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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "host-utils.h"
25 /*****************************************************************************/
26 /* Exceptions processing helpers */
28 void do_raise_exception_err (uint32_t exception, int error_code)
31 if (logfile && exception < 0x100)
32 fprintf(logfile, "%s: %d %d\n", __func__, exception, error_code);
34 env->exception_index = exception;
35 env->error_code = error_code;
39 void do_raise_exception (uint32_t exception)
41 do_raise_exception_err(exception, 0);
44 void do_interrupt_restart (void)
46 if (!(env->CP0_Status & (1 << CP0St_EXL)) &&
47 !(env->CP0_Status & (1 << CP0St_ERL)) &&
48 !(env->hflags & MIPS_HFLAG_DM) &&
49 (env->CP0_Status & (1 << CP0St_IE)) &&
50 (env->CP0_Status & env->CP0_Cause & CP0Ca_IP_mask)) {
51 env->CP0_Cause &= ~(0x1f << CP0Ca_EC);
52 do_raise_exception(EXCP_EXT_INTERRUPT);
56 void do_restore_state (void *pc_ptr)
59 unsigned long pc = (unsigned long) pc_ptr;
63 cpu_restore_state (tb, env, pc, NULL);
67 target_ulong do_clo (target_ulong t0)
72 target_ulong do_clz (target_ulong t0)
77 #if defined(TARGET_MIPS64)
78 target_ulong do_dclo (target_ulong t0)
83 target_ulong do_dclz (target_ulong t0)
87 #endif /* TARGET_MIPS64 */
89 /* 64 bits arithmetic for 32 bits hosts */
90 static always_inline uint64_t get_HILO (void)
92 return ((uint64_t)(env->HI[env->current_tc][0]) << 32) | (uint32_t)env->LO[env->current_tc][0];
95 static always_inline void set_HILO (uint64_t HILO)
97 env->LO[env->current_tc][0] = (int32_t)HILO;
98 env->HI[env->current_tc][0] = (int32_t)(HILO >> 32);
101 static always_inline void set_HIT0_LO (target_ulong t0, uint64_t HILO)
103 env->LO[env->current_tc][0] = (int32_t)(HILO & 0xFFFFFFFF);
104 t0 = env->HI[env->current_tc][0] = (int32_t)(HILO >> 32);
107 static always_inline void set_HI_LOT0 (target_ulong t0, uint64_t HILO)
109 t0 = env->LO[env->current_tc][0] = (int32_t)(HILO & 0xFFFFFFFF);
110 env->HI[env->current_tc][0] = (int32_t)(HILO >> 32);
113 #if TARGET_LONG_BITS > HOST_LONG_BITS
114 void do_madd (target_ulong t0, target_ulong t1)
118 tmp = ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1);
119 set_HILO((int64_t)get_HILO() + tmp);
122 void do_maddu (target_ulong t0, target_ulong t1)
126 tmp = ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1);
127 set_HILO(get_HILO() + tmp);
130 void do_msub (target_ulong t0, target_ulong t1)
134 tmp = ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1);
135 set_HILO((int64_t)get_HILO() - tmp);
138 void do_msubu (target_ulong t0, target_ulong t1)
142 tmp = ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1);
143 set_HILO(get_HILO() - tmp);
145 #endif /* TARGET_LONG_BITS > HOST_LONG_BITS */
147 /* Multiplication variants of the vr54xx. */
148 target_ulong do_muls (target_ulong t0, target_ulong t1)
150 set_HI_LOT0(t0, 0 - ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1));
155 target_ulong do_mulsu (target_ulong t0, target_ulong t1)
157 set_HI_LOT0(t0, 0 - ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1));
162 target_ulong do_macc (target_ulong t0, target_ulong t1)
164 set_HI_LOT0(t0, ((int64_t)get_HILO()) + ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1));
169 target_ulong do_macchi (target_ulong t0, target_ulong t1)
171 set_HIT0_LO(t0, ((int64_t)get_HILO()) + ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1));
176 target_ulong do_maccu (target_ulong t0, target_ulong t1)
178 set_HI_LOT0(t0, ((uint64_t)get_HILO()) + ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1));
183 target_ulong do_macchiu (target_ulong t0, target_ulong t1)
185 set_HIT0_LO(t0, ((uint64_t)get_HILO()) + ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1));
190 target_ulong do_msac (target_ulong t0, target_ulong t1)
192 set_HI_LOT0(t0, ((int64_t)get_HILO()) - ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1));
197 target_ulong do_msachi (target_ulong t0, target_ulong t1)
199 set_HIT0_LO(t0, ((int64_t)get_HILO()) - ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1));
204 target_ulong do_msacu (target_ulong t0, target_ulong t1)
206 set_HI_LOT0(t0, ((uint64_t)get_HILO()) - ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1));
211 target_ulong do_msachiu (target_ulong t0, target_ulong t1)
213 set_HIT0_LO(t0, ((uint64_t)get_HILO()) - ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1));
218 target_ulong do_mulhi (target_ulong t0, target_ulong t1)
220 set_HIT0_LO(t0, (int64_t)(int32_t)t0 * (int64_t)(int32_t)t1);
225 target_ulong do_mulhiu (target_ulong t0, target_ulong t1)
227 set_HIT0_LO(t0, (uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1);
232 target_ulong do_mulshi (target_ulong t0, target_ulong t1)
234 set_HIT0_LO(t0, 0 - ((int64_t)(int32_t)t0 * (int64_t)(int32_t)t1));
239 target_ulong do_mulshiu (target_ulong t0, target_ulong t1)
241 set_HIT0_LO(t0, 0 - ((uint64_t)(uint32_t)t0 * (uint64_t)(uint32_t)t1));
247 void do_dmult (target_ulong t0, target_ulong t1)
249 muls64(&(env->LO[env->current_tc][0]), &(env->HI[env->current_tc][0]), t0, t1);
252 void do_dmultu (target_ulong t0, target_ulong t1)
254 mulu64(&(env->LO[env->current_tc][0]), &(env->HI[env->current_tc][0]), t0, t1);
258 #ifdef TARGET_WORDS_BIGENDIAN
259 #define GET_LMASK(v) ((v) & 3)
260 #define GET_OFFSET(addr, offset) (addr + (offset))
262 #define GET_LMASK(v) (((v) & 3) ^ 3)
263 #define GET_OFFSET(addr, offset) (addr - (offset))
266 target_ulong do_lwl(target_ulong t0, target_ulong t1, int mem_idx)
270 #ifdef CONFIG_USER_ONLY
271 #define ldfun ldub_raw
273 int (*ldfun)(target_ulong);
277 case 0: ldfun = ldub_kernel; break;
278 case 1: ldfun = ldub_super; break;
280 case 2: ldfun = ldub_user; break;
284 t1 = (t1 & 0x00FFFFFF) | (tmp << 24);
286 if (GET_LMASK(t0) <= 2) {
287 tmp = ldfun(GET_OFFSET(t0, 1));
288 t1 = (t1 & 0xFF00FFFF) | (tmp << 16);
291 if (GET_LMASK(t0) <= 1) {
292 tmp = ldfun(GET_OFFSET(t0, 2));
293 t1 = (t1 & 0xFFFF00FF) | (tmp << 8);
296 if (GET_LMASK(t0) == 0) {
297 tmp = ldfun(GET_OFFSET(t0, 3));
298 t1 = (t1 & 0xFFFFFF00) | tmp;
303 target_ulong do_lwr(target_ulong t0, target_ulong t1, int mem_idx)
307 #ifdef CONFIG_USER_ONLY
308 #define ldfun ldub_raw
310 int (*ldfun)(target_ulong);
314 case 0: ldfun = ldub_kernel; break;
315 case 1: ldfun = ldub_super; break;
317 case 2: ldfun = ldub_user; break;
321 t1 = (t1 & 0xFFFFFF00) | tmp;
323 if (GET_LMASK(t0) >= 1) {
324 tmp = ldfun(GET_OFFSET(t0, -1));
325 t1 = (t1 & 0xFFFF00FF) | (tmp << 8);
328 if (GET_LMASK(t0) >= 2) {
329 tmp = ldfun(GET_OFFSET(t0, -2));
330 t1 = (t1 & 0xFF00FFFF) | (tmp << 16);
333 if (GET_LMASK(t0) == 3) {
334 tmp = ldfun(GET_OFFSET(t0, -3));
335 t1 = (t1 & 0x00FFFFFF) | (tmp << 24);
340 void do_swl(target_ulong t0, target_ulong t1, int mem_idx)
342 #ifdef CONFIG_USER_ONLY
343 #define stfun stb_raw
345 void (*stfun)(target_ulong, int);
349 case 0: stfun = stb_kernel; break;
350 case 1: stfun = stb_super; break;
352 case 2: stfun = stb_user; break;
355 stfun(t0, (uint8_t)(t1 >> 24));
357 if (GET_LMASK(t0) <= 2)
358 stfun(GET_OFFSET(t0, 1), (uint8_t)(t1 >> 16));
360 if (GET_LMASK(t0) <= 1)
361 stfun(GET_OFFSET(t0, 2), (uint8_t)(t1 >> 8));
363 if (GET_LMASK(t0) == 0)
364 stfun(GET_OFFSET(t0, 3), (uint8_t)t1);
367 void do_swr(target_ulong t0, target_ulong t1, int mem_idx)
369 #ifdef CONFIG_USER_ONLY
370 #define stfun stb_raw
372 void (*stfun)(target_ulong, int);
376 case 0: stfun = stb_kernel; break;
377 case 1: stfun = stb_super; break;
379 case 2: stfun = stb_user; break;
382 stfun(t0, (uint8_t)t1);
384 if (GET_LMASK(t0) >= 1)
385 stfun(GET_OFFSET(t0, -1), (uint8_t)(t1 >> 8));
387 if (GET_LMASK(t0) >= 2)
388 stfun(GET_OFFSET(t0, -2), (uint8_t)(t1 >> 16));
390 if (GET_LMASK(t0) == 3)
391 stfun(GET_OFFSET(t0, -3), (uint8_t)(t1 >> 24));
394 #if defined(TARGET_MIPS64)
395 /* "half" load and stores. We must do the memory access inline,
396 or fault handling won't work. */
398 #ifdef TARGET_WORDS_BIGENDIAN
399 #define GET_LMASK64(v) ((v) & 7)
401 #define GET_LMASK64(v) (((v) & 7) ^ 7)
404 target_ulong do_ldl(target_ulong t0, target_ulong t1, int mem_idx)
408 #ifdef CONFIG_USER_ONLY
409 #define ldfun ldub_raw
411 int (*ldfun)(target_ulong);
415 case 0: ldfun = ldub_kernel; break;
416 case 1: ldfun = ldub_super; break;
418 case 2: ldfun = ldub_user; break;
422 t1 = (t1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
424 if (GET_LMASK64(t0) <= 6) {
425 tmp = ldfun(GET_OFFSET(t0, 1));
426 t1 = (t1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
429 if (GET_LMASK64(t0) <= 5) {
430 tmp = ldfun(GET_OFFSET(t0, 2));
431 t1 = (t1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
434 if (GET_LMASK64(t0) <= 4) {
435 tmp = ldfun(GET_OFFSET(t0, 3));
436 t1 = (t1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
439 if (GET_LMASK64(t0) <= 3) {
440 tmp = ldfun(GET_OFFSET(t0, 4));
441 t1 = (t1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
444 if (GET_LMASK64(t0) <= 2) {
445 tmp = ldfun(GET_OFFSET(t0, 5));
446 t1 = (t1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
449 if (GET_LMASK64(t0) <= 1) {
450 tmp = ldfun(GET_OFFSET(t0, 6));
451 t1 = (t1 & 0xFFFFFFFFFFFF00FFULL) | (tmp << 8);
454 if (GET_LMASK64(t0) == 0) {
455 tmp = ldfun(GET_OFFSET(t0, 7));
456 t1 = (t1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
462 target_ulong do_ldr(target_ulong t0, target_ulong t1, int mem_idx)
466 #ifdef CONFIG_USER_ONLY
467 #define ldfun ldub_raw
469 int (*ldfun)(target_ulong);
473 case 0: ldfun = ldub_kernel; break;
474 case 1: ldfun = ldub_super; break;
476 case 2: ldfun = ldub_user; break;
480 t1 = (t1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
482 if (GET_LMASK64(t0) >= 1) {
483 tmp = ldfun(GET_OFFSET(t0, -1));
484 t1 = (t1 & 0xFFFFFFFFFFFF00FFULL) | (tmp << 8);
487 if (GET_LMASK64(t0) >= 2) {
488 tmp = ldfun(GET_OFFSET(t0, -2));
489 t1 = (t1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
492 if (GET_LMASK64(t0) >= 3) {
493 tmp = ldfun(GET_OFFSET(t0, -3));
494 t1 = (t1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
497 if (GET_LMASK64(t0) >= 4) {
498 tmp = ldfun(GET_OFFSET(t0, -4));
499 t1 = (t1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
502 if (GET_LMASK64(t0) >= 5) {
503 tmp = ldfun(GET_OFFSET(t0, -5));
504 t1 = (t1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
507 if (GET_LMASK64(t0) >= 6) {
508 tmp = ldfun(GET_OFFSET(t0, -6));
509 t1 = (t1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
512 if (GET_LMASK64(t0) == 7) {
513 tmp = ldfun(GET_OFFSET(t0, -7));
514 t1 = (t1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
520 void do_sdl(target_ulong t0, target_ulong t1, int mem_idx)
522 #ifdef CONFIG_USER_ONLY
523 #define stfun stb_raw
525 void (*stfun)(target_ulong, int);
529 case 0: stfun = stb_kernel; break;
530 case 1: stfun = stb_super; break;
532 case 2: stfun = stb_user; break;
535 stfun(t0, (uint8_t)(t1 >> 56));
537 if (GET_LMASK64(t0) <= 6)
538 stfun(GET_OFFSET(t0, 1), (uint8_t)(t1 >> 48));
540 if (GET_LMASK64(t0) <= 5)
541 stfun(GET_OFFSET(t0, 2), (uint8_t)(t1 >> 40));
543 if (GET_LMASK64(t0) <= 4)
544 stfun(GET_OFFSET(t0, 3), (uint8_t)(t1 >> 32));
546 if (GET_LMASK64(t0) <= 3)
547 stfun(GET_OFFSET(t0, 4), (uint8_t)(t1 >> 24));
549 if (GET_LMASK64(t0) <= 2)
550 stfun(GET_OFFSET(t0, 5), (uint8_t)(t1 >> 16));
552 if (GET_LMASK64(t0) <= 1)
553 stfun(GET_OFFSET(t0, 6), (uint8_t)(t1 >> 8));
555 if (GET_LMASK64(t0) <= 0)
556 stfun(GET_OFFSET(t0, 7), (uint8_t)t1);
559 void do_sdr(target_ulong t0, target_ulong t1, int mem_idx)
561 #ifdef CONFIG_USER_ONLY
562 #define stfun stb_raw
564 void (*stfun)(target_ulong, int);
568 case 0: stfun = stb_kernel; break;
569 case 1: stfun = stb_super; break;
571 case 2: stfun = stb_user; break;
574 stfun(t0, (uint8_t)t1);
576 if (GET_LMASK64(t0) >= 1)
577 stfun(GET_OFFSET(t0, -1), (uint8_t)(t1 >> 8));
579 if (GET_LMASK64(t0) >= 2)
580 stfun(GET_OFFSET(t0, -2), (uint8_t)(t1 >> 16));
582 if (GET_LMASK64(t0) >= 3)
583 stfun(GET_OFFSET(t0, -3), (uint8_t)(t1 >> 24));
585 if (GET_LMASK64(t0) >= 4)
586 stfun(GET_OFFSET(t0, -4), (uint8_t)(t1 >> 32));
588 if (GET_LMASK64(t0) >= 5)
589 stfun(GET_OFFSET(t0, -5), (uint8_t)(t1 >> 40));
591 if (GET_LMASK64(t0) >= 6)
592 stfun(GET_OFFSET(t0, -6), (uint8_t)(t1 >> 48));
594 if (GET_LMASK64(t0) == 7)
595 stfun(GET_OFFSET(t0, -7), (uint8_t)(t1 >> 56));
597 #endif /* TARGET_MIPS64 */
599 #ifdef CONFIG_USER_ONLY
600 void do_mfc0_random (void)
602 cpu_abort(env, "mfc0 random\n");
605 void do_mfc0_count (void)
607 cpu_abort(env, "mfc0 count\n");
610 void cpu_mips_store_count(CPUState *env, uint32_t value)
612 cpu_abort(env, "mtc0 count\n");
615 void cpu_mips_store_compare(CPUState *env, uint32_t value)
617 cpu_abort(env, "mtc0 compare\n");
620 void cpu_mips_start_count(CPUState *env)
622 cpu_abort(env, "start count\n");
625 void cpu_mips_stop_count(CPUState *env)
627 cpu_abort(env, "stop count\n");
630 void cpu_mips_update_irq(CPUState *env)
632 cpu_abort(env, "mtc0 status / mtc0 cause\n");
635 void do_mtc0_status_debug(uint32_t old, uint32_t val)
637 cpu_abort(env, "mtc0 status debug\n");
640 void do_mtc0_status_irqraise_debug (void)
642 cpu_abort(env, "mtc0 status irqraise debug\n");
645 void cpu_mips_tlb_flush (CPUState *env, int flush_global)
647 cpu_abort(env, "mips_tlb_flush\n");
653 target_ulong do_mfc0_mvpcontrol (target_ulong t0)
655 return env->mvp->CP0_MVPControl;
658 target_ulong do_mfc0_mvpconf0 (target_ulong t0)
660 return env->mvp->CP0_MVPConf0;
663 target_ulong do_mfc0_mvpconf1 (target_ulong t0)
665 return env->mvp->CP0_MVPConf1;
668 target_ulong do_mfc0_random (target_ulong t0)
670 return (int32_t)cpu_mips_get_random(env);
673 target_ulong do_mfc0_tcstatus (target_ulong t0)
675 return env->CP0_TCStatus[env->current_tc];
678 target_ulong do_mftc0_tcstatus(target_ulong t0)
680 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
682 return env->CP0_TCStatus[other_tc];
685 target_ulong do_mfc0_tcbind (target_ulong t0)
687 return env->CP0_TCBind[env->current_tc];
690 target_ulong do_mftc0_tcbind(target_ulong t0)
692 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
694 return env->CP0_TCBind[other_tc];
697 target_ulong do_mfc0_tcrestart (target_ulong t0)
699 return env->PC[env->current_tc];
702 target_ulong do_mftc0_tcrestart(target_ulong t0)
704 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
706 return env->PC[other_tc];
709 target_ulong do_mfc0_tchalt (target_ulong t0)
711 return env->CP0_TCHalt[env->current_tc];
714 target_ulong do_mftc0_tchalt(target_ulong t0)
716 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
718 return env->CP0_TCHalt[other_tc];
721 target_ulong do_mfc0_tccontext (target_ulong t0)
723 return env->CP0_TCContext[env->current_tc];
726 target_ulong do_mftc0_tccontext(target_ulong t0)
728 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
730 return env->CP0_TCContext[other_tc];
733 target_ulong do_mfc0_tcschedule (target_ulong t0)
735 return env->CP0_TCSchedule[env->current_tc];
738 target_ulong do_mftc0_tcschedule(target_ulong t0)
740 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
742 return env->CP0_TCSchedule[other_tc];
745 target_ulong do_mfc0_tcschefback (target_ulong t0)
747 return env->CP0_TCScheFBack[env->current_tc];
750 target_ulong do_mftc0_tcschefback(target_ulong t0)
752 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
754 return env->CP0_TCScheFBack[other_tc];
757 target_ulong do_mfc0_count (target_ulong t0)
759 return (int32_t)cpu_mips_get_count(env);
762 target_ulong do_mftc0_entryhi(target_ulong t0)
764 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
766 return (env->CP0_EntryHi & ~0xff) | (env->CP0_TCStatus[other_tc] & 0xff);
769 target_ulong do_mftc0_status(target_ulong t0)
771 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
772 uint32_t tcstatus = env->CP0_TCStatus[other_tc];
774 t0 = env->CP0_Status & ~0xf1000018;
775 t0 |= tcstatus & (0xf << CP0TCSt_TCU0);
776 t0 |= (tcstatus & (1 << CP0TCSt_TMX)) >> (CP0TCSt_TMX - CP0St_MX);
777 t0 |= (tcstatus & (0x3 << CP0TCSt_TKSU)) >> (CP0TCSt_TKSU - CP0St_KSU);
782 target_ulong do_mfc0_lladdr (target_ulong t0)
784 return (int32_t)env->CP0_LLAddr >> 4;
787 target_ulong do_mfc0_watchlo (target_ulong t0, uint32_t sel)
789 return (int32_t)env->CP0_WatchLo[sel];
792 target_ulong do_mfc0_watchhi (target_ulong t0, uint32_t sel)
794 return env->CP0_WatchHi[sel];
797 target_ulong do_mfc0_debug (target_ulong t0)
800 if (env->hflags & MIPS_HFLAG_DM)
806 target_ulong do_mftc0_debug(target_ulong t0)
808 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
810 /* XXX: Might be wrong, check with EJTAG spec. */
811 return (env->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
812 (env->CP0_Debug_tcstatus[other_tc] &
813 ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
816 #if defined(TARGET_MIPS64)
817 target_ulong do_dmfc0_tcrestart (target_ulong t0)
819 return env->PC[env->current_tc];
822 target_ulong do_dmfc0_tchalt (target_ulong t0)
824 return env->CP0_TCHalt[env->current_tc];
827 target_ulong do_dmfc0_tccontext (target_ulong t0)
829 return env->CP0_TCContext[env->current_tc];
832 target_ulong do_dmfc0_tcschedule (target_ulong t0)
834 return env->CP0_TCSchedule[env->current_tc];
837 target_ulong do_dmfc0_tcschefback (target_ulong t0)
839 return env->CP0_TCScheFBack[env->current_tc];
842 target_ulong do_dmfc0_lladdr (target_ulong t0)
844 return env->CP0_LLAddr >> 4;
847 target_ulong do_dmfc0_watchlo (target_ulong t0, uint32_t sel)
849 return env->CP0_WatchLo[sel];
851 #endif /* TARGET_MIPS64 */
853 void do_mtc0_index (target_ulong t0)
856 unsigned int tmp = env->tlb->nb_tlb;
862 env->CP0_Index = (env->CP0_Index & 0x80000000) | (t0 & (num - 1));
865 void do_mtc0_mvpcontrol (target_ulong t0)
870 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
871 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
873 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
874 mask |= (1 << CP0MVPCo_STLB);
875 newval = (env->mvp->CP0_MVPControl & ~mask) | (t0 & mask);
877 // TODO: Enable/disable shared TLB, enable/disable VPEs.
879 env->mvp->CP0_MVPControl = newval;
882 void do_mtc0_vpecontrol (target_ulong t0)
887 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
888 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
889 newval = (env->CP0_VPEControl & ~mask) | (t0 & mask);
891 /* Yield scheduler intercept not implemented. */
892 /* Gating storage scheduler intercept not implemented. */
894 // TODO: Enable/disable TCs.
896 env->CP0_VPEControl = newval;
899 void do_mtc0_vpeconf0 (target_ulong t0)
904 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
905 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
906 mask |= (0xff << CP0VPEC0_XTC);
907 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
909 newval = (env->CP0_VPEConf0 & ~mask) | (t0 & mask);
911 // TODO: TC exclusive handling due to ERL/EXL.
913 env->CP0_VPEConf0 = newval;
916 void do_mtc0_vpeconf1 (target_ulong t0)
921 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
922 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
923 (0xff << CP0VPEC1_NCP1);
924 newval = (env->CP0_VPEConf1 & ~mask) | (t0 & mask);
926 /* UDI not implemented. */
927 /* CP2 not implemented. */
929 // TODO: Handle FPU (CP1) binding.
931 env->CP0_VPEConf1 = newval;
934 void do_mtc0_yqmask (target_ulong t0)
936 /* Yield qualifier inputs not implemented. */
937 env->CP0_YQMask = 0x00000000;
940 void do_mtc0_vpeopt (target_ulong t0)
942 env->CP0_VPEOpt = t0 & 0x0000ffff;
945 void do_mtc0_entrylo0 (target_ulong t0)
947 /* Large physaddr (PABITS) not implemented */
948 /* 1k pages not implemented */
949 env->CP0_EntryLo0 = t0 & 0x3FFFFFFF;
952 void do_mtc0_tcstatus (target_ulong t0)
954 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
957 newval = (env->CP0_TCStatus[env->current_tc] & ~mask) | (t0 & mask);
959 // TODO: Sync with CP0_Status.
961 env->CP0_TCStatus[env->current_tc] = newval;
964 void do_mttc0_tcstatus (target_ulong t0)
966 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
968 // TODO: Sync with CP0_Status.
970 env->CP0_TCStatus[other_tc] = t0;
973 void do_mtc0_tcbind (target_ulong t0)
975 uint32_t mask = (1 << CP0TCBd_TBE);
978 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
979 mask |= (1 << CP0TCBd_CurVPE);
980 newval = (env->CP0_TCBind[env->current_tc] & ~mask) | (t0 & mask);
981 env->CP0_TCBind[env->current_tc] = newval;
984 void do_mttc0_tcbind (target_ulong t0)
986 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
987 uint32_t mask = (1 << CP0TCBd_TBE);
990 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
991 mask |= (1 << CP0TCBd_CurVPE);
992 newval = (env->CP0_TCBind[other_tc] & ~mask) | (t0 & mask);
993 env->CP0_TCBind[other_tc] = newval;
996 void do_mtc0_tcrestart (target_ulong t0)
998 env->PC[env->current_tc] = t0;
999 env->CP0_TCStatus[env->current_tc] &= ~(1 << CP0TCSt_TDS);
1000 env->CP0_LLAddr = 0ULL;
1001 /* MIPS16 not implemented. */
1004 void do_mttc0_tcrestart (target_ulong t0)
1006 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1008 env->PC[other_tc] = t0;
1009 env->CP0_TCStatus[other_tc] &= ~(1 << CP0TCSt_TDS);
1010 env->CP0_LLAddr = 0ULL;
1011 /* MIPS16 not implemented. */
1014 void do_mtc0_tchalt (target_ulong t0)
1016 env->CP0_TCHalt[env->current_tc] = t0 & 0x1;
1018 // TODO: Halt TC / Restart (if allocated+active) TC.
1021 void do_mttc0_tchalt (target_ulong t0)
1023 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1025 // TODO: Halt TC / Restart (if allocated+active) TC.
1027 env->CP0_TCHalt[other_tc] = t0;
1030 void do_mtc0_tccontext (target_ulong t0)
1032 env->CP0_TCContext[env->current_tc] = t0;
1035 void do_mttc0_tccontext (target_ulong t0)
1037 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1039 env->CP0_TCContext[other_tc] = t0;
1042 void do_mtc0_tcschedule (target_ulong t0)
1044 env->CP0_TCSchedule[env->current_tc] = t0;
1047 void do_mttc0_tcschedule (target_ulong t0)
1049 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1051 env->CP0_TCSchedule[other_tc] = t0;
1054 void do_mtc0_tcschefback (target_ulong t0)
1056 env->CP0_TCScheFBack[env->current_tc] = t0;
1059 void do_mttc0_tcschefback (target_ulong t0)
1061 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1063 env->CP0_TCScheFBack[other_tc] = t0;
1066 void do_mtc0_entrylo1 (target_ulong t0)
1068 /* Large physaddr (PABITS) not implemented */
1069 /* 1k pages not implemented */
1070 env->CP0_EntryLo1 = t0 & 0x3FFFFFFF;
1073 void do_mtc0_context (target_ulong t0)
1075 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (t0 & ~0x007FFFFF);
1078 void do_mtc0_pagemask (target_ulong t0)
1080 /* 1k pages not implemented */
1081 env->CP0_PageMask = t0 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1084 void do_mtc0_pagegrain (target_ulong t0)
1086 /* SmartMIPS not implemented */
1087 /* Large physaddr (PABITS) not implemented */
1088 /* 1k pages not implemented */
1089 env->CP0_PageGrain = 0;
1092 void do_mtc0_wired (target_ulong t0)
1094 env->CP0_Wired = t0 % env->tlb->nb_tlb;
1097 void do_mtc0_srsconf0 (target_ulong t0)
1099 env->CP0_SRSConf0 |= t0 & env->CP0_SRSConf0_rw_bitmask;
1102 void do_mtc0_srsconf1 (target_ulong t0)
1104 env->CP0_SRSConf1 |= t0 & env->CP0_SRSConf1_rw_bitmask;
1107 void do_mtc0_srsconf2 (target_ulong t0)
1109 env->CP0_SRSConf2 |= t0 & env->CP0_SRSConf2_rw_bitmask;
1112 void do_mtc0_srsconf3 (target_ulong t0)
1114 env->CP0_SRSConf3 |= t0 & env->CP0_SRSConf3_rw_bitmask;
1117 void do_mtc0_srsconf4 (target_ulong t0)
1119 env->CP0_SRSConf4 |= t0 & env->CP0_SRSConf4_rw_bitmask;
1122 void do_mtc0_hwrena (target_ulong t0)
1124 env->CP0_HWREna = t0 & 0x0000000F;
1127 void do_mtc0_count (target_ulong t0)
1129 cpu_mips_store_count(env, t0);
1132 void do_mtc0_entryhi (target_ulong t0)
1134 target_ulong old, val;
1136 /* 1k pages not implemented */
1137 val = t0 & ((TARGET_PAGE_MASK << 1) | 0xFF);
1138 #if defined(TARGET_MIPS64)
1139 val &= env->SEGMask;
1141 old = env->CP0_EntryHi;
1142 env->CP0_EntryHi = val;
1143 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1144 uint32_t tcst = env->CP0_TCStatus[env->current_tc] & ~0xff;
1145 env->CP0_TCStatus[env->current_tc] = tcst | (val & 0xff);
1147 /* If the ASID changes, flush qemu's TLB. */
1148 if ((old & 0xFF) != (val & 0xFF))
1149 cpu_mips_tlb_flush(env, 1);
1152 void do_mttc0_entryhi(target_ulong t0)
1154 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1156 env->CP0_EntryHi = (env->CP0_EntryHi & 0xff) | (t0 & ~0xff);
1157 env->CP0_TCStatus[other_tc] = (env->CP0_TCStatus[other_tc] & ~0xff) | (t0 & 0xff);
1160 void do_mtc0_compare (target_ulong t0)
1162 cpu_mips_store_compare(env, t0);
1165 void do_mtc0_status (target_ulong t0)
1168 uint32_t mask = env->CP0_Status_rw_bitmask;
1171 old = env->CP0_Status;
1172 env->CP0_Status = (env->CP0_Status & ~mask) | val;
1173 compute_hflags(env);
1174 if (loglevel & CPU_LOG_EXEC)
1175 do_mtc0_status_debug(old, val);
1176 cpu_mips_update_irq(env);
1179 void do_mttc0_status(target_ulong t0)
1181 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1182 uint32_t tcstatus = env->CP0_TCStatus[other_tc];
1184 env->CP0_Status = t0 & ~0xf1000018;
1185 tcstatus = (tcstatus & ~(0xf << CP0TCSt_TCU0)) | (t0 & (0xf << CP0St_CU0));
1186 tcstatus = (tcstatus & ~(1 << CP0TCSt_TMX)) | ((t0 & (1 << CP0St_MX)) << (CP0TCSt_TMX - CP0St_MX));
1187 tcstatus = (tcstatus & ~(0x3 << CP0TCSt_TKSU)) | ((t0 & (0x3 << CP0St_KSU)) << (CP0TCSt_TKSU - CP0St_KSU));
1188 env->CP0_TCStatus[other_tc] = tcstatus;
1191 void do_mtc0_intctl (target_ulong t0)
1193 /* vectored interrupts not implemented, no performance counters. */
1194 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000002e0) | (t0 & 0x000002e0);
1197 void do_mtc0_srsctl (target_ulong t0)
1199 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1200 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (t0 & mask);
1203 void do_mtc0_cause (target_ulong t0)
1205 uint32_t mask = 0x00C00300;
1206 uint32_t old = env->CP0_Cause;
1208 if (env->insn_flags & ISA_MIPS32R2)
1209 mask |= 1 << CP0Ca_DC;
1211 env->CP0_Cause = (env->CP0_Cause & ~mask) | (t0 & mask);
1213 if ((old ^ env->CP0_Cause) & (1 << CP0Ca_DC)) {
1214 if (env->CP0_Cause & (1 << CP0Ca_DC))
1215 cpu_mips_stop_count(env);
1217 cpu_mips_start_count(env);
1220 /* Handle the software interrupt as an hardware one, as they
1222 if (t0 & CP0Ca_IP_mask) {
1223 cpu_mips_update_irq(env);
1227 void do_mtc0_ebase (target_ulong t0)
1229 /* vectored interrupts not implemented */
1230 /* Multi-CPU not implemented */
1231 env->CP0_EBase = 0x80000000 | (t0 & 0x3FFFF000);
1234 void do_mtc0_config0 (target_ulong t0)
1236 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (t0 & 0x00000007);
1239 void do_mtc0_config2 (target_ulong t0)
1241 /* tertiary/secondary caches not implemented */
1242 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1245 void do_mtc0_watchlo (target_ulong t0, uint32_t sel)
1247 /* Watch exceptions for instructions, data loads, data stores
1249 env->CP0_WatchLo[sel] = (t0 & ~0x7);
1252 void do_mtc0_watchhi (target_ulong t0, uint32_t sel)
1254 env->CP0_WatchHi[sel] = (t0 & 0x40FF0FF8);
1255 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & t0 & 0x7);
1258 void do_mtc0_xcontext (target_ulong t0)
1260 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1261 env->CP0_XContext = (env->CP0_XContext & mask) | (t0 & ~mask);
1264 void do_mtc0_framemask (target_ulong t0)
1266 env->CP0_Framemask = t0; /* XXX */
1269 void do_mtc0_debug (target_ulong t0)
1271 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (t0 & 0x13300120);
1272 if (t0 & (1 << CP0DB_DM))
1273 env->hflags |= MIPS_HFLAG_DM;
1275 env->hflags &= ~MIPS_HFLAG_DM;
1278 void do_mttc0_debug(target_ulong t0)
1280 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1282 /* XXX: Might be wrong, check with EJTAG spec. */
1283 env->CP0_Debug_tcstatus[other_tc] = t0 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1284 env->CP0_Debug = (env->CP0_Debug & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1285 (t0 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1288 void do_mtc0_performance0 (target_ulong t0)
1290 env->CP0_Performance0 = t0 & 0x000007ff;
1293 void do_mtc0_taglo (target_ulong t0)
1295 env->CP0_TagLo = t0 & 0xFFFFFCF6;
1298 void do_mtc0_datalo (target_ulong t0)
1300 env->CP0_DataLo = t0; /* XXX */
1303 void do_mtc0_taghi (target_ulong t0)
1305 env->CP0_TagHi = t0; /* XXX */
1308 void do_mtc0_datahi (target_ulong t0)
1310 env->CP0_DataHi = t0; /* XXX */
1313 void do_mtc0_status_debug(uint32_t old, uint32_t val)
1315 fprintf(logfile, "Status %08x (%08x) => %08x (%08x) Cause %08x",
1316 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1317 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1319 switch (env->hflags & MIPS_HFLAG_KSU) {
1320 case MIPS_HFLAG_UM: fputs(", UM\n", logfile); break;
1321 case MIPS_HFLAG_SM: fputs(", SM\n", logfile); break;
1322 case MIPS_HFLAG_KM: fputs("\n", logfile); break;
1323 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
1327 void do_mtc0_status_irqraise_debug(void)
1329 fprintf(logfile, "Raise pending IRQs\n");
1331 #endif /* !CONFIG_USER_ONLY */
1333 /* MIPS MT functions */
1334 target_ulong do_mftgpr(target_ulong t0, uint32_t sel)
1336 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1338 return env->gpr[other_tc][sel];
1341 target_ulong do_mftlo(target_ulong t0, uint32_t sel)
1343 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1345 return env->LO[other_tc][sel];
1348 target_ulong do_mfthi(target_ulong t0, uint32_t sel)
1350 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1352 return env->HI[other_tc][sel];
1355 target_ulong do_mftacx(target_ulong t0, uint32_t sel)
1357 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1359 return env->ACX[other_tc][sel];
1362 target_ulong do_mftdsp(target_ulong t0)
1364 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1366 return env->DSPControl[other_tc];
1369 void do_mttgpr(target_ulong t0, uint32_t sel)
1371 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1373 env->gpr[other_tc][sel] = t0;
1376 void do_mttlo(target_ulong t0, uint32_t sel)
1378 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1380 env->LO[other_tc][sel] = t0;
1383 void do_mtthi(target_ulong t0, uint32_t sel)
1385 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1387 env->HI[other_tc][sel] = t0;
1390 void do_mttacx(target_ulong t0, uint32_t sel)
1392 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1394 env->ACX[other_tc][sel] = t0;
1397 void do_mttdsp(target_ulong t0)
1399 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1401 env->DSPControl[other_tc] = t0;
1404 /* MIPS MT functions */
1405 target_ulong do_dmt(target_ulong t0)
1414 target_ulong do_emt(target_ulong t0)
1423 target_ulong do_dvpe(target_ulong t0)
1432 target_ulong do_evpe(target_ulong t0)
1441 target_ulong do_fork(target_ulong t0, target_ulong t1)
1445 // TODO: store to TC register
1450 target_ulong do_yield(target_ulong t0)
1453 /* No scheduling policy implemented. */
1455 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1456 env->CP0_TCStatus[env->current_tc] & (1 << CP0TCSt_DT)) {
1457 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1458 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1459 do_raise_exception(EXCP_THREAD);
1462 } else if (t0 == 0) {
1463 if (0 /* TODO: TC underflow */) {
1464 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1465 do_raise_exception(EXCP_THREAD);
1467 // TODO: Deallocate TC
1469 } else if (t0 > 0) {
1470 /* Yield qualifier inputs not implemented. */
1471 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1472 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1473 do_raise_exception(EXCP_THREAD);
1475 return env->CP0_YQMask;
1479 void fpu_handle_exception(void)
1481 #ifdef CONFIG_SOFTFLOAT
1482 int flags = get_float_exception_flags(&env->fpu->fp_status);
1483 unsigned int cpuflags = 0, enable, cause = 0;
1485 enable = GET_FP_ENABLE(env->fpu->fcr31);
1487 /* determine current flags */
1488 if (flags & float_flag_invalid) {
1489 cpuflags |= FP_INVALID;
1490 cause |= FP_INVALID & enable;
1492 if (flags & float_flag_divbyzero) {
1493 cpuflags |= FP_DIV0;
1494 cause |= FP_DIV0 & enable;
1496 if (flags & float_flag_overflow) {
1497 cpuflags |= FP_OVERFLOW;
1498 cause |= FP_OVERFLOW & enable;
1500 if (flags & float_flag_underflow) {
1501 cpuflags |= FP_UNDERFLOW;
1502 cause |= FP_UNDERFLOW & enable;
1504 if (flags & float_flag_inexact) {
1505 cpuflags |= FP_INEXACT;
1506 cause |= FP_INEXACT & enable;
1508 SET_FP_FLAGS(env->fpu->fcr31, cpuflags);
1509 SET_FP_CAUSE(env->fpu->fcr31, cause);
1511 SET_FP_FLAGS(env->fpu->fcr31, 0);
1512 SET_FP_CAUSE(env->fpu->fcr31, 0);
1516 #ifndef CONFIG_USER_ONLY
1517 /* TLB management */
1518 void cpu_mips_tlb_flush (CPUState *env, int flush_global)
1520 /* Flush qemu's TLB and discard all shadowed entries. */
1521 tlb_flush (env, flush_global);
1522 env->tlb->tlb_in_use = env->tlb->nb_tlb;
1525 static void r4k_mips_tlb_flush_extra (CPUState *env, int first)
1527 /* Discard entries from env->tlb[first] onwards. */
1528 while (env->tlb->tlb_in_use > first) {
1529 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1533 static void r4k_fill_tlb (int idx)
1537 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1538 tlb = &env->tlb->mmu.r4k.tlb[idx];
1539 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1540 #if defined(TARGET_MIPS64)
1541 tlb->VPN &= env->SEGMask;
1543 tlb->ASID = env->CP0_EntryHi & 0xFF;
1544 tlb->PageMask = env->CP0_PageMask;
1545 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1546 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
1547 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
1548 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
1549 tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
1550 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
1551 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
1552 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
1553 tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
1556 void r4k_do_tlbwi (void)
1558 /* Discard cached TLB entries. We could avoid doing this if the
1559 tlbwi is just upgrading access permissions on the current entry;
1560 that might be a further win. */
1561 r4k_mips_tlb_flush_extra (env, env->tlb->nb_tlb);
1563 r4k_invalidate_tlb(env, env->CP0_Index % env->tlb->nb_tlb, 0);
1564 r4k_fill_tlb(env->CP0_Index % env->tlb->nb_tlb);
1567 void r4k_do_tlbwr (void)
1569 int r = cpu_mips_get_random(env);
1571 r4k_invalidate_tlb(env, r, 1);
1575 void r4k_do_tlbp (void)
1584 ASID = env->CP0_EntryHi & 0xFF;
1585 for (i = 0; i < env->tlb->nb_tlb; i++) {
1586 tlb = &env->tlb->mmu.r4k.tlb[i];
1587 /* 1k pages are not supported. */
1588 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1589 tag = env->CP0_EntryHi & ~mask;
1590 VPN = tlb->VPN & ~mask;
1591 /* Check ASID, virtual page number & size */
1592 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1598 if (i == env->tlb->nb_tlb) {
1599 /* No match. Discard any shadow entries, if any of them match. */
1600 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
1601 tlb = &env->tlb->mmu.r4k.tlb[i];
1602 /* 1k pages are not supported. */
1603 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1604 tag = env->CP0_EntryHi & ~mask;
1605 VPN = tlb->VPN & ~mask;
1606 /* Check ASID, virtual page number & size */
1607 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1608 r4k_mips_tlb_flush_extra (env, i);
1613 env->CP0_Index |= 0x80000000;
1617 void r4k_do_tlbr (void)
1622 ASID = env->CP0_EntryHi & 0xFF;
1623 tlb = &env->tlb->mmu.r4k.tlb[env->CP0_Index % env->tlb->nb_tlb];
1625 /* If this will change the current ASID, flush qemu's TLB. */
1626 if (ASID != tlb->ASID)
1627 cpu_mips_tlb_flush (env, 1);
1629 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
1631 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
1632 env->CP0_PageMask = tlb->PageMask;
1633 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
1634 (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
1635 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
1636 (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
1639 #endif /* !CONFIG_USER_ONLY */
1642 target_ulong do_di (target_ulong t0)
1644 t0 = env->CP0_Status;
1645 env->CP0_Status = t0 & ~(1 << CP0St_IE);
1646 cpu_mips_update_irq(env);
1651 target_ulong do_ei (target_ulong t0)
1653 t0 = env->CP0_Status;
1654 env->CP0_Status = t0 | (1 << CP0St_IE);
1655 cpu_mips_update_irq(env);
1660 void debug_pre_eret (void)
1662 fprintf(logfile, "ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
1663 env->PC[env->current_tc], env->CP0_EPC);
1664 if (env->CP0_Status & (1 << CP0St_ERL))
1665 fprintf(logfile, " ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
1666 if (env->hflags & MIPS_HFLAG_DM)
1667 fprintf(logfile, " DEPC " TARGET_FMT_lx, env->CP0_DEPC);
1668 fputs("\n", logfile);
1671 void debug_post_eret (void)
1673 fprintf(logfile, " => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
1674 env->PC[env->current_tc], env->CP0_EPC);
1675 if (env->CP0_Status & (1 << CP0St_ERL))
1676 fprintf(logfile, " ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
1677 if (env->hflags & MIPS_HFLAG_DM)
1678 fprintf(logfile, " DEPC " TARGET_FMT_lx, env->CP0_DEPC);
1679 switch (env->hflags & MIPS_HFLAG_KSU) {
1680 case MIPS_HFLAG_UM: fputs(", UM\n", logfile); break;
1681 case MIPS_HFLAG_SM: fputs(", SM\n", logfile); break;
1682 case MIPS_HFLAG_KM: fputs("\n", logfile); break;
1683 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
1687 void do_eret (target_ulong t0)
1689 if (loglevel & CPU_LOG_EXEC)
1691 if (env->CP0_Status & (1 << CP0St_ERL)) {
1692 env->PC[env->current_tc] = env->CP0_ErrorEPC;
1693 env->CP0_Status &= ~(1 << CP0St_ERL);
1695 env->PC[env->current_tc] = env->CP0_EPC;
1696 env->CP0_Status &= ~(1 << CP0St_EXL);
1698 compute_hflags(env);
1699 if (loglevel & CPU_LOG_EXEC)
1701 env->CP0_LLAddr = 1;
1704 void do_deret (target_ulong t0)
1706 if (loglevel & CPU_LOG_EXEC)
1708 env->PC[env->current_tc] = env->CP0_DEPC;
1709 env->hflags &= MIPS_HFLAG_DM;
1710 compute_hflags(env);
1711 if (loglevel & CPU_LOG_EXEC)
1713 env->CP0_LLAddr = 1;
1716 target_ulong do_rdhwr_cpunum(target_ulong t0)
1718 if ((env->hflags & MIPS_HFLAG_CP0) ||
1719 (env->CP0_HWREna & (1 << 0)))
1720 t0 = env->CP0_EBase & 0x3ff;
1722 do_raise_exception(EXCP_RI);
1727 target_ulong do_rdhwr_synci_step(target_ulong t0)
1729 if ((env->hflags & MIPS_HFLAG_CP0) ||
1730 (env->CP0_HWREna & (1 << 1)))
1731 t0 = env->SYNCI_Step;
1733 do_raise_exception(EXCP_RI);
1738 target_ulong do_rdhwr_cc(target_ulong t0)
1740 if ((env->hflags & MIPS_HFLAG_CP0) ||
1741 (env->CP0_HWREna & (1 << 2)))
1742 t0 = env->CP0_Count;
1744 do_raise_exception(EXCP_RI);
1749 target_ulong do_rdhwr_ccres(target_ulong t0)
1751 if ((env->hflags & MIPS_HFLAG_CP0) ||
1752 (env->CP0_HWREna & (1 << 3)))
1755 do_raise_exception(EXCP_RI);
1760 /* Bitfield operations. */
1761 target_ulong do_ext(target_ulong t0, target_ulong t1, uint32_t pos, uint32_t size)
1763 return (int32_t)((t1 >> pos) & ((size < 32) ? ((1 << size) - 1) : ~0));
1766 target_ulong do_ins(target_ulong t0, target_ulong t1, uint32_t pos, uint32_t size)
1768 target_ulong mask = ((size < 32) ? ((1 << size) - 1) : ~0) << pos;
1770 return (int32_t)((t0 & ~mask) | ((t1 << pos) & mask));
1773 target_ulong do_wsbh(target_ulong t0, target_ulong t1)
1775 return (int32_t)(((t1 << 8) & ~0x00FF00FF) | ((t1 >> 8) & 0x00FF00FF));
1778 #if defined(TARGET_MIPS64)
1779 target_ulong do_dext(target_ulong t0, target_ulong t1, uint32_t pos, uint32_t size)
1781 return (t1 >> pos) & ((size < 64) ? ((1ULL << size) - 1) : ~0ULL);
1784 target_ulong do_dins(target_ulong t0, target_ulong t1, uint32_t pos, uint32_t size)
1786 target_ulong mask = ((size < 64) ? ((1ULL << size) - 1) : ~0ULL) << pos;
1788 return (t0 & ~mask) | ((t1 << pos) & mask);
1791 target_ulong do_dsbh(target_ulong t0, target_ulong t1)
1793 return ((t1 << 8) & ~0x00FF00FF00FF00FFULL) | ((t1 >> 8) & 0x00FF00FF00FF00FFULL);
1796 target_ulong do_dshd(target_ulong t0, target_ulong t1)
1798 t1 = ((t1 << 16) & ~0x0000FFFF0000FFFFULL) | ((t1 >> 16) & 0x0000FFFF0000FFFFULL);
1799 return (t1 << 32) | (t1 >> 32);
1803 void do_pmon (int function)
1807 case 2: /* TODO: char inbyte(int waitflag); */
1808 if (env->gpr[env->current_tc][4] == 0)
1809 env->gpr[env->current_tc][2] = -1;
1811 case 11: /* TODO: char inbyte (void); */
1812 env->gpr[env->current_tc][2] = -1;
1816 printf("%c", (char)(env->gpr[env->current_tc][4] & 0xFF));
1822 unsigned char *fmt = (void *)(unsigned long)env->gpr[env->current_tc][4];
1832 do_raise_exception(EXCP_HLT);
1835 #if !defined(CONFIG_USER_ONLY)
1837 static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);
1839 #define MMUSUFFIX _mmu
1840 #define ALIGNED_ONLY
1843 #include "softmmu_template.h"
1846 #include "softmmu_template.h"
1849 #include "softmmu_template.h"
1852 #include "softmmu_template.h"
1854 static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr)
1856 env->CP0_BadVAddr = addr;
1857 do_restore_state (retaddr);
1858 do_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
1861 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1863 TranslationBlock *tb;
1864 CPUState *saved_env;
1868 /* XXX: hack to restore env in all cases, even if not called from
1871 env = cpu_single_env;
1872 ret = cpu_mips_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1875 /* now we have a real cpu fault */
1876 pc = (unsigned long)retaddr;
1877 tb = tb_find_pc(pc);
1879 /* the PC is inside the translated code. It means that we have
1880 a virtual CPU fault */
1881 cpu_restore_state(tb, env, pc, NULL);
1884 do_raise_exception_err(env->exception_index, env->error_code);
1889 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
1893 do_raise_exception(EXCP_IBE);
1895 do_raise_exception(EXCP_DBE);
1897 #endif /* !CONFIG_USER_ONLY */
1899 /* Complex FPU operations which may need stack space. */
1901 #define FLOAT_ONE32 make_float32(0x3f8 << 20)
1902 #define FLOAT_ONE64 make_float64(0x3ffULL << 52)
1903 #define FLOAT_TWO32 make_float32(1 << 30)
1904 #define FLOAT_TWO64 make_float64(1ULL << 62)
1905 #define FLOAT_QNAN32 0x7fbfffff
1906 #define FLOAT_QNAN64 0x7ff7ffffffffffffULL
1907 #define FLOAT_SNAN32 0x7fffffff
1908 #define FLOAT_SNAN64 0x7fffffffffffffffULL
1910 /* convert MIPS rounding mode in FCR31 to IEEE library */
1911 unsigned int ieee_rm[] = {
1912 float_round_nearest_even,
1913 float_round_to_zero,
1918 #define RESTORE_ROUNDING_MODE \
1919 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
1921 target_ulong do_cfc1 (target_ulong t0, uint32_t reg)
1925 t0 = (int32_t)env->fpu->fcr0;
1928 t0 = ((env->fpu->fcr31 >> 24) & 0xfe) | ((env->fpu->fcr31 >> 23) & 0x1);
1931 t0 = env->fpu->fcr31 & 0x0003f07c;
1934 t0 = (env->fpu->fcr31 & 0x00000f83) | ((env->fpu->fcr31 >> 22) & 0x4);
1937 t0 = (int32_t)env->fpu->fcr31;
1944 void do_ctc1 (target_ulong t0, uint32_t reg)
1948 if (t0 & 0xffffff00)
1950 env->fpu->fcr31 = (env->fpu->fcr31 & 0x017fffff) | ((t0 & 0xfe) << 24) |
1954 if (t0 & 0x007c0000)
1956 env->fpu->fcr31 = (env->fpu->fcr31 & 0xfffc0f83) | (t0 & 0x0003f07c);
1959 if (t0 & 0x007c0000)
1961 env->fpu->fcr31 = (env->fpu->fcr31 & 0xfefff07c) | (t0 & 0x00000f83) |
1965 if (t0 & 0x007c0000)
1967 env->fpu->fcr31 = t0;
1972 /* set rounding mode */
1973 RESTORE_ROUNDING_MODE;
1974 set_float_exception_flags(0, &env->fpu->fp_status);
1975 if ((GET_FP_ENABLE(env->fpu->fcr31) | 0x20) & GET_FP_CAUSE(env->fpu->fcr31))
1976 do_raise_exception(EXCP_FPE);
1979 static always_inline char ieee_ex_to_mips(char xcpt)
1981 return (xcpt & float_flag_inexact) >> 5 |
1982 (xcpt & float_flag_underflow) >> 3 |
1983 (xcpt & float_flag_overflow) >> 1 |
1984 (xcpt & float_flag_divbyzero) << 1 |
1985 (xcpt & float_flag_invalid) << 4;
1988 static always_inline char mips_ex_to_ieee(char xcpt)
1990 return (xcpt & FP_INEXACT) << 5 |
1991 (xcpt & FP_UNDERFLOW) << 3 |
1992 (xcpt & FP_OVERFLOW) << 1 |
1993 (xcpt & FP_DIV0) >> 1 |
1994 (xcpt & FP_INVALID) >> 4;
1997 static always_inline void update_fcr31(void)
1999 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->fpu->fp_status));
2001 SET_FP_CAUSE(env->fpu->fcr31, tmp);
2002 if (GET_FP_ENABLE(env->fpu->fcr31) & tmp)
2003 do_raise_exception(EXCP_FPE);
2005 UPDATE_FP_FLAGS(env->fpu->fcr31, tmp);
2009 Single precition routines have a "s" suffix, double precision a
2010 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
2011 paired single lower "pl", paired single upper "pu". */
2013 #define FLOAT_OP(name, p) void do_float_##name##_##p(void)
2015 /* unary operations, modifying fp status */
2016 #define FLOAT_UNOP(name) \
2019 FDT2 = float64_ ## name(FDT0, &env->fpu->fp_status); \
2023 FST2 = float32_ ## name(FST0, &env->fpu->fp_status); \
2030 set_float_exception_flags(0, &env->fpu->fp_status);
2031 FDT2 = float32_to_float64(FST0, &env->fpu->fp_status);
2036 set_float_exception_flags(0, &env->fpu->fp_status);
2037 FDT2 = int32_to_float64(WT0, &env->fpu->fp_status);
2042 set_float_exception_flags(0, &env->fpu->fp_status);
2043 FDT2 = int64_to_float64(DT0, &env->fpu->fp_status);
2048 set_float_exception_flags(0, &env->fpu->fp_status);
2049 DT2 = float64_to_int64(FDT0, &env->fpu->fp_status);
2051 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2056 set_float_exception_flags(0, &env->fpu->fp_status);
2057 DT2 = float32_to_int64(FST0, &env->fpu->fp_status);
2059 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2065 set_float_exception_flags(0, &env->fpu->fp_status);
2066 FST2 = int32_to_float32(WT0, &env->fpu->fp_status);
2067 FSTH2 = int32_to_float32(WTH0, &env->fpu->fp_status);
2072 set_float_exception_flags(0, &env->fpu->fp_status);
2073 WT2 = float32_to_int32(FST0, &env->fpu->fp_status);
2074 WTH2 = float32_to_int32(FSTH0, &env->fpu->fp_status);
2076 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2081 set_float_exception_flags(0, &env->fpu->fp_status);
2082 FST2 = float64_to_float32(FDT0, &env->fpu->fp_status);
2087 set_float_exception_flags(0, &env->fpu->fp_status);
2088 FST2 = int32_to_float32(WT0, &env->fpu->fp_status);
2093 set_float_exception_flags(0, &env->fpu->fp_status);
2094 FST2 = int64_to_float32(DT0, &env->fpu->fp_status);
2099 set_float_exception_flags(0, &env->fpu->fp_status);
2105 set_float_exception_flags(0, &env->fpu->fp_status);
2111 set_float_exception_flags(0, &env->fpu->fp_status);
2112 WT2 = float32_to_int32(FST0, &env->fpu->fp_status);
2114 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2119 set_float_exception_flags(0, &env->fpu->fp_status);
2120 WT2 = float64_to_int32(FDT0, &env->fpu->fp_status);
2122 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2128 set_float_rounding_mode(float_round_nearest_even, &env->fpu->fp_status);
2129 DT2 = float64_to_int64(FDT0, &env->fpu->fp_status);
2130 RESTORE_ROUNDING_MODE;
2132 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2137 set_float_rounding_mode(float_round_nearest_even, &env->fpu->fp_status);
2138 DT2 = float32_to_int64(FST0, &env->fpu->fp_status);
2139 RESTORE_ROUNDING_MODE;
2141 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2146 set_float_rounding_mode(float_round_nearest_even, &env->fpu->fp_status);
2147 WT2 = float64_to_int32(FDT0, &env->fpu->fp_status);
2148 RESTORE_ROUNDING_MODE;
2150 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2155 set_float_rounding_mode(float_round_nearest_even, &env->fpu->fp_status);
2156 WT2 = float32_to_int32(FST0, &env->fpu->fp_status);
2157 RESTORE_ROUNDING_MODE;
2159 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2165 DT2 = float64_to_int64_round_to_zero(FDT0, &env->fpu->fp_status);
2167 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2172 DT2 = float32_to_int64_round_to_zero(FST0, &env->fpu->fp_status);
2174 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2179 WT2 = float64_to_int32_round_to_zero(FDT0, &env->fpu->fp_status);
2181 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2186 WT2 = float32_to_int32_round_to_zero(FST0, &env->fpu->fp_status);
2188 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2194 set_float_rounding_mode(float_round_up, &env->fpu->fp_status);
2195 DT2 = float64_to_int64(FDT0, &env->fpu->fp_status);
2196 RESTORE_ROUNDING_MODE;
2198 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2203 set_float_rounding_mode(float_round_up, &env->fpu->fp_status);
2204 DT2 = float32_to_int64(FST0, &env->fpu->fp_status);
2205 RESTORE_ROUNDING_MODE;
2207 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2212 set_float_rounding_mode(float_round_up, &env->fpu->fp_status);
2213 WT2 = float64_to_int32(FDT0, &env->fpu->fp_status);
2214 RESTORE_ROUNDING_MODE;
2216 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2221 set_float_rounding_mode(float_round_up, &env->fpu->fp_status);
2222 WT2 = float32_to_int32(FST0, &env->fpu->fp_status);
2223 RESTORE_ROUNDING_MODE;
2225 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2231 set_float_rounding_mode(float_round_down, &env->fpu->fp_status);
2232 DT2 = float64_to_int64(FDT0, &env->fpu->fp_status);
2233 RESTORE_ROUNDING_MODE;
2235 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2240 set_float_rounding_mode(float_round_down, &env->fpu->fp_status);
2241 DT2 = float32_to_int64(FST0, &env->fpu->fp_status);
2242 RESTORE_ROUNDING_MODE;
2244 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2249 set_float_rounding_mode(float_round_down, &env->fpu->fp_status);
2250 WT2 = float64_to_int32(FDT0, &env->fpu->fp_status);
2251 RESTORE_ROUNDING_MODE;
2253 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2258 set_float_rounding_mode(float_round_down, &env->fpu->fp_status);
2259 WT2 = float32_to_int32(FST0, &env->fpu->fp_status);
2260 RESTORE_ROUNDING_MODE;
2262 if (GET_FP_CAUSE(env->fpu->fcr31) & (FP_OVERFLOW | FP_INVALID))
2266 /* unary operations, not modifying fp status */
2267 #define FLOAT_UNOP(name) \
2270 FDT2 = float64_ ## name(FDT0); \
2274 FST2 = float32_ ## name(FST0); \
2276 FLOAT_OP(name, ps) \
2278 FST2 = float32_ ## name(FST0); \
2279 FSTH2 = float32_ ## name(FSTH0); \
2285 /* MIPS specific unary operations */
2288 set_float_exception_flags(0, &env->fpu->fp_status);
2289 FDT2 = float64_div(FLOAT_ONE64, FDT0, &env->fpu->fp_status);
2294 set_float_exception_flags(0, &env->fpu->fp_status);
2295 FST2 = float32_div(FLOAT_ONE32, FST0, &env->fpu->fp_status);
2301 set_float_exception_flags(0, &env->fpu->fp_status);
2302 FDT2 = float64_sqrt(FDT0, &env->fpu->fp_status);
2303 FDT2 = float64_div(FLOAT_ONE64, FDT2, &env->fpu->fp_status);
2308 set_float_exception_flags(0, &env->fpu->fp_status);
2309 FST2 = float32_sqrt(FST0, &env->fpu->fp_status);
2310 FST2 = float32_div(FLOAT_ONE32, FST2, &env->fpu->fp_status);
2316 set_float_exception_flags(0, &env->fpu->fp_status);
2317 FDT2 = float64_div(FLOAT_ONE64, FDT0, &env->fpu->fp_status);
2322 set_float_exception_flags(0, &env->fpu->fp_status);
2323 FST2 = float32_div(FLOAT_ONE32, FST0, &env->fpu->fp_status);
2326 FLOAT_OP(recip1, ps)
2328 set_float_exception_flags(0, &env->fpu->fp_status);
2329 FST2 = float32_div(FLOAT_ONE32, FST0, &env->fpu->fp_status);
2330 FSTH2 = float32_div(FLOAT_ONE32, FSTH0, &env->fpu->fp_status);
2336 set_float_exception_flags(0, &env->fpu->fp_status);
2337 FDT2 = float64_sqrt(FDT0, &env->fpu->fp_status);
2338 FDT2 = float64_div(FLOAT_ONE64, FDT2, &env->fpu->fp_status);
2343 set_float_exception_flags(0, &env->fpu->fp_status);
2344 FST2 = float32_sqrt(FST0, &env->fpu->fp_status);
2345 FST2 = float32_div(FLOAT_ONE32, FST2, &env->fpu->fp_status);
2348 FLOAT_OP(rsqrt1, ps)
2350 set_float_exception_flags(0, &env->fpu->fp_status);
2351 FST2 = float32_sqrt(FST0, &env->fpu->fp_status);
2352 FSTH2 = float32_sqrt(FSTH0, &env->fpu->fp_status);
2353 FST2 = float32_div(FLOAT_ONE32, FST2, &env->fpu->fp_status);
2354 FSTH2 = float32_div(FLOAT_ONE32, FSTH2, &env->fpu->fp_status);
2358 /* binary operations */
2359 #define FLOAT_BINOP(name) \
2362 set_float_exception_flags(0, &env->fpu->fp_status); \
2363 FDT2 = float64_ ## name (FDT0, FDT1, &env->fpu->fp_status); \
2365 if (GET_FP_CAUSE(env->fpu->fcr31) & FP_INVALID) \
2366 DT2 = FLOAT_QNAN64; \
2370 set_float_exception_flags(0, &env->fpu->fp_status); \
2371 FST2 = float32_ ## name (FST0, FST1, &env->fpu->fp_status); \
2373 if (GET_FP_CAUSE(env->fpu->fcr31) & FP_INVALID) \
2374 WT2 = FLOAT_QNAN32; \
2376 FLOAT_OP(name, ps) \
2378 set_float_exception_flags(0, &env->fpu->fp_status); \
2379 FST2 = float32_ ## name (FST0, FST1, &env->fpu->fp_status); \
2380 FSTH2 = float32_ ## name (FSTH0, FSTH1, &env->fpu->fp_status); \
2382 if (GET_FP_CAUSE(env->fpu->fcr31) & FP_INVALID) { \
2383 WT2 = FLOAT_QNAN32; \
2384 WTH2 = FLOAT_QNAN32; \
2393 /* ternary operations */
2394 #define FLOAT_TERNOP(name1, name2) \
2395 FLOAT_OP(name1 ## name2, d) \
2397 FDT0 = float64_ ## name1 (FDT0, FDT1, &env->fpu->fp_status); \
2398 FDT2 = float64_ ## name2 (FDT0, FDT2, &env->fpu->fp_status); \
2400 FLOAT_OP(name1 ## name2, s) \
2402 FST0 = float32_ ## name1 (FST0, FST1, &env->fpu->fp_status); \
2403 FST2 = float32_ ## name2 (FST0, FST2, &env->fpu->fp_status); \
2405 FLOAT_OP(name1 ## name2, ps) \
2407 FST0 = float32_ ## name1 (FST0, FST1, &env->fpu->fp_status); \
2408 FSTH0 = float32_ ## name1 (FSTH0, FSTH1, &env->fpu->fp_status); \
2409 FST2 = float32_ ## name2 (FST0, FST2, &env->fpu->fp_status); \
2410 FSTH2 = float32_ ## name2 (FSTH0, FSTH2, &env->fpu->fp_status); \
2412 FLOAT_TERNOP(mul, add)
2413 FLOAT_TERNOP(mul, sub)
2416 /* negated ternary operations */
2417 #define FLOAT_NTERNOP(name1, name2) \
2418 FLOAT_OP(n ## name1 ## name2, d) \
2420 FDT0 = float64_ ## name1 (FDT0, FDT1, &env->fpu->fp_status); \
2421 FDT2 = float64_ ## name2 (FDT0, FDT2, &env->fpu->fp_status); \
2422 FDT2 = float64_chs(FDT2); \
2424 FLOAT_OP(n ## name1 ## name2, s) \
2426 FST0 = float32_ ## name1 (FST0, FST1, &env->fpu->fp_status); \
2427 FST2 = float32_ ## name2 (FST0, FST2, &env->fpu->fp_status); \
2428 FST2 = float32_chs(FST2); \
2430 FLOAT_OP(n ## name1 ## name2, ps) \
2432 FST0 = float32_ ## name1 (FST0, FST1, &env->fpu->fp_status); \
2433 FSTH0 = float32_ ## name1 (FSTH0, FSTH1, &env->fpu->fp_status); \
2434 FST2 = float32_ ## name2 (FST0, FST2, &env->fpu->fp_status); \
2435 FSTH2 = float32_ ## name2 (FSTH0, FSTH2, &env->fpu->fp_status); \
2436 FST2 = float32_chs(FST2); \
2437 FSTH2 = float32_chs(FSTH2); \
2439 FLOAT_NTERNOP(mul, add)
2440 FLOAT_NTERNOP(mul, sub)
2441 #undef FLOAT_NTERNOP
2443 /* MIPS specific binary operations */
2446 set_float_exception_flags(0, &env->fpu->fp_status);
2447 FDT2 = float64_mul(FDT0, FDT2, &env->fpu->fp_status);
2448 FDT2 = float64_chs(float64_sub(FDT2, FLOAT_ONE64, &env->fpu->fp_status));
2453 set_float_exception_flags(0, &env->fpu->fp_status);
2454 FST2 = float32_mul(FST0, FST2, &env->fpu->fp_status);
2455 FST2 = float32_chs(float32_sub(FST2, FLOAT_ONE32, &env->fpu->fp_status));
2458 FLOAT_OP(recip2, ps)
2460 set_float_exception_flags(0, &env->fpu->fp_status);
2461 FST2 = float32_mul(FST0, FST2, &env->fpu->fp_status);
2462 FSTH2 = float32_mul(FSTH0, FSTH2, &env->fpu->fp_status);
2463 FST2 = float32_chs(float32_sub(FST2, FLOAT_ONE32, &env->fpu->fp_status));
2464 FSTH2 = float32_chs(float32_sub(FSTH2, FLOAT_ONE32, &env->fpu->fp_status));
2470 set_float_exception_flags(0, &env->fpu->fp_status);
2471 FDT2 = float64_mul(FDT0, FDT2, &env->fpu->fp_status);
2472 FDT2 = float64_sub(FDT2, FLOAT_ONE64, &env->fpu->fp_status);
2473 FDT2 = float64_chs(float64_div(FDT2, FLOAT_TWO64, &env->fpu->fp_status));
2478 set_float_exception_flags(0, &env->fpu->fp_status);
2479 FST2 = float32_mul(FST0, FST2, &env->fpu->fp_status);
2480 FST2 = float32_sub(FST2, FLOAT_ONE32, &env->fpu->fp_status);
2481 FST2 = float32_chs(float32_div(FST2, FLOAT_TWO32, &env->fpu->fp_status));
2484 FLOAT_OP(rsqrt2, ps)
2486 set_float_exception_flags(0, &env->fpu->fp_status);
2487 FST2 = float32_mul(FST0, FST2, &env->fpu->fp_status);
2488 FSTH2 = float32_mul(FSTH0, FSTH2, &env->fpu->fp_status);
2489 FST2 = float32_sub(FST2, FLOAT_ONE32, &env->fpu->fp_status);
2490 FSTH2 = float32_sub(FSTH2, FLOAT_ONE32, &env->fpu->fp_status);
2491 FST2 = float32_chs(float32_div(FST2, FLOAT_TWO32, &env->fpu->fp_status));
2492 FSTH2 = float32_chs(float32_div(FSTH2, FLOAT_TWO32, &env->fpu->fp_status));
2498 set_float_exception_flags(0, &env->fpu->fp_status);
2499 FST2 = float32_add (FST0, FSTH0, &env->fpu->fp_status);
2500 FSTH2 = float32_add (FST1, FSTH1, &env->fpu->fp_status);
2506 set_float_exception_flags(0, &env->fpu->fp_status);
2507 FST2 = float32_mul (FST0, FSTH0, &env->fpu->fp_status);
2508 FSTH2 = float32_mul (FST1, FSTH1, &env->fpu->fp_status);
2512 /* compare operations */
2513 #define FOP_COND_D(op, cond) \
2514 void do_cmp_d_ ## op (long cc) \
2519 SET_FP_COND(cc, env->fpu); \
2521 CLEAR_FP_COND(cc, env->fpu); \
2523 void do_cmpabs_d_ ## op (long cc) \
2526 FDT0 = float64_abs(FDT0); \
2527 FDT1 = float64_abs(FDT1); \
2531 SET_FP_COND(cc, env->fpu); \
2533 CLEAR_FP_COND(cc, env->fpu); \
2536 int float64_is_unordered(int sig, float64 a, float64 b STATUS_PARAM)
2538 if (float64_is_signaling_nan(a) ||
2539 float64_is_signaling_nan(b) ||
2540 (sig && (float64_is_nan(a) || float64_is_nan(b)))) {
2541 float_raise(float_flag_invalid, status);
2543 } else if (float64_is_nan(a) || float64_is_nan(b)) {
2550 /* NOTE: the comma operator will make "cond" to eval to false,
2551 * but float*_is_unordered() is still called. */
2552 FOP_COND_D(f, (float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status), 0))
2553 FOP_COND_D(un, float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status))
2554 FOP_COND_D(eq, !float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status) && float64_eq(FDT0, FDT1, &env->fpu->fp_status))
2555 FOP_COND_D(ueq, float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status) || float64_eq(FDT0, FDT1, &env->fpu->fp_status))
2556 FOP_COND_D(olt, !float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status) && float64_lt(FDT0, FDT1, &env->fpu->fp_status))
2557 FOP_COND_D(ult, float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status) || float64_lt(FDT0, FDT1, &env->fpu->fp_status))
2558 FOP_COND_D(ole, !float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status) && float64_le(FDT0, FDT1, &env->fpu->fp_status))
2559 FOP_COND_D(ule, float64_is_unordered(0, FDT1, FDT0, &env->fpu->fp_status) || float64_le(FDT0, FDT1, &env->fpu->fp_status))
2560 /* NOTE: the comma operator will make "cond" to eval to false,
2561 * but float*_is_unordered() is still called. */
2562 FOP_COND_D(sf, (float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status), 0))
2563 FOP_COND_D(ngle,float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status))
2564 FOP_COND_D(seq, !float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status) && float64_eq(FDT0, FDT1, &env->fpu->fp_status))
2565 FOP_COND_D(ngl, float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status) || float64_eq(FDT0, FDT1, &env->fpu->fp_status))
2566 FOP_COND_D(lt, !float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status) && float64_lt(FDT0, FDT1, &env->fpu->fp_status))
2567 FOP_COND_D(nge, float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status) || float64_lt(FDT0, FDT1, &env->fpu->fp_status))
2568 FOP_COND_D(le, !float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status) && float64_le(FDT0, FDT1, &env->fpu->fp_status))
2569 FOP_COND_D(ngt, float64_is_unordered(1, FDT1, FDT0, &env->fpu->fp_status) || float64_le(FDT0, FDT1, &env->fpu->fp_status))
2571 #define FOP_COND_S(op, cond) \
2572 void do_cmp_s_ ## op (long cc) \
2577 SET_FP_COND(cc, env->fpu); \
2579 CLEAR_FP_COND(cc, env->fpu); \
2581 void do_cmpabs_s_ ## op (long cc) \
2584 FST0 = float32_abs(FST0); \
2585 FST1 = float32_abs(FST1); \
2589 SET_FP_COND(cc, env->fpu); \
2591 CLEAR_FP_COND(cc, env->fpu); \
2594 flag float32_is_unordered(int sig, float32 a, float32 b STATUS_PARAM)
2596 if (float32_is_signaling_nan(a) ||
2597 float32_is_signaling_nan(b) ||
2598 (sig && (float32_is_nan(a) || float32_is_nan(b)))) {
2599 float_raise(float_flag_invalid, status);
2601 } else if (float32_is_nan(a) || float32_is_nan(b)) {
2608 /* NOTE: the comma operator will make "cond" to eval to false,
2609 * but float*_is_unordered() is still called. */
2610 FOP_COND_S(f, (float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status), 0))
2611 FOP_COND_S(un, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status))
2612 FOP_COND_S(eq, !float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) && float32_eq(FST0, FST1, &env->fpu->fp_status))
2613 FOP_COND_S(ueq, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) || float32_eq(FST0, FST1, &env->fpu->fp_status))
2614 FOP_COND_S(olt, !float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) && float32_lt(FST0, FST1, &env->fpu->fp_status))
2615 FOP_COND_S(ult, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) || float32_lt(FST0, FST1, &env->fpu->fp_status))
2616 FOP_COND_S(ole, !float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) && float32_le(FST0, FST1, &env->fpu->fp_status))
2617 FOP_COND_S(ule, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) || float32_le(FST0, FST1, &env->fpu->fp_status))
2618 /* NOTE: the comma operator will make "cond" to eval to false,
2619 * but float*_is_unordered() is still called. */
2620 FOP_COND_S(sf, (float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status), 0))
2621 FOP_COND_S(ngle,float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status))
2622 FOP_COND_S(seq, !float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) && float32_eq(FST0, FST1, &env->fpu->fp_status))
2623 FOP_COND_S(ngl, float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) || float32_eq(FST0, FST1, &env->fpu->fp_status))
2624 FOP_COND_S(lt, !float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) && float32_lt(FST0, FST1, &env->fpu->fp_status))
2625 FOP_COND_S(nge, float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) || float32_lt(FST0, FST1, &env->fpu->fp_status))
2626 FOP_COND_S(le, !float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) && float32_le(FST0, FST1, &env->fpu->fp_status))
2627 FOP_COND_S(ngt, float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) || float32_le(FST0, FST1, &env->fpu->fp_status))
2629 #define FOP_COND_PS(op, condl, condh) \
2630 void do_cmp_ps_ ## op (long cc) \
2636 SET_FP_COND(cc, env->fpu); \
2638 CLEAR_FP_COND(cc, env->fpu); \
2640 SET_FP_COND(cc + 1, env->fpu); \
2642 CLEAR_FP_COND(cc + 1, env->fpu); \
2644 void do_cmpabs_ps_ ## op (long cc) \
2647 FST0 = float32_abs(FST0); \
2648 FSTH0 = float32_abs(FSTH0); \
2649 FST1 = float32_abs(FST1); \
2650 FSTH1 = float32_abs(FSTH1); \
2655 SET_FP_COND(cc, env->fpu); \
2657 CLEAR_FP_COND(cc, env->fpu); \
2659 SET_FP_COND(cc + 1, env->fpu); \
2661 CLEAR_FP_COND(cc + 1, env->fpu); \
2664 /* NOTE: the comma operator will make "cond" to eval to false,
2665 * but float*_is_unordered() is still called. */
2666 FOP_COND_PS(f, (float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status), 0),
2667 (float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status), 0))
2668 FOP_COND_PS(un, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status),
2669 float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status))
2670 FOP_COND_PS(eq, !float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) && float32_eq(FST0, FST1, &env->fpu->fp_status),
2671 !float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status) && float32_eq(FSTH0, FSTH1, &env->fpu->fp_status))
2672 FOP_COND_PS(ueq, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) || float32_eq(FST0, FST1, &env->fpu->fp_status),
2673 float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status) || float32_eq(FSTH0, FSTH1, &env->fpu->fp_status))
2674 FOP_COND_PS(olt, !float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) && float32_lt(FST0, FST1, &env->fpu->fp_status),
2675 !float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status) && float32_lt(FSTH0, FSTH1, &env->fpu->fp_status))
2676 FOP_COND_PS(ult, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) || float32_lt(FST0, FST1, &env->fpu->fp_status),
2677 float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status) || float32_lt(FSTH0, FSTH1, &env->fpu->fp_status))
2678 FOP_COND_PS(ole, !float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) && float32_le(FST0, FST1, &env->fpu->fp_status),
2679 !float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status) && float32_le(FSTH0, FSTH1, &env->fpu->fp_status))
2680 FOP_COND_PS(ule, float32_is_unordered(0, FST1, FST0, &env->fpu->fp_status) || float32_le(FST0, FST1, &env->fpu->fp_status),
2681 float32_is_unordered(0, FSTH1, FSTH0, &env->fpu->fp_status) || float32_le(FSTH0, FSTH1, &env->fpu->fp_status))
2682 /* NOTE: the comma operator will make "cond" to eval to false,
2683 * but float*_is_unordered() is still called. */
2684 FOP_COND_PS(sf, (float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status), 0),
2685 (float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status), 0))
2686 FOP_COND_PS(ngle,float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status),
2687 float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status))
2688 FOP_COND_PS(seq, !float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) && float32_eq(FST0, FST1, &env->fpu->fp_status),
2689 !float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status) && float32_eq(FSTH0, FSTH1, &env->fpu->fp_status))
2690 FOP_COND_PS(ngl, float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) || float32_eq(FST0, FST1, &env->fpu->fp_status),
2691 float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status) || float32_eq(FSTH0, FSTH1, &env->fpu->fp_status))
2692 FOP_COND_PS(lt, !float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) && float32_lt(FST0, FST1, &env->fpu->fp_status),
2693 !float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status) && float32_lt(FSTH0, FSTH1, &env->fpu->fp_status))
2694 FOP_COND_PS(nge, float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) || float32_lt(FST0, FST1, &env->fpu->fp_status),
2695 float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status) || float32_lt(FSTH0, FSTH1, &env->fpu->fp_status))
2696 FOP_COND_PS(le, !float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) && float32_le(FST0, FST1, &env->fpu->fp_status),
2697 !float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status) && float32_le(FSTH0, FSTH1, &env->fpu->fp_status))
2698 FOP_COND_PS(ngt, float32_is_unordered(1, FST1, FST0, &env->fpu->fp_status) || float32_le(FST0, FST1, &env->fpu->fp_status),
2699 float32_is_unordered(1, FSTH1, FSTH0, &env->fpu->fp_status) || float32_le(FSTH0, FSTH1, &env->fpu->fp_status))