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"
27 //#define DEBUG_EXCEPTIONS
28 //#define DEBUG_SOFTWARE_TLB
30 /*****************************************************************************/
31 /* Exceptions processing helpers */
33 void helper_raise_exception_err (uint32_t exception
, uint32_t error_code
)
35 raise_exception_err(env
, exception
, error_code
);
38 void helper_raise_debug (void)
40 raise_exception(env
, EXCP_DEBUG
);
43 /*****************************************************************************/
44 /* Registers load and stores */
45 target_ulong
helper_load_cr (void)
47 return (env
->crf
[0] << 28) |
57 void helper_store_cr (target_ulong val
, uint32_t mask
)
61 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
63 env
->crf
[i
] = (val
>> (sh
* 4)) & 0xFUL
;
67 /*****************************************************************************/
69 void helper_load_dump_spr (uint32_t sprn
)
72 fprintf(logfile
, "Read SPR %d %03x => " ADDRX
"\n",
73 sprn
, sprn
, env
->spr
[sprn
]);
77 void helper_store_dump_spr (uint32_t sprn
)
80 fprintf(logfile
, "Write SPR %d %03x <= " ADDRX
"\n",
81 sprn
, sprn
, env
->spr
[sprn
]);
85 target_ulong
helper_load_tbl (void)
87 return cpu_ppc_load_tbl(env
);
90 target_ulong
helper_load_tbu (void)
92 return cpu_ppc_load_tbu(env
);
95 target_ulong
helper_load_atbl (void)
97 return cpu_ppc_load_atbl(env
);
100 target_ulong
helper_load_atbu (void)
102 return cpu_ppc_load_atbu(env
);
105 target_ulong
helper_load_601_rtcl (void)
107 return cpu_ppc601_load_rtcl(env
);
110 target_ulong
helper_load_601_rtcu (void)
112 return cpu_ppc601_load_rtcu(env
);
115 #if !defined(CONFIG_USER_ONLY)
116 #if defined (TARGET_PPC64)
117 void helper_store_asr (target_ulong val
)
119 ppc_store_asr(env
, val
);
123 void helper_store_sdr1 (target_ulong val
)
125 ppc_store_sdr1(env
, val
);
128 void helper_store_tbl (target_ulong val
)
130 cpu_ppc_store_tbl(env
, val
);
133 void helper_store_tbu (target_ulong val
)
135 cpu_ppc_store_tbu(env
, val
);
138 void helper_store_atbl (target_ulong val
)
140 cpu_ppc_store_atbl(env
, val
);
143 void helper_store_atbu (target_ulong val
)
145 cpu_ppc_store_atbu(env
, val
);
148 void helper_store_601_rtcl (target_ulong val
)
150 cpu_ppc601_store_rtcl(env
, val
);
153 void helper_store_601_rtcu (target_ulong val
)
155 cpu_ppc601_store_rtcu(env
, val
);
158 target_ulong
helper_load_decr (void)
160 return cpu_ppc_load_decr(env
);
163 void helper_store_decr (target_ulong val
)
165 cpu_ppc_store_decr(env
, val
);
168 void helper_store_hid0_601 (target_ulong val
)
172 hid0
= env
->spr
[SPR_HID0
];
173 if ((val
^ hid0
) & 0x00000008) {
174 /* Change current endianness */
175 env
->hflags
&= ~(1 << MSR_LE
);
176 env
->hflags_nmsr
&= ~(1 << MSR_LE
);
177 env
->hflags_nmsr
|= (1 << MSR_LE
) & (((val
>> 3) & 1) << MSR_LE
);
178 env
->hflags
|= env
->hflags_nmsr
;
180 fprintf(logfile
, "%s: set endianness to %c => " ADDRX
"\n",
181 __func__
, val
& 0x8 ? 'l' : 'b', env
->hflags
);
184 env
->spr
[SPR_HID0
] = (uint32_t)val
;
187 void helper_store_403_pbr (uint32_t num
, target_ulong value
)
189 if (likely(env
->pb
[num
] != value
)) {
190 env
->pb
[num
] = value
;
191 /* Should be optimized */
196 target_ulong
helper_load_40x_pit (void)
198 return load_40x_pit(env
);
201 void helper_store_40x_pit (target_ulong val
)
203 store_40x_pit(env
, val
);
206 void helper_store_40x_dbcr0 (target_ulong val
)
208 store_40x_dbcr0(env
, val
);
211 void helper_store_40x_sler (target_ulong val
)
213 store_40x_sler(env
, val
);
216 void helper_store_booke_tcr (target_ulong val
)
218 store_booke_tcr(env
, val
);
221 void helper_store_booke_tsr (target_ulong val
)
223 store_booke_tsr(env
, val
);
226 void helper_store_ibatu (uint32_t nr
, target_ulong val
)
228 ppc_store_ibatu(env
, nr
, val
);
231 void helper_store_ibatl (uint32_t nr
, target_ulong val
)
233 ppc_store_ibatl(env
, nr
, val
);
236 void helper_store_dbatu (uint32_t nr
, target_ulong val
)
238 ppc_store_dbatu(env
, nr
, val
);
241 void helper_store_dbatl (uint32_t nr
, target_ulong val
)
243 ppc_store_dbatl(env
, nr
, val
);
246 void helper_store_601_batl (uint32_t nr
, target_ulong val
)
248 ppc_store_ibatl_601(env
, nr
, val
);
251 void helper_store_601_batu (uint32_t nr
, target_ulong val
)
253 ppc_store_ibatu_601(env
, nr
, val
);
257 /*****************************************************************************/
258 /* Memory load and stores */
260 static always_inline target_ulong
addr_add(target_ulong addr
, target_long arg
)
262 #if defined(TARGET_PPC64)
264 return (uint32_t)(addr
+ arg
);
270 void helper_lmw (target_ulong addr
, uint32_t reg
)
272 for (; reg
< 32; reg
++) {
274 env
->gpr
[reg
] = bswap32(ldl(addr
));
276 env
->gpr
[reg
] = ldl(addr
);
277 addr
= addr_add(addr
, 4);
281 void helper_stmw (target_ulong addr
, uint32_t reg
)
283 for (; reg
< 32; reg
++) {
285 stl(addr
, bswap32((uint32_t)env
->gpr
[reg
]));
287 stl(addr
, (uint32_t)env
->gpr
[reg
]);
288 addr
= addr_add(addr
, 4);
292 void helper_lsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
295 for (; nb
> 3; nb
-= 4) {
296 env
->gpr
[reg
] = ldl(addr
);
297 reg
= (reg
+ 1) % 32;
298 addr
= addr_add(addr
, 4);
300 if (unlikely(nb
> 0)) {
302 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
303 env
->gpr
[reg
] |= ldub(addr
) << sh
;
304 addr
= addr_add(addr
, 1);
308 /* PPC32 specification says we must generate an exception if
309 * rA is in the range of registers to be loaded.
310 * In an other hand, IBM says this is valid, but rA won't be loaded.
311 * For now, I'll follow the spec...
313 void helper_lswx(target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
315 if (likely(xer_bc
!= 0)) {
316 if (unlikely((ra
!= 0 && reg
< ra
&& (reg
+ xer_bc
) > ra
) ||
317 (reg
< rb
&& (reg
+ xer_bc
) > rb
))) {
318 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
320 POWERPC_EXCP_INVAL_LSWX
);
322 helper_lsw(addr
, xer_bc
, reg
);
327 void helper_stsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
330 for (; nb
> 3; nb
-= 4) {
331 stl(addr
, env
->gpr
[reg
]);
332 reg
= (reg
+ 1) % 32;
333 addr
= addr_add(addr
, 4);
335 if (unlikely(nb
> 0)) {
336 for (sh
= 24; nb
> 0; nb
--, sh
-= 8)
337 stb(addr
, (env
->gpr
[reg
] >> sh
) & 0xFF);
338 addr
= addr_add(addr
, 1);
342 static void do_dcbz(target_ulong addr
, int dcache_line_size
)
344 addr
&= ~(dcache_line_size
- 1);
346 for (i
= 0 ; i
< dcache_line_size
; i
+= 4) {
349 if (env
->reserve
== addr
)
350 env
->reserve
= (target_ulong
)-1ULL;
353 void helper_dcbz(target_ulong addr
)
355 do_dcbz(addr
, env
->dcache_line_size
);
358 void helper_dcbz_970(target_ulong addr
)
360 if (((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1)
363 do_dcbz(addr
, env
->dcache_line_size
);
366 void helper_icbi(target_ulong addr
)
370 addr
&= ~(env
->dcache_line_size
- 1);
371 /* Invalidate one cache line :
372 * PowerPC specification says this is to be treated like a load
373 * (not a fetch) by the MMU. To be sure it will be so,
374 * do the load "by hand".
377 tb_invalidate_page_range(addr
, addr
+ env
->icache_line_size
);
381 target_ulong
helper_lscbx (target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
385 for (i
= 0; i
< xer_bc
; i
++) {
387 addr
= addr_add(addr
, 1);
388 /* ra (if not 0) and rb are never modified */
389 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
390 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
392 if (unlikely(c
== xer_cmp
))
394 if (likely(d
!= 0)) {
405 /*****************************************************************************/
406 /* Fixed point operations helpers */
407 #if defined(TARGET_PPC64)
409 /* multiply high word */
410 uint64_t helper_mulhd (uint64_t arg1
, uint64_t arg2
)
414 muls64(&tl
, &th
, arg1
, arg2
);
418 /* multiply high word unsigned */
419 uint64_t helper_mulhdu (uint64_t arg1
, uint64_t arg2
)
423 mulu64(&tl
, &th
, arg1
, arg2
);
427 uint64_t helper_mulldo (uint64_t arg1
, uint64_t arg2
)
432 muls64(&tl
, (uint64_t *)&th
, arg1
, arg2
);
433 /* If th != 0 && th != -1, then we had an overflow */
434 if (likely((uint64_t)(th
+ 1) <= 1)) {
435 env
->xer
&= ~(1 << XER_OV
);
437 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
443 target_ulong
helper_cntlzw (target_ulong t
)
448 #if defined(TARGET_PPC64)
449 target_ulong
helper_cntlzd (target_ulong t
)
455 /* shift right arithmetic helper */
456 target_ulong
helper_sraw (target_ulong value
, target_ulong shift
)
460 if (likely(!(shift
& 0x20))) {
461 if (likely((uint32_t)shift
!= 0)) {
463 ret
= (int32_t)value
>> shift
;
464 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
465 env
->xer
&= ~(1 << XER_CA
);
467 env
->xer
|= (1 << XER_CA
);
470 ret
= (int32_t)value
;
471 env
->xer
&= ~(1 << XER_CA
);
474 ret
= (int32_t)value
>> 31;
476 env
->xer
|= (1 << XER_CA
);
478 env
->xer
&= ~(1 << XER_CA
);
481 return (target_long
)ret
;
484 #if defined(TARGET_PPC64)
485 target_ulong
helper_srad (target_ulong value
, target_ulong shift
)
489 if (likely(!(shift
& 0x40))) {
490 if (likely((uint64_t)shift
!= 0)) {
492 ret
= (int64_t)value
>> shift
;
493 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
494 env
->xer
&= ~(1 << XER_CA
);
496 env
->xer
|= (1 << XER_CA
);
499 ret
= (int64_t)value
;
500 env
->xer
&= ~(1 << XER_CA
);
503 ret
= (int64_t)value
>> 63;
505 env
->xer
|= (1 << XER_CA
);
507 env
->xer
&= ~(1 << XER_CA
);
514 target_ulong
helper_popcntb (target_ulong val
)
516 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
517 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
518 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
522 #if defined(TARGET_PPC64)
523 target_ulong
helper_popcntb_64 (target_ulong val
)
525 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
526 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
527 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
532 /*****************************************************************************/
533 /* Floating point operations helpers */
534 uint64_t helper_float32_to_float64(uint32_t arg
)
539 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
543 uint32_t helper_float64_to_float32(uint64_t arg
)
548 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
552 static always_inline
int fpisneg (float64 d
)
558 return u
.ll
>> 63 != 0;
561 static always_inline
int isden (float64 d
)
567 return ((u
.ll
>> 52) & 0x7FF) == 0;
570 static always_inline
int iszero (float64 d
)
576 return (u
.ll
& ~0x8000000000000000ULL
) == 0;
579 static always_inline
int isinfinity (float64 d
)
585 return ((u
.ll
>> 52) & 0x7FF) == 0x7FF &&
586 (u
.ll
& 0x000FFFFFFFFFFFFFULL
) == 0;
589 #ifdef CONFIG_SOFTFLOAT
590 static always_inline
int isfinite (float64 d
)
596 return (((u
.ll
>> 52) & 0x7FF) != 0x7FF);
599 static always_inline
int isnormal (float64 d
)
605 uint32_t exp
= (u
.ll
>> 52) & 0x7FF;
606 return ((0 < exp
) && (exp
< 0x7FF));
610 uint32_t helper_compute_fprf (uint64_t arg
, uint32_t set_fprf
)
616 isneg
= fpisneg(farg
.d
);
617 if (unlikely(float64_is_nan(farg
.d
))) {
618 if (float64_is_signaling_nan(farg
.d
)) {
619 /* Signaling NaN: flags are undefined */
625 } else if (unlikely(isinfinity(farg
.d
))) {
632 if (iszero(farg
.d
)) {
640 /* Denormalized numbers */
643 /* Normalized numbers */
654 /* We update FPSCR_FPRF */
655 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
656 env
->fpscr
|= ret
<< FPSCR_FPRF
;
658 /* We just need fpcc to update Rc1 */
662 /* Floating-point invalid operations exception */
663 static always_inline
uint64_t fload_invalid_op_excp (int op
)
669 if (op
& POWERPC_EXCP_FP_VXSNAN
) {
670 /* Operation on signaling NaN */
671 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
673 if (op
& POWERPC_EXCP_FP_VXSOFT
) {
674 /* Software-defined condition */
675 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
677 switch (op
& ~(POWERPC_EXCP_FP_VXSOFT
| POWERPC_EXCP_FP_VXSNAN
)) {
678 case POWERPC_EXCP_FP_VXISI
:
679 /* Magnitude subtraction of infinities */
680 env
->fpscr
|= 1 << FPSCR_VXISI
;
682 case POWERPC_EXCP_FP_VXIDI
:
683 /* Division of infinity by infinity */
684 env
->fpscr
|= 1 << FPSCR_VXIDI
;
686 case POWERPC_EXCP_FP_VXZDZ
:
687 /* Division of zero by zero */
688 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
690 case POWERPC_EXCP_FP_VXIMZ
:
691 /* Multiplication of zero by infinity */
692 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
694 case POWERPC_EXCP_FP_VXVC
:
695 /* Ordered comparison of NaN */
696 env
->fpscr
|= 1 << FPSCR_VXVC
;
697 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
698 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
699 /* We must update the target FPR before raising the exception */
701 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
702 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
703 /* Update the floating-point enabled exception summary */
704 env
->fpscr
|= 1 << FPSCR_FEX
;
705 /* Exception is differed */
709 case POWERPC_EXCP_FP_VXSQRT
:
710 /* Square root of a negative number */
711 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
713 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
715 /* Set the result to quiet NaN */
717 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
718 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
721 case POWERPC_EXCP_FP_VXCVI
:
722 /* Invalid conversion */
723 env
->fpscr
|= 1 << FPSCR_VXCVI
;
724 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
726 /* Set the result to quiet NaN */
728 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
729 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
733 /* Update the floating-point invalid operation summary */
734 env
->fpscr
|= 1 << FPSCR_VX
;
735 /* Update the floating-point exception summary */
736 env
->fpscr
|= 1 << FPSCR_FX
;
738 /* Update the floating-point enabled exception summary */
739 env
->fpscr
|= 1 << FPSCR_FEX
;
740 if (msr_fe0
!= 0 || msr_fe1
!= 0)
741 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_FP
| op
);
746 static always_inline
uint64_t float_zero_divide_excp (uint64_t arg1
, uint64_t arg2
)
748 env
->fpscr
|= 1 << FPSCR_ZX
;
749 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
750 /* Update the floating-point exception summary */
751 env
->fpscr
|= 1 << FPSCR_FX
;
753 /* Update the floating-point enabled exception summary */
754 env
->fpscr
|= 1 << FPSCR_FEX
;
755 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
756 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
757 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
760 /* Set the result to infinity */
761 arg1
= ((arg1
^ arg2
) & 0x8000000000000000ULL
);
762 arg1
|= 0x7FFULL
<< 52;
767 static always_inline
void float_overflow_excp (void)
769 env
->fpscr
|= 1 << FPSCR_OX
;
770 /* Update the floating-point exception summary */
771 env
->fpscr
|= 1 << FPSCR_FX
;
773 /* XXX: should adjust the result */
774 /* Update the floating-point enabled exception summary */
775 env
->fpscr
|= 1 << FPSCR_FEX
;
776 /* We must update the target FPR before raising the exception */
777 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
778 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
780 env
->fpscr
|= 1 << FPSCR_XX
;
781 env
->fpscr
|= 1 << FPSCR_FI
;
785 static always_inline
void float_underflow_excp (void)
787 env
->fpscr
|= 1 << FPSCR_UX
;
788 /* Update the floating-point exception summary */
789 env
->fpscr
|= 1 << FPSCR_FX
;
791 /* XXX: should adjust the result */
792 /* Update the floating-point enabled exception summary */
793 env
->fpscr
|= 1 << FPSCR_FEX
;
794 /* We must update the target FPR before raising the exception */
795 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
796 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
800 static always_inline
void float_inexact_excp (void)
802 env
->fpscr
|= 1 << FPSCR_XX
;
803 /* Update the floating-point exception summary */
804 env
->fpscr
|= 1 << FPSCR_FX
;
806 /* Update the floating-point enabled exception summary */
807 env
->fpscr
|= 1 << FPSCR_FEX
;
808 /* We must update the target FPR before raising the exception */
809 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
810 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
814 static always_inline
void fpscr_set_rounding_mode (void)
818 /* Set rounding mode */
821 /* Best approximation (round to nearest) */
822 rnd_type
= float_round_nearest_even
;
825 /* Smaller magnitude (round toward zero) */
826 rnd_type
= float_round_to_zero
;
829 /* Round toward +infinite */
830 rnd_type
= float_round_up
;
834 /* Round toward -infinite */
835 rnd_type
= float_round_down
;
838 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
841 void helper_fpscr_setbit (uint32_t bit
)
845 prev
= (env
->fpscr
>> bit
) & 1;
846 env
->fpscr
|= 1 << bit
;
850 env
->fpscr
|= 1 << FPSCR_FX
;
854 env
->fpscr
|= 1 << FPSCR_FX
;
859 env
->fpscr
|= 1 << FPSCR_FX
;
864 env
->fpscr
|= 1 << FPSCR_FX
;
869 env
->fpscr
|= 1 << FPSCR_FX
;
882 env
->fpscr
|= 1 << FPSCR_VX
;
883 env
->fpscr
|= 1 << FPSCR_FX
;
890 env
->error_code
= POWERPC_EXCP_FP
;
892 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
894 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
896 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
898 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
900 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
902 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
904 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
906 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
908 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
915 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
922 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
929 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
936 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
942 fpscr_set_rounding_mode();
947 /* Update the floating-point enabled exception summary */
948 env
->fpscr
|= 1 << FPSCR_FEX
;
949 /* We have to update Rc1 before raising the exception */
950 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
956 void helper_store_fpscr (uint64_t arg
, uint32_t mask
)
959 * We use only the 32 LSB of the incoming fpr
967 new |= prev
& 0x90000000;
968 for (i
= 0; i
< 7; i
++) {
969 if (mask
& (1 << i
)) {
970 env
->fpscr
&= ~(0xF << (4 * i
));
971 env
->fpscr
|= new & (0xF << (4 * i
));
974 /* Update VX and FEX */
976 env
->fpscr
|= 1 << FPSCR_VX
;
978 env
->fpscr
&= ~(1 << FPSCR_VX
);
979 if ((fpscr_ex
& fpscr_eex
) != 0) {
980 env
->fpscr
|= 1 << FPSCR_FEX
;
981 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
982 /* XXX: we should compute it properly */
983 env
->error_code
= POWERPC_EXCP_FP
;
986 env
->fpscr
&= ~(1 << FPSCR_FEX
);
987 fpscr_set_rounding_mode();
990 void helper_float_check_status (void)
992 #ifdef CONFIG_SOFTFLOAT
993 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
994 (env
->error_code
& POWERPC_EXCP_FP
)) {
995 /* Differred floating-point exception after target FPR update */
996 if (msr_fe0
!= 0 || msr_fe1
!= 0)
997 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
998 } else if (env
->fp_status
.float_exception_flags
& float_flag_overflow
) {
999 float_overflow_excp();
1000 } else if (env
->fp_status
.float_exception_flags
& float_flag_underflow
) {
1001 float_underflow_excp();
1002 } else if (env
->fp_status
.float_exception_flags
& float_flag_inexact
) {
1003 float_inexact_excp();
1006 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
1007 (env
->error_code
& POWERPC_EXCP_FP
)) {
1008 /* Differred floating-point exception after target FPR update */
1009 if (msr_fe0
!= 0 || msr_fe1
!= 0)
1010 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
1015 #ifdef CONFIG_SOFTFLOAT
1016 void helper_reset_fpstatus (void)
1018 env
->fp_status
.float_exception_flags
= 0;
1023 uint64_t helper_fadd (uint64_t arg1
, uint64_t arg2
)
1025 CPU_DoubleU farg1
, farg2
;
1029 #if USE_PRECISE_EMULATION
1030 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1031 float64_is_signaling_nan(farg2
.d
))) {
1033 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1034 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
1035 fpisneg(farg1
.d
) == fpisneg(farg2
.d
))) {
1036 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
1038 /* Magnitude subtraction of infinities */
1039 farg1
.ll
== fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1042 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
1048 uint64_t helper_fsub (uint64_t arg1
, uint64_t arg2
)
1050 CPU_DoubleU farg1
, farg2
;
1054 #if USE_PRECISE_EMULATION
1056 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1057 float64_is_signaling_nan(farg2
.d
))) {
1058 /* sNaN subtraction */
1059 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1060 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
1061 fpisneg(farg1
.d
) != fpisneg(farg2
.d
))) {
1062 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
1064 /* Magnitude subtraction of infinities */
1065 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1069 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
1075 uint64_t helper_fmul (uint64_t arg1
, uint64_t arg2
)
1077 CPU_DoubleU farg1
, farg2
;
1081 #if USE_PRECISE_EMULATION
1082 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1083 float64_is_signaling_nan(farg2
.d
))) {
1084 /* sNaN multiplication */
1085 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1086 } else if (unlikely((isinfinity(farg1
.d
) && iszero(farg2
.d
)) ||
1087 (iszero(farg1
.d
) && isinfinity(farg2
.d
)))) {
1088 /* Multiplication of zero by infinity */
1089 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1091 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1095 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1101 uint64_t helper_fdiv (uint64_t arg1
, uint64_t arg2
)
1103 CPU_DoubleU farg1
, farg2
;
1107 #if USE_PRECISE_EMULATION
1108 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1109 float64_is_signaling_nan(farg2
.d
))) {
1111 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1112 } else if (unlikely(isinfinity(farg1
.d
) && isinfinity(farg2
.d
))) {
1113 /* Division of infinity by infinity */
1114 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI
);
1115 } else if (unlikely(iszero(farg2
.d
))) {
1116 if (iszero(farg1
.d
)) {
1117 /* Division of zero by zero */
1118 farg1
.ll
fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ
);
1120 /* Division by zero */
1121 farg1
.ll
= float_zero_divide_excp(farg1
.d
, farg2
.d
);
1124 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
1127 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
1133 uint64_t helper_fabs (uint64_t arg
)
1138 farg
.d
= float64_abs(farg
.d
);
1143 uint64_t helper_fnabs (uint64_t arg
)
1148 farg
.d
= float64_abs(farg
.d
);
1149 farg
.d
= float64_chs(farg
.d
);
1154 uint64_t helper_fneg (uint64_t arg
)
1159 farg
.d
= float64_chs(farg
.d
);
1163 /* fctiw - fctiw. */
1164 uint64_t helper_fctiw (uint64_t arg
)
1169 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1170 /* sNaN conversion */
1171 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1172 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1173 /* qNan / infinity conversion */
1174 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1176 farg
.ll
= float64_to_int32(farg
.d
, &env
->fp_status
);
1177 #if USE_PRECISE_EMULATION
1178 /* XXX: higher bits are not supposed to be significant.
1179 * to make tests easier, return the same as a real PowerPC 750
1181 farg
.ll
|= 0xFFF80000ULL
<< 32;
1187 /* fctiwz - fctiwz. */
1188 uint64_t helper_fctiwz (uint64_t arg
)
1193 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1194 /* sNaN conversion */
1195 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1196 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1197 /* qNan / infinity conversion */
1198 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1200 farg
.ll
= float64_to_int32_round_to_zero(farg
.d
, &env
->fp_status
);
1201 #if USE_PRECISE_EMULATION
1202 /* XXX: higher bits are not supposed to be significant.
1203 * to make tests easier, return the same as a real PowerPC 750
1205 farg
.ll
|= 0xFFF80000ULL
<< 32;
1211 #if defined(TARGET_PPC64)
1212 /* fcfid - fcfid. */
1213 uint64_t helper_fcfid (uint64_t arg
)
1216 farg
.d
= int64_to_float64(arg
, &env
->fp_status
);
1220 /* fctid - fctid. */
1221 uint64_t helper_fctid (uint64_t arg
)
1226 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1227 /* sNaN conversion */
1228 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1229 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1230 /* qNan / infinity conversion */
1231 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1233 farg
.ll
= float64_to_int64(farg
.d
, &env
->fp_status
);
1238 /* fctidz - fctidz. */
1239 uint64_t helper_fctidz (uint64_t arg
)
1244 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1245 /* sNaN conversion */
1246 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1247 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1248 /* qNan / infinity conversion */
1249 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1251 farg
.ll
= float64_to_int64_round_to_zero(farg
.d
, &env
->fp_status
);
1258 static always_inline
uint64_t do_fri (uint64_t arg
, int rounding_mode
)
1263 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1265 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1266 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1267 /* qNan / infinity round */
1268 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1270 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
1271 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
1272 /* Restore rounding mode from FPSCR */
1273 fpscr_set_rounding_mode();
1278 uint64_t helper_frin (uint64_t arg
)
1280 return do_fri(arg
, float_round_nearest_even
);
1283 uint64_t helper_friz (uint64_t arg
)
1285 return do_fri(arg
, float_round_to_zero
);
1288 uint64_t helper_frip (uint64_t arg
)
1290 return do_fri(arg
, float_round_up
);
1293 uint64_t helper_frim (uint64_t arg
)
1295 return do_fri(arg
, float_round_down
);
1298 /* fmadd - fmadd. */
1299 uint64_t helper_fmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1301 CPU_DoubleU farg1
, farg2
, farg3
;
1306 #if USE_PRECISE_EMULATION
1307 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1308 float64_is_signaling_nan(farg2
.d
) ||
1309 float64_is_signaling_nan(farg3
.d
))) {
1310 /* sNaN operation */
1311 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1314 /* This is the way the PowerPC specification defines it */
1315 float128 ft0_128
, ft1_128
;
1317 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1318 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1319 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1320 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1321 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1322 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1324 /* This is OK on x86 hosts */
1325 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1329 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1330 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1335 /* fmsub - fmsub. */
1336 uint64_t helper_fmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1338 CPU_DoubleU farg1
, farg2
, farg3
;
1343 #if USE_PRECISE_EMULATION
1344 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1345 float64_is_signaling_nan(farg2
.d
) ||
1346 float64_is_signaling_nan(farg3
.d
))) {
1347 /* sNaN operation */
1348 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1351 /* This is the way the PowerPC specification defines it */
1352 float128 ft0_128
, ft1_128
;
1354 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1355 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1356 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1357 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1358 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1359 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1361 /* This is OK on x86 hosts */
1362 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1366 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1367 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1372 /* fnmadd - fnmadd. */
1373 uint64_t helper_fnmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1375 CPU_DoubleU farg1
, farg2
, farg3
;
1381 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1382 float64_is_signaling_nan(farg2
.d
) ||
1383 float64_is_signaling_nan(farg3
.d
))) {
1384 /* sNaN operation */
1385 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1387 #if USE_PRECISE_EMULATION
1389 /* This is the way the PowerPC specification defines it */
1390 float128 ft0_128
, ft1_128
;
1392 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1393 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1394 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1395 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1396 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1397 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1399 /* This is OK on x86 hosts */
1400 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1403 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1404 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1406 if (likely(!isnan(farg1
.d
)))
1407 farg1
.d
= float64_chs(farg1
.d
);
1412 /* fnmsub - fnmsub. */
1413 uint64_t helper_fnmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1415 CPU_DoubleU farg1
, farg2
, farg3
;
1421 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1422 float64_is_signaling_nan(farg2
.d
) ||
1423 float64_is_signaling_nan(farg3
.d
))) {
1424 /* sNaN operation */
1425 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1427 #if USE_PRECISE_EMULATION
1429 /* This is the way the PowerPC specification defines it */
1430 float128 ft0_128
, ft1_128
;
1432 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1433 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1434 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1435 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1436 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1437 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1439 /* This is OK on x86 hosts */
1440 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1443 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1444 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1446 if (likely(!isnan(farg1
.d
)))
1447 farg1
.d
= float64_chs(farg1
.d
);
1453 uint64_t helper_frsp (uint64_t arg
)
1458 #if USE_PRECISE_EMULATION
1459 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1460 /* sNaN square root */
1461 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1463 fard
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1466 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1471 /* fsqrt - fsqrt. */
1472 uint64_t helper_fsqrt (uint64_t arg
)
1477 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1478 /* sNaN square root */
1479 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1480 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1481 /* Square root of a negative nonzero number */
1482 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1484 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1490 uint64_t helper_fre (uint64_t arg
)
1495 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1496 /* sNaN reciprocal */
1497 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1498 } else if (unlikely(iszero(farg
.d
))) {
1499 /* Zero reciprocal */
1500 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1501 } else if (likely(isnormal(farg
.d
))) {
1502 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1504 if (farg
.ll
== 0x8000000000000000ULL
) {
1505 farg
.ll
= 0xFFF0000000000000ULL
;
1506 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1507 farg
.ll
= 0x7FF0000000000000ULL
;
1508 } else if (isnan(farg
.d
)) {
1509 farg
.ll
= 0x7FF8000000000000ULL
;
1510 } else if (fpisneg(farg
.d
)) {
1511 farg
.ll
= 0x8000000000000000ULL
;
1513 farg
.ll
= 0x0000000000000000ULL
;
1520 uint64_t helper_fres (uint64_t arg
)
1525 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1526 /* sNaN reciprocal */
1527 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1528 } else if (unlikely(iszero(farg
.d
))) {
1529 /* Zero reciprocal */
1530 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1531 } else if (likely(isnormal(farg
.d
))) {
1532 #if USE_PRECISE_EMULATION
1533 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1534 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1536 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1539 if (farg
.ll
== 0x8000000000000000ULL
) {
1540 farg
.ll
= 0xFFF0000000000000ULL
;
1541 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1542 farg
.ll
= 0x7FF0000000000000ULL
;
1543 } else if (isnan(farg
.d
)) {
1544 farg
.ll
= 0x7FF8000000000000ULL
;
1545 } else if (fpisneg(farg
.d
)) {
1546 farg
.ll
= 0x8000000000000000ULL
;
1548 farg
.ll
= 0x0000000000000000ULL
;
1554 /* frsqrte - frsqrte. */
1555 uint64_t helper_frsqrte (uint64_t arg
)
1560 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1561 /* sNaN reciprocal square root */
1562 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1563 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1564 /* Reciprocal square root of a negative nonzero number */
1565 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1566 } else if (likely(isnormal(farg
.d
))) {
1567 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1568 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1570 if (farg
.ll
== 0x8000000000000000ULL
) {
1571 farg
.ll
= 0xFFF0000000000000ULL
;
1572 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1573 farg
.ll
= 0x7FF0000000000000ULL
;
1574 } else if (isnan(farg
.d
)) {
1575 farg
.ll
|= 0x000FFFFFFFFFFFFFULL
;
1576 } else if (fpisneg(farg
.d
)) {
1577 farg
.ll
= 0x7FF8000000000000ULL
;
1579 farg
.ll
= 0x0000000000000000ULL
;
1586 uint64_t helper_fsel (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1588 CPU_DoubleU farg1
, farg2
, farg3
;
1594 if (!fpisneg(farg1
.d
) || iszero(farg1
.d
))
1600 uint32_t helper_fcmpu (uint64_t arg1
, uint64_t arg2
)
1602 CPU_DoubleU farg1
, farg2
;
1607 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1608 float64_is_signaling_nan(farg2
.d
))) {
1609 /* sNaN comparison */
1610 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1612 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1614 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1620 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1621 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1625 uint32_t helper_fcmpo (uint64_t arg1
, uint64_t arg2
)
1627 CPU_DoubleU farg1
, farg2
;
1632 if (unlikely(float64_is_nan(farg1
.d
) ||
1633 float64_is_nan(farg2
.d
))) {
1634 if (float64_is_signaling_nan(farg1
.d
) ||
1635 float64_is_signaling_nan(farg2
.d
)) {
1636 /* sNaN comparison */
1637 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
|
1638 POWERPC_EXCP_FP_VXVC
);
1640 /* qNaN comparison */
1641 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC
);
1644 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1646 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1652 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1653 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1657 #if !defined (CONFIG_USER_ONLY)
1658 void helper_store_msr (target_ulong val
)
1660 val
= hreg_store_msr(env
, val
, 0);
1662 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1663 raise_exception(env
, val
);
1667 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
);
1669 static always_inline
void do_rfi (target_ulong nip
, target_ulong msr
,
1670 target_ulong msrm
, int keep_msrh
)
1672 #if defined(TARGET_PPC64)
1673 if (msr
& (1ULL << MSR_SF
)) {
1674 nip
= (uint64_t)nip
;
1675 msr
&= (uint64_t)msrm
;
1677 nip
= (uint32_t)nip
;
1678 msr
= (uint32_t)(msr
& msrm
);
1680 msr
|= env
->msr
& ~((uint64_t)0xFFFFFFFF);
1683 nip
= (uint32_t)nip
;
1684 msr
&= (uint32_t)msrm
;
1686 /* XXX: beware: this is false if VLE is supported */
1687 env
->nip
= nip
& ~((target_ulong
)0x00000003);
1688 hreg_store_msr(env
, msr
, 1);
1689 #if defined (DEBUG_OP)
1690 cpu_dump_rfi(env
->nip
, env
->msr
);
1692 /* No need to raise an exception here,
1693 * as rfi is always the last insn of a TB
1695 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1698 void helper_rfi (void)
1700 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1701 ~((target_ulong
)0xFFFF0000), 1);
1704 #if defined(TARGET_PPC64)
1705 void helper_rfid (void)
1707 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1708 ~((target_ulong
)0xFFFF0000), 0);
1711 void helper_hrfid (void)
1713 do_rfi(env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
],
1714 ~((target_ulong
)0xFFFF0000), 0);
1719 void helper_tw (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1721 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
1722 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
1723 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
1724 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
1725 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
1726 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1730 #if defined(TARGET_PPC64)
1731 void helper_td (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1733 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
1734 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
1735 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
1736 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
1737 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01)))))
1738 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1742 /*****************************************************************************/
1743 /* PowerPC 601 specific instructions (POWER bridge) */
1745 target_ulong
helper_clcs (uint32_t arg
)
1749 /* Instruction cache line size */
1750 return env
->icache_line_size
;
1753 /* Data cache line size */
1754 return env
->dcache_line_size
;
1757 /* Minimum cache line size */
1758 return (env
->icache_line_size
< env
->dcache_line_size
) ?
1759 env
->icache_line_size
: env
->dcache_line_size
;
1762 /* Maximum cache line size */
1763 return (env
->icache_line_size
> env
->dcache_line_size
) ?
1764 env
->icache_line_size
: env
->dcache_line_size
;
1773 target_ulong
helper_div (target_ulong arg1
, target_ulong arg2
)
1775 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1777 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1778 (int32_t)arg2
== 0) {
1779 env
->spr
[SPR_MQ
] = 0;
1782 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1783 return tmp
/ (int32_t)arg2
;
1787 target_ulong
helper_divo (target_ulong arg1
, target_ulong arg2
)
1789 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1791 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1792 (int32_t)arg2
== 0) {
1793 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1794 env
->spr
[SPR_MQ
] = 0;
1797 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1798 tmp
/= (int32_t)arg2
;
1799 if ((int32_t)tmp
!= tmp
) {
1800 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1802 env
->xer
&= ~(1 << XER_OV
);
1808 target_ulong
helper_divs (target_ulong arg1
, target_ulong arg2
)
1810 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1811 (int32_t)arg2
== 0) {
1812 env
->spr
[SPR_MQ
] = 0;
1815 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1816 return (int32_t)arg1
/ (int32_t)arg2
;
1820 target_ulong
helper_divso (target_ulong arg1
, target_ulong arg2
)
1822 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1823 (int32_t)arg2
== 0) {
1824 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1825 env
->spr
[SPR_MQ
] = 0;
1828 env
->xer
&= ~(1 << XER_OV
);
1829 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1830 return (int32_t)arg1
/ (int32_t)arg2
;
1834 #if !defined (CONFIG_USER_ONLY)
1835 target_ulong
helper_rac (target_ulong addr
)
1839 target_ulong ret
= 0;
1841 /* We don't have to generate many instances of this instruction,
1842 * as rac is supervisor only.
1844 /* XXX: FIX THIS: Pretend we have no BAT */
1845 nb_BATs
= env
->nb_BATs
;
1847 if (get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) == 0)
1849 env
->nb_BATs
= nb_BATs
;
1853 void helper_rfsvc (void)
1855 do_rfi(env
->lr
, env
->ctr
, 0x0000FFFF, 0);
1859 /*****************************************************************************/
1860 /* 602 specific instructions */
1861 /* mfrom is the most crazy instruction ever seen, imho ! */
1862 /* Real implementation uses a ROM table. Do the same */
1863 #define USE_MFROM_ROM_TABLE
1864 target_ulong
helper_602_mfrom (target_ulong arg
)
1866 if (likely(arg
< 602)) {
1867 #if defined(USE_MFROM_ROM_TABLE)
1868 #include "mfrom_table.c"
1869 return mfrom_ROM_table
[arg
];
1872 /* Extremly decomposed:
1874 * return 256 * log10(10 + 1.0) + 0.5
1877 d
= float64_div(d
, 256, &env
->fp_status
);
1879 d
= exp10(d
); // XXX: use float emulation function
1880 d
= float64_add(d
, 1.0, &env
->fp_status
);
1881 d
= log10(d
); // XXX: use float emulation function
1882 d
= float64_mul(d
, 256, &env
->fp_status
);
1883 d
= float64_add(d
, 0.5, &env
->fp_status
);
1884 return float64_round_to_int(d
, &env
->fp_status
);
1891 /*****************************************************************************/
1892 /* Embedded PowerPC specific helpers */
1894 /* XXX: to be improved to check access rights when in user-mode */
1895 target_ulong
helper_load_dcr (target_ulong dcrn
)
1897 target_ulong val
= 0;
1899 if (unlikely(env
->dcr_env
== NULL
)) {
1900 if (loglevel
!= 0) {
1901 fprintf(logfile
, "No DCR environment\n");
1903 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1904 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1905 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, dcrn
, &val
) != 0)) {
1906 if (loglevel
!= 0) {
1907 fprintf(logfile
, "DCR read error %d %03x\n", (int)dcrn
, (int)dcrn
);
1909 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1910 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1915 void helper_store_dcr (target_ulong dcrn
, target_ulong val
)
1917 if (unlikely(env
->dcr_env
== NULL
)) {
1918 if (loglevel
!= 0) {
1919 fprintf(logfile
, "No DCR environment\n");
1921 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1922 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1923 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, dcrn
, val
) != 0)) {
1924 if (loglevel
!= 0) {
1925 fprintf(logfile
, "DCR write error %d %03x\n", (int)dcrn
, (int)dcrn
);
1927 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1928 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1932 #if !defined(CONFIG_USER_ONLY)
1933 void helper_40x_rfci (void)
1935 do_rfi(env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
],
1936 ~((target_ulong
)0xFFFF0000), 0);
1939 void helper_rfci (void)
1941 do_rfi(env
->spr
[SPR_BOOKE_CSRR0
], SPR_BOOKE_CSRR1
,
1942 ~((target_ulong
)0x3FFF0000), 0);
1945 void helper_rfdi (void)
1947 do_rfi(env
->spr
[SPR_BOOKE_DSRR0
], SPR_BOOKE_DSRR1
,
1948 ~((target_ulong
)0x3FFF0000), 0);
1951 void helper_rfmci (void)
1953 do_rfi(env
->spr
[SPR_BOOKE_MCSRR0
], SPR_BOOKE_MCSRR1
,
1954 ~((target_ulong
)0x3FFF0000), 0);
1959 target_ulong
helper_dlmzb (target_ulong high
, target_ulong low
, uint32_t update_Rc
)
1965 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1966 if ((high
& mask
) == 0) {
1974 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1975 if ((low
& mask
) == 0) {
1987 env
->xer
= (env
->xer
& ~0x7F) | i
;
1989 env
->crf
[0] |= xer_so
;
1994 /*****************************************************************************/
1995 /* SPE extension helpers */
1996 /* Use a table to make this quicker */
1997 static uint8_t hbrev
[16] = {
1998 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1999 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
2002 static always_inline
uint8_t byte_reverse (uint8_t val
)
2004 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
2007 static always_inline
uint32_t word_reverse (uint32_t val
)
2009 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
2010 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
2013 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
2014 target_ulong
helper_brinc (target_ulong arg1
, target_ulong arg2
)
2016 uint32_t a
, b
, d
, mask
;
2018 mask
= UINT32_MAX
>> (32 - MASKBITS
);
2021 d
= word_reverse(1 + word_reverse(a
| ~b
));
2022 return (arg1
& ~mask
) | (d
& b
);
2025 uint32_t helper_cntlsw32 (uint32_t val
)
2027 if (val
& 0x80000000)
2033 uint32_t helper_cntlzw32 (uint32_t val
)
2038 /* Single-precision floating-point conversions */
2039 static always_inline
uint32_t efscfsi (uint32_t val
)
2043 u
.f
= int32_to_float32(val
, &env
->spe_status
);
2048 static always_inline
uint32_t efscfui (uint32_t val
)
2052 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
2057 static always_inline
int32_t efsctsi (uint32_t val
)
2062 /* NaN are not treated the same way IEEE 754 does */
2063 if (unlikely(isnan(u
.f
)))
2066 return float32_to_int32(u
.f
, &env
->spe_status
);
2069 static always_inline
uint32_t efsctui (uint32_t val
)
2074 /* NaN are not treated the same way IEEE 754 does */
2075 if (unlikely(isnan(u
.f
)))
2078 return float32_to_uint32(u
.f
, &env
->spe_status
);
2081 static always_inline
uint32_t efsctsiz (uint32_t val
)
2086 /* NaN are not treated the same way IEEE 754 does */
2087 if (unlikely(isnan(u
.f
)))
2090 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
2093 static always_inline
uint32_t efsctuiz (uint32_t val
)
2098 /* NaN are not treated the same way IEEE 754 does */
2099 if (unlikely(isnan(u
.f
)))
2102 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
2105 static always_inline
uint32_t efscfsf (uint32_t val
)
2110 u
.f
= int32_to_float32(val
, &env
->spe_status
);
2111 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
2112 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
2117 static always_inline
uint32_t efscfuf (uint32_t val
)
2122 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
2123 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2124 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
2129 static always_inline
uint32_t efsctsf (uint32_t val
)
2135 /* NaN are not treated the same way IEEE 754 does */
2136 if (unlikely(isnan(u
.f
)))
2138 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2139 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2141 return float32_to_int32(u
.f
, &env
->spe_status
);
2144 static always_inline
uint32_t efsctuf (uint32_t val
)
2150 /* NaN are not treated the same way IEEE 754 does */
2151 if (unlikely(isnan(u
.f
)))
2153 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2154 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2156 return float32_to_uint32(u
.f
, &env
->spe_status
);
2159 #define HELPER_SPE_SINGLE_CONV(name) \
2160 uint32_t helper_e##name (uint32_t val) \
2162 return e##name(val); \
2165 HELPER_SPE_SINGLE_CONV(fscfsi
);
2167 HELPER_SPE_SINGLE_CONV(fscfui
);
2169 HELPER_SPE_SINGLE_CONV(fscfuf
);
2171 HELPER_SPE_SINGLE_CONV(fscfsf
);
2173 HELPER_SPE_SINGLE_CONV(fsctsi
);
2175 HELPER_SPE_SINGLE_CONV(fsctui
);
2177 HELPER_SPE_SINGLE_CONV(fsctsiz
);
2179 HELPER_SPE_SINGLE_CONV(fsctuiz
);
2181 HELPER_SPE_SINGLE_CONV(fsctsf
);
2183 HELPER_SPE_SINGLE_CONV(fsctuf
);
2185 #define HELPER_SPE_VECTOR_CONV(name) \
2186 uint64_t helper_ev##name (uint64_t val) \
2188 return ((uint64_t)e##name(val >> 32) << 32) | \
2189 (uint64_t)e##name(val); \
2192 HELPER_SPE_VECTOR_CONV(fscfsi
);
2194 HELPER_SPE_VECTOR_CONV(fscfui
);
2196 HELPER_SPE_VECTOR_CONV(fscfuf
);
2198 HELPER_SPE_VECTOR_CONV(fscfsf
);
2200 HELPER_SPE_VECTOR_CONV(fsctsi
);
2202 HELPER_SPE_VECTOR_CONV(fsctui
);
2204 HELPER_SPE_VECTOR_CONV(fsctsiz
);
2206 HELPER_SPE_VECTOR_CONV(fsctuiz
);
2208 HELPER_SPE_VECTOR_CONV(fsctsf
);
2210 HELPER_SPE_VECTOR_CONV(fsctuf
);
2212 /* Single-precision floating-point arithmetic */
2213 static always_inline
uint32_t efsadd (uint32_t op1
, uint32_t op2
)
2218 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->spe_status
);
2222 static always_inline
uint32_t efssub (uint32_t op1
, uint32_t op2
)
2227 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->spe_status
);
2231 static always_inline
uint32_t efsmul (uint32_t op1
, uint32_t op2
)
2236 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->spe_status
);
2240 static always_inline
uint32_t efsdiv (uint32_t op1
, uint32_t op2
)
2245 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->spe_status
);
2249 #define HELPER_SPE_SINGLE_ARITH(name) \
2250 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2252 return e##name(op1, op2); \
2255 HELPER_SPE_SINGLE_ARITH(fsadd
);
2257 HELPER_SPE_SINGLE_ARITH(fssub
);
2259 HELPER_SPE_SINGLE_ARITH(fsmul
);
2261 HELPER_SPE_SINGLE_ARITH(fsdiv
);
2263 #define HELPER_SPE_VECTOR_ARITH(name) \
2264 uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
2266 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
2267 (uint64_t)e##name(op1, op2); \
2270 HELPER_SPE_VECTOR_ARITH(fsadd
);
2272 HELPER_SPE_VECTOR_ARITH(fssub
);
2274 HELPER_SPE_VECTOR_ARITH(fsmul
);
2276 HELPER_SPE_VECTOR_ARITH(fsdiv
);
2278 /* Single-precision floating-point comparisons */
2279 static always_inline
uint32_t efststlt (uint32_t op1
, uint32_t op2
)
2284 return float32_lt(u1
.f
, u2
.f
, &env
->spe_status
) ? 4 : 0;
2287 static always_inline
uint32_t efststgt (uint32_t op1
, uint32_t op2
)
2292 return float32_le(u1
.f
, u2
.f
, &env
->spe_status
) ? 0 : 4;
2295 static always_inline
uint32_t efststeq (uint32_t op1
, uint32_t op2
)
2300 return float32_eq(u1
.f
, u2
.f
, &env
->spe_status
) ? 4 : 0;
2303 static always_inline
uint32_t efscmplt (uint32_t op1
, uint32_t op2
)
2305 /* XXX: TODO: test special values (NaN, infinites, ...) */
2306 return efststlt(op1
, op2
);
2309 static always_inline
uint32_t efscmpgt (uint32_t op1
, uint32_t op2
)
2311 /* XXX: TODO: test special values (NaN, infinites, ...) */
2312 return efststgt(op1
, op2
);
2315 static always_inline
uint32_t efscmpeq (uint32_t op1
, uint32_t op2
)
2317 /* XXX: TODO: test special values (NaN, infinites, ...) */
2318 return efststeq(op1
, op2
);
2321 #define HELPER_SINGLE_SPE_CMP(name) \
2322 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2324 return e##name(op1, op2) << 2; \
2327 HELPER_SINGLE_SPE_CMP(fststlt
);
2329 HELPER_SINGLE_SPE_CMP(fststgt
);
2331 HELPER_SINGLE_SPE_CMP(fststeq
);
2333 HELPER_SINGLE_SPE_CMP(fscmplt
);
2335 HELPER_SINGLE_SPE_CMP(fscmpgt
);
2337 HELPER_SINGLE_SPE_CMP(fscmpeq
);
2339 static always_inline
uint32_t evcmp_merge (int t0
, int t1
)
2341 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
2344 #define HELPER_VECTOR_SPE_CMP(name) \
2345 uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
2347 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
2350 HELPER_VECTOR_SPE_CMP(fststlt
);
2352 HELPER_VECTOR_SPE_CMP(fststgt
);
2354 HELPER_VECTOR_SPE_CMP(fststeq
);
2356 HELPER_VECTOR_SPE_CMP(fscmplt
);
2358 HELPER_VECTOR_SPE_CMP(fscmpgt
);
2360 HELPER_VECTOR_SPE_CMP(fscmpeq
);
2362 /* Double-precision floating-point conversion */
2363 uint64_t helper_efdcfsi (uint32_t val
)
2367 u
.d
= int32_to_float64(val
, &env
->spe_status
);
2372 uint64_t helper_efdcfsid (uint64_t val
)
2376 u
.d
= int64_to_float64(val
, &env
->spe_status
);
2381 uint64_t helper_efdcfui (uint32_t val
)
2385 u
.d
= uint32_to_float64(val
, &env
->spe_status
);
2390 uint64_t helper_efdcfuid (uint64_t val
)
2394 u
.d
= uint64_to_float64(val
, &env
->spe_status
);
2399 uint32_t helper_efdctsi (uint64_t val
)
2404 /* NaN are not treated the same way IEEE 754 does */
2405 if (unlikely(isnan(u
.d
)))
2408 return float64_to_int32(u
.d
, &env
->spe_status
);
2411 uint32_t helper_efdctui (uint64_t val
)
2416 /* NaN are not treated the same way IEEE 754 does */
2417 if (unlikely(isnan(u
.d
)))
2420 return float64_to_uint32(u
.d
, &env
->spe_status
);
2423 uint32_t helper_efdctsiz (uint64_t val
)
2428 /* NaN are not treated the same way IEEE 754 does */
2429 if (unlikely(isnan(u
.d
)))
2432 return float64_to_int32_round_to_zero(u
.d
, &env
->spe_status
);
2435 uint64_t helper_efdctsidz (uint64_t val
)
2440 /* NaN are not treated the same way IEEE 754 does */
2441 if (unlikely(isnan(u
.d
)))
2444 return float64_to_int64_round_to_zero(u
.d
, &env
->spe_status
);
2447 uint32_t helper_efdctuiz (uint64_t val
)
2452 /* NaN are not treated the same way IEEE 754 does */
2453 if (unlikely(isnan(u
.d
)))
2456 return float64_to_uint32_round_to_zero(u
.d
, &env
->spe_status
);
2459 uint64_t helper_efdctuidz (uint64_t val
)
2464 /* NaN are not treated the same way IEEE 754 does */
2465 if (unlikely(isnan(u
.d
)))
2468 return float64_to_uint64_round_to_zero(u
.d
, &env
->spe_status
);
2471 uint64_t helper_efdcfsf (uint32_t val
)
2476 u
.d
= int32_to_float64(val
, &env
->spe_status
);
2477 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2478 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2483 uint64_t helper_efdcfuf (uint32_t val
)
2488 u
.d
= uint32_to_float64(val
, &env
->spe_status
);
2489 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2490 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2495 uint32_t helper_efdctsf (uint64_t val
)
2501 /* NaN are not treated the same way IEEE 754 does */
2502 if (unlikely(isnan(u
.d
)))
2504 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2505 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2507 return float64_to_int32(u
.d
, &env
->spe_status
);
2510 uint32_t helper_efdctuf (uint64_t val
)
2516 /* NaN are not treated the same way IEEE 754 does */
2517 if (unlikely(isnan(u
.d
)))
2519 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2520 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2522 return float64_to_uint32(u
.d
, &env
->spe_status
);
2525 uint32_t helper_efscfd (uint64_t val
)
2531 u2
.f
= float64_to_float32(u1
.d
, &env
->spe_status
);
2536 uint64_t helper_efdcfs (uint32_t val
)
2542 u2
.d
= float32_to_float64(u1
.f
, &env
->spe_status
);
2547 /* Double precision fixed-point arithmetic */
2548 uint64_t helper_efdadd (uint64_t op1
, uint64_t op2
)
2553 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->spe_status
);
2557 uint64_t helper_efdsub (uint64_t op1
, uint64_t op2
)
2562 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->spe_status
);
2566 uint64_t helper_efdmul (uint64_t op1
, uint64_t op2
)
2571 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->spe_status
);
2575 uint64_t helper_efddiv (uint64_t op1
, uint64_t op2
)
2580 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->spe_status
);
2584 /* Double precision floating point helpers */
2585 uint32_t helper_efdtstlt (uint64_t op1
, uint64_t op2
)
2590 return float64_lt(u1
.d
, u2
.d
, &env
->spe_status
) ? 4 : 0;
2593 uint32_t helper_efdtstgt (uint64_t op1
, uint64_t op2
)
2598 return float64_le(u1
.d
, u2
.d
, &env
->spe_status
) ? 0 : 4;
2601 uint32_t helper_efdtsteq (uint64_t op1
, uint64_t op2
)
2606 return float64_eq(u1
.d
, u2
.d
, &env
->spe_status
) ? 4 : 0;
2609 uint32_t helper_efdcmplt (uint64_t op1
, uint64_t op2
)
2611 /* XXX: TODO: test special values (NaN, infinites, ...) */
2612 return helper_efdtstlt(op1
, op2
);
2615 uint32_t helper_efdcmpgt (uint64_t op1
, uint64_t op2
)
2617 /* XXX: TODO: test special values (NaN, infinites, ...) */
2618 return helper_efdtstgt(op1
, op2
);
2621 uint32_t helper_efdcmpeq (uint64_t op1
, uint64_t op2
)
2623 /* XXX: TODO: test special values (NaN, infinites, ...) */
2624 return helper_efdtsteq(op1
, op2
);
2627 /*****************************************************************************/
2628 /* Softmmu support */
2629 #if !defined (CONFIG_USER_ONLY)
2631 #define MMUSUFFIX _mmu
2634 #include "softmmu_template.h"
2637 #include "softmmu_template.h"
2640 #include "softmmu_template.h"
2643 #include "softmmu_template.h"
2645 /* try to fill the TLB and return an exception if error. If retaddr is
2646 NULL, it means that the function was called in C code (i.e. not
2647 from generated code or from helper.c) */
2648 /* XXX: fix it to restore all registers */
2649 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
2651 TranslationBlock
*tb
;
2652 CPUState
*saved_env
;
2656 /* XXX: hack to restore env in all cases, even if not called from
2659 env
= cpu_single_env
;
2660 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
2661 if (unlikely(ret
!= 0)) {
2662 if (likely(retaddr
)) {
2663 /* now we have a real cpu fault */
2664 pc
= (unsigned long)retaddr
;
2665 tb
= tb_find_pc(pc
);
2667 /* the PC is inside the translated code. It means that we have
2668 a virtual CPU fault */
2669 cpu_restore_state(tb
, env
, pc
, NULL
);
2672 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
2677 /* Segment registers load and store */
2678 target_ulong
helper_load_sr (target_ulong sr_num
)
2680 return env
->sr
[sr_num
];
2683 void helper_store_sr (target_ulong sr_num
, target_ulong val
)
2685 ppc_store_sr(env
, sr_num
, val
);
2688 /* SLB management */
2689 #if defined(TARGET_PPC64)
2690 target_ulong
helper_load_slb (target_ulong slb_nr
)
2692 return ppc_load_slb(env
, slb_nr
);
2695 void helper_store_slb (target_ulong slb_nr
, target_ulong rs
)
2697 ppc_store_slb(env
, slb_nr
, rs
);
2700 void helper_slbia (void)
2702 ppc_slb_invalidate_all(env
);
2705 void helper_slbie (target_ulong addr
)
2707 ppc_slb_invalidate_one(env
, addr
);
2710 #endif /* defined(TARGET_PPC64) */
2712 /* TLB management */
2713 void helper_tlbia (void)
2715 ppc_tlb_invalidate_all(env
);
2718 void helper_tlbie (target_ulong addr
)
2720 ppc_tlb_invalidate_one(env
, addr
);
2723 /* Software driven TLBs management */
2724 /* PowerPC 602/603 software TLB load instructions helpers */
2725 static void do_6xx_tlb (target_ulong new_EPN
, int is_code
)
2727 target_ulong RPN
, CMP
, EPN
;
2730 RPN
= env
->spr
[SPR_RPA
];
2732 CMP
= env
->spr
[SPR_ICMP
];
2733 EPN
= env
->spr
[SPR_IMISS
];
2735 CMP
= env
->spr
[SPR_DCMP
];
2736 EPN
= env
->spr
[SPR_DMISS
];
2738 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2739 #if defined (DEBUG_SOFTWARE_TLB)
2740 if (loglevel
!= 0) {
2741 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2742 " PTE1 " ADDRX
" way %d\n",
2743 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2746 /* Store this TLB */
2747 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2748 way
, is_code
, CMP
, RPN
);
2751 void helper_6xx_tlbd (target_ulong EPN
)
2756 void helper_6xx_tlbi (target_ulong EPN
)
2761 /* PowerPC 74xx software TLB load instructions helpers */
2762 static void do_74xx_tlb (target_ulong new_EPN
, int is_code
)
2764 target_ulong RPN
, CMP
, EPN
;
2767 RPN
= env
->spr
[SPR_PTELO
];
2768 CMP
= env
->spr
[SPR_PTEHI
];
2769 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2770 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2771 #if defined (DEBUG_SOFTWARE_TLB)
2772 if (loglevel
!= 0) {
2773 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2774 " PTE1 " ADDRX
" way %d\n",
2775 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2778 /* Store this TLB */
2779 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2780 way
, is_code
, CMP
, RPN
);
2783 void helper_74xx_tlbd (target_ulong EPN
)
2785 do_74xx_tlb(EPN
, 0);
2788 void helper_74xx_tlbi (target_ulong EPN
)
2790 do_74xx_tlb(EPN
, 1);
2793 static always_inline target_ulong
booke_tlb_to_page_size (int size
)
2795 return 1024 << (2 * size
);
2798 static always_inline
int booke_page_size_to_tlb (target_ulong page_size
)
2802 switch (page_size
) {
2836 #if defined (TARGET_PPC64)
2837 case 0x000100000000ULL
:
2840 case 0x000400000000ULL
:
2843 case 0x001000000000ULL
:
2846 case 0x004000000000ULL
:
2849 case 0x010000000000ULL
:
2861 /* Helpers for 4xx TLB management */
2862 target_ulong
helper_4xx_tlbre_lo (target_ulong entry
)
2869 tlb
= &env
->tlb
[entry
].tlbe
;
2871 if (tlb
->prot
& PAGE_VALID
)
2873 size
= booke_page_size_to_tlb(tlb
->size
);
2874 if (size
< 0 || size
> 0x7)
2877 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2881 target_ulong
helper_4xx_tlbre_hi (target_ulong entry
)
2887 tlb
= &env
->tlb
[entry
].tlbe
;
2889 if (tlb
->prot
& PAGE_EXEC
)
2891 if (tlb
->prot
& PAGE_WRITE
)
2896 void helper_4xx_tlbwe_hi (target_ulong entry
, target_ulong val
)
2899 target_ulong page
, end
;
2901 #if defined (DEBUG_SOFTWARE_TLB)
2902 if (loglevel
!= 0) {
2903 fprintf(logfile
, "%s entry " TDX
" val " TDX
"\n", __func__
, entry
, val
);
2907 tlb
= &env
->tlb
[entry
].tlbe
;
2908 /* Invalidate previous TLB (if it's valid) */
2909 if (tlb
->prot
& PAGE_VALID
) {
2910 end
= tlb
->EPN
+ tlb
->size
;
2911 #if defined (DEBUG_SOFTWARE_TLB)
2912 if (loglevel
!= 0) {
2913 fprintf(logfile
, "%s: invalidate old TLB %d start " ADDRX
2914 " end " ADDRX
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
2917 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2918 tlb_flush_page(env
, page
);
2920 tlb
->size
= booke_tlb_to_page_size((val
>> 7) & 0x7);
2921 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2922 * If this ever occurs, one should use the ppcemb target instead
2923 * of the ppc or ppc64 one
2925 if ((val
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
2926 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
2927 "are not supported (%d)\n",
2928 tlb
->size
, TARGET_PAGE_SIZE
, (int)((val
>> 7) & 0x7));
2930 tlb
->EPN
= val
& ~(tlb
->size
- 1);
2932 tlb
->prot
|= PAGE_VALID
;
2934 tlb
->prot
&= ~PAGE_VALID
;
2936 /* XXX: TO BE FIXED */
2937 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
2939 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2940 tlb
->attr
= val
& 0xFF;
2941 #if defined (DEBUG_SOFTWARE_TLB)
2942 if (loglevel
!= 0) {
2943 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2944 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2945 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2946 tlb
->prot
& PAGE_READ
? 'r' : '-',
2947 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2948 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2949 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2952 /* Invalidate new TLB (if valid) */
2953 if (tlb
->prot
& PAGE_VALID
) {
2954 end
= tlb
->EPN
+ tlb
->size
;
2955 #if defined (DEBUG_SOFTWARE_TLB)
2956 if (loglevel
!= 0) {
2957 fprintf(logfile
, "%s: invalidate TLB %d start " ADDRX
2958 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2961 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2962 tlb_flush_page(env
, page
);
2966 void helper_4xx_tlbwe_lo (target_ulong entry
, target_ulong val
)
2970 #if defined (DEBUG_SOFTWARE_TLB)
2971 if (loglevel
!= 0) {
2972 fprintf(logfile
, "%s entry " TDX
" val " TDX
"\n", __func__
, entry
, val
);
2976 tlb
= &env
->tlb
[entry
].tlbe
;
2977 tlb
->RPN
= val
& 0xFFFFFC00;
2978 tlb
->prot
= PAGE_READ
;
2980 tlb
->prot
|= PAGE_EXEC
;
2982 tlb
->prot
|= PAGE_WRITE
;
2983 #if defined (DEBUG_SOFTWARE_TLB)
2984 if (loglevel
!= 0) {
2985 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2986 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2987 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2988 tlb
->prot
& PAGE_READ
? 'r' : '-',
2989 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2990 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2991 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2996 target_ulong
helper_4xx_tlbsx (target_ulong address
)
2998 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_40x_PID
]);
3001 /* PowerPC 440 TLB management */
3002 void helper_440_tlbwe (uint32_t word
, target_ulong entry
, target_ulong value
)
3005 target_ulong EPN
, RPN
, size
;
3008 #if defined (DEBUG_SOFTWARE_TLB)
3009 if (loglevel
!= 0) {
3010 fprintf(logfile
, "%s word %d entry " TDX
" value " TDX
"\n",
3011 __func__
, word
, entry
, value
);
3016 tlb
= &env
->tlb
[entry
].tlbe
;
3019 /* Just here to please gcc */
3021 EPN
= value
& 0xFFFFFC00;
3022 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
3025 size
= booke_tlb_to_page_size((value
>> 4) & 0xF);
3026 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
3030 tlb
->attr
|= (value
>> 8) & 1;
3031 if (value
& 0x200) {
3032 tlb
->prot
|= PAGE_VALID
;
3034 if (tlb
->prot
& PAGE_VALID
) {
3035 tlb
->prot
&= ~PAGE_VALID
;
3039 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
3044 RPN
= value
& 0xFFFFFC0F;
3045 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
3050 tlb
->attr
= (tlb
->attr
& 0x1) | (value
& 0x0000FF00);
3051 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
3053 tlb
->prot
|= PAGE_READ
<< 4;
3055 tlb
->prot
|= PAGE_WRITE
<< 4;
3057 tlb
->prot
|= PAGE_EXEC
<< 4;
3059 tlb
->prot
|= PAGE_READ
;
3061 tlb
->prot
|= PAGE_WRITE
;
3063 tlb
->prot
|= PAGE_EXEC
;
3068 target_ulong
helper_440_tlbre (uint32_t word
, target_ulong entry
)
3075 tlb
= &env
->tlb
[entry
].tlbe
;
3078 /* Just here to please gcc */
3081 size
= booke_page_size_to_tlb(tlb
->size
);
3082 if (size
< 0 || size
> 0xF)
3085 if (tlb
->attr
& 0x1)
3087 if (tlb
->prot
& PAGE_VALID
)
3089 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
3090 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
3096 ret
= tlb
->attr
& ~0x1;
3097 if (tlb
->prot
& (PAGE_READ
<< 4))
3099 if (tlb
->prot
& (PAGE_WRITE
<< 4))
3101 if (tlb
->prot
& (PAGE_EXEC
<< 4))
3103 if (tlb
->prot
& PAGE_READ
)
3105 if (tlb
->prot
& PAGE_WRITE
)
3107 if (tlb
->prot
& PAGE_EXEC
)
3114 target_ulong
helper_440_tlbsx (target_ulong address
)
3116 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_440_MMUCR
] & 0xFF);
3119 #endif /* !CONFIG_USER_ONLY */