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

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

15 16 17 18
#ifdef DEBUG_MMU
#define DPRINTF_MMU(fmt, args...) \
do { printf("MMU: " fmt , ##args); } while (0)
#else
B
blueswir1 已提交
19
#define DPRINTF_MMU(fmt, args...) do {} while (0)
20 21 22 23 24 25
#endif

#ifdef DEBUG_MXCC
#define DPRINTF_MXCC(fmt, args...) \
do { printf("MXCC: " fmt , ##args); } while (0)
#else
B
blueswir1 已提交
26
#define DPRINTF_MXCC(fmt, args...) do {} while (0)
27 28
#endif

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

B
blueswir1 已提交
34 35 36
#ifdef TARGET_SPARC64
#ifndef TARGET_ABI32
#define AM_CHECK(env1) ((env1)->pstate & PS_AM)
37
#else
B
blueswir1 已提交
38 39
#define AM_CHECK(env1) (1)
#endif
40 41
#endif

42
#if defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
// 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;
    int tsb_split = (env->dmmuregs[5] & 0x1000ULL) ? 1 : 0;
    int tsb_size  = env->dmmuregs[5] & 0xf;

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

#endif

B
blueswir1 已提交
92 93 94 95 96 97 98 99
static inline void address_mask(CPUState *env1, target_ulong *addr)
{
#ifdef TARGET_SPARC64
    if (AM_CHECK(env1))
        *addr &= 0xffffffffULL;
#endif
}

B
blueswir1 已提交
100
static void raise_exception(int tt)
B
bellard 已提交
101 102 103
{
    env->exception_index = tt;
    cpu_loop_exit();
104
}
B
bellard 已提交
105

P
pbrook 已提交
106 107 108 109 110
void HELPER(raise_exception)(int tt)
{
    raise_exception(tt);
}

B
blueswir1 已提交
111 112 113 114 115
static inline void set_cwp(int new_cwp)
{
    cpu_set_cwp(env, new_cwp);
}

B
blueswir1 已提交
116 117
void helper_check_align(target_ulong addr, uint32_t align)
{
118 119 120 121 122
    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 已提交
123
        raise_exception(TT_UNALIGNED);
124
    }
B
blueswir1 已提交
125 126
}

127 128 129
#define F_HELPER(name, p) void helper_f##name##p(void)

#define F_BINOP(name)                                           \
B
blueswir1 已提交
130
    float32 helper_f ## name ## s (float32 src1, float32 src2)  \
131
    {                                                           \
B
blueswir1 已提交
132
        return float32_ ## name (src1, src2, &env->fp_status);  \
133 134 135 136
    }                                                           \
    F_HELPER(name, d)                                           \
    {                                                           \
        DT0 = float64_ ## name (DT0, DT1, &env->fp_status);     \
B
blueswir1 已提交
137 138 139 140
    }                                                           \
    F_HELPER(name, q)                                           \
    {                                                           \
        QT0 = float128_ ## name (QT0, QT1, &env->fp_status);    \
141 142 143 144 145 146 147 148
    }

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

149
void helper_fsmuld(float32 src1, float32 src2)
B
blueswir1 已提交
150
{
151 152
    DT0 = float64_mul(float32_to_float64(src1, &env->fp_status),
                      float32_to_float64(src2, &env->fp_status),
153 154
                      &env->fp_status);
}
B
blueswir1 已提交
155

B
blueswir1 已提交
156 157 158 159 160 161 162
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 已提交
163
float32 helper_fnegs(float32 src)
164
{
B
blueswir1 已提交
165
    return float32_chs(src);
166 167
}

168 169
#ifdef TARGET_SPARC64
F_HELPER(neg, d)
170
{
171
    DT0 = float64_chs(DT1);
172
}
B
blueswir1 已提交
173 174 175 176 177 178

F_HELPER(neg, q)
{
    QT0 = float128_chs(QT1);
}
#endif
179 180

/* Integer to float conversion.  */
B
blueswir1 已提交
181
float32 helper_fitos(int32_t src)
B
bellard 已提交
182
{
B
blueswir1 已提交
183
    return int32_to_float32(src, &env->fp_status);
B
bellard 已提交
184 185
}

186
void helper_fitod(int32_t src)
B
bellard 已提交
187
{
188
    DT0 = int32_to_float64(src, &env->fp_status);
B
bellard 已提交
189
}
190

191
void helper_fitoq(int32_t src)
B
blueswir1 已提交
192
{
193
    QT0 = int32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
194 195
}

B
blueswir1 已提交
196
#ifdef TARGET_SPARC64
197
float32 helper_fxtos(void)
B
blueswir1 已提交
198
{
199
    return int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
B
blueswir1 已提交
200 201
}

202
F_HELPER(xto, d)
B
blueswir1 已提交
203 204 205
{
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
}
B
blueswir1 已提交
206

B
blueswir1 已提交
207 208 209 210 211
F_HELPER(xto, q)
{
    QT0 = int64_to_float128(*((int64_t *)&DT1), &env->fp_status);
}
#endif
212 213 214
#undef F_HELPER

/* floating point conversion */
215
float32 helper_fdtos(void)
216
{
217
    return float64_to_float32(DT1, &env->fp_status);
218 219
}

220
void helper_fstod(float32 src)
221
{
222
    DT0 = float32_to_float64(src, &env->fp_status);
223
}
224

225
float32 helper_fqtos(void)
B
blueswir1 已提交
226
{
227
    return float128_to_float32(QT1, &env->fp_status);
B
blueswir1 已提交
228 229
}

230
void helper_fstoq(float32 src)
B
blueswir1 已提交
231
{
232
    QT0 = float32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
233 234 235 236 237 238 239 240 241 242 243 244
}

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

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

245
/* Float to integer conversion.  */
B
blueswir1 已提交
246
int32_t helper_fstoi(float32 src)
247
{
B
blueswir1 已提交
248
    return float32_to_int32_round_to_zero(src, &env->fp_status);
249 250
}

251
int32_t helper_fdtoi(void)
252
{
253
    return float64_to_int32_round_to_zero(DT1, &env->fp_status);
254 255
}

256
int32_t helper_fqtoi(void)
B
blueswir1 已提交
257
{
258
    return float128_to_int32_round_to_zero(QT1, &env->fp_status);
B
blueswir1 已提交
259 260
}

261
#ifdef TARGET_SPARC64
262
void helper_fstox(float32 src)
263
{
264
    *((int64_t *)&DT0) = float32_to_int64_round_to_zero(src, &env->fp_status);
265 266 267 268 269 270 271
}

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

B
blueswir1 已提交
272 273 274 275 276
void helper_fqtox(void)
{
    *((int64_t *)&DT0) = float128_to_int64_round_to_zero(QT1, &env->fp_status);
}

277 278 279 280 281
void helper_faligndata(void)
{
    uint64_t tmp;

    tmp = (*((uint64_t *)&DT0)) << ((env->gsr & 7) * 8);
B
blueswir1 已提交
282 283 284 285
    /* on many architectures a shift of 64 does nothing */
    if ((env->gsr & 7) != 0) {
        tmp |= (*((uint64_t *)&DT1)) >> (64 - (env->gsr & 7) * 8);
    }
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    *((uint64_t *)&DT0) = tmp;
}

#ifdef WORDS_BIGENDIAN
#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;
506 507 508 509
    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;
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

    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 已提交
530
    uint32_t name##16s(uint32_t src1, uint32_t src2)    \
531 532 533
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
534 535
        s.l = src1;                                     \
        d.l = src2;                                     \
536 537 538 539
                                                        \
        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 已提交
540
        return d.l;                                     \
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    }                                                   \
                                                        \
    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 已提交
556
    uint32_t name##32s(uint32_t src1, uint32_t src2)    \
557 558 559
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
560 561
        s.l = src1;                                     \
        d.l = src2;                                     \
562 563 564
                                                        \
        d.l = F(d.l, s.l);                              \
                                                        \
B
blueswir1 已提交
565
        return d.l;                                     \
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
    }

#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 已提交
647
float32 helper_fabss(float32 src)
648
{
B
blueswir1 已提交
649
    return float32_abs(src);
650 651
}

B
bellard 已提交
652
#ifdef TARGET_SPARC64
653
void helper_fabsd(void)
B
bellard 已提交
654 655 656
{
    DT0 = float64_abs(DT1);
}
B
blueswir1 已提交
657 658 659 660 661 662

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

B
blueswir1 已提交
664
float32 helper_fsqrts(float32 src)
665
{
B
blueswir1 已提交
666
    return float32_sqrt(src, &env->fp_status);
667 668
}

669
void helper_fsqrtd(void)
670
{
B
bellard 已提交
671
    DT0 = float64_sqrt(DT1, &env->fp_status);
672 673
}

B
blueswir1 已提交
674 675 676 677 678
void helper_fsqrtq(void)
{
    QT0 = float128_sqrt(QT1, &env->fp_status);
}

679
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
680
    void glue(helper_, name) (void)                                     \
B
bellard 已提交
681
    {                                                                   \
B
blueswir1 已提交
682 683
        target_ulong new_fsr;                                           \
                                                                        \
B
bellard 已提交
684 685 686
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
B
blueswir1 已提交
687
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
688
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
B
blueswir1 已提交
689
                env->fsr |= new_fsr;                                    \
690 691
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
B
bellard 已提交
692 693 694 695 696 697
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
B
blueswir1 已提交
698
            new_fsr = FSR_FCC0 << FS;                                   \
B
bellard 已提交
699 700
            break;                                                      \
        case float_relation_greater:                                    \
B
blueswir1 已提交
701
            new_fsr = FSR_FCC1 << FS;                                   \
B
bellard 已提交
702 703
            break;                                                      \
        default:                                                        \
B
blueswir1 已提交
704
            new_fsr = 0;                                                \
B
bellard 已提交
705 706
            break;                                                      \
        }                                                               \
B
blueswir1 已提交
707
        env->fsr |= new_fsr;                                            \
708
    }
B
blueswir1 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
#define GEN_FCMPS(name, size, FS, TRAP)                                 \
    void glue(helper_, name)(float32 src1, float32 src2)                \
    {                                                                   \
        target_ulong new_fsr;                                           \
                                                                        \
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (src1, src2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
                env->fsr |= new_fsr;                                    \
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
            new_fsr = FSR_FCC0 << FS;                                   \
            break;                                                      \
        case float_relation_greater:                                    \
            new_fsr = FSR_FCC1 << FS;                                   \
            break;                                                      \
        default:                                                        \
            new_fsr = 0;                                                \
            break;                                                      \
        }                                                               \
        env->fsr |= new_fsr;                                            \
    }
739

B
blueswir1 已提交
740
GEN_FCMPS(fcmps, float32, 0, 0);
741 742
GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);

B
blueswir1 已提交
743
GEN_FCMPS(fcmpes, float32, 0, 1);
744
GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
B
bellard 已提交
745

B
blueswir1 已提交
746 747 748
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);

749 750 751 752 753 754 755 756 757 758
static uint32_t compute_all_flags(void)
{
    return env->psr & PSR_ICC;
}

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

B
Blue Swirl 已提交
759 760 761 762 763 764 765 766 767 768 769
static inline uint32_t get_NZ_icc(target_ulong dst)
{
    uint32_t ret = 0;

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

770 771 772 773 774 775 776 777 778 779 780
#ifdef TARGET_SPARC64
static uint32_t compute_all_flags_xcc(void)
{
    return env->xcc & PSR_ICC;
}

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

B
Blue Swirl 已提交
781 782 783 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
static inline uint32_t get_NZ_xcc(target_ulong dst)
{
    uint32_t ret = 0;

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

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

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

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

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

static uint32_t compute_all_add(void)
{
    uint32_t ret;

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

static uint32_t compute_C_add(void)
{
    return get_C_add_icc(CC_DST, CC_SRC);
}

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

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

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

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

static uint32_t compute_all_add_xcc(void)
{
    uint32_t ret;

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

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

B
Blue Swirl 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
static uint32_t compute_all_addx(void)
{
    uint32_t ret;

    ret = get_NZ_icc(CC_DST);
    ret |= get_C_add_icc(CC_DST - CC_SRC2, CC_SRC);
    ret |= get_C_add_icc(CC_DST, CC_SRC);
    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_add_icc(CC_DST - CC_SRC2, CC_SRC);
    ret |= get_C_add_icc(CC_DST, CC_SRC);
    return ret;
}

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

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

static uint32_t compute_C_addx_xcc(void)
{
    uint32_t ret;

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

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
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

922 923 924 925 926 927 928 929
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 已提交
930
    [CC_OP_ADD] = { compute_all_add, compute_C_add },
B
Blue Swirl 已提交
931
    [CC_OP_ADDX] = { compute_all_addx, compute_C_addx },
932
    [CC_OP_LOGIC] = { compute_all_logic, compute_C_logic },
933 934 935 936 937 938
};

#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 已提交
939
    [CC_OP_ADD] = { compute_all_add_xcc, compute_C_add_xcc },
B
Blue Swirl 已提交
940
    [CC_OP_ADDX] = { compute_all_addx_xcc, compute_C_addx_xcc },
941
    [CC_OP_LOGIC] = { compute_all_logic_xcc, compute_C_logic },
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
};
#endif

void helper_compute_psr(void)
{
    uint32_t new_psr;

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

uint32_t helper_compute_C_icc(void)
{
    uint32_t ret;

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

B
bellard 已提交
966
#ifdef TARGET_SPARC64
B
blueswir1 已提交
967
GEN_FCMPS(fcmps_fcc1, float32, 22, 0);
968
GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
B
blueswir1 已提交
969
GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
970

B
blueswir1 已提交
971
GEN_FCMPS(fcmps_fcc2, float32, 24, 0);
972
GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
B
blueswir1 已提交
973
GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
974

B
blueswir1 已提交
975
GEN_FCMPS(fcmps_fcc3, float32, 26, 0);
976
GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
B
blueswir1 已提交
977
GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
978

B
blueswir1 已提交
979
GEN_FCMPS(fcmpes_fcc1, float32, 22, 1);
980
GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
B
blueswir1 已提交
981
GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
B
bellard 已提交
982

B
blueswir1 已提交
983
GEN_FCMPS(fcmpes_fcc2, float32, 24, 1);
984
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
B
blueswir1 已提交
985
GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
B
bellard 已提交
986

B
blueswir1 已提交
987
GEN_FCMPS(fcmpes_fcc3, float32, 26, 1);
988
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
B
blueswir1 已提交
989 990
GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
#endif
B
blueswir1 已提交
991
#undef GEN_FCMPS
B
bellard 已提交
992

B
blueswir1 已提交
993 994
#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
    defined(DEBUG_MXCC)
995 996 997
static void dump_mxcc(CPUState *env)
{
    printf("mxccdata: %016llx %016llx %016llx %016llx\n",
B
blueswir1 已提交
998 999
           env->mxccdata[0], env->mxccdata[1],
           env->mxccdata[2], env->mxccdata[3]);
1000 1001
    printf("mxccregs: %016llx %016llx %016llx %016llx\n"
           "          %016llx %016llx %016llx %016llx\n",
B
blueswir1 已提交
1002 1003 1004 1005
           env->mxccregs[0], env->mxccregs[1],
           env->mxccregs[2], env->mxccregs[3],
           env->mxccregs[4], env->mxccregs[5],
           env->mxccregs[6], env->mxccregs[7]);
1006 1007 1008
}
#endif

B
blueswir1 已提交
1009 1010 1011 1012
#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)
1013 1014 1015 1016
{
    switch (size)
    {
    case 1:
B
blueswir1 已提交
1017 1018
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xff);
1019 1020
        break;
    case 2:
B
blueswir1 已提交
1021 1022
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffff);
1023 1024
        break;
    case 4:
B
blueswir1 已提交
1025 1026
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffffffff);
1027 1028
        break;
    case 8:
B
blueswir1 已提交
1029 1030
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
                    addr, asi, r1);
1031 1032 1033 1034 1035
        break;
    }
}
#endif

B
blueswir1 已提交
1036 1037 1038
#ifndef TARGET_SPARC64
#ifndef CONFIG_USER_ONLY
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1039
{
B
blueswir1 已提交
1040
    uint64_t ret = 0;
1041
#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
B
blueswir1 已提交
1042
    uint32_t last_addr = addr;
1043
#endif
B
bellard 已提交
1044

1045
    helper_check_align(addr, size - 1);
B
bellard 已提交
1046
    switch (asi) {
1047
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
1048
        switch (addr) {
1049
        case 0x01c00a00: /* MXCC control register */
B
blueswir1 已提交
1050 1051 1052
            if (size == 8)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1053 1054
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1055 1056 1057 1058 1059
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
1060 1061
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1062
            break;
1063 1064
        case 0x01c00c00: /* Module reset register */
            if (size == 8) {
B
blueswir1 已提交
1065
                ret = env->mxccregs[5];
1066 1067
                // should we do something here?
            } else
B
blueswir1 已提交
1068 1069
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1070
            break;
1071
        case 0x01c00f00: /* MBus port address register */
B
blueswir1 已提交
1072 1073 1074
            if (size == 8)
                ret = env->mxccregs[7];
            else
B
blueswir1 已提交
1075 1076
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1077 1078
            break;
        default:
B
blueswir1 已提交
1079 1080
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1081 1082
            break;
        }
B
blueswir1 已提交
1083
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
1084
                     "addr = %08x -> ret = %" PRIx64 ","
B
blueswir1 已提交
1085
                     "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
1086 1087 1088
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1089
        break;
1090
    case 3: /* MMU probe */
B
blueswir1 已提交
1091 1092 1093
        {
            int mmulev;

B
blueswir1 已提交
1094
            mmulev = (addr >> 8) & 15;
B
blueswir1 已提交
1095 1096
            if (mmulev > 4)
                ret = 0;
B
blueswir1 已提交
1097 1098 1099 1100
            else
                ret = mmu_probe(env, addr, mmulev);
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
                        addr, mmulev, ret);
B
blueswir1 已提交
1101 1102
        }
        break;
1103
    case 4: /* read MMU regs */
B
blueswir1 已提交
1104
        {
B
blueswir1 已提交
1105
            int reg = (addr >> 8) & 0x1f;
1106

B
blueswir1 已提交
1107 1108
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
B
blueswir1 已提交
1109 1110 1111 1112 1113
                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 已提交
1114
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
B
blueswir1 已提交
1115 1116
        }
        break;
B
blueswir1 已提交
1117 1118 1119 1120
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1121 1122 1123
    case 9: /* Supervisor code access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1124
            ret = ldub_code(addr);
1125 1126
            break;
        case 2:
1127
            ret = lduw_code(addr);
1128 1129 1130
            break;
        default:
        case 4:
1131
            ret = ldl_code(addr);
1132 1133
            break;
        case 8:
1134
            ret = ldq_code(addr);
1135 1136 1137
            break;
        }
        break;
1138 1139 1140
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1141
            ret = ldub_user(addr);
1142 1143
            break;
        case 2:
1144
            ret = lduw_user(addr);
1145 1146 1147
            break;
        default:
        case 4:
1148
            ret = ldl_user(addr);
1149 1150
            break;
        case 8:
1151
            ret = ldq_user(addr);
1152 1153 1154 1155 1156 1157
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1158
            ret = ldub_kernel(addr);
1159 1160
            break;
        case 2:
1161
            ret = lduw_kernel(addr);
1162 1163 1164
            break;
        default:
        case 4:
1165
            ret = ldl_kernel(addr);
1166 1167
            break;
        case 8:
1168
            ret = ldq_kernel(addr);
1169 1170 1171
            break;
        }
        break;
1172 1173 1174 1175 1176 1177
    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 已提交
1178 1179
        switch(size) {
        case 1:
B
blueswir1 已提交
1180
            ret = ldub_phys(addr);
B
bellard 已提交
1181 1182
            break;
        case 2:
1183
            ret = lduw_phys(addr);
B
bellard 已提交
1184 1185 1186
            break;
        default:
        case 4:
1187
            ret = ldl_phys(addr);
B
bellard 已提交
1188
            break;
B
bellard 已提交
1189
        case 8:
1190
            ret = ldq_phys(addr);
B
blueswir1 已提交
1191
            break;
B
bellard 已提交
1192
        }
B
blueswir1 已提交
1193
        break;
1194
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1195 1196
        switch(size) {
        case 1:
B
blueswir1 已提交
1197
            ret = ldub_phys((target_phys_addr_t)addr
1198 1199 1200
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 2:
1201
            ret = lduw_phys((target_phys_addr_t)addr
1202 1203 1204 1205
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        default:
        case 4:
1206
            ret = ldl_phys((target_phys_addr_t)addr
1207 1208 1209
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 8:
1210
            ret = ldq_phys((target_phys_addr_t)addr
1211
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
B
blueswir1 已提交
1212
            break;
1213
        }
B
blueswir1 已提交
1214
        break;
B
blueswir1 已提交
1215 1216 1217
    case 0x30: // Turbosparc secondary cache diagnostic
    case 0x31: // Turbosparc RAM snoop
    case 0x32: // Turbosparc page table descriptor diagnostic
B
blueswir1 已提交
1218 1219 1220
    case 0x39: /* data cache diagnostic register */
        ret = 0;
        break;
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    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;
            }
            DPRINTF_MMU("read breakpoint reg[%d] 0x%016llx\n", reg, ret);
        }
        break;
B
blueswir1 已提交
1243
    case 8: /* User code access, XXX */
1244
    default:
1245
        do_unassigned_access(addr, 0, 0, asi, size);
B
blueswir1 已提交
1246 1247
        ret = 0;
        break;
1248
    }
1249 1250 1251
    if (sign) {
        switch(size) {
        case 1:
B
blueswir1 已提交
1252
            ret = (int8_t) ret;
B
blueswir1 已提交
1253
            break;
1254
        case 2:
B
blueswir1 已提交
1255 1256 1257 1258
            ret = (int16_t) ret;
            break;
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1259
            break;
1260 1261 1262 1263
        default:
            break;
        }
    }
1264
#ifdef DEBUG_ASI
B
blueswir1 已提交
1265
    dump_asi("read ", last_addr, asi, size, ret);
1266
#endif
B
blueswir1 已提交
1267
    return ret;
1268 1269
}

B
blueswir1 已提交
1270
void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
1271
{
1272
    helper_check_align(addr, size - 1);
1273
    switch(asi) {
1274
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
1275
        switch (addr) {
1276 1277
        case 0x01c00000: /* MXCC stream data register 0 */
            if (size == 8)
B
blueswir1 已提交
1278
                env->mxccdata[0] = val;
1279
            else
B
blueswir1 已提交
1280 1281
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1282 1283 1284
            break;
        case 0x01c00008: /* MXCC stream data register 1 */
            if (size == 8)
B
blueswir1 已提交
1285
                env->mxccdata[1] = val;
1286
            else
B
blueswir1 已提交
1287 1288
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1289 1290 1291
            break;
        case 0x01c00010: /* MXCC stream data register 2 */
            if (size == 8)
B
blueswir1 已提交
1292
                env->mxccdata[2] = val;
1293
            else
B
blueswir1 已提交
1294 1295
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1296 1297 1298
            break;
        case 0x01c00018: /* MXCC stream data register 3 */
            if (size == 8)
B
blueswir1 已提交
1299
                env->mxccdata[3] = val;
1300
            else
B
blueswir1 已提交
1301 1302
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1303 1304 1305
            break;
        case 0x01c00100: /* MXCC stream source */
            if (size == 8)
B
blueswir1 已提交
1306
                env->mxccregs[0] = val;
1307
            else
B
blueswir1 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
                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);
1318 1319 1320
            break;
        case 0x01c00200: /* MXCC stream destination */
            if (size == 8)
B
blueswir1 已提交
1321
                env->mxccregs[1] = val;
1322
            else
B
blueswir1 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
                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]);
1333 1334 1335
            break;
        case 0x01c00a00: /* MXCC control register */
            if (size == 8)
B
blueswir1 已提交
1336
                env->mxccregs[3] = val;
1337
            else
B
blueswir1 已提交
1338 1339
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1340 1341 1342
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
1343
                env->mxccregs[3] = (env->mxccregs[3] & 0xffffffff00000000ULL)
B
blueswir1 已提交
1344
                    | val;
1345
            else
B
blueswir1 已提交
1346 1347
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1348 1349
            break;
        case 0x01c00e00: /* MXCC error register  */
1350
            // writing a 1 bit clears the error
1351
            if (size == 8)
B
blueswir1 已提交
1352
                env->mxccregs[6] &= ~val;
1353
            else
B
blueswir1 已提交
1354 1355
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1356 1357 1358
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8)
B
blueswir1 已提交
1359
                env->mxccregs[7] = val;
1360
            else
B
blueswir1 已提交
1361 1362
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1363 1364
            break;
        default:
B
blueswir1 已提交
1365 1366
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1367 1368
            break;
        }
1369 1370
        DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %" PRIx64 "\n",
                     asi, size, addr, val);
1371 1372 1373
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1374
        break;
1375
    case 3: /* MMU flush */
B
blueswir1 已提交
1376 1377
        {
            int mmulev;
B
bellard 已提交
1378

B
blueswir1 已提交
1379
            mmulev = (addr >> 8) & 15;
1380
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
B
blueswir1 已提交
1381 1382
            switch (mmulev) {
            case 0: // flush page
B
blueswir1 已提交
1383
                tlb_flush_page(env, addr & 0xfffff000);
B
blueswir1 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
                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 已提交
1394
#ifdef DEBUG_MMU
B
blueswir1 已提交
1395
            dump_mmu(env);
B
bellard 已提交
1396
#endif
B
blueswir1 已提交
1397
        }
1398
        break;
1399
    case 4: /* write MMU regs */
B
blueswir1 已提交
1400
        {
B
blueswir1 已提交
1401
            int reg = (addr >> 8) & 0x1f;
B
blueswir1 已提交
1402
            uint32_t oldreg;
1403

B
blueswir1 已提交
1404
            oldreg = env->mmuregs[reg];
B
bellard 已提交
1405
            switch(reg) {
1406
            case 0: // Control Register
B
blueswir1 已提交
1407
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
B
blueswir1 已提交
1408
                                    (val & 0x00ffffff);
B
blueswir1 已提交
1409 1410
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
1411 1412
                if ((oldreg & (MMU_E | MMU_NF | env->def->mmu_bm)) !=
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->def->mmu_bm)))
B
bellard 已提交
1413 1414
                    tlb_flush(env, 1);
                break;
1415
            case 1: // Context Table Pointer Register
1416
                env->mmuregs[reg] = val & env->def->mmu_ctpr_mask;
1417 1418
                break;
            case 2: // Context Register
1419
                env->mmuregs[reg] = val & env->def->mmu_cxr_mask;
B
bellard 已提交
1420 1421 1422 1423 1424 1425
                if (oldreg != env->mmuregs[reg]) {
                    /* we flush when the MMU context changes because
                       QEMU has no MMU context support */
                    tlb_flush(env, 1);
                }
                break;
1426 1427 1428 1429
            case 3: // Synchronous Fault Status Register with Clear
            case 4: // Synchronous Fault Address Register
                break;
            case 0x10: // TLB Replacement Control Register
1430
                env->mmuregs[reg] = val & env->def->mmu_trcr_mask;
B
bellard 已提交
1431
                break;
1432
            case 0x13: // Synchronous Fault Status Register with Read and Clear
1433
                env->mmuregs[3] = val & env->def->mmu_sfsr_mask;
B
blueswir1 已提交
1434
                break;
1435
            case 0x14: // Synchronous Fault Address Register
B
blueswir1 已提交
1436
                env->mmuregs[4] = val;
B
blueswir1 已提交
1437
                break;
B
bellard 已提交
1438
            default:
B
blueswir1 已提交
1439
                env->mmuregs[reg] = val;
B
bellard 已提交
1440 1441 1442
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
B
blueswir1 已提交
1443 1444
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
                            reg, oldreg, env->mmuregs[reg]);
B
bellard 已提交
1445
            }
1446
#ifdef DEBUG_MMU
B
blueswir1 已提交
1447
            dump_mmu(env);
B
bellard 已提交
1448
#endif
B
blueswir1 已提交
1449
        }
1450
        break;
B
blueswir1 已提交
1451 1452 1453 1454
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1455 1456 1457
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1458
            stb_user(addr, val);
1459 1460
            break;
        case 2:
1461
            stw_user(addr, val);
1462 1463 1464
            break;
        default:
        case 4:
1465
            stl_user(addr, val);
1466 1467
            break;
        case 8:
1468
            stq_user(addr, val);
1469 1470 1471 1472 1473 1474
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1475
            stb_kernel(addr, val);
1476 1477
            break;
        case 2:
1478
            stw_kernel(addr, val);
1479 1480 1481
            break;
        default:
        case 4:
1482
            stl_kernel(addr, val);
1483 1484
            break;
        case 8:
1485
            stq_kernel(addr, val);
1486 1487 1488
            break;
        }
        break;
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    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 已提交
1499
    case 0x17: /* Block copy, sta access */
B
blueswir1 已提交
1500
        {
B
blueswir1 已提交
1501 1502
            // val = src
            // addr = dst
B
blueswir1 已提交
1503
            // copy 32 bytes
1504
            unsigned int i;
B
blueswir1 已提交
1505
            uint32_t src = val & ~3, dst = addr & ~3, temp;
1506

1507 1508 1509 1510
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
B
blueswir1 已提交
1511
        }
1512
        break;
B
bellard 已提交
1513
    case 0x1f: /* Block fill, stda access */
B
blueswir1 已提交
1514
        {
B
blueswir1 已提交
1515 1516
            // addr = dst
            // fill 32 bytes with val
1517
            unsigned int i;
B
blueswir1 已提交
1518
            uint32_t dst = addr & 7;
1519 1520 1521

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
1522
        }
1523
        break;
1524
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
1525
        {
B
bellard 已提交
1526 1527
            switch(size) {
            case 1:
B
blueswir1 已提交
1528
                stb_phys(addr, val);
B
bellard 已提交
1529 1530
                break;
            case 2:
1531
                stw_phys(addr, val);
B
bellard 已提交
1532 1533 1534
                break;
            case 4:
            default:
1535
                stl_phys(addr, val);
B
bellard 已提交
1536
                break;
B
bellard 已提交
1537
            case 8:
1538
                stq_phys(addr, val);
B
bellard 已提交
1539
                break;
B
bellard 已提交
1540
            }
B
blueswir1 已提交
1541
        }
1542
        break;
B
blueswir1 已提交
1543
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
B
blueswir1 已提交
1544
        {
1545 1546
            switch(size) {
            case 1:
B
blueswir1 已提交
1547 1548
                stb_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1549 1550
                break;
            case 2:
1551
                stw_phys((target_phys_addr_t)addr
B
blueswir1 已提交
1552
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1553 1554 1555
                break;
            case 4:
            default:
1556
                stl_phys((target_phys_addr_t)addr
B
blueswir1 已提交
1557
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1558 1559
                break;
            case 8:
1560
                stq_phys((target_phys_addr_t)addr
B
blueswir1 已提交
1561
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1562 1563
                break;
            }
B
blueswir1 已提交
1564
        }
1565
        break;
B
blueswir1 已提交
1566 1567 1568
    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 已提交
1569 1570
    case 0x32: // store buffer control or Turbosparc page table
               // descriptor diagnostic
1571 1572
    case 0x36: /* I-cache flash clear */
    case 0x37: /* D-cache flash clear */
B
blueswir1 已提交
1573
    case 0x4c: /* breakpoint action */
1574
        break;
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
    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;
            }
            DPRINTF_MMU("write breakpoint reg[%d] 0x%016llx\n", reg,
                        env->mmuregs[reg]);
        }
        break;
B
blueswir1 已提交
1597
    case 8: /* User code access, XXX */
1598
    case 9: /* Supervisor code access, XXX */
1599
    default:
1600
        do_unassigned_access(addr, 1, 0, asi, size);
1601
        break;
1602
    }
1603
#ifdef DEBUG_ASI
B
blueswir1 已提交
1604
    dump_asi("write", addr, asi, size, val);
1605
#endif
1606 1607
}

1608 1609 1610 1611
#endif /* CONFIG_USER_ONLY */
#else /* TARGET_SPARC64 */

#ifdef CONFIG_USER_ONLY
B
blueswir1 已提交
1612
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1613 1614
{
    uint64_t ret = 0;
B
blueswir1 已提交
1615 1616 1617
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
1618 1619 1620 1621

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

1622
    helper_check_align(addr, size - 1);
B
blueswir1 已提交
1623
    address_mask(env, &addr);
1624

1625 1626 1627
    switch (asi) {
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
B
blueswir1 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636
        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
1637 1638 1639
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
1640
                ret = ldub_raw(addr);
1641 1642
                break;
            case 2:
1643
                ret = lduw_raw(addr);
1644 1645
                break;
            case 4:
1646
                ret = ldl_raw(addr);
1647 1648 1649
                break;
            default:
            case 8:
1650
                ret = ldq_raw(addr);
1651 1652 1653 1654 1655 1656
                break;
            }
        }
        break;
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665
        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
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
        // 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 已提交
1681
            break;
1682 1683
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
1684
            break;
1685 1686
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
1687
            break;
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
1700
            break;
1701 1702
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
1703
            break;
1704 1705
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1706
            break;
1707 1708 1709 1710
        default:
            break;
        }
    }
B
blueswir1 已提交
1711 1712 1713 1714
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
1715 1716
}

B
blueswir1 已提交
1717
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
1718
{
B
blueswir1 已提交
1719 1720 1721
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
1722 1723 1724
    if (asi < 0x80)
        raise_exception(TT_PRIV_ACT);

1725
    helper_check_align(addr, size - 1);
B
blueswir1 已提交
1726
    address_mask(env, &addr);
1727

1728 1729 1730 1731 1732 1733
    /* Convert to little endian */
    switch (asi) {
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
B
blueswir1 已提交
1734
            addr = bswap16(addr);
B
blueswir1 已提交
1735
            break;
1736
        case 4:
B
blueswir1 已提交
1737
            addr = bswap32(addr);
B
blueswir1 已提交
1738
            break;
1739
        case 8:
B
blueswir1 已提交
1740
            addr = bswap64(addr);
B
blueswir1 已提交
1741
            break;
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
        default:
            break;
        }
    default:
        break;
    }

    switch(asi) {
    case 0x80: // Primary
    case 0x88: // Primary LE
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
1755
                stb_raw(addr, val);
1756 1757
                break;
            case 2:
1758
                stw_raw(addr, val);
1759 1760
                break;
            case 4:
1761
                stl_raw(addr, val);
1762 1763 1764
                break;
            case 8:
            default:
1765
                stq_raw(addr, val);
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
                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:
1780
        do_unassigned_access(addr, 1, 0, 1, size);
1781 1782 1783 1784 1785
        return;
    }
}

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

B
blueswir1 已提交
1787
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
B
bellard 已提交
1788
{
B
bellard 已提交
1789
    uint64_t ret = 0;
B
blueswir1 已提交
1790 1791 1792
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
B
bellard 已提交
1793

B
blueswir1 已提交
1794
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1795 1796
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
1797
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
1798
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
1799

1800
    helper_check_align(addr, size - 1);
B
bellard 已提交
1801
    switch (asi) {
B
blueswir1 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
        if (cpu_get_phys_page_debug(env, addr) == -1ULL) {
#ifdef DEBUG_ASI
            dump_asi("read ", last_addr, asi, size, ret);
#endif
            return 0;
        }
        // Fall through
1811 1812 1813 1814
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
1815 1816
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
1817
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1818 1819
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
1820 1821
                switch(size) {
                case 1:
B
blueswir1 已提交
1822
                    ret = ldub_hypv(addr);
B
blueswir1 已提交
1823 1824
                    break;
                case 2:
1825
                    ret = lduw_hypv(addr);
B
blueswir1 已提交
1826 1827
                    break;
                case 4:
1828
                    ret = ldl_hypv(addr);
B
blueswir1 已提交
1829 1830 1831
                    break;
                default:
                case 8:
1832
                    ret = ldq_hypv(addr);
B
blueswir1 已提交
1833 1834 1835 1836 1837
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
1838
                    ret = ldub_kernel(addr);
B
blueswir1 已提交
1839 1840
                    break;
                case 2:
1841
                    ret = lduw_kernel(addr);
B
blueswir1 已提交
1842 1843
                    break;
                case 4:
1844
                    ret = ldl_kernel(addr);
B
blueswir1 已提交
1845 1846 1847
                    break;
                default:
                case 8:
1848
                    ret = ldq_kernel(addr);
B
blueswir1 已提交
1849 1850
                    break;
                }
1851 1852 1853 1854
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
1855
                ret = ldub_user(addr);
1856 1857
                break;
            case 2:
1858
                ret = lduw_user(addr);
1859 1860
                break;
            case 4:
1861
                ret = ldl_user(addr);
1862 1863 1864
                break;
            default:
            case 8:
1865
                ret = ldq_user(addr);
1866 1867 1868 1869
                break;
            }
        }
        break;
B
bellard 已提交
1870 1871
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
1872 1873
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
1874
        {
B
bellard 已提交
1875 1876
            switch(size) {
            case 1:
B
blueswir1 已提交
1877
                ret = ldub_phys(addr);
B
bellard 已提交
1878 1879
                break;
            case 2:
1880
                ret = lduw_phys(addr);
B
bellard 已提交
1881 1882
                break;
            case 4:
1883
                ret = ldl_phys(addr);
B
bellard 已提交
1884 1885 1886
                break;
            default:
            case 8:
1887
                ret = ldq_phys(addr);
B
bellard 已提交
1888 1889
                break;
            }
B
blueswir1 已提交
1890 1891
            break;
        }
B
blueswir1 已提交
1892 1893 1894 1895 1896
    case 0x24: // Nucleus quad LDD 128 bit atomic
    case 0x2c: // Nucleus quad LDD 128 bit atomic LE
        //  Only ldda allowed
        raise_exception(TT_ILL_INSN);
        return 0;
B
blueswir1 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
        if (cpu_get_phys_page_debug(env, addr) == -1ULL) {
#ifdef DEBUG_ASI
            dump_asi("read ", last_addr, asi, size, ret);
#endif
            return 0;
        }
        // Fall through
B
bellard 已提交
1906 1907 1908 1909 1910
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x11: // As if user secondary
    case 0x19: // As if user secondary LE
    case 0x4a: // UPA config
1911
    case 0x81: // Secondary
B
bellard 已提交
1912
    case 0x89: // Secondary LE
B
blueswir1 已提交
1913 1914
        // XXX
        break;
B
bellard 已提交
1915
    case 0x45: // LSU
B
blueswir1 已提交
1916 1917
        ret = env->lsu;
        break;
B
bellard 已提交
1918
    case 0x50: // I-MMU regs
B
blueswir1 已提交
1919
        {
B
blueswir1 已提交
1920
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
1921

1922 1923 1924 1925 1926 1927 1928
            if (reg == 0) {
                // I-TSB Tag Target register
                ret = ultrasparc_tag_target(env->immuregs[6]);
            } else {
                ret = env->immuregs[reg];
            }

B
blueswir1 已提交
1929 1930
            break;
        }
B
bellard 已提交
1931
    case 0x51: // I-MMU 8k TSB pointer
1932 1933 1934 1935 1936 1937 1938
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
            ret = ultrasparc_tsb_pointer(env->immuregs[5], env->immuregs[6],
                                         8*1024);
            break;
        }
B
bellard 已提交
1939
    case 0x52: // I-MMU 64k TSB pointer
1940 1941 1942 1943 1944 1945 1946
        {
            // env->immuregs[5] holds I-MMU TSB register value
            // env->immuregs[6] holds I-MMU Tag Access register value
            ret = ultrasparc_tsb_pointer(env->immuregs[5], env->immuregs[6],
                                         64*1024);
            break;
        }
1947 1948 1949 1950 1951 1952 1953
    case 0x55: // I-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

            ret = env->itlb_tte[reg];
            break;
        }
B
bellard 已提交
1954
    case 0x56: // I-MMU tag read
B
blueswir1 已提交
1955
        {
B
blueswir1 已提交
1956
            int reg = (addr >> 3) & 0x3f;
B
blueswir1 已提交
1957

B
blueswir1 已提交
1958
            ret = env->itlb_tag[reg];
B
blueswir1 已提交
1959 1960
            break;
        }
B
bellard 已提交
1961
    case 0x58: // D-MMU regs
B
blueswir1 已提交
1962
        {
B
blueswir1 已提交
1963
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
1964

1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
            if (reg == 0) {
                // D-TSB Tag Target register
                ret = ultrasparc_tag_target(env->dmmuregs[6]);
            } 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
            ret = ultrasparc_tsb_pointer(env->dmmuregs[5], env->dmmuregs[6],
                                         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
            ret = ultrasparc_tsb_pointer(env->dmmuregs[5], env->dmmuregs[6],
                                         64*1024);
B
blueswir1 已提交
1987 1988
            break;
        }
1989 1990 1991 1992 1993 1994 1995
    case 0x5d: // D-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

            ret = env->dtlb_tte[reg];
            break;
        }
B
bellard 已提交
1996
    case 0x5e: // D-MMU tag read
B
blueswir1 已提交
1997
        {
B
blueswir1 已提交
1998
            int reg = (addr >> 3) & 0x3f;
B
blueswir1 已提交
1999

B
blueswir1 已提交
2000
            ret = env->dtlb_tag[reg];
B
blueswir1 已提交
2001 2002
            break;
        }
2003 2004
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2005 2006 2007
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2008 2009 2010 2011 2012 2013 2014 2015
    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 已提交
2016
    case 0x5b: // D-MMU data pointer
B
bellard 已提交
2017 2018 2019
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
2020 2021
        // XXX
        break;
B
bellard 已提交
2022 2023 2024 2025
    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 已提交
2026
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
2027
    default:
2028
        do_unassigned_access(addr, 0, 0, 1, size);
B
blueswir1 已提交
2029 2030
        ret = 0;
        break;
B
bellard 已提交
2031
    }
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046

    /* 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 已提交
2047
            break;
2048 2049
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
2050
            break;
2051 2052
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
2053
            break;
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
2066
            break;
2067 2068
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
2069
            break;
2070 2071
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
2072
            break;
2073 2074 2075 2076
        default:
            break;
        }
    }
B
blueswir1 已提交
2077 2078 2079 2080
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
B
bellard 已提交
2081 2082
}

B
blueswir1 已提交
2083
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
B
bellard 已提交
2084
{
B
blueswir1 已提交
2085 2086 2087
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
B
blueswir1 已提交
2088
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2089 2090
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2091
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2092
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
2093

2094
    helper_check_align(addr, size - 1);
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
    /* 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:
B
blueswir1 已提交
2106
            addr = bswap16(addr);
B
blueswir1 已提交
2107
            break;
2108
        case 4:
B
blueswir1 已提交
2109
            addr = bswap32(addr);
B
blueswir1 已提交
2110
            break;
2111
        case 8:
B
blueswir1 已提交
2112
            addr = bswap64(addr);
B
blueswir1 已提交
2113
            break;
2114 2115 2116 2117 2118 2119 2120
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
2121
    switch(asi) {
2122 2123 2124 2125
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
2126 2127
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
2128
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
2129 2130
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
2131 2132
                switch(size) {
                case 1:
B
blueswir1 已提交
2133
                    stb_hypv(addr, val);
B
blueswir1 已提交
2134 2135
                    break;
                case 2:
2136
                    stw_hypv(addr, val);
B
blueswir1 已提交
2137 2138
                    break;
                case 4:
2139
                    stl_hypv(addr, val);
B
blueswir1 已提交
2140 2141 2142
                    break;
                case 8:
                default:
2143
                    stq_hypv(addr, val);
B
blueswir1 已提交
2144 2145 2146 2147 2148
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
2149
                    stb_kernel(addr, val);
B
blueswir1 已提交
2150 2151
                    break;
                case 2:
2152
                    stw_kernel(addr, val);
B
blueswir1 已提交
2153 2154
                    break;
                case 4:
2155
                    stl_kernel(addr, val);
B
blueswir1 已提交
2156 2157 2158
                    break;
                case 8:
                default:
2159
                    stq_kernel(addr, val);
B
blueswir1 已提交
2160 2161
                    break;
                }
2162 2163 2164 2165
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
2166
                stb_user(addr, val);
2167 2168
                break;
            case 2:
2169
                stw_user(addr, val);
2170 2171
                break;
            case 4:
2172
                stl_user(addr, val);
2173 2174 2175
                break;
            case 8:
            default:
2176
                stq_user(addr, val);
2177 2178 2179 2180
                break;
            }
        }
        break;
B
bellard 已提交
2181 2182
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
2183 2184
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
2185
        {
B
bellard 已提交
2186 2187
            switch(size) {
            case 1:
B
blueswir1 已提交
2188
                stb_phys(addr, val);
B
bellard 已提交
2189 2190
                break;
            case 2:
2191
                stw_phys(addr, val);
B
bellard 已提交
2192 2193
                break;
            case 4:
2194
                stl_phys(addr, val);
B
bellard 已提交
2195 2196 2197
                break;
            case 8:
            default:
2198
                stq_phys(addr, val);
B
bellard 已提交
2199 2200
                break;
            }
B
blueswir1 已提交
2201 2202
        }
        return;
B
blueswir1 已提交
2203 2204 2205 2206 2207
    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 已提交
2208 2209 2210 2211 2212
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x11: // As if user secondary
    case 0x19: // As if user secondary LE
    case 0x4a: // UPA config
B
blueswir1 已提交
2213
    case 0x81: // Secondary
B
bellard 已提交
2214
    case 0x89: // Secondary LE
B
blueswir1 已提交
2215 2216
        // XXX
        return;
B
bellard 已提交
2217
    case 0x45: // LSU
B
blueswir1 已提交
2218 2219 2220 2221
        {
            uint64_t oldreg;

            oldreg = env->lsu;
B
blueswir1 已提交
2222
            env->lsu = val & (DMMU_E | IMMU_E);
B
blueswir1 已提交
2223 2224 2225
            // Mappings generated during D/I MMU disabled mode are
            // invalid in normal mode
            if (oldreg != env->lsu) {
B
blueswir1 已提交
2226 2227
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
                            oldreg, env->lsu);
B
bellard 已提交
2228
#ifdef DEBUG_MMU
B
blueswir1 已提交
2229
                dump_mmu(env);
B
bellard 已提交
2230
#endif
B
blueswir1 已提交
2231 2232 2233 2234
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
2235
    case 0x50: // I-MMU regs
B
blueswir1 已提交
2236
        {
B
blueswir1 已提交
2237
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
2238
            uint64_t oldreg;
2239

B
blueswir1 已提交
2240
            oldreg = env->immuregs[reg];
B
bellard 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 1: // Not in I-MMU
            case 2:
            case 7:
            case 8:
                return;
            case 3: // SFSR
B
blueswir1 已提交
2251 2252
                if ((val & 1) == 0)
                    val = 0; // Clear SFSR
B
bellard 已提交
2253 2254 2255 2256 2257 2258
                break;
            case 5: // TSB access
            case 6: // Tag access
            default:
                break;
            }
B
blueswir1 已提交
2259
            env->immuregs[reg] = val;
B
bellard 已提交
2260
            if (oldreg != env->immuregs[reg]) {
B
blueswir1 已提交
2261 2262
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
                            PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
B
bellard 已提交
2263
            }
2264
#ifdef DEBUG_MMU
B
blueswir1 已提交
2265
            dump_mmu(env);
B
bellard 已提交
2266
#endif
B
blueswir1 已提交
2267 2268
            return;
        }
B
bellard 已提交
2269
    case 0x54: // I-MMU data in
B
blueswir1 已提交
2270 2271 2272 2273 2274 2275 2276
        {
            unsigned int i;

            // Try finding an invalid entry
            for (i = 0; i < 64; i++) {
                if ((env->itlb_tte[i] & 0x8000000000000000ULL) == 0) {
                    env->itlb_tag[i] = env->immuregs[6];
B
blueswir1 已提交
2277
                    env->itlb_tte[i] = val;
B
blueswir1 已提交
2278 2279 2280 2281 2282 2283 2284
                    return;
                }
            }
            // Try finding an unlocked entry
            for (i = 0; i < 64; i++) {
                if ((env->itlb_tte[i] & 0x40) == 0) {
                    env->itlb_tag[i] = env->immuregs[6];
B
blueswir1 已提交
2285
                    env->itlb_tte[i] = val;
B
blueswir1 已提交
2286 2287 2288 2289 2290 2291
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
2292
    case 0x55: // I-MMU data access
B
blueswir1 已提交
2293
        {
2294 2295
            // TODO: auto demap

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

B
blueswir1 已提交
2298
            env->itlb_tag[i] = env->immuregs[6];
B
blueswir1 已提交
2299
            env->itlb_tte[i] = val;
B
blueswir1 已提交
2300 2301
            return;
        }
B
bellard 已提交
2302
    case 0x57: // I-MMU demap
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
        {
            unsigned int i;

            for (i = 0; i < 64; i++) {
                if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0) {
                    target_ulong mask = 0xffffffffffffe000ULL;

                    mask <<= 3 * ((env->itlb_tte[i] >> 61) & 3);
                    if ((val & mask) == (env->itlb_tag[i] & mask)) {
                        env->itlb_tag[i] = 0;
                        env->itlb_tte[i] = 0;
                    }
                    return;
                }
            }
        }
B
blueswir1 已提交
2319
        return;
B
bellard 已提交
2320
    case 0x58: // D-MMU regs
B
blueswir1 已提交
2321
        {
B
blueswir1 已提交
2322
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
2323
            uint64_t oldreg;
2324

B
blueswir1 已提交
2325
            oldreg = env->dmmuregs[reg];
B
bellard 已提交
2326 2327 2328 2329 2330
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
B
blueswir1 已提交
2331 2332
                if ((val & 1) == 0) {
                    val = 0; // Clear SFSR, Fault address
B
blueswir1 已提交
2333 2334
                    env->dmmuregs[4] = 0;
                }
B
blueswir1 已提交
2335
                env->dmmuregs[reg] = val;
B
bellard 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
                break;
            case 1: // Primary context
            case 2: // Secondary context
            case 5: // TSB access
            case 6: // Tag access
            case 7: // Virtual Watchpoint
            case 8: // Physical Watchpoint
            default:
                break;
            }
B
blueswir1 已提交
2346
            env->dmmuregs[reg] = val;
B
bellard 已提交
2347
            if (oldreg != env->dmmuregs[reg]) {
B
blueswir1 已提交
2348 2349
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
                            PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
B
bellard 已提交
2350
            }
2351
#ifdef DEBUG_MMU
B
blueswir1 已提交
2352
            dump_mmu(env);
B
bellard 已提交
2353
#endif
B
blueswir1 已提交
2354 2355
            return;
        }
B
bellard 已提交
2356
    case 0x5c: // D-MMU data in
B
blueswir1 已提交
2357 2358 2359 2360 2361 2362 2363
        {
            unsigned int i;

            // Try finding an invalid entry
            for (i = 0; i < 64; i++) {
                if ((env->dtlb_tte[i] & 0x8000000000000000ULL) == 0) {
                    env->dtlb_tag[i] = env->dmmuregs[6];
B
blueswir1 已提交
2364
                    env->dtlb_tte[i] = val;
B
blueswir1 已提交
2365 2366 2367 2368 2369 2370 2371
                    return;
                }
            }
            // Try finding an unlocked entry
            for (i = 0; i < 64; i++) {
                if ((env->dtlb_tte[i] & 0x40) == 0) {
                    env->dtlb_tag[i] = env->dmmuregs[6];
B
blueswir1 已提交
2372
                    env->dtlb_tte[i] = val;
B
blueswir1 已提交
2373 2374 2375 2376 2377 2378
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
2379
    case 0x5d: // D-MMU data access
B
blueswir1 已提交
2380
        {
B
blueswir1 已提交
2381
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
2382

B
blueswir1 已提交
2383
            env->dtlb_tag[i] = env->dmmuregs[6];
B
blueswir1 已提交
2384
            env->dtlb_tte[i] = val;
B
blueswir1 已提交
2385 2386
            return;
        }
B
bellard 已提交
2387
    case 0x5f: // D-MMU demap
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
        {
            unsigned int i;

            for (i = 0; i < 64; i++) {
                if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0) {
                    target_ulong mask = 0xffffffffffffe000ULL;

                    mask <<= 3 * ((env->dtlb_tte[i] >> 61) & 3);
                    if ((val & mask) == (env->dtlb_tag[i] & mask)) {
                        env->dtlb_tag[i] = 0;
                        env->dtlb_tte[i] = 0;
                    }
                    return;
                }
            }
        }
        return;
B
bellard 已提交
2405
    case 0x49: // Interrupt data receive
B
blueswir1 已提交
2406 2407
        // XXX
        return;
2408 2409
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2410 2411 2412
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2413 2414 2415 2416 2417 2418 2419 2420
    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 已提交
2421 2422 2423 2424 2425 2426 2427
    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 已提交
2428 2429 2430 2431 2432 2433
    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 已提交
2434
    default:
2435
        do_unassigned_access(addr, 1, 0, 1, size);
B
blueswir1 已提交
2436
        return;
B
bellard 已提交
2437 2438
    }
}
2439
#endif /* CONFIG_USER_ONLY */
2440

B
blueswir1 已提交
2441 2442 2443
void helper_ldda_asi(target_ulong addr, int asi, int rd)
{
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2444 2445
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2446
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
        raise_exception(TT_PRIV_ACT);

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

B
blueswir1 已提交
2488
void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
2489 2490
{
    unsigned int i;
B
blueswir1 已提交
2491
    target_ulong val;
2492

2493
    helper_check_align(addr, 3);
2494 2495 2496 2497 2498
    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 已提交
2499 2500 2501 2502
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2503
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2504
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2505 2506
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4,
                                                         0);
B
blueswir1 已提交
2507
            addr += 4;
2508 2509 2510 2511 2512 2513 2514
        }

        return;
    default:
        break;
    }

B
blueswir1 已提交
2515
    val = helper_ld_asi(addr, asi, size, 0);
2516 2517 2518
    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2519
        *((uint32_t *)&env->fpr[rd]) = val;
2520 2521
        break;
    case 8:
B
blueswir1 已提交
2522
        *((int64_t *)&DT0) = val;
2523
        break;
B
blueswir1 已提交
2524 2525 2526
    case 16:
        // XXX
        break;
2527 2528 2529
    }
}

B
blueswir1 已提交
2530
void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
2531 2532
{
    unsigned int i;
B
blueswir1 已提交
2533
    target_ulong val = 0;
2534

2535
    helper_check_align(addr, 3);
2536
    switch (asi) {
B
blueswir1 已提交
2537 2538
    case 0xe0: // UA2007 Block commit store primary (cache flush)
    case 0xe1: // UA2007 Block commit store secondary (cache flush)
2539 2540 2541 2542
    case 0xf0: // Block store primary
    case 0xf1: // Block store secondary
    case 0xf8: // Block store primary LE
    case 0xf9: // Block store secondary LE
B
blueswir1 已提交
2543 2544 2545 2546
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2547
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2548
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2549 2550 2551
            val = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(addr, val, asi & 0x8f, 4);
            addr += 4;
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
        }

        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2562
        val = *((uint32_t *)&env->fpr[rd]);
2563 2564
        break;
    case 8:
B
blueswir1 已提交
2565
        val = *((int64_t *)&DT0);
2566
        break;
B
blueswir1 已提交
2567 2568 2569
    case 16:
        // XXX
        break;
2570
    }
B
blueswir1 已提交
2571 2572 2573 2574 2575 2576 2577 2578
    helper_st_asi(addr, val, asi, size);
}

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

2579
    val2 &= 0xffffffffUL;
B
blueswir1 已提交
2580 2581
    ret = helper_ld_asi(addr, asi, 4, 0);
    ret &= 0xffffffffUL;
2582 2583
    if (val2 == ret)
        helper_st_asi(addr, val1 & 0xffffffffUL, asi, 4);
B
blueswir1 已提交
2584
    return ret;
2585 2586
}

B
blueswir1 已提交
2587 2588 2589 2590 2591 2592
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);
2593 2594
    if (val2 == ret)
        helper_st_asi(addr, val1, asi, 8);
B
blueswir1 已提交
2595 2596
    return ret;
}
2597
#endif /* TARGET_SPARC64 */
B
bellard 已提交
2598 2599

#ifndef TARGET_SPARC64
B
blueswir1 已提交
2600
void helper_rett(void)
2601
{
2602 2603
    unsigned int cwp;

2604 2605 2606
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

2607
    env->psret = 1;
2608
    cwp = cpu_cwp_inc(env, env->cwp + 1) ;
2609 2610 2611 2612 2613 2614
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
2615
#endif
2616

B
blueswir1 已提交
2617 2618 2619 2620 2621
target_ulong helper_udiv(target_ulong a, target_ulong b)
{
    uint64_t x0;
    uint32_t x1;

2622
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643
    x1 = b;

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

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

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

2644
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
    x1 = b;

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

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

B
blueswir1 已提交
2661 2662
void helper_stdf(target_ulong addr, int mem_idx)
{
2663
    helper_check_align(addr, 7);
B
blueswir1 已提交
2664 2665 2666
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2667
        stfq_user(addr, DT0);
B
blueswir1 已提交
2668 2669
        break;
    case 1:
2670
        stfq_kernel(addr, DT0);
B
blueswir1 已提交
2671 2672 2673
        break;
#ifdef TARGET_SPARC64
    case 2:
2674
        stfq_hypv(addr, DT0);
B
blueswir1 已提交
2675 2676 2677 2678 2679 2680
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2681
    address_mask(env, &addr);
2682
    stfq_raw(addr, DT0);
B
blueswir1 已提交
2683 2684 2685 2686 2687
#endif
}

void helper_lddf(target_ulong addr, int mem_idx)
{
2688
    helper_check_align(addr, 7);
B
blueswir1 已提交
2689 2690 2691
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2692
        DT0 = ldfq_user(addr);
B
blueswir1 已提交
2693 2694
        break;
    case 1:
2695
        DT0 = ldfq_kernel(addr);
B
blueswir1 已提交
2696 2697 2698
        break;
#ifdef TARGET_SPARC64
    case 2:
2699
        DT0 = ldfq_hypv(addr);
B
blueswir1 已提交
2700 2701 2702 2703 2704 2705
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2706
    address_mask(env, &addr);
2707
    DT0 = ldfq_raw(addr);
B
blueswir1 已提交
2708 2709 2710
#endif
}

B
blueswir1 已提交
2711
void helper_ldqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
2712 2713 2714 2715
{
    // XXX add 128 bit load
    CPU_QuadU u;

2716
    helper_check_align(addr, 7);
B
blueswir1 已提交
2717 2718 2719
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2720 2721
        u.ll.upper = ldq_user(addr);
        u.ll.lower = ldq_user(addr + 8);
B
blueswir1 已提交
2722 2723 2724
        QT0 = u.q;
        break;
    case 1:
2725 2726
        u.ll.upper = ldq_kernel(addr);
        u.ll.lower = ldq_kernel(addr + 8);
B
blueswir1 已提交
2727 2728 2729 2730
        QT0 = u.q;
        break;
#ifdef TARGET_SPARC64
    case 2:
2731 2732
        u.ll.upper = ldq_hypv(addr);
        u.ll.lower = ldq_hypv(addr + 8);
B
blueswir1 已提交
2733 2734 2735 2736 2737 2738 2739
        QT0 = u.q;
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2740
    address_mask(env, &addr);
2741 2742
    u.ll.upper = ldq_raw(addr);
    u.ll.lower = ldq_raw((addr + 8) & 0xffffffffULL);
B
blueswir1 已提交
2743
    QT0 = u.q;
B
blueswir1 已提交
2744
#endif
B
blueswir1 已提交
2745 2746
}

B
blueswir1 已提交
2747
void helper_stqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
2748 2749 2750 2751
{
    // XXX add 128 bit store
    CPU_QuadU u;

2752
    helper_check_align(addr, 7);
B
blueswir1 已提交
2753 2754 2755 2756
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
        u.q = QT0;
2757 2758
        stq_user(addr, u.ll.upper);
        stq_user(addr + 8, u.ll.lower);
B
blueswir1 已提交
2759 2760 2761
        break;
    case 1:
        u.q = QT0;
2762 2763
        stq_kernel(addr, u.ll.upper);
        stq_kernel(addr + 8, u.ll.lower);
B
blueswir1 已提交
2764 2765 2766 2767
        break;
#ifdef TARGET_SPARC64
    case 2:
        u.q = QT0;
2768 2769
        stq_hypv(addr, u.ll.upper);
        stq_hypv(addr + 8, u.ll.lower);
B
blueswir1 已提交
2770 2771 2772 2773 2774 2775
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2776
    u.q = QT0;
B
blueswir1 已提交
2777
    address_mask(env, &addr);
2778 2779
    stq_raw(addr, u.ll.upper);
    stq_raw((addr + 8) & 0xffffffffULL, u.ll.lower);
B
blueswir1 已提交
2780
#endif
B
blueswir1 已提交
2781
}
B
blueswir1 已提交
2782

2783
static inline void set_fsr(void)
2784
{
B
bellard 已提交
2785
    int rnd_mode;
B
blueswir1 已提交
2786

2787 2788
    switch (env->fsr & FSR_RD_MASK) {
    case FSR_RD_NEAREST:
B
bellard 已提交
2789
        rnd_mode = float_round_nearest_even;
B
blueswir1 已提交
2790
        break;
B
bellard 已提交
2791
    default:
2792
    case FSR_RD_ZERO:
B
bellard 已提交
2793
        rnd_mode = float_round_to_zero;
B
blueswir1 已提交
2794
        break;
2795
    case FSR_RD_POS:
B
bellard 已提交
2796
        rnd_mode = float_round_up;
B
blueswir1 已提交
2797
        break;
2798
    case FSR_RD_NEG:
B
bellard 已提交
2799
        rnd_mode = float_round_down;
B
blueswir1 已提交
2800
        break;
2801
    }
B
bellard 已提交
2802
    set_float_rounding_mode(rnd_mode, &env->fp_status);
2803
}
B
bellard 已提交
2804

2805
void helper_ldfsr(uint32_t new_fsr)
B
blueswir1 已提交
2806
{
2807 2808
    env->fsr = (new_fsr & FSR_LDFSR_MASK) | (env->fsr & FSR_LDFSR_OLDMASK);
    set_fsr();
B
blueswir1 已提交
2809 2810
}

2811 2812 2813 2814 2815 2816 2817 2818
#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 已提交
2819
void helper_debug(void)
B
bellard 已提交
2820 2821 2822 2823
{
    env->exception_index = EXCP_DEBUG;
    cpu_loop_exit();
}
2824

B
bellard 已提交
2825
#ifndef TARGET_SPARC64
2826 2827 2828 2829 2830 2831
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

2832
    cwp = cpu_cwp_dec(env, env->cwp - 1);
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_OVF);
    }
    set_cwp(cwp);
}

void helper_restore(void)
{
    uint32_t cwp;

2843
    cwp = cpu_cwp_inc(env, env->cwp + 1);
2844 2845 2846 2847 2848 2849
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
}

B
blueswir1 已提交
2850
void helper_wrpsr(target_ulong new_psr)
2851
{
2852
    if ((new_psr & PSR_CWP) >= env->nwindows)
2853 2854
        raise_exception(TT_ILL_INSN);
    else
B
blueswir1 已提交
2855
        PUT_PSR(env, new_psr);
2856 2857
}

B
blueswir1 已提交
2858
target_ulong helper_rdpsr(void)
2859
{
B
blueswir1 已提交
2860
    return GET_PSR(env);
2861
}
B
bellard 已提交
2862 2863

#else
2864 2865 2866 2867 2868 2869
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

2870
    cwp = cpu_cwp_dec(env, env->cwp - 1);
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
    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;

2891
    cwp = cpu_cwp_inc(env, env->cwp + 1);
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
    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)
{
2905
    if (env->cansave != env->nwindows - 2) {
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
        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++;
2924
    if (env->cleanwin < env->nwindows - 1)
2925 2926 2927 2928 2929 2930 2931
        env->cleanwin++;
    if (env->otherwin == 0)
        env->cansave--;
    else
        env->otherwin--;
}

B
blueswir1 已提交
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952
target_ulong helper_rdccr(void)
{
    return GET_CCR(env);
}

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

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

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

2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
// 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 已提交
2985
target_ulong helper_popc(target_ulong val)
B
bellard 已提交
2986
{
B
blueswir1 已提交
2987
    return ctpop64(val);
B
bellard 已提交
2988
}
B
bellard 已提交
2989 2990 2991 2992 2993 2994

static inline uint64_t *get_gregset(uint64_t pstate)
{
    switch (pstate) {
    default:
    case 0:
B
blueswir1 已提交
2995
        return env->bgregs;
B
bellard 已提交
2996
    case PS_AG:
B
blueswir1 已提交
2997
        return env->agregs;
B
bellard 已提交
2998
    case PS_MG:
B
blueswir1 已提交
2999
        return env->mgregs;
B
bellard 已提交
3000
    case PS_IG:
B
blueswir1 已提交
3001
        return env->igregs;
B
bellard 已提交
3002 3003 3004
    }
}

B
blueswir1 已提交
3005
static inline void change_pstate(uint64_t new_pstate)
B
bellard 已提交
3006
{
3007
    uint64_t pstate_regs, new_pstate_regs;
B
bellard 已提交
3008 3009 3010 3011 3012
    uint64_t *src, *dst;

    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
    if (new_pstate_regs != pstate_regs) {
B
blueswir1 已提交
3013 3014 3015 3016 3017
        // 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 已提交
3018 3019 3020 3021
    }
    env->pstate = new_pstate;
}

B
blueswir1 已提交
3022
void helper_wrpstate(target_ulong new_state)
3023
{
3024
    if (!(env->def->features & CPU_FEATURE_GL))
3025
        change_pstate(new_state & 0xf3f);
3026 3027
}

B
blueswir1 已提交
3028
void helper_done(void)
B
bellard 已提交
3029
{
3030 3031 3032 3033 3034 3035
    env->pc = env->tsptr->tpc;
    env->npc = env->tsptr->tnpc + 4;
    PUT_CCR(env, env->tsptr->tstate >> 32);
    env->asi = (env->tsptr->tstate >> 24) & 0xff;
    change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
    PUT_CWP64(env, env->tsptr->tstate & 0xff);
B
blueswir1 已提交
3036
    env->tl--;
3037
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
B
bellard 已提交
3038 3039
}

B
blueswir1 已提交
3040
void helper_retry(void)
B
bellard 已提交
3041
{
3042 3043 3044 3045 3046 3047
    env->pc = env->tsptr->tpc;
    env->npc = env->tsptr->tnpc;
    PUT_CCR(env, env->tsptr->tstate >> 32);
    env->asi = (env->tsptr->tstate >> 24) & 0xff;
    change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
    PUT_CWP64(env, env->tsptr->tstate & 0xff);
B
blueswir1 已提交
3048
    env->tl--;
3049
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
B
bellard 已提交
3050
}
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065

void helper_set_softint(uint64_t value)
{
    env->softint |= (uint32_t)value;
}

void helper_clear_softint(uint64_t value)
{
    env->softint &= (uint32_t)~value;
}

void helper_write_softint(uint64_t value)
{
    env->softint = (uint32_t)value;
}
B
bellard 已提交
3066
#endif
3067

B
blueswir1 已提交
3068
void helper_flush(target_ulong addr)
3069
{
B
blueswir1 已提交
3070 3071
    addr &= ~7;
    tb_invalidate_page_range(addr, addr + 8);
3072 3073
}

B
blueswir1 已提交
3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115
#ifdef TARGET_SPARC64
#ifdef DEBUG_PCALL
static const char * const excp_names[0x80] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_TMISS] = "Instruction Access MMU Miss",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_TOVF] = "Tag Overflow",
    [TT_CLRWIN] = "Clean Windows",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_DFAULT] = "Data Access Fault",
    [TT_DMISS] = "Data Access MMU Miss",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DPROT] = "Data Protection Error",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_PRIV_ACT] = "Privileged Action",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
};
#endif

void do_interrupt(CPUState *env)
{
    int intno = env->exception_index;

#ifdef DEBUG_PCALL
3116
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
        static int count;
        const char *name;

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

3134
        qemu_log("%6d: %s (v=%04x) pc=%016" PRIx64 " npc=%016" PRIx64
B
blueswir1 已提交
3135 3136 3137 3138
                " SP=%016" PRIx64 "\n",
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
3139
        log_cpu_state(env, 0);
B
blueswir1 已提交
3140 3141 3142 3143 3144
#if 0
        {
            int i;
            uint8_t *ptr;

3145
            qemu_log("       code=");
B
blueswir1 已提交
3146 3147
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
3148
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
3149
            }
3150
            qemu_log("\n");
B
blueswir1 已提交
3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204
        }
#endif
        count++;
    }
#endif
#if !defined(CONFIG_USER_ONLY)
    if (env->tl >= env->maxtl) {
        cpu_abort(env, "Trap 0x%04x while trap level (%d) >= MAXTL (%d),"
                  " Error state", env->exception_index, env->tl, env->maxtl);
        return;
    }
#endif
    if (env->tl < env->maxtl - 1) {
        env->tl++;
    } else {
        env->pstate |= PS_RED;
        if (env->tl < env->maxtl)
            env->tl++;
    }
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
    env->tsptr->tstate = ((uint64_t)GET_CCR(env) << 32) |
        ((env->asi & 0xff) << 24) | ((env->pstate & 0xf3f) << 8) |
        GET_CWP64(env);
    env->tsptr->tpc = env->pc;
    env->tsptr->tnpc = env->npc;
    env->tsptr->tt = intno;
    if (!(env->def->features & CPU_FEATURE_GL)) {
        switch (intno) {
        case TT_IVEC:
            change_pstate(PS_PEF | PS_PRIV | PS_IG);
            break;
        case TT_TFAULT:
        case TT_TMISS:
        case TT_DFAULT:
        case TT_DMISS:
        case TT_DPROT:
            change_pstate(PS_PEF | PS_PRIV | PS_MG);
            break;
        default:
            change_pstate(PS_PEF | PS_PRIV | PS_AG);
            break;
        }
    }
    if (intno == TT_CLRWIN)
        cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - 1));
    else if ((intno & 0x1c0) == TT_SPILL)
        cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
    else if ((intno & 0x1c0) == TT_FILL)
        cpu_set_cwp(env, cpu_cwp_inc(env, env->cwp + 1));
    env->tbr &= ~0x7fffULL;
    env->tbr |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
    env->pc = env->tbr;
    env->npc = env->pc + 4;
    env->exception_index = 0;
3205
}
B
blueswir1 已提交
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
#else
#ifdef DEBUG_PCALL
static const char * const excp_names[0x80] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_WIN_OVF] = "Window Overflow",
    [TT_WIN_UNF] = "Window Underflow",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_DFAULT] = "Data Access Fault",
    [TT_TOVF] = "Tag Overflow",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
    [TT_TOVF] = "Tag Overflow",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_NCP_INSN] = "Coprocessor Disabled",
};
#endif
3241

B
blueswir1 已提交
3242
void do_interrupt(CPUState *env)
3243
{
B
blueswir1 已提交
3244 3245 3246
    int cwp, intno = env->exception_index;

#ifdef DEBUG_PCALL
3247
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260
        static int count;
        const char *name;

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

3261
        qemu_log("%6d: %s (v=%02x) pc=%08x npc=%08x SP=%08x\n",
B
blueswir1 已提交
3262 3263 3264
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
3265
        log_cpu_state(env, 0);
B
blueswir1 已提交
3266 3267 3268 3269 3270
#if 0
        {
            int i;
            uint8_t *ptr;

3271
            qemu_log("       code=");
B
blueswir1 已提交
3272 3273
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
3274
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
3275
            }
3276
            qemu_log("\n");
B
blueswir1 已提交
3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
        }
#endif
        count++;
    }
#endif
#if !defined(CONFIG_USER_ONLY)
    if (env->psret == 0) {
        cpu_abort(env, "Trap 0x%02x while interrupts disabled, Error state",
                  env->exception_index);
        return;
    }
#endif
    env->psret = 0;
    cwp = cpu_cwp_dec(env, env->cwp - 1);
    cpu_set_cwp(env, cwp);
    env->regwptr[9] = env->pc;
    env->regwptr[10] = env->npc;
    env->psrps = env->psrs;
    env->psrs = 1;
    env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
    env->pc = env->tbr;
    env->npc = env->pc + 4;
    env->exception_index = 0;
3300
}
B
blueswir1 已提交
3301
#endif
3302

3303
#if !defined(CONFIG_USER_ONLY)
3304

3305 3306 3307
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

3308
#define MMUSUFFIX _mmu
3309
#define ALIGNED_ONLY
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322

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

3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340
/* XXX: make it generic ? */
static void cpu_restore_state2(void *retaddr)
{
    TranslationBlock *tb;
    unsigned long pc;

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

3341 3342 3343
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr)
{
B
blueswir1 已提交
3344
#ifdef DEBUG_UNALIGNED
3345 3346
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
           "\n", addr, env->pc);
B
blueswir1 已提交
3347
#endif
3348
    cpu_restore_state2(retaddr);
B
blueswir1 已提交
3349
    raise_exception(TT_UNALIGNED);
3350
}
3351 3352 3353 3354 3355

/* 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 */
3356
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
3357 3358 3359 3360 3361 3362 3363 3364 3365
{
    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;

3366
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
3367
    if (ret) {
3368
        cpu_restore_state2(retaddr);
3369 3370 3371 3372 3373 3374
        cpu_loop_exit();
    }
    env = saved_env;
}

#endif
3375 3376

#ifndef TARGET_SPARC64
3377
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3378
                          int is_asi, int size)
3379 3380 3381 3382 3383 3384 3385
{
    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;
3386 3387
#ifdef DEBUG_UNASSIGNED
    if (is_asi)
3388
        printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
B
blueswir1 已提交
3389
               " asi 0x%02x from " TARGET_FMT_lx "\n",
3390 3391
               is_exec ? "exec" : is_write ? "write" : "read", size,
               size == 1 ? "" : "s", addr, is_asi, env->pc);
3392
    else
3393 3394 3395 3396
        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);
3397
#endif
3398
    if (env->mmuregs[3]) /* Fault status register */
B
blueswir1 已提交
3399
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410
    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;
    env->mmuregs[4] = addr; /* Fault address register */
    if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
3411 3412 3413 3414
        if (is_exec)
            raise_exception(TT_CODE_ACCESS);
        else
            raise_exception(TT_DATA_ACCESS);
3415 3416 3417 3418
    }
    env = saved_env;
}
#else
3419
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3420
                          int is_asi, int size)
3421 3422 3423 3424 3425 3426 3427 3428
{
#ifdef DEBUG_UNASSIGNED
    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;
B
blueswir1 已提交
3429 3430
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
           "\n", addr, env->pc);
3431 3432
    env = saved_env;
#endif
3433 3434 3435 3436
    if (is_exec)
        raise_exception(TT_CODE_ACCESS);
    else
        raise_exception(TT_DATA_ACCESS);
3437 3438
}
#endif
3439

B
blueswir1 已提交
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
#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