op_helper.c 104.5 KB
Newer Older
1
#include "exec.h"
B
blueswir1 已提交
2
#include "host-utils.h"
B
blueswir1 已提交
3
#include "helper.h"
4 5 6
#if !defined(CONFIG_USER_ONLY)
#include "softmmu_exec.h"
#endif /* !defined(CONFIG_USER_ONLY) */
7

B
bellard 已提交
8
//#define DEBUG_MMU
9
//#define DEBUG_MXCC
B
blueswir1 已提交
10
//#define DEBUG_UNALIGNED
11
//#define DEBUG_UNASSIGNED
12
//#define DEBUG_ASI
B
blueswir1 已提交
13
//#define DEBUG_PCALL
14
//#define DEBUG_PSTATE
B
bellard 已提交
15

16
#ifdef DEBUG_MMU
17 18
#define DPRINTF_MMU(fmt, ...)                                   \
    do { printf("MMU: " fmt , ## __VA_ARGS__); } while (0)
19
#else
20
#define DPRINTF_MMU(fmt, ...) do {} while (0)
21 22 23
#endif

#ifdef DEBUG_MXCC
24 25
#define DPRINTF_MXCC(fmt, ...)                                  \
    do { printf("MXCC: " fmt , ## __VA_ARGS__); } while (0)
26
#else
27
#define DPRINTF_MXCC(fmt, ...) do {} while (0)
28 29
#endif

30
#ifdef DEBUG_ASI
31 32
#define DPRINTF_ASI(fmt, ...)                                   \
    do { printf("ASI: " fmt , ## __VA_ARGS__); } while (0)
33 34
#endif

35 36 37 38 39 40 41
#ifdef DEBUG_PSTATE
#define DPRINTF_PSTATE(fmt, ...)                                   \
    do { printf("PSTATE: " fmt , ## __VA_ARGS__); } while (0)
#else
#define DPRINTF_PSTATE(fmt, ...) do {} while (0)
#endif

B
blueswir1 已提交
42 43 44
#ifdef TARGET_SPARC64
#ifndef TARGET_ABI32
#define AM_CHECK(env1) ((env1)->pstate & PS_AM)
45
#else
B
blueswir1 已提交
46 47
#define AM_CHECK(env1) (1)
#endif
48 49
#endif

50
#if defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
51 52 53 54 55 56
// Calculates TSB pointer value for fault page size 8k or 64k
static uint64_t ultrasparc_tsb_pointer(uint64_t tsb_register,
                                       uint64_t tag_access_register,
                                       int page_size)
{
    uint64_t tsb_base = tsb_register & ~0x1fffULL;
57 58
    int tsb_split = (tsb_register & 0x1000ULL) ? 1 : 0;
    int tsb_size  = tsb_register & 0xf;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    // discard lower 13 bits which hold tag access context
    uint64_t tag_access_va = tag_access_register & ~0x1fffULL;

    // now reorder bits
    uint64_t tsb_base_mask = ~0x1fffULL;
    uint64_t va = tag_access_va;

    // move va bits to correct position
    if (page_size == 8*1024) {
        va >>= 9;
    } else if (page_size == 64*1024) {
        va >>= 12;
    }

    if (tsb_size) {
        tsb_base_mask <<= tsb_size;
    }

    // calculate tsb_base mask and adjust va if split is in use
    if (tsb_split) {
        if (page_size == 8*1024) {
            va &= ~(1ULL << (13 + tsb_size));
        } else if (page_size == 64*1024) {
            va |= (1ULL << (13 + tsb_size));
        }
        tsb_base_mask <<= 1;
    }

    return ((tsb_base & tsb_base_mask) | (va & ~tsb_base_mask)) & ~0xfULL;
}

// Calculates tag target register value by reordering bits
// in tag access register
static uint64_t ultrasparc_tag_target(uint64_t tag_access_register)
{
    return ((tag_access_register & 0x1fff) << 48) | (tag_access_register >> 22);
}

98 99 100
static void replace_tlb_entry(SparcTLBEntry *tlb,
                              uint64_t tlb_tag, uint64_t tlb_tte,
                              CPUState *env1)
101 102 103 104
{
    target_ulong mask, size, va, offset;

    // flush page range if translation is valid
105
    if (TTE_IS_VALID(tlb->tte)) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

        mask = 0xffffffffffffe000ULL;
        mask <<= 3 * ((tlb->tte >> 61) & 3);
        size = ~mask + 1;

        va = tlb->tag & mask;

        for (offset = 0; offset < size; offset += TARGET_PAGE_SIZE) {
            tlb_flush_page(env1, va + offset);
        }
    }

    tlb->tag = tlb_tag;
    tlb->tte = tlb_tte;
}

static void demap_tlb(SparcTLBEntry *tlb, target_ulong demap_addr,
123
                      const char* strmmu, CPUState *env1)
124 125 126 127 128
{
    unsigned int i;
    target_ulong mask;

    for (i = 0; i < 64; i++) {
129
        if (TTE_IS_VALID(tlb[i].tte)) {
130 131 132 133 134

            mask = 0xffffffffffffe000ULL;
            mask <<= 3 * ((tlb[i].tte >> 61) & 3);

            if ((demap_addr & mask) == (tlb[i].tag & mask)) {
135
                replace_tlb_entry(&tlb[i], 0, 0, env1);
136
#ifdef DEBUG_MMU
137 138
                DPRINTF_MMU("%s demap invalidated entry [%02u]\n", strmmu, i);
                dump_mmu(env1);
139 140 141 142 143 144 145 146
#endif
            }
            //return;
        }
    }

}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static void replace_tlb_1bit_lru(SparcTLBEntry *tlb,
                                 uint64_t tlb_tag, uint64_t tlb_tte,
                                 const char* strmmu, CPUState *env1)
{
    unsigned int i, replace_used;

    // Try replacing invalid entry
    for (i = 0; i < 64; i++) {
        if (!TTE_IS_VALID(tlb[i].tte)) {
            replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
#ifdef DEBUG_MMU
            DPRINTF_MMU("%s lru replaced invalid entry [%i]\n", strmmu, i);
            dump_mmu(env1);
#endif
            return;
        }
    }

    // All entries are valid, try replacing unlocked entry

    for (replace_used = 0; replace_used < 2; ++replace_used) {

        // Used entries are not replaced on first pass

        for (i = 0; i < 64; i++) {
            if (!TTE_IS_LOCKED(tlb[i].tte) && !TTE_IS_USED(tlb[i].tte)) {

                replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
#ifdef DEBUG_MMU
                DPRINTF_MMU("%s lru replaced unlocked %s entry [%i]\n",
                            strmmu, (replace_used?"used":"unused"), i);
                dump_mmu(env1);
#endif
                return;
            }
        }

        // Now reset used bit and search for unused entries again

        for (i = 0; i < 64; i++) {
            TTE_SET_UNUSED(tlb[i].tte);
        }
    }

#ifdef DEBUG_MMU
    DPRINTF_MMU("%s lru replacement failed: no entries available\n", strmmu);
#endif
    // error state?
}

197 198
#endif

B
blueswir1 已提交
199 200 201 202 203 204 205 206
static inline void address_mask(CPUState *env1, target_ulong *addr)
{
#ifdef TARGET_SPARC64
    if (AM_CHECK(env1))
        *addr &= 0xffffffffULL;
#endif
}

B
blueswir1 已提交
207
static void raise_exception(int tt)
B
bellard 已提交
208 209 210
{
    env->exception_index = tt;
    cpu_loop_exit();
211
}
B
bellard 已提交
212

P
pbrook 已提交
213 214 215 216 217
void HELPER(raise_exception)(int tt)
{
    raise_exception(tt);
}

B
blueswir1 已提交
218 219 220 221 222
static inline void set_cwp(int new_cwp)
{
    cpu_set_cwp(env, new_cwp);
}

B
blueswir1 已提交
223 224
void helper_check_align(target_ulong addr, uint32_t align)
{
225 226 227 228 229
    if (addr & align) {
#ifdef DEBUG_UNALIGNED
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
           "\n", addr, env->pc);
#endif
B
blueswir1 已提交
230
        raise_exception(TT_UNALIGNED);
231
    }
B
blueswir1 已提交
232 233
}

234 235 236
#define F_HELPER(name, p) void helper_f##name##p(void)

#define F_BINOP(name)                                           \
B
blueswir1 已提交
237
    float32 helper_f ## name ## s (float32 src1, float32 src2)  \
238
    {                                                           \
B
blueswir1 已提交
239
        return float32_ ## name (src1, src2, &env->fp_status);  \
240 241 242 243
    }                                                           \
    F_HELPER(name, d)                                           \
    {                                                           \
        DT0 = float64_ ## name (DT0, DT1, &env->fp_status);     \
B
blueswir1 已提交
244 245 246 247
    }                                                           \
    F_HELPER(name, q)                                           \
    {                                                           \
        QT0 = float128_ ## name (QT0, QT1, &env->fp_status);    \
248 249 250 251 252 253 254 255
    }

F_BINOP(add);
F_BINOP(sub);
F_BINOP(mul);
F_BINOP(div);
#undef F_BINOP

256
void helper_fsmuld(float32 src1, float32 src2)
B
blueswir1 已提交
257
{
258 259
    DT0 = float64_mul(float32_to_float64(src1, &env->fp_status),
                      float32_to_float64(src2, &env->fp_status),
260 261
                      &env->fp_status);
}
B
blueswir1 已提交
262

B
blueswir1 已提交
263 264 265 266 267 268 269
void helper_fdmulq(void)
{
    QT0 = float128_mul(float64_to_float128(DT0, &env->fp_status),
                       float64_to_float128(DT1, &env->fp_status),
                       &env->fp_status);
}

B
blueswir1 已提交
270
float32 helper_fnegs(float32 src)
271
{
B
blueswir1 已提交
272
    return float32_chs(src);
273 274
}

275 276
#ifdef TARGET_SPARC64
F_HELPER(neg, d)
277
{
278
    DT0 = float64_chs(DT1);
279
}
B
blueswir1 已提交
280 281 282 283 284 285

F_HELPER(neg, q)
{
    QT0 = float128_chs(QT1);
}
#endif
286 287

/* Integer to float conversion.  */
B
blueswir1 已提交
288
float32 helper_fitos(int32_t src)
B
bellard 已提交
289
{
B
blueswir1 已提交
290
    return int32_to_float32(src, &env->fp_status);
B
bellard 已提交
291 292
}

293
void helper_fitod(int32_t src)
B
bellard 已提交
294
{
295
    DT0 = int32_to_float64(src, &env->fp_status);
B
bellard 已提交
296
}
297

298
void helper_fitoq(int32_t src)
B
blueswir1 已提交
299
{
300
    QT0 = int32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
301 302
}

B
blueswir1 已提交
303
#ifdef TARGET_SPARC64
304
float32 helper_fxtos(void)
B
blueswir1 已提交
305
{
306
    return int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
B
blueswir1 已提交
307 308
}

309
F_HELPER(xto, d)
B
blueswir1 已提交
310 311 312
{
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
}
B
blueswir1 已提交
313

B
blueswir1 已提交
314 315 316 317 318
F_HELPER(xto, q)
{
    QT0 = int64_to_float128(*((int64_t *)&DT1), &env->fp_status);
}
#endif
319 320 321
#undef F_HELPER

/* floating point conversion */
322
float32 helper_fdtos(void)
323
{
324
    return float64_to_float32(DT1, &env->fp_status);
325 326
}

327
void helper_fstod(float32 src)
328
{
329
    DT0 = float32_to_float64(src, &env->fp_status);
330
}
331

332
float32 helper_fqtos(void)
B
blueswir1 已提交
333
{
334
    return float128_to_float32(QT1, &env->fp_status);
B
blueswir1 已提交
335 336
}

337
void helper_fstoq(float32 src)
B
blueswir1 已提交
338
{
339
    QT0 = float32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
340 341 342 343 344 345 346 347 348 349 350 351
}

void helper_fqtod(void)
{
    DT0 = float128_to_float64(QT1, &env->fp_status);
}

void helper_fdtoq(void)
{
    QT0 = float64_to_float128(DT1, &env->fp_status);
}

352
/* Float to integer conversion.  */
B
blueswir1 已提交
353
int32_t helper_fstoi(float32 src)
354
{
B
blueswir1 已提交
355
    return float32_to_int32_round_to_zero(src, &env->fp_status);
356 357
}

358
int32_t helper_fdtoi(void)
359
{
360
    return float64_to_int32_round_to_zero(DT1, &env->fp_status);
361 362
}

363
int32_t helper_fqtoi(void)
B
blueswir1 已提交
364
{
365
    return float128_to_int32_round_to_zero(QT1, &env->fp_status);
B
blueswir1 已提交
366 367
}

368
#ifdef TARGET_SPARC64
369
void helper_fstox(float32 src)
370
{
371
    *((int64_t *)&DT0) = float32_to_int64_round_to_zero(src, &env->fp_status);
372 373 374 375 376 377 378
}

void helper_fdtox(void)
{
    *((int64_t *)&DT0) = float64_to_int64_round_to_zero(DT1, &env->fp_status);
}

B
blueswir1 已提交
379 380 381 382 383
void helper_fqtox(void)
{
    *((int64_t *)&DT0) = float128_to_int64_round_to_zero(QT1, &env->fp_status);
}

384 385 386 387 388
void helper_faligndata(void)
{
    uint64_t tmp;

    tmp = (*((uint64_t *)&DT0)) << ((env->gsr & 7) * 8);
B
blueswir1 已提交
389 390 391 392
    /* on many architectures a shift of 64 does nothing */
    if ((env->gsr & 7) != 0) {
        tmp |= (*((uint64_t *)&DT1)) >> (64 - (env->gsr & 7) * 8);
    }
393 394 395
    *((uint64_t *)&DT0) = tmp;
}

396
#ifdef HOST_WORDS_BIGENDIAN
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
#define VIS_B64(n) b[7 - (n)]
#define VIS_W64(n) w[3 - (n)]
#define VIS_SW64(n) sw[3 - (n)]
#define VIS_L64(n) l[1 - (n)]
#define VIS_B32(n) b[3 - (n)]
#define VIS_W32(n) w[1 - (n)]
#else
#define VIS_B64(n) b[n]
#define VIS_W64(n) w[n]
#define VIS_SW64(n) sw[n]
#define VIS_L64(n) l[n]
#define VIS_B32(n) b[n]
#define VIS_W32(n) w[n]
#endif

typedef union {
    uint8_t b[8];
    uint16_t w[4];
    int16_t sw[4];
    uint32_t l[2];
    float64 d;
} vis64;

typedef union {
    uint8_t b[4];
    uint16_t w[2];
    uint32_t l;
    float32 f;
} vis32;

void helper_fpmerge(void)
{
    vis64 s, d;

    s.d = DT0;
    d.d = DT1;

    // Reverse calculation order to handle overlap
    d.VIS_B64(7) = s.VIS_B64(3);
    d.VIS_B64(6) = d.VIS_B64(3);
    d.VIS_B64(5) = s.VIS_B64(2);
    d.VIS_B64(4) = d.VIS_B64(2);
    d.VIS_B64(3) = s.VIS_B64(1);
    d.VIS_B64(2) = d.VIS_B64(1);
    d.VIS_B64(1) = s.VIS_B64(0);
    //d.VIS_B64(0) = d.VIS_B64(0);

    DT0 = d.d;
}

void helper_fmul8x16(void)
{
    vis64 s, d;
    uint32_t tmp;

    s.d = DT0;
    d.d = DT1;

#define PMUL(r)                                                 \
    tmp = (int32_t)d.VIS_SW64(r) * (int32_t)s.VIS_B64(r);       \
    if ((tmp & 0xff) > 0x7f)                                    \
        tmp += 0x100;                                           \
    d.VIS_W64(r) = tmp >> 8;

    PMUL(0);
    PMUL(1);
    PMUL(2);
    PMUL(3);
#undef PMUL

    DT0 = d.d;
}

void helper_fmul8x16al(void)
{
    vis64 s, d;
    uint32_t tmp;

    s.d = DT0;
    d.d = DT1;

#define PMUL(r)                                                 \
    tmp = (int32_t)d.VIS_SW64(1) * (int32_t)s.VIS_B64(r);       \
    if ((tmp & 0xff) > 0x7f)                                    \
        tmp += 0x100;                                           \
    d.VIS_W64(r) = tmp >> 8;

    PMUL(0);
    PMUL(1);
    PMUL(2);
    PMUL(3);
#undef PMUL

    DT0 = d.d;
}

void helper_fmul8x16au(void)
{
    vis64 s, d;
    uint32_t tmp;

    s.d = DT0;
    d.d = DT1;

#define PMUL(r)                                                 \
    tmp = (int32_t)d.VIS_SW64(0) * (int32_t)s.VIS_B64(r);       \
    if ((tmp & 0xff) > 0x7f)                                    \
        tmp += 0x100;                                           \
    d.VIS_W64(r) = tmp >> 8;

    PMUL(0);
    PMUL(1);
    PMUL(2);
    PMUL(3);
#undef PMUL

    DT0 = d.d;
}

void helper_fmul8sux16(void)
{
    vis64 s, d;
    uint32_t tmp;

    s.d = DT0;
    d.d = DT1;

#define PMUL(r)                                                         \
    tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
    if ((tmp & 0xff) > 0x7f)                                            \
        tmp += 0x100;                                                   \
    d.VIS_W64(r) = tmp >> 8;

    PMUL(0);
    PMUL(1);
    PMUL(2);
    PMUL(3);
#undef PMUL

    DT0 = d.d;
}

void helper_fmul8ulx16(void)
{
    vis64 s, d;
    uint32_t tmp;

    s.d = DT0;
    d.d = DT1;

#define PMUL(r)                                                         \
    tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
    if ((tmp & 0xff) > 0x7f)                                            \
        tmp += 0x100;                                                   \
    d.VIS_W64(r) = tmp >> 8;

    PMUL(0);
    PMUL(1);
    PMUL(2);
    PMUL(3);
#undef PMUL

    DT0 = d.d;
}

void helper_fmuld8sux16(void)
{
    vis64 s, d;
    uint32_t tmp;

    s.d = DT0;
    d.d = DT1;

#define PMUL(r)                                                         \
    tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
    if ((tmp & 0xff) > 0x7f)                                            \
        tmp += 0x100;                                                   \
    d.VIS_L64(r) = tmp;

    // Reverse calculation order to handle overlap
    PMUL(1);
    PMUL(0);
#undef PMUL

    DT0 = d.d;
}

void helper_fmuld8ulx16(void)
{
    vis64 s, d;
    uint32_t tmp;

    s.d = DT0;
    d.d = DT1;

#define PMUL(r)                                                         \
    tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
    if ((tmp & 0xff) > 0x7f)                                            \
        tmp += 0x100;                                                   \
    d.VIS_L64(r) = tmp;

    // Reverse calculation order to handle overlap
    PMUL(1);
    PMUL(0);
#undef PMUL

    DT0 = d.d;
}

void helper_fexpand(void)
{
    vis32 s;
    vis64 d;

    s.l = (uint32_t)(*(uint64_t *)&DT0 & 0xffffffff);
    d.d = DT1;
613 614 615 616
    d.VIS_W64(0) = s.VIS_B32(0) << 4;
    d.VIS_W64(1) = s.VIS_B32(1) << 4;
    d.VIS_W64(2) = s.VIS_B32(2) << 4;
    d.VIS_W64(3) = s.VIS_B32(3) << 4;
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

    DT0 = d.d;
}

#define VIS_HELPER(name, F)                             \
    void name##16(void)                                 \
    {                                                   \
        vis64 s, d;                                     \
                                                        \
        s.d = DT0;                                      \
        d.d = DT1;                                      \
                                                        \
        d.VIS_W64(0) = F(d.VIS_W64(0), s.VIS_W64(0));   \
        d.VIS_W64(1) = F(d.VIS_W64(1), s.VIS_W64(1));   \
        d.VIS_W64(2) = F(d.VIS_W64(2), s.VIS_W64(2));   \
        d.VIS_W64(3) = F(d.VIS_W64(3), s.VIS_W64(3));   \
                                                        \
        DT0 = d.d;                                      \
    }                                                   \
                                                        \
B
blueswir1 已提交
637
    uint32_t name##16s(uint32_t src1, uint32_t src2)    \
638 639 640
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
641 642
        s.l = src1;                                     \
        d.l = src2;                                     \
643 644 645 646
                                                        \
        d.VIS_W32(0) = F(d.VIS_W32(0), s.VIS_W32(0));   \
        d.VIS_W32(1) = F(d.VIS_W32(1), s.VIS_W32(1));   \
                                                        \
B
blueswir1 已提交
647
        return d.l;                                     \
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    }                                                   \
                                                        \
    void name##32(void)                                 \
    {                                                   \
        vis64 s, d;                                     \
                                                        \
        s.d = DT0;                                      \
        d.d = DT1;                                      \
                                                        \
        d.VIS_L64(0) = F(d.VIS_L64(0), s.VIS_L64(0));   \
        d.VIS_L64(1) = F(d.VIS_L64(1), s.VIS_L64(1));   \
                                                        \
        DT0 = d.d;                                      \
    }                                                   \
                                                        \
B
blueswir1 已提交
663
    uint32_t name##32s(uint32_t src1, uint32_t src2)    \
664 665 666
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
667 668
        s.l = src1;                                     \
        d.l = src2;                                     \
669 670 671
                                                        \
        d.l = F(d.l, s.l);                              \
                                                        \
B
blueswir1 已提交
672
        return d.l;                                     \
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    }

#define FADD(a, b) ((a) + (b))
#define FSUB(a, b) ((a) - (b))
VIS_HELPER(helper_fpadd, FADD)
VIS_HELPER(helper_fpsub, FSUB)

#define VIS_CMPHELPER(name, F)                                        \
    void name##16(void)                                           \
    {                                                             \
        vis64 s, d;                                               \
                                                                  \
        s.d = DT0;                                                \
        d.d = DT1;                                                \
                                                                  \
        d.VIS_W64(0) = F(d.VIS_W64(0), s.VIS_W64(0))? 1: 0;       \
        d.VIS_W64(0) |= F(d.VIS_W64(1), s.VIS_W64(1))? 2: 0;      \
        d.VIS_W64(0) |= F(d.VIS_W64(2), s.VIS_W64(2))? 4: 0;      \
        d.VIS_W64(0) |= F(d.VIS_W64(3), s.VIS_W64(3))? 8: 0;      \
                                                                  \
        DT0 = d.d;                                                \
    }                                                             \
                                                                  \
    void name##32(void)                                           \
    {                                                             \
        vis64 s, d;                                               \
                                                                  \
        s.d = DT0;                                                \
        d.d = DT1;                                                \
                                                                  \
        d.VIS_L64(0) = F(d.VIS_L64(0), s.VIS_L64(0))? 1: 0;       \
        d.VIS_L64(0) |= F(d.VIS_L64(1), s.VIS_L64(1))? 2: 0;      \
                                                                  \
        DT0 = d.d;                                                \
    }

#define FCMPGT(a, b) ((a) > (b))
#define FCMPEQ(a, b) ((a) == (b))
#define FCMPLE(a, b) ((a) <= (b))
#define FCMPNE(a, b) ((a) != (b))

VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
VIS_CMPHELPER(helper_fcmple, FCMPLE)
VIS_CMPHELPER(helper_fcmpne, FCMPNE)
#endif

void helper_check_ieee_exceptions(void)
{
    target_ulong status;

    status = get_float_exception_flags(&env->fp_status);
    if (status) {
        /* Copy IEEE 754 flags into FSR */
        if (status & float_flag_invalid)
            env->fsr |= FSR_NVC;
        if (status & float_flag_overflow)
            env->fsr |= FSR_OFC;
        if (status & float_flag_underflow)
            env->fsr |= FSR_UFC;
        if (status & float_flag_divbyzero)
            env->fsr |= FSR_DZC;
        if (status & float_flag_inexact)
            env->fsr |= FSR_NXC;

        if ((env->fsr & FSR_CEXC_MASK) & ((env->fsr & FSR_TEM_MASK) >> 23)) {
            /* Unmasked exception, generate a trap */
            env->fsr |= FSR_FTT_IEEE_EXCP;
            raise_exception(TT_FP_EXCP);
        } else {
            /* Accumulate exceptions */
            env->fsr |= (env->fsr & FSR_CEXC_MASK) << 5;
        }
    }
}

void helper_clear_float_exceptions(void)
{
    set_float_exception_flags(0, &env->fp_status);
}

B
blueswir1 已提交
754
float32 helper_fabss(float32 src)
755
{
B
blueswir1 已提交
756
    return float32_abs(src);
757 758
}

B
bellard 已提交
759
#ifdef TARGET_SPARC64
760
void helper_fabsd(void)
B
bellard 已提交
761 762 763
{
    DT0 = float64_abs(DT1);
}
B
blueswir1 已提交
764 765 766 767 768 769

void helper_fabsq(void)
{
    QT0 = float128_abs(QT1);
}
#endif
B
bellard 已提交
770

B
blueswir1 已提交
771
float32 helper_fsqrts(float32 src)
772
{
B
blueswir1 已提交
773
    return float32_sqrt(src, &env->fp_status);
774 775
}

776
void helper_fsqrtd(void)
777
{
B
bellard 已提交
778
    DT0 = float64_sqrt(DT1, &env->fp_status);
779 780
}

B
blueswir1 已提交
781 782 783 784 785
void helper_fsqrtq(void)
{
    QT0 = float128_sqrt(QT1, &env->fp_status);
}

786
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
787
    void glue(helper_, name) (void)                                     \
B
bellard 已提交
788
    {                                                                   \
B
blueswir1 已提交
789 790
        target_ulong new_fsr;                                           \
                                                                        \
B
bellard 已提交
791 792 793
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
B
blueswir1 已提交
794
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
795
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
B
blueswir1 已提交
796
                env->fsr |= new_fsr;                                    \
797 798
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
B
bellard 已提交
799 800 801 802 803 804
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
B
blueswir1 已提交
805
            new_fsr = FSR_FCC0 << FS;                                   \
B
bellard 已提交
806 807
            break;                                                      \
        case float_relation_greater:                                    \
B
blueswir1 已提交
808
            new_fsr = FSR_FCC1 << FS;                                   \
B
bellard 已提交
809 810
            break;                                                      \
        default:                                                        \
B
blueswir1 已提交
811
            new_fsr = 0;                                                \
B
bellard 已提交
812 813
            break;                                                      \
        }                                                               \
B
blueswir1 已提交
814
        env->fsr |= new_fsr;                                            \
815
    }
B
blueswir1 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
#define GEN_FCMPS(name, size, FS, TRAP)                                 \
    void glue(helper_, name)(float32 src1, float32 src2)                \
    {                                                                   \
        target_ulong new_fsr;                                           \
                                                                        \
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (src1, src2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
                env->fsr |= new_fsr;                                    \
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
            new_fsr = FSR_FCC0 << FS;                                   \
            break;                                                      \
        case float_relation_greater:                                    \
            new_fsr = FSR_FCC1 << FS;                                   \
            break;                                                      \
        default:                                                        \
            new_fsr = 0;                                                \
            break;                                                      \
        }                                                               \
        env->fsr |= new_fsr;                                            \
    }
846

B
blueswir1 已提交
847
GEN_FCMPS(fcmps, float32, 0, 0);
848 849
GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);

B
blueswir1 已提交
850
GEN_FCMPS(fcmpes, float32, 0, 1);
851
GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
B
bellard 已提交
852

B
blueswir1 已提交
853 854 855
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);

856 857 858 859 860 861 862 863 864 865
static uint32_t compute_all_flags(void)
{
    return env->psr & PSR_ICC;
}

static uint32_t compute_C_flags(void)
{
    return env->psr & PSR_CARRY;
}

B
Blue Swirl 已提交
866 867 868 869 870 871 872 873 874 875 876
static inline uint32_t get_NZ_icc(target_ulong dst)
{
    uint32_t ret = 0;

    if (!(dst & 0xffffffffULL))
        ret |= PSR_ZERO;
    if ((int32_t) (dst & 0xffffffffULL) < 0)
        ret |= PSR_NEG;
    return ret;
}

877 878 879 880 881 882 883 884 885 886 887
#ifdef TARGET_SPARC64
static uint32_t compute_all_flags_xcc(void)
{
    return env->xcc & PSR_ICC;
}

static uint32_t compute_C_flags_xcc(void)
{
    return env->xcc & PSR_CARRY;
}

B
Blue Swirl 已提交
888 889 890 891 892 893 894 895 896 897 898 899
static inline uint32_t get_NZ_xcc(target_ulong dst)
{
    uint32_t ret = 0;

    if (!dst)
        ret |= PSR_ZERO;
    if ((int64_t)dst < 0)
        ret |= PSR_NEG;
    return ret;
}
#endif

B
Blue Swirl 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
static inline uint32_t get_V_div_icc(target_ulong src2)
{
    uint32_t ret = 0;

    if (src2 != 0)
        ret |= PSR_OVF;
    return ret;
}

static uint32_t compute_all_div(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
    ret |= get_V_div_icc(CC_SRC2);
    return ret;
}

static uint32_t compute_C_div(void)
{
    return 0;
}

923 924 925
/* carry = (src1[31] & src2[31]) | ( ~dst[31] & (src1[31] | src2[31])) */
static inline uint32_t get_C_add_icc(target_ulong dst, target_ulong src1,
                                     target_ulong src2)
B
Blue Swirl 已提交
926 927 928
{
    uint32_t ret = 0;

929 930 931
    if (((src1 & (1ULL << 31)) & (src2 & (1ULL << 31)))
        | ((~(dst & (1ULL << 31)))
           & ((src1 & (1ULL << 31)) | (src2 & (1ULL << 31)))))
B
Blue Swirl 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
        ret |= PSR_CARRY;
    return ret;
}

static inline uint32_t get_V_add_icc(target_ulong dst, target_ulong src1,
                                         target_ulong src2)
{
    uint32_t ret = 0;

    if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1ULL << 31))
        ret |= PSR_OVF;
    return ret;
}

#ifdef TARGET_SPARC64
static inline uint32_t get_C_add_xcc(target_ulong dst, target_ulong src1)
{
    uint32_t ret = 0;

    if (dst < src1)
        ret |= PSR_CARRY;
    return ret;
}

static inline uint32_t get_V_add_xcc(target_ulong dst, target_ulong src1,
                                         target_ulong src2)
{
    uint32_t ret = 0;

    if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1ULL << 63))
        ret |= PSR_OVF;
    return ret;
}

static uint32_t compute_all_add_xcc(void)
{
    uint32_t ret;

    ret = get_NZ_xcc(CC_DST);
    ret |= get_C_add_xcc(CC_DST, CC_SRC);
    ret |= get_V_add_xcc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_add_xcc(void)
{
    return get_C_add_xcc(CC_DST, CC_SRC);
}
980 981
#endif

982
static uint32_t compute_all_add(void)
B
Blue Swirl 已提交
983 984 985 986
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
987
    ret |= get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
988 989 990 991
    ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

992
static uint32_t compute_C_add(void)
B
Blue Swirl 已提交
993
{
994
    return get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
}

#ifdef TARGET_SPARC64
static uint32_t compute_all_addx_xcc(void)
{
    uint32_t ret;

    ret = get_NZ_xcc(CC_DST);
    ret |= get_C_add_xcc(CC_DST - CC_SRC2, CC_SRC);
    ret |= get_C_add_xcc(CC_DST, CC_SRC);
    ret |= get_V_add_xcc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_addx_xcc(void)
{
    uint32_t ret;

    ret = get_C_add_xcc(CC_DST - CC_SRC2, CC_SRC);
    ret |= get_C_add_xcc(CC_DST, CC_SRC);
    return ret;
}
#endif

B
Blue Swirl 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
static inline uint32_t get_V_tag_icc(target_ulong src1, target_ulong src2)
{
    uint32_t ret = 0;

    if ((src1 | src2) & 0x3)
        ret |= PSR_OVF;
    return ret;
}

static uint32_t compute_all_tadd(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1033
    ret |= get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1034 1035 1036 1037 1038 1039 1040
    ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
    ret |= get_V_tag_icc(CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_tadd(void)
{
1041
    return get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1042 1043 1044 1045 1046 1047 1048
}

static uint32_t compute_all_taddtv(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1049
    ret |= get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1050 1051 1052 1053 1054
    return ret;
}

static uint32_t compute_C_taddtv(void)
{
1055
    return get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1056 1057
}

1058 1059 1060
/* carry = (~src1[31] & src2[31]) | ( dst[31]  & (~src1[31] | src2[31])) */
static inline uint32_t get_C_sub_icc(target_ulong dst, target_ulong src1,
                                     target_ulong src2)
B
Blue Swirl 已提交
1061 1062 1063
{
    uint32_t ret = 0;

1064 1065 1066
    if (((~(src1 & (1ULL << 31))) & (src2 & (1ULL << 31)))
        | ((dst & (1ULL << 31)) & (( ~(src1 & (1ULL << 31)))
                                   | (src2 & (1ULL << 31)))))
B
Blue Swirl 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 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
        ret |= PSR_CARRY;
    return ret;
}

static inline uint32_t get_V_sub_icc(target_ulong dst, target_ulong src1,
                                     target_ulong src2)
{
    uint32_t ret = 0;

    if (((src1 ^ src2) & (src1 ^ dst)) & (1ULL << 31))
        ret |= PSR_OVF;
    return ret;
}


#ifdef TARGET_SPARC64
static inline uint32_t get_C_sub_xcc(target_ulong src1, target_ulong src2)
{
    uint32_t ret = 0;

    if (src1 < src2)
        ret |= PSR_CARRY;
    return ret;
}

static inline uint32_t get_V_sub_xcc(target_ulong dst, target_ulong src1,
                                     target_ulong src2)
{
    uint32_t ret = 0;

    if (((src1 ^ src2) & (src1 ^ dst)) & (1ULL << 63))
        ret |= PSR_OVF;
    return ret;
}

static uint32_t compute_all_sub_xcc(void)
{
    uint32_t ret;

    ret = get_NZ_xcc(CC_DST);
    ret |= get_C_sub_xcc(CC_SRC, CC_SRC2);
    ret |= get_V_sub_xcc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_sub_xcc(void)
{
    return get_C_sub_xcc(CC_SRC, CC_SRC2);
}
#endif

1118
static uint32_t compute_all_sub(void)
B
Blue Swirl 已提交
1119 1120 1121 1122
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1123
    ret |= get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1124 1125 1126 1127
    ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

1128
static uint32_t compute_C_sub(void)
B
Blue Swirl 已提交
1129
{
1130
    return get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
}

#ifdef TARGET_SPARC64
static uint32_t compute_all_subx_xcc(void)
{
    uint32_t ret;

    ret = get_NZ_xcc(CC_DST);
    ret |= get_C_sub_xcc(CC_DST - CC_SRC2, CC_SRC);
    ret |= get_C_sub_xcc(CC_DST, CC_SRC2);
    ret |= get_V_sub_xcc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_subx_xcc(void)
{
    uint32_t ret;

    ret = get_C_sub_xcc(CC_DST - CC_SRC2, CC_SRC);
    ret |= get_C_sub_xcc(CC_DST, CC_SRC2);
    return ret;
}
#endif

B
Blue Swirl 已提交
1155 1156 1157 1158 1159
static uint32_t compute_all_tsub(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1160
    ret |= get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1161 1162 1163 1164 1165 1166 1167
    ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
    ret |= get_V_tag_icc(CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_tsub(void)
{
1168
    return get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1169 1170 1171 1172 1173 1174 1175
}

static uint32_t compute_all_tsubtv(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1176
    ret |= get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1177 1178 1179 1180 1181
    return ret;
}

static uint32_t compute_C_tsubtv(void)
{
1182
    return get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1183 1184
}

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
static uint32_t compute_all_logic(void)
{
    return get_NZ_icc(CC_DST);
}

static uint32_t compute_C_logic(void)
{
    return 0;
}

#ifdef TARGET_SPARC64
static uint32_t compute_all_logic_xcc(void)
{
    return get_NZ_xcc(CC_DST);
}
#endif

1202 1203 1204 1205 1206 1207 1208 1209
typedef struct CCTable {
    uint32_t (*compute_all)(void); /* return all the flags */
    uint32_t (*compute_c)(void);  /* return the C flag */
} CCTable;

static const CCTable icc_table[CC_OP_NB] = {
    /* CC_OP_DYNAMIC should never happen */
    [CC_OP_FLAGS] = { compute_all_flags, compute_C_flags },
B
Blue Swirl 已提交
1210
    [CC_OP_DIV] = { compute_all_div, compute_C_div },
B
Blue Swirl 已提交
1211
    [CC_OP_ADD] = { compute_all_add, compute_C_add },
1212
    [CC_OP_ADDX] = { compute_all_add, compute_C_add },
B
Blue Swirl 已提交
1213 1214
    [CC_OP_TADD] = { compute_all_tadd, compute_C_tadd },
    [CC_OP_TADDTV] = { compute_all_taddtv, compute_C_taddtv },
B
Blue Swirl 已提交
1215
    [CC_OP_SUB] = { compute_all_sub, compute_C_sub },
1216
    [CC_OP_SUBX] = { compute_all_sub, compute_C_sub },
B
Blue Swirl 已提交
1217 1218
    [CC_OP_TSUB] = { compute_all_tsub, compute_C_tsub },
    [CC_OP_TSUBTV] = { compute_all_tsubtv, compute_C_tsubtv },
1219
    [CC_OP_LOGIC] = { compute_all_logic, compute_C_logic },
1220 1221 1222 1223 1224 1225
};

#ifdef TARGET_SPARC64
static const CCTable xcc_table[CC_OP_NB] = {
    /* CC_OP_DYNAMIC should never happen */
    [CC_OP_FLAGS] = { compute_all_flags_xcc, compute_C_flags_xcc },
B
Blue Swirl 已提交
1226
    [CC_OP_DIV] = { compute_all_logic_xcc, compute_C_logic },
B
Blue Swirl 已提交
1227
    [CC_OP_ADD] = { compute_all_add_xcc, compute_C_add_xcc },
B
Blue Swirl 已提交
1228
    [CC_OP_ADDX] = { compute_all_addx_xcc, compute_C_addx_xcc },
B
Blue Swirl 已提交
1229 1230
    [CC_OP_TADD] = { compute_all_add_xcc, compute_C_add_xcc },
    [CC_OP_TADDTV] = { compute_all_add_xcc, compute_C_add_xcc },
B
Blue Swirl 已提交
1231
    [CC_OP_SUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
B
Blue Swirl 已提交
1232
    [CC_OP_SUBX] = { compute_all_subx_xcc, compute_C_subx_xcc },
B
Blue Swirl 已提交
1233 1234
    [CC_OP_TSUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
    [CC_OP_TSUBTV] = { compute_all_sub_xcc, compute_C_sub_xcc },
1235
    [CC_OP_LOGIC] = { compute_all_logic_xcc, compute_C_logic },
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
};
#endif

void helper_compute_psr(void)
{
    uint32_t new_psr;

    new_psr = icc_table[CC_OP].compute_all();
    env->psr = new_psr;
#ifdef TARGET_SPARC64
    new_psr = xcc_table[CC_OP].compute_all();
    env->xcc = new_psr;
#endif
    CC_OP = CC_OP_FLAGS;
}

uint32_t helper_compute_C_icc(void)
{
    uint32_t ret;

    ret = icc_table[CC_OP].compute_c() >> PSR_CARRY_SHIFT;
    return ret;
}

B
bellard 已提交
1260
#ifdef TARGET_SPARC64
B
blueswir1 已提交
1261
GEN_FCMPS(fcmps_fcc1, float32, 22, 0);
1262
GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
B
blueswir1 已提交
1263
GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
1264

B
blueswir1 已提交
1265
GEN_FCMPS(fcmps_fcc2, float32, 24, 0);
1266
GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
B
blueswir1 已提交
1267
GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
1268

B
blueswir1 已提交
1269
GEN_FCMPS(fcmps_fcc3, float32, 26, 0);
1270
GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
B
blueswir1 已提交
1271
GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
1272

B
blueswir1 已提交
1273
GEN_FCMPS(fcmpes_fcc1, float32, 22, 1);
1274
GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
B
blueswir1 已提交
1275
GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
B
bellard 已提交
1276

B
blueswir1 已提交
1277
GEN_FCMPS(fcmpes_fcc2, float32, 24, 1);
1278
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
B
blueswir1 已提交
1279
GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
B
bellard 已提交
1280

B
blueswir1 已提交
1281
GEN_FCMPS(fcmpes_fcc3, float32, 26, 1);
1282
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
B
blueswir1 已提交
1283 1284
GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
#endif
B
blueswir1 已提交
1285
#undef GEN_FCMPS
B
bellard 已提交
1286

B
blueswir1 已提交
1287 1288
#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
    defined(DEBUG_MXCC)
1289 1290
static void dump_mxcc(CPUState *env)
{
1291 1292
    printf("mxccdata: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n",
B
blueswir1 已提交
1293 1294
           env->mxccdata[0], env->mxccdata[1],
           env->mxccdata[2], env->mxccdata[3]);
1295 1296 1297 1298
    printf("mxccregs: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n"
           "          %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n",
B
blueswir1 已提交
1299 1300 1301 1302
           env->mxccregs[0], env->mxccregs[1],
           env->mxccregs[2], env->mxccregs[3],
           env->mxccregs[4], env->mxccregs[5],
           env->mxccregs[6], env->mxccregs[7]);
1303 1304 1305
}
#endif

B
blueswir1 已提交
1306 1307 1308 1309
#if (defined(TARGET_SPARC64) || !defined(CONFIG_USER_ONLY)) \
    && defined(DEBUG_ASI)
static void dump_asi(const char *txt, target_ulong addr, int asi, int size,
                     uint64_t r1)
1310 1311 1312 1313
{
    switch (size)
    {
    case 1:
B
blueswir1 已提交
1314 1315
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xff);
1316 1317
        break;
    case 2:
B
blueswir1 已提交
1318 1319
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffff);
1320 1321
        break;
    case 4:
B
blueswir1 已提交
1322 1323
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffffffff);
1324 1325
        break;
    case 8:
B
blueswir1 已提交
1326 1327
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
                    addr, asi, r1);
1328 1329 1330 1331 1332
        break;
    }
}
#endif

B
blueswir1 已提交
1333 1334 1335
#ifndef TARGET_SPARC64
#ifndef CONFIG_USER_ONLY
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1336
{
B
blueswir1 已提交
1337
    uint64_t ret = 0;
1338
#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
B
blueswir1 已提交
1339
    uint32_t last_addr = addr;
1340
#endif
B
bellard 已提交
1341

1342
    helper_check_align(addr, size - 1);
B
bellard 已提交
1343
    switch (asi) {
1344
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
1345
        switch (addr) {
1346
        case 0x01c00a00: /* MXCC control register */
B
blueswir1 已提交
1347 1348 1349
            if (size == 8)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1350 1351
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1352 1353 1354 1355 1356
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1357 1358
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1359
            break;
1360 1361
        case 0x01c00c00: /* Module reset register */
            if (size == 8) {
B
blueswir1 已提交
1362
                ret = env->mxccregs[5];
1363 1364
                // should we do something here?
            } else
B
blueswir1 已提交
1365 1366
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1367
            break;
1368
        case 0x01c00f00: /* MBus port address register */
B
blueswir1 已提交
1369 1370 1371
            if (size == 8)
                ret = env->mxccregs[7];
            else
B
blueswir1 已提交
1372 1373
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1374 1375
            break;
        default:
B
blueswir1 已提交
1376 1377
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1378 1379
            break;
        }
B
blueswir1 已提交
1380
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
1381
                     "addr = %08x -> ret = %" PRIx64 ","
B
blueswir1 已提交
1382
                     "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
1383 1384 1385
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1386
        break;
1387
    case 3: /* MMU probe */
B
blueswir1 已提交
1388 1389 1390
        {
            int mmulev;

B
blueswir1 已提交
1391
            mmulev = (addr >> 8) & 15;
B
blueswir1 已提交
1392 1393
            if (mmulev > 4)
                ret = 0;
B
blueswir1 已提交
1394 1395 1396 1397
            else
                ret = mmu_probe(env, addr, mmulev);
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
                        addr, mmulev, ret);
B
blueswir1 已提交
1398 1399
        }
        break;
1400
    case 4: /* read MMU regs */
B
blueswir1 已提交
1401
        {
B
blueswir1 已提交
1402
            int reg = (addr >> 8) & 0x1f;
1403

B
blueswir1 已提交
1404 1405
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
B
blueswir1 已提交
1406 1407 1408 1409 1410
                env->mmuregs[3] = 0;
            else if (reg == 0x13) /* Fault status read */
                ret = env->mmuregs[3];
            else if (reg == 0x14) /* Fault address read */
                ret = env->mmuregs[4];
B
blueswir1 已提交
1411
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
B
blueswir1 已提交
1412 1413
        }
        break;
B
blueswir1 已提交
1414 1415 1416 1417
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1418 1419 1420
    case 9: /* Supervisor code access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1421
            ret = ldub_code(addr);
1422 1423
            break;
        case 2:
1424
            ret = lduw_code(addr);
1425 1426 1427
            break;
        default:
        case 4:
1428
            ret = ldl_code(addr);
1429 1430
            break;
        case 8:
1431
            ret = ldq_code(addr);
1432 1433 1434
            break;
        }
        break;
1435 1436 1437
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1438
            ret = ldub_user(addr);
1439 1440
            break;
        case 2:
1441
            ret = lduw_user(addr);
1442 1443 1444
            break;
        default:
        case 4:
1445
            ret = ldl_user(addr);
1446 1447
            break;
        case 8:
1448
            ret = ldq_user(addr);
1449 1450 1451 1452 1453 1454
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1455
            ret = ldub_kernel(addr);
1456 1457
            break;
        case 2:
1458
            ret = lduw_kernel(addr);
1459 1460 1461
            break;
        default:
        case 4:
1462
            ret = ldl_kernel(addr);
1463 1464
            break;
        case 8:
1465
            ret = ldq_kernel(addr);
1466 1467 1468
            break;
        }
        break;
1469 1470 1471 1472 1473 1474
    case 0xc: /* I-cache tag */
    case 0xd: /* I-cache data */
    case 0xe: /* D-cache tag */
    case 0xf: /* D-cache data */
        break;
    case 0x20: /* MMU passthrough */
B
bellard 已提交
1475 1476
        switch(size) {
        case 1:
B
blueswir1 已提交
1477
            ret = ldub_phys(addr);
B
bellard 已提交
1478 1479
            break;
        case 2:
1480
            ret = lduw_phys(addr);
B
bellard 已提交
1481 1482 1483
            break;
        default:
        case 4:
1484
            ret = ldl_phys(addr);
B
bellard 已提交
1485
            break;
B
bellard 已提交
1486
        case 8:
1487
            ret = ldq_phys(addr);
B
blueswir1 已提交
1488
            break;
B
bellard 已提交
1489
        }
B
blueswir1 已提交
1490
        break;
1491
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1492 1493
        switch(size) {
        case 1:
A
Anthony Liguori 已提交
1494 1495
            ret = ldub_phys((target_phys_addr_t)addr
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1496 1497
            break;
        case 2:
A
Anthony Liguori 已提交
1498 1499
            ret = lduw_phys((target_phys_addr_t)addr
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1500 1501 1502
            break;
        default:
        case 4:
A
Anthony Liguori 已提交
1503 1504
            ret = ldl_phys((target_phys_addr_t)addr
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
1505 1506
            break;
        case 8:
A
Anthony Liguori 已提交
1507 1508
            ret = ldq_phys((target_phys_addr_t)addr
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
B
blueswir1 已提交
1509
            break;
1510
        }
B
blueswir1 已提交
1511
        break;
B
blueswir1 已提交
1512 1513 1514
    case 0x30: // Turbosparc secondary cache diagnostic
    case 0x31: // Turbosparc RAM snoop
    case 0x32: // Turbosparc page table descriptor diagnostic
B
blueswir1 已提交
1515 1516 1517
    case 0x39: /* data cache diagnostic register */
        ret = 0;
        break;
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
    case 0x38: /* SuperSPARC MMU Breakpoint Control Registers */
        {
            int reg = (addr >> 8) & 3;

            switch(reg) {
            case 0: /* Breakpoint Value (Addr) */
                ret = env->mmubpregs[reg];
                break;
            case 1: /* Breakpoint Mask */
                ret = env->mmubpregs[reg];
                break;
            case 2: /* Breakpoint Control */
                ret = env->mmubpregs[reg];
                break;
            case 3: /* Breakpoint Status */
                ret = env->mmubpregs[reg];
                env->mmubpregs[reg] = 0ULL;
                break;
            }
1537 1538
            DPRINTF_MMU("read breakpoint reg[%d] 0x%016" PRIx64 "\n", reg,
                        ret);
1539 1540
        }
        break;
B
blueswir1 已提交
1541
    case 8: /* User code access, XXX */
1542
    default:
1543
        do_unassigned_access(addr, 0, 0, asi, size);
B
blueswir1 已提交
1544 1545
        ret = 0;
        break;
1546
    }
1547 1548 1549
    if (sign) {
        switch(size) {
        case 1:
B
blueswir1 已提交
1550
            ret = (int8_t) ret;
B
blueswir1 已提交
1551
            break;
1552
        case 2:
B
blueswir1 已提交
1553 1554 1555 1556
            ret = (int16_t) ret;
            break;
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1557
            break;
1558 1559 1560 1561
        default:
            break;
        }
    }
1562
#ifdef DEBUG_ASI
B
blueswir1 已提交
1563
    dump_asi("read ", last_addr, asi, size, ret);
1564
#endif
B
blueswir1 已提交
1565
    return ret;
1566 1567
}

B
blueswir1 已提交
1568
void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
1569
{
1570
    helper_check_align(addr, size - 1);
1571
    switch(asi) {
1572
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
1573
        switch (addr) {
1574 1575
        case 0x01c00000: /* MXCC stream data register 0 */
            if (size == 8)
B
blueswir1 已提交
1576
                env->mxccdata[0] = val;
1577
            else
B
blueswir1 已提交
1578 1579
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1580 1581 1582
            break;
        case 0x01c00008: /* MXCC stream data register 1 */
            if (size == 8)
B
blueswir1 已提交
1583
                env->mxccdata[1] = val;
1584
            else
B
blueswir1 已提交
1585 1586
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1587 1588 1589
            break;
        case 0x01c00010: /* MXCC stream data register 2 */
            if (size == 8)
B
blueswir1 已提交
1590
                env->mxccdata[2] = val;
1591
            else
B
blueswir1 已提交
1592 1593
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1594 1595 1596
            break;
        case 0x01c00018: /* MXCC stream data register 3 */
            if (size == 8)
B
blueswir1 已提交
1597
                env->mxccdata[3] = val;
1598
            else
B
blueswir1 已提交
1599 1600
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1601 1602 1603
            break;
        case 0x01c00100: /* MXCC stream source */
            if (size == 8)
B
blueswir1 已提交
1604
                env->mxccregs[0] = val;
1605
            else
B
blueswir1 已提交
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
            env->mxccdata[0] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
                                        0);
            env->mxccdata[1] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
                                        8);
            env->mxccdata[2] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
                                        16);
            env->mxccdata[3] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
                                        24);
1616 1617 1618
            break;
        case 0x01c00200: /* MXCC stream destination */
            if (size == 8)
B
blueswir1 已提交
1619
                env->mxccregs[1] = val;
1620
            else
B
blueswir1 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
            stq_phys((env->mxccregs[1] & 0xffffffffULL) +  0,
                     env->mxccdata[0]);
            stq_phys((env->mxccregs[1] & 0xffffffffULL) +  8,
                     env->mxccdata[1]);
            stq_phys((env->mxccregs[1] & 0xffffffffULL) + 16,
                     env->mxccdata[2]);
            stq_phys((env->mxccregs[1] & 0xffffffffULL) + 24,
                     env->mxccdata[3]);
1631 1632 1633
            break;
        case 0x01c00a00: /* MXCC control register */
            if (size == 8)
B
blueswir1 已提交
1634
                env->mxccregs[3] = val;
1635
            else
B
blueswir1 已提交
1636 1637
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1638 1639 1640
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
1641
                env->mxccregs[3] = (env->mxccregs[3] & 0xffffffff00000000ULL)
B
blueswir1 已提交
1642
                    | val;
1643
            else
B
blueswir1 已提交
1644 1645
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1646 1647
            break;
        case 0x01c00e00: /* MXCC error register  */
1648
            // writing a 1 bit clears the error
1649
            if (size == 8)
B
blueswir1 已提交
1650
                env->mxccregs[6] &= ~val;
1651
            else
B
blueswir1 已提交
1652 1653
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1654 1655 1656
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8)
B
blueswir1 已提交
1657
                env->mxccregs[7] = val;
1658
            else
B
blueswir1 已提交
1659 1660
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1661 1662
            break;
        default:
B
blueswir1 已提交
1663 1664
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1665 1666
            break;
        }
1667 1668
        DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %" PRIx64 "\n",
                     asi, size, addr, val);
1669 1670 1671
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1672
        break;
1673
    case 3: /* MMU flush */
B
blueswir1 已提交
1674 1675
        {
            int mmulev;
B
bellard 已提交
1676

B
blueswir1 已提交
1677
            mmulev = (addr >> 8) & 15;
1678
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
B
blueswir1 已提交
1679 1680
            switch (mmulev) {
            case 0: // flush page
B
blueswir1 已提交
1681
                tlb_flush_page(env, addr & 0xfffff000);
B
blueswir1 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
                break;
            case 1: // flush segment (256k)
            case 2: // flush region (16M)
            case 3: // flush context (4G)
            case 4: // flush entire
                tlb_flush(env, 1);
                break;
            default:
                break;
            }
B
bellard 已提交
1692
#ifdef DEBUG_MMU
B
blueswir1 已提交
1693
            dump_mmu(env);
B
bellard 已提交
1694
#endif
B
blueswir1 已提交
1695
        }
1696
        break;
1697
    case 4: /* write MMU regs */
B
blueswir1 已提交
1698
        {
B
blueswir1 已提交
1699
            int reg = (addr >> 8) & 0x1f;
B
blueswir1 已提交
1700
            uint32_t oldreg;
1701

B
blueswir1 已提交
1702
            oldreg = env->mmuregs[reg];
B
bellard 已提交
1703
            switch(reg) {
1704
            case 0: // Control Register
B
blueswir1 已提交
1705
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
B
blueswir1 已提交
1706
                                    (val & 0x00ffffff);
B
blueswir1 已提交
1707 1708
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
1709 1710
                if ((oldreg & (MMU_E | MMU_NF | env->def->mmu_bm)) !=
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->def->mmu_bm)))
B
bellard 已提交
1711 1712
                    tlb_flush(env, 1);
                break;
1713
            case 1: // Context Table Pointer Register
1714
                env->mmuregs[reg] = val & env->def->mmu_ctpr_mask;
1715 1716
                break;
            case 2: // Context Register
1717
                env->mmuregs[reg] = val & env->def->mmu_cxr_mask;
B
bellard 已提交
1718 1719 1720 1721 1722 1723
                if (oldreg != env->mmuregs[reg]) {
                    /* we flush when the MMU context changes because
                       QEMU has no MMU context support */
                    tlb_flush(env, 1);
                }
                break;
1724 1725 1726 1727
            case 3: // Synchronous Fault Status Register with Clear
            case 4: // Synchronous Fault Address Register
                break;
            case 0x10: // TLB Replacement Control Register
1728
                env->mmuregs[reg] = val & env->def->mmu_trcr_mask;
B
bellard 已提交
1729
                break;
1730
            case 0x13: // Synchronous Fault Status Register with Read and Clear
1731
                env->mmuregs[3] = val & env->def->mmu_sfsr_mask;
B
blueswir1 已提交
1732
                break;
1733
            case 0x14: // Synchronous Fault Address Register
B
blueswir1 已提交
1734
                env->mmuregs[4] = val;
B
blueswir1 已提交
1735
                break;
B
bellard 已提交
1736
            default:
B
blueswir1 已提交
1737
                env->mmuregs[reg] = val;
B
bellard 已提交
1738 1739 1740
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
B
blueswir1 已提交
1741 1742
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
                            reg, oldreg, env->mmuregs[reg]);
B
bellard 已提交
1743
            }
1744
#ifdef DEBUG_MMU
B
blueswir1 已提交
1745
            dump_mmu(env);
B
bellard 已提交
1746
#endif
B
blueswir1 已提交
1747
        }
1748
        break;
B
blueswir1 已提交
1749 1750 1751 1752
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1753 1754 1755
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1756
            stb_user(addr, val);
1757 1758
            break;
        case 2:
1759
            stw_user(addr, val);
1760 1761 1762
            break;
        default:
        case 4:
1763
            stl_user(addr, val);
1764 1765
            break;
        case 8:
1766
            stq_user(addr, val);
1767 1768 1769 1770 1771 1772
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1773
            stb_kernel(addr, val);
1774 1775
            break;
        case 2:
1776
            stw_kernel(addr, val);
1777 1778 1779
            break;
        default:
        case 4:
1780
            stl_kernel(addr, val);
1781 1782
            break;
        case 8:
1783
            stq_kernel(addr, val);
1784 1785 1786
            break;
        }
        break;
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
    case 0xc: /* I-cache tag */
    case 0xd: /* I-cache data */
    case 0xe: /* D-cache tag */
    case 0xf: /* D-cache data */
    case 0x10: /* I/D-cache flush page */
    case 0x11: /* I/D-cache flush segment */
    case 0x12: /* I/D-cache flush region */
    case 0x13: /* I/D-cache flush context */
    case 0x14: /* I/D-cache flush user */
        break;
B
bellard 已提交
1797
    case 0x17: /* Block copy, sta access */
B
blueswir1 已提交
1798
        {
B
blueswir1 已提交
1799 1800
            // val = src
            // addr = dst
B
blueswir1 已提交
1801
            // copy 32 bytes
1802
            unsigned int i;
B
blueswir1 已提交
1803
            uint32_t src = val & ~3, dst = addr & ~3, temp;
1804

1805 1806 1807 1808
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
B
blueswir1 已提交
1809
        }
1810
        break;
B
bellard 已提交
1811
    case 0x1f: /* Block fill, stda access */
B
blueswir1 已提交
1812
        {
B
blueswir1 已提交
1813 1814
            // addr = dst
            // fill 32 bytes with val
1815
            unsigned int i;
B
blueswir1 已提交
1816
            uint32_t dst = addr & 7;
1817 1818 1819

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
1820
        }
1821
        break;
1822
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
1823
        {
B
bellard 已提交
1824 1825
            switch(size) {
            case 1:
B
blueswir1 已提交
1826
                stb_phys(addr, val);
B
bellard 已提交
1827 1828
                break;
            case 2:
1829
                stw_phys(addr, val);
B
bellard 已提交
1830 1831 1832
                break;
            case 4:
            default:
1833
                stl_phys(addr, val);
B
bellard 已提交
1834
                break;
B
bellard 已提交
1835
            case 8:
1836
                stq_phys(addr, val);
B
bellard 已提交
1837
                break;
B
bellard 已提交
1838
            }
B
blueswir1 已提交
1839
        }
1840
        break;
B
blueswir1 已提交
1841
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
B
blueswir1 已提交
1842
        {
1843 1844
            switch(size) {
            case 1:
A
Anthony Liguori 已提交
1845 1846
                stb_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1847 1848
                break;
            case 2:
A
Anthony Liguori 已提交
1849 1850
                stw_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1851 1852 1853
                break;
            case 4:
            default:
A
Anthony Liguori 已提交
1854 1855
                stl_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1856 1857
                break;
            case 8:
A
Anthony Liguori 已提交
1858 1859
                stq_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1860 1861
                break;
            }
B
blueswir1 已提交
1862
        }
1863
        break;
B
blueswir1 已提交
1864 1865 1866
    case 0x30: // store buffer tags or Turbosparc secondary cache diagnostic
    case 0x31: // store buffer data, Ross RT620 I-cache flush or
               // Turbosparc snoop RAM
B
blueswir1 已提交
1867 1868
    case 0x32: // store buffer control or Turbosparc page table
               // descriptor diagnostic
1869 1870
    case 0x36: /* I-cache flash clear */
    case 0x37: /* D-cache flash clear */
B
blueswir1 已提交
1871
    case 0x4c: /* breakpoint action */
1872
        break;
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
    case 0x38: /* SuperSPARC MMU Breakpoint Control Registers*/
        {
            int reg = (addr >> 8) & 3;

            switch(reg) {
            case 0: /* Breakpoint Value (Addr) */
                env->mmubpregs[reg] = (val & 0xfffffffffULL);
                break;
            case 1: /* Breakpoint Mask */
                env->mmubpregs[reg] = (val & 0xfffffffffULL);
                break;
            case 2: /* Breakpoint Control */
                env->mmubpregs[reg] = (val & 0x7fULL);
                break;
            case 3: /* Breakpoint Status */
                env->mmubpregs[reg] = (val & 0xfULL);
                break;
            }
1891
            DPRINTF_MMU("write breakpoint reg[%d] 0x%016x\n", reg,
1892 1893 1894
                        env->mmuregs[reg]);
        }
        break;
B
blueswir1 已提交
1895
    case 8: /* User code access, XXX */
1896
    case 9: /* Supervisor code access, XXX */
1897
    default:
1898
        do_unassigned_access(addr, 1, 0, asi, size);
1899
        break;
1900
    }
1901
#ifdef DEBUG_ASI
B
blueswir1 已提交
1902
    dump_asi("write", addr, asi, size, val);
1903
#endif
1904 1905
}

1906 1907 1908 1909
#endif /* CONFIG_USER_ONLY */
#else /* TARGET_SPARC64 */

#ifdef CONFIG_USER_ONLY
B
blueswir1 已提交
1910
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1911 1912
{
    uint64_t ret = 0;
B
blueswir1 已提交
1913 1914 1915
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
1916 1917 1918 1919

    if (asi < 0x80)
        raise_exception(TT_PRIV_ACT);

1920
    helper_check_align(addr, size - 1);
B
blueswir1 已提交
1921
    address_mask(env, &addr);
1922

1923 1924 1925
    switch (asi) {
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
B
blueswir1 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934
        if (page_check_range(addr, size, PAGE_READ) == -1) {
#ifdef DEBUG_ASI
            dump_asi("read ", last_addr, asi, size, ret);
#endif
            return 0;
        }
        // Fall through
    case 0x80: // Primary
    case 0x88: // Primary LE
1935 1936 1937
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
1938
                ret = ldub_raw(addr);
1939 1940
                break;
            case 2:
1941
                ret = lduw_raw(addr);
1942 1943
                break;
            case 4:
1944
                ret = ldl_raw(addr);
1945 1946 1947
                break;
            default:
            case 8:
1948
                ret = ldq_raw(addr);
1949 1950 1951 1952 1953 1954
                break;
            }
        }
        break;
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963
        if (page_check_range(addr, size, PAGE_READ) == -1) {
#ifdef DEBUG_ASI
            dump_asi("read ", last_addr, asi, size, ret);
#endif
            return 0;
        }
        // Fall through
    case 0x81: // Secondary
    case 0x89: // Secondary LE
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
        // XXX
        break;
    default:
        break;
    }

    /* Convert from little endian */
    switch (asi) {
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
    case 0x8a: // Primary no-fault LE
    case 0x8b: // Secondary no-fault LE
        switch(size) {
        case 2:
            ret = bswap16(ret);
B
blueswir1 已提交
1979
            break;
1980 1981
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
1982
            break;
1983 1984
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
1985
            break;
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
1998
            break;
1999 2000
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
2001
            break;
2002 2003
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
2004
            break;
2005 2006 2007 2008
        default:
            break;
        }
    }
B
blueswir1 已提交
2009 2010 2011 2012
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
2013 2014
}

B
blueswir1 已提交
2015
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
2016
{
B
blueswir1 已提交
2017 2018 2019
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
2020 2021 2022
    if (asi < 0x80)
        raise_exception(TT_PRIV_ACT);

2023
    helper_check_align(addr, size - 1);
B
blueswir1 已提交
2024
    address_mask(env, &addr);
2025

2026 2027 2028 2029 2030 2031
    /* Convert to little endian */
    switch (asi) {
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
2032
            val = bswap16(val);
B
blueswir1 已提交
2033
            break;
2034
        case 4:
2035
            val = bswap32(val);
B
blueswir1 已提交
2036
            break;
2037
        case 8:
2038
            val = bswap64(val);
B
blueswir1 已提交
2039
            break;
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
        default:
            break;
        }
    default:
        break;
    }

    switch(asi) {
    case 0x80: // Primary
    case 0x88: // Primary LE
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
2053
                stb_raw(addr, val);
2054 2055
                break;
            case 2:
2056
                stw_raw(addr, val);
2057 2058
                break;
            case 4:
2059
                stl_raw(addr, val);
2060 2061 2062
                break;
            case 8:
            default:
2063
                stq_raw(addr, val);
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
                break;
            }
        }
        break;
    case 0x81: // Secondary
    case 0x89: // Secondary LE
        // XXX
        return;

    case 0x82: // Primary no-fault, RO
    case 0x83: // Secondary no-fault, RO
    case 0x8a: // Primary no-fault LE, RO
    case 0x8b: // Secondary no-fault LE, RO
    default:
2078
        do_unassigned_access(addr, 1, 0, 1, size);
2079 2080 2081 2082 2083
        return;
    }
}

#else /* CONFIG_USER_ONLY */
B
bellard 已提交
2084

B
blueswir1 已提交
2085
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
B
bellard 已提交
2086
{
B
bellard 已提交
2087
    uint64_t ret = 0;
B
blueswir1 已提交
2088 2089 2090
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
B
bellard 已提交
2091

I
Igor V. Kovalenko 已提交
2092 2093
    asi &= 0xff;

B
blueswir1 已提交
2094
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2095 2096
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2097
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2098
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
2099

2100
    helper_check_align(addr, size - 1);
B
bellard 已提交
2101
    switch (asi) {
B
blueswir1 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
        if (cpu_get_phys_page_debug(env, addr) == -1ULL) {
#ifdef DEBUG_ASI
            dump_asi("read ", last_addr, asi, size, ret);
#endif
            return 0;
        }
        // Fall through
2111 2112 2113 2114
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
2115 2116
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
2117
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
2118 2119
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
2120 2121
                switch(size) {
                case 1:
B
blueswir1 已提交
2122
                    ret = ldub_hypv(addr);
B
blueswir1 已提交
2123 2124
                    break;
                case 2:
2125
                    ret = lduw_hypv(addr);
B
blueswir1 已提交
2126 2127
                    break;
                case 4:
2128
                    ret = ldl_hypv(addr);
B
blueswir1 已提交
2129 2130 2131
                    break;
                default:
                case 8:
2132
                    ret = ldq_hypv(addr);
B
blueswir1 已提交
2133 2134 2135 2136 2137
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
2138
                    ret = ldub_kernel(addr);
B
blueswir1 已提交
2139 2140
                    break;
                case 2:
2141
                    ret = lduw_kernel(addr);
B
blueswir1 已提交
2142 2143
                    break;
                case 4:
2144
                    ret = ldl_kernel(addr);
B
blueswir1 已提交
2145 2146 2147
                    break;
                default:
                case 8:
2148
                    ret = ldq_kernel(addr);
B
blueswir1 已提交
2149 2150
                    break;
                }
2151 2152 2153 2154
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
2155
                ret = ldub_user(addr);
2156 2157
                break;
            case 2:
2158
                ret = lduw_user(addr);
2159 2160
                break;
            case 4:
2161
                ret = ldl_user(addr);
2162 2163 2164
                break;
            default:
            case 8:
2165
                ret = ldq_user(addr);
2166 2167 2168 2169
                break;
            }
        }
        break;
B
bellard 已提交
2170 2171
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
2172 2173
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
2174
        {
B
bellard 已提交
2175 2176
            switch(size) {
            case 1:
B
blueswir1 已提交
2177
                ret = ldub_phys(addr);
B
bellard 已提交
2178 2179
                break;
            case 2:
2180
                ret = lduw_phys(addr);
B
bellard 已提交
2181 2182
                break;
            case 4:
2183
                ret = ldl_phys(addr);
B
bellard 已提交
2184 2185 2186
                break;
            default:
            case 8:
2187
                ret = ldq_phys(addr);
B
bellard 已提交
2188 2189
                break;
            }
B
blueswir1 已提交
2190 2191
            break;
        }
B
blueswir1 已提交
2192 2193 2194 2195 2196
    case 0x24: // Nucleus quad LDD 128 bit atomic
    case 0x2c: // Nucleus quad LDD 128 bit atomic LE
        //  Only ldda allowed
        raise_exception(TT_ILL_INSN);
        return 0;
B
blueswir1 已提交
2197 2198 2199 2200 2201 2202 2203 2204 2205
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
        if (cpu_get_phys_page_debug(env, addr) == -1ULL) {
#ifdef DEBUG_ASI
            dump_asi("read ", last_addr, asi, size, ret);
#endif
            return 0;
        }
        // Fall through
B
bellard 已提交
2206 2207 2208 2209 2210
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x11: // As if user secondary
    case 0x19: // As if user secondary LE
    case 0x4a: // UPA config
2211
    case 0x81: // Secondary
B
bellard 已提交
2212
    case 0x89: // Secondary LE
B
blueswir1 已提交
2213 2214
        // XXX
        break;
B
bellard 已提交
2215
    case 0x45: // LSU
B
blueswir1 已提交
2216 2217
        ret = env->lsu;
        break;
B
bellard 已提交
2218
    case 0x50: // I-MMU regs
B
blueswir1 已提交
2219
        {
B
blueswir1 已提交
2220
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
2221

2222 2223
            if (reg == 0) {
                // I-TSB Tag Target register
2224
                ret = ultrasparc_tag_target(env->immu.tag_access);
2225 2226 2227 2228
            } else {
                ret = env->immuregs[reg];
            }

B
blueswir1 已提交
2229 2230
            break;
        }
B
bellard 已提交
2231
    case 0x51: // I-MMU 8k TSB pointer
2232 2233 2234
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
2235
            ret = ultrasparc_tsb_pointer(env->immu.tsb, env->immu.tag_access,
2236 2237 2238
                                         8*1024);
            break;
        }
B
bellard 已提交
2239
    case 0x52: // I-MMU 64k TSB pointer
2240 2241 2242
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
2243
            ret = ultrasparc_tsb_pointer(env->immu.tsb, env->immu.tag_access,
2244 2245 2246
                                         64*1024);
            break;
        }
2247 2248 2249 2250
    case 0x55: // I-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

2251
            ret = env->itlb[reg].tte;
2252 2253
            break;
        }
B
bellard 已提交
2254
    case 0x56: // I-MMU tag read
B
blueswir1 已提交
2255
        {
B
blueswir1 已提交
2256
            int reg = (addr >> 3) & 0x3f;
B
blueswir1 已提交
2257

2258
            ret = env->itlb[reg].tag;
B
blueswir1 已提交
2259 2260
            break;
        }
B
bellard 已提交
2261
    case 0x58: // D-MMU regs
B
blueswir1 已提交
2262
        {
B
blueswir1 已提交
2263
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
2264

2265 2266
            if (reg == 0) {
                // D-TSB Tag Target register
2267
                ret = ultrasparc_tag_target(env->dmmu.tag_access);
2268 2269 2270 2271 2272 2273 2274 2275 2276
            } else {
                ret = env->dmmuregs[reg];
            }
            break;
        }
    case 0x59: // D-MMU 8k TSB pointer
        {
            // env->dmmuregs[5] holds D-MMU TSB register value
            // env->dmmuregs[6] holds D-MMU Tag Access register value
2277
            ret = ultrasparc_tsb_pointer(env->dmmu.tsb, env->dmmu.tag_access,
2278 2279 2280 2281 2282 2283 2284
                                         8*1024);
            break;
        }
    case 0x5a: // D-MMU 64k TSB pointer
        {
            // env->dmmuregs[5] holds D-MMU TSB register value
            // env->dmmuregs[6] holds D-MMU Tag Access register value
2285
            ret = ultrasparc_tsb_pointer(env->dmmu.tsb, env->dmmu.tag_access,
2286
                                         64*1024);
B
blueswir1 已提交
2287 2288
            break;
        }
2289 2290 2291 2292
    case 0x5d: // D-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

2293
            ret = env->dtlb[reg].tte;
2294 2295
            break;
        }
B
bellard 已提交
2296
    case 0x5e: // D-MMU tag read
B
blueswir1 已提交
2297
        {
B
blueswir1 已提交
2298
            int reg = (addr >> 3) & 0x3f;
B
blueswir1 已提交
2299

2300
            ret = env->dtlb[reg].tag;
B
blueswir1 已提交
2301 2302
            break;
        }
2303 2304
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2305 2306 2307
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2308 2309 2310 2311 2312 2313 2314 2315
    case 0x4e: // E-cache tag data
    case 0x66: // I-cache instruction access
    case 0x67: // I-cache tag access
    case 0x6e: // I-cache predecode
    case 0x6f: // I-cache LRU etc.
    case 0x76: // E-cache tag
    case 0x7e: // E-cache tag
        break;
B
bellard 已提交
2316
    case 0x5b: // D-MMU data pointer
B
bellard 已提交
2317 2318 2319
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
2320 2321
        // XXX
        break;
B
bellard 已提交
2322 2323 2324 2325
    case 0x54: // I-MMU data in, WO
    case 0x57: // I-MMU demap, WO
    case 0x5c: // D-MMU data in, WO
    case 0x5f: // D-MMU demap, WO
B
bellard 已提交
2326
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
2327
    default:
2328
        do_unassigned_access(addr, 0, 0, 1, size);
B
blueswir1 已提交
2329 2330
        ret = 0;
        break;
B
bellard 已提交
2331
    }
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346

    /* Convert from little endian */
    switch (asi) {
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x18: // As if user primary LE
    case 0x19: // As if user secondary LE
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
    case 0x8a: // Primary no-fault LE
    case 0x8b: // Secondary no-fault LE
        switch(size) {
        case 2:
            ret = bswap16(ret);
B
blueswir1 已提交
2347
            break;
2348 2349
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
2350
            break;
2351 2352
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
2353
            break;
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
2366
            break;
2367 2368
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
2369
            break;
2370 2371
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
2372
            break;
2373 2374 2375 2376
        default:
            break;
        }
    }
B
blueswir1 已提交
2377 2378 2379 2380
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
B
bellard 已提交
2381 2382
}

B
blueswir1 已提交
2383
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
B
bellard 已提交
2384
{
B
blueswir1 已提交
2385 2386 2387
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
I
Igor V. Kovalenko 已提交
2388 2389 2390

    asi &= 0xff;

B
blueswir1 已提交
2391
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2392 2393
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2394
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2395
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
2396

2397
    helper_check_align(addr, size - 1);
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
    /* Convert to little endian */
    switch (asi) {
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x18: // As if user primary LE
    case 0x19: // As if user secondary LE
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
2409
            val = bswap16(val);
B
blueswir1 已提交
2410
            break;
2411
        case 4:
2412
            val = bswap32(val);
B
blueswir1 已提交
2413
            break;
2414
        case 8:
2415
            val = bswap64(val);
B
blueswir1 已提交
2416
            break;
2417 2418 2419 2420 2421 2422 2423
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
2424
    switch(asi) {
2425 2426 2427 2428
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
2429 2430
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
2431
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
2432 2433
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
2434 2435
                switch(size) {
                case 1:
B
blueswir1 已提交
2436
                    stb_hypv(addr, val);
B
blueswir1 已提交
2437 2438
                    break;
                case 2:
2439
                    stw_hypv(addr, val);
B
blueswir1 已提交
2440 2441
                    break;
                case 4:
2442
                    stl_hypv(addr, val);
B
blueswir1 已提交
2443 2444 2445
                    break;
                case 8:
                default:
2446
                    stq_hypv(addr, val);
B
blueswir1 已提交
2447 2448 2449 2450 2451
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
2452
                    stb_kernel(addr, val);
B
blueswir1 已提交
2453 2454
                    break;
                case 2:
2455
                    stw_kernel(addr, val);
B
blueswir1 已提交
2456 2457
                    break;
                case 4:
2458
                    stl_kernel(addr, val);
B
blueswir1 已提交
2459 2460 2461
                    break;
                case 8:
                default:
2462
                    stq_kernel(addr, val);
B
blueswir1 已提交
2463 2464
                    break;
                }
2465 2466 2467 2468
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
2469
                stb_user(addr, val);
2470 2471
                break;
            case 2:
2472
                stw_user(addr, val);
2473 2474
                break;
            case 4:
2475
                stl_user(addr, val);
2476 2477 2478
                break;
            case 8:
            default:
2479
                stq_user(addr, val);
2480 2481 2482 2483
                break;
            }
        }
        break;
B
bellard 已提交
2484 2485
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
2486 2487
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
2488
        {
B
bellard 已提交
2489 2490
            switch(size) {
            case 1:
B
blueswir1 已提交
2491
                stb_phys(addr, val);
B
bellard 已提交
2492 2493
                break;
            case 2:
2494
                stw_phys(addr, val);
B
bellard 已提交
2495 2496
                break;
            case 4:
2497
                stl_phys(addr, val);
B
bellard 已提交
2498 2499 2500
                break;
            case 8:
            default:
2501
                stq_phys(addr, val);
B
bellard 已提交
2502 2503
                break;
            }
B
blueswir1 已提交
2504 2505
        }
        return;
B
blueswir1 已提交
2506 2507 2508 2509 2510
    case 0x24: // Nucleus quad LDD 128 bit atomic
    case 0x2c: // Nucleus quad LDD 128 bit atomic LE
        //  Only ldda allowed
        raise_exception(TT_ILL_INSN);
        return;
B
bellard 已提交
2511 2512 2513 2514 2515
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x11: // As if user secondary
    case 0x19: // As if user secondary LE
    case 0x4a: // UPA config
B
blueswir1 已提交
2516
    case 0x81: // Secondary
B
bellard 已提交
2517
    case 0x89: // Secondary LE
B
blueswir1 已提交
2518 2519
        // XXX
        return;
B
bellard 已提交
2520
    case 0x45: // LSU
B
blueswir1 已提交
2521 2522 2523 2524
        {
            uint64_t oldreg;

            oldreg = env->lsu;
B
blueswir1 已提交
2525
            env->lsu = val & (DMMU_E | IMMU_E);
B
blueswir1 已提交
2526 2527 2528
            // Mappings generated during D/I MMU disabled mode are
            // invalid in normal mode
            if (oldreg != env->lsu) {
B
blueswir1 已提交
2529 2530
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
                            oldreg, env->lsu);
B
bellard 已提交
2531
#ifdef DEBUG_MMU
B
blueswir1 已提交
2532
                dump_mmu(env);
B
bellard 已提交
2533
#endif
B
blueswir1 已提交
2534 2535 2536 2537
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
2538
    case 0x50: // I-MMU regs
B
blueswir1 已提交
2539
        {
B
blueswir1 已提交
2540
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
2541
            uint64_t oldreg;
2542

B
blueswir1 已提交
2543
            oldreg = env->immuregs[reg];
B
bellard 已提交
2544 2545 2546 2547 2548 2549 2550
            switch(reg) {
            case 0: // RO
                return;
            case 1: // Not in I-MMU
            case 2:
                return;
            case 3: // SFSR
B
blueswir1 已提交
2551 2552
                if ((val & 1) == 0)
                    val = 0; // Clear SFSR
2553
                env->immu.sfsr = val;
B
bellard 已提交
2554
                break;
2555 2556
            case 4: // RO
                return;
B
bellard 已提交
2557
            case 5: // TSB access
2558 2559 2560 2561
                DPRINTF_MMU("immu TSB write: 0x%016" PRIx64 " -> 0x%016"
                            PRIx64 "\n", env->immu.tsb, val);
                env->immu.tsb = val;
                break;
B
bellard 已提交
2562
            case 6: // Tag access
2563 2564 2565 2566 2567
                env->immu.tag_access = val;
                break;
            case 7:
            case 8:
                return;
B
bellard 已提交
2568 2569 2570
            default:
                break;
            }
2571

B
bellard 已提交
2572
            if (oldreg != env->immuregs[reg]) {
2573
                DPRINTF_MMU("immu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
B
blueswir1 已提交
2574
                            PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
B
bellard 已提交
2575
            }
2576
#ifdef DEBUG_MMU
B
blueswir1 已提交
2577
            dump_mmu(env);
B
bellard 已提交
2578
#endif
B
blueswir1 已提交
2579 2580
            return;
        }
B
bellard 已提交
2581
    case 0x54: // I-MMU data in
2582 2583
        replace_tlb_1bit_lru(env->itlb, env->immu.tag_access, val, "immu", env);
        return;
B
bellard 已提交
2584
    case 0x55: // I-MMU data access
B
blueswir1 已提交
2585
        {
2586 2587
            // TODO: auto demap

B
blueswir1 已提交
2588
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
2589

2590
            replace_tlb_entry(&env->itlb[i], env->immu.tag_access, val, env);
2591 2592

#ifdef DEBUG_MMU
2593
            DPRINTF_MMU("immu data access replaced entry [%i]\n", i);
2594 2595
            dump_mmu(env);
#endif
B
blueswir1 已提交
2596 2597
            return;
        }
B
bellard 已提交
2598
    case 0x57: // I-MMU demap
2599
        demap_tlb(env->itlb, val, "immu", env);
B
blueswir1 已提交
2600
        return;
B
bellard 已提交
2601
    case 0x58: // D-MMU regs
B
blueswir1 已提交
2602
        {
B
blueswir1 已提交
2603
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
2604
            uint64_t oldreg;
2605

B
blueswir1 已提交
2606
            oldreg = env->dmmuregs[reg];
B
bellard 已提交
2607 2608 2609 2610 2611
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
B
blueswir1 已提交
2612 2613
                if ((val & 1) == 0) {
                    val = 0; // Clear SFSR, Fault address
2614
                    env->dmmu.sfar = 0;
B
blueswir1 已提交
2615
                }
2616
                env->dmmu.sfsr = val;
B
bellard 已提交
2617 2618
                break;
            case 1: // Primary context
2619 2620
                env->dmmu.mmu_primary_context = val;
                break;
B
bellard 已提交
2621
            case 2: // Secondary context
2622 2623
                env->dmmu.mmu_secondary_context = val;
                break;
B
bellard 已提交
2624
            case 5: // TSB access
2625 2626 2627 2628
                DPRINTF_MMU("dmmu TSB write: 0x%016" PRIx64 " -> 0x%016"
                            PRIx64 "\n", env->dmmu.tsb, val);
                env->dmmu.tsb = val;
                break;
B
bellard 已提交
2629
            case 6: // Tag access
2630 2631
                env->dmmu.tag_access = val;
                break;
B
bellard 已提交
2632 2633 2634
            case 7: // Virtual Watchpoint
            case 8: // Physical Watchpoint
            default:
2635
                env->dmmuregs[reg] = val;
B
bellard 已提交
2636 2637
                break;
            }
2638

B
bellard 已提交
2639
            if (oldreg != env->dmmuregs[reg]) {
2640
                DPRINTF_MMU("dmmu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
B
blueswir1 已提交
2641
                            PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
B
bellard 已提交
2642
            }
2643
#ifdef DEBUG_MMU
B
blueswir1 已提交
2644
            dump_mmu(env);
B
bellard 已提交
2645
#endif
B
blueswir1 已提交
2646 2647
            return;
        }
B
bellard 已提交
2648
    case 0x5c: // D-MMU data in
2649 2650
        replace_tlb_1bit_lru(env->dtlb, env->dmmu.tag_access, val, "dmmu", env);
        return;
B
bellard 已提交
2651
    case 0x5d: // D-MMU data access
B
blueswir1 已提交
2652
        {
B
blueswir1 已提交
2653
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
2654

2655 2656
            replace_tlb_entry(&env->dtlb[i], env->dmmu.tag_access, val, env);

2657
#ifdef DEBUG_MMU
2658
            DPRINTF_MMU("dmmu data access replaced entry [%i]\n", i);
2659 2660
            dump_mmu(env);
#endif
B
blueswir1 已提交
2661 2662
            return;
        }
B
bellard 已提交
2663
    case 0x5f: // D-MMU demap
2664
        demap_tlb(env->dtlb, val, "dmmu", env);
2665
        return;
B
bellard 已提交
2666
    case 0x49: // Interrupt data receive
B
blueswir1 已提交
2667 2668
        // XXX
        return;
2669 2670
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2671 2672 2673
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2674 2675 2676 2677 2678 2679 2680 2681
    case 0x4e: // E-cache tag data
    case 0x66: // I-cache instruction access
    case 0x67: // I-cache tag access
    case 0x6e: // I-cache predecode
    case 0x6f: // I-cache LRU etc.
    case 0x76: // E-cache tag
    case 0x7e: // E-cache tag
        return;
B
bellard 已提交
2682 2683 2684 2685 2686 2687 2688
    case 0x51: // I-MMU 8k TSB pointer, RO
    case 0x52: // I-MMU 64k TSB pointer, RO
    case 0x56: // I-MMU tag read, RO
    case 0x59: // D-MMU 8k TSB pointer, RO
    case 0x5a: // D-MMU 64k TSB pointer, RO
    case 0x5b: // D-MMU data pointer, RO
    case 0x5e: // D-MMU tag read, RO
B
bellard 已提交
2689 2690 2691 2692 2693 2694
    case 0x48: // Interrupt dispatch, RO
    case 0x7f: // Incoming interrupt vector, RO
    case 0x82: // Primary no-fault, RO
    case 0x83: // Secondary no-fault, RO
    case 0x8a: // Primary no-fault LE, RO
    case 0x8b: // Secondary no-fault LE, RO
B
bellard 已提交
2695
    default:
2696
        do_unassigned_access(addr, 1, 0, 1, size);
B
blueswir1 已提交
2697
        return;
B
bellard 已提交
2698 2699
    }
}
2700
#endif /* CONFIG_USER_ONLY */
2701

B
blueswir1 已提交
2702 2703 2704
void helper_ldda_asi(target_ulong addr, int asi, int rd)
{
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2705 2706
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2707
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748
        raise_exception(TT_PRIV_ACT);

    switch (asi) {
    case 0x24: // Nucleus quad LDD 128 bit atomic
    case 0x2c: // Nucleus quad LDD 128 bit atomic LE
        helper_check_align(addr, 0xf);
        if (rd == 0) {
            env->gregs[1] = ldq_kernel(addr + 8);
            if (asi == 0x2c)
                bswap64s(&env->gregs[1]);
        } else if (rd < 8) {
            env->gregs[rd] = ldq_kernel(addr);
            env->gregs[rd + 1] = ldq_kernel(addr + 8);
            if (asi == 0x2c) {
                bswap64s(&env->gregs[rd]);
                bswap64s(&env->gregs[rd + 1]);
            }
        } else {
            env->regwptr[rd] = ldq_kernel(addr);
            env->regwptr[rd + 1] = ldq_kernel(addr + 8);
            if (asi == 0x2c) {
                bswap64s(&env->regwptr[rd]);
                bswap64s(&env->regwptr[rd + 1]);
            }
        }
        break;
    default:
        helper_check_align(addr, 0x3);
        if (rd == 0)
            env->gregs[1] = helper_ld_asi(addr + 4, asi, 4, 0);
        else if (rd < 8) {
            env->gregs[rd] = helper_ld_asi(addr, asi, 4, 0);
            env->gregs[rd + 1] = helper_ld_asi(addr + 4, asi, 4, 0);
        } else {
            env->regwptr[rd] = helper_ld_asi(addr, asi, 4, 0);
            env->regwptr[rd + 1] = helper_ld_asi(addr + 4, asi, 4, 0);
        }
        break;
    }
}

B
blueswir1 已提交
2749
void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
2750 2751
{
    unsigned int i;
B
blueswir1 已提交
2752
    target_ulong val;
2753

2754
    helper_check_align(addr, 3);
2755 2756 2757 2758 2759
    switch (asi) {
    case 0xf0: // Block load primary
    case 0xf1: // Block load secondary
    case 0xf8: // Block load primary LE
    case 0xf9: // Block load secondary LE
B
blueswir1 已提交
2760 2761 2762 2763
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2764
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2765
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2766 2767
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4,
                                                         0);
B
blueswir1 已提交
2768
            addr += 4;
2769 2770 2771 2772 2773 2774 2775
        }

        return;
    default:
        break;
    }

B
blueswir1 已提交
2776
    val = helper_ld_asi(addr, asi, size, 0);
2777 2778 2779
    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2780
        *((uint32_t *)&env->fpr[rd]) = val;
2781 2782
        break;
    case 8:
B
blueswir1 已提交
2783
        *((int64_t *)&DT0) = val;
2784
        break;
B
blueswir1 已提交
2785 2786 2787
    case 16:
        // XXX
        break;
2788 2789 2790
    }
}

B
blueswir1 已提交
2791
void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
2792 2793
{
    unsigned int i;
B
blueswir1 已提交
2794
    target_ulong val = 0;
2795

2796
    helper_check_align(addr, 3);
2797
    switch (asi) {
B
blueswir1 已提交
2798 2799
    case 0xe0: // UA2007 Block commit store primary (cache flush)
    case 0xe1: // UA2007 Block commit store secondary (cache flush)
2800 2801 2802 2803
    case 0xf0: // Block store primary
    case 0xf1: // Block store secondary
    case 0xf8: // Block store primary LE
    case 0xf9: // Block store secondary LE
B
blueswir1 已提交
2804 2805 2806 2807
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2808
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2809
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2810 2811 2812
            val = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(addr, val, asi & 0x8f, 4);
            addr += 4;
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
        }

        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2823
        val = *((uint32_t *)&env->fpr[rd]);
2824 2825
        break;
    case 8:
B
blueswir1 已提交
2826
        val = *((int64_t *)&DT0);
2827
        break;
B
blueswir1 已提交
2828 2829 2830
    case 16:
        // XXX
        break;
2831
    }
B
blueswir1 已提交
2832 2833 2834 2835 2836 2837 2838 2839
    helper_st_asi(addr, val, asi, size);
}

target_ulong helper_cas_asi(target_ulong addr, target_ulong val1,
                            target_ulong val2, uint32_t asi)
{
    target_ulong ret;

2840
    val2 &= 0xffffffffUL;
B
blueswir1 已提交
2841 2842
    ret = helper_ld_asi(addr, asi, 4, 0);
    ret &= 0xffffffffUL;
2843 2844
    if (val2 == ret)
        helper_st_asi(addr, val1 & 0xffffffffUL, asi, 4);
B
blueswir1 已提交
2845
    return ret;
2846 2847
}

B
blueswir1 已提交
2848 2849 2850 2851 2852 2853
target_ulong helper_casx_asi(target_ulong addr, target_ulong val1,
                             target_ulong val2, uint32_t asi)
{
    target_ulong ret;

    ret = helper_ld_asi(addr, asi, 8, 0);
2854 2855
    if (val2 == ret)
        helper_st_asi(addr, val1, asi, 8);
B
blueswir1 已提交
2856 2857
    return ret;
}
2858
#endif /* TARGET_SPARC64 */
B
bellard 已提交
2859 2860

#ifndef TARGET_SPARC64
B
blueswir1 已提交
2861
void helper_rett(void)
2862
{
2863 2864
    unsigned int cwp;

2865 2866 2867
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

2868
    env->psret = 1;
2869
    cwp = cpu_cwp_inc(env, env->cwp + 1) ;
2870 2871 2872 2873 2874 2875
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
2876
#endif
2877

B
blueswir1 已提交
2878 2879 2880 2881 2882
target_ulong helper_udiv(target_ulong a, target_ulong b)
{
    uint64_t x0;
    uint32_t x1;

2883
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
    x1 = b;

    if (x1 == 0) {
        raise_exception(TT_DIV_ZERO);
    }

    x0 = x0 / x1;
    if (x0 > 0xffffffff) {
        env->cc_src2 = 1;
        return 0xffffffff;
    } else {
        env->cc_src2 = 0;
        return x0;
    }
}

target_ulong helper_sdiv(target_ulong a, target_ulong b)
{
    int64_t x0;
    int32_t x1;

2905
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
    x1 = b;

    if (x1 == 0) {
        raise_exception(TT_DIV_ZERO);
    }

    x0 = x0 / x1;
    if ((int32_t) x0 != x0) {
        env->cc_src2 = 1;
        return x0 < 0? 0x80000000: 0x7fffffff;
    } else {
        env->cc_src2 = 0;
        return x0;
    }
}

B
blueswir1 已提交
2922 2923
void helper_stdf(target_ulong addr, int mem_idx)
{
2924
    helper_check_align(addr, 7);
B
blueswir1 已提交
2925 2926 2927
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2928
        stfq_user(addr, DT0);
B
blueswir1 已提交
2929 2930
        break;
    case 1:
2931
        stfq_kernel(addr, DT0);
B
blueswir1 已提交
2932 2933 2934
        break;
#ifdef TARGET_SPARC64
    case 2:
2935
        stfq_hypv(addr, DT0);
B
blueswir1 已提交
2936 2937 2938 2939 2940 2941
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2942
    address_mask(env, &addr);
2943
    stfq_raw(addr, DT0);
B
blueswir1 已提交
2944 2945 2946 2947 2948
#endif
}

void helper_lddf(target_ulong addr, int mem_idx)
{
2949
    helper_check_align(addr, 7);
B
blueswir1 已提交
2950 2951 2952
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2953
        DT0 = ldfq_user(addr);
B
blueswir1 已提交
2954 2955
        break;
    case 1:
2956
        DT0 = ldfq_kernel(addr);
B
blueswir1 已提交
2957 2958 2959
        break;
#ifdef TARGET_SPARC64
    case 2:
2960
        DT0 = ldfq_hypv(addr);
B
blueswir1 已提交
2961 2962 2963 2964 2965 2966
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2967
    address_mask(env, &addr);
2968
    DT0 = ldfq_raw(addr);
B
blueswir1 已提交
2969 2970 2971
#endif
}

B
blueswir1 已提交
2972
void helper_ldqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
2973 2974 2975 2976
{
    // XXX add 128 bit load
    CPU_QuadU u;

2977
    helper_check_align(addr, 7);
B
blueswir1 已提交
2978 2979 2980
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2981 2982
        u.ll.upper = ldq_user(addr);
        u.ll.lower = ldq_user(addr + 8);
B
blueswir1 已提交
2983 2984 2985
        QT0 = u.q;
        break;
    case 1:
2986 2987
        u.ll.upper = ldq_kernel(addr);
        u.ll.lower = ldq_kernel(addr + 8);
B
blueswir1 已提交
2988 2989 2990 2991
        QT0 = u.q;
        break;
#ifdef TARGET_SPARC64
    case 2:
2992 2993
        u.ll.upper = ldq_hypv(addr);
        u.ll.lower = ldq_hypv(addr + 8);
B
blueswir1 已提交
2994 2995 2996 2997 2998 2999 3000
        QT0 = u.q;
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
3001
    address_mask(env, &addr);
3002 3003
    u.ll.upper = ldq_raw(addr);
    u.ll.lower = ldq_raw((addr + 8) & 0xffffffffULL);
B
blueswir1 已提交
3004
    QT0 = u.q;
B
blueswir1 已提交
3005
#endif
B
blueswir1 已提交
3006 3007
}

B
blueswir1 已提交
3008
void helper_stqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
3009 3010 3011 3012
{
    // XXX add 128 bit store
    CPU_QuadU u;

3013
    helper_check_align(addr, 7);
B
blueswir1 已提交
3014 3015 3016 3017
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
        u.q = QT0;
3018 3019
        stq_user(addr, u.ll.upper);
        stq_user(addr + 8, u.ll.lower);
B
blueswir1 已提交
3020 3021 3022
        break;
    case 1:
        u.q = QT0;
3023 3024
        stq_kernel(addr, u.ll.upper);
        stq_kernel(addr + 8, u.ll.lower);
B
blueswir1 已提交
3025 3026 3027 3028
        break;
#ifdef TARGET_SPARC64
    case 2:
        u.q = QT0;
3029 3030
        stq_hypv(addr, u.ll.upper);
        stq_hypv(addr + 8, u.ll.lower);
B
blueswir1 已提交
3031 3032 3033 3034 3035 3036
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
3037
    u.q = QT0;
B
blueswir1 已提交
3038
    address_mask(env, &addr);
3039 3040
    stq_raw(addr, u.ll.upper);
    stq_raw((addr + 8) & 0xffffffffULL, u.ll.lower);
B
blueswir1 已提交
3041
#endif
B
blueswir1 已提交
3042
}
B
blueswir1 已提交
3043

3044
static inline void set_fsr(void)
3045
{
B
bellard 已提交
3046
    int rnd_mode;
B
blueswir1 已提交
3047

3048 3049
    switch (env->fsr & FSR_RD_MASK) {
    case FSR_RD_NEAREST:
B
bellard 已提交
3050
        rnd_mode = float_round_nearest_even;
B
blueswir1 已提交
3051
        break;
B
bellard 已提交
3052
    default:
3053
    case FSR_RD_ZERO:
B
bellard 已提交
3054
        rnd_mode = float_round_to_zero;
B
blueswir1 已提交
3055
        break;
3056
    case FSR_RD_POS:
B
bellard 已提交
3057
        rnd_mode = float_round_up;
B
blueswir1 已提交
3058
        break;
3059
    case FSR_RD_NEG:
B
bellard 已提交
3060
        rnd_mode = float_round_down;
B
blueswir1 已提交
3061
        break;
3062
    }
B
bellard 已提交
3063
    set_float_rounding_mode(rnd_mode, &env->fp_status);
3064
}
B
bellard 已提交
3065

3066
void helper_ldfsr(uint32_t new_fsr)
B
blueswir1 已提交
3067
{
3068 3069
    env->fsr = (new_fsr & FSR_LDFSR_MASK) | (env->fsr & FSR_LDFSR_OLDMASK);
    set_fsr();
B
blueswir1 已提交
3070 3071
}

3072 3073 3074 3075 3076 3077 3078 3079
#ifdef TARGET_SPARC64
void helper_ldxfsr(uint64_t new_fsr)
{
    env->fsr = (new_fsr & FSR_LDXFSR_MASK) | (env->fsr & FSR_LDXFSR_OLDMASK);
    set_fsr();
}
#endif

B
blueswir1 已提交
3080
void helper_debug(void)
B
bellard 已提交
3081 3082 3083 3084
{
    env->exception_index = EXCP_DEBUG;
    cpu_loop_exit();
}
3085

B
bellard 已提交
3086
#ifndef TARGET_SPARC64
3087 3088 3089 3090 3091 3092
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

3093
    cwp = cpu_cwp_dec(env, env->cwp - 1);
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_OVF);
    }
    set_cwp(cwp);
}

void helper_restore(void)
{
    uint32_t cwp;

3104
    cwp = cpu_cwp_inc(env, env->cwp + 1);
3105 3106 3107 3108 3109 3110
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
}

B
blueswir1 已提交
3111
void helper_wrpsr(target_ulong new_psr)
3112
{
3113
    if ((new_psr & PSR_CWP) >= env->nwindows)
3114 3115
        raise_exception(TT_ILL_INSN);
    else
B
blueswir1 已提交
3116
        PUT_PSR(env, new_psr);
3117 3118
}

B
blueswir1 已提交
3119
target_ulong helper_rdpsr(void)
3120
{
B
blueswir1 已提交
3121
    return GET_PSR(env);
3122
}
B
bellard 已提交
3123 3124

#else
3125 3126 3127 3128 3129 3130
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

3131
    cwp = cpu_cwp_dec(env, env->cwp - 1);
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
    if (env->cansave == 0) {
        raise_exception(TT_SPILL | (env->otherwin != 0 ?
                                    (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
                                    ((env->wstate & 0x7) << 2)));
    } else {
        if (env->cleanwin - env->canrestore == 0) {
            // XXX Clean windows without trap
            raise_exception(TT_CLRWIN);
        } else {
            env->cansave--;
            env->canrestore++;
            set_cwp(cwp);
        }
    }
}

void helper_restore(void)
{
    uint32_t cwp;

3152
    cwp = cpu_cwp_inc(env, env->cwp + 1);
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
    if (env->canrestore == 0) {
        raise_exception(TT_FILL | (env->otherwin != 0 ?
                                   (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
                                   ((env->wstate & 0x7) << 2)));
    } else {
        env->cansave++;
        env->canrestore--;
        set_cwp(cwp);
    }
}

void helper_flushw(void)
{
3166
    if (env->cansave != env->nwindows - 2) {
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
        raise_exception(TT_SPILL | (env->otherwin != 0 ?
                                    (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
                                    ((env->wstate & 0x7) << 2)));
    }
}

void helper_saved(void)
{
    env->cansave++;
    if (env->otherwin == 0)
        env->canrestore--;
    else
        env->otherwin--;
}

void helper_restored(void)
{
    env->canrestore++;
3185
    if (env->cleanwin < env->nwindows - 1)
3186 3187 3188 3189 3190 3191 3192
        env->cleanwin++;
    if (env->otherwin == 0)
        env->cansave--;
    else
        env->otherwin--;
}

B
blueswir1 已提交
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
target_ulong helper_rdccr(void)
{
    return GET_CCR(env);
}

void helper_wrccr(target_ulong new_ccr)
{
    PUT_CCR(env, new_ccr);
}

// CWP handling is reversed in V9, but we still use the V8 register
// order.
target_ulong helper_rdcwp(void)
{
    return GET_CWP64(env);
}

void helper_wrcwp(target_ulong new_cwp)
{
    PUT_CWP64(env, new_cwp);
}
B
bellard 已提交
3214

3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245
// This function uses non-native bit order
#define GET_FIELD(X, FROM, TO)                                  \
    ((X) >> (63 - (TO)) & ((1ULL << ((TO) - (FROM) + 1)) - 1))

// This function uses the order in the manuals, i.e. bit 0 is 2^0
#define GET_FIELD_SP(X, FROM, TO)               \
    GET_FIELD(X, 63 - (TO), 63 - (FROM))

target_ulong helper_array8(target_ulong pixel_addr, target_ulong cubesize)
{
    return (GET_FIELD_SP(pixel_addr, 60, 63) << (17 + 2 * cubesize)) |
        (GET_FIELD_SP(pixel_addr, 39, 39 + cubesize - 1) << (17 + cubesize)) |
        (GET_FIELD_SP(pixel_addr, 17 + cubesize - 1, 17) << 17) |
        (GET_FIELD_SP(pixel_addr, 56, 59) << 13) |
        (GET_FIELD_SP(pixel_addr, 35, 38) << 9) |
        (GET_FIELD_SP(pixel_addr, 13, 16) << 5) |
        (((pixel_addr >> 55) & 1) << 4) |
        (GET_FIELD_SP(pixel_addr, 33, 34) << 2) |
        GET_FIELD_SP(pixel_addr, 11, 12);
}

target_ulong helper_alignaddr(target_ulong addr, target_ulong offset)
{
    uint64_t tmp;

    tmp = addr + offset;
    env->gsr &= ~7ULL;
    env->gsr |= tmp & 7ULL;
    return tmp & ~7ULL;
}

B
blueswir1 已提交
3246
target_ulong helper_popc(target_ulong val)
B
bellard 已提交
3247
{
B
blueswir1 已提交
3248
    return ctpop64(val);
B
bellard 已提交
3249
}
B
bellard 已提交
3250

3251
static inline uint64_t *get_gregset(uint32_t pstate)
B
bellard 已提交
3252 3253 3254
{
    switch (pstate) {
    default:
3255 3256 3257 3258 3259 3260
        DPRINTF_PSTATE("ERROR in get_gregset: active pstate bits=%x%s%s%s\n",
                pstate,
                (pstate & PS_IG) ? " IG" : "",
                (pstate & PS_MG) ? " MG" : "",
                (pstate & PS_AG) ? " AG" : "");
        /* pass through to normal set of global registers */
B
bellard 已提交
3261
    case 0:
B
blueswir1 已提交
3262
        return env->bgregs;
B
bellard 已提交
3263
    case PS_AG:
B
blueswir1 已提交
3264
        return env->agregs;
B
bellard 已提交
3265
    case PS_MG:
B
blueswir1 已提交
3266
        return env->mgregs;
B
bellard 已提交
3267
    case PS_IG:
B
blueswir1 已提交
3268
        return env->igregs;
B
bellard 已提交
3269 3270 3271
    }
}

3272
static inline void change_pstate(uint32_t new_pstate)
B
bellard 已提交
3273
{
3274
    uint32_t pstate_regs, new_pstate_regs;
B
bellard 已提交
3275 3276
    uint64_t *src, *dst;

3277 3278 3279 3280 3281
    if (env->def->features & CPU_FEATURE_GL) {
        // PS_AG is not implemented in this case
        new_pstate &= ~PS_AG;
    }

B
bellard 已提交
3282 3283
    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
3284

B
bellard 已提交
3285
    if (new_pstate_regs != pstate_regs) {
3286 3287
        DPRINTF_PSTATE("change_pstate: switching regs old=%x new=%x\n",
                       pstate_regs, new_pstate_regs);
B
blueswir1 已提交
3288 3289 3290 3291 3292
        // Switch global register bank
        src = get_gregset(new_pstate_regs);
        dst = get_gregset(pstate_regs);
        memcpy32(dst, env->gregs);
        memcpy32(env->gregs, src);
B
bellard 已提交
3293
    }
3294 3295 3296 3297
    else {
        DPRINTF_PSTATE("change_pstate: regs new=%x (unchanged)\n",
                       new_pstate_regs);
    }
B
bellard 已提交
3298 3299 3300
    env->pstate = new_pstate;
}

B
blueswir1 已提交
3301
void helper_wrpstate(target_ulong new_state)
3302
{
3303
    change_pstate(new_state & 0xf3f);
3304 3305 3306 3307 3308 3309

#if !defined(CONFIG_USER_ONLY)
    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
3310 3311
}

3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
void helper_wrpil(target_ulong new_pil)
{
#if !defined(CONFIG_USER_ONLY)
    DPRINTF_PSTATE("helper_wrpil old=%x new=%x\n",
                   env->psrpil, (uint32_t)new_pil);

    env->psrpil = new_pil;

    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
}

B
blueswir1 已提交
3326
void helper_done(void)
B
bellard 已提交
3327
{
3328 3329
    trap_state* tsptr = cpu_tsptr(env);

3330
    env->pc = tsptr->tnpc;
3331 3332 3333 3334 3335
    env->npc = tsptr->tnpc + 4;
    PUT_CCR(env, tsptr->tstate >> 32);
    env->asi = (tsptr->tstate >> 24) & 0xff;
    change_pstate((tsptr->tstate >> 8) & 0xf3f);
    PUT_CWP64(env, tsptr->tstate & 0xff);
B
blueswir1 已提交
3336
    env->tl--;
3337 3338 3339 3340 3341 3342 3343 3344

    DPRINTF_PSTATE("... helper_done tl=%d\n", env->tl);

#if !defined(CONFIG_USER_ONLY)
    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
B
bellard 已提交
3345 3346
}

B
blueswir1 已提交
3347
void helper_retry(void)
B
bellard 已提交
3348
{
3349 3350 3351 3352 3353 3354 3355 3356
    trap_state* tsptr = cpu_tsptr(env);

    env->pc = tsptr->tpc;
    env->npc = tsptr->tnpc;
    PUT_CCR(env, tsptr->tstate >> 32);
    env->asi = (tsptr->tstate >> 24) & 0xff;
    change_pstate((tsptr->tstate >> 8) & 0xf3f);
    PUT_CWP64(env, tsptr->tstate & 0xff);
B
blueswir1 已提交
3357
    env->tl--;
3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378

    DPRINTF_PSTATE("... helper_retry tl=%d\n", env->tl);

#if !defined(CONFIG_USER_ONLY)
    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
}

static void do_modify_softint(const char* operation, uint32_t value)
{
    if (env->softint != value) {
        env->softint = value;
        DPRINTF_PSTATE(": %s new %08x\n", operation, env->softint);
#if !defined(CONFIG_USER_ONLY)
        if (cpu_interrupts_enabled(env)) {
            cpu_check_irqs(env);
        }
#endif
    }
B
bellard 已提交
3379
}
3380 3381 3382

void helper_set_softint(uint64_t value)
{
3383
    do_modify_softint("helper_set_softint", env->softint | (uint32_t)value);
3384 3385 3386 3387
}

void helper_clear_softint(uint64_t value)
{
3388
    do_modify_softint("helper_clear_softint", env->softint & (uint32_t)~value);
3389 3390 3391 3392
}

void helper_write_softint(uint64_t value)
{
3393
    do_modify_softint("helper_write_softint", (uint32_t)value);
3394
}
B
bellard 已提交
3395
#endif
3396

B
blueswir1 已提交
3397
void helper_flush(target_ulong addr)
3398
{
B
blueswir1 已提交
3399 3400
    addr &= ~7;
    tb_invalidate_page_range(addr, addr + 8);
3401 3402
}

B
blueswir1 已提交
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439
#ifdef TARGET_SPARC64
#ifdef DEBUG_PCALL
static const char * const excp_names[0x80] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_TMISS] = "Instruction Access MMU Miss",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_TOVF] = "Tag Overflow",
    [TT_CLRWIN] = "Clean Windows",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_DFAULT] = "Data Access Fault",
    [TT_DMISS] = "Data Access MMU Miss",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DPROT] = "Data Protection Error",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_PRIV_ACT] = "Privileged Action",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
};
#endif

3440 3441 3442 3443 3444
trap_state* cpu_tsptr(CPUState* env)
{
    return &env->ts[env->tl & MAXTL_MASK];
}

B
blueswir1 已提交
3445 3446 3447
void do_interrupt(CPUState *env)
{
    int intno = env->exception_index;
3448
    trap_state* tsptr;
B
blueswir1 已提交
3449 3450

#ifdef DEBUG_PCALL
3451
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
        static int count;
        const char *name;

        if (intno < 0 || intno >= 0x180)
            name = "Unknown";
        else if (intno >= 0x100)
            name = "Trap Instruction";
        else if (intno >= 0xc0)
            name = "Window Fill";
        else if (intno >= 0x80)
            name = "Window Spill";
        else {
            name = excp_names[intno];
            if (!name)
                name = "Unknown";
        }

3469
        qemu_log("%6d: %s (v=%04x) pc=%016" PRIx64 " npc=%016" PRIx64
B
blueswir1 已提交
3470 3471 3472 3473
                " SP=%016" PRIx64 "\n",
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
3474
        log_cpu_state(env, 0);
B
blueswir1 已提交
3475 3476 3477 3478 3479
#if 0
        {
            int i;
            uint8_t *ptr;

3480
            qemu_log("       code=");
B
blueswir1 已提交
3481 3482
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
3483
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
3484
            }
3485
            qemu_log("\n");
B
blueswir1 已提交
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
        }
#endif
        count++;
    }
#endif
#if !defined(CONFIG_USER_ONLY)
    if (env->tl >= env->maxtl) {
        cpu_abort(env, "Trap 0x%04x while trap level (%d) >= MAXTL (%d),"
                  " Error state", env->exception_index, env->tl, env->maxtl);
        return;
    }
#endif
    if (env->tl < env->maxtl - 1) {
        env->tl++;
    } else {
        env->pstate |= PS_RED;
        if (env->tl < env->maxtl)
            env->tl++;
    }
3505 3506 3507
    tsptr = cpu_tsptr(env);

    tsptr->tstate = ((uint64_t)GET_CCR(env) << 32) |
B
blueswir1 已提交
3508 3509
        ((env->asi & 0xff) << 24) | ((env->pstate & 0xf3f) << 8) |
        GET_CWP64(env);
3510 3511 3512
    tsptr->tpc = env->pc;
    tsptr->tnpc = env->npc;
    tsptr->tt = intno;
3513 3514 3515 3516 3517 3518 3519

    switch (intno) {
    case TT_IVEC:
        change_pstate(PS_PEF | PS_PRIV | PS_IG);
        break;
    case TT_TFAULT:
    case TT_DFAULT:
3520 3521 3522
    case TT_TMISS ... TT_TMISS + 3:
    case TT_DMISS ... TT_DMISS + 3:
    case TT_DPROT ... TT_DPROT + 3:
3523 3524 3525 3526 3527
        change_pstate(PS_PEF | PS_PRIV | PS_MG);
        break;
    default:
        change_pstate(PS_PEF | PS_PRIV | PS_AG);
        break;
B
blueswir1 已提交
3528
    }
3529

B
blueswir1 已提交
3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
    if (intno == TT_CLRWIN)
        cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - 1));
    else if ((intno & 0x1c0) == TT_SPILL)
        cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
    else if ((intno & 0x1c0) == TT_FILL)
        cpu_set_cwp(env, cpu_cwp_inc(env, env->cwp + 1));
    env->tbr &= ~0x7fffULL;
    env->tbr |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
    env->pc = env->tbr;
    env->npc = env->pc + 4;
3540
    env->exception_index = -1;
3541
}
B
blueswir1 已提交
3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
#else
#ifdef DEBUG_PCALL
static const char * const excp_names[0x80] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_WIN_OVF] = "Window Overflow",
    [TT_WIN_UNF] = "Window Underflow",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_DFAULT] = "Data Access Fault",
    [TT_TOVF] = "Tag Overflow",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
    [TT_TOVF] = "Tag Overflow",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_NCP_INSN] = "Coprocessor Disabled",
};
#endif
3577

B
blueswir1 已提交
3578
void do_interrupt(CPUState *env)
3579
{
B
blueswir1 已提交
3580 3581 3582
    int cwp, intno = env->exception_index;

#ifdef DEBUG_PCALL
3583
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
        static int count;
        const char *name;

        if (intno < 0 || intno >= 0x100)
            name = "Unknown";
        else if (intno >= 0x80)
            name = "Trap Instruction";
        else {
            name = excp_names[intno];
            if (!name)
                name = "Unknown";
        }

3597
        qemu_log("%6d: %s (v=%02x) pc=%08x npc=%08x SP=%08x\n",
B
blueswir1 已提交
3598 3599 3600
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
3601
        log_cpu_state(env, 0);
B
blueswir1 已提交
3602 3603 3604 3605 3606
#if 0
        {
            int i;
            uint8_t *ptr;

3607
            qemu_log("       code=");
B
blueswir1 已提交
3608 3609
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
3610
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
3611
            }
3612
            qemu_log("\n");
B
blueswir1 已提交
3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
        }
#endif
        count++;
    }
#endif
#if !defined(CONFIG_USER_ONLY)
    if (env->psret == 0) {
        cpu_abort(env, "Trap 0x%02x while interrupts disabled, Error state",
                  env->exception_index);
        return;
    }
#endif
    env->psret = 0;
    cwp = cpu_cwp_dec(env, env->cwp - 1);
    cpu_set_cwp(env, cwp);
    env->regwptr[9] = env->pc;
    env->regwptr[10] = env->npc;
    env->psrps = env->psrs;
    env->psrs = 1;
    env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
    env->pc = env->tbr;
    env->npc = env->pc + 4;
3635
    env->exception_index = -1;
3636
}
B
blueswir1 已提交
3637
#endif
3638

3639
#if !defined(CONFIG_USER_ONLY)
3640

3641 3642 3643
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

3644
#define MMUSUFFIX _mmu
3645
#define ALIGNED_ONLY
3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658

#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"

3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676
/* XXX: make it generic ? */
static void cpu_restore_state2(void *retaddr)
{
    TranslationBlock *tb;
    unsigned long pc;

    if (retaddr) {
        /* now we have a real cpu fault */
        pc = (unsigned long)retaddr;
        tb = tb_find_pc(pc);
        if (tb) {
            /* the PC is inside the translated code. It means that we have
               a virtual CPU fault */
            cpu_restore_state(tb, env, pc, (void *)(long)env->cond);
        }
    }
}

3677 3678 3679
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr)
{
B
blueswir1 已提交
3680
#ifdef DEBUG_UNALIGNED
3681 3682
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
           "\n", addr, env->pc);
B
blueswir1 已提交
3683
#endif
3684
    cpu_restore_state2(retaddr);
B
blueswir1 已提交
3685
    raise_exception(TT_UNALIGNED);
3686
}
3687 3688 3689 3690 3691

/* 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 */
3692
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
3693 3694 3695 3696 3697 3698 3699 3700 3701
{
    int ret;
    CPUState *saved_env;

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;

3702
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
3703
    if (ret) {
3704
        cpu_restore_state2(retaddr);
3705 3706 3707 3708 3709 3710
        cpu_loop_exit();
    }
    env = saved_env;
}

#endif
3711 3712

#ifndef TARGET_SPARC64
A
Anthony Liguori 已提交
3713
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3714
                          int is_asi, int size)
3715 3716
{
    CPUState *saved_env;
3717
    int fault_type;
3718 3719 3720 3721 3722

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;
3723 3724
#ifdef DEBUG_UNASSIGNED
    if (is_asi)
3725
        printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
B
blueswir1 已提交
3726
               " asi 0x%02x from " TARGET_FMT_lx "\n",
3727 3728
               is_exec ? "exec" : is_write ? "write" : "read", size,
               size == 1 ? "" : "s", addr, is_asi, env->pc);
3729
    else
3730 3731 3732 3733
        printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
               " from " TARGET_FMT_lx "\n",
               is_exec ? "exec" : is_write ? "write" : "read", size,
               size == 1 ? "" : "s", addr, env->pc);
3734
#endif
3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757
    /* Don't overwrite translation and access faults */
    fault_type = (env->mmuregs[3] & 0x1c) >> 2;
    if ((fault_type > 4) || (fault_type == 0)) {
        env->mmuregs[3] = 0; /* Fault status register */
        if (is_asi)
            env->mmuregs[3] |= 1 << 16;
        if (env->psrs)
            env->mmuregs[3] |= 1 << 5;
        if (is_exec)
            env->mmuregs[3] |= 1 << 6;
        if (is_write)
            env->mmuregs[3] |= 1 << 7;
        env->mmuregs[3] |= (5 << 2) | 2;
        /* SuperSPARC will never place instruction fault addresses in the FAR */
        if (!is_exec) {
            env->mmuregs[4] = addr; /* Fault address register */
        }
    }
    /* overflow (same type fault was not read before another fault) */
    if (fault_type == ((env->mmuregs[3] & 0x1c)) >> 2) {
        env->mmuregs[3] |= 1;
    }

3758
    if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
3759 3760 3761 3762
        if (is_exec)
            raise_exception(TT_CODE_ACCESS);
        else
            raise_exception(TT_DATA_ACCESS);
3763
    }
3764 3765 3766 3767 3768 3769

    /* flush neverland mappings created during no-fault mode,
       so the sequential MMU faults report proper fault types */
    if (env->mmuregs[0] & MMU_NF) {
        tlb_flush(env, 1);
    }
3770 3771

    env = saved_env;
3772 3773
}
#else
A
Anthony Liguori 已提交
3774
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3775
                          int is_asi, int size)
3776 3777 3778 3779 3780 3781 3782
{
    CPUState *saved_env;

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;
3783 3784

#ifdef DEBUG_UNASSIGNED
B
blueswir1 已提交
3785 3786
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
           "\n", addr, env->pc);
3787
#endif
3788

3789 3790 3791 3792
    if (is_exec)
        raise_exception(TT_CODE_ACCESS);
    else
        raise_exception(TT_DATA_ACCESS);
3793 3794

    env = saved_env;
3795 3796
}
#endif
3797

B
blueswir1 已提交
3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
#ifdef TARGET_SPARC64
void helper_tick_set_count(void *opaque, uint64_t count)
{
#if !defined(CONFIG_USER_ONLY)
    cpu_tick_set_count(opaque, count);
#endif
}

uint64_t helper_tick_get_count(void *opaque)
{
#if !defined(CONFIG_USER_ONLY)
    return cpu_tick_get_count(opaque);
#else
    return 0;
#endif
}

void helper_tick_set_limit(void *opaque, uint64_t limit)
{
#if !defined(CONFIG_USER_ONLY)
    cpu_tick_set_limit(opaque, limit);
#endif
}
#endif