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 env
->nip
= (target_ulong
)(env
->spr
[SPR_SRR0
] & ~0x00000003);
885 T0
= (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
);
886 #if defined(TARGET_PPC64)
887 ppc_store_msr_32(env
, T0
);
889 do_store_msr(env
, T0
);
891 #if defined (DEBUG_OP)
894 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
897 #if defined(TARGET_PPC64)
898 void do_rfi_32 (void)
900 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
901 T0
= (uint32_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
);
902 ppc_store_msr_32(env
, T0
);
903 #if defined (DEBUG_OP)
906 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
911 env
->nip
= (target_ulong
)(env
->spr
[SPR_SRR0
] & ~0x00000003);
912 T0
= (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
);
913 do_store_msr(env
, T0
);
914 #if defined (DEBUG_OP)
917 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
920 void do_rfid_32 (void)
922 env
->nip
= (uint32_t)(env
->spr
[SPR_SRR0
] & ~0x00000003);
923 T0
= (uint64_t)(env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
);
924 do_store_msr(env
, T0
);
925 #if defined (DEBUG_OP)
928 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
933 void do_tw (int flags
)
935 if (!likely(!(((int32_t)T0
< (int32_t)T1
&& (flags
& 0x10)) ||
936 ((int32_t)T0
> (int32_t)T1
&& (flags
& 0x08)) ||
937 ((int32_t)T0
== (int32_t)T1
&& (flags
& 0x04)) ||
938 ((uint32_t)T0
< (uint32_t)T1
&& (flags
& 0x02)) ||
939 ((uint32_t)T0
> (uint32_t)T1
&& (flags
& 0x01)))))
940 do_raise_exception_err(EXCP_PROGRAM
, EXCP_TRAP
);
943 #if defined(TARGET_PPC64)
944 void do_td (int flags
)
946 if (!likely(!(((int64_t)T0
< (int64_t)T1
&& (flags
& 0x10)) ||
947 ((int64_t)T0
> (int64_t)T1
&& (flags
& 0x08)) ||
948 ((int64_t)T0
== (int64_t)T1
&& (flags
& 0x04)) ||
949 ((uint64_t)T0
< (uint64_t)T1
&& (flags
& 0x02)) ||
950 ((uint64_t)T0
> (uint64_t)T1
&& (flags
& 0x01)))))
951 do_raise_exception_err(EXCP_PROGRAM
, EXCP_TRAP
);
955 /*****************************************************************************/
956 /* PowerPC 601 specific instructions (POWER bridge) */
957 void do_POWER_abso (void)
959 if ((uint32_t)T0
== INT32_MIN
) {
969 void do_POWER_clcs (void)
973 /* Instruction cache line size */
974 T0
= ICACHE_LINE_SIZE
;
977 /* Data cache line size */
978 T0
= DCACHE_LINE_SIZE
;
981 /* Minimum cache line size */
982 T0
= ICACHE_LINE_SIZE
< DCACHE_LINE_SIZE
?
983 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
986 /* Maximum cache line size */
987 T0
= ICACHE_LINE_SIZE
> DCACHE_LINE_SIZE
?
988 ICACHE_LINE_SIZE
: DCACHE_LINE_SIZE
;
996 void do_POWER_div (void)
1000 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1001 T0
= (long)((-1) * (T0
>> 31));
1002 env
->spr
[SPR_MQ
] = 0;
1004 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1005 env
->spr
[SPR_MQ
] = tmp
% T1
;
1006 T0
= tmp
/ (int32_t)T1
;
1010 void do_POWER_divo (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;
1020 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1021 env
->spr
[SPR_MQ
] = tmp
% T1
;
1023 if (tmp
> (int64_t)INT32_MAX
|| tmp
< (int64_t)INT32_MIN
) {
1033 void do_POWER_divs (void)
1035 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1036 T0
= (long)((-1) * (T0
>> 31));
1037 env
->spr
[SPR_MQ
] = 0;
1039 env
->spr
[SPR_MQ
] = T0
% T1
;
1040 T0
= (int32_t)T0
/ (int32_t)T1
;
1044 void do_POWER_divso (void)
1046 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== -1) || (int32_t)T1
== 0) {
1047 T0
= (long)((-1) * (T0
>> 31));
1048 env
->spr
[SPR_MQ
] = 0;
1052 T0
= (int32_t)T0
/ (int32_t)T1
;
1053 env
->spr
[SPR_MQ
] = (int32_t)T0
% (int32_t)T1
;
1058 void do_POWER_dozo (void)
1060 if ((int32_t)T1
> (int32_t)T0
) {
1063 if (((uint32_t)(~T2
) ^ (uint32_t)T1
^ UINT32_MAX
) &
1064 ((uint32_t)(~T2
) ^ (uint32_t)T0
) & (1UL << 31)) {
1076 void do_POWER_maskg (void)
1080 if ((uint32_t)T0
== (uint32_t)(T1
+ 1)) {
1083 ret
= (((uint32_t)(-1)) >> ((uint32_t)T0
)) ^
1084 (((uint32_t)(-1) >> ((uint32_t)T1
)) >> 1);
1085 if ((uint32_t)T0
> (uint32_t)T1
)
1091 void do_POWER_mulo (void)
1095 tmp
= (uint64_t)T0
* (uint64_t)T1
;
1096 env
->spr
[SPR_MQ
] = tmp
>> 32;
1098 if (tmp
>> 32 != ((uint64_t)T0
>> 16) * ((uint64_t)T1
>> 16)) {
1106 #if !defined (CONFIG_USER_ONLY)
1107 void do_POWER_rac (void)
1112 /* We don't have to generate many instances of this instruction,
1113 * as rac is supervisor only.
1115 if (get_physical_address(env
, &ctx
, T0
, 0, ACCESS_INT
, 1) == 0)
1120 void do_POWER_rfsvc (void)
1122 env
->nip
= env
->lr
& ~0x00000003UL
;
1123 T0
= env
->ctr
& 0x0000FFFFUL
;
1124 do_store_msr(env
, T0
);
1125 #if defined (DEBUG_OP)
1128 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1131 /* PowerPC 601 BAT management helper */
1132 void do_store_601_batu (int nr
)
1134 do_store_ibatu(env
, nr
, (uint32_t)T0
);
1135 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1136 env
->DBAT
[1][nr
] = env
->IBAT
[1][nr
];
1140 /*****************************************************************************/
1141 /* 602 specific instructions */
1142 /* mfrom is the most crazy instruction ever seen, imho ! */
1143 /* Real implementation uses a ROM table. Do the same */
1144 #define USE_MFROM_ROM_TABLE
1145 void do_op_602_mfrom (void)
1147 if (likely(T0
< 602)) {
1148 #if defined(USE_MFROM_ROM_TABLE)
1149 #include "mfrom_table.c"
1150 T0
= mfrom_ROM_table
[T0
];
1153 /* Extremly decomposed:
1155 * T0 = 256 * log10(10 + 1.0) + 0.5
1158 d
= float64_div(d
, 256, &env
->fp_status
);
1160 d
= exp10(d
); // XXX: use float emulation function
1161 d
= float64_add(d
, 1.0, &env
->fp_status
);
1162 d
= log10(d
); // XXX: use float emulation function
1163 d
= float64_mul(d
, 256, &env
->fp_status
);
1164 d
= float64_add(d
, 0.5, &env
->fp_status
);
1165 T0
= float64_round_to_int(d
, &env
->fp_status
);
1172 /*****************************************************************************/
1173 /* Embedded PowerPC specific helpers */
1174 void do_405_check_ov (void)
1176 if (likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1177 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1185 void do_405_check_sat (void)
1187 if (!likely((((uint32_t)T1
^ (uint32_t)T2
) >> 31) ||
1188 !(((uint32_t)T0
^ (uint32_t)T2
) >> 31))) {
1189 /* Saturate result */
1198 #if !defined(CONFIG_USER_ONLY)
1199 void do_4xx_rfci (void)
1201 env
->nip
= env
->spr
[SPR_40x_SRR2
];
1202 T0
= env
->spr
[SPR_40x_SRR3
] & ~0xFFFF0000;
1203 do_store_msr(env
, T0
);
1204 #if defined (DEBUG_OP)
1207 env
->interrupt_request
= CPU_INTERRUPT_EXITTB
;
1210 void do_4xx_load_dcr (int dcrn
)
1214 if (unlikely(env
->dcr_read
== NULL
))
1215 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_INVAL_INVAL
);
1216 else if (unlikely((*env
->dcr_read
)(env
->dcr_env
, dcrn
, &val
) != 0))
1217 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_PRIV_REG
);
1222 void do_4xx_store_dcr (int dcrn
)
1224 if (unlikely(env
->dcr_write
== NULL
))
1225 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_INVAL_INVAL
);
1226 else if (unlikely((*env
->dcr_write
)(env
->dcr_env
, dcrn
, T0
) != 0))
1227 do_raise_exception_err(EXCP_PROGRAM
, EXCP_INVAL
| EXCP_PRIV_REG
);
1230 void do_load_403_pb (int num
)
1235 void do_store_403_pb (int num
)
1237 if (likely(env
->pb
[num
] != T0
)) {
1239 /* Should be optimized */
1246 void do_440_dlmzb (void)
1252 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1253 if ((T0
& mask
) == 0)
1257 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1258 if ((T1
& mask
) == 0)
1266 #if defined(TARGET_PPCSPE)
1267 /* SPE extension helpers */
1268 /* Use a table to make this quicker */
1269 static uint8_t hbrev
[16] = {
1270 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1271 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1274 static inline uint8_t byte_reverse (uint8_t val
)
1276 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1279 static inline uint32_t word_reverse (uint32_t val
)
1281 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1282 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1285 #define MASKBITS 16 // Random value - to be fixed
1286 void do_brinc (void)
1288 uint32_t a
, b
, d
, mask
;
1290 mask
= (uint32_t)(-1UL) >> MASKBITS
;
1293 d
= word_reverse(1 + word_reverse(a
| ~mask
));
1294 T0_64
= (T0_64
& ~mask
) | (d
& mask
);
1297 #define DO_SPE_OP2(name) \
1298 void do_ev##name (void) \
1300 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) | \
1301 (uint64_t)_do_e##name(T0_64, T1_64); \
1304 #define DO_SPE_OP1(name) \
1305 void do_ev##name (void) \
1307 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) | \
1308 (uint64_t)_do_e##name(T0_64); \
1311 /* Fixed-point vector arithmetic */
1312 static inline uint32_t _do_eabs (uint32_t val
)
1314 if (val
!= 0x80000000)
1320 static inline uint32_t _do_eaddw (uint32_t op1
, uint32_t op2
)
1325 static inline int _do_ecntlsw (uint32_t val
)
1327 if (val
& 0x80000000)
1328 return _do_cntlzw(~val
);
1330 return _do_cntlzw(val
);
1333 static inline int _do_ecntlzw (uint32_t val
)
1335 return _do_cntlzw(val
);
1338 static inline uint32_t _do_eneg (uint32_t val
)
1340 if (val
!= 0x80000000)
1346 static inline uint32_t _do_erlw (uint32_t op1
, uint32_t op2
)
1348 return rotl32(op1
, op2
);
1351 static inline uint32_t _do_erndw (uint32_t val
)
1353 return (val
+ 0x000080000000) & 0xFFFF0000;
1356 static inline uint32_t _do_eslw (uint32_t op1
, uint32_t op2
)
1358 /* No error here: 6 bits are used */
1359 return op1
<< (op2
& 0x3F);
1362 static inline int32_t _do_esrws (int32_t op1
, uint32_t op2
)
1364 /* No error here: 6 bits are used */
1365 return op1
>> (op2
& 0x3F);
1368 static inline uint32_t _do_esrwu (uint32_t op1
, uint32_t op2
)
1370 /* No error here: 6 bits are used */
1371 return op1
>> (op2
& 0x3F);
1374 static inline uint32_t _do_esubfw (uint32_t op1
, uint32_t op2
)
1402 /* evsel is a little bit more complicated... */
1403 static inline uint32_t _do_esel (uint32_t op1
, uint32_t op2
, int n
)
1411 void do_evsel (void)
1413 T0_64
= ((uint64_t)_do_esel(T0_64
>> 32, T1_64
>> 32, T0
>> 3) << 32) |
1414 (uint64_t)_do_esel(T0_64
, T1_64
, (T0
>> 2) & 1);
1417 /* Fixed-point vector comparisons */
1418 #define DO_SPE_CMP(name) \
1419 void do_ev##name (void) \
1421 T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32, \
1422 T1_64 >> 32) << 32, \
1423 _do_e##name(T0_64, T1_64)); \
1426 static inline uint32_t _do_evcmp_merge (int t0
, int t1
)
1428 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1430 static inline int _do_ecmpeq (uint32_t op1
, uint32_t op2
)
1432 return op1
== op2
? 1 : 0;
1435 static inline int _do_ecmpgts (int32_t op1
, int32_t op2
)
1437 return op1
> op2
? 1 : 0;
1440 static inline int _do_ecmpgtu (uint32_t op1
, uint32_t op2
)
1442 return op1
> op2
? 1 : 0;
1445 static inline int _do_ecmplts (int32_t op1
, int32_t op2
)
1447 return op1
< op2
? 1 : 0;
1450 static inline int _do_ecmpltu (uint32_t op1
, uint32_t op2
)
1452 return op1
< op2
? 1 : 0;
1466 /* Single precision floating-point conversions from/to integer */
1467 static inline uint32_t _do_efscfsi (int32_t val
)
1474 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1479 static inline uint32_t _do_efscfui (uint32_t val
)
1486 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1491 static inline int32_t _do_efsctsi (uint32_t val
)
1499 /* NaN are not treated the same way IEEE 754 does */
1500 if (unlikely(isnan(u
.f
)))
1503 return float32_to_int32(u
.f
, &env
->spe_status
);
1506 static inline uint32_t _do_efsctui (uint32_t val
)
1514 /* NaN are not treated the same way IEEE 754 does */
1515 if (unlikely(isnan(u
.f
)))
1518 return float32_to_uint32(u
.f
, &env
->spe_status
);
1521 static inline int32_t _do_efsctsiz (uint32_t val
)
1529 /* NaN are not treated the same way IEEE 754 does */
1530 if (unlikely(isnan(u
.f
)))
1533 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1536 static inline uint32_t _do_efsctuiz (uint32_t val
)
1544 /* NaN are not treated the same way IEEE 754 does */
1545 if (unlikely(isnan(u
.f
)))
1548 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1551 void do_efscfsi (void)
1553 T0_64
= _do_efscfsi(T0_64
);
1556 void do_efscfui (void)
1558 T0_64
= _do_efscfui(T0_64
);
1561 void do_efsctsi (void)
1563 T0_64
= _do_efsctsi(T0_64
);
1566 void do_efsctui (void)
1568 T0_64
= _do_efsctui(T0_64
);
1571 void do_efsctsiz (void)
1573 T0_64
= _do_efsctsiz(T0_64
);
1576 void do_efsctuiz (void)
1578 T0_64
= _do_efsctuiz(T0_64
);
1581 /* Single precision floating-point conversion to/from fractional */
1582 static inline uint32_t _do_efscfsf (uint32_t val
)
1590 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1591 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
1592 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1597 static inline uint32_t _do_efscfuf (uint32_t val
)
1605 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1606 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1607 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1612 static inline int32_t _do_efsctsf (uint32_t val
)
1621 /* NaN are not treated the same way IEEE 754 does */
1622 if (unlikely(isnan(u
.f
)))
1624 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1625 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1627 return float32_to_int32(u
.f
, &env
->spe_status
);
1630 static inline uint32_t _do_efsctuf (uint32_t val
)
1639 /* NaN are not treated the same way IEEE 754 does */
1640 if (unlikely(isnan(u
.f
)))
1642 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1643 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1645 return float32_to_uint32(u
.f
, &env
->spe_status
);
1648 static inline int32_t _do_efsctsfz (uint32_t val
)
1657 /* NaN are not treated the same way IEEE 754 does */
1658 if (unlikely(isnan(u
.f
)))
1660 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1661 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1663 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1666 static inline uint32_t _do_efsctufz (uint32_t val
)
1675 /* NaN are not treated the same way IEEE 754 does */
1676 if (unlikely(isnan(u
.f
)))
1678 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1679 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1681 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1684 void do_efscfsf (void)
1686 T0_64
= _do_efscfsf(T0_64
);
1689 void do_efscfuf (void)
1691 T0_64
= _do_efscfuf(T0_64
);
1694 void do_efsctsf (void)
1696 T0_64
= _do_efsctsf(T0_64
);
1699 void do_efsctuf (void)
1701 T0_64
= _do_efsctuf(T0_64
);
1704 void do_efsctsfz (void)
1706 T0_64
= _do_efsctsfz(T0_64
);
1709 void do_efsctufz (void)
1711 T0_64
= _do_efsctufz(T0_64
);
1714 /* Double precision floating point helpers */
1715 static inline int _do_efdcmplt (uint64_t op1
, uint64_t op2
)
1717 /* XXX: TODO: test special values (NaN, infinites, ...) */
1718 return _do_efdtstlt(op1
, op2
);
1721 static inline int _do_efdcmpgt (uint64_t op1
, uint64_t op2
)
1723 /* XXX: TODO: test special values (NaN, infinites, ...) */
1724 return _do_efdtstgt(op1
, op2
);
1727 static inline int _do_efdcmpeq (uint64_t op1
, uint64_t op2
)
1729 /* XXX: TODO: test special values (NaN, infinites, ...) */
1730 return _do_efdtsteq(op1
, op2
);
1733 void do_efdcmplt (void)
1735 T0
= _do_efdcmplt(T0_64
, T1_64
);
1738 void do_efdcmpgt (void)
1740 T0
= _do_efdcmpgt(T0_64
, T1_64
);
1743 void do_efdcmpeq (void)
1745 T0
= _do_efdcmpeq(T0_64
, T1_64
);
1748 /* Double precision floating-point conversion to/from integer */
1749 static inline uint64_t _do_efdcfsi (int64_t val
)
1756 u
.f
= int64_to_float64(val
, &env
->spe_status
);
1761 static inline uint64_t _do_efdcfui (uint64_t val
)
1768 u
.f
= uint64_to_float64(val
, &env
->spe_status
);
1773 static inline int64_t _do_efdctsi (uint64_t val
)
1781 /* NaN are not treated the same way IEEE 754 does */
1782 if (unlikely(isnan(u
.f
)))
1785 return float64_to_int64(u
.f
, &env
->spe_status
);
1788 static inline uint64_t _do_efdctui (uint64_t val
)
1796 /* NaN are not treated the same way IEEE 754 does */
1797 if (unlikely(isnan(u
.f
)))
1800 return float64_to_uint64(u
.f
, &env
->spe_status
);
1803 static inline int64_t _do_efdctsiz (uint64_t val
)
1811 /* NaN are not treated the same way IEEE 754 does */
1812 if (unlikely(isnan(u
.f
)))
1815 return float64_to_int64_round_to_zero(u
.f
, &env
->spe_status
);
1818 static inline uint64_t _do_efdctuiz (uint64_t val
)
1826 /* NaN are not treated the same way IEEE 754 does */
1827 if (unlikely(isnan(u
.f
)))
1830 return float64_to_uint64_round_to_zero(u
.f
, &env
->spe_status
);
1833 void do_efdcfsi (void)
1835 T0_64
= _do_efdcfsi(T0_64
);
1838 void do_efdcfui (void)
1840 T0_64
= _do_efdcfui(T0_64
);
1843 void do_efdctsi (void)
1845 T0_64
= _do_efdctsi(T0_64
);
1848 void do_efdctui (void)
1850 T0_64
= _do_efdctui(T0_64
);
1853 void do_efdctsiz (void)
1855 T0_64
= _do_efdctsiz(T0_64
);
1858 void do_efdctuiz (void)
1860 T0_64
= _do_efdctuiz(T0_64
);
1863 /* Double precision floating-point conversion to/from fractional */
1864 static inline uint64_t _do_efdcfsf (int64_t val
)
1872 u
.f
= int32_to_float64(val
, &env
->spe_status
);
1873 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
1874 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
1879 static inline uint64_t _do_efdcfuf (uint64_t val
)
1887 u
.f
= uint32_to_float64(val
, &env
->spe_status
);
1888 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
1889 u
.f
= float64_div(u
.f
, tmp
, &env
->spe_status
);
1894 static inline int64_t _do_efdctsf (uint64_t val
)
1903 /* NaN are not treated the same way IEEE 754 does */
1904 if (unlikely(isnan(u
.f
)))
1906 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1907 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1909 return float64_to_int32(u
.f
, &env
->spe_status
);
1912 static inline uint64_t _do_efdctuf (uint64_t val
)
1921 /* NaN are not treated the same way IEEE 754 does */
1922 if (unlikely(isnan(u
.f
)))
1924 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1925 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1927 return float64_to_uint32(u
.f
, &env
->spe_status
);
1930 static inline int64_t _do_efdctsfz (uint64_t val
)
1939 /* NaN are not treated the same way IEEE 754 does */
1940 if (unlikely(isnan(u
.f
)))
1942 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1943 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1945 return float64_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1948 static inline uint64_t _do_efdctufz (uint64_t val
)
1957 /* NaN are not treated the same way IEEE 754 does */
1958 if (unlikely(isnan(u
.f
)))
1960 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
1961 u
.f
= float64_mul(u
.f
, tmp
, &env
->spe_status
);
1963 return float64_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1966 void do_efdcfsf (void)
1968 T0_64
= _do_efdcfsf(T0_64
);
1971 void do_efdcfuf (void)
1973 T0_64
= _do_efdcfuf(T0_64
);
1976 void do_efdctsf (void)
1978 T0_64
= _do_efdctsf(T0_64
);
1981 void do_efdctuf (void)
1983 T0_64
= _do_efdctuf(T0_64
);
1986 void do_efdctsfz (void)
1988 T0_64
= _do_efdctsfz(T0_64
);
1991 void do_efdctufz (void)
1993 T0_64
= _do_efdctufz(T0_64
);
1996 /* Floating point conversion between single and double precision */
1997 static inline uint32_t _do_efscfd (uint64_t val
)
2009 u2
.f
= float64_to_float32(u1
.f
, &env
->spe_status
);
2014 static inline uint64_t _do_efdcfs (uint32_t val
)
2026 u2
.f
= float32_to_float64(u1
.f
, &env
->spe_status
);
2031 void do_efscfd (void)
2033 T0_64
= _do_efscfd(T0_64
);
2036 void do_efdcfs (void)
2038 T0_64
= _do_efdcfs(T0_64
);
2041 /* Single precision fixed-point vector arithmetic */
2057 /* Single-precision floating-point comparisons */
2058 static inline int _do_efscmplt (uint32_t op1
, uint32_t op2
)
2060 /* XXX: TODO: test special values (NaN, infinites, ...) */
2061 return _do_efststlt(op1
, op2
);
2064 static inline int _do_efscmpgt (uint32_t op1
, uint32_t op2
)
2066 /* XXX: TODO: test special values (NaN, infinites, ...) */
2067 return _do_efststgt(op1
, op2
);
2070 static inline int _do_efscmpeq (uint32_t op1
, uint32_t op2
)
2072 /* XXX: TODO: test special values (NaN, infinites, ...) */
2073 return _do_efststeq(op1
, op2
);
2076 void do_efscmplt (void)
2078 T0
= _do_efscmplt(T0_64
, T1_64
);
2081 void do_efscmpgt (void)
2083 T0
= _do_efscmpgt(T0_64
, T1_64
);
2086 void do_efscmpeq (void)
2088 T0
= _do_efscmpeq(T0_64
, T1_64
);
2091 /* Single-precision floating-point vector comparisons */
2093 DO_SPE_CMP(fscmplt
);
2095 DO_SPE_CMP(fscmpgt
);
2097 DO_SPE_CMP(fscmpeq
);
2099 DO_SPE_CMP(fststlt
);
2101 DO_SPE_CMP(fststgt
);
2103 DO_SPE_CMP(fststeq
);
2105 /* Single-precision floating-point vector conversions */
2119 DO_SPE_OP1(fsctsiz
);
2121 DO_SPE_OP1(fsctuiz
);
2126 #endif /* defined(TARGET_PPCSPE) */
2128 /*****************************************************************************/
2129 /* Softmmu support */
2130 #if !defined (CONFIG_USER_ONLY)
2132 #define MMUSUFFIX _mmu
2133 #define GETPC() (__builtin_return_address(0))
2136 #include "softmmu_template.h"
2139 #include "softmmu_template.h"
2142 #include "softmmu_template.h"
2145 #include "softmmu_template.h"
2147 /* try to fill the TLB and return an exception if error. If retaddr is
2148 NULL, it means that the function was called in C code (i.e. not
2149 from generated code or from helper.c) */
2150 /* XXX: fix it to restore all registers */
2151 void tlb_fill (target_ulong addr
, int is_write
, int is_user
, void *retaddr
)
2153 TranslationBlock
*tb
;
2154 CPUState
*saved_env
;
2155 target_phys_addr_t pc
;
2158 /* XXX: hack to restore env in all cases, even if not called from
2161 env
= cpu_single_env
;
2162 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, is_user
, 1);
2163 if (unlikely(ret
!= 0)) {
2164 if (likely(retaddr
)) {
2165 /* now we have a real cpu fault */
2166 pc
= (target_phys_addr_t
)retaddr
;
2167 tb
= tb_find_pc(pc
);
2169 /* the PC is inside the translated code. It means that we have
2170 a virtual CPU fault */
2171 cpu_restore_state(tb
, env
, pc
, NULL
);
2174 do_raise_exception_err(env
->exception_index
, env
->error_code
);
2179 /* TLB invalidation helpers */
2180 void do_tlbia (void)
2182 if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_6xx
)) {
2183 ppc6xx_tlb_invalidate_all(env
);
2184 } else if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_4xx
)) {
2187 ppcbooke_tlb_invalidate_all(env
);
2194 void do_tlbie (void)
2197 #if !defined(FLUSH_ALL_TLBS)
2198 if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_6xx
)) {
2199 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 0);
2200 if (env
->id_tlbs
== 1)
2201 ppc6xx_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
, 1);
2202 } else if (unlikely(PPC_MMU(env
) == PPC_FLAGS_MMU_SOFT_4xx
)) {
2205 ppcbooke_tlb_invalidate_virt(env
, T0
& TARGET_PAGE_MASK
,
2206 env
->spr
[SPR_BOOKE_PID
]);
2209 /* tlbie invalidate TLBs for all segments */
2210 T0
&= TARGET_PAGE_MASK
;
2211 T0
&= ~((target_ulong
)-1 << 28);
2212 /* XXX: this case should be optimized,
2213 * giving a mask to tlb_flush_page
2215 tlb_flush_page(env
, T0
| (0x0 << 28));
2216 tlb_flush_page(env
, T0
| (0x1 << 28));
2217 tlb_flush_page(env
, T0
| (0x2 << 28));
2218 tlb_flush_page(env
, T0
| (0x3 << 28));
2219 tlb_flush_page(env
, T0
| (0x4 << 28));
2220 tlb_flush_page(env
, T0
| (0x5 << 28));
2221 tlb_flush_page(env
, T0
| (0x6 << 28));
2222 tlb_flush_page(env
, T0
| (0x7 << 28));
2223 tlb_flush_page(env
, T0
| (0x8 << 28));
2224 tlb_flush_page(env
, T0
| (0x9 << 28));
2225 tlb_flush_page(env
, T0
| (0xA << 28));
2226 tlb_flush_page(env
, T0
| (0xB << 28));
2227 tlb_flush_page(env
, T0
| (0xC << 28));
2228 tlb_flush_page(env
, T0
| (0xD << 28));
2229 tlb_flush_page(env
, T0
| (0xE << 28));
2230 tlb_flush_page(env
, T0
| (0xF << 28));
2237 #if defined(TARGET_PPC64)
2238 void do_tlbie_64 (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 * As we have 2^36 segments, invalidate all qemu TLBs
2257 T0
&= TARGET_PAGE_MASK
;
2258 T0
&= ~((target_ulong
)-1 << 28);
2259 /* XXX: this case should be optimized,
2260 * giving a mask to tlb_flush_page
2262 tlb_flush_page(env
, T0
| (0x0 << 28));
2263 tlb_flush_page(env
, T0
| (0x1 << 28));
2264 tlb_flush_page(env
, T0
| (0x2 << 28));
2265 tlb_flush_page(env
, T0
| (0x3 << 28));
2266 tlb_flush_page(env
, T0
| (0x4 << 28));
2267 tlb_flush_page(env
, T0
| (0x5 << 28));
2268 tlb_flush_page(env
, T0
| (0x6 << 28));
2269 tlb_flush_page(env
, T0
| (0x7 << 28));
2270 tlb_flush_page(env
, T0
| (0x8 << 28));
2271 tlb_flush_page(env
, T0
| (0x9 << 28));
2272 tlb_flush_page(env
, T0
| (0xA << 28));
2273 tlb_flush_page(env
, T0
| (0xB << 28));
2274 tlb_flush_page(env
, T0
| (0xC << 28));
2275 tlb_flush_page(env
, T0
| (0xD << 28));
2276 tlb_flush_page(env
, T0
| (0xE << 28));
2277 tlb_flush_page(env
, T0
| (0xF << 28));
2288 #if defined(TARGET_PPC64)
2289 void do_slbia (void)
2295 void do_slbie (void)
2302 /* Software driven TLBs management */
2303 /* PowerPC 602/603 software TLB load instructions helpers */
2304 void do_load_6xx_tlb (int is_code
)
2306 target_ulong RPN
, CMP
, EPN
;
2309 RPN
= env
->spr
[SPR_RPA
];
2311 CMP
= env
->spr
[SPR_ICMP
];
2312 EPN
= env
->spr
[SPR_IMISS
];
2314 CMP
= env
->spr
[SPR_DCMP
];
2315 EPN
= env
->spr
[SPR_DMISS
];
2317 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2318 #if defined (DEBUG_SOFTWARE_TLB)
2319 if (loglevel
!= 0) {
2320 fprintf(logfile
, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2321 __func__
, (unsigned long)T0
, (unsigned long)EPN
,
2322 (unsigned long)CMP
, (unsigned long)RPN
, way
);
2325 /* Store this TLB */
2326 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2327 way
, is_code
, CMP
, RPN
);
2330 /* Helpers for 4xx TLB management */
2331 void do_4xx_tlbia (void)
2335 target_ulong page
, end
;
2338 for (i
= 0; i
< 64; i
++) {
2340 if (tlb
->prot
& PAGE_VALID
) {
2341 end
= tlb
->EPN
+ tlb
->size
;
2342 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2343 tlb_flush_page(env
, page
);
2344 tlb
->prot
&= ~PAGE_VALID
;
2350 void do_4xx_tlbre_lo (void)
2356 tlb
= &env
->tlb
[T0
];
2358 env
->spr
[SPR_40x_PID
] = tlb
->pid
;
2362 void do_4xx_tlbre_hi (void)
2368 tlb
= &env
->tlb
[T0
];
2373 static int tlb_4xx_search (target_ulong
virtual)
2377 target_ulong base
, mask
;
2380 /* Default return value is no match */
2382 for (i
= 0; i
< 64; i
++) {
2384 /* Check TLB validity */
2385 if (!(tlb
->prot
& PAGE_VALID
))
2387 /* Check TLB PID vs current PID */
2388 if (tlb
->pid
!= 0 && tlb
->pid
!= env
->spr
[SPR_40x_PID
])
2390 /* Check TLB address vs virtual address */
2392 mask
= ~(tlb
->size
- 1);
2393 if ((base
& mask
) != (virtual & mask
))
2405 void do_4xx_tlbsx (void)
2407 T0
= tlb_4xx_search(T0
);
2410 void do_4xx_tlbsx_ (void)
2414 T0
= tlb_4xx_search(T0
);
2420 void do_4xx_tlbwe_lo (void)
2424 target_ulong page
, end
;
2427 tlb
= &env
->tlb
[T0
];
2428 /* Invalidate previous TLB (if it's valid) */
2429 if (tlb
->prot
& PAGE_VALID
) {
2430 end
= tlb
->EPN
+ tlb
->size
;
2431 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2432 tlb_flush_page(env
, page
);
2434 tlb
->size
= 1024 << (2 * ((T1
>> 7) & 0x7));
2435 tlb
->EPN
= (T1
& 0xFFFFFC00) & ~(tlb
->size
- 1);
2437 tlb
->prot
|= PAGE_VALID
;
2439 tlb
->prot
&= ~PAGE_VALID
;
2440 tlb
->pid
= env
->spr
[SPR_BOOKE_PID
]; /* PID */
2441 /* Invalidate new TLB (if valid) */
2442 if (tlb
->prot
& PAGE_VALID
) {
2443 end
= tlb
->EPN
+ tlb
->size
;
2444 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2445 tlb_flush_page(env
, page
);
2450 void do_4xx_tlbwe_hi (void)
2456 tlb
= &env
->tlb
[T0
];
2457 tlb
->RPN
= T1
& 0xFFFFFC00;
2458 tlb
->prot
= PAGE_READ
;
2460 tlb
->prot
|= PAGE_EXEC
;
2462 tlb
->prot
|= PAGE_WRITE
;
2465 #endif /* !CONFIG_USER_ONLY */