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"
28 //#define DEBUG_EXCEPTIONS
29 //#define DEBUG_SOFTWARE_TLB
31 /*****************************************************************************/
32 /* Exceptions processing helpers */
34 void helper_raise_exception_err (uint32_t exception
, uint32_t error_code
)
36 raise_exception_err(env
, exception
, error_code
);
39 void helper_raise_debug (void)
41 raise_exception(env
, EXCP_DEBUG
);
44 /*****************************************************************************/
45 /* Registers load and stores */
46 target_ulong
helper_load_cr (void)
48 return (env
->crf
[0] << 28) |
58 void helper_store_cr (target_ulong val
, uint32_t mask
)
62 for (i
= 0, sh
= 7; i
< 8; i
++, sh
--) {
64 env
->crf
[i
] = (val
>> (sh
* 4)) & 0xFUL
;
68 #if defined(TARGET_PPC64)
69 void do_store_pri (int prio
)
71 env
->spr
[SPR_PPR
] &= ~0x001C000000000000ULL
;
72 env
->spr
[SPR_PPR
] |= ((uint64_t)prio
& 0x7) << 50;
76 target_ulong
ppc_load_dump_spr (int sprn
)
79 fprintf(logfile
, "Read SPR %d %03x => " ADDRX
"\n",
80 sprn
, sprn
, env
->spr
[sprn
]);
83 return env
->spr
[sprn
];
86 void ppc_store_dump_spr (int sprn
, target_ulong val
)
89 fprintf(logfile
, "Write SPR %d %03x => " ADDRX
" <= " ADDRX
"\n",
90 sprn
, sprn
, env
->spr
[sprn
], val
);
95 /*****************************************************************************/
96 /* Memory load and stores */
98 static always_inline target_ulong
get_addr(target_ulong addr
)
100 #if defined(TARGET_PPC64)
105 return (uint32_t)addr
;
108 void helper_lmw (target_ulong addr
, uint32_t reg
)
110 for (; reg
< 32; reg
++, addr
+= 4) {
112 env
->gpr
[reg
] = bswap32(ldl(get_addr(addr
)));
114 env
->gpr
[reg
] = ldl(get_addr(addr
));
118 void helper_stmw (target_ulong addr
, uint32_t reg
)
120 for (; reg
< 32; reg
++, addr
+= 4) {
122 stl(get_addr(addr
), bswap32((uint32_t)env
->gpr
[reg
]));
124 stl(get_addr(addr
), (uint32_t)env
->gpr
[reg
]);
128 void helper_lsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
131 for (; nb
> 3; nb
-= 4, addr
+= 4) {
132 env
->gpr
[reg
] = ldl(get_addr(addr
));
133 reg
= (reg
+ 1) % 32;
135 if (unlikely(nb
> 0)) {
137 for (sh
= 24; nb
> 0; nb
--, addr
++, sh
-= 8) {
138 env
->gpr
[reg
] |= ldub(get_addr(addr
)) << sh
;
142 /* PPC32 specification says we must generate an exception if
143 * rA is in the range of registers to be loaded.
144 * In an other hand, IBM says this is valid, but rA won't be loaded.
145 * For now, I'll follow the spec...
147 void helper_lswx(target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
149 if (likely(xer_bc
!= 0)) {
150 if (unlikely((ra
!= 0 && reg
< ra
&& (reg
+ xer_bc
) > ra
) ||
151 (reg
< rb
&& (reg
+ xer_bc
) > rb
))) {
152 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
154 POWERPC_EXCP_INVAL_LSWX
);
156 helper_lsw(addr
, xer_bc
, reg
);
161 void helper_stsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
164 for (; nb
> 3; nb
-= 4, addr
+= 4) {
165 stl(get_addr(addr
), env
->gpr
[reg
]);
166 reg
= (reg
+ 1) % 32;
168 if (unlikely(nb
> 0)) {
169 for (sh
= 24; nb
> 0; nb
--, addr
++, sh
-= 8)
170 stb(get_addr(addr
), (env
->gpr
[reg
] >> sh
) & 0xFF);
174 static void do_dcbz(target_ulong addr
, int dcache_line_size
)
176 target_long mask
= get_addr(~(dcache_line_size
- 1));
179 for (i
= 0 ; i
< dcache_line_size
; i
+= 4) {
182 if ((env
->reserve
& mask
) == addr
)
183 env
->reserve
= (target_ulong
)-1ULL;
186 void helper_dcbz(target_ulong addr
)
188 do_dcbz(addr
, env
->dcache_line_size
);
191 void helper_dcbz_970(target_ulong addr
)
193 if (((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1)
196 do_dcbz(addr
, env
->dcache_line_size
);
199 void helper_icbi(target_ulong addr
)
203 addr
= get_addr(addr
& ~(env
->dcache_line_size
- 1));
204 /* Invalidate one cache line :
205 * PowerPC specification says this is to be treated like a load
206 * (not a fetch) by the MMU. To be sure it will be so,
207 * do the load "by hand".
210 tb_invalidate_page_range(addr
, addr
+ env
->icache_line_size
);
214 target_ulong
helper_lscbx (target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
218 for (i
= 0; i
< xer_bc
; i
++) {
219 c
= ldub((uint32_t)addr
++);
220 /* ra (if not 0) and rb are never modified */
221 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
222 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
224 if (unlikely(c
== xer_cmp
))
226 if (likely(d
!= 0)) {
237 /*****************************************************************************/
238 /* Fixed point operations helpers */
239 #if defined(TARGET_PPC64)
241 /* multiply high word */
242 uint64_t helper_mulhd (uint64_t arg1
, uint64_t arg2
)
246 muls64(&tl
, &th
, arg1
, arg2
);
250 /* multiply high word unsigned */
251 uint64_t helper_mulhdu (uint64_t arg1
, uint64_t arg2
)
255 mulu64(&tl
, &th
, arg1
, arg2
);
259 uint64_t helper_mulldo (uint64_t arg1
, uint64_t arg2
)
264 muls64(&tl
, (uint64_t *)&th
, arg1
, arg2
);
265 /* If th != 0 && th != -1, then we had an overflow */
266 if (likely((uint64_t)(th
+ 1) <= 1)) {
267 env
->xer
&= ~(1 << XER_OV
);
269 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
275 target_ulong
helper_cntlzw (target_ulong t
)
280 #if defined(TARGET_PPC64)
281 target_ulong
helper_cntlzd (target_ulong t
)
287 /* shift right arithmetic helper */
288 target_ulong
helper_sraw (target_ulong value
, target_ulong shift
)
292 if (likely(!(shift
& 0x20))) {
293 if (likely((uint32_t)shift
!= 0)) {
295 ret
= (int32_t)value
>> shift
;
296 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
297 env
->xer
&= ~(1 << XER_CA
);
299 env
->xer
|= (1 << XER_CA
);
302 ret
= (int32_t)value
;
303 env
->xer
&= ~(1 << XER_CA
);
306 ret
= (int32_t)value
>> 31;
308 env
->xer
|= (1 << XER_CA
);
310 env
->xer
&= ~(1 << XER_CA
);
313 return (target_long
)ret
;
316 #if defined(TARGET_PPC64)
317 target_ulong
helper_srad (target_ulong value
, target_ulong shift
)
321 if (likely(!(shift
& 0x40))) {
322 if (likely((uint64_t)shift
!= 0)) {
324 ret
= (int64_t)value
>> shift
;
325 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
326 env
->xer
&= ~(1 << XER_CA
);
328 env
->xer
|= (1 << XER_CA
);
331 ret
= (int64_t)value
;
332 env
->xer
&= ~(1 << XER_CA
);
335 ret
= (int64_t)value
>> 63;
337 env
->xer
|= (1 << XER_CA
);
339 env
->xer
&= ~(1 << XER_CA
);
346 target_ulong
helper_popcntb (target_ulong val
)
348 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
349 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
350 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
354 #if defined(TARGET_PPC64)
355 target_ulong
helper_popcntb_64 (target_ulong val
)
357 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
358 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
359 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
364 /*****************************************************************************/
365 /* Floating point operations helpers */
366 uint64_t helper_float32_to_float64(uint32_t arg
)
371 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
375 uint32_t helper_float64_to_float32(uint64_t arg
)
380 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
384 static always_inline
int fpisneg (float64 d
)
390 return u
.ll
>> 63 != 0;
393 static always_inline
int isden (float64 d
)
399 return ((u
.ll
>> 52) & 0x7FF) == 0;
402 static always_inline
int iszero (float64 d
)
408 return (u
.ll
& ~0x8000000000000000ULL
) == 0;
411 static always_inline
int isinfinity (float64 d
)
417 return ((u
.ll
>> 52) & 0x7FF) == 0x7FF &&
418 (u
.ll
& 0x000FFFFFFFFFFFFFULL
) == 0;
421 #ifdef CONFIG_SOFTFLOAT
422 static always_inline
int isfinite (float64 d
)
428 return (((u
.ll
>> 52) & 0x7FF) != 0x7FF);
431 static always_inline
int isnormal (float64 d
)
437 uint32_t exp
= (u
.ll
>> 52) & 0x7FF;
438 return ((0 < exp
) && (exp
< 0x7FF));
442 uint32_t helper_compute_fprf (uint64_t arg
, uint32_t set_fprf
)
448 isneg
= fpisneg(farg
.d
);
449 if (unlikely(float64_is_nan(farg
.d
))) {
450 if (float64_is_signaling_nan(farg
.d
)) {
451 /* Signaling NaN: flags are undefined */
457 } else if (unlikely(isinfinity(farg
.d
))) {
464 if (iszero(farg
.d
)) {
472 /* Denormalized numbers */
475 /* Normalized numbers */
486 /* We update FPSCR_FPRF */
487 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
488 env
->fpscr
|= ret
<< FPSCR_FPRF
;
490 /* We just need fpcc to update Rc1 */
494 /* Floating-point invalid operations exception */
495 static always_inline
uint64_t fload_invalid_op_excp (int op
)
501 if (op
& POWERPC_EXCP_FP_VXSNAN
) {
502 /* Operation on signaling NaN */
503 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
505 if (op
& POWERPC_EXCP_FP_VXSOFT
) {
506 /* Software-defined condition */
507 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
509 switch (op
& ~(POWERPC_EXCP_FP_VXSOFT
| POWERPC_EXCP_FP_VXSNAN
)) {
510 case POWERPC_EXCP_FP_VXISI
:
511 /* Magnitude subtraction of infinities */
512 env
->fpscr
|= 1 << FPSCR_VXISI
;
514 case POWERPC_EXCP_FP_VXIDI
:
515 /* Division of infinity by infinity */
516 env
->fpscr
|= 1 << FPSCR_VXIDI
;
518 case POWERPC_EXCP_FP_VXZDZ
:
519 /* Division of zero by zero */
520 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
522 case POWERPC_EXCP_FP_VXIMZ
:
523 /* Multiplication of zero by infinity */
524 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
526 case POWERPC_EXCP_FP_VXVC
:
527 /* Ordered comparison of NaN */
528 env
->fpscr
|= 1 << FPSCR_VXVC
;
529 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
530 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
531 /* We must update the target FPR before raising the exception */
533 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
534 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
535 /* Update the floating-point enabled exception summary */
536 env
->fpscr
|= 1 << FPSCR_FEX
;
537 /* Exception is differed */
541 case POWERPC_EXCP_FP_VXSQRT
:
542 /* Square root of a negative number */
543 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
545 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
547 /* Set the result to quiet NaN */
549 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
550 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
553 case POWERPC_EXCP_FP_VXCVI
:
554 /* Invalid conversion */
555 env
->fpscr
|= 1 << FPSCR_VXCVI
;
556 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
558 /* Set the result to quiet NaN */
560 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
561 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
565 /* Update the floating-point invalid operation summary */
566 env
->fpscr
|= 1 << FPSCR_VX
;
567 /* Update the floating-point exception summary */
568 env
->fpscr
|= 1 << FPSCR_FX
;
570 /* Update the floating-point enabled exception summary */
571 env
->fpscr
|= 1 << FPSCR_FEX
;
572 if (msr_fe0
!= 0 || msr_fe1
!= 0)
573 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_FP
| op
);
578 static always_inline
uint64_t float_zero_divide_excp (uint64_t arg1
, uint64_t arg2
)
580 env
->fpscr
|= 1 << FPSCR_ZX
;
581 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
582 /* Update the floating-point exception summary */
583 env
->fpscr
|= 1 << FPSCR_FX
;
585 /* Update the floating-point enabled exception summary */
586 env
->fpscr
|= 1 << FPSCR_FEX
;
587 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
588 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
589 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
592 /* Set the result to infinity */
593 arg1
= ((arg1
^ arg2
) & 0x8000000000000000ULL
);
594 arg1
|= 0x7FFULL
<< 52;
599 static always_inline
void float_overflow_excp (void)
601 env
->fpscr
|= 1 << FPSCR_OX
;
602 /* Update the floating-point exception summary */
603 env
->fpscr
|= 1 << FPSCR_FX
;
605 /* XXX: should adjust the result */
606 /* Update the floating-point enabled exception summary */
607 env
->fpscr
|= 1 << FPSCR_FEX
;
608 /* We must update the target FPR before raising the exception */
609 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
610 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
612 env
->fpscr
|= 1 << FPSCR_XX
;
613 env
->fpscr
|= 1 << FPSCR_FI
;
617 static always_inline
void float_underflow_excp (void)
619 env
->fpscr
|= 1 << FPSCR_UX
;
620 /* Update the floating-point exception summary */
621 env
->fpscr
|= 1 << FPSCR_FX
;
623 /* XXX: should adjust the result */
624 /* Update the floating-point enabled exception summary */
625 env
->fpscr
|= 1 << FPSCR_FEX
;
626 /* We must update the target FPR before raising the exception */
627 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
628 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
632 static always_inline
void float_inexact_excp (void)
634 env
->fpscr
|= 1 << FPSCR_XX
;
635 /* Update the floating-point exception summary */
636 env
->fpscr
|= 1 << FPSCR_FX
;
638 /* Update the floating-point enabled exception summary */
639 env
->fpscr
|= 1 << FPSCR_FEX
;
640 /* We must update the target FPR before raising the exception */
641 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
642 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
646 static always_inline
void fpscr_set_rounding_mode (void)
650 /* Set rounding mode */
653 /* Best approximation (round to nearest) */
654 rnd_type
= float_round_nearest_even
;
657 /* Smaller magnitude (round toward zero) */
658 rnd_type
= float_round_to_zero
;
661 /* Round toward +infinite */
662 rnd_type
= float_round_up
;
666 /* Round toward -infinite */
667 rnd_type
= float_round_down
;
670 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
673 void helper_fpscr_setbit (uint32_t bit
)
677 prev
= (env
->fpscr
>> bit
) & 1;
678 env
->fpscr
|= 1 << bit
;
682 env
->fpscr
|= 1 << FPSCR_FX
;
686 env
->fpscr
|= 1 << FPSCR_FX
;
691 env
->fpscr
|= 1 << FPSCR_FX
;
696 env
->fpscr
|= 1 << FPSCR_FX
;
701 env
->fpscr
|= 1 << FPSCR_FX
;
714 env
->fpscr
|= 1 << FPSCR_VX
;
715 env
->fpscr
|= 1 << FPSCR_FX
;
722 env
->error_code
= POWERPC_EXCP_FP
;
724 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
726 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
728 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
730 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
732 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
734 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
736 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
738 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
740 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
747 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
754 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
761 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
768 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
774 fpscr_set_rounding_mode();
779 /* Update the floating-point enabled exception summary */
780 env
->fpscr
|= 1 << FPSCR_FEX
;
781 /* We have to update Rc1 before raising the exception */
782 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
788 void helper_store_fpscr (uint64_t arg
, uint32_t mask
)
791 * We use only the 32 LSB of the incoming fpr
799 new |= prev
& 0x90000000;
800 for (i
= 0; i
< 7; i
++) {
801 if (mask
& (1 << i
)) {
802 env
->fpscr
&= ~(0xF << (4 * i
));
803 env
->fpscr
|= new & (0xF << (4 * i
));
806 /* Update VX and FEX */
808 env
->fpscr
|= 1 << FPSCR_VX
;
810 env
->fpscr
&= ~(1 << FPSCR_VX
);
811 if ((fpscr_ex
& fpscr_eex
) != 0) {
812 env
->fpscr
|= 1 << FPSCR_FEX
;
813 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
814 /* XXX: we should compute it properly */
815 env
->error_code
= POWERPC_EXCP_FP
;
818 env
->fpscr
&= ~(1 << FPSCR_FEX
);
819 fpscr_set_rounding_mode();
822 void helper_float_check_status (void)
824 #ifdef CONFIG_SOFTFLOAT
825 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
826 (env
->error_code
& POWERPC_EXCP_FP
)) {
827 /* Differred floating-point exception after target FPR update */
828 if (msr_fe0
!= 0 || msr_fe1
!= 0)
829 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
830 } else if (env
->fp_status
.float_exception_flags
& float_flag_overflow
) {
831 float_overflow_excp();
832 } else if (env
->fp_status
.float_exception_flags
& float_flag_underflow
) {
833 float_underflow_excp();
834 } else if (env
->fp_status
.float_exception_flags
& float_flag_inexact
) {
835 float_inexact_excp();
838 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
839 (env
->error_code
& POWERPC_EXCP_FP
)) {
840 /* Differred floating-point exception after target FPR update */
841 if (msr_fe0
!= 0 || msr_fe1
!= 0)
842 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
848 #ifdef CONFIG_SOFTFLOAT
849 void helper_reset_fpstatus (void)
851 env
->fp_status
.float_exception_flags
= 0;
856 uint64_t helper_fadd (uint64_t arg1
, uint64_t arg2
)
858 CPU_DoubleU farg1
, farg2
;
862 #if USE_PRECISE_EMULATION
863 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
864 float64_is_signaling_nan(farg2
.d
))) {
866 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
867 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
868 fpisneg(farg1
.d
) == fpisneg(farg2
.d
))) {
869 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
871 /* Magnitude subtraction of infinities */
872 farg1
.ll
== fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
875 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
881 uint64_t helper_fsub (uint64_t arg1
, uint64_t arg2
)
883 CPU_DoubleU farg1
, farg2
;
887 #if USE_PRECISE_EMULATION
889 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
890 float64_is_signaling_nan(farg2
.d
))) {
891 /* sNaN subtraction */
892 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
893 } else if (likely(isfinite(farg1
.d
) || isfinite(farg2
.d
) ||
894 fpisneg(farg1
.d
) != fpisneg(farg2
.d
))) {
895 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
897 /* Magnitude subtraction of infinities */
898 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
902 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
908 uint64_t helper_fmul (uint64_t arg1
, uint64_t arg2
)
910 CPU_DoubleU farg1
, farg2
;
914 #if USE_PRECISE_EMULATION
915 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
916 float64_is_signaling_nan(farg2
.d
))) {
917 /* sNaN multiplication */
918 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
919 } else if (unlikely((isinfinity(farg1
.d
) && iszero(farg2
.d
)) ||
920 (iszero(farg1
.d
) && isinfinity(farg2
.d
)))) {
921 /* Multiplication of zero by infinity */
922 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
924 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
928 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
934 uint64_t helper_fdiv (uint64_t arg1
, uint64_t arg2
)
936 CPU_DoubleU farg1
, farg2
;
940 #if USE_PRECISE_EMULATION
941 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
942 float64_is_signaling_nan(farg2
.d
))) {
944 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
945 } else if (unlikely(isinfinity(farg1
.d
) && isinfinity(farg2
.d
))) {
946 /* Division of infinity by infinity */
947 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI
);
948 } else if (unlikely(iszero(farg2
.d
))) {
949 if (iszero(farg1
.d
)) {
950 /* Division of zero by zero */
951 farg1
.ll
fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ
);
953 /* Division by zero */
954 farg1
.ll
= float_zero_divide_excp(farg1
.d
, farg2
.d
);
957 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
960 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
966 uint64_t helper_fabs (uint64_t arg
)
971 farg
.d
= float64_abs(farg
.d
);
976 uint64_t helper_fnabs (uint64_t arg
)
981 farg
.d
= float64_abs(farg
.d
);
982 farg
.d
= float64_chs(farg
.d
);
987 uint64_t helper_fneg (uint64_t arg
)
992 farg
.d
= float64_chs(farg
.d
);
997 uint64_t helper_fctiw (uint64_t arg
)
1002 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1003 /* sNaN conversion */
1004 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1005 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1006 /* qNan / infinity conversion */
1007 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1009 farg
.ll
= float64_to_int32(farg
.d
, &env
->fp_status
);
1010 #if USE_PRECISE_EMULATION
1011 /* XXX: higher bits are not supposed to be significant.
1012 * to make tests easier, return the same as a real PowerPC 750
1014 farg
.ll
|= 0xFFF80000ULL
<< 32;
1020 /* fctiwz - fctiwz. */
1021 uint64_t helper_fctiwz (uint64_t arg
)
1026 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1027 /* sNaN conversion */
1028 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1029 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1030 /* qNan / infinity conversion */
1031 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1033 farg
.ll
= float64_to_int32_round_to_zero(farg
.d
, &env
->fp_status
);
1034 #if USE_PRECISE_EMULATION
1035 /* XXX: higher bits are not supposed to be significant.
1036 * to make tests easier, return the same as a real PowerPC 750
1038 farg
.ll
|= 0xFFF80000ULL
<< 32;
1044 #if defined(TARGET_PPC64)
1045 /* fcfid - fcfid. */
1046 uint64_t helper_fcfid (uint64_t arg
)
1049 farg
.d
= int64_to_float64(arg
, &env
->fp_status
);
1053 /* fctid - fctid. */
1054 uint64_t helper_fctid (uint64_t arg
)
1059 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1060 /* sNaN conversion */
1061 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1062 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1063 /* qNan / infinity conversion */
1064 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1066 farg
.ll
= float64_to_int64(farg
.d
, &env
->fp_status
);
1071 /* fctidz - fctidz. */
1072 uint64_t helper_fctidz (uint64_t arg
)
1077 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1078 /* sNaN conversion */
1079 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1080 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1081 /* qNan / infinity conversion */
1082 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1084 farg
.ll
= float64_to_int64_round_to_zero(farg
.d
, &env
->fp_status
);
1091 static always_inline
uint64_t do_fri (uint64_t arg
, int rounding_mode
)
1096 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1098 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1099 } else if (unlikely(float64_is_nan(farg
.d
) || isinfinity(farg
.d
))) {
1100 /* qNan / infinity round */
1101 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1103 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
1104 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
1105 /* Restore rounding mode from FPSCR */
1106 fpscr_set_rounding_mode();
1111 uint64_t helper_frin (uint64_t arg
)
1113 return do_fri(arg
, float_round_nearest_even
);
1116 uint64_t helper_friz (uint64_t arg
)
1118 return do_fri(arg
, float_round_to_zero
);
1121 uint64_t helper_frip (uint64_t arg
)
1123 return do_fri(arg
, float_round_up
);
1126 uint64_t helper_frim (uint64_t arg
)
1128 return do_fri(arg
, float_round_down
);
1131 /* fmadd - fmadd. */
1132 uint64_t helper_fmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1134 CPU_DoubleU farg1
, farg2
, farg3
;
1139 #if USE_PRECISE_EMULATION
1140 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1141 float64_is_signaling_nan(farg2
.d
) ||
1142 float64_is_signaling_nan(farg3
.d
))) {
1143 /* sNaN operation */
1144 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1147 /* This is the way the PowerPC specification defines it */
1148 float128 ft0_128
, ft1_128
;
1150 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1151 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1152 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1153 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1154 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1155 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1157 /* This is OK on x86 hosts */
1158 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1162 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1163 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1168 /* fmsub - fmsub. */
1169 uint64_t helper_fmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1171 CPU_DoubleU farg1
, farg2
, farg3
;
1176 #if USE_PRECISE_EMULATION
1177 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1178 float64_is_signaling_nan(farg2
.d
) ||
1179 float64_is_signaling_nan(farg3
.d
))) {
1180 /* sNaN operation */
1181 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1184 /* This is the way the PowerPC specification defines it */
1185 float128 ft0_128
, ft1_128
;
1187 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1188 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1189 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1190 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1191 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1192 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1194 /* This is OK on x86 hosts */
1195 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1199 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1200 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1205 /* fnmadd - fnmadd. */
1206 uint64_t helper_fnmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1208 CPU_DoubleU farg1
, farg2
, farg3
;
1214 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1215 float64_is_signaling_nan(farg2
.d
) ||
1216 float64_is_signaling_nan(farg3
.d
))) {
1217 /* sNaN operation */
1218 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1220 #if USE_PRECISE_EMULATION
1222 /* This is the way the PowerPC specification defines it */
1223 float128 ft0_128
, ft1_128
;
1225 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1226 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1227 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1228 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1229 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1230 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1232 /* This is OK on x86 hosts */
1233 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1236 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1237 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1239 if (likely(!isnan(farg1
.d
)))
1240 farg1
.d
= float64_chs(farg1
.d
);
1245 /* fnmsub - fnmsub. */
1246 uint64_t helper_fnmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1248 CPU_DoubleU farg1
, farg2
, farg3
;
1254 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1255 float64_is_signaling_nan(farg2
.d
) ||
1256 float64_is_signaling_nan(farg3
.d
))) {
1257 /* sNaN operation */
1258 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1260 #if USE_PRECISE_EMULATION
1262 /* This is the way the PowerPC specification defines it */
1263 float128 ft0_128
, ft1_128
;
1265 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1266 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1267 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1268 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1269 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1270 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1272 /* This is OK on x86 hosts */
1273 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1276 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1277 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1279 if (likely(!isnan(farg1
.d
)))
1280 farg1
.d
= float64_chs(farg1
.d
);
1286 uint64_t helper_frsp (uint64_t arg
)
1291 #if USE_PRECISE_EMULATION
1292 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1293 /* sNaN square root */
1294 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1296 fard
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1299 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1304 /* fsqrt - fsqrt. */
1305 uint64_t helper_fsqrt (uint64_t arg
)
1310 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1311 /* sNaN square root */
1312 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1313 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1314 /* Square root of a negative nonzero number */
1315 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1317 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1323 uint64_t helper_fre (uint64_t arg
)
1328 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1329 /* sNaN reciprocal */
1330 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1331 } else if (unlikely(iszero(farg
.d
))) {
1332 /* Zero reciprocal */
1333 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1334 } else if (likely(isnormal(farg
.d
))) {
1335 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1337 if (farg
.ll
== 0x8000000000000000ULL
) {
1338 farg
.ll
= 0xFFF0000000000000ULL
;
1339 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1340 farg
.ll
= 0x7FF0000000000000ULL
;
1341 } else if (isnan(farg
.d
)) {
1342 farg
.ll
= 0x7FF8000000000000ULL
;
1343 } else if (fpisneg(farg
.d
)) {
1344 farg
.ll
= 0x8000000000000000ULL
;
1346 farg
.ll
= 0x0000000000000000ULL
;
1353 uint64_t helper_fres (uint64_t arg
)
1358 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1359 /* sNaN reciprocal */
1360 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1361 } else if (unlikely(iszero(farg
.d
))) {
1362 /* Zero reciprocal */
1363 farg
.ll
= float_zero_divide_excp(1.0, farg
.d
);
1364 } else if (likely(isnormal(farg
.d
))) {
1365 #if USE_PRECISE_EMULATION
1366 farg
.d
= float64_div(1.0, farg
.d
, &env
->fp_status
);
1367 farg
.d
= float64_to_float32(farg
.d
, &env
->fp_status
);
1369 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1372 if (farg
.ll
== 0x8000000000000000ULL
) {
1373 farg
.ll
= 0xFFF0000000000000ULL
;
1374 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1375 farg
.ll
= 0x7FF0000000000000ULL
;
1376 } else if (isnan(farg
.d
)) {
1377 farg
.ll
= 0x7FF8000000000000ULL
;
1378 } else if (fpisneg(farg
.d
)) {
1379 farg
.ll
= 0x8000000000000000ULL
;
1381 farg
.ll
= 0x0000000000000000ULL
;
1387 /* frsqrte - frsqrte. */
1388 uint64_t helper_frsqrte (uint64_t arg
)
1393 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1394 /* sNaN reciprocal square root */
1395 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1396 } else if (unlikely(fpisneg(farg
.d
) && !iszero(farg
.d
))) {
1397 /* Reciprocal square root of a negative nonzero number */
1398 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1399 } else if (likely(isnormal(farg
.d
))) {
1400 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1401 farg
.d
= float32_div(1.0, farg
.d
, &env
->fp_status
);
1403 if (farg
.ll
== 0x8000000000000000ULL
) {
1404 farg
.ll
= 0xFFF0000000000000ULL
;
1405 } else if (farg
.ll
== 0x0000000000000000ULL
) {
1406 farg
.ll
= 0x7FF0000000000000ULL
;
1407 } else if (isnan(farg
.d
)) {
1408 farg
.ll
|= 0x000FFFFFFFFFFFFFULL
;
1409 } else if (fpisneg(farg
.d
)) {
1410 farg
.ll
= 0x7FF8000000000000ULL
;
1412 farg
.ll
= 0x0000000000000000ULL
;
1419 uint64_t helper_fsel (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1421 CPU_DoubleU farg1
, farg2
, farg3
;
1427 if (!fpisneg(farg1
.d
) || iszero(farg1
.d
))
1433 uint32_t helper_fcmpu (uint64_t arg1
, uint64_t arg2
)
1435 CPU_DoubleU farg1
, farg2
;
1440 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1441 float64_is_signaling_nan(farg2
.d
))) {
1442 /* sNaN comparison */
1443 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1445 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1447 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1453 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1454 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1458 uint32_t helper_fcmpo (uint64_t arg1
, uint64_t arg2
)
1460 CPU_DoubleU farg1
, farg2
;
1465 if (unlikely(float64_is_nan(farg1
.d
) ||
1466 float64_is_nan(farg2
.d
))) {
1467 if (float64_is_signaling_nan(farg1
.d
) ||
1468 float64_is_signaling_nan(farg2
.d
)) {
1469 /* sNaN comparison */
1470 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
|
1471 POWERPC_EXCP_FP_VXVC
);
1473 /* qNaN comparison */
1474 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC
);
1477 if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1479 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1485 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1486 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1490 #if !defined (CONFIG_USER_ONLY)
1491 void helper_store_msr (target_ulong val
)
1493 val
= hreg_store_msr(env
, val
, 0);
1495 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1496 raise_exception(env
, val
);
1500 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
);
1502 static always_inline
void do_rfi (target_ulong nip
, target_ulong msr
,
1503 target_ulong msrm
, int keep_msrh
)
1505 #if defined(TARGET_PPC64)
1506 if (msr
& (1ULL << MSR_SF
)) {
1507 nip
= (uint64_t)nip
;
1508 msr
&= (uint64_t)msrm
;
1510 nip
= (uint32_t)nip
;
1511 msr
= (uint32_t)(msr
& msrm
);
1513 msr
|= env
->msr
& ~((uint64_t)0xFFFFFFFF);
1516 nip
= (uint32_t)nip
;
1517 msr
&= (uint32_t)msrm
;
1519 /* XXX: beware: this is false if VLE is supported */
1520 env
->nip
= nip
& ~((target_ulong
)0x00000003);
1521 hreg_store_msr(env
, msr
, 1);
1522 #if defined (DEBUG_OP)
1523 cpu_dump_rfi(env
->nip
, env
->msr
);
1525 /* No need to raise an exception here,
1526 * as rfi is always the last insn of a TB
1528 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1531 void helper_rfi (void)
1533 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1534 ~((target_ulong
)0xFFFF0000), 1);
1537 #if defined(TARGET_PPC64)
1538 void helper_rfid (void)
1540 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1541 ~((target_ulong
)0xFFFF0000), 0);
1544 void helper_hrfid (void)
1546 do_rfi(env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
],
1547 ~((target_ulong
)0xFFFF0000), 0);
1552 void helper_tw (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1554 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
1555 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
1556 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
1557 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
1558 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
1559 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1563 #if defined(TARGET_PPC64)
1564 void helper_td (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1566 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
1567 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
1568 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
1569 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
1570 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01)))))
1571 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1575 /*****************************************************************************/
1576 /* PowerPC 601 specific instructions (POWER bridge) */
1577 void do_POWER_abso (void)
1579 if ((int32_t)T0
== INT32_MIN
) {
1581 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1582 } else if ((int32_t)T0
< 0) {
1584 env
->xer
&= ~(1 << XER_OV
);
1586 env
->xer
&= ~(1 << XER_OV
);
1590 target_ulong
helper_clcs (uint32_t arg
)
1594 /* Instruction cache line size */
1595 return env
->icache_line_size
;
1598 /* Data cache line size */
1599 return env
->dcache_line_size
;
1602 /* Minimum cache line size */
1603 return (env
->icache_line_size
< env
->dcache_line_size
) ?
1604 env
->icache_line_size
: env
->dcache_line_size
;
1607 /* Maximum cache line size */
1608 return (env
->icache_line_size
> env
->dcache_line_size
) ?
1609 env
->icache_line_size
: env
->dcache_line_size
;
1618 target_ulong
helper_div (target_ulong arg1
, target_ulong arg2
)
1620 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1622 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1623 (int32_t)arg2
== 0) {
1624 env
->spr
[SPR_MQ
] = 0;
1627 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1628 return tmp
/ (int32_t)arg2
;
1632 target_ulong
helper_divo (target_ulong arg1
, target_ulong arg2
)
1634 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1636 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1637 (int32_t)arg2
== 0) {
1638 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1639 env
->spr
[SPR_MQ
] = 0;
1642 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1643 tmp
/= (int32_t)arg2
;
1644 if ((int32_t)tmp
!= tmp
) {
1645 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1647 env
->xer
&= ~(1 << XER_OV
);
1653 target_ulong
helper_divs (target_ulong arg1
, target_ulong arg2
)
1655 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1656 (int32_t)arg2
== 0) {
1657 env
->spr
[SPR_MQ
] = 0;
1660 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1661 return (int32_t)arg1
/ (int32_t)arg2
;
1665 target_ulong
helper_divso (target_ulong arg1
, target_ulong arg2
)
1667 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1668 (int32_t)arg2
== 0) {
1669 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1670 env
->spr
[SPR_MQ
] = 0;
1673 env
->xer
&= ~(1 << XER_OV
);
1674 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1675 return (int32_t)arg1
/ (int32_t)arg2
;
1679 #if !defined (CONFIG_USER_ONLY)
1680 target_ulong
helper_rac (target_ulong addr
)
1684 target_ulong ret
= 0;
1686 /* We don't have to generate many instances of this instruction,
1687 * as rac is supervisor only.
1689 /* XXX: FIX THIS: Pretend we have no BAT */
1690 nb_BATs
= env
->nb_BATs
;
1692 if (get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) == 0)
1694 env
->nb_BATs
= nb_BATs
;
1698 void helper_rfsvc (void)
1700 do_rfi(env
->lr
, env
->ctr
, 0x0000FFFF, 0);
1703 void do_store_hid0_601 (void)
1707 hid0
= env
->spr
[SPR_HID0
];
1708 if ((T0
^ hid0
) & 0x00000008) {
1709 /* Change current endianness */
1710 env
->hflags
&= ~(1 << MSR_LE
);
1711 env
->hflags_nmsr
&= ~(1 << MSR_LE
);
1712 env
->hflags_nmsr
|= (1 << MSR_LE
) & (((T0
>> 3) & 1) << MSR_LE
);
1713 env
->hflags
|= env
->hflags_nmsr
;
1714 if (loglevel
!= 0) {
1715 fprintf(logfile
, "%s: set endianness to %c => " ADDRX
"\n",
1716 __func__
, T0
& 0x8 ? 'l' : 'b', env
->hflags
);
1719 env
->spr
[SPR_HID0
] = T0
;
1723 /*****************************************************************************/
1724 /* 602 specific instructions */
1725 /* mfrom is the most crazy instruction ever seen, imho ! */
1726 /* Real implementation uses a ROM table. Do the same */
1727 #define USE_MFROM_ROM_TABLE
1728 target_ulong
helper_602_mfrom (target_ulong arg
)
1730 if (likely(arg
< 602)) {
1731 #if defined(USE_MFROM_ROM_TABLE)
1732 #include "mfrom_table.c"
1733 return mfrom_ROM_table
[T0
];
1736 /* Extremly decomposed:
1738 * return 256 * log10(10 + 1.0) + 0.5
1741 d
= float64_div(d
, 256, &env
->fp_status
);
1743 d
= exp10(d
); // XXX: use float emulation function
1744 d
= float64_add(d
, 1.0, &env
->fp_status
);
1745 d
= log10(d
); // XXX: use float emulation function
1746 d
= float64_mul(d
, 256, &env
->fp_status
);
1747 d
= float64_add(d
, 0.5, &env
->fp_status
);
1748 return float64_round_to_int(d
, &env
->fp_status
);
1755 /*****************************************************************************/
1756 /* Embedded PowerPC specific helpers */
1758 /* XXX: to be improved to check access rights when in user-mode */
1759 target_ulong
helper_load_dcr (target_ulong dcrn
)
1761 target_ulong val
= 0;
1763 if (unlikely(env
->dcr_env
== NULL
)) {
1764 if (loglevel
!= 0) {
1765 fprintf(logfile
, "No DCR environment\n");
1767 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1768 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1769 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, dcrn
, &val
) != 0)) {
1770 if (loglevel
!= 0) {
1771 fprintf(logfile
, "DCR read error %d %03x\n", (int)T0
, (int)T0
);
1773 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1774 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1779 void helper_store_dcr (target_ulong dcrn
, target_ulong val
)
1781 if (unlikely(env
->dcr_env
== NULL
)) {
1782 if (loglevel
!= 0) {
1783 fprintf(logfile
, "No DCR environment\n");
1785 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1786 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1787 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, dcrn
, val
) != 0)) {
1788 if (loglevel
!= 0) {
1789 fprintf(logfile
, "DCR write error %d %03x\n", (int)T0
, (int)T0
);
1791 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
1792 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1796 #if !defined(CONFIG_USER_ONLY)
1797 void helper_40x_rfci (void)
1799 do_rfi(env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
],
1800 ~((target_ulong
)0xFFFF0000), 0);
1803 void helper_rfci (void)
1805 do_rfi(env
->spr
[SPR_BOOKE_CSRR0
], SPR_BOOKE_CSRR1
,
1806 ~((target_ulong
)0x3FFF0000), 0);
1809 void helper_rfdi (void)
1811 do_rfi(env
->spr
[SPR_BOOKE_DSRR0
], SPR_BOOKE_DSRR1
,
1812 ~((target_ulong
)0x3FFF0000), 0);
1815 void helper_rfmci (void)
1817 do_rfi(env
->spr
[SPR_BOOKE_MCSRR0
], SPR_BOOKE_MCSRR1
,
1818 ~((target_ulong
)0x3FFF0000), 0);
1821 void do_load_403_pb (int num
)
1826 void do_store_403_pb (int num
)
1828 if (likely(env
->pb
[num
] != T0
)) {
1830 /* Should be optimized */
1837 target_ulong
helper_dlmzb (target_ulong high
, target_ulong low
, uint32_t update_Rc
)
1843 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1844 if ((high
& mask
) == 0) {
1852 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1853 if ((low
& mask
) == 0) {
1865 env
->xer
= (env
->xer
& ~0x7F) | i
;
1867 env
->crf
[0] |= xer_so
;
1872 /*****************************************************************************/
1873 /* SPE extension helpers */
1874 /* Use a table to make this quicker */
1875 static uint8_t hbrev
[16] = {
1876 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1877 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1880 static always_inline
uint8_t byte_reverse (uint8_t val
)
1882 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1885 static always_inline
uint32_t word_reverse (uint32_t val
)
1887 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1888 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1891 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
1892 target_ulong
helper_brinc (target_ulong arg1
, target_ulong arg2
)
1894 uint32_t a
, b
, d
, mask
;
1896 mask
= UINT32_MAX
>> (32 - MASKBITS
);
1899 d
= word_reverse(1 + word_reverse(a
| ~b
));
1900 return (arg1
& ~mask
) | (d
& b
);
1903 uint32_t helper_cntlsw32 (uint32_t val
)
1905 if (val
& 0x80000000)
1911 uint32_t helper_cntlzw32 (uint32_t val
)
1916 /* Single-precision floating-point conversions */
1917 static always_inline
uint32_t efscfsi (uint32_t val
)
1921 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1926 static always_inline
uint32_t efscfui (uint32_t val
)
1930 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
1935 static always_inline
int32_t efsctsi (uint32_t val
)
1940 /* NaN are not treated the same way IEEE 754 does */
1941 if (unlikely(isnan(u
.f
)))
1944 return float32_to_int32(u
.f
, &env
->spe_status
);
1947 static always_inline
uint32_t efsctui (uint32_t val
)
1952 /* NaN are not treated the same way IEEE 754 does */
1953 if (unlikely(isnan(u
.f
)))
1956 return float32_to_uint32(u
.f
, &env
->spe_status
);
1959 static always_inline
uint32_t efsctsiz (uint32_t val
)
1964 /* NaN are not treated the same way IEEE 754 does */
1965 if (unlikely(isnan(u
.f
)))
1968 return float32_to_int32_round_to_zero(u
.f
, &env
->spe_status
);
1971 static always_inline
uint32_t efsctuiz (uint32_t val
)
1976 /* NaN are not treated the same way IEEE 754 does */
1977 if (unlikely(isnan(u
.f
)))
1980 return float32_to_uint32_round_to_zero(u
.f
, &env
->spe_status
);
1983 static always_inline
uint32_t efscfsf (uint32_t val
)
1988 u
.f
= int32_to_float32(val
, &env
->spe_status
);
1989 tmp
= int64_to_float32(1ULL << 32, &env
->spe_status
);
1990 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
1995 static always_inline
uint32_t efscfuf (uint32_t val
)
2000 u
.f
= uint32_to_float32(val
, &env
->spe_status
);
2001 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2002 u
.f
= float32_div(u
.f
, tmp
, &env
->spe_status
);
2007 static always_inline
uint32_t efsctsf (uint32_t val
)
2013 /* NaN are not treated the same way IEEE 754 does */
2014 if (unlikely(isnan(u
.f
)))
2016 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2017 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2019 return float32_to_int32(u
.f
, &env
->spe_status
);
2022 static always_inline
uint32_t efsctuf (uint32_t val
)
2028 /* NaN are not treated the same way IEEE 754 does */
2029 if (unlikely(isnan(u
.f
)))
2031 tmp
= uint64_to_float32(1ULL << 32, &env
->spe_status
);
2032 u
.f
= float32_mul(u
.f
, tmp
, &env
->spe_status
);
2034 return float32_to_uint32(u
.f
, &env
->spe_status
);
2037 #define HELPER_SPE_SINGLE_CONV(name) \
2038 uint32_t helper_e##name (uint32_t val) \
2040 return e##name(val); \
2043 HELPER_SPE_SINGLE_CONV(fscfsi
);
2045 HELPER_SPE_SINGLE_CONV(fscfui
);
2047 HELPER_SPE_SINGLE_CONV(fscfuf
);
2049 HELPER_SPE_SINGLE_CONV(fscfsf
);
2051 HELPER_SPE_SINGLE_CONV(fsctsi
);
2053 HELPER_SPE_SINGLE_CONV(fsctui
);
2055 HELPER_SPE_SINGLE_CONV(fsctsiz
);
2057 HELPER_SPE_SINGLE_CONV(fsctuiz
);
2059 HELPER_SPE_SINGLE_CONV(fsctsf
);
2061 HELPER_SPE_SINGLE_CONV(fsctuf
);
2063 #define HELPER_SPE_VECTOR_CONV(name) \
2064 uint64_t helper_ev##name (uint64_t val) \
2066 return ((uint64_t)e##name(val >> 32) << 32) | \
2067 (uint64_t)e##name(val); \
2070 HELPER_SPE_VECTOR_CONV(fscfsi
);
2072 HELPER_SPE_VECTOR_CONV(fscfui
);
2074 HELPER_SPE_VECTOR_CONV(fscfuf
);
2076 HELPER_SPE_VECTOR_CONV(fscfsf
);
2078 HELPER_SPE_VECTOR_CONV(fsctsi
);
2080 HELPER_SPE_VECTOR_CONV(fsctui
);
2082 HELPER_SPE_VECTOR_CONV(fsctsiz
);
2084 HELPER_SPE_VECTOR_CONV(fsctuiz
);
2086 HELPER_SPE_VECTOR_CONV(fsctsf
);
2088 HELPER_SPE_VECTOR_CONV(fsctuf
);
2090 /* Single-precision floating-point arithmetic */
2091 static always_inline
uint32_t efsadd (uint32_t op1
, uint32_t op2
)
2096 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->spe_status
);
2100 static always_inline
uint32_t efssub (uint32_t op1
, uint32_t op2
)
2105 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->spe_status
);
2109 static always_inline
uint32_t efsmul (uint32_t op1
, uint32_t op2
)
2114 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->spe_status
);
2118 static always_inline
uint32_t efsdiv (uint32_t op1
, uint32_t op2
)
2123 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->spe_status
);
2127 #define HELPER_SPE_SINGLE_ARITH(name) \
2128 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2130 return e##name(op1, op2); \
2133 HELPER_SPE_SINGLE_ARITH(fsadd
);
2135 HELPER_SPE_SINGLE_ARITH(fssub
);
2137 HELPER_SPE_SINGLE_ARITH(fsmul
);
2139 HELPER_SPE_SINGLE_ARITH(fsdiv
);
2141 #define HELPER_SPE_VECTOR_ARITH(name) \
2142 uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
2144 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
2145 (uint64_t)e##name(op1, op2); \
2148 HELPER_SPE_VECTOR_ARITH(fsadd
);
2150 HELPER_SPE_VECTOR_ARITH(fssub
);
2152 HELPER_SPE_VECTOR_ARITH(fsmul
);
2154 HELPER_SPE_VECTOR_ARITH(fsdiv
);
2156 /* Single-precision floating-point comparisons */
2157 static always_inline
uint32_t efststlt (uint32_t op1
, uint32_t op2
)
2162 return float32_lt(u1
.f
, u2
.f
, &env
->spe_status
) ? 4 : 0;
2165 static always_inline
uint32_t efststgt (uint32_t op1
, uint32_t op2
)
2170 return float32_le(u1
.f
, u2
.f
, &env
->spe_status
) ? 0 : 4;
2173 static always_inline
uint32_t efststeq (uint32_t op1
, uint32_t op2
)
2178 return float32_eq(u1
.f
, u2
.f
, &env
->spe_status
) ? 4 : 0;
2181 static always_inline
uint32_t efscmplt (uint32_t op1
, uint32_t op2
)
2183 /* XXX: TODO: test special values (NaN, infinites, ...) */
2184 return efststlt(op1
, op2
);
2187 static always_inline
uint32_t efscmpgt (uint32_t op1
, uint32_t op2
)
2189 /* XXX: TODO: test special values (NaN, infinites, ...) */
2190 return efststgt(op1
, op2
);
2193 static always_inline
uint32_t efscmpeq (uint32_t op1
, uint32_t op2
)
2195 /* XXX: TODO: test special values (NaN, infinites, ...) */
2196 return efststeq(op1
, op2
);
2199 #define HELPER_SINGLE_SPE_CMP(name) \
2200 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2202 return e##name(op1, op2) << 2; \
2205 HELPER_SINGLE_SPE_CMP(fststlt
);
2207 HELPER_SINGLE_SPE_CMP(fststgt
);
2209 HELPER_SINGLE_SPE_CMP(fststeq
);
2211 HELPER_SINGLE_SPE_CMP(fscmplt
);
2213 HELPER_SINGLE_SPE_CMP(fscmpgt
);
2215 HELPER_SINGLE_SPE_CMP(fscmpeq
);
2217 static always_inline
uint32_t evcmp_merge (int t0
, int t1
)
2219 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
2222 #define HELPER_VECTOR_SPE_CMP(name) \
2223 uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
2225 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
2228 HELPER_VECTOR_SPE_CMP(fststlt
);
2230 HELPER_VECTOR_SPE_CMP(fststgt
);
2232 HELPER_VECTOR_SPE_CMP(fststeq
);
2234 HELPER_VECTOR_SPE_CMP(fscmplt
);
2236 HELPER_VECTOR_SPE_CMP(fscmpgt
);
2238 HELPER_VECTOR_SPE_CMP(fscmpeq
);
2240 /* Double-precision floating-point conversion */
2241 uint64_t helper_efdcfsi (uint32_t val
)
2245 u
.d
= int32_to_float64(val
, &env
->spe_status
);
2250 uint64_t helper_efdcfsid (uint64_t val
)
2254 u
.d
= int64_to_float64(val
, &env
->spe_status
);
2259 uint64_t helper_efdcfui (uint32_t val
)
2263 u
.d
= uint32_to_float64(val
, &env
->spe_status
);
2268 uint64_t helper_efdcfuid (uint64_t val
)
2272 u
.d
= uint64_to_float64(val
, &env
->spe_status
);
2277 uint32_t helper_efdctsi (uint64_t val
)
2282 /* NaN are not treated the same way IEEE 754 does */
2283 if (unlikely(isnan(u
.d
)))
2286 return float64_to_int32(u
.d
, &env
->spe_status
);
2289 uint32_t helper_efdctui (uint64_t val
)
2294 /* NaN are not treated the same way IEEE 754 does */
2295 if (unlikely(isnan(u
.d
)))
2298 return float64_to_uint32(u
.d
, &env
->spe_status
);
2301 uint32_t helper_efdctsiz (uint64_t val
)
2306 /* NaN are not treated the same way IEEE 754 does */
2307 if (unlikely(isnan(u
.d
)))
2310 return float64_to_int32_round_to_zero(u
.d
, &env
->spe_status
);
2313 uint64_t helper_efdctsidz (uint64_t val
)
2318 /* NaN are not treated the same way IEEE 754 does */
2319 if (unlikely(isnan(u
.d
)))
2322 return float64_to_int64_round_to_zero(u
.d
, &env
->spe_status
);
2325 uint32_t helper_efdctuiz (uint64_t val
)
2330 /* NaN are not treated the same way IEEE 754 does */
2331 if (unlikely(isnan(u
.d
)))
2334 return float64_to_uint32_round_to_zero(u
.d
, &env
->spe_status
);
2337 uint64_t helper_efdctuidz (uint64_t val
)
2342 /* NaN are not treated the same way IEEE 754 does */
2343 if (unlikely(isnan(u
.d
)))
2346 return float64_to_uint64_round_to_zero(u
.d
, &env
->spe_status
);
2349 uint64_t helper_efdcfsf (uint32_t val
)
2354 u
.d
= int32_to_float64(val
, &env
->spe_status
);
2355 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2356 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2361 uint64_t helper_efdcfuf (uint32_t val
)
2366 u
.d
= uint32_to_float64(val
, &env
->spe_status
);
2367 tmp
= int64_to_float64(1ULL << 32, &env
->spe_status
);
2368 u
.d
= float64_div(u
.d
, tmp
, &env
->spe_status
);
2373 uint32_t helper_efdctsf (uint64_t val
)
2379 /* NaN are not treated the same way IEEE 754 does */
2380 if (unlikely(isnan(u
.d
)))
2382 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2383 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2385 return float64_to_int32(u
.d
, &env
->spe_status
);
2388 uint32_t helper_efdctuf (uint64_t val
)
2394 /* NaN are not treated the same way IEEE 754 does */
2395 if (unlikely(isnan(u
.d
)))
2397 tmp
= uint64_to_float64(1ULL << 32, &env
->spe_status
);
2398 u
.d
= float64_mul(u
.d
, tmp
, &env
->spe_status
);
2400 return float64_to_uint32(u
.d
, &env
->spe_status
);
2403 uint32_t helper_efscfd (uint64_t val
)
2409 u2
.f
= float64_to_float32(u1
.d
, &env
->spe_status
);
2414 uint64_t helper_efdcfs (uint32_t val
)
2420 u2
.d
= float32_to_float64(u1
.f
, &env
->spe_status
);
2425 /* Double precision fixed-point arithmetic */
2426 uint64_t helper_efdadd (uint64_t op1
, uint64_t op2
)
2431 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->spe_status
);
2435 uint64_t helper_efdsub (uint64_t op1
, uint64_t op2
)
2440 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->spe_status
);
2444 uint64_t helper_efdmul (uint64_t op1
, uint64_t op2
)
2449 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->spe_status
);
2453 uint64_t helper_efddiv (uint64_t op1
, uint64_t op2
)
2458 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->spe_status
);
2462 /* Double precision floating point helpers */
2463 uint32_t helper_efdtstlt (uint64_t op1
, uint64_t op2
)
2468 return float64_lt(u1
.d
, u2
.d
, &env
->spe_status
) ? 4 : 0;
2471 uint32_t helper_efdtstgt (uint64_t op1
, uint64_t op2
)
2476 return float64_le(u1
.d
, u2
.d
, &env
->spe_status
) ? 0 : 4;
2479 uint32_t helper_efdtsteq (uint64_t op1
, uint64_t op2
)
2484 return float64_eq(u1
.d
, u2
.d
, &env
->spe_status
) ? 4 : 0;
2487 uint32_t helper_efdcmplt (uint64_t op1
, uint64_t op2
)
2489 /* XXX: TODO: test special values (NaN, infinites, ...) */
2490 return helper_efdtstlt(op1
, op2
);
2493 uint32_t helper_efdcmpgt (uint64_t op1
, uint64_t op2
)
2495 /* XXX: TODO: test special values (NaN, infinites, ...) */
2496 return helper_efdtstgt(op1
, op2
);
2499 uint32_t helper_efdcmpeq (uint64_t op1
, uint64_t op2
)
2501 /* XXX: TODO: test special values (NaN, infinites, ...) */
2502 return helper_efdtsteq(op1
, op2
);
2505 /*****************************************************************************/
2506 /* Softmmu support */
2507 #if !defined (CONFIG_USER_ONLY)
2509 #define MMUSUFFIX _mmu
2512 #include "softmmu_template.h"
2515 #include "softmmu_template.h"
2518 #include "softmmu_template.h"
2521 #include "softmmu_template.h"
2523 /* try to fill the TLB and return an exception if error. If retaddr is
2524 NULL, it means that the function was called in C code (i.e. not
2525 from generated code or from helper.c) */
2526 /* XXX: fix it to restore all registers */
2527 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
2529 TranslationBlock
*tb
;
2530 CPUState
*saved_env
;
2534 /* XXX: hack to restore env in all cases, even if not called from
2537 env
= cpu_single_env
;
2538 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
2539 if (unlikely(ret
!= 0)) {
2540 if (likely(retaddr
)) {
2541 /* now we have a real cpu fault */
2542 pc
= (unsigned long)retaddr
;
2543 tb
= tb_find_pc(pc
);
2545 /* the PC is inside the translated code. It means that we have
2546 a virtual CPU fault */
2547 cpu_restore_state(tb
, env
, pc
, NULL
);
2550 raise_exception_err(env
, env
->exception_index
, env
->error_code
);
2555 /* Segment registers load and store */
2556 target_ulong
helper_load_sr (target_ulong sr_num
)
2558 return env
->sr
[sr_num
];
2561 void helper_store_sr (target_ulong sr_num
, target_ulong val
)
2563 do_store_sr(env
, sr_num
, val
);
2566 /* SLB management */
2567 #if defined(TARGET_PPC64)
2568 target_ulong
helper_load_slb (target_ulong slb_nr
)
2570 return ppc_load_slb(env
, slb_nr
);
2573 void helper_store_slb (target_ulong slb_nr
, target_ulong rs
)
2575 ppc_store_slb(env
, slb_nr
, rs
);
2578 void helper_slbia (void)
2580 ppc_slb_invalidate_all(env
);
2583 void helper_slbie (target_ulong addr
)
2585 ppc_slb_invalidate_one(env
, addr
);
2588 #endif /* defined(TARGET_PPC64) */
2590 /* TLB management */
2591 void helper_tlbia (void)
2593 ppc_tlb_invalidate_all(env
);
2596 void helper_tlbie (target_ulong addr
)
2598 ppc_tlb_invalidate_one(env
, addr
);
2601 /* Software driven TLBs management */
2602 /* PowerPC 602/603 software TLB load instructions helpers */
2603 static void do_6xx_tlb (target_ulong new_EPN
, int is_code
)
2605 target_ulong RPN
, CMP
, EPN
;
2608 RPN
= env
->spr
[SPR_RPA
];
2610 CMP
= env
->spr
[SPR_ICMP
];
2611 EPN
= env
->spr
[SPR_IMISS
];
2613 CMP
= env
->spr
[SPR_DCMP
];
2614 EPN
= env
->spr
[SPR_DMISS
];
2616 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2617 #if defined (DEBUG_SOFTWARE_TLB)
2618 if (loglevel
!= 0) {
2619 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2620 " PTE1 " ADDRX
" way %d\n",
2621 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2624 /* Store this TLB */
2625 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2626 way
, is_code
, CMP
, RPN
);
2629 void helper_6xx_tlbd (target_ulong EPN
)
2634 void helper_6xx_tlbi (target_ulong EPN
)
2639 /* PowerPC 74xx software TLB load instructions helpers */
2640 static void do_74xx_tlb (target_ulong new_EPN
, int is_code
)
2642 target_ulong RPN
, CMP
, EPN
;
2645 RPN
= env
->spr
[SPR_PTELO
];
2646 CMP
= env
->spr
[SPR_PTEHI
];
2647 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2648 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2649 #if defined (DEBUG_SOFTWARE_TLB)
2650 if (loglevel
!= 0) {
2651 fprintf(logfile
, "%s: EPN " TDX
" " ADDRX
" PTE0 " ADDRX
2652 " PTE1 " ADDRX
" way %d\n",
2653 __func__
, T0
, EPN
, CMP
, RPN
, way
);
2656 /* Store this TLB */
2657 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2658 way
, is_code
, CMP
, RPN
);
2661 void helper_74xx_tlbd (target_ulong EPN
)
2663 do_74xx_tlb(EPN
, 0);
2666 void helper_74xx_tlbi (target_ulong EPN
)
2668 do_74xx_tlb(EPN
, 1);
2671 static always_inline target_ulong
booke_tlb_to_page_size (int size
)
2673 return 1024 << (2 * size
);
2676 static always_inline
int booke_page_size_to_tlb (target_ulong page_size
)
2680 switch (page_size
) {
2714 #if defined (TARGET_PPC64)
2715 case 0x000100000000ULL
:
2718 case 0x000400000000ULL
:
2721 case 0x001000000000ULL
:
2724 case 0x004000000000ULL
:
2727 case 0x010000000000ULL
:
2739 /* Helpers for 4xx TLB management */
2740 target_ulong
helper_4xx_tlbre_lo (target_ulong entry
)
2747 tlb
= &env
->tlb
[entry
].tlbe
;
2749 if (tlb
->prot
& PAGE_VALID
)
2751 size
= booke_page_size_to_tlb(tlb
->size
);
2752 if (size
< 0 || size
> 0x7)
2755 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2759 target_ulong
helper_4xx_tlbre_hi (target_ulong entry
)
2765 tlb
= &env
->tlb
[entry
].tlbe
;
2767 if (tlb
->prot
& PAGE_EXEC
)
2769 if (tlb
->prot
& PAGE_WRITE
)
2774 void helper_4xx_tlbwe_hi (target_ulong entry
, target_ulong val
)
2777 target_ulong page
, end
;
2779 #if defined (DEBUG_SOFTWARE_TLB)
2780 if (loglevel
!= 0) {
2781 fprintf(logfile
, "%s entry " TDX
" val " TDX
"\n", __func__
, entry
, val
);
2785 tlb
= &env
->tlb
[entry
].tlbe
;
2786 /* Invalidate previous TLB (if it's valid) */
2787 if (tlb
->prot
& PAGE_VALID
) {
2788 end
= tlb
->EPN
+ tlb
->size
;
2789 #if defined (DEBUG_SOFTWARE_TLB)
2790 if (loglevel
!= 0) {
2791 fprintf(logfile
, "%s: invalidate old TLB %d start " ADDRX
2792 " end " ADDRX
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
2795 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2796 tlb_flush_page(env
, page
);
2798 tlb
->size
= booke_tlb_to_page_size((val
>> 7) & 0x7);
2799 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2800 * If this ever occurs, one should use the ppcemb target instead
2801 * of the ppc or ppc64 one
2803 if ((val
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
2804 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
2805 "are not supported (%d)\n",
2806 tlb
->size
, TARGET_PAGE_SIZE
, (int)((val
>> 7) & 0x7));
2808 tlb
->EPN
= val
& ~(tlb
->size
- 1);
2810 tlb
->prot
|= PAGE_VALID
;
2812 tlb
->prot
&= ~PAGE_VALID
;
2814 /* XXX: TO BE FIXED */
2815 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
2817 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2818 tlb
->attr
= val
& 0xFF;
2819 #if defined (DEBUG_SOFTWARE_TLB)
2820 if (loglevel
!= 0) {
2821 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2822 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2823 (int)T0
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2824 tlb
->prot
& PAGE_READ
? 'r' : '-',
2825 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2826 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2827 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2830 /* Invalidate new TLB (if valid) */
2831 if (tlb
->prot
& PAGE_VALID
) {
2832 end
= tlb
->EPN
+ tlb
->size
;
2833 #if defined (DEBUG_SOFTWARE_TLB)
2834 if (loglevel
!= 0) {
2835 fprintf(logfile
, "%s: invalidate TLB %d start " ADDRX
2836 " end " ADDRX
"\n", __func__
, (int)T0
, tlb
->EPN
, end
);
2839 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
2840 tlb_flush_page(env
, page
);
2844 void helper_4xx_tlbwe_lo (target_ulong entry
, target_ulong val
)
2848 #if defined (DEBUG_SOFTWARE_TLB)
2849 if (loglevel
!= 0) {
2850 fprintf(logfile
, "%s entry " TDX
" val " TDX
"\n", __func__
, entry
, val
);
2854 tlb
= &env
->tlb
[entry
].tlbe
;
2855 tlb
->RPN
= val
& 0xFFFFFC00;
2856 tlb
->prot
= PAGE_READ
;
2858 tlb
->prot
|= PAGE_EXEC
;
2860 tlb
->prot
|= PAGE_WRITE
;
2861 #if defined (DEBUG_SOFTWARE_TLB)
2862 if (loglevel
!= 0) {
2863 fprintf(logfile
, "%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
2864 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
2865 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2866 tlb
->prot
& PAGE_READ
? 'r' : '-',
2867 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2868 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2869 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2874 target_ulong
helper_4xx_tlbsx (target_ulong address
)
2876 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_40x_PID
]);
2879 /* PowerPC 440 TLB management */
2880 void helper_440_tlbwe (uint32_t word
, target_ulong entry
, target_ulong value
)
2883 target_ulong EPN
, RPN
, size
;
2886 #if defined (DEBUG_SOFTWARE_TLB)
2887 if (loglevel
!= 0) {
2888 fprintf(logfile
, "%s word %d entry " TDX
" value " TDX
"\n",
2889 __func__
, word
, entry
, value
);
2894 tlb
= &env
->tlb
[entry
].tlbe
;
2897 /* Just here to please gcc */
2899 EPN
= value
& 0xFFFFFC00;
2900 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
2903 size
= booke_tlb_to_page_size((value
>> 4) & 0xF);
2904 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
2908 tlb
->attr
|= (value
>> 8) & 1;
2909 if (value
& 0x200) {
2910 tlb
->prot
|= PAGE_VALID
;
2912 if (tlb
->prot
& PAGE_VALID
) {
2913 tlb
->prot
&= ~PAGE_VALID
;
2917 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
2922 RPN
= value
& 0xFFFFFC0F;
2923 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
2928 tlb
->attr
= (tlb
->attr
& 0x1) | (value
& 0x0000FF00);
2929 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
2931 tlb
->prot
|= PAGE_READ
<< 4;
2933 tlb
->prot
|= PAGE_WRITE
<< 4;
2935 tlb
->prot
|= PAGE_EXEC
<< 4;
2937 tlb
->prot
|= PAGE_READ
;
2939 tlb
->prot
|= PAGE_WRITE
;
2941 tlb
->prot
|= PAGE_EXEC
;
2946 target_ulong
helper_440_tlbre (uint32_t word
, target_ulong entry
)
2953 tlb
= &env
->tlb
[entry
].tlbe
;
2956 /* Just here to please gcc */
2959 size
= booke_page_size_to_tlb(tlb
->size
);
2960 if (size
< 0 || size
> 0xF)
2963 if (tlb
->attr
& 0x1)
2965 if (tlb
->prot
& PAGE_VALID
)
2967 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
2968 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
2974 ret
= tlb
->attr
& ~0x1;
2975 if (tlb
->prot
& (PAGE_READ
<< 4))
2977 if (tlb
->prot
& (PAGE_WRITE
<< 4))
2979 if (tlb
->prot
& (PAGE_EXEC
<< 4))
2981 if (tlb
->prot
& PAGE_READ
)
2983 if (tlb
->prot
& PAGE_WRITE
)
2985 if (tlb
->prot
& PAGE_EXEC
)
2992 target_ulong
helper_440_tlbsx (target_ulong address
)
2994 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_440_MMUCR
] & 0xFF);
2997 #endif /* !CONFIG_USER_ONLY */