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
40 /*****************************************************************************/
41 /* Exceptions processing helpers */
43 void do_raise_exception_err (uint32_t exception
, int error_code
)
46 printf("Raise exception %3x code : %d\n", exception
, error_code
);
49 case POWERPC_EXCP_PROGRAM
:
50 if (error_code
== POWERPC_EXCP_FP
&& msr_fe0
== 0 && msr_fe1
== 0)
56 env
->exception_index
= exception
;
57 env
->error_code
= error_code
;
61 void do_raise_exception (uint32_t exception
)
63 do_raise_exception_err(exception
, 0);
66 void cpu_dump_EA (target_ulong EA
);
67 void do_print_mem_EA (target_ulong EA
)
72 /*****************************************************************************/
73 /* Registers load and stores */
74 void do_load_cr (void)
76 T0
= (env
->crf
[0] << 28) |
86 void do_store_cr (uint32_t mask
)
90 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
92 env
->crf
[i
] = (T0
>> (sh
* 4)) & 0xFUL
;
96 void do_load_xer (void)
98 T0
= (xer_so
<< XER_SO
) |
102 (xer_cmp
<< XER_CMP
);
105 void do_store_xer (void)
107 xer_so
= (T0
>> XER_SO
) & 0x01;
108 xer_ov
= (T0
>> XER_OV
) & 0x01;
109 xer_ca
= (T0
>> XER_CA
) & 0x01;
110 xer_cmp
= (T0
>> XER_CMP
) & 0xFF;
111 xer_bc
= (T0
>> XER_BC
) & 0x7F;
114 #if defined(TARGET_PPC64)
115 void do_store_pri (int prio
)
117 env
->spr
[SPR_PPR
] &= ~0x001C000000000000ULL
;
118 env
->spr
[SPR_PPR
] |= ((uint64_t)prio
& 0x7) << 50;
122 void do_load_fpscr (void)
124 /* The 32 MSB of the target fpr are undefined.
135 #if defined(WORDS_BIGENDIAN)
144 for (i
= 0; i
< 8; i
++)
145 u
.s
.u
[WORD1
] |= env
->fpscr
[i
] << (4 * i
);
149 void do_store_fpscr (uint32_t mask
)
152 * We use only the 32 LSB of the incoming fpr
164 env
->fpscr
[0] = (env
->fpscr
[0] & 0x9) | ((u
.s
.u
[WORD1
] >> 28) & ~0x9);
165 for (i
= 1; i
< 7; i
++) {
166 if (mask
& (1 << (7 - i
)))
167 env
->fpscr
[i
] = (u
.s
.u
[WORD1
] >> (4 * (7 - i
))) & 0xF;
169 /* TODO: update FEX & VX */
170 /* Set rounding mode */
171 switch (env
->fpscr
[0] & 0x3) {
173 /* Best approximation (round to nearest) */
174 rnd_type
= float_round_nearest_even
;
177 /* Smaller magnitude (round toward zero) */
178 rnd_type
= float_round_to_zero
;
181 /* Round toward +infinite */
182 rnd_type
= float_round_up
;
186 /* Round toward -infinite */
187 rnd_type
= float_round_down
;
190 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
193 target_ulong
ppc_load_dump_spr (int sprn
)
196 fprintf(logfile
, "Read SPR %d %03x => " ADDRX
"\n",
197 sprn
, sprn
, env
->spr
[sprn
]);
200 return env
->spr
[sprn
];
203 void ppc_store_dump_spr (int sprn
, target_ulong val
)
206 fprintf(logfile
, "Write SPR %d %03x => " ADDRX
" <= " ADDRX
"\n",
207 sprn
, sprn
, env
->spr
[sprn
], val
);
209 env
->spr
[sprn
] = val
;
212 /*****************************************************************************/
213 /* Fixed point operations helpers */
214 #if defined(TARGET_PPC64)
215 static void add128 (uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
224 static void neg128 (uint64_t *plow
, uint64_t *phigh
)
228 add128(plow
, phigh
, 1, 0);
231 static void mul64 (uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
233 uint32_t a0
, a1
, b0
, b1
;
242 v
= (uint64_t)a0
* (uint64_t)b0
;
246 v
= (uint64_t)a0
* (uint64_t)b1
;
247 add128(plow
, phigh
, v
<< 32, v
>> 32);
249 v
= (uint64_t)a1
* (uint64_t)b0
;
250 add128(plow
, phigh
, v
<< 32, v
>> 32);
252 v
= (uint64_t)a1
* (uint64_t)b1
;
254 #if defined(DEBUG_MULDIV)
255 printf("mul: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
256 a
, b
, *phigh
, *plow
);
260 void do_mul64 (uint64_t *plow
, uint64_t *phigh
)
262 mul64(plow
, phigh
, T0
, T1
);
265 static void imul64 (uint64_t *plow
, uint64_t *phigh
, int64_t a
, int64_t b
)
275 mul64(plow
, phigh
, a
, b
);
281 void do_imul64 (uint64_t *plow
, uint64_t *phigh
)
283 imul64(plow
, phigh
, T0
, T1
);
291 if (likely(!((uint32_t)T0
< (uint32_t)T2
||
292 (xer_ca
== 1 && (uint32_t)T0
== (uint32_t)T2
)))) {
299 #if defined(TARGET_PPC64)
300 void do_adde_64 (void)
304 if (likely(!((uint64_t)T0
< (uint64_t)T2
||
305 (xer_ca
== 1 && (uint64_t)T0
== (uint64_t)T2
)))) {
313 void do_addmeo (void)
317 if (likely(!((uint32_t)T1
&
318 ((uint32_t)T1
^ (uint32_t)T0
) & (1UL << 31)))) {
328 #if defined(TARGET_PPC64)
329 void do_addmeo_64 (void)
333 if (likely(!((uint64_t)T1
&
334 ((uint64_t)T1
^ (uint64_t)T0
) & (1ULL << 63)))) {
347 if (likely(!(((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) ||
348 (int32_t)T1
== 0))) {
350 T0
= (int32_t)T0
/ (int32_t)T1
;
354 T0
= (-1) * ((uint32_t)T0
>> 31);
358 #if defined(TARGET_PPC64)
361 if (likely(!(((int64_t)T0
== INT64_MIN
&& (int64_t)T1
== -1ULL) ||
362 (int64_t)T1
== 0))) {
364 T0
= (int64_t)T0
/ (int64_t)T1
;
368 T0
= (-1ULL) * ((uint64_t)T0
>> 63);
373 void do_divwuo (void)
375 if (likely((uint32_t)T1
!= 0)) {
377 T0
= (uint32_t)T0
/ (uint32_t)T1
;
385 #if defined(TARGET_PPC64)
386 void do_divduo (void)
388 if (likely((uint64_t)T1
!= 0)) {
390 T0
= (uint64_t)T0
/ (uint64_t)T1
;
399 void do_mullwo (void)
401 int64_t res
= (int64_t)T0
* (int64_t)T1
;
403 if (likely((int32_t)res
== res
)) {
412 #if defined(TARGET_PPC64)
413 void do_mulldo (void)
419 if (likely(th
== 0)) {
431 if (likely((int32_t)T0
!= INT32_MIN
)) {
440 #if defined(TARGET_PPC64)
441 void do_nego_64 (void)
443 if (likely((int64_t)T0
!= INT64_MIN
)) {
455 T0
= T1
+ ~T0
+ xer_ca
;
456 if (likely((uint32_t)T0
>= (uint32_t)T1
&&
457 (xer_ca
== 0 || (uint32_t)T0
!= (uint32_t)T1
))) {
464 #if defined(TARGET_PPC64)
465 void do_subfe_64 (void)
467 T0
= T1
+ ~T0
+ xer_ca
;
468 if (likely((uint64_t)T0
>= (uint64_t)T1
&&
469 (xer_ca
== 0 || (uint64_t)T0
!= (uint64_t)T1
))) {
477 void do_subfmeo (void)
480 T0
= ~T0
+ xer_ca
- 1;
481 if (likely(!((uint32_t)~T1
& ((uint32_t)~T1
^ (uint32_t)T0
) &
488 if (likely((uint32_t)T1
!= UINT32_MAX
))
492 #if defined(TARGET_PPC64)
493 void do_subfmeo_64 (void)
496 T0
= ~T0
+ xer_ca
- 1;
497 if (likely(!((uint64_t)~T1
& ((uint64_t)~T1
^ (uint64_t)T0
) &
504 if (likely((uint64_t)T1
!= UINT64_MAX
))
509 void do_subfzeo (void)
513 if (likely(!(((uint32_t)~T1
^ UINT32_MAX
) &
514 ((uint32_t)(~T1
) ^ (uint32_t)T0
) & (1UL << 31)))) {
520 if (likely((uint32_t)T0
>= (uint32_t)~T1
)) {
527 #if defined(TARGET_PPC64)
528 void do_subfzeo_64 (void)
532 if (likely(!(((uint64_t)~T1
^ UINT64_MAX
) &
533 ((uint64_t)(~T1
) ^ (uint64_t)T0
) & (1ULL << 63)))) {
539 if (likely((uint64_t)T0
>= (uint64_t)~T1
)) {
547 /* shift right arithmetic helper */
552 if (likely(!(T1
& 0x20UL
))) {
553 if (likely((uint32_t)T1
!= 0)) {
554 ret
= (int32_t)T0
>> (T1
& 0x1fUL
);
555 if (likely(ret
>= 0 || ((int32_t)T0
& ((1 << T1
) - 1)) == 0)) {
565 ret
= (-1) * ((uint32_t)T0
>> 31);
566 if (likely(ret
>= 0 || ((uint32_t)T0
& ~0x80000000UL
) == 0)) {
575 #if defined(TARGET_PPC64)
580 if (likely(!(T1
& 0x40UL
))) {
581 if (likely((uint64_t)T1
!= 0)) {
582 ret
= (int64_t)T0
>> (T1
& 0x3FUL
);
583 if (likely(ret
>= 0 || ((int64_t)T0
& ((1 << T1
) - 1)) == 0)) {
593 ret
= (-1) * ((uint64_t)T0
>> 63);
594 if (likely(ret
>= 0 || ((uint64_t)T0
& ~0x8000000000000000ULL
) == 0)) {
604 static inline int popcnt (uint32_t val
)
608 for (i
= 0; val
!= 0;)
609 val
= val
^ (val
- 1);
614 void do_popcntb (void)
620 for (i
= 0; i
< 32; i
+= 8)
621 ret
|= popcnt((T0
>> i
) & 0xFF) << i
;
625 #if defined(TARGET_PPC64)
626 void do_popcntb_64 (void)
632 for (i
= 0; i
< 64; i
+= 8)
633 ret
|= popcnt((T0
>> i
) & 0xFF) << i
;
638 /*****************************************************************************/
639 /* Floating point operations helpers */
647 p
.i
= float64_to_int32(FT0
, &env
->fp_status
);
648 #if USE_PRECISE_EMULATION
649 /* XXX: higher bits are not supposed to be significant.
650 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
652 p
.i
|= 0xFFF80000ULL
<< 32;
657 void do_fctiwz (void)
664 p
.i
= float64_to_int32_round_to_zero(FT0
, &env
->fp_status
);
665 #if USE_PRECISE_EMULATION
666 /* XXX: higher bits are not supposed to be significant.
667 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
669 p
.i
|= 0xFFF80000ULL
<< 32;
674 #if defined(TARGET_PPC64)
683 FT0
= int64_to_float64(p
.i
, &env
->fp_status
);
693 p
.i
= float64_to_int64(FT0
, &env
->fp_status
);
697 void do_fctidz (void)
704 p
.i
= float64_to_int64_round_to_zero(FT0
, &env
->fp_status
);
710 static inline void do_fri (int rounding_mode
)
714 curmode
= env
->fp_status
.float_rounding_mode
;
715 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
716 FT0
= float64_round_to_int(FT0
, &env
->fp_status
);
717 set_float_rounding_mode(curmode
, &env
->fp_status
);
722 do_fri(float_round_nearest_even
);
727 do_fri(float_round_to_zero
);
732 do_fri(float_round_up
);
737 do_fri(float_round_down
);
740 #if USE_PRECISE_EMULATION
744 float128 ft0_128
, ft1_128
;
746 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
747 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
748 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
749 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
750 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
751 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
753 /* This is OK on x86 hosts */
754 FT0
= (FT0
* FT1
) + FT2
;
761 float128 ft0_128
, ft1_128
;
763 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
764 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
765 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
766 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
767 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
768 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
770 /* This is OK on x86 hosts */
771 FT0
= (FT0
* FT1
) - FT2
;
774 #endif /* USE_PRECISE_EMULATION */
776 void do_fnmadd (void)
778 #if USE_PRECISE_EMULATION
780 float128 ft0_128
, ft1_128
;
782 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
783 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
784 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
785 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
786 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
787 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
789 /* This is OK on x86 hosts */
790 FT0
= (FT0
* FT1
) + FT2
;
793 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
794 FT0
= float64_add(FT0
, FT2
, &env
->fp_status
);
796 if (likely(!isnan(FT0
)))
797 FT0
= float64_chs(FT0
);
800 void do_fnmsub (void)
802 #if USE_PRECISE_EMULATION
804 float128 ft0_128
, ft1_128
;
806 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
807 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
808 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
809 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
810 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
811 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
813 /* This is OK on x86 hosts */
814 FT0
= (FT0
* FT1
) - FT2
;
817 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
818 FT0
= float64_sub(FT0
, FT2
, &env
->fp_status
);
820 if (likely(!isnan(FT0
)))
821 FT0
= float64_chs(FT0
);
826 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
836 if (likely(isnormal(FT0
))) {
837 FT0
= float64_div(1.0, FT0
, &env
->fp_status
);
840 if (p
.i
== 0x8000000000000000ULL
) {
841 p
.i
= 0xFFF0000000000000ULL
;
842 } else if (p
.i
== 0x0000000000000000ULL
) {
843 p
.i
= 0x7FF0000000000000ULL
;
844 } else if (isnan(FT0
)) {
845 p
.i
= 0x7FF8000000000000ULL
;
846 } else if (FT0
< 0.0) {
847 p
.i
= 0x8000000000000000ULL
;
849 p
.i
= 0x0000000000000000ULL
;
862 if (likely(isnormal(FT0
))) {
863 #if USE_PRECISE_EMULATION
864 FT0
= float64_div(1.0, FT0
, &env
->fp_status
);
865 FT0
= float64_to_float32(FT0
, &env
->fp_status
);
867 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
871 if (p
.i
== 0x8000000000000000ULL
) {
872 p
.i
= 0xFFF0000000000000ULL
;
873 } else if (p
.i
== 0x0000000000000000ULL
) {
874 p
.i
= 0x7FF0000000000000ULL
;
875 } else if (isnan(FT0
)) {
876 p
.i
= 0x7FF8000000000000ULL
;
877 } else if (FT0
< 0.0) {
878 p
.i
= 0x8000000000000000ULL
;
880 p
.i
= 0x0000000000000000ULL
;
886 void do_frsqrte (void)
893 if (likely(isnormal(FT0
) && FT0
> 0.0)) {
894 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
895 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
898 if (p
.i
== 0x8000000000000000ULL
) {
899 p
.i
= 0xFFF0000000000000ULL
;
900 } else if (p
.i
== 0x0000000000000000ULL
) {
901 p
.i
= 0x7FF0000000000000ULL
;
902 } else if (isnan(FT0
)) {
903 if (!(p
.i
& 0x0008000000000000ULL
))
904 p
.i
|= 0x000FFFFFFFFFFFFFULL
;
905 } else if (FT0
< 0) {
906 p
.i
= 0x7FF8000000000000ULL
;
908 p
.i
= 0x0000000000000000ULL
;
924 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
925 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
927 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
934 env
->fpscr
[4] |= 0x1;
935 env
->fpscr
[6] |= 0x1;
942 env
->fpscr
[4] &= ~0x1;
943 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
944 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
946 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
953 env
->fpscr
[4] |= 0x1;
954 if (!float64_is_signaling_nan(FT0
) || !float64_is_signaling_nan(FT1
)) {
956 env
->fpscr
[6] |= 0x1;
957 if (!(env
->fpscr
[1] & 0x8))
958 env
->fpscr
[4] |= 0x8;
960 env
->fpscr
[4] |= 0x8;
966 #if !defined (CONFIG_USER_ONLY)
967 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
);
970 #if defined(TARGET_PPC64)
971 if (env
->spr
[SPR_SRR1
] & (1ULL << MSR_SF
)) {
972 env
->nip
= (uint64_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
973 do_store_msr(env
, (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
975 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
976 ppc_store_msr_32(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
979 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
980 do_store_msr(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
982 #if defined (DEBUG_OP)
983 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
985 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
988 #if defined(TARGET_PPC64)
991 if (env
->spr
[SPR_SRR1
] & (1ULL << MSR_SF
)) {
992 env
->nip
= (uint64_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
993 do_store_msr(env
, (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
995 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
996 do_store_msr(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
998 #if defined (DEBUG_OP)
999 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1001 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1004 #if defined(TARGET_PPC64H)
1005 void do_hrfid (void)
1007 if (env
->spr
[SPR_HSRR1
] & (1ULL << MSR_SF
)) {
1008 env
->nip
= (uint64_t)(env
->spr
[SPR_HSRR0
] & ~0x00000003);
1009 do_store_msr(env
, (uint64_t)(env
->spr
[SPR_HSRR1
] & ~0xFFFF0000UL
));
1011 env
->nip
= (uint32_t)(env
->spr
[SPR_HSRR0
] & ~0x00000003);
1012 do_store_msr(env
, (uint32_t)(env
->spr
[SPR_HSRR1
] & ~0xFFFF0000UL
));
1014 #if defined (DEBUG_OP)
1015 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1017 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1022 void do_tw (int flags
)
1024 if (!likely(!(((int32_t)T0
< (int32_t)T1
&& (flags
& 0x10)) ||
1025 ((int32_t)T0
> (int32_t)T1
&& (flags
& 0x08)) ||
1026 ((int32_t)T0
== (int32_t)T1
&& (flags
& 0x04)) ||
1027 ((uint32_t)T0
< (uint32_t)T1
&& (flags
& 0x02)) ||
1028 ((uint32_t)T0
> (uint32_t)T1
&& (flags
& 0x01))))) {
1029 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1033 #if defined(TARGET_PPC64)
1034 void do_td (int flags
)
1036 if (!likely(!(((int64_t)T0
< (int64_t)T1
&& (flags
& 0x10)) ||
1037 ((int64_t)T0
> (int64_t)T1
&& (flags
& 0x08)) ||
1038 ((int64_t)T0
== (int64_t)T1
&& (flags
& 0x04)) ||
1039 ((uint64_t)T0
< (uint64_t)T1
&& (flags
& 0x02)) ||
1040 ((uint64_t)T0
> (uint64_t)T1
&& (flags
& 0x01)))))
1041 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1045 /*****************************************************************************/
1046 /* PowerPC 601 specific instructions (POWER bridge) */
1047 void do_POWER_abso (void)
1049 if ((uint32_t)T0
== INT32_MIN
) {
1059 void do_POWER_clcs (void)
1063 /* Instruction cache line size */
1064 T0
= ICACHE_LINE_SIZE
;
1067 /* Data cache line size */
1068 T0
= DCACHE_LINE_SIZE
;
1071 /* Minimum cache line size */
1072 T0
= ICACHE_LINE_SIZE
< DCACHE_LINE_SIZE
?
1073 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
1076 /* Maximum cache line size */
1077 T0
= ICACHE_LINE_SIZE
> DCACHE_LINE_SIZE
?
1078 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
1086 void do_POWER_div (void)
1090 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1091 T0
= (long)((-1) * (T0
>> 31));
1092 env
->spr
[SPR_MQ
] = 0;
1094 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1095 env
->spr
[SPR_MQ
] = tmp
% T1
;
1096 T0
= tmp
/ (int32_t)T1
;
1100 void do_POWER_divo (void)
1104 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1105 T0
= (long)((-1) * (T0
>> 31));
1106 env
->spr
[SPR_MQ
] = 0;
1110 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1111 env
->spr
[SPR_MQ
] = tmp
% T1
;
1113 if (tmp
> (int64_t)INT32_MAX
|| tmp
< (int64_t)INT32_MIN
) {
1123 void do_POWER_divs (void)
1125 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1126 T0
= (long)((-1) * (T0
>> 31));
1127 env
->spr
[SPR_MQ
] = 0;
1129 env
->spr
[SPR_MQ
] = T0
% T1
;
1130 T0
= (int32_t)T0
/ (int32_t)T1
;
1134 void do_POWER_divso (void)
1136 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1137 T0
= (long)((-1) * (T0
>> 31));
1138 env
->spr
[SPR_MQ
] = 0;
1142 T0
= (int32_t)T0
/ (int32_t)T1
;
1143 env
->spr
[SPR_MQ
] = (int32_t)T0
% (int32_t)T1
;
1148 void do_POWER_dozo (void)
1150 if ((int32_t)T1
> (int32_t)T0
) {
1153 if (((uint32_t)(~T2
) ^ (uint32_t)T1
^ UINT32_MAX
) &
1154 ((uint32_t)(~T2
) ^ (uint32_t)T0
) & (1UL << 31)) {
1166 void do_POWER_maskg (void)
1170 if ((uint32_t)T0
== (uint32_t)(T1
+ 1)) {
1173 ret
= (((uint32_t)(-1)) >> ((uint32_t)T0
)) ^
1174 (((uint32_t)(-1) >> ((uint32_t)T1
)) >> 1);
1175 if ((uint32_t)T0
> (uint32_t)T1
)
1181 void do_POWER_mulo (void)
1185 tmp
= (uint64_t)T0
* (uint64_t)T1
;
1186 env
->spr
[SPR_MQ
] = tmp
>> 32;
1188 if (tmp
>> 32 != ((uint64_t)T0
>> 16) * ((uint64_t)T1
>> 16)) {
1196 #if !defined (CONFIG_USER_ONLY)
1197 void do_POWER_rac (void)
1202 /* We don't have to generate many instances of this instruction,
1203 * as rac is supervisor only.
1205 if (get_physical_address(env
, &ctx
, T0
, 0, ACCESS_INT
, 1) == 0)
1210 void do_POWER_rfsvc (void)
1212 env
->nip
= env
->lr
& ~0x00000003UL
;
1213 T0
= env
->ctr
& 0x0000FFFFUL
;
1214 do_store_msr(env
, T0
);
1215 #if defined (DEBUG_OP)
1216 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1218 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1221 /* PowerPC 601 BAT management helper */
1222 void do_store_601_batu (int nr
)
1224 do_store_ibatu(env
, nr
, (uint32_t)T0
);
1225 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1226 env
->DBAT
[1][nr
] = env
->IBAT
[1][nr
];
1230 /*****************************************************************************/
1231 /* 602 specific instructions */
1232 /* mfrom is the most crazy instruction ever seen, imho ! */
1233 /* Real implementation uses a ROM table. Do the same */
1234 #define USE_MFROM_ROM_TABLE
1235 void do_op_602_mfrom (void)
1237 if (likely(T0
< 602)) {
1238 #if defined(USE_MFROM_ROM_TABLE)
1239 #include "mfrom_table.c"
1240 T0
= mfrom_ROM_table
[T0
];
1243 /* Extremly decomposed:
1245 * T0 = 256 * log10(10 + 1.0) + 0.5
1248 d
= float64_div(d
, 256, &env
->fp_status
);
1250 d
= exp10(d
); // XXX: use float emulation function
1251 d
= float64_add(d
, 1.0, &env
->fp_status
);
1252 d
= log10(d
); // XXX: use float emulation function
1253 d
= float64_mul(d
, 256, &env
->fp_status
);
1254 d
= float64_add(d
, 0.5, &env
->fp_status
);
1255 T0
= float64_round_to_int(d
, &env
->fp_status
);
1262 /*****************************************************************************/
1263 /* Embedded PowerPC specific helpers */
1264 void do_405_check_ov (void)
1266 if (likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1267 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1275 void do_405_check_sat (void)
1277 if (!likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1278 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1279 /* Saturate result */
1288 /* XXX: to be improved to check access rights when in user-mode */
1289 void do_load_dcr (void)
1293 if (unlikely(env
->dcr_env
== NULL
)) {
1294 if (loglevel
!= 0) {
1295 fprintf(logfile
, "No DCR environment\n");
1297 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1298 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1299 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, T0
, &val
) != 0)) {
1300 if (loglevel
!= 0) {
1301 fprintf(logfile
, "DCR read error %d %03x\n", (int)T0
, (int)T0
);
1303 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1304 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1310 void do_store_dcr (void)
1312 if (unlikely(env
->dcr_env
== NULL
)) {
1313 if (loglevel
!= 0) {
1314 fprintf(logfile
, "No DCR environment\n");
1316 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1317 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1318 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, T0
, T1
) != 0)) {
1319 if (loglevel
!= 0) {
1320 fprintf(logfile
, "DCR write error %d %03x\n", (int)T0
, (int)T0
);
1322 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1323 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1327 #if !defined(CONFIG_USER_ONLY)
1328 void do_40x_rfci (void)
1330 env
->nip
= env
->spr
[SPR_40x_SRR2
];
1331 do_store_msr(env
, env
->spr
[SPR_40x_SRR3
] & ~0xFFFF0000);
1332 #if defined (DEBUG_OP)
1333 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1335 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1340 #if defined(TARGET_PPC64)
1341 if (env
->spr
[SPR_BOOKE_CSRR1
] & (1 << MSR_CM
)) {
1342 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_CSRR0
];
1346 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_CSRR0
];
1348 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_CSRR1
] & ~0x3FFF0000);
1349 #if defined (DEBUG_OP)
1350 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1352 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1357 #if defined(TARGET_PPC64)
1358 if (env
->spr
[SPR_BOOKE_DSRR1
] & (1 << MSR_CM
)) {
1359 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_DSRR0
];
1363 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_DSRR0
];
1365 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_DSRR1
] & ~0x3FFF0000);
1366 #if defined (DEBUG_OP)
1367 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1369 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1372 void do_rfmci (void)
1374 #if defined(TARGET_PPC64)
1375 if (env
->spr
[SPR_BOOKE_MCSRR1
] & (1 << MSR_CM
)) {
1376 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_MCSRR0
];
1380 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_MCSRR0
];
1382 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_MCSRR1
] & ~0x3FFF0000);
1383 #if defined (DEBUG_OP)
1384 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1386 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1389 void do_load_403_pb (int num
)
1394 void do_store_403_pb (int num
)
1396 if (likely(env
->pb
[num
] != T0
)) {
1398 /* Should be optimized */
1405 void do_440_dlmzb (void)
1411 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1412 if ((T0
& mask
) == 0)
1416 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1417 if ((T1
& mask
) == 0)
1425 #if defined(TARGET_PPCEMB)
1426 /* SPE extension helpers */
1427 /* Use a table to make this quicker */
1428 static uint8_t hbrev
[16] = {
1429 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1430 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1433 static inline uint8_t byte_reverse (uint8_t val
)
1435 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1438 static inline uint32_t word_reverse (uint32_t val
)
1440 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1441 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1444 #define MASKBITS 16 // Random value - to be fixed
1445 void do_brinc (void)
1447 uint32_t a
, b
, d
, mask
;
1449 mask
= (uint32_t)(-1UL) >> MASKBITS
;
1452 d
= word_reverse(1 + word_reverse(a
| ~mask
));
1453 T0_64
= (T0_64
& ~mask
) | (d
& mask
);
1456 #define DO_SPE_OP2(name) \
1457 void do_ev##name (void) \
1459 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) | \
1460 (uint64_t)_do_e##name(T0_64, T1_64); \
1463 #define DO_SPE_OP1(name) \
1464 void do_ev##name (void) \
1466 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) | \
1467 (uint64_t)_do_e##name(T0_64); \
1470 /* Fixed-point vector arithmetic */
1471 static inline uint32_t _do_eabs (uint32_t val
)
1473 if (val
!= 0x80000000)
1479 static inline uint32_t _do_eaddw (uint32_t op1
, uint32_t op2
)
1484 static inline int _do_ecntlsw (uint32_t val
)
1486 if (val
& 0x80000000)
1487 return _do_cntlzw(~val
);
1489 return _do_cntlzw(val
);
1492 static inline int _do_ecntlzw (uint32_t val
)
1494 return _do_cntlzw(val
);
1497 static inline uint32_t _do_eneg (uint32_t val
)
1499 if (val
!= 0x80000000)
1505 static inline uint32_t _do_erlw (uint32_t op1
, uint32_t op2
)
1507 return rotl32(op1
, op2
);
1510 static inline uint32_t _do_erndw (uint32_t val
)
1512 return (val
+ 0x000080000000) & 0xFFFF0000;
1515 static inline uint32_t _do_eslw (uint32_t op1
, uint32_t op2
)
1517 /* No error here: 6 bits are used */
1518 return op1
<< (op2
& 0x3F);
1521 static inline int32_t _do_esrws (int32_t op1
, uint32_t op2
)
1523 /* No error here: 6 bits are used */
1524 return op1
>> (op2
& 0x3F);
1527 static inline uint32_t _do_esrwu (uint32_t op1
, uint32_t op2
)
1529 /* No error here: 6 bits are used */
1530 return op1
>> (op2
& 0x3F);
1533 static inline uint32_t _do_esubfw (uint32_t op1
, uint32_t op2
)
1561 /* evsel is a little bit more complicated... */
1562 static inline uint32_t _do_esel (uint32_t op1
, uint32_t op2
, int n
)
1570 void do_evsel (void)
1572 T0_64
= ((uint64_t)_do_esel(T0_64
>> 32, T1_64
>> 32, T0
>> 3) << 32) |
1573 (uint64_t)_do_esel(T0_64
, T1_64
, (T0
>> 2) & 1);
1576 /* Fixed-point vector comparisons */
1577 #define DO_SPE_CMP(name) \
1578 void do_ev##name (void) \
1580 T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32, \
1581 T1_64 >> 32) << 32, \
1582 _do_e##name(T0_64, T1_64)); \
1585 static inline uint32_t _do_evcmp_merge (int t0
, int t1
)
1587 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1589 static inline int _do_ecmpeq (uint32_t op1
, uint32_t op2
)
1591 return op1
== op2
? 1 : 0;
1594 static inline int _do_ecmpgts (int32_t op1
, int32_t op2
)
1596 return op1
> op2
? 1 : 0;
1599 static inline int _do_ecmpgtu (uint32_t op1
, uint32_t op2
)
1601 return op1
> op2
? 1 : 0;
1604 static inline int _do_ecmplts (int32_t op1
, int32_t op2
)
1606 return op1
< op2
? 1 : 0;
1609 static inline int _do_ecmpltu (uint32_t op1
, uint32_t op2
)
1611 return op1
< op2
? 1 : 0;
1625 /* Single precision floating-point conversions from/to integer */
1626 static inline uint32_t _do_efscfsi (int32_t val
)
1633 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1638 static inline uint32_t _do_efscfui (uint32_t val
)
1645 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1650 static inline int32_t _do_efsctsi (uint32_t val
)
1658 /* NaN are not treated the same way IEEE 754 does */
1659 if (unlikely(isnan(u
.f
)))
1662 return float32_to_int32(u
.f
, &env
->spe_status
);
1665 static inline uint32_t _do_efsctui (uint32_t val
)
1673 /* NaN are not treated the same way IEEE 754 does */
1674 if (unlikely(isnan(u
.f
)))
1677 return float32_to_uint32(u
.f
, &env
->spe_status
);
1680 static inline int32_t _do_efsctsiz (uint32_t val
)
1688 /* NaN are not treated the same way IEEE 754 does */
1689 if (unlikely(isnan(u
.f
)))
1692 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1695 static inline uint32_t _do_efsctuiz (uint32_t val
)
1703 /* NaN are not treated the same way IEEE 754 does */
1704 if (unlikely(isnan(u
.f
)))
1707 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1710 void do_efscfsi (void)
1712 T0_64
= _do_efscfsi(T0_64
);
1715 void do_efscfui (void)
1717 T0_64
= _do_efscfui(T0_64
);
1720 void do_efsctsi (void)
1722 T0_64
= _do_efsctsi(T0_64
);
1725 void do_efsctui (void)
1727 T0_64
= _do_efsctui(T0_64
);
1730 void do_efsctsiz (void)
1732 T0_64
= _do_efsctsiz(T0_64
);
1735 void do_efsctuiz (void)
1737 T0_64
= _do_efsctuiz(T0_64
);
1740 /* Single precision floating-point conversion to/from fractional */
1741 static inline uint32_t _do_efscfsf (uint32_t val
)
1749 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1750 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
1751 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1756 static inline uint32_t _do_efscfuf (uint32_t val
)
1764 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1765 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1766 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1771 static inline int32_t _do_efsctsf (uint32_t val
)
1780 /* NaN are not treated the same way IEEE 754 does */
1781 if (unlikely(isnan(u
.f
)))
1783 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1784 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1786 return float32_to_int32(u
.f
, &env
->spe_status
);
1789 static inline uint32_t _do_efsctuf (uint32_t val
)
1798 /* NaN are not treated the same way IEEE 754 does */
1799 if (unlikely(isnan(u
.f
)))
1801 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1802 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1804 return float32_to_uint32(u
.f
, &env
->spe_status
);
1807 static inline int32_t _do_efsctsfz (uint32_t val
)
1816 /* NaN are not treated the same way IEEE 754 does */
1817 if (unlikely(isnan(u
.f
)))
1819 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1820 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1822 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1825 static inline uint32_t _do_efsctufz (uint32_t val
)
1834 /* NaN are not treated the same way IEEE 754 does */
1835 if (unlikely(isnan(u
.f
)))
1837 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1838 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1840 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1843 void do_efscfsf (void)
1845 T0_64
= _do_efscfsf(T0_64
);
1848 void do_efscfuf (void)
1850 T0_64
= _do_efscfuf(T0_64
);
1853 void do_efsctsf (void)
1855 T0_64
= _do_efsctsf(T0_64
);
1858 void do_efsctuf (void)
1860 T0_64
= _do_efsctuf(T0_64
);
1863 void do_efsctsfz (void)
1865 T0_64
= _do_efsctsfz(T0_64
);
1868 void do_efsctufz (void)
1870 T0_64
= _do_efsctufz(T0_64
);
1873 /* Double precision floating point helpers */
1874 static inline int _do_efdcmplt (uint64_t op1
, uint64_t op2
)
1876 /* XXX: TODO: test special values (NaN, infinites, ...) */
1877 return _do_efdtstlt(op1
, op2
);
1880 static inline int _do_efdcmpgt (uint64_t op1
, uint64_t op2
)
1882 /* XXX: TODO: test special values (NaN, infinites, ...) */
1883 return _do_efdtstgt(op1
, op2
);
1886 static inline int _do_efdcmpeq (uint64_t op1
, uint64_t op2
)
1888 /* XXX: TODO: test special values (NaN, infinites, ...) */
1889 return _do_efdtsteq(op1
, op2
);
1892 void do_efdcmplt (void)
1894 T0
= _do_efdcmplt(T0_64
, T1_64
);
1897 void do_efdcmpgt (void)
1899 T0
= _do_efdcmpgt(T0_64
, T1_64
);
1902 void do_efdcmpeq (void)
1904 T0
= _do_efdcmpeq(T0_64
, T1_64
);
1907 /* Double precision floating-point conversion to/from integer */
1908 static inline uint64_t _do_efdcfsi (int64_t val
)
1915 u
.f
= int64_to_float64(val
, &env
->spe_status
);
1920 static inline uint64_t _do_efdcfui (uint64_t val
)
1927 u
.f
= uint64_to_float64(val
, &env
->spe_status
);
1932 static inline int64_t _do_efdctsi (uint64_t val
)
1940 /* NaN are not treated the same way IEEE 754 does */
1941 if (unlikely(isnan(u
.f
)))
1944 return float64_to_int64(u
.f
, &env
->spe_status
);
1947 static inline uint64_t _do_efdctui (uint64_t val
)
1955 /* NaN are not treated the same way IEEE 754 does */
1956 if (unlikely(isnan(u
.f
)))
1959 return float64_to_uint64(u
.f
, &env
->spe_status
);
1962 static inline int64_t _do_efdctsiz (uint64_t val
)
1970 /* NaN are not treated the same way IEEE 754 does */
1971 if (unlikely(isnan(u
.f
)))
1974 return float64_to_int64_round_to_zero(u
.f
, &env
->spe_status
);
1977 static inline uint64_t _do_efdctuiz (uint64_t val
)
1985 /* NaN are not treated the same way IEEE 754 does */
1986 if (unlikely(isnan(u
.f
)))
1989 return float64_to_uint64_round_to_zero(u
.f
, &env
->spe_status
);
1992 void do_efdcfsi (void)
1994 T0_64
= _do_efdcfsi(T0_64
);
1997 void do_efdcfui (void)
1999 T0_64
= _do_efdcfui(T0_64
);
2002 void do_efdctsi (void)
2004 T0_64
= _do_efdctsi(T0_64
);
2007 void do_efdctui (void)
2009 T0_64
= _do_efdctui(T0_64
);
2012 void do_efdctsiz (void)
2014 T0_64
= _do_efdctsiz(T0_64
);
2017 void do_efdctuiz (void)
2019 T0_64
= _do_efdctuiz(T0_64
);
2022 /* Double precision floating-point conversion to/from fractional */
2023 static inline uint64_t _do_efdcfsf (int64_t val
)
2031 u
.f
= int32_to_float64(val
, &env
->spe_status
);
2032 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2033 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
2038 static inline uint64_t _do_efdcfuf (uint64_t val
)
2046 u
.f
= uint32_to_float64(val
, &env
->spe_status
);
2047 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2048 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
2053 static inline int64_t _do_efdctsf (uint64_t val
)
2062 /* NaN are not treated the same way IEEE 754 does */
2063 if (unlikely(isnan(u
.f
)))
2065 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2066 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2068 return float64_to_int32(u
.f
, &env
->spe_status
);
2071 static inline uint64_t _do_efdctuf (uint64_t val
)
2080 /* NaN are not treated the same way IEEE 754 does */
2081 if (unlikely(isnan(u
.f
)))
2083 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2084 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2086 return float64_to_uint32(u
.f
, &env
->spe_status
);
2089 static inline int64_t _do_efdctsfz (uint64_t val
)
2098 /* NaN are not treated the same way IEEE 754 does */
2099 if (unlikely(isnan(u
.f
)))
2101 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2102 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2104 return float64_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
2107 static inline uint64_t _do_efdctufz (uint64_t val
)
2116 /* NaN are not treated the same way IEEE 754 does */
2117 if (unlikely(isnan(u
.f
)))
2119 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2120 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2122 return float64_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2125 void do_efdcfsf (void)
2127 T0_64
= _do_efdcfsf(T0_64
);
2130 void do_efdcfuf (void)
2132 T0_64
= _do_efdcfuf(T0_64
);
2135 void do_efdctsf (void)
2137 T0_64
= _do_efdctsf(T0_64
);
2140 void do_efdctuf (void)
2142 T0_64
= _do_efdctuf(T0_64
);
2145 void do_efdctsfz (void)
2147 T0_64
= _do_efdctsfz(T0_64
);
2150 void do_efdctufz (void)
2152 T0_64
= _do_efdctufz(T0_64
);
2155 /* Floating point conversion between single and double precision */
2156 static inline uint32_t _do_efscfd (uint64_t val
)
2168 u2
.f
= float64_to_float32(u1
.f
, &env
->spe_status
);
2173 static inline uint64_t _do_efdcfs (uint32_t val
)
2185 u2
.f
= float32_to_float64(u1
.f
, &env
->spe_status
);
2190 void do_efscfd (void)
2192 T0_64
= _do_efscfd(T0_64
);
2195 void do_efdcfs (void)
2197 T0_64
= _do_efdcfs(T0_64
);
2200 /* Single precision fixed-point vector arithmetic */
2216 /* Single-precision floating-point comparisons */
2217 static inline int _do_efscmplt (uint32_t op1
, uint32_t op2
)
2219 /* XXX: TODO: test special values (NaN, infinites, ...) */
2220 return _do_efststlt(op1
, op2
);
2223 static inline int _do_efscmpgt (uint32_t op1
, uint32_t op2
)
2225 /* XXX: TODO: test special values (NaN, infinites, ...) */
2226 return _do_efststgt(op1
, op2
);
2229 static inline int _do_efscmpeq (uint32_t op1
, uint32_t op2
)
2231 /* XXX: TODO: test special values (NaN, infinites, ...) */
2232 return _do_efststeq(op1
, op2
);
2235 void do_efscmplt (void)
2237 T0
= _do_efscmplt(T0_64
, T1_64
);
2240 void do_efscmpgt (void)
2242 T0
= _do_efscmpgt(T0_64
, T1_64
);
2245 void do_efscmpeq (void)
2247 T0
= _do_efscmpeq(T0_64
, T1_64
);
2250 /* Single-precision floating-point vector comparisons */
2252 DO_SPE_CMP(fscmplt
);
2254 DO_SPE_CMP(fscmpgt
);
2256 DO_SPE_CMP(fscmpeq
);
2258 DO_SPE_CMP(fststlt
);
2260 DO_SPE_CMP(fststgt
);
2262 DO_SPE_CMP(fststeq
);
2264 /* Single-precision floating-point vector conversions */
2278 DO_SPE_OP1(fsctsiz
);
2280 DO_SPE_OP1(fsctuiz
);
2285 #endif /* defined(TARGET_PPCEMB) */
2287 /*****************************************************************************/
2288 /* Softmmu support */
2289 #if !defined (CONFIG_USER_ONLY)
2291 #define MMUSUFFIX _mmu
2292 #define GETPC() (__builtin_return_address(0))
2295 #include "softmmu_template.h"
2298 #include "softmmu_template.h"
2301 #include "softmmu_template.h"
2304 #include "softmmu_template.h"
2306 /* try to fill the TLB and return an exception if error. If retaddr is
2307 NULL, it means that the function was called in C code (i.e. not
2308 from generated code or from helper.c) */
2309 /* XXX: fix it to restore all registers */
2310 void tlb_fill (target_ulong addr
, int is_write
, int is_user
, void *retaddr
)
2312 TranslationBlock
*tb
;
2313 CPUState
*saved_env
;
2314 target_phys_addr_t pc
;
2317 /* XXX: hack to restore env in all cases, even if not called from
2320 env
= cpu_single_env
;
2321 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, is_user
, 1);
2322 if (unlikely(ret
!= 0)) {
2323 if (likely(retaddr
)) {
2324 /* now we have a real cpu fault */
2325 pc
= (target_phys_addr_t
)(unsigned long)retaddr
;
2326 tb
= tb_find_pc(pc
);
2328 /* the PC is inside the translated code. It means that we have
2329 a virtual CPU fault */
2330 cpu_restore_state(tb
, env
, pc
, NULL
);
2333 do_raise_exception_err(env
->exception_index
, env
->error_code
);
2338 /* Software driven TLBs management */
2339 /* PowerPC 602/603 software TLB load instructions helpers */
2340 void do_load_6xx_tlb (int is_code
)
2342 target_ulong RPN
, CMP
, EPN
;
2345 RPN
= env
->spr
[SPR_RPA
];
2347 CMP
= env
->spr
[SPR_ICMP
];
2348 EPN
= env
->spr
[SPR_IMISS
];
2350 CMP
= env
->spr
[SPR_DCMP
];
2351 EPN
= env
->spr
[SPR_DMISS
];
2353 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2354 #if defined (DEBUG_SOFTWARE_TLB)
2355 if (loglevel
!= 0) {
2356 fprintf(logfile
, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2357 __func__
, (unsigned long)T0
, (unsigned long)EPN
,
2358 (unsigned long)CMP
, (unsigned long)RPN
, way
);
2361 /* Store this TLB */
2362 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2363 way
, is_code
, CMP
, RPN
);
2366 void do_load_74xx_tlb (int is_code
)
2368 target_ulong RPN
, CMP
, EPN
;
2371 RPN
= env
->spr
[SPR_PTELO
];
2372 CMP
= env
->spr
[SPR_PTEHI
];
2373 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2374 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2375 #if defined (DEBUG_SOFTWARE_TLB)
2376 if (loglevel
!= 0) {
2377 fprintf(logfile
, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2378 __func__
, (unsigned long)T0
, (unsigned long)EPN
,
2379 (unsigned long)CMP
, (unsigned long)RPN
, way
);
2382 /* Store this TLB */
2383 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2384 way
, is_code
, CMP
, RPN
);
2387 static target_ulong
booke_tlb_to_page_size (int size
)
2389 return 1024 << (2 * size
);
2392 static int booke_page_size_to_tlb (target_ulong page_size
)
2396 switch (page_size
) {
2430 #if defined (TARGET_PPC64)
2431 case 0x000100000000ULL
:
2434 case 0x000400000000ULL
:
2437 case 0x001000000000ULL
:
2440 case 0x004000000000ULL
:
2443 case 0x010000000000ULL
:
2455 /* Helpers for 4xx TLB management */
2456 void do_4xx_tlbre_lo (void)
2462 tlb
= &env
->tlb
[T0
].tlbe
;
2464 if (tlb
->prot
& PAGE_VALID
)
2466 size
= booke_page_size_to_tlb(tlb
->size
);
2467 if (size
< 0 || size
> 0x7)
2470 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2473 void do_4xx_tlbre_hi (void)
2478 tlb
= &env
->tlb
[T0
].tlbe
;
2480 if (tlb
->prot
& PAGE_EXEC
)
2482 if (tlb
->prot
& PAGE_WRITE
)
2486 void do_4xx_tlbwe_hi (void)
2489 target_ulong page
, end
;
2491 #if defined (DEBUG_SOFTWARE_TLB)
2492 if (loglevel
!= 0) {
2493 fprintf(logfile
, "%s T0 " REGX
" T1 " REGX
"\n", __func__
, T0
, T1
);
2497 tlb
= &env
->tlb
[T0
].tlbe
;
2498 /* Invalidate previous TLB (if it's valid) */
2499 if (tlb
->prot
& PAGE_VALID
) {
2500 end
= tlb
->EPN
+ tlb
->size
;
2501 #if defined (DEBUG_SOFTWARE_TLB)
2502 if (loglevel
!= 0) {
2503 fprintf(logfile
, "%s: invalidate old TLB %d start " ADDRX
2504 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2507 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2508 tlb_flush_page(env
, page
);
2510 tlb
->size
= booke_tlb_to_page_size((T1
>> 7) & 0x7);
2511 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2512 * If this ever occurs, one should use the ppcemb target instead
2513 * of the ppc or ppc64 one
2515 if ((T1
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
2516 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
2517 "are not supported (%d)\n",
2518 tlb
->size
, TARGET_PAGE_SIZE
, (int)((T1
>> 7) & 0x7));
2520 tlb
->EPN
= T1
& ~(tlb
->size
- 1);
2522 tlb
->prot
|= PAGE_VALID
;
2524 tlb
->prot
&= ~PAGE_VALID
;
2526 /* XXX: TO BE FIXED */
2527 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
2529 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2530 tlb
->attr
= T1
& 0xFF;
2531 #if defined (DEBUG_SOFTWARE_TLB)
2532 if (loglevel
!= 0) {
2533 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2534 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2535 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2536 tlb
->prot
& PAGE_READ
? 'r' : '-',
2537 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2538 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2539 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2542 /* Invalidate new TLB (if valid) */
2543 if (tlb
->prot
& PAGE_VALID
) {
2544 end
= tlb
->EPN
+ tlb
->size
;
2545 #if defined (DEBUG_SOFTWARE_TLB)
2546 if (loglevel
!= 0) {
2547 fprintf(logfile
, "%s: invalidate TLB %d start " ADDRX
2548 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2551 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2552 tlb_flush_page(env
, page
);
2556 void do_4xx_tlbwe_lo (void)
2560 #if defined (DEBUG_SOFTWARE_TLB)
2561 if (loglevel
!= 0) {
2562 fprintf(logfile
, "%s T0 " REGX
" T1 " REGX
"\n", __func__
, T0
, T1
);
2566 tlb
= &env
->tlb
[T0
].tlbe
;
2567 tlb
->RPN
= T1
& 0xFFFFFC00;
2568 tlb
->prot
= PAGE_READ
;
2570 tlb
->prot
|= PAGE_EXEC
;
2572 tlb
->prot
|= PAGE_WRITE
;
2573 #if defined (DEBUG_SOFTWARE_TLB)
2574 if (loglevel
!= 0) {
2575 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2576 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2577 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2578 tlb
->prot
& PAGE_READ
? 'r' : '-',
2579 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2580 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2581 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2586 /* PowerPC 440 TLB management */
2587 void do_440_tlbwe (int word
)
2590 target_ulong EPN
, RPN
, size
;
2593 #if defined (DEBUG_SOFTWARE_TLB)
2594 if (loglevel
!= 0) {
2595 fprintf(logfile
, "%s word %d T0 " REGX
" T1 " REGX
"\n",
2596 __func__
, word
, T0
, T1
);
2601 tlb
= &env
->tlb
[T0
].tlbe
;
2604 /* Just here to please gcc */
2606 EPN
= T1
& 0xFFFFFC00;
2607 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
2610 size
= booke_tlb_to_page_size((T1
>> 4) & 0xF);
2611 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
2615 tlb
->attr
|= (T1
>> 8) & 1;
2617 tlb
->prot
|= PAGE_VALID
;
2619 if (tlb
->prot
& PAGE_VALID
) {
2620 tlb
->prot
&= ~PAGE_VALID
;
2624 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
2629 RPN
= T1
& 0xFFFFFC0F;
2630 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
2635 tlb
->attr
= (tlb
->attr
& 0x1) | (T1
& 0x0000FF00);
2636 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
2638 tlb
->prot
|= PAGE_READ
<< 4;
2640 tlb
->prot
|= PAGE_WRITE
<< 4;
2642 tlb
->prot
|= PAGE_EXEC
<< 4;
2644 tlb
->prot
|= PAGE_READ
;
2646 tlb
->prot
|= PAGE_WRITE
;
2648 tlb
->prot
|= PAGE_EXEC
;
2653 void do_440_tlbre (int word
)
2659 tlb
= &env
->tlb
[T0
].tlbe
;
2662 /* Just here to please gcc */
2665 size
= booke_page_size_to_tlb(tlb
->size
);
2666 if (size
< 0 || size
> 0xF)
2669 if (tlb
->attr
& 0x1)
2671 if (tlb
->prot
& PAGE_VALID
)
2673 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
2674 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
2680 T0
= tlb
->attr
& ~0x1;
2681 if (tlb
->prot
& (PAGE_READ
<< 4))
2683 if (tlb
->prot
& (PAGE_WRITE
<< 4))
2685 if (tlb
->prot
& (PAGE_EXEC
<< 4))
2687 if (tlb
->prot
& PAGE_READ
)
2689 if (tlb
->prot
& PAGE_WRITE
)
2691 if (tlb
->prot
& PAGE_EXEC
)
2696 #endif /* !CONFIG_USER_ONLY */