op_helper.c 117.6 KB
Newer Older
1
#include "exec.h"
B
blueswir1 已提交
2
#include "host-utils.h"
B
blueswir1 已提交
3
#include "helper.h"
F
Fabien Chouteau 已提交
4
#include "sysemu.h"
5

B
bellard 已提交
6
//#define DEBUG_MMU
7
//#define DEBUG_MXCC
B
blueswir1 已提交
8
//#define DEBUG_UNALIGNED
9
//#define DEBUG_UNASSIGNED
10
//#define DEBUG_ASI
B
blueswir1 已提交
11
//#define DEBUG_PCALL
12
//#define DEBUG_PSTATE
F
Fabien Chouteau 已提交
13
//#define DEBUG_CACHE_CONTROL
B
bellard 已提交
14

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

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

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

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

F
Fabien Chouteau 已提交
41 42 43 44 45 46 47
#ifdef DEBUG_CACHE_CONTROL
#define DPRINTF_CACHE_CONTROL(fmt, ...)                                   \
    do { printf("CACHE_CONTROL: " fmt , ## __VA_ARGS__); } while (0)
#else
#define DPRINTF_CACHE_CONTROL(fmt, ...) do {} while (0)
#endif

B
blueswir1 已提交
48 49 50
#ifdef TARGET_SPARC64
#ifndef TARGET_ABI32
#define AM_CHECK(env1) ((env1)->pstate & PS_AM)
51
#else
B
blueswir1 已提交
52 53
#define AM_CHECK(env1) (1)
#endif
54 55
#endif

56 57 58 59 60
#define DT0 (env->dt0)
#define DT1 (env->dt1)
#define QT0 (env->qt0)
#define QT1 (env->qt1)

F
Fabien Chouteau 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/* Leon3 cache control */

/* Cache control: emulate the behavior of cache control registers but without
   any effect on the emulated */

#define CACHE_STATE_MASK 0x3
#define CACHE_DISABLED   0x0
#define CACHE_FROZEN     0x1
#define CACHE_ENABLED    0x3

/* Cache Control register fields */

#define CACHE_CTRL_IF (1 <<  4)  /* Instruction Cache Freeze on Interrupt */
#define CACHE_CTRL_DF (1 <<  5)  /* Data Cache Freeze on Interrupt */
#define CACHE_CTRL_DP (1 << 14)  /* Data cache flush pending */
#define CACHE_CTRL_IP (1 << 15)  /* Instruction cache flush pending */
#define CACHE_CTRL_IB (1 << 16)  /* Instruction burst fetch */
#define CACHE_CTRL_FI (1 << 21)  /* Flush Instruction cache (Write only) */
#define CACHE_CTRL_FD (1 << 22)  /* Flush Data cache (Write only) */
#define CACHE_CTRL_DS (1 << 23)  /* Data cache snoop enable */

P
Paul Brook 已提交
82 83 84 85 86
#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

87
#if defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
88 89 90 91 92 93
// 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;
94 95
    int tsb_split = (tsb_register & 0x1000ULL) ? 1 : 0;
    int tsb_size  = tsb_register & 0xf;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

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

135 136 137
static void replace_tlb_entry(SparcTLBEntry *tlb,
                              uint64_t tlb_tag, uint64_t tlb_tte,
                              CPUState *env1)
138 139 140 141
{
    target_ulong mask, size, va, offset;

    // flush page range if translation is valid
142
    if (TTE_IS_VALID(tlb->tte)) {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

        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,
160
                      const char* strmmu, CPUState *env1)
161 162 163
{
    unsigned int i;
    target_ulong mask;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    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;
    }
183 184

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

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            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;
                }
            }
209

210
            replace_tlb_entry(&tlb[i], 0, 0, env1);
211
#ifdef DEBUG_MMU
212
            DPRINTF_MMU("%s demap invalidated entry [%02u]\n", strmmu, i);
213
            dump_mmu(stdout, fprintf, env1);
214 215 216 217 218
#endif
        }
    }
}

219 220 221 222 223 224 225 226 227 228 229 230
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);
231
            dump_mmu(stdout, fprintf, env1);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#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);
250
                dump_mmu(stdout, fprintf, env1);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
#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?
}

269 270
#endif

271
static inline target_ulong address_mask(CPUState *env1, target_ulong addr)
B
blueswir1 已提交
272 273 274
{
#ifdef TARGET_SPARC64
    if (AM_CHECK(env1))
275
        addr &= 0xffffffffULL;
B
blueswir1 已提交
276
#endif
277
    return addr;
B
blueswir1 已提交
278 279
}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
/* returns true if access using this ASI is to have address translated by MMU
   otherwise access is to raw physical address */
static inline int is_translating_asi(int asi)
{
#ifdef TARGET_SPARC64
    /* Ultrasparc IIi translating asi
       - note this list is defined by cpu implementation
     */
    switch (asi) {
    case 0x04 ... 0x11:
    case 0x18 ... 0x19:
    case 0x24 ... 0x2C:
    case 0x70 ... 0x73:
    case 0x78 ... 0x79:
    case 0x80 ... 0xFF:
        return 1;

    default:
        return 0;
    }
#else
    /* TODO: check sparc32 bits */
    return 0;
#endif
}

static inline target_ulong asi_address_mask(CPUState *env1,
                                            int asi, target_ulong addr)
{
    if (is_translating_asi(asi)) {
        return address_mask(env, addr);
    } else {
        return addr;
    }
}

B
blueswir1 已提交
316
static void raise_exception(int tt)
B
bellard 已提交
317 318
{
    env->exception_index = tt;
B
Blue Swirl 已提交
319
    cpu_loop_exit(env);
320
}
B
bellard 已提交
321

P
pbrook 已提交
322 323 324 325 326
void HELPER(raise_exception)(int tt)
{
    raise_exception(tt);
}

F
Fabien Chouteau 已提交
327 328 329 330 331 332 333
void helper_shutdown(void)
{
#if !defined(CONFIG_USER_ONLY)
    qemu_system_shutdown_request();
#endif
}

B
blueswir1 已提交
334 335
void helper_check_align(target_ulong addr, uint32_t align)
{
336 337 338 339 340
    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 已提交
341
        raise_exception(TT_UNALIGNED);
342
    }
B
blueswir1 已提交
343 344
}

345 346 347
#define F_HELPER(name, p) void helper_f##name##p(void)

#define F_BINOP(name)                                           \
B
blueswir1 已提交
348
    float32 helper_f ## name ## s (float32 src1, float32 src2)  \
349
    {                                                           \
B
blueswir1 已提交
350
        return float32_ ## name (src1, src2, &env->fp_status);  \
351 352 353 354
    }                                                           \
    F_HELPER(name, d)                                           \
    {                                                           \
        DT0 = float64_ ## name (DT0, DT1, &env->fp_status);     \
B
blueswir1 已提交
355 356 357 358
    }                                                           \
    F_HELPER(name, q)                                           \
    {                                                           \
        QT0 = float128_ ## name (QT0, QT1, &env->fp_status);    \
359 360 361 362 363 364 365 366
    }

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

367
void helper_fsmuld(float32 src1, float32 src2)
B
blueswir1 已提交
368
{
369 370
    DT0 = float64_mul(float32_to_float64(src1, &env->fp_status),
                      float32_to_float64(src2, &env->fp_status),
371 372
                      &env->fp_status);
}
B
blueswir1 已提交
373

B
blueswir1 已提交
374 375 376 377 378 379 380
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 已提交
381
float32 helper_fnegs(float32 src)
382
{
B
blueswir1 已提交
383
    return float32_chs(src);
384 385
}

386 387
#ifdef TARGET_SPARC64
F_HELPER(neg, d)
388
{
389
    DT0 = float64_chs(DT1);
390
}
B
blueswir1 已提交
391 392 393 394 395 396

F_HELPER(neg, q)
{
    QT0 = float128_chs(QT1);
}
#endif
397 398

/* Integer to float conversion.  */
B
blueswir1 已提交
399
float32 helper_fitos(int32_t src)
B
bellard 已提交
400
{
B
blueswir1 已提交
401
    return int32_to_float32(src, &env->fp_status);
B
bellard 已提交
402 403
}

404
void helper_fitod(int32_t src)
B
bellard 已提交
405
{
406
    DT0 = int32_to_float64(src, &env->fp_status);
B
bellard 已提交
407
}
408

409
void helper_fitoq(int32_t src)
B
blueswir1 已提交
410
{
411
    QT0 = int32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
412 413
}

B
blueswir1 已提交
414
#ifdef TARGET_SPARC64
415
float32 helper_fxtos(void)
B
blueswir1 已提交
416
{
417
    return int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
B
blueswir1 已提交
418 419
}

420
F_HELPER(xto, d)
B
blueswir1 已提交
421 422 423
{
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
}
B
blueswir1 已提交
424

B
blueswir1 已提交
425 426 427 428 429
F_HELPER(xto, q)
{
    QT0 = int64_to_float128(*((int64_t *)&DT1), &env->fp_status);
}
#endif
430 431 432
#undef F_HELPER

/* floating point conversion */
433
float32 helper_fdtos(void)
434
{
435
    return float64_to_float32(DT1, &env->fp_status);
436 437
}

438
void helper_fstod(float32 src)
439
{
440
    DT0 = float32_to_float64(src, &env->fp_status);
441
}
442

443
float32 helper_fqtos(void)
B
blueswir1 已提交
444
{
445
    return float128_to_float32(QT1, &env->fp_status);
B
blueswir1 已提交
446 447
}

448
void helper_fstoq(float32 src)
B
blueswir1 已提交
449
{
450
    QT0 = float32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
451 452 453 454 455 456 457 458 459 460 461 462
}

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

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

463
/* Float to integer conversion.  */
B
blueswir1 已提交
464
int32_t helper_fstoi(float32 src)
465
{
B
blueswir1 已提交
466
    return float32_to_int32_round_to_zero(src, &env->fp_status);
467 468
}

469
int32_t helper_fdtoi(void)
470
{
471
    return float64_to_int32_round_to_zero(DT1, &env->fp_status);
472 473
}

474
int32_t helper_fqtoi(void)
B
blueswir1 已提交
475
{
476
    return float128_to_int32_round_to_zero(QT1, &env->fp_status);
B
blueswir1 已提交
477 478
}

479
#ifdef TARGET_SPARC64
480
void helper_fstox(float32 src)
481
{
482
    *((int64_t *)&DT0) = float32_to_int64_round_to_zero(src, &env->fp_status);
483 484 485 486 487 488 489
}

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

B
blueswir1 已提交
490 491 492 493 494
void helper_fqtox(void)
{
    *((int64_t *)&DT0) = float128_to_int64_round_to_zero(QT1, &env->fp_status);
}

495 496 497 498 499
void helper_faligndata(void)
{
    uint64_t tmp;

    tmp = (*((uint64_t *)&DT0)) << ((env->gsr & 7) * 8);
B
blueswir1 已提交
500 501 502 503
    /* on many architectures a shift of 64 does nothing */
    if ((env->gsr & 7) != 0) {
        tmp |= (*((uint64_t *)&DT1)) >> (64 - (env->gsr & 7) * 8);
    }
504 505 506
    *((uint64_t *)&DT0) = tmp;
}

507
#ifdef HOST_WORDS_BIGENDIAN
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 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
#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;
724 725 726 727
    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;
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

    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 已提交
748
    uint32_t name##16s(uint32_t src1, uint32_t src2)    \
749 750 751
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
752 753
        s.l = src1;                                     \
        d.l = src2;                                     \
754 755 756 757
                                                        \
        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 已提交
758
        return d.l;                                     \
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    }                                                   \
                                                        \
    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 已提交
774
    uint32_t name##32s(uint32_t src1, uint32_t src2)    \
775 776 777
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
778 779
        s.l = src1;                                     \
        d.l = src2;                                     \
780 781 782
                                                        \
        d.l = F(d.l, s.l);                              \
                                                        \
B
blueswir1 已提交
783
        return d.l;                                     \
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    }

#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 已提交
865
float32 helper_fabss(float32 src)
866
{
B
blueswir1 已提交
867
    return float32_abs(src);
868 869
}

B
bellard 已提交
870
#ifdef TARGET_SPARC64
871
void helper_fabsd(void)
B
bellard 已提交
872 873 874
{
    DT0 = float64_abs(DT1);
}
B
blueswir1 已提交
875 876 877 878 879 880

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

B
blueswir1 已提交
882
float32 helper_fsqrts(float32 src)
883
{
B
blueswir1 已提交
884
    return float32_sqrt(src, &env->fp_status);
885 886
}

887
void helper_fsqrtd(void)
888
{
B
bellard 已提交
889
    DT0 = float64_sqrt(DT1, &env->fp_status);
890 891
}

B
blueswir1 已提交
892 893 894 895 896
void helper_fsqrtq(void)
{
    QT0 = float128_sqrt(QT1, &env->fp_status);
}

B
Blue Swirl 已提交
897
#define GEN_FCMP(name, size, reg1, reg2, FS, E)                         \
898
    void glue(helper_, name) (void)                                     \
B
bellard 已提交
899
    {                                                                   \
B
Blue Swirl 已提交
900 901 902 903 904 905 906 907
        env->fsr &= FSR_FTT_NMASK;                                      \
        if (E && (glue(size, _is_any_nan)(reg1) ||                      \
                     glue(size, _is_any_nan)(reg2)) &&                  \
            (env->fsr & FSR_NVM)) {                                     \
            env->fsr |= FSR_NVC;                                        \
            env->fsr |= FSR_FTT_IEEE_EXCP;                              \
            raise_exception(TT_FP_EXCP);                                \
        }                                                               \
B
bellard 已提交
908 909
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
B
Blue Swirl 已提交
910
            if ((env->fsr & FSR_NVM)) {                                 \
911 912
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
B
bellard 已提交
913 914
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
B
Blue Swirl 已提交
915 916
                env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);             \
                env->fsr |= (FSR_FCC1 | FSR_FCC0) << FS;                \
B
bellard 已提交
917 918 919 920
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
B
Blue Swirl 已提交
921 922
            env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                 \
            env->fsr |= FSR_FCC0 << FS;                                 \
B
bellard 已提交
923 924
            break;                                                      \
        case float_relation_greater:                                    \
B
Blue Swirl 已提交
925 926
            env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                 \
            env->fsr |= FSR_FCC1 << FS;                                 \
B
bellard 已提交
927 928
            break;                                                      \
        default:                                                        \
B
Blue Swirl 已提交
929
            env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                 \
B
bellard 已提交
930 931
            break;                                                      \
        }                                                               \
932
    }
B
Blue Swirl 已提交
933
#define GEN_FCMPS(name, size, FS, E)                                    \
B
blueswir1 已提交
934 935
    void glue(helper_, name)(float32 src1, float32 src2)                \
    {                                                                   \
B
Blue Swirl 已提交
936 937 938 939 940 941 942 943
        env->fsr &= FSR_FTT_NMASK;                                      \
        if (E && (glue(size, _is_any_nan)(src1) ||                      \
                     glue(size, _is_any_nan)(src2)) &&                  \
            (env->fsr & FSR_NVM)) {                                     \
            env->fsr |= FSR_NVC;                                        \
            env->fsr |= FSR_FTT_IEEE_EXCP;                              \
            raise_exception(TT_FP_EXCP);                                \
        }                                                               \
B
blueswir1 已提交
944 945
        switch (glue(size, _compare) (src1, src2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
B
Blue Swirl 已提交
946
            if ((env->fsr & FSR_NVM)) {                                 \
B
blueswir1 已提交
947 948 949 950
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
B
Blue Swirl 已提交
951 952
                env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);             \
                env->fsr |= (FSR_FCC1 | FSR_FCC0) << FS;                \
B
blueswir1 已提交
953 954 955 956
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
B
Blue Swirl 已提交
957 958
            env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                 \
            env->fsr |= FSR_FCC0 << FS;                                 \
B
blueswir1 已提交
959 960
            break;                                                      \
        case float_relation_greater:                                    \
B
Blue Swirl 已提交
961 962
            env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                 \
            env->fsr |= FSR_FCC1 << FS;                                 \
B
blueswir1 已提交
963 964
            break;                                                      \
        default:                                                        \
B
Blue Swirl 已提交
965
            env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                 \
B
blueswir1 已提交
966 967 968
            break;                                                      \
        }                                                               \
    }
969

B
blueswir1 已提交
970
GEN_FCMPS(fcmps, float32, 0, 0);
971 972
GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);

B
blueswir1 已提交
973
GEN_FCMPS(fcmpes, float32, 0, 1);
974
GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
B
bellard 已提交
975

B
blueswir1 已提交
976 977 978
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);

979 980 981 982 983 984 985 986 987 988
static uint32_t compute_all_flags(void)
{
    return env->psr & PSR_ICC;
}

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

989
static inline uint32_t get_NZ_icc(int32_t dst)
B
Blue Swirl 已提交
990 991 992
{
    uint32_t ret = 0;

993 994 995 996 997
    if (dst == 0) {
        ret = PSR_ZERO;
    } else if (dst < 0) {
        ret = PSR_NEG;
    }
B
Blue Swirl 已提交
998 999 1000
    return ret;
}

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
#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;
}

1012
static inline uint32_t get_NZ_xcc(target_long dst)
B
Blue Swirl 已提交
1013 1014 1015
{
    uint32_t ret = 0;

1016 1017 1018 1019 1020
    if (!dst) {
        ret = PSR_ZERO;
    } else if (dst < 0) {
        ret = PSR_NEG;
    }
B
Blue Swirl 已提交
1021 1022 1023 1024
    return ret;
}
#endif

B
Blue Swirl 已提交
1025 1026 1027 1028
static inline uint32_t get_V_div_icc(target_ulong src2)
{
    uint32_t ret = 0;

1029 1030 1031
    if (src2 != 0) {
        ret = PSR_OVF;
    }
B
Blue Swirl 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    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;
}

1049
static inline uint32_t get_C_add_icc(uint32_t dst, uint32_t src1)
B
Blue Swirl 已提交
1050 1051 1052
{
    uint32_t ret = 0;

1053 1054 1055
    if (dst < src1) {
        ret = PSR_CARRY;
    }
B
Blue Swirl 已提交
1056 1057 1058
    return ret;
}

1059 1060
static inline uint32_t get_C_addx_icc(uint32_t dst, uint32_t src1,
                                      uint32_t src2)
B
Blue Swirl 已提交
1061 1062 1063
{
    uint32_t ret = 0;

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    if (((src1 & src2) | (~dst & (src1 | src2))) & (1U << 31)) {
        ret = PSR_CARRY;
    }
    return ret;
}

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

    if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1U << 31)) {
        ret = PSR_OVF;
    }
B
Blue Swirl 已提交
1078 1079 1080 1081 1082 1083 1084 1085
    return ret;
}

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

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    if (dst < src1) {
        ret = PSR_CARRY;
    }
    return ret;
}

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

    if (((src1 & src2) | (~dst & (src1 | src2))) & (1ULL << 63)) {
        ret = PSR_CARRY;
    }
B
Blue Swirl 已提交
1100 1101 1102 1103 1104 1105 1106 1107
    return ret;
}

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

1108 1109 1110
    if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1ULL << 63)) {
        ret = PSR_OVF;
    }
B
Blue Swirl 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    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);
}
1128 1129
#endif

1130
static uint32_t compute_all_add(void)
B
Blue Swirl 已提交
1131 1132 1133 1134
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1135
    ret |= get_C_add_icc(CC_DST, CC_SRC);
B
Blue Swirl 已提交
1136 1137 1138 1139
    ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

1140
static uint32_t compute_C_add(void)
B
Blue Swirl 已提交
1141
{
1142
    return get_C_add_icc(CC_DST, CC_SRC);
B
Blue Swirl 已提交
1143 1144 1145 1146 1147 1148 1149 1150
}

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

    ret = get_NZ_xcc(CC_DST);
1151
    ret |= get_C_addx_xcc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1152 1153 1154 1155 1156 1157 1158 1159
    ret |= get_V_add_xcc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_addx_xcc(void)
{
    uint32_t ret;

1160
    ret = get_C_addx_xcc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1161 1162 1163 1164
    return ret;
}
#endif

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
static uint32_t compute_all_addx(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
    ret |= get_C_addx_icc(CC_DST, CC_SRC, CC_SRC2);
    ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_addx(void)
{
    uint32_t ret;

    ret = get_C_addx_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

B
Blue Swirl 已提交
1183 1184 1185 1186
static inline uint32_t get_V_tag_icc(target_ulong src1, target_ulong src2)
{
    uint32_t ret = 0;

1187 1188 1189
    if ((src1 | src2) & 0x3) {
        ret = PSR_OVF;
    }
B
Blue Swirl 已提交
1190 1191 1192 1193 1194 1195 1196 1197
    return ret;
}

static uint32_t compute_all_tadd(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1198
    ret |= get_C_add_icc(CC_DST, CC_SRC);
B
Blue Swirl 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    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_all_taddtv(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1209
    ret |= get_C_add_icc(CC_DST, CC_SRC);
B
Blue Swirl 已提交
1210 1211 1212
    return ret;
}

1213
static inline uint32_t get_C_sub_icc(uint32_t src1, uint32_t src2)
B
Blue Swirl 已提交
1214
{
1215 1216 1217 1218 1219 1220
    uint32_t ret = 0;

    if (src1 < src2) {
        ret = PSR_CARRY;
    }
    return ret;
B
Blue Swirl 已提交
1221 1222
}

1223 1224
static inline uint32_t get_C_subx_icc(uint32_t dst, uint32_t src1,
                                      uint32_t src2)
B
Blue Swirl 已提交
1225 1226 1227
{
    uint32_t ret = 0;

1228 1229 1230
    if (((~src1 & src2) | (dst & (~src1 | src2))) & (1U << 31)) {
        ret = PSR_CARRY;
    }
B
Blue Swirl 已提交
1231 1232 1233
    return ret;
}

1234 1235
static inline uint32_t get_V_sub_icc(uint32_t dst, uint32_t src1,
                                     uint32_t src2)
B
Blue Swirl 已提交
1236 1237 1238
{
    uint32_t ret = 0;

1239 1240 1241
    if (((src1 ^ src2) & (src1 ^ dst)) & (1U << 31)) {
        ret = PSR_OVF;
    }
B
Blue Swirl 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250
    return ret;
}


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

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    if (src1 < src2) {
        ret = PSR_CARRY;
    }
    return ret;
}

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

    if (((~src1 & src2) | (dst & (~src1 | src2))) & (1ULL << 63)) {
        ret = PSR_CARRY;
    }
B
Blue Swirl 已提交
1265 1266 1267 1268 1269 1270 1271 1272
    return ret;
}

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

1273 1274 1275
    if (((src1 ^ src2) & (src1 ^ dst)) & (1ULL << 63)) {
        ret = PSR_OVF;
    }
B
Blue Swirl 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    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

1295
static uint32_t compute_all_sub(void)
B
Blue Swirl 已提交
1296 1297 1298 1299
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1300
    ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1301 1302 1303 1304
    ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

1305
static uint32_t compute_C_sub(void)
B
Blue Swirl 已提交
1306
{
1307
    return get_C_sub_icc(CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1308 1309 1310 1311 1312 1313 1314 1315
}

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

    ret = get_NZ_xcc(CC_DST);
1316
    ret |= get_C_subx_xcc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1317 1318 1319 1320 1321 1322 1323 1324
    ret |= get_V_sub_xcc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

static uint32_t compute_C_subx_xcc(void)
{
    uint32_t ret;

1325
    ret = get_C_subx_xcc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1326 1327 1328 1329
    return ret;
}
#endif

1330
static uint32_t compute_all_subx(void)
B
Blue Swirl 已提交
1331 1332 1333 1334
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1335
    ret |= get_C_subx_icc(CC_DST, CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1336 1337 1338 1339
    ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
}

1340
static uint32_t compute_C_subx(void)
B
Blue Swirl 已提交
1341
{
1342 1343 1344 1345
    uint32_t ret;

    ret = get_C_subx_icc(CC_DST, CC_SRC, CC_SRC2);
    return ret;
B
Blue Swirl 已提交
1346 1347
}

1348
static uint32_t compute_all_tsub(void)
B
Blue Swirl 已提交
1349 1350 1351 1352
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
1353 1354 1355
    ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
    ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
    ret |= get_V_tag_icc(CC_SRC, CC_SRC2);
B
Blue Swirl 已提交
1356 1357 1358
    return ret;
}

1359
static uint32_t compute_all_tsubtv(void)
B
Blue Swirl 已提交
1360
{
1361 1362 1363 1364 1365
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
    ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
    return ret;
B
Blue Swirl 已提交
1366 1367
}

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
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

1385 1386 1387 1388 1389 1390 1391 1392
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 已提交
1393
    [CC_OP_DIV] = { compute_all_div, compute_C_div },
B
Blue Swirl 已提交
1394
    [CC_OP_ADD] = { compute_all_add, compute_C_add },
1395 1396 1397
    [CC_OP_ADDX] = { compute_all_addx, compute_C_addx },
    [CC_OP_TADD] = { compute_all_tadd, compute_C_add },
    [CC_OP_TADDTV] = { compute_all_taddtv, compute_C_add },
B
Blue Swirl 已提交
1398
    [CC_OP_SUB] = { compute_all_sub, compute_C_sub },
1399 1400 1401
    [CC_OP_SUBX] = { compute_all_subx, compute_C_subx },
    [CC_OP_TSUB] = { compute_all_tsub, compute_C_sub },
    [CC_OP_TSUBTV] = { compute_all_tsubtv, compute_C_sub },
1402
    [CC_OP_LOGIC] = { compute_all_logic, compute_C_logic },
1403 1404 1405 1406 1407 1408
};

#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 已提交
1409
    [CC_OP_DIV] = { compute_all_logic_xcc, compute_C_logic },
B
Blue Swirl 已提交
1410
    [CC_OP_ADD] = { compute_all_add_xcc, compute_C_add_xcc },
B
Blue Swirl 已提交
1411
    [CC_OP_ADDX] = { compute_all_addx_xcc, compute_C_addx_xcc },
B
Blue Swirl 已提交
1412 1413
    [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 已提交
1414
    [CC_OP_SUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
B
Blue Swirl 已提交
1415
    [CC_OP_SUBX] = { compute_all_subx_xcc, compute_C_subx_xcc },
B
Blue Swirl 已提交
1416 1417
    [CC_OP_TSUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
    [CC_OP_TSUBTV] = { compute_all_sub_xcc, compute_C_sub_xcc },
1418
    [CC_OP_LOGIC] = { compute_all_logic_xcc, compute_C_logic },
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
};
#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;
}

1435
uint32_t helper_compute_C_icc(void)
1436 1437 1438 1439 1440 1441 1442
{
    uint32_t ret;

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

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
static inline void memcpy32(target_ulong *dst, const target_ulong *src)
{
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
    dst[4] = src[4];
    dst[5] = src[5];
    dst[6] = src[6];
    dst[7] = src[7];
}

static void set_cwp(int new_cwp)
{
    /* put the modified wrap registers at their proper location */
    if (env->cwp == env->nwindows - 1) {
        memcpy32(env->regbase, env->regbase + env->nwindows * 16);
    }
    env->cwp = new_cwp;

    /* put the wrap registers at their temporary location */
    if (new_cwp == env->nwindows - 1) {
        memcpy32(env->regbase + env->nwindows * 16, env->regbase);
    }
    env->regwptr = env->regbase + (new_cwp * 16);
}

void cpu_set_cwp(CPUState *env1, int new_cwp)
{
    CPUState *saved_env;

    saved_env = env;
    env = env1;
    set_cwp(new_cwp);
    env = saved_env;
}

static target_ulong get_psr(void)
{
    helper_compute_psr();

#if !defined (TARGET_SPARC64)
    return env->version | (env->psr & PSR_ICC) |
        (env->psref? PSR_EF : 0) |
        (env->psrpil << 8) |
        (env->psrs? PSR_S : 0) |
        (env->psrps? PSR_PS : 0) |
        (env->psret? PSR_ET : 0) | env->cwp;
#else
1492
    return env->psr & PSR_ICC;
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
#endif
}

target_ulong cpu_get_psr(CPUState *env1)
{
    CPUState *saved_env;
    target_ulong ret;

    saved_env = env;
    env = env1;
    ret = get_psr();
    env = saved_env;
    return ret;
}

static void put_psr(target_ulong val)
{
    env->psr = val & PSR_ICC;
1511
#if !defined (TARGET_SPARC64)
1512 1513
    env->psref = (val & PSR_EF)? 1 : 0;
    env->psrpil = (val & PSR_PIL) >> 8;
1514
#endif
1515 1516 1517
#if ((!defined (TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY))
    cpu_check_irqs(env);
#endif
1518
#if !defined (TARGET_SPARC64)
1519 1520 1521 1522
    env->psrs = (val & PSR_S)? 1 : 0;
    env->psrps = (val & PSR_PS)? 1 : 0;
    env->psret = (val & PSR_ET)? 1 : 0;
    set_cwp(val & PSR_CWP);
1523
#endif
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
    env->cc_op = CC_OP_FLAGS;
}

void cpu_put_psr(CPUState *env1, target_ulong val)
{
    CPUState *saved_env;

    saved_env = env;
    env = env1;
    put_psr(val);
    env = saved_env;
}

static int cwp_inc(int cwp)
{
    if (unlikely(cwp >= env->nwindows)) {
        cwp -= env->nwindows;
    }
    return cwp;
}

int cpu_cwp_inc(CPUState *env1, int cwp)
{
    CPUState *saved_env;
    target_ulong ret;

    saved_env = env;
    env = env1;
    ret = cwp_inc(cwp);
    env = saved_env;
    return ret;
}

static int cwp_dec(int cwp)
{
    if (unlikely(cwp < 0)) {
        cwp += env->nwindows;
    }
    return cwp;
}

int cpu_cwp_dec(CPUState *env1, int cwp)
{
    CPUState *saved_env;
    target_ulong ret;

    saved_env = env;
    env = env1;
    ret = cwp_dec(cwp);
    env = saved_env;
    return ret;
}

B
bellard 已提交
1577
#ifdef TARGET_SPARC64
B
blueswir1 已提交
1578
GEN_FCMPS(fcmps_fcc1, float32, 22, 0);
1579
GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
B
blueswir1 已提交
1580
GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
1581

B
blueswir1 已提交
1582
GEN_FCMPS(fcmps_fcc2, float32, 24, 0);
1583
GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
B
blueswir1 已提交
1584
GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
1585

B
blueswir1 已提交
1586
GEN_FCMPS(fcmps_fcc3, float32, 26, 0);
1587
GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
B
blueswir1 已提交
1588
GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
1589

B
blueswir1 已提交
1590
GEN_FCMPS(fcmpes_fcc1, float32, 22, 1);
1591
GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
B
blueswir1 已提交
1592
GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
B
bellard 已提交
1593

B
blueswir1 已提交
1594
GEN_FCMPS(fcmpes_fcc2, float32, 24, 1);
1595
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
B
blueswir1 已提交
1596
GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
B
bellard 已提交
1597

B
blueswir1 已提交
1598
GEN_FCMPS(fcmpes_fcc3, float32, 26, 1);
1599
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
B
blueswir1 已提交
1600 1601
GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
#endif
B
blueswir1 已提交
1602
#undef GEN_FCMPS
B
bellard 已提交
1603

B
blueswir1 已提交
1604 1605
#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
    defined(DEBUG_MXCC)
1606 1607
static void dump_mxcc(CPUState *env)
{
1608 1609
    printf("mxccdata: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n",
B
blueswir1 已提交
1610 1611
           env->mxccdata[0], env->mxccdata[1],
           env->mxccdata[2], env->mxccdata[3]);
1612 1613 1614 1615
    printf("mxccregs: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n"
           "          %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
           "\n",
B
blueswir1 已提交
1616 1617 1618 1619
           env->mxccregs[0], env->mxccregs[1],
           env->mxccregs[2], env->mxccregs[3],
           env->mxccregs[4], env->mxccregs[5],
           env->mxccregs[6], env->mxccregs[7]);
1620 1621 1622
}
#endif

B
blueswir1 已提交
1623 1624 1625 1626
#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)
1627 1628 1629 1630
{
    switch (size)
    {
    case 1:
B
blueswir1 已提交
1631 1632
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xff);
1633 1634
        break;
    case 2:
B
blueswir1 已提交
1635 1636
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffff);
1637 1638
        break;
    case 4:
B
blueswir1 已提交
1639 1640
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffffffff);
1641 1642
        break;
    case 8:
B
blueswir1 已提交
1643 1644
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
                    addr, asi, r1);
1645 1646 1647 1648 1649
        break;
    }
}
#endif

B
blueswir1 已提交
1650 1651
#ifndef TARGET_SPARC64
#ifndef CONFIG_USER_ONLY
F
Fabien Chouteau 已提交
1652 1653 1654 1655


/* Leon3 cache control */

F
Fabien Chouteau 已提交
1656
static void leon3_cache_control_int(void)
F
Fabien Chouteau 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
{
    uint32_t state = 0;

    if (env->cache_control & CACHE_CTRL_IF) {
        /* Instruction cache state */
        state = env->cache_control & CACHE_STATE_MASK;
        if (state == CACHE_ENABLED) {
            state = CACHE_FROZEN;
            DPRINTF_CACHE_CONTROL("Instruction cache: freeze\n");
        }

        env->cache_control &= ~CACHE_STATE_MASK;
        env->cache_control |= state;
    }

    if (env->cache_control & CACHE_CTRL_DF) {
        /* Data cache state */
        state = (env->cache_control >> 2) & CACHE_STATE_MASK;
        if (state == CACHE_ENABLED) {
            state = CACHE_FROZEN;
            DPRINTF_CACHE_CONTROL("Data cache: freeze\n");
        }

        env->cache_control &= ~(CACHE_STATE_MASK << 2);
        env->cache_control |= (state << 2);
    }
}

static void leon3_cache_control_st(target_ulong addr, uint64_t val, int size)
{
    DPRINTF_CACHE_CONTROL("st addr:%08x, val:%" PRIx64 ", size:%d\n",
                          addr, val, size);

    if (size != 4) {
        DPRINTF_CACHE_CONTROL("32bits only\n");
        return;
    }

    switch (addr) {
    case 0x00:              /* Cache control */

        /* These values must always be read as zeros */
        val &= ~CACHE_CTRL_FD;
        val &= ~CACHE_CTRL_FI;
        val &= ~CACHE_CTRL_IB;
        val &= ~CACHE_CTRL_IP;
        val &= ~CACHE_CTRL_DP;

        env->cache_control = val;
        break;
    case 0x04:              /* Instruction cache configuration */
    case 0x08:              /* Data cache configuration */
        /* Read Only */
        break;
    default:
        DPRINTF_CACHE_CONTROL("write unknown register %08x\n", addr);
        break;
    };
}

static uint64_t leon3_cache_control_ld(target_ulong addr, int size)
{
    uint64_t ret = 0;

    if (size != 4) {
        DPRINTF_CACHE_CONTROL("32bits only\n");
        return 0;
    }

    switch (addr) {
    case 0x00:              /* Cache control */
        ret = env->cache_control;
        break;

        /* Configuration registers are read and only always keep those
           predefined values */

    case 0x04:              /* Instruction cache configuration */
        ret = 0x10220000;
        break;
    case 0x08:              /* Data cache configuration */
        ret = 0x18220000;
        break;
    default:
        DPRINTF_CACHE_CONTROL("read unknown register %08x\n", addr);
        break;
    };
F
Fabien Chouteau 已提交
1744
    DPRINTF_CACHE_CONTROL("ld addr:%08x, ret:0x%" PRIx64 ", size:%d\n",
F
Fabien Chouteau 已提交
1745 1746 1747 1748
                          addr, ret, size);
    return ret;
}

F
Fabien Chouteau 已提交
1749 1750 1751 1752 1753 1754
void leon3_irq_manager(void *irq_manager, int intno)
{
    leon3_irq_ack(irq_manager, intno);
    leon3_cache_control_int();
}

B
blueswir1 已提交
1755
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1756
{
B
blueswir1 已提交
1757
    uint64_t ret = 0;
1758
#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
B
blueswir1 已提交
1759
    uint32_t last_addr = addr;
1760
#endif
B
bellard 已提交
1761

1762
    helper_check_align(addr, size - 1);
B
bellard 已提交
1763
    switch (asi) {
F
Fabien Chouteau 已提交
1764
    case 2: /* SuperSparc MXCC registers and Leon3 cache control */
B
blueswir1 已提交
1765
        switch (addr) {
F
Fabien Chouteau 已提交
1766 1767 1768
        case 0x00:          /* Leon3 Cache Control */
        case 0x08:          /* Leon3 Instruction Cache config */
        case 0x0C:          /* Leon3 Date Cache config */
F
Fabien Chouteau 已提交
1769 1770 1771
            if (env->def->features & CPU_FEATURE_CACHE_CTRL) {
                ret = leon3_cache_control_ld(addr, size);
            }
F
Fabien Chouteau 已提交
1772
            break;
1773
        case 0x01c00a00: /* MXCC control register */
B
blueswir1 已提交
1774 1775 1776
            if (size == 8)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1777 1778
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1779 1780 1781 1782 1783
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1784 1785
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1786
            break;
1787 1788
        case 0x01c00c00: /* Module reset register */
            if (size == 8) {
B
blueswir1 已提交
1789
                ret = env->mxccregs[5];
1790 1791
                // should we do something here?
            } else
B
blueswir1 已提交
1792 1793
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1794
            break;
1795
        case 0x01c00f00: /* MBus port address register */
B
blueswir1 已提交
1796 1797 1798
            if (size == 8)
                ret = env->mxccregs[7];
            else
B
blueswir1 已提交
1799 1800
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1801 1802
            break;
        default:
B
blueswir1 已提交
1803 1804
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1805 1806
            break;
        }
B
blueswir1 已提交
1807
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
1808
                     "addr = %08x -> ret = %" PRIx64 ","
B
blueswir1 已提交
1809
                     "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
1810 1811 1812
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1813
        break;
1814
    case 3: /* MMU probe */
B
blueswir1 已提交
1815 1816 1817
        {
            int mmulev;

B
blueswir1 已提交
1818
            mmulev = (addr >> 8) & 15;
B
blueswir1 已提交
1819 1820
            if (mmulev > 4)
                ret = 0;
B
blueswir1 已提交
1821 1822 1823 1824
            else
                ret = mmu_probe(env, addr, mmulev);
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
                        addr, mmulev, ret);
B
blueswir1 已提交
1825 1826
        }
        break;
1827
    case 4: /* read MMU regs */
B
blueswir1 已提交
1828
        {
B
blueswir1 已提交
1829
            int reg = (addr >> 8) & 0x1f;
1830

B
blueswir1 已提交
1831 1832
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
B
blueswir1 已提交
1833 1834 1835 1836 1837
                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 已提交
1838
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
B
blueswir1 已提交
1839 1840
        }
        break;
B
blueswir1 已提交
1841 1842 1843 1844
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1845 1846 1847
    case 9: /* Supervisor code access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1848
            ret = ldub_code(addr);
1849 1850
            break;
        case 2:
1851
            ret = lduw_code(addr);
1852 1853 1854
            break;
        default:
        case 4:
1855
            ret = ldl_code(addr);
1856 1857
            break;
        case 8:
1858
            ret = ldq_code(addr);
1859 1860 1861
            break;
        }
        break;
1862 1863 1864
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1865
            ret = ldub_user(addr);
1866 1867
            break;
        case 2:
1868
            ret = lduw_user(addr);
1869 1870 1871
            break;
        default:
        case 4:
1872
            ret = ldl_user(addr);
1873 1874
            break;
        case 8:
1875
            ret = ldq_user(addr);
1876 1877 1878 1879 1880 1881
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1882
            ret = ldub_kernel(addr);
1883 1884
            break;
        case 2:
1885
            ret = lduw_kernel(addr);
1886 1887 1888
            break;
        default:
        case 4:
1889
            ret = ldl_kernel(addr);
1890 1891
            break;
        case 8:
1892
            ret = ldq_kernel(addr);
1893 1894 1895
            break;
        }
        break;
1896 1897 1898 1899 1900 1901
    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 已提交
1902 1903
        switch(size) {
        case 1:
B
blueswir1 已提交
1904
            ret = ldub_phys(addr);
B
bellard 已提交
1905 1906
            break;
        case 2:
1907
            ret = lduw_phys(addr);
B
bellard 已提交
1908 1909 1910
            break;
        default:
        case 4:
1911
            ret = ldl_phys(addr);
B
bellard 已提交
1912
            break;
B
bellard 已提交
1913
        case 8:
1914
            ret = ldq_phys(addr);
B
blueswir1 已提交
1915
            break;
B
bellard 已提交
1916
        }
B
blueswir1 已提交
1917
        break;
1918
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1919 1920
        switch(size) {
        case 1:
A
Anthony Liguori 已提交
1921 1922
            ret = ldub_phys((target_phys_addr_t)addr
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1923 1924
            break;
        case 2:
A
Anthony Liguori 已提交
1925 1926
            ret = lduw_phys((target_phys_addr_t)addr
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1927 1928 1929
            break;
        default:
        case 4:
A
Anthony Liguori 已提交
1930 1931
            ret = ldl_phys((target_phys_addr_t)addr
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
1932 1933
            break;
        case 8:
A
Anthony Liguori 已提交
1934 1935
            ret = ldq_phys((target_phys_addr_t)addr
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
B
blueswir1 已提交
1936
            break;
1937
        }
B
blueswir1 已提交
1938
        break;
B
blueswir1 已提交
1939 1940 1941
    case 0x30: // Turbosparc secondary cache diagnostic
    case 0x31: // Turbosparc RAM snoop
    case 0x32: // Turbosparc page table descriptor diagnostic
B
blueswir1 已提交
1942 1943 1944
    case 0x39: /* data cache diagnostic register */
        ret = 0;
        break;
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    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;
            }
1964 1965
            DPRINTF_MMU("read breakpoint reg[%d] 0x%016" PRIx64 "\n", reg,
                        ret);
1966 1967
        }
        break;
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
    case 0x49: /* SuperSPARC MMU Counter Breakpoint Value */
        ret = env->mmubpctrv;
        break;
    case 0x4a: /* SuperSPARC MMU Counter Breakpoint Control */
        ret = env->mmubpctrc;
        break;
    case 0x4b: /* SuperSPARC MMU Counter Breakpoint Status */
        ret = env->mmubpctrs;
        break;
    case 0x4c: /* SuperSPARC MMU Breakpoint Action */
        ret = env->mmubpaction;
        break;
B
blueswir1 已提交
1980
    case 8: /* User code access, XXX */
1981
    default:
1982
        do_unassigned_access(addr, 0, 0, asi, size);
B
blueswir1 已提交
1983 1984
        ret = 0;
        break;
1985
    }
1986 1987 1988
    if (sign) {
        switch(size) {
        case 1:
B
blueswir1 已提交
1989
            ret = (int8_t) ret;
B
blueswir1 已提交
1990
            break;
1991
        case 2:
B
blueswir1 已提交
1992 1993 1994 1995
            ret = (int16_t) ret;
            break;
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1996
            break;
1997 1998 1999 2000
        default:
            break;
        }
    }
2001
#ifdef DEBUG_ASI
B
blueswir1 已提交
2002
    dump_asi("read ", last_addr, asi, size, ret);
2003
#endif
B
blueswir1 已提交
2004
    return ret;
2005 2006
}

B
blueswir1 已提交
2007
void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
2008
{
2009
    helper_check_align(addr, size - 1);
2010
    switch(asi) {
F
Fabien Chouteau 已提交
2011
    case 2: /* SuperSparc MXCC registers and Leon3 cache control */
B
blueswir1 已提交
2012
        switch (addr) {
F
Fabien Chouteau 已提交
2013 2014 2015
        case 0x00:          /* Leon3 Cache Control */
        case 0x08:          /* Leon3 Instruction Cache config */
        case 0x0C:          /* Leon3 Date Cache config */
F
Fabien Chouteau 已提交
2016 2017 2018
            if (env->def->features & CPU_FEATURE_CACHE_CTRL) {
                leon3_cache_control_st(addr, val, size);
            }
F
Fabien Chouteau 已提交
2019 2020
            break;

2021 2022
        case 0x01c00000: /* MXCC stream data register 0 */
            if (size == 8)
B
blueswir1 已提交
2023
                env->mxccdata[0] = val;
2024
            else
B
blueswir1 已提交
2025 2026
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2027 2028 2029
            break;
        case 0x01c00008: /* MXCC stream data register 1 */
            if (size == 8)
B
blueswir1 已提交
2030
                env->mxccdata[1] = val;
2031
            else
B
blueswir1 已提交
2032 2033
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2034 2035 2036
            break;
        case 0x01c00010: /* MXCC stream data register 2 */
            if (size == 8)
B
blueswir1 已提交
2037
                env->mxccdata[2] = val;
2038
            else
B
blueswir1 已提交
2039 2040
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2041 2042 2043
            break;
        case 0x01c00018: /* MXCC stream data register 3 */
            if (size == 8)
B
blueswir1 已提交
2044
                env->mxccdata[3] = val;
2045
            else
B
blueswir1 已提交
2046 2047
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2048 2049 2050
            break;
        case 0x01c00100: /* MXCC stream source */
            if (size == 8)
B
blueswir1 已提交
2051
                env->mxccregs[0] = val;
2052
            else
B
blueswir1 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
                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);
2063 2064 2065
            break;
        case 0x01c00200: /* MXCC stream destination */
            if (size == 8)
B
blueswir1 已提交
2066
                env->mxccregs[1] = val;
2067
            else
B
blueswir1 已提交
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
                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]);
2078 2079 2080
            break;
        case 0x01c00a00: /* MXCC control register */
            if (size == 8)
B
blueswir1 已提交
2081
                env->mxccregs[3] = val;
2082
            else
B
blueswir1 已提交
2083 2084
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2085 2086 2087
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
2088
                env->mxccregs[3] = (env->mxccregs[3] & 0xffffffff00000000ULL)
B
blueswir1 已提交
2089
                    | val;
2090
            else
B
blueswir1 已提交
2091 2092
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2093 2094
            break;
        case 0x01c00e00: /* MXCC error register  */
2095
            // writing a 1 bit clears the error
2096
            if (size == 8)
B
blueswir1 已提交
2097
                env->mxccregs[6] &= ~val;
2098
            else
B
blueswir1 已提交
2099 2100
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2101 2102 2103
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8)
B
blueswir1 已提交
2104
                env->mxccregs[7] = val;
2105
            else
B
blueswir1 已提交
2106 2107
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
2108 2109
            break;
        default:
B
blueswir1 已提交
2110 2111
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
2112 2113
            break;
        }
2114 2115
        DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %" PRIx64 "\n",
                     asi, size, addr, val);
2116 2117 2118
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
2119
        break;
2120
    case 3: /* MMU flush */
B
blueswir1 已提交
2121 2122
        {
            int mmulev;
B
bellard 已提交
2123

B
blueswir1 已提交
2124
            mmulev = (addr >> 8) & 15;
2125
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
B
blueswir1 已提交
2126 2127
            switch (mmulev) {
            case 0: // flush page
B
blueswir1 已提交
2128
                tlb_flush_page(env, addr & 0xfffff000);
B
blueswir1 已提交
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
                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 已提交
2139
#ifdef DEBUG_MMU
2140
            dump_mmu(stdout, fprintf, env);
B
bellard 已提交
2141
#endif
B
blueswir1 已提交
2142
        }
2143
        break;
2144
    case 4: /* write MMU regs */
B
blueswir1 已提交
2145
        {
B
blueswir1 已提交
2146
            int reg = (addr >> 8) & 0x1f;
B
blueswir1 已提交
2147
            uint32_t oldreg;
2148

B
blueswir1 已提交
2149
            oldreg = env->mmuregs[reg];
B
bellard 已提交
2150
            switch(reg) {
2151
            case 0: // Control Register
B
blueswir1 已提交
2152
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
B
blueswir1 已提交
2153
                                    (val & 0x00ffffff);
B
blueswir1 已提交
2154 2155
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
2156 2157
                if ((oldreg & (MMU_E | MMU_NF | env->def->mmu_bm)) !=
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->def->mmu_bm)))
B
bellard 已提交
2158 2159
                    tlb_flush(env, 1);
                break;
2160
            case 1: // Context Table Pointer Register
2161
                env->mmuregs[reg] = val & env->def->mmu_ctpr_mask;
2162 2163
                break;
            case 2: // Context Register
2164
                env->mmuregs[reg] = val & env->def->mmu_cxr_mask;
B
bellard 已提交
2165 2166 2167 2168 2169 2170
                if (oldreg != env->mmuregs[reg]) {
                    /* we flush when the MMU context changes because
                       QEMU has no MMU context support */
                    tlb_flush(env, 1);
                }
                break;
2171 2172 2173 2174
            case 3: // Synchronous Fault Status Register with Clear
            case 4: // Synchronous Fault Address Register
                break;
            case 0x10: // TLB Replacement Control Register
2175
                env->mmuregs[reg] = val & env->def->mmu_trcr_mask;
B
bellard 已提交
2176
                break;
2177
            case 0x13: // Synchronous Fault Status Register with Read and Clear
2178
                env->mmuregs[3] = val & env->def->mmu_sfsr_mask;
B
blueswir1 已提交
2179
                break;
2180
            case 0x14: // Synchronous Fault Address Register
B
blueswir1 已提交
2181
                env->mmuregs[4] = val;
B
blueswir1 已提交
2182
                break;
B
bellard 已提交
2183
            default:
B
blueswir1 已提交
2184
                env->mmuregs[reg] = val;
B
bellard 已提交
2185 2186 2187
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
B
blueswir1 已提交
2188 2189
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
                            reg, oldreg, env->mmuregs[reg]);
B
bellard 已提交
2190
            }
2191
#ifdef DEBUG_MMU
2192
            dump_mmu(stdout, fprintf, env);
B
bellard 已提交
2193
#endif
B
blueswir1 已提交
2194
        }
2195
        break;
B
blueswir1 已提交
2196 2197 2198 2199
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
2200 2201 2202
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
2203
            stb_user(addr, val);
2204 2205
            break;
        case 2:
2206
            stw_user(addr, val);
2207 2208 2209
            break;
        default:
        case 4:
2210
            stl_user(addr, val);
2211 2212
            break;
        case 8:
2213
            stq_user(addr, val);
2214 2215 2216 2217 2218 2219
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
2220
            stb_kernel(addr, val);
2221 2222
            break;
        case 2:
2223
            stw_kernel(addr, val);
2224 2225 2226
            break;
        default:
        case 4:
2227
            stl_kernel(addr, val);
2228 2229
            break;
        case 8:
2230
            stq_kernel(addr, val);
2231 2232 2233
            break;
        }
        break;
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
    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 已提交
2244
    case 0x17: /* Block copy, sta access */
B
blueswir1 已提交
2245
        {
B
blueswir1 已提交
2246 2247
            // val = src
            // addr = dst
B
blueswir1 已提交
2248
            // copy 32 bytes
2249
            unsigned int i;
B
blueswir1 已提交
2250
            uint32_t src = val & ~3, dst = addr & ~3, temp;
2251

2252 2253 2254 2255
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
B
blueswir1 已提交
2256
        }
2257
        break;
B
bellard 已提交
2258
    case 0x1f: /* Block fill, stda access */
B
blueswir1 已提交
2259
        {
B
blueswir1 已提交
2260 2261
            // addr = dst
            // fill 32 bytes with val
2262
            unsigned int i;
B
blueswir1 已提交
2263
            uint32_t dst = addr & 7;
2264 2265 2266

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
2267
        }
2268
        break;
2269
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
2270
        {
B
bellard 已提交
2271 2272
            switch(size) {
            case 1:
B
blueswir1 已提交
2273
                stb_phys(addr, val);
B
bellard 已提交
2274 2275
                break;
            case 2:
2276
                stw_phys(addr, val);
B
bellard 已提交
2277 2278 2279
                break;
            case 4:
            default:
2280
                stl_phys(addr, val);
B
bellard 已提交
2281
                break;
B
bellard 已提交
2282
            case 8:
2283
                stq_phys(addr, val);
B
bellard 已提交
2284
                break;
B
bellard 已提交
2285
            }
B
blueswir1 已提交
2286
        }
2287
        break;
B
blueswir1 已提交
2288
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
B
blueswir1 已提交
2289
        {
2290 2291
            switch(size) {
            case 1:
A
Anthony Liguori 已提交
2292 2293
                stb_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
2294 2295
                break;
            case 2:
A
Anthony Liguori 已提交
2296 2297
                stw_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
2298 2299 2300
                break;
            case 4:
            default:
A
Anthony Liguori 已提交
2301 2302
                stl_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
2303 2304
                break;
            case 8:
A
Anthony Liguori 已提交
2305 2306
                stq_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
2307 2308
                break;
            }
B
blueswir1 已提交
2309
        }
2310
        break;
B
blueswir1 已提交
2311 2312 2313
    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 已提交
2314 2315
    case 0x32: // store buffer control or Turbosparc page table
               // descriptor diagnostic
2316 2317 2318
    case 0x36: /* I-cache flash clear */
    case 0x37: /* D-cache flash clear */
        break;
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
    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;
            }
2337
            DPRINTF_MMU("write breakpoint reg[%d] 0x%016x\n", reg,
2338 2339 2340
                        env->mmuregs[reg]);
        }
        break;
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
    case 0x49: /* SuperSPARC MMU Counter Breakpoint Value */
        env->mmubpctrv = val & 0xffffffff;
        break;
    case 0x4a: /* SuperSPARC MMU Counter Breakpoint Control */
        env->mmubpctrc = val & 0x3;
        break;
    case 0x4b: /* SuperSPARC MMU Counter Breakpoint Status */
        env->mmubpctrs = val & 0x3;
        break;
    case 0x4c: /* SuperSPARC MMU Breakpoint Action */
        env->mmubpaction = val & 0x1fff;
        break;
B
blueswir1 已提交
2353
    case 8: /* User code access, XXX */
2354
    case 9: /* Supervisor code access, XXX */
2355
    default:
2356
        do_unassigned_access(addr, 1, 0, asi, size);
2357
        break;
2358
    }
2359
#ifdef DEBUG_ASI
B
blueswir1 已提交
2360
    dump_asi("write", addr, asi, size, val);
2361
#endif
2362 2363
}

2364 2365 2366 2367
#endif /* CONFIG_USER_ONLY */
#else /* TARGET_SPARC64 */

#ifdef CONFIG_USER_ONLY
B
blueswir1 已提交
2368
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
2369 2370
{
    uint64_t ret = 0;
B
blueswir1 已提交
2371 2372 2373
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
2374 2375 2376 2377

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

2378
    helper_check_align(addr, size - 1);
2379
    addr = asi_address_mask(env, asi, addr);
2380

2381 2382 2383
    switch (asi) {
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
B
blueswir1 已提交
2384 2385 2386 2387 2388 2389 2390 2391 2392
        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
2393 2394 2395
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
2396
                ret = ldub_raw(addr);
2397 2398
                break;
            case 2:
2399
                ret = lduw_raw(addr);
2400 2401
                break;
            case 4:
2402
                ret = ldl_raw(addr);
2403 2404 2405
                break;
            default:
            case 8:
2406
                ret = ldq_raw(addr);
2407 2408 2409 2410 2411 2412
                break;
            }
        }
        break;
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
2413 2414 2415 2416 2417 2418 2419 2420 2421
        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
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
        // 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 已提交
2437
            break;
2438 2439
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
2440
            break;
2441 2442
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
2443
            break;
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
2456
            break;
2457 2458
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
2459
            break;
2460 2461
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
2462
            break;
2463 2464 2465 2466
        default:
            break;
        }
    }
B
blueswir1 已提交
2467 2468 2469 2470
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
2471 2472
}

B
blueswir1 已提交
2473
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
2474
{
B
blueswir1 已提交
2475 2476 2477
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
2478 2479 2480
    if (asi < 0x80)
        raise_exception(TT_PRIV_ACT);

2481
    helper_check_align(addr, size - 1);
2482
    addr = asi_address_mask(env, asi, addr);
2483

2484 2485 2486 2487 2488 2489
    /* Convert to little endian */
    switch (asi) {
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
2490
            val = bswap16(val);
B
blueswir1 已提交
2491
            break;
2492
        case 4:
2493
            val = bswap32(val);
B
blueswir1 已提交
2494
            break;
2495
        case 8:
2496
            val = bswap64(val);
B
blueswir1 已提交
2497
            break;
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
        default:
            break;
        }
    default:
        break;
    }

    switch(asi) {
    case 0x80: // Primary
    case 0x88: // Primary LE
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
2511
                stb_raw(addr, val);
2512 2513
                break;
            case 2:
2514
                stw_raw(addr, val);
2515 2516
                break;
            case 4:
2517
                stl_raw(addr, val);
2518 2519 2520
                break;
            case 8:
            default:
2521
                stq_raw(addr, val);
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
                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:
2536
        do_unassigned_access(addr, 1, 0, 1, size);
2537 2538 2539 2540 2541
        return;
    }
}

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

B
blueswir1 已提交
2543
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
B
bellard 已提交
2544
{
B
bellard 已提交
2545
    uint64_t ret = 0;
B
blueswir1 已提交
2546 2547 2548
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
B
bellard 已提交
2549

I
Igor V. Kovalenko 已提交
2550 2551
    asi &= 0xff;

B
blueswir1 已提交
2552
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2553
        || (cpu_has_hypervisor(env)
2554
            && asi >= 0x30 && asi < 0x80
2555
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2556
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
2557

2558
    helper_check_align(addr, size - 1);
2559 2560
    addr = asi_address_mask(env, asi, addr);

B
bellard 已提交
2561
    switch (asi) {
B
blueswir1 已提交
2562 2563
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
2564 2565 2566 2567 2568 2569 2570 2571
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
        {
            /* secondary space access has lowest asi bit equal to 1 */
            int access_mmu_idx = ( asi & 1 ) ? MMU_KERNEL_IDX
                                             : MMU_KERNEL_SECONDARY_IDX;

            if (cpu_get_phys_page_nofault(env, addr, access_mmu_idx) == -1ULL) {
B
blueswir1 已提交
2572
#ifdef DEBUG_ASI
2573
                dump_asi("read ", last_addr, asi, size, ret);
B
blueswir1 已提交
2574
#endif
2575 2576
                return 0;
            }
B
blueswir1 已提交
2577 2578
        }
        // Fall through
2579
    case 0x10: // As if user primary
2580
    case 0x11: // As if user secondary
2581
    case 0x18: // As if user primary LE
2582
    case 0x19: // As if user secondary LE
2583
    case 0x80: // Primary
2584
    case 0x81: // Secondary
2585
    case 0x88: // Primary LE
2586
    case 0x89: // Secondary LE
B
blueswir1 已提交
2587 2588
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
2589
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
2590
            if (cpu_hypervisor_mode(env)) {
B
blueswir1 已提交
2591 2592
                switch(size) {
                case 1:
B
blueswir1 已提交
2593
                    ret = ldub_hypv(addr);
B
blueswir1 已提交
2594 2595
                    break;
                case 2:
2596
                    ret = lduw_hypv(addr);
B
blueswir1 已提交
2597 2598
                    break;
                case 4:
2599
                    ret = ldl_hypv(addr);
B
blueswir1 已提交
2600 2601 2602
                    break;
                default:
                case 8:
2603
                    ret = ldq_hypv(addr);
B
blueswir1 已提交
2604 2605 2606
                    break;
                }
            } else {
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
                /* secondary space access has lowest asi bit equal to 1 */
                if (asi & 1) {
                    switch(size) {
                    case 1:
                        ret = ldub_kernel_secondary(addr);
                        break;
                    case 2:
                        ret = lduw_kernel_secondary(addr);
                        break;
                    case 4:
                        ret = ldl_kernel_secondary(addr);
                        break;
                    default:
                    case 8:
                        ret = ldq_kernel_secondary(addr);
                        break;
                    }
                } else {
                    switch(size) {
                    case 1:
                        ret = ldub_kernel(addr);
                        break;
                    case 2:
                        ret = lduw_kernel(addr);
                        break;
                    case 4:
                        ret = ldl_kernel(addr);
                        break;
                    default:
                    case 8:
                        ret = ldq_kernel(addr);
                        break;
                    }
                }
            }
        } else {
            /* secondary space access has lowest asi bit equal to 1 */
            if (asi & 1) {
B
blueswir1 已提交
2645 2646
                switch(size) {
                case 1:
2647
                    ret = ldub_user_secondary(addr);
B
blueswir1 已提交
2648 2649
                    break;
                case 2:
2650
                    ret = lduw_user_secondary(addr);
B
blueswir1 已提交
2651 2652
                    break;
                case 4:
2653
                    ret = ldl_user_secondary(addr);
B
blueswir1 已提交
2654 2655 2656
                    break;
                default:
                case 8:
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
                    ret = ldq_user_secondary(addr);
                    break;
                }
            } else {
                switch(size) {
                case 1:
                    ret = ldub_user(addr);
                    break;
                case 2:
                    ret = lduw_user(addr);
                    break;
                case 4:
                    ret = ldl_user(addr);
                    break;
                default:
                case 8:
                    ret = ldq_user(addr);
B
blueswir1 已提交
2674 2675
                    break;
                }
2676 2677 2678
            }
        }
        break;
B
bellard 已提交
2679 2680
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
2681 2682
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
2683
        {
B
bellard 已提交
2684 2685
            switch(size) {
            case 1:
B
blueswir1 已提交
2686
                ret = ldub_phys(addr);
B
bellard 已提交
2687 2688
                break;
            case 2:
2689
                ret = lduw_phys(addr);
B
bellard 已提交
2690 2691
                break;
            case 4:
2692
                ret = ldl_phys(addr);
B
bellard 已提交
2693 2694 2695
                break;
            default:
            case 8:
2696
                ret = ldq_phys(addr);
B
bellard 已提交
2697 2698
                break;
            }
B
blueswir1 已提交
2699 2700
            break;
        }
B
blueswir1 已提交
2701 2702 2703 2704 2705
    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
bellard 已提交
2706 2707
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
    {
        switch(size) {
        case 1:
            ret = ldub_nucleus(addr);
            break;
        case 2:
            ret = lduw_nucleus(addr);
            break;
        case 4:
            ret = ldl_nucleus(addr);
            break;
        default:
        case 8:
            ret = ldq_nucleus(addr);
            break;
        }
        break;
    }
B
bellard 已提交
2726
    case 0x4a: // UPA config
B
blueswir1 已提交
2727 2728
        // XXX
        break;
B
bellard 已提交
2729
    case 0x45: // LSU
B
blueswir1 已提交
2730 2731
        ret = env->lsu;
        break;
B
bellard 已提交
2732
    case 0x50: // I-MMU regs
B
blueswir1 已提交
2733
        {
B
blueswir1 已提交
2734
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
2735

2736 2737
            if (reg == 0) {
                // I-TSB Tag Target register
2738
                ret = ultrasparc_tag_target(env->immu.tag_access);
2739 2740 2741 2742
            } else {
                ret = env->immuregs[reg];
            }

B
blueswir1 已提交
2743 2744
            break;
        }
B
bellard 已提交
2745
    case 0x51: // I-MMU 8k TSB pointer
2746 2747 2748
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
2749
            ret = ultrasparc_tsb_pointer(env->immu.tsb, env->immu.tag_access,
2750 2751 2752
                                         8*1024);
            break;
        }
B
bellard 已提交
2753
    case 0x52: // I-MMU 64k TSB pointer
2754 2755 2756
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
2757
            ret = ultrasparc_tsb_pointer(env->immu.tsb, env->immu.tag_access,
2758 2759 2760
                                         64*1024);
            break;
        }
2761 2762 2763 2764
    case 0x55: // I-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

2765
            ret = env->itlb[reg].tte;
2766 2767
            break;
        }
B
bellard 已提交
2768
    case 0x56: // I-MMU tag read
B
blueswir1 已提交
2769
        {
B
blueswir1 已提交
2770
            int reg = (addr >> 3) & 0x3f;
B
blueswir1 已提交
2771

2772
            ret = env->itlb[reg].tag;
B
blueswir1 已提交
2773 2774
            break;
        }
B
bellard 已提交
2775
    case 0x58: // D-MMU regs
B
blueswir1 已提交
2776
        {
B
blueswir1 已提交
2777
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
2778

2779 2780
            if (reg == 0) {
                // D-TSB Tag Target register
2781
                ret = ultrasparc_tag_target(env->dmmu.tag_access);
2782 2783 2784 2785 2786 2787 2788 2789 2790
            } 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
2791
            ret = ultrasparc_tsb_pointer(env->dmmu.tsb, env->dmmu.tag_access,
2792 2793 2794 2795 2796 2797 2798
                                         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
2799
            ret = ultrasparc_tsb_pointer(env->dmmu.tsb, env->dmmu.tag_access,
2800
                                         64*1024);
B
blueswir1 已提交
2801 2802
            break;
        }
2803 2804 2805 2806
    case 0x5d: // D-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

2807
            ret = env->dtlb[reg].tte;
2808 2809
            break;
        }
B
bellard 已提交
2810
    case 0x5e: // D-MMU tag read
B
blueswir1 已提交
2811
        {
B
blueswir1 已提交
2812
            int reg = (addr >> 3) & 0x3f;
B
blueswir1 已提交
2813

2814
            ret = env->dtlb[reg].tag;
B
blueswir1 已提交
2815 2816
            break;
        }
2817 2818
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2819 2820 2821
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2822 2823 2824 2825 2826 2827 2828 2829
    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 已提交
2830
    case 0x5b: // D-MMU data pointer
B
bellard 已提交
2831 2832 2833
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
2834 2835
        // XXX
        break;
B
bellard 已提交
2836 2837 2838 2839
    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 已提交
2840
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
2841
    default:
2842
        do_unassigned_access(addr, 0, 0, 1, size);
B
blueswir1 已提交
2843 2844
        ret = 0;
        break;
B
bellard 已提交
2845
    }
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860

    /* 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 已提交
2861
            break;
2862 2863
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
2864
            break;
2865 2866
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
2867
            break;
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
2880
            break;
2881 2882
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
2883
            break;
2884 2885
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
2886
            break;
2887 2888 2889 2890
        default:
            break;
        }
    }
B
blueswir1 已提交
2891 2892 2893 2894
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
B
bellard 已提交
2895 2896
}

B
blueswir1 已提交
2897
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
B
bellard 已提交
2898
{
B
blueswir1 已提交
2899 2900 2901
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
I
Igor V. Kovalenko 已提交
2902 2903 2904

    asi &= 0xff;

B
blueswir1 已提交
2905
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2906
        || (cpu_has_hypervisor(env)
2907
            && asi >= 0x30 && asi < 0x80
2908
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2909
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
2910

2911
    helper_check_align(addr, size - 1);
2912 2913
    addr = asi_address_mask(env, asi, addr);

2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
    /* 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:
2925
            val = bswap16(val);
B
blueswir1 已提交
2926
            break;
2927
        case 4:
2928
            val = bswap32(val);
B
blueswir1 已提交
2929
            break;
2930
        case 8:
2931
            val = bswap64(val);
B
blueswir1 已提交
2932
            break;
2933 2934 2935 2936 2937 2938 2939
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
2940
    switch(asi) {
2941
    case 0x10: // As if user primary
2942
    case 0x11: // As if user secondary
2943
    case 0x18: // As if user primary LE
2944
    case 0x19: // As if user secondary LE
2945
    case 0x80: // Primary
2946
    case 0x81: // Secondary
2947
    case 0x88: // Primary LE
2948
    case 0x89: // Secondary LE
B
blueswir1 已提交
2949 2950
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
2951
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
2952
            if (cpu_hypervisor_mode(env)) {
B
blueswir1 已提交
2953 2954
                switch(size) {
                case 1:
B
blueswir1 已提交
2955
                    stb_hypv(addr, val);
B
blueswir1 已提交
2956 2957
                    break;
                case 2:
2958
                    stw_hypv(addr, val);
B
blueswir1 已提交
2959 2960
                    break;
                case 4:
2961
                    stl_hypv(addr, val);
B
blueswir1 已提交
2962 2963 2964
                    break;
                case 8:
                default:
2965
                    stq_hypv(addr, val);
B
blueswir1 已提交
2966 2967 2968
                    break;
                }
            } else {
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006
                /* secondary space access has lowest asi bit equal to 1 */
                if (asi & 1) {
                    switch(size) {
                    case 1:
                        stb_kernel_secondary(addr, val);
                        break;
                    case 2:
                        stw_kernel_secondary(addr, val);
                        break;
                    case 4:
                        stl_kernel_secondary(addr, val);
                        break;
                    case 8:
                    default:
                        stq_kernel_secondary(addr, val);
                        break;
                    }
                } else {
                    switch(size) {
                    case 1:
                        stb_kernel(addr, val);
                        break;
                    case 2:
                        stw_kernel(addr, val);
                        break;
                    case 4:
                        stl_kernel(addr, val);
                        break;
                    case 8:
                    default:
                        stq_kernel(addr, val);
                        break;
                    }
                }
            }
        } else {
            /* secondary space access has lowest asi bit equal to 1 */
            if (asi & 1) {
B
blueswir1 已提交
3007 3008
                switch(size) {
                case 1:
3009
                    stb_user_secondary(addr, val);
B
blueswir1 已提交
3010 3011
                    break;
                case 2:
3012
                    stw_user_secondary(addr, val);
B
blueswir1 已提交
3013 3014
                    break;
                case 4:
3015
                    stl_user_secondary(addr, val);
B
blueswir1 已提交
3016 3017 3018
                    break;
                case 8:
                default:
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
                    stq_user_secondary(addr, val);
                    break;
                }
            } else {
                switch(size) {
                case 1:
                    stb_user(addr, val);
                    break;
                case 2:
                    stw_user(addr, val);
                    break;
                case 4:
                    stl_user(addr, val);
                    break;
                case 8:
                default:
                    stq_user(addr, val);
B
blueswir1 已提交
3036 3037
                    break;
                }
3038 3039 3040
            }
        }
        break;
B
bellard 已提交
3041 3042
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
3043 3044
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
3045
        {
B
bellard 已提交
3046 3047
            switch(size) {
            case 1:
B
blueswir1 已提交
3048
                stb_phys(addr, val);
B
bellard 已提交
3049 3050
                break;
            case 2:
3051
                stw_phys(addr, val);
B
bellard 已提交
3052 3053
                break;
            case 4:
3054
                stl_phys(addr, val);
B
bellard 已提交
3055 3056 3057
                break;
            case 8:
            default:
3058
                stq_phys(addr, val);
B
bellard 已提交
3059 3060
                break;
            }
B
blueswir1 已提交
3061 3062
        }
        return;
B
blueswir1 已提交
3063 3064 3065 3066 3067
    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 已提交
3068 3069
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
    {
        switch(size) {
        case 1:
            stb_nucleus(addr, val);
            break;
        case 2:
            stw_nucleus(addr, val);
            break;
        case 4:
            stl_nucleus(addr, val);
            break;
        default:
        case 8:
            stq_nucleus(addr, val);
            break;
        }
        break;
    }

B
bellard 已提交
3089
    case 0x4a: // UPA config
B
blueswir1 已提交
3090 3091
        // XXX
        return;
B
bellard 已提交
3092
    case 0x45: // LSU
B
blueswir1 已提交
3093 3094 3095 3096
        {
            uint64_t oldreg;

            oldreg = env->lsu;
B
blueswir1 已提交
3097
            env->lsu = val & (DMMU_E | IMMU_E);
B
blueswir1 已提交
3098 3099 3100
            // Mappings generated during D/I MMU disabled mode are
            // invalid in normal mode
            if (oldreg != env->lsu) {
B
blueswir1 已提交
3101 3102
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
                            oldreg, env->lsu);
B
bellard 已提交
3103
#ifdef DEBUG_MMU
3104
                dump_mmu(stdout, fprintf, env1);
B
bellard 已提交
3105
#endif
B
blueswir1 已提交
3106 3107 3108 3109
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
3110
    case 0x50: // I-MMU regs
B
blueswir1 已提交
3111
        {
B
blueswir1 已提交
3112
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
3113
            uint64_t oldreg;
3114

B
blueswir1 已提交
3115
            oldreg = env->immuregs[reg];
B
bellard 已提交
3116 3117 3118 3119 3120 3121 3122
            switch(reg) {
            case 0: // RO
                return;
            case 1: // Not in I-MMU
            case 2:
                return;
            case 3: // SFSR
B
blueswir1 已提交
3123 3124
                if ((val & 1) == 0)
                    val = 0; // Clear SFSR
3125
                env->immu.sfsr = val;
B
bellard 已提交
3126
                break;
3127 3128
            case 4: // RO
                return;
B
bellard 已提交
3129
            case 5: // TSB access
3130 3131 3132 3133
                DPRINTF_MMU("immu TSB write: 0x%016" PRIx64 " -> 0x%016"
                            PRIx64 "\n", env->immu.tsb, val);
                env->immu.tsb = val;
                break;
B
bellard 已提交
3134
            case 6: // Tag access
3135 3136 3137 3138 3139
                env->immu.tag_access = val;
                break;
            case 7:
            case 8:
                return;
B
bellard 已提交
3140 3141 3142
            default:
                break;
            }
3143

B
bellard 已提交
3144
            if (oldreg != env->immuregs[reg]) {
3145
                DPRINTF_MMU("immu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
B
blueswir1 已提交
3146
                            PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
B
bellard 已提交
3147
            }
3148
#ifdef DEBUG_MMU
3149
            dump_mmu(stdout, fprintf, env);
B
bellard 已提交
3150
#endif
B
blueswir1 已提交
3151 3152
            return;
        }
B
bellard 已提交
3153
    case 0x54: // I-MMU data in
3154 3155
        replace_tlb_1bit_lru(env->itlb, env->immu.tag_access, val, "immu", env);
        return;
B
bellard 已提交
3156
    case 0x55: // I-MMU data access
B
blueswir1 已提交
3157
        {
3158 3159
            // TODO: auto demap

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

3162
            replace_tlb_entry(&env->itlb[i], env->immu.tag_access, val, env);
3163 3164

#ifdef DEBUG_MMU
3165
            DPRINTF_MMU("immu data access replaced entry [%i]\n", i);
3166
            dump_mmu(stdout, fprintf, env);
3167
#endif
B
blueswir1 已提交
3168 3169
            return;
        }
B
bellard 已提交
3170
    case 0x57: // I-MMU demap
3171
        demap_tlb(env->itlb, addr, "immu", env);
B
blueswir1 已提交
3172
        return;
B
bellard 已提交
3173
    case 0x58: // D-MMU regs
B
blueswir1 已提交
3174
        {
B
blueswir1 已提交
3175
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
3176
            uint64_t oldreg;
3177

B
blueswir1 已提交
3178
            oldreg = env->dmmuregs[reg];
B
bellard 已提交
3179 3180 3181 3182 3183
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
B
blueswir1 已提交
3184 3185
                if ((val & 1) == 0) {
                    val = 0; // Clear SFSR, Fault address
3186
                    env->dmmu.sfar = 0;
B
blueswir1 已提交
3187
                }
3188
                env->dmmu.sfsr = val;
B
bellard 已提交
3189 3190
                break;
            case 1: // Primary context
3191
                env->dmmu.mmu_primary_context = val;
3192 3193 3194
                /* can be optimized to only flush MMU_USER_IDX
                   and MMU_KERNEL_IDX entries */
                tlb_flush(env, 1);
3195
                break;
B
bellard 已提交
3196
            case 2: // Secondary context
3197
                env->dmmu.mmu_secondary_context = val;
3198 3199 3200
                /* can be optimized to only flush MMU_USER_SECONDARY_IDX
                   and MMU_KERNEL_SECONDARY_IDX entries */
                tlb_flush(env, 1);
3201
                break;
B
bellard 已提交
3202
            case 5: // TSB access
3203 3204 3205 3206
                DPRINTF_MMU("dmmu TSB write: 0x%016" PRIx64 " -> 0x%016"
                            PRIx64 "\n", env->dmmu.tsb, val);
                env->dmmu.tsb = val;
                break;
B
bellard 已提交
3207
            case 6: // Tag access
3208 3209
                env->dmmu.tag_access = val;
                break;
B
bellard 已提交
3210 3211 3212
            case 7: // Virtual Watchpoint
            case 8: // Physical Watchpoint
            default:
3213
                env->dmmuregs[reg] = val;
B
bellard 已提交
3214 3215
                break;
            }
3216

B
bellard 已提交
3217
            if (oldreg != env->dmmuregs[reg]) {
3218
                DPRINTF_MMU("dmmu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
B
blueswir1 已提交
3219
                            PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
B
bellard 已提交
3220
            }
3221
#ifdef DEBUG_MMU
3222
            dump_mmu(stdout, fprintf, env);
B
bellard 已提交
3223
#endif
B
blueswir1 已提交
3224 3225
            return;
        }
B
bellard 已提交
3226
    case 0x5c: // D-MMU data in
3227 3228
        replace_tlb_1bit_lru(env->dtlb, env->dmmu.tag_access, val, "dmmu", env);
        return;
B
bellard 已提交
3229
    case 0x5d: // D-MMU data access
B
blueswir1 已提交
3230
        {
B
blueswir1 已提交
3231
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
3232

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

3235
#ifdef DEBUG_MMU
3236
            DPRINTF_MMU("dmmu data access replaced entry [%i]\n", i);
3237
            dump_mmu(stdout, fprintf, env);
3238
#endif
B
blueswir1 已提交
3239 3240
            return;
        }
B
bellard 已提交
3241
    case 0x5f: // D-MMU demap
3242
        demap_tlb(env->dtlb, addr, "dmmu", env);
3243
        return;
B
bellard 已提交
3244
    case 0x49: // Interrupt data receive
B
blueswir1 已提交
3245 3246
        // XXX
        return;
3247 3248
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
3249 3250 3251
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
3252 3253 3254 3255 3256 3257 3258 3259
    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 已提交
3260 3261 3262 3263 3264 3265 3266
    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 已提交
3267 3268 3269 3270 3271 3272
    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 已提交
3273
    default:
3274
        do_unassigned_access(addr, 1, 0, 1, size);
B
blueswir1 已提交
3275
        return;
B
bellard 已提交
3276 3277
    }
}
3278
#endif /* CONFIG_USER_ONLY */
3279

B
blueswir1 已提交
3280 3281 3282
void helper_ldda_asi(target_ulong addr, int asi, int rd)
{
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
3283
        || (cpu_has_hypervisor(env)
3284
            && asi >= 0x30 && asi < 0x80
3285
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
3286 3287
        raise_exception(TT_PRIV_ACT);

3288 3289
    addr = asi_address_mask(env, asi, addr);

B
blueswir1 已提交
3290
    switch (asi) {
B
Blue Swirl 已提交
3291
#if !defined(CONFIG_USER_ONLY)
B
blueswir1 已提交
3292 3293 3294 3295
    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) {
3296
            env->gregs[1] = ldq_nucleus(addr + 8);
B
blueswir1 已提交
3297 3298 3299
            if (asi == 0x2c)
                bswap64s(&env->gregs[1]);
        } else if (rd < 8) {
3300 3301
            env->gregs[rd] = ldq_nucleus(addr);
            env->gregs[rd + 1] = ldq_nucleus(addr + 8);
B
blueswir1 已提交
3302 3303 3304 3305 3306
            if (asi == 0x2c) {
                bswap64s(&env->gregs[rd]);
                bswap64s(&env->gregs[rd + 1]);
            }
        } else {
3307 3308
            env->regwptr[rd] = ldq_nucleus(addr);
            env->regwptr[rd + 1] = ldq_nucleus(addr + 8);
B
blueswir1 已提交
3309 3310 3311 3312 3313 3314
            if (asi == 0x2c) {
                bswap64s(&env->regwptr[rd]);
                bswap64s(&env->regwptr[rd + 1]);
            }
        }
        break;
B
Blue Swirl 已提交
3315
#endif
B
blueswir1 已提交
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
    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 已提交
3331
void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
3332 3333
{
    unsigned int i;
3334
    CPU_DoubleU u;
3335

3336
    helper_check_align(addr, 3);
3337 3338
    addr = asi_address_mask(env, asi, addr);

3339 3340 3341 3342 3343
    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 已提交
3344 3345 3346 3347
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
3348
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
3349
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
3350 3351
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4,
                                                         0);
B
blueswir1 已提交
3352
            addr += 4;
3353 3354
        }

3355
        return;
3356 3357 3358 3359
    case 0x16: /* UA2007 Block load primary, user privilege */
    case 0x17: /* UA2007 Block load secondary, user privilege */
    case 0x1e: /* UA2007 Block load primary LE, user privilege */
    case 0x1f: /* UA2007 Block load secondary LE, user privilege */
3360 3361 3362 3363 3364 3365 3366 3367
    case 0x70: // Block load primary, user privilege
    case 0x71: // Block load secondary, user privilege
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
        helper_check_align(addr, 0x3f);
        for (i = 0; i < 16; i++) {
3368
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x19, 4,
3369 3370 3371 3372
                                                         0);
            addr += 4;
        }

3373 3374 3375 3376 3377 3378 3379 3380
        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
3381
        *((uint32_t *)&env->fpr[rd]) = helper_ld_asi(addr, asi, size, 0);
3382 3383
        break;
    case 8:
3384 3385 3386
        u.ll = helper_ld_asi(addr, asi, size, 0);
        *((uint32_t *)&env->fpr[rd++]) = u.l.upper;
        *((uint32_t *)&env->fpr[rd++]) = u.l.lower;
3387
        break;
B
blueswir1 已提交
3388
    case 16:
3389 3390 3391 3392 3393 3394
        u.ll = helper_ld_asi(addr, asi, 8, 0);
        *((uint32_t *)&env->fpr[rd++]) = u.l.upper;
        *((uint32_t *)&env->fpr[rd++]) = u.l.lower;
        u.ll = helper_ld_asi(addr + 8, asi, 8, 0);
        *((uint32_t *)&env->fpr[rd++]) = u.l.upper;
        *((uint32_t *)&env->fpr[rd++]) = u.l.lower;
B
blueswir1 已提交
3395
        break;
3396 3397 3398
    }
}

B
blueswir1 已提交
3399
void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
3400 3401
{
    unsigned int i;
B
blueswir1 已提交
3402
    target_ulong val = 0;
3403
    CPU_DoubleU u;
3404

3405
    helper_check_align(addr, 3);
3406 3407
    addr = asi_address_mask(env, asi, addr);

3408
    switch (asi) {
B
blueswir1 已提交
3409 3410
    case 0xe0: // UA2007 Block commit store primary (cache flush)
    case 0xe1: // UA2007 Block commit store secondary (cache flush)
3411 3412 3413 3414
    case 0xf0: // Block store primary
    case 0xf1: // Block store secondary
    case 0xf8: // Block store primary LE
    case 0xf9: // Block store secondary LE
B
blueswir1 已提交
3415 3416 3417 3418
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
3419
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
3420
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
3421 3422 3423
            val = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(addr, val, asi & 0x8f, 4);
            addr += 4;
3424 3425
        }

3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439
        return;
    case 0x70: // Block store primary, user privilege
    case 0x71: // Block store secondary, user privilege
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
        helper_check_align(addr, 0x3f);
        for (i = 0; i < 16; i++) {
            val = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(addr, val, asi & 0x1f, 4);
            addr += 4;
        }

3440 3441 3442 3443 3444 3445 3446 3447
        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
3448
        helper_st_asi(addr, *(uint32_t *)&env->fpr[rd], asi, size);
3449 3450
        break;
    case 8:
3451 3452 3453
        u.l.upper = *(uint32_t *)&env->fpr[rd++];
        u.l.lower = *(uint32_t *)&env->fpr[rd++];
        helper_st_asi(addr, u.ll, asi, size);
3454
        break;
B
blueswir1 已提交
3455
    case 16:
3456 3457 3458 3459 3460 3461
        u.l.upper = *(uint32_t *)&env->fpr[rd++];
        u.l.lower = *(uint32_t *)&env->fpr[rd++];
        helper_st_asi(addr, u.ll, asi, 8);
        u.l.upper = *(uint32_t *)&env->fpr[rd++];
        u.l.lower = *(uint32_t *)&env->fpr[rd++];
        helper_st_asi(addr + 8, u.ll, asi, 8);
B
blueswir1 已提交
3462
        break;
3463
    }
B
blueswir1 已提交
3464 3465 3466 3467 3468 3469 3470
}

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

3471
    val2 &= 0xffffffffUL;
B
blueswir1 已提交
3472 3473
    ret = helper_ld_asi(addr, asi, 4, 0);
    ret &= 0xffffffffUL;
3474 3475
    if (val2 == ret)
        helper_st_asi(addr, val1 & 0xffffffffUL, asi, 4);
B
blueswir1 已提交
3476
    return ret;
3477 3478
}

B
blueswir1 已提交
3479 3480 3481 3482 3483 3484
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);
3485 3486
    if (val2 == ret)
        helper_st_asi(addr, val1, asi, 8);
B
blueswir1 已提交
3487 3488
    return ret;
}
3489
#endif /* TARGET_SPARC64 */
B
bellard 已提交
3490 3491

#ifndef TARGET_SPARC64
B
blueswir1 已提交
3492
void helper_rett(void)
3493
{
3494 3495
    unsigned int cwp;

3496 3497 3498
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

3499
    env->psret = 1;
3500
    cwp = cwp_inc(env->cwp + 1) ;
3501 3502 3503 3504 3505 3506
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
3507
#endif
3508

3509
static target_ulong helper_udiv_common(target_ulong a, target_ulong b, int cc)
B
blueswir1 已提交
3510
{
3511
    int overflow = 0;
B
blueswir1 已提交
3512 3513 3514
    uint64_t x0;
    uint32_t x1;

3515
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
3516
    x1 = (b & 0xffffffff);
B
blueswir1 已提交
3517 3518 3519 3520 3521 3522 3523

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

    x0 = x0 / x1;
    if (x0 > 0xffffffff) {
3524 3525 3526 3527 3528 3529 3530 3531
        x0 = 0xffffffff;
        overflow = 1;
    }

    if (cc) {
        env->cc_dst = x0;
        env->cc_src2 = overflow;
        env->cc_op = CC_OP_DIV;
B
blueswir1 已提交
3532
    }
3533
    return x0;
B
blueswir1 已提交
3534 3535
}

3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546
target_ulong helper_udiv(target_ulong a, target_ulong b)
{
    return helper_udiv_common(a, b, 0);
}

target_ulong helper_udiv_cc(target_ulong a, target_ulong b)
{
    return helper_udiv_common(a, b, 1);
}

static target_ulong helper_sdiv_common(target_ulong a, target_ulong b, int cc)
B
blueswir1 已提交
3547
{
3548
    int overflow = 0;
B
blueswir1 已提交
3549 3550 3551
    int64_t x0;
    int32_t x1;

3552
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
3553
    x1 = (b & 0xffffffff);
B
blueswir1 已提交
3554 3555 3556 3557 3558 3559 3560

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

    x0 = x0 / x1;
    if ((int32_t) x0 != x0) {
3561 3562 3563 3564 3565 3566 3567 3568
        x0 = x0 < 0 ? 0x80000000: 0x7fffffff;
        overflow = 1;
    }

    if (cc) {
        env->cc_dst = x0;
        env->cc_src2 = overflow;
        env->cc_op = CC_OP_DIV;
B
blueswir1 已提交
3569
    }
3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580
    return x0;
}

target_ulong helper_sdiv(target_ulong a, target_ulong b)
{
    return helper_sdiv_common(a, b, 0);
}

target_ulong helper_sdiv_cc(target_ulong a, target_ulong b)
{
    return helper_sdiv_common(a, b, 1);
B
blueswir1 已提交
3581 3582
}

B
blueswir1 已提交
3583 3584
void helper_stdf(target_ulong addr, int mem_idx)
{
3585
    helper_check_align(addr, 7);
B
blueswir1 已提交
3586 3587
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
3588
    case MMU_USER_IDX:
3589
        stfq_user(addr, DT0);
B
blueswir1 已提交
3590
        break;
3591
    case MMU_KERNEL_IDX:
3592
        stfq_kernel(addr, DT0);
B
blueswir1 已提交
3593 3594
        break;
#ifdef TARGET_SPARC64
3595
    case MMU_HYPV_IDX:
3596
        stfq_hypv(addr, DT0);
B
blueswir1 已提交
3597 3598 3599
        break;
#endif
    default:
3600
        DPRINTF_MMU("helper_stdf: need to check MMU idx %d\n", mem_idx);
B
blueswir1 已提交
3601 3602 3603
        break;
    }
#else
3604
    stfq_raw(address_mask(env, addr), DT0);
B
blueswir1 已提交
3605 3606 3607 3608 3609
#endif
}

void helper_lddf(target_ulong addr, int mem_idx)
{
3610
    helper_check_align(addr, 7);
B
blueswir1 已提交
3611 3612
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
3613
    case MMU_USER_IDX:
3614
        DT0 = ldfq_user(addr);
B
blueswir1 已提交
3615
        break;
3616
    case MMU_KERNEL_IDX:
3617
        DT0 = ldfq_kernel(addr);
B
blueswir1 已提交
3618 3619
        break;
#ifdef TARGET_SPARC64
3620
    case MMU_HYPV_IDX:
3621
        DT0 = ldfq_hypv(addr);
B
blueswir1 已提交
3622 3623 3624
        break;
#endif
    default:
3625
        DPRINTF_MMU("helper_lddf: need to check MMU idx %d\n", mem_idx);
B
blueswir1 已提交
3626 3627 3628
        break;
    }
#else
3629
    DT0 = ldfq_raw(address_mask(env, addr));
B
blueswir1 已提交
3630 3631 3632
#endif
}

B
blueswir1 已提交
3633
void helper_ldqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
3634 3635 3636 3637
{
    // XXX add 128 bit load
    CPU_QuadU u;

3638
    helper_check_align(addr, 7);
B
blueswir1 已提交
3639 3640
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
3641
    case MMU_USER_IDX:
3642 3643
        u.ll.upper = ldq_user(addr);
        u.ll.lower = ldq_user(addr + 8);
B
blueswir1 已提交
3644 3645
        QT0 = u.q;
        break;
3646
    case MMU_KERNEL_IDX:
3647 3648
        u.ll.upper = ldq_kernel(addr);
        u.ll.lower = ldq_kernel(addr + 8);
B
blueswir1 已提交
3649 3650 3651
        QT0 = u.q;
        break;
#ifdef TARGET_SPARC64
3652
    case MMU_HYPV_IDX:
3653 3654
        u.ll.upper = ldq_hypv(addr);
        u.ll.lower = ldq_hypv(addr + 8);
B
blueswir1 已提交
3655 3656 3657 3658
        QT0 = u.q;
        break;
#endif
    default:
3659
        DPRINTF_MMU("helper_ldqf: need to check MMU idx %d\n", mem_idx);
B
blueswir1 已提交
3660 3661 3662
        break;
    }
#else
3663 3664
    u.ll.upper = ldq_raw(address_mask(env, addr));
    u.ll.lower = ldq_raw(address_mask(env, addr + 8));
B
blueswir1 已提交
3665
    QT0 = u.q;
B
blueswir1 已提交
3666
#endif
B
blueswir1 已提交
3667 3668
}

B
blueswir1 已提交
3669
void helper_stqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
3670 3671 3672 3673
{
    // XXX add 128 bit store
    CPU_QuadU u;

3674
    helper_check_align(addr, 7);
B
blueswir1 已提交
3675 3676
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
3677
    case MMU_USER_IDX:
B
blueswir1 已提交
3678
        u.q = QT0;
3679 3680
        stq_user(addr, u.ll.upper);
        stq_user(addr + 8, u.ll.lower);
B
blueswir1 已提交
3681
        break;
3682
    case MMU_KERNEL_IDX:
B
blueswir1 已提交
3683
        u.q = QT0;
3684 3685
        stq_kernel(addr, u.ll.upper);
        stq_kernel(addr + 8, u.ll.lower);
B
blueswir1 已提交
3686 3687
        break;
#ifdef TARGET_SPARC64
3688
    case MMU_HYPV_IDX:
B
blueswir1 已提交
3689
        u.q = QT0;
3690 3691
        stq_hypv(addr, u.ll.upper);
        stq_hypv(addr + 8, u.ll.lower);
B
blueswir1 已提交
3692 3693 3694
        break;
#endif
    default:
3695
        DPRINTF_MMU("helper_stqf: need to check MMU idx %d\n", mem_idx);
B
blueswir1 已提交
3696 3697 3698
        break;
    }
#else
B
blueswir1 已提交
3699
    u.q = QT0;
3700 3701
    stq_raw(address_mask(env, addr), u.ll.upper);
    stq_raw(address_mask(env, addr + 8), u.ll.lower);
B
blueswir1 已提交
3702
#endif
B
blueswir1 已提交
3703
}
B
blueswir1 已提交
3704

3705
static inline void set_fsr(void)
3706
{
B
bellard 已提交
3707
    int rnd_mode;
B
blueswir1 已提交
3708

3709 3710
    switch (env->fsr & FSR_RD_MASK) {
    case FSR_RD_NEAREST:
B
bellard 已提交
3711
        rnd_mode = float_round_nearest_even;
B
blueswir1 已提交
3712
        break;
B
bellard 已提交
3713
    default:
3714
    case FSR_RD_ZERO:
B
bellard 已提交
3715
        rnd_mode = float_round_to_zero;
B
blueswir1 已提交
3716
        break;
3717
    case FSR_RD_POS:
B
bellard 已提交
3718
        rnd_mode = float_round_up;
B
blueswir1 已提交
3719
        break;
3720
    case FSR_RD_NEG:
B
bellard 已提交
3721
        rnd_mode = float_round_down;
B
blueswir1 已提交
3722
        break;
3723
    }
B
bellard 已提交
3724
    set_float_rounding_mode(rnd_mode, &env->fp_status);
3725
}
B
bellard 已提交
3726

3727
void helper_ldfsr(uint32_t new_fsr)
B
blueswir1 已提交
3728
{
3729 3730
    env->fsr = (new_fsr & FSR_LDFSR_MASK) | (env->fsr & FSR_LDFSR_OLDMASK);
    set_fsr();
B
blueswir1 已提交
3731 3732
}

3733 3734 3735 3736 3737 3738 3739 3740
#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 已提交
3741
void helper_debug(void)
B
bellard 已提交
3742 3743
{
    env->exception_index = EXCP_DEBUG;
B
Blue Swirl 已提交
3744
    cpu_loop_exit(env);
B
bellard 已提交
3745
}
3746

B
bellard 已提交
3747
#ifndef TARGET_SPARC64
3748 3749 3750 3751 3752 3753
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

3754
    cwp = cwp_dec(env->cwp - 1);
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_OVF);
    }
    set_cwp(cwp);
}

void helper_restore(void)
{
    uint32_t cwp;

3765
    cwp = cwp_inc(env->cwp + 1);
3766 3767 3768 3769 3770 3771
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
}

B
blueswir1 已提交
3772
void helper_wrpsr(target_ulong new_psr)
3773
{
3774
    if ((new_psr & PSR_CWP) >= env->nwindows) {
3775
        raise_exception(TT_ILL_INSN);
3776 3777 3778
    } else {
        cpu_put_psr(env, new_psr);
    }
3779 3780
}

B
blueswir1 已提交
3781
target_ulong helper_rdpsr(void)
3782
{
3783
    return get_psr();
3784
}
B
bellard 已提交
3785 3786

#else
3787 3788 3789 3790 3791 3792
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

3793
    cwp = cwp_dec(env->cwp - 1);
3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
    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;

3814
    cwp = cwp_inc(env->cwp + 1);
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827
    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)
{
3828
    if (env->cansave != env->nwindows - 2) {
3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846
        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++;
3847
    if (env->cleanwin < env->nwindows - 1)
3848 3849 3850 3851 3852 3853 3854
        env->cleanwin++;
    if (env->otherwin == 0)
        env->cansave--;
    else
        env->otherwin--;
}

3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
static target_ulong get_ccr(void)
{
    target_ulong psr;

    psr = get_psr();

    return ((env->xcc >> 20) << 4) | ((psr & PSR_ICC) >> 20);
}

target_ulong cpu_get_ccr(CPUState *env1)
{
    CPUState *saved_env;
    target_ulong ret;

    saved_env = env;
    env = env1;
    ret = get_ccr();
    env = saved_env;
    return ret;
}

static void put_ccr(target_ulong val)
{
    target_ulong tmp = val;

    env->xcc = (tmp >> 4) << 20;
    env->psr = (tmp & 0xf) << 20;
    CC_OP = CC_OP_FLAGS;
}

void cpu_put_ccr(CPUState *env1, target_ulong val)
{
    CPUState *saved_env;

    saved_env = env;
    env = env1;
    put_ccr(val);
    env = saved_env;
}

static target_ulong get_cwp64(void)
{
    return env->nwindows - 1 - env->cwp;
}

target_ulong cpu_get_cwp64(CPUState *env1)
{
    CPUState *saved_env;
    target_ulong ret;

    saved_env = env;
    env = env1;
    ret = get_cwp64();
    env = saved_env;
    return ret;
}

static void put_cwp64(int cwp)
{
    if (unlikely(cwp >= env->nwindows || cwp < 0)) {
        cwp %= env->nwindows;
    }
    set_cwp(env->nwindows - 1 - cwp);
}

void cpu_put_cwp64(CPUState *env1, int cwp)
{
    CPUState *saved_env;

    saved_env = env;
    env = env1;
    put_cwp64(cwp);
    env = saved_env;
}

B
blueswir1 已提交
3930 3931
target_ulong helper_rdccr(void)
{
3932
    return get_ccr();
B
blueswir1 已提交
3933 3934 3935 3936
}

void helper_wrccr(target_ulong new_ccr)
{
3937
    put_ccr(new_ccr);
B
blueswir1 已提交
3938 3939 3940 3941 3942 3943
}

// CWP handling is reversed in V9, but we still use the V8 register
// order.
target_ulong helper_rdcwp(void)
{
3944
    return get_cwp64();
B
blueswir1 已提交
3945 3946 3947 3948
}

void helper_wrcwp(target_ulong new_cwp)
{
3949
    put_cwp64(new_cwp);
B
blueswir1 已提交
3950
}
B
bellard 已提交
3951

3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982
// 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 已提交
3983
target_ulong helper_popc(target_ulong val)
B
bellard 已提交
3984
{
B
blueswir1 已提交
3985
    return ctpop64(val);
B
bellard 已提交
3986
}
B
bellard 已提交
3987

3988
static inline uint64_t *get_gregset(uint32_t pstate)
B
bellard 已提交
3989 3990 3991
{
    switch (pstate) {
    default:
3992 3993 3994 3995 3996 3997
        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 已提交
3998
    case 0:
B
blueswir1 已提交
3999
        return env->bgregs;
B
bellard 已提交
4000
    case PS_AG:
B
blueswir1 已提交
4001
        return env->agregs;
B
bellard 已提交
4002
    case PS_MG:
B
blueswir1 已提交
4003
        return env->mgregs;
B
bellard 已提交
4004
    case PS_IG:
B
blueswir1 已提交
4005
        return env->igregs;
B
bellard 已提交
4006 4007 4008
    }
}

4009
static inline void change_pstate(uint32_t new_pstate)
B
bellard 已提交
4010
{
4011
    uint32_t pstate_regs, new_pstate_regs;
B
bellard 已提交
4012 4013
    uint64_t *src, *dst;

4014 4015 4016 4017 4018
    if (env->def->features & CPU_FEATURE_GL) {
        // PS_AG is not implemented in this case
        new_pstate &= ~PS_AG;
    }

B
bellard 已提交
4019 4020
    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
4021

B
bellard 已提交
4022
    if (new_pstate_regs != pstate_regs) {
4023 4024
        DPRINTF_PSTATE("change_pstate: switching regs old=%x new=%x\n",
                       pstate_regs, new_pstate_regs);
B
blueswir1 已提交
4025 4026 4027 4028 4029
        // 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 已提交
4030
    }
4031 4032 4033 4034
    else {
        DPRINTF_PSTATE("change_pstate: regs new=%x (unchanged)\n",
                       new_pstate_regs);
    }
B
bellard 已提交
4035 4036 4037
    env->pstate = new_pstate;
}

B
blueswir1 已提交
4038
void helper_wrpstate(target_ulong new_state)
4039
{
4040
    change_pstate(new_state & 0xf3f);
4041 4042 4043 4044 4045 4046

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

4049 4050 4051 4052 4053 4054 4055 4056 4057 4058
void cpu_change_pstate(CPUState *env1, uint32_t new_pstate)
{
    CPUState *saved_env;

    saved_env = env;
    env = env1;
    change_pstate(new_pstate);
    env = saved_env;
}

4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072
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 已提交
4073
void helper_done(void)
B
bellard 已提交
4074
{
4075 4076
    trap_state* tsptr = cpu_tsptr(env);

4077
    env->pc = tsptr->tnpc;
4078
    env->npc = tsptr->tnpc + 4;
4079
    put_ccr(tsptr->tstate >> 32);
4080 4081
    env->asi = (tsptr->tstate >> 24) & 0xff;
    change_pstate((tsptr->tstate >> 8) & 0xf3f);
4082
    put_cwp64(tsptr->tstate & 0xff);
B
blueswir1 已提交
4083
    env->tl--;
4084 4085 4086 4087 4088 4089 4090 4091

    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 已提交
4092 4093
}

B
blueswir1 已提交
4094
void helper_retry(void)
B
bellard 已提交
4095
{
4096 4097 4098 4099
    trap_state* tsptr = cpu_tsptr(env);

    env->pc = tsptr->tpc;
    env->npc = tsptr->tnpc;
4100
    put_ccr(tsptr->tstate >> 32);
4101 4102
    env->asi = (tsptr->tstate >> 24) & 0xff;
    change_pstate((tsptr->tstate >> 8) & 0xf3f);
4103
    put_cwp64(tsptr->tstate & 0xff);
B
blueswir1 已提交
4104
    env->tl--;
4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125

    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 已提交
4126
}
4127 4128 4129

void helper_set_softint(uint64_t value)
{
4130
    do_modify_softint("helper_set_softint", env->softint | (uint32_t)value);
4131 4132 4133 4134
}

void helper_clear_softint(uint64_t value)
{
4135
    do_modify_softint("helper_clear_softint", env->softint & (uint32_t)~value);
4136 4137 4138 4139
}

void helper_write_softint(uint64_t value)
{
4140
    do_modify_softint("helper_write_softint", (uint32_t)value);
4141
}
B
bellard 已提交
4142
#endif
4143

B
blueswir1 已提交
4144
#ifdef TARGET_SPARC64
4145 4146 4147 4148
trap_state* cpu_tsptr(CPUState* env)
{
    return &env->ts[env->tl & MAXTL_MASK];
}
B
blueswir1 已提交
4149
#endif
4150

4151
#if !defined(CONFIG_USER_ONLY)
4152

4153 4154 4155
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

4156
#define MMUSUFFIX _mmu
4157
#define ALIGNED_ONLY
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170

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

4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183
/* 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 */
4184
            cpu_restore_state(tb, env, pc);
4185 4186 4187 4188
        }
    }
}

4189 4190 4191
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr)
{
B
blueswir1 已提交
4192
#ifdef DEBUG_UNALIGNED
4193 4194
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
           "\n", addr, env->pc);
B
blueswir1 已提交
4195
#endif
4196
    cpu_restore_state2(retaddr);
B
blueswir1 已提交
4197
    raise_exception(TT_UNALIGNED);
4198
}
4199 4200 4201 4202 4203

/* 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 */
4204
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
4205 4206 4207 4208 4209 4210 4211 4212 4213
{
    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;

4214
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
4215
    if (ret) {
4216
        cpu_restore_state2(retaddr);
B
Blue Swirl 已提交
4217
        cpu_loop_exit(env);
4218 4219 4220 4221
    }
    env = saved_env;
}

P
Paul Brook 已提交
4222
#endif /* !CONFIG_USER_ONLY */
4223 4224

#ifndef TARGET_SPARC64
P
Paul Brook 已提交
4225
#if !defined(CONFIG_USER_ONLY)
A
Anthony Liguori 已提交
4226
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
4227
                          int is_asi, int size)
4228 4229
{
    CPUState *saved_env;
4230
    int fault_type;
4231 4232 4233 4234 4235

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;
4236 4237
#ifdef DEBUG_UNASSIGNED
    if (is_asi)
4238
        printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
B
blueswir1 已提交
4239
               " asi 0x%02x from " TARGET_FMT_lx "\n",
4240 4241
               is_exec ? "exec" : is_write ? "write" : "read", size,
               size == 1 ? "" : "s", addr, is_asi, env->pc);
4242
    else
4243 4244 4245 4246
        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);
4247
#endif
4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
    /* 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;
    }

4271
    if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
4272 4273 4274 4275
        if (is_exec)
            raise_exception(TT_CODE_ACCESS);
        else
            raise_exception(TT_DATA_ACCESS);
4276
    }
4277 4278 4279 4280 4281 4282

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

    env = saved_env;
4285
}
P
Paul Brook 已提交
4286 4287 4288 4289 4290
#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)
4291
#else
A
Anthony Liguori 已提交
4292
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
4293
                          int is_asi, int size)
P
Paul Brook 已提交
4294
#endif
4295 4296 4297 4298 4299 4300 4301
{
    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;
4302 4303

#ifdef DEBUG_UNASSIGNED
B
blueswir1 已提交
4304 4305
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
           "\n", addr, env->pc);
4306
#endif
4307

4308 4309 4310 4311
    if (is_exec)
        raise_exception(TT_CODE_ACCESS);
    else
        raise_exception(TT_DATA_ACCESS);
4312 4313

    env = saved_env;
4314 4315
}
#endif
4316

P
Paul Brook 已提交
4317

B
blueswir1 已提交
4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341
#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