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

B
blueswir1 已提交
42 43 44 45 46 47 48 49
static inline void address_mask(CPUState *env1, target_ulong *addr)
{
#ifdef TARGET_SPARC64
    if (AM_CHECK(env1))
        *addr &= 0xffffffffULL;
#endif
}

B
blueswir1 已提交
50
static void raise_exception(int tt)
B
bellard 已提交
51 52 53
{
    env->exception_index = tt;
    cpu_loop_exit();
54
}
B
bellard 已提交
55

P
pbrook 已提交
56 57 58 59 60
void HELPER(raise_exception)(int tt)
{
    raise_exception(tt);
}

B
blueswir1 已提交
61 62 63 64 65
static inline void set_cwp(int new_cwp)
{
    cpu_set_cwp(env, new_cwp);
}

B
blueswir1 已提交
66 67
void helper_check_align(target_ulong addr, uint32_t align)
{
68 69 70 71 72
    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 已提交
73
        raise_exception(TT_UNALIGNED);
74
    }
B
blueswir1 已提交
75 76
}

77 78 79
#define F_HELPER(name, p) void helper_f##name##p(void)

#define F_BINOP(name)                                           \
B
blueswir1 已提交
80
    float32 helper_f ## name ## s (float32 src1, float32 src2)  \
81
    {                                                           \
B
blueswir1 已提交
82
        return float32_ ## name (src1, src2, &env->fp_status);  \
83 84 85 86
    }                                                           \
    F_HELPER(name, d)                                           \
    {                                                           \
        DT0 = float64_ ## name (DT0, DT1, &env->fp_status);     \
B
blueswir1 已提交
87 88 89 90
    }                                                           \
    F_HELPER(name, q)                                           \
    {                                                           \
        QT0 = float128_ ## name (QT0, QT1, &env->fp_status);    \
91 92 93 94 95 96 97 98
    }

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

99
void helper_fsmuld(float32 src1, float32 src2)
B
blueswir1 已提交
100
{
101 102
    DT0 = float64_mul(float32_to_float64(src1, &env->fp_status),
                      float32_to_float64(src2, &env->fp_status),
103 104
                      &env->fp_status);
}
B
blueswir1 已提交
105

B
blueswir1 已提交
106 107 108 109 110 111 112
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 已提交
113
float32 helper_fnegs(float32 src)
114
{
B
blueswir1 已提交
115
    return float32_chs(src);
116 117
}

118 119
#ifdef TARGET_SPARC64
F_HELPER(neg, d)
120
{
121
    DT0 = float64_chs(DT1);
122
}
B
blueswir1 已提交
123 124 125 126 127 128

F_HELPER(neg, q)
{
    QT0 = float128_chs(QT1);
}
#endif
129 130

/* Integer to float conversion.  */
B
blueswir1 已提交
131
float32 helper_fitos(int32_t src)
B
bellard 已提交
132
{
B
blueswir1 已提交
133
    return int32_to_float32(src, &env->fp_status);
B
bellard 已提交
134 135
}

136
void helper_fitod(int32_t src)
B
bellard 已提交
137
{
138
    DT0 = int32_to_float64(src, &env->fp_status);
B
bellard 已提交
139
}
140

141
void helper_fitoq(int32_t src)
B
blueswir1 已提交
142
{
143
    QT0 = int32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
144 145
}

B
blueswir1 已提交
146
#ifdef TARGET_SPARC64
147
float32 helper_fxtos(void)
B
blueswir1 已提交
148
{
149
    return int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
B
blueswir1 已提交
150 151
}

152
F_HELPER(xto, d)
B
blueswir1 已提交
153 154 155
{
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
}
B
blueswir1 已提交
156

B
blueswir1 已提交
157 158 159 160 161
F_HELPER(xto, q)
{
    QT0 = int64_to_float128(*((int64_t *)&DT1), &env->fp_status);
}
#endif
162 163 164
#undef F_HELPER

/* floating point conversion */
165
float32 helper_fdtos(void)
166
{
167
    return float64_to_float32(DT1, &env->fp_status);
168 169
}

170
void helper_fstod(float32 src)
171
{
172
    DT0 = float32_to_float64(src, &env->fp_status);
173
}
174

175
float32 helper_fqtos(void)
B
blueswir1 已提交
176
{
177
    return float128_to_float32(QT1, &env->fp_status);
B
blueswir1 已提交
178 179
}

180
void helper_fstoq(float32 src)
B
blueswir1 已提交
181
{
182
    QT0 = float32_to_float128(src, &env->fp_status);
B
blueswir1 已提交
183 184 185 186 187 188 189 190 191 192 193 194
}

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

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

195
/* Float to integer conversion.  */
B
blueswir1 已提交
196
int32_t helper_fstoi(float32 src)
197
{
B
blueswir1 已提交
198
    return float32_to_int32_round_to_zero(src, &env->fp_status);
199 200
}

201
int32_t helper_fdtoi(void)
202
{
203
    return float64_to_int32_round_to_zero(DT1, &env->fp_status);
204 205
}

206
int32_t helper_fqtoi(void)
B
blueswir1 已提交
207
{
208
    return float128_to_int32_round_to_zero(QT1, &env->fp_status);
B
blueswir1 已提交
209 210
}

211
#ifdef TARGET_SPARC64
212
void helper_fstox(float32 src)
213
{
214
    *((int64_t *)&DT0) = float32_to_int64_round_to_zero(src, &env->fp_status);
215 216 217 218 219 220 221
}

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

B
blueswir1 已提交
222 223 224 225 226
void helper_fqtox(void)
{
    *((int64_t *)&DT0) = float128_to_int64_round_to_zero(QT1, &env->fp_status);
}

227 228 229 230 231
void helper_faligndata(void)
{
    uint64_t tmp;

    tmp = (*((uint64_t *)&DT0)) << ((env->gsr & 7) * 8);
B
blueswir1 已提交
232 233 234 235
    /* on many architectures a shift of 64 does nothing */
    if ((env->gsr & 7) != 0) {
        tmp |= (*((uint64_t *)&DT1)) >> (64 - (env->gsr & 7) * 8);
    }
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 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
    *((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;
456 457 458 459
    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;
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

    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 已提交
480
    uint32_t name##16s(uint32_t src1, uint32_t src2)    \
481 482 483
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
484 485
        s.l = src1;                                     \
        d.l = src2;                                     \
486 487 488 489
                                                        \
        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 已提交
490
        return d.l;                                     \
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    }                                                   \
                                                        \
    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 已提交
506
    uint32_t name##32s(uint32_t src1, uint32_t src2)    \
507 508 509
    {                                                   \
        vis32 s, d;                                     \
                                                        \
B
blueswir1 已提交
510 511
        s.l = src1;                                     \
        d.l = src2;                                     \
512 513 514
                                                        \
        d.l = F(d.l, s.l);                              \
                                                        \
B
blueswir1 已提交
515
        return d.l;                                     \
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
    }

#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 已提交
597
float32 helper_fabss(float32 src)
598
{
B
blueswir1 已提交
599
    return float32_abs(src);
600 601
}

B
bellard 已提交
602
#ifdef TARGET_SPARC64
603
void helper_fabsd(void)
B
bellard 已提交
604 605 606
{
    DT0 = float64_abs(DT1);
}
B
blueswir1 已提交
607 608 609 610 611 612

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

B
blueswir1 已提交
614
float32 helper_fsqrts(float32 src)
615
{
B
blueswir1 已提交
616
    return float32_sqrt(src, &env->fp_status);
617 618
}

619
void helper_fsqrtd(void)
620
{
B
bellard 已提交
621
    DT0 = float64_sqrt(DT1, &env->fp_status);
622 623
}

B
blueswir1 已提交
624 625 626 627 628
void helper_fsqrtq(void)
{
    QT0 = float128_sqrt(QT1, &env->fp_status);
}

629
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
630
    void glue(helper_, name) (void)                                     \
B
bellard 已提交
631
    {                                                                   \
B
blueswir1 已提交
632 633
        target_ulong new_fsr;                                           \
                                                                        \
B
bellard 已提交
634 635 636
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
B
blueswir1 已提交
637
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
638
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
B
blueswir1 已提交
639
                env->fsr |= new_fsr;                                    \
640 641
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
B
bellard 已提交
642 643 644 645 646 647
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
B
blueswir1 已提交
648
            new_fsr = FSR_FCC0 << FS;                                   \
B
bellard 已提交
649 650
            break;                                                      \
        case float_relation_greater:                                    \
B
blueswir1 已提交
651
            new_fsr = FSR_FCC1 << FS;                                   \
B
bellard 已提交
652 653
            break;                                                      \
        default:                                                        \
B
blueswir1 已提交
654
            new_fsr = 0;                                                \
B
bellard 已提交
655 656
            break;                                                      \
        }                                                               \
B
blueswir1 已提交
657
        env->fsr |= new_fsr;                                            \
658
    }
B
blueswir1 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
#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;                                            \
    }
689

B
blueswir1 已提交
690
GEN_FCMPS(fcmps, float32, 0, 0);
691 692
GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);

B
blueswir1 已提交
693
GEN_FCMPS(fcmpes, float32, 0, 1);
694
GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
B
bellard 已提交
695

B
blueswir1 已提交
696 697 698
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);

B
bellard 已提交
699
#ifdef TARGET_SPARC64
B
blueswir1 已提交
700
GEN_FCMPS(fcmps_fcc1, float32, 22, 0);
701
GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
B
blueswir1 已提交
702
GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
703

B
blueswir1 已提交
704
GEN_FCMPS(fcmps_fcc2, float32, 24, 0);
705
GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
B
blueswir1 已提交
706
GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
707

B
blueswir1 已提交
708
GEN_FCMPS(fcmps_fcc3, float32, 26, 0);
709
GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
B
blueswir1 已提交
710
GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
711

B
blueswir1 已提交
712
GEN_FCMPS(fcmpes_fcc1, float32, 22, 1);
713
GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
B
blueswir1 已提交
714
GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
B
bellard 已提交
715

B
blueswir1 已提交
716
GEN_FCMPS(fcmpes_fcc2, float32, 24, 1);
717
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
B
blueswir1 已提交
718
GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
B
bellard 已提交
719

B
blueswir1 已提交
720
GEN_FCMPS(fcmpes_fcc3, float32, 26, 1);
721
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
B
blueswir1 已提交
722 723
GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
#endif
B
blueswir1 已提交
724
#undef GEN_FCMPS
B
bellard 已提交
725

B
blueswir1 已提交
726 727
#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
    defined(DEBUG_MXCC)
728 729 730
static void dump_mxcc(CPUState *env)
{
    printf("mxccdata: %016llx %016llx %016llx %016llx\n",
B
blueswir1 已提交
731 732
           env->mxccdata[0], env->mxccdata[1],
           env->mxccdata[2], env->mxccdata[3]);
733 734
    printf("mxccregs: %016llx %016llx %016llx %016llx\n"
           "          %016llx %016llx %016llx %016llx\n",
B
blueswir1 已提交
735 736 737 738
           env->mxccregs[0], env->mxccregs[1],
           env->mxccregs[2], env->mxccregs[3],
           env->mxccregs[4], env->mxccregs[5],
           env->mxccregs[6], env->mxccregs[7]);
739 740 741
}
#endif

B
blueswir1 已提交
742 743 744 745
#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)
746 747 748 749
{
    switch (size)
    {
    case 1:
B
blueswir1 已提交
750 751
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xff);
752 753
        break;
    case 2:
B
blueswir1 已提交
754 755
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffff);
756 757
        break;
    case 4:
B
blueswir1 已提交
758 759
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffffffff);
760 761
        break;
    case 8:
B
blueswir1 已提交
762 763
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
                    addr, asi, r1);
764 765 766 767 768
        break;
    }
}
#endif

B
blueswir1 已提交
769 770 771
#ifndef TARGET_SPARC64
#ifndef CONFIG_USER_ONLY
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
772
{
B
blueswir1 已提交
773
    uint64_t ret = 0;
774
#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
B
blueswir1 已提交
775
    uint32_t last_addr = addr;
776
#endif
B
bellard 已提交
777

778
    helper_check_align(addr, size - 1);
B
bellard 已提交
779
    switch (asi) {
780
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
781
        switch (addr) {
782
        case 0x01c00a00: /* MXCC control register */
B
blueswir1 已提交
783 784 785
            if (size == 8)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
786 787
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
788 789 790 791 792
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
793 794
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
795
            break;
796 797
        case 0x01c00c00: /* Module reset register */
            if (size == 8) {
B
blueswir1 已提交
798
                ret = env->mxccregs[5];
799 800
                // should we do something here?
            } else
B
blueswir1 已提交
801 802
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
803
            break;
804
        case 0x01c00f00: /* MBus port address register */
B
blueswir1 已提交
805 806 807
            if (size == 8)
                ret = env->mxccregs[7];
            else
B
blueswir1 已提交
808 809
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
810 811
            break;
        default:
B
blueswir1 已提交
812 813
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
814 815
            break;
        }
B
blueswir1 已提交
816
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
817
                     "addr = %08x -> ret = %" PRIx64 ","
B
blueswir1 已提交
818
                     "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
819 820 821
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
822
        break;
823
    case 3: /* MMU probe */
B
blueswir1 已提交
824 825 826
        {
            int mmulev;

B
blueswir1 已提交
827
            mmulev = (addr >> 8) & 15;
B
blueswir1 已提交
828 829
            if (mmulev > 4)
                ret = 0;
B
blueswir1 已提交
830 831 832 833
            else
                ret = mmu_probe(env, addr, mmulev);
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
                        addr, mmulev, ret);
B
blueswir1 已提交
834 835
        }
        break;
836
    case 4: /* read MMU regs */
B
blueswir1 已提交
837
        {
B
blueswir1 已提交
838
            int reg = (addr >> 8) & 0x1f;
839

B
blueswir1 已提交
840 841
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
B
blueswir1 已提交
842 843 844 845 846
                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 已提交
847
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
B
blueswir1 已提交
848 849
        }
        break;
B
blueswir1 已提交
850 851 852 853
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
854 855 856
    case 9: /* Supervisor code access */
        switch(size) {
        case 1:
B
blueswir1 已提交
857
            ret = ldub_code(addr);
858 859
            break;
        case 2:
860
            ret = lduw_code(addr);
861 862 863
            break;
        default:
        case 4:
864
            ret = ldl_code(addr);
865 866
            break;
        case 8:
867
            ret = ldq_code(addr);
868 869 870
            break;
        }
        break;
871 872 873
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
874
            ret = ldub_user(addr);
875 876
            break;
        case 2:
877
            ret = lduw_user(addr);
878 879 880
            break;
        default:
        case 4:
881
            ret = ldl_user(addr);
882 883
            break;
        case 8:
884
            ret = ldq_user(addr);
885 886 887 888 889 890
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
891
            ret = ldub_kernel(addr);
892 893
            break;
        case 2:
894
            ret = lduw_kernel(addr);
895 896 897
            break;
        default:
        case 4:
898
            ret = ldl_kernel(addr);
899 900
            break;
        case 8:
901
            ret = ldq_kernel(addr);
902 903 904
            break;
        }
        break;
905 906 907 908 909 910
    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 已提交
911 912
        switch(size) {
        case 1:
B
blueswir1 已提交
913
            ret = ldub_phys(addr);
B
bellard 已提交
914 915
            break;
        case 2:
916
            ret = lduw_phys(addr);
B
bellard 已提交
917 918 919
            break;
        default:
        case 4:
920
            ret = ldl_phys(addr);
B
bellard 已提交
921
            break;
B
bellard 已提交
922
        case 8:
923
            ret = ldq_phys(addr);
B
blueswir1 已提交
924
            break;
B
bellard 已提交
925
        }
B
blueswir1 已提交
926
        break;
927
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
928 929
        switch(size) {
        case 1:
B
blueswir1 已提交
930
            ret = ldub_phys((target_phys_addr_t)addr
931 932 933
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 2:
934
            ret = lduw_phys((target_phys_addr_t)addr
935 936 937 938
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        default:
        case 4:
939
            ret = ldl_phys((target_phys_addr_t)addr
940 941 942
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 8:
943
            ret = ldq_phys((target_phys_addr_t)addr
944
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
B
blueswir1 已提交
945
            break;
946
        }
B
blueswir1 已提交
947
        break;
B
blueswir1 已提交
948 949 950
    case 0x30: // Turbosparc secondary cache diagnostic
    case 0x31: // Turbosparc RAM snoop
    case 0x32: // Turbosparc page table descriptor diagnostic
B
blueswir1 已提交
951 952 953
    case 0x39: /* data cache diagnostic register */
        ret = 0;
        break;
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    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 已提交
976
    case 8: /* User code access, XXX */
977
    default:
978
        do_unassigned_access(addr, 0, 0, asi, size);
B
blueswir1 已提交
979 980
        ret = 0;
        break;
981
    }
982 983 984
    if (sign) {
        switch(size) {
        case 1:
B
blueswir1 已提交
985
            ret = (int8_t) ret;
B
blueswir1 已提交
986
            break;
987
        case 2:
B
blueswir1 已提交
988 989 990 991
            ret = (int16_t) ret;
            break;
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
992
            break;
993 994 995 996
        default:
            break;
        }
    }
997
#ifdef DEBUG_ASI
B
blueswir1 已提交
998
    dump_asi("read ", last_addr, asi, size, ret);
999
#endif
B
blueswir1 已提交
1000
    return ret;
1001 1002
}

B
blueswir1 已提交
1003
void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
1004
{
1005
    helper_check_align(addr, size - 1);
1006
    switch(asi) {
1007
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
1008
        switch (addr) {
1009 1010
        case 0x01c00000: /* MXCC stream data register 0 */
            if (size == 8)
B
blueswir1 已提交
1011
                env->mxccdata[0] = val;
1012
            else
B
blueswir1 已提交
1013 1014
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1015 1016 1017
            break;
        case 0x01c00008: /* MXCC stream data register 1 */
            if (size == 8)
B
blueswir1 已提交
1018
                env->mxccdata[1] = val;
1019
            else
B
blueswir1 已提交
1020 1021
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1022 1023 1024
            break;
        case 0x01c00010: /* MXCC stream data register 2 */
            if (size == 8)
B
blueswir1 已提交
1025
                env->mxccdata[2] = val;
1026
            else
B
blueswir1 已提交
1027 1028
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1029 1030 1031
            break;
        case 0x01c00018: /* MXCC stream data register 3 */
            if (size == 8)
B
blueswir1 已提交
1032
                env->mxccdata[3] = val;
1033
            else
B
blueswir1 已提交
1034 1035
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1036 1037 1038
            break;
        case 0x01c00100: /* MXCC stream source */
            if (size == 8)
B
blueswir1 已提交
1039
                env->mxccregs[0] = val;
1040
            else
B
blueswir1 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
                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);
1051 1052 1053
            break;
        case 0x01c00200: /* MXCC stream destination */
            if (size == 8)
B
blueswir1 已提交
1054
                env->mxccregs[1] = val;
1055
            else
B
blueswir1 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
                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]);
1066 1067 1068
            break;
        case 0x01c00a00: /* MXCC control register */
            if (size == 8)
B
blueswir1 已提交
1069
                env->mxccregs[3] = val;
1070
            else
B
blueswir1 已提交
1071 1072
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1073 1074 1075
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
1076
                env->mxccregs[3] = (env->mxccregs[3] & 0xffffffff00000000ULL)
B
blueswir1 已提交
1077
                    | val;
1078
            else
B
blueswir1 已提交
1079 1080
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1081 1082
            break;
        case 0x01c00e00: /* MXCC error register  */
1083
            // writing a 1 bit clears the error
1084
            if (size == 8)
B
blueswir1 已提交
1085
                env->mxccregs[6] &= ~val;
1086
            else
B
blueswir1 已提交
1087 1088
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1089 1090 1091
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8)
B
blueswir1 已提交
1092
                env->mxccregs[7] = val;
1093
            else
B
blueswir1 已提交
1094 1095
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
                             size);
1096 1097
            break;
        default:
B
blueswir1 已提交
1098 1099
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
                         size);
1100 1101
            break;
        }
1102 1103
        DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %" PRIx64 "\n",
                     asi, size, addr, val);
1104 1105 1106
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
1107
        break;
1108
    case 3: /* MMU flush */
B
blueswir1 已提交
1109 1110
        {
            int mmulev;
B
bellard 已提交
1111

B
blueswir1 已提交
1112
            mmulev = (addr >> 8) & 15;
1113
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
B
blueswir1 已提交
1114 1115
            switch (mmulev) {
            case 0: // flush page
B
blueswir1 已提交
1116
                tlb_flush_page(env, addr & 0xfffff000);
B
blueswir1 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
                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 已提交
1127
#ifdef DEBUG_MMU
B
blueswir1 已提交
1128
            dump_mmu(env);
B
bellard 已提交
1129
#endif
B
blueswir1 已提交
1130
        }
1131
        break;
1132
    case 4: /* write MMU regs */
B
blueswir1 已提交
1133
        {
B
blueswir1 已提交
1134
            int reg = (addr >> 8) & 0x1f;
B
blueswir1 已提交
1135
            uint32_t oldreg;
1136

B
blueswir1 已提交
1137
            oldreg = env->mmuregs[reg];
B
bellard 已提交
1138
            switch(reg) {
1139
            case 0: // Control Register
B
blueswir1 已提交
1140
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
B
blueswir1 已提交
1141
                                    (val & 0x00ffffff);
B
blueswir1 已提交
1142 1143
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
1144 1145
                if ((oldreg & (MMU_E | MMU_NF | env->def->mmu_bm)) !=
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->def->mmu_bm)))
B
bellard 已提交
1146 1147
                    tlb_flush(env, 1);
                break;
1148
            case 1: // Context Table Pointer Register
1149
                env->mmuregs[reg] = val & env->def->mmu_ctpr_mask;
1150 1151
                break;
            case 2: // Context Register
1152
                env->mmuregs[reg] = val & env->def->mmu_cxr_mask;
B
bellard 已提交
1153 1154 1155 1156 1157 1158
                if (oldreg != env->mmuregs[reg]) {
                    /* we flush when the MMU context changes because
                       QEMU has no MMU context support */
                    tlb_flush(env, 1);
                }
                break;
1159 1160 1161 1162
            case 3: // Synchronous Fault Status Register with Clear
            case 4: // Synchronous Fault Address Register
                break;
            case 0x10: // TLB Replacement Control Register
1163
                env->mmuregs[reg] = val & env->def->mmu_trcr_mask;
B
bellard 已提交
1164
                break;
1165
            case 0x13: // Synchronous Fault Status Register with Read and Clear
1166
                env->mmuregs[3] = val & env->def->mmu_sfsr_mask;
B
blueswir1 已提交
1167
                break;
1168
            case 0x14: // Synchronous Fault Address Register
B
blueswir1 已提交
1169
                env->mmuregs[4] = val;
B
blueswir1 已提交
1170
                break;
B
bellard 已提交
1171
            default:
B
blueswir1 已提交
1172
                env->mmuregs[reg] = val;
B
bellard 已提交
1173 1174 1175
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
B
blueswir1 已提交
1176 1177
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
                            reg, oldreg, env->mmuregs[reg]);
B
bellard 已提交
1178
            }
1179
#ifdef DEBUG_MMU
B
blueswir1 已提交
1180
            dump_mmu(env);
B
bellard 已提交
1181
#endif
B
blueswir1 已提交
1182
        }
1183
        break;
B
blueswir1 已提交
1184 1185 1186 1187
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
1188 1189 1190
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1191
            stb_user(addr, val);
1192 1193
            break;
        case 2:
1194
            stw_user(addr, val);
1195 1196 1197
            break;
        default:
        case 4:
1198
            stl_user(addr, val);
1199 1200
            break;
        case 8:
1201
            stq_user(addr, val);
1202 1203 1204 1205 1206 1207
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
1208
            stb_kernel(addr, val);
1209 1210
            break;
        case 2:
1211
            stw_kernel(addr, val);
1212 1213 1214
            break;
        default:
        case 4:
1215
            stl_kernel(addr, val);
1216 1217
            break;
        case 8:
1218
            stq_kernel(addr, val);
1219 1220 1221
            break;
        }
        break;
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    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 已提交
1232
    case 0x17: /* Block copy, sta access */
B
blueswir1 已提交
1233
        {
B
blueswir1 已提交
1234 1235
            // val = src
            // addr = dst
B
blueswir1 已提交
1236
            // copy 32 bytes
1237
            unsigned int i;
B
blueswir1 已提交
1238
            uint32_t src = val & ~3, dst = addr & ~3, temp;
1239

1240 1241 1242 1243
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
B
blueswir1 已提交
1244
        }
1245
        break;
B
bellard 已提交
1246
    case 0x1f: /* Block fill, stda access */
B
blueswir1 已提交
1247
        {
B
blueswir1 已提交
1248 1249
            // addr = dst
            // fill 32 bytes with val
1250
            unsigned int i;
B
blueswir1 已提交
1251
            uint32_t dst = addr & 7;
1252 1253 1254

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
1255
        }
1256
        break;
1257
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
1258
        {
B
bellard 已提交
1259 1260
            switch(size) {
            case 1:
B
blueswir1 已提交
1261
                stb_phys(addr, val);
B
bellard 已提交
1262 1263
                break;
            case 2:
1264
                stw_phys(addr, val);
B
bellard 已提交
1265 1266 1267
                break;
            case 4:
            default:
1268
                stl_phys(addr, val);
B
bellard 已提交
1269
                break;
B
bellard 已提交
1270
            case 8:
1271
                stq_phys(addr, val);
B
bellard 已提交
1272
                break;
B
bellard 已提交
1273
            }
B
blueswir1 已提交
1274
        }
1275
        break;
B
blueswir1 已提交
1276
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
B
blueswir1 已提交
1277
        {
1278 1279
            switch(size) {
            case 1:
B
blueswir1 已提交
1280 1281
                stb_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1282 1283
                break;
            case 2:
1284
                stw_phys((target_phys_addr_t)addr
B
blueswir1 已提交
1285
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1286 1287 1288
                break;
            case 4:
            default:
1289
                stl_phys((target_phys_addr_t)addr
B
blueswir1 已提交
1290
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1291 1292
                break;
            case 8:
1293
                stq_phys((target_phys_addr_t)addr
B
blueswir1 已提交
1294
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1295 1296
                break;
            }
B
blueswir1 已提交
1297
        }
1298
        break;
B
blueswir1 已提交
1299 1300 1301
    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 已提交
1302 1303
    case 0x32: // store buffer control or Turbosparc page table
               // descriptor diagnostic
1304 1305
    case 0x36: /* I-cache flash clear */
    case 0x37: /* D-cache flash clear */
B
blueswir1 已提交
1306
    case 0x4c: /* breakpoint action */
1307
        break;
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
    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 已提交
1330
    case 8: /* User code access, XXX */
1331
    case 9: /* Supervisor code access, XXX */
1332
    default:
1333
        do_unassigned_access(addr, 1, 0, asi, size);
1334
        break;
1335
    }
1336
#ifdef DEBUG_ASI
B
blueswir1 已提交
1337
    dump_asi("write", addr, asi, size, val);
1338
#endif
1339 1340
}

1341 1342 1343 1344
#endif /* CONFIG_USER_ONLY */
#else /* TARGET_SPARC64 */

#ifdef CONFIG_USER_ONLY
B
blueswir1 已提交
1345
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1346 1347
{
    uint64_t ret = 0;
B
blueswir1 已提交
1348 1349 1350
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
1351 1352 1353 1354

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

1355
    helper_check_align(addr, size - 1);
B
blueswir1 已提交
1356
    address_mask(env, &addr);
1357

1358 1359 1360
    switch (asi) {
    case 0x82: // Primary no-fault
    case 0x8a: // Primary no-fault LE
B
blueswir1 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369
        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
1370 1371 1372
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
1373
                ret = ldub_raw(addr);
1374 1375
                break;
            case 2:
1376
                ret = lduw_raw(addr);
1377 1378
                break;
            case 4:
1379
                ret = ldl_raw(addr);
1380 1381 1382
                break;
            default:
            case 8:
1383
                ret = ldq_raw(addr);
1384 1385 1386 1387 1388 1389
                break;
            }
        }
        break;
    case 0x83: // Secondary no-fault
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398
        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
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
        // 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 已提交
1414
            break;
1415 1416
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
1417
            break;
1418 1419
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
1420
            break;
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
1433
            break;
1434 1435
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
1436
            break;
1437 1438
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1439
            break;
1440 1441 1442 1443
        default:
            break;
        }
    }
B
blueswir1 已提交
1444 1445 1446 1447
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
1448 1449
}

B
blueswir1 已提交
1450
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
1451
{
B
blueswir1 已提交
1452 1453 1454
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
1455 1456 1457
    if (asi < 0x80)
        raise_exception(TT_PRIV_ACT);

1458
    helper_check_align(addr, size - 1);
B
blueswir1 已提交
1459
    address_mask(env, &addr);
1460

1461 1462 1463 1464 1465 1466
    /* Convert to little endian */
    switch (asi) {
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
B
blueswir1 已提交
1467
            addr = bswap16(addr);
B
blueswir1 已提交
1468
            break;
1469
        case 4:
B
blueswir1 已提交
1470
            addr = bswap32(addr);
B
blueswir1 已提交
1471
            break;
1472
        case 8:
B
blueswir1 已提交
1473
            addr = bswap64(addr);
B
blueswir1 已提交
1474
            break;
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
        default:
            break;
        }
    default:
        break;
    }

    switch(asi) {
    case 0x80: // Primary
    case 0x88: // Primary LE
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
1488
                stb_raw(addr, val);
1489 1490
                break;
            case 2:
1491
                stw_raw(addr, val);
1492 1493
                break;
            case 4:
1494
                stl_raw(addr, val);
1495 1496 1497
                break;
            case 8:
            default:
1498
                stq_raw(addr, val);
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
                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:
1513
        do_unassigned_access(addr, 1, 0, 1, size);
1514 1515 1516 1517 1518
        return;
    }
}

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

B
blueswir1 已提交
1520
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
B
bellard 已提交
1521
{
B
bellard 已提交
1522
    uint64_t ret = 0;
B
blueswir1 已提交
1523 1524 1525
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
B
bellard 已提交
1526

B
blueswir1 已提交
1527
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1528 1529
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
1530
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
1531
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
1532

1533
    helper_check_align(addr, size - 1);
B
bellard 已提交
1534
    switch (asi) {
B
blueswir1 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543
    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
1544 1545 1546 1547
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
1548 1549
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
1550
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1551 1552
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
1553 1554
                switch(size) {
                case 1:
B
blueswir1 已提交
1555
                    ret = ldub_hypv(addr);
B
blueswir1 已提交
1556 1557
                    break;
                case 2:
1558
                    ret = lduw_hypv(addr);
B
blueswir1 已提交
1559 1560
                    break;
                case 4:
1561
                    ret = ldl_hypv(addr);
B
blueswir1 已提交
1562 1563 1564
                    break;
                default:
                case 8:
1565
                    ret = ldq_hypv(addr);
B
blueswir1 已提交
1566 1567 1568 1569 1570
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
1571
                    ret = ldub_kernel(addr);
B
blueswir1 已提交
1572 1573
                    break;
                case 2:
1574
                    ret = lduw_kernel(addr);
B
blueswir1 已提交
1575 1576
                    break;
                case 4:
1577
                    ret = ldl_kernel(addr);
B
blueswir1 已提交
1578 1579 1580
                    break;
                default:
                case 8:
1581
                    ret = ldq_kernel(addr);
B
blueswir1 已提交
1582 1583
                    break;
                }
1584 1585 1586 1587
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
1588
                ret = ldub_user(addr);
1589 1590
                break;
            case 2:
1591
                ret = lduw_user(addr);
1592 1593
                break;
            case 4:
1594
                ret = ldl_user(addr);
1595 1596 1597
                break;
            default:
            case 8:
1598
                ret = ldq_user(addr);
1599 1600 1601 1602
                break;
            }
        }
        break;
B
bellard 已提交
1603 1604
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
1605 1606
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
1607
        {
B
bellard 已提交
1608 1609
            switch(size) {
            case 1:
B
blueswir1 已提交
1610
                ret = ldub_phys(addr);
B
bellard 已提交
1611 1612
                break;
            case 2:
1613
                ret = lduw_phys(addr);
B
bellard 已提交
1614 1615
                break;
            case 4:
1616
                ret = ldl_phys(addr);
B
bellard 已提交
1617 1618 1619
                break;
            default:
            case 8:
1620
                ret = ldq_phys(addr);
B
bellard 已提交
1621 1622
                break;
            }
B
blueswir1 已提交
1623 1624
            break;
        }
B
blueswir1 已提交
1625 1626 1627 1628 1629
    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 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638
    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 已提交
1639 1640 1641 1642 1643
    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
1644
    case 0x81: // Secondary
B
bellard 已提交
1645
    case 0x89: // Secondary LE
B
blueswir1 已提交
1646 1647
        // XXX
        break;
B
bellard 已提交
1648
    case 0x45: // LSU
B
blueswir1 已提交
1649 1650
        ret = env->lsu;
        break;
B
bellard 已提交
1651
    case 0x50: // I-MMU regs
B
blueswir1 已提交
1652
        {
B
blueswir1 已提交
1653
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
1654

B
blueswir1 已提交
1655 1656 1657
            ret = env->immuregs[reg];
            break;
        }
B
bellard 已提交
1658 1659
    case 0x51: // I-MMU 8k TSB pointer
    case 0x52: // I-MMU 64k TSB pointer
B
blueswir1 已提交
1660 1661
        // XXX
        break;
1662 1663 1664 1665 1666 1667 1668
    case 0x55: // I-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

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

B
blueswir1 已提交
1673
            ret = env->itlb_tag[reg];
B
blueswir1 已提交
1674 1675
            break;
        }
B
bellard 已提交
1676
    case 0x58: // D-MMU regs
B
blueswir1 已提交
1677
        {
B
blueswir1 已提交
1678
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
1679

B
blueswir1 已提交
1680 1681 1682
            ret = env->dmmuregs[reg];
            break;
        }
1683 1684 1685 1686 1687 1688 1689
    case 0x5d: // D-MMU data access
        {
            int reg = (addr >> 3) & 0x3f;

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

B
blueswir1 已提交
1694
            ret = env->dtlb_tag[reg];
B
blueswir1 已提交
1695 1696
            break;
        }
1697 1698
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
1699 1700 1701
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
1702 1703 1704 1705 1706 1707 1708 1709
    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 已提交
1710 1711 1712
    case 0x59: // D-MMU 8k TSB pointer
    case 0x5a: // D-MMU 64k TSB pointer
    case 0x5b: // D-MMU data pointer
B
bellard 已提交
1713 1714 1715
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
1716 1717
        // XXX
        break;
B
bellard 已提交
1718 1719 1720 1721
    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 已提交
1722
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
1723
    default:
1724
        do_unassigned_access(addr, 0, 0, 1, size);
B
blueswir1 已提交
1725 1726
        ret = 0;
        break;
B
bellard 已提交
1727
    }
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742

    /* 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 已提交
1743
            break;
1744 1745
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
1746
            break;
1747 1748
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
1749
            break;
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
1762
            break;
1763 1764
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
1765
            break;
1766 1767
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1768
            break;
1769 1770 1771 1772
        default:
            break;
        }
    }
B
blueswir1 已提交
1773 1774 1775 1776
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
B
bellard 已提交
1777 1778
}

B
blueswir1 已提交
1779
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
B
bellard 已提交
1780
{
B
blueswir1 已提交
1781 1782 1783
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
B
blueswir1 已提交
1784
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1785 1786
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
1787
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
1788
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
1789

1790
    helper_check_align(addr, size - 1);
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
    /* 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 已提交
1802
            addr = bswap16(addr);
B
blueswir1 已提交
1803
            break;
1804
        case 4:
B
blueswir1 已提交
1805
            addr = bswap32(addr);
B
blueswir1 已提交
1806
            break;
1807
        case 8:
B
blueswir1 已提交
1808
            addr = bswap64(addr);
B
blueswir1 已提交
1809
            break;
1810 1811 1812 1813 1814 1815 1816
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
1817
    switch(asi) {
1818 1819 1820 1821
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
B
blueswir1 已提交
1822 1823
    case 0xe2: // UA2007 Primary block init
    case 0xe3: // UA2007 Secondary block init
1824
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1825 1826
            if ((env->def->features & CPU_FEATURE_HYPV)
                && env->hpstate & HS_PRIV) {
B
blueswir1 已提交
1827 1828
                switch(size) {
                case 1:
B
blueswir1 已提交
1829
                    stb_hypv(addr, val);
B
blueswir1 已提交
1830 1831
                    break;
                case 2:
1832
                    stw_hypv(addr, val);
B
blueswir1 已提交
1833 1834
                    break;
                case 4:
1835
                    stl_hypv(addr, val);
B
blueswir1 已提交
1836 1837 1838
                    break;
                case 8:
                default:
1839
                    stq_hypv(addr, val);
B
blueswir1 已提交
1840 1841 1842 1843 1844
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
1845
                    stb_kernel(addr, val);
B
blueswir1 已提交
1846 1847
                    break;
                case 2:
1848
                    stw_kernel(addr, val);
B
blueswir1 已提交
1849 1850
                    break;
                case 4:
1851
                    stl_kernel(addr, val);
B
blueswir1 已提交
1852 1853 1854
                    break;
                case 8:
                default:
1855
                    stq_kernel(addr, val);
B
blueswir1 已提交
1856 1857
                    break;
                }
1858 1859 1860 1861
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
1862
                stb_user(addr, val);
1863 1864
                break;
            case 2:
1865
                stw_user(addr, val);
1866 1867
                break;
            case 4:
1868
                stl_user(addr, val);
1869 1870 1871
                break;
            case 8:
            default:
1872
                stq_user(addr, val);
1873 1874 1875 1876
                break;
            }
        }
        break;
B
bellard 已提交
1877 1878
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
1879 1880
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
1881
        {
B
bellard 已提交
1882 1883
            switch(size) {
            case 1:
B
blueswir1 已提交
1884
                stb_phys(addr, val);
B
bellard 已提交
1885 1886
                break;
            case 2:
1887
                stw_phys(addr, val);
B
bellard 已提交
1888 1889
                break;
            case 4:
1890
                stl_phys(addr, val);
B
bellard 已提交
1891 1892 1893
                break;
            case 8:
            default:
1894
                stq_phys(addr, val);
B
bellard 已提交
1895 1896
                break;
            }
B
blueswir1 已提交
1897 1898
        }
        return;
B
blueswir1 已提交
1899 1900 1901 1902 1903
    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 已提交
1904 1905 1906 1907 1908
    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 已提交
1909
    case 0x81: // Secondary
B
bellard 已提交
1910
    case 0x89: // Secondary LE
B
blueswir1 已提交
1911 1912
        // XXX
        return;
B
bellard 已提交
1913
    case 0x45: // LSU
B
blueswir1 已提交
1914 1915 1916 1917
        {
            uint64_t oldreg;

            oldreg = env->lsu;
B
blueswir1 已提交
1918
            env->lsu = val & (DMMU_E | IMMU_E);
B
blueswir1 已提交
1919 1920 1921
            // Mappings generated during D/I MMU disabled mode are
            // invalid in normal mode
            if (oldreg != env->lsu) {
B
blueswir1 已提交
1922 1923
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
                            oldreg, env->lsu);
B
bellard 已提交
1924
#ifdef DEBUG_MMU
B
blueswir1 已提交
1925
                dump_mmu(env);
B
bellard 已提交
1926
#endif
B
blueswir1 已提交
1927 1928 1929 1930
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
1931
    case 0x50: // I-MMU regs
B
blueswir1 已提交
1932
        {
B
blueswir1 已提交
1933
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
1934
            uint64_t oldreg;
1935

B
blueswir1 已提交
1936
            oldreg = env->immuregs[reg];
B
bellard 已提交
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
            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 已提交
1947 1948
                if ((val & 1) == 0)
                    val = 0; // Clear SFSR
B
bellard 已提交
1949 1950 1951 1952 1953 1954
                break;
            case 5: // TSB access
            case 6: // Tag access
            default:
                break;
            }
B
blueswir1 已提交
1955
            env->immuregs[reg] = val;
B
bellard 已提交
1956
            if (oldreg != env->immuregs[reg]) {
B
blueswir1 已提交
1957 1958
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
                            PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
B
bellard 已提交
1959
            }
1960
#ifdef DEBUG_MMU
B
blueswir1 已提交
1961
            dump_mmu(env);
B
bellard 已提交
1962
#endif
B
blueswir1 已提交
1963 1964
            return;
        }
B
bellard 已提交
1965
    case 0x54: // I-MMU data in
B
blueswir1 已提交
1966 1967 1968 1969 1970 1971 1972
        {
            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 已提交
1973
                    env->itlb_tte[i] = val;
B
blueswir1 已提交
1974 1975 1976 1977 1978 1979 1980
                    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 已提交
1981
                    env->itlb_tte[i] = val;
B
blueswir1 已提交
1982 1983 1984 1985 1986 1987
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
1988
    case 0x55: // I-MMU data access
B
blueswir1 已提交
1989
        {
1990 1991
            // TODO: auto demap

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

B
blueswir1 已提交
1994
            env->itlb_tag[i] = env->immuregs[6];
B
blueswir1 已提交
1995
            env->itlb_tte[i] = val;
B
blueswir1 已提交
1996 1997
            return;
        }
B
bellard 已提交
1998
    case 0x57: // I-MMU demap
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
        {
            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 已提交
2015
        return;
B
bellard 已提交
2016
    case 0x58: // D-MMU regs
B
blueswir1 已提交
2017
        {
B
blueswir1 已提交
2018
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
2019
            uint64_t oldreg;
2020

B
blueswir1 已提交
2021
            oldreg = env->dmmuregs[reg];
B
bellard 已提交
2022 2023 2024 2025 2026
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
B
blueswir1 已提交
2027 2028
                if ((val & 1) == 0) {
                    val = 0; // Clear SFSR, Fault address
B
blueswir1 已提交
2029 2030
                    env->dmmuregs[4] = 0;
                }
B
blueswir1 已提交
2031
                env->dmmuregs[reg] = val;
B
bellard 已提交
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
                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 已提交
2042
            env->dmmuregs[reg] = val;
B
bellard 已提交
2043
            if (oldreg != env->dmmuregs[reg]) {
B
blueswir1 已提交
2044 2045
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
                            PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
B
bellard 已提交
2046
            }
2047
#ifdef DEBUG_MMU
B
blueswir1 已提交
2048
            dump_mmu(env);
B
bellard 已提交
2049
#endif
B
blueswir1 已提交
2050 2051
            return;
        }
B
bellard 已提交
2052
    case 0x5c: // D-MMU data in
B
blueswir1 已提交
2053 2054 2055 2056 2057 2058 2059
        {
            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 已提交
2060
                    env->dtlb_tte[i] = val;
B
blueswir1 已提交
2061 2062 2063 2064 2065 2066 2067
                    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 已提交
2068
                    env->dtlb_tte[i] = val;
B
blueswir1 已提交
2069 2070 2071 2072 2073 2074
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
2075
    case 0x5d: // D-MMU data access
B
blueswir1 已提交
2076
        {
B
blueswir1 已提交
2077
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
2078

B
blueswir1 已提交
2079
            env->dtlb_tag[i] = env->dmmuregs[6];
B
blueswir1 已提交
2080
            env->dtlb_tte[i] = val;
B
blueswir1 已提交
2081 2082
            return;
        }
B
bellard 已提交
2083
    case 0x5f: // D-MMU demap
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
        {
            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 已提交
2101
    case 0x49: // Interrupt data receive
B
blueswir1 已提交
2102 2103
        // XXX
        return;
2104 2105
    case 0x46: // D-cache data
    case 0x47: // D-cache tag access
2106 2107 2108
    case 0x4b: // E-cache error enable
    case 0x4c: // E-cache asynchronous fault status
    case 0x4d: // E-cache asynchronous fault address
2109 2110 2111 2112 2113 2114 2115 2116
    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 已提交
2117 2118 2119 2120 2121 2122 2123
    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 已提交
2124 2125 2126 2127 2128 2129
    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 已提交
2130
    default:
2131
        do_unassigned_access(addr, 1, 0, 1, size);
B
blueswir1 已提交
2132
        return;
B
bellard 已提交
2133 2134
    }
}
2135
#endif /* CONFIG_USER_ONLY */
2136

B
blueswir1 已提交
2137 2138 2139
void helper_ldda_asi(target_ulong addr, int asi, int rd)
{
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2140 2141
        || ((env->def->features & CPU_FEATURE_HYPV)
            && asi >= 0x30 && asi < 0x80
2142
            && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
        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 已提交
2184
void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
2185 2186
{
    unsigned int i;
B
blueswir1 已提交
2187
    target_ulong val;
2188

2189
    helper_check_align(addr, 3);
2190 2191 2192 2193 2194
    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 已提交
2195 2196 2197 2198
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2199
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2200
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2201 2202
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4,
                                                         0);
B
blueswir1 已提交
2203
            addr += 4;
2204 2205 2206 2207 2208 2209 2210
        }

        return;
    default:
        break;
    }

B
blueswir1 已提交
2211
    val = helper_ld_asi(addr, asi, size, 0);
2212 2213 2214
    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2215
        *((uint32_t *)&env->fpr[rd]) = val;
2216 2217
        break;
    case 8:
B
blueswir1 已提交
2218
        *((int64_t *)&DT0) = val;
2219
        break;
B
blueswir1 已提交
2220 2221 2222
    case 16:
        // XXX
        break;
2223 2224 2225
    }
}

B
blueswir1 已提交
2226
void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
2227 2228
{
    unsigned int i;
B
blueswir1 已提交
2229
    target_ulong val = 0;
2230

2231
    helper_check_align(addr, 3);
2232
    switch (asi) {
B
blueswir1 已提交
2233 2234
    case 0xe0: // UA2007 Block commit store primary (cache flush)
    case 0xe1: // UA2007 Block commit store secondary (cache flush)
2235 2236 2237 2238
    case 0xf0: // Block store primary
    case 0xf1: // Block store secondary
    case 0xf8: // Block store primary LE
    case 0xf9: // Block store secondary LE
B
blueswir1 已提交
2239 2240 2241 2242
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
2243
        helper_check_align(addr, 0x3f);
B
blueswir1 已提交
2244
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
2245 2246 2247
            val = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(addr, val, asi & 0x8f, 4);
            addr += 4;
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
        }

        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
2258
        val = *((uint32_t *)&env->fpr[rd]);
2259 2260
        break;
    case 8:
B
blueswir1 已提交
2261
        val = *((int64_t *)&DT0);
2262
        break;
B
blueswir1 已提交
2263 2264 2265
    case 16:
        // XXX
        break;
2266
    }
B
blueswir1 已提交
2267 2268 2269 2270 2271 2272 2273 2274
    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;

2275
    val2 &= 0xffffffffUL;
B
blueswir1 已提交
2276 2277
    ret = helper_ld_asi(addr, asi, 4, 0);
    ret &= 0xffffffffUL;
2278 2279
    if (val2 == ret)
        helper_st_asi(addr, val1 & 0xffffffffUL, asi, 4);
B
blueswir1 已提交
2280
    return ret;
2281 2282
}

B
blueswir1 已提交
2283 2284 2285 2286 2287 2288
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);
2289 2290
    if (val2 == ret)
        helper_st_asi(addr, val1, asi, 8);
B
blueswir1 已提交
2291 2292
    return ret;
}
2293
#endif /* TARGET_SPARC64 */
B
bellard 已提交
2294 2295

#ifndef TARGET_SPARC64
B
blueswir1 已提交
2296
void helper_rett(void)
2297
{
2298 2299
    unsigned int cwp;

2300 2301 2302
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

2303
    env->psret = 1;
2304
    cwp = cpu_cwp_inc(env, env->cwp + 1) ;
2305 2306 2307 2308 2309 2310
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
2311
#endif
2312

B
blueswir1 已提交
2313 2314 2315 2316 2317
target_ulong helper_udiv(target_ulong a, target_ulong b)
{
    uint64_t x0;
    uint32_t x1;

2318
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
    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;

2340
    x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
B
blueswir1 已提交
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
    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 已提交
2357 2358
void helper_stdf(target_ulong addr, int mem_idx)
{
2359
    helper_check_align(addr, 7);
B
blueswir1 已提交
2360 2361 2362
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2363
        stfq_user(addr, DT0);
B
blueswir1 已提交
2364 2365
        break;
    case 1:
2366
        stfq_kernel(addr, DT0);
B
blueswir1 已提交
2367 2368 2369
        break;
#ifdef TARGET_SPARC64
    case 2:
2370
        stfq_hypv(addr, DT0);
B
blueswir1 已提交
2371 2372 2373 2374 2375 2376
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2377
    address_mask(env, &addr);
2378
    stfq_raw(addr, DT0);
B
blueswir1 已提交
2379 2380 2381 2382 2383
#endif
}

void helper_lddf(target_ulong addr, int mem_idx)
{
2384
    helper_check_align(addr, 7);
B
blueswir1 已提交
2385 2386 2387
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2388
        DT0 = ldfq_user(addr);
B
blueswir1 已提交
2389 2390
        break;
    case 1:
2391
        DT0 = ldfq_kernel(addr);
B
blueswir1 已提交
2392 2393 2394
        break;
#ifdef TARGET_SPARC64
    case 2:
2395
        DT0 = ldfq_hypv(addr);
B
blueswir1 已提交
2396 2397 2398 2399 2400 2401
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2402
    address_mask(env, &addr);
2403
    DT0 = ldfq_raw(addr);
B
blueswir1 已提交
2404 2405 2406
#endif
}

B
blueswir1 已提交
2407
void helper_ldqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
2408 2409 2410 2411
{
    // XXX add 128 bit load
    CPU_QuadU u;

2412
    helper_check_align(addr, 7);
B
blueswir1 已提交
2413 2414 2415
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
2416 2417
        u.ll.upper = ldq_user(addr);
        u.ll.lower = ldq_user(addr + 8);
B
blueswir1 已提交
2418 2419 2420
        QT0 = u.q;
        break;
    case 1:
2421 2422
        u.ll.upper = ldq_kernel(addr);
        u.ll.lower = ldq_kernel(addr + 8);
B
blueswir1 已提交
2423 2424 2425 2426
        QT0 = u.q;
        break;
#ifdef TARGET_SPARC64
    case 2:
2427 2428
        u.ll.upper = ldq_hypv(addr);
        u.ll.lower = ldq_hypv(addr + 8);
B
blueswir1 已提交
2429 2430 2431 2432 2433 2434 2435
        QT0 = u.q;
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2436
    address_mask(env, &addr);
2437 2438
    u.ll.upper = ldq_raw(addr);
    u.ll.lower = ldq_raw((addr + 8) & 0xffffffffULL);
B
blueswir1 已提交
2439
    QT0 = u.q;
B
blueswir1 已提交
2440
#endif
B
blueswir1 已提交
2441 2442
}

B
blueswir1 已提交
2443
void helper_stqf(target_ulong addr, int mem_idx)
B
blueswir1 已提交
2444 2445 2446 2447
{
    // XXX add 128 bit store
    CPU_QuadU u;

2448
    helper_check_align(addr, 7);
B
blueswir1 已提交
2449 2450 2451 2452
#if !defined(CONFIG_USER_ONLY)
    switch (mem_idx) {
    case 0:
        u.q = QT0;
2453 2454
        stq_user(addr, u.ll.upper);
        stq_user(addr + 8, u.ll.lower);
B
blueswir1 已提交
2455 2456 2457
        break;
    case 1:
        u.q = QT0;
2458 2459
        stq_kernel(addr, u.ll.upper);
        stq_kernel(addr + 8, u.ll.lower);
B
blueswir1 已提交
2460 2461 2462 2463
        break;
#ifdef TARGET_SPARC64
    case 2:
        u.q = QT0;
2464 2465
        stq_hypv(addr, u.ll.upper);
        stq_hypv(addr + 8, u.ll.lower);
B
blueswir1 已提交
2466 2467 2468 2469 2470 2471
        break;
#endif
    default:
        break;
    }
#else
B
blueswir1 已提交
2472
    u.q = QT0;
B
blueswir1 已提交
2473
    address_mask(env, &addr);
2474 2475
    stq_raw(addr, u.ll.upper);
    stq_raw((addr + 8) & 0xffffffffULL, u.ll.lower);
B
blueswir1 已提交
2476
#endif
B
blueswir1 已提交
2477
}
B
blueswir1 已提交
2478

2479
static inline void set_fsr(void)
2480
{
B
bellard 已提交
2481
    int rnd_mode;
B
blueswir1 已提交
2482

2483 2484
    switch (env->fsr & FSR_RD_MASK) {
    case FSR_RD_NEAREST:
B
bellard 已提交
2485
        rnd_mode = float_round_nearest_even;
B
blueswir1 已提交
2486
        break;
B
bellard 已提交
2487
    default:
2488
    case FSR_RD_ZERO:
B
bellard 已提交
2489
        rnd_mode = float_round_to_zero;
B
blueswir1 已提交
2490
        break;
2491
    case FSR_RD_POS:
B
bellard 已提交
2492
        rnd_mode = float_round_up;
B
blueswir1 已提交
2493
        break;
2494
    case FSR_RD_NEG:
B
bellard 已提交
2495
        rnd_mode = float_round_down;
B
blueswir1 已提交
2496
        break;
2497
    }
B
bellard 已提交
2498
    set_float_rounding_mode(rnd_mode, &env->fp_status);
2499
}
B
bellard 已提交
2500

2501
void helper_ldfsr(uint32_t new_fsr)
B
blueswir1 已提交
2502
{
2503 2504
    env->fsr = (new_fsr & FSR_LDFSR_MASK) | (env->fsr & FSR_LDFSR_OLDMASK);
    set_fsr();
B
blueswir1 已提交
2505 2506
}

2507 2508 2509 2510 2511 2512 2513 2514
#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 已提交
2515
void helper_debug(void)
B
bellard 已提交
2516 2517 2518 2519
{
    env->exception_index = EXCP_DEBUG;
    cpu_loop_exit();
}
2520

B
bellard 已提交
2521
#ifndef TARGET_SPARC64
2522 2523 2524 2525 2526 2527
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

2528
    cwp = cpu_cwp_dec(env, env->cwp - 1);
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_OVF);
    }
    set_cwp(cwp);
}

void helper_restore(void)
{
    uint32_t cwp;

2539
    cwp = cpu_cwp_inc(env, env->cwp + 1);
2540 2541 2542 2543 2544 2545
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
}

B
blueswir1 已提交
2546
void helper_wrpsr(target_ulong new_psr)
2547
{
2548
    if ((new_psr & PSR_CWP) >= env->nwindows)
2549 2550
        raise_exception(TT_ILL_INSN);
    else
B
blueswir1 已提交
2551
        PUT_PSR(env, new_psr);
2552 2553
}

B
blueswir1 已提交
2554
target_ulong helper_rdpsr(void)
2555
{
B
blueswir1 已提交
2556
    return GET_PSR(env);
2557
}
B
bellard 已提交
2558 2559

#else
2560 2561 2562 2563 2564 2565
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
void helper_save(void)
{
    uint32_t cwp;

2566
    cwp = cpu_cwp_dec(env, env->cwp - 1);
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
    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;

2587
    cwp = cpu_cwp_inc(env, env->cwp + 1);
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
    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)
{
2601
    if (env->cansave != env->nwindows - 2) {
2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
        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++;
2620
    if (env->cleanwin < env->nwindows - 1)
2621 2622 2623 2624 2625 2626 2627
        env->cleanwin++;
    if (env->otherwin == 0)
        env->cansave--;
    else
        env->otherwin--;
}

B
blueswir1 已提交
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
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 已提交
2649

2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
// 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 已提交
2681
target_ulong helper_popc(target_ulong val)
B
bellard 已提交
2682
{
B
blueswir1 已提交
2683
    return ctpop64(val);
B
bellard 已提交
2684
}
B
bellard 已提交
2685 2686 2687 2688 2689 2690

static inline uint64_t *get_gregset(uint64_t pstate)
{
    switch (pstate) {
    default:
    case 0:
B
blueswir1 已提交
2691
        return env->bgregs;
B
bellard 已提交
2692
    case PS_AG:
B
blueswir1 已提交
2693
        return env->agregs;
B
bellard 已提交
2694
    case PS_MG:
B
blueswir1 已提交
2695
        return env->mgregs;
B
bellard 已提交
2696
    case PS_IG:
B
blueswir1 已提交
2697
        return env->igregs;
B
bellard 已提交
2698 2699 2700
    }
}

B
blueswir1 已提交
2701
static inline void change_pstate(uint64_t new_pstate)
B
bellard 已提交
2702
{
2703
    uint64_t pstate_regs, new_pstate_regs;
B
bellard 已提交
2704 2705 2706 2707 2708
    uint64_t *src, *dst;

    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
    if (new_pstate_regs != pstate_regs) {
B
blueswir1 已提交
2709 2710 2711 2712 2713
        // 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 已提交
2714 2715 2716 2717
    }
    env->pstate = new_pstate;
}

B
blueswir1 已提交
2718
void helper_wrpstate(target_ulong new_state)
2719
{
2720
    if (!(env->def->features & CPU_FEATURE_GL))
2721
        change_pstate(new_state & 0xf3f);
2722 2723
}

B
blueswir1 已提交
2724
void helper_done(void)
B
bellard 已提交
2725
{
2726 2727 2728 2729 2730 2731
    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 已提交
2732
    env->tl--;
2733
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
B
bellard 已提交
2734 2735
}

B
blueswir1 已提交
2736
void helper_retry(void)
B
bellard 已提交
2737
{
2738 2739 2740 2741 2742 2743
    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 已提交
2744
    env->tl--;
2745
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
B
bellard 已提交
2746
}
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761

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 已提交
2762
#endif
2763

B
blueswir1 已提交
2764
void helper_flush(target_ulong addr)
2765
{
B
blueswir1 已提交
2766 2767
    addr &= ~7;
    tb_invalidate_page_range(addr, addr + 8);
2768 2769
}

B
blueswir1 已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
#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
2812
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
        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";
        }

2830
        qemu_log("%6d: %s (v=%04x) pc=%016" PRIx64 " npc=%016" PRIx64
B
blueswir1 已提交
2831 2832 2833 2834
                " SP=%016" PRIx64 "\n",
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
2835
        log_cpu_state(env, 0);
B
blueswir1 已提交
2836 2837 2838 2839 2840
#if 0
        {
            int i;
            uint8_t *ptr;

2841
            qemu_log("       code=");
B
blueswir1 已提交
2842 2843
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
2844
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
2845
            }
2846
            qemu_log("\n");
B
blueswir1 已提交
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
        }
#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;
2901
}
B
blueswir1 已提交
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
#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
2937

B
blueswir1 已提交
2938
void do_interrupt(CPUState *env)
2939
{
B
blueswir1 已提交
2940 2941 2942
    int cwp, intno = env->exception_index;

#ifdef DEBUG_PCALL
2943
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
blueswir1 已提交
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
        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";
        }

2957
        qemu_log("%6d: %s (v=%02x) pc=%08x npc=%08x SP=%08x\n",
B
blueswir1 已提交
2958 2959 2960
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
2961
        log_cpu_state(env, 0);
B
blueswir1 已提交
2962 2963 2964 2965 2966
#if 0
        {
            int i;
            uint8_t *ptr;

2967
            qemu_log("       code=");
B
blueswir1 已提交
2968 2969
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
2970
                qemu_log(" %02x", ldub(ptr + i));
B
blueswir1 已提交
2971
            }
2972
            qemu_log("\n");
B
blueswir1 已提交
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
        }
#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;
2996
}
B
blueswir1 已提交
2997
#endif
2998

2999
#if !defined(CONFIG_USER_ONLY)
3000

3001 3002 3003
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

3004
#define MMUSUFFIX _mmu
3005
#define ALIGNED_ONLY
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018

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

3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
/* 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);
        }
    }
}

3037 3038 3039
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr)
{
B
blueswir1 已提交
3040
#ifdef DEBUG_UNALIGNED
3041 3042
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
           "\n", addr, env->pc);
B
blueswir1 已提交
3043
#endif
3044
    cpu_restore_state2(retaddr);
B
blueswir1 已提交
3045
    raise_exception(TT_UNALIGNED);
3046
}
3047 3048 3049 3050 3051

/* 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 */
3052
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
3053 3054 3055 3056 3057 3058 3059 3060 3061
{
    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;

3062
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
3063
    if (ret) {
3064
        cpu_restore_state2(retaddr);
3065 3066 3067 3068 3069 3070
        cpu_loop_exit();
    }
    env = saved_env;
}

#endif
3071 3072

#ifndef TARGET_SPARC64
3073
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3074
                          int is_asi, int size)
3075 3076 3077 3078 3079 3080 3081
{
    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;
3082 3083
#ifdef DEBUG_UNASSIGNED
    if (is_asi)
3084
        printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
B
blueswir1 已提交
3085
               " asi 0x%02x from " TARGET_FMT_lx "\n",
3086 3087
               is_exec ? "exec" : is_write ? "write" : "read", size,
               size == 1 ? "" : "s", addr, is_asi, env->pc);
3088
    else
3089 3090 3091 3092
        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);
3093
#endif
3094
    if (env->mmuregs[3]) /* Fault status register */
B
blueswir1 已提交
3095
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
    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)) {
3107 3108 3109 3110
        if (is_exec)
            raise_exception(TT_CODE_ACCESS);
        else
            raise_exception(TT_DATA_ACCESS);
3111 3112 3113 3114
    }
    env = saved_env;
}
#else
3115
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
3116
                          int is_asi, int size)
3117 3118 3119 3120 3121 3122 3123 3124
{
#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 已提交
3125 3126
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
           "\n", addr, env->pc);
3127 3128
    env = saved_env;
#endif
3129 3130 3131 3132
    if (is_exec)
        raise_exception(TT_CODE_ACCESS);
    else
        raise_exception(TT_DATA_ACCESS);
3133 3134
}
#endif
3135

B
blueswir1 已提交
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
#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