2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2005 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
23 #define MEMSUFFIX _raw
24 #include "op_helper_mem.h"
25 #if !defined(CONFIG_USER_ONLY)
26 #define MEMSUFFIX _user
27 #include "op_helper_mem.h"
28 #define MEMSUFFIX _kernel
29 #include "op_helper_mem.h"
33 //#define DEBUG_EXCEPTIONS
34 //#define FLUSH_ALL_TLBS
36 #define Ts0 (long)((target_long)T0)
37 #define Ts1 (long)((target_long)T1)
38 #define Ts2 (long)((target_long)T2)
40 /*****************************************************************************/
41 /* Exceptions processing helpers */
42 void cpu_loop_exit(void)
44 longjmp(env->jmp_env, 1);
47 void do_raise_exception_err (uint32_t exception, int error_code)
50 printf("Raise exception %3x code : %d\n", exception, error_code);
54 if (error_code == EXCP_FP && msr_fe0 == 0 && msr_fe1 == 0)
60 env->exception_index = exception;
61 env->error_code = error_code;
65 void do_raise_exception (uint32_t exception)
67 do_raise_exception_err(exception, 0);
70 /*****************************************************************************/
71 /* Fixed point operations helpers */
76 if (likely(!((T2 ^ T1 ^ (-1)) & (T2 ^ T0) & (1 << 31)))) {
88 if (likely(T0 >= T2)) {
93 if (likely(!((T2 ^ T1 ^ (-1)) & (T2 ^ T0) & (1 << 31)))) {
105 if (likely(!(T0 < T2 || (xer_ca == 1 && T0 == T2)))) {
116 if (likely(!(T0 < T2 || (xer_ca == 1 && T0 == T2)))) {
121 if (likely(!((T2 ^ T1 ^ (-1)) & (T2 ^ T0) & (1 << 31)))) {
129 void do_addmeo (void)
133 if (likely(!(T1 & (T1 ^ T0) & (1 << 31)))) {
143 void do_addzeo (void)
147 if (likely(!((T1 ^ (-1)) & (T1 ^ T0) & (1 << 31)))) {
153 if (likely(T0 >= T1)) {
162 if (likely(!((Ts0 == INT32_MIN && Ts1 == -1) || Ts1 == 0))) {
168 T0 = (-1) * ((uint32_t)T0 >> 31);
172 void do_divwuo (void)
174 if (likely((uint32_t)T1 != 0)) {
176 T0 = (uint32_t)T0 / (uint32_t)T1;
184 void do_mullwo (void)
186 int64_t res = (int64_t)Ts0 * (int64_t)Ts1;
188 if (likely((int32_t)res == res)) {
199 if (likely(T0 != INT32_MIN)) {
212 if (likely(!(((~T2) ^ T1 ^ (-1)) & ((~T2) ^ T0) & (1 << 31)))) {
221 void do_subfco (void)
225 if (likely(T0 > T1)) {
230 if (likely(!(((~T2) ^ T1 ^ (-1)) & ((~T2) ^ T0) & (1 << 31)))) {
240 T0 = T1 + ~T0 + xer_ca;
241 if (likely(T0 >= T1 && (xer_ca == 0 || T0 != T1))) {
248 void do_subfeo (void)
251 T0 = T1 + ~T0 + xer_ca;
252 if (likely(!((~T2 ^ T1 ^ (-1)) & (~T2 ^ T0) & (1 << 31)))) {
258 if (likely(T0 >= T1 && (xer_ca == 0 || T0 != T1))) {
265 void do_subfmeo (void)
268 T0 = ~T0 + xer_ca - 1;
269 if (likely(!(~T1 & (~T1 ^ T0) & (1 << 31)))) {
275 if (likely(T1 != -1))
279 void do_subfzeo (void)
283 if (likely(!((~T1 ^ (-1)) & ((~T1) ^ T0) & (1 << 31)))) {
289 if (likely(T0 >= ~T1)) {
296 /* shift right arithmetic helper */
301 if (likely(!(T1 & 0x20UL))) {
302 if (likely(T1 != 0)) {
303 ret = (int32_t)T0 >> (T1 & 0x1fUL);
304 if (likely(ret >= 0 || ((int32_t)T0 & ((1 << T1) - 1)) == 0)) {
314 ret = (-1) * ((uint32_t)T0 >> 31);
315 if (likely(ret >= 0 || ((uint32_t)T0 & ~0x80000000UL) == 0)) {
324 /*****************************************************************************/
325 /* Floating point operations helpers */
333 /* XXX: higher bits are not supposed to be significant.
334 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
336 p.i = float64_to_int32(FT0, &env->fp_status);
337 p.i |= 0xFFF80000ULL << 32;
341 void do_fctiwz (void)
348 /* XXX: higher bits are not supposed to be significant.
349 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
351 p.i = float64_to_int32_round_to_zero(FT0, &env->fp_status);
352 p.i |= 0xFFF80000ULL << 32;
356 void do_fnmadd (void)
358 FT0 = float64_mul(FT0, FT1, &env->fp_status);
359 FT0 = float64_add(FT0, FT2, &env->fp_status);
360 if (likely(!isnan(FT0)))
361 FT0 = float64_chs(FT0);
364 void do_fnmsub (void)
366 FT0 = float64_mul(FT0, FT1, &env->fp_status);
367 FT0 = float64_sub(FT0, FT2, &env->fp_status);
368 if (likely(!isnan(FT0)))
369 FT0 = float64_chs(FT0);
374 FT0 = float64_sqrt(FT0, &env->fp_status);
384 if (likely(isnormal(FT0))) {
385 FT0 = (float)(1.0 / FT0);
388 if (p.i == 0x8000000000000000ULL) {
389 p.i = 0xFFF0000000000000ULL;
390 } else if (p.i == 0x0000000000000000ULL) {
391 p.i = 0x7FF0000000000000ULL;
392 } else if (isnan(FT0)) {
393 p.i = 0x7FF8000000000000ULL;
394 } else if (FT0 < 0.0) {
395 p.i = 0x8000000000000000ULL;
397 p.i = 0x0000000000000000ULL;
403 void do_frsqrte (void)
410 if (likely(isnormal(FT0) && FT0 > 0.0)) {
411 FT0 = float64_sqrt(FT0, &env->fp_status);
412 FT0 = float32_div(1.0, FT0, &env->fp_status);
415 if (p.i == 0x8000000000000000ULL) {
416 p.i = 0xFFF0000000000000ULL;
417 } else if (p.i == 0x0000000000000000ULL) {
418 p.i = 0x7FF0000000000000ULL;
419 } else if (isnan(FT0)) {
420 if (!(p.i & 0x0008000000000000ULL))
421 p.i |= 0x000FFFFFFFFFFFFFULL;
422 } else if (FT0 < 0) {
423 p.i = 0x7FF8000000000000ULL;
425 p.i = 0x0000000000000000ULL;
441 if (likely(!isnan(FT0) && !isnan(FT1))) {
442 if (float64_lt(FT0, FT1, &env->fp_status)) {
444 } else if (!float64_le(FT0, FT1, &env->fp_status)) {
451 env->fpscr[4] |= 0x1;
452 env->fpscr[6] |= 0x1;
459 env->fpscr[4] &= ~0x1;
460 if (likely(!isnan(FT0) && !isnan(FT1))) {
461 if (float64_lt(FT0, FT1, &env->fp_status)) {
463 } else if (!float64_le(FT0, FT1, &env->fp_status)) {
470 env->fpscr[4] |= 0x1;
471 /* I don't know how to test "quiet" nan... */
472 if (0 /* || ! quiet_nan(...) */) {
473 env->fpscr[6] |= 0x1;
474 if (!(env->fpscr[1] & 0x8))
475 env->fpscr[4] |= 0x8;
477 env->fpscr[4] |= 0x8;
485 env->nip = env->spr[SPR_SRR0] & ~0x00000003;
486 T0 = env->spr[SPR_SRR1] & ~0xFFFF0000UL;
487 do_store_msr(env, T0);
488 #if defined (DEBUG_OP)
491 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
494 void do_tw (uint32_t cmp, int flags)
496 if (!likely(!((Ts0 < (int32_t)cmp && (flags & 0x10)) ||
497 (Ts0 > (int32_t)cmp && (flags & 0x08)) ||
498 (Ts0 == (int32_t)cmp && (flags & 0x04)) ||
499 (T0 < cmp && (flags & 0x02)) ||
500 (T0 > cmp && (flags & 0x01)))))
501 do_raise_exception_err(EXCP_PROGRAM, EXCP_TRAP);
504 /* Instruction cache invalidation helper */
508 /* Invalidate one cache line :
509 * PowerPC specification says this is to be treated like a load
510 * (not a fetch) by the MMU. To be sure it will be so,
511 * do the load "by hand".
513 #if defined(TARGET_PPC64)
517 tmp = ldl_kernel(T0);
518 T0 &= ~(ICACHE_LINE_SIZE - 1);
519 tb_invalidate_page_range(T0, T0 + ICACHE_LINE_SIZE);
522 /*****************************************************************************/
523 /* MMU related helpers */
524 /* TLB invalidation helpers */
532 #if !defined(FLUSH_ALL_TLBS)
533 tlb_flush_page(env, T0);
539 /*****************************************************************************/
540 /* Softmmu support */
541 #if !defined (CONFIG_USER_ONLY)
543 #define MMUSUFFIX _mmu
544 #define GETPC() (__builtin_return_address(0))
547 #include "softmmu_template.h"
550 #include "softmmu_template.h"
553 #include "softmmu_template.h"
556 #include "softmmu_template.h"
558 /* try to fill the TLB and return an exception if error. If retaddr is
559 NULL, it means that the function was called in C code (i.e. not
560 from generated code or from helper.c) */
561 /* XXX: fix it to restore all registers */
562 void tlb_fill (target_ulong addr, int is_write, int is_user, void *retaddr)
564 TranslationBlock *tb;
566 target_phys_addr_t pc;
569 /* XXX: hack to restore env in all cases, even if not called from
572 env = cpu_single_env;
573 ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, is_user, 1);
574 if (!likely(ret == 0)) {
575 if (likely(retaddr)) {
576 /* now we have a real cpu fault */
577 pc = (target_phys_addr_t)retaddr;
580 /* the PC is inside the translated code. It means that we have
581 a virtual CPU fault */
582 cpu_restore_state(tb, env, pc, NULL);
585 do_raise_exception_err(env->exception_index, env->error_code);
589 #endif /* !CONFIG_USER_ONLY */