gdbstub: Return appropriate watch message to gdb (Jan Kiszka)
[qemu/mini2440/sniper_sniper_test.git] / target-alpha / op_helper.c
blobcdfcd95c21c249fe96fb4cbcbd8aaa9160355901
1 /*
2 * Alpha emulation cpu micro-operations helpers for qemu.
4 * Copyright (c) 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 "exec.h"
22 #include "host-utils.h"
23 #include "softfloat.h"
24 #include "helper.h"
26 void helper_tb_flush (void)
28 tlb_flush(env, 1);
31 /*****************************************************************************/
32 /* Exceptions processing helpers */
33 void helper_excp (int excp, int error)
35 env->exception_index = excp;
36 env->error_code = error;
37 cpu_loop_exit();
40 uint64_t helper_amask (uint64_t arg)
42 switch (env->implver) {
43 case IMPLVER_2106x:
44 /* EV4, EV45, LCA, LCA45 & EV5 */
45 break;
46 case IMPLVER_21164:
47 case IMPLVER_21264:
48 case IMPLVER_21364:
49 arg &= ~env->amask;
50 break;
52 return arg;
55 uint64_t helper_load_pcc (void)
57 /* XXX: TODO */
58 return 0;
61 uint64_t helper_load_implver (void)
63 return env->implver;
66 uint64_t helper_load_fpcr (void)
68 uint64_t ret = 0;
69 #ifdef CONFIG_SOFTFLOAT
70 ret |= env->fp_status.float_exception_flags << 52;
71 if (env->fp_status.float_exception_flags)
72 ret |= 1ULL << 63;
73 env->ipr[IPR_EXC_SUM] &= ~0x3E:
74 env->ipr[IPR_EXC_SUM] |= env->fp_status.float_exception_flags << 1;
75 #endif
76 switch (env->fp_status.float_rounding_mode) {
77 case float_round_nearest_even:
78 ret |= 2ULL << 58;
79 break;
80 case float_round_down:
81 ret |= 1ULL << 58;
82 break;
83 case float_round_up:
84 ret |= 3ULL << 58;
85 break;
86 case float_round_to_zero:
87 break;
89 return ret;
92 void helper_store_fpcr (uint64_t val)
94 #ifdef CONFIG_SOFTFLOAT
95 set_float_exception_flags((val >> 52) & 0x3F, &FP_STATUS);
96 #endif
97 switch ((val >> 58) & 3) {
98 case 0:
99 set_float_rounding_mode(float_round_to_zero, &FP_STATUS);
100 break;
101 case 1:
102 set_float_rounding_mode(float_round_down, &FP_STATUS);
103 break;
104 case 2:
105 set_float_rounding_mode(float_round_nearest_even, &FP_STATUS);
106 break;
107 case 3:
108 set_float_rounding_mode(float_round_up, &FP_STATUS);
109 break;
113 spinlock_t intr_cpu_lock = SPIN_LOCK_UNLOCKED;
115 uint64_t helper_rs(void)
117 uint64_t tmp;
119 spin_lock(&intr_cpu_lock);
120 tmp = env->intr_flag;
121 env->intr_flag = 1;
122 spin_unlock(&intr_cpu_lock);
124 return tmp;
127 uint64_t helper_rc(void)
129 uint64_t tmp;
131 spin_lock(&intr_cpu_lock);
132 tmp = env->intr_flag;
133 env->intr_flag = 0;
134 spin_unlock(&intr_cpu_lock);
136 return tmp;
139 uint64_t helper_addqv (uint64_t op1, uint64_t op2)
141 uint64_t tmp = op1;
142 op1 += op2;
143 if (unlikely((tmp ^ op2 ^ (-1ULL)) & (tmp ^ op1) & (1ULL << 63))) {
144 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
146 return op1;
149 uint64_t helper_addlv (uint64_t op1, uint64_t op2)
151 uint64_t tmp = op1;
152 op1 = (uint32_t)(op1 + op2);
153 if (unlikely((tmp ^ op2 ^ (-1UL)) & (tmp ^ op1) & (1UL << 31))) {
154 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
156 return op1;
159 uint64_t helper_subqv (uint64_t op1, uint64_t op2)
161 uint64_t tmp = op1;
162 op1 -= op2;
163 if (unlikely(((~tmp) ^ op1 ^ (-1ULL)) & ((~tmp) ^ op2) & (1ULL << 63))) {
164 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
166 return op1;
169 uint64_t helper_sublv (uint64_t op1, uint64_t op2)
171 uint64_t tmp = op1;
172 op1 = (uint32_t)(op1 - op2);
173 if (unlikely(((~tmp) ^ op1 ^ (-1UL)) & ((~tmp) ^ op2) & (1UL << 31))) {
174 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
176 return op1;
179 uint64_t helper_mullv (uint64_t op1, uint64_t op2)
181 int64_t res = (int64_t)op1 * (int64_t)op2;
183 if (unlikely((int32_t)res != res)) {
184 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
186 return (int64_t)((int32_t)res);
189 uint64_t helper_mulqv (uint64_t op1, uint64_t op2)
191 uint64_t tl, th;
193 muls64(&tl, &th, op1, op2);
194 /* If th != 0 && th != -1, then we had an overflow */
195 if (unlikely((th + 1) > 1)) {
196 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
198 return tl;
201 uint64_t helper_umulh (uint64_t op1, uint64_t op2)
203 uint64_t tl, th;
205 mulu64(&tl, &th, op1, op2);
206 return th;
209 uint64_t helper_ctpop (uint64_t arg)
211 return ctpop64(arg);
214 uint64_t helper_ctlz (uint64_t arg)
216 return clz64(arg);
219 uint64_t helper_cttz (uint64_t arg)
221 return ctz64(arg);
224 static always_inline uint64_t byte_zap (uint64_t op, uint8_t mskb)
226 uint64_t mask;
228 mask = 0;
229 mask |= ((mskb >> 0) & 1) * 0x00000000000000FFULL;
230 mask |= ((mskb >> 1) & 1) * 0x000000000000FF00ULL;
231 mask |= ((mskb >> 2) & 1) * 0x0000000000FF0000ULL;
232 mask |= ((mskb >> 3) & 1) * 0x00000000FF000000ULL;
233 mask |= ((mskb >> 4) & 1) * 0x000000FF00000000ULL;
234 mask |= ((mskb >> 5) & 1) * 0x0000FF0000000000ULL;
235 mask |= ((mskb >> 6) & 1) * 0x00FF000000000000ULL;
236 mask |= ((mskb >> 7) & 1) * 0xFF00000000000000ULL;
238 return op & ~mask;
241 uint64_t helper_mskbl(uint64_t val, uint64_t mask)
243 return byte_zap(val, 0x01 << (mask & 7));
246 uint64_t helper_insbl(uint64_t val, uint64_t mask)
248 val <<= (mask & 7) * 8;
249 return byte_zap(val, ~(0x01 << (mask & 7)));
252 uint64_t helper_mskwl(uint64_t val, uint64_t mask)
254 return byte_zap(val, 0x03 << (mask & 7));
257 uint64_t helper_inswl(uint64_t val, uint64_t mask)
259 val <<= (mask & 7) * 8;
260 return byte_zap(val, ~(0x03 << (mask & 7)));
263 uint64_t helper_mskll(uint64_t val, uint64_t mask)
265 return byte_zap(val, 0x0F << (mask & 7));
268 uint64_t helper_insll(uint64_t val, uint64_t mask)
270 val <<= (mask & 7) * 8;
271 return byte_zap(val, ~(0x0F << (mask & 7)));
274 uint64_t helper_zap(uint64_t val, uint64_t mask)
276 return byte_zap(val, mask);
279 uint64_t helper_zapnot(uint64_t val, uint64_t mask)
281 return byte_zap(val, ~mask);
284 uint64_t helper_mskql(uint64_t val, uint64_t mask)
286 return byte_zap(val, 0xFF << (mask & 7));
289 uint64_t helper_insql(uint64_t val, uint64_t mask)
291 val <<= (mask & 7) * 8;
292 return byte_zap(val, ~(0xFF << (mask & 7)));
295 uint64_t helper_mskwh(uint64_t val, uint64_t mask)
297 return byte_zap(val, (0x03 << (mask & 7)) >> 8);
300 uint64_t helper_inswh(uint64_t val, uint64_t mask)
302 val >>= 64 - ((mask & 7) * 8);
303 return byte_zap(val, ~((0x03 << (mask & 7)) >> 8));
306 uint64_t helper_msklh(uint64_t val, uint64_t mask)
308 return byte_zap(val, (0x0F << (mask & 7)) >> 8);
311 uint64_t helper_inslh(uint64_t val, uint64_t mask)
313 val >>= 64 - ((mask & 7) * 8);
314 return byte_zap(val, ~((0x0F << (mask & 7)) >> 8));
317 uint64_t helper_mskqh(uint64_t val, uint64_t mask)
319 return byte_zap(val, (0xFF << (mask & 7)) >> 8);
322 uint64_t helper_insqh(uint64_t val, uint64_t mask)
324 val >>= 64 - ((mask & 7) * 8);
325 return byte_zap(val, ~((0xFF << (mask & 7)) >> 8));
328 uint64_t helper_cmpbge (uint64_t op1, uint64_t op2)
330 uint8_t opa, opb, res;
331 int i;
333 res = 0;
334 for (i = 0; i < 8; i++) {
335 opa = op1 >> (i * 8);
336 opb = op2 >> (i * 8);
337 if (opa >= opb)
338 res |= 1 << i;
340 return res;
343 /* Floating point helpers */
345 /* F floating (VAX) */
346 static always_inline uint64_t float32_to_f (float32 fa)
348 uint32_t a;
349 uint64_t r, exp, mant, sig;
351 a = *(uint32_t*)(&fa);
352 sig = ((uint64_t)a & 0x80000000) << 32;
353 exp = (a >> 23) & 0xff;
354 mant = ((uint64_t)a & 0x007fffff) << 29;
356 if (exp == 255) {
357 /* NaN or infinity */
358 r = 1; /* VAX dirty zero */
359 } else if (exp == 0) {
360 if (mant == 0) {
361 /* Zero */
362 r = 0;
363 } else {
364 /* Denormalized */
365 r = sig | ((exp + 1) << 52) | mant;
367 } else {
368 if (exp >= 253) {
369 /* Overflow */
370 r = 1; /* VAX dirty zero */
371 } else {
372 r = sig | ((exp + 2) << 52);
376 return r;
379 static always_inline float32 f_to_float32 (uint64_t a)
381 uint32_t r, exp, mant_sig;
383 exp = ((a >> 55) & 0x80) | ((a >> 52) & 0x7f);
384 mant_sig = ((a >> 32) & 0x80000000) | ((a >> 29) & 0x007fffff);
386 if (unlikely(!exp && mant_sig)) {
387 /* Reserved operands / Dirty zero */
388 helper_excp(EXCP_OPCDEC, 0);
391 if (exp < 3) {
392 /* Underflow */
393 r = 0;
394 } else {
395 r = ((exp - 2) << 23) | mant_sig;
398 return *(float32*)(&a);
401 uint32_t helper_f_to_memory (uint64_t a)
403 uint32_t r;
404 r = (a & 0x00001fffe0000000ull) >> 13;
405 r |= (a & 0x07ffe00000000000ull) >> 45;
406 r |= (a & 0xc000000000000000ull) >> 48;
407 return r;
410 uint64_t helper_memory_to_f (uint32_t a)
412 uint64_t r;
413 r = ((uint64_t)(a & 0x0000c000)) << 48;
414 r |= ((uint64_t)(a & 0x003fffff)) << 45;
415 r |= ((uint64_t)(a & 0xffff0000)) << 13;
416 if (!(a & 0x00004000))
417 r |= 0x7ll << 59;
418 return r;
421 uint64_t helper_addf (uint64_t a, uint64_t b)
423 float32 fa, fb, fr;
425 fa = f_to_float32(a);
426 fb = f_to_float32(b);
427 fr = float32_add(fa, fb, &FP_STATUS);
428 return float32_to_f(fr);
431 uint64_t helper_subf (uint64_t a, uint64_t b)
433 float32 fa, fb, fr;
435 fa = f_to_float32(a);
436 fb = f_to_float32(b);
437 fr = float32_sub(fa, fb, &FP_STATUS);
438 return float32_to_f(fr);
441 uint64_t helper_mulf (uint64_t a, uint64_t b)
443 float32 fa, fb, fr;
445 fa = f_to_float32(a);
446 fb = f_to_float32(b);
447 fr = float32_mul(fa, fb, &FP_STATUS);
448 return float32_to_f(fr);
451 uint64_t helper_divf (uint64_t a, uint64_t b)
453 float32 fa, fb, fr;
455 fa = f_to_float32(a);
456 fb = f_to_float32(b);
457 fr = float32_div(fa, fb, &FP_STATUS);
458 return float32_to_f(fr);
461 uint64_t helper_sqrtf (uint64_t t)
463 float32 ft, fr;
465 ft = f_to_float32(t);
466 fr = float32_sqrt(ft, &FP_STATUS);
467 return float32_to_f(fr);
471 /* G floating (VAX) */
472 static always_inline uint64_t float64_to_g (float64 fa)
474 uint64_t a, r, exp, mant, sig;
476 a = *(uint64_t*)(&fa);
477 sig = a & 0x8000000000000000ull;
478 exp = (a >> 52) & 0x7ff;
479 mant = a & 0x000fffffffffffffull;
481 if (exp == 2047) {
482 /* NaN or infinity */
483 r = 1; /* VAX dirty zero */
484 } else if (exp == 0) {
485 if (mant == 0) {
486 /* Zero */
487 r = 0;
488 } else {
489 /* Denormalized */
490 r = sig | ((exp + 1) << 52) | mant;
492 } else {
493 if (exp >= 2045) {
494 /* Overflow */
495 r = 1; /* VAX dirty zero */
496 } else {
497 r = sig | ((exp + 2) << 52);
501 return r;
504 static always_inline float64 g_to_float64 (uint64_t a)
506 uint64_t r, exp, mant_sig;
508 exp = (a >> 52) & 0x7ff;
509 mant_sig = a & 0x800fffffffffffffull;
511 if (!exp && mant_sig) {
512 /* Reserved operands / Dirty zero */
513 helper_excp(EXCP_OPCDEC, 0);
516 if (exp < 3) {
517 /* Underflow */
518 r = 0;
519 } else {
520 r = ((exp - 2) << 52) | mant_sig;
523 return *(float64*)(&a);
526 uint64_t helper_g_to_memory (uint64_t a)
528 uint64_t r;
529 r = (a & 0x000000000000ffffull) << 48;
530 r |= (a & 0x00000000ffff0000ull) << 16;
531 r |= (a & 0x0000ffff00000000ull) >> 16;
532 r |= (a & 0xffff000000000000ull) >> 48;
533 return r;
536 uint64_t helper_memory_to_g (uint64_t a)
538 uint64_t r;
539 r = (a & 0x000000000000ffffull) << 48;
540 r |= (a & 0x00000000ffff0000ull) << 16;
541 r |= (a & 0x0000ffff00000000ull) >> 16;
542 r |= (a & 0xffff000000000000ull) >> 48;
543 return r;
546 uint64_t helper_addg (uint64_t a, uint64_t b)
548 float64 fa, fb, fr;
550 fa = g_to_float64(a);
551 fb = g_to_float64(b);
552 fr = float64_add(fa, fb, &FP_STATUS);
553 return float64_to_g(fr);
556 uint64_t helper_subg (uint64_t a, uint64_t b)
558 float64 fa, fb, fr;
560 fa = g_to_float64(a);
561 fb = g_to_float64(b);
562 fr = float64_sub(fa, fb, &FP_STATUS);
563 return float64_to_g(fr);
566 uint64_t helper_mulg (uint64_t a, uint64_t b)
568 float64 fa, fb, fr;
570 fa = g_to_float64(a);
571 fb = g_to_float64(b);
572 fr = float64_mul(fa, fb, &FP_STATUS);
573 return float64_to_g(fr);
576 uint64_t helper_divg (uint64_t a, uint64_t b)
578 float64 fa, fb, fr;
580 fa = g_to_float64(a);
581 fb = g_to_float64(b);
582 fr = float64_div(fa, fb, &FP_STATUS);
583 return float64_to_g(fr);
586 uint64_t helper_sqrtg (uint64_t a)
588 float64 fa, fr;
590 fa = g_to_float64(a);
591 fr = float64_sqrt(fa, &FP_STATUS);
592 return float64_to_g(fr);
596 /* S floating (single) */
597 static always_inline uint64_t float32_to_s (float32 fa)
599 uint32_t a;
600 uint64_t r;
602 a = *(uint32_t*)(&fa);
604 r = (((uint64_t)(a & 0xc0000000)) << 32) | (((uint64_t)(a & 0x3fffffff)) << 29);
605 if (((a & 0x7f800000) != 0x7f800000) && (!(a & 0x40000000)))
606 r |= 0x7ll << 59;
607 return r;
610 static always_inline float32 s_to_float32 (uint64_t a)
612 uint32_t r = ((a >> 32) & 0xc0000000) | ((a >> 29) & 0x3fffffff);
613 return *(float32*)(&r);
616 uint32_t helper_s_to_memory (uint64_t a)
618 /* Memory format is the same as float32 */
619 float32 fa = s_to_float32(a);
620 return *(uint32_t*)(&fa);
623 uint64_t helper_memory_to_s (uint32_t a)
625 /* Memory format is the same as float32 */
626 return float32_to_s(*(float32*)(&a));
629 uint64_t helper_adds (uint64_t a, uint64_t b)
631 float32 fa, fb, fr;
633 fa = s_to_float32(a);
634 fb = s_to_float32(b);
635 fr = float32_add(fa, fb, &FP_STATUS);
636 return float32_to_s(fr);
639 uint64_t helper_subs (uint64_t a, uint64_t b)
641 float32 fa, fb, fr;
643 fa = s_to_float32(a);
644 fb = s_to_float32(b);
645 fr = float32_sub(fa, fb, &FP_STATUS);
646 return float32_to_s(fr);
649 uint64_t helper_muls (uint64_t a, uint64_t b)
651 float32 fa, fb, fr;
653 fa = s_to_float32(a);
654 fb = s_to_float32(b);
655 fr = float32_mul(fa, fb, &FP_STATUS);
656 return float32_to_s(fr);
659 uint64_t helper_divs (uint64_t a, uint64_t b)
661 float32 fa, fb, fr;
663 fa = s_to_float32(a);
664 fb = s_to_float32(b);
665 fr = float32_div(fa, fb, &FP_STATUS);
666 return float32_to_s(fr);
669 uint64_t helper_sqrts (uint64_t a)
671 float32 fa, fr;
673 fa = s_to_float32(a);
674 fr = float32_sqrt(fa, &FP_STATUS);
675 return float32_to_s(fr);
679 /* T floating (double) */
680 static always_inline float64 t_to_float64 (uint64_t a)
682 /* Memory format is the same as float64 */
683 return *(float64*)(&a);
686 static always_inline uint64_t float64_to_t (float64 fa)
688 /* Memory format is the same as float64 */
689 return *(uint64*)(&fa);
692 uint64_t helper_addt (uint64_t a, uint64_t b)
694 float64 fa, fb, fr;
696 fa = t_to_float64(a);
697 fb = t_to_float64(b);
698 fr = float64_add(fa, fb, &FP_STATUS);
699 return float64_to_t(fr);
702 uint64_t helper_subt (uint64_t a, uint64_t b)
704 float64 fa, fb, fr;
706 fa = t_to_float64(a);
707 fb = t_to_float64(b);
708 fr = float64_sub(fa, fb, &FP_STATUS);
709 return float64_to_t(fr);
712 uint64_t helper_mult (uint64_t a, uint64_t b)
714 float64 fa, fb, fr;
716 fa = t_to_float64(a);
717 fb = t_to_float64(b);
718 fr = float64_mul(fa, fb, &FP_STATUS);
719 return float64_to_t(fr);
722 uint64_t helper_divt (uint64_t a, uint64_t b)
724 float64 fa, fb, fr;
726 fa = t_to_float64(a);
727 fb = t_to_float64(b);
728 fr = float64_div(fa, fb, &FP_STATUS);
729 return float64_to_t(fr);
732 uint64_t helper_sqrtt (uint64_t a)
734 float64 fa, fr;
736 fa = t_to_float64(a);
737 fr = float64_sqrt(fa, &FP_STATUS);
738 return float64_to_t(fr);
742 /* Sign copy */
743 uint64_t helper_cpys(uint64_t a, uint64_t b)
745 return (a & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
748 uint64_t helper_cpysn(uint64_t a, uint64_t b)
750 return ((~a) & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
753 uint64_t helper_cpyse(uint64_t a, uint64_t b)
755 return (a & 0xFFF0000000000000ULL) | (b & ~0xFFF0000000000000ULL);
759 /* Comparisons */
760 uint64_t helper_cmptun (uint64_t a, uint64_t b)
762 float64 fa, fb;
764 fa = t_to_float64(a);
765 fb = t_to_float64(b);
767 if (float64_is_nan(fa) || float64_is_nan(fb))
768 return 0x4000000000000000ULL;
769 else
770 return 0;
773 uint64_t helper_cmpteq(uint64_t a, uint64_t b)
775 float64 fa, fb;
777 fa = t_to_float64(a);
778 fb = t_to_float64(b);
780 if (float64_eq(fa, fb, &FP_STATUS))
781 return 0x4000000000000000ULL;
782 else
783 return 0;
786 uint64_t helper_cmptle(uint64_t a, uint64_t b)
788 float64 fa, fb;
790 fa = t_to_float64(a);
791 fb = t_to_float64(b);
793 if (float64_le(fa, fb, &FP_STATUS))
794 return 0x4000000000000000ULL;
795 else
796 return 0;
799 uint64_t helper_cmptlt(uint64_t a, uint64_t b)
801 float64 fa, fb;
803 fa = t_to_float64(a);
804 fb = t_to_float64(b);
806 if (float64_lt(fa, fb, &FP_STATUS))
807 return 0x4000000000000000ULL;
808 else
809 return 0;
812 uint64_t helper_cmpgeq(uint64_t a, uint64_t b)
814 float64 fa, fb;
816 fa = g_to_float64(a);
817 fb = g_to_float64(b);
819 if (float64_eq(fa, fb, &FP_STATUS))
820 return 0x4000000000000000ULL;
821 else
822 return 0;
825 uint64_t helper_cmpgle(uint64_t a, uint64_t b)
827 float64 fa, fb;
829 fa = g_to_float64(a);
830 fb = g_to_float64(b);
832 if (float64_le(fa, fb, &FP_STATUS))
833 return 0x4000000000000000ULL;
834 else
835 return 0;
838 uint64_t helper_cmpglt(uint64_t a, uint64_t b)
840 float64 fa, fb;
842 fa = g_to_float64(a);
843 fb = g_to_float64(b);
845 if (float64_lt(fa, fb, &FP_STATUS))
846 return 0x4000000000000000ULL;
847 else
848 return 0;
851 uint64_t helper_cmpfeq (uint64_t a)
853 return !(a & 0x7FFFFFFFFFFFFFFFULL);
856 uint64_t helper_cmpfne (uint64_t a)
858 return (a & 0x7FFFFFFFFFFFFFFFULL);
861 uint64_t helper_cmpflt (uint64_t a)
863 return (a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
866 uint64_t helper_cmpfle (uint64_t a)
868 return (a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
871 uint64_t helper_cmpfgt (uint64_t a)
873 return !(a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
876 uint64_t helper_cmpfge (uint64_t a)
878 return !(a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
882 /* Floating point format conversion */
883 uint64_t helper_cvtts (uint64_t a)
885 float64 fa;
886 float32 fr;
888 fa = t_to_float64(a);
889 fr = float64_to_float32(fa, &FP_STATUS);
890 return float32_to_s(fr);
893 uint64_t helper_cvtst (uint64_t a)
895 float32 fa;
896 float64 fr;
898 fa = s_to_float32(a);
899 fr = float32_to_float64(fa, &FP_STATUS);
900 return float64_to_t(fr);
903 uint64_t helper_cvtqs (uint64_t a)
905 float32 fr = int64_to_float32(a, &FP_STATUS);
906 return float32_to_s(fr);
909 uint64_t helper_cvttq (uint64_t a)
911 float64 fa = t_to_float64(a);
912 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
915 uint64_t helper_cvtqt (uint64_t a)
917 float64 fr = int64_to_float64(a, &FP_STATUS);
918 return float64_to_t(fr);
921 uint64_t helper_cvtqf (uint64_t a)
923 float32 fr = int64_to_float32(a, &FP_STATUS);
924 return float32_to_f(fr);
927 uint64_t helper_cvtgf (uint64_t a)
929 float64 fa;
930 float32 fr;
932 fa = g_to_float64(a);
933 fr = float64_to_float32(fa, &FP_STATUS);
934 return float32_to_f(fr);
937 uint64_t helper_cvtgq (uint64_t a)
939 float64 fa = g_to_float64(a);
940 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
943 uint64_t helper_cvtqg (uint64_t a)
945 float64 fr;
946 fr = int64_to_float64(a, &FP_STATUS);
947 return float64_to_g(fr);
950 uint64_t helper_cvtlq (uint64_t a)
952 return (int64_t)((int32_t)((a >> 32) | ((a >> 29) & 0x3FFFFFFF)));
955 static always_inline uint64_t __helper_cvtql (uint64_t a, int s, int v)
957 uint64_t r;
959 r = ((uint64_t)(a & 0xC0000000)) << 32;
960 r |= ((uint64_t)(a & 0x7FFFFFFF)) << 29;
962 if (v && (int64_t)((int32_t)r) != (int64_t)r) {
963 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
965 if (s) {
966 /* TODO */
968 return r;
971 uint64_t helper_cvtql (uint64_t a)
973 return __helper_cvtql(a, 0, 0);
976 uint64_t helper_cvtqlv (uint64_t a)
978 return __helper_cvtql(a, 0, 1);
981 uint64_t helper_cvtqlsv (uint64_t a)
983 return __helper_cvtql(a, 1, 1);
986 /* PALcode support special instructions */
987 #if !defined (CONFIG_USER_ONLY)
988 void helper_hw_rei (void)
990 env->pc = env->ipr[IPR_EXC_ADDR] & ~3;
991 env->ipr[IPR_EXC_ADDR] = env->ipr[IPR_EXC_ADDR] & 1;
992 /* XXX: re-enable interrupts and memory mapping */
995 void helper_hw_ret (uint64_t a)
997 env->pc = a & ~3;
998 env->ipr[IPR_EXC_ADDR] = a & 1;
999 /* XXX: re-enable interrupts and memory mapping */
1002 uint64_t helper_mfpr (int iprn, uint64_t val)
1004 uint64_t tmp;
1006 if (cpu_alpha_mfpr(env, iprn, &tmp) == 0)
1007 val = tmp;
1009 return val;
1012 void helper_mtpr (int iprn, uint64_t val)
1014 cpu_alpha_mtpr(env, iprn, val, NULL);
1017 void helper_set_alt_mode (void)
1019 env->saved_mode = env->ps & 0xC;
1020 env->ps = (env->ps & ~0xC) | (env->ipr[IPR_ALT_MODE] & 0xC);
1023 void helper_restore_mode (void)
1025 env->ps = (env->ps & ~0xC) | env->saved_mode;
1028 #endif
1030 /*****************************************************************************/
1031 /* Softmmu support */
1032 #if !defined (CONFIG_USER_ONLY)
1034 /* XXX: the two following helpers are pure hacks.
1035 * Hopefully, we emulate the PALcode, then we should never see
1036 * HW_LD / HW_ST instructions.
1038 uint64_t helper_ld_virt_to_phys (uint64_t virtaddr)
1040 uint64_t tlb_addr, physaddr;
1041 int index, mmu_idx;
1042 void *retaddr;
1044 mmu_idx = cpu_mmu_index(env);
1045 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1046 redo:
1047 tlb_addr = env->tlb_table[mmu_idx][index].addr_read;
1048 if ((virtaddr & TARGET_PAGE_MASK) ==
1049 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1050 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1051 } else {
1052 /* the page is not in the TLB : fill it */
1053 retaddr = GETPC();
1054 tlb_fill(virtaddr, 0, mmu_idx, retaddr);
1055 goto redo;
1057 return physaddr;
1060 uint64_t helper_st_virt_to_phys (uint64_t virtaddr)
1062 uint64_t tlb_addr, physaddr;
1063 int index, mmu_idx;
1064 void *retaddr;
1066 mmu_idx = cpu_mmu_index(env);
1067 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1068 redo:
1069 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
1070 if ((virtaddr & TARGET_PAGE_MASK) ==
1071 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1072 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1073 } else {
1074 /* the page is not in the TLB : fill it */
1075 retaddr = GETPC();
1076 tlb_fill(virtaddr, 1, mmu_idx, retaddr);
1077 goto redo;
1079 return physaddr;
1082 void helper_ldl_raw(uint64_t t0, uint64_t t1)
1084 ldl_raw(t1, t0);
1087 void helper_ldq_raw(uint64_t t0, uint64_t t1)
1089 ldq_raw(t1, t0);
1092 void helper_ldl_l_raw(uint64_t t0, uint64_t t1)
1094 env->lock = t1;
1095 ldl_raw(t1, t0);
1098 void helper_ldq_l_raw(uint64_t t0, uint64_t t1)
1100 env->lock = t1;
1101 ldl_raw(t1, t0);
1104 void helper_ldl_kernel(uint64_t t0, uint64_t t1)
1106 ldl_kernel(t1, t0);
1109 void helper_ldq_kernel(uint64_t t0, uint64_t t1)
1111 ldq_kernel(t1, t0);
1114 void helper_ldl_data(uint64_t t0, uint64_t t1)
1116 ldl_data(t1, t0);
1119 void helper_ldq_data(uint64_t t0, uint64_t t1)
1121 ldq_data(t1, t0);
1124 void helper_stl_raw(uint64_t t0, uint64_t t1)
1126 stl_raw(t1, t0);
1129 void helper_stq_raw(uint64_t t0, uint64_t t1)
1131 stq_raw(t1, t0);
1134 uint64_t helper_stl_c_raw(uint64_t t0, uint64_t t1)
1136 uint64_t ret;
1138 if (t1 == env->lock) {
1139 stl_raw(t1, t0);
1140 ret = 0;
1141 } else
1142 ret = 1;
1144 env->lock = 1;
1146 return ret;
1149 uint64_t helper_stq_c_raw(uint64_t t0, uint64_t t1)
1151 uint64_t ret;
1153 if (t1 == env->lock) {
1154 stq_raw(t1, t0);
1155 ret = 0;
1156 } else
1157 ret = 1;
1159 env->lock = 1;
1161 return ret;
1164 #define MMUSUFFIX _mmu
1166 #define SHIFT 0
1167 #include "softmmu_template.h"
1169 #define SHIFT 1
1170 #include "softmmu_template.h"
1172 #define SHIFT 2
1173 #include "softmmu_template.h"
1175 #define SHIFT 3
1176 #include "softmmu_template.h"
1178 /* try to fill the TLB and return an exception if error. If retaddr is
1179 NULL, it means that the function was called in C code (i.e. not
1180 from generated code or from helper.c) */
1181 /* XXX: fix it to restore all registers */
1182 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1184 TranslationBlock *tb;
1185 CPUState *saved_env;
1186 unsigned long pc;
1187 int ret;
1189 /* XXX: hack to restore env in all cases, even if not called from
1190 generated code */
1191 saved_env = env;
1192 env = cpu_single_env;
1193 ret = cpu_alpha_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1194 if (!likely(ret == 0)) {
1195 if (likely(retaddr)) {
1196 /* now we have a real cpu fault */
1197 pc = (unsigned long)retaddr;
1198 tb = tb_find_pc(pc);
1199 if (likely(tb)) {
1200 /* the PC is inside the translated code. It means that we have
1201 a virtual CPU fault */
1202 cpu_restore_state(tb, env, pc, NULL);
1205 /* Exception index and error code are already set */
1206 cpu_loop_exit();
1208 env = saved_env;
1211 #endif