op_helper.c 106.0 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

P
Paul Brook 已提交
50 51 52 53 54
#if defined(CONFIG_USER_ONLY) && defined(TARGET_SPARC64)
static void do_unassigned_access(target_ulong addr, int is_write, int is_exec,
                          int is_asi, int size);
#endif

55
#if defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
56 57 58 59 60 61
// 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;
62 63
    int tsb_split = (tsb_register & 0x1000ULL) ? 1 : 0;
    int tsb_size  = tsb_register & 0xf;
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 98 99 100 101 102

    // 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);
}

103 104 105
static void replace_tlb_entry(SparcTLBEntry *tlb,
                              uint64_t tlb_tag, uint64_t tlb_tte,
                              CPUState *env1)
106 107 108 109
{
    target_ulong mask, size, va, offset;

    // flush page range if translation is valid
110
    if (TTE_IS_VALID(tlb->tte)) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

        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,
128
                      const char* strmmu, CPUState *env1)
129 130 131
{
    unsigned int i;
    target_ulong mask;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    uint64_t context;

    int is_demap_context = (demap_addr >> 6) & 1;

    // demap context
    switch ((demap_addr >> 4) & 3) {
    case 0: // primary
        context = env1->dmmu.mmu_primary_context;
        break;
    case 1: // secondary
        context = env1->dmmu.mmu_secondary_context;
        break;
    case 2: // nucleus
        context = 0;
        break;
    case 3: // reserved
    default:
        return;
    }
151 152

    for (i = 0; i < 64; i++) {
153
        if (TTE_IS_VALID(tlb[i].tte)) {
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            if (is_demap_context) {
                // will remove non-global entries matching context value
                if (TTE_IS_GLOBAL(tlb[i].tte) ||
                    !tlb_compare_context(&tlb[i], context)) {
                    continue;
                }
            } else {
                // demap page
                // will remove any entry matching VA
                mask = 0xffffffffffffe000ULL;
                mask <<= 3 * ((tlb[i].tte >> 61) & 3);

                if (!compare_masked(demap_addr, tlb[i].tag, mask)) {
                    continue;
                }

                // entry should be global or matching context value
                if (!TTE_IS_GLOBAL(tlb[i].tte) &&
                    !tlb_compare_context(&tlb[i], context)) {
                    continue;
                }
            }
177

178
            replace_tlb_entry(&tlb[i], 0, 0, env1);
179
#ifdef DEBUG_MMU
180 181
            DPRINTF_MMU("%s demap invalidated entry [%02u]\n", strmmu, i);
            dump_mmu(env1);
182 183 184 185 186
#endif
        }
    }
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
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?
}

237 238
#endif

239
static inline target_ulong address_mask(CPUState *env1, target_ulong addr)
B
blueswir1 已提交
240 241 242
{
#ifdef TARGET_SPARC64
    if (AM_CHECK(env1))
243
        addr &= 0xffffffffULL;
B
blueswir1 已提交
244
#endif
245
    return addr;
B
blueswir1 已提交
246 247
}

B
blueswir1 已提交
248
static void raise_exception(int tt)
B
bellard 已提交
249 250 251
{
    env->exception_index = tt;
    cpu_loop_exit();
252
}
B
bellard 已提交
253

P
pbrook 已提交
254 255 256 257 258
void HELPER(raise_exception)(int tt)
{
    raise_exception(tt);
}

B
blueswir1 已提交
259 260 261 262 263
static inline void set_cwp(int new_cwp)
{
    cpu_set_cwp(env, new_cwp);
}

B
blueswir1 已提交
264 265
void helper_check_align(target_ulong addr, uint32_t align)
{
266 267 268 269 270
    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 已提交
271
        raise_exception(TT_UNALIGNED);
272
    }
B
blueswir1 已提交
273 274
}

275 276 277
#define F_HELPER(name, p) void helper_f##name##p(void)

#define F_BINOP(name)                                           \
B
blueswir1 已提交
278
    float32 helper_f ## name ## s (float32 src1, float32 src2)  \
279
    {                                                           \
B
blueswir1 已提交
280
        return float32_ ## name (src1, src2, &env->fp_status);  \
281 282 283 284
    }                                                           \
    F_HELPER(name, d)                                           \
    {                                                           \
        DT0 = float64_ ## name (DT0, DT1, &env->fp_status);     \
B
blueswir1 已提交
285 286 287 288
    }                                                           \
    F_HELPER(name, q)                                           \
    {                                                           \
        QT0 = float128_ ## name (QT0, QT1, &env->fp_status);    \
289 290 291 292 293 294 295 296
    }

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

297
void helper_fsmuld(float32 src1, float32 src2)
B
blueswir1 已提交
298
{
299 300
    DT0 = float64_mul(float32_to_float64(src1, &env->fp_status),
                      float32_to_float64(src2, &env->fp_status),
301 302
                      &env->fp_status);
}
B
blueswir1 已提交
303

B
blueswir1 已提交
304 305 306 307 308 309 310
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 已提交
311
float32 helper_fnegs(float32 src)
312
{
B
blueswir1 已提交
313
    return float32_chs(src);
314 315
}

316 317
#ifdef TARGET_SPARC64
F_HELPER(neg, d)
318
{
319
    DT0 = float64_chs(DT1);
320
}
B
blueswir1 已提交
321 322 323 324 325 326

F_HELPER(neg, q)
{
    QT0 = float128_chs(QT1);
}
#endif
327 328

/* Integer to float conversion.  */
B
blueswir1 已提交
329
float32 helper_fitos(int32_t src)
B
bellard 已提交
330
{
B
blueswir1 已提交
331
    return int32_to_float32(src, &env->fp_status);
B
bellard 已提交
332 333
}

334
void helper_fitod(int32_t src)
B
bellard 已提交
335
{
336
    DT0 = int32_to_float64(src, &env->fp_status);
B
bellard 已提交
337
}
338

339
void helper_fitoq(int32_t src)
B
blueswir1 已提交
340
{
341
    QT0 = int32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
342 343
}

B
blueswir1 已提交
344
#ifdef TARGET_SPARC64
345
float32 helper_fxtos(void)
B
blueswir1 已提交
346
{
347
    return int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
B
blueswir1 已提交
348 349
}

350
F_HELPER(xto, d)
B
blueswir1 已提交
351 352 353
{
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
}
B
blueswir1 已提交
354

B
blueswir1 已提交
355 356 357 358 359
F_HELPER(xto, q)
{
    QT0 = int64_to_float128(*((int64_t *)&DT1), &env->fp_status);
}
#endif
360 361 362
#undef F_HELPER

/* floating point conversion */
363
float32 helper_fdtos(void)
364
{
365
    return float64_to_float32(DT1, &env->fp_status);
366 367
}

368
void helper_fstod(float32 src)
369
{
370
    DT0 = float32_to_float64(src, &env->fp_status);
371
}
372

373
float32 helper_fqtos(void)
B
blueswir1 已提交
374
{
375
    return float128_to_float32(QT1, &env->fp_status);
B
blueswir1 已提交
376 377
}

378
void helper_fstoq(float32 src)
B
blueswir1 已提交
379
{
380
    QT0 = float32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
381 382 383 384 385 386 387 388 389 390 391 392
}

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

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

393
/* Float to integer conversion.  */
B
blueswir1 已提交
394
int32_t helper_fstoi(float32 src)
395
{
B
blueswir1 已提交
396
    return float32_to_int32_round_to_zero(src, &env->fp_status);
397 398
}

399
int32_t helper_fdtoi(void)
400
{
401
    return float64_to_int32_round_to_zero(DT1, &env->fp_status);
402 403
}

404
int32_t helper_fqtoi(void)
B
blueswir1 已提交
405
{
406
    return float128_to_int32_round_to_zero(QT1, &env->fp_status);
B
blueswir1 已提交
407 408
}

409
#ifdef TARGET_SPARC64
410
void helper_fstox(float32 src)
411
{
412
    *((int64_t *)&DT0) = float32_to_int64_round_to_zero(src, &env->fp_status);
413 414 415 416 417 418 419
}

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

B
blueswir1 已提交
420 421 422 423 424
void helper_fqtox(void)
{
    *((int64_t *)&DT0) = float128_to_int64_round_to_zero(QT1, &env->fp_status);
}

425 426 427 428 429
void helper_faligndata(void)
{
    uint64_t tmp;

    tmp = (*((uint64_t *)&DT0)) << ((env->gsr & 7) * 8);
B
blueswir1 已提交
430 431 432 433
    /* on many architectures a shift of 64 does nothing */
    if ((env->gsr & 7) != 0) {
        tmp |= (*((uint64_t *)&DT1)) >> (64 - (env->gsr & 7) * 8);
    }
434 435 436
    *((uint64_t *)&DT0) = tmp;
}

437
#ifdef HOST_WORDS_BIGENDIAN
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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
#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;
654 655 656 657
    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;
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

    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 已提交
678
    uint32_t name##16s(uint32_t src1, uint32_t src2)    \
679 680 681
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
682 683
        s.l = src1;                                     \
        d.l = src2;                                     \
684 685 686 687
                                                        \
        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 已提交
688
        return d.l;                                     \
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
    }                                                   \
                                                        \
    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 已提交
704
    uint32_t name##32s(uint32_t src1, uint32_t src2)    \
705 706 707
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
708 709
        s.l = src1;                                     \
        d.l = src2;                                     \
710 711 712
                                                        \
        d.l = F(d.l, s.l);                              \
                                                        \
B
blueswir1 已提交
713
        return d.l;                                     \
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 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    }

#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 已提交
795
float32 helper_fabss(float32 src)
796
{
B
blueswir1 已提交
797
    return float32_abs(src);
798 799
}

B
bellard 已提交
800
#ifdef TARGET_SPARC64
801
void helper_fabsd(void)
B
bellard 已提交
802 803 804
{
    DT0 = float64_abs(DT1);
}
B
blueswir1 已提交
805 806 807 808 809 810

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

B
blueswir1 已提交
812
float32 helper_fsqrts(float32 src)
813
{
B
blueswir1 已提交
814
    return float32_sqrt(src, &env->fp_status);
815 816
}

817
void helper_fsqrtd(void)
818
{
B
bellard 已提交
819
    DT0 = float64_sqrt(DT1, &env->fp_status);
820 821
}

B
blueswir1 已提交
822 823 824 825 826
void helper_fsqrtq(void)
{
    QT0 = float128_sqrt(QT1, &env->fp_status);
}

827
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
828
    void glue(helper_, name) (void)                                     \
B
bellard 已提交
829
    {                                                                   \
B
blueswir1 已提交
830 831
        target_ulong new_fsr;                                           \
                                                                        \
B
bellard 已提交
832 833 834
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
B
blueswir1 已提交
835
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
836
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
B
blueswir1 已提交
837
                env->fsr |= new_fsr;                                    \
838 839
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
B
bellard 已提交
840 841 842 843 844 845
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
B
blueswir1 已提交
846
            new_fsr = FSR_FCC0 << FS;                                   \
B
bellard 已提交
847 848
            break;                                                      \
        case float_relation_greater:                                    \
B
blueswir1 已提交
849
            new_fsr = FSR_FCC1 << FS;                                   \
B
bellard 已提交
850 851
            break;                                                      \
        default:                                                        \
B
blueswir1 已提交
852
            new_fsr = 0;                                                \
B
bellard 已提交
853 854
            break;                                                      \
        }                                                               \
B
blueswir1 已提交
855
        env->fsr |= new_fsr;                                            \
856
    }
B
blueswir1 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
#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;                                            \
    }
887

B
blueswir1 已提交
888
GEN_FCMPS(fcmps, float32, 0, 0);
889 890
GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);

B
blueswir1 已提交
891
GEN_FCMPS(fcmpes, float32, 0, 1);
892
GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
B
bellard 已提交
893

B
blueswir1 已提交
894 895 896
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);

897 898 899 900 901 902 903 904 905 906
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 已提交
907 908 909 910 911 912 913 914 915 916 917
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;
}

918 919 920 921 922 923 924 925 926 927 928
#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 已提交
929 930 931 932 933 934 935 936 937 938 939 940
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 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
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;
}

964 965 966
/* 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 已提交
967 968 969
{
    uint32_t ret = 0;

970 971 972
    if (((src1 & (1ULL << 31)) & (src2 & (1ULL << 31)))
        | ((~(dst & (1ULL << 31)))
           & ((src1 & (1ULL << 31)) | (src2 & (1ULL << 31)))))
B
Blue Swirl 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
        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);
}
1021 1022
#endif

1023
static uint32_t compute_all_add(void)
B
Blue Swirl 已提交
1024 1025 1026 1027
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1028
    ret |= get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1029 1030 1031 1032
    ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

1033
static uint32_t compute_C_add(void)
B
Blue Swirl 已提交
1034
{
1035
    return get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
}

#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 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
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);
1074
    ret |= get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1075 1076 1077 1078 1079 1080 1081
    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)
{
1082
    return get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1083 1084 1085 1086 1087 1088 1089
}

static uint32_t compute_all_taddtv(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1090
    ret |= get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1091 1092 1093 1094 1095
    return ret;
}

static uint32_t compute_C_taddtv(void)
{
1096
    return get_C_add_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1097 1098
}

1099 1100 1101
/* 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 已提交
1102 1103 1104
{
    uint32_t ret = 0;

1105 1106 1107
    if (((~(src1 & (1ULL << 31))) & (src2 & (1ULL << 31)))
        | ((dst & (1ULL << 31)) & (( ~(src1 & (1ULL << 31)))
                                   | (src2 & (1ULL << 31)))))
B
Blue Swirl 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
        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

1159
static uint32_t compute_all_sub(void)
B
Blue Swirl 已提交
1160 1161 1162 1163
{
    uint32_t ret;

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

1169
static uint32_t compute_C_sub(void)
B
Blue Swirl 已提交
1170
{
1171
    return get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
}

#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 已提交
1196 1197 1198 1199 1200
static uint32_t compute_all_tsub(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1201
    ret |= get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1202 1203 1204 1205 1206 1207 1208
    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)
{
1209
    return get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1210 1211 1212 1213 1214 1215 1216
}

static uint32_t compute_all_tsubtv(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1217
    ret |= get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1218 1219 1220 1221 1222
    return ret;
}

static uint32_t compute_C_tsubtv(void)
{
1223
    return get_C_sub_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1224 1225
}

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
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

1243 1244 1245 1246 1247 1248 1249 1250
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 已提交
1251
    [CC_OP_DIV] = { compute_all_div, compute_C_div },
B
Blue Swirl 已提交
1252
    [CC_OP_ADD] = { compute_all_add, compute_C_add },
1253
    [CC_OP_ADDX] = { compute_all_add, compute_C_add },
B
Blue Swirl 已提交
1254 1255
    [CC_OP_TADD] = { compute_all_tadd, compute_C_tadd },
    [CC_OP_TADDTV] = { compute_all_taddtv, compute_C_taddtv },
B
Blue Swirl 已提交
1256
    [CC_OP_SUB] = { compute_all_sub, compute_C_sub },
1257
    [CC_OP_SUBX] = { compute_all_sub, compute_C_sub },
B
Blue Swirl 已提交
1258 1259
    [CC_OP_TSUB] = { compute_all_tsub, compute_C_tsub },
    [CC_OP_TSUBTV] = { compute_all_tsubtv, compute_C_tsubtv },
1260
    [CC_OP_LOGIC] = { compute_all_logic, compute_C_logic },
1261 1262 1263 1264 1265 1266
};

#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 已提交
1267
    [CC_OP_DIV] = { compute_all_logic_xcc, compute_C_logic },
B
Blue Swirl 已提交
1268
    [CC_OP_ADD] = { compute_all_add_xcc, compute_C_add_xcc },
B
Blue Swirl 已提交
1269
    [CC_OP_ADDX] = { compute_all_addx_xcc, compute_C_addx_xcc },
B
Blue Swirl 已提交
1270 1271
    [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 已提交
1272
    [CC_OP_SUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
B
Blue Swirl 已提交
1273
    [CC_OP_SUBX] = { compute_all_subx_xcc, compute_C_subx_xcc },
B
Blue Swirl 已提交
1274 1275
    [CC_OP_TSUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
    [CC_OP_TSUBTV] = { compute_all_sub_xcc, compute_C_sub_xcc },
1276
    [CC_OP_LOGIC] = { compute_all_logic_xcc, compute_C_logic },
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
};
#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 已提交
1301
#ifdef TARGET_SPARC64
B
blueswir1 已提交
1302
GEN_FCMPS(fcmps_fcc1, float32, 22, 0);
1303
GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
B
blueswir1 已提交
1304
GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
1305

B
blueswir1 已提交
1306
GEN_FCMPS(fcmps_fcc2, float32, 24, 0);
1307
GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
B
blueswir1 已提交
1308
GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
1309

B
blueswir1 已提交
1310
GEN_FCMPS(fcmps_fcc3, float32, 26, 0);
1311
GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
B
blueswir1 已提交
1312
GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
1313

B
blueswir1 已提交
1314
GEN_FCMPS(fcmpes_fcc1, float32, 22, 1);
1315
GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
B
blueswir1 已提交
1316
GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
B
bellard 已提交
1317

B
blueswir1 已提交
1318
GEN_FCMPS(fcmpes_fcc2, float32, 24, 1);
1319
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
B
blueswir1 已提交
1320
GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
B
bellard 已提交
1321

B
blueswir1 已提交
1322
GEN_FCMPS(fcmpes_fcc3, float32, 26, 1);
1323
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
B
blueswir1 已提交
1324 1325
GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
#endif
B
blueswir1 已提交
1326
#undef GEN_FCMPS
B
bellard 已提交
1327

B
blueswir1 已提交
1328 1329
#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
    defined(DEBUG_MXCC)
1330 1331
static void dump_mxcc(CPUState *env)
{
1332 1333
    printf("mxccdata: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n",
B
blueswir1 已提交
1334 1335
           env->mxccdata[0], env->mxccdata[1],
           env->mxccdata[2], env->mxccdata[3]);
1336 1337 1338 1339
    printf("mxccregs: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n"
           "          %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n",
B
blueswir1 已提交
1340 1341 1342 1343
           env->mxccregs[0], env->mxccregs[1],
           env->mxccregs[2], env->mxccregs[3],
           env->mxccregs[4], env->mxccregs[5],
           env->mxccregs[6], env->mxccregs[7]);
1344 1345 1346
}
#endif

B
blueswir1 已提交
1347 1348 1349 1350
#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)
1351 1352 1353 1354
{
    switch (size)
    {
    case 1:
B
blueswir1 已提交
1355 1356
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xff);
1357 1358
        break;
    case 2:
B
blueswir1 已提交
1359 1360
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffff);
1361 1362
        break;
    case 4:
B
blueswir1 已提交
1363 1364
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffffffff);
1365 1366
        break;
    case 8:
B
blueswir1 已提交
1367 1368
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
                    addr, asi, r1);
1369 1370 1371 1372 1373
        break;
    }
}
#endif

B
blueswir1 已提交
1374 1375 1376
#ifndef TARGET_SPARC64
#ifndef CONFIG_USER_ONLY
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1377
{
B
blueswir1 已提交
1378
    uint64_t ret = 0;
1379
#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
B
blueswir1 已提交
1380
    uint32_t last_addr = addr;
1381
#endif
B
bellard 已提交
1382

1383
    helper_check_align(addr, size - 1);
B
bellard 已提交
1384
    switch (asi) {
1385
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
1386
        switch (addr) {
1387
        case 0x01c00a00: /* MXCC control register */
B
blueswir1 已提交
1388 1389 1390
            if (size == 8)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1391 1392
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1393 1394 1395 1396 1397
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1398 1399
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1400
            break;
1401 1402
        case 0x01c00c00: /* Module reset register */
            if (size == 8) {
B
blueswir1 已提交
1403
                ret = env->mxccregs[5];
1404 1405
                // should we do something here?
            } else
B
blueswir1 已提交
1406 1407
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1408
            break;
1409
        case 0x01c00f00: /* MBus port address register */
B
blueswir1 已提交
1410 1411 1412
            if (size == 8)
                ret = env->mxccregs[7];
            else
B
blueswir1 已提交
1413 1414
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1415 1416
            break;
        default:
B
blueswir1 已提交
1417 1418
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1419 1420
            break;
        }
B
blueswir1 已提交
1421
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
1422
                     "addr = %08x -> ret = %" PRIx64 ","
B
blueswir1 已提交
1423
                     "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
1424 1425 1426
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1427
        break;
1428
    case 3: /* MMU probe */
B
blueswir1 已提交
1429 1430 1431
        {
            int mmulev;

B
blueswir1 已提交
1432
            mmulev = (addr >> 8) & 15;
B
blueswir1 已提交
1433 1434
            if (mmulev > 4)
                ret = 0;
B
blueswir1 已提交
1435 1436 1437 1438
            else
                ret = mmu_probe(env, addr, mmulev);
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
                        addr, mmulev, ret);
B
blueswir1 已提交
1439 1440
        }
        break;
1441
    case 4: /* read MMU regs */
B
blueswir1 已提交
1442
        {
B
blueswir1 已提交
1443
            int reg = (addr >> 8) & 0x1f;
1444

B
blueswir1 已提交
1445 1446
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
B
blueswir1 已提交
1447 1448 1449 1450 1451
                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 已提交
1452
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
B
blueswir1 已提交
1453 1454
        }
        break;
B
blueswir1 已提交
1455 1456 1457 1458
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1459 1460 1461
    case 9: /* Supervisor code access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1462
            ret = ldub_code(addr);
1463 1464
            break;
        case 2:
1465
            ret = lduw_code(addr);
1466 1467 1468
            break;
        default:
        case 4:
1469
            ret = ldl_code(addr);
1470 1471
            break;
        case 8:
1472
            ret = ldq_code(addr);
1473 1474 1475
            break;
        }
        break;
1476 1477 1478
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1479
            ret = ldub_user(addr);
1480 1481
            break;
        case 2:
1482
            ret = lduw_user(addr);
1483 1484 1485
            break;
        default:
        case 4:
1486
            ret = ldl_user(addr);
1487 1488
            break;
        case 8:
1489
            ret = ldq_user(addr);
1490 1491 1492 1493 1494 1495
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1496
            ret = ldub_kernel(addr);
1497 1498
            break;
        case 2:
1499
            ret = lduw_kernel(addr);
1500 1501 1502
            break;
        default:
        case 4:
1503
            ret = ldl_kernel(addr);
1504 1505
            break;
        case 8:
1506
            ret = ldq_kernel(addr);
1507 1508 1509
            break;
        }
        break;
1510 1511 1512 1513 1514 1515
    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 已提交
1516 1517
        switch(size) {
        case 1:
B
blueswir1 已提交
1518
            ret = ldub_phys(addr);
B
bellard 已提交
1519 1520
            break;
        case 2:
1521
            ret = lduw_phys(addr);
B
bellard 已提交
1522 1523 1524
            break;
        default:
        case 4:
1525
            ret = ldl_phys(addr);
B
bellard 已提交
1526
            break;
B
bellard 已提交
1527
        case 8:
1528
            ret = ldq_phys(addr);
B
blueswir1 已提交
1529
            break;
B
bellard 已提交
1530
        }
B
blueswir1 已提交
1531
        break;
1532
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1533 1534
        switch(size) {
        case 1:
A
Anthony Liguori 已提交
1535 1536
            ret = ldub_phys((target_phys_addr_t)addr
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1537 1538
            break;
        case 2:
A
Anthony Liguori 已提交
1539 1540
            ret = lduw_phys((target_phys_addr_t)addr
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1541 1542 1543
            break;
        default:
        case 4:
A
Anthony Liguori 已提交
1544 1545
            ret = ldl_phys((target_phys_addr_t)addr
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
1546 1547
            break;
        case 8:
A
Anthony Liguori 已提交
1548 1549
            ret = ldq_phys((target_phys_addr_t)addr
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
B
blueswir1 已提交
1550
            break;
1551
        }
B
blueswir1 已提交
1552
        break;
B
blueswir1 已提交
1553 1554 1555
    case 0x30: // Turbosparc secondary cache diagnostic
    case 0x31: // Turbosparc RAM snoop
    case 0x32: // Turbosparc page table descriptor diagnostic
B
blueswir1 已提交
1556 1557 1558
    case 0x39: /* data cache diagnostic register */
        ret = 0;
        break;
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
    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;
            }
1578 1579
            DPRINTF_MMU("read breakpoint reg[%d] 0x%016" PRIx64 "\n", reg,
                        ret);
1580 1581
        }
        break;
B
blueswir1 已提交
1582
    case 8: /* User code access, XXX */
1583
    default:
1584
        do_unassigned_access(addr, 0, 0, asi, size);
B
blueswir1 已提交
1585 1586
        ret = 0;
        break;
1587
    }
1588 1589 1590
    if (sign) {
        switch(size) {
        case 1:
B
blueswir1 已提交
1591
            ret = (int8_t) ret;
B
blueswir1 已提交
1592
            break;
1593
        case 2:
B
blueswir1 已提交
1594 1595 1596 1597
            ret = (int16_t) ret;
            break;
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1598
            break;
1599 1600 1601 1602
        default:
            break;
        }
    }
1603
#ifdef DEBUG_ASI
B
blueswir1 已提交
1604
    dump_asi("read ", last_addr, asi, size, ret);
1605
#endif
B
blueswir1 已提交
1606
    return ret;
1607 1608
}

B
blueswir1 已提交
1609
void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
1610
{
1611
    helper_check_align(addr, size - 1);
1612
    switch(asi) {
1613
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
1614
        switch (addr) {
1615 1616
        case 0x01c00000: /* MXCC stream data register 0 */
            if (size == 8)
B
blueswir1 已提交
1617
                env->mxccdata[0] = val;
1618
            else
B
blueswir1 已提交
1619 1620
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1621 1622 1623
            break;
        case 0x01c00008: /* MXCC stream data register 1 */
            if (size == 8)
B
blueswir1 已提交
1624
                env->mxccdata[1] = val;
1625
            else
B
blueswir1 已提交
1626 1627
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1628 1629 1630
            break;
        case 0x01c00010: /* MXCC stream data register 2 */
            if (size == 8)
B
blueswir1 已提交
1631
                env->mxccdata[2] = val;
1632
            else
B
blueswir1 已提交
1633 1634
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1635 1636 1637
            break;
        case 0x01c00018: /* MXCC stream data register 3 */
            if (size == 8)
B
blueswir1 已提交
1638
                env->mxccdata[3] = val;
1639
            else
B
blueswir1 已提交
1640 1641
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1642 1643 1644
            break;
        case 0x01c00100: /* MXCC stream source */
            if (size == 8)
B
blueswir1 已提交
1645
                env->mxccregs[0] = val;
1646
            else
B
blueswir1 已提交
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
                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);
1657 1658 1659
            break;
        case 0x01c00200: /* MXCC stream destination */
            if (size == 8)
B
blueswir1 已提交
1660
                env->mxccregs[1] = val;
1661
            else
B
blueswir1 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
                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]);
1672 1673 1674
            break;
        case 0x01c00a00: /* MXCC control register */
            if (size == 8)
B
blueswir1 已提交
1675
                env->mxccregs[3] = val;
1676
            else
B
blueswir1 已提交
1677 1678
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1679 1680 1681
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
1682
                env->mxccregs[3] = (env->mxccregs[3] & 0xffffffff00000000ULL)
B
blueswir1 已提交
1683
                    | val;
1684
            else
B
blueswir1 已提交
1685 1686
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1687 1688
            break;
        case 0x01c00e00: /* MXCC error register  */
1689
            // writing a 1 bit clears the error
1690
            if (size == 8)
B
blueswir1 已提交
1691
                env->mxccregs[6] &= ~val;
1692
            else
B
blueswir1 已提交
1693 1694
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1695 1696 1697
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8)
B
blueswir1 已提交
1698
                env->mxccregs[7] = val;
1699
            else
B
blueswir1 已提交
1700 1701
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1702 1703
            break;
        default:
B
blueswir1 已提交
1704 1705
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1706 1707
            break;
        }
1708 1709
        DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %" PRIx64 "\n",
                     asi, size, addr, val);
1710 1711 1712
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1713
        break;
1714
    case 3: /* MMU flush */
B
blueswir1 已提交
1715 1716
        {
            int mmulev;
B
bellard 已提交
1717

B
blueswir1 已提交
1718
            mmulev = (addr >> 8) & 15;
1719
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
B
blueswir1 已提交
1720 1721
            switch (mmulev) {
            case 0: // flush page
B
blueswir1 已提交
1722
                tlb_flush_page(env, addr & 0xfffff000);
B
blueswir1 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
                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 已提交
1733
#ifdef DEBUG_MMU
B
blueswir1 已提交
1734
            dump_mmu(env);
B
bellard 已提交
1735
#endif
B
blueswir1 已提交
1736
        }
1737
        break;
1738
    case 4: /* write MMU regs */
B
blueswir1 已提交
1739
        {
B
blueswir1 已提交
1740
            int reg = (addr >> 8) & 0x1f;
B
blueswir1 已提交
1741
            uint32_t oldreg;
1742

B
blueswir1 已提交
1743
            oldreg = env->mmuregs[reg];
B
bellard 已提交
1744
            switch(reg) {
1745
            case 0: // Control Register
B
blueswir1 已提交
1746
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
B
blueswir1 已提交
1747
                                    (val & 0x00ffffff);
B
blueswir1 已提交
1748 1749
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
1750 1751
                if ((oldreg & (MMU_E | MMU_NF | env->def->mmu_bm)) !=
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->def->mmu_bm)))
B
bellard 已提交
1752 1753
                    tlb_flush(env, 1);
                break;
1754
            case 1: // Context Table Pointer Register
1755
                env->mmuregs[reg] = val & env->def->mmu_ctpr_mask;
1756 1757
                break;
            case 2: // Context Register
1758
                env->mmuregs[reg] = val & env->def->mmu_cxr_mask;
B
bellard 已提交
1759 1760 1761 1762 1763 1764
                if (oldreg != env->mmuregs[reg]) {
                    /* we flush when the MMU context changes because
                       QEMU has no MMU context support */
                    tlb_flush(env, 1);
                }
                break;
1765 1766 1767 1768
            case 3: // Synchronous Fault Status Register with Clear
            case 4: // Synchronous Fault Address Register
                break;
            case 0x10: // TLB Replacement Control Register
1769
                env->mmuregs[reg] = val & env->def->mmu_trcr_mask;
B
bellard 已提交
1770
                break;
1771
            case 0x13: // Synchronous Fault Status Register with Read and Clear
1772
                env->mmuregs[3] = val & env->def->mmu_sfsr_mask;
B
blueswir1 已提交
1773
                break;
1774
            case 0x14: // Synchronous Fault Address Register
B
blueswir1 已提交
1775
                env->mmuregs[4] = val;
B
blueswir1 已提交
1776
                break;
B
bellard 已提交
1777
            default:
B
blueswir1 已提交
1778
                env->mmuregs[reg] = val;
B
bellard 已提交
1779 1780 1781
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
B
blueswir1 已提交
1782 1783
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
                            reg, oldreg, env->mmuregs[reg]);
B
bellard 已提交
1784
            }
1785
#ifdef DEBUG_MMU
B
blueswir1 已提交
1786
            dump_mmu(env);
B
bellard 已提交
1787
#endif
B
blueswir1 已提交
1788
        }
1789
        break;
B
blueswir1 已提交
1790 1791 1792 1793
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1794 1795 1796
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1797
            stb_user(addr, val);
1798 1799
            break;
        case 2:
1800
            stw_user(addr, val);
1801 1802 1803
            break;
        default:
        case 4:
1804
            stl_user(addr, val);
1805 1806
            break;
        case 8:
1807
            stq_user(addr, val);
1808 1809 1810 1811 1812 1813
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1814
            stb_kernel(addr, val);
1815 1816
            break;
        case 2:
1817
            stw_kernel(addr, val);
1818 1819 1820
            break;
        default:
        case 4:
1821
            stl_kernel(addr, val);
1822 1823
            break;
        case 8:
1824
            stq_kernel(addr, val);
1825 1826 1827
            break;
        }
        break;
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
    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 已提交
1838
    case 0x17: /* Block copy, sta access */
B
blueswir1 已提交
1839
        {
B
blueswir1 已提交
1840 1841
            // val = src
            // addr = dst
B
blueswir1 已提交
1842
            // copy 32 bytes
1843
            unsigned int i;
B
blueswir1 已提交
1844
            uint32_t src = val & ~3, dst = addr & ~3, temp;
1845

1846 1847 1848 1849
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
B
blueswir1 已提交
1850
        }
1851
        break;
B
bellard 已提交
1852
    case 0x1f: /* Block fill, stda access */
B
blueswir1 已提交
1853
        {
B
blueswir1 已提交
1854 1855
            // addr = dst
            // fill 32 bytes with val
1856
            unsigned int i;
B
blueswir1 已提交
1857
            uint32_t dst = addr & 7;
1858 1859 1860

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
1861
        }
1862
        break;
1863
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
1864
        {
B
bellard 已提交
1865 1866
            switch(size) {
            case 1:
B
blueswir1 已提交
1867
                stb_phys(addr, val);
B
bellard 已提交
1868 1869
                break;
            case 2:
1870
                stw_phys(addr, val);
B
bellard 已提交
1871 1872 1873
                break;
            case 4:
            default:
1874
                stl_phys(addr, val);
B
bellard 已提交
1875
                break;
B
bellard 已提交
1876
            case 8:
1877
                stq_phys(addr, val);
B
bellard 已提交
1878
                break;
B
bellard 已提交
1879
            }
B
blueswir1 已提交
1880
        }
1881
        break;
B
blueswir1 已提交
1882
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
B
blueswir1 已提交
1883
        {
1884 1885
            switch(size) {
            case 1:
A
Anthony Liguori 已提交
1886 1887
                stb_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1888 1889
                break;
            case 2:
A
Anthony Liguori 已提交
1890 1891
                stw_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1892 1893 1894
                break;
            case 4:
            default:
A
Anthony Liguori 已提交
1895 1896
                stl_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1897 1898
                break;
            case 8:
A
Anthony Liguori 已提交
1899 1900
                stq_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1901 1902
                break;
            }
B
blueswir1 已提交
1903
        }
1904
        break;
B
blueswir1 已提交
1905 1906 1907
    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 已提交
1908 1909
    case 0x32: // store buffer control or Turbosparc page table
               // descriptor diagnostic
1910 1911
    case 0x36: /* I-cache flash clear */
    case 0x37: /* D-cache flash clear */
B
blueswir1 已提交
1912
    case 0x4c: /* breakpoint action */
1913
        break;
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
    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;
            }
1932
            DPRINTF_MMU("write breakpoint reg[%d] 0x%016x\n", reg,
1933 1934 1935
                        env->mmuregs[reg]);
        }
        break;
B
blueswir1 已提交
1936
    case 8: /* User code access, XXX */
1937
    case 9: /* Supervisor code access, XXX */
1938
    default:
1939
        do_unassigned_access(addr, 1, 0, asi, size);
1940
        break;
1941
    }
1942
#ifdef DEBUG_ASI
B
blueswir1 已提交
1943
    dump_asi("write", addr, asi, size, val);
1944
#endif
1945 1946
}

1947 1948 1949 1950
#endif /* CONFIG_USER_ONLY */
#else /* TARGET_SPARC64 */

#ifdef CONFIG_USER_ONLY
B
blueswir1 已提交
1951
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1952 1953
{
    uint64_t ret = 0;
B
blueswir1 已提交
1954 1955 1956
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
1957 1958 1959 1960

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

1961
    helper_check_align(addr, size - 1);
1962
    addr = address_mask(env, addr);
1963

1964 1965 1966
    switch (asi) {
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
B
blueswir1 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975
        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
1976 1977 1978
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
1979
                ret = ldub_raw(addr);
1980 1981
                break;
            case 2:
1982
                ret = lduw_raw(addr);
1983 1984
                break;
            case 4:
1985
                ret = ldl_raw(addr);
1986 1987 1988
                break;
            default:
            case 8:
1989
                ret = ldq_raw(addr);
1990 1991 1992 1993 1994 1995
                break;
            }
        }
        break;
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004
        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
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
        // 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 已提交
2020
            break;
2021 2022
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
2023
            break;
2024 2025
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
2026
            break;
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
2039
            break;
2040 2041
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
2042
            break;
2043 2044
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
2045
            break;
2046 2047 2048 2049
        default:
            break;
        }
    }
B
blueswir1 已提交
2050 2051 2052 2053
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
2054 2055
}

B
blueswir1 已提交
2056
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
2057
{
B
blueswir1 已提交
2058 2059 2060
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
2061 2062 2063
    if (asi < 0x80)
        raise_exception(TT_PRIV_ACT);

2064
    helper_check_align(addr, size - 1);
2065
    addr = address_mask(env, addr);
2066

2067 2068 2069 2070 2071 2072
    /* Convert to little endian */
    switch (asi) {
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
2073
            val = bswap16(val);
B
blueswir1 已提交
2074
            break;
2075
        case 4:
2076
            val = bswap32(val);
B
blueswir1 已提交
2077
            break;
2078
        case 8:
2079
            val = bswap64(val);
B
blueswir1 已提交
2080
            break;
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
        default:
            break;
        }
    default:
        break;
    }

    switch(asi) {
    case 0x80: // Primary
    case 0x88: // Primary LE
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
2094
                stb_raw(addr, val);
2095 2096
                break;
            case 2:
2097
                stw_raw(addr, val);
2098 2099
                break;
            case 4:
2100
                stl_raw(addr, val);
2101 2102 2103
                break;
            case 8:
            default:
2104
                stq_raw(addr, val);
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
                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:
2119
        do_unassigned_access(addr, 1, 0, 1, size);
2120 2121 2122 2123 2124
        return;
    }
}

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

B
blueswir1 已提交
2126
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
B
bellard 已提交
2127
{
B
bellard 已提交
2128
    uint64_t ret = 0;
B
blueswir1 已提交
2129 2130 2131
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
B
bellard 已提交
2132

I
Igor V. Kovalenko 已提交
2133 2134
    asi &= 0xff;

B
blueswir1 已提交
2135
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2136 2137
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2138
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2139
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
2140

2141
    helper_check_align(addr, size - 1);
B
bellard 已提交
2142
    switch (asi) {
B
blueswir1 已提交
2143 2144 2145 2146 2147 2148 2149 2150 2151
    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
2152 2153 2154 2155
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
2156 2157
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
2158
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
2159 2160
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
2161 2162
                switch(size) {
                case 1:
B
blueswir1 已提交
2163
                    ret = ldub_hypv(addr);
B
blueswir1 已提交
2164 2165
                    break;
                case 2:
2166
                    ret = lduw_hypv(addr);
B
blueswir1 已提交
2167 2168
                    break;
                case 4:
2169
                    ret = ldl_hypv(addr);
B
blueswir1 已提交
2170 2171 2172
                    break;
                default:
                case 8:
2173
                    ret = ldq_hypv(addr);
B
blueswir1 已提交
2174 2175 2176 2177 2178
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
2179
                    ret = ldub_kernel(addr);
B
blueswir1 已提交
2180 2181
                    break;
                case 2:
2182
                    ret = lduw_kernel(addr);
B
blueswir1 已提交
2183 2184
                    break;
                case 4:
2185
                    ret = ldl_kernel(addr);
B
blueswir1 已提交
2186 2187 2188
                    break;
                default:
                case 8:
2189
                    ret = ldq_kernel(addr);
B
blueswir1 已提交
2190 2191
                    break;
                }
2192 2193 2194 2195
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
2196
                ret = ldub_user(addr);
2197 2198
                break;
            case 2:
2199
                ret = lduw_user(addr);
2200 2201
                break;
            case 4:
2202
                ret = ldl_user(addr);
2203 2204 2205
                break;
            default:
            case 8:
2206
                ret = ldq_user(addr);
2207 2208 2209 2210
                break;
            }
        }
        break;
B
bellard 已提交
2211 2212
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
2213 2214
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
2215
        {
B
bellard 已提交
2216 2217
            switch(size) {
            case 1:
B
blueswir1 已提交
2218
                ret = ldub_phys(addr);
B
bellard 已提交
2219 2220
                break;
            case 2:
2221
                ret = lduw_phys(addr);
B
bellard 已提交
2222 2223
                break;
            case 4:
2224
                ret = ldl_phys(addr);
B
bellard 已提交
2225 2226 2227
                break;
            default:
            case 8:
2228
                ret = ldq_phys(addr);
B
bellard 已提交
2229 2230
                break;
            }
B
blueswir1 已提交
2231 2232
            break;
        }
B
blueswir1 已提交
2233 2234 2235 2236 2237
    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 已提交
2238 2239 2240 2241 2242 2243 2244 2245 2246
    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 已提交
2247 2248 2249 2250 2251
    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
2252
    case 0x81: // Secondary
B
bellard 已提交
2253
    case 0x89: // Secondary LE
B
blueswir1 已提交
2254 2255
        // XXX
        break;
B
bellard 已提交
2256
    case 0x45: // LSU
B
blueswir1 已提交
2257 2258
        ret = env->lsu;
        break;
B
bellard 已提交
2259
    case 0x50: // I-MMU regs
B
blueswir1 已提交
2260
        {
B
blueswir1 已提交
2261
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
2262

2263 2264
            if (reg == 0) {
                // I-TSB Tag Target register
2265
                ret = ultrasparc_tag_target(env->immu.tag_access);
2266 2267 2268 2269
            } else {
                ret = env->immuregs[reg];
            }

B
blueswir1 已提交
2270 2271
            break;
        }
B
bellard 已提交
2272
    case 0x51: // I-MMU 8k TSB pointer
2273 2274 2275
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
2276
            ret = ultrasparc_tsb_pointer(env->immu.tsb, env->immu.tag_access,
2277 2278 2279
                                         8*1024);
            break;
        }
B
bellard 已提交
2280
    case 0x52: // I-MMU 64k TSB pointer
2281 2282 2283
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
2284
            ret = ultrasparc_tsb_pointer(env->immu.tsb, env->immu.tag_access,
2285 2286 2287
                                         64*1024);
            break;
        }
2288 2289 2290 2291
    case 0x55: // I-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

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

2299
            ret = env->itlb[reg].tag;
B
blueswir1 已提交
2300 2301
            break;
        }
B
bellard 已提交
2302
    case 0x58: // D-MMU regs
B
blueswir1 已提交
2303
        {
B
blueswir1 已提交
2304
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
2305

2306 2307
            if (reg == 0) {
                // D-TSB Tag Target register
2308
                ret = ultrasparc_tag_target(env->dmmu.tag_access);
2309 2310 2311 2312 2313 2314 2315 2316 2317
            } 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
2318
            ret = ultrasparc_tsb_pointer(env->dmmu.tsb, env->dmmu.tag_access,
2319 2320 2321 2322 2323 2324 2325
                                         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
2326
            ret = ultrasparc_tsb_pointer(env->dmmu.tsb, env->dmmu.tag_access,
2327
                                         64*1024);
B
blueswir1 已提交
2328 2329
            break;
        }
2330 2331 2332 2333
    case 0x5d: // D-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

2334
            ret = env->dtlb[reg].tte;
2335 2336
            break;
        }
B
bellard 已提交
2337
    case 0x5e: // D-MMU tag read
B
blueswir1 已提交
2338
        {
B
blueswir1 已提交
2339
            int reg = (addr >> 3) & 0x3f;
B
blueswir1 已提交
2340

2341
            ret = env->dtlb[reg].tag;
B
blueswir1 已提交
2342 2343
            break;
        }
2344 2345
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2346 2347 2348
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2349 2350 2351 2352 2353 2354 2355 2356
    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 已提交
2357
    case 0x5b: // D-MMU data pointer
B
bellard 已提交
2358 2359 2360
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
2361 2362
        // XXX
        break;
B
bellard 已提交
2363 2364 2365 2366
    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 已提交
2367
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
2368
    default:
2369
        do_unassigned_access(addr, 0, 0, 1, size);
B
blueswir1 已提交
2370 2371
        ret = 0;
        break;
B
bellard 已提交
2372
    }
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387

    /* 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 已提交
2388
            break;
2389 2390
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
2391
            break;
2392 2393
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
2394
            break;
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
2407
            break;
2408 2409
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
2410
            break;
2411 2412
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
2413
            break;
2414 2415 2416 2417
        default:
            break;
        }
    }
B
blueswir1 已提交
2418 2419 2420 2421
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
B
bellard 已提交
2422 2423
}

B
blueswir1 已提交
2424
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
B
bellard 已提交
2425
{
B
blueswir1 已提交
2426 2427 2428
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
I
Igor V. Kovalenko 已提交
2429 2430 2431

    asi &= 0xff;

B
blueswir1 已提交
2432
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2433 2434
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2435
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2436
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
2437

2438
    helper_check_align(addr, size - 1);
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
    /* 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:
2450
            val = bswap16(val);
B
blueswir1 已提交
2451
            break;
2452
        case 4:
2453
            val = bswap32(val);
B
blueswir1 已提交
2454
            break;
2455
        case 8:
2456
            val = bswap64(val);
B
blueswir1 已提交
2457
            break;
2458 2459 2460 2461 2462 2463 2464
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
2465
    switch(asi) {
2466 2467 2468 2469
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
2470 2471
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
2472
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
2473 2474
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
2475 2476
                switch(size) {
                case 1:
B
blueswir1 已提交
2477
                    stb_hypv(addr, val);
B
blueswir1 已提交
2478 2479
                    break;
                case 2:
2480
                    stw_hypv(addr, val);
B
blueswir1 已提交
2481 2482
                    break;
                case 4:
2483
                    stl_hypv(addr, val);
B
blueswir1 已提交
2484 2485 2486
                    break;
                case 8:
                default:
2487
                    stq_hypv(addr, val);
B
blueswir1 已提交
2488 2489 2490 2491 2492
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
2493
                    stb_kernel(addr, val);
B
blueswir1 已提交
2494 2495
                    break;
                case 2:
2496
                    stw_kernel(addr, val);
B
blueswir1 已提交
2497 2498
                    break;
                case 4:
2499
                    stl_kernel(addr, val);
B
blueswir1 已提交
2500 2501 2502
                    break;
                case 8:
                default:
2503
                    stq_kernel(addr, val);
B
blueswir1 已提交
2504 2505
                    break;
                }
2506 2507 2508 2509
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
2510
                stb_user(addr, val);
2511 2512
                break;
            case 2:
2513
                stw_user(addr, val);
2514 2515
                break;
            case 4:
2516
                stl_user(addr, val);
2517 2518 2519
                break;
            case 8:
            default:
2520
                stq_user(addr, val);
2521 2522 2523 2524
                break;
            }
        }
        break;
B
bellard 已提交
2525 2526
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
2527 2528
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
2529
        {
B
bellard 已提交
2530 2531
            switch(size) {
            case 1:
B
blueswir1 已提交
2532
                stb_phys(addr, val);
B
bellard 已提交
2533 2534
                break;
            case 2:
2535
                stw_phys(addr, val);
B
bellard 已提交
2536 2537
                break;
            case 4:
2538
                stl_phys(addr, val);
B
bellard 已提交
2539 2540 2541
                break;
            case 8:
            default:
2542
                stq_phys(addr, val);
B
bellard 已提交
2543 2544
                break;
            }
B
blueswir1 已提交
2545 2546
        }
        return;
B
blueswir1 已提交
2547 2548 2549 2550 2551
    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 已提交
2552 2553 2554 2555 2556
    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 已提交
2557
    case 0x81: // Secondary
B
bellard 已提交
2558
    case 0x89: // Secondary LE
B
blueswir1 已提交
2559 2560
        // XXX
        return;
B
bellard 已提交
2561
    case 0x45: // LSU
B
blueswir1 已提交
2562 2563 2564 2565
        {
            uint64_t oldreg;

            oldreg = env->lsu;
B
blueswir1 已提交
2566
            env->lsu = val & (DMMU_E | IMMU_E);
B
blueswir1 已提交
2567 2568 2569
            // Mappings generated during D/I MMU disabled mode are
            // invalid in normal mode
            if (oldreg != env->lsu) {
B
blueswir1 已提交
2570 2571
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
                            oldreg, env->lsu);
B
bellard 已提交
2572
#ifdef DEBUG_MMU
B
blueswir1 已提交
2573
                dump_mmu(env);
B
bellard 已提交
2574
#endif
B
blueswir1 已提交
2575 2576 2577 2578
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
2579
    case 0x50: // I-MMU regs
B
blueswir1 已提交
2580
        {
B
blueswir1 已提交
2581
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
2582
            uint64_t oldreg;
2583

B
blueswir1 已提交
2584
            oldreg = env->immuregs[reg];
B
bellard 已提交
2585 2586 2587 2588 2589 2590 2591
            switch(reg) {
            case 0: // RO
                return;
            case 1: // Not in I-MMU
            case 2:
                return;
            case 3: // SFSR
B
blueswir1 已提交
2592 2593
                if ((val & 1) == 0)
                    val = 0; // Clear SFSR
2594
                env->immu.sfsr = val;
B
bellard 已提交
2595
                break;
2596 2597
            case 4: // RO
                return;
B
bellard 已提交
2598
            case 5: // TSB access
2599 2600 2601 2602
                DPRINTF_MMU("immu TSB write: 0x%016" PRIx64 " -> 0x%016"
                            PRIx64 "\n", env->immu.tsb, val);
                env->immu.tsb = val;
                break;
B
bellard 已提交
2603
            case 6: // Tag access
2604 2605 2606 2607 2608
                env->immu.tag_access = val;
                break;
            case 7:
            case 8:
                return;
B
bellard 已提交
2609 2610 2611
            default:
                break;
            }
2612

B
bellard 已提交
2613
            if (oldreg != env->immuregs[reg]) {
2614
                DPRINTF_MMU("immu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
B
blueswir1 已提交
2615
                            PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
B
bellard 已提交
2616
            }
2617
#ifdef DEBUG_MMU
B
blueswir1 已提交
2618
            dump_mmu(env);
B
bellard 已提交
2619
#endif
B
blueswir1 已提交
2620 2621
            return;
        }
B
bellard 已提交
2622
    case 0x54: // I-MMU data in
2623 2624
        replace_tlb_1bit_lru(env->itlb, env->immu.tag_access, val, "immu", env);
        return;
B
bellard 已提交
2625
    case 0x55: // I-MMU data access
B
blueswir1 已提交
2626
        {
2627 2628
            // TODO: auto demap

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

2631
            replace_tlb_entry(&env->itlb[i], env->immu.tag_access, val, env);
2632 2633

#ifdef DEBUG_MMU
2634
            DPRINTF_MMU("immu data access replaced entry [%i]\n", i);
2635 2636
            dump_mmu(env);
#endif
B
blueswir1 已提交
2637 2638
            return;
        }
B
bellard 已提交
2639
    case 0x57: // I-MMU demap
2640
        demap_tlb(env->itlb, val, "immu", env);
B
blueswir1 已提交
2641
        return;
B
bellard 已提交
2642
    case 0x58: // D-MMU regs
B
blueswir1 已提交
2643
        {
B
blueswir1 已提交
2644
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
2645
            uint64_t oldreg;
2646

B
blueswir1 已提交
2647
            oldreg = env->dmmuregs[reg];
B
bellard 已提交
2648 2649 2650 2651 2652
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
B
blueswir1 已提交
2653 2654
                if ((val & 1) == 0) {
                    val = 0; // Clear SFSR, Fault address
2655
                    env->dmmu.sfar = 0;
B
blueswir1 已提交
2656
                }
2657
                env->dmmu.sfsr = val;
B
bellard 已提交
2658 2659
                break;
            case 1: // Primary context
2660 2661
                env->dmmu.mmu_primary_context = val;
                break;
B
bellard 已提交
2662
            case 2: // Secondary context
2663 2664
                env->dmmu.mmu_secondary_context = val;
                break;
B
bellard 已提交
2665
            case 5: // TSB access
2666 2667 2668 2669
                DPRINTF_MMU("dmmu TSB write: 0x%016" PRIx64 " -> 0x%016"
                            PRIx64 "\n", env->dmmu.tsb, val);
                env->dmmu.tsb = val;
                break;
B
bellard 已提交
2670
            case 6: // Tag access
2671 2672
                env->dmmu.tag_access = val;
                break;
B
bellard 已提交
2673 2674 2675
            case 7: // Virtual Watchpoint
            case 8: // Physical Watchpoint
            default:
2676
                env->dmmuregs[reg] = val;
B
bellard 已提交
2677 2678
                break;
            }
2679

B
bellard 已提交
2680
            if (oldreg != env->dmmuregs[reg]) {
2681
                DPRINTF_MMU("dmmu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
B
blueswir1 已提交
2682
                            PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
B
bellard 已提交
2683
            }
2684
#ifdef DEBUG_MMU
B
blueswir1 已提交
2685
            dump_mmu(env);
B
bellard 已提交
2686
#endif
B
blueswir1 已提交
2687 2688
            return;
        }
B
bellard 已提交
2689
    case 0x5c: // D-MMU data in
2690 2691
        replace_tlb_1bit_lru(env->dtlb, env->dmmu.tag_access, val, "dmmu", env);
        return;
B
bellard 已提交
2692
    case 0x5d: // D-MMU data access
B
blueswir1 已提交
2693
        {
B
blueswir1 已提交
2694
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
2695

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

2698
#ifdef DEBUG_MMU
2699
            DPRINTF_MMU("dmmu data access replaced entry [%i]\n", i);
2700 2701
            dump_mmu(env);
#endif
B
blueswir1 已提交
2702 2703
            return;
        }
B
bellard 已提交
2704
    case 0x5f: // D-MMU demap
2705
        demap_tlb(env->dtlb, val, "dmmu", env);
2706
        return;
B
bellard 已提交
2707
    case 0x49: // Interrupt data receive
B
blueswir1 已提交
2708 2709
        // XXX
        return;
2710 2711
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2712 2713 2714
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2715 2716 2717 2718 2719 2720 2721 2722
    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 已提交
2723 2724 2725 2726 2727 2728 2729
    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 已提交
2730 2731 2732 2733 2734 2735
    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 已提交
2736
    default:
2737
        do_unassigned_access(addr, 1, 0, 1, size);
B
blueswir1 已提交
2738
        return;
B
bellard 已提交
2739 2740
    }
}
2741
#endif /* CONFIG_USER_ONLY */
2742

B
blueswir1 已提交
2743 2744 2745
void helper_ldda_asi(target_ulong addr, int asi, int rd)
{
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2746 2747
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2748
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
        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 已提交
2790
void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
2791 2792
{
    unsigned int i;
B
blueswir1 已提交
2793
    target_ulong val;
2794

2795
    helper_check_align(addr, 3);
2796 2797 2798 2799 2800
    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 已提交
2801 2802 2803 2804
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2805
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2806
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2807 2808
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4,
                                                         0);
B
blueswir1 已提交
2809
            addr += 4;
2810 2811 2812 2813 2814 2815 2816
        }

        return;
    default:
        break;
    }

B
blueswir1 已提交
2817
    val = helper_ld_asi(addr, asi, size, 0);
2818 2819 2820
    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2821
        *((uint32_t *)&env->fpr[rd]) = val;
2822 2823
        break;
    case 8:
B
blueswir1 已提交
2824
        *((int64_t *)&DT0) = val;
2825
        break;
B
blueswir1 已提交
2826 2827 2828
    case 16:
        // XXX
        break;
2829 2830 2831
    }
}

B
blueswir1 已提交
2832
void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
2833 2834
{
    unsigned int i;
B
blueswir1 已提交
2835
    target_ulong val = 0;
2836

2837
    helper_check_align(addr, 3);
2838
    switch (asi) {
B
blueswir1 已提交
2839 2840
    case 0xe0: // UA2007 Block commit store primary (cache flush)
    case 0xe1: // UA2007 Block commit store secondary (cache flush)
2841 2842 2843 2844
    case 0xf0: // Block store primary
    case 0xf1: // Block store secondary
    case 0xf8: // Block store primary LE
    case 0xf9: // Block store secondary LE
B
blueswir1 已提交
2845 2846 2847 2848
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2849
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2850
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2851 2852 2853
            val = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(addr, val, asi & 0x8f, 4);
            addr += 4;
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863
        }

        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2864
        val = *((uint32_t *)&env->fpr[rd]);
2865 2866
        break;
    case 8:
B
blueswir1 已提交
2867
        val = *((int64_t *)&DT0);
2868
        break;
B
blueswir1 已提交
2869 2870 2871
    case 16:
        // XXX
        break;
2872
    }
B
blueswir1 已提交
2873 2874 2875 2876 2877 2878 2879 2880
    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;

2881
    val2 &= 0xffffffffUL;
B
blueswir1 已提交
2882 2883
    ret = helper_ld_asi(addr, asi, 4, 0);
    ret &= 0xffffffffUL;
2884 2885
    if (val2 == ret)
        helper_st_asi(addr, val1 & 0xffffffffUL, asi, 4);
B
blueswir1 已提交
2886
    return ret;
2887 2888
}

B
blueswir1 已提交
2889 2890 2891 2892 2893 2894
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);
2895 2896
    if (val2 == ret)
        helper_st_asi(addr, val1, asi, 8);
B
blueswir1 已提交
2897 2898
    return ret;
}
2899
#endif /* TARGET_SPARC64 */
B
bellard 已提交
2900 2901

#ifndef TARGET_SPARC64
B
blueswir1 已提交
2902
void helper_rett(void)
2903
{
2904 2905
    unsigned int cwp;

2906 2907 2908
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

2909
    env->psret = 1;
2910
    cwp = cpu_cwp_inc(env, env->cwp + 1) ;
2911 2912 2913 2914 2915 2916
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
2917
#endif
2918

B
blueswir1 已提交
2919 2920 2921 2922 2923
target_ulong helper_udiv(target_ulong a, target_ulong b)
{
    uint64_t x0;
    uint32_t x1;

2924
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945
    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;

2946
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962
    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 已提交
2963 2964
void helper_stdf(target_ulong addr, int mem_idx)
{
2965
    helper_check_align(addr, 7);
B
blueswir1 已提交
2966 2967 2968
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2969
        stfq_user(addr, DT0);
B
blueswir1 已提交
2970 2971
        break;
    case 1:
2972
        stfq_kernel(addr, DT0);
B
blueswir1 已提交
2973 2974 2975
        break;
#ifdef TARGET_SPARC64
    case 2:
2976
        stfq_hypv(addr, DT0);
B
blueswir1 已提交
2977 2978 2979 2980 2981 2982
        break;
#endif
    default:
        break;
    }
#else
2983
    stfq_raw(address_mask(env, addr), DT0);
B
blueswir1 已提交
2984 2985 2986 2987 2988
#endif
}

void helper_lddf(target_ulong addr, int mem_idx)
{
2989
    helper_check_align(addr, 7);
B
blueswir1 已提交
2990 2991 2992
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2993
        DT0 = ldfq_user(addr);
B
blueswir1 已提交
2994 2995
        break;
    case 1:
2996
        DT0 = ldfq_kernel(addr);
B
blueswir1 已提交
2997 2998 2999
        break;
#ifdef TARGET_SPARC64
    case 2:
3000
        DT0 = ldfq_hypv(addr);
B
blueswir1 已提交
3001 3002 3003 3004 3005 3006
        break;
#endif
    default:
        break;
    }
#else
3007
    DT0 = ldfq_raw(address_mask(env, addr));
B
blueswir1 已提交
3008 3009 3010
#endif
}

B
blueswir1 已提交
3011
void helper_ldqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
3012 3013 3014 3015
{
    // XXX add 128 bit load
    CPU_QuadU u;

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

B
blueswir1 已提交
3046
void helper_stqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
3047 3048 3049 3050
{
    // XXX add 128 bit store
    CPU_QuadU u;

3051
    helper_check_align(addr, 7);
B
blueswir1 已提交
3052 3053 3054 3055
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
        u.q = QT0;
3056 3057
        stq_user(addr, u.ll.upper);
        stq_user(addr + 8, u.ll.lower);
B
blueswir1 已提交
3058 3059 3060
        break;
    case 1:
        u.q = QT0;
3061 3062
        stq_kernel(addr, u.ll.upper);
        stq_kernel(addr + 8, u.ll.lower);
B
blueswir1 已提交
3063 3064 3065 3066
        break;
#ifdef TARGET_SPARC64
    case 2:
        u.q = QT0;
3067 3068
        stq_hypv(addr, u.ll.upper);
        stq_hypv(addr + 8, u.ll.lower);
B
blueswir1 已提交
3069 3070 3071 3072 3073 3074
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
3075
    u.q = QT0;
3076 3077
    stq_raw(address_mask(env, addr), u.ll.upper);
    stq_raw(address_mask(env, addr + 8), u.ll.lower);
B
blueswir1 已提交
3078
#endif
B
blueswir1 已提交
3079
}
B
blueswir1 已提交
3080

3081
static inline void set_fsr(void)
3082
{
B
bellard 已提交
3083
    int rnd_mode;
B
blueswir1 已提交
3084

3085 3086
    switch (env->fsr & FSR_RD_MASK) {
    case FSR_RD_NEAREST:
B
bellard 已提交
3087
        rnd_mode = float_round_nearest_even;
B
blueswir1 已提交
3088
        break;
B
bellard 已提交
3089
    default:
3090
    case FSR_RD_ZERO:
B
bellard 已提交
3091
        rnd_mode = float_round_to_zero;
B
blueswir1 已提交
3092
        break;
3093
    case FSR_RD_POS:
B
bellard 已提交
3094
        rnd_mode = float_round_up;
B
blueswir1 已提交
3095
        break;
3096
    case FSR_RD_NEG:
B
bellard 已提交
3097
        rnd_mode = float_round_down;
B
blueswir1 已提交
3098
        break;
3099
    }
B
bellard 已提交
3100
    set_float_rounding_mode(rnd_mode, &env->fp_status);
3101
}
B
bellard 已提交
3102

3103
void helper_ldfsr(uint32_t new_fsr)
B
blueswir1 已提交
3104
{
3105 3106
    env->fsr = (new_fsr & FSR_LDFSR_MASK) | (env->fsr & FSR_LDFSR_OLDMASK);
    set_fsr();
B
blueswir1 已提交
3107 3108
}

3109 3110 3111 3112 3113 3114 3115 3116
#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 已提交
3117
void helper_debug(void)
B
bellard 已提交
3118 3119 3120 3121
{
    env->exception_index = EXCP_DEBUG;
    cpu_loop_exit();
}
3122

B
bellard 已提交
3123
#ifndef TARGET_SPARC64
3124 3125 3126 3127 3128 3129
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

3130
    cwp = cpu_cwp_dec(env, env->cwp - 1);
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_OVF);
    }
    set_cwp(cwp);
}

void helper_restore(void)
{
    uint32_t cwp;

3141
    cwp = cpu_cwp_inc(env, env->cwp + 1);
3142 3143 3144 3145 3146 3147
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
}

B
blueswir1 已提交
3148
void helper_wrpsr(target_ulong new_psr)
3149
{
3150
    if ((new_psr & PSR_CWP) >= env->nwindows)
3151 3152
        raise_exception(TT_ILL_INSN);
    else
B
blueswir1 已提交
3153
        PUT_PSR(env, new_psr);
3154 3155
}

B
blueswir1 已提交
3156
target_ulong helper_rdpsr(void)
3157
{
B
blueswir1 已提交
3158
    return GET_PSR(env);
3159
}
B
bellard 已提交
3160 3161

#else
3162 3163 3164 3165 3166 3167
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

3168
    cwp = cpu_cwp_dec(env, env->cwp - 1);
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
    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;

3189
    cwp = cpu_cwp_inc(env, env->cwp + 1);
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202
    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)
{
3203
    if (env->cansave != env->nwindows - 2) {
3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221
        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++;
3222
    if (env->cleanwin < env->nwindows - 1)
3223 3224 3225 3226 3227 3228 3229
        env->cleanwin++;
    if (env->otherwin == 0)
        env->cansave--;
    else
        env->otherwin--;
}

B
blueswir1 已提交
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250
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 已提交
3251

3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
// 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 已提交
3283
target_ulong helper_popc(target_ulong val)
B
bellard 已提交
3284
{
B
blueswir1 已提交
3285
    return ctpop64(val);
B
bellard 已提交
3286
}
B
bellard 已提交
3287

3288
static inline uint64_t *get_gregset(uint32_t pstate)
B
bellard 已提交
3289 3290 3291
{
    switch (pstate) {
    default:
3292 3293 3294 3295 3296 3297
        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 已提交
3298
    case 0:
B
blueswir1 已提交
3299
        return env->bgregs;
B
bellard 已提交
3300
    case PS_AG:
B
blueswir1 已提交
3301
        return env->agregs;
B
bellard 已提交
3302
    case PS_MG:
B
blueswir1 已提交
3303
        return env->mgregs;
B
bellard 已提交
3304
    case PS_IG:
B
blueswir1 已提交
3305
        return env->igregs;
B
bellard 已提交
3306 3307 3308
    }
}

3309
static inline void change_pstate(uint32_t new_pstate)
B
bellard 已提交
3310
{
3311
    uint32_t pstate_regs, new_pstate_regs;
B
bellard 已提交
3312 3313
    uint64_t *src, *dst;

3314 3315 3316 3317 3318
    if (env->def->features & CPU_FEATURE_GL) {
        // PS_AG is not implemented in this case
        new_pstate &= ~PS_AG;
    }

B
bellard 已提交
3319 3320
    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
3321

B
bellard 已提交
3322
    if (new_pstate_regs != pstate_regs) {
3323 3324
        DPRINTF_PSTATE("change_pstate: switching regs old=%x new=%x\n",
                       pstate_regs, new_pstate_regs);
B
blueswir1 已提交
3325 3326 3327 3328 3329
        // 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 已提交
3330
    }
3331 3332 3333 3334
    else {
        DPRINTF_PSTATE("change_pstate: regs new=%x (unchanged)\n",
                       new_pstate_regs);
    }
B
bellard 已提交
3335 3336 3337
    env->pstate = new_pstate;
}

B
blueswir1 已提交
3338
void helper_wrpstate(target_ulong new_state)
3339
{
3340
    change_pstate(new_state & 0xf3f);
3341 3342 3343 3344 3345 3346

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

3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
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 已提交
3363
void helper_done(void)
B
bellard 已提交
3364
{
3365 3366
    trap_state* tsptr = cpu_tsptr(env);

3367
    env->pc = tsptr->tnpc;
3368 3369 3370 3371 3372
    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 已提交
3373
    env->tl--;
3374 3375 3376 3377 3378 3379 3380 3381

    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 已提交
3382 3383
}

B
blueswir1 已提交
3384
void helper_retry(void)
B
bellard 已提交
3385
{
3386 3387 3388 3389 3390 3391 3392 3393
    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 已提交
3394
    env->tl--;
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415

    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 已提交
3416
}
3417 3418 3419

void helper_set_softint(uint64_t value)
{
3420
    do_modify_softint("helper_set_softint", env->softint | (uint32_t)value);
3421 3422 3423 3424
}

void helper_clear_softint(uint64_t value)
{
3425
    do_modify_softint("helper_clear_softint", env->softint & (uint32_t)~value);
3426 3427 3428 3429
}

void helper_write_softint(uint64_t value)
{
3430
    do_modify_softint("helper_write_softint", (uint32_t)value);
3431
}
B
bellard 已提交
3432
#endif
3433

B
blueswir1 已提交
3434
void helper_flush(target_ulong addr)
3435
{
B
blueswir1 已提交
3436 3437
    addr &= ~7;
    tb_invalidate_page_range(addr, addr + 8);
3438 3439
}

B
blueswir1 已提交
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
#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

3477 3478 3479 3480 3481
trap_state* cpu_tsptr(CPUState* env)
{
    return &env->ts[env->tl & MAXTL_MASK];
}

B
blueswir1 已提交
3482 3483 3484
void do_interrupt(CPUState *env)
{
    int intno = env->exception_index;
3485
    trap_state* tsptr;
B
blueswir1 已提交
3486 3487

#ifdef DEBUG_PCALL
3488
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
        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";
        }

3506
        qemu_log("%6d: %s (v=%04x) pc=%016" PRIx64 " npc=%016" PRIx64
B
blueswir1 已提交
3507 3508 3509 3510
                " SP=%016" PRIx64 "\n",
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
3511
        log_cpu_state(env, 0);
B
blueswir1 已提交
3512 3513 3514 3515 3516
#if 0
        {
            int i;
            uint8_t *ptr;

3517
            qemu_log("       code=");
B
blueswir1 已提交
3518 3519
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
3520
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
3521
            }
3522
            qemu_log("\n");
B
blueswir1 已提交
3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541
        }
#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++;
    }
3542 3543 3544
    tsptr = cpu_tsptr(env);

    tsptr->tstate = ((uint64_t)GET_CCR(env) << 32) |
B
blueswir1 已提交
3545 3546
        ((env->asi & 0xff) << 24) | ((env->pstate & 0xf3f) << 8) |
        GET_CWP64(env);
3547 3548 3549
    tsptr->tpc = env->pc;
    tsptr->tnpc = env->npc;
    tsptr->tt = intno;
3550 3551 3552 3553 3554 3555 3556

    switch (intno) {
    case TT_IVEC:
        change_pstate(PS_PEF | PS_PRIV | PS_IG);
        break;
    case TT_TFAULT:
    case TT_DFAULT:
3557 3558 3559
    case TT_TMISS ... TT_TMISS + 3:
    case TT_DMISS ... TT_DMISS + 3:
    case TT_DPROT ... TT_DPROT + 3:
3560 3561 3562 3563 3564
        change_pstate(PS_PEF | PS_PRIV | PS_MG);
        break;
    default:
        change_pstate(PS_PEF | PS_PRIV | PS_AG);
        break;
B
blueswir1 已提交
3565
    }
3566

B
blueswir1 已提交
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
    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;
3577
    env->exception_index = -1;
3578
}
B
blueswir1 已提交
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613
#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
3614

B
blueswir1 已提交
3615
void do_interrupt(CPUState *env)
3616
{
B
blueswir1 已提交
3617 3618 3619
    int cwp, intno = env->exception_index;

#ifdef DEBUG_PCALL
3620
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
        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";
        }

3634
        qemu_log("%6d: %s (v=%02x) pc=%08x npc=%08x SP=%08x\n",
B
blueswir1 已提交
3635 3636 3637
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
3638
        log_cpu_state(env, 0);
B
blueswir1 已提交
3639 3640 3641 3642 3643
#if 0
        {
            int i;
            uint8_t *ptr;

3644
            qemu_log("       code=");
B
blueswir1 已提交
3645 3646
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
3647
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
3648
            }
3649
            qemu_log("\n");
B
blueswir1 已提交
3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
        }
#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;
3672
    env->exception_index = -1;
3673
}
B
blueswir1 已提交
3674
#endif
3675

3676
#if !defined(CONFIG_USER_ONLY)
3677

3678 3679 3680
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

3681
#define MMUSUFFIX _mmu
3682
#define ALIGNED_ONLY
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695

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

3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713
/* 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);
        }
    }
}

3714 3715 3716
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr)
{
B
blueswir1 已提交
3717
#ifdef DEBUG_UNALIGNED
3718 3719
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
           "\n", addr, env->pc);
B
blueswir1 已提交
3720
#endif
3721
    cpu_restore_state2(retaddr);
B
blueswir1 已提交
3722
    raise_exception(TT_UNALIGNED);
3723
}
3724 3725 3726 3727 3728

/* 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 */
3729
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
3730 3731 3732 3733 3734 3735 3736 3737 3738
{
    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;

3739
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
3740
    if (ret) {
3741
        cpu_restore_state2(retaddr);
3742 3743 3744 3745 3746
        cpu_loop_exit();
    }
    env = saved_env;
}

P
Paul Brook 已提交
3747
#endif /* !CONFIG_USER_ONLY */
3748 3749

#ifndef TARGET_SPARC64
P
Paul Brook 已提交
3750
#if !defined(CONFIG_USER_ONLY)
A
Anthony Liguori 已提交
3751
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3752
                          int is_asi, int size)
3753 3754
{
    CPUState *saved_env;
3755
    int fault_type;
3756 3757 3758 3759 3760

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;
3761 3762
#ifdef DEBUG_UNASSIGNED
    if (is_asi)
3763
        printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
B
blueswir1 已提交
3764
               " asi 0x%02x from " TARGET_FMT_lx "\n",
3765 3766
               is_exec ? "exec" : is_write ? "write" : "read", size,
               size == 1 ? "" : "s", addr, is_asi, env->pc);
3767
    else
3768 3769 3770 3771
        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);
3772
#endif
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795
    /* 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;
    }

3796
    if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
3797 3798 3799 3800
        if (is_exec)
            raise_exception(TT_CODE_ACCESS);
        else
            raise_exception(TT_DATA_ACCESS);
3801
    }
3802 3803 3804 3805 3806 3807

    /* 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);
    }
3808 3809

    env = saved_env;
3810
}
P
Paul Brook 已提交
3811 3812 3813 3814 3815
#endif
#else
#if defined(CONFIG_USER_ONLY)
static void do_unassigned_access(target_ulong addr, int is_write, int is_exec,
                          int is_asi, int size)
3816
#else
A
Anthony Liguori 已提交
3817
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3818
                          int is_asi, int size)
P
Paul Brook 已提交
3819
#endif
3820 3821 3822 3823 3824 3825 3826
{
    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;
3827 3828

#ifdef DEBUG_UNASSIGNED
B
blueswir1 已提交
3829 3830
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
           "\n", addr, env->pc);
3831
#endif
3832

3833 3834 3835 3836
    if (is_exec)
        raise_exception(TT_CODE_ACCESS);
    else
        raise_exception(TT_DATA_ACCESS);
3837 3838

    env = saved_env;
3839 3840
}
#endif
3841

P
Paul Brook 已提交
3842

B
blueswir1 已提交
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866
#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