op_helper.c 49.5 KB
Newer Older
1
#include "exec.h"
B
blueswir1 已提交
2
#include "host-utils.h"
3

B
bellard 已提交
4
//#define DEBUG_PCALL
B
bellard 已提交
5
//#define DEBUG_MMU
6
//#define DEBUG_MXCC
B
blueswir1 已提交
7
//#define DEBUG_UNALIGNED
8
//#define DEBUG_UNASSIGNED
B
bellard 已提交
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23
#ifdef DEBUG_MMU
#define DPRINTF_MMU(fmt, args...) \
do { printf("MMU: " fmt , ##args); } while (0)
#else
#define DPRINTF_MMU(fmt, args...)
#endif

#ifdef DEBUG_MXCC
#define DPRINTF_MXCC(fmt, args...) \
do { printf("MXCC: " fmt , ##args); } while (0)
#else
#define DPRINTF_MXCC(fmt, args...)
#endif

B
bellard 已提交
24 25 26 27
void raise_exception(int tt)
{
    env->exception_index = tt;
    cpu_loop_exit();
28
}
B
bellard 已提交
29

30 31 32 33 34
void check_ieee_exceptions()
{
     T0 = get_float_exception_flags(&env->fp_status);
     if (T0)
     {
B
blueswir1 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        /* Copy IEEE 754 flags into FSR */
        if (T0 & float_flag_invalid)
            env->fsr |= FSR_NVC;
        if (T0 & float_flag_overflow)
            env->fsr |= FSR_OFC;
        if (T0 & float_flag_underflow)
            env->fsr |= FSR_UFC;
        if (T0 & float_flag_divbyzero)
            env->fsr |= FSR_DZC;
        if (T0 & 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;
        }
58 59 60
     }
}

B
bellard 已提交
61 62 63
#ifdef USE_INT_TO_FLOAT_HELPERS
void do_fitos(void)
{
64
    set_float_exception_flags(0, &env->fp_status);
T
ths 已提交
65
    FT0 = int32_to_float32(*((int32_t *)&FT1), &env->fp_status);
66
    check_ieee_exceptions();
B
bellard 已提交
67 68 69 70
}

void do_fitod(void)
{
T
ths 已提交
71
    DT0 = int32_to_float64(*((int32_t *)&FT1), &env->fp_status);
B
bellard 已提交
72
}
B
blueswir1 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#ifdef TARGET_SPARC64
void do_fxtos(void)
{
    set_float_exception_flags(0, &env->fp_status);
    FT0 = int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
    check_ieee_exceptions();
}

void do_fxtod(void)
{
    set_float_exception_flags(0, &env->fp_status);
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
    check_ieee_exceptions();
}
#endif
B
bellard 已提交
88 89 90
#endif

void do_fabss(void)
91
{
B
bellard 已提交
92
    FT0 = float32_abs(FT1);
93 94
}

B
bellard 已提交
95 96 97 98 99 100 101
#ifdef TARGET_SPARC64
void do_fabsd(void)
{
    DT0 = float64_abs(DT1);
}
#endif

B
bellard 已提交
102
void do_fsqrts(void)
103
{
104
    set_float_exception_flags(0, &env->fp_status);
B
bellard 已提交
105
    FT0 = float32_sqrt(FT1, &env->fp_status);
106
    check_ieee_exceptions();
107 108
}

B
bellard 已提交
109
void do_fsqrtd(void)
110
{
111
    set_float_exception_flags(0, &env->fp_status);
B
bellard 已提交
112
    DT0 = float64_sqrt(DT1, &env->fp_status);
113
    check_ieee_exceptions();
114 115
}

116
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
B
bellard 已提交
117 118 119 120 121 122
    void glue(do_, name) (void)                                         \
    {                                                                   \
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
            T0 = (FSR_FCC1 | FSR_FCC0) << FS;                           \
123
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
B
bellard 已提交
124
                env->fsr |= T0;                                         \
125 126
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
B
bellard 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
            T0 = FSR_FCC0 << FS;                                        \
            break;                                                      \
        case float_relation_greater:                                    \
            T0 = FSR_FCC1 << FS;                                        \
            break;                                                      \
        default:                                                        \
            T0 = 0;                                                     \
            break;                                                      \
        }                                                               \
        env->fsr |= T0;                                                 \
143 144
    }

145 146 147 148 149
GEN_FCMP(fcmps, float32, FT0, FT1, 0, 0);
GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);

GEN_FCMP(fcmpes, float32, FT0, FT1, 0, 1);
GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
B
bellard 已提交
150 151

#ifdef TARGET_SPARC64
152 153 154 155 156 157 158 159 160 161 162
GEN_FCMP(fcmps_fcc1, float32, FT0, FT1, 22, 0);
GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);

GEN_FCMP(fcmps_fcc2, float32, FT0, FT1, 24, 0);
GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);

GEN_FCMP(fcmps_fcc3, float32, FT0, FT1, 26, 0);
GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);

GEN_FCMP(fcmpes_fcc1, float32, FT0, FT1, 22, 1);
GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
B
bellard 已提交
163

164 165
GEN_FCMP(fcmpes_fcc2, float32, FT0, FT1, 24, 1);
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
B
bellard 已提交
166

167 168
GEN_FCMP(fcmpes_fcc3, float32, FT0, FT1, 26, 1);
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
B
bellard 已提交
169 170 171
#endif

#ifndef TARGET_SPARC64
172
#ifndef CONFIG_USER_ONLY
173 174 175 176 177 178 179 180 181 182 183 184 185

#ifdef DEBUG_MXCC
static void dump_mxcc(CPUState *env)
{
    printf("mxccdata: %016llx %016llx %016llx %016llx\n",
        env->mxccdata[0], env->mxccdata[1], env->mxccdata[2], env->mxccdata[3]);
    printf("mxccregs: %016llx %016llx %016llx %016llx\n"
           "          %016llx %016llx %016llx %016llx\n",
        env->mxccregs[0], env->mxccregs[1], env->mxccregs[2], env->mxccregs[3],
        env->mxccregs[4], env->mxccregs[5], env->mxccregs[6], env->mxccregs[7]);
}
#endif

B
bellard 已提交
186
void helper_ld_asi(int asi, int size, int sign)
187
{
B
bellard 已提交
188
    uint32_t ret = 0;
189
    uint64_t tmp;
190 191 192
#ifdef DEBUG_MXCC
    uint32_t last_T0 = T0;
#endif
B
bellard 已提交
193 194

    switch (asi) {
195
    case 2: /* SuperSparc MXCC registers */
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        switch (T0) {
        case 0x01c00a00: /* MXCC control register */
            if (size == 8) {
                ret = env->mxccregs[3];
                T0 = env->mxccregs[3] >> 32;
            } else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                ret = env->mxccregs[3];
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8) {
                ret = env->mxccregs[7];
                T0 = env->mxccregs[7] >> 32;
            } else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        default:
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", T0, size);
            break;
        }
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, T0 = %08x -> ret = %08x,"
                     "T0 = %08x\n", asi, size, sign, last_T0, ret, T0);
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
226
        break;
227
    case 3: /* MMU probe */
B
blueswir1 已提交
228 229 230 231 232 233 234 235 236 237
        {
            int mmulev;

            mmulev = (T0 >> 8) & 15;
            if (mmulev > 4)
                ret = 0;
            else {
                ret = mmu_probe(env, T0, mmulev);
                //bswap32s(&ret);
            }
238
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08x\n", T0, mmulev, ret);
B
blueswir1 已提交
239 240
        }
        break;
241
    case 4: /* read MMU regs */
B
blueswir1 已提交
242 243
        {
            int reg = (T0 >> 8) & 0xf;
244

B
blueswir1 已提交
245 246 247
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
                env->mmuregs[reg] = 0;
248
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08x\n", reg, ret);
B
blueswir1 已提交
249 250
        }
        break;
251 252 253 254 255 256 257 258 259 260 261 262 263
    case 9: /* Supervisor code access */
        switch(size) {
        case 1:
            ret = ldub_code(T0);
            break;
        case 2:
            ret = lduw_code(T0 & ~1);
            break;
        default:
        case 4:
            ret = ldl_code(T0 & ~3);
            break;
        case 8:
264 265 266
            tmp = ldq_code(T0 & ~7);
            ret = tmp >> 32;
            T0 = tmp & 0xffffffff;
267 268 269
            break;
        }
        break;
270 271 272 273 274 275 276 277 278 279 280 281 282
    case 0xa: /* User data access */
        switch(size) {
        case 1:
            ret = ldub_user(T0);
            break;
        case 2:
            ret = lduw_user(T0 & ~1);
            break;
        default:
        case 4:
            ret = ldl_user(T0 & ~3);
            break;
        case 8:
283 284 285
            tmp = ldq_user(T0 & ~7);
            ret = tmp >> 32;
            T0 = tmp & 0xffffffff;
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
            ret = ldub_kernel(T0);
            break;
        case 2:
            ret = lduw_kernel(T0 & ~1);
            break;
        default:
        case 4:
            ret = ldl_kernel(T0 & ~3);
            break;
        case 8:
302 303 304
            tmp = ldq_kernel(T0 & ~7);
            ret = tmp >> 32;
            T0 = tmp & 0xffffffff;
305 306 307
            break;
        }
        break;
308 309 310 311 312 313
    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 已提交
314 315 316 317 318 319 320 321 322 323 324
        switch(size) {
        case 1:
            ret = ldub_phys(T0);
            break;
        case 2:
            ret = lduw_phys(T0 & ~1);
            break;
        default:
        case 4:
            ret = ldl_phys(T0 & ~3);
            break;
B
bellard 已提交
325
        case 8:
326 327 328
            tmp = ldq_phys(T0 & ~7);
            ret = tmp >> 32;
            T0 = tmp & 0xffffffff;
B
blueswir1 已提交
329
            break;
B
bellard 已提交
330
        }
B
blueswir1 已提交
331
        break;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    case 0x2e: /* MMU passthrough, 0xexxxxxxxx */
    case 0x2f: /* MMU passthrough, 0xfxxxxxxxx */
        switch(size) {
        case 1:
            ret = ldub_phys((target_phys_addr_t)T0
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 2:
            ret = lduw_phys((target_phys_addr_t)(T0 & ~1)
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        default:
        case 4:
            ret = ldl_phys((target_phys_addr_t)(T0 & ~3)
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 8:
349
            tmp = ldq_phys((target_phys_addr_t)(T0 & ~7)
350
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
351 352
            ret = tmp >> 32;
            T0 = tmp & 0xffffffff;
B
blueswir1 已提交
353
            break;
354
        }
B
blueswir1 已提交
355
        break;
356
    case 0x21 ... 0x2d: /* MMU passthrough, unassigned */
357
    default:
358
        do_unassigned_access(T0, 0, 0, 1);
B
blueswir1 已提交
359 360
        ret = 0;
        break;
361
    }
362 363 364 365
    if (sign) {
        switch(size) {
        case 1:
            T1 = (int8_t) ret;
B
blueswir1 已提交
366
            break;
367 368
        case 2:
            T1 = (int16_t) ret;
B
blueswir1 已提交
369
            break;
370 371 372 373 374 375 376
        default:
            T1 = ret;
            break;
        }
    }
    else
        T1 = ret;
377 378
}

379
void helper_st_asi(int asi, int size)
380 381
{
    switch(asi) {
382
    case 2: /* SuperSparc MXCC registers */
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
        switch (T0) {
        case 0x01c00000: /* MXCC stream data register 0 */
            if (size == 8)
                env->mxccdata[0] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00008: /* MXCC stream data register 1 */
            if (size == 8)
                env->mxccdata[1] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00010: /* MXCC stream data register 2 */
            if (size == 8)
                env->mxccdata[2] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00018: /* MXCC stream data register 3 */
            if (size == 8)
                env->mxccdata[3] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00100: /* MXCC stream source */
            if (size == 8)
                env->mxccregs[0] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, 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);
            break;
        case 0x01c00200: /* MXCC stream destination */
            if (size == 8)
                env->mxccregs[1] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, 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]);
            break;
        case 0x01c00a00: /* MXCC control register */
            if (size == 8)
                env->mxccregs[3] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                env->mxccregs[3] = (env->mxccregs[0xa] & 0xffffffff00000000) | T1;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00e00: /* MXCC error register  */
            if (size == 8)
                env->mxccregs[6] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            if (env->mxccregs[6] == 0xffffffffffffffffULL) {
                // this is probably a reset
            }
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8)
                env->mxccregs[7] = ((uint64_t)T1 << 32) | T2;
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        default:
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", T0, size);
            break;
        }
        DPRINTF_MXCC("asi = %d, size = %d, T0 = %08x, T1 = %08x\n", asi, size, T0, T1);
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
463
        break;
464
    case 3: /* MMU flush */
B
blueswir1 已提交
465 466
        {
            int mmulev;
B
bellard 已提交
467

B
blueswir1 已提交
468
            mmulev = (T0 >> 8) & 15;
469
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
B
blueswir1 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482
            switch (mmulev) {
            case 0: // flush page
                tlb_flush_page(env, T0 & 0xfffff000);
                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 已提交
483
#ifdef DEBUG_MMU
B
blueswir1 已提交
484
            dump_mmu(env);
B
bellard 已提交
485
#endif
B
blueswir1 已提交
486 487
            return;
        }
488
    case 4: /* write MMU regs */
B
blueswir1 已提交
489 490 491
        {
            int reg = (T0 >> 8) & 0xf;
            uint32_t oldreg;
492

B
blueswir1 已提交
493
            oldreg = env->mmuregs[reg];
B
bellard 已提交
494 495
            switch(reg) {
            case 0:
496 497
                env->mmuregs[reg] &= ~(MMU_E | MMU_NF | env->mmu_bm);
                env->mmuregs[reg] |= T1 & (MMU_E | MMU_NF | env->mmu_bm);
B
blueswir1 已提交
498 499
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
B
bellard 已提交
500
                if (oldreg != env->mmuregs[reg])
B
bellard 已提交
501 502 503
                    tlb_flush(env, 1);
                break;
            case 2:
B
blueswir1 已提交
504
                env->mmuregs[reg] = T1;
B
bellard 已提交
505 506 507 508 509 510 511 512 513 514
                if (oldreg != env->mmuregs[reg]) {
                    /* we flush when the MMU context changes because
                       QEMU has no MMU context support */
                    tlb_flush(env, 1);
                }
                break;
            case 3:
            case 4:
                break;
            default:
B
blueswir1 已提交
515
                env->mmuregs[reg] = T1;
B
bellard 已提交
516 517 518
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
519
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n", reg, oldreg, env->mmuregs[reg]);
B
bellard 已提交
520
            }
521
#ifdef DEBUG_MMU
B
blueswir1 已提交
522
            dump_mmu(env);
B
bellard 已提交
523
#endif
B
blueswir1 已提交
524 525
            return;
        }
526 527 528 529 530 531 532 533 534 535 536 537 538
    case 0xa: /* User data access */
        switch(size) {
        case 1:
            stb_user(T0, T1);
            break;
        case 2:
            stw_user(T0 & ~1, T1);
            break;
        default:
        case 4:
            stl_user(T0 & ~3, T1);
            break;
        case 8:
539
            stq_user(T0 & ~7, ((uint64_t)T1 << 32) | T2);
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
            stb_kernel(T0, T1);
            break;
        case 2:
            stw_kernel(T0 & ~1, T1);
            break;
        default:
        case 4:
            stl_kernel(T0 & ~3, T1);
            break;
        case 8:
556
            stq_kernel(T0 & ~7, ((uint64_t)T1 << 32) | T2);
557 558 559
            break;
        }
        break;
560 561 562 563 564 565 566 567 568 569
    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 已提交
570
    case 0x17: /* Block copy, sta access */
B
blueswir1 已提交
571 572 573 574
        {
            // value (T1) = src
            // address (T0) = dst
            // copy 32 bytes
575 576
            unsigned int i;
            uint32_t src = T1 & ~3, dst = T0 & ~3, temp;
577

578 579 580 581
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
B
blueswir1 已提交
582 583
        }
        return;
B
bellard 已提交
584
    case 0x1f: /* Block fill, stda access */
B
blueswir1 已提交
585 586 587 588
        {
            // value (T1, T2)
            // address (T0) = dst
            // fill 32 bytes
589 590 591
            unsigned int i;
            uint32_t dst = T0 & 7;
            uint64_t val;
B
bellard 已提交
592

593 594 595 596
            val = (((uint64_t)T1) << 32) | T2;

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
597 598
        }
        return;
599
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
600
        {
B
bellard 已提交
601 602 603 604 605 606 607 608 609 610 611
            switch(size) {
            case 1:
                stb_phys(T0, T1);
                break;
            case 2:
                stw_phys(T0 & ~1, T1);
                break;
            case 4:
            default:
                stl_phys(T0 & ~3, T1);
                break;
B
bellard 已提交
612
            case 8:
613
                stq_phys(T0 & ~7, ((uint64_t)T1 << 32) | T2);
B
bellard 已提交
614
                break;
B
bellard 已提交
615
            }
B
blueswir1 已提交
616 617
        }
        return;
618 619
    case 0x2e: /* MMU passthrough, 0xexxxxxxxx */
    case 0x2f: /* MMU passthrough, 0xfxxxxxxxx */
B
blueswir1 已提交
620
        {
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
            switch(size) {
            case 1:
                stb_phys((target_phys_addr_t)T0
                         | ((target_phys_addr_t)(asi & 0xf) << 32), T1);
                break;
            case 2:
                stw_phys((target_phys_addr_t)(T0 & ~1)
                            | ((target_phys_addr_t)(asi & 0xf) << 32), T1);
                break;
            case 4:
            default:
                stl_phys((target_phys_addr_t)(T0 & ~3)
                           | ((target_phys_addr_t)(asi & 0xf) << 32), T1);
                break;
            case 8:
636 637 638
                stq_phys((target_phys_addr_t)(T0 & ~7)
                           | ((target_phys_addr_t)(asi & 0xf) << 32),
                         ((uint64_t)T1 << 32) | T2);
639 640
                break;
            }
B
blueswir1 已提交
641 642
        }
        return;
643 644 645 646 647
    case 0x31: /* Ross RT620 I-cache flush */
    case 0x36: /* I-cache flash clear */
    case 0x37: /* D-cache flash clear */
        break;
    case 9: /* Supervisor code access, XXX */
648
    case 0x21 ... 0x2d: /* MMU passthrough, unassigned */
649
    default:
650
        do_unassigned_access(T0, 1, 0, 1);
B
blueswir1 已提交
651
        return;
652 653 654
    }
}

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
#endif /* CONFIG_USER_ONLY */
#else /* TARGET_SPARC64 */

#ifdef CONFIG_USER_ONLY
void helper_ld_asi(int asi, int size, int sign)
{
    uint64_t ret = 0;

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

    switch (asi) {
    case 0x80: // Primary
    case 0x82: // Primary no-fault
    case 0x88: // Primary LE
    case 0x8a: // Primary no-fault LE
        {
            switch(size) {
            case 1:
                ret = ldub_raw(T0);
                break;
            case 2:
                ret = lduw_raw(T0 & ~1);
                break;
            case 4:
                ret = ldl_raw(T0 & ~3);
                break;
            default:
            case 8:
                ret = ldq_raw(T0 & ~7);
                break;
            }
        }
        break;
    case 0x81: // Secondary
    case 0x83: // Secondary no-fault
    case 0x89: // Secondary LE
    case 0x8b: // Secondary no-fault LE
        // 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 已提交
708
            break;
709 710
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
711
            break;
712 713
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
714
            break;
715 716 717 718 719 720 721 722 723 724 725 726
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
727
            break;
728 729
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
730
            break;
731 732
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
733
            break;
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
        default:
            break;
        }
    }
    T1 = ret;
}

void helper_st_asi(int asi, int size)
{
    if (asi < 0x80)
        raise_exception(TT_PRIV_ACT);

    /* Convert to little endian */
    switch (asi) {
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
            T0 = bswap16(T0);
B
blueswir1 已提交
753
            break;
754 755
        case 4:
            T0 = bswap32(T0);
B
blueswir1 已提交
756
            break;
757 758
        case 8:
            T0 = bswap64(T0);
B
blueswir1 已提交
759
            break;
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
        default:
            break;
        }
    default:
        break;
    }

    switch(asi) {
    case 0x80: // Primary
    case 0x88: // Primary LE
        {
            switch(size) {
            case 1:
                stb_raw(T0, T1);
                break;
            case 2:
                stw_raw(T0 & ~1, T1);
                break;
            case 4:
                stl_raw(T0 & ~3, T1);
                break;
            case 8:
            default:
                stq_raw(T0 & ~7, T1);
                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:
        do_unassigned_access(T0, 1, 0, 1);
        return;
    }
}

#else /* CONFIG_USER_ONLY */
B
bellard 已提交
804 805 806

void helper_ld_asi(int asi, int size, int sign)
{
B
bellard 已提交
807
    uint64_t ret = 0;
B
bellard 已提交
808

B
blueswir1 已提交
809
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
B
blueswir1 已提交
810
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
811
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
812 813

    switch (asi) {
814 815 816 817 818 819 820
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x82: // Primary no-fault
    case 0x88: // Primary LE
    case 0x8a: // Primary no-fault LE
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
B
blueswir1 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
            if (env->hpstate & HS_PRIV) {
                switch(size) {
                case 1:
                    ret = ldub_hypv(T0);
                    break;
                case 2:
                    ret = lduw_hypv(T0 & ~1);
                    break;
                case 4:
                    ret = ldl_hypv(T0 & ~3);
                    break;
                default:
                case 8:
                    ret = ldq_hypv(T0 & ~7);
                    break;
                }
            } else {
                switch(size) {
                case 1:
                    ret = ldub_kernel(T0);
                    break;
                case 2:
                    ret = lduw_kernel(T0 & ~1);
                    break;
                case 4:
                    ret = ldl_kernel(T0 & ~3);
                    break;
                default:
                case 8:
                    ret = ldq_kernel(T0 & ~7);
                    break;
                }
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
            }
        } else {
            switch(size) {
            case 1:
                ret = ldub_user(T0);
                break;
            case 2:
                ret = lduw_user(T0 & ~1);
                break;
            case 4:
                ret = ldl_user(T0 & ~3);
                break;
            default:
            case 8:
                ret = ldq_user(T0 & ~7);
                break;
            }
        }
        break;
B
bellard 已提交
872 873
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
874 875
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
876
        {
B
bellard 已提交
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
            switch(size) {
            case 1:
                ret = ldub_phys(T0);
                break;
            case 2:
                ret = lduw_phys(T0 & ~1);
                break;
            case 4:
                ret = ldl_phys(T0 & ~3);
                break;
            default:
            case 8:
                ret = ldq_phys(T0 & ~7);
                break;
            }
B
blueswir1 已提交
892 893
            break;
        }
B
bellard 已提交
894 895 896 897 898 899 900
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x11: // As if user secondary
    case 0x19: // As if user secondary LE
    case 0x24: // Nucleus quad LDD 128 bit atomic
    case 0x2c: // Nucleus quad LDD 128 bit atomic
    case 0x4a: // UPA config
901
    case 0x81: // Secondary
B
bellard 已提交
902 903 904
    case 0x83: // Secondary no-fault
    case 0x89: // Secondary LE
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
905 906
        // XXX
        break;
B
bellard 已提交
907
    case 0x45: // LSU
B
blueswir1 已提交
908 909
        ret = env->lsu;
        break;
B
bellard 已提交
910
    case 0x50: // I-MMU regs
B
blueswir1 已提交
911 912
        {
            int reg = (T0 >> 3) & 0xf;
B
bellard 已提交
913

B
blueswir1 已提交
914 915 916
            ret = env->immuregs[reg];
            break;
        }
B
bellard 已提交
917 918 919
    case 0x51: // I-MMU 8k TSB pointer
    case 0x52: // I-MMU 64k TSB pointer
    case 0x55: // I-MMU data access
B
blueswir1 已提交
920 921
        // XXX
        break;
B
bellard 已提交
922
    case 0x56: // I-MMU tag read
B
blueswir1 已提交
923 924 925 926 927 928 929 930 931 932 933 934 935
        {
            unsigned int i;

            for (i = 0; i < 64; i++) {
                // Valid, ctx match, vaddr match
                if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0 &&
                    env->itlb_tag[i] == T0) {
                    ret = env->itlb_tag[i];
                    break;
                }
            }
            break;
        }
B
bellard 已提交
936
    case 0x58: // D-MMU regs
B
blueswir1 已提交
937 938
        {
            int reg = (T0 >> 3) & 0xf;
B
bellard 已提交
939

B
blueswir1 已提交
940 941 942
            ret = env->dmmuregs[reg];
            break;
        }
B
bellard 已提交
943
    case 0x5e: // D-MMU tag read
B
blueswir1 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956
        {
            unsigned int i;

            for (i = 0; i < 64; i++) {
                // Valid, ctx match, vaddr match
                if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0 &&
                    env->dtlb_tag[i] == T0) {
                    ret = env->dtlb_tag[i];
                    break;
                }
            }
            break;
        }
B
bellard 已提交
957 958 959 960
    case 0x59: // D-MMU 8k TSB pointer
    case 0x5a: // D-MMU 64k TSB pointer
    case 0x5b: // D-MMU data pointer
    case 0x5d: // D-MMU data access
B
bellard 已提交
961 962 963
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
964 965
        // XXX
        break;
B
bellard 已提交
966 967 968 969
    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 已提交
970
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
971
    default:
972
        do_unassigned_access(T0, 0, 0, 1);
B
blueswir1 已提交
973 974
        ret = 0;
        break;
B
bellard 已提交
975
    }
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

    /* 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 已提交
991
            break;
992 993
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
994
            break;
995 996
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
997
            break;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
1010
            break;
1011 1012
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
1013
            break;
1014 1015
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1016
            break;
1017 1018 1019 1020
        default:
            break;
        }
    }
B
bellard 已提交
1021 1022 1023
    T1 = ret;
}

1024
void helper_st_asi(int asi, int size)
B
bellard 已提交
1025
{
B
blueswir1 已提交
1026
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
B
blueswir1 已提交
1027
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
1028
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
1029

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    /* 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:
            T0 = bswap16(T0);
B
blueswir1 已提交
1042
            break;
1043 1044
        case 4:
            T0 = bswap32(T0);
B
blueswir1 已提交
1045
            break;
1046 1047
        case 8:
            T0 = bswap64(T0);
B
blueswir1 已提交
1048
            break;
1049 1050 1051 1052 1053 1054 1055
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
1056
    switch(asi) {
1057 1058 1059 1060 1061
    case 0x10: // As if user primary
    case 0x18: // As if user primary LE
    case 0x80: // Primary
    case 0x88: // Primary LE
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
B
blueswir1 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
            if (env->hpstate & HS_PRIV) {
                switch(size) {
                case 1:
                    stb_hypv(T0, T1);
                    break;
                case 2:
                    stw_hypv(T0 & ~1, T1);
                    break;
                case 4:
                    stl_hypv(T0 & ~3, T1);
                    break;
                case 8:
                default:
                    stq_hypv(T0 & ~7, T1);
                    break;
                }
            } else {
                switch(size) {
                case 1:
                    stb_kernel(T0, T1);
                    break;
                case 2:
                    stw_kernel(T0 & ~1, T1);
                    break;
                case 4:
                    stl_kernel(T0 & ~3, T1);
                    break;
                case 8:
                default:
                    stq_kernel(T0 & ~7, T1);
                    break;
                }
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
            }
        } else {
            switch(size) {
            case 1:
                stb_user(T0, T1);
                break;
            case 2:
                stw_user(T0 & ~1, T1);
                break;
            case 4:
                stl_user(T0 & ~3, T1);
                break;
            case 8:
            default:
                stq_user(T0 & ~7, T1);
                break;
            }
        }
        break;
B
bellard 已提交
1113 1114
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
1115 1116
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
1117
        {
B
bellard 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
            switch(size) {
            case 1:
                stb_phys(T0, T1);
                break;
            case 2:
                stw_phys(T0 & ~1, T1);
                break;
            case 4:
                stl_phys(T0 & ~3, T1);
                break;
            case 8:
            default:
                stq_phys(T0 & ~7, T1);
                break;
            }
B
blueswir1 已提交
1133 1134
        }
        return;
B
bellard 已提交
1135 1136 1137 1138 1139 1140 1141
    case 0x04: // Nucleus
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x11: // As if user secondary
    case 0x19: // As if user secondary LE
    case 0x24: // Nucleus quad LDD 128 bit atomic
    case 0x2c: // Nucleus quad LDD 128 bit atomic
    case 0x4a: // UPA config
B
blueswir1 已提交
1142
    case 0x81: // Secondary
B
bellard 已提交
1143
    case 0x89: // Secondary LE
B
blueswir1 已提交
1144 1145
        // XXX
        return;
B
bellard 已提交
1146
    case 0x45: // LSU
B
blueswir1 已提交
1147 1148 1149 1150 1151 1152 1153 1154
        {
            uint64_t oldreg;

            oldreg = env->lsu;
            env->lsu = T1 & (DMMU_E | IMMU_E);
            // Mappings generated during D/I MMU disabled mode are
            // invalid in normal mode
            if (oldreg != env->lsu) {
1155
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", oldreg, env->lsu);
B
bellard 已提交
1156
#ifdef DEBUG_MMU
B
blueswir1 已提交
1157
                dump_mmu(env);
B
bellard 已提交
1158
#endif
B
blueswir1 已提交
1159 1160 1161 1162
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
1163
    case 0x50: // I-MMU regs
B
blueswir1 已提交
1164 1165 1166
        {
            int reg = (T0 >> 3) & 0xf;
            uint64_t oldreg;
1167

B
blueswir1 已提交
1168
            oldreg = env->immuregs[reg];
B
bellard 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
            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 已提交
1179 1180
                if ((T1 & 1) == 0)
                    T1 = 0; // Clear SFSR
B
bellard 已提交
1181 1182 1183 1184 1185 1186
                break;
            case 5: // TSB access
            case 6: // Tag access
            default:
                break;
            }
B
blueswir1 已提交
1187
            env->immuregs[reg] = T1;
B
bellard 已提交
1188
            if (oldreg != env->immuregs[reg]) {
1189
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
B
bellard 已提交
1190
            }
1191
#ifdef DEBUG_MMU
B
blueswir1 已提交
1192
            dump_mmu(env);
B
bellard 已提交
1193
#endif
B
blueswir1 已提交
1194 1195
            return;
        }
B
bellard 已提交
1196
    case 0x54: // I-MMU data in
B
blueswir1 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
        {
            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];
                    env->itlb_tte[i] = T1;
                    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];
                    env->itlb_tte[i] = T1;
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
1219
    case 0x55: // I-MMU data access
B
blueswir1 已提交
1220 1221
        {
            unsigned int i = (T0 >> 3) & 0x3f;
B
bellard 已提交
1222

B
blueswir1 已提交
1223 1224 1225 1226
            env->itlb_tag[i] = env->immuregs[6];
            env->itlb_tte[i] = T1;
            return;
        }
B
bellard 已提交
1227
    case 0x57: // I-MMU demap
B
blueswir1 已提交
1228 1229
        // XXX
        return;
B
bellard 已提交
1230
    case 0x58: // D-MMU regs
B
blueswir1 已提交
1231 1232 1233
        {
            int reg = (T0 >> 3) & 0xf;
            uint64_t oldreg;
1234

B
blueswir1 已提交
1235
            oldreg = env->dmmuregs[reg];
B
bellard 已提交
1236 1237 1238 1239 1240
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
B
blueswir1 已提交
1241 1242 1243 1244 1245
                if ((T1 & 1) == 0) {
                    T1 = 0; // Clear SFSR, Fault address
                    env->dmmuregs[4] = 0;
                }
                env->dmmuregs[reg] = T1;
B
bellard 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
                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 已提交
1256
            env->dmmuregs[reg] = T1;
B
bellard 已提交
1257
            if (oldreg != env->dmmuregs[reg]) {
1258
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
B
bellard 已提交
1259
            }
1260
#ifdef DEBUG_MMU
B
blueswir1 已提交
1261
            dump_mmu(env);
B
bellard 已提交
1262
#endif
B
blueswir1 已提交
1263 1264
            return;
        }
B
bellard 已提交
1265
    case 0x5c: // D-MMU data in
B
blueswir1 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
        {
            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];
                    env->dtlb_tte[i] = T1;
                    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];
                    env->dtlb_tte[i] = T1;
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
1288
    case 0x5d: // D-MMU data access
B
blueswir1 已提交
1289 1290
        {
            unsigned int i = (T0 >> 3) & 0x3f;
B
bellard 已提交
1291

B
blueswir1 已提交
1292 1293 1294 1295
            env->dtlb_tag[i] = env->dmmuregs[6];
            env->dtlb_tte[i] = T1;
            return;
        }
B
bellard 已提交
1296
    case 0x5f: // D-MMU demap
B
bellard 已提交
1297
    case 0x49: // Interrupt data receive
B
blueswir1 已提交
1298 1299
        // XXX
        return;
B
bellard 已提交
1300 1301 1302 1303 1304 1305 1306
    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 已提交
1307 1308 1309 1310 1311 1312
    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 已提交
1313
    default:
1314
        do_unassigned_access(T0, 1, 0, 1);
B
blueswir1 已提交
1315
        return;
B
bellard 已提交
1316 1317
    }
}
1318
#endif /* CONFIG_USER_ONLY */
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

void helper_ldf_asi(int asi, int size, int rd)
{
    target_ulong tmp_T0 = T0, tmp_T1 = T1;
    unsigned int i;

    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 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
        if (T0 & 0x3f) {
            raise_exception(TT_UNALIGNED);
            return;
        }
        for (i = 0; i < 16; i++) {
            helper_ld_asi(asi & 0x8f, 4, 0);
            *(uint32_t *)&env->fpr[rd++] = T1;
            T0 += 4;
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
        }
        T0 = tmp_T0;
        T1 = tmp_T1;

        return;
    default:
        break;
    }

    helper_ld_asi(asi, size, 0);
    switch(size) {
    default:
    case 4:
        *((uint32_t *)&FT0) = T1;
        break;
    case 8:
        *((int64_t *)&DT0) = T1;
        break;
    }
    T1 = tmp_T1;
}

void helper_stf_asi(int asi, int size, int rd)
{
    target_ulong tmp_T0 = T0, tmp_T1 = T1;
    unsigned int i;

    switch (asi) {
    case 0xf0: // Block store primary
    case 0xf1: // Block store secondary
    case 0xf8: // Block store primary LE
    case 0xf9: // Block store secondary LE
B
blueswir1 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
        if (T0 & 0x3f) {
            raise_exception(TT_UNALIGNED);
            return;
        }
        for (i = 0; i < 16; i++) {
            T1 = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(asi & 0x8f, 4);
            T0 += 4;
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
        }
        T0 = tmp_T0;
        T1 = tmp_T1;

        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
        T1 = *((uint32_t *)&FT0);
        break;
    case 8:
        T1 = *((int64_t *)&DT0);
        break;
    }
    helper_st_asi(asi, size);
    T1 = tmp_T1;
}

1408
#endif /* TARGET_SPARC64 */
B
bellard 已提交
1409 1410

#ifndef TARGET_SPARC64
B
bellard 已提交
1411
void helper_rett()
1412
{
1413 1414
    unsigned int cwp;

1415 1416 1417
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

1418
    env->psret = 1;
1419
    cwp = (env->cwp + 1) & (NWINDOWS - 1);
1420 1421 1422 1423 1424 1425
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
1426
#endif
1427

B
bellard 已提交
1428
void helper_ldfsr(void)
1429
{
B
bellard 已提交
1430
    int rnd_mode;
1431 1432
    switch (env->fsr & FSR_RD_MASK) {
    case FSR_RD_NEAREST:
B
bellard 已提交
1433
        rnd_mode = float_round_nearest_even;
B
blueswir1 已提交
1434
        break;
B
bellard 已提交
1435
    default:
1436
    case FSR_RD_ZERO:
B
bellard 已提交
1437
        rnd_mode = float_round_to_zero;
B
blueswir1 已提交
1438
        break;
1439
    case FSR_RD_POS:
B
bellard 已提交
1440
        rnd_mode = float_round_up;
B
blueswir1 已提交
1441
        break;
1442
    case FSR_RD_NEG:
B
bellard 已提交
1443
        rnd_mode = float_round_down;
B
blueswir1 已提交
1444
        break;
1445
    }
B
bellard 已提交
1446
    set_float_rounding_mode(rnd_mode, &env->fp_status);
1447
}
B
bellard 已提交
1448 1449 1450 1451 1452 1453

void helper_debug()
{
    env->exception_index = EXCP_DEBUG;
    cpu_loop_exit();
}
1454

B
bellard 已提交
1455
#ifndef TARGET_SPARC64
1456 1457
void do_wrpsr()
{
1458 1459 1460 1461
    if ((T0 & PSR_CWP) >= NWINDOWS)
        raise_exception(TT_ILL_INSN);
    else
        PUT_PSR(env, T0);
1462 1463 1464 1465 1466 1467
}

void do_rdpsr()
{
    T0 = GET_PSR(env);
}
B
bellard 已提交
1468 1469 1470 1471 1472

#else

void do_popc()
{
B
blueswir1 已提交
1473
    T0 = ctpop64(T1);
B
bellard 已提交
1474
}
B
bellard 已提交
1475 1476 1477 1478 1479 1480

static inline uint64_t *get_gregset(uint64_t pstate)
{
    switch (pstate) {
    default:
    case 0:
B
blueswir1 已提交
1481
        return env->bgregs;
B
bellard 已提交
1482
    case PS_AG:
B
blueswir1 已提交
1483
        return env->agregs;
B
bellard 已提交
1484
    case PS_MG:
B
blueswir1 已提交
1485
        return env->mgregs;
B
bellard 已提交
1486
    case PS_IG:
B
blueswir1 已提交
1487
        return env->igregs;
B
bellard 已提交
1488 1489 1490
    }
}

1491
static inline void change_pstate(uint64_t new_pstate)
B
bellard 已提交
1492
{
1493
    uint64_t pstate_regs, new_pstate_regs;
B
bellard 已提交
1494 1495 1496 1497 1498
    uint64_t *src, *dst;

    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
    if (new_pstate_regs != pstate_regs) {
B
blueswir1 已提交
1499 1500 1501 1502 1503
        // 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 已提交
1504 1505 1506 1507
    }
    env->pstate = new_pstate;
}

1508 1509 1510 1511 1512
void do_wrpstate(void)
{
    change_pstate(T0 & 0xf3f);
}

B
bellard 已提交
1513 1514 1515 1516 1517 1518 1519
void do_done(void)
{
    env->tl--;
    env->pc = env->tnpc[env->tl];
    env->npc = env->tnpc[env->tl] + 4;
    PUT_CCR(env, env->tstate[env->tl] >> 32);
    env->asi = (env->tstate[env->tl] >> 24) & 0xff;
1520
    change_pstate((env->tstate[env->tl] >> 8) & 0xf3f);
1521
    PUT_CWP64(env, env->tstate[env->tl] & 0xff);
B
bellard 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530
}

void do_retry(void)
{
    env->tl--;
    env->pc = env->tpc[env->tl];
    env->npc = env->tnpc[env->tl];
    PUT_CCR(env, env->tstate[env->tl] >> 32);
    env->asi = (env->tstate[env->tl] >> 24) & 0xff;
1531
    change_pstate((env->tstate[env->tl] >> 8) & 0xf3f);
1532
    PUT_CWP64(env, env->tstate[env->tl] & 0xff);
B
bellard 已提交
1533
}
B
bellard 已提交
1534
#endif
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572

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

void cpu_set_cwp(CPUState *env1, int new_cwp)
{
    CPUState *saved_env;
#ifdef reg_REGWPTR
    target_ulong *saved_regwptr;
#endif

    saved_env = env;
#ifdef reg_REGWPTR
    saved_regwptr = REGWPTR;
#endif
    env = env1;
    set_cwp(new_cwp);
    env = saved_env;
#ifdef reg_REGWPTR
    REGWPTR = saved_regwptr;
#endif
}

#ifdef TARGET_SPARC64
void do_interrupt(int intno)
{
#ifdef DEBUG_PCALL
    if (loglevel & CPU_LOG_INT) {
B
blueswir1 已提交
1573 1574
        static int count;
        fprintf(logfile, "%6d: v=%04x pc=%016" PRIx64 " npc=%016" PRIx64 " SP=%016" PRIx64 "\n",
1575 1576 1577
                count, intno,
                env->pc,
                env->npc, env->regwptr[6]);
B
blueswir1 已提交
1578
        cpu_dump_state(env, logfile, fprintf, 0);
1579
#if 0
B
blueswir1 已提交
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
        {
            int i;
            uint8_t *ptr;

            fprintf(logfile, "       code=");
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
                fprintf(logfile, " %02x", ldub(ptr + i));
            }
            fprintf(logfile, "\n");
        }
1591
#endif
B
blueswir1 已提交
1592
        count++;
1593 1594
    }
#endif
1595
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
1596
    if (env->tl == MAXTL) {
B
bellard 已提交
1597
        cpu_abort(env, "Trap 0x%04x while trap level is MAXTL, Error state", env->exception_index);
B
blueswir1 已提交
1598
        return;
1599 1600 1601
    }
#endif
    env->tstate[env->tl] = ((uint64_t)GET_CCR(env) << 32) | ((env->asi & 0xff) << 24) |
B
blueswir1 已提交
1602
        ((env->pstate & 0xf3f) << 8) | GET_CWP64(env);
1603 1604 1605
    env->tpc[env->tl] = env->pc;
    env->tnpc[env->tl] = env->npc;
    env->tt[env->tl] = intno;
1606 1607 1608 1609 1610 1611 1612 1613
    change_pstate(PS_PEF | PS_PRIV | PS_AG);

    if (intno == TT_CLRWIN)
        set_cwp((env->cwp - 1) & (NWINDOWS - 1));
    else if ((intno & 0x1c0) == TT_SPILL)
        set_cwp((env->cwp - env->cansave - 2) & (NWINDOWS - 1));
    else if ((intno & 0x1c0) == TT_FILL)
        set_cwp((env->cwp + 1) & (NWINDOWS - 1));
B
bellard 已提交
1614 1615 1616
    env->tbr &= ~0x7fffULL;
    env->tbr |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
    if (env->tl < MAXTL - 1) {
B
blueswir1 已提交
1617
        env->tl++;
B
bellard 已提交
1618
    } else {
B
blueswir1 已提交
1619 1620 1621
        env->pstate |= PS_RED;
        if (env->tl != MAXTL)
            env->tl++;
B
bellard 已提交
1622
    }
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
    env->pc = env->tbr;
    env->npc = env->pc + 4;
    env->exception_index = 0;
}
#else
void do_interrupt(int intno)
{
    int cwp;

#ifdef DEBUG_PCALL
    if (loglevel & CPU_LOG_INT) {
B
blueswir1 已提交
1634 1635
        static int count;
        fprintf(logfile, "%6d: v=%02x pc=%08x npc=%08x SP=%08x\n",
1636 1637 1638
                count, intno,
                env->pc,
                env->npc, env->regwptr[6]);
B
blueswir1 已提交
1639
        cpu_dump_state(env, logfile, fprintf, 0);
1640
#if 0
B
blueswir1 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
        {
            int i;
            uint8_t *ptr;

            fprintf(logfile, "       code=");
            ptr = (uint8_t *)env->pc;
            for(i = 0; i < 16; i++) {
                fprintf(logfile, " %02x", ldub(ptr + i));
            }
            fprintf(logfile, "\n");
        }
1652
#endif
B
blueswir1 已提交
1653
        count++;
1654 1655
    }
#endif
1656
#if !defined(CONFIG_USER_ONLY)
1657
    if (env->psret == 0) {
B
bellard 已提交
1658
        cpu_abort(env, "Trap 0x%02x while interrupts disabled, Error state", env->exception_index);
B
blueswir1 已提交
1659
        return;
1660 1661 1662
    }
#endif
    env->psret = 0;
1663
    cwp = (env->cwp - 1) & (NWINDOWS - 1);
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
    set_cwp(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;
}
#endif

1676
#if !defined(CONFIG_USER_ONLY)
1677

1678 1679 1680
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

1681
#define MMUSUFFIX _mmu
1682
#define ALIGNED_ONLY
1683 1684 1685 1686 1687
#ifdef __s390__
# define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
#else
# define GETPC() (__builtin_return_address(0))
#endif
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700

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

1701 1702 1703
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr)
{
B
blueswir1 已提交
1704 1705 1706 1707
#ifdef DEBUG_UNALIGNED
    printf("Unaligned access to 0x%x from 0x%x\n", addr, env->pc);
#endif
    raise_exception(TT_UNALIGNED);
1708
}
1709 1710 1711 1712 1713

/* 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 */
1714
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
{
    TranslationBlock *tb;
    int ret;
    unsigned long pc;
    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;

1726
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    if (ret) {
        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 *)T2);
            }
        }
        cpu_loop_exit();
    }
    env = saved_env;
}

#endif
1744 1745

#ifndef TARGET_SPARC64
1746
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
1747 1748 1749 1750 1751 1752 1753 1754 1755
                          int is_asi)
{
    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;
    if (env->mmuregs[3]) /* Fault status register */
B
blueswir1 已提交
1756
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
    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)) {
#ifdef DEBUG_UNASSIGNED
1769
        printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
1770 1771
               "\n", addr, env->pc);
#endif
1772 1773 1774 1775
        if (is_exec)
            raise_exception(TT_CODE_ACCESS);
        else
            raise_exception(TT_DATA_ACCESS);
1776 1777 1778 1779
    }
    env = saved_env;
}
#else
1780
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
1781 1782 1783 1784 1785 1786 1787 1788 1789
                          int is_asi)
{
#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;
1790
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx "\n",
1791 1792 1793
           addr, env->pc);
    env = saved_env;
#endif
1794 1795 1796 1797
    if (is_exec)
        raise_exception(TT_CODE_ACCESS);
    else
        raise_exception(TT_DATA_ACCESS);
1798 1799
}
#endif
1800 1801 1802 1803

#ifdef TARGET_SPARC64
void do_tick_set_count(void *opaque, uint64_t count)
{
B
blueswir1 已提交
1804
#if !defined(CONFIG_USER_ONLY)
1805
    ptimer_set_count(opaque, -count);
B
blueswir1 已提交
1806
#endif
1807 1808 1809 1810
}

uint64_t do_tick_get_count(void *opaque)
{
B
blueswir1 已提交
1811
#if !defined(CONFIG_USER_ONLY)
1812
    return -ptimer_get_count(opaque);
B
blueswir1 已提交
1813 1814 1815
#else
    return 0;
#endif
1816 1817 1818 1819
}

void do_tick_set_limit(void *opaque, uint64_t limit)
{
B
blueswir1 已提交
1820
#if !defined(CONFIG_USER_ONLY)
1821
    ptimer_set_limit(opaque, -limit, 0);
B
blueswir1 已提交
1822
#endif
1823 1824
}
#endif