]>
git.proxmox.com Git - mirror_qemu.git/blob - target-ppc/op_helper.c
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 void cpu_dump_EA (target_ulong EA
);
72 void do_print_mem_EA (target_ulong EA
)
77 /*****************************************************************************/
78 /* Registers load and stores */
79 void do_load_cr (void)
81 T0
= (env
->crf
[0] << 28) |
91 void do_store_cr (uint32_t mask
)
95 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
97 env
->crf
[i
] = (T0
>> (sh
* 4)) & 0xFUL
;
101 void do_load_xer (void)
103 T0
= (xer_so
<< XER_SO
) |
107 (xer_cmp
<< XER_CMP
);
110 void do_store_xer (void)
112 xer_so
= (T0
>> XER_SO
) & 0x01;
113 xer_ov
= (T0
>> XER_OV
) & 0x01;
114 xer_ca
= (T0
>> XER_CA
) & 0x01;
115 xer_cmp
= (T0
>> XER_CMP
) & 0xFF;
116 xer_bc
= (T0
>> XER_BC
) & 0x7F;
119 void do_load_fpscr (void)
121 /* The 32 MSB of the target fpr are undefined.
132 #if defined(WORDS_BIGENDIAN)
141 for (i
= 0; i
< 8; i
++)
142 u
.s
.u
[WORD1
] |= env
->fpscr
[i
] << (4 * i
);
146 void do_store_fpscr (uint32_t mask
)
149 * We use only the 32 LSB of the incoming fpr
161 env
->fpscr
[0] = (env
->fpscr
[0] & 0x9) | ((u
.s
.u
[WORD1
] >> 28) & ~0x9);
162 for (i
= 1; i
< 7; i
++) {
163 if (mask
& (1 << (7 - i
)))
164 env
->fpscr
[i
] = (u
.s
.u
[WORD1
] >> (4 * (7 - i
))) & 0xF;
166 /* TODO: update FEX & VX */
167 /* Set rounding mode */
168 switch (env
->fpscr
[0] & 0x3) {
170 /* Best approximation (round to nearest) */
171 rnd_type
= float_round_nearest_even
;
174 /* Smaller magnitude (round toward zero) */
175 rnd_type
= float_round_to_zero
;
178 /* Round toward +infinite */
179 rnd_type
= float_round_up
;
183 /* Round toward -infinite */
184 rnd_type
= float_round_down
;
187 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
190 target_ulong
ppc_load_dump_spr (int sprn
)
193 fprintf(logfile
, "Read SPR %d %03x => " ADDRX
"\n",
194 sprn
, sprn
, env
->spr
[sprn
]);
197 return env
->spr
[sprn
];
200 void ppc_store_dump_spr (int sprn
, target_ulong val
)
203 fprintf(logfile
, "Write SPR %d %03x => " ADDRX
" <= " ADDRX
"\n",
204 sprn
, sprn
, env
->spr
[sprn
], val
);
206 env
->spr
[sprn
] = val
;
209 /*****************************************************************************/
210 /* Fixed point operations helpers */
211 #if defined(TARGET_PPC64)
212 static void add128 (uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
221 static void neg128 (uint64_t *plow
, uint64_t *phigh
)
225 add128(plow
, phigh
, 1, 0);
228 static void mul64 (uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
230 uint32_t a0
, a1
, b0
, b1
;
239 v
= (uint64_t)a0
* (uint64_t)b0
;
243 v
= (uint64_t)a0
* (uint64_t)b1
;
244 add128(plow
, phigh
, v
<< 32, v
>> 32);
246 v
= (uint64_t)a1
* (uint64_t)b0
;
247 add128(plow
, phigh
, v
<< 32, v
>> 32);
249 v
= (uint64_t)a1
* (uint64_t)b1
;
251 #if defined(DEBUG_MULDIV)
252 printf("mul: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
253 a
, b
, *phigh
, *plow
);
257 void do_mul64 (uint64_t *plow
, uint64_t *phigh
)
259 mul64(plow
, phigh
, T0
, T1
);
262 static void imul64 (uint64_t *plow
, uint64_t *phigh
, int64_t a
, int64_t b
)
271 mul64(plow
, phigh
, a
, b
);
277 void do_imul64 (uint64_t *plow
, uint64_t *phigh
)
279 imul64(plow
, phigh
, T0
, T1
);
287 if (likely(!((uint32_t)T0
< (uint32_t)T2
||
288 (xer_ca
== 1 && (uint32_t)T0
== (uint32_t)T2
)))) {
295 #if defined(TARGET_PPC64)
296 void do_adde_64 (void)
300 if (likely(!((uint64_t)T0
< (uint64_t)T2
||
301 (xer_ca
== 1 && (uint64_t)T0
== (uint64_t)T2
)))) {
309 void do_addmeo (void)
313 if (likely(!((uint32_t)T1
&
314 ((uint32_t)T1
^ (uint32_t)T0
) & (1UL << 31)))) {
324 #if defined(TARGET_PPC64)
325 void do_addmeo_64 (void)
329 if (likely(!((uint64_t)T1
&
330 ((uint64_t)T1
^ (uint64_t)T0
) & (1ULL << 63)))) {
343 if (likely(!(((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) ||
344 (int32_t)T1
== 0))) {
346 T0
= (int32_t)T0
/ (int32_t)T1
;
350 T0
= (-1) * ((uint32_t)T0
>> 31);
354 #if defined(TARGET_PPC64)
357 if (likely(!(((int64_t)T0
== INT64_MIN
&& (int64_t)T1
== -1ULL) ||
358 (int64_t)T1
== 0))) {
360 T0
= (int64_t)T0
/ (int64_t)T1
;
364 T0
= (-1ULL) * ((uint64_t)T0
>> 63);
369 void do_divwuo (void)
371 if (likely((uint32_t)T1
!= 0)) {
373 T0
= (uint32_t)T0
/ (uint32_t)T1
;
381 #if defined(TARGET_PPC64)
382 void do_divduo (void)
384 if (likely((uint64_t)T1
!= 0)) {
386 T0
= (uint64_t)T0
/ (uint64_t)T1
;
395 void do_mullwo (void)
397 int64_t res
= (int64_t)T0
* (int64_t)T1
;
399 if (likely((int32_t)res
== res
)) {
408 #if defined(TARGET_PPC64)
409 void do_mulldo (void)
415 if (likely(th
== 0)) {
427 if (likely((int32_t)T0
!= INT32_MIN
)) {
436 #if defined(TARGET_PPC64)
437 void do_nego_64 (void)
439 if (likely((int64_t)T0
!= INT64_MIN
)) {
451 T0
= T1
+ ~T0
+ xer_ca
;
452 if (likely((uint32_t)T0
>= (uint32_t)T1
&&
453 (xer_ca
== 0 || (uint32_t)T0
!= (uint32_t)T1
))) {
460 #if defined(TARGET_PPC64)
461 void do_subfe_64 (void)
463 T0
= T1
+ ~T0
+ xer_ca
;
464 if (likely((uint64_t)T0
>= (uint64_t)T1
&&
465 (xer_ca
== 0 || (uint64_t)T0
!= (uint64_t)T1
))) {
473 void do_subfmeo (void)
476 T0
= ~T0
+ xer_ca
- 1;
477 if (likely(!((uint32_t)~T1
& ((uint32_t)~T1
^ (uint32_t)T0
) &
484 if (likely((uint32_t)T1
!= UINT32_MAX
))
488 #if defined(TARGET_PPC64)
489 void do_subfmeo_64 (void)
492 T0
= ~T0
+ xer_ca
- 1;
493 if (likely(!((uint64_t)~T1
& ((uint64_t)~T1
^ (uint64_t)T0
) &
500 if (likely((uint64_t)T1
!= UINT64_MAX
))
505 void do_subfzeo (void)
509 if (likely(!(((uint32_t)~T1
^ UINT32_MAX
) &
510 ((uint32_t)(~T1
) ^ (uint32_t)T0
) & (1UL << 31)))) {
516 if (likely((uint32_t)T0
>= (uint32_t)~T1
)) {
523 #if defined(TARGET_PPC64)
524 void do_subfzeo_64 (void)
528 if (likely(!(((uint64_t)~T1
^ UINT64_MAX
) &
529 ((uint64_t)(~T1
) ^ (uint64_t)T0
) & (1ULL << 63)))) {
535 if (likely((uint64_t)T0
>= (uint64_t)~T1
)) {
543 /* shift right arithmetic helper */
548 if (likely(!(T1
& 0x20UL
))) {
549 if (likely((uint32_t)T1
!= 0)) {
550 ret
= (int32_t)T0
>> (T1
& 0x1fUL
);
551 if (likely(ret
>= 0 || ((int32_t)T0
& ((1 << T1
) - 1)) == 0)) {
561 ret
= (-1) * ((uint32_t)T0
>> 31);
562 if (likely(ret
>= 0 || ((uint32_t)T0
& ~0x80000000UL
) == 0)) {
571 #if defined(TARGET_PPC64)
576 if (likely(!(T1
& 0x40UL
))) {
577 if (likely((uint64_t)T1
!= 0)) {
578 ret
= (int64_t)T0
>> (T1
& 0x3FUL
);
579 if (likely(ret
>= 0 || ((int64_t)T0
& ((1 << T1
) - 1)) == 0)) {
589 ret
= (-1) * ((uint64_t)T0
>> 63);
590 if (likely(ret
>= 0 || ((uint64_t)T0
& ~0x8000000000000000ULL
) == 0)) {
600 static inline int popcnt (uint32_t val
)
604 for (i
= 0; val
!= 0;)
605 val
= val
^ (val
- 1);
610 void do_popcntb (void)
616 for (i
= 0; i
< 32; i
+= 8)
617 ret
|= popcnt((T0
>> i
) & 0xFF) << i
;
621 #if defined(TARGET_PPC64)
622 void do_popcntb_64 (void)
628 for (i
= 0; i
< 64; i
+= 8)
629 ret
|= popcnt((T0
>> i
) & 0xFF) << i
;
634 /*****************************************************************************/
635 /* Floating point operations helpers */
643 p
.i
= float64_to_int32(FT0
, &env
->fp_status
);
644 #if USE_PRECISE_EMULATION
645 /* XXX: higher bits are not supposed to be significant.
646 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
648 p
.i
|= 0xFFF80000ULL
<< 32;
653 void do_fctiwz (void)
660 p
.i
= float64_to_int32_round_to_zero(FT0
, &env
->fp_status
);
661 #if USE_PRECISE_EMULATION
662 /* XXX: higher bits are not supposed to be significant.
663 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
665 p
.i
|= 0xFFF80000ULL
<< 32;
670 #if defined(TARGET_PPC64)
679 FT0
= int64_to_float64(p
.i
, &env
->fp_status
);
689 p
.i
= float64_to_int64(FT0
, &env
->fp_status
);
693 void do_fctidz (void)
700 p
.i
= float64_to_int64_round_to_zero(FT0
, &env
->fp_status
);
706 #if USE_PRECISE_EMULATION
710 float128 ft0_128
, ft1_128
;
712 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
713 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
714 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
715 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
716 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
717 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
719 /* This is OK on x86 hosts */
720 FT0
= (FT0
* FT1
) + FT2
;
727 float128 ft0_128
, ft1_128
;
729 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
730 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
731 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
732 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
733 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
734 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
736 /* This is OK on x86 hosts */
737 FT0
= (FT0
* FT1
) - FT2
;
740 #endif /* USE_PRECISE_EMULATION */
742 void do_fnmadd (void)
744 #if USE_PRECISE_EMULATION
746 float128 ft0_128
, ft1_128
;
748 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
749 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
750 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
751 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
752 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
753 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
755 /* This is OK on x86 hosts */
756 FT0
= (FT0
* FT1
) + FT2
;
759 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
760 FT0
= float64_add(FT0
, FT2
, &env
->fp_status
);
762 if (likely(!isnan(FT0
)))
763 FT0
= float64_chs(FT0
);
766 void do_fnmsub (void)
768 #if USE_PRECISE_EMULATION
770 float128 ft0_128
, ft1_128
;
772 ft0_128
= float64_to_float128(FT0
, &env
->fp_status
);
773 ft1_128
= float64_to_float128(FT1
, &env
->fp_status
);
774 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
775 ft1_128
= float64_to_float128(FT2
, &env
->fp_status
);
776 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
777 FT0
= float128_to_float64(ft0_128
, &env
->fp_status
);
779 /* This is OK on x86 hosts */
780 FT0
= (FT0
* FT1
) - FT2
;
783 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
784 FT0
= float64_sub(FT0
, FT2
, &env
->fp_status
);
786 if (likely(!isnan(FT0
)))
787 FT0
= float64_chs(FT0
);
792 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
802 if (likely(isnormal(FT0
))) {
803 #if USE_PRECISE_EMULATION
804 FT0
= float64_div(1.0, FT0
, &env
->fp_status
);
805 FT0
= float64_to_float32(FT0
, &env
->fp_status
);
807 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
811 if (p
.i
== 0x8000000000000000ULL
) {
812 p
.i
= 0xFFF0000000000000ULL
;
813 } else if (p
.i
== 0x0000000000000000ULL
) {
814 p
.i
= 0x7FF0000000000000ULL
;
815 } else if (isnan(FT0
)) {
816 p
.i
= 0x7FF8000000000000ULL
;
817 } else if (FT0
< 0.0) {
818 p
.i
= 0x8000000000000000ULL
;
820 p
.i
= 0x0000000000000000ULL
;
826 void do_frsqrte (void)
833 if (likely(isnormal(FT0
) && FT0
> 0.0)) {
834 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
835 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
838 if (p
.i
== 0x8000000000000000ULL
) {
839 p
.i
= 0xFFF0000000000000ULL
;
840 } else if (p
.i
== 0x0000000000000000ULL
) {
841 p
.i
= 0x7FF0000000000000ULL
;
842 } else if (isnan(FT0
)) {
843 if (!(p
.i
& 0x0008000000000000ULL
))
844 p
.i
|= 0x000FFFFFFFFFFFFFULL
;
845 } else if (FT0
< 0) {
846 p
.i
= 0x7FF8000000000000ULL
;
848 p
.i
= 0x0000000000000000ULL
;
864 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
865 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
867 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
874 env
->fpscr
[4] |= 0x1;
875 env
->fpscr
[6] |= 0x1;
882 env
->fpscr
[4] &= ~0x1;
883 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
884 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
886 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
893 env
->fpscr
[4] |= 0x1;
894 if (!float64_is_signaling_nan(FT0
) || !float64_is_signaling_nan(FT1
)) {
896 env
->fpscr
[6] |= 0x1;
897 if (!(env
->fpscr
[1] & 0x8))
898 env
->fpscr
[4] |= 0x8;
900 env
->fpscr
[4] |= 0x8;
906 #if !defined (CONFIG_USER_ONLY)
907 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
);
910 #if defined(TARGET_PPC64)
911 if (env
->spr
[SPR_SRR1
] & (1ULL << MSR_SF
)) {
912 env
->nip
= (uint64_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
913 do_store_msr(env
, (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
915 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
916 ppc_store_msr_32(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
919 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
920 do_store_msr(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
922 #if defined (DEBUG_OP)
923 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
925 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
928 #if defined(TARGET_PPC64)
931 if (env
->spr
[SPR_SRR1
] & (1ULL << MSR_SF
)) {
932 env
->nip
= (uint64_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
933 do_store_msr(env
, (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
935 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
936 do_store_msr(env
, (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
));
938 #if defined (DEBUG_OP)
939 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
941 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
946 void do_tw (int flags
)
948 if (!likely(!(((int32_t)T0
< (int32_t)T1
&& (flags
& 0x10)) ||
949 ((int32_t)T0
> (int32_t)T1
&& (flags
& 0x08)) ||
950 ((int32_t)T0
== (int32_t)T1
&& (flags
& 0x04)) ||
951 ((uint32_t)T0
< (uint32_t)T1
&& (flags
& 0x02)) ||
952 ((uint32_t)T0
> (uint32_t)T1
&& (flags
& 0x01))))) {
953 do_raise_exception_err(EXCP_PROGRAM
, EXCP_TRAP
);
957 #if defined(TARGET_PPC64)
958 void do_td (int flags
)
960 if (!likely(!(((int64_t)T0
< (int64_t)T1
&& (flags
& 0x10)) ||
961 ((int64_t)T0
> (int64_t)T1
&& (flags
& 0x08)) ||
962 ((int64_t)T0
== (int64_t)T1
&& (flags
& 0x04)) ||
963 ((uint64_t)T0
< (uint64_t)T1
&& (flags
& 0x02)) ||
964 ((uint64_t)T0
> (uint64_t)T1
&& (flags
& 0x01)))))
965 do_raise_exception_err(EXCP_PROGRAM
, EXCP_TRAP
);
969 /*****************************************************************************/
970 /* PowerPC 601 specific instructions (POWER bridge) */
971 void do_POWER_abso (void)
973 if ((uint32_t)T0
== INT32_MIN
) {
983 void do_POWER_clcs (void)
987 /* Instruction cache line size */
988 T0
= ICACHE_LINE_SIZE
;
991 /* Data cache line size */
992 T0
= DCACHE_LINE_SIZE
;
995 /* Minimum cache line size */
996 T0
= ICACHE_LINE_SIZE
< DCACHE_LINE_SIZE
?
997 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
1000 /* Maximum cache line size */
1001 T0
= ICACHE_LINE_SIZE
> DCACHE_LINE_SIZE
?
1002 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
1010 void do_POWER_div (void)
1014 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1015 T0
= (long)((-1) * (T0
>> 31));
1016 env
->spr
[SPR_MQ
] = 0;
1018 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1019 env
->spr
[SPR_MQ
] = tmp
% T1
;
1020 T0
= tmp
/ (int32_t)T1
;
1024 void do_POWER_divo (void)
1028 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1029 T0
= (long)((-1) * (T0
>> 31));
1030 env
->spr
[SPR_MQ
] = 0;
1034 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1035 env
->spr
[SPR_MQ
] = tmp
% T1
;
1037 if (tmp
> (int64_t)INT32_MAX
|| tmp
< (int64_t)INT32_MIN
) {
1047 void do_POWER_divs (void)
1049 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1050 T0
= (long)((-1) * (T0
>> 31));
1051 env
->spr
[SPR_MQ
] = 0;
1053 env
->spr
[SPR_MQ
] = T0
% T1
;
1054 T0
= (int32_t)T0
/ (int32_t)T1
;
1058 void do_POWER_divso (void)
1060 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1061 T0
= (long)((-1) * (T0
>> 31));
1062 env
->spr
[SPR_MQ
] = 0;
1066 T0
= (int32_t)T0
/ (int32_t)T1
;
1067 env
->spr
[SPR_MQ
] = (int32_t)T0
% (int32_t)T1
;
1072 void do_POWER_dozo (void)
1074 if ((int32_t)T1
> (int32_t)T0
) {
1077 if (((uint32_t)(~T2
) ^ (uint32_t)T1
^ UINT32_MAX
) &
1078 ((uint32_t)(~T2
) ^ (uint32_t)T0
) & (1UL << 31)) {
1090 void do_POWER_maskg (void)
1094 if ((uint32_t)T0
== (uint32_t)(T1
+ 1)) {
1097 ret
= (((uint32_t)(-1)) >> ((uint32_t)T0
)) ^
1098 (((uint32_t)(-1) >> ((uint32_t)T1
)) >> 1);
1099 if ((uint32_t)T0
> (uint32_t)T1
)
1105 void do_POWER_mulo (void)
1109 tmp
= (uint64_t)T0
* (uint64_t)T1
;
1110 env
->spr
[SPR_MQ
] = tmp
>> 32;
1112 if (tmp
>> 32 != ((uint64_t)T0
>> 16) * ((uint64_t)T1
>> 16)) {
1120 #if !defined (CONFIG_USER_ONLY)
1121 void do_POWER_rac (void)
1126 /* We don't have to generate many instances of this instruction,
1127 * as rac is supervisor only.
1129 if (get_physical_address(env
, &ctx
, T0
, 0, ACCESS_INT
, 1) == 0)
1134 void do_POWER_rfsvc (void)
1136 env
->nip
= env
->lr
& ~0x00000003UL
;
1137 T0
= env
->ctr
& 0x0000FFFFUL
;
1138 do_store_msr(env
, T0
);
1139 #if defined (DEBUG_OP)
1140 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1142 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1145 /* PowerPC 601 BAT management helper */
1146 void do_store_601_batu (int nr
)
1148 do_store_ibatu(env
, nr
, (uint32_t)T0
);
1149 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1150 env
->DBAT
[1][nr
] = env
->IBAT
[1][nr
];
1154 /*****************************************************************************/
1155 /* 602 specific instructions */
1156 /* mfrom is the most crazy instruction ever seen, imho ! */
1157 /* Real implementation uses a ROM table. Do the same */
1158 #define USE_MFROM_ROM_TABLE
1159 void do_op_602_mfrom (void)
1161 if (likely(T0
< 602)) {
1162 #if defined(USE_MFROM_ROM_TABLE)
1163 #include "mfrom_table.c"
1164 T0
= mfrom_ROM_table
[T0
];
1167 /* Extremly decomposed:
1169 * T0 = 256 * log10(10 + 1.0) + 0.5
1172 d
= float64_div(d
, 256, &env
->fp_status
);
1174 d
= exp10(d
); // XXX: use float emulation function
1175 d
= float64_add(d
, 1.0, &env
->fp_status
);
1176 d
= log10(d
); // XXX: use float emulation function
1177 d
= float64_mul(d
, 256, &env
->fp_status
);
1178 d
= float64_add(d
, 0.5, &env
->fp_status
);
1179 T0
= float64_round_to_int(d
, &env
->fp_status
);
1186 /*****************************************************************************/
1187 /* Embedded PowerPC specific helpers */
1188 void do_405_check_ov (void)
1190 if (likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1191 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1199 void do_405_check_sat (void)
1201 if (!likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1202 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1203 /* Saturate result */
1212 #if !defined(CONFIG_USER_ONLY)
1213 void do_40x_rfci (void)
1215 env
->nip
= env
->spr
[SPR_40x_SRR2
];
1216 do_store_msr(env
, env
->spr
[SPR_40x_SRR3
] & ~0xFFFF0000);
1217 #if defined (DEBUG_OP)
1218 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1220 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1225 #if defined(TARGET_PPC64)
1226 if (env
->spr
[SPR_BOOKE_CSRR1
] & (1 << MSR_CM
)) {
1227 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_CSRR0
];
1231 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_CSRR0
];
1233 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_CSRR1
] & ~0x3FFF0000);
1234 #if defined (DEBUG_OP)
1235 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1237 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1242 #if defined(TARGET_PPC64)
1243 if (env
->spr
[SPR_BOOKE_DSRR1
] & (1 << MSR_CM
)) {
1244 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_DSRR0
];
1248 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_DSRR0
];
1250 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_DSRR1
] & ~0x3FFF0000);
1251 #if defined (DEBUG_OP)
1252 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1254 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1257 void do_rfmci (void)
1259 #if defined(TARGET_PPC64)
1260 if (env
->spr
[SPR_BOOKE_MCSRR1
] & (1 << MSR_CM
)) {
1261 env
->nip
= (uint64_t)env
->spr
[SPR_BOOKE_MCSRR0
];
1265 env
->nip
= (uint32_t)env
->spr
[SPR_BOOKE_MCSRR0
];
1267 do_store_msr(env
, (uint32_t)env
->spr
[SPR_BOOKE_MCSRR1
] & ~0x3FFF0000);
1268 #if defined (DEBUG_OP)
1269 cpu_dump_rfi(env
->nip
, do_load_msr(env
));
1271 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1274 void do_load_dcr (void)
1278 if (unlikely(env
->dcr_env
== NULL
)) {
1279 if (loglevel
!= 0) {
1280 fprintf(logfile
, "No DCR environment\n");
1282 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_INVAL_INVAL
);
1283 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, T0
, &val
) != 0)) {
1284 if (loglevel
!= 0) {
1285 fprintf(logfile
, "DCR read error %d %03x\n", (int)T0
, (int)T0
);
1287 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_PRIV_REG
);
1293 void do_store_dcr (void)
1295 if (unlikely(env
->dcr_env
== NULL
)) {
1296 if (loglevel
!= 0) {
1297 fprintf(logfile
, "No DCR environment\n");
1299 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_INVAL_INVAL
);
1300 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, T0
, T1
) != 0)) {
1301 if (loglevel
!= 0) {
1302 fprintf(logfile
, "DCR write error %d %03x\n", (int)T0
, (int)T0
);
1304 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_PRIV_REG
);
1308 void do_load_403_pb (int num
)
1313 void do_store_403_pb (int num
)
1315 if (likely(env
->pb
[num
] != T0
)) {
1317 /* Should be optimized */
1324 void do_440_dlmzb (void)
1330 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1331 if ((T0
& mask
) == 0)
1335 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1336 if ((T1
& mask
) == 0)
1344 #if defined(TARGET_PPCEMB)
1345 /* SPE extension helpers */
1346 /* Use a table to make this quicker */
1347 static uint8_t hbrev
[16] = {
1348 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1349 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1352 static inline uint8_t byte_reverse (uint8_t val
)
1354 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1357 static inline uint32_t word_reverse (uint32_t val
)
1359 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1360 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1363 #define MASKBITS 16 // Random value - to be fixed
1364 void do_brinc (void)
1366 uint32_t a
, b
, d
, mask
;
1368 mask
= (uint32_t)(-1UL) >> MASKBITS
;
1371 d
= word_reverse(1 + word_reverse(a
| ~mask
));
1372 T0_64
= (T0_64
& ~mask
) | (d
& mask
);
1375 #define DO_SPE_OP2(name) \
1376 void do_ev##name (void) \
1378 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) | \
1379 (uint64_t)_do_e##name(T0_64, T1_64); \
1382 #define DO_SPE_OP1(name) \
1383 void do_ev##name (void) \
1385 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) | \
1386 (uint64_t)_do_e##name(T0_64); \
1389 /* Fixed-point vector arithmetic */
1390 static inline uint32_t _do_eabs (uint32_t val
)
1392 if (val
!= 0x80000000)
1398 static inline uint32_t _do_eaddw (uint32_t op1
, uint32_t op2
)
1403 static inline int _do_ecntlsw (uint32_t val
)
1405 if (val
& 0x80000000)
1406 return _do_cntlzw(~val
);
1408 return _do_cntlzw(val
);
1411 static inline int _do_ecntlzw (uint32_t val
)
1413 return _do_cntlzw(val
);
1416 static inline uint32_t _do_eneg (uint32_t val
)
1418 if (val
!= 0x80000000)
1424 static inline uint32_t _do_erlw (uint32_t op1
, uint32_t op2
)
1426 return rotl32(op1
, op2
);
1429 static inline uint32_t _do_erndw (uint32_t val
)
1431 return (val
+ 0x000080000000) & 0xFFFF0000;
1434 static inline uint32_t _do_eslw (uint32_t op1
, uint32_t op2
)
1436 /* No error here: 6 bits are used */
1437 return op1
<< (op2
& 0x3F);
1440 static inline int32_t _do_esrws (int32_t op1
, uint32_t op2
)
1442 /* No error here: 6 bits are used */
1443 return op1
>> (op2
& 0x3F);
1446 static inline uint32_t _do_esrwu (uint32_t op1
, uint32_t op2
)
1448 /* No error here: 6 bits are used */
1449 return op1
>> (op2
& 0x3F);
1452 static inline uint32_t _do_esubfw (uint32_t op1
, uint32_t op2
)
1480 /* evsel is a little bit more complicated... */
1481 static inline uint32_t _do_esel (uint32_t op1
, uint32_t op2
, int n
)
1489 void do_evsel (void)
1491 T0_64
= ((uint64_t)_do_esel(T0_64
>> 32, T1_64
>> 32, T0
>> 3) << 32) |
1492 (uint64_t)_do_esel(T0_64
, T1_64
, (T0
>> 2) & 1);
1495 /* Fixed-point vector comparisons */
1496 #define DO_SPE_CMP(name) \
1497 void do_ev##name (void) \
1499 T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32, \
1500 T1_64 >> 32) << 32, \
1501 _do_e##name(T0_64, T1_64)); \
1504 static inline uint32_t _do_evcmp_merge (int t0
, int t1
)
1506 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1508 static inline int _do_ecmpeq (uint32_t op1
, uint32_t op2
)
1510 return op1
== op2
? 1 : 0;
1513 static inline int _do_ecmpgts (int32_t op1
, int32_t op2
)
1515 return op1
> op2
? 1 : 0;
1518 static inline int _do_ecmpgtu (uint32_t op1
, uint32_t op2
)
1520 return op1
> op2
? 1 : 0;
1523 static inline int _do_ecmplts (int32_t op1
, int32_t op2
)
1525 return op1
< op2
? 1 : 0;
1528 static inline int _do_ecmpltu (uint32_t op1
, uint32_t op2
)
1530 return op1
< op2
? 1 : 0;
1544 /* Single precision floating-point conversions from/to integer */
1545 static inline uint32_t _do_efscfsi (int32_t val
)
1552 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1557 static inline uint32_t _do_efscfui (uint32_t val
)
1564 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1569 static inline int32_t _do_efsctsi (uint32_t val
)
1577 /* NaN are not treated the same way IEEE 754 does */
1578 if (unlikely(isnan(u
.f
)))
1581 return float32_to_int32(u
.f
, &env
->spe_status
);
1584 static inline uint32_t _do_efsctui (uint32_t val
)
1592 /* NaN are not treated the same way IEEE 754 does */
1593 if (unlikely(isnan(u
.f
)))
1596 return float32_to_uint32(u
.f
, &env
->spe_status
);
1599 static inline int32_t _do_efsctsiz (uint32_t val
)
1607 /* NaN are not treated the same way IEEE 754 does */
1608 if (unlikely(isnan(u
.f
)))
1611 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1614 static inline uint32_t _do_efsctuiz (uint32_t val
)
1622 /* NaN are not treated the same way IEEE 754 does */
1623 if (unlikely(isnan(u
.f
)))
1626 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1629 void do_efscfsi (void)
1631 T0_64
= _do_efscfsi(T0_64
);
1634 void do_efscfui (void)
1636 T0_64
= _do_efscfui(T0_64
);
1639 void do_efsctsi (void)
1641 T0_64
= _do_efsctsi(T0_64
);
1644 void do_efsctui (void)
1646 T0_64
= _do_efsctui(T0_64
);
1649 void do_efsctsiz (void)
1651 T0_64
= _do_efsctsiz(T0_64
);
1654 void do_efsctuiz (void)
1656 T0_64
= _do_efsctuiz(T0_64
);
1659 /* Single precision floating-point conversion to/from fractional */
1660 static inline uint32_t _do_efscfsf (uint32_t val
)
1668 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1669 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
1670 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1675 static inline uint32_t _do_efscfuf (uint32_t val
)
1683 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1684 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1685 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1690 static inline int32_t _do_efsctsf (uint32_t val
)
1699 /* NaN are not treated the same way IEEE 754 does */
1700 if (unlikely(isnan(u
.f
)))
1702 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1703 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1705 return float32_to_int32(u
.f
, &env
->spe_status
);
1708 static inline uint32_t _do_efsctuf (uint32_t val
)
1717 /* NaN are not treated the same way IEEE 754 does */
1718 if (unlikely(isnan(u
.f
)))
1720 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1721 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1723 return float32_to_uint32(u
.f
, &env
->spe_status
);
1726 static inline int32_t _do_efsctsfz (uint32_t val
)
1735 /* NaN are not treated the same way IEEE 754 does */
1736 if (unlikely(isnan(u
.f
)))
1738 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1739 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1741 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1744 static inline uint32_t _do_efsctufz (uint32_t val
)
1753 /* NaN are not treated the same way IEEE 754 does */
1754 if (unlikely(isnan(u
.f
)))
1756 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1757 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1759 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1762 void do_efscfsf (void)
1764 T0_64
= _do_efscfsf(T0_64
);
1767 void do_efscfuf (void)
1769 T0_64
= _do_efscfuf(T0_64
);
1772 void do_efsctsf (void)
1774 T0_64
= _do_efsctsf(T0_64
);
1777 void do_efsctuf (void)
1779 T0_64
= _do_efsctuf(T0_64
);
1782 void do_efsctsfz (void)
1784 T0_64
= _do_efsctsfz(T0_64
);
1787 void do_efsctufz (void)
1789 T0_64
= _do_efsctufz(T0_64
);
1792 /* Double precision floating point helpers */
1793 static inline int _do_efdcmplt (uint64_t op1
, uint64_t op2
)
1795 /* XXX: TODO: test special values (NaN, infinites, ...) */
1796 return _do_efdtstlt(op1
, op2
);
1799 static inline int _do_efdcmpgt (uint64_t op1
, uint64_t op2
)
1801 /* XXX: TODO: test special values (NaN, infinites, ...) */
1802 return _do_efdtstgt(op1
, op2
);
1805 static inline int _do_efdcmpeq (uint64_t op1
, uint64_t op2
)
1807 /* XXX: TODO: test special values (NaN, infinites, ...) */
1808 return _do_efdtsteq(op1
, op2
);
1811 void do_efdcmplt (void)
1813 T0
= _do_efdcmplt(T0_64
, T1_64
);
1816 void do_efdcmpgt (void)
1818 T0
= _do_efdcmpgt(T0_64
, T1_64
);
1821 void do_efdcmpeq (void)
1823 T0
= _do_efdcmpeq(T0_64
, T1_64
);
1826 /* Double precision floating-point conversion to/from integer */
1827 static inline uint64_t _do_efdcfsi (int64_t val
)
1834 u
.f
= int64_to_float64(val
, &env
->spe_status
);
1839 static inline uint64_t _do_efdcfui (uint64_t val
)
1846 u
.f
= uint64_to_float64(val
, &env
->spe_status
);
1851 static inline int64_t _do_efdctsi (uint64_t val
)
1859 /* NaN are not treated the same way IEEE 754 does */
1860 if (unlikely(isnan(u
.f
)))
1863 return float64_to_int64(u
.f
, &env
->spe_status
);
1866 static inline uint64_t _do_efdctui (uint64_t val
)
1874 /* NaN are not treated the same way IEEE 754 does */
1875 if (unlikely(isnan(u
.f
)))
1878 return float64_to_uint64(u
.f
, &env
->spe_status
);
1881 static inline int64_t _do_efdctsiz (uint64_t val
)
1889 /* NaN are not treated the same way IEEE 754 does */
1890 if (unlikely(isnan(u
.f
)))
1893 return float64_to_int64_round_to_zero(u
.f
, &env
->spe_status
);
1896 static inline uint64_t _do_efdctuiz (uint64_t val
)
1904 /* NaN are not treated the same way IEEE 754 does */
1905 if (unlikely(isnan(u
.f
)))
1908 return float64_to_uint64_round_to_zero(u
.f
, &env
->spe_status
);
1911 void do_efdcfsi (void)
1913 T0_64
= _do_efdcfsi(T0_64
);
1916 void do_efdcfui (void)
1918 T0_64
= _do_efdcfui(T0_64
);
1921 void do_efdctsi (void)
1923 T0_64
= _do_efdctsi(T0_64
);
1926 void do_efdctui (void)
1928 T0_64
= _do_efdctui(T0_64
);
1931 void do_efdctsiz (void)
1933 T0_64
= _do_efdctsiz(T0_64
);
1936 void do_efdctuiz (void)
1938 T0_64
= _do_efdctuiz(T0_64
);
1941 /* Double precision floating-point conversion to/from fractional */
1942 static inline uint64_t _do_efdcfsf (int64_t val
)
1950 u
.f
= int32_to_float64(val
, &env
->spe_status
);
1951 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
1952 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
1957 static inline uint64_t _do_efdcfuf (uint64_t val
)
1965 u
.f
= uint32_to_float64(val
, &env
->spe_status
);
1966 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
1967 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
1972 static inline int64_t _do_efdctsf (uint64_t val
)
1981 /* NaN are not treated the same way IEEE 754 does */
1982 if (unlikely(isnan(u
.f
)))
1984 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1985 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1987 return float64_to_int32(u
.f
, &env
->spe_status
);
1990 static inline uint64_t _do_efdctuf (uint64_t val
)
1999 /* NaN are not treated the same way IEEE 754 does */
2000 if (unlikely(isnan(u
.f
)))
2002 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2003 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2005 return float64_to_uint32(u
.f
, &env
->spe_status
);
2008 static inline int64_t _do_efdctsfz (uint64_t val
)
2017 /* NaN are not treated the same way IEEE 754 does */
2018 if (unlikely(isnan(u
.f
)))
2020 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2021 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2023 return float64_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
2026 static inline uint64_t _do_efdctufz (uint64_t val
)
2035 /* NaN are not treated the same way IEEE 754 does */
2036 if (unlikely(isnan(u
.f
)))
2038 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2039 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
2041 return float64_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2044 void do_efdcfsf (void)
2046 T0_64
= _do_efdcfsf(T0_64
);
2049 void do_efdcfuf (void)
2051 T0_64
= _do_efdcfuf(T0_64
);
2054 void do_efdctsf (void)
2056 T0_64
= _do_efdctsf(T0_64
);
2059 void do_efdctuf (void)
2061 T0_64
= _do_efdctuf(T0_64
);
2064 void do_efdctsfz (void)
2066 T0_64
= _do_efdctsfz(T0_64
);
2069 void do_efdctufz (void)
2071 T0_64
= _do_efdctufz(T0_64
);
2074 /* Floating point conversion between single and double precision */
2075 static inline uint32_t _do_efscfd (uint64_t val
)
2087 u2
.f
= float64_to_float32(u1
.f
, &env
->spe_status
);
2092 static inline uint64_t _do_efdcfs (uint32_t val
)
2104 u2
.f
= float32_to_float64(u1
.f
, &env
->spe_status
);
2109 void do_efscfd (void)
2111 T0_64
= _do_efscfd(T0_64
);
2114 void do_efdcfs (void)
2116 T0_64
= _do_efdcfs(T0_64
);
2119 /* Single precision fixed-point vector arithmetic */
2135 /* Single-precision floating-point comparisons */
2136 static inline int _do_efscmplt (uint32_t op1
, uint32_t op2
)
2138 /* XXX: TODO: test special values (NaN, infinites, ...) */
2139 return _do_efststlt(op1
, op2
);
2142 static inline int _do_efscmpgt (uint32_t op1
, uint32_t op2
)
2144 /* XXX: TODO: test special values (NaN, infinites, ...) */
2145 return _do_efststgt(op1
, op2
);
2148 static inline int _do_efscmpeq (uint32_t op1
, uint32_t op2
)
2150 /* XXX: TODO: test special values (NaN, infinites, ...) */
2151 return _do_efststeq(op1
, op2
);
2154 void do_efscmplt (void)
2156 T0
= _do_efscmplt(T0_64
, T1_64
);
2159 void do_efscmpgt (void)
2161 T0
= _do_efscmpgt(T0_64
, T1_64
);
2164 void do_efscmpeq (void)
2166 T0
= _do_efscmpeq(T0_64
, T1_64
);
2169 /* Single-precision floating-point vector comparisons */
2171 DO_SPE_CMP(fscmplt
);
2173 DO_SPE_CMP(fscmpgt
);
2175 DO_SPE_CMP(fscmpeq
);
2177 DO_SPE_CMP(fststlt
);
2179 DO_SPE_CMP(fststgt
);
2181 DO_SPE_CMP(fststeq
);
2183 /* Single-precision floating-point vector conversions */
2197 DO_SPE_OP1(fsctsiz
);
2199 DO_SPE_OP1(fsctuiz
);
2204 #endif /* defined(TARGET_PPCEMB) */
2206 /*****************************************************************************/
2207 /* Softmmu support */
2208 #if !defined (CONFIG_USER_ONLY)
2210 #define MMUSUFFIX _mmu
2211 #define GETPC() (__builtin_return_address(0))
2214 #include "softmmu_template.h"
2217 #include "softmmu_template.h"
2220 #include "softmmu_template.h"
2223 #include "softmmu_template.h"
2225 /* try to fill the TLB and return an exception if error. If retaddr is
2226 NULL, it means that the function was called in C code (i.e. not
2227 from generated code or from helper.c) */
2228 /* XXX: fix it to restore all registers */
2229 void tlb_fill (target_ulong addr
, int is_write
, int is_user
, void *retaddr
)
2231 TranslationBlock
*tb
;
2232 CPUState
*saved_env
;
2233 target_phys_addr_t pc
;
2236 /* XXX: hack to restore env in all cases, even if not called from
2239 env
= cpu_single_env
;
2240 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, is_user
, 1);
2241 if (unlikely(ret
!= 0)) {
2242 if (likely(retaddr
)) {
2243 /* now we have a real cpu fault */
2244 pc
= (target_phys_addr_t
)retaddr
;
2245 tb
= tb_find_pc(pc
);
2247 /* the PC is inside the translated code. It means that we have
2248 a virtual CPU fault */
2249 cpu_restore_state(tb
, env
, pc
, NULL
);
2252 do_raise_exception_err(env
->exception_index
, env
->error_code
);
2257 /* TLB invalidation helpers */
2258 void do_tlbia (void)
2260 ppc_tlb_invalidate_all(env
);
2263 void do_tlbie (void)
2266 #if !defined(FLUSH_ALL_TLBS)
2267 if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_6xx
)) {
2268 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 0);
2269 if (env
->id_tlbs
== 1)
2270 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 1);
2271 } else if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_4xx
)) {
2274 ppcbooke_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
,
2275 env
->spr
[SPR_BOOKE_PID
]);
2278 /* tlbie invalidate TLBs for all segments */
2279 T0
&= TARGET_PAGE_MASK
;
2280 T0
&= ~((target_ulong
)-1 << 28);
2281 /* XXX: this case should be optimized,
2282 * giving a mask to tlb_flush_page
2284 tlb_flush_page(env
, T0
| (0x0 << 28));
2285 tlb_flush_page(env
, T0
| (0x1 << 28));
2286 tlb_flush_page(env
, T0
| (0x2 << 28));
2287 tlb_flush_page(env
, T0
| (0x3 << 28));
2288 tlb_flush_page(env
, T0
| (0x4 << 28));
2289 tlb_flush_page(env
, T0
| (0x5 << 28));
2290 tlb_flush_page(env
, T0
| (0x6 << 28));
2291 tlb_flush_page(env
, T0
| (0x7 << 28));
2292 tlb_flush_page(env
, T0
| (0x8 << 28));
2293 tlb_flush_page(env
, T0
| (0x9 << 28));
2294 tlb_flush_page(env
, T0
| (0xA << 28));
2295 tlb_flush_page(env
, T0
| (0xB << 28));
2296 tlb_flush_page(env
, T0
| (0xC << 28));
2297 tlb_flush_page(env
, T0
| (0xD << 28));
2298 tlb_flush_page(env
, T0
| (0xE << 28));
2299 tlb_flush_page(env
, T0
| (0xF << 28));
2306 #if defined(TARGET_PPC64)
2307 void do_tlbie_64 (void)
2310 #if !defined(FLUSH_ALL_TLBS)
2311 if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_6xx
)) {
2312 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 0);
2313 if (env
->id_tlbs
== 1)
2314 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 1);
2315 } else if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_4xx
)) {
2318 ppcbooke_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
,
2319 env
->spr
[SPR_BOOKE_PID
]);
2322 /* tlbie invalidate TLBs for all segments
2323 * As we have 2^36 segments, invalidate all qemu TLBs
2326 T0
&= TARGET_PAGE_MASK
;
2327 T0
&= ~((target_ulong
)-1 << 28);
2328 /* XXX: this case should be optimized,
2329 * giving a mask to tlb_flush_page
2331 tlb_flush_page(env
, T0
| (0x0 << 28));
2332 tlb_flush_page(env
, T0
| (0x1 << 28));
2333 tlb_flush_page(env
, T0
| (0x2 << 28));
2334 tlb_flush_page(env
, T0
| (0x3 << 28));
2335 tlb_flush_page(env
, T0
| (0x4 << 28));
2336 tlb_flush_page(env
, T0
| (0x5 << 28));
2337 tlb_flush_page(env
, T0
| (0x6 << 28));
2338 tlb_flush_page(env
, T0
| (0x7 << 28));
2339 tlb_flush_page(env
, T0
| (0x8 << 28));
2340 tlb_flush_page(env
, T0
| (0x9 << 28));
2341 tlb_flush_page(env
, T0
| (0xA << 28));
2342 tlb_flush_page(env
, T0
| (0xB << 28));
2343 tlb_flush_page(env
, T0
| (0xC << 28));
2344 tlb_flush_page(env
, T0
| (0xD << 28));
2345 tlb_flush_page(env
, T0
| (0xE << 28));
2346 tlb_flush_page(env
, T0
| (0xF << 28));
2357 #if defined(TARGET_PPC64)
2358 void do_slbia (void)
2364 void do_slbie (void)
2371 /* Software driven TLBs management */
2372 /* PowerPC 602/603 software TLB load instructions helpers */
2373 void do_load_6xx_tlb (int is_code
)
2375 target_ulong RPN
, CMP
, EPN
;
2378 RPN
= env
->spr
[SPR_RPA
];
2380 CMP
= env
->spr
[SPR_ICMP
];
2381 EPN
= env
->spr
[SPR_IMISS
];
2383 CMP
= env
->spr
[SPR_DCMP
];
2384 EPN
= env
->spr
[SPR_DMISS
];
2386 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2387 #if defined (DEBUG_SOFTWARE_TLB)
2388 if (loglevel
!= 0) {
2389 fprintf(logfile
, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2390 __func__
, (unsigned long)T0
, (unsigned long)EPN
,
2391 (unsigned long)CMP
, (unsigned long)RPN
, way
);
2394 /* Store this TLB */
2395 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2396 way
, is_code
, CMP
, RPN
);
2399 static target_ulong
booke_tlb_to_page_size (int size
)
2401 return 1024 << (2 * size
);
2404 static int booke_page_size_to_tlb (target_ulong page_size
)
2408 switch (page_size
) {
2442 #if defined (TARGET_PPC64)
2443 case 0x000100000000ULL
:
2446 case 0x000400000000ULL
:
2449 case 0x001000000000ULL
:
2452 case 0x004000000000ULL
:
2455 case 0x010000000000ULL
:
2467 /* Helpers for 4xx TLB management */
2468 void do_4xx_tlbre_lo (void)
2474 tlb
= &env
->tlb
[T0
].tlbe
;
2476 if (tlb
->prot
& PAGE_VALID
)
2478 size
= booke_page_size_to_tlb(tlb
->size
);
2479 if (size
< 0 || size
> 0x7)
2482 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2485 void do_4xx_tlbre_hi (void)
2490 tlb
= &env
->tlb
[T0
].tlbe
;
2492 if (tlb
->prot
& PAGE_EXEC
)
2494 if (tlb
->prot
& PAGE_WRITE
)
2498 void do_4xx_tlbsx (void)
2500 T0
= ppcemb_tlb_search(env
, T0
);
2503 void do_4xx_tlbsx_ (void)
2507 T0
= ppcemb_tlb_search(env
, T0
);
2513 void do_4xx_tlbwe_hi (void)
2516 target_ulong page
, end
;
2518 #if defined (DEBUG_SOFTWARE_TLB)
2519 if (loglevel
!= 0) {
2520 fprintf(logfile
, "%s T0 " REGX
" T1 " REGX
"\n", __func__
, T0
, T1
);
2524 tlb
= &env
->tlb
[T0
].tlbe
;
2525 /* Invalidate previous TLB (if it's valid) */
2526 if (tlb
->prot
& PAGE_VALID
) {
2527 end
= tlb
->EPN
+ tlb
->size
;
2528 #if defined (DEBUG_SOFTWARE_TLB)
2529 if (loglevel
!= 0) {
2530 fprintf(logfile
, "%s: invalidate old TLB %d start " ADDRX
2531 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2534 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2535 tlb_flush_page(env
, page
);
2537 tlb
->size
= booke_tlb_to_page_size((T1
>> 7) & 0x7);
2538 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2539 * If this ever occurs, one should use the ppcemb target instead
2540 * of the ppc or ppc64 one
2542 if ((T1
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
2543 cpu_abort(env
, "TLB size %u < %u are not supported (%d)\n",
2544 tlb
->size
, TARGET_PAGE_SIZE
, (int)((T1
>> 7) & 0x7));
2546 tlb
->EPN
= (T1
& 0xFFFFFC00) & ~(tlb
->size
- 1);
2548 tlb
->prot
|= PAGE_VALID
;
2550 tlb
->prot
&= ~PAGE_VALID
;
2552 /* XXX: TO BE FIXED */
2553 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
2555 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2556 tlb
->attr
= T1
& 0xFF;
2557 #if defined (DEBUG_SOFTWARE_TLB)
2558 if (loglevel
!= 0) {
2559 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2560 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2561 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2562 tlb
->prot
& PAGE_READ
? 'r' : '-',
2563 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2564 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2565 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2568 /* Invalidate new TLB (if valid) */
2569 if (tlb
->prot
& PAGE_VALID
) {
2570 end
= tlb
->EPN
+ tlb
->size
;
2571 #if defined (DEBUG_SOFTWARE_TLB)
2572 if (loglevel
!= 0) {
2573 fprintf(logfile
, "%s: invalidate TLB %d start " ADDRX
2574 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2577 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2578 tlb_flush_page(env
, page
);
2582 void do_4xx_tlbwe_lo (void)
2586 #if defined (DEBUG_SOFTWARE_TLB)
2587 if (loglevel
!= 0) {
2588 fprintf(logfile
, "%s T0 " REGX
" T1 " REGX
"\n", __func__
, T0
, T1
);
2592 tlb
= &env
->tlb
[T0
].tlbe
;
2593 tlb
->RPN
= T1
& 0xFFFFFC00;
2594 tlb
->prot
= PAGE_READ
;
2596 tlb
->prot
|= PAGE_EXEC
;
2598 tlb
->prot
|= PAGE_WRITE
;
2599 #if defined (DEBUG_SOFTWARE_TLB)
2600 if (loglevel
!= 0) {
2601 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2602 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2603 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2604 tlb
->prot
& PAGE_READ
? 'r' : '-',
2605 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2606 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2607 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2611 #endif /* !CONFIG_USER_ONLY */