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
22 #include "op_helper.h"
24 #define MEMSUFFIX _raw
25 #include "op_helper.h"
26 #include "op_helper_mem.h"
27 #if !defined(CONFIG_USER_ONLY)
28 #define MEMSUFFIX _user
29 #include "op_helper.h"
30 #include "op_helper_mem.h"
31 #define MEMSUFFIX _kernel
32 #include "op_helper.h"
33 #include "op_helper_mem.h"
37 //#define DEBUG_EXCEPTIONS
38 //#define DEBUG_SOFTWARE_TLB
39 //#define FLUSH_ALL_TLBS
41 /*****************************************************************************/
42 /* Exceptions processing helpers */
43 void cpu_loop_exit (void)
45 longjmp(env
->jmp_env
, 1);
48 void do_raise_exception_err (uint32_t exception
, int error_code
)
51 printf("Raise exception %3x code : %d\n", exception
, error_code
);
55 if (error_code
== EXCP_FP
&& msr_fe0
== 0 && msr_fe1
== 0)
61 env
->exception_index
= exception
;
62 env
->error_code
= error_code
;
66 void do_raise_exception (uint32_t exception
)
68 do_raise_exception_err(exception
, 0);
71 /*****************************************************************************/
72 /* Registers load and stores */
73 void do_load_cr (void)
75 T0
= (env
->crf
[0] << 28) |
85 void do_store_cr (uint32_t mask
)
89 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
91 env
->crf
[i
] = (T0
>> (sh
* 4)) & 0xFUL
;
95 void do_load_xer (void)
97 T0
= (xer_so
<< XER_SO
) |
101 (xer_cmp
<< XER_CMP
);
104 void do_store_xer (void)
106 xer_so
= (T0
>> XER_SO
) & 0x01;
107 xer_ov
= (T0
>> XER_OV
) & 0x01;
108 xer_ca
= (T0
>> XER_CA
) & 0x01;
109 xer_cmp
= (T0
>> XER_CMP
) & 0xFF;
110 xer_bc
= (T0
>> XER_BC
) & 0x7F;
113 void do_load_fpscr (void)
115 /* The 32 MSB of the target fpr are undefined.
126 #if defined(WORDS_BIGENDIAN)
135 for (i
= 0; i
< 8; i
++)
136 u
.s
.u
[WORD1
] |= env
->fpscr
[i
] << (4 * i
);
140 void do_store_fpscr (uint32_t mask
)
143 * We use only the 32 LSB of the incoming fpr
155 env
->fpscr
[0] = (env
->fpscr
[0] & 0x9) | ((u
.s
.u
[WORD1
] >> 28) & ~0x9);
156 for (i
= 1; i
< 7; i
++) {
157 if (mask
& (1 << (7 - i
)))
158 env
->fpscr
[i
] = (u
.s
.u
[WORD1
] >> (4 * (7 - i
))) & 0xF;
160 /* TODO: update FEX & VX */
161 /* Set rounding mode */
162 switch (env
->fpscr
[0] & 0x3) {
164 /* Best approximation (round to nearest) */
165 rnd_type
= float_round_nearest_even
;
168 /* Smaller magnitude (round toward zero) */
169 rnd_type
= float_round_to_zero
;
172 /* Round toward +infinite */
173 rnd_type
= float_round_up
;
177 /* Round toward -infinite */
178 rnd_type
= float_round_down
;
181 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
184 /*****************************************************************************/
185 /* Fixed point operations helpers */
186 #if defined(TARGET_PPC64)
187 static void add128 (uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
196 static void neg128 (uint64_t *plow
, uint64_t *phigh
)
200 add128(plow
, phigh
, 1, 0);
203 static void mul64 (uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
205 uint32_t a0
, a1
, b0
, b1
;
214 v
= (uint64_t)a0
* (uint64_t)b0
;
218 v
= (uint64_t)a0
* (uint64_t)b1
;
219 add128(plow
, phigh
, v
<< 32, v
>> 32);
221 v
= (uint64_t)a1
* (uint64_t)b0
;
222 add128(plow
, phigh
, v
<< 32, v
>> 32);
224 v
= (uint64_t)a1
* (uint64_t)b1
;
226 #if defined(DEBUG_MULDIV)
227 printf("mul: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
228 a
, b
, *phigh
, *plow
);
232 void do_mul64 (uint64_t *plow
, uint64_t *phigh
)
234 mul64(plow
, phigh
, T0
, T1
);
237 static void imul64 (uint64_t *plow
, uint64_t *phigh
, int64_t a
, int64_t b
)
246 mul64(plow
, phigh
, a
, b
);
252 void do_imul64 (uint64_t *plow
, uint64_t *phigh
)
254 imul64(plow
, phigh
, T0
, T1
);
262 if (likely(!((uint32_t)T0
< (uint32_t)T2
||
263 (xer_ca
== 1 && (uint32_t)T0
== (uint32_t)T2
)))) {
270 #if defined(TARGET_PPC64)
271 void do_adde_64 (void)
275 if (likely(!((uint64_t)T0
< (uint64_t)T2
||
276 (xer_ca
== 1 && (uint64_t)T0
== (uint64_t)T2
)))) {
284 void do_addmeo (void)
288 if (likely(!((uint32_t)T1
&
289 ((uint32_t)T1
^ (uint32_t)T0
) & (1UL << 31)))) {
299 #if defined(TARGET_PPC64)
300 void do_addmeo_64 (void)
304 if (likely(!((uint64_t)T1
&
305 ((uint64_t)T1
^ (uint64_t)T0
) & (1ULL << 63)))) {
318 if (likely(!(((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) ||
319 (int32_t)T1
== 0))) {
321 T0
= (int32_t)T0
/ (int32_t)T1
;
325 T0
= (-1) * ((uint32_t)T0
>> 31);
329 #if defined(TARGET_PPC64)
332 if (likely(!(((int64_t)T0
== INT64_MIN
&& (int64_t)T1
== -1ULL) ||
333 (int64_t)T1
== 0))) {
335 T0
= (int64_t)T0
/ (int64_t)T1
;
339 T0
= (-1ULL) * ((uint64_t)T0
>> 63);
344 void do_divwuo (void)
346 if (likely((uint32_t)T1
!= 0)) {
348 T0
= (uint32_t)T0
/ (uint32_t)T1
;
356 #if defined(TARGET_PPC64)
357 void do_divduo (void)
359 if (likely((uint64_t)T1
!= 0)) {
361 T0
= (uint64_t)T0
/ (uint64_t)T1
;
370 void do_mullwo (void)
372 int64_t res
= (int64_t)T0
* (int64_t)T1
;
374 if (likely((int32_t)res
== res
)) {
383 #if defined(TARGET_PPC64)
384 void do_mulldo (void)
390 if (likely(th
== 0)) {
402 if (likely((int32_t)T0
!= INT32_MIN
)) {
411 #if defined(TARGET_PPC64)
412 void do_nego_64 (void)
414 if (likely((int64_t)T0
!= INT64_MIN
)) {
426 T0
= T1
+ ~T0
+ xer_ca
;
427 if (likely((uint32_t)T0
>= (uint32_t)T1
&&
428 (xer_ca
== 0 || (uint32_t)T0
!= (uint32_t)T1
))) {
435 #if defined(TARGET_PPC64)
436 void do_subfe_64 (void)
438 T0
= T1
+ ~T0
+ xer_ca
;
439 if (likely((uint64_t)T0
>= (uint64_t)T1
&&
440 (xer_ca
== 0 || (uint64_t)T0
!= (uint64_t)T1
))) {
448 void do_subfmeo (void)
451 T0
= ~T0
+ xer_ca
- 1;
452 if (likely(!((uint32_t)~T1
& ((uint32_t)~T1
^ (uint32_t)T0
) &
459 if (likely((uint32_t)T1
!= UINT32_MAX
))
463 #if defined(TARGET_PPC64)
464 void do_subfmeo_64 (void)
467 T0
= ~T0
+ xer_ca
- 1;
468 if (likely(!((uint64_t)~T1
& ((uint64_t)~T1
^ (uint64_t)T0
) &
475 if (likely((uint64_t)T1
!= UINT64_MAX
))
480 void do_subfzeo (void)
484 if (likely(!(((uint32_t)~T1
^ UINT32_MAX
) &
485 ((uint32_t)(~T1
) ^ (uint32_t)T0
) & (1UL << 31)))) {
491 if (likely((uint32_t)T0
>= (uint32_t)~T1
)) {
498 #if defined(TARGET_PPC64)
499 void do_subfzeo_64 (void)
503 if (likely(!(((uint64_t)~T1
^ UINT64_MAX
) &
504 ((uint64_t)(~T1
) ^ (uint64_t)T0
) & (1ULL << 63)))) {
510 if (likely((uint64_t)T0
>= (uint64_t)~T1
)) {
518 /* shift right arithmetic helper */
523 if (likely(!(T1
& 0x20UL
))) {
524 if (likely((uint32_t)T1
!= 0)) {
525 ret
= (int32_t)T0
>> (T1
& 0x1fUL
);
526 if (likely(ret
>= 0 || ((int32_t)T0
& ((1 << T1
) - 1)) == 0)) {
536 ret
= (-1) * ((uint32_t)T0
>> 31);
537 if (likely(ret
>= 0 || ((uint32_t)T0
& ~0x80000000UL
) == 0)) {
546 #if defined(TARGET_PPC64)
551 if (likely(!(T1
& 0x40UL
))) {
552 if (likely((uint64_t)T1
!= 0)) {
553 ret
= (int64_t)T0
>> (T1
& 0x3FUL
);
554 if (likely(ret
>= 0 || ((int64_t)T0
& ((1 << T1
) - 1)) == 0)) {
564 ret
= (-1) * ((uint64_t)T0
>> 63);
565 if (likely(ret
>= 0 || ((uint64_t)T0
& ~0x8000000000000000ULL
) == 0)) {
575 static inline int popcnt (uint32_t val
)
579 for (i
= 0; val
!= 0;)
580 val
= val
^ (val
- 1);
585 void do_popcntb (void)
591 for (i
= 0; i
< 32; i
+= 8)
592 ret
|= popcnt((T0
>> i
) & 0xFF) << i
;
596 #if defined(TARGET_PPC64)
597 void do_popcntb_64 (void)
603 for (i
= 0; i
< 64; i
+= 8)
604 ret
|= popcnt((T0
>> i
) & 0xFF) << i
;
609 /*****************************************************************************/
610 /* Floating point operations helpers */
618 p
.i
= float64_to_int32(FT0
, &env
->fp_status
);
619 #if USE_PRECISE_EMULATION
620 /* XXX: higher bits are not supposed to be significant.
621 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
623 p
.i
|= 0xFFF80000ULL
<< 32;
628 void do_fctiwz (void)
635 p
.i
= float64_to_int32_round_to_zero(FT0
, &env
->fp_status
);
636 #if USE_PRECISE_EMULATION
637 /* XXX: higher bits are not supposed to be significant.
638 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
640 p
.i
|= 0xFFF80000ULL
<< 32;
645 #if defined(TARGET_PPC64)
654 FT0
= int64_to_float64(p
.i
, &env
->fp_status
);
664 p
.i
= float64_to_int64(FT0
, &env
->fp_status
);
668 void do_fctidz (void)
675 p
.i
= float64_to_int64_round_to_zero(FT0
, &env
->fp_status
);
681 #if USE_PRECISE_EMULATION
685 float128 ft0_128
, ft1_128
;
687 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
688 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
689 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
690 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
691 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
692 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
694 /* This is OK on x86 hosts */
695 FT0
= (FT0
* FT1
) + FT2
;
702 float128 ft0_128
, ft1_128
;
704 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
705 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
706 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
707 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
708 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
709 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
711 /* This is OK on x86 hosts */
712 FT0
= (FT0
* FT1
) - FT2
;
715 #endif /* USE_PRECISE_EMULATION */
717 void do_fnmadd (void)
719 #if USE_PRECISE_EMULATION
721 float128 ft0_128
, ft1_128
;
723 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
724 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
725 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
726 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
727 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
728 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
730 /* This is OK on x86 hosts */
731 FT0
= (FT0
* FT1
) + FT2
;
734 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
735 FT0
= float64_add(FT0
, FT2
, &env
->fp_status
);
737 if (likely(!isnan(FT0
)))
738 FT0
= float64_chs(FT0
);
741 void do_fnmsub (void)
743 #if USE_PRECISE_EMULATION
745 float128 ft0_128
, ft1_128
;
747 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
748 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
749 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
750 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
751 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
752 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
754 /* This is OK on x86 hosts */
755 FT0
= (FT0
* FT1
) - FT2
;
758 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
759 FT0
= float64_sub(FT0
, FT2
, &env
->fp_status
);
761 if (likely(!isnan(FT0
)))
762 FT0
= float64_chs(FT0
);
767 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
777 if (likely(isnormal(FT0
))) {
778 #if USE_PRECISE_EMULATION
779 FT0
= float64_div(1.0, FT0
, &env
->fp_status
);
780 FT0
= float64_to_float32(FT0
, &env
->fp_status
);
782 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
786 if (p
.i
== 0x8000000000000000ULL
) {
787 p
.i
= 0xFFF0000000000000ULL
;
788 } else if (p
.i
== 0x0000000000000000ULL
) {
789 p
.i
= 0x7FF0000000000000ULL
;
790 } else if (isnan(FT0
)) {
791 p
.i
= 0x7FF8000000000000ULL
;
792 } else if (FT0
< 0.0) {
793 p
.i
= 0x8000000000000000ULL
;
795 p
.i
= 0x0000000000000000ULL
;
801 void do_frsqrte (void)
808 if (likely(isnormal(FT0
) && FT0
> 0.0)) {
809 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
810 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
813 if (p
.i
== 0x8000000000000000ULL
) {
814 p
.i
= 0xFFF0000000000000ULL
;
815 } else if (p
.i
== 0x0000000000000000ULL
) {
816 p
.i
= 0x7FF0000000000000ULL
;
817 } else if (isnan(FT0
)) {
818 if (!(p
.i
& 0x0008000000000000ULL
))
819 p
.i
|= 0x000FFFFFFFFFFFFFULL
;
820 } else if (FT0
< 0) {
821 p
.i
= 0x7FF8000000000000ULL
;
823 p
.i
= 0x0000000000000000ULL
;
839 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
840 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
842 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
849 env
->fpscr
[4] |= 0x1;
850 env
->fpscr
[6] |= 0x1;
857 env
->fpscr
[4] &= ~0x1;
858 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
859 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
861 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
868 env
->fpscr
[4] |= 0x1;
869 if (!float64_is_signaling_nan(FT0
) || !float64_is_signaling_nan(FT1
)) {
871 env
->fpscr
[6] |= 0x1;
872 if (!(env
->fpscr
[1] & 0x8))
873 env
->fpscr
[4] |= 0x8;
875 env
->fpscr
[4] |= 0x8;
881 #if !defined (CONFIG_USER_ONLY)
884 #if defined(TARGET_PPC64)
885 if (env
->spr
[SPR_SRR1
] & (1ULL << MSR_SF
)) {
886 env
->nip
= (uint64_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
887 do_store_msr(env
, (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
889 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
890 ppc_store_msr_32(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
893 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
894 do_store_msr(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
896 #if defined (DEBUG_OP)
899 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
902 #if defined(TARGET_PPC64)
905 if (env
->spr
[SPR_SRR1
] & (1ULL << MSR_SF
)) {
906 env
->nip
= (uint64_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
907 do_store_msr(env
, (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
909 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
910 do_store_msr(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
912 #if defined (DEBUG_OP)
915 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
920 void do_tw (int flags
)
922 if (!likely(!(((int32_t)T0
< (int32_t)T1
&& (flags
& 0x10)) ||
923 ((int32_t)T0
> (int32_t)T1
&& (flags
& 0x08)) ||
924 ((int32_t)T0
== (int32_t)T1
&& (flags
& 0x04)) ||
925 ((uint32_t)T0
< (uint32_t)T1
&& (flags
& 0x02)) ||
926 ((uint32_t)T0
> (uint32_t)T1
&& (flags
& 0x01))))) {
927 do_raise_exception_err(EXCP_PROGRAM
, EXCP_TRAP
);
931 #if defined(TARGET_PPC64)
932 void do_td (int flags
)
934 if (!likely(!(((int64_t)T0
< (int64_t)T1
&& (flags
& 0x10)) ||
935 ((int64_t)T0
> (int64_t)T1
&& (flags
& 0x08)) ||
936 ((int64_t)T0
== (int64_t)T1
&& (flags
& 0x04)) ||
937 ((uint64_t)T0
< (uint64_t)T1
&& (flags
& 0x02)) ||
938 ((uint64_t)T0
> (uint64_t)T1
&& (flags
& 0x01)))))
939 do_raise_exception_err(EXCP_PROGRAM
, EXCP_TRAP
);
943 /*****************************************************************************/
944 /* PowerPC 601 specific instructions (POWER bridge) */
945 void do_POWER_abso (void)
947 if ((uint32_t)T0
== INT32_MIN
) {
957 void do_POWER_clcs (void)
961 /* Instruction cache line size */
962 T0
= ICACHE_LINE_SIZE
;
965 /* Data cache line size */
966 T0
= DCACHE_LINE_SIZE
;
969 /* Minimum cache line size */
970 T0
= ICACHE_LINE_SIZE
< DCACHE_LINE_SIZE
?
971 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
974 /* Maximum cache line size */
975 T0
= ICACHE_LINE_SIZE
> DCACHE_LINE_SIZE
?
976 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
984 void do_POWER_div (void)
988 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
989 T0
= (long)((-1) * (T0
>> 31));
990 env
->spr
[SPR_MQ
] = 0;
992 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
993 env
->spr
[SPR_MQ
] = tmp
% T1
;
994 T0
= tmp
/ (int32_t)T1
;
998 void do_POWER_divo (void)
1002 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1003 T0
= (long)((-1) * (T0
>> 31));
1004 env
->spr
[SPR_MQ
] = 0;
1008 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1009 env
->spr
[SPR_MQ
] = tmp
% T1
;
1011 if (tmp
> (int64_t)INT32_MAX
|| tmp
< (int64_t)INT32_MIN
) {
1021 void do_POWER_divs (void)
1023 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1024 T0
= (long)((-1) * (T0
>> 31));
1025 env
->spr
[SPR_MQ
] = 0;
1027 env
->spr
[SPR_MQ
] = T0
% T1
;
1028 T0
= (int32_t)T0
/ (int32_t)T1
;
1032 void do_POWER_divso (void)
1034 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1035 T0
= (long)((-1) * (T0
>> 31));
1036 env
->spr
[SPR_MQ
] = 0;
1040 T0
= (int32_t)T0
/ (int32_t)T1
;
1041 env
->spr
[SPR_MQ
] = (int32_t)T0
% (int32_t)T1
;
1046 void do_POWER_dozo (void)
1048 if ((int32_t)T1
> (int32_t)T0
) {
1051 if (((uint32_t)(~T2
) ^ (uint32_t)T1
^ UINT32_MAX
) &
1052 ((uint32_t)(~T2
) ^ (uint32_t)T0
) & (1UL << 31)) {
1064 void do_POWER_maskg (void)
1068 if ((uint32_t)T0
== (uint32_t)(T1
+ 1)) {
1071 ret
= (((uint32_t)(-1)) >> ((uint32_t)T0
)) ^
1072 (((uint32_t)(-1) >> ((uint32_t)T1
)) >> 1);
1073 if ((uint32_t)T0
> (uint32_t)T1
)
1079 void do_POWER_mulo (void)
1083 tmp
= (uint64_t)T0
* (uint64_t)T1
;
1084 env
->spr
[SPR_MQ
] = tmp
>> 32;
1086 if (tmp
>> 32 != ((uint64_t)T0
>> 16) * ((uint64_t)T1
>> 16)) {
1094 #if !defined (CONFIG_USER_ONLY)
1095 void do_POWER_rac (void)
1100 /* We don't have to generate many instances of this instruction,
1101 * as rac is supervisor only.
1103 if (get_physical_address(env
, &ctx
, T0
, 0, ACCESS_INT
, 1) == 0)
1108 void do_POWER_rfsvc (void)
1110 env
->nip
= env
->lr
& ~0x00000003UL
;
1111 T0
= env
->ctr
& 0x0000FFFFUL
;
1112 do_store_msr(env
, T0
);
1113 #if defined (DEBUG_OP)
1116 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1119 /* PowerPC 601 BAT management helper */
1120 void do_store_601_batu (int nr
)
1122 do_store_ibatu(env
, nr
, (uint32_t)T0
);
1123 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1124 env
->DBAT
[1][nr
] = env
->IBAT
[1][nr
];
1128 /*****************************************************************************/
1129 /* 602 specific instructions */
1130 /* mfrom is the most crazy instruction ever seen, imho ! */
1131 /* Real implementation uses a ROM table. Do the same */
1132 #define USE_MFROM_ROM_TABLE
1133 void do_op_602_mfrom (void)
1135 if (likely(T0
< 602)) {
1136 #if defined(USE_MFROM_ROM_TABLE)
1137 #include "mfrom_table.c"
1138 T0
= mfrom_ROM_table
[T0
];
1141 /* Extremly decomposed:
1143 * T0 = 256 * log10(10 + 1.0) + 0.5
1146 d
= float64_div(d
, 256, &env
->fp_status
);
1148 d
= exp10(d
); // XXX: use float emulation function
1149 d
= float64_add(d
, 1.0, &env
->fp_status
);
1150 d
= log10(d
); // XXX: use float emulation function
1151 d
= float64_mul(d
, 256, &env
->fp_status
);
1152 d
= float64_add(d
, 0.5, &env
->fp_status
);
1153 T0
= float64_round_to_int(d
, &env
->fp_status
);
1160 /*****************************************************************************/
1161 /* Embedded PowerPC specific helpers */
1162 void do_405_check_ov (void)
1164 if (likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1165 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1173 void do_405_check_sat (void)
1175 if (!likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1176 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1177 /* Saturate result */
1186 #if !defined(CONFIG_USER_ONLY)
1187 void do_40x_rfci (void)
1189 env
->nip
= env
->spr
[SPR_40x_SRR2
];
1190 do_store_msr(env
, env
->spr
[SPR_40x_SRR3
] & ~0xFFFF0000);
1191 #if defined (DEBUG_OP)
1194 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1199 #if defined(TARGET_PPC64)
1200 if (env
->spr
[SPR_BOOKE_CSRR1
] & (1 << MSR_CM
)) {
1201 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_CSRR0
];
1205 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_CSRR0
];
1207 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_CSRR1
] & ~0x3FFF0000);
1208 #if defined (DEBUG_OP)
1211 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1216 #if defined(TARGET_PPC64)
1217 if (env
->spr
[SPR_BOOKE_DSRR1
] & (1 << MSR_CM
)) {
1218 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_DSRR0
];
1222 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_DSRR0
];
1224 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_DSRR1
] & ~0x3FFF0000);
1225 #if defined (DEBUG_OP)
1228 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1231 void do_rfmci (void)
1233 #if defined(TARGET_PPC64)
1234 if (env
->spr
[SPR_BOOKE_MCSRR1
] & (1 << MSR_CM
)) {
1235 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_MCSRR0
];
1239 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_MCSRR0
];
1241 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_MCSRR1
] & ~0x3FFF0000);
1242 #if defined (DEBUG_OP)
1245 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1248 void do_load_dcr (void)
1252 if (unlikely(env
->dcr_env
== NULL
)) {
1253 printf("No DCR environment\n");
1254 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_INVAL_INVAL
);
1255 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, T0
, &val
) != 0)) {
1256 printf("DCR read error\n");
1257 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_PRIV_REG
);
1263 void do_store_dcr (void)
1265 if (unlikely(env
->dcr_env
== NULL
)) {
1266 printf("No DCR environment\n");
1267 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_INVAL_INVAL
);
1268 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, T0
, T1
) != 0)) {
1269 printf("DCR write error\n");
1270 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_PRIV_REG
);
1274 void do_load_403_pb (int num
)
1279 void do_store_403_pb (int num
)
1281 if (likely(env
->pb
[num
] != T0
)) {
1283 /* Should be optimized */
1290 void do_440_dlmzb (void)
1296 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1297 if ((T0
& mask
) == 0)
1301 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1302 if ((T1
& mask
) == 0)
1310 #if defined(TARGET_PPCSPE)
1311 /* SPE extension helpers */
1312 /* Use a table to make this quicker */
1313 static uint8_t hbrev
[16] = {
1314 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1315 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1318 static inline uint8_t byte_reverse (uint8_t val
)
1320 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1323 static inline uint32_t word_reverse (uint32_t val
)
1325 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1326 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1329 #define MASKBITS 16 // Random value - to be fixed
1330 void do_brinc (void)
1332 uint32_t a
, b
, d
, mask
;
1334 mask
= (uint32_t)(-1UL) >> MASKBITS
;
1337 d
= word_reverse(1 + word_reverse(a
| ~mask
));
1338 T0_64
= (T0_64
& ~mask
) | (d
& mask
);
1341 #define DO_SPE_OP2(name) \
1342 void do_ev##name (void) \
1344 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) | \
1345 (uint64_t)_do_e##name(T0_64, T1_64); \
1348 #define DO_SPE_OP1(name) \
1349 void do_ev##name (void) \
1351 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) | \
1352 (uint64_t)_do_e##name(T0_64); \
1355 /* Fixed-point vector arithmetic */
1356 static inline uint32_t _do_eabs (uint32_t val
)
1358 if (val
!= 0x80000000)
1364 static inline uint32_t _do_eaddw (uint32_t op1
, uint32_t op2
)
1369 static inline int _do_ecntlsw (uint32_t val
)
1371 if (val
& 0x80000000)
1372 return _do_cntlzw(~val
);
1374 return _do_cntlzw(val
);
1377 static inline int _do_ecntlzw (uint32_t val
)
1379 return _do_cntlzw(val
);
1382 static inline uint32_t _do_eneg (uint32_t val
)
1384 if (val
!= 0x80000000)
1390 static inline uint32_t _do_erlw (uint32_t op1
, uint32_t op2
)
1392 return rotl32(op1
, op2
);
1395 static inline uint32_t _do_erndw (uint32_t val
)
1397 return (val
+ 0x000080000000) & 0xFFFF0000;
1400 static inline uint32_t _do_eslw (uint32_t op1
, uint32_t op2
)
1402 /* No error here: 6 bits are used */
1403 return op1
<< (op2
& 0x3F);
1406 static inline int32_t _do_esrws (int32_t op1
, uint32_t op2
)
1408 /* No error here: 6 bits are used */
1409 return op1
>> (op2
& 0x3F);
1412 static inline uint32_t _do_esrwu (uint32_t op1
, uint32_t op2
)
1414 /* No error here: 6 bits are used */
1415 return op1
>> (op2
& 0x3F);
1418 static inline uint32_t _do_esubfw (uint32_t op1
, uint32_t op2
)
1446 /* evsel is a little bit more complicated... */
1447 static inline uint32_t _do_esel (uint32_t op1
, uint32_t op2
, int n
)
1455 void do_evsel (void)
1457 T0_64
= ((uint64_t)_do_esel(T0_64
>> 32, T1_64
>> 32, T0
>> 3) << 32) |
1458 (uint64_t)_do_esel(T0_64
, T1_64
, (T0
>> 2) & 1);
1461 /* Fixed-point vector comparisons */
1462 #define DO_SPE_CMP(name) \
1463 void do_ev##name (void) \
1465 T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32, \
1466 T1_64 >> 32) << 32, \
1467 _do_e##name(T0_64, T1_64)); \
1470 static inline uint32_t _do_evcmp_merge (int t0
, int t1
)
1472 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1474 static inline int _do_ecmpeq (uint32_t op1
, uint32_t op2
)
1476 return op1
== op2
? 1 : 0;
1479 static inline int _do_ecmpgts (int32_t op1
, int32_t op2
)
1481 return op1
> op2
? 1 : 0;
1484 static inline int _do_ecmpgtu (uint32_t op1
, uint32_t op2
)
1486 return op1
> op2
? 1 : 0;
1489 static inline int _do_ecmplts (int32_t op1
, int32_t op2
)
1491 return op1
< op2
? 1 : 0;
1494 static inline int _do_ecmpltu (uint32_t op1
, uint32_t op2
)
1496 return op1
< op2
? 1 : 0;
1510 /* Single precision floating-point conversions from/to integer */
1511 static inline uint32_t _do_efscfsi (int32_t val
)
1518 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1523 static inline uint32_t _do_efscfui (uint32_t val
)
1530 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1535 static inline int32_t _do_efsctsi (uint32_t val
)
1543 /* NaN are not treated the same way IEEE 754 does */
1544 if (unlikely(isnan(u
.f
)))
1547 return float32_to_int32(u
.f
, &env
->spe_status
);
1550 static inline uint32_t _do_efsctui (uint32_t val
)
1558 /* NaN are not treated the same way IEEE 754 does */
1559 if (unlikely(isnan(u
.f
)))
1562 return float32_to_uint32(u
.f
, &env
->spe_status
);
1565 static inline int32_t _do_efsctsiz (uint32_t val
)
1573 /* NaN are not treated the same way IEEE 754 does */
1574 if (unlikely(isnan(u
.f
)))
1577 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1580 static inline uint32_t _do_efsctuiz (uint32_t val
)
1588 /* NaN are not treated the same way IEEE 754 does */
1589 if (unlikely(isnan(u
.f
)))
1592 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1595 void do_efscfsi (void)
1597 T0_64
= _do_efscfsi(T0_64
);
1600 void do_efscfui (void)
1602 T0_64
= _do_efscfui(T0_64
);
1605 void do_efsctsi (void)
1607 T0_64
= _do_efsctsi(T0_64
);
1610 void do_efsctui (void)
1612 T0_64
= _do_efsctui(T0_64
);
1615 void do_efsctsiz (void)
1617 T0_64
= _do_efsctsiz(T0_64
);
1620 void do_efsctuiz (void)
1622 T0_64
= _do_efsctuiz(T0_64
);
1625 /* Single precision floating-point conversion to/from fractional */
1626 static inline uint32_t _do_efscfsf (uint32_t val
)
1634 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1635 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
1636 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1641 static inline uint32_t _do_efscfuf (uint32_t val
)
1649 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1650 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1651 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1656 static inline int32_t _do_efsctsf (uint32_t val
)
1665 /* NaN are not treated the same way IEEE 754 does */
1666 if (unlikely(isnan(u
.f
)))
1668 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1669 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1671 return float32_to_int32(u
.f
, &env
->spe_status
);
1674 static inline uint32_t _do_efsctuf (uint32_t val
)
1683 /* NaN are not treated the same way IEEE 754 does */
1684 if (unlikely(isnan(u
.f
)))
1686 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1687 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1689 return float32_to_uint32(u
.f
, &env
->spe_status
);
1692 static inline int32_t _do_efsctsfz (uint32_t val
)
1701 /* NaN are not treated the same way IEEE 754 does */
1702 if (unlikely(isnan(u
.f
)))
1704 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1705 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1707 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1710 static inline uint32_t _do_efsctufz (uint32_t val
)
1719 /* NaN are not treated the same way IEEE 754 does */
1720 if (unlikely(isnan(u
.f
)))
1722 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1723 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1725 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1728 void do_efscfsf (void)
1730 T0_64
= _do_efscfsf(T0_64
);
1733 void do_efscfuf (void)
1735 T0_64
= _do_efscfuf(T0_64
);
1738 void do_efsctsf (void)
1740 T0_64
= _do_efsctsf(T0_64
);
1743 void do_efsctuf (void)
1745 T0_64
= _do_efsctuf(T0_64
);
1748 void do_efsctsfz (void)
1750 T0_64
= _do_efsctsfz(T0_64
);
1753 void do_efsctufz (void)
1755 T0_64
= _do_efsctufz(T0_64
);
1758 /* Double precision floating point helpers */
1759 static inline int _do_efdcmplt (uint64_t op1
, uint64_t op2
)
1761 /* XXX: TODO: test special values (NaN, infinites, ...) */
1762 return _do_efdtstlt(op1
, op2
);
1765 static inline int _do_efdcmpgt (uint64_t op1
, uint64_t op2
)
1767 /* XXX: TODO: test special values (NaN, infinites, ...) */
1768 return _do_efdtstgt(op1
, op2
);
1771 static inline int _do_efdcmpeq (uint64_t op1
, uint64_t op2
)
1773 /* XXX: TODO: test special values (NaN, infinites, ...) */
1774 return _do_efdtsteq(op1
, op2
);
1777 void do_efdcmplt (void)
1779 T0
= _do_efdcmplt(T0_64
, T1_64
);
1782 void do_efdcmpgt (void)
1784 T0
= _do_efdcmpgt(T0_64
, T1_64
);
1787 void do_efdcmpeq (void)
1789 T0
= _do_efdcmpeq(T0_64
, T1_64
);
1792 /* Double precision floating-point conversion to/from integer */
1793 static inline uint64_t _do_efdcfsi (int64_t val
)
1800 u
.f
= int64_to_float64(val
, &env
->spe_status
);
1805 static inline uint64_t _do_efdcfui (uint64_t val
)
1812 u
.f
= uint64_to_float64(val
, &env
->spe_status
);
1817 static inline int64_t _do_efdctsi (uint64_t val
)
1825 /* NaN are not treated the same way IEEE 754 does */
1826 if (unlikely(isnan(u
.f
)))
1829 return float64_to_int64(u
.f
, &env
->spe_status
);
1832 static inline uint64_t _do_efdctui (uint64_t val
)
1840 /* NaN are not treated the same way IEEE 754 does */
1841 if (unlikely(isnan(u
.f
)))
1844 return float64_to_uint64(u
.f
, &env
->spe_status
);
1847 static inline int64_t _do_efdctsiz (uint64_t val
)
1855 /* NaN are not treated the same way IEEE 754 does */
1856 if (unlikely(isnan(u
.f
)))
1859 return float64_to_int64_round_to_zero(u
.f
, &env
->spe_status
);
1862 static inline uint64_t _do_efdctuiz (uint64_t val
)
1870 /* NaN are not treated the same way IEEE 754 does */
1871 if (unlikely(isnan(u
.f
)))
1874 return float64_to_uint64_round_to_zero(u
.f
, &env
->spe_status
);
1877 void do_efdcfsi (void)
1879 T0_64
= _do_efdcfsi(T0_64
);
1882 void do_efdcfui (void)
1884 T0_64
= _do_efdcfui(T0_64
);
1887 void do_efdctsi (void)
1889 T0_64
= _do_efdctsi(T0_64
);
1892 void do_efdctui (void)
1894 T0_64
= _do_efdctui(T0_64
);
1897 void do_efdctsiz (void)
1899 T0_64
= _do_efdctsiz(T0_64
);
1902 void do_efdctuiz (void)
1904 T0_64
= _do_efdctuiz(T0_64
);
1907 /* Double precision floating-point conversion to/from fractional */
1908 static inline uint64_t _do_efdcfsf (int64_t val
)
1916 u
.f
= int32_to_float64(val
, &env
->spe_status
);
1917 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
1918 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
1923 static inline uint64_t _do_efdcfuf (uint64_t val
)
1931 u
.f
= uint32_to_float64(val
, &env
->spe_status
);
1932 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
1933 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
1938 static inline int64_t _do_efdctsf (uint64_t val
)
1947 /* NaN are not treated the same way IEEE 754 does */
1948 if (unlikely(isnan(u
.f
)))
1950 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1951 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1953 return float64_to_int32(u
.f
, &env
->spe_status
);
1956 static inline uint64_t _do_efdctuf (uint64_t val
)
1965 /* NaN are not treated the same way IEEE 754 does */
1966 if (unlikely(isnan(u
.f
)))
1968 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1969 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1971 return float64_to_uint32(u
.f
, &env
->spe_status
);
1974 static inline int64_t _do_efdctsfz (uint64_t val
)
1983 /* NaN are not treated the same way IEEE 754 does */
1984 if (unlikely(isnan(u
.f
)))
1986 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1987 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1989 return float64_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1992 static inline uint64_t _do_efdctufz (uint64_t val
)
2001 /* NaN are not treated the same way IEEE 754 does */
2002 if (unlikely(isnan(u
.f
)))
2004 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2005 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2007 return float64_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2010 void do_efdcfsf (void)
2012 T0_64
= _do_efdcfsf(T0_64
);
2015 void do_efdcfuf (void)
2017 T0_64
= _do_efdcfuf(T0_64
);
2020 void do_efdctsf (void)
2022 T0_64
= _do_efdctsf(T0_64
);
2025 void do_efdctuf (void)
2027 T0_64
= _do_efdctuf(T0_64
);
2030 void do_efdctsfz (void)
2032 T0_64
= _do_efdctsfz(T0_64
);
2035 void do_efdctufz (void)
2037 T0_64
= _do_efdctufz(T0_64
);
2040 /* Floating point conversion between single and double precision */
2041 static inline uint32_t _do_efscfd (uint64_t val
)
2053 u2
.f
= float64_to_float32(u1
.f
, &env
->spe_status
);
2058 static inline uint64_t _do_efdcfs (uint32_t val
)
2070 u2
.f
= float32_to_float64(u1
.f
, &env
->spe_status
);
2075 void do_efscfd (void)
2077 T0_64
= _do_efscfd(T0_64
);
2080 void do_efdcfs (void)
2082 T0_64
= _do_efdcfs(T0_64
);
2085 /* Single precision fixed-point vector arithmetic */
2101 /* Single-precision floating-point comparisons */
2102 static inline int _do_efscmplt (uint32_t op1
, uint32_t op2
)
2104 /* XXX: TODO: test special values (NaN, infinites, ...) */
2105 return _do_efststlt(op1
, op2
);
2108 static inline int _do_efscmpgt (uint32_t op1
, uint32_t op2
)
2110 /* XXX: TODO: test special values (NaN, infinites, ...) */
2111 return _do_efststgt(op1
, op2
);
2114 static inline int _do_efscmpeq (uint32_t op1
, uint32_t op2
)
2116 /* XXX: TODO: test special values (NaN, infinites, ...) */
2117 return _do_efststeq(op1
, op2
);
2120 void do_efscmplt (void)
2122 T0
= _do_efscmplt(T0_64
, T1_64
);
2125 void do_efscmpgt (void)
2127 T0
= _do_efscmpgt(T0_64
, T1_64
);
2130 void do_efscmpeq (void)
2132 T0
= _do_efscmpeq(T0_64
, T1_64
);
2135 /* Single-precision floating-point vector comparisons */
2137 DO_SPE_CMP(fscmplt
);
2139 DO_SPE_CMP(fscmpgt
);
2141 DO_SPE_CMP(fscmpeq
);
2143 DO_SPE_CMP(fststlt
);
2145 DO_SPE_CMP(fststgt
);
2147 DO_SPE_CMP(fststeq
);
2149 /* Single-precision floating-point vector conversions */
2163 DO_SPE_OP1(fsctsiz
);
2165 DO_SPE_OP1(fsctuiz
);
2170 #endif /* defined(TARGET_PPCSPE) */
2172 /*****************************************************************************/
2173 /* Softmmu support */
2174 #if !defined (CONFIG_USER_ONLY)
2176 #define MMUSUFFIX _mmu
2177 #define GETPC() (__builtin_return_address(0))
2180 #include "softmmu_template.h"
2183 #include "softmmu_template.h"
2186 #include "softmmu_template.h"
2189 #include "softmmu_template.h"
2191 /* try to fill the TLB and return an exception if error. If retaddr is
2192 NULL, it means that the function was called in C code (i.e. not
2193 from generated code or from helper.c) */
2194 /* XXX: fix it to restore all registers */
2195 void tlb_fill (target_ulong addr
, int is_write
, int is_user
, void *retaddr
)
2197 TranslationBlock
*tb
;
2198 CPUState
*saved_env
;
2199 target_phys_addr_t pc
;
2202 /* XXX: hack to restore env in all cases, even if not called from
2205 env
= cpu_single_env
;
2206 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, is_user
, 1);
2207 if (unlikely(ret
!= 0)) {
2208 if (likely(retaddr
)) {
2209 /* now we have a real cpu fault */
2210 pc
= (target_phys_addr_t
)retaddr
;
2211 tb
= tb_find_pc(pc
);
2213 /* the PC is inside the translated code. It means that we have
2214 a virtual CPU fault */
2215 cpu_restore_state(tb
, env
, pc
, NULL
);
2218 do_raise_exception_err(env
->exception_index
, env
->error_code
);
2223 /* TLB invalidation helpers */
2224 void do_tlbia (void)
2226 if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_6xx
)) {
2227 ppc6xx_tlb_invalidate_all(env
);
2228 } else if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_4xx
)) {
2231 ppcbooke_tlb_invalidate_all(env
);
2238 void do_tlbie (void)
2241 #if !defined(FLUSH_ALL_TLBS)
2242 if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_6xx
)) {
2243 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 0);
2244 if (env
->id_tlbs
== 1)
2245 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 1);
2246 } else if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_4xx
)) {
2249 ppcbooke_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
,
2250 env
->spr
[SPR_BOOKE_PID
]);
2253 /* tlbie invalidate TLBs for all segments */
2254 T0
&= TARGET_PAGE_MASK
;
2255 T0
&= ~((target_ulong
)-1 << 28);
2256 /* XXX: this case should be optimized,
2257 * giving a mask to tlb_flush_page
2259 tlb_flush_page(env
, T0
| (0x0 << 28));
2260 tlb_flush_page(env
, T0
| (0x1 << 28));
2261 tlb_flush_page(env
, T0
| (0x2 << 28));
2262 tlb_flush_page(env
, T0
| (0x3 << 28));
2263 tlb_flush_page(env
, T0
| (0x4 << 28));
2264 tlb_flush_page(env
, T0
| (0x5 << 28));
2265 tlb_flush_page(env
, T0
| (0x6 << 28));
2266 tlb_flush_page(env
, T0
| (0x7 << 28));
2267 tlb_flush_page(env
, T0
| (0x8 << 28));
2268 tlb_flush_page(env
, T0
| (0x9 << 28));
2269 tlb_flush_page(env
, T0
| (0xA << 28));
2270 tlb_flush_page(env
, T0
| (0xB << 28));
2271 tlb_flush_page(env
, T0
| (0xC << 28));
2272 tlb_flush_page(env
, T0
| (0xD << 28));
2273 tlb_flush_page(env
, T0
| (0xE << 28));
2274 tlb_flush_page(env
, T0
| (0xF << 28));
2281 #if defined(TARGET_PPC64)
2282 void do_tlbie_64 (void)
2285 #if !defined(FLUSH_ALL_TLBS)
2286 if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_6xx
)) {
2287 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 0);
2288 if (env
->id_tlbs
== 1)
2289 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 1);
2290 } else if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_4xx
)) {
2293 ppcbooke_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
,
2294 env
->spr
[SPR_BOOKE_PID
]);
2297 /* tlbie invalidate TLBs for all segments
2298 * As we have 2^36 segments, invalidate all qemu TLBs
2301 T0
&= TARGET_PAGE_MASK
;
2302 T0
&= ~((target_ulong
)-1 << 28);
2303 /* XXX: this case should be optimized,
2304 * giving a mask to tlb_flush_page
2306 tlb_flush_page(env
, T0
| (0x0 << 28));
2307 tlb_flush_page(env
, T0
| (0x1 << 28));
2308 tlb_flush_page(env
, T0
| (0x2 << 28));
2309 tlb_flush_page(env
, T0
| (0x3 << 28));
2310 tlb_flush_page(env
, T0
| (0x4 << 28));
2311 tlb_flush_page(env
, T0
| (0x5 << 28));
2312 tlb_flush_page(env
, T0
| (0x6 << 28));
2313 tlb_flush_page(env
, T0
| (0x7 << 28));
2314 tlb_flush_page(env
, T0
| (0x8 << 28));
2315 tlb_flush_page(env
, T0
| (0x9 << 28));
2316 tlb_flush_page(env
, T0
| (0xA << 28));
2317 tlb_flush_page(env
, T0
| (0xB << 28));
2318 tlb_flush_page(env
, T0
| (0xC << 28));
2319 tlb_flush_page(env
, T0
| (0xD << 28));
2320 tlb_flush_page(env
, T0
| (0xE << 28));
2321 tlb_flush_page(env
, T0
| (0xF << 28));
2332 #if defined(TARGET_PPC64)
2333 void do_slbia (void)
2339 void do_slbie (void)
2346 /* Software driven TLBs management */
2347 /* PowerPC 602/603 software TLB load instructions helpers */
2348 void do_load_6xx_tlb (int is_code
)
2350 target_ulong RPN
, CMP
, EPN
;
2353 RPN
= env
->spr
[SPR_RPA
];
2355 CMP
= env
->spr
[SPR_ICMP
];
2356 EPN
= env
->spr
[SPR_IMISS
];
2358 CMP
= env
->spr
[SPR_DCMP
];
2359 EPN
= env
->spr
[SPR_DMISS
];
2361 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2362 #if defined (DEBUG_SOFTWARE_TLB)
2363 if (loglevel
!= 0) {
2364 fprintf(logfile
, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2365 __func__
, (unsigned long)T0
, (unsigned long)EPN
,
2366 (unsigned long)CMP
, (unsigned long)RPN
, way
);
2369 /* Store this TLB */
2370 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2371 way
, is_code
, CMP
, RPN
);
2374 static target_ulong
booke_tlb_to_page_size (int size
)
2376 return 1024 << (2 * size
);
2379 static int booke_page_size_to_tlb (target_ulong page_size
)
2383 switch (page_size
) {
2417 #if defined (TARGET_PPC64)
2418 case 0x000100000000ULL
:
2421 case 0x000400000000ULL
:
2424 case 0x001000000000ULL
:
2427 case 0x004000000000ULL
:
2430 case 0x010000000000ULL
:
2442 /* Helpers for 4xx TLB management */
2443 void do_4xx_tlbia (void)
2448 for (i
= 0; i
< 64; i
++) {
2449 tlb
= &env
->tlb
[i
].tlbe
;
2450 if (tlb
->prot
& PAGE_VALID
) {
2452 end
= tlb
->EPN
+ tlb
->size
;
2453 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2454 tlb_flush_page(env
, page
);
2456 tlb
->prot
&= ~PAGE_VALID
;
2462 void do_4xx_tlbre_lo (void)
2468 tlb
= &env
->tlb
[T0
].tlbe
;
2470 if (tlb
->prot
& PAGE_VALID
)
2472 size
= booke_page_size_to_tlb(tlb
->size
);
2473 if (size
< 0 || size
> 0x7)
2476 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2479 void do_4xx_tlbre_hi (void)
2484 tlb
= &env
->tlb
[T0
].tlbe
;
2486 if (tlb
->prot
& PAGE_EXEC
)
2488 if (tlb
->prot
& PAGE_WRITE
)
2492 static int tlb_4xx_search (target_ulong
virtual)
2495 target_ulong base
, mask
;
2498 /* Default return value is no match */
2500 for (i
= 0; i
< 64; i
++) {
2501 tlb
= &env
->tlb
[i
].tlbe
;
2502 /* Check TLB validity */
2503 if (!(tlb
->prot
& PAGE_VALID
))
2505 /* Check TLB PID vs current PID */
2506 if (tlb
->PID
!= 0 && tlb
->PID
!= env
->spr
[SPR_40x_PID
])
2508 /* Check TLB address vs virtual address */
2510 mask
= ~(tlb
->size
- 1);
2511 if ((base
& mask
) != (virtual & mask
))
2520 void do_4xx_tlbsx (void)
2522 T0
= tlb_4xx_search(T0
);
2525 void do_4xx_tlbsx_ (void)
2529 T0
= tlb_4xx_search(T0
);
2535 void do_4xx_tlbwe_lo (void)
2538 target_ulong page
, end
;
2541 tlb
= &env
->tlb
[T0
].tlbe
;
2542 /* Invalidate previous TLB (if it's valid) */
2543 if (tlb
->prot
& PAGE_VALID
) {
2544 end
= tlb
->EPN
+ tlb
->size
;
2545 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2546 tlb_flush_page(env
, page
);
2548 tlb
->size
= booke_tlb_to_page_size((T1
>> 7) & 0x7);
2549 tlb
->EPN
= (T1
& 0xFFFFFC00) & ~(tlb
->size
- 1);
2551 tlb
->prot
|= PAGE_VALID
;
2553 tlb
->prot
&= ~PAGE_VALID
;
2554 tlb
->PID
= env
->spr
[SPR_BOOKE_PID
]; /* PID */
2555 tlb
->attr
= T1
& 0xFF;
2556 /* Invalidate new TLB (if valid) */
2557 if (tlb
->prot
& PAGE_VALID
) {
2558 end
= tlb
->EPN
+ tlb
->size
;
2559 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2560 tlb_flush_page(env
, page
);
2564 void do_4xx_tlbwe_hi (void)
2569 tlb
= &env
->tlb
[T0
].tlbe
;
2570 tlb
->RPN
= T1
& 0xFFFFFC00;
2571 tlb
->prot
= PAGE_READ
;
2573 tlb
->prot
|= PAGE_EXEC
;
2575 tlb
->prot
|= PAGE_WRITE
;
2577 #endif /* !CONFIG_USER_ONLY */