op_helper.c 49.2 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
        switch (T0) {
        case 0x01c00a00: /* MXCC control register */
            if (size == 8) {
199 200
                ret = env->mxccregs[3] >> 32;
                T0 = env->mxccregs[3];
201 202 203 204 205 206 207 208 209
            } 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;
210 211 212 213 214 215 216 217
        case 0x01c00c00: /* Module reset register */
            if (size == 8) {
                ret = env->mxccregs[5] >> 32;
                T0 = env->mxccregs[5];
                // should we do something here?
            } else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
218 219
        case 0x01c00f00: /* MBus port address register */
            if (size == 8) {
220 221
                ret = env->mxccregs[7] >> 32;
                T0 = env->mxccregs[7];
222 223 224 225 226 227 228 229 230 231 232 233
            } 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
234
        break;
235
    case 3: /* MMU probe */
B
blueswir1 已提交
236 237 238 239 240 241 242 243 244 245
        {
            int mmulev;

            mmulev = (T0 >> 8) & 15;
            if (mmulev > 4)
                ret = 0;
            else {
                ret = mmu_probe(env, T0, mmulev);
                //bswap32s(&ret);
            }
246
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08x\n", T0, mmulev, ret);
B
blueswir1 已提交
247 248
        }
        break;
249
    case 4: /* read MMU regs */
B
blueswir1 已提交
250 251
        {
            int reg = (T0 >> 8) & 0xf;
252

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

387
void helper_st_asi(int asi, int size)
388 389
{
    switch(asi) {
390
    case 2: /* SuperSparc MXCC registers */
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
        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)
B
bellard 已提交
444
                env->mxccregs[3] = (env->mxccregs[0xa] & 0xffffffff00000000ULL) | T1;
445 446 447 448
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00e00: /* MXCC error register  */
449
            // writing a 1 bit clears the error
450
            if (size == 8)
451
                env->mxccregs[6] &= ~(((uint64_t)T1 << 32) | T2);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            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
469
        break;
470
    case 3: /* MMU flush */
B
blueswir1 已提交
471 472
        {
            int mmulev;
B
bellard 已提交
473

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

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

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

599 600 601 602
            val = (((uint64_t)T1) << 32) | T2;

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
603 604
        }
        return;
605
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
606
        {
B
bellard 已提交
607 608 609 610 611 612 613 614 615 616 617
            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 已提交
618
            case 8:
619
                stq_phys(T0 & ~7, ((uint64_t)T1 << 32) | T2);
B
bellard 已提交
620
                break;
B
bellard 已提交
621
            }
B
blueswir1 已提交
622 623
        }
        return;
624 625
    case 0x2e: /* MMU passthrough, 0xexxxxxxxx */
    case 0x2f: /* MMU passthrough, 0xfxxxxxxxx */
B
blueswir1 已提交
626
        {
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
            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:
642 643 644
                stq_phys((target_phys_addr_t)(T0 & ~7)
                           | ((target_phys_addr_t)(asi & 0xf) << 32),
                         ((uint64_t)T1 << 32) | T2);
645 646
                break;
            }
B
blueswir1 已提交
647 648
        }
        return;
649 650 651 652 653
    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 */
654
    case 0x21 ... 0x2d: /* MMU passthrough, unassigned */
655
    default:
656
        do_unassigned_access(T0, 1, 0, 1);
B
blueswir1 已提交
657
        return;
658 659 660
    }
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
#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 已提交
714
            break;
715 716
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
717
            break;
718 719
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
720
            break;
721 722 723 724 725 726 727 728 729 730 731 732
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
733
            break;
734 735
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
736
            break;
737 738
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
739
            break;
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
        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 已提交
759
            break;
760 761
        case 4:
            T0 = bswap32(T0);
B
blueswir1 已提交
762
            break;
763 764
        case 8:
            T0 = bswap64(T0);
B
blueswir1 已提交
765
            break;
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 804 805 806 807 808 809
        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 已提交
810 811 812

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

B
blueswir1 已提交
815
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
B
blueswir1 已提交
816
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
817
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
818 819

    switch (asi) {
820 821 822 823 824 825 826
    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 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
            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;
                }
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
            }
        } 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 已提交
878 879
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
880 881
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
882
        {
B
bellard 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
            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 已提交
898 899
            break;
        }
B
bellard 已提交
900 901 902 903 904 905 906
    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
907
    case 0x81: // Secondary
B
bellard 已提交
908 909 910
    case 0x83: // Secondary no-fault
    case 0x89: // Secondary LE
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
911 912
        // XXX
        break;
B
bellard 已提交
913
    case 0x45: // LSU
B
blueswir1 已提交
914 915
        ret = env->lsu;
        break;
B
bellard 已提交
916
    case 0x50: // I-MMU regs
B
blueswir1 已提交
917 918
        {
            int reg = (T0 >> 3) & 0xf;
B
bellard 已提交
919

B
blueswir1 已提交
920 921 922
            ret = env->immuregs[reg];
            break;
        }
B
bellard 已提交
923 924 925
    case 0x51: // I-MMU 8k TSB pointer
    case 0x52: // I-MMU 64k TSB pointer
    case 0x55: // I-MMU data access
B
blueswir1 已提交
926 927
        // XXX
        break;
B
bellard 已提交
928
    case 0x56: // I-MMU tag read
B
blueswir1 已提交
929 930 931 932 933 934 935 936 937 938 939 940 941
        {
            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 已提交
942
    case 0x58: // D-MMU regs
B
blueswir1 已提交
943 944
        {
            int reg = (T0 >> 3) & 0xf;
B
bellard 已提交
945

B
blueswir1 已提交
946 947 948
            ret = env->dmmuregs[reg];
            break;
        }
B
bellard 已提交
949
    case 0x5e: // D-MMU tag read
B
blueswir1 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962
        {
            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 已提交
963 964 965 966
    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 已提交
967 968 969
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
970 971
        // XXX
        break;
B
bellard 已提交
972 973 974 975
    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 已提交
976
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
977
    default:
978
        do_unassigned_access(T0, 0, 0, 1);
B
blueswir1 已提交
979 980
        ret = 0;
        break;
B
bellard 已提交
981
    }
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996

    /* 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 已提交
997
            break;
998 999
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
1000
            break;
1001 1002
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
1003
            break;
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
1016
            break;
1017 1018
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
1019
            break;
1020 1021
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1022
            break;
1023 1024 1025 1026
        default:
            break;
        }
    }
B
bellard 已提交
1027 1028 1029
    T1 = ret;
}

1030
void helper_st_asi(int asi, int size)
B
bellard 已提交
1031
{
B
blueswir1 已提交
1032
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
B
blueswir1 已提交
1033
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
1034
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    /* 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 已提交
1048
            break;
1049 1050
        case 4:
            T0 = bswap32(T0);
B
blueswir1 已提交
1051
            break;
1052 1053
        case 8:
            T0 = bswap64(T0);
B
blueswir1 已提交
1054
            break;
1055 1056 1057 1058 1059 1060 1061
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
1062
    switch(asi) {
1063 1064 1065 1066 1067
    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 已提交
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 1094 1095 1096 1097 1098 1099
            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;
                }
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
            }
        } 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 已提交
1119 1120
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
1121 1122
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
1123
        {
B
bellard 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
            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 已提交
1139 1140
        }
        return;
B
bellard 已提交
1141 1142 1143 1144 1145 1146 1147
    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 已提交
1148
    case 0x81: // Secondary
B
bellard 已提交
1149
    case 0x89: // Secondary LE
B
blueswir1 已提交
1150 1151
        // XXX
        return;
B
bellard 已提交
1152
    case 0x45: // LSU
B
blueswir1 已提交
1153 1154 1155 1156 1157 1158 1159 1160
        {
            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) {
1161
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", oldreg, env->lsu);
B
bellard 已提交
1162
#ifdef DEBUG_MMU
B
blueswir1 已提交
1163
                dump_mmu(env);
B
bellard 已提交
1164
#endif
B
blueswir1 已提交
1165 1166 1167 1168
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
1169
    case 0x50: // I-MMU regs
B
blueswir1 已提交
1170 1171 1172
        {
            int reg = (T0 >> 3) & 0xf;
            uint64_t oldreg;
1173

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

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

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

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

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 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
        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;
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 1374 1375 1376 1377 1378 1379
        }
        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 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
        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;
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
        }
        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;
}

1414
#endif /* TARGET_SPARC64 */
B
bellard 已提交
1415 1416

#ifndef TARGET_SPARC64
B
bellard 已提交
1417
void helper_rett()
1418
{
1419 1420
    unsigned int cwp;

1421 1422 1423
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

1424
    env->psret = 1;
1425
    cwp = (env->cwp + 1) & (NWINDOWS - 1);
1426 1427 1428 1429 1430 1431
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
1432
#endif
1433

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

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

B
bellard 已提交
1461
#ifndef TARGET_SPARC64
1462 1463
void do_wrpsr()
{
1464 1465 1466 1467
    if ((T0 & PSR_CWP) >= NWINDOWS)
        raise_exception(TT_ILL_INSN);
    else
        PUT_PSR(env, T0);
1468 1469 1470 1471 1472 1473
}

void do_rdpsr()
{
    T0 = GET_PSR(env);
}
B
bellard 已提交
1474 1475 1476 1477 1478

#else

void do_popc()
{
B
blueswir1 已提交
1479
    T0 = ctpop64(T1);
B
bellard 已提交
1480
}
B
bellard 已提交
1481 1482 1483 1484 1485 1486

static inline uint64_t *get_gregset(uint64_t pstate)
{
    switch (pstate) {
    default:
    case 0:
B
blueswir1 已提交
1487
        return env->bgregs;
B
bellard 已提交
1488
    case PS_AG:
B
blueswir1 已提交
1489
        return env->agregs;
B
bellard 已提交
1490
    case PS_MG:
B
blueswir1 已提交
1491
        return env->mgregs;
B
bellard 已提交
1492
    case PS_IG:
B
blueswir1 已提交
1493
        return env->igregs;
B
bellard 已提交
1494 1495 1496
    }
}

1497
static inline void change_pstate(uint64_t new_pstate)
B
bellard 已提交
1498
{
1499
    uint64_t pstate_regs, new_pstate_regs;
B
bellard 已提交
1500 1501 1502 1503 1504
    uint64_t *src, *dst;

    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
    if (new_pstate_regs != pstate_regs) {
B
blueswir1 已提交
1505 1506 1507 1508 1509
        // 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 已提交
1510 1511 1512 1513
    }
    env->pstate = new_pstate;
}

1514 1515 1516 1517 1518
void do_wrpstate(void)
{
    change_pstate(T0 & 0xf3f);
}

B
bellard 已提交
1519 1520 1521 1522 1523 1524 1525
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;
1526
    change_pstate((env->tstate[env->tl] >> 8) & 0xf3f);
1527
    PUT_CWP64(env, env->tstate[env->tl] & 0xff);
B
bellard 已提交
1528 1529 1530 1531 1532 1533 1534 1535 1536
}

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;
1537
    change_pstate((env->tstate[env->tl] >> 8) & 0xf3f);
1538
    PUT_CWP64(env, env->tstate[env->tl] & 0xff);
B
bellard 已提交
1539
}
B
bellard 已提交
1540
#endif
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

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 已提交
1579 1580
        static int count;
        fprintf(logfile, "%6d: v=%04x pc=%016" PRIx64 " npc=%016" PRIx64 " SP=%016" PRIx64 "\n",
1581 1582 1583
                count, intno,
                env->pc,
                env->npc, env->regwptr[6]);
B
blueswir1 已提交
1584
        cpu_dump_state(env, logfile, fprintf, 0);
1585
#if 0
B
blueswir1 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
        {
            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");
        }
1597
#endif
B
blueswir1 已提交
1598
        count++;
1599 1600
    }
#endif
1601
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
1602
    if (env->tl == MAXTL) {
B
bellard 已提交
1603
        cpu_abort(env, "Trap 0x%04x while trap level is MAXTL, Error state", env->exception_index);
B
blueswir1 已提交
1604
        return;
1605 1606 1607
    }
#endif
    env->tstate[env->tl] = ((uint64_t)GET_CCR(env) << 32) | ((env->asi & 0xff) << 24) |
B
blueswir1 已提交
1608
        ((env->pstate & 0xf3f) << 8) | GET_CWP64(env);
1609 1610 1611
    env->tpc[env->tl] = env->pc;
    env->tnpc[env->tl] = env->npc;
    env->tt[env->tl] = intno;
1612 1613 1614 1615 1616 1617 1618 1619
    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 已提交
1620 1621 1622
    env->tbr &= ~0x7fffULL;
    env->tbr |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
    if (env->tl < MAXTL - 1) {
B
blueswir1 已提交
1623
        env->tl++;
B
bellard 已提交
1624
    } else {
B
blueswir1 已提交
1625 1626 1627
        env->pstate |= PS_RED;
        if (env->tl != MAXTL)
            env->tl++;
B
bellard 已提交
1628
    }
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
    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 已提交
1640 1641
        static int count;
        fprintf(logfile, "%6d: v=%02x pc=%08x npc=%08x SP=%08x\n",
1642 1643 1644
                count, intno,
                env->pc,
                env->npc, env->regwptr[6]);
B
blueswir1 已提交
1645
        cpu_dump_state(env, logfile, fprintf, 0);
1646
#if 0
B
blueswir1 已提交
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
        {
            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");
        }
1658
#endif
B
blueswir1 已提交
1659
        count++;
1660 1661
    }
#endif
1662
#if !defined(CONFIG_USER_ONLY)
1663
    if (env->psret == 0) {
B
bellard 已提交
1664
        cpu_abort(env, "Trap 0x%02x while interrupts disabled, Error state", env->exception_index);
B
blueswir1 已提交
1665
        return;
1666 1667 1668
    }
#endif
    env->psret = 0;
1669
    cwp = (env->cwp - 1) & (NWINDOWS - 1);
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
    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

1682
#if !defined(CONFIG_USER_ONLY)
1683

1684 1685 1686
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

1687
#define MMUSUFFIX _mmu
1688
#define ALIGNED_ONLY
1689 1690 1691 1692 1693
#ifdef __s390__
# define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
#else
# define GETPC() (__builtin_return_address(0))
#endif
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706

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

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

/* 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 */
1720
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
{
    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;

1732
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
    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
1750 1751

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