2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "host-utils.h"
24 #include "helper_regs.h"
27 //#define DEBUG_EXCEPTIONS
28 //#define DEBUG_SOFTWARE_TLB
30 /*****************************************************************************/
31 /* Exceptions processing helpers */
33 void helper_raise_exception_err (uint32_t exception, uint32_t error_code)
35 raise_exception_err(env, exception, error_code);
38 void helper_raise_debug (void)
40 raise_exception(env, EXCP_DEBUG);
43 /*****************************************************************************/
44 /* Registers load and stores */
45 target_ulong helper_load_cr (void)
47 return (env->crf[0] << 28) |
57 void helper_store_cr (target_ulong val, uint32_t mask)
61 for (i = 0, sh = 7; i < 8; i++, sh--) {
63 env->crf[i] = (val >> (sh * 4)) & 0xFUL;
67 /*****************************************************************************/
69 void helper_load_dump_spr (uint32_t sprn)
72 fprintf(logfile, "Read SPR %d %03x => " ADDRX "\n",
73 sprn, sprn, env->spr[sprn]);
77 void helper_store_dump_spr (uint32_t sprn)
80 fprintf(logfile, "Write SPR %d %03x <= " ADDRX "\n",
81 sprn, sprn, env->spr[sprn]);
85 target_ulong helper_load_tbl (void)
87 return cpu_ppc_load_tbl(env);
90 target_ulong helper_load_tbu (void)
92 return cpu_ppc_load_tbu(env);
95 target_ulong helper_load_atbl (void)
97 return cpu_ppc_load_atbl(env);
100 target_ulong helper_load_atbu (void)
102 return cpu_ppc_load_atbu(env);
105 target_ulong helper_load_601_rtcl (void)
107 return cpu_ppc601_load_rtcl(env);
110 target_ulong helper_load_601_rtcu (void)
112 return cpu_ppc601_load_rtcu(env);
115 #if !defined(CONFIG_USER_ONLY)
116 #if defined (TARGET_PPC64)
117 void helper_store_asr (target_ulong val)
119 ppc_store_asr(env, val);
123 void helper_store_sdr1 (target_ulong val)
125 ppc_store_sdr1(env, val);
128 void helper_store_tbl (target_ulong val)
130 cpu_ppc_store_tbl(env, val);
133 void helper_store_tbu (target_ulong val)
135 cpu_ppc_store_tbu(env, val);
138 void helper_store_atbl (target_ulong val)
140 cpu_ppc_store_atbl(env, val);
143 void helper_store_atbu (target_ulong val)
145 cpu_ppc_store_atbu(env, val);
148 void helper_store_601_rtcl (target_ulong val)
150 cpu_ppc601_store_rtcl(env, val);
153 void helper_store_601_rtcu (target_ulong val)
155 cpu_ppc601_store_rtcu(env, val);
158 target_ulong helper_load_decr (void)
160 return cpu_ppc_load_decr(env);
163 void helper_store_decr (target_ulong val)
165 cpu_ppc_store_decr(env, val);
168 void helper_store_hid0_601 (target_ulong val)
172 hid0 = env->spr[SPR_HID0];
173 if ((val ^ hid0) & 0x00000008) {
174 /* Change current endianness */
175 env->hflags &= ~(1 << MSR_LE);
176 env->hflags_nmsr &= ~(1 << MSR_LE);
177 env->hflags_nmsr |= (1 << MSR_LE) & (((val >> 3) & 1) << MSR_LE);
178 env->hflags |= env->hflags_nmsr;
180 fprintf(logfile, "%s: set endianness to %c => " ADDRX "\n",
181 __func__, val & 0x8 ? 'l' : 'b', env->hflags);
184 env->spr[SPR_HID0] = (uint32_t)val;
187 void helper_store_403_pbr (uint32_t num, target_ulong value)
189 if (likely(env->pb[num] != value)) {
190 env->pb[num] = value;
191 /* Should be optimized */
196 target_ulong helper_load_40x_pit (void)
198 return load_40x_pit(env);
201 void helper_store_40x_pit (target_ulong val)
203 store_40x_pit(env, val);
206 void helper_store_40x_dbcr0 (target_ulong val)
208 store_40x_dbcr0(env, val);
211 void helper_store_40x_sler (target_ulong val)
213 store_40x_sler(env, val);
216 void helper_store_booke_tcr (target_ulong val)
218 store_booke_tcr(env, val);
221 void helper_store_booke_tsr (target_ulong val)
223 store_booke_tsr(env, val);
226 void helper_store_ibatu (uint32_t nr, target_ulong val)
228 ppc_store_ibatu(env, nr, val);
231 void helper_store_ibatl (uint32_t nr, target_ulong val)
233 ppc_store_ibatl(env, nr, val);
236 void helper_store_dbatu (uint32_t nr, target_ulong val)
238 ppc_store_dbatu(env, nr, val);
241 void helper_store_dbatl (uint32_t nr, target_ulong val)
243 ppc_store_dbatl(env, nr, val);
246 void helper_store_601_batl (uint32_t nr, target_ulong val)
248 ppc_store_ibatl_601(env, nr, val);
251 void helper_store_601_batu (uint32_t nr, target_ulong val)
253 ppc_store_ibatu_601(env, nr, val);
257 /*****************************************************************************/
258 /* Memory load and stores */
260 static always_inline target_ulong addr_add(target_ulong addr, target_long arg)
262 #if defined(TARGET_PPC64)
264 return (uint32_t)(addr + arg);
270 void helper_lmw (target_ulong addr, uint32_t reg)
272 for (; reg < 32; reg++) {
274 env->gpr[reg] = bswap32(ldl(addr));
276 env->gpr[reg] = ldl(addr);
277 addr = addr_add(addr, 4);
281 void helper_stmw (target_ulong addr, uint32_t reg)
283 for (; reg < 32; reg++) {
285 stl(addr, bswap32((uint32_t)env->gpr[reg]));
287 stl(addr, (uint32_t)env->gpr[reg]);
288 addr = addr_add(addr, 4);
292 void helper_lsw(target_ulong addr, uint32_t nb, uint32_t reg)
295 for (; nb > 3; nb -= 4) {
296 env->gpr[reg] = ldl(addr);
297 reg = (reg + 1) % 32;
298 addr = addr_add(addr, 4);
300 if (unlikely(nb > 0)) {
302 for (sh = 24; nb > 0; nb--, sh -= 8) {
303 env->gpr[reg] |= ldub(addr) << sh;
304 addr = addr_add(addr, 1);
308 /* PPC32 specification says we must generate an exception if
309 * rA is in the range of registers to be loaded.
310 * In an other hand, IBM says this is valid, but rA won't be loaded.
311 * For now, I'll follow the spec...
313 void helper_lswx(target_ulong addr, uint32_t reg, uint32_t ra, uint32_t rb)
315 if (likely(xer_bc != 0)) {
316 if (unlikely((ra != 0 && reg < ra && (reg + xer_bc) > ra) ||
317 (reg < rb && (reg + xer_bc) > rb))) {
318 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
320 POWERPC_EXCP_INVAL_LSWX);
322 helper_lsw(addr, xer_bc, reg);
327 void helper_stsw(target_ulong addr, uint32_t nb, uint32_t reg)
330 for (; nb > 3; nb -= 4) {
331 stl(addr, env->gpr[reg]);
332 reg = (reg + 1) % 32;
333 addr = addr_add(addr, 4);
335 if (unlikely(nb > 0)) {
336 for (sh = 24; nb > 0; nb--, sh -= 8)
337 stb(addr, (env->gpr[reg] >> sh) & 0xFF);
338 addr = addr_add(addr, 1);
342 static void do_dcbz(target_ulong addr, int dcache_line_size)
344 addr &= ~(dcache_line_size - 1);
346 for (i = 0 ; i < dcache_line_size ; i += 4) {
349 if (env->reserve == addr)
350 env->reserve = (target_ulong)-1ULL;
353 void helper_dcbz(target_ulong addr)
355 do_dcbz(addr, env->dcache_line_size);
358 void helper_dcbz_970(target_ulong addr)
360 if (((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1)
363 do_dcbz(addr, env->dcache_line_size);
366 void helper_icbi(target_ulong addr)
370 addr &= ~(env->dcache_line_size - 1);
371 /* Invalidate one cache line :
372 * PowerPC specification says this is to be treated like a load
373 * (not a fetch) by the MMU. To be sure it will be so,
374 * do the load "by hand".
377 tb_invalidate_page_range(addr, addr + env->icache_line_size);
381 target_ulong helper_lscbx (target_ulong addr, uint32_t reg, uint32_t ra, uint32_t rb)
385 for (i = 0; i < xer_bc; i++) {
387 addr = addr_add(addr, 1);
388 /* ra (if not 0) and rb are never modified */
389 if (likely(reg != rb && (ra == 0 || reg != ra))) {
390 env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
392 if (unlikely(c == xer_cmp))
394 if (likely(d != 0)) {
405 /*****************************************************************************/
406 /* Fixed point operations helpers */
407 #if defined(TARGET_PPC64)
409 /* multiply high word */
410 uint64_t helper_mulhd (uint64_t arg1, uint64_t arg2)
414 muls64(&tl, &th, arg1, arg2);
418 /* multiply high word unsigned */
419 uint64_t helper_mulhdu (uint64_t arg1, uint64_t arg2)
423 mulu64(&tl, &th, arg1, arg2);
427 uint64_t helper_mulldo (uint64_t arg1, uint64_t arg2)
432 muls64(&tl, (uint64_t *)&th, arg1, arg2);
433 /* If th != 0 && th != -1, then we had an overflow */
434 if (likely((uint64_t)(th + 1) <= 1)) {
435 env->xer &= ~(1 << XER_OV);
437 env->xer |= (1 << XER_OV) | (1 << XER_SO);
443 target_ulong helper_cntlzw (target_ulong t)
448 #if defined(TARGET_PPC64)
449 target_ulong helper_cntlzd (target_ulong t)
455 /* shift right arithmetic helper */
456 target_ulong helper_sraw (target_ulong value, target_ulong shift)
460 if (likely(!(shift & 0x20))) {
461 if (likely((uint32_t)shift != 0)) {
463 ret = (int32_t)value >> shift;
464 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
465 env->xer &= ~(1 << XER_CA);
467 env->xer |= (1 << XER_CA);
470 ret = (int32_t)value;
471 env->xer &= ~(1 << XER_CA);
474 ret = (int32_t)value >> 31;
476 env->xer |= (1 << XER_CA);
478 env->xer &= ~(1 << XER_CA);
481 return (target_long)ret;
484 #if defined(TARGET_PPC64)
485 target_ulong helper_srad (target_ulong value, target_ulong shift)
489 if (likely(!(shift & 0x40))) {
490 if (likely((uint64_t)shift != 0)) {
492 ret = (int64_t)value >> shift;
493 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
494 env->xer &= ~(1 << XER_CA);
496 env->xer |= (1 << XER_CA);
499 ret = (int64_t)value;
500 env->xer &= ~(1 << XER_CA);
503 ret = (int64_t)value >> 63;
505 env->xer |= (1 << XER_CA);
507 env->xer &= ~(1 << XER_CA);
514 target_ulong helper_popcntb (target_ulong val)
516 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
517 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
518 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
522 #if defined(TARGET_PPC64)
523 target_ulong helper_popcntb_64 (target_ulong val)
525 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
526 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
527 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
532 /*****************************************************************************/
533 /* Floating point operations helpers */
534 uint64_t helper_float32_to_float64(uint32_t arg)
539 d.d = float32_to_float64(f.f, &env->fp_status);
543 uint32_t helper_float64_to_float32(uint64_t arg)
548 f.f = float64_to_float32(d.d, &env->fp_status);
552 static always_inline int fpisneg (float64 d)
558 return u.ll >> 63 != 0;
561 static always_inline int isden (float64 d)
567 return ((u.ll >> 52) & 0x7FF) == 0;
570 static always_inline int iszero (float64 d)
576 return (u.ll & ~0x8000000000000000ULL) == 0;
579 static always_inline int isinfinity (float64 d)
585 return ((u.ll >> 52) & 0x7FF) == 0x7FF &&
586 (u.ll & 0x000FFFFFFFFFFFFFULL) == 0;
589 #ifdef CONFIG_SOFTFLOAT
590 static always_inline int isfinite (float64 d)
596 return (((u.ll >> 52) & 0x7FF) != 0x7FF);
599 static always_inline int isnormal (float64 d)
605 uint32_t exp = (u.ll >> 52) & 0x7FF;
606 return ((0 < exp) && (exp < 0x7FF));
610 uint32_t helper_compute_fprf (uint64_t arg, uint32_t set_fprf)
616 isneg = fpisneg(farg.d);
617 if (unlikely(float64_is_nan(farg.d))) {
618 if (float64_is_signaling_nan(farg.d)) {
619 /* Signaling NaN: flags are undefined */
625 } else if (unlikely(isinfinity(farg.d))) {
632 if (iszero(farg.d)) {
640 /* Denormalized numbers */
643 /* Normalized numbers */
654 /* We update FPSCR_FPRF */
655 env->fpscr &= ~(0x1F << FPSCR_FPRF);
656 env->fpscr |= ret << FPSCR_FPRF;
658 /* We just need fpcc to update Rc1 */
662 /* Floating-point invalid operations exception */
663 static always_inline uint64_t fload_invalid_op_excp (int op)
669 if (op & POWERPC_EXCP_FP_VXSNAN) {
670 /* Operation on signaling NaN */
671 env->fpscr |= 1 << FPSCR_VXSNAN;
673 if (op & POWERPC_EXCP_FP_VXSOFT) {
674 /* Software-defined condition */
675 env->fpscr |= 1 << FPSCR_VXSOFT;
677 switch (op & ~(POWERPC_EXCP_FP_VXSOFT | POWERPC_EXCP_FP_VXSNAN)) {
678 case POWERPC_EXCP_FP_VXISI:
679 /* Magnitude subtraction of infinities */
680 env->fpscr |= 1 << FPSCR_VXISI;
682 case POWERPC_EXCP_FP_VXIDI:
683 /* Division of infinity by infinity */
684 env->fpscr |= 1 << FPSCR_VXIDI;
686 case POWERPC_EXCP_FP_VXZDZ:
687 /* Division of zero by zero */
688 env->fpscr |= 1 << FPSCR_VXZDZ;
690 case POWERPC_EXCP_FP_VXIMZ:
691 /* Multiplication of zero by infinity */
692 env->fpscr |= 1 << FPSCR_VXIMZ;
694 case POWERPC_EXCP_FP_VXVC:
695 /* Ordered comparison of NaN */
696 env->fpscr |= 1 << FPSCR_VXVC;
697 env->fpscr &= ~(0xF << FPSCR_FPCC);
698 env->fpscr |= 0x11 << FPSCR_FPCC;
699 /* We must update the target FPR before raising the exception */
701 env->exception_index = POWERPC_EXCP_PROGRAM;
702 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
703 /* Update the floating-point enabled exception summary */
704 env->fpscr |= 1 << FPSCR_FEX;
705 /* Exception is differed */
709 case POWERPC_EXCP_FP_VXSQRT:
710 /* Square root of a negative number */
711 env->fpscr |= 1 << FPSCR_VXSQRT;
713 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
715 /* Set the result to quiet NaN */
717 env->fpscr &= ~(0xF << FPSCR_FPCC);
718 env->fpscr |= 0x11 << FPSCR_FPCC;
721 case POWERPC_EXCP_FP_VXCVI:
722 /* Invalid conversion */
723 env->fpscr |= 1 << FPSCR_VXCVI;
724 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
726 /* Set the result to quiet NaN */
728 env->fpscr &= ~(0xF << FPSCR_FPCC);
729 env->fpscr |= 0x11 << FPSCR_FPCC;
733 /* Update the floating-point invalid operation summary */
734 env->fpscr |= 1 << FPSCR_VX;
735 /* Update the floating-point exception summary */
736 env->fpscr |= 1 << FPSCR_FX;
738 /* Update the floating-point enabled exception summary */
739 env->fpscr |= 1 << FPSCR_FEX;
740 if (msr_fe0 != 0 || msr_fe1 != 0)
741 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_FP | op);
746 static always_inline uint64_t float_zero_divide_excp (uint64_t arg1, uint64_t arg2)
748 env->fpscr |= 1 << FPSCR_ZX;
749 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
750 /* Update the floating-point exception summary */
751 env->fpscr |= 1 << FPSCR_FX;
753 /* Update the floating-point enabled exception summary */
754 env->fpscr |= 1 << FPSCR_FEX;
755 if (msr_fe0 != 0 || msr_fe1 != 0) {
756 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
757 POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
760 /* Set the result to infinity */
761 arg1 = ((arg1 ^ arg2) & 0x8000000000000000ULL);
762 arg1 |= 0x7FFULL << 52;
767 static always_inline void float_overflow_excp (void)
769 env->fpscr |= 1 << FPSCR_OX;
770 /* Update the floating-point exception summary */
771 env->fpscr |= 1 << FPSCR_FX;
773 /* XXX: should adjust the result */
774 /* Update the floating-point enabled exception summary */
775 env->fpscr |= 1 << FPSCR_FEX;
776 /* We must update the target FPR before raising the exception */
777 env->exception_index = POWERPC_EXCP_PROGRAM;
778 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
780 env->fpscr |= 1 << FPSCR_XX;
781 env->fpscr |= 1 << FPSCR_FI;
785 static always_inline void float_underflow_excp (void)
787 env->fpscr |= 1 << FPSCR_UX;
788 /* Update the floating-point exception summary */
789 env->fpscr |= 1 << FPSCR_FX;
791 /* XXX: should adjust the result */
792 /* Update the floating-point enabled exception summary */
793 env->fpscr |= 1 << FPSCR_FEX;
794 /* We must update the target FPR before raising the exception */
795 env->exception_index = POWERPC_EXCP_PROGRAM;
796 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
800 static always_inline void float_inexact_excp (void)
802 env->fpscr |= 1 << FPSCR_XX;
803 /* Update the floating-point exception summary */
804 env->fpscr |= 1 << FPSCR_FX;
806 /* Update the floating-point enabled exception summary */
807 env->fpscr |= 1 << FPSCR_FEX;
808 /* We must update the target FPR before raising the exception */
809 env->exception_index = POWERPC_EXCP_PROGRAM;
810 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
814 static always_inline void fpscr_set_rounding_mode (void)
818 /* Set rounding mode */
821 /* Best approximation (round to nearest) */
822 rnd_type = float_round_nearest_even;
825 /* Smaller magnitude (round toward zero) */
826 rnd_type = float_round_to_zero;
829 /* Round toward +infinite */
830 rnd_type = float_round_up;
834 /* Round toward -infinite */
835 rnd_type = float_round_down;
838 set_float_rounding_mode(rnd_type, &env->fp_status);
841 void helper_fpscr_setbit (uint32_t bit)
845 prev = (env->fpscr >> bit) & 1;
846 env->fpscr |= 1 << bit;
850 env->fpscr |= 1 << FPSCR_FX;
854 env->fpscr |= 1 << FPSCR_FX;
859 env->fpscr |= 1 << FPSCR_FX;
864 env->fpscr |= 1 << FPSCR_FX;
869 env->fpscr |= 1 << FPSCR_FX;
882 env->fpscr |= 1 << FPSCR_VX;
883 env->fpscr |= 1 << FPSCR_FX;
890 env->error_code = POWERPC_EXCP_FP;
892 env->error_code |= POWERPC_EXCP_FP_VXSNAN;
894 env->error_code |= POWERPC_EXCP_FP_VXISI;
896 env->error_code |= POWERPC_EXCP_FP_VXIDI;
898 env->error_code |= POWERPC_EXCP_FP_VXZDZ;
900 env->error_code |= POWERPC_EXCP_FP_VXIMZ;
902 env->error_code |= POWERPC_EXCP_FP_VXVC;
904 env->error_code |= POWERPC_EXCP_FP_VXSOFT;
906 env->error_code |= POWERPC_EXCP_FP_VXSQRT;
908 env->error_code |= POWERPC_EXCP_FP_VXCVI;
915 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
922 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
929 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
936 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
942 fpscr_set_rounding_mode();
947 /* Update the floating-point enabled exception summary */
948 env->fpscr |= 1 << FPSCR_FEX;
949 /* We have to update Rc1 before raising the exception */
950 env->exception_index = POWERPC_EXCP_PROGRAM;
956 void helper_store_fpscr (uint64_t arg, uint32_t mask)
959 * We use only the 32 LSB of the incoming fpr
967 new |= prev & 0x90000000;
968 for (i = 0; i < 7; i++) {
969 if (mask & (1 << i)) {
970 env->fpscr &= ~(0xF << (4 * i));
971 env->fpscr |= new & (0xF << (4 * i));
974 /* Update VX and FEX */
976 env->fpscr |= 1 << FPSCR_VX;
978 env->fpscr &= ~(1 << FPSCR_VX);
979 if ((fpscr_ex & fpscr_eex) != 0) {
980 env->fpscr |= 1 << FPSCR_FEX;
981 env->exception_index = POWERPC_EXCP_PROGRAM;
982 /* XXX: we should compute it properly */
983 env->error_code = POWERPC_EXCP_FP;
986 env->fpscr &= ~(1 << FPSCR_FEX);
987 fpscr_set_rounding_mode();
990 void helper_float_check_status (void)
992 #ifdef CONFIG_SOFTFLOAT
993 if (env->exception_index == POWERPC_EXCP_PROGRAM &&
994 (env->error_code & POWERPC_EXCP_FP)) {
995 /* Differred floating-point exception after target FPR update */
996 if (msr_fe0 != 0 || msr_fe1 != 0)
997 raise_exception_err(env, env->exception_index, env->error_code);
998 } else if (env->fp_status.float_exception_flags & float_flag_overflow) {
999 float_overflow_excp();
1000 } else if (env->fp_status.float_exception_flags & float_flag_underflow) {
1001 float_underflow_excp();
1002 } else if (env->fp_status.float_exception_flags & float_flag_inexact) {
1003 float_inexact_excp();
1006 if (env->exception_index == POWERPC_EXCP_PROGRAM &&
1007 (env->error_code & POWERPC_EXCP_FP)) {
1008 /* Differred floating-point exception after target FPR update */
1009 if (msr_fe0 != 0 || msr_fe1 != 0)
1010 raise_exception_err(env, env->exception_index, env->error_code);
1015 #ifdef CONFIG_SOFTFLOAT
1016 void helper_reset_fpstatus (void)
1018 env->fp_status.float_exception_flags = 0;
1023 uint64_t helper_fadd (uint64_t arg1, uint64_t arg2)
1025 CPU_DoubleU farg1, farg2;
1029 #if USE_PRECISE_EMULATION
1030 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1031 float64_is_signaling_nan(farg2.d))) {
1033 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1034 } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
1035 fpisneg(farg1.d) == fpisneg(farg2.d))) {
1036 farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
1038 /* Magnitude subtraction of infinities */
1039 farg1.ll == fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1042 farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
1048 uint64_t helper_fsub (uint64_t arg1, uint64_t arg2)
1050 CPU_DoubleU farg1, farg2;
1054 #if USE_PRECISE_EMULATION
1056 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1057 float64_is_signaling_nan(farg2.d))) {
1058 /* sNaN subtraction */
1059 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1060 } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
1061 fpisneg(farg1.d) != fpisneg(farg2.d))) {
1062 farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
1064 /* Magnitude subtraction of infinities */
1065 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1069 farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
1075 uint64_t helper_fmul (uint64_t arg1, uint64_t arg2)
1077 CPU_DoubleU farg1, farg2;
1081 #if USE_PRECISE_EMULATION
1082 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1083 float64_is_signaling_nan(farg2.d))) {
1084 /* sNaN multiplication */
1085 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1086 } else if (unlikely((isinfinity(farg1.d) && iszero(farg2.d)) ||
1087 (iszero(farg1.d) && isinfinity(farg2.d)))) {
1088 /* Multiplication of zero by infinity */
1089 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
1091 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1095 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1101 uint64_t helper_fdiv (uint64_t arg1, uint64_t arg2)
1103 CPU_DoubleU farg1, farg2;
1107 #if USE_PRECISE_EMULATION
1108 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1109 float64_is_signaling_nan(farg2.d))) {
1111 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1112 } else if (unlikely(isinfinity(farg1.d) && isinfinity(farg2.d))) {
1113 /* Division of infinity by infinity */
1114 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
1115 } else if (unlikely(iszero(farg2.d))) {
1116 if (iszero(farg1.d)) {
1117 /* Division of zero by zero */
1118 farg1.ll fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
1120 /* Division by zero */
1121 farg1.ll = float_zero_divide_excp(farg1.d, farg2.d);
1124 farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
1127 farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
1133 uint64_t helper_fabs (uint64_t arg)
1138 farg.d = float64_abs(farg.d);
1143 uint64_t helper_fnabs (uint64_t arg)
1148 farg.d = float64_abs(farg.d);
1149 farg.d = float64_chs(farg.d);
1154 uint64_t helper_fneg (uint64_t arg)
1159 farg.d = float64_chs(farg.d);
1163 /* fctiw - fctiw. */
1164 uint64_t helper_fctiw (uint64_t arg)
1169 if (unlikely(float64_is_signaling_nan(farg.d))) {
1170 /* sNaN conversion */
1171 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1172 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1173 /* qNan / infinity conversion */
1174 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1176 farg.ll = float64_to_int32(farg.d, &env->fp_status);
1177 #if USE_PRECISE_EMULATION
1178 /* XXX: higher bits are not supposed to be significant.
1179 * to make tests easier, return the same as a real PowerPC 750
1181 farg.ll |= 0xFFF80000ULL << 32;
1187 /* fctiwz - fctiwz. */
1188 uint64_t helper_fctiwz (uint64_t arg)
1193 if (unlikely(float64_is_signaling_nan(farg.d))) {
1194 /* sNaN conversion */
1195 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1196 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1197 /* qNan / infinity conversion */
1198 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1200 farg.ll = float64_to_int32_round_to_zero(farg.d, &env->fp_status);
1201 #if USE_PRECISE_EMULATION
1202 /* XXX: higher bits are not supposed to be significant.
1203 * to make tests easier, return the same as a real PowerPC 750
1205 farg.ll |= 0xFFF80000ULL << 32;
1211 #if defined(TARGET_PPC64)
1212 /* fcfid - fcfid. */
1213 uint64_t helper_fcfid (uint64_t arg)
1216 farg.d = int64_to_float64(arg, &env->fp_status);
1220 /* fctid - fctid. */
1221 uint64_t helper_fctid (uint64_t arg)
1226 if (unlikely(float64_is_signaling_nan(farg.d))) {
1227 /* sNaN conversion */
1228 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1229 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1230 /* qNan / infinity conversion */
1231 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1233 farg.ll = float64_to_int64(farg.d, &env->fp_status);
1238 /* fctidz - fctidz. */
1239 uint64_t helper_fctidz (uint64_t arg)
1244 if (unlikely(float64_is_signaling_nan(farg.d))) {
1245 /* sNaN conversion */
1246 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1247 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1248 /* qNan / infinity conversion */
1249 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1251 farg.ll = float64_to_int64_round_to_zero(farg.d, &env->fp_status);
1258 static always_inline uint64_t do_fri (uint64_t arg, int rounding_mode)
1263 if (unlikely(float64_is_signaling_nan(farg.d))) {
1265 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1266 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1267 /* qNan / infinity round */
1268 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1270 set_float_rounding_mode(rounding_mode, &env->fp_status);
1271 farg.ll = float64_round_to_int(farg.d, &env->fp_status);
1272 /* Restore rounding mode from FPSCR */
1273 fpscr_set_rounding_mode();
1278 uint64_t helper_frin (uint64_t arg)
1280 return do_fri(arg, float_round_nearest_even);
1283 uint64_t helper_friz (uint64_t arg)
1285 return do_fri(arg, float_round_to_zero);
1288 uint64_t helper_frip (uint64_t arg)
1290 return do_fri(arg, float_round_up);
1293 uint64_t helper_frim (uint64_t arg)
1295 return do_fri(arg, float_round_down);
1298 /* fmadd - fmadd. */
1299 uint64_t helper_fmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1301 CPU_DoubleU farg1, farg2, farg3;
1306 #if USE_PRECISE_EMULATION
1307 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1308 float64_is_signaling_nan(farg2.d) ||
1309 float64_is_signaling_nan(farg3.d))) {
1310 /* sNaN operation */
1311 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1314 /* This is the way the PowerPC specification defines it */
1315 float128 ft0_128, ft1_128;
1317 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1318 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1319 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1320 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1321 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1322 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1324 /* This is OK on x86 hosts */
1325 farg1.d = (farg1.d * farg2.d) + farg3.d;
1329 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1330 farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1335 /* fmsub - fmsub. */
1336 uint64_t helper_fmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1338 CPU_DoubleU farg1, farg2, farg3;
1343 #if USE_PRECISE_EMULATION
1344 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1345 float64_is_signaling_nan(farg2.d) ||
1346 float64_is_signaling_nan(farg3.d))) {
1347 /* sNaN operation */
1348 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1351 /* This is the way the PowerPC specification defines it */
1352 float128 ft0_128, ft1_128;
1354 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1355 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1356 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1357 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1358 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1359 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1361 /* This is OK on x86 hosts */
1362 farg1.d = (farg1.d * farg2.d) - farg3.d;
1366 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1367 farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1372 /* fnmadd - fnmadd. */
1373 uint64_t helper_fnmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1375 CPU_DoubleU farg1, farg2, farg3;
1381 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1382 float64_is_signaling_nan(farg2.d) ||
1383 float64_is_signaling_nan(farg3.d))) {
1384 /* sNaN operation */
1385 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1387 #if USE_PRECISE_EMULATION
1389 /* This is the way the PowerPC specification defines it */
1390 float128 ft0_128, ft1_128;
1392 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1393 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1394 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1395 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1396 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1397 farg1.d= float128_to_float64(ft0_128, &env->fp_status);
1399 /* This is OK on x86 hosts */
1400 farg1.d = (farg1.d * farg2.d) + farg3.d;
1403 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1404 farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1406 if (likely(!isnan(farg1.d)))
1407 farg1.d = float64_chs(farg1.d);
1412 /* fnmsub - fnmsub. */
1413 uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1415 CPU_DoubleU farg1, farg2, farg3;
1421 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1422 float64_is_signaling_nan(farg2.d) ||
1423 float64_is_signaling_nan(farg3.d))) {
1424 /* sNaN operation */
1425 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1427 #if USE_PRECISE_EMULATION
1429 /* This is the way the PowerPC specification defines it */
1430 float128 ft0_128, ft1_128;
1432 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1433 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1434 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1435 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1436 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1437 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1439 /* This is OK on x86 hosts */
1440 farg1.d = (farg1.d * farg2.d) - farg3.d;
1443 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1444 farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1446 if (likely(!isnan(farg1.d)))
1447 farg1.d = float64_chs(farg1.d);
1453 uint64_t helper_frsp (uint64_t arg)
1458 #if USE_PRECISE_EMULATION
1459 if (unlikely(float64_is_signaling_nan(farg.d))) {
1460 /* sNaN square root */
1461 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1463 fard.d = float64_to_float32(farg.d, &env->fp_status);
1466 farg.d = float64_to_float32(farg.d, &env->fp_status);
1471 /* fsqrt - fsqrt. */
1472 uint64_t helper_fsqrt (uint64_t arg)
1477 if (unlikely(float64_is_signaling_nan(farg.d))) {
1478 /* sNaN square root */
1479 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1480 } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
1481 /* Square root of a negative nonzero number */
1482 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1484 farg.d = float64_sqrt(farg.d, &env->fp_status);
1490 uint64_t helper_fre (uint64_t arg)
1495 if (unlikely(float64_is_signaling_nan(farg.d))) {
1496 /* sNaN reciprocal */
1497 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1498 } else if (unlikely(iszero(farg.d))) {
1499 /* Zero reciprocal */
1500 farg.ll = float_zero_divide_excp(1.0, farg.d);
1501 } else if (likely(isnormal(farg.d))) {
1502 farg.d = float64_div(1.0, farg.d, &env->fp_status);
1504 if (farg.ll == 0x8000000000000000ULL) {
1505 farg.ll = 0xFFF0000000000000ULL;
1506 } else if (farg.ll == 0x0000000000000000ULL) {
1507 farg.ll = 0x7FF0000000000000ULL;
1508 } else if (isnan(farg.d)) {
1509 farg.ll = 0x7FF8000000000000ULL;
1510 } else if (fpisneg(farg.d)) {
1511 farg.ll = 0x8000000000000000ULL;
1513 farg.ll = 0x0000000000000000ULL;
1520 uint64_t helper_fres (uint64_t arg)
1525 if (unlikely(float64_is_signaling_nan(farg.d))) {
1526 /* sNaN reciprocal */
1527 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1528 } else if (unlikely(iszero(farg.d))) {
1529 /* Zero reciprocal */
1530 farg.ll = float_zero_divide_excp(1.0, farg.d);
1531 } else if (likely(isnormal(farg.d))) {
1532 #if USE_PRECISE_EMULATION
1533 farg.d = float64_div(1.0, farg.d, &env->fp_status);
1534 farg.d = float64_to_float32(farg.d, &env->fp_status);
1536 farg.d = float32_div(1.0, farg.d, &env->fp_status);
1539 if (farg.ll == 0x8000000000000000ULL) {
1540 farg.ll = 0xFFF0000000000000ULL;
1541 } else if (farg.ll == 0x0000000000000000ULL) {
1542 farg.ll = 0x7FF0000000000000ULL;
1543 } else if (isnan(farg.d)) {
1544 farg.ll = 0x7FF8000000000000ULL;
1545 } else if (fpisneg(farg.d)) {
1546 farg.ll = 0x8000000000000000ULL;
1548 farg.ll = 0x0000000000000000ULL;
1554 /* frsqrte - frsqrte. */
1555 uint64_t helper_frsqrte (uint64_t arg)
1560 if (unlikely(float64_is_signaling_nan(farg.d))) {
1561 /* sNaN reciprocal square root */
1562 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1563 } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
1564 /* Reciprocal square root of a negative nonzero number */
1565 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1566 } else if (likely(isnormal(farg.d))) {
1567 farg.d = float64_sqrt(farg.d, &env->fp_status);
1568 farg.d = float32_div(1.0, farg.d, &env->fp_status);
1570 if (farg.ll == 0x8000000000000000ULL) {
1571 farg.ll = 0xFFF0000000000000ULL;
1572 } else if (farg.ll == 0x0000000000000000ULL) {
1573 farg.ll = 0x7FF0000000000000ULL;
1574 } else if (isnan(farg.d)) {
1575 farg.ll |= 0x000FFFFFFFFFFFFFULL;
1576 } else if (fpisneg(farg.d)) {
1577 farg.ll = 0x7FF8000000000000ULL;
1579 farg.ll = 0x0000000000000000ULL;
1586 uint64_t helper_fsel (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1588 CPU_DoubleU farg1, farg2, farg3;
1594 if (!fpisneg(farg1.d) || iszero(farg1.d))
1600 uint32_t helper_fcmpu (uint64_t arg1, uint64_t arg2)
1602 CPU_DoubleU farg1, farg2;
1607 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1608 float64_is_signaling_nan(farg2.d))) {
1609 /* sNaN comparison */
1610 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1612 if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1614 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1620 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1621 env->fpscr |= ret << FPSCR_FPRF;
1625 uint32_t helper_fcmpo (uint64_t arg1, uint64_t arg2)
1627 CPU_DoubleU farg1, farg2;
1632 if (unlikely(float64_is_nan(farg1.d) ||
1633 float64_is_nan(farg2.d))) {
1634 if (float64_is_signaling_nan(farg1.d) ||
1635 float64_is_signaling_nan(farg2.d)) {
1636 /* sNaN comparison */
1637 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN |
1638 POWERPC_EXCP_FP_VXVC);
1640 /* qNaN comparison */
1641 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
1644 if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1646 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1652 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1653 env->fpscr |= ret << FPSCR_FPRF;
1657 #if !defined (CONFIG_USER_ONLY)
1658 void helper_store_msr (target_ulong val)
1660 val = hreg_store_msr(env, val, 0);
1662 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1663 raise_exception(env, val);
1667 void cpu_dump_rfi (target_ulong RA, target_ulong msr);
1669 static always_inline void do_rfi (target_ulong nip, target_ulong msr,
1670 target_ulong msrm, int keep_msrh)
1672 #if defined(TARGET_PPC64)
1673 if (msr & (1ULL << MSR_SF)) {
1674 nip = (uint64_t)nip;
1675 msr &= (uint64_t)msrm;
1677 nip = (uint32_t)nip;
1678 msr = (uint32_t)(msr & msrm);
1680 msr |= env->msr & ~((uint64_t)0xFFFFFFFF);
1683 nip = (uint32_t)nip;
1684 msr &= (uint32_t)msrm;
1686 /* XXX: beware: this is false if VLE is supported */
1687 env->nip = nip & ~((target_ulong)0x00000003);
1688 hreg_store_msr(env, msr, 1);
1689 #if defined (DEBUG_OP)
1690 cpu_dump_rfi(env->nip, env->msr);
1692 /* No need to raise an exception here,
1693 * as rfi is always the last insn of a TB
1695 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1698 void helper_rfi (void)
1700 do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1701 ~((target_ulong)0xFFFF0000), 1);
1704 #if defined(TARGET_PPC64)
1705 void helper_rfid (void)
1707 do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1708 ~((target_ulong)0xFFFF0000), 0);
1711 void helper_hrfid (void)
1713 do_rfi(env->spr[SPR_HSRR0], env->spr[SPR_HSRR1],
1714 ~((target_ulong)0xFFFF0000), 0);
1719 void helper_tw (target_ulong arg1, target_ulong arg2, uint32_t flags)
1721 if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
1722 ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
1723 ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
1724 ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
1725 ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
1726 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1730 #if defined(TARGET_PPC64)
1731 void helper_td (target_ulong arg1, target_ulong arg2, uint32_t flags)
1733 if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
1734 ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
1735 ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
1736 ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
1737 ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01)))))
1738 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1742 /*****************************************************************************/
1743 /* PowerPC 601 specific instructions (POWER bridge) */
1745 target_ulong helper_clcs (uint32_t arg)
1749 /* Instruction cache line size */
1750 return env->icache_line_size;
1753 /* Data cache line size */
1754 return env->dcache_line_size;
1757 /* Minimum cache line size */
1758 return (env->icache_line_size < env->dcache_line_size) ?
1759 env->icache_line_size : env->dcache_line_size;
1762 /* Maximum cache line size */
1763 return (env->icache_line_size > env->dcache_line_size) ?
1764 env->icache_line_size : env->dcache_line_size;
1773 target_ulong helper_div (target_ulong arg1, target_ulong arg2)
1775 uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
1777 if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1778 (int32_t)arg2 == 0) {
1779 env->spr[SPR_MQ] = 0;
1782 env->spr[SPR_MQ] = tmp % arg2;
1783 return tmp / (int32_t)arg2;
1787 target_ulong helper_divo (target_ulong arg1, target_ulong arg2)
1789 uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
1791 if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1792 (int32_t)arg2 == 0) {
1793 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1794 env->spr[SPR_MQ] = 0;
1797 env->spr[SPR_MQ] = tmp % arg2;
1798 tmp /= (int32_t)arg2;
1799 if ((int32_t)tmp != tmp) {
1800 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1802 env->xer &= ~(1 << XER_OV);
1808 target_ulong helper_divs (target_ulong arg1, target_ulong arg2)
1810 if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1811 (int32_t)arg2 == 0) {
1812 env->spr[SPR_MQ] = 0;
1815 env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
1816 return (int32_t)arg1 / (int32_t)arg2;
1820 target_ulong helper_divso (target_ulong arg1, target_ulong arg2)
1822 if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1823 (int32_t)arg2 == 0) {
1824 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1825 env->spr[SPR_MQ] = 0;
1828 env->xer &= ~(1 << XER_OV);
1829 env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
1830 return (int32_t)arg1 / (int32_t)arg2;
1834 #if !defined (CONFIG_USER_ONLY)
1835 target_ulong helper_rac (target_ulong addr)
1839 target_ulong ret = 0;
1841 /* We don't have to generate many instances of this instruction,
1842 * as rac is supervisor only.
1844 /* XXX: FIX THIS: Pretend we have no BAT */
1845 nb_BATs = env->nb_BATs;
1847 if (get_physical_address(env, &ctx, addr, 0, ACCESS_INT) == 0)
1849 env->nb_BATs = nb_BATs;
1853 void helper_rfsvc (void)
1855 do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
1859 /*****************************************************************************/
1860 /* 602 specific instructions */
1861 /* mfrom is the most crazy instruction ever seen, imho ! */
1862 /* Real implementation uses a ROM table. Do the same */
1863 #if !defined (CONFIG_USER_ONLY)
1864 #define USE_MFROM_ROM_TABLE
1865 target_ulong helper_602_mfrom (target_ulong arg)
1867 if (likely(arg < 602)) {
1868 #if defined(USE_MFROM_ROM_TABLE)
1869 #include "mfrom_table.c"
1870 return mfrom_ROM_table[arg];
1873 /* Extremly decomposed:
1875 * return 256 * log10(10 + 1.0) + 0.5
1878 d = float64_div(d, 256, &env->fp_status);
1880 d = exp10(d); // XXX: use float emulation function
1881 d = float64_add(d, 1.0, &env->fp_status);
1882 d = log10(d); // XXX: use float emulation function
1883 d = float64_mul(d, 256, &env->fp_status);
1884 d = float64_add(d, 0.5, &env->fp_status);
1885 return float64_round_to_int(d, &env->fp_status);
1893 /*****************************************************************************/
1894 /* Embedded PowerPC specific helpers */
1896 /* XXX: to be improved to check access rights when in user-mode */
1897 target_ulong helper_load_dcr (target_ulong dcrn)
1899 target_ulong val = 0;
1901 if (unlikely(env->dcr_env == NULL)) {
1902 if (loglevel != 0) {
1903 fprintf(logfile, "No DCR environment\n");
1905 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1906 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1907 } else if (unlikely(ppc_dcr_read(env->dcr_env, dcrn, &val) != 0)) {
1908 if (loglevel != 0) {
1909 fprintf(logfile, "DCR read error %d %03x\n", (int)dcrn, (int)dcrn);
1911 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1912 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1917 void helper_store_dcr (target_ulong dcrn, target_ulong val)
1919 if (unlikely(env->dcr_env == NULL)) {
1920 if (loglevel != 0) {
1921 fprintf(logfile, "No DCR environment\n");
1923 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1924 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1925 } else if (unlikely(ppc_dcr_write(env->dcr_env, dcrn, val) != 0)) {
1926 if (loglevel != 0) {
1927 fprintf(logfile, "DCR write error %d %03x\n", (int)dcrn, (int)dcrn);
1929 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1930 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1934 #if !defined(CONFIG_USER_ONLY)
1935 void helper_40x_rfci (void)
1937 do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1938 ~((target_ulong)0xFFFF0000), 0);
1941 void helper_rfci (void)
1943 do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1944 ~((target_ulong)0x3FFF0000), 0);
1947 void helper_rfdi (void)
1949 do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1950 ~((target_ulong)0x3FFF0000), 0);
1953 void helper_rfmci (void)
1955 do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1956 ~((target_ulong)0x3FFF0000), 0);
1961 target_ulong helper_dlmzb (target_ulong high, target_ulong low, uint32_t update_Rc)
1967 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1968 if ((high & mask) == 0) {
1976 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1977 if ((low & mask) == 0) {
1989 env->xer = (env->xer & ~0x7F) | i;
1991 env->crf[0] |= xer_so;
1996 /*****************************************************************************/
1997 /* SPE extension helpers */
1998 /* Use a table to make this quicker */
1999 static uint8_t hbrev[16] = {
2000 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
2001 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
2004 static always_inline uint8_t byte_reverse (uint8_t val)
2006 return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
2009 static always_inline uint32_t word_reverse (uint32_t val)
2011 return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
2012 (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
2015 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
2016 target_ulong helper_brinc (target_ulong arg1, target_ulong arg2)
2018 uint32_t a, b, d, mask;
2020 mask = UINT32_MAX >> (32 - MASKBITS);
2023 d = word_reverse(1 + word_reverse(a | ~b));
2024 return (arg1 & ~mask) | (d & b);
2027 uint32_t helper_cntlsw32 (uint32_t val)
2029 if (val & 0x80000000)
2035 uint32_t helper_cntlzw32 (uint32_t val)
2040 /* Single-precision floating-point conversions */
2041 static always_inline uint32_t efscfsi (uint32_t val)
2045 u.f = int32_to_float32(val, &env->spe_status);
2050 static always_inline uint32_t efscfui (uint32_t val)
2054 u.f = uint32_to_float32(val, &env->spe_status);
2059 static always_inline int32_t efsctsi (uint32_t val)
2064 /* NaN are not treated the same way IEEE 754 does */
2065 if (unlikely(isnan(u.f)))
2068 return float32_to_int32(u.f, &env->spe_status);
2071 static always_inline uint32_t efsctui (uint32_t val)
2076 /* NaN are not treated the same way IEEE 754 does */
2077 if (unlikely(isnan(u.f)))
2080 return float32_to_uint32(u.f, &env->spe_status);
2083 static always_inline uint32_t efsctsiz (uint32_t val)
2088 /* NaN are not treated the same way IEEE 754 does */
2089 if (unlikely(isnan(u.f)))
2092 return float32_to_int32_round_to_zero(u.f, &env->spe_status);
2095 static always_inline uint32_t efsctuiz (uint32_t val)
2100 /* NaN are not treated the same way IEEE 754 does */
2101 if (unlikely(isnan(u.f)))
2104 return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
2107 static always_inline uint32_t efscfsf (uint32_t val)
2112 u.f = int32_to_float32(val, &env->spe_status);
2113 tmp = int64_to_float32(1ULL << 32, &env->spe_status);
2114 u.f = float32_div(u.f, tmp, &env->spe_status);
2119 static always_inline uint32_t efscfuf (uint32_t val)
2124 u.f = uint32_to_float32(val, &env->spe_status);
2125 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2126 u.f = float32_div(u.f, tmp, &env->spe_status);
2131 static always_inline uint32_t efsctsf (uint32_t val)
2137 /* NaN are not treated the same way IEEE 754 does */
2138 if (unlikely(isnan(u.f)))
2140 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2141 u.f = float32_mul(u.f, tmp, &env->spe_status);
2143 return float32_to_int32(u.f, &env->spe_status);
2146 static always_inline uint32_t efsctuf (uint32_t val)
2152 /* NaN are not treated the same way IEEE 754 does */
2153 if (unlikely(isnan(u.f)))
2155 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2156 u.f = float32_mul(u.f, tmp, &env->spe_status);
2158 return float32_to_uint32(u.f, &env->spe_status);
2161 #define HELPER_SPE_SINGLE_CONV(name) \
2162 uint32_t helper_e##name (uint32_t val) \
2164 return e##name(val); \
2167 HELPER_SPE_SINGLE_CONV(fscfsi);
2169 HELPER_SPE_SINGLE_CONV(fscfui);
2171 HELPER_SPE_SINGLE_CONV(fscfuf);
2173 HELPER_SPE_SINGLE_CONV(fscfsf);
2175 HELPER_SPE_SINGLE_CONV(fsctsi);
2177 HELPER_SPE_SINGLE_CONV(fsctui);
2179 HELPER_SPE_SINGLE_CONV(fsctsiz);
2181 HELPER_SPE_SINGLE_CONV(fsctuiz);
2183 HELPER_SPE_SINGLE_CONV(fsctsf);
2185 HELPER_SPE_SINGLE_CONV(fsctuf);
2187 #define HELPER_SPE_VECTOR_CONV(name) \
2188 uint64_t helper_ev##name (uint64_t val) \
2190 return ((uint64_t)e##name(val >> 32) << 32) | \
2191 (uint64_t)e##name(val); \
2194 HELPER_SPE_VECTOR_CONV(fscfsi);
2196 HELPER_SPE_VECTOR_CONV(fscfui);
2198 HELPER_SPE_VECTOR_CONV(fscfuf);
2200 HELPER_SPE_VECTOR_CONV(fscfsf);
2202 HELPER_SPE_VECTOR_CONV(fsctsi);
2204 HELPER_SPE_VECTOR_CONV(fsctui);
2206 HELPER_SPE_VECTOR_CONV(fsctsiz);
2208 HELPER_SPE_VECTOR_CONV(fsctuiz);
2210 HELPER_SPE_VECTOR_CONV(fsctsf);
2212 HELPER_SPE_VECTOR_CONV(fsctuf);
2214 /* Single-precision floating-point arithmetic */
2215 static always_inline uint32_t efsadd (uint32_t op1, uint32_t op2)
2220 u1.f = float32_add(u1.f, u2.f, &env->spe_status);
2224 static always_inline uint32_t efssub (uint32_t op1, uint32_t op2)
2229 u1.f = float32_sub(u1.f, u2.f, &env->spe_status);
2233 static always_inline uint32_t efsmul (uint32_t op1, uint32_t op2)
2238 u1.f = float32_mul(u1.f, u2.f, &env->spe_status);
2242 static always_inline uint32_t efsdiv (uint32_t op1, uint32_t op2)
2247 u1.f = float32_div(u1.f, u2.f, &env->spe_status);
2251 #define HELPER_SPE_SINGLE_ARITH(name) \
2252 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2254 return e##name(op1, op2); \
2257 HELPER_SPE_SINGLE_ARITH(fsadd);
2259 HELPER_SPE_SINGLE_ARITH(fssub);
2261 HELPER_SPE_SINGLE_ARITH(fsmul);
2263 HELPER_SPE_SINGLE_ARITH(fsdiv);
2265 #define HELPER_SPE_VECTOR_ARITH(name) \
2266 uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
2268 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
2269 (uint64_t)e##name(op1, op2); \
2272 HELPER_SPE_VECTOR_ARITH(fsadd);
2274 HELPER_SPE_VECTOR_ARITH(fssub);
2276 HELPER_SPE_VECTOR_ARITH(fsmul);
2278 HELPER_SPE_VECTOR_ARITH(fsdiv);
2280 /* Single-precision floating-point comparisons */
2281 static always_inline uint32_t efststlt (uint32_t op1, uint32_t op2)
2286 return float32_lt(u1.f, u2.f, &env->spe_status) ? 4 : 0;
2289 static always_inline uint32_t efststgt (uint32_t op1, uint32_t op2)
2294 return float32_le(u1.f, u2.f, &env->spe_status) ? 0 : 4;
2297 static always_inline uint32_t efststeq (uint32_t op1, uint32_t op2)
2302 return float32_eq(u1.f, u2.f, &env->spe_status) ? 4 : 0;
2305 static always_inline uint32_t efscmplt (uint32_t op1, uint32_t op2)
2307 /* XXX: TODO: test special values (NaN, infinites, ...) */
2308 return efststlt(op1, op2);
2311 static always_inline uint32_t efscmpgt (uint32_t op1, uint32_t op2)
2313 /* XXX: TODO: test special values (NaN, infinites, ...) */
2314 return efststgt(op1, op2);
2317 static always_inline uint32_t efscmpeq (uint32_t op1, uint32_t op2)
2319 /* XXX: TODO: test special values (NaN, infinites, ...) */
2320 return efststeq(op1, op2);
2323 #define HELPER_SINGLE_SPE_CMP(name) \
2324 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2326 return e##name(op1, op2) << 2; \
2329 HELPER_SINGLE_SPE_CMP(fststlt);
2331 HELPER_SINGLE_SPE_CMP(fststgt);
2333 HELPER_SINGLE_SPE_CMP(fststeq);
2335 HELPER_SINGLE_SPE_CMP(fscmplt);
2337 HELPER_SINGLE_SPE_CMP(fscmpgt);
2339 HELPER_SINGLE_SPE_CMP(fscmpeq);
2341 static always_inline uint32_t evcmp_merge (int t0, int t1)
2343 return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
2346 #define HELPER_VECTOR_SPE_CMP(name) \
2347 uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
2349 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
2352 HELPER_VECTOR_SPE_CMP(fststlt);
2354 HELPER_VECTOR_SPE_CMP(fststgt);
2356 HELPER_VECTOR_SPE_CMP(fststeq);
2358 HELPER_VECTOR_SPE_CMP(fscmplt);
2360 HELPER_VECTOR_SPE_CMP(fscmpgt);
2362 HELPER_VECTOR_SPE_CMP(fscmpeq);
2364 /* Double-precision floating-point conversion */
2365 uint64_t helper_efdcfsi (uint32_t val)
2369 u.d = int32_to_float64(val, &env->spe_status);
2374 uint64_t helper_efdcfsid (uint64_t val)
2378 u.d = int64_to_float64(val, &env->spe_status);
2383 uint64_t helper_efdcfui (uint32_t val)
2387 u.d = uint32_to_float64(val, &env->spe_status);
2392 uint64_t helper_efdcfuid (uint64_t val)
2396 u.d = uint64_to_float64(val, &env->spe_status);
2401 uint32_t helper_efdctsi (uint64_t val)
2406 /* NaN are not treated the same way IEEE 754 does */
2407 if (unlikely(isnan(u.d)))
2410 return float64_to_int32(u.d, &env->spe_status);
2413 uint32_t helper_efdctui (uint64_t val)
2418 /* NaN are not treated the same way IEEE 754 does */
2419 if (unlikely(isnan(u.d)))
2422 return float64_to_uint32(u.d, &env->spe_status);
2425 uint32_t helper_efdctsiz (uint64_t val)
2430 /* NaN are not treated the same way IEEE 754 does */
2431 if (unlikely(isnan(u.d)))
2434 return float64_to_int32_round_to_zero(u.d, &env->spe_status);
2437 uint64_t helper_efdctsidz (uint64_t val)
2442 /* NaN are not treated the same way IEEE 754 does */
2443 if (unlikely(isnan(u.d)))
2446 return float64_to_int64_round_to_zero(u.d, &env->spe_status);
2449 uint32_t helper_efdctuiz (uint64_t val)
2454 /* NaN are not treated the same way IEEE 754 does */
2455 if (unlikely(isnan(u.d)))
2458 return float64_to_uint32_round_to_zero(u.d, &env->spe_status);
2461 uint64_t helper_efdctuidz (uint64_t val)
2466 /* NaN are not treated the same way IEEE 754 does */
2467 if (unlikely(isnan(u.d)))
2470 return float64_to_uint64_round_to_zero(u.d, &env->spe_status);
2473 uint64_t helper_efdcfsf (uint32_t val)
2478 u.d = int32_to_float64(val, &env->spe_status);
2479 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2480 u.d = float64_div(u.d, tmp, &env->spe_status);
2485 uint64_t helper_efdcfuf (uint32_t val)
2490 u.d = uint32_to_float64(val, &env->spe_status);
2491 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2492 u.d = float64_div(u.d, tmp, &env->spe_status);
2497 uint32_t helper_efdctsf (uint64_t val)
2503 /* NaN are not treated the same way IEEE 754 does */
2504 if (unlikely(isnan(u.d)))
2506 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2507 u.d = float64_mul(u.d, tmp, &env->spe_status);
2509 return float64_to_int32(u.d, &env->spe_status);
2512 uint32_t helper_efdctuf (uint64_t val)
2518 /* NaN are not treated the same way IEEE 754 does */
2519 if (unlikely(isnan(u.d)))
2521 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2522 u.d = float64_mul(u.d, tmp, &env->spe_status);
2524 return float64_to_uint32(u.d, &env->spe_status);
2527 uint32_t helper_efscfd (uint64_t val)
2533 u2.f = float64_to_float32(u1.d, &env->spe_status);
2538 uint64_t helper_efdcfs (uint32_t val)
2544 u2.d = float32_to_float64(u1.f, &env->spe_status);
2549 /* Double precision fixed-point arithmetic */
2550 uint64_t helper_efdadd (uint64_t op1, uint64_t op2)
2555 u1.d = float64_add(u1.d, u2.d, &env->spe_status);
2559 uint64_t helper_efdsub (uint64_t op1, uint64_t op2)
2564 u1.d = float64_sub(u1.d, u2.d, &env->spe_status);
2568 uint64_t helper_efdmul (uint64_t op1, uint64_t op2)
2573 u1.d = float64_mul(u1.d, u2.d, &env->spe_status);
2577 uint64_t helper_efddiv (uint64_t op1, uint64_t op2)
2582 u1.d = float64_div(u1.d, u2.d, &env->spe_status);
2586 /* Double precision floating point helpers */
2587 uint32_t helper_efdtstlt (uint64_t op1, uint64_t op2)
2592 return float64_lt(u1.d, u2.d, &env->spe_status) ? 4 : 0;
2595 uint32_t helper_efdtstgt (uint64_t op1, uint64_t op2)
2600 return float64_le(u1.d, u2.d, &env->spe_status) ? 0 : 4;
2603 uint32_t helper_efdtsteq (uint64_t op1, uint64_t op2)
2608 return float64_eq(u1.d, u2.d, &env->spe_status) ? 4 : 0;
2611 uint32_t helper_efdcmplt (uint64_t op1, uint64_t op2)
2613 /* XXX: TODO: test special values (NaN, infinites, ...) */
2614 return helper_efdtstlt(op1, op2);
2617 uint32_t helper_efdcmpgt (uint64_t op1, uint64_t op2)
2619 /* XXX: TODO: test special values (NaN, infinites, ...) */
2620 return helper_efdtstgt(op1, op2);
2623 uint32_t helper_efdcmpeq (uint64_t op1, uint64_t op2)
2625 /* XXX: TODO: test special values (NaN, infinites, ...) */
2626 return helper_efdtsteq(op1, op2);
2629 /*****************************************************************************/
2630 /* Softmmu support */
2631 #if !defined (CONFIG_USER_ONLY)
2633 #define MMUSUFFIX _mmu
2636 #include "softmmu_template.h"
2639 #include "softmmu_template.h"
2642 #include "softmmu_template.h"
2645 #include "softmmu_template.h"
2647 /* try to fill the TLB and return an exception if error. If retaddr is
2648 NULL, it means that the function was called in C code (i.e. not
2649 from generated code or from helper.c) */
2650 /* XXX: fix it to restore all registers */
2651 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2653 TranslationBlock *tb;
2654 CPUState *saved_env;
2658 /* XXX: hack to restore env in all cases, even if not called from
2661 env = cpu_single_env;
2662 ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2663 if (unlikely(ret != 0)) {
2664 if (likely(retaddr)) {
2665 /* now we have a real cpu fault */
2666 pc = (unsigned long)retaddr;
2667 tb = tb_find_pc(pc);
2669 /* the PC is inside the translated code. It means that we have
2670 a virtual CPU fault */
2671 cpu_restore_state(tb, env, pc, NULL);
2674 raise_exception_err(env, env->exception_index, env->error_code);
2679 /* Segment registers load and store */
2680 target_ulong helper_load_sr (target_ulong sr_num)
2682 return env->sr[sr_num];
2685 void helper_store_sr (target_ulong sr_num, target_ulong val)
2687 ppc_store_sr(env, sr_num, val);
2690 /* SLB management */
2691 #if defined(TARGET_PPC64)
2692 target_ulong helper_load_slb (target_ulong slb_nr)
2694 return ppc_load_slb(env, slb_nr);
2697 void helper_store_slb (target_ulong slb_nr, target_ulong rs)
2699 ppc_store_slb(env, slb_nr, rs);
2702 void helper_slbia (void)
2704 ppc_slb_invalidate_all(env);
2707 void helper_slbie (target_ulong addr)
2709 ppc_slb_invalidate_one(env, addr);
2712 #endif /* defined(TARGET_PPC64) */
2714 /* TLB management */
2715 void helper_tlbia (void)
2717 ppc_tlb_invalidate_all(env);
2720 void helper_tlbie (target_ulong addr)
2722 ppc_tlb_invalidate_one(env, addr);
2725 /* Software driven TLBs management */
2726 /* PowerPC 602/603 software TLB load instructions helpers */
2727 static void do_6xx_tlb (target_ulong new_EPN, int is_code)
2729 target_ulong RPN, CMP, EPN;
2732 RPN = env->spr[SPR_RPA];
2734 CMP = env->spr[SPR_ICMP];
2735 EPN = env->spr[SPR_IMISS];
2737 CMP = env->spr[SPR_DCMP];
2738 EPN = env->spr[SPR_DMISS];
2740 way = (env->spr[SPR_SRR1] >> 17) & 1;
2741 #if defined (DEBUG_SOFTWARE_TLB)
2742 if (loglevel != 0) {
2743 fprintf(logfile, "%s: EPN " ADDRX " " ADDRX " PTE0 " ADDRX
2744 " PTE1 " ADDRX " way %d\n",
2745 __func__, new_EPN, EPN, CMP, RPN, way);
2748 /* Store this TLB */
2749 ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
2750 way, is_code, CMP, RPN);
2753 void helper_6xx_tlbd (target_ulong EPN)
2758 void helper_6xx_tlbi (target_ulong EPN)
2763 /* PowerPC 74xx software TLB load instructions helpers */
2764 static void do_74xx_tlb (target_ulong new_EPN, int is_code)
2766 target_ulong RPN, CMP, EPN;
2769 RPN = env->spr[SPR_PTELO];
2770 CMP = env->spr[SPR_PTEHI];
2771 EPN = env->spr[SPR_TLBMISS] & ~0x3;
2772 way = env->spr[SPR_TLBMISS] & 0x3;
2773 #if defined (DEBUG_SOFTWARE_TLB)
2774 if (loglevel != 0) {
2775 fprintf(logfile, "%s: EPN " ADDRX " " ADDRX " PTE0 " ADDRX
2776 " PTE1 " ADDRX " way %d\n",
2777 __func__, new_EPN, EPN, CMP, RPN, way);
2780 /* Store this TLB */
2781 ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
2782 way, is_code, CMP, RPN);
2785 void helper_74xx_tlbd (target_ulong EPN)
2787 do_74xx_tlb(EPN, 0);
2790 void helper_74xx_tlbi (target_ulong EPN)
2792 do_74xx_tlb(EPN, 1);
2795 static always_inline target_ulong booke_tlb_to_page_size (int size)
2797 return 1024 << (2 * size);
2800 static always_inline int booke_page_size_to_tlb (target_ulong page_size)
2804 switch (page_size) {
2838 #if defined (TARGET_PPC64)
2839 case 0x000100000000ULL:
2842 case 0x000400000000ULL:
2845 case 0x001000000000ULL:
2848 case 0x004000000000ULL:
2851 case 0x010000000000ULL:
2863 /* Helpers for 4xx TLB management */
2864 target_ulong helper_4xx_tlbre_lo (target_ulong entry)
2871 tlb = &env->tlb[entry].tlbe;
2873 if (tlb->prot & PAGE_VALID)
2875 size = booke_page_size_to_tlb(tlb->size);
2876 if (size < 0 || size > 0x7)
2879 env->spr[SPR_40x_PID] = tlb->PID;
2883 target_ulong helper_4xx_tlbre_hi (target_ulong entry)
2889 tlb = &env->tlb[entry].tlbe;
2891 if (tlb->prot & PAGE_EXEC)
2893 if (tlb->prot & PAGE_WRITE)
2898 void helper_4xx_tlbwe_hi (target_ulong entry, target_ulong val)
2901 target_ulong page, end;
2903 #if defined (DEBUG_SOFTWARE_TLB)
2904 if (loglevel != 0) {
2905 fprintf(logfile, "%s entry %d val " ADDRX "\n", __func__, (int)entry, val);
2909 tlb = &env->tlb[entry].tlbe;
2910 /* Invalidate previous TLB (if it's valid) */
2911 if (tlb->prot & PAGE_VALID) {
2912 end = tlb->EPN + tlb->size;
2913 #if defined (DEBUG_SOFTWARE_TLB)
2914 if (loglevel != 0) {
2915 fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
2916 " end " ADDRX "\n", __func__, (int)entry, tlb->EPN, end);
2919 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2920 tlb_flush_page(env, page);
2922 tlb->size = booke_tlb_to_page_size((val >> 7) & 0x7);
2923 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2924 * If this ever occurs, one should use the ppcemb target instead
2925 * of the ppc or ppc64 one
2927 if ((val & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
2928 cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
2929 "are not supported (%d)\n",
2930 tlb->size, TARGET_PAGE_SIZE, (int)((val >> 7) & 0x7));
2932 tlb->EPN = val & ~(tlb->size - 1);
2934 tlb->prot |= PAGE_VALID;
2936 tlb->prot &= ~PAGE_VALID;
2938 /* XXX: TO BE FIXED */
2939 cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
2941 tlb->PID = env->spr[SPR_40x_PID]; /* PID */
2942 tlb->attr = val & 0xFF;
2943 #if defined (DEBUG_SOFTWARE_TLB)
2944 if (loglevel != 0) {
2945 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2946 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2947 (int)entry, tlb->RPN, tlb->EPN, tlb->size,
2948 tlb->prot & PAGE_READ ? 'r' : '-',
2949 tlb->prot & PAGE_WRITE ? 'w' : '-',
2950 tlb->prot & PAGE_EXEC ? 'x' : '-',
2951 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2954 /* Invalidate new TLB (if valid) */
2955 if (tlb->prot & PAGE_VALID) {
2956 end = tlb->EPN + tlb->size;
2957 #if defined (DEBUG_SOFTWARE_TLB)
2958 if (loglevel != 0) {
2959 fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
2960 " end " ADDRX "\n", __func__, (int)entry, tlb->EPN, end);
2963 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2964 tlb_flush_page(env, page);
2968 void helper_4xx_tlbwe_lo (target_ulong entry, target_ulong val)
2972 #if defined (DEBUG_SOFTWARE_TLB)
2973 if (loglevel != 0) {
2974 fprintf(logfile, "%s entry %i val " ADDRX "\n", __func__, (int)entry, val);
2978 tlb = &env->tlb[entry].tlbe;
2979 tlb->RPN = val & 0xFFFFFC00;
2980 tlb->prot = PAGE_READ;
2982 tlb->prot |= PAGE_EXEC;
2984 tlb->prot |= PAGE_WRITE;
2985 #if defined (DEBUG_SOFTWARE_TLB)
2986 if (loglevel != 0) {
2987 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2988 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2989 (int)entry, tlb->RPN, tlb->EPN, tlb->size,
2990 tlb->prot & PAGE_READ ? 'r' : '-',
2991 tlb->prot & PAGE_WRITE ? 'w' : '-',
2992 tlb->prot & PAGE_EXEC ? 'x' : '-',
2993 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2998 target_ulong helper_4xx_tlbsx (target_ulong address)
3000 return ppcemb_tlb_search(env, address, env->spr[SPR_40x_PID]);
3003 /* PowerPC 440 TLB management */
3004 void helper_440_tlbwe (uint32_t word, target_ulong entry, target_ulong value)
3007 target_ulong EPN, RPN, size;
3010 #if defined (DEBUG_SOFTWARE_TLB)
3011 if (loglevel != 0) {
3012 fprintf(logfile, "%s word %d entry %d value " ADDRX "\n",
3013 __func__, word, (int)entry, value);
3018 tlb = &env->tlb[entry].tlbe;
3021 /* Just here to please gcc */
3023 EPN = value & 0xFFFFFC00;
3024 if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
3027 size = booke_tlb_to_page_size((value >> 4) & 0xF);
3028 if ((tlb->prot & PAGE_VALID) && tlb->size < size)
3032 tlb->attr |= (value >> 8) & 1;
3033 if (value & 0x200) {
3034 tlb->prot |= PAGE_VALID;
3036 if (tlb->prot & PAGE_VALID) {
3037 tlb->prot &= ~PAGE_VALID;
3041 tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
3046 RPN = value & 0xFFFFFC0F;
3047 if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
3052 tlb->attr = (tlb->attr & 0x1) | (value & 0x0000FF00);
3053 tlb->prot = tlb->prot & PAGE_VALID;
3055 tlb->prot |= PAGE_READ << 4;
3057 tlb->prot |= PAGE_WRITE << 4;
3059 tlb->prot |= PAGE_EXEC << 4;
3061 tlb->prot |= PAGE_READ;
3063 tlb->prot |= PAGE_WRITE;
3065 tlb->prot |= PAGE_EXEC;
3070 target_ulong helper_440_tlbre (uint32_t word, target_ulong entry)
3077 tlb = &env->tlb[entry].tlbe;
3080 /* Just here to please gcc */
3083 size = booke_page_size_to_tlb(tlb->size);
3084 if (size < 0 || size > 0xF)
3087 if (tlb->attr & 0x1)
3089 if (tlb->prot & PAGE_VALID)
3091 env->spr[SPR_440_MMUCR] &= ~0x000000FF;
3092 env->spr[SPR_440_MMUCR] |= tlb->PID;
3098 ret = tlb->attr & ~0x1;
3099 if (tlb->prot & (PAGE_READ << 4))
3101 if (tlb->prot & (PAGE_WRITE << 4))
3103 if (tlb->prot & (PAGE_EXEC << 4))
3105 if (tlb->prot & PAGE_READ)
3107 if (tlb->prot & PAGE_WRITE)
3109 if (tlb->prot & PAGE_EXEC)
3116 target_ulong helper_440_tlbsx (target_ulong address)
3118 return ppcemb_tlb_search(env, address, env->spr[SPR_440_MMUCR] & 0xFF);
3121 #endif /* !CONFIG_USER_ONLY */