Blame view

target-alpha/op_helper.c 25.2 KB
1
2
/*
 *  Alpha emulation cpu micro-operations helpers for qemu.
3
 *
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *  Copyright (c) 2007 Jocelyn Mayer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "exec.h"
22
#include "host-utils.h"
23
24
25
26
27
28
29
30
31
#include "softfloat.h"

void helper_tb_flush (void)
{
    tlb_flush(env, 1);
}

/*****************************************************************************/
/* Exceptions processing helpers */
32
void helper_excp (int excp, int error)
33
34
35
36
37
38
{
    env->exception_index = excp;
    env->error_code = error;
    cpu_loop_exit();
}
39
uint64_t helper_amask (uint64_t arg)
40
41
42
43
44
45
46
47
{
    switch (env->implver) {
    case IMPLVER_2106x:
        /* EV4, EV45, LCA, LCA45 & EV5 */
        break;
    case IMPLVER_21164:
    case IMPLVER_21264:
    case IMPLVER_21364:
48
        arg &= ~env->amask;
49
50
        break;
    }
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
73
74
75
76
    env->ipr[IPR_EXC_SUM] &= ~0x3E:
    env->ipr[IPR_EXC_SUM] |= env->fp_status.float_exception_flags << 1;
#endif
    switch (env->fp_status.float_rounding_mode) {
    case float_round_nearest_even:
77
        ret |= 2ULL << 58;
78
79
        break;
    case float_round_down:
80
        ret |= 1ULL << 58;
81
82
        break;
    case float_round_up:
83
        ret |= 3ULL << 58;
84
85
86
87
        break;
    case float_round_to_zero:
        break;
    }
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    case 0:
        set_float_rounding_mode(float_round_to_zero, &FP_STATUS);
        break;
    case 1:
        set_float_rounding_mode(float_round_down, &FP_STATUS);
        break;
    case 2:
        set_float_rounding_mode(float_round_nearest_even, &FP_STATUS);
        break;
    case 3:
        set_float_rounding_mode(float_round_up, &FP_STATUS);
        break;
    }
}
112
spinlock_t intr_cpu_lock = SPIN_LOCK_UNLOCKED;
113
114
uint64_t helper_rs(void)
115
{
116
117
118
119
120
121
122
123
    uint64_t tmp;

    spin_lock(&intr_cpu_lock);
    tmp = env->intr_flag;
    env->intr_flag = 1;
    spin_unlock(&intr_cpu_lock);

    return tmp;
124
125
}
126
uint64_t helper_rc(void)
127
{
128
129
130
131
132
133
134
135
    uint64_t tmp;

    spin_lock(&intr_cpu_lock);
    tmp = env->intr_flag;
    env->intr_flag = 0;
    spin_unlock(&intr_cpu_lock);

    return tmp;
136
137
}
138
uint64_t helper_addqv (uint64_t op1, uint64_t op2)
139
{
140
141
142
    uint64_t tmp = op1;
    op1 += op2;
    if (unlikely((tmp ^ op2 ^ (-1ULL)) & (tmp ^ op1) & (1ULL << 63))) {
143
144
        helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
    }
145
    return op1;
146
147
}
148
uint64_t helper_addlv (uint64_t op1, uint64_t op2)
149
{
150
151
152
    uint64_t tmp = op1;
    op1 = (uint32_t)(op1 + op2);
    if (unlikely((tmp ^ op2 ^ (-1UL)) & (tmp ^ op1) & (1UL << 31))) {
153
154
        helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
    }
155
    return op1;
156
157
}
158
uint64_t helper_subqv (uint64_t op1, uint64_t op2)
159
{
160
161
162
    uint64_t tmp = op1;
    op1 -= op2;
    if (unlikely(((~tmp) ^ op1 ^ (-1ULL)) & ((~tmp) ^ op2) & (1ULL << 63))) {
163
164
        helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
    }
165
    return op1;
166
167
}
168
uint64_t helper_sublv (uint64_t op1, uint64_t op2)
169
{
170
171
172
    uint64_t tmp = op1;
    op1 = (uint32_t)(op1 - op2);
    if (unlikely(((~tmp) ^ op1 ^ (-1UL)) & ((~tmp) ^ op2) & (1UL << 31))) {
173
174
        helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
    }
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
183
184

    if (unlikely((int32_t)res != res)) {
        helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
    }
185
    return (int64_t)((int32_t)res);
186
187
}
188
uint64_t helper_mulqv (uint64_t op1, uint64_t op2)
189
{
190
191
    uint64_t tl, th;
192
    muls64(&tl, &th, op1, op2);
193
194
    /* If th != 0 && th != -1, then we had an overflow */
    if (unlikely((th + 1) > 1)) {
195
196
        helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
    }
197
198
199
200
201
202
203
204
205
    return tl;
}

uint64_t helper_umulh (uint64_t op1, uint64_t op2)
{
    uint64_t tl, th;

    mulu64(&tl, &th, op1, op2);
    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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{
    uint64_t mask;

    mask = 0;
    mask |= ((mskb >> 0) & 1) * 0x00000000000000FFULL;
    mask |= ((mskb >> 1) & 1) * 0x000000000000FF00ULL;
    mask |= ((mskb >> 2) & 1) * 0x0000000000FF0000ULL;
    mask |= ((mskb >> 3) & 1) * 0x00000000FF000000ULL;
    mask |= ((mskb >> 4) & 1) * 0x000000FF00000000ULL;
    mask |= ((mskb >> 5) & 1) * 0x0000FF0000000000ULL;
    mask |= ((mskb >> 6) & 1) * 0x00FF000000000000ULL;
    mask |= ((mskb >> 7) & 1) * 0xFF00000000000000ULL;

    return op & ~mask;
}
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
248
    val <<= (mask & 7) * 8;
    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
259
    val <<= (mask & 7) * 8;
    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
270
    val <<= (mask & 7) * 8;
    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
291
    val <<= (mask & 7) * 8;
    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
302
    val >>= 64 - ((mask & 7) * 8);
    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
313
    val >>= 64 - ((mask & 7) * 8);
    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
324
    val >>= 64 - ((mask & 7) * 8);
    return byte_zap(val, ~((0xFF << (mask & 7)) >> 8));
325
326
}
327
uint64_t helper_cmpbge (uint64_t op1, uint64_t op2)
328
329
330
331
332
333
{
    uint8_t opa, opb, res;
    int i;

    res = 0;
    for (i = 0; i < 7; i++) {
334
335
        opa = op1 >> (i * 8);
        opb = op2 >> (i * 8);
336
337
338
        if (opa >= opb)
            res |= 1 << i;
    }
339
    return res;
340
341
}
342
343
344
345
/* Floating point helpers */

/* F floating (VAX) */
static always_inline uint64_t float32_to_f (float32 fa)
346
{
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
    uint32_t a;
    uint64_t r, exp, mant, sig;

    a = *(uint32_t*)(&fa);
    sig = ((uint64_t)a & 0x80000000) << 32;
    exp = (a >> 23) & 0xff;
    mant = ((uint64_t)a & 0x007fffff) << 29;

    if (exp == 255) {
        /* NaN or infinity */
        r = 1; /* VAX dirty zero */
    } else if (exp == 0) {
        if (mant == 0) {
            /* Zero */
            r = 0;
        } else {
            /* Denormalized */
            r = sig | ((exp + 1) << 52) | mant;
        }
    } else {
        if (exp >= 253) {
            /* Overflow */
            r = 1; /* VAX dirty zero */
        } else {
            r = sig | ((exp + 2) << 52);
        }
    }

    return r;
376
377
}
378
static always_inline float32 f_to_float32 (uint64_t a)
379
{
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
    uint32_t r, exp, mant_sig;

    exp = ((a >> 55) & 0x80) | ((a >> 52) & 0x7f);
    mant_sig = ((a >> 32) & 0x80000000) | ((a >> 29) & 0x007fffff);

    if (unlikely(!exp && mant_sig)) {
        /* Reserved operands / Dirty zero */
        helper_excp(EXCP_OPCDEC, 0);
    }

    if (exp < 3) {
        /* Underflow */
        r = 0;
    } else {
        r = ((exp - 2) << 23) | mant_sig;
    }

    return *(float32*)(&a);
398
399
}
400
uint32_t helper_f_to_memory (uint64_t a)
401
{
402
403
404
405
406
407
    uint32_t r;
    r =  (a & 0x00001fffe0000000ull) >> 13;
    r |= (a & 0x07ffe00000000000ull) >> 45;
    r |= (a & 0xc000000000000000ull) >> 48;
    return r;
}
408
409
410
411
412
413
414
415
416
417
uint64_t helper_memory_to_f (uint32_t a)
{
    uint64_t r;
    r =  ((uint64_t)(a & 0x0000c000)) << 48;
    r |= ((uint64_t)(a & 0x003fffff)) << 45;
    r |= ((uint64_t)(a & 0xffff0000)) << 13;
    if (!(a & 0x00004000))
        r |= 0x7ll << 59;
    return r;
418
419
}
420
uint64_t helper_addf (uint64_t a, uint64_t b)
421
{
422
    float32 fa, fb, fr;
423
424
425
426
427
    fa = f_to_float32(a);
    fb = f_to_float32(b);
    fr = float32_add(fa, fb, &FP_STATUS);
    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
435
436
437
    fa = f_to_float32(a);
    fb = f_to_float32(b);
    fr = float32_sub(fa, fb, &FP_STATUS);
    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
445
446
447
    fa = f_to_float32(a);
    fb = f_to_float32(b);
    fr = float32_mul(fa, fb, &FP_STATUS);
    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
455
456
457
    fa = f_to_float32(a);
    fb = f_to_float32(b);
    fr = float32_div(fa, fb, &FP_STATUS);
    return float32_to_f(fr);
458
459
}
460
uint64_t helper_sqrtf (uint64_t t)
461
{
462
463
464
465
466
    float32 ft, fr;

    ft = f_to_float32(t);
    fr = float32_sqrt(ft, &FP_STATUS);
    return float32_to_f(fr);
467
468
}
469
470
471

/* G floating (VAX) */
static always_inline uint64_t float64_to_g (float64 fa)
472
{
473
    uint64_t a, r, exp, mant, sig;
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    a = *(uint64_t*)(&fa);
    sig = a & 0x8000000000000000ull;
    exp = (a >> 52) & 0x7ff;
    mant = a & 0x000fffffffffffffull;

    if (exp == 2047) {
        /* NaN or infinity */
        r = 1; /* VAX dirty zero */
    } else if (exp == 0) {
        if (mant == 0) {
            /* Zero */
            r = 0;
        } else {
            /* Denormalized */
            r = sig | ((exp + 1) << 52) | mant;
        }
    } else {
        if (exp >= 2045) {
            /* Overflow */
            r = 1; /* VAX dirty zero */
        } else {
            r = sig | ((exp + 2) << 52);
        }
    }

    return r;
501
502
}
503
static always_inline float64 g_to_float64 (uint64_t a)
504
{
505
506
507
508
509
510
511
512
513
    uint64_t r, exp, mant_sig;

    exp = (a >> 52) & 0x7ff;
    mant_sig = a & 0x800fffffffffffffull;

    if (!exp && mant_sig) {
        /* Reserved operands / Dirty zero */
        helper_excp(EXCP_OPCDEC, 0);
    }
514
515
516
517
518
519
520
521
522
    if (exp < 3) {
        /* Underflow */
        r = 0;
    } else {
        r = ((exp - 2) << 52) | mant_sig;
    }

    return *(float64*)(&a);
523
524
}
525
uint64_t helper_g_to_memory (uint64_t a)
526
{
527
528
529
530
531
532
533
    uint64_t r;
    r =  (a & 0x000000000000ffffull) << 48;
    r |= (a & 0x00000000ffff0000ull) << 16;
    r |= (a & 0x0000ffff00000000ull) >> 16;
    r |= (a & 0xffff000000000000ull) >> 48;
    return r;
}
534
535
536
537
538
539
540
541
542
uint64_t helper_memory_to_g (uint64_t a)
{
    uint64_t r;
    r =  (a & 0x000000000000ffffull) << 48;
    r |= (a & 0x00000000ffff0000ull) << 16;
    r |= (a & 0x0000ffff00000000ull) >> 16;
    r |= (a & 0xffff000000000000ull) >> 48;
    return r;
543
544
}
545
uint64_t helper_addg (uint64_t a, uint64_t b)
546
{
547
    float64 fa, fb, fr;
548
549
550
551
552
    fa = g_to_float64(a);
    fb = g_to_float64(b);
    fr = float64_add(fa, fb, &FP_STATUS);
    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
560
561
562
    fa = g_to_float64(a);
    fb = g_to_float64(b);
    fr = float64_sub(fa, fb, &FP_STATUS);
    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
570
571
572
    fa = g_to_float64(a);
    fb = g_to_float64(b);
    fr = float64_mul(fa, fb, &FP_STATUS);
    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
580
581
582
583
584
585
586
587
    fa = g_to_float64(a);
    fb = g_to_float64(b);
    fr = float64_div(fa, fb, &FP_STATUS);
    return float64_to_g(fr);
}

uint64_t helper_sqrtg (uint64_t a)
{
    float64 fa, fr;
588
589
590
591
    fa = g_to_float64(a);
    fr = float64_sqrt(fa, &FP_STATUS);
    return float64_to_g(fr);
592
593
}
594
595
596

/* S floating (single) */
static always_inline uint64_t float32_to_s (float32 fa)
597
{
598
599
    uint32_t a;
    uint64_t r;
600
601
    a = *(uint32_t*)(&fa);
602
603
604
605
606
    r = (((uint64_t)(a & 0xc0000000)) << 32) | (((uint64_t)(a & 0x3fffffff)) << 29);
    if (((a & 0x7f800000) != 0x7f800000) && (!(a & 0x40000000)))
        r |= 0x7ll << 59;
    return r;
607
608
}
609
static always_inline float32 s_to_float32 (uint64_t a)
610
{
611
612
613
    uint32_t r = ((a >> 32) & 0xc0000000) | ((a >> 29) & 0x3fffffff);
    return *(float32*)(&r);
}
614
615
616
617
618
619
620
uint32_t helper_s_to_memory (uint64_t a)
{
    /* Memory format is the same as float32 */
    float32 fa = s_to_float32(a);
    return *(uint32_t*)(&fa);
}
621
622
623
624
625
uint64_t helper_memory_to_s (uint32_t a)
{
    /* Memory format is the same as float32 */
    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
633
634
635
    fa = s_to_float32(a);
    fb = s_to_float32(b);
    fr = float32_add(fa, fb, &FP_STATUS);
    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
643
644
645
    fa = s_to_float32(a);
    fb = s_to_float32(b);
    fr = float32_sub(fa, fb, &FP_STATUS);
    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
653
654
655
    fa = s_to_float32(a);
    fb = s_to_float32(b);
    fr = float32_mul(fa, fb, &FP_STATUS);
    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
663
664
665
    fa = s_to_float32(a);
    fb = s_to_float32(b);
    fr = float32_div(fa, fb, &FP_STATUS);
    return float32_to_s(fr);
666
667
}
668
uint64_t helper_sqrts (uint64_t a)
669
{
670
    float32 fa, fr;
671
672
673
674
    fa = s_to_float32(a);
    fr = float32_sqrt(fa, &FP_STATUS);
    return float32_to_s(fr);
675
676
}
677
678
679

/* T floating (double) */
static always_inline float64 t_to_float64 (uint64_t a)
680
{
681
682
    /* Memory format is the same as float64 */
    return *(float64*)(&a);
683
684
}
685
static always_inline uint64_t float64_to_t (float64 fa)
686
{
687
688
689
    /* Memory format is the same as float64 */
    return *(uint64*)(&fa);
}
690
691
692
693
uint64_t helper_addt (uint64_t a, uint64_t b)
{
    float64 fa, fb, fr;
694
695
696
697
698
    fa = t_to_float64(a);
    fb = t_to_float64(b);
    fr = float64_add(fa, fb, &FP_STATUS);
    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
706
707
708
    fa = t_to_float64(a);
    fb = t_to_float64(b);
    fr = float64_sub(fa, fb, &FP_STATUS);
    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
716
717
718
    fa = t_to_float64(a);
    fb = t_to_float64(b);
    fr = float64_mul(fa, fb, &FP_STATUS);
    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
726
727
728
    fa = t_to_float64(a);
    fb = t_to_float64(b);
    fr = float64_div(fa, fb, &FP_STATUS);
    return float64_to_t(fr);
729
730
}
731
uint64_t helper_sqrtt (uint64_t a)
732
{
733
    float64 fa, fr;
734
735
736
737
    fa = t_to_float64(a);
    fr = float64_sqrt(fa, &FP_STATUS);
    return float64_to_t(fr);
738
739
740
}
741
742
743
744
/* Sign copy */
uint64_t helper_cpys(uint64_t a, uint64_t b)
{
    return (a & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
745
746
}
747
uint64_t helper_cpysn(uint64_t a, uint64_t b)
748
{
749
750
    return ((~a) & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
}
751
752
753
754
uint64_t helper_cpyse(uint64_t a, uint64_t b)
{
    return (a & 0xFFF0000000000000ULL) | (b & ~0xFFF0000000000000ULL);
755
756
}
757
758
759

/* Comparisons */
uint64_t helper_cmptun (uint64_t a, uint64_t b)
760
{
761
    float64 fa, fb;
762
763
764
765
766
767
768
769
    fa = t_to_float64(a);
    fb = t_to_float64(b);

    if (float64_is_nan(fa) || float64_is_nan(fb))
        return 0x4000000000000000ULL;
    else
        return 0;
770
771
}
772
uint64_t helper_cmpteq(uint64_t a, uint64_t b)
773
{
774
    float64 fa, fb;
775
776
777
778
779
780
781
782
    fa = t_to_float64(a);
    fb = t_to_float64(b);

    if (float64_eq(fa, fb, &FP_STATUS))
        return 0x4000000000000000ULL;
    else
        return 0;
783
784
}
785
uint64_t helper_cmptle(uint64_t a, uint64_t b)
786
{
787
    float64 fa, fb;
788
789
790
791
792
793
794
795
    fa = t_to_float64(a);
    fb = t_to_float64(b);

    if (float64_le(fa, fb, &FP_STATUS))
        return 0x4000000000000000ULL;
    else
        return 0;
796
797
}
798
uint64_t helper_cmptlt(uint64_t a, uint64_t b)
799
{
800
    float64 fa, fb;
801
802
803
804
805
806
807
808
    fa = t_to_float64(a);
    fb = t_to_float64(b);

    if (float64_lt(fa, fb, &FP_STATUS))
        return 0x4000000000000000ULL;
    else
        return 0;
809
810
}
811
uint64_t helper_cmpgeq(uint64_t a, uint64_t b)
812
{
813
    float64 fa, fb;
814
815
816
817
818
819
820
821
    fa = g_to_float64(a);
    fb = g_to_float64(b);

    if (float64_eq(fa, fb, &FP_STATUS))
        return 0x4000000000000000ULL;
    else
        return 0;
822
823
}
824
uint64_t helper_cmpgle(uint64_t a, uint64_t b)
825
{
826
827
828
829
    float64 fa, fb;

    fa = g_to_float64(a);
    fb = g_to_float64(b);
830
831
832
833
834
    if (float64_le(fa, fb, &FP_STATUS))
        return 0x4000000000000000ULL;
    else
        return 0;
835
836
}
837
uint64_t helper_cmpglt(uint64_t a, uint64_t b)
838
{
839
840
841
842
    float64 fa, fb;

    fa = g_to_float64(a);
    fb = g_to_float64(b);
843
844
845
846
847
    if (float64_lt(fa, fb, &FP_STATUS))
        return 0x4000000000000000ULL;
    else
        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
858
    return (a & 0x7FFFFFFFFFFFFFFFULL);
}
859
860
861
862
uint64_t helper_cmpflt (uint64_t a)
{
    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
873
    return !(a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
}
874
875
876
877
uint64_t helper_cmpfge (uint64_t a)
{
    return !(a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
878
879
}
880
881
882

/* Floating point format conversion */
uint64_t helper_cvtts (uint64_t a)
883
{
884
885
    float64 fa;
    float32 fr;
886
887
888
889
    fa = t_to_float64(a);
    fr = float64_to_float32(fa, &FP_STATUS);
    return float32_to_s(fr);
890
891
}
892
uint64_t helper_cvtst (uint64_t a)
893
{
894
895
896
897
898
899
    float32 fa;
    float64 fr;

    fa = s_to_float32(a);
    fr = float32_to_float64(fa, &FP_STATUS);
    return float64_to_t(fr);
900
901
}
902
uint64_t helper_cvtqs (uint64_t a)
903
{
904
905
    float32 fr = int64_to_float32(a, &FP_STATUS);
    return float32_to_s(fr);
906
907
}
908
uint64_t helper_cvttq (uint64_t a)
909
{
910
911
912
    float64 fa = t_to_float64(a);
    return float64_to_int64_round_to_zero(fa, &FP_STATUS);
}
913
914
915
916
917
uint64_t helper_cvtqt (uint64_t a)
{
    float64 fr = int64_to_float64(a, &FP_STATUS);
    return float64_to_t(fr);
918
919
}
920
uint64_t helper_cvtqf (uint64_t a)
921
{
922
923
    float32 fr = int64_to_float32(a, &FP_STATUS);
    return float32_to_f(fr);
924
925
}
926
uint64_t helper_cvtgf (uint64_t a)
927
{
928
929
930
931
932
933
    float64 fa;
    float32 fr;

    fa = g_to_float64(a);
    fr = float64_to_float32(fa, &FP_STATUS);
    return float32_to_f(fr);
934
935
}
936
uint64_t helper_cvtgq (uint64_t a)
937
{
938
939
    float64 fa = g_to_float64(a);
    return float64_to_int64_round_to_zero(fa, &FP_STATUS);
940
941
}
942
uint64_t helper_cvtqg (uint64_t a)
943
{
944
945
946
    float64 fr;
    fr = int64_to_float64(a, &FP_STATUS);
    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
957
958
959
960
961
962
963
964
965
966
967
    uint64_t r;

    r = ((uint64_t)(a & 0xC0000000)) << 32;
    r |= ((uint64_t)(a & 0x7FFFFFFF)) << 29;

    if (v && (int64_t)((int32_t)r) != (int64_t)r) {
        helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
    }
    if (s) {
        /* TODO */
    }
    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
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
void helper_hw_rei (void)
{
    env->pc = env->ipr[IPR_EXC_ADDR] & ~3;
    env->ipr[IPR_EXC_ADDR] = env->ipr[IPR_EXC_ADDR] & 1;
    /* XXX: re-enable interrupts and memory mapping */
}

void helper_hw_ret (uint64_t a)
{
    env->pc = a & ~3;
    env->ipr[IPR_EXC_ADDR] = a & 1;
    /* XXX: re-enable interrupts and memory mapping */
}

uint64_t helper_mfpr (int iprn, uint64_t val)
{
    uint64_t tmp;

    if (cpu_alpha_mfpr(env, iprn, &tmp) == 0)
        val = tmp;

    return val;
}

void helper_mtpr (int iprn, uint64_t val)
1012
{
1013
1014
    cpu_alpha_mtpr(env, iprn, val, NULL);
}
1015
1016
1017
1018
1019
void helper_set_alt_mode (void)
{
    env->saved_mode = env->ps & 0xC;
    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
1028
1029
1030
1031
1032
1033
1034
1035
1036
#endif

/*****************************************************************************/
/* Softmmu support */
#if !defined (CONFIG_USER_ONLY)

/* XXX: the two following helpers are pure hacks.
 *      Hopefully, we emulate the PALcode, then we should never see
 *      HW_LD / HW_ST instructions.
 */
1037
uint64_t helper_ld_virt_to_phys (uint64_t virtaddr)
1038
1039
{
    uint64_t tlb_addr, physaddr;
1040
    int index, mmu_idx;
1041
1042
    void *retaddr;
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
1051
1052
    } else {
        /* the page is not in the TLB : fill it */
        retaddr = GETPC();
1053
        tlb_fill(virtaddr, 0, mmu_idx, retaddr);
1054
1055
        goto redo;
    }
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
1064
    void *retaddr;
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
1073
1074
    } else {
        /* the page is not in the TLB : fill it */
        retaddr = GETPC();
1075
        tlb_fill(virtaddr, 1, mmu_idx, retaddr);
1076
1077
        goto redo;
    }
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
    return physaddr;
}

void helper_ldl_raw(uint64_t t0, uint64_t t1)
{
    ldl_raw(t1, t0);
}

void helper_ldq_raw(uint64_t t0, uint64_t t1)
{
    ldq_raw(t1, t0);
}

void helper_ldl_l_raw(uint64_t t0, uint64_t t1)
{
    env->lock = t1;
    ldl_raw(t1, t0);
}

void helper_ldq_l_raw(uint64_t t0, uint64_t t1)
{
    env->lock = t1;
    ldl_raw(t1, t0);
}

void helper_ldl_kernel(uint64_t t0, uint64_t t1)
{
    ldl_kernel(t1, t0);
}

void helper_ldq_kernel(uint64_t t0, uint64_t t1)
{
    ldq_kernel(t1, t0);
}

void helper_ldl_data(uint64_t t0, uint64_t t1)
{
    ldl_data(t1, t0);
}

void helper_ldq_data(uint64_t t0, uint64_t t1)
{
    ldq_data(t1, t0);
}

void helper_stl_raw(uint64_t t0, uint64_t t1)
{
    stl_raw(t1, t0);
}

void helper_stq_raw(uint64_t t0, uint64_t t1)
{
    stq_raw(t1, t0);
}

uint64_t helper_stl_c_raw(uint64_t t0, uint64_t t1)
{
    uint64_t ret;

    if (t1 == env->lock) {
        stl_raw(t1, t0);
        ret = 0;
    } else
        ret = 1;

    env->lock = 1;

    return ret;
}

uint64_t helper_stq_c_raw(uint64_t t0, uint64_t t1)
{
    uint64_t ret;

    if (t1 == env->lock) {
        stq_raw(t1, t0);
        ret = 0;
    } else
        ret = 1;

    env->lock = 1;

    return ret;
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
}

#define MMUSUFFIX _mmu

#define SHIFT 0
#include "softmmu_template.h"

#define SHIFT 1
#include "softmmu_template.h"

#define SHIFT 2
#include "softmmu_template.h"

#define SHIFT 3
#include "softmmu_template.h"

/* try to fill the TLB and return an exception if error. If retaddr is
   NULL, it means that the function was called in C code (i.e. not
   from generated code or from helper.c) */
/* XXX: fix it to restore all registers */
1181
void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1182
1183
1184
{
    TranslationBlock *tb;
    CPUState *saved_env;
bellard authored
1185
    unsigned long pc;
1186
1187
1188
1189
1190
1191
    int ret;

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;
1192
    ret = cpu_alpha_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1193
1194
1195
    if (!likely(ret == 0)) {
        if (likely(retaddr)) {
            /* now we have a real cpu fault */
bellard authored
1196
            pc = (unsigned long)retaddr;
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
            tb = tb_find_pc(pc);
            if (likely(tb)) {
                /* the PC is inside the translated code. It means that we have
                   a virtual CPU fault */
                cpu_restore_state(tb, env, pc, NULL);
            }
        }
        /* Exception index and error code are already set */
        cpu_loop_exit();
    }
    env = saved_env;
}

#endif