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, see <http://www.gnu.org/licenses/>.
21 #include "host-utils.h"
24 #include "helper_regs.h"
27 //#define DEBUG_EXCEPTIONS
28 //#define DEBUG_SOFTWARE_TLB
30 #ifdef DEBUG_SOFTWARE_TLB
31 # define LOG_SWTLB(...) qemu_log(__VA_ARGS__)
33 # define LOG_SWTLB(...) do { } while (0)
37 /*****************************************************************************/
38 /* Exceptions processing helpers */
40 void helper_raise_exception_err (uint32_t exception
, uint32_t error_code
)
43 printf("Raise exception %3x code : %d\n", exception
, error_code
);
45 env
->exception_index
= exception
;
46 env
->error_code
= error_code
;
50 void helper_raise_exception (uint32_t exception
)
52 helper_raise_exception_err(exception
, 0);
55 /*****************************************************************************/
57 void helper_load_dump_spr (uint32_t sprn
)
59 qemu_log("Read SPR %d %03x => " TARGET_FMT_lx
"\n", sprn
, sprn
,
63 void helper_store_dump_spr (uint32_t sprn
)
65 qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx
"\n", sprn
, sprn
,
69 target_ulong
helper_load_tbl (void)
71 return (target_ulong
)cpu_ppc_load_tbl(env
);
74 target_ulong
helper_load_tbu (void)
76 return cpu_ppc_load_tbu(env
);
79 target_ulong
helper_load_atbl (void)
81 return (target_ulong
)cpu_ppc_load_atbl(env
);
84 target_ulong
helper_load_atbu (void)
86 return cpu_ppc_load_atbu(env
);
89 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
90 target_ulong
helper_load_purr (void)
92 return (target_ulong
)cpu_ppc_load_purr(env
);
96 target_ulong
helper_load_601_rtcl (void)
98 return cpu_ppc601_load_rtcl(env
);
101 target_ulong
helper_load_601_rtcu (void)
103 return cpu_ppc601_load_rtcu(env
);
106 #if !defined(CONFIG_USER_ONLY)
107 #if defined (TARGET_PPC64)
108 void helper_store_asr (target_ulong val
)
110 ppc_store_asr(env
, val
);
114 void helper_store_sdr1 (target_ulong val
)
116 ppc_store_sdr1(env
, val
);
119 void helper_store_tbl (target_ulong val
)
121 cpu_ppc_store_tbl(env
, val
);
124 void helper_store_tbu (target_ulong val
)
126 cpu_ppc_store_tbu(env
, val
);
129 void helper_store_atbl (target_ulong val
)
131 cpu_ppc_store_atbl(env
, val
);
134 void helper_store_atbu (target_ulong val
)
136 cpu_ppc_store_atbu(env
, val
);
139 void helper_store_601_rtcl (target_ulong val
)
141 cpu_ppc601_store_rtcl(env
, val
);
144 void helper_store_601_rtcu (target_ulong val
)
146 cpu_ppc601_store_rtcu(env
, val
);
149 target_ulong
helper_load_decr (void)
151 return cpu_ppc_load_decr(env
);
154 void helper_store_decr (target_ulong val
)
156 cpu_ppc_store_decr(env
, val
);
159 void helper_store_hid0_601 (target_ulong val
)
163 hid0
= env
->spr
[SPR_HID0
];
164 if ((val
^ hid0
) & 0x00000008) {
165 /* Change current endianness */
166 env
->hflags
&= ~(1 << MSR_LE
);
167 env
->hflags_nmsr
&= ~(1 << MSR_LE
);
168 env
->hflags_nmsr
|= (1 << MSR_LE
) & (((val
>> 3) & 1) << MSR_LE
);
169 env
->hflags
|= env
->hflags_nmsr
;
170 qemu_log("%s: set endianness to %c => " TARGET_FMT_lx
"\n", __func__
,
171 val
& 0x8 ? 'l' : 'b', env
->hflags
);
173 env
->spr
[SPR_HID0
] = (uint32_t)val
;
176 void helper_store_403_pbr (uint32_t num
, target_ulong value
)
178 if (likely(env
->pb
[num
] != value
)) {
179 env
->pb
[num
] = value
;
180 /* Should be optimized */
185 target_ulong
helper_load_40x_pit (void)
187 return load_40x_pit(env
);
190 void helper_store_40x_pit (target_ulong val
)
192 store_40x_pit(env
, val
);
195 void helper_store_40x_dbcr0 (target_ulong val
)
197 store_40x_dbcr0(env
, val
);
200 void helper_store_40x_sler (target_ulong val
)
202 store_40x_sler(env
, val
);
205 void helper_store_booke_tcr (target_ulong val
)
207 store_booke_tcr(env
, val
);
210 void helper_store_booke_tsr (target_ulong val
)
212 store_booke_tsr(env
, val
);
215 void helper_store_ibatu (uint32_t nr
, target_ulong val
)
217 ppc_store_ibatu(env
, nr
, val
);
220 void helper_store_ibatl (uint32_t nr
, target_ulong val
)
222 ppc_store_ibatl(env
, nr
, val
);
225 void helper_store_dbatu (uint32_t nr
, target_ulong val
)
227 ppc_store_dbatu(env
, nr
, val
);
230 void helper_store_dbatl (uint32_t nr
, target_ulong val
)
232 ppc_store_dbatl(env
, nr
, val
);
235 void helper_store_601_batl (uint32_t nr
, target_ulong val
)
237 ppc_store_ibatl_601(env
, nr
, val
);
240 void helper_store_601_batu (uint32_t nr
, target_ulong val
)
242 ppc_store_ibatu_601(env
, nr
, val
);
246 /*****************************************************************************/
247 /* Memory load and stores */
249 static inline target_ulong
addr_add(target_ulong addr
, target_long arg
)
251 #if defined(TARGET_PPC64)
253 return (uint32_t)(addr
+ arg
);
259 void helper_lmw (target_ulong addr
, uint32_t reg
)
261 for (; reg
< 32; reg
++) {
263 env
->gpr
[reg
] = bswap32(ldl(addr
));
265 env
->gpr
[reg
] = ldl(addr
);
266 addr
= addr_add(addr
, 4);
270 void helper_stmw (target_ulong addr
, uint32_t reg
)
272 for (; reg
< 32; reg
++) {
274 stl(addr
, bswap32((uint32_t)env
->gpr
[reg
]));
276 stl(addr
, (uint32_t)env
->gpr
[reg
]);
277 addr
= addr_add(addr
, 4);
281 void helper_lsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
284 for (; nb
> 3; nb
-= 4) {
285 env
->gpr
[reg
] = ldl(addr
);
286 reg
= (reg
+ 1) % 32;
287 addr
= addr_add(addr
, 4);
289 if (unlikely(nb
> 0)) {
291 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
292 env
->gpr
[reg
] |= ldub(addr
) << sh
;
293 addr
= addr_add(addr
, 1);
297 /* PPC32 specification says we must generate an exception if
298 * rA is in the range of registers to be loaded.
299 * In an other hand, IBM says this is valid, but rA won't be loaded.
300 * For now, I'll follow the spec...
302 void helper_lswx(target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
304 if (likely(xer_bc
!= 0)) {
305 if (unlikely((ra
!= 0 && reg
< ra
&& (reg
+ xer_bc
) > ra
) ||
306 (reg
< rb
&& (reg
+ xer_bc
) > rb
))) {
307 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
309 POWERPC_EXCP_INVAL_LSWX
);
311 helper_lsw(addr
, xer_bc
, reg
);
316 void helper_stsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
319 for (; nb
> 3; nb
-= 4) {
320 stl(addr
, env
->gpr
[reg
]);
321 reg
= (reg
+ 1) % 32;
322 addr
= addr_add(addr
, 4);
324 if (unlikely(nb
> 0)) {
325 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
326 stb(addr
, (env
->gpr
[reg
] >> sh
) & 0xFF);
327 addr
= addr_add(addr
, 1);
332 static void do_dcbz(target_ulong addr
, int dcache_line_size
)
334 addr
&= ~(dcache_line_size
- 1);
336 for (i
= 0 ; i
< dcache_line_size
; i
+= 4) {
339 if (env
->reserve_addr
== addr
)
340 env
->reserve_addr
= (target_ulong
)-1ULL;
343 void helper_dcbz(target_ulong addr
)
345 do_dcbz(addr
, env
->dcache_line_size
);
348 void helper_dcbz_970(target_ulong addr
)
350 if (((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1)
353 do_dcbz(addr
, env
->dcache_line_size
);
356 void helper_icbi(target_ulong addr
)
358 addr
&= ~(env
->dcache_line_size
- 1);
359 /* Invalidate one cache line :
360 * PowerPC specification says this is to be treated like a load
361 * (not a fetch) by the MMU. To be sure it will be so,
362 * do the load "by hand".
365 tb_invalidate_page_range(addr
, addr
+ env
->icache_line_size
);
369 target_ulong
helper_lscbx (target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
373 for (i
= 0; i
< xer_bc
; i
++) {
375 addr
= addr_add(addr
, 1);
376 /* ra (if not 0) and rb are never modified */
377 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
378 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
380 if (unlikely(c
== xer_cmp
))
382 if (likely(d
!= 0)) {
393 /*****************************************************************************/
394 /* Fixed point operations helpers */
395 #if defined(TARGET_PPC64)
397 /* multiply high word */
398 uint64_t helper_mulhd (uint64_t arg1
, uint64_t arg2
)
402 muls64(&tl
, &th
, arg1
, arg2
);
406 /* multiply high word unsigned */
407 uint64_t helper_mulhdu (uint64_t arg1
, uint64_t arg2
)
411 mulu64(&tl
, &th
, arg1
, arg2
);
415 uint64_t helper_mulldo (uint64_t arg1
, uint64_t arg2
)
420 muls64(&tl
, (uint64_t *)&th
, arg1
, arg2
);
421 /* If th != 0 && th != -1, then we had an overflow */
422 if (likely((uint64_t)(th
+ 1) <= 1)) {
423 env
->xer
&= ~(1 << XER_OV
);
425 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
431 target_ulong
helper_cntlzw (target_ulong t
)
436 #if defined(TARGET_PPC64)
437 target_ulong
helper_cntlzd (target_ulong t
)
443 /* shift right arithmetic helper */
444 target_ulong
helper_sraw (target_ulong value
, target_ulong shift
)
448 if (likely(!(shift
& 0x20))) {
449 if (likely((uint32_t)shift
!= 0)) {
451 ret
= (int32_t)value
>> shift
;
452 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
453 env
->xer
&= ~(1 << XER_CA
);
455 env
->xer
|= (1 << XER_CA
);
458 ret
= (int32_t)value
;
459 env
->xer
&= ~(1 << XER_CA
);
462 ret
= (int32_t)value
>> 31;
464 env
->xer
|= (1 << XER_CA
);
466 env
->xer
&= ~(1 << XER_CA
);
469 return (target_long
)ret
;
472 #if defined(TARGET_PPC64)
473 target_ulong
helper_srad (target_ulong value
, target_ulong shift
)
477 if (likely(!(shift
& 0x40))) {
478 if (likely((uint64_t)shift
!= 0)) {
480 ret
= (int64_t)value
>> shift
;
481 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
482 env
->xer
&= ~(1 << XER_CA
);
484 env
->xer
|= (1 << XER_CA
);
487 ret
= (int64_t)value
;
488 env
->xer
&= ~(1 << XER_CA
);
491 ret
= (int64_t)value
>> 63;
493 env
->xer
|= (1 << XER_CA
);
495 env
->xer
&= ~(1 << XER_CA
);
502 #if defined(TARGET_PPC64)
503 target_ulong
helper_popcntb (target_ulong val
)
505 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
506 0x5555555555555555ULL
);
507 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
508 0x3333333333333333ULL
);
509 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
510 0x0f0f0f0f0f0f0f0fULL
);
514 target_ulong
helper_popcntw (target_ulong val
)
516 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
517 0x5555555555555555ULL
);
518 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
519 0x3333333333333333ULL
);
520 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
521 0x0f0f0f0f0f0f0f0fULL
);
522 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) &
523 0x00ff00ff00ff00ffULL
);
524 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) &
525 0x0000ffff0000ffffULL
);
529 target_ulong
helper_popcntd (target_ulong val
)
534 target_ulong
helper_popcntb (target_ulong val
)
536 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
537 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
538 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
542 target_ulong
helper_popcntw (target_ulong val
)
544 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
545 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
546 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
547 val
= (val
& 0x00ff00ff) + ((val
>> 8) & 0x00ff00ff);
548 val
= (val
& 0x0000ffff) + ((val
>> 16) & 0x0000ffff);
553 /*****************************************************************************/
554 /* Floating point operations helpers */
555 uint64_t helper_float32_to_float64(uint32_t arg
)
560 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
564 uint32_t helper_float64_to_float32(uint64_t arg
)
569 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
573 static inline int isden(float64 d
)
579 return ((u
.ll
>> 52) & 0x7FF) == 0;
582 uint32_t helper_compute_fprf (uint64_t arg
, uint32_t set_fprf
)
588 isneg
= float64_is_neg(farg
.d
);
589 if (unlikely(float64_is_any_nan(farg
.d
))) {
590 if (float64_is_signaling_nan(farg
.d
)) {
591 /* Signaling NaN: flags are undefined */
597 } else if (unlikely(float64_is_infinity(farg
.d
))) {
604 if (float64_is_zero(farg
.d
)) {
612 /* Denormalized numbers */
615 /* Normalized numbers */
626 /* We update FPSCR_FPRF */
627 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
628 env
->fpscr
|= ret
<< FPSCR_FPRF
;
630 /* We just need fpcc to update Rc1 */
634 /* Floating-point invalid operations exception */
635 static inline uint64_t fload_invalid_op_excp(int op
)
642 case POWERPC_EXCP_FP_VXSNAN
:
643 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
645 case POWERPC_EXCP_FP_VXSOFT
:
646 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
648 case POWERPC_EXCP_FP_VXISI
:
649 /* Magnitude subtraction of infinities */
650 env
->fpscr
|= 1 << FPSCR_VXISI
;
652 case POWERPC_EXCP_FP_VXIDI
:
653 /* Division of infinity by infinity */
654 env
->fpscr
|= 1 << FPSCR_VXIDI
;
656 case POWERPC_EXCP_FP_VXZDZ
:
657 /* Division of zero by zero */
658 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
660 case POWERPC_EXCP_FP_VXIMZ
:
661 /* Multiplication of zero by infinity */
662 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
664 case POWERPC_EXCP_FP_VXVC
:
665 /* Ordered comparison of NaN */
666 env
->fpscr
|= 1 << FPSCR_VXVC
;
667 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
668 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
669 /* We must update the target FPR before raising the exception */
671 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
672 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
673 /* Update the floating-point enabled exception summary */
674 env
->fpscr
|= 1 << FPSCR_FEX
;
675 /* Exception is differed */
679 case POWERPC_EXCP_FP_VXSQRT
:
680 /* Square root of a negative number */
681 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
683 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
685 /* Set the result to quiet NaN */
686 ret
= 0x7FF8000000000000ULL
;
687 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
688 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
691 case POWERPC_EXCP_FP_VXCVI
:
692 /* Invalid conversion */
693 env
->fpscr
|= 1 << FPSCR_VXCVI
;
694 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
696 /* Set the result to quiet NaN */
697 ret
= 0x7FF8000000000000ULL
;
698 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
699 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
703 /* Update the floating-point invalid operation summary */
704 env
->fpscr
|= 1 << FPSCR_VX
;
705 /* Update the floating-point exception summary */
706 env
->fpscr
|= 1 << FPSCR_FX
;
708 /* Update the floating-point enabled exception summary */
709 env
->fpscr
|= 1 << FPSCR_FEX
;
710 if (msr_fe0
!= 0 || msr_fe1
!= 0)
711 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_FP
| op
);
716 static inline void float_zero_divide_excp(void)
718 env
->fpscr
|= 1 << FPSCR_ZX
;
719 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
720 /* Update the floating-point exception summary */
721 env
->fpscr
|= 1 << FPSCR_FX
;
723 /* Update the floating-point enabled exception summary */
724 env
->fpscr
|= 1 << FPSCR_FEX
;
725 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
726 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
727 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
732 static inline void float_overflow_excp(void)
734 env
->fpscr
|= 1 << FPSCR_OX
;
735 /* Update the floating-point exception summary */
736 env
->fpscr
|= 1 << FPSCR_FX
;
738 /* XXX: should adjust the result */
739 /* Update the floating-point enabled exception summary */
740 env
->fpscr
|= 1 << FPSCR_FEX
;
741 /* We must update the target FPR before raising the exception */
742 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
743 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
745 env
->fpscr
|= 1 << FPSCR_XX
;
746 env
->fpscr
|= 1 << FPSCR_FI
;
750 static inline void float_underflow_excp(void)
752 env
->fpscr
|= 1 << FPSCR_UX
;
753 /* Update the floating-point exception summary */
754 env
->fpscr
|= 1 << FPSCR_FX
;
756 /* XXX: should adjust the result */
757 /* Update the floating-point enabled exception summary */
758 env
->fpscr
|= 1 << FPSCR_FEX
;
759 /* We must update the target FPR before raising the exception */
760 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
761 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
765 static inline void float_inexact_excp(void)
767 env
->fpscr
|= 1 << FPSCR_XX
;
768 /* Update the floating-point exception summary */
769 env
->fpscr
|= 1 << FPSCR_FX
;
771 /* Update the floating-point enabled exception summary */
772 env
->fpscr
|= 1 << FPSCR_FEX
;
773 /* We must update the target FPR before raising the exception */
774 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
775 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
779 static inline void fpscr_set_rounding_mode(void)
783 /* Set rounding mode */
786 /* Best approximation (round to nearest) */
787 rnd_type
= float_round_nearest_even
;
790 /* Smaller magnitude (round toward zero) */
791 rnd_type
= float_round_to_zero
;
794 /* Round toward +infinite */
795 rnd_type
= float_round_up
;
799 /* Round toward -infinite */
800 rnd_type
= float_round_down
;
803 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
806 void helper_fpscr_clrbit (uint32_t bit
)
810 prev
= (env
->fpscr
>> bit
) & 1;
811 env
->fpscr
&= ~(1 << bit
);
816 fpscr_set_rounding_mode();
824 void helper_fpscr_setbit (uint32_t bit
)
828 prev
= (env
->fpscr
>> bit
) & 1;
829 env
->fpscr
|= 1 << bit
;
833 env
->fpscr
|= 1 << FPSCR_FX
;
837 env
->fpscr
|= 1 << FPSCR_FX
;
842 env
->fpscr
|= 1 << FPSCR_FX
;
847 env
->fpscr
|= 1 << FPSCR_FX
;
852 env
->fpscr
|= 1 << FPSCR_FX
;
865 env
->fpscr
|= 1 << FPSCR_VX
;
866 env
->fpscr
|= 1 << FPSCR_FX
;
873 env
->error_code
= POWERPC_EXCP_FP
;
875 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
877 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
879 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
881 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
883 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
885 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
887 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
889 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
891 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
898 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
905 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
912 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
919 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
925 fpscr_set_rounding_mode();
930 /* Update the floating-point enabled exception summary */
931 env
->fpscr
|= 1 << FPSCR_FEX
;
932 /* We have to update Rc1 before raising the exception */
933 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
939 void helper_store_fpscr (uint64_t arg
, uint32_t mask
)
942 * We use only the 32 LSB of the incoming fpr
950 new |= prev
& 0x60000000;
951 for (i
= 0; i
< 8; i
++) {
952 if (mask
& (1 << i
)) {
953 env
->fpscr
&= ~(0xF << (4 * i
));
954 env
->fpscr
|= new & (0xF << (4 * i
));
957 /* Update VX and FEX */
959 env
->fpscr
|= 1 << FPSCR_VX
;
961 env
->fpscr
&= ~(1 << FPSCR_VX
);
962 if ((fpscr_ex
& fpscr_eex
) != 0) {
963 env
->fpscr
|= 1 << FPSCR_FEX
;
964 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
965 /* XXX: we should compute it properly */
966 env
->error_code
= POWERPC_EXCP_FP
;
969 env
->fpscr
&= ~(1 << FPSCR_FEX
);
970 fpscr_set_rounding_mode();
973 void helper_float_check_status (void)
975 #ifdef CONFIG_SOFTFLOAT
976 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
977 (env
->error_code
& POWERPC_EXCP_FP
)) {
978 /* Differred floating-point exception after target FPR update */
979 if (msr_fe0
!= 0 || msr_fe1
!= 0)
980 helper_raise_exception_err(env
->exception_index
, env
->error_code
);
982 int status
= get_float_exception_flags(&env
->fp_status
);
983 if (status
& float_flag_divbyzero
) {
984 float_zero_divide_excp();
985 } else if (status
& float_flag_overflow
) {
986 float_overflow_excp();
987 } else if (status
& float_flag_underflow
) {
988 float_underflow_excp();
989 } else if (status
& float_flag_inexact
) {
990 float_inexact_excp();
994 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
995 (env
->error_code
& POWERPC_EXCP_FP
)) {
996 /* Differred floating-point exception after target FPR update */
997 if (msr_fe0
!= 0 || msr_fe1
!= 0)
998 helper_raise_exception_err(env
->exception_index
, env
->error_code
);
1003 #ifdef CONFIG_SOFTFLOAT
1004 void helper_reset_fpstatus (void)
1006 set_float_exception_flags(0, &env
->fp_status
);
1011 uint64_t helper_fadd (uint64_t arg1
, uint64_t arg2
)
1013 CPU_DoubleU farg1
, farg2
;
1018 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
1019 float64_is_neg(farg1
.d
) != float64_is_neg(farg2
.d
))) {
1020 /* Magnitude subtraction of infinities */
1021 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1023 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1024 float64_is_signaling_nan(farg2
.d
))) {
1026 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1028 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
1035 uint64_t helper_fsub (uint64_t arg1
, uint64_t arg2
)
1037 CPU_DoubleU farg1
, farg2
;
1042 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
1043 float64_is_neg(farg1
.d
) == float64_is_neg(farg2
.d
))) {
1044 /* Magnitude subtraction of infinities */
1045 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1047 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1048 float64_is_signaling_nan(farg2
.d
))) {
1049 /* sNaN subtraction */
1050 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1052 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
1059 uint64_t helper_fmul (uint64_t arg1
, uint64_t arg2
)
1061 CPU_DoubleU farg1
, farg2
;
1066 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1067 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1068 /* Multiplication of zero by infinity */
1069 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1071 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1072 float64_is_signaling_nan(farg2
.d
))) {
1073 /* sNaN multiplication */
1074 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1076 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1083 uint64_t helper_fdiv (uint64_t arg1
, uint64_t arg2
)
1085 CPU_DoubleU farg1
, farg2
;
1090 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
))) {
1091 /* Division of infinity by infinity */
1092 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI
);
1093 } else if (unlikely(float64_is_zero(farg1
.d
) && float64_is_zero(farg2
.d
))) {
1094 /* Division of zero by zero */
1095 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ
);
1097 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1098 float64_is_signaling_nan(farg2
.d
))) {
1100 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1102 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
1109 uint64_t helper_fabs (uint64_t arg
)
1114 farg
.d
= float64_abs(farg
.d
);
1119 uint64_t helper_fnabs (uint64_t arg
)
1124 farg
.d
= float64_abs(farg
.d
);
1125 farg
.d
= float64_chs(farg
.d
);
1130 uint64_t helper_fneg (uint64_t arg
)
1135 farg
.d
= float64_chs(farg
.d
);
1139 /* fctiw - fctiw. */
1140 uint64_t helper_fctiw (uint64_t arg
)
1145 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1146 /* sNaN conversion */
1147 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1148 } else if (unlikely(float64_is_quiet_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1149 /* qNan / infinity conversion */
1150 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1152 farg
.ll
= float64_to_int32(farg
.d
, &env
->fp_status
);
1153 /* XXX: higher bits are not supposed to be significant.
1154 * to make tests easier, return the same as a real PowerPC 750
1156 farg
.ll
|= 0xFFF80000ULL
<< 32;
1161 /* fctiwz - fctiwz. */
1162 uint64_t helper_fctiwz (uint64_t arg
)
1167 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1168 /* sNaN conversion */
1169 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1170 } else if (unlikely(float64_is_quiet_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1171 /* qNan / infinity conversion */
1172 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1174 farg
.ll
= float64_to_int32_round_to_zero(farg
.d
, &env
->fp_status
);
1175 /* XXX: higher bits are not supposed to be significant.
1176 * to make tests easier, return the same as a real PowerPC 750
1178 farg
.ll
|= 0xFFF80000ULL
<< 32;
1183 #if defined(TARGET_PPC64)
1184 /* fcfid - fcfid. */
1185 uint64_t helper_fcfid (uint64_t arg
)
1188 farg
.d
= int64_to_float64(arg
, &env
->fp_status
);
1192 /* fctid - fctid. */
1193 uint64_t helper_fctid (uint64_t arg
)
1198 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1199 /* sNaN conversion */
1200 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1201 } else if (unlikely(float64_is_quiet_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1202 /* qNan / infinity conversion */
1203 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1205 farg
.ll
= float64_to_int64(farg
.d
, &env
->fp_status
);
1210 /* fctidz - fctidz. */
1211 uint64_t helper_fctidz (uint64_t arg
)
1216 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1217 /* sNaN conversion */
1218 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1219 } else if (unlikely(float64_is_quiet_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1220 /* qNan / infinity conversion */
1221 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1223 farg
.ll
= float64_to_int64_round_to_zero(farg
.d
, &env
->fp_status
);
1230 static inline uint64_t do_fri(uint64_t arg
, int rounding_mode
)
1235 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1237 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1238 } else if (unlikely(float64_is_quiet_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1239 /* qNan / infinity round */
1240 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1242 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
1243 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
1244 /* Restore rounding mode from FPSCR */
1245 fpscr_set_rounding_mode();
1250 uint64_t helper_frin (uint64_t arg
)
1252 return do_fri(arg
, float_round_nearest_even
);
1255 uint64_t helper_friz (uint64_t arg
)
1257 return do_fri(arg
, float_round_to_zero
);
1260 uint64_t helper_frip (uint64_t arg
)
1262 return do_fri(arg
, float_round_up
);
1265 uint64_t helper_frim (uint64_t arg
)
1267 return do_fri(arg
, float_round_down
);
1270 /* fmadd - fmadd. */
1271 uint64_t helper_fmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1273 CPU_DoubleU farg1
, farg2
, farg3
;
1279 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1280 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1281 /* Multiplication of zero by infinity */
1282 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1284 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1285 float64_is_signaling_nan(farg2
.d
) ||
1286 float64_is_signaling_nan(farg3
.d
))) {
1287 /* sNaN operation */
1288 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1290 /* This is the way the PowerPC specification defines it */
1291 float128 ft0_128
, ft1_128
;
1293 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1294 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1295 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1296 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1297 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
1298 /* Magnitude subtraction of infinities */
1299 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1301 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1302 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1303 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1310 /* fmsub - fmsub. */
1311 uint64_t helper_fmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1313 CPU_DoubleU farg1
, farg2
, farg3
;
1319 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1320 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1321 /* Multiplication of zero by infinity */
1322 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1324 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1325 float64_is_signaling_nan(farg2
.d
) ||
1326 float64_is_signaling_nan(farg3
.d
))) {
1327 /* sNaN operation */
1328 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1330 /* This is the way the PowerPC specification defines it */
1331 float128 ft0_128
, ft1_128
;
1333 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1334 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1335 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1336 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1337 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
1338 /* Magnitude subtraction of infinities */
1339 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1341 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1342 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1343 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1349 /* fnmadd - fnmadd. */
1350 uint64_t helper_fnmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1352 CPU_DoubleU farg1
, farg2
, farg3
;
1358 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1359 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1360 /* Multiplication of zero by infinity */
1361 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1363 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1364 float64_is_signaling_nan(farg2
.d
) ||
1365 float64_is_signaling_nan(farg3
.d
))) {
1366 /* sNaN operation */
1367 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1369 /* This is the way the PowerPC specification defines it */
1370 float128 ft0_128
, ft1_128
;
1372 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1373 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1374 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1375 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1376 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
1377 /* Magnitude subtraction of infinities */
1378 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1380 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1381 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1382 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1384 if (likely(!float64_is_any_nan(farg1
.d
))) {
1385 farg1
.d
= float64_chs(farg1
.d
);
1391 /* fnmsub - fnmsub. */
1392 uint64_t helper_fnmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1394 CPU_DoubleU farg1
, farg2
, farg3
;
1400 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1401 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1402 /* Multiplication of zero by infinity */
1403 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1405 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1406 float64_is_signaling_nan(farg2
.d
) ||
1407 float64_is_signaling_nan(farg3
.d
))) {
1408 /* sNaN operation */
1409 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1411 /* This is the way the PowerPC specification defines it */
1412 float128 ft0_128
, ft1_128
;
1414 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1415 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1416 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1417 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1418 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
1419 /* Magnitude subtraction of infinities */
1420 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1422 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1423 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1424 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1426 if (likely(!float64_is_any_nan(farg1
.d
))) {
1427 farg1
.d
= float64_chs(farg1
.d
);
1434 uint64_t helper_frsp (uint64_t arg
)
1440 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1441 /* sNaN square root */
1442 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1444 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
1445 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
1450 /* fsqrt - fsqrt. */
1451 uint64_t helper_fsqrt (uint64_t arg
)
1456 if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
1457 /* Square root of a negative nonzero number */
1458 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1460 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1461 /* sNaN square root */
1462 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1464 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1470 uint64_t helper_fre (uint64_t arg
)
1475 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1476 /* sNaN reciprocal */
1477 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1479 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
1484 uint64_t helper_fres (uint64_t arg
)
1490 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1491 /* sNaN reciprocal */
1492 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1494 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
1495 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
1496 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
1501 /* frsqrte - frsqrte. */
1502 uint64_t helper_frsqrte (uint64_t arg
)
1508 if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
1509 /* Reciprocal square root of a negative nonzero number */
1510 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1512 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1513 /* sNaN reciprocal square root */
1514 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1516 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1517 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
1518 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
1519 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
1525 uint64_t helper_fsel (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1531 if ((!float64_is_neg(farg1
.d
) || float64_is_zero(farg1
.d
)) && !float64_is_any_nan(farg1
.d
)) {
1538 void helper_fcmpu (uint64_t arg1
, uint64_t arg2
, uint32_t crfD
)
1540 CPU_DoubleU farg1
, farg2
;
1545 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1546 float64_is_any_nan(farg2
.d
))) {
1548 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1550 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1556 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1557 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1558 env
->crf
[crfD
] = ret
;
1559 if (unlikely(ret
== 0x01UL
1560 && (float64_is_signaling_nan(farg1
.d
) ||
1561 float64_is_signaling_nan(farg2
.d
)))) {
1562 /* sNaN comparison */
1563 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1567 void helper_fcmpo (uint64_t arg1
, uint64_t arg2
, uint32_t crfD
)
1569 CPU_DoubleU farg1
, farg2
;
1574 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1575 float64_is_any_nan(farg2
.d
))) {
1577 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1579 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1585 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1586 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1587 env
->crf
[crfD
] = ret
;
1588 if (unlikely (ret
== 0x01UL
)) {
1589 if (float64_is_signaling_nan(farg1
.d
) ||
1590 float64_is_signaling_nan(farg2
.d
)) {
1591 /* sNaN comparison */
1592 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
|
1593 POWERPC_EXCP_FP_VXVC
);
1595 /* qNaN comparison */
1596 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC
);
1601 #if !defined (CONFIG_USER_ONLY)
1602 void helper_store_msr (target_ulong val
)
1604 val
= hreg_store_msr(env
, val
, 0);
1606 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1607 helper_raise_exception(val
);
1611 static inline void do_rfi(target_ulong nip
, target_ulong msr
,
1612 target_ulong msrm
, int keep_msrh
)
1614 #if defined(TARGET_PPC64)
1615 if (msr
& (1ULL << MSR_SF
)) {
1616 nip
= (uint64_t)nip
;
1617 msr
&= (uint64_t)msrm
;
1619 nip
= (uint32_t)nip
;
1620 msr
= (uint32_t)(msr
& msrm
);
1622 msr
|= env
->msr
& ~((uint64_t)0xFFFFFFFF);
1625 nip
= (uint32_t)nip
;
1626 msr
&= (uint32_t)msrm
;
1628 /* XXX: beware: this is false if VLE is supported */
1629 env
->nip
= nip
& ~((target_ulong
)0x00000003);
1630 hreg_store_msr(env
, msr
, 1);
1631 #if defined (DEBUG_OP)
1632 cpu_dump_rfi(env
->nip
, env
->msr
);
1634 /* No need to raise an exception here,
1635 * as rfi is always the last insn of a TB
1637 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1640 void helper_rfi (void)
1642 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1643 ~((target_ulong
)0x783F0000), 1);
1646 #if defined(TARGET_PPC64)
1647 void helper_rfid (void)
1649 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1650 ~((target_ulong
)0x783F0000), 0);
1653 void helper_hrfid (void)
1655 do_rfi(env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
],
1656 ~((target_ulong
)0x783F0000), 0);
1661 void helper_tw (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1663 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
1664 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
1665 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
1666 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
1667 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
1668 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1672 #if defined(TARGET_PPC64)
1673 void helper_td (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1675 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
1676 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
1677 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
1678 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
1679 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01)))))
1680 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1684 /*****************************************************************************/
1685 /* PowerPC 601 specific instructions (POWER bridge) */
1687 target_ulong
helper_clcs (uint32_t arg
)
1691 /* Instruction cache line size */
1692 return env
->icache_line_size
;
1695 /* Data cache line size */
1696 return env
->dcache_line_size
;
1699 /* Minimum cache line size */
1700 return (env
->icache_line_size
< env
->dcache_line_size
) ?
1701 env
->icache_line_size
: env
->dcache_line_size
;
1704 /* Maximum cache line size */
1705 return (env
->icache_line_size
> env
->dcache_line_size
) ?
1706 env
->icache_line_size
: env
->dcache_line_size
;
1715 target_ulong
helper_div (target_ulong arg1
, target_ulong arg2
)
1717 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1719 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1720 (int32_t)arg2
== 0) {
1721 env
->spr
[SPR_MQ
] = 0;
1724 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1725 return tmp
/ (int32_t)arg2
;
1729 target_ulong
helper_divo (target_ulong arg1
, target_ulong arg2
)
1731 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1733 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1734 (int32_t)arg2
== 0) {
1735 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1736 env
->spr
[SPR_MQ
] = 0;
1739 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1740 tmp
/= (int32_t)arg2
;
1741 if ((int32_t)tmp
!= tmp
) {
1742 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1744 env
->xer
&= ~(1 << XER_OV
);
1750 target_ulong
helper_divs (target_ulong arg1
, target_ulong arg2
)
1752 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1753 (int32_t)arg2
== 0) {
1754 env
->spr
[SPR_MQ
] = 0;
1757 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1758 return (int32_t)arg1
/ (int32_t)arg2
;
1762 target_ulong
helper_divso (target_ulong arg1
, target_ulong arg2
)
1764 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1765 (int32_t)arg2
== 0) {
1766 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1767 env
->spr
[SPR_MQ
] = 0;
1770 env
->xer
&= ~(1 << XER_OV
);
1771 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1772 return (int32_t)arg1
/ (int32_t)arg2
;
1776 #if !defined (CONFIG_USER_ONLY)
1777 target_ulong
helper_rac (target_ulong addr
)
1781 target_ulong ret
= 0;
1783 /* We don't have to generate many instances of this instruction,
1784 * as rac is supervisor only.
1786 /* XXX: FIX THIS: Pretend we have no BAT */
1787 nb_BATs
= env
->nb_BATs
;
1789 if (get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) == 0)
1791 env
->nb_BATs
= nb_BATs
;
1795 void helper_rfsvc (void)
1797 do_rfi(env
->lr
, env
->ctr
, 0x0000FFFF, 0);
1801 /*****************************************************************************/
1802 /* 602 specific instructions */
1803 /* mfrom is the most crazy instruction ever seen, imho ! */
1804 /* Real implementation uses a ROM table. Do the same */
1805 /* Extremly decomposed:
1807 * return 256 * log10(10 + 1.0) + 0.5
1809 #if !defined (CONFIG_USER_ONLY)
1810 target_ulong
helper_602_mfrom (target_ulong arg
)
1812 if (likely(arg
< 602)) {
1813 #include "mfrom_table.c"
1814 return mfrom_ROM_table
[arg
];
1821 /*****************************************************************************/
1822 /* Embedded PowerPC specific helpers */
1824 /* XXX: to be improved to check access rights when in user-mode */
1825 target_ulong
helper_load_dcr (target_ulong dcrn
)
1829 if (unlikely(env
->dcr_env
== NULL
)) {
1830 qemu_log("No DCR environment\n");
1831 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1832 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1833 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, (uint32_t)dcrn
, &val
) != 0)) {
1834 qemu_log("DCR read error %d %03x\n", (uint32_t)dcrn
, (uint32_t)dcrn
);
1835 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1836 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1841 void helper_store_dcr (target_ulong dcrn
, target_ulong val
)
1843 if (unlikely(env
->dcr_env
== NULL
)) {
1844 qemu_log("No DCR environment\n");
1845 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1846 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1847 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, (uint32_t)dcrn
, (uint32_t)val
) != 0)) {
1848 qemu_log("DCR write error %d %03x\n", (uint32_t)dcrn
, (uint32_t)dcrn
);
1849 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1850 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1854 #if !defined(CONFIG_USER_ONLY)
1855 void helper_40x_rfci (void)
1857 do_rfi(env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
],
1858 ~((target_ulong
)0xFFFF0000), 0);
1861 void helper_rfci (void)
1863 do_rfi(env
->spr
[SPR_BOOKE_CSRR0
], SPR_BOOKE_CSRR1
,
1864 ~((target_ulong
)0x3FFF0000), 0);
1867 void helper_rfdi (void)
1869 do_rfi(env
->spr
[SPR_BOOKE_DSRR0
], SPR_BOOKE_DSRR1
,
1870 ~((target_ulong
)0x3FFF0000), 0);
1873 void helper_rfmci (void)
1875 do_rfi(env
->spr
[SPR_BOOKE_MCSRR0
], SPR_BOOKE_MCSRR1
,
1876 ~((target_ulong
)0x3FFF0000), 0);
1881 target_ulong
helper_dlmzb (target_ulong high
, target_ulong low
, uint32_t update_Rc
)
1887 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1888 if ((high
& mask
) == 0) {
1896 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1897 if ((low
& mask
) == 0) {
1909 env
->xer
= (env
->xer
& ~0x7F) | i
;
1911 env
->crf
[0] |= xer_so
;
1916 /*****************************************************************************/
1917 /* Altivec extension helpers */
1918 #if defined(HOST_WORDS_BIGENDIAN)
1926 #if defined(HOST_WORDS_BIGENDIAN)
1927 #define VECTOR_FOR_INORDER_I(index, element) \
1928 for (index = 0; index < ARRAY_SIZE(r->element); index++)
1930 #define VECTOR_FOR_INORDER_I(index, element) \
1931 for (index = ARRAY_SIZE(r->element)-1; index >= 0; index--)
1934 /* If X is a NaN, store the corresponding QNaN into RESULT. Otherwise,
1935 * execute the following block. */
1936 #define DO_HANDLE_NAN(result, x) \
1937 if (float32_is_any_nan(x)) { \
1940 __f.l = __f.l | (1 << 22); /* Set QNaN bit. */ \
1944 #define HANDLE_NAN1(result, x) \
1945 DO_HANDLE_NAN(result, x)
1946 #define HANDLE_NAN2(result, x, y) \
1947 DO_HANDLE_NAN(result, x) DO_HANDLE_NAN(result, y)
1948 #define HANDLE_NAN3(result, x, y, z) \
1949 DO_HANDLE_NAN(result, x) DO_HANDLE_NAN(result, y) DO_HANDLE_NAN(result, z)
1951 /* Saturating arithmetic helpers. */
1952 #define SATCVT(from, to, from_type, to_type, min, max) \
1953 static inline to_type cvt##from##to(from_type x, int *sat) \
1956 if (x < (from_type)min) { \
1959 } else if (x > (from_type)max) { \
1967 #define SATCVTU(from, to, from_type, to_type, min, max) \
1968 static inline to_type cvt##from##to(from_type x, int *sat) \
1971 if (x > (from_type)max) { \
1979 SATCVT(sh
, sb
, int16_t, int8_t, INT8_MIN
, INT8_MAX
)
1980 SATCVT(sw
, sh
, int32_t, int16_t, INT16_MIN
, INT16_MAX
)
1981 SATCVT(sd
, sw
, int64_t, int32_t, INT32_MIN
, INT32_MAX
)
1983 SATCVTU(uh
, ub
, uint16_t, uint8_t, 0, UINT8_MAX
)
1984 SATCVTU(uw
, uh
, uint32_t, uint16_t, 0, UINT16_MAX
)
1985 SATCVTU(ud
, uw
, uint64_t, uint32_t, 0, UINT32_MAX
)
1986 SATCVT(sh
, ub
, int16_t, uint8_t, 0, UINT8_MAX
)
1987 SATCVT(sw
, uh
, int32_t, uint16_t, 0, UINT16_MAX
)
1988 SATCVT(sd
, uw
, int64_t, uint32_t, 0, UINT32_MAX
)
1992 #define LVE(name, access, swap, element) \
1993 void helper_##name (ppc_avr_t *r, target_ulong addr) \
1995 size_t n_elems = ARRAY_SIZE(r->element); \
1996 int adjust = HI_IDX*(n_elems-1); \
1997 int sh = sizeof(r->element[0]) >> 1; \
1998 int index = (addr & 0xf) >> sh; \
2000 r->element[LO_IDX ? index : (adjust - index)] = swap(access(addr)); \
2002 r->element[LO_IDX ? index : (adjust - index)] = access(addr); \
2006 LVE(lvebx
, ldub
, I
, u8
)
2007 LVE(lvehx
, lduw
, bswap16
, u16
)
2008 LVE(lvewx
, ldl
, bswap32
, u32
)
2012 void helper_lvsl (ppc_avr_t
*r
, target_ulong sh
)
2014 int i
, j
= (sh
& 0xf);
2016 VECTOR_FOR_INORDER_I (i
, u8
) {
2021 void helper_lvsr (ppc_avr_t
*r
, target_ulong sh
)
2023 int i
, j
= 0x10 - (sh
& 0xf);
2025 VECTOR_FOR_INORDER_I (i
, u8
) {
2030 #define STVE(name, access, swap, element) \
2031 void helper_##name (ppc_avr_t *r, target_ulong addr) \
2033 size_t n_elems = ARRAY_SIZE(r->element); \
2034 int adjust = HI_IDX*(n_elems-1); \
2035 int sh = sizeof(r->element[0]) >> 1; \
2036 int index = (addr & 0xf) >> sh; \
2038 access(addr, swap(r->element[LO_IDX ? index : (adjust - index)])); \
2040 access(addr, r->element[LO_IDX ? index : (adjust - index)]); \
2044 STVE(stvebx
, stb
, I
, u8
)
2045 STVE(stvehx
, stw
, bswap16
, u16
)
2046 STVE(stvewx
, stl
, bswap32
, u32
)
2050 void helper_mtvscr (ppc_avr_t
*r
)
2052 #if defined(HOST_WORDS_BIGENDIAN)
2053 env
->vscr
= r
->u32
[3];
2055 env
->vscr
= r
->u32
[0];
2057 set_flush_to_zero(vscr_nj
, &env
->vec_status
);
2060 void helper_vaddcuw (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2063 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2064 r
->u32
[i
] = ~a
->u32
[i
] < b
->u32
[i
];
2068 #define VARITH_DO(name, op, element) \
2069 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2072 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2073 r->element[i] = a->element[i] op b->element[i]; \
2076 #define VARITH(suffix, element) \
2077 VARITH_DO(add##suffix, +, element) \
2078 VARITH_DO(sub##suffix, -, element)
2085 #define VARITHFP(suffix, func) \
2086 void helper_v##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2089 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2090 HANDLE_NAN2(r->f[i], a->f[i], b->f[i]) { \
2091 r->f[i] = func(a->f[i], b->f[i], &env->vec_status); \
2095 VARITHFP(addfp
, float32_add
)
2096 VARITHFP(subfp
, float32_sub
)
2099 #define VARITHSAT_CASE(type, op, cvt, element) \
2101 type result = (type)a->element[i] op (type)b->element[i]; \
2102 r->element[i] = cvt(result, &sat); \
2105 #define VARITHSAT_DO(name, op, optype, cvt, element) \
2106 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2110 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2111 switch (sizeof(r->element[0])) { \
2112 case 1: VARITHSAT_CASE(optype, op, cvt, element); break; \
2113 case 2: VARITHSAT_CASE(optype, op, cvt, element); break; \
2114 case 4: VARITHSAT_CASE(optype, op, cvt, element); break; \
2118 env->vscr |= (1 << VSCR_SAT); \
2121 #define VARITHSAT_SIGNED(suffix, element, optype, cvt) \
2122 VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element) \
2123 VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
2124 #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt) \
2125 VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element) \
2126 VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
2127 VARITHSAT_SIGNED(b
, s8
, int16_t, cvtshsb
)
2128 VARITHSAT_SIGNED(h
, s16
, int32_t, cvtswsh
)
2129 VARITHSAT_SIGNED(w
, s32
, int64_t, cvtsdsw
)
2130 VARITHSAT_UNSIGNED(b
, u8
, uint16_t, cvtshub
)
2131 VARITHSAT_UNSIGNED(h
, u16
, uint32_t, cvtswuh
)
2132 VARITHSAT_UNSIGNED(w
, u32
, uint64_t, cvtsduw
)
2133 #undef VARITHSAT_CASE
2135 #undef VARITHSAT_SIGNED
2136 #undef VARITHSAT_UNSIGNED
2138 #define VAVG_DO(name, element, etype) \
2139 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2142 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2143 etype x = (etype)a->element[i] + (etype)b->element[i] + 1; \
2144 r->element[i] = x >> 1; \
2148 #define VAVG(type, signed_element, signed_type, unsigned_element, unsigned_type) \
2149 VAVG_DO(avgs##type, signed_element, signed_type) \
2150 VAVG_DO(avgu##type, unsigned_element, unsigned_type)
2151 VAVG(b
, s8
, int16_t, u8
, uint16_t)
2152 VAVG(h
, s16
, int32_t, u16
, uint32_t)
2153 VAVG(w
, s32
, int64_t, u32
, uint64_t)
2157 #define VCF(suffix, cvt, element) \
2158 void helper_vcf##suffix (ppc_avr_t *r, ppc_avr_t *b, uint32_t uim) \
2161 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2162 float32 t = cvt(b->element[i], &env->vec_status); \
2163 r->f[i] = float32_scalbn (t, -uim, &env->vec_status); \
2166 VCF(ux
, uint32_to_float32
, u32
)
2167 VCF(sx
, int32_to_float32
, s32
)
2170 #define VCMP_DO(suffix, compare, element, record) \
2171 void helper_vcmp##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2173 uint32_t ones = (uint32_t)-1; \
2174 uint32_t all = ones; \
2175 uint32_t none = 0; \
2177 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2178 uint32_t result = (a->element[i] compare b->element[i] ? ones : 0x0); \
2179 switch (sizeof (a->element[0])) { \
2180 case 4: r->u32[i] = result; break; \
2181 case 2: r->u16[i] = result; break; \
2182 case 1: r->u8[i] = result; break; \
2188 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
2191 #define VCMP(suffix, compare, element) \
2192 VCMP_DO(suffix, compare, element, 0) \
2193 VCMP_DO(suffix##_dot, compare, element, 1)
2206 #define VCMPFP_DO(suffix, compare, order, record) \
2207 void helper_vcmp##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2209 uint32_t ones = (uint32_t)-1; \
2210 uint32_t all = ones; \
2211 uint32_t none = 0; \
2213 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2215 int rel = float32_compare_quiet(a->f[i], b->f[i], &env->vec_status); \
2216 if (rel == float_relation_unordered) { \
2218 } else if (rel compare order) { \
2223 r->u32[i] = result; \
2228 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
2231 #define VCMPFP(suffix, compare, order) \
2232 VCMPFP_DO(suffix, compare, order, 0) \
2233 VCMPFP_DO(suffix##_dot, compare, order, 1)
2234 VCMPFP(eqfp
, ==, float_relation_equal
)
2235 VCMPFP(gefp
, !=, float_relation_less
)
2236 VCMPFP(gtfp
, ==, float_relation_greater
)
2240 static inline void vcmpbfp_internal(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
2245 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2246 int le_rel
= float32_compare_quiet(a
->f
[i
], b
->f
[i
], &env
->vec_status
);
2247 if (le_rel
== float_relation_unordered
) {
2248 r
->u32
[i
] = 0xc0000000;
2249 /* ALL_IN does not need to be updated here. */
2251 float32 bneg
= float32_chs(b
->f
[i
]);
2252 int ge_rel
= float32_compare_quiet(a
->f
[i
], bneg
, &env
->vec_status
);
2253 int le
= le_rel
!= float_relation_greater
;
2254 int ge
= ge_rel
!= float_relation_less
;
2255 r
->u32
[i
] = ((!le
) << 31) | ((!ge
) << 30);
2256 all_in
|= (!le
| !ge
);
2260 env
->crf
[6] = (all_in
== 0) << 1;
2264 void helper_vcmpbfp (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2266 vcmpbfp_internal(r
, a
, b
, 0);
2269 void helper_vcmpbfp_dot (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2271 vcmpbfp_internal(r
, a
, b
, 1);
2274 #define VCT(suffix, satcvt, element) \
2275 void helper_vct##suffix (ppc_avr_t *r, ppc_avr_t *b, uint32_t uim) \
2279 float_status s = env->vec_status; \
2280 set_float_rounding_mode(float_round_to_zero, &s); \
2281 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2282 if (float32_is_any_nan(b->f[i])) { \
2283 r->element[i] = 0; \
2285 float64 t = float32_to_float64(b->f[i], &s); \
2287 t = float64_scalbn(t, uim, &s); \
2288 j = float64_to_int64(t, &s); \
2289 r->element[i] = satcvt(j, &sat); \
2293 env->vscr |= (1 << VSCR_SAT); \
2296 VCT(uxs
, cvtsduw
, u32
)
2297 VCT(sxs
, cvtsdsw
, s32
)
2300 void helper_vmaddfp (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2303 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2304 HANDLE_NAN3(r
->f
[i
], a
->f
[i
], b
->f
[i
], c
->f
[i
]) {
2305 /* Need to do the computation in higher precision and round
2306 * once at the end. */
2307 float64 af
, bf
, cf
, t
;
2308 af
= float32_to_float64(a
->f
[i
], &env
->vec_status
);
2309 bf
= float32_to_float64(b
->f
[i
], &env
->vec_status
);
2310 cf
= float32_to_float64(c
->f
[i
], &env
->vec_status
);
2311 t
= float64_mul(af
, cf
, &env
->vec_status
);
2312 t
= float64_add(t
, bf
, &env
->vec_status
);
2313 r
->f
[i
] = float64_to_float32(t
, &env
->vec_status
);
2318 void helper_vmhaddshs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2323 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2324 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
2325 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
2326 r
->s16
[i
] = cvtswsh (t
, &sat
);
2330 env
->vscr
|= (1 << VSCR_SAT
);
2334 void helper_vmhraddshs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2339 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2340 int32_t prod
= a
->s16
[i
] * b
->s16
[i
] + 0x00004000;
2341 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
2342 r
->s16
[i
] = cvtswsh (t
, &sat
);
2346 env
->vscr
|= (1 << VSCR_SAT
);
2350 #define VMINMAX_DO(name, compare, element) \
2351 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2354 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2355 if (a->element[i] compare b->element[i]) { \
2356 r->element[i] = b->element[i]; \
2358 r->element[i] = a->element[i]; \
2362 #define VMINMAX(suffix, element) \
2363 VMINMAX_DO(min##suffix, >, element) \
2364 VMINMAX_DO(max##suffix, <, element)
2374 #define VMINMAXFP(suffix, rT, rF) \
2375 void helper_v##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2378 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2379 HANDLE_NAN2(r->f[i], a->f[i], b->f[i]) { \
2380 if (float32_lt_quiet(a->f[i], b->f[i], &env->vec_status)) { \
2381 r->f[i] = rT->f[i]; \
2383 r->f[i] = rF->f[i]; \
2388 VMINMAXFP(minfp
, a
, b
)
2389 VMINMAXFP(maxfp
, b
, a
)
2392 void helper_vmladduhm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2395 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2396 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
2397 r
->s16
[i
] = (int16_t) (prod
+ c
->s16
[i
]);
2401 #define VMRG_DO(name, element, highp) \
2402 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2406 size_t n_elems = ARRAY_SIZE(r->element); \
2407 for (i = 0; i < n_elems/2; i++) { \
2409 result.element[i*2+HI_IDX] = a->element[i]; \
2410 result.element[i*2+LO_IDX] = b->element[i]; \
2412 result.element[n_elems - i*2 - (1+HI_IDX)] = b->element[n_elems - i - 1]; \
2413 result.element[n_elems - i*2 - (1+LO_IDX)] = a->element[n_elems - i - 1]; \
2418 #if defined(HOST_WORDS_BIGENDIAN)
2425 #define VMRG(suffix, element) \
2426 VMRG_DO(mrgl##suffix, element, MRGHI) \
2427 VMRG_DO(mrgh##suffix, element, MRGLO)
2436 void helper_vmsummbm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2441 for (i
= 0; i
< ARRAY_SIZE(r
->s8
); i
++) {
2442 prod
[i
] = (int32_t)a
->s8
[i
] * b
->u8
[i
];
2445 VECTOR_FOR_INORDER_I(i
, s32
) {
2446 r
->s32
[i
] = c
->s32
[i
] + prod
[4*i
] + prod
[4*i
+1] + prod
[4*i
+2] + prod
[4*i
+3];
2450 void helper_vmsumshm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2455 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2456 prod
[i
] = a
->s16
[i
] * b
->s16
[i
];
2459 VECTOR_FOR_INORDER_I(i
, s32
) {
2460 r
->s32
[i
] = c
->s32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2464 void helper_vmsumshs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2470 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2471 prod
[i
] = (int32_t)a
->s16
[i
] * b
->s16
[i
];
2474 VECTOR_FOR_INORDER_I (i
, s32
) {
2475 int64_t t
= (int64_t)c
->s32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2476 r
->u32
[i
] = cvtsdsw(t
, &sat
);
2480 env
->vscr
|= (1 << VSCR_SAT
);
2484 void helper_vmsumubm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2489 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2490 prod
[i
] = a
->u8
[i
] * b
->u8
[i
];
2493 VECTOR_FOR_INORDER_I(i
, u32
) {
2494 r
->u32
[i
] = c
->u32
[i
] + prod
[4*i
] + prod
[4*i
+1] + prod
[4*i
+2] + prod
[4*i
+3];
2498 void helper_vmsumuhm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2503 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
2504 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
2507 VECTOR_FOR_INORDER_I(i
, u32
) {
2508 r
->u32
[i
] = c
->u32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2512 void helper_vmsumuhs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2518 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
2519 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
2522 VECTOR_FOR_INORDER_I (i
, s32
) {
2523 uint64_t t
= (uint64_t)c
->u32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2524 r
->u32
[i
] = cvtuduw(t
, &sat
);
2528 env
->vscr
|= (1 << VSCR_SAT
);
2532 #define VMUL_DO(name, mul_element, prod_element, evenp) \
2533 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2536 VECTOR_FOR_INORDER_I(i, prod_element) { \
2538 r->prod_element[i] = a->mul_element[i*2+HI_IDX] * b->mul_element[i*2+HI_IDX]; \
2540 r->prod_element[i] = a->mul_element[i*2+LO_IDX] * b->mul_element[i*2+LO_IDX]; \
2544 #define VMUL(suffix, mul_element, prod_element) \
2545 VMUL_DO(mule##suffix, mul_element, prod_element, 1) \
2546 VMUL_DO(mulo##suffix, mul_element, prod_element, 0)
2554 void helper_vnmsubfp (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2557 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2558 HANDLE_NAN3(r
->f
[i
], a
->f
[i
], b
->f
[i
], c
->f
[i
]) {
2559 /* Need to do the computation is higher precision and round
2560 * once at the end. */
2561 float64 af
, bf
, cf
, t
;
2562 af
= float32_to_float64(a
->f
[i
], &env
->vec_status
);
2563 bf
= float32_to_float64(b
->f
[i
], &env
->vec_status
);
2564 cf
= float32_to_float64(c
->f
[i
], &env
->vec_status
);
2565 t
= float64_mul(af
, cf
, &env
->vec_status
);
2566 t
= float64_sub(t
, bf
, &env
->vec_status
);
2568 r
->f
[i
] = float64_to_float32(t
, &env
->vec_status
);
2573 void helper_vperm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2577 VECTOR_FOR_INORDER_I (i
, u8
) {
2578 int s
= c
->u8
[i
] & 0x1f;
2579 #if defined(HOST_WORDS_BIGENDIAN)
2580 int index
= s
& 0xf;
2582 int index
= 15 - (s
& 0xf);
2585 result
.u8
[i
] = b
->u8
[index
];
2587 result
.u8
[i
] = a
->u8
[index
];
2593 #if defined(HOST_WORDS_BIGENDIAN)
2598 void helper_vpkpx (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2602 #if defined(HOST_WORDS_BIGENDIAN)
2603 const ppc_avr_t
*x
[2] = { a
, b
};
2605 const ppc_avr_t
*x
[2] = { b
, a
};
2608 VECTOR_FOR_INORDER_I (i
, u64
) {
2609 VECTOR_FOR_INORDER_I (j
, u32
){
2610 uint32_t e
= x
[i
]->u32
[j
];
2611 result
.u16
[4*i
+j
] = (((e
>> 9) & 0xfc00) |
2612 ((e
>> 6) & 0x3e0) |
2619 #define VPK(suffix, from, to, cvt, dosat) \
2620 void helper_vpk##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2625 ppc_avr_t *a0 = PKBIG ? a : b; \
2626 ppc_avr_t *a1 = PKBIG ? b : a; \
2627 VECTOR_FOR_INORDER_I (i, from) { \
2628 result.to[i] = cvt(a0->from[i], &sat); \
2629 result.to[i+ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat); \
2632 if (dosat && sat) { \
2633 env->vscr |= (1 << VSCR_SAT); \
2637 VPK(shss
, s16
, s8
, cvtshsb
, 1)
2638 VPK(shus
, s16
, u8
, cvtshub
, 1)
2639 VPK(swss
, s32
, s16
, cvtswsh
, 1)
2640 VPK(swus
, s32
, u16
, cvtswuh
, 1)
2641 VPK(uhus
, u16
, u8
, cvtuhub
, 1)
2642 VPK(uwus
, u32
, u16
, cvtuwuh
, 1)
2643 VPK(uhum
, u16
, u8
, I
, 0)
2644 VPK(uwum
, u32
, u16
, I
, 0)
2649 void helper_vrefp (ppc_avr_t
*r
, ppc_avr_t
*b
)
2652 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2653 HANDLE_NAN1(r
->f
[i
], b
->f
[i
]) {
2654 r
->f
[i
] = float32_div(float32_one
, b
->f
[i
], &env
->vec_status
);
2659 #define VRFI(suffix, rounding) \
2660 void helper_vrfi##suffix (ppc_avr_t *r, ppc_avr_t *b) \
2663 float_status s = env->vec_status; \
2664 set_float_rounding_mode(rounding, &s); \
2665 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2666 HANDLE_NAN1(r->f[i], b->f[i]) { \
2667 r->f[i] = float32_round_to_int (b->f[i], &s); \
2671 VRFI(n
, float_round_nearest_even
)
2672 VRFI(m
, float_round_down
)
2673 VRFI(p
, float_round_up
)
2674 VRFI(z
, float_round_to_zero
)
2677 #define VROTATE(suffix, element) \
2678 void helper_vrl##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2681 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2682 unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2683 unsigned int shift = b->element[i] & mask; \
2684 r->element[i] = (a->element[i] << shift) | (a->element[i] >> (sizeof(a->element[0]) * 8 - shift)); \
2692 void helper_vrsqrtefp (ppc_avr_t
*r
, ppc_avr_t
*b
)
2695 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2696 HANDLE_NAN1(r
->f
[i
], b
->f
[i
]) {
2697 float32 t
= float32_sqrt(b
->f
[i
], &env
->vec_status
);
2698 r
->f
[i
] = float32_div(float32_one
, t
, &env
->vec_status
);
2703 void helper_vsel (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2705 r
->u64
[0] = (a
->u64
[0] & ~c
->u64
[0]) | (b
->u64
[0] & c
->u64
[0]);
2706 r
->u64
[1] = (a
->u64
[1] & ~c
->u64
[1]) | (b
->u64
[1] & c
->u64
[1]);
2709 void helper_vexptefp (ppc_avr_t
*r
, ppc_avr_t
*b
)
2712 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2713 HANDLE_NAN1(r
->f
[i
], b
->f
[i
]) {
2714 r
->f
[i
] = float32_exp2(b
->f
[i
], &env
->vec_status
);
2719 void helper_vlogefp (ppc_avr_t
*r
, ppc_avr_t
*b
)
2722 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2723 HANDLE_NAN1(r
->f
[i
], b
->f
[i
]) {
2724 r
->f
[i
] = float32_log2(b
->f
[i
], &env
->vec_status
);
2729 #if defined(HOST_WORDS_BIGENDIAN)
2736 /* The specification says that the results are undefined if all of the
2737 * shift counts are not identical. We check to make sure that they are
2738 * to conform to what real hardware appears to do. */
2739 #define VSHIFT(suffix, leftp) \
2740 void helper_vs##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2742 int shift = b->u8[LO_IDX*15] & 0x7; \
2745 for (i = 0; i < ARRAY_SIZE(r->u8); i++) { \
2746 doit = doit && ((b->u8[i] & 0x7) == shift); \
2751 } else if (leftp) { \
2752 uint64_t carry = a->u64[LO_IDX] >> (64 - shift); \
2753 r->u64[HI_IDX] = (a->u64[HI_IDX] << shift) | carry; \
2754 r->u64[LO_IDX] = a->u64[LO_IDX] << shift; \
2756 uint64_t carry = a->u64[HI_IDX] << (64 - shift); \
2757 r->u64[LO_IDX] = (a->u64[LO_IDX] >> shift) | carry; \
2758 r->u64[HI_IDX] = a->u64[HI_IDX] >> shift; \
2768 #define VSL(suffix, element) \
2769 void helper_vsl##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2772 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2773 unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2774 unsigned int shift = b->element[i] & mask; \
2775 r->element[i] = a->element[i] << shift; \
2783 void helper_vsldoi (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t shift
)
2785 int sh
= shift
& 0xf;
2789 #if defined(HOST_WORDS_BIGENDIAN)
2790 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2793 result
.u8
[i
] = b
->u8
[index
-0x10];
2795 result
.u8
[i
] = a
->u8
[index
];
2799 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2800 int index
= (16 - sh
) + i
;
2802 result
.u8
[i
] = a
->u8
[index
-0x10];
2804 result
.u8
[i
] = b
->u8
[index
];
2811 void helper_vslo (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2813 int sh
= (b
->u8
[LO_IDX
*0xf] >> 3) & 0xf;
2815 #if defined (HOST_WORDS_BIGENDIAN)
2816 memmove (&r
->u8
[0], &a
->u8
[sh
], 16-sh
);
2817 memset (&r
->u8
[16-sh
], 0, sh
);
2819 memmove (&r
->u8
[sh
], &a
->u8
[0], 16-sh
);
2820 memset (&r
->u8
[0], 0, sh
);
2824 /* Experimental testing shows that hardware masks the immediate. */
2825 #define _SPLAT_MASKED(element) (splat & (ARRAY_SIZE(r->element) - 1))
2826 #if defined(HOST_WORDS_BIGENDIAN)
2827 #define SPLAT_ELEMENT(element) _SPLAT_MASKED(element)
2829 #define SPLAT_ELEMENT(element) (ARRAY_SIZE(r->element)-1 - _SPLAT_MASKED(element))
2831 #define VSPLT(suffix, element) \
2832 void helper_vsplt##suffix (ppc_avr_t *r, ppc_avr_t *b, uint32_t splat) \
2834 uint32_t s = b->element[SPLAT_ELEMENT(element)]; \
2836 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2837 r->element[i] = s; \
2844 #undef SPLAT_ELEMENT
2845 #undef _SPLAT_MASKED
2847 #define VSPLTI(suffix, element, splat_type) \
2848 void helper_vspltis##suffix (ppc_avr_t *r, uint32_t splat) \
2850 splat_type x = (int8_t)(splat << 3) >> 3; \
2852 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2853 r->element[i] = x; \
2856 VSPLTI(b
, s8
, int8_t)
2857 VSPLTI(h
, s16
, int16_t)
2858 VSPLTI(w
, s32
, int32_t)
2861 #define VSR(suffix, element) \
2862 void helper_vsr##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2865 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2866 unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2867 unsigned int shift = b->element[i] & mask; \
2868 r->element[i] = a->element[i] >> shift; \
2879 void helper_vsro (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2881 int sh
= (b
->u8
[LO_IDX
*0xf] >> 3) & 0xf;
2883 #if defined (HOST_WORDS_BIGENDIAN)
2884 memmove (&r
->u8
[sh
], &a
->u8
[0], 16-sh
);
2885 memset (&r
->u8
[0], 0, sh
);
2887 memmove (&r
->u8
[0], &a
->u8
[sh
], 16-sh
);
2888 memset (&r
->u8
[16-sh
], 0, sh
);
2892 void helper_vsubcuw (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2895 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2896 r
->u32
[i
] = a
->u32
[i
] >= b
->u32
[i
];
2900 void helper_vsumsws (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2907 #if defined(HOST_WORDS_BIGENDIAN)
2908 upper
= ARRAY_SIZE(r
->s32
)-1;
2912 t
= (int64_t)b
->s32
[upper
];
2913 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
2917 result
.s32
[upper
] = cvtsdsw(t
, &sat
);
2921 env
->vscr
|= (1 << VSCR_SAT
);
2925 void helper_vsum2sws (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2931 #if defined(HOST_WORDS_BIGENDIAN)
2936 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
2937 int64_t t
= (int64_t)b
->s32
[upper
+i
*2];
2939 for (j
= 0; j
< ARRAY_SIZE(r
->u64
); j
++) {
2942 result
.s32
[upper
+i
*2] = cvtsdsw(t
, &sat
);
2947 env
->vscr
|= (1 << VSCR_SAT
);
2951 void helper_vsum4sbs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2956 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
2957 int64_t t
= (int64_t)b
->s32
[i
];
2958 for (j
= 0; j
< ARRAY_SIZE(r
->s32
); j
++) {
2961 r
->s32
[i
] = cvtsdsw(t
, &sat
);
2965 env
->vscr
|= (1 << VSCR_SAT
);
2969 void helper_vsum4shs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2974 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
2975 int64_t t
= (int64_t)b
->s32
[i
];
2976 t
+= a
->s16
[2*i
] + a
->s16
[2*i
+1];
2977 r
->s32
[i
] = cvtsdsw(t
, &sat
);
2981 env
->vscr
|= (1 << VSCR_SAT
);
2985 void helper_vsum4ubs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2990 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2991 uint64_t t
= (uint64_t)b
->u32
[i
];
2992 for (j
= 0; j
< ARRAY_SIZE(r
->u32
); j
++) {
2995 r
->u32
[i
] = cvtuduw(t
, &sat
);
2999 env
->vscr
|= (1 << VSCR_SAT
);
3003 #if defined(HOST_WORDS_BIGENDIAN)
3010 #define VUPKPX(suffix, hi) \
3011 void helper_vupk##suffix (ppc_avr_t *r, ppc_avr_t *b) \
3015 for (i = 0; i < ARRAY_SIZE(r->u32); i++) { \
3016 uint16_t e = b->u16[hi ? i : i+4]; \
3017 uint8_t a = (e >> 15) ? 0xff : 0; \
3018 uint8_t r = (e >> 10) & 0x1f; \
3019 uint8_t g = (e >> 5) & 0x1f; \
3020 uint8_t b = e & 0x1f; \
3021 result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b; \
3029 #define VUPK(suffix, unpacked, packee, hi) \
3030 void helper_vupk##suffix (ppc_avr_t *r, ppc_avr_t *b) \
3035 for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) { \
3036 result.unpacked[i] = b->packee[i]; \
3039 for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); i++) { \
3040 result.unpacked[i-ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
3045 VUPK(hsb
, s16
, s8
, UPKHI
)
3046 VUPK(hsh
, s32
, s16
, UPKHI
)
3047 VUPK(lsb
, s16
, s8
, UPKLO
)
3048 VUPK(lsh
, s32
, s16
, UPKLO
)
3053 #undef DO_HANDLE_NAN
3057 #undef VECTOR_FOR_INORDER_I
3061 /*****************************************************************************/
3062 /* SPE extension helpers */
3063 /* Use a table to make this quicker */
3064 static uint8_t hbrev
[16] = {
3065 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
3066 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
3069 static inline uint8_t byte_reverse(uint8_t val
)
3071 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
3074 static inline uint32_t word_reverse(uint32_t val
)
3076 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
3077 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
3080 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
3081 target_ulong
helper_brinc (target_ulong arg1
, target_ulong arg2
)
3083 uint32_t a
, b
, d
, mask
;
3085 mask
= UINT32_MAX
>> (32 - MASKBITS
);
3088 d
= word_reverse(1 + word_reverse(a
| ~b
));
3089 return (arg1
& ~mask
) | (d
& b
);
3092 uint32_t helper_cntlsw32 (uint32_t val
)
3094 if (val
& 0x80000000)
3100 uint32_t helper_cntlzw32 (uint32_t val
)
3105 /* Single-precision floating-point conversions */
3106 static inline uint32_t efscfsi(uint32_t val
)
3110 u
.f
= int32_to_float32(val
, &env
->vec_status
);
3115 static inline uint32_t efscfui(uint32_t val
)
3119 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
3124 static inline int32_t efsctsi(uint32_t val
)
3129 /* NaN are not treated the same way IEEE 754 does */
3130 if (unlikely(float32_is_quiet_nan(u
.f
)))
3133 return float32_to_int32(u
.f
, &env
->vec_status
);
3136 static inline uint32_t efsctui(uint32_t val
)
3141 /* NaN are not treated the same way IEEE 754 does */
3142 if (unlikely(float32_is_quiet_nan(u
.f
)))
3145 return float32_to_uint32(u
.f
, &env
->vec_status
);
3148 static inline uint32_t efsctsiz(uint32_t val
)
3153 /* NaN are not treated the same way IEEE 754 does */
3154 if (unlikely(float32_is_quiet_nan(u
.f
)))
3157 return float32_to_int32_round_to_zero(u
.f
, &env
->vec_status
);
3160 static inline uint32_t efsctuiz(uint32_t val
)
3165 /* NaN are not treated the same way IEEE 754 does */
3166 if (unlikely(float32_is_quiet_nan(u
.f
)))
3169 return float32_to_uint32_round_to_zero(u
.f
, &env
->vec_status
);
3172 static inline uint32_t efscfsf(uint32_t val
)
3177 u
.f
= int32_to_float32(val
, &env
->vec_status
);
3178 tmp
= int64_to_float32(1ULL << 32, &env
->vec_status
);
3179 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
3184 static inline uint32_t efscfuf(uint32_t val
)
3189 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
3190 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
3191 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
3196 static inline uint32_t efsctsf(uint32_t val
)
3202 /* NaN are not treated the same way IEEE 754 does */
3203 if (unlikely(float32_is_quiet_nan(u
.f
)))
3205 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
3206 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
3208 return float32_to_int32(u
.f
, &env
->vec_status
);
3211 static inline uint32_t efsctuf(uint32_t val
)
3217 /* NaN are not treated the same way IEEE 754 does */
3218 if (unlikely(float32_is_quiet_nan(u
.f
)))
3220 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
3221 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
3223 return float32_to_uint32(u
.f
, &env
->vec_status
);
3226 #define HELPER_SPE_SINGLE_CONV(name) \
3227 uint32_t helper_e##name (uint32_t val) \
3229 return e##name(val); \
3232 HELPER_SPE_SINGLE_CONV(fscfsi
);
3234 HELPER_SPE_SINGLE_CONV(fscfui
);
3236 HELPER_SPE_SINGLE_CONV(fscfuf
);
3238 HELPER_SPE_SINGLE_CONV(fscfsf
);
3240 HELPER_SPE_SINGLE_CONV(fsctsi
);
3242 HELPER_SPE_SINGLE_CONV(fsctui
);
3244 HELPER_SPE_SINGLE_CONV(fsctsiz
);
3246 HELPER_SPE_SINGLE_CONV(fsctuiz
);
3248 HELPER_SPE_SINGLE_CONV(fsctsf
);
3250 HELPER_SPE_SINGLE_CONV(fsctuf
);
3252 #define HELPER_SPE_VECTOR_CONV(name) \
3253 uint64_t helper_ev##name (uint64_t val) \
3255 return ((uint64_t)e##name(val >> 32) << 32) | \
3256 (uint64_t)e##name(val); \
3259 HELPER_SPE_VECTOR_CONV(fscfsi
);
3261 HELPER_SPE_VECTOR_CONV(fscfui
);
3263 HELPER_SPE_VECTOR_CONV(fscfuf
);
3265 HELPER_SPE_VECTOR_CONV(fscfsf
);
3267 HELPER_SPE_VECTOR_CONV(fsctsi
);
3269 HELPER_SPE_VECTOR_CONV(fsctui
);
3271 HELPER_SPE_VECTOR_CONV(fsctsiz
);
3273 HELPER_SPE_VECTOR_CONV(fsctuiz
);
3275 HELPER_SPE_VECTOR_CONV(fsctsf
);
3277 HELPER_SPE_VECTOR_CONV(fsctuf
);
3279 /* Single-precision floating-point arithmetic */
3280 static inline uint32_t efsadd(uint32_t op1
, uint32_t op2
)
3285 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->vec_status
);
3289 static inline uint32_t efssub(uint32_t op1
, uint32_t op2
)
3294 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->vec_status
);
3298 static inline uint32_t efsmul(uint32_t op1
, uint32_t op2
)
3303 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->vec_status
);
3307 static inline uint32_t efsdiv(uint32_t op1
, uint32_t op2
)
3312 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->vec_status
);
3316 #define HELPER_SPE_SINGLE_ARITH(name) \
3317 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
3319 return e##name(op1, op2); \
3322 HELPER_SPE_SINGLE_ARITH(fsadd
);
3324 HELPER_SPE_SINGLE_ARITH(fssub
);
3326 HELPER_SPE_SINGLE_ARITH(fsmul
);
3328 HELPER_SPE_SINGLE_ARITH(fsdiv
);
3330 #define HELPER_SPE_VECTOR_ARITH(name) \
3331 uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
3333 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
3334 (uint64_t)e##name(op1, op2); \
3337 HELPER_SPE_VECTOR_ARITH(fsadd
);
3339 HELPER_SPE_VECTOR_ARITH(fssub
);
3341 HELPER_SPE_VECTOR_ARITH(fsmul
);
3343 HELPER_SPE_VECTOR_ARITH(fsdiv
);
3345 /* Single-precision floating-point comparisons */
3346 static inline uint32_t efststlt(uint32_t op1
, uint32_t op2
)
3351 return float32_lt(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
3354 static inline uint32_t efststgt(uint32_t op1
, uint32_t op2
)
3359 return float32_le(u1
.f
, u2
.f
, &env
->vec_status
) ? 0 : 4;
3362 static inline uint32_t efststeq(uint32_t op1
, uint32_t op2
)
3367 return float32_eq(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
3370 static inline uint32_t efscmplt(uint32_t op1
, uint32_t op2
)
3372 /* XXX: TODO: test special values (NaN, infinites, ...) */
3373 return efststlt(op1
, op2
);
3376 static inline uint32_t efscmpgt(uint32_t op1
, uint32_t op2
)
3378 /* XXX: TODO: test special values (NaN, infinites, ...) */
3379 return efststgt(op1
, op2
);
3382 static inline uint32_t efscmpeq(uint32_t op1
, uint32_t op2
)
3384 /* XXX: TODO: test special values (NaN, infinites, ...) */
3385 return efststeq(op1
, op2
);
3388 #define HELPER_SINGLE_SPE_CMP(name) \
3389 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
3391 return e##name(op1, op2) << 2; \
3394 HELPER_SINGLE_SPE_CMP(fststlt
);
3396 HELPER_SINGLE_SPE_CMP(fststgt
);
3398 HELPER_SINGLE_SPE_CMP(fststeq
);
3400 HELPER_SINGLE_SPE_CMP(fscmplt
);
3402 HELPER_SINGLE_SPE_CMP(fscmpgt
);
3404 HELPER_SINGLE_SPE_CMP(fscmpeq
);
3406 static inline uint32_t evcmp_merge(int t0
, int t1
)
3408 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
3411 #define HELPER_VECTOR_SPE_CMP(name) \
3412 uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
3414 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
3417 HELPER_VECTOR_SPE_CMP(fststlt
);
3419 HELPER_VECTOR_SPE_CMP(fststgt
);
3421 HELPER_VECTOR_SPE_CMP(fststeq
);
3423 HELPER_VECTOR_SPE_CMP(fscmplt
);
3425 HELPER_VECTOR_SPE_CMP(fscmpgt
);
3427 HELPER_VECTOR_SPE_CMP(fscmpeq
);
3429 /* Double-precision floating-point conversion */
3430 uint64_t helper_efdcfsi (uint32_t val
)
3434 u
.d
= int32_to_float64(val
, &env
->vec_status
);
3439 uint64_t helper_efdcfsid (uint64_t val
)
3443 u
.d
= int64_to_float64(val
, &env
->vec_status
);
3448 uint64_t helper_efdcfui (uint32_t val
)
3452 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
3457 uint64_t helper_efdcfuid (uint64_t val
)
3461 u
.d
= uint64_to_float64(val
, &env
->vec_status
);
3466 uint32_t helper_efdctsi (uint64_t val
)
3471 /* NaN are not treated the same way IEEE 754 does */
3472 if (unlikely(float64_is_any_nan(u
.d
))) {
3476 return float64_to_int32(u
.d
, &env
->vec_status
);
3479 uint32_t helper_efdctui (uint64_t val
)
3484 /* NaN are not treated the same way IEEE 754 does */
3485 if (unlikely(float64_is_any_nan(u
.d
))) {
3489 return float64_to_uint32(u
.d
, &env
->vec_status
);
3492 uint32_t helper_efdctsiz (uint64_t val
)
3497 /* NaN are not treated the same way IEEE 754 does */
3498 if (unlikely(float64_is_any_nan(u
.d
))) {
3502 return float64_to_int32_round_to_zero(u
.d
, &env
->vec_status
);
3505 uint64_t helper_efdctsidz (uint64_t val
)
3510 /* NaN are not treated the same way IEEE 754 does */
3511 if (unlikely(float64_is_any_nan(u
.d
))) {
3515 return float64_to_int64_round_to_zero(u
.d
, &env
->vec_status
);
3518 uint32_t helper_efdctuiz (uint64_t val
)
3523 /* NaN are not treated the same way IEEE 754 does */
3524 if (unlikely(float64_is_any_nan(u
.d
))) {
3528 return float64_to_uint32_round_to_zero(u
.d
, &env
->vec_status
);
3531 uint64_t helper_efdctuidz (uint64_t val
)
3536 /* NaN are not treated the same way IEEE 754 does */
3537 if (unlikely(float64_is_any_nan(u
.d
))) {
3541 return float64_to_uint64_round_to_zero(u
.d
, &env
->vec_status
);
3544 uint64_t helper_efdcfsf (uint32_t val
)
3549 u
.d
= int32_to_float64(val
, &env
->vec_status
);
3550 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
3551 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
3556 uint64_t helper_efdcfuf (uint32_t val
)
3561 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
3562 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
3563 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
3568 uint32_t helper_efdctsf (uint64_t val
)
3574 /* NaN are not treated the same way IEEE 754 does */
3575 if (unlikely(float64_is_any_nan(u
.d
))) {
3578 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
3579 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
3581 return float64_to_int32(u
.d
, &env
->vec_status
);
3584 uint32_t helper_efdctuf (uint64_t val
)
3590 /* NaN are not treated the same way IEEE 754 does */
3591 if (unlikely(float64_is_any_nan(u
.d
))) {
3594 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
3595 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
3597 return float64_to_uint32(u
.d
, &env
->vec_status
);
3600 uint32_t helper_efscfd (uint64_t val
)
3606 u2
.f
= float64_to_float32(u1
.d
, &env
->vec_status
);
3611 uint64_t helper_efdcfs (uint32_t val
)
3617 u2
.d
= float32_to_float64(u1
.f
, &env
->vec_status
);
3622 /* Double precision fixed-point arithmetic */
3623 uint64_t helper_efdadd (uint64_t op1
, uint64_t op2
)
3628 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->vec_status
);
3632 uint64_t helper_efdsub (uint64_t op1
, uint64_t op2
)
3637 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->vec_status
);
3641 uint64_t helper_efdmul (uint64_t op1
, uint64_t op2
)
3646 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->vec_status
);
3650 uint64_t helper_efddiv (uint64_t op1
, uint64_t op2
)
3655 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->vec_status
);
3659 /* Double precision floating point helpers */
3660 uint32_t helper_efdtstlt (uint64_t op1
, uint64_t op2
)
3665 return float64_lt(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
3668 uint32_t helper_efdtstgt (uint64_t op1
, uint64_t op2
)
3673 return float64_le(u1
.d
, u2
.d
, &env
->vec_status
) ? 0 : 4;
3676 uint32_t helper_efdtsteq (uint64_t op1
, uint64_t op2
)
3681 return float64_eq(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
3684 uint32_t helper_efdcmplt (uint64_t op1
, uint64_t op2
)
3686 /* XXX: TODO: test special values (NaN, infinites, ...) */
3687 return helper_efdtstlt(op1
, op2
);
3690 uint32_t helper_efdcmpgt (uint64_t op1
, uint64_t op2
)
3692 /* XXX: TODO: test special values (NaN, infinites, ...) */
3693 return helper_efdtstgt(op1
, op2
);
3696 uint32_t helper_efdcmpeq (uint64_t op1
, uint64_t op2
)
3698 /* XXX: TODO: test special values (NaN, infinites, ...) */
3699 return helper_efdtsteq(op1
, op2
);
3702 /*****************************************************************************/
3703 /* Softmmu support */
3704 #if !defined (CONFIG_USER_ONLY)
3706 #define MMUSUFFIX _mmu
3709 #include "softmmu_template.h"
3712 #include "softmmu_template.h"
3715 #include "softmmu_template.h"
3718 #include "softmmu_template.h"
3720 /* try to fill the TLB and return an exception if error. If retaddr is
3721 NULL, it means that the function was called in C code (i.e. not
3722 from generated code or from helper.c) */
3723 /* XXX: fix it to restore all registers */
3724 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
3726 TranslationBlock
*tb
;
3727 CPUState
*saved_env
;
3731 /* XXX: hack to restore env in all cases, even if not called from
3734 env
= cpu_single_env
;
3735 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
3736 if (unlikely(ret
!= 0)) {
3737 if (likely(retaddr
)) {
3738 /* now we have a real cpu fault */
3739 pc
= (unsigned long)retaddr
;
3740 tb
= tb_find_pc(pc
);
3742 /* the PC is inside the translated code. It means that we have
3743 a virtual CPU fault */
3744 cpu_restore_state(tb
, env
, pc
, NULL
);
3747 helper_raise_exception_err(env
->exception_index
, env
->error_code
);
3752 /* Segment registers load and store */
3753 target_ulong
helper_load_sr (target_ulong sr_num
)
3755 #if defined(TARGET_PPC64)
3756 if (env
->mmu_model
& POWERPC_MMU_64
)
3757 return ppc_load_sr(env
, sr_num
);
3759 return env
->sr
[sr_num
];
3762 void helper_store_sr (target_ulong sr_num
, target_ulong val
)
3764 ppc_store_sr(env
, sr_num
, val
);
3767 /* SLB management */
3768 #if defined(TARGET_PPC64)
3769 void helper_store_slb (target_ulong rb
, target_ulong rs
)
3771 if (ppc_store_slb(env
, rb
, rs
) < 0) {
3772 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_INVAL
);
3776 target_ulong
helper_load_slb_esid (target_ulong rb
)
3780 if (ppc_load_slb_esid(env
, rb
, &rt
) < 0) {
3781 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_INVAL
);
3786 target_ulong
helper_load_slb_vsid (target_ulong rb
)
3790 if (ppc_load_slb_vsid(env
, rb
, &rt
) < 0) {
3791 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_INVAL
);
3796 void helper_slbia (void)
3798 ppc_slb_invalidate_all(env
);
3801 void helper_slbie (target_ulong addr
)
3803 ppc_slb_invalidate_one(env
, addr
);
3806 #endif /* defined(TARGET_PPC64) */
3808 /* TLB management */
3809 void helper_tlbia (void)
3811 ppc_tlb_invalidate_all(env
);
3814 void helper_tlbie (target_ulong addr
)
3816 ppc_tlb_invalidate_one(env
, addr
);
3819 /* Software driven TLBs management */
3820 /* PowerPC 602/603 software TLB load instructions helpers */
3821 static void do_6xx_tlb (target_ulong new_EPN
, int is_code
)
3823 target_ulong RPN
, CMP
, EPN
;
3826 RPN
= env
->spr
[SPR_RPA
];
3828 CMP
= env
->spr
[SPR_ICMP
];
3829 EPN
= env
->spr
[SPR_IMISS
];
3831 CMP
= env
->spr
[SPR_DCMP
];
3832 EPN
= env
->spr
[SPR_DMISS
];
3834 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
3835 (void)EPN
; /* avoid a compiler warning */
3836 LOG_SWTLB("%s: EPN " TARGET_FMT_lx
" " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
3837 " PTE1 " TARGET_FMT_lx
" way %d\n", __func__
, new_EPN
, EPN
, CMP
,
3839 /* Store this TLB */
3840 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
3841 way
, is_code
, CMP
, RPN
);
3844 void helper_6xx_tlbd (target_ulong EPN
)
3849 void helper_6xx_tlbi (target_ulong EPN
)
3854 /* PowerPC 74xx software TLB load instructions helpers */
3855 static void do_74xx_tlb (target_ulong new_EPN
, int is_code
)
3857 target_ulong RPN
, CMP
, EPN
;
3860 RPN
= env
->spr
[SPR_PTELO
];
3861 CMP
= env
->spr
[SPR_PTEHI
];
3862 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
3863 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
3864 (void)EPN
; /* avoid a compiler warning */
3865 LOG_SWTLB("%s: EPN " TARGET_FMT_lx
" " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
3866 " PTE1 " TARGET_FMT_lx
" way %d\n", __func__
, new_EPN
, EPN
, CMP
,
3868 /* Store this TLB */
3869 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
3870 way
, is_code
, CMP
, RPN
);
3873 void helper_74xx_tlbd (target_ulong EPN
)
3875 do_74xx_tlb(EPN
, 0);
3878 void helper_74xx_tlbi (target_ulong EPN
)
3880 do_74xx_tlb(EPN
, 1);
3883 static inline target_ulong
booke_tlb_to_page_size(int size
)
3885 return 1024 << (2 * size
);
3888 static inline int booke_page_size_to_tlb(target_ulong page_size
)
3892 switch (page_size
) {
3926 #if defined (TARGET_PPC64)
3927 case 0x000100000000ULL
:
3930 case 0x000400000000ULL
:
3933 case 0x001000000000ULL
:
3936 case 0x004000000000ULL
:
3939 case 0x010000000000ULL
:
3951 /* Helpers for 4xx TLB management */
3952 #define PPC4XX_TLB_ENTRY_MASK 0x0000003f /* Mask for 64 TLB entries */
3954 #define PPC4XX_TLBHI_V 0x00000040
3955 #define PPC4XX_TLBHI_E 0x00000020
3956 #define PPC4XX_TLBHI_SIZE_MIN 0
3957 #define PPC4XX_TLBHI_SIZE_MAX 7
3958 #define PPC4XX_TLBHI_SIZE_DEFAULT 1
3959 #define PPC4XX_TLBHI_SIZE_SHIFT 7
3960 #define PPC4XX_TLBHI_SIZE_MASK 0x00000007
3962 #define PPC4XX_TLBLO_EX 0x00000200
3963 #define PPC4XX_TLBLO_WR 0x00000100
3964 #define PPC4XX_TLBLO_ATTR_MASK 0x000000FF
3965 #define PPC4XX_TLBLO_RPN_MASK 0xFFFFFC00
3967 target_ulong
helper_4xx_tlbre_hi (target_ulong entry
)
3973 entry
&= PPC4XX_TLB_ENTRY_MASK
;
3974 tlb
= &env
->tlb
[entry
].tlbe
;
3976 if (tlb
->prot
& PAGE_VALID
) {
3977 ret
|= PPC4XX_TLBHI_V
;
3979 size
= booke_page_size_to_tlb(tlb
->size
);
3980 if (size
< PPC4XX_TLBHI_SIZE_MIN
|| size
> PPC4XX_TLBHI_SIZE_MAX
) {
3981 size
= PPC4XX_TLBHI_SIZE_DEFAULT
;
3983 ret
|= size
<< PPC4XX_TLBHI_SIZE_SHIFT
;
3984 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
3988 target_ulong
helper_4xx_tlbre_lo (target_ulong entry
)
3993 entry
&= PPC4XX_TLB_ENTRY_MASK
;
3994 tlb
= &env
->tlb
[entry
].tlbe
;
3996 if (tlb
->prot
& PAGE_EXEC
) {
3997 ret
|= PPC4XX_TLBLO_EX
;
3999 if (tlb
->prot
& PAGE_WRITE
) {
4000 ret
|= PPC4XX_TLBLO_WR
;
4005 void helper_4xx_tlbwe_hi (target_ulong entry
, target_ulong val
)
4008 target_ulong page
, end
;
4010 LOG_SWTLB("%s entry %d val " TARGET_FMT_lx
"\n", __func__
, (int)entry
,
4012 entry
&= PPC4XX_TLB_ENTRY_MASK
;
4013 tlb
= &env
->tlb
[entry
].tlbe
;
4014 /* Invalidate previous TLB (if it's valid) */
4015 if (tlb
->prot
& PAGE_VALID
) {
4016 end
= tlb
->EPN
+ tlb
->size
;
4017 LOG_SWTLB("%s: invalidate old TLB %d start " TARGET_FMT_lx
" end "
4018 TARGET_FMT_lx
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
4019 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
) {
4020 tlb_flush_page(env
, page
);
4023 tlb
->size
= booke_tlb_to_page_size((val
>> PPC4XX_TLBHI_SIZE_SHIFT
)
4024 & PPC4XX_TLBHI_SIZE_MASK
);
4025 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
4026 * If this ever occurs, one should use the ppcemb target instead
4027 * of the ppc or ppc64 one
4029 if ((val
& PPC4XX_TLBHI_V
) && tlb
->size
< TARGET_PAGE_SIZE
) {
4030 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
4031 "are not supported (%d)\n",
4032 tlb
->size
, TARGET_PAGE_SIZE
, (int)((val
>> 7) & 0x7));
4034 tlb
->EPN
= val
& ~(tlb
->size
- 1);
4035 if (val
& PPC4XX_TLBHI_V
) {
4036 tlb
->prot
|= PAGE_VALID
;
4037 if (val
& PPC4XX_TLBHI_E
) {
4038 /* XXX: TO BE FIXED */
4040 "Little-endian TLB entries are not supported by now\n");
4043 tlb
->prot
&= ~PAGE_VALID
;
4045 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
4046 LOG_SWTLB("%s: set up TLB %d RPN " TARGET_FMT_plx
" EPN " TARGET_FMT_lx
4047 " size " TARGET_FMT_lx
" prot %c%c%c%c PID %d\n", __func__
,
4048 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
4049 tlb
->prot
& PAGE_READ
? 'r' : '-',
4050 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
4051 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
4052 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
4053 /* Invalidate new TLB (if valid) */
4054 if (tlb
->prot
& PAGE_VALID
) {
4055 end
= tlb
->EPN
+ tlb
->size
;
4056 LOG_SWTLB("%s: invalidate TLB %d start " TARGET_FMT_lx
" end "
4057 TARGET_FMT_lx
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
4058 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
) {
4059 tlb_flush_page(env
, page
);
4064 void helper_4xx_tlbwe_lo (target_ulong entry
, target_ulong val
)
4068 LOG_SWTLB("%s entry %i val " TARGET_FMT_lx
"\n", __func__
, (int)entry
,
4070 entry
&= PPC4XX_TLB_ENTRY_MASK
;
4071 tlb
= &env
->tlb
[entry
].tlbe
;
4072 tlb
->attr
= val
& PPC4XX_TLBLO_ATTR_MASK
;
4073 tlb
->RPN
= val
& PPC4XX_TLBLO_RPN_MASK
;
4074 tlb
->prot
= PAGE_READ
;
4075 if (val
& PPC4XX_TLBLO_EX
) {
4076 tlb
->prot
|= PAGE_EXEC
;
4078 if (val
& PPC4XX_TLBLO_WR
) {
4079 tlb
->prot
|= PAGE_WRITE
;
4081 LOG_SWTLB("%s: set up TLB %d RPN " TARGET_FMT_plx
" EPN " TARGET_FMT_lx
4082 " size " TARGET_FMT_lx
" prot %c%c%c%c PID %d\n", __func__
,
4083 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
4084 tlb
->prot
& PAGE_READ
? 'r' : '-',
4085 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
4086 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
4087 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
4090 target_ulong
helper_4xx_tlbsx (target_ulong address
)
4092 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_40x_PID
]);
4095 /* PowerPC 440 TLB management */
4096 void helper_440_tlbwe (uint32_t word
, target_ulong entry
, target_ulong value
)
4099 target_ulong EPN
, RPN
, size
;
4102 LOG_SWTLB("%s word %d entry %d value " TARGET_FMT_lx
"\n",
4103 __func__
, word
, (int)entry
, value
);
4106 tlb
= &env
->tlb
[entry
].tlbe
;
4109 /* Just here to please gcc */
4111 EPN
= value
& 0xFFFFFC00;
4112 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
4115 size
= booke_tlb_to_page_size((value
>> 4) & 0xF);
4116 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
4120 tlb
->attr
|= (value
>> 8) & 1;
4121 if (value
& 0x200) {
4122 tlb
->prot
|= PAGE_VALID
;
4124 if (tlb
->prot
& PAGE_VALID
) {
4125 tlb
->prot
&= ~PAGE_VALID
;
4129 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
4134 RPN
= value
& 0xFFFFFC0F;
4135 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
4140 tlb
->attr
= (tlb
->attr
& 0x1) | (value
& 0x0000FF00);
4141 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
4143 tlb
->prot
|= PAGE_READ
<< 4;
4145 tlb
->prot
|= PAGE_WRITE
<< 4;
4147 tlb
->prot
|= PAGE_EXEC
<< 4;
4149 tlb
->prot
|= PAGE_READ
;
4151 tlb
->prot
|= PAGE_WRITE
;
4153 tlb
->prot
|= PAGE_EXEC
;
4158 target_ulong
helper_440_tlbre (uint32_t word
, target_ulong entry
)
4165 tlb
= &env
->tlb
[entry
].tlbe
;
4168 /* Just here to please gcc */
4171 size
= booke_page_size_to_tlb(tlb
->size
);
4172 if (size
< 0 || size
> 0xF)
4175 if (tlb
->attr
& 0x1)
4177 if (tlb
->prot
& PAGE_VALID
)
4179 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
4180 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
4186 ret
= tlb
->attr
& ~0x1;
4187 if (tlb
->prot
& (PAGE_READ
<< 4))
4189 if (tlb
->prot
& (PAGE_WRITE
<< 4))
4191 if (tlb
->prot
& (PAGE_EXEC
<< 4))
4193 if (tlb
->prot
& PAGE_READ
)
4195 if (tlb
->prot
& PAGE_WRITE
)
4197 if (tlb
->prot
& PAGE_EXEC
)
4204 target_ulong
helper_440_tlbsx (target_ulong address
)
4206 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_440_MMUCR
] & 0xFF);
4209 #endif /* !CONFIG_USER_ONLY */