2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 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
21 #include "host-utils.h"
23 #include "helper_regs.h"
24 #include "op_helper.h"
26 #define MEMSUFFIX _raw
27 #include "op_helper.h"
28 #include "op_helper_mem.h"
29 #if !defined(CONFIG_USER_ONLY)
30 #define MEMSUFFIX _user
31 #include "op_helper.h"
32 #include "op_helper_mem.h"
33 #define MEMSUFFIX _kernel
34 #include "op_helper.h"
35 #include "op_helper_mem.h"
36 #define MEMSUFFIX _hypv
37 #include "op_helper.h"
38 #include "op_helper_mem.h"
42 //#define DEBUG_EXCEPTIONS
43 //#define DEBUG_SOFTWARE_TLB
45 /*****************************************************************************/
46 /* Exceptions processing helpers */
48 void do_raise_exception_err (uint32_t exception
, int error_code
)
51 printf("Raise exception %3x code : %d\n", exception
, error_code
);
53 env
->exception_index
= exception
;
54 env
->error_code
= error_code
;
58 void do_raise_exception (uint32_t exception
)
60 do_raise_exception_err(exception
, 0);
63 void cpu_dump_EA (target_ulong EA
);
64 void do_print_mem_EA (target_ulong EA
)
69 /*****************************************************************************/
70 /* Registers load and stores */
71 void do_load_cr (void)
73 T0
= (env
->crf
[0] << 28) |
83 void do_store_cr (uint32_t mask
)
87 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
89 env
->crf
[i
] = (T0
>> (sh
* 4)) & 0xFUL
;
93 #if defined(TARGET_PPC64)
94 void do_store_pri (int prio
)
96 env
->spr
[SPR_PPR
] &= ~0x001C000000000000ULL
;
97 env
->spr
[SPR_PPR
] |= ((uint64_t)prio
& 0x7) << 50;
101 target_ulong
ppc_load_dump_spr (int sprn
)
104 fprintf(logfile
, "Read SPR %d %03x => " ADDRX
"\n",
105 sprn
, sprn
, env
->spr
[sprn
]);
108 return env
->spr
[sprn
];
111 void ppc_store_dump_spr (int sprn
, target_ulong val
)
114 fprintf(logfile
, "Write SPR %d %03x => " ADDRX
" <= " ADDRX
"\n",
115 sprn
, sprn
, env
->spr
[sprn
], val
);
117 env
->spr
[sprn
] = val
;
120 /*****************************************************************************/
121 /* Fixed point operations helpers */
126 if (likely(!((uint32_t)T0
< (uint32_t)T2
||
127 (xer_ca
== 1 && (uint32_t)T0
== (uint32_t)T2
)))) {
134 #if defined(TARGET_PPC64)
135 void do_adde_64 (void)
139 if (likely(!((uint64_t)T0
< (uint64_t)T2
||
140 (xer_ca
== 1 && (uint64_t)T0
== (uint64_t)T2
)))) {
148 void do_addmeo (void)
152 xer_ov
= ((uint32_t)T1
& ((uint32_t)T1
^ (uint32_t)T0
)) >> 31;
160 #if defined(TARGET_PPC64)
161 void do_addmeo_64 (void)
165 xer_ov
= ((uint64_t)T1
& ((uint64_t)T1
^ (uint64_t)T0
)) >> 63;
176 if (likely(!(((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
177 (int32_t)T1
== 0))) {
179 T0
= (int32_t)T0
/ (int32_t)T1
;
182 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
187 #if defined(TARGET_PPC64)
190 if (likely(!(((int64_t)T0
== INT64_MIN
&& (int64_t)T1
== (int64_t)-1LL) ||
191 (int64_t)T1
== 0))) {
193 T0
= (int64_t)T0
/ (int64_t)T1
;
196 T0
= UINT64_MAX
* ((uint64_t)T0
>> 63);
202 void do_divwuo (void)
204 if (likely((uint32_t)T1
!= 0)) {
206 T0
= (uint32_t)T0
/ (uint32_t)T1
;
214 #if defined(TARGET_PPC64)
215 void do_divduo (void)
217 if (likely((uint64_t)T1
!= 0)) {
219 T0
= (uint64_t)T0
/ (uint64_t)T1
;
228 void do_mullwo (void)
230 int64_t res
= (int64_t)T0
* (int64_t)T1
;
232 if (likely((int32_t)res
== res
)) {
241 #if defined(TARGET_PPC64)
242 void do_mulldo (void)
247 muls64(&tl
, &th
, T0
, T1
);
249 /* If th != 0 && th != -1, then we had an overflow */
250 if (likely((uint64_t)(th
+ 1) <= 1)) {
261 if (likely((int32_t)T0
!= INT32_MIN
)) {
270 #if defined(TARGET_PPC64)
271 void do_nego_64 (void)
273 if (likely((int64_t)T0
!= INT64_MIN
)) {
285 T0
= T1
+ ~T0
+ xer_ca
;
286 if (likely((uint32_t)T0
>= (uint32_t)T1
&&
287 (xer_ca
== 0 || (uint32_t)T0
!= (uint32_t)T1
))) {
294 #if defined(TARGET_PPC64)
295 void do_subfe_64 (void)
297 T0
= T1
+ ~T0
+ xer_ca
;
298 if (likely((uint64_t)T0
>= (uint64_t)T1
&&
299 (xer_ca
== 0 || (uint64_t)T0
!= (uint64_t)T1
))) {
307 void do_subfmeo (void)
310 T0
= ~T0
+ xer_ca
- 1;
311 xer_ov
= ((uint32_t)~T1
& ((uint32_t)~T1
^ (uint32_t)T0
)) >> 31;
313 if (likely((uint32_t)T1
!= UINT32_MAX
))
319 #if defined(TARGET_PPC64)
320 void do_subfmeo_64 (void)
323 T0
= ~T0
+ xer_ca
- 1;
324 xer_ov
= ((uint64_t)~T1
& ((uint64_t)~T1
^ (uint64_t)T0
)) >> 63;
326 if (likely((uint64_t)T1
!= UINT64_MAX
))
333 void do_subfzeo (void)
337 xer_ov
= (((uint32_t)~T1
^ UINT32_MAX
) &
338 ((uint32_t)(~T1
) ^ (uint32_t)T0
)) >> 31;
340 if (likely((uint32_t)T0
>= (uint32_t)~T1
)) {
347 #if defined(TARGET_PPC64)
348 void do_subfzeo_64 (void)
352 xer_ov
= (((uint64_t)~T1
^ UINT64_MAX
) &
353 ((uint64_t)(~T1
) ^ (uint64_t)T0
)) >> 63;
355 if (likely((uint64_t)T0
>= (uint64_t)~T1
)) {
363 void do_cntlzw (void)
368 #if defined(TARGET_PPC64)
369 void do_cntlzd (void)
375 /* shift right arithmetic helper */
380 if (likely(!(T1
& 0x20UL
))) {
381 if (likely((uint32_t)T1
!= 0)) {
382 ret
= (int32_t)T0
>> (T1
& 0x1fUL
);
383 if (likely(ret
>= 0 || ((int32_t)T0
& ((1 << T1
) - 1)) == 0)) {
393 ret
= UINT32_MAX
* ((uint32_t)T0
>> 31);
394 if (likely(ret
>= 0 || ((uint32_t)T0
& ~0x80000000UL
) == 0)) {
403 #if defined(TARGET_PPC64)
408 if (likely(!(T1
& 0x40UL
))) {
409 if (likely((uint64_t)T1
!= 0)) {
410 ret
= (int64_t)T0
>> (T1
& 0x3FUL
);
411 if (likely(ret
>= 0 || ((int64_t)T0
& ((1 << T1
) - 1)) == 0)) {
421 ret
= UINT64_MAX
* ((uint64_t)T0
>> 63);
422 if (likely(ret
>= 0 || ((uint64_t)T0
& ~0x8000000000000000ULL
) == 0)) {
432 void do_popcntb (void)
438 for (i
= 0; i
< 32; i
+= 8)
439 ret
|= ctpop8((T0
>> i
) & 0xFF) << i
;
443 #if defined(TARGET_PPC64)
444 void do_popcntb_64 (void)
450 for (i
= 0; i
< 64; i
+= 8)
451 ret
|= ctpop8((T0
>> i
) & 0xFF) << i
;
456 /*****************************************************************************/
457 /* Floating point operations helpers */
458 static always_inline
int fpisneg (float64 f
)
467 return u
.u
>> 63 != 0;
470 static always_inline
int isden (float f
)
479 return ((u
.u
>> 52) & 0x7FF) == 0;
482 static always_inline
int iszero (float64 f
)
491 return (u
.u
& ~0x8000000000000000ULL
) == 0;
494 static always_inline
int isinfinity (float64 f
)
503 return ((u
.u
>> 52) & 0x7FF) == 0x7FF &&
504 (u
.u
& 0x000FFFFFFFFFFFFFULL
) == 0;
507 void do_compute_fprf (int set_fprf
)
511 isneg
= fpisneg(FT0
);
512 if (unlikely(float64_is_nan(FT0
))) {
513 if (float64_is_signaling_nan(FT0
)) {
514 /* Signaling NaN: flags are undefined */
520 } else if (unlikely(isinfinity(FT0
))) {
535 /* Denormalized numbers */
538 /* Normalized numbers */
549 /* We update FPSCR_FPRF */
550 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
551 env
->fpscr
|= T0
<< FPSCR_FPRF
;
553 /* We just need fpcc to update Rc1 */
557 /* Floating-point invalid operations exception */
558 static always_inline
void fload_invalid_op_excp (int op
)
563 if (op
& POWERPC_EXCP_FP_VXSNAN
) {
564 /* Operation on signaling NaN */
565 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
567 if (op
& POWERPC_EXCP_FP_VXSOFT
) {
568 /* Software-defined condition */
569 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
571 switch (op
& ~(POWERPC_EXCP_FP_VXSOFT
| POWERPC_EXCP_FP_VXSNAN
)) {
572 case POWERPC_EXCP_FP_VXISI
:
573 /* Magnitude subtraction of infinities */
574 env
->fpscr
|= 1 << FPSCR_VXISI
;
576 case POWERPC_EXCP_FP_VXIDI
:
577 /* Division of infinity by infinity */
578 env
->fpscr
|= 1 << FPSCR_VXIDI
;
580 case POWERPC_EXCP_FP_VXZDZ
:
581 /* Division of zero by zero */
582 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
584 case POWERPC_EXCP_FP_VXIMZ
:
585 /* Multiplication of zero by infinity */
586 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
588 case POWERPC_EXCP_FP_VXVC
:
589 /* Ordered comparison of NaN */
590 env
->fpscr
|= 1 << FPSCR_VXVC
;
591 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
592 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
593 /* We must update the target FPR before raising the exception */
595 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
596 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
597 /* Update the floating-point enabled exception summary */
598 env
->fpscr
|= 1 << FPSCR_FEX
;
599 /* Exception is differed */
603 case POWERPC_EXCP_FP_VXSQRT
:
604 /* Square root of a negative number */
605 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
607 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
609 /* Set the result to quiet NaN */
611 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
612 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
615 case POWERPC_EXCP_FP_VXCVI
:
616 /* Invalid conversion */
617 env
->fpscr
|= 1 << FPSCR_VXCVI
;
618 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
620 /* Set the result to quiet NaN */
622 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
623 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
627 /* Update the floating-point invalid operation summary */
628 env
->fpscr
|= 1 << FPSCR_VX
;
629 /* Update the floating-point exception summary */
630 env
->fpscr
|= 1 << FPSCR_FX
;
632 /* Update the floating-point enabled exception summary */
633 env
->fpscr
|= 1 << FPSCR_FEX
;
634 if (msr_fe0
!= 0 || msr_fe1
!= 0)
635 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_FP
| op
);
639 static always_inline
void float_zero_divide_excp (void)
646 env
->fpscr
|= 1 << FPSCR_ZX
;
647 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
648 /* Update the floating-point exception summary */
649 env
->fpscr
|= 1 << FPSCR_FX
;
651 /* Update the floating-point enabled exception summary */
652 env
->fpscr
|= 1 << FPSCR_FEX
;
653 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
654 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
655 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
658 /* Set the result to infinity */
661 u0
.u
= ((u0
.u
^ u1
.u
) & 0x8000000000000000ULL
);
662 u0
.u
|= 0x7FFULL
<< 52;
667 static always_inline
void float_overflow_excp (void)
669 env
->fpscr
|= 1 << FPSCR_OX
;
670 /* Update the floating-point exception summary */
671 env
->fpscr
|= 1 << FPSCR_FX
;
673 /* XXX: should adjust the result */
674 /* Update the floating-point enabled exception summary */
675 env
->fpscr
|= 1 << FPSCR_FEX
;
676 /* We must update the target FPR before raising the exception */
677 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
678 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
680 env
->fpscr
|= 1 << FPSCR_XX
;
681 env
->fpscr
|= 1 << FPSCR_FI
;
685 static always_inline
void float_underflow_excp (void)
687 env
->fpscr
|= 1 << FPSCR_UX
;
688 /* Update the floating-point exception summary */
689 env
->fpscr
|= 1 << FPSCR_FX
;
691 /* XXX: should adjust the result */
692 /* Update the floating-point enabled exception summary */
693 env
->fpscr
|= 1 << FPSCR_FEX
;
694 /* We must update the target FPR before raising the exception */
695 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
696 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
700 static always_inline
void float_inexact_excp (void)
702 env
->fpscr
|= 1 << FPSCR_XX
;
703 /* Update the floating-point exception summary */
704 env
->fpscr
|= 1 << FPSCR_FX
;
706 /* Update the floating-point enabled exception summary */
707 env
->fpscr
|= 1 << FPSCR_FEX
;
708 /* We must update the target FPR before raising the exception */
709 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
710 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
714 static always_inline
void fpscr_set_rounding_mode (void)
718 /* Set rounding mode */
721 /* Best approximation (round to nearest) */
722 rnd_type
= float_round_nearest_even
;
725 /* Smaller magnitude (round toward zero) */
726 rnd_type
= float_round_to_zero
;
729 /* Round toward +infinite */
730 rnd_type
= float_round_up
;
734 /* Round toward -infinite */
735 rnd_type
= float_round_down
;
738 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
741 void do_fpscr_setbit (int bit
)
745 prev
= (env
->fpscr
>> bit
) & 1;
746 env
->fpscr
|= 1 << bit
;
750 env
->fpscr
|= 1 << FPSCR_FX
;
754 env
->fpscr
|= 1 << FPSCR_FX
;
759 env
->fpscr
|= 1 << FPSCR_FX
;
764 env
->fpscr
|= 1 << FPSCR_FX
;
769 env
->fpscr
|= 1 << FPSCR_FX
;
782 env
->fpscr
|= 1 << FPSCR_VX
;
783 env
->fpscr
|= 1 << FPSCR_FX
;
790 env
->error_code
= POWERPC_EXCP_FP
;
792 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
794 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
796 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
798 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
800 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
802 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
804 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
806 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
808 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
815 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
822 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
829 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
836 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
842 fpscr_set_rounding_mode();
847 /* Update the floating-point enabled exception summary */
848 env
->fpscr
|= 1 << FPSCR_FEX
;
849 /* We have to update Rc1 before raising the exception */
850 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
856 #if defined(WORDS_BIGENDIAN)
863 void do_store_fpscr (uint32_t mask
)
866 * We use only the 32 LSB of the incoming fpr
881 new |= prev
& 0x90000000;
882 for (i
= 0; i
< 7; i
++) {
883 if (mask
& (1 << i
)) {
884 env
->fpscr
&= ~(0xF << (4 * i
));
885 env
->fpscr
|= new & (0xF << (4 * i
));
888 /* Update VX and FEX */
890 env
->fpscr
|= 1 << FPSCR_VX
;
891 if ((fpscr_ex
& fpscr_eex
) != 0) {
892 env
->fpscr
|= 1 << FPSCR_FEX
;
893 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
894 /* XXX: we should compute it properly */
895 env
->error_code
= POWERPC_EXCP_FP
;
897 fpscr_set_rounding_mode();
902 #ifdef CONFIG_SOFTFLOAT
903 void do_float_check_status (void)
905 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
906 (env
->error_code
& POWERPC_EXCP_FP
)) {
907 /* Differred floating-point exception after target FPR update */
908 if (msr_fe0
!= 0 || msr_fe1
!= 0)
909 do_raise_exception_err(env
->exception_index
, env
->error_code
);
910 } else if (env
->fp_status
.float_exception_flags
& float_flag_overflow
) {
911 float_overflow_excp();
912 } else if (env
->fp_status
.float_exception_flags
& float_flag_underflow
) {
913 float_underflow_excp();
914 } else if (env
->fp_status
.float_exception_flags
& float_flag_inexact
) {
915 float_inexact_excp();
920 #if USE_PRECISE_EMULATION
923 if (unlikely(float64_is_signaling_nan(FT0
) ||
924 float64_is_signaling_nan(FT1
))) {
926 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
927 } else if (likely(isfinite(FT0
) || isfinite(FT1
) ||
928 fpisneg(FT0
) == fpisneg(FT1
))) {
929 FT0
= float64_add(FT0
, FT1
, &env
->fp_status
);
931 /* Magnitude subtraction of infinities */
932 fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
938 if (unlikely(float64_is_signaling_nan(FT0
) ||
939 float64_is_signaling_nan(FT1
))) {
940 /* sNaN subtraction */
941 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
942 } else if (likely(isfinite(FT0
) || isfinite(FT1
) ||
943 fpisneg(FT0
) != fpisneg(FT1
))) {
944 FT0
= float64_sub(FT0
, FT1
, &env
->fp_status
);
946 /* Magnitude subtraction of infinities */
947 fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
953 if (unlikely(float64_is_signaling_nan(FT0
) ||
954 float64_is_signaling_nan(FT1
))) {
955 /* sNaN multiplication */
956 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
957 } else if (unlikely((isinfinity(FT0
) && iszero(FT1
)) ||
958 (iszero(FT0
) && isinfinity(FT1
)))) {
959 /* Multiplication of zero by infinity */
960 fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
962 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
968 if (unlikely(float64_is_signaling_nan(FT0
) ||
969 float64_is_signaling_nan(FT1
))) {
971 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
972 } else if (unlikely(isinfinity(FT0
) && isinfinity(FT1
))) {
973 /* Division of infinity by infinity */
974 fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI
);
975 } else if (unlikely(iszero(FT1
))) {
977 /* Division of zero by zero */
978 fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ
);
980 /* Division by zero */
981 float_zero_divide_excp();
984 FT0
= float64_div(FT0
, FT1
, &env
->fp_status
);
987 #endif /* USE_PRECISE_EMULATION */
996 if (unlikely(float64_is_signaling_nan(FT0
))) {
997 /* sNaN conversion */
998 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
999 } else if (unlikely(float64_is_nan(FT0
) || isinfinity(FT0
))) {
1000 /* qNan / infinity conversion */
1001 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1003 p
.i
= float64_to_int32(FT0
, &env
->fp_status
);
1004 #if USE_PRECISE_EMULATION
1005 /* XXX: higher bits are not supposed to be significant.
1006 * to make tests easier, return the same as a real PowerPC 750
1008 p
.i
|= 0xFFF80000ULL
<< 32;
1014 void do_fctiwz (void)
1021 if (unlikely(float64_is_signaling_nan(FT0
))) {
1022 /* sNaN conversion */
1023 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1024 } else if (unlikely(float64_is_nan(FT0
) || isinfinity(FT0
))) {
1025 /* qNan / infinity conversion */
1026 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1028 p
.i
= float64_to_int32_round_to_zero(FT0
, &env
->fp_status
);
1029 #if USE_PRECISE_EMULATION
1030 /* XXX: higher bits are not supposed to be significant.
1031 * to make tests easier, return the same as a real PowerPC 750
1033 p
.i
|= 0xFFF80000ULL
<< 32;
1039 #if defined(TARGET_PPC64)
1040 void do_fcfid (void)
1048 FT0
= int64_to_float64(p
.i
, &env
->fp_status
);
1051 void do_fctid (void)
1058 if (unlikely(float64_is_signaling_nan(FT0
))) {
1059 /* sNaN conversion */
1060 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1061 } else if (unlikely(float64_is_nan(FT0
) || isinfinity(FT0
))) {
1062 /* qNan / infinity conversion */
1063 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1065 p
.i
= float64_to_int64(FT0
, &env
->fp_status
);
1070 void do_fctidz (void)
1077 if (unlikely(float64_is_signaling_nan(FT0
))) {
1078 /* sNaN conversion */
1079 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1080 } else if (unlikely(float64_is_nan(FT0
) || isinfinity(FT0
))) {
1081 /* qNan / infinity conversion */
1082 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1084 p
.i
= float64_to_int64_round_to_zero(FT0
, &env
->fp_status
);
1091 static always_inline
void do_fri (int rounding_mode
)
1093 if (unlikely(float64_is_signaling_nan(FT0
))) {
1095 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1096 } else if (unlikely(float64_is_nan(FT0
) || isinfinity(FT0
))) {
1097 /* qNan / infinity round */
1098 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1100 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
1101 FT0
= float64_round_to_int(FT0
, &env
->fp_status
);
1102 /* Restore rounding mode from FPSCR */
1103 fpscr_set_rounding_mode();
1109 do_fri(float_round_nearest_even
);
1114 do_fri(float_round_to_zero
);
1119 do_fri(float_round_up
);
1124 do_fri(float_round_down
);
1127 #if USE_PRECISE_EMULATION
1128 void do_fmadd (void)
1130 if (unlikely(float64_is_signaling_nan(FT0
) ||
1131 float64_is_signaling_nan(FT1
) ||
1132 float64_is_signaling_nan(FT2
))) {
1133 /* sNaN operation */
1134 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1137 /* This is the way the PowerPC specification defines it */
1138 float128 ft0_128
, ft1_128
;
1140 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
1141 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
1142 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1143 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
1144 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1145 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
1147 /* This is OK on x86 hosts */
1148 FT0
= (FT0
* FT1
) + FT2
;
1153 void do_fmsub (void)
1155 if (unlikely(float64_is_signaling_nan(FT0
) ||
1156 float64_is_signaling_nan(FT1
) ||
1157 float64_is_signaling_nan(FT2
))) {
1158 /* sNaN operation */
1159 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1162 /* This is the way the PowerPC specification defines it */
1163 float128 ft0_128
, ft1_128
;
1165 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
1166 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
1167 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1168 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
1169 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1170 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
1172 /* This is OK on x86 hosts */
1173 FT0
= (FT0
* FT1
) - FT2
;
1177 #endif /* USE_PRECISE_EMULATION */
1179 void do_fnmadd (void)
1181 if (unlikely(float64_is_signaling_nan(FT0
) ||
1182 float64_is_signaling_nan(FT1
) ||
1183 float64_is_signaling_nan(FT2
))) {
1184 /* sNaN operation */
1185 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1187 #if USE_PRECISE_EMULATION
1189 /* This is the way the PowerPC specification defines it */
1190 float128 ft0_128
, ft1_128
;
1192 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
1193 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
1194 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1195 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
1196 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1197 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
1199 /* This is OK on x86 hosts */
1200 FT0
= (FT0
* FT1
) + FT2
;
1203 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
1204 FT0
= float64_add(FT0
, FT2
, &env
->fp_status
);
1206 if (likely(!isnan(FT0
)))
1207 FT0
= float64_chs(FT0
);
1211 void do_fnmsub (void)
1213 if (unlikely(float64_is_signaling_nan(FT0
) ||
1214 float64_is_signaling_nan(FT1
) ||
1215 float64_is_signaling_nan(FT2
))) {
1216 /* sNaN operation */
1217 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1219 #if USE_PRECISE_EMULATION
1221 /* This is the way the PowerPC specification defines it */
1222 float128 ft0_128
, ft1_128
;
1224 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
1225 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
1226 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1227 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
1228 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1229 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
1231 /* This is OK on x86 hosts */
1232 FT0
= (FT0
* FT1
) - FT2
;
1235 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
1236 FT0
= float64_sub(FT0
, FT2
, &env
->fp_status
);
1238 if (likely(!isnan(FT0
)))
1239 FT0
= float64_chs(FT0
);
1243 #if USE_PRECISE_EMULATION
1246 if (unlikely(float64_is_signaling_nan(FT0
))) {
1247 /* sNaN square root */
1248 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1250 FT0
= float64_to_float32(FT0
, &env
->fp_status
);
1253 #endif /* USE_PRECISE_EMULATION */
1255 void do_fsqrt (void)
1257 if (unlikely(float64_is_signaling_nan(FT0
))) {
1258 /* sNaN square root */
1259 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1260 } else if (unlikely(fpisneg(FT0
) && !iszero(FT0
))) {
1261 /* Square root of a negative nonzero number */
1262 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1264 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
1275 if (unlikely(float64_is_signaling_nan(FT0
))) {
1276 /* sNaN reciprocal */
1277 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1278 } else if (unlikely(iszero(FT0
))) {
1279 /* Zero reciprocal */
1280 float_zero_divide_excp();
1281 } else if (likely(isnormal(FT0
))) {
1282 FT0
= float64_div(1.0, FT0
, &env
->fp_status
);
1285 if (p
.i
== 0x8000000000000000ULL
) {
1286 p
.i
= 0xFFF0000000000000ULL
;
1287 } else if (p
.i
== 0x0000000000000000ULL
) {
1288 p
.i
= 0x7FF0000000000000ULL
;
1289 } else if (isnan(FT0
)) {
1290 p
.i
= 0x7FF8000000000000ULL
;
1291 } else if (fpisneg(FT0
)) {
1292 p
.i
= 0x8000000000000000ULL
;
1294 p
.i
= 0x0000000000000000ULL
;
1307 if (unlikely(float64_is_signaling_nan(FT0
))) {
1308 /* sNaN reciprocal */
1309 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1310 } else if (unlikely(iszero(FT0
))) {
1311 /* Zero reciprocal */
1312 float_zero_divide_excp();
1313 } else if (likely(isnormal(FT0
))) {
1314 #if USE_PRECISE_EMULATION
1315 FT0
= float64_div(1.0, FT0
, &env
->fp_status
);
1316 FT0
= float64_to_float32(FT0
, &env
->fp_status
);
1318 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
1322 if (p
.i
== 0x8000000000000000ULL
) {
1323 p
.i
= 0xFFF0000000000000ULL
;
1324 } else if (p
.i
== 0x0000000000000000ULL
) {
1325 p
.i
= 0x7FF0000000000000ULL
;
1326 } else if (isnan(FT0
)) {
1327 p
.i
= 0x7FF8000000000000ULL
;
1328 } else if (fpisneg(FT0
)) {
1329 p
.i
= 0x8000000000000000ULL
;
1331 p
.i
= 0x0000000000000000ULL
;
1337 void do_frsqrte (void)
1344 if (unlikely(float64_is_signaling_nan(FT0
))) {
1345 /* sNaN reciprocal square root */
1346 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1347 } else if (unlikely(fpisneg(FT0
) && !iszero(FT0
))) {
1348 /* Reciprocal square root of a negative nonzero number */
1349 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1350 } else if (likely(isnormal(FT0
))) {
1351 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
1352 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
1355 if (p
.i
== 0x8000000000000000ULL
) {
1356 p
.i
= 0xFFF0000000000000ULL
;
1357 } else if (p
.i
== 0x0000000000000000ULL
) {
1358 p
.i
= 0x7FF0000000000000ULL
;
1359 } else if (isnan(FT0
)) {
1360 p
.i
|= 0x000FFFFFFFFFFFFFULL
;
1361 } else if (fpisneg(FT0
)) {
1362 p
.i
= 0x7FF8000000000000ULL
;
1364 p
.i
= 0x0000000000000000ULL
;
1372 if (!fpisneg(FT0
) || iszero(FT0
))
1378 void do_fcmpu (void)
1380 if (unlikely(float64_is_signaling_nan(FT0
) ||
1381 float64_is_signaling_nan(FT1
))) {
1382 /* sNaN comparison */
1383 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1385 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
1387 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
1393 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1394 env
->fpscr
|= T0
<< FPSCR_FPRF
;
1397 void do_fcmpo (void)
1399 if (unlikely(float64_is_nan(FT0
) ||
1400 float64_is_nan(FT1
))) {
1401 if (float64_is_signaling_nan(FT0
) ||
1402 float64_is_signaling_nan(FT1
)) {
1403 /* sNaN comparison */
1404 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
|
1405 POWERPC_EXCP_FP_VXVC
);
1407 /* qNaN comparison */
1408 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC
);
1411 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
1413 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
1419 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1420 env
->fpscr
|= T0
<< FPSCR_FPRF
;
1423 #if !defined (CONFIG_USER_ONLY)
1424 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
);
1426 void do_store_msr (void)
1428 T0
= hreg_store_msr(env
, T0
, 0);
1430 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1431 do_raise_exception(T0
);
1435 static always_inline
void __do_rfi (target_ulong nip
, target_ulong msr
,
1436 target_ulong msrm
, int keep_msrh
)
1438 #if defined(TARGET_PPC64)
1439 if (msr
& (1ULL << MSR_SF
)) {
1440 nip
= (uint64_t)nip
;
1441 msr
&= (uint64_t)msrm
;
1443 nip
= (uint32_t)nip
;
1444 msr
= (uint32_t)(msr
& msrm
);
1446 msr
|= env
->msr
& ~((uint64_t)0xFFFFFFFF);
1449 nip
= (uint32_t)nip
;
1450 msr
&= (uint32_t)msrm
;
1452 /* XXX: beware: this is false if VLE is supported */
1453 env
->nip
= nip
& ~((target_ulong
)0x00000003);
1454 hreg_store_msr(env
, msr
, 1);
1455 #if defined (DEBUG_OP)
1456 cpu_dump_rfi(env
->nip
, env
->msr
);
1458 /* No need to raise an exception here,
1459 * as rfi is always the last insn of a TB
1461 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1466 __do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1467 ~((target_ulong
)0xFFFF0000), 1);
1470 #if defined(TARGET_PPC64)
1473 __do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1474 ~((target_ulong
)0xFFFF0000), 0);
1477 void do_hrfid (void)
1479 __do_rfi(env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
],
1480 ~((target_ulong
)0xFFFF0000), 0);
1485 void do_tw (int flags
)
1487 if (!likely(!(((int32_t)T0
< (int32_t)T1
&& (flags
& 0x10)) ||
1488 ((int32_t)T0
> (int32_t)T1
&& (flags
& 0x08)) ||
1489 ((int32_t)T0
== (int32_t)T1
&& (flags
& 0x04)) ||
1490 ((uint32_t)T0
< (uint32_t)T1
&& (flags
& 0x02)) ||
1491 ((uint32_t)T0
> (uint32_t)T1
&& (flags
& 0x01))))) {
1492 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1496 #if defined(TARGET_PPC64)
1497 void do_td (int flags
)
1499 if (!likely(!(((int64_t)T0
< (int64_t)T1
&& (flags
& 0x10)) ||
1500 ((int64_t)T0
> (int64_t)T1
&& (flags
& 0x08)) ||
1501 ((int64_t)T0
== (int64_t)T1
&& (flags
& 0x04)) ||
1502 ((uint64_t)T0
< (uint64_t)T1
&& (flags
& 0x02)) ||
1503 ((uint64_t)T0
> (uint64_t)T1
&& (flags
& 0x01)))))
1504 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1508 /*****************************************************************************/
1509 /* PowerPC 601 specific instructions (POWER bridge) */
1510 void do_POWER_abso (void)
1512 if ((int32_t)T0
== INT32_MIN
) {
1515 } else if ((int32_t)T0
< 0) {
1524 void do_POWER_clcs (void)
1528 /* Instruction cache line size */
1529 T0
= env
->icache_line_size
;
1532 /* Data cache line size */
1533 T0
= env
->dcache_line_size
;
1536 /* Minimum cache line size */
1537 T0
= env
->icache_line_size
< env
->dcache_line_size
?
1538 env
->icache_line_size
: env
->dcache_line_size
;
1541 /* Maximum cache line size */
1542 T0
= env
->icache_line_size
> env
->dcache_line_size
?
1543 env
->icache_line_size
: env
->dcache_line_size
;
1551 void do_POWER_div (void)
1555 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1557 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1558 env
->spr
[SPR_MQ
] = 0;
1560 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1561 env
->spr
[SPR_MQ
] = tmp
% T1
;
1562 T0
= tmp
/ (int32_t)T1
;
1566 void do_POWER_divo (void)
1570 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1572 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1573 env
->spr
[SPR_MQ
] = 0;
1576 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1577 env
->spr
[SPR_MQ
] = tmp
% T1
;
1579 if (tmp
> (int64_t)INT32_MAX
|| tmp
< (int64_t)INT32_MIN
) {
1589 void do_POWER_divs (void)
1591 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1593 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1594 env
->spr
[SPR_MQ
] = 0;
1596 env
->spr
[SPR_MQ
] = T0
% T1
;
1597 T0
= (int32_t)T0
/ (int32_t)T1
;
1601 void do_POWER_divso (void)
1603 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1605 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1606 env
->spr
[SPR_MQ
] = 0;
1609 T0
= (int32_t)T0
/ (int32_t)T1
;
1610 env
->spr
[SPR_MQ
] = (int32_t)T0
% (int32_t)T1
;
1616 void do_POWER_dozo (void)
1618 if ((int32_t)T1
> (int32_t)T0
) {
1621 if (((uint32_t)(~T2
) ^ (uint32_t)T1
^ UINT32_MAX
) &
1622 ((uint32_t)(~T2
) ^ (uint32_t)T0
) & (1UL << 31)) {
1634 void do_POWER_maskg (void)
1638 if ((uint32_t)T0
== (uint32_t)(T1
+ 1)) {
1641 ret
= (UINT32_MAX
>> ((uint32_t)T0
)) ^
1642 ((UINT32_MAX
>> ((uint32_t)T1
)) >> 1);
1643 if ((uint32_t)T0
> (uint32_t)T1
)
1649 void do_POWER_mulo (void)
1653 tmp
= (uint64_t)T0
* (uint64_t)T1
;
1654 env
->spr
[SPR_MQ
] = tmp
>> 32;
1656 if (tmp
>> 32 != ((uint64_t)T0
>> 16) * ((uint64_t)T1
>> 16)) {
1664 #if !defined (CONFIG_USER_ONLY)
1665 void do_POWER_rac (void)
1670 /* We don't have to generate many instances of this instruction,
1671 * as rac is supervisor only.
1673 /* XXX: FIX THIS: Pretend we have no BAT */
1674 nb_BATs
= env
->nb_BATs
;
1676 if (get_physical_address(env
, &ctx
, T0
, 0, ACCESS_INT
) == 0)
1678 env
->nb_BATs
= nb_BATs
;
1681 void do_POWER_rfsvc (void)
1683 __do_rfi(env
->lr
, env
->ctr
, 0x0000FFFF, 0);
1686 void do_store_hid0_601 (void)
1690 hid0
= env
->spr
[SPR_HID0
];
1691 if ((T0
^ hid0
) & 0x00000008) {
1692 /* Change current endianness */
1693 env
->hflags
&= ~(1 << MSR_LE
);
1694 env
->hflags_nmsr
&= ~(1 << MSR_LE
);
1695 env
->hflags_nmsr
|= (1 << MSR_LE
) & (((T0
>> 3) & 1) << MSR_LE
);
1696 env
->hflags
|= env
->hflags_nmsr
;
1697 if (loglevel
!= 0) {
1698 fprintf(logfile
, "%s: set endianness to %c => " ADDRX
"\n",
1699 __func__
, T0
& 0x8 ? 'l' : 'b', env
->hflags
);
1702 env
->spr
[SPR_HID0
] = T0
;
1706 /*****************************************************************************/
1707 /* 602 specific instructions */
1708 /* mfrom is the most crazy instruction ever seen, imho ! */
1709 /* Real implementation uses a ROM table. Do the same */
1710 #define USE_MFROM_ROM_TABLE
1711 void do_op_602_mfrom (void)
1713 if (likely(T0
< 602)) {
1714 #if defined(USE_MFROM_ROM_TABLE)
1715 #include "mfrom_table.c"
1716 T0
= mfrom_ROM_table
[T0
];
1719 /* Extremly decomposed:
1721 * T0 = 256 * log10(10 + 1.0) + 0.5
1724 d
= float64_div(d
, 256, &env
->fp_status
);
1726 d
= exp10(d
); // XXX: use float emulation function
1727 d
= float64_add(d
, 1.0, &env
->fp_status
);
1728 d
= log10(d
); // XXX: use float emulation function
1729 d
= float64_mul(d
, 256, &env
->fp_status
);
1730 d
= float64_add(d
, 0.5, &env
->fp_status
);
1731 T0
= float64_round_to_int(d
, &env
->fp_status
);
1738 /*****************************************************************************/
1739 /* Embedded PowerPC specific helpers */
1740 void do_405_check_sat (void)
1742 if (!likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1743 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1744 /* Saturate result */
1753 /* XXX: to be improved to check access rights when in user-mode */
1754 void do_load_dcr (void)
1758 if (unlikely(env
->dcr_env
== NULL
)) {
1759 if (loglevel
!= 0) {
1760 fprintf(logfile
, "No DCR environment\n");
1762 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1763 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1764 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, T0
, &val
) != 0)) {
1765 if (loglevel
!= 0) {
1766 fprintf(logfile
, "DCR read error %d %03x\n", (int)T0
, (int)T0
);
1768 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1769 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1775 void do_store_dcr (void)
1777 if (unlikely(env
->dcr_env
== NULL
)) {
1778 if (loglevel
!= 0) {
1779 fprintf(logfile
, "No DCR environment\n");
1781 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1782 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1783 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, T0
, T1
) != 0)) {
1784 if (loglevel
!= 0) {
1785 fprintf(logfile
, "DCR write error %d %03x\n", (int)T0
, (int)T0
);
1787 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1788 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1792 #if !defined(CONFIG_USER_ONLY)
1793 void do_40x_rfci (void)
1795 __do_rfi(env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
],
1796 ~((target_ulong
)0xFFFF0000), 0);
1801 __do_rfi(env
->spr
[SPR_BOOKE_CSRR0
], SPR_BOOKE_CSRR1
,
1802 ~((target_ulong
)0x3FFF0000), 0);
1807 __do_rfi(env
->spr
[SPR_BOOKE_DSRR0
], SPR_BOOKE_DSRR1
,
1808 ~((target_ulong
)0x3FFF0000), 0);
1811 void do_rfmci (void)
1813 __do_rfi(env
->spr
[SPR_BOOKE_MCSRR0
], SPR_BOOKE_MCSRR1
,
1814 ~((target_ulong
)0x3FFF0000), 0);
1817 void do_load_403_pb (int num
)
1822 void do_store_403_pb (int num
)
1824 if (likely(env
->pb
[num
] != T0
)) {
1826 /* Should be optimized */
1833 void do_440_dlmzb (void)
1839 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1840 if ((T0
& mask
) == 0)
1844 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1845 if ((T1
& mask
) == 0)
1853 /* SPE extension helpers */
1854 /* Use a table to make this quicker */
1855 static uint8_t hbrev
[16] = {
1856 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1857 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1860 static always_inline
uint8_t byte_reverse (uint8_t val
)
1862 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1865 static always_inline
uint32_t word_reverse (uint32_t val
)
1867 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1868 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1871 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
1872 void do_brinc (void)
1874 uint32_t a
, b
, d
, mask
;
1876 mask
= UINT32_MAX
>> (32 - MASKBITS
);
1879 d
= word_reverse(1 + word_reverse(a
| ~b
));
1880 T0
= (T0
& ~mask
) | (d
& b
);
1883 #define DO_SPE_OP2(name) \
1884 void do_ev##name (void) \
1886 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) | \
1887 (uint64_t)_do_e##name(T0_64, T1_64); \
1890 #define DO_SPE_OP1(name) \
1891 void do_ev##name (void) \
1893 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) | \
1894 (uint64_t)_do_e##name(T0_64); \
1897 /* Fixed-point vector arithmetic */
1898 static always_inline
uint32_t _do_eabs (uint32_t val
)
1900 if ((val
& 0x80000000) && val
!= 0x80000000)
1906 static always_inline
uint32_t _do_eaddw (uint32_t op1
, uint32_t op2
)
1911 static always_inline
int _do_ecntlsw (uint32_t val
)
1913 if (val
& 0x80000000)
1919 static always_inline
int _do_ecntlzw (uint32_t val
)
1924 static always_inline
uint32_t _do_eneg (uint32_t val
)
1926 if (val
!= 0x80000000)
1932 static always_inline
uint32_t _do_erlw (uint32_t op1
, uint32_t op2
)
1934 return rotl32(op1
, op2
);
1937 static always_inline
uint32_t _do_erndw (uint32_t val
)
1939 return (val
+ 0x000080000000) & 0xFFFF0000;
1942 static always_inline
uint32_t _do_eslw (uint32_t op1
, uint32_t op2
)
1944 /* No error here: 6 bits are used */
1945 return op1
<< (op2
& 0x3F);
1948 static always_inline
int32_t _do_esrws (int32_t op1
, uint32_t op2
)
1950 /* No error here: 6 bits are used */
1951 return op1
>> (op2
& 0x3F);
1954 static always_inline
uint32_t _do_esrwu (uint32_t op1
, uint32_t op2
)
1956 /* No error here: 6 bits are used */
1957 return op1
>> (op2
& 0x3F);
1960 static always_inline
uint32_t _do_esubfw (uint32_t op1
, uint32_t op2
)
1988 /* evsel is a little bit more complicated... */
1989 static always_inline
uint32_t _do_esel (uint32_t op1
, uint32_t op2
, int n
)
1997 void do_evsel (void)
1999 T0_64
= ((uint64_t)_do_esel(T0_64
>> 32, T1_64
>> 32, T0
>> 3) << 32) |
2000 (uint64_t)_do_esel(T0_64
, T1_64
, (T0
>> 2) & 1);
2003 /* Fixed-point vector comparisons */
2004 #define DO_SPE_CMP(name) \
2005 void do_ev##name (void) \
2007 T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32, \
2008 T1_64 >> 32) << 32, \
2009 _do_e##name(T0_64, T1_64)); \
2012 static always_inline
uint32_t _do_evcmp_merge (int t0
, int t1
)
2014 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
2016 static always_inline
int _do_ecmpeq (uint32_t op1
, uint32_t op2
)
2018 return op1
== op2
? 1 : 0;
2021 static always_inline
int _do_ecmpgts (int32_t op1
, int32_t op2
)
2023 return op1
> op2
? 1 : 0;
2026 static always_inline
int _do_ecmpgtu (uint32_t op1
, uint32_t op2
)
2028 return op1
> op2
? 1 : 0;
2031 static always_inline
int _do_ecmplts (int32_t op1
, int32_t op2
)
2033 return op1
< op2
? 1 : 0;
2036 static always_inline
int _do_ecmpltu (uint32_t op1
, uint32_t op2
)
2038 return op1
< op2
? 1 : 0;
2052 /* Single precision floating-point conversions from/to integer */
2053 static always_inline
uint32_t _do_efscfsi (int32_t val
)
2060 u
.f
= int32_to_float32(val
, &env
->spe_status
);
2065 static always_inline
uint32_t _do_efscfui (uint32_t val
)
2072 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
2077 static always_inline
int32_t _do_efsctsi (uint32_t val
)
2085 /* NaN are not treated the same way IEEE 754 does */
2086 if (unlikely(isnan(u
.f
)))
2089 return float32_to_int32(u
.f
, &env
->spe_status
);
2092 static always_inline
uint32_t _do_efsctui (uint32_t val
)
2100 /* NaN are not treated the same way IEEE 754 does */
2101 if (unlikely(isnan(u
.f
)))
2104 return float32_to_uint32(u
.f
, &env
->spe_status
);
2107 static always_inline
int32_t _do_efsctsiz (uint32_t val
)
2115 /* NaN are not treated the same way IEEE 754 does */
2116 if (unlikely(isnan(u
.f
)))
2119 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
2122 static always_inline
uint32_t _do_efsctuiz (uint32_t val
)
2130 /* NaN are not treated the same way IEEE 754 does */
2131 if (unlikely(isnan(u
.f
)))
2134 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2137 void do_efscfsi (void)
2139 T0_64
= _do_efscfsi(T0_64
);
2142 void do_efscfui (void)
2144 T0_64
= _do_efscfui(T0_64
);
2147 void do_efsctsi (void)
2149 T0_64
= _do_efsctsi(T0_64
);
2152 void do_efsctui (void)
2154 T0_64
= _do_efsctui(T0_64
);
2157 void do_efsctsiz (void)
2159 T0_64
= _do_efsctsiz(T0_64
);
2162 void do_efsctuiz (void)
2164 T0_64
= _do_efsctuiz(T0_64
);
2167 /* Single precision floating-point conversion to/from fractional */
2168 static always_inline
uint32_t _do_efscfsf (uint32_t val
)
2176 u
.f
= int32_to_float32(val
, &env
->spe_status
);
2177 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
2178 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
2183 static always_inline
uint32_t _do_efscfuf (uint32_t val
)
2191 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
2192 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2193 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
2198 static always_inline
int32_t _do_efsctsf (uint32_t val
)
2207 /* NaN are not treated the same way IEEE 754 does */
2208 if (unlikely(isnan(u
.f
)))
2210 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2211 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2213 return float32_to_int32(u
.f
, &env
->spe_status
);
2216 static always_inline
uint32_t _do_efsctuf (uint32_t val
)
2225 /* NaN are not treated the same way IEEE 754 does */
2226 if (unlikely(isnan(u
.f
)))
2228 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2229 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2231 return float32_to_uint32(u
.f
, &env
->spe_status
);
2234 static always_inline
int32_t _do_efsctsfz (uint32_t val
)
2243 /* NaN are not treated the same way IEEE 754 does */
2244 if (unlikely(isnan(u
.f
)))
2246 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2247 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2249 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
2252 static always_inline
uint32_t _do_efsctufz (uint32_t val
)
2261 /* NaN are not treated the same way IEEE 754 does */
2262 if (unlikely(isnan(u
.f
)))
2264 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2265 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2267 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2270 void do_efscfsf (void)
2272 T0_64
= _do_efscfsf(T0_64
);
2275 void do_efscfuf (void)
2277 T0_64
= _do_efscfuf(T0_64
);
2280 void do_efsctsf (void)
2282 T0_64
= _do_efsctsf(T0_64
);
2285 void do_efsctuf (void)
2287 T0_64
= _do_efsctuf(T0_64
);
2290 void do_efsctsfz (void)
2292 T0_64
= _do_efsctsfz(T0_64
);
2295 void do_efsctufz (void)
2297 T0_64
= _do_efsctufz(T0_64
);
2300 /* Double precision floating point helpers */
2301 static always_inline
int _do_efdcmplt (uint64_t op1
, uint64_t op2
)
2303 /* XXX: TODO: test special values (NaN, infinites, ...) */
2304 return _do_efdtstlt(op1
, op2
);
2307 static always_inline
int _do_efdcmpgt (uint64_t op1
, uint64_t op2
)
2309 /* XXX: TODO: test special values (NaN, infinites, ...) */
2310 return _do_efdtstgt(op1
, op2
);
2313 static always_inline
int _do_efdcmpeq (uint64_t op1
, uint64_t op2
)
2315 /* XXX: TODO: test special values (NaN, infinites, ...) */
2316 return _do_efdtsteq(op1
, op2
);
2319 void do_efdcmplt (void)
2321 T0
= _do_efdcmplt(T0_64
, T1_64
);
2324 void do_efdcmpgt (void)
2326 T0
= _do_efdcmpgt(T0_64
, T1_64
);
2329 void do_efdcmpeq (void)
2331 T0
= _do_efdcmpeq(T0_64
, T1_64
);
2334 /* Double precision floating-point conversion to/from integer */
2335 static always_inline
uint64_t _do_efdcfsi (int64_t val
)
2342 u
.f
= int64_to_float64(val
, &env
->spe_status
);
2347 static always_inline
uint64_t _do_efdcfui (uint64_t val
)
2354 u
.f
= uint64_to_float64(val
, &env
->spe_status
);
2359 static always_inline
int64_t _do_efdctsi (uint64_t val
)
2367 /* NaN are not treated the same way IEEE 754 does */
2368 if (unlikely(isnan(u
.f
)))
2371 return float64_to_int64(u
.f
, &env
->spe_status
);
2374 static always_inline
uint64_t _do_efdctui (uint64_t val
)
2382 /* NaN are not treated the same way IEEE 754 does */
2383 if (unlikely(isnan(u
.f
)))
2386 return float64_to_uint64(u
.f
, &env
->spe_status
);
2389 static always_inline
int64_t _do_efdctsiz (uint64_t val
)
2397 /* NaN are not treated the same way IEEE 754 does */
2398 if (unlikely(isnan(u
.f
)))
2401 return float64_to_int64_round_to_zero(u
.f
, &env
->spe_status
);
2404 static always_inline
uint64_t _do_efdctuiz (uint64_t val
)
2412 /* NaN are not treated the same way IEEE 754 does */
2413 if (unlikely(isnan(u
.f
)))
2416 return float64_to_uint64_round_to_zero(u
.f
, &env
->spe_status
);
2419 void do_efdcfsi (void)
2421 T0_64
= _do_efdcfsi(T0_64
);
2424 void do_efdcfui (void)
2426 T0_64
= _do_efdcfui(T0_64
);
2429 void do_efdctsi (void)
2431 T0_64
= _do_efdctsi(T0_64
);
2434 void do_efdctui (void)
2436 T0_64
= _do_efdctui(T0_64
);
2439 void do_efdctsiz (void)
2441 T0_64
= _do_efdctsiz(T0_64
);
2444 void do_efdctuiz (void)
2446 T0_64
= _do_efdctuiz(T0_64
);
2449 /* Double precision floating-point conversion to/from fractional */
2450 static always_inline
uint64_t _do_efdcfsf (int64_t val
)
2458 u
.f
= int32_to_float64(val
, &env
->spe_status
);
2459 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2460 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
2465 static always_inline
uint64_t _do_efdcfuf (uint64_t val
)
2473 u
.f
= uint32_to_float64(val
, &env
->spe_status
);
2474 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2475 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
2480 static always_inline
int64_t _do_efdctsf (uint64_t val
)
2489 /* NaN are not treated the same way IEEE 754 does */
2490 if (unlikely(isnan(u
.f
)))
2492 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2493 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2495 return float64_to_int32(u
.f
, &env
->spe_status
);
2498 static always_inline
uint64_t _do_efdctuf (uint64_t val
)
2507 /* NaN are not treated the same way IEEE 754 does */
2508 if (unlikely(isnan(u
.f
)))
2510 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2511 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2513 return float64_to_uint32(u
.f
, &env
->spe_status
);
2516 static always_inline
int64_t _do_efdctsfz (uint64_t val
)
2525 /* NaN are not treated the same way IEEE 754 does */
2526 if (unlikely(isnan(u
.f
)))
2528 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2529 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2531 return float64_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
2534 static always_inline
uint64_t _do_efdctufz (uint64_t val
)
2543 /* NaN are not treated the same way IEEE 754 does */
2544 if (unlikely(isnan(u
.f
)))
2546 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2547 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2549 return float64_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2552 void do_efdcfsf (void)
2554 T0_64
= _do_efdcfsf(T0_64
);
2557 void do_efdcfuf (void)
2559 T0_64
= _do_efdcfuf(T0_64
);
2562 void do_efdctsf (void)
2564 T0_64
= _do_efdctsf(T0_64
);
2567 void do_efdctuf (void)
2569 T0_64
= _do_efdctuf(T0_64
);
2572 void do_efdctsfz (void)
2574 T0_64
= _do_efdctsfz(T0_64
);
2577 void do_efdctufz (void)
2579 T0_64
= _do_efdctufz(T0_64
);
2582 /* Floating point conversion between single and double precision */
2583 static always_inline
uint32_t _do_efscfd (uint64_t val
)
2595 u2
.f
= float64_to_float32(u1
.f
, &env
->spe_status
);
2600 static always_inline
uint64_t _do_efdcfs (uint32_t val
)
2612 u2
.f
= float32_to_float64(u1
.f
, &env
->spe_status
);
2617 void do_efscfd (void)
2619 T0_64
= _do_efscfd(T0_64
);
2622 void do_efdcfs (void)
2624 T0_64
= _do_efdcfs(T0_64
);
2627 /* Single precision fixed-point vector arithmetic */
2643 /* Single-precision floating-point comparisons */
2644 static always_inline
int _do_efscmplt (uint32_t op1
, uint32_t op2
)
2646 /* XXX: TODO: test special values (NaN, infinites, ...) */
2647 return _do_efststlt(op1
, op2
);
2650 static always_inline
int _do_efscmpgt (uint32_t op1
, uint32_t op2
)
2652 /* XXX: TODO: test special values (NaN, infinites, ...) */
2653 return _do_efststgt(op1
, op2
);
2656 static always_inline
int _do_efscmpeq (uint32_t op1
, uint32_t op2
)
2658 /* XXX: TODO: test special values (NaN, infinites, ...) */
2659 return _do_efststeq(op1
, op2
);
2662 void do_efscmplt (void)
2664 T0
= _do_efscmplt(T0_64
, T1_64
);
2667 void do_efscmpgt (void)
2669 T0
= _do_efscmpgt(T0_64
, T1_64
);
2672 void do_efscmpeq (void)
2674 T0
= _do_efscmpeq(T0_64
, T1_64
);
2677 /* Single-precision floating-point vector comparisons */
2679 DO_SPE_CMP(fscmplt
);
2681 DO_SPE_CMP(fscmpgt
);
2683 DO_SPE_CMP(fscmpeq
);
2685 DO_SPE_CMP(fststlt
);
2687 DO_SPE_CMP(fststgt
);
2689 DO_SPE_CMP(fststeq
);
2691 /* Single-precision floating-point vector conversions */
2705 DO_SPE_OP1(fsctsiz
);
2707 DO_SPE_OP1(fsctuiz
);
2713 /*****************************************************************************/
2714 /* Softmmu support */
2715 #if !defined (CONFIG_USER_ONLY)
2717 #define MMUSUFFIX _mmu
2719 # define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
2721 # define GETPC() (__builtin_return_address(0))
2725 #include "softmmu_template.h"
2728 #include "softmmu_template.h"
2731 #include "softmmu_template.h"
2734 #include "softmmu_template.h"
2736 /* try to fill the TLB and return an exception if error. If retaddr is
2737 NULL, it means that the function was called in C code (i.e. not
2738 from generated code or from helper.c) */
2739 /* XXX: fix it to restore all registers */
2740 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
2742 TranslationBlock
*tb
;
2743 CPUState
*saved_env
;
2747 /* XXX: hack to restore env in all cases, even if not called from
2750 env
= cpu_single_env
;
2751 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
2752 if (unlikely(ret
!= 0)) {
2753 if (likely(retaddr
)) {
2754 /* now we have a real cpu fault */
2755 pc
= (unsigned long)retaddr
;
2756 tb
= tb_find_pc(pc
);
2758 /* the PC is inside the translated code. It means that we have
2759 a virtual CPU fault */
2760 cpu_restore_state(tb
, env
, pc
, NULL
);
2763 do_raise_exception_err(env
->exception_index
, env
->error_code
);
2768 /* Software driven TLBs management */
2769 /* PowerPC 602/603 software TLB load instructions helpers */
2770 void do_load_6xx_tlb (int is_code
)
2772 target_ulong RPN
, CMP
, EPN
;
2775 RPN
= env
->spr
[SPR_RPA
];
2777 CMP
= env
->spr
[SPR_ICMP
];
2778 EPN
= env
->spr
[SPR_IMISS
];
2780 CMP
= env
->spr
[SPR_DCMP
];
2781 EPN
= env
->spr
[SPR_DMISS
];
2783 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2784 #if defined (DEBUG_SOFTWARE_TLB)
2785 if (loglevel
!= 0) {
2786 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2787 " PTE1 " ADDRX
" way %d\n",
2788 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2791 /* Store this TLB */
2792 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2793 way
, is_code
, CMP
, RPN
);
2796 void do_load_74xx_tlb (int is_code
)
2798 target_ulong RPN
, CMP
, EPN
;
2801 RPN
= env
->spr
[SPR_PTELO
];
2802 CMP
= env
->spr
[SPR_PTEHI
];
2803 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2804 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2805 #if defined (DEBUG_SOFTWARE_TLB)
2806 if (loglevel
!= 0) {
2807 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2808 " PTE1 " ADDRX
" way %d\n",
2809 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2812 /* Store this TLB */
2813 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2814 way
, is_code
, CMP
, RPN
);
2817 static always_inline target_ulong
booke_tlb_to_page_size (int size
)
2819 return 1024 << (2 * size
);
2822 static always_inline
int booke_page_size_to_tlb (target_ulong page_size
)
2826 switch (page_size
) {
2860 #if defined (TARGET_PPC64)
2861 case 0x000100000000ULL
:
2864 case 0x000400000000ULL
:
2867 case 0x001000000000ULL
:
2870 case 0x004000000000ULL
:
2873 case 0x010000000000ULL
:
2885 /* Helpers for 4xx TLB management */
2886 void do_4xx_tlbre_lo (void)
2892 tlb
= &env
->tlb
[T0
].tlbe
;
2894 if (tlb
->prot
& PAGE_VALID
)
2896 size
= booke_page_size_to_tlb(tlb
->size
);
2897 if (size
< 0 || size
> 0x7)
2900 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2903 void do_4xx_tlbre_hi (void)
2908 tlb
= &env
->tlb
[T0
].tlbe
;
2910 if (tlb
->prot
& PAGE_EXEC
)
2912 if (tlb
->prot
& PAGE_WRITE
)
2916 void do_4xx_tlbwe_hi (void)
2919 target_ulong page
, end
;
2921 #if defined (DEBUG_SOFTWARE_TLB)
2922 if (loglevel
!= 0) {
2923 fprintf(logfile
, "%s T0 " TDX
" T1 " TDX
"\n", __func__
, T0
, T1
);
2927 tlb
= &env
->tlb
[T0
].tlbe
;
2928 /* Invalidate previous TLB (if it's valid) */
2929 if (tlb
->prot
& PAGE_VALID
) {
2930 end
= tlb
->EPN
+ tlb
->size
;
2931 #if defined (DEBUG_SOFTWARE_TLB)
2932 if (loglevel
!= 0) {
2933 fprintf(logfile
, "%s: invalidate old TLB %d start " ADDRX
2934 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2937 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2938 tlb_flush_page(env
, page
);
2940 tlb
->size
= booke_tlb_to_page_size((T1
>> 7) & 0x7);
2941 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2942 * If this ever occurs, one should use the ppcemb target instead
2943 * of the ppc or ppc64 one
2945 if ((T1
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
2946 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
2947 "are not supported (%d)\n",
2948 tlb
->size
, TARGET_PAGE_SIZE
, (int)((T1
>> 7) & 0x7));
2950 tlb
->EPN
= T1
& ~(tlb
->size
- 1);
2952 tlb
->prot
|= PAGE_VALID
;
2954 tlb
->prot
&= ~PAGE_VALID
;
2956 /* XXX: TO BE FIXED */
2957 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
2959 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2960 tlb
->attr
= T1
& 0xFF;
2961 #if defined (DEBUG_SOFTWARE_TLB)
2962 if (loglevel
!= 0) {
2963 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2964 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2965 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2966 tlb
->prot
& PAGE_READ
? 'r' : '-',
2967 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2968 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2969 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2972 /* Invalidate new TLB (if valid) */
2973 if (tlb
->prot
& PAGE_VALID
) {
2974 end
= tlb
->EPN
+ tlb
->size
;
2975 #if defined (DEBUG_SOFTWARE_TLB)
2976 if (loglevel
!= 0) {
2977 fprintf(logfile
, "%s: invalidate TLB %d start " ADDRX
2978 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2981 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2982 tlb_flush_page(env
, page
);
2986 void do_4xx_tlbwe_lo (void)
2990 #if defined (DEBUG_SOFTWARE_TLB)
2991 if (loglevel
!= 0) {
2992 fprintf(logfile
, "%s T0 " TDX
" T1 " TDX
"\n", __func__
, T0
, T1
);
2996 tlb
= &env
->tlb
[T0
].tlbe
;
2997 tlb
->RPN
= T1
& 0xFFFFFC00;
2998 tlb
->prot
= PAGE_READ
;
3000 tlb
->prot
|= PAGE_EXEC
;
3002 tlb
->prot
|= PAGE_WRITE
;
3003 #if defined (DEBUG_SOFTWARE_TLB)
3004 if (loglevel
!= 0) {
3005 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
3006 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
3007 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
3008 tlb
->prot
& PAGE_READ
? 'r' : '-',
3009 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
3010 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
3011 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
3016 /* PowerPC 440 TLB management */
3017 void do_440_tlbwe (int word
)
3020 target_ulong EPN
, RPN
, size
;
3023 #if defined (DEBUG_SOFTWARE_TLB)
3024 if (loglevel
!= 0) {
3025 fprintf(logfile
, "%s word %d T0 " TDX
" T1 " TDX
"\n",
3026 __func__
, word
, T0
, T1
);
3031 tlb
= &env
->tlb
[T0
].tlbe
;
3034 /* Just here to please gcc */
3036 EPN
= T1
& 0xFFFFFC00;
3037 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
3040 size
= booke_tlb_to_page_size((T1
>> 4) & 0xF);
3041 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
3045 tlb
->attr
|= (T1
>> 8) & 1;
3047 tlb
->prot
|= PAGE_VALID
;
3049 if (tlb
->prot
& PAGE_VALID
) {
3050 tlb
->prot
&= ~PAGE_VALID
;
3054 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
3059 RPN
= T1
& 0xFFFFFC0F;
3060 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
3065 tlb
->attr
= (tlb
->attr
& 0x1) | (T1
& 0x0000FF00);
3066 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
3068 tlb
->prot
|= PAGE_READ
<< 4;
3070 tlb
->prot
|= PAGE_WRITE
<< 4;
3072 tlb
->prot
|= PAGE_EXEC
<< 4;
3074 tlb
->prot
|= PAGE_READ
;
3076 tlb
->prot
|= PAGE_WRITE
;
3078 tlb
->prot
|= PAGE_EXEC
;
3083 void do_440_tlbre (int word
)
3089 tlb
= &env
->tlb
[T0
].tlbe
;
3092 /* Just here to please gcc */
3095 size
= booke_page_size_to_tlb(tlb
->size
);
3096 if (size
< 0 || size
> 0xF)
3099 if (tlb
->attr
& 0x1)
3101 if (tlb
->prot
& PAGE_VALID
)
3103 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
3104 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
3110 T0
= tlb
->attr
& ~0x1;
3111 if (tlb
->prot
& (PAGE_READ
<< 4))
3113 if (tlb
->prot
& (PAGE_WRITE
<< 4))
3115 if (tlb
->prot
& (PAGE_EXEC
<< 4))
3117 if (tlb
->prot
& PAGE_READ
)
3119 if (tlb
->prot
& PAGE_WRITE
)
3121 if (tlb
->prot
& PAGE_EXEC
)
3126 #endif /* !CONFIG_USER_ONLY */