target-mips: optimize gen_muldiv()
[qemu/qemu-JZ.git] / target-ppc / op_helper.c
blob9582f4f83515ed3e6df314ee1dd4a43742ed3bee
1 /*
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
20 #include "exec.h"
21 #include "host-utils.h"
23 #include "helper_regs.h"
24 #include "op_helper.h"
26 #define MEMSUFFIX _raw
27 #include "op_helper.h"
28 #include "op_helper_mem.h"
29 #if !defined(CONFIG_USER_ONLY)
30 #define MEMSUFFIX _user
31 #include "op_helper.h"
32 #include "op_helper_mem.h"
33 #define MEMSUFFIX _kernel
34 #include "op_helper.h"
35 #include "op_helper_mem.h"
36 #define MEMSUFFIX _hypv
37 #include "op_helper.h"
38 #include "op_helper_mem.h"
39 #endif
41 //#define DEBUG_OP
42 //#define DEBUG_EXCEPTIONS
43 //#define DEBUG_SOFTWARE_TLB
45 /*****************************************************************************/
46 /* Exceptions processing helpers */
48 void do_raise_exception_err (uint32_t exception, int error_code)
50 #if 0
51 printf("Raise exception %3x code : %d\n", exception, error_code);
52 #endif
53 env->exception_index = exception;
54 env->error_code = error_code;
55 cpu_loop_exit();
58 void do_raise_exception (uint32_t exception)
60 do_raise_exception_err(exception, 0);
63 /*****************************************************************************/
64 /* Registers load and stores */
65 uint32_t helper_load_cr (void)
67 return (env->crf[0] << 28) |
68 (env->crf[1] << 24) |
69 (env->crf[2] << 20) |
70 (env->crf[3] << 16) |
71 (env->crf[4] << 12) |
72 (env->crf[5] << 8) |
73 (env->crf[6] << 4) |
74 (env->crf[7] << 0);
77 void helper_store_cr (target_ulong val, uint32_t mask)
79 int i, sh;
81 for (i = 0, sh = 7; i < 8; i++, sh--) {
82 if (mask & (1 << sh))
83 env->crf[i] = (val >> (sh * 4)) & 0xFUL;
87 #if defined(TARGET_PPC64)
88 void do_store_pri (int prio)
90 env->spr[SPR_PPR] &= ~0x001C000000000000ULL;
91 env->spr[SPR_PPR] |= ((uint64_t)prio & 0x7) << 50;
93 #endif
95 target_ulong ppc_load_dump_spr (int sprn)
97 if (loglevel != 0) {
98 fprintf(logfile, "Read SPR %d %03x => " ADDRX "\n",
99 sprn, sprn, env->spr[sprn]);
102 return env->spr[sprn];
105 void ppc_store_dump_spr (int sprn, target_ulong val)
107 if (loglevel != 0) {
108 fprintf(logfile, "Write SPR %d %03x => " ADDRX " <= " ADDRX "\n",
109 sprn, sprn, env->spr[sprn], val);
111 env->spr[sprn] = val;
114 /*****************************************************************************/
115 /* Fixed point operations helpers */
116 #if defined(TARGET_PPC64)
118 /* multiply high word */
119 uint64_t helper_mulhd (uint64_t arg1, uint64_t arg2)
121 uint64_t tl, th;
123 muls64(&tl, &th, arg1, arg2);
124 return th;
127 /* multiply high word unsigned */
128 uint64_t helper_mulhdu (uint64_t arg1, uint64_t arg2)
130 uint64_t tl, th;
132 mulu64(&tl, &th, arg1, arg2);
133 return th;
136 uint64_t helper_mulldo (uint64_t arg1, uint64_t arg2)
138 int64_t th;
139 uint64_t tl;
141 muls64(&tl, (uint64_t *)&th, arg1, arg2);
142 /* If th != 0 && th != -1, then we had an overflow */
143 if (likely((uint64_t)(th + 1) <= 1)) {
144 env->xer &= ~(1 << XER_OV);
145 } else {
146 env->xer |= (1 << XER_OV) | (1 << XER_SO);
148 return (int64_t)tl;
150 #endif
152 target_ulong helper_cntlzw (target_ulong t)
154 return clz32(t);
157 #if defined(TARGET_PPC64)
158 target_ulong helper_cntlzd (target_ulong t)
160 return clz64(t);
162 #endif
164 /* shift right arithmetic helper */
165 target_ulong helper_sraw (target_ulong value, target_ulong shift)
167 int32_t ret;
169 if (likely(!(shift & 0x20))) {
170 if (likely((uint32_t)shift != 0)) {
171 shift &= 0x1f;
172 ret = (int32_t)value >> shift;
173 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
174 env->xer &= ~(1 << XER_CA);
175 } else {
176 env->xer |= (1 << XER_CA);
178 } else {
179 ret = (int32_t)value;
180 env->xer &= ~(1 << XER_CA);
182 } else {
183 ret = (int32_t)value >> 31;
184 if (ret) {
185 env->xer |= (1 << XER_CA);
186 } else {
187 env->xer &= ~(1 << XER_CA);
190 return (target_long)ret;
193 #if defined(TARGET_PPC64)
194 target_ulong helper_srad (target_ulong value, target_ulong shift)
196 int64_t ret;
198 if (likely(!(shift & 0x40))) {
199 if (likely((uint64_t)shift != 0)) {
200 shift &= 0x3f;
201 ret = (int64_t)value >> shift;
202 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
203 env->xer &= ~(1 << XER_CA);
204 } else {
205 env->xer |= (1 << XER_CA);
207 } else {
208 ret = (int64_t)value;
209 env->xer &= ~(1 << XER_CA);
211 } else {
212 ret = (int64_t)value >> 63;
213 if (ret) {
214 env->xer |= (1 << XER_CA);
215 } else {
216 env->xer &= ~(1 << XER_CA);
219 return ret;
221 #endif
223 target_ulong helper_popcntb (target_ulong val)
225 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
226 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
227 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
228 return val;
231 #if defined(TARGET_PPC64)
232 target_ulong helper_popcntb_64 (target_ulong val)
234 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
235 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
236 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
237 return val;
239 #endif
241 /*****************************************************************************/
242 /* Floating point operations helpers */
243 static always_inline int fpisneg (float64 d)
245 CPU_DoubleU u;
247 u.d = d;
249 return u.ll >> 63 != 0;
252 static always_inline int isden (float64 d)
254 CPU_DoubleU u;
256 u.d = d;
258 return ((u.ll >> 52) & 0x7FF) == 0;
261 static always_inline int iszero (float64 d)
263 CPU_DoubleU u;
265 u.d = d;
267 return (u.ll & ~0x8000000000000000ULL) == 0;
270 static always_inline int isinfinity (float64 d)
272 CPU_DoubleU u;
274 u.d = d;
276 return ((u.ll >> 52) & 0x7FF) == 0x7FF &&
277 (u.ll & 0x000FFFFFFFFFFFFFULL) == 0;
280 #ifdef CONFIG_SOFTFLOAT
281 static always_inline int isfinite (float64 d)
283 CPU_DoubleU u;
285 u.d = d;
287 return (((u.ll >> 52) & 0x7FF) != 0x7FF);
290 static always_inline int isnormal (float64 d)
292 CPU_DoubleU u;
294 u.d = d;
296 uint32_t exp = (u.ll >> 52) & 0x7FF;
297 return ((0 < exp) && (exp < 0x7FF));
299 #endif
301 void do_compute_fprf (int set_fprf)
303 int isneg;
305 isneg = fpisneg(FT0);
306 if (unlikely(float64_is_nan(FT0))) {
307 if (float64_is_signaling_nan(FT0)) {
308 /* Signaling NaN: flags are undefined */
309 T0 = 0x00;
310 } else {
311 /* Quiet NaN */
312 T0 = 0x11;
314 } else if (unlikely(isinfinity(FT0))) {
315 /* +/- infinity */
316 if (isneg)
317 T0 = 0x09;
318 else
319 T0 = 0x05;
320 } else {
321 if (iszero(FT0)) {
322 /* +/- zero */
323 if (isneg)
324 T0 = 0x12;
325 else
326 T0 = 0x02;
327 } else {
328 if (isden(FT0)) {
329 /* Denormalized numbers */
330 T0 = 0x10;
331 } else {
332 /* Normalized numbers */
333 T0 = 0x00;
335 if (isneg) {
336 T0 |= 0x08;
337 } else {
338 T0 |= 0x04;
342 if (set_fprf) {
343 /* We update FPSCR_FPRF */
344 env->fpscr &= ~(0x1F << FPSCR_FPRF);
345 env->fpscr |= T0 << FPSCR_FPRF;
347 /* We just need fpcc to update Rc1 */
348 T0 &= 0xF;
351 /* Floating-point invalid operations exception */
352 static always_inline void fload_invalid_op_excp (int op)
354 int ve;
356 ve = fpscr_ve;
357 if (op & POWERPC_EXCP_FP_VXSNAN) {
358 /* Operation on signaling NaN */
359 env->fpscr |= 1 << FPSCR_VXSNAN;
361 if (op & POWERPC_EXCP_FP_VXSOFT) {
362 /* Software-defined condition */
363 env->fpscr |= 1 << FPSCR_VXSOFT;
365 switch (op & ~(POWERPC_EXCP_FP_VXSOFT | POWERPC_EXCP_FP_VXSNAN)) {
366 case POWERPC_EXCP_FP_VXISI:
367 /* Magnitude subtraction of infinities */
368 env->fpscr |= 1 << FPSCR_VXISI;
369 goto update_arith;
370 case POWERPC_EXCP_FP_VXIDI:
371 /* Division of infinity by infinity */
372 env->fpscr |= 1 << FPSCR_VXIDI;
373 goto update_arith;
374 case POWERPC_EXCP_FP_VXZDZ:
375 /* Division of zero by zero */
376 env->fpscr |= 1 << FPSCR_VXZDZ;
377 goto update_arith;
378 case POWERPC_EXCP_FP_VXIMZ:
379 /* Multiplication of zero by infinity */
380 env->fpscr |= 1 << FPSCR_VXIMZ;
381 goto update_arith;
382 case POWERPC_EXCP_FP_VXVC:
383 /* Ordered comparison of NaN */
384 env->fpscr |= 1 << FPSCR_VXVC;
385 env->fpscr &= ~(0xF << FPSCR_FPCC);
386 env->fpscr |= 0x11 << FPSCR_FPCC;
387 /* We must update the target FPR before raising the exception */
388 if (ve != 0) {
389 env->exception_index = POWERPC_EXCP_PROGRAM;
390 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
391 /* Update the floating-point enabled exception summary */
392 env->fpscr |= 1 << FPSCR_FEX;
393 /* Exception is differed */
394 ve = 0;
396 break;
397 case POWERPC_EXCP_FP_VXSQRT:
398 /* Square root of a negative number */
399 env->fpscr |= 1 << FPSCR_VXSQRT;
400 update_arith:
401 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
402 if (ve == 0) {
403 /* Set the result to quiet NaN */
404 FT0 = UINT64_MAX;
405 env->fpscr &= ~(0xF << FPSCR_FPCC);
406 env->fpscr |= 0x11 << FPSCR_FPCC;
408 break;
409 case POWERPC_EXCP_FP_VXCVI:
410 /* Invalid conversion */
411 env->fpscr |= 1 << FPSCR_VXCVI;
412 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
413 if (ve == 0) {
414 /* Set the result to quiet NaN */
415 FT0 = UINT64_MAX;
416 env->fpscr &= ~(0xF << FPSCR_FPCC);
417 env->fpscr |= 0x11 << FPSCR_FPCC;
419 break;
421 /* Update the floating-point invalid operation summary */
422 env->fpscr |= 1 << FPSCR_VX;
423 /* Update the floating-point exception summary */
424 env->fpscr |= 1 << FPSCR_FX;
425 if (ve != 0) {
426 /* Update the floating-point enabled exception summary */
427 env->fpscr |= 1 << FPSCR_FEX;
428 if (msr_fe0 != 0 || msr_fe1 != 0)
429 do_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_FP | op);
433 static always_inline void float_zero_divide_excp (void)
435 CPU_DoubleU u0, u1;
437 env->fpscr |= 1 << FPSCR_ZX;
438 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
439 /* Update the floating-point exception summary */
440 env->fpscr |= 1 << FPSCR_FX;
441 if (fpscr_ze != 0) {
442 /* Update the floating-point enabled exception summary */
443 env->fpscr |= 1 << FPSCR_FEX;
444 if (msr_fe0 != 0 || msr_fe1 != 0) {
445 do_raise_exception_err(POWERPC_EXCP_PROGRAM,
446 POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
448 } else {
449 /* Set the result to infinity */
450 u0.d = FT0;
451 u1.d = FT1;
452 u0.ll = ((u0.ll ^ u1.ll) & 0x8000000000000000ULL);
453 u0.ll |= 0x7FFULL << 52;
454 FT0 = u0.d;
458 static always_inline void float_overflow_excp (void)
460 env->fpscr |= 1 << FPSCR_OX;
461 /* Update the floating-point exception summary */
462 env->fpscr |= 1 << FPSCR_FX;
463 if (fpscr_oe != 0) {
464 /* XXX: should adjust the result */
465 /* Update the floating-point enabled exception summary */
466 env->fpscr |= 1 << FPSCR_FEX;
467 /* We must update the target FPR before raising the exception */
468 env->exception_index = POWERPC_EXCP_PROGRAM;
469 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
470 } else {
471 env->fpscr |= 1 << FPSCR_XX;
472 env->fpscr |= 1 << FPSCR_FI;
476 static always_inline void float_underflow_excp (void)
478 env->fpscr |= 1 << FPSCR_UX;
479 /* Update the floating-point exception summary */
480 env->fpscr |= 1 << FPSCR_FX;
481 if (fpscr_ue != 0) {
482 /* XXX: should adjust the result */
483 /* Update the floating-point enabled exception summary */
484 env->fpscr |= 1 << FPSCR_FEX;
485 /* We must update the target FPR before raising the exception */
486 env->exception_index = POWERPC_EXCP_PROGRAM;
487 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
491 static always_inline void float_inexact_excp (void)
493 env->fpscr |= 1 << FPSCR_XX;
494 /* Update the floating-point exception summary */
495 env->fpscr |= 1 << FPSCR_FX;
496 if (fpscr_xe != 0) {
497 /* Update the floating-point enabled exception summary */
498 env->fpscr |= 1 << FPSCR_FEX;
499 /* We must update the target FPR before raising the exception */
500 env->exception_index = POWERPC_EXCP_PROGRAM;
501 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
505 static always_inline void fpscr_set_rounding_mode (void)
507 int rnd_type;
509 /* Set rounding mode */
510 switch (fpscr_rn) {
511 case 0:
512 /* Best approximation (round to nearest) */
513 rnd_type = float_round_nearest_even;
514 break;
515 case 1:
516 /* Smaller magnitude (round toward zero) */
517 rnd_type = float_round_to_zero;
518 break;
519 case 2:
520 /* Round toward +infinite */
521 rnd_type = float_round_up;
522 break;
523 default:
524 case 3:
525 /* Round toward -infinite */
526 rnd_type = float_round_down;
527 break;
529 set_float_rounding_mode(rnd_type, &env->fp_status);
532 void do_fpscr_setbit (int bit)
534 int prev;
536 prev = (env->fpscr >> bit) & 1;
537 env->fpscr |= 1 << bit;
538 if (prev == 0) {
539 switch (bit) {
540 case FPSCR_VX:
541 env->fpscr |= 1 << FPSCR_FX;
542 if (fpscr_ve)
543 goto raise_ve;
544 case FPSCR_OX:
545 env->fpscr |= 1 << FPSCR_FX;
546 if (fpscr_oe)
547 goto raise_oe;
548 break;
549 case FPSCR_UX:
550 env->fpscr |= 1 << FPSCR_FX;
551 if (fpscr_ue)
552 goto raise_ue;
553 break;
554 case FPSCR_ZX:
555 env->fpscr |= 1 << FPSCR_FX;
556 if (fpscr_ze)
557 goto raise_ze;
558 break;
559 case FPSCR_XX:
560 env->fpscr |= 1 << FPSCR_FX;
561 if (fpscr_xe)
562 goto raise_xe;
563 break;
564 case FPSCR_VXSNAN:
565 case FPSCR_VXISI:
566 case FPSCR_VXIDI:
567 case FPSCR_VXZDZ:
568 case FPSCR_VXIMZ:
569 case FPSCR_VXVC:
570 case FPSCR_VXSOFT:
571 case FPSCR_VXSQRT:
572 case FPSCR_VXCVI:
573 env->fpscr |= 1 << FPSCR_VX;
574 env->fpscr |= 1 << FPSCR_FX;
575 if (fpscr_ve != 0)
576 goto raise_ve;
577 break;
578 case FPSCR_VE:
579 if (fpscr_vx != 0) {
580 raise_ve:
581 env->error_code = POWERPC_EXCP_FP;
582 if (fpscr_vxsnan)
583 env->error_code |= POWERPC_EXCP_FP_VXSNAN;
584 if (fpscr_vxisi)
585 env->error_code |= POWERPC_EXCP_FP_VXISI;
586 if (fpscr_vxidi)
587 env->error_code |= POWERPC_EXCP_FP_VXIDI;
588 if (fpscr_vxzdz)
589 env->error_code |= POWERPC_EXCP_FP_VXZDZ;
590 if (fpscr_vximz)
591 env->error_code |= POWERPC_EXCP_FP_VXIMZ;
592 if (fpscr_vxvc)
593 env->error_code |= POWERPC_EXCP_FP_VXVC;
594 if (fpscr_vxsoft)
595 env->error_code |= POWERPC_EXCP_FP_VXSOFT;
596 if (fpscr_vxsqrt)
597 env->error_code |= POWERPC_EXCP_FP_VXSQRT;
598 if (fpscr_vxcvi)
599 env->error_code |= POWERPC_EXCP_FP_VXCVI;
600 goto raise_excp;
602 break;
603 case FPSCR_OE:
604 if (fpscr_ox != 0) {
605 raise_oe:
606 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
607 goto raise_excp;
609 break;
610 case FPSCR_UE:
611 if (fpscr_ux != 0) {
612 raise_ue:
613 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
614 goto raise_excp;
616 break;
617 case FPSCR_ZE:
618 if (fpscr_zx != 0) {
619 raise_ze:
620 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
621 goto raise_excp;
623 break;
624 case FPSCR_XE:
625 if (fpscr_xx != 0) {
626 raise_xe:
627 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
628 goto raise_excp;
630 break;
631 case FPSCR_RN1:
632 case FPSCR_RN:
633 fpscr_set_rounding_mode();
634 break;
635 default:
636 break;
637 raise_excp:
638 /* Update the floating-point enabled exception summary */
639 env->fpscr |= 1 << FPSCR_FEX;
640 /* We have to update Rc1 before raising the exception */
641 env->exception_index = POWERPC_EXCP_PROGRAM;
642 break;
647 #if defined(WORDS_BIGENDIAN)
648 #define WORD0 0
649 #define WORD1 1
650 #else
651 #define WORD0 1
652 #define WORD1 0
653 #endif
654 void do_store_fpscr (uint32_t mask)
657 * We use only the 32 LSB of the incoming fpr
659 CPU_DoubleU u;
660 uint32_t prev, new;
661 int i;
663 u.d = FT0;
664 prev = env->fpscr;
665 new = u.l.lower;
666 new &= ~0x90000000;
667 new |= prev & 0x90000000;
668 for (i = 0; i < 7; i++) {
669 if (mask & (1 << i)) {
670 env->fpscr &= ~(0xF << (4 * i));
671 env->fpscr |= new & (0xF << (4 * i));
674 /* Update VX and FEX */
675 if (fpscr_ix != 0)
676 env->fpscr |= 1 << FPSCR_VX;
677 else
678 env->fpscr &= ~(1 << FPSCR_VX);
679 if ((fpscr_ex & fpscr_eex) != 0) {
680 env->fpscr |= 1 << FPSCR_FEX;
681 env->exception_index = POWERPC_EXCP_PROGRAM;
682 /* XXX: we should compute it properly */
683 env->error_code = POWERPC_EXCP_FP;
685 else
686 env->fpscr &= ~(1 << FPSCR_FEX);
687 fpscr_set_rounding_mode();
689 #undef WORD0
690 #undef WORD1
692 #ifdef CONFIG_SOFTFLOAT
693 void do_float_check_status (void)
695 if (env->exception_index == POWERPC_EXCP_PROGRAM &&
696 (env->error_code & POWERPC_EXCP_FP)) {
697 /* Differred floating-point exception after target FPR update */
698 if (msr_fe0 != 0 || msr_fe1 != 0)
699 do_raise_exception_err(env->exception_index, env->error_code);
700 } else if (env->fp_status.float_exception_flags & float_flag_overflow) {
701 float_overflow_excp();
702 } else if (env->fp_status.float_exception_flags & float_flag_underflow) {
703 float_underflow_excp();
704 } else if (env->fp_status.float_exception_flags & float_flag_inexact) {
705 float_inexact_excp();
708 #endif
710 #if USE_PRECISE_EMULATION
711 void do_fadd (void)
713 if (unlikely(float64_is_signaling_nan(FT0) ||
714 float64_is_signaling_nan(FT1))) {
715 /* sNaN addition */
716 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
717 } else if (likely(isfinite(FT0) || isfinite(FT1) ||
718 fpisneg(FT0) == fpisneg(FT1))) {
719 FT0 = float64_add(FT0, FT1, &env->fp_status);
720 } else {
721 /* Magnitude subtraction of infinities */
722 fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
726 void do_fsub (void)
728 if (unlikely(float64_is_signaling_nan(FT0) ||
729 float64_is_signaling_nan(FT1))) {
730 /* sNaN subtraction */
731 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
732 } else if (likely(isfinite(FT0) || isfinite(FT1) ||
733 fpisneg(FT0) != fpisneg(FT1))) {
734 FT0 = float64_sub(FT0, FT1, &env->fp_status);
735 } else {
736 /* Magnitude subtraction of infinities */
737 fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
741 void do_fmul (void)
743 if (unlikely(float64_is_signaling_nan(FT0) ||
744 float64_is_signaling_nan(FT1))) {
745 /* sNaN multiplication */
746 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
747 } else if (unlikely((isinfinity(FT0) && iszero(FT1)) ||
748 (iszero(FT0) && isinfinity(FT1)))) {
749 /* Multiplication of zero by infinity */
750 fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
751 } else {
752 FT0 = float64_mul(FT0, FT1, &env->fp_status);
756 void do_fdiv (void)
758 if (unlikely(float64_is_signaling_nan(FT0) ||
759 float64_is_signaling_nan(FT1))) {
760 /* sNaN division */
761 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
762 } else if (unlikely(isinfinity(FT0) && isinfinity(FT1))) {
763 /* Division of infinity by infinity */
764 fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
765 } else if (unlikely(iszero(FT1))) {
766 if (iszero(FT0)) {
767 /* Division of zero by zero */
768 fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
769 } else {
770 /* Division by zero */
771 float_zero_divide_excp();
773 } else {
774 FT0 = float64_div(FT0, FT1, &env->fp_status);
777 #endif /* USE_PRECISE_EMULATION */
779 void do_fctiw (void)
781 CPU_DoubleU p;
783 if (unlikely(float64_is_signaling_nan(FT0))) {
784 /* sNaN conversion */
785 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
786 } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
787 /* qNan / infinity conversion */
788 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
789 } else {
790 p.ll = float64_to_int32(FT0, &env->fp_status);
791 #if USE_PRECISE_EMULATION
792 /* XXX: higher bits are not supposed to be significant.
793 * to make tests easier, return the same as a real PowerPC 750
795 p.ll |= 0xFFF80000ULL << 32;
796 #endif
797 FT0 = p.d;
801 void do_fctiwz (void)
803 CPU_DoubleU p;
805 if (unlikely(float64_is_signaling_nan(FT0))) {
806 /* sNaN conversion */
807 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
808 } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
809 /* qNan / infinity conversion */
810 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
811 } else {
812 p.ll = float64_to_int32_round_to_zero(FT0, &env->fp_status);
813 #if USE_PRECISE_EMULATION
814 /* XXX: higher bits are not supposed to be significant.
815 * to make tests easier, return the same as a real PowerPC 750
817 p.ll |= 0xFFF80000ULL << 32;
818 #endif
819 FT0 = p.d;
823 #if defined(TARGET_PPC64)
824 void do_fcfid (void)
826 CPU_DoubleU p;
828 p.d = FT0;
829 FT0 = int64_to_float64(p.ll, &env->fp_status);
832 void do_fctid (void)
834 CPU_DoubleU p;
836 if (unlikely(float64_is_signaling_nan(FT0))) {
837 /* sNaN conversion */
838 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
839 } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
840 /* qNan / infinity conversion */
841 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
842 } else {
843 p.ll = float64_to_int64(FT0, &env->fp_status);
844 FT0 = p.d;
848 void do_fctidz (void)
850 CPU_DoubleU p;
852 if (unlikely(float64_is_signaling_nan(FT0))) {
853 /* sNaN conversion */
854 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
855 } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
856 /* qNan / infinity conversion */
857 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
858 } else {
859 p.ll = float64_to_int64_round_to_zero(FT0, &env->fp_status);
860 FT0 = p.d;
864 #endif
866 static always_inline void do_fri (int rounding_mode)
868 if (unlikely(float64_is_signaling_nan(FT0))) {
869 /* sNaN round */
870 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
871 } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
872 /* qNan / infinity round */
873 fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
874 } else {
875 set_float_rounding_mode(rounding_mode, &env->fp_status);
876 FT0 = float64_round_to_int(FT0, &env->fp_status);
877 /* Restore rounding mode from FPSCR */
878 fpscr_set_rounding_mode();
882 void do_frin (void)
884 do_fri(float_round_nearest_even);
887 void do_friz (void)
889 do_fri(float_round_to_zero);
892 void do_frip (void)
894 do_fri(float_round_up);
897 void do_frim (void)
899 do_fri(float_round_down);
902 #if USE_PRECISE_EMULATION
903 void do_fmadd (void)
905 if (unlikely(float64_is_signaling_nan(FT0) ||
906 float64_is_signaling_nan(FT1) ||
907 float64_is_signaling_nan(FT2))) {
908 /* sNaN operation */
909 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
910 } else {
911 #ifdef FLOAT128
912 /* This is the way the PowerPC specification defines it */
913 float128 ft0_128, ft1_128;
915 ft0_128 = float64_to_float128(FT0, &env->fp_status);
916 ft1_128 = float64_to_float128(FT1, &env->fp_status);
917 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
918 ft1_128 = float64_to_float128(FT2, &env->fp_status);
919 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
920 FT0 = float128_to_float64(ft0_128, &env->fp_status);
921 #else
922 /* This is OK on x86 hosts */
923 FT0 = (FT0 * FT1) + FT2;
924 #endif
928 void do_fmsub (void)
930 if (unlikely(float64_is_signaling_nan(FT0) ||
931 float64_is_signaling_nan(FT1) ||
932 float64_is_signaling_nan(FT2))) {
933 /* sNaN operation */
934 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
935 } else {
936 #ifdef FLOAT128
937 /* This is the way the PowerPC specification defines it */
938 float128 ft0_128, ft1_128;
940 ft0_128 = float64_to_float128(FT0, &env->fp_status);
941 ft1_128 = float64_to_float128(FT1, &env->fp_status);
942 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
943 ft1_128 = float64_to_float128(FT2, &env->fp_status);
944 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
945 FT0 = float128_to_float64(ft0_128, &env->fp_status);
946 #else
947 /* This is OK on x86 hosts */
948 FT0 = (FT0 * FT1) - FT2;
949 #endif
952 #endif /* USE_PRECISE_EMULATION */
954 void do_fnmadd (void)
956 if (unlikely(float64_is_signaling_nan(FT0) ||
957 float64_is_signaling_nan(FT1) ||
958 float64_is_signaling_nan(FT2))) {
959 /* sNaN operation */
960 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
961 } else {
962 #if USE_PRECISE_EMULATION
963 #ifdef FLOAT128
964 /* This is the way the PowerPC specification defines it */
965 float128 ft0_128, ft1_128;
967 ft0_128 = float64_to_float128(FT0, &env->fp_status);
968 ft1_128 = float64_to_float128(FT1, &env->fp_status);
969 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
970 ft1_128 = float64_to_float128(FT2, &env->fp_status);
971 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
972 FT0 = float128_to_float64(ft0_128, &env->fp_status);
973 #else
974 /* This is OK on x86 hosts */
975 FT0 = (FT0 * FT1) + FT2;
976 #endif
977 #else
978 FT0 = float64_mul(FT0, FT1, &env->fp_status);
979 FT0 = float64_add(FT0, FT2, &env->fp_status);
980 #endif
981 if (likely(!isnan(FT0)))
982 FT0 = float64_chs(FT0);
986 void do_fnmsub (void)
988 if (unlikely(float64_is_signaling_nan(FT0) ||
989 float64_is_signaling_nan(FT1) ||
990 float64_is_signaling_nan(FT2))) {
991 /* sNaN operation */
992 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
993 } else {
994 #if USE_PRECISE_EMULATION
995 #ifdef FLOAT128
996 /* This is the way the PowerPC specification defines it */
997 float128 ft0_128, ft1_128;
999 ft0_128 = float64_to_float128(FT0, &env->fp_status);
1000 ft1_128 = float64_to_float128(FT1, &env->fp_status);
1001 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1002 ft1_128 = float64_to_float128(FT2, &env->fp_status);
1003 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1004 FT0 = float128_to_float64(ft0_128, &env->fp_status);
1005 #else
1006 /* This is OK on x86 hosts */
1007 FT0 = (FT0 * FT1) - FT2;
1008 #endif
1009 #else
1010 FT0 = float64_mul(FT0, FT1, &env->fp_status);
1011 FT0 = float64_sub(FT0, FT2, &env->fp_status);
1012 #endif
1013 if (likely(!isnan(FT0)))
1014 FT0 = float64_chs(FT0);
1018 #if USE_PRECISE_EMULATION
1019 void do_frsp (void)
1021 if (unlikely(float64_is_signaling_nan(FT0))) {
1022 /* sNaN square root */
1023 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1024 } else {
1025 FT0 = float64_to_float32(FT0, &env->fp_status);
1028 #endif /* USE_PRECISE_EMULATION */
1030 void do_fsqrt (void)
1032 if (unlikely(float64_is_signaling_nan(FT0))) {
1033 /* sNaN square root */
1034 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1035 } else if (unlikely(fpisneg(FT0) && !iszero(FT0))) {
1036 /* Square root of a negative nonzero number */
1037 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1038 } else {
1039 FT0 = float64_sqrt(FT0, &env->fp_status);
1043 void do_fre (void)
1045 CPU_DoubleU p;
1047 if (unlikely(float64_is_signaling_nan(FT0))) {
1048 /* sNaN reciprocal */
1049 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1050 } else if (unlikely(iszero(FT0))) {
1051 /* Zero reciprocal */
1052 float_zero_divide_excp();
1053 } else if (likely(isnormal(FT0))) {
1054 FT0 = float64_div(1.0, FT0, &env->fp_status);
1055 } else {
1056 p.d = FT0;
1057 if (p.ll == 0x8000000000000000ULL) {
1058 p.ll = 0xFFF0000000000000ULL;
1059 } else if (p.ll == 0x0000000000000000ULL) {
1060 p.ll = 0x7FF0000000000000ULL;
1061 } else if (isnan(FT0)) {
1062 p.ll = 0x7FF8000000000000ULL;
1063 } else if (fpisneg(FT0)) {
1064 p.ll = 0x8000000000000000ULL;
1065 } else {
1066 p.ll = 0x0000000000000000ULL;
1068 FT0 = p.d;
1072 void do_fres (void)
1074 CPU_DoubleU p;
1076 if (unlikely(float64_is_signaling_nan(FT0))) {
1077 /* sNaN reciprocal */
1078 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1079 } else if (unlikely(iszero(FT0))) {
1080 /* Zero reciprocal */
1081 float_zero_divide_excp();
1082 } else if (likely(isnormal(FT0))) {
1083 #if USE_PRECISE_EMULATION
1084 FT0 = float64_div(1.0, FT0, &env->fp_status);
1085 FT0 = float64_to_float32(FT0, &env->fp_status);
1086 #else
1087 FT0 = float32_div(1.0, FT0, &env->fp_status);
1088 #endif
1089 } else {
1090 p.d = FT0;
1091 if (p.ll == 0x8000000000000000ULL) {
1092 p.ll = 0xFFF0000000000000ULL;
1093 } else if (p.ll == 0x0000000000000000ULL) {
1094 p.ll = 0x7FF0000000000000ULL;
1095 } else if (isnan(FT0)) {
1096 p.ll = 0x7FF8000000000000ULL;
1097 } else if (fpisneg(FT0)) {
1098 p.ll = 0x8000000000000000ULL;
1099 } else {
1100 p.ll = 0x0000000000000000ULL;
1102 FT0 = p.d;
1106 void do_frsqrte (void)
1108 CPU_DoubleU p;
1110 if (unlikely(float64_is_signaling_nan(FT0))) {
1111 /* sNaN reciprocal square root */
1112 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1113 } else if (unlikely(fpisneg(FT0) && !iszero(FT0))) {
1114 /* Reciprocal square root of a negative nonzero number */
1115 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1116 } else if (likely(isnormal(FT0))) {
1117 FT0 = float64_sqrt(FT0, &env->fp_status);
1118 FT0 = float32_div(1.0, FT0, &env->fp_status);
1119 } else {
1120 p.d = FT0;
1121 if (p.ll == 0x8000000000000000ULL) {
1122 p.ll = 0xFFF0000000000000ULL;
1123 } else if (p.ll == 0x0000000000000000ULL) {
1124 p.ll = 0x7FF0000000000000ULL;
1125 } else if (isnan(FT0)) {
1126 p.ll |= 0x000FFFFFFFFFFFFFULL;
1127 } else if (fpisneg(FT0)) {
1128 p.ll = 0x7FF8000000000000ULL;
1129 } else {
1130 p.ll = 0x0000000000000000ULL;
1132 FT0 = p.d;
1136 void do_fsel (void)
1138 if (!fpisneg(FT0) || iszero(FT0))
1139 FT0 = FT1;
1140 else
1141 FT0 = FT2;
1144 uint32_t helper_fcmpu (void)
1146 uint32_t ret = 0;
1148 if (unlikely(float64_is_signaling_nan(FT0) ||
1149 float64_is_signaling_nan(FT1))) {
1150 /* sNaN comparison */
1151 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1152 } else {
1153 if (float64_lt(FT0, FT1, &env->fp_status)) {
1154 ret = 0x08UL;
1155 } else if (!float64_le(FT0, FT1, &env->fp_status)) {
1156 ret = 0x04UL;
1157 } else {
1158 ret = 0x02UL;
1161 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1162 env->fpscr |= ret << FPSCR_FPRF;
1163 return ret;
1166 uint32_t helper_fcmpo (void)
1168 uint32_t ret = 0;
1170 if (unlikely(float64_is_nan(FT0) ||
1171 float64_is_nan(FT1))) {
1172 if (float64_is_signaling_nan(FT0) ||
1173 float64_is_signaling_nan(FT1)) {
1174 /* sNaN comparison */
1175 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN |
1176 POWERPC_EXCP_FP_VXVC);
1177 } else {
1178 /* qNaN comparison */
1179 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
1181 } else {
1182 if (float64_lt(FT0, FT1, &env->fp_status)) {
1183 ret = 0x08UL;
1184 } else if (!float64_le(FT0, FT1, &env->fp_status)) {
1185 ret = 0x04UL;
1186 } else {
1187 ret = 0x02UL;
1190 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1191 env->fpscr |= ret << FPSCR_FPRF;
1192 return ret;
1195 #if !defined (CONFIG_USER_ONLY)
1196 void cpu_dump_rfi (target_ulong RA, target_ulong msr);
1198 void do_store_msr (void)
1200 T0 = hreg_store_msr(env, T0, 0);
1201 if (T0 != 0) {
1202 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1203 do_raise_exception(T0);
1207 static always_inline void __do_rfi (target_ulong nip, target_ulong msr,
1208 target_ulong msrm, int keep_msrh)
1210 #if defined(TARGET_PPC64)
1211 if (msr & (1ULL << MSR_SF)) {
1212 nip = (uint64_t)nip;
1213 msr &= (uint64_t)msrm;
1214 } else {
1215 nip = (uint32_t)nip;
1216 msr = (uint32_t)(msr & msrm);
1217 if (keep_msrh)
1218 msr |= env->msr & ~((uint64_t)0xFFFFFFFF);
1220 #else
1221 nip = (uint32_t)nip;
1222 msr &= (uint32_t)msrm;
1223 #endif
1224 /* XXX: beware: this is false if VLE is supported */
1225 env->nip = nip & ~((target_ulong)0x00000003);
1226 hreg_store_msr(env, msr, 1);
1227 #if defined (DEBUG_OP)
1228 cpu_dump_rfi(env->nip, env->msr);
1229 #endif
1230 /* No need to raise an exception here,
1231 * as rfi is always the last insn of a TB
1233 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1236 void do_rfi (void)
1238 __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1239 ~((target_ulong)0xFFFF0000), 1);
1242 #if defined(TARGET_PPC64)
1243 void do_rfid (void)
1245 __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1246 ~((target_ulong)0xFFFF0000), 0);
1249 void do_hrfid (void)
1251 __do_rfi(env->spr[SPR_HSRR0], env->spr[SPR_HSRR1],
1252 ~((target_ulong)0xFFFF0000), 0);
1254 #endif
1255 #endif
1257 void do_tw (int flags)
1259 if (!likely(!(((int32_t)T0 < (int32_t)T1 && (flags & 0x10)) ||
1260 ((int32_t)T0 > (int32_t)T1 && (flags & 0x08)) ||
1261 ((int32_t)T0 == (int32_t)T1 && (flags & 0x04)) ||
1262 ((uint32_t)T0 < (uint32_t)T1 && (flags & 0x02)) ||
1263 ((uint32_t)T0 > (uint32_t)T1 && (flags & 0x01))))) {
1264 do_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1268 #if defined(TARGET_PPC64)
1269 void do_td (int flags)
1271 if (!likely(!(((int64_t)T0 < (int64_t)T1 && (flags & 0x10)) ||
1272 ((int64_t)T0 > (int64_t)T1 && (flags & 0x08)) ||
1273 ((int64_t)T0 == (int64_t)T1 && (flags & 0x04)) ||
1274 ((uint64_t)T0 < (uint64_t)T1 && (flags & 0x02)) ||
1275 ((uint64_t)T0 > (uint64_t)T1 && (flags & 0x01)))))
1276 do_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1278 #endif
1280 /*****************************************************************************/
1281 /* PowerPC 601 specific instructions (POWER bridge) */
1282 void do_POWER_abso (void)
1284 if ((int32_t)T0 == INT32_MIN) {
1285 T0 = INT32_MAX;
1286 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1287 } else if ((int32_t)T0 < 0) {
1288 T0 = -T0;
1289 env->xer &= ~(1 << XER_OV);
1290 } else {
1291 env->xer &= ~(1 << XER_OV);
1295 void do_POWER_clcs (void)
1297 switch (T0) {
1298 case 0x0CUL:
1299 /* Instruction cache line size */
1300 T0 = env->icache_line_size;
1301 break;
1302 case 0x0DUL:
1303 /* Data cache line size */
1304 T0 = env->dcache_line_size;
1305 break;
1306 case 0x0EUL:
1307 /* Minimum cache line size */
1308 T0 = env->icache_line_size < env->dcache_line_size ?
1309 env->icache_line_size : env->dcache_line_size;
1310 break;
1311 case 0x0FUL:
1312 /* Maximum cache line size */
1313 T0 = env->icache_line_size > env->dcache_line_size ?
1314 env->icache_line_size : env->dcache_line_size;
1315 break;
1316 default:
1317 /* Undefined */
1318 break;
1322 void do_POWER_div (void)
1324 uint64_t tmp;
1326 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1327 (int32_t)T1 == 0) {
1328 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1329 env->spr[SPR_MQ] = 0;
1330 } else {
1331 tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1332 env->spr[SPR_MQ] = tmp % T1;
1333 T0 = tmp / (int32_t)T1;
1337 void do_POWER_divo (void)
1339 int64_t tmp;
1341 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1342 (int32_t)T1 == 0) {
1343 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1344 env->spr[SPR_MQ] = 0;
1345 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1346 } else {
1347 tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1348 env->spr[SPR_MQ] = tmp % T1;
1349 tmp /= (int32_t)T1;
1350 if (tmp > (int64_t)INT32_MAX || tmp < (int64_t)INT32_MIN) {
1351 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1352 } else {
1353 env->xer &= ~(1 << XER_OV);
1355 T0 = tmp;
1359 void do_POWER_divs (void)
1361 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1362 (int32_t)T1 == 0) {
1363 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1364 env->spr[SPR_MQ] = 0;
1365 } else {
1366 env->spr[SPR_MQ] = T0 % T1;
1367 T0 = (int32_t)T0 / (int32_t)T1;
1371 void do_POWER_divso (void)
1373 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1374 (int32_t)T1 == 0) {
1375 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
1376 env->spr[SPR_MQ] = 0;
1377 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1378 } else {
1379 T0 = (int32_t)T0 / (int32_t)T1;
1380 env->spr[SPR_MQ] = (int32_t)T0 % (int32_t)T1;
1381 env->xer &= ~(1 << XER_OV);
1385 void do_POWER_dozo (void)
1387 if ((int32_t)T1 > (int32_t)T0) {
1388 T2 = T0;
1389 T0 = T1 - T0;
1390 if (((uint32_t)(~T2) ^ (uint32_t)T1 ^ UINT32_MAX) &
1391 ((uint32_t)(~T2) ^ (uint32_t)T0) & (1UL << 31)) {
1392 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1393 } else {
1394 env->xer &= ~(1 << XER_OV);
1396 } else {
1397 T0 = 0;
1398 env->xer &= ~(1 << XER_OV);
1402 void do_POWER_maskg (void)
1404 uint32_t ret;
1406 if ((uint32_t)T0 == (uint32_t)(T1 + 1)) {
1407 ret = UINT32_MAX;
1408 } else {
1409 ret = (UINT32_MAX >> ((uint32_t)T0)) ^
1410 ((UINT32_MAX >> ((uint32_t)T1)) >> 1);
1411 if ((uint32_t)T0 > (uint32_t)T1)
1412 ret = ~ret;
1414 T0 = ret;
1417 void do_POWER_mulo (void)
1419 uint64_t tmp;
1421 tmp = (uint64_t)T0 * (uint64_t)T1;
1422 env->spr[SPR_MQ] = tmp >> 32;
1423 T0 = tmp;
1424 if (tmp >> 32 != ((uint64_t)T0 >> 16) * ((uint64_t)T1 >> 16)) {
1425 env->xer |= (1 << XER_OV) | (1 << XER_SO);
1426 } else {
1427 env->xer &= ~(1 << XER_OV);
1431 #if !defined (CONFIG_USER_ONLY)
1432 void do_POWER_rac (void)
1434 mmu_ctx_t ctx;
1435 int nb_BATs;
1437 /* We don't have to generate many instances of this instruction,
1438 * as rac is supervisor only.
1440 /* XXX: FIX THIS: Pretend we have no BAT */
1441 nb_BATs = env->nb_BATs;
1442 env->nb_BATs = 0;
1443 if (get_physical_address(env, &ctx, T0, 0, ACCESS_INT) == 0)
1444 T0 = ctx.raddr;
1445 env->nb_BATs = nb_BATs;
1448 void do_POWER_rfsvc (void)
1450 __do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
1453 void do_store_hid0_601 (void)
1455 uint32_t hid0;
1457 hid0 = env->spr[SPR_HID0];
1458 if ((T0 ^ hid0) & 0x00000008) {
1459 /* Change current endianness */
1460 env->hflags &= ~(1 << MSR_LE);
1461 env->hflags_nmsr &= ~(1 << MSR_LE);
1462 env->hflags_nmsr |= (1 << MSR_LE) & (((T0 >> 3) & 1) << MSR_LE);
1463 env->hflags |= env->hflags_nmsr;
1464 if (loglevel != 0) {
1465 fprintf(logfile, "%s: set endianness to %c => " ADDRX "\n",
1466 __func__, T0 & 0x8 ? 'l' : 'b', env->hflags);
1469 env->spr[SPR_HID0] = T0;
1471 #endif
1473 /*****************************************************************************/
1474 /* 602 specific instructions */
1475 /* mfrom is the most crazy instruction ever seen, imho ! */
1476 /* Real implementation uses a ROM table. Do the same */
1477 #define USE_MFROM_ROM_TABLE
1478 void do_op_602_mfrom (void)
1480 if (likely(T0 < 602)) {
1481 #if defined(USE_MFROM_ROM_TABLE)
1482 #include "mfrom_table.c"
1483 T0 = mfrom_ROM_table[T0];
1484 #else
1485 double d;
1486 /* Extremly decomposed:
1487 * -T0 / 256
1488 * T0 = 256 * log10(10 + 1.0) + 0.5
1490 d = T0;
1491 d = float64_div(d, 256, &env->fp_status);
1492 d = float64_chs(d);
1493 d = exp10(d); // XXX: use float emulation function
1494 d = float64_add(d, 1.0, &env->fp_status);
1495 d = log10(d); // XXX: use float emulation function
1496 d = float64_mul(d, 256, &env->fp_status);
1497 d = float64_add(d, 0.5, &env->fp_status);
1498 T0 = float64_round_to_int(d, &env->fp_status);
1499 #endif
1500 } else {
1501 T0 = 0;
1505 /*****************************************************************************/
1506 /* Embedded PowerPC specific helpers */
1508 /* XXX: to be improved to check access rights when in user-mode */
1509 void do_load_dcr (void)
1511 target_ulong val;
1513 if (unlikely(env->dcr_env == NULL)) {
1514 if (loglevel != 0) {
1515 fprintf(logfile, "No DCR environment\n");
1517 do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1518 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1519 } else if (unlikely(ppc_dcr_read(env->dcr_env, T0, &val) != 0)) {
1520 if (loglevel != 0) {
1521 fprintf(logfile, "DCR read error %d %03x\n", (int)T0, (int)T0);
1523 do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1524 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1525 } else {
1526 T0 = val;
1530 void do_store_dcr (void)
1532 if (unlikely(env->dcr_env == NULL)) {
1533 if (loglevel != 0) {
1534 fprintf(logfile, "No DCR environment\n");
1536 do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1537 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1538 } else if (unlikely(ppc_dcr_write(env->dcr_env, T0, T1) != 0)) {
1539 if (loglevel != 0) {
1540 fprintf(logfile, "DCR write error %d %03x\n", (int)T0, (int)T0);
1542 do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1543 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1547 #if !defined(CONFIG_USER_ONLY)
1548 void do_40x_rfci (void)
1550 __do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1551 ~((target_ulong)0xFFFF0000), 0);
1554 void do_rfci (void)
1556 __do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1557 ~((target_ulong)0x3FFF0000), 0);
1560 void do_rfdi (void)
1562 __do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1563 ~((target_ulong)0x3FFF0000), 0);
1566 void do_rfmci (void)
1568 __do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1569 ~((target_ulong)0x3FFF0000), 0);
1572 void do_load_403_pb (int num)
1574 T0 = env->pb[num];
1577 void do_store_403_pb (int num)
1579 if (likely(env->pb[num] != T0)) {
1580 env->pb[num] = T0;
1581 /* Should be optimized */
1582 tlb_flush(env, 1);
1585 #endif
1587 /* 440 specific */
1588 void do_440_dlmzb (void)
1590 target_ulong mask;
1591 int i;
1593 i = 1;
1594 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1595 if ((T0 & mask) == 0)
1596 goto done;
1597 i++;
1599 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1600 if ((T1 & mask) == 0)
1601 break;
1602 i++;
1604 done:
1605 T0 = i;
1608 /* SPE extension helpers */
1609 /* Use a table to make this quicker */
1610 static uint8_t hbrev[16] = {
1611 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1612 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1615 static always_inline uint8_t byte_reverse (uint8_t val)
1617 return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
1620 static always_inline uint32_t word_reverse (uint32_t val)
1622 return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
1623 (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
1626 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
1627 target_ulong helper_brinc (target_ulong arg1, target_ulong arg2)
1629 uint32_t a, b, d, mask;
1631 mask = UINT32_MAX >> (32 - MASKBITS);
1632 a = arg1 & mask;
1633 b = arg2 & mask;
1634 d = word_reverse(1 + word_reverse(a | ~b));
1635 return (arg1 & ~mask) | (d & b);
1638 uint32_t helper_cntlsw32 (uint32_t val)
1640 if (val & 0x80000000)
1641 return clz32(~val);
1642 else
1643 return clz32(val);
1646 uint32_t helper_cntlzw32 (uint32_t val)
1648 return clz32(val);
1651 #define DO_SPE_OP1(name) \
1652 void do_ev##name (void) \
1654 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) | \
1655 (uint64_t)_do_e##name(T0_64); \
1658 #define DO_SPE_OP2(name) \
1659 void do_ev##name (void) \
1661 T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) | \
1662 (uint64_t)_do_e##name(T0_64, T1_64); \
1665 /* Fixed-point vector comparisons */
1666 #define DO_SPE_CMP(name) \
1667 void do_ev##name (void) \
1669 T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32, \
1670 T1_64 >> 32) << 32, \
1671 _do_e##name(T0_64, T1_64)); \
1674 static always_inline uint32_t _do_evcmp_merge (int t0, int t1)
1676 return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
1679 /* Single precision floating-point conversions from/to integer */
1680 static always_inline uint32_t _do_efscfsi (int32_t val)
1682 CPU_FloatU u;
1684 u.f = int32_to_float32(val, &env->spe_status);
1686 return u.l;
1689 static always_inline uint32_t _do_efscfui (uint32_t val)
1691 CPU_FloatU u;
1693 u.f = uint32_to_float32(val, &env->spe_status);
1695 return u.l;
1698 static always_inline int32_t _do_efsctsi (uint32_t val)
1700 CPU_FloatU u;
1702 u.l = val;
1703 /* NaN are not treated the same way IEEE 754 does */
1704 if (unlikely(isnan(u.f)))
1705 return 0;
1707 return float32_to_int32(u.f, &env->spe_status);
1710 static always_inline uint32_t _do_efsctui (uint32_t val)
1712 CPU_FloatU u;
1714 u.l = val;
1715 /* NaN are not treated the same way IEEE 754 does */
1716 if (unlikely(isnan(u.f)))
1717 return 0;
1719 return float32_to_uint32(u.f, &env->spe_status);
1722 static always_inline int32_t _do_efsctsiz (uint32_t val)
1724 CPU_FloatU u;
1726 u.l = val;
1727 /* NaN are not treated the same way IEEE 754 does */
1728 if (unlikely(isnan(u.f)))
1729 return 0;
1731 return float32_to_int32_round_to_zero(u.f, &env->spe_status);
1734 static always_inline uint32_t _do_efsctuiz (uint32_t val)
1736 CPU_FloatU u;
1738 u.l = val;
1739 /* NaN are not treated the same way IEEE 754 does */
1740 if (unlikely(isnan(u.f)))
1741 return 0;
1743 return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
1746 void do_efscfsi (void)
1748 T0_64 = _do_efscfsi(T0_64);
1751 void do_efscfui (void)
1753 T0_64 = _do_efscfui(T0_64);
1756 void do_efsctsi (void)
1758 T0_64 = _do_efsctsi(T0_64);
1761 void do_efsctui (void)
1763 T0_64 = _do_efsctui(T0_64);
1766 void do_efsctsiz (void)
1768 T0_64 = _do_efsctsiz(T0_64);
1771 void do_efsctuiz (void)
1773 T0_64 = _do_efsctuiz(T0_64);
1776 /* Single precision floating-point conversion to/from fractional */
1777 static always_inline uint32_t _do_efscfsf (uint32_t val)
1779 CPU_FloatU u;
1780 float32 tmp;
1782 u.f = int32_to_float32(val, &env->spe_status);
1783 tmp = int64_to_float32(1ULL << 32, &env->spe_status);
1784 u.f = float32_div(u.f, tmp, &env->spe_status);
1786 return u.l;
1789 static always_inline uint32_t _do_efscfuf (uint32_t val)
1791 CPU_FloatU u;
1792 float32 tmp;
1794 u.f = uint32_to_float32(val, &env->spe_status);
1795 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
1796 u.f = float32_div(u.f, tmp, &env->spe_status);
1798 return u.l;
1801 static always_inline int32_t _do_efsctsf (uint32_t val)
1803 CPU_FloatU u;
1804 float32 tmp;
1806 u.l = val;
1807 /* NaN are not treated the same way IEEE 754 does */
1808 if (unlikely(isnan(u.f)))
1809 return 0;
1810 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
1811 u.f = float32_mul(u.f, tmp, &env->spe_status);
1813 return float32_to_int32(u.f, &env->spe_status);
1816 static always_inline uint32_t _do_efsctuf (uint32_t val)
1818 CPU_FloatU u;
1819 float32 tmp;
1821 u.l = val;
1822 /* NaN are not treated the same way IEEE 754 does */
1823 if (unlikely(isnan(u.f)))
1824 return 0;
1825 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
1826 u.f = float32_mul(u.f, tmp, &env->spe_status);
1828 return float32_to_uint32(u.f, &env->spe_status);
1831 static always_inline int32_t _do_efsctsfz (uint32_t val)
1833 CPU_FloatU u;
1834 float32 tmp;
1836 u.l = val;
1837 /* NaN are not treated the same way IEEE 754 does */
1838 if (unlikely(isnan(u.f)))
1839 return 0;
1840 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
1841 u.f = float32_mul(u.f, tmp, &env->spe_status);
1843 return float32_to_int32_round_to_zero(u.f, &env->spe_status);
1846 static always_inline uint32_t _do_efsctufz (uint32_t val)
1848 CPU_FloatU u;
1849 float32 tmp;
1851 u.l = val;
1852 /* NaN are not treated the same way IEEE 754 does */
1853 if (unlikely(isnan(u.f)))
1854 return 0;
1855 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
1856 u.f = float32_mul(u.f, tmp, &env->spe_status);
1858 return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
1861 void do_efscfsf (void)
1863 T0_64 = _do_efscfsf(T0_64);
1866 void do_efscfuf (void)
1868 T0_64 = _do_efscfuf(T0_64);
1871 void do_efsctsf (void)
1873 T0_64 = _do_efsctsf(T0_64);
1876 void do_efsctuf (void)
1878 T0_64 = _do_efsctuf(T0_64);
1881 void do_efsctsfz (void)
1883 T0_64 = _do_efsctsfz(T0_64);
1886 void do_efsctufz (void)
1888 T0_64 = _do_efsctufz(T0_64);
1891 /* Double precision floating point helpers */
1892 static always_inline int _do_efdcmplt (uint64_t op1, uint64_t op2)
1894 /* XXX: TODO: test special values (NaN, infinites, ...) */
1895 return _do_efdtstlt(op1, op2);
1898 static always_inline int _do_efdcmpgt (uint64_t op1, uint64_t op2)
1900 /* XXX: TODO: test special values (NaN, infinites, ...) */
1901 return _do_efdtstgt(op1, op2);
1904 static always_inline int _do_efdcmpeq (uint64_t op1, uint64_t op2)
1906 /* XXX: TODO: test special values (NaN, infinites, ...) */
1907 return _do_efdtsteq(op1, op2);
1910 void do_efdcmplt (void)
1912 T0 = _do_efdcmplt(T0_64, T1_64);
1915 void do_efdcmpgt (void)
1917 T0 = _do_efdcmpgt(T0_64, T1_64);
1920 void do_efdcmpeq (void)
1922 T0 = _do_efdcmpeq(T0_64, T1_64);
1925 /* Double precision floating-point conversion to/from integer */
1926 static always_inline uint64_t _do_efdcfsi (int64_t val)
1928 CPU_DoubleU u;
1930 u.d = int64_to_float64(val, &env->spe_status);
1932 return u.ll;
1935 static always_inline uint64_t _do_efdcfui (uint64_t val)
1937 CPU_DoubleU u;
1939 u.d = uint64_to_float64(val, &env->spe_status);
1941 return u.ll;
1944 static always_inline int64_t _do_efdctsi (uint64_t val)
1946 CPU_DoubleU u;
1948 u.ll = val;
1949 /* NaN are not treated the same way IEEE 754 does */
1950 if (unlikely(isnan(u.d)))
1951 return 0;
1953 return float64_to_int64(u.d, &env->spe_status);
1956 static always_inline uint64_t _do_efdctui (uint64_t val)
1958 CPU_DoubleU u;
1960 u.ll = val;
1961 /* NaN are not treated the same way IEEE 754 does */
1962 if (unlikely(isnan(u.d)))
1963 return 0;
1965 return float64_to_uint64(u.d, &env->spe_status);
1968 static always_inline int64_t _do_efdctsiz (uint64_t val)
1970 CPU_DoubleU u;
1972 u.ll = val;
1973 /* NaN are not treated the same way IEEE 754 does */
1974 if (unlikely(isnan(u.d)))
1975 return 0;
1977 return float64_to_int64_round_to_zero(u.d, &env->spe_status);
1980 static always_inline uint64_t _do_efdctuiz (uint64_t val)
1982 CPU_DoubleU u;
1984 u.ll = val;
1985 /* NaN are not treated the same way IEEE 754 does */
1986 if (unlikely(isnan(u.d)))
1987 return 0;
1989 return float64_to_uint64_round_to_zero(u.d, &env->spe_status);
1992 void do_efdcfsi (void)
1994 T0_64 = _do_efdcfsi(T0_64);
1997 void do_efdcfui (void)
1999 T0_64 = _do_efdcfui(T0_64);
2002 void do_efdctsi (void)
2004 T0_64 = _do_efdctsi(T0_64);
2007 void do_efdctui (void)
2009 T0_64 = _do_efdctui(T0_64);
2012 void do_efdctsiz (void)
2014 T0_64 = _do_efdctsiz(T0_64);
2017 void do_efdctuiz (void)
2019 T0_64 = _do_efdctuiz(T0_64);
2022 /* Double precision floating-point conversion to/from fractional */
2023 static always_inline uint64_t _do_efdcfsf (int64_t val)
2025 CPU_DoubleU u;
2026 float64 tmp;
2028 u.d = int32_to_float64(val, &env->spe_status);
2029 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2030 u.d = float64_div(u.d, tmp, &env->spe_status);
2032 return u.ll;
2035 static always_inline uint64_t _do_efdcfuf (uint64_t val)
2037 CPU_DoubleU u;
2038 float64 tmp;
2040 u.d = uint32_to_float64(val, &env->spe_status);
2041 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2042 u.d = float64_div(u.d, tmp, &env->spe_status);
2044 return u.ll;
2047 static always_inline int64_t _do_efdctsf (uint64_t val)
2049 CPU_DoubleU u;
2050 float64 tmp;
2052 u.ll = val;
2053 /* NaN are not treated the same way IEEE 754 does */
2054 if (unlikely(isnan(u.d)))
2055 return 0;
2056 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2057 u.d = float64_mul(u.d, tmp, &env->spe_status);
2059 return float64_to_int32(u.d, &env->spe_status);
2062 static always_inline uint64_t _do_efdctuf (uint64_t val)
2064 CPU_DoubleU u;
2065 float64 tmp;
2067 u.ll = val;
2068 /* NaN are not treated the same way IEEE 754 does */
2069 if (unlikely(isnan(u.d)))
2070 return 0;
2071 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2072 u.d = float64_mul(u.d, tmp, &env->spe_status);
2074 return float64_to_uint32(u.d, &env->spe_status);
2077 static always_inline int64_t _do_efdctsfz (uint64_t val)
2079 CPU_DoubleU u;
2080 float64 tmp;
2082 u.ll = val;
2083 /* NaN are not treated the same way IEEE 754 does */
2084 if (unlikely(isnan(u.d)))
2085 return 0;
2086 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2087 u.d = float64_mul(u.d, tmp, &env->spe_status);
2089 return float64_to_int32_round_to_zero(u.d, &env->spe_status);
2092 static always_inline uint64_t _do_efdctufz (uint64_t val)
2094 CPU_DoubleU u;
2095 float64 tmp;
2097 u.ll = val;
2098 /* NaN are not treated the same way IEEE 754 does */
2099 if (unlikely(isnan(u.d)))
2100 return 0;
2101 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2102 u.d = float64_mul(u.d, tmp, &env->spe_status);
2104 return float64_to_uint32_round_to_zero(u.d, &env->spe_status);
2107 void do_efdcfsf (void)
2109 T0_64 = _do_efdcfsf(T0_64);
2112 void do_efdcfuf (void)
2114 T0_64 = _do_efdcfuf(T0_64);
2117 void do_efdctsf (void)
2119 T0_64 = _do_efdctsf(T0_64);
2122 void do_efdctuf (void)
2124 T0_64 = _do_efdctuf(T0_64);
2127 void do_efdctsfz (void)
2129 T0_64 = _do_efdctsfz(T0_64);
2132 void do_efdctufz (void)
2134 T0_64 = _do_efdctufz(T0_64);
2137 /* Floating point conversion between single and double precision */
2138 static always_inline uint32_t _do_efscfd (uint64_t val)
2140 CPU_DoubleU u1;
2141 CPU_FloatU u2;
2143 u1.ll = val;
2144 u2.f = float64_to_float32(u1.d, &env->spe_status);
2146 return u2.l;
2149 static always_inline uint64_t _do_efdcfs (uint32_t val)
2151 CPU_DoubleU u2;
2152 CPU_FloatU u1;
2154 u1.l = val;
2155 u2.d = float32_to_float64(u1.f, &env->spe_status);
2157 return u2.ll;
2160 void do_efscfd (void)
2162 T0_64 = _do_efscfd(T0_64);
2165 void do_efdcfs (void)
2167 T0_64 = _do_efdcfs(T0_64);
2170 /* Single precision fixed-point vector arithmetic */
2171 /* evfsabs */
2172 DO_SPE_OP1(fsabs);
2173 /* evfsnabs */
2174 DO_SPE_OP1(fsnabs);
2175 /* evfsneg */
2176 DO_SPE_OP1(fsneg);
2177 /* evfsadd */
2178 DO_SPE_OP2(fsadd);
2179 /* evfssub */
2180 DO_SPE_OP2(fssub);
2181 /* evfsmul */
2182 DO_SPE_OP2(fsmul);
2183 /* evfsdiv */
2184 DO_SPE_OP2(fsdiv);
2186 /* Single-precision floating-point comparisons */
2187 static always_inline int _do_efscmplt (uint32_t op1, uint32_t op2)
2189 /* XXX: TODO: test special values (NaN, infinites, ...) */
2190 return _do_efststlt(op1, op2);
2193 static always_inline int _do_efscmpgt (uint32_t op1, uint32_t op2)
2195 /* XXX: TODO: test special values (NaN, infinites, ...) */
2196 return _do_efststgt(op1, op2);
2199 static always_inline int _do_efscmpeq (uint32_t op1, uint32_t op2)
2201 /* XXX: TODO: test special values (NaN, infinites, ...) */
2202 return _do_efststeq(op1, op2);
2205 void do_efscmplt (void)
2207 T0 = _do_efscmplt(T0_64, T1_64);
2210 void do_efscmpgt (void)
2212 T0 = _do_efscmpgt(T0_64, T1_64);
2215 void do_efscmpeq (void)
2217 T0 = _do_efscmpeq(T0_64, T1_64);
2220 /* Single-precision floating-point vector comparisons */
2221 /* evfscmplt */
2222 DO_SPE_CMP(fscmplt);
2223 /* evfscmpgt */
2224 DO_SPE_CMP(fscmpgt);
2225 /* evfscmpeq */
2226 DO_SPE_CMP(fscmpeq);
2227 /* evfststlt */
2228 DO_SPE_CMP(fststlt);
2229 /* evfststgt */
2230 DO_SPE_CMP(fststgt);
2231 /* evfststeq */
2232 DO_SPE_CMP(fststeq);
2234 /* Single-precision floating-point vector conversions */
2235 /* evfscfsi */
2236 DO_SPE_OP1(fscfsi);
2237 /* evfscfui */
2238 DO_SPE_OP1(fscfui);
2239 /* evfscfuf */
2240 DO_SPE_OP1(fscfuf);
2241 /* evfscfsf */
2242 DO_SPE_OP1(fscfsf);
2243 /* evfsctsi */
2244 DO_SPE_OP1(fsctsi);
2245 /* evfsctui */
2246 DO_SPE_OP1(fsctui);
2247 /* evfsctsiz */
2248 DO_SPE_OP1(fsctsiz);
2249 /* evfsctuiz */
2250 DO_SPE_OP1(fsctuiz);
2251 /* evfsctsf */
2252 DO_SPE_OP1(fsctsf);
2253 /* evfsctuf */
2254 DO_SPE_OP1(fsctuf);
2256 /*****************************************************************************/
2257 /* Softmmu support */
2258 #if !defined (CONFIG_USER_ONLY)
2260 #define MMUSUFFIX _mmu
2262 #define SHIFT 0
2263 #include "softmmu_template.h"
2265 #define SHIFT 1
2266 #include "softmmu_template.h"
2268 #define SHIFT 2
2269 #include "softmmu_template.h"
2271 #define SHIFT 3
2272 #include "softmmu_template.h"
2274 /* try to fill the TLB and return an exception if error. If retaddr is
2275 NULL, it means that the function was called in C code (i.e. not
2276 from generated code or from helper.c) */
2277 /* XXX: fix it to restore all registers */
2278 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2280 TranslationBlock *tb;
2281 CPUState *saved_env;
2282 unsigned long pc;
2283 int ret;
2285 /* XXX: hack to restore env in all cases, even if not called from
2286 generated code */
2287 saved_env = env;
2288 env = cpu_single_env;
2289 ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2290 if (unlikely(ret != 0)) {
2291 if (likely(retaddr)) {
2292 /* now we have a real cpu fault */
2293 pc = (unsigned long)retaddr;
2294 tb = tb_find_pc(pc);
2295 if (likely(tb)) {
2296 /* the PC is inside the translated code. It means that we have
2297 a virtual CPU fault */
2298 cpu_restore_state(tb, env, pc, NULL);
2301 do_raise_exception_err(env->exception_index, env->error_code);
2303 env = saved_env;
2306 /* Software driven TLBs management */
2307 /* PowerPC 602/603 software TLB load instructions helpers */
2308 void do_load_6xx_tlb (int is_code)
2310 target_ulong RPN, CMP, EPN;
2311 int way;
2313 RPN = env->spr[SPR_RPA];
2314 if (is_code) {
2315 CMP = env->spr[SPR_ICMP];
2316 EPN = env->spr[SPR_IMISS];
2317 } else {
2318 CMP = env->spr[SPR_DCMP];
2319 EPN = env->spr[SPR_DMISS];
2321 way = (env->spr[SPR_SRR1] >> 17) & 1;
2322 #if defined (DEBUG_SOFTWARE_TLB)
2323 if (loglevel != 0) {
2324 fprintf(logfile, "%s: EPN " TDX " " ADDRX " PTE0 " ADDRX
2325 " PTE1 " ADDRX " way %d\n",
2326 __func__, T0, EPN, CMP, RPN, way);
2328 #endif
2329 /* Store this TLB */
2330 ppc6xx_tlb_store(env, (uint32_t)(T0 & TARGET_PAGE_MASK),
2331 way, is_code, CMP, RPN);
2334 void do_load_74xx_tlb (int is_code)
2336 target_ulong RPN, CMP, EPN;
2337 int way;
2339 RPN = env->spr[SPR_PTELO];
2340 CMP = env->spr[SPR_PTEHI];
2341 EPN = env->spr[SPR_TLBMISS] & ~0x3;
2342 way = env->spr[SPR_TLBMISS] & 0x3;
2343 #if defined (DEBUG_SOFTWARE_TLB)
2344 if (loglevel != 0) {
2345 fprintf(logfile, "%s: EPN " TDX " " ADDRX " PTE0 " ADDRX
2346 " PTE1 " ADDRX " way %d\n",
2347 __func__, T0, EPN, CMP, RPN, way);
2349 #endif
2350 /* Store this TLB */
2351 ppc6xx_tlb_store(env, (uint32_t)(T0 & TARGET_PAGE_MASK),
2352 way, is_code, CMP, RPN);
2355 static always_inline target_ulong booke_tlb_to_page_size (int size)
2357 return 1024 << (2 * size);
2360 static always_inline int booke_page_size_to_tlb (target_ulong page_size)
2362 int size;
2364 switch (page_size) {
2365 case 0x00000400UL:
2366 size = 0x0;
2367 break;
2368 case 0x00001000UL:
2369 size = 0x1;
2370 break;
2371 case 0x00004000UL:
2372 size = 0x2;
2373 break;
2374 case 0x00010000UL:
2375 size = 0x3;
2376 break;
2377 case 0x00040000UL:
2378 size = 0x4;
2379 break;
2380 case 0x00100000UL:
2381 size = 0x5;
2382 break;
2383 case 0x00400000UL:
2384 size = 0x6;
2385 break;
2386 case 0x01000000UL:
2387 size = 0x7;
2388 break;
2389 case 0x04000000UL:
2390 size = 0x8;
2391 break;
2392 case 0x10000000UL:
2393 size = 0x9;
2394 break;
2395 case 0x40000000UL:
2396 size = 0xA;
2397 break;
2398 #if defined (TARGET_PPC64)
2399 case 0x000100000000ULL:
2400 size = 0xB;
2401 break;
2402 case 0x000400000000ULL:
2403 size = 0xC;
2404 break;
2405 case 0x001000000000ULL:
2406 size = 0xD;
2407 break;
2408 case 0x004000000000ULL:
2409 size = 0xE;
2410 break;
2411 case 0x010000000000ULL:
2412 size = 0xF;
2413 break;
2414 #endif
2415 default:
2416 size = -1;
2417 break;
2420 return size;
2423 /* Helpers for 4xx TLB management */
2424 void do_4xx_tlbre_lo (void)
2426 ppcemb_tlb_t *tlb;
2427 int size;
2429 T0 &= 0x3F;
2430 tlb = &env->tlb[T0].tlbe;
2431 T0 = tlb->EPN;
2432 if (tlb->prot & PAGE_VALID)
2433 T0 |= 0x400;
2434 size = booke_page_size_to_tlb(tlb->size);
2435 if (size < 0 || size > 0x7)
2436 size = 1;
2437 T0 |= size << 7;
2438 env->spr[SPR_40x_PID] = tlb->PID;
2441 void do_4xx_tlbre_hi (void)
2443 ppcemb_tlb_t *tlb;
2445 T0 &= 0x3F;
2446 tlb = &env->tlb[T0].tlbe;
2447 T0 = tlb->RPN;
2448 if (tlb->prot & PAGE_EXEC)
2449 T0 |= 0x200;
2450 if (tlb->prot & PAGE_WRITE)
2451 T0 |= 0x100;
2454 void do_4xx_tlbwe_hi (void)
2456 ppcemb_tlb_t *tlb;
2457 target_ulong page, end;
2459 #if defined (DEBUG_SOFTWARE_TLB)
2460 if (loglevel != 0) {
2461 fprintf(logfile, "%s T0 " TDX " T1 " TDX "\n", __func__, T0, T1);
2463 #endif
2464 T0 &= 0x3F;
2465 tlb = &env->tlb[T0].tlbe;
2466 /* Invalidate previous TLB (if it's valid) */
2467 if (tlb->prot & PAGE_VALID) {
2468 end = tlb->EPN + tlb->size;
2469 #if defined (DEBUG_SOFTWARE_TLB)
2470 if (loglevel != 0) {
2471 fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
2472 " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2474 #endif
2475 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2476 tlb_flush_page(env, page);
2478 tlb->size = booke_tlb_to_page_size((T1 >> 7) & 0x7);
2479 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2480 * If this ever occurs, one should use the ppcemb target instead
2481 * of the ppc or ppc64 one
2483 if ((T1 & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
2484 cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
2485 "are not supported (%d)\n",
2486 tlb->size, TARGET_PAGE_SIZE, (int)((T1 >> 7) & 0x7));
2488 tlb->EPN = T1 & ~(tlb->size - 1);
2489 if (T1 & 0x40)
2490 tlb->prot |= PAGE_VALID;
2491 else
2492 tlb->prot &= ~PAGE_VALID;
2493 if (T1 & 0x20) {
2494 /* XXX: TO BE FIXED */
2495 cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
2497 tlb->PID = env->spr[SPR_40x_PID]; /* PID */
2498 tlb->attr = T1 & 0xFF;
2499 #if defined (DEBUG_SOFTWARE_TLB)
2500 if (loglevel != 0) {
2501 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2502 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2503 (int)T0, tlb->RPN, tlb->EPN, tlb->size,
2504 tlb->prot & PAGE_READ ? 'r' : '-',
2505 tlb->prot & PAGE_WRITE ? 'w' : '-',
2506 tlb->prot & PAGE_EXEC ? 'x' : '-',
2507 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2509 #endif
2510 /* Invalidate new TLB (if valid) */
2511 if (tlb->prot & PAGE_VALID) {
2512 end = tlb->EPN + tlb->size;
2513 #if defined (DEBUG_SOFTWARE_TLB)
2514 if (loglevel != 0) {
2515 fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
2516 " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2518 #endif
2519 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2520 tlb_flush_page(env, page);
2524 void do_4xx_tlbwe_lo (void)
2526 ppcemb_tlb_t *tlb;
2528 #if defined (DEBUG_SOFTWARE_TLB)
2529 if (loglevel != 0) {
2530 fprintf(logfile, "%s T0 " TDX " T1 " TDX "\n", __func__, T0, T1);
2532 #endif
2533 T0 &= 0x3F;
2534 tlb = &env->tlb[T0].tlbe;
2535 tlb->RPN = T1 & 0xFFFFFC00;
2536 tlb->prot = PAGE_READ;
2537 if (T1 & 0x200)
2538 tlb->prot |= PAGE_EXEC;
2539 if (T1 & 0x100)
2540 tlb->prot |= PAGE_WRITE;
2541 #if defined (DEBUG_SOFTWARE_TLB)
2542 if (loglevel != 0) {
2543 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2544 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2545 (int)T0, tlb->RPN, tlb->EPN, tlb->size,
2546 tlb->prot & PAGE_READ ? 'r' : '-',
2547 tlb->prot & PAGE_WRITE ? 'w' : '-',
2548 tlb->prot & PAGE_EXEC ? 'x' : '-',
2549 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2551 #endif
2554 /* PowerPC 440 TLB management */
2555 void do_440_tlbwe (int word)
2557 ppcemb_tlb_t *tlb;
2558 target_ulong EPN, RPN, size;
2559 int do_flush_tlbs;
2561 #if defined (DEBUG_SOFTWARE_TLB)
2562 if (loglevel != 0) {
2563 fprintf(logfile, "%s word %d T0 " TDX " T1 " TDX "\n",
2564 __func__, word, T0, T1);
2566 #endif
2567 do_flush_tlbs = 0;
2568 T0 &= 0x3F;
2569 tlb = &env->tlb[T0].tlbe;
2570 switch (word) {
2571 default:
2572 /* Just here to please gcc */
2573 case 0:
2574 EPN = T1 & 0xFFFFFC00;
2575 if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
2576 do_flush_tlbs = 1;
2577 tlb->EPN = EPN;
2578 size = booke_tlb_to_page_size((T1 >> 4) & 0xF);
2579 if ((tlb->prot & PAGE_VALID) && tlb->size < size)
2580 do_flush_tlbs = 1;
2581 tlb->size = size;
2582 tlb->attr &= ~0x1;
2583 tlb->attr |= (T1 >> 8) & 1;
2584 if (T1 & 0x200) {
2585 tlb->prot |= PAGE_VALID;
2586 } else {
2587 if (tlb->prot & PAGE_VALID) {
2588 tlb->prot &= ~PAGE_VALID;
2589 do_flush_tlbs = 1;
2592 tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
2593 if (do_flush_tlbs)
2594 tlb_flush(env, 1);
2595 break;
2596 case 1:
2597 RPN = T1 & 0xFFFFFC0F;
2598 if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
2599 tlb_flush(env, 1);
2600 tlb->RPN = RPN;
2601 break;
2602 case 2:
2603 tlb->attr = (tlb->attr & 0x1) | (T1 & 0x0000FF00);
2604 tlb->prot = tlb->prot & PAGE_VALID;
2605 if (T1 & 0x1)
2606 tlb->prot |= PAGE_READ << 4;
2607 if (T1 & 0x2)
2608 tlb->prot |= PAGE_WRITE << 4;
2609 if (T1 & 0x4)
2610 tlb->prot |= PAGE_EXEC << 4;
2611 if (T1 & 0x8)
2612 tlb->prot |= PAGE_READ;
2613 if (T1 & 0x10)
2614 tlb->prot |= PAGE_WRITE;
2615 if (T1 & 0x20)
2616 tlb->prot |= PAGE_EXEC;
2617 break;
2621 void do_440_tlbre (int word)
2623 ppcemb_tlb_t *tlb;
2624 int size;
2626 T0 &= 0x3F;
2627 tlb = &env->tlb[T0].tlbe;
2628 switch (word) {
2629 default:
2630 /* Just here to please gcc */
2631 case 0:
2632 T0 = tlb->EPN;
2633 size = booke_page_size_to_tlb(tlb->size);
2634 if (size < 0 || size > 0xF)
2635 size = 1;
2636 T0 |= size << 4;
2637 if (tlb->attr & 0x1)
2638 T0 |= 0x100;
2639 if (tlb->prot & PAGE_VALID)
2640 T0 |= 0x200;
2641 env->spr[SPR_440_MMUCR] &= ~0x000000FF;
2642 env->spr[SPR_440_MMUCR] |= tlb->PID;
2643 break;
2644 case 1:
2645 T0 = tlb->RPN;
2646 break;
2647 case 2:
2648 T0 = tlb->attr & ~0x1;
2649 if (tlb->prot & (PAGE_READ << 4))
2650 T0 |= 0x1;
2651 if (tlb->prot & (PAGE_WRITE << 4))
2652 T0 |= 0x2;
2653 if (tlb->prot & (PAGE_EXEC << 4))
2654 T0 |= 0x4;
2655 if (tlb->prot & PAGE_READ)
2656 T0 |= 0x8;
2657 if (tlb->prot & PAGE_WRITE)
2658 T0 |= 0x10;
2659 if (tlb->prot & PAGE_EXEC)
2660 T0 |= 0x20;
2661 break;
2664 #endif /* !CONFIG_USER_ONLY */