2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "host-utils.h"
24 #include "helper_regs.h"
25 #include "op_helper.h"
27 #define MEMSUFFIX _raw
28 #include "op_helper.h"
29 #include "op_helper_mem.h"
30 #if !defined(CONFIG_USER_ONLY)
31 #define MEMSUFFIX _user
32 #include "op_helper.h"
33 #include "op_helper_mem.h"
34 #define MEMSUFFIX _kernel
35 #include "op_helper.h"
36 #include "op_helper_mem.h"
37 #define MEMSUFFIX _hypv
38 #include "op_helper.h"
39 #include "op_helper_mem.h"
43 //#define DEBUG_EXCEPTIONS
44 //#define DEBUG_SOFTWARE_TLB
46 /*****************************************************************************/
47 /* Exceptions processing helpers */
49 void do_raise_exception_err (uint32_t exception
, int error_code
)
52 printf("Raise exception %3x code : %d\n", exception
, error_code
);
54 env
->exception_index
= exception
;
55 env
->error_code
= error_code
;
59 void do_raise_exception (uint32_t exception
)
61 do_raise_exception_err(exception
, 0);
64 /*****************************************************************************/
65 /* Registers load and stores */
66 target_ulong
helper_load_cr (void)
68 return (env
->crf
[0] << 28) |
78 void helper_store_cr (target_ulong val
, uint32_t mask
)
82 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
84 env
->crf
[i
] = (val
>> (sh
* 4)) & 0xFUL
;
88 #if defined(TARGET_PPC64)
89 void do_store_pri (int prio
)
91 env
->spr
[SPR_PPR
] &= ~0x001C000000000000ULL
;
92 env
->spr
[SPR_PPR
] |= ((uint64_t)prio
& 0x7) << 50;
96 target_ulong
ppc_load_dump_spr (int sprn
)
99 fprintf(logfile
, "Read SPR %d %03x => " ADDRX
"\n",
100 sprn
, sprn
, env
->spr
[sprn
]);
103 return env
->spr
[sprn
];
106 void ppc_store_dump_spr (int sprn
, target_ulong val
)
109 fprintf(logfile
, "Write SPR %d %03x => " ADDRX
" <= " ADDRX
"\n",
110 sprn
, sprn
, env
->spr
[sprn
], val
);
112 env
->spr
[sprn
] = val
;
115 /*****************************************************************************/
116 /* Fixed point operations helpers */
117 #if defined(TARGET_PPC64)
119 /* multiply high word */
120 uint64_t helper_mulhd (uint64_t arg1
, uint64_t arg2
)
124 muls64(&tl
, &th
, arg1
, arg2
);
128 /* multiply high word unsigned */
129 uint64_t helper_mulhdu (uint64_t arg1
, uint64_t arg2
)
133 mulu64(&tl
, &th
, arg1
, arg2
);
137 uint64_t helper_mulldo (uint64_t arg1
, uint64_t arg2
)
142 muls64(&tl
, (uint64_t *)&th
, arg1
, arg2
);
143 /* If th != 0 && th != -1, then we had an overflow */
144 if (likely((uint64_t)(th
+ 1) <= 1)) {
145 env
->xer
&= ~(1 << XER_OV
);
147 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
153 target_ulong
helper_cntlzw (target_ulong t
)
158 #if defined(TARGET_PPC64)
159 target_ulong
helper_cntlzd (target_ulong t
)
165 /* shift right arithmetic helper */
166 target_ulong
helper_sraw (target_ulong value
, target_ulong shift
)
170 if (likely(!(shift
& 0x20))) {
171 if (likely((uint32_t)shift
!= 0)) {
173 ret
= (int32_t)value
>> shift
;
174 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
175 env
->xer
&= ~(1 << XER_CA
);
177 env
->xer
|= (1 << XER_CA
);
180 ret
= (int32_t)value
;
181 env
->xer
&= ~(1 << XER_CA
);
184 ret
= (int32_t)value
>> 31;
186 env
->xer
|= (1 << XER_CA
);
188 env
->xer
&= ~(1 << XER_CA
);
191 return (target_long
)ret
;
194 #if defined(TARGET_PPC64)
195 target_ulong
helper_srad (target_ulong value
, target_ulong shift
)
199 if (likely(!(shift
& 0x40))) {
200 if (likely((uint64_t)shift
!= 0)) {
202 ret
= (int64_t)value
>> shift
;
203 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
204 env
->xer
&= ~(1 << XER_CA
);
206 env
->xer
|= (1 << XER_CA
);
209 ret
= (int64_t)value
;
210 env
->xer
&= ~(1 << XER_CA
);
213 ret
= (int64_t)value
>> 63;
215 env
->xer
|= (1 << XER_CA
);
217 env
->xer
&= ~(1 << XER_CA
);
224 target_ulong
helper_popcntb (target_ulong val
)
226 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
227 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
228 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
232 #if defined(TARGET_PPC64)
233 target_ulong
helper_popcntb_64 (target_ulong val
)
235 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
236 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
237 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
242 /*****************************************************************************/
243 /* Floating point operations helpers */
244 static always_inline
int fpisneg (float64 d
)
250 return u
.ll
>> 63 != 0;
253 static always_inline
int isden (float64 d
)
259 return ((u
.ll
>> 52) & 0x7FF) == 0;
262 static always_inline
int iszero (float64 d
)
268 return (u
.ll
& ~0x8000000000000000ULL
) == 0;
271 static always_inline
int isinfinity (float64 d
)
277 return ((u
.ll
>> 52) & 0x7FF) == 0x7FF &&
278 (u
.ll
& 0x000FFFFFFFFFFFFFULL
) == 0;
281 #ifdef CONFIG_SOFTFLOAT
282 static always_inline
int isfinite (float64 d
)
288 return (((u
.ll
>> 52) & 0x7FF) != 0x7FF);
291 static always_inline
int isnormal (float64 d
)
297 uint32_t exp
= (u
.ll
>> 52) & 0x7FF;
298 return ((0 < exp
) && (exp
< 0x7FF));
302 uint32_t helper_compute_fprf (uint64_t arg
, uint32_t set_fprf
)
308 isneg
= fpisneg(farg
.d
);
309 if (unlikely(float64_is_nan(farg
.d
))) {
310 if (float64_is_signaling_nan(farg
.d
)) {
311 /* Signaling NaN: flags are undefined */
317 } else if (unlikely(isinfinity(farg
.d
))) {
324 if (iszero(farg
.d
)) {
332 /* Denormalized numbers */
335 /* Normalized numbers */
346 /* We update FPSCR_FPRF */
347 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
348 env
->fpscr
|= ret
<< FPSCR_FPRF
;
350 /* We just need fpcc to update Rc1 */
354 /* Floating-point invalid operations exception */
355 static always_inline
uint64_t fload_invalid_op_excp (int op
)
361 if (op
& POWERPC_EXCP_FP_VXSNAN
) {
362 /* Operation on signaling NaN */
363 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
365 if (op
& POWERPC_EXCP_FP_VXSOFT
) {
366 /* Software-defined condition */
367 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
369 switch (op
& ~(POWERPC_EXCP_FP_VXSOFT
| POWERPC_EXCP_FP_VXSNAN
)) {
370 case POWERPC_EXCP_FP_VXISI
:
371 /* Magnitude subtraction of infinities */
372 env
->fpscr
|= 1 << FPSCR_VXISI
;
374 case POWERPC_EXCP_FP_VXIDI
:
375 /* Division of infinity by infinity */
376 env
->fpscr
|= 1 << FPSCR_VXIDI
;
378 case POWERPC_EXCP_FP_VXZDZ
:
379 /* Division of zero by zero */
380 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
382 case POWERPC_EXCP_FP_VXIMZ
:
383 /* Multiplication of zero by infinity */
384 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
386 case POWERPC_EXCP_FP_VXVC
:
387 /* Ordered comparison of NaN */
388 env
->fpscr
|= 1 << FPSCR_VXVC
;
389 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
390 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
391 /* We must update the target FPR before raising the exception */
393 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
394 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
395 /* Update the floating-point enabled exception summary */
396 env
->fpscr
|= 1 << FPSCR_FEX
;
397 /* Exception is differed */
401 case POWERPC_EXCP_FP_VXSQRT
:
402 /* Square root of a negative number */
403 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
405 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
407 /* Set the result to quiet NaN */
409 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
410 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
413 case POWERPC_EXCP_FP_VXCVI
:
414 /* Invalid conversion */
415 env
->fpscr
|= 1 << FPSCR_VXCVI
;
416 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
418 /* Set the result to quiet NaN */
420 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
421 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
425 /* Update the floating-point invalid operation summary */
426 env
->fpscr
|= 1 << FPSCR_VX
;
427 /* Update the floating-point exception summary */
428 env
->fpscr
|= 1 << FPSCR_FX
;
430 /* Update the floating-point enabled exception summary */
431 env
->fpscr
|= 1 << FPSCR_FEX
;
432 if (msr_fe0
!= 0 || msr_fe1
!= 0)
433 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_FP
| op
);
438 static always_inline
uint64_t float_zero_divide_excp (uint64_t arg1
, uint64_t arg2
)
440 env
->fpscr
|= 1 << FPSCR_ZX
;
441 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
442 /* Update the floating-point exception summary */
443 env
->fpscr
|= 1 << FPSCR_FX
;
445 /* Update the floating-point enabled exception summary */
446 env
->fpscr
|= 1 << FPSCR_FEX
;
447 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
448 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
449 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
452 /* Set the result to infinity */
453 arg1
= ((arg1
^ arg2
) & 0x8000000000000000ULL
);
454 arg1
|= 0x7FFULL
<< 52;
459 static always_inline
void float_overflow_excp (void)
461 env
->fpscr
|= 1 << FPSCR_OX
;
462 /* Update the floating-point exception summary */
463 env
->fpscr
|= 1 << FPSCR_FX
;
465 /* XXX: should adjust the result */
466 /* Update the floating-point enabled exception summary */
467 env
->fpscr
|= 1 << FPSCR_FEX
;
468 /* We must update the target FPR before raising the exception */
469 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
470 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
472 env
->fpscr
|= 1 << FPSCR_XX
;
473 env
->fpscr
|= 1 << FPSCR_FI
;
477 static always_inline
void float_underflow_excp (void)
479 env
->fpscr
|= 1 << FPSCR_UX
;
480 /* Update the floating-point exception summary */
481 env
->fpscr
|= 1 << FPSCR_FX
;
483 /* XXX: should adjust the result */
484 /* Update the floating-point enabled exception summary */
485 env
->fpscr
|= 1 << FPSCR_FEX
;
486 /* We must update the target FPR before raising the exception */
487 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
488 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
492 static always_inline
void float_inexact_excp (void)
494 env
->fpscr
|= 1 << FPSCR_XX
;
495 /* Update the floating-point exception summary */
496 env
->fpscr
|= 1 << FPSCR_FX
;
498 /* Update the floating-point enabled exception summary */
499 env
->fpscr
|= 1 << FPSCR_FEX
;
500 /* We must update the target FPR before raising the exception */
501 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
502 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
506 static always_inline
void fpscr_set_rounding_mode (void)
510 /* Set rounding mode */
513 /* Best approximation (round to nearest) */
514 rnd_type
= float_round_nearest_even
;
517 /* Smaller magnitude (round toward zero) */
518 rnd_type
= float_round_to_zero
;
521 /* Round toward +infinite */
522 rnd_type
= float_round_up
;
526 /* Round toward -infinite */
527 rnd_type
= float_round_down
;
530 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
533 void helper_fpscr_setbit (uint32_t bit
)
537 prev
= (env
->fpscr
>> bit
) & 1;
538 env
->fpscr
|= 1 << bit
;
542 env
->fpscr
|= 1 << FPSCR_FX
;
546 env
->fpscr
|= 1 << FPSCR_FX
;
551 env
->fpscr
|= 1 << FPSCR_FX
;
556 env
->fpscr
|= 1 << FPSCR_FX
;
561 env
->fpscr
|= 1 << FPSCR_FX
;
574 env
->fpscr
|= 1 << FPSCR_VX
;
575 env
->fpscr
|= 1 << FPSCR_FX
;
582 env
->error_code
= POWERPC_EXCP_FP
;
584 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
586 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
588 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
590 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
592 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
594 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
596 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
598 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
600 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
607 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
614 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
621 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
628 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
634 fpscr_set_rounding_mode();
639 /* Update the floating-point enabled exception summary */
640 env
->fpscr
|= 1 << FPSCR_FEX
;
641 /* We have to update Rc1 before raising the exception */
642 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
648 void helper_store_fpscr (uint64_t arg
, uint32_t mask
)
651 * We use only the 32 LSB of the incoming fpr
659 new |= prev
& 0x90000000;
660 for (i
= 0; i
< 7; i
++) {
661 if (mask
& (1 << i
)) {
662 env
->fpscr
&= ~(0xF << (4 * i
));
663 env
->fpscr
|= new & (0xF << (4 * i
));
666 /* Update VX and FEX */
668 env
->fpscr
|= 1 << FPSCR_VX
;
670 env
->fpscr
&= ~(1 << FPSCR_VX
);
671 if ((fpscr_ex
& fpscr_eex
) != 0) {
672 env
->fpscr
|= 1 << FPSCR_FEX
;
673 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
674 /* XXX: we should compute it properly */
675 env
->error_code
= POWERPC_EXCP_FP
;
678 env
->fpscr
&= ~(1 << FPSCR_FEX
);
679 fpscr_set_rounding_mode();
682 void helper_float_check_status (void)
684 #ifdef CONFIG_SOFTFLOAT
685 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
686 (env
->error_code
& POWERPC_EXCP_FP
)) {
687 /* Differred floating-point exception after target FPR update */
688 if (msr_fe0
!= 0 || msr_fe1
!= 0)
689 do_raise_exception_err(env
->exception_index
, env
->error_code
);
690 } else if (env
->fp_status
.float_exception_flags
& float_flag_overflow
) {
691 float_overflow_excp();
692 } else if (env
->fp_status
.float_exception_flags
& float_flag_underflow
) {
693 float_underflow_excp();
694 } else if (env
->fp_status
.float_exception_flags
& float_flag_inexact
) {
695 float_inexact_excp();
698 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
699 (env
->error_code
& POWERPC_EXCP_FP
)) {
700 /* Differred floating-point exception after target FPR update */
701 if (msr_fe0
!= 0 || msr_fe1
!= 0)
702 do_raise_exception_err(env
->exception_index
, env
->error_code
);
708 #ifdef CONFIG_SOFTFLOAT
709 void helper_reset_fpstatus (void)
711 env
->fp_status
.float_exception_flags
= 0;
716 uint64_t helper_fadd (uint64_t arg1
, uint64_t arg2
)
718 CPU_DoubleU farg1
, farg2
;
722 #if USE_PRECISE_EMULATION
723 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
724 float64_is_signaling_nan(farg2
.d
))) {
726 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
727 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
728 fpisneg(farg1
.d
) == fpisneg(farg2
.d
))) {
729 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
731 /* Magnitude subtraction of infinities */
732 farg1
.ll
== fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
735 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
741 uint64_t helper_fsub (uint64_t arg1
, uint64_t arg2
)
743 CPU_DoubleU farg1
, farg2
;
747 #if USE_PRECISE_EMULATION
749 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
750 float64_is_signaling_nan(farg2
.d
))) {
751 /* sNaN subtraction */
752 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
753 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
754 fpisneg(farg1
.d
) != fpisneg(farg2
.d
))) {
755 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
757 /* Magnitude subtraction of infinities */
758 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
762 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
768 uint64_t helper_fmul (uint64_t arg1
, uint64_t arg2
)
770 CPU_DoubleU farg1
, farg2
;
774 #if USE_PRECISE_EMULATION
775 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
776 float64_is_signaling_nan(farg2
.d
))) {
777 /* sNaN multiplication */
778 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
779 } else if (unlikely((isinfinity(farg1
.d
) && iszero(farg2
.d
)) ||
780 (iszero(farg1
.d
) && isinfinity(farg2
.d
)))) {
781 /* Multiplication of zero by infinity */
782 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
784 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
788 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
794 uint64_t helper_fdiv (uint64_t arg1
, uint64_t arg2
)
796 CPU_DoubleU farg1
, farg2
;
800 #if USE_PRECISE_EMULATION
801 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
802 float64_is_signaling_nan(farg2
.d
))) {
804 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
805 } else if (unlikely(isinfinity(farg1
.d
) && isinfinity(farg2
.d
))) {
806 /* Division of infinity by infinity */
807 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI
);
808 } else if (unlikely(iszero(farg2
.d
))) {
809 if (iszero(farg1
.d
)) {
810 /* Division of zero by zero */
811 farg1
.ll
fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ
);
813 /* Division by zero */
814 farg1
.ll
= float_zero_divide_excp(farg1
.d
, farg2
.d
);
817 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
820 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
826 uint64_t helper_fabs (uint64_t arg
)
831 farg
.d
= float64_abs(farg
.d
);
836 uint64_t helper_fnabs (uint64_t arg
)
841 farg
.d
= float64_abs(farg
.d
);
842 farg
.d
= float64_chs(farg
.d
);
847 uint64_t helper_fneg (uint64_t arg
)
852 farg
.d
= float64_chs(farg
.d
);
857 uint64_t helper_fctiw (uint64_t arg
)
862 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
863 /* sNaN conversion */
864 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
865 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
866 /* qNan / infinity conversion */
867 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
869 farg
.ll
= float64_to_int32(farg
.d
, &env
->fp_status
);
870 #if USE_PRECISE_EMULATION
871 /* XXX: higher bits are not supposed to be significant.
872 * to make tests easier, return the same as a real PowerPC 750
874 farg
.ll
|= 0xFFF80000ULL
<< 32;
880 /* fctiwz - fctiwz. */
881 uint64_t helper_fctiwz (uint64_t arg
)
886 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
887 /* sNaN conversion */
888 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
889 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
890 /* qNan / infinity conversion */
891 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
893 farg
.ll
= float64_to_int32_round_to_zero(farg
.d
, &env
->fp_status
);
894 #if USE_PRECISE_EMULATION
895 /* XXX: higher bits are not supposed to be significant.
896 * to make tests easier, return the same as a real PowerPC 750
898 farg
.ll
|= 0xFFF80000ULL
<< 32;
904 #if defined(TARGET_PPC64)
906 uint64_t helper_fcfid (uint64_t arg
)
909 farg
.d
= int64_to_float64(arg
, &env
->fp_status
);
914 uint64_t helper_fctid (uint64_t arg
)
919 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
920 /* sNaN conversion */
921 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
922 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
923 /* qNan / infinity conversion */
924 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
926 farg
.ll
= float64_to_int64(farg
.d
, &env
->fp_status
);
931 /* fctidz - fctidz. */
932 uint64_t helper_fctidz (uint64_t arg
)
937 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
938 /* sNaN conversion */
939 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
940 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
941 /* qNan / infinity conversion */
942 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
944 farg
.ll
= float64_to_int64_round_to_zero(farg
.d
, &env
->fp_status
);
951 static always_inline
uint64_t do_fri (uint64_t arg
, int rounding_mode
)
956 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
958 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
959 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
960 /* qNan / infinity round */
961 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
963 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
964 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
965 /* Restore rounding mode from FPSCR */
966 fpscr_set_rounding_mode();
971 uint64_t helper_frin (uint64_t arg
)
973 return do_fri(arg
, float_round_nearest_even
);
976 uint64_t helper_friz (uint64_t arg
)
978 return do_fri(arg
, float_round_to_zero
);
981 uint64_t helper_frip (uint64_t arg
)
983 return do_fri(arg
, float_round_up
);
986 uint64_t helper_frim (uint64_t arg
)
988 return do_fri(arg
, float_round_down
);
992 uint64_t helper_fmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
994 CPU_DoubleU farg1
, farg2
, farg3
;
999 #if USE_PRECISE_EMULATION
1000 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1001 float64_is_signaling_nan(farg2
.d
) ||
1002 float64_is_signaling_nan(farg3
.d
))) {
1003 /* sNaN operation */
1004 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1007 /* This is the way the PowerPC specification defines it */
1008 float128 ft0_128
, ft1_128
;
1010 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1011 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1012 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1013 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1014 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1015 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1017 /* This is OK on x86 hosts */
1018 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1022 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1023 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1028 /* fmsub - fmsub. */
1029 uint64_t helper_fmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1031 CPU_DoubleU farg1
, farg2
, farg3
;
1036 #if USE_PRECISE_EMULATION
1037 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1038 float64_is_signaling_nan(farg2
.d
) ||
1039 float64_is_signaling_nan(farg3
.d
))) {
1040 /* sNaN operation */
1041 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1044 /* This is the way the PowerPC specification defines it */
1045 float128 ft0_128
, ft1_128
;
1047 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1048 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1049 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1050 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1051 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1052 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1054 /* This is OK on x86 hosts */
1055 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1059 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1060 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1065 /* fnmadd - fnmadd. */
1066 uint64_t helper_fnmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1068 CPU_DoubleU farg1
, farg2
, farg3
;
1074 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1075 float64_is_signaling_nan(farg2
.d
) ||
1076 float64_is_signaling_nan(farg3
.d
))) {
1077 /* sNaN operation */
1078 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1080 #if USE_PRECISE_EMULATION
1082 /* This is the way the PowerPC specification defines it */
1083 float128 ft0_128
, ft1_128
;
1085 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1086 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1087 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1088 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1089 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1090 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1092 /* This is OK on x86 hosts */
1093 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1096 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1097 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1099 if (likely(!isnan(farg1
.d
)))
1100 farg1
.d
= float64_chs(farg1
.d
);
1105 /* fnmsub - fnmsub. */
1106 uint64_t helper_fnmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1108 CPU_DoubleU farg1
, farg2
, farg3
;
1114 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1115 float64_is_signaling_nan(farg2
.d
) ||
1116 float64_is_signaling_nan(farg3
.d
))) {
1117 /* sNaN operation */
1118 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1120 #if USE_PRECISE_EMULATION
1122 /* This is the way the PowerPC specification defines it */
1123 float128 ft0_128
, ft1_128
;
1125 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1126 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1127 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1128 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1129 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1130 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1132 /* This is OK on x86 hosts */
1133 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1136 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1137 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1139 if (likely(!isnan(farg1
.d
)))
1140 farg1
.d
= float64_chs(farg1
.d
);
1147 uint64_t helper_frsp (uint64_t arg
)
1152 #if USE_PRECISE_EMULATION
1153 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1154 /* sNaN square root */
1155 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1157 fard
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1160 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1165 /* fsqrt - fsqrt. */
1166 uint64_t helper_fsqrt (uint64_t arg
)
1171 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1172 /* sNaN square root */
1173 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1174 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1175 /* Square root of a negative nonzero number */
1176 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1178 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1184 uint64_t helper_fre (uint64_t arg
)
1189 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1190 /* sNaN reciprocal */
1191 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1192 } else if (unlikely(iszero(farg
.d
))) {
1193 /* Zero reciprocal */
1194 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1195 } else if (likely(isnormal(farg
.d
))) {
1196 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1198 if (farg
.ll
== 0x8000000000000000ULL
) {
1199 farg
.ll
= 0xFFF0000000000000ULL
;
1200 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1201 farg
.ll
= 0x7FF0000000000000ULL
;
1202 } else if (isnan(farg
.d
)) {
1203 farg
.ll
= 0x7FF8000000000000ULL
;
1204 } else if (fpisneg(farg
.d
)) {
1205 farg
.ll
= 0x8000000000000000ULL
;
1207 farg
.ll
= 0x0000000000000000ULL
;
1214 uint64_t helper_fres (uint64_t arg
)
1219 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1220 /* sNaN reciprocal */
1221 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1222 } else if (unlikely(iszero(farg
.d
))) {
1223 /* Zero reciprocal */
1224 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1225 } else if (likely(isnormal(farg
.d
))) {
1226 #if USE_PRECISE_EMULATION
1227 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1228 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1230 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1233 if (farg
.ll
== 0x8000000000000000ULL
) {
1234 farg
.ll
= 0xFFF0000000000000ULL
;
1235 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1236 farg
.ll
= 0x7FF0000000000000ULL
;
1237 } else if (isnan(farg
.d
)) {
1238 farg
.ll
= 0x7FF8000000000000ULL
;
1239 } else if (fpisneg(farg
.d
)) {
1240 farg
.ll
= 0x8000000000000000ULL
;
1242 farg
.ll
= 0x0000000000000000ULL
;
1248 /* frsqrte - frsqrte. */
1249 uint64_t helper_frsqrte (uint64_t arg
)
1254 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1255 /* sNaN reciprocal square root */
1256 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1257 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1258 /* Reciprocal square root of a negative nonzero number */
1259 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1260 } else if (likely(isnormal(farg
.d
))) {
1261 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1262 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1264 if (farg
.ll
== 0x8000000000000000ULL
) {
1265 farg
.ll
= 0xFFF0000000000000ULL
;
1266 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1267 farg
.ll
= 0x7FF0000000000000ULL
;
1268 } else if (isnan(farg
.d
)) {
1269 farg
.ll
|= 0x000FFFFFFFFFFFFFULL
;
1270 } else if (fpisneg(farg
.d
)) {
1271 farg
.ll
= 0x7FF8000000000000ULL
;
1273 farg
.ll
= 0x0000000000000000ULL
;
1280 uint64_t helper_fsel (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1282 CPU_DoubleU farg1
, farg2
, farg3
;
1288 if (!fpisneg(farg1
.d
) || iszero(farg1
.d
))
1294 uint32_t helper_fcmpu (uint64_t arg1
, uint64_t arg2
)
1296 CPU_DoubleU farg1
, farg2
;
1301 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1302 float64_is_signaling_nan(farg2
.d
))) {
1303 /* sNaN comparison */
1304 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1306 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1308 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1314 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1315 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1319 uint32_t helper_fcmpo (uint64_t arg1
, uint64_t arg2
)
1321 CPU_DoubleU farg1
, farg2
;
1326 if (unlikely(float64_is_nan(farg1
.d
) ||
1327 float64_is_nan(farg2
.d
))) {
1328 if (float64_is_signaling_nan(farg1
.d
) ||
1329 float64_is_signaling_nan(farg2
.d
)) {
1330 /* sNaN comparison */
1331 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
|
1332 POWERPC_EXCP_FP_VXVC
);
1334 /* qNaN comparison */
1335 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC
);
1338 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1340 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1346 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1347 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1351 #if !defined (CONFIG_USER_ONLY)
1352 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
);
1354 void do_store_msr (void)
1356 T0
= hreg_store_msr(env
, T0
, 0);
1358 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1359 do_raise_exception(T0
);
1363 static always_inline
void __do_rfi (target_ulong nip
, target_ulong msr
,
1364 target_ulong msrm
, int keep_msrh
)
1366 #if defined(TARGET_PPC64)
1367 if (msr
& (1ULL << MSR_SF
)) {
1368 nip
= (uint64_t)nip
;
1369 msr
&= (uint64_t)msrm
;
1371 nip
= (uint32_t)nip
;
1372 msr
= (uint32_t)(msr
& msrm
);
1374 msr
|= env
->msr
& ~((uint64_t)0xFFFFFFFF);
1377 nip
= (uint32_t)nip
;
1378 msr
&= (uint32_t)msrm
;
1380 /* XXX: beware: this is false if VLE is supported */
1381 env
->nip
= nip
& ~((target_ulong
)0x00000003);
1382 hreg_store_msr(env
, msr
, 1);
1383 #if defined (DEBUG_OP)
1384 cpu_dump_rfi(env
->nip
, env
->msr
);
1386 /* No need to raise an exception here,
1387 * as rfi is always the last insn of a TB
1389 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1394 __do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1395 ~((target_ulong
)0xFFFF0000), 1);
1398 #if defined(TARGET_PPC64)
1401 __do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1402 ~((target_ulong
)0xFFFF0000), 0);
1405 void do_hrfid (void)
1407 __do_rfi(env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
],
1408 ~((target_ulong
)0xFFFF0000), 0);
1413 void do_tw (int flags
)
1415 if (!likely(!(((int32_t)T0
< (int32_t)T1
&& (flags
& 0x10)) ||
1416 ((int32_t)T0
> (int32_t)T1
&& (flags
& 0x08)) ||
1417 ((int32_t)T0
== (int32_t)T1
&& (flags
& 0x04)) ||
1418 ((uint32_t)T0
< (uint32_t)T1
&& (flags
& 0x02)) ||
1419 ((uint32_t)T0
> (uint32_t)T1
&& (flags
& 0x01))))) {
1420 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1424 #if defined(TARGET_PPC64)
1425 void do_td (int flags
)
1427 if (!likely(!(((int64_t)T0
< (int64_t)T1
&& (flags
& 0x10)) ||
1428 ((int64_t)T0
> (int64_t)T1
&& (flags
& 0x08)) ||
1429 ((int64_t)T0
== (int64_t)T1
&& (flags
& 0x04)) ||
1430 ((uint64_t)T0
< (uint64_t)T1
&& (flags
& 0x02)) ||
1431 ((uint64_t)T0
> (uint64_t)T1
&& (flags
& 0x01)))))
1432 do_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1436 /*****************************************************************************/
1437 /* PowerPC 601 specific instructions (POWER bridge) */
1438 void do_POWER_abso (void)
1440 if ((int32_t)T0
== INT32_MIN
) {
1442 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1443 } else if ((int32_t)T0
< 0) {
1445 env
->xer
&= ~(1 << XER_OV
);
1447 env
->xer
&= ~(1 << XER_OV
);
1451 void do_POWER_clcs (void)
1455 /* Instruction cache line size */
1456 T0
= env
->icache_line_size
;
1459 /* Data cache line size */
1460 T0
= env
->dcache_line_size
;
1463 /* Minimum cache line size */
1464 T0
= env
->icache_line_size
< env
->dcache_line_size
?
1465 env
->icache_line_size
: env
->dcache_line_size
;
1468 /* Maximum cache line size */
1469 T0
= env
->icache_line_size
> env
->dcache_line_size
?
1470 env
->icache_line_size
: env
->dcache_line_size
;
1478 void do_POWER_div (void)
1482 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1484 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1485 env
->spr
[SPR_MQ
] = 0;
1487 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1488 env
->spr
[SPR_MQ
] = tmp
% T1
;
1489 T0
= tmp
/ (int32_t)T1
;
1493 void do_POWER_divo (void)
1497 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1499 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1500 env
->spr
[SPR_MQ
] = 0;
1501 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1503 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1504 env
->spr
[SPR_MQ
] = tmp
% T1
;
1506 if (tmp
> (int64_t)INT32_MAX
|| tmp
< (int64_t)INT32_MIN
) {
1507 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1509 env
->xer
&= ~(1 << XER_OV
);
1515 void do_POWER_divs (void)
1517 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1519 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1520 env
->spr
[SPR_MQ
] = 0;
1522 env
->spr
[SPR_MQ
] = T0
% T1
;
1523 T0
= (int32_t)T0
/ (int32_t)T1
;
1527 void do_POWER_divso (void)
1529 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1531 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1532 env
->spr
[SPR_MQ
] = 0;
1533 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1535 T0
= (int32_t)T0
/ (int32_t)T1
;
1536 env
->spr
[SPR_MQ
] = (int32_t)T0
% (int32_t)T1
;
1537 env
->xer
&= ~(1 << XER_OV
);
1541 void do_POWER_dozo (void)
1543 if ((int32_t)T1
> (int32_t)T0
) {
1546 if (((uint32_t)(~T2
) ^ (uint32_t)T1
^ UINT32_MAX
) &
1547 ((uint32_t)(~T2
) ^ (uint32_t)T0
) & (1UL << 31)) {
1548 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1550 env
->xer
&= ~(1 << XER_OV
);
1554 env
->xer
&= ~(1 << XER_OV
);
1558 void do_POWER_maskg (void)
1562 if ((uint32_t)T0
== (uint32_t)(T1
+ 1)) {
1565 ret
= (UINT32_MAX
>> ((uint32_t)T0
)) ^
1566 ((UINT32_MAX
>> ((uint32_t)T1
)) >> 1);
1567 if ((uint32_t)T0
> (uint32_t)T1
)
1573 void do_POWER_mulo (void)
1577 tmp
= (uint64_t)T0
* (uint64_t)T1
;
1578 env
->spr
[SPR_MQ
] = tmp
>> 32;
1580 if (tmp
>> 32 != ((uint64_t)T0
>> 16) * ((uint64_t)T1
>> 16)) {
1581 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1583 env
->xer
&= ~(1 << XER_OV
);
1587 #if !defined (CONFIG_USER_ONLY)
1588 void do_POWER_rac (void)
1593 /* We don't have to generate many instances of this instruction,
1594 * as rac is supervisor only.
1596 /* XXX: FIX THIS: Pretend we have no BAT */
1597 nb_BATs
= env
->nb_BATs
;
1599 if (get_physical_address(env
, &ctx
, T0
, 0, ACCESS_INT
) == 0)
1601 env
->nb_BATs
= nb_BATs
;
1604 void do_POWER_rfsvc (void)
1606 __do_rfi(env
->lr
, env
->ctr
, 0x0000FFFF, 0);
1609 void do_store_hid0_601 (void)
1613 hid0
= env
->spr
[SPR_HID0
];
1614 if ((T0
^ hid0
) & 0x00000008) {
1615 /* Change current endianness */
1616 env
->hflags
&= ~(1 << MSR_LE
);
1617 env
->hflags_nmsr
&= ~(1 << MSR_LE
);
1618 env
->hflags_nmsr
|= (1 << MSR_LE
) & (((T0
>> 3) & 1) << MSR_LE
);
1619 env
->hflags
|= env
->hflags_nmsr
;
1620 if (loglevel
!= 0) {
1621 fprintf(logfile
, "%s: set endianness to %c => " ADDRX
"\n",
1622 __func__
, T0
& 0x8 ? 'l' : 'b', env
->hflags
);
1625 env
->spr
[SPR_HID0
] = T0
;
1629 /*****************************************************************************/
1630 /* 602 specific instructions */
1631 /* mfrom is the most crazy instruction ever seen, imho ! */
1632 /* Real implementation uses a ROM table. Do the same */
1633 #define USE_MFROM_ROM_TABLE
1634 void do_op_602_mfrom (void)
1636 if (likely(T0
< 602)) {
1637 #if defined(USE_MFROM_ROM_TABLE)
1638 #include "mfrom_table.c"
1639 T0
= mfrom_ROM_table
[T0
];
1642 /* Extremly decomposed:
1644 * T0 = 256 * log10(10 + 1.0) + 0.5
1647 d
= float64_div(d
, 256, &env
->fp_status
);
1649 d
= exp10(d
); // XXX: use float emulation function
1650 d
= float64_add(d
, 1.0, &env
->fp_status
);
1651 d
= log10(d
); // XXX: use float emulation function
1652 d
= float64_mul(d
, 256, &env
->fp_status
);
1653 d
= float64_add(d
, 0.5, &env
->fp_status
);
1654 T0
= float64_round_to_int(d
, &env
->fp_status
);
1661 /*****************************************************************************/
1662 /* Embedded PowerPC specific helpers */
1664 /* XXX: to be improved to check access rights when in user-mode */
1665 void do_load_dcr (void)
1669 if (unlikely(env
->dcr_env
== NULL
)) {
1670 if (loglevel
!= 0) {
1671 fprintf(logfile
, "No DCR environment\n");
1673 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1674 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1675 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, T0
, &val
) != 0)) {
1676 if (loglevel
!= 0) {
1677 fprintf(logfile
, "DCR read error %d %03x\n", (int)T0
, (int)T0
);
1679 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1680 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1686 void do_store_dcr (void)
1688 if (unlikely(env
->dcr_env
== NULL
)) {
1689 if (loglevel
!= 0) {
1690 fprintf(logfile
, "No DCR environment\n");
1692 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1693 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1694 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, T0
, T1
) != 0)) {
1695 if (loglevel
!= 0) {
1696 fprintf(logfile
, "DCR write error %d %03x\n", (int)T0
, (int)T0
);
1698 do_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1699 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1703 #if !defined(CONFIG_USER_ONLY)
1704 void do_40x_rfci (void)
1706 __do_rfi(env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
],
1707 ~((target_ulong
)0xFFFF0000), 0);
1712 __do_rfi(env
->spr
[SPR_BOOKE_CSRR0
], SPR_BOOKE_CSRR1
,
1713 ~((target_ulong
)0x3FFF0000), 0);
1718 __do_rfi(env
->spr
[SPR_BOOKE_DSRR0
], SPR_BOOKE_DSRR1
,
1719 ~((target_ulong
)0x3FFF0000), 0);
1722 void do_rfmci (void)
1724 __do_rfi(env
->spr
[SPR_BOOKE_MCSRR0
], SPR_BOOKE_MCSRR1
,
1725 ~((target_ulong
)0x3FFF0000), 0);
1728 void do_load_403_pb (int num
)
1733 void do_store_403_pb (int num
)
1735 if (likely(env
->pb
[num
] != T0
)) {
1737 /* Should be optimized */
1744 void do_440_dlmzb (void)
1750 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1751 if ((T0
& mask
) == 0)
1755 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1756 if ((T1
& mask
) == 0)
1764 /* SPE extension helpers */
1765 /* Use a table to make this quicker */
1766 static uint8_t hbrev
[16] = {
1767 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1768 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1771 static always_inline
uint8_t byte_reverse (uint8_t val
)
1773 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1776 static always_inline
uint32_t word_reverse (uint32_t val
)
1778 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1779 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1782 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
1783 target_ulong
helper_brinc (target_ulong arg1
, target_ulong arg2
)
1785 uint32_t a
, b
, d
, mask
;
1787 mask
= UINT32_MAX
>> (32 - MASKBITS
);
1790 d
= word_reverse(1 + word_reverse(a
| ~b
));
1791 return (arg1
& ~mask
) | (d
& b
);
1794 uint32_t helper_cntlsw32 (uint32_t val
)
1796 if (val
& 0x80000000)
1802 uint32_t helper_cntlzw32 (uint32_t val
)
1807 #define DO_SPE_OP1(name) \
1808 void do_ev##name (void) \
1810 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) | \
1811 (uint64_t)_do_e##name(T0_64); \
1814 #define DO_SPE_OP2(name) \
1815 void do_ev##name (void) \
1817 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) | \
1818 (uint64_t)_do_e##name(T0_64, T1_64); \
1821 /* Fixed-point vector comparisons */
1822 #define DO_SPE_CMP(name) \
1823 void do_ev##name (void) \
1825 T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32, \
1826 T1_64 >> 32) << 32, \
1827 _do_e##name(T0_64, T1_64)); \
1830 static always_inline
uint32_t _do_evcmp_merge (int t0
, int t1
)
1832 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1835 /* Single precision floating-point conversions from/to integer */
1836 static always_inline
uint32_t _do_efscfsi (int32_t val
)
1840 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1845 static always_inline
uint32_t _do_efscfui (uint32_t val
)
1849 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1854 static always_inline
int32_t _do_efsctsi (uint32_t val
)
1859 /* NaN are not treated the same way IEEE 754 does */
1860 if (unlikely(isnan(u
.f
)))
1863 return float32_to_int32(u
.f
, &env
->spe_status
);
1866 static always_inline
uint32_t _do_efsctui (uint32_t val
)
1871 /* NaN are not treated the same way IEEE 754 does */
1872 if (unlikely(isnan(u
.f
)))
1875 return float32_to_uint32(u
.f
, &env
->spe_status
);
1878 static always_inline
int32_t _do_efsctsiz (uint32_t val
)
1883 /* NaN are not treated the same way IEEE 754 does */
1884 if (unlikely(isnan(u
.f
)))
1887 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1890 static always_inline
uint32_t _do_efsctuiz (uint32_t val
)
1895 /* NaN are not treated the same way IEEE 754 does */
1896 if (unlikely(isnan(u
.f
)))
1899 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1902 void do_efscfsi (void)
1904 T0_64
= _do_efscfsi(T0_64
);
1907 void do_efscfui (void)
1909 T0_64
= _do_efscfui(T0_64
);
1912 void do_efsctsi (void)
1914 T0_64
= _do_efsctsi(T0_64
);
1917 void do_efsctui (void)
1919 T0_64
= _do_efsctui(T0_64
);
1922 void do_efsctsiz (void)
1924 T0_64
= _do_efsctsiz(T0_64
);
1927 void do_efsctuiz (void)
1929 T0_64
= _do_efsctuiz(T0_64
);
1932 /* Single precision floating-point conversion to/from fractional */
1933 static always_inline
uint32_t _do_efscfsf (uint32_t val
)
1938 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1939 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
1940 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1945 static always_inline
uint32_t _do_efscfuf (uint32_t val
)
1950 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1951 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1952 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1957 static always_inline
int32_t _do_efsctsf (uint32_t val
)
1963 /* NaN are not treated the same way IEEE 754 does */
1964 if (unlikely(isnan(u
.f
)))
1966 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1967 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1969 return float32_to_int32(u
.f
, &env
->spe_status
);
1972 static always_inline
uint32_t _do_efsctuf (uint32_t val
)
1978 /* NaN are not treated the same way IEEE 754 does */
1979 if (unlikely(isnan(u
.f
)))
1981 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1982 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1984 return float32_to_uint32(u
.f
, &env
->spe_status
);
1987 static always_inline
int32_t _do_efsctsfz (uint32_t val
)
1993 /* NaN are not treated the same way IEEE 754 does */
1994 if (unlikely(isnan(u
.f
)))
1996 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1997 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1999 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
2002 static always_inline
uint32_t _do_efsctufz (uint32_t val
)
2008 /* NaN are not treated the same way IEEE 754 does */
2009 if (unlikely(isnan(u
.f
)))
2011 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2012 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2014 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2017 void do_efscfsf (void)
2019 T0_64
= _do_efscfsf(T0_64
);
2022 void do_efscfuf (void)
2024 T0_64
= _do_efscfuf(T0_64
);
2027 void do_efsctsf (void)
2029 T0_64
= _do_efsctsf(T0_64
);
2032 void do_efsctuf (void)
2034 T0_64
= _do_efsctuf(T0_64
);
2037 void do_efsctsfz (void)
2039 T0_64
= _do_efsctsfz(T0_64
);
2042 void do_efsctufz (void)
2044 T0_64
= _do_efsctufz(T0_64
);
2047 /* Double precision floating point helpers */
2048 static always_inline
int _do_efdcmplt (uint64_t op1
, uint64_t op2
)
2050 /* XXX: TODO: test special values (NaN, infinites, ...) */
2051 return _do_efdtstlt(op1
, op2
);
2054 static always_inline
int _do_efdcmpgt (uint64_t op1
, uint64_t op2
)
2056 /* XXX: TODO: test special values (NaN, infinites, ...) */
2057 return _do_efdtstgt(op1
, op2
);
2060 static always_inline
int _do_efdcmpeq (uint64_t op1
, uint64_t op2
)
2062 /* XXX: TODO: test special values (NaN, infinites, ...) */
2063 return _do_efdtsteq(op1
, op2
);
2066 void do_efdcmplt (void)
2068 T0
= _do_efdcmplt(T0_64
, T1_64
);
2071 void do_efdcmpgt (void)
2073 T0
= _do_efdcmpgt(T0_64
, T1_64
);
2076 void do_efdcmpeq (void)
2078 T0
= _do_efdcmpeq(T0_64
, T1_64
);
2081 /* Double precision floating-point conversion to/from integer */
2082 static always_inline
uint64_t _do_efdcfsi (int64_t val
)
2086 u
.d
= int64_to_float64(val
, &env
->spe_status
);
2091 static always_inline
uint64_t _do_efdcfui (uint64_t val
)
2095 u
.d
= uint64_to_float64(val
, &env
->spe_status
);
2100 static always_inline
int64_t _do_efdctsi (uint64_t val
)
2105 /* NaN are not treated the same way IEEE 754 does */
2106 if (unlikely(isnan(u
.d
)))
2109 return float64_to_int64(u
.d
, &env
->spe_status
);
2112 static always_inline
uint64_t _do_efdctui (uint64_t val
)
2117 /* NaN are not treated the same way IEEE 754 does */
2118 if (unlikely(isnan(u
.d
)))
2121 return float64_to_uint64(u
.d
, &env
->spe_status
);
2124 static always_inline
int64_t _do_efdctsiz (uint64_t val
)
2129 /* NaN are not treated the same way IEEE 754 does */
2130 if (unlikely(isnan(u
.d
)))
2133 return float64_to_int64_round_to_zero(u
.d
, &env
->spe_status
);
2136 static always_inline
uint64_t _do_efdctuiz (uint64_t val
)
2141 /* NaN are not treated the same way IEEE 754 does */
2142 if (unlikely(isnan(u
.d
)))
2145 return float64_to_uint64_round_to_zero(u
.d
, &env
->spe_status
);
2148 void do_efdcfsi (void)
2150 T0_64
= _do_efdcfsi(T0_64
);
2153 void do_efdcfui (void)
2155 T0_64
= _do_efdcfui(T0_64
);
2158 void do_efdctsi (void)
2160 T0_64
= _do_efdctsi(T0_64
);
2163 void do_efdctui (void)
2165 T0_64
= _do_efdctui(T0_64
);
2168 void do_efdctsiz (void)
2170 T0_64
= _do_efdctsiz(T0_64
);
2173 void do_efdctuiz (void)
2175 T0_64
= _do_efdctuiz(T0_64
);
2178 /* Double precision floating-point conversion to/from fractional */
2179 static always_inline
uint64_t _do_efdcfsf (int64_t val
)
2184 u
.d
= int32_to_float64(val
, &env
->spe_status
);
2185 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2186 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2191 static always_inline
uint64_t _do_efdcfuf (uint64_t val
)
2196 u
.d
= uint32_to_float64(val
, &env
->spe_status
);
2197 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2198 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2203 static always_inline
int64_t _do_efdctsf (uint64_t val
)
2209 /* NaN are not treated the same way IEEE 754 does */
2210 if (unlikely(isnan(u
.d
)))
2212 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2213 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2215 return float64_to_int32(u
.d
, &env
->spe_status
);
2218 static always_inline
uint64_t _do_efdctuf (uint64_t val
)
2224 /* NaN are not treated the same way IEEE 754 does */
2225 if (unlikely(isnan(u
.d
)))
2227 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2228 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2230 return float64_to_uint32(u
.d
, &env
->spe_status
);
2233 static always_inline
int64_t _do_efdctsfz (uint64_t val
)
2239 /* NaN are not treated the same way IEEE 754 does */
2240 if (unlikely(isnan(u
.d
)))
2242 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2243 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2245 return float64_to_int32_round_to_zero(u
.d
, &env
->spe_status
);
2248 static always_inline
uint64_t _do_efdctufz (uint64_t val
)
2254 /* NaN are not treated the same way IEEE 754 does */
2255 if (unlikely(isnan(u
.d
)))
2257 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2258 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2260 return float64_to_uint32_round_to_zero(u
.d
, &env
->spe_status
);
2263 void do_efdcfsf (void)
2265 T0_64
= _do_efdcfsf(T0_64
);
2268 void do_efdcfuf (void)
2270 T0_64
= _do_efdcfuf(T0_64
);
2273 void do_efdctsf (void)
2275 T0_64
= _do_efdctsf(T0_64
);
2278 void do_efdctuf (void)
2280 T0_64
= _do_efdctuf(T0_64
);
2283 void do_efdctsfz (void)
2285 T0_64
= _do_efdctsfz(T0_64
);
2288 void do_efdctufz (void)
2290 T0_64
= _do_efdctufz(T0_64
);
2293 /* Floating point conversion between single and double precision */
2294 static always_inline
uint32_t _do_efscfd (uint64_t val
)
2300 u2
.f
= float64_to_float32(u1
.d
, &env
->spe_status
);
2305 static always_inline
uint64_t _do_efdcfs (uint32_t val
)
2311 u2
.d
= float32_to_float64(u1
.f
, &env
->spe_status
);
2316 void do_efscfd (void)
2318 T0_64
= _do_efscfd(T0_64
);
2321 void do_efdcfs (void)
2323 T0_64
= _do_efdcfs(T0_64
);
2326 /* Single precision fixed-point vector arithmetic */
2342 /* Single-precision floating-point comparisons */
2343 static always_inline
int _do_efscmplt (uint32_t op1
, uint32_t op2
)
2345 /* XXX: TODO: test special values (NaN, infinites, ...) */
2346 return _do_efststlt(op1
, op2
);
2349 static always_inline
int _do_efscmpgt (uint32_t op1
, uint32_t op2
)
2351 /* XXX: TODO: test special values (NaN, infinites, ...) */
2352 return _do_efststgt(op1
, op2
);
2355 static always_inline
int _do_efscmpeq (uint32_t op1
, uint32_t op2
)
2357 /* XXX: TODO: test special values (NaN, infinites, ...) */
2358 return _do_efststeq(op1
, op2
);
2361 void do_efscmplt (void)
2363 T0
= _do_efscmplt(T0_64
, T1_64
);
2366 void do_efscmpgt (void)
2368 T0
= _do_efscmpgt(T0_64
, T1_64
);
2371 void do_efscmpeq (void)
2373 T0
= _do_efscmpeq(T0_64
, T1_64
);
2376 /* Single-precision floating-point vector comparisons */
2378 DO_SPE_CMP(fscmplt
);
2380 DO_SPE_CMP(fscmpgt
);
2382 DO_SPE_CMP(fscmpeq
);
2384 DO_SPE_CMP(fststlt
);
2386 DO_SPE_CMP(fststgt
);
2388 DO_SPE_CMP(fststeq
);
2390 /* Single-precision floating-point vector conversions */
2404 DO_SPE_OP1(fsctsiz
);
2406 DO_SPE_OP1(fsctuiz
);
2412 /*****************************************************************************/
2413 /* Softmmu support */
2414 #if !defined (CONFIG_USER_ONLY)
2416 #define MMUSUFFIX _mmu
2419 #include "softmmu_template.h"
2422 #include "softmmu_template.h"
2425 #include "softmmu_template.h"
2428 #include "softmmu_template.h"
2430 /* try to fill the TLB and return an exception if error. If retaddr is
2431 NULL, it means that the function was called in C code (i.e. not
2432 from generated code or from helper.c) */
2433 /* XXX: fix it to restore all registers */
2434 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
2436 TranslationBlock
*tb
;
2437 CPUState
*saved_env
;
2441 /* XXX: hack to restore env in all cases, even if not called from
2444 env
= cpu_single_env
;
2445 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
2446 if (unlikely(ret
!= 0)) {
2447 if (likely(retaddr
)) {
2448 /* now we have a real cpu fault */
2449 pc
= (unsigned long)retaddr
;
2450 tb
= tb_find_pc(pc
);
2452 /* the PC is inside the translated code. It means that we have
2453 a virtual CPU fault */
2454 cpu_restore_state(tb
, env
, pc
, NULL
);
2457 do_raise_exception_err(env
->exception_index
, env
->error_code
);
2462 /* Software driven TLBs management */
2463 /* PowerPC 602/603 software TLB load instructions helpers */
2464 void do_load_6xx_tlb (int is_code
)
2466 target_ulong RPN
, CMP
, EPN
;
2469 RPN
= env
->spr
[SPR_RPA
];
2471 CMP
= env
->spr
[SPR_ICMP
];
2472 EPN
= env
->spr
[SPR_IMISS
];
2474 CMP
= env
->spr
[SPR_DCMP
];
2475 EPN
= env
->spr
[SPR_DMISS
];
2477 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2478 #if defined (DEBUG_SOFTWARE_TLB)
2479 if (loglevel
!= 0) {
2480 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2481 " PTE1 " ADDRX
" way %d\n",
2482 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2485 /* Store this TLB */
2486 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2487 way
, is_code
, CMP
, RPN
);
2490 void do_load_74xx_tlb (int is_code
)
2492 target_ulong RPN
, CMP
, EPN
;
2495 RPN
= env
->spr
[SPR_PTELO
];
2496 CMP
= env
->spr
[SPR_PTEHI
];
2497 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2498 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2499 #if defined (DEBUG_SOFTWARE_TLB)
2500 if (loglevel
!= 0) {
2501 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2502 " PTE1 " ADDRX
" way %d\n",
2503 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2506 /* Store this TLB */
2507 ppc6xx_tlb_store(env
, (uint32_t)(T0
& TARGET_PAGE_MASK
),
2508 way
, is_code
, CMP
, RPN
);
2511 static always_inline target_ulong
booke_tlb_to_page_size (int size
)
2513 return 1024 << (2 * size
);
2516 static always_inline
int booke_page_size_to_tlb (target_ulong page_size
)
2520 switch (page_size
) {
2554 #if defined (TARGET_PPC64)
2555 case 0x000100000000ULL
:
2558 case 0x000400000000ULL
:
2561 case 0x001000000000ULL
:
2564 case 0x004000000000ULL
:
2567 case 0x010000000000ULL
:
2579 /* Helpers for 4xx TLB management */
2580 void do_4xx_tlbre_lo (void)
2586 tlb
= &env
->tlb
[T0
].tlbe
;
2588 if (tlb
->prot
& PAGE_VALID
)
2590 size
= booke_page_size_to_tlb(tlb
->size
);
2591 if (size
< 0 || size
> 0x7)
2594 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2597 void do_4xx_tlbre_hi (void)
2602 tlb
= &env
->tlb
[T0
].tlbe
;
2604 if (tlb
->prot
& PAGE_EXEC
)
2606 if (tlb
->prot
& PAGE_WRITE
)
2610 void do_4xx_tlbwe_hi (void)
2613 target_ulong page
, end
;
2615 #if defined (DEBUG_SOFTWARE_TLB)
2616 if (loglevel
!= 0) {
2617 fprintf(logfile
, "%s T0 " TDX
" T1 " TDX
"\n", __func__
, T0
, T1
);
2621 tlb
= &env
->tlb
[T0
].tlbe
;
2622 /* Invalidate previous TLB (if it's valid) */
2623 if (tlb
->prot
& PAGE_VALID
) {
2624 end
= tlb
->EPN
+ tlb
->size
;
2625 #if defined (DEBUG_SOFTWARE_TLB)
2626 if (loglevel
!= 0) {
2627 fprintf(logfile
, "%s: invalidate old TLB %d start " ADDRX
2628 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2631 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2632 tlb_flush_page(env
, page
);
2634 tlb
->size
= booke_tlb_to_page_size((T1
>> 7) & 0x7);
2635 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2636 * If this ever occurs, one should use the ppcemb target instead
2637 * of the ppc or ppc64 one
2639 if ((T1
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
2640 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
2641 "are not supported (%d)\n",
2642 tlb
->size
, TARGET_PAGE_SIZE
, (int)((T1
>> 7) & 0x7));
2644 tlb
->EPN
= T1
& ~(tlb
->size
- 1);
2646 tlb
->prot
|= PAGE_VALID
;
2648 tlb
->prot
&= ~PAGE_VALID
;
2650 /* XXX: TO BE FIXED */
2651 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
2653 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2654 tlb
->attr
= T1
& 0xFF;
2655 #if defined (DEBUG_SOFTWARE_TLB)
2656 if (loglevel
!= 0) {
2657 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2658 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2659 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2660 tlb
->prot
& PAGE_READ
? 'r' : '-',
2661 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2662 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2663 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2666 /* Invalidate new TLB (if valid) */
2667 if (tlb
->prot
& PAGE_VALID
) {
2668 end
= tlb
->EPN
+ tlb
->size
;
2669 #if defined (DEBUG_SOFTWARE_TLB)
2670 if (loglevel
!= 0) {
2671 fprintf(logfile
, "%s: invalidate TLB %d start " ADDRX
2672 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2675 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2676 tlb_flush_page(env
, page
);
2680 void do_4xx_tlbwe_lo (void)
2684 #if defined (DEBUG_SOFTWARE_TLB)
2685 if (loglevel
!= 0) {
2686 fprintf(logfile
, "%s T0 " TDX
" T1 " TDX
"\n", __func__
, T0
, T1
);
2690 tlb
= &env
->tlb
[T0
].tlbe
;
2691 tlb
->RPN
= T1
& 0xFFFFFC00;
2692 tlb
->prot
= PAGE_READ
;
2694 tlb
->prot
|= PAGE_EXEC
;
2696 tlb
->prot
|= PAGE_WRITE
;
2697 #if defined (DEBUG_SOFTWARE_TLB)
2698 if (loglevel
!= 0) {
2699 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2700 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2701 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2702 tlb
->prot
& PAGE_READ
? 'r' : '-',
2703 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2704 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2705 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2710 /* PowerPC 440 TLB management */
2711 void do_440_tlbwe (int word
)
2714 target_ulong EPN
, RPN
, size
;
2717 #if defined (DEBUG_SOFTWARE_TLB)
2718 if (loglevel
!= 0) {
2719 fprintf(logfile
, "%s word %d T0 " TDX
" T1 " TDX
"\n",
2720 __func__
, word
, T0
, T1
);
2725 tlb
= &env
->tlb
[T0
].tlbe
;
2728 /* Just here to please gcc */
2730 EPN
= T1
& 0xFFFFFC00;
2731 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
2734 size
= booke_tlb_to_page_size((T1
>> 4) & 0xF);
2735 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
2739 tlb
->attr
|= (T1
>> 8) & 1;
2741 tlb
->prot
|= PAGE_VALID
;
2743 if (tlb
->prot
& PAGE_VALID
) {
2744 tlb
->prot
&= ~PAGE_VALID
;
2748 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
2753 RPN
= T1
& 0xFFFFFC0F;
2754 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
2759 tlb
->attr
= (tlb
->attr
& 0x1) | (T1
& 0x0000FF00);
2760 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
2762 tlb
->prot
|= PAGE_READ
<< 4;
2764 tlb
->prot
|= PAGE_WRITE
<< 4;
2766 tlb
->prot
|= PAGE_EXEC
<< 4;
2768 tlb
->prot
|= PAGE_READ
;
2770 tlb
->prot
|= PAGE_WRITE
;
2772 tlb
->prot
|= PAGE_EXEC
;
2777 void do_440_tlbre (int word
)
2783 tlb
= &env
->tlb
[T0
].tlbe
;
2786 /* Just here to please gcc */
2789 size
= booke_page_size_to_tlb(tlb
->size
);
2790 if (size
< 0 || size
> 0xF)
2793 if (tlb
->attr
& 0x1)
2795 if (tlb
->prot
& PAGE_VALID
)
2797 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
2798 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
2804 T0
= tlb
->attr
& ~0x1;
2805 if (tlb
->prot
& (PAGE_READ
<< 4))
2807 if (tlb
->prot
& (PAGE_WRITE
<< 4))
2809 if (tlb
->prot
& (PAGE_EXEC
<< 4))
2811 if (tlb
->prot
& PAGE_READ
)
2813 if (tlb
->prot
& PAGE_WRITE
)
2815 if (tlb
->prot
& PAGE_EXEC
)
2820 #endif /* !CONFIG_USER_ONLY */