2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "host-utils.h"
24 #include "helper_regs.h"
25 #include "op_helper.h"
27 #define MEMSUFFIX _raw
28 #include "op_helper.h"
29 #include "op_helper_mem.h"
30 #if !defined(CONFIG_USER_ONLY)
31 #define MEMSUFFIX _user
32 #include "op_helper.h"
33 #include "op_helper_mem.h"
34 #define MEMSUFFIX _kernel
35 #include "op_helper.h"
36 #include "op_helper_mem.h"
37 #define MEMSUFFIX _hypv
38 #include "op_helper.h"
39 #include "op_helper_mem.h"
43 //#define DEBUG_EXCEPTIONS
44 //#define DEBUG_SOFTWARE_TLB
46 /*****************************************************************************/
47 /* Exceptions processing helpers */
49 void helper_raise_exception_err (uint32_t exception, uint32_t error_code)
51 raise_exception_err(env, exception, error_code);
54 void helper_raise_debug (void)
56 raise_exception(env, EXCP_DEBUG);
59 /*****************************************************************************/
60 /* Registers load and stores */
61 target_ulong helper_load_cr (void)
63 return (env->crf[0] << 28) |
73 void helper_store_cr (target_ulong val, uint32_t mask)
77 for (i = 0, sh = 7; i < 8; i++, sh--) {
79 env->crf[i] = (val >> (sh * 4)) & 0xFUL;
83 #if defined(TARGET_PPC64)
84 void do_store_pri (int prio)
86 env->spr[SPR_PPR] &= ~0x001C000000000000ULL;
87 env->spr[SPR_PPR] |= ((uint64_t)prio & 0x7) << 50;
91 target_ulong ppc_load_dump_spr (int sprn)
94 fprintf(logfile, "Read SPR %d %03x => " ADDRX "\n",
95 sprn, sprn, env->spr[sprn]);
98 return env->spr[sprn];
101 void ppc_store_dump_spr (int sprn, target_ulong val)
104 fprintf(logfile, "Write SPR %d %03x => " ADDRX " <= " ADDRX "\n",
105 sprn, sprn, env->spr[sprn], val);
107 env->spr[sprn] = val;
110 /*****************************************************************************/
111 /* Memory load and stores */
113 static always_inline target_ulong get_addr(target_ulong addr)
115 #if defined(TARGET_PPC64)
120 return (uint32_t)addr;
123 void helper_lmw (target_ulong addr, uint32_t reg)
125 #ifdef CONFIG_USER_ONLY
126 #define ldfun ldl_raw
128 int (*ldfun)(target_ulong);
130 switch (env->mmu_idx) {
132 case 0: ldfun = ldl_user;
134 case 1: ldfun = ldl_kernel;
136 case 2: ldfun = ldl_hypv;
140 for (; reg < 32; reg++, addr += 4) {
142 env->gpr[reg] = bswap32(ldfun(get_addr(addr)));
144 env->gpr[reg] = ldfun(get_addr(addr));
148 void helper_stmw (target_ulong addr, uint32_t reg)
150 #ifdef CONFIG_USER_ONLY
151 #define stfun stl_raw
153 void (*stfun)(target_ulong, int);
155 switch (env->mmu_idx) {
157 case 0: stfun = stl_user;
159 case 1: stfun = stl_kernel;
161 case 2: stfun = stl_hypv;
165 for (; reg < 32; reg++, addr += 4) {
167 stfun(get_addr(addr), bswap32((uint32_t)env->gpr[reg]));
169 stfun(get_addr(addr), (uint32_t)env->gpr[reg]);
173 static void do_dcbz(target_ulong addr, int dcache_line_size)
175 target_long mask = get_addr(~(dcache_line_size - 1));
177 #ifdef CONFIG_USER_ONLY
178 #define stfun stl_raw
180 void (*stfun)(target_ulong, int);
182 switch (env->mmu_idx) {
184 case 0: stfun = stl_user;
186 case 1: stfun = stl_kernel;
188 case 2: stfun = stl_hypv;
193 for (i = 0 ; i < dcache_line_size ; i += 4) {
196 if ((env->reserve & mask) == addr)
197 env->reserve = (target_ulong)-1ULL;
200 void helper_dcbz(target_ulong addr)
202 do_dcbz(addr, env->dcache_line_size);
205 void helper_dcbz_970(target_ulong addr)
207 if (((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1)
210 do_dcbz(addr, env->dcache_line_size);
213 /*****************************************************************************/
214 /* Fixed point operations helpers */
215 #if defined(TARGET_PPC64)
217 /* multiply high word */
218 uint64_t helper_mulhd (uint64_t arg1, uint64_t arg2)
222 muls64(&tl, &th, arg1, arg2);
226 /* multiply high word unsigned */
227 uint64_t helper_mulhdu (uint64_t arg1, uint64_t arg2)
231 mulu64(&tl, &th, arg1, arg2);
235 uint64_t helper_mulldo (uint64_t arg1, uint64_t arg2)
240 muls64(&tl, (uint64_t *)&th, arg1, arg2);
241 /* If th != 0 && th != -1, then we had an overflow */
242 if (likely((uint64_t)(th + 1) <= 1)) {
243 env->xer &= ~(1 << XER_OV);
245 env->xer |= (1 << XER_OV) | (1 << XER_SO);
251 target_ulong helper_cntlzw (target_ulong t)
256 #if defined(TARGET_PPC64)
257 target_ulong helper_cntlzd (target_ulong t)
263 /* shift right arithmetic helper */
264 target_ulong helper_sraw (target_ulong value, target_ulong shift)
268 if (likely(!(shift & 0x20))) {
269 if (likely((uint32_t)shift != 0)) {
271 ret = (int32_t)value >> shift;
272 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
273 env->xer &= ~(1 << XER_CA);
275 env->xer |= (1 << XER_CA);
278 ret = (int32_t)value;
279 env->xer &= ~(1 << XER_CA);
282 ret = (int32_t)value >> 31;
284 env->xer |= (1 << XER_CA);
286 env->xer &= ~(1 << XER_CA);
289 return (target_long)ret;
292 #if defined(TARGET_PPC64)
293 target_ulong helper_srad (target_ulong value, target_ulong shift)
297 if (likely(!(shift & 0x40))) {
298 if (likely((uint64_t)shift != 0)) {
300 ret = (int64_t)value >> shift;
301 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
302 env->xer &= ~(1 << XER_CA);
304 env->xer |= (1 << XER_CA);
307 ret = (int64_t)value;
308 env->xer &= ~(1 << XER_CA);
311 ret = (int64_t)value >> 63;
313 env->xer |= (1 << XER_CA);
315 env->xer &= ~(1 << XER_CA);
322 target_ulong helper_popcntb (target_ulong val)
324 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
325 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
326 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
330 #if defined(TARGET_PPC64)
331 target_ulong helper_popcntb_64 (target_ulong val)
333 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
334 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
335 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
340 /*****************************************************************************/
341 /* Floating point operations helpers */
342 uint64_t helper_float32_to_float64(uint32_t arg)
347 d.d = float32_to_float64(f.f, &env->fp_status);
351 uint32_t helper_float64_to_float32(uint64_t arg)
356 f.f = float64_to_float32(d.d, &env->fp_status);
360 static always_inline int fpisneg (float64 d)
366 return u.ll >> 63 != 0;
369 static always_inline int isden (float64 d)
375 return ((u.ll >> 52) & 0x7FF) == 0;
378 static always_inline int iszero (float64 d)
384 return (u.ll & ~0x8000000000000000ULL) == 0;
387 static always_inline int isinfinity (float64 d)
393 return ((u.ll >> 52) & 0x7FF) == 0x7FF &&
394 (u.ll & 0x000FFFFFFFFFFFFFULL) == 0;
397 #ifdef CONFIG_SOFTFLOAT
398 static always_inline int isfinite (float64 d)
404 return (((u.ll >> 52) & 0x7FF) != 0x7FF);
407 static always_inline int isnormal (float64 d)
413 uint32_t exp = (u.ll >> 52) & 0x7FF;
414 return ((0 < exp) && (exp < 0x7FF));
418 uint32_t helper_compute_fprf (uint64_t arg, uint32_t set_fprf)
424 isneg = fpisneg(farg.d);
425 if (unlikely(float64_is_nan(farg.d))) {
426 if (float64_is_signaling_nan(farg.d)) {
427 /* Signaling NaN: flags are undefined */
433 } else if (unlikely(isinfinity(farg.d))) {
440 if (iszero(farg.d)) {
448 /* Denormalized numbers */
451 /* Normalized numbers */
462 /* We update FPSCR_FPRF */
463 env->fpscr &= ~(0x1F << FPSCR_FPRF);
464 env->fpscr |= ret << FPSCR_FPRF;
466 /* We just need fpcc to update Rc1 */
470 /* Floating-point invalid operations exception */
471 static always_inline uint64_t fload_invalid_op_excp (int op)
477 if (op & POWERPC_EXCP_FP_VXSNAN) {
478 /* Operation on signaling NaN */
479 env->fpscr |= 1 << FPSCR_VXSNAN;
481 if (op & POWERPC_EXCP_FP_VXSOFT) {
482 /* Software-defined condition */
483 env->fpscr |= 1 << FPSCR_VXSOFT;
485 switch (op & ~(POWERPC_EXCP_FP_VXSOFT | POWERPC_EXCP_FP_VXSNAN)) {
486 case POWERPC_EXCP_FP_VXISI:
487 /* Magnitude subtraction of infinities */
488 env->fpscr |= 1 << FPSCR_VXISI;
490 case POWERPC_EXCP_FP_VXIDI:
491 /* Division of infinity by infinity */
492 env->fpscr |= 1 << FPSCR_VXIDI;
494 case POWERPC_EXCP_FP_VXZDZ:
495 /* Division of zero by zero */
496 env->fpscr |= 1 << FPSCR_VXZDZ;
498 case POWERPC_EXCP_FP_VXIMZ:
499 /* Multiplication of zero by infinity */
500 env->fpscr |= 1 << FPSCR_VXIMZ;
502 case POWERPC_EXCP_FP_VXVC:
503 /* Ordered comparison of NaN */
504 env->fpscr |= 1 << FPSCR_VXVC;
505 env->fpscr &= ~(0xF << FPSCR_FPCC);
506 env->fpscr |= 0x11 << FPSCR_FPCC;
507 /* We must update the target FPR before raising the exception */
509 env->exception_index = POWERPC_EXCP_PROGRAM;
510 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
511 /* Update the floating-point enabled exception summary */
512 env->fpscr |= 1 << FPSCR_FEX;
513 /* Exception is differed */
517 case POWERPC_EXCP_FP_VXSQRT:
518 /* Square root of a negative number */
519 env->fpscr |= 1 << FPSCR_VXSQRT;
521 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
523 /* Set the result to quiet NaN */
525 env->fpscr &= ~(0xF << FPSCR_FPCC);
526 env->fpscr |= 0x11 << FPSCR_FPCC;
529 case POWERPC_EXCP_FP_VXCVI:
530 /* Invalid conversion */
531 env->fpscr |= 1 << FPSCR_VXCVI;
532 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
534 /* Set the result to quiet NaN */
536 env->fpscr &= ~(0xF << FPSCR_FPCC);
537 env->fpscr |= 0x11 << FPSCR_FPCC;
541 /* Update the floating-point invalid operation summary */
542 env->fpscr |= 1 << FPSCR_VX;
543 /* Update the floating-point exception summary */
544 env->fpscr |= 1 << FPSCR_FX;
546 /* Update the floating-point enabled exception summary */
547 env->fpscr |= 1 << FPSCR_FEX;
548 if (msr_fe0 != 0 || msr_fe1 != 0)
549 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_FP | op);
554 static always_inline uint64_t float_zero_divide_excp (uint64_t arg1, uint64_t arg2)
556 env->fpscr |= 1 << FPSCR_ZX;
557 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
558 /* Update the floating-point exception summary */
559 env->fpscr |= 1 << FPSCR_FX;
561 /* Update the floating-point enabled exception summary */
562 env->fpscr |= 1 << FPSCR_FEX;
563 if (msr_fe0 != 0 || msr_fe1 != 0) {
564 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
565 POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
568 /* Set the result to infinity */
569 arg1 = ((arg1 ^ arg2) & 0x8000000000000000ULL);
570 arg1 |= 0x7FFULL << 52;
575 static always_inline void float_overflow_excp (void)
577 env->fpscr |= 1 << FPSCR_OX;
578 /* Update the floating-point exception summary */
579 env->fpscr |= 1 << FPSCR_FX;
581 /* XXX: should adjust the result */
582 /* Update the floating-point enabled exception summary */
583 env->fpscr |= 1 << FPSCR_FEX;
584 /* We must update the target FPR before raising the exception */
585 env->exception_index = POWERPC_EXCP_PROGRAM;
586 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
588 env->fpscr |= 1 << FPSCR_XX;
589 env->fpscr |= 1 << FPSCR_FI;
593 static always_inline void float_underflow_excp (void)
595 env->fpscr |= 1 << FPSCR_UX;
596 /* Update the floating-point exception summary */
597 env->fpscr |= 1 << FPSCR_FX;
599 /* XXX: should adjust the result */
600 /* Update the floating-point enabled exception summary */
601 env->fpscr |= 1 << FPSCR_FEX;
602 /* We must update the target FPR before raising the exception */
603 env->exception_index = POWERPC_EXCP_PROGRAM;
604 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
608 static always_inline void float_inexact_excp (void)
610 env->fpscr |= 1 << FPSCR_XX;
611 /* Update the floating-point exception summary */
612 env->fpscr |= 1 << FPSCR_FX;
614 /* Update the floating-point enabled exception summary */
615 env->fpscr |= 1 << FPSCR_FEX;
616 /* We must update the target FPR before raising the exception */
617 env->exception_index = POWERPC_EXCP_PROGRAM;
618 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
622 static always_inline void fpscr_set_rounding_mode (void)
626 /* Set rounding mode */
629 /* Best approximation (round to nearest) */
630 rnd_type = float_round_nearest_even;
633 /* Smaller magnitude (round toward zero) */
634 rnd_type = float_round_to_zero;
637 /* Round toward +infinite */
638 rnd_type = float_round_up;
642 /* Round toward -infinite */
643 rnd_type = float_round_down;
646 set_float_rounding_mode(rnd_type, &env->fp_status);
649 void helper_fpscr_setbit (uint32_t bit)
653 prev = (env->fpscr >> bit) & 1;
654 env->fpscr |= 1 << bit;
658 env->fpscr |= 1 << FPSCR_FX;
662 env->fpscr |= 1 << FPSCR_FX;
667 env->fpscr |= 1 << FPSCR_FX;
672 env->fpscr |= 1 << FPSCR_FX;
677 env->fpscr |= 1 << FPSCR_FX;
690 env->fpscr |= 1 << FPSCR_VX;
691 env->fpscr |= 1 << FPSCR_FX;
698 env->error_code = POWERPC_EXCP_FP;
700 env->error_code |= POWERPC_EXCP_FP_VXSNAN;
702 env->error_code |= POWERPC_EXCP_FP_VXISI;
704 env->error_code |= POWERPC_EXCP_FP_VXIDI;
706 env->error_code |= POWERPC_EXCP_FP_VXZDZ;
708 env->error_code |= POWERPC_EXCP_FP_VXIMZ;
710 env->error_code |= POWERPC_EXCP_FP_VXVC;
712 env->error_code |= POWERPC_EXCP_FP_VXSOFT;
714 env->error_code |= POWERPC_EXCP_FP_VXSQRT;
716 env->error_code |= POWERPC_EXCP_FP_VXCVI;
723 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
730 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
737 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
744 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
750 fpscr_set_rounding_mode();
755 /* Update the floating-point enabled exception summary */
756 env->fpscr |= 1 << FPSCR_FEX;
757 /* We have to update Rc1 before raising the exception */
758 env->exception_index = POWERPC_EXCP_PROGRAM;
764 void helper_store_fpscr (uint64_t arg, uint32_t mask)
767 * We use only the 32 LSB of the incoming fpr
775 new |= prev & 0x90000000;
776 for (i = 0; i < 7; i++) {
777 if (mask & (1 << i)) {
778 env->fpscr &= ~(0xF << (4 * i));
779 env->fpscr |= new & (0xF << (4 * i));
782 /* Update VX and FEX */
784 env->fpscr |= 1 << FPSCR_VX;
786 env->fpscr &= ~(1 << FPSCR_VX);
787 if ((fpscr_ex & fpscr_eex) != 0) {
788 env->fpscr |= 1 << FPSCR_FEX;
789 env->exception_index = POWERPC_EXCP_PROGRAM;
790 /* XXX: we should compute it properly */
791 env->error_code = POWERPC_EXCP_FP;
794 env->fpscr &= ~(1 << FPSCR_FEX);
795 fpscr_set_rounding_mode();
798 void helper_float_check_status (void)
800 #ifdef CONFIG_SOFTFLOAT
801 if (env->exception_index == POWERPC_EXCP_PROGRAM &&
802 (env->error_code & POWERPC_EXCP_FP)) {
803 /* Differred floating-point exception after target FPR update */
804 if (msr_fe0 != 0 || msr_fe1 != 0)
805 raise_exception_err(env, env->exception_index, env->error_code);
806 } else if (env->fp_status.float_exception_flags & float_flag_overflow) {
807 float_overflow_excp();
808 } else if (env->fp_status.float_exception_flags & float_flag_underflow) {
809 float_underflow_excp();
810 } else if (env->fp_status.float_exception_flags & float_flag_inexact) {
811 float_inexact_excp();
814 if (env->exception_index == POWERPC_EXCP_PROGRAM &&
815 (env->error_code & POWERPC_EXCP_FP)) {
816 /* Differred floating-point exception after target FPR update */
817 if (msr_fe0 != 0 || msr_fe1 != 0)
818 raise_exception_err(env, env->exception_index, env->error_code);
824 #ifdef CONFIG_SOFTFLOAT
825 void helper_reset_fpstatus (void)
827 env->fp_status.float_exception_flags = 0;
832 uint64_t helper_fadd (uint64_t arg1, uint64_t arg2)
834 CPU_DoubleU farg1, farg2;
838 #if USE_PRECISE_EMULATION
839 if (unlikely(float64_is_signaling_nan(farg1.d) ||
840 float64_is_signaling_nan(farg2.d))) {
842 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
843 } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
844 fpisneg(farg1.d) == fpisneg(farg2.d))) {
845 farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
847 /* Magnitude subtraction of infinities */
848 farg1.ll == fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
851 farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
857 uint64_t helper_fsub (uint64_t arg1, uint64_t arg2)
859 CPU_DoubleU farg1, farg2;
863 #if USE_PRECISE_EMULATION
865 if (unlikely(float64_is_signaling_nan(farg1.d) ||
866 float64_is_signaling_nan(farg2.d))) {
867 /* sNaN subtraction */
868 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
869 } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
870 fpisneg(farg1.d) != fpisneg(farg2.d))) {
871 farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
873 /* Magnitude subtraction of infinities */
874 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
878 farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
884 uint64_t helper_fmul (uint64_t arg1, uint64_t arg2)
886 CPU_DoubleU farg1, farg2;
890 #if USE_PRECISE_EMULATION
891 if (unlikely(float64_is_signaling_nan(farg1.d) ||
892 float64_is_signaling_nan(farg2.d))) {
893 /* sNaN multiplication */
894 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
895 } else if (unlikely((isinfinity(farg1.d) && iszero(farg2.d)) ||
896 (iszero(farg1.d) && isinfinity(farg2.d)))) {
897 /* Multiplication of zero by infinity */
898 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
900 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
904 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
910 uint64_t helper_fdiv (uint64_t arg1, uint64_t arg2)
912 CPU_DoubleU farg1, farg2;
916 #if USE_PRECISE_EMULATION
917 if (unlikely(float64_is_signaling_nan(farg1.d) ||
918 float64_is_signaling_nan(farg2.d))) {
920 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
921 } else if (unlikely(isinfinity(farg1.d) && isinfinity(farg2.d))) {
922 /* Division of infinity by infinity */
923 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
924 } else if (unlikely(iszero(farg2.d))) {
925 if (iszero(farg1.d)) {
926 /* Division of zero by zero */
927 farg1.ll fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
929 /* Division by zero */
930 farg1.ll = float_zero_divide_excp(farg1.d, farg2.d);
933 farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
936 farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
942 uint64_t helper_fabs (uint64_t arg)
947 farg.d = float64_abs(farg.d);
952 uint64_t helper_fnabs (uint64_t arg)
957 farg.d = float64_abs(farg.d);
958 farg.d = float64_chs(farg.d);
963 uint64_t helper_fneg (uint64_t arg)
968 farg.d = float64_chs(farg.d);
973 uint64_t helper_fctiw (uint64_t arg)
978 if (unlikely(float64_is_signaling_nan(farg.d))) {
979 /* sNaN conversion */
980 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
981 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
982 /* qNan / infinity conversion */
983 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
985 farg.ll = float64_to_int32(farg.d, &env->fp_status);
986 #if USE_PRECISE_EMULATION
987 /* XXX: higher bits are not supposed to be significant.
988 * to make tests easier, return the same as a real PowerPC 750
990 farg.ll |= 0xFFF80000ULL << 32;
996 /* fctiwz - fctiwz. */
997 uint64_t helper_fctiwz (uint64_t arg)
1002 if (unlikely(float64_is_signaling_nan(farg.d))) {
1003 /* sNaN conversion */
1004 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1005 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1006 /* qNan / infinity conversion */
1007 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1009 farg.ll = float64_to_int32_round_to_zero(farg.d, &env->fp_status);
1010 #if USE_PRECISE_EMULATION
1011 /* XXX: higher bits are not supposed to be significant.
1012 * to make tests easier, return the same as a real PowerPC 750
1014 farg.ll |= 0xFFF80000ULL << 32;
1020 #if defined(TARGET_PPC64)
1021 /* fcfid - fcfid. */
1022 uint64_t helper_fcfid (uint64_t arg)
1025 farg.d = int64_to_float64(arg, &env->fp_status);
1029 /* fctid - fctid. */
1030 uint64_t helper_fctid (uint64_t arg)
1035 if (unlikely(float64_is_signaling_nan(farg.d))) {
1036 /* sNaN conversion */
1037 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1038 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1039 /* qNan / infinity conversion */
1040 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1042 farg.ll = float64_to_int64(farg.d, &env->fp_status);
1047 /* fctidz - fctidz. */
1048 uint64_t helper_fctidz (uint64_t arg)
1053 if (unlikely(float64_is_signaling_nan(farg.d))) {
1054 /* sNaN conversion */
1055 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1056 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1057 /* qNan / infinity conversion */
1058 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1060 farg.ll = float64_to_int64_round_to_zero(farg.d, &env->fp_status);
1067 static always_inline uint64_t do_fri (uint64_t arg, int rounding_mode)
1072 if (unlikely(float64_is_signaling_nan(farg.d))) {
1074 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1075 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
1076 /* qNan / infinity round */
1077 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1079 set_float_rounding_mode(rounding_mode, &env->fp_status);
1080 farg.ll = float64_round_to_int(farg.d, &env->fp_status);
1081 /* Restore rounding mode from FPSCR */
1082 fpscr_set_rounding_mode();
1087 uint64_t helper_frin (uint64_t arg)
1089 return do_fri(arg, float_round_nearest_even);
1092 uint64_t helper_friz (uint64_t arg)
1094 return do_fri(arg, float_round_to_zero);
1097 uint64_t helper_frip (uint64_t arg)
1099 return do_fri(arg, float_round_up);
1102 uint64_t helper_frim (uint64_t arg)
1104 return do_fri(arg, float_round_down);
1107 /* fmadd - fmadd. */
1108 uint64_t helper_fmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1110 CPU_DoubleU farg1, farg2, farg3;
1115 #if USE_PRECISE_EMULATION
1116 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1117 float64_is_signaling_nan(farg2.d) ||
1118 float64_is_signaling_nan(farg3.d))) {
1119 /* sNaN operation */
1120 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1123 /* This is the way the PowerPC specification defines it */
1124 float128 ft0_128, ft1_128;
1126 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1127 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1128 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1129 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1130 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1131 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1133 /* This is OK on x86 hosts */
1134 farg1.d = (farg1.d * farg2.d) + farg3.d;
1138 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1139 farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1144 /* fmsub - fmsub. */
1145 uint64_t helper_fmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1147 CPU_DoubleU farg1, farg2, farg3;
1152 #if USE_PRECISE_EMULATION
1153 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1154 float64_is_signaling_nan(farg2.d) ||
1155 float64_is_signaling_nan(farg3.d))) {
1156 /* sNaN operation */
1157 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1160 /* This is the way the PowerPC specification defines it */
1161 float128 ft0_128, ft1_128;
1163 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1164 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1165 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1166 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1167 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1168 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1170 /* This is OK on x86 hosts */
1171 farg1.d = (farg1.d * farg2.d) - farg3.d;
1175 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1176 farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1181 /* fnmadd - fnmadd. */
1182 uint64_t helper_fnmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1184 CPU_DoubleU farg1, farg2, farg3;
1190 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1191 float64_is_signaling_nan(farg2.d) ||
1192 float64_is_signaling_nan(farg3.d))) {
1193 /* sNaN operation */
1194 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1196 #if USE_PRECISE_EMULATION
1198 /* This is the way the PowerPC specification defines it */
1199 float128 ft0_128, ft1_128;
1201 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1202 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1203 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1204 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1205 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1206 farg1.d= float128_to_float64(ft0_128, &env->fp_status);
1208 /* This is OK on x86 hosts */
1209 farg1.d = (farg1.d * farg2.d) + farg3.d;
1212 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1213 farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1215 if (likely(!isnan(farg1.d)))
1216 farg1.d = float64_chs(farg1.d);
1221 /* fnmsub - fnmsub. */
1222 uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1224 CPU_DoubleU farg1, farg2, farg3;
1230 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1231 float64_is_signaling_nan(farg2.d) ||
1232 float64_is_signaling_nan(farg3.d))) {
1233 /* sNaN operation */
1234 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1236 #if USE_PRECISE_EMULATION
1238 /* This is the way the PowerPC specification defines it */
1239 float128 ft0_128, ft1_128;
1241 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1242 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1243 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1244 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1245 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1246 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1248 /* This is OK on x86 hosts */
1249 farg1.d = (farg1.d * farg2.d) - farg3.d;
1252 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1253 farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1255 if (likely(!isnan(farg1.d)))
1256 farg1.d = float64_chs(farg1.d);
1262 uint64_t helper_frsp (uint64_t arg)
1267 #if USE_PRECISE_EMULATION
1268 if (unlikely(float64_is_signaling_nan(farg.d))) {
1269 /* sNaN square root */
1270 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1272 fard.d = float64_to_float32(farg.d, &env->fp_status);
1275 farg.d = float64_to_float32(farg.d, &env->fp_status);
1280 /* fsqrt - fsqrt. */
1281 uint64_t helper_fsqrt (uint64_t arg)
1286 if (unlikely(float64_is_signaling_nan(farg.d))) {
1287 /* sNaN square root */
1288 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1289 } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
1290 /* Square root of a negative nonzero number */
1291 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1293 farg.d = float64_sqrt(farg.d, &env->fp_status);
1299 uint64_t helper_fre (uint64_t arg)
1304 if (unlikely(float64_is_signaling_nan(farg.d))) {
1305 /* sNaN reciprocal */
1306 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1307 } else if (unlikely(iszero(farg.d))) {
1308 /* Zero reciprocal */
1309 farg.ll = float_zero_divide_excp(1.0, farg.d);
1310 } else if (likely(isnormal(farg.d))) {
1311 farg.d = float64_div(1.0, farg.d, &env->fp_status);
1313 if (farg.ll == 0x8000000000000000ULL) {
1314 farg.ll = 0xFFF0000000000000ULL;
1315 } else if (farg.ll == 0x0000000000000000ULL) {
1316 farg.ll = 0x7FF0000000000000ULL;
1317 } else if (isnan(farg.d)) {
1318 farg.ll = 0x7FF8000000000000ULL;
1319 } else if (fpisneg(farg.d)) {
1320 farg.ll = 0x8000000000000000ULL;
1322 farg.ll = 0x0000000000000000ULL;
1329 uint64_t helper_fres (uint64_t arg)
1334 if (unlikely(float64_is_signaling_nan(farg.d))) {
1335 /* sNaN reciprocal */
1336 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1337 } else if (unlikely(iszero(farg.d))) {
1338 /* Zero reciprocal */
1339 farg.ll = float_zero_divide_excp(1.0, farg.d);
1340 } else if (likely(isnormal(farg.d))) {
1341 #if USE_PRECISE_EMULATION
1342 farg.d = float64_div(1.0, farg.d, &env->fp_status);
1343 farg.d = float64_to_float32(farg.d, &env->fp_status);
1345 farg.d = float32_div(1.0, farg.d, &env->fp_status);
1348 if (farg.ll == 0x8000000000000000ULL) {
1349 farg.ll = 0xFFF0000000000000ULL;
1350 } else if (farg.ll == 0x0000000000000000ULL) {
1351 farg.ll = 0x7FF0000000000000ULL;
1352 } else if (isnan(farg.d)) {
1353 farg.ll = 0x7FF8000000000000ULL;
1354 } else if (fpisneg(farg.d)) {
1355 farg.ll = 0x8000000000000000ULL;
1357 farg.ll = 0x0000000000000000ULL;
1363 /* frsqrte - frsqrte. */
1364 uint64_t helper_frsqrte (uint64_t arg)
1369 if (unlikely(float64_is_signaling_nan(farg.d))) {
1370 /* sNaN reciprocal square root */
1371 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1372 } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
1373 /* Reciprocal square root of a negative nonzero number */
1374 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1375 } else if (likely(isnormal(farg.d))) {
1376 farg.d = float64_sqrt(farg.d, &env->fp_status);
1377 farg.d = float32_div(1.0, farg.d, &env->fp_status);
1379 if (farg.ll == 0x8000000000000000ULL) {
1380 farg.ll = 0xFFF0000000000000ULL;
1381 } else if (farg.ll == 0x0000000000000000ULL) {
1382 farg.ll = 0x7FF0000000000000ULL;
1383 } else if (isnan(farg.d)) {
1384 farg.ll |= 0x000FFFFFFFFFFFFFULL;
1385 } else if (fpisneg(farg.d)) {
1386 farg.ll = 0x7FF8000000000000ULL;
1388 farg.ll = 0x0000000000000000ULL;
1395 uint64_t helper_fsel (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1397 CPU_DoubleU farg1, farg2, farg3;
1403 if (!fpisneg(farg1.d) || iszero(farg1.d))
1409 uint32_t helper_fcmpu (uint64_t arg1, uint64_t arg2)
1411 CPU_DoubleU farg1, farg2;
1416 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1417 float64_is_signaling_nan(farg2.d))) {
1418 /* sNaN comparison */
1419 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1421 if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1423 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1429 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1430 env->fpscr |= ret << FPSCR_FPRF;
1434 uint32_t helper_fcmpo (uint64_t arg1, uint64_t arg2)
1436 CPU_DoubleU farg1, farg2;
1441 if (unlikely(float64_is_nan(farg1.d) ||
1442 float64_is_nan(farg2.d))) {
1443 if (float64_is_signaling_nan(farg1.d) ||
1444 float64_is_signaling_nan(farg2.d)) {
1445 /* sNaN comparison */
1446 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN |
1447 POWERPC_EXCP_FP_VXVC);
1449 /* qNaN comparison */
1450 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
1453 if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1455 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1461 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1462 env->fpscr |= ret << FPSCR_FPRF;
1466 #if !defined (CONFIG_USER_ONLY)
1467 void cpu_dump_rfi (target_ulong RA, target_ulong msr);
1469 void do_store_msr (void)
1471 T0 = hreg_store_msr(env, T0, 0);
1473 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1474 raise_exception(env, T0);
1478 static always_inline void __do_rfi (target_ulong nip, target_ulong msr,
1479 target_ulong msrm, int keep_msrh)
1481 #if defined(TARGET_PPC64)
1482 if (msr & (1ULL << MSR_SF)) {
1483 nip = (uint64_t)nip;
1484 msr &= (uint64_t)msrm;
1486 nip = (uint32_t)nip;
1487 msr = (uint32_t)(msr & msrm);
1489 msr |= env->msr & ~((uint64_t)0xFFFFFFFF);
1492 nip = (uint32_t)nip;
1493 msr &= (uint32_t)msrm;
1495 /* XXX: beware: this is false if VLE is supported */
1496 env->nip = nip & ~((target_ulong)0x00000003);
1497 hreg_store_msr(env, msr, 1);
1498 #if defined (DEBUG_OP)
1499 cpu_dump_rfi(env->nip, env->msr);
1501 /* No need to raise an exception here,
1502 * as rfi is always the last insn of a TB
1504 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1509 __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1510 ~((target_ulong)0xFFFF0000), 1);
1513 #if defined(TARGET_PPC64)
1516 __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1517 ~((target_ulong)0xFFFF0000), 0);
1520 void do_hrfid (void)
1522 __do_rfi(env->spr[SPR_HSRR0], env->spr[SPR_HSRR1],
1523 ~((target_ulong)0xFFFF0000), 0);
1528 void helper_tw (target_ulong arg1, target_ulong arg2, uint32_t flags)
1530 if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
1531 ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
1532 ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
1533 ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
1534 ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
1535 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1539 #if defined(TARGET_PPC64)
1540 void helper_td (target_ulong arg1, target_ulong arg2, uint32_t flags)
1542 if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
1543 ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
1544 ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
1545 ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
1546 ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01)))))
1547 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1551 /*****************************************************************************/
1552 /* PowerPC 601 specific instructions (POWER bridge) */
1553 void do_POWER_abso (void)
1555 if ((int32_t)T0 == INT32_MIN) {
1557 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1558 } else if ((int32_t)T0 < 0) {
1560 env->xer &= ~(1 << XER_OV);
1562 env->xer &= ~(1 << XER_OV);
1566 void do_POWER_clcs (void)
1570 /* Instruction cache line size */
1571 T0 = env->icache_line_size;
1574 /* Data cache line size */
1575 T0 = env->dcache_line_size;
1578 /* Minimum cache line size */
1579 T0 = env->icache_line_size < env->dcache_line_size ?
1580 env->icache_line_size : env->dcache_line_size;
1583 /* Maximum cache line size */
1584 T0 = env->icache_line_size > env->dcache_line_size ?
1585 env->icache_line_size : env->dcache_line_size;
1593 void do_POWER_div (void)
1597 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1599 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1600 env->spr[SPR_MQ] = 0;
1602 tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1603 env->spr[SPR_MQ] = tmp % T1;
1604 T0 = tmp / (int32_t)T1;
1608 void do_POWER_divo (void)
1612 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1614 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1615 env->spr[SPR_MQ] = 0;
1616 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1618 tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1619 env->spr[SPR_MQ] = tmp % T1;
1621 if (tmp > (int64_t)INT32_MAX || tmp < (int64_t)INT32_MIN) {
1622 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1624 env->xer &= ~(1 << XER_OV);
1630 void do_POWER_divs (void)
1632 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1634 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1635 env->spr[SPR_MQ] = 0;
1637 env->spr[SPR_MQ] = T0 % T1;
1638 T0 = (int32_t)T0 / (int32_t)T1;
1642 void do_POWER_divso (void)
1644 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1646 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1647 env->spr[SPR_MQ] = 0;
1648 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1650 T0 = (int32_t)T0 / (int32_t)T1;
1651 env->spr[SPR_MQ] = (int32_t)T0 % (int32_t)T1;
1652 env->xer &= ~(1 << XER_OV);
1656 void do_POWER_dozo (void)
1658 if ((int32_t)T1 > (int32_t)T0) {
1661 if (((uint32_t)(~T2) ^ (uint32_t)T1 ^ UINT32_MAX) &
1662 ((uint32_t)(~T2) ^ (uint32_t)T0) & (1UL << 31)) {
1663 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1665 env->xer &= ~(1 << XER_OV);
1669 env->xer &= ~(1 << XER_OV);
1673 void do_POWER_maskg (void)
1677 if ((uint32_t)T0 == (uint32_t)(T1 + 1)) {
1680 ret = (UINT32_MAX >> ((uint32_t)T0)) ^
1681 ((UINT32_MAX >> ((uint32_t)T1)) >> 1);
1682 if ((uint32_t)T0 > (uint32_t)T1)
1688 void do_POWER_mulo (void)
1692 tmp = (uint64_t)T0 * (uint64_t)T1;
1693 env->spr[SPR_MQ] = tmp >> 32;
1695 if (tmp >> 32 != ((uint64_t)T0 >> 16) * ((uint64_t)T1 >> 16)) {
1696 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1698 env->xer &= ~(1 << XER_OV);
1702 #if !defined (CONFIG_USER_ONLY)
1703 void do_POWER_rac (void)
1708 /* We don't have to generate many instances of this instruction,
1709 * as rac is supervisor only.
1711 /* XXX: FIX THIS: Pretend we have no BAT */
1712 nb_BATs = env->nb_BATs;
1714 if (get_physical_address(env, &ctx, T0, 0, ACCESS_INT) == 0)
1716 env->nb_BATs = nb_BATs;
1719 void do_POWER_rfsvc (void)
1721 __do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
1724 void do_store_hid0_601 (void)
1728 hid0 = env->spr[SPR_HID0];
1729 if ((T0 ^ hid0) & 0x00000008) {
1730 /* Change current endianness */
1731 env->hflags &= ~(1 << MSR_LE);
1732 env->hflags_nmsr &= ~(1 << MSR_LE);
1733 env->hflags_nmsr |= (1 << MSR_LE) & (((T0 >> 3) & 1) << MSR_LE);
1734 env->hflags |= env->hflags_nmsr;
1735 if (loglevel != 0) {
1736 fprintf(logfile, "%s: set endianness to %c => " ADDRX "\n",
1737 __func__, T0 & 0x8 ? 'l' : 'b', env->hflags);
1740 env->spr[SPR_HID0] = T0;
1744 /*****************************************************************************/
1745 /* 602 specific instructions */
1746 /* mfrom is the most crazy instruction ever seen, imho ! */
1747 /* Real implementation uses a ROM table. Do the same */
1748 #define USE_MFROM_ROM_TABLE
1749 target_ulong helper_602_mfrom (target_ulong arg)
1751 if (likely(arg < 602)) {
1752 #if defined(USE_MFROM_ROM_TABLE)
1753 #include "mfrom_table.c"
1754 return mfrom_ROM_table[T0];
1757 /* Extremly decomposed:
1759 * return 256 * log10(10 + 1.0) + 0.5
1762 d = float64_div(d, 256, &env->fp_status);
1764 d = exp10(d); // XXX: use float emulation function
1765 d = float64_add(d, 1.0, &env->fp_status);
1766 d = log10(d); // XXX: use float emulation function
1767 d = float64_mul(d, 256, &env->fp_status);
1768 d = float64_add(d, 0.5, &env->fp_status);
1769 return float64_round_to_int(d, &env->fp_status);
1776 /*****************************************************************************/
1777 /* Embedded PowerPC specific helpers */
1779 /* XXX: to be improved to check access rights when in user-mode */
1780 void do_load_dcr (void)
1784 if (unlikely(env->dcr_env == NULL)) {
1785 if (loglevel != 0) {
1786 fprintf(logfile, "No DCR environment\n");
1788 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1789 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1790 } else if (unlikely(ppc_dcr_read(env->dcr_env, T0, &val) != 0)) {
1791 if (loglevel != 0) {
1792 fprintf(logfile, "DCR read error %d %03x\n", (int)T0, (int)T0);
1794 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1795 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1801 void do_store_dcr (void)
1803 if (unlikely(env->dcr_env == NULL)) {
1804 if (loglevel != 0) {
1805 fprintf(logfile, "No DCR environment\n");
1807 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1808 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1809 } else if (unlikely(ppc_dcr_write(env->dcr_env, T0, T1) != 0)) {
1810 if (loglevel != 0) {
1811 fprintf(logfile, "DCR write error %d %03x\n", (int)T0, (int)T0);
1813 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1814 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1818 #if !defined(CONFIG_USER_ONLY)
1819 void do_40x_rfci (void)
1821 __do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1822 ~((target_ulong)0xFFFF0000), 0);
1827 __do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1828 ~((target_ulong)0x3FFF0000), 0);
1833 __do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1834 ~((target_ulong)0x3FFF0000), 0);
1837 void do_rfmci (void)
1839 __do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1840 ~((target_ulong)0x3FFF0000), 0);
1843 void do_load_403_pb (int num)
1848 void do_store_403_pb (int num)
1850 if (likely(env->pb[num] != T0)) {
1852 /* Should be optimized */
1859 void do_440_dlmzb (void)
1865 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1866 if ((T0 & mask) == 0)
1870 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1871 if ((T1 & mask) == 0)
1879 /*****************************************************************************/
1880 /* SPE extension helpers */
1881 /* Use a table to make this quicker */
1882 static uint8_t hbrev[16] = {
1883 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1884 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1887 static always_inline uint8_t byte_reverse (uint8_t val)
1889 return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
1892 static always_inline uint32_t word_reverse (uint32_t val)
1894 return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
1895 (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
1898 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
1899 target_ulong helper_brinc (target_ulong arg1, target_ulong arg2)
1901 uint32_t a, b, d, mask;
1903 mask = UINT32_MAX >> (32 - MASKBITS);
1906 d = word_reverse(1 + word_reverse(a | ~b));
1907 return (arg1 & ~mask) | (d & b);
1910 uint32_t helper_cntlsw32 (uint32_t val)
1912 if (val & 0x80000000)
1918 uint32_t helper_cntlzw32 (uint32_t val)
1923 /* Single-precision floating-point conversions */
1924 static always_inline uint32_t efscfsi (uint32_t val)
1928 u.f = int32_to_float32(val, &env->spe_status);
1933 static always_inline uint32_t efscfui (uint32_t val)
1937 u.f = uint32_to_float32(val, &env->spe_status);
1942 static always_inline int32_t efsctsi (uint32_t val)
1947 /* NaN are not treated the same way IEEE 754 does */
1948 if (unlikely(isnan(u.f)))
1951 return float32_to_int32(u.f, &env->spe_status);
1954 static always_inline uint32_t efsctui (uint32_t val)
1959 /* NaN are not treated the same way IEEE 754 does */
1960 if (unlikely(isnan(u.f)))
1963 return float32_to_uint32(u.f, &env->spe_status);
1966 static always_inline uint32_t efsctsiz (uint32_t val)
1971 /* NaN are not treated the same way IEEE 754 does */
1972 if (unlikely(isnan(u.f)))
1975 return float32_to_int32_round_to_zero(u.f, &env->spe_status);
1978 static always_inline uint32_t efsctuiz (uint32_t val)
1983 /* NaN are not treated the same way IEEE 754 does */
1984 if (unlikely(isnan(u.f)))
1987 return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
1990 static always_inline uint32_t efscfsf (uint32_t val)
1995 u.f = int32_to_float32(val, &env->spe_status);
1996 tmp = int64_to_float32(1ULL << 32, &env->spe_status);
1997 u.f = float32_div(u.f, tmp, &env->spe_status);
2002 static always_inline uint32_t efscfuf (uint32_t val)
2007 u.f = uint32_to_float32(val, &env->spe_status);
2008 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2009 u.f = float32_div(u.f, tmp, &env->spe_status);
2014 static always_inline uint32_t efsctsf (uint32_t val)
2020 /* NaN are not treated the same way IEEE 754 does */
2021 if (unlikely(isnan(u.f)))
2023 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2024 u.f = float32_mul(u.f, tmp, &env->spe_status);
2026 return float32_to_int32(u.f, &env->spe_status);
2029 static always_inline uint32_t efsctuf (uint32_t val)
2035 /* NaN are not treated the same way IEEE 754 does */
2036 if (unlikely(isnan(u.f)))
2038 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2039 u.f = float32_mul(u.f, tmp, &env->spe_status);
2041 return float32_to_uint32(u.f, &env->spe_status);
2044 #define HELPER_SPE_SINGLE_CONV(name) \
2045 uint32_t helper_e##name (uint32_t val) \
2047 return e##name(val); \
2050 HELPER_SPE_SINGLE_CONV(fscfsi);
2052 HELPER_SPE_SINGLE_CONV(fscfui);
2054 HELPER_SPE_SINGLE_CONV(fscfuf);
2056 HELPER_SPE_SINGLE_CONV(fscfsf);
2058 HELPER_SPE_SINGLE_CONV(fsctsi);
2060 HELPER_SPE_SINGLE_CONV(fsctui);
2062 HELPER_SPE_SINGLE_CONV(fsctsiz);
2064 HELPER_SPE_SINGLE_CONV(fsctuiz);
2066 HELPER_SPE_SINGLE_CONV(fsctsf);
2068 HELPER_SPE_SINGLE_CONV(fsctuf);
2070 #define HELPER_SPE_VECTOR_CONV(name) \
2071 uint64_t helper_ev##name (uint64_t val) \
2073 return ((uint64_t)e##name(val >> 32) << 32) | \
2074 (uint64_t)e##name(val); \
2077 HELPER_SPE_VECTOR_CONV(fscfsi);
2079 HELPER_SPE_VECTOR_CONV(fscfui);
2081 HELPER_SPE_VECTOR_CONV(fscfuf);
2083 HELPER_SPE_VECTOR_CONV(fscfsf);
2085 HELPER_SPE_VECTOR_CONV(fsctsi);
2087 HELPER_SPE_VECTOR_CONV(fsctui);
2089 HELPER_SPE_VECTOR_CONV(fsctsiz);
2091 HELPER_SPE_VECTOR_CONV(fsctuiz);
2093 HELPER_SPE_VECTOR_CONV(fsctsf);
2095 HELPER_SPE_VECTOR_CONV(fsctuf);
2097 /* Single-precision floating-point arithmetic */
2098 static always_inline uint32_t efsadd (uint32_t op1, uint32_t op2)
2103 u1.f = float32_add(u1.f, u2.f, &env->spe_status);
2107 static always_inline uint32_t efssub (uint32_t op1, uint32_t op2)
2112 u1.f = float32_sub(u1.f, u2.f, &env->spe_status);
2116 static always_inline uint32_t efsmul (uint32_t op1, uint32_t op2)
2121 u1.f = float32_mul(u1.f, u2.f, &env->spe_status);
2125 static always_inline uint32_t efsdiv (uint32_t op1, uint32_t op2)
2130 u1.f = float32_div(u1.f, u2.f, &env->spe_status);
2134 #define HELPER_SPE_SINGLE_ARITH(name) \
2135 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2137 return e##name(op1, op2); \
2140 HELPER_SPE_SINGLE_ARITH(fsadd);
2142 HELPER_SPE_SINGLE_ARITH(fssub);
2144 HELPER_SPE_SINGLE_ARITH(fsmul);
2146 HELPER_SPE_SINGLE_ARITH(fsdiv);
2148 #define HELPER_SPE_VECTOR_ARITH(name) \
2149 uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
2151 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
2152 (uint64_t)e##name(op1, op2); \
2155 HELPER_SPE_VECTOR_ARITH(fsadd);
2157 HELPER_SPE_VECTOR_ARITH(fssub);
2159 HELPER_SPE_VECTOR_ARITH(fsmul);
2161 HELPER_SPE_VECTOR_ARITH(fsdiv);
2163 /* Single-precision floating-point comparisons */
2164 static always_inline uint32_t efststlt (uint32_t op1, uint32_t op2)
2169 return float32_lt(u1.f, u2.f, &env->spe_status) ? 4 : 0;
2172 static always_inline uint32_t efststgt (uint32_t op1, uint32_t op2)
2177 return float32_le(u1.f, u2.f, &env->spe_status) ? 0 : 4;
2180 static always_inline uint32_t efststeq (uint32_t op1, uint32_t op2)
2185 return float32_eq(u1.f, u2.f, &env->spe_status) ? 4 : 0;
2188 static always_inline uint32_t efscmplt (uint32_t op1, uint32_t op2)
2190 /* XXX: TODO: test special values (NaN, infinites, ...) */
2191 return efststlt(op1, op2);
2194 static always_inline uint32_t efscmpgt (uint32_t op1, uint32_t op2)
2196 /* XXX: TODO: test special values (NaN, infinites, ...) */
2197 return efststgt(op1, op2);
2200 static always_inline uint32_t efscmpeq (uint32_t op1, uint32_t op2)
2202 /* XXX: TODO: test special values (NaN, infinites, ...) */
2203 return efststeq(op1, op2);
2206 #define HELPER_SINGLE_SPE_CMP(name) \
2207 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2209 return e##name(op1, op2) << 2; \
2212 HELPER_SINGLE_SPE_CMP(fststlt);
2214 HELPER_SINGLE_SPE_CMP(fststgt);
2216 HELPER_SINGLE_SPE_CMP(fststeq);
2218 HELPER_SINGLE_SPE_CMP(fscmplt);
2220 HELPER_SINGLE_SPE_CMP(fscmpgt);
2222 HELPER_SINGLE_SPE_CMP(fscmpeq);
2224 static always_inline uint32_t evcmp_merge (int t0, int t1)
2226 return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
2229 #define HELPER_VECTOR_SPE_CMP(name) \
2230 uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
2232 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
2235 HELPER_VECTOR_SPE_CMP(fststlt);
2237 HELPER_VECTOR_SPE_CMP(fststgt);
2239 HELPER_VECTOR_SPE_CMP(fststeq);
2241 HELPER_VECTOR_SPE_CMP(fscmplt);
2243 HELPER_VECTOR_SPE_CMP(fscmpgt);
2245 HELPER_VECTOR_SPE_CMP(fscmpeq);
2247 /* Double-precision floating-point conversion */
2248 uint64_t helper_efdcfsi (uint32_t val)
2252 u.d = int32_to_float64(val, &env->spe_status);
2257 uint64_t helper_efdcfsid (uint64_t val)
2261 u.d = int64_to_float64(val, &env->spe_status);
2266 uint64_t helper_efdcfui (uint32_t val)
2270 u.d = uint32_to_float64(val, &env->spe_status);
2275 uint64_t helper_efdcfuid (uint64_t val)
2279 u.d = uint64_to_float64(val, &env->spe_status);
2284 uint32_t helper_efdctsi (uint64_t val)
2289 /* NaN are not treated the same way IEEE 754 does */
2290 if (unlikely(isnan(u.d)))
2293 return float64_to_int32(u.d, &env->spe_status);
2296 uint32_t helper_efdctui (uint64_t val)
2301 /* NaN are not treated the same way IEEE 754 does */
2302 if (unlikely(isnan(u.d)))
2305 return float64_to_uint32(u.d, &env->spe_status);
2308 uint32_t helper_efdctsiz (uint64_t val)
2313 /* NaN are not treated the same way IEEE 754 does */
2314 if (unlikely(isnan(u.d)))
2317 return float64_to_int32_round_to_zero(u.d, &env->spe_status);
2320 uint64_t helper_efdctsidz (uint64_t val)
2325 /* NaN are not treated the same way IEEE 754 does */
2326 if (unlikely(isnan(u.d)))
2329 return float64_to_int64_round_to_zero(u.d, &env->spe_status);
2332 uint32_t helper_efdctuiz (uint64_t val)
2337 /* NaN are not treated the same way IEEE 754 does */
2338 if (unlikely(isnan(u.d)))
2341 return float64_to_uint32_round_to_zero(u.d, &env->spe_status);
2344 uint64_t helper_efdctuidz (uint64_t val)
2349 /* NaN are not treated the same way IEEE 754 does */
2350 if (unlikely(isnan(u.d)))
2353 return float64_to_uint64_round_to_zero(u.d, &env->spe_status);
2356 uint64_t helper_efdcfsf (uint32_t val)
2361 u.d = int32_to_float64(val, &env->spe_status);
2362 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2363 u.d = float64_div(u.d, tmp, &env->spe_status);
2368 uint64_t helper_efdcfuf (uint32_t val)
2373 u.d = uint32_to_float64(val, &env->spe_status);
2374 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2375 u.d = float64_div(u.d, tmp, &env->spe_status);
2380 uint32_t helper_efdctsf (uint64_t val)
2386 /* NaN are not treated the same way IEEE 754 does */
2387 if (unlikely(isnan(u.d)))
2389 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2390 u.d = float64_mul(u.d, tmp, &env->spe_status);
2392 return float64_to_int32(u.d, &env->spe_status);
2395 uint32_t helper_efdctuf (uint64_t val)
2401 /* NaN are not treated the same way IEEE 754 does */
2402 if (unlikely(isnan(u.d)))
2404 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2405 u.d = float64_mul(u.d, tmp, &env->spe_status);
2407 return float64_to_uint32(u.d, &env->spe_status);
2410 uint32_t helper_efscfd (uint64_t val)
2416 u2.f = float64_to_float32(u1.d, &env->spe_status);
2421 uint64_t helper_efdcfs (uint32_t val)
2427 u2.d = float32_to_float64(u1.f, &env->spe_status);
2432 /* Double precision fixed-point arithmetic */
2433 uint64_t helper_efdadd (uint64_t op1, uint64_t op2)
2438 u1.d = float64_add(u1.d, u2.d, &env->spe_status);
2442 uint64_t helper_efdsub (uint64_t op1, uint64_t op2)
2447 u1.d = float64_sub(u1.d, u2.d, &env->spe_status);
2451 uint64_t helper_efdmul (uint64_t op1, uint64_t op2)
2456 u1.d = float64_mul(u1.d, u2.d, &env->spe_status);
2460 uint64_t helper_efddiv (uint64_t op1, uint64_t op2)
2465 u1.d = float64_div(u1.d, u2.d, &env->spe_status);
2469 /* Double precision floating point helpers */
2470 uint32_t helper_efdtstlt (uint64_t op1, uint64_t op2)
2475 return float64_lt(u1.d, u2.d, &env->spe_status) ? 4 : 0;
2478 uint32_t helper_efdtstgt (uint64_t op1, uint64_t op2)
2483 return float64_le(u1.d, u2.d, &env->spe_status) ? 0 : 4;
2486 uint32_t helper_efdtsteq (uint64_t op1, uint64_t op2)
2491 return float64_eq(u1.d, u2.d, &env->spe_status) ? 4 : 0;
2494 uint32_t helper_efdcmplt (uint64_t op1, uint64_t op2)
2496 /* XXX: TODO: test special values (NaN, infinites, ...) */
2497 return helper_efdtstlt(op1, op2);
2500 uint32_t helper_efdcmpgt (uint64_t op1, uint64_t op2)
2502 /* XXX: TODO: test special values (NaN, infinites, ...) */
2503 return helper_efdtstgt(op1, op2);
2506 uint32_t helper_efdcmpeq (uint64_t op1, uint64_t op2)
2508 /* XXX: TODO: test special values (NaN, infinites, ...) */
2509 return helper_efdtsteq(op1, op2);
2512 /*****************************************************************************/
2513 /* Softmmu support */
2514 #if !defined (CONFIG_USER_ONLY)
2516 #define MMUSUFFIX _mmu
2519 #include "softmmu_template.h"
2522 #include "softmmu_template.h"
2525 #include "softmmu_template.h"
2528 #include "softmmu_template.h"
2530 /* try to fill the TLB and return an exception if error. If retaddr is
2531 NULL, it means that the function was called in C code (i.e. not
2532 from generated code or from helper.c) */
2533 /* XXX: fix it to restore all registers */
2534 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2536 TranslationBlock *tb;
2537 CPUState *saved_env;
2541 /* XXX: hack to restore env in all cases, even if not called from
2544 env = cpu_single_env;
2545 ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2546 if (unlikely(ret != 0)) {
2547 if (likely(retaddr)) {
2548 /* now we have a real cpu fault */
2549 pc = (unsigned long)retaddr;
2550 tb = tb_find_pc(pc);
2552 /* the PC is inside the translated code. It means that we have
2553 a virtual CPU fault */
2554 cpu_restore_state(tb, env, pc, NULL);
2557 raise_exception_err(env, env->exception_index, env->error_code);
2562 /* Software driven TLBs management */
2563 /* PowerPC 602/603 software TLB load instructions helpers */
2564 static void helper_load_6xx_tlb (target_ulong new_EPN, int is_code)
2566 target_ulong RPN, CMP, EPN;
2569 RPN = env->spr[SPR_RPA];
2571 CMP = env->spr[SPR_ICMP];
2572 EPN = env->spr[SPR_IMISS];
2574 CMP = env->spr[SPR_DCMP];
2575 EPN = env->spr[SPR_DMISS];
2577 way = (env->spr[SPR_SRR1] >> 17) & 1;
2578 #if defined (DEBUG_SOFTWARE_TLB)
2579 if (loglevel != 0) {
2580 fprintf(logfile, "%s: EPN " TDX " " ADDRX " PTE0 " ADDRX
2581 " PTE1 " ADDRX " way %d\n",
2582 __func__, T0, EPN, CMP, RPN, way);
2585 /* Store this TLB */
2586 ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
2587 way, is_code, CMP, RPN);
2590 void helper_load_6xx_tlbd (target_ulong EPN)
2592 helper_load_6xx_tlb(EPN, 0);
2595 void helper_load_6xx_tlbi (target_ulong EPN)
2597 helper_load_6xx_tlb(EPN, 1);
2600 /* PowerPC 74xx software TLB load instructions helpers */
2601 static void helper_load_74xx_tlb (target_ulong new_EPN, int is_code)
2603 target_ulong RPN, CMP, EPN;
2606 RPN = env->spr[SPR_PTELO];
2607 CMP = env->spr[SPR_PTEHI];
2608 EPN = env->spr[SPR_TLBMISS] & ~0x3;
2609 way = env->spr[SPR_TLBMISS] & 0x3;
2610 #if defined (DEBUG_SOFTWARE_TLB)
2611 if (loglevel != 0) {
2612 fprintf(logfile, "%s: EPN " TDX " " ADDRX " PTE0 " ADDRX
2613 " PTE1 " ADDRX " way %d\n",
2614 __func__, T0, EPN, CMP, RPN, way);
2617 /* Store this TLB */
2618 ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
2619 way, is_code, CMP, RPN);
2622 void helper_load_74xx_tlbd (target_ulong EPN)
2624 helper_load_74xx_tlb(EPN, 0);
2627 void helper_load_74xx_tlbi (target_ulong EPN)
2629 helper_load_74xx_tlb(EPN, 1);
2632 static always_inline target_ulong booke_tlb_to_page_size (int size)
2634 return 1024 << (2 * size);
2637 static always_inline int booke_page_size_to_tlb (target_ulong page_size)
2641 switch (page_size) {
2675 #if defined (TARGET_PPC64)
2676 case 0x000100000000ULL:
2679 case 0x000400000000ULL:
2682 case 0x001000000000ULL:
2685 case 0x004000000000ULL:
2688 case 0x010000000000ULL:
2700 /* Helpers for 4xx TLB management */
2701 void do_4xx_tlbre_lo (void)
2707 tlb = &env->tlb[T0].tlbe;
2709 if (tlb->prot & PAGE_VALID)
2711 size = booke_page_size_to_tlb(tlb->size);
2712 if (size < 0 || size > 0x7)
2715 env->spr[SPR_40x_PID] = tlb->PID;
2718 void do_4xx_tlbre_hi (void)
2723 tlb = &env->tlb[T0].tlbe;
2725 if (tlb->prot & PAGE_EXEC)
2727 if (tlb->prot & PAGE_WRITE)
2731 void do_4xx_tlbwe_hi (void)
2734 target_ulong page, end;
2736 #if defined (DEBUG_SOFTWARE_TLB)
2737 if (loglevel != 0) {
2738 fprintf(logfile, "%s T0 " TDX " T1 " TDX "\n", __func__, T0, T1);
2742 tlb = &env->tlb[T0].tlbe;
2743 /* Invalidate previous TLB (if it's valid) */
2744 if (tlb->prot & PAGE_VALID) {
2745 end = tlb->EPN + tlb->size;
2746 #if defined (DEBUG_SOFTWARE_TLB)
2747 if (loglevel != 0) {
2748 fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
2749 " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2752 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2753 tlb_flush_page(env, page);
2755 tlb->size = booke_tlb_to_page_size((T1 >> 7) & 0x7);
2756 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2757 * If this ever occurs, one should use the ppcemb target instead
2758 * of the ppc or ppc64 one
2760 if ((T1 & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
2761 cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
2762 "are not supported (%d)\n",
2763 tlb->size, TARGET_PAGE_SIZE, (int)((T1 >> 7) & 0x7));
2765 tlb->EPN = T1 & ~(tlb->size - 1);
2767 tlb->prot |= PAGE_VALID;
2769 tlb->prot &= ~PAGE_VALID;
2771 /* XXX: TO BE FIXED */
2772 cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
2774 tlb->PID = env->spr[SPR_40x_PID]; /* PID */
2775 tlb->attr = T1 & 0xFF;
2776 #if defined (DEBUG_SOFTWARE_TLB)
2777 if (loglevel != 0) {
2778 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2779 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2780 (int)T0, tlb->RPN, tlb->EPN, tlb->size,
2781 tlb->prot & PAGE_READ ? 'r' : '-',
2782 tlb->prot & PAGE_WRITE ? 'w' : '-',
2783 tlb->prot & PAGE_EXEC ? 'x' : '-',
2784 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2787 /* Invalidate new TLB (if valid) */
2788 if (tlb->prot & PAGE_VALID) {
2789 end = tlb->EPN + tlb->size;
2790 #if defined (DEBUG_SOFTWARE_TLB)
2791 if (loglevel != 0) {
2792 fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
2793 " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2796 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2797 tlb_flush_page(env, page);
2801 void do_4xx_tlbwe_lo (void)
2805 #if defined (DEBUG_SOFTWARE_TLB)
2806 if (loglevel != 0) {
2807 fprintf(logfile, "%s T0 " TDX " T1 " TDX "\n", __func__, T0, T1);
2811 tlb = &env->tlb[T0].tlbe;
2812 tlb->RPN = T1 & 0xFFFFFC00;
2813 tlb->prot = PAGE_READ;
2815 tlb->prot |= PAGE_EXEC;
2817 tlb->prot |= PAGE_WRITE;
2818 #if defined (DEBUG_SOFTWARE_TLB)
2819 if (loglevel != 0) {
2820 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2821 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2822 (int)T0, tlb->RPN, tlb->EPN, tlb->size,
2823 tlb->prot & PAGE_READ ? 'r' : '-',
2824 tlb->prot & PAGE_WRITE ? 'w' : '-',
2825 tlb->prot & PAGE_EXEC ? 'x' : '-',
2826 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2831 /* PowerPC 440 TLB management */
2832 void do_440_tlbwe (int word)
2835 target_ulong EPN, RPN, size;
2838 #if defined (DEBUG_SOFTWARE_TLB)
2839 if (loglevel != 0) {
2840 fprintf(logfile, "%s word %d T0 " TDX " T1 " TDX "\n",
2841 __func__, word, T0, T1);
2846 tlb = &env->tlb[T0].tlbe;
2849 /* Just here to please gcc */
2851 EPN = T1 & 0xFFFFFC00;
2852 if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
2855 size = booke_tlb_to_page_size((T1 >> 4) & 0xF);
2856 if ((tlb->prot & PAGE_VALID) && tlb->size < size)
2860 tlb->attr |= (T1 >> 8) & 1;
2862 tlb->prot |= PAGE_VALID;
2864 if (tlb->prot & PAGE_VALID) {
2865 tlb->prot &= ~PAGE_VALID;
2869 tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
2874 RPN = T1 & 0xFFFFFC0F;
2875 if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
2880 tlb->attr = (tlb->attr & 0x1) | (T1 & 0x0000FF00);
2881 tlb->prot = tlb->prot & PAGE_VALID;
2883 tlb->prot |= PAGE_READ << 4;
2885 tlb->prot |= PAGE_WRITE << 4;
2887 tlb->prot |= PAGE_EXEC << 4;
2889 tlb->prot |= PAGE_READ;
2891 tlb->prot |= PAGE_WRITE;
2893 tlb->prot |= PAGE_EXEC;
2898 void do_440_tlbre (int word)
2904 tlb = &env->tlb[T0].tlbe;
2907 /* Just here to please gcc */
2910 size = booke_page_size_to_tlb(tlb->size);
2911 if (size < 0 || size > 0xF)
2914 if (tlb->attr & 0x1)
2916 if (tlb->prot & PAGE_VALID)
2918 env->spr[SPR_440_MMUCR] &= ~0x000000FF;
2919 env->spr[SPR_440_MMUCR] |= tlb->PID;
2925 T0 = tlb->attr & ~0x1;
2926 if (tlb->prot & (PAGE_READ << 4))
2928 if (tlb->prot & (PAGE_WRITE << 4))
2930 if (tlb->prot & (PAGE_EXEC << 4))
2932 if (tlb->prot & PAGE_READ)
2934 if (tlb->prot & PAGE_WRITE)
2936 if (tlb->prot & PAGE_EXEC)
2941 #endif /* !CONFIG_USER_ONLY */