]> git.proxmox.com Git - qemu.git/blob - target-alpha/op_helper.c
target-ppc: fix flags computation for tcg_gen_qemu_st
[qemu.git] / target-alpha / op_helper.c
1 /*
2 * Alpha emulation cpu micro-operations helpers for qemu.
3 *
4 * Copyright (c) 2007 Jocelyn Mayer
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "exec.h"
22 #include "host-utils.h"
23 #include "softfloat.h"
24
25 void helper_tb_flush (void)
26 {
27 tlb_flush(env, 1);
28 }
29
30 /*****************************************************************************/
31 /* Exceptions processing helpers */
32 void helper_excp (int excp, int error)
33 {
34 env->exception_index = excp;
35 env->error_code = error;
36 cpu_loop_exit();
37 }
38
39 uint64_t helper_amask (uint64_t arg)
40 {
41 switch (env->implver) {
42 case IMPLVER_2106x:
43 /* EV4, EV45, LCA, LCA45 & EV5 */
44 break;
45 case IMPLVER_21164:
46 case IMPLVER_21264:
47 case IMPLVER_21364:
48 arg &= ~env->amask;
49 break;
50 }
51 return arg;
52 }
53
54 uint64_t helper_load_pcc (void)
55 {
56 /* XXX: TODO */
57 return 0;
58 }
59
60 uint64_t helper_load_implver (void)
61 {
62 return env->implver;
63 }
64
65 uint64_t helper_load_fpcr (void)
66 {
67 uint64_t ret = 0;
68 #ifdef CONFIG_SOFTFLOAT
69 ret |= env->fp_status.float_exception_flags << 52;
70 if (env->fp_status.float_exception_flags)
71 ret |= 1ULL << 63;
72 env->ipr[IPR_EXC_SUM] &= ~0x3E:
73 env->ipr[IPR_EXC_SUM] |= env->fp_status.float_exception_flags << 1;
74 #endif
75 switch (env->fp_status.float_rounding_mode) {
76 case float_round_nearest_even:
77 ret |= 2ULL << 58;
78 break;
79 case float_round_down:
80 ret |= 1ULL << 58;
81 break;
82 case float_round_up:
83 ret |= 3ULL << 58;
84 break;
85 case float_round_to_zero:
86 break;
87 }
88 return ret;
89 }
90
91 void helper_store_fpcr (uint64_t val)
92 {
93 #ifdef CONFIG_SOFTFLOAT
94 set_float_exception_flags((val >> 52) & 0x3F, &FP_STATUS);
95 #endif
96 switch ((val >> 58) & 3) {
97 case 0:
98 set_float_rounding_mode(float_round_to_zero, &FP_STATUS);
99 break;
100 case 1:
101 set_float_rounding_mode(float_round_down, &FP_STATUS);
102 break;
103 case 2:
104 set_float_rounding_mode(float_round_nearest_even, &FP_STATUS);
105 break;
106 case 3:
107 set_float_rounding_mode(float_round_up, &FP_STATUS);
108 break;
109 }
110 }
111
112 spinlock_t intr_cpu_lock = SPIN_LOCK_UNLOCKED;
113
114 uint64_t helper_rs(void)
115 {
116 uint64_t tmp;
117
118 spin_lock(&intr_cpu_lock);
119 tmp = env->intr_flag;
120 env->intr_flag = 1;
121 spin_unlock(&intr_cpu_lock);
122
123 return tmp;
124 }
125
126 uint64_t helper_rc(void)
127 {
128 uint64_t tmp;
129
130 spin_lock(&intr_cpu_lock);
131 tmp = env->intr_flag;
132 env->intr_flag = 0;
133 spin_unlock(&intr_cpu_lock);
134
135 return tmp;
136 }
137
138 uint64_t helper_addqv (uint64_t op1, uint64_t op2)
139 {
140 uint64_t tmp = op1;
141 op1 += op2;
142 if (unlikely((tmp ^ op2 ^ (-1ULL)) & (tmp ^ op1) & (1ULL << 63))) {
143 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
144 }
145 return op1;
146 }
147
148 uint64_t helper_addlv (uint64_t op1, uint64_t op2)
149 {
150 uint64_t tmp = op1;
151 op1 = (uint32_t)(op1 + op2);
152 if (unlikely((tmp ^ op2 ^ (-1UL)) & (tmp ^ op1) & (1UL << 31))) {
153 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
154 }
155 return op1;
156 }
157
158 uint64_t helper_subqv (uint64_t op1, uint64_t op2)
159 {
160 uint64_t tmp = op1;
161 op1 -= op2;
162 if (unlikely(((~tmp) ^ op1 ^ (-1ULL)) & ((~tmp) ^ op2) & (1ULL << 63))) {
163 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
164 }
165 return op1;
166 }
167
168 uint64_t helper_sublv (uint64_t op1, uint64_t op2)
169 {
170 uint64_t tmp = op1;
171 op1 = (uint32_t)(op1 - op2);
172 if (unlikely(((~tmp) ^ op1 ^ (-1UL)) & ((~tmp) ^ op2) & (1UL << 31))) {
173 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
174 }
175 return op1;
176 }
177
178 uint64_t helper_mullv (uint64_t op1, uint64_t op2)
179 {
180 int64_t res = (int64_t)op1 * (int64_t)op2;
181
182 if (unlikely((int32_t)res != res)) {
183 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
184 }
185 return (int64_t)((int32_t)res);
186 }
187
188 uint64_t helper_mulqv (uint64_t op1, uint64_t op2)
189 {
190 uint64_t tl, th;
191
192 muls64(&tl, &th, op1, op2);
193 /* If th != 0 && th != -1, then we had an overflow */
194 if (unlikely((th + 1) > 1)) {
195 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
196 }
197 return tl;
198 }
199
200 uint64_t helper_umulh (uint64_t op1, uint64_t op2)
201 {
202 uint64_t tl, th;
203
204 mulu64(&tl, &th, op1, op2);
205 return th;
206 }
207
208 uint64_t helper_ctpop (uint64_t arg)
209 {
210 return ctpop64(arg);
211 }
212
213 uint64_t helper_ctlz (uint64_t arg)
214 {
215 return clz64(arg);
216 }
217
218 uint64_t helper_cttz (uint64_t arg)
219 {
220 return ctz64(arg);
221 }
222
223 static always_inline uint64_t byte_zap (uint64_t op, uint8_t mskb)
224 {
225 uint64_t mask;
226
227 mask = 0;
228 mask |= ((mskb >> 0) & 1) * 0x00000000000000FFULL;
229 mask |= ((mskb >> 1) & 1) * 0x000000000000FF00ULL;
230 mask |= ((mskb >> 2) & 1) * 0x0000000000FF0000ULL;
231 mask |= ((mskb >> 3) & 1) * 0x00000000FF000000ULL;
232 mask |= ((mskb >> 4) & 1) * 0x000000FF00000000ULL;
233 mask |= ((mskb >> 5) & 1) * 0x0000FF0000000000ULL;
234 mask |= ((mskb >> 6) & 1) * 0x00FF000000000000ULL;
235 mask |= ((mskb >> 7) & 1) * 0xFF00000000000000ULL;
236
237 return op & ~mask;
238 }
239
240 uint64_t helper_mskbl(uint64_t val, uint64_t mask)
241 {
242 return byte_zap(val, 0x01 << (mask & 7));
243 }
244
245 uint64_t helper_insbl(uint64_t val, uint64_t mask)
246 {
247 val <<= (mask & 7) * 8;
248 return byte_zap(val, ~(0x01 << (mask & 7)));
249 }
250
251 uint64_t helper_mskwl(uint64_t val, uint64_t mask)
252 {
253 return byte_zap(val, 0x03 << (mask & 7));
254 }
255
256 uint64_t helper_inswl(uint64_t val, uint64_t mask)
257 {
258 val <<= (mask & 7) * 8;
259 return byte_zap(val, ~(0x03 << (mask & 7)));
260 }
261
262 uint64_t helper_mskll(uint64_t val, uint64_t mask)
263 {
264 return byte_zap(val, 0x0F << (mask & 7));
265 }
266
267 uint64_t helper_insll(uint64_t val, uint64_t mask)
268 {
269 val <<= (mask & 7) * 8;
270 return byte_zap(val, ~(0x0F << (mask & 7)));
271 }
272
273 uint64_t helper_zap(uint64_t val, uint64_t mask)
274 {
275 return byte_zap(val, mask);
276 }
277
278 uint64_t helper_zapnot(uint64_t val, uint64_t mask)
279 {
280 return byte_zap(val, ~mask);
281 }
282
283 uint64_t helper_mskql(uint64_t val, uint64_t mask)
284 {
285 return byte_zap(val, 0xFF << (mask & 7));
286 }
287
288 uint64_t helper_insql(uint64_t val, uint64_t mask)
289 {
290 val <<= (mask & 7) * 8;
291 return byte_zap(val, ~(0xFF << (mask & 7)));
292 }
293
294 uint64_t helper_mskwh(uint64_t val, uint64_t mask)
295 {
296 return byte_zap(val, (0x03 << (mask & 7)) >> 8);
297 }
298
299 uint64_t helper_inswh(uint64_t val, uint64_t mask)
300 {
301 val >>= 64 - ((mask & 7) * 8);
302 return byte_zap(val, ~((0x03 << (mask & 7)) >> 8));
303 }
304
305 uint64_t helper_msklh(uint64_t val, uint64_t mask)
306 {
307 return byte_zap(val, (0x0F << (mask & 7)) >> 8);
308 }
309
310 uint64_t helper_inslh(uint64_t val, uint64_t mask)
311 {
312 val >>= 64 - ((mask & 7) * 8);
313 return byte_zap(val, ~((0x0F << (mask & 7)) >> 8));
314 }
315
316 uint64_t helper_mskqh(uint64_t val, uint64_t mask)
317 {
318 return byte_zap(val, (0xFF << (mask & 7)) >> 8);
319 }
320
321 uint64_t helper_insqh(uint64_t val, uint64_t mask)
322 {
323 val >>= 64 - ((mask & 7) * 8);
324 return byte_zap(val, ~((0xFF << (mask & 7)) >> 8));
325 }
326
327 uint64_t helper_cmpbge (uint64_t op1, uint64_t op2)
328 {
329 uint8_t opa, opb, res;
330 int i;
331
332 res = 0;
333 for (i = 0; i < 7; i++) {
334 opa = op1 >> (i * 8);
335 opb = op2 >> (i * 8);
336 if (opa >= opb)
337 res |= 1 << i;
338 }
339 return res;
340 }
341
342 /* Floating point helpers */
343
344 /* F floating (VAX) */
345 static always_inline uint64_t float32_to_f (float32 fa)
346 {
347 uint32_t a;
348 uint64_t r, exp, mant, sig;
349
350 a = *(uint32_t*)(&fa);
351 sig = ((uint64_t)a & 0x80000000) << 32;
352 exp = (a >> 23) & 0xff;
353 mant = ((uint64_t)a & 0x007fffff) << 29;
354
355 if (exp == 255) {
356 /* NaN or infinity */
357 r = 1; /* VAX dirty zero */
358 } else if (exp == 0) {
359 if (mant == 0) {
360 /* Zero */
361 r = 0;
362 } else {
363 /* Denormalized */
364 r = sig | ((exp + 1) << 52) | mant;
365 }
366 } else {
367 if (exp >= 253) {
368 /* Overflow */
369 r = 1; /* VAX dirty zero */
370 } else {
371 r = sig | ((exp + 2) << 52);
372 }
373 }
374
375 return r;
376 }
377
378 static always_inline float32 f_to_float32 (uint64_t a)
379 {
380 uint32_t r, exp, mant_sig;
381
382 exp = ((a >> 55) & 0x80) | ((a >> 52) & 0x7f);
383 mant_sig = ((a >> 32) & 0x80000000) | ((a >> 29) & 0x007fffff);
384
385 if (unlikely(!exp && mant_sig)) {
386 /* Reserved operands / Dirty zero */
387 helper_excp(EXCP_OPCDEC, 0);
388 }
389
390 if (exp < 3) {
391 /* Underflow */
392 r = 0;
393 } else {
394 r = ((exp - 2) << 23) | mant_sig;
395 }
396
397 return *(float32*)(&a);
398 }
399
400 uint32_t helper_f_to_memory (uint64_t a)
401 {
402 uint32_t r;
403 r = (a & 0x00001fffe0000000ull) >> 13;
404 r |= (a & 0x07ffe00000000000ull) >> 45;
405 r |= (a & 0xc000000000000000ull) >> 48;
406 return r;
407 }
408
409 uint64_t helper_memory_to_f (uint32_t a)
410 {
411 uint64_t r;
412 r = ((uint64_t)(a & 0x0000c000)) << 48;
413 r |= ((uint64_t)(a & 0x003fffff)) << 45;
414 r |= ((uint64_t)(a & 0xffff0000)) << 13;
415 if (!(a & 0x00004000))
416 r |= 0x7ll << 59;
417 return r;
418 }
419
420 uint64_t helper_addf (uint64_t a, uint64_t b)
421 {
422 float32 fa, fb, fr;
423
424 fa = f_to_float32(a);
425 fb = f_to_float32(b);
426 fr = float32_add(fa, fb, &FP_STATUS);
427 return float32_to_f(fr);
428 }
429
430 uint64_t helper_subf (uint64_t a, uint64_t b)
431 {
432 float32 fa, fb, fr;
433
434 fa = f_to_float32(a);
435 fb = f_to_float32(b);
436 fr = float32_sub(fa, fb, &FP_STATUS);
437 return float32_to_f(fr);
438 }
439
440 uint64_t helper_mulf (uint64_t a, uint64_t b)
441 {
442 float32 fa, fb, fr;
443
444 fa = f_to_float32(a);
445 fb = f_to_float32(b);
446 fr = float32_mul(fa, fb, &FP_STATUS);
447 return float32_to_f(fr);
448 }
449
450 uint64_t helper_divf (uint64_t a, uint64_t b)
451 {
452 float32 fa, fb, fr;
453
454 fa = f_to_float32(a);
455 fb = f_to_float32(b);
456 fr = float32_div(fa, fb, &FP_STATUS);
457 return float32_to_f(fr);
458 }
459
460 uint64_t helper_sqrtf (uint64_t t)
461 {
462 float32 ft, fr;
463
464 ft = f_to_float32(t);
465 fr = float32_sqrt(ft, &FP_STATUS);
466 return float32_to_f(fr);
467 }
468
469
470 /* G floating (VAX) */
471 static always_inline uint64_t float64_to_g (float64 fa)
472 {
473 uint64_t a, r, exp, mant, sig;
474
475 a = *(uint64_t*)(&fa);
476 sig = a & 0x8000000000000000ull;
477 exp = (a >> 52) & 0x7ff;
478 mant = a & 0x000fffffffffffffull;
479
480 if (exp == 2047) {
481 /* NaN or infinity */
482 r = 1; /* VAX dirty zero */
483 } else if (exp == 0) {
484 if (mant == 0) {
485 /* Zero */
486 r = 0;
487 } else {
488 /* Denormalized */
489 r = sig | ((exp + 1) << 52) | mant;
490 }
491 } else {
492 if (exp >= 2045) {
493 /* Overflow */
494 r = 1; /* VAX dirty zero */
495 } else {
496 r = sig | ((exp + 2) << 52);
497 }
498 }
499
500 return r;
501 }
502
503 static always_inline float64 g_to_float64 (uint64_t a)
504 {
505 uint64_t r, exp, mant_sig;
506
507 exp = (a >> 52) & 0x7ff;
508 mant_sig = a & 0x800fffffffffffffull;
509
510 if (!exp && mant_sig) {
511 /* Reserved operands / Dirty zero */
512 helper_excp(EXCP_OPCDEC, 0);
513 }
514
515 if (exp < 3) {
516 /* Underflow */
517 r = 0;
518 } else {
519 r = ((exp - 2) << 52) | mant_sig;
520 }
521
522 return *(float64*)(&a);
523 }
524
525 uint64_t helper_g_to_memory (uint64_t a)
526 {
527 uint64_t r;
528 r = (a & 0x000000000000ffffull) << 48;
529 r |= (a & 0x00000000ffff0000ull) << 16;
530 r |= (a & 0x0000ffff00000000ull) >> 16;
531 r |= (a & 0xffff000000000000ull) >> 48;
532 return r;
533 }
534
535 uint64_t helper_memory_to_g (uint64_t a)
536 {
537 uint64_t r;
538 r = (a & 0x000000000000ffffull) << 48;
539 r |= (a & 0x00000000ffff0000ull) << 16;
540 r |= (a & 0x0000ffff00000000ull) >> 16;
541 r |= (a & 0xffff000000000000ull) >> 48;
542 return r;
543 }
544
545 uint64_t helper_addg (uint64_t a, uint64_t b)
546 {
547 float64 fa, fb, fr;
548
549 fa = g_to_float64(a);
550 fb = g_to_float64(b);
551 fr = float64_add(fa, fb, &FP_STATUS);
552 return float64_to_g(fr);
553 }
554
555 uint64_t helper_subg (uint64_t a, uint64_t b)
556 {
557 float64 fa, fb, fr;
558
559 fa = g_to_float64(a);
560 fb = g_to_float64(b);
561 fr = float64_sub(fa, fb, &FP_STATUS);
562 return float64_to_g(fr);
563 }
564
565 uint64_t helper_mulg (uint64_t a, uint64_t b)
566 {
567 float64 fa, fb, fr;
568
569 fa = g_to_float64(a);
570 fb = g_to_float64(b);
571 fr = float64_mul(fa, fb, &FP_STATUS);
572 return float64_to_g(fr);
573 }
574
575 uint64_t helper_divg (uint64_t a, uint64_t b)
576 {
577 float64 fa, fb, fr;
578
579 fa = g_to_float64(a);
580 fb = g_to_float64(b);
581 fr = float64_div(fa, fb, &FP_STATUS);
582 return float64_to_g(fr);
583 }
584
585 uint64_t helper_sqrtg (uint64_t a)
586 {
587 float64 fa, fr;
588
589 fa = g_to_float64(a);
590 fr = float64_sqrt(fa, &FP_STATUS);
591 return float64_to_g(fr);
592 }
593
594
595 /* S floating (single) */
596 static always_inline uint64_t float32_to_s (float32 fa)
597 {
598 uint32_t a;
599 uint64_t r;
600
601 a = *(uint32_t*)(&fa);
602
603 r = (((uint64_t)(a & 0xc0000000)) << 32) | (((uint64_t)(a & 0x3fffffff)) << 29);
604 if (((a & 0x7f800000) != 0x7f800000) && (!(a & 0x40000000)))
605 r |= 0x7ll << 59;
606 return r;
607 }
608
609 static always_inline float32 s_to_float32 (uint64_t a)
610 {
611 uint32_t r = ((a >> 32) & 0xc0000000) | ((a >> 29) & 0x3fffffff);
612 return *(float32*)(&r);
613 }
614
615 uint32_t helper_s_to_memory (uint64_t a)
616 {
617 /* Memory format is the same as float32 */
618 float32 fa = s_to_float32(a);
619 return *(uint32_t*)(&fa);
620 }
621
622 uint64_t helper_memory_to_s (uint32_t a)
623 {
624 /* Memory format is the same as float32 */
625 return float32_to_s(*(float32*)(&a));
626 }
627
628 uint64_t helper_adds (uint64_t a, uint64_t b)
629 {
630 float32 fa, fb, fr;
631
632 fa = s_to_float32(a);
633 fb = s_to_float32(b);
634 fr = float32_add(fa, fb, &FP_STATUS);
635 return float32_to_s(fr);
636 }
637
638 uint64_t helper_subs (uint64_t a, uint64_t b)
639 {
640 float32 fa, fb, fr;
641
642 fa = s_to_float32(a);
643 fb = s_to_float32(b);
644 fr = float32_sub(fa, fb, &FP_STATUS);
645 return float32_to_s(fr);
646 }
647
648 uint64_t helper_muls (uint64_t a, uint64_t b)
649 {
650 float32 fa, fb, fr;
651
652 fa = s_to_float32(a);
653 fb = s_to_float32(b);
654 fr = float32_mul(fa, fb, &FP_STATUS);
655 return float32_to_s(fr);
656 }
657
658 uint64_t helper_divs (uint64_t a, uint64_t b)
659 {
660 float32 fa, fb, fr;
661
662 fa = s_to_float32(a);
663 fb = s_to_float32(b);
664 fr = float32_div(fa, fb, &FP_STATUS);
665 return float32_to_s(fr);
666 }
667
668 uint64_t helper_sqrts (uint64_t a)
669 {
670 float32 fa, fr;
671
672 fa = s_to_float32(a);
673 fr = float32_sqrt(fa, &FP_STATUS);
674 return float32_to_s(fr);
675 }
676
677
678 /* T floating (double) */
679 static always_inline float64 t_to_float64 (uint64_t a)
680 {
681 /* Memory format is the same as float64 */
682 return *(float64*)(&a);
683 }
684
685 static always_inline uint64_t float64_to_t (float64 fa)
686 {
687 /* Memory format is the same as float64 */
688 return *(uint64*)(&fa);
689 }
690
691 uint64_t helper_addt (uint64_t a, uint64_t b)
692 {
693 float64 fa, fb, fr;
694
695 fa = t_to_float64(a);
696 fb = t_to_float64(b);
697 fr = float64_add(fa, fb, &FP_STATUS);
698 return float64_to_t(fr);
699 }
700
701 uint64_t helper_subt (uint64_t a, uint64_t b)
702 {
703 float64 fa, fb, fr;
704
705 fa = t_to_float64(a);
706 fb = t_to_float64(b);
707 fr = float64_sub(fa, fb, &FP_STATUS);
708 return float64_to_t(fr);
709 }
710
711 uint64_t helper_mult (uint64_t a, uint64_t b)
712 {
713 float64 fa, fb, fr;
714
715 fa = t_to_float64(a);
716 fb = t_to_float64(b);
717 fr = float64_mul(fa, fb, &FP_STATUS);
718 return float64_to_t(fr);
719 }
720
721 uint64_t helper_divt (uint64_t a, uint64_t b)
722 {
723 float64 fa, fb, fr;
724
725 fa = t_to_float64(a);
726 fb = t_to_float64(b);
727 fr = float64_div(fa, fb, &FP_STATUS);
728 return float64_to_t(fr);
729 }
730
731 uint64_t helper_sqrtt (uint64_t a)
732 {
733 float64 fa, fr;
734
735 fa = t_to_float64(a);
736 fr = float64_sqrt(fa, &FP_STATUS);
737 return float64_to_t(fr);
738 }
739
740
741 /* Sign copy */
742 uint64_t helper_cpys(uint64_t a, uint64_t b)
743 {
744 return (a & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
745 }
746
747 uint64_t helper_cpysn(uint64_t a, uint64_t b)
748 {
749 return ((~a) & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
750 }
751
752 uint64_t helper_cpyse(uint64_t a, uint64_t b)
753 {
754 return (a & 0xFFF0000000000000ULL) | (b & ~0xFFF0000000000000ULL);
755 }
756
757
758 /* Comparisons */
759 uint64_t helper_cmptun (uint64_t a, uint64_t b)
760 {
761 float64 fa, fb;
762
763 fa = t_to_float64(a);
764 fb = t_to_float64(b);
765
766 if (float64_is_nan(fa) || float64_is_nan(fb))
767 return 0x4000000000000000ULL;
768 else
769 return 0;
770 }
771
772 uint64_t helper_cmpteq(uint64_t a, uint64_t b)
773 {
774 float64 fa, fb;
775
776 fa = t_to_float64(a);
777 fb = t_to_float64(b);
778
779 if (float64_eq(fa, fb, &FP_STATUS))
780 return 0x4000000000000000ULL;
781 else
782 return 0;
783 }
784
785 uint64_t helper_cmptle(uint64_t a, uint64_t b)
786 {
787 float64 fa, fb;
788
789 fa = t_to_float64(a);
790 fb = t_to_float64(b);
791
792 if (float64_le(fa, fb, &FP_STATUS))
793 return 0x4000000000000000ULL;
794 else
795 return 0;
796 }
797
798 uint64_t helper_cmptlt(uint64_t a, uint64_t b)
799 {
800 float64 fa, fb;
801
802 fa = t_to_float64(a);
803 fb = t_to_float64(b);
804
805 if (float64_lt(fa, fb, &FP_STATUS))
806 return 0x4000000000000000ULL;
807 else
808 return 0;
809 }
810
811 uint64_t helper_cmpgeq(uint64_t a, uint64_t b)
812 {
813 float64 fa, fb;
814
815 fa = g_to_float64(a);
816 fb = g_to_float64(b);
817
818 if (float64_eq(fa, fb, &FP_STATUS))
819 return 0x4000000000000000ULL;
820 else
821 return 0;
822 }
823
824 uint64_t helper_cmpgle(uint64_t a, uint64_t b)
825 {
826 float64 fa, fb;
827
828 fa = g_to_float64(a);
829 fb = g_to_float64(b);
830
831 if (float64_le(fa, fb, &FP_STATUS))
832 return 0x4000000000000000ULL;
833 else
834 return 0;
835 }
836
837 uint64_t helper_cmpglt(uint64_t a, uint64_t b)
838 {
839 float64 fa, fb;
840
841 fa = g_to_float64(a);
842 fb = g_to_float64(b);
843
844 if (float64_lt(fa, fb, &FP_STATUS))
845 return 0x4000000000000000ULL;
846 else
847 return 0;
848 }
849
850 uint64_t helper_cmpfeq (uint64_t a)
851 {
852 return !(a & 0x7FFFFFFFFFFFFFFFULL);
853 }
854
855 uint64_t helper_cmpfne (uint64_t a)
856 {
857 return (a & 0x7FFFFFFFFFFFFFFFULL);
858 }
859
860 uint64_t helper_cmpflt (uint64_t a)
861 {
862 return (a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
863 }
864
865 uint64_t helper_cmpfle (uint64_t a)
866 {
867 return (a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
868 }
869
870 uint64_t helper_cmpfgt (uint64_t a)
871 {
872 return !(a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
873 }
874
875 uint64_t helper_cmpfge (uint64_t a)
876 {
877 return !(a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
878 }
879
880
881 /* Floating point format conversion */
882 uint64_t helper_cvtts (uint64_t a)
883 {
884 float64 fa;
885 float32 fr;
886
887 fa = t_to_float64(a);
888 fr = float64_to_float32(fa, &FP_STATUS);
889 return float32_to_s(fr);
890 }
891
892 uint64_t helper_cvtst (uint64_t a)
893 {
894 float32 fa;
895 float64 fr;
896
897 fa = s_to_float32(a);
898 fr = float32_to_float64(fa, &FP_STATUS);
899 return float64_to_t(fr);
900 }
901
902 uint64_t helper_cvtqs (uint64_t a)
903 {
904 float32 fr = int64_to_float32(a, &FP_STATUS);
905 return float32_to_s(fr);
906 }
907
908 uint64_t helper_cvttq (uint64_t a)
909 {
910 float64 fa = t_to_float64(a);
911 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
912 }
913
914 uint64_t helper_cvtqt (uint64_t a)
915 {
916 float64 fr = int64_to_float64(a, &FP_STATUS);
917 return float64_to_t(fr);
918 }
919
920 uint64_t helper_cvtqf (uint64_t a)
921 {
922 float32 fr = int64_to_float32(a, &FP_STATUS);
923 return float32_to_f(fr);
924 }
925
926 uint64_t helper_cvtgf (uint64_t a)
927 {
928 float64 fa;
929 float32 fr;
930
931 fa = g_to_float64(a);
932 fr = float64_to_float32(fa, &FP_STATUS);
933 return float32_to_f(fr);
934 }
935
936 uint64_t helper_cvtgq (uint64_t a)
937 {
938 float64 fa = g_to_float64(a);
939 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
940 }
941
942 uint64_t helper_cvtqg (uint64_t a)
943 {
944 float64 fr;
945 fr = int64_to_float64(a, &FP_STATUS);
946 return float64_to_g(fr);
947 }
948
949 uint64_t helper_cvtlq (uint64_t a)
950 {
951 return (int64_t)((int32_t)((a >> 32) | ((a >> 29) & 0x3FFFFFFF)));
952 }
953
954 static always_inline uint64_t __helper_cvtql (uint64_t a, int s, int v)
955 {
956 uint64_t r;
957
958 r = ((uint64_t)(a & 0xC0000000)) << 32;
959 r |= ((uint64_t)(a & 0x7FFFFFFF)) << 29;
960
961 if (v && (int64_t)((int32_t)r) != (int64_t)r) {
962 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
963 }
964 if (s) {
965 /* TODO */
966 }
967 return r;
968 }
969
970 uint64_t helper_cvtql (uint64_t a)
971 {
972 return __helper_cvtql(a, 0, 0);
973 }
974
975 uint64_t helper_cvtqlv (uint64_t a)
976 {
977 return __helper_cvtql(a, 0, 1);
978 }
979
980 uint64_t helper_cvtqlsv (uint64_t a)
981 {
982 return __helper_cvtql(a, 1, 1);
983 }
984
985 /* PALcode support special instructions */
986 #if !defined (CONFIG_USER_ONLY)
987 void helper_hw_rei (void)
988 {
989 env->pc = env->ipr[IPR_EXC_ADDR] & ~3;
990 env->ipr[IPR_EXC_ADDR] = env->ipr[IPR_EXC_ADDR] & 1;
991 /* XXX: re-enable interrupts and memory mapping */
992 }
993
994 void helper_hw_ret (uint64_t a)
995 {
996 env->pc = a & ~3;
997 env->ipr[IPR_EXC_ADDR] = a & 1;
998 /* XXX: re-enable interrupts and memory mapping */
999 }
1000
1001 uint64_t helper_mfpr (int iprn, uint64_t val)
1002 {
1003 uint64_t tmp;
1004
1005 if (cpu_alpha_mfpr(env, iprn, &tmp) == 0)
1006 val = tmp;
1007
1008 return val;
1009 }
1010
1011 void helper_mtpr (int iprn, uint64_t val)
1012 {
1013 cpu_alpha_mtpr(env, iprn, val, NULL);
1014 }
1015
1016 void helper_set_alt_mode (void)
1017 {
1018 env->saved_mode = env->ps & 0xC;
1019 env->ps = (env->ps & ~0xC) | (env->ipr[IPR_ALT_MODE] & 0xC);
1020 }
1021
1022 void helper_restore_mode (void)
1023 {
1024 env->ps = (env->ps & ~0xC) | env->saved_mode;
1025 }
1026
1027 #endif
1028
1029 /*****************************************************************************/
1030 /* Softmmu support */
1031 #if !defined (CONFIG_USER_ONLY)
1032
1033 /* XXX: the two following helpers are pure hacks.
1034 * Hopefully, we emulate the PALcode, then we should never see
1035 * HW_LD / HW_ST instructions.
1036 */
1037 uint64_t helper_ld_virt_to_phys (uint64_t virtaddr)
1038 {
1039 uint64_t tlb_addr, physaddr;
1040 int index, mmu_idx;
1041 void *retaddr;
1042
1043 mmu_idx = cpu_mmu_index(env);
1044 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1045 redo:
1046 tlb_addr = env->tlb_table[mmu_idx][index].addr_read;
1047 if ((virtaddr & TARGET_PAGE_MASK) ==
1048 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1049 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1050 } else {
1051 /* the page is not in the TLB : fill it */
1052 retaddr = GETPC();
1053 tlb_fill(virtaddr, 0, mmu_idx, retaddr);
1054 goto redo;
1055 }
1056 return physaddr;
1057 }
1058
1059 uint64_t helper_st_virt_to_phys (uint64_t virtaddr)
1060 {
1061 uint64_t tlb_addr, physaddr;
1062 int index, mmu_idx;
1063 void *retaddr;
1064
1065 mmu_idx = cpu_mmu_index(env);
1066 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1067 redo:
1068 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
1069 if ((virtaddr & TARGET_PAGE_MASK) ==
1070 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1071 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1072 } else {
1073 /* the page is not in the TLB : fill it */
1074 retaddr = GETPC();
1075 tlb_fill(virtaddr, 1, mmu_idx, retaddr);
1076 goto redo;
1077 }
1078 return physaddr;
1079 }
1080
1081 void helper_ldl_raw(uint64_t t0, uint64_t t1)
1082 {
1083 ldl_raw(t1, t0);
1084 }
1085
1086 void helper_ldq_raw(uint64_t t0, uint64_t t1)
1087 {
1088 ldq_raw(t1, t0);
1089 }
1090
1091 void helper_ldl_l_raw(uint64_t t0, uint64_t t1)
1092 {
1093 env->lock = t1;
1094 ldl_raw(t1, t0);
1095 }
1096
1097 void helper_ldq_l_raw(uint64_t t0, uint64_t t1)
1098 {
1099 env->lock = t1;
1100 ldl_raw(t1, t0);
1101 }
1102
1103 void helper_ldl_kernel(uint64_t t0, uint64_t t1)
1104 {
1105 ldl_kernel(t1, t0);
1106 }
1107
1108 void helper_ldq_kernel(uint64_t t0, uint64_t t1)
1109 {
1110 ldq_kernel(t1, t0);
1111 }
1112
1113 void helper_ldl_data(uint64_t t0, uint64_t t1)
1114 {
1115 ldl_data(t1, t0);
1116 }
1117
1118 void helper_ldq_data(uint64_t t0, uint64_t t1)
1119 {
1120 ldq_data(t1, t0);
1121 }
1122
1123 void helper_stl_raw(uint64_t t0, uint64_t t1)
1124 {
1125 stl_raw(t1, t0);
1126 }
1127
1128 void helper_stq_raw(uint64_t t0, uint64_t t1)
1129 {
1130 stq_raw(t1, t0);
1131 }
1132
1133 uint64_t helper_stl_c_raw(uint64_t t0, uint64_t t1)
1134 {
1135 uint64_t ret;
1136
1137 if (t1 == env->lock) {
1138 stl_raw(t1, t0);
1139 ret = 0;
1140 } else
1141 ret = 1;
1142
1143 env->lock = 1;
1144
1145 return ret;
1146 }
1147
1148 uint64_t helper_stq_c_raw(uint64_t t0, uint64_t t1)
1149 {
1150 uint64_t ret;
1151
1152 if (t1 == env->lock) {
1153 stq_raw(t1, t0);
1154 ret = 0;
1155 } else
1156 ret = 1;
1157
1158 env->lock = 1;
1159
1160 return ret;
1161 }
1162
1163 #define MMUSUFFIX _mmu
1164
1165 #define SHIFT 0
1166 #include "softmmu_template.h"
1167
1168 #define SHIFT 1
1169 #include "softmmu_template.h"
1170
1171 #define SHIFT 2
1172 #include "softmmu_template.h"
1173
1174 #define SHIFT 3
1175 #include "softmmu_template.h"
1176
1177 /* try to fill the TLB and return an exception if error. If retaddr is
1178 NULL, it means that the function was called in C code (i.e. not
1179 from generated code or from helper.c) */
1180 /* XXX: fix it to restore all registers */
1181 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1182 {
1183 TranslationBlock *tb;
1184 CPUState *saved_env;
1185 unsigned long pc;
1186 int ret;
1187
1188 /* XXX: hack to restore env in all cases, even if not called from
1189 generated code */
1190 saved_env = env;
1191 env = cpu_single_env;
1192 ret = cpu_alpha_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1193 if (!likely(ret == 0)) {
1194 if (likely(retaddr)) {
1195 /* now we have a real cpu fault */
1196 pc = (unsigned long)retaddr;
1197 tb = tb_find_pc(pc);
1198 if (likely(tb)) {
1199 /* the PC is inside the translated code. It means that we have
1200 a virtual CPU fault */
1201 cpu_restore_state(tb, env, pc, NULL);
1202 }
1203 }
1204 /* Exception index and error code are already set */
1205 cpu_loop_exit();
1206 }
1207 env = saved_env;
1208 }
1209
1210 #endif