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
22 #define MEMSUFFIX _raw
23 #include "op_helper_mem.h"
24 #if !defined(CONFIG_USER_ONLY)
25 #define MEMSUFFIX _user
26 #include "op_helper_mem.h"
27 #define MEMSUFFIX _kernel
28 #include "op_helper_mem.h"
32 //#define DEBUG_EXCEPTIONS
33 //#define FLUSH_ALL_TLBS
35 #define Ts0 (long)((target_long)T0)
36 #define Ts1 (long)((target_long)T1)
37 #define Ts2 (long)((target_long)T2)
39 /*****************************************************************************/
40 /* Exceptions processing helpers */
41 void cpu_loop_exit(void)
43 longjmp(env
->jmp_env
, 1);
46 void do_raise_exception_err (uint32_t exception
, int error_code
)
49 printf("Raise exception %3x code : %d\n", exception
, error_code
);
53 if (error_code
== EXCP_FP
&& msr_fe0
== 0 && msr_fe1
== 0)
59 env
->exception_index
= exception
;
60 env
->error_code
= error_code
;
64 void do_raise_exception (uint32_t exception
)
66 do_raise_exception_err(exception
, 0);
69 /*****************************************************************************/
70 /* Fixed point operations helpers */
75 if (likely(!((T2
^ T1
^ (-1)) & (T2
^ T0
) & (1 << 31)))) {
87 if (likely(T0
>= T2
)) {
92 if (likely(!((T2
^ T1
^ (-1)) & (T2
^ T0
) & (1 << 31)))) {
104 if (likely(!(T0
< T2
|| (xer_ca
== 1 && T0
== T2
)))) {
115 if (likely(!(T0
< T2
|| (xer_ca
== 1 && T0
== T2
)))) {
120 if (likely(!((T2
^ T1
^ (-1)) & (T2
^ T0
) & (1 << 31)))) {
128 void do_addmeo (void)
132 if (likely(!(T1
& (T1
^ T0
) & (1 << 31)))) {
142 void do_addzeo (void)
146 if (likely(!((T1
^ (-1)) & (T1
^ T0
) & (1 << 31)))) {
152 if (likely(T0
>= T1
)) {
161 if (likely(!((Ts0
== INT32_MIN
&& Ts1
== -1) || Ts1
== 0))) {
167 T0
= (-1) * ((uint32_t)T0
>> 31);
171 void do_divwuo (void)
173 if (likely((uint32_t)T1
!= 0)) {
175 T0
= (uint32_t)T0
/ (uint32_t)T1
;
183 void do_mullwo (void)
185 int64_t res
= (int64_t)Ts0
* (int64_t)Ts1
;
187 if (likely((int32_t)res
== res
)) {
198 if (likely(T0
!= INT32_MIN
)) {
211 if (likely(!(((~T2
) ^ T1
^ (-1)) & ((~T2
) ^ T0
) & (1 << 31)))) {
220 void do_subfco (void)
224 if (likely(T0
> T1
)) {
229 if (likely(!(((~T2
) ^ T1
^ (-1)) & ((~T2
) ^ T0
) & (1 << 31)))) {
239 T0
= T1
+ ~T0
+ xer_ca
;
240 if (likely(T0
>= T1
&& (xer_ca
== 0 || T0
!= T1
))) {
247 void do_subfeo (void)
250 T0
= T1
+ ~T0
+ xer_ca
;
251 if (likely(!((~T2
^ T1
^ (-1)) & (~T2
^ T0
) & (1 << 31)))) {
257 if (likely(T0
>= T1
&& (xer_ca
== 0 || T0
!= T1
))) {
264 void do_subfmeo (void)
267 T0
= ~T0
+ xer_ca
- 1;
268 if (likely(!(~T1
& (~T1
^ T0
) & (1 << 31)))) {
274 if (likely(T1
!= -1))
278 void do_subfzeo (void)
282 if (likely(!((~T1
^ (-1)) & ((~T1
) ^ T0
) & (1 << 31)))) {
288 if (likely(T0
>= ~T1
)) {
295 /* shift right arithmetic helper */
300 if (likely(!(T1
& 0x20UL
))) {
301 if (likely(T1
!= 0)) {
302 ret
= (int32_t)T0
>> (T1
& 0x1fUL
);
303 if (likely(ret
>= 0 || ((int32_t)T0
& ((1 << T1
) - 1)) == 0)) {
313 ret
= (-1) * ((uint32_t)T0
>> 31);
314 if (likely(ret
>= 0 || ((uint32_t)T0
& ~0x80000000UL
) == 0)) {
323 /*****************************************************************************/
324 /* Floating point operations helpers */
332 /* XXX: higher bits are not supposed to be significant.
333 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
335 p
.i
= float64_to_int32(FT0
, &env
->fp_status
);
336 p
.i
|= 0xFFF80000ULL
<< 32;
340 void do_fctiwz (void)
347 /* XXX: higher bits are not supposed to be significant.
348 * to make tests easier, return the same as a real PowerPC 750 (aka G3)
350 p
.i
= float64_to_int32_round_to_zero(FT0
, &env
->fp_status
);
351 p
.i
|= 0xFFF80000ULL
<< 32;
355 void do_fnmadd (void)
357 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
358 FT0
= float64_add(FT0
, FT2
, &env
->fp_status
);
359 if (likely(!isnan(FT0
)))
360 FT0
= float64_chs(FT0
);
363 void do_fnmsub (void)
365 FT0
= float64_mul(FT0
, FT1
, &env
->fp_status
);
366 FT0
= float64_sub(FT0
, FT2
, &env
->fp_status
);
367 if (likely(!isnan(FT0
)))
368 FT0
= float64_chs(FT0
);
373 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
383 if (likely(isnormal(FT0
))) {
384 FT0
= (float)(1.0 / FT0
);
387 if (p
.i
== 0x8000000000000000ULL
) {
388 p
.i
= 0xFFF0000000000000ULL
;
389 } else if (p
.i
== 0x0000000000000000ULL
) {
390 p
.i
= 0x7FF0000000000000ULL
;
391 } else if (isnan(FT0
)) {
392 p
.i
= 0x7FF8000000000000ULL
;
393 } else if (FT0
< 0.0) {
394 p
.i
= 0x8000000000000000ULL
;
396 p
.i
= 0x0000000000000000ULL
;
402 void do_frsqrte (void)
409 if (likely(isnormal(FT0
) && FT0
> 0.0)) {
410 FT0
= float64_sqrt(FT0
, &env
->fp_status
);
411 FT0
= float32_div(1.0, FT0
, &env
->fp_status
);
414 if (p
.i
== 0x8000000000000000ULL
) {
415 p
.i
= 0xFFF0000000000000ULL
;
416 } else if (p
.i
== 0x0000000000000000ULL
) {
417 p
.i
= 0x7FF0000000000000ULL
;
418 } else if (isnan(FT0
)) {
419 if (!(p
.i
& 0x0008000000000000ULL
))
420 p
.i
|= 0x000FFFFFFFFFFFFFULL
;
421 } else if (FT0
< 0) {
422 p
.i
= 0x7FF8000000000000ULL
;
424 p
.i
= 0x0000000000000000ULL
;
440 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
441 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
443 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
450 env
->fpscr
[4] |= 0x1;
451 env
->fpscr
[6] |= 0x1;
458 env
->fpscr
[4] &= ~0x1;
459 if (likely(!isnan(FT0
) && !isnan(FT1
))) {
460 if (float64_lt(FT0
, FT1
, &env
->fp_status
)) {
462 } else if (!float64_le(FT0
, FT1
, &env
->fp_status
)) {
469 env
->fpscr
[4] |= 0x1;
470 /* I don't know how to test "quiet" nan... */
471 if (0 /* || ! quiet_nan(...) */) {
472 env
->fpscr
[6] |= 0x1;
473 if (!(env
->fpscr
[1] & 0x8))
474 env
->fpscr
[4] |= 0x8;
476 env
->fpscr
[4] |= 0x8;
484 env
->nip
= env
->spr
[SPR_SRR0
] & ~0x00000003;
485 T0
= env
->spr
[SPR_SRR1
] & ~0xFFFF0000UL
;
486 do_store_msr(env
, T0
);
487 #if defined (DEBUG_OP)
490 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
493 void do_tw (uint32_t cmp
, int flags
)
495 if (!likely(!((Ts0
< (int32_t)cmp
&& (flags
& 0x10)) ||
496 (Ts0
> (int32_t)cmp
&& (flags
& 0x08)) ||
497 (Ts0
== (int32_t)cmp
&& (flags
& 0x04)) ||
498 (T0
< cmp
&& (flags
& 0x02)) ||
499 (T0
> cmp
&& (flags
& 0x01)))))
500 do_raise_exception_err(EXCP_PROGRAM
, EXCP_TRAP
);
503 /* Instruction cache invalidation helper */
507 /* Invalidate one cache line :
508 * PowerPC specification says this is to be treated like a load
509 * (not a fetch) by the MMU. To be sure it will be so,
510 * do the load "by hand".
512 #if defined(TARGET_PPC64)
516 tmp
= ldl_kernel(T0
);
517 T0
&= ~(ICACHE_LINE_SIZE
- 1);
518 tb_invalidate_page_range(T0
, T0
+ ICACHE_LINE_SIZE
);
521 /*****************************************************************************/
522 /* MMU related helpers */
523 /* TLB invalidation helpers */
531 #if !defined(FLUSH_ALL_TLBS)
532 tlb_flush_page(env
, T0
);
538 /*****************************************************************************/
539 /* Softmmu support */
540 #if !defined (CONFIG_USER_ONLY)
542 #define MMUSUFFIX _mmu
543 #define GETPC() (__builtin_return_address(0))
546 #include "softmmu_template.h"
549 #include "softmmu_template.h"
552 #include "softmmu_template.h"
555 #include "softmmu_template.h"
557 /* try to fill the TLB and return an exception if error. If retaddr is
558 NULL, it means that the function was called in C code (i.e. not
559 from generated code or from helper.c) */
560 /* XXX: fix it to restore all registers */
561 void tlb_fill (target_ulong addr
, int is_write
, int is_user
, void *retaddr
)
563 TranslationBlock
*tb
;
565 target_phys_addr_t pc
;
568 /* XXX: hack to restore env in all cases, even if not called from
571 env
= cpu_single_env
;
572 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, is_user
, 1);
573 if (!likely(ret
== 0)) {
574 if (likely(retaddr
)) {
575 /* now we have a real cpu fault */
576 pc
= (target_phys_addr_t
)retaddr
;
579 /* the PC is inside the translated code. It means that we have
580 a virtual CPU fault */
581 cpu_restore_state(tb
, env
, pc
, NULL
);
584 do_raise_exception_err(env
->exception_index
, env
->error_code
);
588 #endif /* !CONFIG_USER_ONLY */