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 helper_raise_exception_err (uint32_t exception
, uint32_t error_code
)
51 raise_exception_err(env
, exception
, error_code
);
54 void helper_raise_debug (void)
56 raise_exception(env
, EXCP_DEBUG
);
60 /*****************************************************************************/
61 /* Registers load and stores */
62 target_ulong
helper_load_cr (void)
64 return (env
->crf
[0] << 28) |
74 void helper_store_cr (target_ulong val
, uint32_t mask
)
78 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
80 env
->crf
[i
] = (val
>> (sh
* 4)) & 0xFUL
;
84 #if defined(TARGET_PPC64)
85 void do_store_pri (int prio
)
87 env
->spr
[SPR_PPR
] &= ~0x001C000000000000ULL
;
88 env
->spr
[SPR_PPR
] |= ((uint64_t)prio
& 0x7) << 50;
92 target_ulong
ppc_load_dump_spr (int sprn
)
95 fprintf(logfile
, "Read SPR %d %03x => " ADDRX
"\n",
96 sprn
, sprn
, env
->spr
[sprn
]);
99 return env
->spr
[sprn
];
102 void ppc_store_dump_spr (int sprn
, target_ulong val
)
105 fprintf(logfile
, "Write SPR %d %03x => " ADDRX
" <= " ADDRX
"\n",
106 sprn
, sprn
, env
->spr
[sprn
], val
);
108 env
->spr
[sprn
] = val
;
111 /*****************************************************************************/
112 /* Memory load and stores */
114 static always_inline target_ulong
get_addr(target_ulong addr
)
116 #if defined(TARGET_PPC64)
121 return (uint32_t)addr
;
124 void helper_lmw (target_ulong addr
, uint32_t reg
)
126 #ifdef CONFIG_USER_ONLY
127 #define ldfun ldl_raw
129 int (*ldfun
)(target_ulong
);
131 switch (env
->mmu_idx
) {
133 case 0: ldfun
= ldl_user
;
135 case 1: ldfun
= ldl_kernel
;
137 case 2: ldfun
= ldl_hypv
;
141 for (; reg
< 32; reg
++, addr
+= 4) {
143 env
->gpr
[reg
] = bswap32(ldfun(get_addr(addr
)));
145 env
->gpr
[reg
] = ldfun(get_addr(addr
));
149 void helper_stmw (target_ulong addr
, uint32_t reg
)
151 #ifdef CONFIG_USER_ONLY
152 #define stfun stl_raw
154 void (*stfun
)(target_ulong
, int);
156 switch (env
->mmu_idx
) {
158 case 0: stfun
= stl_user
;
160 case 1: stfun
= stl_kernel
;
162 case 2: stfun
= stl_hypv
;
166 for (; reg
< 32; reg
++, addr
+= 4) {
168 stfun(get_addr(addr
), bswap32((uint32_t)env
->gpr
[reg
]));
170 stfun(get_addr(addr
), (uint32_t)env
->gpr
[reg
]);
174 /*****************************************************************************/
175 /* Fixed point operations helpers */
176 #if defined(TARGET_PPC64)
178 /* multiply high word */
179 uint64_t helper_mulhd (uint64_t arg1
, uint64_t arg2
)
183 muls64(&tl
, &th
, arg1
, arg2
);
187 /* multiply high word unsigned */
188 uint64_t helper_mulhdu (uint64_t arg1
, uint64_t arg2
)
192 mulu64(&tl
, &th
, arg1
, arg2
);
196 uint64_t helper_mulldo (uint64_t arg1
, uint64_t arg2
)
201 muls64(&tl
, (uint64_t *)&th
, arg1
, arg2
);
202 /* If th != 0 && th != -1, then we had an overflow */
203 if (likely((uint64_t)(th
+ 1) <= 1)) {
204 env
->xer
&= ~(1 << XER_OV
);
206 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
212 target_ulong
helper_cntlzw (target_ulong t
)
217 #if defined(TARGET_PPC64)
218 target_ulong
helper_cntlzd (target_ulong t
)
224 /* shift right arithmetic helper */
225 target_ulong
helper_sraw (target_ulong value
, target_ulong shift
)
229 if (likely(!(shift
& 0x20))) {
230 if (likely((uint32_t)shift
!= 0)) {
232 ret
= (int32_t)value
>> shift
;
233 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
234 env
->xer
&= ~(1 << XER_CA
);
236 env
->xer
|= (1 << XER_CA
);
239 ret
= (int32_t)value
;
240 env
->xer
&= ~(1 << XER_CA
);
243 ret
= (int32_t)value
>> 31;
245 env
->xer
|= (1 << XER_CA
);
247 env
->xer
&= ~(1 << XER_CA
);
250 return (target_long
)ret
;
253 #if defined(TARGET_PPC64)
254 target_ulong
helper_srad (target_ulong value
, target_ulong shift
)
258 if (likely(!(shift
& 0x40))) {
259 if (likely((uint64_t)shift
!= 0)) {
261 ret
= (int64_t)value
>> shift
;
262 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
263 env
->xer
&= ~(1 << XER_CA
);
265 env
->xer
|= (1 << XER_CA
);
268 ret
= (int64_t)value
;
269 env
->xer
&= ~(1 << XER_CA
);
272 ret
= (int64_t)value
>> 63;
274 env
->xer
|= (1 << XER_CA
);
276 env
->xer
&= ~(1 << XER_CA
);
283 target_ulong
helper_popcntb (target_ulong val
)
285 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
286 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
287 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
291 #if defined(TARGET_PPC64)
292 target_ulong
helper_popcntb_64 (target_ulong val
)
294 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
295 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
296 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
301 /*****************************************************************************/
302 /* Floating point operations helpers */
303 uint64_t helper_float32_to_float64(uint32_t arg
)
308 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
312 uint32_t helper_float64_to_float32(uint64_t arg
)
317 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
321 static always_inline
int fpisneg (float64 d
)
327 return u
.ll
>> 63 != 0;
330 static always_inline
int isden (float64 d
)
336 return ((u
.ll
>> 52) & 0x7FF) == 0;
339 static always_inline
int iszero (float64 d
)
345 return (u
.ll
& ~0x8000000000000000ULL
) == 0;
348 static always_inline
int isinfinity (float64 d
)
354 return ((u
.ll
>> 52) & 0x7FF) == 0x7FF &&
355 (u
.ll
& 0x000FFFFFFFFFFFFFULL
) == 0;
358 #ifdef CONFIG_SOFTFLOAT
359 static always_inline
int isfinite (float64 d
)
365 return (((u
.ll
>> 52) & 0x7FF) != 0x7FF);
368 static always_inline
int isnormal (float64 d
)
374 uint32_t exp
= (u
.ll
>> 52) & 0x7FF;
375 return ((0 < exp
) && (exp
< 0x7FF));
379 uint32_t helper_compute_fprf (uint64_t arg
, uint32_t set_fprf
)
385 isneg
= fpisneg(farg
.d
);
386 if (unlikely(float64_is_nan(farg
.d
))) {
387 if (float64_is_signaling_nan(farg
.d
)) {
388 /* Signaling NaN: flags are undefined */
394 } else if (unlikely(isinfinity(farg
.d
))) {
401 if (iszero(farg
.d
)) {
409 /* Denormalized numbers */
412 /* Normalized numbers */
423 /* We update FPSCR_FPRF */
424 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
425 env
->fpscr
|= ret
<< FPSCR_FPRF
;
427 /* We just need fpcc to update Rc1 */
431 /* Floating-point invalid operations exception */
432 static always_inline
uint64_t fload_invalid_op_excp (int op
)
438 if (op
& POWERPC_EXCP_FP_VXSNAN
) {
439 /* Operation on signaling NaN */
440 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
442 if (op
& POWERPC_EXCP_FP_VXSOFT
) {
443 /* Software-defined condition */
444 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
446 switch (op
& ~(POWERPC_EXCP_FP_VXSOFT
| POWERPC_EXCP_FP_VXSNAN
)) {
447 case POWERPC_EXCP_FP_VXISI
:
448 /* Magnitude subtraction of infinities */
449 env
->fpscr
|= 1 << FPSCR_VXISI
;
451 case POWERPC_EXCP_FP_VXIDI
:
452 /* Division of infinity by infinity */
453 env
->fpscr
|= 1 << FPSCR_VXIDI
;
455 case POWERPC_EXCP_FP_VXZDZ
:
456 /* Division of zero by zero */
457 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
459 case POWERPC_EXCP_FP_VXIMZ
:
460 /* Multiplication of zero by infinity */
461 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
463 case POWERPC_EXCP_FP_VXVC
:
464 /* Ordered comparison of NaN */
465 env
->fpscr
|= 1 << FPSCR_VXVC
;
466 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
467 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
468 /* We must update the target FPR before raising the exception */
470 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
471 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
472 /* Update the floating-point enabled exception summary */
473 env
->fpscr
|= 1 << FPSCR_FEX
;
474 /* Exception is differed */
478 case POWERPC_EXCP_FP_VXSQRT
:
479 /* Square root of a negative number */
480 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
482 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
484 /* Set the result to quiet NaN */
486 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
487 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
490 case POWERPC_EXCP_FP_VXCVI
:
491 /* Invalid conversion */
492 env
->fpscr
|= 1 << FPSCR_VXCVI
;
493 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
495 /* Set the result to quiet NaN */
497 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
498 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
502 /* Update the floating-point invalid operation summary */
503 env
->fpscr
|= 1 << FPSCR_VX
;
504 /* Update the floating-point exception summary */
505 env
->fpscr
|= 1 << FPSCR_FX
;
507 /* Update the floating-point enabled exception summary */
508 env
->fpscr
|= 1 << FPSCR_FEX
;
509 if (msr_fe0
!= 0 || msr_fe1
!= 0)
510 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_FP
| op
);
515 static always_inline
uint64_t float_zero_divide_excp (uint64_t arg1
, uint64_t arg2
)
517 env
->fpscr
|= 1 << FPSCR_ZX
;
518 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
519 /* Update the floating-point exception summary */
520 env
->fpscr
|= 1 << FPSCR_FX
;
522 /* Update the floating-point enabled exception summary */
523 env
->fpscr
|= 1 << FPSCR_FEX
;
524 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
525 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
526 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
529 /* Set the result to infinity */
530 arg1
= ((arg1
^ arg2
) & 0x8000000000000000ULL
);
531 arg1
|= 0x7FFULL
<< 52;
536 static always_inline
void float_overflow_excp (void)
538 env
->fpscr
|= 1 << FPSCR_OX
;
539 /* Update the floating-point exception summary */
540 env
->fpscr
|= 1 << FPSCR_FX
;
542 /* XXX: should adjust the result */
543 /* Update the floating-point enabled exception summary */
544 env
->fpscr
|= 1 << FPSCR_FEX
;
545 /* We must update the target FPR before raising the exception */
546 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
547 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
549 env
->fpscr
|= 1 << FPSCR_XX
;
550 env
->fpscr
|= 1 << FPSCR_FI
;
554 static always_inline
void float_underflow_excp (void)
556 env
->fpscr
|= 1 << FPSCR_UX
;
557 /* Update the floating-point exception summary */
558 env
->fpscr
|= 1 << FPSCR_FX
;
560 /* XXX: should adjust the result */
561 /* Update the floating-point enabled exception summary */
562 env
->fpscr
|= 1 << FPSCR_FEX
;
563 /* We must update the target FPR before raising the exception */
564 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
565 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
569 static always_inline
void float_inexact_excp (void)
571 env
->fpscr
|= 1 << FPSCR_XX
;
572 /* Update the floating-point exception summary */
573 env
->fpscr
|= 1 << FPSCR_FX
;
575 /* Update the floating-point enabled exception summary */
576 env
->fpscr
|= 1 << FPSCR_FEX
;
577 /* We must update the target FPR before raising the exception */
578 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
579 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
583 static always_inline
void fpscr_set_rounding_mode (void)
587 /* Set rounding mode */
590 /* Best approximation (round to nearest) */
591 rnd_type
= float_round_nearest_even
;
594 /* Smaller magnitude (round toward zero) */
595 rnd_type
= float_round_to_zero
;
598 /* Round toward +infinite */
599 rnd_type
= float_round_up
;
603 /* Round toward -infinite */
604 rnd_type
= float_round_down
;
607 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
610 void helper_fpscr_setbit (uint32_t bit
)
614 prev
= (env
->fpscr
>> bit
) & 1;
615 env
->fpscr
|= 1 << bit
;
619 env
->fpscr
|= 1 << FPSCR_FX
;
623 env
->fpscr
|= 1 << FPSCR_FX
;
628 env
->fpscr
|= 1 << FPSCR_FX
;
633 env
->fpscr
|= 1 << FPSCR_FX
;
638 env
->fpscr
|= 1 << FPSCR_FX
;
651 env
->fpscr
|= 1 << FPSCR_VX
;
652 env
->fpscr
|= 1 << FPSCR_FX
;
659 env
->error_code
= POWERPC_EXCP_FP
;
661 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
663 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
665 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
667 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
669 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
671 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
673 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
675 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
677 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
684 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
691 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
698 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
705 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
711 fpscr_set_rounding_mode();
716 /* Update the floating-point enabled exception summary */
717 env
->fpscr
|= 1 << FPSCR_FEX
;
718 /* We have to update Rc1 before raising the exception */
719 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
725 void helper_store_fpscr (uint64_t arg
, uint32_t mask
)
728 * We use only the 32 LSB of the incoming fpr
736 new |= prev
& 0x90000000;
737 for (i
= 0; i
< 7; i
++) {
738 if (mask
& (1 << i
)) {
739 env
->fpscr
&= ~(0xF << (4 * i
));
740 env
->fpscr
|= new & (0xF << (4 * i
));
743 /* Update VX and FEX */
745 env
->fpscr
|= 1 << FPSCR_VX
;
747 env
->fpscr
&= ~(1 << FPSCR_VX
);
748 if ((fpscr_ex
& fpscr_eex
) != 0) {
749 env
->fpscr
|= 1 << FPSCR_FEX
;
750 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
751 /* XXX: we should compute it properly */
752 env
->error_code
= POWERPC_EXCP_FP
;
755 env
->fpscr
&= ~(1 << FPSCR_FEX
);
756 fpscr_set_rounding_mode();
759 void helper_float_check_status (void)
761 #ifdef CONFIG_SOFTFLOAT
762 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
763 (env
->error_code
& POWERPC_EXCP_FP
)) {
764 /* Differred floating-point exception after target FPR update */
765 if (msr_fe0
!= 0 || msr_fe1
!= 0)
766 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
767 } else if (env
->fp_status
.float_exception_flags
& float_flag_overflow
) {
768 float_overflow_excp();
769 } else if (env
->fp_status
.float_exception_flags
& float_flag_underflow
) {
770 float_underflow_excp();
771 } else if (env
->fp_status
.float_exception_flags
& float_flag_inexact
) {
772 float_inexact_excp();
775 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
776 (env
->error_code
& POWERPC_EXCP_FP
)) {
777 /* Differred floating-point exception after target FPR update */
778 if (msr_fe0
!= 0 || msr_fe1
!= 0)
779 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
785 #ifdef CONFIG_SOFTFLOAT
786 void helper_reset_fpstatus (void)
788 env
->fp_status
.float_exception_flags
= 0;
793 uint64_t helper_fadd (uint64_t arg1
, uint64_t arg2
)
795 CPU_DoubleU farg1
, farg2
;
799 #if USE_PRECISE_EMULATION
800 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
801 float64_is_signaling_nan(farg2
.d
))) {
803 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
804 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
805 fpisneg(farg1
.d
) == fpisneg(farg2
.d
))) {
806 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
808 /* Magnitude subtraction of infinities */
809 farg1
.ll
== fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
812 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
818 uint64_t helper_fsub (uint64_t arg1
, uint64_t arg2
)
820 CPU_DoubleU farg1
, farg2
;
824 #if USE_PRECISE_EMULATION
826 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
827 float64_is_signaling_nan(farg2
.d
))) {
828 /* sNaN subtraction */
829 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
830 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
831 fpisneg(farg1
.d
) != fpisneg(farg2
.d
))) {
832 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
834 /* Magnitude subtraction of infinities */
835 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
839 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
845 uint64_t helper_fmul (uint64_t arg1
, uint64_t arg2
)
847 CPU_DoubleU farg1
, farg2
;
851 #if USE_PRECISE_EMULATION
852 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
853 float64_is_signaling_nan(farg2
.d
))) {
854 /* sNaN multiplication */
855 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
856 } else if (unlikely((isinfinity(farg1
.d
) && iszero(farg2
.d
)) ||
857 (iszero(farg1
.d
) && isinfinity(farg2
.d
)))) {
858 /* Multiplication of zero by infinity */
859 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
861 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
865 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
871 uint64_t helper_fdiv (uint64_t arg1
, uint64_t arg2
)
873 CPU_DoubleU farg1
, farg2
;
877 #if USE_PRECISE_EMULATION
878 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
879 float64_is_signaling_nan(farg2
.d
))) {
881 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
882 } else if (unlikely(isinfinity(farg1
.d
) && isinfinity(farg2
.d
))) {
883 /* Division of infinity by infinity */
884 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI
);
885 } else if (unlikely(iszero(farg2
.d
))) {
886 if (iszero(farg1
.d
)) {
887 /* Division of zero by zero */
888 farg1
.ll
fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ
);
890 /* Division by zero */
891 farg1
.ll
= float_zero_divide_excp(farg1
.d
, farg2
.d
);
894 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
897 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
903 uint64_t helper_fabs (uint64_t arg
)
908 farg
.d
= float64_abs(farg
.d
);
913 uint64_t helper_fnabs (uint64_t arg
)
918 farg
.d
= float64_abs(farg
.d
);
919 farg
.d
= float64_chs(farg
.d
);
924 uint64_t helper_fneg (uint64_t arg
)
929 farg
.d
= float64_chs(farg
.d
);
934 uint64_t helper_fctiw (uint64_t arg
)
939 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
940 /* sNaN conversion */
941 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
942 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
943 /* qNan / infinity conversion */
944 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
946 farg
.ll
= float64_to_int32(farg
.d
, &env
->fp_status
);
947 #if USE_PRECISE_EMULATION
948 /* XXX: higher bits are not supposed to be significant.
949 * to make tests easier, return the same as a real PowerPC 750
951 farg
.ll
|= 0xFFF80000ULL
<< 32;
957 /* fctiwz - fctiwz. */
958 uint64_t helper_fctiwz (uint64_t arg
)
963 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
964 /* sNaN conversion */
965 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
966 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
967 /* qNan / infinity conversion */
968 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
970 farg
.ll
= float64_to_int32_round_to_zero(farg
.d
, &env
->fp_status
);
971 #if USE_PRECISE_EMULATION
972 /* XXX: higher bits are not supposed to be significant.
973 * to make tests easier, return the same as a real PowerPC 750
975 farg
.ll
|= 0xFFF80000ULL
<< 32;
981 #if defined(TARGET_PPC64)
983 uint64_t helper_fcfid (uint64_t arg
)
986 farg
.d
= int64_to_float64(arg
, &env
->fp_status
);
991 uint64_t helper_fctid (uint64_t arg
)
996 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
997 /* sNaN conversion */
998 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
999 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1000 /* qNan / infinity conversion */
1001 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1003 farg
.ll
= float64_to_int64(farg
.d
, &env
->fp_status
);
1008 /* fctidz - fctidz. */
1009 uint64_t helper_fctidz (uint64_t arg
)
1014 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1015 /* sNaN conversion */
1016 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1017 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1018 /* qNan / infinity conversion */
1019 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1021 farg
.ll
= float64_to_int64_round_to_zero(farg
.d
, &env
->fp_status
);
1028 static always_inline
uint64_t do_fri (uint64_t arg
, int rounding_mode
)
1033 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1035 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1036 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1037 /* qNan / infinity round */
1038 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1040 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
1041 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
1042 /* Restore rounding mode from FPSCR */
1043 fpscr_set_rounding_mode();
1048 uint64_t helper_frin (uint64_t arg
)
1050 return do_fri(arg
, float_round_nearest_even
);
1053 uint64_t helper_friz (uint64_t arg
)
1055 return do_fri(arg
, float_round_to_zero
);
1058 uint64_t helper_frip (uint64_t arg
)
1060 return do_fri(arg
, float_round_up
);
1063 uint64_t helper_frim (uint64_t arg
)
1065 return do_fri(arg
, float_round_down
);
1068 /* fmadd - fmadd. */
1069 uint64_t helper_fmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1071 CPU_DoubleU farg1
, farg2
, farg3
;
1076 #if USE_PRECISE_EMULATION
1077 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1078 float64_is_signaling_nan(farg2
.d
) ||
1079 float64_is_signaling_nan(farg3
.d
))) {
1080 /* sNaN operation */
1081 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1084 /* This is the way the PowerPC specification defines it */
1085 float128 ft0_128
, ft1_128
;
1087 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1088 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1089 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1090 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1091 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1092 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1094 /* This is OK on x86 hosts */
1095 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1099 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1100 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1105 /* fmsub - fmsub. */
1106 uint64_t helper_fmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1108 CPU_DoubleU farg1
, farg2
, farg3
;
1113 #if USE_PRECISE_EMULATION
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
);
1121 /* This is the way the PowerPC specification defines it */
1122 float128 ft0_128
, ft1_128
;
1124 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1125 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1126 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1127 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1128 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1129 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1131 /* This is OK on x86 hosts */
1132 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
);
1142 /* fnmadd - fnmadd. */
1143 uint64_t helper_fnmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1145 CPU_DoubleU farg1
, farg2
, farg3
;
1151 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1152 float64_is_signaling_nan(farg2
.d
) ||
1153 float64_is_signaling_nan(farg3
.d
))) {
1154 /* sNaN operation */
1155 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1157 #if USE_PRECISE_EMULATION
1159 /* This is the way the PowerPC specification defines it */
1160 float128 ft0_128
, ft1_128
;
1162 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1163 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1164 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1165 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1166 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1167 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1169 /* This is OK on x86 hosts */
1170 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1173 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1174 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1176 if (likely(!isnan(farg1
.d
)))
1177 farg1
.d
= float64_chs(farg1
.d
);
1182 /* fnmsub - fnmsub. */
1183 uint64_t helper_fnmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1185 CPU_DoubleU farg1
, farg2
, farg3
;
1191 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1192 float64_is_signaling_nan(farg2
.d
) ||
1193 float64_is_signaling_nan(farg3
.d
))) {
1194 /* sNaN operation */
1195 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1197 #if USE_PRECISE_EMULATION
1199 /* This is the way the PowerPC specification defines it */
1200 float128 ft0_128
, ft1_128
;
1202 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1203 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1204 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1205 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1206 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1207 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1209 /* This is OK on x86 hosts */
1210 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1213 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1214 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1216 if (likely(!isnan(farg1
.d
)))
1217 farg1
.d
= float64_chs(farg1
.d
);
1224 uint64_t helper_frsp (uint64_t arg
)
1229 #if USE_PRECISE_EMULATION
1230 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1231 /* sNaN square root */
1232 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1234 fard
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1237 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1242 /* fsqrt - fsqrt. */
1243 uint64_t helper_fsqrt (uint64_t arg
)
1248 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1249 /* sNaN square root */
1250 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1251 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1252 /* Square root of a negative nonzero number */
1253 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1255 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1261 uint64_t helper_fre (uint64_t arg
)
1266 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1267 /* sNaN reciprocal */
1268 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1269 } else if (unlikely(iszero(farg
.d
))) {
1270 /* Zero reciprocal */
1271 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1272 } else if (likely(isnormal(farg
.d
))) {
1273 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1275 if (farg
.ll
== 0x8000000000000000ULL
) {
1276 farg
.ll
= 0xFFF0000000000000ULL
;
1277 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1278 farg
.ll
= 0x7FF0000000000000ULL
;
1279 } else if (isnan(farg
.d
)) {
1280 farg
.ll
= 0x7FF8000000000000ULL
;
1281 } else if (fpisneg(farg
.d
)) {
1282 farg
.ll
= 0x8000000000000000ULL
;
1284 farg
.ll
= 0x0000000000000000ULL
;
1291 uint64_t helper_fres (uint64_t arg
)
1296 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1297 /* sNaN reciprocal */
1298 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1299 } else if (unlikely(iszero(farg
.d
))) {
1300 /* Zero reciprocal */
1301 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1302 } else if (likely(isnormal(farg
.d
))) {
1303 #if USE_PRECISE_EMULATION
1304 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1305 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1307 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1310 if (farg
.ll
== 0x8000000000000000ULL
) {
1311 farg
.ll
= 0xFFF0000000000000ULL
;
1312 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1313 farg
.ll
= 0x7FF0000000000000ULL
;
1314 } else if (isnan(farg
.d
)) {
1315 farg
.ll
= 0x7FF8000000000000ULL
;
1316 } else if (fpisneg(farg
.d
)) {
1317 farg
.ll
= 0x8000000000000000ULL
;
1319 farg
.ll
= 0x0000000000000000ULL
;
1325 /* frsqrte - frsqrte. */
1326 uint64_t helper_frsqrte (uint64_t arg
)
1331 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1332 /* sNaN reciprocal square root */
1333 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1334 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1335 /* Reciprocal square root of a negative nonzero number */
1336 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1337 } else if (likely(isnormal(farg
.d
))) {
1338 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1339 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1341 if (farg
.ll
== 0x8000000000000000ULL
) {
1342 farg
.ll
= 0xFFF0000000000000ULL
;
1343 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1344 farg
.ll
= 0x7FF0000000000000ULL
;
1345 } else if (isnan(farg
.d
)) {
1346 farg
.ll
|= 0x000FFFFFFFFFFFFFULL
;
1347 } else if (fpisneg(farg
.d
)) {
1348 farg
.ll
= 0x7FF8000000000000ULL
;
1350 farg
.ll
= 0x0000000000000000ULL
;
1357 uint64_t helper_fsel (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1359 CPU_DoubleU farg1
, farg2
, farg3
;
1365 if (!fpisneg(farg1
.d
) || iszero(farg1
.d
))
1371 uint32_t helper_fcmpu (uint64_t arg1
, uint64_t arg2
)
1373 CPU_DoubleU farg1
, farg2
;
1378 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1379 float64_is_signaling_nan(farg2
.d
))) {
1380 /* sNaN comparison */
1381 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1383 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1385 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1391 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1392 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1396 uint32_t helper_fcmpo (uint64_t arg1
, uint64_t arg2
)
1398 CPU_DoubleU farg1
, farg2
;
1403 if (unlikely(float64_is_nan(farg1
.d
) ||
1404 float64_is_nan(farg2
.d
))) {
1405 if (float64_is_signaling_nan(farg1
.d
) ||
1406 float64_is_signaling_nan(farg2
.d
)) {
1407 /* sNaN comparison */
1408 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
|
1409 POWERPC_EXCP_FP_VXVC
);
1411 /* qNaN comparison */
1412 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC
);
1415 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1417 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1423 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1424 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1428 #if !defined (CONFIG_USER_ONLY)
1429 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
);
1431 void do_store_msr (void)
1433 T0
= hreg_store_msr(env
, T0
, 0);
1435 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1436 raise_exception(env
, T0
);
1440 static always_inline
void __do_rfi (target_ulong nip
, target_ulong msr
,
1441 target_ulong msrm
, int keep_msrh
)
1443 #if defined(TARGET_PPC64)
1444 if (msr
& (1ULL << MSR_SF
)) {
1445 nip
= (uint64_t)nip
;
1446 msr
&= (uint64_t)msrm
;
1448 nip
= (uint32_t)nip
;
1449 msr
= (uint32_t)(msr
& msrm
);
1451 msr
|= env
->msr
& ~((uint64_t)0xFFFFFFFF);
1454 nip
= (uint32_t)nip
;
1455 msr
&= (uint32_t)msrm
;
1457 /* XXX: beware: this is false if VLE is supported */
1458 env
->nip
= nip
& ~((target_ulong
)0x00000003);
1459 hreg_store_msr(env
, msr
, 1);
1460 #if defined (DEBUG_OP)
1461 cpu_dump_rfi(env
->nip
, env
->msr
);
1463 /* No need to raise an exception here,
1464 * as rfi is always the last insn of a TB
1466 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1471 __do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1472 ~((target_ulong
)0xFFFF0000), 1);
1475 #if defined(TARGET_PPC64)
1478 __do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1479 ~((target_ulong
)0xFFFF0000), 0);
1482 void do_hrfid (void)
1484 __do_rfi(env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
],
1485 ~((target_ulong
)0xFFFF0000), 0);
1490 void helper_tw (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1492 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
1493 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
1494 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
1495 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
1496 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
1497 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1501 #if defined(TARGET_PPC64)
1502 void helper_td (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1504 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
1505 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
1506 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
1507 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
1508 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01)))))
1509 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1513 /*****************************************************************************/
1514 /* PowerPC 601 specific instructions (POWER bridge) */
1515 void do_POWER_abso (void)
1517 if ((int32_t)T0
== INT32_MIN
) {
1519 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1520 } else if ((int32_t)T0
< 0) {
1522 env
->xer
&= ~(1 << XER_OV
);
1524 env
->xer
&= ~(1 << XER_OV
);
1528 void do_POWER_clcs (void)
1532 /* Instruction cache line size */
1533 T0
= env
->icache_line_size
;
1536 /* Data cache line size */
1537 T0
= env
->dcache_line_size
;
1540 /* Minimum cache line size */
1541 T0
= env
->icache_line_size
< env
->dcache_line_size
?
1542 env
->icache_line_size
: env
->dcache_line_size
;
1545 /* Maximum cache line size */
1546 T0
= env
->icache_line_size
> env
->dcache_line_size
?
1547 env
->icache_line_size
: env
->dcache_line_size
;
1555 void do_POWER_div (void)
1559 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1561 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1562 env
->spr
[SPR_MQ
] = 0;
1564 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1565 env
->spr
[SPR_MQ
] = tmp
% T1
;
1566 T0
= tmp
/ (int32_t)T1
;
1570 void do_POWER_divo (void)
1574 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1576 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1577 env
->spr
[SPR_MQ
] = 0;
1578 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1580 tmp
= ((uint64_t)T0
<< 32) | env
->spr
[SPR_MQ
];
1581 env
->spr
[SPR_MQ
] = tmp
% T1
;
1583 if (tmp
> (int64_t)INT32_MAX
|| tmp
< (int64_t)INT32_MIN
) {
1584 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1586 env
->xer
&= ~(1 << XER_OV
);
1592 void do_POWER_divs (void)
1594 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1596 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1597 env
->spr
[SPR_MQ
] = 0;
1599 env
->spr
[SPR_MQ
] = T0
% T1
;
1600 T0
= (int32_t)T0
/ (int32_t)T1
;
1604 void do_POWER_divso (void)
1606 if (((int32_t)T0
== INT32_MIN
&& (int32_t)T1
== (int32_t)-1) ||
1608 T0
= UINT32_MAX
* ((uint32_t)T0
>> 31);
1609 env
->spr
[SPR_MQ
] = 0;
1610 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1612 T0
= (int32_t)T0
/ (int32_t)T1
;
1613 env
->spr
[SPR_MQ
] = (int32_t)T0
% (int32_t)T1
;
1614 env
->xer
&= ~(1 << XER_OV
);
1618 void do_POWER_dozo (void)
1620 if ((int32_t)T1
> (int32_t)T0
) {
1623 if (((uint32_t)(~T2
) ^ (uint32_t)T1
^ UINT32_MAX
) &
1624 ((uint32_t)(~T2
) ^ (uint32_t)T0
) & (1UL << 31)) {
1625 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1627 env
->xer
&= ~(1 << XER_OV
);
1631 env
->xer
&= ~(1 << XER_OV
);
1635 void do_POWER_maskg (void)
1639 if ((uint32_t)T0
== (uint32_t)(T1
+ 1)) {
1642 ret
= (UINT32_MAX
>> ((uint32_t)T0
)) ^
1643 ((UINT32_MAX
>> ((uint32_t)T1
)) >> 1);
1644 if ((uint32_t)T0
> (uint32_t)T1
)
1650 void do_POWER_mulo (void)
1654 tmp
= (uint64_t)T0
* (uint64_t)T1
;
1655 env
->spr
[SPR_MQ
] = tmp
>> 32;
1657 if (tmp
>> 32 != ((uint64_t)T0
>> 16) * ((uint64_t)T1
>> 16)) {
1658 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1660 env
->xer
&= ~(1 << XER_OV
);
1664 #if !defined (CONFIG_USER_ONLY)
1665 void do_POWER_rac (void)
1670 /* We don't have to generate many instances of this instruction,
1671 * as rac is supervisor only.
1673 /* XXX: FIX THIS: Pretend we have no BAT */
1674 nb_BATs
= env
->nb_BATs
;
1676 if (get_physical_address(env
, &ctx
, T0
, 0, ACCESS_INT
) == 0)
1678 env
->nb_BATs
= nb_BATs
;
1681 void do_POWER_rfsvc (void)
1683 __do_rfi(env
->lr
, env
->ctr
, 0x0000FFFF, 0);
1686 void do_store_hid0_601 (void)
1690 hid0
= env
->spr
[SPR_HID0
];
1691 if ((T0
^ hid0
) & 0x00000008) {
1692 /* Change current endianness */
1693 env
->hflags
&= ~(1 << MSR_LE
);
1694 env
->hflags_nmsr
&= ~(1 << MSR_LE
);
1695 env
->hflags_nmsr
|= (1 << MSR_LE
) & (((T0
>> 3) & 1) << MSR_LE
);
1696 env
->hflags
|= env
->hflags_nmsr
;
1697 if (loglevel
!= 0) {
1698 fprintf(logfile
, "%s: set endianness to %c => " ADDRX
"\n",
1699 __func__
, T0
& 0x8 ? 'l' : 'b', env
->hflags
);
1702 env
->spr
[SPR_HID0
] = T0
;
1706 /*****************************************************************************/
1707 /* 602 specific instructions */
1708 /* mfrom is the most crazy instruction ever seen, imho ! */
1709 /* Real implementation uses a ROM table. Do the same */
1710 #define USE_MFROM_ROM_TABLE
1711 target_ulong
helper_602_mfrom (target_ulong arg
)
1713 if (likely(arg
< 602)) {
1714 #if defined(USE_MFROM_ROM_TABLE)
1715 #include "mfrom_table.c"
1716 return mfrom_ROM_table
[T0
];
1719 /* Extremly decomposed:
1721 * return 256 * log10(10 + 1.0) + 0.5
1724 d
= float64_div(d
, 256, &env
->fp_status
);
1726 d
= exp10(d
); // XXX: use float emulation function
1727 d
= float64_add(d
, 1.0, &env
->fp_status
);
1728 d
= log10(d
); // XXX: use float emulation function
1729 d
= float64_mul(d
, 256, &env
->fp_status
);
1730 d
= float64_add(d
, 0.5, &env
->fp_status
);
1731 return float64_round_to_int(d
, &env
->fp_status
);
1738 /*****************************************************************************/
1739 /* Embedded PowerPC specific helpers */
1741 /* XXX: to be improved to check access rights when in user-mode */
1742 void do_load_dcr (void)
1746 if (unlikely(env
->dcr_env
== NULL
)) {
1747 if (loglevel
!= 0) {
1748 fprintf(logfile
, "No DCR environment\n");
1750 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1751 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1752 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, T0
, &val
) != 0)) {
1753 if (loglevel
!= 0) {
1754 fprintf(logfile
, "DCR read error %d %03x\n", (int)T0
, (int)T0
);
1756 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1757 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1763 void do_store_dcr (void)
1765 if (unlikely(env
->dcr_env
== NULL
)) {
1766 if (loglevel
!= 0) {
1767 fprintf(logfile
, "No DCR environment\n");
1769 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1770 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1771 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, T0
, T1
) != 0)) {
1772 if (loglevel
!= 0) {
1773 fprintf(logfile
, "DCR write error %d %03x\n", (int)T0
, (int)T0
);
1775 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1776 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1780 #if !defined(CONFIG_USER_ONLY)
1781 void do_40x_rfci (void)
1783 __do_rfi(env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
],
1784 ~((target_ulong
)0xFFFF0000), 0);
1789 __do_rfi(env
->spr
[SPR_BOOKE_CSRR0
], SPR_BOOKE_CSRR1
,
1790 ~((target_ulong
)0x3FFF0000), 0);
1795 __do_rfi(env
->spr
[SPR_BOOKE_DSRR0
], SPR_BOOKE_DSRR1
,
1796 ~((target_ulong
)0x3FFF0000), 0);
1799 void do_rfmci (void)
1801 __do_rfi(env
->spr
[SPR_BOOKE_MCSRR0
], SPR_BOOKE_MCSRR1
,
1802 ~((target_ulong
)0x3FFF0000), 0);
1805 void do_load_403_pb (int num
)
1810 void do_store_403_pb (int num
)
1812 if (likely(env
->pb
[num
] != T0
)) {
1814 /* Should be optimized */
1821 void do_440_dlmzb (void)
1827 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1828 if ((T0
& mask
) == 0)
1832 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1833 if ((T1
& mask
) == 0)
1841 /*****************************************************************************/
1842 /* SPE extension helpers */
1843 /* Use a table to make this quicker */
1844 static uint8_t hbrev
[16] = {
1845 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1846 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1849 static always_inline
uint8_t byte_reverse (uint8_t val
)
1851 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1854 static always_inline
uint32_t word_reverse (uint32_t val
)
1856 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1857 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1860 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
1861 target_ulong
helper_brinc (target_ulong arg1
, target_ulong arg2
)
1863 uint32_t a
, b
, d
, mask
;
1865 mask
= UINT32_MAX
>> (32 - MASKBITS
);
1868 d
= word_reverse(1 + word_reverse(a
| ~b
));
1869 return (arg1
& ~mask
) | (d
& b
);
1872 uint32_t helper_cntlsw32 (uint32_t val
)
1874 if (val
& 0x80000000)
1880 uint32_t helper_cntlzw32 (uint32_t val
)
1885 /* Single-precision floating-point conversions */
1886 static always_inline
uint32_t efscfsi (uint32_t val
)
1890 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1895 static always_inline
uint32_t efscfui (uint32_t val
)
1899 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1904 static always_inline
int32_t efsctsi (uint32_t val
)
1909 /* NaN are not treated the same way IEEE 754 does */
1910 if (unlikely(isnan(u
.f
)))
1913 return float32_to_int32(u
.f
, &env
->spe_status
);
1916 static always_inline
uint32_t efsctui (uint32_t val
)
1921 /* NaN are not treated the same way IEEE 754 does */
1922 if (unlikely(isnan(u
.f
)))
1925 return float32_to_uint32(u
.f
, &env
->spe_status
);
1928 static always_inline
uint32_t efsctsiz (uint32_t val
)
1933 /* NaN are not treated the same way IEEE 754 does */
1934 if (unlikely(isnan(u
.f
)))
1937 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1940 static always_inline
uint32_t efsctuiz (uint32_t val
)
1945 /* NaN are not treated the same way IEEE 754 does */
1946 if (unlikely(isnan(u
.f
)))
1949 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1952 static always_inline
uint32_t efscfsf (uint32_t val
)
1957 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1958 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
1959 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1964 static always_inline
uint32_t efscfuf (uint32_t val
)
1969 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1970 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1971 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1976 static always_inline
uint32_t efsctsf (uint32_t val
)
1982 /* NaN are not treated the same way IEEE 754 does */
1983 if (unlikely(isnan(u
.f
)))
1985 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
1986 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
1988 return float32_to_int32(u
.f
, &env
->spe_status
);
1991 static always_inline
uint32_t efsctuf (uint32_t val
)
1997 /* NaN are not treated the same way IEEE 754 does */
1998 if (unlikely(isnan(u
.f
)))
2000 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2001 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2003 return float32_to_uint32(u
.f
, &env
->spe_status
);
2006 #define HELPER_SPE_SINGLE_CONV(name) \
2007 uint32_t helper_e##name (uint32_t val) \
2009 return e##name(val); \
2012 HELPER_SPE_SINGLE_CONV(fscfsi
);
2014 HELPER_SPE_SINGLE_CONV(fscfui
);
2016 HELPER_SPE_SINGLE_CONV(fscfuf
);
2018 HELPER_SPE_SINGLE_CONV(fscfsf
);
2020 HELPER_SPE_SINGLE_CONV(fsctsi
);
2022 HELPER_SPE_SINGLE_CONV(fsctui
);
2024 HELPER_SPE_SINGLE_CONV(fsctsiz
);
2026 HELPER_SPE_SINGLE_CONV(fsctuiz
);
2028 HELPER_SPE_SINGLE_CONV(fsctsf
);
2030 HELPER_SPE_SINGLE_CONV(fsctuf
);
2032 #define HELPER_SPE_VECTOR_CONV(name) \
2033 uint64_t helper_ev##name (uint64_t val) \
2035 return ((uint64_t)e##name(val >> 32) << 32) | \
2036 (uint64_t)e##name(val); \
2039 HELPER_SPE_VECTOR_CONV(fscfsi
);
2041 HELPER_SPE_VECTOR_CONV(fscfui
);
2043 HELPER_SPE_VECTOR_CONV(fscfuf
);
2045 HELPER_SPE_VECTOR_CONV(fscfsf
);
2047 HELPER_SPE_VECTOR_CONV(fsctsi
);
2049 HELPER_SPE_VECTOR_CONV(fsctui
);
2051 HELPER_SPE_VECTOR_CONV(fsctsiz
);
2053 HELPER_SPE_VECTOR_CONV(fsctuiz
);
2055 HELPER_SPE_VECTOR_CONV(fsctsf
);
2057 HELPER_SPE_VECTOR_CONV(fsctuf
);
2059 /* Single-precision floating-point arithmetic */
2060 static always_inline
uint32_t efsadd (uint32_t op1
, uint32_t op2
)
2065 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->spe_status
);
2069 static always_inline
uint32_t efssub (uint32_t op1
, uint32_t op2
)
2074 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->spe_status
);
2078 static always_inline
uint32_t efsmul (uint32_t op1
, uint32_t op2
)
2083 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->spe_status
);
2087 static always_inline
uint32_t efsdiv (uint32_t op1
, uint32_t op2
)
2092 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->spe_status
);
2096 #define HELPER_SPE_SINGLE_ARITH(name) \
2097 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2099 return e##name(op1, op2); \
2102 HELPER_SPE_SINGLE_ARITH(fsadd
);
2104 HELPER_SPE_SINGLE_ARITH(fssub
);
2106 HELPER_SPE_SINGLE_ARITH(fsmul
);
2108 HELPER_SPE_SINGLE_ARITH(fsdiv
);
2110 #define HELPER_SPE_VECTOR_ARITH(name) \
2111 uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
2113 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
2114 (uint64_t)e##name(op1, op2); \
2117 HELPER_SPE_VECTOR_ARITH(fsadd
);
2119 HELPER_SPE_VECTOR_ARITH(fssub
);
2121 HELPER_SPE_VECTOR_ARITH(fsmul
);
2123 HELPER_SPE_VECTOR_ARITH(fsdiv
);
2125 /* Single-precision floating-point comparisons */
2126 static always_inline
uint32_t efststlt (uint32_t op1
, uint32_t op2
)
2131 return float32_lt(u1
.f
, u2
.f
, &env
->spe_status
) ? 4 : 0;
2134 static always_inline
uint32_t efststgt (uint32_t op1
, uint32_t op2
)
2139 return float32_le(u1
.f
, u2
.f
, &env
->spe_status
) ? 0 : 4;
2142 static always_inline
uint32_t efststeq (uint32_t op1
, uint32_t op2
)
2147 return float32_eq(u1
.f
, u2
.f
, &env
->spe_status
) ? 4 : 0;
2150 static always_inline
uint32_t efscmplt (uint32_t op1
, uint32_t op2
)
2152 /* XXX: TODO: test special values (NaN, infinites, ...) */
2153 return efststlt(op1
, op2
);
2156 static always_inline
uint32_t efscmpgt (uint32_t op1
, uint32_t op2
)
2158 /* XXX: TODO: test special values (NaN, infinites, ...) */
2159 return efststgt(op1
, op2
);
2162 static always_inline
uint32_t efscmpeq (uint32_t op1
, uint32_t op2
)
2164 /* XXX: TODO: test special values (NaN, infinites, ...) */
2165 return efststeq(op1
, op2
);
2168 #define HELPER_SINGLE_SPE_CMP(name) \
2169 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2171 return e##name(op1, op2) << 2; \
2174 HELPER_SINGLE_SPE_CMP(fststlt
);
2176 HELPER_SINGLE_SPE_CMP(fststgt
);
2178 HELPER_SINGLE_SPE_CMP(fststeq
);
2180 HELPER_SINGLE_SPE_CMP(fscmplt
);
2182 HELPER_SINGLE_SPE_CMP(fscmpgt
);
2184 HELPER_SINGLE_SPE_CMP(fscmpeq
);
2186 static always_inline
uint32_t evcmp_merge (int t0
, int t1
)
2188 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
2191 #define HELPER_VECTOR_SPE_CMP(name) \
2192 uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
2194 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
2197 HELPER_VECTOR_SPE_CMP(fststlt
);
2199 HELPER_VECTOR_SPE_CMP(fststgt
);
2201 HELPER_VECTOR_SPE_CMP(fststeq
);
2203 HELPER_VECTOR_SPE_CMP(fscmplt
);
2205 HELPER_VECTOR_SPE_CMP(fscmpgt
);
2207 HELPER_VECTOR_SPE_CMP(fscmpeq
);
2209 /* Double-precision floating-point conversion */
2210 uint64_t helper_efdcfsi (uint32_t val
)
2214 u
.d
= int32_to_float64(val
, &env
->spe_status
);
2219 uint64_t helper_efdcfsid (uint64_t val
)
2223 u
.d
= int64_to_float64(val
, &env
->spe_status
);
2228 uint64_t helper_efdcfui (uint32_t val
)
2232 u
.d
= uint32_to_float64(val
, &env
->spe_status
);
2237 uint64_t helper_efdcfuid (uint64_t val
)
2241 u
.d
= uint64_to_float64(val
, &env
->spe_status
);
2246 uint32_t helper_efdctsi (uint64_t val
)
2251 /* NaN are not treated the same way IEEE 754 does */
2252 if (unlikely(isnan(u
.d
)))
2255 return float64_to_int32(u
.d
, &env
->spe_status
);
2258 uint32_t helper_efdctui (uint64_t val
)
2263 /* NaN are not treated the same way IEEE 754 does */
2264 if (unlikely(isnan(u
.d
)))
2267 return float64_to_uint32(u
.d
, &env
->spe_status
);
2270 uint32_t helper_efdctsiz (uint64_t val
)
2275 /* NaN are not treated the same way IEEE 754 does */
2276 if (unlikely(isnan(u
.d
)))
2279 return float64_to_int32_round_to_zero(u
.d
, &env
->spe_status
);
2282 uint64_t helper_efdctsidz (uint64_t val
)
2287 /* NaN are not treated the same way IEEE 754 does */
2288 if (unlikely(isnan(u
.d
)))
2291 return float64_to_int64_round_to_zero(u
.d
, &env
->spe_status
);
2294 uint32_t helper_efdctuiz (uint64_t val
)
2299 /* NaN are not treated the same way IEEE 754 does */
2300 if (unlikely(isnan(u
.d
)))
2303 return float64_to_uint32_round_to_zero(u
.d
, &env
->spe_status
);
2306 uint64_t helper_efdctuidz (uint64_t val
)
2311 /* NaN are not treated the same way IEEE 754 does */
2312 if (unlikely(isnan(u
.d
)))
2315 return float64_to_uint64_round_to_zero(u
.d
, &env
->spe_status
);
2318 uint64_t helper_efdcfsf (uint32_t val
)
2323 u
.d
= int32_to_float64(val
, &env
->spe_status
);
2324 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2325 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2330 uint64_t helper_efdcfuf (uint32_t val
)
2335 u
.d
= uint32_to_float64(val
, &env
->spe_status
);
2336 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2337 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2342 uint32_t helper_efdctsf (uint64_t val
)
2348 /* NaN are not treated the same way IEEE 754 does */
2349 if (unlikely(isnan(u
.d
)))
2351 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2352 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2354 return float64_to_int32(u
.d
, &env
->spe_status
);
2357 uint32_t helper_efdctuf (uint64_t val
)
2363 /* NaN are not treated the same way IEEE 754 does */
2364 if (unlikely(isnan(u
.d
)))
2366 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2367 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2369 return float64_to_uint32(u
.d
, &env
->spe_status
);
2372 uint32_t helper_efscfd (uint64_t val
)
2378 u2
.f
= float64_to_float32(u1
.d
, &env
->spe_status
);
2383 uint64_t helper_efdcfs (uint32_t val
)
2389 u2
.d
= float32_to_float64(u1
.f
, &env
->spe_status
);
2394 /* Double precision fixed-point arithmetic */
2395 uint64_t helper_efdadd (uint64_t op1
, uint64_t op2
)
2400 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->spe_status
);
2404 uint64_t helper_efdsub (uint64_t op1
, uint64_t op2
)
2409 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->spe_status
);
2413 uint64_t helper_efdmul (uint64_t op1
, uint64_t op2
)
2418 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->spe_status
);
2422 uint64_t helper_efddiv (uint64_t op1
, uint64_t op2
)
2427 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->spe_status
);
2431 /* Double precision floating point helpers */
2432 uint32_t helper_efdtstlt (uint64_t op1
, uint64_t op2
)
2437 return float64_lt(u1
.d
, u2
.d
, &env
->spe_status
) ? 4 : 0;
2440 uint32_t helper_efdtstgt (uint64_t op1
, uint64_t op2
)
2445 return float64_le(u1
.d
, u2
.d
, &env
->spe_status
) ? 0 : 4;
2448 uint32_t helper_efdtsteq (uint64_t op1
, uint64_t op2
)
2453 return float64_eq(u1
.d
, u2
.d
, &env
->spe_status
) ? 4 : 0;
2456 uint32_t helper_efdcmplt (uint64_t op1
, uint64_t op2
)
2458 /* XXX: TODO: test special values (NaN, infinites, ...) */
2459 return helper_efdtstlt(op1
, op2
);
2462 uint32_t helper_efdcmpgt (uint64_t op1
, uint64_t op2
)
2464 /* XXX: TODO: test special values (NaN, infinites, ...) */
2465 return helper_efdtstgt(op1
, op2
);
2468 uint32_t helper_efdcmpeq (uint64_t op1
, uint64_t op2
)
2470 /* XXX: TODO: test special values (NaN, infinites, ...) */
2471 return helper_efdtsteq(op1
, op2
);
2474 /*****************************************************************************/
2475 /* Softmmu support */
2476 #if !defined (CONFIG_USER_ONLY)
2478 #define MMUSUFFIX _mmu
2481 #include "softmmu_template.h"
2484 #include "softmmu_template.h"
2487 #include "softmmu_template.h"
2490 #include "softmmu_template.h"
2492 /* try to fill the TLB and return an exception if error. If retaddr is
2493 NULL, it means that the function was called in C code (i.e. not
2494 from generated code or from helper.c) */
2495 /* XXX: fix it to restore all registers */
2496 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
2498 TranslationBlock
*tb
;
2499 CPUState
*saved_env
;
2503 /* XXX: hack to restore env in all cases, even if not called from
2506 env
= cpu_single_env
;
2507 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
2508 if (unlikely(ret
!= 0)) {
2509 if (likely(retaddr
)) {
2510 /* now we have a real cpu fault */
2511 pc
= (unsigned long)retaddr
;
2512 tb
= tb_find_pc(pc
);
2514 /* the PC is inside the translated code. It means that we have
2515 a virtual CPU fault */
2516 cpu_restore_state(tb
, env
, pc
, NULL
);
2519 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
2524 /* Software driven TLBs management */
2525 /* PowerPC 602/603 software TLB load instructions helpers */
2526 static void helper_load_6xx_tlb (target_ulong new_EPN
, int is_code
)
2528 target_ulong RPN
, CMP
, EPN
;
2531 RPN
= env
->spr
[SPR_RPA
];
2533 CMP
= env
->spr
[SPR_ICMP
];
2534 EPN
= env
->spr
[SPR_IMISS
];
2536 CMP
= env
->spr
[SPR_DCMP
];
2537 EPN
= env
->spr
[SPR_DMISS
];
2539 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2540 #if defined (DEBUG_SOFTWARE_TLB)
2541 if (loglevel
!= 0) {
2542 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2543 " PTE1 " ADDRX
" way %d\n",
2544 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2547 /* Store this TLB */
2548 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2549 way
, is_code
, CMP
, RPN
);
2552 void helper_load_6xx_tlbd (target_ulong EPN
)
2554 helper_load_6xx_tlb(EPN
, 0);
2557 void helper_load_6xx_tlbi (target_ulong EPN
)
2559 helper_load_6xx_tlb(EPN
, 1);
2562 /* PowerPC 74xx software TLB load instructions helpers */
2563 static void helper_load_74xx_tlb (target_ulong new_EPN
, int is_code
)
2565 target_ulong RPN
, CMP
, EPN
;
2568 RPN
= env
->spr
[SPR_PTELO
];
2569 CMP
= env
->spr
[SPR_PTEHI
];
2570 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2571 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2572 #if defined (DEBUG_SOFTWARE_TLB)
2573 if (loglevel
!= 0) {
2574 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2575 " PTE1 " ADDRX
" way %d\n",
2576 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2579 /* Store this TLB */
2580 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2581 way
, is_code
, CMP
, RPN
);
2584 void helper_load_74xx_tlbd (target_ulong EPN
)
2586 helper_load_74xx_tlb(EPN
, 0);
2589 void helper_load_74xx_tlbi (target_ulong EPN
)
2591 helper_load_74xx_tlb(EPN
, 1);
2594 static always_inline target_ulong
booke_tlb_to_page_size (int size
)
2596 return 1024 << (2 * size
);
2599 static always_inline
int booke_page_size_to_tlb (target_ulong page_size
)
2603 switch (page_size
) {
2637 #if defined (TARGET_PPC64)
2638 case 0x000100000000ULL
:
2641 case 0x000400000000ULL
:
2644 case 0x001000000000ULL
:
2647 case 0x004000000000ULL
:
2650 case 0x010000000000ULL
:
2662 /* Helpers for 4xx TLB management */
2663 void do_4xx_tlbre_lo (void)
2669 tlb
= &env
->tlb
[T0
].tlbe
;
2671 if (tlb
->prot
& PAGE_VALID
)
2673 size
= booke_page_size_to_tlb(tlb
->size
);
2674 if (size
< 0 || size
> 0x7)
2677 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2680 void do_4xx_tlbre_hi (void)
2685 tlb
= &env
->tlb
[T0
].tlbe
;
2687 if (tlb
->prot
& PAGE_EXEC
)
2689 if (tlb
->prot
& PAGE_WRITE
)
2693 void do_4xx_tlbwe_hi (void)
2696 target_ulong page
, end
;
2698 #if defined (DEBUG_SOFTWARE_TLB)
2699 if (loglevel
!= 0) {
2700 fprintf(logfile
, "%s T0 " TDX
" T1 " TDX
"\n", __func__
, T0
, T1
);
2704 tlb
= &env
->tlb
[T0
].tlbe
;
2705 /* Invalidate previous TLB (if it's valid) */
2706 if (tlb
->prot
& PAGE_VALID
) {
2707 end
= tlb
->EPN
+ tlb
->size
;
2708 #if defined (DEBUG_SOFTWARE_TLB)
2709 if (loglevel
!= 0) {
2710 fprintf(logfile
, "%s: invalidate old TLB %d start " ADDRX
2711 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2714 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2715 tlb_flush_page(env
, page
);
2717 tlb
->size
= booke_tlb_to_page_size((T1
>> 7) & 0x7);
2718 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2719 * If this ever occurs, one should use the ppcemb target instead
2720 * of the ppc or ppc64 one
2722 if ((T1
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
2723 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
2724 "are not supported (%d)\n",
2725 tlb
->size
, TARGET_PAGE_SIZE
, (int)((T1
>> 7) & 0x7));
2727 tlb
->EPN
= T1
& ~(tlb
->size
- 1);
2729 tlb
->prot
|= PAGE_VALID
;
2731 tlb
->prot
&= ~PAGE_VALID
;
2733 /* XXX: TO BE FIXED */
2734 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
2736 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2737 tlb
->attr
= T1
& 0xFF;
2738 #if defined (DEBUG_SOFTWARE_TLB)
2739 if (loglevel
!= 0) {
2740 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2741 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2742 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2743 tlb
->prot
& PAGE_READ
? 'r' : '-',
2744 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2745 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2746 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2749 /* Invalidate new TLB (if valid) */
2750 if (tlb
->prot
& PAGE_VALID
) {
2751 end
= tlb
->EPN
+ tlb
->size
;
2752 #if defined (DEBUG_SOFTWARE_TLB)
2753 if (loglevel
!= 0) {
2754 fprintf(logfile
, "%s: invalidate TLB %d start " ADDRX
2755 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2758 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2759 tlb_flush_page(env
, page
);
2763 void do_4xx_tlbwe_lo (void)
2767 #if defined (DEBUG_SOFTWARE_TLB)
2768 if (loglevel
!= 0) {
2769 fprintf(logfile
, "%s T0 " TDX
" T1 " TDX
"\n", __func__
, T0
, T1
);
2773 tlb
= &env
->tlb
[T0
].tlbe
;
2774 tlb
->RPN
= T1
& 0xFFFFFC00;
2775 tlb
->prot
= PAGE_READ
;
2777 tlb
->prot
|= PAGE_EXEC
;
2779 tlb
->prot
|= PAGE_WRITE
;
2780 #if defined (DEBUG_SOFTWARE_TLB)
2781 if (loglevel
!= 0) {
2782 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2783 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2784 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2785 tlb
->prot
& PAGE_READ
? 'r' : '-',
2786 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2787 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2788 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2793 /* PowerPC 440 TLB management */
2794 void do_440_tlbwe (int word
)
2797 target_ulong EPN
, RPN
, size
;
2800 #if defined (DEBUG_SOFTWARE_TLB)
2801 if (loglevel
!= 0) {
2802 fprintf(logfile
, "%s word %d T0 " TDX
" T1 " TDX
"\n",
2803 __func__
, word
, T0
, T1
);
2808 tlb
= &env
->tlb
[T0
].tlbe
;
2811 /* Just here to please gcc */
2813 EPN
= T1
& 0xFFFFFC00;
2814 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
2817 size
= booke_tlb_to_page_size((T1
>> 4) & 0xF);
2818 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
2822 tlb
->attr
|= (T1
>> 8) & 1;
2824 tlb
->prot
|= PAGE_VALID
;
2826 if (tlb
->prot
& PAGE_VALID
) {
2827 tlb
->prot
&= ~PAGE_VALID
;
2831 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
2836 RPN
= T1
& 0xFFFFFC0F;
2837 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
2842 tlb
->attr
= (tlb
->attr
& 0x1) | (T1
& 0x0000FF00);
2843 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
2845 tlb
->prot
|= PAGE_READ
<< 4;
2847 tlb
->prot
|= PAGE_WRITE
<< 4;
2849 tlb
->prot
|= PAGE_EXEC
<< 4;
2851 tlb
->prot
|= PAGE_READ
;
2853 tlb
->prot
|= PAGE_WRITE
;
2855 tlb
->prot
|= PAGE_EXEC
;
2860 void do_440_tlbre (int word
)
2866 tlb
= &env
->tlb
[T0
].tlbe
;
2869 /* Just here to please gcc */
2872 size
= booke_page_size_to_tlb(tlb
->size
);
2873 if (size
< 0 || size
> 0xF)
2876 if (tlb
->attr
& 0x1)
2878 if (tlb
->prot
& PAGE_VALID
)
2880 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
2881 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
2887 T0
= tlb
->attr
& ~0x1;
2888 if (tlb
->prot
& (PAGE_READ
<< 4))
2890 if (tlb
->prot
& (PAGE_WRITE
<< 4))
2892 if (tlb
->prot
& (PAGE_EXEC
<< 4))
2894 if (tlb
->prot
& PAGE_READ
)
2896 if (tlb
->prot
& PAGE_WRITE
)
2898 if (tlb
->prot
& PAGE_EXEC
)
2903 #endif /* !CONFIG_USER_ONLY */